Documentation
¶
Overview ¶
Package limits keeps the machine's subscription limits: rate-limits.json, one entry per provider merged field by field from every source that measures them (the agent's statusline, codexbar, Codex's usage endpoint, the Codex transcript), its watcher, and limits-history.jsonl for the burn-rate forecast.
Index ¶
- Constants
- Variables
- func CodexHome(env, home string) string
- func CodexbarBin(path string) string
- func RotateHistory(file string, now float64) error
- func Sanitize(text string) string
- func ShouldRecord(prevT float64, prev *Windows, w Windows, now float64) bool
- func Watch(ctx context.Context, file string, onChange func(map[string]any))
- type Cache
- type CodexLogin
- type Data
- func CodexTranscriptLimits(path string, now float64) Data
- func Merge(prev, next Data) Data
- func ParseCodexUsage(body any, now float64) Data
- func ParseCodexbar(stdout []byte, now float64) (Data, string)
- func PollCodexUsageOnce(ctx context.Context, login CodexLogin, client *http.Client, url string, ...) (Data, error)
- func PollCodexbarOnce(ctx context.Context, bin string, run RunCodexbar, cache *Cache, ...) (Data, error)
- type History
- type HistoryReader
- type NamedWindow
- type Pollers
- type Record
- type RunCodexbar
- type Sample
- type Window
- type Windows
Constants ¶
const ( CodexUsageURL = "https://chatgpt.com/backend-api/wham/usage" CodexUsageInterval = 120 * time.Second )
const ( SourceCodexbar = "codexbar" SourceCodexUsage = "codex-usage" )
Sources.
const (
CodexbarInterval = 60 * time.Second
)
const HistoryKeepSec = 8 * 86400
HistoryKeepSec is how much history the rotation keeps.
const PrecedenceSec = 180
PrecedenceSec is how long a fresh value of a usage endpoint outranks the statusline and the Codex transcript: those echo what the CLI saw on its last request and would only make the percentage jump back and forth.
Variables ¶
var CodexbarArgs = []string{"usage", "--provider", "claude", "--source", "oauth", "--format", "json"}
CodexbarArgs is the fixed command line.
var Providers = []string{"claude", "codex"}
Providers are the entries rate-limits.json holds.
Functions ¶
func CodexbarBin ¶
CodexbarBin is the configured CLI when it is an absolute path; otherwise there is no poller at all.
func RotateHistory ¶
RotateHistory keeps the last eight days; the file is replaced atomically and stays 0600. A missing file is left so.
func Sanitize ¶
Sanitize is error text safe to store and show: no e-mail addresses, no control characters, at most 200 units.
func ShouldRecord ¶
ShouldRecord is whether a provider's new state is worth a line: some window was measured after the previous line, and either a value changed or that line is a minute old.
Types ¶
type Cache ¶
type Cache struct {
File string
// contains filtered or unexported fields
}
Cache is rate-limits.json. Every read-merge-write goes through one lock; the file is replaced, never rewritten in place, because other writers (a root timer copying limits between accounts) and the watcher read it at any moment.
type CodexLogin ¶
CodexLogin is the ChatGPT login of Codex.
func ReadCodexLogin ¶
func ReadCodexLogin(codexHome string) *CodexLogin
ReadCodexLogin is the login in <codex home>/auth.json; nil for an API-key login or no login at all.
type Data ¶
Data is one provider's entry as it is stored: a JSON object whose fields beyond the windows are optional, so writers merge field by field and fields a writer does not know survive.
func CodexTranscriptLimits ¶
CodexTranscriptLimits reads the limits Codex wrote into the tail of its transcript; nil when there are none.
func Merge ¶
Merge is one provider's merge: a window or a codexbar field is replaced only by a value measured later, so the statusline never wipes codexbar's plan, pace or extra windows, and a stale copy never overwrites a fresh one.
func ParseCodexUsage ¶
ParseCodexUsage is both windows as the plan has them now. A Pro plan has only the weekly one, so the missing window is written as null and replaces whatever an older transcript copy left there. Nil when the answer has no window.
func ParseCodexbar ¶
ParseCodexbar reads codexbar's answer measured at now (epoch seconds): the provider data, or the failure message.
func PollCodexUsageOnce ¶
func PollCodexUsageOnce(ctx context.Context, login CodexLogin, client *http.Client, url string, cache *Cache, clock func() float64) (Data, error)
PollCodexUsageOnce asks ChatGPT once and writes the answer (or the failure) as Codex's entry.
func PollCodexbarOnce ¶
func PollCodexbarOnce(ctx context.Context, bin string, run RunCodexbar, cache *Cache, clock func() float64) (Data, error)
PollCodexbarOnce runs codexbar once and writes the result (or the failure) as Claude's entry.
type History ¶
type History struct {
File string
// contains filtered or unexported fields
}
History appends to limits-history.jsonl. What it wrote last lives in memory only, so the first state after a start always gets a line.
type HistoryReader ¶
type HistoryReader struct {
File string
// contains filtered or unexported fields
}
HistoryReader keeps every record of the last eight days and reads only what was appended since its previous call. A file replaced (another inode) or cut shorter is read again from the start.
func (*HistoryReader) Read ¶
func (h *HistoryReader) Read(now float64) []Record
Read is every record of the last eight days before now (epoch seconds). The slice is the reader's own: callers do not change it.
type NamedWindow ¶
NamedWindow is one window of a record.
type Pollers ¶
type Pollers struct {
Cache *Cache
CodexbarBin string // absolute path, or empty for no codexbar poller
CodexHome string
Client *http.Client
URL string // the Codex usage endpoint
Clock func() float64
RunCLI RunCodexbar
// Warn gets the failures that are only worth a log line.
Warn func(msg string, err error)
}
Pollers ask the usage endpoints on their own schedule. Each one is single-flight: a slow run delays the next instead of piling up.
type Record ¶
type Record struct {
T float64
P string
W []NamedWindow
}
Record is one line of limits-history.jsonl as the forecast reads it: its windows that are whole tuples of finite numbers. The windows are kept in the record itself and their names shared, so a scan over a week of lines stays in cache.
type RunCodexbar ¶
RunCodexbar runs the CLI; codexbar exits 1 with a JSON error entry on stdout, which is more useful than the exit code.