Skip to content

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

8.9 KB
Raw Blame
package cmd

import (
	"reflect"
	"testing"

	"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)

// piFamilyRuntimes is every runtime the shared Pi-family mapper serves. Tests that assert a
// property of the shape rather than of one product walk this, so a runtime added later inherits
// the same guarantees instead of quietly opting out of them.
var piFamilyRuntimes = []piFamily{piRuntime, ompRuntime}

func piFamilyToolCallID(t *testing.T, event normalizedEvent) string {
	t.Helper()
	genAI, ok := event.fields["gen_ai"].(map[string]interface{})
	if !ok {
		return ""
	}
	tool, ok := genAI["tool"].(map[string]interface{})
	if !ok {
		return ""
	}
	call, ok := tool["call"].(map[string]interface{})
	if !ok {
		return ""
	}
	id, _ := call["id"].(string)
	return id
}

// The join key. Both halves of a tool call carry the runtime's own `toolCallId`, and it is the only
// field that links the tool.invoked to whatever the completion turned into -- a file.modified, a
// command.executed, or, on Oh My Pi, the approval decision for the same execution. Before this was
// promoted, the two halves of one tool call sat in the log as unrelated rows that merely shared a
// session id and a nearby timestamp.
func TestPiFamilyPromotesTheToolCallIDOnBothHalves(t *testing.T) {
	for _, runtime := range piFamilyRuntimes {
		t.Run(runtime.platform, func(t *testing.T) {
			for _, payload := range []map[string]interface{}{
				{"type": "tool_call", "toolName": "bash", "toolCallId": "call-77",
					"input": map[string]interface{}{"command": "ls"}},
				{"type": "tool_result", "toolName": "bash", "toolCallId": "call-77",
					"input": map[string]interface{}{"command": "ls"}},
			} {
				events := runtime.endpointEvents(payload, "sess-1")
				if len(events) != 1 {
					t.Fatalf("%s produced %d events, want 1", payload["type"], len(events))
				}
				if got := piFamilyToolCallID(t, events[0]); got != "call-77" {
					t.Fatalf("%s gen_ai.tool.call.id = %q, want call-77 -- without it the two "+
						"halves of one tool call cannot be joined", payload["type"], got)
				}
			}
		})
	}
}

func TestOmpContextEventMapsSkillListingAsSystemContext(t *testing.T) {
	listing := "- oracle-index: Lists conventions. Canary BCN-2AFE23-C01I.\n- oracle-probe: Runs the probe."
	events := ompRuntime.endpointEvents(map[string]interface{}{
		"type":         "context",
		"skillListing": listing,
	}, "sess-1")
	if len(events) != 1 || events[0].action != "session.context" || events[0].category != "session" {
		t.Fatalf("events = %+v, want one session.context", events)
	}
	want := asymptoteobserve.ParseSkillListing(asymptoteobserve.SystemContextSourceSystemPrompt, listing, nil).Fields(asymptoteobserve.DefaultStringLimit)
	genAI, _ := events[0].fields["gen_ai"].(map[string]interface{})
	if !reflect.DeepEqual(genAI, want["gen_ai"]) {
		t.Fatalf("gen_ai = %#v, want only the listing's system_instructions, one part per skill", genAI)
	}
	if !reflect.DeepEqual(events[0].fields["system_context"], want["system_context"]) || !reflect.DeepEqual(events[0].fields["content"], want["content"]) {
		t.Fatalf("system_context = %#v content = %#v, want the shared builder's", events[0].fields["system_context"], events[0].fields["content"])
	}
	if _, ok := events[0].fields["prompt"]; ok {
		t.Fatalf("session context was mislabeled as operator prompt: %#v", events[0].fields["prompt"])
	}
	raw, _ := events[0].fields["raw"].(map[string]interface{})
	if !reflect.DeepEqual(raw, map[string]interface{}{"omp": map[string]interface{}{"type": "context"}}) {
		t.Fatalf("raw = %#v, want event-type metadata without a copy of the listing", raw)
	}
}

// One context payload can carry both listings. The skill index keeps its own event, and each route
// is built from the envelope rather than from the skill event's fields.
func TestOmpContextEventMapsSkillListingAndMCPRoutesApart(t *testing.T) {
	events := ompRuntime.endpointEvents(map[string]interface{}{
		"type":         "context",
		"skillListing": "- deploy: Deploy applications.",
		"mcpToolRoutes": []interface{}{
			map[string]interface{}{"name": "mcp__notes_save_note", "description": "Saves a note."},
		},
	}, "sess-1")
	if len(events) != 2 || events[0].action != "session.context" || events[1].action != "mcp.tool_listed" {
		t.Fatalf("events = %+v, want session.context then mcp.tool_listed", events)
	}
	if _, ok := events[1].fields["system_context"]; ok {
		t.Fatalf("route listing inherited the skill listing's system_context: %#v", events[1].fields)
	}
	genAI, _ := events[1].fields["gen_ai"].(map[string]interface{})
	if _, ok := genAI["system_instructions"]; ok {
		t.Fatalf("route listing inherited the skill listing's system_instructions: %#v", genAI)
	}
}

// The call ID must survive onto whatever action the completion resolved to, not just onto the
// generic tool.completed. A file.modified with no join key cannot be tied back to the tool.invoked
// that proposed the edit, which is exactly the link an investigation follows. A failure is the
// case an investigation is most likely to be reading, so it gets the join key too.
func TestPiFamilyToolCallIDSurvivesOntoFileAndCommandActions(t *testing.T) {
	for _, runtime := range piFamilyRuntimes {
		for _, tc := range []struct {
			tool    string
			args    map[string]interface{}
			isError bool
			action  string
		}{
			{"edit", map[string]interface{}{"path": "/repo/main.go"}, false, "file.modified"},
			{"write", map[string]interface{}{"path": "/repo/new.go"}, false, "file.created"},
			{"read", map[string]interface{}{"path": "/repo/main.go"}, false, "file.read"},
			{"bash", map[string]interface{}{"command": "ls"}, false, "command.executed"},
			{"grep", map[string]interface{}{"pattern": "TODO"}, false, "tool.completed"},
			{"bash", map[string]interface{}{"command": "false"}, true, "tool.failed"},
		} {
			t.Run(runtime.platform+"/"+tc.action, func(t *testing.T) {
				events := runtime.endpointEvents(map[string]interface{}{
					"type": "tool_result", "toolName": tc.tool, "toolCallId": "call-9", "input": tc.args,
					"isError": tc.isError,
				}, "sess-1")
				if len(events) != 1 || events[0].action != tc.action {
					t.Fatalf("events = %v, want a single %s", events, tc.action)
				}
				if got := piFamilyToolCallID(t, events[0]); got != "call-9" {
					t.Fatalf("%s gen_ai.tool.call.id = %q, want call-9", tc.action, got)
				}
			})
		}
	}
}

// Only an event that describes a tool action takes a join key. A session lifecycle payload or a
// prompt that happened to echo an ID is not a tool call, and giving it the join key of one would
// join it to work it did not do -- the invariant applyToolCallID exists to keep.
func TestPiFamilyNonToolEventsTakeNoToolCallID(t *testing.T) {
	for _, runtime := range piFamilyRuntimes {
		for _, payload := range []map[string]interface{}{
			{"type": "session_start", "reason": "startup", "toolCallId": "call-1"},
			{"type": "session_shutdown", "reason": "quit", "toolCallId": "call-1"},
			{"type": "input", "text": "hello", "toolCallId": "call-1"},
			{"type": "message_end", "toolCallId": "call-1", "message": map[string]interface{}{
				"role":  "assistant",
				"usage": map[string]interface{}{"input": float64(1), "output": float64(1)},
			}},
		} {
			t.Run(runtime.platform+"/"+payload["type"].(string), func(t *testing.T) {
				for _, event := range runtime.endpointEvents(payload, "sess-1") {
					if got := piFamilyToolCallID(t, event); got != "" {
						t.Fatalf("%s took a tool call id (%q); it describes no tool action",
							event.action, got)
					}
				}
			})
		}
	}
}

// A payload with no call ID must still produce its event. Losing the join key is bad; losing the
// record of the tool call is worse.
func TestPiFamilyToolEventsSurviveAMissingToolCallID(t *testing.T) {
	for _, runtime := range piFamilyRuntimes {
		t.Run(runtime.platform, func(t *testing.T) {
			events := runtime.endpointEvents(map[string]interface{}{
				"type": "tool_result", "toolName": "bash",
				"input": map[string]interface{}{"command": "ls"},
			}, "sess-1")
			if len(events) != 1 || events[0].action != "command.executed" {
				t.Fatalf("events = %v, want a single command.executed", events)
			}
			if got := piFamilyToolCallID(t, events[0]); got != "" {
				t.Fatalf("gen_ai.tool.call.id = %q, want it absent when the runtime sent none", got)
			}
		})
	}
}

// The user's own `!` command is not a tool call the model made, so it carries no runtime tool call
// id. It still records the command, which is the fact that matters about it.
func TestPiFamilyUserBashHasNoToolCallID(t *testing.T) {
	for _, runtime := range piFamilyRuntimes {
		t.Run(runtime.platform, func(t *testing.T) {
			events := runtime.endpointEvents(map[string]interface{}{
				"type": "user_bash", "command": "git status", "cwd": "/repo",
			}, "sess-1")
			if len(events) != 1 || events[0].action != "command.executed" {
				t.Fatalf("events = %v, want a single command.executed", events)
			}
			if got := piFamilyToolCallID(t, events[0]); got != "" {
				t.Fatalf("gen_ai.tool.call.id = %q, want none for an operator-run command", got)
			}
		})
	}
}