feat(harness): token-usage optimizations across loop, review panel, and gates - #49
Merged
Merged
Conversation
…nd gates Cost scales with context size and is re-paid every message; these changes attack the three biggest sinks measured against that model: - pr-loop / pr-loop-self: three-tier adaptive cadence. FAST (1m) only when the loop can act NOW (unaddressed feedback, or an issue ready to advance); WATCH (5m) while PRs wait on human review/CI; IDLE (15m) otherwise. Ticks skip reading USAGE.md/agent defs unless they orchestrate, and report tersely. - Per-lens reviewer model routing: budget.reviewer_models maps correctness/ security -> opus, everything else falls back to reviewer_model (now sonnet). Orchestrator passes the model at spawn; reviewer frontmatter is the fallback. Default max_parallel_workers 3 -> 2 (template + root adapter). - Review iterations re-run ONLY rejecting lenses (feature-fanout.js v2 + orchestrator rule): an approval stands; a 1-lens rejection costs 1 re-review, not the whole panel. MANAGED_VERSION bumped so /orchestrator:sync re-stamps. - gate.sh buffers gate output: 5-line tail on pass, last 100 lines on fail (GATE_VERBOSE=1 or CI streams fully; GATE_TAIL_PASS/FAIL tune it). Hook exit codes still propagate. - Agents told to reference (branch/path/issue) instead of pasting content into spawn prompts, and to filter raw logs before ingesting them. - docs/TOKEN_BUDGET.md rewritten around the new mechanics + official guidance (/usage attribution, /clear between tasks, effort levels, MCP overhead). Self gates (build/lint/test) pass; template stays in sync with the shipped workflow (marker header only). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TdLbxgUQhEVr6XtFGzBpsY
robercano
approved these changes
Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Several usage limits were hit this week. Cost scales with context size and is re-paid on every message, so this PR attacks the orchestrator's three biggest token sinks: cron-tick cadence, the all-Opus review panel, and raw gate/log output flooding agent contexts. Grounded in the official guidance (costs, usage-limit best practices).
What changed
Loop cadence (
pr-loop.md,pr-loop-self.md) — every tick is a fresh full-context session. Old rule ran FAST (1/min) whenever any PR was open, including PRs just waiting on the owner. New three-tier rule:module:*issueTicks also skip reading USAGE.md / agent definitions unless they actually orchestrate (steps 3–4), and keep reports terse. Requires re-arming (
/pr-loopor/pr-loop-self) — an armed cron keeps its old prompt.Per-lens reviewer routing (
gates.json,self/gates.json, setup template,reviewer.md,orchestrator.md) — newbudget.reviewer_modelsmapscorrectness/security→ opus; fallbackreviewer_modelis now sonnet. The orchestrator passes the model at spawn time; reviewer frontmatter (now sonnet) is only the fallback. Defaultmax_parallel_workers3 → 2.Cheaper review iterations (
feature-fanout.js→ v2 + orchestrator rule) — on rejection, only the lenses that rejected re-review the fix; approvals stand.MANAGED_VERSIONbumped to 2 so/orchestrator:syncre-stamps downstream copies.Gate output truncation (
gate.sh) — gate runs are buffered: 5-line tail on pass, last 100 lines on fail.GATE_VERBOSE=1or CI ($CI) restores full streaming;GATE_TAIL_PASS/GATE_TAIL_FAILtune it. Exit codes still propagate, so Stop/PostToolUse hooks behave as before. This fires on every lint hook (each Edit/Write) and every Stop-hooktest_affectedrun across all agents.Context hygiene in agent prompts (
orchestrator.md,implementer.md,reviewer.md) — new token-discipline section: route models frombudget, reference branches/paths instead of pasting content into spawn prompts (pasting a diff into 4 reviewer prompts pays 4×), demand terse reports, continue existing workers instead of re-spawning, filter raw logs before ingesting.docs/TOKEN_BUDGET.mdrewritten around the new mechanics plus the habits config can't encode:/clearbetween tasks (long-conversation compounding),/usageattribution (skills/subagents/MCP,d/w), effort levels, MCP overhead, statusline context display.Validation
GATES_FILE=.claude/self/gates.json gate.sh build|lint|testall passgate.shpass path (5-line tail, rc=0) and fail path (truncated tail, rc propagated) exercised directlyfeature-fanout.js= marker header + byte-identical body to the shipped workflow; templategates.jsonandscaffold.shsyntax-checked🤖 Generated with Claude Code
https://claude.ai/code/session_01TdLbxgUQhEVr6XtFGzBpsY