forecast

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 8 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 forecast measures how fast a fleet of accounts is spending its quota and decides what that rate implies.

It is pure arithmetic. Nothing here opens a clock, a filesystem, an environment variable or a store: every input arrives as an argument, in the manner of internal/view. Nothing here polls either -- the readings it works from are the ones the daemon was already taking.

No strategy.Thresholds value may enter this package. Hover rewrites thresholds every tick, and a forecast that moved because a ranking moved would report a change in the fleet that never happened.

That rule is now checked rather than only written down, in both directions: TestTheForecastDoesNotImportTheEngine here, and TestTheEngineDoesNotImportTheForecast in internal/strategy. Each half is asked from inside the package whose own sources decide it, because `go list -deps` runs in a subprocess and its answer is cached against the asking binary's inputs -- a half asked from the other side stays green through the very edit it exists to catch.

The separation costs something real and it is worth naming so nobody rediscovers it as a bug. The fleet answer this package computes is invariant under routing: replacing the simulation's live-account rule with its inverse, and then with a fixed account, moved AccountsNeeded, NeededBy, PointsLeft and every verdict by nothing at all. So the engine could not use this measurement to route better even if it could see it. What routing does move is perishability -- quota that expires before anyone reaches it -- and that is derived inside the engine, from each account's own reading, by hoverStranded.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountRow

type AccountRow struct {
	UUID string

	// Window is the window Left and Burn describe -- the account's least-room
	// weekly one, which is the window the fleet's points are counted on.
	//
	// HasWindow is whether the account reported a weekly window at all. When
	// it is false, Window, Left and Burn carry NO QUANTITY -- not an unknown
	// one -- and the account is out of PointsLeft and PointsTotal. It still
	// gets a row, because it is still in the fleet and the run still has
	// something to say about when it goes out; what it has no weekly figure
	// for is this column. Naming its five-hour window here instead would put a
	// five-hour room and a five-hour rate in a column that is summed into the
	// weekly axis above it, and those are the two quantities this package adds
	// nowhere else.
	Window    usage.WindowName
	HasWindow bool

	// Left is 100 minus that window's utilization, read from the CURRENT
	// snapshot. The rows with a window sum to Fleet.PointsLeft, which is what
	// lets a reader check one figure against the other.
	Left float64

	// Burn is this account's own consumption over the SHARED span the axis
	// figure was divided by, so the column sums to the axis figure above it
	// rather than to something larger. A measured zero is a reading -- the
	// account was up, was polled and did not burn -- and Known false is not
	// one.
	Burn Band

	// EmptyAt is the moment the RUN first found this account out, and HasEmpty
	// is whether it ever did. It is the run's answer rather than a second
	// arithmetic: a per-account "if it carried the fleet alone" figure would
	// disagree with the run on the same page, because the run burns whichever
	// account is live at the fleet rate and spends the account with the most
	// room first.
	EmptyAt  time.Time
	HasEmpty bool

	// OutNow is whether the account is already out at now, which is a fact
	// about this minute rather than a projection.
	OutNow bool
}

AccountRow is one account's line in the per-account block: which window binds it, how much of that window is left, how fast this account itself spent it, and when the run first found the account out.

type Axis

type Axis struct {
	// Burn is the fleet's measured consumption of this axis.
	Burn Band

	// Replenish is what the axis gives back, in percentage points per hour:
	// one window's worth per readable account per window length.
	//
	// It is an EXPLANATION of the verdict and never its source. It counts an
	// account that is out on the other axis, which supplies nothing, and a
	// window with no reported reset, which supplies its current room once and
	// never again; the run below counts neither. Presenting it as the decision
	// would promise capacity the fleet cannot reach.
	Replenish float64

	// Verdict is the answer, and it comes from the run and from nothing else.
	Verdict Verdict

	// DryAt is when the run's own burn left no account able to take work, and
	// HasDryAt is whether there is such a moment. They are meaningful only
	// when Verdict is VerdictRunsDry.
	DryAt    time.Time
	HasDryAt bool
}

Axis is one axis's answer: how fast it is being spent, how fast it comes back, and whether that holds.

type Band

