tool-subagent.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Fiber, Layer, Schema, Stream } from "effect"
  3. import path from "path"
  4. import { Money } from "@opencode-ai/schema/money"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  7. import { Global } from "@opencode-ai/util/global"
  8. import { makeGlobalNode, makeLocationNode } from "@opencode-ai/util/effect/app-node"
  9. import { Database } from "@opencode-ai/core/database/database"
  10. import { Bus } from "@opencode-ai/core/bus"
  11. import { Config } from "@opencode-ai/core/config"
  12. import { Location } from "@opencode-ai/core/location"
  13. import { Model } from "@opencode-ai/core/model"
  14. import { Provider } from "@opencode-ai/core/provider"
  15. import { AbsolutePath } from "@opencode-ai/core/schema"
  16. import { Agent } from "@opencode-ai/core/agent"
  17. import { Job } from "@opencode-ai/core/job"
  18. import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
  19. import { Session } from "@opencode-ai/core/session"
  20. import { SessionEvent } from "@opencode-ai/core/session/event"
  21. import { SessionExecution } from "@opencode-ai/core/session/execution"
  22. import { SessionInbox } from "@opencode-ai/core/session/inbox"
  23. import { SessionMessage } from "@opencode-ai/core/session/message"
  24. import { SessionRunnerModel } from "@opencode-ai/core/session/runner/model"
  25. import { SessionStore } from "@opencode-ai/core/session/store"
  26. import { PluginRuntime } from "@opencode-ai/core/plugin/runtime"
  27. import { PluginSupervisor } from "@opencode-ai/core/plugin/supervisor"
  28. import { Permission } from "@opencode-ai/core/permission"
  29. import { SubagentTool } from "@opencode-ai/core/tool/plugin/subagent"
  30. import { Tool } from "@opencode-ai/core/tool"
  31. import { tmpdir } from "./fixture/tmpdir"
  32. import { tempGlobalLayer } from "./fixture/global"
  33. import { testEffect } from "./lib/effect"
  34. import { executeTool, registerToolPlugin, toolIdentity } from "./lib/tool"
  35. const childText = "child final response"
  36. const childModel = Model.Ref.make({ id: Model.ID.make("child"), providerID: Provider.ID.make("test") })
  37. const parentModel = Model.Ref.make({ id: Model.ID.make("parent"), providerID: Provider.ID.make("test") })
  38. const tokens = { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
  39. const outputSessionID = (value: unknown) =>
  40. Schema.decodeUnknownSync(Schema.Struct({ sessionID: Session.ID }))(value).sessionID
  41. const executionNode = makeGlobalNode({
  42. service: SessionExecution.Service,
  43. layer: Layer.effect(
  44. SessionExecution.Service,
  45. Effect.gen(function* () {
  46. const bus = yield* Bus.Service
  47. const store = yield* SessionStore.Service
  48. const completed = new Set<Session.ID>()
  49. const complete = Effect.fn("SubagentTest.complete")(function* (sessionID: Session.ID) {
  50. if (completed.has(sessionID)) return
  51. if ((yield* store.get(sessionID))?.title?.includes("fail")) {
  52. yield* new SessionRunnerModel.ModelNotSelectedError({ sessionID })
  53. return
  54. }
  55. completed.add(sessionID)
  56. const assistantMessageID = SessionMessage.ID.create()
  57. yield* bus.publish(SessionEvent.Step.Started, {
  58. sessionID,
  59. assistantMessageID,
  60. agent: Agent.ID.make("reviewer"),
  61. model: childModel,
  62. })
  63. yield* bus.publish(SessionEvent.Text.Started, {
  64. sessionID,
  65. assistantMessageID,
  66. ordinal: 0,
  67. })
  68. yield* bus.publish(SessionEvent.Text.Ended, {
  69. sessionID,
  70. assistantMessageID,
  71. ordinal: 0,
  72. text: childText,
  73. })
  74. yield* bus.publish(SessionEvent.Step.Ended, {
  75. sessionID,
  76. assistantMessageID,
  77. finish: "stop",
  78. cost: Money.USD.zero,
  79. tokens,
  80. })
  81. })
  82. return SessionExecution.Service.of({
  83. active: Effect.succeed(new Set()),
  84. resume: complete,
  85. wake: () => Effect.void,
  86. wakeActive: () => Effect.void,
  87. interrupt: () => Effect.void,
  88. awaitIdle: (sessionID) => complete(sessionID).pipe(Effect.exit, Effect.asVoid),
  89. })
  90. }),
  91. ),
  92. deps: [Bus.node, SessionStore.node],
  93. })
  94. const subagentPluginSupervisor = makeLocationNode({
  95. service: PluginSupervisor.Service,
  96. layer: Layer.effect(
  97. PluginSupervisor.Service,
  98. registerToolPlugin(SubagentTool.Plugin).pipe(Effect.as(PluginSupervisor.Service.of({ flush: Effect.void }))),
  99. ),
  100. deps: [Agent.node, Config.node, Permission.node, PluginRuntime.node, Tool.node],
  101. })
  102. const nodes = LayerNode.group([
  103. Database.node,
  104. Bus.node,
  105. Job.node,
  106. Session.node,
  107. SessionExecution.node,
  108. PluginRuntime.providerNode,
  109. LocationServiceMap.node,
  110. ])
  111. const replacements = [
  112. [SessionExecution.node, executionNode],
  113. [Global.node, tempGlobalLayer],
  114. ] satisfies LayerNode.Replacements
  115. const productionIt = testEffect(AppNodeBuilder.build(nodes, replacements))
  116. const it = testEffect(AppNodeBuilder.build(nodes, [...replacements, [PluginSupervisor.node, subagentPluginSupervisor]]))
  117. const withSubagent = (location: Location.Ref) =>
  118. Effect.gen(function* () {
  119. const locations = yield* LocationServiceMap.Service
  120. yield* PluginSupervisor.Service.use((supervisor) => supervisor.flush).pipe(Effect.provide(locations.get(location)))
  121. yield* Agent.Service.use((agents) =>
  122. agents.transform((draft) => {
  123. // The caller identity used by executeTool; subagent permission asserts against it.
  124. draft.update(toolIdentity.agent, (agent) => {
  125. agent.mode = "primary"
  126. agent.permissions.push({ action: "*", resource: "*", effect: "allow" })
  127. })
  128. draft.update(Agent.ID.make("reviewer"), (agent) => {
  129. agent.mode = "subagent"
  130. agent.model = childModel
  131. })
  132. draft.update(Agent.ID.make("fallback"), (agent) => {
  133. agent.mode = "subagent"
  134. })
  135. draft.update(Agent.ID.make("primary"), (agent) => {
  136. agent.mode = "primary"
  137. })
  138. }),
  139. ).pipe(Effect.provide(locations.get(location)))
  140. })
  141. describe("SubagentTool", () => {
  142. productionIt.live("registers globally while resolving agents from the caller location", () =>
  143. Effect.acquireRelease(
  144. Effect.promise(() => tmpdir()),
  145. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  146. ).pipe(
  147. Effect.flatMap((dir) =>
  148. Effect.gen(function* () {
  149. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  150. const session = yield* Session.Service
  151. const parent = yield* session.create({ location })
  152. yield* withSubagent(parent.location)
  153. const locations = yield* LocationServiceMap.Service
  154. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  155. expect((yield* registry.snapshot()).definitions.map((tool) => tool.name)).toContain(SubagentTool.name)
  156. expect(
  157. yield* executeTool(registry, {
  158. sessionID: parent.id,
  159. ...toolIdentity,
  160. call: {
  161. type: "tool-call",
  162. id: "call-primary",
  163. name: SubagentTool.name,
  164. input: { agent: "primary", description: "primary", prompt: "should fail" },
  165. },
  166. }),
  167. ).toEqual({
  168. status: "error",
  169. error: { type: "tool.execution", message: "Agent primary cannot run as a subagent" },
  170. })
  171. }),
  172. ),
  173. ),
  174. )
  175. it.live("prevents subagents from launching subagents by default", () =>
  176. Effect.acquireRelease(
  177. Effect.promise(() => tmpdir()),
  178. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  179. ).pipe(
  180. Effect.flatMap((dir) =>
  181. Effect.gen(function* () {
  182. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  183. const sessions = yield* Session.Service
  184. const root = yield* sessions.create({ location })
  185. const parent = yield* sessions.create({ parentID: root.id, title: "parent" })
  186. yield* withSubagent(parent.location)
  187. const locations = yield* LocationServiceMap.Service
  188. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  189. expect(
  190. yield* executeTool(registry, {
  191. sessionID: parent.id,
  192. ...toolIdentity,
  193. call: {
  194. type: "tool-call",
  195. id: "call-nested-subagent",
  196. name: SubagentTool.name,
  197. input: { agent: "reviewer", description: "nested", prompt: "should fail" },
  198. },
  199. }),
  200. ).toEqual({
  201. status: "error",
  202. error: {
  203. type: "tool.execution",
  204. message: expect.stringContaining("Subagent depth limit reached (1)"),
  205. },
  206. })
  207. expect((yield* sessions.list({ parentID: parent.id })).data).toHaveLength(0)
  208. }),
  209. ),
  210. ),
  211. )
  212. it.live("allows nested subagents up to the configured depth", () =>
  213. Effect.acquireRelease(
  214. Effect.promise(() => tmpdir()),
  215. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  216. ).pipe(
  217. Effect.flatMap((dir) =>
  218. Effect.gen(function* () {
  219. yield* Effect.promise(() =>
  220. Bun.write(path.join(dir.path, "opencode.json"), JSON.stringify({ experimental: { subagent_depth: 2 } })),
  221. )
  222. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  223. const sessions = yield* Session.Service
  224. const root = yield* sessions.create({ location })
  225. const parent = yield* sessions.create({ parentID: root.id, title: "parent", model: parentModel })
  226. yield* withSubagent(parent.location)
  227. const locations = yield* LocationServiceMap.Service
  228. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  229. const settled = yield* executeTool(registry, {
  230. sessionID: parent.id,
  231. ...toolIdentity,
  232. call: {
  233. type: "tool-call",
  234. id: "call-configured-nested-subagent",
  235. name: SubagentTool.name,
  236. input: { agent: "reviewer", description: "nested", prompt: "should run" },
  237. },
  238. })
  239. expect(settled).toMatchObject({
  240. status: "completed",
  241. metadata: { status: "completed" },
  242. content: [{ type: "text", text: childText }],
  243. })
  244. expect(settled.metadata).toEqual({
  245. sessionID: outputSessionID(settled.metadata),
  246. status: "completed",
  247. })
  248. expect((yield* sessions.get(outputSessionID(settled.metadata))).parentID).toBe(parent.id)
  249. }),
  250. ),
  251. ),
  252. )
  253. it.live("runs a foreground child session and returns the final assistant text", () =>
  254. Effect.acquireRelease(
  255. Effect.promise(() => tmpdir()),
  256. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  257. ).pipe(
  258. Effect.flatMap((dir) =>
  259. Effect.gen(function* () {
  260. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  261. const sessions = yield* Session.Service
  262. const parent = yield* sessions.create({ location, model: parentModel })
  263. yield* withSubagent(parent.location)
  264. const locations = yield* LocationServiceMap.Service
  265. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  266. const progress: Tool.Metadata[] = []
  267. const settled = yield* executeTool(registry, {
  268. sessionID: parent.id,
  269. ...toolIdentity,
  270. progress: (update) => Effect.sync(() => progress.push(update)),
  271. call: {
  272. type: "tool-call",
  273. id: "call-subagent",
  274. name: SubagentTool.name,
  275. input: { agent: "reviewer", description: "review", prompt: "review this" },
  276. },
  277. })
  278. expect(settled).toMatchObject({
  279. status: "completed",
  280. metadata: { status: "completed" },
  281. content: [{ type: "text", text: childText }],
  282. })
  283. const child = yield* sessions.get(outputSessionID(settled.metadata))
  284. expect(settled.metadata).toEqual({ sessionID: child.id, status: "completed" })
  285. expect(progress[0]).toEqual({ sessionID: child.id, status: "running" })
  286. expect(child).toMatchObject({
  287. parentID: parent.id,
  288. location: parent.location,
  289. agent: "reviewer",
  290. model: childModel,
  291. })
  292. expect((yield* sessions.inbox(child.id)).find((message) => message.type === "user")?.payload.text).toBe(
  293. "You are a subagent spawned by another session.\nreview this",
  294. )
  295. const fallback = yield* executeTool(registry, {
  296. sessionID: parent.id,
  297. ...toolIdentity,
  298. call: {
  299. type: "tool-call",
  300. id: "call-subagent-fallback",
  301. name: SubagentTool.name,
  302. input: { agent: "fallback", description: "fallback", prompt: "fallback" },
  303. },
  304. })
  305. const fallbackChild = yield* sessions.get(outputSessionID(fallback.metadata))
  306. expect(fallbackChild).toMatchObject({ parentID: parent.id, model: parentModel })
  307. }),
  308. ),
  309. ),
  310. )
  311. it.live("returns child runner failures as tool errors", () =>
  312. Effect.acquireRelease(
  313. Effect.promise(() => tmpdir()),
  314. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  315. ).pipe(
  316. Effect.flatMap((dir) =>
  317. Effect.gen(function* () {
  318. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  319. const sessions = yield* Session.Service
  320. const parent = yield* sessions.create({ location })
  321. yield* withSubagent(parent.location)
  322. const locations = yield* LocationServiceMap.Service
  323. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  324. expect(
  325. yield* executeTool(registry, {
  326. sessionID: parent.id,
  327. ...toolIdentity,
  328. call: {
  329. type: "tool-call",
  330. id: "call-subagent-failure",
  331. name: SubagentTool.name,
  332. input: { agent: "reviewer", description: "fail review", prompt: "please fail" },
  333. },
  334. }),
  335. ).toEqual({
  336. status: "error",
  337. error: {
  338. type: "tool.execution",
  339. message: expect.stringContaining("No model is available for session"),
  340. },
  341. })
  342. }),
  343. ),
  344. ),
  345. )
  346. it.live("notifies once when background work completes", () =>
  347. Effect.acquireRelease(
  348. Effect.promise(() => tmpdir()),
  349. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  350. ).pipe(
  351. Effect.flatMap((dir) =>
  352. Effect.gen(function* () {
  353. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  354. const sessions = yield* Session.Service
  355. const parent = yield* sessions.create({ location })
  356. yield* withSubagent(parent.location)
  357. const locations = yield* LocationServiceMap.Service
  358. const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
  359. const bus = yield* Bus.Service
  360. const admitted = yield* bus.subscribe(SessionEvent.InboxEnqueued).pipe(
  361. Stream.filter((event) => event.data.sessionID === parent.id && event.data.item.type === "synthetic"),
  362. Stream.take(1),
  363. Stream.runCollect,
  364. Effect.forkScoped({ startImmediately: true }),
  365. )
  366. const settled = yield* executeTool(registry, {
  367. sessionID: parent.id,
  368. ...toolIdentity,
  369. call: {
  370. type: "tool-call",
  371. id: "call-background-subagent",
  372. name: SubagentTool.name,
  373. input: { agent: "reviewer", description: "background review", prompt: "review", background: true },
  374. },
  375. })
  376. const childID = outputSessionID(settled.metadata)
  377. expect(settled.metadata).toMatchObject({
  378. status: "running",
  379. })
  380. expect(settled.metadata).toEqual({ sessionID: childID, status: "running" })
  381. expect(settled.content).toEqual([{ type: "text", text: expect.stringContaining(`id: ${childID}`) }])
  382. const admission = Array.from(yield* Fiber.join(admitted))[0]
  383. expect(admission?.data.item.type).toBe("synthetic")
  384. if (admission?.data.item.type !== "synthetic") return yield* Effect.die("Expected synthetic inbox item")
  385. expect(admission?.data.item.payload.text).toContain(`<subagent id="${childID}" state="completed"`)
  386. expect(admission?.data.item.payload).toMatchObject({
  387. description: "background review",
  388. metadata: {
  389. source: "subagent",
  390. childID,
  391. agent: "reviewer",
  392. state: "completed",
  393. },
  394. })
  395. const database = yield* Database.Service
  396. yield* SessionInbox.promote(database.db, bus, parent.id, "steer")
  397. const synthetic = (yield* sessions.context(parent.id)).filter((message) => message.type === "synthetic")
  398. expect(synthetic).toHaveLength(1)
  399. expect(synthetic[0]?.text).toContain(`<subagent id="${childID}" state="completed"`)
  400. expect(synthetic[0]?.text).toContain(childText)
  401. }),
  402. ),
  403. ),
  404. )
  405. })