Skip to content

feat(agent): compact ChatGPT context before model switches - #6979

Merged
M4n5ter merged 2 commits into
apache:rust-previewfrom
me2seeks:feat/codex-model-switch
Nov 18, 2026
Merged

M4n5ter merged 2 commits into
apache:rust-previewfrom
me2seeks:feat/codex-model-switch

Conversation

@me2seeks

Copy link
Copy Markdown
Contributor

Summary

Finish the ChatGPT subscription model-switch flow from #6579 after #6654. On a new Turn (send or resume), differing known compaction hashes or an over-budget context downshift trigger native compaction with the previous model before the target model sees the new input.

The native driver owns switch planning. Engine/State persist and validate the pre-turn checkpoint independently of a pending Main, retaining both the actual producer and the intended continuing model. A qualifying, definitively failed previous-model compaction may transfer once to the target model through an explicit recovery fact. Unknown outcomes and cancellation do not grant that transfer. The target may compact the replacement again if its threshold is still reached; ordinary target context-overflow remains an error.

This covers stopped/resumed conversations and inherited fork history. It adds no background model-switch coordination, account-based replay prohibition, text-summary fallback, or catalog timer.

Fixes #6579

Verification

  • Real Native Host regression exercises direct A-to-B conversion, target re-compaction, previous-model HTTP failure, forked history, a forked native checkpoint, and stop/resume. It asserts request order, preservation of encrypted reasoning, exact replacement checkpoints, and exclusion of the new input from pre-turn compaction.
  • Final exact-source run: 5 passed, 0 failed (the new regression contains six scenarios). Existing replay-admission and continuation regressions ran alongside it. Command: TMPDIR=/home/me2seeks/project cargo test --locked -p maka-agent-native --lib -- switching_compacts_with_previous_model_before_target_input replay_admission::tests continue_run_with_web_tools_reaches_the_model codex_compaction_checkpoint_satisfies_continuation_replay_boundary.
  • Scoped formatting and git diff --check.
  • Strict Clippy does not pass. The scoped run reports eight findings in unchanged Native code (external Codex custody, session tools, driver/cleanup, GitHub helpers, notifications, history projection). A command-only allowance was needed for an unchanged engine lint expectation; no source suppressions or unrelated fixes are included.
  • No live subscription-account probe was performed; transport responses in these regressions are controlled fixtures.

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Codex implemented and tested the change. A separate GPT-6.1 Sol agent at medium reasoning reviewed the implementation using first-principles and simplicity criteria; its actionable findings were addressed. Upstream Codex behavior attribution is recorded in docs/code-origin-audit.md.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

@github-actions github-actions Bot added the effort/XL Under 2500 readable lines label Nov 18, 2026

@jackwener jackwener left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[kabi-sol]

Automated review notice: This comment was posted by an automated review agent operated by jackwener. It is not an independent human review and does not replace one.

Update (2026-11-18): the P2 below is fixed in final raw a10ecbd730f1c6ad52334c1c36ab86f95672bc06. I reviewed the complete two-file increment from 95501d69. The planner now falls back to the sealed input-token estimate when terminal usage is absent. The unchanged independent same-hash stopped/downshift probe and its different-hash control both pass: previous-model compaction occurs, followed by a 1,439-byte target inference request. All seven selected tests passed (the five existing targeted regressions plus both independent probes; 97.92s).

Final raw merged with the landing parent produces the exact tree of landing 12083302f17796b464418770cb3decf8bfc85570. The four production files involved in this finding and the updated switch regression are unchanged from final raw through landing to current Rust 99b0dff69b1676af13a4c697c3773af8d526591a; changes in other overlapping PR files were also inspected. No new P0–P2 found in this increment. Runtime tests were run on final raw, not the full landed/current workspace. Final-raw CI still has failed checks; no green-CI certification, live-account validation, approval or merge is claimed.

The original review below remains as historical evidence for 95501d69; its P2 is no longer outstanding on the verified revisions.


Reviewed all 24 changed files at 95501d69022417e218dfb5511ebdb00771552e70. The previous-model/continuing-model separation and durable pre-turn checkpoint address the real need in #6579, but one P2 remains: a stopped request without terminal usage can bypass downshift compaction entirely (inline finding).

