refactor(notifications): Plumb token + cost usage into context#1237
Merged
Conversation
…ontext
Extends the context dicts produced by _render_payload (single job /
schedule) and _render_batch_payload (multi-job rollup) with token and
cost data sourced from Activity.input_tokens / output_tokens /
total_tokens / cost_usd. For batches the four values are aggregated via
SUM across siblings, and rows now also carry per-activity status so the
batch context can include a `repo_results` list (one {repo, ok} entry
per sibling) for renderers to show per-repo outcome.
Bell, email, and in-app behavior are unchanged: existing consumers
ignore the new keys, and tests covering the established context fields
still pass. Groundwork for the upcoming Rocket Chat renderer registry,
which will surface usage as channel fields.
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.
Summary
_render_payloadaddsinput_tokens,output_tokens,total_tokens,cost_usd(read directly fromActivity) to theNotification.contextdict._render_batch_payloaddoes the same, but the four values are summed across siblings viaSum(...)in the existing aggregate query.status, and the renderer builds arepo_results: list[{repo, ok}]so downstream consumers can show per-repo outcomes (✓/✗) without re-querying.Why
Groundwork for the upcoming Rocket Chat renderer registry. The RC channel will surface usage as channel attachment fields ("12.4k in · 38.1k out · $0.21") and the batch breakdown wants per-repo status. Putting these values in
contextkeeps the channel renderer agnostic ofActivity.Notes
cost_usdis converted tofloatbefore going intocontextbecauseNotification.contextis aJSONFieldandDecimaldoesn't JSON-encode cleanly across DB backends._batch_durationwas updated to destructure the new 4-tuplerowsshape.Test plan
test_signals.py: single-job tokens populated, single-job tokens None when activity has none, batch tokens summed +repo_resultsshape, batch totals None when no siblings have usage.make testgreen