`!`…`` in a command file ran through bash -c with the arguments pasted in and only the hardline checked — for a prompt from the link too, before any mode applied. It is now judged as a bash call of the line in the session's mode and rules and runs only on allow; the arguments are positional parameters and ARGUMENTS in the environment; placeholders are filled in one pass; a substitution has the bash tool's environment and a 10 s limit. Spec: permission/rules.md §6. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
144 lines
7.6 KiB
TypeScript
144 lines
7.6 KiB
TypeScript
import { afterAll, describe, expect, test } from "bun:test"
|
|
import { existsSync, mkdirSync, mkdtempSync, writeFileSync, rmSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { paths } from "../src/config/paths.ts"
|
|
import { DEFAULT_RULES, evaluate, toRules } from "../src/permission/evaluate.ts"
|
|
import { hardlineRules } from "../src/permission/hardline.ts"
|
|
import { attachmentsFor } from "../src/project/attach.ts"
|
|
import { customCommands, expandCommand } from "../src/project/commands.ts"
|
|
import { FileIndex } from "../src/project/files.ts"
|
|
import { findProject, projectDirName } from "../src/project/root.ts"
|
|
|
|
// The whole suite shares one config directory: what this file puts under commands/ is taken away
|
|
// again, or every later start-up warns about it.
|
|
afterAll(() => rmSync(join(paths.config, "commands"), { recursive: true, force: true }))
|
|
|
|
function tree() {
|
|
const root = mkdtempSync(join(tmpdir(), "ph-proj-"))
|
|
mkdirSync(join(root, "src/deep"), { recursive: true })
|
|
writeFileSync(join(root, "src/app.ts"), Array.from({ length: 30 }, (_, i) => `line ${i + 1}`).join("\n") + "\n")
|
|
writeFileSync(join(root, "src/deep/util.ts"), "export {}\n")
|
|
writeFileSync(join(root, "README.md"), "# hi\n")
|
|
return root
|
|
}
|
|
|
|
describe("@ attachments", () => {
|
|
test("a file, a line range, a directory; an email is left alone; attached files count as read", () => {
|
|
const root = tree()
|
|
const ctx = { root, cwd: root, readFiles: new Set<string>(), fileStamps: new Map() }
|
|
const atts = attachmentsFor("look at @src/app.ts and @src/app.ts#3-5 and @src/ — mail me@example.com", ctx)
|
|
expect(atts.map((a) => a.path)).toEqual(["src/app.ts", "src/app.ts", "src/"])
|
|
expect(atts[0]!.text).toStartWith('<file path="src/app.ts">\n1: line 1')
|
|
expect(atts[1]!.text).toBe('<file path="src/app.ts" lines="3-5">\n3: line 3\n4: line 4\n5: line 5\n</file>')
|
|
expect(atts[2]!.text).toBe('<directory path="src/">\napp.ts\ndeep/\n</directory>')
|
|
expect(ctx.readFiles.has(join(root, "src/app.ts"))).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe("a custom command's !`…` is a bash call of the session (rules.md §6)", () => {
|
|
const cmd = (body: string) => ({ name: "x", description: "", body, source: "project" as const, file: "/x.md" })
|
|
const judgeIn = (mode: "manual" | "plan" | "auto", root: string) => (line: string) =>
|
|
evaluate({ permission: "bash", class: "execute", patterns: [line], command: line, paths: [root] }, { mode, rules: toRules(DEFAULT_RULES, "default"), hardline: hardlineRules(), root, branch: () => undefined })
|
|
|
|
test("the arguments are never part of the command line", () => {
|
|
const root = tree()
|
|
const marker = join(root, "ran")
|
|
const body = "Log: !`printf '%s|' $ARGUMENTS \"$1\" \"$2\"`"
|
|
const out = expandCommand(cmd(body), `a; touch ${marker}`, root, hardlineRules(), judgeIn("auto", root))
|
|
expect(existsSync(marker)).toBe(false)
|
|
expect(out).toBe(`Log: a;|touch|${marker}|a;|touch|`)
|
|
})
|
|
|
|
test("manual mode: what bash would ask about is not run, and the model is told", () => {
|
|
const root = tree()
|
|
const marker = join(root, "ran")
|
|
const out = expandCommand(cmd(`A: !\`touch ${marker}\` B: !\`pwd\``), "", root, hardlineRules(), judgeIn("manual", root))
|
|
expect(existsSync(marker)).toBe(false)
|
|
expect(out).toContain("needs the person's approval here")
|
|
expect(out).toContain(`B: ${root}`)
|
|
})
|
|
|
|
test("an allowed command with an argument in it is not known safe: not run in manual mode", () => {
|
|
const root = tree()
|
|
const out = expandCommand(cmd("!`cat $1`"), ".env", root, hardlineRules(), judgeIn("manual", root))
|
|
expect(out).toContain("needs the person's approval here")
|
|
})
|
|
|
|
test("plan mode (a remote session limited to it) runs nothing but what the rules allow", () => {
|
|
const root = tree()
|
|
const marker = join(root, "ran")
|
|
const out = expandCommand(cmd(`!\`touch ${marker}\``), "", root, hardlineRules(), judgeIn("plan", root))
|
|
expect(existsSync(marker)).toBe(false)
|
|
expect(out).toContain("[not run — plan mode")
|
|
})
|
|
|
|
test("the floor is checked with the arguments in place too", () => {
|
|
const root = tree()
|
|
expect(expandCommand(cmd("!`rm -rf $1`"), "/", root, hardlineRules(), judgeIn("auto", root))).toContain("[not run — recursive delete of the root filesystem]")
|
|
})
|
|
|
|
test("a $ in the arguments is text: one pass", () => {
|
|
const root = tree()
|
|
expect(expandCommand(cmd("Say: $ARGUMENTS (first: $1)"), "costs $$5 and $& and $1", root, hardlineRules(), judgeIn("manual", root))).toBe("Say: costs $$5 and $& and $1 (first: costs)")
|
|
})
|
|
})
|
|
|
|
describe("custom commands", () => {
|
|
test("global and project, the project overriding; $ARGUMENTS, $1, !`cmd`, and the hardline", () => {
|
|
const root = tree()
|
|
mkdirSync(join(paths.config, "commands"), { recursive: true })
|
|
writeFileSync(join(paths.config, "commands", "review.md"), "---\ndescription: review a file\n---\nReview $1 carefully. All: $ARGUMENTS\nBranch: !`echo main`\n")
|
|
writeFileSync(join(paths.config, "commands", "danger.md"), "Here: !`rm -rf /`\n")
|
|
const pdir = join(root, ".agent")
|
|
mkdirSync(join(pdir, "commands"), { recursive: true })
|
|
writeFileSync(join(pdir, "commands", "danger.md"), "---\ndescription: project one\n---\nsafe\n")
|
|
const cmds = customCommands(pdir)
|
|
const review = cmds.find((c) => c.name === "review")!
|
|
expect(review.description).toBe("review a file")
|
|
expect(cmds.find((c) => c.name === "danger")!.source).toBe("project")
|
|
expect(expandCommand(review, "src/app.ts quickly", root, hardlineRules(), () => ({ action: "allow", reason: "" }))).toBe("Review src/app.ts carefully. All: src/app.ts quickly\nBranch: main")
|
|
const globalDanger = customCommands(undefined).find((c) => c.name === "danger")!
|
|
expect(expandCommand(globalDanger, "", root, hardlineRules(), () => ({ action: "allow", reason: "" }))).toContain("[not run — recursive delete of the root filesystem]")
|
|
})
|
|
})
|
|
|
|
describe("file index", () => {
|
|
test("fuzzy match, and a trailing / lists one level", () => {
|
|
const idx = new FileIndex(tree())
|
|
expect(idx.search("utl")[0]).toBe("src/deep/util.ts")
|
|
expect(idx.search("src/")).toEqual(["src/deep/", "src/app.ts"])
|
|
})
|
|
})
|
|
|
|
describe("the project directory", () => {
|
|
test("a new project gets .agent at the git root; an empty .agent is where it goes", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "lembas-proj-"))
|
|
mkdirSync(join(root, ".git"))
|
|
mkdirSync(join(root, "src"))
|
|
expect(findProject(join(root, "src"))).toMatchObject({ root, dir: join(root, ".agent"), exists: false })
|
|
mkdirSync(join(root, ".agent"))
|
|
expect(projectDirName(root)).toBe(".agent")
|
|
})
|
|
|
|
test("another tool's .agent is not a project: a new one is .lembas, not inside it", () => {
|
|
const theirs = mkdtempSync(join(tmpdir(), "lembas-proj-"))
|
|
mkdirSync(join(theirs, ".git"))
|
|
mkdirSync(join(theirs, ".agent", "rules"), { recursive: true })
|
|
expect(findProject(theirs)).toMatchObject({ root: theirs, dir: join(theirs, ".lembas"), exists: false })
|
|
mkdirSync(join(theirs, ".lembas"))
|
|
expect(findProject(theirs)).toMatchObject({ dir: join(theirs, ".lembas"), exists: true })
|
|
})
|
|
|
|
test("our own .agent is found by any one of its markers", () => {
|
|
for (const marker of ["config.yaml", "plans", "tasks.md", "local"]) {
|
|
const mine = mkdtempSync(join(tmpdir(), "lembas-proj-"))
|
|
if (marker.includes(".")) {
|
|
mkdirSync(join(mine, ".agent"))
|
|
writeFileSync(join(mine, ".agent", marker), "")
|
|
} else mkdirSync(join(mine, ".agent", marker), { recursive: true })
|
|
expect(findProject(mine)).toMatchObject({ dir: join(mine, ".agent"), exists: true })
|
|
}
|
|
})
|
|
})
|