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(sync): stop every session sync before rotation discards its own output
avinash.kumar2fad25e9c7c14
diff --git a/cli/beacon/cmd/endpoint_claude.go b/cli/beacon/cmd/endpoint_claude.goindex 5af16010..107ed7ee 100644--- a/cli/beacon/cmd/endpoint_claude.go+++ b/cli/beacon/cmd/endpoint_claude.go@@ -10,7 +10,6 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/claudesession"endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer""github.com/spf13/cobra")@@ -77,16 +76,13 @@ func init() {sync.BoolVar(&endpointClaudeOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointClaudeOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointClaudeOpts.interval, "interval", time.Minute, "Sweep interval for --watch")- sync.Int64Var(&endpointClaudeOpts.rotateBytes, "rotate-bytes", 0,- "Rotate the runtime log at this many bytes while this sync writes it (default 10485760, the size every Beacon writer uses; must not be smaller)")+ addSyncRotateBytesFlag(sync, &endpointClaudeOpts.rotateBytes)}// claudeSyncOptions turns the sync flags into collector options.func claudeSyncOptions(cmd *cobra.Command) (claudesession.CollectOptions, error) {- // Smaller than the shared size would only make this sync rotate more often than every other- // writer, shrinking what the log keeps for everyone.- if endpointClaudeOpts.rotateBytes != 0 && endpointClaudeOpts.rotateBytes < writer.DefaultRotateBytes {- return claudesession.CollectOptions{}, fmt.Errorf("--rotate-bytes must be at least %d, the size every Beacon writer rotates the runtime log at", writer.DefaultRotateBytes)+ if err := checkSyncRotateBytes(endpointClaudeOpts.rotateBytes); err != nil {+ return claudesession.CollectOptions{}, err}userMode := endpointUserMode()opts := claudesession.CollectOptions{diff --git a/cli/beacon/cmd/endpoint_cline.go b/cli/beacon/cmd/endpoint_cline.goindex 7870d788..e8b11313 100644--- a/cli/beacon/cmd/endpoint_cline.go+++ b/cli/beacon/cmd/endpoint_cline.go@@ -42,12 +42,13 @@ var endpointClineStatusCmd = &cobra.Command{}var endpointClineOpts struct {- clineDir string- statePath string- logPath string- print bool- watch bool- interval time.Duration+ clineDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ rotateBytes int64}func init() {@@ -69,16 +70,21 @@ func init() {sync.BoolVar(&endpointClineOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointClineOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointClineOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointClineOpts.rotateBytes)}func runEndpointClineSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointClineOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := clinesession.CollectOptions{- ClineDir: endpointClineOpts.clineDir,- Print: endpointClineOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointClineOpts.print,- UserMode: userMode,+ RotateBytes: endpointClineOpts.rotateBytes,+ ClineDir: endpointClineOpts.clineDir,+ Print: endpointClineOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointClineOpts.print,+ UserMode: userMode,}if !endpointClineOpts.print {opts.StatePath = resolveClineStatePath(endpointClineOpts.statePath, userMode)@@ -125,6 +131,7 @@ func reportClineSweep(cmd *cobra.Command, summary clinesession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "cline sync: %d traces, %d changed, %d events, %d errors\n",summary.Traces, summary.TracesChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)}type clineStatusRef struct {diff --git a/cli/beacon/cmd/endpoint_codex.go b/cli/beacon/cmd/endpoint_codex.goindex 1466a5d6..9066ddcf 100644--- a/cli/beacon/cmd/endpoint_codex.go+++ b/cli/beacon/cmd/endpoint_codex.go@@ -41,12 +41,13 @@ var endpointCodexStatusCmd = &cobra.Command{}var endpointCodexOpts struct {- codexDir string- statePath string- logPath string- print bool- watch bool- interval time.Duration+ codexDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ rotateBytes int64}func init() {@@ -68,16 +69,21 @@ func init() {sync.BoolVar(&endpointCodexOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointCodexOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointCodexOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointCodexOpts.rotateBytes)}func runEndpointCodexSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointCodexOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := codexsession.CollectOptions{- CodexDir: endpointCodexOpts.codexDir,- Print: endpointCodexOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointCodexOpts.print,- UserMode: userMode,+ CodexDir: endpointCodexOpts.codexDir,+ RotateBytes: endpointCodexOpts.rotateBytes,+ Print: endpointCodexOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointCodexOpts.print,+ UserMode: userMode,}if !endpointCodexOpts.print {opts.StatePath = resolveCodexStatePath(endpointCodexOpts.statePath, userMode)@@ -126,6 +132,7 @@ func reportCodexSweep(cmd *cobra.Command, summary codexsession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "codex sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Codex session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_copilot.go b/cli/beacon/cmd/endpoint_copilot.goindex e795660d..31563a7c 100644--- a/cli/beacon/cmd/endpoint_copilot.go+++ b/cli/beacon/cmd/endpoint_copilot.go@@ -41,14 +41,15 @@ var endpointCopilotStatusCmd = &cobra.Command{}var endpointCopilotOpts struct {- copilotDir string- statePath string- logPath string- print bool- watch bool- interval time.Duration- workspace string- sessionID string+ copilotDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ workspace string+ sessionID string+ rotateBytes int64}func init() {@@ -72,18 +73,23 @@ func init() {sync.BoolVar(&endpointCopilotOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor")sync.BoolVar(&endpointCopilotOpts.watch, "watch", false, "Sweep continuously on --interval")sync.DurationVar(&endpointCopilotOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointCopilotOpts.rotateBytes)}func runEndpointCopilotSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointCopilotOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := copilotsession.CollectOptions{- CopilotDir: endpointCopilotOpts.copilotDir,- Print: endpointCopilotOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointCopilotOpts.print,- UserMode: userMode,- Workspace: endpointCopilotOpts.workspace,- SessionID: endpointCopilotOpts.sessionID,+ RotateBytes: endpointCopilotOpts.rotateBytes,+ CopilotDir: endpointCopilotOpts.copilotDir,+ Print: endpointCopilotOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointCopilotOpts.print,+ UserMode: userMode,+ Workspace: endpointCopilotOpts.workspace,+ SessionID: endpointCopilotOpts.sessionID,}if !endpointCopilotOpts.print {opts.StatePath = resolveCopilotStatePath(endpointCopilotOpts.statePath, userMode)@@ -130,6 +136,7 @@ func reportCopilotSweep(cmd *cobra.Command, summary copilotsession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "copilot sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in GitHub Copilot CLI session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_cursor.go b/cli/beacon/cmd/endpoint_cursor.goindex 9d881c25..b22446e3 100644--- a/cli/beacon/cmd/endpoint_cursor.go+++ b/cli/beacon/cmd/endpoint_cursor.go@@ -56,6 +56,7 @@ var endpointCursorOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -78,11 +79,16 @@ func init() {sync.BoolVar(&endpointCursorOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointCursorOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointCursorOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointCursorOpts.rotateBytes)}func runEndpointCursorSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointCursorOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := cursorsession.CollectOptions{+ RotateBytes: endpointCursorOpts.rotateBytes,GlobalDBPath: endpointCursorOpts.globalDBPath,ProjectsDir: endpointCursorOpts.projectsDir,Print: endpointCursorOpts.print,@@ -135,6 +141,7 @@ func reportCursorSweep(cmd *cobra.Command, summary cursorsession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "cursor sync: %d traces, %d changed, %d events, %d errors\n",summary.Traces, summary.TracesChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)}type cursorStatusRef struct {diff --git a/cli/beacon/cmd/endpoint_devin.go b/cli/beacon/cmd/endpoint_devin.goindex b05a437f..0bb207ac 100644--- a/cli/beacon/cmd/endpoint_devin.go+++ b/cli/beacon/cmd/endpoint_devin.go@@ -88,16 +88,13 @@ func init() {sync.BoolVar(&endpointDevinOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointDevinOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointDevinOpts.interval, "interval", time.Minute, "Sweep interval for --watch")- sync.Int64Var(&endpointDevinOpts.rotateBytes, "rotate-bytes", 0,- "Rotate the runtime log at this many bytes while this sync writes it (default 10485760, the size every Beacon writer uses; must not be smaller)")+ addSyncRotateBytesFlag(sync, &endpointDevinOpts.rotateBytes)}// devinSyncOptions turns the sync flags into collector options.func devinSyncOptions(cmd *cobra.Command) (devinsession.CollectOptions, error) {- // Smaller than the shared size would only make this sync rotate more often than every other- // writer, shrinking what the log keeps for everyone.- if endpointDevinOpts.rotateBytes != 0 && endpointDevinOpts.rotateBytes < writer.DefaultRotateBytes {- return devinsession.CollectOptions{}, fmt.Errorf("--rotate-bytes must be at least %d, the size every Beacon writer rotates the runtime log at", writer.DefaultRotateBytes)+ if err := checkSyncRotateBytes(endpointDevinOpts.rotateBytes); err != nil {+ return devinsession.CollectOptions{}, err}userMode := endpointUserMode()logPath := lifecycle.ResolveRuntimeLog(userMode, endpointDevinOpts.logPath).EffectiveLogPathdiff --git a/cli/beacon/cmd/endpoint_dsh.go b/cli/beacon/cmd/endpoint_dsh.goindex 8e1f27c1..3612b46c 100644--- a/cli/beacon/cmd/endpoint_dsh.go+++ b/cli/beacon/cmd/endpoint_dsh.go@@ -41,14 +41,15 @@ var endpointDshStatusCmd = &cobra.Command{}var endpointDshOpts struct {- dshHome string- statePath string- logPath string- print bool- watch bool- interval time.Duration- workspace string- sessionID string+ dshHome string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ workspace string+ sessionID string+ rotateBytes int64}func init() {@@ -72,18 +73,23 @@ func init() {sync.BoolVar(&endpointDshOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor")sync.BoolVar(&endpointDshOpts.watch, "watch", false, "Sweep continuously on --interval")sync.DurationVar(&endpointDshOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointDshOpts.rotateBytes)}func runEndpointDshSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointDshOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := dshsession.CollectOptions{- DSHHome: endpointDshOpts.dshHome,- Print: endpointDshOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointDshOpts.print,- UserMode: userMode,- Workspace: endpointDshOpts.workspace,- SessionID: endpointDshOpts.sessionID,+ RotateBytes: endpointDshOpts.rotateBytes,+ DSHHome: endpointDshOpts.dshHome,+ Print: endpointDshOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointDshOpts.print,+ UserMode: userMode,+ Workspace: endpointDshOpts.workspace,+ SessionID: endpointDshOpts.sessionID,}if !endpointDshOpts.print {opts.StatePath = resolveDshStatePath(endpointDshOpts.statePath, userMode)@@ -130,6 +136,7 @@ func reportDshSweep(cmd *cobra.Command, summary dshsession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "dsh sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in DeepSeek session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_factory.go b/cli/beacon/cmd/endpoint_factory.goindex 4c232828..a4085a6f 100644--- a/cli/beacon/cmd/endpoint_factory.go+++ b/cli/beacon/cmd/endpoint_factory.go@@ -48,6 +48,7 @@ var endpointFactoryOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -69,11 +70,16 @@ func init() {sync.BoolVar(&endpointFactoryOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointFactoryOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointFactoryOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointFactoryOpts.rotateBytes)}func runEndpointFactorySync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointFactoryOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := factorysession.CollectOptions{+ RotateBytes: endpointFactoryOpts.rotateBytes,SessionsDir: endpointFactoryOpts.sessionsDir,Print: endpointFactoryOpts.print,Out: cmd.OutOrStdout(),@@ -125,6 +131,7 @@ func reportFactorySweep(cmd *cobra.Command, summary factorysession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "Factory sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Factory session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_fx.go b/cli/beacon/cmd/endpoint_fx.goindex 8743aa0c..93a1a9ad 100644--- a/cli/beacon/cmd/endpoint_fx.go+++ b/cli/beacon/cmd/endpoint_fx.go@@ -58,6 +58,7 @@ var endpointFxOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -79,11 +80,16 @@ func init() {sync.BoolVar(&endpointFxOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointFxOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointFxOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointFxOpts.rotateBytes)}func runEndpointFxSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointFxOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := fxsession.CollectOptions{+ RotateBytes: endpointFxOpts.rotateBytes,SessionsDir: endpointFxOpts.sessionsDir,Print: endpointFxOpts.print,Out: cmd.OutOrStdout(),@@ -144,6 +150,7 @@ func reportFxSweep(cmd *cobra.Command, summary fxsession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "fx sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)// Said out loud rather than left in a counter: a damaged session log and an empty one produce// the same event count, and only one of them is a reason to look at the machine.if summary.MalformedLines > 0 {diff --git a/cli/beacon/cmd/endpoint_grok.go b/cli/beacon/cmd/endpoint_grok.goindex a8fe7040..c6a23b7d 100644--- a/cli/beacon/cmd/endpoint_grok.go+++ b/cli/beacon/cmd/endpoint_grok.go@@ -47,6 +47,7 @@ var endpointGrokOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -68,11 +69,16 @@ func init() {sync.BoolVar(&endpointGrokOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointGrokOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointGrokOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointGrokOpts.rotateBytes)}func runEndpointGrokSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointGrokOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := groksession.CollectOptions{+ RotateBytes: endpointGrokOpts.rotateBytes,SessionsDir: endpointGrokOpts.sessionsDir,Print: endpointGrokOpts.print,Out: cmd.OutOrStdout(),@@ -125,6 +131,7 @@ func reportGrokSweep(cmd *cobra.Command, summary groksession.SummaryResult) {}fmt.Fprintf(cmd.OutOrStdout(), "grok sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Grok session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_hermes.go b/cli/beacon/cmd/endpoint_hermes.goindex 1f4457f5..1e9ffd47 100644--- a/cli/beacon/cmd/endpoint_hermes.go+++ b/cli/beacon/cmd/endpoint_hermes.go@@ -43,12 +43,13 @@ var endpointHermesStatusCmd = &cobra.Command{}var endpointHermesOpts struct {- dbPath string- statePath string- logPath string- print bool- watch bool- interval time.Duration+ dbPath string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ rotateBytes int64}func init() {@@ -70,17 +71,22 @@ func init() {sync.BoolVar(&endpointHermesOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointHermesOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointHermesOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointHermesOpts.rotateBytes)}func runEndpointHermesSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointHermesOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()dbPath := resolveHermesDBPath(endpointHermesOpts.dbPath)opts := hermessession.CollectOptions{- DBPath: dbPath,- Print: endpointHermesOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointHermesOpts.print,- UserMode: userMode,+ RotateBytes: endpointHermesOpts.rotateBytes,+ DBPath: dbPath,+ Print: endpointHermesOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointHermesOpts.print,+ UserMode: userMode,}if !endpointHermesOpts.print {opts.StatePath = resolveHermesStatePath(endpointHermesOpts.statePath, dbPath, userMode)@@ -129,6 +135,7 @@ func reportHermesSweep(cmd *cobra.Command, summary hermessession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "Hermes sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)}type hermesSessionStatus struct {diff --git a/cli/beacon/cmd/endpoint_openclaw_sessions.go b/cli/beacon/cmd/endpoint_openclaw_sessions.goindex 0d5347cd..a60ae726 100644--- a/cli/beacon/cmd/endpoint_openclaw_sessions.go+++ b/cli/beacon/cmd/endpoint_openclaw_sessions.go@@ -50,6 +50,7 @@ var endpointOpenClawSessionsOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -71,11 +72,16 @@ func init() {sync.BoolVar(&endpointOpenClawSessionsOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointOpenClawSessionsOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointOpenClawSessionsOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointOpenClawSessionsOpts.rotateBytes)}func runEndpointOpenClawSessionsSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointOpenClawSessionsOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := openclawsession.CollectOptions{+ RotateBytes: endpointOpenClawSessionsOpts.rotateBytes,OpenClawDir: endpointOpenClawSessionsOpts.openClawDir,Print: endpointOpenClawSessionsOpts.print,Out: cmd.OutOrStdout(),@@ -127,6 +133,7 @@ func reportOpenClawSessionsSweep(cmd *cobra.Command, summary openclawsession.Sum}fmt.Fprintf(cmd.OutOrStdout(), "openclaw sessions sync: %d traces, %d changed, %d events, %d errors\n",summary.Traces, summary.TracesChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)}type openClawSessionStatusRef struct {diff --git a/cli/beacon/cmd/endpoint_opencode.go b/cli/beacon/cmd/endpoint_opencode.goindex 04468a93..6874972f 100644--- a/cli/beacon/cmd/endpoint_opencode.go+++ b/cli/beacon/cmd/endpoint_opencode.go@@ -41,12 +41,13 @@ var endpointOpenCodeStatusCmd = &cobra.Command{}var endpointOpenCodeOpts struct {- dataDir string- statePath string- logPath string- print bool- watch bool- interval time.Duration+ dataDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ rotateBytes int64}func init() {@@ -68,16 +69,21 @@ func init() {sync.BoolVar(&endpointOpenCodeOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointOpenCodeOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointOpenCodeOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointOpenCodeOpts.rotateBytes)}func runEndpointOpenCodeSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointOpenCodeOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := opencodesession.CollectOptions{- DataDir: endpointOpenCodeOpts.dataDir,- Print: endpointOpenCodeOpts.print,- Out: cmd.OutOrStdout(),- Write: !endpointOpenCodeOpts.print,- UserMode: userMode,+ RotateBytes: endpointOpenCodeOpts.rotateBytes,+ DataDir: endpointOpenCodeOpts.dataDir,+ Print: endpointOpenCodeOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointOpenCodeOpts.print,+ UserMode: userMode,}if !endpointOpenCodeOpts.print {opts.StatePath = resolveOpenCodeStatePath(endpointOpenCodeOpts.statePath, userMode)@@ -124,6 +130,7 @@ func reportOpenCodeSweep(cmd *cobra.Command, summary opencodesession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "opencode sync: %d traces, %d changed, %d events, %d errors\n",summary.Traces, summary.TracesChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)}type openCodeStatusRef struct {diff --git a/cli/beacon/cmd/endpoint_pi.go b/cli/beacon/cmd/endpoint_pi.goindex d30eb0b5..ed3959bc 100644--- a/cli/beacon/cmd/endpoint_pi.go+++ b/cli/beacon/cmd/endpoint_pi.go@@ -47,6 +47,7 @@ var endpointPiOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -68,11 +69,16 @@ func init() {sync.BoolVar(&endpointPiOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointPiOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointPiOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointPiOpts.rotateBytes)}func runEndpointPiSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointPiOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := pisession.CollectOptions{+ RotateBytes: endpointPiOpts.rotateBytes,SessionsDir: endpointPiOpts.sessionsDir,Print: endpointPiOpts.print,Out: cmd.OutOrStdout(),@@ -125,6 +131,7 @@ func reportPiSweep(cmd *cobra.Command, summary pisession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "Pi sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Pi session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/endpoint_prime.go b/cli/beacon/cmd/endpoint_prime.goindex 9cdc5fcc..de86c701 100644--- a/cli/beacon/cmd/endpoint_prime.go+++ b/cli/beacon/cmd/endpoint_prime.go@@ -48,6 +48,7 @@ var endpointPrimeOpts struct {print boolwatch boolinterval time.Duration+ rotateBytes int64}func init() {@@ -70,11 +71,16 @@ func init() {sync.BoolVar(&endpointPrimeOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")sync.BoolVar(&endpointPrimeOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")sync.DurationVar(&endpointPrimeOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ addSyncRotateBytesFlag(sync, &endpointPrimeOpts.rotateBytes)}func runEndpointPrimeSync(cmd *cobra.Command, args []string) error {+ if err := checkSyncRotateBytes(endpointPrimeOpts.rotateBytes); err != nil {+ return err+ }userMode := endpointUserMode()opts := primesession.CollectOptions{+ RotateBytes: endpointPrimeOpts.rotateBytes,SessionsDir: endpointPrimeOpts.sessionsDir,ArtifactsDir: endpointPrimeOpts.artifactsDir,Print: endpointPrimeOpts.print,@@ -128,6 +134,7 @@ func reportPrimeSweep(cmd *cobra.Command, summary primesession.Summary) {}fmt.Fprintf(cmd.OutOrStdout(), "Prime Agent sync: %d sessions, %d changed, %d events, %d errors\n",summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ reportSyncRetention(cmd.OutOrStdout(), summary.Retention)if summary.MalformedLines > 0 {fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Prime Agent session logs\n", summary.MalformedLines)}diff --git a/cli/beacon/cmd/session_sync_retention.go b/cli/beacon/cmd/session_sync_retention.gonew file mode 100644index 00000000..f2c42c1d--- /dev/null+++ b/cli/beacon/cmd/session_sync_retention.go@@ -0,0 +1,38 @@+package cmd++import (+ "fmt"+ "io"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"+ "github.com/spf13/pflag"+)++// The --rotate-bytes flag and the retention report every `beacon endpoint <runtime> sync` shares.+// Claude Code and Devin CLI report pending sessions as well and keep their own report.++func addSyncRotateBytesFlag(f *pflag.FlagSet, p *int64) {+ f.Int64Var(p, "rotate-bytes", 0,+ "Rotate the runtime log at this many bytes while this sync writes it (default 10485760, the size every Beacon writer uses; must not be smaller)")+}++// checkSyncRotateBytes refuses a size smaller than the shared one, which would only make this sync+// rotate more often than every other writer, shrinking what the log keeps for everyone.+func checkSyncRotateBytes(n int64) error {+ if n != 0 && n < writer.DefaultRotateBytes {+ return fmt.Errorf("--rotate-bytes must be at least %d, the size every Beacon writer rotates the runtime log at", writer.DefaultRotateBytes)+ }+ return nil+}++func reportSyncRetention(out io.Writer, r sessionsync.Retention) {+ if r.RetentionLimited {+ fmt.Fprint(out, " stopped before log rotation would discard this sweep's own events; the rest is left for the next sweep.\n"++ " Ship or copy the runtime log before the next sweep to keep every event, or sync in one sweep with a larger --rotate-bytes.\n")+ }+ if r.EventsRotatedOut > 0 {+ fmt.Fprintf(out, " %d event(s) this sweep wrote were rotated out of the runtime log by another writer before it finished\n",+ r.EventsRotatedOut)+ }+}diff --git a/cli/beacon/cmd/session_sync_retention_test.go b/cli/beacon/cmd/session_sync_retention_test.gonew file mode 100644index 00000000..8e518c3d--- /dev/null+++ b/cli/beacon/cmd/session_sync_retention_test.go@@ -0,0 +1,81 @@+package cmd++import (+ "bytes"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+ "github.com/spf13/cobra"+)++// Every session-store sync takes --rotate-bytes and refuses one smaller than the shared size+// before reading or writing anything (#973).+func TestEverySessionSyncRefusesARotateBytesBelowTheDefault(t *testing.T) {+ cases := []struct {+ name string+ cmd *cobra.Command+ run func(*cobra.Command, []string) error+ opt *int64+ }{+ {"claude", endpointClaudeSyncCmd, runEndpointClaudeSync, &endpointClaudeOpts.rotateBytes},+ {"devin", endpointDevinSyncCmd, runEndpointDevinSync, &endpointDevinOpts.rotateBytes},+ {"codex", endpointCodexSyncCmd, runEndpointCodexSync, &endpointCodexOpts.rotateBytes},+ {"cline", endpointClineSyncCmd, runEndpointClineSync, &endpointClineOpts.rotateBytes},+ {"copilot", endpointCopilotSyncCmd, runEndpointCopilotSync, &endpointCopilotOpts.rotateBytes},+ {"cursor", endpointCursorSyncCmd, runEndpointCursorSync, &endpointCursorOpts.rotateBytes},+ {"dsh", endpointDshSyncCmd, runEndpointDshSync, &endpointDshOpts.rotateBytes},+ {"factory", endpointFactorySyncCmd, runEndpointFactorySync, &endpointFactoryOpts.rotateBytes},+ {"fx", endpointFxSyncCmd, runEndpointFxSync, &endpointFxOpts.rotateBytes},+ {"grok", endpointGrokSyncCmd, runEndpointGrokSync, &endpointGrokOpts.rotateBytes},+ {"hermes", endpointHermesSyncCmd, runEndpointHermesSync, &endpointHermesOpts.rotateBytes},+ {"openclaw sessions", endpointOpenClawSessionsSyncCmd, runEndpointOpenClawSessionsSync, &endpointOpenClawSessionsOpts.rotateBytes},+ {"opencode", endpointOpenCodeSyncCmd, runEndpointOpenCodeSync, &endpointOpenCodeOpts.rotateBytes},+ {"pi", endpointPiSyncCmd, runEndpointPiSync, &endpointPiOpts.rotateBytes},+ {"prime", endpointPrimeSyncCmd, runEndpointPrimeSync, &endpointPrimeOpts.rotateBytes},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ home := t.TempDir()+ testenv.SetHome(t, home)+ if tc.cmd.Flags().Lookup("rotate-bytes") == nil {+ t.Fatal("sync has no --rotate-bytes flag")+ }+ *tc.opt = 1024+ t.Cleanup(func() { *tc.opt = 0 })+ var out bytes.Buffer+ tc.cmd.SetOut(&out)+ tc.cmd.SetErr(&out)+ t.Cleanup(func() { tc.cmd.SetOut(nil); tc.cmd.SetErr(nil) })++ err := tc.run(tc.cmd, nil)+ if err == nil || !strings.Contains(err.Error(), "--rotate-bytes") {+ t.Fatalf("err = %v, want a --rotate-bytes error", err)+ }+ if _, statErr := os.Stat(filepath.Join(home, ".beacon")); !os.IsNotExist(statErr) {+ t.Error("a refused --rotate-bytes still wrote endpoint state")+ }+ })+ }+}++func TestReportSyncRetention(t *testing.T) {+ var out bytes.Buffer+ reportSyncRetention(&out, sessionsync.Retention{})+ if out.Len() != 0 {+ t.Fatalf("an unlimited sweep reported %q", out.String())+ }+ reportSyncRetention(&out, sessionsync.Retention{RetentionLimited: true, EventsRotatedOut: 3})+ for _, want := range []string{"left for the next sweep", "--rotate-bytes", "3 event(s)"} {+ if !strings.Contains(out.String(), want) {+ t.Errorf("report %q lacks %q", out.String(), want)+ }+ }+ if checkSyncRotateBytes(0) != nil || checkSyncRotateBytes(writer.DefaultRotateBytes) != nil || checkSyncRotateBytes(writer.DefaultRotateBytes-1) == nil {+ t.Error("checkSyncRotateBytes accepts the wrong sizes")+ }+}diff --git a/cli/beacon/internal/clinesession/collect.go b/cli/beacon/internal/clinesession/collect.goindex c9b911af..7eb3bb81 100644--- a/cli/beacon/internal/clinesession/collect.go+++ b/cli/beacon/internal/clinesession/collect.go@@ -79,6 +79,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -86,6 +92,7 @@ type Summary struct {TracesChanged int `json:"traces_changed"`EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -110,14 +117,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectTrace(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.TracesChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -130,6 +140,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.TracesChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -215,7 +228,7 @@ func kanbanContentHash(r Record) string {var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}// recordComplete reports whether mapped[i] is the last event of its source record.diff --git a/cli/beacon/internal/clinesession/retention_test.go b/cli/beacon/internal/clinesession/retention_test.gonew file mode 100644index 00000000..b2c2f6ca--- /dev/null+++ b/cli/beacon/internal/clinesession/retention_test.go@@ -0,0 +1,28 @@+package clinesession++import (+ "fmt"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ f := newSweepFixture(t)+ for i := 0; i < sessions; i++ {+ start := int64(1700000000000 + i*60000)+ f.writeHistoryTask(t, fmt.Sprintf("task-%03d", i), []interface{}{+ userEntry(start, fmt.Sprintf("<task>Update src/file%03d.go</task>", i)),+ assistantEntry(start+1000, "Updated the file"),+ })+ }+ opts := f.options()+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/codexsession/collect.go b/cli/beacon/internal/codexsession/collect.goindex f0766a67..2f3bb355 100644--- a/cli/beacon/internal/codexsession/collect.go+++ b/cli/beacon/internal/codexsession/collect.go@@ -44,6 +44,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -53,6 +59,7 @@ type Summary struct {Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`PartialSessions int `json:"partial_sessions"`+ sessionsync.Retention}func DefaultStatePath() string {@@ -110,12 +117,15 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}()+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)var errs []error+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.+ stopErr = collectErrif changed {summary.SessionsChanged++}@@ -131,6 +141,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -183,7 +196,7 @@ func advanceCursor(cursor *Cursor, ref SessionRef, lines int) {var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}// advanceCursorPartial moves the cursor past source lines whose mapped events were all emitted, sodiff --git a/cli/beacon/internal/codexsession/retention_test.go b/cli/beacon/internal/codexsession/retention_test.gonew file mode 100644index 00000000..d1d551ce--- /dev/null+++ b/cli/beacon/internal/codexsession/retention_test.go@@ -0,0 +1,28 @@+package codexsession++import (+ "fmt"+ "path/filepath"+ "testing"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ codexDir := filepath.Join(dir, ".codex")+ now := time.Now()+ for i := 0; i < sessions; i++ {+ writeDatedSession(t, codexDir, fmt.Sprintf("s%03d", i), now.Add(-time.Duration(i+1)*time.Minute))+ }+ opts := CollectOptions{CodexDir: codexDir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/copilotsession/collect.go b/cli/beacon/internal/copilotsession/collect.goindex 10a73ddb..3a49e3b6 100644--- a/cli/beacon/internal/copilotsession/collect.go+++ b/cli/beacon/internal/copilotsession/collect.go@@ -44,6 +44,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -53,6 +59,7 @@ type Summary struct {Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`PartialSessions int `json:"partial_sessions"`+ sessionsync.Retention}func DefaultStatePath() string {@@ -116,14 +123,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -136,6 +146,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -210,7 +223,7 @@ func advanceCursorPartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}func FilterRefs(refs []SessionRef, opts CollectOptions) []SessionRef {diff --git a/cli/beacon/internal/copilotsession/retention_test.go b/cli/beacon/internal/copilotsession/retention_test.gonew file mode 100644index 00000000..0caf476c--- /dev/null+++ b/cli/beacon/internal/copilotsession/retention_test.go@@ -0,0 +1,41 @@+package copilotsession++import (+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ copilotDir := filepath.Join(dir, ".copilot")+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("s%03d", i)+ ts := 1770000000000 + int64(i)*60000+ sessionDir := filepath.Join(copilotDir, "session-state", id)+ if err := os.MkdirAll(sessionDir, 0o755); err != nil {+ t.Fatal(err)+ }+ lines := []string{+ fmt.Sprintf(`{"id":"s1","timestamp":%d,"type":"session.start","data":{"sessionId":%q,"selectedModel":"gpt-5.3-codex","context":{"cwd":"/repo"}}}`, ts, id),+ fmt.Sprintf(`{"id":"u1","timestamp":%d,"type":"user.message","data":{"content":"run tests"}}`, ts+100),+ fmt.Sprintf(`{"id":"a1","timestamp":%d,"type":"assistant.message","data":{"content":"Done.","messageId":"msg1","model":"gpt-5.3-codex","turnId":"turn1"}}`, ts+200),+ }+ if err := os.WriteFile(filepath.Join(sessionDir, EventsFile), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {+ t.Fatal(err)+ }+ }+ opts := CollectOptions{CopilotDir: copilotDir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/cursorsession/collect.go b/cli/beacon/internal/cursorsession/collect.goindex 44802e78..9f1b8da0 100644--- a/cli/beacon/internal/cursorsession/collect.go+++ b/cli/beacon/internal/cursorsession/collect.go@@ -82,6 +82,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -90,6 +96,7 @@ type Summary struct {EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`MalformedItems int `json:"malformed_items"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -118,14 +125,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.Errors++errs = append(errs, listErr)}+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectTrace(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.TracesChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -138,6 +148,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.TracesChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -202,5 +215,5 @@ func partialLastOrder(start int, mapped []MappedEvent, failedIdx int) int {var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/cursorsession/retention_test.go b/cli/beacon/internal/cursorsession/retention_test.gonew file mode 100644index 00000000..5e3bc666--- /dev/null+++ b/cli/beacon/internal/cursorsession/retention_test.go@@ -0,0 +1,41 @@+package cursorsession++import (+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ projectsDir := filepath.Join(dir, "projects")+ transcripts := filepath.Join(projectsDir, "-tmp-cursor-project", "agent-transcripts")+ if err := os.MkdirAll(transcripts, 0o755); err != nil {+ t.Fatal(err)+ }+ start := time.Date(2026, 9, 19, 20, 0, 0, 0, time.UTC)+ for i := 0; i < sessions; i++ {+ ts := start.Add(time.Duration(i) * time.Minute)+ lines := []string{+ fmt.Sprintf(`{"type":"user_message","id":"u1","text":"run the tests","timestamp":%q}`, ts.Format(time.RFC3339)),+ fmt.Sprintf(`{"type":"message","id":"a1","message":{"role":"assistant","content":[{"type":"text","text":"Tests are green"}],"model":"claude-sonnet-4"},"timestamp":%q}`, ts.Add(time.Second).Format(time.RFC3339)),+ }+ if err := os.WriteFile(filepath.Join(transcripts, fmt.Sprintf("session-%03d.jsonl", i)), []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil {+ t.Fatal(err)+ }+ }+ opts := CollectOptions{GlobalDBPath: filepath.Join(dir, "missing.vscdb"), ProjectsDir: projectsDir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/dshsession/collect.go b/cli/beacon/internal/dshsession/collect.goindex 708ce71c..a2dc6621 100644--- a/cli/beacon/internal/dshsession/collect.go+++ b/cli/beacon/internal/dshsession/collect.go@@ -47,6 +47,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -56,6 +62,7 @@ type Summary struct {Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`PartialSessions int `json:"partial_sessions"`+ sessionsync.Retention}func DefaultStatePath() string {@@ -114,14 +121,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -134,6 +144,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -219,7 +232,7 @@ func advanceCursorPartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}func filterRefs(refs []SessionRef, opts CollectOptions) []SessionRef {diff --git a/cli/beacon/internal/dshsession/retention_test.go b/cli/beacon/internal/dshsession/retention_test.gonew file mode 100644index 00000000..ac2b12be--- /dev/null+++ b/cli/beacon/internal/dshsession/retention_test.go@@ -0,0 +1,29 @@+package dshsession++import (+ "fmt"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ root := t.TempDir()+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("s%03d", i)+ writeSession(t, filepath.Join(root, "sessions", id), SessionFileJSON,+ record("session", map[string]interface{}{"id": id, "cwd": "/repo"}),+ record("user/message", map[string]interface{}{"message": map[string]interface{}{"content": "hello"}}),+ )+ }+ opts := CollectOptions{DSHHome: root, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/factorysession/collect.go b/cli/beacon/internal/factorysession/collect.goindex 410c65af..690f3579 100644--- a/cli/beacon/internal/factorysession/collect.go+++ b/cli/beacon/internal/factorysession/collect.go@@ -80,6 +80,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -89,6 +95,7 @@ type Summary struct {Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`PartialSessions int `json:"partial_sessions"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -116,14 +123,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -136,6 +146,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -241,5 +254,5 @@ func advanceCursorPartial(cursor *Cursor, ref SessionRef, mapped []MappedEvent,var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/factorysession/retention_test.go b/cli/beacon/internal/factorysession/retention_test.gonew file mode 100644index 00000000..2485ba00--- /dev/null+++ b/cli/beacon/internal/factorysession/retention_test.go@@ -0,0 +1,36 @@+package factorysession++import (+ "fmt"+ "os"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ root := filepath.Join(dir, "sessions")+ project := filepath.Join(root, "-repo")+ if err := os.MkdirAll(project, 0o755); err != nil {+ t.Fatal(err)+ }+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("session-%03d", i)+ writeFile(t, filepath.Join(project, id+".jsonl"),+ `{"type":"session_start","id":"`+id+`","title":"hi","cwd":"/repo"}`+"\n"++ `{"type":"message","id":"u1","timestamp":"2026-05-16T22:11:04.611Z","message":{"role":"user","content":[{"type":"text","text":"hello"}]}}`+"\n"++ `{"type":"message","id":"a1","timestamp":"2026-05-16T22:11:05.611Z","message":{"role":"assistant","content":[{"type":"text","text":"done"}]}}`+"\n")+ writeFile(t, filepath.Join(project, id+".settings.json"), `{"model":"claude-opus-4-7","tokenUsage":{"inputTokens":1,"outputTokens":2}}`)+ }+ opts := CollectOptions{SessionsDir: root, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/fxsession/collect.go b/cli/beacon/internal/fxsession/collect.goindex 24e79480..2fbf8292 100644--- a/cli/beacon/internal/fxsession/collect.go+++ b/cli/beacon/internal/fxsession/collect.go@@ -112,6 +112,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}// Summary reports what a sweep did. Malformed and PartialSessions are reported rather than logged@@ -123,6 +129,7 @@ type Summary struct {Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`PartialSessions int `json:"partial_sessions"`+ sessionsync.Retention}// CollectOnce performs one sweep: list fx's sessions, read the ones that moved since the last@@ -166,14 +173,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -186,6 +196,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -337,5 +350,5 @@ func generationOf(events []Event) string {}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/fxsession/retention_test.go b/cli/beacon/internal/fxsession/retention_test.gonew file mode 100644index 00000000..501534be--- /dev/null+++ b/cli/beacon/internal/fxsession/retention_test.go@@ -0,0 +1,26 @@+package fxsession++import (+ "fmt"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ f := newSweepFixture(t)+ // An assistant turn maps to many events, so some sweeps stop inside one and the next writes+ // that turn again under the same ids.+ for i := 0; i < sessions; i++ {+ f.writeSessionLog(t, fmt.Sprintf("s%03d", i), []string{sessionStartedLine(1, "/repo"), assistantTurnLine(2), usageCheckpointLine(3, 4200, 880, 0.12)})+ }+ opts := f.options()+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/groksession/collect.go b/cli/beacon/internal/groksession/collect.goindex 7b7a94fe..be51199e 100644--- a/cli/beacon/internal/groksession/collect.go+++ b/cli/beacon/internal/groksession/collect.go@@ -95,6 +95,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type SummaryResult struct {@@ -103,6 +109,7 @@ type SummaryResult struct {EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary SummaryResult, err error) {@@ -133,14 +140,17 @@ func CollectOnce(opts CollectOptions) (summary SummaryResult, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectSession(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -153,6 +163,9 @@ func CollectOnce(opts CollectOptions) (summary SummaryResult, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -254,5 +267,5 @@ func commit(cursor *Cursor, mapped []MappedEvent, written int) {}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/groksession/retention_test.go b/cli/beacon/internal/groksession/retention_test.gonew file mode 100644index 00000000..f81fe046--- /dev/null+++ b/cli/beacon/internal/groksession/retention_test.go@@ -0,0 +1,28 @@+package groksession++import (+ "fmt"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ root := filepath.Join(dir, "sessions")+ for i := 0; i < sessions; i++ {+ writeFixtureSession(t, root, "/tmp/grok project", fmt.Sprintf("session-%03d", i))+ }+ opts := CollectOptions{SessionsDir: root, Write: true, UserMode: true}+ // chat_history.jsonl records carry no time, so their events are stamped when read and their+ // ids differ between sweeps; compare them without it.+ sessionsynctest.CheckSweepKeepsItsOutputBy(t, sessionsynctest.IgnoringTimestamp, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/hermessession/collect.go b/cli/beacon/internal/hermessession/collect.goindex 176cd92e..0adbcd76 100644--- a/cli/beacon/internal/hermessession/collect.go+++ b/cli/beacon/internal/hermessession/collect.go@@ -66,6 +66,12 @@ type CollectOptions struct {// keyed by home. A sweep whose StatePath holds no sessions yet starts from it, so an upgrade does not// re-emit history that was already collected; the cursor is then saved to StatePath.LegacyStatePath string+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -73,6 +79,7 @@ type Summary struct {SessionsChanged int `json:"sessions_changed"`EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`+ sessionsync.Retention}type Session struct {@@ -213,14 +220,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, session := range sessions {changed, collectErr := collectSession(store, session, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -233,6 +243,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -315,7 +328,7 @@ func updateCursorTotals(cursor *Cursor, session Session) {}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}type Store struct {diff --git a/cli/beacon/internal/hermessession/retention_test.go b/cli/beacon/internal/hermessession/retention_test.gonew file mode 100644index 00000000..ec9cbf2b--- /dev/null+++ b/cli/beacon/internal/hermessession/retention_test.go@@ -0,0 +1,41 @@+package hermessession++import (+ "database/sql"+ "fmt"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ dbPath := filepath.Join(dir, "state.db")+ createHermesFixtureDB(t, dbPath)+ db, err := sql.Open("sqlite", dbPath)+ if err != nil {+ t.Fatal(err)+ }+ for i := 1; i < sessions; i++ {+ id := fmt.Sprintf("hermes-session-%03d", i)+ if _, err := db.Exec(`INSERT INTO sessions (id, source, model, cwd, started_at, ended_at, end_reason, message_count, tool_call_count, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, reasoning_tokens, estimated_cost_usd, title)+SELECT ?, source, model, cwd, started_at + ?, ended_at + ?, end_reason, message_count, tool_call_count, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, reasoning_tokens, estimated_cost_usd, title FROM sessions WHERE id = 'hermes-session-1'`, id, i, i); err != nil {+ t.Fatal(err)+ }+ if _, err := db.Exec(`INSERT INTO messages (session_id, role, content, tool_call_id, tool_calls, tool_name, timestamp, reasoning_content, finish_reason, active)+SELECT ?, role, content, tool_call_id, tool_calls, tool_name, timestamp + ?, reasoning_content, finish_reason, active FROM messages WHERE session_id = 'hermes-session-1' ORDER BY id`, id, i); err != nil {+ t.Fatal(err)+ }+ }+ db.Close()+ opts := CollectOptions{DBPath: dbPath, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/openclawsession/collect.go b/cli/beacon/internal/openclawsession/collect.goindex e3c51487..6a451c93 100644--- a/cli/beacon/internal/openclawsession/collect.go+++ b/cli/beacon/internal/openclawsession/collect.go@@ -79,6 +79,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -86,6 +92,7 @@ type Summary struct {TracesChanged int `json:"traces_changed"`EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -110,14 +117,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectTrace(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.TracesChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -130,6 +140,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.TracesChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -187,5 +200,5 @@ func partialLastOrder(start int, mapped []MappedEvent, failedIdx int) int {var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/openclawsession/retention_test.go b/cli/beacon/internal/openclawsession/retention_test.gonew file mode 100644index 00000000..74621928--- /dev/null+++ b/cli/beacon/internal/openclawsession/retention_test.go@@ -0,0 +1,27 @@+package openclawsession++import (+ "fmt"+ "path/filepath"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ openClawDir := filepath.Join(dir, ".openclaw")+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("s%03d", i)+ writeFile(t, filepath.Join(openClawDir, "agents", "main", "sessions", id+".jsonl"), openClawLines(id, "/repo")...)+ }+ opts := CollectOptions{OpenClawDir: openClawDir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/opencodesession/collect.go b/cli/beacon/internal/opencodesession/collect.goindex b2cbefcc..8653248b 100644--- a/cli/beacon/internal/opencodesession/collect.go+++ b/cli/beacon/internal/opencodesession/collect.go@@ -83,6 +83,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -90,6 +96,7 @@ type Summary struct {TracesChanged int `json:"traces_changed"`EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -119,6 +126,8 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {errs = append(errs, listErr)}saved := time.Now()+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {if time.Since(saved) >= stateCheckpointInterval {if err := state.Save(opts.StatePath); err == nil {@@ -126,12 +135,13 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}changed, collectErr := collectTrace(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.TracesChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -144,6 +154,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.TracesChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -211,5 +224,5 @@ func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions,}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/opencodesession/retention_test.go b/cli/beacon/internal/opencodesession/retention_test.gonew file mode 100644index 00000000..ecc7caad--- /dev/null+++ b/cli/beacon/internal/opencodesession/retention_test.go@@ -0,0 +1,38 @@+package opencodesession++import (+ "fmt"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ db := newOpenCodeDB(t, filepath.Join(dir, "opencode.db"))+ mustExec(t, db, `DELETE FROM session`)+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("ses_%03d", i)+ created := int64(1770000000000 + i*10000)+ mustExec(t, db, `INSERT INTO session (id, title, directory, time_created, time_updated) VALUES (?, 'Fix tests', '/repo', ?, ?)`, id, created, created+5000)+ for p := 0; p < 3; p++ {+ msg := fmt.Sprintf("msg_%03d_%d", i, p)+ at := created + int64(p+1)*1000+ insertJSON(t, db, `INSERT INTO message (id, session_id, time_created, data) VALUES (?, ?, ?, ?)`, msg, id, at, map[string]interface{}{"id": msg, "role": "user"})+ insertJSON(t, db, `INSERT INTO part (id, message_id, session_id, time_created, data) VALUES (?, ?, ?, ?, ?)`, "prt_"+msg, msg, id, at, map[string]interface{}{+ "id": "prt_" + msg, "type": "text", "text": fmt.Sprintf("prompt %d of %s %s", p, id, strings.Repeat("x", 200)),+ })+ }+ }+ opts := CollectOptions{DataDir: dir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/pisession/collect.go b/cli/beacon/internal/pisession/collect.goindex 3b4056e3..e9af64a0 100644--- a/cli/beacon/internal/pisession/collect.go+++ b/cli/beacon/internal/pisession/collect.go@@ -26,6 +26,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -34,6 +40,7 @@ type Summary struct {EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -61,14 +68,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectFile(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -81,6 +91,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -151,5 +164,5 @@ func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/pisession/retention_test.go b/cli/beacon/internal/pisession/retention_test.gonew file mode 100644index 00000000..1c37eca7--- /dev/null+++ b/cli/beacon/internal/pisession/retention_test.go@@ -0,0 +1,37 @@+package pisession++import (+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ sessionsDir := filepath.Join(dir, "sessions")+ if err := os.MkdirAll(sessionsDir, 0o755); err != nil {+ t.Fatal(err)+ }+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("session-%03d", i)+ lines := []string{fmt.Sprintf(`{"type":"session","id":%q,"cwd":"/work/project"}`, id)}+ for p := 0; p < 3; p++ {+ text := fmt.Sprintf("prompt %d of %s %s", p, id, strings.Repeat("x", 200))+ lines = append(lines, fmt.Sprintf(`{"type":"message","message":{"role":"user","content":[{"text":%q}]}}`, text))+ }+ writePiJSONL(t, filepath.Join(sessionsDir, id+".jsonl"), lines)+ }+ opts := CollectOptions{SessionsDir: sessionsDir, Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/primesession/collect.go b/cli/beacon/internal/primesession/collect.goindex 2ff753d4..70b9a077 100644--- a/cli/beacon/internal/primesession/collect.go+++ b/cli/beacon/internal/primesession/collect.go@@ -27,6 +27,12 @@ type CollectOptions struct {// Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event// that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.Budget *writer.Budget+ // RotateBytes is the size the runtime log rotates at while this sweep writes it; zero is+ // writer.DefaultRotateBytes.+ RotateBytes int64++ // guard keeps the sweep from rotating its own output out of the log; CollectOnce sets it.+ guard *writer.RetentionGuard}type Summary struct {@@ -35,6 +41,7 @@ type Summary struct {EventsEmitted int `json:"events_emitted"`Errors int `json:"errors"`MalformedLines int `json:"malformed_lines"`+ sessionsync.Retention}func CollectOnce(opts CollectOptions) (summary Summary, err error) {@@ -62,14 +69,17 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}()var errs []error+ opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)+ var stopErr errorfor _, ref := range refs {changed, collectErr := collectFile(store, ref, state, opts, &summary)- if errors.Is(collectErr, writer.ErrBudgetSpent) {+ if sessionsync.Stopped(collectErr) {// Not a failed session: this one and the rest are left for a later sync, with cursors// that have not moved past anything that was not written.if changed {summary.SessionsChanged++}+ stopErr = collectErrerrs = append(errs, collectErr)break}@@ -82,6 +92,9 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}+ if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {+ errs = append(errs, rotatedOut)+ }if len(errs) > 0 {return summary, errors.Join(errs...)}@@ -153,5 +166,5 @@ func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int, parent}func (o CollectOptions) sink() sessionsync.Sink {- return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, Budget: o.Budget}}+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode, RotateSize: o.RotateBytes, Budget: o.Budget, Guard: o.guard}}}diff --git a/cli/beacon/internal/primesession/retention_test.go b/cli/beacon/internal/primesession/retention_test.gonew file mode 100644index 00000000..6dc051b1--- /dev/null+++ b/cli/beacon/internal/primesession/retention_test.go@@ -0,0 +1,37 @@+package primesession++import (+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync/sessionsynctest"+)++// A sync larger than the rotation window used to rotate its own first events away while moving+// every cursor past them, and report success (#973).+func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {+ const sessions = 60+ dir := t.TempDir()+ sessionsDir := filepath.Join(dir, "sessions")+ if err := os.MkdirAll(sessionsDir, 0o755); err != nil {+ t.Fatal(err)+ }+ for i := 0; i < sessions; i++ {+ id := fmt.Sprintf("session-%03d", i)+ lines := []string{fmt.Sprintf(`{"type":"session","id":%q,"cwd":"/work/project"}`, id)}+ for p := 0; p < 3; p++ {+ text := fmt.Sprintf("prompt %d of %s %s", p, id, strings.Repeat("x", 200))+ lines = append(lines, fmt.Sprintf(`{"type":"message","message":{"role":"user","content":[{"text":%q}]}}`, text))+ }+ writePrimeJSONL(t, filepath.Join(sessionsDir, id+".jsonl"), lines)+ }+ opts := CollectOptions{SessionsDir: sessionsDir, ArtifactsDir: filepath.Join(dir, "session-artifacts"), Write: true, UserMode: true}+ sessionsynctest.CheckSweepKeepsItsOutput(t, func(statePath, logPath string, rotateBytes int64) (sessionsynctest.Result, error) {+ opts.StatePath, opts.LogPath, opts.RotateBytes = statePath, logPath, rotateBytes+ s, err := CollectOnce(opts)+ return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err+ })+}diff --git a/cli/beacon/internal/sessionsync/sessionsync.go b/cli/beacon/internal/sessionsync/sessionsync.goindex 3f0ca860..e9171824 100644--- a/cli/beacon/internal/sessionsync/sessionsync.go+++ b/cli/beacon/internal/sessionsync/sessionsync.go@@ -133,11 +133,54 @@ func (s Sink) Emit(event schema.Event) error {// GuardRetention gives a sweep that writes without printing a retention guard, so it stops with// writer.ErrRetentionWindowFull rather than rotate its own output out of the log.func (s *Sink) GuardRetention() {- if s.Write && !s.Print {- s.Log.Guard = &writer.RetentionGuard{}+ if g := NewRetentionGuard(s.Print, s.Write); g != nil {+ s.Log.Guard = g}}+// NewRetentionGuard is the guard for one sweep that writes without printing, or nil for one that+// prints or does not write. A collector whose sink is built per event keeps it in its options for+// the whole sweep, since the guard tracks every file the sweep's appends landed in.+func NewRetentionGuard(print, write bool) *writer.RetentionGuard {+ if write && !print {+ return &writer.RetentionGuard{}+ }+ return nil+}++// Stopped reports whether err ends a sweep early without failing it: the budget ran out, or the+// retention guard refused an append that would have rotated the sweep's own output away. Either+// way nothing was written for the refused event, so a collector whose cursors move only past what+// was emitted leaves the rest for the next sweep.+func Stopped(err error) bool {+ return errors.Is(err, writer.ErrBudgetSpent) || errors.Is(err, writer.ErrRetentionWindowFull)+}++// Retention is the part of a sweep's summary that says how much of what it wrote the runtime log+// still holds. Collectors embed it, so the fields sit beside their own in --json output.+type Retention struct {+ // EventsRetained is how many of the events this sweep wrote are still in the runtime log or+ // its archives when it finishes. Events the writer suppressed as duplicates are in neither.+ EventsRetained int `json:"events_retained"`+ // EventsRotatedOut is how many events this sweep wrote that another writer's rotations+ // discarded before it finished. The sweep never rotates out its own output.+ EventsRotatedOut int `json:"events_rotated_out"`+ // RetentionLimited is set when the sweep stopped because writing more would have rotated its+ // own output out of the log. The sessions it had not finished keep their cursors.+ RetentionLimited bool `json:"retention_limited"`+}++// Finish records how the guarded sweep through s ended. stopErr is the error that stopped it+// early, if any. It returns an error when another writer rotated out some of what it wrote.+func (r *Retention) Finish(s Sink, stopErr error) error {+ if errors.Is(stopErr, writer.ErrRetentionWindowFull) {+ r.RetentionLimited = true+ }+ var err error+ r.EventsRetained, r.EventsRotatedOut, err = s.Retention()+ return err+}+// Retention reports how many of the events the guarded sweep wrote are still in the runtime log or// its archives, and how many another writer's rotations discarded, with an error when any were.// Without a guard it reports nothing.diff --git a/cli/beacon/internal/sessionsync/sessionsync_test.go b/cli/beacon/internal/sessionsync/sessionsync_test.goindex 4e1f0c33..c678d4eb 100644--- a/cli/beacon/internal/sessionsync/sessionsync_test.go+++ b/cli/beacon/internal/sessionsync/sessionsync_test.go@@ -165,3 +165,45 @@ func TestSinkStopsOnBudgetAndRetention(t *testing.T) {t.Fatalf("Retention() without a guard = %d, %d, %v", retained, rotatedOut, err)}}++func TestStoppedAndRetentionFinish(t *testing.T) {+ if !Stopped(writer.ErrBudgetSpent) || !Stopped(errors.Join(errors.New("other"), writer.ErrRetentionWindowFull)) {+ t.Fatal("a spent budget or a full window does not stop the sweep")+ }+ if Stopped(nil) || Stopped(errors.New("read failed")) {+ t.Fatal("an ordinary error stops the sweep")+ }+ if NewRetentionGuard(true, true) != nil || NewRetentionGuard(false, false) != nil || NewRetentionGuard(false, true) == nil {+ t.Fatal("only a sweep that writes without printing is guarded")+ }++ // A guard shared by sinks built per event, as collectors do, stops before rotating out the+ // sweep's first event and reports everything it wrote as retained.+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ guard := NewRetentionGuard(false, true)+ sink := func() Sink {+ return Sink{Write: true, Log: writer.Options{Path: logPath, UserMode: true, RotateSize: 512, RotateArchives: 1, Guard: guard}}+ }+ var stopErr error+ written := 0+ for i := 0; i < 100 && stopErr == nil; i++ {+ if stopErr = sink().Emit(testEvent(strings.Repeat("x", 100))); stopErr == nil {+ written+++ }+ }+ if !errors.Is(stopErr, writer.ErrRetentionWindowFull) {+ t.Fatalf("sweep ended with %v, want ErrRetentionWindowFull", stopErr)+ }+ var r Retention+ if err := r.Finish(sink(), stopErr); err != nil {+ t.Fatal(err)+ }+ if !r.RetentionLimited || r.EventsRetained != written || r.EventsRotatedOut != 0 || written == 0 {+ t.Fatalf("Retention = %+v after writing %d", r, written)+ }++ var budget Retention+ if err := budget.Finish(Sink{}, writer.ErrBudgetSpent); err != nil || budget.RetentionLimited {+ t.Fatalf("a spent budget finished as %+v, %v", budget, err)+ }+}diff --git a/cli/beacon/internal/sessionsync/sessionsynctest/sessionsynctest.go b/cli/beacon/internal/sessionsync/sessionsynctest/sessionsynctest.gonew file mode 100644index 00000000..c837bc7d--- /dev/null+++ b/cli/beacon/internal/sessionsync/sessionsynctest/sessionsynctest.go@@ -0,0 +1,197 @@+// Package sessionsynctest checks that a session collector's sweep keeps what it writes: a sweep+// larger than the runtime log's rotation window stops before rotating its own output away, and+// repeated sweeps, with the log shipped in between, collect exactly the events one sweep with room+// for everything collects (#619, #973).+package sessionsynctest++import (+ "bufio"+ "encoding/json"+ "errors"+ "os"+ "path/filepath"+ "sort"+ "strconv"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"+)++// SmallWindow is a rotation size a few dozen fixture sessions overflow many times over.+const SmallWindow = 4096++// roomyWindow holds any fixture in one sweep.+const roomyWindow = 1 << 30++// Result is what one sweep reported.+type Result struct {+ EventsEmitted int+ Errors int+ sessionsync.Retention+}++// Sweep runs one write sweep over the fixture store with its cursor file at statePath, appending to+// logPath with the runtime log rotating at rotateBytes.+type Sweep func(statePath, logPath string, rotateBytes int64) (Result, error)++// RetainedEvents reads every file the rotation contract keeps for logPath.+func RetainedEvents(t *testing.T, logPath string) []schema.Event {+ t.Helper()+ var out []schema.Event+ for _, p := range writer.RetainedLogPaths(logPath) {+ f, err := os.Open(p)+ if errors.Is(err, os.ErrNotExist) {+ continue+ }+ if err != nil {+ t.Fatal(err)+ }+ scanner := bufio.NewScanner(f)+ scanner.Buffer(make([]byte, 1<<20), 1<<24)+ for scanner.Scan() {+ if len(scanner.Bytes()) == 0 {+ continue+ }+ var ev schema.Event+ if err := json.Unmarshal(scanner.Bytes(), &ev); err != nil {+ f.Close()+ t.Fatalf("decode %s: %v", p, err)+ }+ out = append(out, ev)+ }+ f.Close()+ if err := scanner.Err(); err != nil {+ t.Fatal(err)+ }+ }+ return out+}++// shipAway moves the retained log files aside, standing in for a forwarder that has shipped them,+// and returns what they held.+func shipAway(t *testing.T, logPath string, batch int) []schema.Event {+ t.Helper()+ events := RetainedEvents(t, logPath)+ for _, p := range writer.RetainedLogPaths(logPath) {+ if err := os.Rename(p, p+".shipped"+strconv.Itoa(batch)); err != nil && !os.IsNotExist(err) {+ t.Fatal(err)+ }+ }+ return events+}++// CheckSweepKeepsItsOutput runs sweep over a fixture store too large for SmallWindow and checks+// that:+// - the first sweep at SmallWindow stops with writer.ErrRetentionWindowFull, reports it, counts+// no session as failed, and every event it reports emitted is still on disk;+// - repeated sweeps at SmallWindow, with the log shipped away in between, end up with exactly the+// event ids one sweep with room for everything writes. A sweep that stops inside a source+// record may write that record's events again on the next one, under the same deterministic+// ids the forwarder and ingest deduplicate on; an event lost, or written again under a+// different id, fails.+func CheckSweepKeepsItsOutput(t *testing.T, sweep Sweep) {+ t.Helper()+ CheckSweepKeepsItsOutputBy(t, EventID, sweep)+}++// EventID identifies an event by its deterministic event.id.+func EventID(ev schema.Event) string { return ev.Event.ID }++// IgnoringTimestamp identifies an event by everything but its timestamp and the id derived from+// it, for a collector whose records carry no time of their own, so each sweep stamps its events+// with the time it read them.+func IgnoringTimestamp(ev schema.Event) string {+ ev.Timestamp = ""+ ev.Event.ID = ""+ data, err := json.Marshal(ev)+ if err != nil {+ return ""+ }+ return string(data)+}++// CheckSweepKeepsItsOutputBy is CheckSweepKeepsItsOutput comparing events by key.+func CheckSweepKeepsItsOutputBy(t *testing.T, key func(schema.Event) string, sweep Sweep) {+ t.Helper()+ dir := t.TempDir()++ refLog := filepath.Join(dir, "reference", "runtime.jsonl")+ ref, err := sweep(filepath.Join(dir, "reference", "state.json"), refLog, roomyWindow)+ if err != nil {+ t.Fatalf("reference sweep: %v", err)+ }+ want := map[string]bool{}+ for _, ev := range RetainedEvents(t, refLog) {+ want[key(ev)] = true+ }+ if ref.RetentionLimited || len(want) == 0 || len(want) != ref.EventsEmitted {+ t.Fatalf("reference sweep: emitted=%d distinct events=%d limited=%v", ref.EventsEmitted, len(want), ref.RetentionLimited)+ }++ statePath := filepath.Join(dir, "limited", "state.json")+ logPath := filepath.Join(dir, "limited", "runtime.jsonl")+ got := map[string]bool{}+ limited := 0+ for batch := 0; batch < 200; batch++ {+ result, err := sweep(statePath, logPath, SmallWindow)+ if err != nil && !errors.Is(err, writer.ErrRetentionWindowFull) {+ t.Fatalf("sweep %d: %v", batch, err)+ }+ if result.RetentionLimited != errors.Is(err, writer.ErrRetentionWindowFull) {+ t.Fatalf("sweep %d: RetentionLimited = %v with err %v", batch, result.RetentionLimited, err)+ }+ if result.Errors != 0 {+ t.Fatalf("sweep %d: Errors = %d, want 0: stopping at the window is not a failed session", batch, result.Errors)+ }+ if result.EventsRotatedOut != 0 {+ t.Fatalf("sweep %d: EventsRotatedOut = %d, want 0", batch, result.EventsRotatedOut)+ }+ shipped := shipAway(t, logPath, batch)+ if result.EventsRetained != result.EventsEmitted || len(shipped) != result.EventsEmitted {+ t.Fatalf("sweep %d: emitted=%d retained=%d on disk=%d, want all equal",+ batch, result.EventsEmitted, result.EventsRetained, len(shipped))+ }+ for _, ev := range shipped {+ got[key(ev)] = true+ }+ if batch == 0 && !result.RetentionLimited {+ t.Fatal("the first sweep did not fill the window; the fixture proves nothing")+ }+ if !result.RetentionLimited {+ break+ }+ limited+++ if result.EventsEmitted == 0 {+ t.Fatalf("sweep %d stopped at the window without writing anything; it can never finish", batch)+ }+ }+ if limited == 0 {+ t.Fatal("no sweep was retention limited")+ }+ var missing, extra []string+ for id := range want {+ if !got[id] {+ missing = append(missing, id)+ }+ }+ for id := range got {+ if !want[id] {+ extra = append(extra, id)+ }+ }+ sort.Strings(missing)+ sort.Strings(extra)+ if len(missing) > 0 || len(extra) > 0 {+ t.Fatalf("%d limited sweeps collected %d distinct events, one sweep %d: %d missing %v, %d under ids one sweep never writes %v",+ limited+1, len(got), len(want), len(missing), head(missing), len(extra), head(extra))+ }+}++func head(ids []string) []string {+ if len(ids) > 5 {+ return ids[:5]+ }+ return ids+}diff --git a/docs/cli/endpoint-copilot.mdx b/docs/cli/endpoint-copilot.mdxindex 41eb51ba..63982659 100644--- a/docs/cli/endpoint-copilot.mdx+++ b/docs/cli/endpoint-copilot.mdx@@ -46,6 +46,7 @@ Both subcommands accept:| `--print` | Print mapped events as JSON without writing them or advancing the cursor || `--watch` | Sweep continuously on `--interval` instead of sweeping once and exiting || `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller. See [large backfills and log rotation](/runtimes/claude-code#large-backfills-and-log-rotation) |## Sweepingdiff --git a/docs/cli/endpoint-dsh.mdx b/docs/cli/endpoint-dsh.mdxindex fb0d0877..3606c12f 100644--- a/docs/cli/endpoint-dsh.mdx+++ b/docs/cli/endpoint-dsh.mdx@@ -54,6 +54,7 @@ Both subcommands accept:| `--print` | Print mapped events without writing them or advancing the cursor || `--watch` | Sweep continuously on `--interval` || `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller. See [large backfills and log rotation](/runtimes/claude-code#large-backfills-and-log-rotation) |## Sweepingdiff --git a/docs/cli/endpoint-fx.mdx b/docs/cli/endpoint-fx.mdxindex 8355b7b6..0dc324a6 100644--- a/docs/cli/endpoint-fx.mdx+++ b/docs/cli/endpoint-fx.mdx@@ -44,6 +44,7 @@ Both subcommands accept:| `--print` | Print mapped events as JSON without writing them or advancing the cursor || `--watch` | Sweep continuously on `--interval` instead of sweeping once and exiting || `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller. See [large backfills and log rotation](/runtimes/claude-code#large-backfills-and-log-rotation) |## Sweepingdiff --git a/docs/cli/endpoint-opencode.mdx b/docs/cli/endpoint-opencode.mdxindex 7d4df118..38ebf31b 100644--- a/docs/cli/endpoint-opencode.mdx+++ b/docs/cli/endpoint-opencode.mdx@@ -44,6 +44,7 @@ Both subcommands accept:| `--print` | Print mapped events as JSON without writing them or advancing the cursor || `--watch` | Sweep continuously on `--interval` instead of sweeping once and exiting || `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller. See [large backfills and log rotation](/runtimes/claude-code#large-backfills-and-log-rotation) |## Sweepingdiff --git a/docs/cli/endpoint-pi.mdx b/docs/cli/endpoint-pi.mdxindex 7951afa0..b89c48ee 100644--- a/docs/cli/endpoint-pi.mdx+++ b/docs/cli/endpoint-pi.mdx@@ -44,6 +44,7 @@ Both subcommands accept:| `--print` | Print mapped events as JSON without writing them or advancing the cursor || `--watch` | Sweep continuously on `--interval` instead of sweeping once and exiting || `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller. See [large backfills and log rotation](/runtimes/claude-code#large-backfills-and-log-rotation) |## Sweepingdiff --git a/docs/runtimes/claude-code.mdx b/docs/runtimes/claude-code.mdxindex d31b7c78..c0b585cd 100644--- a/docs/runtimes/claude-code.mdx+++ b/docs/runtimes/claude-code.mdx@@ -207,6 +207,8 @@ claude sync: 3496 sessions, 168 changed, 33689 events (33689 retained), 0 errorsstopped before log rotation would discard this sweep's own events; 3329 session(s) left pending for the next sweep.```+Every `beacon endpoint <runtime> sync` that reads a session store (Codex, Copilot, Cursor, Cline, OpenCode, Pi, Prime Agent, Factory Droid, Grok Build, Hermes Agent, OpenClaw, DeepSeek Harness and fx as well as Claude Code and Devin CLI) stops the same way and takes the same `--rotate-bytes`. Their summaries carry `events_retained`, `events_rotated_out` and `retention_limited`; only Claude Code and Devin CLI also count `sessions_pending`.+With `--json`, the summary carries `events_retained` (events this sweep wrote that are still in the log or its archives when it finishes), `events_rotated_out` (events another writer's rotations discarded during the sweep, which also makes the sweep exit non-zero), `retention_limited`, and `sessions_pending`.The next sweep continues where this one stopped. The guard covers one sweep at a time, so the next sweep's rotations can discard the previous sweep's output. That includes `--watch`, which sweeps again on every interval. If you need every event, ship or copy the log files between sweeps. To backfill in one sweep, raise the file size for this sync only: