Skip to content

fix(codex): rotate incompatible context-engine threads - #81223

Merged
jalehman merged 3 commits into
openclaw:mainfrom
jalehman:buce/codex-context-engine-sidecar-policy
May 13, 2026
Merged

jalehman merged 3 commits into
openclaw:mainfrom
jalehman:buce/codex-context-engine-sidecar-policy

Conversation

@jalehman

@jalehman jalehman commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Lossless-managed Codex sessions could blindly resume an old native Codex backend thread, making Codex hidden history an untracked second source of context.
  • Why it matters: an otherwise valid Lossless projection could fail with a Codex context-window error and then incorrectly persist fallback to MiniMax.
  • What changed: Codex app-server sidecar bindings now carry context-engine metadata, legacy/incompatible context-engine bindings rotate to a fresh native thread, and early context-window overflow can retry once fresh before model fallback.
  • What did NOT change: plain non-Lossless Codex sessions still resume their native backend thread normally, and Codex native compaction remains Codex-local.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #
  • Related #
  • This PR fixes a bug or regression

Real behavior proof (required for external PRs)

  • Behavior or issue addressed: A Lossless-managed Telegram topic with a stale Codex app-server sidecar should rotate to a fresh Codex backend thread instead of resuming stale hidden native history; the fallback to MiniMax should not persist for this context-window failure path.
  • Real environment tested: Local macOS OpenClaw gateway on branch buce/codex-context-engine-sidecar-policy, Telegram forum topics 2268 and 3731, Lossless Claw enabled, Codex app-server harness using openai/gpt-5.5.
  • Exact steps or command run after this patch: Built the branch with pnpm build; restarted the local gateway with launchctl kickstart -k gui/$(id -u)/ai.openclaw.gateway; sent live Telegram messages into topic 2268 and resumed the previously failing topic 3731; checked gateway health, Codex sidecar binding JSON, and Lossless assembly logs from /tmp/openclaw/openclaw-2026-05-12.log.
  • Evidence after fix (screenshot, recording, terminal capture, console output, redacted runtime log, linked artifact, or copied live output):
$ curl -fsS http://127.0.0.1:18789/health
{"ok":true,"status":"live"}

$ jq sidecar summary for topic 3731
{
  "threadId": "019e1e9e-a00f-7982-b2ba-5afdff70c40b",
  "createdAt": "2026-05-12T23:56:12.803Z",
  "updatedAt": "2026-05-12T23:56:12.803Z",
  "contextEngine": {
    "schemaVersion": 1,
    "engineId": "lossless-claw",
    "policyFingerprint": "{redacted stable lossless-claw policy fingerprint with contextTokenBudget=400000}"
  }
}

2026-05-12T16:55:36.094-07:00 codex app-server context-engine binding changed; starting a new thread
2026-05-12T16:55:35.853-07:00 [lcm] assemble: done conversation=4754 session=4b009e50-4b7c-425a-b116-748e331b18cf sessionKey=agent:main:telegram:group:{redacted}:topic:3731 contextItems=159 hasSummaryItems=true inputMessages=325 outputMessages=154 tokenBudget=400000 estimatedTokens=61891 stubbed=0 tokensSaved=0 duration=33ms

topic 2268: hasSummaryItems=true contextItems=246 summaryCount=8 rawMessageCount=238 estimatedTokens=62326 freshTail=128
topic 3731: hasSummaryItems=true contextItems=161 summaryCount=12 rawMessageCount=149 estimatedTokens=61900 freshTail=128
  • Observed result after fix: Both tested Lossless-managed sessions created context-engine-stamped Codex sidecars on fresh backend thread IDs. The previously failing topic 3731 assembled Lossless summaries plus fresh tail and did not repeat the MiniMax fallback/context-overflow behavior during the live smoke.
  • What was not tested: I did not run a full production CI suite locally. I did not test every non-Telegram integration. I did not force a second artificial overflow after the fresh retry path.
  • Before evidence (optional but encouraged): Before this patch, topic 3731 had a legacy sidecar created at 07:52:27 with thread 019e1cac-cdb8-7801-acf8-efc11c77d024; the failing turn updated that binding, implying resume, then Codex failed at first prompt with context-window overflow and persisted fallback to minimax/MiniMax-M2.7.

Root Cause (if applicable)

  • Root cause: Codex app-server bindings did not distinguish plain Codex persistence from context-engine-managed persistence. Lossless could provide the canonical projected context while Codex simultaneously resumed hidden native thread history.
  • Missing detection / guardrail: Existing sidecar compatibility checks did not include context-engine policy metadata, and early Codex context-window errors were treated like ordinary model failures.
  • Contributing context (if known): The failing session had Lossless summaries well under the 400k budget, but Codex still failed at first prompt after resuming a stale native thread.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: extensions/codex/src/app-server/run-attempt.context-engine.test.ts, extensions/codex/src/app-server/run-attempt.test.ts, extensions/codex/src/app-server/session-binding.test.ts.
  • Scenario the test should lock in: Lossless/context-engine-managed turns rotate incompatible legacy Codex sidecars, preserve compatible bindings, and retry once fresh on early context-window overflow before fallback persistence.
  • Why this is the smallest reliable guardrail: The bug lives at Codex sidecar lifecycle and run-attempt error handling; focused app-server tests cover that boundary without needing a real provider call.
  • Existing test that already covers this (if any): Existing Codex app-server tests covered plain resume behavior, but not context-engine compatibility metadata.
  • If no new test is added, why not: N/A.

