Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix: abort chat computer commands when a turn stops (#138)

jz-kronof45f25eaa3b1
diff --git a/apps/server/src/engine/conversation.ts b/apps/server/src/engine/conversation.ts
index e7a4f72..14e8b93 100644
--- a/apps/server/src/engine/conversation.ts
+++ b/apps/server/src/engine/conversation.ts
@@ -141,7 +141,16 @@ export class ConversationAgent extends AbstractAgent {
const browserAbort = new AbortController();
const browserConfigured = !!(this.config.workerUrl && this.config.workerToken);
const tools = [
- ...computerTools(this.service.computer, this.service.files, this.owner, `chat:${requestKey}`),
+ ...computerTools(
+ this.service.computer,
+ this.service.files,
+ this.owner,
+ `chat:${requestKey}`,
+ {
+ signal: browserAbort.signal,
+ before: async () => browserAbort.signal.throwIfAborted(),
+ },
+ ),
...(jev
? [
presentChoicesTool(
diff --git a/tests/conversation-browser.test.ts b/tests/conversation-browser.test.ts
index c31c6aa..64d6c70 100644
--- a/tests/conversation-browser.test.ts
+++ b/tests/conversation-browser.test.ts
@@ -153,6 +153,51 @@ test("unsubscribing from chat stops queued browser navigation and further model
assert.equal(requests.length, 1);
});
+test("unsubscribing from chat aborts an in-flight computer command", async (t) => {
+ await modelFixture(t, (index) =>
+ index === 0
+ ? {
+ name: "run_computer_command",
+ arguments: { operationId: "slow", command: "sleep 30" },
+ }
+ : undefined,
+ );
+ const fixture = await chatFixture(t);
+ let started!: () => void;
+ const commandStarted = new Promise<void>((resolve) => {
+ started = resolve;
+ });
+ let release!: () => void;
+ let signal: AbortSignal | undefined;
+ fixture.computer.execute = async (_owner, args, options) => {
+ signal = options?.signal;
+ started();
+ await new Promise<void>((resolve) => {
+ release = resolve;
+ signal?.addEventListener("abort", () => resolve(), { once: true });
+ });
+ return {
+ id: "slow",
+ command: (args as { command: string }).command,
+ cwd: "/workspace",
+ status: "interrupted",
+ stdout: "",
+ stderr: "Stopped by user",
+ truncated: false,
+ startedAt: new Date().toISOString(),
+ };
+ };
+ const subscription = fixture.conversation.run(runInput()).subscribe();
+ try {
+ await commandStarted;
+ subscription.unsubscribe();
+ assert.equal(signal?.aborted, true, "Stop must reach the running computer command");
+ } finally {
+ subscription.unsubscribe();
+ release?.();
+ }
+});
+
test("chat searches and reads actual owner mail without creating a task or sending", async (t) => {
const { requests } = await modelFixture(t, (index) =>
index === 0