Skip to content

fix(tracking): bind tariffs to source rate records#201

Closed
kfallah wants to merge 3 commits into
codex/provider-tariff-catalog-stacked-v2from
codex/provider-tariff-bindings-stacked-v2
Closed

fix(tracking): bind tariffs to source rate records#201
kfallah wants to merge 3 commits into
codex/provider-tariff-catalog-stacked-v2from
codex/provider-tariff-bindings-stacked-v2

Conversation

@kfallah

@kfallah kfallah commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Disposition

This branch is superseded and is not intended for merge. Exact head preserved: 51fac14. No branch was deleted.

@greptile-apps

greptile-apps Bot commented Jul 20, 2026

Copy link
Copy Markdown

Greptile Summary

This PR binds each priced usage dimension to an exact, retained source rate record by introducing ProviderTariffBillingMeter and wiring per-record source evidence (locator, digest, rate IDs, effective date) through the catalog, provenance, and route models.

  • Adds ProviderTariffBillingMeter to budget.py, bumps ProviderTariffRoute to schema v2 (requiring an ordered (input_tokens, output_tokens) meter pair), and bumps ProviderTariffProvenance to schema v3 to reflect the structural change.
  • Replaces the single global _normalized_bedrock_source_snapshot_digest() / _AWS_BEDROCK_SOURCE_SNAPSHOT_DIGEST aggregate hash with per-record source coordinates and independently-pinned per-record content digests in _BEDROCK_CATALOG_RECORD_DIGESTS, verified at import time.
  • Extends tests to assert rate IDs and billing-meter ordering in catalog snapshots, and adds parametrized validation tests for invalid meter configurations.

Confidence Score: 5/5

Safe to stack — the per-record integrity check runs at import time, the billing-meter ordering is enforced by both the static type and the model validator, and the tests cover drift, wrong count, and wrong order.

Every changed path is guarded: catalog records are content-hashed and verified at import, the new billing_meters field is statically typed as an exact 2-tuple and dynamically validated for (input, output) ordering, and the test suite asserts the full set of new provenance fields for each catalog entry. No gaps in the fail-closed logic were found.

No files require special attention.

Important Files Changed

Filename Overview
wmh/tracking/budget.py Introduces ProviderTariffBillingMeter (schema v1) with usage_dimension + rate_id; bumps ProviderTariffRoute to schema v2 with mandatory billing_meters exact-2-tuple; bumps ProviderTariffProvenance to schema v3. Ordering is enforced by model validator; type annotation enforces 2-element constraint statically.
wmh/tracking/tariffs.py Replaces single aggregate catalog digest with per-record source coordinates (locator, digest, rate IDs, effective_on) in _BedrockCatalogRecord and independently pinned per-record content digests in _BEDROCK_CATALOG_RECORD_DIGESTS; integrity guard called at import time. No logic errors found.
wmh/tracking/tariffs_test.py Adds integrity-guard tests for price-drift and record-set-drift, extends parametrized catalog tests to assert per-record source locator / digest / rate IDs / billing SKU, and adds billing-meter ordering validation tests covering wrong count and wrong order.
wmh/tracking/_testing.py Adds billing_meters to synthetic_tariff_provenance so callers that use ProviderTariffRoute now supply the mandatory field; no issues.
wmh/tracking/init.py Exports ProviderTariffBillingMeter from the tracking public API; change is additive and consistent with existing export pattern.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Import as Module Import
    participant VCI as _verify_bedrock_catalog_integrity
    participant Digest as _bedrock_catalog_record_digest
    participant Records as _BEDROCK_CATALOG_RECORDS
    participant Pins as _BEDROCK_CATALOG_RECORD_DIGESTS

    Import->>VCI: _verify_bedrock_catalog_integrity(_BEDROCK_CATALOG_RECORDS)
    VCI->>Records: iterate record.model keys
    VCI->>Pins: compare ordered keys
    alt set / order mismatch
        VCI-->>Import: RuntimeError("record set changed")
    end
    loop each record
        VCI->>Digest: _bedrock_catalog_record_digest(record)
        Digest->>Digest: "json.dumps(record.model_dump(mode="json"), sort_keys=True)"
        Digest->>Digest: sha256(encoded)
        Digest-->>VCI: "sha256:..."
        VCI->>Pins: lookup _BEDROCK_CATALOG_RECORD_DIGESTS[record.model]
        alt digest mismatch
            VCI-->>Import: "RuntimeError("record changed: {model}")"
        end
    end
    VCI-->>Import: OK

    Note over Import: Build _CATALOG from _BEDROCK_CATALOG_RECORDS

    participant Factory as ProviderTokenTariff.from_usd_per_million
    participant Route as ProviderTariffRoute
    participant Meter as ProviderTariffBillingMeter

    Import->>Factory: "_bedrock_tariff(record=record)"
    Factory->>Meter: "ProviderTariffBillingMeter(usage_dimension="input_tokens", rate_id=input_rate_id)"
    Factory->>Meter: "ProviderTariffBillingMeter(usage_dimension="output_tokens", rate_id=output_rate_id)"
    Factory->>Route: "ProviderTariffRoute(..., billing_meters=(input_meter, output_meter))"
    Route->>Route: validate ordering: input_tokens then output_tokens
    Route-->>Factory: frozen route
    Factory-->>Import: ProviderTokenTariff (immutable)
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Import as Module Import
    participant VCI as _verify_bedrock_catalog_integrity
    participant Digest as _bedrock_catalog_record_digest
    participant Records as _BEDROCK_CATALOG_RECORDS
    participant Pins as _BEDROCK_CATALOG_RECORD_DIGESTS

    Import->>VCI: _verify_bedrock_catalog_integrity(_BEDROCK_CATALOG_RECORDS)
    VCI->>Records: iterate record.model keys
    VCI->>Pins: compare ordered keys
    alt set / order mismatch
        VCI-->>Import: RuntimeError("record set changed")
    end
    loop each record
        VCI->>Digest: _bedrock_catalog_record_digest(record)
        Digest->>Digest: "json.dumps(record.model_dump(mode="json"), sort_keys=True)"
        Digest->>Digest: sha256(encoded)
        Digest-->>VCI: "sha256:..."
        VCI->>Pins: lookup _BEDROCK_CATALOG_RECORD_DIGESTS[record.model]
        alt digest mismatch
            VCI-->>Import: "RuntimeError("record changed: {model}")"
        end
    end
    VCI-->>Import: OK

    Note over Import: Build _CATALOG from _BEDROCK_CATALOG_RECORDS

    participant Factory as ProviderTokenTariff.from_usd_per_million
    participant Route as ProviderTariffRoute
    participant Meter as ProviderTariffBillingMeter

    Import->>Factory: "_bedrock_tariff(record=record)"
    Factory->>Meter: "ProviderTariffBillingMeter(usage_dimension="input_tokens", rate_id=input_rate_id)"
    Factory->>Meter: "ProviderTariffBillingMeter(usage_dimension="output_tokens", rate_id=output_rate_id)"
    Factory->>Route: "ProviderTariffRoute(..., billing_meters=(input_meter, output_meter))"
    Route->>Route: validate ordering: input_tokens then output_tokens
    Route-->>Factory: frozen route
    Factory-->>Import: ProviderTokenTariff (immutable)
Loading

Reviews (5): Last reviewed commit: "refactor(tracking): type exact billing m..." | Re-trigger Greptile

Comment thread wmh/tracking/budget.py Outdated
@kfallah
kfallah force-pushed the codex/provider-tariff-catalog-stacked-v2 branch from be92998 to 7b67b0f Compare July 20, 2026 02:14
@kfallah
kfallah force-pushed the codex/provider-tariff-bindings-stacked-v2 branch from fad6441 to 48fcaaf Compare July 20, 2026 02:14
@kfallah
kfallah force-pushed the codex/provider-tariff-catalog-stacked-v2 branch from 7b67b0f to 5a0bf22 Compare July 20, 2026 02:35
@kfallah
kfallah force-pushed the codex/provider-tariff-bindings-stacked-v2 branch 2 times, most recently from 2ce6d9a to 7e593e3 Compare July 20, 2026 02:55
@kfallah
kfallah force-pushed the codex/provider-tariff-catalog-stacked-v2 branch from 5a0bf22 to 02e95cf Compare July 20, 2026 02:55
@kfallah
kfallah force-pushed the codex/provider-tariff-catalog-stacked-v2 branch from 02e95cf to ee8e443 Compare July 20, 2026 03:29
@kfallah
kfallah force-pushed the codex/provider-tariff-bindings-stacked-v2 branch from 7e593e3 to 51fac14 Compare July 20, 2026 03:29
@kfallah

kfallah commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Closing because this branch is superseded and is not intended for merge. Exact head preserved: 51fac14. No branch was deleted.

@kfallah kfallah closed this Jul 20, 2026
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