Skip to content

improve(auth): reduce temporary allocations during profile reads - #163245

Merged
vincentkoc merged 1 commit into
mainfrom
perf/sqlite-auth-frame-base64-20261002
Oct 2, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
perf/sqlite-auth-frame-base64-20261002

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Auth-profile reads create an extra byte-buffer copy for every JSON IPC frame before encoding it.

User Impact

Reduces temporary allocation and copying in auth-profile transfers. Returned credentials, cancellation, child cleanup, and database behavior remain unchanged. No configuration or migration is required. End-to-end latency and peak-memory improvements are not claimed.

Why This Change Was Made

Encode a view of the frame's exact backing-buffer range synchronously; only the resulting immutable base64 string escapes. Producer-frame ownership and receiver reassembly remain intact. The same pattern already exists in src/infra/node-duplex-framing.ts, and follows the Node Buffer contract.

Production delta is +10 lines, including formatting and one ownership comment; there is no new helper, cache, worker, or fallback. Tests add 22 lines protecting offset/sentinel boundaries and string independence for both Uint8Array and Buffer views.

Evidence

  • 52/52 tests passed across the new encoder test, real auth-worker tests, read-only session lifecycle tests, and generic transfer tests on Node 24.16.0 / macOS arm64. Coverage includes complete oversized native/broker/snapshot results, source freshness, database-byte preservation, cancellation, child joins, malformed frames, and results over 64 MiB.

  • Targeted oxfmt, oxlint with the core configuration, and git diff --check passed. Independent review found no actionable issue.

  • A bounded encoder-only screen compared three fresh baseline/candidate process pairs per runtime, alternating order, with three warmups and GC before each cell. All 12 children joined and byte equivalence checks passed. Each paired wall cell improved at 1 KiB, 64 KiB and 8 MiB. For eight 8 MiB encodes, Node 26.7.0 baseline 12.671/12.161/12.890 ms became 9.766/9.191/8.944 ms; Bun 1.3.14 baseline 16.186/16.402/15.624 ms became 8.613/9.920/11.802 ms. This isolates encoding and does not measure complete auth acquisition.

  • The Node proof command took 76.92 s wall, 73.55 s user and 37.72 s system CPU, including a 33.6 s cold worker build. These are validation costs, not before/after performance results.

  • Testbox remained queued without a runner for five minutes, so that owned lease was stopped and the same focused proof ran in an independent frozen installation. No remote test result is claimed.

  • A local Bun 1.3.14 Vitest attempt failed before executing tests because its worker thread streams were unavailable. The repository's supported test runtime is the checksum-pinned Linux/X64 OpenClaw Bun 1.4.3 fork in .github/actions/setup-test-bun/action.yml; its exact-head CI proof is recorded below. No pool, timeout, assertions or runtime pin was weakened.

  • Exact-head CI attempt 1 passed for 42de5b9e85d5291f011f35b67f2a200996565845, testing merge 2079d6977c33596eda8a5efd070b7b4b910144f0 over cc618e0e7be6c02ab7f53e2487641fe2fe6b8f0c. The changed-file job actually ran the encoder cases on pinned Bun 1.4.3-canary.1+fc90aa4d9: 2/2 passed, 501 ms Vitest / 1.68 s wrapper. Session lifecycle ran on Node 24.19.0: 14/14 passed, 2.04 s / 3.35 s. Both exited 0 with completed worker-generation cleanup. Auth-worker and generic-transfer coverage comes from the separate focused Node proof above; those two files were not observed executing in this CI run.

  • Exact-head ClawSweeper review has no actionable finding or remaining rank-up move. Native squash landed as 76832258508202d51fc5d1b8c384d0f733ee0de4; live main reachability and both changed-file hashes were verified. No CI exception or merge bypass was used.

@vincentkoc vincentkoc self-assigned this Oct 2, 2026
@clawsweeper

clawsweeper Bot commented Oct 2, 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.

@vincentkoc
vincentkoc marked this pull request as ready for review October 2, 2026 04:28
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 2, 2026
@clawsweeper

clawsweeper Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 2, 2026, 12:33 AM ET / 04:33 UTC.

ClawSweeper review

What this changes

The PR removes an extra byte-buffer copy when encoding auth-profile transfer frames and adds tests for byte-range boundaries and independent string results.

Merge readiness

✅ Ready for maintainer review

Keep open: this is a useful, bounded optimization absent from current main, with no actionable correctness or security findings. The MEMBER-authored PR remains subject to normal landing gates.

Priority: P3
Reviewed head: 42de5b9e85d5291f011f35b67f2a200996565845

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused optimization with meaningful boundary coverage, restrained performance claims, and no actionable findings.
Proof confidence 🌊 off-meta tidepool Not applicable: The MEMBER-authored optimization is exempt from ordinary contributor proof. Reported auth-worker runs exercise the encoder through real child-process transfers, while encoder measurements address only encoding cost; no authority or stored-data change requires an additional proof gate.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The MEMBER-authored optimization is exempt from ordinary contributor proof. Reported auth-worker runs exercise the encoder through real child-process transfers, while encoder measurements address only encoding cost; no authority or stored-data change requires an additional proof gate.
Evidence reviewed 7 items Pinned introduced change: The introduced delta changes only synchronous frame encoding and adds a focused test. The verified test-merge comparison contains the same change, without removing base-branch work.
Producer and transport ownership: The transfer producer creates an owned Uint8Array for each bounded frame. The production worker encodes that frame synchronously before process.send; only the resulting string escapes. Receiver identity, sequence, size, canonical-base64, and completion checks remain unchanged.
Focused boundary coverage: Both Uint8Array and Buffer subviews use sentinel bytes outside the encoded range. Assertions check the independent expected base64 value, unchanged input, and string stability after the backing bytes are overwritten. No production seam, timers, processes, or mocks are added.
Findings None None.
Security None None.

How this fits together

OpenClaw reads auth profiles in a SQLite worker process and transfers serialized records to the requesting process. This encoder turns bounded binary frames into base64 strings for JSON IPC before the receiver reassembles the records.

flowchart LR
  A[Auth profile request] --> B[SQLite read worker]
  B --> C[Owned binary frames]
  C --> D[Exact-range base64 encoding]
  D --> E[JSON process transport]
  E --> F[Validated record reassembly]
  F --> G[Auth profile results]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +10, Tests +22. Total +32 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 11 1 +10
Tests 1 22 0 +22
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 33 1 +32

Review metrics

None.

Technical review

Best possible solution:

Retain the existing transfer owner and wire contract while avoiding the redundant copy during synchronous encoding.

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

Not applicable as a behavioral bug reproduction: source confirms the redundant copy on current main, and this read-only review ran no benchmark or tests.

Is this the best way to solve the issue?

Yes: encoding the exact backing-buffer range synchronously is a narrow optimization consistent with producer ownership and the existing wire format.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P3: This is an internal allocation optimization with unchanged user behavior and no demonstrated urgent failure.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The MEMBER-authored optimization is exempt from ordinary contributor proof. Reported auth-worker runs exercise the encoder through real child-process transfers, while encoder measurements address only encoding cost; no authority or stored-data change requires an additional proof gate.

Label justifications:

  • P3: This is an internal allocation optimization with unchanged user behavior and no demonstrated urgent failure.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The MEMBER-authored optimization is exempt from ordinary contributor proof. Reported auth-worker runs exercise the encoder through real child-process transfers, while encoder measurements address only encoding cost; no authority or stored-data change requires an additional proof gate.

Evidence

What I checked:

  • Pinned introduced change: The introduced delta changes only synchronous frame encoding and adds a focused test. The verified test-merge comparison contains the same change, without removing base-branch work. (src/infra/sqlite-readonly-auth-transfer.ts:12, 42de5b9e85d5)
  • Producer and transport ownership: The transfer producer creates an owned Uint8Array for each bounded frame. The production worker encodes that frame synchronously before process.send; only the resulting string escapes. Receiver identity, sequence, size, canonical-base64, and completion checks remain unchanged. (src/infra/sqlite-readonly-location.worker.ts:210, 42de5b9e85d5)
  • Focused boundary coverage: Both Uint8Array and Buffer subviews use sentinel bytes outside the encoded range. Assertions check the independent expected base64 value, unchanged input, and string stability after the backing bytes are overwritten. No production seam, timers, processes, or mocks are added. (src/infra/sqlite-readonly-auth-transfer.test.ts:4, 42de5b9e85d5)
  • Node Buffer contract: The official Node documentation confirms that Buffer.from(arrayBuffer, byteOffset, length) creates a view without copying memory and explains why explicit bounds matter: Node Buffer contract.
  • Current-main necessity and release check: Fetched main still uses Buffer.from(frame.bytes), which copies the frame before base64 encoding. The supplied latest-release commit has the same copying implementation, so this optimization is not already present in either inspected revision. (src/infra/sqlite-readonly-auth-transfer.ts:13, 5e81e62b87d1)
  • Reported validation and scope: The captured PR body reports 52 passing encoder, real auth-worker, session-lifecycle, and transfer tests on Node 24.16.0/macOS arm64, plus bounded encoder measurements with byte equivalence. It explicitly disclaims end-to-end latency and peak-memory gains and records the unsuccessful Bun attempt. These are author-reported results; this read-only review executed no target code. MEMBER association exempts ordinary contributor proof, and the patch changes neither authority nor stored-data contracts. (42de5b9e85d5)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@vincentkoc
vincentkoc merged commit 7683225 into main Oct 2, 2026
195 checks passed
@vincentkoc
vincentkoc deleted the perf/sqlite-auth-frame-base64-20261002 branch October 2, 2026 04:39
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

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: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS 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