Skip to content

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

fix(hooks): keep the volume when a Cline path walks above its root

Claudefde1ea89f5f3
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.go
index 1d134be0..14015acf 100644
--- a/cli/beacon-hooks/cmd/cline_event.go
+++ b/cli/beacon-hooks/cmd/cline_event.go
@@ -2,6 +2,7 @@ package cmd
import (
"encoding/json"
+ "path"
"path/filepath"
"strings"
@@ -134,27 +135,7 @@ func clineEndpointEvents(input map[string]interface{}, sessionID string) []norma
if usage := clineUsage(input); len(usage) > 0 {
fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
}
- reason := getFirstStr(input, "completionStatus", "completion_status", "reason")
- if reason == "" {
- for _, key := range []string{"taskCancel", "task_cancel", "taskMetadata", "task_metadata", "result", "metadata"} {
- if nested := firstMap(input, key); nested != nil {
- reason = getFirstStr(nested, "completionStatus", "completion_status", "reason")
- if reason != "" {
- break
- }
- if inner := firstMap(nested, "taskMetadata", "task_metadata", "metadata"); inner != nil {
- reason = getFirstStr(inner, "completionStatus", "completion_status", "reason")
- if reason != "" {
- break
- }
- }
- }
- }
- }
- if reason == "" {
- reason = "cancelled"
- }
- fields["session"] = mergeNested(fields["session"], map[string]interface{}{"cancel_reason": reason})
+ fields["session"] = mergeNested(fields["session"], map[string]interface{}{"cancel_reason": clineCancelReason(input)})
return one("session.ended", "session", "info", "Cline task cancelled", fields)
case clineStageTaskError:
fields["error"] = map[string]interface{}{"type": clineErrorType(input)}
@@ -501,30 +482,53 @@ func joinWorkspacePath(root, rel string) string {
if strings.Contains(root, "\\") && !strings.Contains(root, "/") {
separator = "\\"
}
- rel = strings.TrimPrefix(strings.TrimPrefix(rel, "./"), ".\\")
- rel = strings.ReplaceAll(strings.ReplaceAll(rel, "\\", separator), "/", separator)
- joined := strings.TrimRight(root, "/\\") + separator + strings.TrimLeft(rel, "/\\")
- return cleanPathSegments(joined, separator)
+ // Cleaning happens in slash space with the volume held aside, then reattached. A volume prefix
+ // cannot go through path.Clean: a leading "//" collapses, turning a UNC share into a directory,
+ // and "C:" is an ordinary segment that ".." can walk past -- which turned "C:\repo" plus
+ // "..\..\..\x.ts" into the bare relative "x.ts", an absolute Windows path with no drive left
+ // on it.
+ volume, rootRest := splitPathVolume(root)
+ joined := path.Join(toSlashPath(rootRest), toSlashPath(rel))
+ if separator != "/" {
+ joined = strings.ReplaceAll(joined, "/", separator)
+ }
+ return volume + joined
}
-// cleanPathSegments collapses . and .. segments in a path without using filepath.Clean (which
-// would impose the host's separator). The separator must be "/" or "\\".
-func cleanPathSegments(path, separator string) string {
- parts := strings.Split(path, separator)
- var cleaned []string
- for _, part := range parts {
- switch part {
- case ".":
- continue
- case "..":
- if len(cleaned) > 0 && cleaned[len(cleaned)-1] != "" && cleaned[len(cleaned)-1] != ".." {
- cleaned = cleaned[:len(cleaned)-1]
- }
- default:
- cleaned = append(cleaned, part)
+// splitPathVolume separates a Windows volume prefix -- a drive letter or a UNC share -- from the
+// rest of a path. Returns an empty volume for anything else, including every POSIX path.
+func splitPathVolume(p string) (string, string) {
+ if len(p) >= 2 && p[1] == ':' {
+ if letter := p[0] | 0x20; letter >= 'a' && letter <= 'z' {
+ return p[:2], p[2:]
}
}
- return strings.Join(cleaned, separator)
+ if len(p) >= 2 && isPathSeparator(p[0]) && isPathSeparator(p[1]) {
+ rest := p[2:]
+ server := indexPathSeparator(rest)
+ if server < 0 {
+ return p, ""
+ }
+ share := indexPathSeparator(rest[server+1:])
+ if share < 0 {
+ return p, ""
+ }
+ end := 2 + server + 1 + share
+ return p[:end], p[end:]
+ }
+ return "", p
+}
+
+func toSlashPath(p string) string {
+ return strings.ReplaceAll(p, "\\", "/")
+}
+
+func isPathSeparator(c byte) bool {
+ return c == '/' || c == '\\'
+}
+
+func indexPathSeparator(p string) int {
+ return strings.IndexAny(p, "/\\")
}
func clineToolPath(toolInput map[string]interface{}, root string) string {
@@ -667,6 +671,39 @@ func clineToolAfterEvents(input map[string]interface{}, fields map[string]interf
}}
}
+// clineCancelReason reports why a task ended without completing: cancelled, abandoned, or whatever
+// else the payload says.
+//
+// Searched across the nestings Cline's file-based hook payload is reported to use -- notably
+// taskCancel.taskMetadata.completionStatus -- as well as the top level, because the two hook
+// surfaces do not agree on shape. Those paths come from a review of this change rather than from a
+// payload captured from a running Cline, so the search is additive: a miss costs the
+// cancelled-versus-abandoned distinction and falls back to "cancelled", never to a wrong reason.
+//
+// A named function rather than a loop inside the event switch so it can be tested directly, which
+// is what a set of guessed field paths most needs.
+func clineCancelReason(input map[string]interface{}) string {
+ if reason := getFirstStr(input, "completionStatus", "completion_status", "reason"); reason != "" {
+ return reason
+ }
+ outers := []map[string]interface{}{input}
+ for _, key := range []string{"taskCancel", "task_cancel", "taskMetadata", "task_metadata", "result", "metadata"} {
+ if nested := firstMap(input, key); nested != nil {
+ outers = append(outers, nested)
+ }
+ }
+ for _, outer := range outers {
+ if reason := getFirstStr(outer, "completionStatus", "completion_status", "reason"); reason != "" {
+ return reason
+ }
+ inner := firstMap(outer, "taskMetadata", "task_metadata", "metadata")
+ if reason := getFirstStr(inner, "completionStatus", "completion_status", "reason"); reason != "" {
+ return reason
+ }
+ }
+ return "cancelled"
+}
+
// clineUsage normalizes Cline's reported token counts into gen_ai.usage.
//
// Read from the task-end payload only, and deliberately not from any per-model-call stage. Beacon's
diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.go
index 50574d11..475625e6 100644
--- a/cli/beacon-hooks/cmd/cline_event_test.go
+++ b/cli/beacon-hooks/cmd/cline_event_test.go
@@ -565,6 +565,18 @@ func TestClineWorkspacePathIsHostIndependent(t *testing.T) {
{name: "no root leaves the path relative", path: "src/app.ts", root: "", want: "src/app.ts"},
{name: "empty path stays empty", path: "", root: "/repo", want: ""},
{name: "quoted path is unquoted first", path: `"src/app.ts"`, root: "/repo", want: "/repo/src/app.ts"},
+ // Traversal is the case detection cares about most: stored uncollapsed, "/repo/../../.ssh/id_rsa"
+ // names the same file as "/home/u/.ssh/id_rsa" and matches no rule written against either.
+ {name: "traversal is collapsed", path: "../sibling/a.ts", root: "/repo/pkg", want: "/repo/sibling/a.ts"},
+ {name: "traversal above the root resolves lexically", path: "../../.ssh/id_rsa", root: "/repo", want: "/.ssh/id_rsa"},
+ {name: "dot segments in the middle are collapsed", path: "src/./util/../app.ts", root: "/repo", want: "/repo/src/app.ts"},
+ // A drive prefix cannot go through path.Clean: it treats "C:" as an ordinary segment that
+ // ".." can walk past, which would turn a drive path into a relative one.
+ {name: "traversal under a drive root keeps the drive", path: `..\other\a.ts`, root: `C:\repo\pkg`, want: `C:\repo\other\a.ts`},
+ {name: "traversal above a drive root keeps the drive", path: `..\..\..\x.ts`, root: `C:\repo`, want: `C:\x.ts`},
+ // A leading "//" is a share, not a doubled separator, and path.Clean would collapse it.
+ {name: "unc share survives cleaning", path: `..\b.ts`, root: `\\server\share\repo\pkg`, want: `\\server\share\repo\b.ts`},
+
{name: "dotdot segments are collapsed", path: "../../.ssh/id_rsa", root: "/home/u/proj", want: "/home/.ssh/id_rsa"},
{name: "single dot segments are collapsed", path: "./src/./app.ts", root: "/repo", want: "/repo/src/app.ts"},
{name: "mixed traversal is collapsed", path: "src/../lib/app.ts", root: "/repo", want: "/repo/lib/app.ts"},
@@ -680,3 +692,50 @@ func TestClineEventTaskErrorStaysAnError(t *testing.T) {
t.Errorf("error.type = %q, want provider_timeout", got)
}
}
+
+// 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.
+func TestClineCancelReasonReadsBothShapes(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ input map[string]interface{}
+ want string
+ }{
+ {
+ name: "top level status",
+ input: map[string]interface{}{"completionStatus": "abandoned"},
+ want: "abandoned",
+ },
+ {
+ name: "nested under taskCancel.taskMetadata",
+ input: map[string]interface{}{
+ "taskCancel": map[string]interface{}{
+ "taskMetadata": map[string]interface{}{"completionStatus": "abandoned"},
+ },
+ },
+ want: "abandoned",
+ },
+ {
+ name: "nested under taskMetadata",
+ input: map[string]interface{}{"taskMetadata": map[string]interface{}{"completionStatus": "abandoned"}},
+ want: "abandoned",
+ },
+ {
+ name: "top level wins over nested",
+ input: map[string]interface{}{"reason": "cancelled", "taskMetadata": map[string]interface{}{"completionStatus": "abandoned"}},
+ want: "cancelled",
+ },
+ {
+ name: "falls back to cancelled",
+ input: map[string]interface{}{},
+ want: "cancelled",
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := clineCancelReason(tc.input); got != tc.want {
+ t.Errorf("clineCancelReason() = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}