"""Private synthetic control: deletion in a sidecar table is isolated from a search-index copy."""
from pathlib import Path
import sqlite3,json,hashlib
P=Path(__file__).resolve().parent
mark='SYNTHETIC-REFERENCE-529'
con=sqlite3.connect(':memory:')
con.executescript('CREATE TABLE source(doc TEXT PRIMARY KEY,bytes BLOB); CREATE TABLE sidecar(doc TEXT PRIMARY KEY,derived_text TEXT); CREATE TABLE search_copy(doc TEXT PRIMARY KEY,indexed_text TEXT);')
con.execute('INSERT INTO source VALUES (?,?)',('fixture',b'placeholder for an original scan; no OCR or PDF reader is run'))
con.execute('INSERT INTO sidecar VALUES (?,?)',('fixture',mark))
con.execute('INSERT INTO search_copy VALUES (?,?)',('fixture',mark))
def state():
 return {'original_rows':con.execute('SELECT count(*) FROM source').fetchone()[0],'marker_in_sidecar':con.execute('SELECT count(*) FROM sidecar WHERE derived_text=?',(mark,)).fetchone()[0],'marker_in_index_copy':con.execute('SELECT count(*) FROM search_copy WHERE indexed_text=?',(mark,)).fetchone()[0]}
obs=[{'stage':'before_disposal',**state()}]
con.execute('DELETE FROM sidecar WHERE doc=?',('fixture',));obs.append({'stage':'after_sidecar_disposal_only',**state()})
con.execute('DELETE FROM search_copy WHERE doc=?',('fixture',));obs.append({'stage':'after_index_entry_removed',**state()})
assert [tuple(r[k] for k in ('original_rows','marker_in_sidecar','marker_in_index_copy')) for r in obs]==[(1,1,1),(1,0,1),(1,0,0)]
result={'scope':'Isolated in-memory SQLite control with one placeholder original and two deliberately duplicated synthetic strings. SQL rows only. No physical erasure, backup expiry, actual OCR, product behaviour or customer retention rule measured.','sqlite':sqlite3.sqlite_version,'marker':mark,'observations':obs,'script_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest()}
(P/'observations.json').write_text(json.dumps(result,indent=2)+'\n')
print(obs)
