import { assert, assertEquals } from "@std/assert"; import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context"; import type { ConversationView } from "@earendil-works/pi-durable"; import { fauxAssistantMessage, fauxToolCall } from "@earendil-works/pi-ai/providers/faux"; import { Type } from "@earendil-works/pi-ai"; import { defineExtension, type JsonValue } from "@berry/api"; import { setup } from "./fixture.ts"; Deno.test("joining a running tool receives its execution start independently of progress details", async () => { await using harness = await setup(); const { berry, model, faux } = harness; const entered = Promise.withResolvers<(details: JsonValue) => void>(); const release = Promise.withResolvers(); await berry.install(defineExtension({ name: "timed", initial: () => null, setup(ext) { ext.tool({ name: "wait", description: "wait", expose: "model", parameters: Type.Object({}), execute(_, ctx) { entered.resolve(ctx.progress); return release.promise; }, }); }, })); const tid = await berry.create({ extensions: { timed: {} }, model }); const conversation = (await berry.conversation(tid))!; faux.setResponses([ fauxAssistantMessage(fauxToolCall("wait", {}, { id: "waiting" }), { stopReason: "toolUse" }), fauxAssistantMessage("done"), ]); const before = Date.now(); const submission = await conversation.submit({ type: "input", content: "go" }, context); try { const progress = await entered.promise; const feed = (await berry.watch(tid))!; const slot = (view: unknown) => ((view as ConversationView).docs["pi.live"].tools as { callId: string; startedAt?: number; details?: JsonValue; }[]).find((slot) => slot.callId === "waiting")!; const start = slot(feed.value).startedAt; assert(typeof start === "number" && start >= before && start <= Date.now()); const updated = Promise.withResolvers(); await feed.start((view) => { const current = slot(view); if (Array.isArray(current?.details)) { assertEquals(current.startedAt, start); assertEquals(current.details, ["still running"]); updated.resolve(); } return Promise.resolve(); }); try { progress(["still running"]); await updated.promise; const rejoined = (await berry.watch(tid))!; assertEquals(slot(rejoined.value).startedAt, start); await rejoined.stop(); } finally { await feed.stop(); } } finally { release.resolve("done"); await submission.wait(context); await conversation.waitForIdle(context); } });