Skip to content

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

fix(context): capture model-visible skill listings as system context

Kyle Elliottb022e8715746
diff --git a/beacon-sandbox/check/invariants.go b/beacon-sandbox/check/invariants.go
index 0490758f..abb51da6 100644
--- a/beacon-sandbox/check/invariants.go
+++ b/beacon-sandbox/check/invariants.go
@@ -22,7 +22,7 @@ const MaxEventBytes = 64 * 1024
// unrecognized action is reported rather than ignored.
var KnownActions = map[string]bool{
// agent runtime activity
- "session.started": true, "session.ended": true, "session.activity": true,
+ "session.started": true, "session.ended": true, "session.activity": true, "session.context": true,
"session.compacting": true, "session.event": true, "session.created": true,
"session.deleted": true, "session.idle": true, "session.error": true,
"session.compacted": true,
diff --git a/cli/beacon-hooks/cmd/omp_approval_test.go b/cli/beacon-hooks/cmd/omp_approval_test.go
index 3fd1c40e..f8e06c4c 100644
--- a/cli/beacon-hooks/cmd/omp_approval_test.go
+++ b/cli/beacon-hooks/cmd/omp_approval_test.go
@@ -198,7 +198,7 @@ func TestOmpOnlyEventTypesAreNotInPis(t *testing.T) {
for _, name := range supportedPiEventTypes() {
pi[name] = true
}
- for _, name := range []string{"tool_approval_requested", "tool_approval_resolved", "user_python"} {
+ for _, name := range []string{"tool_approval_requested", "tool_approval_resolved", "user_python", "context"} {
if pi[name] {
t.Fatalf("Pi's extension subscribes to %q; Pi exposes no such event, and subscribing "+
"to one would put a decision nobody made into the log", name)
diff --git a/cli/beacon-hooks/cmd/omp_event.go b/cli/beacon-hooks/cmd/omp_event.go
index 206869b1..044f1d13 100644
--- a/cli/beacon-hooks/cmd/omp_event.go
+++ b/cli/beacon-hooks/cmd/omp_event.go
@@ -46,6 +46,11 @@ func runOmpEvent(cmd *cobra.Command, args []string) {
// signal Beacon has always refused to synthesize on runtimes that do not report it; `user_python`
// is the operator's own `$` code, which no tool event covers.
//
+// `context` fires before every model call and carries the whole conversation, which is the kind of
+// event this list otherwise refuses. It is here because its handler context is the only supported
+// way to read the effective system prompt: the extension sends nothing for it but the `<skills>`
+// index the model was shown, and only when that index changes.
+//
// `mcp_notification` is deliberately absent. It fires for every JSON-RPC notification a connected
// server sends, most of them routine tools/resources list refreshes, and it describes MCP transport
// plumbing rather than an action the agent took. The MCP activity worth recording is the agent
@@ -56,6 +61,7 @@ func supportedOmpEventTypes() []string {
"session_start",
"session_shutdown",
"input",
+ "context",
"tool_call",
"tool_result",
"user_bash",
diff --git a/cli/beacon-hooks/cmd/omp_event_test.go b/cli/beacon-hooks/cmd/omp_event_test.go
index 19f62128..1930a569 100644
--- a/cli/beacon-hooks/cmd/omp_event_test.go
+++ b/cli/beacon-hooks/cmd/omp_event_test.go
@@ -3,7 +3,10 @@ package cmd
import (
"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.
@@ -47,6 +50,7 @@ func ompPayloads() 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"},
@@ -218,6 +222,32 @@ func TestOmpEventPromptRecordsTextAndSource(t *testing.T) {
}
}
+// 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"])
+ }
+}
+
// 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.
diff --git a/cli/beacon-hooks/cmd/pi_family.go b/cli/beacon-hooks/cmd/pi_family.go
index 1b745ea1..34e691b9 100644
--- a/cli/beacon-hooks/cmd/pi_family.go
+++ b/cli/beacon-hooks/cmd/pi_family.go
@@ -129,6 +129,22 @@ func (f piFamily) endpointEvents(input map[string]interface{}, sessionID string)
}
return events
+ case "context":
+ // The skill index the model was shown, which the extension reads from the system prompt
+ // and sends in place of the event itself. It is system context, not a prompt: the operator
+ // did not write it, so it is never recorded under `prompt` or as input.
+ listing := asymptoteobserve.ParseSkillListing(asymptoteobserve.SystemContextSourceSystemPrompt, getFirstStr(input, "skillListing"), nil)
+ skill := listing.Fields(asymptoteobserve.DefaultStringLimit)
+ if skill == nil {
+ return nil
+ }
+ fields["gen_ai"] = mergeNested(fields["gen_ai"], skill["gen_ai"].(map[string]interface{}))
+ fields["system_context"] = skill["system_context"]
+ fields["content"] = skill["content"]
+ // baseFields copied the whole input under raw, which would store the listing a second time.
+ fields["raw"] = map[string]interface{}{f.platform: map[string]interface{}{"type": "context"}}
+ return f.one("session.context", "session", "info", "system skill listing exposed to the model", fields)
+
case "tool_call":
// The pre-execution half of a tool call: the runtime has decided to run it and named its
// arguments, but nothing has happened yet. Recorded as tool.invoked to match the Cline
diff --git a/cli/beacon-hooks/cmd/pi_family_test.go b/cli/beacon-hooks/cmd/pi_family_test.go
index 46c9a892..dfd05e84 100644
--- a/cli/beacon-hooks/cmd/pi_family_test.go
+++ b/cli/beacon-hooks/cmd/pi_family_test.go
@@ -1,6 +1,11 @@
package cmd
-import "testing"
+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
@@ -52,6 +57,32 @@ func TestPiFamilyPromotesTheToolCallIDOnBothHalves(t *testing.T) {
}
}
+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)
+ }
+}
+
// 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.
diff --git a/cli/beacon/internal/claudesession/claudesession_test.go b/cli/beacon/internal/claudesession/claudesession_test.go
index b9c51694..c5de9206 100644
--- a/cli/beacon/internal/claudesession/claudesession_test.go
+++ b/cli/beacon/internal/claudesession/claudesession_test.go
@@ -4,10 +4,13 @@ import (
"encoding/json"
"os"
"path/filepath"
+ "reflect"
+ "slices"
"strings"
"testing"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
func TestListFindsClaudeSessionsAndSubagents(t *testing.T) {
@@ -76,6 +79,79 @@ func TestMapClaudeTranscriptProducesEndpointEvents(t *testing.T) {
}
}
+// The skill_listing line is a Claude Code 2.1.291 transcript entry recorded by a beacon-oracle
+// c01-presence run, trimmed to two skills: the index is one string in `content`, and `rendered`
+// is the system-role text the model was sent.
+func TestMapSkillListingAttachmentAsSystemContext(t *testing.T) {
+ const session = "2cf4b751-7e9a-420d-81c6-99a7dd772fab"
+ ref := SessionRef{ID: session, Path: "/tmp/" + session + ".jsonl", ProjectPath: "/home/agent/project"}
+ listing := "- oracle-index: Lists the repository conventions. Skill index marker BCN-3WH4ST-C01I.\n- oracle-probe: Runs the repository probe procedure."
+ records := decodeFixture(t, []string{
+ `{"type":"attachment","attachment":{"type":"environment","snapshot":{"workingDirectory":"/home/agent/project"}},"uuid":"a0","timestamp":"2026-10-06T20:20:49.000Z","sessionId":"` + session + `","cwd":"/home/agent/project"}`,
+ `{"parentUuid":"cb3d1f0e-5d1b-44c8-8ed1-60b2e7922da2","isSidechain":false,"attachment":{"type":"skill_listing","content":"- oracle-index: Lists the repository conventions. Skill index marker BCN-3WH4ST-C01I.\n- oracle-probe: Runs the repository probe procedure.","skillCount":2,"isInitial":true,"names":["oracle-index","oracle-probe"]},"type":"attachment","uuid":"f9b0a9f9-1240-4592-b7f7-3a0654c21ed4","timestamp":"2026-10-06T20:20:49.170Z","rendered":[{"content":"<system-reminder>\nThe following skills are available for use with the Skill tool:\n\n- oracle-index: Lists the repository conventions. Skill index marker BCN-3WH4ST-C01I.\n- oracle-probe: Runs the repository probe procedure.\n</system-reminder>"}],"renderedRole":"system","userType":"external","entrypoint":"sdk-cli","cwd":"/home/agent/project","sessionId":"` + session + `","version":"2.1.291","gitBranch":"master"}`,
+ `{"type":"user","isMeta":true,"message":{"role":"user","content":"skill body"},"uuid":"u1","timestamp":"2026-10-06T20:20:50.000Z","sessionId":"` + session + `"}`,
+ })
+
+ mapped := MapSession(ref, records, MapOptions{})
+ if got, want := strings.Join(actions(mapped), ","), "session.started,session.context"; got != want {
+ t.Fatalf("actions = %q, want %q", got, want)
+ }
+ ev := findAction(t, mapped, "session.context")
+ if ev.GenAI == nil {
+ t.Fatal("session.context carries no gen_ai block")
+ }
+ parts := asymptoteobserve.GenAIText(ev.GenAI.SystemInstructions, "", asymptoteobserve.GenAIPartTypeText)
+ if want := strings.Split(listing, "\n"); !slices.Equal(parts, want) {
+ t.Fatalf("gen_ai.system_instructions parts = %q, want one per skill %q", parts, want)
+ }
+ if ev.Prompt != nil || ev.GenAI.Input != nil {
+ t.Fatalf("the listing was recorded as operator input: prompt=%+v gen_ai.input=%+v", ev.Prompt, ev.GenAI.Input)
+ }
+ wantContext := &schema.SystemContextInfo{
+ Kind: asymptoteobserve.SystemContextSkillListing,
+ Source: asymptoteobserve.SystemContextSourceTranscript,
+ Skills: []schema.SkillRefInfo{
+ {SkillName: "oracle-index", SkillNameHash: asymptoteobserve.SkillNameHash("oracle-index")},
+ {SkillName: "oracle-probe", SkillNameHash: asymptoteobserve.SkillNameHash("oracle-probe")},
+ },
+ }
+ if !reflect.DeepEqual(ev.SystemContext, wantContext) {
+ t.Fatalf("system_context = %+v, want %+v", ev.SystemContext, wantContext)
+ }
+ if ev.Content == nil || !ev.Content.Included || ev.Content.Bytes != len(listing) {
+ t.Fatalf("content = %+v, want the retained listing's marker", ev.Content)
+ }
+ provenance, _ := ev.Raw["claude_attachment"].(map[string]interface{})
+ if provenance["type"] != "skill_listing" || provenance["is_initial"] != true || provenance["skill_count"] != float64(2) {
+ t.Fatalf("raw.claude_attachment = %#v, want the attachment's type, isInitial and skillCount", ev.Raw["claude_attachment"])
+ }
+ if err := ev.Validate(); err != nil {
+ t.Fatalf("context event failed validation: %v", err)
+ }
+}
+
+// A subagent transcript starts its events with gen_ai.agent already set, so the listing has to be
+// recorded alongside it; and a listing written as text blocks, without names, reads the same as a
+// string.
+func TestMapSkillListingOnSubagentTranscriptWithBlockContent(t *testing.T) {
+ ref := SessionRef{ID: "agent-a", Path: "/tmp/sess-1/subagents/agent-a.jsonl", ProjectPath: "/tmp/repo", IsSidechain: true, ParentSessionID: "sess-1"}
+ records := decodeFixture(t, []string{
+ `{"type":"attachment","isSidechain":true,"attachment":{"type":"skill_listing","content":[{"type":"text","text":"- deploy: Deploy applications."},{"type":"text","text":"- review: Review a diff."}]},"uuid":"a1","timestamp":"2026-09-19T22:00:01.000Z","sessionId":"sess-1","cwd":"/tmp/repo"}`,
+ })
+
+ ev := findAction(t, MapSession(ref, records, MapOptions{}), "session.context")
+ if ev.GenAI == nil || ev.GenAI.Agent == nil {
+ t.Fatalf("gen_ai = %+v, want the subagent's agent info", ev.GenAI)
+ }
+ want := []string{"- deploy: Deploy applications.", "- review: Review a diff."}
+ if got := asymptoteobserve.GenAIText(ev.GenAI.SystemInstructions, "", asymptoteobserve.GenAIPartTypeText); !slices.Equal(got, want) {
+ t.Fatalf("subagent gen_ai.system_instructions = %q, want %q", got, want)
+ }
+ if ev.SystemContext == nil || len(ev.SystemContext.Skills) != 2 {
+ t.Fatalf("subagent system_context = %+v, want both skills named", ev.SystemContext)
+ }
+}
+
func TestCollectOnceUsesCursorAndPrintDoesNotAdvance(t *testing.T) {
dir := t.TempDir()
projects := filepath.Join(dir, "projects")
diff --git a/cli/beacon/internal/claudesession/mapper.go b/cli/beacon/internal/claudesession/mapper.go
index 12f307dd..379b999b 100644
--- a/cli/beacon/internal/claudesession/mapper.go
+++ b/cli/beacon/internal/claudesession/mapper.go
@@ -76,6 +76,10 @@ func (m *mapper) consume(record Record) {
m.consumeUser(record, emit)
case "assistant":
m.consumeAssistant(record, emit)
+ case "attachment":
+ if emit && stringValue(entry.Attachment["type"]) == "skill_listing" {
+ m.emitSkillListing(record, entry.Attachment)
+ }
case "summary":
if emit {
m.emitSummary(record)
@@ -176,6 +180,32 @@ func (m *mapper) emitSessionStarted(record Record) {
m.append(record, "session.started", ev)
}
+// emitSkillListing records the skill index Claude Code showed the model. The attachment's names are
+// the skills it listed, which is what splits the index into one entry per skill. isInitial and
+// skillCount are kept as provenance: an attachment that is not the initial one may list only the
+// skills added since, which the index text alone does not say.
+func (m *mapper) emitSkillListing(record Record, attachment map[string]interface{}) {
+ var names []string
+ listed, _ := attachment["names"].([]interface{})
+ for _, name := range listed {
+ names = append(names, stringValue(name))
+ }
+ listing := asymptoteobserve.ParseSkillListing(asymptoteobserve.SystemContextSourceTranscript, toolResultText(attachment["content"]), names)
+ if len(listing.Entries) == 0 {
+ return
+ }
+ ev := m.base(record, "session.context", "session", schema.SeverityInfo, schema.FidelityObserved, "Claude Code skill listing exposed to the model")
+ listing.Apply(&ev, asymptoteobserve.DefaultRawStringLimit)
+ provenance := map[string]interface{}{"type": "skill_listing"}
+ for key, field := range map[string]string{"isInitial": "is_initial", "skillCount": "skill_count"} {
+ if value, ok := attachment[key]; ok {
+ provenance[field] = value
+ }
+ }
+ ev.Raw = map[string]interface{}{"claude_session": m.raw(record), "claude_attachment": provenance}
+ m.append(record, "session.context:skill_listing", ev)
+}
+
func (m *mapper) emitPrompt(record Record, text string) {
ev := m.base(record, "prompt.submitted", "prompt", schema.SeverityInfo, schema.FidelityObserved, "Prompt submitted to Claude Code")
ev.Prompt = &schema.PromptInfo{ID: m.promptID, Text: text}
diff --git a/cli/beacon/internal/endpoint/asymptote/asymptote_test.go b/cli/beacon/internal/endpoint/asymptote/asymptote_test.go
index 5cbb0f1c..2a736ee3 100644
--- a/cli/beacon/internal/endpoint/asymptote/asymptote_test.go
+++ b/cli/beacon/internal/endpoint/asymptote/asymptote_test.go
@@ -4,12 +4,17 @@ import (
"bufio"
"encoding/json"
"errors"
+ "io/fs"
"os"
+ "os/exec"
"path/filepath"
"strings"
"testing"
"testing/fstest"
+ "github.com/pelletier/go-toml/v2"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"
)
@@ -264,6 +269,79 @@ func TestRenderedPrivacyConfigsValidateWithVector(t *testing.T) {
}
}
+// Runs the metadata-only transform on every sample event with Vector's own VRL runtime. No retained
+// text survives, and a session.context skill index still says what it was and which skills it
+// listed, so the backend can tell it from any other session.context and join it to inventory.
+func TestMetadataTransformKeepsSystemContextWithVector(t *testing.T) {
+ vector := os.Getenv("BEACON_TEST_VECTOR_BIN")
+ if vector == "" {
+ t.Skip("set BEACON_TEST_VECTOR_BIN to run the metadata transform with Vector")
+ }
+ var transforms struct {
+ Transforms map[string]struct {
+ Source string `toml:"source"`
+ } `toml:"transforms"`
+ }
+ data, err := fs.ReadFile(packFS, privacyTransformAsset)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := toml.Unmarshal(data, &transforms); err != nil {
+ t.Fatal(err)
+ }
+ dir := t.TempDir()
+ program := filepath.Join(dir, "runtime.vrl")
+ if err := os.WriteFile(program, []byte(transforms.Transforms["beacon_runtime_metadata"].Source), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ samples, err := fs.ReadFile(packFS, "pack/sample-event.jsonl")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var input strings.Builder
+ for _, line := range strings.Split(strings.TrimSpace(string(samples)), "\n") {
+ wrapped, _ := json.Marshal(map[string]string{"message": line})
+ input.Write(append(wrapped, '\n'))
+ }
+ inputPath := filepath.Join(dir, "input.jsonl")
+ if err := os.WriteFile(inputPath, []byte(input.String()), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ out, err := exec.Command(vector, "vrl", "--input", inputPath, "--program", program, "--print-object").CombinedOutput()
+ if err != nil {
+ t.Fatalf("vector vrl: %v\n%s", err, out)
+ }
+ sawListing := false
+ for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
+ var wrapped struct {
+ Message string `json:"message"`
+ }
+ if err := json.Unmarshal([]byte(line), &wrapped); err != nil || wrapped.Message == "" {
+ continue
+ }
+ var event schema.Event
+ if err := json.Unmarshal([]byte(wrapped.Message), &event); err != nil {
+ t.Fatalf("transformed event is not an event: %v\n%s", err, wrapped.Message)
+ }
+ if event.GenAI != nil && (event.GenAI.SystemInstructions != nil || event.GenAI.Input != nil) {
+ t.Fatalf("metadata-only kept retained text: %s", wrapped.Message)
+ }
+ if event.SystemContext == nil {
+ continue
+ }
+ sawListing = true
+ if event.SystemContext.Kind != schema.SystemContextSkillListing || len(event.SystemContext.Skills) == 0 || event.SystemContext.Skills[0].SkillNameHash == "" {
+ t.Fatalf("system_context = %+v, want the listing's kind and named skills", event.SystemContext)
+ }
+ if strings.Contains(wrapped.Message, "release checklist") || event.Content == nil || event.Content.Retention != schema.ContentRetentionMetadata {
+ t.Fatalf("skill listing text or retention survived metadata-only: %s", wrapped.Message)
+ }
+ }
+ if !sawListing {
+ t.Fatal("no sample event carries a skill listing; the pack's samples should show one")
+ }
+}
+
func TestSecretsFileContentIsTheShapeTheTemplateReads(t *testing.T) {
content := SecretsFileContent(`bcn_device_abcdefgh_` + strings.Repeat("x", 43))
var parsed map[string]string
diff --git a/cli/beacon/internal/endpoint/asymptote/pack/README.md b/cli/beacon/internal/endpoint/asymptote/pack/README.md
index 37a8b342..06a674f5 100644
--- a/cli/beacon/internal/endpoint/asymptote/pack/README.md
+++ b/cli/beacon/internal/endpoint/asymptote/pack/README.md
@@ -25,7 +25,9 @@ history.
This hand-run pack uses Standard privacy. `beacon endpoint connect
--privacy-mode metadata-only` renders an endpoint-specific config that removes
retained text, raw fields, diffs, inventory content, and MCP definitions before
-buffering or upload.
+buffering or upload. It keeps metadata: on a `session.context` skill index, the
+`system_context` block that says what the event is and names each listed skill,
+while the index text in `gen_ai.system_instructions` is removed.
## Prerequisites
diff --git a/cli/beacon/internal/endpoint/asymptote/pack/metadata-transforms.toml b/cli/beacon/internal/endpoint/asymptote/pack/metadata-transforms.toml
index fe5eb35d..8555c499 100644
--- a/cli/beacon/internal/endpoint/asymptote/pack/metadata-transforms.toml
+++ b/cli/beacon/internal/endpoint/asymptote/pack/metadata-transforms.toml
@@ -15,8 +15,14 @@ del(event.command.output)
del(event.file.diff)
del(event.gen_ai.input)
del(event.gen_ai.output)
+del(event.gen_ai.system_instructions)
del(event.gen_ai.tool.call.arguments)
del(event.gen_ai.tool.call.result)
+if is_object(event.gen_ai) && is_empty(object!(event.gen_ai)) {
+ del(event.gen_ai)
+}
+# system_context says what a session.context event recorded and names the skills a skill index
+# listed, the way inventory names skills on disk; its text was in gen_ai.system_instructions.
if is_object(event.content) {
event.content.retention = "metadata"
event.content.included = false
diff --git a/cli/beacon/internal/endpoint/asymptote/pack/sample-event.jsonl b/cli/beacon/internal/endpoint/asymptote/pack/sample-event.jsonl
index 2eb3c774..376daa55 100644
--- a/cli/beacon/internal/endpoint/asymptote/pack/sample-event.jsonl
+++ b/cli/beacon/internal/endpoint/asymptote/pack/sample-event.jsonl
@@ -1,3 +1,4 @@
{"timestamp":"2026-09-03T18:20:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"agent.detected","category":"validation","id":"5a1d2c9e-6b3f-5f4a-9c8d-1e2f3a4b5c6d"},"severity":"info","endpoint":{"hostname":"example-mac","os":"darwin","agent_version":"1.2.7"},"user":{"name":"local-user"},"harness":{"name":"test_harness","version":"test"},"destination":{"type":"asymptote","mode":"asymptote_managed_http","status":"configured"},"message":"Beacon endpoint Asymptote validation event"}
{"timestamp":"2026-09-03T18:21:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"command.executed","category":"command","id":"7c2e4f1a-8d9b-5a6c-b1e2-3f4a5b6c7d8e","fidelity":"observed"},"severity":"info","endpoint":{"hostname":"example-mac","os":"darwin","agent_version":"1.2.7"},"user":{"name":"local-user","uid":"501"},"harness":{"name":"claude_code","version":"1.0.120","collection_method":"hook"},"session":{"id":"conversation-1","working_directory":"/Users/local-user/repo"},"tool":{"name":"Bash","command":"go test ./...","path":"/bin/zsh"},"command":{"command":"go test ./...","exit_code":0,"duration_ms":1234},"repository":"file:///Users/local-user/repo","branch":"main","message":"Shell command executed"}
{"timestamp":"2026-09-03T18:22:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"token.usage","category":"metric","id":"9e3f5a2b-0c1d-5e7f-a3b4-5c6d7e8f9a0b","fidelity":"observed"},"severity":"info","endpoint":{"hostname":"example-mac","os":"darwin","agent_version":"1.2.7"},"user":{"name":"local-user"},"harness":{"name":"claude_code","version":"1.0.120","collection_method":"otlp"},"session":{"id":"conversation-1"},"model":"claude-fable-5-1","gen_ai":{"usage":{"input_tokens":1200,"output_tokens":340,"cache_read":{"input_tokens":8000}}},"message":"claude_code.api_request"}
+{"timestamp":"2026-09-03T18:23:00.000000000Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"id":"b4d6e8f0-2a1c-5e3b-9d7f-0c1e2f3a4b5c","kind":"agent_runtime","action":"session.context","category":"session","fidelity":"observed"},"severity":"info","endpoint":{"hostname":"example-mac","os":"darwin","agent_version":"1.2.7"},"user":{"name":"local-user"},"harness":{"name":"claude_code","collection_method":"poll"},"origin":"local","session":{"id":"conversation-1","working_directory":"/Users/local-user/repo"},"system_context":{"kind":"skill_listing","source":"transcript","skills":[{"skill_name":"deploy","skill_name_hash":"sha256:b7bd55c11b781b0ccc43aa6e57f9dadf0660e9d1d4e27e0979ee43a407d454ae"},{"skill_name":"review","skill_name_hash":"sha256:c97ace4c8fef2cee8fa0f3c9f52aab18dbd4f42438afe362ffb8f75ce4c04b84"}]},"content":{"retention":"full","included":true,"hash":"5ec6d82224aa0afde0b91d068ed418031926eeb387b4b528baebd311b2f93c6b","bytes":117},"gen_ai":{"system_instructions":[{"content":"- deploy: Deploy applications with the release checklist.","type":"text"},{"content":"- review: Review a diff against the repository conventions.","type":"text"}]},"repository":"/Users/local-user/repo","branch":"main","message":"Claude Code skill listing exposed to the model","raw":{"claude_attachment":{"is_initial":true,"skill_count":2,"type":"skill_listing"},"claude_session":{"entrypoint":"cli","is_sidechain":false,"line":1,"source_path":"/Users/local-user/.claude/projects/-Users-local-user-repo/conversation-1.jsonl","user_type":"","version":"2.1.291"}}}
diff --git a/cli/beacon/internal/endpoint/dashboard/traces.go b/cli/beacon/internal/endpoint/dashboard/traces.go
index c6054627..e40a9e95 100644
--- a/cli/beacon/internal/endpoint/dashboard/traces.go
+++ b/cli/beacon/internal/endpoint/dashboard/traces.go
@@ -1,7 +1,6 @@
package dashboard
import (
- "encoding/json"
"fmt"
"net/http"
"sort"
@@ -700,89 +699,14 @@ func RetainedOutputText(genAI *schema.GenAIInfo, partType string) string {
}
// retainedOutputText joins the assistant parts of partType in
-// gen_ai.output.messages, in order. It reads both shapes Beacon writes: the
-// semconv {"role","parts":[{"type","content"}]} form and the
-// {"role","content":[{"type","text"}]} form some session mappers use. Parts of
-// another type -- reasoning on a message event, tool calls -- are skipped, so
-// an assistant message never shows its reasoning as what it said.
+// gen_ai.output.messages, in order, as asymptoteobserve.GenAIText reads them.
+// Parts of another type -- reasoning on a message event, tool calls -- are
+// skipped, so an assistant message never shows its reasoning as what it said.
func retainedOutputText(genAI *schema.GenAIInfo, partType string) string {
- if genAI == nil || genAI.Output == nil || genAI.Output.Messages == nil {
+ if genAI == nil || genAI.Output == nil {
return ""
}
- // The collector keeps an OTLP messages attribute that is not JSON as the
- // bare string it arrived as; that string is the output text itself.
- if text, ok := genAI.Output.Messages.(string); ok {
- if partType == asymptoteobserve.GenAIPartTypeText {
- return strings.TrimSpace(text)
- }
- return ""
- }
- var texts []string
- for _, message := range genAIItems(genAI.Output.Messages) {
- msg, ok := message.(map[string]interface{})
- if !ok {
- continue
- }
- if role, _ := msg["role"].(string); role != "" && role != asymptoteobserve.RoleAssistant {
- continue
- }
- parts, hasParts := msg["parts"]
- if !hasParts {
- parts = msg["content"]
- }
- if text, ok := parts.(string); ok {
- // A bare string body is the message's text, never its reasoning.
- if partType == asymptoteobserve.GenAIPartTypeText && strings.TrimSpace(text) != "" {
- texts = append(texts, strings.TrimSpace(text))
- }
- continue
- }
- for _, raw := range genAIItems(parts) {
- part, ok := raw.(map[string]interface{})
- if !ok {
- continue
- }
- if kind, _ := part["type"].(string); kind != partType {
- continue
- }
- text, _ := part["content"].(string)
- if strings.TrimSpace(text) == "" {
- text, _ = part["text"].(string)
- }
- if trimmed := strings.TrimSpace(text); trimmed != "" {
- texts = append(texts, trimmed)
- }
- }
- }
- return strings.Join(texts, "\n\n")
-}
-
-// genAIItems normalizes a messages or parts value to a slice. Events decoded
-// from the log hold []interface{}; events built in memory by a mapper can hold
-// a typed slice, which is walked through a JSON round trip rather than one case
-// per Go type.
-func genAIItems(value interface{}) []interface{} {
- switch typed := value.(type) {
- case nil:
- return nil
- case []interface{}:
- return typed
- case []map[string]interface{}:
- out := make([]interface{}, len(typed))
- for i, item := range typed {
- out[i] = item
- }
- return out
- }
- data, err := json.Marshal(value)
- if err != nil {
- return nil
- }
- var out []interface{}
- if err := json.Unmarshal(data, &out); err != nil {
- return nil
- }
- return out
+ return strings.Join(asymptoteobserve.GenAIText(genAI.Output.Messages, asymptoteobserve.RoleAssistant, partType), "\n\n")
}
func contentFromText(text string, info *schema.ContentInfo) *TraceContentV1 {
diff --git a/cli/beacon/internal/endpoint/elastic/elastic_test.go b/cli/beacon/internal/endpoint/elastic/elastic_test.go
index 88effff9..b242dbf0 100644
--- a/cli/beacon/internal/endpoint/elastic/elastic_test.go
+++ b/cli/beacon/internal/endpoint/elastic/elastic_test.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path/filepath"
+ "slices"
"strings"
"testing"
"testing/fstest"
@@ -311,6 +312,48 @@ func TestIngestPipelineKeepsNativeVCS(t *testing.T) {
}
}
+// system_context says what a session.context event recorded, so it is copied under beacon.* and
+// mapped as keywords. gen_ai.system_instructions stays where the semconv puts it, mapped as the
+// parts list every writer now emits, so an index cannot be fixed to another shape by whichever
+// event arrives first.
+func TestIngestPipelineKeepsSystemContext(t *testing.T) {
+ pipeline := mustRead("pack/ingest-pipeline.json")
+ if !strings.Contains(pipeline, "if (ctx.system_context instanceof Map) { beacon.system_context = ctx.system_context; }") {
+ t.Fatal("ingest pipeline does not copy system_context under beacon")
+ }
+ var parsed struct {
+ Processors []map[string]map[string]interface{} `json:"processors"`
+ }
+ if err := json.Unmarshal([]byte(pipeline), &parsed); err != nil {
+ t.Fatal(err)
+ }
+ removed := false
+ for _, processor := range parsed.Processors {
+ fields, _ := processor["remove"]["field"].([]interface{})
+ removed = removed || slices.Contains(fields, interface{}("system_context"))
+ }
+ if !removed {
+ t.Fatal("the source system_context is not removed after it is copied")
+ }
+
+ var mappings map[string]interface{}
+ if err := json.Unmarshal([]byte(mustRead("pack/component-template-mappings.json")), &mappings); err != nil {
+ t.Fatal(err)
+ }
+ props := mappings["template"].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
+ context := props["beacon"].(map[string]interface{})["properties"].(map[string]interface{})["system_context"].(map[string]interface{})["properties"].(map[string]interface{})
+ skills := context["skills"].(map[string]interface{})["properties"].(map[string]interface{})
+ for name, mapping := range map[string]interface{}{"kind": context["kind"], "skill_name": skills["skill_name"], "skill_name_hash": skills["skill_name_hash"]} {
+ if mapping.(map[string]interface{})["type"] != "keyword" {
+ t.Fatalf("beacon.system_context %s mapping = %#v, want keyword", name, mapping)
+ }
+ }
+ instructions := props["gen_ai"].(map[string]interface{})["properties"].(map[string]interface{})["system_instructions"].(map[string]interface{})["properties"].(map[string]interface{})
+ if instructions["content"].(map[string]interface{})["type"] != "match_only_text" {
+ t.Fatalf("gen_ai.system_instructions.content mapping = %#v, want text", instructions["content"])
+ }
+}
+
// prompt.id names the turn an event belongs to and is on events that carry no prompt text, so the
// pipeline copies it into beacon.prompt on its own -- not only alongside the text -- before the
// remove processor drops the source prompt object, and the template maps it as a keyword.
diff --git a/cli/beacon/internal/endpoint/elastic/pack/component-template-mappings.json b/cli/beacon/internal/endpoint/elastic/pack/component-template-mappings.json
index c0ae14fa..846dc3f6 100644
--- a/cli/beacon/internal/endpoint/elastic/pack/component-template-mappings.json
+++ b/cli/beacon/internal/endpoint/elastic/pack/component-template-mappings.json
@@ -104,6 +104,18 @@
"text": { "type": "match_only_text" }
}
},
+ "system_context": {
+ "properties": {
+ "kind": { "type": "keyword" },
+ "source": { "type": "keyword" },
+ "skills": {
+ "properties": {
+ "skill_name": { "type": "keyword" },
+ "skill_name_hash": { "type": "keyword" }
+ }
+ }
+ }
+ },
"content": {
"properties": {
"retention": { "type": "keyword" },
@@ -226,6 +238,12 @@
"properties": {
"model": { "type": "keyword" }
}
+ },
+ "system_instructions": {
+ "properties": {
+ "type": { "type": "keyword" },
+ "content": { "type": "match_only_text" }
+ }
}
}
},
diff --git a/cli/beacon/internal/endpoint/elastic/pack/ingest-pipeline.json b/cli/beacon/internal/endpoint/elastic/pack/ingest-pipeline.json
index 0e6ba971..e2479d78 100644
--- a/cli/beacon/internal/endpoint/elastic/pack/ingest-pipeline.json
+++ b/cli/beacon/internal/endpoint/elastic/pack/ingest-pipeline.json
@@ -38,7 +38,7 @@
"script": {
"description": "Copy Beacon fields to ECS and preserve native fields under beacon.*",
"lang": "painless",
- "source": "Map ensure(Map parent, String key) { if (parent[key] == null || !(parent[key] instanceof Map)) { parent[key] = new HashMap(); } return parent[key]; } Map beacon = ensure(ctx, 'beacon'); if (ctx.vendor != null) { beacon.vendor = ctx.vendor; } if (ctx.product != null) { beacon.product = ctx.product; } if (ctx.schema_version != null) { beacon.schema_version = ctx.schema_version; } if (ctx.sequence != null) { beacon.sequence = ctx.sequence; } if (ctx.severity != null) { beacon.severity = ctx.severity; } if (ctx.field_truncated != null) { beacon.truncated = ctx.field_truncated; } Map agent = ensure(ctx, 'agent'); agent.name = 'beacon'; agent.type = 'endpoint-agent'; if (ctx.endpoint instanceof Map && ctx.endpoint.agent_version != null) { agent.version = ctx.endpoint.agent_version; } else if (ctx.schema_version != null) { agent.version = ctx.schema_version; } Map event = ensure(ctx, 'event'); event.kind = 'event'; if (ctx.event instanceof Map) { if (ctx.event.action != null) { event.action = ctx.event.action; } if (ctx.event.category != null) { event.category = [ctx.event.category.toString()]; } } if (ctx.severity != null) { Map sev = new HashMap(); sev.put('info', 2); sev.put('low', 3); sev.put('medium', 4); sev.put('high', 5); sev.put('critical', 6); String key = ctx.severity.toString().toLowerCase(); if (sev.containsKey(key)) { event.severity = sev[key]; } } if (ctx.endpoint instanceof Map) { Map host = ensure(ctx, 'host'); if (ctx.endpoint.hostname != null) { host.hostname = ctx.endpoint.hostname; host.name = ctx.endpoint.hostname; } if (ctx.endpoint.os != null) { Map os = ensure(host, 'os'); String osName = ctx.endpoint.os.toString(); os.family = osName; os.type = osName == 'darwin' ? 'macos' : osName; } } if (ctx.user instanceof Map) { Map user = ensure(ctx, 'user'); if (ctx.user.name != null) { user.name = ctx.user.name; } if (ctx.user.uid != null) { user.id = ctx.user.uid; } } if (ctx.harness instanceof Map) { Map harness = ensure(beacon, 'harness'); if (ctx.harness.name != null) { harness.name = ctx.harness.name; ensure(ctx, 'service').name = ctx.harness.name; } if (ctx.harness.version != null) { harness.version = ctx.harness.version; } if (ctx.harness.executable_path != null) { harness.executable_path = ctx.harness.executable_path; } if (ctx.harness.config_path != null) { harness.config_path = ctx.harness.config_path; } } if (ctx.session instanceof Map) { Map session = ensure(beacon, 'session'); if (ctx.session.id != null) { session.id = ctx.session.id; if (ctx.trace == null) { ctx.trace = new HashMap(); } if (ctx.trace.id == null && ctx.session.id.toString().length() == 32) { ctx.trace.id = ctx.session.id; } } if (ctx.session.working_directory != null) { ensure(ctx, 'process').working_directory = ctx.session.working_directory; } } if (ctx.tool instanceof Map) { Map tool = ensure(beacon, 'tool'); if (ctx.tool.name != null) { tool.name = ctx.tool.name; } Map process = ensure(ctx, 'process'); if (process.command_line == null && ctx.tool.command != null) { process.command_line = ctx.tool.command; } if (ctx.tool.path != null) { process.executable = ctx.tool.path; } } if (ctx.command instanceof Map) { Map process = ensure(ctx, 'process'); if (ctx.command.command != null) { process.command_line = ctx.command.command; } if (ctx.command.exit_code != null) { process.exit_code = ctx.command.exit_code; } if (ctx.command.duration_ms != null) { event.duration = ((long)ctx.command.duration_ms) * 1000000L; } } if (ctx.file instanceof Map) { Map file = ensure(ctx, 'file'); Map beaconFile = ensure(beacon, 'file'); if (ctx.file.path != null) { String p = ctx.file.path.toString(); file.path = p; int slash = p.lastIndexOf('/'); String name = slash >= 0 ? p.substring(slash + 1) : p; if (slash > 0) { file.directory = p.substring(0, slash); } file.name = name; int dot = name.lastIndexOf('.'); if (dot > 0 && dot < name.length() - 1) { file.extension = name.substring(dot + 1); } } if (ctx.file.diff_hash != null) { ensure(file, 'hash').sha256 = ctx.file.diff_hash; } if (ctx.file.diff_bytes != null) { file.size = ctx.file.diff_bytes; } if (ctx.file.operation != null) { beaconFile.operation = ctx.file.operation; } if (ctx.file.language != null) { beaconFile.language = ctx.file.language; } } if (ctx.mcp instanceof Map) { Map mcp = ensure(beacon, 'mcp'); if (ctx.mcp.server != null) { mcp.server = ctx.mcp.server; } if (ctx.mcp.tool != null) { mcp.tool = ctx.mcp.tool; } } if (ctx.approval instanceof Map) { Map approval = ensure(beacon, 'approval'); if (ctx.approval.required != null) { approval.required = ctx.approval.required; } if (ctx.approval.decision != null) { String decision = ctx.approval.decision.toString(); approval.decision = decision; if (decision == 'allow' || decision == 'approved') { event.outcome = 'success'; } else if (decision == 'deny' || decision == 'denied' || decision == 'block' || decision == 'blocked') { event.outcome = 'failure'; } } if (ctx.approval.reason != null) { approval.reason = ctx.approval.reason; } } if (ctx.policy instanceof Map) { Map policy = ensure(beacon, 'policy'); if (ctx.policy.id != null) { policy.id = ctx.policy.id; } if (ctx.policy.name != null) { policy.name = ctx.policy.name; } if (ctx.policy.decision != null) { policy.decision = ctx.policy.decision; } if (ctx.policy.enforcement != null) { policy.enforcement = ctx.policy.enforcement; } if (ctx.policy.reason != null) { policy.reason = ctx.policy.reason; } } if (ctx.prompt instanceof Map) { if (ctx.prompt.id != null) { ensure(beacon, 'prompt').id = ctx.prompt.id; } if (ctx.prompt.text != null) { ensure(beacon, 'prompt').text = ctx.prompt.text; } } if (ctx.content instanceof Map) { Map content = ensure(beacon, 'content'); if (ctx.content.retention != null) { content.retention = ctx.content.retention; } if (ctx.content.included != null) { content.included = ctx.content.included; } if (ctx.content.redacted != null) { content.redacted = ctx.content.redacted; } if (ctx.content.truncated != null) { content.truncated = ctx.content.truncated; } } if (ctx.destination instanceof Map) { Map destination = ensure(beacon, 'destination'); if (ctx.destination.type != null) { destination.type = ctx.destination.type; } if (ctx.destination.mode != null) { destination.mode = ctx.destination.mode; } if (ctx.destination.status != null) { destination.status = ctx.destination.status; } } if (ctx.health instanceof Map) { Map health = ensure(beacon, 'health'); if (ctx.health.component != null) { health.component = ctx.health.component; } if (ctx.health.status != null) { health.status = ctx.health.status; } if (ctx.health.reason != null) { health.reason = ctx.health.reason; } } if (ctx.model != null) { ensure(ensure(ctx, 'gen_ai'), 'request').model = ctx.model; } Map nativeVcs = ctx.vcs instanceof Map ? ctx.vcs : null; ctx.remove('vcs'); String vcsUrl = null; String vcsHeadName = null; String vcsRevision = null; if (nativeVcs != null) { beacon.vcs = nativeVcs; if (nativeVcs.repository instanceof Map && nativeVcs.repository.url instanceof Map && nativeVcs.repository.url.full != null) { vcsUrl = nativeVcs.repository.url.full.toString(); } if (nativeVcs.ref instanceof Map && nativeVcs.ref.head instanceof Map) { if (nativeVcs.ref.head.name != null) { vcsHeadName = nativeVcs.ref.head.name.toString(); } if (nativeVcs.ref.head.revision != null) { vcsRevision = nativeVcs.ref.head.revision.toString(); } } } if (vcsUrl == null && ctx.repository != null) { vcsUrl = ctx.repository.toString(); } if (vcsHeadName == null && ctx.branch != null) { vcsHeadName = ctx.branch.toString(); } if (vcsUrl != null) { ensure(ensure(ctx, 'vcs'), 'repository').url = vcsUrl; } if (vcsHeadName != null) { ensure(ensure(ensure(ctx, 'vcs'), 'ref'), 'head').name = vcsHeadName; } if (vcsRevision != null) { ensure(ensure(ensure(ctx, 'vcs'), 'ref'), 'head').revision = vcsRevision; } if (ctx.raw instanceof Map) { Map otel = ensure(beacon, 'otel'); if (ctx.raw.otel_signal != null) { otel.signal = ctx.raw.otel_signal; } if (ctx.raw.metric_name != null) { ensure(beacon, 'metric').name = ctx.raw.metric_name; if (event.action == null || event.action == 'metric.observed') { event.action = ctx.raw.metric_name; } } if (ctx.raw.metric_description != null) { otel.metric_description = ctx.raw.metric_description; } if (ctx.raw.metric_unit != null) { otel.metric_unit = ctx.raw.metric_unit; } if (ctx.raw.attribute_count != null) { otel.attribute_count = ctx.raw.attribute_count; } if (ctx.raw.attributes != null) { otel.attributes = ctx.raw.attributes; } }"
+ "source": "Map ensure(Map parent, String key) { if (parent[key] == null || !(parent[key] instanceof Map)) { parent[key] = new HashMap(); } return parent[key]; } Map beacon = ensure(ctx, 'beacon'); if (ctx.vendor != null) { beacon.vendor = ctx.vendor; } if (ctx.product != null) { beacon.product = ctx.product; } if (ctx.schema_version != null) { beacon.schema_version = ctx.schema_version; } if (ctx.sequence != null) { beacon.sequence = ctx.sequence; } if (ctx.severity != null) { beacon.severity = ctx.severity; } if (ctx.field_truncated != null) { beacon.truncated = ctx.field_truncated; } Map agent = ensure(ctx, 'agent'); agent.name = 'beacon'; agent.type = 'endpoint-agent'; if (ctx.endpoint instanceof Map && ctx.endpoint.agent_version != null) { agent.version = ctx.endpoint.agent_version; } else if (ctx.schema_version != null) { agent.version = ctx.schema_version; } Map event = ensure(ctx, 'event'); event.kind = 'event'; if (ctx.event instanceof Map) { if (ctx.event.action != null) { event.action = ctx.event.action; } if (ctx.event.category != null) { event.category = [ctx.event.category.toString()]; } } if (ctx.severity != null) { Map sev = new HashMap(); sev.put('info', 2); sev.put('low', 3); sev.put('medium', 4); sev.put('high', 5); sev.put('critical', 6); String key = ctx.severity.toString().toLowerCase(); if (sev.containsKey(key)) { event.severity = sev[key]; } } if (ctx.endpoint instanceof Map) { Map host = ensure(ctx, 'host'); if (ctx.endpoint.hostname != null) { host.hostname = ctx.endpoint.hostname; host.name = ctx.endpoint.hostname; } if (ctx.endpoint.os != null) { Map os = ensure(host, 'os'); String osName = ctx.endpoint.os.toString(); os.family = osName; os.type = osName == 'darwin' ? 'macos' : osName; } } if (ctx.user instanceof Map) { Map user = ensure(ctx, 'user'); if (ctx.user.name != null) { user.name = ctx.user.name; } if (ctx.user.uid != null) { user.id = ctx.user.uid; } } if (ctx.harness instanceof Map) { Map harness = ensure(beacon, 'harness'); if (ctx.harness.name != null) { harness.name = ctx.harness.name; ensure(ctx, 'service').name = ctx.harness.name; } if (ctx.harness.version != null) { harness.version = ctx.harness.version; } if (ctx.harness.executable_path != null) { harness.executable_path = ctx.harness.executable_path; } if (ctx.harness.config_path != null) { harness.config_path = ctx.harness.config_path; } } if (ctx.session instanceof Map) { Map session = ensure(beacon, 'session'); if (ctx.session.id != null) { session.id = ctx.session.id; if (ctx.trace == null) { ctx.trace = new HashMap(); } if (ctx.trace.id == null && ctx.session.id.toString().length() == 32) { ctx.trace.id = ctx.session.id; } } if (ctx.session.working_directory != null) { ensure(ctx, 'process').working_directory = ctx.session.working_directory; } } if (ctx.tool instanceof Map) { Map tool = ensure(beacon, 'tool'); if (ctx.tool.name != null) { tool.name = ctx.tool.name; } Map process = ensure(ctx, 'process'); if (process.command_line == null && ctx.tool.command != null) { process.command_line = ctx.tool.command; } if (ctx.tool.path != null) { process.executable = ctx.tool.path; } } if (ctx.command instanceof Map) { Map process = ensure(ctx, 'process'); if (ctx.command.command != null) { process.command_line = ctx.command.command; } if (ctx.command.exit_code != null) { process.exit_code = ctx.command.exit_code; } if (ctx.command.duration_ms != null) { event.duration = ((long)ctx.command.duration_ms) * 1000000L; } } if (ctx.file instanceof Map) { Map file = ensure(ctx, 'file'); Map beaconFile = ensure(beacon, 'file'); if (ctx.file.path != null) { String p = ctx.file.path.toString(); file.path = p; int slash = p.lastIndexOf('/'); String name = slash >= 0 ? p.substring(slash + 1) : p; if (slash > 0) { file.directory = p.substring(0, slash); } file.name = name; int dot = name.lastIndexOf('.'); if (dot > 0 && dot < name.length() - 1) { file.extension = name.substring(dot + 1); } } if (ctx.file.diff_hash != null) { ensure(file, 'hash').sha256 = ctx.file.diff_hash; } if (ctx.file.diff_bytes != null) { file.size = ctx.file.diff_bytes; } if (ctx.file.operation != null) { beaconFile.operation = ctx.file.operation; } if (ctx.file.language != null) { beaconFile.language = ctx.file.language; } } if (ctx.mcp instanceof Map) { Map mcp = ensure(beacon, 'mcp'); if (ctx.mcp.server != null) { mcp.server = ctx.mcp.server; } if (ctx.mcp.tool != null) { mcp.tool = ctx.mcp.tool; } } if (ctx.approval instanceof Map) { Map approval = ensure(beacon, 'approval'); if (ctx.approval.required != null) { approval.required = ctx.approval.required; } if (ctx.approval.decision != null) { String decision = ctx.approval.decision.toString(); approval.decision = decision; if (decision == 'allow' || decision == 'approved') { event.outcome = 'success'; } else if (decision == 'deny' || decision == 'denied' || decision == 'block' || decision == 'blocked') { event.outcome = 'failure'; } } if (ctx.approval.reason != null) { approval.reason = ctx.approval.reason; } } if (ctx.policy instanceof Map) { Map policy = ensure(beacon, 'policy'); if (ctx.policy.id != null) { policy.id = ctx.policy.id; } if (ctx.policy.name != null) { policy.name = ctx.policy.name; } if (ctx.policy.decision != null) { policy.decision = ctx.policy.decision; } if (ctx.policy.enforcement != null) { policy.enforcement = ctx.policy.enforcement; } if (ctx.policy.reason != null) { policy.reason = ctx.policy.reason; } } if (ctx.prompt instanceof Map) { if (ctx.prompt.id != null) { ensure(beacon, 'prompt').id = ctx.prompt.id; } if (ctx.prompt.text != null) { ensure(beacon, 'prompt').text = ctx.prompt.text; } } if (ctx.content instanceof Map) { Map content = ensure(beacon, 'content'); if (ctx.content.retention != null) { content.retention = ctx.content.retention; } if (ctx.content.included != null) { content.included = ctx.content.included; } if (ctx.content.redacted != null) { content.redacted = ctx.content.redacted; } if (ctx.content.truncated != null) { content.truncated = ctx.content.truncated; } } if (ctx.system_context instanceof Map) { beacon.system_context = ctx.system_context; } if (ctx.destination instanceof Map) { Map destination = ensure(beacon, 'destination'); if (ctx.destination.type != null) { destination.type = ctx.destination.type; } if (ctx.destination.mode != null) { destination.mode = ctx.destination.mode; } if (ctx.destination.status != null) { destination.status = ctx.destination.status; } } if (ctx.health instanceof Map) { Map health = ensure(beacon, 'health'); if (ctx.health.component != null) { health.component = ctx.health.component; } if (ctx.health.status != null) { health.status = ctx.health.status; } if (ctx.health.reason != null) { health.reason = ctx.health.reason; } } if (ctx.model != null) { ensure(ensure(ctx, 'gen_ai'), 'request').model = ctx.model; } Map nativeVcs = ctx.vcs instanceof Map ? ctx.vcs : null; ctx.remove('vcs'); String vcsUrl = null; String vcsHeadName = null; String vcsRevision = null; if (nativeVcs != null) { beacon.vcs = nativeVcs; if (nativeVcs.repository instanceof Map && nativeVcs.repository.url instanceof Map && nativeVcs.repository.url.full != null) { vcsUrl = nativeVcs.repository.url.full.toString(); } if (nativeVcs.ref instanceof Map && nativeVcs.ref.head instanceof Map) { if (nativeVcs.ref.head.name != null) { vcsHeadName = nativeVcs.ref.head.name.toString(); } if (nativeVcs.ref.head.revision != null) { vcsRevision = nativeVcs.ref.head.revision.toString(); } } } if (vcsUrl == null && ctx.repository != null) { vcsUrl = ctx.repository.toString(); } if (vcsHeadName == null && ctx.branch != null) { vcsHeadName = ctx.branch.toString(); } if (vcsUrl != null) { ensure(ensure(ctx, 'vcs'), 'repository').url = vcsUrl; } if (vcsHeadName != null) { ensure(ensure(ensure(ctx, 'vcs'), 'ref'), 'head').name = vcsHeadName; } if (vcsRevision != null) { ensure(ensure(ensure(ctx, 'vcs'), 'ref'), 'head').revision = vcsRevision; } if (ctx.raw instanceof Map) { Map otel = ensure(beacon, 'otel'); if (ctx.raw.otel_signal != null) { otel.signal = ctx.raw.otel_signal; } if (ctx.raw.metric_name != null) { ensure(beacon, 'metric').name = ctx.raw.metric_name; if (event.action == null || event.action == 'metric.observed') { event.action = ctx.raw.metric_name; } } if (ctx.raw.metric_description != null) { otel.metric_description = ctx.raw.metric_description; } if (ctx.raw.metric_unit != null) { otel.metric_unit = ctx.raw.metric_unit; } if (ctx.raw.attribute_count != null) { otel.attribute_count = ctx.raw.attribute_count; } if (ctx.raw.attributes != null) { otel.attributes = ctx.raw.attributes; } }"
}
},
{
@@ -59,6 +59,7 @@
"approval",
"policy",
"prompt",
+ "system_context",
"content",
"destination",
"health",
diff --git a/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts
index cf8b56d8..fbac406e 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts
@@ -39,6 +39,7 @@ interface OmpContext {
model?: { id?: string; name?: string; provider?: string } | undefined
mode?: string
agent?: { kind?: string }
+ getSystemPrompt?: () => string[]
}
// The events Beacon subscribes to, and nothing else.
@@ -49,6 +50,10 @@ interface OmpContext {
// runtime log with rows no investigation asks for and would put Beacon in the path of every
// streaming token update.
//
+// `context` is the one exception: its supported handler context exposes the effective system
+// prompt. Beacon extracts only the `<skills>` index and discards the conversation messages and
+// every other system-prompt section.
+//
// `tool_call` and `tool_result` are the pair carrying tool activity: the first names the tool and
// its arguments before it runs, the second carries the outcome. `user_bash` and `user_python` are
// code the operator ran with Oh My Pi's `!` and `$` prefixes, which no tool event covers.
@@ -70,6 +75,7 @@ const subscribedEvents = [
"session_start",
"session_shutdown",
"input",
+ "context",
"tool_call",
"tool_result",
"tool_approval_requested",
@@ -79,6 +85,42 @@ const subscribedEvents = [
"message_end",
] as const
+// Oh My Pi renders its skill index under this heading in the runtime section of the system prompt,
+// after a line of instructions. The match is anchored to the heading because the prompt also
+// carries operator text -- project context files, appended prompts -- and a `<skills>` block quoted
+// there is not the index the runtime gave the model.
+const skillsSection = /^# Skills & Rules\n(?:[^\n]*\n){0,3}?<skills>\s*([\s\S]*?)\s*<\/skills>/m
+
+// How many sessions' last-sent listings are remembered, least recently sent evicted first.
+const maxRememberedSkillSessions = 64
+
+function skillListing(ctx: OmpContext | undefined): string {
+ try {
+ const prompt = ctx?.getSystemPrompt?.()
+ if (!Array.isArray(prompt)) return ""
+ for (const block of prompt) {
+ if (typeof block !== "string") continue
+ const match = skillsSection.exec(block)
+ const listing = match?.[1]?.trim()
+ if (listing) return listing
+ }
+ } catch (err) {
+ debugLog("system prompt could not be read", err)
+ }
+ return ""
+}
+
+function rememberSkillListing(seen: Map<string, string>, key: string, listing: string) {
+ // Re-inserted rather than updated so the map's insertion order is send order, and eviction
+ // drops the session that has gone longest without a change.
+ seen.delete(key)
+ if (seen.size >= maxRememberedSkillSessions) {
+ const oldest = seen.keys().next().value
+ if (oldest !== undefined) seen.delete(oldest)
+ }
+ seen.set(key, listing)
+}
+
// How many in-flight tool calls the approval enrichment below will remember at once.
//
// Bounded because the map is cleared by `tool_result`, and a call that never produces one -- an
@@ -100,6 +142,7 @@ const maxPendingToolCalls = 64
// process, so remembering it until `tool_result` is exact: the join is the runtime's own call id,
// not a timestamp or a guess.
const pendingToolCalls = new Map<string, unknown>()
+const rememberedSkillListings = new Map<string, string>()
function rememberToolCall(event: OmpEvent) {
const id = typeof event.toolCallId === "string" ? event.toolCallId : ""
@@ -198,15 +241,17 @@ function safeClone(value: unknown, depth = 0, seen = new WeakSet<object>()): unk
}
}
-async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
+// sendToBeacon resolves true only when the hook binary read the event and exited cleanly. Every
+// failure is still swallowed -- telemetry never interrupts the run -- but a caller that sends
+// something once rather than per event needs to know whether to try again.
+async function sendToBeacon(payload: Record<string, unknown>): Promise<boolean> {
// Flattened before anything else sees it, so every consumer of this function is handed plain
// data rather than a live event with cycles and functions still attached.
const safe = safeClone(payload)
const testSender = (globalThis as Record<symbol, unknown>)[Symbol.for("beacon.omp.testSender")]
if (typeof testSender === "function") {
- await (testSender as (value: unknown) => unknown)(safe)
- return
+ return (await (testSender as (value: unknown) => unknown)(safe)) !== false
}
let body: string
@@ -214,21 +259,21 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
body = JSON.stringify(safe)
} catch (err) {
debugLog("payload could not be serialized", err)
- return
+ return false
}
- if (!body) return
+ if (!body) return false
try {
// Imported lazily so a host without node:child_process fails to send rather than failing to
// load: a throwing import at module scope would surface to the user as a broken extension, and
// Oh My Pi reports an extension that throws at load time rather than continuing without it.
const { spawn } = await import("node:child_process")
- await new Promise<void>((resolve) => {
+ return await new Promise<boolean>((resolve) => {
let settled = false
- const finish = () => {
+ const finish = (ok: boolean) => {
if (settled) return
settled = true
- resolve()
+ resolve(ok)
}
let child
try {
@@ -238,7 +283,7 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
})
} catch (err) {
debugLog("hook binary could not be spawned", err)
- finish()
+ finish(false)
return
}
const timer = setTimeout(() => {
@@ -248,19 +293,19 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
// Already gone.
}
debugLog("hook binary timed out", { type: payload?.type })
- finish()
+ finish(false)
}, sendTimeoutMs)
// An unreferenced timer cannot keep Oh My Pi alive at exit waiting on Beacon telemetry.
if (typeof timer.unref === "function") timer.unref()
- const done = () => {
+ const done = (ok: boolean) => {
clearTimeout(timer)
- finish()
+ finish(ok)
}
child.on("error", (err) => {
debugLog("hook binary failed", err)
- done()
+ done(false)
})
- child.on("close", done)
+ child.on("close", (code: number | null) => done(code === 0))
// Without this, a hook binary that exits before reading stdin turns an EPIPE into an
// unhandled error event in the host process. Beacon telemetry must never do that.
child.stdin?.on("error", () => {})
@@ -273,6 +318,7 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
} catch (err) {
debugLog("send failed", err)
// Beacon telemetry must never interrupt Oh My Pi execution.
+ return false
}
}
@@ -343,10 +389,24 @@ export function createBeaconExtension() {
const forward = async (event: OmpEvent, ctx: OmpContext) => {
try {
+ const fields = identity(ctx)
+ if (event.type === "context") {
+ // Only the listing leaves the extension, and only when it changed: `context` fires before
+ // every model call and carries the whole conversation. It is remembered after a send that
+ // landed, so a failed send is retried on the next call instead of losing the listing.
+ const listing = skillListing(ctx)
+ // A session without an id is remembered under "", so it is still sent once rather than
+ // before every model call.
+ const key = typeof fields.sessionId === "string" ? fields.sessionId : ""
+ if (!listing || rememberedSkillListings.get(key) === listing) return
+ if (await sendToBeacon({ ...fields, type: "context", skillListing: listing })) {
+ rememberSkillListing(rememberedSkillListings, key, listing)
+ }
+ return
+ }
// The event is spread last so a field it carries itself wins over the context's. `user_bash`
// and `user_python` both carry their own cwd, and the approval events carry their own
// sessionId -- in each case the event's is the one that describes this action.
- const fields = identity(ctx)
const envelope: Record<string, unknown> = { ...fields, ...event }
const sessionId = typeof envelope.sessionId === "string" ? envelope.sessionId : ""
@@ -373,6 +433,7 @@ export function createBeaconExtension() {
// visible in one place.
pendingToolCalls.clear()
inputSessionId = null
+ rememberedSkillListings.delete(sessionId)
} else if (event.type === "tool_approval_requested" || event.type === "tool_approval_resolved") {
// Attached under `input`, the same key `tool_call` uses, so the mapper reads one shape for
// both and an approval resolves to the same command or file path its tool call did.
diff --git a/cli/beacon/internal/endpoint/inventory/inventory_test.go b/cli/beacon/internal/endpoint/inventory/inventory_test.go
index 718e3d05..023b3bd0 100644
--- a/cli/beacon/internal/endpoint/inventory/inventory_test.go
+++ b/cli/beacon/internal/endpoint/inventory/inventory_test.go
@@ -7,6 +7,8 @@ import (
"strings"
"testing"
"time"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
func TestScanCurrentUserMCPInventory(t *testing.T) {
@@ -638,6 +640,10 @@ func TestScanCurrentUserAndProjectSkillInventory(t *testing.T) {
if deploy.SourceScope != ScopeUser || !deploy.Exists || !deploy.Readable || deploy.ParserStatus != StatusOK {
t.Fatalf("Claude skill status = %#v", deploy)
}
+ // The same hash a session.context skill listing names the skill under, so the two join.
+ if deploy.SkillNameHash != asymptoteobserve.SkillNameHash("deploy") || deploy.SkillNameHash != hashString("deploy") {
+ t.Fatalf("skill_name_hash = %q, want the skill-listing hash of its name", deploy.SkillNameHash)
+ }
if deploy.FileSHA256 == "" || deploy.SkillNameHash == "" || deploy.RootPathHash == "" || deploy.ManifestPathHash == "" {
t.Fatalf("Claude skill missing hashes: %#v", deploy)
}
diff --git a/cli/beacon/internal/endpoint/inventory/skills.go b/cli/beacon/internal/endpoint/inventory/skills.go
index 2449cb8f..d3568d9c 100644
--- a/cli/beacon/internal/endpoint/inventory/skills.go
+++ b/cli/beacon/internal/endpoint/inventory/skills.go
@@ -5,6 +5,8 @@ import (
"path/filepath"
"sort"
"time"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
const maxSkillsPerRoot = 200
@@ -146,7 +148,7 @@ func inspectSkillManifest(root skillRoot, name, manifestPath, redaction string,
skill := Skill{
Runtime: root.runtime,
SkillName: valueForName(name, redaction),
- SkillNameHash: hashString(name),
+ SkillNameHash: asymptoteobserve.SkillNameHash(name),
RootPath: valueForPath(root.path, redaction),
RootPathHash: hashString(root.path),
SourceScope: root.scope,
diff --git a/cli/beacon/internal/endpoint/schema/event.go b/cli/beacon/internal/endpoint/schema/event.go
index 8c433b2a..6a980cac 100644
--- a/cli/beacon/internal/endpoint/schema/event.go
+++ b/cli/beacon/internal/endpoint/schema/event.go
@@ -99,6 +99,8 @@ type VCSRepositoryURLInfo = asymptoteobserve.VCSRepositoryURLInfo
type VCSAttributionInfo = asymptoteobserve.VCSAttributionInfo
type PolicyInfo = asymptoteobserve.PolicyInfo
type PromptInfo = asymptoteobserve.PromptInfo
+type SystemContextInfo = asymptoteobserve.SystemContextInfo
+type SkillRefInfo = asymptoteobserve.SkillRefInfo
type ContentInfo = asymptoteobserve.ContentInfo
type DestinationInfo = asymptoteobserve.DestinationInfo
type HealthInfo = asymptoteobserve.HealthInfo
@@ -135,6 +137,10 @@ const (
ContentRetentionMetadata = asymptoteobserve.ContentRetentionMetadata
ContentRetentionRedacted = asymptoteobserve.ContentRetentionRedacted
ContentRetentionFull = asymptoteobserve.ContentRetentionFull
+
+ SystemContextSkillListing = asymptoteobserve.SystemContextSkillListing
+ SystemContextSourceSystemPrompt = asymptoteobserve.SystemContextSourceSystemPrompt
+ SystemContextSourceTranscript = asymptoteobserve.SystemContextSourceTranscript
)
var NewEvent = asymptoteobserve.NewEvent
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/content_test.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/content_test.go
index dd66f414..26ad3bd2 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/content_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/content_test.go
@@ -1,12 +1,39 @@
package beaconevent
import (
+ "reflect"
"testing"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
"go.opentelemetry.io/collector/pdata/plog"
)
+// gen_ai.system_instructions is a list of parts on every event the collector writes, whatever shape
+// the producer sent: a typed index maps the field from the first event it sees and rejects the rest.
+func TestSystemInstructionsAreWrittenAsParts(t *testing.T) {
+ for name, tc := range map[string]struct {
+ value interface{}
+ want []string
+ }{
+ "plain string": {"Be concise.", []string{"Be concise."}},
+ "JSON parts string": {`[{"type":"text","content":"From JSON."}]`, []string{"From JSON."}},
+ "single part object": {map[string]interface{}{"type": "text", "content": "One part."}, []string{"One part."}},
+ } {
+ t.Run(name, func(t *testing.T) {
+ genAI := GenAIFromAttrs(map[string]interface{}{"gen_ai.system_instructions": tc.value})
+ if genAI == nil {
+ t.Fatal("no gen_ai block")
+ }
+ if _, ok := genAI.SystemInstructions.([]interface{}); !ok {
+ t.Fatalf("system_instructions = %#v, want a parts list", genAI.SystemInstructions)
+ }
+ if got := asymptoteobserve.GenAIText(genAI.SystemInstructions, "", asymptoteobserve.GenAIPartTypeText); !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("text = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
+
// firstPartContent reads the text out of a gen_ai messages value in the semconv
// shape, so the assertions below read the field the way a consumer would.
func firstPartContent(t *testing.T, messages interface{}) string {
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
index 109abee3..8fe8dfd7 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
@@ -1267,7 +1267,7 @@ func GenAIFromAttrs(attrs map[string]interface{}) *GenAIInfo {
genai.Retrieval = &GenAIRetrievalInfo{QueryText: query}
}
if instructions, ok := AnyAttr(attrs, "gen_ai.system_instructions"); ok {
- genai.SystemInstructions = instructions
+ genai.SystemInstructions = SystemInstructionParts(instructions)
}
if tokenType := FirstString(attrs, "gen_ai.token.type"); tokenType != "" {
genai.Token = &GenAITokenInfo{Type: tokenType}
@@ -1926,30 +1926,30 @@ func FirstMessageText(genai *GenAIInfo) string {
return firstTextFromAny(genai.Input.Messages)
}
+// firstTextFromAny is the first meaningful text part in a messages, parts or prompt value, read the
+// way every Beacon reader reads one.
func firstTextFromAny(value interface{}) string {
+ for _, text := range asymptoteobserve.GenAIText(value, "", asymptoteobserve.GenAIPartTypeText) {
+ if text = meaningfulText(text); text != "" {
+ return text
+ }
+ }
+ return ""
+}
+
+// SystemInstructionParts puts a gen_ai.system_instructions value in the semconv shape, a list of
+// parts. Producers that predate the convention send the instructions as one string, and AnyAttr
+// keeps a value that is not JSON as that string. Stored as is, the field would hold a string on
+// some events and a list on others, and a typed log index such as Elasticsearch's rejects every
+// event whose shape differs from the first it saw. A single part object becomes a one-part list.
+func SystemInstructionParts(value interface{}) interface{} {
switch typed := value.(type) {
case string:
- return meaningfulText(typed)
- case []interface{}:
- for _, item := range typed {
- if text := firstTextFromAny(item); text != "" {
- return text
- }
- }
+ return asymptoteobserve.SystemInstructionParts(typed)
case map[string]interface{}:
- if content, ok := typed["content"]; ok {
- if text := firstTextFromAny(content); text != "" {
- return text
- }
- }
- if parts, ok := typed["parts"]; ok {
- return firstTextFromAny(parts)
- }
- if messages, ok := typed["messages"]; ok {
- return firstTextFromAny(messages)
- }
+ return []interface{}{typed}
}
- return ""
+ return value
}
func meaningfulText(value string) string {
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index 37288129..f77eae49 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -9,6 +9,14 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
---
+## October 2026
+
+- **Model-visible skill listings** · Claude transcript sync and the Oh My Pi extension capture only the skill index the model was shown, as a `session.context` event rather than an operator prompt. `gen_ai.system_instructions` holds one OpenTelemetry GenAI `text` part per skill, so each skill's description is limited on its own and none is cut off by the ones before it. A new `system_context` block says the event is a skill index and names each skill with the `skill_name_hash` the configuration inventory uses; it is metadata, so metadata-only forwarding keeps it while removing the text. Invoked skill bodies and unrelated system instructions remain outside this signal.
+- **New rule field `e.gen_ai.system_instructions_text`** · Threat rules can match the text of `gen_ai.system_instructions`, whether Beacon recorded it or an OTLP producer reported it. Like `e.gen_ai.tool.call.result_text`, it is derived by the engine from text parts only, redacted, capped at 4 KB per part, and empty when content was not retained. `e.system_context.kind` and `e.system_context.skills` select a skill index without matching its text.
+- **System instructions are always a list of parts** · The collector now writes an OTLP `gen_ai.system_instructions` that arrives as a plain string or a single part as a parts list, so the field has one shape on every event and a typed index such as the Elastic pack's maps it consistently.
+
+---
+
## September 2026
### v1.3.22 · September 23, 2026
diff --git a/docs/cli/rules-fields.mdx b/docs/cli/rules-fields.mdx
index cfc7001e..e0c9a355 100644
--- a/docs/cli/rules-fields.mdx
+++ b/docs/cli/rules-fields.mdx
@@ -25,6 +25,8 @@ Threat-rule expressions bind each Beacon event as `e`. Field paths mirror the ev
| `e.prompt.text` | Match prompt text when retained |
| `e.prompt.id` | Group the events of one turn: the prompt and everything the agent did for it |
| `e.gen_ai.tool.call.result_text` | Match text a file read, web fetch, or MCP tool returned, when retained (derived by the engine; empty for other actions) |
+| `e.gen_ai.system_instructions_text` | Match runtime-provided system instructions, such as the skill index on `session.context`, when retained (derived by the engine from text parts) |
+| `e.system_context.kind` | Tell a skill index (`skill_listing`) from other `session.context` events |
| `e.gen_ai.usage.input_tokens` | Match normalized token usage fields |
## Examples
diff --git a/docs/detections/yaml-schema.mdx b/docs/detections/yaml-schema.mdx
index 798398ed..87ddf10d 100644
--- a/docs/detections/yaml-schema.mdx
+++ b/docs/detections/yaml-schema.mdx
@@ -145,6 +145,8 @@ CEL expressions use `e.<field>` paths that match the Beacon event schema. Common
| `e.file.path` | Match file paths. |
| `e.prompt.text` | Match prompt text when retained. |
| `e.gen_ai.tool.call.result_text` | Match what a file read, web fetch, or MCP tool returned, when retained. Derived by the engine, redacted, capped at 4 KB, and empty for other actions. |
+| `e.gen_ai.system_instructions_text` | Match runtime-provided system instructions, such as the skill index on `session.context`, when retained. Derived by the engine from `gen_ai.system_instructions` text parts, redacted, and capped at 4 KB per part, so every skill of an index is visible. Never operator prompt text. |
+| `e.system_context.kind`, `e.system_context.skills` | Select a skill index without matching its text: `e.system_context.kind == "skill_listing" && e.system_context.skills.exists(s, s.skill_name == "deploy")`. |
| `e.session.id` | Correlate activity within one session. |
| `e.mcp.server` | Match MCP-like server context. |
| `e.approval.decision` | Match approval or policy outcomes. |
diff --git a/docs/runtimes/claude-code.mdx b/docs/runtimes/claude-code.mdx
index 71b67e46..bb1d2af2 100644
--- a/docs/runtimes/claude-code.mdx
+++ b/docs/runtimes/claude-code.mdx
@@ -81,6 +81,7 @@ Beacon detects Claude Code through the `claude` executable and Claude settings p
| Hook lifecycle and tool telemetry | Optional through `beacon endpoint hooks install --harness claude` |
| Historical session sync | Supported through `beacon endpoint claude sync`; marked as poll collection |
| Claude Code cloud agent sessions | Supported through cloud sandbox hooks with GCS upload |
+| Model-visible skill listings | Supported through Claude transcript `attachment.type=skill_listing`, one part per skill in `gen_ai.system_instructions`, with the skills named in `system_context`; invoked skill file bodies remain excluded |
| Local JSONL and dashboard | Supported |
| MDM deployment | Supported through the system-mode package or MDM-managed settings |
@@ -131,6 +132,17 @@ Detailed tool inputs can contain sensitive command arguments, paths, URLs, searc
Claude Code writes one streamed API response as several transcript lines, one per content block (thinking, text, each tool call), and every line carries a copy of the response's `message.usage`. The sync emits one `token.usage` event per response, not per line: lines are grouped by `message.id` and `requestId`, and the event carries the snapshot with the largest output count, because lines written before the stream finished carry a placeholder output count. When a sweep lands between two lines of one response, the next sweep adds only what the later lines report beyond what was already counted, so the total is the same however the response is split across sweeps. A forked or resumed session that replays earlier responses into another file is counted once when both files are read in the same sweep.
+When Claude Code records an `attachment` whose type is `skill_listing`, sync emits
+`session.context` with one `text` part per skill in `gen_ai.system_instructions`,
+split on the skill names the attachment reports, and a `system_context` block
+(`kind: skill_listing`, `source: transcript`) naming each skill with the same
+`skill_name_hash` the configuration inventory records. Rules match the text as
+`e.gen_ai.system_instructions_text`. This is the model-visible index (skill
+names and descriptions), not an operator prompt, so it is never written to
+`prompt` or `gen_ai.input`. Other attachments and invoked skill bodies are not
+captured by this mapping. Metadata-only managed forwarding removes the listing
+text before upload and keeps `system_context`.
+
Run `beacon endpoint claude sync --watch`, or schedule one-shot sweeps, if you want the poll path to stay current. The live OTLP and hook paths remain the real-time integrations; the poll path is for backfill and catch-up.
### Large backfills and log rotation
diff --git a/docs/runtimes/oh-my-pi.mdx b/docs/runtimes/oh-my-pi.mdx
index 603d5587..6866d230 100644
--- a/docs/runtimes/oh-my-pi.mdx
+++ b/docs/runtimes/oh-my-pi.mdx
@@ -106,6 +106,7 @@ Unlike Pi, a project-level install needs no trust prompt: Oh My Pi loads `.omp`
| File activity | Supported. `file.read`, `file.created`, and `file.modified` under the absolute path Oh My Pi resolved, with the edit's diff. An edit that changes several files records one `file.modified` per file. Only filesystem targets count: see [file paths](#file-paths) and [tool devices and URIs](#tool-devices-and-uris) |
| MCP activity | Supported. `tool.invoked` then `mcp.tool_invoked`, whether the model called the tool directly or through its `xd://` device. `mcp.server` and `mcp.tool` are the server's configured name and the tool's own name, read from the result Oh My Pi reports. A `read mcp://<uri>` is recorded as an MCP `resources/read` with `mcp.resource.uri` |
| Agent reasoning | Supported. `agent.reasoning` from an assistant message's thinking parts, in the OTel GenAI reasoning-part shape |
+| Model-visible skill listings | Supported through the `context` event's `ctx.getSystemPrompt()`; Beacon retains only the `<skills>` index under the system prompt's `# Skills & Rules` heading, one part per skill in `gen_ai.system_instructions` with the skills named in `system_context`, not the full system prompt or skill bodies |
| Token usage and cost | Supported. `token.usage` normalized into `gen_ai.usage`, including cache and reasoning tokens and runtime-reported cost |
| Local JSONL and dashboard | Supported |
| MDM deployment | Supported for the endpoint agent. The extension installs separately in the logged-in user's context |
@@ -154,12 +155,21 @@ Detections in `rules/approval-abuse/` fire on Oh My Pi activity, unlike on Pi. A
Oh My Pi content is handled like every other runtime. Prompts, tool arguments, commands, Python source, paths, diffs, and reasoning text are retained in local or customer-controlled logs, with local secret redaction and per-string limits applied before writing, and a hash and byte count on each content-bearing event. Events over the 64 KiB limit drop raw and retained content, preserve stable metadata, and set `field_truncated`.
+The `context` event is the exception to the extension's otherwise narrow event
+subscriptions: Oh My Pi's supported `ctx.getSystemPrompt()` supplies the effective
+system prompt, from which Beacon extracts only the `<skills>` index under the
+`# Skills & Rules` heading. That index is recorded as `session.context` with one
+`text` part per skill in `gen_ai.system_instructions` and a `system_context` block
+(`kind: skill_listing`, `source: system_prompt`) naming each skill, never as an
+operator prompt. Other system-prompt instructions and the conversation messages
+delivered with `context` are discarded by the extension.
+
The extension itself sends nothing over the network. It spawns the local `beacon-hooks` binary with a fixed argv and writes the event to its stdin. A send that has not completed in two seconds is abandoned so Beacon cannot stall Oh My Pi's agent loop, and a failure to spawn costs the event rather than the run. Every handler returns nothing, so Oh My Pi never reads a directive back from Beacon — the extension cannot block a tool call, revise its arguments, or answer a question that was put to the operator.
## Known gaps
- **No enforcement.** As with every other runtime, enforcement stays behind the optional, off-by-default policy provider seam (`BEACON_POLICY_PROVIDER`), which is not wired to Oh My Pi. Its blocking `tool_call` handler and its approval events make it a viable target for that seam later.
-- **Streaming events are not collected.** The extension subscribes to ten of Oh My Pi's event types and ignores the rest, which are provider-request internals, streaming message updates, compaction and retry signals, and TUI plumbing. Assistant output is recorded once when a message finalizes rather than token by token, so Beacon stays out of the streaming path.
+- **Streaming events are not collected.** The extension subscribes to eleven of Oh My Pi's event types and ignores the rest, which are provider-request internals, streaming message updates, compaction and retry signals, and TUI plumbing. The `context` event is narrowly used only to extract the skill index from the supported system-prompt accessor; its conversation messages are discarded. Assistant output is recorded once when a message finalizes rather than token by token, so Beacon stays out of the streaming path.
- **MCP notifications are not collected.** `mcp_notification` fires for every JSON-RPC notification a connected server sends, most of them routine tools and resources list refreshes. It describes MCP transport plumbing rather than an action the agent took. MCP tool calls the agent makes *are* collected.
- **One profile at a time.** Beacon installs into the active profile, named by `OMP_PROFILE` or `PI_PROFILE`, or into the default profile. It does not enumerate `~/.omp/profiles/`, so instrumenting another profile means running the install with that profile's variable set.
- **Print and ACP prompts are recorded as delivered, not as typed.** Oh My Pi expands slash commands, prompt templates, and model mentions before it delivers a user message, so on print and ACP `prompt.text` holds the expanded text, which can include a template's body, and carries no input source. Runtime-generated turns, messages one agent hands another, and subagent tasks are not recorded as prompts. A message an extension sends as the user is, because Oh My Pi attributes it to the user. `/skill:` invocations are delivered as a different message type and are not recorded on these surfaces.
diff --git a/docs/telemetry-schema/fields.mdx b/docs/telemetry-schema/fields.mdx
index 10c19a56..1ffa5f29 100644
--- a/docs/telemetry-schema/fields.mdx
+++ b/docs/telemetry-schema/fields.mdx
@@ -30,6 +30,7 @@ Beacon models each event as an action plus a set of typed entities. The [`event`
| `handoff` | On a `session.handoff` event, the session this one continues from a [handoff brief](/cli/handoff#linking-a-new-session-to-the-one-it-continues) | `source_harness`, `source_session_id` |
| `policy` | Policy metadata when available from a runtime or integration | `id`, `name`, `decision`, `enforcement`, `reason` |
| `prompt` | Prompt text when the source emits it and content handling allows it | `text` |
+| `system_context` | On a `session.context` event, what the runtime gave the model outside the conversation. Metadata, so it is kept when content is not; the text is in `gen_ai.system_instructions` | `kind` (`skill_listing`), `source` (`system_prompt`, `transcript`), `skills[].skill_name`, `skills[].skill_name_hash` |
| `content` | Content handling state for the event | `retention`, `included`, `redacted`, `truncated` |
| `gen_ai` | OpenTelemetry GenAI semantic convention context | `agent`, `conversation`, `input`, `output`, `provider`, `request`, `response`, `retrieval`, `system_instructions`, `tool`, `usage`, `workflow` |
| `destination` | Output or validation destination state | `type`, `mode`, `status` |
@@ -53,6 +54,7 @@ When a runtime emits OpenTelemetry GenAI semantic convention attributes, Beacon
| `gen_ai.response` | Response metadata such as `id`, `model`, `finish_reasons`, and `time_to_first_chunk` |
| `gen_ai.usage` | Token counters such as `input_tokens`, `output_tokens`, `cache_creation.input_tokens` (with its optional one-hour subset `cache_creation.ephemeral_1h_input_tokens`), `cache_read.input_tokens`, and `reasoning.output_tokens`, plus runtime-reported `cost_usd` |
| `gen_ai.input.messages`, `gen_ai.output.messages` | Message arrays or objects emitted by the runtime |
+| `gen_ai.system_instructions` | Instructions given to the model outside the conversation, as a list of parts (`type`, `content`). A skill index on `session.context` has one `text` part per skill, so each skill is limited on its own |
| `gen_ai.tool` | Tool metadata such as `name`, `type`, `description`, `definitions`, and `call.id`, `call.arguments`, or `call.result` |
| `gen_ai.retrieval` | Retrieval context such as `query_text` and `documents` |
| `gen_ai.evaluation` | Evaluation metadata such as `name`, `explanation`, `score.label`, and `score.value` |
diff --git a/docs/telemetry-schema/normalization.mdx b/docs/telemetry-schema/normalization.mdx
index 88441576..96b029ec 100644
--- a/docs/telemetry-schema/normalization.mdx
+++ b/docs/telemetry-schema/normalization.mdx
@@ -150,7 +150,7 @@ Beacon preserves OpenTelemetry GenAI semantic convention fields under the nested
| `gen_ai.operation.name` | `gen_ai.operation.name` |
| `gen_ai.prompt.name` | `gen_ai.prompt.name` |
| `gen_ai.provider.name`, `gen_ai.system` | `gen_ai.provider.name` |
-| `gen_ai.system_instructions` | `gen_ai.system_instructions` |
+| `gen_ai.system_instructions` | `gen_ai.system_instructions`, always a list of parts: a plain string becomes one `text` part and a single part object a one-part list |
| `gen_ai.retrieval.query.text`, `gen_ai.retrieval.documents` | `gen_ai.retrieval.*` |
| `gen_ai.token.type` | `gen_ai.token.type` |
| `gen_ai.workflow.name` | `gen_ai.workflow.name` |
diff --git a/pkg/asymptoteobserve/content.go b/pkg/asymptoteobserve/content.go
index 70c57521..5ac40147 100644
--- a/pkg/asymptoteobserve/content.go
+++ b/pkg/asymptoteobserve/content.go
@@ -3,6 +3,8 @@ package asymptoteobserve
import (
"crypto/sha256"
"encoding/hex"
+ "encoding/json"
+ "strings"
)
// Roles and part types Beacon writes into gen_ai.input.messages and
@@ -12,6 +14,7 @@ import (
const (
RoleUser = "user"
RoleAssistant = "assistant"
+ RoleSystem = "system"
GenAIPartTypeText = "text"
GenAIPartTypeReasoning = "reasoning"
@@ -36,6 +39,97 @@ func GenAIMessages(role, partType, text string) []interface{} {
}
}
+// SystemInstructionParts is text in the gen_ai.system_instructions shape. The
+// semconv defines that attribute as a bare list of message parts -- no role and
+// no message wrapper, unlike gen_ai.input.messages -- so a Beacon-written value
+// and one an OTLP producer sends are read the same way.
+func SystemInstructionParts(text string) []interface{} {
+ return []interface{}{
+ map[string]interface{}{"type": GenAIPartTypeText, "content": text},
+ }
+}
+
+// GenAIText returns the content of every part of partType in a GenAI messages or
+// parts value, in order, each trimmed, blanks dropped. It is the one reader for
+// gen_ai.input.messages, gen_ai.output.messages and gen_ai.system_instructions,
+// and it accepts every shape the capture paths write or receive:
+//
+// - the semconv message list, [{"role", "parts": [{"type", "content"}]}];
+// - a bare parts list, the gen_ai.system_instructions shape;
+// - a message whose body is "content" rather than "parts", as a string or a
+// block list, and a part whose text is under "text" rather than "content",
+// the shapes several session mappers and providers write;
+// - a {"messages": [...]} wrapper;
+// - a bare string, as the whole value, a message body or a list element,
+// which is text and never reasoning;
+// - a typed value from an in-process caller, normalized through JSON.
+//
+// A map with a role, a parts list or type "message" is a message, any other map
+// with a type is a part, and a map with neither is a message. role, when not
+// empty, keeps only messages from that role or naming none; a part outside any
+// message is kept.
+//
+// It does no redaction or truncation; callers apply the limit that governs
+// where the text goes.
+func GenAIText(value interface{}, role, partType string) []string {
+ var out []string
+ collectGenAIText(value, role, partType, &out)
+ return out
+}
+
+func collectGenAIText(v interface{}, role, partType string, out *[]string) {
+ switch typed := v.(type) {
+ case string:
+ if text := strings.TrimSpace(typed); text != "" && partType == GenAIPartTypeText {
+ *out = append(*out, text)
+ }
+ case []interface{}:
+ for _, item := range typed {
+ collectGenAIText(item, role, partType, out)
+ }
+ case map[string]interface{}:
+ if messages, ok := typed["messages"]; ok {
+ collectGenAIText(messages, role, partType, out)
+ return
+ }
+ kind, _ := typed["type"].(string)
+ _, hasRole := typed["role"]
+ body, hasParts := typed["parts"]
+ if kind != "" && kind != "message" && !hasRole && !hasParts {
+ if kind != partType {
+ return
+ }
+ text, _ := typed["content"].(string)
+ if strings.TrimSpace(text) == "" {
+ text, _ = typed["text"].(string)
+ }
+ if text = strings.TrimSpace(text); text != "" {
+ *out = append(*out, text)
+ }
+ return
+ }
+ if messageRole, _ := typed["role"].(string); role != "" && messageRole != "" && messageRole != role {
+ return
+ }
+ if !hasParts {
+ body = typed["content"]
+ }
+ collectGenAIText(body, role, partType, out)
+ case nil, bool, float64, float32, int, int64, int32, json.Number:
+ default:
+ // A typed value from an in-process caller rather than the generic shape a log
+ // line decodes to. Normalized through JSON once; the result is handled above.
+ data, err := json.Marshal(typed)
+ if err != nil {
+ return
+ }
+ var generic interface{}
+ if json.Unmarshal(data, &generic) == nil {
+ collectGenAIText(generic, role, partType, out)
+ }
+ }
+}
+
// TextOutputMessages is the assistant's own text -- what it said, as opposed to
// what it thought (ReasoningOutputMessages) -- in the gen_ai.output.messages shape.
func TextOutputMessages(text string) []interface{} {
@@ -104,7 +198,10 @@ func RetainedContent(text string, storeLimit int) *ContentInfo {
// integers: a float64 byte count would serialize the same but sit next to
// file.diff_bytes, an int, in the same fields map.
func RetainedContentFields(text string, storeLimit int) map[string]interface{} {
- info := RetainedContent(text, storeLimit)
+ return contentFields(RetainedContent(text, storeLimit))
+}
+
+func contentFields(info *ContentInfo) map[string]interface{} {
if info == nil {
return nil
}
diff --git a/pkg/asymptoteobserve/event.go b/pkg/asymptoteobserve/event.go
index dc56fe99..6236564e 100644
--- a/pkg/asymptoteobserve/event.go
+++ b/pkg/asymptoteobserve/event.go
@@ -235,6 +235,38 @@ type PromptInfo struct {
Text string `json:"text,omitempty"`
}
+// SystemContextInfo says what a session.context event recorded when the runtime gave the model
+// context of its own, outside the conversation: which kind it was, where Beacon read it, and for a
+// skill index, which skills it listed. The text itself is in gen_ai.system_instructions. This block
+// is metadata, so it stays on an event whose text is removed, as metadata-only forwarding does.
+type SystemContextInfo struct {
+ Kind string `json:"kind"`
+ Source string `json:"source,omitempty"`
+ Skills []SkillRefInfo `json:"skills,omitempty"`
+}
+
+// SkillRefInfo names one skill. The fields are spelled and SkillNameHash is computed the way the
+// configuration inventory records a skill it found on disk, so a skill the model was shown joins
+// to the SKILL.md it came from.
+type SkillRefInfo struct {
+ SkillName string `json:"skill_name,omitempty"`
+ SkillNameHash string `json:"skill_name_hash,omitempty"`
+}
+
+// SystemContextInfo.Kind values.
+const (
+ // SystemContextSkillListing is a skill index: each skill's name and description, which the
+ // runtime lists so the model can choose one. A skill's body is not part of it.
+ SystemContextSkillListing = "skill_listing"
+)
+
+// SystemContextInfo.Source values: where the runtime exposed the context to Beacon. With
+// harness.name they say exactly which reader produced an event.
+const (
+ SystemContextSourceSystemPrompt = "system_prompt"
+ SystemContextSourceTranscript = "transcript"
+)
+
// ContentInfo marks how an event's retained raw content was handled. Hash and
// Bytes describe the original content before redaction/truncation, so events
// keep a stable identifier and size even when the stored text was cut down.
@@ -508,10 +540,15 @@ type GenAIInfo struct {
Response *GenAIResponseInfo `json:"response,omitempty"`
Retrieval *GenAIRetrievalInfo `json:"retrieval,omitempty"`
SystemInstructions interface{} `json:"system_instructions,omitempty"`
- Token *GenAITokenInfo `json:"token,omitempty"`
- Tool *GenAIToolInfo `json:"tool,omitempty"`
- Usage *GenAIUsageInfo `json:"usage,omitempty"`
- Workflow *GenAIWorkflowInfo `json:"workflow,omitempty"`
+ // SystemInstructionsText is SystemInstructions as plain text, for the threat-rules
+ // engine only, for the same reason as GenAIToolCallInfo.ResultText: CEL cannot address
+ // the interface{} semconv value. It is never written or read; the engine fills it on its
+ // own copy of each event. See threatrules.SystemInstructionsText.
+ SystemInstructionsText string `json:"-" cel:"system_instructions_text"`
+ Token *GenAITokenInfo `json:"token,omitempty"`
+ Tool *GenAIToolInfo `json:"tool,omitempty"`
+ Usage *GenAIUsageInfo `json:"usage,omitempty"`
+ Workflow *GenAIWorkflowInfo `json:"workflow,omitempty"`
}
type DestinationInfo struct {
@@ -622,6 +659,7 @@ type Event struct {
Approval *ApprovalInfo `json:"approval,omitempty"`
Policy *PolicyInfo `json:"policy,omitempty"`
Prompt *PromptInfo `json:"prompt,omitempty"`
+ SystemContext *SystemContextInfo `json:"system_context,omitempty"`
Content *ContentInfo `json:"content,omitempty"`
Destination *DestinationInfo `json:"destination,omitempty"`
Health *HealthInfo `json:"health,omitempty"`
diff --git a/pkg/asymptoteobserve/privacy.go b/pkg/asymptoteobserve/privacy.go
index aba7942c..bfe0a0eb 100644
--- a/pkg/asymptoteobserve/privacy.go
+++ b/pkg/asymptoteobserve/privacy.go
@@ -143,6 +143,9 @@ func SanitizeEvent(event Event, maxBytes int) Event {
if event.MCP != nil {
event.MCP = sanitizeTyped(event.MCP, PrivacyOptions{RedactSecrets: true, StringLimit: DefaultRawStringLimit})
}
+ if event.SystemContext != nil {
+ event.SystemContext = sanitizeTyped(event.SystemContext, PrivacyOptions{RedactSecrets: true, StringLimit: DefaultRawStringLimit})
+ }
if event.GenAI != nil {
event.GenAI = sanitizeTyped(event.GenAI, PrivacyOptions{RedactSecrets: true, StringLimit: DefaultRawStringLimit})
}
diff --git a/pkg/asymptoteobserve/privacy_test.go b/pkg/asymptoteobserve/privacy_test.go
index abd5c23e..11116699 100644
--- a/pkg/asymptoteobserve/privacy_test.go
+++ b/pkg/asymptoteobserve/privacy_test.go
@@ -66,6 +66,7 @@ func TestSanitizeEventRedactsAndTruncates(t *testing.T) {
}
event.Policy = &PolicyInfo{Reason: "api_key=policy-secret"}
event.Prompt = &PromptInfo{Text: "token=prompt-secret"}
+ event.GenAI = &GenAIInfo{SystemInstructions: SystemInstructionParts("token=system-instructions-secret")}
event.Raw = map[string]interface{}{"nested": map[string]interface{}{"token": "token=raw-secret"}}
sanitized := SanitizeEvent(event, 64*1024)
@@ -74,7 +75,7 @@ func TestSanitizeEventRedactsAndTruncates(t *testing.T) {
t.Fatalf("marshal sanitized event: %v", err)
}
text := string(data)
- for _, secret := range []string{"message-secret", "command-secret", "policy-secret", "prompt-secret", "raw-secret"} {
+ for _, secret := range []string{"message-secret", "command-secret", "policy-secret", "prompt-secret", "system-instructions-secret", "raw-secret"} {
if strings.Contains(text, secret) {
t.Fatalf("secret %q was not redacted: %s", secret, text)
}
diff --git a/pkg/asymptoteobserve/skilllisting.go b/pkg/asymptoteobserve/skilllisting.go
new file mode 100644
index 00000000..ed72ca89
--- /dev/null
+++ b/pkg/asymptoteobserve/skilllisting.go
@@ -0,0 +1,188 @@
+package asymptoteobserve
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "strings"
+)
+
+// SkillListing is a skill index a runtime showed the model, split into one entry per skill.
+//
+// Every capture path builds its session.context event from one, so a skill index is recorded the
+// same way whichever runtime or collection method saw it: one gen_ai.system_instructions text part
+// per entry, and a system_context block naming each skill. Supporting another runtime means
+// finding where it exposes its index and handing the text to ParseSkillListing; nothing about the
+// event is runtime-specific.
+//
+// Each entry gets its own part because a writer limits every string it stores, not the event. One
+// part holding a 6 KB index of fifteen skills keeps the first four; split, every skill keeps its
+// description up to the limit.
+type SkillListing struct {
+ // Source is where the runtime exposed the index: a SystemContextSource value.
+ Source string
+ Entries []SkillListingEntry
+}
+
+// SkillListingEntry is one skill as the model was shown it. Name is empty for text the listing
+// carries outside any skill's entry, which is kept so nothing the model saw is dropped.
+type SkillListingEntry struct {
+ Name string
+ Text string
+}
+
+// ParseSkillListing splits a rendered skill index into entries. Both runtimes Beacon reads one from,
+// Claude Code's skill_listing attachment and Oh My Pi's <skills> block, render a skill as a line
+// "- <name>: <description>", and a description can run onto the lines after it.
+//
+// names, when the runtime reports them, are the skills the listing holds, and only a line naming one
+// of them starts an entry, so a description cannot forge another skill's entry with a line that
+// looks like one. A name can contain a colon (Claude Code's plugin skills are plugin:skill), so a
+// line is matched against the longest name it begins with. Without names, an entry starts at any
+// "- " line whose name, the text before ": ", has no spaces.
+func ParseSkillListing(source, text string, names []string) SkillListing {
+ listing := SkillListing{Source: source}
+ remaining := make(map[string]bool, len(names))
+ for _, name := range names {
+ if name != "" {
+ remaining[name] = true
+ }
+ }
+ var name string
+ var lines []string
+ flush := func() {
+ if entry := strings.TrimSpace(strings.Join(lines, "\n")); entry != "" {
+ listing.Entries = append(listing.Entries, SkillListingEntry{Name: name, Text: entry})
+ }
+ lines = nil
+ }
+ for _, line := range strings.Split(text, "\n") {
+ line = strings.TrimRight(line, "\r")
+ if next, ok := skillEntryName(line, len(names) > 0, remaining); ok {
+ flush()
+ name = next
+ delete(remaining, next)
+ }
+ lines = append(lines, line)
+ }
+ flush()
+ return listing
+}
+
+func skillEntryName(line string, named bool, remaining map[string]bool) (string, bool) {
+ rest, ok := strings.CutPrefix(line, "- ")
+ if !ok {
+ return "", false
+ }
+ if named {
+ var longest string
+ for name := range remaining {
+ if strings.HasPrefix(rest, name+":") && len(name) > len(longest) {
+ longest = name
+ }
+ }
+ return longest, longest != ""
+ }
+ name, _, found := strings.Cut(rest, ": ")
+ if !found {
+ name, found = strings.CutSuffix(rest, ":")
+ }
+ if !found || name == "" || strings.ContainsAny(name, " \t") {
+ return "", false
+ }
+ return name, true
+}
+
+// SkillNameHash is the hash a skill's name is recorded under, here and in the configuration
+// inventory, so a skill the model was shown and a SKILL.md found on disk join on it.
+func SkillNameHash(name string) string {
+ if name == "" {
+ return ""
+ }
+ sum := sha256.Sum256([]byte(name))
+ return "sha256:" + hex.EncodeToString(sum[:])
+}
+
+// Text is the listing as the model was shown it: the entries, one after another.
+func (l SkillListing) Text() string {
+ texts := make([]string, len(l.Entries))
+ for i, entry := range l.Entries {
+ texts[i] = entry.Text
+ }
+ return strings.Join(texts, "\n")
+}
+
+// SystemInstructions is the listing in the gen_ai.system_instructions shape: one text part per entry.
+func (l SkillListing) SystemInstructions() []interface{} {
+ parts := make([]interface{}, len(l.Entries))
+ for i, entry := range l.Entries {
+ parts[i] = map[string]interface{}{"type": GenAIPartTypeText, "content": entry.Text}
+ }
+ return parts
+}
+
+// SystemContext names the listing's skills.
+func (l SkillListing) SystemContext() *SystemContextInfo {
+ info := &SystemContextInfo{Kind: SystemContextSkillListing, Source: l.Source}
+ for _, entry := range l.Entries {
+ if entry.Name != "" {
+ info.Skills = append(info.Skills, SkillRefInfo{SkillName: entry.Name, SkillNameHash: SkillNameHash(entry.Name)})
+ }
+ }
+ return info
+}
+
+// Content is the listing's content marker. Hash and Bytes cover the whole listing. storeLimit is
+// the limit the writer applies to each string it stores, and Truncated says whether any one entry
+// is longer than that, because each entry is stored as its own string.
+func (l SkillListing) Content(storeLimit int) *ContentInfo {
+ info := RetainedContent(l.Text(), 0)
+ if info == nil {
+ return nil
+ }
+ for _, entry := range l.Entries {
+ if storeLimit > 0 && len(entry.Text) > storeLimit {
+ info.Truncated = true
+ }
+ }
+ return info
+}
+
+// Apply records the listing on ev, keeping what ev.GenAI already holds, such as a subagent's
+// agent block.
+func (l SkillListing) Apply(ev *Event, storeLimit int) {
+ if len(l.Entries) == 0 {
+ return
+ }
+ if ev.GenAI == nil {
+ ev.GenAI = &GenAIInfo{}
+ }
+ ev.GenAI.SystemInstructions = l.SystemInstructions()
+ ev.SystemContext = l.SystemContext()
+ ev.Content = l.Content(storeLimit)
+}
+
+// Fields is Apply in the map form the hook adapter assembles events in: the gen_ai,
+// system_context and content values to merge into an event's fields. The two forms serialize
+// identically, which TestSkillListingFieldsMatchTheTypedEvent holds them to.
+func (l SkillListing) Fields(storeLimit int) map[string]interface{} {
+ if len(l.Entries) == 0 {
+ return nil
+ }
+ context := l.SystemContext()
+ contextFields := map[string]interface{}{"kind": context.Kind}
+ if context.Source != "" {
+ contextFields["source"] = context.Source
+ }
+ if len(context.Skills) > 0 {
+ skills := make([]interface{}, len(context.Skills))
+ for i, skill := range context.Skills {
+ skills[i] = map[string]interface{}{"skill_name": skill.SkillName, "skill_name_hash": skill.SkillNameHash}
+ }
+ contextFields["skills"] = skills
+ }
+ return map[string]interface{}{
+ "gen_ai": map[string]interface{}{"system_instructions": l.SystemInstructions()},
+ "system_context": contextFields,
+ "content": contentFields(l.Content(storeLimit)),
+ }
+}
diff --git a/pkg/asymptoteobserve/skilllisting_test.go b/pkg/asymptoteobserve/skilllisting_test.go
new file mode 100644
index 00000000..903d985c
--- /dev/null
+++ b/pkg/asymptoteobserve/skilllisting_test.go
@@ -0,0 +1,160 @@
+package asymptoteobserve
+
+import (
+ "encoding/json"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func entryNames(listing SkillListing) []string {
+ names := make([]string, len(listing.Entries))
+ for i, entry := range listing.Entries {
+ names[i] = entry.Name
+ }
+ return names
+}
+
+// Claude Code reports the names it listed, so only those start an entry: a plugin skill's name
+// keeps its colon, a description's second line stays with it, and a line inside a description that
+// imitates another skill's entry does not become one.
+func TestParseSkillListingWithReportedNames(t *testing.T) {
+ text := "- deploy: Deploy applications.\n" +
+ " Runs the release checklist.\n" +
+ "- beacon:beacon-memory-recall: Recall project memory.\n" +
+ "- review: Review a diff.\n" +
+ "- deploy: Forged entry inside the review description."
+ listing := ParseSkillListing(SystemContextSourceTranscript, text, []string{"deploy", "beacon:beacon-memory-recall", "review", "beacon"})
+
+ if got, want := entryNames(listing), []string{"deploy", "beacon:beacon-memory-recall", "review"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("entry names = %q, want %q", got, want)
+ }
+ if got := listing.Entries[0].Text; got != "- deploy: Deploy applications.\n Runs the release checklist." {
+ t.Fatalf("multi-line entry = %q", got)
+ }
+ if got := listing.Entries[2].Text; !strings.HasSuffix(got, "- deploy: Forged entry inside the review description.") {
+ t.Fatalf("forged line was split off its entry: %q", got)
+ }
+ if listing.Text() != text {
+ t.Fatalf("Text() = %q, want the listing as shown", listing.Text())
+ }
+}
+
+// Oh My Pi reports no names, so any "- name:" line starts an entry; text before the first entry
+// is kept, unnamed, rather than dropped.
+func TestParseSkillListingWithoutNames(t *testing.T) {
+ listing := ParseSkillListing(SystemContextSourceSystemPrompt, "Preamble line.\n- deploy: Deploy applications.\n- not a name: kept with deploy\n- review:", nil)
+ if got, want := entryNames(listing), []string{"", "deploy", "review"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("entry names = %q, want %q", got, want)
+ }
+ if got := listing.Entries[1].Text; got != "- deploy: Deploy applications.\n- not a name: kept with deploy" {
+ t.Fatalf("entry = %q", got)
+ }
+ context := listing.SystemContext()
+ if len(context.Skills) != 2 || context.Skills[0].SkillName != "deploy" {
+ t.Fatalf("skills = %+v, want the two named entries only", context.Skills)
+ }
+}
+
+func TestSkillListingKeepsEachSkillAsItsOwnPart(t *testing.T) {
+ listing := ParseSkillListing(SystemContextSourceTranscript, "- a: one\n- b: "+strings.Repeat("x", 3000), []string{"a", "b"})
+ event := Event{GenAI: &GenAIInfo{Agent: &GenAIAgentInfo{ID: "sub-1"}}}
+ listing.Apply(&event, DefaultRawStringLimit)
+
+ if event.GenAI.Agent == nil || event.GenAI.Agent.ID != "sub-1" {
+ t.Fatal("Apply replaced the event's existing gen_ai block")
+ }
+ parts := GenAIText(event.GenAI.SystemInstructions, "", GenAIPartTypeText)
+ if len(parts) != 2 || parts[0] != "- a: one" {
+ t.Fatalf("parts = %q, want one per skill", parts)
+ }
+ if event.Content == nil || !event.Content.Truncated || event.Content.Bytes != len(listing.Text()) {
+ t.Fatalf("content = %+v, want the whole listing's size and truncated because one entry exceeds the limit", event.Content)
+ }
+ if got := ParseSkillListing("", "- a: one", nil).Content(DefaultRawStringLimit); got.Truncated {
+ t.Fatal("a listing whose entries all fit is marked truncated")
+ }
+
+ sanitized := SanitizeEvent(event, 64*1024)
+ stored := GenAIText(sanitized.GenAI.SystemInstructions, "", GenAIPartTypeText)
+ if len(stored) != 2 || stored[0] != "- a: one" || len(stored[1]) > DefaultRawStringLimit {
+ t.Fatalf("stored parts = %d, want the short skill intact and the long one capped on its own", len(stored))
+ }
+}
+
+func TestSkillListingFieldsMatchTheTypedEvent(t *testing.T) {
+ listing := ParseSkillListing(SystemContextSourceSystemPrompt, "Intro.\n- deploy: Deploy.\n- review: api_key=sk-abcdefghijklmnopqrstuvwxyz0123", nil)
+ var typed Event
+ listing.Apply(&typed, DefaultStringLimit)
+ want, err := json.Marshal(map[string]interface{}{"gen_ai": typed.GenAI, "system_context": typed.SystemContext, "content": typed.Content})
+ if err != nil {
+ t.Fatal(err)
+ }
+ got, err := json.Marshal(listing.Fields(DefaultStringLimit))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var gotValue, wantValue interface{}
+ if err := json.Unmarshal(got, &gotValue); err != nil {
+ t.Fatal(err)
+ }
+ if err := json.Unmarshal(want, &wantValue); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(gotValue, wantValue) {
+ t.Fatalf("map form:\n%s\ntyped form:\n%s", got, want)
+ }
+ if listing.Fields(DefaultStringLimit)["content"].(map[string]interface{})["redacted"] != true {
+ t.Fatal("content marker does not say the listing will be redacted")
+ }
+ if ParseSkillListing("", " \n", nil).Fields(DefaultStringLimit) != nil {
+ t.Fatal("an empty listing produced fields")
+ }
+}
+
+func TestSkillNameHashIsTheInventoryFormat(t *testing.T) {
+ if got := SkillNameHash("deploy"); got != "sha256:b7bd55c11b781b0ccc43aa6e57f9dadf0660e9d1d4e27e0979ee43a407d454ae" {
+ t.Fatalf("SkillNameHash = %q", got)
+ }
+ if SkillNameHash("") != "" {
+ t.Fatal("an empty name has a hash")
+ }
+}
+
+func TestGenAITextReadsEveryWrittenShape(t *testing.T) {
+ decode := func(s string) interface{} {
+ var v interface{}
+ if err := json.Unmarshal([]byte(s), &v); err != nil {
+ t.Fatal(err)
+ }
+ return v
+ }
+ cases := []struct {
+ name string
+ value interface{}
+ role string
+ partType string
+ want []string
+ }{
+ {"semconv messages", decode(`[{"role":"user","parts":[{"type":"text","content":" hi "}]}]`), "", GenAIPartTypeText, []string{"hi"}},
+ {"bare parts", decode(`[{"type":"text","content":"a"},{"type":"uri","uri":"x"},{"type":"text","content":"b"}]`), "", GenAIPartTypeText, []string{"a", "b"}},
+ {"content body and text key", decode(`[{"role":"assistant","content":[{"type":"text","text":"said"},{"type":"reasoning","content":"thought"}]}]`), RoleAssistant, GenAIPartTypeText, []string{"said"}},
+ {"reasoning parts", decode(`[{"role":"assistant","parts":[{"type":"text","content":"said"},{"type":"reasoning","content":"thought"}]}]`), RoleAssistant, GenAIPartTypeReasoning, []string{"thought"}},
+ {"role filter", decode(`[{"role":"user","parts":[{"type":"text","content":"asked"}]},{"role":"assistant","parts":[{"type":"text","content":"said"}]}]`), RoleAssistant, GenAIPartTypeText, []string{"said"}},
+ {"string body", decode(`[{"role":"user","content":"plain"}]`), "", GenAIPartTypeText, []string{"plain"}},
+ {"bare string", "whole value", "", GenAIPartTypeText, []string{"whole value"}},
+ {"bare string is never reasoning", "whole value", "", GenAIPartTypeReasoning, nil},
+ {"messages wrapper", decode(`{"messages":[{"role":"user","parts":[{"type":"text","content":"wrapped"}]}]}`), "", GenAIPartTypeText, []string{"wrapped"}},
+ {"message item", decode(`[{"type":"message","role":"user","content":"item"}]`), "", GenAIPartTypeText, []string{"item"}},
+ {"typed value", TextOutputMessages("typed"), RoleAssistant, GenAIPartTypeText, []string{"typed"}},
+ {"typed slice", []map[string]interface{}{{"type": "text", "content": "slice"}}, "", GenAIPartTypeText, []string{"slice"}},
+ {"nil", nil, "", GenAIPartTypeText, nil},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := GenAIText(tc.value, tc.role, tc.partType); !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("GenAIText = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
diff --git a/pkg/asymptoteobserve/threatrules/cel.go b/pkg/asymptoteobserve/threatrules/cel.go
index f3d826d3..94ee1aa6 100644
--- a/pkg/asymptoteobserve/threatrules/cel.go
+++ b/pkg/asymptoteobserve/threatrules/cel.go
@@ -103,9 +103,10 @@ func CompileMatch(expr string) (cel.Program, error) {
// EvalMatch runs a compiled match program against one event and reports whether it
// matched. A non-bool result is treated as an evaluation error.
//
-// Derived fields (gen_ai.tool.call.result_text) are filled on a copy of the event first, so
-// a caller holding only a program sees the same values a CompiledRule does. CompiledRule
-// skips that step for rules that never reference a derived field.
+// Derived fields (gen_ai.tool.call.result_text, gen_ai.system_instructions_text) are filled
+// on a copy of the event first, so a caller holding only a program sees the same values a
+// CompiledRule does. CompiledRule skips that step for rules that never reference a derived
+// field.
func EvalMatch(prog cel.Program, event asymptoteobserve.Event) (bool, error) {
return evalMatch(prog, withDerivedFields(event))
}
diff --git a/pkg/asymptoteobserve/threatrules/derived.go b/pkg/asymptoteobserve/threatrules/derived.go
new file mode 100644
index 00000000..1be31464
--- /dev/null
+++ b/pkg/asymptoteobserve/threatrules/derived.go
@@ -0,0 +1,79 @@
+package threatrules
+
+import (
+ "reflect"
+ "sort"
+ "strings"
+ "sync"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+// DerivedFields returns every engine-derived field as a CEL path after "e.": an event field a rule
+// can address (it has a `cel` name) that is never on the wire (`json:"-"`).
+//
+// The list is read from the event schema, so declaring a field that way is what makes it derived.
+// Compile decides from these names which rules pay for derivation, the field reference documents
+// each one from derivedFieldDocs, and the tests require each to be documented in SPEC.md and
+// cleared by withDerivedFields when a caller asserts it.
+func DerivedFields() []string {
+ return derivedFields()
+}
+
+var derivedFields = sync.OnceValue(func() []string {
+ var out []string
+ collectDerivedFields(reflect.TypeOf(asymptoteobserve.Event{}), "", map[reflect.Type]bool{}, &out)
+ sort.Strings(out)
+ return out
+})
+
+func collectDerivedFields(t reflect.Type, prefix string, seen map[reflect.Type]bool, out *[]string) {
+ for t.Kind() == reflect.Pointer {
+ t = t.Elem()
+ }
+ if t.Kind() != reflect.Struct || seen[t] {
+ return
+ }
+ seen[t] = true
+ defer delete(seen, t)
+ for i := range t.NumField() {
+ f := t.Field(i)
+ name := celFieldName(f)
+ if !f.IsExported() || name == "" || name == "-" {
+ continue
+ }
+ path := name
+ if prefix != "" {
+ path = prefix + "." + name
+ }
+ if f.Tag.Get("json") == "-" {
+ *out = append(*out, path)
+ continue
+ }
+ collectDerivedFields(f.Type, path, seen, out)
+ }
+}
+
+// derivedFieldNames is the last segment of each derived path, the only way an expression can
+// reach the field.
+var derivedFieldNames = sync.OnceValue(func() []string {
+ paths := DerivedFields()
+ names := make([]string, len(paths))
+ for i, path := range paths {
+ names[i] = path[strings.LastIndex(path, ".")+1:]
+ }
+ return names
+})
+
+// referencesDerivedField reports whether a match expression names an engine-derived field.
+// A textual check is enough: a false positive (the name inside a string literal) only costs
+// the derivation, and a false negative cannot happen because CEL has no other way to reach
+// the field.
+func referencesDerivedField(expr string) bool {
+ for _, name := range derivedFieldNames() {
+ if strings.Contains(expr, name) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/pkg/asymptoteobserve/threatrules/derived_test.go b/pkg/asymptoteobserve/threatrules/derived_test.go
new file mode 100644
index 00000000..b5f4a141
--- /dev/null
+++ b/pkg/asymptoteobserve/threatrules/derived_test.go
@@ -0,0 +1,106 @@
+package threatrules
+
+import (
+ "os"
+ "path/filepath"
+ "reflect"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+func TestDerivedFieldsAreReadFromTheSchema(t *testing.T) {
+ want := []string{SystemInstructionsTextPath, ToolResultTextPath}
+ if got := DerivedFields(); !slices.Equal(got, want) {
+ t.Fatalf("DerivedFields = %q, want %q", got, want)
+ }
+ for _, path := range DerivedFields() {
+ name := path[strings.LastIndex(path, ".")+1:]
+ if !referencesDerivedField("e." + path + `.contains("x")`) {
+ t.Errorf("a rule naming %s is not marked as needing derivation", name)
+ }
+ }
+ if referencesDerivedField(`e.gen_ai.system_instructions == null || e.prompt.text == "x"`) {
+ t.Error("a rule naming no derived field is marked as needing derivation")
+ }
+}
+
+func TestEveryDerivedFieldIsDocumented(t *testing.T) {
+ spec, err := os.ReadFile(filepath.Join(specDir(t), "SPEC.md"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, path := range DerivedFields() {
+ if derivedFieldDocs[path] == "" {
+ t.Errorf("%s has no entry in derivedFieldDocs", path)
+ }
+ if !strings.Contains(string(spec), "`e."+path+"`") {
+ t.Errorf("SPEC.md does not define the derivation of e.%s", path)
+ }
+ }
+ for path := range derivedFieldDocs {
+ if !slices.Contains(DerivedFields(), path) {
+ t.Errorf("derivedFieldDocs documents %s, which is not a derived field", path)
+ }
+ }
+}
+
+// A derived field is computed, never asserted: whatever an in-process caller put there is cleared,
+// for every field the schema declares derived, including one added after this test was written.
+func TestEveryDerivedFieldIsClearedWhenAsserted(t *testing.T) {
+ for _, path := range DerivedFields() {
+ t.Run(path, func(t *testing.T) {
+ event := asymptoteobserve.Event{}
+ setDerived(t, reflect.ValueOf(&event).Elem(), strings.Split(path, "."), "asserted")
+ derived := withDerivedFields(event)
+ if got := derivedValue(reflect.ValueOf(derived), strings.Split(path, ".")); got != "" {
+ t.Fatalf("withDerivedFields kept a caller-asserted %s = %q", path, got)
+ }
+ })
+ }
+}
+
+func fieldByCELName(v reflect.Value, name string) reflect.Value {
+ for i := range v.NumField() {
+ if celFieldName(v.Type().Field(i)) == name {
+ return v.Field(i)
+ }
+ }
+ return reflect.Value{}
+}
+
+func setDerived(t *testing.T, v reflect.Value, segments []string, value string) {
+ t.Helper()
+ field := fieldByCELName(v, segments[0])
+ if !field.IsValid() {
+ t.Fatalf("no field %q on %s", segments[0], v.Type())
+ }
+ if len(segments) == 1 {
+ field.SetString(value)
+ return
+ }
+ if field.Kind() == reflect.Pointer {
+ if field.IsNil() {
+ field.Set(reflect.New(field.Type().Elem()))
+ }
+ field = field.Elem()
+ }
+ setDerived(t, field, segments[1:], value)
+}
+
+func derivedValue(v reflect.Value, segments []string) string {
+ for _, segment := range segments {
+ for v.Kind() == reflect.Pointer {
+ if v.IsNil() {
+ return ""
+ }
+ v = v.Elem()
+ }
+ if v = fieldByCELName(v, segment); !v.IsValid() {
+ return ""
+ }
+ }
+ return v.String()
+}
diff --git a/pkg/asymptoteobserve/threatrules/evaluator.go b/pkg/asymptoteobserve/threatrules/evaluator.go
index 0de34a40..5f668ff9 100644
--- a/pkg/asymptoteobserve/threatrules/evaluator.go
+++ b/pkg/asymptoteobserve/threatrules/evaluator.go
@@ -2,7 +2,6 @@ package threatrules
import (
"fmt"
- "strings"
"time"
"github.com/google/cel-go/cel"
@@ -33,14 +32,6 @@ type CompiledRule struct {
derived bool
}
-// referencesDerivedField reports whether a match expression names an engine-derived field.
-// A textual check is enough: a false positive (the name inside a string literal) only costs
-// the derivation, and a false negative cannot happen because CEL has no other way to reach
-// the field.
-func referencesDerivedField(expr string) bool {
- return strings.Contains(expr, "result_text")
-}
-
// eval evaluates one compiled expression against one event, deriving fields first only for
// rules that reference them.
func (c *CompiledRule) eval(prog cel.Program, event asymptoteobserve.Event) (bool, error) {
diff --git a/pkg/asymptoteobserve/threatrules/fields.go b/pkg/asymptoteobserve/threatrules/fields.go
index 005d106d..f04049e6 100644
--- a/pkg/asymptoteobserve/threatrules/fields.go
+++ b/pkg/asymptoteobserve/threatrules/fields.go
@@ -128,10 +128,22 @@ func RenderFieldsMarkdown() string {
b.WriteString("\n## Derived fields\n\n")
b.WriteString("These are computed by the engine from the event just before evaluation. They are\n")
b.WriteString("never written to or read from the log, so a fixture sets the source field, not these.\n\n")
- fmt.Fprintf(&b, "- `e.%s`: `gen_ai.tool.call.result` as text, only on read-type tool\n", ToolResultTextPath)
- b.WriteString(" results (`file.read`, `mcp.tool_invoked`, MCP-named tools, web fetch/search tools) whose\n")
- b.WriteString(" content was retained. String leaves of an object or list result are joined one per line;\n")
- b.WriteString(" content blocks in a list contribute their text without metadata or encoded bytes.\n")
- fmt.Fprintf(&b, " Capped at %d bytes and secret-redacted like other retained text; empty otherwise.\n", ToolResultTextLimit)
+ for _, path := range DerivedFields() {
+ fmt.Fprintf(&b, "- `e.%s`: %s\n", path, derivedFieldDocs[path])
+ }
return b.String()
}
+
+// derivedFieldDocs is the field reference's entry for each of DerivedFields, wrapped as it is
+// rendered. TestEveryDerivedFieldIsDocumented requires one per field.
+var derivedFieldDocs = map[string]string{
+ ToolResultTextPath: "`gen_ai.tool.call.result` as text, only on read-type tool\n" +
+ " results (`file.read`, `mcp.tool_invoked`, MCP-named tools, web fetch/search tools) whose\n" +
+ " content was retained. String leaves of an object or list result are joined one per line;\n" +
+ " content blocks in a list contribute their text without metadata or encoded bytes.\n" +
+ fmt.Sprintf(" Capped at %d bytes and secret-redacted like other retained text; empty otherwise.", ToolResultTextLimit),
+ SystemInstructionsTextPath: "`gen_ai.system_instructions` as text: the `content` of each\n" +
+ " `text` part, one per line (a bare string as is), when content was retained. These are\n" +
+ " runtime-provided instructions such as a skill index, never operator prompt text.\n" +
+ fmt.Sprintf(" Each part is capped at %d bytes and secret-redacted like other retained text; empty otherwise.", SystemInstructionsPartLimit),
+}
diff --git a/pkg/asymptoteobserve/threatrules/systeminstructions.go b/pkg/asymptoteobserve/threatrules/systeminstructions.go
new file mode 100644
index 00000000..0ac4d222
--- /dev/null
+++ b/pkg/asymptoteobserve/threatrules/systeminstructions.go
@@ -0,0 +1,44 @@
+package threatrules
+
+import (
+ "strings"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+// SystemInstructionsTextPath is the CEL path of the derived system-instructions text, after
+// the "e." prefix.
+const SystemInstructionsTextPath = "gen_ai.system_instructions_text"
+
+// SystemInstructionsPartLimit bounds each text part a rule sees, at the limit the writers apply
+// to retained text. The bound is per part, not on the whole value, because a skill index holds one
+// part per skill: capping the joined text would hide every skill after the first few.
+const SystemInstructionsPartLimit = asymptoteobserve.DefaultStringLimit
+
+// SystemInstructionsTextLimit bounds the whole derived text at the largest event a writer keeps,
+// so it only limits an event built in memory; anything read from a log is already smaller.
+const SystemInstructionsTextLimit = 64 * 1024
+
+// SystemInstructionsText returns the text a rule sees as e.gen_ai.system_instructions_text: the
+// instructions a runtime gave the model outside the conversation, such as the skill index Beacon
+// records on session.context, or the system instructions an OTLP producer reports on an inference
+// span. It is never operator-submitted prompt text.
+//
+// The value is read in its OpenTelemetry GenAI semconv shape, a list of message parts, by
+// asymptoteobserve.GenAIText: the text of each `text` part, in order, one per line, and a bare
+// string as text. Each part is capped at SystemInstructionsPartLimit and passed through the
+// writers' secret redaction, and the text is empty when the event's content marker, if it has one,
+// says content was not retained, so a rule never sees more than was retained.
+//
+// It is exported so that another engine implementing the Threat Rules contract can derive the
+// field the same way; fixtures that exercise it only conform if the derivation agrees.
+func SystemInstructionsText(event asymptoteobserve.Event) string {
+ if event.GenAI == nil || event.GenAI.SystemInstructions == nil || !contentRetained(event.Content) {
+ return ""
+ }
+ parts := asymptoteobserve.GenAIText(event.GenAI.SystemInstructions, "", asymptoteobserve.GenAIPartTypeText)
+ for i, part := range parts {
+ parts[i] = asymptoteobserve.CleanString(part, SystemInstructionsPartLimit, true)
+ }
+ return asymptoteobserve.TruncateString(strings.Join(parts, "\n"), SystemInstructionsTextLimit)
+}
diff --git a/pkg/asymptoteobserve/threatrules/systeminstructions_test.go b/pkg/asymptoteobserve/threatrules/systeminstructions_test.go
new file mode 100644
index 00000000..07418c7f
--- /dev/null
+++ b/pkg/asymptoteobserve/threatrules/systeminstructions_test.go
@@ -0,0 +1,193 @@
+package threatrules
+
+import (
+ "encoding/json"
+ "fmt"
+ "strings"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+const skillCanary = "BCN-2AFE23-C01I"
+
+func systemContextEvent(instructions interface{}) asymptoteobserve.Event {
+ return asymptoteobserve.Event{
+ Event: asymptoteobserve.EventInfo{Action: "session.context"},
+ Session: &asymptoteobserve.SessionInfo{ID: "s1"},
+ GenAI: &asymptoteobserve.GenAIInfo{SystemInstructions: instructions},
+ }
+}
+
+func decodeEvent(t *testing.T, line string) asymptoteobserve.Event {
+ t.Helper()
+ var event asymptoteobserve.Event
+ if err := json.Unmarshal([]byte(line), &event); err != nil {
+ t.Fatalf("decode %s: %v", line, err)
+ }
+ return event
+}
+
+func TestSystemInstructionsTextIsAddressableInCEL(t *testing.T) {
+ var found bool
+ for _, f := range EventFields() {
+ if f.Path == SystemInstructionsTextPath {
+ found = true
+ if f.Type != "string" {
+ t.Fatalf("%s type = %q, want string", f.Path, f.Type)
+ }
+ }
+ }
+ if !found {
+ t.Fatalf("%s missing from EventFields", SystemInstructionsTextPath)
+ }
+ if _, err := CompileMatch(`e.gen_ai.system_instructions == "x"`); err == nil {
+ t.Fatal("gen_ai.system_instructions compiled; want only the derived text in the CEL schema")
+ }
+}
+
+// Beacon's own events and OTLP-sourced ones carry the same semconv shape, so one rule matches both.
+func TestSystemInstructionsTextReadsTheSemconvShapes(t *testing.T) {
+ const expr = `e.gen_ai.system_instructions_text.contains("` + skillCanary + `")`
+ cases := map[string]asymptoteobserve.Event{
+ "typed parts from a mapper": systemContextEvent(asymptoteobserve.SystemInstructionParts("- oracle-index: " + skillCanary)),
+ "parts from a log line": decodeEvent(t, `{"gen_ai":{"system_instructions":[{"type":"text","content":"`+skillCanary+`"}]}}`),
+ "bare string from OTLP": decodeEvent(t, `{"gen_ai":{"system_instructions":"`+skillCanary+`"}}`),
+ "text part among others": decodeEvent(t, `{"gen_ai":{"system_instructions":[`+
+ `{"type":"uri","uri":"https://example.test"},{"type":"text","content":"`+skillCanary+`"}]}}`),
+ }
+ for name, event := range cases {
+ t.Run(name, func(t *testing.T) {
+ if !mustMatch(t, expr, event) {
+ t.Fatalf("no match; derived text = %q", SystemInstructionsText(event))
+ }
+ })
+ }
+
+ nonText := decodeEvent(t, `{"gen_ai":{"system_instructions":[{"type":"uri","uri":"`+skillCanary+`"}]}}`)
+ if got := SystemInstructionsText(nonText); got != "" {
+ t.Fatalf("non-text part derived %q, want empty", got)
+ }
+ multi := systemContextEvent([]interface{}{
+ map[string]interface{}{"type": "text", "content": "first"},
+ map[string]interface{}{"type": "text", "content": "second"},
+ })
+ if got := SystemInstructionsText(multi); got != "first\nsecond" {
+ t.Fatalf("multi-part text = %q, want parts joined in order", got)
+ }
+}
+
+func TestSystemInstructionsTextEmptyWhenContentNotRetained(t *testing.T) {
+ for name, content := range map[string]*asymptoteobserve.ContentInfo{
+ "not included": {Retention: asymptoteobserve.ContentRetentionFull, Included: false},
+ "metadata only": {Retention: asymptoteobserve.ContentRetentionMetadata, Included: true},
+ } {
+ t.Run(name, func(t *testing.T) {
+ event := systemContextEvent(asymptoteobserve.SystemInstructionParts(skillCanary))
+ event.Content = content
+ if got := SystemInstructionsText(event); got != "" {
+ t.Fatalf("SystemInstructionsText = %q, want empty", got)
+ }
+ if mustMatch(t, `e.gen_ai.system_instructions_text != ""`, event) {
+ t.Fatal("system_instructions_text non-empty in CEL")
+ }
+ })
+ }
+}
+
+func TestSystemInstructionsTextIsRedactedAndCapped(t *testing.T) {
+ const secret = "sk-abcdefghijklmnopqrstuvwxyz0123"
+ event := systemContextEvent(asymptoteobserve.SystemInstructionParts("skill index\napi_key=" + secret))
+ text := SystemInstructionsText(event)
+ if strings.Contains(text, secret) || !strings.Contains(text, "[REDACTED]") {
+ t.Fatalf("derived text = %q, want the redacted copy", text)
+ }
+ if mustMatch(t, `e.gen_ai.system_instructions_text.contains("`+secret+`")`, event) {
+ t.Fatal("a rule matched the raw secret")
+ }
+
+ long := systemContextEvent(asymptoteobserve.SystemInstructionParts(strings.Repeat("a", SystemInstructionsPartLimit*3)))
+ if got := SystemInstructionsText(long); len(got) > SystemInstructionsPartLimit {
+ t.Fatalf("one part derived %d bytes, want <= %d", len(got), SystemInstructionsPartLimit)
+ }
+ many := make([]interface{}, 40)
+ for i := range many {
+ many[i] = map[string]interface{}{"type": "text", "content": strings.Repeat("b", SystemInstructionsPartLimit)}
+ }
+ if got := SystemInstructionsText(systemContextEvent(many)); len(got) > SystemInstructionsTextLimit {
+ t.Fatalf("whole value derived %d bytes, want <= %d", len(got), SystemInstructionsTextLimit)
+ }
+}
+
+// The case the per-part bound exists for: Claude Code's real index ran to about 6 KB for fifteen
+// skills, and the writer caps each stored string at 2 KB. One part kept the first four skills; one
+// part per skill keeps all of them, so a skill listed last is as visible as one listed first.
+func TestSystemInstructionsTextSeesEverySkillOfALongIndex(t *testing.T) {
+ names := make([]string, 15)
+ var lines []string
+ for i := range names {
+ names[i] = fmt.Sprintf("skill-%02d", i)
+ lines = append(lines, "- "+names[i]+": "+strings.Repeat("Describes when to use this skill. ", 12))
+ }
+ lines[14] += " " + skillCanary
+ listing := asymptoteobserve.ParseSkillListing(asymptoteobserve.SystemContextSourceTranscript, strings.Join(lines, "\n"), names)
+ if len(listing.Text()) < 6000 {
+ t.Fatalf("fixture is %d bytes, want a realistic 6 KB index", len(listing.Text()))
+ }
+ event := systemContextEvent(nil)
+ listing.Apply(&event, asymptoteobserve.DefaultRawStringLimit)
+
+ line, err := json.Marshal(asymptoteobserve.SanitizeEvent(event, 64*1024))
+ if err != nil {
+ t.Fatal(err)
+ }
+ fromLog := decodeEvent(t, string(line))
+ if !mustMatch(t, `e.gen_ai.system_instructions_text.contains("`+skillCanary+`")`, fromLog) {
+ t.Fatalf("the last skill is invisible to rules after the writer stored the index")
+ }
+ if !mustMatch(t, `e.system_context.kind == "skill_listing" && e.system_context.skills.exists(s, s.skill_name == "skill-14")`, fromLog) {
+ t.Fatal("system_context does not name the listed skills")
+ }
+}
+
+func TestSystemInstructionsTextIsNeverSerialized(t *testing.T) {
+ event := systemContextEvent(asymptoteobserve.SystemInstructionParts("plain"))
+ event.GenAI.SystemInstructionsText = "asserted"
+ data, err := json.Marshal(event)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(data), "system_instructions_text") || strings.Contains(string(data), "asserted") {
+ t.Fatalf("system_instructions_text serialized: %s", data)
+ }
+ decoded := decodeEvent(t, `{"gen_ai":{"system_instructions_text":"`+skillCanary+`"}}`)
+ if decoded.GenAI.SystemInstructionsText != "" {
+ t.Fatalf("a log line set system_instructions_text: %q", decoded.GenAI.SystemInstructionsText)
+ }
+ if mustMatch(t, `e.gen_ai.system_instructions_text == "asserted"`, event) {
+ t.Fatal("engine used a caller-asserted system_instructions_text")
+ }
+}
+
+func TestCompiledRuleDerivesSystemInstructionsTextWithoutMutatingEvents(t *testing.T) {
+ rule := validSingleEventRule()
+ rule.Match = `e.gen_ai.system_instructions_text.contains("` + skillCanary + `")`
+ compiled, err := Compile(rule)
+ if err != nil {
+ t.Fatalf("compile: %v", err)
+ }
+ if !compiled.derived {
+ t.Fatal("rule referencing system_instructions_text not marked as needing derivation")
+ }
+ events := []asymptoteobserve.Event{systemContextEvent(asymptoteobserve.SystemInstructionParts("Skill index " + skillCanary))}
+ verdict, err := compiled.Evaluate(events)
+ if err != nil {
+ t.Fatalf("evaluate: %v", err)
+ }
+ if verdict != VerdictMatch {
+ t.Fatalf("verdict = %s, want match", verdict)
+ }
+ if got := events[0].GenAI.SystemInstructionsText; got != "" {
+ t.Fatalf("caller's event mutated: system_instructions_text = %q", got)
+ }
+}
diff --git a/pkg/asymptoteobserve/threatrules/toolresult.go b/pkg/asymptoteobserve/threatrules/toolresult.go
index 62f49c2a..7033b774 100644
--- a/pkg/asymptoteobserve/threatrules/toolresult.go
+++ b/pkg/asymptoteobserve/threatrules/toolresult.go
@@ -56,18 +56,26 @@ func ToolResultText(event asymptoteobserve.Event) string {
// the path are copied rather than written through, so the caller's event -- which is shared
// across rules and may be evidence in a Finding -- is never mutated.
func withDerivedFields(event asymptoteobserve.Event) asymptoteobserve.Event {
- text := ToolResultText(event)
- if text == "" && (event.GenAI == nil || event.GenAI.Tool == nil || event.GenAI.Tool.Call == nil || event.GenAI.Tool.Call.ResultText == "") {
+ if event.GenAI == nil {
return event
}
- // Either there is text to set or an in-process caller set the field itself; the field is
+ resultText := ToolResultText(event)
+ instructionsText := SystemInstructionsText(event)
+ callAsserted := event.GenAI.Tool != nil && event.GenAI.Tool.Call != nil && event.GenAI.Tool.Call.ResultText != ""
+ if resultText == "" && !callAsserted && instructionsText == "" && event.GenAI.SystemInstructionsText == "" {
+ return event
+ }
+ // Either there is text to set or an in-process caller set a field itself; the fields are
// derived, never asserted, so the second case is cleared.
genAI := *event.GenAI
- tool := *genAI.Tool
- call := *tool.Call
- call.ResultText = text
- tool.Call = &call
- genAI.Tool = &tool
+ genAI.SystemInstructionsText = instructionsText
+ if resultText != "" || callAsserted {
+ tool := *genAI.Tool
+ call := *tool.Call
+ call.ResultText = resultText
+ tool.Call = &call
+ genAI.Tool = &tool
+ }
event.GenAI = &genAI
return event
}
diff --git a/plugins/omp-beacon/src/beacon.test.ts b/plugins/omp-beacon/src/beacon.test.ts
index 30b4652f..8821830d 100644
--- a/plugins/omp-beacon/src/beacon.test.ts
+++ b/plugins/omp-beacon/src/beacon.test.ts
@@ -6,10 +6,12 @@ const senderKey = Symbol.for("beacon.omp.testSender")
type Sent = Record<string, unknown>
-function captureSends(): Sent[] {
+// accepted decides what each send reports back, standing in for whether the hook binary took it.
+function captureSends(accepted: () => boolean = () => true): Sent[] {
const sent: Sent[] = []
;(globalThis as Record<symbol, unknown>)[senderKey] = (payload: Sent) => {
sent.push(payload)
+ return accepted()
}
return sent
}
@@ -54,6 +56,7 @@ describe("beacon oh my pi extension", () => {
createBeaconExtension().register(omp.api)
expect([...omp.handlers.keys()].sort()).toEqual([
+ "context",
"input",
"message_end",
"session_shutdown",
@@ -89,7 +92,6 @@ describe("beacon oh my pi extension", () => {
"message_start",
"before_provider_request",
"after_provider_response",
- "context",
"tool_execution_update",
"tool_execution_start",
"auto_compaction_start",
@@ -110,6 +112,109 @@ describe("beacon oh my pi extension", () => {
expect(omp.handlers.has("mcp_notification")).toBe(false)
})
+ // The skill-listing cache outlives one extension instance, so each test below uses its own
+ // session id rather than inheriting another test's remembered listing.
+ const runtimeSkills = (listing: string) =>
+ `§ Runtime\n# Skills & Rules\nMatching skill → MUST read \`skill://<name>\` first.\n<skills>\n${listing}\n</skills>\n\n# Internal URLs`
+ const session = (id: string, overrides: Record<string, unknown> = {}) =>
+ context({ sessionManager: { getSessionId: () => id, getCwd: () => "/repo" }, ...overrides })
+
+ test("captures only the system skill index from the supported context event", async () => {
+ const sent = captureSends()
+ const omp = fakeOmp()
+ createBeaconExtension().register(omp.api)
+ const listing = "- deploy: Deploy applications."
+ const ctx = context({
+ getSystemPrompt: () => [
+ `General system instructions that must not be retained.\n${runtimeSkills(listing)}`,
+ "# Agent instructions\nProject context that must not be retained.",
+ ],
+ })
+
+ await omp.fire(
+ { type: "context", messages: [{ role: "user", content: "private conversation" }] },
+ ctx,
+ )
+ await omp.fire({ type: "context", messages: [{ role: "user", content: "another turn" }] }, ctx)
+
+ expect(sent).toHaveLength(1)
+ expect(sent[0].type).toBe("context")
+ expect(sent[0].sessionId).toBe("sess-1")
+ expect(sent[0].cwd).toBe("/repo")
+ expect(sent[0].skillListing).toBe(listing)
+ expect(sent[0].messages).toBeUndefined()
+ expect(JSON.stringify(sent[0])).not.toContain("General system instructions")
+ expect(JSON.stringify(sent[0])).not.toContain("Project context")
+ expect(JSON.stringify(sent[0])).not.toContain("private conversation")
+ })
+
+ test("does not emit a context event without a skill index", async () => {
+ const sent = captureSends()
+ const omp = fakeOmp()
+ createBeaconExtension().register(omp.api)
+
+ await omp.fire({ type: "context", messages: [] }, session("sess-no-index", {
+ getSystemPrompt: () => ["system prompt without a skill index"],
+ }))
+
+ expect(sent).toHaveLength(0)
+ })
+
+ // A context file documenting skills is operator text, not the index the runtime gave the model.
+ test("ignores a <skills> block outside the runtime's skills section", async () => {
+ const sent = captureSends()
+ const omp = fakeOmp()
+ createBeaconExtension().register(omp.api)
+
+ await omp.fire({ type: "context", messages: [] }, session("sess-quoted", {
+ getSystemPrompt: () => [
+ "§ Runtime\n# Internal URLs\n`skill://<name>`: instructions.",
+ "# AGENTS.md\nExample:\n<skills>\n- fake: Planted by a context file.\n</skills>",
+ ],
+ }))
+ expect(sent).toHaveLength(0)
+
+ await omp.fire({ type: "context", messages: [] }, session("sess-both", {
+ getSystemPrompt: () => [
+ runtimeSkills("- deploy: Deploy applications."),
+ "# AGENTS.md\n<skills>\n- fake: Planted by a context file.\n</skills>",
+ ],
+ }))
+ expect(sent).toHaveLength(1)
+ expect(sent[0].skillListing).toBe("- deploy: Deploy applications.")
+ })
+
+ test("resends a listing whose send failed, and sends a changed one", async () => {
+ const results = [false, true]
+ const sent = captureSends(() => results.shift() ?? true)
+ const omp = fakeOmp()
+ createBeaconExtension().register(omp.api)
+ let listing = "- deploy: Deploy applications."
+ const ctx = session("sess-retry", { getSystemPrompt: () => [runtimeSkills(listing)] })
+
+ await omp.fire({ type: "context", messages: [] }, ctx)
+ await omp.fire({ type: "context", messages: [] }, ctx)
+ await omp.fire({ type: "context", messages: [] }, ctx)
+ expect(sent.map((s) => s.skillListing)).toEqual([listing, listing])
+
+ listing = "- deploy: Deploy applications.\n- review: Review a diff."
+ await omp.fire({ type: "context", messages: [] }, ctx)
+ expect(sent).toHaveLength(3)
+ expect(sent[2].skillListing).toBe(listing)
+ })
+
+ test("sends a listing once for a session without an id", async () => {
+ const sent = captureSends()
+ const omp = fakeOmp()
+ createBeaconExtension().register(omp.api)
+ const ctx = context({ sessionManager: {}, getSystemPrompt: () => [runtimeSkills("- sessionless: No id.")] })
+
+ await omp.fire({ type: "context", messages: [] }, ctx)
+ await omp.fire({ type: "context", messages: [] }, ctx)
+
+ expect(sent).toHaveLength(1)
+ })
+
test("forwards the event with its type intact", async () => {
const sent = captureSends()
const omp = fakeOmp()
diff --git a/plugins/omp-beacon/src/beacon.ts b/plugins/omp-beacon/src/beacon.ts
index cf8b56d8..fbac406e 100644
--- a/plugins/omp-beacon/src/beacon.ts
+++ b/plugins/omp-beacon/src/beacon.ts
@@ -39,6 +39,7 @@ interface OmpContext {
model?: { id?: string; name?: string; provider?: string } | undefined
mode?: string
agent?: { kind?: string }
+ getSystemPrompt?: () => string[]
}
// The events Beacon subscribes to, and nothing else.
@@ -49,6 +50,10 @@ interface OmpContext {
// runtime log with rows no investigation asks for and would put Beacon in the path of every
// streaming token update.
//
+// `context` is the one exception: its supported handler context exposes the effective system
+// prompt. Beacon extracts only the `<skills>` index and discards the conversation messages and
+// every other system-prompt section.
+//
// `tool_call` and `tool_result` are the pair carrying tool activity: the first names the tool and
// its arguments before it runs, the second carries the outcome. `user_bash` and `user_python` are
// code the operator ran with Oh My Pi's `!` and `$` prefixes, which no tool event covers.
@@ -70,6 +75,7 @@ const subscribedEvents = [
"session_start",
"session_shutdown",
"input",
+ "context",
"tool_call",
"tool_result",
"tool_approval_requested",
@@ -79,6 +85,42 @@ const subscribedEvents = [
"message_end",
] as const
+// Oh My Pi renders its skill index under this heading in the runtime section of the system prompt,
+// after a line of instructions. The match is anchored to the heading because the prompt also
+// carries operator text -- project context files, appended prompts -- and a `<skills>` block quoted
+// there is not the index the runtime gave the model.
+const skillsSection = /^# Skills & Rules\n(?:[^\n]*\n){0,3}?<skills>\s*([\s\S]*?)\s*<\/skills>/m
+
+// How many sessions' last-sent listings are remembered, least recently sent evicted first.
+const maxRememberedSkillSessions = 64
+
+function skillListing(ctx: OmpContext | undefined): string {
+ try {
+ const prompt = ctx?.getSystemPrompt?.()
+ if (!Array.isArray(prompt)) return ""
+ for (const block of prompt) {
+ if (typeof block !== "string") continue
+ const match = skillsSection.exec(block)
+ const listing = match?.[1]?.trim()
+ if (listing) return listing
+ }
+ } catch (err) {
+ debugLog("system prompt could not be read", err)
+ }
+ return ""
+}
+
+function rememberSkillListing(seen: Map<string, string>, key: string, listing: string) {
+ // Re-inserted rather than updated so the map's insertion order is send order, and eviction
+ // drops the session that has gone longest without a change.
+ seen.delete(key)
+ if (seen.size >= maxRememberedSkillSessions) {
+ const oldest = seen.keys().next().value
+ if (oldest !== undefined) seen.delete(oldest)
+ }
+ seen.set(key, listing)
+}
+
// How many in-flight tool calls the approval enrichment below will remember at once.
//
// Bounded because the map is cleared by `tool_result`, and a call that never produces one -- an
@@ -100,6 +142,7 @@ const maxPendingToolCalls = 64
// process, so remembering it until `tool_result` is exact: the join is the runtime's own call id,
// not a timestamp or a guess.
const pendingToolCalls = new Map<string, unknown>()
+const rememberedSkillListings = new Map<string, string>()
function rememberToolCall(event: OmpEvent) {
const id = typeof event.toolCallId === "string" ? event.toolCallId : ""
@@ -198,15 +241,17 @@ function safeClone(value: unknown, depth = 0, seen = new WeakSet<object>()): unk
}
}
-async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
+// sendToBeacon resolves true only when the hook binary read the event and exited cleanly. Every
+// failure is still swallowed -- telemetry never interrupts the run -- but a caller that sends
+// something once rather than per event needs to know whether to try again.
+async function sendToBeacon(payload: Record<string, unknown>): Promise<boolean> {
// Flattened before anything else sees it, so every consumer of this function is handed plain
// data rather than a live event with cycles and functions still attached.
const safe = safeClone(payload)
const testSender = (globalThis as Record<symbol, unknown>)[Symbol.for("beacon.omp.testSender")]
if (typeof testSender === "function") {
- await (testSender as (value: unknown) => unknown)(safe)
- return
+ return (await (testSender as (value: unknown) => unknown)(safe)) !== false
}
let body: string
@@ -214,21 +259,21 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
body = JSON.stringify(safe)
} catch (err) {
debugLog("payload could not be serialized", err)
- return
+ return false
}
- if (!body) return
+ if (!body) return false
try {
// Imported lazily so a host without node:child_process fails to send rather than failing to
// load: a throwing import at module scope would surface to the user as a broken extension, and
// Oh My Pi reports an extension that throws at load time rather than continuing without it.
const { spawn } = await import("node:child_process")
- await new Promise<void>((resolve) => {
+ return await new Promise<boolean>((resolve) => {
let settled = false
- const finish = () => {
+ const finish = (ok: boolean) => {
if (settled) return
settled = true
- resolve()
+ resolve(ok)
}
let child
try {
@@ -238,7 +283,7 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
})
} catch (err) {
debugLog("hook binary could not be spawned", err)
- finish()
+ finish(false)
return
}
const timer = setTimeout(() => {
@@ -248,19 +293,19 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
// Already gone.
}
debugLog("hook binary timed out", { type: payload?.type })
- finish()
+ finish(false)
}, sendTimeoutMs)
// An unreferenced timer cannot keep Oh My Pi alive at exit waiting on Beacon telemetry.
if (typeof timer.unref === "function") timer.unref()
- const done = () => {
+ const done = (ok: boolean) => {
clearTimeout(timer)
- finish()
+ finish(ok)
}
child.on("error", (err) => {
debugLog("hook binary failed", err)
- done()
+ done(false)
})
- child.on("close", done)
+ child.on("close", (code: number | null) => done(code === 0))
// Without this, a hook binary that exits before reading stdin turns an EPIPE into an
// unhandled error event in the host process. Beacon telemetry must never do that.
child.stdin?.on("error", () => {})
@@ -273,6 +318,7 @@ async function sendToBeacon(payload: Record<string, unknown>): Promise<void> {
} catch (err) {
debugLog("send failed", err)
// Beacon telemetry must never interrupt Oh My Pi execution.
+ return false
}
}
@@ -343,10 +389,24 @@ export function createBeaconExtension() {
const forward = async (event: OmpEvent, ctx: OmpContext) => {
try {
+ const fields = identity(ctx)
+ if (event.type === "context") {
+ // Only the listing leaves the extension, and only when it changed: `context` fires before
+ // every model call and carries the whole conversation. It is remembered after a send that
+ // landed, so a failed send is retried on the next call instead of losing the listing.
+ const listing = skillListing(ctx)
+ // A session without an id is remembered under "", so it is still sent once rather than
+ // before every model call.
+ const key = typeof fields.sessionId === "string" ? fields.sessionId : ""
+ if (!listing || rememberedSkillListings.get(key) === listing) return
+ if (await sendToBeacon({ ...fields, type: "context", skillListing: listing })) {
+ rememberSkillListing(rememberedSkillListings, key, listing)
+ }
+ return
+ }
// The event is spread last so a field it carries itself wins over the context's. `user_bash`
// and `user_python` both carry their own cwd, and the approval events carry their own
// sessionId -- in each case the event's is the one that describes this action.
- const fields = identity(ctx)
const envelope: Record<string, unknown> = { ...fields, ...event }
const sessionId = typeof envelope.sessionId === "string" ? envelope.sessionId : ""
@@ -373,6 +433,7 @@ export function createBeaconExtension() {
// visible in one place.
pendingToolCalls.clear()
inputSessionId = null
+ rememberedSkillListings.delete(sessionId)
} else if (event.type === "tool_approval_requested" || event.type === "tool_approval_resolved") {
// Attached under `input`, the same key `tool_call` uses, so the mapper reads one shape for
// both and an approval resolves to the same command or file path its tool call did.
diff --git a/spec/threat-rules/FIELDS.md b/spec/threat-rules/FIELDS.md
index 0a44c235..28c65282 100644
--- a/spec/threat-rules/FIELDS.md
+++ b/spec/threat-rules/FIELDS.md
@@ -75,6 +75,7 @@ Regenerate with `beacon rules fields --markdown > spec/threat-rules/FIELDS.md`.
| `e.gen_ai.response.model` | string |
| `e.gen_ai.response.time_to_first_chunk` | double |
| `e.gen_ai.retrieval.query_text` | string |
+| `e.gen_ai.system_instructions_text` | string |
| `e.gen_ai.token.type` | string |
| `e.gen_ai.tool.call.id` | string |
| `e.gen_ai.tool.call.result_text` | string |
@@ -143,6 +144,9 @@ Regenerate with `beacon rules fields --markdown > spec/threat-rules/FIELDS.md`.
| `e.session.id` | string |
| `e.session.working_directory` | string |
| `e.severity` | string |
+| `e.system_context.kind` | string |
+| `e.system_context.skills` | list(asymptoteobserve.SkillRefInfo) |
+| `e.system_context.source` | string |
| `e.timestamp` | string |
| `e.tool.command` | string |
| `e.tool.name` | string |
@@ -168,6 +172,10 @@ Regenerate with `beacon rules fields --markdown > spec/threat-rules/FIELDS.md`.
These are computed by the engine from the event just before evaluation. They are
never written to or read from the log, so a fixture sets the source field, not these.
+- `e.gen_ai.system_instructions_text`: `gen_ai.system_instructions` as text: the `content` of each
+ `text` part, one per line (a bare string as is), when content was retained. These are
+ runtime-provided instructions such as a skill index, never operator prompt text.
+ Each part is capped at 4096 bytes and secret-redacted like other retained text; empty otherwise.
- `e.gen_ai.tool.call.result_text`: `gen_ai.tool.call.result` as text, only on read-type tool
results (`file.read`, `mcp.tool_invoked`, MCP-named tools, web fetch/search tools) whose
content was retained. String leaves of an object or list result are joined one per line;
diff --git a/spec/threat-rules/SPEC.md b/spec/threat-rules/SPEC.md
index 5a02390e..bf55601f 100644
--- a/spec/threat-rules/SPEC.md
+++ b/spec/threat-rules/SPEC.md
@@ -153,6 +153,23 @@ the steps in both orders still fires once. A single event never satisfies two st
as an MCP tool (`mcp__…`, `MCP:…`) or a web fetch/search tool — and its `content`
marker, if present, is `included` with a `retention` other than `metadata`. The
reference derivation is `threatrules.ToolResultText`.
+- `e.gen_ai.system_instructions_text` is derived under the same rules (computed, never
+ read from input, secret redaction, empty unless the `content` marker, if present, is
+ `included` with a `retention` other than `metadata`). It is `gen_ai.system_instructions`
+ read in its OpenTelemetry GenAI semconv shape, a list of message parts: the text of each
+ part whose `type` is `text` (its `content`, or its `text` when `content` is empty), each
+ trimmed and capped at 4096 bytes, in order, joined by `\n`, and the whole capped at 65536
+ bytes. A bare string, as the whole value or as a list element, is taken as text; parts of
+ any other type contribute nothing. The cap is per part because a skill index holds one
+ part per skill, and a cap on the joined text would hide every skill after the first few.
+ It is runtime-provided model instructions (for example the skill index on
+ `session.context`), never operator-submitted prompt text. The reference derivation is
+ `threatrules.SystemInstructionsText`.
+- `e.system_context` is on the wire, not derived: on `session.context` it names what the
+ runtime gave the model. `kind` is `skill_listing` for a skill index, `source` is where
+ Beacon read it (`system_prompt`, `transcript`), and `skills` names each listed skill as
+ `skill_name` and `skill_name_hash`, so a rule can select an index without matching its
+ text: `e.system_context.skills.exists(s, s.skill_name == "deploy")`.
- An engine MUST reject (at load) any rule whose CEL expression fails to compile or does
not type to `bool`. Because field paths are checked against the event schema, a typo
like `e.fil.path` is a load-time error.