Multiplayer agent harness for work.
Repair legacy Inbox source migration for Email and Slack (#1490)
Regan Belld402fd88a3d8
diff --git a/src/api/routes/inbox.ts b/src/api/routes/inbox.tsindex 25db73fe3..7ab9b479e 100644--- a/src/api/routes/inbox.ts+++ b/src/api/routes/inbox.ts@@ -29,6 +29,7 @@ async function inbox(ctx: ApiCtx): Promise<void> {});const available: Loop[] = [];for (const loop of await deps.store.list()) {+ if (loop.id === legacy?.id && migrated && legacyItems.length === 0) continue;if (await canAdministerLoop(ctx, loop, acting)) available.push(loop);}let ids = Array.isArray(pref.value) ? pref.value.filter((value): value is string => typeof value === "string") : [];diff --git a/src/loops/inbox-migration.ts b/src/loops/inbox-migration.tsindex ccc1a1baa..eb4855309 100644--- a/src/loops/inbox-migration.ts+++ b/src/loops/inbox-migration.ts@@ -1,3 +1,4 @@+import { adapterForItem } from "./sources/index.ts";import { DECISION_LEASE_MS } from "./item-ledger.ts";import type { Loop } from "../types.ts";import type { LoopServiceDeps } from "../api/routes/loops.ts";@@ -8,6 +9,7 @@ import { renderSourceInboxTask } from "./inbox-loop.ts";interface Migration {phase: "pending" | "complete";enabled: boolean;+ sourcePayloadMigrated?: boolean;}async function migrateInboxLocked(@@ -23,9 +25,12 @@ async function migrateInboxLocked(updatedAt: Date.now(),});const migration = record.value as Migration;- if (migration.phase === "complete") return true;- await deps.store.setState(legacy.id, "paused");- if (cron) await deps.crons!.setEnabled(cron.id, false);+ const repair = migration.phase === "complete";+ if (repair && migration.sourcePayloadMigrated) return true;+ if (!repair) {+ await deps.store.setState(legacy.id, "paused");+ if (cron) await deps.crons!.setEnabled(cron.id, false);+ }if (cron && (await deps.crons!.listFires(cron.id)).runs.some((fire) => fire.status === "running")) return false;const items = await deps.items.byLoop(legacy.id);if (@@ -45,6 +50,7 @@ async function migrateInboxLocked(for (const item of await deps.items.byLoop(loop.id)) {if (item.previousLoopId === legacy.id) await deps.outputs.rebindItem(item.id, legacy.id, loop.id);}+ if (repair) continue;if (cron && deps.crons) {const current = await deps.store.get(loop.id);const replacement =@@ -67,13 +73,12 @@ async function migrateInboxLocked(}if (!migration.enabled) await deps.store.setState(loop.id, "paused");}- if ((await deps.items.byLoop(legacy.id)).some((item) => item.source === "gmail" || item.source === "slack"))- return false;for (const output of await deps.outputs.byLoop(legacy.id)) {const item = await deps.items.get(output.itemId);if (item?.previousLoopId === legacy.id && item.loopId !== legacy.id) return false;}- if (migration.enabled) {+ if ((await deps.items.byLoop(legacy.id)).some((item) => adapterForItem(item))) return repair;+ if (!repair && migration.enabled) {for (const loop of defaults) {const current = await deps.store.get(loop.id);if (current?.cronId && current.state === "enabled") await deps.crons?.setEnabled(current.cronId, true);@@ -81,7 +86,10 @@ async function migrateInboxLocked(}if (cron) await deps.crons!.update(cron.id, { archived: true, enabled: false });await deps.store.setState(legacy.id, "archived");- await preferences.put(key, { value: { ...migration, phase: "complete" }, updatedAt: Date.now() });+ await preferences.put(key, {+ value: { ...migration, phase: "complete", sourcePayloadMigrated: true },+ updatedAt: Date.now(),+ });return true;}diff --git a/src/loops/item-ledger.ts b/src/loops/item-ledger.tsindex 3402f62dd..9e0b1e576 100644--- a/src/loops/item-ledger.ts+++ b/src/loops/item-ledger.ts@@ -242,9 +242,8 @@ export function createLoopItemLedger(async moveSource(from, to, source) {const targets = await forLoop(to);for (const item of await forLoop(from)) {- if (item.source !== source) continue;- if (targets.some((target) => target.sourceKey === item.sourceKey && target.id !== item.id))- throw new Error("Inbox migration found conflicting source records");+ if ((item.source ?? item.sourcePayload?.source) !== source) continue;+ if (targets.some((target) => target.sourceKey === item.sourceKey && target.id !== item.id)) continue;await update(item.id, (current) => {if (current.loopId !== from ||@@ -252,7 +251,13 @@ export function createLoopItemLedger((current.decisionToken && (current.decisionAt ?? 0) + DECISION_LEASE_MS > Date.now()))return current;- return { ...current, loopId: to, previousLoopId: from, inboxPreview: inboxPreview(current.sourcePayload) };+ return {+ ...current,+ source,+ loopId: to,+ previousLoopId: from,+ inboxPreview: inboxPreview(current.sourcePayload),+ };});}},diff --git a/test/inbox-selection.test.ts b/test/inbox-selection.test.tsindex b2c431e47..9bde1626b 100644--- a/test/inbox-selection.test.ts+++ b/test/inbox-selection.test.ts@@ -9,6 +9,7 @@ import { createShipGrantStore } from "../src/loops/ship-grant-store.ts";import { createCronStore } from "../src/cron/cron-store.ts";import { createMemoryConfigStore } from "../src/resolution/config-store.ts";import { createMemoryMap } from "../src/persistence/durable-map.ts";+import { uiStateId } from "../src/surfaces/ui-state.ts";import type { PersistedUiState } from "../src/surfaces/ui-state.ts";import type { ApiCtx } from "../src/api/routes/route.ts";import { ensureDefaultInboxLoops, ensureInboxLoop } from "../src/loops/inbox-loop.ts";@@ -195,7 +196,8 @@ test("moving records refuses collisions and a cached worker cannot claim a movedassert.equal(await w.deps.items.claim(item!.id, undefined, legacy.id), null);await w.deps.items.ingest([{ loopId: legacy.id, dedupeKey: "collision", source: "gmail", sourcePayload: {} }]);await w.deps.items.ingest([{ loopId: target!.id, dedupeKey: "collision", source: "gmail", sourcePayload: {} }]);- await assert.rejects(w.deps.items.moveSource(legacy.id, target!.id, "gmail"), /conflicting/);+ await w.deps.items.moveSource(legacy.id, target!.id, "gmail");+ assert.equal((await w.deps.items.byLoop(legacy.id)).length, 1);});test("disabled rollout cannot create defaults, migrate legacy Inbox, or update selection", async () => {@@ -235,3 +237,138 @@ test("Sent chat items stay out of Inbox counts and handled history remains reada[item.id],);});++for (const completed of [false, true]) {+ test(`payload-only source migration repairs ${completed ? "completed" : "pending"} accounts without losing drafts`, async () => {+ const w = world();+ const legacy = await ensureInboxLoop(w.deps.store, "alice");+ const defaults = await ensureDefaultInboxLoops(w.deps.store, "alice");+ const cron = await w.deps.crons.create({+ owner: "alice",+ createdBy: "alice",+ ownerScopeId: "personal:alice",+ schedule: { everyMs: 900000 },+ enabled: false,+ });+ await w.deps.store.update(legacy.id, { cronId: cron.id });+ if (completed) {+ await w.deps.store.setState(legacy.id, "archived");+ await w.uiState.put(uiStateId("alice", "inbox-migration"), {+ value: { phase: "complete", enabled: false },+ updatedAt: Date.now(),+ });+ }+ await w.uiState.put(uiStateId("alice", "inbox-loops"), {+ value: [...defaults.map((loop) => loop.id), legacy.id],+ updatedAt: Date.now(),+ });+ if (completed) {+ for (const [index, loop] of defaults.entries()) {+ const sourceCron = await w.deps.crons.create({+ loopId: loop.id,+ owner: "alice",+ createdBy: "alice",+ ownerScopeId: "personal:alice",+ schedule: { everyMs: 120000 + index * 60000 },+ enabled: index === 0,+ runAs: "owner",+ });+ await w.deps.store.update(loop.id, { cronId: sourceCron.id, runAs: "owner" });+ if (index === 1) await w.deps.store.setState(loop.id, "paused");+ }+ }+ const beforeCrons = await Promise.all(+ defaults.map(async (loop) => {+ const current = await w.deps.store.get(loop.id);+ return current?.cronId ? w.deps.crons.get(current.cronId) : null;+ }),+ );+ const beforeDefaults = await Promise.all(defaults.map((loop) => w.deps.store.get(loop.id)));+ for (const source of ["gmail", "slack"]) {+ await w.deps.items.ingest([+ {+ loopId: legacy.id,+ dedupeKey: source,+ sourcePayload: { source, title: `${source} draft` },+ proposal: { by: "human", data: { body: "Keep my words" } },+ },+ ]);+ }+ const originals = await w.deps.items.byLoop(legacy.id);+ for (const item of originals) {+ await w.deps.items.appendThread(item.id, [{ role: "human", text: "Preserve history" }]);+ await w.deps.outputs.capture({+ loopId: legacy.id,+ itemId: item.id,+ attemptId: "a",+ shipAction: "send",+ title: "Draft",+ capturedBy: "agent",+ });+ }+ const result = (await w.call()).data;+ assert.equal(result.migrationPending, false);+ assert.equal(result.total, 2);+ assert.deepEqual(+ result.selected.map((loop: any) => loop.id),+ defaults.map((loop) => loop.id),+ );+ for (const original of originals) {+ const item = (await w.deps.items.get(original.id))!;+ assert.equal(item.source, original.sourcePayload!.source);+ assert.equal(item.loopId, defaults.find((loop) => loop.sources![0] === item.source)!.id);+ assert.equal(item.proposal!.data.body, "Keep my words");+ assert.equal(item.thread![0]!.text, "Preserve history");+ assert.equal((await w.deps.outputs.byItem(item.id))[0]!.loopId, item.loopId);+ assert.equal(result.items.find((entry: any) => entry.id === item.id).source, item.source);+ }+ if (completed)+ assert.deepEqual(await Promise.all(defaults.map((loop) => w.deps.store.get(loop.id))), beforeDefaults);+ if (completed)+ assert.deepEqual(+ await Promise.all(+ defaults.map(async (loop) => {+ const current = await w.deps.store.get(loop.id);+ return current?.cronId ? w.deps.crons.get(current.cronId) : null;+ }),+ ),+ beforeCrons,+ );+ assert.deepEqual((await w.call()).data.items, result.items);+ assert.equal((await w.deps.items.byLoop(legacy.id)).length, 0);+ });+}++test("completed repair keeps conflicting drafts visible while moving unrelated items", async () => {+ const w = world();+ const legacy = await ensureInboxLoop(w.deps.store, "alice");+ const defaults = await ensureDefaultInboxLoops(w.deps.store, "alice");+ await w.deps.store.setState(legacy.id, "archived");+ await w.uiState.put(uiStateId("alice", "inbox-migration"), {+ value: { phase: "complete", enabled: true },+ updatedAt: Date.now(),+ });+ for (const [loopId, key, body] of [+ [legacy.id, "collision", "Human draft"],+ [defaults[0]!.id, "collision", "New draft"],+ [legacy.id, "unrelated", "Other draft"],+ ]) {+ await w.deps.items.ingest([+ {+ loopId: loopId!,+ dedupeKey: key!,+ sourcePayload: { source: "gmail", title: body },+ proposal: { by: "human", data: { body } },+ },+ ]);+ }+ for (let attempt = 0; attempt < 2; attempt++) {+ const result = await w.call();+ assert.equal(result.status, 200);+ assert.equal(result.data.migrationPending, false);+ assert.equal(result.data.total, 3);+ assert.equal(result.data.selected.find((loop: any) => loop.id === legacy.id).count, 1);+ assert.equal(result.data.selected.find((loop: any) => loop.id === defaults[0]!.id).count, 2);+ assert.equal((await w.deps.items.byLoop(legacy.id))[0]!.proposal!.data.body, "Human draft");+ }+});