106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import type { AgentConfig } from "../src/agents.ts";
|
|
import type { RunWaitResult, SubagentManager, WaitResult } from "../src/manager.ts";
|
|
import type { JobSnapshot, PreparedInvocation, RunResult, TaskRunner, RunOptions } from "../src/types.ts";
|
|
|
|
export const agent: AgentConfig = {
|
|
name: "scout",
|
|
description: "Test agent",
|
|
systemPrompt: "Test prompt",
|
|
source: "user",
|
|
filePath: "/tmp/scout.md",
|
|
};
|
|
|
|
export function invocation(mode: PreparedInvocation["mode"] = "single"): PreparedInvocation {
|
|
const count = mode === "single" ? 1 : 2;
|
|
return {
|
|
mode,
|
|
agentScope: "user",
|
|
projectAgentsDir: null,
|
|
dispatchDefaults: {},
|
|
tasks: Array.from({ length: count }, (_, index) => ({
|
|
agent,
|
|
task: `task ${index + 1}`,
|
|
cwd: "/tmp",
|
|
step: mode === "chain" ? index + 1 : undefined,
|
|
})),
|
|
};
|
|
}
|
|
|
|
export function result(task: { agent: AgentConfig; task: string; cwd: string; step?: number }, output = "done"): RunResult {
|
|
return {
|
|
agent: task.agent.name,
|
|
agentSource: task.agent.source,
|
|
task: task.task,
|
|
cwd: task.cwd,
|
|
step: task.step,
|
|
exitCode: 0,
|
|
messages: [],
|
|
output,
|
|
stderr: "",
|
|
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
|
};
|
|
}
|
|
|
|
interface PendingRun {
|
|
task: PreparedInvocation["tasks"][number];
|
|
options: RunOptions;
|
|
resolve?: (result: RunResult) => void;
|
|
settled?: boolean;
|
|
}
|
|
|
|
export class FakeRunner implements TaskRunner {
|
|
readonly calls: PendingRun[] = [];
|
|
readonly aborted: string[] = [];
|
|
private readonly resolveOnAbort: boolean;
|
|
|
|
constructor(resolveOnAbort = true) {
|
|
this.resolveOnAbort = resolveOnAbort;
|
|
}
|
|
|
|
async run(task: PreparedInvocation["tasks"][number], _defaults: PreparedInvocation["dispatchDefaults"], options: RunOptions): Promise<RunResult> {
|
|
const pending: PendingRun = { task, options };
|
|
this.calls.push(pending);
|
|
return new Promise<RunResult>((resolve) => {
|
|
options.signal.addEventListener("abort", () => {
|
|
this.aborted.push(task.task);
|
|
if (!this.resolveOnAbort) return;
|
|
pending.settled = true;
|
|
resolve({
|
|
...result(task),
|
|
exitCode: null,
|
|
stopReason: "aborted",
|
|
errorMessage: "aborted",
|
|
terminationSignal: "SIGTERM",
|
|
});
|
|
}, { once: true });
|
|
pending.resolve = resolve;
|
|
});
|
|
}
|
|
|
|
complete(task: string, output = "done", overrides: Partial<RunResult> = {}): void {
|
|
const pending = this.calls.find((call) => call.task.task === task && !call.settled);
|
|
if (!pending?.resolve) throw new Error(`No pending run for ${task}`);
|
|
pending.settled = true;
|
|
pending.resolve({ ...result(pending.task, output), ...overrides });
|
|
}
|
|
}
|
|
|
|
/** True when a wait resolved with a run's bounded summary instead of a job snapshot. */
|
|
export function isRunWait(result: WaitResult): result is RunWaitResult {
|
|
return result.kind === "run";
|
|
}
|
|
|
|
/**
|
|
* Wait for a job, then read the full snapshot: for a run, `wait` returns the
|
|
* bounded summary, so tests that inspect every row use the explicit `get` path.
|
|
*/
|
|
export async function waitForSnapshot(manager: SubagentManager, jobId: string, timeoutSeconds = 1): Promise<JobSnapshot> {
|
|
const result = await manager.wait(jobId, timeoutSeconds);
|
|
if (isRunWait(result)) {
|
|
const snapshot = manager.get(jobId);
|
|
return result.timedOut ? { ...snapshot, timedOut: true } : snapshot;
|
|
}
|
|
const { kind: _kind, ...snapshot } = result;
|
|
return snapshot;
|
|
}
|