Multiplayer agent harness for work.
Drain full memory index batches within one tick (#2326)
Regan Bellbb2c1a2f6c86
diff --git a/src/memory/semantic-memory.ts b/src/memory/semantic-memory.tsindex b9e7d700d..4465f7a82 100644--- a/src/memory/semantic-memory.ts+++ b/src/memory/semantic-memory.ts@@ -42,7 +42,10 @@ export function createSemanticMemory(opts: {return {memory,start: () => sweeper.start(),- stop: () => sweeper.stop(),+ stop() {+ void indexer.stop();+ return sweeper.stop();+ },async close() {await store?.close();store = undefined;diff --git a/src/memory/vector-indexer.ts b/src/memory/vector-indexer.tsindex 291600a01..a944c1a27 100644--- a/src/memory/vector-indexer.ts+++ b/src/memory/vector-indexer.ts@@ -13,17 +13,19 @@ export function createMemoryVectorIndexer(opts: {}) {const onError = opts.onError ?? ((error: unknown) => reportFailure("memory vector index", error));let pending: Promise<void> | undefined;+ let active: AbortController | undefined;let model: string | undefined;let dimensions: number | undefined;let afterId: string | undefined;let nextProbeAt = 0;async function run(): Promise<void> {+ const controller = (active = new AbortController());await opts.leaderLease.hold("memory:vector-index", async (lost) => {const current = await opts.resolve();if (!current) return;- const controller = new AbortController();void lost.then(() => controller.abort());const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(60_000)]);+ signal.throwIfAborted();const { embedder, store } = current;const now = (opts.now ?? Date.now)();if (model !== embedder.id || dimensions === undefined || now >= nextProbeAt) {@@ -36,11 +38,12 @@ export function createMemoryVectorIndexer(opts: {dimensions = width;nextProbeAt = now + 10 * 60_000;}- signal.throwIfAborted();- const facts = await store.missing(embedder.id, 64, dimensions, signal, afterId);- afterId = facts.at(-1)?.factId;try {- if (facts.length) {+ do {+ signal.throwIfAborted();+ const facts = await store.missing(embedder.id, 64, dimensions, signal, afterId);+ afterId = facts.at(-1)?.factId;+ if (!facts.length) break;const vectors = await embedder.embed(facts.map((fact) => fact.text.slice(0, 8000)),signal,@@ -61,18 +64,24 @@ export function createMemoryVectorIndexer(opts: {}signal.throwIfAborted();await store.put(rows, signal);- }+ if (facts.length < 64) break;+ } while ((opts.now ?? Date.now)() - now < 20_000);} finally {if (!signal.aborted) await store.prune(embedder.id, 200, dimensions, signal).catch(onError);}});}return {+ stop(): Promise<void> {+ active?.abort();+ return pending ?? Promise.resolve();+ },tick(): Promise<void> {pending ??= run().catch(onError).finally(() => {pending = undefined;+ active = undefined;});return pending;},diff --git a/test/memory-vector-indexer.test.ts b/test/memory-vector-indexer.test.tsindex 5d939344a..6f67d8c4a 100644--- a/test/memory-vector-indexer.test.ts+++ b/test/memory-vector-indexer.test.ts@@ -54,8 +54,8 @@ function fixture() {writes,failures,prunes,- advance: () => {- now += 600001;+ advance: (ms = 600001) => {+ now += ms;},};}@@ -197,3 +197,65 @@ for (const operation of ["embed", "put"] as const) {assert.ok(f.failures.includes(cleanup));});}++test("indexer drains full batches within twenty seconds and prunes once per tick", async () => {+ const f = fixture();+ const missing = f.store.missing;+ const put = f.store.put;+ const sizes = [64, 64, 3];+ f.store.missing = async (...args) => {+ const [fact] = await missing(...args);+ return Array.from({ length: sizes.shift() ?? 0 }, (_, i) => ({+ ...fact!,+ factId: String(Number(args[4] ?? 0) + i + 1),+ }));+ };+ f.store.put = async (...args) => {+ await put(...args);+ f.advance(10_000);+ };+ await f.indexer.tick();+ assert.deepEqual(+ f.writes.map((rows) => rows.length),+ [64, 64],+ );+ assert.equal(f.prunes.length, 1);+ await f.indexer.tick();+ assert.deepEqual(+ f.writes.map((rows) => rows.length),+ [64, 64, 3],+ );+ assert.deepEqual(+ f.requests.map((request) => request.afterId),+ [undefined, "64", "128"],+ );+ assert.equal(f.prunes.length, 2);+ assert.deepEqual(f.failures, []);+});++test("stopping a mid-drain indexer aborts promptly without another put", { timeout: 1000 }, async () => {+ const f = fixture();+ const missing = f.store.missing;+ const entered = Promise.withResolvers<void>();+ f.store.missing = async (...args) => {+ const [fact] = await missing(...args);+ return Array.from({ length: 64 }, (_, i) => ({+ ...fact!,+ factId: String(Number(args[4] ?? 0) + i + 1),+ }));+ };+ f.embedder.embed = async (texts, signal) => {+ if (f.writes.length) {+ entered.resolve();+ await new Promise<void>((resolve) => signal!.addEventListener("abort", () => resolve(), { once: true }));+ }+ return texts.map(() => [1, 0]);+ };+ const tick = f.indexer.tick();+ await entered.promise;+ await f.indexer.stop();+ await tick;+ assert.equal(f.writes.length, 1);+ assert.equal(f.requests.length, 2);+ assert.equal(f.prunes.length, 0);+});