Skip to content

perf(memory): reduce cold first-turn retrieval waits - #158563

Merged
steipete merged 1 commit into
mainfrom
perf/l41-memory-retrieval-prewarm
Sep 26, 2026
Merged

steipete merged 1 commit into
mainfrom
perf/l41-memory-retrieval-prewarm

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

The first Control UI prompt can wait for cold memory retrieval initialization before contacting the model. A traced run spent 1.574 seconds in its first project-memory worker task, versus 14.5/5.2 ms on later tasks.

User Impact

After idle warmup, the first project-memory retrieval uses an initialized worker and returns the same eligible memory entries. Readiness and retrieval semantics are unchanged. An immediate request, or one after the worker's normal idle retirement, can still pay cold initialization.

Why This Change Was Made

The dominant cold cost is worker module initialization: a built probe that could not open any database took 919 ms on its first request, including only 12.8 ms to reach worker online, and 0.79 ms on repeat. Curated recall does not initialize an embedding provider.

Memory Core now exposes a lazy worker warmup entry point. One registration in the existing post-ready idle scheduler calls it only for active Memory Core. Its private acknowledgment loads the retrieval code without opening an index, creating a manager, indexing files, or starting a provider. The existing pool owns admission, reuse, retirement, and cleanup; normal requests remain authoritative. No protocol, configuration, database schema, or upgrade behavior changes.

Related: #158555 changes the same idle scheduler and test fixture, but does not warm memory. The second PR landed will need a rebase retaining both sets of registrations and adopting its getConfig test fixture. This PR remains based on main rather than stacking the two changes.

Evidence

Five alternating cold/warmed pairs on the same Blacksmith Testbox, using the built candidate and an isolated seeded project-memory entry:

First retrieval Cold After worker warmup
Median wall time 169.96 ms 4.74 ms
Median main-thread CPU 4.70 ms 1.14 ms
Median process RSS 378.79 MiB 378.30 MiB
Retrieval workers 1 1

All ten returned identical entries. Warmup itself took a median 172.08 ms with 5.31 ms of main-thread CPU, paid before retrieval. This is component proof (35.9× faster first retrieval), not a full concurrent-client latency claim. Five interleaved source-level pairs on the busy development host independently showed 10.13 s → 39.96 ms; those include TypeScript loading.

  • node scripts/run-vitest.mjs src/gateway/server-startup-handler-prewarm.test.ts extensions/memory-core/src/memory/manager-retrieval-offthread.test.ts --maxWorkers=1: both files passed, 30.92 s command wall; file durations 1.13 s and 26.97 s. The changed idle-owner file has eight passing cases.
  • Replacing only the registration module with the original version makes the new active-Memory-Core case fail for missing warmup (2.02 s command wall); the candidate module was restored before checks.
  • pnpm build: passed, 6m04.8s for the final committed candidate. An earlier dirty-candidate build also passed in 9m19.4s. The build reports an untouched UI startup-size warning (363.0 KiB gzip versus 362.6 KiB); no budget was changed.
  • node scripts/check-changed.mjs -- <the seven changed files>: passed in 1,880.57 s (31m20.6s), including core/extension production and test types, lint, boundaries, dead exports, and storage guards.
  • Diff-scoped deslop completed; independent Codex autoreview is scoped-clean through P2, with no actionable findings.
  • Production +32/−1 (net +31); tests +46/−19 (net +27); docs +5. The production growth is the lazy public boundary, database-free acknowledgment, and idle registration; there is no superseded retrieval path to remove.

Remote proof used provider blacksmith-testbox, lease tbx_01m3djqtv2fgeeymw2bv4g3ttw, hydration run. Initial missing dependencies were installed. Unpublished-commit sync fallback timed out; publishing the reviewed branch let Testbox fetch the exact commit and complete validation. A local retrieval-suite attempt was stopped during slow worker-fixture preparation; the remote run supplies its passing result. The local idle-owner file independently passed all eight cases in 26.52 s. Hosted PR CI is pending.

Move cold retrieval module initialization into the existing idle lifecycle without opening databases or initializing embedding providers. Keep request-time retrieval and worker retirement authoritative.
@clawsweeper

clawsweeper Bot commented Sep 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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime extensions: memory-core Extension: memory-core size: S labels Sep 26, 2026
@steipete
steipete marked this pull request as ready for review September 26, 2026 02:05
@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 Sep 26, 2026
@clawsweeper

clawsweeper Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 25, 2026, 10:09 PM ET / September 26, 2026, 02:09 UTC.

ClawSweeper review

What this changes

After Gateway readiness, the branch warms the active Memory Core search worker during idle time and documents and tests when that warmup runs.

Merge readiness

⛔ Blocked before merge - 1 item remains

Current main lacks memory-worker warmup, and the related open Gateway prewarm PR addresses different first-use work. The recorded retrieval measurements give this focused PR a distinct reason to remain open.

Priority: P2
Reviewed head: bc07d75001dceb6adf4b6eedee6a91579504486b

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused code and paired built-worker measurements support the change, with the claimed benefit limited to component retrieval latency.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.
Evidence reviewed 10 items Idle registration: The proposed scheduler checks the active memory plugin before loading its narrow warmup artifact.
Existing worker owner: Warmup uses the same single-worker retrieval pool as normal searches.
Database-free acknowledgment: The new worker request returns before the database-opening retrieval branches.
Findings None None.
Security None None.

How this fits together

Memory Core searches indexed SQLite memory for agent turns. Gateway startup schedules optional idle work, which can prepare the search worker before an eligible request arrives.

flowchart LR
  A[Gateway ready] --> B[Idle scheduler]
  B --> C{Memory Core active?}
  C -- Yes --> D[Retrieval worker pool]
  D --> E[Search worker]
  F[Agent memory request] --> D
  E --> G[Memory results]
Loading

Before merge

  • Add data-model compatibility proof - Confirm migration or upgrade compatibility proof before merge.
Agent review details

Security

None.

PR surface

Source +31, Tests +27, Docs +5. Total +63 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 5 32 1 +31
Tests 1 46 19 +27
Docs 1 5 0 +5
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 83 20 +63

Review metrics

Metric Value Why it matters
First retrieval after warmup 169.96 ms → 4.74 ms median across 5 pairs The recorded component measurement shows the benefit and does not claim full Control UI latency.

Stored data model

Persistent data-model change detected: vector/embedding metadata: extensions/memory-core/src/memory/manager-cpu-worker-runtime.ts, vector/embedding metadata: extensions/memory-core/src/memory/manager-search.worker.ts. Confirm migration or upgrade compatibility proof before merge.

Technical review

Best possible solution:

The first eligible search after Gateway startup should reuse a prepared worker, while request-time retrieval and normal worker retirement remain authoritative.

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

Yes: alternating cold and warmed retrieval against seeded project memory is a specific repeatable comparison, and the PR records its results. This read-only review did not rerun it.

Is this the best way to solve the issue?

Yes: the existing idle scheduler and retrieval worker pool provide a narrow place to move initialization without changing search results or stored data.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b03cf209ea6b.

Labels

Label changes:

  • add P2: This is a bounded first-turn latency improvement with no demonstrated broad availability regression.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.
  • 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. Sufficient (live_output): The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.

Label justifications:

  • P2: This is a bounded first-turn latency improvement with no demonstrated broad availability regression.
  • 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. Sufficient (live_output): The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR records five alternating runs of a built candidate against seeded project memory: after the real worker warmup, first retrieval was faster and returned identical entries. The canceled linked hydration workflow does not independently verify that table, and no full Control UI latency result is claimed. No stored-data contract changes.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Jason (Json): 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.

@steipete
steipete merged commit e26681d into main Sep 26, 2026
274 of 276 checks passed
@steipete
steipete deleted the perf/l41-memory-retrieval-prewarm branch September 26, 2026 02:54
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 26, 2026
…#158563)

Move cold retrieval module initialization into the existing idle lifecycle without opening databases or initializing embedding providers. Keep request-time retrieval and worker retirement authoritative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: memory-core Extension: memory-core 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. 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