location-layer.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Deferred, Effect, Equal, Hash, Layer, Schema, Stream } from "effect"
  5. import { Tool } from "@opencode-ai/core/public"
  6. import { define } from "@opencode-ai/plugin/v2/effect"
  7. import { AgentV2 } from "@opencode-ai/core/agent"
  8. import { Catalog } from "@opencode-ai/core/catalog"
  9. import { LocationServiceMap } from "@opencode-ai/core/location-layer"
  10. import { Location } from "@opencode-ai/core/location"
  11. import { PluginBoot } from "@opencode-ai/core/plugin/boot"
  12. import { ProviderV2 } from "@opencode-ai/core/provider"
  13. import { AbsolutePath } from "@opencode-ai/core/schema"
  14. import { tmpdir } from "./fixture/tmpdir"
  15. import { testEffect } from "./lib/effect"
  16. import { toolDefinitions } from "./lib/tool"
  17. import { FSUtil } from "../src/fs-util"
  18. import { Credential } from "../src/credential"
  19. import { Database } from "../src/database/database"
  20. import { EventV2 } from "../src/event"
  21. import { Global } from "../src/global"
  22. import { ModelsDev } from "../src/models-dev"
  23. import { Npm } from "../src/npm"
  24. import { Project } from "../src/project"
  25. import { Reference } from "../src/reference"
  26. import { ToolRegistry } from "../src/tool/registry"
  27. import { ApplicationTools } from "../src/tool/application-tools"
  28. const applicationTools = ApplicationTools.layer
  29. const it = testEffect(
  30. Layer.merge(
  31. Layer.mergeAll(applicationTools, Database.defaultLayer, EventV2.defaultLayer),
  32. LocationServiceMap.layer.pipe(
  33. Layer.provide(applicationTools),
  34. Layer.provide(
  35. Layer.mergeAll(
  36. Project.defaultLayer,
  37. EventV2.defaultLayer,
  38. Credential.defaultLayer.pipe(Layer.fresh),
  39. Npm.defaultLayer,
  40. ModelsDev.defaultLayer,
  41. FSUtil.defaultLayer,
  42. Global.defaultLayer,
  43. ),
  44. ),
  45. ),
  46. ),
  47. )
  48. describe("LocationServiceMap", () => {
  49. it.effect("compares equivalent location refs by value", () =>
  50. Effect.sync(() => {
  51. const directory = AbsolutePath.make("/project")
  52. expect(Equal.equals(Location.Ref.make({ directory }), Location.Ref.make({ directory }))).toBe(true)
  53. expect(Hash.hash(Location.Ref.make({ directory }))).toBe(
  54. Hash.hash(Location.Ref.make({ directory, workspaceID: undefined })),
  55. )
  56. }),
  57. )
  58. it.live("isolates location state while sharing location policy with catalog", () =>
  59. Effect.acquireRelease(
  60. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  61. (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
  62. ).pipe(
  63. Effect.flatMap(([blocked, allowed]) =>
  64. Effect.gen(function* () {
  65. yield* (yield* ApplicationTools.Service).register({
  66. application_context: Tool.make({
  67. description: "Read application context",
  68. input: Schema.Struct({}),
  69. output: Schema.Struct({ ok: Schema.Boolean }),
  70. execute: () => Effect.succeed({ ok: true }),
  71. }),
  72. })
  73. yield* Effect.promise(() =>
  74. fs.writeFile(
  75. path.join(blocked.path, "opencode.json"),
  76. JSON.stringify({
  77. experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
  78. }),
  79. ),
  80. )
  81. const update = (directory: string) =>
  82. Effect.gen(function* () {
  83. yield* PluginBoot.Service.use((boot) => boot.wait())
  84. yield* Reference.Service
  85. const catalog = yield* Catalog.Service
  86. yield* catalog.transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
  87. return {
  88. providers: yield* catalog.provider.all(),
  89. tools: yield* toolDefinitions(yield* ToolRegistry.Service),
  90. }
  91. }).pipe(
  92. Effect.scoped,
  93. Effect.provide(LocationServiceMap.get(Location.Ref.make({ directory: AbsolutePath.make(directory) }))),
  94. )
  95. const blockedState = yield* update(blocked.path)
  96. expect(blockedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(false)
  97. expect(blockedState.tools.map((tool) => tool.name).sort()).toEqual([
  98. "application_context",
  99. "apply_patch",
  100. "bash",
  101. "edit",
  102. "glob",
  103. "grep",
  104. "question",
  105. "read",
  106. "skill",
  107. "todowrite",
  108. "webfetch",
  109. "websearch",
  110. "write",
  111. ])
  112. const allowedState = yield* update(allowed.path)
  113. expect(allowedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(true)
  114. expect(allowedState.tools.map((tool) => tool.name).sort()).toEqual([
  115. "application_context",
  116. "apply_patch",
  117. "bash",
  118. "edit",
  119. "glob",
  120. "grep",
  121. "question",
  122. "read",
  123. "skill",
  124. "todowrite",
  125. "webfetch",
  126. "websearch",
  127. "write",
  128. ])
  129. }),
  130. ),
  131. ),
  132. )
  133. it.live("installs public plugins into a location", () =>
  134. Effect.acquireRelease(
  135. Effect.promise(() => tmpdir()),
  136. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  137. ).pipe(
  138. Effect.flatMap((dir) =>
  139. Effect.gen(function* () {
  140. const boot = yield* PluginBoot.Service
  141. const catalogUpdated = yield* Deferred.make<void>()
  142. const seen: string[] = []
  143. yield* boot.add(
  144. define({
  145. id: "reviewer",
  146. effect: (ctx) =>
  147. Effect.gen(function* () {
  148. yield* ctx.event.subscribe("catalog.updated").pipe(
  149. Stream.runForEach(() => Deferred.succeed(catalogUpdated, undefined).pipe(Effect.asVoid)),
  150. Effect.forkScoped({ startImmediately: true }),
  151. )
  152. yield* ctx.agent.transform((agent) => {
  153. agent.update("reviewer", (item) => {
  154. item.description = "Reviews code"
  155. item.mode = "subagent"
  156. })
  157. })
  158. seen.push((yield* ctx.agent.get("reviewer"))?.description ?? "")
  159. yield* ctx.catalog.transform((catalog) => {
  160. catalog.provider.update("public", (provider) => {
  161. provider.name = "Public provider"
  162. })
  163. })
  164. }),
  165. }),
  166. )
  167. yield* Deferred.await(catalogUpdated)
  168. expect(seen).toEqual(["Reviews code"])
  169. expect(yield* (yield* AgentV2.Service).get(AgentV2.ID.make("reviewer"))).toMatchObject({
  170. description: "Reviews code",
  171. mode: "subagent",
  172. })
  173. }).pipe(
  174. Effect.scoped,
  175. Effect.provide(LocationServiceMap.get(Location.Ref.make({ directory: AbsolutePath.make(dir.path) }))),
  176. ),
  177. ),
  178. ),
  179. )
  180. })