Skip to content

perf(memory): batch chunk writes across indexed sources - #174874

Merged
steipete merged 6 commits into
mainfrom
dbasync/memindex-write-cost
Oct 17, 2026
Merged

steipete merged 6 commits into
mainfrom
dbasync/memindex-write-cost

Conversation

@steipete

@steipete steipete commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Transcript memory indexing groups several sources in one worker transaction but still executes three relational INSERTs separately for every small source. SQLite also probes vectors and rereads old embeddings before applying publication-owned replacements.

User Impact

Reduces memory-indexing SQL while preserving indexed text, metadata, provenance, FTS and vector results. Recall keeps the existing freshness policy: indexing after 30 seconds quiet, or the next idle gap after five minutes pending; search reads the last committed index and does not force a flush. No configuration or migration is required.

Design

The existing publication kernel combines final partial chunk batches across distinct sources, carrying source context on each row and bounding batches at 128 rows. Full batches, intermediate removal flushes and repeated-source boundaries publish in order within the existing transaction. Individual-source writes use the same kernel. Ordinary memory files retain their individual path when remote embedding batches are disabled; grouped transcript publication benefits from the batching.

SQLite consumes supplied embeddings without per-vector existence probes or old-embedding comparisons. The final supplied embedding for each ID wins, including changed and empty vectors. PostgreSQL skips manual-vector candidate work because its projection is trigger-owned. Old IDs remain available for deleting rows and vectors removed by full replacement; metadata-only retained rows preserve their vectors. Shadow-to-live publication and transaction boundaries are unchanged.

Removed checks and accepted edge cases:

  • Per-vector existence probes: replacement can rewrite an unchanged supplied vector; the same write repairs a missing vector.
  • Old-embedding reread/comparison: the publication owner's supplied embedding wins without comparing previously published bytes; metadata-only retained rows keep their vectors.

Session-forget tombstones and vector-repair state remain because they protect deleted user data and actual failed cleanup. The batching tradeoff is retaining at most one 128-row partial relational batch until the existing group commits. No schema, SDK, watcher, scheduling, generic-staging or PostgreSQL engine implementation changes.

Measurements

Exact baseline 2a42cf8ae96897a847364cd084c77ac21197c95d versus candidate 9e1a9f67169821a8766eae730069b9f30fe4919d. Three alternating memory-enabled pairs (M,C,M,C,M,C), followed by one standard-v3 non-regression pair, reuse pinned builds and the same Node/SQLite runtime. The private shared-v3 adapter uses three synthetic agents, 24 notebooks per agent, 24 seeded sessions / 576 messages, deterministic loopback embeddings, normal memory-search activation, and edits to eight notebooks per agent. No real data or external model service.

Workload totals and normalized costs below are median [min–max] across three runs per arm. All runs submit 590 chunks. Each has ten chunk-producing passes; there are seven zero-chunk passes except C3, which has six. Per-pass rows summarize each run's mean, not the distribution of individual passes.

Metric Baseline Candidate
Memory-pass SQL, whole capture 4,842 [4,835–4,846] 4,705 [4,687–4,706]
Memory-pass worker requests, whole capture 786 [786–787] 787 [786–789]
Memory-pass BEGIN transactions, whole capture 406 [405–408] 408 [408–409]
SQL per indexing pass (run mean) 284.824 [284.412–285.059] 276.824 [276.765–292.938]
Requests per indexing pass (run mean) 46.235 [46.235–46.294] 46.412 [46.294–49.125]
Transactions per indexing pass (run mean) 23.882 [23.824–24] 24.059 [24–25.5]
SQL per submitted chunk 8.207 [8.195–8.214] 7.975 [7.944–7.976]
Requests per submitted chunk 1.332 [1.332–1.334] 1.334 [1.332–1.337]
Whole-Gateway SQL, all phases 10,186 [10,116–10,396] 10,130 [9,935–10,149]
Whole-Gateway requests, all phases 2,168 [2,159–2,189] 2,176 [2,157–2,186]

