sleep

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Pick(candidates []Candidate, now float64, underPressure bool, c Config) []string

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

func ResumeNeedsActivity(persistedState string, c Config) bool

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.

func ReadConfig

func ReadConfig(getenv func(string) string, names EnvNames) Config

ReadConfig reads the thresholds; the sweep calls it every time, so a change needs no restart.

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

func (s *Sleeper) Candidates() []Candidate

Candidates are the agent tabs that may sleep, looked at a few at a time against one tmux snapshot.

func (*Sleeper) Forget

func (s *Sleeper) Forget(path string)

Forget drops what is cached of a transcript whose tab is gone.

func (*Sleeper) ForgetTab

func (s *Sleeper) ForgetTab(tabID string)

ForgetTab drops what is cached of the transcript the sweep read for a tab that is gone.

func (*Sleeper) Kill

func (s *Sleeper) Kill(tabID, session string) int

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.

func (*Sleeper) Run

func (s *Sleeper) Run(ctx context.Context, period time.Duration, report func(slept []string, underPressure bool))

Run sweeps every period (SweepEvery when 0) until ctx ends; report gets each sweep's result.

func (*Sleeper) Sweep

func (s *Sleeper) Sweep() (slept []string, underPressure bool)

Sweep puts the agents picked now to sleep and returns their tabs, and whether memory was short.

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.

type Tabs

type Tabs interface {
	// AgentTabs are the tabs whose panel runs an agent.
	AgentTabs() []Tab
	// Current is a tab's state now (false when it is gone), asked again right before its agent is stopped.
	Current(tabID string) (Tab, bool)
}

Tabs is where the sweep reads the agent tabs from.

Jump to

Keyboard shortcuts

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