Skip to content

perf(runtime): prune at seams so each run extends the last request (CX3) - #254

Merged
lg2m merged 7 commits into
feat/eng-997-cx4-compacting-activityfrom
perf/eng-996-cx3-prune-watermark
Oct 7, 2026
Merged

lg2m merged 7 commits into
feat/eng-997-cx4-compacting-activityfrom
perf/eng-996-cx3-prune-watermark

Conversation

@lg2m

@lg2m lg2m commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

CX3: prune at seams so each run extends the last request

Spec: docs/plans/compaction.md § CX3 · ADR-0056 § 6 · Linear: ENG-996 (plan ENG-992)
Stacked on #252 (CX4): merge that first.

Why

Between runs, assembly stubbed every read-only result older than 4 turns, but a live run stubbed only on overflow. Each run's first request therefore rewrote the middle of the previous run's last request: 6 of 18 Anthropic follow-ups within 5 minutes mostly missed the cache in the CX0 baseline.

What changed

  • Durable prune watermark. Store schema 41 → 42 adds sessions.prune_through_ordinal and sessions.prune_through_turn. Assembly stubs as if the context ended at that turn. Between seams, every request extends the last one byte for byte.
  • Live overflow-prune seam. After stubbing, the loop yields RuntimeEvent::ContextPruned. The session commits the watermark before the stubbed request is sent, so the next run replays the same stubs.
  • Proactive-threshold seam. On ContextPlan::Compact, the prompt first moves the watermark to the newest prior turn and reassembles, once per run. If the result fits, no summarizer runs, which is cheaper than compacting. Otherwise it compacts as before.
  • Bug fix. A live run re-stubbed its own stubs on later overflowing turns, so the stub named the earlier stub's size, which disagreed with replay. prunable_stub now never stubs a stub.
  • Upgrade. Upgraded sessions start at their newest turn and assemble exactly as at 41. The watermark never moves backwards.

Verification

  • cargo test --workspace: 2 105 passed, 13 ignored. Soak: 7 passed. fmt and clippy -D warnings are clean.
  • New tests. The first three fail when their mechanism is disabled.
    • each_run_extends_the_previous_runs_last_request_until_a_seam: the golden test that each run's first request extends the previous run's last one.
    • a_live_prune_moves_the_watermark_and_the_next_run_extends_it
    • the_proactive_threshold_stubs_stale_reads_before_it_compacts
    • version_forty_one_gains_the_prune_watermark_at_the_newest_turn: the schema upgrade from 41.
  • Reference oracle. The assembly reference oracle applies the watermark independently.
  • Adjusted tests. Tests that check stubbing itself now set a seam explicitly with mark_prune_seam.

Performance

context_assembly assemble times, paired against the CX4 tip on the same host. They are within noise.

archived runs base CX3
10 60.5 / 52.4 µs 55.4 / 56.2 µs
1 000 56.8 / 60.8 µs 57.3 / 92.4 µs (one outlier)
10 000 67.7 / 60.3 µs 66.0 / 62.2 µs

The change costs one extra row read per assembly and one write per seam.

Compatibility

  • Store schema 42: older binaries cannot open an upgraded store, as with every schema bump. There is no protocol change; it stays 31 from feat(protocol)!: runs report a compacting activity (CX4) #252.
  • Between seams, old read-only results stay verbatim until the threshold. Context grows a little faster, but the prefix cache hits.

Next

CX5 is live qualification: re-run the CX0 query 7 days after this merges.

lg2m added a commit that referenced this pull request Oct 5, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 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-07T17:12:46.926184Z ee55433 New commits
ℹ️ 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 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: f5fda8e3ac

ℹ️ 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".

@lg2m
lg2m added this pull request to stack #255 October 5, 2026 16:36
lg2m added a commit that referenced this pull request Oct 5, 2026
lg2m added a commit that referenced this pull request Oct 5, 2026
The re-stub guard matched any result whose last line began with
"[pruned: ", so a large read-only result ending that way was never
pruned. Now content is skipped only when it is exactly the stub this
call would produce (an optional header line, then the stub line with
this call's name, arguments and hint).

Addresses Codex review on #254.

Refs: ENG-992, ENG-996
@lg2m
lg2m force-pushed the perf/eng-996-cx3-prune-watermark branch from 95e81f1 to d9feda8 Compare October 5, 2026 17:37

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

ℹ️ 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".

lg2m added a commit that referenced this pull request Oct 7, 2026
lg2m added a commit that referenced this pull request Oct 7, 2026
The re-stub guard matched any result whose last line began with
"[pruned: ", so a large read-only result ending that way was never
pruned. Now content is skipped only when it is exactly the stub this
call would produce (an optional header line, then the stub line with
this call's name, arguments and hint).

Addresses Codex review on #254.

Refs: ENG-992, ENG-996
@lg2m
lg2m force-pushed the perf/eng-996-cx3-prune-watermark branch from d9feda8 to 3812ce1 Compare October 7, 2026 05:16

@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: 3812ce17fc

ℹ️ 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
lg2m added a commit that referenced this pull request Oct 7, 2026
lg2m added a commit that referenced this pull request Oct 7, 2026
The re-stub guard matched any result whose last line began with
"[pruned: ", so a large read-only result ending that way was never
pruned. Now content is skipped only when it is exactly the stub this
call would produce (an optional header line, then the stub line with
this call's name, arguments and hint).

Addresses Codex review on #254.

Refs: ENG-992, ENG-996
@lg2m
lg2m force-pushed the perf/eng-996-cx3-prune-watermark branch from 3812ce1 to a143e6e Compare October 7, 2026 05:30
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

lg2m added a commit that referenced this pull request Oct 7, 2026
…ffect

The live overflow prune knew effects only for this run's calls, so an
earlier run's read-only result outside the built-in names (load_skill,
spawn_agent, select_tools) stayed verbatim live but was stubbed on
replay, breaking the cross-run extension at that seam. Assembly now also
returns stored effects in result order; the claim and every reload carry
them into the run, and the live prune classifies inherited results by
them exactly as assembly does.

Addresses the third Codex review on #254.

Refs: ENG-992, ENG-996

@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: 4a9d4561f5

ℹ️ 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".

lg2m added a commit that referenced this pull request Oct 7, 2026
lg2m added a commit that referenced this pull request Oct 7, 2026
The re-stub guard matched any result whose last line began with
"[pruned: ", so a large read-only result ending that way was never
pruned. Now content is skipped only when it is exactly the stub this
call would produce (an optional header line, then the stub line with
this call's name, arguments and hint).

Addresses Codex review on #254.

Refs: ENG-992, ENG-996
lg2m added a commit that referenced this pull request Oct 7, 2026
…ffect

The live overflow prune knew effects only for this run's calls, so an
earlier run's read-only result outside the built-in names (load_skill,
spawn_agent, select_tools) stayed verbatim live but was stubbed on
replay, breaking the cross-run extension at that seam. Assembly now also
returns stored effects in result order; the claim and every reload carry
them into the run, and the live prune classifies inherited results by
them exactly as assembly does.

Addresses the third Codex review on #254.

Refs: ENG-992, ENG-996
@lg2m
lg2m force-pushed the perf/eng-996-cx3-prune-watermark branch from 4a9d456 to 22f4104 Compare October 7, 2026 16:33
lg2m added a commit that referenced this pull request Oct 7, 2026
…t a prompt

- The re-stub guard's optional header must pass the same bounded
  header_line check the stub is built with, and the size must be a u64;
  a large first line before a stub-shaped line no longer escapes pruning.
- A summarizer's live prune is not a seam (its request is not a
  transcript), and advance_prune_watermark fails instead of silently
  succeeding for a run with no prompt row.

Addresses the fourth Codex review on #254.

Refs: ENG-992, ENG-996
lg2m added 6 commits October 7, 2026 09:50
Assembly used to stub every stale read-only result between runs while a
live run stubbed only on overflow, so a run's first request rewrote the
middle of the previous run's last request and missed the provider cache.

- Store schema 41 -> 42: sessions.prune_through_ordinal/_turn, a durable
  prune watermark. Assembly stubs as if the context ended at that turn;
  between seams each request extends the last one byte for byte.
- Seams: the live overflow prune (RuntimeEvent::ContextPruned, committed
  before the stubbed request is sent) and the proactive threshold, which
  now moves the watermark and reassembles once before it compacts; if the
  stubbed context fits, no summarizer runs.
- Fix: a live run re-stubbed its own stubs on later overflowing turns, so
  the stub named the previous stub's size and disagreed with replay.
- Upgraded sessions start at their newest turn and assemble as before.

context_assembly is within noise against the CX4 tip.

Refs: ENG-992, ENG-996
The re-stub guard matched any result whose last line began with
"[pruned: ", so a large read-only result ending that way was never
pruned. Now content is skipped only when it is exactly the stub this
call would produce (an optional header line, then the stub line with
this call's name, arguments and hint).

Addresses Codex review on #254.

Refs: ENG-992, ENG-996
…window

Codex flagged that a new session's unset watermark would leave a
windowless model unstubbed until the 4 MiB backstop refused it. The
backstop plans Compact, which runs the threshold seam; this end-to-end
test pins that (it fails at run 156 with the seam disabled). tools.md
names the backstop as part of the seam.

Refs: ENG-992, ENG-996
…ffect

The live overflow prune knew effects only for this run's calls, so an
earlier run's read-only result outside the built-in names (load_skill,
spawn_agent, select_tools) stayed verbatim live but was stubbed on
replay, breaking the cross-run extension at that seam. Assembly now also
returns stored effects in result order; the claim and every reload carry
them into the run, and the live prune classifies inherited results by
them exactly as assembly does.

Addresses the third Codex review on #254.

Refs: ENG-992, ENG-996
…t a prompt

- The re-stub guard's optional header must pass the same bounded
  header_line check the stub is built with, and the size must be a u64;
  a large first line before a stub-shaped line no longer escapes pruning.
- A summarizer's live prune is not a seam (its request is not a
  transcript), and advance_prune_watermark fails instead of silently
  succeeding for a run with no prompt row.

Addresses the fourth Codex review on #254.

Refs: ENG-992, ENG-996
main's AP4.2 (#257) took schema 42 (child_reports). The prune watermark
is now step 43; step 42 accepts 43, every version list gains it, and the
migration test upgrades from 42. The live-seam test's window grows with
the larger tool block so its second prompt is not itself a seam, and it
now asserts the watermark held still between the runs.

Refs: ENG-992, ENG-996
@lg2m
lg2m force-pushed the perf/eng-996-cx3-prune-watermark branch from 2c48484 to ee55433 Compare October 7, 2026 17:04

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

ℹ️ 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 +316 to +320
let mut ordered = Vec::new();
for (message_index, message) in context.iter().enumerate() {
for (block_index, block) in message.content().iter().enumerate() {
if matches!(block, ContentBlock::ToolResult { .. }) {
ordered.push(effects.get(&(message_index, block_index)).copied());

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 Exercise effect collection in the assembly benchmark

Production claims now use load_model_context_with_effects, so sessions with many tool results pay this additional full-context traversal and Vec allocation before their first request. However, the context_assembly benchmark still enters through bench_support::assemble, which calls load_model_context and skips this new work; consequently the ledger's within-noise result does not measure the changed production hot path. Update the benchmark entry point to collect effects as claims do.

AGENTS.md reference: AGENTS.md:L153-L154

Useful? React with 👍 / 👎.

Comment thread docs/plans/compaction.md
### CX3 — Durable prune watermark
**Inputs:** CX2
**Owned paths:** `crates/qq-core/src/sessions/{transcript,context}.rs`, schema (41 → 42), `crates/qq-core/src/lib.rs` (live overflow prune)
**Owned paths:** `crates/qq-core/src/sessions/{transcript,context}.rs`, schema (42 → 43; main took 42 for AP4.2), `crates/qq-core/src/lib.rs` (live overflow prune)

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 Declare the files CX3 actually owns

The slice still declares only transcript.rs, context.rs, the schema, and lib.rs as owned, but this commit also changes runtime/events.rs and the session claim, compaction, execution, settlement, store, and test modules. docs/plans/workflow.md makes edits outside a slice's owned paths blocking during review, so the plan must expand and coordinate this ownership or the out-of-scope edits must be removed before merge.

AGENTS.md reference: AGENTS.md:L14-L19

Useful? React with 👍 / 👎.

@lg2m
lg2m merged commit 065cdf3 into main Oct 7, 2026
6 checks passed
@lg2m
lg2m deleted the perf/eng-996-cx3-prune-watermark branch October 7, 2026 17:34
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