Skip to content

fix: protectedContext floors compaction tail on long-superseded messages - #469

Closed
Satya Arjunan (satya-arjunan) wants to merge 1 commit into
synthetic-sciences:mainfrom
satya-arjunan:fix/protected-context-tail-floor
Closed

Satya Arjunan (satya-arjunan) wants to merge 1 commit into
synthetic-sciences:mainfrom
satya-arjunan:fix/protected-context-tail-floor

Conversation

@satya-arjunan

@satya-arjunan Satya Arjunan (satya-arjunan) commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #470

Problem

protectedContext in backend/cli/src/session/compaction.ts is documented as returning "the transcript span that still belongs to the active, unanswered turn," but the implementation scans forward from the start of the whole transcript for the first unanswered user message, not backward from the current turn. A common real pattern breaks this: a user message immediately superseded by a retry/resend (the retry gets answered, the original never does, since answered is keyed by parentID). That original message satisfies the scan forever, so selectTail's protectedStart floor (if (protectedStart > 0) cut = Math.min(cut, protectedStart)) permanently pins tailStartId to it — no matter how much later work has been fully, properly resolved.

Found this via a real session where every subsequent compaction kept flooring tailStartId at an old, superseded message well past 100k+ tokens of genuine follow-up work, even after raising the model's context window.

Fix

Scope the scan to the contiguous unanswered span ending at the current turn: walk backward from current and stop at the first user message that's already terminally answered or already compacted. This matches the function's own doc comment and still protects a run of genuinely-pending retries ending at the current turn — it just no longer reaches back to unrelated, long-resolved history.

Verification

  • Added a regression test in message-v2.test.ts (session.compaction.selectTail — protectedContext floor (regression)) that builds a realistic history with real Identifier.ascending("message") ids: 3 earlier answered turns, an orphaned original request + its answered retry, then 8 further fully-answered work turns. Fails on main (tailStartId resolves to the orphaned message); passes with this fix.
  • bun run typecheck — clean.
  • bunx prettier --check on both changed files — clean.
  • bun test across backend/cli: 470 pass / 3 fail, same 3 pre-existing unrelated failures as on main (a stale MCP tool-description string assertion, unrelated to this change) — confirmed by running the full suite against main before this fix (469 pass / 4 fail there, the 4th being the bug this PR fixes).

See #470 for the full root-cause writeup and real-session evidence.

protectedContext scanned forward from the start of the whole transcript
for the first unanswered user message, instead of walking backward from
the current turn as its own doc comment describes. A user message that's
immediately superseded by a retry/resend (the retry gets answered, the
original never does) satisfies that scan forever, so selectTail's
protectedStart floor pins tailStartId to it permanently, no matter how
much later work is fully resolved.

Walk backward from the current turn instead, stopping at the first
already-answered or already-compacted user message. Adds a regression
test in message-v2.test.ts using real Identifier.ascending ids.
@vercel

vercel Bot commented Aug 31, 2026

Copy link
Copy Markdown

Satya Arjunan (@satya-arjunan) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

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.

Compaction's protectedContext floors tailStartId at a long-superseded message, permanently

2 participants