fix(agents): estimate harness role sizes in context guard char estimator (fixes #97927) - #97928
Conversation
The in-loop context-overflow guard (installToolResultContextGuard) sizes transcript messages via estimateMessageChars, which only handled user, assistant, and toolResult roles. Harness roles (bashExecution, compactionSummary, branchSummary, custom) fell through to a flat 256-char return, causing the guard to undercount summary- and bash-dominated context by orders of magnitude. This is the sibling defect of the just-merged openclaw#97861 which fixed the same class in estimateMessageTokenPressure (the pre-prompt precheck). This commit applies the identical pattern to the live in-loop guard estimator. Fixes openclaw#97927
|
Codex review: needs maintainer review before merge. Reviewed June 29, 2026, 5:44 PM ET / 21:44 UTC. Summary PR surface: Source +37, Tests +80. Total +117 across 2 files. Reproducibility: yes. Current main still falls through to Review metrics: none identified. Stored data model Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Next step before merge
Security Review detailsBest possible solution: Land a focused estimator fix that keeps the in-loop guard aligned with provider-rendered harness message content and preserves the existing pre-prompt estimator behavior. Do we have a high-confidence way to reproduce the issue? Yes. Current main still falls through to Is this the best way to solve the issue? Yes. Updating the existing char estimator to reuse the same rendered-role facts as the sibling pre-prompt estimator is the narrowest maintainable fix; a broader guard or compaction policy change is unnecessary. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 15de9d881a84. Label changesLabel justifications:
Evidence reviewedPR surface: Source +37, Tests +80. Total +117 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
…tor (openclaw#97928) The in-loop context-overflow guard (installToolResultContextGuard) sizes transcript messages via estimateMessageChars, which only handled user, assistant, and toolResult roles. Harness roles (bashExecution, compactionSummary, branchSummary, custom) fell through to a flat 256-char return, causing the guard to undercount summary- and bash-dominated context by orders of magnitude. This is the sibling defect of the just-merged openclaw#97861 which fixed the same class in estimateMessageTokenPressure (the pre-prompt precheck). This commit applies the identical pattern to the live in-loop guard estimator. Fixes openclaw#97927
…tor (openclaw#97928) The in-loop context-overflow guard (installToolResultContextGuard) sizes transcript messages via estimateMessageChars, which only handled user, assistant, and toolResult roles. Harness roles (bashExecution, compactionSummary, branchSummary, custom) fell through to a flat 256-char return, causing the guard to undercount summary- and bash-dominated context by orders of magnitude. This is the sibling defect of the just-merged openclaw#97861 which fixed the same class in estimateMessageTokenPressure (the pre-prompt precheck). This commit applies the identical pattern to the live in-loop guard estimator. Fixes openclaw#97927
What Problem This Solves
The in-loop context-overflow guard (
installToolResultContextGuard) sizes each transcript message withestimateMessageChars, which special-cases onlyuser,assistant, andtoolResultroles. Harness rolesbashExecution,compactionSummary,branchSummary, andcustomfall through to a flatreturn 256, even thoughconvertToLlmexpands each of these into full-text content sent to the provider. The guard undercounts summary- and bash-dominated context by orders of magnitude and never trips its overflow protection for those transcripts.This is the sibling defect of the just-merged #97861 (24626e5), which fixed the same class in
estimateMessageTokenPressure(the pre-prompt precheck) but did not update this estimator that backs the live in-loop guard.Why This Change Was Made
estimateMessageCharsintool-result-char-estimator.tshandlesuser,assistant, andtoolResultroles with content-aware estimation, then returns a flat 256 for everything else. AfterconvertToLlmexpands harness roles into full user messages, the actual provider payload can be orders of magnitude larger than 256 chars. The guard is blind to this context.User Impact
Users running long agent sessions with heavy bash output or multiple compaction rounds could silently exceed the context window because the in-loop guard never fires. This can cause model errors, lost context, or degraded response quality.
Changes Made
src/agents/embedded-agent-runner/tool-result-char-estimator.ts: Added content-aware estimation forbashExecution(viabashExecutionToText),branchSummary(with prefix/suffix),compactionSummary(with prefix/suffix), andcustom(string or array content) roles.bashExecutionwithexcludeFromContextreturns 0.src/agents/embedded-agent-runner/tool-result-char-estimator.test.ts: Added 6 tests covering each harness role estimation,excludeFromContext, and prefix/suffix inclusion.Evidence
node --import tsx -ewith representative harness messages (60k-line bash output, 20k-word compaction summary, 10k-word branch summary)bashExecutionwith large output returns 900k chars instead of 256.compactionSummaryandbranchSummaryinclude their prefix/suffix overhead.excludeFromContextcorrectly returns 0. The guard would now correctly trip overflow protection for harness-dominated transcripts.Real behavior proof
estimateMessageCharsCachedfromtool-result-char-estimator.tsnode --import tsx -eand calledestimateMessageCharsCachedwithbashExecution(60k lines),compactionSummary(20k words),branchSummary(10k words), andbashExecutionwithexcludeFromContext: trueObserved result after fix: All harness roles return content-aware estimates. The guard sees 1498x more context for harness-dominated transcripts.
excludeFromContextreturns 0 as expected.What was not tested: Live end-to-end agent session with real model call (the guard is an internal safety mechanism that fires only on extreme context pressure)
AI-assisted (Hermes Agent)