User-visible / Behavior Changes

Lossless-managed Codex sessions may start a fresh native Codex backend thread when an existing sidecar lacks compatible context-engine metadata. Plain non-Lossless Codex sessions keep normal backend resume behavior.

Diagram (if applicable)

Before:
Lossless projection -> Codex resumes stale native thread -> duplicated/hidden context -> context-window failure -> MiniMax fallback may persist

After:
Lossless projection -> sidecar compatibility check -> rotate legacy/incompatible thread -> fresh Codex backend thread -> context-engine-stamped binding

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No
  • If any Yes, explain risk + mitigation: N/A

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: local OpenClaw gateway, Node 22.17.0
  • Model/provider: Codex app-server harness with openai/gpt-5.5
  • Integration/channel (if any): Telegram group forum topics 2268 and 3731
  • Relevant config (redacted): Lossless Claw enabled with context token budget 400000; Codex harness selected for openai/gpt-5.5.

Steps

  1. Run the patched branch locally and restart the gateway.
  2. Send a message into a Lossless-managed session with an existing legacy Codex app-server sidecar.
  3. Inspect sidecar JSON and gateway logs for context-engine metadata and thread rotation.
  4. Resume the previously failing topic 3731 and inspect Lossless assembly/fallback behavior.

Expected

  • Legacy/unmarked context-engine-managed sidecar is not blindly resumed.
  • A fresh Codex backend thread is started and stamped with context-engine metadata.
  • Lossless summaries remain in the assembled context.
  • The session does not persist a MiniMax fallback for the stale-thread context-window failure path.

Actual

  • Topic 3731 sidecar changed to a new thread ID 019e1e9e-a00f-7982-b2ba-5afdff70c40b with contextEngine.engineId=lossless-claw.
  • Gateway logs showed: codex app-server context-engine binding changed; starting a new thread.
  • Lossless assembled hasSummaryItems=true, summaryCount=12, and approximately 61.9k estimated context tokens for topic 3731.
  • No repeat MiniMax fallback was observed in the live smoke after the restart.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: Local gateway restart on the patched branch; topic 2268 and topic 3731 sidecar rotation/stamping; Lossless summaries present in both assembled contexts; topic 3731 no longer immediately repeated the MiniMax fallback path.
  • Edge cases checked: Plain non-Lossless resume behavior is covered by existing/updated tests; compatible context-engine binding reuse is covered by tests.
  • What you did not verify: Full CI, every provider, every channel, and an artificially forced second overflow after fresh retry.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No

@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

Summary
The PR adds Codex app-server context-engine sidecar metadata, rotates legacy or incompatible context-engine bindings to fresh native Codex threads, retries one early context-window overflow on a fresh thread, and adds focused tests plus a changelog entry.

Reproducibility: yes. source-reproducible. Current main resumes Codex sidecar bindings without context-engine compatibility metadata, and the PR body provides live before/after logs for a stale Lossless-managed Codex sidecar; I did not rerun that Telegram gateway setup locally.

Real behavior proof
Sufficient (logs): The PR body includes redacted live gateway output, sidecar JSON, and Lossless assembly logs showing after-fix fresh context-engine-stamped Codex threads and no repeated fallback in the tested topic.

Next step before merge
A narrow PR-introduced retry cleanup bug is present in run-attempt.ts and can be repaired without product or ownership judgment.

Security
Cleared: The diff changes Codex app-server sidecar metadata, retry logic, tests, and changelog only; it adds no dependencies, workflows, permissions, downloads, or secret-handling changes.

Review findings

  • [P2] Catch fresh-thread retry startup failures — extensions/codex/src/app-server/run-attempt.ts:1499
Review details

Best possible solution:

Keep the sidecar compatibility policy in the Codex app-server, fix retry-start error handling, then land after focused Codex tests and maintainer review.

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

Yes, source-reproducible. Current main resumes Codex sidecar bindings without context-engine compatibility metadata, and the PR body provides live before/after logs for a stale Lossless-managed Codex sidecar; I did not rerun that Telegram gateway setup locally.

Is this the best way to solve the issue?

No, not yet. Stamping and rotating context-engine sidecars is the right maintainable boundary, but the fresh-thread retry must catch restart failures and route them through the same cleanup and failure-reporting path before merge.

Full review comments:

  • [P2] Catch fresh-thread retry startup failures — extensions/codex/src/app-server/run-attempt.ts:1499
    If the context-window retry clears the sidecar and startOrResumeThread fails while starting the fresh thread, this await throws before the existing turn === undefined failure block runs. That skips turn_start_failed, agent-end hooks, native hook unregister, request/notification cleanup, and trajectory flushing for this failure path; catch the restart failure and assign it to turnStartError so the normal cleanup path runs.
    Confidence: 0.87

Overall correctness: patch is incorrect
Overall confidence: 0.84

Acceptance criteria:

  • pnpm test extensions/codex/src/app-server/run-attempt.context-engine.test.ts extensions/codex/src/app-server/run-attempt.test.ts extensions/codex/src/app-server/session-binding.test.ts
  • pnpm check:changed

What I checked:

  • Protected live PR state: The live PR metadata shows the PR is open, not merged, head SHA 029253f, and labeled maintainer, extensions: codex, and proof: sufficient. (029253fd6e6c)
  • Current main binding has no context-engine stamp: Current main persists Codex app-server thread binding fields for thread, model, dynamic tools, and plugin policy, but no context-engine compatibility metadata, matching the stale hidden native-history root cause. (extensions/codex/src/app-server/session-binding.ts:31, 58bfefbad3e3)
  • Current main resumes sidecars without a context-engine check: Current main's startOrResumeThread reads an existing sidecar and proceeds through plugin and dynamic-tool compatibility before thread/resume; there is no context-engine compatibility branch before resume. (extensions/codex/src/app-server/thread-lifecycle.ts:70, 58bfefbad3e3)
  • PR adds context-engine sidecar metadata: The PR head adds contextEngine to the binding contract, reads it from sidecar JSON, and writes it back with the persisted binding payload. (extensions/codex/src/app-server/session-binding.ts:46, 029253fd6e6c)
  • PR rotates incompatible context-engine bindings: The PR head builds a context-engine policy fingerprint and clears the existing sidecar when prior metadata is absent, inactive, or incompatible with the active context-engine policy. (extensions/codex/src/app-server/thread-lifecycle.ts:87, 029253fd6e6c)
  • PR retry path has an unhandled restart failure gap: The retry branch clears the sidecar and awaits restartContextEngineCodexThread() before entering the existing turn === undefined failure block, so a failure while starting the fresh thread bypasses the normal failure hooks and cleanup. (extensions/codex/src/app-server/run-attempt.ts:1499, 029253fd6e6c)

Likely related people:

  • @jalehman: GitHub commit search shows prior merged Codex/context-engine lifecycle work by @jalehman, including commit 51186d2, and this PR continues that same Codex app-server context-engine lifecycle boundary. (role: context-engine feature owner and current PR author with prior merged history; confidence: high; commits: 51186d272543, 751d5b7849ca, 029253fd6e6c; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/thread-lifecycle.ts, src/context-engine/types.ts)
  • Peter Steinberger: Local blame on current main attributes the sampled Codex app-server binding, thread lifecycle, and turn-start handling lines to commit d00e9eb. (role: recent area contributor; confidence: medium; commits: d00e9eba65dd; files: extensions/codex/src/app-server/thread-lifecycle.ts, extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/session-binding.ts)

Remaining risk / open question:

  • The fresh-thread retry failure path can skip existing turn-start cleanup and failure reporting if startOrResumeThread throws during the retry.
  • No local tests or live Telegram gateway run were performed in this read-only review; the verdict relies on source inspection and the PR body's redacted live logs.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 58bfefbad3e3.

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 13, 2026
@jalehman jalehman self-assigned this May 13, 2026
@jalehman
jalehman force-pushed the buce/codex-context-engine-sidecar-policy branch from 7d445b0 to 029253f Compare May 13, 2026 18:30
@jalehman
jalehman merged commit b55d9fa into openclaw:main May 13, 2026
94 of 105 checks passed
steipete pushed a commit that referenced this pull request May 13, 2026
* fix(codex): rotate incompatible context-engine threads

* fix(codex): tighten context-engine sidecar policy

* fix: type context-engine binding policy config

---------

Co-authored-by: Josh Lehman <[email protected]>
(cherry picked from commit b55d9fa)
l3ocifer pushed a commit to l3ocifer/frack-openclaw that referenced this pull request May 13, 2026
* fix(codex): rotate incompatible context-engine threads

* fix(codex): tighten context-engine sidecar policy

* fix: type context-engine binding policy config

---------

Co-authored-by: Josh Lehman <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* fix(codex): rotate incompatible context-engine threads

* fix(codex): tighten context-engine sidecar policy

* fix: type context-engine binding policy config

---------

Co-authored-by: Josh Lehman <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
* fix(codex): rotate incompatible context-engine threads

* fix(codex): tighten context-engine sidecar policy

* fix: type context-engine binding policy config

---------

Co-authored-by: Josh Lehman <[email protected]>
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
* fix(codex): rotate incompatible context-engine threads

* fix(codex): tighten context-engine sidecar policy

* fix: type context-engine binding policy config

---------

Co-authored-by: Josh Lehman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: codex maintainer Maintainer-authored PR proof: sufficient ClawSweeper judged the real behavior proof convincing. size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant