cctally 1.114.0 → 1.116.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/bin/_cctally_account.py +32 -8
  3. package/bin/_cctally_cache.py +4491 -295
  4. package/bin/_cctally_completion.py +1355 -0
  5. package/bin/_cctally_config.py +37 -0
  6. package/bin/_cctally_core.py +282 -9
  7. package/bin/_cctally_dashboard.py +8 -11
  8. package/bin/_cctally_dashboard_conversation.py +292 -23
  9. package/bin/_cctally_dashboard_envelope.py +42 -35
  10. package/bin/_cctally_dashboard_sources.py +18 -9
  11. package/bin/_cctally_db.py +401 -9
  12. package/bin/_cctally_doctor.py +469 -27
  13. package/bin/_cctally_five_hour.py +166 -1049
  14. package/bin/_cctally_forecast.py +81 -9
  15. package/bin/_cctally_journal.py +5076 -177
  16. package/bin/_cctally_journal_repair.py +110 -19
  17. package/bin/_cctally_oauth_samples.py +280 -0
  18. package/bin/_cctally_parser.py +47 -0
  19. package/bin/_cctally_project.py +8 -2
  20. package/bin/_cctally_quarantine.py +662 -0
  21. package/bin/_cctally_quota.py +96 -12
  22. package/bin/_cctally_quota_model.py +391 -83
  23. package/bin/_cctally_record.py +1088 -269
  24. package/bin/_cctally_rederive.py +854 -192
  25. package/bin/_cctally_refresh.py +113 -13
  26. package/bin/_cctally_source_analytics.py +23 -2
  27. package/bin/_cctally_statusline.py +7062 -505
  28. package/bin/_cctally_store.py +1244 -91
  29. package/bin/_cctally_transcript.py +8 -7
  30. package/bin/_cctally_tui.py +119 -60
  31. package/bin/_cctally_update.py +25 -0
  32. package/bin/_lib_account_binding.py +152 -0
  33. package/bin/_lib_account_protocol.py +569 -0
  34. package/bin/_lib_aggregators.py +4 -0
  35. package/bin/_lib_cache_coverage.py +13 -1
  36. package/bin/_lib_cache_writer_lock.py +19 -3
  37. package/bin/_lib_clone_copy.py +177 -0
  38. package/bin/_lib_codex_conversation_query.py +133 -19
  39. package/bin/_lib_codex_conversation_watch.py +59 -14
  40. package/bin/_lib_codex_drain_admission.py +214 -0
  41. package/bin/_lib_codex_metadata.py +18 -1
  42. package/bin/_lib_codex_publication_barrier.py +277 -0
  43. package/bin/_lib_codex_rollout_container.py +502 -0
  44. package/bin/_lib_conversation_dispatch.py +3 -0
  45. package/bin/_lib_conversation_query.py +11 -3
  46. package/bin/_lib_dashboard_sources.py +57 -15
  47. package/bin/_lib_detector_access_registry.py +4038 -0
  48. package/bin/_lib_doctor.py +753 -1
  49. package/bin/_lib_five_hour.py +126 -0
  50. package/bin/_lib_ingest_frontier.py +414 -20
  51. package/bin/_lib_journal.py +261 -10
  52. package/bin/_lib_journal_router.py +166 -48
  53. package/bin/_lib_jsonl.py +5 -4
  54. package/bin/_lib_legacy_spool.py +126 -0
  55. package/bin/_lib_legacy_writer_evidence.py +136 -0
  56. package/bin/_lib_meter_rate_change.py +10 -0
  57. package/bin/_lib_oauth_samples.py +238 -0
  58. package/bin/_lib_outcome_quarantine.py +690 -0
  59. package/bin/_lib_pricing.py +7 -2
  60. package/bin/_lib_pricing_check.py +0 -12
  61. package/bin/_lib_provider_activity.py +770 -0
  62. package/bin/_lib_quarantine.py +155 -0
  63. package/bin/_lib_quota_calibration.py +53 -8
  64. package/bin/_lib_quota_copy.py +2 -1
  65. package/bin/_lib_quota_model.py +384 -52
  66. package/bin/_lib_rebuild_progress.py +146 -0
  67. package/bin/_lib_record.py +22 -0
  68. package/bin/_lib_rederive.py +393 -8
  69. package/bin/_lib_segment_summary.py +35 -2
  70. package/bin/_lib_snapshot_cache.py +43 -1
  71. package/bin/_lib_stale_proof.py +708 -0
  72. package/bin/_lib_statusline_candidates.py +356 -27
  73. package/bin/_lib_worker_launches.py +315 -0
  74. package/bin/cctally +29 -12
  75. package/dashboard/static/assets/{ConversationsView-Bc8LCOAZ.js → ConversationsView-Dflb_myA.js} +1 -1
  76. package/dashboard/static/assets/DoctorModal-DFX5CfIs.js +1 -0
  77. package/dashboard/static/assets/{ModalRoot-DLPgyAM_.js → ModalRoot-HlxoagIa.js} +1 -1
  78. package/dashboard/static/assets/{ProjectsDrillPanel-z53a4v4M.js → ProjectsDrillPanel-C3PJy1xx.js} +1 -1
  79. package/dashboard/static/assets/{SourceDetailModal-Bw0T8W-W.js → SourceDetailModal-BczsWKhD.js} +1 -1
  80. package/dashboard/static/assets/{UpdateModal-D5AKx4Yw.js → UpdateModal-Os-Lo5cs.js} +1 -1
  81. package/dashboard/static/assets/{index-BXDOzSng.js → index-CYZ3NLqF.js} +3 -3
  82. package/dashboard/static/assets/{outlineNavigation-JObY7cUY.js → outlineNavigation-k-zIif6W.js} +1 -1
  83. package/dashboard/static/dashboard.html +1 -1
  84. package/package.json +20 -2
  85. package/bin/_lib_merge_gate.py +0 -725
  86. package/dashboard/static/assets/DoctorModal-D--qVBwe.js +0 -1
@@ -12,6 +12,8 @@ import math
12
12
  import re
13
13
  from typing import Callable, Mapping
14
14
 
15
+ import _lib_provider_activity
16
+
15
17
 
16
18
  #: Candidate and tombstone documents. Every session on this machine writes
17
19
  #: candidates, so this version may only move when peer sessions running the
@@ -24,6 +26,9 @@ CONTROL_SCHEMA_VERSION = 2
24
26
  #: Control documents this binary can read. Version 1 predates the pending
25
27
  #: drop's deadline and retained evidence; see `_pending_drop`.
26
28
  CONTROL_SCHEMA_VERSIONS_READ = (1, 2)
29
+ #: #825/#832: the account-bound protocol generation. Every current document,
30
+ #: retry namespace and plan names it; the legacy spool never carries it.
31
+ PROTOCOL_GENERATION = "account-bound-v1"
27
32
  #: D5: a non-extendable wall clock measured from the first pending drop.
28
33
  PENDING_DROP_DEADLINE_SECONDS = 180
29
34
  CANDIDATE_DOCUMENT_MAX_BYTES = 4 * 1024
@@ -53,6 +58,14 @@ class Candidate:
53
58
  received_at: int = 0
54
59
  five_hour: AxisValue | None = None
55
60
  seven_day: AxisValue | None = None
61
+ #: The immutable `AccountBinding` captured with this candidate (#825/#832
62
+ #: spec 4.2). The reducer partitions by its account; it is never inferred.
63
+ binding: object | None = None
64
+ #: The provider-activity witness (#797 spec 4.2,
65
+ #: `_lib_provider_activity.ActivityWitness`) captured once at this
66
+ #: arrival, before its intake commit. Required in every v2 candidate
67
+ #: document, so the intake identity hashes it; never recomputed.
68
+ activity: object | None = None
56
69
 
57
70
 
58
71
  @dataclasses.dataclass(frozen=True)
@@ -62,7 +75,11 @@ class AxisProjection:
62
75
  canonical_key: int
63
76
  captured_at: int
64
77
  source: str
65
- reset_generation: int
78
+ #: The reset generation the projected value belongs to. Weekly: the
79
+ #: reset-aware floor epoch (an int). Five-hour (#832): the LOGICAL
80
+ #: generation string of the exact account's active reset event, or "" for
81
+ #: no event — never a SQLite rowid, so a rebuild cannot change it.
82
+ reset_generation: int | str
66
83
 
67
84
 
68
85
  @dataclasses.dataclass(frozen=True)
@@ -73,11 +90,79 @@ class FileFingerprint:
73
90
  mtime_ns: int
74
91
 
75
92
 
93
+ #: The classification evidence of one projected axis (#825/#832 spec 4.3
94
+ #: revision 5, Codex design-fix-round-5 Q1). ``present``: the axis holds a
95
+ #: baseline. ``expired_predecessor``: every admitted five-hour predecessor
96
+ #: left the projection only through expiry at the evaluation's instant (on
97
+ #: the weekly axis: every retained weekly row lies in a week that ends at or
98
+ #: before ``predecessor_key``, fix round K14c). ``proven_empty``: the covered
99
+ #: snapshot proves the account has no admitted baseline on the axis.
100
+ #: ``floor_retired`` (revision 6, Codex design-fix-round-6 Q2): the axis's
101
+ #: group has an effective, admitted floor of the account and no accepted
102
+ #: observation of the axis survives at or after it. ``unknown``: anything
103
+ #: else — never inferred from an absent value.
104
+ EVIDENCE_PRESENT = "present"
105
+ EVIDENCE_EXPIRED_PREDECESSOR = "expired_predecessor"
106
+ EVIDENCE_PROVEN_EMPTY = "proven_empty"
107
+ EVIDENCE_FLOOR_RETIRED = "floor_retired"
108
+ EVIDENCE_UNKNOWN = "unknown"
109
+
110
+
111
+ @dataclasses.dataclass(frozen=True)
112
+ class AxisEvidence:
113
+ """How an axis of a projection was classified, computed inside the same
114
+ validated, prefix-covered snapshot as the projection itself. An
115
+ expired predecessor keeps the greatest expired window's account-canonical
116
+ key and raw reset: the evidence a later reading's window is compared
117
+ with, never a value to display or to fill an axis with."""
118
+
119
+ kind: str
120
+ predecessor_key: int | None = None
121
+ predecessor_raw: int | None = None
122
+ #: Fix round K12 (K11R-10): for an unknown axis that wall time alone can
123
+ #: classify — a row beyond the future band — the earliest instant at
124
+ #: which such a row becomes admissible; else None.
125
+ admissible_at: int | None = None
126
+ #: ``floor_retired`` only (spec 4.3 revision 6): the account, the axis,
127
+ #: the retired group's account-canonical key, the floor instant, the
128
+ #: floor's logical identity and the group's anchor raw reset. Never a
129
+ #: retired percentage.
130
+ account_key: str | None = None
131
+ axis: str | None = None
132
+ group_key: int | None = None
133
+ floor_epoch: int | None = None
134
+ floor_identity: str | None = None
135
+ anchor_raw: int | None = None
136
+
137
+
138
+ EVIDENCE_UNKNOWN_AXIS = AxisEvidence(EVIDENCE_UNKNOWN)
139
+
140
+ #: How `classify_absent_axis` classifies a reading on an axis that holds no
141
+ #: projected baseline. ``publish``: a first reading or a new window, under
142
+ #: either drop gate. ``stale``: an older window, or a reading proven captured
143
+ #: before the floor of a retired group — the older-window disposition, which
144
+ #: publishes nothing on the axis and opens no consensus. ``floor``: a reading
145
+ #: in a floor-retired group, which publishes only from contributors captured
146
+ #: at or after the floor. ``unknown``: unclassifiable; the caller defers the
147
+ #: whole evaluation.
148
+ CLASS_PUBLISH = "publish"
149
+ CLASS_STALE = "stale"
150
+ CLASS_FLOOR = "floor"
151
+ CLASS_UNKNOWN = "unknown"
152
+
153
+
76
154
  @dataclasses.dataclass(frozen=True)
77
155
  class DbProjection:
78
156
  five_hour: AxisProjection | None
79
157
  seven_day: AxisProjection | None
80
158
  db_files: Mapping[str, FileFingerprint | None] | None = None
159
+ #: The exact account this projection was read for (#825/#832 spec 4.5).
160
+ account_key: str | None = None
161
+ #: Per-axis `AxisEvidence` (spec 4.3 revision 5), keyed by axis name. Only
162
+ #: the covered projection read carries it; a projection without it — a
163
+ #: persisted control document, a deferred read — classifies an absent
164
+ #: axis as unknown.
165
+ evidence: Mapping[str, AxisEvidence] | None = None
81
166
 
82
167
 
83
168
  @dataclasses.dataclass(frozen=True)
@@ -92,7 +177,12 @@ class RetrySignature:
92
177
  candidate_percent: float
93
178
  db_key: int | None
94
179
  db_percent: float | None
95
- db_reset_generation: int | None
180
+ db_reset_generation: int | str | None
181
+ #: The retry namespace (#825/#832 spec 4.3): protocol generation, account
182
+ #: and axis. Window, value and logical generation are the fields above.
183
+ protocol_generation: str | None = None
184
+ account_key: str | None = None
185
+ axis: str | None = None
96
186
 
97
187
 
98
188
  @dataclasses.dataclass(frozen=True)
@@ -174,6 +264,12 @@ class ReductionDecision:
174
264
  action: str
175
265
  control: ControlState
176
266
  plan: PublicationPlan | None = None
267
+ #: Fix round K13 (review cycle 7 finding P1-2, spec 4.3 *Classification
268
+ #: evidence*): the axes on which a reading met no projected baseline and
269
+ #: no classification evidence. Neither drop gate publishes such an axis;
270
+ #: the caller defers the whole evaluation instead of reading the NOOP
271
+ #: on it as an unchanged reading.
272
+ unknown_axes: frozenset = frozenset()
177
273
 
178
274
 
179
275
  def _is_int(value: object) -> bool:
@@ -218,6 +314,16 @@ def _require_ascii_source(value: object) -> str:
218
314
  return value
219
315
 
220
316
 
317
+ def validate_activity(value: object):
318
+ """The candidate's activity witness, or ``StateValidationError``: a
319
+ missing or malformed witness makes the v2 document malformed and never
320
+ silently reads as ``unknown`` (#797 spec 4.2)."""
321
+ try:
322
+ return _lib_provider_activity.witness_from_json(value)
323
+ except _lib_provider_activity.MalformedWitness as exc:
324
+ raise StateValidationError(str(exc)) from exc
325
+
326
+
221
327
  def _axis_value(
222
328
  value: object,
223
329
  *,
@@ -318,18 +424,32 @@ def _axis_projection(value: object) -> AxisProjection | None:
318
424
  for key in ("rawResetsAt", "canonicalKey", "capturedAt"):
319
425
  if not _is_int(doc[key]):
320
426
  raise StateValidationError(f"{key} must be an integer")
321
- if not _is_nonnegative_int(doc["resetGeneration"]):
322
- raise StateValidationError("resetGeneration must be nonnegative")
427
+ generation = _reset_generation(doc["resetGeneration"], "resetGeneration")
323
428
  return AxisProjection(
324
429
  percent=_percent(doc["percent"]),
325
430
  raw_resets_at=doc["rawResetsAt"],
326
431
  canonical_key=doc["canonicalKey"],
327
432
  captured_at=doc["capturedAt"],
328
433
  source=_require_ascii_source(doc["source"]),
329
- reset_generation=doc["resetGeneration"],
434
+ reset_generation=generation,
330
435
  )
331
436
 
332
437
 
438
+ #: A logical five-hour generation is `<effective>|<detected>|<journal id>`.
439
+ _RESET_GENERATION_MAX_CHARS = 512
440
+
441
+
442
+ def _reset_generation(value: object, label: str) -> int | str:
443
+ """A weekly floor epoch (nonnegative int) or a five-hour logical
444
+ generation (bounded ASCII string; "" means no event)."""
445
+ if _is_nonnegative_int(value):
446
+ return value
447
+ if (isinstance(value, str) and len(value) <= _RESET_GENERATION_MAX_CHARS
448
+ and value.isascii()):
449
+ return value
450
+ raise StateValidationError(f"{label} must be a nonnegative int or a logical generation")
451
+
452
+
333
453
  def _fingerprint(value: object) -> FileFingerprint | None:
334
454
  if value is None:
335
455
  return None
@@ -360,21 +480,30 @@ def _retry_signature(value: object) -> RetrySignature | None:
360
480
  if value is None:
361
481
  return None
362
482
  doc = _require_object(value, "retrySignature")
363
- _require_exact_keys(
364
- doc,
365
- {"candidateKey", "candidatePercent", "dbKey", "dbPercent", "dbResetGeneration"},
366
- )
483
+ base = {"candidateKey", "candidatePercent", "dbKey", "dbPercent", "dbResetGeneration"}
484
+ scoped = base | {"protocolGeneration", "accountKey", "axis"}
485
+ if set(doc) not in (base, scoped):
486
+ raise StateValidationError("unexpected retrySignature keys")
367
487
  if not _is_int(doc["candidateKey"]):
368
488
  raise StateValidationError("candidateKey must be an integer")
369
489
  db_generation = doc["dbResetGeneration"]
370
- if db_generation is not None and not _is_nonnegative_int(db_generation):
371
- raise StateValidationError("dbResetGeneration must be nonnegative or null")
490
+ if db_generation is not None:
491
+ db_generation = _reset_generation(db_generation, "dbResetGeneration")
492
+ scope = (None, None, None)
493
+ if set(doc) == scoped:
494
+ scope = (doc["protocolGeneration"], doc["accountKey"], doc["axis"])
495
+ if (not isinstance(scope[0], str) or not isinstance(scope[1], str)
496
+ or not scope[1] or scope[2] not in AXES):
497
+ raise StateValidationError("invalid retry namespace")
372
498
  return RetrySignature(
373
499
  candidate_key=doc["candidateKey"],
374
500
  candidate_percent=_percent(doc["candidatePercent"]),
375
501
  db_key=_nullable_int(doc["dbKey"], "dbKey"),
376
502
  db_percent=_nullable_percent(doc["dbPercent"], "dbPercent"),
377
503
  db_reset_generation=db_generation,
504
+ protocol_generation=scope[0],
505
+ account_key=scope[1],
506
+ axis=scope[2],
378
507
  )
379
508
 
380
509
 
@@ -611,6 +740,9 @@ class _AxisDecision:
611
740
  action: str
612
741
  candidate: AxisValue | None
613
742
  pending: PendingDrop | None
743
+ #: A reading on an axis with no baseline and no classification evidence
744
+ #: (fix round K13): never published, whatever the drop gate.
745
+ unknown: bool = False
614
746
 
615
747
 
616
748
  def _candidate_axis(candidate: Candidate, axis: str) -> AxisValue | None:
@@ -621,6 +753,62 @@ def _projection_axis(projection: DbProjection, axis: str) -> AxisProjection | No
621
753
  return projection.five_hour if axis == "fiveHour" else projection.seven_day
622
754
 
623
755
 
756
+ def axis_evidence(projection: DbProjection, axis: str) -> AxisEvidence:
757
+ """The classification evidence of ``axis``: ``present`` whenever the
758
+ axis holds a baseline, else what the covered read recorded, else
759
+ unknown (an absent value alone proves nothing)."""
760
+ if _projection_axis(projection, axis) is not None:
761
+ return AxisEvidence(EVIDENCE_PRESENT)
762
+ recorded = (projection.evidence or {}).get(axis)
763
+ return recorded if isinstance(recorded, AxisEvidence) else EVIDENCE_UNKNOWN_AXIS
764
+
765
+
766
+ def classify_absent_axis(evidence: AxisEvidence | None, key: int, *,
767
+ axis: str) -> str:
768
+ """How a reading in window ``key`` on ``axis``, which holds no projected
769
+ baseline, is classified (spec 4.3 revisions 5 and 6): one of the
770
+ ``CLASS_*`` answers.
771
+
772
+ - Proven empty, or a predecessor whose window ``key`` strictly follows:
773
+ a first reading or a new window, so it publishes under a closed drop
774
+ gate too.
775
+ - An expired five-hour predecessor whose key ``key`` does not strictly
776
+ exceed (revision 6, *Stale expired window*, Codex design-fix-round-6
777
+ Q3): stale. On the weekly axis the predecessor's key is only a bound,
778
+ so such a reading stays unknown.
779
+ - A floor-retired group (revision 6, *Floor retired*, Q2): a strictly
780
+ newer group takes the new-window rule, an older group is stale, and a
781
+ reading in the group itself depends on its contributors' capture
782
+ instants (``CLASS_FLOOR``).
783
+ - Anything else is unknown classification."""
784
+ if evidence is None:
785
+ return CLASS_UNKNOWN
786
+ if evidence.kind == EVIDENCE_PROVEN_EMPTY:
787
+ return CLASS_PUBLISH
788
+ if (evidence.kind == EVIDENCE_EXPIRED_PREDECESSOR
789
+ and evidence.predecessor_key is not None):
790
+ if int(key) > int(evidence.predecessor_key):
791
+ return CLASS_PUBLISH
792
+ return CLASS_STALE if axis == "fiveHour" else CLASS_UNKNOWN
793
+ if (evidence.kind == EVIDENCE_FLOOR_RETIRED
794
+ and evidence.group_key is not None
795
+ and evidence.floor_epoch is not None):
796
+ if int(key) > int(evidence.group_key):
797
+ return CLASS_PUBLISH
798
+ if int(key) < int(evidence.group_key):
799
+ return CLASS_STALE
800
+ return CLASS_FLOOR
801
+ return CLASS_UNKNOWN
802
+
803
+
804
+ def classifies_first_reading(evidence: AxisEvidence | None, key: int) -> bool:
805
+ """Whether a reading in window ``key`` on an axis with no projected
806
+ baseline is a first reading or a new window whatever its contributors
807
+ (`classify_absent_axis` answers ``publish``). Such a reading is never a
808
+ drop, so it publishes under a closed drop gate too."""
809
+ return classify_absent_axis(evidence, key, axis="sevenDay") == CLASS_PUBLISH
810
+
811
+
624
812
  def _axis_value_key(value: AxisValue) -> int:
625
813
  return value.canonical_key if value.canonical_key is not None else value.raw_resets_at
626
814
 
@@ -793,13 +981,22 @@ def _axis_control_changed(before: PendingDrop | None, after: PendingDrop | None)
793
981
  return before != after
794
982
 
795
983
 
796
- def _build_retry_signature(reduced: AxisValue, db_axis: AxisProjection | None) -> RetrySignature:
984
+ def _build_retry_signature(
985
+ reduced: AxisValue, db_axis: AxisProjection | None, *, scope=None,
986
+ ) -> RetrySignature:
987
+ """``scope`` is ``(protocol_generation, account_key, axis)`` for a bound
988
+ partition (#825/#832 spec 4.3), so two accounts never share a spent
989
+ attempt even with identical values."""
990
+ protocol, account, axis = scope if scope is not None else (None, None, None)
797
991
  return RetrySignature(
798
992
  candidate_key=_axis_value_key(reduced),
799
993
  candidate_percent=reduced.percent,
800
994
  db_key=None if db_axis is None else db_axis.canonical_key,
801
995
  db_percent=None if db_axis is None else db_axis.percent,
802
996
  db_reset_generation=None if db_axis is None else db_axis.reset_generation,
997
+ protocol_generation=protocol,
998
+ account_key=account,
999
+ axis=axis,
803
1000
  )
804
1001
 
805
1002
 
@@ -807,6 +1004,8 @@ def _pending_kernel_attempt(
807
1004
  pending: PendingDrop,
808
1005
  reduced: AxisValue,
809
1006
  db_axis: AxisProjection | None,
1007
+ *,
1008
+ scope=None,
810
1009
  ) -> PendingDrop | None:
811
1010
  """Return the persisted state for one bounded kernel attempt.
812
1011
 
@@ -815,7 +1014,7 @@ def _pending_kernel_attempt(
815
1014
  plan. A successful post-record re-reduction clears it from DB truth; an
816
1015
  unchanged projection retains this bounded retry state for the next tick.
817
1016
  """
818
- signature = _build_retry_signature(reduced, db_axis)
1017
+ signature = _build_retry_signature(reduced, db_axis, scope=scope)
819
1018
  if pending.kernel_stage == "suppressed":
820
1019
  if pending.retry_signature == signature:
821
1020
  return None
@@ -888,7 +1087,8 @@ def _supporters_retracted(
888
1087
 
889
1088
 
890
1089
  def _hold_pending(
891
- pending: PendingDrop | None, db_axis: AxisProjection | None, now_epoch: int
1090
+ pending: PendingDrop | None, db_axis: AxisProjection | None, now_epoch: int,
1091
+ *, scope=None, drop_allowed: bool = True,
892
1092
  ) -> _AxisDecision:
893
1093
  """Decide a pending drop with no eligible candidate on its axis.
894
1094
 
@@ -910,10 +1110,13 @@ def _hold_pending(
910
1110
  if db_axis.percent <= pending.retained.percent:
911
1111
  # The database already holds the drop, so the record has done its work.
912
1112
  return _AxisDecision("WRITE_CONTROL", None, None)
913
- if now_epoch < pending.deadline_at:
1113
+ if now_epoch < pending.deadline_at or not drop_allowed:
1114
+ # #825/#832 spec 5.6 item 7: while the account lacks either recovery
1115
+ # certificate a drop below the database baseline is not published at
1116
+ # its deadline either. It stays pending, with no attempt spent.
914
1117
  return _AxisDecision("NOOP", None, pending)
915
1118
  publish = _retained_axis_value(pending.retained, db_axis)
916
- attempted = _pending_kernel_attempt(pending, publish, db_axis)
1119
+ attempted = _pending_kernel_attempt(pending, publish, db_axis, scope=scope)
917
1120
  if attempted is not None:
918
1121
  return _AxisDecision("PUBLISH_DB", publish, attempted)
919
1122
  # The bounded attempts are spent and no contributor remains that could
@@ -928,28 +1131,93 @@ def _reduce_axis(
928
1131
  pending: PendingDrop | None,
929
1132
  tombstone: Tombstone | None,
930
1133
  now_epoch: int,
1134
+ *,
1135
+ scope=None,
1136
+ drop_allowed: bool = True,
1137
+ evidence: AxisEvidence | None = None,
1138
+ member_facts: Mapping[str, object] | None = None,
931
1139
  ) -> _AxisDecision:
1140
+ # #797 spec 4.4 (Q3): the per-member refusal facts
1141
+ # (`_lib_stale_proof.MemberFacts`, keyed by contributor token) are
1142
+ # resolved before the kernel. None, or no refused member, reduces
1143
+ # exactly as before.
1144
+ refused = frozenset(
1145
+ token for token, facts in (member_facts or {}).items() if facts.refused)
932
1146
  reduced_data = _reduced_candidate(
933
1147
  candidates, axis=axis, tombstone=tombstone, db_axis=db_axis
934
1148
  )
935
1149
  if reduced_data is None:
936
- return _hold_pending(pending, db_axis, now_epoch)
1150
+ return _hold_pending(pending, db_axis, now_epoch, scope=scope,
1151
+ drop_allowed=drop_allowed)
937
1152
  reduced, in_window = reduced_data
938
1153
  key = _axis_value_key(reduced)
939
1154
 
940
1155
  if db_axis is None:
1156
+ verdict = classify_absent_axis(evidence, key, axis=axis)
1157
+ if verdict == CLASS_FLOOR:
1158
+ # Spec 4.3 revision 6 (*Floor retired*, Codex design-fix-round-6
1159
+ # Q2): the group's admitted floor retired every accepted reading.
1160
+ # Only contributors captured at or after the floor are the
1161
+ # ordinary first post-floor reading; one captured before it is
1162
+ # stale and is never relabelled as post-floor. The reading opens
1163
+ # no consensus and spends no attempt, under either gate.
1164
+ # #797 spec 4.4: a refused member is excluded from the post-floor
1165
+ # set; every other member keeps the receipt-floor test.
1166
+ post = tuple((candidate, value) for candidate, value in in_window
1167
+ if candidate.token not in refused
1168
+ and candidate.received_at >= int(evidence.floor_epoch))
1169
+ if not post:
1170
+ verdict = CLASS_STALE
1171
+ else:
1172
+ maximum = max(value.percent for _, value in post)
1173
+ reduced = AxisValue(
1174
+ maximum,
1175
+ min(value.raw_resets_at for _, value in post
1176
+ if value.percent == maximum),
1177
+ canonical_key=key)
1178
+ verdict = CLASS_PUBLISH
1179
+ if verdict == CLASS_UNKNOWN:
1180
+ # #825/#832 review finding P1-2 (Codex fix-round-4 Q2): a missing
1181
+ # projection alone proves no empty account — it cannot tell a
1182
+ # first reading from a drop below a baseline it does not show.
1183
+ # Only classification evidence does (spec 4.3 revision 5,
1184
+ # design-fix-round-5 Q1): a proven-empty baseline, or an expired
1185
+ # five-hour predecessor whose window this reading's strictly
1186
+ # follows. Without it the reading is unknown: never published,
1187
+ # and a pending drop's evidence is kept. Fix round K13 (review
1188
+ # cycle 7 P1-2): whatever the drop gate — an open gate is no
1189
+ # classification evidence either.
1190
+ return _AxisDecision("NOOP", None, pending, unknown=True)
1191
+ if verdict == CLASS_STALE:
1192
+ # Spec 4.3 revision 6 (*Stale expired window*, *Floor retired*):
1193
+ # the existing older-window disposition — nothing published on
1194
+ # this axis, no consensus opened, the pending drop kept.
1195
+ return _hold_pending(pending, None, now_epoch, scope=scope,
1196
+ drop_allowed=drop_allowed)
1197
+ if (evidence.kind == EVIDENCE_FLOOR_RETIRED and pending is not None
1198
+ and pending.canonical_key > int(evidence.group_key)):
1199
+ # Pending state retires only where the floor proves it obsolete:
1200
+ # a pending drop of a newer window is not the retired group's.
1201
+ return _AxisDecision("PUBLISH_DB", reduced, pending)
941
1202
  return _AxisDecision("PUBLISH_DB", reduced, None)
942
1203
  if key > db_axis.canonical_key:
943
1204
  return _AxisDecision("PUBLISH_DB", reduced, None)
944
1205
  if key < db_axis.canonical_key:
945
1206
  # A stale older window says nothing about the pending newer one.
946
- return _hold_pending(pending, db_axis, now_epoch)
1207
+ return _hold_pending(pending, db_axis, now_epoch, scope=scope,
1208
+ drop_allowed=drop_allowed)
947
1209
  # `reduced` is the MAXIMUM across active contributors, so it can equal or
948
1210
  # exceed the database value while a lower contributor is reporting a real
949
1211
  # drop. The maximum decides what is displayed; it may not erase the
950
1212
  # evidence that a drop is pending, which is why the low is read separately
951
1213
  # here and why a rise is a publication rather than a branch of its own.
952
- rising = reduced.percent > db_axis.percent
1214
+ # #797 spec 4.4 (Q3): `rising` and the published value come from the
1215
+ # members that are not refused; low evidence, supporter retraction,
1216
+ # unanimity, pending membership, `reduced_percent`, deadlines and attempt
1217
+ # accounting keep the unfiltered set. The eligible value applies at every
1218
+ # rising return (plain, armed, cancelled and reconciled).
1219
+ rise = _eligible_rise(reduced, in_window, refused, db_axis)
1220
+ rising = rise is not None and rise.percent > db_axis.percent
953
1221
  low = min(value.percent for _, value in in_window)
954
1222
  armed = (
955
1223
  pending is not None
@@ -961,7 +1229,7 @@ def _reduce_axis(
961
1229
  if rising:
962
1230
  # The rise moves the baseline and no drop is pending against the old
963
1231
  # one, so there is nothing to preserve.
964
- return _AxisDecision("PUBLISH_DB", reduced, None)
1232
+ return _AxisDecision("PUBLISH_DB", rise, None)
965
1233
  if low >= db_axis.percent:
966
1234
  return _AxisDecision(
967
1235
  "WRITE_CONTROL" if pending is not None else "NOOP", None, None
@@ -977,7 +1245,7 @@ def _reduce_axis(
977
1245
  # A cancelled or already-recorded drop stops being pending, and a rise still
978
1246
  # publishes on the way out.
979
1247
  cancelled = _AxisDecision(
980
- "PUBLISH_DB" if rising else "WRITE_CONTROL", reduced if rising else None, None
1248
+ "PUBLISH_DB" if rising else "WRITE_CONTROL", rise if rising else None, None
981
1249
  )
982
1250
  if _supporters_retracted(pending, in_window, db_axis):
983
1251
  return cancelled
@@ -1010,15 +1278,20 @@ def _reduce_axis(
1010
1278
  # reporting a higher number erased another session's real reset. The
1011
1279
  # retained low keeps its meaning, because the baseline only moved UP —
1012
1280
  # a low armed below the old baseline is still a drop below the new one.
1013
- return _AxisDecision("PUBLISH_DB", reduced, updated)
1281
+ return _AxisDecision("PUBLISH_DB", rise, updated)
1014
1282
 
1015
1283
  unanimous = (
1016
1284
  reduced.percent < db_axis.percent
1017
1285
  and bool(updated.contributors)
1018
1286
  and all(item.satisfied for item in updated.contributors.values())
1019
1287
  )
1020
- if unanimous:
1021
- attempted = _pending_kernel_attempt(updated, reduced, db_axis)
1288
+ if not drop_allowed:
1289
+ # #825/#832 spec 5.6 item 7: withheld — neither consensus nor the
1290
+ # deadline publishes a drop below the baseline, and no bounded attempt
1291
+ # is spent; the record stays pending with its evidence.
1292
+ pass
1293
+ elif unanimous:
1294
+ attempted = _pending_kernel_attempt(updated, reduced, db_axis, scope=scope)
1022
1295
  if attempted is not None:
1023
1296
  return _AxisDecision("PUBLISH_DB", reduced, attempted)
1024
1297
  elif now_epoch >= updated.deadline_at:
@@ -1027,7 +1300,7 @@ def _reduce_axis(
1027
1300
  # all-contributor maximum, never the old high, and never a fresh
1028
1301
  # generation over the same drop.
1029
1302
  publish = _retained_axis_value(updated.retained, db_axis)
1030
- attempted = _pending_kernel_attempt(updated, publish, db_axis)
1303
+ attempted = _pending_kernel_attempt(updated, publish, db_axis, scope=scope)
1031
1304
  if attempted is not None:
1032
1305
  return _AxisDecision("PUBLISH_DB", publish, attempted)
1033
1306
  # The bounded attempts are spent against an unchanged signature, so this
@@ -1049,6 +1322,25 @@ def _reduce_axis(
1049
1322
  )
1050
1323
 
1051
1324
 
1325
+ def _eligible_rise(
1326
+ reduced: AxisValue,
1327
+ in_window: tuple[tuple[Candidate, AxisValue], ...],
1328
+ refused: frozenset,
1329
+ db_axis: AxisProjection,
1330
+ ) -> AxisValue | None:
1331
+ """The same-window maximum over members that are not refused (#797 spec
1332
+ 4.4), or None when every member is refused. Without a refused member it
1333
+ is ``reduced`` itself."""
1334
+ if not refused or not any(c.token in refused for c, _ in in_window):
1335
+ return reduced
1336
+ eligible = [value.percent for candidate, value in in_window
1337
+ if candidate.token not in refused]
1338
+ if not eligible:
1339
+ return None
1340
+ return AxisValue(max(eligible), db_axis.raw_resets_at,
1341
+ canonical_key=reduced.canonical_key)
1342
+
1343
+
1052
1344
  def reduce_candidates(
1053
1345
  candidates: tuple[Candidate, ...] | list[Candidate],
1054
1346
  *,
@@ -1056,12 +1348,36 @@ def reduce_candidates(
1056
1348
  control: ControlState,
1057
1349
  tombstones: Mapping[str, Tombstone | None],
1058
1350
  now_epoch: int,
1351
+ account_key: str | None = None,
1352
+ drop_consensus_allowed: bool = True,
1353
+ member_facts: Mapping[str, Mapping[str, object]] | None = None,
1059
1354
  ) -> ReductionDecision:
1060
1355
  """Reduce active candidates independently for 5h and 7d axes.
1061
1356
 
1062
1357
  The output describes only a pure state transition. The caller owns
1063
1358
  filesystem persistence, DB reconciliation, and invoking the record kernel.
1359
+
1360
+ ``account_key`` names the bound partition (#825/#832 spec 4.3): every
1361
+ retry signature minted here then carries the namespace
1362
+ ``(PROTOCOL_GENERATION, account_key, axis)``. The caller passes only that
1363
+ partition's candidates, control and tombstones.
1364
+
1365
+ ``drop_consensus_allowed`` is the outcome-quarantine control gate
1366
+ (#825/#832 spec 5.6 item 7). False while the account lacks either recovery
1367
+ certificate: a pending drop below the database baseline is then published
1368
+ neither by consensus nor at its deadline, and stays pending; rises and new
1369
+ windows publish as always. An axis with no projected baseline publishes
1370
+ under either gate only when ``db`` carries classification evidence for
1371
+ it (`classify_absent_axis`: a first reading, a new window or the first
1372
+ reading after a retiring floor); a stale reading takes the older-window
1373
+ disposition, and an unclassifiable one is named in ``unknown_axes`` (fix
1374
+ rounds K13 and K14c).
1375
+
1376
+ ``member_facts`` maps an axis to its per-member refusal facts (#797 spec
1377
+ 4.4, `_lib_stale_proof.MemberFacts` by contributor token). None, or an
1378
+ axis without facts, means every member is eligible.
1064
1379
  """
1380
+ facts = member_facts or {}
1065
1381
  active = tuple(
1066
1382
  candidate
1067
1383
  for candidate in candidates
@@ -1074,6 +1390,10 @@ def reduce_candidates(
1074
1390
  control.pending_drops.get("fiveHour"),
1075
1391
  tombstones.get("fiveHour"),
1076
1392
  now_epoch,
1393
+ scope=None if account_key is None else (PROTOCOL_GENERATION, account_key, "fiveHour"),
1394
+ drop_allowed=drop_consensus_allowed,
1395
+ evidence=axis_evidence(db, "fiveHour"),
1396
+ member_facts=facts.get("fiveHour"),
1077
1397
  )
1078
1398
  seven = _reduce_axis(
1079
1399
  "sevenDay",
@@ -1082,17 +1402,26 @@ def reduce_candidates(
1082
1402
  control.pending_drops.get("sevenDay"),
1083
1403
  tombstones.get("sevenDay"),
1084
1404
  now_epoch,
1405
+ scope=None if account_key is None else (PROTOCOL_GENERATION, account_key, "sevenDay"),
1406
+ drop_allowed=drop_consensus_allowed,
1407
+ evidence=axis_evidence(db, "sevenDay"),
1408
+ member_facts=facts.get("sevenDay"),
1085
1409
  )
1086
1410
  next_control = ControlState(
1087
1411
  db_projection=db,
1088
1412
  pending_drops={"fiveHour": five.pending, "sevenDay": seven.pending},
1089
1413
  )
1414
+ unknown = frozenset(
1415
+ axis for axis, decided in (("fiveHour", five), ("sevenDay", seven))
1416
+ if decided.unknown)
1090
1417
  if five.action == "PUBLISH_DB" or seven.action == "PUBLISH_DB":
1091
1418
  return ReductionDecision(
1092
1419
  "PUBLISH_DB",
1093
1420
  next_control,
1094
1421
  PublicationPlan(seven_day=seven.candidate, five_hour=five.candidate),
1422
+ unknown_axes=unknown,
1095
1423
  )
1096
1424
  if five.action == "WRITE_CONTROL" or seven.action == "WRITE_CONTROL":
1097
- return ReductionDecision("WRITE_CONTROL", next_control)
1098
- return ReductionDecision("NOOP", next_control)
1425
+ return ReductionDecision("WRITE_CONTROL", next_control,
1426
+ unknown_axes=unknown)
1427
+ return ReductionDecision("NOOP", next_control, unknown_axes=unknown)