Skip to content

fix(core): recover context overflow without manual compaction - #371

Open
lg2m wants to merge 5 commits into
mainfrom
fix/compaction-overflow
Open

lg2m wants to merge 5 commits into
mainfrom
fix/compaction-overflow

Conversation

@lg2m

@lg2m lg2m commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

What changed

Fix the SQLite-audited LiteLLM/Bedrock Opus failure: missing model context metadata disabled proactive compaction, then both prompt and manual summarization overflowed.

  • Resolve known gateway aliases to the smallest bundled context limit; explicit deployment limits win. No inferred pricing/output/API capabilities.
  • Recover inherited-history overflow within the same prompt, including its first rejected request. An owned helper atomically commits a prefix marker before the active prompt; current work and queued prompts stay verbatim.
  • Recover oversized summarizer input by bounded hierarchical folding: retain all remaining text portions, fold partial summaries into later portions, commit only the final summary. No excerpt truncation or session deletion.
  • Preserve original history and overflow evidence when recovery cannot complete.

Design

Reuse owned compaction cancellation, budgets and transactional markers. No protocol or SQLite schema change. Tool calls/results become inert evidence only during fallback. At most 32 splits. Addresses both ordinary request overflow and overflow of the summarizer itself, informed by Codex local compaction and OpenCode. Updated as-built architecture docs.

Verification

Regression tests cover same-prompt inherited overflow, oversized summarizer recovery/replay, and manual folding consuming both transcript ends while committing only the final summary. Existing cancellation/budget/failure coverage remains enabled.

cargo xtask check

Head 5f9e31c7e30c vs origin/main; 17 changed file(s); scope: qq, qq-client, qq-config, qq-core, qq-harness, qq-server, qq-tui, xtask.

Gate Result Tests Time Command
fmt pass 1.6s cargo fmt --all -- --check
lint pass 2.1s cargo xtask lint
clippy pass 21.6s cargo clippy --color never -p qq -p qq-client -p qq-config -p qq-core -p qq-harness -p qq-server -p qq-tui -p xtask --all-targets --all-features -- -D warnings
test pass 2361/2361 passed, 14 skipped 296.9s cargo nextest run --color never --profile default -p qq -p qq-client -p qq-config -p qq-core -p qq-harness -p qq-server -p qq-tui -p xtask

All gates passed in 322.3s.

cargo xtask smoke: 9 passed, 0 failed on final head.

Limits

Provider outages, exhausted budgets and irreducible fixed prefixes can still prevent success; failures preserve stored history. No live-provider repair of the original session was attempted; the user DB was read-only throughout. Recovery can add summarizer requests; no cost/latency benchmark was run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T05:10:51.338271Z 1477c11 Manual request
🔒 Security Review ✅ Completed 2026-10-20T03:40:31.440862Z 5f9e31c Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 5f9e31c7e3

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@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: 5f9e31c7e3

ℹ️ 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 thread crates/qq-core/src/runtime/run_loop.rs Resolved
Comment thread crates/qq-config/src/models.rs Resolved
Comment thread crates/qq-core/src/runtime/run_loop.rs Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator 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: cb5c3bf95d

ℹ️ 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 thread crates/qq-core/src/lib.rs Resolved
Comment thread crates/qq-core/src/lib.rs Resolved
Comment thread crates/qq-config/src/lib.rs Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator 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: fdb88ca2f5

ℹ️ 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 thread crates/qq-core/src/runtime/run_loop.rs Resolved
Comment thread crates/qq-core/src/runtime/run_loop.rs Resolved
Comment thread crates/qq-config/src/models.rs Resolved
Comment thread crates/qq-core/src/lib.rs Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Fixed the four latest findings in cca2e11. Seven targeted compaction/config regressions passed; fmt/lint passed, and workspace all-target/all-feature Clippy passed after fixing a collapsible-if warning. The subsequent broad test run was cancelled, so no fresh aggregate pass is claimed. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@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: cca2e11c8f

ℹ️ 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 +1270 to +1271
let inherited_prefix = provider_rejected_window && reducible_messages > 0 && !run.inherited_compaction_attempted
&& sessions::in_run_compaction_boundary(&messages[run_start..], sessions::CONTEXT_PRUNE_KEEP_TURNS).is_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.

P1 Badge Do not compact a synthetic summary-only prefix

When a prompt was admitted through the summary-only fallback (sessions/execution.rs:1784), reducible_messages is 1 even though that synthetic opening deliberately omits retained rows after the latest marker. If the provider then rejects this request for context overflow, this condition treats the opening as the complete inherited prefix; the helper summarizes only that message but commits an unscoped marker through the ordinal immediately before the active prompt. Subsequent replay therefore excludes those omitted post-marker runs from model context without ever summarizing them. Preserve the represented cutoff with the live prefix and either reload the complete span before compacting or disable inherited-prefix recovery for summary-only admission.

Useful? React with 👍 / 👎.

Comment on lines 3073 to +3076
if !summary_text.is_empty() && !summary_continues_truncated_turn {
summary_text.push('\n');
// A later non-continuation reply supersedes a
// completed partial fold, rather than joining it.
summary_text.clear();

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 Preserve faulted-turn fragments on retry

When an internal compaction turn streams text and then hits a recoverable provider fault, the run loop persists that partial assistant message and appends TURN_RETRY_CONTINUE_NOTICE before retrying (runtime/run_loop.rs:2341-2374), but the event's truncated flag remains false. Consequently summary_continues_truncated_turn is false and this branch clears the partial text when the continuation completes. A between-run or manual compaction can therefore fail required-section validation or commit an incomplete summary after successfully recovering from a mid-stream transport fault. Track fault-driven continuations as continuations too, and clear accumulated text only when a genuinely new fold response supersedes it.

Useful? React with 👍 / 👎.

Comment on lines +2050 to +2053
&& (reducible_messages > 0 && !run.inherited_compaction_attempted || sessions::in_run_compaction_boundary(
&messages[reducible_messages.saturating_add(1)..],
sessions::CONTEXT_PRUNE_KEEP_TURNS,
)
.is_some();
).is_some());

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 Allow inherited recovery after compacting active turns

When an overflowing request contains both inherited history and enough active-run turns to compact, the preparation path chooses the active-run boundary first. After that compaction the request is retried at the same turn_ordinal, but reactive_compaction_turn remains set to that ordinal, so a second provider context rejection cannot enter this newly added inherited-prefix alternative. A provider whose actual limit requires reducing both spans therefore still fails even though the inherited prefix remains reducible. Rearm recovery after a successful compaction or track the attempted target so one active-run pass and one inherited-prefix pass can occur without resending an unchanged request.

Useful? React with 👍 / 👎.

@lg2m

lg2m commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator 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: 1477c113c8

ℹ️ 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 thread crates/qq-core/src/lib.rs
let message = error.to_string();
if rejected_before_work
&& kind == RunFailureKind::ProviderContextExceeded
&& ordinal < 64

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 Track fold splits instead of total turn ordinal

When an in-run fold has already produced 64 turns—for example, 16 portions that each needed three output continuations—this guard rejects a later context-overflow split even though SummaryFold still has capacity under its 32-split limit. A sufficiently oversized transcript can therefore fail on a remaining portion solely because earlier portions required continuations; gate on the number of splits, as the regular session fold path does, rather than on the cumulative provider-turn ordinal.

Useful? React with 👍 / 👎.

Comment thread src/runtime.rs
credential_profile,
max_output_tokens,
context_window: metadata.and_then(qq_config::ModelMetadata::context_window),
context_window: provider.model_context_window(snapshot.model().model()),

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 Propagate inferred limits to delegation roster entries

When a recognized Custom/LiteLLM alias is configured as a delegation target rather than the selected route, this effective-limit lookup is bypassed: delegation_roster still populates each entry from raw ModelMetadata::context_window at src/runtime.rs:3531. The compiled system prompt therefore omits the child model's known context capacity even though spawning that same route uses the inferred limit, so the parent receives inconsistent routing guidance; use ProviderConfig::model_context_window for roster entries too.

Useful? React with 👍 / 👎.

if self.splits >= 32 {
return false;
}
let Some(instruction_at) = messages.iter().rposition(|message| message.content().iter().any(|block| matches!(block, qq_provider::ContentBlock::Text { text } if text.contains(crate::sessions::COMPACTION_INSTRUCTION)))) else {

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 Identify the actual compaction instruction message

When a truncated summarizer reply echoes COMPACTION_INSTRUCTION and the subsequent continuation request overflows, this reverse substring search selects the echoed assistant message instead of the original user instruction. split then places an assistant message at the end of each restarted request and treats the real instruction as transcript evidence, so the retry may no longer ask the model to summarize and can fail validation despite further splitting being possible; match the expected user instruction by role and exact shape.

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
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.

1 participant