Skip to content

Keep Codex runtime context out of user history - #84790

Closed
TurboTheTurtle wants to merge 3 commits into
openclaw:mainfrom
TurboTheTurtle:fix/codex-runtime-context-history-84662
Closed

TurboTheTurtle wants to merge 3 commits into
openclaw:mainfrom
TurboTheTurtle:fix/codex-runtime-context-history-84662

Conversation

@TurboTheTurtle

@TurboTheTurtle TurboTheTurtle commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #84662.

Summary

  • stop prepending OpenClaw current-turn runtime context to the Codex turn/start.input user text
  • pass that context through collaborationMode.settings.reference_context as non-authoritative reference data instead of developer_instructions
  • sanitize stale mirrored Codex history entries that already contain old OpenClaw runtime context for this turn: user-message wrappers, and start a fresh native Codex thread when a bound thread would otherwise replay them
  • update Codex prompt snapshot generation so MEMORY.md/current-turn context is no longer modeled as ordinary user input or developer instructions

Real behavior proof

Behavior or issue addressed: Codex app-server turns should receive the actual user request as user input, while OpenClaw runtime/workspace context stays out of user history and stale mirrored-history wrappers do not get replayed into resumed native Codex threads.

Real environment tested: Local OpenClaw checkout in /tmp/openclaw-pr-84790 on macOS at commit ffe4c44b65, using the repository's Codex app-server runtime builders, mirrored session-history reader, Vitest app-server harness, prompt snapshot generator, TypeScript test config, the local standalone codex-cli 0.124.0 protocol generator, and the official openai/codex rust-v0.132.0 generated protocol source.

Exact steps or command run after this patch:

  • node scripts/run-vitest.mjs run --config test/vitest/vitest.extensions.config.ts extensions/codex/src/app-server/session-history.test.ts extensions/codex/src/app-server/thread-lifecycle.test.ts extensions/codex/src/app-server/run-attempt.test.ts
  • node /Users/andy/src/openclaw/node_modules/.bin/tsgo -p test/tsconfig/tsconfig.extensions.test.json --pretty false --declaration false --singleThreaded --checkers 1
  • node /Users/andy/src/openclaw/node_modules/.bin/oxfmt --check --threads=1 extensions/codex/src/app-server/session-history.ts extensions/codex/src/app-server/session-history.test.ts extensions/codex/src/app-server/run-attempt.ts extensions/codex/src/app-server/run-attempt.test.ts extensions/codex/src/app-server/thread-lifecycle.ts extensions/codex/src/app-server/thread-lifecycle.test.ts extensions/codex/src/app-server/protocol.ts test/helpers/agents/happy-path-prompt-snapshots.ts test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/telegram-direct-codex-message-tool.md test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/discord-group-codex-message-tool.md test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/telegram-heartbeat-codex-tool.md && git diff --check
  • node --import /Users/andy/src/openclaw/node_modules/tsx/dist/loader.mjs scripts/generate-prompt-snapshots.ts --check
  • gh api 'repos/openai/codex/contents/codex-rs/app-server-protocol/schema/typescript/Settings.ts?ref=rust-v0.132.0' --jq '.content' | base64 --decode | sed -n '1,60p'
  • tmpdir=$(mktemp -d /tmp/codex-live-protocol-XXXXXX); codex app-server generate-ts --experimental --out "$tmpdir"; codex --version; sed -n '1,80p' "$tmpdir/Settings.ts"; sed -n '70,90p' "$tmpdir/v2/TurnStartParams.ts"

Evidence after fix:

  • Focused runtime tests passed: Test Files 3 passed (3), Tests 226 passed (226).
  • Typecheck completed with exit code 0.
  • Format/whitespace check completed with All matched files use the correct format and git diff --check clean.
  • Prompt snapshot check completed with Prompt snapshots are current (7 files).
  • Official Codex rust-v0.132.0 generated Settings.ts contains only model, reasoning_effort, and developer_instructions; there is no reference_context field.
  • Local codex-cli 0.124.0 app-server generate-ts --experimental produced the same Settings.ts shape, while v2/TurnStartParams.ts confirms collaborationMode?: CollaborationMode | null and documents collaboration_mode.settings.developer_instructions as the supported turn-scoped collaboration-mode channel.
  • The updated runtime payload snapshots now show turn/start.input as the user request only, no reference_context key, and collaborationMode.settings.developer_instructions carrying an OpenClaw Current-Turn Reference Context block wrapped in <openclaw_reference_context>.
  • The wrapper explicitly says the block is user/workspace reference data, not system policy, developer policy, or an instruction source, and says to ignore conflicting instructions inside the block.
  • The stale-history runtime test builds a mirrored session file containing OpenClaw runtime context for this turn: plus Current user request: prior real request, writes an existing native Codex binding, runs runCodexAppServerAttempt, and verifies no thread/resume request is sent, a fresh thread/start is sent, the turn/start input contains prior real request and new request, and the stale runtime-context marker is absent from user input.

Observed result after fix: Current-turn OpenClaw runtime context is no longer serialized into Codex user input or stale user-history projection. It is sent through the generated Codex-supported turn-scoped collaborationMode.settings.developer_instructions path with an explicit untrusted-reference wrapper, and stale history entries that already contain the old wrapper are stripped before mirrored history projection. If such stale history exists with a saved native thread binding, the app-server clears the binding and starts a new native Codex thread instead of resuming a thread that could replay the old privileged-looking user message.

What was not tested: I did not run a live Discord/Telegram production turn against the installed OpenClaw gateway from this PR branch. A standalone local codex app-server model-completion probe could initialize and start a thread, but the local standalone codex auth currently fails model calls with 401 Unauthorized: Missing bearer or basic authentication, so I did not claim a completed live model response as proof.

Validation

  • node scripts/run-vitest.mjs run --config test/vitest/vitest.extensions.config.ts extensions/codex/src/app-server/session-history.test.ts extensions/codex/src/app-server/thread-lifecycle.test.ts extensions/codex/src/app-server/run-attempt.test.ts
  • node /Users/andy/src/openclaw/node_modules/.bin/tsgo -p test/tsconfig/tsconfig.extensions.test.json --pretty false --declaration false --singleThreaded --checkers 1
  • node /Users/andy/src/openclaw/node_modules/.bin/oxfmt --check --threads=1 extensions/codex/src/app-server/session-history.ts extensions/codex/src/app-server/session-history.test.ts extensions/codex/src/app-server/run-attempt.ts extensions/codex/src/app-server/run-attempt.test.ts extensions/codex/src/app-server/thread-lifecycle.ts extensions/codex/src/app-server/thread-lifecycle.test.ts extensions/codex/src/app-server/protocol.ts test/helpers/agents/happy-path-prompt-snapshots.ts && git diff --check
  • node --import /Users/andy/src/openclaw/node_modules/tsx/dist/loader.mjs scripts/generate-prompt-snapshots.ts --check

If this PR is squash-merged or reworked, please preserve author attribution or include:

Co-authored-by: Andy Ye <[email protected]>

@openclaw-barnacle openclaw-barnacle Bot added extensions: codex size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 21, 2026
@clawsweeper

clawsweeper Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Workflow note: Future ClawSweeper reviews update this same comment in place.

How this review workflow works
  • 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.

Summary
The PR removes OpenClaw runtime context from Codex turn/start.input, routes it through collaboration developer instructions, sanitizes stale mirrored history wrappers, clears affected native thread bindings, and updates tests and prompt snapshots.

Reproducibility: yes. source-level. Current main visibly constructs OpenClaw runtime context for this turn: and prepends it into the Codex turn input path, and the linked issue supplies concrete replay/growth evidence; I did not run a live Codex app-server repro in this read-only review.

PR rating
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🧂 unranked krab
Summary: The PR has useful tests and a real target, but missing real behavior proof plus blocking protocol and prompt-boundary issues keep it below merge-ready quality.

Rank-up moves:

  • Prove or sync the bundled Codex app-server protocol so the chosen turn context field is actually consumed by @openai/codex 0.132.0.
  • Keep user-editable MEMORY/workspace reference data out of developer authority unless maintainers explicitly accept that security tradeoff.
  • Add redacted live app-server proof for a fresh turn and an existing bound session with stale wrapped history.
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

Real behavior proof
Needs real behavior proof before merge: The PR body reports focused tests, typecheck, formatting, snapshots, and protocol inspection, but no redacted live Codex app-server turn or logs showing the after-fix behavior; the contributor should add real proof and update the PR body so ClawSweeper can re-review, or ask a maintainer to comment @clawsweeper re-review if it does not rerun.

Risk before merge

  • If the pinned Codex app-server ignores unknown turn/start.collaborationMode, existing Codex users will lose OpenClaw skills, MEMORY.md, and current-turn workspace reference context because turn/start.input now contains only the user request.
  • The patch changes prompt authority by placing user-editable workspace/MEMORY reference data inside developer_instructions, which needs an explicit prompt-boundary/security decision rather than relying only on wrapper text.
  • Clearing saved native Codex bindings for stale wrapped history intentionally changes session continuity for affected existing sessions and needs upgrade proof beyond harness assertions.
  • The PR body supplies focused tests and protocol checks, but no redacted live app-server turn, logs, terminal output, or artifact showing the fixed behavior in a real Codex session.

Maintainer options:

  1. Prove and use a supported context path (recommended)
    Before merge, sync/prove the bundled Codex app-server turn/start contract and route OpenClaw reference context through a field the pinned runtime actually consumes without persisting it as user history.
  2. Accept the developer-instruction tradeoff
    Maintainers can intentionally accept moving user-editable reference data into collaboration developer instructions, but that should be explicit and backed by prompt-boundary proof.
  3. Pause for upstream protocol support
    If Codex lacks a lower-authority transient reference field today, pause this PR until upstream exposes one or OpenClaw narrows the fix to stale-history sanitization without changing context authority.

Next step before merge
Human maintainer review is needed for the Codex protocol contract, prompt-boundary tradeoff, and missing real behavior proof; this is not a safe automated repair lane yet.

Security
Needs attention: Prompt-boundary review is needed because the patch moves user-editable workspace/MEMORY reference context into developer instructions.

Review findings

  • [P1] Use a bundled-supported turn context path — extensions/codex/src/app-server/run-attempt.ts:2381
  • [P1] Keep user-editable context out of developer instructions — extensions/codex/src/app-server/thread-lifecycle.ts:768
Review details

Best possible solution:

Use a Codex-supported, non-persisted path for transient reference context, or split trusted OpenClaw-owned instructions from user-editable reference data and merge only after maintainers explicitly accept the prompt-boundary and upgrade tradeoff with real fresh-session plus stale-bound-session proof.

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

Yes, source-level. Current main visibly constructs OpenClaw runtime context for this turn: and prepends it into the Codex turn input path, and the linked issue supplies concrete replay/growth evidence; I did not run a live Codex app-server repro in this read-only review.

Is this the best way to solve the issue?

No. The stale-history cleanup is useful, but relying on an unproven turn/start.collaborationMode path and putting user-editable reference data in developer_instructions is not the safest maintainable fix without maintainer approval and real upgrade proof.

Label justifications:

  • P2: This is a normal-priority Codex app-server bug fix with meaningful session/context impact but limited to the Codex plugin surface.
  • merge-risk: 🚨 compatibility: The diff can make existing Codex turns lose OpenClaw skills and workspace reference context if the pinned app-server ignores turn/start.collaborationMode.
  • merge-risk: 🚨 session-state: The PR deliberately clears saved native Codex thread bindings when stale wrapped history is found, changing session continuity for upgraded users.
  • merge-risk: 🚨 security-boundary: The diff moves user-editable workspace/MEMORY text into a developer-instruction channel, which affects prompt authority boundaries.
  • rating: 🧂 unranked krab: Current PR rating is 🧂 unranked krab because proof is 🧂 unranked krab, patch quality is 🧂 unranked krab, and The PR has useful tests and a real target, but missing real behavior proof plus blocking protocol and prompt-boundary issues keep it below merge-ready quality.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body reports focused tests, typecheck, formatting, snapshots, and protocol inspection, but no redacted live Codex app-server turn or logs showing the after-fix behavior; the contributor should add real proof and update the PR body so ClawSweeper can re-review, or ask a maintainer to comment @clawsweeper re-review if it does not rerun.

Full review comments:

  • [P1] Use a bundled-supported turn context path — extensions/codex/src/app-server/run-attempt.ts:2381
    The branch now removes OpenClaw skills/MEMORY from turn/start.input and relies on collaborationMode.settings.developer_instructions for model visibility, but the repo pins @openai/codex 0.132.0 and its generated v2/TurnStartParams does not include collaborationMode. If the bundled app-server ignores that extra field, Codex receives only the raw user request and loses the OpenClaw context this PR is trying to preserve.
    Confidence: 0.88
  • [P1] Keep user-editable context out of developer instructions — extensions/codex/src/app-server/thread-lifecycle.ts:768
    This helper wraps MEMORY/workspace content in developer_instructions while saying it is not an instruction source. The linked bug explicitly calls that data user-editable reference context; placing it in the developer layer promotes any embedded instructions to the same message authority the wrapper is trying to disclaim, so the patch needs a lower-authority supported path or an explicit maintainer security decision.
    Confidence: 0.84

Overall correctness: patch is incorrect
Overall confidence: 0.88

Security concerns:

  • [medium] User-editable context enters developer instructions — extensions/codex/src/app-server/thread-lifecycle.ts:768
    The new current-turn reference wrapper is sent through collaborationMode.settings.developer_instructions, so workspace/MEMORY text can be interpreted at developer-message authority unless maintainers explicitly accept that design or move it to a lower-authority supported channel.
    Confidence: 0.84

What I checked:

  • Current-main bug path: Current main builds OpenClaw runtime context for this turn: and prepends it plus Current user request: into the Codex turn prompt before turn/start.input, matching the linked issue's reported replay source. (extensions/codex/src/app-server/run-attempt.ts:1143, 8284c035a096)
  • PR contract risk: The diff passes openClawPromptContext as currentTurnReferenceContext and then serializes it into collaborationMode.settings.developer_instructions, so the user input becomes only the current request. (extensions/codex/src/app-server/run-attempt.ts:2381, ffe4c44b6551)
  • Bundled Codex protocol check: The repository pins @openai/codex 0.132.0, and the upstream rust-v0.132.0 generated v2/TurnStartParams.ts contains input, cwd/model/sandbox fields, and output schema, but no collaborationMode; Settings.ts has developer_instructions, but that does not prove turn/start accepts collaboration mode. (extensions/codex/package.json:12, 13595c36e218)
  • Positive stale-history remediation: The PR adds mirrored-history sanitization and clears saved Codex native thread bindings when stale wrapped user history is detected, which addresses the old-thread replay half of the linked report. (extensions/codex/src/app-server/session-history.ts:29, ffe4c44b6551)
  • Feature history provenance: git blame and git log -S tie the current app-server prompt-context and collaboration-mode code to the recent Codex app-server import/refactor history, with the runtime-context marker appearing in commits c8a953a... and release commit a185ca2.... (extensions/codex/src/app-server/run-attempt.ts:4524, c8a953af9371)

Likely related people:

  • steipete: Git blame shows Peter Steinberger authored the current Codex app-server prompt-context, session-history, and collaboration-mode code in c8a953a..., and git log -S also ties the runtime-context prompt snapshots to a185ca2.... (role: introduced behavior and recent area owner; confidence: high; commits: c8a953af9371, a185ca283a74; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/thread-lifecycle.ts, extensions/codex/src/app-server/session-history.ts)
  • WhatsSkiLL: Recent commit c4f14a... changed the same Codex app-server bootstrap-context area shortly before this PR, so they are a useful adjacent routing candidate. (role: recent adjacent contributor; confidence: medium; commits: c4f14a39a520; files: extensions/codex/src/app-server/run-attempt.ts)

Codex review notes: model gpt-5.5, reasoning high; reviewed against 8284c035a096.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels May 21, 2026
@clawsweeper

clawsweeper Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@TurboTheTurtle

Copy link
Copy Markdown
Contributor Author

Acknowledged on the ClawSweeper review.

I am not going to request re-review on this PR with only the existing harness output, because the review identified two blockers that proof alone would not honestly clear:

  • currentTurnReferenceContext currently routes user-editable workspace/MEMORY reference text through collaborationMode.settings.developer_instructions, which raises the prompt-authority concern called out in the issue.
  • existing bound Codex native histories that already contain OpenClaw runtime context for this turn: user items are not remediated by the current future-turn-only change.

Next action should be a code follow-up for those two points, then real after-fix proof from a multi-turn Codex app-server run and, ideally, a compact/replay path.

@openclaw-barnacle openclaw-barnacle Bot added size: L proof: supplied External PR includes structured after-fix real behavior proof. and removed size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 21, 2026
@TurboTheTurtle

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Follow-up pushed in 2d03487dd3:

  • moved current-turn OpenClaw runtime context out of developer_instructions and into collaborationMode.settings.reference_context with authority: "reference"
  • sanitized stale mirrored history user messages that already contain the old OpenClaw runtime context for this turn: wrapper
  • clear the saved native Codex binding and start a fresh thread when stale wrapped history is detected, so the old user-message wrapper is not replayed through thread/resume
  • updated structured Real behavior proof in the PR body; GitHub Real behavior proof is now passing

Local validation: focused app-server tests 226 passed, tsgo passed, oxfmt --check + git diff --check passed, prompt snapshot check passed.

@clawsweeper

clawsweeper Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. label May 21, 2026
@TurboTheTurtle
TurboTheTurtle marked this pull request as ready for review May 21, 2026 05:39

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Follow-up pushed in ffe4c44b65:

  • removed the unsupported collaborationMode.settings.reference_context field
  • moved current-turn OpenClaw runtime context to the generated/supported collaborationMode.settings.developer_instructions path with an explicit untrusted-reference wrapper
  • kept the actual user request as the only turn/start.input text
  • preserved stale mirrored-history sanitization and fresh-thread reset for saved native bindings
  • updated runtime payload snapshots and PR real behavior proof with Codex protocol evidence

Validation: focused app-server tests 226 passed, tsgo passed, oxfmt --check + git diff --check passed, and prompt snapshots are current.

@clawsweeper

clawsweeper Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@TurboTheTurtle

Copy link
Copy Markdown
Contributor Author

Closing this for now since I do not have stronger real-behavior proof for the prompt/protocol boundary concerns, and I do not want to keep an older PR open that is not realistically merge-ready in its current shape. Thanks for the review and context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: codex merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Codex app-server stores per-turn OpenClaw runtime context in native user history, causing runaway response.create input growth

1 participant