Skip to content

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

Keep history settings and file identities in their stored width

shukieshah17bf2236f15b
diff --git a/cli/beacon/cmd/endpoint_traces_history.go b/cli/beacon/cmd/endpoint_traces_history.go
index 8c21d812..564af4a4 100644
--- a/cli/beacon/cmd/endpoint_traces_history.go
+++ b/cli/beacon/cmd/endpoint_traces_history.go
@@ -134,7 +134,16 @@ var endpointTracesResetCmd = &cobra.Command{
},
}
+// maxHistorySizeMB bounds --max-size-mb well above any real disk, so the byte count cannot overflow.
+const maxHistorySizeMB = 1 << 30
+
func runEndpointTracesReindex(cmd *cobra.Command, args []string) error {
+ if endpointTraceHistoryOpts.retentionDays < 0 {
+ return errors.New("--retention-days must be zero (keep the current setting) or more")
+ }
+ if endpointTraceHistoryOpts.maxSizeMB < 0 || endpointTraceHistoryOpts.maxSizeMB > maxHistorySizeMB {
+ return fmt.Errorf("--max-size-mb must be between 0 (keep the current setting) and %d", maxHistorySizeMB)
+ }
logPath := endpointTraceLogPath()
opts := dashboard.HistoryOptions{
RetentionDays: endpointTraceHistoryOpts.retentionDays,
diff --git a/cli/beacon/cmd/endpoint_traces_history_test.go b/cli/beacon/cmd/endpoint_traces_history_test.go
index d9b271da..15a982e8 100644
--- a/cli/beacon/cmd/endpoint_traces_history_test.go
+++ b/cli/beacon/cmd/endpoint_traces_history_test.go
@@ -136,3 +136,18 @@ func TestTraceHistoryResetNeedsConfirmation(t *testing.T) {
t.Fatalf("history still present after reset --yes: %v", err)
}
}
+
+func TestTraceHistoryReindexRejectsOutOfRangeLimits(t *testing.T) {
+ traceHistoryFixture(t, false, "")
+ for _, set := range []func(){
+ func() { endpointTraceHistoryOpts.retentionDays = -1 },
+ func() { endpointTraceHistoryOpts.maxSizeMB = -1 },
+ func() { endpointTraceHistoryOpts.maxSizeMB = maxHistorySizeMB + 1 },
+ } {
+ endpointTraceHistoryOpts.retentionDays, endpointTraceHistoryOpts.maxSizeMB = 0, 0
+ set()
+ if err := runEndpointTracesReindex(&cobra.Command{}, nil); err == nil {
+ t.Fatalf("reindex accepted retention %d, size %d", endpointTraceHistoryOpts.retentionDays, endpointTraceHistoryOpts.maxSizeMB)
+ }
+ }
+}
diff --git a/cli/beacon/internal/endpoint/dashboard/history_ingest.go b/cli/beacon/internal/endpoint/dashboard/history_ingest.go
index 9654f747..8080925f 100644
--- a/cli/beacon/internal/endpoint/dashboard/history_ingest.go
+++ b/cli/beacon/internal/endpoint/dashboard/history_ingest.go
@@ -41,6 +41,10 @@ func (lf logFile) key() string {
}
type fileCheckpoint struct {
+ // dev and ino are as the files table stores them, so the row can be addressed again.
+ dev int64
+ ino int64
+ head string
offset int64
lines int64
size int64
@@ -150,13 +154,11 @@ func (s *historyStore) loadCheckpoints(q queryer, sourceID int64) (map[string]fi
defer rows.Close()
out := map[string]fileCheckpoint{}
for rows.Next() {
- var dev, ino int64
- var head string
var cp fileCheckpoint
- if err := rows.Scan(&dev, &ino, &head, &cp.offset, &cp.lines, &cp.size, &cp.archive); err != nil {
+ if err := rows.Scan(&cp.dev, &cp.ino, &cp.head, &cp.offset, &cp.lines, &cp.size, &cp.archive); err != nil {
return nil, err
}
- out[fmt.Sprintf("%d:%d:%s", uint64(dev), uint64(ino), head)] = cp
+ out[fmt.Sprintf("%d:%d:%s", uint64(cp.dev), uint64(cp.ino), cp.head)] = cp
}
return out, rows.Err()
}
@@ -301,11 +303,7 @@ func (s *historyStore) catchUp(logPath string, progress func(done, total int64),
if cp.archive == 0 || cp.size > cp.offset {
gaps++
}
- dev, ino, head, err := splitFileKey(key)
- if err != nil {
- return 0, err
- }
- if _, err := tx.Exec(`DELETE FROM files WHERE source_id = ? AND dev = ? AND ino = ? AND head = ?`, sourceID, dev, ino, head); err != nil {
+ if _, err := tx.Exec(`DELETE FROM files WHERE source_id = ? AND dev = ? AND ino = ? AND head = ?`, sourceID, cp.dev, cp.ino, cp.head); err != nil {
return 0, err
}
}
@@ -340,22 +338,6 @@ func (s *historyStore) catchUp(logPath string, progress func(done, total int64),
return sourceID, nil
}
-func splitFileKey(key string) (int64, int64, string, error) {
- parts := strings.SplitN(key, ":", 3)
- if len(parts) != 3 {
- return 0, 0, "", fmt.Errorf("malformed file key %q", key)
- }
- dev, err := strconv.ParseUint(parts[0], 10, 64)
- if err != nil {
- return 0, 0, "", err
- }
- ino, err := strconv.ParseUint(parts[1], 10, 64)
- if err != nil {
- return 0, 0, "", err
- }
- return int64(dev), int64(ino), parts[2], nil
-}
-
// historyCheckpointEvery is how often, in events, a trace's running aggregate is saved. An event
// that arrives after later ones -- an OpenTelemetry batch lands seconds after the hook events it
// precedes -- belongs earlier in its trace, and the summary is refolded from the last checkpoint
diff --git a/cli/beacon/internal/endpoint/dashboard/history_store.go b/cli/beacon/internal/endpoint/dashboard/history_store.go
index dcec1dd5..a94f028a 100644
--- a/cli/beacon/internal/endpoint/dashboard/history_store.go
+++ b/cli/beacon/internal/endpoint/dashboard/history_store.go
@@ -75,7 +75,7 @@ type HistoryStatus struct {
NewestEventAt string `json:"newest_event_at,omitempty"`
Gaps int `json:"gaps,omitempty"`
GapBytes int64 `json:"gap_bytes,omitempty"`
- RetentionDays int `json:"retention_days,omitempty"`
+ RetentionDays int64 `json:"retention_days,omitempty"`
MaxBytes int64 `json:"max_bytes,omitempty"`
}
@@ -445,7 +445,7 @@ func (s *historyStore) status(sourceID int64) (HistoryStatus, error) {
Enabled: true,
SizeBytes: fileSize(s.path),
WALBytes: fileSize(s.path + "-wal"),
- RetentionDays: int(s.metaInt("retention_days", defaultHistoryRetentionDays)),
+ RetentionDays: s.metaInt("retention_days", defaultHistoryRetentionDays),
MaxBytes: s.metaInt("max_bytes", defaultHistoryMaxBytes),
}
var oldest, newest sql.NullInt64