Repository navigation
fix(agents): stop sessions_history from counting raw entries as logical turns - #157
Conversation
…al turns chat.history's `limit` bounds raw transcript entries — delivery-mirror duplicates and tool-call/thinking-only assistant stubs (the message-tool delivery path's real inference-turn record, which carries no visible text at all) count against it just like real turns, so a caller asking for N logical turns could get a window that was mostly noise. Confirmed live against a customer thread: the last 50 raw entries held only 24 real user/assistant turns. sessions_history now drops tool-plumbing-only assistant stubs (never the delivery-mirror entries that hold the actual reply text) and does a bounded single-retry grow of the raw fetch window, sized from the noise ratio observed in the first response, when that window was fully consumed and still came up short. ENG-18919
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.
Tip: instead of fixing issues one by one fix them all with cubic
Re-trigger cubic
…act-limit false truncation, drop empty-text stubs
Addresses PR review feedback:
- desiredLimit could exceed chat.history's own 1000-entry hard cap with no
signal that the read was therefore incomplete; clamp to the cap and treat
any caller request above it as inherently truncated.
- A session ending exactly at the requested limit was misreported as
truncated, since equality between the raw count and the requested limit
was treated as proof more history exists. Now overreads by one raw entry
(mirroring chat.history's own internal +1 overread pattern) so an exact
match can be told apart from a capped response.
- hasAssistantVisibleContentBlock counted an empty text block as visible
content, so a stub with `{type:"text", text:""}` alongside its tool calls
wasn't dropped. Now requires a non-empty trimmed value, matching
extractAssistantTextForPhase's own convention.
- Dropped ticket references from source/test comments and extracted the
duplicated temp-config test lifecycle into a shared helper, per repo
convention.
Added regression tests for the exact-limit and above-hard-cap cases.
There was a problem hiding this comment.
Approved.
Walked the diff, traced the grow loop against the test's 30-turn / 4:1-noise transcript locally, and re-ran the file — 16/16 pass on my box (matches CI).
What I checked
dropToolPlumbingOnlyAssistantMessagesis only wired intosessions_history.stripToolMessagesitself is unchanged, sorun-wait.ts,sessions-list-tool.ts, andaction-log.tskeep their existing behavior — no ripple.SESSIONS_HISTORY_DEFAULT_LIMIT=200andSESSIONS_HISTORY_GATEWAY_HARD_CAP=1000genuinely mirrorchat.ts(defaultLimit=200,hardMax=1000,maxMessages+1overread convention — src/gateway/server-methods/chat.ts:2695-2702).delivery-mirrorgate lives before the empty-content check, so a mirror entry with unusual content shape never gets dropped by mistake — matches the doc comment's promise that mirror entries are the only text record.- Hard-cap edge case is handled: when
rawLimit === 1000,overreadLimitclamps to 1000,canDetectOverreadgoes false, and a full window is treated as "maybe more" — correct, since it genuinely can't be disproven. droppedMessagessemantics now cover the three real cases: byte-cap trim, hard-cap placeholder, and more-real-history-before-window. That's the right union.
One follow-up thought, not blocking
The grow step is a single retry. Observed ratio underestimates in the pathological case (first fetch is 100% toolResult → logical.length=0 → ratio uses raw / max(0,1) = raw/1, so ceil(desired × raw) which is usually fine). But for a very tool-heavy session where even the grown window still comes up short, there's no second grow. In practice the hard cap of 1000 plus normal ~4:1 noise makes this a non-issue; worth a comment or a small while loop with a max-retries guard if this ever surfaces.
Ship it.
What Problem This Solves
sessions_history'slimitwas applied to raw session-transcript entries, not logical conversation turns.chat.history(src/gateway/server-methods/chat.ts) reads up tolimitraw entries before any filtering, andstripToolMessagesonly dropstoolResult/toolroles — it never dropped the message-tool delivery path's real inference-turn record, which carries onlythinking/toolCallblocks and no visible text at all (the actual reply text lives solely in a paireddelivery-mirrorentry written back after the send). So a caller asking for N turns of history could get a window that was mostlytoolResult/tool-plumbing noise.Why This Change Was Made
Live-verified against a real customer thread flagged P0 (Boon Agent "losing context" mid-conversation,
#customer-escalationsSlack thread) by SSHing into the fleet host and reading the actual session transcript: the last 50 raw entries (the tool's default read window) held only 24 real user/assistant turns — the rest wastoolResultanddelivery-mirror/tool-plumbing noise. That matches the reported symptom of the agent claiming a dozen-message thread had "nothing before" the current turn.Filed and tracked as ENG-18919.
User Impact
The Boon Agent's own
sessions_historyself-lookup tool now reliably returns the number of real turns a caller asks for (up to a bounded ceiling), instead of silently starving on threads with heavy message-tool/tool-call usage.truncated/droppedMessagesin the tool's response now also correctly reflect when more real history exists before the window — previously that flag only tracked byte-size overflow, so it could readfalsewhile the raw-count window had already cut off real content.Evidence
dropToolPlumbingOnlyAssistantMessages(src/agents/tools/chat-history-text.ts) drops assistant entries with no visible text/image content, never touchingdelivery-mirrorentries (the only record of that turn's actual text).sessions_history(src/agents/tools/sessions-history-tool.ts) does a bounded single-retry grow of the raw fetch window, sized from the noise ratio observed in the first response, when that window was fully consumed and still came up short of the requested logical-turn count.sessions-history-tool.test.ts); full file: 6/6 passing.tsgo:coreandtsgo:core:testclean,oxlint/oxfmtclean on touched files.Summary by cubic
Fixes
sessions_historyso itslimitcounts logical conversation turns instead of raw transcript entries, which previously let delivery-mirror duplicates and tool-call/thinking-only stubs eat the window and starve real history. Confirmed against a live thread (ENG-18919) where the last 50 raw entries held only 24 real user/assistant turns.What changed
delivery-mirrorentries that hold the actual reply text are never touched.truncated/droppedMessagesnow also reflect when more real history exists before the window, not just byte-size overflow.sessions_historytool path changes;chat.historyis untouched.Written for commit 1212deb. Summary will update on new commits.