cctally 1.108.0 → 1.110.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +6 -6
  3. package/bin/_cctally_account.py +4 -0
  4. package/bin/_cctally_alerts.py +6 -2
  5. package/bin/_cctally_cache.py +1652 -166
  6. package/bin/_cctally_config.py +3 -2
  7. package/bin/_cctally_core.py +594 -33
  8. package/bin/_cctally_dashboard.py +1585 -201
  9. package/bin/_cctally_dashboard_conversation.py +494 -48
  10. package/bin/_cctally_dashboard_envelope.py +167 -11
  11. package/bin/_cctally_dashboard_share.py +154 -36
  12. package/bin/_cctally_dashboard_sources.py +2615 -422
  13. package/bin/_cctally_db.py +678 -5
  14. package/bin/_cctally_diagnosis_sources.py +238 -26
  15. package/bin/_cctally_doctor.py +99 -18
  16. package/bin/_cctally_five_hour.py +1295 -40
  17. package/bin/_cctally_forecast.py +124 -28
  18. package/bin/_cctally_journal.py +408 -38
  19. package/bin/_cctally_milestone_history.py +150 -14
  20. package/bin/_cctally_parser.py +5 -0
  21. package/bin/_cctally_percent_breakdown.py +6 -1
  22. package/bin/_cctally_project.py +48 -76
  23. package/bin/_cctally_quota.py +39 -7
  24. package/bin/_cctally_quota_model.py +17 -2
  25. package/bin/_cctally_record.py +1647 -443
  26. package/bin/_cctally_rederive.py +899 -30
  27. package/bin/_cctally_refresh.py +2 -1
  28. package/bin/_cctally_share.py +24 -5
  29. package/bin/_cctally_source_analytics.py +1035 -78
  30. package/bin/_cctally_statusline.py +83 -19
  31. package/bin/_cctally_store.py +61 -4
  32. package/bin/_cctally_transcript.py +27 -2
  33. package/bin/_cctally_tui.py +264 -76
  34. package/bin/_cctally_weekrefs.py +28 -5
  35. package/bin/_lib_blocks.py +127 -20
  36. package/bin/_lib_cache_report.py +20 -5
  37. package/bin/_lib_codex_conversation_query.py +297 -64
  38. package/bin/_lib_codex_hooks.py +102 -0
  39. package/bin/_lib_codex_metadata.py +207 -0
  40. package/bin/_lib_conversation.py +119 -0
  41. package/bin/_lib_conversation_dispatch.py +31 -1
  42. package/bin/_lib_conversation_query.py +315 -51
  43. package/bin/_lib_conversation_retention.py +413 -25
  44. package/bin/_lib_dashboard_sources.py +267 -7
  45. package/bin/_lib_diagnosis.py +10 -0
  46. package/bin/_lib_diff_kernel.py +12 -1
  47. package/bin/_lib_doctor.py +230 -8
  48. package/bin/_lib_ingest_frontier.py +1366 -49
  49. package/bin/_lib_journal.py +19 -2
  50. package/bin/_lib_merge_gate.py +307 -0
  51. package/bin/_lib_pricing.py +588 -29
  52. package/bin/_lib_record.py +130 -12
  53. package/bin/_lib_rederive.py +903 -2
  54. package/bin/_lib_render.py +9 -3
  55. package/bin/_lib_retained_size.py +27 -2
  56. package/bin/_lib_segment_summary.py +66 -8
  57. package/bin/_lib_share_templates.py +28 -18
  58. package/bin/_lib_snapshot_cache.py +115 -1
  59. package/bin/_lib_source_retry.py +320 -0
  60. package/bin/_lib_statusline_candidates.py +400 -36
  61. package/bin/_lib_tick_stats.py +119 -0
  62. package/bin/_lib_view_models.py +256 -76
  63. package/bin/cctally +69 -0
  64. package/dashboard/static/assets/ConversationsView-BB9vUiL6.js +72 -0
  65. package/dashboard/static/assets/{DoctorModal-CWn3U4Wl.js → DoctorModal-DFY2iPDf.js} +1 -1
  66. package/dashboard/static/assets/ModalRoot-DvRKHbQE.js +1 -0
  67. package/dashboard/static/assets/ProjectsDrillPanel-DAv39uSo.js +1 -0
  68. package/dashboard/static/assets/SourceDetailModal-DSAWo2Fp.js +1 -0
  69. package/dashboard/static/assets/{UpdateModal-D3m8GG6V.js → UpdateModal-R_9dOVnh.js} +3 -3
  70. package/dashboard/static/assets/dashboardStream.shared-worker-DbG6Ef0T.js +1 -0
  71. package/dashboard/static/assets/index-COhxe3vU.css +1 -0
  72. package/dashboard/static/assets/index-DdU62sov.js +13 -0
  73. package/dashboard/static/assets/outlineNavigation-BOCLcRa6.js +9 -0
  74. package/dashboard/static/assets/useKeymap-CW7Up0Ay.js +1 -0
  75. package/dashboard/static/dashboard.html +3 -3
  76. package/package.json +4 -1
  77. package/dashboard/static/assets/ConversationsView-BOSaBRtu.js +0 -72
  78. package/dashboard/static/assets/ModalRoot-SR070V6c.js +0 -1
  79. package/dashboard/static/assets/ProjectsDrillPanel-QLI9i5mZ.js +0 -1
  80. package/dashboard/static/assets/SourceDetailModal-pv0wlFxl.js +0 -1
  81. package/dashboard/static/assets/dashboardStream.shared-worker-1XTMV3nr.js +0 -1
  82. package/dashboard/static/assets/index-BHu4mxd8.css +0 -1
  83. package/dashboard/static/assets/index-CIWsbux3.js +0 -13
  84. package/dashboard/static/assets/outlineNavigation-CJvKqmLV.js +0 -9
  85. package/dashboard/static/assets/useKeymap-ffqsS5G0.js +0 -1
