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
refactor(sessionsync): share the session collectors' sync machinery (#978)
claude[bot]3e1b32f5fa37
diff --git a/cli/beacon/internal/claudesession/collect.go b/cli/beacon/internal/claudesession/collect.goindex 4b9ea88a..b204311b 100644--- a/cli/beacon/internal/claudesession/collect.go+++ b/cli/beacon/internal/claudesession/collect.go@@ -1,7 +1,6 @@package claudesessionimport (- "encoding/json""errors""fmt""io"@@ -10,8 +9,8 @@ import ("strings""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -80,47 +79,16 @@ func DefaultStatePath() string {return filepath.Join(base, "endpoint", "state", "claude.json")}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Files: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Claude collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Files == nil {- return state, nil- }- state.Files = stored.Files- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Claude",+ New: func() *State { return &State{Version: StateVersion, Files: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Files != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(path string) *Cursor {if s.Files == nil {@@ -158,14 +126,12 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}()- var guard *writer.RetentionGuard- if opts.Write && !opts.Print {- guard = &writer.RetentionGuard{}- }+ sink := opts.sink()+ sink.GuardRetention()var errs []errorresponses := responseLedger{}for i, ref := range refs {- changed, collectErr := collectSession(store, ref, state, opts, guard, responses, &summary)+ changed, collectErr := collectSession(store, ref, state, opts, sink, responses, &summary)if errors.Is(collectErr, writer.ErrRetentionWindowFull) {// Not a failed session: this one and the rest are simply left for the next sweep,// with cursors that have not moved past anything that was not written.@@ -196,13 +162,10 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}- if guard != nil {- summary.EventsRetained = guard.Retained()- summary.EventsRotatedOut = guard.Written() - summary.EventsRetained- if summary.EventsRotatedOut > 0 {- errs = append(errs, fmt.Errorf("%d of the %d events this sweep wrote were rotated out of the runtime log by another writer before it finished",- summary.EventsRotatedOut, guard.Written()))- }+ var rotatedOut error+ summary.EventsRetained, summary.EventsRotatedOut, rotatedOut = sink.Retention()+ if rotatedOut != nil {+ errs = append(errs, rotatedOut)}if len(errs) > 0 {return summary, errors.Join(errs...)@@ -223,7 +186,7 @@ func countChanged(refs []SessionRef, state *State) int {return n}-func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptions, guard *writer.RetentionGuard, responses responseLedger, summary *Summary) (bool, error) {+func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptions, sink sessionsync.Sink, responses responseLedger, summary *Summary) (bool, error) {cursor := state.cursor(ref.Path)if ref.SizeBytes < cursor.SizeBytes {cursor.LastLine = 0@@ -246,7 +209,7 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptireturn false, nil}for i, item := range mapped {- if err := emit(item.Event, opts, guard); err != nil {+ if err := sink.Emit(item.Event); err != nil {advanceCursorPartial(cursor, mapped, i)return true, err}@@ -296,17 +259,6 @@ func advanceCursorPartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {cursor.LastLine = lastCompleteLine}-func emit(event schema.Event, opts CollectOptions, guard *writer.RetentionGuard) error {- if opts.Print {- out := opts.Out- if out == nil {- out = io.Discard- }- return json.NewEncoder(out).Encode(event)- }- if !opts.Write {- return nil- }- _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, RotateSize: opts.RotateBytes, Guard: guard, Budget: opts.Budget})- return err+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, RotateSize: o.RotateBytes, Budget: o.Budget}}}diff --git a/cli/beacon/internal/clinesession/collect.go b/cli/beacon/internal/clinesession/collect.goindex 7b68f513..c9b911af 100644--- a/cli/beacon/internal/clinesession/collect.go+++ b/cli/beacon/internal/clinesession/collect.go@@ -3,7 +3,6 @@ package clinesessionimport ("crypto/sha1""encoding/hex"- "encoding/json""errors""fmt""io"@@ -14,6 +13,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -31,46 +31,17 @@ type State struct {Traces map[string]*Cursor `json:"traces"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Traces: map[string]*Cursor{}}- if strings.TrimSpace(path) == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Cline collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Traces == nil {- return state, nil- }- return &stored, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Cline",+ New: func() *State { return &State{Version: StateVersion, Traces: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Traces != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+ BlankPath: true,}-func (s *State) Save(path string) error {- if strings.TrimSpace(path) == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(ref TraceRef) *Cursor {if s.Traces == nil {@@ -240,25 +211,11 @@ func kanbanContentHash(r Record) string {return hex.EncodeToString(h[:])}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}// recordComplete reports whether mapped[i] is the last event of its source record.diff --git a/cli/beacon/internal/codexsession/collect.go b/cli/beacon/internal/codexsession/collect.goindex e1aa8687..f0766a67 100644--- a/cli/beacon/internal/codexsession/collect.go+++ b/cli/beacon/internal/codexsession/collect.go@@ -1,7 +1,6 @@package codexsessionimport (- "encoding/json""errors""fmt""io"@@ -12,6 +11,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -63,47 +63,16 @@ func DefaultStatePath() string {return filepath.Join(base, "endpoint", "state", "codex.json")}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Files: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Codex collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Files == nil {- return state, nil- }- state.Files = stored.Files- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Codex",+ New: func() *State { return &State{Version: StateVersion, Files: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Files != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(path string) *Cursor {if s.Files == nil {@@ -210,22 +179,11 @@ func advanceCursor(cursor *Cursor, ref SessionRef, lines int) {cursor.ModTimeUnixMS = ref.ModTimeUnixMS}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print {- out := opts.Out- if out == nil {- out = io.Discard- }- return json.NewEncoder(out).Encode(event)- }- if !opts.Write {- return nil- }- _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget})- return err+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}}}// advanceCursorPartial moves the cursor past source lines whose mapped events were all emitted, sodiff --git a/cli/beacon/internal/copilotsession/collect.go b/cli/beacon/internal/copilotsession/collect.goindex 6da931b2..10a73ddb 100644--- a/cli/beacon/internal/copilotsession/collect.go+++ b/cli/beacon/internal/copilotsession/collect.go@@ -1,7 +1,6 @@package copilotsessionimport (- "encoding/json""errors""fmt""io"@@ -10,8 +9,8 @@ import ("strings""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -64,47 +63,16 @@ func DefaultStatePath() string {return filepath.Join(base, "endpoint", "state", "copilot.json")}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Files: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read GitHub Copilot CLI collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Files == nil {- return state, nil- }- state.Files = stored.Files- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "GitHub Copilot CLI",+ New: func() *State { return &State{Version: StateVersion, Files: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Files != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(path string) *Cursor {if s.Files == nil {@@ -197,7 +165,7 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptireturn false, nil}for i, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {advanceCursorPartial(cursor, mapped, i)return true, err}@@ -241,22 +209,8 @@ func advanceCursorPartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {}}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}func FilterRefs(refs []SessionRef, opts CollectOptions) []SessionRef {diff --git a/cli/beacon/internal/cursorsession/account.go b/cli/beacon/internal/cursorsession/account.goindex f677fdf8..1e275f60 100644--- a/cli/beacon/internal/cursorsession/account.go+++ b/cli/beacon/internal/cursorsession/account.go@@ -6,6 +6,8 @@ import ("errors""fmt""strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync")// cachedEmailKey is the ItemTable row where Cursor keeps the email of the account signed in to@@ -24,7 +26,7 @@ func CachedAccountEmail(globalDBPath string) (string, error) {if !fileExists(globalDBPath) {return "", nil}- db, err := openSQLiteReadOnly(globalDBPath)+ db, err := sessionsync.OpenSQLite(globalDBPath, "busy_timeout(1000)")if err != nil {return "", fmt.Errorf("open Cursor global storage: %w", err)}diff --git a/cli/beacon/internal/cursorsession/collect.go b/cli/beacon/internal/cursorsession/collect.goindex 01b64d0a..44802e78 100644--- a/cli/beacon/internal/cursorsession/collect.go+++ b/cli/beacon/internal/cursorsession/collect.go@@ -1,7 +1,6 @@package cursorsessionimport (- "encoding/json""errors""fmt""io"@@ -12,6 +11,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -29,46 +29,17 @@ type State struct {Traces map[string]*Cursor `json:"traces"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Traces: map[string]*Cursor{}}- if strings.TrimSpace(path) == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Cursor collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Traces == nil {- return state, nil- }- return &stored, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Cursor",+ New: func() *State { return &State{Version: StateVersion, Traces: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Traces != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+ BlankPath: true,}-func (s *State) Save(path string) error {- if strings.TrimSpace(path) == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(ref TraceRef) *Cursor {if s.Traces == nil {@@ -227,23 +198,9 @@ func partialLastOrder(start int, mapped []MappedEvent, failedIdx int) int {return start}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/cursorsession/store.go b/cli/beacon/internal/cursorsession/store.goindex 9921c001..074ded0e 100644--- a/cli/beacon/internal/cursorsession/store.go+++ b/cli/beacon/internal/cursorsession/store.go@@ -5,16 +5,16 @@ import ("encoding/json""errors""fmt"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri""io/fs"- "net/url""os""path/filepath""runtime""sort""strings"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"+_ "modernc.org/sqlite")@@ -99,7 +99,7 @@ func (s *Store) listGlobalStorage() ([]TraceRef, error) {if !fileExists(s.GlobalDBPath) {return nil, nil}- db, err := openSQLiteReadOnly(s.GlobalDBPath)+ db, err := sessionsync.OpenSQLite(s.GlobalDBPath, "busy_timeout(1000)")if err != nil {return nil, fmt.Errorf("open Cursor global storage: %w", err)}@@ -268,7 +268,7 @@ func (s *Store) Read(ref TraceRef) ([]Record, error) {}func (s *Store) readGlobalStorage(ref TraceRef) ([]Record, error) {- db, err := openSQLiteReadOnly(s.GlobalDBPath)+ db, err := sessionsync.OpenSQLite(s.GlobalDBPath, "busy_timeout(1000)")if err != nil {return nil, fmt.Errorf("open Cursor global storage: %w", err)}@@ -428,14 +428,6 @@ func (s *Store) readTranscript(ref TraceRef) ([]Record, error) {return out, nil}-func openSQLiteReadOnly(path string) (*sql.DB, error) {- path, err := sessionfs.SQLitePath(path)- if err != nil {- return nil, err- }- return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": {"busy_timeout(1000)"}}))-}-func fileExists(path string) bool {if strings.TrimSpace(path) == "" {return falsediff --git a/cli/beacon/internal/cursorusage/collect.go b/cli/beacon/internal/cursorusage/collect.goindex 1e6ad3ca..d343b280 100644--- a/cli/beacon/internal/cursorusage/collect.go+++ b/cli/beacon/internal/cursorusage/collect.go@@ -2,7 +2,6 @@ package cursorusageimport ("context"- "encoding/json""errors""fmt""io"@@ -14,6 +13,7 @@ import ("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")// StateVersion is the on-disk version of the collector state. A file of another version is@@ -71,49 +71,19 @@ func ScopeKey(q Query) string {return "team"}-// LoadState reads the state file. A missing file or an empty path is an empty state.-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Scopes: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Cursor usage collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Scopes == nil {- return state, nil- }- state.Scopes = stored.Scopes- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Cursor usage",+ New: func() *State { return &State{Version: StateVersion, Scopes: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Scopes != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+ DirMode: 0o700,}+// LoadState reads the state file. A missing file or an empty path is an empty state.+func LoadState(path string) (*State, error) { return stateFile.Load(path) }+// Save writes the state atomically. An empty path is a no-op.-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func (s *State) Save(path string) error { return stateFile.Save(path, s) }// Cursor returns the scope's cursor, creating it.func (s *State) Cursor(scope string) *Cursor {@@ -396,19 +366,9 @@ func emit(event schema.Event, opts CollectOptions) error {if opts.emitHook != nil {return opts.emitHook(event)}- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write && !opts.Print {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode}); err != nil {- return err- }- }- return nil+ 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}}}diff --git a/cli/beacon/internal/devincloud/pull.go b/cli/beacon/internal/devincloud/pull.goindex 6dbf8dfc..ce2a4b15 100644--- a/cli/beacon/internal/devincloud/pull.go+++ b/cli/beacon/internal/devincloud/pull.go@@ -9,8 +9,8 @@ import ("path""sort"- "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")// Uploader uploads one session's full JSONL snapshot to a destination (GCS).@@ -130,7 +130,7 @@ func processSession(ctx context.Context, client *Client, s Session, ss *SessionSif ss.Emitted[me.DedupID] {continue}- if err := emit(me.Event, opts); err != nil {+ if err := opts.sink().Emit(me.Event); err != nil {return err}ss.Emitted[me.DedupID] = true@@ -145,7 +145,7 @@ func processSession(ctx context.Context, client *Client, s Session, ss *SessionSterminal := IsTerminal(s.Status)if terminal {if !ss.EndedEmitted {- if err := emit(EndedEvent(s), opts); err != nil {+ if err := opts.sink().Emit(EndedEvent(s)); err != nil {return err}ss.EndedEmitted = true@@ -186,22 +186,8 @@ func processSession(ctx context.Context, client *Client, s Session, ss *SessionSreturn nil}-func emit(ev schema.Event, opts PullOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(ev)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(ev, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode}); err != nil {- return err- }- }- return nil+func (o PullOptions) sink() sessionsync.Sink {+ return sessionsync.Sink{Print: o.Print, Out: o.Out, Write: o.Write, Log: writer.Options{Path: o.LogPath, UserMode: o.UserMode}}}// marshalEvents builds the per-session JSONL snapshot for upload. Events are rundiff --git a/cli/beacon/internal/devincloud/state.go b/cli/beacon/internal/devincloud/state.goindex 5e9d713d..d25013b2 100644--- a/cli/beacon/internal/devincloud/state.go+++ b/cli/beacon/internal/devincloud/state.go@@ -1,10 +1,6 @@package devincloud-import (- "encoding/json"- "os"- "path/filepath"-)+import "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"// State tracks, per org, which session events have already been emitted so that// re-polling is idempotent. It is a small JSON file managed by the connector.@@ -25,44 +21,15 @@ type SessionState struct {// LoadState reads state from path. A missing file yields empty state. An empty// path yields empty, non-persisted state.-func LoadState(path string) (*State, error) {- s := &State{Sessions: map[string]*SessionState{}}- if path == "" {- return s, nil- }- data, err := os.ReadFile(path)- if err != nil {- if os.IsNotExist(err) {- return s, nil- }- return nil, err- }- if err := json.Unmarshal(data, s); err != nil {- return nil, err- }- if s.Sessions == nil {- s.Sessions = map[string]*SessionState{}- }- return s, nil-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }// Save writes state to path atomically. A nil/empty path is a no-op.-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)+func (s *State) Save(path string) error { return stateFile.Save(path, s) }++// stateFile has no version and no collector name: an unreadable file returns the decode error as is.+var stateFile = sessionsync.StateFile[State]{+ New: func() *State { return &State{Sessions: map[string]*SessionState{}} },+ Valid: func(s *State) bool { return s.Sessions != nil },}func (s *State) get(sessionID string) *SessionState {diff --git a/cli/beacon/internal/devinsession/collect.go b/cli/beacon/internal/devinsession/collect.goindex d900023d..a6b42975 100644--- a/cli/beacon/internal/devinsession/collect.go+++ b/cli/beacon/internal/devinsession/collect.go@@ -22,21 +22,18 @@ package devinsessionimport ("database/sql"- "encoding/json""errors""fmt""io"- "net/url""os"- "path/filepath""strings""time""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/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri"_ "modernc.org/sqlite")@@ -176,47 +173,16 @@ type MappedEvent struct {Event schema.Event}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Devin collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sessions == nil {- return state, nil- }- state.Sessions = stored.Sessions- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Devin",+ New: func() *State { return &State{Version: StateVersion, Sessions: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sessions != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(sessionID string) *Cursor {if s.Sessions == nil {@@ -281,13 +247,11 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}}()- var guard *writer.RetentionGuard- if opts.Write && !opts.Print {- guard = &writer.RetentionGuard{}- }+ sink := opts.sink()+ sink.GuardRetention()var errs []errorfor i, session := range sessions {- changed, collectErr := collectSession(store, session, state, opts, guard, &summary)+ changed, collectErr := collectSession(store, session, state, opts, sink, &summary)if errors.Is(collectErr, writer.ErrRetentionWindowFull) {// Not a failed session: this one and the rest are left for the next sweep, with// cursors that have not moved past anything that was not written.@@ -317,13 +281,10 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {summary.SessionsChanged++}}- if guard != nil {- summary.EventsRetained = guard.Retained()- summary.EventsRotatedOut = guard.Written() - summary.EventsRetained- if summary.EventsRotatedOut > 0 {- errs = append(errs, fmt.Errorf("%d of the %d events this sweep wrote were rotated out of the runtime log by another writer before it finished",- summary.EventsRotatedOut, guard.Written()))- }+ var rotatedOut error+ summary.EventsRetained, summary.EventsRotatedOut, rotatedOut = sink.Retention()+ if rotatedOut != nil {+ errs = append(errs, rotatedOut)}if len(errs) > 0 {return summary, errors.Join(errs...)@@ -348,7 +309,7 @@ func countPending(store *Store, sessions []Session, state *State) int {return n}-func collectSession(store *Store, session Session, state *State, opts CollectOptions, guard *writer.RetentionGuard, summary *Summary) (bool, error) {+func collectSession(store *Store, session Session, state *State, opts CollectOptions, sink sessionsync.Sink, summary *Summary) (bool, error) {cursor := state.cursor(session.ID)m := newMapper(session, cursor)changed := false@@ -368,7 +329,7 @@ func collectSession(store *Store, session Session, state *State, opts CollectOptstarted := m.mapSessionStarted(startTS)if suppressedLive(started.Event.Event.Action, started.TimestampMS, session, opts) {summary.EventsSuppressedLive++- } else if err := emit(started.Event, opts, guard); err != nil {+ } else if err := sink.Emit(started.Event); err != nil {return true, err} else {summary.EventsEmitted++@@ -401,7 +362,7 @@ func collectSession(store *Store, session Session, state *State, opts CollectOptsummary.EventsSuppressedLive++continue}- if err := emit(item.Event, opts, guard); err != nil {+ if err := sink.Emit(item.Event); err != nil {m.rollbackRow(journal)return true, err}@@ -436,19 +397,8 @@ func suppressedLive(action string, timestampMS int64, session Session, opts Collreturn false}-func emit(event schema.Event, opts CollectOptions, guard *writer.RetentionGuard) error {- if opts.Print {- out := opts.Out- if out == nil {- out = io.Discard- }- return json.NewEncoder(out).Encode(event)- }- if !opts.Write {- return nil- }- _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, RotateSize: opts.RotateBytes, Guard: guard, Budget: opts.Budget})- return err+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, RotateSize: o.RotateBytes, Budget: o.Budget}}}// Store is a read-only handle on sessions.db.@@ -460,7 +410,7 @@ type Store struct {}func OpenStore(path string) (*Store, error) {- db, err := openSQLiteReadOnly(path)+ db, err := sessionsync.OpenSQLite(path, "busy_timeout(1000)")if err != nil {return nil, err}@@ -672,11 +622,3 @@ func unixSecondsMS(value sql.NullInt64) int64 {}return value.Int64 * 1000}--func openSQLiteReadOnly(path string) (*sql.DB, error) {- path, err := sessionfs.SQLitePath(path)- if err != nil {- return nil, err- }- return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": {"busy_timeout(1000)"}}))-}diff --git a/cli/beacon/internal/dshsession/collect.go b/cli/beacon/internal/dshsession/collect.goindex f69b1d77..708ce71c 100644--- a/cli/beacon/internal/dshsession/collect.go+++ b/cli/beacon/internal/dshsession/collect.go@@ -1,7 +1,6 @@package dshsessionimport (- "encoding/json""errors""fmt""io"@@ -12,6 +11,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -66,47 +66,16 @@ func DefaultStatePath() string {return filepath.Join(base, "endpoint", "state", "dsh.json")}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sources: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read DeepSeek collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sources == nil {- return state, nil- }- state.Sources = stored.Sources- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "DeepSeek",+ New: func() *State { return &State{Version: StateVersion, Sources: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sources != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(source string) *Cursor {if s.Sources == nil {@@ -246,25 +215,11 @@ func advanceCursorPartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {}}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}func filterRefs(refs []SessionRef, opts CollectOptions) []SessionRef {diff --git a/cli/beacon/internal/factorysession/collect.go b/cli/beacon/internal/factorysession/collect.goindex 17f740e0..410c65af 100644--- a/cli/beacon/internal/factorysession/collect.go+++ b/cli/beacon/internal/factorysession/collect.go@@ -1,7 +1,6 @@package factorysessionimport (- "encoding/json""errors""fmt""io"@@ -12,6 +11,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -34,47 +34,16 @@ type State struct {Sessions map[string]*Cursor `json:"sessions"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Factory collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sessions == nil {- return state, nil- }- state.Sessions = stored.Sessions- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Factory",+ New: func() *State { return &State{Version: StateVersion, Sessions: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sessions != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(sessionID string) *Cursor {if s.Sessions == nil {@@ -268,23 +237,9 @@ func advanceCursorPartial(cursor *Cursor, ref SessionRef, mapped []MappedEvent,}}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/fxsession/collect.go b/cli/beacon/internal/fxsession/collect.goindex 88c9a223..24e79480 100644--- a/cli/beacon/internal/fxsession/collect.go+++ b/cli/beacon/internal/fxsession/collect.go@@ -1,7 +1,6 @@package fxsessionimport (- "encoding/json""errors""fmt""io"@@ -10,8 +9,8 @@ import ("strings""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -43,54 +42,23 @@ type State struct {Sessions map[string]*Cursor `json:"sessions"`}+var stateFile = sessionsync.StateFile[State]{+ Name: "fx",+ New: func() *State { return &State{Version: StateVersion, Sessions: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sessions != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+}+// LoadState reads the cursor file. A missing file is an empty state, which is what a first run// looks like. An empty path yields state that is never persisted, which is what --print uses.//// A file from another state version is discarded rather than parsed: re-collecting a session// appends events that the writer's own duplicate window and the deterministic event ids already// make harmless, while reading a cursor under the wrong meaning skips events for good.-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read fx collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sessions == nil {- return state, nil- }- state.Sessions = stored.Sessions- return state, nil-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }// Save writes the cursor file atomically. An empty path is a no-op.-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(sessionID string) *Cursor {if s.Sessions == nil {@@ -271,7 +239,7 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti}for i, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {// Advance the cursor to cover events that were fully emitted so the// next sweep does not re-append them. Only source events whose entire// set of mapped events was written are advanced past; the partially@@ -368,20 +336,6 @@ func generationOf(events []Event) string {return events[len(events)-1].LogGeneration}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/groksession/collect.go b/cli/beacon/internal/groksession/collect.goindex 47a7ced4..7b7a94fe 100644--- a/cli/beacon/internal/groksession/collect.go+++ b/cli/beacon/internal/groksession/collect.go@@ -1,7 +1,6 @@package groksessionimport (- "encoding/json""errors""fmt""io"@@ -10,8 +9,8 @@ import ("strings""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -53,49 +52,21 @@ func DefaultStatePath() string {return filepath.Join(base, "endpoint", "state", "grok-sessions.json")}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Grok session",+ New: func() *State { return &State{Version: StateVersion, Sessions: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sessions != nil },+ Stamp: func(s *State) {+ if s.Sessions == nil {+ s.Sessions = map[string]*Cursor{}}- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Grok session collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sessions == nil {- return state, nil- }- return &stored, nil+ s.Version = StateVersion+ },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if s.Sessions == nil {- s.Sessions = map[string]*Cursor{}- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(id string) *Cursor {if s.Sessions == nil {@@ -234,7 +205,7 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti}written := 0for _, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {commit(cursor, mapped, written)return true, err}@@ -282,20 +253,6 @@ func commit(cursor *Cursor, mapped []MappedEvent, written int) {cursor.Events += written}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/hermessession/collect.go b/cli/beacon/internal/hermessession/collect.goindex 6260d4fe..176cd92e 100644--- a/cli/beacon/internal/hermessession/collect.go+++ b/cli/beacon/internal/hermessession/collect.go@@ -8,9 +8,7 @@ import ("encoding/json""errors""fmt"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs""io"- "net/url""os""path/filepath""strconv"@@ -19,8 +17,9 @@ import ("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/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri""github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"_ "modernc.org/sqlite")@@ -159,47 +158,16 @@ func LoadStateSeeded(path, legacy string) (*State, error) {return LoadState(legacy)}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Hermes collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Sessions == nil {- return state, nil- }- state.Sessions = stored.Sessions- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Hermes",+ New: func() *State { return &State{Version: StateVersion, Sessions: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Sessions != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(sessionID string) *Cursor {if s.Sessions == nil {@@ -290,7 +258,7 @@ func collectSession(store *Store, session Session, state *State, opts CollectOptreturn false, nil}for _, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {return true, err}summary.EventsEmitted++@@ -346,22 +314,8 @@ func updateCursorTotals(cursor *Cursor, session Session) {}}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}type Store struct {@@ -373,7 +327,7 @@ type Store struct {}func OpenStore(path string) (*Store, error) {- db, err := openSQLiteReadOnly(path)+ db, err := sessionsync.OpenSQLite(path, "busy_timeout(1000)")if err != nil {return nil, err}@@ -602,14 +556,6 @@ func (s *Store) LastMessageID(sessionID string) (int64, error) {return last.Int64, nil}-func openSQLiteReadOnly(path string) (*sql.DB, error) {- path, err := sessionfs.SQLitePath(path)- if err != nil {- return nil, err- }- return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": {"busy_timeout(1000)"}}))-}-// MapSession maps one session's new messages into endpoint events. It also returns the tool calls// that are still waiting for their result, because Hermes writes a call's arguments on the// assistant row and the result on a later one: without carrying them across sweeps, a resultdiff --git a/cli/beacon/internal/openclawsession/collect.go b/cli/beacon/internal/openclawsession/collect.goindex 79ecfc21..e3c51487 100644--- a/cli/beacon/internal/openclawsession/collect.go+++ b/cli/beacon/internal/openclawsession/collect.go@@ -1,7 +1,6 @@package openclawsessionimport (- "encoding/json""errors""fmt""io"@@ -12,6 +11,7 @@ import ("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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -27,46 +27,17 @@ type State struct {Traces map[string]*Cursor `json:"traces"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Traces: map[string]*Cursor{}}- if strings.TrimSpace(path) == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read OpenClaw collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Traces == nil {- return state, nil- }- return &stored, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "OpenClaw",+ New: func() *State { return &State{Version: StateVersion, Traces: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Traces != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+ BlankPath: true,}-func (s *State) Save(path string) error {- if strings.TrimSpace(path) == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(ref TraceRef) *Cursor {if s.Traces == nil {@@ -212,23 +183,9 @@ func partialLastOrder(start int, mapped []MappedEvent, failedIdx int) int {return start}-// emitEvent is emit, swapped by tests to fail a chosen write.-var emitEvent = emit+// emitEvent emits through opts.sink(), swapped by tests to fail a chosen write.+var emitEvent = func(event schema.Event, opts CollectOptions) error { return opts.sink().Emit(event) }-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/opencodesession/collect.go b/cli/beacon/internal/opencodesession/collect.goindex 5424d8e2..b2cbefcc 100644--- a/cli/beacon/internal/opencodesession/collect.go+++ b/cli/beacon/internal/opencodesession/collect.go@@ -1,7 +1,6 @@package opencodesessionimport (- "encoding/json""errors""fmt""io"@@ -10,8 +9,8 @@ import ("strings""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -32,46 +31,17 @@ type State struct {Traces map[string]*Cursor `json:"traces"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Traces: map[string]*Cursor{}}- if strings.TrimSpace(path) == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read OpenCode collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Traces == nil {- return state, nil- }- return &stored, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "OpenCode",+ New: func() *State { return &State{Version: StateVersion, Traces: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Traces != nil },+ Stamp: func(s *State) { s.Version = StateVersion },+ BlankPath: true,}-func (s *State) Save(path string) error {- if strings.TrimSpace(path) == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(ref TraceRef) *Cursor {if s.Traces == nil {@@ -216,7 +186,7 @@ func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions,if cursor.Emitted[id] {continue}- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {// The ids already written stay recorded, so a retry resumes rather than doubling the// log. The trace's timestamp is left alone so the next sweep comes back for the rest.return changed, err@@ -240,20 +210,6 @@ func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions,return changed, nil}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/opencodesession/store.go b/cli/beacon/internal/opencodesession/store.goindex dd24a916..33885504 100644--- a/cli/beacon/internal/opencodesession/store.go+++ b/cli/beacon/internal/opencodesession/store.go@@ -5,16 +5,16 @@ import ("encoding/json""errors""fmt"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri""io/fs"- "net/url""os""path/filepath""sort""strconv""strings"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync"+_ "modernc.org/sqlite")@@ -832,14 +832,9 @@ func legacyProjectDirectory(path string) string {return firstNonEmpty(stringFromMap(raw, "worktree"), stringFromMap(raw, "directory"), stringFromMap(raw, "name"))}-// openSQLiteReadOnly opens OpenCode's store read-only. modernc.org/sqlite opens every database-// READWRITE|CREATE, so mode=ro in the URI is the one thing keeping this read from writing to it.+// openSQLiteReadOnly opens OpenCode's store read-only, with query_only in place of a busy timeout.func openSQLiteReadOnly(path string) (*sql.DB, error) {- path, err := sessionfs.SQLitePath(path)- if err != nil {- return nil, err- }- return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": {"query_only(1)"}}))+ return sessionsync.OpenSQLite(path, "query_only(1)")}func sqliteTableExists(db *sql.DB, table string) bool {diff --git a/cli/beacon/internal/pisession/collect.go b/cli/beacon/internal/pisession/collect.goindex 790676e0..3b4056e3 100644--- a/cli/beacon/internal/pisession/collect.go+++ b/cli/beacon/internal/pisession/collect.go@@ -1,14 +1,13 @@package pisessionimport (- "encoding/json""errors""fmt""io""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -107,7 +106,7 @@ func collectFile(store *Store, ref SessionRef, state *State, opts CollectOptionsreturn false, nil}for i, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {advancePartial(cursor, mapped, i)return true, err}@@ -151,20 +150,6 @@ func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {}}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/pisession/state.go b/cli/beacon/internal/pisession/state.goindex 2f5a2bd6..611a2e29 100644--- a/cli/beacon/internal/pisession/state.go+++ b/cli/beacon/internal/pisession/state.go@@ -1,12 +1,11 @@package pisessionimport (- "encoding/json"- "errors"- "fmt""os""path/filepath""strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync")const StateVersion = 1@@ -23,47 +22,16 @@ type State struct {Files map[string]*Cursor `json:"files"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Files: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Pi collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Files == nil {- return state, nil- }- state.Files = stored.Files- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Pi",+ New: func() *State { return &State{Version: StateVersion, Files: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Files != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(path string) *Cursor {if s.Files == nil {diff --git a/cli/beacon/internal/primesession/collect.go b/cli/beacon/internal/primesession/collect.goindex 0843725f..2ff753d4 100644--- a/cli/beacon/internal/primesession/collect.go+++ b/cli/beacon/internal/primesession/collect.go@@ -1,14 +1,13 @@package primesessionimport (- "encoding/json""errors""fmt""io""time"- "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""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow")@@ -110,7 +109,7 @@ func collectFile(store *Store, ref SessionRef, state *State, opts CollectOptionsreturn false, nil}for i, item := range mapped {- if err := emit(item.Event, opts); err != nil {+ if err := opts.sink().Emit(item.Event); err != nil {advancePartial(cursor, mapped, i, parent)return true, err}@@ -153,20 +152,6 @@ func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int, parentcursor.SubagentOf = parent}-func emit(event schema.Event, opts CollectOptions) error {- if opts.Print && opts.Out != nil {- data, err := json.Marshal(event)- if err != nil {- return err- }- if _, err := opts.Out.Write(append(data, '\n')); err != nil {- return err- }- }- if opts.Write {- if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, Budget: opts.Budget}); err != nil {- return err- }- }- return nil+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}}}diff --git a/cli/beacon/internal/primesession/state.go b/cli/beacon/internal/primesession/state.goindex 9c1ba2a9..f625a121 100644--- a/cli/beacon/internal/primesession/state.go+++ b/cli/beacon/internal/primesession/state.go@@ -1,12 +1,11 @@package primesessionimport (- "encoding/json"- "errors"- "fmt""os""path/filepath""strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionsync")const StateVersion = 1@@ -24,47 +23,16 @@ type State struct {Files map[string]*Cursor `json:"files"`}-func LoadState(path string) (*State, error) {- state := &State{Version: StateVersion, Files: map[string]*Cursor{}}- if path == "" {- return state, nil- }- data, err := os.ReadFile(path)- if err != nil {- if errors.Is(err, os.ErrNotExist) {- return state, nil- }- return nil, err- }- var stored State- if err := json.Unmarshal(data, &stored); err != nil {- return nil, fmt.Errorf("read Prime Agent collector state %s: %w", path, err)- }- if stored.Version != StateVersion || stored.Files == nil {- return state, nil- }- state.Files = stored.Files- return state, nil+var stateFile = sessionsync.StateFile[State]{+ Name: "Prime Agent",+ New: func() *State { return &State{Version: StateVersion, Files: map[string]*Cursor{}} },+ Valid: func(s *State) bool { return s.Version == StateVersion && s.Files != nil },+ Stamp: func(s *State) { s.Version = StateVersion },}-func (s *State) Save(path string) error {- if path == "" {- return nil- }- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {- return err- }- s.Version = StateVersion- data, err := json.MarshalIndent(s, "", " ")- if err != nil {- return err- }- tmp := path + ".tmp"- if err := os.WriteFile(tmp, data, 0o600); err != nil {- return err- }- return os.Rename(tmp, path)-}+func LoadState(path string) (*State, error) { return stateFile.Load(path) }++func (s *State) Save(path string) error { return stateFile.Save(path, s) }func (s *State) cursor(path string) *Cursor {if s.Files == nil {diff --git a/cli/beacon/internal/sessionfs/collectors_test.go b/cli/beacon/internal/sessionfs/collectors_test.goindex f07994a1..2e287542 100644--- a/cli/beacon/internal/sessionfs/collectors_test.go+++ b/cli/beacon/internal/sessionfs/collectors_test.go@@ -30,12 +30,9 @@ var directReads = map[string]bool{// allowedDirectReads are the exceptions, by package, function and call.var allowedDirectReads = map[string]bool{// The collector's own cursor file, under Beacon's base directory rather than the user's store.- "*.LoadState.os.ReadFile": true,+ "sessionsync.Load.os.ReadFile": true,// The driver open itself, after SQLitePath has turned the path into a confined snapshot.- "cursorsession.openSQLiteReadOnly.sql.Open": true,- "hermessession.openSQLiteReadOnly.sql.Open": true,- "opencodesession.openSQLiteReadOnly.sql.Open": true,- "devinsession.openSQLiteReadOnly.sql.Open": true,+ "sessionsync.OpenSQLite.sql.Open": true,// Existence checks on project paths named inside a transcript, to infer the workspace. They// read no content and the paths are usually outside the home."cursorsession.normalizeDirectoryCandidate.os.Stat": true,@@ -43,7 +40,8 @@ var allowedDirectReads = map[string]bool{}func TestBackfillCollectorsReadTheirStoresThroughSessionfs(t *testing.T) {- for _, pkg := range backfillCollectors {+ // sessionsync is the sync machinery every collector shares.+ for _, pkg := range append(backfillCollectors, "sessionsync") {files, err := filepath.Glob(filepath.Join("..", pkg, "*.go"))if err != nil || len(files) == 0 {t.Fatalf("%s: no sources (%v)", pkg, err)diff --git a/cli/beacon/internal/sessionsync/sessionsync.go b/cli/beacon/internal/sessionsync/sessionsync.gonew file mode 100644index 00000000..3f0ca860--- /dev/null+++ b/cli/beacon/internal/sessionsync/sessionsync.go@@ -0,0 +1,167 @@+// Package sessionsync holds the sync machinery every session-store collector shares: its cursor+// file, where its events go, and the read-only open of a runtime's SQLite store. Each collector+// keeps only how it reads its runtime's store and maps the records to events.+package sessionsync++import (+ "database/sql"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net/url"+ "os"+ "path/filepath"+ "strings"++ "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/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri"++ _ "modernc.org/sqlite"+)++// StateFile describes one collector's cursor file.+type StateFile[S any] struct {+ // Name is the collector an unreadable file's error names. Empty returns the decode error as is.+ Name string+ // New returns the state an empty path, a missing file or a file Valid rejects starts from.+ New func() *S+ // Valid reports whether a decoded file can be used, as a matching version does. Nil accepts any.+ Valid func(*S) bool+ // Stamp runs before each save, to record the version.+ Stamp func(*S)+ // BlankPath treats a path of only whitespace as empty.+ BlankPath bool+ // DirMode is the mode the state directory is created with; zero means 0o755.+ DirMode os.FileMode+}++func (f StateFile[S]) empty(path string) bool {+ if f.BlankPath {+ return strings.TrimSpace(path) == ""+ }+ return path == ""+}++// Load reads the cursor file at path. An empty path or a missing file yields a new state, and so+// does a file Valid rejects, which resets the collector to read its store from the start.+func (f StateFile[S]) Load(path string) (*S, error) {+ if f.empty(path) {+ return f.New(), nil+ }+ data, err := os.ReadFile(path)+ if err != nil {+ if errors.Is(err, os.ErrNotExist) {+ return f.New(), nil+ }+ return nil, err+ }+ stored := new(S)+ if err := json.Unmarshal(data, stored); err != nil {+ if f.Name == "" {+ return nil, err+ }+ return nil, fmt.Errorf("read %s collector state %s: %w", f.Name, path, err)+ }+ if f.Valid != nil && !f.Valid(stored) {+ return f.New(), nil+ }+ return stored, nil+}++// Save writes s to path through a temporary file and a rename, so a reader never sees half of+// it. An empty path saves nothing.+func (f StateFile[S]) Save(path string, s *S) error {+ if f.empty(path) {+ return nil+ }+ mode := f.DirMode+ if mode == 0 {+ mode = 0o755+ }+ if err := os.MkdirAll(filepath.Dir(path), mode); err != nil {+ return err+ }+ if f.Stamp != nil {+ f.Stamp(s)+ }+ data, err := json.MarshalIndent(s, "", " ")+ if err != nil {+ return err+ }+ tmp := path + ".tmp"+ if err := os.WriteFile(tmp, data, 0o600); err != nil {+ return err+ }+ return os.Rename(tmp, path)+}++// Sink is where a sweep's events go: one JSON object per line to Out for --print, or appended to+// the runtime log with Log.+type Sink struct {+ Print bool+ // Out receives printed events; nil drops them.+ Out io.Writer+ Write bool+ Log writer.Options+}++// Emit prints or appends one event. A printing sweep never writes. An append refused by+// Log.Budget or Log.Guard returns writer.ErrBudgetSpent or writer.ErrRetentionWindowFull, which+// the sweep stops on.+func (s Sink) Emit(event schema.Event) error {+ if s.Print {+ if s.Out == nil {+ return nil+ }+ data, err := json.Marshal(event)+ if err != nil {+ return err+ }+ _, err = s.Out.Write(append(data, '\n'))+ return err+ }+ if !s.Write {+ return nil+ }+ _, err := writer.AppendEvent(event, s.Log)+ return err+}++// 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{}+ }+}++// 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.+func (s Sink) Retention() (retained, rotatedOut int, err error) {+ g := s.Log.Guard+ if g == nil {+ return 0, 0, nil+ }+ retained = g.Retained()+ rotatedOut = g.Written() - retained+ if rotatedOut > 0 {+ err = fmt.Errorf("%d of the %d events this sweep wrote were rotated out of the runtime log by another writer before it finished",+ rotatedOut, g.Written())+ }+ return retained, rotatedOut, err+}++// OpenSQLite opens a runtime's SQLite store read-only, through the snapshot a confined read takes,+// with pragmas passed as _pragma. modernc.org/sqlite opens every database READWRITE|CREATE, so+// mode=ro in the URI is the one thing keeping the read from writing to it.+func OpenSQLite(path string, pragmas ...string) (*sql.DB, error) {+ path, err := sessionfs.SQLitePath(path)+ if err != nil {+ return nil, err+ }+ return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": pragmas}))+}diff --git a/cli/beacon/internal/sessionsync/sessionsync_test.go b/cli/beacon/internal/sessionsync/sessionsync_test.gonew file mode 100644index 00000000..4e1f0c33--- /dev/null+++ b/cli/beacon/internal/sessionsync/sessionsync_test.go@@ -0,0 +1,167 @@+package sessionsync++import (+ "bytes"+ "database/sql"+ "errors"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+)++type testState struct {+ Version int `json:"version"`+ Files map[string]*int64 `json:"files"`+}++var testFile = StateFile[testState]{+ Name: "Test",+ New: func() *testState { return &testState{Version: 2, Files: map[string]*int64{}} },+ Valid: func(s *testState) bool { return s.Version == 2 && s.Files != nil },+ Stamp: func(s *testState) { s.Version = 2 },+}++func testEvent(message string) schema.Event {+ return schema.NewEvent(schema.NewEventOptions{Action: "agent.detected", Harness: schema.HarnessInfo{Name: "test"}, Message: message})+}++func TestStateFileRoundTrip(t *testing.T) {+ path := filepath.Join(t.TempDir(), "state", "test.json")+ state, err := testFile.Load(path)+ if err != nil || state.Version != 2 || len(state.Files) != 0 {+ t.Fatalf("missing file loaded %+v, %v", state, err)+ }+ n := int64(7)+ state.Files["a"] = &n+ state.Version = 0+ if err := testFile.Save(path, state); err != nil {+ t.Fatal(err)+ }+ data, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ if want := "{\n \"version\": 2,\n \"files\": {\n \"a\": 7\n }\n}"; string(data) != want {+ t.Fatalf("saved %q, want %q", data, want)+ }+ if _, err := os.Stat(path + ".tmp"); !errors.Is(err, os.ErrNotExist) {+ t.Fatalf("temporary file left behind: %v", err)+ }+ loaded, err := testFile.Load(path)+ if err != nil || loaded.Files["a"] == nil || *loaded.Files["a"] != 7 {+ t.Fatalf("reloaded %+v, %v", loaded, err)+ }++ if err := os.WriteFile(path, []byte(`{"version":1,"files":{"a":7}}`), 0o600); err != nil {+ t.Fatal(err)+ }+ if reset, err := testFile.Load(path); err != nil || len(reset.Files) != 0 {+ t.Fatalf("another version should reset, got %+v, %v", reset, err)+ }+ if err := os.WriteFile(path, []byte(`{`), 0o600); err != nil {+ t.Fatal(err)+ }+ if _, err := testFile.Load(path); err == nil || !strings.HasPrefix(err.Error(), "read Test collector state "+path+": ") {+ t.Fatalf("corrupt file error = %v", err)+ }+}++func TestStateFileEmptyPath(t *testing.T) {+ dir := t.TempDir()+ t.Chdir(dir)+ blank := testFile+ blank.BlankPath = true+ for _, f := range []StateFile[testState]{testFile, blank} {+ if err := f.Save("", f.New()); err != nil {+ t.Fatal(err)+ }+ }+ if err := blank.Save(" ", blank.New()); err != nil {+ t.Fatal(err)+ }+ if entries, _ := os.ReadDir(dir); len(entries) != 0 {+ t.Fatalf("an empty path wrote %v", entries)+ }+}++func TestOpenSQLiteIsReadOnly(t *testing.T) {+ path := filepath.Join(t.TempDir(), "store.db")+ rw, err := sql.Open("sqlite", path)+ if err != nil {+ t.Fatal(err)+ }+ if _, err := rw.Exec(`CREATE TABLE t (v TEXT); INSERT INTO t VALUES ('x')`); err != nil {+ t.Fatal(err)+ }+ rw.Close()++ db, err := OpenSQLite(path, "busy_timeout(1000)")+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ var v string+ if err := db.QueryRow(`SELECT v FROM t`).Scan(&v); err != nil || v != "x" {+ t.Fatalf("read %q, %v", v, err)+ }+ if _, err := db.Exec(`INSERT INTO t VALUES ('y')`); err == nil {+ t.Fatal("a read-only open accepted a write")+ }++ missing := filepath.Join(t.TempDir(), "missing.db")+ if db, err := OpenSQLite(missing); err == nil {+ db.Ping()+ db.Close()+ }+ if _, err := os.Stat(missing); !errors.Is(err, os.ErrNotExist) {+ t.Fatalf("opening a missing store created it: %v", err)+ }+}++func TestSinkPrintOnly(t *testing.T) {+ event := testEvent("printed")+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ var out bytes.Buffer+ for _, sink := range []Sink{+ {Print: true, Out: &out, Write: true, Log: writer.Options{Path: logPath, UserMode: true}},+ {Print: true, Write: true, Log: writer.Options{Path: logPath, UserMode: true}},+ } {+ if err := sink.Emit(event); err != nil {+ t.Fatal(err)+ }+ }+ if !strings.HasSuffix(out.String(), "\n") || strings.Count(out.String(), "\n") != 1 || !strings.Contains(out.String(), `"message":"printed"`) {+ t.Fatalf("printed %q", out.String())+ }+ if _, err := os.Stat(logPath); !errors.Is(err, os.ErrNotExist) {+ t.Fatalf("a printing sink wrote the log: %v", err)+ }+}++func TestSinkStopsOnBudgetAndRetention(t *testing.T) {+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")+ sink := Sink{Write: true, Log: writer.Options{Path: logPath, UserMode: true, Budget: writer.NewBudget(1)}}+ sink.GuardRetention()+ if sink.Log.Guard == nil {+ t.Fatal("a writing sink has no retention guard")+ }+ if err := sink.Emit(testEvent("too big for the budget")); !errors.Is(err, writer.ErrBudgetSpent) {+ t.Fatalf("over budget = %v, want ErrBudgetSpent", err)+ }+ if retained, rotatedOut, err := sink.Retention(); retained != 0 || rotatedOut != 0 || err != nil {+ t.Fatalf("Retention() = %d, %d, %v after nothing was written", retained, rotatedOut, err)+ }++ printing := Sink{Print: true, Write: true}+ printing.GuardRetention()+ if printing.Log.Guard != nil {+ t.Fatal("a printing sink got a retention guard")+ }+ if retained, rotatedOut, err := printing.Retention(); retained != 0 || rotatedOut != 0 || err != nil {+ t.Fatalf("Retention() without a guard = %d, %d, %v", retained, rotatedOut, err)+ }+}