Skip to content

fix(codex): detect compaction boundary when a system-role message follows the user turn - #205

Merged
raine merged 2 commits into
raine:mainfrom
Bjorgz:fix/compact-detect-trailing-system-message
Nov 21, 2026
Merged

raine merged 2 commits into
raine:mainfrom
Bjorgz:fix/compact-detect-trailing-system-message

Conversation

@Bjorgz

@Bjorgz Bjorgz commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Issue: #204.

Claude Code 2.1.319 appends a system-role message after the user turn whenever a UserPromptSubmit hook or an output style emits text, so the compaction instruction (CRITICAL: Respond with TEXT ONLY… + Your task is to create a detailed summary…) sits in the final user message while messages.last() is that system message. is_compact_messages_request only looked at messages.last(), so with the normal (cache-safe) system prompt neither detection path matched and native compaction never triggered on such installs.

Change: skip trailing system-role messages and check the last user message. Any other trailing role (e.g. assistant) still means the request is not a compaction boundary, so the existing "markers must be in the final user message" behaviour is kept.

Tests: compact_request_detected_when_hook_system_message_follows_user (the captured 2.1.319 shape) and compact_message_not_detected_when_assistant_message_follows_user; the existing compaction detection tests pass unchanged.

Verified against a live Claude Code 2.1.319 session through the proxy: /compact produced zero server_compaction_* events before the change; the same request shape with the instruction as the final user message triggers server_compaction_triggered + server_compaction_completed in ~5 s.

🤖 Generated with Claude Code

…lows the user turn

Claude Code 2.1.319 appends a `system`-role message after the user turn
whenever a UserPromptSubmit hook or an output style emits text, so the
compaction instruction sits in the final user message while
`messages.last()` is that system message. With the normal (cache-safe)
system prompt neither detection path matched and native compaction never
triggered on such installs.

Skip trailing `system` messages and check the last user message; any other
trailing role still means "not a boundary".

Fixes raine#204
Native compaction must remove the summary instruction even when hooks add
system messages after the user turn. Skip translated system messages while
preserving assistant barriers and retain the hook content. Cover removal
and native replay with trailing messages.

Issue: raine#204
@raine
raine force-pushed the fix/compact-detect-trailing-system-message branch from 2956268 to 3a5e3a5 Compare November 21, 2026 10:39
@raine
raine merged commit 9b1b14e into raine:main Nov 21, 2026
@raine

raine commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks for the fix! v0.1.49 handles the trailing system message during both detection and replay.

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.

2 participants