sessioncleanup

package
v0.7.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sessioncleanup plans and runs the bulk deletion of a profile's archived session history: whole archived threads, or only their child sessions or subagents, picked by the day the thread was archived.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidRequest wraps every refused cleanup request.
	ErrInvalidRequest = errors.New("sessioncleanup: invalid request")
	// ErrRunActive refuses a second run for a profile whose cleanup is still running.
	ErrRunActive = errors.New("sessioncleanup: a cleanup is already running for this profile")
	// ErrNoActiveRun reports a stop request with nothing running.
	ErrNoActiveRun = errors.New("sessioncleanup: no cleanup is running for this profile")
	// ErrShuttingDown refuses new runs while the daemon stops.
	ErrShuttingDown = errors.New("sessioncleanup: service is shutting down")
)

Functions

This section is empty.

Types

type ChoiceTotals

type ChoiceTotals struct {
	Everything int
	Children   int
	Subagents  int
}

ChoiceTotals are the per-check totals a picker shows beside each check.

type Counts

type Counts struct {
	// Sessions are archived main sessions (Everything only).
	Sessions  int
	Children  int
	Subagents int
}

Counts is how many sessions a cleanup deletes, by kind.

func (Counts) Total

func (c Counts) Total() int

Total is every session the counts delete.

type Deleter

type Deleter interface {
	DeleteArchived(ctx context.Context, target store.ArchivedSessionDelete) error
}

Deleter deletes one archived session through the normal session delete path. It must return store.ErrSessionNotFound for a session already gone and store.ErrSessionNotArchived for one restored or moved since the plan.

type Failure

type Failure struct {
	RootID      string
	WorkspaceID string
	Title       string
	Code        FailureCode
	Message     string
}

Failure is one thread that kept sessions the run meant to delete. Retrying it means a new run limited to RootID.

type FailureCode

type FailureCode string

FailureCode classifies why a thread kept some of its sessions.

const (
	FailureInstanceMain FailureCode = "instance_main"
	FailureDeleteFailed FailureCode = "delete_failed"
)

type Lister

type Lister interface {
	ListSessionHistoryCleanupRows(
		ctx context.Context,
		query store.SessionHistoryCleanupQuery,
	) ([]store.SessionHistoryCleanupRow, error)
}

Lister reads the archived history a cleanup plans from.

type Option

type Option func(*Service)

Option configures a Service.

func WithClock

func WithClock(now func() time.Time) Option

WithClock sets the service clock.

func WithConcurrency

func WithConcurrency(workers int) Option

WithConcurrency bounds the threads deleted at once.

func WithDeleteTimeout

func WithDeleteTimeout(timeout time.Duration) Option

WithDeleteTimeout bounds one accepted delete, including lifecycle lock admission. Nonpositive values retain the default timeout.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the service logger.

func WithProgressInterval

func WithProgressInterval(interval time.Duration) Option

WithProgressInterval sets the minimum gap between progress wakes.

func WithPublisher

func WithPublisher(publish func(profileID string)) Option

WithPublisher wakes clients of a profile when its run progresses or ends.

type Plan

type Plan struct {
	// CountedAt is the instant the plan counted up to: a session archived later
	// is planned as still in the list, so a start sent with it deletes no more
	// than the plan counted.
	CountedAt time.Time
	Counts    Counts
	// KeptLive counts child sessions still in the session list or in Later that
	// the selection would otherwise delete; they and everything under them stay.
	KeptLive int
	// Choices are the totals each check alone would delete in the same range.
	Choices    ChoiceTotals
	Workspaces []WorkspaceTotal
	Threads    []Thread
}

Plan is the dry run of one cleanup.

func BuildPlan

func BuildPlan(rows []store.SessionHistoryCleanupRow, request Request) Plan

BuildPlan places the rows in their threads and decides, for every picked thread, which sessions go. It is pure: the same rows always plan the same way.

type Request

type Request struct {
	Scope     Scope
	Selection Selection
	// ArchivedBefore picks threads whose main session was archived strictly
	// before it; nil picks every archived thread.
	ArchivedBefore *time.Time
	// RootIDs, when set, narrows the picked threads to these main sessions (a retry).
	RootIDs []string
	// CountedAt, when set, is the instant of the dry run being confirmed: a
	// session archived after it is planned as still in the list, so the cleanup
	// deletes no more than that dry run counted.
	CountedAt *time.Time
}

Request is one cleanup: what to delete, from which threads.

type Run

type Run struct {
	ID             string
	ProfileID      string
	Scope          Scope
	Selection      Selection
	ArchivedBefore *time.Time
	// CountedAt is the plan's count: nothing archived later is in the run.
	CountedAt time.Time
	State     RunState
	// Total is every session the run planned to delete.
	Total int
	// Deleted counts sessions removed (or already gone).
	Deleted int
	// Skipped counts sessions restored or moved after the plan, so kept.
	Skipped       int
	FailedThreads int
	Failures      []Failure
	StartedAt     time.Time
	FinishedAt    *time.Time
}

Run is a snapshot of one cleanup run.

func (Run) Done

func (r Run) Done() int

Done counts the planned sessions the run has settled.

type RunState

type RunState string

RunState is where one cleanup run is.

const (
	// RunRunning deletes in the background.
	RunRunning RunState = "running"
	// RunCompleted deleted every planned session.
	RunCompleted RunState = "completed"
	// RunPartial deleted some sessions; Failures name the threads that kept the rest.
	RunPartial RunState = "partial"
	// RunFailed deleted nothing; Failures say why.
	RunFailed RunState = "failed"
	// RunStopped ended on request or at daemon shutdown before finishing.
	RunStopped RunState = "stopped"
)

type Scope

type Scope struct {
	ProfileID     string
	WorkspaceID   string
	AllWorkspaces bool
}

Scope is the archived history one cleanup reads: one profile, in one workspace (or the user selector) or across all its workspaces.

type Selection

type Selection struct {
	Everything bool
	Children   bool
	Subagents  bool
}

Selection is what one cleanup deletes. Everything deletes each picked archived thread whole, so it includes the other two; without it, Children and Subagents thin the threads out and keep their main sessions.

func (Selection) Empty

func (s Selection) Empty() bool

Empty reports a selection that deletes nothing.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service plans cleanups and runs at most one per profile in the background.

func NewService

func NewService(lister Lister, deleter Deleter, opts ...Option) (*Service, error)

NewService builds a cleanup service over the archived history and the delete path.

func (*Service) Current

func (s *Service) Current(profileID string) (Run, bool)

Current returns the profile's running or most recent run.

func (*Service) Preview

func (s *Service) Preview(ctx context.Context, request Request) (Plan, error)

Preview plans a cleanup without deleting anything.

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context) error

Shutdown stops admitting runs, stops the running ones, and waits for them.

func (*Service) Start

func (s *Service) Start(ctx context.Context, request Request) (Run, Plan, error)

Start plans a cleanup and deletes it in the background. The plan is frozen: sessions archived after it are never picked up by this run.

func (*Service) Stop

func (s *Service) Stop(ctx context.Context, profileID, runID string) (Run, error)

Stop asks the profile's running cleanup to stop after the deletes in flight and waits for it to settle; what it deleted stays deleted. A non-empty runID stops only that run, so a caller that checked one run never stops another that replaced it.

type Thread

type Thread struct {
	RootID      string
	WorkspaceID string
	Title       string
	Delete      []string
	Counts      Counts
}

Thread is one picked archived thread and the sessions deleted from it, deepest first, so a failure part-way leaves every remaining session reachable from the thread's main session.

type WorkspaceTotal

type WorkspaceTotal struct {
	WorkspaceID string
	Total       int
}

WorkspaceTotal is how many sessions a cleanup deletes in one workspace.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL