Skip to content

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

Fix afterRun ignoring abort and failure status in Cline hook

Cursor Agentd1169d903018
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.go
index 296e8154..296a8512 100644
--- a/cli/beacon-hooks/cmd/cline_event.go
+++ b/cli/beacon-hooks/cmd/cline_event.go
@@ -127,10 +127,25 @@ func clineEndpointEvents(input map[string]interface{}, sessionID string) []norma
case clineStageToolAfter:
return clineToolAfterEvents(input, fields)
case clineStageTaskEnd:
- if usage := clineUsage(input); len(usage) > 0 {
- fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
+ switch clineRunStatus(input) {
+ case "aborted":
+ if usage := clineUsage(input); len(usage) > 0 {
+ fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
+ }
+ fields["session"] = mergeNested(fields["session"], map[string]interface{}{"cancel_reason": clineCancelReason(input)})
+ return one("session.ended", "session", "info", "Cline task cancelled", fields)
+ case "failed":
+ if usage := clineUsage(input); len(usage) > 0 {
+ fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
+ }
+ fields["error"] = map[string]interface{}{"type": clineErrorType(input)}
+ return one("session.error", "session", "high", "Cline task ended with an error", fields)
+ default:
+ if usage := clineUsage(input); len(usage) > 0 {
+ fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
+ }
+ return one("session.ended", "session", "info", "Cline task completed", fields)
}
- return one("session.ended", "session", "info", "Cline task completed", fields)
case clineStageTaskCancel:
if usage := clineUsage(input); len(usage) > 0 {
fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
@@ -686,6 +701,25 @@ func clineToolAfterEvents(input map[string]interface{}, fields map[string]interf
}}
}
+// clineRunStatus reads the outcome status from an afterRun payload.
+//
+// Cline's AgentRunResult carries status: "completed" | "aborted" | "failed". The value may sit at
+// the top level (plugin SDK shape) or nested under result/run (file-based hook shape). An empty
+// return means a normal completion or an unrecognized shape, both of which are treated identically.
+func clineRunStatus(input map[string]interface{}) string {
+ if s := getFirstStr(input, "status"); s != "" {
+ return strings.ToLower(s)
+ }
+ for _, key := range []string{"result", "run"} {
+ if nested := firstMap(input, key); nested != nil {
+ if s := getFirstStr(nested, "status"); s != "" {
+ return strings.ToLower(s)
+ }
+ }
+ }
+ return ""
+}
+
// clineCancelReason reports why a task ended without completing: cancelled, abandoned, or whatever
// else the payload says.
//
diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.go
index ed1b1c09..3704760e 100644
--- a/cli/beacon-hooks/cmd/cline_event_test.go
+++ b/cli/beacon-hooks/cmd/cline_event_test.go
@@ -702,6 +702,70 @@ func TestClineEventTaskErrorStaysAnError(t *testing.T) {
}
}
+// afterRun with status "aborted" must produce a cancel event, not a normal completion.
+func TestClineEventAfterRunAbortedIsCancelNotCompletion(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "type": "afterRun",
+ "taskId": "task-aborted",
+ "status": "aborted",
+ "reason": "user_stopped",
+ "result": map[string]interface{}{"usage": map[string]interface{}{"inputTokens": 50, "outputTokens": 8}},
+ })
+
+ event := clineEventWithAction(t, logPath, "session.ended")
+ if got := event["severity"]; got != "info" {
+ t.Errorf("severity = %q, want info", got)
+ }
+ if got := nested(t, event, "session")["cancel_reason"]; got != "user_stopped" {
+ t.Errorf("session.cancel_reason = %v, want user_stopped", got)
+ }
+ if got := nested(t, event, "gen_ai", "usage")["input_tokens"]; got != float64(50) {
+ t.Errorf("gen_ai.usage.input_tokens = %v, want 50", got)
+ }
+}
+
+// afterRun with status "failed" must produce a session.error event, not a normal completion.
+func TestClineEventAfterRunFailedIsErrorNotCompletion(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "type": "afterRun",
+ "taskId": "task-failed-run",
+ "status": "failed",
+ "error": map[string]interface{}{"name": "api_error"},
+ "result": map[string]interface{}{"usage": map[string]interface{}{"inputTokens": 30, "outputTokens": 3}},
+ })
+
+ 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 != "api_error" {
+ t.Errorf("error.type = %q, want api_error", got)
+ }
+ if got := nested(t, event, "gen_ai", "usage")["input_tokens"]; got != float64(30) {
+ t.Errorf("gen_ai.usage.input_tokens = %v, want 30", got)
+ }
+}
+
+// afterRun with status nested under result still routes correctly.
+func TestClineEventAfterRunNestedStatusIsRecognized(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "type": "afterRun",
+ "taskId": "task-nested-abort",
+ "result": map[string]interface{}{"status": "aborted"},
+ })
+
+ event := clineEventWithAction(t, logPath, "session.ended")
+ if got := nested(t, event, "session")["cancel_reason"]; got == nil {
+ t.Errorf("session.cancel_reason is missing, want a cancel reason for an aborted run")
+ }
+}
+
// Cline's two hook surfaces disagree about where the cancel status lives. Missing the nested one
// labels an abandoned task as cancelled -- a wrong distinction rather than a missing one, which is
// why it is read from both.