Skip to content

feat(harness): token-usage optimizations across loop, review panel, and gates - #49

Merged
robercano-ghbot merged 1 commit into
mainfrom
feat/token-usage-optimization
Jul 6, 2026
Merged

feat(harness): token-usage optimizations across loop, review panel, and gates#49
robercano-ghbot merged 1 commit into
mainfrom
feat/token-usage-optimization

Conversation

@robercano-ghbot

Copy link
Copy Markdown
Collaborator

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:

  • FAST (1m) only when the loop can act now: unaddressed CHANGES_REQUESTED, or zero PRs + an open module:* issue
  • WATCH (5m) while PRs wait on human review/CI
  • IDLE (15m) otherwise

Ticks also skip reading USAGE.md / agent definitions unless they actually orchestrate (steps 3–4), and keep reports terse. Requires re-arming (/pr-loop or /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) — new budget.reviewer_models maps correctness/security → opus; fallback reviewer_model is now sonnet. The orchestrator passes the model at spawn time; reviewer frontmatter (now sonnet) is only the fallback. Default max_parallel_workers 3 → 2.

Cheaper review iterations (feature-fanout.js → v2 + orchestrator rule) — on rejection, only the lenses that rejected re-review the fix; approvals stand. MANAGED_VERSION bumped to 2 so /orchestrator:sync re-stamps downstream copies.

Gate output truncation (gate.sh) — gate runs are buffered: 5-line tail on pass, last 100 lines on fail. GATE_VERBOSE=1 or CI ($CI) restores full streaming; GATE_TAIL_PASS/GATE_TAIL_FAIL tune it. Exit codes still propagate, so Stop/PostToolUse hooks behave as before. This fires on every lint hook (each Edit/Write) and every Stop-hook test_affected run across all agents.

Context hygiene in agent prompts (orchestrator.md, implementer.md, reviewer.md) — new token-discipline section: route models from budget, 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.md rewritten around the new mechanics plus the habits config can't encode: /clear between tasks (long-conversation compounding), /usage attribution (skills/subagents/MCP, d/w), effort levels, MCP overhead, statusline context display.

Validation

  • Self gates green: GATES_FILE=.claude/self/gates.json gate.sh build|lint|test all pass
  • gate.sh pass path (5-line tail, rc=0) and fail path (truncated tail, rc propagated) exercised directly
  • Template feature-fanout.js = marker header + byte-identical body to the shipped workflow; template gates.json and scaffold.sh syntax-checked

🤖 Generated with Claude Code

https://claude.ai/code/session_01TdLbxgUQhEVr6XtFGzBpsY

…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-ghbot
robercano-ghbot merged commit 7822de1 into main Jul 6, 2026
9 checks passed
@robercano-ghbot
robercano-ghbot deleted the feat/token-usage-optimization branch July 6, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants