From fa1942d5ac0f0894f2b374c2d0d3a026071e221e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 5 Nov 2026 05:16:08 +0000 Subject: [PATCH] fix(cli): timeline apply checks the overlap of a plan's end state, not each step (#7214) * fix(cli): timeline apply checks the overlap of a plan's end state, not each step * fix(cli): drop two doc comments from the plan end-state check --- packages/cli/src/timeline/a2Commands.ts | 28 ++++++++-- .../cli/src/timeline/timeline.e2e.test.ts | 55 +++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/timeline/a2Commands.ts b/packages/cli/src/timeline/a2Commands.ts index 74e6c39a68..1a6884e8e0 100644 --- a/packages/cli/src/timeline/a2Commands.ts +++ b/packages/cli/src/timeline/a2Commands.ts @@ -73,8 +73,9 @@ export async function runIds(args: Record): Promise { if (json) console.log(JSON.stringify(withMeta(result), null, 2)); else console.log(`ids: stamped ${receipts.length} file${receipts.length === 1 ? "" : "s"}`); } +type SettledClip = { verb: MutationVerb; ref: string }; type ApplyEditResult = - | { ok: true; file: string; after: string } + | { ok: true; file: string; after: string; settled?: SettledClip } | { ok: false; reason: string; fix: string }; const PLAN_VERBS = new Set(["move", "trim", "split", "delete", "set", "duplicate"]); @@ -154,16 +155,32 @@ function applyPlanEdit( }; const decision = decideMutation(verb, context, { ...planEdit, _: [planEdit.ref] }); if (!decision.ok) return { ok: false, reason: decision.reason, fix: decision.fix }; + const movesClip = verb === "move" || verb === "trim"; const conflict = mutationConflict( verb as MutationVerb, - planEdit.overwrite === true, + planEdit.overwrite === true || movesClip, row, timeline, decision.nextStart, decision.nextDuration, ); if (conflict) return { ok: false, reason: conflict.reason, fix: conflict.fix }; - return { ok: true, file: row.file, after: decision.after }; + const settled = movesClip && planEdit.overwrite !== true ? { verb, ref: row.ref } : undefined; + return { ok: true, file: row.file, after: decision.after, ...(settled && { settled }) }; +} + +function overlapAtEnd( + settled: SettledClip[], + timeline: ProjectTimeline, +): { reason: string; fix: string } | null { + for (const { verb, ref } of settled) { + const found = resolveRef(timeline, ref); + if (!found.ok) continue; + const { row } = found; + const conflict = mutationConflict(verb, false, row, timeline, row.start, row.duration); + if (conflict) return conflict; + } + return null; } type EditPlan = { ok: true; edits: unknown[] } | { ok: false; reason: string; fix: string }; @@ -200,13 +217,16 @@ async function applyPlanEdits( project: ReturnType, sourceByFile: Map, ): Promise<{ ok: true } | { ok: false; reason: string; fix: string }> { + const settled: SettledClip[] = []; for (const edit of edits) { const result = applyPlanEdit(edit, timeline, project, sourceByFile); if (!result.ok) return result; + if (result.settled) settled.push(result.settled); sourceByFile.set(result.file, result.after); timeline = await describeProject(project.indexPath, undefined, sourceByFile); } - return { ok: true }; + const conflict = overlapAtEnd(settled, timeline); + return conflict ? { ok: false, ...conflict } : { ok: true }; } export async function runApply(args: Record): Promise { diff --git a/packages/cli/src/timeline/timeline.e2e.test.ts b/packages/cli/src/timeline/timeline.e2e.test.ts index 815e2837f3..1dcad2e757 100644 --- a/packages/cli/src/timeline/timeline.e2e.test.ts +++ b/packages/cli/src/timeline/timeline.e2e.test.ts @@ -822,6 +822,61 @@ describe("timeline edit command", () => { } }); + describe("a plan that shifts adjacent clips", () => { + const row = (id: string, start: number) => + `
`; + const track = () => { + const dir = mkdtempSync(join(tmpdir(), "hf-timeline-shift-")); + writeFileSync( + join(dir, "index.html"), + `
${row("a", 0)}${row("b", 2)}${row("c", 4)}
`, + ); + return dir; + }; + const apply = (dir: string, edits: object[]) => { + const planPath = join(dir, "edits.json"); + writeFileSync(planPath, JSON.stringify(edits)); + return run(dir, "apply", planPath); + }; + + it("applies when each clip moves forward ahead of the clip it passes", () => { + const dir = track(); + try { + const result = apply(dir, [ + { verb: "move", ref: "#a", time: "1" }, + { verb: "move", ref: "#b", time: "3" }, + { verb: "move", ref: "#c", time: "5" }, + ]); + expect(result.status, result.stdout).toBe(0); + const source = readFileSync(join(dir, "index.html"), "utf8"); + for (const [id, start] of [ + ["a", 1], + ["b", 3], + ["c", 5], + ] as const) + expect(source).toMatch(new RegExp(`id="${id}"[^>]*data-start="${start}"`)); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + it("refuses a plan whose end state overlaps, and writes nothing", () => { + const dir = track(); + try { + const before = readFileSync(join(dir, "index.html"), "utf8"); + const result = apply(dir, [ + { verb: "move", ref: "#a", time: "1" }, + { verb: "move", ref: "#b", time: "3" }, + ]); + expect(result.status).toBe(2); + expect(result.stdout).toContain("would overlap"); + expect(readFileSync(join(dir, "index.html"), "utf8")).toBe(before); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + }); + it("applies a JSON plan atomically and undoes its receipt", () => { const dir = project(); try {