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
@@ -56,23 +56,37 @@ def _cctally():
56
56
  return sys.modules["cctally"]
57
57
 
58
58
 
59
- # Per-model context window (used by `cctally statusline` segment 4).
60
- # Keep in sync with Anthropic's docs:
61
- # https://docs.anthropic.com/en/docs/about-claude/models
62
- # Unknown model id → segment renders `🧠 N/A` + one-shot stderr warn.
59
+ # Per-model context window FALLBACK (used by `cctally statusline` segment 4
60
+ # and by `explain`'s per-request capacity). The payload's own
61
+ # `context_window.context_window_size` is authoritative whenever Claude Code
62
+ # sends it (#1002); these tables answer only when it does not, and they are
63
+ # `explain`'s only source because transcripts record a bare model id.
64
+ # Resolution order lives in `_resolve_context_window`: any `[1m]`-suffixed id
65
+ # → 1M (generic, no entry needed), this registry, the family default below,
66
+ # else `🧠 N/A` + a one-shot stderr warning.
67
+ #
68
+ # Registry of VERIFIED current-generation defaults: ids whose bare form runs
69
+ # in a 1M window (Claude API model reference shipped with Claude Code
70
+ # 2.1.312). Exact match on the lowercased id with one trailing `-YYYYMMDD`
71
+ # stripped, so `claude-opus-5` never matches `claude-opus-5-5`. Add a model
72
+ # here, beside its pricing, when it ships. Prior generations (Opus 5 / 4.x,
73
+ # Sonnet 5 / 4.x, Haiku 4.5) stay on the 200K family default. Where one offered
74
+ # 1M (Opus 5, 4.8 and 4.7, Sonnet 4.5) it was an explicit `[1m]` opt-in that
75
+ # the transcript id does not record, so a bare id of theirs is historically
76
+ # ambiguous and deliberately left at 200K.
63
77
  CLAUDE_MODEL_CONTEXT_WINDOWS = {
64
- # 1M-token variants (explicit IDs override the family default).
65
- "claude-opus-5[1m]": 1_000_000,
66
- "claude-opus-4-8[1m]": 1_000_000,
67
- "claude-opus-4-7[1m]": 1_000_000,
68
- "claude-sonnet-4-5[1m]": 1_000_000,
69
- # Default 200K for every other Sonnet/Opus/Haiku family member.
70
- # The resolver does a substring match on the family token if the
71
- # exact id is missing — see _resolve_context_window.
78
+ "claude-opus-5-5": 1_000_000,
79
+ "claude-sonnet-5-5": 1_000_000,
80
+ "claude-haiku-5-5": 1_000_000,
81
+ "claude-fable-5-1": 1_000_000,
82
+ "claude-fable-5": 1_000_000,
83
+ "claude-mythos-5-1": 1_000_000,
72
84
  }
73
85
 
74
86
  CLAUDE_MODEL_CONTEXT_WINDOW_DEFAULT_FAMILY = {
75
87
  # Substring (case-insensitive) → window. Order matters; first hit wins.
88
+ # Deliberately no `fable`/`mythos` entry: an unregistered member of those
89
+ # lines has no verified window and renders `N/A` rather than a guess.
76
90
  "sonnet": 200_000,
77
91
  "opus": 200_000,
78
92
  "haiku": 200_000,
@@ -321,7 +335,8 @@ def cmd_statusline(args: argparse.Namespace) -> int:
321
335
  # =========================================================================
322
336
  #
323
337
  # Every current statusline coordination file lives under ONE sibling root,
324
- # `APP_DIR/statusline-account-bound-v2/`, partitioned by the exact account:
338
+ # `APP_DIR/statusline-account-bound-v3/` (#1019/#1014; v2 before it), partitioned
339
+ # by the exact account:
325
340
  #
326
341
  # selected.lock the selected-state / persist flock
327
342
  # transport.last render-path transport marker
@@ -658,7 +673,7 @@ def _candidate_from_input(parsed, *, received_at: int) -> "_candidates.Candidate
658
673
 
659
674
 
660
675
  def _activity_cache_path(token: str) -> pathlib.Path:
661
- """The contributor's provider-activity scan cache in the v2 spool (#797
676
+ """The contributor's provider-activity scan cache in the v3 spool (#797
662
677
  spec 4.1, D10a): one accelerator document per contributor, never read by
663
678
  acknowledgement, detached reduction or frozen-plan recovery."""
664
679
  if _candidates.CANDIDATE_TOKEN_RE.fullmatch(token) is None:
@@ -666,6 +681,17 @@ def _activity_cache_path(token: str) -> pathlib.Path:
666
681
  return _bound_root() / "activity-cache" / f"{token}.json"
667
682
 
668
683
 
684
+ def _legacy_activity_cache_path(token: str) -> pathlib.Path:
685
+ """The same contributor's scan cache in the v2 spool: the starting state
686
+ of an absent v3 document (#1019 C4), read once and never locked, written
687
+ or pruned. The cache is an accelerator, not coordination state; without
688
+ it every session with more than eight subagent files carried unknown
689
+ witnesses for its first renders after the cutover."""
690
+ if _candidates.CANDIDATE_TOKEN_RE.fullmatch(token) is None:
691
+ raise ValueError("invalid candidate token")
692
+ return _cctally_core.STATUSLINE_ACCOUNT_BOUND_V2_DIR / "activity-cache" / f"{token}.json"
693
+
694
+
669
695
  def _capture_activity(parsed, token: str):
670
696
  """The arrival's frozen provider-activity witness (#797 spec 4.1, 4.2),
671
697
  captured once here: in the render process, after Claude Code composed the
@@ -676,7 +702,8 @@ def _capture_activity(parsed, token: str):
676
702
 
677
703
  path = parsed.transcript_path if isinstance(parsed.transcript_path, str) else None
678
704
  return _lib_provider_activity.capture_activity(
679
- path, cache_path=_activity_cache_path(token))
705
+ path, cache_path=_activity_cache_path(token),
706
+ seed_path=_legacy_activity_cache_path(token))
680
707
 
681
708
 
682
709
  #: Completion markers of the account-bound root's persistence barriers
@@ -1207,6 +1234,7 @@ def _read_db_projection_once(*, account_key: str, at: "int | None" = None,
1207
1234
 
1208
1235
  five_projection = None
1209
1236
  five_retired = None
1237
+ five_floor_epoch = None
1210
1238
  if five_groups:
1211
1239
  canonical = max(five_groups)
1212
1240
  # #832: the exact account's event, by logical order. A database
@@ -1219,6 +1247,7 @@ def _read_db_projection_once(*, account_key: str, at: "int | None" = None,
1219
1247
  five_floor = _cctally_journal.resolve_five_hour_axis_floor(
1220
1248
  conn, account_key=account_key, canonical_window_key=canonical)
1221
1249
  generation = five_floor.logical_generation
1250
+ five_floor_epoch = five_floor.floor_epoch
1222
1251
  eligible = [
1223
1252
  row for row in five_groups[canonical]
1224
1253
  if _cctally_journal.captured_at_or_after_floor(
@@ -1263,7 +1292,8 @@ def _read_db_projection_once(*, account_key: str, at: "int | None" = None,
1263
1292
  conn, account_key, groups=weekly_groups, selected=weekly_selected,
1264
1293
  projection=weekly_projection, floor=weekly_floor,
1265
1294
  reference=reference,
1266
- five_window=max(five_groups) if five_groups else None))
1295
+ five_window=max(five_groups) if five_groups else None,
1296
+ five_projection=five_projection, five_floor_epoch=five_floor_epoch))
1267
1297
  finally:
1268
1298
  conn.close()
1269
1299
  if validity is not None:
@@ -1312,12 +1342,15 @@ class _StaleInputs:
1312
1342
  five_samples: tuple = ()
1313
1343
 
1314
1344
 
1315
- #: #797 spec 4.4: ``(samples_after, receipts_held)`` for the partition the
1316
- #: detached reducer is working on — the earliest provider-activity instant
1317
- #: any of its members or receipts can need a sample after, and whether it
1318
- #: holds receipts (a refusal is then possible, so an uncovered sample
1319
- #: projection makes classification unavailable, never "no proof needed").
1320
- _STALE_SCOPE = contextvars.ContextVar("statusline_stale_scope", default=(None, False))
1345
+ #: #797 spec 4.4: ``(samples_after, receipts_held, drop_work)`` for the
1346
+ #: partition the detached reducer is working on — the earliest instant any
1347
+ #: of its members, receipts, retained supports or frozen-plan dependencies
1348
+ #: can need a sample after, whether it holds receipts, and its drop-proof
1349
+ #: work (#1019 spec 4.4, `_DropWork`, None outside a detached pass). A
1350
+ #: refusal is then possible, so an uncovered sample projection makes
1351
+ #: classification unavailable, never "no proof needed".
1352
+ _STALE_SCOPE = contextvars.ContextVar("statusline_stale_scope",
1353
+ default=(None, False, None))
1321
1354
 
1322
1355
 
1323
1356
  def _stale_index_identity(conn) -> str:
@@ -1507,30 +1540,44 @@ def _five_hour_window_credits(conn, account_key: str, window: int) -> tuple:
1507
1540
 
1508
1541
 
1509
1542
  def _read_stale_inputs(conn, account_key: str, *, groups, selected, projection,
1510
- floor, reference, five_window=None) -> _StaleInputs:
1543
+ floor, reference, five_window=None, five_projection=None,
1544
+ five_floor_epoch=None) -> _StaleInputs:
1511
1545
  """The account's `_StaleInputs` on the caller's snapshot (#797 spec 4.4).
1512
1546
  Provider samples are read only when the partition's members or receipts
1513
1547
  need them (`_STALE_SCOPE`); an uncovered sample projection with receipts
1514
- held raises `ProjectionUnavailable`, so the classification is
1515
- unavailable rather than silently "no proof"."""
1548
+ held, or with proof work on either axis (#1019 spec 4.4, 4.5,
1549
+ `_DropWork`: drop-proof or rise-proof work, the five-hour axis judged
1550
+ against ``five_projection`` and ``five_floor_epoch``), raises
1551
+ `ProjectionUnavailable`, so the classification is unavailable rather
1552
+ than silently "no proof"."""
1516
1553
  import _cctally_oauth_samples
1517
1554
  import _lib_stale_proof
1518
1555
 
1519
1556
  identity = _stale_index_identity(conn)
1520
1557
  if not groups and five_window is None:
1521
1558
  return _StaleInputs(None, None, (), (), None, True, identity, frozenset())
1522
- after, receipts_held = _STALE_SCOPE.get()
1559
+ after, receipts_held, drop_work = _STALE_SCOPE.get()
1523
1560
  covered = _cctally_oauth_samples.projection_covered(conn, account_key=account_key)
1524
- if not covered and receipts_held:
1561
+ if not covered and (receipts_held or (
1562
+ drop_work is not None and (
1563
+ drop_work.needs_samples(
1564
+ max(groups) if groups else None, projection,
1565
+ None if floor is None else floor.floor_epoch)
1566
+ or drop_work.needs_samples(
1567
+ five_window, five_projection, five_floor_epoch,
1568
+ axis="fiveHour")))):
1525
1569
  raise _cctally_oauth_samples.ProjectionUnavailable(
1526
1570
  "the provider sample projection does not cover this account")
1527
1571
  five_accounted, five_credits, five_samples = _five_hour_stale_inputs(
1528
1572
  conn, account_key, five_window, after=after if covered else None)
1529
1573
  if not groups:
1530
1574
  # Review F-I5-1: the five-hour refusal inputs never depend on a
1531
- # weekly reading. The weekly fields keep the no-window shape.
1532
- return _StaleInputs(None, None, (), (), None, True, identity, frozenset(),
1533
- five_window, five_accounted, five_credits, five_samples)
1575
+ # weekly reading. The weekly fields keep the no-window shape, but the
1576
+ # sample bound and coverage are the five-hour samples' own (#1019:
1577
+ # the render cache decides five-hour drop proofs from them).
1578
+ return _StaleInputs(None, None, (), (), after if covered else None, covered,
1579
+ identity, frozenset(), five_window, five_accounted,
1580
+ five_credits, five_samples)
1534
1581
  window = max(groups)
1535
1582
  supports = {f"row:{int(row['id'])}" for row in groups[window]}
1536
1583
  samples = ()
@@ -2108,6 +2155,8 @@ def _pending_document(value: "_candidates.PendingDrop | None") -> dict | None:
2108
2155
  "percent": item.percent,
2109
2156
  "rawResetsAt": item.raw_resets_at,
2110
2157
  "receivedAt": item.received_at,
2158
+ # #1019/#1014 (spec 4.2): control schema 3.
2159
+ "witness": _witness_document(item.witness),
2111
2160
  }
2112
2161
  for item in value.retained.observations
2113
2162
  ],
@@ -2116,6 +2165,14 @@ def _pending_document(value: "_candidates.PendingDrop | None") -> dict | None:
2116
2165
  }
2117
2166
 
2118
2167
 
2168
+ def _witness_document(witness) -> "dict | None":
2169
+ """A retained observation's persisted witness, or None."""
2170
+ if witness is None:
2171
+ return None
2172
+ import _lib_provider_activity
2173
+ return _lib_provider_activity.witness_to_json(witness)
2174
+
2175
+
2119
2176
  def _control_document(control: "_candidates.ControlState") -> dict:
2120
2177
  files = control.db_projection.db_files or {"main": None, "wal": None}
2121
2178
  return {
@@ -2770,11 +2827,14 @@ class _Transition:
2770
2827
  publication: "dict | None" = None
2771
2828
  #: #825/#832 spec 5.6 item 7: ``{"authorityCommit": <observation id of
2772
2829
  #: the last bound authoritative publication whose control reconcile
2773
- #: committed>, "dropWithheld": bool}``, or None. The control recovery
2774
- #: certificate needs BOTH the journal-derived witness in the index and
2775
- #: this transition's record of its commit; ``dropWithheld`` lets the
2776
- #: render path's file-only fork decision see a withheld drop without
2777
- #: opening the index.
2830
+ #: committed>, "dropWithheld": bool}``, or None. Since #948 neither key
2831
+ #: gates recovery (authority is journal-only, spec 6.4);
2832
+ #: ``dropWithheld`` (some axis's drop was withheld) lets the render
2833
+ #: path's file-only fork decision see a withheld drop without opening
2834
+ #: the index. When exactly one axis is withheld the document also
2835
+ #: carries ``"dropWithheldAxes": [axis]`` so the render reduces each
2836
+ #: axis with its own permission (review finding F1); without it a set
2837
+ #: ``dropWithheld`` withholds both axes (`_withheld_axes`).
2778
2838
  quarantine: "dict | None" = None
2779
2839
  #: #825/#832 spec 4.3 revision 2: the intake acknowledgement checkpoint —
2780
2840
  #: every logged evaluation with a sequence number at or below it has a
@@ -2824,6 +2884,16 @@ class _StaleCache:
2824
2884
  #: limit. They are no longer receipt work for a render (eviction and
2825
2885
  #: omission still mean eligible) until a writer replaces this cache.
2826
2886
  omitted: frozenset = frozenset()
2887
+ #: #1019 (spec 4.5): the compact five-hour provider samples of
2888
+ #: ``five_window`` read after ``samples_after``, so a render can tell
2889
+ #: whether a five-hour reading's drop proof is decided (`_render_drop_work`).
2890
+ five_samples: tuple = ()
2891
+ #: #1019 review finding P1 (and its #797 twin): the sample identities
2892
+ #: whose RISE the completion that wrote this cache refused against the
2893
+ #: receipts it kept. The render reduces with those refusals while the
2894
+ #: index fingerprint is unchanged; without them a refused rise reads as
2895
+ #: an eligible one and every render dispatches the detached reducer.
2896
+ refused: frozenset = frozenset()
2827
2897
 
2828
2898
 
2829
2899
  @dataclasses.dataclass(frozen=True)
@@ -2892,6 +2962,9 @@ def _receipt_document(receipt) -> dict:
2892
2962
 
2893
2963
  _SAMPLE_FIELDS = ("observationId", "requestedAt", "capturedAt", "weeklyPercent",
2894
2964
  "weeklyResetsAt")
2965
+ #: #1019 (spec 4.5): one cached five-hour provider sample.
2966
+ _FIVE_SAMPLE_FIELDS = ("observationId", "requestedAt", "capturedAt", "fiveHourPercent",
2967
+ "fiveHourResetsAt", "fiveHourWindowKey")
2895
2968
 
2896
2969
 
2897
2970
  def _stale_cache_document(cache: _StaleCache) -> dict:
@@ -2907,6 +2980,14 @@ def _stale_cache_document(cache: _StaleCache) -> dict:
2907
2980
  "fiveHourWindow": cache.five_window,
2908
2981
  "fiveHourAccountedPercent": cache.five_accounted_percent,
2909
2982
  "omitted": sorted(cache.omitted),
2983
+ # #1019 (spec 4.5): written only when there are any, so a cache
2984
+ # without five-hour samples keeps its earlier shape.
2985
+ **({} if not cache.five_samples else {"fiveHourSamples": [
2986
+ [s.observation_id, s.requested_at_utc, s.captured_at_utc,
2987
+ s.five_hour_percent, s.five_hour_resets_at_utc, s.five_hour_window_key]
2988
+ for s in cache.five_samples]}),
2989
+ # Written only when there are any, like the five-hour samples.
2990
+ **({} if not cache.refused else {"refusedRises": sorted(cache.refused)}),
2910
2991
  }
2911
2992
 
2912
2993
 
@@ -2921,14 +3002,19 @@ def _validate_stale_cache(value, *, account_key: str):
2921
3002
  base = {"window", "accountedPercent", "floorEpoch", "samplesAfter",
2922
3003
  "samplesCovered", "samples"}
2923
3004
  five_keys = {"fiveHourWindow", "fiveHourAccountedPercent", "omitted"}
2924
- if set(doc) != base and set(doc) != base | five_keys:
3005
+ keys = set(doc)
3006
+ if not (keys == base or base | five_keys <= keys
3007
+ <= base | five_keys | {"fiveHourSamples", "refusedRises"}):
2925
3008
  raise _candidates.StateValidationError("unexpected stale contributor cache keys")
2926
3009
  five_window = doc.get("fiveHourWindow")
2927
3010
  five_percent = doc.get("fiveHourAccountedPercent")
2928
3011
  omitted = doc.get("omitted", [])
3012
+ refused = doc.get("refusedRises", [])
2929
3013
  if (not (five_window is None or _candidates._is_int(five_window))
2930
3014
  or not isinstance(omitted, list)
2931
- or not all(isinstance(k, str) and len(k) == 64 for k in omitted)):
3015
+ or not all(isinstance(k, str) and len(k) == 64 for k in omitted)
3016
+ or not isinstance(refused, list)
3017
+ or not all(isinstance(k, str) and len(k) == 64 for k in refused)):
2932
3018
  raise _candidates.StateValidationError("invalid stale contributor cache")
2933
3019
  if five_percent is not None:
2934
3020
  five_percent = _candidates._percent(five_percent)
@@ -2949,6 +3035,19 @@ def _validate_stale_cache(value, *, account_key: str):
2949
3035
  raise _candidates.StateValidationError("invalid cached provider sample")
2950
3036
  samples.append(_lib_oauth_samples.OAuthSample(
2951
3037
  account_key, row[0], row[1], row[2], float(row[3]), row[4], None, None, None))
3038
+ five_rows = doc.get("fiveHourSamples", [])
3039
+ if not isinstance(five_rows, list) or (five_rows and five_window is None):
3040
+ raise _candidates.StateValidationError("invalid stale contributor cache")
3041
+ five_samples = []
3042
+ for row in five_rows:
3043
+ if (not isinstance(row, list) or len(row) != len(_FIVE_SAMPLE_FIELDS)
3044
+ or not all(isinstance(row[i], str) for i in (0, 1, 2, 4))
3045
+ or isinstance(row[3], bool) or not isinstance(row[3], (int, float))
3046
+ or not _candidates._is_int(row[5]) or int(row[5]) != int(five_window)):
3047
+ raise _candidates.StateValidationError("invalid cached provider sample")
3048
+ five_samples.append(_lib_oauth_samples.OAuthSample(
3049
+ account_key, row[0], row[1], row[2], None, None, float(row[3]), row[4],
3050
+ int(row[5])))
2952
3051
  accounted = None
2953
3052
  if window is not None:
2954
3053
  accounted = _lib_stale_proof.Accounted(
@@ -2956,7 +3055,8 @@ def _validate_stale_cache(value, *, account_key: str):
2956
3055
  support_id=None, support_hash=None, floor_epoch=floor, credits=(),
2957
3056
  coverage=None)
2958
3057
  return _StaleCache(window, accounted, tuple(samples), after, covered,
2959
- five_window, five_percent, frozenset(omitted))
3058
+ five_window, five_percent, frozenset(omitted), tuple(five_samples),
3059
+ frozenset(refused))
2960
3060
 
2961
3061
 
2962
3062
  def _validate_receipts(value, *, account_key: str) -> tuple:
@@ -3145,13 +3245,61 @@ def _validate_transition_quarantine(value):
3145
3245
  if value is None:
3146
3246
  return None
3147
3247
  doc = _candidates._require_object(value, "outcomeQuarantine")
3148
- _candidates._require_exact_keys(doc, {"authorityCommit", "dropWithheld"})
3248
+ _candidates._require_exact_keys(
3249
+ doc, {"authorityCommit", "dropWithheld"}
3250
+ | ({"dropWithheldAxes"} & set(doc)))
3149
3251
  commit = doc["authorityCommit"]
3150
3252
  if commit is not None and (not isinstance(commit, str) or not commit):
3151
3253
  raise _candidates.StateValidationError("invalid authority commit")
3152
3254
  if not isinstance(doc["dropWithheld"], bool):
3153
3255
  raise _candidates.StateValidationError("invalid drop-withheld flag")
3154
- return {"authorityCommit": commit, "dropWithheld": doc["dropWithheld"]}
3256
+ out = {"authorityCommit": commit, "dropWithheld": doc["dropWithheld"]}
3257
+ if "dropWithheldAxes" in doc:
3258
+ axes = doc["dropWithheldAxes"]
3259
+ # #948 review finding F1: present only for a proper, non-empty
3260
+ # subset of the axes, sorted, with the flag set.
3261
+ if (not doc["dropWithheld"] or not isinstance(axes, list)
3262
+ or not axes or len(axes) >= len(_candidates.AXES)
3263
+ or axes != sorted(set(axes))
3264
+ or not set(axes) <= set(_candidates.AXES)):
3265
+ raise _candidates.StateValidationError("invalid drop-withheld axes")
3266
+ out["dropWithheldAxes"] = list(axes)
3267
+ return out
3268
+
3269
+
3270
+ def _withheld_axes(transition) -> frozenset:
3271
+ """The axes whose drop the transition's last detached decision withheld
3272
+ (#948 spec 6.8, review finding F1): none without a set flag, the named
3273
+ axes when the document names them, and both axes for a set flag that
3274
+ names none (the conservative reading of every earlier document)."""
3275
+ doc = None if transition is None else transition.quarantine
3276
+ if not doc or not doc.get("dropWithheld"):
3277
+ return frozenset()
3278
+ axes = doc.get("dropWithheldAxes")
3279
+ return frozenset(_candidates.AXES if axes is None else axes)
3280
+
3281
+
3282
+ def _withheld_permission(withheld: frozenset):
3283
+ """``reduce_candidates``' drop permission for the withheld axes: open
3284
+ when none, closed when all, else per axis."""
3285
+ if not withheld:
3286
+ return True
3287
+ if withheld >= frozenset(_candidates.AXES):
3288
+ return False
3289
+ return {axis: axis not in withheld for axis in _candidates.AXES}
3290
+
3291
+
3292
+ def _quarantine_document(transition, withheld: frozenset) -> dict:
3293
+ """The transition's quarantine document recording ``withheld``: the two
3294
+ keys every reader knows, plus ``dropWithheldAxes`` only when exactly one
3295
+ axis is withheld (both or none is the two-key form)."""
3296
+ doc = {"authorityCommit": (None if transition is None
3297
+ or transition.quarantine is None
3298
+ else transition.quarantine.get("authorityCommit")),
3299
+ "dropWithheld": bool(withheld)}
3300
+ if withheld and not withheld >= frozenset(_candidates.AXES):
3301
+ doc["dropWithheldAxes"] = sorted(withheld)
3302
+ return doc
3155
3303
 
3156
3304
 
3157
3305
  def _write_transition(transition: _Transition) -> None:
@@ -3293,8 +3441,8 @@ def _reconcile_selected_control(
3293
3441
  anchor = _retained_anchor(
3294
3442
  projection, None if existing is None else existing.five_hour_anchor)
3295
3443
  # #825/#832 spec 5.6 item 7: the control transition records the commit of
3296
- # this bound authoritative publication — half of the control recovery
3297
- # certificate (the index holds the journal-ordered other half).
3444
+ # this bound authoritative publication (routine bookkeeping; since #948
3445
+ # it gates no recovery — spec 6.4).
3298
3446
  quarantine = ... if authority_commit is None else {
3299
3447
  "authorityCommit": authority_commit, "dropWithheld": False}
3300
3448
  _write_transition(_replace_transition(
@@ -3528,6 +3676,37 @@ def _authoritative_repair_required(*, now_epoch: int, binding=None) -> bool:
3528
3676
  return False
3529
3677
 
3530
3678
 
3679
+ def _plausible_oauth_axes(args: argparse.Namespace, observed_axes):
3680
+ """#948 spec 6.4: the record kernel's ingress bands, applied before any
3681
+ fence. Returns the observed axes to fence — five-hour dropped (and its
3682
+ fields cleared, exactly as the kernel would drop them) when its reset is
3683
+ out of band — or None when the weekly reset is implausible."""
3684
+ import _cctally_record
3685
+
3686
+ now_dt = _cctally_core._command_as_of()
3687
+ now_check = int(now_dt.timestamp())
3688
+ if not _cctally_record.check_resets_at_plausibility(
3689
+ int(args.resets_at), now_check,
3690
+ past_slack_s=_cctally_record._RECORD_USAGE_WEEK_PAST_SLACK_S,
3691
+ future_band_s=_cctally_record._RECORD_USAGE_WEEK_FUTURE_BAND_S):
3692
+ # The record step's own notice (review finding F16: the rejection
3693
+ # the user sees is unchanged by checking the band first).
3694
+ eprint(_cctally_record._weekly_resets_rejection_notice(
3695
+ int(args.resets_at), now_dt))
3696
+ return None
3697
+ if (getattr(args, "five_hour_resets_at", None) is not None
3698
+ and not _cctally_record.check_resets_at_plausibility(
3699
+ int(args.five_hour_resets_at), now_check,
3700
+ past_slack_s=_cctally_record._RECORD_USAGE_5H_PAST_SLACK_S,
3701
+ future_band_s=_cctally_record._RECORD_USAGE_5H_FUTURE_BAND_S)):
3702
+ eprint(_cctally_record._five_hour_resets_drop_notice(
3703
+ int(args.five_hour_resets_at), now_dt))
3704
+ args.five_hour_resets_at = None
3705
+ args.five_hour_percent = None
3706
+ return tuple(axis for axis in observed_axes if axis != "fiveHour")
3707
+ return observed_axes
3708
+
3709
+
3531
3710
  def _authoritative_record_usage(
3532
3711
  args: argparse.Namespace,
3533
3712
  observed_axes,
@@ -3597,6 +3776,15 @@ def _authoritative_record_usage(
3597
3776
  # here and this publication continues; only an uncertainty op that
3598
3777
  # could not be made durable raises, and then nothing is published.
3599
3778
  _rescan_legacy_spool()
3779
+ # #948 spec 6.4 *Plausibility first*: the OAuth ingress bands the
3780
+ # record kernel applies are checked BEFORE authoritative windows and
3781
+ # fences are installed, so an implausible reading installs no fence
3782
+ # and covers nothing.
3783
+ observed_axes = _plausible_oauth_axes(args, observed_axes)
3784
+ if observed_axes is None:
3785
+ # The record step's exit status, as before the band moved ahead
3786
+ # of the fences (review finding F16): nothing is fenced.
3787
+ return _AuthoritativeRecordResult("record_failed", "exit 2")
3600
3788
  windows = _authoritative_windows(args, binding, now_epoch=now_epoch)
3601
3789
  if "fiveHour" in windows:
3602
3790
  args.five_hour_window_key = windows["fiveHour"]
@@ -3609,7 +3797,8 @@ def _authoritative_record_usage(
3609
3797
  try:
3610
3798
  rc = _cctally().cmd_record_usage(
3611
3799
  args, binding=binding, nudge_dashboard=nudge_dashboard,
3612
- nudge_sink=nudge_sink, appended=appended)
3800
+ nudge_sink=nudge_sink, appended=appended,
3801
+ selected_state_locked=True)
3613
3802
  except _cctally().StatsRebuildDeferred as exc:
3614
3803
  return _AuthoritativeRecordResult("record_failed", str(exc))
3615
3804
  except Exception as exc:
@@ -3629,6 +3818,13 @@ def _authoritative_record_usage(
3629
3818
  observed_axes=observed_axes,
3630
3819
  authority_commit=(appended[-1] if appended else None),
3631
3820
  )
3821
+ if appended:
3822
+ # #948 spec 6.4: the reconciliation has committed, in this same
3823
+ # critical section, so the completion op (appended only when this
3824
+ # observation newly covers a paused axis) proves it.
3825
+ import _cctally_quarantine
3826
+ _cctally_quarantine.run_authority_completion(
3827
+ binding.account_key, appended[-1], appended[-1])
3632
3828
  _cctally()._statusline_observe_touch(binding=binding)
3633
3829
  except Exception as exc:
3634
3830
  # Do not clean up the inflight tombstone on a post-record failure.
@@ -3764,7 +3960,7 @@ def _axis_value_document(value) -> "dict | None":
3764
3960
  def _bound_publication_plan(decision, projection, candidates, binding, *,
3765
3961
  generation: int, now_epoch: int,
3766
3962
  intake_sequence: int,
3767
- refused=None) -> "dict | None":
3963
+ refused=None, drop_refused=None) -> "dict | None":
3768
3964
  """Freeze one partition's combined two-axis publication plan.
3769
3965
 
3770
3966
  Every published axis records its provenance — ``candidate`` (the winning
@@ -3777,15 +3973,24 @@ def _bound_publication_plan(decision, projection, candidates, binding, *,
3777
3973
  same control replacement.
3778
3974
 
3779
3975
  ``refused`` maps an axis to the contributor tokens the reducer refused
3780
- on it (#797 spec 4.4): a candidate axis's provenance names the selected
3781
- ELIGIBLE winner, never a refused member that happens to tie its value.
3976
+ on it (#797 spec 4.4) — every rise-ineligible token, receipt-refused or
3977
+ drop-refused (#1019 P-R2, `_rise_ineligible_tokens`): a candidate axis's
3978
+ provenance names the selected ELIGIBLE winner, never a refused member
3979
+ that happens to tie its value.
3782
3980
  Refusal filters only a rise: an accepted drop (consensus or deadline,
3783
3981
  below the same window's baseline) publishes exactly as today, its
3784
3982
  provenance included (review RV-3), because the reducer selects it from
3785
- the unfiltered set."""
3983
+ the unfiltered set.
3984
+
3985
+ ``drop_refused`` maps an axis to the tokens whose LOW the reducer
3986
+ refused (#1019 spec 4.3): a drop's provenance names the selected
3987
+ DROP-ELIGIBLE winner, never a drop-refused member that ties its value;
3988
+ when every member of that value is drop-refused the drop came from the
3989
+ retained low and its provenance says so."""
3786
3990
  import _cctally_journal
3787
3991
 
3788
3992
  refused = refused or {}
3993
+ drop_refused = drop_refused or {}
3789
3994
  account_key = binding.account_key
3790
3995
  if projection.account_key not in (None, account_key):
3791
3996
  raise ValueError("cross-account publication projection")
@@ -3809,7 +4014,10 @@ def _bound_publication_plan(decision, projection, candidates, binding, *,
3809
4014
  drop = (baseline is not None
3810
4015
  and baseline.canonical_key == value.canonical_key
3811
4016
  and value.percent < baseline.percent)
3812
- excluded = () if drop else refused.get(axis, ())
4017
+ # #1019 (spec 4.3, P-R2): a drop's winner is drop-eligible; a rise's
4018
+ # winner is rise-eligible (``refused`` is the rise-ineligible set).
4019
+ excluded = (drop_refused.get(axis, ()) if drop
4020
+ else refused.get(axis, ()))
3813
4021
  winners = sorted(
3814
4022
  (candidate for candidate in candidates
3815
4023
  if (axis_value := _candidates._candidate_axis(candidate, axis)) is not None
@@ -3830,6 +4038,9 @@ def _bound_publication_plan(decision, projection, candidates, binding, *,
3830
4038
  provenance[axis] = {
3831
4039
  "kind": "retained_low", "account": account_key,
3832
4040
  "window": value.canonical_key, "controlGeneration": generation,
4041
+ # #1019/#1014 (spec 4.3): the supports the retained low is
4042
+ # published from, frozen inside the publication identity.
4043
+ "supports": _retained_supports(decision, axis),
3833
4044
  }
3834
4045
  plan = {
3835
4046
  "capturedAt": now_epoch,
@@ -3859,6 +4070,29 @@ def _bound_publication_plan(decision, projection, candidates, binding, *,
3859
4070
  return plan
3860
4071
 
3861
4072
 
4073
+ def _retained_supports(decision, axis: str) -> list:
4074
+ """The retained observations a ``retained_low`` axis publishes from, as
4075
+ documents sorted by observation id, ties broken by the canonical JSON of
4076
+ the witness (#1019/#1014 spec 4.3). Empty when the decision's control
4077
+ state holds no retained evidence on ``axis``."""
4078
+ pending = (decision.control.pending_drops or {}).get(axis)
4079
+ retained = None if pending is None else pending.retained
4080
+ if retained is None:
4081
+ return []
4082
+ supports = [{
4083
+ "observationId": item.observation_id,
4084
+ "token": item.token,
4085
+ "percent": item.percent,
4086
+ "rawResetsAt": item.raw_resets_at,
4087
+ "receivedAt": item.received_at,
4088
+ "witness": _witness_document(item.witness),
4089
+ } for item in retained.observations]
4090
+ return sorted(supports, key=lambda item: (
4091
+ item["observationId"],
4092
+ json.dumps(item["witness"], sort_keys=True, separators=(",", ":"),
4093
+ allow_nan=False)))
4094
+
4095
+
3862
4096
  def _activity_provenance(candidate: "_candidates.Candidate") -> dict:
3863
4097
  """The selected contributor's frozen provider-activity evidence as plan
3864
4098
  provenance (#797 spec 4.5): its instant (or null), its typed status and
@@ -3892,14 +4126,7 @@ def _axis_members(candidates, axis: str) -> list:
3892
4126
  value = candidate.seven_day if axis == "sevenDay" else candidate.five_hour
3893
4127
  if value is None:
3894
4128
  continue
3895
- if axis == "sevenDay":
3896
- window = int(_cctally_core._normalize_week_boundary_dt(
3897
- dt.datetime.fromtimestamp(value.raw_resets_at, tz=dt.timezone.utc)
3898
- ).timestamp())
3899
- elif value.canonical_key is not None:
3900
- window = int(value.canonical_key)
3901
- else:
3902
- window = int(_cctally()._canonical_5h_window_key(int(value.raw_resets_at)))
4129
+ window = _member_window(value, axis)
3903
4130
  sigma = _lib_stale_proof.sample_identity(
3904
4131
  candidate.token, candidate.activity, axis=axis,
3905
4132
  percent=value.percent, window=window)
@@ -3908,6 +4135,112 @@ def _axis_members(candidates, axis: str) -> list:
3908
4135
  return members
3909
4136
 
3910
4137
 
4138
+ def _member_window(value, axis: str) -> int:
4139
+ """A member's window on ``axis``: the normalized weekly reset, or the
4140
+ canonical five-hour window key (the value's own when it carries one)."""
4141
+ if axis == "sevenDay":
4142
+ return int(_cctally_core._normalize_week_boundary_dt(
4143
+ dt.datetime.fromtimestamp(value.raw_resets_at, tz=dt.timezone.utc)
4144
+ ).timestamp())
4145
+ if value.canonical_key is not None:
4146
+ return int(value.canonical_key)
4147
+ return int(_cctally()._canonical_5h_window_key(int(value.raw_resets_at)))
4148
+
4149
+
4150
+ #: #1019 (spec 4.1-4.5): the axes whose drops the glue refuses. Both: the
4151
+ #: five-hour leg (spec 4.5) reads the samples `_five_hour_stale_inputs`
4152
+ #: reads, in the account-local canonical five-hour window, and each member's
4153
+ #: and support's advertised five-hour reset. Weekly and five-hour
4154
+ #: eligibility are decided independently per member.
4155
+ _DROP_AXES = ("sevenDay", "fiveHour")
4156
+
4157
+
4158
+ def _axis_drop_members(candidates, axis: str) -> list:
4159
+ """The ``axis`` readings of ``candidates`` as `_lib_stale_proof.DropMember`s
4160
+ (#1019 spec 4.1): every member with a usable bound — its frozen
4161
+ supported ``t_m``, else its ``received_at`` second (`drop_bound`). The
4162
+ fallback never becomes a σ: it reaches the drop proof only."""
4163
+ import _lib_stale_proof
4164
+
4165
+ members = []
4166
+ for candidate in candidates:
4167
+ value = _candidates._candidate_axis(candidate, axis)
4168
+ if value is None:
4169
+ continue
4170
+ window = _member_window(value, axis)
4171
+ bound = _lib_stale_proof.drop_bound(
4172
+ candidate.token, candidate.activity, int(candidate.received_at),
4173
+ axis=axis, percent=value.percent, window=window)
4174
+ if bound is not None:
4175
+ members.append(_lib_stale_proof.DropMember(
4176
+ candidate.token, bound, axis, float(value.percent), window,
4177
+ int(value.raw_resets_at)))
4178
+ return members
4179
+
4180
+
4181
+ def _support_drop_member(item, axis: str, window: "int | None" = None):
4182
+ """One retained supporting observation as a `DropMember`: its own frozen
4183
+ witness, else its own ``received_at`` (#1019/#1014 spec 4.2), never a
4184
+ later candidate's. None without a usable bound.
4185
+
4186
+ ``window`` is the support's account-local five-hour key — the pending
4187
+ drop's, or the frozen plan value's (#1019 spec 4.5) — which a raw floor
4188
+ of its reset could miss across a ten-minute boundary; without one the
4189
+ window is derived from the reset (`_member_window`)."""
4190
+ import _lib_stale_proof
4191
+
4192
+ if window is None:
4193
+ window = _member_window(
4194
+ _candidates.AxisValue(item.percent, item.raw_resets_at), axis)
4195
+ bound = _lib_stale_proof.drop_bound(
4196
+ item.token, item.witness, int(item.received_at), axis=axis,
4197
+ percent=item.percent, window=window)
4198
+ if bound is None:
4199
+ return None
4200
+ return _lib_stale_proof.DropMember(item.token, bound, axis, float(item.percent),
4201
+ window, int(item.raw_resets_at))
4202
+
4203
+
4204
+ def _support_window(axis: str, key) -> "int | None":
4205
+ """The window a retained support of ``axis`` is proven in: on the
4206
+ five-hour axis the account-local key its pending drop or frozen plan
4207
+ carries (#1019 spec 4.5); on the weekly axis None, so the normalized
4208
+ reset of the support itself decides (`_member_window`)."""
4209
+ if axis != "fiveHour" or isinstance(key, bool) or not isinstance(key, int):
4210
+ return None
4211
+ return int(key)
4212
+
4213
+
4214
+ def _refuted_supports(stale: "_StaleInputs | None", pending_drops) -> "dict | None":
4215
+ """Per drop axis, the `support_identity` of every retained supporting
4216
+ observation the current samples refute (#1019/#1014 spec 4.2): each one
4217
+ proven stale on its own bound, its contributor present or absent. None
4218
+ when nothing is refuted, so the reducer prunes nothing."""
4219
+ import _lib_stale_proof
4220
+
4221
+ if stale is None or not pending_drops:
4222
+ return None
4223
+ out = {}
4224
+ for axis, window, samples, _credits, _supports in _stale_axes(stale):
4225
+ if axis not in _DROP_AXES or window is None:
4226
+ continue
4227
+ pending = pending_drops.get(axis)
4228
+ if pending is None or pending.retained is None:
4229
+ continue
4230
+ refuted = set()
4231
+ for item in pending.retained.observations:
4232
+ member = _support_drop_member(item, axis, _support_window(
4233
+ axis, pending.canonical_key))
4234
+ if member is not None and _lib_stale_proof.proven_stale_low(
4235
+ member.bound_utc, samples, axis=axis, window=member.window,
4236
+ percent=member.percent,
4237
+ advertised_reset_epoch=member.advertised_reset_epoch) is not None:
4238
+ refuted.add(_candidates.support_identity(item))
4239
+ if refuted:
4240
+ out[axis] = frozenset(refuted)
4241
+ return out or None
4242
+
4243
+
3911
4244
  def _weekly_members(candidates) -> list:
3912
4245
  return _axis_members(candidates, "sevenDay")
3913
4246
 
@@ -3926,24 +4259,33 @@ def _stale_axes(stale: "_StaleInputs") -> tuple:
3926
4259
 
3927
4260
  def _stale_member_facts(stale: "_StaleInputs | None", receipts, candidates):
3928
4261
  """The reducer's per-axis member facts (#797 spec 4.4), or None when no
3929
- member is refused, so such a window reduces exactly as before. Each axis
3930
- is decided on its own window's members, samples, credits and receipts;
3931
- an axis without a refused member is absent (all eligible)."""
4262
+ member is refused in either direction, so such a window reduces exactly
4263
+ as before. Each axis is decided on its own window's members, samples,
4264
+ credits and receipts; an axis without a refused member is absent (all
4265
+ eligible).
4266
+
4267
+ #1019 (spec 4.4): the rise facts still need receipts; the drop facts
4268
+ need none and come from the same validated snapshot, for every member
4269
+ with a usable bound on a drop axis (`_DROP_AXES`)."""
3932
4270
  import _lib_stale_proof
3933
4271
 
3934
- if stale is None or not receipts:
4272
+ if stale is None:
3935
4273
  return None
3936
4274
  out = {}
3937
4275
  for axis, window, samples, credits, supports in _stale_axes(stale):
3938
4276
  if window is None:
3939
4277
  continue
3940
- members = [m for m in _axis_members(candidates, axis) if m.sigma.window == window]
3941
- if not members:
4278
+ members = ([m for m in _axis_members(candidates, axis)
4279
+ if m.sigma.window == window] if receipts else [])
4280
+ drops = (_axis_drop_members(candidates, axis) if axis in _DROP_AXES
4281
+ else [])
4282
+ if not members and not drops:
3942
4283
  continue
3943
4284
  facts = _lib_stale_proof.refusal_facts(
3944
4285
  axis, members, samples, receipts, credits,
3945
- index_identity=stale.index_identity, supports=supports)
3946
- if any(f.refused for f in facts.values()):
4286
+ index_identity=stale.index_identity, supports=supports,
4287
+ drop_members=drops)
4288
+ if any(f.refused or f.drop_refused for f in facts.values()):
3947
4289
  out[axis] = facts
3948
4290
  return out or None
3949
4291
 
@@ -3953,6 +4295,19 @@ def _refused_tokens(member_facts) -> dict:
3953
4295
  for axis, facts in (member_facts or {}).items()}
3954
4296
 
3955
4297
 
4298
+ def _drop_refused_tokens(member_facts) -> dict:
4299
+ """Per axis, the contributor tokens whose LOW the facts refuse (#1019)."""
4300
+ return {axis: _candidates.drop_refused_tokens(facts)
4301
+ for axis, facts in (member_facts or {}).items()}
4302
+
4303
+
4304
+ def _rise_ineligible_tokens(member_facts) -> dict:
4305
+ """Per axis, the contributor tokens that may not RISE: receipt-refused
4306
+ or drop-refused (#1019 spec 4.2 P-R2, `rise_ineligible_tokens`)."""
4307
+ return {axis: _candidates.rise_ineligible_tokens(facts)
4308
+ for axis, facts in (member_facts or {}).items()}
4309
+
4310
+
3956
4311
  def _completion_instant() -> str:
3957
4312
  """``e_σ``: the wall clock at a covered completion, in the samples' ``q``
3958
4313
  form. It is an upper bound on the completion, taken after it; an earlier
@@ -3993,6 +4348,13 @@ def _completion_receipts(transition, classified, members):
3993
4348
  completed_at_utc=completed, index_identity=stale.index_identity))
3994
4349
  receipts = _lib_stale_proof.latest_per_token(
3995
4350
  _lib_stale_proof.merge_receipts(kept, earned))
4351
+ # #1019 review finding P1 (#797 twin): the rises this completion refuses
4352
+ # against the receipts it keeps, for the render to reduce with.
4353
+ facts = _stale_member_facts(stale, receipts, members) or {}
4354
+ refused = frozenset(
4355
+ m.sigma.key() for axis, axis_facts in facts.items()
4356
+ for m in _axis_members(members, axis)
4357
+ if getattr(axis_facts.get(m.sigma.token), "refused", False))
3996
4358
  floor = None if stale.accounted is None else stale.accounted.floor_epoch
3997
4359
  cache = _StaleCache(
3998
4360
  stale.window, stale.accounted,
@@ -4000,7 +4362,11 @@ def _completion_receipts(transition, classified, members):
4000
4362
  stale.samples, window=stale.window, floor_epoch=floor),
4001
4363
  stale.samples_after, stale.samples_covered,
4002
4364
  stale.five_window,
4003
- None if stale.five_accounted is None else stale.five_accounted.percent)
4365
+ None if stale.five_accounted is None else stale.five_accounted.percent,
4366
+ five_samples=() if stale.five_window is None else _lib_stale_proof.compact_samples(
4367
+ stale.five_samples, window=stale.five_window, floor_epoch=None,
4368
+ axis="fiveHour"),
4369
+ refused=refused)
4004
4370
  return receipts, cache
4005
4371
 
4006
4372
 
@@ -4029,9 +4395,11 @@ def _fit_transition(transition: _Transition, *, prior=(), referenced=()) -> _Tra
4029
4395
  # the cache. Its member stays eligible.
4030
4396
  fitted = dataclasses.replace(transition, receipts=tuple(kept))
4031
4397
  if fitted.stale_cache is not None:
4398
+ # A refusal rests on its member's receipt: eviction means eligible.
4399
+ held = {r.sigma.key() for r in kept}
4032
4400
  fitted = dataclasses.replace(fitted, stale_cache=dataclasses.replace(
4033
- fitted.stale_cache,
4034
- omitted=evaluated - {r.sigma.key() for r in kept}))
4401
+ fitted.stale_cache, omitted=evaluated - held,
4402
+ refused=fitted.stale_cache.refused & held))
4035
4403
  return fitted
4036
4404
 
4037
4405
  receipts = _lib_stale_proof.bounded_receipts(
@@ -4113,10 +4481,210 @@ def _receipt_work(transition: _Transition, members) -> bool:
4113
4481
  return False
4114
4482
 
4115
4483
 
4484
+ @dataclasses.dataclass(frozen=True)
4485
+ class _DropWork:
4486
+ """The proof work a partition's detached pass may meet (#1019 spec 4.4,
4487
+ P-R3, P-R4): the ``(axis, window, percent, received_at)`` readings of its
4488
+ logged and current members on the drop axes, whether a pending drop
4489
+ holds retained support, and whether an unappended frozen plan publishes
4490
+ a candidate or retained reading on a drop axis.
4491
+
4492
+ Drop-proof work is a reading below the same window's baseline, a pending
4493
+ drop with retained support or an unappended drop plan; rise-proof work
4494
+ is a reading above that baseline, a reading received at or after the
4495
+ floor of a floor-retired window (a potential first post-floor
4496
+ publication) or an unappended rise plan — a rise's eligibility depends
4497
+ on drop facts (P-R2). With either, an uncovered sample projection makes
4498
+ the classification unavailable (`_read_stale_inputs`), never "no
4499
+ proof". Each axis is judged against its own window, baseline and floor
4500
+ (#1019 spec 4.5: ``axis`` names it; the weekly axis by default)."""
4501
+
4502
+ readings: frozenset = frozenset()
4503
+ retained: bool = False
4504
+ plan: bool = False
4505
+
4506
+ def needs_samples(self, window: "int | None", projection,
4507
+ floor_epoch: "int | None" = None, *,
4508
+ axis: str = "sevenDay") -> bool:
4509
+ if self.retained or self.plan:
4510
+ return True
4511
+ if window is None:
4512
+ return False
4513
+ for reading_axis, w, percent, received_at in self.readings:
4514
+ if reading_axis != axis or int(w) != int(window):
4515
+ continue
4516
+ if projection is not None:
4517
+ if float(percent) != float(projection.percent):
4518
+ return True
4519
+ elif floor_epoch is not None and int(received_at) >= int(floor_epoch):
4520
+ return True
4521
+ return False
4522
+
4523
+
4524
+ #: Every drop-proof work there could be: the error path's scope.
4525
+ _ALL_DROP_WORK = _DropWork(retained=True, plan=True)
4526
+ #: The earliest request instant, so every sample of a window is read.
4527
+ _EVERY_SAMPLE = "1970-01-01T00:00:00.000000Z"
4528
+
4529
+
4530
+ def _plan_supports(source) -> "list | None":
4531
+ """A frozen ``retained_low`` provenance's supports as
4532
+ `SupportingObservation`s (#1019/#1014 spec 4.3), or None when they are
4533
+ missing or malformed (the plan then cannot be revalidated)."""
4534
+ supports = source.get("supports") if isinstance(source, dict) else None
4535
+ if not isinstance(supports, list) or not supports:
4536
+ return None
4537
+ out = []
4538
+ try:
4539
+ for doc in supports:
4540
+ out.append(_candidates._supporting_observation(
4541
+ doc, now_epoch=2 ** 62))
4542
+ except (_candidates.StateValidationError, TypeError, KeyError):
4543
+ return None
4544
+ return out
4545
+
4546
+
4547
+ def _plan_frozen_drops(plan: dict) -> tuple:
4548
+ """The axes a frozen plan publishes as a DROP (#1019/#1014 spec 4.3),
4549
+ decided on the plan's own frozen evidence — its ``retained_low``
4550
+ provenance, or a ``candidate`` value below the baseline of the same
4551
+ window in the control projection frozen with it — never on the current
4552
+ baseline, which could classify a formerly selected drop differently."""
4553
+ out = []
4554
+ provenance = plan.get("provenance") or {}
4555
+ frozen = ((plan.get("control") or {}).get("dbProjection") or {})
4556
+ for axis in _candidates.AXES:
4557
+ value, source = plan.get(axis), provenance.get(axis)
4558
+ if value is None or not isinstance(source, dict):
4559
+ continue
4560
+ if source.get("kind") == "retained_low":
4561
+ out.append(axis)
4562
+ elif source.get("kind") == "candidate":
4563
+ base = frozen.get(axis)
4564
+ if (isinstance(base, dict) and base.get("canonicalKey") == value.get(
4565
+ "canonicalKey") and float(value["percent"]) < float(base["percent"])):
4566
+ out.append(axis)
4567
+ return tuple(out)
4568
+
4569
+
4570
+ def _cached_drop_proof(cache, member) -> tuple:
4571
+ """``(decided, proof)`` for one drop member against the render cache
4572
+ (#1019 spec 4.4, review finding P1): decided when the cache is covered,
4573
+ holds the member's own window on its axis and every sample requested
4574
+ after its bound; ``proof`` the cached sample proving its low stale, or
4575
+ None. `compact_samples` keeps every `proven_stale_low` answer, so a
4576
+ decided proof is the one the detached reducer reaches on the same index."""
4577
+ import _lib_stale_proof
4578
+
4579
+ if cache is None or not cache.samples_covered:
4580
+ return False, None
4581
+ window, samples = ((cache.window, cache.samples) if member.axis == "sevenDay"
4582
+ else (cache.five_window, cache.five_samples))
4583
+ if window is None or member.window != window:
4584
+ return False, None
4585
+ bound = _lib_stale_proof.floor_microsecond(member.bound_utc)
4586
+ if cache.samples_after is None or bound is None or cache.samples_after > bound:
4587
+ return False, None
4588
+ return True, _lib_stale_proof.proven_stale_low(
4589
+ member.bound_utc, samples, axis=member.axis, window=member.window,
4590
+ percent=member.percent, advertised_reset_epoch=member.advertised_reset_epoch)
4591
+
4592
+
4593
+ def _render_member_facts(transition: "_Transition | None", members) -> "dict | None":
4594
+ """The refusal facts the transition's render cache decides for
4595
+ ``members`` (#1019 review finding P1 and its #797 twin), per axis: the
4596
+ rises the completion that wrote the cache refused (``refused``, by exact
4597
+ sample identity), and the lows a cached sample proves stale, refused
4598
+ exactly as the detached reducer refuses them. The render reduces only
4599
+ against an unchanged index fingerprint, so the cache answers for the
4600
+ index the reducer would read. Without them a refused reading looks
4601
+ eligible in every render's reduction, and every render dispatches a
4602
+ detached pass that refuses it again. A member the cache cannot decide
4603
+ gets no fact (eligible) and stays the detached reducer's work
4604
+ (`_render_drop_work`, `_receipt_work`). None when nothing is refused."""
4605
+ import _lib_stale_proof
4606
+
4607
+ cache = None if transition is None else transition.stale_cache
4608
+ if cache is None:
4609
+ return None
4610
+ out = {}
4611
+ for axis in _candidates.AXES:
4612
+ facts = {}
4613
+ if cache.refused:
4614
+ for member in _axis_members(members, axis):
4615
+ if member.sigma.key() in cache.refused:
4616
+ facts[member.sigma.token] = dataclasses.replace(
4617
+ _lib_stale_proof.ALL_ELIGIBLE, refused=True, receipt_valid=True)
4618
+ if axis in _DROP_AXES:
4619
+ for member in _axis_drop_members(members, axis):
4620
+ decided, proof = _cached_drop_proof(cache, member)
4621
+ if decided and proof is not None:
4622
+ facts[member.token] = dataclasses.replace(
4623
+ facts.get(member.token, _lib_stale_proof.ALL_ELIGIBLE),
4624
+ drop_refused=True,
4625
+ drop_proof_observation_id=proof.observation_id)
4626
+ if facts:
4627
+ out[axis] = facts
4628
+ return out or None
4629
+
4630
+
4631
+ def _render_drop_work(transition: _Transition, members) -> bool:
4632
+ """Whether a NOOP the render would acknowledge leaves proof work, drop
4633
+ or rise, that the transition's cached facts cannot decide (#1019 spec
4634
+ 4.4, P-R3, P-R4), judged from those files alone (C4: the render reads no
4635
+ index): a pending drop's retained support, a member reading below or
4636
+ above the same window's baseline, or a member received at or after the
4637
+ floor of a floor-retired window, with no covered cache of the samples
4638
+ after its bound. A retained support the cached samples prove stale is
4639
+ work too: the detached reducer prunes it from the pending drop. A member
4640
+ they prove stale is not: the render already reduced with that refusal
4641
+ (`_render_member_facts`, review finding P1). Such work is the detached
4642
+ reducer's: the render neither acknowledges nor swallows it. Each axis is
4643
+ judged on its own cached window and samples (#1019 spec 4.5: the
4644
+ five-hour ones beside the weekly ones)."""
4645
+ projection = transition.control.db_projection
4646
+ supports, active = [], []
4647
+ for axis in _DROP_AXES:
4648
+ pending = transition.control.pending_drops.get(axis)
4649
+ if pending is not None and _candidates._has_evidence(pending.retained):
4650
+ key = _support_window(axis, pending.canonical_key)
4651
+ supports.extend(m for m in (_support_drop_member(item, axis, key)
4652
+ for item in pending.retained.observations)
4653
+ if m is not None)
4654
+ db_axis = _candidates._projection_axis(projection, axis)
4655
+ if db_axis is not None:
4656
+ active.extend(d for d in _axis_drop_members(members, axis)
4657
+ if d.window == db_axis.canonical_key
4658
+ and d.percent != db_axis.percent)
4659
+ else:
4660
+ evidence = _candidates.axis_evidence(projection, axis)
4661
+ if (evidence is not None
4662
+ and evidence.kind == _candidates.EVIDENCE_FLOOR_RETIRED
4663
+ and evidence.floor_epoch is not None):
4664
+ for candidate in members:
4665
+ if int(candidate.received_at) < int(evidence.floor_epoch):
4666
+ continue
4667
+ active.extend(d for d in _axis_drop_members((candidate,), axis)
4668
+ if d.window == int(evidence.group_key))
4669
+ cache = transition.stale_cache
4670
+ for member in supports:
4671
+ decided, proof = _cached_drop_proof(cache, member)
4672
+ if not decided or proof is not None:
4673
+ return True
4674
+ for member in active:
4675
+ decided, _proof = _cached_drop_proof(cache, member)
4676
+ if not decided:
4677
+ return True
4678
+ return False
4679
+
4680
+
4116
4681
  def _stale_scope(account_key: str, candidates, transition) -> tuple:
4117
- """``(samples_after, receipts_held)`` for one partition's detached work
4118
- (`_STALE_SCOPE`): the earliest instant any member of its logged and
4119
- current evaluations, or any receipt it holds, needs a sample after."""
4682
+ """``(samples_after, receipts_held, drop_work)`` for one partition's
4683
+ detached work (`_STALE_SCOPE`): the earliest instant any member of its
4684
+ logged and current evaluations, any receipt it holds, any retained
4685
+ supporter of its pending drops or any dependency of its frozen plan
4686
+ needs a sample after (#1019 spec 4.4: a member's drop bound, its σ or
4687
+ its ``received_at``), and the drop-proof work it may meet (`_DropWork`)."""
4120
4688
  import _lib_stale_proof
4121
4689
 
4122
4690
  instants = [r.sigma.activity_at_utc
@@ -4127,12 +4695,47 @@ def _stale_scope(account_key: str, candidates, transition) -> tuple:
4127
4695
  account_key, after=_acknowledged_through(transition)))
4128
4696
  except (OSError, ValueError, TypeError, KeyError):
4129
4697
  pass
4698
+ readings = set()
4130
4699
  for members in sets:
4131
4700
  instants.extend(m.sigma.activity_at_utc for m in _all_members(members))
4701
+ for axis in _DROP_AXES:
4702
+ for candidate in members:
4703
+ for d in _axis_drop_members((candidate,), axis):
4704
+ instants.append(d.bound_utc)
4705
+ readings.add((axis, d.window, d.percent,
4706
+ int(candidate.received_at)))
4707
+ retained = plan = False
4708
+ if transition is not None:
4709
+ for axis in _DROP_AXES:
4710
+ pending = transition.control.pending_drops.get(axis)
4711
+ if pending is None or not _candidates._has_evidence(pending.retained):
4712
+ continue
4713
+ retained = True
4714
+ for item in pending.retained.observations:
4715
+ member = _support_drop_member(item, axis)
4716
+ if member is not None:
4717
+ instants.append(member.bound_utc)
4718
+ if transition.publication is not None:
4719
+ frozen = [a for a in _plan_frozen_drops(transition.publication)
4720
+ if a in _DROP_AXES]
4721
+ provenance = transition.publication.get("provenance") or {}
4722
+ # Drop or rise: every unappended plan whose drop axis publishes a
4723
+ # candidate or retained reading is proof work (P-R3).
4724
+ plan = bool(frozen) or any(
4725
+ isinstance(provenance.get(axis), dict)
4726
+ and provenance[axis].get("kind") == "candidate"
4727
+ and transition.publication.get(axis) is not None
4728
+ for axis in _DROP_AXES)
4729
+ for axis in frozen:
4730
+ for item in _plan_supports(provenance.get(axis)) or ():
4731
+ member = _support_drop_member(item, axis)
4732
+ if member is not None:
4733
+ instants.append(member.bound_utc)
4132
4734
  bounds = [b for b in (_lib_stale_proof.floor_microsecond(i) for i in instants)
4133
4735
  if b is not None]
4134
4736
  return (min(bounds) if bounds else None,
4135
- bool(transition is not None and transition.receipts))
4737
+ bool(transition is not None and transition.receipts),
4738
+ _DropWork(frozenset(readings), retained, plan))
4136
4739
 
4137
4740
 
4138
4741
  def _plan_members(account_key: str, plan: dict) -> tuple:
@@ -4151,15 +4754,154 @@ def _plan_members(account_key: str, plan: dict) -> tuple:
4151
4754
 
4152
4755
  def _plan_winner_refused(plan: dict, member_facts) -> bool:
4153
4756
  """Whether a frozen plan's candidate provenance names a member the
4154
- current facts refuse on that axis (#797 spec 4.4, T4)."""
4155
- refused = _refused_tokens(member_facts)
4757
+ current facts refuse on that axis (#797 spec 4.4, T4). #1019 (spec 4.3,
4758
+ P-R2): a winner that became drop-refused is refused for its rise too
4759
+ (`_rise_ineligible_tokens`); a drop's winner is decided by
4760
+ `_plan_drop_dependencies` on its bound evidence."""
4761
+ receipt_refused = _refused_tokens(member_facts)
4762
+ rise_refused = _rise_ineligible_tokens(member_facts)
4763
+ drops = _plan_frozen_drops(plan)
4156
4764
  for axis, source in (plan.get("provenance") or {}).items():
4765
+ refused = receipt_refused if axis in drops else rise_refused
4157
4766
  if (isinstance(source, dict) and source.get("kind") == "candidate"
4158
4767
  and source.get("token") in refused.get(axis, ())):
4159
4768
  return True
4160
4769
  return False
4161
4770
 
4162
4771
 
4772
+ #: `_plan_drop_dependencies` answers.
4773
+ _DROP_SUPPORTED = "supported"
4774
+ _DROP_REFUSED = "refused"
4775
+ _DROP_DEFER = "defer"
4776
+ _DROP_EVIDENCE_REASON = "drop dependency evidence unavailable"
4777
+
4778
+
4779
+ def _plan_drop_facts(account_key: str, plan: dict, stale) -> "dict | None":
4780
+ """The current drop facts of a frozen plan's ``candidate`` winners on
4781
+ the drop axes, drop and rise alike (#1019/#1014 spec 4.3, P-R3: a
4782
+ rise's eligibility needs the same drop facts), per drop axis, keyed by
4783
+ token — each computed only for the plan's own evaluation member BOUND
4784
+ to the frozen provenance: the same token, the same observation identity
4785
+ (account partition, axis, window and value) and the same activity
4786
+ witness fields. A winner no such member backs is absent (its evidence
4787
+ is missing). None when the stale inputs are unavailable."""
4788
+ import _lib_stale_proof
4789
+
4790
+ if stale is None:
4791
+ return None
4792
+ provenance = plan.get("provenance") or {}
4793
+ members = None
4794
+ out: dict = {}
4795
+ for axis, window, samples, _credits, _supports in _stale_axes(stale):
4796
+ if axis not in _DROP_AXES:
4797
+ continue
4798
+ out[axis] = {}
4799
+ source = provenance.get(axis)
4800
+ if not isinstance(source, dict) or source.get("kind") != "candidate":
4801
+ continue
4802
+ if members is None:
4803
+ members = _plan_members(account_key, plan)
4804
+ bound = []
4805
+ for member in members:
4806
+ value = _candidates._candidate_axis(member, axis)
4807
+ if (member.token != source.get("token") or value is None
4808
+ or _candidates.observation_identity(member.token, axis, value)
4809
+ != source.get("observationIdentity")):
4810
+ continue
4811
+ try:
4812
+ activity = _activity_provenance(member)
4813
+ except ValueError:
4814
+ continue
4815
+ if all(source.get(k) == v for k, v in activity.items()):
4816
+ bound.append(member)
4817
+ if len(bound) != 1:
4818
+ continue
4819
+ facts = _lib_stale_proof.refusal_facts(
4820
+ axis, [], samples if window is not None else (), (), (),
4821
+ index_identity=stale.index_identity,
4822
+ drop_members=_axis_drop_members(bound, axis))
4823
+ if bound[0].token in facts:
4824
+ out[axis][bound[0].token] = facts[bound[0].token]
4825
+ return out
4826
+
4827
+
4828
+ def _plan_refuted_supports(plan: dict, stale) -> "dict | None":
4829
+ """Per drop axis, the `support_identity` of every frozen ``retained_low``
4830
+ support the current samples refute (#1019/#1014 spec 4.3), read from the
4831
+ plan itself. None when the stale inputs are unavailable."""
4832
+ import _lib_stale_proof
4833
+
4834
+ if stale is None:
4835
+ return None
4836
+ provenance = plan.get("provenance") or {}
4837
+ out: dict = {}
4838
+ for axis, window, samples, _credits, _supports in _stale_axes(stale):
4839
+ if axis not in _DROP_AXES:
4840
+ continue
4841
+ refuted = set()
4842
+ frozen = plan.get(axis)
4843
+ key = _support_window(
4844
+ axis, frozen.get("canonicalKey") if isinstance(frozen, dict) else None)
4845
+ for item in _plan_supports(provenance.get(axis)) or ():
4846
+ member = _support_drop_member(item, axis, key)
4847
+ if (member is not None and window is not None
4848
+ and _lib_stale_proof.proven_stale_low(
4849
+ member.bound_utc, samples, axis=axis, window=member.window,
4850
+ percent=member.percent,
4851
+ advertised_reset_epoch=member.advertised_reset_epoch)
4852
+ is not None):
4853
+ refuted.add(_candidates.support_identity(item))
4854
+ out[axis] = frozenset(refuted)
4855
+ return out
4856
+
4857
+
4858
+ def _plan_drop_dependencies(plan: dict, facts, refuted_supports) -> str:
4859
+ """Revalidate an unappended frozen plan's drop dependencies (#1019/#1014
4860
+ spec 4.3), beside `_plan_winner_refused`: ``"refused"`` when a
4861
+ ``candidate`` winner's bound member is drop-refused — a drop's, or a
4862
+ rise's (P-R2: a drop-refused member is rise-ineligible) — or every
4863
+ frozen support of a ``retained_low`` drop is refuted; ``"defer"`` when
4864
+ the evidence to decide either is missing (never read as "no proof",
4865
+ P-R3); else ``"supported"``. A ``retained_low`` stays supported while
4866
+ one frozen support remains eligible.
4867
+
4868
+ ``facts`` (`_plan_drop_facts`) and ``refuted_supports``
4869
+ (`_plan_refuted_supports`) name the axes they decide; an axis they
4870
+ leave out is not decided here (all eligible), and None for either is
4871
+ missing evidence."""
4872
+ verdict = _DROP_SUPPORTED
4873
+ provenance = plan.get("provenance") or {}
4874
+ drops = _plan_frozen_drops(plan)
4875
+ for axis in _candidates.AXES:
4876
+ source = provenance.get(axis)
4877
+ if (plan.get(axis) is None or not isinstance(source, dict)
4878
+ or source.get("kind") not in ("candidate", "retained_low")
4879
+ or (source.get("kind") == "retained_low" and axis not in drops)):
4880
+ continue
4881
+ if source.get("kind") == "candidate":
4882
+ if facts is None:
4883
+ return _DROP_DEFER
4884
+ if axis not in facts:
4885
+ continue
4886
+ fact = facts[axis].get(source.get("token"))
4887
+ if fact is None:
4888
+ return _DROP_DEFER
4889
+ if fact.drop_refused:
4890
+ verdict = _DROP_REFUSED
4891
+ else:
4892
+ if refuted_supports is None:
4893
+ return _DROP_DEFER
4894
+ if axis not in refuted_supports:
4895
+ continue
4896
+ supports = _plan_supports(source)
4897
+ if supports is None:
4898
+ return _DROP_DEFER
4899
+ if all(_candidates.support_identity(item) in refuted_supports[axis]
4900
+ for item in supports):
4901
+ verdict = _DROP_REFUSED
4902
+ return verdict
4903
+
4904
+
4163
4905
  def _plan_record_args(plan: dict) -> argparse.Namespace:
4164
4906
  five = plan["fiveHour"]
4165
4907
  return _record_args(
@@ -4662,6 +5404,9 @@ def _publish_plan(transition, plan: dict, binding, classified: "_Classified", *,
4662
5404
  rc = _cctally().cmd_record_usage(
4663
5405
  _plan_record_args(plan), binding=binding, publication=plan,
4664
5406
  ingest_mode="opportunistic", nudge_sink=nudge_sink,
5407
+ # #948 spec 6.7: the reducer pass holds the selected-state lock,
5408
+ # so the release protocol runs inside its critical section.
5409
+ selected_state_locked=True,
4665
5410
  **_bound_append_kwargs(classified.high_water))
4666
5411
  except _cctally_journal.JournalError as exc:
4667
5412
  return _Resolution(transition, None, "deferred", _frontier_conflict(
@@ -4733,7 +5478,18 @@ def _resolve_frozen_publication(transition: _Transition, *, now_epoch: int, nudg
4733
5478
  # evaluation, still unacknowledged, is reduced again with those facts.
4734
5479
  facts = _stale_member_facts(classified.stale, transition.receipts,
4735
5480
  _plan_members(account_key, plan))
4736
- if _plan_winner_refused(plan, facts):
5481
+ # #1019/#1014 spec 4.3: a drop the plan froze is revalidated against
5482
+ # current drop facts bound to its frozen evidence — a refused candidate
5483
+ # or exhausted retained support supersedes the WHOLE unappended plan
5484
+ # (its evaluation is reduced again, the other axis preserved); missing
5485
+ # evidence keeps it frozen under its identity.
5486
+ dependencies = _plan_drop_dependencies(
5487
+ plan, _plan_drop_facts(account_key, plan, classified.stale),
5488
+ _plan_refuted_supports(plan, classified.stale))
5489
+ if dependencies == _DROP_DEFER:
5490
+ return _Resolution(transition, classified, "deferred", AccountResult(
5491
+ account_key, "deferred", None, _DROP_EVIDENCE_REASON))
5492
+ if _plan_winner_refused(plan, facts) or dependencies == _DROP_REFUSED:
4737
5493
  dropped = _replace_transition(
4738
5494
  transition, account_key=account_key, binding=transition.binding,
4739
5495
  projection=before, publication=None)
@@ -4766,18 +5522,22 @@ def _resolve_frozen_publication(transition: _Transition, *, now_epoch: int, nudg
4766
5522
  return _Resolution(transition, classified, "deferred", AccountResult(
4767
5523
  account_key, "deferred", None, _BASELINE_UNKNOWN_REASON,
4768
5524
  retry_at=_evidence_retry_at(classified)))
4769
- if authority is not None and not _drop_permitted(classified, transition, frontier):
5525
+ if authority is not None and any(
5526
+ not _drop_permitted(classified, transition, frontier, axis)
5527
+ for axis in _plan_drop_axes(plan, classified.projection)):
4770
5528
  # #825/#832 spec 5.6 item 7: a plan frozen before a trigger must not
4771
5529
  # publish a withheld drop after it. Dropped durably without spending
4772
5530
  # an attempt; the pending drop itself stays in the control state.
5531
+ # #948 spec 6.8: only when an axis the plan drops on is withheld —
5532
+ # a drop on a released axis publishes.
4773
5533
  withheld = _replace_transition(
4774
5534
  transition, account_key=account_key, binding=transition.binding,
4775
5535
  projection=before, publication=None,
4776
- quarantine={
4777
- "authorityCommit": (
4778
- None if transition.quarantine is None
4779
- else transition.quarantine.get("authorityCommit")),
4780
- "dropWithheld": True})
5536
+ quarantine=_quarantine_document(transition, _withheld_axes(
5537
+ transition) | frozenset(
5538
+ axis for axis in _plan_drop_axes(plan, classified.projection)
5539
+ if not _drop_permitted(classified, transition, frontier,
5540
+ axis))))
4781
5541
  _write_transition(withheld)
4782
5542
  return _Resolution(withheld, classified, "withheld")
4783
5543
  supersession = _publication_supersession(
@@ -4857,6 +5617,21 @@ def _plan_reading_after_floor(plan: dict, axis: str, evidence, receipts) -> "boo
4857
5617
  for key, raw, percent, received in loaded[axis])
4858
5618
 
4859
5619
 
5620
+ def _plan_drop_axes(plan: dict, projection: "_candidates.DbProjection") -> tuple:
5621
+ """The axes a frozen plan publishes BELOW the classified database
5622
+ baseline of the same window (#948 spec 6.8: the drop gate is consulted
5623
+ for exactly these)."""
5624
+ axes = []
5625
+ for axis in _candidates.AXES:
5626
+ value = plan.get(axis)
5627
+ db_axis = _candidates._projection_axis(projection, axis)
5628
+ if (value is not None and db_axis is not None
5629
+ and db_axis.canonical_key == value["canonicalKey"]
5630
+ and float(value["percent"]) < db_axis.percent):
5631
+ axes.append(axis)
5632
+ return tuple(axes)
5633
+
5634
+
4860
5635
  def _plan_drop_authority(plan: dict, projection: "_candidates.DbProjection", *,
4861
5636
  receipts=None) -> "str | None":
4862
5637
  """Whether a frozen plan needs drop authority, revalidated against the
@@ -4980,7 +5755,8 @@ def _later_reading_fence(axis: str, tombstone, db_axis, *, at: int,
4980
5755
 
4981
5756
  def _reduce_partition(account_key, candidates, transition, *, now_epoch, projection,
4982
5757
  drop_consensus_allowed=True, fence_epoch=None,
4983
- same_second_later=frozenset(), member_facts=None):
5758
+ same_second_later=frozenset(), member_facts=None,
5759
+ refuted_supports=None):
4984
5760
  """One partition's pure transition: (decision, anchor, canonical
4985
5761
  candidates, tombstones). Only this account's candidates, control,
4986
5762
  anchor and fences are inputs. ``now_epoch`` is the reduction instant (a
@@ -4990,8 +5766,11 @@ def _reduce_partition(account_key, candidates, transition, *, now_epoch, project
4990
5766
  projected reading shares the instant and was accepted after this
4991
5767
  evaluation (`_later_reading_fence`); only the detached reducer, which
4992
5768
  can read the journal, ever supplies it. ``member_facts`` are the
4993
- per-axis refusal facts (#797 spec 4.4); only the detached reducer, which
4994
- reads the provider samples, ever supplies them."""
5769
+ per-axis refusal facts (#797 spec 4.4) and ``refuted_supports`` the
5770
+ refuted retained supports (#1019/#1014 spec 4.2); only the detached
5771
+ reducer, which reads the provider samples, supplies them in full. A
5772
+ render supplies just the refusals its cache decides
5773
+ (`_render_member_facts`, #1019 review finding P1)."""
4995
5774
  control = (
4996
5775
  _candidates.ControlState(projection, transition.control.pending_drops)
4997
5776
  if transition is not None else _empty_control(projection)
@@ -5012,7 +5791,8 @@ def _reduce_partition(account_key, candidates, transition, *, now_epoch, project
5012
5791
  decision = _candidates.reduce_candidates(
5013
5792
  active, db=projection, control=control, tombstones=tombstones,
5014
5793
  now_epoch=now_epoch, account_key=account_key,
5015
- drop_consensus_allowed=drop_consensus_allowed, member_facts=member_facts)
5794
+ drop_consensus_allowed=drop_consensus_allowed, member_facts=member_facts,
5795
+ refuted_supports=refuted_supports)
5016
5796
  return decision, anchor, active, tombstones
5017
5797
 
5018
5798
 
@@ -5437,20 +6217,30 @@ def _rebuild_unclassifiable() -> _Unclassifiable:
5437
6217
 
5438
6218
 
5439
6219
  def _drop_permitted(classified: _Classified, transition,
5440
- frontier: "_PublicationFrontier | None") -> bool:
5441
- """The control gate of the outcome quarantine (#825/#832 spec 5.6 item
5442
- 7). Consulted only when a reduction would publish a drop below the
5443
- classified baseline. A `legacy_spool_activity` op this pass appended
5444
- withholds outright. Otherwise allowed when no trigger exists, or when the
5445
- account holds BOTH certificates and its control transition records the
5446
- commit of the very publication that witnesses the control certificate."""
5447
- import _cctally_quarantine
5448
-
6220
+ frontier: "_PublicationFrontier | None", axis: str) -> bool:
6221
+ """The per-axis drop gate of the outcome quarantine (#948 spec 6.8).
6222
+ Consulted only when a reduction would publish a drop below the
6223
+ classified baseline on ``axis``. Withheld when the pass appended a
6224
+ trigger (``frontier.local_activity``) or ``axis`` of the account is
6225
+ paused; allowed when no trigger exists or the axis is released in the
6226
+ current generation. Release state is the fold's, read from the same
6227
+ validated index snapshot (covering the journal through ``frontier``)
6228
+ that the classification used — never from selected state, so
6229
+ ``transition`` gates nothing (spec 6.4: authority is journal-only)."""
6230
+ del transition
5449
6231
  if frontier is None or frontier.local_activity:
5450
6232
  return False
5451
- commit = (None if transition is None or transition.quarantine is None
5452
- else transition.quarantine.get("authorityCommit"))
5453
- return _cctally_quarantine.control_recovered(classified.status, commit)
6233
+ status = classified.status if isinstance(classified.status, dict) else {}
6234
+ axis_status = (status.get("axes") or {}).get(axis)
6235
+ return bool(isinstance(axis_status, dict)
6236
+ and axis_status.get("allowed") is True)
6237
+
6238
+
6239
+ def _axis_view(decision, axis: str):
6240
+ """What ``decision`` does on ``axis``: the candidate it publishes there
6241
+ and the pending drop it leaves (#948 spec 6.8 compares per axis)."""
6242
+ return (_plan_axis(decision.plan, axis),
6243
+ decision.control.pending_drops.get(axis))
5454
6244
 
5455
6245
 
5456
6246
  def _bound_append_kwargs(bound) -> dict:
@@ -5460,34 +6250,44 @@ def _bound_append_kwargs(bound) -> dict:
5460
6250
 
5461
6251
  def _gated_reduction(account_key, candidates, transition, *, eval_epoch,
5462
6252
  fence_epoch, classified: _Classified, frontier=None,
5463
- same_second_later=frozenset(), member_facts=None):
6253
+ same_second_later=frozenset(), member_facts=None,
6254
+ refuted_supports=None):
5464
6255
  """Reduce once with drop consensus open and once closed, against the
5465
- classified projection. Only when they differ — a drop below the baseline
5466
- would publish — is the quarantine gate consulted, so the ordinary tick
5467
- pays nothing for it. Returns the reduction, whether a drop was withheld,
5468
- and the axes whose baseline that withholding leaves unestablished
6256
+ classified projection. Only an axis on which they differ — a drop below
6257
+ the baseline would publish there — consults that axis's quarantine gate,
6258
+ so the ordinary tick pays nothing for it (#948 spec 6.8: per axis, the
6259
+ opened reduction where allowed, the closed one where withheld). Returns
6260
+ the reduction, the frozenset of axes whose drop was withheld (empty when
6261
+ none), and the axes whose baseline that withholding leaves unestablished
5469
6262
  (`_unestablished_axes`)."""
5470
- opened = _reduce_partition(account_key, candidates, transition,
5471
- now_epoch=eval_epoch, fence_epoch=fence_epoch,
5472
- projection=classified.projection,
5473
- same_second_later=same_second_later,
5474
- member_facts=member_facts)
5475
- closed = _reduce_partition(account_key, candidates, transition,
5476
- now_epoch=eval_epoch, fence_epoch=fence_epoch,
5477
- projection=classified.projection,
5478
- drop_consensus_allowed=False,
5479
- same_second_later=same_second_later,
5480
- member_facts=member_facts)
6263
+ def reduce(allowed):
6264
+ return _reduce_partition(account_key, candidates, transition,
6265
+ now_epoch=eval_epoch, fence_epoch=fence_epoch,
6266
+ projection=classified.projection,
6267
+ drop_consensus_allowed=allowed,
6268
+ same_second_later=same_second_later,
6269
+ member_facts=member_facts,
6270
+ refuted_supports=refuted_supports)
6271
+
6272
+ opened = reduce(True)
6273
+ closed = reduce(False)
5481
6274
  # Fix round K13 (review cycle 7 finding P1-2): unknown classification
5482
6275
  # defers the whole evaluation whatever the gate says — an open gate is
5483
6276
  # no classification evidence (spec 4.3 *Classification evidence*).
5484
6277
  unknown = opened[0].unknown_axes | closed[0].unknown_axes
5485
6278
  if opened[0] == closed[0]:
5486
- return opened, False, unknown
5487
- if _drop_permitted(classified, transition, frontier):
5488
- return opened, False, unknown
5489
- return closed, True, _unestablished_axes(
5490
- opened[0], closed[0], classified.projection)
6279
+ return opened, frozenset(), unknown
6280
+ withheld = frozenset(
6281
+ axis for axis in _candidates.AXES
6282
+ if _axis_view(opened[0], axis) != _axis_view(closed[0], axis)
6283
+ and not _drop_permitted(classified, transition, frontier, axis))
6284
+ if not withheld:
6285
+ return opened, withheld, unknown
6286
+ chosen = (closed if withheld == frozenset(_candidates.AXES)
6287
+ else reduce({axis: axis not in withheld
6288
+ for axis in _candidates.AXES}))
6289
+ return chosen, withheld, _unestablished_axes(
6290
+ opened[0], chosen[0], classified.projection)
5491
6291
 
5492
6292
 
5493
6293
  def _unestablished_axes(opened, closed,
@@ -5587,11 +6387,17 @@ def _reduce_evaluation(account_key, evaluation: _Evaluation, transition, *,
5587
6387
  # this classification's snapshot and the transition's receipts.
5588
6388
  facts = _stale_member_facts(
5589
6389
  classified.stale, () if transition is None else transition.receipts, members)
6390
+ # #1019/#1014 spec 4.2: the retained supports the same samples refute,
6391
+ # pruned before the kernel decides the pending drops.
6392
+ refuted = _refuted_supports(
6393
+ classified.stale,
6394
+ None if transition is None else transition.control.pending_drops)
5590
6395
  (decision, anchor, active, tombstones), withheld, unestablished = (
5591
6396
  _gated_reduction(
5592
6397
  account_key, members, transition, eval_epoch=evaluation.at,
5593
6398
  fence_epoch=fence_epoch, classified=classified, frontier=frontier,
5594
- same_second_later=same_second_later | unknown, member_facts=facts))
6399
+ same_second_later=same_second_later | unknown, member_facts=facts,
6400
+ refuted_supports=refuted))
5595
6401
  if unknown:
5596
6402
  # Fix round K6 (825-832-CD6-V01): an order that cannot be established
5597
6403
  # is neither later nor earlier. When the reduction is the same either
@@ -5603,7 +6409,8 @@ def _reduce_evaluation(account_key, evaluation: _Evaluation, transition, *,
5603
6409
  unfenced, unfenced_withheld, unfenced_unestablished = _gated_reduction(
5604
6410
  account_key, members, transition, eval_epoch=evaluation.at,
5605
6411
  fence_epoch=fence_epoch, classified=classified, frontier=frontier,
5606
- same_second_later=same_second_later, member_facts=facts)
6412
+ same_second_later=same_second_later, member_facts=facts,
6413
+ refuted_supports=refuted)
5607
6414
  if (unfenced[0] != decision or unfenced_withheld != withheld
5608
6415
  or unfenced_unestablished != unestablished):
5609
6416
  return _Resolution(transition, classified, "deferred", AccountResult(
@@ -5633,16 +6440,13 @@ def _reduce_evaluation(account_key, evaluation: _Evaluation, transition, *,
5633
6440
  if _plan_axis(decision.plan, axis) is not None
5634
6441
  else decision.control.pending_drops.get(axis))
5635
6442
  for axis in _candidates.AXES}))
5636
- prior_withheld = bool(transition is not None and transition.quarantine
5637
- and transition.quarantine.get("dropWithheld"))
5638
6443
  quarantine = ...
5639
- if withheld != prior_withheld:
5640
- quarantine = {
5641
- "authorityCommit": (None if transition is None
5642
- or transition.quarantine is None
5643
- else transition.quarantine.get("authorityCommit")),
5644
- "dropWithheld": withheld,
5645
- }
6444
+ if frozenset(withheld) != _withheld_axes(transition):
6445
+ # #948 review finding F1: the persisted flag is True when some axis's
6446
+ # drop was withheld, and names that axis when only one is, so the
6447
+ # render path reduces each axis with its own permission and a
6448
+ # released axis's drop is never acknowledged as nothing to do.
6449
+ quarantine = _quarantine_document(transition, frozenset(withheld))
5646
6450
  control_repair_required = (
5647
6451
  transition is None
5648
6452
  or transition.control.db_projection.db_files != projection.db_files
@@ -5657,7 +6461,8 @@ def _reduce_evaluation(account_key, evaluation: _Evaluation, transition, *,
5657
6461
  plan = _bound_publication_plan(
5658
6462
  decision, projection, active, binding, generation=generation + 1,
5659
6463
  now_epoch=evaluation.at, intake_sequence=evaluation.sequence,
5660
- refused=_refused_tokens(facts))
6464
+ refused=_rise_ineligible_tokens(facts),
6465
+ drop_refused=_drop_refused_tokens(facts))
5661
6466
  if plan is None:
5662
6467
  if decision.action == "NOOP" and control_repair_required:
5663
6468
  decision = dataclasses.replace(decision, action="WRITE_CONTROL")
@@ -5722,7 +6527,9 @@ def _reduce_evaluation(account_key, evaluation: _Evaluation, transition, *,
5722
6527
  active, db=after, control=decision.control,
5723
6528
  tombstones=tombstones, now_epoch=evaluation.at,
5724
6529
  account_key=account_key,
5725
- drop_consensus_allowed=not withheld, member_facts=facts).control
6530
+ drop_consensus_allowed={
6531
+ axis: axis not in withheld for axis in _candidates.AXES},
6532
+ member_facts=facts, refuted_supports=refuted).control
5726
6533
  # The record kernel can deliberately leave the DB unchanged: the
5727
6534
  # first zero arms its own debounce and unsupported small drops are HWM
5728
6535
  # rejected. Preserve the precomputed bounded attempt state so the next
@@ -5764,7 +6571,10 @@ def _reduce_and_publish_partition(account_key, candidates, transition, *,
5764
6571
  try:
5765
6572
  scope = _stale_scope(account_key, candidates, transition)
5766
6573
  except (OSError, ValueError, TypeError, KeyError):
5767
- scope = (None, bool(transition is not None and transition.receipts))
6574
+ # Unknown scope: every sample is read and every kind of drop-proof
6575
+ # work is assumed (#1019 spec 4.4), never "no proof needed".
6576
+ scope = (_EVERY_SAMPLE, bool(transition is not None and transition.receipts),
6577
+ _ALL_DROP_WORK)
5768
6578
  token = _STALE_SCOPE.set(scope)
5769
6579
  try:
5770
6580
  return _reduce_and_publish_partition_work(
@@ -6161,8 +6971,11 @@ def _rescan_legacy_spool() -> bool:
6161
6971
  # #797/#823/#840 spec 4.6 B4: the v1 coordination state joins the
6162
6972
  # inventory at the revision-2 boundary, never before it, so a store
6163
6973
  # whose revision is not yet established compares like for like.
6974
+ # #1019/#1014 spec 4.7 C4: the v2 state joins at the revision-3
6975
+ # boundary the same way.
6164
6976
  state = _cctally_journal.capture_legacy_spool_state(
6165
- include_v1=cutover.revision is not None)
6977
+ include_v1=cutover.revision is not None,
6978
+ include_v2=cutover.revision3 is not None)
6166
6979
  except _cctally_journal.LegacySpoolInventoryIncomplete as failure:
6167
6980
  return _cctally_journal.record_legacy_spool_inventory_incomplete(
6168
6981
  failure) is not None
@@ -6187,9 +7000,15 @@ def _rescan_legacy_spool() -> bool:
6187
7000
  changed=changed, new_state=state, reason="baseline-unavailable")
6188
7001
  return True
6189
7002
  if baseline.digest() == state.digest():
6190
- if anchor.episode_open:
6191
- # A complete inventory ends the failure episode; the spool is
6192
- # unchanged, so it anchors nothing new and clears nothing.
7003
+ if anchor.uncertain is not None:
7004
+ # #948 spec 6.3: a complete inventory ends the failure episode the
7005
+ # JOURNAL shows open (whatever the sidecar says, so an upgrade
7006
+ # over a sidecar-closed episode still completes it): the spool is
7007
+ # unchanged, so it anchors nothing new; the completion op closes
7008
+ # the episode for the outcome quarantine's recovery barrier and is
7009
+ # no trigger. The sidecar stays the writer's local dedup hint.
7010
+ _cctally_journal.append_legacy_spool_inventory_completion(
7011
+ episode=anchor.uncertain, digest=state.digest())
6193
7012
  _cctally_journal.close_legacy_spool_inventory_episode(
6194
7013
  anchor.uncertain)
6195
7014
  return False
@@ -6370,10 +7189,12 @@ def _wait_class(result: "AccountResult") -> "dict | None":
6370
7189
  kind = "evidence"
6371
7190
  elif reason in ("unreadable", "coverage", "rebuild", "busy",
6372
7191
  _ORDER_UNKNOWN_REASON, _LEGACY_INVENTORY_REASON,
6373
- _PUBLICATION_SCAN_REASON):
7192
+ _PUBLICATION_SCAN_REASON, _DROP_EVIDENCE_REASON):
6374
7193
  # Unknown same-second order resolves when the journal becomes
6375
7194
  # readable, which only a retry can observe, as well as on a later
6376
- # accepted reading (a relevant evidence change).
7195
+ # accepted reading (a relevant evidence change). #1019 review
7196
+ # finding P2: so does a frozen plan's missing drop evidence; without
7197
+ # a wait every render dispatched a pass that deferred it again.
6377
7198
  kind = "io"
6378
7199
  else:
6379
7200
  return None
@@ -6471,7 +7292,10 @@ def _tokens_from(conn, account_key: str, reason: "str | None") -> str:
6471
7292
  status = _cctally_quarantine.account_status(conn, account_key)
6472
7293
  document = {"index": index, "rows": rows,
6473
7294
  "quarantine": {k: status[k] for k in sorted(status)}}
6474
- if reason == "coverage":
7295
+ # #1019 review finding P2: a frozen plan's missing drop evidence is
7296
+ # most often unprojected provider samples, so it waits on the
7297
+ # journal cursor too.
7298
+ if reason in ("coverage", _DROP_EVIDENCE_REASON):
6475
7299
  try:
6476
7300
  cursor = conn.execute(
6477
7301
  "SELECT segment, offset, applied_segment, applied_offset "
@@ -7685,7 +8509,7 @@ def _render_acknowledge(account_key: str, transition: _Transition, *,
7685
8509
  proof = transition.coverage
7686
8510
  if proof is None or proof["digest"] != coverage:
7687
8511
  return False
7688
- withheld = bool(transition.quarantine and transition.quarantine.get("dropWithheld"))
8512
+ withheld = _withheld_axes(transition)
7689
8513
  last = None
7690
8514
  for count, evaluation in enumerate(_intake_evaluations(
7691
8515
  account_key, after=transition.intake_acknowledged)):
@@ -7707,10 +8531,12 @@ def _render_acknowledge(account_key: str, transition: _Transition, *,
7707
8531
  # ends where the evaluation recorded — then the reading precedes
7708
8532
  # it. Otherwise the detached reducer decides.
7709
8533
  return False
8534
+ facts = _render_member_facts(transition, evaluation.members)
7710
8535
  decision, anchor, _active, _fences = _reduce_partition(
7711
8536
  account_key, evaluation.members, transition, now_epoch=evaluation.at,
7712
8537
  fence_epoch=now_epoch, projection=transition.control.db_projection,
7713
- drop_consensus_allowed=not withheld)
8538
+ drop_consensus_allowed=_withheld_permission(withheld),
8539
+ member_facts=facts)
7714
8540
  if (decision.action != "NOOP" or decision.unknown_axes
7715
8541
  or anchor != transition.five_hour_anchor
7716
8542
  or _partition_binding(evaluation.members, transition)
@@ -7722,7 +8548,8 @@ def _render_acknowledge(account_key: str, transition: _Transition, *,
7722
8548
  if withheld and _unestablished_axes(_reduce_partition(
7723
8549
  account_key, evaluation.members, transition,
7724
8550
  now_epoch=evaluation.at, fence_epoch=now_epoch,
7725
- projection=transition.control.db_projection)[0], decision,
8551
+ projection=transition.control.db_projection,
8552
+ member_facts=facts)[0], decision,
7726
8553
  transition.control.db_projection):
7727
8554
  # Fix round K10 (P1-2): a NOOP only because the gate is closed on
7728
8555
  # an axis with no baseline is unknown classification, never an
@@ -7736,6 +8563,10 @@ def _render_acknowledge(account_key: str, transition: _Transition, *,
7736
8563
  # #797 spec 4.4 (D7c q3): a NOOP that leaves receipt work for
7737
8564
  # either earning path is the detached reducer's to complete.
7738
8565
  return False
8566
+ if _render_drop_work(transition, evaluation.members):
8567
+ # #1019 spec 4.4: nor is pending drop-proof work the cached
8568
+ # facts cannot decide consumed here.
8569
+ return False
7739
8570
  last = evaluation.sequence
7740
8571
  if last is None:
7741
8572
  return True
@@ -7825,13 +8656,15 @@ def _preliminary_decision() -> "_candidates.ReductionDecision | None":
7825
8656
  return fork
7826
8657
  # A drop the detached reducer last withheld stays withheld here: the
7827
8658
  # render path cannot open the index to re-check the quarantine (C4),
7828
- # and re-forking every render for it would be pure disk cost.
8659
+ # and re-forking every render for it would be pure disk cost. Each
8660
+ # axis keeps its own permission (#948 review finding F1): a released
8661
+ # axis's drop is reduced open, so its deadline forks.
7829
8662
  decision, _anchor, _active, _fences = _reduce_partition(
7830
8663
  account_key, active, transition, now_epoch=now_epoch,
7831
8664
  projection=transition.control.db_projection,
7832
- drop_consensus_allowed=not (
7833
- transition.quarantine
7834
- and transition.quarantine.get("dropWithheld")))
8665
+ drop_consensus_allowed=_withheld_permission(
8666
+ _withheld_axes(transition)),
8667
+ member_facts=_render_member_facts(transition, active))
7835
8668
  if decision.action != "NOOP":
7836
8669
  return decision
7837
8670
  if decision.unknown_axes:
@@ -7844,6 +8677,10 @@ def _preliminary_decision() -> "_candidates.ReductionDecision | None":
7844
8677
  # preliminary NOOP exit must not swallow it: the detached reducer
7845
8678
  # completes the evaluation and earns.
7846
8679
  return fork
8680
+ if _render_drop_work(transition, active):
8681
+ # #1019 spec 4.4: drop-proof work the cached facts cannot decide
8682
+ # is the detached reducer's too.
8683
+ return fork
7847
8684
  return None
7848
8685
 
7849
8686
 
@@ -8098,16 +8935,26 @@ def _statusline_oauth_tick(*, sync_for_test: bool = False) -> None:
8098
8935
 
8099
8936
 
8100
8937
  def _resolve_context_window(model_id, warn_once) -> "int | None":
8101
- """Look up ``model_id`` in ``CLAUDE_MODEL_CONTEXT_WINDOWS``; fall back
8102
- to a family-substring match against
8938
+ """The model FALLBACK for a context window (#1002 spec §5.3).
8939
+
8940
+ Empty id → ``None`` silently. Otherwise, on the lowercased id: a
8941
+ terminal ``[1m]`` → 1,000,000 whatever the family; then, with one
8942
+ trailing ``-YYYYMMDD`` stripped, an exact hit in
8943
+ ``CLAUDE_MODEL_CONTEXT_WINDOWS``; then a family-substring match against
8103
8944
  ``CLAUDE_MODEL_CONTEXT_WINDOW_DEFAULT_FAMILY``. Unknown id → ``None`` +
8104
- one-shot stderr warning.
8945
+ one-shot stderr warning (naming the id as given).
8105
8946
  """
8106
8947
  if not model_id:
8107
8948
  return None
8108
- if model_id in CLAUDE_MODEL_CONTEXT_WINDOWS:
8109
- return CLAUDE_MODEL_CONTEXT_WINDOWS[model_id]
8110
8949
  mid_lower = model_id.lower()
8950
+ if mid_lower.endswith("[1m]"):
8951
+ return 1_000_000
8952
+ # One anchored trailing snapshot date (`claude-opus-5-5-20261001`).
8953
+ registered = CLAUDE_MODEL_CONTEXT_WINDOWS.get(
8954
+ re.sub(r"-\d{8}$", "", mid_lower)
8955
+ )
8956
+ if registered is not None:
8957
+ return registered
8111
8958
  for family, window in CLAUDE_MODEL_CONTEXT_WINDOW_DEFAULT_FAMILY.items():
8112
8959
  if family in mid_lower:
8113
8960
  return window
@@ -8500,39 +9347,55 @@ def _build_statusline_injections(warn_once):
8500
9347
  except Exception:
8501
9348
  pass
8502
9349
 
8503
- def _context_pct(transcript_path, model_id):
8504
- if not transcript_path or not model_id:
8505
- return None
8506
- window = _resolve_context_window(model_id, warn_once)
8507
- if window is None:
8508
- return None
8509
- try:
8510
- usage = _read_last_assistant_usage(transcript_path)
8511
- except Exception:
8512
- return None
8513
- if not isinstance(usage, dict):
9350
+ def _context_pct(transcript_path, model_id, window_size, context_tokens):
9351
+ # #1002 spec §5.2. The payload's window and numerator (already
9352
+ # validated by the kernel) each win when not None; the model table
9353
+ # and the transcript tail are independent fallbacks. `is not None`
9354
+ # throughout so a payload zero is honored.
9355
+ if context_tokens is None and not transcript_path:
9356
+ return None # silent: nothing to measure (today's path)
9357
+ if window_size is not None:
9358
+ window = window_size # skips the model lookup and its warning
9359
+ elif model_id:
9360
+ # May warn; model resolution precedes any transcript read.
9361
+ window = _resolve_context_window(model_id, warn_once)
9362
+ else:
9363
+ return None # silent: no window source at all
9364
+ if window is None or window <= 0:
8514
9365
  return None
9366
+ if context_tokens is not None:
9367
+ ctx_tokens = context_tokens # the transcript is not opened
9368
+ else:
9369
+ try:
9370
+ usage = _read_last_assistant_usage(transcript_path)
9371
+ except Exception:
9372
+ return None
9373
+ if not isinstance(usage, dict):
9374
+ return None
9375
+ try:
9376
+ ctx_tokens = (
9377
+ int(usage.get("input_tokens", 0) or 0)
9378
+ + int(usage.get("cache_read_input_tokens", 0) or 0)
9379
+ + int(usage.get("cache_creation_input_tokens", 0) or 0)
9380
+ )
9381
+ except (TypeError, ValueError):
9382
+ return None
8515
9383
  try:
8516
- ctx_tokens = (
8517
- int(usage.get("input_tokens", 0) or 0)
8518
- + int(usage.get("cache_read_input_tokens", 0) or 0)
8519
- + int(usage.get("cache_creation_input_tokens", 0) or 0)
8520
- )
8521
- except (TypeError, ValueError):
8522
- return None
8523
- if window <= 0:
8524
- return None
8525
- return ctx_tokens / window * 100.0
9384
+ return ctx_tokens / window * 100.0
9385
+ except OverflowError:
9386
+ return None # a count no float can hold: `🧠 N/A`, not a crash
8526
9387
 
8527
9388
  def _quota_regimes() -> tuple:
8528
9389
  """#661 S2 §9. The stored metering regimes, read without mutating.
8529
9390
 
8530
9391
  Three properties this read must have, and each is a decision:
8531
9392
 
8532
- * It takes NO flock and NO throttle. `save_calibrations` writes
8533
- through `os.replace` in the same directory, so a lock-free reader
8534
- always sees a complete old or new inode; a flock here would let a
8535
- writer stall a prompt.
9393
+ * It takes NO blocking flock and NO throttle. `save_calibrations`
9394
+ writes through `os.replace` in the same directory, so a lock-free
9395
+ reader always sees a complete old or new inode; a blocking flock
9396
+ here would let a writer stall a prompt. (#1015: the lineage facts
9397
+ read tries the stats maintenance flock SHARED and NON-blocking, and
9398
+ a busy lock hides the marker rather than waiting.)
8536
9399
  * It goes through `read_stored_state_readonly`, not through
8537
9400
  `load_calibrations`. The loader renames a malformed or
8538
9401
  version-ahead file aside through `_quarantine`, and a status line
@@ -8547,13 +9410,22 @@ def _build_statusline_injections(warn_once):
8547
9410
 
8548
9411
  It also NEVER opens `cache.db` and never calls `analyse`. The
8549
9412
  marker is derived from the regime pair alone.
9413
+
9414
+ #1015 spec 5.6: the regimes handed to the marker are the CLEAN ones,
9415
+ and none at all when the active transition is superseded or the
9416
+ correction facts cannot be verified (no marker is asserted). The
9417
+ facts are read — through the fail-fast read-only opener, which never
9418
+ waits, recovers or writes — only when a change is active.
8550
9419
  """
8551
9420
  try:
8552
9421
  glue = _cctally()._load_sibling("_cctally_quota_model")
8553
9422
  state = glue.read_stored_state_readonly()
8554
9423
  if state is None:
8555
9424
  return ()
8556
- return tuple(glue.stored_regimes(state, None))
9425
+ calibration = _cctally()._load_sibling(
9426
+ "_cctally_quota_calibration")
9427
+ return tuple(calibration.marker_regimes(
9428
+ None, regimes=glue.stored_regimes(state, None)))
8557
9429
  except Exception: # noqa: BLE001
8558
9430
  return ()
8559
9431