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
Justin D'Souzaa3cf0e1a75ca
diff --git a/cli/beacon/internal/ingest/client.go b/cli/beacon/internal/ingest/client.goindex e94c0977..4894245c 100644--- a/cli/beacon/internal/ingest/client.go+++ b/cli/beacon/internal/ingest/client.go@@ -57,9 +57,19 @@ func (c Client) UploadBatch(ctx context.Context, token string, reqBody uploadReqif resp.StatusCode < 200 || resp.StatusCode >= 300 {return uploadResponse{}, fmt.Errorf("ingest upload failed: HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(respBody)))}+ if strings.TrimSpace(string(respBody)) == "" {+ return uploadResponse{}, fmt.Errorf("ingest upload response was empty")+ }var parsed uploadResponse- if err := json.Unmarshal(respBody, &parsed); err != nil && len(respBody) > 0 {+ if err := json.Unmarshal(respBody, &parsed); err != nil {return uploadResponse{}, fmt.Errorf("ingest upload response was invalid: %w", err)}+ if parsed.Accepted < 0 || parsed.Rejected < 0 {+ return uploadResponse{}, fmt.Errorf("ingest upload response had negative event counts")+ }+ accounted := parsed.Accepted + parsed.Rejected+ if accounted != len(reqBody.Events) {+ return uploadResponse{}, fmt.Errorf("ingest upload response accounted for %d events, want %d", accounted, len(reqBody.Events))+ }return parsed, nil}diff --git a/cli/beacon/internal/ingest/upload.go b/cli/beacon/internal/ingest/upload.goindex eca76816..142093ef 100644--- a/cli/beacon/internal/ingest/upload.go+++ b/cli/beacon/internal/ingest/upload.go@@ -39,11 +39,12 @@ func Upload(ctx context.Context, opts Options) Result {_ = opts.Store.Save(state)return Result{State: state}}+ state.LastError = ""uploaded := falsefor _, batch := range batches {- state.RejectedCount += batch.Rejectedif len(batch.Events) == 0 {+ state.RejectedCount += batch.Rejectedstate.FileOffsets[batch.Cursor.LogPath] = batch.Cursor.Offsetcontinue}@@ -59,7 +60,7 @@ func Upload(ctx context.Context, opts Options) Result {}now := time.Now().UTC().Format(time.RFC3339)state.AcceptedCount += response.Accepted- state.RejectedCount += response.Rejected+ state.RejectedCount += batch.Rejected + response.Rejectedstate.LastUploadAt = nowstate.LastEventAt = nowstate.LastCursor = batch.Cursordiff --git a/cli/beacon/internal/ingest/upload_test.go b/cli/beacon/internal/ingest/upload_test.goindex 7cca85eb..40afc0ff 100644--- a/cli/beacon/internal/ingest/upload_test.go+++ b/cli/beacon/internal/ingest/upload_test.go@@ -7,6 +7,7 @@ import ("net/http/httptest""os""path/filepath"+ "strings""testing"beaconauth "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/auth"@@ -118,6 +119,37 @@ func TestUploadPostsBatchAndPersistsCursor(t *testing.T) {}}+func TestUploadClearsStaleErrorWhenNoBatchesRemain(t *testing.T) {+ statePath := filepath.Join(t.TempDir(), "upload-state.json")+ store := Store{Path: statePath}+ if err := store.Save(State{LastError: "run beacon login before endpoint ingest upload"}); err != nil {+ t.Fatal(err)+ }++ source := &fakeSource{+ metadata: SourceMetadata{SourceID: "source-1", EndpointMode: "user", LogPath: "runtime.jsonl", ContentRetention: "redacted", ManagedMode: true},+ }+ res := Upload(context.Background(), Options{+ Settings: Settings{Enabled: true, Managed: true, SourceID: "source-1"},+ Store: store,+ Creds: &beaconauth.Credentials{Token: "token", UserID: "user"},+ Source: source,+ })++ if !source.called {+ t.Fatal("upload should check source batches")+ }+ if res.Uploaded {+ t.Fatalf("no batches should not mark upload as sent: %#v", res.State)+ }+ if res.State.LastError != "" {+ t.Fatalf("LastError = %q", res.State.LastError)+ }+ if stored := store.Load(); stored.LastError != "" {+ t.Fatalf("persisted LastError = %q", stored.LastError)+ }+}+func TestUploadDoesNotAdvanceCursorOnFailedPost(t *testing.T) {server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {http.Error(w, "nope", http.StatusInternalServerError)@@ -145,3 +177,112 @@ func TestUploadDoesNotAdvanceCursorOnFailedPost(t *testing.T) {t.Fatalf("cursor advanced on failed post: %#v", res.State)}}++func TestUploadDoesNotAdvanceCursorWhenResponseDoesNotAccountForBatch(t *testing.T) {+ tests := []struct {+ name string+ body string+ want string+ }{+ {name: "empty body", body: "", want: "response was empty"},+ {name: "zero counts", body: `{}`, want: "accounted for 0 events, want 1"},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _, _ = w.Write([]byte(tt.body))+ }))+ defer server.Close()++ source := &fakeSource{+ metadata: SourceMetadata{SourceID: "source-1", EndpointMode: "user", LogPath: "runtime.jsonl", ContentRetention: "redacted", ManagedMode: true},+ batches: []Batch{{+ Cursor: Cursor{LogPath: "runtime.jsonl", Offset: 128, Line: 1},+ Events: []json.RawMessage{json.RawMessage(`{"event":"ok"}`)},+ }},+ }+ res := Upload(context.Background(), Options{+ Settings: Settings{Enabled: true, Managed: true, SourceID: "source-1", IngestURL: server.URL},+ Store: Store{Path: filepath.Join(t.TempDir(), "upload-state.json")},+ Creds: &beaconauth.Credentials{Token: "token", UserID: "user"},+ Source: source,+ })++ if res.Uploaded {+ t.Fatalf("invalid response should not mark upload as sent: %#v", res.State)+ }+ if !strings.Contains(res.State.LastError, tt.want) {+ t.Fatalf("LastError = %q, want containing %q", res.State.LastError, tt.want)+ }+ if res.State.AcceptedCount != 0 || res.State.RejectedCount != 0 {+ t.Fatalf("counts changed on invalid response: %#v", res.State)+ }+ if res.State.FileOffsets["runtime.jsonl"] != 0 || res.State.LastCursor.Offset != 0 {+ t.Fatalf("cursor advanced on invalid response: %#v", res.State)+ }+ })+ }+}++func TestUploadDoesNotDoubleCountMalformedLinesOnFailedPostRetry(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "nope", http.StatusInternalServerError)+ }))+ defer server.Close()++ store := Store{Path: filepath.Join(t.TempDir(), "upload-state.json")}+ source := &fakeSource{+ metadata: SourceMetadata{SourceID: "source-1", EndpointMode: "user", LogPath: "runtime.jsonl", ContentRetention: "redacted", ManagedMode: true},+ batches: []Batch{{+ Cursor: Cursor{LogPath: "runtime.jsonl", Offset: 128, Line: 2},+ Events: []json.RawMessage{json.RawMessage(`{"event":"ok"}`)},+ Rejected: 1,+ }},+ }+ opts := Options{+ Settings: Settings{Enabled: true, Managed: true, SourceID: "source-1", IngestURL: server.URL},+ Store: store,+ Creds: &beaconauth.Credentials{Token: "token", UserID: "user"},+ Source: source,+ }++ first := Upload(context.Background(), opts)+ second := Upload(context.Background(), opts)++ if first.State.LastError == "" || second.State.LastError == "" {+ t.Fatalf("expected upload errors: first=%#v second=%#v", first.State, second.State)+ }+ if second.State.FileOffsets["runtime.jsonl"] != 0 || second.State.LastCursor.Offset != 0 {+ t.Fatalf("cursor advanced on failed post retry: %#v", second.State)+ }+ if second.State.RejectedCount != 0 {+ t.Fatalf("RejectedCount = %d, want 0 before cursor advances", second.State.RejectedCount)+ }+}++func TestUploadCountsMalformedLinesWhenCursorAdvancesWithoutPost(t *testing.T) {+ source := &fakeSource{+ metadata: SourceMetadata{SourceID: "source-1", EndpointMode: "user", LogPath: "runtime.jsonl", ContentRetention: "redacted", ManagedMode: true},+ batches: []Batch{{+ Cursor: Cursor{LogPath: "runtime.jsonl", Offset: 128, Line: 1},+ Rejected: 2,+ }},+ }+ res := Upload(context.Background(), Options{+ Settings: Settings{Enabled: true, Managed: true, SourceID: "source-1"},+ Store: Store{Path: filepath.Join(t.TempDir(), "upload-state.json")},+ Creds: &beaconauth.Credentials{Token: "token", UserID: "user"},+ Source: source,+ })++ if res.Uploaded {+ t.Fatalf("rejected-only batch should not mark upload as sent: %#v", res.State)+ }+ if res.State.FileOffsets["runtime.jsonl"] != 128 {+ t.Fatalf("cursor did not advance for rejected-only batch: %#v", res.State)+ }+ if res.State.RejectedCount != 2 {+ t.Fatalf("RejectedCount = %d, want 2", res.State.RejectedCount)+ }+}