Multiplayer agent harness for work.
fix: preserve attachments in completed chat history (#1068)
Joshua Francef2fb24007d43
diff --git a/src/harness/tape-projection.ts b/src/harness/tape-projection.tsindex 717f863c4..b6472e4eb 100644--- a/src/harness/tape-projection.ts+++ b/src/harness/tape-projection.ts@@ -313,6 +313,7 @@ export function projectTapeEntries(});}} else if (message.role === "toolResult") {+ if (message.toolName === "attach") return null;const draft = toolResultDraft(row);if (draft) events.push({ kind: "item", item: draft });} else {diff --git a/test/pi-harness-tape-sync.test.ts b/test/pi-harness-tape-sync.test.tsindex bc0874caa..668c7a1d7 100644--- a/test/pi-harness-tape-sync.test.ts+++ b/test/pi-harness-tape-sync.test.ts@@ -186,3 +186,70 @@ test("a lease lost mid-turn fails the turn instead of degrading silently", asyncglobalThis.fetch = realFetch;}});++test("completed Pi attach results retain openable files in viewer history", async () => {+ const { createTranscriptSource } = await import("../src/harness/tape-projection.ts");+ const store = createMemorySessionStore();+ const scopeLabel = "personal:tester" as ScopeId;+ const session = await store.getOrCreateByThread("web:tester:attach-history", "dm", scopeLabel);+ await store.addParticipant(session.id, "tester", undefined, { includeHistory: true });+ const { lease } = await store.acquireLease(session.id);+ assert.ok(lease);+ const files = [+ { name: "desktop.png", mimetype: "image/png", sizeBytes: 123, artifactId: "desktop" },+ { name: "phone.png", mimetype: "image/png", sizeBytes: 456, artifactId: "phone" },+ { name: "preview.html", mimetype: "text/html", sizeBytes: 789, artifactId: "preview" },+ ];+ const harness = createPiHarness({ apiKey: "sk-test" });+ const realFetch = globalThis.fetch;+ let calls = 0;+ globalThis.fetch = (async () => {+ if (calls++ > 0) return sse(textReplyEvents("Here are the previews."));+ const events = textReplyEvents("");+ events[1] = {+ type: "content_block_start",+ index: 0,+ content_block: { type: "tool_use", id: "attach-preview", name: "attach", input: {} },+ };+ events[2] = {+ type: "content_block_delta",+ index: 0,+ delta: { type: "input_json_delta", partial_json: JSON.stringify({ files: files.map((f) => f.name) }) },+ };+ events[4] = { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 3 } };+ return sse(events);+ }) as typeof globalThis.fetch;+ try {+ const result = await harness.turns.runTurn(+ turnInput(+ session.id,+ { entries: [], tape: [] },+ {+ session,+ tools: {+ attach: async () => ({ ok: true, files, staged: files.length }),+ } as unknown as HarnessTurnInput["tools"],+ emit: (entry) => store.append(lease, entry),+ tape: async (rec) => {+ await store.appendTape(lease, rec);+ },+ },+ ),+ );+ assert.equal(result.reply, "Here are the previews.");+ assert.equal(calls, 2);+ const original = (await store.getEntries(session.id)).find((e) => e.type === "tool_result");+ assert.ok(original);+ assert.deepEqual((original.payload as { files?: unknown[] }).files, files);+ const source = createTranscriptSource(store);+ for (const read of [await source.forRender(session.id), await source.forViewer(session.id, "tester")]) {+ const restored = read.entries.find((e) => e.type === "tool_result");+ assert.ok(restored);+ assert.deepEqual((restored.payload as { files?: unknown[] }).files, files);+ assert.deepEqual(restored, original);+ }+ } finally {+ globalThis.fetch = realFetch;+ await store.releaseLease(lease);+ }+});diff --git a/test/tape-projection.test.ts b/test/tape-projection.test.tsindex b80c196f1..892705e62 100644--- a/test/tape-projection.test.ts+++ b/test/tape-projection.test.ts@@ -36,6 +36,7 @@ interface SimCall {result: string;isError?: boolean;resultScope?: ScopeId;+ resultPayload?: Record<string, unknown>;}interface SimStep {@@ -128,7 +129,7 @@ async function simTurn(sim: Sim, turn: SimTurn): Promise<void> {await emit("tool_call", { ...c.args, tool: c.name, callId: c.id });await emit("tool_result",- { tool: c.name, callId: c.id, isError: c.isError === true, result: c.result },+ { ...c.resultPayload, tool: c.name, callId: c.id, isError: c.isError === true, result: c.result },c.resultScope ?? scope,);await tape({@@ -1073,3 +1074,75 @@ test("model-facing glue user rows (goal notes, env-only nudges) never render", aassert.equal(userRows.length, 1);assert.equal((userRows[0]!.payload as { text: string }).text, "work on it");});++for (const failed of [false, true]) {+ test(`attach transcript preserves structured results after completion (failed=${failed})`, async () => {+ const sim = await simSession();+ await sim.store.addParticipant(sim.session.id, "[email protected]", undefined, { includeHistory: true });+ const files = [+ { name: "desktop.png", mimetype: "image/png", sizeBytes: 123, artifactId: "desktop" },+ { name: "phone.png", mimetype: "image/png", sizeBytes: 456, artifactId: "phone" },+ { name: "preview.html", mimetype: "text/html", sizeBytes: 789, artifactId: "preview" },+ ];+ await simTurn(sim, {+ input: "show the preview",+ steps: [+ {+ calls: [+ {+ id: "attach-1",+ name: "attach",+ args: { files: files.map((f) => f.name) },+ result: failed ? "[not attached] file not found" : "[attached] preview files",+ isError: failed,+ resultPayload: { ok: !failed, ...(failed ? {} : { files }) },+ },+ ],+ },+ ],+ reply: failed ? "the file is missing" : "here is the preview",+ });+ const source = createTranscriptSource(sim.store);+ const expected = await sim.store.getEntries(sim.session.id);+ assert.deepEqual((await source.forRender(sim.session.id)).entries, expected);+ assert.deepEqual((await source.forRender(sim.session.id, { limit: 3 })).entries, expected.slice(-3));+ assert.deepEqual((await source.forRender(sim.session.id, { sinceSeq: 2 })).entries, expected.slice(2));+ assert.deepEqual((await source.forViewer(sim.session.id, "[email protected]", { limit: 3 })).entries, expected);+ assert.deepEqual((await source.forViewer(sim.session.id, "[email protected]")).entries, []);+ });+}++test("attachment fallback preserves participant windows and repeated calls in an anchored tail", async () => {+ const sim = await simSession();+ await sim.store.addParticipant(sim.session.id, "early", undefined, { includeHistory: true });+ for (let i = 0; i < 60; i++) await simTurn(sim, { input: `question ${i}`, reply: `answer ${i}` });+ await sim.store.addParticipant(sim.session.id, "late");+ await simTurn(sim, {+ input: "attach twice",+ steps: [+ {+ calls: ["old", "new"].map((artifactId) => ({+ id: artifactId,+ name: "attach",+ args: { files: ["image.png"] },+ result: "[attached] image.png",+ resultPayload: { ok: true, files: [{ name: "image.png", mimetype: "image/png", sizeBytes: 12, artifactId }] },+ })),+ },+ ],+ reply: "latest image",+ });+ await sim.store.removeParticipant(sim.session.id, "early");+ await simTurn(sim, { input: "private follow-up", reply: "later" });+ const source = createTranscriptSource(sim.store);+ assert.deepEqual(+ (await source.forRender(sim.session.id, { limit: 8 })).entries,+ await sim.store.getEntries(sim.session.id, { limit: 8 }),+ );+ for (const viewer of ["early", "late", "stranger"]) {+ assert.deepEqual(+ (await source.forViewer(sim.session.id, viewer, { limit: 8 })).entries,+ await sim.store.visibleEntries(sim.session.id, viewer),+ );+ }+});