Repair independent idle state clocks and validate full avatar index

This commit is contained in:
zenord
2026-10-13 06:58:39 +08:00
parent c4ddf8964f
commit 97f87e2409
5 changed files with 27226 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
"""Recovery diff, immutable source and independent mouth/body clocks; no paid/media generation."""
import copy
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
ROOT=Path(__file__).resolve().parents[2]
spec=importlib.util.spec_from_file_location('repair_clock',ROOT/'scripts/repair-idle-state-clock.py')
repair=importlib.util.module_from_spec(spec);spec.loader.exec_module(repair)
def broken_runtime(body_count=97):
ids=['idle','opening','talk1','talk2','end-talk']
states=[]
for name in ids:
n=6 if name in ('opening','end-talk') else 12
states.append({'id':name,'frameCount':body_count,'fpsNumerator':24,'fpsDenominator':1,
'durationMs':n*1000/24,'ptsUs':[(i*1_000_000+12)//24 for i in range(body_count)],
'globalAlphaByFrame':[0]*n,'entryMouthAnchor':'fixture_A','exitMouthAnchor':'fixture_B'})
return {'schemaVersion':2,'mouthStates':states,'body':{'frameCount':body_count,'sha256':'a'*64},
'patches':[{'frameIndex':i,'sentinel':'own-fixture'} for i in range(body_count)],'publishApproval':False}
class IdleRepairTests(unittest.TestCase):
def test_independent_state_clock_preserves_every_other_field(self):
for body_count in (25,49,97,121):
original=broken_runtime(body_count);snapshot=copy.deepcopy(original)
fixed,changes=repair.repair_states(original)
self.assertEqual(original,snapshot);self.assertEqual(fixed['body'],original['body'])
self.assertEqual(fixed['patches'],original['patches']);self.assertFalse(fixed['publishApproval'])
self.assertEqual(len(changes),10)
for before,after in zip(original['mouthStates'],fixed['mouthStates']):
n=len(before['globalAlphaByFrame'])
self.assertEqual(after['frameCount'],n);self.assertEqual(len(after['ptsUs']),n)
self.assertEqual(after['ptsUs'][0],0);self.assertLess(after['ptsUs'][-1],after['durationMs']*1000)
self.assertEqual(after['globalAlphaByFrame'],before['globalAlphaByFrame'])
self.assertEqual(after['entryMouthAnchor'],before['entryMouthAnchor'])
self.assertEqual(after['exitMouthAnchor'],before['exitMouthAnchor'])
self.assertEqual(repair.repair_states(fixed)[1],[])
def test_unknown_timing_rejected_without_guessing(self):
for key,value in (('fpsNumerator',30),('durationMs',501),('globalAlphaByFrame',[0]*13),('durationUs',123)):
data=broken_runtime();data['mouthStates'][0][key]=value
with self.subTest(key=key),self.assertRaises(ValueError):repair.repair_states(data)
def test_exact_copy_diff_backups_and_existing_destination_guard(self):
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);source=root/'source';source.mkdir();files=[];packages=[]
for package in sorted(repair.IDS):
runtime_path=source/package/'manifest.json';runtime_path.parent.mkdir()
runtime_path.write_text(json.dumps(broken_runtime()))
packages.append({'id':package,'runtimeManifest':{'path':str(runtime_path.relative_to(source)),
'sha256':repair.pack.file_sha(runtime_path)}})
(source/'immutable-fixture.bin').write_bytes(b'exact nonmedia fixture bytes')
for path in sorted(source.rglob('*')):
if path.is_file():files.append({'path':str(path.relative_to(source)),'sha256':repair.pack.file_sha(path),'bytes':path.stat().st_size})
index={'schemaVersion':2,'packId':'a01-expression-action-v1','conversationPackages':packages,'files':files,'reviewStatus':'pending'}
manifest=source/'manifest.json';manifest.write_text(json.dumps(index));pin=repair.pack.file_sha(manifest)
output=root/'recovery';result=repair.repair(manifest,pin,output)
self.assertEqual(result['original_files_changed'],[str(r['runtimeManifest']['path']) for r in packages])
self.assertEqual((output/'immutable-fixture.bin').read_bytes(),(source/'immutable-fixture.bin').read_bytes())
self.assertEqual((output/'docs/code-recovery-original-metadata/candidate03-index.json').read_bytes(),manifest.read_bytes())
self.assertEqual(repair.pack.file_sha(manifest),pin)
for f in files:self.assertEqual(repair.pack.file_sha(source/f['path']),f['sha256'])
updated=json.loads((output/'manifest.json').read_bytes())
for row in updated['files']:
self.assertEqual(repair.pack.file_sha(output/row['path']),row['sha256'])
self.assertEqual((output/row['path']).stat().st_size,row['bytes'])
self.assertEqual(updated['reviewStatus'],'pending')
repeated=repair.repair(manifest,pin,root/'independent-repeat')
self.assertEqual(repeated['manifest_sha256'],result['manifest_sha256'])
with self.assertRaises(ValueError):repair.repair(manifest,pin,output)
with self.assertRaises(ValueError):repair.repair(manifest,'0'*64,root/'wrong-pin')
self.assertFalse((root/'wrong-pin').exists())
if __name__=='__main__':unittest.main(verbosity=2)