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
@@ -95,6 +95,123 @@ def _floor_to_hour(ts: dt.datetime) -> dt.datetime:
95
95
  BLOCK_DURATION = dt.timedelta(hours=5)
96
96
 
97
97
 
98
+ @dataclass(frozen=True)
99
+ class OwnedWindow:
100
+ """One candidate owner of an entry: a canonical five-hour interval.
101
+
102
+ `key` is the window's canonical identity — the floored anchor
103
+ ``R`` on the grouping path, the ``five_hour_window_key`` on the
104
+ persisted path, a native block key in the diagnosis sources. It is
105
+ opaque to the rule apart from the tie-break below.
106
+ """
107
+ key: Any
108
+ start: dt.datetime
109
+ reset: dt.datetime
110
+
111
+
112
+ def _canonical_key_order(key: Any) -> tuple:
113
+ """A total order over canonical keys of the shapes this repo uses.
114
+
115
+ Datetimes, numbers and everything else each order among themselves and
116
+ never against each other, so a heterogeneous set still sorts without
117
+ raising.
118
+ """
119
+ if isinstance(key, dt.datetime):
120
+ # A naive key is read as UTC, explicitly. `astimezone` on a naive
121
+ # datetime reads it as HOST-LOCAL, which would make the tie-break
122
+ # depend on where the process runs. Every caller passes an aware
123
+ # datetime today, and this states the rule rather than leaving it
124
+ # latent (#769 S2 review P3-5). UTC is the repository's reading of
125
+ # a naive moment elsewhere too (`--block-start`).
126
+ moment = key if key.tzinfo is not None else key.replace(
127
+ tzinfo=dt.timezone.utc,
128
+ )
129
+ return (0, moment.astimezone(dt.timezone.utc).isoformat(), 0.0, "")
130
+ if isinstance(key, bool):
131
+ return (2, "", 0.0, str(key))
132
+ if isinstance(key, (int, float)):
133
+ return (1, "", float(key), "")
134
+ return (2, "", 0.0, str(key))
135
+
136
+
137
+ def _window_order(window: OwnedWindow) -> tuple:
138
+ """Earliest reset first; an equal reset breaks by canonical key."""
139
+ return (window.reset,) + _canonical_key_order(window.key)
140
+
141
+
142
+ def _entry_timestamp(entry: Any) -> dt.datetime:
143
+ # `datetime` carries a `timestamp` METHOD, so the datetime case is
144
+ # tested first rather than reached through getattr.
145
+ if isinstance(entry, dt.datetime):
146
+ return entry
147
+ return entry.timestamp
148
+
149
+
150
+ def resolve_owning_window(entry: Any, windows) -> "OwnedWindow | None":
151
+ """Return the one window that owns `entry`, or None (issue #751a).
152
+
153
+ An entry belongs to the containing half-open interval ``[start,
154
+ reset)`` whose reset is EARLIEST; an equal reset breaks
155
+ deterministically by canonical key. The result does not depend on the
156
+ order `windows` arrives in.
157
+
158
+ Ownership is decided among CONTAINING windows only, so the globally
159
+ earliest reset is not automatically the answer — a credit-truncated
160
+ window can reset before a later window that started after it.
161
+
162
+ `entry` is anything carrying a `.timestamp`, or a bare datetime.
163
+ Returning None means no exact window contains the entry, which is what
164
+ hands it to the heuristic grouper.
165
+
166
+ Pure: no I/O, no database, no clock.
167
+ """
168
+ owner = None
169
+ owner_order = None
170
+ ts = _entry_timestamp(entry)
171
+ for window in windows:
172
+ if not (window.start <= ts < window.reset):
173
+ continue
174
+ order = _window_order(window)
175
+ if owner is None or order < owner_order:
176
+ owner, owner_order = window, order
177
+ return owner
178
+
179
+
180
+ def partition_entries_by_owner(entries, windows):
181
+ """Split `entries` across `windows` under `resolve_owning_window`.
182
+
183
+ Returns ``(owned, leftovers)`` where `owned` maps every window's
184
+ canonical key to the entries it owns — including the empty list for a
185
+ window that owns none — and `leftovers` holds the entries no exact
186
+ window contains. Input order is preserved inside each bucket.
187
+
188
+ Each entry appears exactly once across the two results, which is the
189
+ property that stops one physical entry being priced into two windows.
190
+
191
+ Pure: no I/O, no database, no clock.
192
+ """
193
+ window_list = list(windows)
194
+ ordered = sorted(window_list, key=_window_order)
195
+ reset_keys = [w.reset for w in ordered]
196
+ owned: dict[Any, list] = {w.key: [] for w in window_list}
197
+ leftovers: list = []
198
+ for entry in entries:
199
+ ts = _entry_timestamp(entry)
200
+ # Every containing window resets after `ts`, so the search starts
201
+ # at the first such window and walks in ownership order; the first
202
+ # one that also started early enough is the owner.
203
+ owner = None
204
+ for window in ordered[bisect.bisect_right(reset_keys, ts):]:
205
+ if window.start <= ts:
206
+ owner = window
207
+ break
208
+ if owner is None:
209
+ leftovers.append(entry)
210
+ else:
211
+ owned[owner.key].append(entry)
212
+ return owned, leftovers
213
+
214
+
98
215
  def _group_entries_into_blocks(
99
216
  entries: list[UsageEntry],
100
217
  mode: str = "auto",
@@ -176,27 +293,17 @@ def _group_entries_into_blocks(
176
293
  # shape) go into recorded_buckets[R]. Everything else (gaps between
177
294
  # recorded windows, or fully outside any window) drops into
178
295
  # `leftover` and runs through the existing heuristic grouper.
179
- recorded_buckets: dict[dt.datetime, list[UsageEntry]] = {
180
- R: [] for R in recorded_windows
181
- }
182
- leftover: list[UsageEntry] = []
183
- # Sort the (interval, R) pairs by rs so bisect_right(rs_keys, ts)
184
- # locates the candidate window in O(log n) per entry.
185
- sorted_intervals: list[
186
- tuple[tuple[dt.datetime, dt.datetime], dt.datetime]
187
- ] = sorted(
188
- [(_exact_interval(R), R) for R in recorded_windows],
189
- key=lambda item: item[0][1],
296
+ # Ownership is `resolve_owning_window`'s rule, applied here through
297
+ # `partition_entries_by_owner` (issue #751a) so this pass and every
298
+ # consumer that later reasons about the same overlap share one rule
299
+ # rather than four independent reselections.
300
+ ownership_windows = [
301
+ OwnedWindow(key=R, start=bs, reset=rs)
302
+ for R, (bs, rs) in ((R, _exact_interval(R)) for R in recorded_windows)
303
+ ]
304
+ recorded_buckets, leftover = partition_entries_by_owner(
305
+ entries_sorted, ownership_windows,
190
306
  )
191
- rs_keys = [iv[0][1] for iv in sorted_intervals]
192
- for entry in entries_sorted:
193
- idx = bisect.bisect_right(rs_keys, entry.timestamp)
194
- if idx < len(sorted_intervals):
195
- (bs, rs), R = sorted_intervals[idx]
196
- if bs <= entry.timestamp < rs:
197
- recorded_buckets[R].append(entry)
198
- continue
199
- leftover.append(entry)
200
307
 
201
308
  # Phase 1: Group leftover entries into raw activity blocks
202
309
  raw_blocks: list[dict[str, Any]] = []
@@ -78,6 +78,7 @@ def _import_pricing_kernel():
78
78
  m.CACHE_WRITE_1H_MULTIPLIER,
79
79
  m._claude_fast_multiplier,
80
80
  m.claude_usage_dict,
81
+ m.TIERED_THRESHOLD,
81
82
  )
82
83
  from pathlib import Path
83
84
  import importlib.util
@@ -97,16 +98,23 @@ def _import_pricing_kernel():
97
98
  m.CACHE_WRITE_1H_MULTIPLIER,
98
99
  m._claude_fast_multiplier,
99
100
  m.claude_usage_dict,
101
+ m.TIERED_THRESHOLD,
100
102
  )
101
103
 
102
104
 
103
105
  (CACHE_WRITE_1H_MULTIPLIER, _claude_fast_multiplier,
104
- claude_usage_dict) = _import_pricing_kernel()
106
+ claude_usage_dict, DEFAULT_TIERED_THRESHOLD) = _import_pricing_kernel()
105
107
 
106
108
 
107
- # Anthropic's per-call >200K-tokens tier — kept in sync with bin/_lib_pricing.
108
- # Callers may override via the ``tiered_threshold`` kwarg.
109
- DEFAULT_TIERED_THRESHOLD = 200_000
109
+ # ``DEFAULT_TIERED_THRESHOLD`` above is Anthropic's per-call >200K-tokens tier,
110
+ # READ FROM ``_lib_pricing`` rather than duplicated here (#714). It used to be a
111
+ # literal `200_000` with nothing keeping it equal to
112
+ # ``_lib_pricing.TIERED_THRESHOLD``, which made it a pricing value a reload
113
+ # could not replace. It is refreshed by ``PRICING_EXPORT_BINDINGS`` on every
114
+ # swap, and ``_compute_entry_cache_dollars`` resolves it at CALL time — a
115
+ # default argument would bind it once at function-definition time, so the
116
+ # refresh would reach the name and never the calls.
117
+ # Callers may still override via the ``tiered_threshold`` kwarg.
110
118
 
111
119
 
112
120
  # Minimum baseline samples for the per-row anomaly classifier.
@@ -349,7 +357,7 @@ def _compute_entry_cache_dollars(
349
357
  cache_read_tokens: int,
350
358
  *,
351
359
  pricing: dict,
352
- tiered_threshold: int = DEFAULT_TIERED_THRESHOLD,
360
+ tiered_threshold: "int | None" = None,
353
361
  cache_1h_tokens: int | None = None,
354
362
  speed=None,
355
363
  ) -> tuple[float, float, float]:
@@ -374,7 +382,14 @@ def _compute_entry_cache_dollars(
374
382
  Unknown models (no pricing entry) → ``(0.0, 0.0, 0.0)`` silently;
375
383
  the CLI's ``_calculate_entry_cost`` path emits the one-shot stderr
376
384
  warning for unknown models elsewhere.
385
+
386
+ ``tiered_threshold=None`` resolves ``DEFAULT_TIERED_THRESHOLD`` HERE rather
387
+ than in the signature (#714). A default argument is evaluated once, at
388
+ function-definition time, so an in-process pricing reload would refresh the
389
+ module global and leave every call pricing from the superseded revision.
377
390
  """
391
+ if tiered_threshold is None:
392
+ tiered_threshold = DEFAULT_TIERED_THRESHOLD
378
393
  p = _lookup_pricing(model, pricing) or {}
379
394
  if not p:
380
395
  return (0.0, 0.0, 0.0)