The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package cmd
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
// ompTestLog puts an Oh My Pi extension run in a temp endpoint log and returns its path.
func ompTestLog(t *testing.T) string {
t.Helper()
setupHookConfigDirs(t)
platformFlag = "omp"
logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
t.Setenv("BEACON_ENDPOINT_MODE", "1")
t.Setenv("BEACON_ENDPOINT_LOG", logPath)
t.Setenv("BEACON_CONTENT_RETENTION", "full")
return logPath
}
func ompEventActions(t *testing.T, logPath string) []string {
t.Helper()
var actions []string
for _, event := range endpointEvents(t, logPath) {
meta, _ := event["event"].(map[string]interface{})
actions = append(actions, meta["action"].(string))
}
return actions
}
func ompEventWithAction(t *testing.T, logPath, action string) map[string]interface{} {
t.Helper()
for _, event := range endpointEvents(t, logPath) {
meta, _ := event["event"].(map[string]interface{})
if meta["action"] == action {
return event
}
}
t.Fatalf("no %s event in log; got %v", action, ompEventActions(t, logPath))
return nil
}
// ompPayloads returns one payload per supported event type, each carrying the minimum its branch
// needs to emit. Shared by the contract test and the Pi/Oh My Pi separation test.
func ompPayloads() map[string]map[string]interface{} {
return map[string]map[string]interface{}{
"session_start": {"type": "session_start", "reason": "startup"},
"session_shutdown": {"type": "session_shutdown", "reason": "quit"},
"input": {"type": "input", "text": "do the thing", "source": "interactive"},
"context": {"type": "context", "skillListing": "- deploy: Deploy applications."},
"tool_call": {"type": "tool_call", "toolName": "bash", "toolCallId": "c1", "input": map[string]interface{}{"command": "ls"}},
"tool_result": {"type": "tool_result", "toolName": "bash", "toolCallId": "c1", "input": map[string]interface{}{"command": "ls"}},
"user_bash": {"type": "user_bash", "command": "git status", "cwd": "/repo"},
"user_python": {"type": "user_python", "code": "print(1)", "cwd": "/repo"},
"tool_approval_requested": {"type": "tool_approval_requested", "toolName": "bash", "toolCallId": "c1",
"approvalMode": "always-ask", "reason": "writes outside the workspace"},
"tool_approval_resolved": {"type": "tool_approval_resolved", "toolName": "bash", "toolCallId": "c1",
"approved": true},
"message_end": {"type": "message_end", "message": map[string]interface{}{
"role": "assistant",
"usage": map[string]interface{}{"input": float64(10), "output": float64(5)},
}},
}
}
// An unrecognized type is silent rather than generic. Oh My Pi publishes far more than the
// extension subscribes to -- provider request/response internals, streaming message updates,
// compaction and retry signals -- and a future one becoming an undifferentiated row would fill the
// log with records no investigation asks for.
func TestOmpEventUnknownTypeProducesNothing(t *testing.T) {
for _, name := range []string{
"message_update", "before_provider_request", "after_provider_response", "turn_start",
"auto_compaction_start", "ttsr_triggered", "", "totally_new",
} {
events := ompRuntime.endpointEvents(map[string]interface{}{"type": name}, "sess-1")
if len(events) != 0 {
t.Fatalf("Oh My Pi event type %q produced %d events, want none", name, len(events))
}
}
}
// Oh My Pi and Pi are separately installed products that a fleet can run side by side. Sharing the
// mapper must not mean sharing identity: every event has to name the runtime that produced it, in
// the harness name, in the message, and in the `raw` block an operator reads to see the payload.
//
// This is the test that fails if the shared core in pi_family.go ever starts hardcoding one
// runtime's name -- which is the way a refactor like that goes wrong silently.
func TestOmpEventIsAttributedToOhMyPiNotPi(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "session_start", "reason": "startup", "sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "session.started")
// The harness name is normalized at write time, so an Oh My Pi session is not split across two
// spellings in any query that groups by harness.name -- and is never merged into Pi's.
harness := nested(t, event, "harness")
if harness["name"] != "omp" {
t.Fatalf("harness.name = %v, want omp", harness["name"])
}
if event["message"] != "Oh My Pi session started" {
t.Fatalf("message = %v, want it to name Oh My Pi", event["message"])
}
raw := nested(t, event, "raw")
if _, ok := raw["omp"]; !ok {
t.Fatalf("raw = %v, want the verbatim payload under an omp key", raw)
}
if _, ok := raw["pi"]; ok {
t.Fatalf("raw = %v, want no pi key on an Oh My Pi event", raw)
}
if raw["omp_session_reason"] != "startup" {
t.Fatalf("raw.omp_session_reason = %v, want startup", raw["omp_session_reason"])
}
}
// The converse, on the same shared mapper: a Pi payload must keep naming Pi. Together with the test
// above, this pins the property that matters -- one shared shape, two identities -- rather than
// just asserting each side in isolation.
func TestPiAndOmpProduceTheSameShapeUnderDifferentIdentities(t *testing.T) {
shared := map[string]bool{}
for _, name := range supportedPiEventTypes() {
shared[name] = true
}
for name, payload := range ompPayloads() {
// The Oh My Pi-only types are excluded rather than expected to match: Pi's extension never
// sends them, so there is no Pi behavior to compare against. TestOmpOnlyEventTypesAreNotInPis
// pins that asymmetry from the other side.
if !shared[name] {
continue
}
t.Run(name, func(t *testing.T) {
piEvents := piRuntime.endpointEvents(cloneFields(payload), "sess-1")
ompEvents := ompRuntime.endpointEvents(cloneFields(payload), "sess-1")
if len(piEvents) != len(ompEvents) {
t.Fatalf("pi produced %d events and omp %d for %q; the shared mapper should emit "+
"the same events for the same payload", len(piEvents), len(ompEvents), name)
}
for i := range piEvents {
if piEvents[i].action != ompEvents[i].action {
t.Fatalf("action[%d] = %q (pi) vs %q (omp)", i, piEvents[i].action, ompEvents[i].action)
}
if piEvents[i].category != ompEvents[i].category || piEvents[i].severity != ompEvents[i].severity {
t.Fatalf("event[%d] category/severity differ between runtimes: %+v vs %+v",
i, piEvents[i], ompEvents[i])
}
if piEvents[i].message == ompEvents[i].message {
t.Fatalf("event[%d] message %q is identical for both runtimes; a reader could "+
"not tell which one produced it", i, piEvents[i].message)
}
if _, ok := piEvents[i].fields["raw"].(map[string]interface{})["pi"]; !ok {
t.Fatalf("pi event[%d] raw block is not keyed by pi: %v", i, piEvents[i].fields["raw"])
}
if _, ok := ompEvents[i].fields["raw"].(map[string]interface{})["omp"]; !ok {
t.Fatalf("omp event[%d] raw block is not keyed by omp: %v", i, ompEvents[i].fields["raw"])
}
}
})
}
}
// Beacon ships and versions the extension file Oh My Pi loads, so its events are plugin-collected,
// not hook-collected. `event.fidelity` stays observed because every action here was named by the
// runtime rather than derived by Beacon.
func TestOmpEventCarriesPluginProvenance(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "input", "text": "hello", "sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "prompt.submitted")
if method := nested(t, event, "harness")["collection_method"]; method != "plugin" {
t.Fatalf("harness.collection_method = %v, want plugin", method)
}
if fidelity := nested(t, event, "event")["fidelity"]; fidelity != "observed" {
t.Fatalf("event.fidelity = %v, want observed", fidelity)
}
}
// The skill index the extension reads from the system prompt reaches the log as the shared builder
// shapes it: system_context names each skill and gen_ai.system_instructions keeps one part per
// skill, and the hook logger's per-string limit applies to each part rather than to the index.
func TestOmpEventSkillListingIsWrittenAsSystemContext(t *testing.T) {
logPath := ompTestLog(t)
long := strings.Repeat("Describes the release procedure. ", 150)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "context", "sessionId": "sess-1",
"skillListing": "- deploy: " + long + "\n- review: Reviews a diff. BCN-2AFE23-C01I",
})
event := ompEventWithAction(t, logPath, "session.context")
context := nested(t, event, "system_context")
skills, _ := context["skills"].([]interface{})
if context["kind"] != "skill_listing" || context["source"] != "system_prompt" || len(skills) != 2 {
t.Fatalf("system_context = %v, want both skills of a system-prompt skill_listing", context)
}
parts := asymptoteobserve.GenAIText(nested(t, event, "gen_ai")["system_instructions"], "", asymptoteobserve.GenAIPartTypeText)
if len(parts) != 2 || !strings.HasSuffix(parts[1], "BCN-2AFE23-C01I") {
t.Fatalf("system_instructions parts = %d, want the second skill kept whole after a long first one", len(parts))
}
if _, ok := event["prompt"]; ok {
t.Fatalf("skill listing recorded as a prompt: %v", event["prompt"])
}
}
// Each MCP route the extension read from the system prompt is one mcp.tool_listed whose description
// the poisoning rule reads. The device name does not name a server, so there is no mcp block, and
// raw holds no copy of the route list.
func TestOmpEventMCPToolRoutesAreWrittenAsListings(t *testing.T) {
logPath := ompTestLog(t)
out := runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "context", "sessionId": "sess-1",
"mcpToolRoutes": []interface{}{
map[string]interface{}{"name": "mcp__notes_save_note", "description": "Saves a note. BCN-7F3A21-C03"},
map[string]interface{}{"name": "mcp__docs_search", "description": "Searches the docs."},
map[string]interface{}{"name": "read", "description": "Not an MCP route."},
map[string]interface{}{"name": "mcp__notes_empty", "description": ""},
},
})
if out["recorded"] != float64(2) {
t.Fatalf("reply = %v, want both MCP routes recorded", out)
}
events := endpointEvents(t, logPath)
if len(events) != 2 {
t.Fatalf("events = %v, want one mcp.tool_listed per described MCP route", ompEventActions(t, logPath))
}
event := events[0]
if meta := nested(t, event, "event"); meta["action"] != "mcp.tool_listed" || meta["category"] != "mcp" {
t.Fatalf("event = %v, want an MCP tool listing", meta)
}
tool := nested(t, nested(t, event, "gen_ai"), "tool")
if tool["name"] != "mcp__notes_save_note" || tool["description"] != "Saves a note. BCN-7F3A21-C03" {
t.Fatalf("gen_ai.tool = %v, want the route's device name and description", tool)
}
for _, key := range []string{"mcp", "prompt", "system_context"} {
if _, ok := event[key]; ok {
t.Fatalf("listing carries %s: %v", key, event[key])
}
}
if raw, _ := json.Marshal(event["raw"]); strings.Contains(string(raw), "BCN-7F3A21-C03") {
t.Fatalf("raw = %s, want no second copy of the description", raw)
}
}
// The extension decides whether to resend a skill listing from this reply, because the hook exits 0
// whether or not it wrote anything. Every way an event is dropped must report nothing recorded; an
// event staged in the workspace spool is recorded, since a drain delivers it later.
func TestOmpEventReplyReportsWhatWasRecorded(t *testing.T) {
listing := map[string]interface{}{"type": "context", "sessionId": "sess-1", "skillListing": "- deploy: Deploy applications."}
noLog := func(t *testing.T) {
for _, key := range []string{"BEACON_ENDPOINT_LOG", "BEACON_CLOUD_LOG_PATH", "BEACON_LOG_PATH", "BEACON_RUNTIME_LOG", "BEACON_ENDPOINT_MODE"} {
t.Setenv(key, "")
}
}
unwritable := func(t *testing.T) {
dir := filepath.Join(t.TempDir(), "runtime.jsonl")
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("BEACON_ENDPOINT_LOG", dir)
}
// A workspace under a regular file cannot be created on any platform, so nothing stages.
unwritableWorkspace := func(t *testing.T) string {
blocker := filepath.Join(t.TempDir(), "not-a-directory")
if err := os.WriteFile(blocker, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
return filepath.Join(blocker, "workspace")
}
writableWorkspace := func(t *testing.T) string { return t.TempDir() }
for _, tc := range []struct {
name string
input map[string]interface{}
setup func(*testing.T)
cwd func(*testing.T) string
written float64
}{
{"written", listing, nil, nil, 1},
{"log not writable", listing, unwritable, unwritableWorkspace, 0},
{"log not writable, staged in the workspace", listing, unwritable, writableWorkspace, 1},
{"no endpoint log configured", listing, noLog, nil, 0},
{"type this build does not map", map[string]interface{}{"type": "message_update", "sessionId": "sess-1"}, nil, nil, 0},
} {
t.Run(tc.name, func(t *testing.T) {
ompTestLog(t)
if tc.setup != nil {
tc.setup(t)
}
input := make(map[string]interface{}, len(tc.input)+1)
for key, value := range tc.input {
input[key] = value
}
if tc.cwd != nil {
input["cwd"] = tc.cwd(t)
}
if out := runHookWithInput(t, runOmpEvent, input); out["recorded"] != tc.written {
t.Fatalf("reply = %v, want recorded = %v", out, tc.written)
}
})
}
}
// Print and ACP never emit `input`; the extension forwards the delivered user message instead,
// with its text under `prompt`. The row names that origin, because the text is what Oh My Pi sent
// the model after expanding templates and commands, not what an `input` event would carry.
func TestOmpEventUserMessagePromptRecordsDeliveredText(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "message_end", "message": map[string]interface{}{"role": "user"},
"prompt": "summarize the changes", "sessionId": "sess-print", "ompMode": "print",
})
event := ompEventWithAction(t, logPath, "prompt.submitted")
if prompt := nested(t, event, "prompt"); prompt["text"] != "summarize the changes" {
t.Fatalf("prompt.text = %v, want the delivered text", prompt["text"])
}
if content := nested(t, event, "content"); content["included"] != true || content["hash"] == "" {
t.Fatalf("content = %v, want retained content with a hash", content)
}
raw := nested(t, event, "raw")
if raw["omp_prompt_origin"] != "message_end" {
t.Fatalf("raw.omp_prompt_origin = %v, want message_end", raw["omp_prompt_origin"])
}
if _, ok := raw["omp_input_source"]; ok {
t.Fatalf("raw.omp_input_source = %v, want none: no input event reported a source", raw["omp_input_source"])
}
}
// A user message the extension did not mark as a prompt -- every one an older extension forwarded
// -- records nothing, so an interactive or RPC prompt already recorded from `input` is never
// counted twice.
func TestOmpEventUnmarkedUserMessageProducesNothing(t *testing.T) {
events := ompRuntime.endpointEvents(map[string]interface{}{
"type": "message_end",
"message": map[string]interface{}{
"role": "user",
"content": []interface{}{map[string]interface{}{"type": "text", "text": "typed"}},
},
}, "sess-1")
if len(events) != 0 {
t.Fatalf("unmarked user message produced %v, want nothing", events)
}
}
// A tool_call is not an approval. Oh My Pi's tool_call handler can block a call, but that is an
// extension deciding rather than an operator being asked; the runtime's real operator decisions
// arrive as their own approval events. Recording a block as an approval would be indistinguishable
// from a decision a human actually made.
func TestOmpEventToolCallIsNotAnApproval(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "tool_call", "toolName": "bash", "toolCallId": "call-1",
"input": map[string]interface{}{"command": "rm -rf /tmp/x"}, "sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "tool.invoked")
if tool := nested(t, event, "tool"); tool["name"] != "bash" || tool["command"] != "rm -rf /tmp/x" {
t.Fatalf("tool = %v, want the bash tool and its command", tool)
}
if _, ok := event["approval"]; ok {
t.Fatalf("tool.invoked carries an approval block: %v", event)
}
}
func TestOmpEventFailedToolIsRecordedAsAFailure(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "tool_result", "toolName": "bash", "toolCallId": "call-1",
"input": map[string]interface{}{"command": "false"}, "isError": true, "sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "tool.failed")
if event["severity"] != "high" {
t.Fatalf("severity = %v, want high for a failed tool", event["severity"])
}
if meta := nested(t, event, "event"); meta["category"] != "tool" {
t.Fatalf("category = %v, want tool", meta["category"])
}
if errBlock := nested(t, event, "error"); errBlock["type"] != "tool_error" {
t.Fatalf("error = %v, want a tool_error", errBlock)
}
}
// ompTextBlocks is a tool result's content as Oh My Pi delivers it: a list of text blocks.
func ompTextBlocks(texts ...string) []interface{} {
blocks := make([]interface{}, len(texts))
for i, text := range texts {
blocks[i] = map[string]interface{}{"type": "text", "text": text}
}
return blocks
}
// fieldsToolCallResult reads gen_ai.tool.call.result, the value e.gen_ai.tool.call.result_text is
// derived from, or nil when the event carries none.
func fieldsToolCallResult(event map[string]interface{}) interface{} {
genAI, _ := event["gen_ai"].(map[string]interface{})
tool, _ := genAI["tool"].(map[string]interface{})
call, _ := tool["call"].(map[string]interface{})
return call["result"]
}
// The payloads are Oh My Pi 18.6.3's, as a beacon-oracle run recorded them. Before this, every one
// of these results was kept only under raw.omp, which no rule can match: the model read the file,
// the page, the skill and the MCP reply, and nothing a detection reads held any of them.
func TestOmpToolResultsAreRecordedWhereRulesReadThem(t *testing.T) {
// The runtime reports a skill's file as an absolute path on its own host, so the fixture's is
// one on the test's: `C:\...` on Windows, where a path from the root names no drive.
skillFile := filepath.Join(t.TempDir(), ".omp", "skills", "oracle-probe", "SKILL.md")
for _, tc := range []struct {
name string
payload map[string]interface{}
action string
// marker is text the result carries; "" means the result is out of scope and not recorded.
marker string
check func(t *testing.T, event map[string]interface{})
}{
{
name: "file read", action: "file.read", marker: "File content marker BCN-5WWWUQ-D01",
payload: map[string]interface{}{
"toolName": "read", "input": map[string]interface{}{"path": "notes/canary.txt"},
"details": map[string]interface{}{"meta": map[string]interface{}{"source": map[string]interface{}{"type": "path", "value": "/repo/notes/canary.txt"}}},
"content": ompTextBlocks("[notes/canary.txt#5B60]\n1:File content marker BCN-5WWWUQ-D01."),
},
},
{
name: "web page through read", action: "tool.completed", marker: "Web page marker BCN-BALBZ7-D06",
payload: map[string]interface{}{
"toolName": "read", "input": map[string]interface{}{"path": "https://docs.fixture.test/guide"},
"details": map[string]interface{}{"url": "https://docs.fixture.test/guide", "meta": map[string]interface{}{"source": map[string]interface{}{"type": "url", "value": "https://docs.fixture.test/guide"}}},
"content": ompTextBlocks("URL: https://docs.fixture.test/guide\n\n---\n\n<p>Web page marker BCN-BALBZ7-D06.</p>"),
},
},
{
// A skill body is the skill author's file, read from disk: recorded as the read of that
// SKILL.md, the row the Skills lens and every other runtime's skill load are found by.
name: "skill body", action: "file.read", marker: "Skill body marker BCN-R5H6GL-C01",
payload: map[string]interface{}{
"toolName": "read", "input": map[string]interface{}{"path": "skill://oracle-probe"},
"details": map[string]interface{}{
"resolvedPath": skillFile,
"meta": map[string]interface{}{"source": map[string]interface{}{"type": "internal", "value": "skill://oracle-probe"}},
},
"content": ompTextBlocks("[Skill file: " + skillFile + "]\n---\nname: oracle-probe\n---\nSkill body marker BCN-R5H6GL-C01.\n"),
},
check: func(t *testing.T, event map[string]interface{}) {
if got := nested(t, event, "file")["path"]; got != skillFile {
t.Fatalf("file.path = %v, want the SKILL.md the runtime read", got)
}
if got := nested(t, event, "tool")["path"]; got != "skill://oracle-probe" {
t.Fatalf("tool.path = %v, want the target as the model asked for it", got)
}
},
},
{
// Media is kept as its size and digest, the way the Claude Code hook records it: a stored
// copy of the bytes could never be decoded, and would spend the event's size budget.
name: "MCP result with media", action: "mcp.tool_invoked", marker: "Text block marker BCN-GRDUR6-D19",
payload: map[string]interface{}{
"toolName": "mcp__media_capture", "input": map[string]interface{}{"target": "screen"},
"details": map[string]interface{}{"serverName": "media", "mcpToolName": "capture"},
"content": []interface{}{
map[string]interface{}{"type": "text", "text": "Captured. Text block marker BCN-GRDUR6-D19."},
map[string]interface{}{"type": "image", "mimeType": "image/png", "data": "aW1hZ2UtYnl0ZXM="},
},
},
check: func(t *testing.T, event map[string]interface{}) {
blocks, _ := fieldsToolCallResult(event).(map[string]interface{})["content"].([]interface{})
image, _ := blocks[1].(map[string]interface{})
if _, kept := image["data"]; kept || image["bytes"] != float64(11) || image["sha256"] == "" {
t.Fatalf("image block = %v, want its size and digest in place of the bytes", image)
}
},
},
{
// What a failing server returned reached the model as well.
name: "failed MCP call", action: "tool.failed", marker: "MCP error marker",
payload: map[string]interface{}{
"toolName": "mcp__media_capture", "isError": true, "input": map[string]interface{}{"target": "screen"},
"details": map[string]interface{}{"serverName": "media", "mcpToolName": "capture"},
"content": ompTextBlocks("MCP error marker: capture device busy."),
},
},
{
// A shell's output has its own field; it is not recorded a second time as a result.
name: "shell output", action: "command.executed",
payload: map[string]interface{}{
"toolName": "bash", "input": map[string]interface{}{"command": "sh scripts/probe.sh"},
"details": map[string]interface{}{"wallTimeMs": 16.9},
"content": ompTextBlocks("Shell output marker BCN-MBS2GE-D03.\n\n\nWall time: 0.02 seconds"),
},
check: func(t *testing.T, event map[string]interface{}) {
command := nested(t, event, "command")
if command["output"] != "Shell output marker BCN-MBS2GE-D03.\n\n\nWall time: 0.02 seconds" || command["command"] != "sh scripts/probe.sh" {
t.Fatalf("command = %v, want the command and the output the model was given", command)
}
},
},
{
// A subagent's report is the runtime's own storage, not outside content.
name: "runtime resource", action: "tool.completed",
payload: map[string]interface{}{
"toolName": "read", "input": map[string]interface{}{"path": "agent://reviewer"},
"details": map[string]interface{}{
"resolvedPath": "/repo/.omp/agent/sessions/s/agents/reviewer.md",
"meta": map[string]interface{}{"source": map[string]interface{}{"type": "internal", "value": "agent://reviewer"}},
},
"content": ompTextBlocks("Subagent report."),
},
},
{
// A write's result echoes what the agent itself produced.
name: "file write", action: "file.created",
payload: map[string]interface{}{
"toolName": "write", "input": map[string]interface{}{"path": "/repo/out.md", "content": "notes"},
"details": map[string]interface{}{"resolvedPath": "/repo/out.md"},
"content": ompTextBlocks("Wrote /repo/out.md."),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
logPath := ompTestLog(t)
payload := cloneFields(tc.payload)
// "/repo", not a path under /home: on macOS /home is an automount, and every stat of a
// path beneath it costs ~40ms.
payload["type"], payload["toolCallId"], payload["sessionId"], payload["cwd"] = "tool_result", "call-1", "sess-1", "/repo"
if _, ok := payload["isError"]; !ok {
payload["isError"] = false
}
runHookWithInput(t, runOmpEvent, payload)
event := ompEventWithAction(t, logPath, tc.action)
result := fieldsToolCallResult(event)
if tc.marker == "" {
if result != nil {
t.Fatalf("gen_ai.tool.call.result = %v, want none for a result outside the scope", result)
}
} else {
if text := asymptoteobserve.ToolResultPlainText(result); !strings.Contains(text, tc.marker) {
t.Fatalf("gen_ai.tool.call.result = %v, want the content blocks the model read", result)
}
if content := nested(t, event, "content"); content["included"] != true {
t.Fatalf("content = %v, want the result's retention marker", content)
}
}
// raw keeps the payload verbatim whatever was promoted from it.
if !reflect.DeepEqual(nested(t, event, "raw", "omp")["content"], payload["content"]) {
t.Fatalf("raw.omp.content = %v, want the result as the runtime sent it", nested(t, event, "raw", "omp")["content"])
}
if tc.check != nil {
tc.check(t, event)
}
})
}
}
func TestOmpEventEditResultRecordsTheUnifiedPatch(t *testing.T) {
logPath := ompTestLog(t)
patch := "--- a/main.go\n+++ b/main.go\n@@ -1 +1 @@\n-old\n+new\n"
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "tool_result", "toolName": "edit", "toolCallId": "call-1",
"input": map[string]interface{}{"path": "/repo/main.go"},
"details": map[string]interface{}{"patch": patch, "diff": "display-only"},
"sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "file.modified")
file := nested(t, event, "file")
if file["path"] != "/repo/main.go" || file["operation"] != "modify" || file["language"] != "go" {
t.Fatalf("file = %v, want the edited Go file", file)
}
// The unified patch is the machine-readable form; the display `diff` is only a fallback, and
// it is shorter -- so the retained byte count is what distinguishes which one was recorded.
if content := nested(t, event, "content"); content["bytes"] != float64(len(patch)) {
t.Fatalf("content.bytes = %v, want the unified patch length %d", content["bytes"], len(patch))
}
}
// A command the operator ran with the `!` prefix is the one command shape here the agent did not
// originate, so it is marked rather than merged in with agent-run commands.
func TestOmpEventUserBashIsMarkedOperatorInitiated(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "user_bash", "command": "git status", "excludeFromContext": true,
"cwd": "/repo", "sessionId": "sess-1",
})
event := ompEventWithAction(t, logPath, "command.executed")
if command := nested(t, event, "command"); command["command"] != "git status" {
t.Fatalf("command = %v, want git status", command)
}
raw := nested(t, event, "raw")
if raw["omp_user_initiated"] != true {
t.Fatalf("raw.omp_user_initiated = %v, want true", raw["omp_user_initiated"])
}
if raw["omp_exclude_from_context"] != true {
t.Fatalf("raw.omp_exclude_from_context = %v, want true", raw["omp_exclude_from_context"])
}
}
// Oh My Pi reports token usage the same way Pi does, and `gen_ai.usage` stays the only place
// Beacon records it. `output` already includes `reasoning`, so reasoning gets its own key and is
// added to nothing; `totalTokens` is dropped rather than written as a field that can disagree with
// its own parts.
func TestOmpEventNormalizesTokenUsage(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "message_end", "sessionId": "sess-1",
"message": map[string]interface{}{
"role": "assistant",
"model": "anthropic/claude-opus-4",
"usage": map[string]interface{}{
"input": float64(120), "output": float64(45),
"cacheRead": float64(900), "cacheWrite": float64(30),
"reasoning": float64(12), "totalTokens": float64(1095),
"cost": map[string]interface{}{"total": 0.0123},
},
},
})
event := ompEventWithAction(t, logPath, "token.usage")
usage := nested(t, nested(t, event, "gen_ai"), "usage")
for key, want := range map[string]float64{
"input_tokens": 120, "output_tokens": 45, "cost_usd": 0.0123,
} {
if got, _ := usage[key].(float64); got != want {
t.Fatalf("gen_ai.usage.%s = %v, want %v", key, usage[key], want)
}
}
if got := nested(t, usage, "cache_read")["input_tokens"]; got != float64(900) {
t.Fatalf("cache_read.input_tokens = %v, want 900", got)
}
if got := nested(t, usage, "cache_creation")["input_tokens"]; got != float64(30) {
t.Fatalf("cache_creation.input_tokens = %v, want 30", got)
}
if got := nested(t, usage, "reasoning")["output_tokens"]; got != float64(12) {
t.Fatalf("reasoning.output_tokens = %v, want 12", got)
}
if _, ok := usage["total_tokens"]; ok {
t.Fatalf("gen_ai.usage carries a total: %v", usage)
}
// Provider and model are recorded in separate fields; the mapper's provider/model composite
// is split at write time.
if model := event["model"]; model != "claude-opus-4" {
t.Fatalf("model = %v, want the bare responding model", model)
}
if got := nested(t, nested(t, event, "gen_ai"), "provider")["name"]; got != "anthropic" {
t.Fatalf("gen_ai.provider.name = %v, want the provider the composite carried", got)
}
}
// Only the thinking parts are reasoning. The assistant's visible answer is not, and recording it as
// such would put the model's output where a reader looking for its private deliberation expects to
// find it.
func TestOmpEventRecordsOnlyThinkingPartsAsReasoning(t *testing.T) {
logPath := ompTestLog(t)
runHookWithInput(t, runOmpEvent, map[string]interface{}{
"type": "message_end", "sessionId": "sess-1",
"message": map[string]interface{}{
"role": "assistant",
"content": []interface{}{
map[string]interface{}{"type": "thinking", "thinking": "weighing two approaches"},
map[string]interface{}{"type": "text", "text": "I'll use the second one."},
},
},
})
event := ompEventWithAction(t, logPath, "agent.reasoning")
messages, ok := nested(t, nested(t, event, "gen_ai"), "output")["messages"].([]interface{})
if !ok || len(messages) != 1 {
t.Fatalf("gen_ai.output.messages = %v, want one message", messages)
}
parts, _ := messages[0].(map[string]interface{})["parts"].([]interface{})
if len(parts) != 1 {
t.Fatalf("parts = %v, want one reasoning part", parts)
}
part, _ := parts[0].(map[string]interface{})
if part["type"] != "reasoning" || part["content"] != "weighing two approaches" {
t.Fatalf("part = %v, want only the thinking text as a reasoning part", part)
}
// The retained content is the thinking text alone, not the thinking plus the visible answer.
thinking := "weighing two approaches"
if content := nested(t, event, "content"); content["bytes"] != float64(len(thinking)) {
t.Fatalf("content.bytes = %v, want just the thinking text (%d bytes); the assistant's "+
"visible answer must not be recorded as reasoning", content["bytes"], len(thinking))
}
}
// message_end fires for user and toolResult messages too. A message with neither usage nor
// reasoning must produce nothing rather than an empty row per turn.
func TestOmpEventBareMessageEndProducesNothing(t *testing.T) {
for _, message := range []map[string]interface{}{
{"role": "user", "content": []interface{}{}},
{"role": "assistant", "content": []interface{}{}},
{"role": "toolResult"},
} {
events := ompRuntime.endpointEvents(map[string]interface{}{
"type": "message_end", "message": message,
}, "sess-1")
if len(events) != 0 {
t.Fatalf("message_end for %v produced %d events, want none", message, len(events))
}
}
}