+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
157 lines
4.9 KiB
TypeScript
157 lines
4.9 KiB
TypeScript
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { describe, expect } from "bun:test"
|
|
import path from "path"
|
|
import { Effect } from "effect"
|
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|
import type { Tool } from "@/tool/tool"
|
|
import { assertExternalDirectoryEffect } from "../../src/tool/external-directory"
|
|
import { Filesystem } from "@/util/filesystem"
|
|
import { TestInstance, tmpdirScoped } from "../fixture/fixture"
|
|
import type { Permission } from "../../src/permission"
|
|
import { SessionID, MessageID } from "../../src/session/schema"
|
|
import { testEffect } from "../lib/effect"
|
|
|
|
const it = testEffect(LayerNode.compile(CrossSpawnSpawner.node))
|
|
|
|
const baseCtx: Omit<Tool.Context, "ask"> = {
|
|
sessionID: SessionID.make("ses_test"),
|
|
messageID: MessageID.make("msg_test"),
|
|
callID: "",
|
|
agent: "build",
|
|
abort: AbortSignal.any([]),
|
|
messages: [],
|
|
metadata: () => Effect.void,
|
|
}
|
|
|
|
const glob = (p: string) =>
|
|
process.platform === "win32" ? Filesystem.normalizePathPattern(p) : p.replaceAll("\\", "/")
|
|
|
|
function makeCtx() {
|
|
const requests: Array<Omit<PermissionV1.Request, "id" | "sessionID" | "tool">> = []
|
|
const ctx: Tool.Context = {
|
|
...baseCtx,
|
|
ask: (req) =>
|
|
Effect.sync(() => {
|
|
requests.push(req)
|
|
}),
|
|
}
|
|
return { requests, ctx }
|
|
}
|
|
|
|
describe("tool.assertExternalDirectory", () => {
|
|
it.live("no-ops for empty target", () =>
|
|
Effect.gen(function* () {
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
yield* assertExternalDirectoryEffect(ctx)
|
|
|
|
expect(requests.length).toBe(0)
|
|
}),
|
|
)
|
|
|
|
it.instance("no-ops for paths inside the instance directory", () =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, path.join(test.directory, "file.txt"))
|
|
|
|
expect(requests.length).toBe(0)
|
|
}),
|
|
)
|
|
|
|
it.instance("asks with a single canonical glob", () =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
const target = path.join(path.dirname(test.directory), "outside", "file.txt")
|
|
const expected = glob(path.join(path.dirname(target), "*"))
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, target)
|
|
|
|
const req = requests.find((r) => r.permission === "external_directory")
|
|
expect(req).toBeDefined()
|
|
expect(req!.patterns).toEqual([expected])
|
|
expect(req!.always).toEqual([expected])
|
|
}),
|
|
)
|
|
|
|
it.instance("uses target directory when kind=directory", () =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
const target = path.join(path.dirname(test.directory), "outside")
|
|
const expected = glob(path.join(target, "*"))
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, target, { kind: "directory" })
|
|
|
|
const req = requests.find((r) => r.permission === "external_directory")
|
|
expect(req).toBeDefined()
|
|
expect(req!.patterns).toEqual([expected])
|
|
expect(req!.always).toEqual([expected])
|
|
}),
|
|
)
|
|
|
|
it.live("skips prompting when bypass=true", () =>
|
|
Effect.gen(function* () {
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, "/tmp/outside/file.txt", { bypass: true })
|
|
|
|
expect(requests.length).toBe(0)
|
|
}),
|
|
)
|
|
|
|
if (process.platform === "win32") {
|
|
it.instance(
|
|
"normalizes Windows path variants to one glob",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
const outerTmp = yield* tmpdirScoped()
|
|
yield* Effect.promise(() => Bun.write(path.join(outerTmp, "outside.txt"), "x"))
|
|
|
|
const target = path.join(outerTmp, "outside.txt")
|
|
const alt = target
|
|
.replace(/^[A-Za-z]:/, "")
|
|
.replaceAll("\\", "/")
|
|
.toLowerCase()
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, alt)
|
|
|
|
const req = requests.find((r) => r.permission === "external_directory")
|
|
const expected = glob(path.join(outerTmp, "*"))
|
|
expect(req).toBeDefined()
|
|
expect(req!.patterns).toEqual([expected])
|
|
expect(req!.always).toEqual([expected])
|
|
}),
|
|
{ git: true },
|
|
)
|
|
|
|
it.instance(
|
|
"uses drive root glob for root files",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const { requests, ctx } = makeCtx()
|
|
|
|
const tmp = yield* TestInstance
|
|
const root = path.parse(tmp.directory).root
|
|
const target = path.join(root, "boot.ini")
|
|
|
|
yield* assertExternalDirectoryEffect(ctx, target)
|
|
|
|
const req = requests.find((r) => r.permission === "external_directory")
|
|
const expected = path.join(root, "*")
|
|
expect(req).toBeDefined()
|
|
expect(req!.patterns).toEqual([expected])
|
|
expect(req!.always).toEqual([expected])
|
|
}),
|
|
{ git: true },
|
|
)
|
|
}
|
|
})
|