Skip to content

fix(agent): keep new turns when the session transcript hits its byte cap - #138

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-detection-895e
Draft

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-detection-895e

Conversation

@cursor

@cursor cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown

Bug and impact

A session whose JSON is near the 128 KiB transcript cap, but whose message count is still under max_context_messages (default 40), never compacts. The next successful turn prints a document over the cap, copy_printed_session_json refuses the truncated write, and session_save is skipped. The reply is delivered, then every later turn is forgotten while the old transcript stays in place.

Concrete trigger: two stored messages of about 62 KiB each (count 2, under the compaction threshold) and one more assistant reply of 16 KiB. WebChat can hit the same cap in normal use: each turn can store up to 32 KiB of user text plus 32 KiB of assistant text, so a handful of long replies fills 128 KiB long before 40 messages.

Root cause

Compaction is count-based (msg_count > max_context_messages). The byte cap is enforced only by refusing the save, which was the right guard against persisting clipped JSON, but it left the row stuck at the old size.

Fix

append_exchange_to_session_json drops the oldest messages until the new exchange fits, then writes valid JSON. If the newest messages alone still do not fit, the save is still refused and the stored row is left unchanged.

Validation

make test_agent passed. The overflow regression now requires the new user text to be stored, the older retained turn to remain, and the evicted oldest turn to be gone. The separate case of a blob already larger than the load cap still skips persist and does not wipe the row.

Open in Web聽View Automation聽

Compaction runs only after max_context_messages, so a few long replies
filled the 128 KiB JSON cap and the save was refused. Later turns were
dropped while the old transcript stayed in place. Evict the oldest
messages until this exchange fits, and still refuse a truncated JSON write.

Co-authored-by: esadrianno <esadrianno@gmail.com>

This branch has not been deployed

No deployments
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