fix: preserve first user message to guarantee API validity - #196
Merged
Conversation
The v1.13.2 preserve-last-user fix searched the messages array for a pruned user message to restore when filtering would leave zero user-role messages. After OpenCode compaction removes pruned messages from the array, the search finds nothing and zero-user requests hit zhipuai-lb code 1214 (session freeze). Replace with preserve-first-user: the first user message (session's original task, always present in the array) is unconditionally force-preserved (survive=true), regardless of prune state. This does not depend on the integrity of the pruned portion of the array. Trade-off: when the first user is compressed and a later user also survives, two adjacent user messages can appear in the output. Zero-user is a hard API rejection; adjacent users are accepted by virtually all providers. Files: lib/messages/prune.ts Tests: tests/prune.test.ts (5 tests rewritten for new invariant) Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Three e2e tests in message-transform pipeline asserted u1 (first user) is pruned when it falls in a compression range. Under preserve-first-user, u1 is always force-preserved. Update assertions accordingly. Bug 36 (no adjacent users) test: the force-preserved first user is exempted from the adjacent-user check — zero-user is a hard API rejection while adjacent users are accepted by virtually all providers. Tests: tests/e2e-message-transform.test.ts Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the month-long session-freeze bug (zhipuai-lb code 1214, "messages 参数非法") where compression leaves zero user-role messages in the request.
Root cause
The v1.13.2
preserve-last-userfix (lib/messages/prune.ts) searched the messages array for a pruned user message to restore when filtering would leave zero users. After OpenCode compaction removes pruned messages from the array, the search finds nothing — zero-user requests slip through and the provider hard-rejects.Reproduced in
ses_0b89319b1ffeK25eKU3GMfCK8U: pre-compress 128 msgs / 12 user → post-compress 11 msgs / 0 user → API 400 code 1214 → session frozen.Fix
Replace
preserve-last-userwithpreserve-first-user: the first user message (the session's original task, always present in the array) is unconditionally force-preserved (survive = true), regardless of prune state. This does not depend on the integrity of the pruned portion of the array.Trade-off
When the first user is compressed and a later user also survives, two adjacent user messages can appear (e.g.,
[u1, u2, a2]). This is an accepted trade-off:The Bug 36 e2e test was updated: the force-preserved first user is exempted from the adjacent-user check. The no-adjacent-users invariant still holds for all non-first-user pairs.
Files
lib/messages/prune.ts— preserve-first-user logic (replaces preserve-last-user)tests/prune.test.ts— 5 tests rewritten for new invarianttests/e2e-message-transform.test.ts— 3 e2e tests updatedVerification
npm run typecheck— cleannpm test— 846 pass, 0 failnpm run build— cleanUltraworked with Sisyphus