cctally 1.108.0 → 1.110.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +6 -6
  3. package/bin/_cctally_account.py +4 -0
  4. package/bin/_cctally_alerts.py +6 -2
  5. package/bin/_cctally_cache.py +1652 -166
  6. package/bin/_cctally_config.py +3 -2
  7. package/bin/_cctally_core.py +594 -33
  8. package/bin/_cctally_dashboard.py +1585 -201
  9. package/bin/_cctally_dashboard_conversation.py +494 -48
  10. package/bin/_cctally_dashboard_envelope.py +167 -11
  11. package/bin/_cctally_dashboard_share.py +154 -36
  12. package/bin/_cctally_dashboard_sources.py +2615 -422
  13. package/bin/_cctally_db.py +678 -5
  14. package/bin/_cctally_diagnosis_sources.py +238 -26
  15. package/bin/_cctally_doctor.py +99 -18
  16. package/bin/_cctally_five_hour.py +1295 -40
  17. package/bin/_cctally_forecast.py +124 -28
  18. package/bin/_cctally_journal.py +408 -38
  19. package/bin/_cctally_milestone_history.py +150 -14
  20. package/bin/_cctally_parser.py +5 -0
  21. package/bin/_cctally_percent_breakdown.py +6 -1
  22. package/bin/_cctally_project.py +48 -76
  23. package/bin/_cctally_quota.py +39 -7
  24. package/bin/_cctally_quota_model.py +17 -2
  25. package/bin/_cctally_record.py +1647 -443
  26. package/bin/_cctally_rederive.py +899 -30
  27. package/bin/_cctally_refresh.py +2 -1
  28. package/bin/_cctally_share.py +24 -5
  29. package/bin/_cctally_source_analytics.py +1035 -78
  30. package/bin/_cctally_statusline.py +83 -19
  31. package/bin/_cctally_store.py +61 -4
  32. package/bin/_cctally_transcript.py +27 -2
  33. package/bin/_cctally_tui.py +264 -76
  34. package/bin/_cctally_weekrefs.py +28 -5
  35. package/bin/_lib_blocks.py +127 -20
  36. package/bin/_lib_cache_report.py +20 -5
  37. package/bin/_lib_codex_conversation_query.py +297 -64
  38. package/bin/_lib_codex_hooks.py +102 -0
  39. package/bin/_lib_codex_metadata.py +207 -0
  40. package/bin/_lib_conversation.py +119 -0
  41. package/bin/_lib_conversation_dispatch.py +31 -1
  42. package/bin/_lib_conversation_query.py +315 -51
  43. package/bin/_lib_conversation_retention.py +413 -25
  44. package/bin/_lib_dashboard_sources.py +267 -7
  45. package/bin/_lib_diagnosis.py +10 -0
  46. package/bin/_lib_diff_kernel.py +12 -1
  47. package/bin/_lib_doctor.py +230 -8
  48. package/bin/_lib_ingest_frontier.py +1366 -49
  49. package/bin/_lib_journal.py +19 -2
  50. package/bin/_lib_merge_gate.py +307 -0
  51. package/bin/_lib_pricing.py +588 -29
  52. package/bin/_lib_record.py +130 -12
  53. package/bin/_lib_rederive.py +903 -2
  54. package/bin/_lib_render.py +9 -3
  55. package/bin/_lib_retained_size.py +27 -2
  56. package/bin/_lib_segment_summary.py +66 -8
  57. package/bin/_lib_share_templates.py +28 -18
  58. package/bin/_lib_snapshot_cache.py +115 -1
  59. package/bin/_lib_source_retry.py +320 -0
  60. package/bin/_lib_statusline_candidates.py +400 -36
  61. package/bin/_lib_tick_stats.py +119 -0
  62. package/bin/_lib_view_models.py +256 -76
  63. package/bin/cctally +69 -0
  64. package/dashboard/static/assets/ConversationsView-BB9vUiL6.js +72 -0
  65. package/dashboard/static/assets/{DoctorModal-CWn3U4Wl.js → DoctorModal-DFY2iPDf.js} +1 -1
  66. package/dashboard/static/assets/ModalRoot-DvRKHbQE.js +1 -0
  67. package/dashboard/static/assets/ProjectsDrillPanel-DAv39uSo.js +1 -0
  68. package/dashboard/static/assets/SourceDetailModal-DSAWo2Fp.js +1 -0
  69. package/dashboard/static/assets/{UpdateModal-D3m8GG6V.js → UpdateModal-R_9dOVnh.js} +3 -3
  70. package/dashboard/static/assets/dashboardStream.shared-worker-DbG6Ef0T.js +1 -0
  71. package/dashboard/static/assets/index-COhxe3vU.css +1 -0
  72. package/dashboard/static/assets/index-DdU62sov.js +13 -0
  73. package/dashboard/static/assets/outlineNavigation-BOCLcRa6.js +9 -0
  74. package/dashboard/static/assets/useKeymap-CW7Up0Ay.js +1 -0
  75. package/dashboard/static/dashboard.html +3 -3
  76. package/package.json +4 -1
  77. package/dashboard/static/assets/ConversationsView-BOSaBRtu.js +0 -72
  78. package/dashboard/static/assets/ModalRoot-SR070V6c.js +0 -1
  79. package/dashboard/static/assets/ProjectsDrillPanel-QLI9i5mZ.js +0 -1
  80. package/dashboard/static/assets/SourceDetailModal-pv0wlFxl.js +0 -1
  81. package/dashboard/static/assets/dashboardStream.shared-worker-1XTMV3nr.js +0 -1
  82. package/dashboard/static/assets/index-BHu4mxd8.css +0 -1
  83. package/dashboard/static/assets/index-CIWsbux3.js +0 -13
  84. package/dashboard/static/assets/outlineNavigation-CJvKqmLV.js +0 -9
  85. package/dashboard/static/assets/useKeymap-ffqsS5G0.js +0 -1
@@ -130,6 +130,48 @@ class ConversationSyncRecord:
130
130
  return {f.name: getattr(self, f.name) for f in dataclasses.fields(self)}
131
131
 
132
132
 
133
+ #: The maintenance phases recorded independently of the sync body (#780).
134
+ #: A long reclaim used to run INSIDE the measured conversation-sync body, so it
135
+ #: was published as `conversation_sync status="ok"` with a large duration and
136
+ #: nothing said which part of the pass had been slow.
137
+ #: The three real phases, plus the coercion sink. `other` is deliberately NOT
138
+ #: one of the three: an unrecognised caller name has to become something, and
139
+ #: making it `reclaim` publishes a reclaim record that no reclaim produced —
140
+ #: a fabricated maintenance history rather than a visible coercion.
141
+ MAINTENANCE_PHASES = ("delete", "reclaim", "checkpoint", "other")
142
+
143
+ #: The maintenance ring is deliberately SHORTER than the two tick rings. The
144
+ #: whole published state is capped at a frozen 65,536 bytes, and a third
145
+ #: full-length ring would spend that budget on a phase that runs about once a
146
+ #: day rather than about once a second. Sixteen records is several weeks of
147
+ #: maintenance history at the throttled cadence, and many continuation passes
148
+ #: at the escalated one.
149
+ MAINTENANCE_RING_CAPACITY = 16
150
+
151
+
152
+ @dataclasses.dataclass(frozen=True, slots=True)
153
+ class MaintenancePhaseRecord:
154
+ """One timed maintenance phase.
155
+
156
+ `phase` is normalized against `MAINTENANCE_PHASES` in the recorder, for the
157
+ same reason `ConversationSyncRecord.status` is: a frozen field typed `str`
158
+ would accept `str(exc)` or a filesystem path and publish it through the
159
+ debug endpoint.
160
+ """
161
+
162
+ seq: int
163
+ phase: str
164
+ started_ns: int
165
+ duration_ns: int
166
+ outcome: str
167
+ pages_reclaimed: int = 0
168
+ bytes_returned: int = 0
169
+ pending: bool = False
170
+
171
+ def as_wire(self) -> dict:
172
+ return {f.name: getattr(self, f.name) for f in dataclasses.fields(self)}
173
+
174
+
133
175
  @dataclasses.dataclass(frozen=True, slots=True)
134
176
  class StatsSnapshot:
135
177
  """An immutable whole-state read. Rebound as one object, never mutated."""
@@ -140,6 +182,7 @@ class StatsSnapshot:
140
182
  records: "tuple[TickRecord, ...]"
141
183
  standalone: "TickRecord | None"
142
184
  conversation_records: "tuple[ConversationSyncRecord, ...]" = ()
185
+ maintenance_records: "tuple[MaintenancePhaseRecord, ...]" = ()
143
186
 
144
187
 
145
188
  def _frozen_counts(kinds, source=None) -> "types.MappingProxyType[str, int]":
@@ -154,6 +197,7 @@ _EMPTY = StatsSnapshot(
154
197
  records=(),
155
198
  standalone=None,
156
199
  conversation_records=(),
200
+ maintenance_records=(),
157
201
  )
158
202
 
159
203
  _LOCK = threading.Lock()
@@ -449,6 +493,10 @@ class TickContext:
449
493
  # the whole suite stayed green. Regression:
450
494
  # `tests/test_tick_stats.py`.
451
495
  conversation_records=prior.conversation_records,
496
+ # #780's maintenance ring fell into exactly the hazard the
497
+ # comment above describes, on the run that added it: 128 ticks
498
+ # wiped it 128 times and the suite reported one record.
499
+ maintenance_records=prior.maintenance_records,
452
500
  )
453
501
 
454
502
 
@@ -516,6 +564,77 @@ def record_conversation_pass(
516
564
  )
517
565
 
518
566
 
567
+ def record_maintenance_phase(phase: str, payload: "dict | None" = None) -> None:
568
+ """Append one maintenance phase to its own bounded ring (#780).
569
+
570
+ Separate from the conversation-sync ring on purpose, so a two-second
571
+ reclaim and a slow checkpoint each appear as their own timed phase with
572
+ their own outcome instead of being visible only as one inflated
573
+ `conversation_sync` duration that reports `ok`.
574
+
575
+ Retention still runs INSIDE the measured sync body. §4c asked for it to be
576
+ moved out; the tranche deliberately did not move it, because the sync
577
+ thread is the only owner that holds the right locks in the right order,
578
+ and that deviation is recorded where the decision was taken. This ring is
579
+ what delivers §4c's actual requirement — that a long reclaim can no longer
580
+ hide inside `conversation_sync status="ok"` — and it does so by reporting
581
+ the phase separately, not by relocating it.
582
+
583
+ Every field is coerced here. The caller passes a phase record built from
584
+ SQLite pragma results, and this is the boundary that keeps an unexpected
585
+ value out of the published state.
586
+ """
587
+ global _STATE
588
+ data = payload or {}
589
+ safe_phase = phase if phase in MAINTENANCE_PHASES else "other"
590
+ duration_s = data.get("duration_s") or 0.0
591
+ try:
592
+ duration_ns = max(0, int(float(duration_s) * 1_000_000_000))
593
+ except (TypeError, ValueError):
594
+ duration_ns = 0
595
+ pending = bool(data.get("pending"))
596
+ if safe_phase == "reclaim":
597
+ pending = bool(data.get("deadline_hit")) or not bool(
598
+ data.get("made_progress", True))
599
+ outcome = "deadline" if data.get("deadline_hit") else (
600
+ "ok" if data.get("made_progress", True) else "no_progress")
601
+ elif safe_phase == "checkpoint":
602
+ result = data.get("result")
603
+ outcome = "ok" if (result and result[0] == 0) else "busy"
604
+ pending = not (result and result[0] == 0 and result[1] == 0)
605
+ else:
606
+ outcome = "ok"
607
+
608
+ def _as_int(value):
609
+ try:
610
+ return max(0, int(value or 0))
611
+ except (TypeError, ValueError):
612
+ return 0
613
+
614
+ record = MaintenancePhaseRecord(
615
+ seq=0,
616
+ phase=safe_phase,
617
+ started_ns=time.monotonic_ns() - duration_ns,
618
+ duration_ns=duration_ns,
619
+ outcome=outcome,
620
+ pages_reclaimed=_as_int(data.get("pages_reclaimed")),
621
+ bytes_returned=_as_int(data.get("bytes_returned")),
622
+ pending=pending,
623
+ )
624
+ with _LOCK:
625
+ prior = _STATE
626
+ retained = prior.maintenance_records
627
+ record = dataclasses.replace(
628
+ record,
629
+ seq=(retained[-1].seq + 1) if retained else 1,
630
+ )
631
+ _STATE = dataclasses.replace(
632
+ prior,
633
+ maintenance_records=(
634
+ retained + (record,))[-MAINTENANCE_RING_CAPACITY:],
635
+ )
636
+
637
+
519
638
  def begin_tick(
520
639
  *, monotonic_ns=None, thread_time_ns=None, standalone: bool = False,
521
640
  ) -> TickContext:
@@ -211,6 +211,11 @@ class BlocksPanelRow:
211
211
  cost_usd: float
212
212
  models: list[dict[str, Any]] # ModelCostRow shape, sorted desc by cost
213
213
  label: str # "HH:MM MMM DD" in local tz, e.g. "14:00 Apr 26"
214
+ # Which evidence supplied the displayed cost and model totals. The
215
+ # dashboard adapter upgrades this to ``retained`` only when the same
216
+ # unambiguous retained-facts decision replaces the row totals; absent
217
+ # retained facts stay ``computed`` (including partial/inconsistent facts).
218
+ facts_source: str = "computed"
214
219
 
215
220
 
216
221
  @dataclass
@@ -572,6 +577,81 @@ class WeeklyView:
572
577
  display_tz_label: str = ""
573
578
 
574
579
 
580
+ def _parse_iso_or_none(value):
581
+ """A tz-aware datetime, or ``None`` when the string will not parse.
582
+
583
+ A malformed bound must not raise inside a render path; it loses its
584
+ collision suffix instead, which is what the caller already does with
585
+ every other unparseable timestamp on the row.
586
+ """
587
+ if not value:
588
+ return None
589
+ try:
590
+ return _load_lib("_cctally_core").parse_iso_datetime(
591
+ value, "segment.start_ts")
592
+ except (TypeError, ValueError):
593
+ return None
594
+
595
+
596
+ def apply_label_collision_suffixes(items, *, display_tz=None):
597
+ """Suffix only the labels that two segments of ONE week render alike.
598
+
599
+ #750 S4 spec §3.2a / D5. ``items`` is a sequence of
600
+ ``(week_key, base_label, start_dt)`` and the return is a list of labels
601
+ positionally aligned with it.
602
+
603
+ ``week_key`` identifies the CANONICAL WEEK — the shared `start_date` /
604
+ `week_start_date` that epic invariant 3 keeps identical across a credited
605
+ week's segments — and grouping is confined to it. That restriction is
606
+ required rather than decorative: every base format in the tree is
607
+ year-free (`%m-%d` on the Weekly panel, `%b %d` on Trend), so grouping
608
+ across the whole rendered list would make two ordinary cycles exactly one
609
+ year apart collide and acquire suffixes neither needs.
610
+
611
+ Inside a group, a label is a collision when two or more siblings render it
612
+ identically. Only those gain a suffix, rendered from each segment's own
613
+ start instant through `format_display_dt`, the display-timezone
614
+ chokepoint. A segment whose rendered label is already unique is untouched,
615
+ so an uncredited week — and an ordinarily-credited week, whose segments
616
+ fall on different dates — is byte-identical.
617
+ """
618
+ items = list(items)
619
+ groups: dict = {}
620
+ for index, (week_key, base_label, _start) in enumerate(items):
621
+ groups.setdefault(week_key, {}).setdefault(base_label, []).append(index)
622
+ colliding: set = set()
623
+ for by_label in groups.values():
624
+ for indexes in by_label.values():
625
+ if len(indexes) > 1:
626
+ colliding.update(indexes)
627
+ if not colliding:
628
+ return [base for _key, base, _start in items]
629
+ tz = display_tz
630
+ if isinstance(tz, str):
631
+ tz = _load_lib("_lib_display_tz")._resolve_tz(tz)
632
+ fmt_dt = _cctally().format_display_dt
633
+ out: list = []
634
+ for index, (_key, base_label, start_dt) in enumerate(items):
635
+ if index not in colliding or start_dt is None:
636
+ out.append(base_label)
637
+ continue
638
+ out.append(
639
+ f"{base_label} "
640
+ f"{fmt_dt(start_dt, tz, fmt='%H:%M', suffix=False)}")
641
+ return out
642
+
643
+
644
+ def _capture_bound_iso(value_dt: "dt.datetime") -> str:
645
+ """A segment bound spelled the way `captured_at_utc` is stored.
646
+
647
+ Delegates to `_cctally_core.canonical_capture_bound`: the projects
648
+ reducer spells its bounds the same way, and two copies of a
649
+ normalization SQLite's lexicographic compare depends on is how two
650
+ surfaces disagree about which snapshot a cycle owns.
651
+ """
652
+ return _load_lib("_cctally_core").canonical_capture_bound(value_dt)
653
+
654
+
575
655
  def build_weekly_view(conn, entries, *, weeks, now_utc, display_tz=None,
576
656
  as_of_utc=None, mode="auto", aggregated_override=None,
577
657
  account_key=None):
