* feat(server): let agents queue, steer, or interrupt messages to other agents An agent sender's `steer` used to mean "wait for the turn to end", unlike a person's steer. Agents now get the same three choices a person has, plus a named default: `queue` waits for the turn end, `steer` joins the running turn (interrupting when the provider cannot steer), and `interrupt` replaces it. A steer or interrupt carries the messages queued ahead of it, so delivery order still equals admission order. The receipt returns the applied behavior and guidance built from whether the recipient is running and whether its provider can steer. A queued message's guidance tells the sender to follow up with a short steer or interrupt instead of resending, because a resent message is read twice. Older clients send `steer` for every default agent send, so the daemon maps their agent `steer` to `queue` when the client lacks the `agent_active_turn_behaviors` capability. New clients talking to an older daemon refuse `steer` rather than silently queueing it. * fix(server): keep a steer's carried messages when the turn ends first A steer that reached the provider after the turn ended made the manager throw a plain error, and delivery failed the whole batch, including the queued messages the steer carried. The manager now throws AgentTurnChangedError, and delivery sends the batch again, into the next turn or as a new one. Also report the applied activeTurnBehavior in `paseo send --no-wait` output, and expect the new hello capability in the client test. * docs(skills): warn that an unread steer loses the messages it carried * docs(skills): shorten the send_agent_prompt guidance Keep what a sender acts on: leave activeTurnBehavior out, the three values, read the guidance, follow up instead of resending, and the steer loss case. Drop delivery facts the sender cannot act on. * fix(server): stop the send_agent_prompt description promising an unread steer survives
3438 lines
123 KiB
TypeScript
3438 lines
123 KiB
TypeScript
import { mkdtemp, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { AgentStorage } from "./agent-storage.js";
|
|
import { createIdleRuntimeCleanup } from "./idle-runtime-cleanup/index.js";
|
|
import { relative } from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import { runAcpProvider, type AcpStreamMessage } from "@getpaseo/plugin/server/acp";
|
|
import type {
|
|
ProviderConnection,
|
|
ProviderLaunch,
|
|
ProviderEvent,
|
|
ProviderInput,
|
|
ProviderRegistration,
|
|
} from "@getpaseo/plugin/server/provider";
|
|
import { sendPreparedPromptToAgent } from "./agent-prompt.js";
|
|
import { setImmediate as nextTurn } from "node:timers/promises";
|
|
import { describe, expect, test } from "vitest";
|
|
import { createTestLogger } from "../../test-utils/test-logger.js";
|
|
import type { AgentClient, AgentStreamEvent } from "./agent-sdk-types.js";
|
|
import { toStoredAgentRecord } from "./agent-projections.js";
|
|
import { AgentManager } from "./agent-manager.js";
|
|
import { buildProviderRegistry } from "./provider-registry.js";
|
|
import { ProviderOverrideSchema } from "@getpaseo/protocol/provider-config";
|
|
import { PluginAgentClientRegistry } from "./plugin-provider.js";
|
|
import {
|
|
isStaleProviderSessionError,
|
|
StaleProviderSessionError,
|
|
} from "./stale-provider-session-error.js";
|
|
|
|
test("long concurrent plugin and child streams retain text-sized history and release it on archive", () => {
|
|
const fixture = fileURLToPath(new URL("./test-utils/plugin-stream-memory.ts", import.meta.url));
|
|
const output = execFileSync(
|
|
process.execPath,
|
|
["--expose-gc", "--max-old-space-size=512", "--import", "tsx", fixture],
|
|
{
|
|
encoding: "utf8",
|
|
timeout: 20_000,
|
|
},
|
|
);
|
|
const memory = JSON.parse(output) as { retained: number; closed: number };
|
|
expect(memory.retained).toBeLessThan(24 * 1024 * 1024);
|
|
expect(memory.closed).toBeLessThan(6 * 1024 * 1024);
|
|
});
|
|
|
|
const CAPABILITIES = [
|
|
"prompt.message",
|
|
"session.configure",
|
|
"session.persistence",
|
|
"session.subsession",
|
|
"permission",
|
|
] as const;
|
|
|
|
interface ProviderHarnessOptions {
|
|
capabilities?: ProviderConnection["capabilities"];
|
|
completeTurn?: boolean;
|
|
closeAcknowledgementOnly?: boolean;
|
|
openChildren?: (rootSessionId: string, emit: (event: ProviderEvent) => void) => void;
|
|
handleInput?: (input: ProviderInput, emit: (event: ProviderEvent) => void) => Promise<boolean>;
|
|
}
|
|
|
|
function controlKey(control: { key: string }): string {
|
|
return control.key;
|
|
}
|
|
|
|
function lastConfigure(inputs: ProviderInput[]): ProviderInput | undefined {
|
|
return inputs.findLast((input) => input.type === "session.configure");
|
|
}
|
|
|
|
function createProviderHarness(options: ProviderHarnessOptions = {}) {
|
|
let listener: ((event: ProviderEvent) => void) | null = null;
|
|
let closeCount = 0;
|
|
let resolveClosed!: () => void;
|
|
const closed = new Promise<void>((resolve) => {
|
|
resolveClosed = resolve;
|
|
});
|
|
const inputs: ProviderInput[] = [];
|
|
const emit = (event: ProviderEvent) => listener?.(event);
|
|
const capabilities = options.capabilities ?? CAPABILITIES;
|
|
|
|
const connection: ProviderConnection = {
|
|
version: 1,
|
|
capabilities,
|
|
async send(input) {
|
|
inputs.push(input);
|
|
if (await options.handleInput?.(input, emit)) return;
|
|
if (input.type === "catalog") {
|
|
emit({
|
|
type: "catalog",
|
|
requestId: input.requestId,
|
|
catalog: {
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [{ id: "deep", label: "Deep" }],
|
|
defaultModel: "plugin-model",
|
|
defaultMode: "build",
|
|
defaultThinkingOption: "deep",
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
if (input.type === "session.open") {
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities,
|
|
restoration: "core",
|
|
persistence: { version: 1, data: { token: "root" } },
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
model: "plugin-model",
|
|
mode: "build",
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [],
|
|
settings: [
|
|
{
|
|
type: "select",
|
|
id: "voice",
|
|
label: "Voice",
|
|
value: "direct",
|
|
options: [{ label: "Direct", value: "direct" }],
|
|
},
|
|
],
|
|
},
|
|
});
|
|
emit({
|
|
type: "session.opened",
|
|
sessionId: "child-1",
|
|
parentSessionId: input.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
title: "Plugin child",
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: "child-1",
|
|
item: { type: "assistant_message", id: "child-message", text: "Child result" },
|
|
});
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: "child-1",
|
|
turnId: "child-turn",
|
|
state: "completed",
|
|
});
|
|
emit({ type: "session.ready", sessionId: "child-1" });
|
|
options.openChildren?.(input.sessionId, emit);
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return;
|
|
}
|
|
if (input.type === "session.prompt") {
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "turn", turnId: "turn-1" },
|
|
});
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: input.sessionId,
|
|
turnId: "turn-1",
|
|
state: "started",
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: { type: "assistant_message", id: "answer", text: "Hel" },
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: { type: "assistant_message", id: "answer", text: "Hello" },
|
|
});
|
|
emit({
|
|
type: "session.permission",
|
|
sessionId: input.sessionId,
|
|
request: { id: "permission-1", name: "write", kind: "tool" },
|
|
});
|
|
if (options.completeTurn !== false) {
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: input.sessionId,
|
|
turnId: "turn-1",
|
|
state: "completed",
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
if (input.type === "session.permission") {
|
|
emit({
|
|
type: "session.permission_resolved",
|
|
sessionId: input.sessionId,
|
|
permissionId: input.permissionId,
|
|
});
|
|
return;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
model: input.changes.model ?? "plugin-model",
|
|
mode: "build",
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [],
|
|
settings: [],
|
|
},
|
|
});
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return;
|
|
}
|
|
if (input.type === "session.close") {
|
|
emit({ type: "session.closed", sessionId: input.sessionId });
|
|
if (options.closeAcknowledgementOnly) return;
|
|
}
|
|
if ("requestId" in input) {
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
}
|
|
},
|
|
onEvent(nextListener) {
|
|
listener = nextListener;
|
|
return () => {
|
|
if (listener === nextListener) listener = null;
|
|
};
|
|
},
|
|
async close() {
|
|
closeCount += 1;
|
|
resolveClosed();
|
|
},
|
|
};
|
|
|
|
const registration: ProviderRegistration = {
|
|
id: "plugin-direct",
|
|
label: "Plugin direct",
|
|
async connect() {
|
|
return connection;
|
|
},
|
|
};
|
|
|
|
return {
|
|
registration,
|
|
emit,
|
|
inputs,
|
|
closeCount: () => closeCount,
|
|
waitForClose: () => closed,
|
|
};
|
|
}
|
|
|
|
function eventsOfType(events: AgentStreamEvent[], type: AgentStreamEvent["type"]) {
|
|
return events.filter((event) => event.type === type);
|
|
}
|
|
|
|
test("preserves skills in a plugin session's command list", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: tmpdir(),
|
|
});
|
|
try {
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
harness.emit({
|
|
type: "session.commands",
|
|
sessionId: open.sessionId,
|
|
commands: [
|
|
{ name: "compact", description: "Compact the conversation" },
|
|
{ name: "diagnose", description: "Diagnose a bug", kind: "skill" },
|
|
],
|
|
});
|
|
await expect(session.listCommands!()).resolves.toEqual([
|
|
{
|
|
name: "compact",
|
|
description: "Compact the conversation",
|
|
argumentHint: "",
|
|
kind: "command",
|
|
},
|
|
{ name: "diagnose", description: "Diagnose a bug", argumentHint: "", kind: "skill" },
|
|
]);
|
|
} finally {
|
|
await session.close();
|
|
}
|
|
});
|
|
|
|
function createRewindProviderHarness(
|
|
options: {
|
|
reuseAnswerId?: boolean;
|
|
forkOnRevert?: boolean;
|
|
handleInput?: ProviderHarnessOptions["handleInput"];
|
|
} = {},
|
|
) {
|
|
const nativeMessages: string[] = [];
|
|
let originalMessages: string[] | null = null;
|
|
const harness = createProviderHarness({
|
|
closeAcknowledgementOnly: true,
|
|
capabilities: [
|
|
...CAPABILITIES,
|
|
"session.revert.conversation",
|
|
"session.revert.files",
|
|
"session.revert.both",
|
|
],
|
|
async handleInput(input, emit) {
|
|
if (await options.handleInput?.(input, emit)) return true;
|
|
if (input.type === "session.open" && input.history === "replay") {
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: [
|
|
...CAPABILITIES,
|
|
"session.revert.conversation",
|
|
"session.revert.files",
|
|
"session.revert.both",
|
|
],
|
|
restoration: "core",
|
|
persistence: input.persistence ?? { version: 1, data: { token: "root" } },
|
|
cwd: input.config.cwd,
|
|
});
|
|
const messages =
|
|
options.forkOnRevert &&
|
|
(input.persistence?.data as { token?: string })?.token === "root" &&
|
|
originalMessages
|
|
? originalMessages
|
|
: nativeMessages;
|
|
for (const [index, text] of messages.entries()) {
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: { type: "user_message", id: `user-${text}`, text, revertToken: text },
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: {
|
|
type: "assistant_message",
|
|
id: options.reuseAnswerId ? `answer-${index + 1}` : `answer-${text}`,
|
|
text: `Reply ${text}`,
|
|
},
|
|
});
|
|
}
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.prompt") {
|
|
if (input.prompt.input.type !== "message") throw new Error("Expected a message");
|
|
const text = input.prompt.input.content
|
|
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
.join("");
|
|
nativeMessages.push(text);
|
|
const turnId = `turn-${text}`;
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "turn", turnId },
|
|
});
|
|
emit({ type: "session.turn", sessionId: input.sessionId, turnId, state: "started" });
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: {
|
|
type: "user_message",
|
|
id: `user-${text}`,
|
|
text,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
revertToken: text,
|
|
},
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId: input.sessionId,
|
|
item: {
|
|
type: "assistant_message",
|
|
id: options.reuseAnswerId ? `answer-${nativeMessages.length}` : `answer-${text}`,
|
|
text: `Reply ${text}`,
|
|
},
|
|
});
|
|
emit({ type: "session.turn", sessionId: input.sessionId, turnId, state: "completed" });
|
|
return true;
|
|
}
|
|
if (input.type === "session.revert") {
|
|
if (options.forkOnRevert && input.scope !== "files") {
|
|
originalMessages = [...nativeMessages];
|
|
emit({
|
|
type: "session.persistence",
|
|
sessionId: input.sessionId,
|
|
persistence: { version: 1, data: { token: "fork" } },
|
|
});
|
|
}
|
|
if (input.scope !== "files")
|
|
nativeMessages.splice(nativeMessages.indexOf(String(input.token)));
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
return { ...harness, nativeMessages };
|
|
}
|
|
|
|
async function withRewindAgent(
|
|
run: (context: {
|
|
manager: AgentManager;
|
|
agentId: string;
|
|
nativeMessages: string[];
|
|
messages: () => string[];
|
|
}) => Promise<void>,
|
|
options: Parameters<typeof createRewindProviderHarness>[0] = {},
|
|
) {
|
|
const harness = createRewindProviderHarness(options);
|
|
const logger = createTestLogger();
|
|
const registry = new PluginAgentClientRegistry(logger);
|
|
registry.replace([harness.registration]);
|
|
const manager = new AgentManager({
|
|
logger,
|
|
clients: registry.clients(),
|
|
providerDefinitions: registry.definitions(),
|
|
});
|
|
const agent = await manager.createAgent(
|
|
{ provider: harness.registration.id, cwd: tmpdir() },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
const messages = () =>
|
|
manager
|
|
.getTimeline(agent.id)
|
|
.flatMap((item) =>
|
|
item.type === "user_message" || item.type === "assistant_message" ? [item.text] : [],
|
|
);
|
|
try {
|
|
await manager.runAgent(agent.id, "A", { clientMessageId: "client-A" });
|
|
await manager.runAgent(agent.id, "B", { clientMessageId: "client-B" });
|
|
await manager.runAgent(agent.id, "C", { clientMessageId: "client-C" });
|
|
expect(messages()).toEqual(["A", "Reply A", "B", "Reply B", "C", "Reply C"]);
|
|
await run({ manager, agentId: agent.id, nativeMessages: harness.nativeMessages, messages });
|
|
} finally {
|
|
await manager.closeAgent(agent.id);
|
|
await registry.shutdown();
|
|
}
|
|
}
|
|
|
|
test("conversation rewind discards withdrawn plugin turns before and after a replacement prompt", async () => {
|
|
await withRewindAgent(async ({ manager, agentId, nativeMessages, messages }) => {
|
|
await manager.rewind(agentId, "user-B", "conversation");
|
|
const afterRewind = messages();
|
|
expect(nativeMessages).toEqual(["A"]);
|
|
await manager.runAgent(agentId, "D", { clientMessageId: "client-D" });
|
|
const afterReplacement = messages();
|
|
expect(nativeMessages).toEqual(["A", "D"]);
|
|
expect({ afterRewind, afterReplacement }).toEqual({
|
|
afterRewind: ["A", "Reply A"],
|
|
afterReplacement: ["A", "Reply A", "D", "Reply D"],
|
|
});
|
|
});
|
|
});
|
|
|
|
test("file-only plugin rewind preserves the conversation before and after a replacement prompt", async () => {
|
|
await withRewindAgent(async ({ manager, agentId, nativeMessages, messages }) => {
|
|
await manager.rewind(agentId, "user-B", "files");
|
|
expect(nativeMessages).toEqual(["A", "B", "C"]);
|
|
expect(messages()).toEqual(["A", "Reply A", "B", "Reply B", "C", "Reply C"]);
|
|
await manager.runAgent(agentId, "D", { clientMessageId: "client-D" });
|
|
expect(messages()).toEqual(["A", "Reply A", "B", "Reply B", "C", "Reply C", "D", "Reply D"]);
|
|
});
|
|
});
|
|
|
|
test("plugin rewind restores translation state when a replacement reuses a discarded item id", async () => {
|
|
const harness = createRewindProviderHarness({ reuseAnswerId: true });
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: harness.registration.id, cwd: tmpdir() });
|
|
const live: AgentStreamEvent[] = [];
|
|
try {
|
|
await session.run("A");
|
|
await session.run("B");
|
|
session.subscribe((event) => live.push(event));
|
|
await session.revertConversation!({ messageId: "user-B" });
|
|
live.length = 0;
|
|
await session.run("B revised");
|
|
const assistant = live.flatMap((event) =>
|
|
event.type === "timeline" && event.item.type === "assistant_message" ? [event.item.text] : [],
|
|
);
|
|
expect(harness.nativeMessages).toEqual(["A", "B revised"]);
|
|
expect(assistant).toEqual(["Reply B revised"]);
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
async function withPluginRewindSession(
|
|
options: Parameters<typeof createRewindProviderHarness>[0],
|
|
run: (
|
|
session: Awaited<ReturnType<AgentClient["createSession"]>>,
|
|
harness: ReturnType<typeof createRewindProviderHarness>,
|
|
) => Promise<void>,
|
|
) {
|
|
const harness = createRewindProviderHarness(options);
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: harness.registration.id, cwd: tmpdir() });
|
|
try {
|
|
await session.run("A");
|
|
await session.run("B");
|
|
await session.run("C");
|
|
await run(session, harness);
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
}
|
|
|
|
async function pluginMessages(session: Awaited<ReturnType<AgentClient["createSession"]>>) {
|
|
const events = await Array.fromAsync(session.streamHistory());
|
|
return events.flatMap((event) =>
|
|
event.type === "timeline" &&
|
|
(event.item.type === "user_message" || event.item.type === "assistant_message")
|
|
? [event.item.text]
|
|
: [],
|
|
);
|
|
}
|
|
|
|
test("plugin rewind discards child outcomes from the withdrawn branch", async () => {
|
|
await withPluginRewindSession(
|
|
{
|
|
async handleInput(input, emit) {
|
|
if (input.type !== "session.open" || input.history !== "replay") return false;
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: CAPABILITIES,
|
|
restoration: "core",
|
|
persistence: input.persistence,
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "session.opened",
|
|
sessionId: "rewound-child",
|
|
parentSessionId: input.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({ type: "session.closed", sessionId: "rewound-child" });
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
harness.emit({
|
|
type: "session.opened",
|
|
sessionId: "rewound-child",
|
|
parentSessionId: open.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: tmpdir(),
|
|
});
|
|
harness.emit({
|
|
type: "session.turn",
|
|
sessionId: "rewound-child",
|
|
turnId: "withdrawn-turn",
|
|
state: "failed",
|
|
});
|
|
await session.revertConversation!({ messageId: "user-B" });
|
|
const history = await Array.fromAsync(session.streamHistory());
|
|
expect(history.findLast((event) => event.type === "provider_subagent")).toMatchObject({
|
|
event: { type: "upsert", id: "rewound-child", status: "completed" },
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
test("combined plugin rewind replays the retained native branch", async () => {
|
|
await withPluginRewindSession({}, async (session) => {
|
|
await session.revertBoth!({ messageId: "user-B" });
|
|
expect(await pluginMessages(session)).toEqual(["A", "Reply A"]);
|
|
await session.revertConversation!({ messageId: "user-A" });
|
|
expect(await pluginMessages(session)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test("a rewind reopens with the options as they stand, a reset included", async () => {
|
|
const options: Record<string, boolean> = {};
|
|
const report = (emit: (event: ProviderEvent) => void, sessionId: string) =>
|
|
emit({
|
|
type: "session.config",
|
|
sessionId,
|
|
config: {
|
|
model: "plugin-model",
|
|
mode: "build",
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [],
|
|
controls: [{ type: "toggle", key: "concise", label: "Concise" }],
|
|
providerOptions: { ...options },
|
|
},
|
|
});
|
|
const harness = createRewindProviderHarness({
|
|
handleInput: async (input, emit) => {
|
|
if (input.type === "session.open" && input.history !== "replay") {
|
|
options.concise = true;
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: [...CAPABILITIES, "session.revert.conversation"],
|
|
restoration: "core",
|
|
persistence: { version: 1, data: { token: "root" } },
|
|
cwd: input.config.cwd,
|
|
});
|
|
report(emit, input.sessionId);
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
// A plugin that represents the default by leaving the key out.
|
|
delete options.concise;
|
|
report(emit, input.sessionId);
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: tmpdir(),
|
|
providerOptions: { concise: true },
|
|
});
|
|
await session.run("A");
|
|
await session.applyOptions?.({ concise: null });
|
|
|
|
await session.revertConversation!({ messageId: "user-A" });
|
|
|
|
const reopened = harness.inputs.findLast((input) => input.type === "session.open");
|
|
expect(reopened?.config.providerOptions).toEqual({});
|
|
await session.close();
|
|
await registry.shutdown();
|
|
});
|
|
|
|
// A plugin may report no options at all while it runs the ones it was given.
|
|
async function withUnreportedOptionSession(
|
|
run: (
|
|
session: Awaited<ReturnType<AgentClient["createSession"]>>,
|
|
harness: ReturnType<typeof createRewindProviderHarness>,
|
|
) => Promise<void>,
|
|
) {
|
|
const harness = createRewindProviderHarness({
|
|
handleInput: async (input, emit) => {
|
|
if (input.type === "session.open" && input.history !== "replay") {
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: [...CAPABILITIES, "session.revert.conversation"],
|
|
restoration: "core",
|
|
persistence: { version: 1, data: { token: "root" } },
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
model: "plugin-model",
|
|
mode: "build",
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [],
|
|
controls: [],
|
|
providerOptions: {},
|
|
},
|
|
});
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: tmpdir(),
|
|
providerOptions: { token_budget: 1000 },
|
|
});
|
|
try {
|
|
await run(session, harness);
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
}
|
|
|
|
test("applying an option the plugin never reports leaves the stored option alone", async () => {
|
|
await withUnreportedOptionSession(async (session) => {
|
|
// The same value as a whole-profile patch carries it, then a new one.
|
|
expect(await session.applyOptions?.({ token_budget: 1000 })).toEqual({});
|
|
expect(await session.applyOptions?.({ token_budget: 2000 })).toEqual({});
|
|
});
|
|
});
|
|
|
|
test("a rewind reopens with the value the plugin accepted for an option it never reports", async () => {
|
|
await withUnreportedOptionSession(async (session, harness) => {
|
|
await session.run("A");
|
|
await session.applyOptions?.({ token_budget: 2000 });
|
|
|
|
await session.revertConversation!({ messageId: "user-A" });
|
|
|
|
const reopened = harness.inputs.findLast((input) => input.type === "session.open");
|
|
expect(reopened?.config.providerOptions).toEqual({ token_budget: 2000 });
|
|
});
|
|
});
|
|
|
|
test("plugin rewind resumes the latest persistence rather than the original handle", async () => {
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type === "session.revert")
|
|
emit({
|
|
type: "session.persistence",
|
|
sessionId: input.sessionId,
|
|
persistence: { version: 1, data: { token: "fork" } },
|
|
});
|
|
return false;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
await session.revertConversation!({ messageId: "user-B" });
|
|
expect(harness.inputs.findLast((input) => input.type === "session.open")).toMatchObject({
|
|
persistence: { version: 1, data: { token: "fork" } },
|
|
history: "replay",
|
|
});
|
|
expect(await pluginMessages(session)).toEqual(["A", "Reply A"]);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("failed native plugin rewind preserves history and does not reopen", async () => {
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.revert") return false;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "native rewind failed" },
|
|
});
|
|
return true;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
await expect(session.revertConversation!({ messageId: "user-B" })).rejects.toThrow(
|
|
"native rewind failed",
|
|
);
|
|
expect(await pluginMessages(session)).toEqual([
|
|
"A",
|
|
"Reply A",
|
|
"B",
|
|
"Reply B",
|
|
"C",
|
|
"Reply C",
|
|
]);
|
|
expect(harness.inputs.filter((input) => input.type === "session.open")).toHaveLength(1);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("failed plugin rewind replay blocks prompts until retry without repeating native rewind", async () => {
|
|
let failReplay = true;
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.open" || input.history !== "replay" || !failReplay)
|
|
return false;
|
|
failReplay = false;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "replay failed" },
|
|
});
|
|
return true;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
await expect(session.revertConversation!({ messageId: "user-B" })).rejects.toThrow(
|
|
"replay failed",
|
|
);
|
|
await expect(session.run("D")).rejects.toThrow("Retry the rewind or reload");
|
|
await session.revertConversation!({ messageId: "user-B" });
|
|
await session.run("D");
|
|
expect(await pluginMessages(session)).toEqual(["A", "Reply A", "D", "Reply D"]);
|
|
expect(harness.inputs.filter((input) => input.type === "session.revert")).toHaveLength(1);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("retrying a failed plugin rewind replay replaces the manager timeline before prompting", async () => {
|
|
let failReplay = true;
|
|
await withRewindAgent(
|
|
async ({ manager, agentId, messages }) => {
|
|
await expect(manager.rewind(agentId, "user-B", "conversation")).rejects.toThrow(
|
|
"replay failed",
|
|
);
|
|
await expect(manager.runAgent(agentId, "D")).rejects.toThrow("Retry the rewind or reload");
|
|
await manager.rewind(agentId, "user-B", "conversation");
|
|
expect(messages()).toEqual(["A", "Reply A"]);
|
|
await manager.runAgent(agentId, "D");
|
|
expect(messages()).toEqual(["A", "Reply A", "D", "Reply D"]);
|
|
},
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.open" || input.history !== "replay" || !failReplay)
|
|
return false;
|
|
failReplay = false;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "replay failed" },
|
|
});
|
|
return true;
|
|
},
|
|
},
|
|
);
|
|
});
|
|
|
|
test("reload after failed plugin rewind replay resumes the committed fork", async () => {
|
|
let failReplay = true;
|
|
await withRewindAgent(
|
|
async ({ manager, agentId, messages }) => {
|
|
await expect(manager.rewind(agentId, "user-B", "conversation")).rejects.toThrow(
|
|
"replay failed",
|
|
);
|
|
await manager.reloadAgentSession(agentId, undefined, { rehydrateFromDisk: true });
|
|
await manager.hydrateTimelineFromProvider(agentId, { force: true });
|
|
expect(messages()).toEqual(["A", "Reply A"]);
|
|
},
|
|
{
|
|
forkOnRevert: true,
|
|
handleInput: async (input, emit) => {
|
|
if (input.type === "session.close") {
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
if (input.type !== "session.open" || input.history !== "replay" || !failReplay)
|
|
return false;
|
|
failReplay = false;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "replay failed" },
|
|
});
|
|
return true;
|
|
},
|
|
},
|
|
);
|
|
});
|
|
|
|
test("closing during plugin rewind replay closes each native binding once", async () => {
|
|
let opened!: () => void;
|
|
let release!: () => void;
|
|
const replayStarted = new Promise<void>((resolve) => {
|
|
opened = resolve;
|
|
});
|
|
const replayAllowed = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input) => {
|
|
if (input.type === "session.open" && input.history === "replay") {
|
|
opened();
|
|
await replayAllowed;
|
|
}
|
|
return false;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
const rewind = session.revertConversation!({ messageId: "user-B" });
|
|
await replayStarted;
|
|
const closing = session.close();
|
|
release();
|
|
await Promise.all([rewind, closing]);
|
|
const closes = harness.inputs.flatMap((input) =>
|
|
input.type === "session.close" ? [input.sessionId] : [],
|
|
);
|
|
expect(closes).toHaveLength(2);
|
|
expect(new Set(closes).size).toBe(2);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("plugin rewind retries a failed close before reopening the native writer", async () => {
|
|
let failClose = true;
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.close" || !failClose) return false;
|
|
failClose = false;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "close failed" },
|
|
});
|
|
return true;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
await expect(session.revertConversation!({ messageId: "user-B" })).rejects.toThrow(
|
|
"close failed",
|
|
);
|
|
expect(harness.inputs.filter((input) => input.type === "session.open")).toHaveLength(1);
|
|
expect(await pluginMessages(session)).toEqual(["A", "Reply A"]);
|
|
expect(harness.inputs.filter((input) => input.type === "session.revert")).toHaveLength(1);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("plugin rewind retries permission settlement before retiring its session", async () => {
|
|
let failPermission = true;
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input) => {
|
|
if (input.type !== "session.permission" || !failPermission) return false;
|
|
failPermission = false;
|
|
throw new Error("permission delivery failed");
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
const opened = harness.inputs.find((input) => input.type === "session.open")!;
|
|
if (opened.type !== "session.open") throw new Error("Missing root");
|
|
harness.emit({
|
|
type: "session.permission",
|
|
sessionId: opened.sessionId,
|
|
request: { id: "pending", name: "tool", kind: "tool" },
|
|
});
|
|
await expect(session.revertConversation!({ messageId: "user-B" })).rejects.toThrow(
|
|
"permission delivery failed",
|
|
);
|
|
expect(session.getPendingPermissions()).toHaveLength(1);
|
|
expect(harness.inputs.filter((input) => input.type === "session.close")).toHaveLength(0);
|
|
expect(await pluginMessages(session)).toEqual(["A", "Reply A"]);
|
|
expect(session.getPendingPermissions()).toEqual([]);
|
|
expect(harness.inputs.filter((input) => input.type === "session.permission")).toHaveLength(2);
|
|
},
|
|
);
|
|
});
|
|
|
|
test("plugin rewind replaces child listeners and settles old pending permissions", async () => {
|
|
await withPluginRewindSession({}, async (session, harness) => {
|
|
const root = harness.inputs.find((input) => input.type === "session.open")!;
|
|
if (root.type !== "session.open") throw new Error("Missing root");
|
|
harness.emit({
|
|
type: "session.permission",
|
|
sessionId: root.sessionId,
|
|
request: { id: "old-permission", name: "tool", kind: "tool", title: "Run tool" },
|
|
});
|
|
expect(session.getPendingPermissions()).toHaveLength(1);
|
|
await session.revertConversation!({ messageId: "user-B" });
|
|
expect(session.getPendingPermissions()).toEqual([]);
|
|
expect(harness.inputs).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "session.permission",
|
|
permissionId: "old-permission",
|
|
response: { behavior: "deny", message: "Session replaced after rewind" },
|
|
}),
|
|
);
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: "child-1",
|
|
item: { type: "assistant_message", id: "child-message", text: "Discarded child update" },
|
|
});
|
|
const history = await Array.fromAsync(session.streamHistory());
|
|
expect(history.filter((event) => event.type === "provider_subagent")).toEqual([]);
|
|
const reopened = harness.inputs.findLast((input) => input.type === "session.open")!;
|
|
if (reopened.type !== "session.open") throw new Error("Missing reopened root");
|
|
harness.emit({
|
|
type: "session.opened",
|
|
sessionId: "child-1",
|
|
parentSessionId: reopened.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: tmpdir(),
|
|
});
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: "child-1",
|
|
item: { type: "assistant_message", id: "child-message", text: "New child reply" },
|
|
});
|
|
const next = await Array.fromAsync(session.streamHistory());
|
|
expect(next).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "provider_subagent",
|
|
event: expect.objectContaining({
|
|
type: "timeline",
|
|
item: expect.objectContaining({ text: "New child reply" }),
|
|
}),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
test("nonpersisted plugin sessions reject conversation rewind before native mutation", async () => {
|
|
await withPluginRewindSession(
|
|
{
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.open") return false;
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: [...CAPABILITIES, "session.revert.conversation", "session.revert.files"],
|
|
restoration: "core",
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
},
|
|
},
|
|
async (session, harness) => {
|
|
expect(session.capabilities.supportsRewindConversation).toBe(false);
|
|
await expect(session.revertConversation!({ messageId: "user-B" })).rejects.toThrow(
|
|
"requires session persistence",
|
|
);
|
|
expect(harness.inputs.filter((input) => input.type === "session.revert")).toEqual([]);
|
|
await session.revertFiles!({ messageId: "user-B" });
|
|
expect(harness.nativeMessages).toEqual(["A", "B", "C"]);
|
|
},
|
|
);
|
|
});
|
|
|
|
function openNestedChildren(rootSessionId: string, emit: (event: ProviderEvent) => void) {
|
|
for (const [sessionId, parentSessionId] of [
|
|
["a", rootSessionId],
|
|
["a.b", "a"],
|
|
["a.b.c", "a.b"],
|
|
["sibling", rootSessionId],
|
|
]) {
|
|
emit({
|
|
type: "session.opened",
|
|
sessionId,
|
|
parentSessionId,
|
|
toolCallId: `${sessionId}-task`,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: "/workspace",
|
|
});
|
|
emit({
|
|
type: "timeline.item",
|
|
sessionId,
|
|
item: { type: "assistant_message", id: `${sessionId}-message`, text: sessionId },
|
|
});
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId,
|
|
turnId: `${sessionId}-turn`,
|
|
state: "completed",
|
|
});
|
|
}
|
|
}
|
|
|
|
function expectNestedChildren(events: AgentStreamEvent[]) {
|
|
for (const [id, parentSubagentId] of [
|
|
["a", null],
|
|
["a.b", "a"],
|
|
["a.b.c", "a.b"],
|
|
["sibling", null],
|
|
]) {
|
|
expect(events).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "provider_subagent",
|
|
event: expect.objectContaining({
|
|
type: "upsert",
|
|
id,
|
|
parentSubagentId,
|
|
toolCallId: `${id}-task`,
|
|
status: "running",
|
|
}),
|
|
}),
|
|
);
|
|
expect(events).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "provider_subagent",
|
|
event: expect.objectContaining({
|
|
type: "timeline",
|
|
id,
|
|
item: expect.objectContaining({ text: id }),
|
|
}),
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
describe("PluginAgentClientRegistry", () => {
|
|
test("history keeps the tool's first position when completion follows later text", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: client.provider, cwd: "/tmp" });
|
|
const opened = harness.inputs.find((input) => input.type === "session.open");
|
|
if (opened?.type !== "session.open") throw new Error("missing session open");
|
|
const tool = {
|
|
type: "tool_call" as const,
|
|
id: "tool-position",
|
|
callId: "call-position",
|
|
name: "read",
|
|
detail: { type: "unknown" as const, input: null, output: null },
|
|
error: null,
|
|
};
|
|
try {
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: opened.sessionId,
|
|
item: { ...tool, status: "running" },
|
|
});
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: opened.sessionId,
|
|
item: { type: "assistant_message", id: "later-text", text: "Working" },
|
|
});
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: opened.sessionId,
|
|
item: { ...tool, status: "completed" },
|
|
});
|
|
const items = [];
|
|
for await (const event of session.streamHistory())
|
|
if (event.type === "timeline") items.push(event.item);
|
|
expect(items).toEqual([
|
|
{
|
|
type: "tool_call",
|
|
callId: "call-position",
|
|
name: "read",
|
|
detail: tool.detail,
|
|
error: null,
|
|
status: "completed",
|
|
},
|
|
{ type: "assistant_message", messageId: "later-text", text: "Working" },
|
|
]);
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("detached text deltas preserve split surrogates and history replays one complete message", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: client.provider, cwd: "/tmp" });
|
|
const opened = harness.inputs.find((input) => input.type === "session.open");
|
|
if (opened?.type !== "session.open") throw new Error("missing session open");
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
const prefix = "long prefix ".repeat(10);
|
|
const first = prefix + "a\uD83D";
|
|
const complete = first + "\uDE00b";
|
|
try {
|
|
for (const text of [first, complete, complete]) {
|
|
harness.emit({
|
|
type: "timeline.item",
|
|
sessionId: opened.sessionId,
|
|
item: { type: "assistant_message", id: "split-message", text },
|
|
});
|
|
}
|
|
const deltas = events.flatMap((event) =>
|
|
event.type === "timeline" && event.item.type === "assistant_message"
|
|
? [event.item.text]
|
|
: [],
|
|
);
|
|
expect(deltas).toEqual([first, "\uDE00b"]);
|
|
expect(deltas.join("")).toBe(prefix + "a😀b");
|
|
const history: AgentStreamEvent[] = [];
|
|
for await (const event of session.streamHistory()) history.push(event);
|
|
expect(
|
|
history.flatMap((event) =>
|
|
event.type === "timeline" && event.item.type === "assistant_message" ? [event.item] : [],
|
|
),
|
|
).toEqual([{ type: "assistant_message", messageId: "split-message", text: complete }]);
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("stores the agent options and the settings the plugin opened with, not the merged config defaults", async () => {
|
|
const logger = createTestLogger();
|
|
const harness = createProviderHarness();
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([harness.registration]);
|
|
const registry = buildProviderRegistry(logger, {
|
|
pluginProviders: plugins.definitions(),
|
|
providerOverrides: {
|
|
"plugin-direct": { options: { nested: { base: true, replace: "config" } } },
|
|
},
|
|
});
|
|
const manager = new AgentManager({
|
|
logger,
|
|
clients: { "plugin-direct": registry["plugin-direct"].createClient(logger) },
|
|
providerDefinitions: { "plugin-direct": registry["plugin-direct"] },
|
|
});
|
|
const own = { nested: { replace: "agent" } };
|
|
const agent = await manager.createAgent(
|
|
{ provider: "plugin-direct", cwd: tmpdir(), providerOptions: own },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
// `nested.base` comes from `config.json` and is not stored; `voice` is the plugin's own opening state.
|
|
expect(toStoredAgentRecord(agent).config?.providerOptions).toEqual({ ...own, voice: "direct" });
|
|
expect(harness.inputs.find((input) => input.type === "session.open")).toMatchObject({
|
|
config: { providerOptions: { nested: { base: true, replace: "agent" } } },
|
|
});
|
|
await agent.session?.close();
|
|
});
|
|
|
|
// The same contract holds for a plugin that reports `controls` and `providerOptions` and for one
|
|
// still reporting `settings`.
|
|
describe.each(["controls", "settings"] as const)("options of a plugin reporting %s", (shape) => {
|
|
// A plugin that keeps `voice` on "direct" whatever it is asked, and takes `loud` as given.
|
|
async function openStubbornVoiceSession(
|
|
given: Record<string, boolean | string>,
|
|
{ refuseAfterChanging = false }: { refuseAfterChanging?: boolean } = {},
|
|
) {
|
|
const state: { voice: string; loud: boolean; tokenBudget?: number } = {
|
|
voice: "direct",
|
|
loud: false,
|
|
};
|
|
let openedSessionId = "";
|
|
const reported = () =>
|
|
shape === "controls"
|
|
? {
|
|
controls: [
|
|
{
|
|
type: "select" as const,
|
|
key: "voice",
|
|
label: "Voice",
|
|
icon: "Mic",
|
|
defaultValue: "direct",
|
|
choices: [
|
|
{ label: "Direct", value: "direct" },
|
|
{ label: "Other", value: "other" },
|
|
],
|
|
},
|
|
{
|
|
type: "toggle" as const,
|
|
key: "loud",
|
|
label: "Loud",
|
|
icon: "Volume2",
|
|
defaultValue: false,
|
|
},
|
|
],
|
|
providerOptions: {
|
|
voice: state.voice,
|
|
loud: state.loud,
|
|
...(state.tokenBudget === undefined ? {} : { token_budget: state.tokenBudget }),
|
|
},
|
|
}
|
|
: {
|
|
settings: [
|
|
{
|
|
type: "select" as const,
|
|
id: "voice",
|
|
label: "Voice",
|
|
value: state.voice,
|
|
options: [
|
|
{ label: "Direct", value: "direct" },
|
|
{ label: "Other", value: "other" },
|
|
],
|
|
},
|
|
{ type: "toggle" as const, id: "loud", label: "Loud", value: state.loud },
|
|
],
|
|
};
|
|
const emitConfig = (emit: (event: ProviderEvent) => void, sessionId: string) =>
|
|
emit({
|
|
type: "session.config",
|
|
sessionId,
|
|
config: {
|
|
model: "plugin-model",
|
|
mode: "build",
|
|
models: [{ id: "plugin-model", label: "Plugin model" }],
|
|
modes: [{ id: "build", label: "Build" }],
|
|
thinkingOptions: [],
|
|
...reported(),
|
|
},
|
|
});
|
|
const harness = createProviderHarness({
|
|
async handleInput(input, emit) {
|
|
if (input.type === "session.open") {
|
|
openedSessionId = input.sessionId;
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: CAPABILITIES,
|
|
restoration: "core",
|
|
persistence: { version: 1, data: { token: "root" } },
|
|
cwd: input.config.cwd,
|
|
});
|
|
emitConfig(emit, input.sessionId);
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
const patch =
|
|
shape === "settings" ? input.changes.settings : input.changes.providerOptions;
|
|
const { loud, token_budget: tokenBudget } = patch ?? {};
|
|
// A plugin that takes `token_budget` live refuses the whole request on a bad value.
|
|
if (tokenBudget !== undefined && typeof tokenBudget !== "number") {
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "bad budget" },
|
|
});
|
|
return true;
|
|
}
|
|
if (typeof loud === "boolean") state.loud = loud;
|
|
if (typeof tokenBudget === "number") state.tokenBudget = tokenBudget;
|
|
// Going loud returns an unrequested token budget to its default, by leaving it out.
|
|
if (loud === true && tokenBudget === undefined) state.tokenBudget = undefined;
|
|
emitConfig(emit, input.sessionId);
|
|
emit(
|
|
refuseAfterChanging
|
|
? { type: "request.failed", requestId: input.requestId, error: { message: "no" } }
|
|
: { type: "request.completed", requestId: input.requestId },
|
|
);
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
const plugins = new PluginAgentClientRegistry(createTestLogger());
|
|
plugins.replace([harness.registration]);
|
|
const session = await plugins.clients()["plugin-direct"]!.createSession({
|
|
provider: "plugin-direct",
|
|
cwd: tmpdir(),
|
|
providerOptions: given,
|
|
});
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
const changeOnItsOwn = (change: Partial<typeof state>) => {
|
|
Object.assign(state, change);
|
|
emitConfig(harness.emit, openedSessionId);
|
|
};
|
|
return { harness, session, events, changeOnItsOwn };
|
|
}
|
|
|
|
test("forwards a setting the plugin changes on its own", async () => {
|
|
const { session, events, changeOnItsOwn } = await openStubbornVoiceSession({});
|
|
|
|
changeOnItsOwn({ loud: true });
|
|
|
|
expect(eventsOfType(events, "options_changed")).toEqual([
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { loud: true } },
|
|
]);
|
|
await session.close();
|
|
});
|
|
|
|
test.runIf(shape === "controls")(
|
|
"reports an option the plugin stops reporting as removed, alone or with another change",
|
|
async () => {
|
|
const { session, events, changeOnItsOwn } = await openStubbornVoiceSession({});
|
|
changeOnItsOwn({ tokenBudget: 500 });
|
|
|
|
changeOnItsOwn({ tokenBudget: undefined });
|
|
changeOnItsOwn({ tokenBudget: 700 });
|
|
changeOnItsOwn({ tokenBudget: undefined, loud: true });
|
|
|
|
expect(eventsOfType(events, "options_changed")).toEqual([
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { token_budget: 500 } },
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { token_budget: null } },
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { token_budget: 700 } },
|
|
{
|
|
type: "options_changed",
|
|
provider: "plugin-direct",
|
|
patch: { token_budget: null, loud: true },
|
|
},
|
|
]);
|
|
await session.close();
|
|
},
|
|
);
|
|
|
|
test.runIf(shape === "controls")(
|
|
"an apply returns the option the plugin reset along the way as removed",
|
|
async () => {
|
|
const { session, events, changeOnItsOwn } = await openStubbornVoiceSession({});
|
|
changeOnItsOwn({ tokenBudget: 500 });
|
|
|
|
const ended = await session.applyOptions?.({ loud: true });
|
|
|
|
expect(ended).toEqual({ token_budget: null });
|
|
// The apply reports it; the plugin's later identical report is not a second change.
|
|
expect(eventsOfType(events, "options_changed")).toEqual([
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { token_budget: 500 } },
|
|
]);
|
|
await session.close();
|
|
},
|
|
);
|
|
|
|
test("a rejected apply reports what the plugin moved before refusing, since nothing is stored", async () => {
|
|
const { session, events } = await openStubbornVoiceSession({}, { refuseAfterChanging: true });
|
|
|
|
await expect(session.applyOptions?.({ loud: true })).rejects.toThrow("no");
|
|
|
|
expect(eventsOfType(events, "options_changed")).toEqual([
|
|
{ type: "options_changed", provider: "plugin-direct", patch: { loud: true } },
|
|
]);
|
|
await session.close();
|
|
});
|
|
|
|
test("a setting the plugin opened differently is the session's open patch, not an event", async () => {
|
|
const { session, events } = await openStubbornVoiceSession({ voice: "other", loud: false });
|
|
|
|
expect(session.openOptionsPatch).toEqual({ voice: "direct" });
|
|
expect(eventsOfType(events, "options_changed")).toEqual([]);
|
|
await session.close();
|
|
});
|
|
|
|
test("options the plugin opened with that nobody set are in the open patch, defaults included", async () => {
|
|
const { session, events } = await openStubbornVoiceSession({});
|
|
|
|
expect(session.openOptionsPatch).toEqual({ voice: "direct", loud: false });
|
|
expect(eventsOfType(events, "options_changed")).toEqual([]);
|
|
await session.close();
|
|
});
|
|
|
|
test("applying options configures only the settings it is given", async () => {
|
|
const { harness, session } = await openStubbornVoiceSession({});
|
|
|
|
await session.applyOptions?.({ loud: true });
|
|
|
|
const configure = lastConfigure(harness.inputs);
|
|
// Only a plugin that reports `settings` is also sent them.
|
|
expect((configure as { changes: object }).changes).toEqual(
|
|
shape === "settings"
|
|
? { providerOptions: { loud: true }, settings: { loud: true } }
|
|
: { providerOptions: { loud: true } },
|
|
);
|
|
await session.close();
|
|
});
|
|
|
|
test("a removal reaches the plugin as null", async () => {
|
|
const { harness, session } = await openStubbornVoiceSession({});
|
|
|
|
const ended = await session.applyOptions?.({ loud: null });
|
|
|
|
expect(lastConfigure(harness.inputs)).toMatchObject({
|
|
changes: { providerOptions: { loud: null } },
|
|
});
|
|
// It went back to its default, and what it ended up with is reported like any provider's.
|
|
expect(ended).toEqual({ loud: false });
|
|
await session.close();
|
|
});
|
|
|
|
test("draws a control from what the plugin reports", async () => {
|
|
const { session } = await openStubbornVoiceSession({});
|
|
|
|
expect((session.controls ?? []).map(controlKey)).toEqual(["voice", "loud"]);
|
|
await session.close();
|
|
});
|
|
|
|
test.runIf(shape === "controls")(
|
|
"keeps a control's icon, default and choices as the plugin declared them",
|
|
async () => {
|
|
const { session } = await openStubbornVoiceSession({});
|
|
|
|
expect(session.controls?.[0]).toMatchObject({
|
|
icon: "Mic",
|
|
defaultValue: "direct",
|
|
choices: [
|
|
{ label: "Direct", value: "direct" },
|
|
{ label: "Other", value: "other" },
|
|
],
|
|
});
|
|
await session.close();
|
|
},
|
|
);
|
|
|
|
test.runIf(shape === "controls")(
|
|
"sends a live option that has no control, and the plugin's answer decides",
|
|
async () => {
|
|
const { harness, session, events } = await openStubbornVoiceSession({});
|
|
|
|
await session.applyOptions?.({ token_budget: 2000 });
|
|
expect(lastConfigure(harness.inputs)).toMatchObject({
|
|
changes: { providerOptions: { token_budget: 2000 } },
|
|
});
|
|
|
|
// The plugin refuses the request as a whole, so the other key it was given is not applied.
|
|
await expect(session.applyOptions?.({ loud: true, token_budget: "lots" })).rejects.toThrow(
|
|
"bad budget",
|
|
);
|
|
expect(eventsOfType(events, "options_changed")).toEqual([]);
|
|
expect(await session.applyOptions?.({ token_budget: 3000 })).toEqual({});
|
|
await session.close();
|
|
},
|
|
);
|
|
|
|
test("sends a key the plugin does not report, for the plugin to accept or refuse", async () => {
|
|
const { harness, session } = await openStubbornVoiceSession({});
|
|
|
|
await session.applyOptions?.({ loud: true, launch_key: "changed" });
|
|
|
|
const configure = lastConfigure(harness.inputs) as {
|
|
changes: { providerOptions: object; settings?: object };
|
|
};
|
|
expect(configure.changes.providerOptions).toEqual({ loud: true, launch_key: "changed" });
|
|
// A plugin reporting `settings` is sent the same keys under that name.
|
|
expect(configure.changes.settings).toEqual(
|
|
shape === "settings" ? { loud: true, launch_key: "changed" } : undefined,
|
|
);
|
|
await session.close();
|
|
});
|
|
|
|
test("applying options returns the settings that ended up different, and emits nothing", async () => {
|
|
const { session, events } = await openStubbornVoiceSession({});
|
|
|
|
const ended = await session.applyOptions?.({ voice: "other", loud: true });
|
|
|
|
expect(ended).toEqual({ voice: "direct" });
|
|
expect(eventsOfType(events, "options_changed")).toEqual([]);
|
|
await session.close();
|
|
});
|
|
});
|
|
|
|
test.each([
|
|
{ options: { nested: { base: true, replace: "config" }, list: [1, 2], scalar: "config" } },
|
|
{ params: { nested: { base: true, replace: "config" }, list: [1, 2], scalar: "config" } },
|
|
{
|
|
params: { ignored: true },
|
|
options: { nested: { base: true, replace: "config" }, list: [1, 2], scalar: "config" },
|
|
},
|
|
])(
|
|
"merges configured options into plugin session.open, preserving caller input: %j",
|
|
async (override) => {
|
|
const logger = createTestLogger();
|
|
const harness = createProviderHarness();
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([harness.registration]);
|
|
const registry = buildProviderRegistry(logger, {
|
|
pluginProviders: plugins.definitions(),
|
|
providerOverrides: { "plugin-direct": ProviderOverrideSchema.parse(override) },
|
|
});
|
|
const client = registry["plugin-direct"].createClient(logger);
|
|
const defaultsSession = await client.createSession({
|
|
provider: "plugin-direct",
|
|
cwd: "/tmp",
|
|
});
|
|
const defaultsOpen = harness.inputs.findLast((input) => input.type === "session.open")!;
|
|
expect(defaultsOpen.config.providerOptions).toEqual({
|
|
nested: { base: true, replace: "config" },
|
|
list: [1, 2],
|
|
scalar: "config",
|
|
});
|
|
await defaultsSession.close();
|
|
const config = {
|
|
provider: "plugin-direct",
|
|
cwd: "/tmp",
|
|
providerOptions: { nested: { replace: "agent" }, list: [3], scalar: null },
|
|
};
|
|
const session = await client.createSession(config);
|
|
const open = harness.inputs.findLast((input) => input.type === "session.open")!;
|
|
expect(open.config.providerOptions).toEqual({
|
|
nested: { base: true, replace: "agent" },
|
|
list: [3],
|
|
scalar: null,
|
|
});
|
|
expect(config.providerOptions).toEqual({
|
|
nested: { replace: "agent" },
|
|
list: [3],
|
|
scalar: null,
|
|
});
|
|
const handle = session.describePersistence()!;
|
|
await session.close();
|
|
const updatedRegistry = buildProviderRegistry(logger, {
|
|
pluginProviders: plugins.definitions(),
|
|
providerOverrides: {
|
|
"plugin-direct": {
|
|
options: {
|
|
nested: { base: false, replace: "new-config" },
|
|
list: [4],
|
|
scalar: "new-config",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const resumed = await updatedRegistry["plugin-direct"]
|
|
.createClient(logger)
|
|
.resumeSession(handle, config);
|
|
const resumedOpen = harness.inputs.findLast((input) => input.type === "session.open")!;
|
|
expect(resumedOpen.config.providerOptions).toEqual({
|
|
nested: { base: false, replace: "agent" },
|
|
list: [3],
|
|
scalar: null,
|
|
});
|
|
await resumed.close();
|
|
},
|
|
);
|
|
|
|
test.each([false, true])(
|
|
"contains a failed session open while send is pending: %s",
|
|
async (pendingSend) => {
|
|
let listener: ((event: ProviderEvent) => void) | undefined;
|
|
const unhandled: unknown[] = [];
|
|
const observeUnhandled = (reason: unknown) => unhandled.push(reason);
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([
|
|
{
|
|
id: "failing-provider",
|
|
label: "Failing provider",
|
|
async connect() {
|
|
return {
|
|
version: 1,
|
|
capabilities: ["session.persistence"],
|
|
async send(input) {
|
|
if (input.type !== "session.open") return;
|
|
listener?.({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "OMP persistent session registration is in progress" },
|
|
});
|
|
// Keep acceptance pending across a Node event-loop turn, as IPC can do.
|
|
if (pendingSend) await nextTurn();
|
|
},
|
|
onEvent(nextListener) {
|
|
listener = nextListener;
|
|
return () => {
|
|
listener = undefined;
|
|
};
|
|
},
|
|
async close() {},
|
|
};
|
|
},
|
|
},
|
|
]);
|
|
process.on("unhandledRejection", observeUnhandled);
|
|
try {
|
|
await expect(
|
|
registry.clients()["failing-provider"]!.createSession({
|
|
provider: "failing-provider",
|
|
cwd: "/workspace",
|
|
}),
|
|
).rejects.toThrow("OMP persistent session registration is in progress");
|
|
await nextTurn();
|
|
expect(unhandled).toEqual([]);
|
|
} finally {
|
|
await registry.shutdown();
|
|
process.off("unhandledRejection", observeUnhandled);
|
|
}
|
|
},
|
|
);
|
|
|
|
test("preserves nested provider child ownership during opening", async () => {
|
|
const harness = createProviderHarness({ openChildren: openNestedChildren });
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
try {
|
|
const events: AgentStreamEvent[] = [];
|
|
for await (const event of session.streamHistory()) events.push(event);
|
|
expectNestedChildren(events);
|
|
} finally {
|
|
await session.close();
|
|
registry.replace([]);
|
|
}
|
|
});
|
|
|
|
test("preserves nested provider child ownership during live events", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
try {
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
openNestedChildren(open.sessionId, harness.emit);
|
|
expectNestedChildren(events);
|
|
} finally {
|
|
await session.close();
|
|
registry.replace([]);
|
|
}
|
|
});
|
|
|
|
test.each(["completed", "canceled", "failed"] as const)(
|
|
"reports a reused plugin child as running until its next turn is %s",
|
|
async (outcome) => {
|
|
const logger = createTestLogger();
|
|
const harness = createProviderHarness();
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([harness.registration]);
|
|
const registry = buildProviderRegistry(logger, { pluginProviders: plugins.definitions() });
|
|
const manager = new AgentManager({
|
|
logger,
|
|
clients: { "plugin-direct": registry["plugin-direct"].createClient(logger) },
|
|
providerDefinitions: { "plugin-direct": registry["plugin-direct"] },
|
|
});
|
|
try {
|
|
const agent = await manager.createAgent(
|
|
{ provider: "plugin-direct", cwd: tmpdir() },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
harness.emit({
|
|
type: "session.opened",
|
|
sessionId: "reusable-child",
|
|
parentSessionId: open.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
title: "Plugin child",
|
|
cwd: tmpdir(),
|
|
});
|
|
harness.emit({
|
|
type: "session.turn",
|
|
sessionId: "reusable-child",
|
|
turnId: "first-turn",
|
|
state: "started",
|
|
});
|
|
harness.emit({
|
|
type: "session.turn",
|
|
sessionId: "reusable-child",
|
|
turnId: "first-turn",
|
|
state: "completed",
|
|
});
|
|
await nextTurn();
|
|
const [child] = manager.listProviderSubagents(agent.id);
|
|
expect(child).toMatchObject({
|
|
id: "reusable-child",
|
|
title: "Plugin child",
|
|
status: "completed",
|
|
});
|
|
|
|
harness.emit({
|
|
type: "session.turn",
|
|
sessionId: "reusable-child",
|
|
turnId: "second-turn",
|
|
state: "started",
|
|
});
|
|
await nextTurn();
|
|
expect(manager.listProviderSubagents(agent.id)).toMatchObject([
|
|
{ id: child.id, title: child.title, status: "running" },
|
|
]);
|
|
|
|
harness.emit({
|
|
type: "session.turn",
|
|
sessionId: "reusable-child",
|
|
turnId: "second-turn",
|
|
state: outcome,
|
|
});
|
|
await nextTurn();
|
|
expect(manager.listProviderSubagents(agent.id)).toMatchObject([
|
|
{ id: child.id, title: child.title, status: outcome },
|
|
]);
|
|
harness.emit({ type: "session.closed", sessionId: "reusable-child" });
|
|
await nextTurn();
|
|
expect(manager.listProviderSubagents(agent.id)).toMatchObject([
|
|
{ id: child.id, title: child.title, status: outcome },
|
|
]);
|
|
} finally {
|
|
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
|
|
await plugins.shutdown();
|
|
}
|
|
},
|
|
);
|
|
|
|
test.each(["started", "completed", "canceled", "failed"] as const)(
|
|
"replays a reused plugin child's latest turn state: %s",
|
|
async (state) => {
|
|
const harness = createProviderHarness({
|
|
openChildren(rootSessionId, emit) {
|
|
emit({
|
|
type: "session.opened",
|
|
sessionId: "reusable-child",
|
|
parentSessionId: rootSessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: "/workspace",
|
|
});
|
|
for (const [turnId, turnState] of [
|
|
["first-turn", "started"],
|
|
["first-turn", "completed"],
|
|
["second-turn", "started"],
|
|
] as const) {
|
|
emit({ type: "session.turn", sessionId: "reusable-child", turnId, state: turnState });
|
|
}
|
|
if (state !== "started") {
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: "reusable-child",
|
|
turnId: "second-turn",
|
|
state,
|
|
});
|
|
emit({ type: "session.closed", sessionId: "reusable-child" });
|
|
}
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
try {
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
const statuses = [];
|
|
for await (const event of session.streamHistory()) {
|
|
if (
|
|
event.type === "provider_subagent" &&
|
|
event.event.type === "upsert" &&
|
|
event.event.id === "reusable-child"
|
|
) {
|
|
statuses.push(event.event.status);
|
|
}
|
|
}
|
|
expect(statuses.at(-1)).toBe(state === "started" ? "running" : state);
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
},
|
|
);
|
|
test("keeps a child's canceled or failed turn when the child closes normally", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
try {
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
const outcomes = [
|
|
["child-canceled", "canceled"],
|
|
["child-failed", "failed"],
|
|
["child-completed", "completed"],
|
|
] as const;
|
|
for (const [sessionId, state] of outcomes) {
|
|
harness.emit({
|
|
type: "session.opened",
|
|
sessionId,
|
|
parentSessionId: open.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: "/workspace",
|
|
});
|
|
harness.emit({ type: "session.turn", sessionId, turnId: "turn", state: "started" });
|
|
harness.emit({ type: "session.turn", sessionId, turnId: "turn", state });
|
|
harness.emit({ type: "session.closed", sessionId });
|
|
}
|
|
|
|
const lastStatus = (id: string) =>
|
|
events
|
|
.flatMap((event) =>
|
|
event.type === "provider_subagent" &&
|
|
event.event.type === "upsert" &&
|
|
event.event.id === id
|
|
? [event.event.status]
|
|
: [],
|
|
)
|
|
.at(-1);
|
|
expect(lastStatus("child-canceled")).toBe("canceled");
|
|
expect(lastStatus("child-failed")).toBe("failed");
|
|
expect(lastStatus("child-completed")).toBe("completed");
|
|
} finally {
|
|
await session.close();
|
|
registry.replace([]);
|
|
}
|
|
});
|
|
|
|
test.each([
|
|
{ turns: [], error: false, expected: "completed" },
|
|
{ turns: ["canceled", "started"], error: false, expected: "completed" },
|
|
{ turns: ["failed", "started", "completed"], error: false, expected: "completed" },
|
|
{ turns: ["canceled"], error: true, expected: "failed" },
|
|
] as const)(
|
|
"closes a plugin child using its latest turn: $turns, error=$error",
|
|
async ({ turns, error, expected }) => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
try {
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: tmpdir(),
|
|
});
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
const open = harness.inputs.find((input) => input.type === "session.open")!;
|
|
harness.emit({
|
|
type: "session.opened",
|
|
sessionId: "close-control",
|
|
parentSessionId: open.sessionId,
|
|
capabilities: [],
|
|
restoration: "parent",
|
|
cwd: tmpdir(),
|
|
});
|
|
for (const state of turns) {
|
|
harness.emit({ type: "session.turn", sessionId: "close-control", turnId: "turn", state });
|
|
}
|
|
harness.emit({
|
|
type: "session.closed",
|
|
sessionId: "close-control",
|
|
...(error ? { error: { message: "Provider failed to close" } } : {}),
|
|
});
|
|
expect(events.at(-1)).toMatchObject({
|
|
type: "provider_subagent",
|
|
event: { type: "upsert", id: "close-control", status: expected },
|
|
});
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
},
|
|
);
|
|
|
|
test("gates persistence operations on negotiated provider capabilities", async () => {
|
|
const harness = createProviderHarness({ capabilities: ["session.persistence"] });
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
|
|
await expect(client.isAvailable()).resolves.toBe(true);
|
|
expect(client.capabilities).toMatchObject({
|
|
supportsSessionPersistence: true,
|
|
supportsSessionListing: false,
|
|
});
|
|
await expect(client.listImportableSessions?.()).resolves.toEqual([]);
|
|
|
|
const persistence = {
|
|
provider: harness.registration.id,
|
|
sessionId: 'plugin:{"version":1,"data":{"token":"root"}}',
|
|
metadata: { pluginProviderPersistence: { version: 1, data: { token: "root" } } },
|
|
};
|
|
await expect(client.archiveNativeSession?.(persistence)).resolves.toBeUndefined();
|
|
await expect(client.unarchiveNativeSession?.(persistence)).resolves.toBeUndefined();
|
|
expect(harness.inputs).toEqual([]);
|
|
await registry.shutdown();
|
|
});
|
|
|
|
test("terminalizes an active turn exactly once when its plugin provider is removed", async () => {
|
|
const harness = createProviderHarness({ completeTurn: false });
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
|
|
const run = session.run("hello", { clientMessageId: "active-message" });
|
|
void run.catch(() => undefined);
|
|
await expect.poll(() => eventsOfType(events, "turn_started")).toHaveLength(1);
|
|
|
|
registry.replace([]);
|
|
|
|
await expect
|
|
.poll(() => eventsOfType(events, "turn_failed"))
|
|
.toEqual([
|
|
expect.objectContaining({
|
|
type: "turn_failed",
|
|
provider: harness.registration.id,
|
|
turnId: "turn-1",
|
|
error: "Provider connection closed",
|
|
}),
|
|
]);
|
|
await expect(run).rejects.toThrow("Provider connection closed");
|
|
await expect.poll(harness.closeCount).toBe(1);
|
|
|
|
registry.replace([]);
|
|
expect(eventsOfType(events, "turn_failed")).toHaveLength(1);
|
|
});
|
|
|
|
test("leaves a completed session without a failure when its plugin provider is removed", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
|
|
await session.run("hello", { clientMessageId: "completed-message" });
|
|
expect(eventsOfType(events, "turn_completed")).toHaveLength(1);
|
|
|
|
registry.replace([]);
|
|
await harness.waitForClose();
|
|
|
|
expect(eventsOfType(events, "turn_failed")).toEqual([]);
|
|
await expect(
|
|
session.startTurn("after reload", { clientMessageId: "after-reload" }),
|
|
).rejects.toBeInstanceOf(StaleProviderSessionError);
|
|
});
|
|
|
|
test("fails an accepted turn that has not started when its plugin provider is removed", async () => {
|
|
const harness = createProviderHarness({
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.prompt") return false;
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "turn", turnId: "accepted-turn" },
|
|
});
|
|
return true;
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
const events: AgentStreamEvent[] = [];
|
|
session.subscribe((event) => events.push(event));
|
|
|
|
await expect(
|
|
session.startTurn("hello", { clientMessageId: "accepted-message" }),
|
|
).resolves.toEqual({ turnId: "accepted-turn" });
|
|
|
|
registry.replace([]);
|
|
await harness.waitForClose();
|
|
|
|
expect(eventsOfType(events, "turn_failed")).toEqual([
|
|
expect.objectContaining({ error: "Provider connection closed" }),
|
|
]);
|
|
});
|
|
|
|
test("closes a stale session after its plugin provider is replaced", async () => {
|
|
const old = createProviderHarness();
|
|
const next = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
|
|
try {
|
|
registry.replace([old.registration]);
|
|
|
|
const stale = await registry.clients()[old.registration.id]!.createSession({
|
|
provider: old.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
const persistence = stale.describePersistence();
|
|
expect(persistence).not.toBeNull();
|
|
|
|
registry.replace([next.registration]);
|
|
await old.waitForClose();
|
|
expect(old.closeCount()).toBe(1);
|
|
|
|
await expect(stale.close()).resolves.toBeUndefined();
|
|
|
|
const replacement = registry.clients()[next.registration.id];
|
|
expect(replacement).toBeDefined();
|
|
const resumed = await replacement!.resumeSession(persistence!, {
|
|
cwd: "/workspace",
|
|
});
|
|
|
|
await expect(
|
|
resumed.startTurn("after reload", { clientMessageId: "after-reload" }),
|
|
).resolves.toEqual({ turnId: "turn-1" });
|
|
|
|
expect(next.inputs).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "session.open",
|
|
history: "replay",
|
|
persistence: {
|
|
version: 1,
|
|
data: { token: "root" },
|
|
},
|
|
}),
|
|
);
|
|
|
|
await resumed.close();
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("prompting a stale session raises StaleProviderSessionError", async () => {
|
|
const old = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
|
|
try {
|
|
registry.replace([old.registration]);
|
|
const stale = await registry.clients()[old.registration.id]!.createSession({
|
|
provider: old.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
|
|
registry.replace([]);
|
|
await old.waitForClose();
|
|
expect(old.closeCount()).toBe(1);
|
|
|
|
const failure = await stale
|
|
.startTurn("after reload", { clientMessageId: "after-reload" })
|
|
.then(
|
|
() => null,
|
|
(error: unknown) => error,
|
|
);
|
|
expect(failure).toBeInstanceOf(StaleProviderSessionError);
|
|
expect(isStaleProviderSessionError(failure)).toBe(true);
|
|
expect(isStaleProviderSessionError(new Error("Provider connection is closed"))).toBe(false);
|
|
expect(isStaleProviderSessionError(new Error("boom"))).toBe(false);
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("adapts callback providers into the existing AgentClient and AgentSession path", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id];
|
|
expect(client).toBeDefined();
|
|
|
|
await expect(client!.fetchCatalog({ scope: "global", force: false })).resolves.toMatchObject({
|
|
models: [
|
|
{
|
|
provider: "plugin-direct",
|
|
id: "plugin-model",
|
|
isDefault: true,
|
|
thinkingOptions: [{ id: "deep", label: "Deep" }],
|
|
defaultThinkingOptionId: "deep",
|
|
},
|
|
],
|
|
modes: [{ id: "build" }],
|
|
defaultModeId: "build",
|
|
});
|
|
|
|
const session = await client!.createSession({ provider: "plugin-direct", cwd: "/workspace" });
|
|
expect(session.controls).toEqual([
|
|
{
|
|
type: "select",
|
|
key: "voice",
|
|
label: "Voice",
|
|
choices: [{ value: "direct", label: "Direct" }],
|
|
},
|
|
]);
|
|
expect(session.describePersistence()).toMatchObject({
|
|
provider: "plugin-direct",
|
|
metadata: { pluginProviderPersistence: { version: 1, data: { token: "root" } } },
|
|
});
|
|
|
|
const history: AgentStreamEvent[] = [];
|
|
for await (const event of session.streamHistory()) history.push(event);
|
|
expect(history).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "provider_subagent",
|
|
event: expect.objectContaining({ type: "timeline", id: "child-1" }),
|
|
}),
|
|
);
|
|
expect(history).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "provider_subagent",
|
|
event: expect.objectContaining({ type: "upsert", id: "child-1", status: "completed" }),
|
|
}),
|
|
);
|
|
|
|
const events: AgentStreamEvent[] = [];
|
|
const unsubscribe = session.subscribe((event) => events.push(event));
|
|
await expect(
|
|
session.startTurn("hello", { clientMessageId: "client-message" }),
|
|
).resolves.toEqual({ turnId: "turn-1" });
|
|
expect(
|
|
events
|
|
.filter((event) => event.type === "timeline")
|
|
.map((event) => (event.type === "timeline" ? event.item : null)),
|
|
).toEqual([
|
|
{ type: "assistant_message", text: "Hel", messageId: "answer" },
|
|
{ type: "assistant_message", text: "lo", messageId: "answer" },
|
|
]);
|
|
expect(session.getPendingPermissions()).toEqual([
|
|
expect.objectContaining({ id: "permission-1", provider: "plugin-direct" }),
|
|
]);
|
|
|
|
await session.respondToPermission("permission-1", { behavior: "allow" });
|
|
expect(events).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "permission_resolved",
|
|
requestId: "permission-1",
|
|
resolution: { behavior: "allow" },
|
|
}),
|
|
);
|
|
await session.setModel?.("plugin-model");
|
|
expect(await session.getRuntimeInfo()).toMatchObject({
|
|
model: "plugin-model",
|
|
modeId: "build",
|
|
});
|
|
|
|
unsubscribe();
|
|
await session.close();
|
|
registry.replace([]);
|
|
await expect.poll(harness.closeCount).toBe(1);
|
|
expect(harness.inputs.map((input) => input.type)).toContain("session.close");
|
|
});
|
|
|
|
test("delivers a supported steer to the active turn", async () => {
|
|
const harness = createProviderHarness({
|
|
capabilities: [...CAPABILITIES, "prompt.steer"],
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.prompt") return false;
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "steer", turnId: "turn-1" },
|
|
});
|
|
return true;
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: harness.registration.id, cwd: "/tmp" });
|
|
try {
|
|
await expect(
|
|
session.steerActiveTurn!("follow up", {
|
|
expectedTurnId: "turn-1",
|
|
clientMessageId: "steer-1",
|
|
clearPendingPermissions: true,
|
|
}),
|
|
).resolves.toEqual({ status: "accepted" });
|
|
expect(harness.inputs.find((input) => input.type === "session.prompt")).toMatchObject({
|
|
prompt: {
|
|
delivery: "steer",
|
|
clientMessageId: "steer-1",
|
|
input: { type: "message", content: [{ type: "text", text: "follow up" }] },
|
|
clearPendingPermissions: true,
|
|
},
|
|
});
|
|
} finally {
|
|
await session.close();
|
|
registry.replace([]);
|
|
}
|
|
});
|
|
|
|
test("offers no steering when the provider did not negotiate prompt.steer", async () => {
|
|
const harness = createProviderHarness({
|
|
capabilities: [
|
|
"prompt.message",
|
|
"session.configure",
|
|
"session.persistence",
|
|
"session.subsession",
|
|
"permission",
|
|
],
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const session = await registry.clients()[harness.registration.id]!.createSession({
|
|
provider: harness.registration.id,
|
|
cwd: "/workspace",
|
|
});
|
|
try {
|
|
expect(session.steerActiveTurn).toBeUndefined();
|
|
} finally {
|
|
await session.close();
|
|
registry.replace([]);
|
|
}
|
|
});
|
|
});
|
|
|
|
type RequestKind = "session.open" | "catalog" | "session.configure" | "session.prompt";
|
|
|
|
async function requestFromClient(client: AgentClient, kind: RequestKind): Promise<unknown> {
|
|
if (kind === "catalog") return client.fetchCatalog({ scope: "global" });
|
|
const session = await client.createSession({ provider: client.provider, cwd: "/workspace" });
|
|
if (kind === "session.open") return session;
|
|
if (kind === "session.configure") return session.setMode("build");
|
|
return session.startTurn("hello", { clientMessageId: "pending-message" });
|
|
}
|
|
|
|
async function withObservedProvider(
|
|
options: ProviderHarnessOptions,
|
|
run: (client: AgentClient, registry: PluginAgentClientRegistry) => Promise<void>,
|
|
): Promise<void> {
|
|
const harness = createProviderHarness(options);
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
const unhandled: unknown[] = [];
|
|
const observe = (reason: unknown) => unhandled.push(reason);
|
|
process.on("unhandledRejection", observe);
|
|
registry.replace([harness.registration]);
|
|
try {
|
|
await run(registry.clients()[harness.registration.id]!, registry);
|
|
await registry.shutdown();
|
|
await nextTurn();
|
|
expect(unhandled).toEqual([]);
|
|
} finally {
|
|
await registry.shutdown();
|
|
process.off("unhandledRejection", observe);
|
|
}
|
|
}
|
|
|
|
test("commits notices emitted during session open as the initial timeline with stable history timestamps", async () => {
|
|
await withObservedProvider(
|
|
{
|
|
openChildren(sessionId, emit) {
|
|
emit({
|
|
type: "session.notice",
|
|
sessionId,
|
|
notice: {
|
|
id: "startup",
|
|
severity: "warning",
|
|
title: "Full access",
|
|
description: "Tools run without asking.",
|
|
},
|
|
});
|
|
emit({
|
|
type: "session.notice",
|
|
sessionId,
|
|
notice: { id: "dismissed", severity: "info", title: "Hidden", dismissed: true },
|
|
});
|
|
},
|
|
},
|
|
async (client) => {
|
|
const session = await client.createSession({ provider: client.provider, cwd: "/workspace" });
|
|
try {
|
|
expect(session.initialTimeline).toEqual([
|
|
{
|
|
timestamp: expect.any(String),
|
|
item: {
|
|
type: "notification",
|
|
level: "warning",
|
|
message: "Full access\nTools run without asking.",
|
|
},
|
|
},
|
|
]);
|
|
const history: AgentStreamEvent[] = [];
|
|
for await (const event of session.streamHistory()) history.push(event);
|
|
expect(
|
|
history.filter(
|
|
(event) => event.type === "timeline" && event.item.type === "notification",
|
|
),
|
|
).toEqual([
|
|
{ type: "timeline", provider: client.provider, ...session.initialTimeline![0] },
|
|
]);
|
|
} finally {
|
|
await session.close();
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("pending provider responses", () => {
|
|
test.each<RequestKind>(["session.open", "catalog", "session.configure", "session.prompt"])(
|
|
"preserves the send error for %s",
|
|
async (kind) => {
|
|
const failure = new Error("Provider send failed");
|
|
await withObservedProvider(
|
|
{
|
|
async handleInput(input) {
|
|
if (input.type === kind) throw failure;
|
|
return false;
|
|
},
|
|
},
|
|
async (client) => {
|
|
await expect(requestFromClient(client, kind)).rejects.toBe(failure);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
[
|
|
"catalog",
|
|
expect.objectContaining({
|
|
models: [expect.objectContaining({ id: "plugin-model", isDefault: true })],
|
|
defaultModeId: "build",
|
|
}),
|
|
],
|
|
["session.configure", undefined],
|
|
] as const)(
|
|
"contains an early request.failed for %s and permits a successful retry",
|
|
async (kind, result) => {
|
|
let failed = false;
|
|
await withObservedProvider(
|
|
{
|
|
async handleInput(input, emit) {
|
|
if (input.type !== kind || !("requestId" in input) || failed) return false;
|
|
failed = true;
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "Provider request failed", code: "busy", diagnostic: "retry" },
|
|
});
|
|
await nextTurn();
|
|
return true;
|
|
},
|
|
},
|
|
async (client) => {
|
|
await expect(requestFromClient(client, kind)).rejects.toMatchObject({
|
|
message: "Provider request failed",
|
|
code: "busy",
|
|
diagnostic: "retry",
|
|
});
|
|
await expect(requestFromClient(client, kind)).resolves.toEqual(result);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
["session.open", "Provider session closed"],
|
|
["session.configure", "Provider session closed"],
|
|
["session.prompt", StaleProviderSessionError],
|
|
] as const)("contains session closure while accepting %s", async (kind, error) => {
|
|
await withObservedProvider(
|
|
{
|
|
async handleInput(input, emit) {
|
|
if (input.type !== kind || !("sessionId" in input)) return false;
|
|
emit({
|
|
type: "session.closed",
|
|
sessionId: input.sessionId,
|
|
error: { message: "Provider session closed" },
|
|
});
|
|
await nextTurn();
|
|
return true;
|
|
},
|
|
},
|
|
async (client) => {
|
|
await expect(requestFromClient(client, kind)).rejects.toThrow(error);
|
|
},
|
|
);
|
|
});
|
|
|
|
test.each([
|
|
["session.open", "Provider connection closed"],
|
|
["catalog", "Provider closed"],
|
|
["session.configure", "Provider closed"],
|
|
["session.prompt", StaleProviderSessionError],
|
|
] as const)("contains connection shutdown while accepting %s", async (kind, error) => {
|
|
let disconnect!: () => Promise<void>;
|
|
await withObservedProvider(
|
|
{
|
|
async handleInput(input) {
|
|
if (input.type !== kind) return false;
|
|
await disconnect();
|
|
await nextTurn();
|
|
return true;
|
|
},
|
|
},
|
|
async (client, registry) => {
|
|
disconnect = () => registry.shutdown();
|
|
await expect(requestFromClient(client, kind)).rejects.toThrow(error);
|
|
},
|
|
);
|
|
});
|
|
|
|
test.each([true, false])(
|
|
"preserves cancellation with acceptance pending: %s",
|
|
async (pending) => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("Catalog refresh cancelled");
|
|
await withObservedProvider(
|
|
{
|
|
async handleInput(input) {
|
|
if (input.type !== "catalog") return false;
|
|
if (pending) {
|
|
controller.abort(reason);
|
|
await nextTurn();
|
|
} else {
|
|
setImmediate(() => controller.abort(reason));
|
|
}
|
|
return true;
|
|
},
|
|
},
|
|
async (client) => {
|
|
await expect(
|
|
client.fetchCatalog({ scope: "global" }, { signal: controller.signal }),
|
|
).rejects.toBe(reason);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("plugin provider launch and status", () => {
|
|
test("resolves the executable against the provider's PATH override", async () => {
|
|
const harness = createProviderHarness();
|
|
const registration: ProviderRegistration = { ...harness.registration, command: ["node"] };
|
|
const logger = createTestLogger();
|
|
const registry = new PluginAgentClientRegistry(logger);
|
|
registry.replace([registration]);
|
|
try {
|
|
const definition = registry.definitions()[registration.id]!;
|
|
const client = definition.createClient(logger, { env: { PATH: "/paseo-nonexistent-bin" } });
|
|
expect(await client.isAvailable()).toBe(false);
|
|
expect(await client.getDiagnostic!()).toEqual({ diagnostic: "node not found on PATH" });
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("strips parent-session and daemon-control variables from session env overlays", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
try {
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
await client.createSession(
|
|
{ provider: harness.registration.id, cwd: "/workspace" },
|
|
{
|
|
env: {
|
|
SESSION_TOKEN: "session",
|
|
PASEO_AGENT_ID: "agent",
|
|
CLAUDECODE: "parent",
|
|
PASEO_NODE_ENV: "development",
|
|
},
|
|
},
|
|
);
|
|
const input = harness.inputs.find((value) => value.type === "session.open");
|
|
expect(input).toMatchObject({
|
|
config: { env: { SESSION_TOKEN: "session", PASEO_AGENT_ID: "agent" } },
|
|
});
|
|
if (input?.type !== "session.open") throw new Error("Expected session.open");
|
|
expect(input.config.env).not.toHaveProperty("CLAUDECODE");
|
|
expect(input.config.env).not.toHaveProperty("PASEO_NODE_ENV");
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("exposes the session launch for usage discovery", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([{ ...harness.registration, command: [process.execPath, "acp"] }]);
|
|
try {
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession(
|
|
{ provider: harness.registration.id, cwd: "/workspace" },
|
|
{ env: { HOME: "/session-home" } },
|
|
);
|
|
expect(session.usageSession?.()).toMatchObject({
|
|
provider: harness.registration.id,
|
|
model: "plugin-model",
|
|
env: { HOME: "/session-home" },
|
|
launch: { command: process.execPath, args: ["acp"], env: { HOME: "/session-home" } },
|
|
sessionKey: expect.any(String),
|
|
});
|
|
await session.close();
|
|
expect(session.usageSession?.()).toBeNull();
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("resolves relative executable paths before sending launch data", async () => {
|
|
const harness = createProviderHarness();
|
|
const launches: Array<ProviderLaunch | undefined> = [];
|
|
const registration: ProviderRegistration = {
|
|
...harness.registration,
|
|
command: [relative(process.cwd(), process.execPath)],
|
|
async connect(request) {
|
|
launches.push(request.launch);
|
|
return harness.registration.connect(request);
|
|
},
|
|
};
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([registration]);
|
|
try {
|
|
await registry.clients()[registration.id]!.fetchCatalog({ scope: "global" });
|
|
expect(launches).toMatchObject([{ command: process.execPath }]);
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("a missing executable is unavailable with a diagnostic without connecting", async () => {
|
|
const harness = createProviderHarness();
|
|
const registration: ProviderRegistration = {
|
|
...harness.registration,
|
|
command: ["paseo-nonexistent-provider-executable"],
|
|
};
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([registration]);
|
|
try {
|
|
const client = registry.clients()[registration.id]!;
|
|
expect(await client.isAvailable()).toBe(false);
|
|
expect(await client.getDiagnostic!()).toEqual({
|
|
diagnostic: "paseo-nonexistent-provider-executable not found on PATH",
|
|
});
|
|
expect(harness.inputs).toEqual([]);
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test.each([
|
|
{ mode: "default" as const },
|
|
{ mode: "append" as const, args: ["extra"] },
|
|
{ mode: "replace" as const, argv: [process.execPath, "replacement"] },
|
|
])(
|
|
"resolves $mode launch and supplies identical data to status, catalogue identity, and connect",
|
|
async (command) => {
|
|
const harness = createProviderHarness();
|
|
const launches: Array<ProviderLaunch | undefined> = [];
|
|
const registration: ProviderRegistration = {
|
|
...harness.registration,
|
|
command: [process.execPath, "default"],
|
|
async status(request) {
|
|
launches.push(request.launch);
|
|
return { available: true };
|
|
},
|
|
async getCatalogCacheKey(options) {
|
|
launches.push(options.launch);
|
|
return "shared";
|
|
},
|
|
async connect(request) {
|
|
launches.push(request.launch);
|
|
return harness.registration.connect(request);
|
|
},
|
|
};
|
|
const logger = createTestLogger();
|
|
const registry = new PluginAgentClientRegistry(logger);
|
|
registry.replace([registration]);
|
|
try {
|
|
const client = registry.definitions()[registration.id]!.createClient(logger, {
|
|
command,
|
|
env: { PLUGIN_SETTING: "custom", CLAUDECODE: "parent" },
|
|
});
|
|
expect(await client.isAvailable()).toBe(true);
|
|
expect(await client.getCatalogCacheKey!({ scope: "global" })).toBe("shared");
|
|
await client.fetchCatalog({ scope: "global" });
|
|
let expectedArgs = ["default"];
|
|
if (command.mode === "replace") expectedArgs = ["replacement"];
|
|
if (command.mode === "append") expectedArgs = ["default", "extra"];
|
|
expect(launches).toHaveLength(3);
|
|
for (const launch of launches) {
|
|
expect(launch).toMatchObject({
|
|
command: process.execPath,
|
|
args: expectedArgs,
|
|
env: { PLUGIN_SETTING: "custom" },
|
|
});
|
|
expect(launch!.env).not.toHaveProperty("CLAUDECODE");
|
|
expect(launch!.env).not.toHaveProperty("PASEO_NODE_ENV");
|
|
}
|
|
expect(launches[0]).toEqual(launches[1]);
|
|
expect(launches[1]).toEqual(launches[2]);
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
test("config-required plugin membership is independent of enablement and other providers", async () => {
|
|
const logger = createTestLogger();
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
const harness = createProviderHarness();
|
|
let connections = 0;
|
|
let probes = 0;
|
|
plugins.replace([
|
|
{
|
|
...harness.registration,
|
|
requiresConfiguration: true,
|
|
async status() {
|
|
probes++;
|
|
return { available: true };
|
|
},
|
|
async connect(request) {
|
|
connections++;
|
|
return harness.registration.connect(request);
|
|
},
|
|
},
|
|
]);
|
|
const build = (providerOverrides = {}) =>
|
|
buildProviderRegistry(logger, {
|
|
pluginProviders: plugins.definitions(),
|
|
providerOverrides,
|
|
});
|
|
try {
|
|
const before = build();
|
|
expect(before["plugin-direct"]).toBeUndefined();
|
|
expect({ probes, connections }).toEqual({ probes: 0, connections: 0 });
|
|
const added = build({ "plugin-direct": {} });
|
|
expect(added["plugin-direct"].enabled).toBe(true);
|
|
const client = added["plugin-direct"].createClient(logger);
|
|
expect(await client.isAvailable()).toBe(true);
|
|
const catalog = await client.fetchCatalog({ scope: "global" });
|
|
expect(catalog.models.map((model) => model.id)).toEqual(["plugin-model"]);
|
|
expect({ probes, connections }).toEqual({ probes: 1, connections: 1 });
|
|
const disabled = build({ "plugin-direct": { enabled: false } });
|
|
expect(disabled["plugin-direct"].enabled).toBe(false);
|
|
expect(Object.keys(build())).toEqual(Object.keys(before));
|
|
for (const id of Object.keys(before)) {
|
|
expect(added[id].enabled).toBe(before[id].enabled);
|
|
}
|
|
} finally {
|
|
await plugins.shutdown();
|
|
}
|
|
});
|
|
|
|
test.each([true, false, "error", "wrong-session", "missing"] as const)(
|
|
"plugin runtime safety query: %s",
|
|
async (answer) => {
|
|
const harness = createProviderHarness({
|
|
capabilities:
|
|
answer === "missing" ? CAPABILITIES : [...CAPABILITIES, "session.can_stop_runtime"],
|
|
handleInput: async (input, emit) => {
|
|
if (input.type !== "session.can_stop_runtime") return false;
|
|
if (answer === "error")
|
|
emit({
|
|
type: "request.failed",
|
|
requestId: input.requestId,
|
|
error: { message: "Probe failed" },
|
|
});
|
|
else
|
|
emit({
|
|
type: "session.can_stop_runtime",
|
|
requestId: input.requestId,
|
|
sessionId: answer === "wrong-session" ? "child-1" : input.sessionId,
|
|
canStop: answer === true,
|
|
});
|
|
return true;
|
|
},
|
|
});
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const session = await client.createSession({ provider: harness.registration.id, cwd: "/tmp" });
|
|
try {
|
|
if (answer === "error" || answer === "wrong-session") {
|
|
await expect(session.canStopRuntime!()).rejects.toThrow(
|
|
answer === "error" ? "Probe failed" : "another session",
|
|
);
|
|
} else if (answer === "missing") expect(session.canStopRuntime).toBeUndefined();
|
|
else expect(await session.canStopRuntime!()).toBe(answer === true);
|
|
const requests = harness.inputs.filter((input) => input.type === "session.can_stop_runtime");
|
|
expect(requests).toHaveLength(answer === "missing" ? 0 : 1);
|
|
if (answer !== "missing")
|
|
expect(requests[0]).toMatchObject({
|
|
sessionId: harness.inputs.find((input) => input.type === "session.open")?.sessionId,
|
|
});
|
|
} finally {
|
|
await session.close();
|
|
await registry.shutdown();
|
|
}
|
|
},
|
|
);
|
|
|
|
test("plugin without a runtime safety capability uses baseline collection without a query", async () => {
|
|
const home = await mkdtemp(join(tmpdir(), "plugin-baseline-cleanup-"));
|
|
const logger = createTestLogger();
|
|
const storage = new AgentStorage(join(home, "agents"), logger);
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(logger);
|
|
registry.replace([harness.registration]);
|
|
const manager = new AgentManager({ clients: registry.clients(), registry: storage, logger });
|
|
const agent = await manager.createAgent(
|
|
{ provider: harness.registration.id, cwd: home },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
const cleanup = createIdleRuntimeCleanup({
|
|
enabled: true,
|
|
logger,
|
|
runtimes: () => manager.getRuntimeCleanupCandidates(),
|
|
now: () => agent.updatedAt.getTime() + 60 * 60_000,
|
|
schedule: () => () => {},
|
|
});
|
|
try {
|
|
expect(agent.session?.canStopRuntime).toBeUndefined();
|
|
expect((await cleanup.sweep()).collected).toHaveLength(1);
|
|
expect(manager.getAgent(agent.id)).toBeNull();
|
|
expect(harness.inputs.filter((input) => input.type === "session.can_stop_runtime")).toEqual([]);
|
|
} finally {
|
|
await cleanup.close();
|
|
await Promise.all(manager.listAgents().map((item) => manager.closeAgent(item.id)));
|
|
await registry.shutdown();
|
|
await storage.flush();
|
|
await rm(home, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("plugin provider import launches with runtime MCP servers and returns only saved configuration", async () => {
|
|
const harness = createProviderHarness();
|
|
const registry = new PluginAgentClientRegistry(createTestLogger());
|
|
registry.replace([harness.registration]);
|
|
const client = registry.clients()[harness.registration.id]!;
|
|
const storedConfig = { provider: harness.registration.id, cwd: "/project" };
|
|
const company = { type: "http", url: "https://tools.example.com/mcp" } as const;
|
|
try {
|
|
const imported = await client.importSession!(
|
|
{ providerHandleId: 'plugin:{"version":1,"data":{"token":"root"}}', cwd: "/project" },
|
|
{ config: { ...storedConfig, mcpServers: { company } }, storedConfig },
|
|
);
|
|
expect(harness.inputs).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "session.open",
|
|
config: expect.objectContaining({ mcpServers: { company } }),
|
|
}),
|
|
);
|
|
expect(imported.config).toEqual(storedConfig);
|
|
await imported.session.close();
|
|
} finally {
|
|
await registry.shutdown();
|
|
}
|
|
});
|
|
|
|
test("resumes a goose session saved by the generic ACP provider", async () => {
|
|
const logger = createTestLogger();
|
|
const harness = createProviderHarness();
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([{ ...harness.registration, id: "goose" }]);
|
|
const registry = buildProviderRegistry(logger, { pluginProviders: plugins.definitions() });
|
|
const client = registry.goose.createClient(logger);
|
|
|
|
try {
|
|
const session = await client.resumeSession(
|
|
{
|
|
provider: "goose",
|
|
sessionId: "native-goose-session",
|
|
nativeHandle: "native-goose-session",
|
|
},
|
|
{ provider: "goose", cwd: "/tmp" },
|
|
);
|
|
expect(harness.inputs.findLast((input) => input.type === "session.open")).toMatchObject({
|
|
persistence: { version: 1, data: { sessionId: "native-goose-session" } },
|
|
});
|
|
await session.close();
|
|
} finally {
|
|
await plugins.shutdown();
|
|
}
|
|
});
|
|
|
|
test("preserves a deferred ACP background job on steer and cancels it on explicit Stop through AgentManager", async () => {
|
|
const logger = createTestLogger();
|
|
const home = await mkdtemp(join(tmpdir(), "paseo-acp-deferred-daemon-"));
|
|
const storage = new AgentStorage(join(home, "agents"), logger);
|
|
let cancelCount = 0;
|
|
let promptCount = 0;
|
|
let currentMode = "default";
|
|
let notifyAgent: ((message: AcpStreamMessage) => void) | null = null;
|
|
let holdNextPrompt: ((release: () => void) => void) | null = null;
|
|
const modeConfig = () => [
|
|
{
|
|
id: "mode",
|
|
name: "Mode",
|
|
category: "mode",
|
|
type: "select",
|
|
currentValue: currentMode,
|
|
options: [
|
|
{ value: "default", name: "Default" },
|
|
{ value: "plan", name: "Plan" },
|
|
],
|
|
},
|
|
];
|
|
const registration = runAcpProvider({
|
|
id: "deferred-acp-daemon",
|
|
label: "Deferred ACP Daemon",
|
|
acpOptions: {
|
|
deferTurnOnRunningTools: { settleDelayMs: 25, quietLimitMs: 1_000 },
|
|
},
|
|
connector() {
|
|
let controller!: ReadableStreamDefaultController<AcpStreamMessage>;
|
|
const readable = new ReadableStream<AcpStreamMessage>({
|
|
start(ctrl) {
|
|
controller = ctrl;
|
|
},
|
|
});
|
|
const send = (message: AcpStreamMessage) => controller.enqueue(message);
|
|
notifyAgent = send;
|
|
const writable = new WritableStream<AcpStreamMessage>({
|
|
write(message) {
|
|
if (!("method" in message)) return;
|
|
if (message.method === "session/cancel") {
|
|
cancelCount += 1;
|
|
return;
|
|
}
|
|
if (!("id" in message)) return;
|
|
if (message.method === "initialize") {
|
|
send({
|
|
jsonrpc: "2.0",
|
|
id: message.id,
|
|
result: { protocolVersion: 1, agentCapabilities: { loadSession: true } },
|
|
});
|
|
return;
|
|
}
|
|
if (message.method === "session/new") {
|
|
send({
|
|
jsonrpc: "2.0",
|
|
id: message.id,
|
|
result: { sessionId: "acp-s1", configOptions: modeConfig() },
|
|
});
|
|
return;
|
|
}
|
|
if (message.method === "session/load") {
|
|
send({ jsonrpc: "2.0", id: message.id, result: { configOptions: modeConfig() } });
|
|
return;
|
|
}
|
|
if (message.method === "session/set_config_option") {
|
|
const params = message.params as { value?: string } | undefined;
|
|
if (typeof params?.value === "string") currentMode = params.value;
|
|
send({ jsonrpc: "2.0", id: message.id, result: { configOptions: modeConfig() } });
|
|
return;
|
|
}
|
|
if (message.method === "session/prompt") {
|
|
promptCount += 1;
|
|
if (promptCount === 1) {
|
|
send({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: {
|
|
sessionUpdate: "tool_call",
|
|
toolCallId: "bg-job-1",
|
|
name: "run_command",
|
|
title: "Background job",
|
|
status: "in_progress",
|
|
},
|
|
},
|
|
});
|
|
} else if (promptCount === 2) {
|
|
send({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: {
|
|
sessionUpdate: "agent_message_chunk",
|
|
content: { type: "text", text: "Steered reply" },
|
|
},
|
|
},
|
|
});
|
|
} else {
|
|
send({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: {
|
|
sessionUpdate: "tool_call",
|
|
toolCallId: "bg-job-2",
|
|
name: "run_command",
|
|
title: "Cancelable job",
|
|
status: "in_progress",
|
|
},
|
|
},
|
|
});
|
|
}
|
|
if (holdNextPrompt) {
|
|
const hook = holdNextPrompt;
|
|
holdNextPrompt = null;
|
|
hook(() =>
|
|
send({ jsonrpc: "2.0", id: message.id, result: { stopReason: "cancelled" } }),
|
|
);
|
|
return;
|
|
}
|
|
send({ jsonrpc: "2.0", id: message.id, result: { stopReason: "end_turn" } });
|
|
return;
|
|
}
|
|
if (message.method === "session/close") {
|
|
send({ jsonrpc: "2.0", id: message.id, result: {} });
|
|
}
|
|
},
|
|
});
|
|
return { readable, writable };
|
|
},
|
|
});
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([registration]);
|
|
const manager = new AgentManager({ clients: plugins.clients(), registry: storage, logger });
|
|
try {
|
|
const agent = await manager.createAgent({ provider: registration.id, cwd: home }, undefined, {
|
|
workspaceId: undefined,
|
|
});
|
|
const first = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "start background job",
|
|
logger,
|
|
});
|
|
expect(first.disposition).toBe("turn_started");
|
|
if (first.disposition === "turn_started") await first.started;
|
|
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
expect(manager.getAgent(agent.id)?.lifecycle).toBe("running");
|
|
|
|
// Follow-up steer through AgentManager preserves the background job without session/cancel.
|
|
const followUp = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "still working?",
|
|
activeTurnBehavior: "steer",
|
|
logger,
|
|
});
|
|
expect(followUp.disposition).toBe("steered");
|
|
expect(cancelCount).toBe(0);
|
|
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
expect(manager.getAgent(agent.id)?.lifecycle).toBe("running");
|
|
|
|
// Completing the job + reply + usage_update settles the turn cleanly.
|
|
notifyAgent!({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: { sessionUpdate: "tool_call_update", toolCallId: "bg-job-1", status: "completed" },
|
|
},
|
|
});
|
|
notifyAgent!({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: {
|
|
sessionUpdate: "agent_message_chunk",
|
|
content: { type: "text", text: "Background job finished." },
|
|
},
|
|
},
|
|
});
|
|
notifyAgent!({
|
|
jsonrpc: "2.0",
|
|
method: "session/update",
|
|
params: {
|
|
sessionId: "acp-s1",
|
|
update: { sessionUpdate: "usage_update", used: 120, size: 1000 },
|
|
},
|
|
});
|
|
await expect.poll(() => manager.getAgent(agent.id)?.lifecycle, { timeout: 3_000 }).toBe("idle");
|
|
expect(cancelCount).toBe(0);
|
|
|
|
// Start a second deferred job, send two steers while steer 1 waits on ACP, and press Stop.
|
|
let heldSteerResponse: (() => void) | null = null;
|
|
const second = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "start cancelable job",
|
|
logger,
|
|
});
|
|
if (second.disposition === "turn_started") await second.started;
|
|
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
expect(manager.getAgent(agent.id)?.lifecycle).toBe("running");
|
|
|
|
// Hold the next steer prompt so Stop races with a queued second steer.
|
|
holdNextPrompt = (release) => {
|
|
heldSteerResponse = release;
|
|
};
|
|
const steerOne = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "held steer 1",
|
|
activeTurnBehavior: "steer",
|
|
logger,
|
|
});
|
|
expect(steerOne.disposition).toBe("steered");
|
|
const steerTwo = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "queued steer 2",
|
|
activeTurnBehavior: "steer",
|
|
logger,
|
|
});
|
|
expect(steerTwo.disposition).toBe("steered");
|
|
await expect.poll(() => heldSteerResponse).not.toBeNull();
|
|
const promptsBeforeStop = promptCount;
|
|
|
|
// Stop must send session/cancel immediately, retire the deferred session cleanly,
|
|
// and never dispatch the queued steer 2 to ACP.
|
|
await manager.cancelAgentRun(agent.id);
|
|
expect(cancelCount).toBe(1);
|
|
await expect.poll(() => manager.getAgent(agent.id)?.lifecycle, { timeout: 3_000 }).toBe("idle");
|
|
expect(promptCount).toBe(promptsBeforeStop);
|
|
|
|
// Changing mode immediately after stopping a deferred turn must transparently reopen the session.
|
|
await manager.setAgentMode(agent.id, "plan");
|
|
expect(manager.getAgent(agent.id)?.currentModeId).toBe("plan");
|
|
expect(currentMode).toBe("plan");
|
|
} finally {
|
|
await Promise.all(manager.listAgents().map((item) => manager.closeAgent(item.id)));
|
|
await plugins.shutdown();
|
|
await storage.flush();
|
|
await rm(home, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("reopens cleanly for mode changes and follow-up sends initiated while Stop is closing a slow ACP process", async () => {
|
|
const logger = createTestLogger();
|
|
const home = await mkdtemp(join(tmpdir(), "paseo-acp-slow-stop-"));
|
|
const storage = new AgentStorage(join(home, "agents"), logger);
|
|
const script = join(home, "slow-stop-agent.cjs");
|
|
const { writeFile } = await import("node:fs/promises");
|
|
await writeFile(
|
|
script,
|
|
`
|
|
const { createInterface } = require("node:readline");
|
|
const send = (msg) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...msg }) + "\\n");
|
|
let mode = "default";
|
|
const configOptions = () => [{
|
|
id: "mode", name: "Mode", category: "mode", type: "select", currentValue: mode,
|
|
options: [{ value: "default", name: "Default" }, { value: "plan", name: "Plan" }],
|
|
}];
|
|
process.on("SIGTERM", () => {
|
|
setTimeout(() => process.exit(0), 250);
|
|
});
|
|
createInterface({ input: process.stdin }).on("line", (line) => {
|
|
const msg = JSON.parse(line);
|
|
if (msg.method === "initialize") {
|
|
send({ id: msg.id, result: { protocolVersion: 1, agentCapabilities: { loadSession: true } } });
|
|
} else if (msg.method === "session/new") {
|
|
send({ id: msg.id, result: { sessionId: "slow-s1", configOptions: configOptions() } });
|
|
} else if (msg.method === "session/load") {
|
|
send({ id: msg.id, result: { configOptions: configOptions() } });
|
|
} else if (msg.method === "session/set_config_option") {
|
|
if (msg.params && typeof msg.params.value === "string") mode = msg.params.value;
|
|
send({ id: msg.id, result: { configOptions: configOptions() } });
|
|
} else if (msg.method === "session/prompt") {
|
|
const text = msg.params?.prompt?.[0]?.text ?? "";
|
|
if (text.includes("start bg")) {
|
|
send({ method: "session/update", params: { sessionId: "slow-s1", update: {
|
|
sessionUpdate: "tool_call", toolCallId: "slow-bg-1", name: "run_command",
|
|
title: "Slow bg job", status: "in_progress",
|
|
} } });
|
|
} else {
|
|
send({ method: "session/update", params: { sessionId: "slow-s1", update: {
|
|
sessionUpdate: "agent_message_chunk", content: { type: "text", text: "After stop reply" },
|
|
} } });
|
|
}
|
|
send({ id: msg.id, result: { stopReason: "end_turn" } });
|
|
} else if (msg.method === "session/close") {
|
|
send({ id: msg.id, result: {} });
|
|
}
|
|
});
|
|
`,
|
|
);
|
|
const registration = runAcpProvider({
|
|
id: "slow-stop-acp",
|
|
label: "Slow Stop ACP",
|
|
command: [process.execPath, script],
|
|
acpOptions: {
|
|
deferTurnOnRunningTools: { settleDelayMs: 25, quietLimitMs: 1_000 },
|
|
},
|
|
});
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([registration]);
|
|
const manager = new AgentManager({ clients: plugins.clients(), registry: storage, logger });
|
|
try {
|
|
const agent = await manager.createAgent({ provider: registration.id, cwd: home }, undefined, {
|
|
workspaceId: undefined,
|
|
});
|
|
const first = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "start bg",
|
|
logger,
|
|
});
|
|
if (first.disposition === "turn_started") await first.started;
|
|
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
expect(manager.getAgent(agent.id)?.lifecycle).toBe("running");
|
|
|
|
// Start Stop (takes 250ms to terminate the child on SIGTERM) and trigger a mode change mid-shutdown.
|
|
const stopPromise = manager.cancelAgentRun(agent.id);
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
const modePromise = manager.setAgentMode(agent.id, "plan");
|
|
await Promise.all([stopPromise, modePromise]);
|
|
expect(manager.getAgent(agent.id)?.currentModeId).toBe("plan");
|
|
expect(manager.getAgent(agent.id)?.lifecycle).toBe("idle");
|
|
|
|
// A follow-up prompt immediately after Stop + mode change succeeds cleanly.
|
|
const next = await sendPreparedPromptToAgent({
|
|
agentManager: manager,
|
|
agentStorage: storage,
|
|
agentId: agent.id,
|
|
prompt: "fresh prompt",
|
|
logger,
|
|
});
|
|
expect(next.disposition).toBe("turn_started");
|
|
if (next.disposition === "turn_started") await next.started;
|
|
await expect.poll(() => manager.getAgent(agent.id)?.lifecycle, { timeout: 3_000 }).toBe("idle");
|
|
} finally {
|
|
await Promise.all(manager.listAgents().map((item) => manager.closeAgent(item.id)));
|
|
await plugins.shutdown();
|
|
await storage.flush();
|
|
await rm(home, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("does not block mode changes after a plugin refuses Stop by ignoring session.interrupt", async () => {
|
|
const logger = createTestLogger();
|
|
let currentMode = "code";
|
|
const harness = createProviderHarness({
|
|
async handleInput(input, emit) {
|
|
if (input.type === "session.open") {
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: ["prompt.message", "session.interrupt", "session.configure"],
|
|
restoration: "core",
|
|
title: input.config.title,
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
mode: currentMode,
|
|
modes: [
|
|
{ id: "code", label: "Code" },
|
|
{ id: "plan", label: "Plan" },
|
|
],
|
|
models: [],
|
|
thinkingOptions: [],
|
|
},
|
|
});
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.prompt") {
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "turn", turnId: "hung-turn" },
|
|
});
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: input.sessionId,
|
|
turnId: "hung-turn",
|
|
state: "started",
|
|
});
|
|
return true;
|
|
}
|
|
if (input.type === "session.interrupt") {
|
|
// Intentionally never acknowledge session.interrupt.
|
|
return true;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
if (input.changes.mode) currentMode = input.changes.mode;
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
mode: currentMode,
|
|
modes: [
|
|
{ id: "code", label: "Code" },
|
|
{ id: "plan", label: "Plan" },
|
|
],
|
|
models: [],
|
|
thinkingOptions: [],
|
|
},
|
|
});
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([harness.registration]);
|
|
const registry = buildProviderRegistry(logger, { pluginProviders: plugins.definitions() });
|
|
const manager = new AgentManager({
|
|
logger,
|
|
clients: { "plugin-direct": registry["plugin-direct"].createClient(logger) },
|
|
providerDefinitions: { "plugin-direct": registry["plugin-direct"] },
|
|
rescueTimeouts: { interruptSessionMs: 50 },
|
|
});
|
|
try {
|
|
const agent = await manager.createAgent(
|
|
{ provider: "plugin-direct", cwd: tmpdir() },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
const stream = manager.streamAgent(agent.id, "start");
|
|
void stream.next();
|
|
await expect
|
|
.poll(() => manager.getAgent(agent.id)?.lifecycle, { timeout: 2_000 })
|
|
.toBe("running");
|
|
|
|
const cancelResult = await manager.cancelAgentRun(agent.id);
|
|
expect(cancelResult.status).toBe("refused");
|
|
|
|
// Settings change must not hang behind the unacknowledged session.interrupt.
|
|
await manager.setAgentMode(agent.id, "plan");
|
|
expect(manager.getAgent(agent.id)?.currentModeId).toBe("plan");
|
|
} finally {
|
|
await Promise.all(manager.listAgents().map((item) => manager.closeAgent(item.id)));
|
|
await plugins.shutdown();
|
|
}
|
|
});
|
|
|
|
test("bounds settings wait when Stop is queued behind an unanswered steer", async () => {
|
|
const logger = createTestLogger();
|
|
let currentMode = "code";
|
|
let releaseSteer: (() => void) | null = null;
|
|
const harness = createProviderHarness({
|
|
capabilities: ["prompt.message", "prompt.steer", "session.interrupt", "session.configure"],
|
|
async handleInput(input, emit) {
|
|
if (input.type === "session.open") {
|
|
emit({
|
|
type: "session.opened",
|
|
requestId: input.requestId,
|
|
sessionId: input.sessionId,
|
|
capabilities: [
|
|
"prompt.message",
|
|
"prompt.steer",
|
|
"session.interrupt",
|
|
"session.configure",
|
|
],
|
|
restoration: "core",
|
|
title: input.config.title,
|
|
cwd: input.config.cwd,
|
|
});
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
mode: currentMode,
|
|
modes: [
|
|
{ id: "code", label: "Code" },
|
|
{ id: "plan", label: "Plan" },
|
|
],
|
|
models: [],
|
|
thinkingOptions: [],
|
|
},
|
|
});
|
|
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
|
|
return true;
|
|
}
|
|
if (input.type === "session.prompt") {
|
|
if (input.prompt.delivery === "steer") {
|
|
releaseSteer = () =>
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "steer", turnId: "steer-turn" },
|
|
});
|
|
return true;
|
|
}
|
|
emit({
|
|
type: "session.prompt_result",
|
|
sessionId: input.sessionId,
|
|
clientMessageId: input.prompt.clientMessageId,
|
|
result: { type: "turn", turnId: "steer-turn" },
|
|
});
|
|
emit({
|
|
type: "session.turn",
|
|
sessionId: input.sessionId,
|
|
turnId: "steer-turn",
|
|
state: "started",
|
|
});
|
|
return true;
|
|
}
|
|
if (input.type === "session.interrupt") {
|
|
return true;
|
|
}
|
|
if (input.type === "session.configure") {
|
|
if (input.changes.mode) currentMode = input.changes.mode;
|
|
emit({
|
|
type: "session.config",
|
|
sessionId: input.sessionId,
|
|
config: {
|
|
mode: currentMode,
|
|
modes: [
|
|
{ id: "code", label: "Code" },
|
|
{ id: "plan", label: "Plan" },
|
|
],
|
|
models: [],
|
|
thinkingOptions: [],
|
|
},
|
|
});
|
|
emit({ type: "request.completed", requestId: input.requestId });
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
const plugins = new PluginAgentClientRegistry(logger);
|
|
plugins.replace([harness.registration]);
|
|
const registry = buildProviderRegistry(logger, { pluginProviders: plugins.definitions() });
|
|
const manager = new AgentManager({
|
|
logger,
|
|
clients: { "plugin-direct": registry["plugin-direct"].createClient(logger) },
|
|
providerDefinitions: { "plugin-direct": registry["plugin-direct"] },
|
|
rescueTimeouts: { interruptSessionMs: 50 },
|
|
});
|
|
try {
|
|
const agent = await manager.createAgent(
|
|
{ provider: "plugin-direct", cwd: tmpdir() },
|
|
undefined,
|
|
{ workspaceId: undefined },
|
|
);
|
|
const stream = manager.streamAgent(agent.id, "start");
|
|
void stream.next();
|
|
await expect
|
|
.poll(() => manager.getAgent(agent.id)?.lifecycle, { timeout: 2_000 })
|
|
.toBe("running");
|
|
|
|
// Start an unanswered steer, queue Stop behind it, then change mode.
|
|
const steerPromise = manager.steerAgentRun(agent.id, "unanswered steer").catch(() => undefined);
|
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
const stopPromise = manager.cancelAgentRun(agent.id);
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
|
|
// Mode change must complete within the bounded interrupt timeout even while Stop is queued.
|
|
await manager.setAgentMode(agent.id, "plan");
|
|
expect(manager.getAgent(agent.id)?.currentModeId).toBe("plan");
|
|
|
|
releaseSteer?.();
|
|
await Promise.all([steerPromise, stopPromise]);
|
|
} finally {
|
|
releaseSteer?.();
|
|
await Promise.all(manager.listAgents().map((item) => manager.closeAgent(item.id)));
|
|
await plugins.shutdown();
|
|
}
|
|
});
|