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(hooks): resolve Cline paths by the payload's convention, not the host's
Claudeda30f4681916
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.goindex e4ddd469..8fac1dbf 100644--- a/cli/beacon-hooks/cmd/cline_event.go+++ b/cli/beacon-hooks/cmd/cline_event.go@@ -398,12 +398,59 @@ func clineToolMessage(action string) string {//// Joining is skipped when the path is already absolute, and when no workspace root was resolved:// a wrong root would be worse than a relative path, because it names a file that was never touched.+//+// Deliberately not implemented with filepath.IsAbs and filepath.Join, which answer for the host+// Beacon is running on rather than for the path in the payload. Those are different questions here:+// the hook binary runs on the host while Cline may be addressing a workspace somewhere else, which+// is the ordinary case for VS Code Remote and WSL. On Windows, filepath.IsAbs("/home/u/repo/a.ts")+// is false, so an absolute POSIX path was joined onto the workspace root and recorded as+// "\\tmp\\project\\home\\u\\repo\\a.ts" -- a file nothing touched. Answering for the+// path itself also makes the result identical on every host, which is what lets one test pin it.func clineWorkspacePath(path, root string) string {path = hookdiff.NormalizePath(path)- if path == "" || root == "" || filepath.IsAbs(path) {+ if path == "" || root == "" || isRootedPath(path) {return path}- return filepath.Join(root, path)+ return joinWorkspacePath(root, path)+}++// isRootedPath reports whether a path names a location without needing a base, under either+// platform's convention.+//+// Both conventions, always, because the payload decides the spelling and the host does not. A+// Windows path treated as relative and a POSIX path treated as relative fail the same way: the+// workspace root is prepended and the event names a file that was never touched.+func isRootedPath(path string) bool {+ switch {+ case path == "":+ return false+ // POSIX absolute, a UNC share, and a Windows path rooted on the current volume.+ case path[0] == '/' || path[0] == '\\':+ return true+ // A drive-qualified Windows path: C:\repo or C:/repo. "C:repo" is deliberately excluded --+ // it is relative to that drive's working directory, not rooted.+ case len(path) >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/'):+ letter := path[0] | 0x20+ return letter >= 'a' && letter <= 'z'+ default:+ return false+ }+}++// joinWorkspacePath joins a relative path under a root using the root's own separator.+//+// filepath.Join would use the host's, which mangles the result whenever the two disagree: a POSIX+// workspace root on a Windows host produced "\\tmp\\project\\src\\app.ts", a path that+// matches nothing and belongs to no filesystem. Taking the separator from the root keeps the+// recorded path in the same shape as the workspace it came from.+func joinWorkspacePath(root, rel string) string {+ separator := "/"+ if strings.Contains(root, "\\") && !strings.Contains(root, "/") {+ separator = "\\"+ }+ rel = strings.TrimPrefix(strings.TrimPrefix(rel, "./"), ".\\")+ rel = strings.ReplaceAll(strings.ReplaceAll(rel, "\\", separator), "/", separator)+ return strings.TrimRight(root, "/\\") + separator + strings.TrimLeft(rel, "/\\")}func clineToolPath(toolInput map[string]interface{}, root string) string {diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.goindex d5468d27..b25644f3 100644--- a/cli/beacon-hooks/cmd/cline_event_test.go+++ b/cli/beacon-hooks/cmd/cline_event_test.go@@ -172,13 +172,17 @@ func TestClineEventResolvesWorkspaceRelativePaths(t *testing.T) {want string}{{+ // A literal expectation, not filepath.Join: the result must be the same on every host,+ // because the workspace the path belongs to is not necessarily the host's.name: "relative path is joined to the workspace root",roots: []interface{}{"/tmp/project"},path: "src/app.ts",- want: filepath.Join("/tmp/project", "src/app.ts"),+ want: "/tmp/project/src/app.ts",},{- name: "absolute path is left alone",+ // filepath.IsAbs says false for this on Windows, so the old implementation joined it and+ // recorded \tmp\project\etc\hosts -- a file nothing touched.+ name: "absolute POSIX path is left alone",roots: []interface{}{"/tmp/project"},path: "/etc/hosts",want: "/etc/hosts",@@ -216,7 +220,9 @@ func TestClineEventWriteToFileRecordsDiff(t *testing.T) {event := clineEventWithAction(t, logPath, "file.modified")file := nested(t, event, "file")- if got := file["path"]; got != filepath.Join("/Users/example/projects/beacon-demo", "src/health.ts") {+ // Literal, not filepath.Join: the fixture's workspace is a POSIX one, and the recorded path must+ // keep that shape whichever host the hook binary runs on.+ if got := file["path"]; got != "/Users/example/projects/beacon-demo/src/health.ts" {t.Errorf("file.path = %q, want the resolved workspace path", got)}// write_to_file creates a file. diffFields records every diff as a modification, so a create@@ -525,3 +531,41 @@ func TestClineToolActionDoesNotMatchSubstrings(t *testing.T) {}}}++// The workspace a Cline path belongs to is not necessarily the host Beacon runs on -- VS Code Remote+// and WSL are the ordinary cases -- so this resolution answers for the path, not for the host, and+// every row below holds identically on Linux, macOS, and Windows.+func TestClineWorkspacePathIsHostIndependent(t *testing.T) {+ for _, tc := range []struct {+ name string+ path string+ root string+ want string+ }{+ {name: "posix relative under posix root", path: "src/app.ts", root: "/repo", want: "/repo/src/app.ts"},+ {name: "windows relative under windows root", path: `src\app.ts`, root: `C:\repo`, want: `C:\repo\src\app.ts`},+ // Mixed spellings happen: the payload and the workspace root do not have to agree, and the+ // recorded path should match the root's shape rather than becoming a mixture.+ {name: "posix relative under windows root", path: "src/app.ts", root: `C:\repo`, want: `C:\repo\src\app.ts`},+ {name: "windows relative under posix root", path: `src\app.ts`, root: "/repo", want: "/repo/src/app.ts"},+ {name: "dot prefix is dropped", path: "./src/app.ts", root: "/repo", want: "/repo/src/app.ts"},+ {name: "trailing root separator is not doubled", path: "src/app.ts", root: "/repo/", want: "/repo/src/app.ts"},+ {name: "posix absolute is left alone", path: "/etc/hosts", root: `C:\repo`, want: "/etc/hosts"},+ {name: "drive absolute is left alone", path: `C:\Windows\hosts`, root: "/repo", want: `C:\Windows\hosts`},+ {name: "drive absolute with forward slashes is left alone", path: "D:/data/a.ts", root: "/repo", want: "D:/data/a.ts"},+ {name: "unc path is left alone", path: `\\server\share\a.ts`, root: "/repo", want: `\\server\share\a.ts`},+ {name: "volume-rooted path is left alone", path: `\Users\x\a.ts`, root: "/repo", want: `\Users\x\a.ts`},+ // Drive-relative, which names a location only in the context of that drive's working+ // directory. Not rooted, so it is resolved like any other relative path.+ {name: "drive relative is treated as relative", path: "C:src/app.ts", root: "/repo", want: "/repo/C:src/app.ts"},+ {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"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ if got := clineWorkspacePath(tc.path, tc.root); got != tc.want {+ t.Errorf("clineWorkspacePath(%q, %q) = %q, want %q", tc.path, tc.root, got, tc.want)+ }+ })+ }+}