Skip to content

docs(hook_streaming): correct why _sum_cost_usd is not foundation's sum_cost_usd - #156

Merged
Salil Das (sadlilas) merged 1 commit into
mainfrom
docs/sum-cost-usd-divergence-rationale
Aug 26, 2026
Merged

docs(hook_streaming): correct why _sum_cost_usd is not foundation's sum_cost_usd#156
Salil Das (sadlilas) merged 1 commit into
mainfrom
docs/sum-cost-usd-divergence-rationale

Conversation

@sadlilas

@sadlilas Salil Das (sadlilas) commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

_sum_cost_usd in hook_streaming.py carried this rationale:

Replicated inline (not imported) from amplifier_foundation.bundle._prepared.sum_cost_usd to keep this hook free of foundation coupling.

That is false, and it invites a refactor that would introduce a real bug. Docstring only — no code change.

Coupling is not the reason

This package declares amplifier-foundation as a dependency in pyproject.toml and imports it in eleven places across src/, including elsewhere in this same bundle package. There is no boundary here to protect.

The actual reason: two differences a de-duplication pass would erase

  1. It returns a str, not a Decimal. sessionCostTotal is a decimal string on the wire (docs/spec/wire-protocol.md).
  2. It skips non-finite values. Decimal("NaN") and Decimal("Infinity") are valid Decimals that do not raise InvalidOperation, and x + NaN is NaN. Foundation's helper has no such guard. That reasoning is already recorded in the comment on the is_finite check itself, so the docstring points there rather than restating it.

Neither regression would fail a type check: _emit takes dict[str, Any], so a Decimal would reach the wire unchecked, and there are no unit tests over this function.

Verification

ruff check src/          All checks passed
ruff format --check      93 files already formatted

Zero executable lines changed.

🤖 Generated with Amplifier

…um_cost_usd

The docstring described this function as "replicated inline (not imported) ...
to keep this hook free of foundation coupling." That is false: this package
declares amplifier-foundation as a dependency and imports it in eleven places
across src/, including elsewhere in this same bundle package. There is no
boundary here to protect.

The real reason is that this function differs from foundation's in two ways
that a "de-duplicate this into the import" pass would silently regress: it
returns a str (sessionCostTotal is a decimal string on the wire) and it skips
non-finite values (foundation's has no such guard). Neither regression would
fail a type check, because _emit takes dict[str, Any].

The docstring now says that, and points at the existing is_finite comment
rather than restating it.

Docstring only. No code change.

Generated with [Amplifier](https://github.com/microsoft/amplifier)
Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
@sadlilas
Salil Das (sadlilas) force-pushed the docs/sum-cost-usd-divergence-rationale branch from 4961d8b to 73acf3a Compare August 26, 2026 20:27
@sadlilas Salil Das (sadlilas) changed the title docs(hook_streaming): say why _sum_cost_usd is not foundation's sum_cost_usd docs(hook_streaming): correct why _sum_cost_usd is not foundation's sum_cost_usd Aug 26, 2026
@sadlilas
Salil Das (sadlilas) merged commit 99eea7a into main Aug 26, 2026
4 checks passed
@sadlilas
Salil Das (sadlilas) deleted the docs/sum-cost-usd-divergence-rationale branch August 26, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant