@@ -73,8 +73,9 @@ export async function runIds(args: Record<string, unknown>): Promise<void> {
7373 if ( json ) console . log ( JSON . stringify ( withMeta ( result ) , null , 2 ) ) ;
7474 else console . log ( `ids: stamped ${ receipts . length } file${ receipts . length === 1 ? "" : "s" } ` ) ;
7575}
76+ type SettledClip = { verb : MutationVerb ; ref : string } ;
7677type ApplyEditResult =
77- | { ok : true ; file : string ; after : string }
78+ | { ok : true ; file : string ; after : string ; settled ?: SettledClip }
7879 | { ok : false ; reason : string ; fix : string } ;
7980
8081const PLAN_VERBS = new Set < MutationVerb > ( [ "move" , "trim" , "split" , "delete" , "set" , "duplicate" ] ) ;
@@ -154,16 +155,32 @@ function applyPlanEdit(
154155 } ;
155156 const decision = decideMutation ( verb , context , { ...planEdit , _ : [ planEdit . ref ] } ) ;
156157 if ( ! decision . ok ) return { ok : false , reason : decision . reason , fix : decision . fix } ;
158+ const movesClip = verb === "move" || verb === "trim" ;
157159 const conflict = mutationConflict (
158160 verb as MutationVerb ,
159- planEdit . overwrite === true ,
161+ planEdit . overwrite === true || movesClip ,
160162 row ,
161163 timeline ,
162164 decision . nextStart ,
163165 decision . nextDuration ,
164166 ) ;
165167 if ( conflict ) return { ok : false , reason : conflict . reason , fix : conflict . fix } ;
166- return { ok : true , file : row . file , after : decision . after } ;
168+ const settled = movesClip && planEdit . overwrite !== true ? { verb, ref : row . ref } : undefined ;
169+ return { ok : true , file : row . file , after : decision . after , ...( settled && { settled } ) } ;
170+ }
171+
172+ function overlapAtEnd (
173+ settled : SettledClip [ ] ,
174+ timeline : ProjectTimeline ,
175+ ) : { reason : string ; fix : string } | null {
176+ for ( const { verb, ref } of settled ) {
177+ const found = resolveRef ( timeline , ref ) ;
178+ if ( ! found . ok ) continue ;
179+ const { row } = found ;
180+ const conflict = mutationConflict ( verb , false , row , timeline , row . start , row . duration ) ;
181+ if ( conflict ) return conflict ;
182+ }
183+ return null ;
167184}
168185
169186type EditPlan = { ok : true ; edits : unknown [ ] } | { ok : false ; reason : string ; fix : string } ;
@@ -200,13 +217,16 @@ async function applyPlanEdits(
200217 project : ReturnType < typeof resolveProject > ,
201218 sourceByFile : Map < string , string > ,
202219) : Promise < { ok : true } | { ok : false ; reason : string ; fix : string } > {
220+ const settled : SettledClip [ ] = [ ] ;
203221 for ( const edit of edits ) {
204222 const result = applyPlanEdit ( edit , timeline , project , sourceByFile ) ;
205223 if ( ! result . ok ) return result ;
224+ if ( result . settled ) settled . push ( result . settled ) ;
206225 sourceByFile . set ( result . file , result . after ) ;
207226 timeline = await describeProject ( project . indexPath , undefined , sourceByFile ) ;
208227 }
209- return { ok : true } ;
228+ const conflict = overlapAtEnd ( settled , timeline ) ;
229+ return conflict ? { ok : false , ...conflict } : { ok : true } ;
210230}
211231
212232export async function runApply ( args : Record < string , unknown > ) : Promise < void > {
0 commit comments