cctally 1.116.1 → 1.117.2

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 (76) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +4 -6
  3. package/bin/_cctally_account.py +57 -3
  4. package/bin/_cctally_cache.py +5720 -885
  5. package/bin/_cctally_claude_attribution.py +997 -0
  6. package/bin/_cctally_completion.py +101 -28
  7. package/bin/_cctally_core.py +235 -12
  8. package/bin/_cctally_dashboard.py +263 -17
  9. package/bin/_cctally_dashboard_conversation.py +107 -21
  10. package/bin/_cctally_dashboard_envelope.py +44 -0
  11. package/bin/_cctally_db.py +928 -80
  12. package/bin/_cctally_diagnosis_sources.py +78 -13
  13. package/bin/_cctally_doctor.py +211 -86
  14. package/bin/_cctally_five_hour.py +446 -22
  15. package/bin/_cctally_forecast.py +302 -6
  16. package/bin/_cctally_journal.py +2418 -158
  17. package/bin/_cctally_milestone_history.py +379 -91
  18. package/bin/_cctally_oauth_samples.py +185 -7
  19. package/bin/_cctally_parser.py +38 -0
  20. package/bin/_cctally_project.py +34 -8
  21. package/bin/_cctally_quarantine.py +821 -343
  22. package/bin/_cctally_quota.py +69 -4
  23. package/bin/_cctally_quota_calibration.py +677 -3
  24. package/bin/_cctally_quota_model.py +927 -127
  25. package/bin/_cctally_record.py +922 -445
  26. package/bin/_cctally_rederive.py +119 -15
  27. package/bin/_cctally_statusline.py +1048 -176
  28. package/bin/_cctally_store.py +544 -110
  29. package/bin/_cctally_tui.py +455 -42
  30. package/bin/_cctally_update.py +77 -14
  31. package/bin/_lib_account_protocol.py +227 -19
  32. package/bin/_lib_claude_attribution.py +421 -0
  33. package/bin/_lib_clone_copy.py +30 -0
  34. package/bin/_lib_codex_conversation_query.py +45 -9
  35. package/bin/_lib_codex_conversation_watch.py +72 -13
  36. package/bin/_lib_codex_rollout_container.py +234 -19
  37. package/bin/_lib_codex_worker_lifecycle.py +374 -0
  38. package/bin/_lib_credit.py +22 -13
  39. package/bin/_lib_dashboard_sources.py +257 -18
  40. package/bin/_lib_detector_access_registry.py +1335 -166
  41. package/bin/_lib_doctor.py +276 -92
  42. package/bin/_lib_ingest_frontier.py +269 -36
  43. package/bin/_lib_journal.py +273 -22
  44. package/bin/_lib_jsonl.py +12 -0
  45. package/bin/_lib_meter_rate_change.py +72 -0
  46. package/bin/_lib_outcome_quarantine.py +917 -554
  47. package/bin/_lib_provider_activity.py +24 -6
  48. package/bin/_lib_quota_calibration.py +57 -3
  49. package/bin/_lib_quota_copy.py +7 -2
  50. package/bin/_lib_quota_lineage.py +613 -0
  51. package/bin/_lib_quota_model.py +464 -33
  52. package/bin/_lib_rate_change_delivery.py +16 -0
  53. package/bin/_lib_reclaim_planner.py +6 -4
  54. package/bin/_lib_rederive.py +2046 -21
  55. package/bin/_lib_selector_state.py +131 -3
  56. package/bin/_lib_snapshot_cache.py +8 -0
  57. package/bin/_lib_stale_proof.py +113 -4
  58. package/bin/_lib_statusline.py +72 -3
  59. package/bin/_lib_statusline_candidates.py +246 -42
  60. package/bin/_lib_tick_stats.py +10 -5
  61. package/bin/_lib_write_io.py +419 -6
  62. package/bin/cctally +28 -11
  63. package/dashboard/static/assets/{ConversationsView-Dflb_myA.js → ConversationsView-oqNsWzWV.js} +1 -1
  64. package/dashboard/static/assets/{DoctorModal-DFX5CfIs.js → DoctorModal-B0DnU9d9.js} +1 -1
  65. package/dashboard/static/assets/ModalRoot-CYua2wP_.js +1 -0
  66. package/dashboard/static/assets/{ProjectsDrillPanel-C3PJy1xx.js → ProjectsDrillPanel-DSxXQSa-.js} +1 -1
  67. package/dashboard/static/assets/{SourceDetailModal-BczsWKhD.js → SourceDetailModal-BleDpQqG.js} +1 -1
  68. package/dashboard/static/assets/{UpdateModal-Os-Lo5cs.js → UpdateModal-CMqaAkSK.js} +1 -1
  69. package/dashboard/static/assets/{index-CYZ3NLqF.js → index-BjQV-0-D.js} +3 -3
  70. package/dashboard/static/assets/index-D34eYhAc.css +1 -0
  71. package/dashboard/static/assets/{outlineNavigation-k-zIif6W.js → outlineNavigation-CO-VWBm7.js} +1 -1
  72. package/dashboard/static/assets/{useKeymap-CW7Up0Ay.js → useKeymap-CUgEnnua.js} +1 -1
  73. package/dashboard/static/dashboard.html +3 -3
  74. package/package.json +5 -1
  75. package/dashboard/static/assets/ModalRoot-HlxoagIa.js +0 -1
  76. package/dashboard/static/assets/index-BKITk1CT.css +0 -1
@@ -1020,14 +1020,22 @@ def _decorated_combined_leg(
1020
1020
  5, provider, "account_reconciliation_failed"),)
1021
1021
 
1022
1022
  total = math.fsum(cost for _key, cost in card_rows)
1023
- hero = _combined_hero(state)
1024
- hero_cost = hero.get("cost_usd") if hero is not None else None
1025
- if (
1026
- _counter_reason(hero_cost, integral=False) is not None
1027
- or abs(float(hero_cost) - total) > 1e-9
1028
- ):
1029
- return None, (_CombinedCause(
1030
- 5, provider, "account_reconciliation_failed"),)
1023
+ # The cards and their certified contributions above (keys, order, costs
1024
+ # within 1e-9, periods) are what certify the leg, and the headline is their
1025
+ # sum. A decorated Codex hero IS that sum (`build_codex_source_state`), so
1026
+ # it must still agree. A decorated Claude hero is not: under #836's U3 it is
1027
+ # the DISPLAYED account's own spend, by design one card rather than all of
1028
+ # them, so requiring equality would withhold the combined headline on every
1029
+ # multi-account Claude install.
1030
+ if provider != "claude":
1031
+ hero = _combined_hero(state)
1032
+ hero_cost = hero.get("cost_usd") if hero is not None else None
1033
+ if (
1034
+ _counter_reason(hero_cost, integral=False) is not None
1035
+ or abs(float(hero_cost) - total) > 1e-9
1036
+ ):
1037
+ return None, (_CombinedCause(
1038
+ 5, provider, "account_reconciliation_failed"),)
1031
1039
  return {
1032
1040
  "state": "current",
1033
1041
  "scope": "account_cycles",
@@ -1867,6 +1875,29 @@ def _meter_rate_change_relation(provider: str) -> tuple[str, str]:
1867
1875
  )
1868
1876
 
1869
1877
 
1878
+ # #1015 spec 5.4/5.7. A supersession relabels a published rate-change row
1879
+ # ("superseded by a correction") without touching `meter_rate_change_events`,
1880
+ # so the explicit `meter_rate_change_supersession` event needs its own relation
1881
+ # beside the events one: otherwise it moved neither provider digest, and
1882
+ # `data_version` and the provider reuse identity stayed put. Every column is
1883
+ # selected because the table is exactly the projection of that event; the
1884
+ # derived (not yet journalled) supersession is covered by the dispatch key's
1885
+ # `calibration_dependency` instead, which digests the correction lineage.
1886
+ _METER_RATE_CHANGE_SUPERSESSION_COLUMNS = (
1887
+ "provider, account_key, effective_from, reason, corrections, "
1888
+ "superseded_at_utc, created_at_utc"
1889
+ )
1890
+
1891
+
1892
+ def _meter_rate_change_supersession_relation(provider: str) -> tuple[str, str]:
1893
+ return (
1894
+ "meter_rate_change_supersessions",
1895
+ f"SELECT {_METER_RATE_CHANGE_SUPERSESSION_COLUMNS} "
1896
+ f"FROM meter_rate_change_supersessions WHERE provider='{provider}' "
1897
+ f"ORDER BY {_METER_RATE_CHANGE_SUPERSESSION_COLUMNS}",
1898
+ )
1899
+
1900
+
1870
1901
  # The column order is the approved cross-database identity contract. Keep the
1871
1902
  # relation sequence and tuples fixed: neither SQLite insertion order nor
1872
1903
  # surrogate/provenance/reconciliation-only fields may perturb the digest.
@@ -1921,6 +1952,7 @@ _CODEX_STATS_DIGEST_RELATIONS: tuple[tuple[str, str], ...] = (
1921
1952
  "projected_value, denominator, crossed_at_utc, alerted_at",
1922
1953
  ),
