Skip to content

fix(gateway): hold the chat root through terminal persistence - #126183

Merged
steipete merged 1 commit into
mainfrom
steipete/gateway-rootwork-persistence
Aug 19, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/gateway-rootwork-persistence

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

A Gateway chat run could lose its root-work admission before its terminal state was written, so a
restart preflight reported it was safe to restart while persistGatewaySessionLifecycleEvent was
still in flight
. The session's terminal state is then lost across the restart, with nothing logged.

The invariant is stated in src/gateway/server-methods/chat-send-dispatch-errors.ts:

// Stop exposing the rejected run before projecting its terminal state, but keep the
// admitted root until persistence settles so restart drain still observes this work.

finalize() upholds it by holding the root across the persistence await. But the root was only
held because of one line in chat-send-agent-dispatch.ts, executed after the ACK:

setReleaseGatewayRootContinuation(retainGatewayRootWorkAdmissionContinuation() ?? undefined);

retainGatewayRootWorkAdmissionContinuation() returns null when the ALS root store is absent or
already retired, and ?? undefined swallowed it. No continuation was registered, the initial
admission was released, and the root count reached 0 before persistence ran — silently.

This surfaced as a CI failure on an unrelated PR (#125958), job checks-node-compact-small-3, shard
agentic-control-plane-agent-chat-hosted-1, on three separate attempts:

marks a running webchat session failed when restart drain overlaps dispatch rejection
AssertionError: expected +0 to be 1

Why This Change Was Made

The reservation moves into admitChatSend, immediately after the session work admission is
registered — the earliest point where the request's root is provably live, and before
onAdmissionOwned, attachment preparation, the ACK, and startChatDispatch. The existing
cleanupAdmittedRun closure is the single release owner, and the early-abandonment paths release it
too. The late setter and the silent ?? undefined are deleted.

Callers that enter handleChatSend outside a Gateway request envelope (internal chat entries such as
skills and suggestion flows) hold no root at all, so their continuation is a no-op — there is no
request root to extend and no drain guarantee to preserve. This matters: an earlier iteration
threw when the reservation was unavailable, which broke 183 tests across those legitimate
callers. That is why the no-root case is a no-op rather than an error, and why the full
server-methods suite is part of the evidence below.

Rejected alternatives: keeping the late reservation (preserves the silent-null window); retaining
inside dispatch-error finalization (already too late if the inherited root was retired); relaxing the
assertion, retrying, or widening a timeout (masks silent terminal-state loss).

Known gap, stated plainly: the exact producer that retires the ALS root in the CI runs is not
proven. Source tracing found no normal chat caller that leaves the root scope, and
startChatDispatch is synchronous with no await before the old reservation. What is proven is the
consequence and its repair: forcing only the reservation to fail reproduces the CI signature exactly,
and reserving at admission removes the window regardless of which producer retires the root.

User Impact

A chat run whose dispatch fails now keeps the Gateway root until its terminal state is persisted, so
a restart preflight reports "not safe" until that write completes instead of waving the restart
through. No API, config, or protocol change.

Evidence

Mechanism confirmed before writing any fix. Replacing only the old retain with
setReleaseGatewayRootContinuation(undefined), changing nothing else, reproduced the CI failure
verbatim: AssertionError: expected +0 to be 1.

Hypotheses tested and disproved, recorded so they are not re-tread: async release while
persistence is blocked (instrumented; count stayed 1 at t+0/25/225ms); a foreign
persistGatewaySessionLifecycleEvent resolving the test's deferred early (instrumented the spy;
exactly one call, runId=idem-dispatch-error-1 phase=error rootCount=1, for both the single test and
the full file); leaked restartDraining from a sibling file (fails differently, at
expect(res.ok).toBe(true)); sibling gateway files leaking admission state (all reset; running them
together passes).

The regression now asserts the operator-visible surface, not an internal counter. While
persistence is blocked it asserts createSafeGatewayRestartPreflight(...) reports
{ safe: false, counts: { rootRequests: 1 } }, then that it flips to safe: true once the write is
released. Proof it is load-bearing: with the reservation stubbed out the preflight reports
safe: true while the terminal state is still being written — i.e. the failure message now names the
actual bug — and restored, it passes.

Suite results on current main:

  • src/gateway/server-methods — 3415 passed (186 files).
  • src/gateway/server.chat.gateway-server-chat.test.ts — 55 passed.
  • src/process/gateway-work-admission.test.ts — 18 passed.
  • node scripts/run-tsgo-core-test-shards.mjs — 0 type errors.

Branch-mode autoreview clean (patch is correct). Production LOC +9/-11 (net −2); tests +15/-2.

Note: waitForActiveGatewayRootWork was removed from main by #126024 mid-work, so the test uses the
current createSafeGatewayRestartPreflight surface.

startChatDispatch reserved the detached dispatch's root-work continuation
after the ACK, and swallowed a failed reservation with ?? undefined. When
the reservation did not land, the request's root was released before
chat-send-dispatch-errors finalize() awaited
persistGatewaySessionLifecycleEvent, so a concurrent restart drain saw
zero active roots and completed while the session's terminal state was
still being written.

Reserve in admitChatSend instead, where the request root is provably
live, and release it from the single cleanupAdmittedRun owner. Callers
outside a Gateway request envelope hold no root, so their continuation is
a no-op.

The regression now starts a real waitForActiveGatewayRootWork() drain
while persistence is blocked and asserts it cannot settle until the write
is released, instead of asserting an absolute process-global counter.
@clawsweeper

clawsweeper Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete
steipete marked this pull request as ready for review August 19, 2026 04:27
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS maintainer Maintainer-authored PR labels Aug 19, 2026
@steipete
steipete merged commit d96f1d5 into main Aug 19, 2026
282 of 286 checks passed
@steipete
steipete deleted the steipete/gateway-rootwork-persistence branch August 19, 2026 04:38
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

steipete added a commit that referenced this pull request Aug 19, 2026
The vi.mock of resolveGatewayModelSupportsImages in
server.chat.gateway-server-chat.test.ts was added by #123847 as an
isolation workaround while the gateway shard's chat failures were
unexplained. The producers are fixed on main (#126183 for the
root-work drain race; #126062/#125946/#126113 for detached completion
binding), and the exact #126030 shard invocation is green with the
mask removed (5/5 on 95ac5b2, 2/2 on this head), so the mask now
only hides the real capability-resolution path from the suite.
steipete added a commit that referenced this pull request Aug 19, 2026
* test(gateway): drop the chat image-capability mask

The vi.mock of resolveGatewayModelSupportsImages in
server.chat.gateway-server-chat.test.ts was added by #123847 as an
isolation workaround while the gateway shard's chat failures were
unexplained. The producers are fixed on main (#126183 for the
root-work drain race; #126062/#125946/#126113 for detached completion
binding), and the exact #126030 shard invocation is green with the
mask removed (5/5 on 95ac5b2, 2/2 on this head), so the mask now
only hides the real capability-resolution path from the suite.

* test(gateway): assert inline-image dispatch after mask removal

ClawSweeper P2 on #126217: the two image sends only asserted RPC success
and drain, so a silent false from the real capability resolver would
offload the images and the suite would stay green without proving the
discovered model's image capability. Capture the dispatch args and
assert both runs deliver exactly one inline image. Teeth check: with the
discovered model narrowed to text-only input, the assertion fails with
images undefined (offloaded).
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 20, 2026
…aw#126183)

startChatDispatch reserved the detached dispatch's root-work continuation
after the ACK, and swallowed a failed reservation with ?? undefined. When
the reservation did not land, the request's root was released before
chat-send-dispatch-errors finalize() awaited
persistGatewaySessionLifecycleEvent, so a concurrent restart drain saw
zero active roots and completed while the session's terminal state was
still being written.

Reserve in admitChatSend instead, where the request root is provably
live, and release it from the single cleanupAdmittedRun owner. Callers
outside a Gateway request envelope hold no root, so their continuation is
a no-op.

The regression now starts a real waitForActiveGatewayRootWork() drain
while persistence is blocked and asserts it cannot settle until the write
is released, instead of asserting an absolute process-global counter.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 20, 2026
* test(gateway): drop the chat image-capability mask

The vi.mock of resolveGatewayModelSupportsImages in
server.chat.gateway-server-chat.test.ts was added by openclaw#123847 as an
isolation workaround while the gateway shard's chat failures were
unexplained. The producers are fixed on main (openclaw#126183 for the
root-work drain race; openclaw#126062/openclaw#125946/openclaw#126113 for detached completion
binding), and the exact openclaw#126030 shard invocation is green with the
mask removed (5/5 on 534f1cb, 2/2 on this head), so the mask now
only hides the real capability-resolution path from the suite.

* test(gateway): assert inline-image dispatch after mask removal

ClawSweeper P2 on openclaw#126217: the two image sends only asserted RPC success
and drain, so a silent false from the real capability resolver would
offload the images and the suite would stay green without proving the
discovered model's image capability. Capture the dispatch args and
assert both runs deliver exactly one inline image. Teeth check: with the
discovered model narrowed to text-only input, the assertion fails with
images undefined (offloaded).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant