47 lines
3.4 KiB
Python
Executable File
47 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Repeatable, prefix-isolated orphan/missing reconciler experiment (lab use only)."""
|
|
import hashlib, json, subprocess, sys, time, uuid
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
|
from app.main import bucket, pool, s3
|
|
|
|
prefix='reconcile-experiment-'+uuid.uuid4().hex+'/'
|
|
missing_key=prefix+'missing.png'; orphan_key=prefix+'orphan.bin'
|
|
with pool.connection() as c:
|
|
chat=c.execute('SELECT c.id,m.user_id FROM chats c JOIN members m ON m.chat_id=c.id LIMIT 1').fetchone()
|
|
if not chat: raise SystemExit('Create one chat before running this experiment')
|
|
sentinel=c.execute("SELECT id,object_key,sha256 FROM attachments WHERE status='ready' ORDER BY created_at DESC LIMIT 1").fetchone()
|
|
if not sentinel: raise SystemExit('Upload one valid attachment before running this experiment')
|
|
sid=str(sentinel['id']); skey=sentinel['object_key']; expected=sentinel['sha256'].strip()
|
|
sentinel_before=s3.get_object(Bucket=bucket,Key=skey)['Body'].read()
|
|
if hashlib.sha256(sentinel_before).hexdigest()!=expected: raise RuntimeError('sentinel attachment checksum mismatch before experiment')
|
|
script=Path(__file__).with_name('reconcile.py')
|
|
def reconcile(*args):
|
|
r=subprocess.run([sys.executable,str(script),'--prefix',prefix,*args],capture_output=True,text=True,check=True)
|
|
return json.loads(r.stdout)
|
|
try:
|
|
with pool.connection() as c:
|
|
c.execute("INSERT INTO attachments(id,chat_id,owner_id,object_key,filename,content_type,size_bytes,sha256,status) VALUES (%s,%s,%s,%s,%s,'image/png',1,%s,'ready')",(uuid.uuid4(),chat['id'],chat['user_id'],missing_key,'missing.png','0'*64))
|
|
s3.put_object(Bucket=bucket,Key=orphan_key,Body=b'reconciler experiment orphan')
|
|
dry=reconcile()
|
|
if dry['missing_objects']!=[missing_key] or dry['orphan_objects']!=[orphan_key] or dry['deleted_objects']:
|
|
raise RuntimeError('dry-run did not report both injected faults without mutation')
|
|
fresh_apply=reconcile('--grace-seconds','3600','--apply')
|
|
if fresh_apply['eligible_orphans'] or fresh_apply['deleted_objects'] or fresh_apply['s3_objects']!=1:
|
|
raise RuntimeError('apply deleted an orphan before the grace period')
|
|
time.sleep(2.1)
|
|
applied=reconcile('--grace-seconds','1','--allow-short-grace','--apply')
|
|
if applied['deleted_objects']!=[orphan_key] or applied['missing_objects']!=[missing_key]:
|
|
raise RuntimeError('apply result did not match scoped safety policy')
|
|
repeated=reconcile('--grace-seconds','1','--allow-short-grace')
|
|
if repeated['orphan_objects'] or repeated['missing_objects']!=[missing_key]:
|
|
raise RuntimeError('repeat dry-run is not stable')
|
|
sentinel_after=s3.get_object(Bucket=bucket,Key=skey)['Body'].read()
|
|
if hashlib.sha256(sentinel_after).hexdigest()!=expected or sentinel_after!=sentinel_before:
|
|
raise RuntimeError('pre-existing attachment changed during reconciliation')
|
|
print(json.dumps({'prefix':prefix,'faults_injected':{'missing_metadata_object':missing_key,'unreferenced_s3_object':orphan_key},'dry_run':dry,'fresh_orphan_apply':fresh_apply,'aged_orphan_apply':applied,'repeat_dry_run':repeated,'sentinel_attachment_preserved':True,'sentinel_sha256':expected},indent=2))
|
|
finally:
|
|
with pool.connection() as c: c.execute('DELETE FROM attachments WHERE object_key=%s',(missing_key,))
|
|
try: s3.delete_object(Bucket=bucket,Key=orphan_key)
|
|
except Exception: pass
|