Files
openbot/scripts/agent-tasks-e2e.ts
Aleksander DygońandClaude Opus 5.5 619ef1b967 Hold new turns while a queued provider change waits (#14469)
* Hold new turns while a queued provider change waits

A provider, account or custom-agent change that is queued as
"After this turn" applies only when no turn of the agent runs. The
drain did not wait for it: when the main chat turn ended while side
chats still ran, the next queued message started on the old runtime.
With messages queued, some lane was always busy, so the change could
wait until the user stopped all work.

Add a "pending-runtime" drain hold. While agent.pendingRuntime is set
and the same busy test as the handover (#runtimeHandoverBusy) is true,
no chat, task or channel lane starts a new turn. The hold is never set
on an idle agent, so the existing followUsageLimit path applies the
change when the last running turn ends. Running turns are not
interrupted. When the queued change is removed, the drain starts again.

A queued task shows the existing "agent-busy" hold text. The
agent-tasks E2E script now checks that a new task waits while a switch
is queued and starts when the switch is removed.

Fixes #14455

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Settle stale task turns while a queued provider change waits

The pending-runtime hold counts each working task snapshot as busy.
A task turn that no provider runs any more could then hold every
lane of the agent. Before the hold, only the queued change waited.

When a lane gets the pending-runtime hold, run the existing
settleStaleTaskTurns once at a time for the agent. It ends only task
turns that no provider runs, as on a provider update. The end of the
turn schedules the drain again.

The agent-tasks E2E script also checks that the first task still runs
while the third task waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:19:56 +01:00

254 lines
9.7 KiB
TypeScript

import assert from "node:assert/strict";
import { mkdirSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { agentProviderDescriptor } from "@openbot/contracts/agent-providers";
import {
type AgentSummary,
type AgentTask,
type AgentTasksSnapshot,
type ClientAgentSummary,
isAgentModelOption,
isAgentSummary,
type OpenBotDesktopApi,
} from "@openbot/contracts/ipc";
import { createOpenBotLogger } from "@openbot/logging";
import { assertMutationAllowed, connectToDevApp } from "./dev-automation/cdp-client";
import { readDevInstanceRecords } from "./dev-automation/instance-registry";
declare global {
interface Window {
openbot: OpenBotDesktopApi;
}
}
const OUT = resolve(import.meta.dirname, "../.openbot-build/agent-tasks-e2e");
const argument = (name: string) => process.argv.find((arg) => arg.startsWith(`--${name}=`))?.slice(`--${name}=`.length);
const agentArgument = argument("agent");
assert.ok(agentArgument, "Pass --agent=<agent id> of an agent whose provider is signed in.");
const agentId: string = agentArgument;
const LONG_PROMPT = "Write a numbered list of 40 short facts about rivers. Do not use tools.";
const SHORT_PROMPT = "Reply with the single word ready. Do not use tools.";
const WAIT_MS = 120_000;
interface Report {
agentId: string;
provider?: string;
limitOne?: { first: string; second: string; secondHold: string | null; limit: number };
raisedLimit?: { second: string; limit: number };
stop?: { states: string[]; outcomes: Array<string | null> };
providerSwitch?: {
refused: boolean;
target: string | null;
provider?: string;
pendingProvider?: string | null;
pendingModel?: string | null;
};
switchHold?: { first: string; third: string; thirdHold: string | null; thirdAfterRevert?: string };
errorField?: { present: boolean };
restored?: { taskLimit: number | null };
passed: boolean;
}
const report: Report = { agentId, passed: false };
const started: string[] = [];
const instances = readDevInstanceRecords().filter(
(record) => record.service === "app" && resolve(record.projectRoot) === resolve(process.cwd()),
);
assert.equal(instances.length, 1, "Start one dev app for this worktree first.");
const instance = instances[0];
assert.ok(instance);
assertMutationAllowed({
command: "agent-tasks-e2e",
allowMutations: process.argv.includes("--allow-mutations"),
instanceNamed: true,
target: instance.instanceId,
});
const session = await connectToDevApp(instance.remoteDebuggingPort, createOpenBotLogger("agent-tasks-e2e"), {
expectedRendererPort: instance.rendererPort,
ownerPid: instance.pid,
});
const page = session.page;
function listTasks(): Promise<AgentTasksSnapshot> {
return page.evaluate((id) => window.openbot.agent.listTasks(id), agentId);
}
async function readAgent(): Promise<AgentSummary> {
const agent = await page.evaluate(async (id) => {
const agent = (await window.openbot.agent.listAgents()).find((candidate) => candidate.id === id);
if (!agent) throw new Error(`Unknown agent: ${id}`);
return agent;
}, agentId);
if (!isAgentSummary(agent)) throw new Error(`The agent ${agentId} has a provider that this build does not know.`);
return agent;
}
function setTaskLimit(taskLimit: number | null): Promise<ClientAgentSummary> {
return page.evaluate(({ id, limit }) => window.openbot.agent.updateAgent({ agentId: id, taskLimit: limit }), {
id: agentId,
limit: taskLimit,
});
}
async function startTask(text: string): Promise<AgentTask> {
const task = await page.evaluate(
({ id, prompt }) =>
window.openbot.agent.startTask({ agentId: id, text: prompt, clientMessageId: crypto.randomUUID() }),
{ id: agentId, prompt: text },
);
started.push(task.id);
return task;
}
async function waitForTasks(
label: string,
ready: (snapshot: AgentTasksSnapshot) => boolean,
): Promise<AgentTasksSnapshot> {
const deadline = Date.now() + WAIT_MS;
for (;;) {
const snapshot = await listTasks();
if (ready(snapshot)) return snapshot;
if (Date.now() > deadline) throw new Error(`Timed out: ${label}. Tasks: ${JSON.stringify(snapshot.tasks)}`);
await page.waitForTimeout(250);
}
}
const taskIn = (snapshot: AgentTasksSnapshot, taskId: string): AgentTask => {
const task = snapshot.tasks.find((candidate) => candidate.id === taskId);
assert.ok(task, `Task ${taskId} is not in the list.`);
return task;
};
const before = await readAgent();
report.provider = before.provider;
assert.ok(
agentProviderDescriptor(before.provider).concurrentTurns,
`${before.provider} runs one turn at a time. Choose an agent on another provider.`,
);
const originalLimit = before.taskLimit ?? null;
try {
await setTaskLimit(1);
const first = await startTask(LONG_PROMPT);
await waitForTasks("the first task runs", (snapshot) => taskIn(snapshot, first.id).state === "running");
const second = await startTask(SHORT_PROMPT);
const held = await waitForTasks(
"the second task waits",
(snapshot) => taskIn(snapshot, second.id).state === "queued" && taskIn(snapshot, first.id).state === "running",
);
report.limitOne = {
first: taskIn(held, first.id).state,
second: taskIn(held, second.id).state,
secondHold: taskIn(held, second.id).hold,
limit: held.limit,
};
assert.equal(held.limit, 1);
assert.equal(taskIn(held, second.id).hold, "task-limit");
await setTaskLimit(2);
const raised = await waitForTasks(
"the second task starts",
(snapshot) => taskIn(snapshot, second.id).state !== "queued",
);
report.raisedLimit = { second: taskIn(raised, second.id).state, limit: raised.limit };
assert.equal(raised.limit, 2);
const models = await page.evaluate(() => window.openbot.agent.listModels());
const target = models.filter(isAgentModelOption).find((model) => model.provider !== before.provider) ?? null;
const running = await listTasks();
if (target && taskIn(running, first.id).state === "running") {
const outcome = await page.evaluate(
async ({ id, provider, model }) => {
const agent = await window.openbot.agent.updateAgent({ agentId: id, provider, model });
return {
refused: false,
provider: agent.provider,
pendingProvider: agent.pendingRuntime?.provider ?? null,
pendingModel: agent.pendingRuntime?.model ?? null,
};
},
{ id: agentId, provider: target.provider, model: target.id },
);
report.providerSwitch = { ...outcome, target: `${target.provider}/${target.id}` };
assert.equal(outcome.refused, false, "The provider switch is queued while a task runs.");
assert.equal(outcome.provider, before.provider, "The running provider stays until the task ends.");
assert.equal(outcome.pendingProvider, target.provider);
assert.equal(outcome.pendingModel, target.id);
await setTaskLimit(3);
const third = await startTask(SHORT_PROMPT);
const switchWaits = await waitForTasks(
"a new task waits for the queued switch",
(snapshot) => taskIn(snapshot, third.id).hold === "agent-busy",
);
report.switchHold = {
first: taskIn(switchWaits, first.id).state,
third: taskIn(switchWaits, third.id).state,
thirdHold: taskIn(switchWaits, third.id).hold,
};
assert.equal(report.switchHold.first, "running", "The third task waits while the first task still runs.");
assert.equal(report.switchHold.third, "queued", "No new turn starts on the old provider while a switch waits.");
await page.evaluate(
({ id, provider, model }) => window.openbot.agent.updateAgent({ agentId: id, provider, model }),
{ id: agentId, provider: before.provider, model: before.model },
);
const reverted = await waitForTasks(
"the new task starts when the queued switch is removed",
(snapshot) => taskIn(snapshot, third.id).state !== "queued",
);
report.switchHold.thirdAfterRevert = taskIn(reverted, third.id).state;
} else {
report.providerSwitch = { refused: false, target: null };
}
for (const taskId of [first.id, second.id]) {
await page.evaluate(({ id, task }) => window.openbot.agent.stopTask({ agentId: id, taskId: task }), {
id: agentId,
task: taskId,
});
}
const stopped = await waitForTasks("both tasks stop", (snapshot) =>
[first.id, second.id].every((taskId) => taskIn(snapshot, taskId).state === "idle"),
);
report.stop = {
states: [first.id, second.id].map((taskId) => taskIn(stopped, taskId).state),
outcomes: [first.id, second.id].map((taskId) => taskIn(stopped, taskId).outcome),
};
assert.equal(taskIn(stopped, first.id).outcome, "interrupted");
report.errorField = { present: stopped.tasks.every((task) => "error" in task) };
assert.ok(report.errorField.present);
report.passed = true;
} finally {
for (const taskId of started) {
await page
.evaluate(({ id, task }) => window.openbot.agent.stopTask({ agentId: id, taskId: task }), {
id: agentId,
task: taskId,
})
.catch(() => undefined);
}
if (started.length > 0) {
await waitForTasks("the started tasks stop", (snapshot) =>
started.every((taskId) => taskIn(snapshot, taskId).state !== "running"),
).catch(() => undefined);
}
for (const taskId of started) {
await page
.evaluate(({ id, task }) => window.openbot.agent.closeTask({ agentId: id, taskId: task }), {
id: agentId,
task: taskId,
})
.catch(() => undefined);
}
const restored = await setTaskLimit(originalLimit);
report.restored = { taskLimit: restored.taskLimit ?? null };
mkdirSync(OUT, { recursive: true });
writeFileSync(resolve(OUT, "report.json"), `${JSON.stringify(report, null, 2)}\n`);
await session.close();
}
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
if (!report.passed) process.exitCode = 1;