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
fix(backfill): stop suppressed replays from spending the byte budget (#838)
Rudy Mizrahi Celekli14a6c872b754
diff --git a/cli/beacon/internal/endpoint/writer/budget_test.go b/cli/beacon/internal/endpoint/writer/budget_test.goindex 5e815d97..850b23fc 100644--- a/cli/beacon/internal/endpoint/writer/budget_test.go+++ b/cli/beacon/internal/endpoint/writer/budget_test.go@@ -75,3 +75,69 @@ func TestSubBudgetIsBoundByItsParent(t *testing.T) {t.Fatalf("refused flags first=%t second=%t", first.Refused(), second.Refused())}}++func replayBudgetEvent(callID string) schema.Event {+ event := schema.NewEvent(schema.NewEventOptions{Action: "file.read", Harness: schema.HarnessInfo{Name: "claude_code"}, Message: "File read"})+ event.Timestamp = "2026-10-13T12:00:00Z"+ event.Session = &schema.SessionInfo{ID: "budget-replay"}+ event.File = &schema.FileInfo{Path: "/repo/item.go"}+ event.GenAI = &schema.GenAIInfo{Tool: &schema.GenAIToolInfo{Call: &schema.GenAIToolCallInfo{ID: callID}}}+ return event+}++func TestBudgetSuppressedReplayFitsAnExhaustedBudget(t *testing.T) {+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ event := replayBudgetEvent("call-a")+ if _, err := AppendEvent(event, Options{Path: path}); err != nil {+ t.Fatal(err)+ }+ budget := NewBudget(0)+ if _, err := AppendEvent(event, Options{Path: path, Budget: budget}); err != nil {+ t.Fatalf("zero-byte replay refused: %v", err)+ }+ if budget.Used() != 0 || budget.Refused() {+ t.Fatalf("replay changed exhausted budget: used=%d refused=%t", budget.Used(), budget.Refused())+ }+}++func TestBudgetSuppressedReplayLeavesRoomForNewTelemetry(t *testing.T) {+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ first := replayBudgetEvent("call-a")+ if _, err := AppendEvent(first, Options{Path: path}); err != nil {+ t.Fatal(err)+ }+ before, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ // A budget with exactly one event of space must survive a zero-byte replay.+ budget := NewBudget(before.Size())+ if _, err := AppendEvent(first, Options{Path: path, Budget: budget}); err != nil {+ t.Fatal(err)+ }+ if _, err := AppendEvent(replayBudgetEvent("call-b"), Options{Path: path, Budget: budget}); err != nil {+ t.Fatalf("new telemetry blocked by replay: %v", err)+ }+ after, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ if after.Size() != 2*before.Size() || budget.Used() != after.Size()-before.Size() {+ t.Fatalf("physical bytes before=%d after=%d budget=%d", before.Size(), after.Size(), budget.Used())+ }+}++func TestBudgetReleasesParentAndChildOnFailedAppend(t *testing.T) {+ path := filepath.Join(t.TempDir(), "runtime.jsonl")+ if err := os.Mkdir(path, 0700); err != nil {+ t.Fatal(err)+ }+ parent := NewBudget(1 << 20)+ child := parent.Sub(1 << 20)+ if _, err := AppendEvent(replayBudgetEvent("call-a"), Options{Path: path, Budget: child}); err == nil {+ t.Fatal("writing to a directory unexpectedly succeeded")+ }+ if child.Used() != 0 || parent.Used() != 0 {+ t.Fatalf("failed append left child=%d parent=%d charged", child.Used(), parent.Used())+ }+}diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.goindex 4faf3b58..e403be7f 100644--- a/cli/beacon/internal/endpoint/writer/writer.go+++ b/cli/beacon/internal/endpoint/writer/writer.go@@ -109,13 +109,7 @@ func AppendEvent(event schema.Event, opts Options) (string, error) {}}line := append(data, '\n')- if opts.Budget != nil && !opts.Budget.reserve(int64(len(line))) {- return "", ErrBudgetSpent- }- if err := appendJSONL(opts.Path, line, opts.RotateSize, opts.RotateArchives, opts.Guard); err != nil {- if opts.Budget != nil {- opts.Budget.release(int64(len(line)))- }+ if err := appendJSONL(opts.Path, line, opts.RotateSize, opts.RotateArchives, opts.Guard, opts.Budget); err != nil {return "", err}return opts.Path, nil@@ -217,7 +211,7 @@ func SanitizeEvent(event schema.Event, maxBytes int) schema.Event {}// Keep this rotation and file-mode contract mirrored with the hook adapter and beaconjson exporter.-func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int, guard *RetentionGuard) error {+func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int, guard *RetentionGuard, budget *Budget) (err error) {_, dirMode, _ := asymptoteobserve.RuntimeLogModes(path)if err := os.MkdirAll(filepath.Dir(path), dirMode); err != nil {return err@@ -240,6 +234,18 @@ func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives intif asymptoteobserve.IsDuplicateEndpointEvent(path, line, asymptoteobserve.EndpointDuplicateWindow) {return nil}+ // A replay adds no bytes. Reserve only after the duplicate check under the+ // existing log lock, so suppressed events cannot spend backfill capacity.+ if budget != nil {+ if !budget.reserve(int64(len(line))) {+ return ErrBudgetSpent+ }+ defer func() {+ if err != nil {+ budget.release(int64(len(line)))+ }+ }()+ }if guard != nil {// Decided under the lock, so no other Beacon writer can rotate between the check and the// rotation it permits.diff --git a/cli/beacon/internal/endpoint/writer/writer_unix_test.go b/cli/beacon/internal/endpoint/writer/writer_unix_test.goindex 60254390..11e48a2a 100644--- a/cli/beacon/internal/endpoint/writer/writer_unix_test.go+++ b/cli/beacon/internal/endpoint/writer/writer_unix_test.go@@ -92,7 +92,7 @@ func TestAppendJSONLAppendsWhenRotationIsRefused(t *testing.T) {t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })line := []byte("{\"next\":true}\n")- if err := appendJSONL(path, line, 8, DefaultRotateArchives, nil); err != nil {+ if err := appendJSONL(path, line, 8, DefaultRotateArchives, nil, nil); err != nil {t.Fatalf("append with rotation refused: %v", err)}data, err := os.ReadFile(path)