Set up a self-optimizing `slow-turn-retro` Stop hook for my coding agents so that whenever an interactive turn takes 10+ minutes of active work, the agent diagnoses what was slow, ships safe default-path speedups, and logs ROI telemetry.
1. Inspect my setup first
- Detect which coding agents I have installed that support Stop hooks (e.g., Claude Code, Codex, Antigravity, etc.) and locate my global instructions file and hook configuration for each.
- Read 2–3 real recent session transcripts per tool to confirm each tool's exact JSONL/event schema before writing any parser.
2. Add the standing rule
- Add a concise rule to my global instructions: "Slowness is a bug. Diagnose why something is slow instead of waiting it out. After any interactive turn taking 10+ minutes of active time (enforced by the `slow-turn-retro` Stop hook), check whether it could have run meaningfully faster without losing accuracy, verification, or scope; if so, fix the default path right then and record reusable findings."
3. Build and register one fast, stdlib-only Python Stop hook (<100 ms per Stop, fail-open on errors)
- Accurate active-time accounting:
- Measure active time from the latest real user prompt (ignoring queued continuation noise, system reminders, and mid-turn hook injections) to now.
- Subtract passive waits: user prompts/approvals, timers/`sleep`, idle subagent/message waits, and external CI/log/chat watchers (e.g., `gh run watch`, `gh pr checks --watch`, `--follow` tailers). Match watchers against the full raw shell command at command position (not truncated summaries or quoted text/heredocs), and merge overlapping wait intervals before subtracting so parallel watches are not double-counted.
- Exclude local file I/O tools (`read`/`edit`/`write`) from `slow_tools` so harnesses that stream parallel edit arguments do not falsely inflate tool duration.
- Break down remaining active time into tool calls, model thinking, and top 3 slowest tools (>= 15 s).
- Four skip gates (log every fire, skip, and completion to a local JSONL file so ROI can be audited):
1. Active time < 600 s (10 min).
2. Non-interactive runs (subagents, headless/automated runs, or non-user-initiated turns).
3. Thinking-only turns: slowest non-file tool call < 30 s AND timer/sleep wait < 60 s (keeps detached remote/cloud jobs polled via timers, while skipping pure-reasoning turns even if the user answered a question mid-turn).
4. Provider latency: median model-step latency >= 45 s (upstream inference slowdown the agent cannot fix).
- Once-per-turn + automatic `retro_s` telemetry:
- Track fired turn IDs on disk (capped FIFO, e.g., 400 keys) so each slow turn triggers at most once.
- Evaluate the seen-turn guard *before* the < 30 s and latency gates so that on the closing Stop after a retro finishes (once background tasks and any answer-rescue finish), the hook writes a `decision: "complete"` record with wall-clock `retro_s` (and `"rescued": true` if answer-rescue ran).
- Collapsing-UI answer guard:
- For any agent UI that hides/collapses the pre-Stop reply once a Stop hook continues the turn, instruct the agent to lead its final message with its complete answer and append a brief `Speedup retro:` note at the bottom. On the closing Stop, check content overlap against the pre-Stop reply and block once to rescue the answer if it was dropped.
- High-ROI retro prompt (returned as the Stop-hook continuation reason, prefixed with the turn's timing breakdown):
- Fix the default path: ship safe, reversible speedups directly in the default execution path (code, CLI default, config, cache, parallel runner, test target, shared helper, or a fast `PreToolUse` guard for recurring slow/hanging shell patterns like interactive prompts missing `-y` or unfiltered recursive `grep -r` over huge data/cache/venv directories). Avoid opt-in flags nobody passes and doc-only notes for fixable slowness.
- Only optimize what will run again: prioritize shared tooling and active multi-run pipelines; skip one-off scripts, finished migrations, superseded experiments, and irreducible external waits. Never cut corners on what the user cares about, and do not take costly, outward-facing, or risky actions without asking.
- Timebox with a revert-and-exit rule: keep local speedup passes to ~2–3 min (longer only for shared cross-project tooling). If a local optimization hits an unexpected hang, failure, or cascading breakage and is not green within ~3 min, revert it and finish instead of burning 10+ min debugging mid-retro.
- Verify and close cleanly: verify the speedup, record reusable findings in durable project notes/docs, and finish concisely (or say in one sentence if nothing meaningful could safely be sped up).
4. Verify end to end
- Add a `--dry-run` mode that prints `active_s`, `user_wait_s`, `timer_wait_s`, `max_tool_s`, `tool_calls`, `median_step_latency_s`, and the hook decision for any transcript.
- Write unit tests covering active-time math, merged overlapping watch spans, raw-command CI-watcher detection, local-file-tool exclusion, all four skip gates, once-per-turn idempotency, closing `retro_s` logging, and collapsing-UI answer rescue.
- Replay at least 3 real past transcripts per installed tool through `--dry-run` to confirm accuracy and verify hook execution stays under 100 ms.