type Band struct {
	Low, High float64
	Known     bool
}

Band is a measured rate and the upper bound the measurement cannot rule out, both in percentage points per hour.

It is an interval rather than a number because the endpoint reports whole percents -- thirty live values read across six accounts on 2026-08-25 were integers without exception -- so a measured rise of three points is anything from just over two to just under four. Low is what gets printed. High is what a claim that an axis holds has to survive, and that is the whole defence against a thin basis: a coarse measurement produces a wide band, and a wide band declines to promise anything.

Known false is not a rate of zero. A fleet nobody has enough readings for is not a fleet burning nothing, and the two must never render the same.

type Basis

type Basis struct {
	// Window is how far back a rate is measured, which is fixed.
	Window time.Duration

	// Observed is the span the readings actually reach across, which is
	// usually shorter -- a daemon started twenty minutes ago has twenty
	// minutes of evidence -- and is what every rate here was divided by.
	Observed time.Duration

	// Readings is how many samples fell inside the window across the fleet.
	Readings int

	// Accounts is the fleet size, before any of the exclusions below.
	Accounts int

	// Unmeasured is readable accounts that cleared no contribution gate, and
	// they are absent from every sum rather than counted as burning nothing.
	Unmeasured int

	// Unreadable is accounts whose current reading carried no window at all.
	// They are counted here and NOWHERE else: an unreadable account is
	// excluded because nobody can see it, not because it is spent, and an
	// account is counted under one of these two headings or neither, so a
	// reader can add them against Accounts.
	Unreadable int

	// Ineligible is accounts the rotation cannot hand work to -- see
	// Input.Eligible. They are excluded before anything is measured, so they
	// appear here and in no other figure on this page, and Accounts less this
	// and the two headings above it is what was actually measured.
	Ineligible int

	// Known is false when nothing was measured at all. It is not a rate of
	// zero: a fleet nobody has enough readings for is not a fleet burning
	// nothing.
	Known bool
}

Basis is what the measurement was made from, so a reader can weigh it. Every surface that prints a rate prints this beside it: a four-hour rate is a speedometer, and a speedometer read for twenty minutes says less than one read for four hours.

type CreditFleet

type CreditFleet struct {
	// Currency is the ISO code every contributing account agreed on. Amounts in
	// two currencies do not add, so a figure exists only when they match.
	Currency string

	// SpendPerHour is the fleet's measured spend in Currency's MAJOR unit per
	// hour -- dollars an hour for USD, not cents. usage.ExtraUsage's accessors
	// have already done that conversion; nothing here divides by 100.
	SpendPerHour float64

	// DryAt is when the summed balance reaches zero at SpendPerHour.
	DryAt time.Time

	// Spent is the money the rate was measured FROM, in Currency's major unit,
	// and Observed is the span it was measured over. They are this row's basis,
	// and they are carried because a rate is only ever as good as the quantity
	// it divided.
	//
	// used_credits arrives in whole minor units, so a fleet a few cents into
	// its billing month divides a numerator of one or two CENTS, and one cent
	// entering or leaving the trailing window changes the rate by half.
	// Measured 2026-09-01 on one live account: three readings ninety minutes
	// apart put its dry date at 2046, 2037-02-26 and 2036-04-19. Every one of
	// those was correctly computed. What no surface could say was how thin the
	// evidence under them was, because this pair was measured and then dropped.
	//
	// The existing gates do not cover it and are not meant to: minSamples and
	// minCover both ask about the READINGS, and there were twenty-seven of them
	// spread over nearly four hours. Neither asks whether the quantity measured
	// is large enough to divide by.
	Spent    float64
	Observed time.Duration

	// Readings is how many credit-carrying samples the sum ran over, across the
	// fleet. It is what Basis.Readings is for the window axes, and it is here
	// rather than there because Basis counts samples carrying a WINDOW, which a
	// seat metered only in money never has.
	Readings int

	Known bool
}

CreditFleet is the paid-usage answer, and it is deliberately one date.

There is no verdict here and no replenishment figure, because credits are not a window: the endpoint reports no renewal boundary for them, so there is nothing for a rate to be sustainable against and no claim of the form "this lasts the month" can be made from what can be seen. A balance divided by a spend rate, with nothing coming back, is the whole of it. The surfaces render the replenishment cell as the glyph for "no such quantity here" rather than the one for "could not be read".

