Skip to content

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

fix: emit OpenCode shell deletion side effects

Cursor Agent244de6557296
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index ef892235..a60abe49 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -89,7 +89,8 @@ func opencodeEndpointEvents(input map[string]interface{}, sessionID string) []op
action, category = "tool.completed", "tool"
}
}
- return one(action, category, "info", opencodeToolMessage(action), fields)
+ events := one(action, category, "info", opencodeToolMessage(action), fields)
+ return append(events, opencodeFileMutationEvents(input, fields)...)
case "message.updated":
info := opencodeMap(input, "message_info", "info")
if len(info) == 0 {
@@ -622,6 +623,41 @@ func opencodeDiffEvents(input map[string]interface{}, base map[string]interface{
return events
}
+func opencodeFileMutationEvents(input map[string]interface{}, base map[string]interface{}) []opencodeNormalizedEvent {
+ command, _ := base["command"].(map[string]interface{})
+ if exitCode, ok := opencodeInt(command["exit_code"]); ok && exitCode != 0 {
+ return nil
+ }
+ items, _ := input["file_mutations"].([]interface{})
+ if len(items) == 0 {
+ return nil
+ }
+ var events []opencodeNormalizedEvent
+ for _, item := range items {
+ mutation, ok := item.(map[string]interface{})
+ if !ok {
+ continue
+ }
+ path := getFirstStr(mutation, "path", "file", "filePath", "file_path")
+ operation := firstNonEmpty(getFirstStr(mutation, "operation"), "modify")
+ if path == "" {
+ continue
+ }
+ fields := cloneOpenCodeFields(base)
+ delete(fields, "command")
+ fields["file"] = map[string]interface{}{
+ "path": path,
+ "operation": operation,
+ "language": strings.TrimPrefix(filepath.Ext(path), "."),
+ }
+ events = append(events, opencodeNormalizedEvent{
+ action: "file.modified", category: "file", severity: "info",
+ message: "opencode file " + operation + " observed", fields: fields,
+ })
+ }
+ return events
+}
+
func opencodeApprovalAction(decision string) string {
switch strings.ToLower(strings.TrimSpace(decision)) {
case "once", "always", "accepted", "accept", "allow", "allowed", "approve", "approved":
diff --git a/cli/beacon-hooks/cmd/opencode_event_test.go b/cli/beacon-hooks/cmd/opencode_event_test.go
index 11391dfc..19730e34 100644
--- a/cli/beacon-hooks/cmd/opencode_event_test.go
+++ b/cli/beacon-hooks/cmd/opencode_event_test.go
@@ -380,6 +380,43 @@ func TestOpenCodeWatcherUnlinkNormalizesFileDeletion(t *testing.T) {
}
}
+func TestOpenCodeSuccessfulRMEmitsCorrelatedFileDeletion(t *testing.T) {
+ input := map[string]interface{}{
+ "type": "tool.execute.after",
+ "session_id": "ses_test",
+ "tool_name": "bash",
+ "call_id": "call_rm",
+ "tool_input": map[string]interface{}{"command": `rm "/repo/.tmp/e2e.txt"`},
+ "tool_response": map[string]interface{}{
+ "output": "",
+ "metadata": map[string]interface{}{"exit": 0},
+ },
+ "file_mutations": []interface{}{
+ map[string]interface{}{"path": "/repo/.tmp/e2e.txt", "operation": "delete"},
+ },
+ }
+ events := opencodeEndpointEvents(input, "ses_test")
+ if len(events) != 2 || events[0].action != "command.executed" || events[1].action != "file.modified" {
+ t.Fatalf("events = %#v", events)
+ }
+ file := events[1].fields["file"].(map[string]interface{})
+ if file["operation"] != "delete" {
+ t.Fatalf("file = %#v", file)
+ }
+ call := events[1].fields["gen_ai"].(map[string]interface{})["tool"].(map[string]interface{})["call"].(map[string]interface{})
+ if call["id"] != "call_rm" {
+ t.Fatalf("call = %#v", call)
+ }
+
+ input["tool_response"] = map[string]interface{}{
+ "output": "failed",
+ "metadata": map[string]interface{}{"exit": 1},
+ }
+ if failed := opencodeEndpointEvents(input, "ses_test"); len(failed) != 1 {
+ t.Fatalf("failed rm emitted %d events, want command only", len(failed))
+ }
+}
+
func TestOpenCodePermissionRepliesMapAllowAndDeny(t *testing.T) {
for _, tt := range []struct {
reply string
diff --git a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
index 9d9991df..0b4af3f4 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
@@ -260,6 +260,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
duration_ms: active?.started_at ? Date.now() - active.started_at : undefined,
tool_input: args,
tool_response: output,
+ file_mutations: shellMutationPaths(input?.tool, args).map((path) => ({ path, operation: "delete" })),
}),
)
activeCalls.delete(input?.callID)
@@ -329,12 +330,9 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
properties.diff = filtered
}
if (type === "file.edited" || type === "file.watcher.updated") {
- if (!sid) {
- const seen = recentFilePaths.get(filePath(properties))
- if (!seen || Date.now() - seen.time >= 5000) return
- properties.sessionID = seen.sessionID
- properties.callID = seen.callID
- }
+ const seen = recentFilePaths.get(filePath(properties))
+ if (seen && Date.now() - seen.time < 5000) return
+ if (!sid) return
}
void enqueue(
payload(type, {
diff --git a/plugins/opencode-beacon/src/beacon.test.ts b/plugins/opencode-beacon/src/beacon.test.ts
index 90df5b9f..6f40d4e9 100644
--- a/plugins/opencode-beacon/src/beacon.test.ts
+++ b/plugins/opencode-beacon/src/beacon.test.ts
@@ -192,7 +192,7 @@ describe("BeaconEndpointPlugin", () => {
expect(payloads.some((item) => item.type === "message.part.updated")).toBe(false)
})
- test("correlates file deletion watcher events with an active bash call", async () => {
+ test("records shell deletion side effects and suppresses watcher duplicates", async () => {
const hooks = await plugin()
const path = "/repo/.tmp/beacon-opencode-e2e.txt"
await hooks["tool.execute.before"]!(
@@ -202,12 +202,18 @@ describe("BeaconEndpointPlugin", () => {
await hooks.event!({
event: { type: "file.watcher.updated", properties: { file: path, event: "unlink" } },
} as any)
+ await hooks["tool.execute.after"]!(
+ { tool: "bash", sessionID: "ses_1", callID: "call_rm", args: { command: `rm "${path}"` } },
+ { title: "rm", output: "", metadata: { exit: 0 } },
+ )
await Bun.sleep(20)
+ expect(payloads.map((item) => item.type)).toEqual(["tool.execute.before", "tool.execute.after"])
expect(payloads[1]).toMatchObject({
- type: "file.watcher.updated",
+ type: "tool.execute.after",
session_id: "ses_1",
- properties: { sessionID: "ses_1", callID: "call_rm", file: path, event: "unlink" },
+ call_id: "call_rm",
+ file_mutations: [{ path, operation: "delete" }],
})
})
})
diff --git a/plugins/opencode-beacon/src/beacon.ts b/plugins/opencode-beacon/src/beacon.ts
index 9d9991df..0b4af3f4 100644
--- a/plugins/opencode-beacon/src/beacon.ts
+++ b/plugins/opencode-beacon/src/beacon.ts
@@ -260,6 +260,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
duration_ms: active?.started_at ? Date.now() - active.started_at : undefined,
tool_input: args,
tool_response: output,
+ file_mutations: shellMutationPaths(input?.tool, args).map((path) => ({ path, operation: "delete" })),
}),
)
activeCalls.delete(input?.callID)
@@ -329,12 +330,9 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
properties.diff = filtered
}
if (type === "file.edited" || type === "file.watcher.updated") {
- if (!sid) {
- const seen = recentFilePaths.get(filePath(properties))
- if (!seen || Date.now() - seen.time >= 5000) return
- properties.sessionID = seen.sessionID
- properties.callID = seen.callID
- }
+ const seen = recentFilePaths.get(filePath(properties))
+ if (seen && Date.now() - seen.time < 5000) return
+ if (!sid) return
}
void enqueue(
payload(type, {