Agent backend
xcodon has one agent loop of its own: LangGraph via Deep Agents (run_deepagents_loop in agent/deepagents_loop.py). A plan-mode run drives the checklist with it. A direct-mode run (agent/direct_mode.py) has no plan and calls the same loop once per conversation. With direct_mode_agent set, a direct run sends those conversations to an outside coding agent over the Agent Client Protocol (ACP) instead. See Direct mode and ACP agents.
Runtime overview
| Concern | Implementation |
|---|---|
| Entry | run_deepagents_loop in agent/deepagents_loop.py |
| Orchestration | create_deep_agent (agent/deepagents_integration.py) |
| Primary state | state/deepagents.sqlite (LangGraph thread) + state/checkpoint.json bridge for TUI Chat tab |
| Session bootstrap | begin_agent_session in agent/session.py |
| Tool dispatch | _dispatch_tool in agent/tool_dispatch.py. Object arguments sent as JSON text are parsed first; a call the tool schema still rejects reaches dispatch, which returns the reason as the tool result |
| Chat model | build_deepagents_chat_model (agent/deepagents_integration.py): agent_reasoning_effort (default high), llm_temperature (default 0.2; omitted for models that refuse it), llm_tool_choice (default auto: no tool choice, except required once after a turn that answered in prose; a model or provider that refuses a forced choice gets it unforced) |
| Repeat guard / loop breaker | agent/tool_repeat_guard.py blocks repeated identical calls; agent/loop_breaker.py ends the graph run after three blocks of one call and collapses repeated call/result pairs in what the model sees |
| Context compression | xcodon's summarizer (agent/compaction.py) takes the place of Deep Agents' built-in SummarizationMiddleware; it compacts at compaction_trigger_fraction (0.8) of the context budget and backs the on-demand compact_conversation tool |
| XCodon compaction hooks | XCodonCompactionHooksMiddleware → outputs/plan_context_digest.md, harness compaction log, memory hooks |
Manual /compact | force_compact_deepagents_thread compacts the LangGraph sqlite thread (not checkpoint.json alone) |
| Per-round tool digest | agent/run_inbox.py adds it as a HumanMessage before the next model call, once every tool call has its result. round_digest_enabled: false turns it off; cheap_looks: true (default) does so for plan-mode runs, and direct runs have none |
| Request log | agent/request_log.py saves the system prompt and tool schemas of each agent model call to state/llm_requests.jsonl. Unchanged text is stored once; read_request_log reads it back. Keys and endpoints are never written |
| Reflection | Post-turn run_reflection_pass; optional reflection_on_step_limit on recursion limit |
| HITL / execution gate | build_interrupt_on_map + LangGraph interrupt_on (see parity note below) |
| Harness | HarnessIncrementalSync + flush_harness_from_langgraph_values; per-step deterministic gate (agent/step_gate.py, harness_gate_enabled) → LLM judge for analysis/synthesis steps (agent/harness_validation_llm.py, harness_judge_enabled) → optional solution-LLM revision; one end-of-run referee review (agent/final_referee_review.py, harness_final_review_enabled) that may append checklist steps |
| Provenance | agent/provenance.py writes state/provenance/ledger.jsonl (tools.trace_executions); agent/provenance_checks.py turns it into the tool_ran, tool_unused, lineage, workload and source_used gate rows |
| Step outcomes | agent/step_blocking.py (blocked steps, fabrication quarantine), agent/tool_substitutes.py (declared substitutes), agent/block_review.py (judge review of blocks and substitutes), agent/stuck_detector.py (unresolved steps), agent/stale_steps.py (reopen stale steps) |
| Skills | Deep Agents SkillsMiddleware (omit_skill_digest=True in system prompt — intentional vs native skill digest); research skills in three tiers (plan_skill_context.py, skills_bootstrap.ON_DEMAND_BUNDLED_SKILLS); live library skill prefetch gate in tool_dispatch.py |
Context compression
Deep Agents registers SummarizationMiddleware on every create_deep_agent call. build_session_middleware (agent/deepagents_loop.py) passes xcodon's own middleware:
xcodon_compaction_middleware(agent/compaction.py) — a summarizer with the same name as the built-in one, so Deep Agents swaps it in. It writes a handoff recap with fixed sections. ASummarizationToolMiddlewareon the same summarizer exposescompact_conversation, so the model can compact on demand mid-turn. TUI/compactis a separate path (force_compact_deepagents_thread, see the table above).XCodonCompactionHooksMiddleware— on each new_summarization_event, appends toplan_context_digest.md, logs harness compaction, and runs compaction memory hooks.XCodonToolPairRepairMiddleware(agent/message_tool_repair.py) — repairs orphaned assistant tool-call /ToolMessagepairs before the request is sent.
Compaction config keys:
context_budget_tokens(default 200,000;0uses the full window). The budget is the smaller of this and the model's context window.compaction_trigger_fraction(default 0.8). The recap keeps the most recent 15% of the budget verbatim.model_context_window_tokens— the window for a model LiteLLM does not know. Without it, xcodon assumes 128,000 tokens.
Still used for /codon plan:
plan_context_digest_max_chars,plan_context_digest_file_max_chars,plan_digest_substance_min_chars
Validation pipeline (gate → judge → referee)
Each completed checklist step passes a deterministic gate first (agent/step_gate.py). Its families fall under three questions: data (artifact, declared_check, row_key, quarantine, fabrication, lineage, workload, source_used, data_facts, id_values), method (restriction, tool_ran, tool_unused, visual_check, method_test) and outcome (expected_number, output_consistency, claim, deliverable, expected_coverage). Each family is one entry in agent/rules/gate_rules.yaml; a project's harness_rules.yaml turns single checks on or off. A red gate is reported back to the agent as a table with no LLM call. Rows from tool_ran, tool_unused, lineage, workload, source_used and fabrication are evidence: a step red only on them still goes to the judge, and the judge's verdict replaces the rows, a pass included (harness_tidy.py, _EVIDENCE_FAMILIES). One exception: a judge's pass does not clear a tool_ran row that says the tool ran only as a dry run. Green analysis / synthesis steps then go to the LLM judge (agent/harness_validation_llm.py), with harness_judge_max_rounds solution-LLM revisions after a fail_fixable. harness_judge_mechanical_steps (default false) also judges mechanical steps. Before the run may stop, one referee review (agent/final_referee_review.py) reads the graded deliverables and can only append checklist steps for a missing piece of the investigation.
Budgets. The continuation budget per user message is harness_gate_max_rounds + harness_judge_max_rounds (deepagents_loop.py, _validation_continuation_budget); harness_validation_max_continuations applies only when the gate is disabled. One step is sent back at most VALIDATION_ROUNDS_PER_STEP (2) times (harness_validation_continue.py), so at the default harness_gate_max_rounds: 3 that cap ends a step's rounds first. A fabrication finding gets its own FABRICATION_ROUNDS_PER_STEP (3) rounds outside the budget (step_blocking.py); then xcodon blocks the step and quarantines the files its runs wrote. A step out of rounds, or one whose failure returns with the same fingerprint (stuck_detector.py), is closed as unresolved: it stays done so the run goes on, and run_manifest.json lists it under unresolved_steps. User-facing detail: Step validation.
Direct mode and ACP agents
A direct run has no plan and no checklist steps, so the gate, the judge and the referee do not run. run_direct (agent/direct_mode.py) runs a fixed set of conversations instead.
| Concern | Implementation |
|---|---|
| Switch | direct_mode: true (headless runs; xcodon init --run-mode direct). The TUI and web setup set it from run_mode (project_init.set_run_mode_for_subject) |
| Entry | run_direct, or run_direct_async for the TUI and the web chat |
| Work turn | One run_deepagents_loop call, at most direct_mode_steps (150) model calls. Short system prompt (system_parts), three tools (TOOL_NAMES: coala_execute_python, coala_execute_r, run_shell), plain script output, no round digest. direct_mode_tool_search (default true) adds coala_search and coala_invoke |
| First message | The task, then the method rules of the bundled skills scientific-basics and analysis-method-selection (direct_mode_method_rules), then domain skills to read (agent/direct_skills.py, direct_mode_domain_skills) |
| Reviews | One side call lists the conditions the task names. direct_mode_reviews: each reviews each condition in a fresh conversation (direct_mode_condition_steps, 20), then runs the last review (direct_mode_check_steps, 30). one runs only the last review. auto (default) picks one for an ACP agent and each for xcodon's own loop. A task that names no condition gets no review |
| Hard facts | hard_fact_problems checks that each named deliverable exists and reads, and that its identifiers come from the inputs. A failure goes back to the conversation at most direct_mode_fix_rounds (2) times |
| State | state/direct_mode.json keeps the stage, so a stopped run resumes. A finished conversation moves to state/direct_work/. At the end, a one-step outputs/research_plan.md records the run |
| Plan-mode reviews | plan_review: true (default) gives a finished headless plan-mode run the same condition reviews (agent/plan_review.py) |
| ACP agent | direct_mode_agent names a /codon handoff preset with protocol: acp, such as the built-in antigravity (runs agy acp). A model id acp/<preset> does the same (AgentProjectConfig.acp_agent). AcpWorker (agent/acp_worker.py) then sends every turn and side call to that agent. No LiteLLM model or key is needed |
| ACP client | tools/acp_handoff.py (acp_prompt, run_acp_handoff) starts the agent in the project root, opens a session, sends the prompt, and collects the reply, tool calls and permission answers. /codon antigravity uses it too |
| ACP tools | agent/acp_tools_server.py serves xcodon's script tools, and coala_search / coala_invoke with tool search, as an MCP (Model Context Protocol) stdio server. Each call goes through _dispatch_tool. A preset with xcodon_tools: false leaves it out |
| ACP limits | An agent reports no model calls, so a turn's call budget becomes a time limit (direct_mode_agent_sec_per_step, 60 s per call). The turn is also cancelled at the tool call after its budget |
| ACP records | state/acp_session.json (session id), state/acp_turns.md (each turn in full), state/acp_stderr.log, state/acp_turn_index.json, and outputs/run_manifest.json (no token counts). Each turn also goes to the project notebook |
HITL parity (native vs Deep Agents)
| Mechanism | Native (removed) | Deep Agents |
|---|---|---|
| Pre-LLM tool filtering | filter_tools_for_execution_approval hid risky tools until user unlock | The function still runs inside begin_agent_session (session.py), but the deepagents loop discards that snapshot and binds the unfiltered tool list; HumanInTheLoopMiddleware interrupts before side-effect calls |
| Side-effect tool set | Read-only builtins + skills until unlock | _DEEP_SIDE_EFFECT_TOOLS + _xcodon_side_effect_tool_names in build_interrupt_on_map |
| Unlock signal | Prior-turn-aware (run it always; proceed only after fenced plan) | Same via user_unlocked_execution(..., prior_messages=…) → approve vs reject interrupt decisions |
| Super mode | Disables both gates | Same — empty interrupt_on map |
Gap: Native hid tools from the schema entirely pre-unlock; Deep Agents shows tools but blocks execution via interrupt. Behavior is equivalent for safety; UX differs (model may plan writes before interrupt).
Historical note
Before plan 013, xcodon maintained a native LiteLLM loop (run_agent_loop in agent/loop.py) selectable via backend: native. That path was retired; shared code lives in agent/session.py, agent/tool_dispatch.py, and agent/progress.py.
Related tests
tests/test_deepagents_loop.pytests/test_deepagents_compaction_hooks.pytests/test_deepagents_hot_load_rebuild.pytests/test_deepagents_checkpoint_bridge.pytests/test_tool_dispatch_watch.py,tests/test_tool_gate.pytests/test_step_gate.py,tests/test_provenance.py,tests/test_provenance_checks.pytests/test_step_blocking.py,tests/test_block_review.py,tests/test_stuck_detector.py,tests/test_stale_steps.pytests/test_compaction.py,tests/test_run_inbox.py,tests/test_loop_breaker.py,tests/test_request_log.pytests/test_direct_mode.py,tests/test_direct_skills.py,tests/test_plan_review.pytests/test_acp_worker.py,tests/test_acp_handoff.py
