The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
refactor(observe): fit and stamp event lines in one place for every writer
Kyle Elliott6d70e3002ec1
diff --git a/cli/beacon-hooks/internal/logging/endpoint_event_test.go b/cli/beacon-hooks/internal/logging/endpoint_event_test.goindex 77b1d1f2..0812909f 100644--- a/cli/beacon-hooks/internal/logging/endpoint_event_test.go+++ b/cli/beacon-hooks/internal/logging/endpoint_event_test.go@@ -383,32 +383,6 @@ func TestEndpointEventCarriesExplicitInferredFidelity(t *testing.T) {}}-// The size-limit fallback rewrites the event down to a key allowlist. Dropping the ordering keys-// there would leave exactly the events most worth ordering unorderable. Both provenance fields live-// inside blocks that survive wholesale, but a future edit to that list could drop them -- and-// losing the marker silently turns an inferred event into an unmarked one, which reads as trusted.-func TestMinimalEndpointEventKeepsOrderingAndProvenance(t *testing.T) {- minimal := minimalEndpointEvent(map[string]interface{}{- "timestamp": "2026-08-21T10:00:00.000000000Z",- "sequence": uint64(42),- "message": "big",- "event": map[string]interface{}{"action": "approval.allowed", "fidelity": "inferred"},- "harness": map[string]interface{}{"name": "cursor", "collection_method": "hook"},- })- if minimal["timestamp"] != "2026-08-21T10:00:00.000000000Z" {- t.Fatalf("minimal timestamp = %v", minimal["timestamp"])- }- if minimal["sequence"] != uint64(42) {- t.Fatalf("minimal sequence = %v, want 42", minimal["sequence"])- }- if fidelity, ok := nestedString(t, minimal, "event", "fidelity"); !ok || fidelity != "inferred" {- t.Fatalf("compacted event.fidelity = %q (present=%v), want inferred", fidelity, ok)- }- if method, ok := nestedString(t, minimal, "harness", "collection_method"); !ok || method != "hook" {- t.Fatalf("compacted harness.collection_method = %q (present=%v), want hook", method, ok)- }-}-// The provider prefix that canonicalizing removes is kept rather than dropped: it moves to// gen_ai.provider.name, where a provider belongs and stays queryable.func TestEndpointEventKeepsTheStrippedProviderPrefix(t *testing.T) {diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.goindex e641a937..42ad3f15 100644--- a/cli/beacon-hooks/internal/logging/logging.go+++ b/cli/beacon-hooks/internal/logging/logging.go@@ -559,43 +559,23 @@ func writeEndpointJSONFile(path string, event map[string]interface{}) error {return deliverEndpointLine(path, data)}-// encodeEndpointLine is the line an endpoint event is written as: sanitized, cut down to the-// 64 KiB event limit, and stamped with its event.id.+// encodeEndpointLine is the line an endpoint event is written as: sanitized, then fitted to the+// event size limit and stamped with its event.id the way every writer does (FitEventLine).func encodeEndpointLine(event map[string]interface{}) ([]byte, error) {data, err := json.Marshal(sanitizeEndpointMap(event))if err != nil {return nil, err}- if len(data) > 64*1024 {- event["field_truncated"] = true- event["raw"] = nil- event["message"] = truncateEndpoint(fmt.Sprint(event["message"]), 1024)- data, err = json.Marshal(sanitizeEndpointMap(event))- if err != nil {- return nil, err- }- }- if len(data) > 64*1024 {- compactEndpointContent(event)- data, err = json.Marshal(sanitizeEndpointMap(event))- if err != nil {- return nil, err- }- }- if len(data) > 64*1024 {- event = minimalEndpointEvent(event)- data, err = json.Marshal(sanitizeEndpointMap(event))- if err != nil {- return nil, err+ var stamp func(id string) ([]byte, error)+ if info, ok := event["event"].(map[string]interface{}); ok {+ if id, _ := info["id"].(string); strings.TrimSpace(id) == "" {+ stamp = func(id string) ([]byte, error) {+ info["id"] = id+ return json.Marshal(sanitizeEndpointMap(event))+ }}}- if len(data) > 64*1024 {- return nil, fmt.Errorf("endpoint event exceeds 64 KiB after metadata fallback")- }- // Stamped last, over the bytes that are actually about to be written, so- // none of the size fallbacks above can leave the ID describing an event that- // no longer exists.- return withEndpointEventID(event, data)+ return asymptoteobserve.FitEventLine(data, asymptoteobserve.DefaultMaxEventBytes, stamp)}// EndpointIngest delivers to the system log through the collector when the endpoint is in the@@ -636,89 +616,6 @@ func deliverEndpointLine(path string, data []byte) error {})}-// withEndpointEventID fills event.id on an event assembled as a map and returns-// its re-marshalled line. line must be the marshalled form of event with no ID-// on it, which is what EventIDForLine is derived from.-func withEndpointEventID(event map[string]interface{}, line []byte) ([]byte, error) {- info, ok := event["event"].(map[string]interface{})- if !ok {- return line, nil- }- if id, _ := info["id"].(string); strings.TrimSpace(id) != "" {- return line, nil- }- info["id"] = asymptoteobserve.EventIDForLine(line)- data, err := json.Marshal(sanitizeEndpointMap(event))- if err != nil {- return nil, err- }- // An event already at the size limit keeps its bytes and goes without an ID:- // the limit is a hard contract, and refusing to write an event over forty- // bytes of identity would be the worse trade.- if len(data) > 64*1024 {- return line, nil- }- return data, nil-}--func compactEndpointContent(event map[string]interface{}) {- if file, ok := event["file"].(map[string]interface{}); ok {- delete(file, "diff")- }- if command, ok := event["command"].(map[string]interface{}); ok {- delete(command, "output")- }- if genAI, ok := event["gen_ai"].(map[string]interface{}); ok {- delete(genAI, "input")- delete(genAI, "output")- if tool, ok := genAI["tool"].(map[string]interface{}); ok {- if call, ok := tool["call"].(map[string]interface{}); ok {- delete(call, "arguments")- delete(call, "result")- }- }- }- if content, ok := event["content"].(map[string]interface{}); ok {- content["included"] = false- content["truncated"] = true- }-}--func minimalEndpointEvent(event map[string]interface{}) map[string]interface{} {- out := map[string]interface{}{"field_truncated": true}- for _, key := range []string{- "timestamp", "sequence", "vendor", "product", "schema_version", "event", "severity",- "endpoint", "user", "harness", "origin", "run", "session", "trace",- "error", "tool", "file", "command", "mcp", "approval", "policy",- "content", "destination", "health", "model", "repository",- "branch", "message",- } {- if value, ok := event[key]; ok && value != nil {- out[key] = value- }- }- if genAI, ok := event["gen_ai"].(map[string]interface{}); ok {- summary := map[string]interface{}{}- for _, key := range []string{"operation", "usage", "response", "provider"} {- if value := genAI[key]; value != nil {- summary[key] = value- }- }- if tool, ok := genAI["tool"].(map[string]interface{}); ok {- toolSummary := map[string]interface{}{"name": tool["name"], "type": tool["type"]}- if call, ok := tool["call"].(map[string]interface{}); ok {- toolSummary["call"] = map[string]interface{}{"id": call["id"]}- }- summary["tool"] = toolSummary- }- if len(summary) > 0 {- out["gen_ai"] = summary- }- }- out["message"] = truncateEndpoint(fmt.Sprint(out["message"]), 1024)- return out-}-// EndpointLogConfigured reports whether this process has a runtime log to write to. Without one,// EndpointEvent records nothing and returns nil, so a caller reporting what it recorded checks this// rather than reading a nil error as a write.@@ -754,10 +651,6 @@ func sanitizeEndpointSlice(input []interface{}) []interface{} {})}-func truncateEndpoint(value string, limit int) string {- return asymptoteobserve.TruncateString(value, limit)-}-// Keep this file-mode contract mirrored with the endpoint CLI and beaconjson exporter; the rotation// itself is asymptoteobserve.RotateRuntimeLog for all three.func appendEndpointJSONL(path string, line []byte, rotateBytes int64, rotateArchives int) error {diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.goindex d02626b1..b11a7012 100644--- a/cli/beacon/internal/endpoint/writer/writer.go+++ b/cli/beacon/internal/endpoint/writer/writer.go@@ -7,7 +7,6 @@ import ("fmt""os""path/filepath"- "reflect""runtime""testing"@@ -94,44 +93,19 @@ func AppendEvent(event schema.Event, opts Options) (string, error) {if err != nil {return "", err}- if len(data) > opts.MaxBytes {- event.Raw = nil- event.Message = asymptoteobserve.TruncateString(event.Message, 1024)- event.Truncated = true- data, err = json.Marshal(event)- if err != nil {- return "", err- }- }- if len(data) > opts.MaxBytes {- // Retained content is the next thing to go after raw, as in the hook adapter and the- // collector exporter. Each string is capped but their number is not (a multi-edit's- // arguments, a long todo list), and an error here makes a session sweep stop at the line- // and meet it again on every later sweep.- compactRetainedContent(&event)- data, err = json.Marshal(event)- if err != nil {- return "", err- }- }- if len(data) > opts.MaxBytes {- return "", fmt.Errorf("event exceeds maximum size after truncation: %d bytes", len(data))- }- // Stamped last, over the bytes that are actually about to be written, so the- // truncation fallback above cannot leave the ID describing an event that no- // longer exists. An event already at the size limit keeps its bytes and goes- // without one: the limit is a hard contract, and refusing to write an event- // over forty bytes of identity would be the worse trade.+ var stamp func(id string) ([]byte, error)if event.Event.ID == "" {- event.Event.ID = asymptoteobserve.EventIDForLine(data)- stamped, err := json.Marshal(event)- if err != nil {- return "", err- }- if len(stamped) <= opts.MaxBytes {- data = stamped+ stamp = func(id string) ([]byte, error) {+ event.Event.ID = id+ return json.Marshal(event)}}+ // An oversized event is cut down rather than refused: a session sweep that gets an error stops+ // at that line and meets it again on every later sweep.+ data, err = asymptoteobserve.FitEventLine(data, opts.MaxBytes, stamp)+ if err != nil {+ return "", err+ }line := append(data, '\n')if opts.Guard == nil && opts.Budget == nil && Ingest.Applies(opts.Path) {if err := deliver(opts, data); err != nil {@@ -184,38 +158,6 @@ func deliver(opts Options, data []byte) error {return err}-// compactRetainedContent drops an event's retained text and records that it did, so a reader can-// still tell what the event was about and that its content was held back rather than never-// captured. Mirrors the collector exporter's step of the same name.-func compactRetainedContent(event *schema.Event) {- if event.File != nil {- event.File.Diff = ""- }- if event.Command != nil {- event.Command.Output = ""- }- if event.Prompt != nil {- event.Prompt.Text = ""- }- if event.GenAI != nil {- event.GenAI.Input = nil- event.GenAI.Output = nil- if event.GenAI.Tool != nil && event.GenAI.Tool.Call != nil {- event.GenAI.Tool.Call.Arguments = nil- event.GenAI.Tool.Call.Result = nil- }- // An event whose only gen_ai content was the turn text drops the object rather than- // writing "gen_ai":{}, as the exporter does.- if reflect.ValueOf(*event.GenAI).IsZero() {- event.GenAI = nil- }- }- if event.Content != nil {- event.Content.Included = false- event.Content.Truncated = true- }-}-// EnsureSystemLogWritable prepares a machine-wide runtime log so the people whose agent sessions// this endpoint captures can append to it, and nobody else can redirect what the privileged// writers do there.diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.goindex feb43883..0911995e 100644--- a/collector-builder/exporter/beaconjsonexporter/writer.go+++ b/collector-builder/exporter/beaconjsonexporter/writer.go@@ -2,7 +2,6 @@ package beaconjsonexporterimport ("encoding/json"- "fmt""os""path/filepath"@@ -25,44 +24,17 @@ func (w jsonlWriter) append(event beaconEvent) error {if err != nil {return err}- if len(data) > w.maxEventBytes {- event.Raw = nil- event.Message = asymptoteobserve.TruncateString(event.Message, 1024)- event.Truncated = true- data, err = json.Marshal(event)- if err != nil {- return err- }- }- if len(data) > w.maxEventBytes {- // Retained content is the next thing to go after raw, mirroring the hook- // writer's compactEndpointContent. Before this path existed the only step- // past dropping raw was the hard error below, which was survivable while- // no OTLP event carried a turn's text and is not now that they do.- compactRetainedContent(&event)- data, err = json.Marshal(event)- if err != nil {- return err- }- }- if len(data) > w.maxEventBytes {- return fmt.Errorf("event exceeds maximum size after truncation: %d bytes", len(data))- }- // Stamped last, over the bytes that are actually about to be written, so a- // size fallback above cannot leave the ID describing an event that no longer- // exists. Marshalling twice is the cost of an identity derived from the- // event rather than asserted about it. An event already at the size limit- // keeps its bytes and goes without one.+ var stamp func(id string) ([]byte, error)if event.Event.ID == "" {- event.Event.ID = asymptoteobserve.EventIDForLine(data)- stamped, err := json.Marshal(event)- if err != nil {- return err- }- if len(stamped) <= w.maxEventBytes {- data = stamped+ stamp = func(id string) ([]byte, error) {+ event.Event.ID = id+ return json.Marshal(event)}}+ data, err = asymptoteobserve.FitEventLine(data, w.maxEventBytes, stamp)+ if err != nil {+ return err+ }_, dirMode, _ := asymptoteobserve.RuntimeLogModes(w.path)if err := os.MkdirAll(filepath.Dir(w.path), dirMode); err != nil {return err@@ -70,39 +42,6 @@ func (w jsonlWriter) append(event beaconEvent) error {return appendJSONL(w.path, append(data, '\n'), w.rotateBytes, w.rotateArchives)}-// compactRetainedContent drops an event's retained text and records that it did,-// so a reader can still tell what the event was about and that its content was-// held back rather than never captured. The hash and byte count in the content-// marker describe the whole redacted text and stay put.-func compactRetainedContent(event *beaconEvent) {- if event.File != nil {- event.File.Diff = ""- }- if event.Command != nil {- event.Command.Output = ""- }- if event.Prompt != nil {- event.Prompt.Text = ""- }- if event.GenAI != nil {- event.GenAI.Input = nil- event.GenAI.Output = nil- if event.GenAI.Tool != nil && event.GenAI.Tool.Call != nil {- event.GenAI.Tool.Call.Arguments = nil- event.GenAI.Tool.Call.Result = nil- }- // An event whose only gen_ai content was the turn text drops the object- // rather than writing "gen_ai":{}.- if beaconevent.IsZeroJSON(event.GenAI) {- event.GenAI = nil- }- }- if event.Content != nil {- event.Content.Included = false- event.Content.Truncated = true- }-}-func (w jsonlWriter) sanitize(event beaconEvent) beaconEvent {event.Message = w.cleanString(event.Message, asymptoteobserve.DefaultStringLimit)if event.Tool != nil {diff --git a/pkg/asymptoteobserve/eventline.go b/pkg/asymptoteobserve/eventline.gonew file mode 100644index 00000000..93076fe0--- /dev/null+++ b/pkg/asymptoteobserve/eventline.go@@ -0,0 +1,161 @@+package asymptoteobserve++import (+ "bytes"+ "encoding/json"+ "fmt"+)++// eventLineMessageLimit is what an oversized event's message is cut to.+const eventLineMessageLimit = 1024++// FitEventLine returns the line a runtime-log writer appends for one event: within maxBytes, and with+// event.id stamped when the event has none. Every writer -- the hook adapter, the CLI and the+// collector exporter -- encodes its own event type and hands the result here, so what an oversized+// event loses, and in what order, is decided once.+//+// line is the writer's encoding of the event, carrying no event.id unless the event already has one.+// stamp re-encodes the writer's event with event.id set; it is nil when the event already has an id.+// A line that fits is kept as the writer encoded it, so stamp is only called for one. An oversized+// line is decoded and cut down, each step on top of the last, until it fits:+//+// 1. raw goes, the message is cut to 1 KiB, and field_truncated is set;+// 2. retained content goes -- diffs, command output, prompt text, model input and output, tool+// arguments and results -- and the content marker says it was held back;+// 3. only the blocks that identify and order the event remain.+//+// An event still over maxBytes is an error. The id is derived from the bytes about to be written, so+// a cut can never leave it describing an event that no longer exists, and an event already at the+// limit keeps its bytes and goes without one: the limit is a hard contract, and refusing an event+// over forty bytes of identity would be the worse trade.+func FitEventLine(line []byte, maxBytes int, stamp func(id string) ([]byte, error)) ([]byte, error) {+ if len(line) <= maxBytes {+ return stampEventLine(line, maxBytes, stamp)+ }+ decoder := json.NewDecoder(bytes.NewReader(line))+ // Numbers stay as written: a sequence or token count past 2^53 must not come back rounded.+ decoder.UseNumber()+ var event map[string]interface{}+ if err := decoder.Decode(&event); err != nil {+ return nil, fmt.Errorf("decode oversized event: %w", err)+ }+ for _, cut := range []func(map[string]interface{}) map[string]interface{}{+ dropRawAndMessage, compactRetainedContent, minimalEvent,+ } {+ event = cut(event)+ data, err := json.Marshal(event)+ if err != nil {+ return nil, err+ }+ if len(data) > maxBytes {+ continue+ }+ info, ok := event["event"].(map[string]interface{})+ if stamp == nil || !ok {+ return data, nil+ }+ return stampEventLine(data, maxBytes, func(id string) ([]byte, error) {+ info["id"] = id+ return json.Marshal(event)+ })+ }+ return nil, fmt.Errorf("event exceeds %d bytes after truncation", maxBytes)+}++// stampEventLine is line with event.id set to the id derived from line, as stamp encodes it, or line+// itself when there is nothing to stamp or the stamped line would not fit.+func stampEventLine(line []byte, maxBytes int, stamp func(id string) ([]byte, error)) ([]byte, error) {+ if stamp == nil {+ return line, nil+ }+ stamped, err := stamp(EventIDForLine(line))+ if err != nil {+ return nil, err+ }+ if len(stamped) > maxBytes {+ return line, nil+ }+ return stamped, nil+}++func dropRawAndMessage(event map[string]interface{}) map[string]interface{} {+ delete(event, "raw")+ if message, ok := event["message"].(string); ok {+ event["message"] = TruncateString(message, eventLineMessageLimit)+ }+ event["field_truncated"] = true+ return event+}++// compactRetainedContent drops an event's retained text and records that it did, so a reader can+// still tell what the event was about and that its content was held back rather than never+// captured. The hash and byte count in the content marker describe the whole redacted text and stay.+func compactRetainedContent(event map[string]interface{}) map[string]interface{} {+ if file, ok := event["file"].(map[string]interface{}); ok {+ delete(file, "diff")+ }+ if command, ok := event["command"].(map[string]interface{}); ok {+ delete(command, "output")+ }+ if prompt, ok := event["prompt"].(map[string]interface{}); ok {+ delete(prompt, "text")+ }+ if genAI, ok := event["gen_ai"].(map[string]interface{}); ok {+ delete(genAI, "input")+ delete(genAI, "output")+ if tool, ok := genAI["tool"].(map[string]interface{}); ok {+ if call, ok := tool["call"].(map[string]interface{}); ok {+ delete(call, "arguments")+ delete(call, "result")+ }+ }+ // An event whose only gen_ai content was the turn text drops the object rather than+ // writing "gen_ai":{}.+ if len(genAI) == 0 {+ delete(event, "gen_ai")+ }+ }+ if content, ok := event["content"].(map[string]interface{}); ok {+ content["included"] = false+ content["truncated"] = true+ }+ return event+}++// minimalEventKeys are the top-level blocks an event keeps when nothing else fits: what it was,+// when, in what order, from whom and which session, and its provenance markers.+var minimalEventKeys = []string{+ "timestamp", "sequence", "vendor", "product", "schema_version", "event", "severity",+ "endpoint", "user", "harness", "origin", "run", "session", "trace",+ "error", "tool", "file", "command", "mcp", "approval", "policy",+ "content", "destination", "health", "model", "repository",+ "branch", "message",+}++func minimalEvent(event map[string]interface{}) map[string]interface{} {+ out := map[string]interface{}{"field_truncated": true}+ for _, key := range minimalEventKeys {+ if value, ok := event[key]; ok && value != nil {+ out[key] = value+ }+ }+ if genAI, ok := event["gen_ai"].(map[string]interface{}); ok {+ summary := map[string]interface{}{}+ for _, key := range []string{"operation", "usage", "response", "provider"} {+ if value := genAI[key]; value != nil {+ summary[key] = value+ }+ }+ if tool, ok := genAI["tool"].(map[string]interface{}); ok {+ toolSummary := map[string]interface{}{"name": tool["name"], "type": tool["type"]}+ if call, ok := tool["call"].(map[string]interface{}); ok {+ toolSummary["call"] = map[string]interface{}{"id": call["id"]}+ }+ summary["tool"] = toolSummary+ }+ if len(summary) > 0 {+ out["gen_ai"] = summary+ }+ }+ return out+}diff --git a/pkg/asymptoteobserve/eventline_test.go b/pkg/asymptoteobserve/eventline_test.gonew file mode 100644index 00000000..70d317b8--- /dev/null+++ b/pkg/asymptoteobserve/eventline_test.go@@ -0,0 +1,135 @@+package asymptoteobserve++import (+ "bytes"+ "encoding/json"+ "strings"+ "testing"+)++func decodeLine(t *testing.T, line []byte) map[string]interface{} {+ t.Helper()+ decoder := json.NewDecoder(bytes.NewReader(line))+ decoder.UseNumber()+ var event map[string]interface{}+ if err := decoder.Decode(&event); err != nil {+ t.Fatalf("line is not JSON: %v\n%s", err, line)+ }+ return event+}++func stampMap(event map[string]interface{}) func(string) ([]byte, error) {+ return func(id string) ([]byte, error) {+ event["event"].(map[string]interface{})["id"] = id+ return json.Marshal(event)+ }+}++// A line that fits is the writer's own encoding plus its id: no decode, no reordering, nothing cut.+func TestFitEventLineKeepsAFittingLineAndStampsIt(t *testing.T) {+ event := map[string]interface{}{"event": map[string]interface{}{"action": "prompt.submitted"}, "message": "hi"}+ line, _ := json.Marshal(event)++ got, err := FitEventLine(line, DefaultMaxEventBytes, stampMap(event))+ if err != nil {+ t.Fatal(err)+ }+ if id := decodeLine(t, got)["event"].(map[string]interface{})["id"]; id != EventIDForLine(line) {+ t.Fatalf("event.id = %v, want the id derived from the unstamped line", id)+ }+ if again, _ := FitEventLine(line, DefaultMaxEventBytes, nil); !bytes.Equal(again, line) {+ t.Fatalf("an event that already has an id was re-encoded:\n%s", again)+ }+}++// Retained content is the second thing to go: the event keeps what it was about, says its content+// was held back, and does not carry an emptied gen_ai object.+func TestFitEventLineDropsRetainedContentBeforeIdentity(t *testing.T) {+ long := strings.Repeat("x", 8192)+ event := map[string]interface{}{+ "event": map[string]interface{}{"action": "prompt.submitted"},+ "raw": map[string]interface{}{"body": long},+ "message": long,+ "prompt": map[string]interface{}{"text": long, "id": "prompt-1"},+ "command": map[string]interface{}{"command": "make", "output": long},+ "file": map[string]interface{}{"path": "/repo/a.go", "diff": long},+ "gen_ai": map[string]interface{}{"input": []interface{}{long}},+ "content": map[string]interface{}{"included": true, "bytes": 8192},+ }+ line, _ := json.Marshal(event)++ got, err := FitEventLine(line, 4096, stampMap(event))+ if err != nil {+ t.Fatal(err)+ }+ if len(got) > 4096 {+ t.Fatalf("line is %d bytes, over the limit", len(got))+ }+ fitted := decodeLine(t, got)+ if fitted["field_truncated"] != true || fitted["raw"] != nil {+ t.Fatalf("field_truncated/raw = %v/%v, want true/absent", fitted["field_truncated"], fitted["raw"])+ }+ prompt := fitted["prompt"].(map[string]interface{})+ if _, ok := prompt["text"]; ok || prompt["id"] != "prompt-1" {+ t.Fatalf("prompt = %v, want the text gone and the id kept", prompt)+ }+ if fitted["command"].(map[string]interface{})["command"] != "make" {+ t.Fatalf("command lost its identity: %v", fitted["command"])+ }+ if _, ok := fitted["gen_ai"]; ok {+ t.Fatalf(`an emptied gen_ai was written: %v`, fitted["gen_ai"])+ }+ content := fitted["content"].(map[string]interface{})+ if content["included"] != false || content["truncated"] != true || content["bytes"] != json.Number("8192") {+ t.Fatalf("content marker = %v, want included=false truncated=true and the original byte count", content)+ }+ if id, _ := fitted["event"].(map[string]interface{})["id"].(string); id == "" {+ t.Fatal("a fitted event went without an id")+ }+}++// The last step keeps only identifying blocks. Losing the ordering keys there would leave exactly+// the events most worth ordering unorderable, and losing a provenance marker silently turns an+// inferred event into an unmarked one, which reads as trusted.+func TestFitEventLineMinimalEventKeepsOrderingAndProvenance(t *testing.T) {+ huge := make([]interface{}, 64)+ for i := range huge {+ huge[i] = strings.Repeat("y", 1024)+ }+ line := []byte(`{"timestamp":"2026-08-21T10:00:00.000000000Z","sequence":18446744073709551615,` ++ `"event":{"action":"approval.allowed","fidelity":"inferred"},` ++ `"harness":{"name":"cursor","collection_method":"hook"},` ++ `"gen_ai":{"tool":{"name":"edit","call":{"id":"call_1"}},"usage":{"input_tokens":3}},"extra":`)+ extra, _ := json.Marshal(huge)+ line = append(append(line, extra...), '}')++ got, err := FitEventLine(line, 4096, nil)+ if err != nil {+ t.Fatal(err)+ }+ fitted := decodeLine(t, got)+ if fitted["sequence"] != json.Number("18446744073709551615") || fitted["timestamp"] != "2026-08-21T10:00:00.000000000Z" {+ t.Fatalf("ordering keys = %v/%v, want them exactly as written", fitted["sequence"], fitted["timestamp"])+ }+ if fitted["event"].(map[string]interface{})["fidelity"] != "inferred" ||+ fitted["harness"].(map[string]interface{})["collection_method"] != "hook" {+ t.Fatalf("provenance lost: event=%v harness=%v", fitted["event"], fitted["harness"])+ }+ tool := fitted["gen_ai"].(map[string]interface{})["tool"].(map[string]interface{})+ if tool["call"].(map[string]interface{})["id"] != "call_1" {+ t.Fatalf("tool call identity lost: %v", tool)+ }+ if _, ok := fitted["extra"]; ok {+ t.Fatal("a block outside the minimal set survived")+ }+}++func TestFitEventLineRefusesAnEventWhoseIdentityAloneIsTooLarge(t *testing.T) {+ line, _ := json.Marshal(map[string]interface{}{+ "event": map[string]interface{}{"action": "tool.invoked"},+ "harness": map[string]interface{}{"name": strings.Repeat("h", 2048)},+ })+ if _, err := FitEventLine(line, 512, nil); err == nil {+ t.Fatal("an event over the limit after every cut was accepted")+ }+}