agent.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import path from "path"
  2. import { describe, expect } from "bun:test"
  3. import { Effect, Exit, Fiber, Layer, Scope, Stream } from "effect"
  4. import { TestClock } from "effect/testing"
  5. import { Agent } from "@opencode-ai/core/agent"
  6. import { Bus } from "@opencode-ai/core/bus"
  7. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  8. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  9. import { Location } from "@opencode-ai/core/location"
  10. import { Permission } from "@opencode-ai/core/permission"
  11. import { AgentPlugin } from "@opencode-ai/core/plugin/agent"
  12. import { AbsolutePath } from "@opencode-ai/core/schema"
  13. import { Global } from "@opencode-ai/util/global"
  14. import { location } from "./fixture/location"
  15. import { testEffect } from "./lib/effect"
  16. import { agentHost, host } from "./plugin/host"
  17. const testLocation = location({ directory: AbsolutePath.make("/project") })
  18. const locationLayer = Layer.succeed(Location.Service, Location.Service.of(testLocation))
  19. const global = Global.make({ data: "/data", config: "/config", tmp: "/tmp/opencode" })
  20. const globalLayer = Layer.succeed(Global.Service, Global.Service.of(global))
  21. const it = testEffect(
  22. AppNodeBuilder.build(LayerNode.group([Agent.node, Bus.node, Location.node]), [
  23. [Global.node, globalLayer],
  24. [Location.node, locationLayer],
  25. ]) as unknown as Layer.Layer<unknown, never>,
  26. )
  27. describe("Agent", () => {
  28. it.effect("publishes an updated event after agent changes", () =>
  29. Effect.gen(function* () {
  30. const agent = yield* Agent.Service
  31. const bus = yield* Bus.Service
  32. const updated = yield* bus
  33. .subscribe(Agent.Event.Updated)
  34. .pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  35. yield* Effect.yieldNow
  36. yield* agent.transform((editor) => editor.update(Agent.ID.make("reviewer"), () => {}))
  37. expect(yield* Fiber.join(updated)).toMatchObject([{ location: { directory: testLocation.directory } }])
  38. }),
  39. )
  40. it.effect("starts without agents", () =>
  41. Effect.gen(function* () {
  42. const agent = yield* Agent.Service
  43. expect(yield* agent.list()).toEqual([])
  44. expect(yield* agent.get(Agent.ID.make("build"))).toBeUndefined()
  45. }),
  46. )
  47. it.effect("materializes replayable agent transforms", () =>
  48. Effect.gen(function* () {
  49. const agent = yield* Agent.Service
  50. const id = Agent.ID.make("reviewer")
  51. yield* agent.transform((editor) =>
  52. editor.update(id, (info) => {
  53. info.description = "Reviews code"
  54. info.mode = "subagent"
  55. }),
  56. )
  57. expect(yield* agent.get(id)).toMatchObject({ id, description: "Reviews code", mode: "subagent" })
  58. expect((yield* agent.list()).map((info) => info.id)).toEqual([id])
  59. }),
  60. )
  61. it.effect("rebuilds state when a transform is replaced", () =>
  62. Effect.gen(function* () {
  63. const agent = yield* Agent.Service
  64. const id = Agent.ID.make("reviewer")
  65. let description = "Old description"
  66. let hidden = true
  67. yield* agent.transform((editor) =>
  68. editor.update(id, (info) => {
  69. info.description = description
  70. info.hidden = hidden
  71. }),
  72. )
  73. description = "New description"
  74. hidden = false
  75. const reload = yield* agent.reload().pipe(Effect.forkChild({ startImmediately: true }))
  76. yield* TestClock.adjust("500 millis")
  77. yield* Fiber.join(reload)
  78. expect(yield* agent.get(id)).toMatchObject({ description: "New description", hidden: false })
  79. }),
  80. )
  81. it.effect("removes a transform when its scope closes", () =>
  82. Effect.gen(function* () {
  83. const agent = yield* Agent.Service
  84. const id = Agent.ID.make("scoped")
  85. const scope = yield* Scope.make()
  86. yield* agent.transform((editor) => editor.update(id, () => {})).pipe(Scope.provide(scope))
  87. expect(yield* agent.get(id)).toBeDefined()
  88. yield* Scope.close(scope, Exit.void)
  89. expect(yield* agent.get(id)).toBeUndefined()
  90. }),
  91. )
  92. it.effect("applies direct agent updates", () =>
  93. Effect.gen(function* () {
  94. const agent = yield* Agent.Service
  95. const id = Agent.ID.make("build")
  96. yield* agent.transform((editor) =>
  97. editor.update(id, (info) => {
  98. info.mode = "primary"
  99. info.hidden = true
  100. }),
  101. )
  102. expect(yield* agent.get(id)).toMatchObject({ id, mode: "primary", hidden: true })
  103. }),
  104. )
  105. it.effect("creates agents with runtime defaults and supports direct removal", () =>
  106. Effect.gen(function* () {
  107. const agent = yield* Agent.Service
  108. const id = Agent.ID.make("custom")
  109. yield* agent.transform((editor) => editor.update(id, () => {}))
  110. const info = yield* agent.get(id)
  111. expect(info?.mode).toBe("primary")
  112. expect(info?.permissions.slice(0, Agent.Info.default(id).permissions.length)).toEqual(
  113. Agent.Info.default(id).permissions,
  114. )
  115. expect(
  116. Permission.evaluate("external_directory", path.join(global.data, "shell", "*", "*"), info?.permissions ?? [])
  117. .effect,
  118. ).toBe("allow")
  119. expect(
  120. Permission.evaluate("external_directory", path.join(global.data, "tool-output", "*"), info?.permissions ?? [])
  121. .effect,
  122. ).toBe("allow")
  123. expect(
  124. Permission.evaluate("external_directory", path.join(global.config, "*"), info?.permissions ?? []).effect,
  125. ).toBe("allow")
  126. expect(
  127. Permission.evaluate("external_directory", path.join(global.tmp, "*"), info?.permissions ?? []).effect,
  128. ).toBe("allow")
  129. yield* agent.transform((editor) => editor.remove(id))
  130. expect(yield* agent.get(id)).toBeUndefined()
  131. }),
  132. )
  133. it.effect("applies managed external directories without opting built-in agents into bash", () =>
  134. Effect.gen(function* () {
  135. const agent = yield* Agent.Service
  136. yield* AgentPlugin.Plugin.effect(
  137. host({
  138. agent: agentHost(agent),
  139. }),
  140. )
  141. const agents = yield* agent.list()
  142. expect(agents.map((item) => String(item.id)).sort()).toEqual([
  143. "build",
  144. "compaction",
  145. "explore",
  146. "general",
  147. "plan",
  148. "summary",
  149. "title",
  150. ])
  151. expect((yield* agent.get(Agent.defaultID))?.system).toBeUndefined()
  152. const permissions = (yield* agent.get(Agent.defaultID))?.permissions ?? []
  153. expect(
  154. Permission.evaluate("external_directory", path.join(global.data, "shell", "*", "*"), permissions).effect,
  155. ).toBe("allow")
  156. expect(
  157. Permission.evaluate("external_directory", path.join(global.data, "tool-output", "*"), permissions).effect,
  158. ).toBe("allow")
  159. expect(Permission.evaluate("external_directory", path.join(global.config, "*"), permissions).effect).toBe("allow")
  160. expect(Permission.evaluate("external_directory", path.join(global.tmp, "*"), permissions).effect).toBe("allow")
  161. const explore = yield* agent.get(Agent.ID.make("explore"))
  162. expect(Permission.evaluate("read", ".env", explore?.permissions ?? []).effect).toBe("ask")
  163. expect(Permission.evaluate("read", ".env.local", explore?.permissions ?? []).effect).toBe("ask")
  164. expect(Permission.evaluate("read", ".env.example", explore?.permissions ?? []).effect).toBe("allow")
  165. expect(Permission.evaluate("read", "src/index.ts", explore?.permissions ?? []).effect).toBe("allow")
  166. for (const item of agents) {
  167. expect(item.permissions.some((rule) => rule.action === "bash" && rule.effect !== "deny")).toBe(false)
  168. }
  169. }),
  170. )
  171. it.effect("denies the subagent tool for built-in subagents", () =>
  172. Effect.gen(function* () {
  173. const agent = yield* Agent.Service
  174. yield* AgentPlugin.Plugin.effect(
  175. host({
  176. agent: agentHost(agent),
  177. }),
  178. )
  179. yield* Effect.forEach(["general", "explore"], (id) =>
  180. Effect.gen(function* () {
  181. const info = yield* agent.get(Agent.ID.make(id))
  182. if (!info) throw new Error(`expected built-in agent: ${id}`)
  183. expect(info.mode).toBe("subagent")
  184. expect(info.permissions).toContainEqual({ action: "subagent", resource: "*", effect: "deny" })
  185. expect(Permission.evaluate("subagent", "*", info.permissions).effect).toBe("deny")
  186. }),
  187. )
  188. }),
  189. )
  190. })