Documentation
¶
Overview ¶
Package sleep puts idle agents to sleep: an idle Claude holds a few hundred megabytes with its MCP bridges whatever it does, and a machine that never lets one go runs out of memory. A slept conversation stays a chat; its next prompt resumes it. Every five minutes the sweep looks at the agent tabs, picks Claude agents quiet for longer than a threshold (shorter under memory pressure) and stops their process trees, keeping the pane's shell, the tab and the transcript.
Index ¶
- Constants
- func Pick(candidates []Candidate, now float64, underPressure bool, c Config) []string
- func ResumeNeedsActivity(persistedState string, c Config) bool
- func ShouldResumeAtStartup(persistedState string, lastActivityAt *float64, now float64, c Config) bool
- type Candidate
- type Config
- type EnvNames
- type Panes
- type Sleeper
- func (s *Sleeper) Candidates() []Candidate
- func (s *Sleeper) Forget(path string)
- func (s *Sleeper) ForgetTab(tabID string)
- func (s *Sleeper) Kill(tabID, session string) int
- func (s *Sleeper) Run(ctx context.Context, period time.Duration, ...)
- func (s *Sleeper) Sweep() (slept []string, underPressure bool)
- type Tab
- type Tabs
Constants ¶
const ( // SweepEvery is how often the sweep runs; the first runs one period after the start. SweepEvery = 5 * time.Minute // PressureRatio: memory is short when less than this share of it is available. PressureRatio = 0.15 )
Variables ¶
This section is empty.
Functions ¶
func Pick ¶
Pick is which agents to put to sleep, oldest first. Working, asking and background-running agents never sleep, nor Codex; an unread answer (ready-for-review) keeps its agent unless memory is short.
func ResumeNeedsActivity ¶
ResumeNeedsActivity says whether ShouldResumeAtStartup looks at the conversation's last activity at all: finding it reads the transcript, and a state that decides alone, or no window, needs no read.
func ShouldResumeAtStartup ¶
func ShouldResumeAtStartup(persistedState string, lastActivityAt *float64, now float64, c Config) bool
ShouldResumeAtStartup: only a conversation that was mid-turn when the runtime stopped gets its agent back, plus, when the window is set, the ones active within it. lastActivityAt is nil when unknown.
Types ¶
type Candidate ¶
type Candidate struct {
TabID string
Provider string
CliState string
// LastActivityAt is the newest of the last user or assistant message and the last hook event other than
// SessionStart, in epoch ms.
LastActivityAt float64
BackgroundTasks int
}
Candidate is an agent tab the sweep may put to sleep.
type Config ¶
type Config struct {
IdleMs, PressureIdleMs, ResumeRecentMs float64
}
Config are the thresholds in milliseconds; 0 turns a rule off.
type EnvNames ¶
type EnvNames struct {
// IdleMin: an idle agent sleeps after this long (default 360; 0 never).
IdleMin string
// PressureMin: under memory pressure the threshold drops to this, and unread answers may sleep too (default 20).
PressureMin string
// ResumeRecentMin: at startup a conversation that was not mid-turn is resumed only if it was active this recently
// (default 0: none).
ResumeRecentMin string
}
EnvNames are the variables the thresholds are read from, in minutes; the deployment chooses the names.
type Panes ¶
type Panes interface {
ReadAllPanes() (map[string]tmux.PaneInfo, bool)
Capture(name string) (string, bool)
HasSession(name string) bool
PanePID(name string) int
}
Panes is what the sweep asks tmux; *tmux.Tmux has it.
type Sleeper ¶
type Sleeper struct {
Tabs Tabs
Panes Panes
// Claude tells whether Claude runs under a pane and where its transcripts are.
Claude *claude.Detector
// Activity and Background read transcripts; both keep a cache per transcript (see Forget).
Activity *claude.ActivityReader
Background *background.Reader
// Getenv and Env read the thresholds at every sweep.
Getenv func(string) string
Env EnvNames
// MemRatio is the available share of memory (proc.MemAvailableRatio by default); unreadable is no pressure.
MemRatio func() (float64, bool)
// KillTree stops a pane's processes (proc.KillTree with a 3 s grace by default) and returns how many it stopped.
KillTree func(panePID int) int
// MarkGone tells the state machine the tab's agent is gone (it goes inactive silently, its launch ends).
MarkGone func(tabID string)
Now func() time.Time
// contains filtered or unexported fields
}
Sleeper runs the sweep.
func (*Sleeper) Candidates ¶
Candidates are the agent tabs that may sleep, looked at a few at a time against one tmux snapshot.
func (*Sleeper) ForgetTab ¶
ForgetTab drops what is cached of the transcript the sweep read for a tab that is gone.
func (*Sleeper) Kill ¶
Kill stops the agent of a tmux session and everything it started, keeping the pane's shell, then marks the tab's agent gone. It returns how many processes it stopped; 0 without the session or its pane.
type Tab ¶
type Tab struct {
TabID string
PanelType string
Provider string
CliState string
TmuxSession string
// JSONLPath is the transcript when known; AgentSessionID finds it otherwise.
JSONLPath string
AgentSessionID string
// LastEventName and LastEventAt (epoch ms, 0 when none) are the last work-state event.
LastEventName string
LastEventAt float64
}
Tab is the state machine's view of an agent tab.