@@ -40,10 +40,12 @@ import json
40
40
  import os
41
41
  import pickle
42
42
  import re
43
+ import signal
43
44
  import shlex
44
45
  import sqlite3
45
46
  import sys
46
47
  import threading
48
+ import time
47
49
  from collections import defaultdict
48
50
  from dataclasses import dataclass, field
49
51
  from functools import cached_property
@@ -71,6 +73,14 @@ UTC = dt.timezone.utc
71
73
  # bounded wait rather than an immediate `database is locked`.
72
74
  READ_BUSY_TIMEOUT_MS = 4000
73
75
 
76
+ # A provider read can lose its component-local coherence check to the writer
77
+ # that is publishing the next generation. Retry the whole provider build a
78
+ # small number of times while a monotonic admission budget remains. The
79
+ # `_establish` retry remains deliberately narrower: it still rereads one
80
+ # component once and refuses a second divergence within that build.
81
+ DIAGNOSIS_RETRY_MAX_ATTEMPTS = 3
82
+ DIAGNOSIS_RETRY_DEADLINE_SECONDS = 1.0
83
+
74
84
  _STORE_PATHS = {
75
85
  "cache": "CACHE_DB_PATH",
76
86
  "stats": "DB_PATH",
@@ -1045,28 +1055,22 @@ def _compiled_codex_pricer(model: str, speed: str):
1045
1055
  reasoning_output_tokens: int) -> float:
1046
1056
  del reasoning_output_tokens
1047
1057
  non_cached_input = max(0, input_tokens - cached_input_tokens)
1058
+ long_context = input_tokens > threshold
1048
1059
  if non_cached_input <= 0 or not input_rate:
1049
1060
  input_cost = 0.0
1050
- elif non_cached_input > threshold and input_tier is not None:
1051
- input_cost = (threshold * input_rate
1052
- + (non_cached_input - threshold) * input_tier)
1053
1061
  else:
1054
- input_cost = non_cached_input * input_rate
1062
+ input_cost = non_cached_input * (
1063
+ input_tier if long_context and input_tier is not None else input_rate)
1055
1064
  if cached_input_tokens <= 0 or not cache_rate:
1056
1065
  cached_input_cost = 0.0
1057
- elif cached_input_tokens > threshold and cache_tier is not None:
1058
- cached_input_cost = (
1059
- threshold * cache_rate
1060
- + (cached_input_tokens - threshold) * cache_tier)
1061
1066
  else:
1062
- cached_input_cost = cached_input_tokens * cache_rate
1067
+ cached_input_cost = cached_input_tokens * (
1068
+ cache_tier if long_context and cache_tier is not None else cache_rate)
1063
1069
  if output_tokens <= 0 or not output_rate:
1064
1070
  output_cost = 0.0
1065
- elif output_tokens > threshold and output_tier is not None:
1066
- output_cost = (threshold * output_rate
1067
- + (output_tokens - threshold) * output_tier)
1068
1071
  else:
1069
- output_cost = output_tokens * output_rate
1072
+ output_cost = output_tokens * (
1073
+ output_tier if long_context and output_tier is not None else output_rate)
1070
1074
  base = input_cost + cached_input_cost + output_cost
1071
1075
  if speed == "fast":
1072
1076
  base *= multiplier
@@ -2793,6 +2797,23 @@ def _evaluate_codex_short_high_context(scope: DiagnosisScope,
2793
2797
  excludes it from the delegated one. The consequence is stated rather than
2794
2798
  hidden: this class is an identifiable subset of unknown completeness on
2795
2799
  Codex, not only the fan-out class.
2800
+
2801
+ #834 S2 (#800): RAW-EVIDENCE PRESENCE IS CLASSIFIED PER CONVERSATION KEY,
2802
+ before origin, turn count or high-context qualification. The population and
2803
+ the origin come from ACCOUNTING rows in `cache.db` while the prompt count
2804
+ comes from the TRANSCRIPT store, so a conversation the accounting names and
2805
+ the transcript store does not retain reached the turn count as zero and was
2806
+ reported as a decided non-contributor. That overstated
2807
+ `evaluabilityCoverage` — it published 1.0 over a window whose transcripts
2808
+ were absent — and it dropped that conversation's cost silently. The Claude
2809
+ twin returns `_unestablished()` in the same situation.
2810
+
2811
+ A GLOBAL test of whether the candidate query returned any row is not
2812
+ sufficient, which is why the classification is per key: candidate rows are
2813
+ stored per conversation, so a global guard still treats a conversation with
2814
+ no retained transcript as evaluated with zero turns whenever some OTHER
2815
+ conversation has rows. A key with rows none of which qualify stays a decided
2816
+ non-contributor, because that is a measurement rather than an absence.
2796
2817
  """
2797
2818
  facts = bundle.facts
2798
2819
  by_conversation, _labels = facts.grouped_entries["session"]
@@ -2813,6 +2834,17 @@ def _evaluate_codex_short_high_context(scope: DiagnosisScope,
2813
2834
  overflowed.add(key)
2814
2835
  continue
2815
2836
  candidate_rows[key].append(row)
2837
+ # Presence of RAW candidate evidence, per key. An overflowed key HAS
2838
+ # evidence — its rows exist and ran past the normalize budget — so it is
2839
+ # present here and reports `scan_budget_exhausted` below, which is the
2840
+ # truthful cause for it.
2841
+ has_evidence = {key for key in scoped
2842
+ if candidate_rows.get(key) or key in overflowed}
2843
+ if not has_evidence:
2844
+ # No key in the window retains a single candidate row, so nothing about
2845
+ # this class was established at all. Matching the Claude twin, which
2846
+ # returns the same when its session population is empty.
2847
+ return _unestablished()
2816
2848
  prompt_counts: dict[str, int] = {}
2817
2849
 
2818
2850
  def _prompt_count(key: str) -> int:
@@ -2848,6 +2880,13 @@ def _evaluate_codex_short_high_context(scope: DiagnosisScope,
2848
2880
  # the published maximum came from a per-turn capacity.
2849
2881
  session_level: dict[str, bool] = {}
2850
2882
  for key in scoped:
2883
+ if key not in has_evidence:
2884
+ # NOT decided. The predicate needs a human turn count and the
2885
+ # transcript store retains nothing to count, so this conversation
2886
+ # leaves the evaluated population rather than being reported as a
2887
+ # non-contributor with zero turns.
2888
+ unevaluable[key] = kernel.GAP_NO_RETAINED_TRANSCRIPT
2889
+ continue
2851
2890
  thread = threads.get(key)
2852
2891
  origin = _codex_origin(
2853
2892
  thread["root_thread_id"] if thread is not None else None)
@@ -3809,23 +3848,34 @@ def _assign_entries_to_blocks(entries: Sequence[AccountingEntry],
3809
3848
  most one window, so no entry is ever counted in two pools. An entry
3810
3849
  matching no compatible window is a coverage gap, reported through
3811
3850
  `unmatched`, and appears in no block.
3851
+
3852
+ Which of several compatible containing windows wins is
3853
+ `_lib_blocks.resolve_owning_window`'s decision (issue #751a): the
3854
+ earliest reset, ties broken by canonical key. This used to take the
3855
+ first containing window in iteration order, and Claude blocks arrive
3856
+ ordered by START, so a window truncated by a credit — one that begins
3857
+ later and resets earlier — lost its own entries to the wider window
3858
+ that began before it.
3812
3859
  """
3860
+ import _lib_blocks
3861
+
3862
+ candidates: dict[tuple, list] = {}
3863
+ for block in blocks:
3864
+ candidates.setdefault((block.root_key, block.pool), []).append(
3865
+ _lib_blocks.OwnedWindow(
3866
+ key=block.key, start=block.start_at, reset=block.end_at,
3867
+ )
3868
+ )
3813
3869
  assigned: dict[str, list[AccountingEntry]] = {}
3814
3870
  unmatched: list[AccountingEntry] = []
3815
3871
  for entry in entries:
3816
- match = None
3817
- for block in blocks:
3818
- if block.root_key != entry.root_key:
3819
- continue
3820
- if block.pool != entry.pool:
3821
- continue
3822
- if block.start_at <= entry.timestamp < block.end_at:
3823
- match = block
3824
- break
3825
- if match is None:
3872
+ owner = _lib_blocks.resolve_owning_window(
3873
+ entry, candidates.get((entry.root_key, entry.pool), ()),
3874
+ )
3875
+ if owner is None:
3826
3876
  unmatched.append(entry)
3827
3877
  continue
3828
- assigned.setdefault(match.key, []).append(entry)
3878
+ assigned.setdefault(owner.key, []).append(entry)
3829
3879
  return assigned, unmatched
3830
3880
 
3831
3881
 
@@ -4299,11 +4349,43 @@ def build_provider_diagnosis(scope: DiagnosisScope, *,
4299
4349
  )
4300
4350
 
4301
4351
 
4352
+ def _build_provider_with_retry(scope: DiagnosisScope, *,
4353
+ transcripts_visible: bool
4354
+ ) -> kernel.ProviderResult:
4355
+ """Build one provider, admitting only bounded fresh retries.
4356
+
4357
+ A `generation_incoherent` result means the provider's read crossed two
4358
+ writes during one attempt. Restarting the provider build is safe because
4359
+ every attempt owns fresh read-only connections and facts. Other typed
4360
+ establishment failures and unexpected exceptions retain their existing
4361
+ handling at the caller; they are never converted into retries.
4362
+ """
4363
+ deadline = time.monotonic() + DIAGNOSIS_RETRY_DEADLINE_SECONDS
4364
+ for attempt in range(DIAGNOSIS_RETRY_MAX_ATTEMPTS):
4365
+ try:
4366
+ return build_provider_diagnosis(
4367
+ scope, transcripts_visible=transcripts_visible)
4368
+ except EstablishmentFailure as exc:
4369
+ if exc.code != EstablishmentError.GENERATION_INCOHERENT.value:
4370
+ raise
4371
+ if attempt + 1 >= DIAGNOSIS_RETRY_MAX_ATTEMPTS:
4372
+ raise
4373
+ remaining = deadline - time.monotonic()
4374
+ if remaining <= 0:
4375
+ raise
4376
+ # Give an in-flight writer a chance to finish before reopening
4377
+ # every store. Normal coherent reads pay no delay.
4378
+ time.sleep(min(0.1 * (attempt + 1), remaining))
4379
+ if time.monotonic() >= deadline:
4380
+ raise
4381
+ raise AssertionError("provider diagnosis retry loop did not return")
4382
+
4383
+
4302
4384
  def _build_provider_task(args) -> kernel.ProviderResult:
4303
4385
  """Pickle-safe provider build for the independent `all` branches."""
4304
4386
  scope, transcripts_visible = args
4305
4387
  try:
4306
- result = build_provider_diagnosis(
4388
+ result = _build_provider_with_retry(
4307
4389
  scope, transcripts_visible=transcripts_visible)
4308
4390
  except EstablishmentFailure as exc:
4309
4391
  if exc.code != EstablishmentError.STORE_UNAVAILABLE.value:
@@ -4373,6 +4455,136 @@ def _spawn_worker_init(cctally_path: str,
4373
4455
  setattr(_cctally_core, name, pathlib.Path(raw))
4374
4456
 
4375
4457
 
4458
+ _DIAGNOSIS_PROCESS_TERMINATE_GRACE_SECONDS = 0.5
4459
+
4460
+
4461
+ def _bounded_process_entry(result_path: str, task, args,
4462
+ initializer, initargs) -> None:
4463
+ """Run one pickle-safe task in its own killable process group."""
4464
+ if hasattr(os, "setsid"):
4465
+ try:
4466
+ os.setsid()
4467
+ except OSError:
4468
+ # The parent still terminates this process directly if the group
4469
+ # was not established. No descendants exist before this point.
4470
+ pass
4471
+ try:
4472
+ if initializer is not None:
4473
+ initializer(*initargs)
4474
+ payload = ("ok", task(*args))
4475
+ except EstablishmentFailure as exc:
4476
+ payload = ("establishment", exc.code, exc.message)
4477
+ except BaseException as exc:
4478
+ payload = ("unexpected", type(exc).__name__, str(exc))
4479
+ with open(result_path, "wb") as stream:
4480
+ pickle.dump(payload, stream, protocol=5)
4481
+
4482
+
4483
+ def _terminate_process_group(process) -> None:
4484
+ """Terminate and reap one bounded worker plus any provider descendants."""
4485
+ grouped = False
4486
+ if process.pid is not None and hasattr(os, "killpg"):
4487
+ try:
4488
+ os.killpg(process.pid, signal.SIGTERM)
4489
+ grouped = True
4490
+ except ProcessLookupError:
4491
+ pass
4492
+ except OSError:
4493
+ pass
4494
+ if process.is_alive() and not grouped:
4495
+ process.terminate()
4496
+ process.join(_DIAGNOSIS_PROCESS_TERMINATE_GRACE_SECONDS)
4497
+ if grouped and process.pid is not None:
4498
+ try:
4499
+ # The leader may have exited on SIGTERM while a provider child
4500
+ # ignored it. The process group remains addressable until every
4501
+ # descendant is gone, so always finish the group with SIGKILL.
4502
+ os.killpg(process.pid, signal.SIGKILL)
4503
+ except ProcessLookupError:
4504
+ pass
4505
+ except OSError:
4506
+ pass
4507
+ elif process.is_alive():
4508
+ process.kill()
4509
+ process.join(_DIAGNOSIS_PROCESS_TERMINATE_GRACE_SECONDS)
4510
+
4511
+
4512
+ def _run_process_with_deadline(task, args, *, timeout_seconds: float,
4513
+ initializer=None, initargs=()):
4514
+ """Run TASK in a one-shot process and forcibly retire it at the deadline."""
4515
+ import multiprocessing
4516
+ import tempfile
4517
+
4518
+ if timeout_seconds <= 0:
4519
+ raise TimeoutError("diagnosis process deadline expired")
4520
+ methods = multiprocessing.get_all_start_methods()
4521
+ method = "forkserver" if "forkserver" in methods else "spawn"
4522
+ context = multiprocessing.get_context(method)
4523
+ fd, result_path = tempfile.mkstemp(prefix="cctally-diagnosis-")
4524
+ os.close(fd)
4525
+ process = context.Process(
4526
+ target=_bounded_process_entry,
4527
+ args=(result_path, task, args, initializer, initargs),
4528
+ name="cctally-diagnosis-process",
4529
+ )
4530
+ try:
4531
+ process.start()
4532
+ process.join(timeout_seconds)
4533
+ if process.is_alive():
4534
+ _terminate_process_group(process)
4535
+ raise TimeoutError("diagnosis process deadline expired")
4536
+ if process.exitcode != 0:
4537
+ raise RuntimeError(
4538
+ f"diagnosis process exited {process.exitcode}")
4539
+ try:
4540
+ with open(result_path, "rb") as stream:
4541
+ payload = pickle.load(stream)
4542
+ except (EOFError, OSError, pickle.PickleError) as exc:
4543
+ raise RuntimeError("diagnosis process returned no result") from exc
4544
+ finally:
4545
+ if process.is_alive():
4546
+ _terminate_process_group(process)
4547
+ try:
4548
+ os.unlink(result_path)
4549
+ except FileNotFoundError:
4550
+ pass
4551
+ process.close()
4552
+ if payload[0] == "ok":
4553
+ return payload[1]
4554
+ if payload[0] == "establishment":
4555
+ raise EstablishmentFailure(payload[1], payload[2])
4556
+ raise RuntimeError(f"{payload[1]}: {payload[2]}")
4557
+
4558
+
4559
+ def _build_diagnosis_process_task(scope: DiagnosisScope,
4560
+ measured_at: dt.datetime | None,
4561
+ transcripts_visible: bool):
4562
+ return build_diagnosis(
4563
+ scope, measured_at=measured_at,
4564
+ transcripts_visible=transcripts_visible,
4565
+ )
4566
+
4567
+
4568
+ def build_diagnosis_bounded(scope: DiagnosisScope, *,
4569
+ measured_at: dt.datetime | None = None,
4570
+ transcripts_visible: bool,
4571
+ timeout_seconds: float) -> kernel.DiagnosisReport:
4572
+ """Build one dashboard diagnosis in a process retired at TIMEOUT_SECONDS."""
4573
+ c = _cctally()
4574
+ paths = {
4575
+ name: str(getattr(_cctally_core, name))
4576
+ for name in ("CACHE_DB_PATH", "DB_PATH",
4577
+ "CONVERSATIONS_DB_PATH", "CONFIG_PATH")
4578
+ }
4579
+ return _run_process_with_deadline(
4580
+ _build_diagnosis_process_task,
4581
+ (scope, measured_at, transcripts_visible),
4582
+ timeout_seconds=timeout_seconds,
4583
+ initializer=_spawn_worker_init,
4584
+ initargs=(str(c.__file__), paths),
4585
+ )
4586
+
4587
+
4376
4588
  def _build_isolated_provider(args):
4377
4589
  """Run one provider in a portable, one-shot safe process worker."""
4378
4590
  import concurrent.futures
@@ -4421,7 +4633,7 @@ def build_diagnosis(scope: DiagnosisScope,
4421
4633
  label=scope.label,
4422
4634
  )
4423
4635
  try:
4424
- return build_provider_diagnosis(
4636
+ return _build_provider_with_retry(
4425
4637
  provider_scope, transcripts_visible=transcripts_visible)
4426
4638
  except EstablishmentFailure as exc:
4427
4639
  # A store that cannot be OPENED withholds its provider rather than
@@ -78,7 +78,9 @@ def _stats_ro_guarded():
78
78
 
79
79
  return _cctally_store.stats_open_guarded(
80
80
  _cctally_core.DB_PATH,
81
- connect=lambda p: sqlite3.connect(f"file:{p}?mode=ro", uri=True),
81
+ # #778: forward the opener's `cached_statements=0`.
82
+ connect=lambda p, **kw: sqlite3.connect(
83
+ f"file:{p}?mode=ro", uri=True, **kw),
82
84
  )
83
85
 
84
86
 
@@ -1126,9 +1128,10 @@ def _gather_quota_rate_change(c, rejection=None) -> "dict | None":
1126
1128
  return dict(active, assessed=True)
1127
1129
 
1128
1130
 
1129
- def _read_rollup_pricing_state(conn) -> "tuple[dict | None, str | None]":
1131
+ def _read_rollup_pricing_state(conn) -> "tuple[dict | None, str | None, object]":
1130
1132
  """Read the ordered-write guard's two ``cache_meta`` keys from ``conn``
1131
- (#705), returning ``(refusal_record, stored_fingerprint)``.
1133
+ (#705), returning
1134
+ ``(refusal_record, stored_fingerprint, fingerprint_observation)``.
1132
1135
 
1133
1136
  ONE reader for BOTH stores. The guard runs on the cache.db connection in
1134
1137
  ``sync_cache`` and on the conversations.db connection in
@@ -1145,8 +1148,19 @@ def _read_rollup_pricing_state(conn) -> "tuple[dict | None, str | None]":
1145
1148
  ordinary refusal (an older process against a newer store) apart from a
1146
1149
  value no version can parse, which no restart or rebuild clears.
1147
1150
 
1148
- Degrades to ``(None, None)`` on a store whose ``cache_meta`` cannot be
1149
- read, like every other probe in this gather.
1151
+ The fingerprint read goes through ``_cctally_cache
1152
+ ._read_pricing_fingerprint_observation`` — the SAME helper the three writer
1153
+ sites use — so doctor classifies a store exactly as the guard acts on it.
1154
+ Doctor adds a READ here, not a fourth authorization site, but the read it
1155
+ performed was collapsing ``sqlite3.OperationalError`` to absence, so a
1156
+ store it could not open reported "no refused pricing write recorded" and
1157
+ doctor exited OK over a store it had no evidence about (#728). The third
1158
+ element carries that distinction; the first two keep their shapes so every
1159
+ existing consumer of the record and the value is unaffected.
1160
+
1161
+ The refusal record still degrades to None on a store whose ``cache_meta``
1162
+ cannot be read, like every other probe in this gather — the observation is
1163
+ what reports that the read failed.
1150
1164
  """
1151
1165
  import _cctally_cache as _cc_sib
1152
1166
  record = None
@@ -1164,16 +1178,22 @@ def _read_rollup_pricing_state(conn) -> "tuple[dict | None, str | None]":
1164
1178
  except (ValueError, TypeError):
1165
1179
  parsed = None
1166
1180
  record = parsed if isinstance(parsed, dict) else {"__malformed__": True}
1167
- try:
1168
- fp_row = conn.execute(
1169
- "SELECT value FROM cache_meta WHERE key=?",
1170
- (_cc_sib.CONVERSATION_ROLLUP_PRICING_FP_KEY,),
1171
- ).fetchone()
1172
- except sqlite3.OperationalError:
1173
- fp_row = None
1174
- if fp_row and fp_row[0]:
1175
- stored_fp = str(fp_row[0])
1176
- return record, stored_fp
1181
+ observation = _cc_sib._read_pricing_fingerprint_observation(conn)
1182
+ # #769 S3: only a state whose read actually produced the store's value may
1183
+ # publish one. The `and observation.raw` half is DEFENCE IN DEPTH and is
1184
+ # currently unreachable, which the Tranche 2 review was right to say the
1185
+ # previous comment obscured: `_read_pricing_fingerprint_observation` passes
1186
+ # `raw=None` on every degraded return, so a DEGRADED observation carries no
1187
+ # value to publish today. The guard is kept because the classifier's own
1188
+ # signature permits `found=True, raw=..., error_kind=...`, so a future
1189
+ # degraded path COULD carry one — and publishing it as `stored_fp` would
1190
+ # route doctor's unparseable-value branch, whose remedy is "delete that
1191
+ # cache_meta row", at a row this process never managed to read. MALFORMED
1192
+ # stays included because that value WAS read, and it is the input the
1193
+ # unparseable diagnosis is built from.
1194
+ if observation.state in ("present", "malformed") and observation.raw:
1195
+ stored_fp = str(observation.raw)
1196
+ return record, stored_fp, observation
1177
1197
 
1178
1198
 
1179
1199
  def doctor_gather_state(
@@ -1466,6 +1486,29 @@ def _doctor_gather_state_impl(
1466
1486
  import _cctally_store as _store_mod
1467
1487
  conn = _store_mod.stats_open_guarded(_cctally_core.DB_PATH)
1468
1488
  try:
1489
+ # HELD-INCLUSIVE, and that is the decision rather than an
1490
+ # omission (#769 S11, #824). Two different freshness
1491
+ # questions exist and this leg asks the first of them:
1492
+ #
1493
+ # * PIPELINE freshness — did the write path run recently?
1494
+ # `data.latest_snapshot_age` remediates with "check
1495
+ # hooks are installed" and "hooks may be broken", so it
1496
+ # is asking whether ticks are still arriving. A
1497
+ # `weekly_observation_held` row is a real write by a
1498
+ # real tick carrying genuine five-hour evidence, so it
1499
+ # answers this question and must be counted. Excluding
1500
+ # held rows would make doctor report broken hooks
1501
+ # during a stretch when the hooks are working and only
1502
+ # the weekly meter is clamped.
1503
+ # * WEEKLY-AXIS freshness — how old is the newest genuine
1504
+ # weekly reading? That is what the status line, the
1505
+ # dashboard chip, `report`'s per-row stamp and the
1506
+ # hook's OAuth throttle ask, and every one of those
1507
+ # excludes held rows.
1508
+ #
1509
+ # Pinned by `test_doctor_latest_snapshot_age_counts_a_held_row`
1510
+ # so a later change that silently converts this leg to the
1511
+ # second question fails rather than passing quietly.
1469
1512
  try:
1470
1513
  row = conn.execute(
1471
1514
  "SELECT MAX(captured_at_utc) FROM weekly_usage_snapshots"
@@ -1520,11 +1563,19 @@ def _doctor_gather_state_impl(
1520
1563
  for cr in credit_rows:
1521
1564
  end_at = cr[1]
1522
1565
  evt_id = cr[0]
1566
+ # Held rows excluded: this reads a weekly VALUE
1567
+ # and compares it against a threshold, unlike the
1568
+ # freshness leg above, which asks whether the write
1569
+ # path is alive and therefore counts a held tick as
1570
+ # the real write it is (#769 S11, #824). The basis
1571
+ # of a held row carries the same `week_end_at`, so
1572
+ # the exclusion never empties this lookup.
1523
1573
  latest = conn.execute(
1524
1574
  """
1525
1575
  SELECT week_start_date, weekly_percent
1526
1576
  FROM weekly_usage_snapshots
1527
1577
  WHERE week_end_at = ?
1578
+ AND weekly_observation_held = 0
1528
1579
  ORDER BY captured_at_utc DESC, id DESC
1529
1580
  LIMIT 1
1530
1581
  """,
@@ -1571,6 +1622,7 @@ def _doctor_gather_state_impl(
1571
1622
  # `_cache_probe_allowed` gate as its neighbours.
1572
1623
  cache_rollup_pricing_refusal: "dict | None" = None
1573
1624
  cache_rollup_pricing_fp: "str | None" = None
1625
+ cache_rollup_pricing_observation = None
1574
1626
  try:
1575
1627
  if _cache_probe_allowed and _cctally_core.CACHE_DB_PATH.exists():
1576
1628
  conn = sqlite3.connect(str(_cctally_core.CACHE_DB_PATH))
@@ -1587,6 +1639,7 @@ def _doctor_gather_state_impl(
1587
1639
  (
1588
1640
  cache_rollup_pricing_refusal,
1589
1641
  cache_rollup_pricing_fp,
1642
+ cache_rollup_pricing_observation,
1590
1643
  ) = _read_rollup_pricing_state(conn)
1591
1644
  row = conn.execute(
1592
1645
  "SELECT COUNT(*), MAX(timestamp_utc) FROM session_entries"
@@ -1627,9 +1680,11 @@ def _doctor_gather_state_impl(
1627
1680
  conv_rollup_sync_in_progress = False
1628
1681
  conversations_db_page_count = None
1629
1682
  conversations_db_freelist_count = None
1683
+ conversations_reclaim_pending = None
1630
1684
  codex_prune_refusals: list[dict] = []
1631
1685
  conversation_rollup_pricing_refusal: "dict | None" = None
1632
1686
  conversation_rollup_pricing_fp: "str | None" = None
1687
+ conversation_rollup_pricing_observation = None
1633
1688
  try:
1634
1689
  if _cctally_core.CONVERSATIONS_DB_PATH.exists():
1635
1690
  # This gather also runs inside dashboard snapshot precompute. A
@@ -1651,6 +1706,15 @@ def _doctor_gather_state_impl(
1651
1706
  conversations_db_freelist_count = int(row[0])
1652
1707
  except sqlite3.Error:
1653
1708
  pass
1709
+ try:
1710
+ # #780: the durable reclaim backlog. Read here rather
1711
+ # than derived from the freelist, because the freelist
1712
+ # goes to zero while the bytes are still in the WAL.
1713
+ import _lib_conversation_retention as _conv_retention
1714
+ conversations_reclaim_pending = (
1715
+ _conv_retention.read_reclaim_pending(conn))
1716
+ except (ImportError, sqlite3.Error):
1717
+ pass
1654
1718
  try:
1655
1719
  row = conn.execute(
1656
1720
  "SELECT COUNT(*) FROM conversation_sessions"
@@ -1686,6 +1750,7 @@ def _doctor_gather_state_impl(
1686
1750
  (
1687
1751
  conversation_rollup_pricing_refusal,
1688
1752
  conversation_rollup_pricing_fp,
1753
+ conversation_rollup_pricing_observation,
1689
1754
  ) = _read_rollup_pricing_state(conn)
1690
1755
  # Pending reingest/split/backfill flags ⇒ a full sync hasn't yet
1691
1756
  # reconciled the rollup. Read the canonical flag set from
@@ -1745,9 +1810,15 @@ def _doctor_gather_state_impl(
1745
1810
  if row:
1746
1811
  codex_entries_count = int(row[0]) if row[0] is not None else 0
1747
1812
  if row[1]:
1748
- codex_last_entry_at = parse_iso_datetime(
1749
- row[1], "codex_session_entries.timestamp_utc",
1750
- ).astimezone(dt.timezone.utc)
1813
+ try:
1814
+ codex_last_entry_at = parse_iso_datetime(
1815
+ row[1], "codex_session_entries.timestamp_utc",
1816
+ ).astimezone(dt.timezone.utc)
1817
+ except ValueError:
1818
+ # A malformed newest row cannot establish cache
1819
+ # freshness. Continue to the all-history health
1820
+ # partition so doctor can count the bad row.
1821
+ codex_last_entry_at = None
1751
1822
  try:
1752
1823
  row = conn.execute(
1753
1824
  "SELECT COUNT(*) FROM quota_window_snapshots "
@@ -1774,7 +1845,13 @@ def _doctor_gather_state_impl(
1774
1845
  "qualified_rows": health.qualified_rows,
1775
1846
  "missing_conversation_key_rows": health.missing_conversation_key_rows,
1776
1847
  "missing_thread_join_rows": health.missing_thread_join_rows,
1848
+ "undecodable_metadata_rows": (
1849
+ health.undecodable_metadata_rows),
1777
1850
  }
1851
+ if health.malformed_accounting_rows:
1852
+ codex_project_metadata_health[
1853
+ "malformed_accounting_rows"] = (
1854
+ health.malformed_accounting_rows)
1778
1855
  except Exception as exc:
1779
1856
  codex_project_metadata_error = type(exc).__name__
1780
1857
  except sqlite3.OperationalError as exc:
@@ -2647,6 +2724,9 @@ def _doctor_gather_state_impl(
2647
2724
  conversation_rollup_pricing_fp=conversation_rollup_pricing_fp,
2648
2725
  cache_rollup_pricing_refusal=cache_rollup_pricing_refusal,
2649
2726
  cache_rollup_pricing_fp=cache_rollup_pricing_fp,
2727
+ conversation_rollup_pricing_observation=(
2728
+ conversation_rollup_pricing_observation),
2729
+ cache_rollup_pricing_observation=cache_rollup_pricing_observation,
2650
2730
  stats_db_quick_check=stats_db_quick_check,
2651
2731
  cache_db_quick_check=cache_db_quick_check,
2652
2732
  conversations_db_quick_check=conversations_db_quick_check,
@@ -2666,6 +2746,7 @@ def _doctor_gather_state_impl(
2666
2746
  cache_db_freelist_count=cache_db_freelist_count,
2667
2747
  conversations_db_page_count=conversations_db_page_count,
2668
2748
  conversations_db_freelist_count=conversations_db_freelist_count,
2749
+ conversations_reclaim_pending=conversations_reclaim_pending,
2669
2750
  codex_quota_windows=codex_quota_windows,
2670
2751
  codex_hook_roots=codex_hook_roots,
2671
2752
  codex_hook_liveness=codex_hook_liveness,