Repository navigation
Count tool-call argument JSON in history token budget; remove redundant summarization gate - #2225
Conversation
…nt summarization gate (closes #2197) maybe_summarize_history estimated history tokens from msg.content only, but a tool-call assistant turn carries its signal in tool_calls[].arguments_json (content is empty by construction) — up to MAX_RESPONSE_TOKENS worth of JSON per call went uncounted. A second budget check then compared against a static total_budget - SYSTEM_PROMPT_BUDGET (4,000), looser than the real tool-registered system prompt (~6,600 measured tokens), letting it pass and skip summarization even after the first check had already established the prompt overflows the window. Fixes: count each tool call's function_name and arguments_json into the history text; drop the second, redundant gate so the first (correct) check against the measured system prompt is authoritative. Co-Authored-By: Claude <[email protected]>
Pragmatic Code Review — PR #2225Review Type: Initial Review Issue: #2197 — History token budget undercounts real prompt and refuses to summarize an over-budget conversation Requirements CheckThis is a bug report (no acceptance-criteria checklist) describing two concrete defects at
Code Review FindingsNo correctness, security, or quality issues found. Notes below are informational, not blocking. 🟢 Suggestion: pre-existing gap, not introduced here — summarization content path already correct Worth confirming (not a finding against this PR): the code that builds 🟢 Suggestion: fallback branch (
🟢 Suggestion:
Test Coverage
Verification Performed
RecommendationAPPROVE Minimal, correctly-scoped fix for a real, well-diagnosed bug. Single commit, root-cause fix with no compatibility shims, a regression test that's been verified to actually catch the regression, and no unrelated changes. Nothing here blocks merge. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.
Address-Review SummaryThe review returned APPROVE with zero 🔴 Critical or 🟡 Important findings. All three 🟢 Suggestions were explicitly marked non-blocking / informational by the reviewer:
No code changes made — nothing in the review required implementation. Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: The review found no correctness, security, or quality issues, and returned a clean APPROVE. All findings were explicitly informational/non-blocking with no suggested code changes. No commits were added since the reviewed commit (9c11487). The PR is unchanged from what was already reviewed and approved. The PR is ready for merge (pending final go-ahead). |
Pragmatic Code Review — PR #2225Code Review SummaryThis is a tight, well-scoped fix that correctly identifies and closes a real bug. The change is a single file, a single commit, and does exactly one thing: it makes the summarization trigger count the tokens that actually exist in the prompt, and it deletes a second gate whose budget was provably looser than the first one it followed. I verified the central claim empirically rather than taking the PR description on faith. I temporarily reverted only the With the loop restored: Both premises behind the fix check out against the actual code, not just the issue text:
The removal is behaviorally safe in the intended direction. The remaining gate is strictly more aggressive, so the only change is "summarizes in cases where it previously (wrongly) did not." Nothing can now skip summarization that didn't before. I also checked the thing that most often makes a fix like this half-complete — whether the content being summarized is still blind to tool calls even after the trigger is fixed. It is not: Net assessment: clear net improvement to code health. No blockers. Requirements CheckIssue #2197 is a bug report with a described failure scenario rather than an acceptance-criteria checklist, so I validated against the root cause as described.
The dominant term — argument JSON, up to FindingsCritical IssuesNone. Suggested Improvements🟡 [agent_loop.rs:2796-2812] The tool-definitions JSON is still uncounted, and it is a large, always-present term.
This is a constant undercount on every single turn, not an edge case. The comment on I would not block on this: it is a pre-existing gap, it is smaller than the arg-JSON term this PR fixes, and folding it in properly means either threading 🟡 [agent_loop.rs:4333, and the pre-existing sibling at 4136] The test comment's stated arithmetic does not match the budget the test actually exercises. The comment reads The real numbers: with The test still passes for the right reason and by a wide margin (6,135 + 2 vs 4,001), so this is a comment-accuracy issue rather than a correctness one. But it matters more than a typo normally would: a future reader tuning these constants will reason from "4096" and reach a wrong conclusion about how much headroom the test has. Suggest something like: 4096, // → total_budget floors at SYSTEM_PROMPT_BUDGET + 1 = 4001;
// history measures ~6,135 tokens, well over itWorth noting the same off-by-a-floor framing already exists at line 4136 in 🟢 [agent_loop.rs:2814-2821] The removal-rationale comment is doing archaeology, and will age poorly. Seven lines explaining what a now-deleted check used to do and why it was wrong. That history is already captured well in the commit message and the PR body, which is where it belongs — per this repo's own standard that comments explain the why of current code, not the story of its edits. Once #2197 is a year old, this paragraph costs every reader time to determine it describes nothing that exists. Suggest collapsing to the invariant a future reader actually needs, which is the non-obvious part worth keeping: // Sole gate: measured history + the *actual* system prompt against the
// window. Deliberately no secondary check against a static budget —
// SYSTEM_PROMPT_BUDGET (4,000) is well under the real tool-registered
// prompt (~6,600), so any such check is looser than this one and can
// only undo it.That preserves the load-bearing warning ("don't re-add a static-budget check") without narrating the diff. 🟢 [agent_loop.rs:2776-2782] Same over-narration in the accumulation-loop comment. "...that the naive content-only scan would miss entirely, which is exactly the corridor where..." describes the bug being fixed rather than the code as it stands. The genuinely non-obvious fact — that NitpicksNit: [agent_loop.rs:671 / 4689] Removing Flagging this only because it was raised as an open question. Inlining is correct here. The const had exactly one remaining use — a test sanity ceiling — and keeping a named module-level constant alive for a single test reference would imply it still participates in production budgeting, which it does not. That implication is precisely what made the old second gate look legitimate. Deleting it removes a misleading affordance, and matches the repo's "delete, don't preserve" stance. No change requested. Nit: [agent_loop.rs:2786-2791] The separator spaces are slightly generous, in the safe direction. Each call appends four space separators. Against a Nit: [agent_loop.rs:2769] The extra work is proportional to argument JSON already resident in memory, and it is dwarfed by the inference call that follows, so there is no practical cost. Not worth optimizing — mentioning it only to confirm it was considered and dismissed. Edge cases checked
RecommendationAPPROVE The fix is correct, minimal, and grounded in verified facts about the codebase rather than assumption. The new regression test is real — I confirmed it fails without the fix and passes with it. The gate-removal is safe and strictly conservative in direction. Nothing here blocks merge. The only item I would genuinely like to see not get lost is the uncounted tool-definitions JSON (🟡 above): #2197 named it, this PR does not close it, and it is a permanent per-turn undercount. A follow-up issue is the right home for it — folding it in here would expand the scope beyond the single clean change this PR is. The comment-accuracy and comment-verbosity items are cheap to fold in now if convenient, but are not merge conditions. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.
Pragmatic Code Review — PR #2225Verdict: APPROVE
Does it fix the reported bug?Yes — both halves of #2197, and nothing beyond them. Issue #2197 reported two co-operating defects in
The diff addresses each directly: the counting loop now folds Verified the ~6,600 figure is real, not folklore. Correctness
Test qualityThe new test I reverted only the production counting loop, leaving the test untouched, and re-ran it: It fails without the fix and passes with it. The file was restored to The test is well-constructed for what it targets: every assistant turn has empty The touched pre-existing test ( Verification run
StandardsComplies with the project guide: no backward-compat shims, no Observations (non-blocking, no action requested)
Recommendation: APPROVE. The change is tightly scoped to the reported defect, both halves are correct, the fix direction is conservative (summarize more readily, never less), the new test is a verified regression guard, and the full suite plus clippy are green. |
malibio
left a comment
There was a problem hiding this comment.
Verdict: APPROVE (posted as a comment because GitHub rejects self-approval on an own-authored PR.)
Full review is in the preceding PR comment. Summary: both halves of #2197 are fixed — tool-call arguments_json now counts toward the history budget, and the redundant looser second gate is removed so the check against the real measured system prompt is authoritative. The ~6,600-token system-prompt figure is corroborated by nlp-engine/src/chat/mod.rs:1571-1577.
Verified rather than assumed: reverting only the production counting loop makes the new test history_summarization_counts_tool_call_arguments fail ("Before: 24, After: 24"), so it is a genuine regression guard. cargo test -p nodespace-agent --lib → 540 passed / 0 failed; cargo clippy clean. No dead constants left behind, and the tool-call/tool-result pairing guard is untouched.
Three non-blocking observations (uncounted per-message template overhead, the now-oddly-named SYSTEM_PROMPT_BUDGET floor clamp, comment length) are detailed in the review — none warrant a change. No re-review needed.
Summary
maybe_summarize_historyestimated history tokens frommsg.contentonly. A tool-call assistant turn carries its signal intool_calls[].arguments_json—contentis empty by construction — so up toMAX_RESPONSE_TOKENS(2,048) of JSON per tool call went uncounted (entire markdown documents forcreate_nodes_from_markdown, etc.).A second budget check then compared history against a static
total_budget - SYSTEM_PROMPT_BUDGET(4,000), looser than the real tool-registered system prompt (~6,600 measured tokens per thenlp-engineN_CTX_MINIMUMtest). That let the second check pass — and skip summarization — even in the corridor where the first check had already established the real prompt overflows the window.Fix
function_nameandarguments_jsoninto the history text used for token estimation.HISTORY_TOKEN_BUDGETconst (inlined the equivalent bound in the one test that used it as a sanity ceiling).Test plan
history_summarization_counts_tool_call_arguments: a history built entirely from empty-content tool-call turns whose argument JSON alone exceeds a reduced effective window (4096). Verified this test fails without the fix (confirmed by manually reverting just thehistory_textloop) and passes with it.cargo test -p nodespace-agent --lib: 540 passed, 0 failed.cargo clippy -p nodespace-agent -p nodespace-core -p nodespace-nlp-engine -p nodespace-types --all-targets -- -D warnings -D clippy::unused-async: clean.bun run test:all: 227/227 frontend test files, all Rust workspace tests passing (pre-push gate).Closes #2197
🤖 Generated with Claude Code