@@ -624,67 +704,77 @@ def build_weekly_view(conn, entries, *, weeks, now_utc, display_tz=None,
624
704
  make_ref = _cct_core.make_week_ref
625
705
  get_usage = _cct_core.get_latest_usage_for_week
626
706
 
627
- # Segments of an in-place-credited week share `week_start_date`, so one
628
- # global `as_of_utc` resolves both to the same snapshot and renders the
629
- # same percent on both rows. Bound each such segment's lookup to its own
630
- # `end_ts` so the pre-credit segment reads the last snapshot captured
631
- # before the credit and the post-credit segment the latest one. Same
632
- # shape as `build_trend_view`'s `split_keys` handling.
633
- #
634
- # Every segment sharing a `start_date` is bounded EXCEPT THE LAST one.
635
- # The last segment's `end_ts` is the week's real end, not a credit
636
- # moment, so bounding it would reject a snapshot stamped with this
637
- # `week_start_date` but captured a few seconds past the boundary — the
638
- # capture-jitter tolerance
639
- # `test_build_weekly_view_keeps_the_global_as_of_for_an_uncredited_week`
640
- # protects on every uncredited week. Dropping that tolerance moves the
641
- # post-credit row's `Used %` to an earlier reading and raises `$/1%`
642
- # with it, on a week nobody edited.
707
+ # A segment's usage lookup is bounded when, and only when, an in-place
708
+ # CREDIT cut it — never merely because two segments share a `start_date`
709
+ # (#736). The decision comes from `in_place_cut_instants` bound through
710
+ # `credited_segment_cuts`, the same pair `build_trend_view`'s split
711
+ # handling calls; one shared predicate is what stops the two consumers
712
+ # drifting apart from each other and from the two appliers (epic
713
+ # invariant 1).
714
+ _cut_instants = _cct_core.in_place_cut_instants(
715
+ conn, account_key=account_key)
716
+
717
+ def _canon(value):
718
+ try:
719
+ return parse_iso(
720
+ value, "week.bound").astimezone(dt.timezone.utc)
721
+ except ValueError:
722
+ return None
723
+
724
+ # Grouping by `start_date` identifies the WEEK, which is what that column
725
+ # is (invariant 3); it is never the segment's identity. `segment_key` is.
643
726
  _by_start_date: dict = {}
644
727
  for w in weeks:
645
728
  _by_start_date.setdefault(w.start_date.isoformat(), []).append(w)
646
729
  _bounded_keys: set = set()
730
+ _credited_keys: set = set()
647
731
  for _group in _by_start_date.values():
648
- if len(_group) < 2:
732
+ # The week's cuts are matched through the segments' own STARTS, never
733
+ # through the week end: a cut is verbatim the start of the segment
734
+ # that follows it, whereas the tail's end is moved away from
735
+ # `new_week_end_at` by the overlap clamp or by a boundary shift.
736
+ _week_cuts = _cct_core.credited_segment_cuts(
737
+ (_canon(_w.start_ts) for _w in _group), _cut_instants)
738
+ if not _week_cuts:
649
739
  continue
650
- # Order by the segment's own end instant; a malformed `end_ts`
651
- # sorts last so the ordering stays total and the malformed segment
652
- # keeps the global `as_of` (which `_as_of_for` also falls back to).
653
- def _end_sort_key(w):
654
- try:
655
- return (0, parse_iso(w.end_ts, "week.end_ts"))
656
- except ValueError:
657
- return (1, dt.datetime.max.replace(tzinfo=dt.timezone.utc))
658
- _ordered = sorted(_group, key=_end_sort_key)
659
- _bounded_keys.update(w.segment_key for w in _ordered[:-1])
660
-
661
- def _as_of_for(sw):
662
- if sw.segment_key not in _bounded_keys:
663
- return as_of_utc
664
- try:
665
- seg_end_dt = parse_iso(sw.end_ts, "week.end_ts")
666
- except ValueError:
667
- return as_of_utc
668
- # Match the stored `captured_at_utc` spelling (UTC, seconds, `Z`)
669
- # so SQLite's lexicographic compare orders correctly — the same
670
- # normalization `cmd_weekly` applies to the global bound.
671
- seg_end = (
672
- seg_end_dt.astimezone(dt.timezone.utc)
673
- .replace(microsecond=0)
674
- .isoformat()
675
- .replace("+00:00", "Z")
676
- )
677
- if as_of_utc is None:
678
- return seg_end
740
+ _credited_keys.update(w.segment_key for w in _group)
741
+ for _w in _group:
742
+ _end = _canon(_w.end_ts)
743
+ if _end is not None and _end in _week_cuts:
744
+ _bounded_keys.add(_w.segment_key)
745
+
746
+ def _segment_bounds(sw):
747
+ """``(since_utc, before_utc)`` for one segment (#750 S4 spec §1.2).
748
+
749
+ A segment of an UNCREDITED week is unchanged in every respect, so
750
+ both are ``None`` there. Every segment of a credited week carries the
751
+ INCLUSIVE lower bound: without it a credited tail with no post-cut
752
+ observation resolves to the latest PRE-cut reading — a percent from
753
+ the previous cycle — instead of being reported as missing. Only a
754
+ segment whose own end IS a cut carries the EXCLUSIVE upper bound; the
755
+ tail keeps the global ``as_of_utc`` as its upper bound, which is the
756
+ post-boundary capture-jitter tolerance
757
+ ``test_build_weekly_view_keeps_the_global_as_of_for_an_uncredited_week``
758
+ protects on every uncredited week.
759
+ """
760
+ if sw.segment_key not in _credited_keys:
761
+ return None, None
679
762
  try:
680
- global_dt = parse_iso(as_of_utc, "weekly.as_of_utc")
763
+ since = _capture_bound_iso(parse_iso(sw.start_ts, "week.start_ts"))
681
764
  except ValueError:
682
- return seg_end
683
- return seg_end if seg_end_dt <= global_dt else as_of_utc
765
+ since = None
766
+ before = None
767
+ if sw.segment_key in _bounded_keys:
768
+ try:
769
+ before = _capture_bound_iso(parse_iso(sw.end_ts, "week.end_ts"))
770
+ except ValueError:
771
+ before = None
772
+ return since, before
684
773
 
685
774
  # Build asc overlay + asc WeeklyPeriodRow list first; reverse later.
686
775
  asc_overlay: list = []
687
776
  asc_rows: list = []
777
+ asc_label_items: list = []
688
778
  total_cost = 0.0
689
779
  total_tok = 0
690
780
  for i, b in enumerate(buckets_asc):
@@ -709,8 +799,11 @@ def build_weekly_view(conn, entries, *, weeks, now_utc, display_tz=None,
709
799
  week_start_at=sw.start_ts,
710
800
  week_end_at=sw.end_ts,
711
801
  )
712
- usage_row = get_usage(conn, ref, as_of_utc=_as_of_for(sw),
713
- account_key=account_key)
802
+ _since_utc, _before_utc = _segment_bounds(sw)
803
+ usage_row = get_usage(conn, ref, as_of_utc=as_of_utc,
804
+ account_key=account_key,
805
+ since_utc=_since_utc,
806
+ before_utc=_before_utc)
714
807
  used_pct = None
715
808
  if usage_row is not None and usage_row["weekly_percent"] is not None:
716
809
  used_pct = float(usage_row["weekly_percent"])
@@ -735,7 +828,14 @@ def build_weekly_view(conn, entries, *, weeks, now_utc, display_tz=None,
735
828
  is_current = False
736
829
 
737
830
  asc_rows.append(WeeklyPeriodRow(
738
- label=sw.start_date.strftime("%m-%d"),
831
+ # `display_start_date`, NOT the shared `start_date` join key:
832
+ # the applier moves the display date per segment, so a
833
+ # credited week's two cycles carry two labels. Every renderer
834
+ # in the tree already reads that field — `_render_weekly_table`,
835
+ # the weekly share artifact, and `_dashboard_build_weekly_periods`,
836
+ # which OVERWRITES this one with it. Building it right here is
837
+ # what removes that overwrite's reason to exist (#750 S4 §3.2).
838
+ label=sw.display_start_date.strftime("%m-%d"),
739
839
  cost_usd=b.cost_usd,
740
840
  total_tokens=b.total_tokens,
741
841
  input_tokens=b.input_tokens,
@@ -752,9 +852,23 @@ def build_weekly_view(conn, entries, *, weeks, now_utc, display_tz=None,
752
852
  week_start_at=sw.start_ts,
753
853
  week_end_at=sw.end_ts,
754
854
  ))
855
+ asc_label_items.append((
856
+ sw.start_date.isoformat(), asc_rows[-1].label,
857
+ _parse_iso_or_none(sw.start_ts),
858
+ ))
755
859
  total_cost += b.cost_usd
756
860
  total_tok += b.total_tokens
757
861
 
862
+ # D5 / §3.2a: two segments of one credited week whose display dates fall
863
+ # on ONE calendar day render the same `%m-%d`. Only those gain a time
864
+ # suffix; an uncredited week and an ordinarily-credited week are
865
+ # byte-identical.
866
+ for _row, _label in zip(
867
+ asc_rows,
868
+ apply_label_collision_suffixes(
869
+ asc_label_items, display_tz=display_tz)):
870
+ _row.label = _label
871
+
758
872
  # Reverse to newest-first across all three parallel lists.
759
873
  rows = list(reversed(asc_rows))
760
874
  aggregated = list(reversed(buckets_asc))
@@ -884,8 +998,10 @@ def build_trend_view(conn, *, now_utc, n=8, display_tz=None, skip_sync=False,
884
998
  if latest_usage is not None and latest_usage["week_start_date"] is not None:
885
999
  current_key = latest_usage["week_start_date"]
886
1000
  try:
1001
+ # SCOPED to the requesting account (#834 S2, #837), as the
1002
+ # `latest_usage` read above and `get_recent_weeks` already are.
887
1003
  canon_start, canon_end = c._get_canonical_boundary_for_date(
888
- conn, latest_usage["week_start_date"]
1004
+ conn, latest_usage["week_start_date"], account_key=account_key
889
1005
  )
890
1006
  current_ref = make_ref(
891
1007
  week_start_date=latest_usage["week_start_date"],
@@ -893,7 +1009,12 @@ def build_trend_view(conn, *, now_utc, n=8, display_tz=None, skip_sync=False,
893
1009
  week_start_at=canon_start,
894
1010
  week_end_at=canon_end,
895
1011
  )
896
- _adjusted = c._apply_reset_events_to_weekrefs(conn, [current_ref])
1012
+ # Scoped too (#834 S2, #837). A scoped boundary followed by a
1013
+ # merged reset-event applier is still a merged answer: another
1014
+ # account's in-place credit would split THIS account's current
1015
+ # ref and the trend row would stop matching it.
1016
+ _adjusted = c._apply_reset_events_to_weekrefs(
1017
+ conn, [current_ref], account_key=account_key)
897
1018
  if _adjusted:
898
1019
  current_week_start_at = _adjusted[0].week_start_at
899
1020
  except Exception:
@@ -903,13 +1024,56 @@ def build_trend_view(conn, *, now_utc, n=8, display_tz=None, skip_sync=False,
903
1024
  # week_refs come newest-first from get_recent_weeks; reverse.
904
1025
  chrono = list(reversed(week_refs))
905
1026
 
906
- # Split-key set (Bug D): credited weeks appear twice in week_refs
907
- # with identical WeekRef.key. Pin as_of_utc=week_end_at for those
908
- # so each segment finds its own latest snapshot.
909
- split_keys = {
910
- r.key for r in week_refs
911
- if sum(1 for x in week_refs if x.key == r.key) > 1
912
- }
1027
+ # Bug D, re-decided by #750 S4 §1.1: a credited week appears more than
1028
+ # once in `week_refs` under one `WeekRef.key`, and each appearance must
1029
+ # resolve its own snapshot. The trigger is the CREDIT, not the repeated
1030
+ # key — a key repeated by reset-day drift, a backfill or a boundary
1031
+ # repair must not silently move a rendered percent (#736). The predicate
1032
+ # is `in_place_cut_instants` bound through `credited_segment_cuts`, the
1033
+ # same pair `build_weekly_view` calls, which is what keeps the two
1034
+ # consumers from drifting apart from each other and from the two appliers
1035
+ # (epic invariant 1).
1036
+ def _canon_bound(value):
1037
+ if not value:
1038
+ return None
1039
+ try:
1040
+ return parse_iso(
1041
+ value, "week_ref.bound").astimezone(dt.timezone.utc)
1042
+ except ValueError:
1043
+ return None
1044
+
1045
+ _cut_instants = _cct_core.in_place_cut_instants(
1046
+ conn, account_key=account_key)
1047
+ _refs_by_key: dict = {}
1048
+ for _index, _r in enumerate(chrono):
1049
+ _refs_by_key.setdefault(_r.key, []).append((_index, _r))
1050
+ # Keyed on the ref's POSITION in `chrono`, which is the loop below's own
1051
+ # index: two segments of one credited week differ only in their bounds,
1052
+ # and `WeekRef.key` — the one field that would look like an identity — is
1053
+ # exactly what they share. `id(...)` also distinguishes them, but only
1054
+ # while every object stays alive, so a later dedup or regeneration of the
1055
+ # list would silently unbind every segment.
1056
+ segment_bounds_by_index: dict = {}
1057
+ for _refs in _refs_by_key.values():
1058
+ # Matched through the segments' own STARTS. A cut is verbatim the
1059
+ # start of the segment that follows it, whereas the tail's end is
1060
+ # moved away from `new_week_end_at` by `_apply_overlap_clamp_to_
1061
+ # subweeks` or by a boundary shift, and a lookup keyed on the week
1062
+ # end then found nothing and reported the week as uncredited.
1063
+ _week_cuts = _cct_core.credited_segment_cuts(
1064
+ (_canon_bound(_r.week_start_at) for _, _r in _refs),
1065
+ _cut_instants,
1066
+ )
1067
+ if not _week_cuts:
1068
+ continue
1069
+ for _index, _r in _refs:
1070
+ _end = _canon_bound(_r.week_end_at)
1071
+ _start = _canon_bound(_r.week_start_at)
1072
+ segment_bounds_by_index[_index] = (
1073
+ _capture_bound_iso(_start) if _start is not None else None,
1074
+ _capture_bound_iso(_end)
1075
+ if (_end is not None and _end in _week_cuts) else None,
1076
+ )
913
1077
 
914
1078
  try:
915
1079
  _fresh_cfg = c._get_oauth_usage_config(c.load_config())
@@ -917,13 +1081,13 @@ def build_trend_view(conn, *, now_utc, n=8, display_tz=None, skip_sync=False,
917
1081
  _fresh_cfg = c._get_oauth_usage_config({})
918
1082
 
919
1083
  intermediate: list = []
920
- for week_ref in chrono:
1084
+ for _chrono_index, week_ref in enumerate(chrono):
1085
+ _since_utc, _before_utc = segment_bounds_by_index.get(
1086
+ _chrono_index, (None, None))
921
1087
  usage = get_usage(
922
- conn, week_ref,
923
- as_of_utc=(
924
- week_ref.week_end_at if week_ref.key in split_keys else None
925
- ),
1088
+ conn, week_ref, as_of_utc=None,
926
1089
  account_key=account_key,
1090
+ since_utc=_since_utc, before_utc=_before_utc,
927
1091
  )
928
1092
  usage_captured_at = usage["captured_at_utc"] if usage else None
929
1093
  if c._week_ref_has_reset_event(conn, week_ref):
@@ -1085,6 +1249,16 @@ def build_trend_view(conn, *, now_utc, n=8, display_tz=None, skip_sync=False,
1085
1249
  if dpp is not None:
1086
1250
  prev_dpp = dpp
1087
1251
 
1252
+ # D5 / §3.2a: two segments of one credited week whose start instants fall
1253
+ # on ONE calendar day render the same `%b %d`. Only those gain a time
1254
+ # suffix, and grouping is confined to siblings of one canonical week
1255
+ # because `%b %d` is year-free.
1256
+ for _row, _label in zip(rows, apply_label_collision_suffixes(
1257
+ [(str(r.week_start_date), r.week_label, r.week_start_at)
1258
+ for r in rows],
1259
+ display_tz=display_tz)):
1260
+ _row.week_label = _label
1261
+
1088
1262
  # 3-sample average rule (spec §4.3): mean of non-None dpps iff at
1089
1263
  # least 3 samples qualify.
1090
1264
  valid_dpps = [r.dollars_per_percent for r in rows
@@ -1362,6 +1536,13 @@ def build_blocks_view(
1362
1536
  _lib_blocks = _load_lib("_lib_blocks")
1363
1537
  _lib_pricing = _load_lib("_lib_pricing")
1364
1538
  c = _cctally()
1539
+ # #751a: the grouping pass already decided which entry each block owns,
1540
+ # and adjacent recorded windows can overlap after a reset shift. The
1541
+ # per-model breakdown below consumes that decision instead of running
1542
+ # its own `start <= timestamp < end` rescan, which counted an
1543
+ # overlapping entry into both windows and made the breakdown exceed its
1544
+ # own parent row.
1545
+ entry_membership: dict[int, list] = {}
1365
1546
  blocks = _lib_blocks._group_entries_into_blocks(
1366
1547
  entries,
1367
1548
  mode=mode,
@@ -1369,6 +1550,7 @@ def build_blocks_view(
1369
1550
  block_start_overrides=block_start_overrides,
1370
1551
  canonical_intervals=canonical_intervals,
1371
1552
  now=now_utc,
1553
+ _entry_membership=entry_membership,
1372
1554
  )
1373
1555
  rows: list = []
1374
1556
  total_cost = 0.0
@@ -1379,17 +1561,15 @@ def build_blocks_view(
1379
1561
  continue
1380
1562
  if not skip_rows:
1381
1563
  # Per-block per-model breakdown for the dashboard row.
1382
- # Mirrors `_dashboard_build_blocks_panel`'s historical
1383
- # inline body — re-aggregates entries inside the block
1384
- # interval through the single pricing chokepoint so per-
1385
- # model costs reconcile exactly with `b.cost_usd`.
1564
+ # Prices the entries the grouping pass assigned to THIS
1565
+ # block, through the single pricing chokepoint, so per-model
1566
+ # costs reconcile exactly with `b.cost_usd`.
1386
1567
  per_model: dict[str, float] = {}
1387
- for e in entries:
1388
- if b.start_time <= e.timestamp < b.end_time:
1389
- cost = _lib_pricing._calculate_entry_cost(
1390
- e.model, e.usage, mode=mode, cost_usd=e.cost_usd,
1391
- )
1392
- per_model[e.model] = per_model.get(e.model, 0.0) + cost
1568
+ for e in entry_membership.get(id(b), ()):
1569
+ cost = _lib_pricing._calculate_entry_cost(
1570
+ e.model, e.usage, mode=mode, cost_usd=e.cost_usd,
1571
+ )
1572
+ per_model[e.model] = per_model.get(e.model, 0.0) + cost
1393
1573
  model_breakdowns = [
1394
1574
  {"modelName": name, "cost": cost}
1395
1575
  for name, cost in sorted(