1923
1954
  _meter_rate_change_relation("codex"),
1955
+ _meter_rate_change_supersession_relation("codex"),
1924
1956
  )
1925
1957
 
1926
1958
 
@@ -1979,6 +2011,7 @@ _CLAUDE_STATS_DIGEST_RELATIONS: tuple[tuple[str, str], ...] = (
1979
2011
  "budget_usd, spent_usd, consumption_pct, crossed_at_utc, alerted_at",
1980
2012
  ),
1981
2013
  _meter_rate_change_relation("claude"),
2014
+ _meter_rate_change_supersession_relation("claude"),
1982
2015
  )
1983
2016
 
1984
2017
 
@@ -2074,9 +2107,50 @@ def _stats_relations_digest_memoizable(
2074
2107
  )
2075
2108
 
2076
2109
 
2110
+ def _sqlite_order_row_key(row) -> tuple:
2111
+ """Sort key giving SQLite's ``ORDER BY`` over every column of ``row``
2112
+ (#1046 spec 5.1): NULL, then numbers by exact value (an ``int`` against a
2113
+ ``float`` compares exactly in Python, as in SQLite, so neither is ever
2114
+ converted), then TEXT in code-point order (BINARY collation of a UTF-8
2115
+ store), then BLOB by bytes."""
2116
+ key = []
2117
+ for value in row:
2118
+ if value is None:
2119
+ key.append((0, 0))
2120
+ elif isinstance(value, (int, float)):
2121
+ key.append((1, value))
2122
+ elif isinstance(value, str):
2123
+ key.append((2, value))
2124
+ else:
2125
+ key.append((3, value))
2126
+ return tuple(key)
2127
+
2128
+
2129
+ def _stats_relation_rows(
2130
+ stats_conn: sqlite3.Connection,
2131
+ entry: tuple,
2132
+ ) -> list[list[object]]:
2133
+ """One digest relation's rows: ``entry`` is ``(name, sql)`` or
2134
+ ``(name, sql, row_key)``. A missing table is an empty relation; other
2135
+ SQLite failures propagate. With a row key the statement carries no
2136
+ ``ORDER BY`` and the rows are sorted here, so no history-sized sorter is
2137
+ built on the recurring dashboard path (#1046)."""
2138
+ query = entry[1]
2139
+ row_key = entry[2] if len(entry) > 2 else None
2140
+ try:
2141
+ rows = stats_conn.execute(query).fetchall()
2142
+ except sqlite3.OperationalError as exc:
2143
+ if "no such table" not in str(exc).lower():
2144
+ raise
2145
+ rows = []
2146
+ if row_key is not None:
2147
+ rows = sorted(rows, key=row_key)
2148
+ return [list(row) for row in rows]
2149
+
2150
+
2077
2151
  def _stats_relations_digest(
2078
2152
  stats_conn: sqlite3.Connection,
2079
- relations: tuple[tuple[str, str], ...],
2153
+ relations: tuple[tuple, ...],
2080
2154
  *,
2081
2155
  memo_namespace: "str | None" = None,
2082
2156
  ) -> str:
