openclaw - 💡(How to fix) Fix Hoist withOwnedSessionTranscriptWrites ALS scope to span agent.prompt() to fix vanilla-openclaw same-lane fence trip

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Same-lane EmbeddedAttemptSessionTakeoverError race in vanilla openclaw / pi caused by pi's listener-driven session writes happening outside the withOwnedSessionTranscriptWrites AsyncLocalStorage scope. The current ALS context only wraps agent.streamFn(...), but pi's session-event listeners (_handleAgentEvent_persistappendFileSync) iterate the constructed stream after streamFn resolves, so their writes never publish to ownedSessionFileWrites. The next withSessionWriteLock call (e.g., a beforeToolCall hook) then trips assertSessionFileFence because the file changed without a matching owned-write record.

Root Cause

Same-lane EmbeddedAttemptSessionTakeoverError race in vanilla openclaw / pi caused by pi's listener-driven session writes happening outside the withOwnedSessionTranscriptWrites AsyncLocalStorage scope. The current ALS context only wraps agent.streamFn(...), but pi's session-event listeners (_handleAgentEvent_persistappendFileSync) iterate the constructed stream after streamFn resolves, so their writes never publish to ownedSessionFileWrites. The next withSessionWriteLock call (e.g., a beforeToolCall hook) then trips assertSessionFileFence because the file changed without a matching owned-write record.

Fix Action

Fix / Workaround

  • Reproduce one of @cby2k's deterministic cases on a patched build
  • Confirm cuobjdump-equivalent for the embedded-runner path: writes during the for-await loop appear in ownedSessionFileWrites after the patch
  • A regression test that drives a synthetic prompt-emitted-during-streaming write and asserts no fence trip on the subsequent withSessionWriteLock
RAW_BUFFERClick to expand / collapse

Summary

Same-lane EmbeddedAttemptSessionTakeoverError race in vanilla openclaw / pi caused by pi's listener-driven session writes happening outside the withOwnedSessionTranscriptWrites AsyncLocalStorage scope. The current ALS context only wraps agent.streamFn(...), but pi's session-event listeners (_handleAgentEvent_persistappendFileSync) iterate the constructed stream after streamFn resolves, so their writes never publish to ownedSessionFileWrites. The next withSessionWriteLock call (e.g., a beforeToolCall hook) then trips assertSessionFileFence because the file changed without a matching owned-write record.

Why this is separate from #86067

#86067 closes two specific races:

  1. Cross-lane same-file race — file-scoped prompt-window guard (commit 65705d8c39)
  2. Same-lane post-stream drain — for deployments that populate _agentEventQueue (commit 4254386508, reported by @kesslerio for AlphaClaw)

Neither change touches the ALS scope. In vanilla openclaw both fixes are either narrow (case 1) or no-op (case 2 — _agentEventQueue isn't populated by the upstream session manager). The case described above is what vanilla openclaw users (e.g. @cby2k's deterministic repro on session UUID 4f181fe9-…) are likely hitting.

Proposed fix shape

Hoist the withOwnedSessionTranscriptWrites wrapper from installPromptSubmissionLockRelease (currently src/agents/pi-embedded-runner/run/attempt.session-lock.ts:1015-1041, wrapping only originalStreamFn) up to span the entire agent.prompt(...) call. This requires composing the wrapper at src/agents/pi-embedded-runner/run/attempt.ts:3243-3249 (or equivalent) so pi's listener-driven writes during the for-await loop also see the ALS context, get published as owned writes, and the fence accepts them on subsequent checks.

Alternative shape: instrument pi's _handleAgentEvent listener wrapper to publish writes directly into ownedSessionFileWrites regardless of ALS state. Less elegant but avoids touching the call composition above.

What this would address

  • @cby2k's deterministic repro on session 4f181fe9-… (long inbound turns hanging ~22s then dropping; gateway-fallback fresh-session takeover path)
  • The kesslerio post-fence trip path that the #86067 drain doesn't reach
  • The 122-event/3-day burst pattern documented in #85913's live-repro comment

Acceptance criteria (suggested)

  • Reproduce one of @cby2k's deterministic cases on a patched build
  • Confirm cuobjdump-equivalent for the embedded-runner path: writes during the for-await loop appear in ownedSessionFileWrites after the patch
  • A regression test that drives a synthetic prompt-emitted-during-streaming write and asserts no fence trip on the subsequent withSessionWriteLock

Risk

Hoisting the ALS scope means anything pi's listener machinery writes during the prompt window will be classified as an "owned write." Need to verify no spurious classifications (e.g., from extension code that legitimately writes external data inside a listener). Test surface should cover the corner case where a session listener also touches a file outside the trusted scope.

Related

  • #85913 — root issue tracking EmbeddedAttemptSessionTakeoverError
  • #86067 — fixes cross-lane race + AlphaClaw drain case (not vanilla pi root cause)
  • #84250 — earlier partial fix referenced by @cby2k
  • #85955 — TurboTheTurtle's parallel attempt (rated 🧂 unranked krab, waiting on author)

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING