Capstone 01 — Terminal-Native Coding Agent
A coding agent is a terminal harness around a model: a stateful plan, a sandboxed tool surface, and a loop that plans, acts, observes, and recovers. Claude Code, Cursor 3, and OpenCode all share that shape. You build one end to end, CLI in and pull request out, then measure it against mini-swe-agent and Live-SWE-agent on SWE-bench Pro. The model call turns out to be the easy part. The tool loop, the sandbox, and the cost ceiling on a 50-turn run are where the work is.
Learning Objectives
- Build a plan, act, observe, recover harness whose TodoWrite plan is rewritten whole each turn and persisted for crash resume.
- Wrap every tool call in a sandboxed git worktree, with outputs truncated to 4k tokens and a PreToolUse guard against destructive commands.
- Enforce the cost ceilings (50 turns, 200k context, $5 per task) and use a PreCompact hook to keep long runs inside them.
- Evaluate the harness against mini-swe-agent on 30 SWE-bench Pro tasks using pass@1, turns-per-task, and dollar-per-task, with OpenTelemetry traces.
Problem
A terminal-native coding agent is a command-line program that wraps a frontier model in a plan-act-observe loop, gives it a permissioned set of tools, and runs everything inside a sandbox. Coding agents became the dominant AI application category in 2026, and Claude Code (Anthropic), Cursor 3 with Composer 2 and Agent Tabs (Cursor), Amp (Sourcegraph), OpenCode (112k stars), Factory Droids, and Google Jules all ship variations of that architecture.
The model frontier is narrow. Live-SWE-agent reached 79.2% on SWE-bench Verified with Opus 4.5. The engineering craft around it is wide, and most failures live there rather than in the model: tool-loop instability, context poisoning, runaway token cost, and destructive filesystem operations.
You can't reason about these agents from the outside. You have to build one, watch the loop crash on turn 47 when ripgrep returns 8MB of matches, and rebuild the truncation layer. That is the point of this capstone.
Concept
A harness is the code around the model that dispatches tools, maintains plan state, and enforces budgets. It has four surfaces:
- Plan maintains a TodoWrite-style state object that the model rewrites each turn.
- Act dispatches tool calls: read, edit, run, search, git.
- Observe captures stdout, stderr, and exit codes, truncates them, and feeds the summary back.
- Recover handles tool errors without blowing the context window or looping forever.
The 2026 shape adds a fifth piece: hooks. These are configurable extension points where the operator injects policy, telemetry, and guardrails. There are eight: PreToolUse, PostToolUse, SessionStart, SessionEnd, UserPromptSubmit, Notification, Stop, and PreCompact.
The sandbox is E2B or Daytona. Each task runs in a fresh devcontainer with a git worktree mounted read-write, and the harness never touches the host filesystem. The worktree is torn down on success or failure.
Cost control is enforced at three layers: a per-turn token ceiling, a per-session dollar budget, and a hard turn limit (typically 50). Observability is OpenTelemetry spans with GenAI semantic conventions, shipped to a self-hosted Langfuse.
Architecture
user CLI -> harness (Bun + Ink TUI)
|
v
plan / act / observe loop <---> Claude Sonnet 4.7 / GPT-5.4-Codex / Gemini 3 Pro
| (via OpenRouter, model-agnostic)
v
tool dispatcher (MCP StreamableHTTP client)
|
+------------+------------+----------+
v v v v
read/edit ripgrep tree-sitter git/run
| | | |
+------------+------------+----------+
|
v
E2B / Daytona sandbox (worktree isolated)
|
v
hooks: Pre/Post, Session, Prompt, Compact
|
v
OpenTelemetry -> Langfuse (spans, tokens, $)
|
v
PR via GitHub app
Stack
- Harness runtime: Bun 1.2 + Ink 5 (React-in-terminal)
- Model access: OpenRouter unified API with Claude Sonnet 4.7, GPT-5.4-Codex, Gemini 3 Pro, Opus 4.5 (for hardest tasks)
- Tool transport: Model Context Protocol StreamableHTTP (MCP 2026 revision)
- Sandbox: E2B sandboxes (JS SDK) or Daytona devcontainers
- Code search: ripgrep subprocess, tree-sitter parsers for 17 languages (pre-compiled)
- Isolation:
git worktree addper task, cleanup on success / failure - Eval harness: SWE-bench Pro (verified subset) + Terminal-Bench 2.0 + your own 30-task holdout
- Observability: OpenTelemetry SDK with
gen_ai.*semconv → self-hosted Langfuse - PR posting: GitHub App with fine-grained token, scope limited to the target repo
Build It
-
TUI and command loop. Scaffold a Bun project with Ink and accept
agent run <repo> "<task>". Print a split view: plan pane on top, tool-call stream in the middle, token budget at the bottom. Add a Ctrl-C handler that fires theSessionEndhook before exit. -
Plan state. Define a typed TodoWrite schema with pending, in_progress, done, and failed items plus notes. The model rewrites the full state each turn as a tool call; don't let it mutate the state incrementally. Persist the plan to
.agent/state.jsonso a crashed run can resume. -
Tool surface. Define six tools:
read_file,edit_file(with diff preview),ripgrep,tree_sitter_symbols,run_shell(with timeout), andgit(status, diff, commit, push). Expose them over MCP StreamableHTTP so the harness stays transport-agnostic. Every tool returns truncated output, capped at 4k tokens per call. -
Sandbox wrapping. Each task spawns an E2B sandbox. Run
git worktree add -b agent/$TASK_IDto create a fresh branch. All tool calls execute inside the sandbox, and the host filesystem is unreachable. -
Hooks. Implement all eight 2026 hook types, then wire at least four user-authored hooks: (a) a
PreToolUsedestructive-command guard that blocksrm -rfoutside the worktree, (b)PostToolUsetoken accounting, (c)SessionStartbudget initialization, and (d)Stopwriting a final trace bundle. -
Eval loop. Clone a 30-issue subset of SWE-bench Pro Python and run your harness against each issue. Compare against mini-swe-agent (the minimal baseline) on pass@1, turns-per-task, and $-per-task. Write the results to
eval/results.jsonl. -
Cost control. Set hard cutoffs of 50 turns, 200k context, and $5 per task. At the 150k mark, a
PreCompacthook summarizes older turns into a prior-state block, which frees room for new observations without losing the plan. -
PR posting. On success, the final step is
git pushplus a GitHub API call that opens a PR with the plan and the diff summary in the body.
The companion code is a scaffold of the loop, not the full spec above. main.py (and the TypeScript port under ts/) is offline (the Python file is stdlib-only). A scripted stub stands in for the model, so there is no OpenRouter call, no Bun or Ink TUI, no MCP transport, and no E2B or git-worktree sandbox. It implements two of the six tools (read_file and run_shell; the TypeScript run_shell is a canned stub that does not execute anything), the turn, token, and dollar budget, and all eight hook events on the bus. Only a PreToolUse destructive-command guard and trace hooks are wired, and PreCompact is never fired. Plan persistence and PR posting are left for you to build, and the TypeScript eval is a three-task offline pass/fail counter, not the SWE-bench Pro comparison. One quirk: the stub's second step reads README.md, which the Python directory does not contain, so the trace records a failed read_file while the plan still ends all done. That makes a handy first test for the recover step.
Use It
$ agent run ./my-repo "Fix the race condition in worker.rs"
[plan] 1 locate worker.rs and enumerate mutex uses
2 identify shared state under contention
3 propose fix, verify tests
[tool] ripgrep mutex.*lock -t rust (44 matches, truncated)
[tool] read_file src/worker.rs 120..180
[tool] edit_file src/worker.rs (+8 -3)
[tool] run_shell cargo test worker:: (passed)
[plan] 1 done · 2 done · 3 done
[done] PR opened: #482 turns=9 tokens=38k cost=$0.41
Ship It
The deliverable is a skill at outputs/skill-terminal-coding-agent.md. Given a repo path and a task description, it runs the full plan-act-observe loop in a sandbox and returns a PR URL plus a trace bundle. The capstone is graded on this rubric:
| Weight | Criterion | How it is measured |
|---|---|---|
| 25 | SWE-bench Pro pass@1 vs baseline | Your harness vs mini-swe-agent on 30 matched Python tasks |
| 20 | Architecture clarity | Plan/act/observe separation, hook surface, tool schema, reviewed against Live-SWE-agent layout |
| 20 | Safety | Sandbox escape tests, permission prompts, destructive-command guard passes red-team |
| 20 | Observability | Trace completeness (100% of tool calls spanned), token accounting per turn |
| 15 | Developer UX | Cold-start < 2s, crash recovery resumes plan, Ctrl-C cancels mid-tool cleanly |
| 100 |
I'd build the cost cutoffs and the 4k truncation cap before the first eval run. The Problem section lists runaway token cost and context poisoning as failure modes, and both get worse when a tool dumps a large result into a 50-turn session. An agent that can't afford to finish a task tells you nothing about pass@1.
I'd also hold off on the reviewer sub-agent from the exercises until the single-agent number is on the board. The exercise hint says false-positive reviews usually push the pass rate below the single-agent baseline, so without that baseline you can't tell whether the reviewer helped or hurt.
Exercises
-
Swap the backing model from Claude Sonnet 4.7 to Qwen3-Coder-30B served on vLLM. Compare pass@1 and $-per-task, and report where the open model underperforms.
-
Add a
reviewersub-agent that reads the diff before PR posting and can request a revision loop. Measure whether false-positive reviews drop the SWE-bench pass rate below the single-agent baseline (hint: usually yes). -
Stress-test the sandbox. Write one task that tries to
curlan external URL and another that writes outside the worktree. Extend the step 5 PreToolUse guard if needed, confirm it blocks both, and log the attempts. -
Implement
PreCompactsummarization with a smaller model (Haiku 4.5). Measure how much plan fidelity is lost at 3x compaction. -
Swap the MCP StreamableHTTP transport for stdio. Benchmark cold-start and per-call latency, then pick a winner for local-only use.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Harness | "The agent loop" | The code surrounding the model that dispatches tools, maintains plan state, and enforces budgets |
| Hook | "Agent event listener" | A user-authored script the harness runs on one of eight lifecycle events |
| Worktree | "Git sandbox" | A linked git checkout at a separate path, disposable without touching the main clone |
| TodoWrite | "Plan state" | A typed list of pending, in-progress, done, and failed items that the model rewrites each turn |
| StreamableHTTP | "MCP transport" | The 2026 MCP revision: a long-lived HTTP connection with bidirectional streaming, replacing SSE |
| Token ceiling | "Context budget" | A per-turn or per-session cap on input plus output tokens, which triggers compaction or termination |
| pass@1 | "Single-attempt pass rate" | Fraction of SWE-bench tasks solved on the first run, with no retry and no test-set peeking |
Further Reading
- Claude Code documentation: the reference harness from Anthropic
- Cursor 3 changelog: Agent Tabs and Composer 2 product notes
- mini-swe-agent: the minimal baseline for SWE-bench harness comparison
- Live-SWE-agent: 79.2% on SWE-bench Verified with Opus 4.5
- OpenCode: open harness, 112k stars
- SWE-bench Pro leaderboard: the evaluation this capstone targets
- Model Context Protocol 2026 roadmap: StreamableHTTP, capability metadata
- OpenTelemetry GenAI semantic conventions: span schema for tool calls and token usage
Code
- main.py (python, 244 lines)
- ts/package.json (json, 20 lines)
- ts/README.md (markdown, 37 lines)
- ts/src/eval.ts (typescript, 24 lines)
- ts/src/harness.ts (typescript, 92 lines)
- ts/src/hooks.ts (typescript, 52 lines)
- ts/src/index.ts (typescript, 56 lines)
- ts/src/model.ts (typescript, 63 lines)
- ts/src/plan.ts (typescript, 63 lines)
- ts/src/repl.ts (typescript, 69 lines)
- ts/src/tools.ts (typescript, 44 lines)
- ts/src/types.ts (typescript, 46 lines)
- ts/tests/harness.test.ts (typescript, 83 lines)
- ts/tests/tools.test.ts (typescript, 49 lines)
- ts/tsconfig.json (json, 22 lines)