Expand description
The catch-up sweep for Claude Code’s cost-state report (VIS-1108).
Claude Code writes a session’s cost report, a cost-state transcript
line, at exit, after Stop; the SessionEnd hook sends it. When that
hook failed or timed out, or was not installed yet (sessions that ended
before the upgrade that adds it), the line stays on disk unsent.
session-start runs sweep for its repository: a tracked session
(.tracevault/sessions/*) whose main transcript has unsent lines, and
whose unsent lines hold a cost-state, gets them sent the way its
SessionEnd hook would have. Unsent lines without one are left to the
session’s own hooks: it may still be running.
Only main transcripts are read: subagent files never hold a cost-state.
The sweep must never slow a session start much or fail it, so it is
capped: transcripts written in the last MAX_AGE, the MAX_SCANNED
most recent of them read, at most MAX_SESSIONS sent per run, at most
MAX_UNSENT_BYTES unsent per session, and no file read or request
started after its deadline. What a run leaves is picked up by the next
session start, while it is within the caps. Errors go to stderr.
It does not run when TRACEVAULT_PROJECT or
TRACEVAULT_PROJECT_ATTRIBUTION is set: they attribute what this process
sends, and an earlier session’s process may have had other values. For the
same reason a session is sent only under the attribution its own hooks
recorded (see [stream::catch_up_may_send]).
Structs§
- Candidate
- A session the sweep sends the rest of.
Constants§
- BUDGET
- The sweep’s own time budget, counted from its start.
- MAX_AGE
- How far back the sweep looks: Claude Code deletes transcripts older than
this by default (
cleanupPeriodDays), so older sessions are mostly gone. - MAX_
SCANNED - Sessions with unsent lines read per run, most recent first. Most have no report (killed sessions, or sessions still running), and they stay unsent, so without this cap every session start would read them all.
- MAX_
SESSIONS - Sessions sent per run, so one session start sends at most this many. A session skipped for its attribution does not count: it is never sent, and must not hold back older ones.
- MAX_
UNSENT_ BYTES - A session with more unsent than this lost much more than its report (every hook of it failed), and reading it on every session start would cost too much; the sweep leaves it.
Functions§
- candidates
- The sessions under
project_rootwhose unsent transcript lines hold acost-state, most recently written first, within the caps; no transcript is read afterdeadline. The current session is left out: its own hooks send its lines. - deadline
- When a sweep starting
now, in a hook that started athook_started, must stop starting work:BUDGETfrom now, but never past [HOOK_LATEST] into the hook. - sweep
- Send the rest of every
candidatessession ofproject_root, as itsSessionEndhook would, starting nothing afterdeadline, untilMAX_SESSIONSare sent. Stops at the first session the server did not take in full: the rest wait for the next session start.