cap

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 4 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

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

This section is empty.

Variables

This section is empty.

Functions

func Suggest

func Suggest(medianDayUSD float64) float64

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

func (g *Guard) Check(ctx context.Context) bool

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.

func (*Guard) FiredOn

func (g *Guard) FiredOn() string

FiredOn reports the day the cap last fired, for the API to say so.

func (*Guard) Reset

func (g *Guard) Reset()

Reset clears the fired state. Used when the limit changes: raising it after a pause should let work continue, not leave the cap latched until midnight.

type Notify

type Notify func(ctx context.Context, ev Event)

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 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 Spend

type Spend func(ctx context.Context) (float64, error)

Spend reports what today has cost so far, in the daemon's local day.

Jump to

Keyboard shortcuts

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