Skip to content

Never carry the opening request without the turn that follows it - #9424

Merged
danielhanchen merged 6 commits into
mainfrom
fix-carried-forward-newest-task
Aug 23, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix-carried-forward-newest-task

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

The bug

carried_forward_items in studio/backend/core/inference/checkpoint.py reserves the OLDEST evicted turn ahead of every newer one, so the slot cap drops the turn immediately after it. That turn is exactly where an early change of direction lives, so the block carries the abandoned request and hides the correction to it.

On current main:

from core.inference.checkpoint import carried_forward_items
turns = lambda *t: [{"role": "user", "content": x} for x in t]

carried_forward_items(turns("Build Flappy Bird", "Actually build Tetris instead", "Add music"), max_items = 2)
# ['Build Flappy Bird', 'Add music']

carried_forward_items(
    turns("Build Flappy Bird", "Actually build Tetris instead", *[f"Add feature {i}" for i in range(1, 8)]),
    max_items = 8,
)
# ['Build Flappy Bird', 'Add feature 1', ... 'Add feature 7']

Both blocks tell the model to build the game the user walked away from, and then apply every later increment to it. A wrong statement of the current task is the failure the reservation was added to prevent, and the reservation is what produces it.

With this PR:

['Actually build Tetris instead', 'Add music']
['Build Flappy Bird', 'Actually build Tetris instead', 'Add feature 2', ... 'Add feature 7']

The case the reservation exists for is unchanged. A task stated once at the front followed by nineteen increments, at max_items = 8, still carries Build a Flappy Bird game first and increment 19 last.

The rule

The opening turn is reserved together with the next evicted instruction turn, both or neither, so the block can never show an opening request without what the user said next about it.

The pair is slotted in behind the newest turn that can actually be taken, exactly as the single reservation was, and it costs one more slot, paid by the oldest turn the newest-first walk would otherwise have taken. If the pair does not fit whole (a cap of one or two slots, or the budget already spent), the reservation is abandoned and the newest-first walk decides WITHOUT the opening turn: leaving it in the fallback simply picked it up again whenever it was the cheaper of the two, which is the same wrong statement of the task by another route.

One exception, and it is the boundary of the rule: when the successor costs more than the whole budget, no ordering carries it and there was never a pair to take. Dropping the opening then buys nothing, because on those threads the opening is the only affordable turn. The campaign's headline case is exactly that shape, a 43-token standing instruction followed by eight 160-token sections under a 100-token budget, and excluding the opening sends the block out empty, which is the failure the whole pass exists to stop. Nothing but the English separates that thread from an unaffordable correction, so both keep the opening.

Rendering position is read off the transcript rather than off the walk, for the same reason the header's later-wins rule exists: the reserved pair can fill the slot cap before the walk reaches a newer copy of a restated instruction, and the surviving copy then rendered at the position of the older one.

It needs no reading of the English. It does not ask which turn is a task statement and which is a correction; it only guarantees that a correction sitting immediately after the opening request cannot be hidden behind it.

Boundary: the pair needs two slots behind the newest usable turn, so three slots is where it starts fitting. ["Build Flappy Bird", "Actually build Tetris instead", "Add feature 1", "Add feature 2"] gives ['Add feature 1', 'Add feature 2'] at max_items = 2 (nothing wrong, only missing) and ['Build Flappy Bird', 'Actually build Tetris instead', 'Add feature 2'] at 3. The same rule applies against the token cap: a budget with room for the newest turn and one of the two opening turns now buys the correction, not the abandoned request.

Alternatives rejected

Drop the reservation entirely. It regresses the case the reservation was added for, which is the ordinary shape of a real session: the task is stated once at the front and every later turn is an increment ("add music", "now the score", "fix the pipes"). A plain newest-first walk then carries eight ways to change a game it never names. test_the_opening_task_still_survives_a_run_of_short_increments is that case.

Reserve the newest task-establishing turn instead of the oldest. This needs a classifier for "task-establishing", and there is no structural signal for it. Nothing separates "Actually make it Tetris" from "fix it" except the English, and the file already records the cost of guessing at phrasing: the 80-character floor was a guess of exactly this kind and dropped "Create a Flappy Bird game in HTML" (33 chars) on a live session. A rule that cannot be stated without reading the words is a rule that cannot be tested at its boundary.

Keep the reservation but never let it displace a newer turn. Every turn in the walk is newer than the reserved one, so the reservation only ever displaces newer turns. The condition is unsatisfiable, and the rule degenerates to dropping the reservation, with the regression above. A weaker reading, "abandon the reservation whenever the walk cannot also reach the turn after the opening", is the same regression: on a task plus nineteen increments at eight slots the walk reaches the thirteenth turn, nowhere near the second, so the reservation is abandoned and the task statement goes with it. Promoting the successor alongside the opening is what makes the condition satisfiable, which is why the fix is a pair rather than a veto.

Tests

studio/backend/tests/test_checkpoint_compaction.py gains nine tests: both repro cases above, the long run of increments with no correction, the both-or-neither boundary at two slots versus three, the same rule against the token cap, an opening turn that nothing follows (still reserved, since nothing can be hidden behind it), the fallback that must not re-select the opening, the unaffordable-successor boundary where it must, and a restatement that has to outrank its own older copy even when the pair fills the cap.

python -m pytest tests/test_checkpoint_compaction.py -q     85 passed
python -m pytest tests/test_context_overflow_truncation.py tests/test_llama_cpp_context_fit.py \
  tests/test_conversation_recall_injection.py tests/test_llama_cpp_tool_loop.py \
  tests/test_chat_history_routes.py -q                      427 passed (all six suites)

No existing test changed.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 592fba5f26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed this hits _select_items in studio/backend/core/inference/checkpoint.py, where the reserved opening turn is carried while the turn correcting it is the one the cap drops, and the render position still comes off the walk. Will get this reviewed.

carried_forward_items reserved the oldest evicted turn ahead of every newer
one, so the slot cap dropped the turn immediately after it: a correction that
restated the task was lost while the abandoned original was carried.

The reservation now takes the opening turn together with the next evicted
instruction turn, both or neither.
…en the reservation is abandoned

The reserved pair can fill the slot cap before the walk reaches a newer copy
of an instruction, so a restatement rendered at the position of the older
copy and the block's later-wins rule reported the opposite of the truth.
Position is now read off the transcript rather than off the walk.

Abandoning the reservation also has to exclude the opening turn from the
fallback walk, or the cheaper opening is simply picked up again with the
correction to it still dropped. Not when the successor costs more than the
whole budget: no ordering carries it, the opening is then usually the only
affordable turn, and the block would go out empty.
@danielhanchen
danielhanchen force-pushed the fix-carried-forward-newest-task branch from 592fba5 to 5ac3e89 Compare August 23, 2026 01:03
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A request whose system turn already carries a block re-caps it together with
whatever was just evicted, and that walk had no pair awareness. So one
compaction later it split the pair the fresh walk had kept: on a 4096-token
window with 512 reserved, a carried Flappy Bird opening and its Tetris
correction plus four increments dropped the correction and kept the opening,
which is the block this pass exists to prevent, rebuilt one round on.

The selection body is extracted unchanged into _pick, over (text, cost)
positions. _select_items becomes an adapter over evicted turns, _recap an
adapter over the merged strings, and _project asks for the reservation only
when the block has two bullets to pair. Equivalence of the extraction was
fuzzed against the previous implementation rather than assumed.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Reserving the first two bullets was the wrong invariant. The block is ordered
by each item's newest copy, so a correction the user restated renders behind the
turn that came between it and the opening, and the pair then formed was the
opening with that intervening turn while the correction itself was dropped. The
result was worse than not reserving at all: the block stated the task the user
had cancelled and applied every later increment to it, which is the output the
reservation exists to prevent.

Neither positional reading survives, so the whole carried block is reserved
together and the newest-first walk decides when it does not fit. Reverting to
the plain recap was measured rather than assumed and is not the safe direction:
it states the abandoned task too, which a test now pins.

Fuzzed over 30,000 merged cases: wrong-task outputs 700 to 0, with no block
emptied that the plain walk would have filled and no newest affordable turn
lost. The cost is that the opening bullet is sometimes dropped where the plain
walk kept it, which is the intended trade.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: a7199e9d94

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6351c80852

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +217 to +222
successor = (
None
if oldest is None
else next((i for i in range(oldest + 1, len(entries)) if _item(i)), None)
)
unit = [] if oldest is None else [oldest] if successor is None else [oldest, successor]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid treating the second turn as the only possible correction

With the default MAX_ITEMS=8, a nine-turn sequence such as “Build Flappy Bird”, “Add music”, “Actually build Tetris instead”, followed by six Tetris increments now reserves the first two turns and fills the remaining slots with the six newest increments, dropping the third-turn correction. The resulting checkpoint tells the model to build Flappy Bird with Tetris-specific changes; before this change, the newest-first tail retained the correction. Reserving the immediate successor must not allow it to displace a later change of direction.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of your factual claims check out, and I still reject the conclusion: the dropped turn is the SLOT CAP, not the successor reservation.

Reproduced your nine-turn thread exactly, through carried_forward_items and again end to end through fit_checkpoint_context. At max_items 8 the merge base carries 'Build a Flappy Bird clone in HTML' then 'Actually build Tetris instead', and head carries the opening then 'Add music to the game', correction gone. So yes, on this input the pre-pair code retained the correction and head does not. (One correction to the record: the pre-pair base is ee1e0f8, the #9379 merge base. 5ac3e89 sits after 5eb56f1, which is where the successor reservation went in.)

What that example is, though, is one index of a translation, not a new failure class. With the opening reserved and the budget non-binding, max_items always drops a CONTIGUOUS block of exactly n - max_items turns. Measured drop sets:

n= 9 base drops [1] head drops [2]
n=10 base drops [1,2] head drops [2,3]
n=12 base drops [1,2,3,4] head drops [2,3,4,5]
n=15 base drops [1..7] head drops [2..8]

Identical hole size, shifted one index newer. Head is worse at exactly one index, c = n - max_items + 1, which is your example (n=9, k=8, correction at index 2). Head is better at exactly one index, c = 1, which is this PR's own test_a_correction_is_not_buried_by_the_increments_that_follow_it. Your item and that test are the two boundaries of the same one-place shift.

And there is no ordering that removes the class. Any kept set of size k containing index 0 leaves n - k turns dropped, so a change of direction at any of them produces the same block. The only rule that scores zero is not carrying the opening when n > k, which is the behaviour #9379 landed to fix.

Net effect, fuzzed over 40,000 random threads with a correction at a uniform random index, mixed binding and non-binding budgets:

wrong-task blocks, base (opening only) 19610
wrong-task blocks, head (opening+successor) 16156
base wrong / head right 5696 head wrong / base right 2242

An 18% net reduction, fixing about 2.5 cases for every one it breaks, with blocks-emptied and newest-turn-lost both at 0. I also checked your suggested direction and the obvious neighbours. Reserving the opening plus the NEWEST instruction degenerates to the base, since the walk already takes the newest turn first. A structural 'only if not contradicted later' has contiguity as its only sound definition, and that drops the opening on 89% of correction-free threads and fails both test_the_opening_task_still_survives_a_run_of_short_increments and test_the_opening_task_survives_a_long_run_of_increments_with_no_correction.

Conceded: _reserved_order's docstring claims a correction cannot be hidden behind the request it corrects, which is true against the token cap and not against the slot cap. That is a comment overclaim and I will tighten it rather than leave the wrong promise in the tree. 245 pass across the compaction and context-fit suites, no code changed.

It claimed a correction cannot be hidden behind the request it corrects. That
holds against the token cap and not against the slot cap, where reserving the
opening leaves a contiguous run dropped whatever the order, so the pair moves
the hole one index rather than closing it. Records the measured trade instead of
the promise.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7a4e28eb5b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +217 to +220
successor = (
None
if oldest is None
else next((i for i in range(oldest + 1, len(entries)) if _item(i)), None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pair only adjacent transcript turns

When a middle turn is protected, truncate_oldest_messages leaves it live while evicting groups on both sides (context_window.py lines 131-158), so entries is not necessarily a contiguous transcript. This search therefore treats the next evicted instruction as the opening's successor even though an intervening protected instruction was what actually followed it. With an evicted opening plus two later evicted instructions and max_items=2, the new fallback drops the opening and carries the two later instructions, whereas the previous reservation retained the opening and newest instruction; this can remove the task statement even though its real successor remains visible in the prompt.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your non-contiguity fact is right and the conclusion does not follow. context_window.py:142-144 is if index in protected_groups: index += 1; continue, so eviction skips a protected group and carries on past it, and eviction_units is a list with holes. Reproduced your case: a 10-message thread with a mid-transcript system turn evicts transcript indices [1,2,4,5,6,7], and at max_items 2 the block is ['Add music to the game', 'Now add a score counter'] with the opening gone, exactly as you say.

The control is what decides it. Delete the protected turn so the same three instructions are evicted contiguously:

with protected middle turn : evicted [1,2,4,5,6,7] -> ['Add music to the game', 'Now add a score counter']
contiguous control : evicted [1,2,3,4,5,6] -> ['Add music to the game', 'Now add a score counter']

Byte-identical. _pick only ever sees entries and cannot observe protection, so the protected turn changes nothing about which branch runs. This is not a new failure mode, it is the trade already adjudicated in 3837570116 (the pair moves the slot-cap hole one index) re-described with a live turn sitting in the gap.

On the safety argument specifically: 'its real successor remains visible in the prompt' is not a safety property here. The pair guards against a task the user abandoned INVISIBLY, and the earliest contradiction the model cannot see is by construction the next EVICTED turn, which is what _reserved_order reserves. In this very repro the live turn is 'always render in dark theme', which says nothing about which game, so its visibility offers no protection at all had 'Add music' instead been 'Actually scrap that, build Tetris'. Evicted-list adjacency is the correct notion for this pass; transcript adjacency is not.

I also measured your implied rule, falling back to the single reservation whenever the opening's transcript successor is live, over 60,000 non-contiguous threads:

wrong-task blocks base 18385 head 12479 your rule 14267
wrong-task, live-aware base 6364 head 3025 your rule 3952
opening lost base 0 head 6892 your rule 4741
blocks emptied / newest turn lost: 0 everywhere

It buys 2,151 retained openings for 1,788 extra wrong-task blocks, about 1.2 to 1, against the roughly 2.5 to 1 the shipped pair delivers. And on the non-contiguous class you single out, the pair does BETTER than average: 32% fewer wrong-task blocks versus 22% on contiguous threads. So the change you ask for is a partial revert that loses on the metric that justified the original. No code change. 245 pass across the compaction and context-fit suites.

@danielhanchen
danielhanchen merged commit 6392e25 into main Aug 23, 2026
36 checks passed
@danielhanchen
danielhanchen deleted the fix-carried-forward-newest-task branch August 23, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant