fix: revive the tp-* response-budget watchdog (0.49.0) - #60
Merged
Conversation
Every tp-* agent declares a "Response budget: ~N tokens" and a watchdog was supposed to flag replies that overran it. It never ran once. The check lived in PostToolUse:Task, which does not fire for the dispatch tool on current Claude Code. This repo already knew that — installer.ts has said so since v0.39.3 — but nobody connected it to the budget path. The evidence was quiet and total: .token-pilot/over-budget.log did not exist, and every recorded task event carried a null budget. Moved the check to SubagentStop, where completions actually arrive, and corrected what it measures. It now sizes the agent's FINAL reply instead of everything the agent emitted along the way. That distinction matters: a response budget is about the answer handed back, and summing all output would have flagged essentially every agent (55k of output against a 600 token budget). The source is the transcript, not the payload's last_assistant_message. That field is present in the 2.1.220 bundle, but nothing here has ever read it, so there is no evidence it is populated — while the transcript is a path we already depend on and verified against real data. over-budget.log is written unconditionally. Surfacing the warning in the transcript rides the existing TOKEN_PILOT_SUBAGENT_FEEDBACK gate, since additionalContext on SubagentStop needs CC 2.1.163+. Verified end to end on a built dist: an overrun writes agent=tp-debugger budget=100 actual=2000, and a reply within budget writes nothing while still recording its task event.
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.
What
Revives the tp-* response-budget watchdog, which has been dead for months, and cuts 0.49.0.
Why
Every tp-* agent declares a
Response budget: ~N tokens, and a watchdog was supposed to flag replies that overran it. It never ran once.The check lived in
PostToolUse:Task, which does not fire for the dispatch tool on current Claude Code. This repo already knew that —installer.tshas said so since v0.39.3 — but nobody connected it to the budget path. The evidence was quiet and total:.token-pilot/over-budget.logdid not exist on any machine, and every recorded task event carried a null budget.It surfaced while chasing a suspected undercount in token accounting. There was no undercount, but this was sitting next to it.
How
SubagentStop, where subagent completions actually arrive.last_assistant_message. That field is present in the 2.1.220 bundle, but nothing in this codebase has ever read it, so there is no evidence it gets populated. The transcript is a path we already depend on and verified against real data.loadAgentBody/parseAgentBudget/decideBudgetAdvice/appendOverBudgetLoghelpers rather than reimplementing them.over-budget.logis written unconditionally. Surfacing the warning in the transcript rides the existingTOKEN_PILOT_SUBAGENT_FEEDBACKgate, becauseadditionalContextonSubagentStopneeds CC 2.1.163+.Risk
Low.
checkSubagentBudgetreturns null on every negative path — non-tp agent, no budget declared, agent file missing, transcript unreadable — and is wrapped so it cannot throw. A watchdog must not be able to break subagent completion.Verified end to end on a built dist: an overrun writes
agent=tp-debugger budget=100 actual=2000 over=19.00, and a reply within budget writes nothing while still recording its task event. 11 new tests, full suite 1438 green.Notes for reviewer
The measurement axis is the judgement call worth checking. "Response budget" reads as the size of the answer handed back, so that is what this measures. If the intent was the agent's total output instead, the threshold in every agent file is wrong by two orders of magnitude and the fix belongs there rather than here.