Skip to content

fix(agents): stop sessions_history from counting raw entries as logical turns - #157

Merged
convyusboy merged 2 commits into
boonfrom
ridho/eng-18919-sessions-history-raw-window
Aug 27, 2026
Merged

convyusboy merged 2 commits into
boonfrom
ridho/eng-18919-sessions-history-raw-window

Conversation

@convyusboy

@convyusboy convyusboy commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

sessions_history's limit was applied to raw session-transcript entries, not logical conversation turns. chat.history (src/gateway/server-methods/chat.ts) reads up to limit raw entries before any filtering, and stripToolMessages only drops toolResult/tool roles — it never dropped the message-tool delivery path's real inference-turn record, which carries only thinking/toolCall blocks and no visible text at all (the actual reply text lives solely in a paired delivery-mirror entry written back after the send). So a caller asking for N turns of history could get a window that was mostly toolResult/tool-plumbing noise.

Why This Change Was Made

Live-verified against a real customer thread flagged P0 (Boon Agent "losing context" mid-conversation, #customer-escalations Slack 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 was toolResult and delivery-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_history self-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/droppedMessages in 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 read false while the raw-count window had already cut off real content.

Evidence

  • New helper dropToolPlumbingOnlyAssistantMessages (src/agents/tools/chat-history-text.ts) drops assistant entries with no visible text/image content, never touching delivery-mirror entries (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.
  • Added 2 tests reproducing the live thread's exact raw-entry shape (sessions-history-tool.test.ts); full file: 6/6 passing.
  • tsgo:core and tsgo:core:test clean, oxlint/oxfmt clean on touched files.

Summary by cubic

Fixes sessions_history so its limit counts 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

  • Drops assistant entries with no visible text or image, including empty text blocks; delivery-mirror entries that hold the actual reply text are never touched.
  • Overreads by one raw entry so a session ending exactly at the requested limit is no longer misreported as truncated, and clamps requests to the gateway's 1000-entry hard cap so over-cap requests report as truncated.
  • When the raw window is exhausted and still short, does one bounded retry with a window grown from the observed noise ratio.
  • truncated/droppedMessages now also reflect when more real history exists before the window, not just byte-size overflow.
  • Only the sessions_history tool path changes; chat.history is untouched.
  • Adds tests reproducing the live transcript shape plus regression tests for the exact-limit and over-cap cases.

Written for commit 1212deb. Summary will update on new commits.

Review in cubic

…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

claude Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/agents/tools/chat-history-text.ts Resolved
…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.

@gandalfboon gandalfboon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • dropToolPlumbingOnlyAssistantMessages is only wired into sessions_history. stripToolMessages itself is unchanged, so run-wait.ts, sessions-list-tool.ts, and action-log.ts keep their existing behavior — no ripple.
  • SESSIONS_HISTORY_DEFAULT_LIMIT=200 and SESSIONS_HISTORY_GATEWAY_HARD_CAP=1000 genuinely mirror chat.ts (defaultLimit=200, hardMax=1000, maxMessages+1 overread convention — src/gateway/server-methods/chat.ts:2695-2702).
  • delivery-mirror gate 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, overreadLimit clamps to 1000, canDetectOverread goes false, and a full window is treated as "maybe more" — correct, since it genuinely can't be disproven.
  • droppedMessages semantics 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.

@convyusboy
convyusboy merged commit 6db170c into boon Aug 27, 2026
147 of 151 checks passed
@convyusboy
convyusboy deleted the ridho/eng-18919-sessions-history-raw-window branch August 27, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant