location-layer.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { DateTime, Effect, Equal, Hash, Layer, Schema } 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 { PluginV2 } from "@opencode-ai/core/plugin"
  12. import { ModelV2 } from "@opencode-ai/core/model"
  13. import { ProjectV2 } from "@opencode-ai/core/project"
  14. import { ProviderV2 } from "@opencode-ai/core/provider"
  15. import { AbsolutePath } from "@opencode-ai/core/schema"
  16. import { SessionV2 } from "@opencode-ai/core/session"
  17. import { SessionRunnerModel } from "@opencode-ai/core/session/runner/model"
  18. import { tmpdir } from "./fixture/tmpdir"
  19. import { testEffect } from "./lib/effect"
  20. import { toolDefinitions } from "./lib/tool"
  21. import { FSUtil } from "../src/fs-util"
  22. import { Credential } from "../src/credential"
  23. import { Database } from "../src/database/database"
  24. import { EventV2 } from "../src/event"
  25. import { Global } from "../src/global"
  26. import { ModelsDev } from "../src/models-dev"
  27. import { Npm } from "../src/npm"
  28. import { Project } from "../src/project"
  29. import { Reference } from "../src/reference"
  30. import { ToolRegistry } from "../src/tool/registry"
  31. import { ApplicationTools } from "../src/tool/application-tools"
  32. const applicationTools = ApplicationTools.layer
  33. const it = testEffect(
  34. Layer.merge(
  35. Layer.mergeAll(applicationTools, Database.defaultLayer, EventV2.defaultLayer),
  36. LocationServiceMap.layer.pipe(
  37. Layer.provide(applicationTools),
  38. Layer.provide(
  39. Layer.mergeAll(
  40. Project.defaultLayer,
  41. EventV2.defaultLayer,
  42. Credential.defaultLayer.pipe(Layer.fresh),
  43. Npm.defaultLayer,
  44. ModelsDev.defaultLayer,
  45. FSUtil.defaultLayer,
  46. Global.defaultLayer,
  47. ),
  48. ),
  49. ),
  50. ),
  51. )
  52. describe("LocationServiceMap", () => {
  53. it.effect("compares equivalent location refs by value", () =>
  54. Effect.sync(() => {
  55. const directory = AbsolutePath.make("/project")
  56. expect(Equal.equals(Location.Ref.make({ directory }), Location.Ref.make({ directory }))).toBe(true)
  57. expect(Hash.hash(Location.Ref.make({ directory }))).toBe(
  58. Hash.hash(Location.Ref.make({ directory, workspaceID: undefined })),
  59. )
  60. }),
  61. )
  62. it.live("isolates location state while sharing location policy with catalog", () =>
  63. Effect.acquireRelease(
  64. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  65. (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
  66. ).pipe(
  67. Effect.flatMap(([blocked, allowed]) =>
  68. Effect.gen(function* () {
  69. yield* (yield* ApplicationTools.Service).register({
  70. application_context: Tool.make({
  71. description: "Read application context",
  72. input: Schema.Struct({}),
  73. output: Schema.Struct({ ok: Schema.Boolean }),
  74. execute: () => Effect.succeed({ ok: true }),
  75. }),
  76. })
  77. yield* Effect.promise(() =>
  78. fs.writeFile(
  79. path.join(blocked.path, "opencode.json"),
  80. JSON.stringify({
  81. experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
  82. }),
  83. ),
  84. )
  85. const update = (directory: string) =>
  86. Effect.gen(function* () {
  87. yield* Reference.Service
  88. const catalog = yield* Catalog.Service
  89. yield* catalog.transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
  90. return {
  91. providers: yield* catalog.provider.all(),
  92. tools: yield* toolDefinitions(yield* ToolRegistry.Service),
  93. }
  94. }).pipe(
  95. Effect.scoped,
  96. Effect.provide(LocationServiceMap.get(Location.Ref.make({ directory: AbsolutePath.make(directory) }))),
  97. )
  98. const blockedState = yield* update(blocked.path)
  99. expect(blockedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(false)
  100. expect(blockedState.tools.map((tool) => tool.name).sort()).toEqual([
  101. "application_context",
  102. "apply_patch",
  103. "bash",
  104. "edit",
  105. "glob",
  106. "grep",
  107. "question",
  108. "read",
  109. "skill",
  110. "todowrite",
  111. "webfetch",
  112. "websearch",
  113. "write",
  114. ])
  115. const allowedState = yield* update(allowed.path)
  116. expect(allowedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(true)
  117. expect(allowedState.tools.map((tool) => tool.name).sort()).toEqual([
  118. "application_context",
  119. "apply_patch",
  120. "bash",
  121. "edit",
  122. "glob",
  123. "grep",
  124. "question",
  125. "read",
  126. "skill",
  127. "todowrite",
  128. "webfetch",
  129. "websearch",
  130. "write",
  131. ])
  132. }),
  133. ),
  134. ),
  135. )
  136. it.live("rejects an unavailable selected model during location model resolution", () =>
  137. Effect.acquireRelease(
  138. Effect.promise(() => tmpdir()),
  139. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  140. ).pipe(
  141. Effect.flatMap((dir) =>
  142. Effect.gen(function* () {
  143. const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
  144. yield* Effect.promise(() =>
  145. fs.writeFile(
  146. path.join(dir.path, "opencode.json"),
  147. JSON.stringify({
  148. providers: {
  149. unavailable: {
  150. name: "Unavailable",
  151. api: { type: "native", settings: {} },
  152. models: { chat: { disabled: true } },
  153. },
  154. },
  155. }),
  156. ),
  157. )
  158. const failure = yield* SessionRunnerModel.Service.use((models) =>
  159. models.resolve(
  160. SessionV2.Info.make({
  161. id: SessionV2.ID.make("ses_unavailable_model"),
  162. projectID: ProjectV2.ID.global,
  163. title: "test",
  164. model: {
  165. id: ModelV2.ID.make("chat"),
  166. providerID: ProviderV2.ID.make("unavailable"),
  167. },
  168. cost: 0,
  169. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  170. time: { created: DateTime.makeUnsafe(0), updated: DateTime.makeUnsafe(0) },
  171. location,
  172. }),
  173. ),
  174. ).pipe(Effect.provide(LocationServiceMap.get(location)), Effect.flip)
  175. expect(failure).toMatchObject({
  176. _tag: "SessionRunnerModel.ModelUnavailableError",
  177. providerID: "unavailable",
  178. modelID: "chat",
  179. })
  180. }),
  181. ),
  182. ),
  183. )
  184. it.live("installs public plugins into a location", () =>
  185. Effect.acquireRelease(
  186. Effect.promise(() => tmpdir()),
  187. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  188. ).pipe(
  189. Effect.flatMap((dir) =>
  190. Effect.gen(function* () {
  191. const plugins = yield* PluginV2.Service
  192. const reviewer = define({
  193. id: "reviewer",
  194. effect: (ctx) =>
  195. ctx.agent
  196. .transform((agent) => {
  197. agent.update("reviewer", (item) => {
  198. item.description = "Reviews code"
  199. item.mode = "subagent"
  200. })
  201. })
  202. .pipe(Effect.asVoid),
  203. })
  204. yield* plugins.add(PluginV2.ID.make(reviewer.id), reviewer.effect)
  205. expect(yield* (yield* AgentV2.Service).get(AgentV2.ID.make("reviewer"))).toMatchObject({
  206. description: "Reviews code",
  207. mode: "subagent",
  208. })
  209. }).pipe(
  210. Effect.scoped,
  211. Effect.provide(LocationServiceMap.get(Location.Ref.make({ directory: AbsolutePath.make(dir.path) }))),
  212. ),
  213. ),
  214. ),
  215. )
  216. })