location-layer.test.ts 7.1 KB

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