Skip to content

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

8.9 KB
Raw Blame
package factorysession

import (
	"errors"
	"fmt"
	"io"
	"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/sessionsync"
	"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow"
)

const StateVersion = 1

type Cursor struct {
	SourcePath     string `json:"source_path,omitempty"`
	LastLine       int    `json:"last_line"`
	Started        bool   `json:"started"`
	ModTimeUnixMS  int64  `json:"mtime_ms,omitempty"`
	SizeBytes      int64  `json:"size_bytes,omitempty"`
	SettingsUnixMS int64  `json:"settings_mtime_ms,omitempty"`
	// Usage is the cumulative settings usage Beacon has already emitted for this session, the
	// baseline the next token.usage delta is measured from. Nil means none has been emitted yet.
	Usage *UsageTotals `json:"usage_totals,omitempty"`
}

type State struct {
	Version  int                `json:"version"`
	Sessions map[string]*Cursor `json:"sessions"`
}

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 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 {
		s.Sessions = map[string]*Cursor{}
	}
	cursor := s.Sessions[sessionID]
	if cursor == nil {
		cursor = &Cursor{}
		s.Sessions[sessionID] = cursor
	}
	return cursor
}

func DefaultStatePath() string {
	base := filepath.Join(os.TempDir(), "beacon")
	if home, err := os.UserHomeDir(); err == nil && strings.TrimSpace(home) != "" {
		base = filepath.Join(home, ".beacon")
	}
	return filepath.Join(base, "endpoint", "state", "factory.json")
}

type CollectOptions struct {
	SessionsDir string
	StatePath   string
	Write       bool
	LogPath     string
	UserMode    bool
	Print       bool
	Out         io.Writer
	// Since, when set, limits the sweep to sessions modified at or after it, newest first. Older
	// sessions are not read and their cursors do not move, so a later sync still collects them.
	// The install-time backfill sets it; an ordinary sync leaves it zero.
	Since time.Time
	// 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 {
	Sessions        int `json:"sessions"`
	SessionsChanged int `json:"sessions_changed"`
	EventsEmitted   int `json:"events_emitted"`
	Errors          int `json:"errors"`
	MalformedLines  int `json:"malformed_lines"`
	PartialSessions int `json:"partial_sessions"`
	sessionsync.Retention
}

func CollectOnce(opts CollectOptions) (summary Summary, err error) {
	store, err := NewStore(opts.SessionsDir)
	if err != nil {
		return summary, err
	}
	refs, err := store.List()
	if err != nil {
		return summary, err
	}
	refs = sessionwindow.Recent(refs, opts.Since, func(r SessionRef) int64 { return r.ModTimeUnixMS })
	summary.Sessions = len(refs)
	if len(refs) == 0 {
		return summary, nil
	}
	state, err := LoadState(opts.StatePath)
	if err != nil {
		return summary, err
	}
	defer func() {
		if saveErr := state.Save(opts.StatePath); saveErr != nil && err == nil {
			err = fmt.Errorf("save Factory collector state: %w", saveErr)
		}
	}()

	var errs []error
	opts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)
	var stopErr error
	for _, ref := range refs {
		changed, collectErr := collectSession(store, ref, state, opts, &summary)
		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 = collectErr
			errs = append(errs, collectErr)
			break
		}
		if collectErr != nil {
			summary.Errors++
			errs = append(errs, fmt.Errorf("Factory session %s: %w", ref.ID, collectErr))
			continue
		}
		if changed {
			summary.SessionsChanged++
		}
	}
	if rotatedOut := summary.Finish(opts.sink(), stopErr); rotatedOut != nil {
		errs = append(errs, rotatedOut)
	}
	if len(errs) > 0 {
		return summary, errors.Join(errs...)
	}
	return summary, nil
}

func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptions, summary *Summary) (bool, error) {
	cursor := state.cursor(ref.ID)
	if cursor.SourcePath != "" && cursor.SourcePath != ref.Path {
		cursor.LastLine = 0
		cursor.Started = false
		cursor.SettingsUnixMS = 0
		// Usage stays: the session id is the same session, and its settings totals are still
		// cumulative from the same start, so forgetting them would count them again.
	}
	seedLegacyUsageBaseline(cursor, ref)
	if cursor.LastLine > 0 &&
		cursor.ModTimeUnixMS == ref.ModTimeUnixMS &&
		cursor.SizeBytes == ref.SizeBytes &&
		cursor.SettingsUnixMS == ref.SettingsUnixMS {
		return false, nil
	}
	records, stats, err := store.Read(ref)
	summary.MalformedLines += stats.Malformed
	if stats.Partial {
		summary.PartialSessions++
	}
	if err != nil {
		return false, err
	}
	emitSettingsUsage := ref.Settings != nil && ref.SettingsUnixMS > 0 && ref.SettingsUnixMS != cursor.SettingsUnixMS
	mapped := MapSession(ref, records, MapOptions{
		MinLine:            cursor.LastLine,
		SkipSessionStarted: cursor.Started,
		EmitSettingsUsage:  emitSettingsUsage,
		PreviousUsage:      cursor.Usage,
	})
	for i, item := range mapped {
		if err := emitEvent(item.Event, opts); err != nil {
			advanceCursorPartial(cursor, ref, mapped, i)
			return true, err
		}
		summary.EventsEmitted++
		if item.Event.Event.Action == "session.started" {
			cursor.Started = true
		}
	}
	advanceCursor(cursor, ref, stats)
	// Committed only once every event is on disk, so a failed write retries the same delta. A
	// settings file with no usable totals keeps the baseline it had.
	if emitSettingsUsage {
		if totals := SettingsUsageTotals(ref.Settings); totals != nil {
			cursor.Usage = totals
		}
	}
	return len(mapped) > 0 || stats.Lines != cursor.LastLine || emitSettingsUsage, nil
}

// seedLegacyUsageBaseline upgrades state written before Beacon kept usage totals. That release
// emitted the whole cumulative total each time the settings mtime changed, so a cursor that has a
// settings mtime but no totals already holds a token.usage event for exactly the settings it
// recorded. When the file is still at that mtime, its totals are what was emitted, and they become
// the baseline instead of being counted again. A file that changed since then cannot be told
// apart from first sight, so it is counted from zero, as that release would have.
func seedLegacyUsageBaseline(cursor *Cursor, ref SessionRef) {
	if cursor.Usage != nil || cursor.SettingsUnixMS == 0 || cursor.SettingsUnixMS != ref.SettingsUnixMS {
		return
	}
	cursor.Usage = SettingsUsageTotals(ref.Settings)
}

func advanceCursor(cursor *Cursor, ref SessionRef, stats ReadStats) {
	cursor.SourcePath = ref.Path
	cursor.LastLine = stats.Lines
	cursor.ModTimeUnixMS = ref.ModTimeUnixMS
	cursor.SizeBytes = ref.SizeBytes
	cursor.SettingsUnixMS = ref.SettingsUnixMS
}

// advanceCursorPartial moves the cursor past source lines whose mapped events were all emitted, so
// the next sweep retries from the source line that failed. One line maps to several events (a
// message's text blocks, a tool call and its result); stopping at the last emitted event's line
// would skip the rest of that line for good. failedIdx is the index into mapped of the event whose
// emit failed.
func advanceCursorPartial(cursor *Cursor, ref SessionRef, mapped []MappedEvent, failedIdx int) {
	cursor.SourcePath = ref.Path
	for i := 0; i < failedIdx; i++ {
		if mapped[i].Event.Event.Action == "session.started" {
			cursor.Started = true
		}
	}
	for i := failedIdx - 1; i >= 0; i-- {
		if mapped[i].SourceLine != mapped[failedIdx].SourceLine {
			if mapped[i].SourceLine > cursor.LastLine {
				cursor.LastLine = mapped[i].SourceLine
			}
			return
		}
	}
}

// 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 (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, Guard: o.guard}}
}