Skip to content

Commit d758ec4

Browse files
SunnyShu0925claudejalehman
authored
fix(context-engine): durable state stalls in long sessions (#121647)
* [AI] fix(context-engine): bound accepted-turn cap to the current turn range The accepted-turn transcript read applied the 8 MiB / 20k-event cap to the entire active prefix instead of the admitted turn range, so durable context engines permanently blocked every later small turn once session history exceeded the cap. Restrict the cap check to rows at or after the admission message position while preserving the full messages + prePromptMessageCount payload contract. Fixes #121623 Co-Authored-By: Claude <[email protected]> * [AI] test(context-engine): clean up large-prefix regression temp dirs ClawSweeper P3: the two new accepted-turn regression cases created multi-megabyte SQLite fixture directories that were never removed. Track them and delete them in afterEach. Related to #121623 Co-Authored-By: Claude <[email protected]> * oc-136: make durable turn commits turn-local * oc-514: use canonical temp-dir cleanup helper * oc-514.7: version turn-local durable commits * oc-514.9: preserve legacy durable rows * oc-514.8: refresh plugin SDK API baseline --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Josh Lehman <[email protected]>
1 parent 4b08d9a commit d758ec4

12 files changed

Lines changed: 571 additions & 197 deletions

‎docs/.generated/plugin-sdk-api-baseline.jsonl‎

Lines changed: 115 additions & 115 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/concepts/context-engine.md‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default function register(api) {
132132
acceptedHostParams: ["sessionKey", "runtimeContext"],
133133
transcriptSemantics: {
134134
currentTurnFence: "before-current-turn-entry-v1",
135-
turnAdvancementIdempotency: "atomic-idempotent-v1",
135+
turnAdvancementIdempotency: "atomic-idempotent-turn-local-v1",
136136
},
137137
},
138138

@@ -167,12 +167,19 @@ export default function register(api) {
167167
},
168168

169169
async commitTurn({ advancementKey, messages, prePromptMessageCount }) {
170+
// Retain v1 while durable rows from older plugin versions can exist.
171+
return await commitAcceptedTurn({
172+
advancementKey,
173+
messages: messages.slice(prePromptMessageCount),
174+
});
175+
},
176+
177+
async commitTurnLocal({ advancementKey, messages }) {
170178
// Atomically store the accepted turn and advancementKey. Return
171179
// "duplicate" when that exact key was committed by an earlier retry.
172180
return await commitAcceptedTurn({
173181
advancementKey,
174182
messages,
175-
prePromptMessageCount,
176183
});
177184
},
178185
}));
@@ -225,16 +232,26 @@ receive every current host field.
225232
For durable admitted turns, declare both transcript semantics:
226233

227234
- `currentTurnFence: "before-current-turn-entry-v1"`
228-
- `turnAdvancementIdempotency: "atomic-idempotent-v1"`
235+
- `turnAdvancementIdempotency: "atomic-idempotent-turn-local-v1"`
229236

230-
and implement `commitTurn(...)` as one atomic, idempotent write keyed by
237+
and implement `commitTurnLocal(...)` as one atomic, idempotent write keyed by
231238
`advancementKey`. Return `{ status: "committed" }` for the first write and
232239
`{ status: "duplicate" }` when a host retry presents an already-committed key.
240+
The `messages` payload contains only the inclusive range from the admitted user
241+
entry through the accepted terminal entry. Engines that need the earlier
242+
transcript during bootstrap or rebuild should read it through the transcript
243+
cursor API, `readSessionTranscriptVisibleMessageDelta(...)`.
233244
Pre-turn transcript reads during bootstrap, maintenance, assembly, and retries
234245
then see the exact transcript prefix before the admitted user message. The host
235-
calls `commitTurn` only for the accepted successful turn; failed or aborted
246+
calls `commitTurnLocal` only for the accepted successful turn; failed or aborted
236247
turns do not advance context-engine state.
237248

249+
The turn-local contract is additive: engines must also retain `commitTurn(...)`
250+
with the older `atomic-idempotent-v1` full-history semantics. OpenClaw uses it
251+
to drain durable v1 work queued before a plugin upgrade. New commits use
252+
`commitTurnLocal(...)`, so transcript history is obtained through the cursor
253+
API instead of being repeated in every durable commit.
254+
238255
Without the full declaration and method, OpenClaw uses the legacy context path
239256
for the whole logical turn, including retries. The configured context-engine
240257
slot is not changed, and OpenClaw tries the configured engine again on the next

‎docs/plugins/sdk-overview.md‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,19 @@ For an end-to-end authoring guide, see
626626

627627
To participate in durable admitted turns, context engines must declare
628628
`currentTurnFence: "before-current-turn-entry-v1"` and
629-
`turnAdvancementIdempotency: "atomic-idempotent-v1"` under
630-
`info.transcriptSemantics`, then implement `commitTurn(...)` as an atomic,
631-
idempotent write keyed by `advancementKey`. Without the full contract, OpenClaw
632-
uses the legacy context path for the whole logical turn and its retries, leaves
633-
the configured engine unchanged, and tries that engine again on the next
634-
logical turn.
629+
`turnAdvancementIdempotency: "atomic-idempotent-turn-local-v1"` under
630+
`info.transcriptSemantics`, then implement `commitTurnLocal(...)` as an atomic,
631+
idempotent write keyed by `advancementKey`. Retain `commitTurn(...)` with its
632+
v1 full-history behavior so queued work survives plugin upgrades. OpenClaw supplies only the inclusive
633+
accepted turn, from its admitted user entry through its terminal entry; use the
634+
`readSessionTranscriptVisibleMessageDelta(...)` cursor API to bootstrap or
635+
rebuild earlier history. Without the full contract, OpenClaw uses the legacy
636+
context path for the whole logical turn and its retries, leaves the configured
637+
engine unchanged, and tries that engine again on the next logical turn.
638+
639+
The older `atomic-idempotent-v1` and `commitTurn(...)` contract supplies
640+
transcript history plus `prePromptMessageCount`. The turn-local declaration is
641+
valid only when the engine implements both commit methods.
635642

636643
### Deprecated memory embedding adapters
637644

‎src/agents/harness/context-engine-logical-turn.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { sanitizeForLog } from "../../../packages/terminal-core/src/ansi.js";
22
import type { OpenClawConfig } from "../../config/types.openclaw.js";
33
import {
44
evaluateContextEngineHostSupport,
5+
supportsContextEngineDurableTurnAdvancement,
56
type ContextEngineHostSupport,
67
} from "../../context-engine/host-compat.js";
78
import { ensureContextEnginesInitialized } from "../../context-engine/init.js";
@@ -149,9 +150,7 @@ export async function createContextEngineLogicalTurnLease(params: {
149150
}
150151
if (
151152
selection.requiresDurableCommit &&
152-
(effective.engine.info.transcriptSemantics?.turnAdvancementIdempotency !==
153-
"atomic-idempotent-v1" ||
154-
typeof effective.engine.commitTurn !== "function")
153+
!supportsContextEngineDurableTurnAdvancement(effective.engine)
155154
) {
156155
return "atomic idempotent turn advancement is not declared";
157156
}

0 commit comments

Comments
 (0)