Repository navigation
perf(infra): reuse SQLite readers in detached Gateway callbacks - #155779
Conversation
|
🦞👀 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 September 22, 2026, 3:00 PM ET / 19:00 UTC (Revision 2). ClawSweeper reviewWhat this changesPreserves reusable SQLite reader processes across detached Gateway callbacks and includes their launches in existing process diagnostics. Merge readiness✅ Ready for maintainer review The change remains useful: fetched main still loses reader reuse across detached callbacks. No actionable correctness or security defects were found, and the supplied production-path measurements support the improvement. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway reads stored authentication rows when resolving models for connections and scheduled work. Its reader scope owns reusable child processes while each request independently acquires database admission and reads fresh rows. flowchart TD
A[Connection callbacks] --> C[Captured Gateway reader scope]
B[Scheduled callbacks] --> C
C --> D{Owner still active?}
D -->|No| E[Reject late callback]
D -->|Yes| F[Reusable reader process]
F --> G[Fresh database admission and rows]
G --> H[Model resolution]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +49, Tests +107, Docs +3. Total +159 across 16 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep reader reuse with the existing Gateway lifecycle owner while preserving per-read admission, fresh rows, and joined shutdown cleanup. Do we have a high-confidence way to reproduce the issue? Yes: fetched main's detached callbacks omit the reader scope, causing unscoped auth reads to create and close children. The supplied before/after Testbox runs demonstrate that mechanism; this review did not execute it locally. Is this the best way to solve the issue? Yes: carrying the existing reader scope through the owning callback boundaries repairs reuse without adding another cache, changing credential selection, or retaining startup authority. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 097cd998a151. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
5cf37af to
edb9902
Compare
|
Merged via squash.
|
What Problem This Solves
Detached Gateway connection and scheduled callbacks lose the reusable SQLite reader scope. After auth-row invalidation, model resolution starts and closes a fresh Node child for every read.
User Impact
Repeated model resolution after auth bookkeeping avoids child startup overhead. No configuration, schema, credential selection, retention, or update behavior changes. Doctor/update inspection and the existing process isolation remain intact.
Why This Change Was Made
Carry only the existing read-only worker scope through connection work, scheduled runs (cron, heartbeat, and hooks), and lazy cron/hook construction. Captured owners reject callbacks after shutdown and cannot borrow another Gateway's scope. The existing scope still owns FIFO, launch compatibility, fresh read admission, and child cleanup. The session spawner also reports admitted children through the existing process-spawn counter; it previously bypassed that counter. The counter stays in a transport-free module so reader entry points preserve the spawn broker's lazy boundary.
The auth child intentionally remains a process so SQLite reads can coexist with the memory publication worker. Its open uses
readOnly: true, checks the schema version, reads auth rows, and closes handles; it does not run integrity validation or update the host verification record. Ordinary cron-store loads already use the shared-state worker. The separate artifact-preserving cron inspection path launchessync, not the observedsessionmode.Evidence
Before/after measurements on Blacksmith Testbox (
blacksmith-testbox, profileopenclaw-check, leasetbx_01m34k1kqp9pk8cqhr7x87vjsf), Node 24.19.0, source runtime, synthetic SQLite auth database. A temporary probe ran realresolveTieredModelwith a fixture provider hook through direct, connection, and scheduled entry points. Model fields and selected auth profile/mode matched the golden fixture throughout. Auth bookkeeping invalidated persisted rows before each measured resolution.One initial child served all 600 candidate resolutions, including the cache-hit controls. End-of-probe parent RSS was 667 MB before / 517 MB after; this short fixture run is not a production memory claim. Launch stacks identified
runSqliteAuthProfileWorker→createScopedSqliteReadOnlyWorker→createSqliteReadOnlyWorkerSession. Cron timers explicitly detach their async context, explaining why startup-created scope reuse did not cover those calls.Remote runs on the same lease (Testbox runner):
node scripts/run-vitest.mjs src/agents/embedded-agent-runner/model.child-churn.lane115.test.ts --pool=forks --testTimeout=240000; 55.48 s / 11.34 s test time. The temporary benchmark was removed after measurement.node scripts/run-vitest.mjs src/infra/sqlite-readonly-worker.auth.test.ts src/infra/sqlite-readonly-worker-session.test.ts src/gateway/server-cron-lazy.test.ts src/gateway/scheduled-run-gateway-context.test.ts src/gateway/server-connection-work.test.ts src/gateway/server-runtime-state.test.ts --maxWorkers=1: 79 tests passed, 49.27 s runner wall. Existing auth transport tests now prove the same child and fresh rows through detached connection/scheduler callbacks for native, broker, and private-snapshot transports.node scripts/check-changed.mjs: passed all selected typechecks, lint, storage/architecture guards, and zero runtime import cycles. Its default comparison base selected additional UI and Discord checks; these files are not part of the branch diff.pnpm build: passed after the diagnostics import repair (6m 11.5s). It also printed a Control UI startup-gzip budget diagnostic: 370952 B versus a 370876 B threshold, while exiting successfully. No UI files changed.check-changed --base HEAD -- <five import-cutover production files>passed core types and 13 core-test shards before its backing Actions run was cancelled (remote command exit 255; no code failure). Completed proof was retained.tbx_01m34rvgqp3q5x5t8m4sbvtxx2: original-code probes failed exactly as intended in four cases (three children instead of one across native/broker/snapshot transports; no session-spawn diagnostic). After restoring candidate bytes, all 63 tests in the four measured files passed.node scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.core.test.other.json --incremental, targetedrun-oxlinton the seven import-cutover production/test files,check-import-cycles.ts, andgit diff --checkall passed. The final command took 187 s. Both leases were stopped.Per-file cost on the replacement lease, using
pnpm test <file> --maxWorkers=1 --reporter=dot:sqlite-readonly-worker.auth.test.ts33.94 s wall;sqlite-readonly-worker-session.test.ts6.09 s;server-cron-lazy.test.ts16.27 s;spawn-utils.test.ts6.94 s. The auth file includes 13.3 s of cold worker compilation and existing real lost-broker cleanup coverage (about 10 s); actual test time was about 16 s. The added detached-read assertions reuse its existing child fixtures. Resume CI job timing is recorded below.Setup corrections: the first Testbox invocation found missing synced-checkout dependencies;
pnpm install --frozen-lockfilefixed that. The initial model probe used a Vitest thread, where shared-state admission correctly refused a non-host caller; using the fork pool exercised the actual host boundary. Neither was a product failure. The first build then caught an ineffective dynamic import introduced by importing metrics throughspawn-utils; moving the existing counter and its consumers tospawn-diagnosticspreserves that lazy boundary.Scope: +49 net production lines (+3 documentation lines), no new result cache. Independent Codex review found no actionable P0–P2 findings. No live Gateway was changed; the synthetic Testbox measurements do not establish the exact production process rate or savings on a 26 GB database.
CI repair and resumed validation
The compact shard failed while importing
src/cli/gateway-cli/run-loop.test.ts: connection callback capture eagerly loaded the SQLite worker logger before the fixture logger initialized. The reader scope and capture operation now live in a lightweight context module; all four Gateway consumers use it, while the existing worker retains ownership of admission, FIFO, cancellation, and child cleanup. The interrupted repair was preserved and completed.The other two failures were upstream defects already fixed on main: memory candidate-repair synchronization in #155745, and the Windows checkout fixture's duplicate PID-file publication in #154720. This branch incorporates those fixes by rebase; it adds no duplicate repair.
Blacksmith Testbox lease
tbx_01m356v8b9x0y31h1eqyf9p8q4(run):065eb1f58f13c5355be49edd113f82f22bc86f1d: all 218 CLI-loop tests passed (63.15 s runner wall), plus all 3 memory candidate-repair tests (34.99 s).node scripts/run-vitest.mjs src/cli/gateway-cli/run-loop.test.ts src/infra/sqlite-readonly-worker-session.test.ts src/infra/sqlite-readonly-worker.auth.test.ts src/gateway/server-cron-lazy.test.ts src/gateway/scheduled-run-gateway-context.test.ts src/gateway/server-connection-work.test.ts src/gateway/server-runtime-state.test.ts --maxWorkers=1passed all 297 tests across seven files, 126.50 s total runner wall.git diff --checkpassed. The lease was stopped. No tests, typechecks, or builds ran on the saturated Mac.Fresh independent AutoReview of the fully staged repair is clean through P2. The repair was folded into one commit with the original subject and rebased without conflicts.
Exact-head CI run 35770030471 passed for
edb9902b19d9974977c425154a7ecd9697246360, includingopenclaw/ci-gate. No job rerun was needed. Previously failing job wall times: compact-small-19 356 s, changed-extensions-bundle-28 298 s, Windows-node-test-2 357 s; these are whole-shard times, not the cost of the added assertions.Final rebased-candidate proof on Blacksmith Testbox
tbx_01m3579138f36dj8qqy3m19z4v(run):node scripts/check-changed.mjs --base HEADpassed all selected checks, including core types, all 25 test-type shards, zero dead exports, zero lint warnings/errors, storage/architecture guards, and zero runtime import cycles.pnpm buildpassed in 6m 14.1s, including the CLI bootstrap guard, 95 plugin packages, and built control-plane module/Doctor import checks. The combined command took 38m 30s. The build emitted an advisory startup-CSS budget warning while staying within its hard limit; this PR changes no UI files.The raw Testbox checkout deliberately retained the main Git base with the candidate applied as a working-tree delta. An initial HEAD-only assertion stopped before validation; the corrected preflight verified the exact base, all 16 changed paths, and every changed file's SHA-256 against the pushed commit before running proof. Both task leases were stopped after use.