limits

package
v0.16.16 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 27 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 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

View Source
const (
	CodexUsageURL      = "https://chatgpt.com/backend-api/wham/usage"
	CodexUsageInterval = 120 * time.Second
)
View Source
const (
	SourceCodexbar   = "codexbar"
	SourceCodexUsage = "codex-usage"
)

Sources.

View Source
const (
	CodexbarInterval = 60 * time.Second
)
View Source
const HistoryKeepSec = 8 * 86400

HistoryKeepSec is how much history the rotation keeps.

View Source
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

View Source
var CodexbarArgs = []string{"usage", "--provider", "claude", "--source", "oauth", "--format", "json"}

CodexbarArgs is the fixed command line.

View Source
var Providers = []string{"claude", "codex"}

Providers are the entries rate-limits.json holds.

Functions

func CodexHome

func CodexHome(env, home string) string

CodexHome is $CODEX_HOME, else ~/.codex.

func CodexbarBin

func CodexbarBin(path string) string

CodexbarBin is the configured CLI when it is an absolute path; otherwise there is no poller at all.

func ExhaustedReset added in v0.15.40

func ExhaustedReset(d Data, now float64) float64

ExhaustedReset is when the windows of an entry that ran out reset (unix seconds), the latest of them: what the limits say a chat held on its usage limit waits for. 0 when no window is at its limit, or none resets after now.

func RotateHistory

func RotateHistory(file string, now float64) error

RotateHistory keeps the last eight days; the file is replaced atomically and stays 0600. A missing file is left so.

func Sanitize

func Sanitize(text string) string

Sanitize is error text safe to store and show: no e-mail addresses, no control characters, at most 200 units.

func ShouldRecord

func ShouldRecord(prevT float64, prev *Windows, w Windows, now float64) bool

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.

func Watch

func Watch(ctx context.Context, file string, onChange func(map[string]any))

Watch reports the file's content every time its top-level `ts` changes: once at the start, then after writes settle. A directory that is missing or a watch that fails is retried every few seconds. It returns when ctx ends.

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.

func (*Cache) Read

func (c *Cache) Read() map[string]Data

Read is the stored providers; a missing or broken file is empty.

func (*Cache) Write

func (c *Cache) Write(provider string, data Data) (map[string]any, error)

Write merges data into the provider's entry and returns the whole new content.

type CodexLogin

type CodexLogin struct {
	AccessToken string
	AccountID   string
}

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

type Data = map[string]any

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

func CodexTranscriptLimits(path string, now float64) Data

CodexTranscriptLimits reads the limits Codex wrote into the tail of its transcript; nil when there are none.

func Merge

func Merge(prev, next Data) Data

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

func ParseCodexUsage(body any, now float64) Data

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

func ParseCodexbar(stdout []byte, now float64) (Data, string)

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.

func (*History) Record

func (h *History) Record(cache map[string]any, now float64) error

Record appends a line for every provider of the content whose state is worth one; now is epoch seconds.

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

type NamedWindow struct {
	ID string
	W  Window
}

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.

func (*Pollers) Run

func (p *Pollers) Run(ctx context.Context)

Run polls until ctx ends: codexbar every minute when configured, Codex's usage every two minutes while the machine has a ChatGPT login of Codex (re-read every time, Codex refreshes it).

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.

func (Record) Get

func (r Record) Get(id string) (Window, bool)

Get is the record's window of an id.

type RunCodexbar

type RunCodexbar func(ctx context.Context, bin string, args []string) ([]byte, error)

RunCodexbar runs the CLI; codexbar exits 1 with a JSON error entry on stdout, which is more useful than the exit code.

type Sample

type Sample struct {
	T, U, R float64
}

Sample is one measurement of a window: epoch seconds, percent used, reset time.

func SamplesOf

func SamplesOf(records []Record, provider, windowID string) []Sample

SamplesOf is one window of one provider, sorted by measurement time, one sample per measurement (the last line that carries it).

type Window

type Window [3]float64

Window is a history tuple: used %, resets at, measured at.

type Windows

type Windows struct {
	IDs    []string
	Tuples map[string]Window
}

Windows keeps the order the tuples are written in.

func HistoryWindows

func HistoryWindows(d Data) Windows

HistoryWindows is one provider as history tuples: 5 h, 7 d and codexbar's extra windows under their own ids.

Jump to

Keyboard shortcuts

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