diff --git a/bun.lock b/bun.lock index 1e3a6425..df79eb50 100644 --- a/bun.lock +++ b/bun.lock @@ -682,6 +682,7 @@ "@types/yargs": "17.0.33", "@typescript/native-preview": "catalog:", "drizzle-orm": "catalog:", + "fast-check": "4.10.2", "prettier": "3.6.2", "typescript": "catalog:", "vscode-languageserver-types": "3.17.5", @@ -3724,7 +3725,7 @@ "extsprintf": ["extsprintf@1.4.1", "", {}, "sha512-Wrk35e8ydCKDj/ArClo1VrPVmN8zph5V4AtHwIuHhvMXsKf73UT3BOD+azBIW+3wOJ4FhEH7zyaJCFvChjYvMA=="], - "fast-check": ["fast-check@4.8.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-GOJ158CUMnN6cSahsv4+ExARvIDuzzinFjkp0E9WtiBa5zcVeLozVkWaE4IzFcc+Y48Wp1EDlUZsXRyAztQcSg=="], + "fast-check": ["fast-check@4.10.2", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-iK2f+YrcmoeGqk6fA0ea2bptcu/itMIm4NfEozq6N25+aG6h7s5HZbB/k1aV7b5w5sFLMCbbtRUsTVR+BgC3xw=="], "fast-decode-uri-component": ["fast-decode-uri-component@1.0.1", "", {}, "sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg=="], @@ -6280,6 +6281,8 @@ "effect/@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="], + "effect/fast-check": ["fast-check@4.8.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-GOJ158CUMnN6cSahsv4+ExARvIDuzzinFjkp0E9WtiBa5zcVeLozVkWaE4IzFcc+Y48Wp1EDlUZsXRyAztQcSg=="], + "electron-builder/chalk": ["chalk@4.1.2", "", { "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" } }, "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA=="], "electron-builder/yargs": ["yargs@17.7.2", "", { "dependencies": { "cliui": "^8.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "require-directory": "^2.1.1", "string-width": "^4.2.3", "y18n": "^5.0.5", "yargs-parser": "^21.1.1" } }, "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w=="], @@ -6962,8 +6965,12 @@ "@standard-community/standard-json/effect/@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="], + "@standard-community/standard-json/effect/fast-check": ["fast-check@4.8.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-GOJ158CUMnN6cSahsv4+ExARvIDuzzinFjkp0E9WtiBa5zcVeLozVkWaE4IzFcc+Y48Wp1EDlUZsXRyAztQcSg=="], + "@standard-community/standard-openapi/effect/@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="], + "@standard-community/standard-openapi/effect/fast-check": ["fast-check@4.8.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-GOJ158CUMnN6cSahsv4+ExARvIDuzzinFjkp0E9WtiBa5zcVeLozVkWaE4IzFcc+Y48Wp1EDlUZsXRyAztQcSg=="], + "@storybook/csf-plugin/unplugin/acorn": ["acorn@8.16.0", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw=="], "@storybook/csf-plugin/unplugin/webpack-virtual-modules": ["webpack-virtual-modules@0.6.2", "", {}, "sha512-66/V2i5hQanC51vBQKPH4aI8NMAcBW59FVBs+rC7eGHupMyfn34q7rZIE+ETlJ+XTevqfUhVVBgSUNSW2flEUQ=="], diff --git a/packages/opencode/package.json b/packages/opencode/package.json index 5db88bf2..d11826ab 100644 --- a/packages/opencode/package.json +++ b/packages/opencode/package.json @@ -46,6 +46,7 @@ "@types/yargs": "17.0.33", "@typescript/native-preview": "catalog:", "drizzle-orm": "catalog:", + "fast-check": "4.10.2", "prettier": "3.6.2", "typescript": "catalog:", "vscode-languageserver-types": "3.17.5", diff --git a/packages/opencode/test/permission/permission.property.test.ts b/packages/opencode/test/permission/permission.property.test.ts new file mode 100644 index 00000000..048e819e --- /dev/null +++ b/packages/opencode/test/permission/permission.property.test.ts @@ -0,0 +1,76 @@ +import { test, expect } from "bun:test" +import fc from "fast-check" +import { Permission } from "../../src/permission" + +// R2: A command that matches no rule resolves to `ask`. +// Kind: invariant +// Generator: any permission name and any command string; the ruleset is empty, +// so no rule can match. fc.string() includes "", spaces, and symbols. +test("R2: no matching rule resolves to ask", () => { + fc.assert( + fc.property(fc.string(), fc.string(), (permission, command) => { + expect(Permission.evaluate(permission, command, []).action).toBe("ask") + }), + ) +}) + +// Shared generators: small value sets so rules often overlap, +// which is where "last match wins" can go wrong. +const action = fc.constantFrom("allow", "deny", "ask") +const rule = fc.record({ + permission: fc.constantFrom("bash", "edit", "*"), + pattern: fc.constantFrom("rm *", "git *", "*"), + action, +}) +const command = fc.constantFrom("rm -rf /", "git push", "ls", "") +// Hand-written match table: which commands each pattern matches. +const matches: Record = { + "rm *": ["rm -rf /"], + "git *": ["git push"], + "*": ["rm -rf /", "git push", "ls", ""], +} + +// R1: When a command matches several rules, the last matching rule in the config decides the action. +// Kind: model (reference = findLast over the hand-written match table) +test("R1: the last matching rule decides", () => { + fc.assert( + fc.property(fc.array(rule), command, (rules, cmd) => { + const expected = rules.findLast((r) => r.permission !== "edit" && matches[r.pattern].includes(cmd)) + expect(Permission.evaluate("bash", cmd, rules).action).toBe(expected?.action ?? "ask") + }), + ) +}) + +// R5: The result identifies the deciding rule: it is the matching rule from the config itself; +// when nothing matches, it is an `ask` rule with pattern `*`. +// Kind: invariant +test("R5: the result is the deciding rule itself", () => { + fc.assert( + fc.property(fc.array(rule), fc.string(), (rules, cmd) => { + const result = Permission.evaluate("bash", cmd, rules) + if (rules.some((r) => r === result)) return + expect(result).toEqual({ permission: "bash", pattern: "*", action: "ask" }) + }), + ) +}) + +// R6: A pattern ` *` matches the bare `` and `` followed by arguments, and nothing else. +// Kind: model (reference = cmd === "ls" || cmd.startsWith("ls ")) +// Generator: three layers. Hand-picked near-misses (lsof, LS, " ls"), structured +// command + flag + random tail, and fully random strings. Plain fc.string() almost never produces "ls". +const ls = fc.oneof( + fc.constantFrom("ls", "ls ", "ls -la", "lsof", "LS", " ls", ""), + fc + .tuple(fc.constantFrom("ls", "lsof", "cat"), fc.constantFrom("", " ", " -la"), fc.string()) + .map(([cmd, flag, rest]) => cmd + flag + rest), + fc.string(), +) +test("R6: `ls *` matches bare ls and ls with arguments only", () => { + fc.assert( + fc.property(ls, (cmd) => { + const expected = cmd === "ls" || cmd.startsWith("ls ") ? "deny" : "ask" + const rules = [{ permission: "bash", pattern: "ls *", action: "deny" as const }] + expect(Permission.evaluate("bash", cmd, rules).action).toBe(expected) + }), + ) +}) \ No newline at end of file diff --git a/traceability/joey.md b/traceability/joey.md new file mode 100644 index 00000000..acec98cf --- /dev/null +++ b/traceability/joey.md @@ -0,0 +1,27 @@ +# Traceability: Joey + +**Target feature:** opencode permission rules — `packages/opencode/src/permission/index.ts`(`evaluate`)、`src/util/wildcard.ts` + +> 練習用:需求來自 P3 學習時整理的權限規則行為,沒有實際訪談,所以 Interview evidence 欄寫「—」。 + +## Requirements + +| ID | Interview evidence | Requirement | Acceptance Criteria | Changed in P4? | Property (kind) | Test | +|---|---|---|---|---|---|---| +| R1 | — | When a command matches several rules, the last matching rule in the config decides the action. | `{"bash": {"*": "allow", "rm *": "deny"}}`: `rm -rf /` → `deny`, `ls -la` → `allow`. | No | Model | `permission.property.test.ts` › R1: the last matching rule decides | +| R2 | — | In `evaluate` with only the given rules, a command that matches no rule resolves to `ask`. (The app merges a built-in `"*": "allow"` default first, so at app level unmatched commands are `allow` unless the user sets a catch-all.) | Empty ruleset, any command → `ask`. | Yes (scope clarified) | Invariant | `permission.property.test.ts` › R2: no matching rule resolves to ask | +| R3 | — | A command whose last matching rule is `deny` is never executed. With the bash catch-all set to `ask` (`{"bash": {"*": "ask", "rm *": "deny"}}`), any command not allowed by a more specific rule needs user approval. | App level with that config: `find . -delete` → `ask`, `rm -rf /` → `deny`. | Yes (infeasible) | — (config recommendation; deny part covered by R1) | — | +| R4 | — | Merged into R1. | — | Yes (conflict) | — | — | +| R5 | — | The result identifies the deciding rule: it is the matching rule from the config itself; when nothing matches, it is an `ask` rule with pattern `*`. | `{"bash": {"rm *": "deny"}}`, `rm x` → returns `{permission: "bash", pattern: "rm *", action: "deny"}`. | Yes (uncheckable) | Invariant | `permission.property.test.ts` › R5: the result is the deciding rule itself | +| R6 | — | A pattern ` *` matches the bare `` and `` followed by arguments, and nothing else. | `{"bash": {"ls *": "deny"}}`: `ls` → `deny`, `ls -la` → `deny`, `lsof` → `ask`. | No | Model | `permission.property.test.ts` › R6: `ls *` matches bare ls and ls with arguments only | + +Properties written or suggested by opencode or another AI tool: none + +## Requirement changes + +| ID | Original requirement (P3) | Revised requirement | Why it changed | +|---|---|---|---| +| R3 | The agent can never delete important files. | A command whose last matching rule is `deny` is never executed. With the bash catch-all set to `ask` (`{"bash": {"*": "ask", "rm *": "deny"}}`), any command not allowed by a more specific rule needs user approval. | **Infeasible.** Matching is on the command string only; `find . -delete` is `allow` under `{"bash": {"*": "allow", "rm *": "deny"}}`, and the system cannot know which files are important. The need ("nothing deleted without my approval") is kept by the recommended config (verified at app level: `find . -delete` → `ask`). opencode's built-in default is `"*": "allow"`, so this is a config recommendation, not a default guarantee. | +| R2 | A command that matches no rule resolves to `ask`. | Scoped to `evaluate` with only the given rules. | **Infeasible as an app-level promise.** `src/agent/agent.ts` merges a built-in `"*": "allow"` before user rules, so at app level an unmatched command is `allow` (verified: empty user config, `git push` → `allow`). The property tests `evaluate` directly, where `ask` holds. | +| R4 | A more specific rule overrides a general one. | Merged into R1: config order decides, not specificity. | **Conflict** with R1. `{"bash": {"rm *": "deny", "*": "allow"}}` gives `rm -rf /` → `allow` (code uses `findLast`). Kept R1 because it matches the code and is checkable. The need (a deny should not be silently overridden) becomes a follow-up: warn when a deny is fully shadowed by a later rule. | +| R5 | Permission rules should be intuitive. | The result identifies the deciding rule (see R5 above). | **Uncheckable.** "Intuitive" has no pass/fail. The underlying need is knowing why a command was blocked, which can be checked on `evaluate`'s return value. | \ No newline at end of file