import { expect, test } from "bun:test"; import { Effect } from "effect"; import { Agent } from "@opencode/schema/agent"; import { Session } from "@opencode/schema/session"; import { SessionMessage } from "@opencode/schema/session-message"; import { Tool } from "@opencode/schema/tool"; import type { Context } from "@opencode/plugin/effect/plugin"; import type { ToolEditor, ToolHooks } from "@opencode/plugin/effect/tool"; import type { AgentEditor } from "@opencode/plugin/effect/agent"; import type { SessionContext } from "@opencode/plugin/effect/session"; import { z } from "zod"; import { Herdr, type HerdrRoot } from "../src/multi-work/herdr.js"; import { bindingKey, bindingSchema, installMultiWork } from "../src/multi-work/multi-work.js"; class FixtureHerdr extends Herdr { eligible = true; rootWorkspaceID = "w1"; rootRepo = "/root/.git"; creations = 0; verified = 0; delayCreation = false; creationAborted = false; createdBase: string | undefined; createdBranch: string | undefined; createdLabel: string | undefined; creationStarted = Promise.withResolvers(); override async root(directory: string) { return this.eligible && directory === "/root" ? { directory, workspaceID: this.rootWorkspaceID, repo: this.rootRepo } : undefined; } override async create( _root: HerdrRoot, base: string, branch: string, signal: AbortSignal, label?: string, ) { this.creations++; this.createdBase = base; this.createdBranch = branch; this.createdLabel = label; if (!this.eligible) throw new Error("root closed"); if (this.delayCreation) { this.creationStarted.resolve(); await new Promise((_resolve, reject) => { signal.addEventListener( "abort", () => { this.creationAborted = true; reject(new Error("creation interrupted")); }, { once: true }, ); }); } return { directory: "/worktree", workspaceID: "w2", branch }; } override async verify() { this.verified++; } } interface FixtureSession { id: Session.ID; projectID: string; agent: string; parentID: string | undefined; location: { directory: string }; permissions: Agent.Info["permissions"]; } function fixture() { const herdr = new FixtureHerdr(); const storage = new Map[1]>(); const agents = new Map>>(); for (const name of ["coordinate", "fast-lo", "fast-hi", "good-lo", "good-hi"]) agents.set(name, { ...Agent.Info.default(Agent.ID.make(name)), permissions: [] }); const parent: FixtureSession = { id: Session.ID.make("ses_parent"), projectID: "project-fixture", agent: "multi-work", parentID: undefined, location: { directory: "/root" }, permissions: [], }; const sessions = new Map([[String(parent.id), parent]]); const order: string[] = []; let cancelled = false; let hold = false; let delayMove = false; let crossProject = false; let destinationAvailable = true; let warming = false; let agentReads = 0; let prompts = 0; let serial = 0; const entered = Promise.withResolvers(); const progress: Tool.Metadata[] = []; const native: Tool.Info = { name: "subagent", description: "native", input: z.object({}), options: { codemode: false }, execute: (raw, call) => Effect.gen(function* () { const input = z .object({ agent: z.string(), sessionID: z.string().optional(), background: z.boolean().optional(), }) .parse(raw); const id = Session.ID.make(input.sessionID ?? `ses_child${++serial}`); if (!input.sessionID) sessions.set(id, { ...parent, id, parentID: call.sessionID, agent: input.agent, location: { directory: "/root" }, }); order.push("create"); yield* call.progress({ sessionID: id, status: "running" }); order.push(`prompt:${sessions.get(id)!.location.directory}`); prompts++; entered.resolve(); if (hold) yield* Effect.never.pipe( Effect.onInterrupt(() => Effect.sync(() => { cancelled = true; }), ), ); return { content: "native result", metadata: { sessionID: id, status: input.background ? "running" : "completed" }, }; }), }; let tool = native; const toolHooks: ((event: ToolHooks["execute.before"]) => Effect.Effect)[] = []; const hooks: ((event: SessionContext) => Effect.Effect)[] = []; let prompt!: (event: Pick) => Effect.Effect; let transform!: (editor: AgentEditor) => void; const registration = Effect.succeed({ dispose: Effect.void }); const editAgents = (): AgentEditor => ({ get: (id) => agents.get(id), list: () => [...agents.values()], default() {}, remove(id) { agents.delete(id); }, update(id, update) { // SAFETY: Agent.Info.default returns a fresh draft that the fixture exclusively owns. const agent = (agents.get(id) ?? Agent.Info.default(Agent.ID.make(id))) as NonNullable< ReturnType >; update(agent); agents.set(id, agent); }, }); const editTools: ToolEditor = { list: () => [{ ...tool, id: tool.name }], get: () => ({ ...tool, id: tool.name }), add() {}, namespace() {}, remove() {}, update(_id, update) { const draft = { ...tool }; update(draft); tool = draft; }, }; const host = { options: {}, location: { directory: "/root" }, storage: { get: (key: string) => Effect.sync(() => storage.get(key)), set: (key: string, value: Parameters[1]) => Effect.sync(() => { storage.set(key, value); }), }, agent: { transform: (callback: typeof transform) => Effect.sync(() => { transform = callback; callback(editAgents()); }).pipe(Effect.andThen(registration)), reload: () => Effect.sync(() => { if (transform) transform(editAgents()); }), get: () => Effect.succeed({ data: agents.get("multi-work")! }), list: () => Effect.sync(() => { agentReads++; if (!destinationAvailable) throw new Error("Destination location unavailable"); return { data: warming && agentReads === 1 ? [] : [...agents.values()] }; }), }, session: { get: ({ sessionID }: { sessionID: string }) => Effect.sync(() => sessions.get(sessionID)!), move: ({ sessionID, directory }: { sessionID: string; directory: string }) => Effect.sync(() => { order.push("move-request"); if (!delayMove) { sessions.get(sessionID)!.location.directory = directory; order.push("moved"); if (crossProject) sessions.get(sessionID)!.projectID = "another-project"; } entered.resolve(); }), hook: (name: string, callback: (event: SessionContext) => Effect.Effect) => Effect.sync(() => { if (name === "context") hooks.push(callback); if (name === "prompt") { // SAFETY: The prompt admission hook reads only the native session ID. prompt = callback as typeof prompt; } }).pipe(Effect.andThen(registration)), }, tool: { transform: (callback: (editor: ToolEditor) => void) => Effect.sync(() => callback(editTools)).pipe(Effect.andThen(registration)), hook: ( _name: string, callback: (event: ToolHooks["execute.before"]) => Effect.Effect, ) => Effect.sync(() => { toolHooks.push(callback); }).pipe(Effect.andThen(registration)), }, }; // SAFETY: The test setup only receives its exercised services; unexpected host dependencies fail loudly. const install = installMultiWork as ( context: Pick, buildPermissions: () => Agent.Info["permissions"], herdr: Herdr, ) => ReturnType; const call: Tool.Context = { sessionID: parent.id, agent: Agent.ID.make("multi-work"), messageID: SessionMessage.ID.make("msg_test"), id: Tool.CallID.make("call_test"), progress: (value) => Effect.sync(() => { progress.push(value); }), }; return { herdr, agents, sessions, parent, storage, order, entered, hooks, prompt: (event: Pick) => prompt(event), toolHooks, call, progress, install: install( host, () => agents.get("build")?.permissions ?? Agent.Info.default(Agent.ID.make("build")).permissions, herdr, ), invoke: ( input = { agent: "coordinate", description: "test", prompt: "work", worktree: { base: "main" }, }, caller = call, ) => tool.execute(input, caller), execute: (input: z.infer, caller = call) => tool.execute(input, caller), hold() { hold = true; }, delayMove() { delayMove = true; }, missingDestination() { destinationAvailable = false; }, crossProject() { crossProject = true; }, warmDestination() { warming = true; }, get agentReads() { return agentReads; }, get cancelled() { return cancelled; }, get prompts() { return prompts; }, get tool() { return tool; }, }; } const request = z.object({ agent: z.string(), description: z.string(), prompt: z.string(), worktree: z .object({ base: z.string(), branch: z.string().optional(), label: z.string().optional(), }) .optional(), sessionID: z.string().optional(), background: z.boolean().optional(), }); test("coordinate can create and resume a root-local background child without Herdr creation or movement", async () => { const f = fixture(); f.parent.permissions = [{ action: "herdr_worktree", resource: "*", effect: "deny" }]; await Effect.runPromise( Effect.scoped( Effect.gen(function* () { yield* f.install; const created = yield* f.execute({ agent: "coordinate", description: "local edit", prompt: "edit assigned files", background: true, }); expect(created.metadata).toMatchObject({ sessionID: "ses_child1", status: "running", directory: "/root", workspaceID: "w1", branch: "root", }); expect(f.herdr.creations).toBe(0); expect(f.order).toEqual(["create", "prompt:/root"]); expect(bindingSchema.parse(f.storage.get(bindingKey("ses_child1")))).toMatchObject({ owner: "ses_parent", directory: "/root", workspaceID: "w1", branch: "root", ready: true, }); yield* f.execute({ agent: "coordinate", description: "resume local", prompt: "continue", sessionID: "ses_child1", }); expect(f.herdr.creations).toBe(0); expect(f.herdr.verified).toBe(0); }), ), ); }); test("root-local resume rejects location, binding, and live-root changes", async () => { for (const tamper of ["session", "workspace", "root", "branch", "live-root"] as const) { const f = fixture(); await Effect.runPromise( Effect.scoped( Effect.gen(function* () { yield* f.install; yield* f.execute({ agent: "coordinate", description: "local edit", prompt: "work", }); if (tamper === "session") f.sessions.get("ses_child1")!.location.directory = "/other"; else if (tamper === "live-root") f.herdr.rootWorkspaceID = "foreign"; else { const binding = bindingSchema.parse( f.storage.get(bindingKey("ses_child1")), ); const changed = { ...binding }; if (tamper === "workspace") changed.workspaceID = "foreign"; if (tamper === "root") { changed.root = { ...binding.root, workspaceID: "foreign" }; changed.workspaceID = "foreign"; } if (tamper === "branch") changed.branch = "main"; f.storage.set(bindingKey("ses_child1"), changed); } expect( (yield* Effect.exit( f.execute({ agent: "coordinate", description: "resume", prompt: "continue", sessionID: "ses_child1", }), ))._tag, ).toBe("Failure"); expect(f.prompts).toBe(1); }), ), ); } }); test("interrupted root-local creation retains its binding and cannot be blindly replayed", async () => { const f = fixture(); f.hold(); const controller = new AbortController(); const running = Effect.runPromise( Effect.scoped( Effect.gen(function* () { yield* f.install; return yield* f.execute({ agent: "coordinate", description: "local edit", prompt: "work", }); }), ), { signal: controller.signal }, ); await f.entered.promise; controller.abort(); await expect(running).rejects.toBeDefined(); expect(bindingSchema.parse(f.storage.get(bindingKey("ses_child1"))).ready).toBe(true); expect(f.storage.has(`multi-work-call:${f.call.sessionID}:${f.call.id}`)).toBe(true); const replay = await Effect.runPromise( Effect.exit( f.execute({ agent: "coordinate", description: "local edit", prompt: "work", }), ), ); expect(replay._tag).toBe("Failure"); expect(f.prompts).toBe(1); }); test("root loss, session permission denial and missing destination fail before prompting", async () => { for (const failure of ["root", "permission", "ask", "destination", "project"] as const) { const f = fixture(); await Effect.runPromise( Effect.scoped( Effect.gen(function* () { yield* f.install; if (failure === "root") f.herdr.eligible = false; if (failure === "permission") f.parent.permissions = [ { action: "herdr_worktree", resource: "*", effect: "deny" }, ]; if (failure === "destination") f.missingDestination(); if (failure === "project") f.crossProject(); if (failure === "ask") f.parent.permissions = [ { action: "herdr_worktree", resource: "*", effect: "ask" }, ]; const result = yield* Effect.exit(f.invoke()); expect(result._tag).toBe("Failure"); expect(f.prompts).toBe(0); expect(f.herdr.creations).toBe( failure === "destination" || failure === "project" ? 1 : 0, ); }), ), ); } });