Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
76 changes: 76 additions & 0 deletions packages/opencode/test/permission/permission.property.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string[]> = {
"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 `<cmd> *` matches the bare `<cmd>` and `<cmd>` 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)
}),
)
})
27 changes: 27 additions & 0 deletions traceability/joey.md
Original file line number Diff line number Diff line change
@@ -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 `<cmd> *` matches the bare `<cmd>` and `<cmd>` 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. |
Loading