Skip to content

fix(agents): serialize native compaction with session writers - #135441

Merged
obviyus merged 7 commits into
openclaw:mainfrom
jjjhenriksen:fix/codex-compaction-active-writer
Sep 2, 2026
Merged

obviyus merged 7 commits into
openclaw:mainfrom
jjjhenriksen:fix/codex-compaction-active-writer

Conversation

@jjjhenriksen

@jjjhenriksen jjjhenriksen commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Primary native compaction could start while the same OpenClaw session still owned an active writer. Codex treats manual compaction as a turn-like task and replaces an active task, so the overlap could interrupt the user turn and strand compaction before its terminal item.

Why This Change Was Made

The host compaction coordinator now admits primary native harness and native CLI control operations through the existing per-session and global lanes. It revalidates the Codex session owner after admission. Direct compaction for callers that already own a lane remains unchanged.

The contributor's client-router wait, extra timeout, and duplicate rejection test were removed. A route can end before OpenClaw finishes its transcript checkpoint, so route lifetime was not the writer ownership boundary.

User Impact

Codex-backed turns finish and persist before queued native compaction starts. The change does not add configuration, retries, timeouts, locks, or compaction policy.

Evidence

  • Current-main red at 0fafd993b4f93af51037dd9e29f69fcb94479a6e: the compaction request preceded writer completion, Codex interrupted the writer, and the compaction item did not complete.
  • Exact-head green at 64c0b970febf7dadb4814e10d24632e6e5d6ee92: writer command completed, writer turn completed, compaction request started, and the context-compaction item completed in that order.
  • Transcript proof: both unique markers remained after compaction, and a separate follow-up turn recalled them.
  • Regression proof: the native CLI ownership test fails on pre-repair head 250755ad0768f80197b2d87d1690c6bc1703fbcb because compaction starts after the writer claim changes; it passes on this head without starting native I/O.
  • Focused tests: all 173 compaction owner tests passed.
  • Focused lint, format, and git diff --check passed.

Real runtime order

Baseline Exact head
writer start → command start → compaction request → writer interrupted → compaction item start writer start → command start → command complete → writer complete → compaction request → compaction item start → compaction item complete

Redacted event trace

baseline 0fafd993b4f93af51037dd9e29f69fcb94479a6e
01 writer_turn_started
02 writer_command_started
03 compaction_trigger
04 compaction_request firstTurnCompleted=false
05 writer_turn_completed status=interrupted
06 compaction_item_started
-- compaction_item_completed missing

exact head 64c0b970febf7dadb4814e10d24632e6e5d6ee92
01 writer_turn_started
02 writer_command_started
03 compaction_trigger
04 writer_command_completed
05 writer_turn_completed status=completed
06 compaction_request firstTurnCompleted=true
07 compaction_item_started
08 compaction_item_completed

Exact-head assertions: writer terminal ok; compaction ok; completion marker present; input and completion markers persisted; separate recall turn terminal ok; recall returned WRITER_RECALL_OK.

@clawsweeper

clawsweeper Bot commented Sep 1, 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 extensions: codex size: M triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Sep 1, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. 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. labels Sep 1, 2026
@clawsweeper

clawsweeper Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 2, 2026, 10:56 AM ET / 14:56 UTC.

ClawSweeper review

What this changes

The PR serializes native /compact work with session writers and rejects compaction whose writer claim changed while it waited.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open. The PR provides a focused repair for the active-writer compaction race, with no introduced correctness or security defect found; current-main source could not be independently loaded from the unavailable promisor remote.

Priority: P1
Reviewed head: 64c0b970febf7dadb4814e10d24632e6e5d6ee92

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch has targeted owner-boundary tests and real runtime evidence for a focused session-concurrency repair.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR supplies redacted real-runtime before/after traces: the baseline writer is interrupted before compaction completes, while exact head records writer and compaction completion, persists markers, and completes a separate recall turn.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR supplies redacted real-runtime before/after traces: the baseline writer is interrupted before compaction completes, while exact head records writer and compaction completion, persists markers, and completes a separate recall turn.
Evidence reviewed 6 items Shared lane and claim boundary: The introduced helper enters the session lane before the global lane, rereads the durable session entry immediately before native work, and rejects changed session, lifecycle, or writer-claim identity.
Both primary native paths use the boundary: Native CLI execution receives the lane wrapper, and primary native-harness compaction is executed through the same wrapper.
Existing run ordering matches: Normal embedded runs already use session admission followed by global admission; this PR does not introduce a lock-order inversion.
Findings None None.
Security None None.

How this fits together

Native compaction asks the Codex runtime to persist a shorter conversation transcript. It shares the session’s execution lanes with ordinary agent turns so transcript writes and compaction cannot overlap.

flowchart LR
  Command[Manual compact command] --> Target[Durable session target]
  Writer[Active agent writer] --> SessionLane[Session lane]
  Target --> SessionLane
  SessionLane --> GlobalLane[Global execution lane]
  GlobalLane --> ClaimCheck[Writer claim check]
  ClaimCheck --> Native[Native compaction]
  Native --> Transcript[Persisted compacted transcript]
Loading

Before merge

  • Resolve merge risk (P1) - This changes session scheduling and durable writer-claim validation; an unobserved edge case could stall compaction or reject it when a session lifecycle changes.
  • Resolve merge risk (P1) - Current-main source equivalence was not independently inspectable because required promisor blobs could not be fetched.
Agent review details

Security

None.

PR surface

Source +48, Tests +150. Total +198 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 104 56 +48
Tests 1 152 2 +150
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 256 58 +198

Review metrics

Metric Value Why it matters
Production vs test delta production +104/-56 (net +48); tests +152/-2 (net +150) The production increase centralizes one two-lane and claim-revalidation boundary shared by native CLI and native-harness compaction.

Merge-risk options

Maintainer options:

  1. Merge with the tested scheduling boundary (recommended)
    Accept the residual concurrency risk because both native paths use the established lane order and targeted claim-replacement coverage plus supplied runtime evidence exercise the repaired boundary.

Technical review

Best possible solution:

Retain the shared session-then-global lane wrapper with its immediate durable claim revalidation for both native execution paths.

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

No current-main reproduction was run locally; the exact-head source and supplied before/after real-runtime trace provide a clear reproduction and recovery scenario.

Is this the best way to solve the issue?

Yes. It reuses the normal embedded-run lane order and verifies the durable writer claim immediately before either native execution path can modify the transcript.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: The reported race can interrupt or leave native compaction incomplete during an active agent turn.
  • merge-risk: 🚨 session-state: The PR changes ordering and writer-claim validation around persisted session transcripts.
  • merge-risk: 🚨 availability: A defect in shared lane scheduling could stall a manual compaction or its competing agent turn.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR supplies redacted real-runtime before/after traces: the baseline writer is interrupted before compaction completes, while exact head records writer and compaction completion, persists markers, and completes a separate recall turn.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR supplies redacted real-runtime before/after traces: the baseline writer is interrupted before compaction completes, while exact head records writer and compaction completion, persists markers, and completes a separate recall turn.

Evidence

What I checked:

  • Shared lane and claim boundary: The introduced helper enters the session lane before the global lane, rereads the durable session entry immediately before native work, and rejects changed session, lifecycle, or writer-claim identity. (src/agents/embedded-agent-runner/compact.queued.ts:298, 64c0b970febf)
  • Both primary native paths use the boundary: Native CLI execution receives the lane wrapper, and primary native-harness compaction is executed through the same wrapper. (src/agents/embedded-agent-runner/compact.queued.ts:377, 64c0b970febf)
  • Existing run ordering matches: Normal embedded runs already use session admission followed by global admission; this PR does not introduce a lock-order inversion. (src/agents/embedded-agent-runner/run/lane-controller.ts:354, 64c0b970febf)
  • Regression coverage: Added cases hold a queued /compact call behind a session lane, verify no native harness call before release, and verify claim replacement prevents the call; a parallel native-CLI case verifies no CLI run occurs after replacement. (src/agents/embedded-agent-runner/compact.hooks.test.ts:3577, 64c0b970febf)
  • Codex compaction contract: Directly inspected Codex source shows manual compaction starts a standalone turn, emits one compaction item, persists replacement history, then completes that item. The local Codex checkout uses a cache remote, so repository ownership could not be independently verified. (codex-rs/core/src/compact.rs:148, 5adb68a49933ae446bf11935662c83dba55a0804)
  • Current-main limitation: The locally recorded current main commit exists, but loading historical/main source blobs attempted a promisor fetch and failed because github.com could not be resolved; main equivalence was therefore not asserted. (4adab7342dd8)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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 (9 earlier review cycles; latest 8 shown)
  • reviewed 2026-09-01T19:02:24.749Z sha 01da97eeebdf828455019f90721871417f3e24d3 :: needs real behavior proof before merge. :: [P1] Stop retrying after the compaction deadline | [P1] Do not attribute rejected requests to another compaction
  • reviewed 2026-09-01T21:55:18.044Z sha 7763d98 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-01T22:03:11.616Z sha 7763d98 :: needs real behavior proof before merge. :: [P1] Wait for route idleness before taking the binding lease
  • reviewed 2026-09-01T23:56:41.823Z sha 90389f8 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-02T00:33:27.765Z sha 90389f8 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-02T12:41:22.878Z sha fdbcbbd :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-02T13:27:59.054Z sha 250755a :: needs real behavior proof before merge. :: [P1] Revalidate the writer claim after native-CLI lane admission
  • reviewed 2026-09-02T14:47:11.856Z sha 64c0b97 :: needs maintainer review before merge. :: none

@jjjhenriksen
jjjhenriksen force-pushed the fix/codex-compaction-active-writer branch from 01da97e to 7763d98 Compare September 1, 2026 21:29
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Sep 1, 2026
@jjjhenriksen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 2, 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 exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling and removed extensions: codex labels Sep 2, 2026
@obviyus obviyus changed the title fix(codex): coordinate native compaction with active writers fix(agents): serialize native compaction with session writers Sep 2, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Sep 2, 2026
@obviyus

obviyus commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

Addressed all rank-up moves on 64c0b970febf7dadb4814e10d24632e6e5d6ee92:

  • Both native CLI and native harness compaction now validate the captured writer claim in the shared post-admission callback.
  • The shipped /compact path and native CLI path cover writer replacement during the lane wait.
  • The PR body now includes a redacted baseline and exact-head runtime event trace, including successful compaction completion and transcript recall.

@clawsweeper

clawsweeper Bot commented Sep 2, 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 exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper

clawsweeper Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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: 🧂 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. labels Sep 2, 2026
@obviyus
obviyus merged commit e571ea3 into openclaw:main Sep 2, 2026
995 of 1008 checks passed
@obviyus

obviyus commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jjjhenriksen!

Proof: A real managed Codex writer ran a command while native compaction was requested. The writer completed before compaction started, compaction completed, both transcript markers persisted, and a separate turn recalled them.

  • Bug: Native compaction could replace an active Codex writer and leave compaction incomplete.
  • Fix: Primary native CLI and harness compaction now use the session's existing execution lanes and revalidate the writer claim before native work.
Before After
The compaction request interrupted the writer and never completed its compaction item. The writer completed first, compaction completed next, and transcript state remained available.

Landed: e571ea3

Source: 64c0b97

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 3, 2026
…aw#135441)

## What Problem This Solves

Primary native compaction could start while the same OpenClaw session still owned an active writer. Codex treats manual compaction as a turn-like task and replaces an active task, so the overlap could interrupt the user turn and strand compaction before its terminal item.

## Why This Change Was Made

The host compaction coordinator now admits primary native harness and native CLI control operations through the existing per-session and global lanes. It revalidates the Codex session owner after admission. Direct compaction for callers that already own a lane remains unchanged.

The contributor's client-router wait, extra timeout, and duplicate rejection test were removed. A route can end before OpenClaw finishes its transcript checkpoint, so route lifetime was not the writer ownership boundary.

## User Impact

Codex-backed turns finish and persist before queued native compaction starts. The change does not add configuration, retries, timeouts, locks, or compaction policy.

## Evidence

- Current-main red at `d49fd235bfcc4e0ed94b09d13c7b7a6060908d5c`: the compaction request preceded writer completion, Codex interrupted the writer, and the compaction item did not complete.
- Exact-head green at `64c0b970febf7dadb4814e10d24632e6e5d6ee92`: writer command completed, writer turn completed, compaction request started, and the context-compaction item completed in that order.
- Transcript proof: both unique markers remained after compaction, and a separate follow-up turn recalled them.
- Regression proof: the native CLI ownership test fails on pre-repair head `250755ad0768f80197b2d87d1690c6bc1703fbcb` because compaction starts after the writer claim changes; it passes on this head without starting native I/O.
- Focused tests: all 173 compaction owner tests passed.
- Focused lint, format, and `git diff --check` passed.

### Real runtime order

| Baseline | Exact head |
| --- | --- |
| writer start → command start → compaction request → writer interrupted → compaction item start | writer start → command start → command complete → writer complete → compaction request → compaction item start → compaction item complete |

### Redacted event trace

```text
baseline d49fd23
01 writer_turn_started
02 writer_command_started
03 compaction_trigger
04 compaction_request firstTurnCompleted=false
05 writer_turn_completed status=interrupted
06 compaction_item_started
-- compaction_item_completed missing

exact head 64c0b97
01 writer_turn_started
02 writer_command_started
03 compaction_trigger
04 writer_command_completed
05 writer_turn_completed status=completed
06 compaction_request firstTurnCompleted=true
07 compaction_item_started
08 compaction_item_completed
```

Exact-head assertions: writer terminal `ok`; compaction `ok`; completion marker present; input and completion markers persisted; separate recall turn terminal `ok`; recall returned `WRITER_RECALL_OK`.

Co-authored-by: Ayaan Zaidi <[email protected]>
@jjjhenriksen
jjjhenriksen deleted the fix/codex-compaction-active-writer branch September 27, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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.

2 participants