Skip to content

perf(state): remove SQLite reply completion envelopes - #184101

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-sqlite-worker-reply-decode-20261026
Oct 26, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-sqlite-worker-reply-decode-20261026

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Every successful SQLite worker reply allocates a completion envelope solely to pass its decoded value back to the reply owner. This adds avoidable main-thread allocation during frequent state reads and writes.

User Impact

A small allocation reduction with no user-visible behavior change. Result values/types, bounded framing, native receipts, cleanup failures, and current-authority settlement retain their existing behavior. No configuration, schema, migration, or stored-data changes.

Why This Change Was Made

Inline the decoder into its sole caller's existing diagnostic scope. Completed replies assign their decoded value directly; incomplete transfers return their existing continuation request. Delete both temporary envelope variants and the single-use decoder helper. V8 serialization/deserialization stays: it preserves result types and participates in transport-size and uncertain-write contracts.

Runtime production delta: −16 LOC (+75/−91, mostly moving the decoder into its caller). The standalone measurement script adds 140 tooling lines; the regression adds 23 test lines. No cache or alternate fast path.

Evidence

The regression fails on base d4ae3a73118767a3f9bf80625a91733d19055393: the diagnostic callback hands off an additional {type: "complete", value: undefined} wrapper. With the change, completed replies settle the exact decoded values (including Buffer and bigint) without that wrapper. Completion wrappers handed off: 1 → 0 per reply. The regression permits either no callback result or direct decoded-value identity; it does not fix the diagnostic return shape or claim to detect arbitrary discarded allocations. The separate benchmark measures allocated bytes.

Owner benchmark: node --expose-gc --import ./scripts/tsx.mjs scripts/perf/sqlite-worker-reply-measure.ts. Actual receiveSqliteWorkerReply, synthetic scalar/session-entry/64-row-page results, prepared jobs and serialized inputs outside the loop. Node v26.12.1, macOS arm64, A/B then B/A. Each arm has five 5,000-reply untraced timing passes and one separate 5,000-reply allocation pass including collected objects (512-byte sampling). Table uses mean of each arm's median CPU and mean sampled allocation; 50,000 timing replies plus 10,000 allocation replies per revision per shape.

Reply shape Sampled bytes/reply, base → candidate Change Main-thread CPU μs/reply, base → candidate
Scalar 255.55 → 214.67 −16.0% 0.3210 → 0.3267
Session entry 943.86 → 909.91 −3.6% 0.9006 → 0.9046
64-row page 40,143.42 → 40,066.04 −0.2% 38.0379 → 39.0400

This is a small allocation win, not a demonstrated CPU or whole-Gateway latency improvement. CPU pair deltas have mixed signs; page cost remains dominated by required deserialization. Host one-minute load by arm: base A 25.96→24.84; candidate B 27.54→29.66; candidate B 29.66→29.66; base A 29.45→33.74. End-of-page sampled-pass RSS/heap MiB: base 334.5/75.8 and 336.9/75.9; candidate 336.1/75.6 and 318.5/75.3. Memory snapshots are context, not retained-heap claims. No 10-minute Gateway stress or A/A campaign was run for this envelope deletion; there is no claim of Gateway p99, GC-rate, or throughput improvement.

Focused proof: 46 tests passed across sqlite-worker-broker-reply.test.ts, sqlite-worker-broker-reply.cleanup.test.ts, sqlite-worker-input.test.ts, and sqlite-worker-store.test.ts with --maxWorkers=1. This includes real worker round trips, 72 MiB input framing, receipt ordering, cleanup/outcome uncertainty, and post-commit serialization failure. Combined wall time 83.64 s (74.30 s Vitest, including worker fixture compilation); the changed reply test's cases run in milliseconds. No Testbox lease used; broad gates belong to PR CI.

Export-name collision guard and both closure test files passed (22 tests, 59.57 s wall time). Changed-file format/lint passed. Independent subagent review (autoreview route unavailable) was scoped-clean at P0/P1/P2; the helper refused the configured provider mismatch before inference. The automated review correctly identified overly narrow wording/assertion in the regression: it now permits direct decoded-value identity and explicitly limits its claim to wrapper handoff. No accepted findings remain. The refined regression was rerun: base fails on the extra wrapper, candidate passes all four reply tests (78.98 s wall, including worker fixture compilation). Exact-head CI run 40259966088 passed on a6fea445adae538ddd60d561522c779c7b76d1f1, including all PostgreSQL conformance shards and the CI gate job. ClawSweeper reviewed that same head with no actionable findings and no before-merge requirements; its owner-only proof and diagnostic-handoff-test caveats are explicitly retained above. Ready for maintainer handoff; intentionally left unmerged.

