Expand description
Deterministic transforms — slice 2 of folding usage_signal.py into Rust
(ROADMAP E1). Ported 1:1 from empty_metrics/_add_metrics,
split_logical_sessions, _empty_bucket/_accumulate, bucket_aggregates,
and project_name_from_cwd. Pure given (events, NOW, UTC offset) — pinned by
a golden fixture generated from the Python (tests/aggregate_golden.rs).
Day/week/month bucketing uses a fixed UTC offset (seconds east of UTC). The
Python uses the system local tz via astimezone(); for fixed-offset zones
(e.g. Türkiye, permanent UTC+3) that is identical, and the golden pins it
with TZ=UTC / offset 0. DST-aware per-timestamp offsets are a later
refinement (would need a tz database); documented in COST_MODEL/ROADMAP.
Structs§
- Buckets
- Result of
bucket_aggregates— mirrors the snapshot’s aggregate fields. - File
Events - One transcript file’s per-turn events plus its resolved model/cwd.
- Session
- A logical session chunk that feeds
bucket_aggregates. - Turn
Metrics - Per-turn token buckets + estimated cost.
totalis the stats token total (fresh_in + output) the parser computes; the four buckets feed the cost view.
Constants§
- SESSION_
IDLE_ GAP - Idle gap (seconds) that splits one transcript file into logical sessions — matches Claude’s 5h window, which resets 5h after the first turn.
- WIN_30D
- 30-day rolling window in seconds.
Functions§
- bucket_
aggregates - Roll sessions into day/week/month/model/project buckets + the day×project
cross-tab + 30d totals.
nowis epoch seconds;offsetis the fixed local UTC offset. Mirrorsbucket_aggregates(days=365, weeks=52, months=24, instance_days=30, instance_rows=200). - iso_utc
- Format an epoch-seconds value as a UTC ISO8601 string ending in
Z. Subsecond is emitted as 6 digits only when nonzero (mirrors Python’sdatetime.fromtimestamp(ts, tz=utc).isoformat().replace("+00:00","Z")). - parse_
iso - Parse an ISO-8601 / RFC-3339 timestamp to epoch seconds (mirrors Python
datetime.fromisoformat(...).timestamp()for theZ/offset forms used in transcripts). ReturnsNoneon empty/unparseable input. - project_
name_ from_ cwd project_name_from_cwd: basename of the cwd, or—when absent.- split_
logical_ sessions - Split each file’s events into logical sessions on the 5h idle gap, returning
(sessions, recent)— sessions feedbucket_aggregates, recent feedsrecent_sessions.filesis iterated in its existing order (use aBTreeMapfor determinism). Mirrorssplit_logical_sessions.