Drills D03

AI coding agent or PR reviewer

Design an AI reviewer that comments on every pull request in a large company's repositories. Then tell me what changes if it is also allowed to push fixes.

45 minutes · revise 28.03 · 28.05 · 18.08 · 21.10 · 20.11 · 28.11 · 33.03

Start with the reviewer: it is the take-home archetype (“a code-review agent with accuracy evaluation”) and it is read-only, which keeps the design honest. Then extend to an agent that edits code, where the problems become sandboxing, test-driven loops and permissions. The interviewer is listening for one idea above all: a reviewer lives or dies on precision, because developers mute a noisy bot within a week.

Clarify and scope

Baseline architecture

 PR opened / updated ─► webhook ─► queue (dedupe by PR + head SHA)
   ─► worker (sandbox, read-only repo token):
        checkout head ─► diff ─► split into reviewable units (file / hunk groups)
        ─► static pass first: linters, type checker, secret scan, tests if cheap
        ─► context builder per unit:
             changed hunks with line numbers
             enclosing functions / classes (tree-sitter)
             callers and callees (ripgrep + symbol index)
             related tests, repo review guidelines (cached prefix)
        ─► LLM review per unit → structured comments
             {file, line, severity, category, claim, evidence, fix, confidence}
        ─► verifier:
             line exists in diff? evidence quote matches code?
             duplicate of linter output? confidence ≥ threshold?
             optional: second model "is this a real bug?" on high-severity only
        ─► rank, cap at N comments per PR ─► post via API ─► trace
   developer reactions (resolved / changed / thumbs) ─► feedback store ─► evals

Repository navigation is the hard part, not the prompt. A diff alone hides most bugs: a changed function signature only matters if a caller is now wrong. Grep and a symbol index find callers exactly; embeddings help find “similar code elsewhere” but miss exact references, so use both with grep first. See code retrieval.

Key choices and trade-offs

Workflow, not a free agent, for review. The steps are known: diff, context, review, verify, post. A bounded workflow is cheaper and predictable. Allow one agentic step: the reviewer may call read_file and grep tools up to a small budget when it needs more context, because the context builder will sometimes miss.

Structured comments with evidence. Each comment must quote the line it criticises and state the failing scenario (“if items is empty, items[0] raises”). The verifier checks the quote exists at that line. Comments without a concrete scenario are dropped. This single rule removes much of the noise.

Precision over recall. Cap comments per PR (say 5), suppress low-confidence ones, and never repeat what a linter already said. A reviewer that posts three true bugs beats one that posts thirty mixed comments.

Model choice. A strong model for the review of risky files (auth, payments, migrations), a cheaper one for the rest, routed by path and diff size. Reasoning effort higher for security categories.

Extending to a coding agent that pushes fixes. Now it is an agent loop: read issue or failing check, plan, edit, run tests, read failures, edit again, until tests pass or the budget ends. Required additions:

Evals

Guardrails and fallbacks

Observability and cost

Trace per PR: units reviewed, context tokens per unit, tool calls, comments generated, comments dropped by the verifier and why, comments posted, latency, cost. Dashboards: precision by category from developer reactions, cost per PR, p95 time to first comment, and verifier drop rate (a rising drop rate means the prompt or model regressed).

The numbers

Tokens per PR (assumptions to state).

changed lines           ~300 → at ~10 tokens per line   ≈ 3K tokens of diff
context per PR          enclosing code, callers, tests  ≈ 15-25K tokens
guidelines + rules      ≈ 2K tokens (cacheable prefix)
input per PR            ≈ 25K tokens; output ≈ 2K (comments + reasoning)
verifier (high severity only, ~30% of PRs) ≈ 5K in, 300 out

Cost per 1K PRs (P in dollars per million tokens; check current prices):

review   = 25 × P_in + 2 × P_out
verifier = 0.3 × (5 × P_in + 0.3 × P_out)
total    ≈ 26.5 × P_in + 2.1 × P_out   (before prefix caching)
per day  = 5 × that, at 5,000 PRs/day

Throughput. 5,000 PRs over a 10-hour working day is about 0.14 PRs per second, around 3.5K input tokens per second. Small; rate limits are rarely the constraint, PR size outliers are.

Latency. Checkout and static pass 30 to 90 s, context building 5 to 20 s, LLM review per unit 10 to 40 s with units in parallel, verifier 5 to 10 s. Fits a 5-minute target.

Coding agent. Each loop iteration re-sends a growing context, so total tokens grow roughly with steps squared unless you trim tool output. If every step adds about 3K tokens (an edit plus test output) on a 10K start, a 30-step task reads about 30 × 10K + 3K × (30 × 29 / 2) ≈ 0.3M + 1.3M ≈ 1.6M input tokens, about 60 times one 25K-token review. Prefix caching cuts the price of the re-read part; trimming cuts the growth. Budget per task, not per call.

Follow-ups they will ask

How to bring in your own work

Be direct: you have not built a coding agent or a PR reviewer, and none of the example projects has a sandbox, a repo-navigation tool or a test-running loop. What you can bring is adjacent and real.

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open