#!/usr/bin/env python3
"""Materialize and manually ratify four historical runtime-assembled batches."""
from __future__ import annotations

import ast
import datetime as dt
import hashlib
import sqlite3
from pathlib import Path

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"
ARTIFACT = "batch_scripts/remediate_literal_authorship_239_240_241_329.py"
LOG = "process_logs/literal-authorship-remediation-batches-239-240-241-329.md"
LENSES = ("morphology", "lexicon", "syntax", "fidelity", "register")
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")


def row(seq: int, english: str, kuku: str, analysis: str) -> tuple[int, str, str, str]:
    return seq, english, kuku, analysis


ROWS_239 = [
row(1, 'This stone is black.', 'Yala kulji ngumbu.', 'yala kulji ngumbu | this stone black'),
row(2, 'This tree is black.', 'Yala juku ngumbu.', 'yala juku ngumbu | this tree black'),
row(3, 'This cloud is dark.', 'Yala ngurrban ngumbu.', 'yala ngurrban ngumbu | this cloud dark'),
row(4, 'This hair is light-coloured.', 'Yala mungka bingaji.', 'yala mungka bingaji | this hair light-coloured'),
row(5, 'This clothing is light-coloured.', 'Yala kambi bingaji.', 'yala kambi bingaji | this clothing light-coloured'),
row(6, 'This flower is red.', 'Yala nganka ngala-ngala.', 'yala nganka ngala-ngala | this flower red'),
row(7, 'This king parrot is red.', 'Yala karrambil ngala-ngala.', 'yala karrambil ngala-ngala | this king parrot red'),
row(8, 'This water is clear.', 'Yala bana dayirr.', 'yala bana dayirr | this water clear'),
row(9, 'This ground is clean.', 'Yala bubu dayirr.', 'yala bubu dayirr | this ground clean'),
row(10, 'This house is clean.', 'Yala bayan dayirr.', 'yala bayan dayirr | this house clean'),
row(11, 'The black stone is here.', 'Kulji ngumbu yaluy.', 'kulji ngumbu yaluy | stone black here'),
row(12, 'The black tree is here.', 'Juku ngumbu yaluy.', 'juku ngumbu yaluy | tree black here'),
row(13, 'The dark cloud is here.', 'Ngurrban ngumbu yaluy.', 'ngurrban ngumbu yaluy | cloud dark here'),
row(14, 'The light-coloured hair is here.', 'Mungka bingaji yaluy.', 'mungka bingaji yaluy | hair light-coloured here'),
row(15, 'The light-coloured clothing is here.', 'Kambi bingaji yaluy.', 'kambi bingaji yaluy | clothing light-coloured here'),
row(16, 'The red flower is here.', 'Nganka ngala-ngala yaluy.', 'nganka ngala-ngala yaluy | flower red here'),
row(17, 'The red king parrot is here.', 'Karrambil ngala-ngala yaluy.', 'karrambil ngala-ngala yaluy | king parrot red here'),
row(18, 'The clear water is here.', 'Bana dayirr yaluy.', 'bana dayirr yaluy | water clear here'),
row(19, 'The clean ground is here.', 'Bubu dayirr yaluy.', 'bubu dayirr yaluy | ground clean here'),
row(20, 'The clean house is here.', 'Bayan dayirr yaluy.', 'bayan dayirr yaluy | house clean here'),
row(21, 'That black stone is there.', 'Yinya kulji ngumbu yinyay.', 'yinya kulji ngumbu yinyay | that stone black there'),
row(22, 'That black tree is there.', 'Yinya juku ngumbu yinyay.', 'yinya juku ngumbu yinyay | that tree black there'),
row(23, 'That dark cloud is there.', 'Yinya ngurrban ngumbu yinyay.', 'yinya ngurrban ngumbu yinyay | that cloud dark there'),
row(24, 'That light-coloured hair is there.', 'Yinya mungka bingaji yinyay.', 'yinya mungka bingaji yinyay | that hair light-coloured there'),
row(25, 'That light-coloured clothing is there.', 'Yinya kambi bingaji yinyay.', 'yinya kambi bingaji yinyay | that clothing light-coloured there'),
row(26, 'That red flower is there.', 'Yinya nganka ngala-ngala yinyay.', 'yinya nganka ngala-ngala yinyay | that flower red there'),
row(27, 'That red king parrot is there.', 'Yinya karrambil ngala-ngala yinyay.', 'yinya karrambil ngala-ngala yinyay | that king parrot red there'),
row(28, 'That clear water is there.', 'Yinya bana dayirr yinyay.', 'yinya bana dayirr yinyay | that water clear there'),
row(29, 'That clean ground is there.', 'Yinya bubu dayirr yinyay.', 'yinya bubu dayirr yinyay | that ground clean there'),
row(30, 'That clean house is there.', 'Yinya bayan dayirr yinyay.', 'yinya bayan dayirr yinyay | that house clean there'),
row(31, 'The black stone is there.', 'Kulji ngumbu yinyay.', 'kulji ngumbu yinyay | stone black there'),
row(32, 'The black tree is there.', 'Juku ngumbu yinyay.', 'juku ngumbu yinyay | tree black there'),
row(33, 'The dark cloud is there.', 'Ngurrban ngumbu yinyay.', 'ngurrban ngumbu yinyay | cloud dark there'),
row(34, 'The light-coloured hair is there.', 'Mungka bingaji yinyay.', 'mungka bingaji yinyay | hair light-coloured there'),
row(35, 'The light-coloured clothing is there.', 'Kambi bingaji yinyay.', 'kambi bingaji yinyay | clothing light-coloured there'),
row(36, 'The red flower is there.', 'Nganka ngala-ngala yinyay.', 'nganka ngala-ngala yinyay | flower red there'),
row(37, 'The red king parrot is there.', 'Karrambil ngala-ngala yinyay.', 'karrambil ngala-ngala yinyay | king parrot red there'),
row(38, 'The clear water is there.', 'Bana dayirr yinyay.', 'bana dayirr yinyay | water clear there'),
row(39, 'The clean ground is there.', 'Bubu dayirr yinyay.', 'bubu dayirr yinyay | ground clean there'),
row(40, 'The clean house is there.', 'Bayan dayirr yinyay.', 'bayan dayirr yinyay | house clean there'),
row(41, 'This black stone is here.', 'Yala kulji ngumbu yaluy.', 'yala kulji ngumbu yaluy | this stone black here'),
row(42, 'This black tree is here.', 'Yala juku ngumbu yaluy.', 'yala juku ngumbu yaluy | this tree black here'),
row(43, 'This dark cloud is here.', 'Yala ngurrban ngumbu yaluy.', 'yala ngurrban ngumbu yaluy | this cloud dark here'),
row(44, 'This light-coloured hair is here.', 'Yala mungka bingaji yaluy.', 'yala mungka bingaji yaluy | this hair light-coloured here'),
row(45, 'This light-coloured clothing is here.', 'Yala kambi bingaji yaluy.', 'yala kambi bingaji yaluy | this clothing light-coloured here'),
row(46, 'This red flower is here.', 'Yala nganka ngala-ngala yaluy.', 'yala nganka ngala-ngala yaluy | this flower red here'),
row(47, 'This red king parrot is here.', 'Yala karrambil ngala-ngala yaluy.', 'yala karrambil ngala-ngala yaluy | this king parrot red here'),
row(48, 'This clear water is here.', 'Yala bana dayirr yaluy.', 'yala bana dayirr yaluy | this water clear here'),
row(49, 'This clean ground is here.', 'Yala bubu dayirr yaluy.', 'yala bubu dayirr yaluy | this ground clean here'),
row(50, 'This clean house is here.', 'Yala bayan dayirr yaluy.', 'yala bayan dayirr yaluy | this house clean here'),
]

