Skip to content

refactor(notifications): Plumb token + cost usage into context#1237

Merged
srtab merged 1 commit into
mainfrom
refactor/notification-token-context
May 20, 2026
Merged

refactor(notifications): Plumb token + cost usage into context#1237
srtab merged 1 commit into
mainfrom
refactor/notification-token-context

Conversation

@srtab
Copy link
Copy Markdown
Owner

@srtab srtab commented May 20, 2026

Summary

  • _render_payload adds input_tokens, output_tokens, total_tokens, cost_usd (read directly from Activity) to the Notification.context dict.
  • _render_batch_payload does the same, but the four values are summed across siblings via Sum(...) in the existing aggregate query.
  • The batch sibling query now also pulls status, and the renderer builds a repo_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 context keeps the channel renderer agnostic of Activity.

Notes

  • Purely additive. No existing context key changed, no consumer reads the new keys yet (bell, email templates, in-app list all unaffected). All 146 pre-existing notification tests still pass.
  • cost_usd is converted to float before going into context because Notification.context is a JSONField and Decimal doesn't JSON-encode cleanly across DB backends.
  • _batch_duration was updated to destructure the new 4-tuple rows shape.

Test plan

  • 4 new tests in test_signals.py: single-job tokens populated, single-job tokens None when activity has none, batch tokens summed + repo_results shape, batch totals None when no siblings have usage.
  • CI: full make test green
  • CI: lint + ty pass

…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.
@srtab srtab merged commit 2c3ea88 into main May 20, 2026
6 checks passed
@srtab srtab deleted the refactor/notification-token-context branch May 20, 2026 12:58
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