Repository navigation
improve(database): rebuild ported memory indexes concurrently - #185237
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. What this changesRebuilds ported agents’ memory indexes concurrently, with fanout bounded by host CPU capacity and free memory. Example: Port 11 agents to PostgreSQL on a 32-CPU Mac using a delayed local embedding endpoint.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR provides a focused, supported reduction in database-port downtime that current main still lacks; no actionable defects remain. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherAfter database publication releases port custody, the command acquires offline memory ownership, rebuilds the ported agents through the selected memory plugin, and returns ordered results and recovery warnings. flowchart TD
A[Database port command] --> B[Publish agent stores]
B --> C[Acquire offline memory ownership]
C --> D[Resource bounded scheduler]
D --> E[Selected memory plugin]
E --> F[Agent memory indexes]
E --> G[Ordered results and warnings]
Technical reviewBest possible solution: Use the existing bounded scheduler within the existing offline owner, preserving per-agent cleanup, ordered results, and recovery guidance. Do we have a high-confidence way to reproduce the issue? Current main visibly awaits agent rebuilds sequentially, and the supplied built-CLI comparison demonstrates the resulting multi-agent downtime. Is this the best way to solve the issue? The smallest complete improvement is bounded scheduling around the existing per-agent operation; this patch does that without adding another state owner or changing persisted contracts. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against d45ff6de6c48. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +17, Tests +120, Docs +5. Total +142 across 6 files. Added test files: 1. View PR surface stats
LabelsLabel changes: No label changes. Label justifications:
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (3 earlier review cycles)
Reviewed October 27, 2026, 3:43 PM ET / 19:43 UTC (Revision 4). |
b3f72cc to
4a75ef2
Compare
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
- Run post-port memory index rebuilds through runTasksWithConcurrency instead of one agent at a time. - Size fanout with resolveMemoryRebuildConcurrency in src/commands/doctor-database-port-memory-concurrency.ts: min(compute pool size, free memory / 2 / 512 MiB), at least 1, so hosts with two or fewer CPUs or under 2 GiB free stay sequential. - Keep the sizing next to its only consumer and leave src/infra/worker-pool-sizing.ts unchanged: that file is in the trusted-anchor PR wrapper inventory (scripts/pr-lib/wrapper-components.txt), and test/scripts/pr-wrapper-source-closure.test.ts and eager-import-closure.test.ts require the inventory to stay closed over its relative runtime imports. - Keep failures per agent: each rebuild returns its own result and recovery command, and an unexpected throw fails only that agent. - Rebuild still runs under fresh offline ownership; the Gateway starts after the command finishes. - Document concurrent rebuilds in the PostgreSQL guide and database engine reference. Related: openclaw#181859
4a75ef2 to
5956e4e
Compare
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
…aw#185237) - Run post-port memory index rebuilds through runTasksWithConcurrency instead of one agent at a time. - Size fanout with resolveMemoryRebuildConcurrency in src/commands/doctor-database-port-memory-concurrency.ts: min(compute pool size, free memory / 2 / 512 MiB), at least 1, so hosts with two or fewer CPUs or under 2 GiB free stay sequential. - Keep the sizing next to its only consumer and leave src/infra/worker-pool-sizing.ts unchanged: that file is in the trusted-anchor PR wrapper inventory (scripts/pr-lib/wrapper-components.txt), and test/scripts/pr-wrapper-source-closure.test.ts and eager-import-closure.test.ts require the inventory to stay closed over its relative runtime imports. - Keep failures per agent: each rebuild returns its own result and recovery command, and an unexpected throw fails only that agent. - Rebuild still runs under fresh offline ownership; the Gateway starts after the command finishes. - Document concurrent rebuilds in the PostgreSQL guide and database engine reference. Related: openclaw#181859
Related: #181859
What Problem This Solves
Moving an installation's agent databases to PostgreSQL keeps the Gateway down while every agent's memory index is rebuilt, one agent after another, so downtime grows with the total of all agents instead of the slowest one.
User Impact
User impact: operators running
openclaw database porton a multi-agent installation get their Gateway back sooner, because memory indexes for up to four agents now rebuild at the same time when the host has the CPUs and free memory for it. Small hosts (two or fewer CPUs, or under 2 GiB free memory) keep the previous one-at-a-time behavior. Results, warnings, and the per-agentopenclaw memory index --agent <id>retry guidance are unchanged, and one agent's failure still leaves the others rebuilt. The Gateway must still stay stopped until the command finishes.On a real 11-agent installation, the data copy finished in about 6 minutes and the sequential rebuilds then took 5,738 s (96 min); the slowest single agent took 2,865 s (48 min). With this change that installation's rebuild step is bounded by roughly the slowest agent instead of the sum.
Why This Change Was Made
Each agent's memory index lives in its own store with its own reindex lock, and the rebuild added in #182008 already isolates failures per agent, but it ran the agents in a plain loop. This change runs that same per-agent body through the repository's bounded task runner. The width is measured from the host at run time: the existing compute worker sizing (CPU count minus one, at most four) further limited by free memory at 512 MiB per rebuild with half kept in reserve. Embedding requests per agent keep their existing in-agent limit and retry/backoff on retryable provider errors, so a remote rate-limited provider sees at most four agents' worth of requests.
Starting the Gateway before the rebuild finishes is not part of this change: the rebuild acquires fresh offline ownership (
runWithLocalStateOwnerwithonForeignOwner: "refuse"), so a running Gateway would make it refuse. Changing that ownership model is a larger design decision.Evidence
node scripts/run-vitest.mjs): a new scheduling test shows three rebuilds in flight at once with one agent failing and the other three rebuilt; each fake rebuild waits, with no timer, until three have started, so a one-at-a-time scheduler (as onmain) never releases them and fails by the test timeout. A second test shows a host limit of one keeps rebuilds strictly sequential. Sizing tests (doctor-database-port-memory-concurrency.test.ts) cover 1 CPU, 2 CPUs, no free memory, 2 GiB, and a large host. All 6 port-memory tests, 5 sizing tests, and the existingdoctor-database-port.test.tspass.test/scripts/pr-wrapper-source-closure.test.ts,test/scripts/eager-import-closure.test.ts(27/27) andtest/scripts/pr-worktree-provision.test.tspass with the sizing helper kept beside its consumer, leaving the trusted-anchor wrapper inventory unchanged. One provisioner case (materializes full cold/warm PR siblings, then preserves native sparse transitions) times out at 120 s on the test host, and does the same onmainwith no diff.agent-port.postgres.test.ts(consumer of the rebuild results) passes against a local PostgreSQL 17.11: 9/9.openclaw database port --family agent --to postgresfrom builtdist, local PostgreSQL 17, and an OpenAI-compatible embedding stub answering each request after 2 s. The embedding model changed between seeding and the port so every chunk was re-embedded (287 requests in both runs).main3b3efe9)The slowest agent took 76.5 s in both runs, which is the floor for the rebuild step. Small agents took slightly longer each when sharing the host (for example 2.5 s to 4.8 s), with no failures.
pnpm changed:lanes --json: core, core tests, docs.pnpm check:changed: typecheck core and core tests, lint, format, mock-export ratchet, and the other guards pass. Two checks fail identically onmainin files this change does not touch (deprecated API usage inextensions/codex/harness.ts:261, and the Knip unused-export scan).git diff --checkis clean.Technical detail
src/commands/doctor-database-port-memory.ts: the per-target loop body becomes arebuild(target)function that returns its result instead of pushing and continuing, andrunTasksWithConcurrencyruns it in continue-on-error mode. Results stay in target order. A defensive fallback maps any slot left empty by an unexpected throw to that agent's existing failure result with recovery guidance.src/commands/doctor-database-port-memory-concurrency.ts:resolveMemoryRebuildConcurrency()returnsmax(1, min(resolveWorkerPoolSize("compute"), floor(readHostFreeMemoryBytes() / 2 / 512 MiB))). It is measured once per port. It lives beside its only consumer rather than insrc/infra/worker-pool-sizing.ts, which belongs to the PR wrapper inventory (scripts/pr-lib/wrapper-components.txt) that the closure tests keep closed over its relative imports.docs/gateway/postgresql.mdanddocs/reference/database-engines.mddescribe the concurrent rebuild, the small-host fallback, and that the rebuild stays offline.