#!/usr/bin/env python3
"""Bank batch-167: Rule 125 source-backed lexical expansion after Rule 124."""

from __future__ import annotations

import datetime as dt
import json
import re
import sqlite3
import sys

from process_ledger import ensure_schema, record_event

DB = "/mnt/donto-data/donto-resources/research/translation-training/kuku-yalanji-runpod-2026-06-30/synthetic/claude-synthetic-v1-2026-07-02/synthetic.db"
LABEL = "batch-167"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-167-source-backed-lexical-expansion-after-rule124-process.md"

DENY = {"yah", "ngawuya", "walnga", "maja-nga", "yunga", "bada-bada", "wangkar-wangkar", "biyul"}
CONSTRUCTION_CLITIC_MARKERS = ("-kuda", "-ngarr", "-ngurr", "-l-urr", "wawu-")
PRONOUN_A = {"ngayu", "ngana", "nganjin", "yundu", "yurra", "jana", "nyulu"}
SOURCED_COMITATIVES = ("ngawa-nji", "nganjan-anji", "jawun-unji", "juku-ji", "kambi-ji")
LAUGH_TARGET_SUFFIXES = ("-anka", "-anda", "-nda", "-ndu", "-nka")


def js(values: list[str]) -> str:
    return json.dumps(values, ensure_ascii=False)


ROWS = [
    (1, "The fish lay in the bag.",
     "Kuyu balji-nga wunan.",
     "kuyu balji-nga wuna-ny | fish bag-LOC lie-PAST",
     "r125_rest_fish_bag", 3, js(["kuyu", "balji", "wunay"]), js([]),
     "Dictionary/Patz source kuyu fish; balji-nga bag LOC and wuna-y are retained controls.", "0.84"),
    (2, "The meat lay in the shade.",
     "Minya wabarr-ba wunan.",
     "minya wabarr-ba wuna-ny | meat shade-LOC lie-PAST",
     "r125_rest_meat_shade", 3, js(["minya", "wabarr", "wunay"]), js([]),
     "Minya meat/flesh-food checked; wabarr-ba shade LOC retained.", "0.84"),
    (3, "The axe lay on the ground.",
     "Warrbi bubu-ngu wunan.",
     "warrbi bubu-ngu wuna-ny | axe ground-LOC lie-PAST",
     "r125_rest_axe_ground", 3, js(["warrbi", "bubu", "wunay"]), js([]),
     "Warrbi used only in the axe sense; bubu-ngu ground LOC retained.", "0.82"),
    (4, "The knife lay in the bag.",
     "Naybu balji-nga wunan.",
     "naybu balji-nga wuna-ny | knife bag-LOC lie-PAST",
     "r125_rest_knife_bag", 3, js(["naybu", "balji", "wunay"]), js(["naybu"]),
     "Naybu is an attested English loan for knife; object remains ABS.", "0.82"),
    (5, "The spear lay outside.",
     "Kalka ngalkal-ba wunan.",
     "kalka ngalkal-ba wuna-ny | spear outside-LOC lie-PAST",
     "r125_rest_spear_outside", 3, js(["kalka", "ngalkal", "wunay"]), js([]),
     "Kalka is fishing spear/generic spear; used instead of excluded biyul.", "0.84"),
    (6, "The club lay on the road.",
     "Mirru baral-ba wunan.",
     "mirru baral-ba wuna-ny | club road-LOC lie-PAST",
     "r125_rest_club_road", 3, js(["mirru", "baral", "wunay"]), js([]),
     "Mirru club/nullanulla checked; baral-ba road LOC retained.", "0.84"),
    (7, "The shield lay outside.",
     "Kunjuri ngalkal-ba wunan.",
     "kunjuri ngalkal-ba wuna-ny | shield outside-LOC lie-PAST",
     "r125_rest_shield_outside", 3, js(["kunjuri", "ngalkal", "wunay"]), js([]),
     "Kunjuri shield checked in dictionary and grammar instrument example.", "0.84"),
    (8, "The boomerang lay on the ground.",
     "Wangal bubu-ngu wunan.",
     "wangal bubu-ngu wuna-ny | boomerang ground-LOC lie-PAST",
     "r125_rest_boomerang_ground", 3, js(["wangal", "bubu", "wunay"]), js([]),
     "Wangal is boomerang noun, not a dance verb; bubu-ngu retained.", "0.84"),
    (9, "Rain came.",
     "Kaba kadan.",
     "kaba kada-ny | rain come-PAST",
     "r125_weather_rain_came", 2, js(["kaba", "kaday"]), js([]),
     "Grammar sources kaba kada-y rain came; no new case morphology.", "0.86"),
    (10, "The gun lay at camp.",
     "Marrkin bayan-ba wunan.",
     "marrkin bayan-ba wuna-ny | gun camp-LOC lie-PAST",
     "r125_rest_gun_camp", 3, js(["marrkin", "bayan", "wunay"]), js(["marrkin"]),
     "Marrkin is a source-backed loan from musket/rifle; bayan-ba camp LOC retained.", "0.82"),

    (11, "The man put the fish in the bag.",
     "Dingkar-angka kuyu balji-nga yijarrin.",
     "dingkar-angka kuyu balji-nga yijarri-ny | man-ERG fish bag-LOC put-PAST",
     "r125_object_fish_bag", 4, js(["dingkar", "kuyu", "balji", "yijarril"]), js([]),
     "Kuyu object enters the retained yijarri-l placement frame; common-noun A marked ERG.", "0.84"),
    (12, "The woman left the meat in the shade.",
     "Jalbu-ngku minya wabarr-ba bawan.",
     "jalbu-ngku minya wabarr-ba bawa-ny | woman-ERG meat shade-LOC leave-PAST",
     "r125_object_meat_shade", 4, js(["jalbu", "minya", "wabarr", "bawal"]), js([]),
     "Minya meat object with source-backed shade LOC and leave frame.", "0.84"),
    (13, "The man put the axe on the ground.",
     "Dingkar-angka warrbi bubu-ngu yijarrin.",
     "dingkar-angka warrbi bubu-ngu yijarri-ny | man-ERG axe ground-LOC put-PAST",
     "r125_object_axe_ground", 4, js(["dingkar", "warrbi", "bubu", "yijarril"]), js([]),
     "Warrbi axe sense only; no grass-house sense in this row.", "0.82"),
    (14, "Mother put the knife in the bag.",
     "Ngamu-ngku naybu balji-nga yijarrin.",
     "ngamu-ngku naybu balji-nga yijarri-ny | mother-ERG knife bag-LOC put-PAST",
     "r125_object_knife_bag", 4, js(["ngamu", "naybu", "balji", "yijarril"]), js(["naybu"]),
     "Naybu loan object in a retained placement frame; no instrument morphology introduced.", "0.82"),
    (15, "The young man left the spear outside.",
     "Warru-ngku kalka ngalkal-ba bawan.",
     "warru-ngku kalka ngalkal-ba bawa-ny | young.man-ERG spear outside-LOC leave-PAST",
     "r125_object_spear_outside", 4, js(["warru", "kalka", "ngalkal", "bawal"]), js([]),
     "Kalka spear object; ngalkal-ba outside LOC retained.", "0.84"),
    (16, "The man brought the club from the house.",
     "Dingkar-angka mirru bayan-muny wundin.",
     "dingkar-angka mirru bayan-muny wundi-ny | man-ERG club house-ABL bring-PAST",
     "r125_object_club_house_source", 4, js(["dingkar", "mirru", "bayan", "wundil"]), js([]),
     "Mirru club object with bayan-muny ABL source.", "0.84"),
    (17, "Grandfather got the shield at camp.",
     "Kami-ngka kunjuri bayan-ba manin.",
     "kami-ngka kunjuri bayan-ba mani-ny | grandfather-ERG shield camp-LOC get-PAST",
     "r125_object_shield_camp", 4, js(["kami", "kunjuri", "bayan", "manil"]), js([]),
     "Kunjuri shield object in a retained get-at-place frame.", "0.84"),
    (18, "The old woman left the boomerang outside.",
     "Wulbuman-angka wangal ngalkal-ba bawan.",
     "wulbuman-angka wangal ngalkal-ba bawa-ny | old.woman-ERG boomerang outside-LOC leave-PAST",
     "r125_object_boomerang_outside", 4, js(["wulbuman", "wangal", "ngalkal", "bawal"]), js([]),
     "Wangal boomerang object; no dance-verb sense.", "0.84"),
    (19, "We brought fish from the sea.",
     "Ngana kuyu jalun-muny wundin.",
     "ngana kuyu jalun-muny wundi-ny | we fish sea-ABL bring-PAST",
     "r125_object_fish_sea_source", 4, js(["ngana", "kuyu", "jalun", "wundil"]), js([]),
     "Pronoun A remains bare; kuyu object plus jalun-muny source.", "0.84"),
    (20, "I took the gun to camp.",
     "Ngayu marrkin bayan-ka wundin.",
     "ngayu marrkin bayan-ka wundi-ny | I gun camp-DAT take-PAST",
     "r125_object_gun_camp_goal", 4, js(["ngayu", "marrkin", "bayan", "wundil"]), js(["marrkin"]),
     "Marrkin loan object; pronoun A bare; bayan-ka DAT goal retained.", "0.82"),

    (21, "The woman saw the fish at the sea.",
     "Jalbu-ngku kuyu jalun-bu nyajin.",
     "jalbu-ngku kuyu jalun-bu nyaji-ny | woman-ERG fish sea-LOC see-PAST",
     "r125_perception_woman_fish_sea", 4, js(["jalbu", "kuyu", "jalun", "nyajil"]), js([]),
     "Common-noun perceiver ERG; kuyu object at jalun-bu.", "0.84"),
    (22, "I saw the fish at the sea.",
     "Ngayu kuyu jalun-bu nyajin.",
     "ngayu kuyu jalun-bu nyaji-ny | I fish sea-LOC see-PAST",
     "r125_perception_pronoun_fish_sea", 4, js(["ngayu", "kuyu", "jalun", "nyajil"]), js([]),
     "Pronoun A remains unmarked in the matched fish perception frame.", "0.84"),
    (23, "The man saw the meat on the ground.",
     "Dingkar-angka minya bubu-ngu nyajin.",
     "dingkar-angka minya bubu-ngu nyaji-ny | man-ERG meat ground-LOC see-PAST",
     "r125_perception_man_meat_ground", 4, js(["dingkar", "minya", "bubu", "nyajil"]), js([]),
     "Common-noun perceiver ERG; minya object on bubu-ngu.", "0.84"),
    (24, "We saw the meat on the ground.",
     "Ngana minya bubu-ngu nyajin.",
     "ngana minya bubu-ngu nyaji-ny | we meat ground-LOC see-PAST",
     "r125_perception_pronoun_meat_ground", 4, js(["ngana", "minya", "bubu", "nyajil"]), js([]),
     "Pronoun A contrast for row 23.", "0.84"),
    (25, "The child saw the knife in the bag.",
     "Karrkay-ngku naybu balji-nga nyajin.",
     "karrkay-ngku naybu balji-nga nyaji-ny | child-ERG knife bag-LOC see-PAST",
     "r125_perception_child_knife_bag", 4, js(["karrkay", "naybu", "balji", "nyajil"]), js(["naybu"]),
     "Naybu loan object with ERG common-noun perceiver.", "0.82"),
    (26, "I saw the knife in the bag.",
     "Ngayu naybu balji-nga nyajin.",
     "ngayu naybu balji-nga nyaji-ny | I knife bag-LOC see-PAST",
     "r125_perception_pronoun_knife_bag", 4, js(["ngayu", "naybu", "balji", "nyajil"]), js(["naybu"]),
     "Pronoun A contrast for row 25.", "0.82"),
    (27, "Grandmother saw the axe outside.",
     "Babi-ngka warrbi ngalkal-ba nyajin.",
     "babi-ngka warrbi ngalkal-ba nyaji-ny | grandmother-ERG axe outside-LOC see-PAST",
     "r125_perception_grandmother_axe_outside", 4, js(["babi", "warrbi", "ngalkal", "nyajil"]), js([]),
     "Warrbi axe sense with ERG common-noun perceiver.", "0.82"),
    (28, "We saw the axe outside.",
     "Ngana warrbi ngalkal-ba nyajin.",
     "ngana warrbi ngalkal-ba nyaji-ny | we axe outside-LOC see-PAST",
     "r125_perception_pronoun_axe_outside", 4, js(["ngana", "warrbi", "ngalkal", "nyajil"]), js([]),
     "Pronoun A contrast for row 27.", "0.82"),
    (29, "The young man saw the moon outside.",
     "Warru-ngku kija ngalkal-ba nyajin.",
     "warru-ngku kija ngalkal-ba nyaji-ny | young.man-ERG moon outside-LOC see-PAST",
     "r125_perception_young_man_moon_outside", 4, js(["warru", "kija", "ngalkal", "nyajil"]), js([]),
     "Kija is moon only here; common-noun perceiver is ERG-marked.", "0.84"),
    (30, "I saw the star outside.",
     "Ngayu dawar ngalkal-ba nyajin.",
     "ngayu dawar ngalkal-ba nyaji-ny | I star outside-LOC see-PAST",
     "r125_perception_pronoun_star_outside", 4, js(["ngayu", "dawar", "ngalkal", "nyajil"]), js([]),
     "Dawar is the safe star lexeme; pronoun A bare.", "0.84"),

    (31, "The old man asked grandfather about meat.",
     "Wulman kami-nda minya-ka babajin.",
     "wulman kami-nda minya-ka babaji-ny | old.man grandfather-LOC:pt meat-DAT ask-PAST",
     "r125_ask_old_man_grandfather_meat", 4, js(["wulman", "kami", "minya", "babaji"]), js(["wulman"]),
     "Minya-ka is grammar-sourced; babaji-y asker remains ABS/S.", "0.86"),
    (32, "The young man asked mother about meat.",
     "Warru ngamu-ndu minya-ka babajin.",
     "warru ngamu-ndu minya-ka babaji-ny | young.man mother-LOC:pt meat-DAT ask-PAST",
     "r125_ask_young_man_mother_meat", 4, js(["warru", "ngamu", "minya", "babaji"]), js([]),
     "Ask-about frame with LOC:pt person and minya-ka DAT topic.", "0.86"),
    (33, "The woman asked father about meat.",
     "Jalbu nganjan-anda minya-ka babajin.",
     "jalbu nganjan-anda minya-ka babaji-ny | woman father-LOC:pt meat-DAT ask-PAST",
     "r125_ask_woman_father_meat", 4, js(["jalbu", "nganjan", "minya", "babaji"]), js([]),
     "Duplicate-safe replacement for a mayi-ka row; minya-ka is grammar-sourced.", "0.86"),
    (34, "The friend asked the girl about news.",
     "Jawun maral-anda kuku-ku babajin.",
     "jawun maral-anda kuku-ku babaji-ny | friend girl-LOC:pt news-DAT ask-PAST",
     "r125_ask_friend_girl_news", 4, js(["jawun", "maral", "kuku", "babaji"]), js([]),
     "Kuku-ku DAT topic retained; no ERG on asker.", "0.86"),
    (35, "The child asked grandmother about the mango.",
     "Karrkay babi-nda mayngku-ku babajin.",
     "karrkay babi-nda mayngku-ku babaji-ny | child grandmother-LOC:pt mango-DAT ask-PAST",
     "r125_ask_child_grandmother_mango", 4, js(["karrkay", "babi", "mayngku", "babaji"]), js(["mayngku"]),
     "Mayngku-ku remains directly grammar-sourced.", "0.86"),

    (36, "The old man laughed at grandmother.",
     "Wulman babi-nda yajarrin.",
     "wulman babi-nda yajarri-ny | old.man grandmother-DAT:pt laugh-PAST",
     "r125_laugh_old_man_grandmother", 3, js(["wulman", "babi", "yajarril"]), js(["wulman"]),
     "DAT:pt target retained; no ERG on laugher.", "0.84"),
    (37, "Mother laughed at the girl.",
     "Ngamu maral-anda yajarrin.",
     "ngamu maral-anda yajarri-ny | mother girl-DAT:pt laugh-PAST",
     "r125_laugh_mother_girl", 3, js(["ngamu", "maral", "yajarril"]), js([]),
     "Duplicate-safe target change; maral-anda DAT:pt retained.", "0.84"),
    (38, "Father laughed at the girl.",
     "Nganjan maral-anda yajarrin.",
     "nganjan maral-anda yajarri-ny | father girl-DAT:pt laugh-PAST",
     "r125_laugh_father_girl", 3, js(["nganjan", "maral", "yajarril"]), js([]),
     "DAT:pt target retained.", "0.84"),
    (39, "The girl laughed at the child.",
     "Maral karrkay-anka yajarrin.",
     "maral karrkay-anka yajarri-ny | girl child-DAT:pt laugh-PAST",
     "r125_laugh_girl_child", 3, js(["maral", "karrkay", "yajarril"]), js([]),
     "Karrkay-anka target form retained.", "0.84"),
    (40, "The friend laughed at grandfather.",
     "Jawun kami-nda yajarrin.",
     "jawun kami-nda yajarri-ny | friend grandfather-DAT:pt laugh-PAST",
     "r125_laugh_friend_grandfather", 3, js(["jawun", "kami", "yajarril"]), js([]),
     "Duplicate-safe replacement for the repeated warru target; DAT:pt target retained.", "0.84"),

    (41, "Grandmother went to the sea with the baby.",
     "Babi ngawa-nji jalun-bu dungan.",
     "babi ngawa-nji jalun-bu dunga-ny | grandmother baby-COMIT:pt sea-LOC go-PAST",
     "r125_comit_grandmother_baby_sea", 4, js(["babi", "ngawa", "jalun", "dungay"]), js([]),
     "Ngawa-nji remains direct-source only; jalun-bu sea LOC retained.", "0.84"),
    (42, "The young man went outside with a friend.",
     "Warru jawun-unji ngalkal-ba dungan.",
     "warru jawun-unji ngalkal-ba dunga-ny | young.man friend-COMIT:pt outside-LOC go-PAST",
     "r125_comit_young_man_friend_outside", 4, js(["warru", "jawun", "ngalkal", "dungay"]), js([]),
     "Jawun-unji is in the closed comitative set.", "0.84"),
    (43, "The woman went to the shade with father.",
     "Jalbu nganjan-anji wabarr-ba dungan.",
     "jalbu nganjan-anji wabarr-ba dunga-ny | woman father-COMIT:pt shade-LOC go-PAST",
     "r125_comit_woman_father_shade", 4, js(["jalbu", "nganjan", "wabarr", "dungay"]), js([]),
     "Nganjan-anji closed comitative plus retained shade LOC.", "0.84"),
    (44, "The child went to the road with a stick.",
     "Karrkay juku-ji baral-ba dungan.",
     "karrkay juku-ji baral-ba dunga-ny | child stick-COMIT road-LOC go-PAST",
     "r125_comit_child_stick_road", 4, js(["karrkay", "juku", "baral", "dungay"]), js([]),
     "Juku-ji closed neutral comitative; no new implement comitative.", "0.84"),
    (45, "Mother went to the road with clothes.",
     "Ngamu kambi-ji baral-ba dungan.",
     "ngamu kambi-ji baral-ba dunga-ny | mother clothes-COMIT road-LOC go-PAST",
     "r125_comit_mother_clothes_road", 4, js(["ngamu", "kambi", "baral", "dungay"]), js([]),
     "Kambi-ji closed comitative; duplicate-safe location replacement.", "0.84"),
    (46, "The old man is staying in the shade with the baby.",
     "Wulman wabarr-ba bundanday ngawa-nji.",
     "wulman wabarr-ba bunda-n-bunda-y ngawa-nji | old.man shade-LOC stay-REDUP-NONPAST baby-COMIT:pt",
     "r125_comit_old_man_baby_shade", 4, js(["wulman", "wabarr", "bunday", "ngawa"]), js(["wulman"]),
     "Under-review repair restored source-like place-before-comitative order while avoiding an exact duplicate.", "0.84"),

    (47, "The man left the shield in the shade.",
     "Dingkar-angka kunjuri wabarr-ba bawan.",
     "dingkar-angka kunjuri wabarr-ba bawa-ny | man-ERG shield shade-LOC leave-PAST",
     "r125_object_shield_shade", 4, js(["dingkar", "kunjuri", "wabarr", "bawal"]), js([]),
     "Kunjuri shield object in leave-location frame.", "0.84"),
    (48, "The old woman brought the boomerang from the house.",
     "Wulbuman-angka wangal bayan-muny wundin.",
     "wulbuman-angka wangal bayan-muny wundi-ny | old.woman-ERG boomerang house-ABL bring-PAST",
     "r125_object_boomerang_house_source", 4, js(["wulbuman", "wangal", "bayan", "wundil"]), js([]),
     "Wangal boomerang object plus bayan-muny source.", "0.84"),
    (49, "The young man left the gun outside.",
     "Warru-ngku marrkin ngalkal-ba bawan.",
     "warru-ngku marrkin ngalkal-ba bawa-ny | young.man-ERG gun outside-LOC leave-PAST",
     "r125_object_gun_outside", 4, js(["warru", "marrkin", "ngalkal", "bawal"]), js(["marrkin"]),
     "Marrkin loan object in leave-location frame.", "0.82"),
    (50, "I took the club to camp.",
     "Ngayu mirru bayan-ka wundin.",
     "ngayu mirru bayan-ka wundi-ny | I club camp-DAT take-PAST",
     "r125_object_club_camp_goal", 4, js(["ngayu", "mirru", "bayan", "wundil"]), js([]),
     "Pronoun A bare; mirru object and bayan-ka DAT goal retained.", "0.84"),
]


def has_denied_token(low: str, token: str) -> bool:
    return re.search(rf"(?<![a-z-]){re.escape(token)}(?![a-z-])", low) is not None


def scan_static() -> list[str]:
    issues: list[str] = []
    if len(ROWS) != 50:
        issues.append(f"{LABEL}: expected 50 rows, found {len(ROWS)}")
    seen_ku: set[str] = set()
    for expected, row in enumerate(ROWS, start=1):
        seq, english, kuku, analysis, frame, _tier, words_used, loans_used, evidence, confidence = row
        label = f"{LABEL}.{seq:02d}"
        low = kuku.lower()
        analysis_low = analysis.lower()
        if seq != expected:
            issues.append(f"{label}: sequence mismatch, expected {expected}")
        if not english[:1].isupper() or english[-1:] not in ".!?":
            issues.append(f"{label}: English punctuation/capitalization failure")
        if " | " not in analysis:
            issues.append(f"{label}: analysis missing gloss separator")
        if not frame.startswith("r125_"):
            issues.append(f"{label}: Rule 125 batch frame must start with r125_")
        if low in seen_ku:
            issues.append(f"{label}: duplicate Kuku surface within batch")
        seen_ku.add(low)
        for marker in CONSTRUCTION_CLITIC_MARKERS:
            if marker in low:
                issues.append(f"{label}: lexical batch must not reuse temporal clitic marker {marker}")
        for denied in DENY:
            if has_denied_token(low, denied):
                issues.append(f"{label}: denied or out-of-scope token {denied!r}")
        if "baral-muny" in low:
            issues.append(f"{label}: baral-muny is not source-logged")
        if "kadar-anji" in low:
            issues.append(f"{label}: kadar-anji is outside this batch's closed comitative set")
        if "ngunyin-ji" in low:
            issues.append(f"{label}: ngunyin-ji is attested but not admitted by Rule 125's closed-comitative policy")
        if "kija" in low and not re.search(r"\b(moon|month)\b", analysis_low):
            issues.append(f"{label}: kija is moon/month only; analysis must say moon/month")
        if "star" in english.lower() and "dawar" not in low:
            issues.append(f"{label}: star row must use dawar")
        if "wangal" in low and "boomerang" not in analysis_low:
            issues.append(f"{label}: wangal must be boomerang noun, not dance")
        if "warrbi" in low and "axe" not in analysis_low:
            issues.append(f"{label}: warrbi must be axe in this batch")
        if "babajin" in low and re.match(r"^[A-Za-z-]+-(?:ngka|angka|ngku)\b", kuku):
            issues.append(f"{label}: babaji-y asker drifted into ergative A marking")
        if "nyaji" in analysis_low:
            first = low.split()[0].strip(".,!?")
            if first not in PRONOUN_A and not re.search(r"-(?:ngka|angka|ngku)\b", first):
                issues.append(f"{label}: common-noun perceiver with nyaji-l should be ERG-marked")
        if "yajarrin" in low and not any(suffix in low for suffix in LAUGH_TARGET_SUFFIXES):
            issues.append(f"{label}: laugh target should show sourced DAT:pt target suffix")
        if "COMIT" in analysis and not any(form in low for form in SOURCED_COMITATIVES):
            issues.append(f"{label}: comitative form is not in the closed sourced set")
        if any(form in low for form in ("kaya-nga", "ngamu-ngu")) and "POSS" not in analysis:
            issues.append(f"{label}: possessive form needs explicit POSS analysis")
        try:
            json.loads(words_used)
            json.loads(loans_used)
        except json.JSONDecodeError as exc:
            issues.append(f"{label}: JSON field failed: {exc}")
        if not evidence or not confidence:
            issues.append(f"{label}: missing evidence/confidence")
    return issues


def main() -> int:
    issues = scan_static()
    if issues:
        print("pre-bank scan failed:")
        for issue in issues:
            print(f"  - {issue}")
        return 1

    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    existing = db.execute("SELECT id FROM batches WHERE label=?", (LABEL,)).fetchone()
    if existing:
        batch_id = existing[0]
        accepted = db.execute(
            "SELECT COUNT(*) FROM sentences WHERE batch_id=? AND status IN ('accepted','revised')",
            (batch_id,),
        ).fetchone()[0]
        if accepted:
            raise SystemExit(f"{LABEL} already has accepted/revised rows; refusing to overwrite")
        db.execute("DELETE FROM reviews WHERE sentence_id IN (SELECT id FROM sentences WHERE batch_id=?)", (batch_id,))
        db.execute("DELETE FROM sentences WHERE batch_id=?", (batch_id,))
        db.execute("UPDATE batches SET notes=?, created_at=? WHERE id=?", ("rebanked under_review", NOW, batch_id))
    else:
        cur = db.execute(
            "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
            (LABEL, "Rule 125 draft: source-backed lexical expansion after Rule 124", "operator-guide-2026-07-07", "under_review", NOW),
        )
        batch_id = cur.lastrowid

    dupes: list[str] = []
    for seq, _english, kuku, *_rest in ROWS:
        hit = db.execute(
            """
            SELECT b.label, s.seq
            FROM sentences s JOIN batches b ON b.id=s.batch_id
            WHERE lower(trim(s.kuku))=lower(trim(?))
              AND s.status!='rejected'
              AND s.batch_id!=?
            """,
            (kuku, batch_id),
        ).fetchone()
        if hit:
            dupes.append(f"{LABEL}.{seq:02d} duplicates {hit[0]}.{hit[1]:02d}: {kuku}")
    if dupes:
        db.rollback()
        print("cross-corpus duplicate check failed:")
        for dupe in dupes:
            print(f"  - {dupe}")
        return 1

    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail=(
            "Rule 125 source pass checked dictionary.yaml, Patz grammar, existing corpus lexemes, and planned-surface duplicates. "
            "New or foregrounded heads were admitted only as ABS objects/subjects or sourced DAT topics inside retained Rule 124 frames."
        ),
        command="rg/sed over dictionary.yaml and grammar.md; sqlite lexeme readback and duplicate scan",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="dictionary-source-check",
        actor="claude-main",
        detail=(
            "Exact dictionary readback confirmed kuyu fish, minya meat/flesh food, kaba rain, dawar star, kija moon, "
            "warrbi axe, naybu knife, kalka spear, mirru club, kunjuri shield, wangal boomerang, and marrkin gun."
        ),
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="grammar-source-check",
        actor="claude-main",
        detail=(
            "Grammar controls checked: kuyu/minya food and fish examples, kaba kada-y, kija moon/month, naybu-bu instrument, "
            "marrkin object/comitative/instrument examples, minya-ka DAT topic, kuku-ku and mayngku-ku, kalka-ji, and closed comitatives."
        ),
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="risk-controls",
        actor="claude-main",
        detail=(
            "Controls: biyul excluded for Nyungkal/Yalanji caution; kija moon/month only; dawar required for star; "
            "wangal noun only; warrbi axe only; no ngunyin-ji, no new comitatives, no free possessives, no baral-muny, "
            "no ergative babaji-y askers, common-noun nyaji-l perceivers ERG-marked."
        ),
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="existing-surface-scan",
        actor="claude-main",
        detail=(
            "Initial planned scan found four duplicates in frame-maintenance rows; topic/target/location repairs were made. "
            "Final scan checked all 50 planned Kuku surfaces against the full non-rejected corpus and found no exact duplicates."
        ),
        command="sqlite exact-surface query over planned Batch 167 rows",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="process",
        event_type="process-capture-expanded",
        actor="claude-main",
        detail=(
            "Batch 167 stores source inventory, candidate exclusion, duplicate repairs, static guards, row grouping, "
            "bank outcome, and later lint/review/close/export/docs evidence as process_events plus this process log."
        ),
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    db.executemany(
        """
        INSERT INTO sentences(
            batch_id, seq, english, kuku, analysis, frame, tier, words_used,
            loans_used, evidence, status, confidence, rights_status, author,
            created_at, updated_at
        ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (
                batch_id, seq, english, kuku, analysis, frame, tier, words_used,
                loans_used, evidence, "under_review", confidence,
                "project_approved_synthetic_pending_elder_verification",
                "claude-main", NOW, NOW,
            )
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="bank",
        event_type="bank-success",
        actor="claude-main",
        detail="Banked 50 hand-authored Rule 125 lexical-expansion rows under_review after static guards and duplicate checks.",
        command="python3 batch_scripts/bank_167.py",
        artifact_path="batch_scripts/bank_167.py",
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="bank",
        event_type="row-inventory",
        actor="claude-main",
        detail=(
            "Rows 1-10 rest/source; 11-20 object location/source/goal; 21-30 perception contrasts; 31-35 ask-about; "
            "36-40 laugh targets; 41-46 closed comitatives; 47-50 additional object rows for shield, boomerang, gun, and club."
        ),
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    db.commit()

    count = db.execute("SELECT COUNT(*) FROM sentences WHERE batch_id=?", (batch_id,)).fetchone()[0]
    avg_words = db.execute(
        "SELECT AVG(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1.0) FROM sentences WHERE batch_id=?",
        (batch_id,),
    ).fetchone()[0]
    process_events = db.execute("SELECT COUNT(*) FROM process_events WHERE batch_label=?", (LABEL,)).fetchone()[0]
    print(f"banked {LABEL}: {count} rows under_review, avg_words={avg_words:.2f}, process_events={process_events}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
