diff --git a/packages/core/test/subagent-lifetime.test.ts b/packages/core/test/subagent-lifetime.test.ts index 996018e082c..cac92a8a9a9 100644 --- a/packages/core/test/subagent-lifetime.test.ts +++ b/packages/core/test/subagent-lifetime.test.ts @@ -15,6 +15,8 @@ import { Provider } from "@opencode/core/provider" import { AbsolutePath } from "@opencode/core/schema" import { Session } from "@opencode/core/session" import { SessionRunnerModel } from "@opencode/core/session/runner/model" +import { SessionSchema } from "@opencode/core/session/schema" +import { SubagentJob } from "@opencode/core/session/subagent-job" import { Tool } from "@opencode/core/tool" import { LayerNode } from "@opencode/util/effect/layer-node" import { Global } from "@opencode/util/global" @@ -28,7 +30,7 @@ const llmLayer = TestLLM.testLayer({ fallback: TestLLM.text("Review complete", " const it = testEffect( Layer.merge( llmLayer, - AppNodeBuilder.build(LayerNode.group([Session.node, Job.node, LocationServiceMap.node]), [ + AppNodeBuilder.build(LayerNode.group([Session.node, Job.node, SubagentJob.node, LocationServiceMap.node]), [ Global.node.replace(tempGlobalLayer), offlineModels, Watcher.node.replace(Watcher.configured({ enabled: false })), @@ -52,6 +54,82 @@ const it = testEffect( ), ) +/** A parent with a reviewer child in one directory, plus the parent's tool registry. */ +const family = Effect.fnUntraced(function* (descendantMetadata?: SessionSchema.Metadata) { + const dir = yield* tmpdirScoped() + yield* Effect.promise(() => + Bun.write( + `${dir.path}/opencode.json`, + JSON.stringify({ + agents: { reviewer: { mode: "subagent" } }, + permissions: [{ action: "*", resource: "*", effect: "allow" }], + }), + ), + ) + const sessions = yield* Session.Service + const locations = yield* LocationServiceMap.Service + const parent = yield* sessions.create({ + location: { directory: AbsolutePath.make(dir.path) }, + agent: Agent.ID.make("build"), + model: Model.Ref.make({ id: Model.ID.make("parent"), providerID: Provider.ID.make("test") }), + }) + const child = yield* sessions.create({ parentID: parent.id, agent: Agent.ID.make("reviewer") }) + const descendant = yield* sessions.create({ + parentID: child.id, + agent: Agent.ID.make("reviewer"), + ...(descendantMetadata === undefined ? {} : { metadata: descendantMetadata }), + }) + yield* Plugin.Service.use((plugins) => plugins.awaitActivation).pipe(Effect.provide(locations.get(parent.location))) + const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location))) + const spawn = executeTool(registry, { + sessionID: parent.id, + ...toolIdentity, + call: { + type: "tool-call", + id: "call-review", + name: "subagent", + input: { + agent: "reviewer", + sessionID: child.id, + description: "Review fixture", + prompt: "Review the fixture", + background: true, + }, + }, + }) + return { sessions, parent, child, descendant, spawn } +}) + +/** Mirrors the shell tool: a backgrounded shell job whose exit notice is admitted to its owner. */ +const backgroundShell = Effect.fnUntraced(function* (owner: SessionSchema.ID) { + const jobs = yield* Job.Service + const sessions = yield* Session.Service + const exit = yield* Deferred.make() + yield* jobs.start({ + id: "shell-dev", + type: "shell", + title: "dev server", + recovery: { kind: "shell", sessionID: owner, shellID: "shell-dev", command: "bun dev" }, + run: Deferred.await(exit).pipe(Effect.as("exited")), + }) + yield* jobs.background("shell-dev") + return Effect.gen(function* () { + yield* Deferred.succeed(exit, undefined) + const shell = (yield* jobs.wait({ id: "shell-dev" })).info + yield* sessions.synthetic({ + ...(shell?.notificationID ? { id: shell.notificationID } : {}), + sessionID: owner, + text: "Background shell exited", + }) + if (shell?.notificationID) yield* jobs.completeBackground(shell.notificationID) + }) +}) + +const reports = Effect.fnUntraced(function* (parentID: SessionSchema.ID) { + const sessions = yield* Session.Service + return (yield* sessions.context(parentID)).filter((message) => message.type === "synthetic") +}) + describe("subagent observer lifetime", () => { for (const [evict, outcome] of [ [false, "completed"], @@ -260,4 +338,107 @@ describe("subagent observer lifetime", () => { }), ) } + + it.live("a directly prompted child reports once, only after its background shell exits", () => + Effect.gen(function* () { + const jobs = yield* Job.Service + const llm = yield* TestLLM.Test + const f = yield* family() + const exitShell = yield* backgroundShell(f.child.id) + yield* llm.push(TestLLM.text("first", "first"), TestLLM.text("final", "final")) + + yield* f.sessions.prompt({ sessionID: f.child.id, text: "Start the dev server" }) + yield* f.sessions.wait(f.child.id) + expect(yield* reports(f.parent.id)).toEqual([]) + expect((yield* jobs.get("shell-dev"))?.status).toBe("running") + + yield* exitShell + yield* llm.wait(3).pipe(Effect.timeout("2 seconds")) + yield* f.sessions.wait(f.child.id) + yield* f.sessions.wait(f.parent.id) + const notices = yield* reports(f.parent.id) + expect(notices).toHaveLength(1) + expect(notices[0]).toMatchObject({ metadata: { source: "subagent", childID: f.child.id, state: "completed" } }) + expect(notices[0]?.text).toContain("final") + expect(notices[0]?.text).not.toContain("first") + expect(yield* jobs.pendingBackground).toEqual([]) + expect(yield* llm.requests()).toHaveLength(3) + }), + ) + + it.live("a silent background grandchild job releases its parent's held report", () => + Effect.gen(function* () { + const jobs = yield* Job.Service + const subagents = yield* SubagentJob.Service + const llm = yield* TestLLM.Test + const f = yield* family() + const go = yield* Deferred.make() + yield* llm.push( + Stream.unwrap( + Deferred.await(go).pipe(Effect.as(Stream.fromIterable(TestLLM.text("grandchild done", "grandchild")))), + ), + TestLLM.text("first", "first"), + ) + const input = { + recovery: { + kind: "subagent" as const, + parentSessionID: f.child.id, + childSessionID: f.descendant.id, + agent: "reviewer", + description: "Check the tests", + }, + notify: false, + } + yield* f.sessions.prompt({ sessionID: f.descendant.id, text: "Check the tests", resume: false }) + yield* subagents.start(input) + yield* subagents.background(input) + yield* llm.wait(1).pipe(Effect.timeout("2 seconds")) + + expect((yield* f.spawn).status).toBe("completed") + yield* llm.wait(2).pipe(Effect.timeout("2 seconds")) + yield* f.sessions.wait(f.child.id) + expect(yield* reports(f.parent.id)).toEqual([]) + expect((yield* jobs.get(f.child.id))?.status).toBe("running") + + yield* Deferred.succeed(go, undefined) + yield* jobs.wait({ id: f.child.id }).pipe(Effect.timeout("5 seconds")) + yield* llm.wait(3).pipe(Effect.timeout("2 seconds")) + yield* f.sessions.wait(f.parent.id) + const notices = yield* reports(f.parent.id) + expect(notices).toHaveLength(1) + expect(notices[0]).toMatchObject({ metadata: { source: "subagent", childID: f.child.id, state: "completed" } }) + expect(notices[0]?.text).toContain("first") + expect(notices[0]?.text).not.toContain("grandchild done") + expect(yield* jobs.pendingBackground).toEqual([]) + expect(yield* llm.requests()).toHaveLength(3) + }), + ) + + it.live("a silent descendant woken by its shell releases the child's held report", () => + Effect.gen(function* () { + const jobs = yield* Job.Service + const llm = yield* TestLLM.Test + const f = yield* family({ "subagent.notify": false }) + const exitShell = yield* backgroundShell(f.descendant.id) + yield* llm.push(TestLLM.text("first", "first"), TestLLM.text("descendant done", "descendant")) + + expect((yield* f.spawn).status).toBe("completed") + yield* llm.wait(1).pipe(Effect.timeout("2 seconds")) + yield* f.sessions.wait(f.child.id) + expect(yield* reports(f.parent.id)).toEqual([]) + expect((yield* jobs.get(f.child.id))?.status).toBe("running") + + yield* exitShell + yield* jobs.wait({ id: f.child.id }).pipe(Effect.timeout("5 seconds")) + yield* llm.wait(3).pipe(Effect.timeout("2 seconds")) + yield* f.sessions.wait(f.parent.id) + const notices = yield* reports(f.parent.id) + expect(notices).toHaveLength(1) + expect(notices[0]).toMatchObject({ metadata: { source: "subagent", childID: f.child.id, state: "completed" } }) + expect(notices[0]?.text).toContain("first") + expect(notices[0]?.text).not.toContain("descendant done") + expect(yield* jobs.pendingBackground).toEqual([]) + expect(yield* llm.requests()).toHaveLength(3) + }), + ) })