refactor(ripgrep): use embedded wasm backend (#21703)
This commit is contained in:
@@ -6,6 +6,21 @@ import path from "path"
|
||||
import { tmpdir } from "../fixture/fixture"
|
||||
import { Ripgrep } from "../../src/file/ripgrep"
|
||||
|
||||
async function seed(dir: string, count: number, size = 16) {
|
||||
const txt = "a".repeat(size)
|
||||
await Promise.all(Array.from({ length: count }, (_, i) => Bun.write(path.join(dir, `file-${i}.txt`), `${txt}${i}\n`)))
|
||||
}
|
||||
|
||||
function env(name: string, value: string | undefined) {
|
||||
const prev = process.env[name]
|
||||
if (value === undefined) delete process.env[name]
|
||||
else process.env[name] = value
|
||||
return () => {
|
||||
if (prev === undefined) delete process.env[name]
|
||||
else process.env[name] = prev
|
||||
}
|
||||
}
|
||||
|
||||
describe("file.ripgrep", () => {
|
||||
test("defaults to include hidden", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
@@ -16,11 +31,9 @@ describe("file.ripgrep", () => {
|
||||
},
|
||||
})
|
||||
|
||||
const files = await Array.fromAsync(Ripgrep.files({ cwd: tmp.path }))
|
||||
const hasVisible = files.includes("visible.txt")
|
||||
const hasHidden = files.includes(path.join(".opencode", "thing.json"))
|
||||
expect(hasVisible).toBe(true)
|
||||
expect(hasHidden).toBe(true)
|
||||
const files = await Array.fromAsync(await Ripgrep.files({ cwd: tmp.path }))
|
||||
expect(files.includes("visible.txt")).toBe(true)
|
||||
expect(files.includes(path.join(".opencode", "thing.json"))).toBe(true)
|
||||
})
|
||||
|
||||
test("hidden false excludes hidden", async () => {
|
||||
@@ -32,15 +45,11 @@ describe("file.ripgrep", () => {
|
||||
},
|
||||
})
|
||||
|
||||
const files = await Array.fromAsync(Ripgrep.files({ cwd: tmp.path, hidden: false }))
|
||||
const hasVisible = files.includes("visible.txt")
|
||||
const hasHidden = files.includes(path.join(".opencode", "thing.json"))
|
||||
expect(hasVisible).toBe(true)
|
||||
expect(hasHidden).toBe(false)
|
||||
const files = await Array.fromAsync(await Ripgrep.files({ cwd: tmp.path, hidden: false }))
|
||||
expect(files.includes("visible.txt")).toBe(true)
|
||||
expect(files.includes(path.join(".opencode", "thing.json"))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Ripgrep.Service", () => {
|
||||
test("search returns empty when nothing matches", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
@@ -48,15 +57,119 @@ describe("Ripgrep.Service", () => {
|
||||
},
|
||||
})
|
||||
|
||||
const result = await Effect.gen(function* () {
|
||||
const rg = yield* Ripgrep.Service
|
||||
return yield* rg.search({ cwd: tmp.path, pattern: "needle" })
|
||||
}).pipe(Effect.provide(Ripgrep.defaultLayer), Effect.runPromise)
|
||||
|
||||
const result = await Ripgrep.search({ cwd: tmp.path, pattern: "needle" })
|
||||
expect(result.partial).toBe(false)
|
||||
expect(result.items).toEqual([])
|
||||
})
|
||||
|
||||
test("search returns match metadata with normalized path", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await fs.mkdir(path.join(dir, "src"), { recursive: true })
|
||||
await Bun.write(path.join(dir, "src", "match.ts"), "const needle = 1\n")
|
||||
},
|
||||
})
|
||||
|
||||
const result = await Ripgrep.search({ cwd: tmp.path, pattern: "needle" })
|
||||
expect(result.partial).toBe(false)
|
||||
expect(result.items).toHaveLength(1)
|
||||
expect(result.items[0]?.path.text).toBe(path.join("src", "match.ts"))
|
||||
expect(result.items[0]?.line_number).toBe(1)
|
||||
expect(result.items[0]?.lines.text).toContain("needle")
|
||||
})
|
||||
|
||||
test("files returns empty when glob matches no files in worker mode", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await fs.mkdir(path.join(dir, "packages", "console"), { recursive: true })
|
||||
await Bun.write(path.join(dir, "packages", "console", "package.json"), "{}")
|
||||
},
|
||||
})
|
||||
|
||||
const ctl = new AbortController()
|
||||
const files = await Array.fromAsync(
|
||||
await Ripgrep.files({
|
||||
cwd: tmp.path,
|
||||
glob: ["packages/*"],
|
||||
signal: ctl.signal,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(files).toEqual([])
|
||||
})
|
||||
|
||||
test("ignores RIPGREP_CONFIG_PATH in direct mode", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await Bun.write(path.join(dir, "match.ts"), "const needle = 1\n")
|
||||
},
|
||||
})
|
||||
|
||||
const restore = env("RIPGREP_CONFIG_PATH", path.join(tmp.path, "missing-ripgreprc"))
|
||||
try {
|
||||
const result = await Ripgrep.search({ cwd: tmp.path, pattern: "needle" })
|
||||
expect(result.items).toHaveLength(1)
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test("ignores RIPGREP_CONFIG_PATH in worker mode", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await Bun.write(path.join(dir, "match.ts"), "const needle = 1\n")
|
||||
},
|
||||
})
|
||||
|
||||
const restore = env("RIPGREP_CONFIG_PATH", path.join(tmp.path, "missing-ripgreprc"))
|
||||
try {
|
||||
const ctl = new AbortController()
|
||||
const result = await Ripgrep.search({
|
||||
cwd: tmp.path,
|
||||
pattern: "needle",
|
||||
signal: ctl.signal,
|
||||
})
|
||||
expect(result.items).toHaveLength(1)
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test("aborts files scan in worker mode", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await seed(dir, 4000)
|
||||
},
|
||||
})
|
||||
|
||||
const ctl = new AbortController()
|
||||
const iter = await Ripgrep.files({ cwd: tmp.path, signal: ctl.signal })
|
||||
const pending = Array.fromAsync(iter)
|
||||
setTimeout(() => ctl.abort(), 0)
|
||||
|
||||
const err = await pending.catch((err) => err)
|
||||
expect(err).toBeInstanceOf(Error)
|
||||
expect(err.name).toBe("AbortError")
|
||||
}, 15_000)
|
||||
|
||||
test("aborts search in worker mode", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
await seed(dir, 512, 64 * 1024)
|
||||
},
|
||||
})
|
||||
|
||||
const ctl = new AbortController()
|
||||
const pending = Ripgrep.search({ cwd: tmp.path, pattern: "needle", signal: ctl.signal })
|
||||
setTimeout(() => ctl.abort(), 0)
|
||||
|
||||
const err = await pending.catch((err) => err)
|
||||
expect(err).toBeInstanceOf(Error)
|
||||
expect(err.name).toBe("AbortError")
|
||||
}, 15_000)
|
||||
})
|
||||
|
||||
describe("Ripgrep.Service", () => {
|
||||
test("search returns matched rows", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
init: async (dir) => {
|
||||
|
||||
@@ -32,18 +32,18 @@ const ctx = {
|
||||
ask: () => Effect.void,
|
||||
}
|
||||
|
||||
const projectRoot = path.join(__dirname, "../..")
|
||||
const root = path.join(__dirname, "../..")
|
||||
|
||||
describe("tool.grep", () => {
|
||||
it.live("basic search", () =>
|
||||
Effect.gen(function* () {
|
||||
const info = yield* GrepTool
|
||||
const grep = yield* info.init()
|
||||
const result = yield* provideInstance(projectRoot)(
|
||||
const result = yield* provideInstance(root)(
|
||||
grep.execute(
|
||||
{
|
||||
pattern: "export",
|
||||
path: path.join(projectRoot, "src/tool"),
|
||||
path: path.join(root, "src/tool"),
|
||||
include: "*.ts",
|
||||
},
|
||||
ctx,
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import { Effect, Layer, ManagedRuntime } from "effect"
|
||||
import { Agent } from "../../src/agent/agent"
|
||||
import { Skill } from "../../src/skill"
|
||||
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
|
||||
import { Ripgrep } from "../../src/file/ripgrep"
|
||||
import { Truncate } from "../../src/tool/truncate"
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { afterEach, describe, expect } from "bun:test"
|
||||
import path from "path"
|
||||
import { pathToFileURL } from "url"
|
||||
import type { Permission } from "../../src/permission"
|
||||
@@ -12,7 +8,7 @@ import type { Tool } from "../../src/tool/tool"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { SkillTool } from "../../src/tool/skill"
|
||||
import { ToolRegistry } from "../../src/tool/registry"
|
||||
import { provideTmpdirInstance, tmpdir } from "../fixture/fixture"
|
||||
import { provideTmpdirInstance } from "../fixture/fixture"
|
||||
import { SessionID, MessageID } from "../../src/session/schema"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
@@ -131,14 +127,15 @@ description: ${description}
|
||||
),
|
||||
)
|
||||
|
||||
test("execute returns skill content block with files", async () => {
|
||||
await using tmp = await tmpdir({
|
||||
git: true,
|
||||
init: async (dir) => {
|
||||
const skillDir = path.join(dir, ".opencode", "skill", "tool-skill")
|
||||
await Bun.write(
|
||||
path.join(skillDir, "SKILL.md"),
|
||||
`---
|
||||
it.live("execute returns skill content block with files", () =>
|
||||
provideTmpdirInstance(
|
||||
(dir) =>
|
||||
Effect.gen(function* () {
|
||||
const skill = path.join(dir, ".opencode", "skill", "tool-skill")
|
||||
yield* Effect.promise(() =>
|
||||
Bun.write(
|
||||
path.join(skill, "SKILL.md"),
|
||||
`---
|
||||
name: tool-skill
|
||||
description: Skill for tool tests.
|
||||
---
|
||||
@@ -147,23 +144,27 @@ description: Skill for tool tests.
|
||||
|
||||
Use this skill.
|
||||
`,
|
||||
)
|
||||
await Bun.write(path.join(skillDir, "scripts", "demo.txt"), "demo")
|
||||
},
|
||||
})
|
||||
|
||||
const home = process.env.OPENCODE_TEST_HOME
|
||||
process.env.OPENCODE_TEST_HOME = tmp.path
|
||||
|
||||
try {
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const runtime = ManagedRuntime.make(
|
||||
Layer.mergeAll(Skill.defaultLayer, Ripgrep.defaultLayer, Truncate.defaultLayer, Agent.defaultLayer),
|
||||
),
|
||||
)
|
||||
const info = await runtime.runPromise(SkillTool)
|
||||
const tool = await runtime.runPromise(info.init())
|
||||
yield* Effect.promise(() => Bun.write(path.join(skill, "scripts", "demo.txt"), "demo"))
|
||||
|
||||
const home = process.env.OPENCODE_TEST_HOME
|
||||
process.env.OPENCODE_TEST_HOME = dir
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.sync(() => {
|
||||
process.env.OPENCODE_TEST_HOME = home
|
||||
}),
|
||||
)
|
||||
|
||||
const registry = yield* ToolRegistry.Service
|
||||
const agent = { name: "build", mode: "primary" as const, permission: [], options: {} }
|
||||
const tool = (yield* registry.tools({
|
||||
providerID: "opencode" as any,
|
||||
modelID: "gpt-5" as any,
|
||||
agent,
|
||||
})).find((tool) => tool.id === SkillTool.id)
|
||||
if (!tool) throw new Error("Skill tool not found")
|
||||
|
||||
const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
|
||||
const ctx: Tool.Context = {
|
||||
...baseCtx,
|
||||
@@ -173,23 +174,19 @@ Use this skill.
|
||||
}),
|
||||
}
|
||||
|
||||
const result = await runtime.runPromise(tool.execute({ name: "tool-skill" }, ctx))
|
||||
const dir = path.join(tmp.path, ".opencode", "skill", "tool-skill")
|
||||
const file = path.resolve(dir, "scripts", "demo.txt")
|
||||
const result = yield* tool.execute({ name: "tool-skill" }, ctx)
|
||||
const file = path.resolve(skill, "scripts", "demo.txt")
|
||||
|
||||
expect(requests.length).toBe(1)
|
||||
expect(requests[0].permission).toBe("skill")
|
||||
expect(requests[0].patterns).toContain("tool-skill")
|
||||
expect(requests[0].always).toContain("tool-skill")
|
||||
|
||||
expect(result.metadata.dir).toBe(dir)
|
||||
expect(result.metadata.dir).toBe(skill)
|
||||
expect(result.output).toContain(`<skill_content name="tool-skill">`)
|
||||
expect(result.output).toContain(`Base directory for this skill: ${pathToFileURL(dir).href}`)
|
||||
expect(result.output).toContain(`Base directory for this skill: ${pathToFileURL(skill).href}`)
|
||||
expect(result.output).toContain(`<file>${file}</file>`)
|
||||
},
|
||||
})
|
||||
} finally {
|
||||
process.env.OPENCODE_TEST_HOME = home
|
||||
}
|
||||
})
|
||||
}),
|
||||
{ git: true },
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user