Skip to content

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

fix(telemetry): stop applying MCP alias rules to commands and paths (#983)

Rudy Mizrahi Celekli0da032418cb7
diff --git a/pkg/asymptoteobserve/dedupe.go b/pkg/asymptoteobserve/dedupe.go
index d6079353..75750e3c 100644
--- a/pkg/asymptoteobserve/dedupe.go
+++ b/pkg/asymptoteobserve/dedupe.go
@@ -271,7 +271,7 @@ func dedupeTarget(action string, event map[string]interface{}) string {
),
)
case "command.executed":
- return canonicalTarget("command", nestedString(event, "command", "command"), nestedString(event, "tool", "command"))
+ return canonicalLiteralTarget("command", nestedString(event, "command", "command"), nestedString(event, "tool", "command"))
case "file.read", "file.modified":
// The path alone, deliberately. file.operation is the one part of a file
// event the two capture paths describe differently for the same action:
@@ -285,7 +285,7 @@ func dedupeTarget(action string, event map[string]interface{}) string {
// between two words for the same event -- so it discriminated nothing
// and disagreed often. The sandbox checker's mirror of this key has
// always used the path alone.
- return canonicalTarget("file", nestedString(event, "file", "path"))
+ return canonicalLiteralTarget("file", nestedString(event, "file", "path"))
case "tool.completed":
return canonicalTarget("tool.completed", stringValue(event["model"]), stringValue(event["message"]))
case "tool.invoked", "tool.failed":
@@ -295,13 +295,37 @@ func dedupeTarget(action string, event map[string]interface{}) string {
}
}
+// canonicalTarget builds a dedupe key from values that name an MCP server or
+// tool. Those arrive spelled differently from the two capture paths -- a Claude
+// Code tool name reaches the hook as "mcp__server__tool" and the collector
+// reports the server and tool separately -- so the prefix and "__" separator
+// are folded away to let one call match itself.
func canonicalTarget(prefix string, values ...string) string {
+ return joinCanonicalTarget(prefix, values, true)
+}
+
+// canonicalLiteralTarget builds a dedupe key from free-form text: a shell
+// command line or a filesystem path. It folds case, because the two capture
+// paths can spell the same command or directory differently, but leaves the
+// text otherwise intact.
+//
+// The MCP alias rules canonicalTarget applies have no meaning here: "__" is an
+// ordinary character sequence in a command argument or a path segment, and
+// rewriting it to ":" makes the key lossy about input it was never meant to
+// interpret.
+func canonicalLiteralTarget(prefix string, values ...string) string {
+ return joinCanonicalTarget(prefix, values, false)
+}
+
+func joinCanonicalTarget(prefix string, values []string, aliasMCPNames bool) string {
var parts []string
for _, value := range values {
value = strings.ToLower(strings.TrimSpace(value))
- value = strings.TrimPrefix(value, "mcp:")
- value = strings.TrimPrefix(value, "mcp__")
- value = strings.ReplaceAll(value, "__", ":")
+ if aliasMCPNames {
+ value = strings.TrimPrefix(value, "mcp:")
+ value = strings.TrimPrefix(value, "mcp__")
+ value = strings.ReplaceAll(value, "__", ":")
+ }
if value != "" && value != "<nil>" {
parts = append(parts, value)
}
diff --git a/pkg/asymptoteobserve/dedupe_test.go b/pkg/asymptoteobserve/dedupe_test.go
index ee77d4fd..d834f64c 100644
--- a/pkg/asymptoteobserve/dedupe_test.go
+++ b/pkg/asymptoteobserve/dedupe_test.go
@@ -169,3 +169,58 @@ func TestIsDuplicateEndpointEventKeepsEditsToDifferentFiles(t *testing.T) {
t.Fatal("two files edited by one MultiEdit are two edits and must both survive")
}
}
+
+// "__" is ordinary text in a command or a path, not an MCP separator, so two
+// different commands or files must not collapse because one spells ":" where
+// the other has "__".
+func TestIsDuplicateEndpointEventKeepsUnderscoresInCommandsAndPaths(t *testing.T) {
+ for _, tc := range []struct{ action, hook, collector string }{
+ {"command.executed", `"command":{"command":"echo a__b"}`, `"command":{"command":"echo a:b"}`},
+ {"file.modified", `"file":{"path":"/repo/x__y.py"}`, `"file":{"path":"/repo/x:y.py"}`},
+ } {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ hook := `{"timestamp":"2026-08-21T18:00:01Z","event":{"action":"` + tc.action + `"},"harness":{"name":"claude_code"},"session":{"id":"s1","working_directory":"/repo"},` + tc.hook + `}`
+ collector := []byte(`{"timestamp":"2026-08-21T18:00:01Z","event":{"action":"` + tc.action + `"},"harness":{"name":"cursor"},"session":{"id":"s1","working_directory":"/repo"},` + tc.collector + `}`)
+ if err := os.WriteFile(path, []byte(hook+"\n"), 0644); err != nil {
+ t.Fatalf("write existing event: %v", err)
+ }
+ if IsDuplicateEndpointEvent(path, collector, EndpointDuplicateWindow) {
+ t.Errorf("%s: %s and %s are different and must both survive", tc.action, tc.hook, tc.collector)
+ }
+ }
+}
+
+// The alias rules still have to apply where they came from, or an MCP call
+// reported as "mcp__server__tool" by the hook stops matching the collector's
+// separate server and tool fields.
+func TestDedupeTargetStillAliasesMCPNames(t *testing.T) {
+ hook := map[string]interface{}{
+ "mcp": map[string]interface{}{"server": "clickhouse"},
+ "tool": map[string]interface{}{"name": "mcp__clickhouse__execute_sql"},
+ }
+ collector := map[string]interface{}{
+ "mcp": map[string]interface{}{"server": "clickhouse", "tool": "clickhouse:execute_sql"},
+ }
+
+ hookTarget := dedupeTarget("mcp.tool_invoked", hook)
+ collectorTarget := dedupeTarget("mcp.tool_invoked", collector)
+ if hookTarget != collectorTarget {
+ t.Fatalf("MCP targets diverged: hook %q, collector %q", hookTarget, collectorTarget)
+ }
+}
+
+// Case folding is deliberately kept: the two capture paths of one call can
+// spell the same directory with different case, and folding is what lets them
+// match. Only the alias rewriting is scoped out.
+func TestIsDuplicateEndpointEventStillFoldsCommandCase(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ hook := `{"timestamp":"2026-08-21T18:00:01Z","event":{"action":"command.executed"},"harness":{"name":"claude_code"},"session":{"id":"s1","working_directory":"/repo"},"command":{"command":"Printf A"}}`
+ collector := []byte(`{"timestamp":"2026-08-21T18:00:01Z","event":{"action":"command.executed"},"harness":{"name":"cursor"},"session":{"id":"s1","working_directory":"/repo"},"command":{"command":"printf a"}}`)
+ if err := os.WriteFile(path, []byte(hook+"\n"), 0644); err != nil {
+ t.Fatalf("write existing event: %v", err)
+ }
+
+ if !IsDuplicateEndpointEvent(path, collector, EndpointDuplicateWindow) {
+ t.Fatal("one command reported by both paths should still collapse across case")
+ }
+}