session-runner-tool-registry.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. import { describe, expect } from "bun:test"
  2. import { Tool } from "@opencode-ai/core/tool/tool"
  3. import { AgentV2 } from "@opencode-ai/core/agent"
  4. import { ApplicationTools } from "@opencode-ai/core/tool/application-tools"
  5. import { SessionV2 } from "@opencode-ai/core/session"
  6. import { SessionMessage } from "@opencode-ai/core/session/message"
  7. import { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  8. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  9. import { executeTool, settleTool, toolDefinitions } from "./lib/tool"
  10. import { Cause, Deferred, Effect, Exit, Fiber, Layer, Option, Schema, SchemaGetter, SchemaIssue, Scope } from "effect"
  11. import { testEffect } from "./lib/effect"
  12. const bounds: ToolOutputStore.BoundInput[] = []
  13. const retentionFailure = new ToolOutputStore.StorageError({ operation: "write", cause: new Error("disk full") })
  14. const outputStore = Layer.mock(ToolOutputStore.Service, {
  15. bound: (input) => {
  16. if (input.toolCallID === "call-retention-failure") return Effect.fail(retentionFailure)
  17. return Effect.sync(() => bounds.push(input)).pipe(
  18. Effect.as(
  19. input.toolCallID === "call-bounded"
  20. ? {
  21. output: { structured: {}, content: [{ type: "text" as const, text: "bounded reference" }] },
  22. outputPaths: ["/managed/generic"],
  23. }
  24. : { output: input.output, outputPaths: [] },
  25. ),
  26. )
  27. },
  28. })
  29. const registry = ToolRegistry.layer.pipe(Layer.provide(ApplicationTools.layer), Layer.provide(outputStore))
  30. const it = testEffect(registry)
  31. const integrated = testEffect(Layer.mergeAll(ApplicationTools.layer, registry))
  32. const identity = {
  33. agent: AgentV2.ID.make("build"),
  34. assistantMessageID: SessionMessage.ID.make("msg_registry"),
  35. }
  36. const sessionID = SessionV2.ID.make("ses_registry")
  37. const call = (name: string, id = `call-${name}`): ToolRegistry.ExecuteInput => ({
  38. sessionID,
  39. ...identity,
  40. call: { type: "tool-call", id, name, input: { text: name } },
  41. })
  42. const make = (permission?: string) => {
  43. const tool = Tool.make({
  44. description: "Echo text",
  45. input: Schema.Struct({ text: Schema.String }),
  46. output: Schema.Struct({ text: Schema.String }),
  47. execute: ({ text }) => Effect.succeed({ text }),
  48. toModelOutput: ({ output }) => [{ type: "text", text: output.text }],
  49. })
  50. return permission ? Tool.withPermission(tool, permission) : tool
  51. }
  52. describe("ToolRegistry", () => {
  53. it.effect("filters disabled tools with edit aliases and ordered wildcard precedence", () =>
  54. Effect.gen(function* () {
  55. const service = yield* ToolRegistry.Service
  56. yield* service.register({
  57. question: make(),
  58. bash: make(),
  59. edit: make("edit"),
  60. write: make("edit"),
  61. apply_patch: make("edit"),
  62. })
  63. const names = (rules: Parameters<ToolRegistry.Interface["materialize"]>[0]) =>
  64. toolDefinitions(service, rules).pipe(Effect.map((definitions) => definitions.map((tool) => tool.name)))
  65. expect(yield* names([{ action: "question", resource: "*", effect: "deny" }])).toEqual([
  66. "bash",
  67. "edit",
  68. "write",
  69. "apply_patch",
  70. ])
  71. expect(
  72. yield* names([
  73. { action: "*", resource: "*", effect: "deny" },
  74. { action: "question", resource: "private", effect: "allow" },
  75. ]),
  76. ).toEqual(["question"])
  77. expect(
  78. yield* names([
  79. { action: "question", resource: "private", effect: "allow" },
  80. { action: "*", resource: "*", effect: "deny" },
  81. ]),
  82. ).toEqual([])
  83. expect(yield* names([{ action: "edit", resource: "*", effect: "deny" }])).toEqual(["question", "bash"])
  84. }),
  85. )
  86. it.effect("keeps permission decoration isolated between registrations", () =>
  87. Effect.gen(function* () {
  88. const service = yield* ToolRegistry.Service
  89. const shared = make()
  90. yield* service.register({ first: shared })
  91. yield* service.register({ second: Tool.withPermission(shared, "edit") })
  92. Tool.withPermission(shared, "question")
  93. expect(
  94. (yield* toolDefinitions(service, [{ action: "edit", resource: "*", effect: "deny" }])).map(
  95. (definition) => definition.name,
  96. ),
  97. ).toEqual(["first"])
  98. }),
  99. )
  100. it.effect("reuses model definitions across provider turns", () =>
  101. Effect.gen(function* () {
  102. const service = yield* ToolRegistry.Service
  103. yield* service.register({ echo: make() })
  104. const first = yield* toolDefinitions(service)
  105. const second = yield* toolDefinitions(service)
  106. expect(second[0]).toBe(first[0])
  107. }),
  108. )
  109. it.effect("removes a scoped registration", () =>
  110. Effect.gen(function* () {
  111. const service = yield* ToolRegistry.Service
  112. const scope = yield* Scope.make()
  113. yield* service.register({ echo: make() }).pipe(Scope.provide(scope))
  114. expect((yield* toolDefinitions(service)).map((tool) => tool.name)).toEqual(["echo"])
  115. yield* Scope.close(scope, Exit.void)
  116. expect(yield* toolDefinitions(service)).toEqual([])
  117. }),
  118. )
  119. it.effect("preserves an interrupted registration until its scope closes", () =>
  120. Effect.gen(function* () {
  121. const service = yield* ToolRegistry.Service
  122. const scope = yield* Scope.make()
  123. const registered = yield* Deferred.make<void>()
  124. const fiber = yield* service
  125. .register({ echo: make() })
  126. .pipe(
  127. Effect.andThen(Deferred.succeed(registered, undefined)),
  128. Effect.andThen(Effect.never),
  129. Scope.provide(scope),
  130. Effect.forkChild,
  131. )
  132. yield* Deferred.await(registered)
  133. yield* Fiber.interrupt(fiber)
  134. expect((yield* toolDefinitions(service)).map((tool) => tool.name)).toEqual(["echo"])
  135. yield* Scope.close(scope, Exit.void)
  136. expect(yield* toolDefinitions(service)).toEqual([])
  137. }),
  138. )
  139. it.effect("returns model errors without swallowing interruption or defects", () =>
  140. Effect.gen(function* () {
  141. const service = yield* ToolRegistry.Service
  142. yield* service.register({
  143. failed: Tool.make({
  144. description: "Failed",
  145. input: Schema.Struct({}),
  146. output: Schema.Struct({ ok: Schema.Boolean }),
  147. execute: () => Effect.fail(new Tool.Failure({ message: "Denied" })),
  148. }),
  149. })
  150. expect(
  151. yield* executeTool(service, {
  152. sessionID,
  153. ...identity,
  154. call: { type: "tool-call", id: "failed", name: "failed", input: {} },
  155. }),
  156. ).toEqual({ type: "error", value: "Denied" })
  157. expect(
  158. yield* executeTool(service, {
  159. sessionID,
  160. ...identity,
  161. call: { type: "tool-call", id: "missing", name: "missing", input: {} },
  162. }),
  163. ).toEqual({ type: "error", value: "Unknown tool: missing" })
  164. yield* service.register({
  165. defect: Tool.make({
  166. description: "Defect",
  167. input: Schema.Struct({}),
  168. output: Schema.Struct({}),
  169. execute: () => Effect.die("unexpected executor defect"),
  170. }),
  171. })
  172. expect(
  173. yield* service.materialize().pipe(
  174. Effect.flatMap((materialized) =>
  175. materialized.settle({
  176. sessionID,
  177. ...identity,
  178. call: { type: "tool-call", id: "defect", name: "defect", input: {} },
  179. }),
  180. ),
  181. Effect.catchDefect(Effect.succeed),
  182. ),
  183. ).toBe("unexpected executor defect")
  184. }),
  185. )
  186. it.effect("propagates retention failures through settlement", () =>
  187. Effect.gen(function* () {
  188. const service = yield* ToolRegistry.Service
  189. yield* service.register({ echo: make() })
  190. const materialized = yield* service.materialize()
  191. const exit = yield* materialized.settle(call("echo", "call-retention-failure")).pipe(Effect.exit)
  192. expect(Exit.isFailure(exit)).toBe(true)
  193. if (Exit.isFailure(exit)) expect(Option.getOrUndefined(Cause.findErrorOption(exit.cause))).toBe(retentionFailure)
  194. }),
  195. )
  196. it.effect("exposes settlement only through materialization", () =>
  197. Effect.gen(function* () {
  198. const service = yield* ToolRegistry.Service
  199. expect("definitions" in service).toBe(false)
  200. expect("execute" in service).toBe(false)
  201. expect("settle" in service).toBe(false)
  202. expect(typeof service.materialize).toBe("function")
  203. }),
  204. )
  205. it.effect("passes complete invocation identity to the canonical handler", () =>
  206. Effect.gen(function* () {
  207. const service = yield* ToolRegistry.Service
  208. const contexts: Tool.Context[] = []
  209. yield* service.register({
  210. context: Tool.make({
  211. description: "Context",
  212. input: Schema.Struct({}),
  213. output: Schema.Struct({ ok: Schema.Boolean }),
  214. execute: (_, context) => Effect.sync(() => contexts.push(context)).pipe(Effect.as({ ok: true })),
  215. }),
  216. })
  217. yield* executeTool(service, {
  218. sessionID,
  219. ...identity,
  220. call: { type: "tool-call", id: "call-context", name: "context", input: {} },
  221. })
  222. expect(contexts).toEqual([{ sessionID, ...identity, toolCallID: "call-context" }])
  223. }),
  224. )
  225. it.effect("encodes output and applies generic settlement bounding", () =>
  226. Effect.gen(function* () {
  227. bounds.length = 0
  228. const service = yield* ToolRegistry.Service
  229. yield* service.register({ bounded: make() })
  230. expect(
  231. yield* settleTool(service, {
  232. sessionID,
  233. ...identity,
  234. call: { type: "tool-call", id: "call-bounded", name: "bounded", input: { text: "complete" } },
  235. }),
  236. ).toEqual({
  237. result: { type: "text", value: "bounded reference" },
  238. output: { structured: {}, content: [{ type: "text", text: "bounded reference" }] },
  239. outputPaths: ["/managed/generic"],
  240. })
  241. expect(bounds).toHaveLength(1)
  242. }),
  243. )
  244. it.effect("enforces transformed codecs at execution and projection boundaries", () =>
  245. Effect.gen(function* () {
  246. const service = yield* ToolRegistry.Service
  247. const executed: string[] = []
  248. const Transformed = Schema.Boolean.pipe(
  249. Schema.decodeTo(Schema.String, {
  250. decode: SchemaGetter.transform((value) => (value ? "yes" : "no")),
  251. encode: SchemaGetter.transform((value) => value === "yes"),
  252. }),
  253. )
  254. yield* service.register({
  255. transformed: Tool.make({
  256. description: "Transform values",
  257. input: Schema.Struct({ value: Transformed }),
  258. output: Schema.Struct({ value: Transformed }),
  259. execute: ({ value }) => Effect.sync(() => executed.push(value)).pipe(Effect.as({ value })),
  260. toModelOutput: ({ output }) => [{ type: "text", text: String(output.value) }],
  261. }),
  262. })
  263. expect(
  264. yield* executeTool(service, {
  265. sessionID,
  266. ...identity,
  267. call: { type: "tool-call", id: "transformed", name: "transformed", input: { value: true } },
  268. }),
  269. ).toEqual({ type: "text", value: "true" })
  270. expect(executed).toEqual(["yes"])
  271. expect(
  272. yield* executeTool(service, {
  273. sessionID,
  274. ...identity,
  275. call: { type: "tool-call", id: "invalid-input", name: "transformed", input: { value: "yes" } },
  276. }),
  277. ).toMatchObject({ type: "error", value: expect.stringContaining("Invalid tool input") })
  278. expect(executed).toEqual(["yes"])
  279. yield* service.register({
  280. invalid_output: Tool.make({
  281. description: "Return invalid output",
  282. input: Schema.Struct({}),
  283. output: Schema.Struct({
  284. value: Schema.Boolean.pipe(
  285. Schema.decodeTo(Schema.String, {
  286. decode: SchemaGetter.transform((value) => String(value)),
  287. encode: SchemaGetter.transformOrFail((value) =>
  288. value === "valid"
  289. ? Effect.succeed(true)
  290. : Effect.fail(new SchemaIssue.InvalidValue(Option.some(value), { message: "invalid output" })),
  291. ),
  292. }),
  293. ),
  294. }),
  295. execute: () => Effect.succeed({ value: "invalid" }),
  296. }),
  297. })
  298. expect(
  299. yield* executeTool(service, {
  300. sessionID,
  301. ...identity,
  302. call: { type: "tool-call", id: "invalid-output", name: "invalid_output", input: {} },
  303. }),
  304. ).toMatchObject({ type: "error", value: expect.stringContaining("invalid value for its output schema") })
  305. }),
  306. )
  307. it.effect("executes the unchanged registration advertised for a provider turn", () =>
  308. Effect.gen(function* () {
  309. const service = yield* ToolRegistry.Service
  310. yield* service.register({ echo: make() })
  311. const materialized = yield* service.materialize()
  312. expect((yield* materialized.settle(call("echo"))).result).toEqual({ type: "text", value: "echo" })
  313. }),
  314. )
  315. it.effect("rejects a call when its advertised registration was removed", () =>
  316. Effect.gen(function* () {
  317. const service = yield* ToolRegistry.Service
  318. const scope = yield* Scope.make()
  319. yield* service.register({ echo: make() }).pipe(Scope.provide(scope))
  320. const materialized = yield* service.materialize()
  321. yield* Scope.close(scope, Exit.void)
  322. expect((yield* materialized.settle(call("echo"))).result).toEqual({
  323. type: "error",
  324. value: "Stale tool call: echo",
  325. })
  326. }),
  327. )
  328. it.effect("rejects only the replaced name from a multi-tool provider turn", () =>
  329. Effect.gen(function* () {
  330. const service = yield* ToolRegistry.Service
  331. yield* service.register({ first: make(), second: make() })
  332. const materialized = yield* service.materialize()
  333. yield* service.register({ first: make() })
  334. expect((yield* materialized.settle(call("first"))).result).toEqual({
  335. type: "error",
  336. value: "Stale tool call: first",
  337. })
  338. expect((yield* materialized.settle(call("second"))).result).toEqual({ type: "text", value: "second" })
  339. }),
  340. )
  341. it.effect("treats revealing a previous overlay as stale", () =>
  342. Effect.gen(function* () {
  343. const service = yield* ToolRegistry.Service
  344. yield* service.register({ echo: make() })
  345. const overlay = yield* Scope.make()
  346. yield* service.register({ echo: make() }).pipe(Scope.provide(overlay))
  347. const materialized = yield* service.materialize()
  348. yield* Scope.close(overlay, Exit.void)
  349. expect((yield* materialized.settle(call("echo"))).result).toEqual({
  350. type: "error",
  351. value: "Stale tool call: echo",
  352. })
  353. }),
  354. )
  355. integrated.effect("rejects an application call after a Location override is registered", () =>
  356. Effect.gen(function* () {
  357. const applications = yield* ApplicationTools.Service
  358. const service = yield* ToolRegistry.Service
  359. yield* applications.register({ echo: make() })
  360. const materialized = yield* service.materialize()
  361. yield* service.register({ echo: make() })
  362. expect((yield* materialized.settle(call("echo"))).result).toEqual({
  363. type: "error",
  364. value: "Stale tool call: echo",
  365. })
  366. }),
  367. )
  368. integrated.effect("rejects a Location call after removal reveals an application registration", () =>
  369. Effect.gen(function* () {
  370. const applications = yield* ApplicationTools.Service
  371. const service = yield* ToolRegistry.Service
  372. yield* applications.register({ echo: make() })
  373. const scope = yield* Scope.make()
  374. yield* service.register({ echo: make() }).pipe(Scope.provide(scope))
  375. const materialized = yield* service.materialize()
  376. yield* Scope.close(scope, Exit.void)
  377. expect((yield* materialized.settle(call("echo"))).result).toEqual({
  378. type: "error",
  379. value: "Stale tool call: echo",
  380. })
  381. }),
  382. )
  383. it.effect("keeps captured execution running after registration mutation", () =>
  384. Effect.gen(function* () {
  385. const service = yield* ToolRegistry.Service
  386. const started = yield* Deferred.make<void>()
  387. const release = yield* Deferred.make<void>()
  388. const scope = yield* Scope.make()
  389. yield* service
  390. .register({
  391. echo: Tool.make({
  392. description: "Echo text",
  393. input: Schema.Struct({ text: Schema.String }),
  394. output: Schema.Struct({ text: Schema.String }),
  395. execute: ({ text }) =>
  396. Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(release)), Effect.as({ text })),
  397. toModelOutput: ({ output }) => [{ type: "text", text: output.text }],
  398. }),
  399. })
  400. .pipe(Scope.provide(scope))
  401. const materialized = yield* service.materialize()
  402. const settlement = yield* materialized.settle(call("echo")).pipe(Effect.forkChild)
  403. yield* Deferred.await(started)
  404. yield* Scope.close(scope, Exit.void)
  405. yield* service.register({ echo: make() })
  406. yield* Deferred.succeed(release, undefined)
  407. expect(yield* Fiber.join(settlement)).toMatchObject({ result: { type: "text", value: "echo" } })
  408. }),
  409. )
  410. })