rollup

package
v0.62.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 13 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 rollup is the single write path for events: it stores the event, upserts the session, prices assistant turns, and maintains session_stats / daily_stats / session_files in one transaction, then publishes live frames. Every producer (hookd, ingest, harness) goes through Recorder.Record.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Recorder

type Recorder struct {
	Store *store.Store
	Table *cost.Table
	Bus   *bus.Bus
	Log   *slog.Logger
	// Now is overridable in tests.
	Now func() time.Time
	// Location decides which calendar day a turn belongs to (daily_stats).
	Location *time.Location
	// OnPriced is called after a turn has been priced and stored, with the
	// daily spend cap as its only caller today.
	//
	// A hook rather than a direct dependency: the recorder is the write path
	// for every event on the machine, and it must not grow a reason to know
	// what a spend cap is. It is also called *after* the transaction commits —
	// a cap that paused sessions from inside the write path would hold the
	// database open while signalling processes.
	OnPriced func(ctx context.Context)
	// contains filtered or unexported fields
}

Recorder wires store + pricing + bus.

func New

func New(st *store.Store, tb *cost.Table, b *bus.Bus, log *slog.Logger) *Recorder

New builds a Recorder with sane defaults.

func (*Recorder) MarkIdle

func (r *Recorder) MarkIdle(ctx context.Context, idleAfter, endAfter time.Duration) error

MarkIdle flips sessions silent for longer than idleAfter to idle, and sessions silent for longer than endAfter (when > 0) to ended, publishing their new state. Called periodically by the daemon.

func (*Recorder) PriceUnpriced added in v0.59.0

func (r *Recorder) PriceUnpriced(ctx context.Context) (priced int, err error)

PriceUnpriced prices the turns stored with no cost because their model was missing from the pricing table, now that the table has a row for it.

This is not repricing. A turn that was priced keeps the figure it was given when it ran — a bump never restates history. A turn with no cost was never given a figure at all; it sat outside every total as "unpriced", and adding the model's row fixed only the turns after it. 5,788 Codex turns on the owner's machine (gpt-6-sol and gpt-6-luna) were in that state. Each is priced at the row in force at its own timestamp, exactly as it would have been had the row existed then, and the session and daily totals take the same amount in the same transaction, so every screen stays consistent with the events.

func (*Recorder) RebuildCodexDaily added in v0.59.0

func (r *Recorder) RebuildCodexDaily(ctx context.Context, keepFrom time.Time) (rows int, err error)

RebuildCodexDaily recomputes daily_stats' Codex rows from the Codex events.

Migrations 0022 and 0023 deleted every Codex event and zeroed the per-session totals so the importer could write them again correctly — but left daily_stats alone. The re-import then added each re-read turn to the day it was already in. On the owner's machine that put five days $3.68 and 7.9M tokens above what the events add up to (2025-10-16/17 on gpt-5-codex, three days in August and September on gpt-5.6-sol), in every screen that reads the daily table.

daily_stats cannot simply be rebuilt from events in general: it outlives events that retention has pruned, and its project is the one the turn's own cwd resolved to at the time. For Codex both are knowable — the importer passes the session's cwd, so the project is the session's — which is why this is scoped to Codex, and within Codex:

  • a (day, model) that another source also used on that day is skipped, because its row mixes usage this cannot separate;
  • a day before keepFrom is skipped, because retention may have pruned its events and rebuilding it would erase real history.

Tokens and cost are rewritten; the session count is left as it is, because daily_sessions never double-counted. Runs once (store.MetaCodexDailyRebuilt).

func (*Recorder) Record

func (r *Recorder) Record(ctx context.Context, ev *event.Event, info SessionInfo) (Result, error)

Record stores one event and updates every rollup. It is safe to call from multiple goroutines; the store serializes writers.

type Result

type Result struct {
	Stored   bool // false when the event was a duplicate
	Event    event.Event
	Session  store.Session
	Stats    store.Stats
	Priced   bool // cost computed for this event
	NewFiles int  // files touched for the first time by this event
}

Result reports what Record did, for callers that need to fan out further.

type SessionFrame

type SessionFrame struct {
	Session store.Session `json:"session"`
	Stats   store.Stats   `json:"stats"`
}

SessionFrame is the payload of a "session" live frame.

type SessionInfo

type SessionInfo struct {
	Cwd, TranscriptPath, GitBranch, Version, Model string
	// PID is the process the session belongs to, when it is known: the shim
	// reports its parent, which is Claude Code. Zero means unknown, and an
	// unknown pid is why the staleness sweep still exists.
	PID int
	// Agent names the coding agent this session belongs to. Empty means Claude
	// Code, which is what every session was before OpenCode support, and what
	// the column defaults to.
	Agent string
	// ReplacesPID is set when this event starts a session that takes over a
	// process already running one — a `SessionStart` whose source is `clear`.
	// The sibling it replaces cannot be found by the staleness sweep, because
	// they share the live process the sweep uses to judge liveness.
	ReplacesPID bool
	// Title is the agent's own name for the session, when its source carries
	// one (OpenCode). Empty leaves a stored title alone.
	Title string
}

SessionInfo is what a producer knows about the session an event belongs to. Empty fields are "unknown", never overwrite.

Jump to

Keyboard shortcuts

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