Skip to content

improve(sessions): reduce memory for lightweight transcript reads - #198512

Merged
vincentkoc merged 4 commits into
mainfrom
perf/transcript-worker-demand-loading-20261113
Nov 14, 2026
Merged

vincentkoc merged 4 commits into
mainfrom
perf/transcript-worker-demand-loading-20261113

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Lightweight transcript reads load the full history presentation graph when a foreground worker starts, even with Control UI disabled.

User Impact

Lightweight workers retain about 10 MiB less heap and finish their first lookup sooner. History presentation remains available on demand, and enabled Control UI still uses explicit prewarming. A worker that has not prepared presentation pays the deferred import cost on its first presentation request (about 160 ms in this benchmark). Fully prepared history workers retain about the same heap.

Why This Change Was Made

Move the four presentation imports into the existing prewarm request and remove eager foreground-lane startup loading. Let Gateway prewarming reach an already-started lightweight worker, using the existing per-database prewarm deduplication. Separate worker lanes, database admission, and read behavior remain intact. No configuration options are added.

Evidence

Acceptance rule: at least 20% less retained heap for a fresh foreground worker performing a lightweight read, with no 20% regression in total explicit-prewarm latency. Five interleaved baseline/candidate pairs, built runtime, Node 26.13.0, one Blacksmith host with 4 CPUs and approximately 16 GiB RAM:

Workload / metric Before After
Lightweight read: retained heap after GC 29.95 MiB 19.98 MiB
Lightweight read: evaluated modules 1,253 410
Lightweight read: spawn to reply, median / p95 272.25 / 308.73 ms 174.16 / 213.22 ms
Explicit prewarm: spawn to reply, median / p95 293.98 / 311.94 ms 272.53 / 308.01 ms
Explicit prewarm: retained heap after GC 30.01 MiB 29.97 MiB
Explicit prewarm: total allocations 184.30 MiB 192.07 MiB

The import cost moves from startup into the first presentation request: measured request-only prewarm median 5.17 → 164.91 ms. The heap result is per-worker retained JavaScript heap, not a measurement of whole-Gateway RSS.

Reproduction: scripts/perf/worker-spawn-measure.mjs --built --build-dir <candidate> --compare-build-dir <baseline> --worker session-transcript --prewarm-history --transcript-task <sqlite-target|prewarm> --samples 5, run with Node 26.13.0 through --import ./scripts/tsx.mjs.

  • Both runtime builds passed.
  • Import-timing and existing-worker prewarm regressions fail on their original source and pass with the fix.
  • Focused history integration, Gateway prewarm/startup, pool, and lane lifecycle suites passed.
  • Targeted lint/format and pnpm tsgo:core passed.
  • Blacksmith validation run; task-owned testbox stopped after proof.

Local ClawSweeper 0.3.3 completed a full committed-range review of 91046f7bbbb8..602c92076abe with no priority filter and no code findings. It identified the deliberate first-presentation latency tradeoff and requested additional live Gateway evidence covering a second foreground worker. The maintainer accepted demand loading after the measured tradeoff was presented. The additional probe now passes through the built Gateway on Node 26.13.0: Control UI disabled and enabled, each with a lightweight sessions.list followed by two waves of eight concurrent chat.history requests. Both foreground workers served history in each case; all 32 responses succeeded with nonempty seeded messages, and cold/warm message digests matched. The probe used isolated synthetic state and exercised natural request-driven loading (no prewarm tasks were observed). These instrumented functional timings are not a before/after Gateway latency benchmark. The previously queued extra Testbox was stopped; this Gateway proof ran locally in the native PR checkout.

The final follow-up commits document mock isolation for the existing Gateway prewarm test and remove its obsolete baseline entry. The targeted mock-export ratchet passes; production code is unchanged from the reviewed and measured head.

One unrelated existing test (session-message-events.test.ts, same-clock avatar revisions) failed at subscribeMessages on both baseline and candidate. Its root cause remains outside this change; the focused worker/Gateway suites and real Gateway history proof pass.

Inherited CI failure

CI job fails four native Talk cancellation variants: a new consultation after cancellation returns the fallback text instead of the expected completion. The worker/history tests in that shard pass. Reproduced all four identical expected/actual failures locally with both candidate and original worker/prewarm production source, using only main's test-fixture correction that asserts the terminal response rather than waiting forever for correct output. The unchanged original fixture times out on both versions. Temporary diagnostic changes were restored. An independent source review confirmed the inherited attribution; the voice defect remains outside this memory change. CI is retained as failed and landing uses the native inherited-failure exception, subject to remaining checks/security.

Exercise foreground presentation prewarming and report post-GC retained heap plus p95 timings. Sample heap before the debugger census to exclude inventory allocations.

Validated with five paired built-worker samples, targeted lint and formatting, and the core typecheck.
Move presentation imports from foreground worker startup to explicit prewarm requests. Let Gateway warm an existing lightweight reader through the resource-owned prewarm deduplication. Keep separate worker lanes and database admission unchanged.

Built Node 26.13.0, five interleaved pairs: lightweight retained heap 31,399,848 -> 20,946,464 bytes; evaluated modules 1253 -> 410; spawn through first reply median 272.25 -> 174.16 ms (p95 308.73 -> 213.22 ms). Explicit prewarm median 293.98 -> 272.53 ms (p95 311.94 -> 308.01 ms); full presentation retains about 30 MiB in either version. The first presentation request pays deferred imports; this is worker heap evidence, not whole-process RSS.

Validation: regression failures on original source, green import/history integration and prewarm/pool/lane lifecycle suites, both runtime builds, targeted lint/format, core typecheck, and independent source review.
@vincentkoc vincentkoc self-assigned this Nov 14, 2026
@clawsweeper

clawsweeper Bot commented Nov 14, 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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime scripts Repository scripts size: S labels Nov 14, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review November 14, 2026 00:18
@vincentkoc
vincentkoc enabled auto-merge (squash) November 14, 2026 00:38
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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 Nov 14, 2026
@clawsweeper

clawsweeper Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR defers transcript presentation imports until explicit prewarming or a history request needs them.

Example: A fresh foreground transcript worker resolves a lightweight SQLite target.

  • Before: The reported benchmark retains 29.95 MiB of heap and evaluates 1,253 modules.
  • After: It retains 19.98 MiB and evaluates 410 modules; later presentation requests load the deferred code.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, measured optimization with production-entry-point evidence and no identified patch defect.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): Supplied built-worker measurements cover retained heap and explicit-prewarm latency, and the built-Gateway results cover lightweight reads followed by concurrent cold and warm history reads through both foreground workers.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Lightweight session reads pay startup time and retained memory for history presentation code they do not need.
Reason: The measured reduction exceeds the stated acceptance rule, and the maintainer-authored direction explicitly accepts deferred first-presentation cost.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR delivers a measured memory reduction with a documented latency tradeoff, and no actionable patch defect was found.

Priority: P2
Reviewed head: d4098fded005399f2cdc734a9cd2716ed0bf9b94

Before merge

None.

Findings

None.

Agent review details

How this fits together

Gateway session operations dispatch reads through database-owned worker lanes; lightweight requests resolve metadata, while history requests load presentation code and return transcript pages.

flowchart TD
  A[Gateway session requests] --> B[Database resource owner]
  B --> C[Foreground worker pool]
  C --> D[Lightweight metadata reads]
  C --> E[History presentation imports]
  F[Explicit Gateway prewarm] --> B
  E --> G[Transcript page response]
Loading

Technical review

Best possible solution:

Retain demand loading and the existing explicit-prewarm owner with the documented first-presentation latency tradeoff.

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

Current-main source eagerly imports presentation code for every foreground worker, and the supplied paired benchmark measures the resulting overhead; this review ran no target code.

Is this the best way to solve the issue?

The patch is a focused deferral using existing owners, preserves request-time history loading, and adds regressions for import timing and prewarming an already-started lightweight worker.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Foreground presentation imports and pool startup option changes intended behavior with a stated reason (f20e7db: Prepare every actual foreground worker because a pool-level prewarm may select only one slot.)
  • Gateway cold-only prewarm shortcut changes intended behavior with a stated reason (79798ed: Avoid redundant store discovery and possible sibling-worker startup when recovery already activated fully prepared readers.)
  • Explicit history-worker prewarm request keeps the original intent (9ac4735: Move cold history preparation ahead of the first transcript read through normal database custody.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 9ac4735 adds src/gateway/server-history-prewarm.ts:29 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9ac4735581d6; files: src/gateway/server-history-prewarm.ts)
  • Vincent Koc: Raw commit 625945b adds src/config/sessions/session-transcript.worker.ts:29 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 625945b7ae01; files: src/config/sessions/session-transcript.worker.ts)

PR surface

Source -16, Tests +13, Other +31. Total +28 across 9 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 4 10 26 -16
Tests 3 63 50 +13
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 32 1 +31
Total 9 105 77 +28

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Benchmark-only options 2 flags --prewarm-history and --transcript-task select reproducible measurement scenarios; neither changes Gateway configuration.

Labels

Label changes:

  • add P2: The measured per-worker memory and lightweight-read improvements have useful but limited runtime impact.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit 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:

  • P2: The measured per-worker memory and lightweight-read improvements have useful but limited runtime impact.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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 November 13, 2026, 7:50 PM ET / November 14, 2026, 00:50 UTC.

@vincentkoc
vincentkoc disabled auto-merge November 14, 2026 00:57
@vincentkoc
vincentkoc merged commit 07fcef0 into main Nov 14, 2026
150 of 153 checks passed
@vincentkoc
vincentkoc deleted the perf/transcript-worker-demand-loading-20261113 branch November 14, 2026 01:06
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 14, 2026
…enclaw#198512)

* test(perf): measure foreground transcript worker memory

Exercise foreground presentation prewarming and report post-GC retained heap plus p95 timings. Sample heap before the debugger census to exclude inventory allocations.

Validated with five paired built-worker samples, targeted lint and formatting, and the core typecheck.

* perf(sessions): load history presentation on demand

Move presentation imports from foreground worker startup to explicit prewarm requests. Let Gateway warm an existing lightweight reader through the resource-owned prewarm deduplication. Keep separate worker lanes and database admission unchanged.

Built Node 26.13.0, five interleaved pairs: lightweight retained heap 31,399,848 -> 20,946,464 bytes; evaluated modules 1253 -> 410; spawn through first reply median 272.25 -> 174.16 ms (p95 308.73 -> 213.22 ms). Explicit prewarm median 293.98 -> 272.53 ms (p95 311.94 -> 308.01 ms); full presentation retains about 30 MiB in either version. The first presentation request pays deferred imports; this is worker heap evidence, not whole-process RSS.

Validation: regression failures on original source, green import/history integration and prewarm/pool/lane lifecycle suites, both runtime builds, targeted lint/format, core typecheck, and independent source review.

* test(sessions): explain prewarm mock isolation

* test(sessions): prune documented mock isolation baseline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts 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