Prior head 8345056b1ac68987517460cfe4c616e58f6182e3 hit a UI-only failure in CI job 121367137069: chat-nested-tool-presentation.e2e.test.ts:135, diagnostics.isVisible() returned false. Its mocked Gateway/browser path does not exercise this SQLite reply owner. No UI fix or successful-retry attribution is claimed; the subsequent push addresses the independently reviewed test assertion above.

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T02:38:51.777657Z a6fea44 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: M labels Oct 26, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8345056b1a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@steipete
steipete force-pushed the steipete/perf-sqlite-worker-reply-decode-20261026 branch from 8345056 to a6fea44 Compare October 26, 2026 02:36
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR inlines SQLite reply decoding into its existing caller and removes temporary completion and continuation envelopes.

Example: A SQLite worker returns the scalar result 42.

  • Before: The broker decodes 42, wraps it in a completion object, then settles with 42.
  • After: The broker assigns the decoded 42 directly and settles with the same value.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The production simplification appears correct; evidence is limited to owner-level measurements and reported tests, and the added diagnostic-shape assertion has low value.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; its reported Node 26 macOS benchmark exercises the actual reply owner with synthetic serialized results and supports only the stated allocation reduction.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Frequent state operations create avoidable temporary objects on the main thread.
Reason: The maintainer-selected direction removes production machinery and reports a bounded allocation benefit without claiming a latency improvement.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR makes a focused allocation reduction, and inspection found no introduced production defect.

Priority: P3
Reviewed head: a6fea445adae538ddd60d561522c779c7b76d1f1

Before merge

None.

Findings

None.

Tests

  • Low-value test src/infra/sqlite-worker-broker-reply.test.ts: Remove the diagnostic spy and return-shape assertion at lines 41 and 52–55, retaining decoded-value settlement checks: the assertion can pass with discarded envelope allocations and constrains an internal diagnostic handoff rather than an independent behavior contract.
  • Missing end-to-end proof: The benchmark exercises the reply owner directly; no shipped Gateway allocation measurement is supplied.
Agent review details

How this fits together

The SQLite broker receives serialized worker replies, assembles bounded transfers, publishes receipts, and settles the waiting operation through its existing admission owner.

flowchart LR
  A[State operation] --> B[SQLite broker]
  B --> C[Database worker]
  C --> D[Reply decoder]
  D -->|More frames| C
  D --> E[Receipt and admission settlement]
  E --> F[Waiting caller]
Loading

Technical review

Best possible solution:

Keep direct decoded-value assignment in the existing reply owner while preserving serialization, framing, receipts, and settlement.

Do we have a high-confidence way to reproduce the issue?

Current main constructs a completion object for successful decoded replies; source confirms the allocation path, and no target code was executed during this read-only review.

Is this the best way to solve the issue?

Inlining the single-use decoder removes the unnecessary representation at its existing owner without adding state, caches, or a competing path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 5bc90d07df85.

Provenance checked

  • Completion and continuation envelopes changes intended behavior with a stated reason (a02a33d: The bounded large-command transport represented decoder completion and continuation with a tagged union.)
  • Completed outcomes and cleanup ordering keeps the original intent (ed2192d: Completed results must survive coordinator cleanup failures while incomplete delivery remains uncertain.)
  • Decode diagnostics and transferable input bytes keeps the original intent (fix: prevent Bun Doctor and updates from failing to clone database commands #172175: Bun serialization can use shared backing storage, so continuation input must retain owned transferable bytes; decode diagnostic attribution also remains intact.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Dallin Romney: Raw commit 35d2e97 adds src/infra/sqlite-worker-broker-reply.ts:15 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: low; commits: 35d2e97a4af7; files: src/infra/sqlite-worker-broker-reply.ts)

PR surface

Source -16, Tests +23, Other +140. Total +147 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 75 91 -16
Tests 1 23 0 +23
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 140 0 +140
Total 3 238 91 +147

Labels

Label changes:

  • add P3: This is a small allocation optimization with unchanged user-visible behavior and no demonstrated urgent runtime failure.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P3: This is a small allocation optimization with unchanged user-visible behavior and no demonstrated urgent runtime failure.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 25, 2026, 10:52 PM ET / October 26, 2026, 02:52 UTC.

@steipete
steipete merged commit 10a3260 into main Oct 26, 2026
141 checks passed
@steipete
steipete deleted the steipete/perf-sqlite-worker-reply-decode-20261026 branch October 26, 2026 03:06
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
* perf(state): remove SQLite reply completion envelopes

* test(state): allow direct decoded reply handoff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant