Skip to content

fix(chat): Keep earlier Turn request context out of compaction - #2301

Merged
dcramer merged 2 commits into
mainfrom
fix/compaction-stale-turn-context
Oct 28, 2026
Merged

dcramer merged 2 commits into
mainfrom
fix/compaction-stale-turn-context

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

After an in-turn compaction, Junior refused to call createEventAutomation in a Turn that a person started with a Slack message. In that Turn the tool was enabled. The model made no tool call. It said the tool was off for the Turn and asked the person to send the request again. The retry worked because no compaction ran.

Root cause. An earlier Agent invocation Turn correctly had the event automation tools disabled, so its Turn context had a <disabled-tools> block. The next Turn, started by a person, added no Turn context block. Active compaction rebuilds runtime context from the newest <runtime-turn-context> block in history (effectiveRuntimeContext). That block was the follow-up block from the earlier Turn. The replacement history put it first, ahead of the current instruction, as "These tools are disabled in this Turn". The production request after compaction had that exact block. It also had no session context (<runtime>, <context>, available skills), because the follow-up block replaced the first Turn's session block as the anchor. This started when #2148 put disabled tools in follow-up Turn context. Plugin context and Conversation notes also go stale this way.

Fix. The agent now passes the current Turn start to compactActiveContextIfNeeded. Compaction keeps all runtime context from the current Turn. From earlier Turns it keeps only the latest session context, without its request sections (plugin-context, conversation-notes, disabled-tools), and the visible AGENTS.md instructions. The section tag names moved to turn-context-tag.ts, so prompt assembly and compaction read the same names. The existing split host context test now has an earlier session Turn and a follow-up Turn that no person started. On main it fails because the session context is missing.

Remaining risk. compactContextForHandoff still uses the old newest-block rule. A handoff in the middle of a Turn can probably keep stale request context too. I did not verify that, and this PR does not change handoff. When a resumed record has no Turn start, compaction uses the old rule.

via David Cramer.

--

View Junior Session [Sentry]

An in-turn compaction rebuilt runtime context from the newest
<runtime-turn-context> block in history. When the current Turn had no
block of its own, that was a follow-up block from an earlier Turn. Its
disabled tools and plugin context came back as current facts, and the
session context from the first Turn was lost.

Pass the Turn start to active compaction. Keep the current Turn's
runtime context, and from earlier Turns keep only the latest session
context without its request sections, plus AGENTS.md instructions.

Co-Authored-By: David Cramer <[email protected]>
@vercel

vercel Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Oct 28, 2026 8:43pm UTC

Request Review

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3454ca8. Configure here.

The compaction checkpoint kept the Turn start index from the old
history. A resumed Turn could then compact again and strip this Turn's
request context as if it came from an earlier Turn.

Set the index to 0 before the checkpoint and restore it if the
checkpoint fails.
@github-actions

Copy link
Copy Markdown
Contributor

177 screenshot changes — 0 changed · 177 added · 0 removed

Review screenshots in Frameshift

@dcramer
dcramer merged commit e5e8f7d into main Oct 28, 2026
44 checks passed
@dcramer
dcramer deleted the fix/compaction-stale-turn-context branch October 28, 2026 21:25

This branch was successfully deployed

1 active deployment
Preview – junior-docs — f2471876 Deployed Oct 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant