Skip to content

Latest commit

 

History

History
95 lines (70 loc) · 4.19 KB

transcript_ingest.md

File metadata and controls

95 lines (70 loc) · 4.19 KB

transcript_ingest.py

Revision: 1 Last modified: 2026-12-04T19:00:00Z

Overview

transcript_ingest.py converts real Claude Code transcripts into usage-only telemetry: token counts, model identifiers and structural ids.

Its defining constraint is a privacy boundary, not a feature. Real transcripts contain full conversational content (message["content"]). This module extracts and persists only usage counts, model identifiers and structural ids — session id, agent id, message id, timestamps. It never touches message["content"], or any nested field under it (for example a tool_use block's input.description), by dict-key access, anywhere in the file, for any purpose — not to hash it, not to log it, not to measure its length.

That rule is stated at the top of the file (§11.4.10 credential safety) because the obvious "helpful" addition — hashing content to dedupe messages, or logging a length — would silently copy the substance of a private conversation into a telemetry store. The tool exists to make token attribution possible without that.

Prerequisites

  • python3 (stdlib only).
  • Read access to a Claude Code transcript store.
  • A configuration naming what to ingest. An invalid configuration is refused before anything is read or written.
  • No credentials are required, and the tool must never be given any.

Usage examples

# ingest transcripts into usage-only telemetry
python3 scripts/fastcycle/tokens/transcript_ingest.py \
  --config token_ingest.yaml --out usage.json

# a run that finds nothing to ingest is still a success
python3 scripts/fastcycle/tokens/transcript_ingest.py \
  --config token_ingest.yaml --out usage.json
# exit 0, output records that nothing was found

Configuration values are validated as non-negative numbers; anything else is refused with a ConfigError (exit 2) rather than coerced.

Exit codes

Code Meaning
0 Successful run — including "found nothing to ingest". An empty result is a legitimate outcome, not a failure.
2 ConfigError — the configuration is invalid or a value is of the wrong type. Raised before anything is read or written.

Edge cases

  • A transcript directory with nothing new. Exit 0 with an empty result. Reporting this as an error would train callers to ignore the exit code.
  • A malformed configuration. Refused with 2 before any file is opened — so a bad config cannot leave a half-written telemetry file behind.
  • A non-negative-number field given a string or negative value. ConfigError (2); never silently coerced, because a coerced token count is a wrong token count.
  • A transcript containing a tool_use block with a description. The description is not extracted. This is the intended behaviour, and the reason the rule is "no dict-key access to content, ever".
  • A transcript with no usage metadata. Nothing to attribute; contributes nothing rather than a zero row that would dilute an average.

Internal behaviour

  1. Loads and validates the configuration; a ConfigError exits 2 before any I/O.
  2. Enumerates the transcripts named by the configuration.
  3. For each message, extracts only usage counts, model identifiers and structural ids (session id, agent id, message id, timestamps).
  4. Aggregates the usage-only telemetry.
  5. Writes the output and exits 0 (including the empty case).

The content of a message is never accessed, hashed, logged or measured anywhere in this sequence.

Related scripts

Guarded by scripts/fastcycle/tests/test_token_attribution_red.sh (T020).

Feeds token attribution and, downstream, the token baseline in docs/scripts/collect_baseline.py.md (which reports UNMEASURED for periods that predate this instrument).

Contract/plan: T038, plan.md T-A06, FR-013, FR-001, SC-005, SC-001.

See also docs/scripts/dispatch_stamp.sh.md (the sibling token-accounting hook) and docs/scripts/fc_common.py.md.

Last verified

2026-12-04 — python3 -c "import ast;ast.parse(open('scripts/fastcycle/tokens/transcript_ingest.py').read())" → exit 0. python3 scripts/fastcycle/tokens/transcript_ingest.py with no arguments exits 2 (required configuration absent).