This is a route through the running system, not a folder inventory. Use it when you need “where should I change this?” without cargo-culting a nearby file.
Pair with ARCHITECTURE.md for package rules and the coupling cleanup. This guide focuses on how to learn the code: timeboxed paths, end-to-end traces, exercises, and debugging recipes.
| After… | You should be able to… |
|---|---|
| 30 minutes | Name the main turn path and open the five critical files |
| 60 minutes | Trace one tool call from model output to JSONL to UI |
| 2 hours | Place a small change in the correct layer and name the tests to run |
- Skim ARCHITECTURE.md §§2–5 (mental model + one turn).
- Open these files in order (do not deep-read yet):
firstcoder/cli.pyfirstcoder/app/factory.py(create_firstcoder_app)firstcoder/app/runtime.py(AgentChatRunner)firstcoder/agent/loop.py(AgentLoop)firstcoder/session/bootstrap.py(SessionBootstrap)
- Run:
rg -n "class AgentLoop|def create_firstcoder_app|class SessionBootstrap" firstcoderPick one of:
- loop limits →
agent/loop_limits.py+ stop handling inloop.py - permission ask →
permissions/manager.py+ resume inAgentChatRunner - compaction trigger →
AgentLoophelpers +context/manager.py
For the chosen topic:
rg -n "keyword" firstcoder tests- Read the owning module, then one caller, then one test.
- Run the narrowest pytest file you found.
- Finish Path B.
- Read the matching design doc end-to-end (tools / permissions / context / …).
- Complete Exercise 1 and Exercise 2 below without editing product code first.
- Only then make a one-file change and re-run the focused tests.
utils/permissions/toolsimportfirstcoder.runtime, neverfirstcoder.agent.- Build sessions through
session.bootstrap.SessionBootstrap. - UI/CLI boundaries use
app.ports/agent.ports. - Hidden status tools live in
tools.hidden.HIDDEN_TOOL_STATUS_NAMES.
If a PR violates these, review architecture before debating style.
Trace a request such as “find the loop limit and explain it”:
firstcoder/cli.py
-> firstcoder/app/factory.py:create_firstcoder_app
SessionBootstrap.from_project
ContextWindowManager
AgentChatRunner
command router + FirstCoderApp
-> firstcoder/app/runtime.py:AgentChatRunner.run_user_turn
-> firstcoder/agent/loop.py:AgentLoop
-> ContextBuilder builds ChatRequest(messages, tools, system)
-> provider.complete / astream
-> session tool registry execute (+ permissions)
-> append facts to .firstcoder/sessions/<id>.jsonl
-> runtime events back to TUI / CLI
That trace teaches the central contract:
| Layer | Does | Does not |
|---|---|---|
| Loop | Coordinate the turn | Call vendor SDKs directly for business logic |
| Provider | Translate protocols | Persist sessions / decide permissions |
| Tools | Local side effects | Own final allow/deny policy |
| Context | Persist + project facts | Render widgets |
| App/TUI | Present + collect input | Reimplement tool execution |
| Area | Read first | Owns | Does not own |
|---|---|---|---|
runtime/ |
cancellation.py, user_input.py |
Shared cancel + user-input DTOs | Loop policy, UI |
app/ |
factory.py, runtime.py, tui.py, ports.py |
Wiring, ports, terminal UX | Model protocol translation |
agent/ |
loop.py, session.py, loop_limits.py |
One-turn orchestration | Concrete shell/HTTP |
context/ |
store.py, writer.py, context_builder.py, manager.py |
Durable facts + projection | UI widgets |
tools/ |
builtin.py, registry.py, session_registry.py |
Schemas, dispatch, session wrappers | Final permission policy |
permissions/ |
manager.py, policy.py, grants.py |
allow/ask/deny + grants | Executing a tool |
providers/ |
types.py, factory.py, adapters |
Internal ↔ vendor conversion | Session persistence |
skills/ |
discovery.py, router.py, loader.py |
Skill catalog + load audit | Tool registration |
session/ |
bootstrap.py, catalog.py, resume.py, fork.py |
Lifecycle services | Agent turn semantics |
mcp/ |
manager + client modules | External tools as first-class tools | Core loop control |
Use this instead of randomly opening large files.
Search the visible command, tool name, error string, or UI label:
rg -n "term" firstcoder testsAvoid guessing from filenames alone (manager.py is not unique enough).
Follow imports toward an interface or dataclass before an implementation:
ChatProviderTool/ registry protocolsContextManagerLikeChatRunnerLikePermissionRequest/UserInputRequest
Seams tell you what is allowed to change independently.
For one turn, keep these objects in view:
| Object | Why it matters |
|---|---|
ChatRequest |
What the model actually receives |
ChatResponse / stream events |
What came back |
ToolCall / ToolResult |
Local work unit |
| Session JSONL events | Durable truth |
UserInputRequest |
Why the turn paused |
AgentTurnResult |
What the UI was told |
If you only follow if branches, you will miss projection and resume bugs.
Tests specify error handling and invariants more precisely than happy-path code. Run the narrow test before editing:
.venv/bin/python -m pytest tests/test_whatever.py -qUse pytest tests or explicit paths—not a bare repository-wide pytest—because
generated benchmark trees may ship their own tests/ directories.
| Term | Meaning in FirstCoder |
|---|---|
| Turn | One user submission handled by AgentLoop until stop or pause |
| Fact | An append-only session event (or derived effective state after replay) |
| Projection | Provider-visible messages built from facts (ContextBuilder) |
| Compaction | Shrinking the projection; does not erase the audit log |
| Checkpoint | L4 handoff summary used as a new projection basis |
| Grant | Remembered allow decision for a scope |
| Port | Protocol boundary so UI/tests do not hard-bind implementations |
| Bootstrap | Single assembly path for a project-bound AgentSession |
| Hidden tool | Still callable; omitted from noisy human status streams |
| Bypass mode | A permission policy mode, not “no safety code” |
# orchestration & assembly
rg -n "class AgentLoop|def create_firstcoder_app|class SessionBootstrap" firstcoder tests
# request / tool / permission seams
rg -n "ChatRequest\(|tool_registry\.execute|preflight\(" firstcoder tests
# pause / resume human input
rg -n "UserInputRequest|resume_with_user_input|permission_confirmation" firstcoder tests
# compact triggers
rg -n "_auto_compact|compact_if_needed|ContextWindowTrigger" firstcoder tests
# full unit suite (from repo root)
.venv/bin/python -m pytest tests -q| You want… | Change… | Do not… |
|---|---|---|
| New vendor option | providers/ + config |
Patch the loop with vendor fields |
| New local capability | new Tool + permission spec |
Add ad-hoc if in AgentLoop |
| Different approval rule | permissions/policy.py or grants |
Hardcode allow in a tool |
| Different history visibility | context projection / compaction | Destructively edit JSONL |
| New slash command | app/ command handlers |
Bypass session services |
| Shared cancel/input DTO | runtime/ |
Import agent from tools |
“The system prompt is the whole context.”
No. It is a stable prefix. ContextBuilder projects session history separately;
tool schemas ride on ChatRequest.tools.
“Bypass means no safety code exists.”
No. Bypass is a policy mode. Execution still goes through the registry and
returns structured results.
“Compaction deleted history.”
No. JSONL facts remain; a checkpoint changes the current provider view.
“The TUI owns the session.”
No. The TUI displays and routes. Session facts live under context / .firstcoder.
“High out-degree means the module is badly designed.”
Not always. Factories and the agent loop should depend on many collaborators.
High in-degree on a leaf that imports upward is the smell.
“I can trust the model to resend the pending tool_call after approval.”
No. Resume must use the locally stored original call.
- Confirm
UserInputRequest.idmatches what the UI submits. - Trace
AgentChatRunner.resume_with_user_input. - Ensure the original
tool_callis restored from session/runtime state. - Tests:
rg -n "resume_with_user_input|permission_confirmation" tests.
- Did the call become a durable tool result event?
- Was the decision
ASKorDENY? - Did the executor raise and get normalized into a
ToolResult? - Follow
PermissionAwareToolRegistrythen the concrete tool module.
- Open
.firstcoder/sessions/<id>.jsonland check events actually appended. - Replay path: store → view →
ContextBuilder. - Look for checkpoint / compaction events that changed the projection.
- Confirm you did not expect process-local
SessionRuntimeStateto survive.
- Factory tool list: builtins vs
McpToolProvidermerge. - MCP connect happened? Background connect failures are easy to miss.
- Session registry vs global registry: session-scoped tools need
create_session_tool_registry.
- Read
agent/loop_limits.pyand the stop-reason enum. - Find where
AgentLooprecordstool_round_limit/provider_call_limit/turn_timeout. - Check whether the caller passed a non-default
AgentLoopLimitsprofile (default,swe_lite,summary, …).
Do these with the repo open. Prefer reading and tests over speculative edits.
- Open
firstcoder/agent/loop_limits.py. - Run:
rg -n "max_tool_rounds|TOOL_ROUND_LIMIT|AgentLoopLimits" tests firstcoder
.venv/bin/python -m pytest tests -q -k "loop_limit or tool_round or AgentLoopLimits" - Write (for yourself) one sentence: what stops, what is recorded, who sees it.
- List call sites:
rg -n "SessionBootstrap|AgentSession.create|AgentSession.resume" firstcoder- Confirm new/resume/fork/factory go through bootstrap for grants/skills/tools.
- If you find a parallel assembly path, treat it as debt—not as a template.
- Start from
PermissionManager.preflight/build_confirmation. - Find how
UserInputRequestreaches the TUI. - Find how the answer returns to tool execution.
- Note where the original
tool_callis stored.
- Read
ContextBuilder.build_provider_messages. - Find one compaction event type in
context/and the test that protects tool-call ordering. - Explain why deleting a JSONL line is not a valid “fix context overflow”.
| Concern | Good starting tests (search) |
|---|---|
| App assembly | tests/test_app_factory.py, tests/test_cli.py |
| TUI / runner | tests/test_app_tui.py, tests/test_app_runtime.py |
| Sessions | tests/test_session_*.py |
| Permissions | tests/test_permissions_manager.py, tests/test_permission_results.py |
| Context / compact | tests/test_context_*.py |
| Tools | tests/test_tools_*.py or tool-named files |
| Providers | tests/test_providers_*.py / adapter tests |
When a design doc and a test disagree, believe the test, then fix the doc in the same PR as the behavior change.
- ARCHITECTURE.md — boundaries and dependency rules
- CLI_TUI_DESIGN.md — startup, commands, streaming
- AGENT_LOOP_GUARDRAILS.md — stop/pause/continue
- CONTEXT_MANAGEMENT_DESIGN.md — facts & compact
- TOOLS_DESIGN.md / PERMISSIONS_DESIGN.md
- PROVIDERS_DESIGN.md / SKILL_SYSTEM_DESIGN.md
- docs/README.md — full index