Skip to content

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

8.7 KB
Raw Blame
package asymptoteobserve

import (
	"encoding/json"
	"strings"
	"testing"
)

func TestValidateFidelityValues(t *testing.T) {
	for _, fidelity := range []string{"", FidelityObserved, FidelityInferred} {
		t.Run("accepts_"+fidelity, func(t *testing.T) {
			event := NewEvent(NewEventOptions{
				Action:   "tool.invoked",
				Harness:  HarnessInfo{Name: "cursor"},
				Fidelity: fidelity,
			})
			if err := event.Validate(); err != nil {
				t.Fatalf("Validate rejected fidelity %q: %v", fidelity, err)
			}
		})
	}

	// "probably" is the shape of the mistake worth guarding: a third value invented at a call
	// site, which would leave consumers filtering on a vocabulary that no longer covers the log.
	event := NewEvent(NewEventOptions{Action: "tool.invoked", Harness: HarnessInfo{Name: "cursor"}, Fidelity: "probably"})
	if err := event.Validate(); err == nil || !strings.Contains(err.Error(), "event.fidelity must be observed or inferred") {
		t.Fatalf("Validate error = %v, want fidelity error", err)
	}
}

func TestValidateCollectionMethodValues(t *testing.T) {
	for _, method := range []string{"", CollectionMethodHook, CollectionMethodOTLP, CollectionMethodPlugin, CollectionMethodPoll, CollectionMethodGateway} {
		t.Run("accepts_"+method, func(t *testing.T) {
			event := NewEvent(NewEventOptions{
				Action:  "tool.invoked",
				Harness: HarnessInfo{Name: "cursor", CollectionMethod: method},
			})
			if err := event.Validate(); err != nil {
				t.Fatalf("Validate rejected collection_method %q: %v", method, err)
			}
		})
	}

	event := NewEvent(NewEventOptions{Action: "tool.invoked", Harness: HarnessInfo{Name: "cursor", CollectionMethod: "webhook"}})
	if err := event.Validate(); err == nil || !strings.Contains(err.Error(), "harness.collection_method must be hook, otlp, plugin, poll, or gateway") {
		t.Fatalf("Validate error = %v, want collection_method error", err)
	}
}

// Both fields are additive and optional, so an event from a producer that has never heard of them
// must serialize exactly as it did before. Asserting on absence rather than on a default value is
// the point: an empty string written into either field would be a value consumers have to handle.
func TestProvenanceFieldsOmittedWhenUnset(t *testing.T) {
	event := NewEvent(NewEventOptions{Action: "tool.invoked", Harness: HarnessInfo{Name: "cursor"}})
	data, err := json.Marshal(event)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	for _, key := range []string{"fidelity", "collection_method"} {
		if strings.Contains(string(data), key) {
			t.Fatalf("unset provenance field %q serialized: %s", key, data)
		}
	}
}

func TestProvenanceFieldsSerializeUnderSchemaNames(t *testing.T) {
	event := NewEvent(NewEventOptions{
		Action:   "command.executed",
		Harness:  HarnessInfo{Name: "codex_cli", CollectionMethod: CollectionMethodOTLP},
		Fidelity: FidelityInferred,
	})
	data, err := json.Marshal(event)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var decoded struct {
		Event struct {
			Fidelity string `json:"fidelity"`
		} `json:"event"`
		Harness struct {
			CollectionMethod string `json:"collection_method"`
		} `json:"harness"`
	}
	if err := json.Unmarshal(data, &decoded); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if decoded.Event.Fidelity != FidelityInferred {
		t.Fatalf("event.fidelity = %q, want %q", decoded.Event.Fidelity, FidelityInferred)
	}
	if decoded.Harness.CollectionMethod != CollectionMethodOTLP {
		t.Fatalf("harness.collection_method = %q, want %q", decoded.Harness.CollectionMethod, CollectionMethodOTLP)
	}
}

func TestCollectionMethodForPlatform(t *testing.T) {
	for platform, want := range map[string]string{
		// Hook-shaped: the vendor exposes a hook configuration and Beacon registers in it.
		"cursor":        CollectionMethodHook,
		"claude":        CollectionMethodHook,
		"devin":         CollectionMethodHook,
		"devin-cli":     CollectionMethodHook,
		"devin-desktop": CollectionMethodHook,
		"grok":          CollectionMethodHook,
		"antigravity":   CollectionMethodHook,
		"hermes":        CollectionMethodHook,
		"factory":       CollectionMethodHook,
		"copilot":       CollectionMethodHook,
		// Muse Code is hook-shaped even though Beacon writes the whole hooks file rather than
		// merging into one the vendor already has. The distinction this field draws is about what
		// Beacon ships, not who owns the file: the events are Meta's own lifecycle events and the
		// payloads are Meta's to change, so a coverage gap is the vendor's to expose. Grok is the
		// same shape for the same reason. A `plugin` here would claim Beacon ships and versions
		// source the runtime loads, which for Muse Code it does not.
		"muse": CollectionMethodHook,
		// OpenHands is hook-shaped: the events, the payload shapes and the .openhands/hooks.json
		// contract are all the vendor's, and Beacon only registers commands in a file the runtime
		// already reads. A `plugin` here would claim Beacon ships and versions source OpenHands
		// loads, which it does not -- and would send someone to fix Beacon for a field OpenHands
		// never sent.
		"openhands": CollectionMethodHook,
		// Kiro is hook-shaped. The events, the payload shapes and the `.kiro/hooks/*.json` v1
		// schema are all AWS's, and Beacon only adds a hook file of its own to a directory the
		// runtime already scans -- owning that one file is a question of who wrote it, not of who
		// defines the contract. A `plugin` here would claim Beacon ships and versions source Kiro
		// loads and executes, which it does not: Kiro runs a command, and the command is the same
		// beacon-hooks binary every other hook runtime runs.
		"kiro": CollectionMethodHook,
		// DeepSeek Harness is hook-shaped, and it is the case most likely to be argued the other
		// way. The runtime reaches Beacon through `@deepseek-ai/dsh-hooks-claude-code`, a plugin --
		// but that plugin is DeepSeek's, shipped in the `dsh` CLI and versioned with it. What Beacon
		// contributes is a hooks file of command entries and one row mounting the vendor's bridge, so
		// the thing that runs on each event is the same beacon-hooks binary every other hook runtime
		// runs. `plugin` is reserved for the runtimes where Beacon ships and versions the source the
		// runtime loads (opencode, Cline, Pi, Oh My Pi), and claiming it here would send someone to
		// fix Beacon for a field the bridge never sent.
		"dsh": CollectionMethodHook,
		// Kimi Code is hook-shaped. The `[[hooks]]` array, the event names and the payload shapes
		// are all Moonshot's, and Beacon only appends command entries to a config file the runtime
		// already reads -- it ships no source the runtime loads. Kimi Code also has a plugin system
		// that can declare hooks, which is the thing that makes this worth stating: Beacon
		// deliberately does not use it, because a plugin install needs an interactive step and a
		// `/reload`, and choosing it would have changed this field as a side effect of an install
		// mechanic rather than of what runs on each event.
		"kimi": CollectionMethodHook,
		// vscode is the case that justifies keying on --platform rather than on the normalized
		// harness name: its hook and OTLP telemetry both normalize to vscode_copilot, so the
		// harness name cannot distinguish them and only the flag can.
		"vscode": CollectionMethodHook,
		// Plugin-shaped: Beacon ships and versions the file the runtime loads.
		"opencode": CollectionMethodPlugin,
		"cline":    CollectionMethodPlugin,
		"pi":       CollectionMethodPlugin,
		"omp":      CollectionMethodPlugin,
		"prime":    CollectionMethodPlugin,
		"omo":      CollectionMethodPlugin,
		// Unset stays unset rather than defaulting to a method, so an event with no platform does
		// not claim one.
		"": "",
		// Case and surrounding whitespace come from a hook command line written into a settings
		// file, so they are worth normalizing rather than trusting.
		"  CLINE  ": CollectionMethodPlugin,
		// An unrecognized platform resolves to hook rather than to empty, mirroring how
		// NormalizeHarnessName keeps an unknown runtime's own name. A new hook-shaped integration
		// then needs no change here; a new plugin-shaped one does.
		"some-future-runtime": CollectionMethodHook,
	} {
		if got := CollectionMethodForPlatform(platform); got != want {
			t.Fatalf("CollectionMethodForPlatform(%q) = %q, want %q", platform, got, want)
		}
	}
}

func TestValidCollectionMethodAndFidelityRejectEmpty(t *testing.T) {
	// Validate() checks for empty separately before calling these, so the predicates themselves
	// treat "" as not-a-value. Pinning that keeps a future caller from using them as an
	// "is this allowed" check and accidentally permitting the empty string.
	if ValidCollectionMethod("") {
		t.Fatal("ValidCollectionMethod(\"\") = true, want false")
	}
	if ValidFidelity("") {
		t.Fatal("ValidFidelity(\"\") = true, want false")
	}
}