Skip to content

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

fix(telemetry): drop the uri slash before a windows drive in mcp file paths (#1072)

Rudy Mizrahi Celeklie4bf0dd521a4
diff --git a/cli/beacon/internal/gitlink/git.go b/cli/beacon/internal/gitlink/git.go
index 5c2357d8..ea689713 100644
--- a/cli/beacon/internal/gitlink/git.go
+++ b/cli/beacon/internal/gitlink/git.go
@@ -18,6 +18,8 @@ import (
"os/exec"
"path/filepath"
"strings"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
// Git runs git against one repository. Dir is any directory inside the work tree.
@@ -188,7 +190,12 @@ func (r Repo) RelPath(path, base string) (string, bool) {
if path == "" {
return "", false
}
- path = strings.TrimPrefix(path, "file://")
+ if strings.HasPrefix(path, "file:") {
+ if path = asymptoteobserve.FilePathFromURI(path); path == "" {
+ return "", false
+ }
+ path = filepath.FromSlash(path)
+ }
if !filepath.IsAbs(path) {
if base == "" || !filepath.IsAbs(base) {
return "", false
diff --git a/cli/beacon/internal/gitlink/relpath_test.go b/cli/beacon/internal/gitlink/relpath_test.go
new file mode 100644
index 00000000..a4beb022
--- /dev/null
+++ b/cli/beacon/internal/gitlink/relpath_test.go
@@ -0,0 +1,23 @@
+package gitlink
+
+import (
+ "net/url"
+ "path/filepath"
+ "testing"
+)
+
+// Editors and MCP tools report files as file: URIs. They name the same file as the plain path.
+func TestRelPathReadsFileURIs(t *testing.T) {
+ r := newTestRepo(t)
+ repo := r.open()
+ file := filepath.Join(repo.Root, "docs", "My Notes.md")
+ uri := (&url.URL{Scheme: "file", Path: filepath.ToSlash(file)}).String()
+ for _, path := range []string{file, uri, "file://localhost" + (&url.URL{Path: filepath.ToSlash(file)}).EscapedPath()} {
+ if rel, ok := repo.RelPath(path, ""); !ok || rel != "docs/My Notes.md" {
+ t.Errorf("RelPath(%q) = %q, %v; want docs/My Notes.md", path, rel, ok)
+ }
+ }
+ if rel, ok := repo.RelPath("file://elsewhere/share/x.md", repo.Root); ok {
+ t.Errorf("a file on another host was placed in the repository as %q", rel)
+ }
+}
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
index c54332e7..1bd45426 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
@@ -1213,7 +1213,7 @@ func (c Converter) PopulateCommon(event *Event, attrs map[string]interface{}) {
path := FirstString(attrs, "file.path", "file_path", "code.filepath")
operation := FirstString(attrs, "file.operation", "operation")
if path == "" {
- path = FilePathFromURI(FirstString(attrs, "mcp.resource.uri"))
+ path = asymptoteobserve.FilePathFromURI(FirstString(attrs, "mcp.resource.uri"))
if path != "" && operation == "" && event.Event.Action == "file.read" {
operation = "read"
}
@@ -2233,18 +2233,7 @@ func IsMeaningfulValue(value interface{}) bool {
}
func IsFileURI(value string) bool {
- return FilePathFromURI(value) != ""
-}
-
-func FilePathFromURI(value string) string {
- parsed, err := url.Parse(value)
- if err != nil || parsed.Scheme != "file" {
- return ""
- }
- if parsed.Host != "" && parsed.Host != "localhost" {
- return "//" + parsed.Host + parsed.Path
- }
- return parsed.Path
+ return asymptoteobserve.FilePathFromURI(value) != ""
}
func HasPromptLikeContent(attrs map[string]interface{}) bool {
diff --git a/pkg/asymptoteobserve/fileuri.go b/pkg/asymptoteobserve/fileuri.go
new file mode 100644
index 00000000..0f669b27
--- /dev/null
+++ b/pkg/asymptoteobserve/fileuri.go
@@ -0,0 +1,20 @@
+package asymptoteobserve
+
+import "net/url"
+
+// FilePathFromURI returns the path a file: URI names, and "" for anything that is not one. A
+// host other than localhost is a UNC share (//host/path). The slash before a Windows drive letter
+// is URI syntax, so file:///C:/Users/x names C:/Users/x. Percent-escapes are decoded.
+func FilePathFromURI(value string) string {
+ parsed, err := url.Parse(value)
+ if err != nil || parsed.Scheme != "file" {
+ return ""
+ }
+ if parsed.Host != "" && parsed.Host != "localhost" {
+ return "//" + parsed.Host + parsed.Path
+ }
+ if p := parsed.Path; len(p) >= 3 && p[0] == '/' && p[2] == ':' && (p[1]|0x20 >= 'a' && p[1]|0x20 <= 'z') {
+ return p[1:]
+ }
+ return parsed.Path
+}
diff --git a/pkg/asymptoteobserve/fileuri_test.go b/pkg/asymptoteobserve/fileuri_test.go
new file mode 100644
index 00000000..511de163
--- /dev/null
+++ b/pkg/asymptoteobserve/fileuri_test.go
@@ -0,0 +1,19 @@
+package asymptoteobserve
+
+import "testing"
+
+func TestFilePathFromURI(t *testing.T) {
+ for uri, want := range map[string]string{
+ "file:///tmp/report.md": "/tmp/report.md",
+ "file://localhost/tmp/report.md": "/tmp/report.md",
+ "file:///C:/Users/dev/.aws/credentials": "C:/Users/dev/.aws/credentials",
+ "file:///c%3A/Users/dev/My%20Notes.md": "c:/Users/dev/My Notes.md",
+ "file://server/share/report.md": "//server/share/report.md",
+ "https://example.com/C:/not-a-file-path": "",
+ "/tmp/plain/path": "",
+ } {
+ if got := FilePathFromURI(uri); got != want {
+ t.Errorf("FilePathFromURI(%q) = %q, want %q", uri, got, want)
+ }
+ }
+}