#!/usr/bin/env python3
"""Bank batch-331: Rule 289 visible traditional weapon/tool nonpast 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-331"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-331-visible-traditional-weapon-tool-singular-pronoun-nonpast-perception-after-rule288-process.md"

TARGET_EVIDENCE = {
    "bangkal": "Dictionary lines 1478-1489 give bangkal as noun 'tongs', made from voya vine and used with kurrma. Rule 289 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 289 selects visible spear-point reference only.",
    "dara": "Dictionary lines 5215-5226 give dara as noun 'bamboo spear stick'. Rule 289 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 289 selects visible stick reference only.",
    "kunjuri": "Dictionary lines 15915-15923 give kunjuri as noun 'shield'. Rule 289 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 289 selects visible bamboo-stick reference only.",
    "wangal": "Dictionary lines 25943-25951 give wangal as noun 'boomerang'. Rule 289 selects visible boomerang reference only.",
    "manbir": "Dictionary lines 17699-17707 give manbir as noun 'barb of a spear'. Rule 289 selects visible spear-barb reference only.",
    "wakay": "Dictionary lines 25192-25202 give wakay as noun 'wooden sword'. Rule 289 selects visible wooden-sword reference only.",
    "wulburr": "Dictionary lines 27767-27776 give wulburr as noun 'bamboo spear stick' and 'spear'. Rule 289 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; and lines 3043-3049 source "
    "nyaji-l 'see'."
)

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",
    "nyajin", "nyaji-ny", "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 see the tongs here.", "Ngayu bangkal yaluy nyajil.", "ngayu bangkal yaluy nyajil | 1sg.NOM tongs here see.NPST", "r289_1sg_here_weapon_tool_see_bangkal", ["ngayu", "bangkal", "yaluy", "nyajil"]),
    row(2, "I see the spear point here.", "Ngayu kurrajan yaluy nyajil.", "ngayu kurrajan yaluy nyajil | 1sg.NOM spear.point here see.NPST", "r289_1sg_here_weapon_tool_see_kurrajan", ["ngayu", "kurrajan", "yaluy", "nyajil"]),
    row(3, "I see the bamboo spear stick here.", "Ngayu dara yaluy nyajil.", "ngayu dara yaluy nyajil | 1sg.NOM bamboo.spear.stick here see.NPST", "r289_1sg_here_weapon_tool_see_dara", ["ngayu", "dara", "yaluy", "nyajil"]),
    row(4, "I see the stick here.", "Ngayu jaba yaluy nyajil.", "ngayu jaba yaluy nyajil | 1sg.NOM stick here see.NPST", "r289_1sg_here_weapon_tool_see_jaba", ["ngayu", "jaba", "yaluy", "nyajil"]),
    row(5, "I see the shield here.", "Ngayu kunjuri yaluy nyajil.", "ngayu kunjuri yaluy nyajil | 1sg.NOM shield here see.NPST", "r289_1sg_here_weapon_tool_see_kunjuri", ["ngayu", "kunjuri", "yaluy", "nyajil"]),
    row(6, "I see the bamboo stick here.", "Ngayu nambarr yaluy nyajil.", "ngayu nambarr yaluy nyajil | 1sg.NOM bamboo.stick here see.NPST", "r289_1sg_here_weapon_tool_see_nambarr", ["ngayu", "nambarr", "yaluy", "nyajil"]),
    row(7, "I see the boomerang here.", "Ngayu wangal yaluy nyajil.", "ngayu wangal yaluy nyajil | 1sg.NOM boomerang here see.NPST", "r289_1sg_here_weapon_tool_see_wangal", ["ngayu", "wangal", "yaluy", "nyajil"]),
    row(8, "I see the spear barb here.", "Ngayu manbir yaluy nyajil.", "ngayu manbir yaluy nyajil | 1sg.NOM spear.barb here see.NPST", "r289_1sg_here_weapon_tool_see_manbir", ["ngayu", "manbir", "yaluy", "nyajil"]),
    row(9, "I see the wooden sword here.", "Ngayu wakay yaluy nyajil.", "ngayu wakay yaluy nyajil | 1sg.NOM wooden.sword here see.NPST", "r289_1sg_here_weapon_tool_see_wakay", ["ngayu", "wakay", "yaluy", "nyajil"]),
    row(10, "I see the bamboo spear stick here.", "Ngayu wulburr yaluy nyajil.", "ngayu wulburr yaluy nyajil | 1sg.NOM bamboo.spear.stick here see.NPST", "r289_1sg_here_weapon_tool_see_wulburr", ["ngayu", "wulburr", "yaluy", "nyajil"]),
    row(11, "You see the tongs there.", "Yundu bangkal yinyay nyajil.", "yundu bangkal yinyay nyajil | 2sg.NOM tongs there see.NPST", "r289_2sg_there_weapon_tool_see_bangkal", ["yundu", "bangkal", "yinyay", "nyajil"]),
    row(12, "You see the spear point there.", "Yundu kurrajan yinyay nyajil.", "yundu kurrajan yinyay nyajil | 2sg.NOM spear.point there see.NPST", "r289_2sg_there_weapon_tool_see_kurrajan", ["yundu", "kurrajan", "yinyay", "nyajil"]),
    row(13, "You see the bamboo spear stick there.", "Yundu dara yinyay nyajil.", "yundu dara yinyay nyajil | 2sg.NOM bamboo.spear.stick there see.NPST", "r289_2sg_there_weapon_tool_see_dara", ["yundu", "dara", "yinyay", "nyajil"]),
    row(14, "You see the stick there.", "Yundu jaba yinyay nyajil.", "yundu jaba yinyay nyajil | 2sg.NOM stick there see.NPST", "r289_2sg_there_weapon_tool_see_jaba", ["yundu", "jaba", "yinyay", "nyajil"]),
    row(15, "You see the shield there.", "Yundu kunjuri yinyay nyajil.", "yundu kunjuri yinyay nyajil | 2sg.NOM shield there see.NPST", "r289_2sg_there_weapon_tool_see_kunjuri", ["yundu", "kunjuri", "yinyay", "nyajil"]),
    row(16, "You see the bamboo stick there.", "Yundu nambarr yinyay nyajil.", "yundu nambarr yinyay nyajil | 2sg.NOM bamboo.stick there see.NPST", "r289_2sg_there_weapon_tool_see_nambarr", ["yundu", "nambarr", "yinyay", "nyajil"]),
    row(17, "You see the boomerang there.", "Yundu wangal yinyay nyajil.", "yundu wangal yinyay nyajil | 2sg.NOM boomerang there see.NPST", "r289_2sg_there_weapon_tool_see_wangal", ["yundu", "wangal", "yinyay", "nyajil"]),
    row(18, "You see the spear barb there.", "Yundu manbir yinyay nyajil.", "yundu manbir yinyay nyajil | 2sg.NOM spear.barb there see.NPST", "r289_2sg_there_weapon_tool_see_manbir", ["yundu", "manbir", "yinyay", "nyajil"]),
    row(19, "You see the wooden sword there.", "Yundu wakay yinyay nyajil.", "yundu wakay yinyay nyajil | 2sg.NOM wooden.sword there see.NPST", "r289_2sg_there_weapon_tool_see_wakay", ["yundu", "wakay", "yinyay", "nyajil"]),
    row(20, "You see the bamboo spear stick there.", "Yundu wulburr yinyay nyajil.", "yundu wulburr yinyay nyajil | 2sg.NOM bamboo.spear.stick there see.NPST", "r289_2sg_there_weapon_tool_see_wulburr", ["yundu", "wulburr", "yinyay", "nyajil"]),
    row(21, "He sees the tongs here.", "Nyulu bangkal yaluy nyajil.", "nyulu bangkal yaluy nyajil | 3sg.NOM tongs here see.NPST", "r289_3sg_here_weapon_tool_see_bangkal", ["nyulu", "bangkal", "yaluy", "nyajil"]),
    row(22, "He sees the spear point here.", "Nyulu kurrajan yaluy nyajil.", "nyulu kurrajan yaluy nyajil | 3sg.NOM spear.point here see.NPST", "r289_3sg_here_weapon_tool_see_kurrajan", ["nyulu", "kurrajan", "yaluy", "nyajil"]),
    row(23, "He sees the bamboo spear stick here.", "Nyulu dara yaluy nyajil.", "nyulu dara yaluy nyajil | 3sg.NOM bamboo.spear.stick here see.NPST", "r289_3sg_here_weapon_tool_see_dara", ["nyulu", "dara", "yaluy", "nyajil"]),
    row(24, "He sees the stick here.", "Nyulu jaba yaluy nyajil.", "nyulu jaba yaluy nyajil | 3sg.NOM stick here see.NPST", "r289_3sg_here_weapon_tool_see_jaba", ["nyulu", "jaba", "yaluy", "nyajil"]),
    row(25, "He sees the shield here.", "Nyulu kunjuri yaluy nyajil.", "nyulu kunjuri yaluy nyajil | 3sg.NOM shield here see.NPST", "r289_3sg_here_weapon_tool_see_kunjuri", ["nyulu", "kunjuri", "yaluy", "nyajil"]),
    row(26, "He sees the bamboo stick here.", "Nyulu nambarr yaluy nyajil.", "nyulu nambarr yaluy nyajil | 3sg.NOM bamboo.stick here see.NPST", "r289_3sg_here_weapon_tool_see_nambarr", ["nyulu", "nambarr", "yaluy", "nyajil"]),
    row(27, "He sees the boomerang here.", "Nyulu wangal yaluy nyajil.", "nyulu wangal yaluy nyajil | 3sg.NOM boomerang here see.NPST", "r289_3sg_here_weapon_tool_see_wangal", ["nyulu", "wangal", "yaluy", "nyajil"]),
    row(28, "He sees the spear barb here.", "Nyulu manbir yaluy nyajil.", "nyulu manbir yaluy nyajil | 3sg.NOM spear.barb here see.NPST", "r289_3sg_here_weapon_tool_see_manbir", ["nyulu", "manbir", "yaluy", "nyajil"]),
    row(29, "He sees the wooden sword here.", "Nyulu wakay yaluy nyajil.", "nyulu wakay yaluy nyajil | 3sg.NOM wooden.sword here see.NPST", "r289_3sg_here_weapon_tool_see_wakay", ["nyulu", "wakay", "yaluy", "nyajil"]),
    row(30, "He sees the bamboo spear stick here.", "Nyulu wulburr yaluy nyajil.", "nyulu wulburr yaluy nyajil | 3sg.NOM bamboo.spear.stick here see.NPST", "r289_3sg_here_weapon_tool_see_wulburr", ["nyulu", "wulburr", "yaluy", "nyajil"]),
    row(31, "They two see the tongs there.", "Bula bangkal yinyay nyajil.", "bula bangkal yinyay nyajil | 3du.NOM tongs there see.NPST", "r289_3du_there_weapon_tool_see_bangkal", ["bula", "bangkal", "yinyay", "nyajil"]),
    row(32, "They two see the spear point there.", "Bula kurrajan yinyay nyajil.", "bula kurrajan yinyay nyajil | 3du.NOM spear.point there see.NPST", "r289_3du_there_weapon_tool_see_kurrajan", ["bula", "kurrajan", "yinyay", "nyajil"]),
    row(33, "They two see the bamboo spear stick there.", "Bula dara yinyay nyajil.", "bula dara yinyay nyajil | 3du.NOM bamboo.spear.stick there see.NPST", "r289_3du_there_weapon_tool_see_dara", ["bula", "dara", "yinyay", "nyajil"]),
    row(34, "They two see the stick there.", "Bula jaba yinyay nyajil.", "bula jaba yinyay nyajil | 3du.NOM stick there see.NPST", "r289_3du_there_weapon_tool_see_jaba", ["bula", "jaba", "yinyay", "nyajil"]),
    row(35, "They two see the shield there.", "Bula kunjuri yinyay nyajil.", "bula kunjuri yinyay nyajil | 3du.NOM shield there see.NPST", "r289_3du_there_weapon_tool_see_kunjuri", ["bula", "kunjuri", "yinyay", "nyajil"]),
    row(36, "They two see the bamboo stick there.", "Bula nambarr yinyay nyajil.", "bula nambarr yinyay nyajil | 3du.NOM bamboo.stick there see.NPST", "r289_3du_there_weapon_tool_see_nambarr", ["bula", "nambarr", "yinyay", "nyajil"]),
    row(37, "They two see the boomerang there.", "Bula wangal yinyay nyajil.", "bula wangal yinyay nyajil | 3du.NOM boomerang there see.NPST", "r289_3du_there_weapon_tool_see_wangal", ["bula", "wangal", "yinyay", "nyajil"]),
    row(38, "They two see the spear barb there.", "Bula manbir yinyay nyajil.", "bula manbir yinyay nyajil | 3du.NOM spear.barb there see.NPST", "r289_3du_there_weapon_tool_see_manbir", ["bula", "manbir", "yinyay", "nyajil"]),
    row(39, "They two see the wooden sword there.", "Bula wakay yinyay nyajil.", "bula wakay yinyay nyajil | 3du.NOM wooden.sword there see.NPST", "r289_3du_there_weapon_tool_see_wakay", ["bula", "wakay", "yinyay", "nyajil"]),
    row(40, "They two see the bamboo spear stick there.", "Bula wulburr yinyay nyajil.", "bula wulburr yinyay nyajil | 3du.NOM bamboo.spear.stick there see.NPST", "r289_3du_there_weapon_tool_see_wulburr", ["bula", "wulburr", "yinyay", "nyajil"]),
    row(41, "They see the tongs here.", "Jana bangkal yaluy nyajil.", "jana bangkal yaluy nyajil | 3pl.NOM tongs here see.NPST", "r289_3pl_here_weapon_tool_see_bangkal", ["jana", "bangkal", "yaluy", "nyajil"]),
    row(42, "They see the spear point here.", "Jana kurrajan yaluy nyajil.", "jana kurrajan yaluy nyajil | 3pl.NOM spear.point here see.NPST", "r289_3pl_here_weapon_tool_see_kurrajan", ["jana", "kurrajan", "yaluy", "nyajil"]),
    row(43, "They see the bamboo spear stick here.", "Jana dara yaluy nyajil.", "jana dara yaluy nyajil | 3pl.NOM bamboo.spear.stick here see.NPST", "r289_3pl_here_weapon_tool_see_dara", ["jana", "dara", "yaluy", "nyajil"]),
    row(44, "They see the stick here.", "Jana jaba yaluy nyajil.", "jana jaba yaluy nyajil | 3pl.NOM stick here see.NPST", "r289_3pl_here_weapon_tool_see_jaba", ["jana", "jaba", "yaluy", "nyajil"]),
    row(45, "They see the shield here.", "Jana kunjuri yaluy nyajil.", "jana kunjuri yaluy nyajil | 3pl.NOM shield here see.NPST", "r289_3pl_here_weapon_tool_see_kunjuri", ["jana", "kunjuri", "yaluy", "nyajil"]),
    row(46, "They see the bamboo stick here.", "Jana nambarr yaluy nyajil.", "jana nambarr yaluy nyajil | 3pl.NOM bamboo.stick here see.NPST", "r289_3pl_here_weapon_tool_see_nambarr", ["jana", "nambarr", "yaluy", "nyajil"]),
    row(47, "They see the boomerang here.", "Jana wangal yaluy nyajil.", "jana wangal yaluy nyajil | 3pl.NOM boomerang here see.NPST", "r289_3pl_here_weapon_tool_see_wangal", ["jana", "wangal", "yaluy", "nyajil"]),
    row(48, "They see the spear barb here.", "Jana manbir yaluy nyajil.", "jana manbir yaluy nyajil | 3pl.NOM spear.barb here see.NPST", "r289_3pl_here_weapon_tool_see_manbir", ["jana", "manbir", "yaluy", "nyajil"]),
    row(49, "They see the wooden sword here.", "Jana wakay yaluy nyajil.", "jana wakay yaluy nyajil | 3pl.NOM wooden.sword here see.NPST", "r289_3pl_here_weapon_tool_see_wakay", ["jana", "wakay", "yaluy", "nyajil"]),
    row(50, "They see the bamboo spear stick here.", "Jana wulburr yaluy nyajil.", "jana wulburr yaluy nyajil | 3pl.NOM bamboo.spear.stick here see.NPST", "r289_3pl_here_weapon_tool_see_wulburr", ["jana", "wulburr", "yaluy", "nyajil"]),
]


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 "nyajil" not in words or not lower_kuku.endswith(" nyajil."):
            issues.append(f"{seq}: missing final nyajil")
        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] != "nyajil":
            issues.append(f"{seq}: word 4 is not nyajil {words[3]}")
        if lower_kuku.rstrip(".") != " ".join(words):
            issues.append(f"{seq}: Kuku does not match words_used {words}")
        if not frame.startswith("r289_"):
            issues.append(f"{seq}: frame not r289 {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", "nyajil"])
    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 NONPAST PERCEPTION SWEEP AFTER RULE 288 "
        "(Rule 289). 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 nyajil. FRAMES: Ngayu TARGET yaluy nyajil, Yundu TARGET yinyay nyajil, Nyulu TARGET yaluy "
        "nyajil, Bula TARGET yinyay nyajil, and Jana TARGET yaluy nyajil. Only bare visible object nonpast "
        "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, past/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 289 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, and nyaji-l nonpast support before authoring Rule 289.", 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 a fresh visible traditional weapon/tool lexical audit after the Rule 285-288 artefact/tool consolidation cycle. Excluded collided heads durray, jinil, mirru, kurajan, ngurmu, and birungubay, and excluded Nyungkal-labelled biyul.", 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 289 weapon/tool surfaces returned no accepted, revised, or under-review collisions after replacing collided prior Batch 231/232 heads and Nyungkal-labelled biyul.", 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 nyajil, 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_331.py && python3 -c \"import sys; sys.path.insert(0,'batch_scripts'); import bank_331; print(bank_331.scan_static())\"", output_excerpt="issues 0", artifact_path="batch_scripts/bank_331.py", created_at=NOW)
    record_event(db, batch_label=LABEL, phase="bank", event_type="bank-success", actor="claude-main", detail=f"Banked 50 Rule 289 rows as under_review. Verified total remains {verified_before}; Kuku word count remains {words_before}.", command="python3 batch_scripts/bank_331.py", output_excerpt=f"banked batch-331: 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-331: 50 rows under_review")
    return 0


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