Skip to content

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

fix(traces): stop huge page and context values from overflowing (#845)

Rudy Mizrahi Celekli544de26a6d66
diff --git a/cli/beacon/internal/endpoint/dashboard/history_query.go b/cli/beacon/internal/endpoint/dashboard/history_query.go
index 3e0fee6c..aba60a5b 100644
--- a/cli/beacon/internal/endpoint/dashboard/history_query.go
+++ b/cli/beacon/internal/endpoint/dashboard/history_query.go
@@ -187,9 +187,10 @@ func (s *historyStore) list(sourceID int64, query TraceQuery) (TraceListResultV1
if page <= 0 {
page = 1
}
- start := (page - 1) * limit
- if start > total {
- start = total
+ // Clamp before multiplying: a valid positive page can overflow an int.
+ start := total
+ if page-1 <= total/limit {
+ start = (page - 1) * limit
}
summaries, err := s.summaries(`SELECT t.aggregate FROM traces t WHERE `+where+` ORDER BY t.updated_ns DESC, t.trace_key ASC LIMIT ? OFFSET ?`,
append(args, limit, start)...)
diff --git a/cli/beacon/internal/endpoint/dashboard/trace_page_bounds_test.go b/cli/beacon/internal/endpoint/dashboard/trace_page_bounds_test.go
new file mode 100644
index 00000000..e8fa8436
--- /dev/null
+++ b/cli/beacon/internal/endpoint/dashboard/trace_page_bounds_test.go
@@ -0,0 +1,71 @@
+package dashboard
+
+import (
+ "encoding/json"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "net/http"
+ "net/http/httptest"
+ "strconv"
+ "testing"
+)
+
+func TestTraceListLargePageIsEmpty(t *testing.T) {
+ for _, indexed := range []bool{false, true} {
+ t.Run(strconv.FormatBool(indexed), func(t *testing.T) {
+ event := testSchemaEvent("2026-10-13T12:00:00Z", "claude_code", "prompt.submitted", "prompt", "")
+ event.Session = &schema.SessionInfo{ID: "page-control"}
+ var path string
+ if indexed {
+ path = traceStoreIndexedLog(t, marshalEvents(t, event))
+ } else {
+ path = traceStoreBlockedLog(t, marshalEvents(t, event))
+ }
+ handler, err := Handler(Options{UserMode: true, LogPath: path})
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, page := range []int{1, 2, int(^uint(0) >> 1)} {
+ rec := httptest.NewRecorder()
+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/traces?limit=10&page="+strconv.Itoa(page), nil))
+ if rec.Code != http.StatusOK {
+ t.Fatalf("page%d status%d: %s", page, rec.Code, rec.Body.String())
+ }
+ var got TraceListResultV1
+ if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
+ t.Fatal(err)
+ }
+ want := 0
+ if page == 1 {
+ want = 1
+ }
+ if got.Page != page || got.Limit != 10 || got.Returned != want || got.TotalMatched != 1 || len(got.Traces) != want || got.Truncated {
+ t.Fatalf("page%d: %+v", page, got)
+ }
+ }
+ })
+ }
+}
+
+func TestTraceWindowLargeContextDoesNotOverflow(t *testing.T) {
+ for _, indexed := range []bool{false, true} {
+ t.Run(strconv.FormatBool(indexed), func(t *testing.T) {
+ event := testSchemaEvent("2026-10-13T12:00:00Z", "claude_code", "prompt.submitted", "prompt", "")
+ event.Session = &schema.SessionInfo{ID: "window-control"}
+ var path string
+ if indexed {
+ path = traceStoreIndexedLog(t, marshalEvents(t, event))
+ } else {
+ path = traceStoreBlockedLog(t, marshalEvents(t, event))
+ }
+ for _, width := range []int{1, int(^uint(0) >> 1)} {
+ got, found, err := ShowTrace(path, "session:claude_code:window-control", TraceQuery{AroundEvent: 1, Before: width, After: width})
+ if err != nil || !found {
+ t.Fatalf("show: found%v err%v", found, err)
+ }
+ if len(got.Events) != 1 || got.Range.ReturnedEvents != 1 || got.Range.Offset != 1 {
+ t.Fatalf("width%d: %+v", width, got)
+ }
+ }
+ })
+ }
+}
diff --git a/cli/beacon/internal/endpoint/dashboard/traces.go b/cli/beacon/internal/endpoint/dashboard/traces.go
index 6f8c19ca..9750e7ee 100644
--- a/cli/beacon/internal/endpoint/dashboard/traces.go
+++ b/cli/beacon/internal/endpoint/dashboard/traces.go
@@ -198,9 +198,10 @@ func ReadTraceList(path string, query TraceQuery) (TraceListResultV1, error) {
if page <= 0 {
page = 1
}
- start := (page - 1) * limit
- if start > len(filtered) {
- start = len(filtered)
+ // Clamp before multiplying: a valid positive page can overflow an int.
+ start := len(filtered)
+ if page-1 <= len(filtered)/limit {
+ start = (page - 1) * limit
}
end := start + limit
if end > len(filtered) {
@@ -465,7 +466,14 @@ func traceRange(query TraceQuery, total int) (int, int) {
if offset < 1 {
offset = 1
}
- limit = before + 1 + after
+ // Bound each side to the available trace before adding them. Context
+ // widths are user supplied and their sum can otherwise overflow.
+ limit = 1
+ if before >= total || after >= total || before >= total-1-after {
+ limit = total
+ } else {
+ limit += before + after
+ }
}
if total == 0 {
return 1, 0