#!/usr/bin/env python3
"""Bank batch-336: Rule 294 complementary weapon/tool non-singular 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-336"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-336-traditional-weapon-tool-complementary-nonsingular-past-perception-after-rule293-process.md"

TARGET_EVIDENCE = {
    "bangkal": "Dictionary lines 1478-1489 give bangkal as noun 'tongs', made from voya vine and used with kurrma. Rule 294 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 294 selects visible spear-point reference only.",
    "dara": "Dictionary lines 5215-5226 give dara as noun 'bamboo spear stick'. Rule 294 selects visible spear-stick reference only.",
    "jaba": "Dictionary lines 6843-6857 give jaba as noun 'stick', also sapling, post, walking stick, and horn. Rule 294 selects visible stick reference only.",
    "kunjuri": "Dictionary lines 15915-15923 give kunjuri as noun 'shield'. Rule 294 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 294 selects visible bamboo-stick reference only.",
    "wangal": "Dictionary lines 25943-25951 give wangal as noun 'boomerang'. Rule 294 selects visible boomerang reference only.",
    "manbir": "Dictionary lines 17699-17707 give manbir as noun 'barb of a spear'. Rule 294 selects visible spear-barb reference only.",
    "wakay": "Dictionary lines 25192-25202 give wakay as noun 'wooden sword'. Rule 294 selects visible wooden-sword reference only.",
    "wulburr": "Dictionary lines 27767-27776 give wulburr as noun 'bamboo spear stick' and 'spear'. Rule 294 selects visible bamboo-spear-stick reference only.",
}

SUPPORT_EVIDENCE = (
    "Patz lines 2577-2603 source the bare nominative pronouns used here: ngali, ngaliny, yubal, "
    "nganjin, and ngana; lines 2674-2686 source the non-singular person/number contrasts; lines 2838-2845 "
    "source yaluy/yinyay; lines 2023 and 2568-2571 source nyaji-ny 'see-PAST'; and lines 3149-3162 source "
    "the past-tense contrast. Rule 294 uses corpus-conventional nyajin as the past visual-perception surface."
)

TARGET_ORDER = ["bangkal", "kurrajan", "dara", "jaba", "kunjuri", "nambarr", "wangal", "manbir", "wakay", "wulburr"]
FRAME_SPECS = [
    ("ngali", "yinyay"),
    ("ngaliny", "yaluy"),
    ("yubal", "yinyay"),
    ("nganjin", "yaluy"),
    ("ngana", "yinyay"),
]

DENY_KUKU = {
    "biyul", "durray", "jinil", "mirru", "kurajan", "ngurmu", "birungubay", "biruwybay", "birubay",
    "bangkalji", "kurrajanji", "daraji", "jabaji", "kunjuriji", "nambarriji", "wangalji",
    "manbirji", "wakayji", "wulburrji", "ngalingan", "ngalinyan", "yubalan", "nganjinan",
    "nganangan", "ngalinka", "ngalinyanka", "yubalanka", "nganjinanka", "ngananka", "ngalinda",
    "ngalinyanda", "yubalanda", "nganjinanda", "ngananda", "ngalinji", "ngalinyanji", "yubalanji",
    "nganjinanji", "ngananji", "ngalinga", "ngalinyanga", "yubalanga", "nganjinanga", "ngananga",
    "nyajil", "nyaji-l", "nyaji-nka", "nyaji-l-nyaji", "yinyangka", "yinyayngka",
    "yinyamba", "yinyamuny", "yalungku", "yaluyngku", "yaluymbu", "yaluyundu", "yalamuny",
}
DENY_ENGLISH = (
    "cook", "cooking", "earth oven", "voya", "vine", "gather", "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, "We two (not you) saw the tongs there.", "Ngali bangkal yinyay nyajin.", "ngali bangkal yinyay nyajin | 1du.excl.NOM tongs there see.PAST", "r294_1du_excl_there_weapon_tool_saw_bangkal", ["ngali", "bangkal", "yinyay", "nyajin"]),
    row(2, "We two (not you) saw the spear point there.", "Ngali kurrajan yinyay nyajin.", "ngali kurrajan yinyay nyajin | 1du.excl.NOM spear.point there see.PAST", "r294_1du_excl_there_weapon_tool_saw_kurrajan", ["ngali", "kurrajan", "yinyay", "nyajin"]),
    row(3, "We two (not you) saw the bamboo spear stick there.", "Ngali dara yinyay nyajin.", "ngali dara yinyay nyajin | 1du.excl.NOM bamboo.spear.stick there see.PAST", "r294_1du_excl_there_weapon_tool_saw_dara", ["ngali", "dara", "yinyay", "nyajin"]),
    row(4, "We two (not you) saw the stick there.", "Ngali jaba yinyay nyajin.", "ngali jaba yinyay nyajin | 1du.excl.NOM stick there see.PAST", "r294_1du_excl_there_weapon_tool_saw_jaba", ["ngali", "jaba", "yinyay", "nyajin"]),
    row(5, "We two (not you) saw the shield there.", "Ngali kunjuri yinyay nyajin.", "ngali kunjuri yinyay nyajin | 1du.excl.NOM shield there see.PAST", "r294_1du_excl_there_weapon_tool_saw_kunjuri", ["ngali", "kunjuri", "yinyay", "nyajin"]),
    row(6, "We two (not you) saw the bamboo stick there.", "Ngali nambarr yinyay nyajin.", "ngali nambarr yinyay nyajin | 1du.excl.NOM bamboo.stick there see.PAST", "r294_1du_excl_there_weapon_tool_saw_nambarr", ["ngali", "nambarr", "yinyay", "nyajin"]),
    row(7, "We two (not you) saw the boomerang there.", "Ngali wangal yinyay nyajin.", "ngali wangal yinyay nyajin | 1du.excl.NOM boomerang there see.PAST", "r294_1du_excl_there_weapon_tool_saw_wangal", ["ngali", "wangal", "yinyay", "nyajin"]),
    row(8, "We two (not you) saw the spear barb there.", "Ngali manbir yinyay nyajin.", "ngali manbir yinyay nyajin | 1du.excl.NOM spear.barb there see.PAST", "r294_1du_excl_there_weapon_tool_saw_manbir", ["ngali", "manbir", "yinyay", "nyajin"]),
    row(9, "We two (not you) saw the wooden sword there.", "Ngali wakay yinyay nyajin.", "ngali wakay yinyay nyajin | 1du.excl.NOM wooden.sword there see.PAST", "r294_1du_excl_there_weapon_tool_saw_wakay", ["ngali", "wakay", "yinyay", "nyajin"]),
    row(10, "We two (not you) saw the bamboo spear stick there.", "Ngali wulburr yinyay nyajin.", "ngali wulburr yinyay nyajin | 1du.excl.NOM bamboo.spear.stick there see.PAST", "r294_1du_excl_there_weapon_tool_saw_wulburr", ["ngali", "wulburr", "yinyay", "nyajin"]),
    row(11, "You and I saw the tongs here.", "Ngaliny bangkal yaluy nyajin.", "ngaliny bangkal yaluy nyajin | 1du.incl.NOM tongs here see.PAST", "r294_1du_incl_here_weapon_tool_saw_bangkal", ["ngaliny", "bangkal", "yaluy", "nyajin"]),
    row(12, "You and I saw the spear point here.", "Ngaliny kurrajan yaluy nyajin.", "ngaliny kurrajan yaluy nyajin | 1du.incl.NOM spear.point here see.PAST", "r294_1du_incl_here_weapon_tool_saw_kurrajan", ["ngaliny", "kurrajan", "yaluy", "nyajin"]),
    row(13, "You and I saw the bamboo spear stick here.", "Ngaliny dara yaluy nyajin.", "ngaliny dara yaluy nyajin | 1du.incl.NOM bamboo.spear.stick here see.PAST", "r294_1du_incl_here_weapon_tool_saw_dara", ["ngaliny", "dara", "yaluy", "nyajin"]),
    row(14, "You and I saw the stick here.", "Ngaliny jaba yaluy nyajin.", "ngaliny jaba yaluy nyajin | 1du.incl.NOM stick here see.PAST", "r294_1du_incl_here_weapon_tool_saw_jaba", ["ngaliny", "jaba", "yaluy", "nyajin"]),
    row(15, "You and I saw the shield here.", "Ngaliny kunjuri yaluy nyajin.", "ngaliny kunjuri yaluy nyajin | 1du.incl.NOM shield here see.PAST", "r294_1du_incl_here_weapon_tool_saw_kunjuri", ["ngaliny", "kunjuri", "yaluy", "nyajin"]),
    row(16, "You and I saw the bamboo stick here.", "Ngaliny nambarr yaluy nyajin.", "ngaliny nambarr yaluy nyajin | 1du.incl.NOM bamboo.stick here see.PAST", "r294_1du_incl_here_weapon_tool_saw_nambarr", ["ngaliny", "nambarr", "yaluy", "nyajin"]),
    row(17, "You and I saw the boomerang here.", "Ngaliny wangal yaluy nyajin.", "ngaliny wangal yaluy nyajin | 1du.incl.NOM boomerang here see.PAST", "r294_1du_incl_here_weapon_tool_saw_wangal", ["ngaliny", "wangal", "yaluy", "nyajin"]),
    row(18, "You and I saw the spear barb here.", "Ngaliny manbir yaluy nyajin.", "ngaliny manbir yaluy nyajin | 1du.incl.NOM spear.barb here see.PAST", "r294_1du_incl_here_weapon_tool_saw_manbir", ["ngaliny", "manbir", "yaluy", "nyajin"]),
    row(19, "You and I saw the wooden sword here.", "Ngaliny wakay yaluy nyajin.", "ngaliny wakay yaluy nyajin | 1du.incl.NOM wooden.sword here see.PAST", "r294_1du_incl_here_weapon_tool_saw_wakay", ["ngaliny", "wakay", "yaluy", "nyajin"]),
    row(20, "You and I saw the bamboo spear stick here.", "Ngaliny wulburr yaluy nyajin.", "ngaliny wulburr yaluy nyajin | 1du.incl.NOM bamboo.spear.stick here see.PAST", "r294_1du_incl_here_weapon_tool_saw_wulburr", ["ngaliny", "wulburr", "yaluy", "nyajin"]),
    row(21, "You two saw the tongs there.", "Yubal bangkal yinyay nyajin.", "yubal bangkal yinyay nyajin | 2du.NOM tongs there see.PAST", "r294_2du_there_weapon_tool_saw_bangkal", ["yubal", "bangkal", "yinyay", "nyajin"]),
    row(22, "You two saw the spear point there.", "Yubal kurrajan yinyay nyajin.", "yubal kurrajan yinyay nyajin | 2du.NOM spear.point there see.PAST", "r294_2du_there_weapon_tool_saw_kurrajan", ["yubal", "kurrajan", "yinyay", "nyajin"]),
    row(23, "You two saw the bamboo spear stick there.", "Yubal dara yinyay nyajin.", "yubal dara yinyay nyajin | 2du.NOM bamboo.spear.stick there see.PAST", "r294_2du_there_weapon_tool_saw_dara", ["yubal", "dara", "yinyay", "nyajin"]),
    row(24, "You two saw the stick there.", "Yubal jaba yinyay nyajin.", "yubal jaba yinyay nyajin | 2du.NOM stick there see.PAST", "r294_2du_there_weapon_tool_saw_jaba", ["yubal", "jaba", "yinyay", "nyajin"]),
    row(25, "You two saw the shield there.", "Yubal kunjuri yinyay nyajin.", "yubal kunjuri yinyay nyajin | 2du.NOM shield there see.PAST", "r294_2du_there_weapon_tool_saw_kunjuri", ["yubal", "kunjuri", "yinyay", "nyajin"]),
    row(26, "You two saw the bamboo stick there.", "Yubal nambarr yinyay nyajin.", "yubal nambarr yinyay nyajin | 2du.NOM bamboo.stick there see.PAST", "r294_2du_there_weapon_tool_saw_nambarr", ["yubal", "nambarr", "yinyay", "nyajin"]),
    row(27, "You two saw the boomerang there.", "Yubal wangal yinyay nyajin.", "yubal wangal yinyay nyajin | 2du.NOM boomerang there see.PAST", "r294_2du_there_weapon_tool_saw_wangal", ["yubal", "wangal", "yinyay", "nyajin"]),
    row(28, "You two saw the spear barb there.", "Yubal manbir yinyay nyajin.", "yubal manbir yinyay nyajin | 2du.NOM spear.barb there see.PAST", "r294_2du_there_weapon_tool_saw_manbir", ["yubal", "manbir", "yinyay", "nyajin"]),
    row(29, "You two saw the wooden sword there.", "Yubal wakay yinyay nyajin.", "yubal wakay yinyay nyajin | 2du.NOM wooden.sword there see.PAST", "r294_2du_there_weapon_tool_saw_wakay", ["yubal", "wakay", "yinyay", "nyajin"]),
    row(30, "You two saw the bamboo spear stick there.", "Yubal wulburr yinyay nyajin.", "yubal wulburr yinyay nyajin | 2du.NOM bamboo.spear.stick there see.PAST", "r294_2du_there_weapon_tool_saw_wulburr", ["yubal", "wulburr", "yinyay", "nyajin"]),
    row(31, "We all, not you, saw the tongs here.", "Nganjin bangkal yaluy nyajin.", "nganjin bangkal yaluy nyajin | 1pl.excl.NOM tongs here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_bangkal", ["nganjin", "bangkal", "yaluy", "nyajin"]),
    row(32, "We all, not you, saw the spear point here.", "Nganjin kurrajan yaluy nyajin.", "nganjin kurrajan yaluy nyajin | 1pl.excl.NOM spear.point here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_kurrajan", ["nganjin", "kurrajan", "yaluy", "nyajin"]),
    row(33, "We all, not you, saw the bamboo spear stick here.", "Nganjin dara yaluy nyajin.", "nganjin dara yaluy nyajin | 1pl.excl.NOM bamboo.spear.stick here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_dara", ["nganjin", "dara", "yaluy", "nyajin"]),
    row(34, "We all, not you, saw the stick here.", "Nganjin jaba yaluy nyajin.", "nganjin jaba yaluy nyajin | 1pl.excl.NOM stick here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_jaba", ["nganjin", "jaba", "yaluy", "nyajin"]),
    row(35, "We all, not you, saw the shield here.", "Nganjin kunjuri yaluy nyajin.", "nganjin kunjuri yaluy nyajin | 1pl.excl.NOM shield here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_kunjuri", ["nganjin", "kunjuri", "yaluy", "nyajin"]),
    row(36, "We all, not you, saw the bamboo stick here.", "Nganjin nambarr yaluy nyajin.", "nganjin nambarr yaluy nyajin | 1pl.excl.NOM bamboo.stick here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_nambarr", ["nganjin", "nambarr", "yaluy", "nyajin"]),
    row(37, "We all, not you, saw the boomerang here.", "Nganjin wangal yaluy nyajin.", "nganjin wangal yaluy nyajin | 1pl.excl.NOM boomerang here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_wangal", ["nganjin", "wangal", "yaluy", "nyajin"]),
    row(38, "We all, not you, saw the spear barb here.", "Nganjin manbir yaluy nyajin.", "nganjin manbir yaluy nyajin | 1pl.excl.NOM spear.barb here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_manbir", ["nganjin", "manbir", "yaluy", "nyajin"]),
    row(39, "We all, not you, saw the wooden sword here.", "Nganjin wakay yaluy nyajin.", "nganjin wakay yaluy nyajin | 1pl.excl.NOM wooden.sword here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_wakay", ["nganjin", "wakay", "yaluy", "nyajin"]),
    row(40, "We all, not you, saw the bamboo spear stick here.", "Nganjin wulburr yaluy nyajin.", "nganjin wulburr yaluy nyajin | 1pl.excl.NOM bamboo.spear.stick here see.PAST", "r294_1pl_excl_here_weapon_tool_saw_wulburr", ["nganjin", "wulburr", "yaluy", "nyajin"]),
    row(41, "We saw the tongs there.", "Ngana bangkal yinyay nyajin.", "ngana bangkal yinyay nyajin | 1pl.incl.NOM tongs there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_bangkal", ["ngana", "bangkal", "yinyay", "nyajin"]),
    row(42, "We saw the spear point there.", "Ngana kurrajan yinyay nyajin.", "ngana kurrajan yinyay nyajin | 1pl.incl.NOM spear.point there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_kurrajan", ["ngana", "kurrajan", "yinyay", "nyajin"]),
    row(43, "We saw the bamboo spear stick there.", "Ngana dara yinyay nyajin.", "ngana dara yinyay nyajin | 1pl.incl.NOM bamboo.spear.stick there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_dara", ["ngana", "dara", "yinyay", "nyajin"]),
    row(44, "We saw the stick there.", "Ngana jaba yinyay nyajin.", "ngana jaba yinyay nyajin | 1pl.incl.NOM stick there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_jaba", ["ngana", "jaba", "yinyay", "nyajin"]),
    row(45, "We saw the shield there.", "Ngana kunjuri yinyay nyajin.", "ngana kunjuri yinyay nyajin | 1pl.incl.NOM shield there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_kunjuri", ["ngana", "kunjuri", "yinyay", "nyajin"]),
    row(46, "We saw the bamboo stick there.", "Ngana nambarr yinyay nyajin.", "ngana nambarr yinyay nyajin | 1pl.incl.NOM bamboo.stick there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_nambarr", ["ngana", "nambarr", "yinyay", "nyajin"]),
    row(47, "We saw the boomerang there.", "Ngana wangal yinyay nyajin.", "ngana wangal yinyay nyajin | 1pl.incl.NOM boomerang there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_wangal", ["ngana", "wangal", "yinyay", "nyajin"]),
    row(48, "We saw the spear barb there.", "Ngana manbir yinyay nyajin.", "ngana manbir yinyay nyajin | 1pl.incl.NOM spear.barb there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_manbir", ["ngana", "manbir", "yinyay", "nyajin"]),
    row(49, "We saw the wooden sword there.", "Ngana wakay yinyay nyajin.", "ngana wakay yinyay nyajin | 1pl.incl.NOM wooden.sword there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_wakay", ["ngana", "wakay", "yinyay", "nyajin"]),
    row(50, "We saw the bamboo spear stick there.", "Ngana wulburr yinyay nyajin.", "ngana wulburr yinyay nyajin | 1pl.incl.NOM bamboo.spear.stick there see.PAST", "r294_1pl_incl_there_weapon_tool_saw_wulburr", ["ngana", "wulburr", "yinyay", "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] != TARGET_ORDER[(seq - 1) % 10]:
            issues.append(f"{seq}: target order mismatch {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("r294_"):
            issues.append(f"{seq}: frame not r294 {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, ["ngali", "ngaliny", "yubal", "nganjin", "ngana", "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 COMPLEMENTARY NON-SINGULAR PAST PERCEPTION SWEEP AFTER RULE 293 "
        "(Rule 294). 50 under-review rows over ten checked targets: 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 ngali, ngaliny, yubal, "
        "nganjin, and ngana, plus yaluy/yinyay and nyajin. Only bare visible object past perception/reference "
        "rows are opened; weapon use, cooking, turtle spearing, fire-hardening, nose-piercing, 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 294 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 ten visible traditional weapon/tool targets and Patz non-singular pronoun, demonstrative, and nyaji-ny past support before authoring Rule 294.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="planning", event_type="past-counterpart-scope", actor="claude-main", detail="Selected the sourced past counterpart to Rule 293's complementary local-demonstrative non-singular nonpast perception bands. The batch fills zero-collision ngali, ngaliny, yubal, nganjin, and ngana bands and does not open weapon-use or source-prose 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 294 traditional weapon/tool complementary non-singular past surfaces returned no accepted, revised, or under-review collisions.", 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 non-singular pronoun bands, bare yaluy/yinyay, final nyajin, no loans, no denied Kuku forms, and no weapon-use/source-prose/case/compound drift.", command="python3 -m py_compile batch_scripts/bank_336.py && python3 -c \"import sys; sys.path.insert(0,'batch_scripts'); import bank_336; print(bank_336.scan_static())\"", output_excerpt="issues 0", artifact_path="batch_scripts/bank_336.py", created_at=NOW)
    record_event(db, batch_label=LABEL, phase="bank", event_type="bank-success", actor="claude-main", detail=f"Banked 50 Rule 294 rows as under_review. Verified total remains {verified_before}; Kuku word count remains {words_before}.", command="python3 batch_scripts/bank_336.py", output_excerpt=f"banked batch-336: 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-336: 50 rows under_review")
    return 0


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