import type { Document, HistoryState, Id, OperationIntent, Registry } from '../canvas-core/types.ts'; import type { EngineSnapshot } from './contracts.ts'; import { invert } from '../canvas-core/document.ts'; import { planHistoryChanges } from '../canvas-core/history.ts'; import { fault, freeze, readMap } from '../canvas-core/values.ts'; /** Rewind only proved local stack moves, then replay the explicitly retained traversals. Never publish the temporary cursor. */ export function replanTraversals(snapshot:EngineSnapshot,pending:readonly OperationIntent[],replacements:ReadonlyMap,kept:ReadonlySet,boundaries:ReadonlyMap,registry:Registry):HistoryState { const traversals=pending.filter(intent=>intent.changes.length); if(traversals.some(intent=>!['undo','redo'].includes(intent.origin)))throw fault('recovery_history_mixed_traversal_not_supported'); const owner=new Map();for(const entry of snapshot.history.entries.values())for(const id of entry.operationIds)owner.set(id,entry.id); const seen=new Set(),undo=[...snapshot.history.undo],redo=[...snapshot.history.redo],entries=new Map(snapshot.history.entries); function entryId(intent:OperationIntent){const id=owner.get(intent.id);if(!id)throw fault('recovery_history_traversal_evidence_missing',[intent.id]);return id;} function move(source:Id[],destination:Id[],id:Id){if(source.at(-1)!==id)throw fault('recovery_history_traversal_order',[id]);source.pop();destination.push(id);} for(const intent of [...traversals].reverse()){ const id=entryId(intent);if(seen.has(id))throw fault('recovery_history_repeated_traversal_not_supported',[id]);seen.add(id); if(intent.origin==='undo')move(redo,undo,id);else move(undo,redo,id); } for(const intent of traversals){ const id=entryId(intent),entry=entries.get(id)!,source=intent.origin==='undo'?undo:redo,destination=intent.origin==='undo'?redo:undo; if(source.at(-1)!==id)throw fault('recovery_history_traversal_order',[id]); const replacement=replacements.get(intent.id),boundary=boundaries.get(id)!; if(kept.has(intent.id)){ if(!replacement){source.pop();entries.delete(id);continue;} // No remaining local effect, retired with an audit rather than a fabricated redo. const changes=intent.origin==='undo'?invert(replacement.changes):replacement.changes; try{planHistoryChanges(boundary.after,intent.origin==='undo'?changes:invert(changes),registry);} catch{throw fault('recovery_history_traversal_conflict',[id]);} move(source,destination,id); entries.set(id,freeze({...entry,changes,operationIds:entry.operationIds.map(operationId=>operationId===intent.id?replacement.id:operationId)})); }else{ const changes=intent.origin==='undo'?invert(intent.changes):intent.changes; try{planHistoryChanges(boundary.before,intent.origin==='undo'?invert(changes):changes,registry);} catch{throw fault('recovery_history_traversal_conflict',[id]);} entries.set(id,freeze({...entry,changes,operationIds:entry.operationIds.filter(operationId=>operationId!==intent.id)})); } } return Object.freeze({undo:freeze(undo),redo:freeze(redo),entries:readMap(entries)}); }