Documentation
¶
Overview ¶
Package receipt holds the token counts of a developer's own agent sessions on one branch, as costroid receipt prints them for the developer to post on the pull request, and as a report reads, refuses and prices them.
A receipt states counts and never an amount. Every string in it is a closed word, a day or a pattern, and no field holds text a person or a model wrote.
Index ¶
- Constants
- Variables
- func Collect(requests []claudecode.LocalRequest, scope Scope) (Receipt, Tally, error)
- func Days(r Receipt, held func(session string) bool) (first, last string, sessions int)
- func ValidBranch(name string) bool
- type Figure
- type Posted
- type Priced
- type Reading
- type Receipt
- type Refusal
- type Scope
- type Session
- type Tally
- type Tokens
Constants ¶
const ( // Marker opens a receipt's comment. Costroid's own comment opens with // another marker, and neither is a prefix of the other. Marker = "<!-- costroid:receipt -->" // Schema is the version of the receipt contract this release writes and // reads. Schema = "costroid.receipt/0.1" // ClaudeCode is the one agent whose session files this version of the // contract reads. ClaudeCode = "claude-code" // MaxSessions is the most entries a receipt holds. MaxSessions = 150 // MaxBody is the most bytes a receipt's comment holds: a comment on // GitHub holds 65,536 characters. MaxBody = 65536 )
Variables ¶
var ErrNoSessions = errors.New("receipt: no session on the branch was found")
ErrNoSessions says no request on the branch was found.
Functions ¶
func Collect ¶
func Collect(requests []claudecode.LocalRequest, scope Scope) (Receipt, Tally, error)
Collect sums the requests that count into a receipt for scope.Branch. A request counts when its line recorded the branch and a working directory inside the repository, and it was made from scope.Since on. A session that moved to another branch counts up to the line where it moved.
func Days ¶
Days are the first and last UTC day of the receipt's entries that held does not leave out, and how many sessions those entries are.
func ValidBranch ¶
ValidBranch reports a branch name a receipt can be for. A detached head has no branch, and HEAD is never one.
Types ¶
type Posted ¶
Posted is a comment the pull request's author posted that opens with Marker, as the host states it.
type Priced ¶
type Priced struct {
// contains filtered or unexported fields
}
Priced are the rows Rows built from a receipt, in memory. Only Rows makes them, so no row a file held can reach a receipt's figure: every reader of a rows file refuses a row that states costroid/origin, and none can make Priced.
func Rows ¶
func Rows(r Receipt, prices *catalog.Catalog, ci map[string]string, held func(session string) bool) (Priced, error)
Rows prices a receipt's entries at the catalog the report's own binary loads, one row per entry and billed class that holds tokens. The receipt states counts and no price, so every price is the catalog's. A model the sheet does not name takes the sheet's fallback, which is never zero. ci holds the tags the CI system states, the repository, the pull request and the head; a CI run is never one, because the sessions ran in none. An entry whose session the run's own rows also hold is left out: held reports it. The rows are for the report's memory, and no file.
type Reading ¶
type Reading struct {
Comment Posted
// SHA256 is the digest of the comment's body, byte for byte, as the host
// returned it.
SHA256 string
// Receipt is the receipt read, the zero value when it was refused.
Receipt Receipt
// Refused is why none of its sessions was read, empty when they were.
Refused Refusal
}
Reading is the receipt that decides: the newest the pull request's author posted, read or refused.
func Newest ¶
Newest reads the newest of the comments the pull request's author posted with the marker, and is false when there is none. Only that comment decides: an older one is never read in its place. allowed is false when costroid.yml on the base branch turns receipts off, and branch is the pull request's head branch.
type Receipt ¶
type Receipt struct {
Schema string `json:"schema"`
Branch string `json:"branch"`
Written string `json:"written"`
// Since is the UTC day of the oldest entry of the branch's reflog on the
// machine, nil when the reflog holds none.
Since *string `json:"since"`
Agents []string `json:"agents"`
Sessions []Session `json:"sessions"`
SessionsOmitted int `json:"sessions_omitted"`
}
Receipt is one receipt, as the contract lays it out.
func Parse ¶
Parse reads a comment's body as a receipt. A browser can store a comment with CRLF line endings, and a copy from a terminal can gain or lose a final line feed, so each CRLF reads as LF and blank lines after the closing fence are ignored, and nothing else is normalised. The body is then the marker line, the fenced object and at most one line feed, with no carriage return left. The object holds every key the contract requires and no other, each key once. Any fault refuses the whole receipt, and none of its sessions is read.
type Refusal ¶
type Refusal string
Refusal is why a report read none of a receipt's sessions. The list is closed, and each value is the machine block's.
const ( // Edited is a comment edited after it was posted. A new count is a new // comment. Edited Refusal = "receipt_edited" // OtherBranch is a receipt for another branch than the pull request's. OtherBranch Refusal = "receipt_other_branch" // Invalid is a comment that is not a valid receipt. Invalid Refusal = "receipt_invalid" // Off is a receipt costroid.yml on the base branch turns off. Off Refusal = "receipts_off" )
The refusals.
type Scope ¶
type Scope struct {
Branch string
// Roots are the repository's worktrees, as absolute paths.
Roots []string
// Since is the oldest entry of the branch's reflog, zero when it holds
// none.
Since time.Time
Now time.Time
}
Scope is what a receipt counts: the requests made on Branch, in a working directory under one of Roots, from Since on. Now is when it is written.
type Session ¶
type Session struct {
Agent string `json:"agent"`
AgentVersion string `json:"agent_version"`
SessionID string `json:"session_id"`
Model string `json:"model"`
ServiceTier *string `json:"service_tier"`
Region *string `json:"region"`
FirstDay string `json:"first_day"`
LastDay string `json:"last_day"`
Requests int64 `json:"requests"`
// LargestInput is the largest whole input of any one request the entry
// sums. It selects a context tier.
LargestInput string `json:"largest_input"`
Tokens Tokens `json:"tokens"`
// AssumedTTL names the cache lifetimes the reader's rule assigned because
// the session file did not state them. Empty when it stated each.
AssumedTTL []string `json:"assumed_ttl,omitempty"`
}
Session is one entry: the requests of one session on one model, service tier and region, summed.
type Tally ¶
type Tally struct {
Counted int
// Elsewhere counts the requests made on another branch, and NoBranch
// those whose line recorded none.
Elsewhere, NoBranch int
// Outside counts the requests made in a working directory outside the
// repository.
Outside int
// Earlier counts the requests made before Since, and Untimed those whose
// files state no time or a time after Now.
Earlier, Untimed int
// Unnamed counts the requests of sessions whose id or version a receipt
// cannot state.
Unnamed int
// SessionsOmitted counts the oldest sessions left out so that the
// receipt fits in one comment.
SessionsOmitted int
}
Tally is what Collect counted and what it left out, in requests.
type Tokens ¶
type Tokens struct {
InputUncached string `json:"input_uncached"`
CacheRead string `json:"cache_read"`
CacheWrite5m string `json:"cache_write_5m"`
CacheWrite1h string `json:"cache_write_1h"`
Output string `json:"output"`
}
Tokens are a Claude Code entry's counts by billed class, each a whole number as a decimal string.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package receipttest writes synthetic Claude Code session files for the tests of costroid receipt.
|
Package receipttest writes synthetic Claude Code session files for the tests of costroid receipt. |