@@ -2092,15 +2166,9 @@ def _stats_relations_digest(
2092
2166
  cached = _STATS_RELATIONS_DIGEST_MEMO.get(key)
2093
2167
  if cached is not None:
2094
2168
  return cached
2095
- relation_rows: list[list[list[object]]] = []
2096
- for _name, query in relations:
2097
- try:
2098
- rows = stats_conn.execute(query).fetchall()
2099
- except sqlite3.OperationalError as exc:
2100
- if "no such table" not in str(exc).lower():
2101
- raise
2102
- rows = ()
2103
- relation_rows.append([list(row) for row in rows])
2169
+ relation_rows: list[list[list[object]]] = [
2170
+ _stats_relation_rows(stats_conn, entry) for entry in relations
2171
+ ]
2104
2172
  canonical = json.dumps(
2105
2173
  relation_rows,
2106
2174
  allow_nan=False,
@@ -2146,6 +2214,177 @@ def codex_stats_digest(stats_conn: sqlite3.Connection) -> str:
2146
2214
  )
2147
2215
 
2148
2216
 
2217
+ # #836 spec 5.7 item 1: the Claude weekly REJECTION state. A weekly credit's
2218
+ # retry can retire a stale replica in place with no new snapshot and a
2219
+ # deduplicated reset, and a suppression replacement or a convergence can
2220
+ # restore one; a DELETE of a referenced snapshot rewrites the milestone's FK to
2221
+ # the `0` sentinel without moving any `MAX(id)`. None of those moves another
2222
+ # signature leg, so this one digests the two relations they change:
2223
+ #
2224
+ # * the suppression-retired snapshots (held, without the held-write payload
2225
+ # marker), which the card's sample selection depends on;
2226
+ # * the five-hour milestones whose effective weekly join is unavailable
2227
+ # (absent snapshot, the `0` sentinel, or retired), which every published
2228
+ # `effective_seven_day_pct_at_crossing` depends on.
2229
+ #
2230
+ # The predicates are `_cctally_five_hour.effective_weekly_value_sql` /
2231
+ # `weekly_join_unavailable_sql`'s, spelled here because this kernel may not
2232
+ # import the glue. Ids only, rowid-ordered (no sorter); both relations are
2233
+ # empty on a store that never retired a row, and the wrapper maps that state to
2234
+ # "" so `data_version` and the Claude source version keep their bytes.
2235
+ _WEEKLY_RETIRED_PREDICATE = (
2236
+ "{a}weekly_observation_held = 1 AND "
2237
+ "json_extract({a}payload_json, '$.weeklyObservationHeld') IS NOT 1"
2238
+ )
2239
+
2240
+
2241
+ def _claude_weekly_retired_relations(
2242
+ stats_conn: sqlite3.Connection,
2243
+ ) -> tuple[tuple[str, str], ...]:
2244
+ try:
2245
+ cols = {str(row[1]) for row in stats_conn.execute(
2246
+ "PRAGMA table_info(weekly_usage_snapshots)")}
2247
+ except sqlite3.Error:
2248
+ cols = set()
2249
+ if "weekly_observation_held" in cols:
2250
+ retired = _WEEKLY_RETIRED_PREDICATE.format(a="")
2251
+ unavailable = ("s.id IS NULL OR ("
2252
+ + _WEEKLY_RETIRED_PREDICATE.format(a="s.") + ")")
2253
+ else:
2254
+ # A store without the column cannot hold a held row
2255
+ # (`_cctally_core.weekly_held_exclusion`).
2256
+ retired = "0"
2257
+ unavailable = "s.id IS NULL"
2258
+ return (
2259
+ (
2260
+ "weekly_usage_snapshots_retired",
2261
+ f"SELECT id FROM weekly_usage_snapshots WHERE {retired} "
2262
+ "ORDER BY id",
2263
+ ),
2264
+ (
2265
+ "five_hour_milestones_unavailable_join",
2266
+ "SELECT m.id FROM five_hour_milestones AS m "
2267
+ "LEFT JOIN weekly_usage_snapshots AS s ON s.id = m.usage_snapshot_id "
2268
+ f"WHERE {unavailable} ORDER BY m.id",
2269
+ ),
2270
+ )
2271
+
2272
+
2273
+ #: `_stats_relations_digest` over two EMPTY relations: the no-rejection state.
2274
+ _EMPTY_TWO_RELATION_DIGEST = hashlib.sha256(b"[[],[]]").hexdigest()
2275
+
2276
+
2277
+ def claude_weekly_retired_digest(stats_conn: sqlite3.Connection) -> str:
2278
+ """One digest over the Claude weekly rejection state (#836, spec 5.7
2279
+ item 1), or ``""`` when no snapshot is retired and no five-hour
2280
+ milestone's join is unavailable.
2281
+
2282
+ Read through `_stats_relations_digest` under its own memo namespace,
2283
+ statement-scoped and outside any stats transaction (as
2284
+ `_tui_compute_dispatch_signature` reads every stats leg), so a settled
2285
+ rollback-journal index costs no scan on an idle tick.
2286
+ """
2287
+ digest = _stats_relations_digest(
2288
+ stats_conn,
2289
+ _claude_weekly_retired_relations(stats_conn),
2290
+ memo_namespace="claude_weekly_retired",
2291
+ )
2292
+ return "" if digest == _EMPTY_TWO_RELATION_DIGEST else digest
2293
+
2294
+
2295
+ # #1015 spec 5.4: the two relation digests of the dashboard dispatch key's
2296
+ # `calibration_dependency` component. Neither is a `SnapshotSignature` leg, so
2297
+ # the published `data_version` contract is unchanged; both ride the dispatch
2298
+ # KEY, as `codex_dependency` does, and each memoizes under its own namespace so
2299
+ # an unchanged, settled stats file costs no relation scan on an idle tick.
2300
+ #
2301
+ # The basis digest covers every input `read_correction_facts` reads: the
2302
+ # completed batches with their commit coordinates and footprints, and each
2303
+ # reviewed decision row with its op coordinate and the index row it resolves
2304
+ # to. The index is joined rather than scanned whole: it grows with every Claude
2305
+ # weekly reading, and only the rows a decision references (or a reference that
2306
+ # later resolves) can change an assessment.
2307
+ #
2308
+ # #1046: the lineage and ops relations carry a third element, a row key, and no
2309
+ # `ORDER BY`. Each key admits NULL, so its autoindex serves only the first
2310
+ # term of the all-column order and SQLite sorted the rest of the whole history
2311
+ # in a temp B-tree on every cold dashboard build. `_stats_relation_rows` sorts
2312
+ # the rows the digest already materializes, in SQLite's own order, so the
2313
+ # digest bytes are unchanged.
2314
+ _CALIBRATION_BASIS_DIGEST_RELATIONS: tuple[tuple, ...] = (
2315
+ (
2316
+ "correction_batch_lineage",
2317
+ "SELECT batch_id, commit_segment, commit_offset "
2318
+ "FROM correction_batch_lineage",
2319
+ _sqlite_order_row_key,
2320
+ ),
2321
+ (
2322
+ "correction_batch_footprint",
2323
+ "SELECT batch_id, provider, account_key FROM correction_batch_footprint "
2324
+ "ORDER BY batch_id, provider, account_key",
2325
+ ),
2326
+ (
2327
+ "claude_weekly_correction_history",
2328
+ "SELECT h.decision_op_id, h.observation_id, h.disposition, "
2329
+ "o.op_segment, o.op_offset, i.account_key, i.captured_at_utc "
2330
+ "FROM claude_weekly_correction_history AS h "
2331
+ "LEFT JOIN claude_weekly_correction_ops AS o "
2332
+ "ON o.decision_op_id = h.decision_op_id "
2333
+ "LEFT JOIN claude_weekly_observation_index AS i "
2334
+ "ON i.observation_id = h.observation_id "
2335
+ "ORDER BY h.decision_op_id, h.observation_id, h.disposition, "
2336
+ "o.op_segment, o.op_offset, i.account_key, i.captured_at_utc",
2337
+ ),
2338
+ (
2339
+ "claude_weekly_correction_ops",
2340
+ "SELECT decision_op_id, op_segment, op_offset "
2341
+ "FROM claude_weekly_correction_ops",
2342
+ _sqlite_order_row_key,
2343
+ ),
2344
+ )
2345
+
2346
+ # The supersession digest: the explicit rows (every provider) and each
2347
+ # transition's frozen lineage, the other input of a derived supersession.
2348
+ _CALIBRATION_SUPERSESSION_DIGEST_RELATIONS: tuple[tuple[str, str], ...] = (
2349
+ (
2350
+ "meter_rate_change_supersessions",
2351
+ f"SELECT {_METER_RATE_CHANGE_SUPERSESSION_COLUMNS} "
2352
+ "FROM meter_rate_change_supersessions "
2353
+ f"ORDER BY {_METER_RATE_CHANGE_SUPERSESSION_COLUMNS}",
2354
+ ),
2355
+ (
2356
+ "meter_rate_change_events",
2357
+ "SELECT provider, account_key, effective_from, detected_at_utc, "
2358
+ "fit_lineage FROM meter_rate_change_events "
2359
+ "ORDER BY provider, account_key, effective_from, detected_at_utc, "
2360
+ "fit_lineage",
2361
+ ),
2362
+ )
2363
+
2364
+
2365
+ def calibration_basis_digest(stats_conn: sqlite3.Connection) -> str:
2366
+ """Hash the correction lineage relations (#1015 spec 5.4).
2367
+
2368
+ A missing table is an empty relation, so a store older than epoch 1022
2369
+ has a stable digest; other SQLite failures propagate, like the two
2370
+ provider digests.
2371
+ """
2372
+ return _stats_relations_digest(
2373
+ stats_conn,
2374
+ _CALIBRATION_BASIS_DIGEST_RELATIONS,
2375
+ memo_namespace="calibration_basis",
2376
+ )
2377
+
2378
+
2379
+ def calibration_supersession_digest(stats_conn: sqlite3.Connection) -> str:
2380
+ """Hash the supersession rows and the transitions' frozen lineage."""
2381
+ return _stats_relations_digest(
2382
+ stats_conn,
2383
+ _CALIBRATION_SUPERSESSION_DIGEST_RELATIONS,
2384
+ memo_namespace="calibration_supersession",
2385
+ )
2386
+
2387
+
2149
2388
  def assess_codex_projection_coherence(
2150
2389
  *,
2151
2390
  active_root_keys: tuple[str, ...] | list[str] | set[str],