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
@@ -605,6 +605,21 @@ _REVIEWED_WEEKLY_KIND = _lib_account_protocol.WEEKLY_OBSERVATION_DECISION_V2_KIN
605
605
  _REVIEWED_WEEKLY_LEGACY_KIND = "claude_weekly_observation_decision"
606
606
  _REVIEWED_WEEKLY_KINDS = frozenset((
607
607
  _REVIEWED_WEEKLY_LEGACY_KIND, _REVIEWED_WEEKLY_KIND))
608
+ #: #944: a Claude cost attribution decision is an ingest byproduct. Any sync or
609
+ #: cache open appends one when it decides rows, including rows a previous
610
+ #: release ingested across a downgrade, so it can land after an unfinished
611
+ #: reviewed decision with no new evidence behind it. It is `retained` outside
612
+ #: the claude-usage derivation, which reads only the attribution the cache
613
+ #: materialized, and the reviewed plan hashes already refuse any material
614
+ #: effect; the reviewed-decision suffix checks therefore skip it rather than
615
+ #: demand a fresh manifest.
616
+ _REVIEW_NEUTRAL_OP_KINDS = frozenset(("claude_cost_account",))
617
+
618
+
619
+ def _is_review_neutral(record) -> bool:
620
+ return (record.get("t") == "op"
621
+ and (record.get("payload") or {}).get("kind")
622
+ in _REVIEW_NEUTRAL_OP_KINDS)
608
623
  _REVIEWED_MANIFEST_KEYS_V1 = frozenset({
609
624
  "schemaVersion", "journalHighWater", "journalPrefixHash",
610
625
  "reviewedAt", "reason", "decisions",
@@ -987,6 +1002,9 @@ def _validate_retained_reviewed_op(record, prior_high_water, hasher) -> None:
987
1002
  # #797/#823/#840 (spec 4.6 B1): the protocol revision is part
988
1003
  # of a current decision's content-id core.
989
1004
  writer_revision=record.get("wpr"),
1005
+ # #1019/#1014 (spec 4.7 C5): and so is the successor
1006
+ # revision; a 1.116.1 decision has none.
1007
+ writer_successor=record.get("wpx"),
990
1008
  )):
991
1009
  raise _lib_rederive.RederiveConflict(
992
1010
  "malformed retained reviewed weekly decision op"
@@ -1261,6 +1279,11 @@ def replay_scratch_records(
1261
1279
  try:
1262
1280
  for hook in hooks:
1263
1281
  hook(ctx, record)
1282
+ # Flush point (b), #956 class 1: the end of a complete
1283
+ # observation or op, before its harvest.
1284
+ import _cctally_record
1285
+ _cctally_record.flush_five_hour_dirty_windows(
1286
+ conn, ctx, as_of=ctx.as_of_for(record))
1264
1287
  _journal._harvest(ctx)
1265
1288
  finally:
1266
1289
  if retention is not None:
@@ -1376,9 +1399,11 @@ def plan_claude_usage(
1376
1399
  "payload": logical["payload"],
1377
1400
  })
1378
1401
  config_fingerprint = _fingerprint(config_basis)
1402
+ accumulators: dict = {}
1379
1403
  selection = _lib_journal.resolve_effective_events(
1380
1404
  records,
1381
1405
  protocol_prefix_evidence=protocol_prefix_evidence,
1406
+ accumulators=accumulators,
1382
1407
  )
1383
1408
  tainted = [
1384
1409
  *selection.protocol_violations,
@@ -1392,6 +1417,15 @@ def plan_claude_usage(
1392
1417
  raise _lib_rederive.RederiveConflict(
1393
1418
  "journal contains tainted correction batch(es): " + summary
1394
1419
  )
1420
+ # #884 spec §5.1: the five-hour milestone lineage, compacted from the
1421
+ # selector's own fold. The fold is released here, before the scratch
1422
+ # replay; the lineage holds summaries only.
1423
+ physical = _excluded_physical_revisions(records)
1424
+ lineage = _lib_rederive.build_five_hour_lineage(
1425
+ accumulators.pop("fold"), selection=selection,
1426
+ physical_revisions=physical,
1427
+ )
1428
+ del accumulators
1395
1429
  # Only an unmarked close with a zero-cost model can need the historical
1396
1430
  # proof, so the scratch replay keeps retained entries for those blocks
1397
1431
  # alone rather than for every block in the journal.
@@ -1421,7 +1455,8 @@ def plan_claude_usage(
1421
1455
  )
1422
1456
  return _lib_rederive.build_claude_usage_plan(
1423
1457
  selection=selection,
1424
- physical_revisions=_excluded_physical_revisions(records),
1458
+ physical_revisions=physical,
1459
+ milestone_lineage=lineage,
1425
1460
  desired_events=desired,
1426
1461
  journal_high_water=journal_high_water,
1427
1462
  cache_fingerprint=cache_fingerprint,
@@ -1864,6 +1899,56 @@ def _latest_completed_family_batch(
1864
1899
  return latest_id, latest_high_water
1865
1900
 
1866
1901
 
1902
+ def _decision_batch_completed(records, index, completed_batches, family) -> bool:
1903
+ """Whether the reviewed decision op at ``records[index]`` has its own
1904
+ completed decision batch: the first non-review-neutral record after it
1905
+ is the begin marker of a completed ``family`` correction batch.
1906
+
1907
+ An op resumed after a torn batch begin (its begin marker written, its
1908
+ commit never) is treated as abandoned here. That is safe only because
1909
+ the op's corrective actions re-enter the fresh decision's delta and
1910
+ apply idempotently: re-applying an action the torn batch already
1911
+ materialized changes nothing."""
1912
+ for record in records[index + 1:]:
1913
+ if _is_review_neutral(record):
1914
+ continue
1915
+ return (
1916
+ record.get("t") == "correction_batch"
1917
+ and record.get("phase") == "begin"
1918
+ and record.get("family") == family
1919
+ and record.get("id") in completed_batches
1920
+ )
1921
+ return False
1922
+
1923
+
1924
+ def _fresh_manifest_baseline(records, completed_batches, family, *,
1925
+ exclude_index=None) -> list:
1926
+ """The baseline of a fresh reviewed manifest (#1015 R8): the journal
1927
+ prefix without any earlier reviewed decision op whose own decision batch
1928
+ never completed. Replaying such an op in the baseline put its corrective
1929
+ actions in the baseline plan, so `causal_delta_plan` dropped them from
1930
+ the fresh decision's batch and the held inputs stayed uncorrected until
1931
+ an unrelated plain rederive. The fresh manifest supersedes the abandoned
1932
+ op, so its actions belong in the fresh delta. The plain path that
1933
+ resumes or re-checks a retained decision (``exclude_index``) builds the
1934
+ same baseline, so its plan hashes equal the reviewed preview's.
1935
+
1936
+ Dropping an op whose batch was torn after its begin assumes the
1937
+ idempotence stated on `_decision_batch_completed`: the op's actions
1938
+ re-enter the delta and apply idempotently over anything the torn batch
1939
+ already wrote."""
1940
+ return [
1941
+ record for index, record in enumerate(records)
1942
+ if index != exclude_index and not (
1943
+ record.get("t") == "op"
1944
+ and (record.get("payload") or {}).get("kind")
1945
+ in _REVIEWED_WEEKLY_KINDS
1946
+ and not _decision_batch_completed(
1947
+ records, index, completed_batches, family)
1948
+ )
1949
+ ]
1950
+
1951
+
1867
1952
  def _preview_from_snapshot(
1868
1953
  family: str,
1869
1954
  *,
@@ -1903,7 +1988,7 @@ def _preview_from_snapshot(
1903
1988
  "reviewed weekly decision journalPrefixHash drifted"
1904
1989
  )
1905
1990
  planning_records = [*records, reviewed_op]
1906
- baseline_records = records
1991
+ baseline_records = None # #1015 R8: resolved after the selection
1907
1992
  else:
1908
1993
  decision_indexes = [
1909
1994
  index for index, record in enumerate(records)
@@ -1919,16 +2004,17 @@ def _preview_from_snapshot(
1919
2004
  records,
1920
2005
  protocol_prefix_evidence=protocol_evidence,
1921
2006
  )
2007
+ if reviewed_op is not None:
2008
+ baseline_records = _fresh_manifest_baseline(
2009
+ records, selection.completed_batches, family)
1922
2010
  if retained_decision is not None:
1923
- suffix = records[retained_decision_index + 1:]
1924
- first = suffix[0] if suffix else None
1925
- completed_decision_batch = (
1926
- first is not None
1927
- and first.get("t") == "correction_batch"
1928
- and first.get("phase") == "begin"
1929
- and first.get("family") == family
1930
- and first.get("id") in selection.completed_batches
1931
- )
2011
+ suffix = [
2012
+ record for record in records[retained_decision_index + 1:]
2013
+ if not _is_review_neutral(record)
2014
+ ]
2015
+ completed_decision_batch = _decision_batch_completed(
2016
+ records, retained_decision_index, selection.completed_batches,
2017
+ family)
1932
2018
  correction_only_suffix = all(
1933
2019
  record.get("t") in {"correction", "correction_batch"}
1934
2020
  for record in suffix
@@ -1937,10 +2023,9 @@ def _preview_from_snapshot(
1937
2023
  decision_op = retained_decision
1938
2024
  decision_from_journal = not completed_decision_batch
1939
2025
  decision_batch_completed = completed_decision_batch
1940
- baseline_records = [
1941
- *records[:retained_decision_index],
1942
- *records[retained_decision_index + 1:],
1943
- ]
2026
+ baseline_records = _fresh_manifest_baseline(
2027
+ records, selection.completed_batches, family,
2028
+ exclude_index=retained_decision_index)
1944
2029
  elif not completed_decision_batch:
1945
2030
  raise _lib_rederive.RederiveConflict(
1946
2031
  "unfinished reviewed weekly decision has new journal records "
@@ -2525,6 +2610,7 @@ def apply_db_rederive(
2525
2610
  and record.get("batch") == original_batch_id)
2526
2611
  )
2527
2612
  for record in preview.records[op_index + 1:]
2613
+ if not _is_review_neutral(record)
2528
2614
  ):
2529
2615
  raise _lib_rederive.RederiveConflict(
2530
2616
  "reviewed weekly decision retry has new journal records "
@@ -2812,6 +2898,16 @@ def _command_payload(
2812
2898
  body["reviewedWeeklyDecisionCounts"] = reviewed_decision_counts
2813
2899
  if plan is not None and plan.uncertain_cost_fact_count:
2814
2900
  body["uncertainCostFactCount"] = plan.uncertain_cost_fact_count
2901
+ if plan is not None and plan.withheld_milestone_repairs:
2902
+ # #884 spec §5.7: the five-hour blocks whose milestone repair the
2903
+ # lineage withheld (their money kept), outside the plan body.
2904
+ body["withheldMilestoneRepairs"] = [
2905
+ block.to_dict() for block in plan.withheld_milestone_repairs]
2906
+ if plan is not None and plan.orphaned_milestone_ids:
2907
+ # #943/#956 spec 4.4: five-hour milestones that resolved no block
2908
+ # before this plan (pre-existing damage), reported, never refused.
2909
+ body["orphanedFiveHourMilestoneCount"] = len(
2910
+ plan.orphaned_milestone_ids)
2815
2911
  if preview is not None and preview.baseline_plan is not None:
2816
2912
  body["baselinePlanHash"] = preview.baseline_plan.plan_hash
2817
2913
  body["baselineActionCounts"] = dict(preview.baseline_plan.counts)
@@ -2863,6 +2959,14 @@ def _emit_command_payload(payload: dict, *, as_json: bool) -> None:
2863
2959
  f"cctally: {payload['family']} is already current; "
2864
2960
  "no correction batch was appended."
2865
2961
  )
2962
+ withheld = payload.get("withheldMilestoneRepairs")
2963
+ if withheld and status in {"preview", "applied", "recovered", "no-op"}:
2964
+ # #884 operator decision O2: one line, right after the status line.
2965
+ print(
2966
+ f"Milestone repair withheld for {len(withheld)} five-hour "
2967
+ "block(s) with ambiguous history; rerun with --json and see "
2968
+ "withheldMilestoneRepairs."
2969
+ )
2866
2970
  if "baselinePlanHash" in payload:
2867
2971
  def _counts_text(counts):
2868
2972
  return ", ".join(