Documentation
¶
Overview ¶
A daily spend cap that pauses the sessions Caprock started.
This is the first paid feature, and the argument for it is one sentence: a runaway loop stops at $40 instead of finishing at $400. Everything else this product does is observation — you look and you learn something. A cap is the first thing that acts while you are asleep.
What it may and may not touch ¶
Only sessions Caprock spawned. [Rule 7] predates this feature and survives it: a session you started in your own terminal is watched and never signalled, however much it is costing. That is not a limitation to be worked around later — it is the reason anyone lets this daemon near their machine.
Pause, not kill ¶
A paused session is SIGSTOP'd and can be resumed with everything intact: the conversation, the working directory, the context it has built up. Killing would be simpler and would throw away work that has already been paid for, which is a strange thing for a tool that exists to stop waste.
What happens when the licence lapses ¶
The cap keeps working; the threshold becomes read-only. Switching off someone's overspend protection the moment their card expires is the worst possible timing for it — they are least able to notice and most likely to be hurt. Selling protection and then removing it on a technicality is not what was sold. See ADR-022 for why the key is a convenience rather than a lock.
[Rule 7]: ../../CLAUDE.md
Index ¶
- Constants
- func Fresh(w Window, now time.Time) (bool, string)
- func Suggest(medianDayUSD float64) float64
- type Event
- type Guard
- type Notify
- type PausedSession
- type Settings
- type Signaller
- type Spend
- type Window
- type WindowEvent
- type WindowGuard
- func (g *WindowGuard) Check(ctx context.Context) bool
- func (g *WindowGuard) Load(st WindowState)
- func (g *WindowGuard) Release(ctx context.Context, sessionID string)
- func (g *WindowGuard) ResetLatch(ctx context.Context)
- func (g *WindowGuard) ResumeDue(ctx context.Context) []string
- func (g *WindowGuard) Run(ctx context.Context, every time.Duration)
- func (g *WindowGuard) State() WindowState
- type WindowSettings
- type WindowSignaller
- type WindowState
Constants ¶
const ( MinWindowPct = 50 MaxWindowPct = 99 )
MinWindowPct and MaxWindowPct bound the setting. Below half the stop would fire on an ordinary morning; at 100% Claude Code has already stopped itself.
const ( WindowFiveHour = "five_hour" WindowSevenDay = "seven_day" )
The plan windows by the names the status line feed stores them under.
const DefaultWindowPct = 90
DefaultWindowPct is the share used when the user has never chosen one: the same 90% at which the free alert speaks.
const FreshFor = 10 * time.Minute
FreshFor is how old a plan-window sample may be and still pause anything.
Claude Code sends one per assistant message while a session runs, so a running owned session — the only thing this guard can pause — keeps the figures minutes old at most. Ten minutes leaves room for a long tool call.
const ResumeGrace = time.Minute
ResumeGrace is how long after a window's reset the paused sessions resume. A clock a few seconds apart from Anthropic's would otherwise resume a turn into the last moments of the old window.
Variables ¶
This section is empty.
Functions ¶
func Fresh ¶ added in v0.92.0
Fresh reports whether a sample may be acted on at now, and if not, why.
func Suggest ¶
Suggest proposes a limit from the user's own history rather than an invented round number.
A blank field asking for a dollar figure gets one of two answers: a number so high it never fires, or one so low it fires on a normal Tuesday. Both teach the person that the feature is useless. Their own median day is the only figure that is neither, and it is a fact rather than a guess (rule 6).
The multiplier is deliberate slack: a cap set at the median fires half the time by definition. 2x the median day is "today is unusual", which is what someone actually wants to be told.
Types ¶
type Event ¶
type Event struct {
At time.Time
LimitUSD float64
SpendUSD float64
// Paused is the sessions that were actually paused, which is not the same
// as the sessions that were running: one may exit between the decision and
// the signal.
Paused []string
}
Event is what happened, for the notifier and for the log.
type Guard ¶
type Guard struct {
Settings func() Settings
Spend Spend
Sig Signaller
Notify Notify
Log *slog.Logger
// Now is injected so the day boundary is testable. A cap that resets at
// the wrong hour is a cap that fires at breakfast.
Now func() time.Time
// contains filtered or unexported fields
}
Guard watches the day's spend and pauses owned sessions when it crosses the limit.
func (*Guard) Check ¶
Check evaluates the cap and pauses if it has been crossed. It is called after every priced turn, so it must be cheap and must not block the write path.
Returns true when it fired.
type Notify ¶
Notify is called once when the cap fires. Delivery is somebody else's problem — this package decides *that* it happened, never how it is announced.
type PausedSession ¶ added in v0.92.0
type PausedSession struct {
SessionID string `json:"session_id"`
// ResumeAt is the reset (unix seconds) after which it is resumed: the
// latest reset among the windows over the share when it was paused.
ResumeAt int64 `json:"resume_at"`
// Window is the window that resets last, the one the notice names.
Window string `json:"window"`
PausedAt int64 `json:"paused_at"` // unix ms
}
PausedSession is one session the guard paused and will resume.
type Settings ¶
type Settings struct {
// LimitUSD is the day's ceiling. Zero or negative means the cap is off,
// which is the default: a threshold nobody chose is a threshold that will
// eventually stop work for a reason its owner cannot explain.
LimitUSD float64
}
Settings is the cap's own configuration, read fresh on every check so a change takes effect without a restart.
type Signaller ¶
type Signaller interface {
// PauseOwned pauses the session if Caprock owns it, and reports whether it
// did. A session it does not own must be refused here rather than filtered
// by the caller — the rule belongs next to the thing that enforces it.
PauseOwned(sessionID string) (bool, error)
// OwnedRunning lists the sessions Caprock started that are still running.
OwnedRunning() []string
}
Signaller pauses one owned session. Implemented by agents.Manager; an interface so this package can be tested without a PTY, and so it cannot reach anything else on the manager.
type WindowEvent ¶ added in v0.92.0
type WindowEvent struct {
// Kind is "paused" or "resumed".
Kind string `json:"kind"`
At int64 `json:"at"` // unix ms
// Window and UsedPct name the window that resets last among those over
// the share; ResumeAt is its reset (unix seconds).
Window string `json:"window,omitempty"`
UsedPct float64 `json:"used_percentage,omitempty"`
Pct int `json:"threshold_pct,omitempty"`
ResumeAt int64 `json:"resume_at,omitempty"`
// Sessions are the ones actually paused or resumed.
Sessions []string `json:"sessions"`
}
WindowEvent is a stop or a resume, for the notifier and the dashboard.
type WindowGuard ¶ added in v0.92.0
type WindowGuard struct {
Settings func() WindowSettings
Windows func(ctx context.Context) ([]Window, error)
Sig WindowSignaller
// Notify announces a stop or a resume. Optional.
Notify func(ctx context.Context, ev WindowEvent)
// Save persists the state after every change. Optional.
Save func(ctx context.Context, st WindowState) error
Log *slog.Logger
Now func() time.Time
// contains filtered or unexported fields
}
WindowGuard pauses owned Claude Code sessions when a plan window crosses the chosen share, and resumes them after its reset.
func (*WindowGuard) Check ¶ added in v0.92.0
func (g *WindowGuard) Check(ctx context.Context) bool
Check pauses the owned Claude Code sessions if a fresh window has crossed the share and has not fired already. Returns whether it paused anything.
func (*WindowGuard) Load ¶ added in v0.92.0
func (g *WindowGuard) Load(st WindowState)
Load restores the state a previous run saved.
func (*WindowGuard) Release ¶ added in v0.92.0
func (g *WindowGuard) Release(ctx context.Context, sessionID string)
Release forgets a session the user signalled by hand — resumed, paused again or ended — so the guard does not resume it later behind their back.
func (*WindowGuard) ResetLatch ¶ added in v0.92.0
func (g *WindowGuard) ResetLatch(ctx context.Context)
ResetLatch lets the stop fire again in the current window. Used when the share changes: a share lowered after a stop is a new instruction. Sessions already paused stay on their resume schedule.
func (*WindowGuard) ResumeDue ¶ added in v0.92.0
func (g *WindowGuard) ResumeDue(ctx context.Context) []string
ResumeDue resumes every session whose window has reset, and returns them. A session that has ended in the meantime is dropped quietly.
func (*WindowGuard) Run ¶ added in v0.92.0
func (g *WindowGuard) Run(ctx context.Context, every time.Duration)
Run resumes what is due and re-checks the windows every interval until ctx ends. The check also runs whenever a new sample arrives; this loop is what resumes sessions after a reset, when no session is left to send one.
func (*WindowGuard) State ¶ added in v0.92.0
func (g *WindowGuard) State() WindowState
State returns a copy of the current state.
type WindowSettings ¶ added in v0.92.0
type WindowSettings struct {
// Pct is the share of a window that pauses; zero or less is off.
Pct int
// Licensed is whether Premium is active. Without it nothing is paused.
Licensed bool
}
WindowSettings is read fresh on every check.
type WindowSignaller ¶ added in v0.92.0
type WindowSignaller interface {
// PauseOwned and ResumeOwned act only on a session Caprock owns, and
// report whether they did; an id the manager does not own is refused there.
PauseOwned(sessionID string) (bool, error)
ResumeOwned(sessionID string) (bool, error)
// OwnedRunningKind lists the running sessions Caprock started with the
// named agent ("claude").
OwnedRunningKind(kind string) []string
}
WindowSignaller is the slice of the session manager this guard may use.
type WindowState ¶ added in v0.92.0
type WindowState struct {
Paused []PausedSession `json:"paused,omitempty"`
Latched []latch `json:"latched,omitempty"`
// Last is the most recent stop, for the notice.
Last *WindowEvent `json:"last,omitempty"`
}
WindowState is what the guard persists between daemon runs.