The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(grok): preserve failed writes as failed attempts (#965)
Divyam Talwar2a858c2092da
diff --git a/cli/beacon-hooks/cmd/grok_hooks_test.go b/cli/beacon-hooks/cmd/grok_hooks_test.goindex fe9a8a04..0976305c 100644--- a/cli/beacon-hooks/cmd/grok_hooks_test.go+++ b/cli/beacon-hooks/cmd/grok_hooks_test.go@@ -2,6 +2,7 @@ package cmdimport ("path/filepath"+ "strings""testing")@@ -156,6 +157,116 @@ func TestGrokPostToolMapsCommandAndFailure(t *testing.T) {}}+func TestGrokPostToolFailedEditsRemainFailed(t *testing.T) {+ for _, tt := range []struct {+ name, eventKey, eventName, toolName, extension, errorText string+ }{+ {"native write failure", "hookEventName", "post_tool_use_failure", "write_file", ".go", ""},+ {"PascalCase event", "hookEventName", "PostToolUseFailure", "write_file", ".go", ""},+ {"camelCase event", "hook_event_name", "postToolUseFailure", "write_file", ".go", ""},+ {"explicit error", "hookEventName", "post_tool_use", "write_file", ".go", "permission denied"},+ {"unscannable path", "hookEventName", "post_tool_use_failure", "write_file", ".md", ""},+ {"edit failure", "hookEventName", "post_tool_use_failure", "Edit", ".go", ""},+ {"search replace failure", "hookEventName", "post_tool_use_failure", "search_replace", ".go", ""},+ } {+ t.Run(tt.name, func(t *testing.T) {+ setupHookConfigDirs(t)+ platformFlag = "grok"+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)+ path := filepath.Join(t.TempDir(), "not-written"+tt.extension)+ input := map[string]interface{}{+ tt.eventKey: tt.eventName,+ "sessionId": "grok-failed-edit",+ "toolName": tt.toolName,+ "tool_use_id": "failed-edit-call",+ "toolInput": map[string]interface{}{+ "path": path, "content": "package requested_but_not_written\n",+ "old_string": "package old", "new_string": "package requested_but_not_written",+ },+ "error": tt.errorText,+ }+ // A structured patch makes search_replace eligible for the success-only+ // diff path even though its name has no dedicated diff constructor.+ if tt.toolName == "search_replace" {+ input["toolResponse"] = map[string]interface{}{"structuredPatch": []interface{}{+ map[string]interface{}{+ "oldStart": 1, "oldLines": 1, "newStart": 1, "newLines": 1,+ "lines": []interface{}{"-package old", "+package requested_but_not_written"},+ },+ }}+ }+ out := runHookWithInput(t, runPostTool, input)+ if len(out) != 0 {+ t.Fatalf("response = %#v, want empty object", out)+ }+ events := endpointEvents(t, logPath)+ if len(events) != 1 {+ t.Fatalf("events = %#v, want exactly one failed attempt", events)+ }+ event := events[0]+ classification := event["event"].(map[string]interface{})+ if classification["action"] != "tool.failed" || classification["category"] != "tool" || event["severity"] != "high" {+ t.Fatalf("classification = %#v severity %v, want tool.failed/tool/high", classification, event["severity"])+ }+ if session := event["session"].(map[string]interface{}); session["id"] != "grok-failed-edit" {+ t.Fatalf("session = %#v, want attempted session", session)+ }+ if tool := event["tool"].(map[string]interface{}); tool["name"] != tt.toolName || tool["path"] != path {+ t.Fatalf("tool = %#v, want attempted tool and path", tool)+ }+ file := event["file"].(map[string]interface{})+ if file["path"] != path {+ t.Fatalf("file.path = %v, want %s", file["path"], path)+ }+ for _, key := range []string{"diff", "diff_hash", "diff_bytes"} {+ if _, present := file[key]; present {+ t.Fatalf("failed attempt has fabricated file.%s: %#v", key, file)+ }+ }+ genAI := event["gen_ai"].(map[string]interface{})+ call := genAI["tool"].(map[string]interface{})["call"].(map[string]interface{})+ if call["id"] != "failed-edit-call" {+ t.Fatalf("call.id = %v, want attempted call", call["id"])+ }+ raw := event["raw"].(map[string]interface{})["grok"].(map[string]interface{})+ if raw[tt.eventKey] != tt.eventName || raw["toolName"] != tt.toolName || raw["error"] != tt.errorText {+ t.Fatalf("raw.grok = %#v, want native failed-attempt payload", raw)+ }+ })+ }+}++func TestGrokPostToolSuccessfulWriteRetainsDiff(t *testing.T) {+ setupHookConfigDirs(t)+ platformFlag = "grok"+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)+ path := filepath.Join(t.TempDir(), "written.go")+ runHookWithInput(t, runPostTool, map[string]interface{}{+ "hookEventName": "post_tool_use",+ "sessionId": "grok-successful-edit",+ "toolName": "write_file",+ "toolInput": map[string]interface{}{"path": path, "content": "package written\n"},+ "error": "",+ })+ events := endpointEvents(t, logPath)+ if len(events) != 1 {+ t.Fatalf("events = %#v, want one successful edit", events)+ }+ event := events[0]+ if action := event["event"].(map[string]interface{})["action"]; action != "file.modified" {+ t.Fatalf("event.action = %v, want file.modified", action)+ }+ file := event["file"].(map[string]interface{})+ diff, _ := file["diff"].(string)+ hash, _ := file["diff_hash"].(string)+ bytes, _ := file["diff_bytes"].(float64)+ if file["path"] != path || !strings.Contains(diff, "+package written") || hash == "" || bytes <= 0 {+ t.Fatalf("file = %#v, want successful write path and retained diff metadata", file)+ }+}+func TestGrokCwdFallsBackToEnvironment(t *testing.T) {setupHookConfigDirs(t)platformFlag = "grok"diff --git a/cli/beacon-hooks/cmd/post_tool.go b/cli/beacon-hooks/cmd/post_tool.goindex 32474842..230218b1 100644--- a/cli/beacon-hooks/cmd/post_tool.go+++ b/cli/beacon-hooks/cmd/post_tool.go@@ -223,13 +223,17 @@ func parseClaudeCopilotInput(input map[string]interface{}, logger *logging.Logge// missing the event: the log would assert a file changed when it did not.//// Returning nil hands the payload back to emitPostToolObserved, which classifies it as- // `tool.failed` at high severity. Antigravity guards the same way just above, for the same- // reason. Scoped to the platform rather than applied to every runtime because the branch above- // establishes that each runtime's failure signal is its own; widening it would change Claude- // Code's recorded behavior with no fixture to say what that behavior should become.+ // `tool.failed` at high severity. Antigravity guards the same way just above, and Grok just+ // below, for the same reason. Scoped to the platform rather than applied to every runtime+ // because the branch above establishes that each runtime's failure signal is its own; widening+ // it would change Claude Code's recorded behavior with no fixture to say what that behavior+ // should become.if platformFlag == "qwen" && qwenToolFailed(input) {return nil}+ if platformFlag == "grok" && reportedPostToolFailure(getFirstStr(input, "hook_event_name", "hookEventName"), input) {+ return nil+ }filePath := diff.GetStringFromMaps("file_path", toolInput, toolResponse)if filePath == "" {@@ -480,7 +484,7 @@ func emitPostToolObserved(logger *logging.Logger, input map[string]interface{})if platformFlag == kimiPlatform {applyKimiToolResult(fields, toolName, toolInput, input)}- if hookEvent == "PostToolUseFailure" || hookEvent == "postToolUseFailure" || hookEvent == "post_tool_use_failure" || getFirstStr(input, "error") != "" {+ if reportedPostToolFailure(hookEvent, input) {emitHookEvent(logger, "tool.failed", "tool", "high", "Tool execution failed", input, fields)return}@@ -554,6 +558,11 @@ func emitPostToolObserved(logger *logging.Logger, input map[string]interface{})emitHookEvent(logger, action, category, "info", message, input, fields)}+func reportedPostToolFailure(hookEvent string, input map[string]interface{}) bool {+ return hookEvent == "PostToolUseFailure" || hookEvent == "postToolUseFailure" ||+ hookEvent == "post_tool_use_failure" || getFirstStr(input, "error") != ""+}+func emitCursorPostHookObserved(logger *logging.Logger, input map[string]interface{}) bool {hookEvent := getFirstStr(input, "hook_event_name", "hookEventName")sessionID := resolveSessionID(input, "cursor")diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdxindex 5a9a60a8..1a5df6be 100644--- a/docs/changelog/cli.mdx+++ b/docs/changelog/cli.mdx@@ -18,6 +18,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.- **Shared Hermes sessions keep their token usage and cost** · `beacon share` of a Hermes Agent session read from its `state.db` left out the session's token usage and cost, so the shared trace showed none. They are now included, with Hermes's actual cost preferred over its estimate, as `beacon endpoint hermes sync` records them.- **Dashboard token totals honor the session filter** · With the dashboard's session-state filter set to captured, recurring or empty, the token view showed zero tokens and no events, and `all` or an unrecognized value hid everything. Token totals now cover the same sessions the event list shows for that filter.- **Hermes sync keeps profiles apart** · Every Hermes home shared one sync cursor, so syncing a second profile or a custom `HERMES_HOME` on the same host could skip sessions the first sync had already passed. Each home now has its own cursor, and synced events record the home in `harness.config_path` and the profile in the new `harness.profile` field. The native `~/.hermes` root keeps its existing cursor, and the home a default sync resolves to (through `HERMES_HOME` or a sticky profile) starts its keyed cursor from the one it had been advancing, so neither re-emits history; any other home collects its retained history once more on its first sync.+- **Failed Grok writes are recorded as failures** · When a Grok `write_file` or edit failed, the hook recorded it as a completed `file.modified` with a diff built from content that was never written, and the threat scan read that content as a change. The failed attempt is now a high-severity `tool.failed` with its tool, path and call ID and no diff. Successful writes are unchanged.- **Dashboard output tokens count reasoning once** · The local dashboard's token view added reasoning tokens to output, but `gen_ai.usage.output_tokens` already includes them, so models that report reasoning had their output and total tokens overstated. Output and the total now count reasoning once, the split bar no longer shows reasoning as a separate segment, and a note below it gives the reasoning count included in output. `beacon token-usage` already counted it this way.---