idle

package
v0.6.76 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0, Apache-2.0, MIT Imports: 12 Imported by: 0

Documentation

Overview

Package idle is the daemon's suspend detector (DBX-47, section 4.3a). It evaluates activity signals, reports IdleState changes and emits SuspendRequested once the instance has been idle for the grace period (or a ceiling is reached). The gateway executes the suspend.

Index

Constants

View Source
const (
	DefaultActivityWindow     = 2 * time.Minute
	DefaultCPUThreshold       = 5.0
	DefaultGracePeriod        = 5 * time.Minute
	DefaultMaxDeferral        = 12 * time.Hour
	DefaultMaxContinuousAwake = 24 * time.Hour
)

Policy defaults (IdlePolicy).

Variables

This section is empty.

Functions

This section is empty.

Types

type CPUSampler

type CPUSampler interface {
	Sample(now time.Time) (total float64, byPgid map[int]float64)
}

CPUSampler reports CPU usage since the previous sample, as percent of one core, in total and per process group.

type Detector

type Detector struct {

	// SettledOffset settles in-flight entities and returns the log offset through which everything is settled.
	SettledOffset func() uint64
	// contains filtered or unexported fields
}

Detector is the idle state machine.

func New

func New(c clock.Clock, policy Policy, src Sources, emit Emitter) *Detector

New returns a Detector.

func (*Detector) Evaluate

func (d *Detector) Evaluate() *devboxv1.IdleState

Evaluate runs one evaluation and emits events. It's called on a ticker (and directly by tests).

func (*Detector) Reset

func (d *Detector) Reset()

Reset restarts activity timers (memory restore detected, or a fresh boot).

func (*Detector) SetLeases

func (d *Detector) SetLeases(leases []*devboxv1.Lease, generation uint64) []*devboxv1.Lease

SetLeases replaces the leases when generation is newer than the last applied one. Deferrals are clamped to now + maxDeferral; expired leases are dropped.

func (*Detector) SetPolicy

func (d *Detector) SetPolicy(p Policy)

SetPolicy replaces the policy (Initialize).

func (*Detector) State

func (d *Detector) State() *devboxv1.IdleState

State returns the last evaluated state.

type Emitter

type Emitter interface {
	IdleState(*devboxv1.IdleState)
	SuspendRequested(*devboxv1.SuspendRequested)
}

Emitter receives detector events.

type Policy

type Policy struct {
	ActivityWindow     time.Duration
	CPUThresholdPct    float64
	GracePeriod        time.Duration
	MaxDeferral        time.Duration
	MaxContinuousAwake time.Duration
}

Policy is the resolved idle policy.

func PolicyOf

func PolicyOf(p *devboxv1.IdlePolicy) Policy

PolicyOf resolves an IdlePolicy with defaults.

type ProcSampler

type ProcSampler struct {
	Root string
	// Exclude reports processes that don't count (the daemon, browser helpers); called with pid and pgid.
	Exclude func(pid, pgid int) bool
	// contains filtered or unexported fields
}

ProcSampler samples /proc/<pid>/stat CPU times for every process outside the daemon.

func (*ProcSampler) Sample

func (s *ProcSampler) Sample(now time.Time) (float64, map[int]float64)

Sample implements CPUSampler. The first sample establishes a baseline and reports zero.

type Sources

type Sources struct {
	// Agent returns agent turn and pending-interaction reasons.
	Agent func(now time.Time) []*devboxv1.BusyReason
	// TerminalJobs returns PTY foreground jobs.
	TerminalJobs func() []TerminalJob
	// StreamActivity returns the last byte time of each interactive stream source (terminal, vnc, preview, cdp).
	StreamActivity func() map[string]time.Time
	// CPU samples process CPU outside the daemon.
	CPU CPUSampler
}

Sources provide the signals.

type TerminalJob

type TerminalJob struct {
	TerminalID string
	Command    string
	// Pgid is the job's process group (for CPU attribution); 0 when unknown.
	Pgid int
}

TerminalJob is a PTY foreground job.

Jump to

Keyboard shortcuts

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