Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): keep a boundary piece that rounds to a whole millisecond

EtienneLescot25532ef67db3
diff --git a/src/lib/ai-edition/timeline/timelineMap.test.ts b/src/lib/ai-edition/timeline/timelineMap.test.ts
index 6e10bd488..2e4bdb014 100644
--- a/src/lib/ai-edition/timeline/timelineMap.test.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.test.ts
@@ -933,6 +933,37 @@ describe("a dragged pill stays whole against its neighbour (#1008)", () => {
expect(out.map((r) => r.id)).toEqual(["z"]);
expect(spans(out)).toEqual([[105_900, 114_130, "clip_2"]]);
});
+
+ it("keeps a half-millisecond piece that rounds to a whole millisecond", () => {
+ // A boundary at 1.0005 s leaves 0.5 ms of a 1000–1002 ms region on the first clip. Stored,
+ // that piece is 1000–1001 ms: dropping it would bring the region back a millisecond short.
+ const halfMsClips = [
+ clip({
+ id: "clip_1",
+ assetId: "rec",
+ sourceStartSec: 0,
+ sourceEndSec: 1.0005,
+ timelineStartSec: 0,
+ timelineEndSec: 1.0005,
+ }),
+ clip({
+ id: "clip_2",
+ assetId: "rec_2",
+ sourceStartSec: 0,
+ sourceEndSec: 30,
+ timelineStartSec: 1.0005,
+ timelineEndSec: 31.0005,
+ }),
+ ];
+ const out = anchorRegionsWithDerivedMs(
+ [{ id: "z", startMs: 1_000, endMs: 1_002, depth: 3 }],
+ halfMsClips,
+ ids(),
+ );
+ expect(out[0].id).toBe("z");
+ expect(Math.min(...out.map((r) => r.startMs))).toBe(1_000);
+ expect(Math.max(...out.map((r) => r.endMs))).toBe(1_002);
+ });
});
describe("legacy groupId must never affect identity (regression: test 1)", () => {
diff --git a/src/lib/ai-edition/timeline/timelineMap.ts b/src/lib/ai-edition/timeline/timelineMap.ts
index b22703e6e..8e3dcbcf7 100644
--- a/src/lib/ai-edition/timeline/timelineMap.ts
+++ b/src/lib/ai-edition/timeline/timelineMap.ts
@@ -38,9 +38,18 @@ export type ClipAnchored<T> = Omit<T, "startMs" | "endMs"> & {
};
/** Region edges are stored in whole ms and clip boundaries are not, so an edge put on a
- * boundary misses it by up to half a ms. A piece that short past a boundary is that miss,
- * not content: stored, it was a zero-length fragment holding the region's id (#1008). */
-const MS_ROUNDING_SEC = 0.0005;
+ * boundary misses it by up to half a ms. A piece past a boundary whose whole-ms span, as
+ * `anchorRegionsWithDerivedMs` stores it, is empty is that miss, not content: stored, it was
+ * a zero-length fragment holding the region's id (#1008). One that rounds to a millisecond
+ * stays, or the region would come back a millisecond short. */
+function roundsToNothing(
+ clip: AxcutClip | undefined,
+ fragment: { localStartSec: number; localEndSec: number },
+): boolean {
+ if (!clip) return true;
+ const ms = (localSec: number) => Math.round((clip.timelineStartSec + localSec) * 1000);
+ return ms(fragment.localEndSec) <= ms(fragment.localStartSec);
+}
/**
* Migrate RAW-virtual-ms regions (the v4 document-level storage) to clip-anchored
@@ -50,7 +59,7 @@ const MS_ROUNDING_SEC = 0.0005;
* merge rule, since they share properties — no bookkeeping). Each fragment
* gets its own unique `id` (first keeps the original region id; extras from
* `makeId`). A zero-length / off-timeline region covers no clip and is dropped (it
- * could never play), and so is a piece no longer than {@link MS_ROUNDING_SEC}. Pure;
+ * could never play), and so is a piece that rounds to no whole ms ({@link roundsToNothing}). Pure;
* reused by the v4→v5 schema migration and by re-anchoring after a raw edit.
*/
export function anchorRawRegionsToClips<T extends { id: string; startMs: number; endMs: number }>(
@@ -65,7 +74,7 @@ export function anchorRawRegionsToClips<T extends { id: string; startMs: number;
region.startMs / 1000,
region.endMs / 1000,
rawClips,
- ).filter((f) => f.localEndSec - f.localStartSec > MS_ROUNDING_SEC);
+ ).filter((f) => !roundsToNothing(byId.get(f.clipId), f));
frags.forEach((f, i) => {
const clip = byId.get(f.clipId);
if (!clip) return;
diff --git a/technical-documentation/architecture/document-model.md b/technical-documentation/architecture/document-model.md
index ccae26bb7..31bdff450 100644
--- a/technical-documentation/architecture/document-model.md
+++ b/technical-documentation/architecture/document-model.md
@@ -67,7 +67,7 @@ each region is split into one fragment per covered clip, with the source-time
window (`clipId`, `sourceStartSec`, `sourceEndSec`) as the source of truth and
`startMs`/`endMs` re-derived as a transition cache. The upgrader reads the RAW
clip layout out of `timeline.clips` and runs every region array through
-`anchorRegionsWithDerivedMs` (`src/lib/ai-edition/timeline/timelineMap.ts:408`):
+`anchorRegionsWithDerivedMs` (`src/lib/ai-edition/timeline/timelineMap.ts:417`):
- `document.zoomRanges`
- `document.annotations`
diff --git a/technical-documentation/architecture/timeline-model.md b/technical-documentation/architecture/timeline-model.md
index 749ee158b..13fd4a1a1 100644
--- a/technical-documentation/architecture/timeline-model.md
+++ b/technical-documentation/architecture/timeline-model.md
@@ -60,19 +60,19 @@ Every public export of
| Function | What it converts | Direction |
|---|---|---|
-| `anchorRawRegionsToClips` (`:56`) | v4 RAW-virtual-ms region → one anchored fragment per covered clip (drops zero-length / off-timeline regions, and the sub-ms sliver a whole-ms edge leaves past a clip boundary) | RAW-virtual → clip-anchored |
-| `anchorRegionsWithDerivedMs` (`:408`) | Same as above but never drops user data: emits `{…fragment, startMs, endMs}` for anchored regions and passes un-anchorable regions through with their original ms | RAW-virtual → v5 stored shape |
-| `anchoredToRawSpanSec` (`:101`) | One anchored fragment → its current RAW-virtual span on the ruler | clip-anchored → RAW-virtual |
-| `regionIdentityKey` (`:172`) | A region → canonical identity key (properties minus position/provenance); equal keys = "same kind, same look" | region → identity string |
-| `coalesceByIdentity` (`:202`) | Set of identified spans → merged runs that touch and share an identity | spans → pills |
-| `clampSpanAgainstNeighbours` (`:239`) | A desired span clamped against different-identity neighbours, backing off toward the span before the edit (a move keeps its length; no cascade) | desired + span before the edit → clamped span |
-| `coalesceRegionsForRuler` (`:281`) | Region array → ruler pills (one entry per merged run, payload carried by `member`) | regions → pills |
-| `resolvePillIds` (`:305`) | Region id → every region id under its pill (recomputed, not stored) | id → ids |
-| `dropPillById` / `dropPillsByIds` (`:320` / `:329`) | Delete every region under a pill (resolved from the merge rule) | regions → regions |
-| `replacePillSpan` (`:347`) | Move/resize a pill: clamp against different-identity neighbours, then re-anchor to the clamped span | pill + clip layout → re-anchored fragments |
-| `segmentRawSpanSec` (`:433`) | One kept playback segment → its RAW-virtual extent | segment → RAW span |
-| `projectRegionsToSource` (`:590`) | Region array → source-ms entries with `clipIndex` for native (anchored path uses anchor; unanchored path falls back to RAW mapping through each segment's own raw extent — never drops an un-anchorable region onto an unrelated clip). A region wholly under a trim is emitted once, marked `underTrim`, addressed by the segment the cut interrupts | RAW/anchored → source + `clipIndex` |
-| `resolveNativePosition` (`:708`) | RAW-virtual playhead → `{clip, clipIndex, sourceTimeSec}` for the active native decoder + paired camera (over a trimmed-out stretch it presents the removed frames themselves, borrowing the same segment index the modifiers under that cut borrow) | RAW-virtual → source + `clipIndex` |
+| `anchorRawRegionsToClips` (`:65`) | v4 RAW-virtual-ms region → one anchored fragment per covered clip (drops zero-length / off-timeline regions, and the sub-ms sliver a whole-ms edge leaves past a clip boundary) | RAW-virtual → clip-anchored |
+| `anchorRegionsWithDerivedMs` (`:417`) | Same as above but never drops user data: emits `{…fragment, startMs, endMs}` for anchored regions and passes un-anchorable regions through with their original ms | RAW-virtual → v5 stored shape |
+| `anchoredToRawSpanSec` (`:110`) | One anchored fragment → its current RAW-virtual span on the ruler | clip-anchored → RAW-virtual |
+| `regionIdentityKey` (`:181`) | A region → canonical identity key (properties minus position/provenance); equal keys = "same kind, same look" | region → identity string |
+| `coalesceByIdentity` (`:211`) | Set of identified spans → merged runs that touch and share an identity | spans → pills |
+| `clampSpanAgainstNeighbours` (`:248`) | A desired span clamped against different-identity neighbours, backing off toward the span before the edit (a move keeps its length; no cascade) | desired + span before the edit → clamped span |
+| `coalesceRegionsForRuler` (`:290`) | Region array → ruler pills (one entry per merged run, payload carried by `member`) | regions → pills |
+| `resolvePillIds` (`:314`) | Region id → every region id under its pill (recomputed, not stored) | id → ids |
+| `dropPillById` / `dropPillsByIds` (`:329` / `:338`) | Delete every region under a pill (resolved from the merge rule) | regions → regions |
+| `replacePillSpan` (`:356`) | Move/resize a pill: clamp against different-identity neighbours, then re-anchor to the clamped span | pill + clip layout → re-anchored fragments |
+| `segmentRawSpanSec` (`:442`) | One kept playback segment → its RAW-virtual extent | segment → RAW span |
+| `projectRegionsToSource` (`:599`) | Region array → source-ms entries with `clipIndex` for native (anchored path uses anchor; unanchored path falls back to RAW mapping through each segment's own raw extent — never drops an un-anchorable region onto an unrelated clip). A region wholly under a trim is emitted once, marked `underTrim`, addressed by the segment the cut interrupts | RAW/anchored → source + `clipIndex` |
+| `resolveNativePosition` (`:717`) | RAW-virtual playhead → `{clip, clipIndex, sourceTimeSec}` for the active native decoder + paired camera (over a trimmed-out stretch it presents the removed frames themselves, borrowing the same segment index the modifiers under that cut borrow) | RAW-virtual → source + `clipIndex` |
The two **universal region rules** every region kind obeys are expressed once in this
file rather than re-derived per kind:
@@ -242,9 +242,11 @@ the contract a reviewer can grade against. Each is asserted in
it is never cut short or thrown past it (#1008). (`replacePillSpan` "stops a zoom
against a neighbour that ends on a clip boundary, keeping its length".)
- **No fragment is a rounding sliver.** Region edges are whole ms and clip boundaries are
- not, so an edge placed on a junction misses it by up to half a ms. That miss is never
- stored as a fragment of its own. (`anchorRegionsWithDerivedMs` "stores no zero-length
- fragment for an edge rounded onto a clip boundary".)
+ not, so an edge placed on a junction misses it by up to half a ms. A piece whose stored
+ whole-ms span would be empty is never stored as a fragment of its own; one that rounds to
+ a millisecond stays, so the region keeps its length. (`anchorRegionsWithDerivedMs` "stores
+ no zero-length fragment for an edge rounded onto a clip boundary", "keeps a
+ half-millisecond piece that rounds to a whole millisecond".)
- **Identity ignores provenance.** `id`, `clipId`, `sourceStartSec`, `reason`,
`origin`, `source`, `annotationSource`, and the legacy `groupId` are *not* part of
the identity key. Two regions that differ only in any of those still merge when