Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion apps/server/src/chat/turn-handover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import { openPlannerSession } from "../harness/session";
import * as Plan from "../plan/service";
import { configured } from "../testing/config";
import {
finishTurn,
LINK_PERSISTENCE,
linkAuth,
linkServer,
MEMBER_TOKEN,
rawLink,
} from "../testing/agent-link";
import { connectAgent } from "@chopin/agent-link/client";
import * as Chat from "./service";

import type { Server } from "bun";
import type { AgentHandlers, TurnContext } from "@chopin/agent-link/client";
import type { HostedAuth } from "../auth/routes";
import type { Lease, PlannerLinkHold } from "../storage/model";
import type { Socket, SocketData } from "../wire";
Expand Down Expand Up @@ -315,7 +318,7 @@ async function cutProcess(

/** The next process: it takes the lease, rebuilds the holds and opens the document. */
async function takeOver(
d: Deployed,
d: { linked: Linked },
options: { failHoldWrites?: () => boolean; name?: string } = {},
) {
let { linked } = d;
Expand Down Expand Up @@ -812,6 +815,63 @@ describe("a remote turn Chat cannot go on with", () => {
});
});

describe("the reference client across a deploy", () => {
it("keeps its turn running, takes it back from the next process, and the reply ends once, whole", async () => {
let linked = await linkAuth(undefined, { persistent: true });
let first = await startProcess(linked, "first");
let gate = Promise.withResolvers<void>();
let edited: unknown;
let handlers: AgentHandlers = {
async turn(turn: TurnContext) {
turn.emit({ type: "text-start", id: "t1" });
turn.emit({ type: "text-delta", id: "t1", delta: "Writing" });
edited = await turn.callHostTool("edit_plan", {
revision: first.plan.revision,
operations: [{ op: "replace_root", source: "# Plan\n\nApplied once.\n" }],
});
// Still running when the deploy begins, and after the next process takes it back.
await gate.promise;
turn.emit({ type: "text-delta", id: "t1", delta: " more." });
turn.emit({ type: "text-end", id: "t1" });
finishTurn(turn);
},
};
let client = await connectAgent({
url: first.server.url,
token: MEMBER_TOKEN,
document: linked.channel.id,
handlers,
resumable: true,
});
expect(client.features).toContain("turn-handover");
await first.send("@chopin write it");
await until(() => edited !== undefined);
expect(edited).toEqual({ output: expect.anything(), isError: false });
expect((await cutProcess(linked, first)).handed).toBe(true);
let held = await client.resumption;
expect(held?.turns.size).toBe(1);

let next = await takeOver({ linked });
await next.browserReturns();
let back = await connectAgent({
url: next.server.url,
token: MEMBER_TOKEN,
document: linked.channel.id,
handlers,
resume: held!,
});
cleanups.push(() => back.detach());
gate.resolve();
await next.adopted;
await until(() => !next.context.chat.busy);

expect(Plan.source(next.plan).match(/Applied once\./g)).toHaveLength(1);
let said = next.context.chat.entries.map(entry => entry.text);
expect(said.filter(text => text.includes("Writing"))).toEqual(["Writing more."]);
expect(said.some(text => /restart/i.test(text))).toBe(false);
});
});

/** What the hold of the first process says once its drain has written it. */
async function cutRows(d: Deployed): Promise<PlannerLinkHold[]> {
await d.first.attachments.keepHandedTurn(d.linked.channel.id);
Expand Down
Loading
Loading