ROWS_240 = [
row(1, 'I see the black stone.', 'Ngayu kulji ngumbu nyajil.', 'ngayu kulji ngumbu nyaji-l | I stone black see-NONPAST'),
row(2, 'I see the black tree.', 'Ngayu juku ngumbu nyajil.', 'ngayu juku ngumbu nyaji-l | I tree black see-NONPAST'),
row(3, 'I see the dark cloud.', 'Ngayu ngurrban ngumbu nyajil.', 'ngayu ngurrban ngumbu nyaji-l | I cloud dark see-NONPAST'),
row(4, 'I see the light-coloured hair.', 'Ngayu mungka bingaji nyajil.', 'ngayu mungka bingaji nyaji-l | I hair light-coloured see-NONPAST'),
row(5, 'I see the light-coloured clothing.', 'Ngayu kambi bingaji nyajil.', 'ngayu kambi bingaji nyaji-l | I clothing light-coloured see-NONPAST'),
row(6, 'I see the red flower.', 'Ngayu nganka ngala-ngala nyajil.', 'ngayu nganka ngala-ngala nyaji-l | I flower red see-NONPAST'),
row(7, 'I see the red king parrot.', 'Ngayu karrambil ngala-ngala nyajil.', 'ngayu karrambil ngala-ngala nyaji-l | I king.parrot red see-NONPAST'),
row(8, 'I see the clear water.', 'Ngayu bana dayirr nyajil.', 'ngayu bana dayirr nyaji-l | I water clear see-NONPAST'),
row(9, 'I see the clean ground.', 'Ngayu bubu dayirr nyajil.', 'ngayu bubu dayirr nyaji-l | I ground clean see-NONPAST'),
row(10, 'I see the clean house.', 'Ngayu bayan dayirr nyajil.', 'ngayu bayan dayirr nyaji-l | I house clean see-NONPAST'),
row(11, 'You see the black stone.', 'Yundu kulji ngumbu nyajil.', 'yundu kulji ngumbu nyaji-l | you stone black see-NONPAST'),
row(12, 'You see the black tree.', 'Yundu juku ngumbu nyajil.', 'yundu juku ngumbu nyaji-l | you tree black see-NONPAST'),
row(13, 'You see the dark cloud.', 'Yundu ngurrban ngumbu nyajil.', 'yundu ngurrban ngumbu nyaji-l | you cloud dark see-NONPAST'),
row(14, 'You see the light-coloured hair.', 'Yundu mungka bingaji nyajil.', 'yundu mungka bingaji nyaji-l | you hair light-coloured see-NONPAST'),
row(15, 'You see the light-coloured clothing.', 'Yundu kambi bingaji nyajil.', 'yundu kambi bingaji nyaji-l | you clothing light-coloured see-NONPAST'),
row(16, 'You see the red flower.', 'Yundu nganka ngala-ngala nyajil.', 'yundu nganka ngala-ngala nyaji-l | you flower red see-NONPAST'),
row(17, 'You see the red king parrot.', 'Yundu karrambil ngala-ngala nyajil.', 'yundu karrambil ngala-ngala nyaji-l | you king.parrot red see-NONPAST'),
row(18, 'You see the clear water.', 'Yundu bana dayirr nyajil.', 'yundu bana dayirr nyaji-l | you water clear see-NONPAST'),
row(19, 'You see the clean ground.', 'Yundu bubu dayirr nyajil.', 'yundu bubu dayirr nyaji-l | you ground clean see-NONPAST'),
row(20, 'You see the clean house.', 'Yundu bayan dayirr nyajil.', 'yundu bayan dayirr nyaji-l | you house clean see-NONPAST'),
row(21, 'They saw the black stone.', 'Jana kulji ngumbu nyajin.', 'jana kulji ngumbu nyaji-ny | they stone black see-PAST'),
row(22, 'They saw the black tree.', 'Jana juku ngumbu nyajin.', 'jana juku ngumbu nyaji-ny | they tree black see-PAST'),
row(23, 'They saw the dark cloud.', 'Jana ngurrban ngumbu nyajin.', 'jana ngurrban ngumbu nyaji-ny | they cloud dark see-PAST'),
row(24, 'They saw the light-coloured hair.', 'Jana mungka bingaji nyajin.', 'jana mungka bingaji nyaji-ny | they hair light-coloured see-PAST'),
row(25, 'They saw the light-coloured clothing.', 'Jana kambi bingaji nyajin.', 'jana kambi bingaji nyaji-ny | they clothing light-coloured see-PAST'),
row(26, 'They saw the red flower.', 'Jana nganka ngala-ngala nyajin.', 'jana nganka ngala-ngala nyaji-ny | they flower red see-PAST'),
row(27, 'They saw the red king parrot.', 'Jana karrambil ngala-ngala nyajin.', 'jana karrambil ngala-ngala nyaji-ny | they king.parrot red see-PAST'),
row(28, 'They saw the clear water.', 'Jana bana dayirr nyajin.', 'jana bana dayirr nyaji-ny | they water clear see-PAST'),
row(29, 'They saw the clean ground.', 'Jana bubu dayirr nyajin.', 'jana bubu dayirr nyaji-ny | they ground clean see-PAST'),
row(30, 'They saw the clean house.', 'Jana bayan dayirr nyajin.', 'jana bayan dayirr nyaji-ny | they house clean see-PAST'),
row(31, 'We see the black stone here.', 'Ngana kulji ngumbu yaluy nyajil.', 'ngana kulji ngumbu yaluy nyaji-l | we stone black here see-NONPAST'),
row(32, 'We see the black tree here.', 'Ngana juku ngumbu yaluy nyajil.', 'ngana juku ngumbu yaluy nyaji-l | we tree black here see-NONPAST'),
row(33, 'We see the dark cloud here.', 'Ngana ngurrban ngumbu yaluy nyajil.', 'ngana ngurrban ngumbu yaluy nyaji-l | we cloud dark here see-NONPAST'),
row(34, 'We see the light-coloured hair here.', 'Ngana mungka bingaji yaluy nyajil.', 'ngana mungka bingaji yaluy nyaji-l | we hair light-coloured here see-NONPAST'),
row(35, 'We see the light-coloured clothing here.', 'Ngana kambi bingaji yaluy nyajil.', 'ngana kambi bingaji yaluy nyaji-l | we clothing light-coloured here see-NONPAST'),
row(36, 'We see the red flower here.', 'Ngana nganka ngala-ngala yaluy nyajil.', 'ngana nganka ngala-ngala yaluy nyaji-l | we flower red here see-NONPAST'),
row(37, 'We see the red king parrot here.', 'Ngana karrambil ngala-ngala yaluy nyajil.', 'ngana karrambil ngala-ngala yaluy nyaji-l | we king.parrot red here see-NONPAST'),
row(38, 'We see the clear water here.', 'Ngana bana dayirr yaluy nyajil.', 'ngana bana dayirr yaluy nyaji-l | we water clear here see-NONPAST'),
row(39, 'We see the clean ground here.', 'Ngana bubu dayirr yaluy nyajil.', 'ngana bubu dayirr yaluy nyaji-l | we ground clean here see-NONPAST'),
row(40, 'We see the clean house here.', 'Ngana bayan dayirr yaluy nyajil.', 'ngana bayan dayirr yaluy nyaji-l | we house clean here see-NONPAST'),
row(41, 'You see the black stone there.', 'Yundu kulji ngumbu yinyay nyajil.', 'yundu kulji ngumbu yinyay nyaji-l | you stone black there see-NONPAST'),
row(42, 'You see the black tree there.', 'Yundu juku ngumbu yinyay nyajil.', 'yundu juku ngumbu yinyay nyaji-l | you tree black there see-NONPAST'),
row(43, 'You see the dark cloud there.', 'Yundu ngurrban ngumbu yinyay nyajil.', 'yundu ngurrban ngumbu yinyay nyaji-l | you cloud dark there see-NONPAST'),
row(44, 'You see the light-coloured hair there.', 'Yundu mungka bingaji yinyay nyajil.', 'yundu mungka bingaji yinyay nyaji-l | you hair light-coloured there see-NONPAST'),
row(45, 'You see the light-coloured clothing there.', 'Yundu kambi bingaji yinyay nyajil.', 'yundu kambi bingaji yinyay nyaji-l | you clothing light-coloured there see-NONPAST'),
row(46, 'You see the red flower there.', 'Yundu nganka ngala-ngala yinyay nyajil.', 'yundu nganka ngala-ngala yinyay nyaji-l | you flower red there see-NONPAST'),
row(47, 'You see the red king parrot there.', 'Yundu karrambil ngala-ngala yinyay nyajil.', 'yundu karrambil ngala-ngala yinyay nyaji-l | you king.parrot red there see-NONPAST'),
row(48, 'You see the clear water there.', 'Yundu bana dayirr yinyay nyajil.', 'yundu bana dayirr yinyay nyaji-l | you water clear there see-NONPAST'),
row(49, 'You see the clean ground there.', 'Yundu bubu dayirr yinyay nyajil.', 'yundu bubu dayirr yinyay nyaji-l | you ground clean there see-NONPAST'),
row(50, 'You see the clean house there.', 'Yundu bayan dayirr yinyay nyajil.', 'yundu bayan dayirr yinyay nyaji-l | you house clean there see-NONPAST'),
]

