initcap

package
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 13 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 initcap is a person's optional cap on what init spends, in the unit they pay in: an amount of money, a share of their plan's usage as a ceiling, or weighted tokens. Its guard starts no session that would pass the cap: once one is refused, init starts nothing more, sessions already running finish, and the guard says what was spent in the cap's unit and how to continue. Its only I/O is the agent's own usage reading and the lines it prints for init to the writer it is given.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Again

func Again(words []string, recovering bool) string

Again is the line that continues a run the cap stopped, from the words typed after init: "physa init" and those words without --cap, or, when recovering was stopped in a bare run, "physa init --recover" and the words of the bare run --recover takes (--agent, --write, --push, --pr, --hooks and --json).

func Sum

func Sum(rows []initrecord.Spent) spend.Spend

Sum is what init's record has spent, phase by phase, together: a capped run's Base when it does not gather anew.

Types

type Cap

type Cap struct {
	Kind Kind
	// MicroUSD is a Money cap, in millionths of a dollar.
	MicroUSD int64
	// Percent is a Share cap, above 0 and at most 100.
	Percent float64
	// Tokens is a Tokens cap, in weighted tokens.
	Tokens int64
}

Cap is what a person caps init's spend at. The zero Cap is none.

func Parse

func Parse(s string) (Cap, error)

Parse reads a cap as a person gives it: "" is none; "$5" or "5usd" (any case, to the cent) is money; "80%" (above 0, at most 100) is a share of the plan's usage; anything else is weighted tokens, read as an appetite is (200k, 1.5m, 50000). Every amount is above zero.

func Read

func Read(s string, starts bool) (Cap, error)

Read is Parse for init's line: a cap with a phase that starts no session (starts false, as --place) is refused too.

func (Cap) Show

func (c Cap) Show(s spend.Spend) string

Show is s in c's unit: money leads with dollars, tokens beside ("$0.95 (240k tokens)"), and says no cost is known when s is Unpriced; tokens and a share show s as spend does ("240k tokens (about $0.95)"), a share's usage being shown from its reading.

func (Cap) String

func (c Cap) String() string

String is c as a person would write it: "$5.00", "80%", "2M tokens", or "none".

type Guard

type Guard struct {
	// Cap is what init's spend is capped at.
	Cap Cap
	// Agent is what sessions start on: the metered agent.
	Agent adapter.Adapter
	// Raw is the agent a share's usage is read from; nil means Agent.
	Raw adapter.Adapter
	// Cache prices a session's budget for a money cap; nil prices nothing.
	Cache *spend.Cache
	// AgentName is the agent whose default model a budget is priced at.
	AgentName string
	// Base is what init's record had spent before this run.
	Base spend.Spend
	// Spent is what this run has spent so far; nil means Agent's, when Agent
	// is a *spend.Meter, else nothing.
	Spent func() spend.Spend
	// contains filtered or unexported fields
}

Guard is an adapter that starts a session on Agent only when the session's budget would not pass Cap. Once it refuses a start it refuses every later one, so a stopped run does not skip ahead to smaller sessions; sessions already running are never cancelled, so a run can end past its cap by what they spend over their budgets. It is safe for concurrent use.

func Start

func Start(ctx context.Context, r Run) (*Guard, int)

Start holds a capped run's sessions to its cap from here on and checks it before anything starts. It returns the guard, nil when the agent cannot be resolved (the phase then says why), and the code to exit with, 0 to go on: 2 when the agent cannot report the usage a share needs, saying why, or init's record cannot be read; 1 when the cap is already reached, saying how to continue.

func (*Guard) Check

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

Check runs before anything starts. For a share it reads the plan's usage once: an agent that cannot report it gives an error wrapping adapter.ErrNoUsage that says why, and usage already at or over the cap stops the guard and wraps adapter.ErrCapped. For money and tokens, init's record already at the cap stops the guard the same way.

func (*Guard) End

func (g *Guard) End() string

End is what initrun prints after "physa init: " when a capped run ended without being stopped: what init has spent, in the cap's unit, against the cap.

func (*Guard) Finish

func (g *Guard) Finish(w io.Writer, code int, again string) int

Finish says on w, after "physa init: ", how a capped run with code ended against its cap and returns its code: when the guard stopped it, Reached with again and a code of at least 1; otherwise End and code. A nil guard says nothing.

func (*Guard) Name

func (g *Guard) Name() string

Name is Agent's name.

func (*Guard) Reached

func (g *Guard) Reached(continueLine string) string

Reached is what initrun prints after "physa init: " when the guard stopped a run: the cap was reached, nothing more was started, sessions running finished, what init has spent in the cap's unit (or the plan's usage), and how to continue with continueLine.

func (*Guard) Start

func (g *Guard) Start(ctx context.Context, spec adapter.Spec) (adapter.Session, error)

Start starts spec on Agent unless its budget would pass the cap: for money and tokens, when what init has spent, the budgets of sessions running and this one's together pass it; for a share, when the plan's usage, read now, is at or over it, or cannot be read. A refused start returns an error wrapping adapter.ErrCapped, and so does every start after it.

func (*Guard) StartLine

func (g *Guard) StartLine() string

StartLine is what initrun prints once before the first session when a cap is set, after "physa init: ": the cap, that init stops before a session that would pass it, and what init's record has spent so far in the cap's unit, or, for a share, the plan's usage as read. A nil guard, a run with no cap, says init runs to the end.

func (*Guard) Stopped

func (g *Guard) Stopped() bool

Stopped reports whether the guard has refused a start; a nil guard never has.

func (*Guard) Warnings

func (g *Guard) Warnings() []string

Warnings is what the raw agent warns of before any session.

func (*Guard) Window

func (g *Guard) Window() int

Window is the window Agent states, 0 when it states none.

type Kind

type Kind int

Kind is the unit a cap is given in.

const (
	// None is no cap: init runs to the end.
	None Kind = iota
	// Money is an amount of money, in millionths of a dollar.
	Money
	// Share is a share of the plan's usage, as a percent ceiling.
	Share
	// Tokens is an amount of weighted tokens.
	Tokens
)

type Run

type Run struct {
	// Cap is the run's cap.
	Cap Cap
	// Agent resolves the agent sessions start on: the metered agent.
	Agent func() (adapter.Adapter, error)
	// Cache prices budgets.
	Cache *spend.Cache
	// Record is init's record, whose spend so far is the guard's Base unless
	// Fresh: a run that gathers anew clears it.
	Record Spending
	Fresh  bool
	// Again is the line that continues the run when the cap is already reached.
	Again string
	// Say prints the cap's StartLine; a bare run says it in its own first line.
	Say bool
	// Out is where Start prints, each line after "physa init: ".
	Out io.Writer
}

Run is what Start needs of a capped run of init.

type Spending

type Spending interface {
	Spending() ([]initrecord.Spent, error)
}

Spending is init's record's spend, phase by phase.

Jump to

Keyboard shortcuts

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