Skip to content

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

fix duplicate events

Justin D'Souzab44dacc0156d
diff --git a/cli/beacon-hooks/cmd/endpoint_events.go b/cli/beacon-hooks/cmd/endpoint_events.go
index 8f808288..eff3da05 100644
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@ -180,6 +180,15 @@ func toolFieldsWithResponse(toolName string, toolInput, toolResponse map[string]
}
func mcpToolFields(toolName string, toolInput, toolResponse map[string]interface{}) map[string]interface{} {
+ maps := []map[string]interface{}{toolInput, toolResponse}
+ mcpServer := firstToolStringAcross(maps, "mcp_server", "mcp_server_name", "mcp.server", "mcp.server.name")
+ mcpTool := firstToolStringAcross(maps, "mcp_tool", "mcp_tool_name", "mcp.tool", "mcp.tool.name")
+ mcpMethod := firstToolStringAcross(maps, "mcp.method.name", "mcp_method_name", "mcp_method")
+ mcpProtocol := firstToolStringAcross(maps, "mcp.protocol.version", "mcp_protocol_version")
+ mcpResource := firstToolStringAcross(maps, "mcp.resource.uri", "mcp_resource_uri")
+ mcpSession := firstToolStringAcross(maps, "mcp.session.id", "mcp_session_id")
+ hasCascadeServerToolPair := isCascadePlatform(platformFlag) && firstToolStringAcross(maps, "server_name") != "" && firstToolStringAcross(maps, "tool_name") != ""
+
server := firstToolStringAcross([]map[string]interface{}{toolInput, toolResponse}, "server", "server_name", "mcp_server", "mcp_server_name", "mcp.server", "mcp.server.name")
tool := firstToolStringAcross([]map[string]interface{}{toolInput, toolResponse}, "tool", "tool_name", "function_name", "mcp_tool", "mcp_tool_name", "mcp.tool", "mcp.tool.name", "gen_ai.tool.name")
genericToolName := firstToolStringAcross([]map[string]interface{}{toolInput, toolResponse}, "name")
@@ -197,7 +206,7 @@ func mcpToolFields(toolName string, toolInput, toolResponse map[string]interface
}
}
- isMCP := server != "" || tool != "" || method != "" || protocol != "" || resource != "" || session != "" || strings.Contains(strings.ToLower(toolName), "mcp")
+ isMCP := mcpServer != "" || mcpTool != "" || mcpMethod != "" || mcpProtocol != "" || mcpResource != "" || mcpSession != "" || hasCascadeServerToolPair || strings.Contains(strings.ToLower(toolName), "mcp")
if !isMCP {
return nil
}
@@ -323,12 +332,23 @@ func mcpErrorType(toolResponse map[string]interface{}) string {
if isError := firstToolString(toolResponse, "isError", "is_error"); strings.EqualFold(isError, "true") {
return "tool_error"
}
- if errorText := firstToolString(toolResponse, "error"); errorText != "" {
+ if mcpHasErrorValue(toolResponse, "error") {
return "tool_error"
}
return ""
}
+func mcpHasErrorValue(toolResponse map[string]interface{}, key string) bool {
+ value, ok := toolResponse[key]
+ if !ok || value == nil {
+ return false
+ }
+ if boolValue, ok := value.(bool); ok {
+ return boolValue
+ }
+ return normalizeToolString(value) != ""
+}
+
func diffFields(filePath, diffStr string) map[string]interface{} {
if filePath == "" {
return nil
diff --git a/cli/beacon-hooks/cmd/endpoint_events_test.go b/cli/beacon-hooks/cmd/endpoint_events_test.go
index 0de4e41c..ffe74e6e 100644
--- a/cli/beacon-hooks/cmd/endpoint_events_test.go
+++ b/cli/beacon-hooks/cmd/endpoint_events_test.go
@@ -105,6 +105,29 @@ func TestToolFieldsMapsMCPTransportContext(t *testing.T) {
}
}
+func TestToolFieldsIgnoresFalseMCPErrorField(t *testing.T) {
+ fields := toolFieldsWithResponse("MCP:get_organizations", map[string]interface{}{}, map[string]interface{}{
+ "error": false,
+ "ok": true,
+ })
+ if _, ok := fields["error"]; ok {
+ t.Fatalf("fields should not include error for false MCP error field: %#v", fields["error"])
+ }
+}
+
+func TestToolFieldsMapsTruthyMCPErrorField(t *testing.T) {
+ fields := toolFieldsWithResponse("MCP:get_organizations", map[string]interface{}{}, map[string]interface{}{
+ "error": true,
+ })
+ errorFields, ok := fields["error"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("fields error missing or wrong type: %#v", fields["error"])
+ }
+ if got := errorFields["type"]; got != "tool_error" {
+ t.Fatalf("error.type = %q, want tool_error", got)
+ }
+}
+
func TestToolFieldsDoesNotTreatGenericNameAsMCP(t *testing.T) {
fields := toolFields("Write", map[string]interface{}{"name": "README.md"})
if _, ok := fields["mcp"]; ok {
@@ -115,6 +138,58 @@ func TestToolFieldsDoesNotTreatGenericNameAsMCP(t *testing.T) {
}
}
+func TestToolFieldsDoesNotTreatGenericFlatTransportKeysAsMCP(t *testing.T) {
+ fields := toolFieldsWithResponse("HTTPClient", map[string]interface{}{
+ "method": "GET",
+ "protocol_version": "HTTP/2",
+ "uri": "https://example.com/report",
+ }, map[string]interface{}{
+ "status_code": "200",
+ })
+ if _, ok := fields["mcp"]; ok {
+ t.Fatalf("non-MCP tool should not include mcp fields: %#v", fields)
+ }
+ if _, ok := fields["gen_ai"]; ok {
+ t.Fatalf("non-MCP tool should not include gen_ai fields: %#v", fields)
+ }
+}
+
+func TestToolFieldsDoesNotTreatGenericServerToolPairAsMCP(t *testing.T) {
+ oldPlatform := platformFlag
+ defer func() {
+ platformFlag = oldPlatform
+ }()
+ platformFlag = "cursor"
+ fields := toolFields("HTTPClient", map[string]interface{}{
+ "server_name": "api",
+ "tool_name": "request",
+ })
+ if _, ok := fields["mcp"]; ok {
+ t.Fatalf("non-MCP tool should not include mcp fields: %#v", fields)
+ }
+ if _, ok := fields["gen_ai"]; ok {
+ t.Fatalf("non-MCP tool should not include gen_ai fields: %#v", fields)
+ }
+}
+
+func TestToolFieldsMapsGenericFlatTransportKeysForKnownMCP(t *testing.T) {
+ fields := toolFields("MCP:read_resource", map[string]interface{}{
+ "method": "resources/read",
+ "protocol_version": "2025-06-18",
+ "uri": "file:///tmp/report.md",
+ })
+ mcp := fields["mcp"].(map[string]interface{})
+ if got := mcp["method"].(map[string]interface{})["name"]; got != "resources/read" {
+ t.Fatalf("mcp.method.name = %q, want resources/read", got)
+ }
+ if got := mcp["protocol"].(map[string]interface{})["version"]; got != "2025-06-18" {
+ t.Fatalf("mcp.protocol.version = %q, want 2025-06-18", got)
+ }
+ if got := mcp["resource"].(map[string]interface{})["uri"]; got != "file:///tmp/report.md" {
+ t.Fatalf("mcp.resource.uri = %q, want file URI", got)
+ }
+}
+
func TestMaybeEmitInventoryHeartbeatInvokesBeaconCLI(t *testing.T) {
oldPlatform := platformFlag
oldRunner := runInventoryHeartbeatCommand
diff --git a/cli/beacon-hooks/internal/logging/endpoint_event_test.go b/cli/beacon-hooks/internal/logging/endpoint_event_test.go
index 7bd9b0b2..397955bc 100644
--- a/cli/beacon-hooks/internal/logging/endpoint_event_test.go
+++ b/cli/beacon-hooks/internal/logging/endpoint_event_test.go
@@ -148,6 +148,28 @@ func TestEndpointEventRotatesRuntimeLog(t *testing.T) {
}
}
+func TestAppendEndpointJSONLDedupesRuntimeEvents(t *testing.T) {
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ existing := []byte(`{"timestamp":"2026-06-18T21:11:24Z","event":{"action":"mcp.tool_invoked"},"harness":{"name":"cursor"},"session":{"id":"s1"},"mcp":{"server":"clickhouse","tool":"execute_sql"},"message":"Tool execution observed"}` + "\n")
+ candidate := []byte(`{"timestamp":"2026-06-18T21:11:25Z","event":{"action":"mcp.tool_invoked"},"harness":{"name":"claude"},"session":{"id":"s1"},"mcp":{"server":"clickhouse","tool":"execute_sql"},"message":"Tool execution observed"}` + "\n")
+
+ if err := appendEndpointJSONL(logPath, existing, defaultEndpointRotateBytes, defaultEndpointRotateArchives); err != nil {
+ t.Fatalf("first appendEndpointJSONL returned error: %v", err)
+ }
+ if err := appendEndpointJSONL(logPath, candidate, defaultEndpointRotateBytes, defaultEndpointRotateArchives); err != nil {
+ t.Fatalf("second appendEndpointJSONL returned error: %v", err)
+ }
+
+ data, err := os.ReadFile(logPath)
+ if err != nil {
+ t.Fatalf("read endpoint log: %v", err)
+ }
+ lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+ if len(lines) != 1 {
+ t.Fatalf("expected duplicate event to be suppressed, got %d lines: %s", len(lines), string(data))
+ }
+}
+
func TestEndpointEventSurfacesWriteFailure(t *testing.T) {
dir := t.TempDir()
logPath := filepath.Join(dir, "runtime.jsonl")
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.go
index 15c95d7c..f712f462 100644
--- a/cli/beacon-hooks/internal/logging/logging.go
+++ b/cli/beacon-hooks/internal/logging/logging.go
@@ -283,6 +283,9 @@ func appendEndpointJSONL(path string, line []byte, rotateBytes int64, rotateArch
}
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
defer lock.Close()
+ if asymptoteobserve.IsDuplicateEndpointEvent(path, line, asymptoteobserve.EndpointDuplicateWindow) {
+ return nil
+ }
if err := rotateEndpointLogIfNeeded(path, rotateBytes, rotateArchives, int64(len(line))); err != nil {
return err
}
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index 5312cb00..e28f1fed 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -117,6 +117,9 @@ func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int
}
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
defer lock.Close()
+ if asymptoteobserve.IsDuplicateEndpointEvent(path, line, asymptoteobserve.EndpointDuplicateWindow) {
+ return nil
+ }
if err := rotateIfNeeded(path, rotateBytes, rotateArchives, int64(len(line))); err != nil {
return err
}
diff --git a/cli/beacon/internal/endpoint/writer/writer_test.go b/cli/beacon/internal/endpoint/writer/writer_test.go
index f24d4671..b3674672 100644
--- a/cli/beacon/internal/endpoint/writer/writer_test.go
+++ b/cli/beacon/internal/endpoint/writer/writer_test.go
@@ -149,6 +149,37 @@ func TestAppendEventPrunesNumberedArchives(t *testing.T) {
}
}
+func TestAppendEventDedupesRuntimeEvents(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ event := schema.NewEvent(schema.NewEventOptions{
+ Action: "mcp.tool_invoked",
+ Harness: schema.HarnessInfo{Name: "cursor"},
+ Message: "Tool execution observed",
+ })
+ event.Timestamp = "2026-06-18T21:11:24Z"
+ event.Session = &schema.SessionInfo{ID: "s1"}
+ event.MCP = &schema.MCPInfo{Server: "clickhouse", Tool: "execute_sql"}
+ duplicate := event
+ duplicate.Harness.Name = "claude"
+ duplicate.Timestamp = "2026-06-18T21:11:25Z"
+
+ if _, err := AppendEvent(event, Options{Path: path}); err != nil {
+ t.Fatalf("first AppendEvent returned error: %v", err)
+ }
+ if _, err := AppendEvent(duplicate, Options{Path: path}); err != nil {
+ t.Fatalf("second AppendEvent returned error: %v", err)
+ }
+
+ data, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("read log: %v", err)
+ }
+ lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+ if len(lines) != 1 {
+ t.Fatalf("expected duplicate event to be suppressed, got %d lines: %s", len(lines), string(data))
+ }
+}
+
func TestAppendEventSerializesConcurrentWrites(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
event := schema.NewEvent(schema.NewEventOptions{
diff --git a/collector-builder/exporter/beaconjsonexporter/exporter_test.go b/collector-builder/exporter/beaconjsonexporter/exporter_test.go
index 9ded5945..64ea8b29 100644
--- a/collector-builder/exporter/beaconjsonexporter/exporter_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/exporter_test.go
@@ -126,6 +126,40 @@ func TestJSONLWriterRotatesAndPrunesArchives(t *testing.T) {
}
}
+func TestJSONLWriterDedupesRuntimeEvents(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ writer := jsonlWriter{
+ path: path,
+ maxEventBytes: defaultMaxEventBytes,
+ rotateBytes: defaultRotateBytes,
+ rotateArchives: defaultRotateArchives,
+ redactSecrets: true,
+ }
+ event := newBeaconEvent("mcp.tool_invoked", "mcp", "info", "cursor", time.Date(2026, 6, 18, 21, 11, 24, 0, time.UTC))
+ event.Message = "Tool execution observed"
+ event.Session = &sessionInfo{ID: "s1"}
+ event.MCP = &mcpInfo{Server: "clickhouse", Tool: "execute_sql"}
+ duplicate := event
+ duplicate.Harness.Name = "claude"
+ duplicate.Timestamp = time.Date(2026, 6, 18, 21, 11, 25, 0, time.UTC).Format(time.RFC3339)
+
+ if err := writer.append(event); err != nil {
+ t.Fatalf("first append returned error: %v", err)
+ }
+ if err := writer.append(duplicate); err != nil {
+ t.Fatalf("second append returned error: %v", err)
+ }
+
+ data, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("read log: %v", err)
+ }
+ lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+ if len(lines) != 1 {
+ t.Fatalf("expected duplicate event to be suppressed, got %d lines: %s", len(lines), string(data))
+ }
+}
+
func TestConsumeLogsWritesBeaconJSONL(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
exp, err := newExporter(&Config{
diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.go
index accecd6f..96a1cc76 100644
--- a/collector-builder/exporter/beaconjsonexporter/writer.go
+++ b/collector-builder/exporter/beaconjsonexporter/writer.go
@@ -138,6 +138,9 @@ func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int
}
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
defer lock.Close()
+ if asymptoteobserve.IsDuplicateEndpointEvent(path, line, asymptoteobserve.EndpointDuplicateWindow) {
+ return nil
+ }
if err := rotateIfNeeded(path, rotateBytes, rotateArchives, int64(len(line))); err != nil {
return err
}
diff --git a/pkg/asymptoteobserve/dedupe.go b/pkg/asymptoteobserve/dedupe.go
index d3db1ce4..e067691d 100644
--- a/pkg/asymptoteobserve/dedupe.go
+++ b/pkg/asymptoteobserve/dedupe.go
@@ -17,9 +17,10 @@ const (
)
type endpointDedupeEvent struct {
- action string
- key string
- ts time.Time
+ action string
+ harness string
+ key string
+ ts time.Time
}
// IsDuplicateEndpointEvent reports whether candidateLine duplicates a recently
@@ -40,6 +41,9 @@ func IsDuplicateEndpointEvent(path string, candidateLine []byte, window time.Dur
if !ok || existing.key != candidate.key {
continue
}
+ if existing.harness == candidate.harness {
+ continue
+ }
diff := candidate.ts.Sub(existing.ts)
if diff < 0 {
diff = -diff
@@ -95,6 +99,7 @@ func endpointDedupeCandidate(line []byte) (endpointDedupeEvent, bool) {
if err != nil || ts.IsZero() {
return endpointDedupeEvent{}, false
}
+ harness := strings.ToLower(nestedString(event, "harness", "name"))
target := dedupeTarget(action, event)
if target == "" {
return endpointDedupeEvent{}, false
@@ -109,7 +114,7 @@ func endpointDedupeCandidate(line []byte) (endpointDedupeEvent, bool) {
strings.ToLower(workspace),
target,
}, "\x00")
- return endpointDedupeEvent{action: strings.ToLower(action), key: key, ts: ts.UTC()}, true
+ return endpointDedupeEvent{action: strings.ToLower(action), harness: harness, key: key, ts: ts.UTC()}, true
}
func endpointDedupeWindow(action string, fallback time.Duration) time.Duration {
diff --git a/pkg/asymptoteobserve/dedupe_test.go b/pkg/asymptoteobserve/dedupe_test.go
index f31ff343..f6f76acd 100644
--- a/pkg/asymptoteobserve/dedupe_test.go
+++ b/pkg/asymptoteobserve/dedupe_test.go
@@ -32,6 +32,19 @@ func TestIsDuplicateEndpointEventKeepsSeparateCallsOutsideWindow(t *testing.T) {
}
}
+func TestIsDuplicateEndpointEventKeepsSameHarnessCalls(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ existing := `{"timestamp":"2026-06-18T21:11:24Z","event":{"action":"mcp.tool_invoked"},"harness":{"name":"cursor"},"session":{"id":"s1"},"mcp":{"server":"clickhouse","tool":"execute_sql"}}`
+ candidate := []byte(`{"timestamp":"2026-06-18T21:11:25Z","event":{"action":"mcp.tool_invoked"},"harness":{"name":"cursor"},"session":{"id":"s1"},"mcp":{"server":"clickhouse","tool":"execute_sql"}}`)
+ if err := os.WriteFile(path, []byte(existing+"\n"), 0644); err != nil {
+ t.Fatalf("write existing event: %v", err)
+ }
+
+ if IsDuplicateEndpointEvent(path, candidate, EndpointDuplicateWindow) {
+ t.Fatal("same-harness events should not dedupe")
+ }
+}
+
func TestIsDuplicateEndpointEventRequiresSession(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
existing := `{"timestamp":"2026-06-18T21:11:24Z","event":{"action":"mcp.tool_invoked"},"harness":{"name":"cursor"},"mcp":{"tool":"list_tables"}}`