ROWS_241 = [
row(1, 'The jinjajalka tree is here.', 'Jinjajalka yaluy.', 'jinjajalka yaluy | jinjajalka.tree here'),
row(2, 'The kurunkal mangrove is here.', 'Kurunkal yaluy.', 'kurunkal yaluy | kurunkal.mangrove here'),
row(3, 'The madu sea pine is here.', 'Madu yaluy.', 'madu yaluy | madu.sea.pine here'),
row(4, 'The marngkil ti tree is here.', 'Marngkil yaluy.', 'marngkil yaluy | marngkil.ti.tree here'),
row(5, 'The marrabal ti tree is here.', 'Marrabal yaluy.', 'marrabal yaluy | marrabal.ti.tree here'),
row(6, 'The milbarakal tree is here.', 'Milbarakal yaluy.', 'milbarakal yaluy | milbarakal.tree here'),
row(7, 'The mungari kaori pine is here.', 'Mungari yaluy.', 'mungari yaluy | mungari.kaori.pine here'),
row(8, 'The wurmbariji scrub tree is here.', 'Wurmbariji yaluy.', 'wurmbariji yaluy | wurmbariji.scrub.tree here'),
row(9, 'The wuymbariji scrub tree is here.', 'Wuymbariji yaluy.', 'wuymbariji yaluy | wuymbariji.scrub.tree here'),
row(10, 'The kurrun tree is here.', 'Kurrun yaluy.', 'kurrun yaluy | kurrun.tree here'),
row(11, 'I see the jinjajalka tree.', 'Ngayu jinjajalka nyajil.', 'ngayu jinjajalka nyaji-l | I jinjajalka.tree see-NONPAST'),
row(12, 'I see the kurunkal mangrove.', 'Ngayu kurunkal nyajil.', 'ngayu kurunkal nyaji-l | I kurunkal.mangrove see-NONPAST'),
row(13, 'I see the madu sea pine.', 'Ngayu madu nyajil.', 'ngayu madu nyaji-l | I madu.sea.pine see-NONPAST'),
row(14, 'I see the marngkil ti tree.', 'Ngayu marngkil nyajil.', 'ngayu marngkil nyaji-l | I marngkil.ti.tree see-NONPAST'),
row(15, 'I see the marrabal ti tree.', 'Ngayu marrabal nyajil.', 'ngayu marrabal nyaji-l | I marrabal.ti.tree see-NONPAST'),
row(16, 'I see the milbarakal tree.', 'Ngayu milbarakal nyajil.', 'ngayu milbarakal nyaji-l | I milbarakal.tree see-NONPAST'),
row(17, 'I see the mungari kaori pine.', 'Ngayu mungari nyajil.', 'ngayu mungari nyaji-l | I mungari.kaori.pine see-NONPAST'),
row(18, 'I see the wurmbariji scrub tree.', 'Ngayu wurmbariji nyajil.', 'ngayu wurmbariji nyaji-l | I wurmbariji.scrub.tree see-NONPAST'),
row(19, 'I see the wuymbariji scrub tree.', 'Ngayu wuymbariji nyajil.', 'ngayu wuymbariji nyaji-l | I wuymbariji.scrub.tree see-NONPAST'),
row(20, 'I see the kurrun tree.', 'Ngayu kurrun nyajil.', 'ngayu kurrun nyaji-l | I kurrun.tree see-NONPAST'),
row(21, 'You see the jinjajalka tree there.', 'Yundu jinjajalka yinyay nyajil.', 'yundu jinjajalka yinyay nyaji-l | you jinjajalka.tree there see-NONPAST'),
row(22, 'You see the kurunkal mangrove there.', 'Yundu kurunkal yinyay nyajil.', 'yundu kurunkal yinyay nyaji-l | you kurunkal.mangrove there see-NONPAST'),
row(23, 'You see the madu sea pine there.', 'Yundu madu yinyay nyajil.', 'yundu madu yinyay nyaji-l | you madu.sea.pine there see-NONPAST'),
row(24, 'You see the marngkil ti tree there.', 'Yundu marngkil yinyay nyajil.', 'yundu marngkil yinyay nyaji-l | you marngkil.ti.tree there see-NONPAST'),
row(25, 'You see the marrabal ti tree there.', 'Yundu marrabal yinyay nyajil.', 'yundu marrabal yinyay nyaji-l | you marrabal.ti.tree there see-NONPAST'),
row(26, 'You see the milbarakal tree there.', 'Yundu milbarakal yinyay nyajil.', 'yundu milbarakal yinyay nyaji-l | you milbarakal.tree there see-NONPAST'),
row(27, 'You see the mungari kaori pine there.', 'Yundu mungari yinyay nyajil.', 'yundu mungari yinyay nyaji-l | you mungari.kaori.pine there see-NONPAST'),
row(28, 'You see the wurmbariji scrub tree there.', 'Yundu wurmbariji yinyay nyajil.', 'yundu wurmbariji yinyay nyaji-l | you wurmbariji.scrub.tree there see-NONPAST'),
row(29, 'You see the wuymbariji scrub tree there.', 'Yundu wuymbariji yinyay nyajil.', 'yundu wuymbariji yinyay nyaji-l | you wuymbariji.scrub.tree there see-NONPAST'),
row(30, 'You see the kurrun tree there.', 'Yundu kurrun yinyay nyajil.', 'yundu kurrun yinyay nyaji-l | you kurrun.tree there see-NONPAST'),
row(31, 'They saw the jinjajalka tree there.', 'Jana jinjajalka yinyay nyajin.', 'jana jinjajalka yinyay nyaji-ny | they jinjajalka.tree there see-PAST'),
row(32, 'They saw the kurunkal mangrove there.', 'Jana kurunkal yinyay nyajin.', 'jana kurunkal yinyay nyaji-ny | they kurunkal.mangrove there see-PAST'),
row(33, 'They saw the madu sea pine there.', 'Jana madu yinyay nyajin.', 'jana madu yinyay nyaji-ny | they madu.sea.pine there see-PAST'),
row(34, 'They saw the marngkil ti tree there.', 'Jana marngkil yinyay nyajin.', 'jana marngkil yinyay nyaji-ny | they marngkil.ti.tree there see-PAST'),
row(35, 'They saw the marrabal ti tree there.', 'Jana marrabal yinyay nyajin.', 'jana marrabal yinyay nyaji-ny | they marrabal.ti.tree there see-PAST'),
row(36, 'They saw the milbarakal tree there.', 'Jana milbarakal yinyay nyajin.', 'jana milbarakal yinyay nyaji-ny | they milbarakal.tree there see-PAST'),
row(37, 'They saw the mungari kaori pine there.', 'Jana mungari yinyay nyajin.', 'jana mungari yinyay nyaji-ny | they mungari.kaori.pine there see-PAST'),
row(38, 'They saw the wurmbariji scrub tree there.', 'Jana wurmbariji yinyay nyajin.', 'jana wurmbariji yinyay nyaji-ny | they wurmbariji.scrub.tree there see-PAST'),
row(39, 'They saw the wuymbariji scrub tree there.', 'Jana wuymbariji yinyay nyajin.', 'jana wuymbariji yinyay nyaji-ny | they wuymbariji.scrub.tree there see-PAST'),
row(40, 'They saw the kurrun tree there.', 'Jana kurrun yinyay nyajin.', 'jana kurrun yinyay nyaji-ny | they kurrun.tree there see-PAST'),
row(41, 'We see the jinjajalka tree here.', 'Ngana jinjajalka yaluy nyajil.', 'ngana jinjajalka yaluy nyaji-l | we jinjajalka.tree here see-NONPAST'),
row(42, 'We see the kurunkal mangrove here.', 'Ngana kurunkal yaluy nyajil.', 'ngana kurunkal yaluy nyaji-l | we kurunkal.mangrove here see-NONPAST'),
row(43, 'We see the madu sea pine here.', 'Ngana madu yaluy nyajil.', 'ngana madu yaluy nyaji-l | we madu.sea.pine here see-NONPAST'),
row(44, 'We see the marngkil ti tree here.', 'Ngana marngkil yaluy nyajil.', 'ngana marngkil yaluy nyaji-l | we marngkil.ti.tree here see-NONPAST'),
row(45, 'We see the marrabal ti tree here.', 'Ngana marrabal yaluy nyajil.', 'ngana marrabal yaluy nyaji-l | we marrabal.ti.tree here see-NONPAST'),
row(46, 'We see the milbarakal tree here.', 'Ngana milbarakal yaluy nyajil.', 'ngana milbarakal yaluy nyaji-l | we milbarakal.tree here see-NONPAST'),
row(47, 'We see the mungari kaori pine here.', 'Ngana mungari yaluy nyajil.', 'ngana mungari yaluy nyaji-l | we mungari.kaori.pine here see-NONPAST'),
row(48, 'We see the wurmbariji scrub tree here.', 'Ngana wurmbariji yaluy nyajil.', 'ngana wurmbariji yaluy nyaji-l | we wurmbariji.scrub.tree here see-NONPAST'),
row(49, 'We see the wuymbariji scrub tree here.', 'Ngana wuymbariji yaluy nyajil.', 'ngana wuymbariji yaluy nyaji-l | we wuymbariji.scrub.tree here see-NONPAST'),
row(50, 'We see the kurrun tree here.', 'Ngana kurrun yaluy nyajil.', 'ngana kurrun yaluy nyaji-l | we kurrun.tree here see-NONPAST'),
]

