Reuse validated immutable history ranges during resource retention

This commit is contained in:
2026-11-11 01:56:43 +08:00
parent 26d1688f2f
commit 72a8e01114
5 changed files with 90 additions and 4 deletions
@@ -97,3 +97,39 @@ test('history-disabled replacement evidence cannot be omitted merely because the
test('adoption evidence preserves replacement sequence even when all history transitions are no-ops',async()=>{
const f=await fixture(),engine=f.engine;try{edit(engine.editor,'first',{history:false});await engine.settle();engine.editor.dispatch({type:'element.data',target:{id:'a',incarnation:1},patch:{title:'second'}},{history:false});await engine.settle();const session=await engine.prepareRecovery(),plan=session.plan(new Map([...engine.getSnapshot().runtime.ledger.operations].map(([id])=>[id,'keep-local'])));await session.resolve(plan);await engine.settle();const image=f.image();assert.equal(image.payload.runtime.recoveries[0][1].historyReplay.events.length,2);image.payload.runtime.recoveries[0][1].historyReplay.events.reverse();assert.throws(()=>decodeRecovery(image,registry),/corrupt_recovery_image/);}finally{await engine.close();}
});
test('resource scans reuse validated immutable transition ranges without skipping caller validation',()=>{
const editor=createEditor({registry,document});edit(editor,'cached');
const transition=[...editor.getSnapshot().operations.values()].at(-1).historyTransition;
const first=historyTransitionChanges(transition),second=historyTransitionChanges(transition);
assert.equal(first,second);assert.ok(Object.isFrozen(first));assert.throws(()=>first.push(first[0]),TypeError);
let checked=0;validateHistoryTransition(transition,()=>{checked++;});assert.equal(checked,2*transition.entries.length);
assert.throws(()=>validateHistoryTransition(transition,()=>{throw Error('current schema rejected');}),/current schema rejected/);
});
test('mutable, shallow frozen and accessor transition evidence remains live across resource scans',()=>{
const editor=createEditor({registry,document});edit(editor,'source');
const original=[...editor.getSnapshot().operations.values()].at(-1).historyTransition;
const mutable=structuredClone(original),shallow=Object.freeze(structuredClone(original));
for(const transition of [mutable,shallow]){
const first=historyTransitionChanges(transition);transition.entries[0].changes.after[0]={...first[0],after:'changed'};
const second=historyTransitionChanges(transition);assert.notEqual(first,second);assert.equal(second[0].after,'changed');
transition.entries[0].changes.offset=999;assert.throws(()=>historyTransitionChanges(transition),/invalid_history_transition/);
}
let current=structuredClone(original).entries;
const accessor=Object.freeze({...original,get entries(){return current;}});
const first=historyTransitionChanges(accessor);current=structuredClone(original).entries;current[0].changes.after[0]={...first[0],after:'getter changed'};
assert.equal(historyTransitionChanges(accessor)[0].after,'getter changed');
});
test('hidden frozen cycles and sparse transition data do not qualify for cached ranges',()=>{
const editor=createEditor({registry,document});edit(editor,'source');
const original=[...editor.getSnapshot().operations.values()].at(-1).historyTransition;
const cycle={};Object.defineProperty(cycle,'hidden',{value:cycle});Object.freeze(cycle);
// Non-enumerable metadata is outside JSON validation but must not recurse forever.
const hidden={...original};Object.defineProperty(hidden,'hidden',{value:cycle});Object.freeze(hidden);
assert.notEqual(historyTransitionChanges(hidden),historyTransitionChanges(hidden));
const sparse=Object.freeze([,]);const withSparse={...original};Object.defineProperty(withSparse,'hidden',{value:sparse});Object.freeze(withSparse);
assert.notEqual(historyTransitionChanges(withSparse),historyTransitionChanges(withSparse));
});