Known false is a refusal and never a zero. creditFleet names every way this figure can fail to exist, and each of them is a reason to print nothing rather than a reason to print a date.

type Fleet

type Fleet struct {
	Basis Basis

	// FiveHour and Weekly are the two axes, each judged with only its own
	// windows burning and only they ending the run.
	FiveHour Axis
	Weekly   Axis

	// Both is the fleet answer: every window burning at once, which is the
	// question a user is asking when they ask how long the fleet lasts.
	//
	// It carries a Verdict and a moment and NOTHING ELSE. Its Burn is unknown
	// and its Replenish is zero because one percentage point of a five-hour
	// window and one of a weekly window are different quantities: a rate
	// exists only inside an axis, and a figure summing the two would have no
	// unit. A renderer must print this axis's verdict and never its cells.
	Both Axis

	// PointsLeft and PointsTotal are the weekly axis's room, counted ONE
	// window per account -- the least-room weekly one -- over the accounts
	// that were readable. The weekly axis is a set of windows that all meter
	// the same consumption, so summing them would count the same tokens three
	// to five times while the replenishment figure beside them counted one
	// window per account, and the two columns a reader is told to compare
	// would not be on one basis.
	//
	// An account that is already out still counts toward PointsTotal, and
	// contributes its zero to PointsLeft. It is a readable seat of the fleet's
	// capacity that happens to be empty right now, and dropping it would move
	// the denominator every time an account rolled over. An account that
	// reported no weekly window counts toward NEITHER, and that is a different
	// case: there is no weekly quota of it to count.
	PointsLeft  float64
	PointsTotal float64

	// AccountsUsable is how many accounts the run had to work with: the ones
	// that are eligible for the rotation and have at least one readable
	// window. An account that is out right now is one of them -- being spent
	// is a fact about this minute, and its quota comes back.
	//
	// The cross-check a reader has is Basis.Accounts less Basis.Unreadable and
	// Basis.Ineligible, and that subtraction is exact. It is NOT PointsTotal
	// divided by a hundred and NOT the denominator of either Replenish figure,
	// and those three are three different counts: PointsTotal counts only the
	// accounts that reported a WEEKLY window, the weekly Replenish is measured
	// over that same narrower set, and the five-hour Replenish over the
	// accounts carrying five_hour. An account that reported a five-hour window
	// and no weekly one is in this count and in neither of those, so inviting
	// the comparison would hand a reader a subtraction that fails on an
	// ordinary fleet.
	AccountsUsable int

	// AccountsNeeded is the smallest fleet size at which BOTH axes survive the
	// horizon, found by re-running the rotation with hypothetical accounts
	// appended -- one mechanism answering a third question, not a second
	// mechanism free to disagree with the first two on the same page.
	//
	// It counts accounts on the plan the fleet already has, so the tier notice
	// applies to it: on a fleet whose plans disagree, "one more account" is not
	// a unit.
	//
	// HasNeeded is false when there was no basis to search from, and the figure
	// is then ABSENT rather than zero: a fleet that needs no more accounts and
	// a fleet nobody measured must never read the same.
	AccountsNeeded int
	HasNeeded      bool

	// NeededBy names the axis that set the figure -- the one whose own run was
	// still failing at the count below it. It carries that axis's
	// representative window name, five_hour or seven_day, the same two the
	// replenishment figures are measured on.
	//
	// Which axis it is is MEASURED. The two axes imply counts in the ratio of
	// their measured rates against 20 and 100/168 points an hour, and nothing
	// in the design fixes which is larger. HasNeededBy is false when the count
	// below the answer failed on both axes, or on neither -- there the figure
	// came from the two together and naming one of them would invent a fact.
	NeededBy    usage.WindowName
	HasNeededBy bool

	// NeededCapped is true when the search reached its bound without finding a
	// count that holds. AccountsNeeded is then the bound itself and means "more
	// than this": a fleet that appears to need that many accounts has a
	// measurement problem rather than a purchasing one.
	NeededCapped bool

	// Rows is one line per readable account, ordered by EmptyAt ascending and
	// then by the account's index, so the account that goes first reads first.
	// PointsTotal is 100 times the rows with a weekly window, which is not
	// always every row -- see AccountRow.HasWindow.
	Rows []AccountRow

	// Credit is the paid-usage answer, which is one date or nothing.
	Credit CreditFleet

	// TierNotice is empty when every readable account reported the same plan
	// tier. Summing percentage points across accounts assumes their quotas are
	// the same size, and this says so when they may not be.
	TierNotice string
}

Fleet is the whole answer.

func Of

func Of(in []Input, now time.Time) Fleet

Of measures the fleet and decides what the measurement implies.

The order is fixed by what depends on what: the current snapshots give every level and the window set, the series give a rate per WINDOW NAME, the two axes and the fleet are each run twice over that band, and the rows are read out of the fleet run rather than computed a second way.

type Input

type Input struct {
	UUID string

	// Idx is the account's display index in the store, and it is the FIRST of
	// the two things that break ties: two accounts with identical room must be
	// picked in the same order on every run, or six runs of one fleet disagree
	// about a fleet that never changed.
	//
	// It does not break them on its own, because it is numbered per provider --
	// a Claude seat and a Codex one carry the same number as a matter of course.
	// UUID above finishes the order; see simAccount.before.
	Idx int

	// Tier is rate_limit_tier, and "" is a real state -- an account added
	// while the profile endpoint was unreachable carries it and is still
	// polled and ranked. It is never counted as agreement.
	Tier string

	// Snapshot is the current reading. A nil one is an account nobody can see.
	Snapshot *usage.Snapshot

	// Series is this account's samples, oldest first, already filtered by the
	// read side.
	Series []history.Sample

	// Eligible is whether the ROTATION can hand this account work. It is the
	// caller's copy of the engine's own eligibility rule, which is stated once
	// in eligible() in internal/strategy/rank.go; this package must not import
	// a ranking, so the answer arrives as a field.
	//
	// An ineligible account's quota is quota the fleet cannot reach: a run
	// that made it live would spend capacity no switch can get to, and would
	// report a runway several times longer than the fleet has. That is the
	// same fail-open simAccount.readable refuses -- an account nobody can see
	// and an account nothing can switch to are unreachable for different
	// reasons and by the same amount.
	//
	// The zero value EXCLUDES, which is deliberate: a caller that forgets this
	// field reports an empty fleet, loudly, rather than an over-long runway
	// that reads like an answer.
	Eligible bool
}

Input is one account as the forecast sees it.

The forecast never opens a store, a cache or a clock: everything arrives here. Snapshot is the CURRENT reading and is the only source of a level; Series is the only source of a slope. Keeping the two apart is what stops two commands disagreeing about one number, because the store gives each quantity exactly one authoritative file and history.json is not the one for levels -- its newest sample merely duplicates the cache's.

type Verdict

type Verdict uint8

Verdict is what a measured rate implies for one axis.

VerdictUnknown is the zero value on purpose, so a value that was never decided reads as undecided rather than as a promise. It covers two states that must never be rendered as "holds": there is no basis to decide from, and the two runs of the measured band disagreed. Both mean the evidence does not carry a claim; neither means the fleet is safe.

const (
	VerdictUnknown Verdict = iota
	VerdictHolds
	VerdictRunsDry
	// VerdictNoWindow is a five-hour axis no account in the fleet reports a window
	// for.
	//
	// It is not VerdictUnknown. That one says the evidence is not in yet and more
	// of it may settle the question; a fleet of verified Ultra seats reports no
	// five_hour at all, so nothing is ever going to settle anything, and "cannot
	// tell yet" there would be a promise of an answer that stays on the screen
	// forever. There is no limit on this axis to run out of.
	//
	// The weekly axis does not get it. Every Claude plan has a week, so a fleet
	// reporting none is a fleet nobody has read properly, and "cannot tell" is
	// exactly right. TestAnAxisWithNoWindowOfItsOwnPromisesNothing is that line.
	VerdictNoWindow
)

Jump to

Keyboard shortcuts

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