limitpark

package
v0.271.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 6 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 limitpark tells a STALE usage-limit menu from a live one.

The failure it answers

When a claude-code turn meets its account's usage limit, the CLI raises its usage-limit menu ("❯ 1. Stop and wait for limit to reset") in that session. The menu stays until someone answers it, past the limit's reset too, and ccd refuses every later turn into that pane with a 409 rate_limited (cmd/ccd limitMenuFailure), which the gateway reads as the account's usage limit: it marks the account and asks the failover to move the agent.

The usage poller (internal/ccusage) clears an account's mark once a LIVE reading shows it usable again. But a pane still on its old menu refuses the next turn with the same 409, and the account was marked again within minutes. Measured on a production deployment: seven officer instances sat on the menu for six hours, the poller cleared the mark every interval, the panes re-marked it every time, and their slots were never freed although the account read 21% of its weekly window. A Stop and Start cleared each.

What the board does

It remembers, per agent and LANE (ccd runs one CLI per lane: its primary and each session's), since when that pane has been parked on the limit (a Park: the first usage-limit failure since the agent's last completed turn), and, per account, the poller's latest good reading. Then:

  • Board.NoteLimit (the gateway, on every usage-limit failure) answers whether the failure is STALE: a 409 from a pane parked SINCE BEFORE a newer live reading that shows the account usable for the limit the pane names, with no fresh limit hit on the account after that reading. For an agent muster restarts off such a menu (a pod officer's instance: internal/api Server.LimitRestartable, the one rule), a stale failure marks nothing and moves nobody: it says nothing the live reading did not already overrule. Any other agent's still marks and fails over. Decided by timestamps, never text: a 429 (a turn that WAS typed and hit the limit) is always fresh, and so is any failure newer than the reading.
  • Board.Stuck (the officer runtime, each wake-loop pass) answers whether an agent's pane is stale-parked AND was still seen on the menu after that reading — the instance the wake loop then restarts (internal/ officers restartLimitParked), as an operator's Stop and Start would.

In memory, and why that is safe

The board is per process (muster runs one replica). A restarted muster has no parks: the next 409 from a stuck pane parks it NOW and marks the account as before; the poller's next pass clears the mark with a reading newer than that park, and from then on the pane is stale. The cost of a restart is one poll interval, never a wrong verdict.

Index

Constants

View Source
const MaxRestartsPerDay = 4

MaxRestartsPerDay is a BACKSTOP to the timing rule, not its pacing: a restart needs the primary pane parked since before a live reading newer than the park, and the stop ends the park, so one agent is restarted at most once per usage-poll interval. This bounds what that rule would allow if it were wrong (a CLI that re-raises the menu at once, every interval): past it the agent is HELD for the rest of the UTC day — Stuck answers held, and its refusals are no longer stale, so they mark the account and ask the failover exactly as before this board existed (the agent is not left silenced with nothing to free it). Kept in memory: a restarted muster forgets it, and the timing rule still holds.

⚠ NOT the officers' auto-restart cap (internal/officers autorestart.go, migration 0170), deliberately: that book counts restarts of an instance in `error`, per error episode, with its class; a limit-parked instance is running and has no episode, and sharing it would spend the error path's daily places on a different failure.

View Source
const PrimaryLane = ""

PrimaryLane is the lane of ccd's primary CLI (a turn with no session header): the officer instance's queue work, and the only lane whose stale park makes the instance Stuck. A chat lane parked on the menu is stopped by ccd itself once no turn holds it (cmd/ccd lanes.release), so restarting the whole instance for it would only kill the primary's turn.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board struct {

	// Logger, when set, gets one line per park the first time a refusal from
	// it is found stale (and so marks nothing). Set it before first use.
	Logger *log.Logger
	// contains filtered or unexported fields
}

Board is the parks and readings. The zero value is not usable: use New.

func New

func New(marksOwner func(ctx context.Context, owner int64) int64, now func() time.Time) *Board

New builds a board. marksOwner maps an agent's owner to the owner its account's marks and readings are kept under (ccpool.Pool.MarksOwner); nil keys every account by the owner given. now is a test seam (nil: time.Now).

func (*Board) Clear

func (b *Board) Clear(agentID int64, lane string)

Clear forgets agentID's park in lane: a turn there completed, or ccd said a turn is running in it. Another lane's park stands.

func (*Board) NoteLimit

func (b *Board) NoteLimit(ctx context.Context, agentID int64, lane string, owner int64, account, detail string, menu bool) (stale bool, why string)

NoteLimit records a usage-limit failure of agentID's turn in lane (its HeaderCCSession; PrimaryLane for ccd's primary CLI) on owner's account — menu: ccd's 409 refusal from a pane parked on the usage-limit menu (nothing typed); otherwise a typed turn that hit the limit (a 429) — and reports whether it is STALE, and why: a menu refusal from a pane parked since before a newer live reading that shows the account usable (see the package doc). A stale failure must not mark the account. An agent HELD at MaxRestartsPerDay is never stale: its refusals mark as before.

func (*Board) NoteReading

func (b *Board) NoteReading(ctx context.Context, owner int64, account string, row ccusage.Row, markDetail string)

NoteReading records a good live reading of owner's account (the usage poller, ccusage.Poller.Reading): row as saved, taken at row.ReadAt, against a mark whose detail was markDetail ("" when the account was not marked). owner is the poller's, which is already the marks owner. A row that is not a good reading taken by its own attempt is ignored, as is one older than the account's latest.

func (*Board) Parked

func (b *Board) Parked(agentID int64, lane string) (Park, bool)

Parked is agentID's park in lane, if it has one (for tests and the log).

func (*Board) Stopped

func (b *Board) Stopped(agentID int64)

Stopped forgets every park of agentID — its pod (and every CLI in it) was stopped or started — and, when its primary pane was Stuck at that moment, counts a restart off a stale menu against MaxRestartsPerDay.

func (*Board) Stuck

func (b *Board) Stuck(agentID int64) (why string, stuck, held bool)

Stuck reports whether agentID's PRIMARY pane is stale-parked (NoteLimit's verdict against the account's latest reading) AND was seen on the menu at or after that reading — so a restart now is what frees it — and says why. held: it would be, but the agent was restarted MaxRestartsPerDay times today (the backstop), so it is not to be restarted again today. A chat lane's park never makes the agent stuck (PrimaryLane).

type Park

type Park struct {
	// Owner is the owner the account's marks and readings are kept under
	// (Board.MarksOwner of the agent's owner); Account the account's name.
	Owner   int64
	Account string
	// Detail is the latest failure message that names a subscription window
	// (ccusage.MarkedLimitWindow), "" when none did.
	Detail string
	// Since is the park's first usage-limit failure; Seen its latest.
	Since, Seen time.Time
	// contains filtered or unexported fields
}

Park is one agent's pane parked on its account's usage limit.

Jump to

Keyboard shortcuts

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