Skip to content

feat(ai): stage-attributed spend ledger and by-stage reporting (#594) - #627

Merged
dan-petty merged 1 commit into
release/v0.2.23from
feat/594-part2-stage-attributed-spend-ledger
Sep 26, 2026
Merged

dan-petty merged 1 commit into
release/v0.2.23from
feat/594-part2-stage-attributed-spend-ledger

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Description

This PR implements Part 2 of #594:

  • Add nullable stage column in place to ai_spend_records SQLite ledger table with idx_spend_stage index (handled cleanly on schema initialization and migration).
  • Introduce current_stage ContextVar scope and stage_scope context manager in devops_cli.ai.spend.stage alongside resolve_spend_stage validator.
  • Wrap review.file_review and review.verification spans in devops_cli.ai.review.pipeline with stage_scope.
  • Add task_name to AIConfig set via for_task for valid AITasksConfig fields, falling back to unattributed when unset or unrecognized.
  • Pass stage through all 4 track_request_spend call sites (src/devops_cli/ai/client/unified.py and src/devops_cli/ai/direct.py).
  • Add --by stage to devops ai cost and devops ai cost report commands, rendering Stage, Calls, Tokens, Cost side by side in table and structured formats.
  • Update MCP tool ai_spend_report and resource://ai/spend to use --by instead of --group-by.
  • Decompose _find_project_config_path in settings.py into _match_dir_candidate helper to maintain McCabe complexity M <= 10.
  • Add comprehensive unit tests in tests/test_ai_spend_ledger.py verifying in-place migration, stage aggregation, stage scope, settings, and CLI rendering.

Validation

  • uv run pytest tests/test_ai_spend_ledger.py (10 passed)
  • uv run pytest tests/test_architectural_invariants.py (13 passed)
  • uv run devops scan complexity (passed on modified files)
  • uv run devops ci typecheck (passed: 0 issues in 471 source files)
  • uv run devops ci (100% passing across all quality gates)

- Add nullable stage column in place to ai_spend_records SQLite ledger with idx_spend_stage index
- Add current_stage ContextVar and stage_scope context manager in ai.spend.stage
- Wrap review.file_review and review.verification pipeline spans in stage_scope
- Add task_name to AIConfig set via for_task for valid AITasksConfig fields with fallback to unattributed
- Route stage through track_request_spend call sites in unified client and direct runner
- Add --by stage reporting to devops ai cost and cost report commands
- Update MCP tool ai_spend_report and resource://ai/spend to pass --by instead of --group-by
- Add migration in-place, stage aggregation, stage scope, and CLI tests to test_ai_spend_ledger
@dan-petty
dan-petty merged commit 59599be into release/v0.2.23 Sep 26, 2026
8 checks passed
@dan-petty
dan-petty deleted the feat/594-part2-stage-attributed-spend-ledger branch September 26, 2026 21:23
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