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
@@ -276,6 +276,7 @@ from _cctally_dashboard_sources import (
276
276
  build_codex_source_state,
277
277
  capture_codex_source_state,
278
278
  codex_decision_deadline_passed,
279
+ read_account_registry,
279
280
  refresh_codex_source_clock,
280
281
  resolve_dashboard_source_semantics,
281
282
  )
@@ -303,6 +304,11 @@ from _lib_dashboard_sources import (
303
304
  reuse_coherent_source_state,
304
305
  unavailable_source_state,
305
306
  )
307
+ from _lib_source_retry import (
308
+ EMPTY_RETRY_STATE,
309
+ clear_partial_retry,
310
+ plan_partial_retry,
311
+ )
306
312
 
307
313
 
308
314
  # === Module-level back-ref shims for helpers that STAY in bin/cctally ======
@@ -1033,10 +1039,13 @@ def _tui_build_percent_milestones(
1033
1039
  def _tui_build_five_hour_milestones(
1034
1040
  conn: sqlite3.Connection,
1035
1041
  five_hour_window_key: int | None,
1042
+ account_key: "str | None" = None,
1043
+ *,
1044
+ block_id: "int | None" = None,
1036
1045
  ) -> list[dict]:
1037
- """Return per-percent 5h-block milestones for the given window, in
1038
- capture-time order. Spec §5.3 — drives the CurrentWeekModal's new
1039
- 5h-milestone timeline section.
1046
+ """Return per-percent 5h-block milestones for the dashboard's live
1047
+ current-week route, in capture-time order. Spec §5.3 — drives the
1048
+ CurrentWeekModal's 5h-milestone timeline section.
1040
1049
 
1041
1050
  Bucket B per §3.2: NO ``reset_event_id`` filter — both pre- and
1042
1051
  post-credit segments render in the merged chronological stream so
@@ -1044,40 +1053,29 @@ def _tui_build_five_hour_milestones(
1044
1053
  repeated threshold values after an in-place credit. The React layer
1045
1054
  differentiates rows by ``reset_event_id`` for key uniqueness.
1046
1055
 
1047
- Returns [] when the current week has no API-anchored 5h block. The
1048
- envelope-shaped dict mirrors the CLI ``five-hour-breakdown --json``
1049
- milestone objects but with snake_case keys (envelope convention).
1056
+ #834 S1 (#836): a thin caller of the ONE milestone read,
1057
+ ``_load_five_hour_milestones`` (bin/_cctally_five_hour.py). Its own query
1058
+ filtered on ``five_hour_window_key`` alone while block uniqueness is
1059
+ ``(account_key, five_hour_window_key)``, so one physical window's
1060
+ per-account blocks all showed every account's milestones. ``block_id`` is
1061
+ the precise selector and is preferred when the caller holds one; the
1062
+ ``(window key, account)`` pair is the same uniqueness expressed as its two
1063
+ parts and is what a caller holding only a window passes. ``account_key``
1064
+ None stays the merged read, byte-stable on a single-account install.
1065
+
1066
+ The returned dicts carry the envelope's snake_case keys, including the
1067
+ additive ``effective_seven_day_pct_at_crossing``.
1068
+
1069
+ Returns [] when the current week has no API-anchored 5h block.
1050
1070
  """
1051
- if five_hour_window_key is None:
1071
+ if block_id is None and five_hour_window_key is None:
1052
1072
  return []
1053
- rows = conn.execute(
1054
- """
1055
- SELECT percent_threshold, captured_at_utc, block_cost_usd,
1056
- marginal_cost_usd, seven_day_pct_at_crossing,
1057
- reset_event_id
1058
- FROM five_hour_milestones
1059
- WHERE five_hour_window_key = ?
1060
- ORDER BY captured_at_utc ASC, id ASC
1061
- """,
1062
- (int(five_hour_window_key),),
1063
- ).fetchall()
1064
- out: list[dict] = []
1065
- for r in rows:
1066
- out.append({
1067
- "percent_threshold": int(r["percent_threshold"]),
1068
- "captured_at_utc": r["captured_at_utc"],
1069
- "block_cost_usd": float(r["block_cost_usd"]),
1070
- "marginal_cost_usd": (
1071
- None if r["marginal_cost_usd"] is None
1072
- else float(r["marginal_cost_usd"])
1073
- ),
1074
- "seven_day_pct_at_crossing": (
1075
- None if r["seven_day_pct_at_crossing"] is None
1076
- else float(r["seven_day_pct_at_crossing"])
1077
- ),
1078
- "reset_event_id": int(r["reset_event_id"] or 0),
1079
- })
1080
- return out
1073
+ c = sys.modules["cctally"]
1074
+ if block_id is not None:
1075
+ return c._load_five_hour_milestones(conn, block_id=int(block_id))
1076
+ return c._load_five_hour_milestones(
1077
+ conn, five_hour_window_key=int(five_hour_window_key),
1078
+ account_key=account_key)
1081
1079
 
1082
1080
 
1083
1081
  @dataclass(frozen=True)
@@ -1677,27 +1675,50 @@ def _tui_build_current_week(
1677
1675
 
1678
1676
  Returns None when no current-week usage snapshot exists.
1679
1677
  """
1680
- fetched = _fetch_current_week_snapshots(conn, now_utc)
1678
+ # #769 S11 (#824): ONE read, held-inclusive, split per axis below.
1679
+ # Retain the owner of the exact non-held weekly sample this card displays,
1680
+ # including when one real account coexists with unattributed history. R8
1681
+ # controls decoration, not the physical population used for computation.
1682
+ # Cost and forecast retain their existing merged semantics.
1683
+ fetched = _fetch_current_week_snapshots(
1684
+ conn, now_utc, include_held=True, include_account=True)
1681
1685
  if fetched is None:
1682
1686
  return None
1683
- week_start_at, week_end_at, samples = fetched
1684
- if not samples:
1687
+ week_start_at, week_end_at, observations = fetched
1688
+ if not observations:
1685
1689
  return None
1686
1690
  # Mirror the reset override applied by `_load_forecast_inputs` so the
1687
1691
  # Current Week card's spent_usd and $/1% reflect the post-reset window.
1688
- week_start_at, samples = _apply_midweek_reset_override(
1689
- conn, week_start_at, week_end_at, samples, now_utc=now_utc
1692
+ week_start_at, observations = _apply_midweek_reset_override(
1693
+ conn, week_start_at, week_end_at, observations, now_utc=now_utc
1690
1694
  )
1691
- if not samples:
1695
+ if not observations:
1696
+ return None
1697
+ # Held-inclusive sample shape: (captured_at_utc, weekly_percent,
1698
+ # five_hour_percent, weekly_observation_held[, account_key]). The optional
1699
+ # account key identifies the displayed weekly sample on a decorated store.
1700
+ # The helper does not surface five_hour_resets_at, so do a targeted lookup
1701
+ # below for the freshest non-NULL reset timestamp on the current week.
1702
+ #
1703
+ # #769 S11 (#824). This card publishes BOTH axes, and the dashboard
1704
+ # envelope reads `used_pct` and `latest_snapshot_at` straight off it for
1705
+ # `current_week.freshness` and `snapshot_age_seconds`. A held row carries
1706
+ # an older row's weekly value under this tick's capture instant, so
1707
+ # letting one reach `latest` would present stale weekly evidence as freshly
1708
+ # captured — review finding 5. The five-hour percentage takes the newest
1709
+ # observation of either kind, because a held row is written exactly when
1710
+ # its five-hour reading grew.
1711
+ weekly_samples = [s for s in observations if not s[3]]
1712
+ if not weekly_samples:
1692
1713
  return None
1693
- # samples tuple shape: (captured_at_utc, weekly_percent, five_hour_percent).
1694
- # See _fetch_current_week_snapshots at bin/cctally:9122
1695
- # (lines ~9189-9194 and ~9221-9226). That helper does not surface
1696
- # five_hour_resets_at, so do a targeted lookup here for the freshest
1697
- # non-NULL reset timestamp on the current week.
1698
- latest = samples[-1]
1714
+ latest = weekly_samples[-1]
1699
1715
  used_pct = float(latest[1])
1700
- five_hr_pct = float(latest[2]) if latest[2] is not None else None
1716
+ weekly_account = latest[4]
1717
+ # Five-hour readings may be newer held observations, but must remain on
1718
+ # the account that supplied the displayed weekly reading.
1719
+ five_hour_samples = [s for s in observations if s[4] == weekly_account]
1720
+ five_hr_raw = five_hour_samples[-1][2]
1721
+ five_hr_pct = float(five_hr_raw) if five_hr_raw is not None else None
1701
1722
  # #556 S1 §3.3: one walk yields both halves. The range is whatever
1702
1723
  # `spent_usd` already used — taken AFTER `_apply_midweek_reset_override`
1703
1724
  # above, so a mid-week reset shortens the accumulation and the published
@@ -1734,12 +1755,17 @@ def _tui_build_current_week(
1734
1755
  five_hr_resets_at: dt.datetime | None = None
1735
1756
  if matching_ws_texts:
1736
1757
  placeholders = ",".join("?" * len(matching_ws_texts))
1758
+ # HELD-INCLUSIVE, deliberately (#769 S11, #824): the reset instant is a
1759
+ # five-hour fact, and a held row's five-hour fields are the writing
1760
+ # tick's own. Excluding held rows here would make the countdown lag the
1761
+ # five-hour percentage rendered beside it.
1737
1762
  reset_row = conn.execute(
1738
1763
  f"SELECT five_hour_resets_at FROM weekly_usage_snapshots "
1739
1764
  f"WHERE week_start_at IN ({placeholders}) "
1740
1765
  f" AND five_hour_resets_at IS NOT NULL "
1766
+ f" AND account_key = ? "
1741
1767
  f"ORDER BY captured_at_utc DESC, id DESC LIMIT 1",
1742
- tuple(matching_ws_texts),
1768
+ tuple(matching_ws_texts) + (weekly_account,),
1743
1769
  ).fetchone()
1744
1770
  if reset_row is not None:
1745
1771
  try:
@@ -1782,6 +1808,7 @@ def _tui_build_current_week(
1782
1808
  freshness_age=freshness_age,
1783
1809
  five_hour_block=_select_current_block_for_envelope(
1784
1810
  conn, current_used_pct=used_pct, now_utc=now_utc,
1811
+ account_key=weekly_account,
1785
1812
  ),
1786
1813
  total_tokens=total_tokens,
1787
1814
  )
@@ -2856,24 +2883,19 @@ def _tui_resolve_account_scope(stats_conn, provider: str) -> dict[str, object] |
2856
2883
  swallow-and-degrade behaviour for the accounts WIRE, where the fallback
2857
2884
  loses decoration styling; here it would publish a wrong number.
2858
2885
 
2859
- The catch is narrow on purpose. ``real_account_count`` is one SELECT over
2860
- ``accounts`` — no file I/O, no label lookups — so ``sqlite3.Error`` covers
2861
- every failure the CALL can produce, and anything else is a real defect that
2862
- must not be swallowed into a silent withholding. ``ImportError`` is caught
2863
- alongside it because the deferred import is a different failure class from
2864
- the query: an unimportable module is exactly the "count cannot be read"
2865
- state this function exists to report, and letting it propagate would fail
2866
- the whole dashboard tick instead of withholding one figure.
2886
+ #819: the read and the narrow catch that used to be written out here now
2887
+ live in ``read_account_registry``, which the Codex source build also uses
2888
+ for its R8 decoration gate. That is the point — two written forms of one
2889
+ read could classify a failure differently and publish a decorated envelope
2890
+ beside a withheld combined figure. The reasoning for the catch is recorded
2891
+ at that function.
2892
+
2893
+ This surface remains the resolver for CLAUDE, and for the Codex reuse and
2894
+ degrade branches where no build established a reading this tick. A Codex
2895
+ state this tick BUILT already carries the authoritative scope, and the
2896
+ caller reuses that rather than reading a second time.
2867
2897
  """
2868
- try:
2869
- import _cctally_account
2870
- return {
2871
- "real_account_count": int(
2872
- _cctally_account.real_account_count(stats_conn, provider)
2873
- ),
2874
- }
2875
- except (ImportError, sqlite3.Error):
2876
- return None
2898
+ return read_account_registry(stats_conn, provider).account_scope
2877
2899
 
2878
2900
 
2879
2901
  def _tui_with_account_scope(
@@ -3497,6 +3519,82 @@ def _tui_claude_data_with_budget(
3497
3519
  return merged
3498
3520
 
3499
3521
 
3522
+ #: Process-local retry state for degraded (``partial``) source generations.
3523
+ #:
3524
+ #: #834 S2 (#830). ``reuse_coherent_source_state`` now refuses every
3525
+ #: ``partial`` prior, which is the correct statement of reuse eligibility and
3526
+ #: replaces the hand-maintained warning-code allowlist that used to refuse two
3527
+ #: of them. That refusal makes a persistent structural cause pay for a full
3528
+ #: rebuild on every tick, and ``_lib_source_retry`` is what bounds that cost:
3529
+ #: it retains a REPEATED retainable cause under one non-extendable deadline
3530
+ #: and refuses everything else outright, so an unclassified cause fails closed.
3531
+ #:
3532
+ #: The state is process-local publication state, in the same class as the
3533
+ #: retained hero cohort: a fresh process starts with no continuity and must
3534
+ #: rebuild. It is cleared whenever a tick runs with no prior bundle, which is
3535
+ #: exactly that cold start.
3536
+ _PARTIAL_RETRY_STATE = EMPTY_RETRY_STATE
3537
+
3538
+
3539
+ def _tui_reset_partial_retry_state() -> None:
3540
+ """Drop every armed retry key. Called on a cold tick; used by tests."""
3541
+ global _PARTIAL_RETRY_STATE
3542
+ _PARTIAL_RETRY_STATE = EMPTY_RETRY_STATE
3543
+
3544
+
3545
+ def _tui_retain_refused_partial(
3546
+ prior, *, provider: str, now_utc, data_version: str,
3547
+ ) -> bool:
3548
+ """Whether a refused ``partial`` prior may nonetheless be retained now.
3549
+
3550
+ Consulted only where exact-version reuse already refused. The prior must
3551
+ still be the generation that reuse WOULD have returned but for its
3552
+ availability — same version, fresh, carrying data — because retaining a
3553
+ generation whose evidence has moved on would republish stale rows rather
3554
+ than throttle a repeat.
3555
+
3556
+ The provider's armed key is dropped first when the prior is not partial,
3557
+ which is the tick after a healthy publish. A key left armed across a
3558
+ recovery would still be inside its deadline when the same cause returned,
3559
+ so the first partial after the recovery would be retained rather than
3560
+ rebuilt.
3561
+ """
3562
+ global _PARTIAL_RETRY_STATE
3563
+ if prior is None or prior.availability != "partial":
3564
+ _PARTIAL_RETRY_STATE = clear_partial_retry(
3565
+ _PARTIAL_RETRY_STATE, provider=provider,
3566
+ )
3567
+ return False
3568
+ # #846 §4.6. THE CARRIER DECIDES RETENTION, and it decides before the
3569
+ # warnings are consulted. This leg derived the retry cause from
3570
+ # `prior.warnings` alone, and a transient qualified-read failure publishes
3571
+ # the warning code `codex_metadata_incomplete`, which the retry kernel
3572
+ # normalizes to the retainable cause `metadata_incomplete` — so the second
3573
+ # tick retained the transient generation for up to `PARTIAL_RETRY_INTERVAL`
3574
+ # while the chip promised a retry that never came. No reuse or retention
3575
+ # gate may retain a retryable carrier.
3576
+ health = getattr(prior, "metadata_health", None)
3577
+ if isinstance(health, Mapping) and bool(health.get("retryable")):
3578
+ _PARTIAL_RETRY_STATE = clear_partial_retry(
3579
+ _PARTIAL_RETRY_STATE, provider=provider,
3580
+ )
3581
+ return False
3582
+ if (
3583
+ prior.freshness != "fresh"
3584
+ or prior.data is None
3585
+ or prior.data_version != data_version
3586
+ ):
3587
+ return False
3588
+ decision, _PARTIAL_RETRY_STATE = plan_partial_retry(
3589
+ _PARTIAL_RETRY_STATE,
3590
+ provider=provider,
3591
+ cause=tuple(prior.warnings),
3592
+ data_version=prior.data_version,
3593
+ now=now_utc,
3594
+ )
3595
+ return decision.action == "retain"
3596
+
3597
+
3500
3598
  def _tui_note_codex_regime(value: str) -> None:
3501
3599
  """Stamp the REALISED Codex source-leg decision on the open tick (§1.5).
3502
3600
 
@@ -3766,6 +3864,18 @@ def _tui_build_source_bundle(
3766
3864
  claude = reuse_coherent_source_state(
3767
3865
  prior_claude, data_version=claude_reuse_version,
3768
3866
  )
3867
+ # #834 S2 (#830): exact-version reuse now refuses every `partial`
3868
+ # prior. The kernel decides whether a REPEATED retainable cause
3869
+ # may nonetheless be retained for this tick, so refusing reuse
3870
+ # does not turn one persistent structural cause into a full
3871
+ # rebuild on every tick for the life of the process.
3872
+ if claude is None and _tui_retain_refused_partial(
3873
+ prior_claude,
3874
+ provider="claude",
3875
+ now_utc=now_utc,
3876
+ data_version=claude_reuse_version,
3877
+ ):
3878
+ claude = prior_claude
3769
3879
  # #556 S2 §3.6, gate 2 of 2. The version fragment above already
3770
3880
  # rejects a failed generation, but this gate is stated explicitly
3771
3881
  # rather than left implicit in string arithmetic: exact-version
@@ -3793,18 +3903,31 @@ def _tui_build_source_bundle(
3793
3903
  else unavailable_source_state("codex", warning)
3794
3904
  )
3795
3905
  else:
3906
+ # #834 S2 (#830): `_CODEX_NON_REUSABLE_WARNING_CODES` used to sit
3907
+ # in this condition, naming the two warning codes whose degraded
3908
+ # generation must not be reused. It is gone: a hand-maintained
3909
+ # list of warning strings was a second and incomplete definition
3910
+ # of non-reusability, and `reuse_coherent_source_state` now
3911
+ # refuses every `partial` prior unconditionally.
3912
+ _codex_forced_rebuild = prior_codex is not None and (
3913
+ _codex_accounting_pending
3914
+ or codex_decision_deadline_passed(prior_codex, now_utc)
3915
+ )
3796
3916
  codex = (
3797
- None if prior_codex is not None and (
3798
- _codex_accounting_pending
3799
- or any(
3800
- warning.code == "codex_projection_incoherent"
3801
- for warning in prior_codex.warnings
3802
- )
3803
- or codex_decision_deadline_passed(prior_codex, now_utc)
3804
- ) else reuse_coherent_source_state(
3917
+ None if _codex_forced_rebuild
3918
+ else reuse_coherent_source_state(
3805
3919
  prior_codex, data_version=codex_reuse_version,
3806
3920
  )
3807
3921
  )
3922
+ if codex is None and not _codex_forced_rebuild and (
3923
+ _tui_retain_refused_partial(
3924
+ prior_codex,
3925
+ provider="codex",
3926
+ now_utc=now_utc,
3927
+ data_version=codex_reuse_version,
3928
+ )
3929
+ ):
3930
+ codex = prior_codex
3808
3931
  if codex is not None and aggregate_scope_failed(codex):
3809
3932
  codex = None
3810
3933
  _tui_note_codex_regime("active" if codex is None else "idle")
@@ -4020,6 +4143,11 @@ def _tui_build_source_bundle(
4020
4143
  combined_accounting=claude_combined_accounting,
4021
4144
  aggregate_scope=claude_aggregate_scope,
4022
4145
  )
4146
+ # #819: did THIS tick build Codex, and therefore already establish the
4147
+ # authoritative account reading? Only a fresh build has one; the reuse
4148
+ # and degrade branches publish a generation some earlier tick produced,
4149
+ # and their scope is still resolved from the current tick below.
4150
+ codex_built_this_tick = False
4023
4151
  if codex is None:
4024
4152
  try:
4025
4153
  if codex_capture_failed or codex_context is None:
@@ -4031,10 +4159,24 @@ def _tui_build_source_bundle(
4031
4159
  codex_capture,
4032
4160
  data_version=codex_version,
4033
4161
  path_scope=codex_path_scope_value,
4162
+ # #769 S6 / #753: the prior bundle is handed forward so
4163
+ # a build landing in the reconcile gap can republish the
4164
+ # last coherent hero cohort instead of erasing a known
4165
+ # spend. Retention is process-local publication state:
4166
+ # after a restart whose first build is incoherent there
4167
+ # is no prior generation, and the hero renders Pending.
4168
+ prior_hero_cohort=(
4169
+ prior_codex.hero_cohort
4170
+ if prior_codex is not None else None
4171
+ ),
4034
4172
  )
4035
4173
  else:
4036
4174
  codex = build_codex_source_state(
4037
4175
  codex_context, data_version=codex_version,
4176
+ prior_hero_cohort=(
4177
+ prior_codex.hero_cohort
4178
+ if prior_codex is not None else None
4179
+ ),
4038
4180
  )
4039
4181
  # Attached ONLY on a fresh build, never on the reuse or degrade
4040
4182
  # paths: those carry rows this tick did not produce, and their
@@ -4043,6 +4185,7 @@ def _tui_build_source_bundle(
4043
4185
  codex,
4044
4186
  aggregate_scope=build_aggregate_scope(published_range),
4045
4187
  )
4188
+ codex_built_this_tick = True
4046
4189
  except Exception:
4047
4190
  _lib_log.get_logger("dashboard").error(
4048
4191
  "codex_read_model source build failed",
@@ -4079,8 +4222,17 @@ def _tui_build_source_bundle(
4079
4222
  claude = _tui_with_account_scope(
4080
4223
  claude, _tui_resolve_account_scope(stats_conn, "claude"),
4081
4224
  )
4225
+ # #819: a fresh Codex build already read the registry once, for its own
4226
+ # R8 decoration gate, and published what it read as this state's
4227
+ # `account_scope`. Reading again here is what let one tick decorate the
4228
+ # Codex envelope while the combined figure was withheld for
4229
+ # `account_scope_unresolved`, and it is also why a read failure could
4230
+ # be reported by this surface and by nothing else. Reuse and degrade
4231
+ # branches establish no reading, so they still resolve from this tick.
4082
4232
  codex = _tui_with_account_scope(
4083
- codex, _tui_resolve_account_scope(stats_conn, "codex"),
4233
+ codex,
4234
+ codex.account_scope if codex_built_this_tick
4235
+ else _tui_resolve_account_scope(stats_conn, "codex"),
4084
4236
  )
4085
4237
  combined = compose_all_state(claude, codex)
4086
4238
  bundle = SourceDashboardBundle(
@@ -4194,6 +4346,31 @@ def _tui_source_bundle_can_idle(bundle: SourceDashboardBundle | None) -> bool:
4194
4346
  # rebuild per tick, so it creates no retry loop.
4195
4347
  if aggregate_scope_failed(state):
4196
4348
  return False
4349
+ # #834 S2 (#829, #830), gate 2 of 2, and the same shape as gate 1. A
4350
+ # caught detail-probe read failure ALSO leaves an otherwise `ok` and
4351
+ # `fresh` provider, because `availability` turns `partial` only on
4352
+ # incomplete accounting rows, a failed hero projection or an unreadable
4353
+ # account registry -- never on the probe. Without this leg the bundle
4354
+ # qualifies for idle reuse and the transient state is republished for
4355
+ # the life of the process, which is what the browser measured: 4 minutes
4356
+ # 47 seconds across roughly twenty generations with no rebuild.
4357
+ #
4358
+ # `_lib_dashboard_sources._reusable_provider` refuses the same
4359
+ # generation, and refusing it there is NOT sufficient: this branch
4360
+ # republishes through the clock refreshes without ever calling
4361
+ # `reuse_coherent_source_state`, so the predicate is bypassed before it
4362
+ # is consulted. Both legs are required, and the browser gate found this
4363
+ # one by reading the path the first fix did not reach.
4364
+ health = state.metadata_health
4365
+ if health is not None and bool(health.get("retryable")):
4366
+ # #846 §4.6: refuse AND clear any armed key, so the tick after the
4367
+ # fault clears rebuilds rather than finding a key still inside its
4368
+ # deadline.
4369
+ global _PARTIAL_RETRY_STATE
4370
+ _PARTIAL_RETRY_STATE = clear_partial_retry(
4371
+ _PARTIAL_RETRY_STATE, provider=source,
4372
+ )
4373
+ return False
4197
4374
  return True
4198
4375
 
4199
4376
 
@@ -4977,9 +5154,20 @@ def _tui_build_snapshot_once(
4977
5154
  with _perf.phase("build.five_hour_milestones"):
4978
5155
  try:
4979
5156
  win_key = None
5157
+ acct_key = None
5158
+ block_id = None
4980
5159
  if cw is not None and isinstance(cw.five_hour_block, dict):
4981
5160
  win_key = cw.five_hour_block.get("five_hour_window_key")
4982
- fh_milestones = _tui_build_five_hour_milestones(conn, win_key)
5161
+ # #834 S1 (#836): the internal selector fields
5162
+ # `_select_current_block_for_envelope` carries. They are
5163
+ # stripped before publication (see `_five_hour_block_wire`),
5164
+ # so the wire shape is unchanged. `_block_id` is the precise
5165
+ # selector; `_account_key` scopes the window-key fallback,
5166
+ # which is what a stubbed or pre-#836 block dict takes.
5167
+ acct_key = cw.five_hour_block.get("_account_key")
5168
+ block_id = cw.five_hour_block.get("_block_id")
5169
+ fh_milestones = _tui_build_five_hour_milestones(
5170
+ conn, win_key, acct_key, block_id=block_id)
4983
5171
  except Exception as exc:
4984
5172
  capture_failure("five-hour-milestones", "stats", exc)
4985
5173
  # ---- hero-modal historical milestones week index (spec §1a/§3) ----
@@ -51,19 +51,34 @@ def _cctally():
51
51
  def _get_canonical_boundary_for_date(
52
52
  conn: sqlite3.Connection,
53
53
  week_start_date_str: str,
54
+ *,
55
+ account_key: "str | None" = None,
54
56
  ) -> tuple[str | None, str | None]:
55
- """Return the first established (week_start_at, week_end_at) for a week."""
57
+ """Return the first established (week_start_at, week_end_at) for a week.
58
+
59
+ #834 S2 (#837): ``account_key`` is keyword-only and defaults to ``None``,
60
+ which keeps the merged account-blind read every deliberately-merged caller
61
+ relies on. A real key or the ``unattributed`` sentinel scopes the read.
62
+
63
+ The scope matters because this query takes the EARLIEST established
64
+ boundary, so on a two-account store the account that captured first owned
65
+ every other account's boundary until it was scoped. Scoping to the
66
+ ``unattributed`` sentinel on a legacy store selects exactly the rows the
67
+ account-blind read selected, because every row there carries the sentinel.
68
+ """
69
+ acct_pred = "" if account_key is None else " AND account_key = ?"
70
+ acct_p: tuple = () if account_key is None else (account_key,)
56
71
  row = conn.execute(
57
- """
72
+ f"""
58
73
  SELECT week_start_at, week_end_at
59
74
  FROM weekly_usage_snapshots
60
75
  WHERE week_start_date = ?
61
76
  AND week_start_at IS NOT NULL AND week_start_at != ''
62
- AND week_end_at IS NOT NULL AND week_end_at != ''
77
+ AND week_end_at IS NOT NULL AND week_end_at != ''{acct_pred}
63
78
  ORDER BY captured_at_utc ASC, id ASC
64
79
  LIMIT 1
65
80
  """,
66
- (week_start_date_str,),
81
+ (week_start_date_str, *acct_p),
67
82
  ).fetchone()
68
83
  if row:
69
84
  start_at = _canonicalize_optional_iso(row["week_start_at"], "weekStartAt")
@@ -106,7 +121,8 @@ def get_recent_weeks(
106
121
  refs: list[WeekRef] = []
107
122
  for row in rows:
108
123
  date_str = row["week_start_date"]
109
- canon_start, canon_end = _get_canonical_boundary_for_date(conn, date_str)
124
+ canon_start, canon_end = _get_canonical_boundary_for_date(
125
+ conn, date_str, account_key=account_key)
110
126
  try:
111
127
  ref = make_week_ref(
112
128
  week_start_date=date_str,
@@ -573,11 +589,18 @@ def _backfill_week_reset_events(conn: sqlite3.Connection) -> None:
573
589
  # window, where the only rows that can carry an hour-floored instant
574
590
  # live. After cutover this is False and every genuine reset is admitted.
575
591
  legacy_window = _legacy_reset_window(conn)
592
+ # `weekly_observation_held = 0` (#769 S11, #824): this is a weekly
593
+ # PREDECESSOR scan — it derives a reset from a consecutive boundary
594
+ # change plus a drop. A held row repeats its basis's boundary and
595
+ # weekly value, so it can only contribute a no-change pair, and the
596
+ # predicate is what makes a divergent one impossible rather than
597
+ # improbable.
576
598
  rows = conn.execute(
577
599
  "SELECT captured_at_utc, week_end_at, weekly_percent, account_key"
578
600
  f"{origin_select} "
579
601
  "FROM weekly_usage_snapshots "
580
602
  "WHERE week_end_at IS NOT NULL "
603
+ " AND weekly_observation_held = 0 "
581
604
  "ORDER BY account_key ASC, captured_at_utc ASC, id ASC"
582
605
  ).fetchall()
583
606
  except sqlite3.DatabaseError: