Skip to content

Fix #780: state the unattributed-spend caveat on every surface (option 3) - #789

Merged
anilmurty merged 2 commits into
mainfrom
fix/780-unattributed-bucket-caveat
Nov 15, 2026
Merged

anilmurty merged 2 commits into
mainfrom
fix/780-unattributed-bucket-caveat

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

The unattributed spend bucket can count one call twice, and until now only a code comment said so. This PR records the decision on #780 and shows a one-line caveat on every surface that renders the bucket.

Closes #780

Decision

Option 3: keep the raw sum and disclose it. The bucket stays SUM(cost_usd) over sessionless rows. The duplicate-observation winner rule is scoped by session_id, and these rows have none. We could scope it by trace_id (option 1) or apply the fingerprint with no grouping key (option 2), but either one would be a dedup path nobody has exercised, and getting it wrong under-reports spend. Under-reporting is the silent-loss failure #749 exists to prevent, and it is worse than the over-report it would replace. Real sessionless dedup is deferred. The query comment in core/db.py now points at this decision ("option 3, decided for 0.7.1; see #780").

Summary

  • New shared constant UNATTRIBUTED_SPEND_CAVEAT in tokenjam/core/db.py, next to the query. Core does not import cli or api.

    This figure may count the same call twice when it arrived both live and via a later backfill, so it may overstate unattributed spend.

  • get_unattributed_spend_summary returns it as caveat on the bucket object. This follows the existing "caveat": str pattern on cost payloads. The field is the empty string when the bucket is empty, so users with no sessionless spend see nothing new. ApiBackend's fallback dict carries caveat: "" too.
  • Surfaces:
    • tj cost: printed under the existing Note line (tokenjam/cli/cmd_cost.py). The --json output gets the field through the bucket dict.
    • tj status: same treatment (tokenjam/cli/cmd_status.py).
    • tj doctor: added to the "Unattributed spend" check message (tokenjam/cli/cmd_doctor.py).
    • GET /api/v1/cost and GET /api/v1/status: unattributed_spend.caveat. The routes needed no change because the field rides on the dict.
    • Lens Sessions page: renders unattributedSpend.caveat from the API, appended to the existing "grand total can exceed…" line (tokenjam/ui/index.html). No second copy of the string, no new spacing literal.
  • I checked the issue's surface list against main by grepping get_unattributed_spend and unattributed_spend. It matches. The MCP server does not render the bucket. The tenant-analytics unattributed_cost_usd on /cost is a different figure (spend with no tenant set), so it is left alone.

Tests / Verification

  • tests/integration/test_full_pipeline.py::test_session_attribution_invariant_on_a_mixed_source_corpus (new). This extends the sum invariant to a mixed-source corpus, with both observations built through their real ingest paths:
    • The live observation goes through IngestPipeline. Two session markers on one trace send the call through attribution step 3, which leaves it sessionless.
    • The backfill observation goes through ingest_langfuse over an export file. That adapter writes sessionless spans tagged ingest.langfuse.
    • The test then uses the product's own accounting.call_fingerprint / ingest_source to prove the two rows are one call from two observers. It asserts that the bucket counts the call twice (span_count == 2, cost == 2×), that it carries the caveat, and that the raw identity still holds: raw span sum == materialised session totals + raw bucket. The docstring states that this identity compares raw with raw and does not mean either side is deduplicated.
  • tests/integration/test_cli.py: four new tests. tj cost and tj status show the caveat with a non-zero bucket and in --json, and leave it out when there is no sessionless spend. Click runner output is whitespace-flattened to tolerate Rich wrapping.
  • tests/integration/test_cost_attribution.py: /cost and /status carry unattributed_spend.caveat. With an empty bucket, /cost has caveat == "" and /status has no bucket.
  • tests/unit/test_doctor_unattributed_spend.py: the caveat appears in the doctor message when the bucket is non-zero and is absent when empty.
  • tests/unit/test_lens_unattributed_spend.py: the Sessions view reads unattributedSpend.caveat, and the inverse is also pinned: the sentence is not hard-coded anywhere in index.html. test_lens_module_parses passes.
  • tests/integration/test_storage_backend_parity.py: the exact-dict assertion now includes caveat (Critical Rule 23: this test would otherwise have asserted the old shape). The DB-vs-shim projection now compares caveat, so daemon-mode tj cost --json cannot drop it.
  • Results:
    • env -u FORCE_COLOR pytest tests/unit tests/integration tests/synthetic -n auto: 6046 passed, 17 skipped, 5 failed. The 5 failures are test_summarize_relocate, test_summarize_cli ×2 and test_xdist_isolation ×2, none of which touch this diff. All 45 pass when run on their own, so they look like xdist flakes.
    • Targeted run, including test_lens_module_parses, test_no_tracker_ids_in_printed_strings and test_advertised_commands_are_invocable: 392 passed.
    • ruff check tokenjam/ and mypy tokenjam/ are clean.

What's NOT in this PR

  • No sessionless dedup path (options 1 and 2). Deferred by decision; see above.
  • No change to the bucket figure itself. The number is unchanged. Only the disclosure is new.
  • No caveat key on the doctor check dict. Doctor checks share a name/level/message shape, so the caveat lives inside message rather than widening that shape for one check.

🤖 Generated with Claude Code

https://claude.ai/code/session_011uyjtSF8w9YGsDDujqydev

anilmurty and others added 2 commits November 15, 2026 11:30
The unattributed (sessionless) spend bucket is a raw SUM, because the
duplicate-observation winner rule is scoped by session_id and these rows
have none. One call seen both live and via a later backfill is therefore
counted twice in the bucket. The query comment said so, but no surface
rendering the figure did, so a reader had no way to know the bucket
itself may overstate.

The maintainer chose option 3 for 0.7.1: keep the raw sum and disclose
it, rather than ship a dedup path nobody has exercised. A wrong dedup
under-reports, which is the silent-loss failure #749 exists to prevent.

- One shared constant, UNATTRIBUTED_SPEND_CAVEAT, in core/db.py, carried
  as `caveat` on the bucket dict (empty when the bucket is empty), so
  /api/v1/cost, /api/v1/status and both CLI --json payloads get it.
- tj cost, tj status and tj doctor print it next to the bucket; Lens
  renders the API-provided string rather than a second copy.
- The sum-invariant test now runs on a mixed-source corpus built through
  the real live pipeline and the real Langfuse backfill adapter, and
  pins the documented over-count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011uyjtSF8w9YGsDDujqydev
@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; it adds the disclosure without changing spend calculations.

Summary

Adds one shared warning that unattributed spend may count a call twice, without changing the amount.

  • Unattributed spend displays explain when duplicate calls can inflate the total.

Reviews (1) · Last reviewed commit: "Merge branch 'main' into fix/780-unattri..." · Reviewed by Greptile

@anilmurty
anilmurty merged commit 1e88907 into main Nov 15, 2026
6 checks passed
@anilmurty
anilmurty deleted the fix/780-unattributed-bucket-caveat branch November 15, 2026 19:47
@anilmurty anilmurty mentioned this pull request Nov 15, 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.

Unattributed spend bucket sums raw sessionless rows without the duplicate-observation winner rule

1 participant