Independent validation: the five existing targeted Native/replay/continuation tests passed. An added real Native Host regression with equal compatibility hashes, a 128k-to-8k downshift, 108KB of prior input, and stop/resume failed: the target received an uncompressed 115,841-byte inference request without previous-model compaction. An otherwise identical different-hash control passed, issuing previous-model compaction and then a 1,439-byte target inference request. These use local HTTP/WebSocket response fixtures, not a live subscription account.

The current rust-preview integration is conflict-free; this is a merge-tree check, not an integration-suite pass. Exact-head CI run 44125574942 is failing. The Windows Markdown failure also occurs on the PR base; I have not established attribution for the other CI failures. No approval or merge performed.

Comment on lines +1202 to +1207
let tokens = main.closed.as_ref().and_then(|closed| {
closed
.usage
.input_tokens
.zip(closed.usage.output_tokens)
.and_then(|(a, b)| a.checked_add(b))

@jackwener jackwener Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Resolved in a10ecbd730f1c6ad52334c1c36ab86f95672bc06 (verified 2026-11-18). The caller now uses sealed input-token evidence when this optional usage is absent. The original same-hash stopped/downshift regression now passes: old-model compaction precedes a 1,439-byte target request. The repair is present in landing 12083302 and current Rust 99b0dff6; see the updated review summary for validation boundaries.

Original finding on 95501d69:

[P2] Retain a size estimate when stopped requests have no usage

A stopped/aborted Main normally has no terminal token usage, so this returns tokens: None. initial_switch_plan then treats both capacity checks as false (tokens.zip(...).is_some_and(...)). If the source and target hashes match, a smaller target gets no preflight at all; the new first-Main Codex suppression also disables the existing estimate-based compaction check. In a real Native Host stop/resume regression (128k source, 8k target, equal hashes, 108KB of prior input), the next wire request was a 115,841-byte target inference, with no old-model compaction. Changing only the hashes made the control pass and reduced target input to 1,439 bytes after old-model compaction. A real target enforcing its window will reject the first path, and the new Codex overflow branch terminates it. Use sealed request/history estimates when terminal usage is absent, and cover the equal-hash stopped downshift rather than treating unknown usage as below threshold.

@M4n5ter

M4n5ter commented Nov 18, 2026

Copy link
Copy Markdown
Member

Verified the same-hash window downshift on 95501d6 with a real ChatGPT subscription backend and a temporary Host state root. Both models reported comp_hash=3000. I started on gpt-6.2-sol (500k context), gave it a random passphrase, let that turn complete, then switched to gpt-6.2-astra (272k context) with Astra’s compaction threshold set to 3,000 tokens.

The Host recorded exactly three requests in order: Sol inference → Sol native compaction → Astra inference. Sol’s compaction input excluded the new question. The encrypted checkpoint returned by Sol matched the ciphertext in Astra’s frozen input byte for byte. Astra’s input contained the new question and no plaintext copy of the passphrase. Astra returned the exact passphrase.

The live catalog for this account returned 12 models, all with comp_hash=3000, so a live cross-hash handoff remains unverified. This run covered a completed source turn. The stopped-turn case is tracked in the open review thread.

@M4n5ter

M4n5ter commented Nov 18, 2026

Copy link
Copy Markdown
Member

I reproduced the stopped-turn downshift issue on 95501d6 with a live ChatGPT subscription and a temporary Host state root. I stopped gpt-6.2-sol after its first text output was recorded, confirmed cancellation with no final usage, then continued on gpt-6.2-astra. Both models reported comp_hash=3000, and Astra’s compaction threshold was 3,000 tokens.

On 95501d6, the first request after resume was Astra inference with an estimated 9,836 input tokens. Sol skipped compaction.

I tested a local fix that uses the original request’s recorded input estimate when final usage is missing. Repeating the live run produced Sol compaction followed by Astra inference with an estimated 1,501 input tokens. The ciphertext in Astra’s input matched Sol’s checkpoint byte for byte, and Astra replied RESUMED.

I also updated the existing stop/resume regression to use equal hashes and a smaller target window. Its request-order assertion failed on 95501d6 and passed with the local fix.

@M4n5ter
M4n5ter merged commit 1208330 into apache:rust-preview Nov 18, 2026
1 check passed
M4n5ter added a commit to sunrioa/maka that referenced this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/XL Under 2500 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants