Repository navigation
Fix #780: state the unattributed-spend caveat on every surface (option 3) - #789
Merged
Merged
Conversation
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
Contributor
[Low impact] The PR appears safe to merge; it adds the disclosure without changing spend calculations.
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 bysession_id, and these rows have none. We could scope it bytrace_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 incore/db.pynow points at this decision ("option 3, decided for 0.7.1; see #780").Summary
UNATTRIBUTED_SPEND_CAVEATintokenjam/core/db.py, next to the query. Core does not import cli or api.get_unattributed_spend_summaryreturns it ascaveaton the bucket object. This follows the existing"caveat": strpattern 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 carriescaveat: ""too.tj cost: printed under the existing Note line (tokenjam/cli/cmd_cost.py). The--jsonoutput 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/costandGET /api/v1/status:unattributed_spend.caveat. The routes needed no change because the field rides on the dict.unattributedSpend.caveatfrom 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.get_unattributed_spendandunattributed_spend. It matches. The MCP server does not render the bucket. The tenant-analyticsunattributed_cost_usdon/costis 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:IngestPipeline. Two session markers on one trace send the call through attribution step 3, which leaves it sessionless.ingest_langfuseover an export file. That adapter writes sessionless spans taggedingest.langfuse.accounting.call_fingerprint/ingest_sourceto 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 costandtj statusshow 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:/costand/statuscarryunattributed_spend.caveat. With an empty bucket,/costhascaveat == ""and/statushas 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 readsunattributedSpend.caveat, and the inverse is also pinned: the sentence is not hard-coded anywhere inindex.html.test_lens_module_parsespasses.tests/integration/test_storage_backend_parity.py: the exact-dict assertion now includescaveat(Critical Rule 23: this test would otherwise have asserted the old shape). The DB-vs-shim projection now comparescaveat, so daemon-modetj cost --jsoncannot drop it.env -u FORCE_COLOR pytest tests/unit tests/integration tests/synthetic -n auto: 6046 passed, 17 skipped, 5 failed. The 5 failures aretest_summarize_relocate,test_summarize_cli×2 andtest_xdist_isolation×2, none of which touch this diff. All 45 pass when run on their own, so they look like xdist flakes.test_lens_module_parses,test_no_tracker_ids_in_printed_stringsandtest_advertised_commands_are_invocable: 392 passed.ruff check tokenjam/andmypy tokenjam/are clean.What's NOT in this PR
caveatkey on the doctor check dict. Doctor checks share aname/level/messageshape, so the caveat lives insidemessagerather than widening that shape for one check.🤖 Generated with Claude Code
https://claude.ai/code/session_011uyjtSF8w9YGsDDujqydev