Skip to content

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

fix: isolate OpenCode sibling event metadata

Cursor Agent595e0d61de40
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index a60abe49..56a87b71 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -625,7 +625,8 @@ func opencodeDiffEvents(input map[string]interface{}, base map[string]interface{
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 {
+ exitCode, ok := opencodeInt(command["exit_code"])
+ if !ok || exitCode != 0 {
return nil
}
items, _ := input["file_mutations"].([]interface{})
@@ -748,6 +749,12 @@ func opencodeFloat(value interface{}) (float64, bool) {
}
func cloneOpenCodeFields(input map[string]interface{}) map[string]interface{} {
+ if data, err := json.Marshal(input); err == nil {
+ var out map[string]interface{}
+ if err := json.Unmarshal(data, &out); err == nil {
+ return out
+ }
+ }
out := make(map[string]interface{}, len(input))
for key, value := range input {
out[key] = value
diff --git a/cli/beacon-hooks/cmd/opencode_event_test.go b/cli/beacon-hooks/cmd/opencode_event_test.go
index 19730e34..5c2f846f 100644
--- a/cli/beacon-hooks/cmd/opencode_event_test.go
+++ b/cli/beacon-hooks/cmd/opencode_event_test.go
@@ -415,6 +415,43 @@ func TestOpenCodeSuccessfulRMEmitsCorrelatedFileDeletion(t *testing.T) {
if failed := opencodeEndpointEvents(input, "ses_test"); len(failed) != 1 {
t.Fatalf("failed rm emitted %d events, want command only", len(failed))
}
+ input["tool_response"] = map[string]interface{}{"output": "unknown", "metadata": map[string]interface{}{}}
+ if unknown := opencodeEndpointEvents(input, "ses_test"); len(unknown) != 1 {
+ t.Fatalf("rm without exit metadata emitted %d events, want command only", len(unknown))
+ }
+}
+
+func TestOpenCodeSiblingEventsDoNotShareNestedFields(t *testing.T) {
+ events := opencodeEndpointEvents(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"},
+ },
+ }, "ses_test")
+ if len(events) != 2 {
+ t.Fatalf("events = %d", len(events))
+ }
+ firstCall := events[0].fields["gen_ai"].(map[string]interface{})["tool"].(map[string]interface{})["call"].(map[string]interface{})
+ delete(firstCall, "arguments")
+ firstContent := events[0].fields["content"].(map[string]interface{})
+ firstContent["included"] = false
+
+ secondCall := events[1].fields["gen_ai"].(map[string]interface{})["tool"].(map[string]interface{})["call"].(map[string]interface{})
+ if secondCall["arguments"] == nil {
+ t.Fatal("mutating first event removed second event arguments")
+ }
+ secondContent := events[1].fields["content"].(map[string]interface{})
+ if secondContent["included"] != true {
+ t.Fatalf("mutating first event changed second content: %#v", secondContent)
+ }
}
func TestOpenCodePermissionRepliesMapAllowAndDeny(t *testing.T) {
diff --git a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
index 260d0912..e107aca0 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
@@ -281,6 +281,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
"tool.execute.after": async (input, output) => {
const active = activeCalls.get(input?.callID)
const args = input?.args || active?.args || {}
+ const exitCode = output?.metadata?.exit ?? output?.metadata?.exitCode ?? output?.metadata?.exit_code
rememberFilePath(input?.tool, args, sessionID(input), input?.callID)
await enqueue(
payload("tool.execute.after", {
@@ -290,7 +291,10 @@ 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" })),
+ file_mutations:
+ exitCode === 0
+ ? shellMutationPaths(input?.tool, args).map((path) => ({ path, operation: "delete" }))
+ : [],
}),
)
activeCalls.delete(input?.callID)
diff --git a/plugins/opencode-beacon/src/beacon.test.ts b/plugins/opencode-beacon/src/beacon.test.ts
index d493b262..5803e423 100644
--- a/plugins/opencode-beacon/src/beacon.test.ts
+++ b/plugins/opencode-beacon/src/beacon.test.ts
@@ -313,4 +313,19 @@ describe("BeaconEndpointPlugin", () => {
expect(payloads[1].file_mutations).toEqual([])
expect(payloads[2].model).toBe("moonshotai/kimi-k3")
})
+
+ test("requires a successful shell exit before emitting delete mutations", async () => {
+ const hooks = await plugin()
+ const path = "/tmp/maybe-not-deleted"
+ await hooks["tool.execute.before"]!(
+ { tool: "bash", sessionID: "ses_1", callID: "call_rm_unknown" },
+ { args: { command: `rm "${path}"` } },
+ )
+ await hooks["tool.execute.after"]!(
+ { tool: "bash", sessionID: "ses_1", callID: "call_rm_unknown", args: { command: `rm "${path}"` } },
+ { title: "rm", output: "", metadata: {} },
+ )
+
+ expect(payloads[1].file_mutations).toEqual([])
+ })
})
diff --git a/plugins/opencode-beacon/src/beacon.ts b/plugins/opencode-beacon/src/beacon.ts
index 260d0912..e107aca0 100644
--- a/plugins/opencode-beacon/src/beacon.ts
+++ b/plugins/opencode-beacon/src/beacon.ts
@@ -281,6 +281,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
"tool.execute.after": async (input, output) => {
const active = activeCalls.get(input?.callID)
const args = input?.args || active?.args || {}
+ const exitCode = output?.metadata?.exit ?? output?.metadata?.exitCode ?? output?.metadata?.exit_code
rememberFilePath(input?.tool, args, sessionID(input), input?.callID)
await enqueue(
payload("tool.execute.after", {
@@ -290,7 +291,10 @@ 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" })),
+ file_mutations:
+ exitCode === 0
+ ? shellMutationPaths(input?.tool, args).map((path) => ({ path, operation: "delete" }))
+ : [],
}),
)
activeCalls.delete(input?.callID)