Repository navigation
perf(gateway): serve profile avatars without blocking SQLite reads - #158445
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 25, 2026, 11:20 PM ET / September 26, 2026, 03:20 UTC (Revision 6). ClawSweeper reviewWhat this changesMoves authenticated Control UI avatar reads to a shared SQLite worker, avoids fetching image bytes for HEAD and 304 responses, and shares pending reads for the same profile. Merge readiness✅ Ready for maintainer review Keep open: current main still has the synchronous avatar path, and this PR remains a distinct, coherent candidate. The earlier shared-reader capacity concern has an explicit as-is disposition in the PR discussion. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway serves profile images requested by Control UI browsers after checking read access. It reads saved avatars from OpenClaw's SQLite profile store and returns an image, a conditional response, or a host-photo or Gravatar fallback. flowchart LR
A[Control UI image request] --> B[Gateway access check]
B --> C[Avatar reader]
C --> D[SQLite read worker]
D --> E{Saved avatar available?}
E -->|Yes| F[Image or conditional response]
E -->|No| G[Host photo or Gravatar]
G --> H[Image or not found]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +305, Tests +230. Total +535 across 21 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep the off-thread avatar path and its authorization and representation checks; address sustained distinct-profile overload through the shared reader's capacity owner if demand warrants it. Do we have a high-confidence way to reproduce the issue? Yes. Current main calls the synchronous full-avatar getter in the authenticated HTTP handler, and the PR gives a concrete concurrent-request benchmark; this read-only review did not rerun it. Is this the best way to solve the issue? Yes. The existing shared SQLite reader owns database work, while the Gateway retains authorization and HTTP response decisions; the PR adds no competing store or setting. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 8ccd3f581c1e. 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 (5 earlier review cycles)
|
Break the type-import cycle between the shared read-worker contract and the profile database implementation. Preserve avatar runtime behavior and keep one leaf definition for the request, reply, and metadata types.
Retain both independent multi-account test imports and preserve the reviewed avatar delta. The stable context-free patch ID is unchanged; focused auth and multi-account tests, core types, and the cycle check pass on the merged candidate.
|
Merged via squash.
|
…penclaw#158445) * perf(gateway): prepare profile avatars outside the main thread * refactor(state): keep avatar byte selection private * fix(state): keep avatar read contracts in leaf modules Break the type-import cycle between the shared read-worker contract and the profile database implementation. Preserve avatar runtime behavior and keep one leaf definition for the request, reply, and metadata types.
What Problem This Solves
Concurrent Control UI profile-avatar requests synchronously read and copy SQLite BLOBs on the Gateway thread, even for HEAD and unchanged responses.
User Impact
Saved-avatar reads run in the existing shared-state read worker. HEAD and 304 responses fetch no image bytes, and overlapping requests share unsettled reads. Avatar bytes, MIME types, ETags, merge behavior, authentication, and cache headers remain unchanged. There is no new configuration, public protocol, SQLite schema, or update migration.
Why This Change Was Made
The avatar owner prepares merge-aware metadata before requesting bytes, retains the selected physical database through refreshes, and checks the original alias and representation again when materializing an image. The existing profile publication revision fences local catalog changes; worker reads continue to observe foreign commits. Tailscale inspection shares the metadata query. The synchronous getter and duplicate inspection are removed.
A first worker-only cutover overloaded the existing 128-task reader limit during the 300-request proof. Pending-only coalescing fixes that failure without raising the limit or retaining completed avatar results. Explicit database/environment and maintenance/schema scopes keep their own admission. Current request authorization is checked after asynchronous preparation and before publishing responses.
Evidence
Actual HTTP handler, three synthetic uploaded profiles, 300 simultaneous calls, seven warmed rounds, Node 26.9.0/macOS arm64:
All 12,600 measured responses passed status, ETag, Content-Length, and body-length checks. Every final burst performed zero main-thread SQLite/BLOB reads in the avatar path and exactly three worker inspections. HEAD/304 transferred zero avatar bytes. GET transferred 1.5 MiB from workers while delivering 150 MiB across 300 responses, instead of materializing 300 BLOBs on the main thread.
The same 32 KiB cases reduced sentinel delays from 19.4–25.0 ms to 3.7–6.2 ms. Small-image GET completion increased 19.374 → 26.661 ms. These are synthetic handler measurements on a heavily shared host, without network transport. A 19.05-second source-worker startup outlier is retained in the samples; warm medians are not cold-start or deployed-latency claims.
node scripts/run-vitest.mjs src/gateway/user-profiles-http.test.ts src/gateway/user-profiles-http.auth.test.ts src/gateway/user-profiles-http.gravatar.test.ts src/state/user-profiles.avatar-lifetime.test.ts --maxWorkers=1.node scripts/run-vitest.mjs src/gateway/user-profiles-http.test.ts src/gateway/user-profiles-http.auth.test.ts --maxWorkers=1. Isolated databases now share suite-lifetime workers. The 300-request regression took 1.917 s; replacement/merge races took 1.127/1.134 s. Worker compilation dominates these wall times. CI seconds are pending.Landing validation
Exact-head CI run 36206083575 found a type-import cycle: the shared read-worker contract imported avatar types through the database implementation. The run was cancelled after that failure, and its CI gate reflected the architecture failure. Commit
9a9a492c3e8473e5861d82476e0c7c7d70ea86damoves the contracts into the existing leaf type modules without changing runtime behavior.node --import ./scripts/tsx.mjs scripts/check-madge-import-cycles.ts: 0 cycles, 120.36 s wall.tsgo: passed, 45.52 s wall.node scripts/run-vitest.mjs src/state/user-profiles.avatar-worker.test.ts: 17 passed, 1157.59 s total wall including source-worker compilation and cleanup; Vitest duration 644.54 s.git diff --check: passed. Independent Codex review of the repair: scoped-clean through P2, no actionable findings.Review disposition: shared reader saturation
ClawSweeper's latest review identifies a real capacity limit: concurrent requests for distinct profiles cannot share a pending read, so enough outstanding requests can reach the shared reader's 128-task admission bound and receive
profile_lookup_failedHTTP 500. The reported burst proof covers coalescing across a small set of profiles; it does not establish success for arbitrary distinct-profile saturation.For this requested as-is landing, the shared admission policy is retained and this limitation is recorded as a follow-up, rather than adding another queue owned by the avatar adapter.
src/state/openclaw-state-read-worker.tsselects the shared task limit, andsrc/infra/worker-task-pool-core.tsdeliberately rejects excess tasks; the worker-pool tests protect that capacity contract. Queueing or overload-response improvements should be addressed through those existing owners. This disposition does not claim that saturation is fixed or that a distinct-profile burst passed.Main-merge validation
Native merge admission stopped before dispatch when advancing main introduced an import conflict in
src/state/user-profiles-multi-account.test.ts. Merge commit4fca52f4a968d4f37b5a35f3141e692deb9e176cincorporates main at37259b7cab6b1816211d381200848048a05f197dand keeps both independent imports. The stable context-free patch ID before and after the merge is identical (10d2c4debf9cf97e4f0e88f1a228ac31baa177de), preserving the independently reviewed PR delta.tsgo: passed, 40.21 s wall.