Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Batch inbox ingestion, sidebar reads, and Slack catchup writes (#2073)

Regan Bellec981a1214ef
diff --git a/package.json b/package.json
index 38e5323c9..1e56d7061 100644
--- a/package.json
+++ b/package.json
@@ -46,7 +46,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node scripts/run-pg-tests.mjs test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
+ "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.ts
index 21f5f3d63..6a7bd4704 100644
--- a/src/api/app-sessions.ts
+++ b/src/api/app-sessions.ts
@@ -438,22 +438,30 @@ export function createSessionMethods(
if (ref) cronCounts.set(ref, (cronCounts.get(ref) ?? 0) + 1);
}
}
+ const working = sessions.filter((s) => workingThreadRefs.has(s.threadRef));
+ const idleChildren = sessions.filter(
+ (s) => s.parentSessionId && !workingThreadRefs.has(s.threadRef) && !waiting.has(s.id),
+ );
+ const [latestRuns, recentEntries] = await Promise.all([
+ deps.runs.latestForThreads([...working, ...idleChildren].map((s) => s.threadRef)),
+ deps.sessions.getRecentEntries(
+ working.map((s) => s.id),
+ GOAL_LOOKBACK_ENTRIES,
+ ),
+ ]);
const failedChildren = new Set<string>();
- for (const s of sessions) {
- if (!s.parentSessionId || workingThreadRefs.has(s.threadRef) || waiting.has(s.id)) continue;
- const run = await deps.runs.latestForThread(s.threadRef);
+ for (const s of idleChildren) {
+ const run = latestRuns.get(s.threadRef);
if (run?.status === "failed" || run?.result?.status === "failed") failedChildren.add(s.id);
}
const goals = new Map<
string,
{ objective: string; activeMs: number; runningSince?: number; floor?: Record<string, number> }
>();
- for (const s of sessions) {
- if (!workingThreadRefs.has(s.threadRef)) continue;
- const since = Math.max(0, (await deps.sessions.latestEntrySeq(s.id)) - GOAL_LOOKBACK_ENTRIES);
- const goal = latestGoalRecord(await deps.sessions.getEntries(s.id, { sinceSeq: since }));
+ for (const s of working) {
+ const goal = latestGoalRecord(recentEntries.get(s.id) ?? []);
if (!goal) continue;
- const runningSince = (await deps.runs.latestForThread(s.threadRef))?.startedAt ?? undefined;
+ const runningSince = latestRuns.get(s.threadRef)?.startedAt ?? undefined;
if (goal.status === "active")
goals.set(s.id, {
objective: goal.objective,
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index f9f57d9ef..60cd319f9 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -1005,33 +1005,40 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
await withManagedRosterVersion(async () => {
await reconcileSessionParticipants(session.id);
await Promise.all(pendingScreenRequests.splice(0).map((rec) => recordScreenRequest(rec)));
- for (const overheard of screenedOverheard) {
- const imported = await deps.sessions.append(lease, {
+ const importedEntries = await deps.sessions.appendMany(
+ lease,
+ screenedOverheard.map((overheard) => ({
type: "user",
payload: { ...overheard, securityTainted: true },
scopeLabel: scopeId,
- });
- await deps.sessions.appendTape(lease, {
- kind: "message",
- payload: {
- role: "user",
- content: [{ type: "text", text: renderOverheard(overheard) }],
- timestamp: imported.createdAt,
- },
- scopeLabel: scopeId,
- entrySeq: imported.seq,
- meta: {
- overheard: true,
- ...(overheard.sourceRole ? { sourceRole: overheard.sourceRole } : {}),
- bareText: overheard.text,
- ts: overheard.ts,
- ...(overheard.name ? { author: overheard.name } : {}),
- ...(overheard.files?.length ? { attachments: overheard.files } : {}),
- securityTainted: true,
- entryCreatedAt: imported.createdAt,
- },
- });
- }
+ })),
+ );
+ await deps.sessions.appendTapeMany(
+ lease,
+ screenedOverheard.map((overheard, index) => {
+ const imported = importedEntries[index]!;
+ return {
+ kind: "message",
+ payload: {
+ role: "user",
+ content: [{ type: "text", text: renderOverheard(overheard) }],
+ timestamp: imported.createdAt,
+ },
+ scopeLabel: scopeId,
+ entrySeq: imported.seq,
+ meta: {
+ overheard: true,
+ ...(overheard.sourceRole ? { sourceRole: overheard.sourceRole } : {}),
+ bareText: overheard.text,
+ ts: overheard.ts,
+ ...(overheard.name ? { author: overheard.name } : {}),
+ ...(overheard.files?.length ? { attachments: overheard.files } : {}),
+ securityTainted: true,
+ entryCreatedAt: imported.createdAt,
+ },
+ };
+ }),
+ );
const taintedPayload: Record<string, unknown> = {
...swarmEntryProvenance,
text: input.text,
@@ -2988,44 +2995,48 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
.forRender(session.id)
.then((read) => selectOverheardToImport(input.overheard!, recordedMessageTimestamps(read.entries)))
.catch(swallowAs("orchestrator: overheard catch-up import", [] as OverheardEntryPayload[]));
- for (const p of toImport) {
- let imported;
- try {
- imported = await withManagedRosterVersion(() =>
- deps.sessions.append(lease, {
- type: "user",
- payload: p,
- scopeLabel: scopeId,
- }),
- );
- } catch (e) {
- if (e instanceof ProjectRosterChanged) throw e;
- swallow("orchestrator: overheard catch-up import", e);
- break;
- }
- importedOverheard.push(p);
- preAppendedSeqs.push(imported.seq);
+ let importedEntries: SessionEntry[] = [];
+ try {
+ importedEntries = await withManagedRosterVersion(() =>
+ deps.sessions.appendMany(
+ lease,
+ toImport.map((p) => ({ type: "user", payload: p, scopeLabel: scopeId })),
+ ),
+ );
+ } catch (e) {
+ if (e instanceof ProjectRosterChanged) throw e;
+ swallow("orchestrator: overheard catch-up import", e);
+ }
+ if (importedEntries.length) {
+ importedOverheard.push(...toImport);
+ preAppendedSeqs.push(...importedEntries.map((entry) => entry.seq));
await withManagedRosterVersion(() =>
- deps.sessions.appendTape(lease, {
- kind: "message",
- payload: {
- role: "user",
- content: [{ type: "text", text: renderOverheard(p) }],
- timestamp: imported.createdAt,
- },
- scopeLabel: scopeId,
- entrySeq: imported.seq,
- meta: {
- overheard: true,
- ...(p.sourceRole ? { sourceRole: p.sourceRole } : {}),
- bareText: p.text,
- ts: p.ts,
- ...(p.changeTime ? { changeTime: p.changeTime } : {}),
- ...(p.name ? { author: p.name } : {}),
- ...(p.files?.length ? { attachments: p.files } : {}),
- entryCreatedAt: imported.createdAt,
- },
- }),
+ deps.sessions.appendTapeMany(
+ lease,
+ toImport.map((p, index) => {
+ const imported = importedEntries[index]!;
+ return {
+ kind: "message",
+ payload: {
+ role: "user",
+ content: [{ type: "text", text: renderOverheard(p) }],
+ timestamp: imported.createdAt,
+ },
+ scopeLabel: scopeId,
+ entrySeq: imported.seq,
+ meta: {
+ overheard: true,
+ ...(p.sourceRole ? { sourceRole: p.sourceRole } : {}),
+ bareText: p.text,
+ ts: p.ts,
+ ...(p.changeTime ? { changeTime: p.changeTime } : {}),
+ ...(p.name ? { author: p.name } : {}),
+ ...(p.files?.length ? { attachments: p.files } : {}),
+ entryCreatedAt: imported.createdAt,
+ },
+ };
+ }),
+ ),
);
}
}
diff --git a/src/loops/item-ledger.ts b/src/loops/item-ledger.ts
index cc0ba970a..3357529b9 100644
--- a/src/loops/item-ledger.ts
+++ b/src/loops/item-ledger.ts
@@ -325,47 +325,64 @@ export function createLoopItemLedger(
},
async ingest(entries) {
const outcome: IngestOutcome = { created: 0, updated: 0, skipped: 0 };
- for (const entry of entries) {
- const id = await idFor(entry.loopId, entry.dedupeKey);
- const now = Date.now();
- const candidate: LoopItem = {
- id,
- loopId: entry.loopId,
- sourceKey: entry.dedupeKey,
- status: entry.proposal ? "ready" : "queued",
- attempts: 0,
- runIds: [],
- outputIds: [],
- sourcePayload: entry.sourcePayload,
- inboxPreview: inboxPreview(entry.sourcePayload),
- createdAt: now,
- updatedAt: now,
- ...(entry.source !== undefined ? { source: entry.source } : {}),
- ...(entry.summary !== undefined ? { sourceSummary: entry.summary } : {}),
- ...(entry.sourceAt !== undefined ? { sourceAt: entry.sourceAt } : {}),
- ...(entry.proposal ? { proposal: { ...entry.proposal, at: now } } : {}),
- ...withAgentDraft({ agentDrafts: [] }, entry.proposal ? { ...entry.proposal, at: now } : undefined),
- };
- const inserted = backing.insertIfAbsent
- ? await backing.insertIfAbsent(id, candidate)
- : (await backing.putIfAbsent(id, candidate)).createdAt === candidate.createdAt;
- if (inserted) {
- outcome.created++;
- emit(candidate, "ingest");
- continue;
- }
- let merged = false;
- await update(id, (item) => {
- const next = mergeIngest(item, entry, Date.now());
- if (next === null) return item;
- merged = true;
- return next;
- });
- if (merged) {
- outcome.updated++;
- emit(await backing.get(id), "ingest");
- } else outcome.skipped++;
+ if (!entries.length) return outcome;
+ const existing = await backing.select({
+ where: { field: "loopId", anyOfFold: [...new Set(entries.map((entry) => entry.loopId))] },
+ omit: ["sourcePayload", "proposal", "agentDrafts", "thread"],
+ });
+ const ids = new Set(existing.map((item) => item.id));
+ const legacy = new Map<string, string>();
+ for (const item of existing) {
+ const key = JSON.stringify([item.loopId, item.sourceKey]);
+ if (!legacy.has(key)) legacy.set(key, item.id);
}
+ const changed: LoopItem[] = [];
+ await backing.mutateMany(
+ entries.map((entry) => {
+ const canonical = loopItemId(entry.loopId, entry.dedupeKey);
+ const id = ids.has(canonical)
+ ? canonical
+ : (legacy.get(JSON.stringify([entry.loopId, entry.dedupeKey])) ?? canonical);
+ return {
+ id,
+ apply: (item: LoopItem | null): LoopItem => {
+ const now = Date.now();
+ const candidate: LoopItem = {
+ id,
+ loopId: entry.loopId,
+ sourceKey: entry.dedupeKey,
+ status: entry.proposal ? "ready" : "queued",
+ attempts: 0,
+ runIds: [],
+ outputIds: [],
+ sourcePayload: entry.sourcePayload,
+ inboxPreview: inboxPreview(entry.sourcePayload),
+ createdAt: now,
+ updatedAt: now,
+ ...(entry.source !== undefined ? { source: entry.source } : {}),
+ ...(entry.summary !== undefined ? { sourceSummary: entry.summary } : {}),
+ ...(entry.sourceAt !== undefined ? { sourceAt: entry.sourceAt } : {}),
+ ...(entry.proposal ? { proposal: { ...entry.proposal, at: now } } : {}),
+ ...withAgentDraft({ agentDrafts: [] }, entry.proposal ? { ...entry.proposal, at: now } : undefined),
+ };
+ if (!item) {
+ outcome.created++;
+ changed.push(candidate);
+ return candidate;
+ }
+ const next = mergeIngest(item, entry, now);
+ if (next) {
+ outcome.updated++;
+ changed.push(next);
+ return next;
+ }
+ outcome.skipped++;
+ return item;
+ },
+ };
+ }),
+ );
+ for (const item of changed) emit(item, "ingest");
return outcome;
},
async setProposal(id, proposal, opts) {
@@ -700,8 +717,14 @@ export function createLoopItemLedger(
enqueue: (input) => guarded([input.loopId], () => ledger.enqueue(input)),
ingest: async (entries) => {
const result = { created: 0, updated: 0, skipped: 0 };
+ const groups = new Map<string, typeof entries>();
for (const entry of entries) {
- const next = await guarded([entry.loopId], () => ledger.ingest([entry]));
+ const group = groups.get(entry.loopId) ?? [];
+ group.push(entry);
+ groups.set(entry.loopId, group);
+ }
+ for (const [loopId, group] of groups) {
+ const next = await guarded([loopId], () => ledger.ingest(group));
result.created += next.created;
result.updated += next.updated;
result.skipped += next.skipped;
diff --git a/src/persistence/durable-map.ts b/src/persistence/durable-map.ts
index 07bb473fb..ab8563a10 100644
--- a/src/persistence/durable-map.ts
+++ b/src/persistence/durable-map.ts
@@ -19,6 +19,7 @@ export interface DurableMap<T> {
insertIfAbsent?(id: string, value: T): Promise<boolean>;
merge(id: string, patch: Partial<T>): Promise<T | null>;
update?(id: string, fn: (value: T) => T): Promise<T | null>;
+ mutateMany(updates: readonly { id: string; apply: (value: T | null) => T }[]): Promise<T[]>;
deleteIf?(id: string, predicate: (value: T) => boolean): Promise<boolean>;
delete(id: string): Promise<void>;
take(id: string): Promise<T | null>;
@@ -145,6 +146,16 @@ export function createMemoryMap<T>(): DurableMap<T> {
m.set(id, next);
return next;
},
+ async mutateMany(updates) {
+ const pending = new Map<string, T>();
+ const results = updates.map(({ id, apply }) => {
+ const next = apply(structuredClone(pending.get(id) ?? m.get(id) ?? null));
+ pending.set(id, next);
+ return next;
+ });
+ for (const [id, value] of pending) m.set(id, value);
+ return results;
+ },
async deleteIf(id, predicate) {
const value = m.get(id);
if (value === undefined || !predicate(value)) return false;
@@ -349,6 +360,28 @@ export function createPostgresMap<T>(
return next;
});
},
+ async mutateMany(updates) {
+ if (!updates.length) return [];
+ return withBump(async (client) => {
+ const rows = await client.query(
+ `SELECT id, json FROM ${table} WHERE id = ANY($1::text[]) ORDER BY id FOR UPDATE`,
+ [updates.map(({ id }) => id)],
+ );
+ const current = new Map(rows.rows.map((row) => [row.id as string, row.json as T]));
+ const pending = new Map<string, T>();
+ const results = updates.map(({ id, apply }) => {
+ const next = apply(structuredClone(pending.get(id) ?? current.get(id) ?? null));
+ pending.set(id, next);
+ return next;
+ });
+ await client.query(
+ `INSERT INTO ${table} (id, json) SELECT id, json FROM jsonb_to_recordset($1::jsonb) AS batch(id text, json jsonb)
+ ON CONFLICT (id) DO UPDATE SET json = EXCLUDED.json`,
+ [jsonbStringify([...pending].map(([id, json]) => ({ id, json })))],
+ );
+ return results;
+ });
+ },
async deleteIf(id, predicate) {
return withBump(async (client) => {
const current = await client.query(`SELECT json FROM ${table} WHERE id = $1 FOR UPDATE`, [id]);
diff --git a/src/runs/memory-run-store.ts b/src/runs/memory-run-store.ts
index c49b78926..ea264b0ca 100644
--- a/src/runs/memory-run-store.ts
+++ b/src/runs/memory-run-store.ts
@@ -156,6 +156,16 @@ export function createMemoryRunStore(opts?: { maxClaims?: number }): MemoryRunti
return true;
},
+ async latestForThreads(threadRefs, opts) {
+ const wanted = new Set(threadRefs);
+ const latest = new Map<string, Run>();
+ for (const run of runs.values()) {
+ if (!wanted.has(run.sessionId) || (opts?.excludePrivateMessages && run.request.privateSessionMessage)) continue;
+ if (!latest.has(run.sessionId) || latest.get(run.sessionId)!.createdAt <= run.createdAt)
+ latest.set(run.sessionId, run);
+ }
+ return latest;
+ },
async latestForThread(threadRef, opts) {
return (
[...runs.values()]
diff --git a/src/runs/postgres-run-store.ts b/src/runs/postgres-run-store.ts
index 4824f9ab0..01d675e66 100644
--- a/src/runs/postgres-run-store.ts
+++ b/src/runs/postgres-run-store.ts
@@ -383,6 +383,24 @@ export function createPostgresRunStore(connectionString: string, opts?: { maxCla
return rowCount > 0;
},
+ async latestForThreads(threadRefs, opts) {
+ if (!threadRefs.length) return new Map();
+ const { rows } = await q(
+ `SELECT latest.* FROM unnest($1::text[]) AS threads(id)
+ CROSS JOIN LATERAL (
+ SELECT * FROM runs WHERE session_id = threads.id
+ AND (NOT $2::boolean OR COALESCE(request::jsonb->>'privateSessionMessage', 'false') <> 'true')
+ ORDER BY created_at DESC, seq DESC LIMIT 1
+ ) latest`,
+ [[...new Set(threadRefs)], Boolean(opts?.excludePrivateMessages)],
+ );
+ return new Map(
+ rows.map((row) => {
+ const run = rowToRun(row);
+ return [run.sessionId, run];
+ }),
+ );
+ },
async latestForThread(threadRef, opts) {
const { rows } = await q(
"SELECT * FROM runs WHERE session_id = $1 AND (NOT $2::boolean OR COALESCE(request::jsonb->>'privateSessionMessage', 'false') <> 'true') ORDER BY created_at DESC, seq DESC LIMIT 1",
diff --git a/src/runs/run-store.ts b/src/runs/run-store.ts
index fcc328196..4286db883 100644
--- a/src/runs/run-store.ts
+++ b/src/runs/run-store.ts
@@ -87,6 +87,10 @@ export interface RunStore {
noteTurnUserSeq(runId: string, seq: number): Promise<boolean>;
+ latestForThreads(
+ threadRefs: readonly string[],
+ opts?: { excludePrivateMessages?: boolean },
+ ): Promise<Map<string, Run>>;
latestForThread(threadRef: string, opts?: { excludePrivateMessages?: boolean }): Promise<Run | null>;
pendingReturns(limit?: number, afterId?: string): Promise<Run[]>;
terminalFinished(after: TerminalCursor, beforeMs: number, limit: number): Promise<Run[]>;
diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.ts
index 7bef5d19d..06ec59641 100644
--- a/src/sessions/memory-session-store.ts
+++ b/src/sessions/memory-session-store.ts
@@ -306,6 +306,15 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
});
},
+ async getRecentEntries(sessionIds, lookback) {
+ return new Map(
+ sessionIds.map((id) => {
+ const log = entries.get(id) ?? [];
+ const since = Math.max(0, (log.at(-1)?.seq ?? -1) - lookback);
+ return [id, log.filter((entry) => entry.seq >= since)];
+ }),
+ );
+ },
async getEntries(sessionId, opts?: GetEntriesOptions) {
const log = entries.get(sessionId) ?? [];
const since = opts?.sinceSeq ?? 0;
@@ -372,14 +381,22 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
},
async appendTape(lease, rec: NewTapeRecord): Promise<TapeRecord> {
+ return (await this.appendTapeMany(lease, [rec]))[0]!;
+ },
+ async appendTapeMany(lease, records): Promise<TapeRecord[]> {
const held = leases.get(lease.sessionId);
if (!held || held.token !== lease.token || now() >= held.expiresAt)
throw new Error("tape append without a valid session lease");
held.expiresAt = now() + leaseTtlMs;
const log = tape.get(lease.sessionId) ?? [];
tape.set(lease.sessionId, log);
- const full: TapeRecord = { ...rec, sessionId: lease.sessionId, seq: log.length, createdAt: now() };
- log.push(full);
+ const full = records.map((rec, index) => ({
+ ...rec,
+ sessionId: lease.sessionId,
+ seq: log.length + index,
+ createdAt: now(),
+ }));
+ log.push(...full);
return full;
},
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index 5eb3b1ed2..ac405a98e 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -1,5 +1,6 @@
import { randomUUID } from "node:crypto";
import { createPgPool, type PgPool, type PoolClient, withPgTransaction } from "../persistence/pg-pool.ts";
+import { jsonbStringify } from "../persistence/durable-map.ts";
import { jsonbSafeStringify } from "../util/text.ts";
import { reportFailure } from "../util/errors.ts";
import type { Session, SessionEntry, SessionType, ScopeId } from "../types.ts";
@@ -802,40 +803,55 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
return fn(client);
});
- const insertTapeRow = async (client: PoolClient, sessionId: string, rec: NewTapeRecord): Promise<TapeRecord> => {
+ const insertTapeRows = async (
+ client: PoolClient,
+ sessionId: string,
+ records: readonly NewTapeRecord[],
+ ): Promise<TapeRecord[]> => {
+ if (!records.length) return [];
const max = await client.query("SELECT COALESCE(MAX(seq), -1) + 1 AS n FROM session_tape WHERE session_id = $1", [
sessionId,
]);
- const seq = Number(max.rows[0]!.n);
- const stored = jsonbSafeStringify(rec.payload ?? null);
- const createdAt = now();
+ const firstSeq = Number(max.rows[0]!.n);
+ const full = records.map((rec, index) => ({
+ ...rec,
+ payload: JSON.parse(jsonbSafeStringify(rec.payload ?? null)),
+ sessionId,
+ seq: firstSeq + index,
+ createdAt: now(),
+ }));
await client.query(
`INSERT INTO session_tape(session_id, seq, kind, harness, payload, scope_label, bare_text, ts, change_time, hidden, overheard, author, attachments, display, security_tainted, entry_created_at, entry_seq, covers_entry_seq, created_at, source_role)
- VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20)`,
+ SELECT $1, seq, kind, harness, payload, scope_label, bare_text, ts, change_time, hidden, overheard, author, attachments, display, security_tainted, entry_created_at, entry_seq, covers_entry_seq, created_at, source_role
+ FROM jsonb_to_recordset($2::jsonb) AS batch(seq int, kind text, harness text, payload text, scope_label text, bare_text text, ts text, change_time text, hidden boolean, overheard boolean, author text, attachments text, display text, security_tainted boolean, entry_created_at bigint, entry_seq int, covers_entry_seq int, created_at bigint, source_role text)`,
[
sessionId,
- seq,
- rec.kind,
- rec.harness ?? null,
- stored,
- rec.scopeLabel,
- rec.meta?.bareText ?? null,
- rec.meta?.ts ?? null,
- rec.meta?.changeTime ?? null,
- rec.meta?.hidden ?? null,
- rec.meta?.overheard ?? null,
- rec.meta?.author ?? null,
- rec.meta?.attachments !== undefined ? jsonbSafeStringify(rec.meta.attachments) : null,
- rec.meta?.display ?? null,
- rec.meta?.securityTainted ?? null,
- rec.meta?.entryCreatedAt ?? null,
- rec.entrySeq ?? null,
- rec.coversEntrySeq ?? null,
- createdAt,
- rec.meta?.sourceRole ?? null,
+ jsonbStringify(
+ full.map((rec) => ({
+ seq: rec.seq,
+ kind: rec.kind,
+ harness: rec.harness,
+ payload: JSON.stringify(rec.payload),
+ scope_label: rec.scopeLabel,
+ bare_text: rec.meta?.bareText,
+ ts: rec.meta?.ts,
+ change_time: rec.meta?.changeTime,
+ hidden: rec.meta?.hidden,
+ overheard: rec.meta?.overheard,
+ author: rec.meta?.author,
+ attachments: rec.meta?.attachments !== undefined ? jsonbSafeStringify(rec.meta.attachments) : undefined,
+ display: rec.meta?.display,
+ security_tainted: rec.meta?.securityTainted,
+ entry_created_at: rec.meta?.entryCreatedAt,
+ entry_seq: rec.entrySeq,
+ covers_entry_seq: rec.coversEntrySeq,
+ created_at: rec.createdAt,
+ source_role: rec.meta?.sourceRole,
+ })),
+ ),
],
);
- return { ...rec, payload: JSON.parse(stored), sessionId, seq, createdAt };
+ return full;
};
return {
@@ -1069,7 +1085,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
[sessionId],
);
for (const row of updated.rows) {
- await insertTapeRow(client, sessionId, tapeTranscriptEntryRecord(rowToEntry(row)));
+ await insertTapeRows(client, sessionId, [tapeTranscriptEntryRecord(rowToEntry(row))]);
}
if (updated.rows.length > 0) return true;
return (await client.query("SELECT 1 FROM sessions WHERE id = $1", [sessionId])).rows.length === 1;
@@ -1107,8 +1123,12 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
},
async appendTape(lease, rec: NewTapeRecord): Promise<TapeRecord> {
+ return (await this.appendTapeMany(lease, [rec]))[0]!;
+ },
+
+ async appendTapeMany(lease, records) {
return withLease(lease, "tape append without a valid session lease", (client) =>
- insertTapeRow(client, lease.sessionId, rec),
+ insertTapeRows(client, lease.sessionId, records),
);
},
@@ -1152,6 +1172,21 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
return Number(rows[0]?.n ?? -1);
},
+ async getRecentEntries(sessionIds, lookback) {
+ const result = new Map<string, SessionEntry[]>(sessionIds.map((id) => [id, []]));
+ if (!sessionIds.length) return result;
+ const rows = await q(
+ `SELECT recent.* FROM unnest($1::text[]) AS sessions(id)
+ CROSS JOIN LATERAL (
+ SELECT * FROM session_entries WHERE session_id = sessions.id
+ AND seq >= GREATEST(0, (SELECT COALESCE(MAX(seq), -1) FROM session_entries WHERE session_id = sessions.id) - $2)
+ ORDER BY seq
+ ) recent ORDER BY recent.session_id, recent.seq`,
+ [[...result.keys()], lookback],
+ );
+ for (const row of rows) result.get(row.session_id as string)!.push(rowToEntry(row));
+ return result;
+ },
async getEntries(sessionId, opts?: GetEntriesOptions): Promise<SessionEntry[]> {
const params: unknown[] = [sessionId, opts?.sinceSeq ?? 0];
let sql = "SELECT * FROM session_entries WHERE session_id = $1 AND seq >= $2";
diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.ts
index 32189824f..cc529058a 100644
--- a/src/sessions/session-store.ts
+++ b/src/sessions/session-store.ts
@@ -730,6 +730,7 @@ export interface SessionStore {
append(lease: Lease, entry: NewEntry): Promise<SessionEntry>;
appendMany(lease: Lease, entries: readonly NewEntry[]): Promise<SessionEntry[]>;
+ getRecentEntries(sessionIds: readonly string[], lookback: number): Promise<Map<string, SessionEntry[]>>;
getEntries(sessionId: string, opts?: GetEntriesOptions): Promise<SessionEntry[]>;
getTranscriptEntries(sessionId: string, opts?: GetEntriesOptions): Promise<SessionEntry[]>;
canReadTranscriptSuffix(sessionId: string, beforeSeq: number): Promise<boolean>;
@@ -738,6 +739,7 @@ export interface SessionStore {
latestEntrySeq(sessionId: string): Promise<number>;
clearSecurityTaint(sessionId: string): Promise<boolean>;
+ appendTapeMany(lease: Lease, records: readonly NewTapeRecord[]): Promise<TapeRecord[]>;
appendTape(lease: Lease, rec: NewTapeRecord): Promise<TapeRecord>;
getTape(sessionId: string, opts?: GetTapeOptions): Promise<TapeRecord[]>;
tapeCoverage(sessionId: string): Promise<number>;
diff --git a/test/batch-paths.test.ts b/test/batch-paths.test.ts
new file mode 100644
index 000000000..71d541462
--- /dev/null
+++ b/test/batch-paths.test.ts
@@ -0,0 +1,179 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { randomUUID } from "node:crypto";
+import { createMemoryMap, createPostgresMapFactory } from "../src/persistence/durable-map.ts";
+import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
+import { createPostgresSessionStore } from "../src/sessions/postgres-session-store.ts";
+import { createMemoryRunStore } from "../src/runs/memory-run-store.ts";
+import { createPostgresRunStore } from "../src/runs/postgres-run-store.ts";
+import pg from "pg";
+import { scopeId } from "../src/types.ts";
+
+for (const backend of ["memory", "postgres"] as const) {
+ const skip = backend === "postgres" && !process.env.DATABASE_URL;
+ test(
+ `batch map mutations serialize duplicates and concurrent writers, and roll back: ${backend}`,
+ { skip },
+ async () => {
+ const factory = backend === "postgres" ? createPostgresMapFactory(process.env.DATABASE_URL!) : null;
+ const map =
+ factory?.map<{ n: number }>(`batch_${randomUUID().replaceAll("-", "")}`) ?? createMemoryMap<{ n: number }>();
+ try {
+ const increment = (value: { n: number } | null) => ({ n: (value?.n ?? 0) + 1 });
+ assert.deepEqual(
+ await map.mutateMany([
+ { id: "a", apply: increment },
+ { id: "a", apply: increment },
+ ]),
+ [{ n: 1 }, { n: 2 }],
+ );
+ await Promise.all(
+ Array.from({ length: 10 }, () =>
+ map.mutateMany([
+ { id: "a", apply: increment },
+ { id: "b", apply: increment },
+ ]),
+ ),
+ );
+ await Promise.all([map.mutateMany([{ id: "a", apply: increment }]), map.update!("a", increment)]);
+ assert.deepEqual(await map.get("a"), { n: 14 });
+ assert.deepEqual(await map.get("b"), { n: 10 });
+ await assert.rejects(
+ map.mutateMany([
+ {
+ id: "a",
+ apply: (value) => {
+ value!.n = 999;
+ return value!;
+ },
+ },
+ {
+ id: "c",
+ apply: () => {
+ throw new Error("rollback");
+ },
+ },
+ ]),
+ /rollback/,
+ );
+ assert.deepEqual(await map.get("a"), { n: 14 });
+ assert.equal(await map.get("c"), null);
+ assert.deepEqual(await map.mutateMany([]), []);
+ } finally {
+ await factory?.pool.close();
+ }
+ },
+ );
+
+ test(
+ `batch tape retains metadata, order, lease checks and recent-entry boundaries: ${backend}`,
+ { skip },
+ async () => {
+ const store =
+ backend === "memory" ? createMemorySessionStore() : createPostgresSessionStore(process.env.DATABASE_URL!);
+ const scope = scopeId("personal", "batch");
+ const ids: string[] = [];
+ for (let i = 0; i < 2; i++) {
+ const session = await store.getOrCreateByThread(`web:batch:${randomUUID()}`, "dm", scope);
+ ids.push(session.id);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ const entries = await store.appendMany(
+ lease,
+ Array.from({ length: 5 }, (_, n) => ({
+ type: "user" as const,
+ payload: { text: `message ${n}` },
+ scopeLabel: scope,
+ })),
+ );
+ const records = entries.map((entry) => ({
+ kind: "message" as const,
+ payload: { text: `mirror ${entry.seq}` },
+ scopeLabel: scope,
+ entrySeq: entry.seq,
+ meta: {
+ overheard: true,
+ sourceRole: "agent" as const,
+ bareText: "hello",
+ ts: "123.4",
+ changeTime: "125.6",
+ author: "Ada",
+ attachments: [{ name: "a.txt" }],
+ hidden: true,
+ securityTainted: true,
+ entryCreatedAt: entry.createdAt,
+ display: "hidden",
+ },
+ }));
+ const appended = await store.appendTapeMany(lease, records);
+ assert.deepEqual(
+ appended.map((row) => row.seq),
+ [5, 6, 7, 8, 9],
+ );
+ assert.deepEqual((await store.getTape(session.id)).slice(-5), appended);
+ assert.deepEqual(await store.appendTapeMany(lease, []), []);
+ if (backend === "postgres") {
+ const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
+ try {
+ await store.appendTapeMany(lease, [
+ { ...records[0]!, meta: { bareText: "bad\ud800", author: "bad\udc00", attachments: ["literal\\ud800"] } },
+ ]);
+ const last = (await store.getTape(session.id)).at(-1)!;
+ assert.equal(last.meta?.bareText, "bad�");
+ assert.equal(last.meta?.author, "bad�");
+ assert.deepEqual(last.meta?.attachments, ["literal\\ud800"]);
+ const constraint = `tape_batch_${randomUUID().replaceAll("-", "")}`;
+ await pool.query(
+ `ALTER TABLE session_tape ADD CONSTRAINT ${constraint} CHECK (session_id <> '${session.id}' OR seq <> 12)`,
+ );
+ try {
+ await assert.rejects(store.appendTapeMany(lease, records));
+ assert.equal((await store.getTape(session.id)).length, 11);
+ } finally {
+ await pool.query(`ALTER TABLE session_tape DROP CONSTRAINT ${constraint}`);
+ }
+ } finally {
+ await pool.end();
+ }
+ }
+ await store.releaseLease(lease);
+ await assert.rejects(store.appendTapeMany(lease, records), /lease/);
+ await assert.rejects(store.appendTapeMany(lease, []), /lease/);
+ }
+ const recent = await store.getRecentEntries([...ids, "missing", ids[0]!], 2);
+ for (const id of ids) assert.deepEqual(recent.get(id), await store.getEntries(id, { sinceSeq: 2 }));
+ assert.deepEqual(recent.get("missing"), []);
+ assert.deepEqual(await store.getRecentEntries([], 2), new Map());
+ },
+ );
+
+ test(`latest runs batch matches single reads including private messages and ties: ${backend}`, { skip }, async () => {
+ const runtime = backend === "memory" ? createMemoryRunStore() : createPostgresRunStore(process.env.DATABASE_URL!);
+ const { runs } = runtime;
+ const ids = [`batch:${randomUUID()}`, `batch:${randomUUID()}`];
+ const actor = { id: "internal:batch", type: "internal" as const };
+ try {
+ for (const id of ids)
+ for (const privateSessionMessage of [false, true])
+ await runs.enqueue({
+ sessionId: id,
+ request: {
+ actor,
+ conversation: { kind: "dm", threadRef: id, audience: [actor] },
+ origin: { kind: "direct" },
+ text: "batch",
+ ...(privateSessionMessage ? { privateSessionMessage: true as const } : {}),
+ },
+ });
+ for (const excludePrivateMessages of [false, true]) {
+ const batch = await runs.latestForThreads([...ids, "missing", ids[0]!], { excludePrivateMessages });
+ assert.equal(batch.size, 2);
+ for (const id of ids)
+ assert.deepEqual(batch.get(id), await runs.latestForThread(id, { excludePrivateMessages }));
+ }
+ assert.deepEqual(await runs.latestForThreads([]), new Map());
+ } finally {
+ await runs.close?.();
+ }
+ });
+}
diff --git a/test/connectors.test.ts b/test/connectors.test.ts
index 6bd9a5fc2..553f90653 100644
--- a/test/connectors.test.ts
+++ b/test/connectors.test.ts
@@ -283,6 +283,9 @@ test("connector token keys are Postgres-safe (no NUL byte) — round-trip throug
seenKeys.push(k);
};
const creds: DurableMap<KeychainCredential> = {
+ async mutateMany() {
+ throw new Error("unused in connector token test");
+ },
async all() {
return [...store.values()];
},
diff --git a/test/loop-ledger.test.ts b/test/loop-ledger.test.ts
index fbc53f6de..00ee57ed5 100644
--- a/test/loop-ledger.test.ts
+++ b/test/loop-ledger.test.ts
@@ -433,3 +433,48 @@ test("flagged summaries classify payload-only legacy email without changing stor
assert.deepEqual(await ledger.summaries([LOOP]), before);
assert.equal((await ledger.byLoop(LOOP))[0]!.source, undefined);
});
+
+test("ingest batches per-loop locks and writes while resolving legacy IDs and duplicate events", async () => {
+ const backing = createMemoryMap<LoopItem>();
+ const seed = createLoopItemLedger(backing);
+ await seed.ingest([entry()]);
+ const original = (await seed.byLoop(LOOP))[0]!;
+ await backing.delete(original.id);
+ await backing.put("legacy", { ...original, id: "legacy" });
+ let selects = 0;
+ let writes = 0;
+ let locks = 0;
+ const ledger = createLoopItemLedger(
+ {
+ ...backing,
+ select: async (query) => {
+ selects++;
+ return backing.select(query);
+ },
+ get: async () => {
+ throw new Error("per-item read");
+ },
+ mutateMany: async (updates) => {
+ writes++;
+ return backing.mutateMany(updates);
+ },
+ },
+ undefined,
+ {
+ lock: {
+ withLock: async (_key, fn) => {
+ locks++;
+ return fn();
+ },
+ },
+ accepts: async () => true,
+ },
+ );
+ const batch = Array.from({ length: 100 }, (_, i) => entry({ dedupeKey: `new:${i}` }));
+ const outcome = await ledger.ingest([entry(), ...batch, ...batch]);
+ assert.deepEqual(outcome, { created: 100, updated: 0, skipped: 101 });
+ assert.deepEqual({ selects, writes, locks }, { selects: 1, writes: 1, locks: 1 });
+ assert.equal((await backing.all()).length, 101);
+ assert.ok(await backing.get("legacy"));
+ assert.equal(await backing.get(original.id), null);
+});
diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.ts
index 90331bfc1..7d3b0625b 100644
--- a/test/orchestrator.test.ts
+++ b/test/orchestrator.test.ts
@@ -2194,7 +2194,20 @@ test("priorTurns are routed to the harness as structured roled turns (PR3)", asy
});
test("overheard messages are imported ONCE into the durable log, author-labeled, and handed to the harness", async () => {
- const { app } = freshApp();
+ const { app, sessions } = freshApp();
+ const entryBatches: number[] = [];
+ const tapeBatches: number[] = [];
+ const appendMany = sessions.appendMany.bind(sessions);
+ const appendTapeMany = sessions.appendTapeMany.bind(sessions);
+ sessions.appendMany = async (lease, entries) => {
+ if (entries.some((entry) => (entry.payload as { overheard?: boolean })?.overheard))
+ entryBatches.push(entries.length);
+ return appendMany(lease, entries);
+ };
+ sessions.appendTapeMany = async (lease, records) => {
+ if (records.some((record) => record.meta?.overheard)) tapeBatches.push(records.length);
+ return appendTapeMany(lease, records);
+ };
const overheardEntries = (entries: { type: string; payload: unknown }[]) =>
entries.filter((e) => e.type === "user" && (e.payload as { overheard?: boolean }).overheard === true);
@@ -2243,6 +2256,8 @@ test("overheard messages are imported ONCE into the durable log, author-labeled,
["100.001", "100.002", "100.003", "100.004"],
"append-only: each message recorded exactly once",
);
+ assert.deepEqual(entryBatches, [3, 1]);
+ assert.deepEqual(tapeBatches, [3, 1]);
});
test("a message answered on one turn is not re-imported as overheard on the next (full-stack dedupe)", async () => {
diff --git a/test/sessions-working.test.ts b/test/sessions-working.test.ts
index 4c9c8572d..d7df37630 100644
--- a/test/sessions-working.test.ts
+++ b/test/sessions-working.test.ts
@@ -91,3 +91,66 @@ test("listSessions flags a subagent whose latest turn failed, and only while it
await runs.complete(retry!.id, retry!.leaseToken ?? "", { status: "ok", reply: "fine" });
assert.ok(!(await app.listSessions("U1")).find((s) => s.id === child.sessionId)?.lastTurnFailed);
});
+
+test("sidebar batches visible goals and preserves the lookback boundary and inactive overrides", async () => {
+ const { app, sessions, runs } = freshApp();
+ const visible: string[] = [];
+ const refs: string[] = [];
+ for (const [index, owner] of ["U1", "U1", "hidden"].entries()) {
+ const ref = `web:${owner}:batch:${index}`;
+ const session = await sessions.getOrCreateByThread(ref, "dm", `personal:${owner}`);
+ await sessions.addParticipant(session.id, owner);
+ const { lease } = await sessions.acquireLease(session.id);
+ assert.ok(lease);
+ const goal = {
+ objective: `goal ${index}`,
+ status: "active",
+ tokensUsed: 0,
+ createdAt: 1,
+ updatedAt: 1,
+ activeMs: 123,
+ };
+ await sessions.appendMany(lease, [
+ { type: "system", payload: { kind: "goal", goal }, scopeLabel: session.scopeId },
+ ...Array.from({ length: index === 1 ? 1999 : 2000 }, () => ({
+ type: "user" as const,
+ payload: { text: "filler" },
+ scopeLabel: session.scopeId,
+ })),
+ ...(index === 1
+ ? [
+ {
+ type: "system" as const,
+ payload: { kind: "goal", goal: { ...goal, status: "complete" } },
+ scopeLabel: session.scopeId,
+ },
+ ]
+ : []),
+ ]);
+ await sessions.releaseLease(lease);
+ await runs.enqueue({ sessionId: ref, request: enqueueRequest() });
+ if (owner === "U1") {
+ visible.push(session.id);
+ refs.push(ref);
+ }
+ }
+ const recent = sessions.getRecentEntries.bind(sessions);
+ const latest = runs.latestForThreads.bind(runs);
+ let reads = 0;
+ let runReads = 0;
+ sessions.getRecentEntries = async (ids, lookback) => {
+ reads++;
+ assert.deepEqual(new Set(ids), new Set(visible));
+ return recent(ids, lookback);
+ };
+ runs.latestForThreads = async (ids, opts) => {
+ runReads++;
+ assert.deepEqual(new Set(ids), new Set(refs));
+ return latest(ids, opts);
+ };
+ const list = await app.listSessions("U1");
+ assert.deepEqual({ reads, runReads }, { reads: 1, runReads: 1 });
+ assert.equal(list.length, 2);
+ assert.equal(list.find((row) => row.id === visible[0])?.goal?.objective, "goal 0");
+ assert.equal(list.find((row) => row.id === visible[1])?.goal, undefined);
+});
diff --git a/test/tape-nudge-continuation.test.ts b/test/tape-nudge-continuation.test.ts
index fc7384ba6..024a1ef2b 100644
--- a/test/tape-nudge-continuation.test.ts
+++ b/test/tape-nudge-continuation.test.ts
@@ -540,10 +540,10 @@ test("overheard imports are this turn's own witnessed appends: served, watermark
test("a failed overheard mirror fails the turn loudly; the next turn's read heals the gap", async () => {
const { modes, folds, sessions, session, orchestrator, input } = await runScenario();
- const realAppendTape = sessions.appendTape.bind(sessions);
- sessions.appendTape = async (lease, rec) => {
- if (rec.kind === "message" && rec.meta?.overheard) throw new Error("mirror down");
- return realAppendTape(lease, rec);
+ const realAppendTapeMany = sessions.appendTapeMany.bind(sessions);
+ sessions.appendTapeMany = async (lease, records) => {
+ if (records.some((rec) => rec.kind === "message" && rec.meta?.overheard)) throw new Error("mirror down");
+ return realAppendTapeMany(lease, records);
};
await assert.rejects(
orchestrator.handleTurn(
@@ -553,7 +553,7 @@ test("a failed overheard mirror fails the turn loudly; the next turn's read heal
),
/mirror down/,
);
- sessions.appendTape = realAppendTape;
+ sessions.appendTapeMany = realAppendTapeMany;
const withheld = await sessions.getEntries(session.id);
assert.ok(
(await sessions.tapeCoverage(session.id)) < withheld.at(-1)!.seq,
diff --git a/test/turn-bookkeeping-failures.test.ts b/test/turn-bookkeeping-failures.test.ts
index 71c5b50e1..f00a9c71c 100644
--- a/test/turn-bookkeeping-failures.test.ts
+++ b/test/turn-bookkeeping-failures.test.ts
@@ -206,25 +206,40 @@ test("a cancel-stopped turn still persists and surfaces its pending approvals",
test("an overheard import failure aborts the batch instead of skipping one message", async () => {
const { orchestrator, sessions, input } = buildScenario();
await orchestrator.handleTurn(input("prime"));
- const append = sessions.append.bind(sessions);
- sessions.append = async (lease, entry) => {
- const payload = entry.payload as { overheard?: unknown; ts?: unknown } | null;
- if (payload?.overheard === true && payload.ts === "200.2") throw new Error("append refused");
- return append(lease, entry);
+ const appendMany = sessions.appendMany.bind(sessions);
+ sessions.appendMany = async (lease, entries) => {
+ if (
+ entries.some((entry) => {
+ const payload = entry.payload as { overheard?: unknown; ts?: unknown } | null;
+ return payload?.overheard === true && payload.ts === "200.2";
+ })
+ )
+ throw new Error("append refused");
+ return appendMany(lease, entries);
};
- const result = await orchestrator.handleTurn(
- input("what did I miss?", {
- overheard: [
- { role: "user", name: "Ann", text: "first overheard", ts: "100.1" },
- { role: "user", name: "Bob", text: "second overheard", ts: "200.2" },
- { role: "user", name: "Cee", text: "third overheard", ts: "300.3" },
- ],
- }),
- );
+ const request = input("what did I miss?", {
+ overheard: [
+ { role: "user", name: "Ann", text: "first overheard", ts: "100.1" },
+ { role: "user", name: "Bob", text: "second overheard", ts: "200.2" },
+ { role: "user", name: "Cee", text: "third overheard", ts: "300.3" },
+ ],
+ });
+ const result = await orchestrator.handleTurn(request);
assert.equal(result.status, "ok");
const session = (await sessions.getByThread(conversation.threadRef))!;
const overheardTexts = (await sessions.getEntries(session.id))
.filter((e) => (e.payload as { overheard?: unknown } | null)?.overheard === true)
.map((e) => (e.payload as { text?: string }).text);
- assert.deepEqual(overheardTexts, ["first overheard"], "the batch stops at the failure; nothing lands out of order");
+ assert.deepEqual(overheardTexts, [], "the atomic batch leaves no partial history on failure");
+ sessions.appendMany = appendMany;
+ await orchestrator.handleTurn(request);
+ await orchestrator.handleTurn(request);
+ const recovered = (await sessions.getEntries(session.id))
+ .filter((e) => (e.payload as { overheard?: unknown } | null)?.overheard === true)
+ .map((e) => (e.payload as { text?: string }).text);
+ assert.deepEqual(
+ recovered,
+ ["first overheard", "second overheard", "third overheard"],
+ "retry imports the whole batch once, in order",
+ );
});