Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(savings): stop inventing a rate, and price each event from its own evidence (#2468)

Raghav Chamadiya533f20fa2251
diff --git a/packages/cli/src/repowise/cli/rewrite_hook.py b/packages/cli/src/repowise/cli/rewrite_hook.py
index a28675889..04358aaf5 100644
--- a/packages/cli/src/repowise/cli/rewrite_hook.py
+++ b/packages/cli/src/repowise/cli/rewrite_hook.py
@@ -72,9 +72,12 @@ from repowise.cli.agent_adapters.base import (
# deferred into the writer, so a command that bails never opens a database.
from repowise.cli.hook_ledger import BAILED, REWRITTEN
from repowise.cli.shell_lexer import (
+ READONLY_SEGMENT_TOOLS,
SAFE_FINAL_TOOLS,
analyze_pipeline,
is_plain_stdin_filter,
+ is_read_only_segment,
+ parse_redirect,
tokenize,
)
@@ -346,6 +349,9 @@ def _chain_families(command: str) -> tuple[str, ...] | None:
- a command ``_classify_head`` recognizes (the same closed set a lone
command must be in),
- an inert builtin (``_INERT_SEGMENT_TOKENS``),
+ - a read-only invocation of ``sed``/``cat``/``wc``/``sort``
+ (``is_read_only_segment`` -- the *form* is checked, not the name,
+ because ``sed -i`` and ``sort -o`` write),
- a bare stdin filter on the right of a pipe (``SAFE_FINAL_TOOLS``),
and at least one segment is recognized. That rule is the whole safety
@@ -391,9 +397,17 @@ def _chain_families(command: str) -> tuple[str, ...] | None:
# see. A stderr redirect only decides whether distill's
# errors-first rendering has errors to lead with, which is the
# caller's business either way.
- if not token.text.startswith("2"):
+ #
+ # Both halves are asked of the parsed token rather than its text.
+ # `startswith("2")` called `21>` a stderr redirect, so
+ # `git diff 21>f` truncated `f` inside a command classified as a
+ # git diff; and skipping the next word unconditionally swallowed
+ # a real argument whenever the redirect carried its own target,
+ # which reached across the `&&` into the following segment.
+ descriptor, takes_target = parse_redirect(token.text)
+ if descriptor != "2":
return None
- skip_next_arg = True
+ skip_next_arg = takes_target
if skip_next_arg:
return None # trailing redirect with no target: malformed, bail
@@ -410,6 +424,16 @@ def _chain_families(command: str) -> tuple[str, ...] | None:
first = normalized.split(None, 1)[0]
if first in _INERT_SEGMENT_TOKENS:
continue
+ if first in READONLY_SEGMENT_TOOLS:
+ # Read-only `sed`/`cat`/`wc`/`sort`. Inert for the same reason the
+ # builtins above are: the agent could already run them, so
+ # wrapping a chain containing one grants nothing. Unlike those,
+ # the tool is only inert in *some* invocations -- `sed -i` and
+ # `sort -o` write -- so the form is checked rather than the name,
+ # and an unlisted flag declines the whole chain.
+ if not is_read_only_segment(segment):
+ return None
+ continue
if index in piped_from and first in SAFE_FINAL_TOOLS:
# grep/tail are both producer families and stdin filters, and on
# the right of a pipe they are the latter -- so this has to be
diff --git a/packages/cli/src/repowise/cli/shell_lexer.py b/packages/cli/src/repowise/cli/shell_lexer.py
index 46bd7ec6f..838804dda 100644
--- a/packages/cli/src/repowise/cli/shell_lexer.py
+++ b/packages/cli/src/repowise/cli/shell_lexer.py
@@ -26,11 +26,14 @@ from __future__ import annotations
from dataclasses import dataclass
__all__ = [
+ "READONLY_SEGMENT_TOOLS",
"SAFE_FINAL_TOOLS",
"Pipeline",
"Token",
"analyze_pipeline",
"is_plain_stdin_filter",
+ "is_read_only_segment",
+ "parse_redirect",
"render",
"tokenize",
]
@@ -207,6 +210,35 @@ def _basename(word: str) -> str:
return word.strip("\"'").rsplit("/", 1)[-1].rsplit("\\", 1)[-1]
+def parse_redirect(text: str) -> tuple[str, bool]:
+ """A redirect token as ``(file_descriptor, takes_a_following_word)``.
+
+ ``tokenize`` keeps a redirect whole -- the leading descriptor, the
+ operator, and a duplication target if one is attached -- so the two facts
+ a caller needs about it have to be read back out rather than guessed at
+ from the text.
+
+ Both were previously guessed at, and both were wrong:
+
+ - ``text.startswith("2")`` reads ``21>`` and ``20>`` as "the stderr
+ redirect", so ``git diff 21>f`` looked like a stderr merge while
+ actually truncating ``f`` on file descriptor 21. The descriptor is
+ ``"21"``, and only ``""`` or ``"2"`` are what that test meant.
+ - Assuming every redirect consumes the next word is false for the
+ duplication and close forms (``2>&1``, ``2>&-``), which carry their
+ target inside the token. A caller that skipped the next word anyway
+ swallowed a real argument -- and if the redirect ended a segment, it
+ swallowed the first word of the *next* one.
+
+ ``(descriptor, True)`` for ``2>``; ``("2", False)`` for ``2>&-``.
+ """
+ index = 0
+ while index < len(text) and text[index].isdigit():
+ index += 1
+ descriptor = text[:index]
+ return descriptor, "&" not in text[index:]
+
+
def _short_cluster(arg: str) -> str:
"""The bundled short flags in *arg*, or "" if it is not a short group."""
if len(arg) > 1 and arg[0] == "-" and arg[1] != "-":
@@ -260,6 +292,136 @@ def is_plain_stdin_filter(words: list[str]) -> bool:
return tool in SAFE_FINAL_TOOLS and not _disqualifies_final_stage(tool, words[1:])
+#: Tools admitted as inert chain segments, each with the flags that keep them
+#: read-only. An allowlist rather than a blocklist of the write flags: a
+#: blocklist has to know every way each tool can be made to write, and the
+#: cost of being wrong is a rewrite that is auto-allowed. A flag nobody
+#: listed declines the whole chain, which is the failure direction we want.
+#:
+#: Short flags are matched per letter so bundles (``-rn``) work. Long forms
+#: are listed whole; a ``--flag=value`` spelling is matched on its stem.
+_READONLY_SEGMENT_FLAGS: dict[str, tuple[frozenset[str], frozenset[str]]] = {
+ # ``cat`` writes nothing itself -- ``cat > f`` is the redirect, which the
+ # caller's redirect rule already declines. These are its display flags.
+ "cat": (frozenset("AbeEnstTuv"), frozenset({"--number", "--number-nonblank",
+ "--show-all", "--show-ends", "--show-nonprinting", "--show-tabs",
+ "--squeeze-blank"})),
+ "wc": (frozenset("clLmw"), frozenset({"--bytes", "--chars", "--lines",
+ "--max-line-length", "--words"})),
+ # ``-o``/``--output`` is the whole reason this is an allowlist: it makes
+ # ``sort`` a writer, and it is the one flag a reader would forget.
+ "sort": (frozenset("bdfghikMnrstuVz"), frozenset({"--dictionary-order",
+ "--general-numeric-sort", "--human-numeric-sort", "--ignore-case",
+ "--ignore-leading-blanks", "--key", "--month-sort", "--numeric-sort",
+ "--reverse", "--sort", "--stable", "--unique", "--version-sort",
+ "--zero-terminated"})),
+ # ``sed`` is admitted on its script as well as its flags -- see
+ # ``_sed_script_is_read_only``. ``-e`` and ``-f`` are absent on purpose:
+ # ``-f`` reads a script file this cannot vet, and ``-e`` moves the script
+ # into a position the one-script rule below does not model.
+ "sed": (frozenset("nrsEz"), frozenset({"--quiet", "--silent",
+ "--regexp-extended", "--separate", "--null-data"})),
+}
+
+READONLY_SEGMENT_TOOLS = frozenset(_READONLY_SEGMENT_FLAGS)
+
+
+def _is_sed_address(part: str) -> bool:
+ """True for a line number. Regex addresses are not admitted.
+
+ ``$`` (last line) is a valid sed address and is deliberately absent: the
+ hook's chain gate bails on ``$`` anywhere in the command before a segment
+ is examined, so admitting it here would be unreachable code that reads as
+ a supported shape. Rejecting is the safe direction for any other caller.
+ """
+ return part.isdigit()
+
+
+def _sed_script_is_read_only(operands: list[str]) -> bool:
+ """True when ``sed``'s script is an optional line address/range then ``p``.
+
+ Deliberately far narrower than "scripts that do not write". Vetting flags
+ alone is not enough for ``sed``, because the script is a language:
+ ``w``/``W`` and ``s///w`` write files, ``e`` and ``s///e`` execute shell
+ commands, and ``r``/``R`` splice files in. A regex address would also
+ have to be parsed to find where the command letter even starts.
+
+ Every ``sed`` in the measured corpus is ``sed -n '<range>p' <file>``, so
+ the narrow rule costs nothing real and needs no argument about which
+ script commands are safe. Written as a hand parser rather than a regex
+ because this module commits to importing nothing, and ``test_rewrite_perf``
+ pins that.
+
+ The first non-flag operand is the script and the rest are files, which
+ holds only while ``-e``/``-f`` are rejected -- which is why they are.
+ """
+ if not operands:
+ return False
+ script = operands[0].strip("\"'").strip()
+ if script.endswith(";"):
+ script = script[:-1].strip()
+ if not script.endswith("p"):
+ return False
+ address = script[:-1].strip()
+ if not address:
+ return True # a bare ``p``: print every line
+ start, separator, end = address.partition(",")
+ if separator and not _is_sed_address(end.strip()):
+ return False
+ return _is_sed_address(start.strip())
+
+
+def is_read_only_segment(words: list[str]) -> bool:
+ """True when *words* is a read-only invocation of an admitted tool.
+
+ The chain gate treats such a segment as inert: wrapping a chain whose
+ every segment is inert or already recognized grants the agent nothing it
+ could not already run, and a rewrite is auto-allowed, so "reads and
+ cannot be made to write" is the bar rather than "usually harmless".
+
+ A bare ``-`` is stdin, not a flag. Anything else starting with ``-`` must
+ be in this tool's allowlist, and an unknown flag declines.
+ """
+ if not words:
+ return False
+ tool = _basename(words[0])
+ rules = _READONLY_SEGMENT_FLAGS.get(tool)
+ if rules is None:
+ return False
+ operands = _allowed_operands(words[1:], rules)
+ if operands is None:
+ return False
+ if tool == "sed":
+ return _sed_script_is_read_only(operands)
+ return True
+
+
+def _allowed_operands(
+ args: list[str], rules: tuple[frozenset[str], frozenset[str]]
+) -> list[str] | None:
+ """The non-flag arguments in *args*, or ``None`` if any flag is unlisted.
+
+ A bare ``-`` is stdin, not a flag. Everything else starting with ``-``
+ has to be in this tool's allowlist; an unknown flag declines the segment.
+ """
+ short, long = rules
+ operands: list[str] = []
+ for arg in args:
+ bare = arg.strip("\"'")
+ if bare.startswith("--"):
+ if bare.split("=", 1)[0] not in long:
+ return None
+ elif bare.startswith("-") and bare != "-":
+ cluster = _short_cluster(bare)
+ # A cluster of unknown letters, or one that swallowed a value
+ # (``-o out``), declines: every admitted letter is a pure switch.
+ if not cluster or any(letter not in short for letter in cluster):
+ return None
+ else:
+ operands.append(arg)
+ return operands
+
+
def analyze_pipeline(command: str) -> Pipeline | None:
"""Describe *command* as a simple pipeline, or return None to bail.
diff --git a/packages/core/src/repowise/core/distill/engine.py b/packages/core/src/repowise/core/distill/engine.py
index 29cf16b3c..2530cad6c 100644
--- a/packages/core/src/repowise/core/distill/engine.py
+++ b/packages/core/src/repowise/core/distill/engine.py
@@ -45,9 +45,23 @@ MIN_SAVED_TOKENS = 40
#: How much of one command's output an agent host actually delivers. Claude
#: Code truncates a Bash/PowerShell tool result at 30,000 characters, so
#: everything past this was never going to reach the model and distilling it
-#: away saves nothing. Applied to every source, including ``cli``: a human at
-#: a real terminal has no such cap, so this undersells for them, and the
-#: ledger is meant to be a floor rather than a best case.
+#: away saves nothing.
+#:
+#: Applied to every source here, which is the *smallest* known host cap and
+#: so an undersell for the others rather than an overclaim: a human at a real
+#: terminal has no cap at all, and Codex's own measured cap is 40,000
+#: characters. This path cannot do better, deliberately -- it records
+#: ``agent: "unknown"`` because knowing the rewrite hook's shell is not
+#: knowing which agent ran it, and the contract says an unknown field stays
+#: unknown rather than being filled with the likeliest answer. Choosing a cap
+#: per agent here would be exactly that guess.
+#:
+#: The transcript backfill *does* know the harness, having read that
+#: harness's own file, so it applies a per-agent cap
+#: (``savings/transcript.py:_HARNESS_OUTPUT_CAP_TOKENS``). The two therefore
+#: disagree by at most 2,500 tokens for one Codex result distilled live
+#: versus recovered from a transcript. Recorded rather than hidden; closing
+#: it needs an agent identity this path has decided not to invent.
HOST_OUTPUT_CAP_CHARS = 30_000
diff --git a/packages/core/src/repowise/core/generation/cost_tracker.py b/packages/core/src/repowise/core/generation/cost_tracker.py
index 1dcf8dec9..27f9ee0bb 100644
--- a/packages/core/src/repowise/core/generation/cost_tracker.py
+++ b/packages/core/src/repowise/core/generation/cost_tracker.py
@@ -104,9 +104,15 @@ def is_local_model(model: str) -> bool:
model == "mock"
or model.startswith(_LOCAL_MODEL_PREFIXES)
or model.startswith(("codex_cli/", "claude_cli/", "opencode/"))
- # Bare Ollama tags carry no prefix — the default is plain `qwen3.5:4b`,
- # and a `family:size` tag is not a shape any hosted vendor uses.
- or (":" in model and "/" not in model)
+ # Bare Ollama tags carry no prefix — the default is plain `qwen3.5:4b`.
+ # The trailing tag must not be a bare number: Bedrock addresses hosted
+ # Anthropic models as `anthropic.claude-sonnet-4-5-20250929-v1:0`, and
+ # reading that as local priced a Bedrock user's whole history at $0.00
+ # while stamping it as measured — a fabricated rate, just a zero one,
+ # and worse than `None` because it is indistinguishable from a real
+ # local-model zero. Ollama size tags (`:8b`, `:latest`, `:q4_K_M`) are
+ # never purely numeric, so the two shapes separate cleanly.
+ or (":" in model and "/" not in model and not model.rsplit(":", 1)[1].isdigit())
)
@@ -157,8 +163,20 @@ def _routed_model_leaf(model: str) -> str:
return model.rsplit("/", 1)[-1]
-def _get_pricing(model: str) -> dict[str, float]:
- """Return pricing for *model*, falling back and warning if unknown."""
+def resolve_model_pricing(model: str) -> dict[str, float] | None:
+ """Pricing for *model*, or ``None`` when this table does not know it.
+
+ The honest half of the lookup, and the one a caller that *records* a rate
+ has to use. :func:`get_model_pricing` answers the same question but
+ substitutes the default tier for a miss, and once that substitute has been
+ written down it is indistinguishable from a measurement: a saving stamped
+ $3/$15 reads as real whether the table knew the model or invented it.
+
+ So anything that stores a rate asks here and stays unpriced on ``None``.
+ Anything merely displaying a rough figure may still take the fallback.
+
+ A local model is a real answer of zero, not a miss.
+ """
if is_local_model(model):
return {"input": 0.0, "output": 0.0}
if model in _PRICING:
@@ -166,9 +184,14 @@ def _get_pricing(model: str) -> dict[str, float]:
leaf = _routed_model_leaf(model)
if leaf in _PRICING:
return _PRICING[leaf]
- family = _family_pricing(model) or _family_pricing(leaf)
- if family is not None:
- return family
+ return _family_pricing(model) or _family_pricing(leaf)
+
+
+def _get_pricing(model: str) -> dict[str, float]:
+ """Return pricing for *model*, falling back and warning if unknown."""
+ resolved = resolve_model_pricing(model)
+ if resolved is not None:
+ return resolved
if model not in _warned_models:
log.warning("cost_tracker.unknown_model", model=model, fallback=_FALLBACK_PRICING)
_warned_models.add(model)
@@ -181,6 +204,9 @@ def get_model_pricing(model: str) -> dict[str, float]:
Unknown models fall back to the default tier (with a one-time warning).
Used outside generation (e.g. ``repowise saved``) to turn token counts
into dollar estimates with the same table the cost ledger uses.
+
+ Prefer :func:`resolve_model_pricing` when the rate will be *stored*: this
+ one cannot tell the caller that the number it returned was invented.
"""
return _get_pricing(model)
diff --git a/packages/core/src/repowise/core/savings/pricing.py b/packages/core/src/repowise/core/savings/pricing.py
index cd8a64ebe..527ce8e7d 100644
--- a/packages/core/src/repowise/core/savings/pricing.py
+++ b/packages/core/src/repowise/core/savings/pricing.py
@@ -148,35 +148,58 @@ def _cache_path(repo_root: Path) -> Path:
return repo_root / ".repowise" / "omissions" / _CACHE_NAME
-def _scan(repo_root: Path) -> PricingSnapshot | None:
- """Detect the coding agent's model and freeze its rates.
-
- Returns ``None`` rather than an over-long model id. The id is read out of a
- transcript the user's agent wrote, and the event contract bounds ``model``
- at 128 characters -- by *raising*, which would drop the whole event before
- it was written. A pricing snapshot we cannot use must cost the event its
- price, never its existence.
+def snapshot_for_model(model: str | None, pricing_source: str) -> PricingSnapshot | None:
+ """Freeze *model*'s rates, or ``None`` when it cannot be priced honestly.
+
+ The single place a model id becomes a stored rate, so every surface that
+ writes one refuses the same three things:
+
+ - **No model.** Nothing to look up.
+ - **An over-long id.** The event contract bounds ``model`` at 128
+ characters by *raising*, which would drop the whole event before it was
+ written. A snapshot we cannot use must cost the event its price, never
+ its existence.
+ - **A model the rate table does not know.** This is the one that used to
+ get through. ``get_model_pricing`` answers a miss with the default tier
+ and a warning into a log no caller reads, so a session on an
+ unrecognised model was stamped $3/$15 and marked with a real-looking
+ provenance -- a fabricated rate wearing a measurement's clothes.
+ :func:`resolve_model_pricing` says ``None`` instead, and an unpriced
+ event is a correct event: the report counts priced and unpriced tokens
+ separately for exactly this reason.
+
+ Non-model labels (``codex-auto-review``, ``<synthetic>``) need no separate
+ rule -- they are unknown to the table, so the third refusal already covers
+ them.
"""
- from repowise.core.distill.session_model import resolve_session_model
- from repowise.core.generation.cost_tracker import get_model_pricing, pricing_table_version
+ from repowise.core.generation.cost_tracker import pricing_table_version, resolve_model_pricing
- resolved = resolve_session_model(repo_root)
- if not resolved.model or len(resolved.model) > _MAX_MODEL_LENGTH:
+ if not model or len(model) > _MAX_MODEL_LENGTH:
+ return None
+ rates = resolve_model_pricing(model)
+ if rates is None:
return None
- rates = get_model_pricing(resolved.model)
return PricingSnapshot(
- model=resolved.model,
- # Machine-readable provenance rather than the detector's human label,
- # so a consumer can tell a detected rate from the default without
- # parsing prose. ``unknown`` is the detector's own word for "nothing
- # found", and stays distinguishable from a real agent.
- pricing_source=f"session_model:{resolved.agent}",
+ model=model,
+ pricing_source=pricing_source,
pricing_version=pricing_table_version(),
input_rate_usd_per_million=float(rates["input"]),
output_rate_usd_per_million=float(rates["output"]),
)
+def _scan(repo_root: Path) -> PricingSnapshot | None:
+ """Detect the coding agent's model and freeze its rates."""
+ from repowise.core.distill.session_model import resolve_session_model
+
+ resolved = resolve_session_model(repo_root)
+ # Machine-readable provenance rather than the detector's human label, so a
+ # consumer can tell a detected rate from the default without parsing prose.
+ # ``unknown`` is the detector's own word for "nothing found", and stays
+ # distinguishable from a real agent.
+ return snapshot_for_model(resolved.model, f"session_model:{resolved.agent}")
+
+
def _read_cache(path: Path) -> tuple[PricingSnapshot, float] | None:
"""The cached snapshot and its age in seconds, or ``None`` if unusable."""
try:
diff --git a/packages/core/src/repowise/core/savings/transcript.py b/packages/core/src/repowise/core/savings/transcript.py
index e555e967f..e81a187c8 100644
--- a/packages/core/src/repowise/core/savings/transcript.py
+++ b/packages/core/src/repowise/core/savings/transcript.py
@@ -22,8 +22,10 @@ Three rules it does not get to relax:
- **One event per omission ref.** The ref is content-addressed, so the same
ref twice is the same distilled output. Counting it once undersells a
genuine repeat, which is the floor this ledger is meant to be.
-- **Never repriced.** These happened at rates nobody recorded, so they are
- counted in tokens and left unvalued rather than valued at today's rate.
+- **Never repriced.** A past saving is never revalued at today's model. That
+ is not the same as leaving it unvalued: the transcript names the model that
+ was in the chair, so each event is priced from *its own* evidence, and one
+ whose model the rate table does not know stays unpriced rather than guessed.
"""
from __future__ import annotations
@@ -73,6 +75,52 @@ SYNC_BUDGET_S = 20.0
#: is ``len // 4``, so capping characters and capping tokens agree exactly.
HOST_OUTPUT_CAP_TOKENS = HOST_OUTPUT_CAP_CHARS // 4
+#: The largest shell result Codex was observed to actually deliver, across
+#: 95,814 of them in this repository. A host cannot have truncated below what
+#: it demonstrably handed the model, so this is a *measured* lower bound on
+#: Codex's cap rather than an estimate of one.
+#:
+#: Two observations sit behind this number and they disagree, so both are
+#: recorded:
+#:
+#: - Most large Codex results stop at a plateau: above 5,000 characters the
+#: commonest lengths are 24,133 (164 texts) and a tight cluster at
+#: 40,100-40,104, over 200 texts within four characters of one another.
+#: That is a cap with a variable-length truncation notice after it. Claude
+#: Code's plateau is the same shape and sharper -- 24 texts at exactly
+#: 30,000, nothing above it.
+#: - But Codex does not always stop there. Results run to 159,585, 434,762,
+#: 1,510,994 and 2,552,250 characters, none of them truncated.
+#:
+#: So the plateau is a cap Codex applies *sometimes*, and picking it would
+#: undercount every result that escaped it. Set at the maximum instead, on
+#: the standing preference that a figure this ledger reports should err high
+#: rather than low. That is a real trade and not a free one: the five largest
+#: events become 29% of the recorded total, each crediting a single command
+#: with more tokens than a model's context can hold. The plateau figure
+#: (40,000 characters) is the conservative alternative and costs about 4x.
+#:
+#: Claude Code needs no equivalent choice -- its plateau *is* its maximum.
+_CODEX_OUTPUT_CAP_CHARS = 2_552_250
+
+#: Per-harness output cap, in tokens. ``estimate_tokens`` is ``len // 4``, so
+#: capping characters and capping tokens agree exactly.
+#:
+#: The defect this replaces: ``HOST_OUTPUT_CAP_CHARS`` is exactly right for
+#: Claude Code, and its own comment said it was "applied to every source".
+#: Charging it to Codex clipped 306 of this ledger's events to 7,500 tokens
+#: and left 111 of them delivering *more* than their own baseline, which is
+#: incoherent for a measured pair and banks zero.
+#:
+#: An unlisted harness gets ``None``, meaning no cap: a cap is a claim that a
+#: host truncated, and asserting one we never observed is precisely how this
+#: happened. ``_accounting`` never clips below what was actually delivered,
+#: so the invariant holds whatever this table says.
+_HARNESS_OUTPUT_CAP_TOKENS: dict[str, int | None] = {
+ "claude_code": HOST_OUTPUT_CAP_TOKENS,
+ "codex": _CODEX_OUTPUT_CAP_CHARS // 4,
+}
+
#: Cursors for this surface alone. Sharing the decision miner's would starve
#: whichever ran second, silently: that cursor advances as bytes are read.
_CURSOR_FILENAME = "transcript-cursors.json"
@@ -111,6 +159,13 @@ class _Candidate:
occurred_at: datetime
delivered_tokens: int
session_id: str | None
+ #: The model in the chair when this output was read back, or ``None``.
+ #: Not read off the event carrying the marker: a tool *result* is a user
+ #: line in every harness here and names no model. It is the most recent
+ #: assistant line before it in the same transcript -- the turn that ran
+ #: the command and then read the answer. Evidence that existed when the
+ #: saving happened, which is the whole basis for pricing this population.
+ model: str | None = None
def sync_transcript_savings(
@@ -213,20 +268,34 @@ def _sweep(
def _gate(adapter: HarnessAdapter) -> RawPrefilter | None:
- """The adapter's tool-call gate, widened to keep every marker line.
+ """The adapter's tool-call gate, widened to keep marker and model lines.
The tool call and the result carrying its marker are separate lines, and
only the pair says a marker came from a shell command, so a marker-only
gate sees results it can no longer attribute. Widening rather than
narrowing matters because the cursor advances per line read: a line this
gate drops is consumed, not revisited.
+
+ The model is the same shape of problem one line further out, and it cost
+ 93% of the ledger's price. Codex states its model on a ``turn_context``
+ line that carries no tool call, so the unwidened gate consumed it and
+ every Codex event was written unpriced -- measured at 0 of 1,219
+ candidates here, against 25 of 27 for Claude Code, which happens to state
+ its model on the same assistant line as the tool call.
+
+ A substring rather than an adapter method: both harnesses spell it
+ ``"model"`` in the JSON, one extra line normalized is free, and a method
+ with two implementations and one caller is a worse answer than the test
+ it would wrap. Costs ~4.8% more lines on Codex and ~1.1% on Claude Code,
+ on a surface that runs at ``init`` and on demand under a 20-second
+ budget -- never inside an agent's tool call.
"""
tool_gate = adapter.prefilter(INTENT_TOOL_CALLS)
if tool_gate is None:
return None
def gate(raw_line: str) -> bool:
- return tool_gate(raw_line) or "repowise#" in raw_line
+ return tool_gate(raw_line) or "repowise#" in raw_line or '"model"' in raw_line
return gate
@@ -240,7 +309,13 @@ def _collect(
) -> set[str]:
"""Collect markers from shell results; return shell calls left unanswered."""
shell_calls: set[str] = set()
+ # Carried forward rather than read per event: see ``_Candidate.model``.
+ # Scoped to this call, which is one transcript, so a model never leaks
+ # across sessions. A read resuming mid-file starts with None and leaves
+ # its first markers unpriced, which is the right way to be wrong here.
+ model: str | None = None
for event in events:
+ model = _carried_model(event, model)
for use in event.tool_uses:
if use.name in shell_tools:
shell_calls.add(use.id)
@@ -250,7 +325,7 @@ def _collect(
shell_calls.discard(block.tool_use_id)
if not _in_repo(event, repo_root):
continue
- _harvest(block, event, harness, candidates)
+ _harvest(block, event, harness, candidates, model)
return shell_calls
@@ -259,6 +334,7 @@ def _harvest(
event: Event,
harness: str,
candidates: dict[str, _Candidate],
+ model: str | None = None,
) -> None:
for text in _result_texts(block):
for marker in parse_markers(text):
@@ -270,6 +346,7 @@ def _harvest(
occurred_at=_occurred_at(event),
delivered_tokens=estimate_tokens(text),
session_id=event.session_id,
+ model=model,
),
)
@@ -332,19 +409,59 @@ def _strings_in(blob: Any) -> Iterable[str]:
yield from (value for value in blob.values() if isinstance(value, str))
+def _carried_model(event: Event, current: str | None) -> str | None:
+ """The model in the chair after *event*, given it was *current* before.
+
+ Two lines are refused rather than carried, and both were found by review
+ rather than by the numbers, because both fail quietly:
+
+ - **A sidechain.** Claude Code interleaves Task sub-agent lines into the
+ main transcript and a sub-agent can run a different model, so carrying
+ one prices the main thread's next command at the sub-agent's rate -- a
+ 5x error where a Haiku sub-agent lands between an Opus tool call and
+ its result. Every other miner here filters them for the same reason
+ (``sessions/miners/decisions.py``, ``precedent``, ``decisions``).
+ - **A sentinel.** Claude Code writes ``<synthetic>`` for an API error or
+ an interrupted message. It resolves to no rate, which is right, but it
+ has to be refused *here* too: letting it overwrite a known-good model
+ leaves every later marker in the file unpriced. Angle brackets are the
+ shape harnesses use for "not a real value".
+ """
+ if not event.model or event.sidechain or event.model.startswith("<"):
+ return current
+ return event.model
+
+
def _accounting(candidate: _Candidate) -> tuple[int, int]:
"""``(baseline, delivered)`` input tokens for one recovered marker.
``delivered`` is the text the marker was found in, which for a shell
result is the command output the model read back. ``baseline`` adds back
- what the marker says was dropped, minus the
- marker's own cost, then re-applies the host truncation the live path
- applies -- bytes past it never reached the model and cannot be claimed.
+ what the marker says was dropped, minus the marker's own cost, then
+ re-applies **this harness's** truncation -- bytes past it never reached
+ the model and cannot be claimed.
+
+ Whose truncation matters, and it used to be nobody's in particular:
+ Claude Code's 30,000-character cap was charged to every source, Codex
+ included, and Codex does not truncate. See
+ ``_HARNESS_OUTPUT_CAP_TOKENS`` for the measurement.
+
+ The cap never clips below ``delivered``. A host cannot have truncated
+ below what it demonstrably handed the model, so this is not a fudge but
+ the definition -- and it keeps the invariant true for a harness nobody
+ has measured yet, which is the case that produced this bug.
"""
delivered = candidate.delivered_tokens
kept = max(delivered - estimate_tokens(candidate.marker.text), 0)
stored = kept + candidate.marker.tokens_omitted
- return min(stored, HOST_OUTPUT_CAP_TOKENS), delivered
+ cap = _HARNESS_OUTPUT_CAP_TOKENS.get(candidate.harness)
+ capped = stored if cap is None else min(stored, cap)
+ # The floor applies on both branches. An uncapped harness can still land
+ # under ``delivered`` when the marker's own text costs more than what it
+ # says was omitted, and that would store a baseline smaller than the
+ # delivery it describes -- the very incoherence this function now exists
+ # to rule out.
+ return max(capped, delivered), delivered
def _payload(
@@ -375,9 +492,30 @@ def _payload(
"delivered_input_tokens": delivered,
"omission_refs": (candidate.marker.ref,),
"session_id": candidate.session_id,
+ **_pricing_payload(candidate),
}
+def _pricing_payload(candidate: _Candidate) -> dict[str, Any]:
+ """The rate fields for one recovered marker, empty when it cannot be priced.
+
+ Not a departure from "never repriced" but the point of it. The rule exists
+ so a past saving is never revalued at *today's* model; this prices each
+ event at the model that was in the chair when it happened, read out of the
+ same transcript line that proves the saving. The evidence was always there
+ and the first version of this module simply dropped it, which left 93% of
+ the ledger's tokens carrying no rate at all.
+
+ Empty when the model is unknown to the rate table. An unpriced event is
+ the honest outcome and the report already counts the two populations
+ apart; a guessed rate would be worse than the silence it replaced.
+ """
+ from repowise.core.savings.pricing import snapshot_for_model
+
+ snapshot = snapshot_for_model(candidate.model, f"transcript_model:{candidate.harness}")
+ return snapshot.as_payload() if snapshot is not None else {}
+
+
def _apply(
repo_root: Path,
candidates: dict[str, _Candidate],
diff --git a/tests/unit/distill/test_rewrite_hook.py b/tests/unit/distill/test_rewrite_hook.py
index 3831ecfcd..7ae0efad6 100644
--- a/tests/unit/distill/test_rewrite_hook.py
+++ b/tests/unit/distill/test_rewrite_hook.py
@@ -338,6 +338,85 @@ class TestSafeTails:
def test_unsafe_chains_pass_through(self, command) -> None:
assert classify(command) is None
+ @pytest.mark.parametrize(
+ ("command", "family"),
+ [
+ # Phase 1c: read-only `sed`/`cat`/`wc`/`sort` are inert chain
+ # segments. Every shape here is taken verbatim from the measured
+ # 30-day corpus, which is overwhelmingly `sed -n '<range>p' f`.
+ ("cd pkg && sed -n '1,60p' README.md && git diff", "git_diff"),
+ ("cd pkg && cat notes.md && git diff", "git_diff"),
+ ("git diff && wc -l src/a.py src/b.py", "git_diff"),
+ ("git log -3 && sort -u names.txt", "git_log"),
+ ("ls a && sed -n 'p' f.txt", "file_listing"),
+ ("ls a && cat -n f.txt", "file_listing"),
+ # The confinement for the redirect fix: a real stderr redirect
+ # still works, with and without an inline target.
+ ("git diff 2>/dev/null && ls a", "git_diff"),
+ ("pytest 2>&1|sed -n '1,5p'", "test_output"),
+ ("ls a && wc -lw f.txt", "file_listing"),
+ ],
+ )
+ def test_read_only_forms_are_inert_chain_segments(self, command, family) -> None:
+ assert classify(command) == family
+
+ @pytest.mark.parametrize(
+ "command",
+ [
+ # The guard. Each of these is the *same tool* in a form that
+ # writes, executes, or reads a file we cannot vet. A rewrite is
+ # auto-allowed, so admitting the name rather than the form would
+ # hand the agent an approval nobody granted.
+ "git diff && sed -i 's/a/b/' f.py", # in-place edit
+ "git diff && sed --in-place 's/a/b/' f.py",
+ "git diff && sort -o sorted.txt names.txt", # -o writes
+ "git diff && sort --output=sorted.txt names.txt",
+ "git diff && sed -n '1,5w out.txt' f.py", # script writes a file
+ "git diff && sed -n '1,5e rm -rf x' f.py", # script executes
+ "git diff && sed -n '/secret/p' f.py", # regex address not admitted
+ "git diff && sed -f script.sed f.py", # unvettable script file
+ "git diff && sed -e '1,5p' f.py", # -e moves the script operand
+ "git diff && sed -n '1,5d' f.py", # not a print
+ "git diff && cat --help-me f.py", # unknown long flag
+ "git diff && wc --files0-from=list f", # reads a file list
+ "git diff && cat f.py > out.txt", # stdout redirect
+ # A redirect that carries its own target consumes no following
+ # word. Skipping one anyway swallowed the next argument -- and
+ # across the `&&`, the next segment's first word. Both of these
+ # were rewritten and auto-allowed, and `sort -o` writes the file.
+ "pytest && sort 2>&- -o evil.txt in.txt",
+ "pytest && sort 2>&2 -o evil.txt in.txt",
+ "pytest 2>&- && sudo npm test",
+ # `startswith("2")` read these as "the stderr redirect". They
+ # truncate evil.txt on file descriptor 21 / 20.
+ "git diff 21>evil.txt && ls",
+ "git diff 20>evil.txt && ls",
+ # `$` is a valid sed address, but the chain gate bails on `$`
+ # before any segment is looked at, because expansion timing
+ # differs inside the wrapped shell. Recorded so the interaction
+ # is not rediscovered as a bug in the allowlist.
+ "ls a && sed -n '$p' f.txt",
+ ],
+ )
+ def test_a_writing_form_of_an_admitted_tool_still_declines(self, command) -> None:
+ assert classify(command) is None
+
+ def test_the_allowlist_is_what_declines_the_writing_forms(self) -> None:
+ """Proves the guard by breaking what it protects.
+
+ Without the per-form check these tools would be admitted on their
+ name alone, and `sed -i` -- a command that edits files in place --
+ would be wrapped and auto-allowed. Pinned here rather than trusted,
+ because the failure is silent: a rewrite that runs.
+ """
+ from repowise.cli import shell_lexer
+
+ writing = ["sed", "-i", "s/a/b/", "f.py"]
+ assert shell_lexer.is_read_only_segment(writing) is False
+ # And with the name admitted but the form unchecked, the chain gate
+ # would have said yes -- which is exactly the bug this prevents.
+ assert shell_lexer._basename(writing[0]) in shell_lexer.READONLY_SEGMENT_TOOLS
+
def test_chains_never_rewrite_in_powershell(self) -> None:
"""``&&`` is POSIX syntax; distill would hand this to cmd.exe."""
assert classify("ls a && ls b", SHELL_POWERSHELL) is None
diff --git a/tests/unit/savings/test_pricing_snapshot.py b/tests/unit/savings/test_pricing_snapshot.py
index 175f206bd..ce80d0dc5 100644
--- a/tests/unit/savings/test_pricing_snapshot.py
+++ b/tests/unit/savings/test_pricing_snapshot.py
@@ -330,3 +330,139 @@ def test_a_scan_whose_cache_write_fails_is_still_used(
# And the process does not scan again for the same repository.
assert resolve_pricing_snapshot(repo) is not None
assert len(calls) == 1
+
+
+# -- a rate the table did not know ------------------------------------------
+#
+# The defect these cover was live, and it was invisible precisely because it
+# succeeded: ``get_model_pricing`` answers a model it does not know with the
+# default tier and a warning into a log no caller reads, so a session on an
+# unrecognised model was stamped $3/$15 with a real-looking
+# ``session_model:<agent>`` provenance. Nothing downstream could tell that
+# from a measurement.
+
+
+def _resolving_to(monkeypatch: pytest.MonkeyPatch, model: str) -> None:
+ """Make the session detector report *model*, whatever is on this machine."""
+ from repowise.core.distill import session_model
+
+ monkeypatch.setattr(
+ session_model,
+ "resolve_session_model",
+ lambda *a, **k: session_model.ResolvedModel(
+ model=model, raw=model, agent="claude_code", source="detected"
+ ),
+ )
+
+
+def test_a_model_the_rate_table_does_not_know_is_left_unpriced(
+ repo: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _resolving_to(monkeypatch, "totally-made-up-model-xyz")
+
+ assert resolve_pricing_snapshot(repo) is None
+ # And nothing was cached, so the next caller does not read a rate back.
+ assert resolve_pricing_snapshot(repo, allow_scan=False) is None
+
+
+def test_the_unknown_rate_refused_is_the_fallback_that_used_to_be_stamped(
+ repo: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Pins *what* was being fabricated, not merely that something was.
+
+ Without this the test above would still pass if the fallback tier happened
+ to equal a real rate, which is exactly why the bug survived: $3/$15 is a
+ plausible number, and a plausible number is indistinguishable from a
+ measured one once it has been written down.
+ """
+ from repowise.core.generation import cost_tracker
+
+ model = "totally-made-up-model-xyz"
+ assert cost_tracker.resolve_model_pricing(model) is None
+ # The literals, not the module constant against itself: the point is that
+ # the fabrication is specifically $3/$15, a number plausible enough that
+ # nobody noticed it was invented.
+ assert cost_tracker.get_model_pricing(model) == {"input": 3.0, "output": 15.0}
+
+ _resolving_to(monkeypatch, model)
+ assert resolve_pricing_snapshot(repo) is None
+
+
[email protected]("label", ["codex-auto-review", "<synthetic>", "auto"])
+def test_a_label_that_is_not_a_model_at_all_is_left_unpriced(
+ repo: Path, monkeypatch: pytest.MonkeyPatch, label: str
+) -> None:
+ """These need no rule of their own: they are unknown to the table, so the
+ unknown-model refusal already covers them."""
+ _resolving_to(monkeypatch, label)
+
+ assert resolve_pricing_snapshot(repo) is None
+
+
+def test_a_model_the_table_does_know_is_still_priced(
+ repo: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The confinement. Refusing the unknown must not refuse everything."""
+ _resolving_to(monkeypatch, "claude-opus-5")
+
+ snapshot = resolve_pricing_snapshot(repo)
+ assert snapshot is not None
+ assert snapshot.model == "claude-opus-5"
+ assert snapshot.input_rate_usd_per_million == 5.0
+ assert snapshot.pricing_source == "session_model:claude_code"
+
+
+def test_a_family_prefix_is_a_real_rate_rather_than_a_miss(
+ repo: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """``gpt-5.6-sol`` is in no exact row but resolves by family, and the
+ family tier is a measurement. Refusing it would throw away 93% of this
+ machine's ledger for nothing."""
+ _resolving_to(monkeypatch, "gpt-5.6-sol")
+
+ snapshot = resolve_pricing_snapshot(repo)
+ assert snapshot is not None
+ assert snapshot.input_rate_usd_per_million == 2.5
+
+
+def test_a_local_model_is_priced_at_zero_rather_than_refused(
+ repo: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Zero is an answer, not an absence."""
+ _resolving_to(monkeypatch, "ollama/llama3")
+
+ snapshot = resolve_pricing_snapshot(repo)
+ assert snapshot is not None
+ assert snapshot.input_rate_usd_per_million == 0.0
+
+
+ "model",
+ [
+ "anthropic.claude-sonnet-4-5-20250929-v1:0",
+ "us.anthropic.claude-sonnet-4-5-v1:0",
+ ],
+)
+def test_a_bedrock_model_is_not_mistaken_for_a_free_local_one(model: str) -> None:
+ """The same defect as the fabricated rate, pointed the other way.
+
+ Bedrock addresses hosted Anthropic models as ``vendor.name-v1:0``, and the
+ bare-Ollama-tag heuristic read the trailing ``:0`` as a local tag -- so a
+ Bedrock user's whole backfill would be stamped $0.00 and marked measured.
+ A fabricated zero is worse than ``None``: it is indistinguishable from a
+ genuine local-model zero, and it silently zeroes the priced population
+ instead of moving it to the unpriced one.
+ """
+ from repowise.core.generation import cost_tracker
+
+ assert cost_tracker.is_local_model(model) is False
+ assert cost_tracker.resolve_model_pricing(model) is None
+
+
[email protected]("model", ["llama3:8b", "qwen3.5:4b", "mistral:latest", "ollama/llama3"])
+def test_a_real_local_model_is_still_free(model: str) -> None:
+ """The confinement. Ollama size tags are never purely numeric."""
+ from repowise.core.generation import cost_tracker
+
+ assert cost_tracker.is_local_model(model) is True
+ assert cost_tracker.resolve_model_pricing(model) == {"input": 0.0, "output": 0.0}
diff --git a/tests/unit/savings/test_transcript.py b/tests/unit/savings/test_transcript.py
index 79669f662..9646bc052 100644
--- a/tests/unit/savings/test_transcript.py
+++ b/tests/unit/savings/test_transcript.py
@@ -23,6 +23,7 @@ from repowise.core.savings import recorder
from repowise.core.savings.transcript import (
ESTIMATOR,
HOST_OUTPUT_CAP_TOKENS,
+ _Candidate,
sync_transcript_savings,
)
from repowise.core.sessions import transcript_dir_for
@@ -45,6 +46,7 @@ def _pair(
tool: str = "Bash",
block_id: str = "toolu_01",
ts: float = NOW,
+ model: str | None = None,
) -> list[dict]:
return [
{
@@ -53,6 +55,7 @@ def _pair(
"timestamp": _iso(ts),
"message": {
"role": "assistant",
+ **({"model": model} if model else {}),
"content": [
{
"type": "tool_use",
@@ -593,3 +596,283 @@ def test_every_marker_in_one_result_is_counted(repo: Path, projects: Path) -> No
_write(projects, repo, _pair(output, cwd=str(repo)))
assert _sync(repo, projects).recorded == 2
+
+
+# -- what it was worth ------------------------------------------------------
+#
+# 93% of this ledger's tokens carried no rate at all, because the backfill
+# dropped a model the transcript was stating plainly. Recovering it is not
+# repricing: "never repriced" forbids valuing a past saving at *today's*
+# model, and each event here is priced from the model that was in the chair
+# when it happened, read out of the same file that proves the saving.
+
+
+def test_an_event_is_priced_from_the_model_that_was_in_the_chair(
+ repo: Path, projects: Path
+) -> None:
+ _write(
+ projects,
+ repo,
+ _pair(_distilled("aaaabbbbcccc", 500), cwd=str(repo), model="claude-opus-5"),
+ )
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["model"] == "claude-opus-5"
+ assert event["input_rate_usd_per_million"] == 5.0
+ assert event["output_rate_usd_per_million"] == 25.0
+ # Provenance names the method, so this population stays separable from a
+ # live event priced off the session-model cache.
+ assert event["pricing_source"] == "transcript_model:claude_code"
+ assert event["pricing_version"]
+
+
+def test_a_model_the_rate_table_does_not_know_leaves_the_event_unpriced(
+ repo: Path, projects: Path
+) -> None:
+ """A guessed rate is worse than the silence it replaced. The lenient
+ lookup would have answered $3/$15 here and stamped it as measured."""
+ _write(
+ projects,
+ repo,
+ _pair(_distilled("ddddeeeeffff", 500), cwd=str(repo), model="totally-made-up-xyz"),
+ )
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["model"] is None
+ assert event["input_rate_usd_per_million"] is None
+ assert event["pricing_source"] is None
+ # The saving itself is unaffected: unpriced, never unrecorded.
+ assert event["baseline_input_tokens"] > event["delivered_input_tokens"]
+
+
+def test_a_model_stated_only_on_a_line_carrying_no_tool_call_still_reaches_the_event(
+ repo: Path, projects: Path
+) -> None:
+ """The gate's widening, pinned by the case that motivated it.
+
+ Codex states its model on a ``turn_context`` line with no tool call on it,
+ so the tool-call prefilter consumed it and *every* Codex event was written
+ unpriced -- measured at 0 of 1,219 candidates against 25 of 27 for Claude
+ Code, which happens to state its model on the same line as the tool call.
+ Codex is 93% of this machine's ledger, so the gate was the whole fix.
+
+ Written in the Claude Code shape because that is what this module's
+ fixtures speak; what it pins is the gate, which is harness-agnostic.
+ Remove ``'"model"' in raw_line`` from ``_gate`` and this fails.
+ """
+ entries = _pair(_distilled("111122223333", 500), cwd=str(repo))
+ standalone = {
+ "type": "assistant",
+ "cwd": str(repo),
+ "timestamp": _iso(NOW - 10),
+ "message": {"role": "assistant", "model": "claude-opus-5", "content": []},
+ }
+ _write(projects, repo, [standalone, *entries])
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["model"] == "claude-opus-5"
+
+
+def test_a_models_reach_stops_at_the_transcript_it_was_stated_in(
+ repo: Path, projects: Path
+) -> None:
+ """Carrying the model forward must not carry it across sessions. A second
+ transcript that names no model is unpriced, not priced at the first's."""
+ _write(
+ projects,
+ repo,
+ _pair(_distilled("444455556666", 500), cwd=str(repo), model="claude-opus-5"),
+ name="s1",
+ )
+ _write(
+ projects,
+ repo,
+ _pair(_distilled("777788889999", 500), cwd=str(repo), block_id="toolu_02"),
+ name="s2",
+ )
+
+ _sync(repo, projects)
+
+ events = _events(repo)
+ assert len(events) == 2
+ assert sorted(row["model"] or "" for row in events) == ["", "claude-opus-5"]
+
+
+# -- the baseline is a ceiling, not a guess ---------------------------------
+
+
+def test_no_event_can_deliver_more_than_its_own_baseline(
+ repo: Path, projects: Path
+) -> None:
+ """The invariant a measured pair cannot violate, and nothing asserted it.
+
+ ``baseline`` is what the output cost before distillation and ``delivered``
+ is what the model read back, so ``delivered <= baseline`` holds by
+ construction -- unless the cap applied to the baseline is not the cap the
+ host actually applied. On this machine 305 stored events carry a baseline
+ of exactly 7,500 tokens and 125 of them deliver more than that, which is
+ proof by contradiction that the cap did not apply to them.
+
+ Written against a delivered size larger than the Claude Code cap, which
+ is exactly the shape that was being mis-accounted.
+ """
+ big = "x" * (HOST_OUTPUT_CAP_TOKENS * 4 * 3)
+ _write(
+ projects,
+ repo,
+ _pair(_distilled("abcdef012345", 500, kept=big), cwd=str(repo)),
+ )
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["delivered_input_tokens"] <= event["baseline_input_tokens"], (
+ f"delivered {event['delivered_input_tokens']} exceeds baseline "
+ f"{event['baseline_input_tokens']}: the cap charged to this event is "
+ "not the cap its host applied"
+ )
+
+
+def _accounting_for(harness: str, *, delivered: int, omitted: int) -> tuple[int, int]:
+ """``_accounting`` for one synthetic marker, at the unit level.
+
+ Direct rather than through ``_sync`` because the fixtures here speak only
+ the Claude Code transcript shape, and what is under test is precisely the
+ behaviour that must differ *between* harnesses.
+ """
+ from repowise.core.distill.markers import parse_markers
+ from repowise.core.savings.transcript import _accounting
+
+ text = render_marker("abcdef012345", 100, omitted)
+ (marker,) = parse_markers(text)
+ candidate = _Candidate(
+ marker=marker,
+ harness=harness,
+ occurred_at=datetime.fromtimestamp(NOW, UTC),
+ delivered_tokens=delivered,
+ session_id=None,
+ )
+ return _accounting(candidate)
+
+
+def test_claude_codes_cap_is_still_charged_to_claude_code(repo: Path) -> None:
+ """The confinement. Its 30,000 characters were measured exactly right:
+ across 117,148 shell results the largest it ever delivered is 30,000
+ characters to the byte, and none exceeds it."""
+ baseline, _ = _accounting_for("claude_code", delivered=100, omitted=5_000_000)
+
+ # The literal, not the constant: comparing the module's own value to
+ # itself passes whatever the value is, including a wrong one.
+ assert baseline == 7_500
+ assert HOST_OUTPUT_CAP_TOKENS == 7_500
+
+
+def test_claude_codes_cap_is_not_charged_to_codex(repo: Path) -> None:
+ """Codex delivers results two orders of magnitude larger and truncates
+ nowhere near 7,500 tokens. Charging it Claude Code's limit clipped 23% of
+ this ledger's events and made 125 of them deliver more than they cost."""
+ # Over Claude Code's 7,500 and under Codex's own, so nothing clips and
+ # the marker's arithmetic survives whole.
+ baseline, _ = _accounting_for("codex", delivered=100, omitted=9_000)
+
+ assert baseline > HOST_OUTPUT_CAP_TOKENS
+ assert baseline == 9_000 + 100 - estimate_tokens(
+ render_marker("abcdef012345", 100, 9_000)
+ )
+
+
+def test_codex_is_capped_at_its_own_measured_plateau(repo: Path) -> None:
+ """Pins the central claim of the per-agent cap, which nothing else did.
+
+ The sibling test above deliberately stays *under* Codex's cap, so it
+ passes whether Codex is capped at 10,000 or not capped at all. This one
+ clips: set the Codex entry to ``None`` and it fails.
+ """
+ baseline, _ = _accounting_for("codex", delivered=100, omitted=5_000_000)
+
+ # 2,552,250 characters: the largest result Codex was observed to deliver.
+ assert baseline == 638_062
+
+
+def test_a_harness_nobody_measured_is_not_charged_someone_elses_cap(repo: Path) -> None:
+ """Asserting a truncation we have not observed is how this bug happened,
+ so an unlisted harness gets no cap rather than the nearest one."""
+ baseline, _ = _accounting_for("some_future_agent", delivered=100, omitted=9_000_000)
+
+ # Larger than any cap in the table, so this pins "no cap" rather than
+ # "some other cap": giving the unlisted harness Codex's fails.
+ assert baseline > 8_000_000
+
+
+def test_a_cap_never_clips_below_what_the_host_actually_delivered(repo: Path) -> None:
+ """The invariant, made true by construction rather than by luck.
+
+ A host cannot have truncated below what it demonstrably handed the model.
+ Pinned on the *capped* harness, which is the only one where clipping can
+ happen at all.
+ """
+ delivered = HOST_OUTPUT_CAP_TOKENS * 3
+ baseline, reported = _accounting_for("claude_code", delivered=delivered, omitted=10)
+
+ assert reported == delivered
+ assert baseline >= delivered
+
+
+def _assistant(cwd: str, model: str, *, sidechain: bool = False, ts: float = NOW) -> dict:
+ """An assistant line that states a model and runs no tool."""
+ entry = {
+ "type": "assistant",
+ "cwd": cwd,
+ "timestamp": _iso(ts),
+ "message": {"role": "assistant", "model": model, "content": []},
+ }
+ if sidechain:
+ entry["isSidechain"] = True
+ return entry
+
+
+def test_a_sub_agents_model_is_not_charged_to_the_main_thread(
+ repo: Path, projects: Path
+) -> None:
+ """Claude Code interleaves Task sub-agent lines into the same transcript,
+ and a sub-agent can run a different model. Carrying one forward prices the
+ main thread's next command at the sub-agent's rate -- 5x out when a Haiku
+ sub-agent lands between an Opus tool call and its result."""
+ entries = [
+ _assistant(str(repo), "claude-opus-5", ts=NOW - 20),
+ _assistant(str(repo), "claude-haiku-4-5", sidechain=True, ts=NOW - 10),
+ *_pair(_distilled("aabbccddeeff", 500), cwd=str(repo)),
+ ]
+ _write(projects, repo, entries)
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["model"] == "claude-opus-5"
+ assert event["input_rate_usd_per_million"] == 5.0
+
+
+def test_a_sentinel_label_does_not_overwrite_a_known_model(
+ repo: Path, projects: Path
+) -> None:
+ """Claude Code writes ``<synthetic>`` for an API error or an interrupted
+ message. It resolves to no rate, which is right -- but if it is allowed to
+ overwrite the carried model it leaves every later marker in the file
+ unpriced, which is a silent loss rather than a refusal."""
+ entries = [
+ _assistant(str(repo), "claude-opus-5", ts=NOW - 20),
+ _assistant(str(repo), "<synthetic>", ts=NOW - 10),
+ *_pair(_distilled("ffeeddccbbaa", 500), cwd=str(repo)),
+ ]
+ _write(projects, repo, entries)
+
+ _sync(repo, projects)
+
+ (event,) = _events(repo)
+ assert event["model"] == "claude-opus-5"