ROWS_329 = [
row(1, 'We two (not you) see the hook here.', 'Ngali badur yaluy nyajil.', 'ngali badur yaluy nyajil | 1du.EXCL.NOM hook here see.NPST'),
row(2, 'We two (not you) see the dilly bag here.', 'Ngali balji yaluy nyajil.', 'ngali balji yaluy nyajil | 1du.EXCL.NOM dilly.bag here see.NPST'),
row(3, 'We two (not you) see the spear thrower here.', 'Ngali balur yaluy nyajil.', 'ngali balur yaluy nyajil | 1du.EXCL.NOM spear.thrower here see.NPST'),
row(4, 'We two (not you) see the outrigger canoe here.', 'Ngali kaljan yaluy nyajil.', 'ngali kaljan yaluy nyajil | 1du.EXCL.NOM outrigger.canoe here see.NPST'),
row(5, 'We two (not you) see the fighting spear here.', 'Ngali dajal yaluy nyajil.', 'ngali dajal yaluy nyajil | 1du.EXCL.NOM fighting.spear here see.NPST'),
row(6, 'We two (not you) see the cup here.', 'Ngali dakilji yaluy nyajil.', 'ngali dakilji yaluy nyajil | 1du.EXCL.NOM cup here see.NPST'),
row(7, 'We two (not you) see the torch here.', 'Ngali jidi yaluy nyajil.', 'ngali jidi yaluy nyajil | 1du.EXCL.NOM torch here see.NPST'),
row(8, 'We two (not you) see the three-pronged spear here.', 'Ngali jindal yaluy nyajil.', 'ngali jindal yaluy nyajil | 1du.EXCL.NOM three.pronged.spear here see.NPST'),
row(9, 'We two (not you) see the pumice stone here.', 'Ngali jundi yaluy nyajil.', 'ngali jundi yaluy nyajil | 1du.EXCL.NOM pumice.stone here see.NPST'),
row(10, 'We two (not you) see the rope here.', 'Ngali karra yaluy nyajil.', 'ngali karra yaluy nyajil | 1du.EXCL.NOM rope here see.NPST'),
row(11, 'You and I see the hook there.', 'Ngaliny badur yinyay nyajil.', 'ngaliny badur yinyay nyajil | 1du.INCL.NOM hook there see.NPST'),
row(12, 'You and I see the dilly bag there.', 'Ngaliny balji yinyay nyajil.', 'ngaliny balji yinyay nyajil | 1du.INCL.NOM dilly.bag there see.NPST'),
row(13, 'You and I see the spear thrower there.', 'Ngaliny balur yinyay nyajil.', 'ngaliny balur yinyay nyajil | 1du.INCL.NOM spear.thrower there see.NPST'),
row(14, 'You and I see the outrigger canoe there.', 'Ngaliny kaljan yinyay nyajil.', 'ngaliny kaljan yinyay nyajil | 1du.INCL.NOM outrigger.canoe there see.NPST'),
row(15, 'You and I see the fighting spear there.', 'Ngaliny dajal yinyay nyajil.', 'ngaliny dajal yinyay nyajil | 1du.INCL.NOM fighting.spear there see.NPST'),
row(16, 'You and I see the cup there.', 'Ngaliny dakilji yinyay nyajil.', 'ngaliny dakilji yinyay nyajil | 1du.INCL.NOM cup there see.NPST'),
row(17, 'You and I see the torch there.', 'Ngaliny jidi yinyay nyajil.', 'ngaliny jidi yinyay nyajil | 1du.INCL.NOM torch there see.NPST'),
row(18, 'You and I see the three-pronged spear there.', 'Ngaliny jindal yinyay nyajil.', 'ngaliny jindal yinyay nyajil | 1du.INCL.NOM three.pronged.spear there see.NPST'),
row(19, 'You and I see the pumice stone there.', 'Ngaliny jundi yinyay nyajil.', 'ngaliny jundi yinyay nyajil | 1du.INCL.NOM pumice.stone there see.NPST'),
row(20, 'You and I see the rope there.', 'Ngaliny karra yinyay nyajil.', 'ngaliny karra yinyay nyajil | 1du.INCL.NOM rope there see.NPST'),
row(21, 'You two see the hook here.', 'Yubal badur yaluy nyajil.', 'yubal badur yaluy nyajil | 2du.NOM hook here see.NPST'),
row(22, 'You two see the dilly bag here.', 'Yubal balji yaluy nyajil.', 'yubal balji yaluy nyajil | 2du.NOM dilly.bag here see.NPST'),
row(23, 'You two see the spear thrower here.', 'Yubal balur yaluy nyajil.', 'yubal balur yaluy nyajil | 2du.NOM spear.thrower here see.NPST'),
row(24, 'You two see the outrigger canoe here.', 'Yubal kaljan yaluy nyajil.', 'yubal kaljan yaluy nyajil | 2du.NOM outrigger.canoe here see.NPST'),
row(25, 'You two see the fighting spear here.', 'Yubal dajal yaluy nyajil.', 'yubal dajal yaluy nyajil | 2du.NOM fighting.spear here see.NPST'),
row(26, 'You two see the cup here.', 'Yubal dakilji yaluy nyajil.', 'yubal dakilji yaluy nyajil | 2du.NOM cup here see.NPST'),
row(27, 'You two see the torch here.', 'Yubal jidi yaluy nyajil.', 'yubal jidi yaluy nyajil | 2du.NOM torch here see.NPST'),
row(28, 'You two see the three-pronged spear here.', 'Yubal jindal yaluy nyajil.', 'yubal jindal yaluy nyajil | 2du.NOM three.pronged.spear here see.NPST'),
row(29, 'You two see the pumice stone here.', 'Yubal jundi yaluy nyajil.', 'yubal jundi yaluy nyajil | 2du.NOM pumice.stone here see.NPST'),
row(30, 'You two see the rope here.', 'Yubal karra yaluy nyajil.', 'yubal karra yaluy nyajil | 2du.NOM rope here see.NPST'),
row(31, 'You all see the hook there.', 'Yurra badur yinyay nyajil.', 'yurra badur yinyay nyajil | 2pl.NOM hook there see.NPST'),
row(32, 'You all see the dilly bag there.', 'Yurra balji yinyay nyajil.', 'yurra balji yinyay nyajil | 2pl.NOM dilly.bag there see.NPST'),
row(33, 'You all see the spear thrower there.', 'Yurra balur yinyay nyajil.', 'yurra balur yinyay nyajil | 2pl.NOM spear.thrower there see.NPST'),
row(34, 'You all see the outrigger canoe there.', 'Yurra kaljan yinyay nyajil.', 'yurra kaljan yinyay nyajil | 2pl.NOM outrigger.canoe there see.NPST'),
row(35, 'You all see the fighting spear there.', 'Yurra dajal yinyay nyajil.', 'yurra dajal yinyay nyajil | 2pl.NOM fighting.spear there see.NPST'),
row(36, 'You all see the cup there.', 'Yurra dakilji yinyay nyajil.', 'yurra dakilji yinyay nyajil | 2pl.NOM cup there see.NPST'),
row(37, 'You all see the torch there.', 'Yurra jidi yinyay nyajil.', 'yurra jidi yinyay nyajil | 2pl.NOM torch there see.NPST'),
row(38, 'You all see the three-pronged spear there.', 'Yurra jindal yinyay nyajil.', 'yurra jindal yinyay nyajil | 2pl.NOM three.pronged.spear there see.NPST'),
row(39, 'You all see the pumice stone there.', 'Yurra jundi yinyay nyajil.', 'yurra jundi yinyay nyajil | 2pl.NOM pumice.stone there see.NPST'),
row(40, 'You all see the rope there.', 'Yurra karra yinyay nyajil.', 'yurra karra yinyay nyajil | 2pl.NOM rope there see.NPST'),
row(41, 'They see the hook here.', 'Jana badur yaluy nyajil.', 'jana badur yaluy nyajil | 3pl.NOM hook here see.NPST'),
row(42, 'They see the dilly bag here.', 'Jana balji yaluy nyajil.', 'jana balji yaluy nyajil | 3pl.NOM dilly.bag here see.NPST'),
row(43, 'They see the spear thrower here.', 'Jana balur yaluy nyajil.', 'jana balur yaluy nyajil | 3pl.NOM spear.thrower here see.NPST'),
row(44, 'They see the outrigger canoe here.', 'Jana kaljan yaluy nyajil.', 'jana kaljan yaluy nyajil | 3pl.NOM outrigger.canoe here see.NPST'),
row(45, 'They see the fighting spear here.', 'Jana dajal yaluy nyajil.', 'jana dajal yaluy nyajil | 3pl.NOM fighting.spear here see.NPST'),
row(46, 'They see the cup here.', 'Jana dakilji yaluy nyajil.', 'jana dakilji yaluy nyajil | 3pl.NOM cup here see.NPST'),
row(47, 'They see the torch here.', 'Jana jidi yaluy nyajil.', 'jana jidi yaluy nyajil | 3pl.NOM torch here see.NPST'),
row(48, 'They see the three-pronged spear here.', 'Jana jindal yaluy nyajil.', 'jana jindal yaluy nyajil | 3pl.NOM three.pronged.spear here see.NPST'),
row(49, 'They see the pumice stone here.', 'Jana jundi yaluy nyajil.', 'jana jundi yaluy nyajil | 3pl.NOM pumice.stone here see.NPST'),
row(50, 'They see the rope here.', 'Jana karra yaluy nyajil.', 'jana karra yaluy nyajil | 3pl.NOM rope here see.NPST'),
]

BATCH_ROWS = {239: ROWS_239, 240: ROWS_240, 241: ROWS_241, 329: ROWS_329}


def literal_guard() -> list[str]:
    tree = ast.parse(Path(__file__).read_text())
    wanted = {f"ROWS_{number}" for number in BATCH_ROWS}
    assignments: dict[str, ast.AST] = {}
    for node in tree.body:
        if isinstance(node, ast.Assign):
            for target in node.targets:
                if isinstance(target, ast.Name) and target.id in wanted:
                    assignments[target.id] = node.value
    issues: list[str] = []
    for name in sorted(wanted):
        value = assignments.get(name)
        if not isinstance(value, ast.List) or len(value.elts) != 50:
            issues.append(f"{name}: not a 50-item literal list")
            continue
        for seq, element in enumerate(value.elts, 1):
            if not isinstance(element, ast.Call) or not isinstance(element.func, ast.Name) or element.func.id != "row":
                issues.append(f"{name}:{seq}: not row(...)")
                continue
            try:
                args = [ast.literal_eval(arg) for arg in element.args]
            except Exception:
                issues.append(f"{name}:{seq}: nonliteral argument")
                continue
            if len(args) != 4 or args[0] != seq or not all(isinstance(x, str) for x in args[1:]):
                issues.append(f"{name}:{seq}: malformed literal record")
    return issues


def main() -> int:
    issues = literal_guard()
    if issues:
        raise SystemExit("\n".join(issues))
    script_hash = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()
    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys=ON")
    db.execute("BEGIN")
    ensure_schema(db)
    db.execute(
        """CREATE TABLE IF NOT EXISTS authorship_remediation (
               id INTEGER PRIMARY KEY,
               sentence_id INTEGER NOT NULL REFERENCES sentences(id),
               original_author TEXT NOT NULL,
               remediation_author TEXT NOT NULL,
               finding TEXT NOT NULL,
               resolution TEXT NOT NULL,
               artifact_path TEXT NOT NULL,
               artifact_sha256 TEXT NOT NULL,
               created_at TEXT NOT NULL,
               UNIQUE(sentence_id, artifact_path)
           )"""
    )
    for number, expected in BATCH_ROWS.items():
        label = f"batch-{number}"
        rows = db.execute(
            """SELECT s.id,s.seq,s.english,s.kuku,s.analysis,s.evidence,s.words_used,s.author
                 FROM sentences s JOIN batches b ON b.id=s.batch_id
                WHERE b.label=? AND s.status IN ('accepted','revised') ORDER BY s.seq""",
            (label,),
        ).fetchall()
        actual = [(seq, english, kuku, analysis) for _sid, seq, english, kuku, analysis, *_rest in rows]
        if actual != expected or len(rows) != 50:
            raise SystemExit(f"{label}: literal-to-database mismatch")
        if any(not evidence or not words_used for _sid, _seq, _en, _ku, _an, evidence, words_used, _author in rows):
            raise SystemExit(f"{label}: missing source evidence or words_used")
        lens_counts = dict(
            db.execute(
                """SELECT r.lens,COUNT(*) FROM reviews r JOIN sentences s ON s.id=r.sentence_id
                     JOIN batches b ON b.id=s.batch_id WHERE b.label=? AND r.round=1 AND r.verdict='pass'
                     GROUP BY r.lens""",
                (label,),
            )
        )
        if lens_counts != {lens: 50 for lens in LENSES}:
            raise SystemExit(f"{label}: original five-lens evidence {lens_counts}")
        if db.execute(
            """SELECT COUNT(*) FROM reviews r JOIN sentences s ON s.id=r.sentence_id
                 JOIN batches b ON b.id=s.batch_id
                WHERE b.label=? AND r.reviewer='codex-main-literal-remediation'""",
            (label,),
        ).fetchone()[0]:
            raise SystemExit(f"{label}: remediation already applied")
        db.executemany(
            """INSERT INTO reviews(sentence_id,round,lens,verdict,issues,suggested_fix,reviewer,created_at)
               VALUES(?,2,?,'pass',NULL,NULL,'codex-main-literal-remediation',?)""",
            [(sid, lens, NOW) for sid, *_rest in rows for lens in LENSES],
        )
        db.executemany(
            """INSERT INTO authorship_remediation(
                   sentence_id,original_author,remediation_author,finding,resolution,
                   artifact_path,artifact_sha256,created_at
               ) VALUES(?,?,?,?,?,?,?,?)""",
            [
                (
                    sid,
                    author,
                    "codex-main",
                    "Historical bank script assembled sentence fields at runtime from smaller literal inventories.",
                    "Sentence was individually read through all five lenses, manually ratified without field change, and materialized as an explicit literal row.",
                    ARTIFACT,
                    script_hash,
                    NOW,
                )
                for sid, _seq, _en, _ku, _an, _evidence, _words_used, author in rows
            ],
        )
        bid = db.execute("SELECT id FROM batches WHERE label=?", (label,)).fetchone()[0]
        db.execute(
            "INSERT INTO lessons(batch_id,category,lesson,guide_change,created_at) VALUES(?,?,?,?,?)",
            (
                bid,
                "process",
                "Completion audit found runtime sentence assembly; all 50 rows were manually ratified and materialized as literal row calls without changing linguistic fields.",
                f"Literal-authorship remediation artifact {ARTIFACT}; original bank script retained.",
                NOW,
            ),
        )
        record_event(
            db,
            batch_label=label,
            phase="completion-audit",
            event_type="literal-authorship-remediation",
            actor="codex",
            detail=f"50/50 English-Kuku-analysis records materialized as literal row calls; exact DB readback; original runtime-assembly script retained; artifact SHA {script_hash}.",
            artifact_path=ARTIFACT,
            created_at=NOW,
        )
        record_event(
            db,
            batch_label=label,
            phase="completion-audit",
            event_type="second-five-lens-remediation",
            actor="codex",
            detail="Manual second review stored: morphology/lexicon/syntax/fidelity/register 50/50 pass each; no field change required.",
            artifact_path=LOG,
            created_at=NOW,
        )
    if db.execute("SELECT COUNT(*) FROM authorship_remediation WHERE artifact_path=?", (ARTIFACT,)).fetchone()[0] != 200:
        raise SystemExit("remediation ledger count")
    db.commit()
    print(f"literal-authorship remediation complete: 200 rows; SHA {script_hash}")
    return 0


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