Skip to content

fix(context-engine): long sessions continue past 20k events - #122149

Merged
jalehman merged 2 commits into
mainfrom
oc-5b3/canonical-commit-turn
Aug 11, 2026
Merged

jalehman merged 2 commits into
mainfrom
oc-5b3/canonical-commit-turn

Conversation

@jalehman

@jalehman jalehman commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Related: #121647

What Problem This Solves

Fixes an issue where context-engine plugins implementing commitTurn(...) receive the complete session prefix for every accepted turn. Once that prefix exceeds 20,000 events or 8 MiB, durable advancement is blocked even when the new turn is small.

The interim repair added commitTurnLocal(...) beside commitTurn(...) and persisted which method recovery should call. The original commitTurn(...) contract was not included in a public release, so the duplicate API and recovery paths do not serve a released compatibility requirement.

Why This Change Was Made

commitTurn(...) now receives only messages from the accepted turn's admission event through its terminal event. The existing 20,000-event and 8 MiB sentinel limits remain in place and apply to that bounded range. Transcript bootstrap and rebuild continue to use the bounded cursor API.

The change removes commitTurnLocal(...), persisted contract discrimination, and legacy recovery dispatch so live commits and recovered commits use the same payload and method.

User Impact

Long sessions continue advancing context-engine state after their total history exceeds 20,000 events. A single accepted turn that exceeds the event or byte limit remains blocked as an explicit resource-safety outcome.

Plugin developers implement one durable commit method. This changes no contract included in a public release.

AI-assisted with Codex. I reviewed the implementation and understand the resulting commit, recovery, and bootstrap behavior.

Evidence

  • The regression coverage builds a session with a large historical prefix and verifies that the next small turn commits with only its user and assistant messages.
  • Oversized-current-turn coverage verifies that the 20,000-event and 8 MiB guards still reject a single excessive turn.
  • Retry, recovery, stale-terminal, and ordering tests verify the same turn-local payload across live and recovered dispatch.
  • node scripts/run-vitest.mjs src/agents/harness/context-engine-turn-attempt.test.ts src/agents/harness/context-engine-turn-outbox.test.ts src/context-engine/host-compat.test.ts src/context-engine/context-engine.test.ts passed 81 tests.
  • node scripts/run-vitest.mjs src/context-engine/host-param-projection.test.ts src/agents/harness/context-engine-turn-attempt.test.ts src/agents/harness/context-engine-turn-outbox.test.ts passed 19 tests on the rebased head.
  • pnpm check:test-types passed on the rebased head.
  • pnpm plugin-sdk:api:check passed on the rebased head.
  • .agents/skills/autoreview/scripts/autoreview --mode local reported no actionable findings with 0.99 correctness confidence.

Agent Transcript

Redacted Codex session transcript
source: local Codex session, task-relevant excerpt
redaction: local paths, raw tool output, system instructions, credentials, and unrelated turns omitted

[user]
the intended fix involves using the transcript cursor API in place of naive reading of the entire transcript up to 20k events?

[assistant]
Not exactly. Normal `commitTurn` reads only the exact admission-to-terminal range using the stored turn anchors. Bootstrap and rebuild use the transcript cursor to consume older history in bounded pages. The 20k and 8 MiB caps therefore protect one turn rather than limiting the session's lifetime history.

[user]
Why did we end up with commitTurnLocal instead of the original design?

[user]
commitTurn was unreleased code. why do we need backward compatibility?

[user]
Ok. Let's create a new PR to set this right.

[assistant]
The replacement restores one `commitTurn` contract with turn-local messages. It removes `commitTurnLocal`, persisted contract discrimination, legacy recovery dispatch, compatibility-only tests, and the provisional SDK documentation. Focused context-engine tests pass, generated SDK baselines match, and autoreview reports no actionable findings.

@clawsweeper

clawsweeper Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Aug 11, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 11, 2026
@clawsweeper

clawsweeper Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 11, 2026, 1:53 PM ET / 17:53 UTC.

ClawSweeper review

What this changes

This PR removes the provisional duplicate durable context-engine callback and makes accepted-turn commits and recovery deliver only the admitted-to-terminal transcript range.

Regression provenance

Possible regression — probable (reviewed change; known regression link). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open for normal protected-PR handling: the prior type-check finding is fixed, and the branch consistently uses one bounded durable commit contract; exact-head CI is still running.

Priority: P2
Reviewed head: a9a7dca70130fc47a3be84fb36b4244aa75fc38a

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch is a coherent net-negative contract cleanup with focused behavioral evidence; required exact-head CI is still in progress.
Proof confidence 🌊 off-meta tidepool Not applicable: This is a maintainer-authored internal runtime PR; the external-contributor proof gate does not apply, and the body supplies focused after-fix harness results.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a maintainer-authored internal runtime PR; the external-contributor proof gate does not apply, and the body supplies focused after-fix harness results.
Evidence reviewed 7 items Current-main defect path: Current main chooses the full-transcript range for the original atomic contract, so its event cap can include historical session messages rather than only the accepted turn.
Canonical bounded producer: The PR removes the range mode and always bounds the SQL read from admission through terminal, preserving the event and byte sentinels for one accepted turn.
Recovery uses the same payload: Recovered accepted rows reread the same bounded range and enqueue the same single-method commit payload as the live path.
Findings None None.
Security None None.

How this fits together

The agent harness records an accepted transcript turn, bounds its messages, and queues a durable context-engine commit. The outbox retries that same commit after interruption, so live and recovery paths must provide identical payload semantics to the plugin.

flowchart LR
A[Session transcript] --> B[Accepted turn]
B --> C[Bounded range reader]
C --> D[Durable commit outbox]
D --> E[Context-engine callback]
E --> F[Plugin context state]
D --> G[Restart recovery]
G --> E
Loading

Before merge

  • Resolve merge risk (P1) - This removes a plugin callback and changes durable outbox payload interpretation; it is not in a release, but maintainers should accept the source-main upgrade boundary after exact-head CI completes.
  • Complete next step (P2) - This protected maintainer PR needs exact-head CI and normal merge review; no narrow repair-lane change is outstanding.
Agent review details

Security

None.

PR surface

Source -77, Tests -111, Docs -19, Generated 0. Total -207 across 32 files.

View PR surface stats
Area Files Added Removed Net
Source 6 12 89 -77
Tests 3 18 129 -111
Docs 2 8 27 -19
Config 0 0 0 0
Generated 21 21 21 0
Other 0 0 0 0
Total 32 59 266 -207

Review metrics

Metric Value Why it matters
Production vs test LOC production +12/-89; tests +18/-129; docs +8/-27; generated baselines +21/-21 The fix is net-negative production cleanup while preserving focused behavioral coverage.
Durable commit API 1 callback removed; 1 contract variant removed The public plugin-facing durable-turn surface is consolidated to one callback and needs compatibility review.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/agents/harness/context-engine-turn-outbox.test.ts, serialized state: src/config/sessions/session-accessor.transcript-range.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Accept the unreleased contract consolidation (recommended)
    Merge after exact-head CI if maintainers accept removal of the unshipped callback and its provisional outbox fields.
  2. Retain temporary compatibility
    Keep the removed callback and payload discriminator only if maintainers identify a supported pre-release upgrade population that requires it.

Technical review

Best possible solution:

Land the single atomic-idempotent contract after exact-head CI succeeds, keeping bounded transcript reads at the producer and the same projection in recovery rather than retaining an unreleased compatibility branch.

Do we have a high-confidence way to reproduce the issue?

Yes in source: the focused regression fixture creates an over-8 MiB historical prefix followed by a small accepted turn and verifies that only the new turn is committed; this read-only review did not execute it.

Is this the best way to solve the issue?

Yes. The PR repairs the range producer and its live and recovery consumers, while bootstrap/rebuild keep the existing cursor API; retaining the duplicate unshipped callback would preserve unnecessary policy branches.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d9766c3d5723.

Labels

Label changes:

  • add merge-risk: 🚨 compatibility: It removes a plugin callback and changes the interpretation of persisted durable-turn payloads, even though the interim contract is unshipped.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored internal runtime PR; the external-contributor proof gate does not apply, and the body supplies focused after-fix harness results.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: The patch addresses durable context advancement in long-running sessions without an immediate release-blocking failure.
  • merge-risk: 🚨 compatibility: It removes a plugin callback and changes the interpretation of persisted durable-turn payloads, even though the interim contract is unshipped.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored internal runtime PR; the external-contributor proof gate does not apply, and the body supplies focused after-fix harness results.

Evidence

What I checked:

Likely related people:

  • SunnyShu0925: The related merged PR introduced the bounded durable-turn and provisional compatibility design that this PR consolidates. (role: introduced the related durable-turn implementation; confidence: high; commits: d758ec48d4d0; files: src/config/sessions/session-accessor.transcript-range.ts, src/agents/harness/context-engine-turn-outbox.ts)
  • Shakker: Current-main blame for the pre-PR range-selection code resolves to this recent source history. (role: recent adjacent source contributor; confidence: medium; commits: 4def22ebfcf1; files: src/config/sessions/session-accessor.transcript-range.ts, src/context-engine/types.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Let exact-head required CI complete before merge.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-08-11T16:55:16.671Z sha a58f1ed7886009395ed6dd3718bfaaeee486c108 :: needs changes before merge. :: [P1] Remove the obsolete test argument

@jalehman
jalehman force-pushed the oc-5b3/canonical-commit-turn branch from a58f1ed to c67897c Compare August 11, 2026 17:06
@jalehman jalehman changed the title refactor(context-engine): restore one turn-local commit contract fix(context-engine): long sessions continue past 20k events Aug 11, 2026
@jalehman

Copy link
Copy Markdown
Contributor Author

Addressed the ClawSweeper review on exact head c67897c753b:

  • Removed obsolete prePromptMessageCount from the commitTurn projection test.
  • Removed the stale persisted turnAdvancementIdempotency test argument exposed by pnpm check:test-types.
  • Passed the requested focused context-engine tests: 19 tests.
  • Passed pnpm check:test-types.
  • Passed pnpm plugin-sdk:api:check.
  • Rebased onto main; GitHub reports the PR mergeable.
  • Rewrote the title and description to follow .github/pull_request_template.md and CONTRIBUTING.md.

Exact-head CI is running. The PR description now contains the durable evidence record.

@jalehman
jalehman force-pushed the oc-5b3/canonical-commit-turn branch from c67897c to a9a7dca Compare August 11, 2026 17:50
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 11, 2026
@jalehman
jalehman force-pushed the oc-5b3/canonical-commit-turn branch from a9a7dca to d8d9f59 Compare August 11, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant