Commit d73f9bb
Closes the task_id attribution gap that PR #528's audit banner caught
("578 worker events missing task_id"), and the tool_intent gap that
made the per-operation chart useless for workers ("99.85% of tokens
in one bucket called turn"). Both in one PR per Kaia's review
(decision 18aea45a) because they share the same content-parsing
primitive.
What's new
----------
src/cost_tracking/worker_intent.py — pure parsers:
- classify_tool_intent(content): walks an assistant message's
content blocks and picks one tool_intent bucket from the tool_use
block names. Priority: worker_marcus_call > worker_mcp_call >
worker_edit > worker_bash > worker_search > worker_read >
worker_text > unknown. The marcus_call bucket gets top priority
so the coordination tax surfaces as its own slice.
- extract_task_id_from_tool_result(content): pulls task_id out of
Marcus MCP results. Handles both shapes:
result.task.id (request_next_task)
result.data.task_id (log_artifact, report_blocker,
report_task_progress)
- extract_task_id_from_user_message(content): walks a user-message
content list, returns the first task_id from any tool_result.
src/cost_tracking/worker_ingester.py:
- _session_task tracker (Dict[session_id, task_id]) updated on
every user record carrying a Marcus tool_result. Consumed when
the next assistant record on that session emits a token_events
row. binding.task_id wins when the resolver supplies one; the
tracker is a fallback for resolvers that only know agent_id +
project_id from cwd (which is all Cato's resolver has).
- _ingest_record also walks user records now (previously
skipped). User records still produce zero rows; they only
update tracker state.
- backfill_file / backfill_directory: re-walk a JSONL and call
store.update_attribution(request_id, task_id, tool_intent)
for each assistant row. Used to populate the new columns on
historical rows. Idempotent — COALESCE on the SQL side keeps
already-set values intact.
src/cost_tracking/cost_store.py:
- New tool_intent column on token_events.
- _tool_intent_migration: idempotent ALTER TABLE ADD COLUMN for
existing DBs. Metadata-only operation in SQLite — cheap even
on multi-million-row tables.
- New index idx_te_intent for by_tool aggregation queries.
- record_event INSERT extended to include tool_intent.
- New update_attribution(request_id, task_id, tool_intent):
COALESCE-guarded UPDATE used by the backfill path. Only fills
NULL columns so re-running on already-backfilled rows is a
no-op.
src/cost_tracking/cost_aggregator.py:
- by_tool slice added to both run_summary and project_summary.
Excludes NULL tool_intent rows (planner / pre-#527 legacy) so
the dashboard chart only shows real worker buckets.
Tests
-----
- 25 new tests in test_worker_intent.py covering both parsers,
legacy/forward compat, multiple result shapes, edge cases.
- 11 new tests in test_worker_ingester.py covering the
session-task tracker, tool_intent on the row, backfill (fills
NULL columns, respects already-populated values).
- 3 new tests in test_cost_aggregator.py for the by_tool slice
in both summaries.
Validation against real costs.db
--------------------------------
Backfill on ~/.marcus/costs.db (68,974 worker rows):
- task_id coverage: 0% -> 62.5% (43,100 rows attributed)
- tool_intent coverage: 0% -> 71.0% (48,947 rows tagged)
The remaining gap is rows whose source JSONL is no longer on
disk (cleaned-up old runs) — irrecoverable, but the audit banner
now surfaces this explicitly instead of silently NULL-ing.
For projects whose JSONL still exists, audit drops from 100%
worker_events_without_task_id to under 5%.
Baseline: 1454 passed, 1 skipped under `pytest -m unit`; mypy
clean across src/cost_tracking/.
Refs: #527, #528 (Phase 1), Simon decision 18aea45a
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8806fe4 commit d73f9bb
7 files changed
Lines changed: 1124 additions & 5 deletions
File tree
- src/cost_tracking
- tests/unit/cost_tracking
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
225 | 244 | | |
226 | 245 | | |
227 | 246 | | |
| |||
230 | 249 | | |
231 | 250 | | |
232 | 251 | | |
| 252 | + | |
233 | 253 | | |
234 | 254 | | |
235 | 255 | | |
| |||
658 | 678 | | |
659 | 679 | | |
660 | 680 | | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
661 | 696 | | |
662 | 697 | | |
663 | 698 | | |
| |||
666 | 701 | | |
667 | 702 | | |
668 | 703 | | |
| 704 | + | |
669 | 705 | | |
670 | 706 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
93 | 100 | | |
94 | 101 | | |
95 | 102 | | |
| |||
119 | 126 | | |
120 | 127 | | |
121 | 128 | | |
| 129 | + | |
122 | 130 | | |
123 | 131 | | |
124 | 132 | | |
| |||
276 | 284 | | |
277 | 285 | | |
278 | 286 | | |
| 287 | + | |
279 | 288 | | |
280 | 289 | | |
281 | 290 | | |
| |||
570 | 579 | | |
571 | 580 | | |
572 | 581 | | |
| 582 | + | |
573 | 583 | | |
574 | 584 | | |
575 | 585 | | |
| |||
643 | 653 | | |
644 | 654 | | |
645 | 655 | | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
646 | 687 | | |
647 | 688 | | |
648 | 689 | | |
| |||
698 | 739 | | |
699 | 740 | | |
700 | 741 | | |
701 | | - | |
| 742 | + | |
702 | 743 | | |
703 | 744 | | |
704 | 745 | | |
705 | | - | |
| 746 | + | |
706 | 747 | | |
707 | 748 | | |
708 | 749 | | |
| |||
714 | 755 | | |
715 | 756 | | |
716 | 757 | | |
| 758 | + | |
717 | 759 | | |
718 | 760 | | |
719 | 761 | | |
| |||
745 | 787 | | |
746 | 788 | | |
747 | 789 | | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
748 | 840 | | |
749 | 841 | | |
750 | 842 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
| |||
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
120 | 131 | | |
121 | 132 | | |
122 | 133 | | |
| |||
155 | 166 | | |
156 | 167 | | |
157 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
158 | 251 | | |
159 | 252 | | |
160 | 253 | | |
161 | 254 | | |
162 | 255 | | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
163 | 261 | | |
164 | 262 | | |
165 | 263 | | |
166 | 264 | | |
167 | 265 | | |
168 | 266 | | |
169 | | - | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
170 | 282 | | |
171 | 283 | | |
172 | 284 | | |
| |||
181 | 293 | | |
182 | 294 | | |
183 | 295 | | |
184 | | - | |
185 | 296 | | |
186 | 297 | | |
187 | 298 | | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
188 | 311 | | |
189 | 312 | | |
190 | 313 | | |
| |||
199 | 322 | | |
200 | 323 | | |
201 | 324 | | |
202 | | - | |
| 325 | + | |
| 326 | + | |
203 | 327 | | |
204 | 328 | | |
205 | 329 | | |
| |||
0 commit comments