fix(sp38): restore per-kind control_number in find_ack_orphans + review cleanups

Three pr-reviewer followups from the 2026-07-07 review of commit ad14b56:

1. BUG: find_ack_orphans refactor routed 277ca/ta1 through
   _ack_control_number which only knew 999 — restored per-kind source
   (999 reads raw_json.envelope.control_number, 277ca/ta1 read the ORM
   control_number column). Added regression test pinning all three
   kinds.

2. DOCSTRING DRIFT: reconcile_orphan_st02s said 'remaining columns
   take their defaults' but explicitly passes parsed_at and
   transaction_set_control_number — added both to the explicit list
   and clarified the rest take schema defaults.

3. WASTED SORT: _iter_orphan_999_st02s yielded sorted() but the
   caller re-sorts by (-ack_count, st02) — yielding unsorted now.

Plus three cleanups the reviewer flagged:

* Hoisted 'json' / 'uuid' / 'datetime' imports to module top of
  store/__init__.py (replaced in-method imports).
* Added two missing tests: sentinel grep-discoverability via
  LIKE '<synthetic:%>' + 999-walk tolerance for non-dict raw_json
  (None / list shapes — bytes is unreachable through the ORM).
* Aligned spec/plan exit codes to the cyclone-cli convention
  (exit 1 on DB error, not 2 — matches the existing CLI
  sys.exit(1) and the cyclone-cli skill documentation).

36/36 SP38 tests pass.
This commit is contained in:
Nora
2026-07-07 13:11:29 -06:00
parent 76923a79f5
commit 07ea7ca1d6
6 changed files with 191 additions and 25 deletions
+58 -1
View File
@@ -317,4 +317,61 @@ def test_reconcile_records_ack_count_in_totals_json():
)
totals = json.loads(row.totals_json or "{}")
assert totals.get("orphan_reconcile") is True
assert totals.get("ack_count") == 4
assert totals.get("ack_count") == 4
def test_reconcile_sentinel_is_grep_discoverable():
"""``input_filename = '<synthetic:orphan-reconcile>'`` must be
discoverable via a plain ``LIKE '<synthetic:%>'`` query so
operators can find every sp38-created row without knowing the
exact sentinel string. Pins the spec's D2 grep-discoverability
invariant."""
with db.SessionLocal()() as s:
_ingest_999(s, "991102988")
s.commit()
CycloneStore().reconcile_orphan_st02s(dry_run=False)
with db.SessionLocal()() as s:
n = (
s.query(Batch)
.filter(Batch.input_filename.like("<synthetic:%>"))
.count()
)
assert n == 1
def test_summary_skips_999s_with_non_dict_raw_json():
"""``_extract_999_st02`` must tolerate the un-dict shapes a JSON
column can take (None, list) and never raise. Rows with
non-dict raw_json are skipped — they have no ST02 to contribute
to the summary regardless.
Note: the SQLAlchemy JSON column rejects bytes at insert time
(it must be JSON-serializable), so we don't exercise the
``bytes`` branch here — that's a defensive-programming guard for
raw sqlite3 reads, not a normal ORM codepath.
"""
with db.SessionLocal()() as s:
# Valid orphan — contributes 1 row
_ingest_999(s, "991102988")
# None — should be skipped, not raise
s.add(Ack(
source_batch_id="999-none-raw",
accepted_count=1, rejected_count=0, received_count=1,
ack_code="A", parsed_at=_now(),
raw_json=None,
))
# list — not a dict; should be skipped (not raise)
s.add(Ack(
source_batch_id="999-list-raw",
accepted_count=1, rejected_count=0, received_count=1,
ack_code="A", parsed_at=_now(),
raw_json=[1, 2, 3],
))
s.commit()
summary = CycloneStore().find_ack_orphan_st02_summary()
by_st02 = {row["st02"]: row for row in summary}
assert by_st02 == {"991102988": {"st02": "991102988", "ack_count": 1,
"has_batch": False, "batch_id": None}}