Skip to content

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

fix bugs

Justin D'Souzaaa2d148c2b5b
diff --git a/collector-builder/exporter/beaconjsonexporter/exporter_test.go b/collector-builder/exporter/beaconjsonexporter/exporter_test.go
index 2e549f38..ab7745e4 100644
--- a/collector-builder/exporter/beaconjsonexporter/exporter_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/exporter_test.go
@@ -63,6 +63,13 @@ func TestDefaultConfigUsesBoundedRotation(t *testing.T) {
}
}
+func TestIntAttrRejectsOutOfRangeString(t *testing.T) {
+ attrs := map[string]interface{}{"value": "9223372036854775808"}
+ if got, ok := intAttr(attrs, "value"); ok {
+ t.Fatalf("intAttr() = %d, true; want false for out-of-range value", got)
+ }
+}
+
func TestNewExporterNormalizesRotationDefaults(t *testing.T) {
cfg := &Config{
Path: filepath.Join(t.TempDir(), "runtime.jsonl"),
@@ -299,6 +306,34 @@ func TestEventFromLogKeepsTypedPrompt(t *testing.T) {
}
}
+func TestEventFromLogDoesNotPromoteEmptyGenAIToolArgumentsToTool(t *testing.T) {
+ exp, err := newExporter(&Config{
+ Path: filepath.Join(t.TempDir(), "runtime.jsonl"),
+ MaxEventBytes: defaultMaxEventBytes,
+ RotateBytes: defaultRotateBytes,
+ RedactSecrets: true,
+ }, exporter.Settings{})
+ if err != nil {
+ t.Fatalf("newExporter returned error: %v", err)
+ }
+ rec := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
+ rec.Body().SetStr("chat gpt-4o")
+ rec.Attributes().PutStr("service.name", "openai")
+ rec.Attributes().PutStr("gen_ai.operation.name", "chat")
+ rec.Attributes().PutStr("gen_ai.input.messages", `[{"content":"hello"}]`)
+ rec.Attributes().PutEmptyMap("gen_ai.tool.call.arguments")
+ rec.Attributes().PutStr("gen_ai.tool.call.id", "{}")
+ rec.Attributes().PutStr("gen_ai.request.model", "gpt-4o")
+
+ event := exp.eventFromLog(nil, rec)
+ if event.Event.Action != "prompt.submitted" {
+ t.Fatalf("action = %q, want prompt.submitted", event.Event.Action)
+ }
+ if event.Tool != nil {
+ t.Fatalf("empty structured tool arguments should not create top-level tool: %#v", event.Tool)
+ }
+}
+
func TestConsumeLogsMapsOTelGenAIAttributes(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
exp, err := newExporter(&Config{
@@ -440,6 +475,9 @@ func TestEventFromLogMapsOTelMCPAttributes(t *testing.T) {
if event.Event.Action != "file.read" || event.Event.Category != "file" {
t.Fatalf("MCP resource action/category = %#v, want file.read/file", event.Event)
}
+ if event.File == nil || event.File.Path != "/tmp/report.md" || event.File.Operation != "read" {
+ t.Fatalf("MCP resource file mapping missing: %#v", event.File)
+ }
if event.MCP == nil || event.MCP.Method == nil || event.MCP.Method.Name != "resources/read" || event.MCP.Protocol == nil || event.MCP.Protocol.Version != "2025-06-18" {
t.Fatalf("MCP method/protocol missing: %#v", event.MCP)
}
@@ -832,6 +870,86 @@ func TestInferActionMapsCodexOpAttributeToPrompt(t *testing.T) {
}
}
+func TestInferActionMapsGenAIChatWithEmptyToolArgumentsToPrompt(t *testing.T) {
+ attrs := map[string]interface{}{
+ "gen_ai.operation.name": "chat",
+ "gen_ai.input.messages": []interface{}{map[string]interface{}{"content": "hello"}},
+ "gen_ai.tool.call.arguments": map[string]interface{}{},
+ "gen_ai.tool.call.id": "{}",
+ "gen_ai.request.model": "gpt-4o",
+ "service.name": "openai",
+ }
+ if got := inferAction(attrs, "chat gpt-4o"); got != "prompt.submitted" {
+ t.Fatalf("inferAction() = %q, want prompt.submitted", got)
+ }
+}
+
+func TestInferActionDoesNotMapEmptyGenAIInputMessagesToPrompt(t *testing.T) {
+ cases := map[string]interface{}{
+ "empty map": map[string]interface{}{},
+ "empty array": []interface{}{},
+ "json empty map": "{}",
+ "json empty array": "[]",
+ "null string": "null",
+ "nil placeholder": "<nil>",
+ "blank content": []interface{}{map[string]interface{}{"content": " "}},
+ "non-text content": []interface{}{map[string]interface{}{"content": false}},
+ "empty nested parts": []interface{}{map[string]interface{}{"parts": []interface{}{map[string]interface{}{"content": "\t"}}}},
+ }
+
+ for name, messages := range cases {
+ t.Run(name, func(t *testing.T) {
+ attrs := map[string]interface{}{
+ "gen_ai.operation.name": "chat",
+ "gen_ai.input.messages": messages,
+ "gen_ai.request.model": "gpt-4o",
+ "service.name": "openai",
+ }
+ if got := inferAction(attrs, "chat gpt-4o"); got == "prompt.submitted" {
+ t.Fatalf("inferAction() = %q, want non-prompt action", got)
+ }
+ })
+ }
+}
+
+func TestInferActionDoesNotMapEmptyGenAIPromptToPrompt(t *testing.T) {
+ for _, prompt := range []interface{}{"{}", "[]", "null", "<nil>", map[string]interface{}{}, []interface{}{}} {
+ attrs := map[string]interface{}{
+ "gen_ai.operation.name": "chat",
+ "gen_ai.prompt": prompt,
+ "gen_ai.request.model": "gpt-4o",
+ "service.name": "openai",
+ }
+ if got := inferAction(attrs, "chat gpt-4o"); got == "prompt.submitted" {
+ t.Fatalf("inferAction(%#v) = %q, want non-prompt action", prompt, got)
+ }
+ }
+}
+
+func TestInferActionMapsGenAIGenerateContentWithStructuredToolArgumentsToPrompt(t *testing.T) {
+ attrs := map[string]interface{}{
+ "gen_ai.operation.name": "generate_content",
+ "gen_ai.prompt": "summarize this file",
+ "gen_ai.tool.call.arguments": map[string]interface{}{"path": "README.md"},
+ "gen_ai.request.model": "gemini-2.5-pro",
+ "service.name": "genai-client",
+ }
+ if got := inferAction(attrs, "generate_content"); got != "prompt.submitted" {
+ t.Fatalf("inferAction() = %q, want prompt.submitted", got)
+ }
+}
+
+func TestInferActionMapsMeaningfulToolCallArgumentsToTool(t *testing.T) {
+ attrs := map[string]interface{}{
+ "gen_ai.tool.call.arguments": map[string]interface{}{"path": "README.md"},
+ "gen_ai.request.model": "gpt-4o",
+ "service.name": "openai",
+ }
+ if got := inferAction(attrs, "tool call"); got != "tool.invoked" {
+ t.Fatalf("inferAction() = %q, want tool.invoked", got)
+ }
+}
+
func TestConsumeTracesDropsCodexUserInputSpan(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
exp, err := newExporter(&Config{
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
index 6e273fcc..1a114dfb 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
@@ -405,17 +405,25 @@ func (c Converter) PopulateCommon(event *Event, attrs map[string]interface{}) {
WorkingDirectory: FirstString(attrs, "cwd", "working_directory", "process.command_args.cwd", "workspace"),
}
}
- if name := FirstString(attrs, "tool.name", "gen_ai.tool.name", "mcp.tool.name", "function_name", "tool_name"); name != "" || FirstString(attrs, "tool.command", "command", "function_args", "gen_ai.tool.call.arguments") != "" {
+ if name := FirstString(attrs, "tool.name", "gen_ai.tool.name", "mcp.tool.name", "function_name", "tool_name"); name != "" || ToolCommandString(attrs) != "" {
event.Tool = &ToolInfo{
Name: name,
- Command: FirstString(attrs, "tool.command", "command", "process.command_line", "function_args", "gen_ai.tool.call.arguments"),
+ Command: FirstNonEmpty(ToolCommandString(attrs), FirstString(attrs, "process.command_line")),
Path: FirstString(attrs, "tool.path", "file.path", "file_path"),
}
}
- if path := FirstString(attrs, "file.path", "file_path", "code.filepath"); path != "" {
+ path := FirstString(attrs, "file.path", "file_path", "code.filepath")
+ operation := FirstString(attrs, "file.operation", "operation")
+ if path == "" {
+ path = FilePathFromURI(FirstString(attrs, "mcp.resource.uri"))
+ if path != "" && operation == "" && event.Event.Action == "file.read" {
+ operation = "read"
+ }
+ }
+ if path != "" {
event.File = &FileInfo{
Path: path,
- Operation: FirstString(attrs, "file.operation", "operation"),
+ Operation: operation,
Language: FirstString(attrs, "code.language", "language"),
}
}
@@ -439,7 +447,7 @@ func (c Converter) PopulateCommon(event *Event, attrs map[string]interface{}) {
}
}
if event.Event.Category == "prompt" {
- if text := FirstNonEmpty(FirstString(attrs, "gen_ai.prompt", "prompt", "user_prompt", "input.prompt", "copilot_chat.user_request"), FirstMessageText(event.GenAI)); text != "" {
+ if text := FirstNonEmpty(FirstTextAttr(attrs, "gen_ai.prompt", "prompt", "user_prompt", "input.prompt", "copilot_chat.user_request"), FirstMessageText(event.GenAI)); text != "" {
event.Prompt = &PromptInfo{Text: text}
}
}
@@ -729,6 +737,37 @@ func FirstString(attrs map[string]interface{}, keys ...string) string {
return ""
}
+func ToolCommandString(attrs map[string]interface{}) string {
+ if command := FirstString(attrs, "tool.command", "command", "function_args"); command != "" {
+ return command
+ }
+ return FirstStringAttr(attrs, "gen_ai.tool.call.arguments")
+}
+
+func FirstStringAttr(attrs map[string]interface{}, keys ...string) string {
+ for _, key := range keys {
+ if value, ok := attrs[key]; ok {
+ if str, ok := value.(string); ok {
+ if trimmed := strings.TrimSpace(str); trimmed != "" {
+ return trimmed
+ }
+ }
+ }
+ }
+ return ""
+}
+
+func FirstTextAttr(attrs map[string]interface{}, keys ...string) string {
+ for _, key := range keys {
+ if value, ok := attrs[key]; ok {
+ if text := firstTextFromAny(value); text != "" {
+ return text
+ }
+ }
+ }
+ return ""
+}
+
func HasAttr(attrs map[string]interface{}, key string) bool {
_, ok := attrs[key]
return ok
@@ -744,16 +783,36 @@ func FirstNonEmpty(values ...string) string {
}
func IntAttr(attrs map[string]interface{}, keys ...string) (int, bool) {
- value, ok := Int64Attr(attrs, keys...)
- if !ok {
- return 0, false
+ for _, key := range keys {
+ switch typed := attrs[key].(type) {
+ case int:
+ return typed, true
+ case int64:
+ if !FitsInInt(typed) {
+ continue
+ }
+ return int(typed), true
+ case float64:
+ value := int64(typed)
+ if !FitsInInt(value) {
+ continue
+ }
+ return int(value), true
+ case string:
+ value, err := strconv.Atoi(strings.TrimSpace(typed))
+ if err == nil {
+ return value, true
+ }
+ }
}
- maxInt := int64(^uint(0) >> 1)
- minInt := -maxInt - 1
- if value < minInt || value > maxInt {
- return 0, false
+ return 0, false
+}
+
+func FitsInInt(value int64) bool {
+ if strconv.IntSize == 32 {
+ return value >= -1<<31 && value <= 1<<31-1
}
- return int(value), true
+ return true
}
func Int64Attr(attrs map[string]interface{}, keys ...string) (int64, bool) {
@@ -937,7 +996,7 @@ func FirstMessageText(genai *GenAIInfo) string {
func firstTextFromAny(value interface{}) string {
switch typed := value.(type) {
case string:
- return typed
+ return meaningfulText(typed)
case []interface{}:
for _, item := range typed {
if text := firstTextFromAny(item); text != "" {
@@ -946,7 +1005,9 @@ func firstTextFromAny(value interface{}) string {
}
case map[string]interface{}:
if content, ok := typed["content"]; ok {
- return strings.TrimSpace(fmt.Sprint(content))
+ if text := firstTextFromAny(content); text != "" {
+ return text
+ }
}
if parts, ok := typed["parts"]; ok {
return firstTextFromAny(parts)
@@ -958,6 +1019,16 @@ func firstTextFromAny(value interface{}) string {
return ""
}
+func meaningfulText(value string) string {
+ trimmed := strings.TrimSpace(value)
+ switch strings.ToLower(trimmed) {
+ case "", "<nil>", "{}", "[]", "null":
+ return ""
+ default:
+ return trimmed
+ }
+}
+
func IsZeroJSON(value interface{}) bool {
data, err := json.Marshal(value)
if err != nil {
@@ -1060,10 +1131,12 @@ func InferAction(attrs map[string]interface{}, fallback string) string {
return "mcp.tool_invoked"
case mcpMethod == "resources/read" && IsFileURI(FirstString(attrs, "mcp.resource.uri")):
return "file.read"
- case operation == "execute_tool" || FirstString(attrs, "gen_ai.tool.call.id", "gen_ai.tool.call.arguments") != "":
+ case operation == "execute_tool":
return "tool.invoked"
case (operation == "chat" || operation == "generate_content" || operation == "text_completion") && HasPromptLikeContent(attrs):
return "prompt.submitted"
+ case HasToolCall(attrs):
+ return "tool.invoked"
case strings.Contains(text, "gemini_cli.user_prompt"):
return "prompt.submitted"
case strings.Contains(text, "gemini_cli.tool_call"):
@@ -1087,13 +1160,56 @@ func InferAction(attrs map[string]interface{}, fallback string) string {
}
}
+func HasToolCall(attrs map[string]interface{}) bool {
+ if IsMeaningfulValue(attrs["gen_ai.tool.call.id"]) {
+ return true
+ }
+ return IsMeaningfulValue(attrs["gen_ai.tool.call.arguments"])
+}
+
+func IsMeaningfulValue(value interface{}) bool {
+ switch typed := value.(type) {
+ case nil:
+ return false
+ case string:
+ trimmed := strings.TrimSpace(typed)
+ return trimmed != "" && trimmed != "<nil>" && trimmed != "{}" && trimmed != "[]" && trimmed != "null"
+ case map[string]interface{}:
+ for _, item := range typed {
+ if IsMeaningfulValue(item) {
+ return true
+ }
+ }
+ return false
+ case []interface{}:
+ for _, item := range typed {
+ if IsMeaningfulValue(item) {
+ return true
+ }
+ }
+ return false
+ default:
+ return true
+ }
+}
+
func IsFileURI(value string) bool {
+ return FilePathFromURI(value) != ""
+}
+
+func FilePathFromURI(value string) string {
parsed, err := url.Parse(value)
- return err == nil && parsed.Scheme == "file"
+ if err != nil || parsed.Scheme != "file" {
+ return ""
+ }
+ if parsed.Host != "" && parsed.Host != "localhost" {
+ return "//" + parsed.Host + parsed.Path
+ }
+ return parsed.Path
}
func HasPromptLikeContent(attrs map[string]interface{}) bool {
- if FirstString(attrs, "gen_ai.prompt", "prompt", "user_prompt", "input.prompt", "copilot_chat.user_request") != "" {
+ if FirstTextAttr(attrs, "gen_ai.prompt", "prompt", "user_prompt", "input.prompt", "copilot_chat.user_request") != "" {
return true
}
if v, ok := AnyAttr(attrs, "gen_ai.input.messages"); ok && firstTextFromAny(v) != "" {