Skip to content

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

fix: tighten OpenCode tool classification

Cursor Agent217476c024ed
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index 56a87b71..4b95bce7 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -330,7 +330,7 @@ func opencodeToolFields(input map[string]interface{}, completed bool) map[string
}
if path := opencodeToolPath(toolInput); path != "" {
- operation := fileOperation(toolName)
+ operation := opencodeFileOperation(toolName)
if operation == "" {
delete(fields, "file")
if tool, ok := fields["tool"].(map[string]interface{}); ok {
@@ -356,7 +356,7 @@ func opencodeToolFields(input map[string]interface{}, completed bool) map[string
if path != "" {
mergeMap(fields, diffFields(path, diffText))
file := fields["file"].(map[string]interface{})
- file["operation"] = fileOperation(toolName)
+ file["operation"] = opencodeFileOperation(toolName)
}
}
if action, _ := opencodeToolAction(input); action == "command.executed" {
@@ -420,19 +420,43 @@ func opencodeDecision(input map[string]interface{}) string {
func opencodeToolAction(input map[string]interface{}) (string, string) {
name := strings.ToLower(opencodeToolName(input))
switch {
- case strings.Contains(name, "mcp") || strings.HasPrefix(name, "list_mcp_") || strings.HasPrefix(name, "read_mcp_"):
+ case opencodeToolHasToken(name, "mcp"):
return "mcp.tool_invoked", "mcp"
- case name == "bash" || strings.Contains(name, "shell") || strings.Contains(name, "terminal"):
+ case opencodeToolHasToken(name, "bash") || opencodeToolHasToken(name, "shell") || opencodeToolHasToken(name, "terminal") || name == "powershell":
return "command.executed", "command"
- case strings.Contains(name, "read"):
+ case opencodeToolHasToken(name, "read"):
return "file.read", "file"
- case strings.Contains(name, "edit") || strings.Contains(name, "write") || strings.Contains(name, "patch"):
+ case opencodeToolHasToken(name, "edit") || opencodeToolHasToken(name, "write") || opencodeToolHasToken(name, "patch") || opencodeToolHasToken(name, "create") || name == "applypatch" || name == "multiedit":
return "file.modified", "file"
default:
return "tool.completed", "tool"
}
}
+func opencodeToolHasToken(name, token string) bool {
+ for _, part := range strings.FieldsFunc(strings.ToLower(name), func(r rune) bool {
+ return (r < 'a' || r > 'z') && (r < '0' || r > '9')
+ }) {
+ if part == token {
+ return true
+ }
+ }
+ return false
+}
+
+func opencodeFileOperation(name string) string {
+ switch {
+ case opencodeToolHasToken(name, "read"), opencodeToolHasToken(name, "view"), opencodeToolHasToken(name, "list"):
+ return "read"
+ case opencodeToolHasToken(name, "write"), opencodeToolHasToken(name, "create"):
+ return "create"
+ case opencodeToolHasToken(name, "edit"), opencodeToolHasToken(name, "patch"), strings.EqualFold(name, "applypatch"), strings.EqualFold(name, "multiedit"):
+ return "modify"
+ default:
+ return ""
+ }
+}
+
func opencodeToolMessage(action string) string {
switch action {
case "command.executed":
diff --git a/cli/beacon-hooks/cmd/opencode_event_test.go b/cli/beacon-hooks/cmd/opencode_event_test.go
index 5c2f846f..6bf7de11 100644
--- a/cli/beacon-hooks/cmd/opencode_event_test.go
+++ b/cli/beacon-hooks/cmd/opencode_event_test.go
@@ -242,6 +242,25 @@ func TestOpenCodeToolLifecycleNormalization(t *testing.T) {
}
}
+func TestOpenCodeToolClassificationUsesWholeNameTokens(t *testing.T) {
+ for _, name := range []string{"thread", "credit_report"} {
+ action, category, _, _, fields := opencodeEndpointEvent(map[string]interface{}{
+ "type": "tool.execute.after",
+ "session_id": "ses_test",
+ "tool_name": name,
+ "call_id": "call_" + name,
+ "tool_input": map[string]interface{}{"filePath": "/repo/not-a-file-tool"},
+ "tool_response": map[string]interface{}{"output": "ok"},
+ }, "ses_test")
+ if action != "tool.completed" || category != "tool" {
+ t.Fatalf("%s classified as %s/%s", name, action, category)
+ }
+ if _, ok := fields["file"]; ok {
+ t.Fatalf("%s produced file fields: %#v", name, fields["file"])
+ }
+ }
+}
+
func TestOpenCodeCompletedAssistantNormalizesUsageOnce(t *testing.T) {
input := map[string]interface{}{
"type": "message.updated",
diff --git a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
index ca19640c..bcfeb41a 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
@@ -115,7 +115,11 @@ function filePath(value) {
function isFileMutationTool(tool) {
const name = String(tool || "").toLowerCase()
- return ["edit", "write", "patch", "apply_patch", "create"].some((part) => name.includes(part))
+ const tokens = name.split(/[^a-z0-9]+/).filter(Boolean)
+ return (
+ ["edit", "write", "patch", "create"].some((part) => tokens.includes(part)) ||
+ ["applypatch", "multiedit"].includes(name)
+ )
}
function shellMutationPaths(tool, args) {
@@ -375,6 +379,9 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
if (type === "file.edited" || type === "file.watcher.updated") {
const seen = recentFilePaths.get(filePath(properties))
if (seen && Date.now() - seen.time < 5000) return
+ // OpenCode watcher events are filesystem-scoped and commonly omit a
+ // session. Never guess an active session: ambient IDE/user changes
+ // must not be attributed to an agent trace.
if (!sid) return
}
const queued = enqueue(
diff --git a/plugins/opencode-beacon/src/beacon.test.ts b/plugins/opencode-beacon/src/beacon.test.ts
index c7c70dbe..3c43d823 100644
--- a/plugins/opencode-beacon/src/beacon.test.ts
+++ b/plugins/opencode-beacon/src/beacon.test.ts
@@ -353,4 +353,25 @@ describe("BeaconEndpointPlugin", () => {
expect(payloads[1].file_mutations).toEqual([])
})
+
+ test("does not treat substrings inside custom tool names as file mutations", async () => {
+ const hooks = await plugin()
+ const path = "/repo/credit.txt"
+ await hooks["tool.execute.before"]!(
+ { tool: "credit_report", sessionID: "ses_1", callID: "call_credit" },
+ { args: { filePath: path } },
+ )
+ await hooks.event!({
+ event: {
+ type: "session.diff",
+ properties: {
+ sessionID: "ses_1",
+ diff: [{ file: path, before: "old", after: "new", additions: 1, deletions: 1 }],
+ },
+ },
+ } as any)
+ await Bun.sleep(20)
+
+ expect(payloads.map((item) => item.type)).toEqual(["tool.execute.before", "session.diff"])
+ })
})
diff --git a/plugins/opencode-beacon/src/beacon.ts b/plugins/opencode-beacon/src/beacon.ts
index ca19640c..bcfeb41a 100644
--- a/plugins/opencode-beacon/src/beacon.ts
+++ b/plugins/opencode-beacon/src/beacon.ts
@@ -115,7 +115,11 @@ function filePath(value) {
function isFileMutationTool(tool) {
const name = String(tool || "").toLowerCase()
- return ["edit", "write", "patch", "apply_patch", "create"].some((part) => name.includes(part))
+ const tokens = name.split(/[^a-z0-9]+/).filter(Boolean)
+ return (
+ ["edit", "write", "patch", "create"].some((part) => tokens.includes(part)) ||
+ ["applypatch", "multiedit"].includes(name)
+ )
}
function shellMutationPaths(tool, args) {
@@ -375,6 +379,9 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
if (type === "file.edited" || type === "file.watcher.updated") {
const seen = recentFilePaths.get(filePath(properties))
if (seen && Date.now() - seen.time < 5000) return
+ // OpenCode watcher events are filesystem-scoped and commonly omit a
+ // session. Never guess an active session: ambient IDE/user changes
+ // must not be attributed to an agent trace.
if (!sid) return
}
const queued = enqueue(