Skip to content

perf(sessions): avoid cloning rebuilt actor projections - #170377

Merged
steipete merged 2 commits into
mainfrom
dbasync/workercpu
Oct 13, 2026
Merged

steipete merged 2 commits into
mainfrom
dbasync/workercpu

Conversation

@steipete

@steipete steipete commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Problem and change

Session actor commands deep-cloned the previous hot projection and transcript navigation, then immediately rebuilt the derived collections. Construct the projection from detached retained facts and fresh derived collections, and scan the actor-owned navigation directly.

The returned snapshot stays isolated from caller mutation. Rollback snapshots and committed-receipt copies remain. No SQL, command batching, cache, routing, schema, or authority contract changes.

Measurements

Base 0975d2455659f8715e47d483b63d871b4bc86ce5; measured production candidate ae98aca55e675f6d0fee85d73e4aa3a10688c8e0. Final head 5bd718f2c6b78987a0c5ae66164ebfa7a7062556 differs only by test typing. Three alternating same-host profile pairs, ten warmed turns each, under the local CPU gate. One separate unchanged shared-v2 SQL pair, ten warmed turns each, supplies causal counts.

Projection samples: 9.670 [7.862–22.349] → 5.293 [5.126–5.863] ms/turn (−4.377 ms, about 45%). The ranges do not overlap. This is a modest per-command optimization; the requested −80 ms summed-service target was not achieved.

Worker service and V8 sampling intervals are elapsed time, not scheduler CPU accounting. The 32-core macOS host started profile runs at 1-minute loads of roughly 93–132; the proof queue was fully occupied before admission, with observed load around 159. Admission waits, descheduling, native SQL, GC and profiler overhead inflate the totals. Aggregate latency/service regressions below are not evidence of a demonstrated CPU regression, and no end-to-end speedup is claimed. Direct projection samples are the primary signal. Native SQL is inside handler time, not additive.

SQL and requests

Warmed metric Base mean; per-turn median [min–max] Candidate mean; per-turn median [min–max] Mean Δ
All-thread turn-caused statements 186.8; 183.5 [180–201] 181.5; 181.5 [180–183] -5.3
Turn-caused requests 46; 46.5 [43–49] 44.5; 44.5 [43–46] -1.5
Turn-caused MAIN statements 0; 0 [0–0] 0; 0 [0–0] +0.0

Legacy warmed-window statements: 236.5 → 220.3; window requests: 63.3 → 58.2; MAIN: 0 → 0. Whole workload: 7,166 → 6,854 statements, 1,383 → 1,271 requests, and 849 → 848 MAIN statements (startup/one-shot work included). Causal coverage is complete. Every exact SQL shape’s warmed causal execution count stayed equal or fell; none increased. These observed reductions are not credited to the SQL-neutral patch.

Actor execute envelopes were 9.3 → 9.4/turn (one extra envelope in ten turns, with no increased SQL shape); transcript tasks 7.3 → 6.4; ordered entry reads 4.2 → 4.1; patch commit 4 → 4 and prepare 3 → 3; state-worker executes 6 → 6. Counts are disclosed as workload variation, not command batching or query savings.

T1 in the touched owner: 0 → 0. The implementation stays inside the existing database worker.

Profiled elapsed service and latency

Per-run mean service: 2,189.897 [1,426.663–8,107.257] → 3,717.561 [3,470.182–4,224.213] ms/turn. The broad overlap and unrelated-worker inflation make this unsuitable for attributing CPU cost. No aggregate-service saving is claimed.

Alternating run Warmed wall p50 / p95 (ms) Summed service p50 / p95 (ms)
1 M 2455 / 3664 2201.748 / 3340.781
2 C 3036.5 / 5191 2849.215 / 8547.453
3 M 1183.5 / 3206 817.302 / 5482.481
4 C 2556.5 / 5986 2367.718 / 6981.48
5 M 6987.5 / 15826 7302.051 / 22273.245
6 C 3810 / 6661 3729.772 / 7701.409
Worker, command, stage and top-10 flame summaries

Worker service (ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
sqlite_writer 1524.696 [868.797–5195.959] 2459.848 [2048.129–2463.201] +935.152
sessionTranscript 501.464 [462.615–2176.245] 1246.915 [944.341–1537.365] +745.451
memorySearch 24.771 [23.775–264.008] 127.335 [44.334–137.702] +102.564
stateRead 49.452 [41.215–83.963] 79.844 [38.411–92.662] +30.392
sqlite_read 56.236 [17.19–335.711] 37.758 [35.321–73.775] -18.478
providerPromptState 20.429 [2.922–31.047] 8.505 [3.763–13.498] -11.923
memoryIndex 20.325 [1.912–21.086] 7.06 [2.749–9.439] -13.265

Command service (ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
sessionTranscript:task 501.464 [462.615–2176.245] 1246.915 [944.341–1537.365] +745.451
sqlite_writer:database.domain.publish 413.752 [176.161–1344.976] 482.667 [416.623–595.012] +68.916
sqlite_writer:session.entry.patch.commit 203.135 [100.076–480.237] 316.444 [300.588–317.318] +113.309
sqlite_writer:session.actor.acceptInput 132.598 [87.427–431.69] 218.355 [114.699–281.019] +85.757
sqlite_writer:session.actor.appendTranscriptEvent 95.845 [87.611–405.858] 204.351 [200.291–219.19] +108.506
sqlite_writer:session.entry.read 113.386 [52.291–292.117] 179.184 [104.11–225.549] +65.798
sqlite_writer:transcripts 147.201 [59.74–424.539] 174.608 [153.61–251.617] +27.407
memorySearch:task 24.771 [23.775–264.008] 127.335 [44.334–137.702] +102.564
sqlite_writer:audit.writer.process 39.235 [27.669–170.884] 124.453 [87.477–126.39] +85.218
sqlite_writer:session.actor.deliverySettled 50.363 [46.358–224.507] 109.377 [102.455–154.791] +59.014
sqlite_writer:placementTurns 44.417 [38.048–159.034] 105.981 [92.613–118.701] +61.564
sqlite_writer:session.actor.completeTurn 44.667 [20.79–97.309] 81.954 [17.33–127.319] +37.287
sqlite_writer:session.entry.patch.prepare 73.902 [65.779–228.665] 54.838 [39.192–103.283] -19.064
sqlite_writer:session.actor.read 57.947 [31.753–203.645] 67.127 [66.326–74.06] +9.180
sqlite_writer:session.lifecycle.project 42.084 [22.52–69.536] 58.942 [47.974–64.119] +16.858
sqlite_writer:session.actor.adoptRun 46.902 [31.428–50.901] 57.761 [51.074–76.589] +10.859
sqlite_read:auth_profiles 56.236 [17.19–335.711] 37.758 [35.321–73.775] -18.478
sqlite_writer:usageCache 11.081 [9.565–45.137] 38.636 [6.417–66.96] +27.554
stateRead:acpSessions 33.502 [20.108–43.197] 9.468 [6.576–19.979] -24.034
providerPromptState:task 20.429 [2.922–31.047] 8.505 [3.763–13.498] -11.923
memoryIndex:task 20.325 [1.912–21.086] 7.06 [2.749–9.439] -13.265
sqlite_writer:cron 4.563 [0.423–40.451] 13.275 [12.632–19.981] +8.712
stateRead:execute 4.911 [4.016–27.161] 13.204 [10.132–61.135] +8.293
stateRead:subagents 2.494 [2.239–5.786] 10.411 [2.75–15.43] +7.917
sqlite_writer:deliveryQueue 0 [0–12.712] 3.848 [0.534–10.174] +3.848
stateRead:channelIngress 0 [0–1.783] 3.688 [0.379–11.68] +3.688
stateRead:workerPlacements 0 [0–28.4] 3.629 [1.971–16.627] +3.629
stateRead:workers 0 [0–0.257] 3.024 [0.401–12.469] +3.024
sqlite_writer:database.domain.close 2.376 [0–25.359] 0 [0–0] -2.376
sqlite_writer:bootstrap 0 [0–0.394] 2.353 [1.449–2.995] +2.353
sqlite_writer:sessionUpstream 1.352 [0.31–3.993] 0.796 [0.389–1.08] -0.556
stateRead:agentDatabaseDeletion 0.308 [0–0.468] 1.256 [0.502–6.207] +0.947
sqlite_writer:database.domain.bind 0.907 [0–1.878] 0 [0–0] -0.907
sqlite_writer:workerPlacements 0 [0–12.121] 0.896 [0.345–18.442] +0.896
sqlite_writer:secrets 0.815 [0.574–5.925] 0 [0–0] -0.815
sqlite_writer:domain_execute 0.763 [0–65.806] 0 [0–0] -0.763
sqlite_writer:execute 0.573 [0–131.348] 0.7 [0–3.921] +0.127
sqlite_writer:database.prepareWrite 0.464 [0–62.483] 0 [0–0] -0.464
sqlite_writer:close 0 [0–211.093] 0 [0–0] +0.000

Writer stages (ms per warmed turn; decode includes admission preparation)

Metric Base median [min–max] Candidate median [min–max] Δ
handler 885.852 [565.797–3173.744] 1484.595 [1357.543–1672.29] +598.743
inboundAndDelivery 236.377 [113.872–836.287] 319.832 [236.227–441.558] +83.455
workerDecodeAdmissionPreparation 178.713 [33.684–446.955] 146.917 [133.287–156.866] -31.796
outboundAndDispatch 139.843 [114.991–451.207] 177.347 [171.515–274.301] +37.504
hostBeforePost 58.569 [26.055–178.565] 73.85 [73.628–102.613] +15.281
resultSerialization 10.012 [5.137–15.395] 9.077 [7.357–11.682] -0.935
queue 1.722 [1.648–2.515] 1.974 [1.406–15.193] +0.252
replyPreparation 0.463 [0.407–2.403] 1.277 [0.854–3.271] +0.814

Top 10 active self-time functions: sqlite-store.worker.js (sampled ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
exec @ 70.323 [55.955–139.029] 104.117 [40.458–427.179] +33.794
structuredClone @ node:internal/worker/js_transferable 66.208 [31.444–112.387] 58.81 [29.882–61.806] -7.398
get @ 24.986 [19.779–76.746] 47.117 [29.019–144.018] +22.131
(garbage collector) @ 34.699 [29.197–37.954] 44.186 [37.99–91.783] +9.486
(program) @ 20.167 [9.977–51.511] 25.379 [18.021–29.929] +5.213
all @ 8.391 [6.674–27.261] 17.413 [7.969–18.739] +9.021
processTimers @ node:internal/timers 12.097 [1.291–17.057] 4.019 [2.57–13.181] -8.078
serialize @ node:v8 9.865 [8.777–12.377] 11.944 [5.946–15.105] +2.079
realpath @ 3.172 [1.563–34.648] 7.299 [2.665–17.37] +4.127
MessagePort.postMessage @ perf-observer.cjs 1.435 [1.411–1.593] 5.605 [2.532–6.144] +4.170

Top 10 active self-time functions: session-transcript.worker.js (sampled ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
read @ 4.786 [0–10.543] 88.675 [78.989–94.311] +83.888
open @ 8.216 [0–26.043] 73.642 [72.157–127.847] +65.426
internalModuleStat @ 6.968 [0–35.189] 60.617 [47.891–68.389] +53.649
(program) @ 40.37 [19.484–176.584] 47.086 [36.139–99.179] +6.715
(garbage collector) @ 30.28 [20.974–158.755] 45.786 [33.934–97.232] +15.506
processTimers @ node:internal/timers 27.589 [11.552–35.361] 17.712 [9.373–23.465] -9.877
compileSourceTextModule @ node:internal/modules/esm/utils 3.966 [0–20.48] 23.557 [6.122–54.653] +19.591
listOnTimeout @ node:internal/timers 10.862 [8.283–20.119] 12.81 [7.155–25.132] +1.948
getLibuvNow @ 6.709 [1.382–6.781] 8.232 [2.414–9.493] +1.523
defaultLoadSync @ node:internal/modules/esm/load 0.122 [0–0.438] 6.392 [3.747–39.888] +6.270

Top 10 active self-time functions: openclaw-state-read.worker.js (sampled ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
(program) @ 6.152 [3.498–11.494] 6.178 [4.272–7.184] +0.026
processTimers @ node:internal/timers 1.971 [1.559–3.654] 5.021 [2.077–8.151] +3.049
listOnTimeout @ node:internal/timers 3.158 [2.043–4.146] 4.284 [1.755–5.171] +1.126
realpath @ 0.304 [0.074–3.108] 1.379 [0.448–2.532] +1.075
getLibuvNow @ 0.753 [0.511–1.196] 0.965 [0.162–1.409] +0.212
stat @ 0.727 [0–1.324] 0.099 [0–0.115] -0.628
pushAsyncContext @ node:internal/async_hooks 0.517 [0.221–0.67] 0.658 [0–2.224] +0.142
emitBeforeScript @ node:internal/async_hooks 0.512 [0.237–5.826] 0.27 [0–0.556] -0.242
(garbage collector) @ 0.407 [0–1.089] 0.243 [0.133–1.006] -0.165
percolateDown @ node:internal/priority_queue 0.174 [0.124–2.744] 0.371 [0–0.637] +0.198

Top 10 active self-time functions: memory-search.worker.js (sampled ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
(garbage collector) @ 2.103 [0.628–32.182] 8.586 [6.625–11.026] +6.483
(program) @ 8.097 [2.55–14.955] 2.542 [1.7–8.934] -5.554
processTimers @ node:internal/timers 3.237 [1.465–7.141] 1.64 [0.685–2.128] -1.597
listOnTimeout @ node:internal/timers 0.867 [0.555–5.254] 0.774 [0.273–2.077] -0.093
pushAsyncContext @ node:internal/async_hooks 0.584 [0.18–0.931] 0 [0–0.12] -0.584
getLibuvNow @ 0.263 [0–0.29] 0.549 [0.152–1.188] +0.285
popAsyncContext @ node:internal/async_hooks 0.447 [0–0.491] 0.272 [0.187–0.382] -0.175
insert @ node:internal/timers 0.361 [0–0.513] 0.346 [0–0.509] -0.015
Database @ 0 [0–4.384] 0.319 [0–4.489] +0.319
realpath @ 0.284 [0–0.433] 0 [0–0] -0.284

Top 10 active self-time functions: memory-index.worker.js (sampled ms per warmed turn)

Metric Base median [min–max] Candidate median [min–max] Δ
(program) @ 2.012 [1.796–10.771] 5.549 [1.682–8.934] +3.537
processTimers @ node:internal/timers 0.766 [0.722–1.681] 3.517 [0.584–4.794] +2.751
(garbage collector) @ 2.064 [1.837–3.68] 3.455 [0.15–5.702] +1.391
listOnTimeout @ node:internal/timers 0.311 [0.177–0.425] 2.752 [1.102–3.02] +2.441
emitBeforeScript @ node:internal/async_hooks 0.358 [0–0.591] 0.455 [0.296–0.801] +0.097
popAsyncContext @ node:internal/async_hooks 0.403 [0–0.487] 0.318 [0.297–0.835] -0.085
compareTimersLists @ node:internal/timers 0.165 [0–0.238] 0.38 [0–0.537] +0.215
getLibuvNow @ 0.363 [0.25–0.785] 0 [0–0.361] -0.363
emitAfterScript @ node:internal/async_hooks 0.151 [0–0.196] 0.309 [0–1.477] +0.158
pushAsyncContext @ node:internal/async_hooks 0.258 [0.233–0.536] 0.219 [0.141–0.245] -0.038

Removed work and boundaries

  • Removed the deep copy of derived projection collections that are immediately replaced. Accepted edge case: none added; fresh collections retain caller isolation.
  • Removed the deep copy before the synchronous read-only navigation scan. Accepted edge case: none added; the scanner reads actor-owned data and builds separate output.
  • No security, authority, or corruption checks were removed. The rollback copy is retained: it sampled 11.572 ms/turn in the initial corrected baseline and protects real rollback isolation. Removing it needs an owner/copy-on-write redesign. Query compilation sampled only 0.688 ms/turn; no query cache is warranted by this evidence.

Verification

  • Production-path actor regression covers hydration, writes, rollback, rehydration and caller mutation of retained/nested facts and derived transcript collections. Both focused tests passed: pnpm test src/config/sessions/session-actor-kernel.test.ts --maxWorkers=1 (291.11 s Vitest; 393.82 s wrapper; 4.483 s test bodies). First-use worker compilation dominates.
  • Exact-head CI passed: selected tests, production/test types, lint, source/export contracts, Madge and database/baseline ratchets. Full CI span: 447 s. The first run found only strict typing in the new mutation test; fixed in the test-only second commit.
  • Knip production, full-tree and script export scans all passed with zero entries (pnpm deadcode:exports, 447.4 s).
  • Two Codex autoreview rounds completed scoped-clean at P0/P1. Local formatting/lint and git diff --check passed. SDK diff and update cell are not applicable to this scope.
  • Six complete profiler fixtures and both complete SQL fixtures used isolated synthetic state; all children exited. A preliminary file-polling observer capture and one candidate startup timeout were rejected and excluded.

LOC: +24/−5 production; +33 tests. No changelog edit: release-note context is this PR.

@clawsweeper

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

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Oct 12, 2026
@clawsweeper

clawsweeper Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR selectively clones retained session facts and rebuilds derived projections without copying transcript navigation.

Example: Project a session containing events named user and assistant.

  • Before: Copy the previous derived collections and navigation, then rebuild the two-event projection.
  • After: Build the same ordered projection directly while keeping returned facts detached from retained state.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The focused patch appears correct and has useful isolation coverage, with limited evidence of its practical performance benefit.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; supplied validation exercises the production projection owner through a database fixture, while the practical runtime performance benefit remains unmeasured.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Repeated session commands spend worker CPU copying derived facts that are immediately rebuilt.
Reason: The maintainer-authored direction removes identifiable redundant work while preserving existing behavior and ownership.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR removes redundant projection copying that current main still performs, with no actionable correctness or security finding.

Priority: P2
Reviewed head: 5bd718f2c6b78987a0c5ae66164ebfa7a7062556

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The promised comparable production-worker profiles and warmed latency results remain absent.
Agent review details

How this fits together

The session database worker turns retained actor state into snapshots for command receipts and session readers.

flowchart LR
  A[Session command] --> B[Database worker]
  B --> C[Retained actor state]
  C --> D[Hot state projection]
  D --> E[Detached receipt]
  E --> F[Session readers]
Loading

Technical review

Best possible solution:

Keep selective cloning at the existing projection owner while preserving rollback and committed-receipt isolation.

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

Source confirms that current main copies collections immediately replaced during projection; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch removes redundant work at its existing owner, preserves nested output isolation, and adds no competing cache or state authority.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Detached actor projections and transcript navigation keeps the original intent (d5173cc: Actor commands require isolated working state and detached postimages for publication and recovery.)
  • Board-presence projection keeps the original intent (2d7b1c1: Reuse committed actor-owned Board facts instead of rereading them.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit d5173cc adds src/config/sessions/session-actor-hydration.worker.ts:413 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: d5173ccc77b9; files: src/config/sessions/session-actor-hydration.worker.ts)

PR surface

Source +19, Tests +33. Total +52 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 24 5 +19
Tests 1 33 0 +33
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 57 5 +52

Labels

Label changes:

No label changes.

Label justifications:

  • P2: A focused worker CPU optimization with limited scope and no demonstrated urgent user-facing 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.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Optionally add the promised comparable worker profiles and warmed latency results.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-12T23:25:45.779Z sha ae98aca :: needs maintainer review before merge. :: none

Reviewed October 12, 2026, 7:50 PM ET / 23:50 UTC (Revision 2).

@steipete
steipete merged commit 1650b92 into main Oct 13, 2026
107 of 108 checks passed
@steipete
steipete deleted the dbasync/workercpu branch October 13, 2026 00:42
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
)

Build session actor hot projections from detached retained facts and fresh derived collections, and scan actor-owned navigation without a redundant copy. Preserve rollback and receipt isolation, with a production-path caller-mutation regression.

Three alternating profile pairs reduced projection samples from 9.67 to 5.29 ms per warmed turn; aggregate service remained too noisy to claim a speedup. The shared SQL pair showed no statement or request increase, and warmed MAIN statements remained zero.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S 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