The memory-pass SQL median falls 2.83%; this change does not establish a worker-request reduction. The changed operations are exact in every arm: source.replace.group 594→520 SQL with 5 commands / 5 transactions, and source.replace.inline 1,998→1,926 SQL with 104 commands / 104 transactions. That is 146 fewer statements with unchanged command/transaction counts. Shadow-to-live database.publish remains 159 SQL / 3 commands / 6 transactions in every arm. Other work varies, including vector.ensure (11–12 baseline commands versus 12 candidate) and no-op passes. Whole-Gateway totals include unrelated boot, turn and background work.

Metric Baseline Candidate
Initial activation, 510 chunks / 3 passes: SQL 3,669 [3,661–3,672] 3,614 [3,598–3,616]
Initial activation, 510 chunks / 3 passes: requests 553 [553–554] 555 [554–556]
Initial activation, 510 chunks / 3 passes: SQL/pass 1,223 [1,220.333–1,224] 1,204.667 [1,199.333–1,205.333]
Initial activation, 510 chunks / 3 passes: requests/pass 184.333 [184.333–184.667] 185 [184.667–185.333]
Incremental edit, 72 chunks / 3 passes: SQL 819 [819–819] 747 [747–747]
Incremental edit, 72 chunks / 3 passes: requests 126 [126–126] 126 [126–126]
Incremental edit, 72 chunks / 3 passes: SQL/pass 273 [273–273] 249 [249–249]
Incremental edit, 72 chunks / 3 passes: requests/pass 42 [42–42] 42 [42–42]

Incremental SQL falls 8.79% with requests unchanged. Initial activation is selected by phase and chunk-producing work, regardless of trigger: C3's main-agent pass was triggered by watch, while the other initial passes were search-bootstrap.

Warmed rows below are pooled individual-turn median [min–max], separately from the three-run summaries above.

Metric Baseline Candidate
Memory enabled: warmed SQL (30 turns/arm) 78 [78–80] 78 [78–87]
Memory enabled: warmed requests (30 turns/arm) 23 [23–24] 23 [23–28]
Memory enabled: warmed MAIN SQL 0 [0–0] 0 [0–0]
Standard v3: warmed SQL (10 turns/arm) 65 [64–83] 64 [64–99]
Standard v3: warmed requests (10 turns/arm) 19.5 [19–22] 19 [19–25]
Standard v3: warmed MAIN SQL 0 [0–9] 0 [0–13]

Memory-enabled causal warmed work is 78 SQL / 23.1 requests per turn in every run (run mean), with 0 MAIN. C3's final raw warmed window includes 9 background statements / 5 background requests; causal turn work is unchanged. Standard-v3 causal warmed distributions are identical: 64 [64–65] SQL / 19 [19–20] requests / 0 MAIN. Nonzero raw MAIN samples are background work; the lower standard raw medians are not claimed as patch savings. No memory SQL overlaps these warmed windows, so this proves non-regression, not concurrent-indexing savings. Changed-owner T1 call sites remain 0→0.

Rows, bytes, FTS and vector work

Each arm submits 590 chunk, metadata and provenance rows, 710,049 UTF-8 text bytes, and 18,880 binary64 chunk-embedding bytes. Successful native run().changes totals are 3,267 [3,267–3,267] baseline versus 3,267 [3,267–3,291] candidate; C2's extra 24 changes are cache bookkeeping (eight deletes, sixteen inserts). These are logical submissions/observed changes, not disk or WAL bytes. run().changes omits exec DML, shadow-to-live copies and trigger-internal changes. Actual indexed-message counts are unavailable; the 576 seeded messages are not used as that denominator.

Metric Baseline Candidate
Chunk SQL 588 [586–588] 565 [565–565]
Recall metadata SQL 141 [141–141] 118 [118–118]
Provenance SQL 162 [162–162] 139 [139–139]
Explicit vector SQL 901 [899–901] 824 [824–824]
Explicit FTS SQL 93 [93–93] 93 [93–93]
Embedding-cache SQL 442 [442–442] 442 [442–442]

FTS/vector figures count explicit top-level SQL; trigger-internal FTS VM work is unmeasured. Final FTS/vector rows and ordered keyword/vector search output are byte-identical in the owner capture. All six workloads have five session-source group commands, up to eight sources per group; 538 JSON staging steps and zero worker staging commands per run. Generic staging inserts were not changed, so no generic-staging PostgreSQL rerun is requested.

Observed timing (overlapping intervals; no latency claim)

Metric Baseline Candidate
Pass elapsed ms 883.774 [684.77–1,304.69] 992.928 [790.061–1,348.73]
Observed native SQL ms 153.764 [114.342–206.205] 168.867 [114.92–195]
Observed embedding ms (including cache persistence) 405.307 [324.842–438.719] 462.809 [267.412–570.057]
JSON fragment construction ms 1.574 [1.472–2.16] 1.682 [1.538–2.623]
Worker staging-handler ms 0 [0–0] 0 [0–0]
Publication ms 321.758 [224.912–384.489] 303.456 [225.182–416.962]

These are milliseconds per pass, summarized as three-run median [min–max]. The shared 32-core host was busy (recorded one-minute load 37.375–109.08 across run start/end samples). SQL, embedding, publication, serialization and staging intervals overlap, may run concurrently, and include instrumentation overhead; they are not additive exclusive timing buckets and do not establish latency improvement or regression. Worker staging handlers were not exercised; JSON fragment construction was.

Proof and Limitations

Owner capture passed exact stored-row and ordered keyword/vector-search equality, obsolete-vector deletion, changed same-ID vector bytes, delta/full replacement equivalence, trigger-abort rollback, and an actual child SIGKILL after native chunk INSERT but before COMMIT followed by reopen recovery. Eight one-chunk sources cost 91→70 SQL; repeated publication 99→78; each remains one transaction with identical successful direct row counts and bound-write bytes (40 rows / 4,416 bytes initially; 56 / 4,928 on repeat). The capture starts without a clean vector marker, so these savings establish batching; committed clean-index regressions cover removed probes, replacement, retention and cleanup.

Final-head CI 38990924116 passed changed-extension suites, production/test types, type-aware lint, source contracts/export collisions, architecture/Madge, T1/dialect ratchets and PostgreSQL conformance. Both allowed independent Codex autoreview rounds found no actionable P0–P2 defects. Changed-file formatting, direct lint and diff checks passed. Knip has no new findings: 28 production findings match integrated main 2a42cf8ae968, 22 full-tree findings match main 86abec416118, scripts are clean, and no finding is in the changed owners. This is an inherited-finding qualification, not a clean whole-repository Knip result. A local type-aware wrapper timed out preparing SDK declarations; final-head CI passed. SDK API diff and published-driver update cell do not apply.

Failed attempts are excluded, not counted as passes. Early fixture issues were repaired (SQLite row-prototype assertion, missing seeder runtime-config preparation, and tool-receipt/observer parsing). One C3 attempt closed WebSocket 1006 during activation; its cause remains unestablished, and the successful replay does not prove a fix. Two later attempts timed out before Gateway launch, during synthetic account setup and transcript seeding. The private adapter allows account setup 300s instead of 120s and C3 seeding 600s instead of 180s; both standard arms use the same account adjustment. Exact source normalization/hashes verify only these unmeasured deadlines changed. Shared harness files and measured workload/observer inputs remain untouched by those adjustments. All failed-attempt children exited.

Production +94/−79 (net +15); tests +231/−54 (net +177). The production growth carries per-row source context through one bounded implementation; superseded checks and obsolete assertions are removed. Follow-up for the engine owner: at the candidate head, the removed prepareExists caller leaves only engine-contract tests; retire that unused capability in the engine-owned scope.

@clawsweeper

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T01:30:51.680260Z 9e1a9f6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper clawsweeper Bot added the clawsweeper:intake:feature Preliminary submission classification; full source review pending. label Oct 17, 2026
@steipete
steipete merged commit 3aaea79 into main Oct 17, 2026
110 of 111 checks passed
@steipete
steipete deleted the dbasync/memindex-write-cost branch October 17, 2026 02:21
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 17, 2026
)

* perf(memory): batch chunk writes across indexed sources

* test(memory): bind native vector queries as float32

* test(memory): simplify grouped source fixture setup

* perf(memory): trust supplied vector replacements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:feature Preliminary submission classification; full source review pending. extensions: memory-core Extension: memory-core size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant