Setting up Alan Code for contributing.
- Python 3.11 or newer.
- Git.
- Optional: ripgrep (
rg) on your PATH for faster Grep tool performance during dev.
git clone https://github.com/<your-fork>/alan-code.git
cd alan-code
python -m venv venv
source venv/bin/activate
pip install -e '.[dev]'-e installs in editable mode — changes to alancode/ take effect immediately without reinstalling. [dev] pulls the test tooling (pytest, pytest-asyncio, pytest-cov, ruff).
After editable install:
alancode --version # reads from alancode/__version__.py
alancode # runs from your working copyEdit a file under alancode/, save, rerun — changes apply.
pytest -x -q-xstops at first failure. Useful when iterating.-qsuppresses per-test output. Drop for verbose per-test lines.
Run a specific file:
pytest tests/unit/test_compaction.pyRun tests matching a keyword:
pytest -k "hook or permission"With coverage:
pytest --cov=alancode --cov-report=term-missingSee contributing/testing.md for the test organisation.
ruff check .With auto-fix:
ruff check --fix .No separate formatter — ruff format is available but we don't enforce it. Match the surrounding style; PRs that reformat unrelated code will likely be pushed back on.
alan-code/
├── alancode/ # the package
│ ├── agent.py # AlanCodeAgent class
│ ├── query/ # query_loop + state
│ ├── backends/ # Anthropic, LiteLLM, Scripted
│ ├── tools/ # built-in tools + orchestration
│ ├── messages/ # message dataclasses + normalization
│ ├── session/ # session persistence, state, transcripts
│ ├── permissions/ # permission pipeline + rules
│ ├── compact/ # 3-layer compaction + deterministic fallback
│ ├── hooks/ # pre/post tool-use hooks
│ ├── memory/ # memory system
│ ├── skills/ # skill registry + parser
│ ├── cli/ # CLI entry point + REPL + display
│ ├── gui/ # browser GUI (FastAPI + WebSocket + static/)
│ ├── prompt/ # system prompt assembly
│ ├── api/ # retry, cost tracking
│ └── utils/ # atomic I/O, token counting, env helpers
├── tests/
│ ├── unit/ # fast, no-network, no-disk tests
│ ├── integration/ # full agent runs with scripted backend
│ └── conftest.py
├── examples/ # runnable example scripts
├── docs/ # these files
├── pyproject.toml
├── LICENSE
└── README.md
Start reading in alancode/query/loop.py::query_loop. See architecture/overview.md for the full subsystem map.
Keep standard-library and normal internal imports at module scope. A function-local import is reserved for a genuinely optional/heavy dependency (litellm, anthropic, requests, httpx, FastAPI/uvicorn UI pieces) or a documented circular-import boundary. Local imports otherwise hide dependencies, defer obvious failures until runtime, and make tests harder to patch consistently.
Catch exceptions at external boundaries where Alan must turn a failure into a model/user-visible result: SDK streams, filesystem tools, subprocesses, hooks, UI connections, and best-effort shutdown. Narrow the exception type when possible. If recovery or cleanup intentionally continues, log enough context at DEBUG or higher; do not silently swallow an actionable failure. Internal invariant failures should propagate to the agent boundary and retain their traceback.
For code that touches the agent loop or message handling, use the ScriptedBackend to write deterministic tests without hitting real APIs:
from alancode.backends.scripted_backend import ScriptedBackend, text, tool_call
backend = ScriptedBackend.from_responses([
tool_call("Bash", {"command": "ls"}),
text("Found 3 files"),
])
agent = AlanCodeAgent(backend=backend, permission_mode="yolo")See tests/integration/test_agent_loop.py for examples.
alancode --guiStatic assets at alancode/gui/static/ (HTML, JS, CSS). Browser caches them aggressively — hard-refresh (Ctrl+Shift+R / Cmd+Shift+R) after JS/CSS edits. A regular reload won't pick them up.
Edits to alancode/gui/server.py or gui_ui.py require restarting alancode (the Python side isn't hot-reloaded).
- Create a feature branch:
git checkout -b feat/my-feature. - Make the change. Keep commits small and focused.
- Run tests:
pytest -x -q. Fix regressions. - Run lint:
ruff check .. - If you changed the agent behaviour: run a real-model smoke test with
alancode --model openrouter/anthropic/claude-haiku-5.5 --permission-mode yoloin a scratch directory. - If you changed prompt behaviour: use
--guiand inspect the LLM Perspective panel to verify Alan's normalized pre-backend prompt/messages are what you expect. - Push and open a PR.
Look at git log --oneline -20 for current style. Generally:
- Short imperative mood ("add X", "fix Y", "rename Z").
- No scope prefixes (this isn't Conventional Commits).
- First line under 72 chars; expand in the body only if needed.
- Create
alancode/tools/builtin/my_tool.py, subclassTool, implementname,description,input_schema,permission_level,call. - Register in
alancode/tools/builtin/__init__.py(import + add toALL_BUILTIN_TOOLS). - Add schema tests in
tests/unit/test_tools.py. - Document in
docs/reference/tools.md.
- Add to
alancode/settings.py::SETTINGS_DEFAULTS. - Add a validator in
SETTING_VALIDATORSif the type needs checking. - Propagate to wherever it's consumed (usually
QueryParams→query_loop). - Add a CLI flag in
alancode/cli/main.pyif appropriate. - Document in
docs/reference/settings.md.
- Add the entry to
SLASH_COMMANDSdict inalancode/cli/repl.py. - Add a dispatch case in
_handle_slash_command. - Write
_handle_<command>function. - Add focused unit or integration coverage near the affected REPL behavior.
- Document in
docs/reference/slash-commands.md.
- contributing/testing.md — test organisation + conventions.
- contributing/release.md — PyPI release process.
- architecture/overview.md — the 10,000-ft system view.