Skip to content

fix(openai-history): keepTail:0 in mixed Responses collapse protected every message - #154

Merged
teamchong merged 1 commit into
teamchong:mainfrom
dex0shubham:fix/mixed-collapse-keeptail-zero
Jul 27, 2026
Merged

fix(openai-history): keepTail:0 in mixed Responses collapse protected every message#154
teamchong merged 1 commit into
teamchong:mainfrom
dex0shubham:fix/mixed-collapse-keeptail-zero

Conversation

@dex0shubham

Copy link
Copy Markdown
Contributor

Problem

In planResponsesMixedCollapse (src/core/openai-history.ts), the protected trailing messages are computed as:

messageIndices.slice(-Math.max(0, Math.floor(o.keepTail)))

When keepTail is 0, that expression is slice(-0). Array.prototype.slice(-0) treats -0 as 0 and returns the entire array — so "protect zero trailing messages" instead protects every message. Every old turn then becomes a hard barrier, no history collapses, and the planner returns reason: 'no_closed_prefix' with nothing imaged (the exact inverse of the intent).

keepTail: 0 is a legal, reachable config: it flows through nonNegativeInt(historyIn?.keepTail, …) (which accepts 0) from a host's gptHistory.keepTail override. The shipped Sol profile uses keepTail: 1, so this is a latent correctness bug on an override path, not a default-path failure.

Fix

Guard keepTail === 0 explicitly so it protects nothing; the latest user turn is still protected separately via latestUserIndex.

const tail = Math.max(0, Math.floor(o.keepTail));
const protectedMessages = new Set(tail > 0 ? messageIndices.slice(-tail) : []);

Added a regression test (verified it fails on the old code, passes on the fix). Full suite green except 3 pre-existing Windows-only file-permission failures in node-security.test.ts (unrelated; fail on a clean tree here too). typecheck clean.

Generated with Claude Code

… every message

planResponsesMixedCollapse computed the protected tail as
messageIndices.slice(-Math.max(0, Math.floor(keepTail))). When keepTail is 0
that is slice(-0), which returns the WHOLE array, so "protect zero trailing
messages" instead protected every message and blocked all history collapse
(reason: no_closed_prefix, nothing imaged). keepTail:0 is a legal host override
(nonNegativeInt accepts 0 via gptHistory.keepTail). Guard keepTail===0
explicitly so it protects nothing; the latest user turn is still protected
separately via latestUserIndex.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@teamchong
teamchong merged commit cbb27dd into teamchong:main Jul 27, 2026
1 check passed
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