Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(opencode): record every path a shell rm deletes (#1056)

Rudy Mizrahi Celekli69b5d21db044
diff --git a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
index 5bcc4e8f..85fc79ed 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
@@ -126,11 +126,26 @@ function shellMutationPaths(tool, args) {
if (name !== "bash" && !name.includes("shell")) return []
const command = String(args?.command || args?.cmd || "")
const paths = []
- const pattern =
- /(?:(?:^|&&|\|\||;|\n)[ \t]*)(?:sudo\s+)?(?:command\s+)?(?:\/bin\/)?(?:rm|unlink)\s+(?:-[^\s]+\s+)*(?:"([^"]+)"|'([^']+)'|([^\s;&|]+))/g
+ // Operands end at a separator or a redirection; `2>/dev/null` is not a path.
+ const operand = /[ \t]+(?:"([^"]+)"|'([^']+)'|([^\s;&|<>"']+))(?=[\s;&|]|$)/y
+ const pattern = /(?:(?:^|&&|\|\||;|\n)[ \t]*)(?:sudo\s+)?(?:command\s+)?(?:\/bin\/)?(?:rm|unlink)(?=[ \t])/g
for (const match of command.matchAll(pattern)) {
- const path = match[1] || match[2] || match[3]
- if (path) paths.push(path)
+ operand.lastIndex = match.index + match[0].length
+ let options = true
+ for (let arg = operand.exec(command); arg; arg = operand.exec(command)) {
+ const bare = arg[3]
+ // A comment ends the command, and a command substitution is another command.
+ if (bare?.startsWith("#") || /\$\(|`/.test(arg[0])) break
+ if (options && bare === "--") {
+ options = false
+ continue
+ }
+ if (options && bare?.startsWith("-")) continue
+ // A variable or glob is not a path this can know, and a wrong path is worse than none.
+ if (/\$/.test(arg[1] || "") || (bare && /[$*?[]|^~/.test(bare))) continue
+ const path = arg[1] || arg[2] || bare
+ if (path) paths.push(path)
+ }
}
return paths
}
diff --git a/plugins/opencode-beacon/src/beacon.test.ts b/plugins/opencode-beacon/src/beacon.test.ts
index d7156144..93977687 100644
--- a/plugins/opencode-beacon/src/beacon.test.ts
+++ b/plugins/opencode-beacon/src/beacon.test.ts
@@ -316,6 +316,34 @@ describe("BeaconEndpointPlugin", () => {
})
})
+ test("records every path a shell rm deletes", async () => {
+ const hooks = await plugin()
+ const command = `rm -f /repo/a.txt "/repo/b c.txt" -- -d.txt 2>/dev/null && echo done`
+ await hooks["tool.execute.after"]!(
+ { tool: "bash", sessionID: "ses_1", callID: "call_rm_many", args: { command } },
+ { title: "rm", output: "done", metadata: { exit: 0 } },
+ )
+ await nextTurn()
+
+ expect(payloads[0].file_mutations).toEqual([
+ { path: "/repo/a.txt", operation: "delete" },
+ { path: "/repo/b c.txt", operation: "delete" },
+ { path: "-d.txt", operation: "delete" },
+ ])
+ })
+
+ test("records no comment, variable or substitution as an rm path", async () => {
+ const hooks = await plugin()
+ const command = `rm /repo/a.txt "$OUT" /repo/*.log ~/x # cleanup b.txt\nrm -rf $(cat list.txt)`
+ await hooks["tool.execute.after"]!(
+ { tool: "bash", sessionID: "ses_1", callID: "call_rm_noise", args: { command } },
+ { title: "rm", output: "", metadata: { exit: 0 } },
+ )
+ await nextTurn()
+
+ expect(payloads[0].file_mutations).toEqual([{ path: "/repo/a.txt", operation: "delete" }])
+ })
+
test("drains completed assistant parts when the message role arrives later", async () => {
const hooks = await plugin()
await hooks.event!({
diff --git a/plugins/opencode-beacon/src/beacon.ts b/plugins/opencode-beacon/src/beacon.ts
index 5bcc4e8f..85fc79ed 100644
--- a/plugins/opencode-beacon/src/beacon.ts
+++ b/plugins/opencode-beacon/src/beacon.ts
@@ -126,11 +126,26 @@ function shellMutationPaths(tool, args) {
if (name !== "bash" && !name.includes("shell")) return []
const command = String(args?.command || args?.cmd || "")
const paths = []
- const pattern =
- /(?:(?:^|&&|\|\||;|\n)[ \t]*)(?:sudo\s+)?(?:command\s+)?(?:\/bin\/)?(?:rm|unlink)\s+(?:-[^\s]+\s+)*(?:"([^"]+)"|'([^']+)'|([^\s;&|]+))/g
+ // Operands end at a separator or a redirection; `2>/dev/null` is not a path.
+ const operand = /[ \t]+(?:"([^"]+)"|'([^']+)'|([^\s;&|<>"']+))(?=[\s;&|]|$)/y
+ const pattern = /(?:(?:^|&&|\|\||;|\n)[ \t]*)(?:sudo\s+)?(?:command\s+)?(?:\/bin\/)?(?:rm|unlink)(?=[ \t])/g
for (const match of command.matchAll(pattern)) {
- const path = match[1] || match[2] || match[3]
- if (path) paths.push(path)
+ operand.lastIndex = match.index + match[0].length
+ let options = true
+ for (let arg = operand.exec(command); arg; arg = operand.exec(command)) {
+ const bare = arg[3]
+ // A comment ends the command, and a command substitution is another command.
+ if (bare?.startsWith("#") || /\$\(|`/.test(arg[0])) break
+ if (options && bare === "--") {
+ options = false
+ continue
+ }
+ if (options && bare?.startsWith("-")) continue
+ // A variable or glob is not a path this can know, and a wrong path is worse than none.
+ if (/\$/.test(arg[1] || "") || (bare && /[$*?[]|^~/.test(bare))) continue
+ const path = arg[1] || arg[2] || bare
+ if (path) paths.push(path)
+ }
}
return paths
}