Repository navigation
perf(sqlite): release caller context from reusable read workers - #164370
Conversation
Launch scoped readers and native replacements in the existing detached lifecycle context after capturing launch facts. Release settled operation promises and keep FIFO tails outside request context while preserving per-read admission and command callback authority. Real native/broker GC regressions and two-turn dist Gateway snapshots prove completed callers collect while workers remain reusable. Related: #164307.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 3, 2026, 11:27 AM ET / 15:27 UTC. ClawSweeper reviewWhat this changesThe PR prevents reusable SQLite read processes and settled promises from retaining completed callers, with real-process GC regressions and lifecycle documentation. Merge readiness✅ Ready for maintainer review Keep open: this is a focused, well-supported retention fix still absent from current main. No actionable introduced defect or additional proof blocker was found. Priority: P2 Review scores
Verification
How this fits togetherOpenClaw uses reusable SQLite inspection processes to read database state without blocking the Gateway. Caller requests enter a serialized read queue, and results return while the lifecycle owner retains and eventually closes the process. flowchart TD
A[Caller read request] --> B[Capture launch facts]
B --> C[Serialized read queue]
C --> D[Reusable SQLite process]
E[Detached lifecycle context] --> D
D --> F[Result and caller settlement]
F --> G[Release completed caller references]
E --> H[Process cleanup]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +12, Tests +148, Docs +6. Total +166 across 6 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep reusable reader lifetime independent of completed callers while preserving per-read admission, cancellation, FIFO ordering, and native cleanup custody. Do we have a high-confidence way to reproduce the issue? Yes: the real-process GC regression supplies a focused reproduction, and the body records intended baseline failures for both transports. Current main retains the relevant production paths; this reviewer did not execute them. Is this the best way to solve the issue? Yes: reusing the existing detached lifecycle owner and releasing settled promises addresses the retaining roots without changing the generic broker callback contract or introducing another owner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against fd8397a6bb6e. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
…claw#164370) Launch scoped readers and native replacements in the existing detached lifecycle context after capturing launch facts. Release settled operation promises and keep FIFO tails outside request context while preserving per-read admission and command callback authority. Real native/broker GC regressions and two-turn dist Gateway snapshots prove completed callers collect while workers remain reusable. Related: openclaw#164307.
What Problem This Solves
A reusable SQLite inspection child can keep the completed agent turn that first created it alive until the child closes. The broker's stdout/stderr callback context carries that turn's settlement closure, which can reach prompt and transcript data. This is bounded retention per worker, not evidence of an unbounded per-turn leak.
Related: #164307.
User Impact
Long-running Gateways can release completed read callers while retaining their reusable inspection process. There are no configuration, schema, stored-data, or update-migration changes.
Why This Change Was Made
Capture the runtime generation, broker, environment, and working directory before launching the reusable session in the existing detached lifecycle context. The same boundary covers native replacement after confirmed broker nonadmission. The scope's FIFO tail is created outside the request context, and the session drops its completed operation promise while preserving the join for outstanding work.
Ordinary broker commands continue to deliver callbacks in their original command context. Read admission, cancellation, deadline ownership, native-close custody, and broker-loss cleanup remain with their existing owners.
Production LOC: +12 net. Growth enforces the separate lifetimes required by the existing command-callback and read-admission contracts: two creation boundaries, one neutral FIFO tail, and clearing completed work at its three settlement paths. No second implementation, context manager, cache, or configuration option is introduced.
Evidence
Independent review, before/after real-worker and dist Gateway heap proofs, focused Testbox tests, and the staged changed-check gate passed. CI run 37132984808 and
openclaw/ci-gatepassed for headd23cd21b5afb8baa2dd992dc032619fd8575e45e.60e849f1a4aa2101382883e91e97f87aaf297451.tbx_01m414hjd2cghxm107946f0rmz, profileopenclaw-check, Node 24.19.0: 40 tests passed across five files, candidate command wall time 40.77s. The original queued leasetbx_01m410n7kd1z43nmd6mn2hg6xydisappeared before admission and was replaced. After 20 minutes queued, the authorized fallback startednode scripts/check-changed.mjs --stagedthrough/srv/rig/rig-staged-check.sh 384-check 60e849f1a4aa2101382883e91e97f87aaf297451in a separate isolated Linux rig checkout; exit 0 (about 58 minutes, cold caches). Its final summary is below. The stale SSH connection was closed after the log confirmed completion; no validation process remained./srv/rig/wt-384, synthetic provider and state, dist Gateway. Baseline first-turnBrokerChild.callbackContext → AsyncContextFrame → owner.settle → params.runLocal → contextEngineLogicalTurnLeasepersists after a second unrelated turn and full GC. The frame exclusively retains 51,784 bytes, including a 2,608-byte logical-turn lease. After the fix the same worker callback boundary is neutral (0 retained bytes in that measured context subgraph), and the first turn’s exact logical-turn lease node is absent from the second snapshot. These are graph bytes, not snapshot file sizes or an hourly leak-rate claim.node scripts/run-vitest.mjs src/infra/sqlite-readonly-worker.retention.test.ts --maxWorkers=1, pass, 6.08s wall / 0.721s test time (0.308s native + 0.413s broker); compile-generation lookup accounts for 2.37s. CI measured 3.86s file envelope / 3.59s runner / 0.819s test cases (0.367s native + 0.452s broker) in checks-node-changed-4, using the Bun harness with Node GC subprocesses.Focused candidate command on the same Testbox lease (exit 0):
The unchanged retention test first ran against the original two production files and failed both cases with the intended post-GC assertion (18.66s wall including cold compilation). The candidate then passed; source hashes were checked against the reviewed staged patch. The missing Testbox dependencies were installed once with
pnpm install --frozen-lockfile.sourcePerformancedist buildRig commands used Node 26.10.0, the repository's synthetic provider, isolated state and loopback ports. Both
node --import ./scripts/tsx.mjs scripts/build-all.mts sourcePerformancebuilds and both two-turn dist Gateway runs exited 0.scripts/heap-snapshot-diff.mjscompared each process's own snapshot pair and reported named strong paths/dominators. BrokerChild's own exclusive 9,488-byte size is unchanged; the removed graph belonged to its captured request context. The first baseline build stamp rejected a fixture copied mid-build; the stable-input rebuild passed.The GC regression uses bounded event-loop yields, not wall-clock sleeps. Real child/pipe resources are necessary here because mocked events do not reproduce Node’s async-context retention.
No live deployment was changed. The census follow-ups below have source evidence only, and this bounded retention fix does not establish an improvement to an hourly whole-Gateway heap slope.
Staged changed-check summary (Linux rig, exit 0)
node scripts/check-changed.mjs --staged== exit=0 (15:12Z)Long-lived resource census and follow-up owners
“Yes” below denotes source-supported caller-context capture unless separately backed by the heap evidence above. It is not a measured byte count or proof of unbounded growth. Existing module-initialized context snapshots protect creation only when imported before request admission.
createScopedSqliteReadOnlyWorker/ inspection scoperunSqliteReadOnlyWorker/ sessioncaptureSqliteSnapshotStagingOwner/ cached native sourcecaptureResourcesnapshots calleropenclaw-state-worker-owner/ actor entrySqliteWorkerInputAdmission.queueOpen/ global brokeropenTailjoinOpensworker-native-lifecyclesubagent-registry-sweeper.schedulelazy-promise,llm/streamprovider-local-servicecode-mode-stategateway-schedulersystem-diskscaptureRetainedNativeWorkerSource/ source ownerA caller-created
setImmediatestill inherits ALS;triggerAsyncId: 0alone does not clear stores. The patch reuses the established lifecycle root rather than creating a new request-local async resource.