Skip to content

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

fix(hooks): treat the presence of a tool error as the failure signal

Claude8bfba6e99a53
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.go
index ceb5920d..fe6ec198 100644
--- a/cli/beacon-hooks/cmd/cline_event.go
+++ b/cli/beacon-hooks/cmd/cline_event.go
@@ -314,25 +314,45 @@ func clineToolResponse(input map[string]interface{}) map[string]interface{} {
return nil
}
-// clineToolError reports the failure text on a completed tool call, or "" when it succeeded.
-func clineToolError(input map[string]interface{}) string {
+// clineToolFailure reports whether a completed tool call failed, and the text describing it.
+//
+// Failure and its description are separate answers because a payload can carry one without the
+// other. Reading the text and treating "" as success left the reported bug half-open: an error
+// object of {"code": 2} has no string field at all, and an empty {} has no field, so both were
+// still recorded as successful tool calls -- the quietest way to be wrong, since the event writes
+// and reads as ordinary activity. The presence of the error object is the failure signal; its
+// contents only describe it.
+//
+// The response is read only for keys that name an error explicitly. It deliberately does not read
+// `message`: Cline results carry status text there on success ("File written successfully"), and
+// reading it turned every such completion into a high-severity failure.
+func clineToolFailure(input map[string]interface{}) (string, bool) {
if text := getFirstStr(input, "error", "errorMessage", "error_message"); text != "" {
- return text
+ return text, true
}
if errMap := firstMap(input, "error"); errMap != nil {
- if text := getFirstStr(errMap, "message", "error", "errorMessage", "error_message"); text != "" {
- return text
- }
- if name := getFirstStr(errMap, "name", "type", "code"); name != "" {
- return name
- }
+ return getFirstStr(errMap, "message", "error", "errorMessage", "error_message", "name", "type", "code"), true
}
if response := clineToolResponse(input); response != nil {
if text := getFirstStr(response, "error", "errorMessage", "error_message"); text != "" {
- return text
+ return text, true
}
}
- return ""
+ return "", false
+}
+
+// clineToolErrorType names a failed tool call's error for the error.type field.
+//
+// Reads the same keys clineErrorType reads for session errors, which were inconsistent: a session
+// error reported its name while a tool error was always "tool_error", discarding the one detail an
+// investigator would filter on.
+func clineToolErrorType(input map[string]interface{}) string {
+ if errMap := firstMap(input, "error"); errMap != nil {
+ if name := getFirstStr(errMap, "name", "type", "code"); name != "" {
+ return name
+ }
+ }
+ return "tool_error"
}
func clineErrorType(input map[string]interface{}) string {
@@ -584,9 +604,11 @@ func clineCommandFields(input, toolInput, toolResponse map[string]interface{}) m
func clineToolAfterEvents(input map[string]interface{}, fields map[string]interface{}) []normalizedEvent {
mergeMap(fields, clineToolFields(input, true))
- if errText := clineToolError(input); errText != "" {
- fields["error"] = map[string]interface{}{"type": "tool_error"}
- fields["content"] = retainedContentFields(errText)
+ if errText, failed := clineToolFailure(input); failed {
+ fields["error"] = map[string]interface{}{"type": clineToolErrorType(input)}
+ if errText != "" {
+ fields["content"] = retainedContentFields(errText)
+ }
return []normalizedEvent{{
action: "tool.failed", category: "tool", severity: "high",
message: "Cline tool failed", fields: fields,
diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.go
index eb6e4316..476794ef 100644
--- a/cli/beacon-hooks/cmd/cline_event_test.go
+++ b/cli/beacon-hooks/cmd/cline_event_test.go
@@ -487,10 +487,13 @@ func TestClineStageRecognizesSpellingVariants(t *testing.T) {
"tool_call_after": clineStageToolAfter,
"afterRun": clineStageTaskEnd,
"session_shutdown": clineStageTaskEnd,
- "TaskCancel": clineStageTaskCancel,
- "stop_error": clineStageTaskError,
- "iteration_start": "",
- "": "",
+ // A cancel and a failure are different outcomes and must not share a stage.
+ "TaskCancel": clineStageTaskCancel,
+ "task_cancel": clineStageTaskCancel,
+ "stop_error": clineStageTaskError,
+ "error": clineStageTaskError,
+ "iteration_start": "",
+ "": "",
} {
t.Run(input, func(t *testing.T) {
if got := clineStage(map[string]interface{}{"type": input}); got != want {
@@ -570,3 +573,85 @@ func TestClineWorkspacePathIsHostIndependent(t *testing.T) {
})
}
}
+
+// An error object with no message-like field is still a failure. Treating its empty text as "no
+// error" recorded a failed tool call as a successful one -- the quietest way to be wrong, because
+// the event still writes and reads as ordinary activity.
+func TestClineEventToolFailureWithoutAMessage(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ errValue interface{}
+ wantType string
+ }{
+ {name: "name only", errValue: map[string]interface{}{"name": "ENOENT"}, wantType: "ENOENT"},
+ {name: "type and code", errValue: map[string]interface{}{"type": "spawn_failed", "code": float64(2)}, wantType: "spawn_failed"},
+ {name: "numeric code only", errValue: map[string]interface{}{"code": float64(2)}, wantType: "tool_error"},
+ {name: "empty object", errValue: map[string]interface{}{}, wantType: "tool_error"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ logPath := clineTestLog(t)
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "type": "afterTool",
+ "taskId": "task-1",
+ "toolCall": map[string]interface{}{"name": "read_file", "input": map[string]interface{}{"path": "missing.ts"}},
+ "error": tc.errValue,
+ })
+
+ event := clineEventWithAction(t, logPath, "tool.failed")
+ if got := event["severity"]; got != "high" {
+ t.Errorf("severity = %q, want high", got)
+ }
+ if got := nested(t, event, "error")["type"]; got != tc.wantType {
+ t.Errorf("error.type = %q, want %q", got, tc.wantType)
+ }
+ })
+ }
+}
+
+// A cancel is a person stopping their own task, not an incident. Recorded as session.error at high
+// severity it counted every deliberate stop as a failure, inflating error rollups and tripping
+// alerts on the most ordinary thing anyone does with an agent.
+func TestClineEventTaskCancelIsAnEndNotAnError(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "hookName": "TaskCancel",
+ "taskId": "task-cancelled",
+ "usage": map[string]interface{}{"inputTokens": 40, "outputTokens": 5},
+ })
+
+ event := clineEventWithAction(t, logPath, "session.ended")
+ if got := event["severity"]; got != "info" {
+ t.Errorf("severity = %q, want info", got)
+ }
+ if _, ok := event["error"]; ok {
+ t.Errorf("cancel carried an error field: %v", event["error"])
+ }
+ // The reason field is what lets a rollup separate cancels from completions without parsing the
+ // message text.
+ if got := nested(t, event, "session")["cancel_reason"]; got != "cancelled" {
+ t.Errorf("session.cancel_reason = %v, want cancelled", got)
+ }
+ // A cancelled task still reports what it spent.
+ if got := nested(t, event, "gen_ai", "usage")["input_tokens"]; got != float64(40) {
+ t.Errorf("gen_ai.usage.input_tokens = %v, want 40", got)
+ }
+}
+
+func TestClineEventTaskErrorStaysAnError(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "hookName": "stop_error",
+ "taskId": "task-failed",
+ "error": map[string]interface{}{"name": "provider_timeout"},
+ })
+
+ event := clineEventWithAction(t, logPath, "session.error")
+ if got := event["severity"]; got != "high" {
+ t.Errorf("severity = %q, want high", got)
+ }
+ if got := nested(t, event, "error")["type"]; got != "provider_timeout" {
+ t.Errorf("error.type = %q, want provider_timeout", got)
+ }
+}