Repository navigation
refactor(gateway): lifecycle-owned ambient state via factory-registered drains - #118271
Conversation
|
Codex review: needs real behavior proof before merge. Reviewed August 2, 2026, 8:47 PM ET / August 3, 2026, 00:47 UTC. ClawSweeper reviewWhat this changesThis PR registers process-local gateway, plugin, agent, and session state with lifecycle-specific drains so shutdown, in-process restart, and plugin-registry replacement settle waiters, cancel timers, and clear stale registries. Merge readiness⛔ Blocked until stronger real behavior proof is added - 5 items remain Keep this PR open for maintainer review. The lifecycle design is coherent and the current-head source preserves close-only state during SIGUSR1 restart, but the final head adds an agent-job listener-lifecycle change after the submitted runtime transcript and expands an existing public Plugin SDK helper without visible maintainer confirmation in the supplied discussion. Priority: P2 Review scores
Verification
How this fits togetherGateway, agent, session, and plugin flows create process-local maps and listeners that can outlive a request. Gateway close and SIGUSR1 restart invoke lifecycle drains so the next server lifecycle does not inherit stale state, while plugin-registry replacement clears plugin-contributed registrations separately. flowchart LR
A[Agent, session, and plugin activity] --> B[Process-local registries]
C[Gateway close or SIGUSR1 restart] --> D[Lifecycle drain coordinator]
E[Plugin registry replacement] --> F[Plugin-registry drain]
D --> G[Settled waiters and cancelled timers]
F --> G
G --> H[Clean next gateway lifecycle]
Decision needed
Why: The implementation preserves existing call signatures, but it changes the behavior and supported capabilities of helpers exported from an established SDK subpath; the supplied PR body alone is not maintainer-visible approval. Before merge
Agent review detailsSecurityNone. PR surfaceSource +399, Tests +132, Generated 0, Other +1. Total +532 across 49 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Retain one lifecycle-owner mechanism, obtain explicit maintainer confirmation that its SDK-facing arguments are a supported public contract, and publish a redacted current-head dist-backed close/SIGUSR1 transcript that covers the final agent-job listener change. Do we have a high-confidence way to reproduce the issue? Yes, source-reproducible: the close and SIGUSR1 paths select distinct lifecycle drain events, and focused tests exercise the drain contract. The supplied real runtime transcript is not current-head evidence because it predates the final agent-job listener change. Is this the best way to solve the issue? Unclear pending maintainer approval: a single factory-owned drain is a maintainable repair for shared runtime state, but exposing reset registration through the public Plugin SDK must be explicitly accepted rather than inferred from an additive signature. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 18c9f27e2de3. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (5 earlier review cycles)
|
599cbbb to
517a443
Compare
Keep agent-job data process-shared while preserving module-local event subscription ownership. Ratchet the public wildcard budget down after replacing text-runtime's wildcard singleton export with named exports.
517a443 to
2071a82
Compare
…ed drains (openclaw#118271) * refactor(runtime): own ambient state lifecycle cleanup * chore(plugin-sdk): refresh additive API baseline * fix(runtime): restore agent wait listener lifecycle Keep agent-job data process-shared while preserving module-local event subscription ownership. Ratchet the public wildcard budget down after replacing text-runtime's wildcard singleton export with named exports.
What Problem This Solves
The conversation-binding pilot (#118075) established the pattern: ambient module state needs a lifecycle owner. A read-only inventory of current main found 44 hand-verified sites that lack one — cross-request pending state (agent questions, conversation turns, placement claim waiters, agent-job maps with live timers, MCP grants), plus listener/registry sets that accumulate across lifecycles. Worse, the tree has a structural asymmetry: close-path owners don't run on in-process SIGUSR1 restart and restart-path owners don't run on close, so each lifecycle leaks through the other.
Why This Change Was Made
Ownership moved to the factory instead of 44 hand-wired clears.
resolveGlobalSingletonnow accepts an owner class at slot creation:clearActivePluginRegistryfor plugin-contributed registrationsDrain ordering is deliberate: the gateway drain runs after plugin cleanup and before secret-snapshot clearing; the SIGUSR1 drain runs before durable task reload; plugin host runtime state clears in
finallyafter plugin cleanup — this also delivers the highest-value single fix from the inventory (wiringclearPluginHostRuntimeStateinto registry clearing; it was previously reachable only from the test reset).Conversions are semantic, not just map-dropping: pending question/turn/placement waiters are rejected or settled so no promise dead-ends at shutdown; agent-job timers are canceled; MCP watchers are canceled and awaited. Sites where clearing would violate serialization or need channel-specific teardown are explicitly deferred with reasons (table in the report below).
Named LOC tradeoff: production +525/−118 (net +407). The growth is the semantic cancellation paths for live timers, waiters, and OS resources — the alternative (silent map clears) would trade a leak class for a dead-ended-promise class. The deleted per-site
*ForTestresets and the removed restart/close asymmetry are the structural payback.User Impact
Gateway close and in-process restart now start from a clean slate: no stale pending approvals, no orphaned waiters resolving into the next lifecycle, no duplicate listener dispatch after restart, and minted MCP grants do not survive termination.
Deferred (explicit, with reasons)
Stateful target drivers (atomic builtin-registration reset needed), task-flow registry (SQLite-backed, reloads), health registry (extension WeakSet), before-agent-reply (ALS scope, not a registry), fire-and-forget hooks (needs generation-aware cancellation), session/store/SQLite writer queues (clearing promise tails violates serialization), extension candidates (need channel/plugin teardown owners).
Evidence
Dist-driven lifecycle proof
Exact head
599cbbb67c3864494c1a8c35512dbdf643fd14ef; directdist/entry.js; ephemeralOPENCLAW_STATE_DIRandTMPDIR; dynamically allocated loopback-only port;--allow-unconfigured; channel startup skipped; child environment scrubbed to proof-only variables. The proof slots use the public lifecycle API and log only when their real reset callback executes.Close transcript:
SIGUSR1 restart transcript (close-only emits no marker during the restart phase):
Verdict:
{"head":"599cbbb67c3864494c1a8c35512dbdf643fd14ef","distEntry":"dist/entry.js","isolatedLoopback":true,"ephemeralStateRemoved":true,"close":{"health":200,"drainOrder":["plugin-registry","close-and-restart","close-only"],"cleanShutdown":true,"exit":0},"restart":{"initialHealth":200,"returnedHealth":200,"pluginRegistryDrainObserved":true,"closeAndRestartDrainObserved":true,"closeOnlyDrainedDuringRestart":false,"returnedReady":true,"finalExit":0}}Maintainer decision: the optional lifecycle-reset parameters on resolveGlobalSingleton/resolveGlobalMap are accepted as additive public SDK surface; existing external plugin call sites are unaffected (prior signatures unchanged, defaults preserve prior behavior), manifest regenerated per the gate's intentional-change flow.
🤖 Generated with Claude Code