Skip to content

Commit 47f78a3

Browse files
authored
fix(ai): preserve long Responses sessions after server compaction (#120457)
* fix(ai): preserve Responses server compaction state Persist opaque Responses compaction items as fenced provider replay state so long stateless sessions can resume authoritative compressed history without exposing it in display or diagnostics. Carry state through worker transcripts and prune replay prefixes without splitting tool pairs. Release note: Preserve long OpenAI Responses sessions across server-side compaction and worker restarts. Related: #95788 * test(ai): align long-context fixtures with CI contracts Make tool-result fixtures type-complete, use the canonical model selector helper, remove unused test-helper exports, and route the paid long-context live probe through the dedicated Gateway profile shard. * test(ai): type mocked Responses terminal events Give the mock SSE event collection an explicit open event shape so terminal response events coexist with output-item events under the root test typecheck. * fix(ai): suppress rejected compaction replay Persist a route-fenced suppression tombstone when encrypted-content recovery rejects a compaction item, so later turns do not retry the same opaque state. Preserve the tombstone through transcript redaction and cover successful fallback followed by the next turn. * fix(ai): keep compaction suppression transport-private Keep the suppression contract local to its sole Responses transport owner and make the regression fixture satisfy root type and lint checks without widening the Plugin SDK surface. * refactor(ai): remove compaction suppression re-export * fix(ai): scope compaction suppression to replay route Keep foreign-route rejection tombstones from hiding the newest compatible Responses compaction while preserving same-route suppression. * fix(ai): harden Responses replay recovery Stage encrypted replay recovery so compaction is only suppressed after an attributable rejection. Preserve terminal ordering and keep provider replay within worker frame budgets without truncating opaque state. * refactor(ai): centralize Responses output indexes Keep normalized output identity tracking in the stream-slot owner, move response failure state to its diagnostic owner, and remove the obsolete replay clone export so exact-head static gates remain shrink-only. * fix(ai): retain idless terminal tool identity Use the canonical empty identity only when a provider supplies neither call nor item id, preventing terminal recovery from duplicating a done-only tool call while preserving stronger identities when available. * fix(sessions): hide provider replay from public events * fix(ai): stage encrypted replay recovery * fix(ai): keep replay attempt kind internal * fix(ai): route Azure through replay recovery Use the shared encrypted-content retry owner for Azure Responses so compaction suppression and prompt-observer variants stay coherent across transports. * fix(ai): harden replay persistence boundaries Fence Azure replay by the resolved request endpoint, drop invalid replay during transcript sanitization, and surface worker-launch replay omissions through the existing redacted diagnostic path.
1 parent a901261 commit 47f78a3

62 files changed

Lines changed: 6736 additions & 456 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/help/testing-live.md‎

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,17 @@ Docker notes:
346346
`OPENCLAW_LIVE_CODEX_HARNESS_COMPACTION_STRESS_TURNS` (1-8) and
347347
`OPENCLAW_LIVE_CODEX_HARNESS_LARGE_OUTPUT_BYTES` (100000-800000).
348348
- Full direct-API context: `OPENCLAW_LIVE_CODEX_HARNESS_FULL_CONTEXT=1` applies
349-
the `922000` context and `700000` total compaction limits, sends dense bounded
350-
user turns, runs two explicit native compaction checkpoints per wave, and
351-
continues with later turns after each checkpoint. It requires
349+
the `922000` context and `700000` total automatic-compaction limits, sends
350+
dense bounded user turns without `/compact` or another manual checkpoint,
351+
and requires a later small turn to trigger native automatic compaction. It
352+
requires
352353
`OPENCLAW_LIVE_CODEX_HARNESS_AUTH=api-key` plus an absolute
353354
`OPENCLAW_LIVE_CODEX_HARNESS_MODEL_CATALOG` path. The catalog must expose the
354-
selected model with `max_context_window: 922000` so Codex does not clamp the
355-
override back to its normal catalog window. The ordinary reduced-threshold
356-
stress above keeps the stricter automatic-compaction and hidden-marker
357-
retention assertions.
355+
exact selected model with `context_window: 922000`,
356+
`max_context_window: 922000`, and `auto_compact_token_limit: 700000` so Codex
357+
does not clamp the override back to its normal catalog window. The ordinary
358+
reduced-threshold stress above keeps the stricter automatic-compaction and
359+
hidden-marker retention assertions.
358360
- Optional loop-relay opt-out probe:
359361
`OPENCLAW_LIVE_CODEX_HARNESS_DISABLE_LOOP_RELAY=1`
360362
- The requested thinking preference may map to the nearest effort advertised
@@ -409,11 +411,12 @@ OPENCLAW_LIVE_CODEX_HARNESS=1 \
409411
OPENCLAW_LIVE_CODEX_HARNESS_AUTH=api-key \
410412
OPENCLAW_LIVE_CODEX_HARNESS_FULL_CONTEXT=1 \
411413
OPENCLAW_LIVE_CODEX_HARNESS_MODEL_CATALOG=/absolute/path/to/models-api-1m.json \
412-
OPENCLAW_LIVE_CODEX_HARNESS_MODEL=openai/gpt-5.6-terra \
413-
OPENCLAW_LIVE_CODEX_HARNESS_THINKING=medium \
414+
OPENCLAW_LIVE_CODEX_HARNESS_MODEL=openai/gpt-5.6-sol \
415+
OPENCLAW_LIVE_CODEX_HARNESS_THINKING=low \
414416
OPENCLAW_LIVE_CODEX_HARNESS_COMPACTION_STRESS_TURNS=8 \
415417
OPENCLAW_LIVE_CODEX_HARNESS_LARGE_OUTPUT_BYTES=800000 \
416-
pnpm test:live -- src/gateway/gateway-codex-harness.live.test.ts
418+
OPENCLAW_LIVE_CODEX_HARNESS_DEBUG=1 \
419+
node scripts/test-live.mjs --quiet src/gateway/gateway-codex-harness.live.test.ts
417420
```
418421

419422
GPT-5.6 native Codex matrix:
@@ -424,41 +427,76 @@ OPENCLAW_LIVE_CODEX_HARNESS_AUTH=api-key \
424427
pnpm test:docker:live-codex-harness
425428
```
426429

427-
## Live: OpenAI repeated compaction
428-
429-
- Goal: exercise the embedded OpenClaw `openai-responses` agent loop through at
430-
least two real automatic compactions, then verify a durable marker survives.
431-
- Test: `src/agents/sessions/agent-session.openai-compaction.live.test.ts`
432-
- Enable: `OPENCLAW_LIVE_OPENAI_COMPACTION=1`
433-
- Default model: `gpt-5.6-luna`
434-
- Model override: `OPENCLAW_LIVE_OPENAI_COMPACTION_MODEL=<model>`
435-
- The normal stress mode uses a reduced client context budget to reach the same
436-
real compaction path with bounded API spend.
437-
- Full-context mode sets the client budget to `922000` and compaction reserve to
438-
`222000`, so automatic compaction starts at `700000`. It also requires an
439-
observed provider input count above the `272000` long-context pricing boundary.
430+
## Live: OpenAI long context
431+
432+
- Goal: validate exact-model embedded OpenClaw execution through a
433+
process-owned isolated Gateway, cross the long-context pricing boundary,
434+
observe a first-class OpenAI Responses compaction item, and prove opaque
435+
replay plus prefix pruning on the next request.
436+
- Test: `src/gateway/gateway-openai-long-context.live.test.ts`
437+
- Enable: `OPENCLAW_LIVE_OPENAI_LONG_CONTEXT=1`
438+
- Profiles: `OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_PROFILE=full` selects exact
439+
`openai/gpt-5.6-sol` with a `1050000` total window, `922000` safe active
440+
input, `128000` maximum output, and `700000` compaction threshold. `reduced`
441+
reaches the same transport and persistence path with a smaller budget.
442+
- Metrics: `OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_METRICS=1` emits phase timing and
443+
token observations. These measurements are informational, not pass/fail
444+
latency targets.
445+
- Long output: `OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_OUTPUT=1` requires a
446+
deterministic response between 4000 and 8000 output tokens.
447+
- Optional raw read-tool stress:
448+
`OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_TOOL_OUTPUT=1`. It is not part of the
449+
default recipe because the effective tool surface may use Code Mode instead
450+
of exposing the raw read tool.
440451

441-
Bounded live recipe:
452+
Full `922000` input-budget recipe:
442453

443454
```bash
444-
OPENCLAW_LIVE_TEST=1 \
445-
OPENCLAW_LIVE_OPENAI_COMPACTION=1 \
446-
pnpm test:live -- src/agents/sessions/agent-session.openai-compaction.live.test.ts
455+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT=1 \
456+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_PROFILE=full \
457+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_METRICS=1 \
458+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_OUTPUT=1 \
459+
node scripts/test-live.mjs --quiet src/gateway/gateway-openai-long-context.live.test.ts
447460
```
448461

449-
Full `922000` input-budget recipe:
462+
Reduced-budget recipe:
450463

451464
```bash
452-
OPENCLAW_LIVE_TEST=1 \
453-
OPENCLAW_LIVE_OPENAI_COMPACTION=1 \
454-
OPENCLAW_LIVE_OPENAI_COMPACTION_FULL=1 \
455-
OPENCLAW_LIVE_OPENAI_COMPACTION_MODEL=gpt-5.6-terra \
456-
pnpm test:live -- src/agents/sessions/agent-session.openai-compaction.live.test.ts
465+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT=1 \
466+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_PROFILE=reduced \
467+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_METRICS=1 \
468+
OPENCLAW_LIVE_OPENAI_LONG_CONTEXT_OUTPUT=1 \
469+
node scripts/test-live.mjs --quiet src/gateway/gateway-openai-long-context.live.test.ts
457470
```
458471

472+
### Long-context hard oracles
473+
474+
The full embedded and native recipes are proof runs, not throughput
475+
benchmarks. They fail unless the following runtime contracts hold:
476+
477+
- Runtime and model identity are exact: embedded OpenClaw or native Codex as
478+
requested, both on `openai/gpt-5.6-sol`.
479+
- At least one provider request crosses `272000` input tokens and every call
480+
reports priority service.
481+
- Embedded OpenClaw receives and persists a first-class encrypted Responses
482+
`compaction` item, replays the exact opaque item on the next request, and
483+
prunes the earlier input prefix. The encrypted content must never appear in
484+
display or diagnostics.
485+
- Native Codex reports an effective window of `875900`, grows beyond the
486+
`700000` total-scope threshold without a manual compact, and automatically
487+
compacts on the next turn.
488+
- Each runtime produces a deterministic long response between 4000 and 8000
489+
output tokens and preserves a durable marker through compaction and a
490+
Gateway restart.
491+
492+
Compaction duration, restart latency, turn latency, and total suite duration
493+
are emitted as informational metrics only.
494+
459495
<Warning>
460-
The full mode deliberately crosses OpenAI's long-context pricing boundary and
461-
can make several large API calls. Use it only with explicit spend approval.
496+
The full modes deliberately cross OpenAI's long-context pricing boundary and
497+
make several large API calls. Above `272000` input tokens, the whole request is
498+
2× input/cache and 1.5× output; Fast/Priority doubles that tier again. Use full
499+
mode only with explicit spend approval.
462500
</Warning>
463501

464502
Fresh OpenAI API-key default:

‎docs/plugins/codex-harness.md‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,22 @@ backends.
276276
Codex subscription and direct OpenAI API traffic are separate contracts. The
277277
live ChatGPT/Codex catalog commonly exposes a `272000` token model window,
278278
while OpenAI documents a `1050000` token Platform API window and `128000`
279-
maximum output for GPT-5.5 and GPT-5.6. Reserving the full output allowance
280-
leaves a derived `922000` token input budget. Requests above `272000` input
281-
tokens use OpenAI's higher long-context pricing.
279+
maximum output for GPT-5.5 and GPT-5.6. Both runtime translations use the same
280+
safe arithmetic:
281+
282+
```text
283+
1050000 total - 128000 maximum output = 922000 safe active input
284+
automatic compaction threshold = 700000 active tokens
285+
```
286+
287+
The native Codex translation is not a Responses parameter set. Codex owns the
288+
native thread's context and compaction, so do not add
289+
`responsesServerCompaction` or `responsesCompactThreshold` to a Codex-backed
290+
model.
282291

283292
Start from a complete Codex model catalog compatible with the installed Codex
284-
version. For each direct GPT-5.5 or GPT-5.6 entry that should use long context,
285-
preserve the rest of the descriptor and set:
293+
version. For the exact `gpt-5.6-sol` entry, preserve the rest of the descriptor
294+
and set:
286295

287296
```json
288297
{
@@ -293,7 +302,7 @@ preserve the rest of the descriptor and set:
293302
```
294303

295304
Codex applies its normal 95% effective-window reserve to the `922000` catalog
296-
value, so it reports about `875900` usable tokens. Compacting at `700000`
305+
value, so it reports exactly `875900` usable tokens. Compacting at `700000`
297306
leaves `175900` tokens before that effective guard and `222000` before the
298307
provider-safe input allowance. This larger margin is deliberate: Codex checks
299308
already-recorded context before adding the next user message and context
@@ -305,7 +314,7 @@ read the API key from a system keychain or secret manager while the normal
305314
ChatGPT login remains available for connectors:
306315

307316
```toml
308-
model = "gpt-5.6-terra"
317+
model = "gpt-5.6-sol"
309318
model_provider = "openai_api_direct"
310319
model_context_window = 922000
311320
model_auto_compact_token_limit = 700000
@@ -327,8 +336,10 @@ refresh_interval_ms = 300000
327336
The auth helper must print only the key to stdout. Do not put it in TOML.
328337

329338
For the OpenClaw Codex app-server harness, keep the default agent-scoped Codex
330-
home and let OpenClaw inject an `openai` API-key profile. Pass the catalog and
331-
context limits as native Codex app-server arguments:
339+
home and let OpenClaw inject an `openai` API-key profile. Create the profile by
340+
the normal OpenAI API-key auth flow, put its actual id first in
341+
`auth.order.openai`, and pass the catalog and context limits as native Codex
342+
app-server arguments:
332343

333344
```json5
334345
{
@@ -363,30 +374,56 @@ context limits as native Codex app-server arguments:
363374
},
364375
agents: {
365376
defaults: {
366-
model: "openai/gpt-5.6-terra",
377+
model: { primary: "openai/gpt-5.6-sol" },
367378
models: {
368-
"openai/gpt-5.6-terra": { agentRuntime: { id: "codex" } },
379+
"openai/gpt-5.6-sol": {
380+
agentRuntime: { id: "codex" },
381+
params: { fastMode: true },
382+
},
369383
},
370384
},
371385
},
372386
}
373387
```
374388

375-
Replace `openai:api-key` with the actual API-key profile id if needed. The
389+
Replace `openai:api-key` with the actual API-key profile id. The
376390
agent-scoped app-server receives only that prepared key; the operator's native
377391
`~/.codex` ChatGPT login, plugins, connectors, and thread store remain
378392
untouched. Use the injected agent-scoped API-key path above for this route
379393
rather than relying on `homeScope: "user"` to provide the intended credential.
380394

395+
The model catalog, `model_context_window`, total-scope automatic compaction
396+
limit, exact `openai/gpt-5.6-sol` route, and API-key profile order form one
397+
configuration unit. Apply them together. OpenClaw can keep embedded and native
398+
long-context choices at the same time only when their model refs or agent
399+
configurations are distinguishable; one model entry cannot carry both
400+
runtime-owned compaction strategies.
401+
381402
After changing the catalog or app-server arguments, restart the Gateway and
382-
start a fresh chat. Existing native threads preserve their recorded provider
383-
and model settings. Verify the runtime with `/status` and `/codex status`, then
384-
send a harmless direct API turn before starting a long session.
403+
native Codex app-server, then start a fresh chat. Run `/model default -s` when
404+
an existing session has a model or runtime override. Existing native threads
405+
preserve their recorded provider and model settings. Verify the runtime with
406+
`/status` and `/codex status`, then send a harmless direct API turn before
407+
starting a long session.
408+
409+
A process-owned isolated Gateway and app-server run verified this exact
410+
`openai/gpt-5.6-sol` API-key configuration. Codex reported an effective window
411+
of `875900`. Active context grew from `197032` to `377386`, `561957`, and
412+
`750745` tokens without manual compaction; the next small turn triggered
413+
automatic compaction to `75980` active tokens, with a minimum after-compaction
414+
snapshot of `68375`. Compaction took `2810` ms and persisted a count of one. A
415+
durable marker survived compaction and restart, a deterministic long response
416+
produced `5442` output tokens, every call reported `serviceTier: priority`, and
417+
the full suite took `401.37` seconds. These timings are observations, not
418+
service-level guarantees.
385419

386420
<Warning>
387-
Long context is deliberately opt-in. OpenAI bills the entire request at 2×
388-
input and 1.5× output rates once input exceeds `272000` tokens. The API remains
389-
authoritative for access, actual limits, and billing. See
421+
Long context is deliberately opt-in. Once input exceeds `272000` tokens,
422+
OpenAI bills the entire request at 2× input and cache rates and 1.5× output
423+
rates. Fast/Priority adds another 2× tier, so long-context Fast traffic is 4×
424+
short-context Standard input-side pricing and 3× short-context Standard output
425+
pricing. The API remains authoritative for access, actual limits, and billing.
426+
See
390427
[OpenAI model limits](https://developers.openai.com/api/docs/models/compare) and
391428
[API pricing](https://developers.openai.com/api/docs/pricing).
392429
</Warning>

0 commit comments

Comments
 (0)