#!/usr/bin/env python3
"""Bank batch-332: Rule 290 visible traditional weapon/tool past perception."""

from __future__ import annotations

import datetime as dt
import json
import re
import sqlite3

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-332"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-332-visible-traditional-weapon-tool-singular-pronoun-past-perception-after-rule289-process.md"

TARGET_EVIDENCE = {
    "bangkal": "Dictionary lines 1478-1489 give bangkal as noun 'tongs', made from voya vine and used with kurrma. Rule 290 selects visible tongs reference only and leaves cooking/vine/earth-oven/action rows closed.",
    "kurrajan": "Dictionary lines 16196-16207 give kurrajan as noun 'spear point', with source prose about wattle, fire-hardening, and turtle spearing. Rule 290 selects visible spear-point reference only.",
    "dara": "Dictionary lines 5215-5226 give dara as noun 'bamboo spear stick'. Rule 290 selects visible spear-stick reference only and leaves use/action rows closed.",
    "jaba": "Dictionary lines 6843-6857 give jaba as noun 'stick', also sapling, post, walking stick, and horn. Rule 290 selects visible stick reference only.",
    "kunjuri": "Dictionary lines 15915-15923 give kunjuri as noun 'shield'. Rule 290 selects visible shield reference only.",
    "nambarr": "Dictionary lines 21516-21526 give nambarr as noun 'bamboo stick', a flexible bamboo stick used in nose piercing. Rule 290 selects visible bamboo-stick reference only.",
    "wangal": "Dictionary lines 25943-25951 give wangal as noun 'boomerang'. Rule 290 selects visible boomerang reference only.",
    "manbir": "Dictionary lines 17699-17707 give manbir as noun 'barb of a spear'. Rule 290 selects visible spear-barb reference only.",
    "wakay": "Dictionary lines 25192-25202 give wakay as noun 'wooden sword'. Rule 290 selects visible wooden-sword reference only.",
    "wulburr": "Dictionary lines 27767-27776 give wulburr as noun 'bamboo spear stick' and 'spear'. Rule 290 selects visible bamboo-spear-stick reference only.",
}

SUPPORT_EVIDENCE = (
    "Patz lines 2577-2603 source bare nominative ngayu, yundu, nyulu, bula, and jana; lines 2674-2686 "
    "source non-singular bula/jana notes; lines 2838-2845 source yaluy/yinyay; lines 2568-2571 and "
    "2023 source nyaji-ny 'see-PAST'; and lines 3160-3162 source past -ny. The corpus uses the elder-style "
    "surface nyajin for nyaji-ny."
)

TARGET_ORDER = ["bangkal", "kurrajan", "dara", "jaba", "kunjuri", "nambarr", "wangal", "manbir", "wakay", "wulburr"]
FRAME_SPECS = [
    ("ngayu", "yaluy"),
    ("yundu", "yinyay"),
    ("nyulu", "yaluy"),
    ("bula", "yinyay"),
    ("jana", "yaluy"),
]

DENY_KUKU = {
    "biyul", "durray", "jinil", "mirru", "kurajan", "ngurmu", "birungubay", "biruwybay", "birubay",
    "bangkal-ny", "bangkalji", "kurrajanji", "daraji", "jabaji", "kunjuriji", "nambarriji", "wangalji",
    "manbirji", "wakayji", "wulburrji", "nganya", "yununy", "nyunguny", "bulangan", "janangan",
    "bulanka", "jananka", "bulanda", "jananda", "bulanji", "jananji", "bulanga", "jananga",
    "nyajil", "nyaji-ny", "nyajiny", "nyaji-nka", "nyaji-l-nyaji", "yinyangka", "yinyayngka",
    "yinyamba", "yinyamuny", "yalungku", "yaluyngku", "yaluymbu", "yaluyundu", "yalamuny",
}
DENY_ENGLISH = (
    "cook", "cooking", "earth oven", "voya", "vine", "gather", "accumulate", "turtle spearing",
    "turtle", "wattle", "fire", "fire-hardening", "hardened", "pierce", "piercing", "throw",
    "throwing", "threw", "hunting", "hunt", "fighting with", "fight with", "defend", "defence",
    "cutting", "cut with", "stab", "stabbing", "injury", "nose piercing", "made", "make", "used",
    "use", "owned", "belongs", "route", "travel", "place", "story",
)


def row(seq: int, english: str, kuku: str, analysis: str, frame: str, words: list[str]) -> tuple:
    target = words[1]
    evidence = f"{TARGET_EVIDENCE[target]} {SUPPORT_EVIDENCE}"
    return (seq, english, kuku, analysis, frame, 3, json.dumps(words, ensure_ascii=False), json.dumps([], ensure_ascii=False), evidence, "0.86")


ROWS = [
    row(1, "I saw the tongs here.", "Ngayu bangkal yaluy nyajin.", "ngayu bangkal yaluy nyajin | 1sg.NOM tongs here see.PAST", "r290_1sg_here_weapon_tool_saw_bangkal", ["ngayu", "bangkal", "yaluy", "nyajin"]),
    row(2, "I saw the spear point here.", "Ngayu kurrajan yaluy nyajin.", "ngayu kurrajan yaluy nyajin | 1sg.NOM spear.point here see.PAST", "r290_1sg_here_weapon_tool_saw_kurrajan", ["ngayu", "kurrajan", "yaluy", "nyajin"]),
    row(3, "I saw the bamboo spear stick here.", "Ngayu dara yaluy nyajin.", "ngayu dara yaluy nyajin | 1sg.NOM bamboo.spear.stick here see.PAST", "r290_1sg_here_weapon_tool_saw_dara", ["ngayu", "dara", "yaluy", "nyajin"]),
    row(4, "I saw the stick here.", "Ngayu jaba yaluy nyajin.", "ngayu jaba yaluy nyajin | 1sg.NOM stick here see.PAST", "r290_1sg_here_weapon_tool_saw_jaba", ["ngayu", "jaba", "yaluy", "nyajin"]),
    row(5, "I saw the shield here.", "Ngayu kunjuri yaluy nyajin.", "ngayu kunjuri yaluy nyajin | 1sg.NOM shield here see.PAST", "r290_1sg_here_weapon_tool_saw_kunjuri", ["ngayu", "kunjuri", "yaluy", "nyajin"]),
    row(6, "I saw the bamboo stick here.", "Ngayu nambarr yaluy nyajin.", "ngayu nambarr yaluy nyajin | 1sg.NOM bamboo.stick here see.PAST", "r290_1sg_here_weapon_tool_saw_nambarr", ["ngayu", "nambarr", "yaluy", "nyajin"]),
    row(7, "I saw the boomerang here.", "Ngayu wangal yaluy nyajin.", "ngayu wangal yaluy nyajin | 1sg.NOM boomerang here see.PAST", "r290_1sg_here_weapon_tool_saw_wangal", ["ngayu", "wangal", "yaluy", "nyajin"]),
    row(8, "I saw the spear barb here.", "Ngayu manbir yaluy nyajin.", "ngayu manbir yaluy nyajin | 1sg.NOM spear.barb here see.PAST", "r290_1sg_here_weapon_tool_saw_manbir", ["ngayu", "manbir", "yaluy", "nyajin"]),
    row(9, "I saw the wooden sword here.", "Ngayu wakay yaluy nyajin.", "ngayu wakay yaluy nyajin | 1sg.NOM wooden.sword here see.PAST", "r290_1sg_here_weapon_tool_saw_wakay", ["ngayu", "wakay", "yaluy", "nyajin"]),
    row(10, "I saw the bamboo spear stick here.", "Ngayu wulburr yaluy nyajin.", "ngayu wulburr yaluy nyajin | 1sg.NOM bamboo.spear.stick here see.PAST", "r290_1sg_here_weapon_tool_saw_wulburr", ["ngayu", "wulburr", "yaluy", "nyajin"]),
    row(11, "You saw the tongs there.", "Yundu bangkal yinyay nyajin.", "yundu bangkal yinyay nyajin | 2sg.NOM tongs there see.PAST", "r290_2sg_there_weapon_tool_saw_bangkal", ["yundu", "bangkal", "yinyay", "nyajin"]),
    row(12, "You saw the spear point there.", "Yundu kurrajan yinyay nyajin.", "yundu kurrajan yinyay nyajin | 2sg.NOM spear.point there see.PAST", "r290_2sg_there_weapon_tool_saw_kurrajan", ["yundu", "kurrajan", "yinyay", "nyajin"]),
    row(13, "You saw the bamboo spear stick there.", "Yundu dara yinyay nyajin.", "yundu dara yinyay nyajin | 2sg.NOM bamboo.spear.stick there see.PAST", "r290_2sg_there_weapon_tool_saw_dara", ["yundu", "dara", "yinyay", "nyajin"]),
    row(14, "You saw the stick there.", "Yundu jaba yinyay nyajin.", "yundu jaba yinyay nyajin | 2sg.NOM stick there see.PAST", "r290_2sg_there_weapon_tool_saw_jaba", ["yundu", "jaba", "yinyay", "nyajin"]),
    row(15, "You saw the shield there.", "Yundu kunjuri yinyay nyajin.", "yundu kunjuri yinyay nyajin | 2sg.NOM shield there see.PAST", "r290_2sg_there_weapon_tool_saw_kunjuri", ["yundu", "kunjuri", "yinyay", "nyajin"]),
    row(16, "You saw the bamboo stick there.", "Yundu nambarr yinyay nyajin.", "yundu nambarr yinyay nyajin | 2sg.NOM bamboo.stick there see.PAST", "r290_2sg_there_weapon_tool_saw_nambarr", ["yundu", "nambarr", "yinyay", "nyajin"]),
    row(17, "You saw the boomerang there.", "Yundu wangal yinyay nyajin.", "yundu wangal yinyay nyajin | 2sg.NOM boomerang there see.PAST", "r290_2sg_there_weapon_tool_saw_wangal", ["yundu", "wangal", "yinyay", "nyajin"]),
    row(18, "You saw the spear barb there.", "Yundu manbir yinyay nyajin.", "yundu manbir yinyay nyajin | 2sg.NOM spear.barb there see.PAST", "r290_2sg_there_weapon_tool_saw_manbir", ["yundu", "manbir", "yinyay", "nyajin"]),
    row(19, "You saw the wooden sword there.", "Yundu wakay yinyay nyajin.", "yundu wakay yinyay nyajin | 2sg.NOM wooden.sword there see.PAST", "r290_2sg_there_weapon_tool_saw_wakay", ["yundu", "wakay", "yinyay", "nyajin"]),
    row(20, "You saw the bamboo spear stick there.", "Yundu wulburr yinyay nyajin.", "yundu wulburr yinyay nyajin | 2sg.NOM bamboo.spear.stick there see.PAST", "r290_2sg_there_weapon_tool_saw_wulburr", ["yundu", "wulburr", "yinyay", "nyajin"]),
    row(21, "He saw the tongs here.", "Nyulu bangkal yaluy nyajin.", "nyulu bangkal yaluy nyajin | 3sg.NOM tongs here see.PAST", "r290_3sg_here_weapon_tool_saw_bangkal", ["nyulu", "bangkal", "yaluy", "nyajin"]),
    row(22, "He saw the spear point here.", "Nyulu kurrajan yaluy nyajin.", "nyulu kurrajan yaluy nyajin | 3sg.NOM spear.point here see.PAST", "r290_3sg_here_weapon_tool_saw_kurrajan", ["nyulu", "kurrajan", "yaluy", "nyajin"]),
    row(23, "He saw the bamboo spear stick here.", "Nyulu dara yaluy nyajin.", "nyulu dara yaluy nyajin | 3sg.NOM bamboo.spear.stick here see.PAST", "r290_3sg_here_weapon_tool_saw_dara", ["nyulu", "dara", "yaluy", "nyajin"]),
    row(24, "He saw the stick here.", "Nyulu jaba yaluy nyajin.", "nyulu jaba yaluy nyajin | 3sg.NOM stick here see.PAST", "r290_3sg_here_weapon_tool_saw_jaba", ["nyulu", "jaba", "yaluy", "nyajin"]),
    row(25, "He saw the shield here.", "Nyulu kunjuri yaluy nyajin.", "nyulu kunjuri yaluy nyajin | 3sg.NOM shield here see.PAST", "r290_3sg_here_weapon_tool_saw_kunjuri", ["nyulu", "kunjuri", "yaluy", "nyajin"]),
    row(26, "He saw the bamboo stick here.", "Nyulu nambarr yaluy nyajin.", "nyulu nambarr yaluy nyajin | 3sg.NOM bamboo.stick here see.PAST", "r290_3sg_here_weapon_tool_saw_nambarr", ["nyulu", "nambarr", "yaluy", "nyajin"]),
    row(27, "He saw the boomerang here.", "Nyulu wangal yaluy nyajin.", "nyulu wangal yaluy nyajin | 3sg.NOM boomerang here see.PAST", "r290_3sg_here_weapon_tool_saw_wangal", ["nyulu", "wangal", "yaluy", "nyajin"]),
    row(28, "He saw the spear barb here.", "Nyulu manbir yaluy nyajin.", "nyulu manbir yaluy nyajin | 3sg.NOM spear.barb here see.PAST", "r290_3sg_here_weapon_tool_saw_manbir", ["nyulu", "manbir", "yaluy", "nyajin"]),
    row(29, "He saw the wooden sword here.", "Nyulu wakay yaluy nyajin.", "nyulu wakay yaluy nyajin | 3sg.NOM wooden.sword here see.PAST", "r290_3sg_here_weapon_tool_saw_wakay", ["nyulu", "wakay", "yaluy", "nyajin"]),
    row(30, "He saw the bamboo spear stick here.", "Nyulu wulburr yaluy nyajin.", "nyulu wulburr yaluy nyajin | 3sg.NOM bamboo.spear.stick here see.PAST", "r290_3sg_here_weapon_tool_saw_wulburr", ["nyulu", "wulburr", "yaluy", "nyajin"]),
    row(31, "They two saw the tongs there.", "Bula bangkal yinyay nyajin.", "bula bangkal yinyay nyajin | 3du.NOM tongs there see.PAST", "r290_3du_there_weapon_tool_saw_bangkal", ["bula", "bangkal", "yinyay", "nyajin"]),
    row(32, "They two saw the spear point there.", "Bula kurrajan yinyay nyajin.", "bula kurrajan yinyay nyajin | 3du.NOM spear.point there see.PAST", "r290_3du_there_weapon_tool_saw_kurrajan", ["bula", "kurrajan", "yinyay", "nyajin"]),
    row(33, "They two saw the bamboo spear stick there.", "Bula dara yinyay nyajin.", "bula dara yinyay nyajin | 3du.NOM bamboo.spear.stick there see.PAST", "r290_3du_there_weapon_tool_saw_dara", ["bula", "dara", "yinyay", "nyajin"]),
    row(34, "They two saw the stick there.", "Bula jaba yinyay nyajin.", "bula jaba yinyay nyajin | 3du.NOM stick there see.PAST", "r290_3du_there_weapon_tool_saw_jaba", ["bula", "jaba", "yinyay", "nyajin"]),
    row(35, "They two saw the shield there.", "Bula kunjuri yinyay nyajin.", "bula kunjuri yinyay nyajin | 3du.NOM shield there see.PAST", "r290_3du_there_weapon_tool_saw_kunjuri", ["bula", "kunjuri", "yinyay", "nyajin"]),
    row(36, "They two saw the bamboo stick there.", "Bula nambarr yinyay nyajin.", "bula nambarr yinyay nyajin | 3du.NOM bamboo.stick there see.PAST", "r290_3du_there_weapon_tool_saw_nambarr", ["bula", "nambarr", "yinyay", "nyajin"]),
    row(37, "They two saw the boomerang there.", "Bula wangal yinyay nyajin.", "bula wangal yinyay nyajin | 3du.NOM boomerang there see.PAST", "r290_3du_there_weapon_tool_saw_wangal", ["bula", "wangal", "yinyay", "nyajin"]),
    row(38, "They two saw the spear barb there.", "Bula manbir yinyay nyajin.", "bula manbir yinyay nyajin | 3du.NOM spear.barb there see.PAST", "r290_3du_there_weapon_tool_saw_manbir", ["bula", "manbir", "yinyay", "nyajin"]),
    row(39, "They two saw the wooden sword there.", "Bula wakay yinyay nyajin.", "bula wakay yinyay nyajin | 3du.NOM wooden.sword there see.PAST", "r290_3du_there_weapon_tool_saw_wakay", ["bula", "wakay", "yinyay", "nyajin"]),
    row(40, "They two saw the bamboo spear stick there.", "Bula wulburr yinyay nyajin.", "bula wulburr yinyay nyajin | 3du.NOM bamboo.spear.stick there see.PAST", "r290_3du_there_weapon_tool_saw_wulburr", ["bula", "wulburr", "yinyay", "nyajin"]),
    row(41, "They saw the tongs here.", "Jana bangkal yaluy nyajin.", "jana bangkal yaluy nyajin | 3pl.NOM tongs here see.PAST", "r290_3pl_here_weapon_tool_saw_bangkal", ["jana", "bangkal", "yaluy", "nyajin"]),
    row(42, "They saw the spear point here.", "Jana kurrajan yaluy nyajin.", "jana kurrajan yaluy nyajin | 3pl.NOM spear.point here see.PAST", "r290_3pl_here_weapon_tool_saw_kurrajan", ["jana", "kurrajan", "yaluy", "nyajin"]),
    row(43, "They saw the bamboo spear stick here.", "Jana dara yaluy nyajin.", "jana dara yaluy nyajin | 3pl.NOM bamboo.spear.stick here see.PAST", "r290_3pl_here_weapon_tool_saw_dara", ["jana", "dara", "yaluy", "nyajin"]),
    row(44, "They saw the stick here.", "Jana jaba yaluy nyajin.", "jana jaba yaluy nyajin | 3pl.NOM stick here see.PAST", "r290_3pl_here_weapon_tool_saw_jaba", ["jana", "jaba", "yaluy", "nyajin"]),
    row(45, "They saw the shield here.", "Jana kunjuri yaluy nyajin.", "jana kunjuri yaluy nyajin | 3pl.NOM shield here see.PAST", "r290_3pl_here_weapon_tool_saw_kunjuri", ["jana", "kunjuri", "yaluy", "nyajin"]),
    row(46, "They saw the bamboo stick here.", "Jana nambarr yaluy nyajin.", "jana nambarr yaluy nyajin | 3pl.NOM bamboo.stick here see.PAST", "r290_3pl_here_weapon_tool_saw_nambarr", ["jana", "nambarr", "yaluy", "nyajin"]),
    row(47, "They saw the boomerang here.", "Jana wangal yaluy nyajin.", "jana wangal yaluy nyajin | 3pl.NOM boomerang here see.PAST", "r290_3pl_here_weapon_tool_saw_wangal", ["jana", "wangal", "yaluy", "nyajin"]),
    row(48, "They saw the spear barb here.", "Jana manbir yaluy nyajin.", "jana manbir yaluy nyajin | 3pl.NOM spear.barb here see.PAST", "r290_3pl_here_weapon_tool_saw_manbir", ["jana", "manbir", "yaluy", "nyajin"]),
    row(49, "They saw the wooden sword here.", "Jana wakay yaluy nyajin.", "jana wakay yaluy nyajin | 3pl.NOM wooden.sword here see.PAST", "r290_3pl_here_weapon_tool_saw_wakay", ["jana", "wakay", "yaluy", "nyajin"]),
    row(50, "They saw the bamboo spear stick here.", "Jana wulburr yaluy nyajin.", "jana wulburr yaluy nyajin | 3pl.NOM bamboo.spear.stick here see.PAST", "r290_3pl_here_weapon_tool_saw_wulburr", ["jana", "wulburr", "yaluy", "nyajin"]),
]


def scan_static() -> list[str]:
    issues: list[str] = []
    if len(ROWS) != 50:
        issues.append(f"row count {len(ROWS)} != 50")
    if [r[0] for r in ROWS] != list(range(1, 51)):
        issues.append("sequence is not 1..50")
    kukus = [r[2] for r in ROWS]
    if len(set(kukus)) != len(kukus):
        issues.append("duplicate Kuku surface inside batch")
    for seq, english, kuku, _analysis, frame, _tier, words_json, loans_json, _evidence, _confidence in ROWS:
        words = json.loads(words_json)
        loans = json.loads(loans_json)
        lower_kuku = kuku.lower()
        lower_english = english.lower()
        if loans:
            issues.append(f"{seq}: loans used {loans}")
        denied = sorted({w for w in DENY_KUKU if re.search(rf"(^|\\s){re.escape(w)}([\\s\\.]|$)", lower_kuku)})
        if denied:
            issues.append(f"{seq}: denied Kuku forms {denied}")
        denied_english = [
            term for term in DENY_ENGLISH
            if re.search(rf"\b{re.escape(term.strip())}\b", lower_english)
        ]
        if denied_english:
            issues.append(f"{seq}: denied English drift {denied_english}")
        target_hits = [w for w in TARGET_ORDER if w in words]
        if len(target_hits) != 1:
            issues.append(f"{seq}: target hits {target_hits}")
        if len(words) != 4:
            issues.append(f"{seq}: expected 4 words, got {words}")
        if "nyajin" not in words or not lower_kuku.endswith(" nyajin."):
            issues.append(f"{seq}: missing final nyajin")
        if len([w for w in ("yaluy", "yinyay") if w in words]) != 1:
            issues.append(f"{seq}: demonstrative problem {words}")
        expected_pronoun, expected_demo = FRAME_SPECS[(seq - 1) // 10]
        if words[0] != expected_pronoun:
            issues.append(f"{seq}: expected pronoun {expected_pronoun}, got {words[0]}")
        if words[2] != expected_demo:
            issues.append(f"{seq}: expected demonstrative {expected_demo}, got {words[2]}")
        if words[1] not in TARGET_ORDER:
            issues.append(f"{seq}: word 2 is not a target {words[1]}")
        if words[3] != "nyajin":
            issues.append(f"{seq}: word 4 is not nyajin {words[3]}")
        if lower_kuku.rstrip(".") != " ".join(words):
            issues.append(f"{seq}: Kuku does not match words_used {words}")
        if not frame.startswith("r290_"):
            issues.append(f"{seq}: frame not r290 {frame}")
    return issues


def count_words(db: sqlite3.Connection, words: list[str]) -> dict[str, int]:
    return {
        word: db.execute(
            """
            SELECT COUNT(*)
            FROM sentences s
            WHERE s.status IN ('accepted','revised')
              AND EXISTS (SELECT 1 FROM json_each(s.words_used) WHERE value=?)
            """,
            (word,),
        ).fetchone()[0]
        for word in words
    }


def main() -> int:
    issues = scan_static()
    if issues:
        for issue in issues:
            print(issue)
        raise SystemExit(1)

    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    if db.execute("SELECT 1 FROM batches WHERE label=?", (LABEL,)).fetchone():
        raise SystemExit(f"{LABEL} already exists")
    collisions = []
    for _seq, _english, kuku, *_rest in ROWS:
        existing = db.execute(
            """
            SELECT b.label, s.seq, s.status
            FROM sentences s
            JOIN batches b ON b.id=s.batch_id
            WHERE lower(s.kuku)=lower(?) AND s.status!='rejected'
            """,
            (kuku,),
        ).fetchall()
        if existing:
            collisions.append((kuku, existing))
    if collisions:
        raise SystemExit(f"duplicate preflight failed: {collisions}")

    target_counts = count_words(db, TARGET_ORDER)
    support_counts = count_words(db, ["ngayu", "yundu", "nyulu", "bula", "jana", "yaluy", "yinyay", "nyajin"])
    verified_before, words_before = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()

    notes = (
        "VISIBLE TRADITIONAL WEAPON/TOOL SINGULAR-AND-THIRD PRONOUN PAST PERCEPTION SWEEP AFTER RULE 289 "
        "(Rule 290). 50 under-review rows over bangkal tongs, kurrajan spear point, dara bamboo spear stick, "
        "jaba stick, kunjuri shield, nambarr bamboo stick, wangal boomerang, manbir spear barb, wakay wooden sword, "
        "and wulburr bamboo spear stick. SUPPORT: bare nominative ngayu, yundu, nyulu, bula, and jana, plus yaluy/"
        "yinyay and nyajin. FRAMES: Ngayu TARGET yaluy nyajin, Yundu TARGET yinyay nyajin, Nyulu TARGET yaluy "
        "nyajin, Bula TARGET yinyay nyajin, and Jana TARGET yaluy nyajin. Only bare visible object past perception/"
        "reference rows are opened; cooking, earth-oven/vine source, turtle spearing, fire-hardening, nose-piercing, "
        "weapon use, throwing, hunting, fighting, piercing, cutting, material-source, target case, pronoun case, "
        "demonstrative case, compounds, productive reduplication, nonpast/purposive/reduplicated perception drift, "
        "and support inflection remain closed."
    )
    cur = db.execute(
        "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
        (LABEL, "Rule 290 under review", "operator-guide-2026-07-09", notes, NOW),
    )
    batch_id = cur.lastrowid
    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", NOW, NOW,
            )
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    record_event(db, batch_label=LABEL, phase="source", event_type="source-readback", actor="claude-main", detail="Reread dictionary source lines for visible traditional weapon/tool targets and Patz pronoun, demonstrative, nyaji-ny past, and general past -ny support before authoring Rule 290.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="planning", event_type="lexical-field-scope", actor="claude-main", detail="Selected the past-tense counterpart to Rule 289's visible traditional weapon/tool lexical audit. Reused only the ten already source-checked Yalanji-safe heads and did not open weapon-use, cooking, material-source, turtle, fire-hardening, nose-piercing, throwing, hunting, fighting, piercing, cutting, or case/compound semantics.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="coverage-audit", actor="claude-main", detail=f"Token-aware accepted/revised coverage before banking from words_used JSON: targets={target_counts}; support={support_counts}.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="duplicate-preflight", actor="claude-main", detail="Final 50 Rule 290 weapon/tool past-perception surfaces returned no accepted, revised, or under-review collisions; planned forms are the Rule 289 target set with sourced past nyajin only.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="static-guard-pass", actor="claude-main", detail="Static guard passed: 50 explicit hand-authored rows, one target each, bare nominative pronoun/demonstrative bands, final nyajin, no loans, no denied Kuku forms, no cooking/turtle/fire/nose-piercing/weapon-use/case/compound drift.", command="python3 -m py_compile batch_scripts/bank_332.py && python3 -c \"import sys; sys.path.insert(0,'batch_scripts'); import bank_332; print(bank_332.scan_static())\"", output_excerpt="issues 0", artifact_path="batch_scripts/bank_332.py", created_at=NOW)
    record_event(db, batch_label=LABEL, phase="bank", event_type="bank-success", actor="claude-main", detail=f"Banked 50 Rule 290 rows as under_review. Verified total remains {verified_before}; Kuku word count remains {words_before}.", command="python3 batch_scripts/bank_332.py", output_excerpt=f"banked batch-332: 50 rows under_review; verified_before={verified_before}; kuku_words_before={words_before}", artifact_path=PROCESS_LOG, created_at=NOW)
    db.commit()
    print("banked batch-332: 50 rows under_review")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
