diff --git a/packages/core/src/tool/plugin/glob.ts b/packages/core/src/tool/plugin/glob.ts index ff3e20c4c99..e27d2e63ab4 100644 --- a/packages/core/src/tool/plugin/glob.ts +++ b/packages/core/src/tool/plugin/glob.ts @@ -6,7 +6,10 @@ import { Effect, Schema } from "effect" import path from "path" import { toolFailure } from "../runtime.js" import { ToolSearch } from "../search.js" +import { Config } from "../../config.js" +import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js" import { Environment } from "../../environment/index.js" +import { Flag } from "../../flag.js" import { FileSystem } from "../../filesystem.js" import { Location } from "../../location.js" import { FileAccess } from "../../file-access.js" @@ -52,9 +55,14 @@ export const Plugin = { const location = yield* Location.Service const access = yield* FileAccess.Service const permission = yield* Permission.Service + const config = yield* Config.Service + // Internal plugins are not re-run on config changes, so reload the tool transform instead. + const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, ctx.tool.reload()) + const flags = yield* Flag.Service yield* ctx.tool - .transform((editor) => + .transform((editor) => { + if (flags.resolve(loaded.entries).full_codemode.enabled) return editor.add({ name, options: { codemode: false }, @@ -111,8 +119,8 @@ export const Plugin = { : toolFailure(`Unable to find files matching ${input.pattern}`)(error), ), ), - }), - ) + }) + }) .pipe(Effect.orDie) }), } diff --git a/packages/core/src/tool/plugin/grep.ts b/packages/core/src/tool/plugin/grep.ts index 6caebdc01c0..31468559419 100644 --- a/packages/core/src/tool/plugin/grep.ts +++ b/packages/core/src/tool/plugin/grep.ts @@ -4,7 +4,10 @@ import type { Context } from "@opencode/plugin/effect/plugin" import { ToolFailure } from "@opencode/ai/effect" import { Effect, Schema } from "effect" import path from "path" +import { Config } from "../../config.js" +import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js" import { Environment } from "../../environment/index.js" +import { Flag } from "../../flag.js" import { FileSystem } from "../../filesystem.js" import { Location } from "../../location.js" import { FileAccess } from "../../file-access.js" @@ -70,9 +73,14 @@ export const Plugin = { const location = yield* Location.Service const access = yield* FileAccess.Service const permission = yield* Permission.Service + const config = yield* Config.Service + // Internal plugins are not re-run on config changes, so reload the tool transform instead. + const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, ctx.tool.reload()) + const flags = yield* Flag.Service yield* ctx.tool - .transform((editor) => + .transform((editor) => { + if (flags.resolve(loaded.entries).full_codemode.enabled) return editor.add({ name, options: { codemode: false }, @@ -145,8 +153,8 @@ export const Plugin = { return new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error }) }), ), - }), - ) + }) + }) .pipe(Effect.orDie) }), } diff --git a/packages/core/test/tool-search.test.ts b/packages/core/test/tool-search.test.ts index 0f3e5bda595..d25a8c3c9b2 100644 --- a/packages/core/test/tool-search.test.ts +++ b/packages/core/test/tool-search.test.ts @@ -1,12 +1,17 @@ import { describe, expect } from "bun:test" import fs from "fs/promises" import path from "path" -import { Effect, Layer } from "effect" +import { Context, Effect, Layer, PubSub, Stream } from "effect" +import { Document, Info, type Entry } from "@opencode/schema/config" +import { Event } from "@opencode/schema/event" +import { ConfigExperimental } from "@opencode/schema/config/experimental" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Config } from "@opencode/core/config" import { makeLocationNode } from "@opencode/util/effect/app-node" import { LayerNode } from "@opencode/util/effect/layer-node" import { Environment } from "@opencode/core/environment/index" import { FileSystem } from "@opencode/core/filesystem" +import { Flag } from "@opencode/core/flag" import { Location } from "@opencode/core/location" import { FileAccess } from "@opencode/core/file-access" import { Permission } from "@opencode/core/permission" @@ -20,45 +25,63 @@ import { location } from "./fixture/location" import { tmpdir, tmpdirScoped, withTempDir } from "./fixture/tmpdir" import { it } from "./lib/effect" import { permissionLayer } from "./lib/permission" -import { executeTool, registerToolPlugin, toolIdentity } from "./lib/tool" +import { codeModeListings, executeTool, registerToolPlugin, toolIdentity, waitForTool } from "./lib/tool" -const globToolNode = makeLocationNode({ - name: "test/glob-tool-plugin", - layer: Layer.effectDiscard(registerToolPlugin(GlobTool.Plugin)), - deps: [Tool.node, Environment.node, Ripgrep.node, Location.node, FileAccess.node, Permission.node], -}) -const grepToolNode = makeLocationNode({ - name: "test/grep-tool-plugin", - layer: Layer.effectDiscard(registerToolPlugin(GrepTool.Plugin)), - deps: [Tool.node, Environment.node, Ripgrep.node, Location.node, FileAccess.node, Permission.node], -}) const sessionID = Session.ID.make("ses_search_tool_test") +const configUpdated = () => ({ id: Event.ID.create(), created: Date.now(), type: "config.updated" as const, data: {} }) const withTools = ( directory: string, - body: (registry: Tool.Interface) => Effect.Effect, + body: (registry: Tool.Interface, configure: (entries: Entry[]) => Effect.Effect) => Effect.Effect, assertions?: Permission.AssertInput[], + entries: Entry[] = [], ) => Effect.gen(function* () { - const registry = yield* Tool.Service - return yield* body(registry) - }).pipe( - Effect.provide( - AppNodeBuilder.build(LayerNode.group([Tool.node, globToolNode, grepToolNode]), [ - Location.node.replace( - Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), - ), - Permission.node.replace( - permissionLayer({ - assert: (input) => - Effect.sync(() => { - assertions?.push(input) - }), - }), - ), - ]), - ), - ) + const config = yield* Layer.build(Config.testLayer(entries)) + const updates = yield* PubSub.unbounded>() + const event = { subscribe: () => Stream.fromPubSub(updates) } + const configure = (next: Entry[]) => + Context.get(config, Config.Test) + .setEntries(next) + .pipe(Effect.andThen(PubSub.publish(updates, configUpdated())), Effect.asVoid) + const searchToolsNode = makeLocationNode({ + name: "test/search-tool-plugins", + layer: Layer.effectDiscard( + Effect.all([registerToolPlugin(GlobTool.Plugin, { event }), registerToolPlugin(GrepTool.Plugin, { event })]), + ), + deps: [ + Config.node, + Environment.node, + FileAccess.node, + Flag.node, + Location.node, + Permission.node, + Ripgrep.node, + Tool.node, + ], + }) + return yield* Effect.gen(function* () { + const registry = yield* Tool.Service + return yield* body(registry, configure) + }).pipe( + Effect.provide( + AppNodeBuilder.build(LayerNode.group([Tool.node, searchToolsNode]), [ + Config.node.replace(Layer.succeedContext(config)), + Location.node.replace( + Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), + ), + Permission.node.replace( + permissionLayer({ + assert: (input) => + Effect.sync(() => { + assertions?.push(input) + }), + }), + ), + ]), + ), + ) + }).pipe(Effect.scoped) const call = (name: "glob" | "grep", input: unknown) => ({ sessionID, @@ -66,7 +89,44 @@ const call = (name: "glob" | "grep", input: unknown) => ({ call: { type: "tool-call" as const, id: `call-${name}`, name, input }, }) +const fullCodeMode = new Document({ + type: "document", + info: new Info({ experimental: new ConfigExperimental.Info({ full_codemode: true }) }), +}) + +const exposed = (registry: Tool.Interface) => + registry + .snapshot() + .pipe( + Effect.map((snapshot) => [ + ...snapshot.definitions.map((tool) => tool.name), + ...(snapshot.codeModeCatalog ? codeModeListings(snapshot.codeModeCatalog).map((entry) => entry.path) : []), + ]), + ) + describe("search tools", () => { + it.live("are absent with full_codemode and return when it is turned off", () => + withTempDir((tmp) => + withTools( + tmp.path, + (registry, configure) => + Effect.gen(function* () { + const names = yield* exposed(registry) + expect(names).not.toContain("glob") + expect(names).not.toContain("grep") + expect(yield* executeTool(registry, call("grep", { pattern: "needle" }))).toMatchObject({ status: "error" }) + + yield* configure([]) + yield* waitForTool(registry, "grep") + yield* waitForTool(registry, "glob") + expect(yield* exposed(registry)).toEqual(expect.arrayContaining(["glob", "grep"])) + }), + undefined, + [fullCodeMode], + ), + ), + ) + for (const name of ["glob", "grep"] as const) { it.live(`${name} distinguishes a malformed filename glob from an empty search`, () => withTempDir((tmp) =>