From f23dd8fe2779056c5a83b9cb4646da8f86c1220d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 17:06:03 +0300 Subject: [PATCH] fix(engine): clamp a caption window on GROWTH, and raise the ceiling A ceiling is only half an invariant. Every door -- the parse, `with_subs`, `place_sub` -- sees a window on the way IN, so on its own it left two holes, both of which cost a user their saved work. **The app could write a file it refused to open.** `trim_sub`'s End arm GROWS `out_us`, and it grew past the ceiling: a caption trimmed out to the room the engine itself offered reached 25 hours, `save_project` wrote `sub 1 0 3240000 0 0 90039000000`, and `open_project` refused that file with "past the 86400000000us a caption can hold". The trim is committed and undo does not survive a restart, so there is no in-app way back -- the editor wrote work it then could not reopen. The `saturating_add` that killed the overflow panic did not keep the value inside the bound. **The ceiling was below real use.** A caption's window is a window into SOURCE media, so its width is the length of the recording it was cut from, and that is not bounded by a day. A 25-hour conference stream or a lecture archive measures its own palette row in tens of hours, so the ceiling refused the user's own media back at them, at `place_sub`, with nothing placed. "A window this wide is a number nobody meant" holds for i64::MAX; it does not hold for a long recording. So: - `MAX_WINDOW_US` is 30 days, and the reason is arithmetic rather than editorial. `SubClip::window_at`'s proportion cut saturates rather than wrapping, so the ceiling is about PRECISION: f64 holds an integer exactly to 2^53, and 30 days is 2.6e12 -- nine orders of magnitude inside it, so the cut is exact rather than merely finite. It is 3.5e6x below i64::MAX, and exact in `sub_lane_cues`'s cue mapping for the same reason. No real recording reaches it; a typed number does not survive it. - `grow_window` is the growth law, and is now the single function that may write `out_us`. `trim_sub`'s End arm and the group-follow `write_sub_edge` both go through it. Growth CLAMPS and entry REFUSES, and the asymmetry is the point: a trim is a continuous gesture whose every intermediate frame the user sees, so clamping is a drag that stops -- exactly like the frame walls `trim_sub` already applies. A value arriving from a file or a foreign caller is not mid-gesture, so it is named and refused rather than quietly changed. - `trim_sub_room` is walled by the same number. A front-end asks it to draw the drag and then trims to whatever it is told, so an engine that offers a trim its own `trim_sub` would clamp is a drag that snaps back at the end of the gesture. The room and the trim now agree by construction. Witnesses, each with its own mutation (saveload.rs): - `a_trim_grown_to_the_offered_room_saves_and_reopens` -- 40-day track, trim to the offered room, save, reopen. Clamp removed: "gave a 2592027000000us window, past the 2592000000000us ceiling". - `a_group_trim_carries_its_sibling_no_further_than_the_ceiling` -- `follow_group` SKIPS the dragged member, so a primary-trim-only fix misses this entirely: the route is a sibling on a second lane, placed 20 days into the track so the same delta overshoots. Clamp removed: "the group carried the sibling to a 25919974080030us window" -- 10x the ceiling. - `a_caption_over_a_day_of_track_places_and_round_trips` -- a 25-hour track placed exactly as `Player::sub_of_track` builds it. Ceiling lowered to a day: refused at `place_sub`, nothing placed. Swept every other writer of `in_us`/`out_us`, all now inside the bound: - `sub_clear` (both halves) and `sub_open_room` (both halves): the values are `window_at` results, which `SubClip::window_at` already clamps into the window -- a split cannot widen what it splits. - `Project::close_gaps`-style join: takes the neighbour's own `out_us`. - `trim_sub` Start and `write_sub_edge` Start: `in_us` is clamped to `out_us - 1`, which is below the ceiling. - `app`'s `trimmed_sub`: moves `frames` only, never a clock. - `Project::sub_lane_cues`: reads the window, writes nothing. --- crates/engine/src/project.rs | 98 +++++++++++--- crates/engine/tests/saveload.rs | 218 ++++++++++++++++++++++++++++++++ 2 files changed, 298 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index fe5ec2a..b620085 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -93,19 +93,37 @@ pub const MIN_GAIN_DB: f32 = -60.0; pub const MAX_GAIN_DB: f32 = 12.0; /// The longest `[in_us, out_us)` window a [`SubClip`] may show, in -/// microseconds: a day of track, and past it the number is not a caption any -/// project holds -- a `.edith` a hand edited (or a hostile actor wrote) reaches -/// this with a line the never-empty check at the parser cannot see past, -/// because `out_us > in_us` says the window is *open* and never how wide it is. +/// microseconds: **30 days** of track. /// -/// A ceiling rather than a clamp for the reason [`crate::is_resolution`] is -/// one: a window this wide is a number nobody meant, and quietly turning it -/// into a day-long caption would put words on screen for a day. The loader -/// refuses it by name instead, next to every other range check a `.edith` line -/// has to pass -- and the arithmetic that cuts a window ([`SubClip::window_at`]) -/// saturates besides, so no caller can reintroduce the wrap this door is here -/// to stop. -pub const MAX_WINDOW_US: i64 = 86_400_000_000; +/// A window is a stretch of *source media*, not of the timeline, so its width +/// is the length of the recording a caption was cut from -- and that is not +/// bounded by anything a day. A two-day conference stream, a lecture +/// archive, a long interview session: all of them produce a palette row whose +/// `out_us` is measured in tens of hours, and a ceiling that refuses those is +/// refusing the user's own media back at them. So the ceiling is set where no +/// real recording reaches and a typed number does not survive. +/// +/// What it has to clear, concretely: +/// +/// - [`SubClip::window_at`]'s proportion cut. It saturates rather than +/// wrapping, so this is about *precision*, not safety: `f64` holds an +/// integer exactly to 2^53, and 30 days is 2.6e12 -- nine orders of magnitude +/// inside that, so the cut is exact rather than merely finite. +/// - The `i64` multiply underneath it, which `saturating_mul` already makes +/// total. 30 days is 3.5e6x below `i64::MAX`, so it never saturates in +/// practice either. +/// - `sub_lane_cues`'s f64 mapping of cues onto the timeline, likewise exact. +/// +/// Past it, the value is not a caption any project holds -- a `.edith` a hand +/// edited (or a hostile actor wrote) reaches this with a line the +/// never-empty check at the parser cannot see past, because `out_us > in_us` +/// says the window is *open* and never how wide it is. That line is refused by +/// name, next to every other range check a `.edith` line has to pass. The +/// bounds that *grow* a window rather than read one -- [`Project::trim_sub`] +/// and the group-follow twin it calls -- clamp to this rather than refuse, +/// because a trim that ran out of window should stop at the window's end, not +/// fail: see [`MAX_WINDOW_US`'s growth law](MAX_WINDOW_US). +pub const MAX_WINDOW_US: i64 = 30 * 24 * 60 * 60 * 1_000_000; /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for @@ -2257,10 +2275,18 @@ impl Project { s.start = to; } Edge::End => { - s.out_us = s - .out_us - .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)) - .max(s.in_us + 1); + // ...and `grow_window`, because this arm is where a caption's + // clock grows and the doors only ever *read* one. Without it a + // trim out to the end of a long track produced a window past + // [`MAX_WINDOW_US`], `save_project` wrote it, and the loader + // then refused the file the editor had just written -- a user's + // saved work that stops opening, with no in-app way back. The + // trim stops at the ceiling instead, which is the same answer + // the frame walls above already give for running out of room. + s.out_us = grow_window( + s.in_us, + s.out_us.saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)), + ); s.frames = to - s.start; } } @@ -2311,10 +2337,17 @@ impl Project { .get(s.track) .and_then(|t| t.cues.iter().map(|c| c.end_us).max()) .unwrap_or(s.out_us); + // ...and never past [`grow_window`]'s ceiling, which is the + // wall that matters here: a front-end asks this to draw the + // drag, then trims to whatever it is told. An engine that + // offers room its own trim would refuse is a drag that snaps + // back at the end of the gesture -- so the room stops where + // the window stops, and the two agree by construction. + let wall_us = track_end.min(s.in_us.saturating_add(MAX_WINDOW_US)); ( s.start + 1, s.start - .saturating_add(frames_of(track_end - s.in_us).max(1)) + .saturating_add(frames_of((wall_us - s.in_us).max(1))) .min(subs.get(idx + 1).map_or(u32::MAX, |n| n.start)), ) } @@ -5644,6 +5677,31 @@ fn us_of(frames: i64, fps: f64) -> i64 { } } +/// The growth law, and the one function here that may write `out_us` past what +/// a caller asked for: the ceiling a caption's window may reach, given the +/// `in_us` it starts from. +/// +/// Every *door* -- the parser, [`Project::with_subs`], [`Project::place_sub`] +/// -- refuses a window wider than [`MAX_WINDOW_US`] by name. A door only sees +/// a value on the way IN, so on its own it is not enough: [`Project::trim_sub`] +/// and the group-follow [`write_sub_edge`] both *grow* `out_us`, and a project +/// could be trimmed past the ceiling after loading legally. That produced a +/// file `save_project` wrote and `open_project` then refused -- the user's own +/// saved work, stopping, with no way back in the app. +/// +/// So growth clamps and entry refuses, and that asymmetry is the point: a trim +/// is a continuous gesture whose every intermediate frame the user sees, so +/// clamping it to the end of the window is a drag that stops -- exactly like +/// the frame walls `trim_sub` already applies. A value *arriving* from a file +/// or a foreign caller is a different thing, where nobody was mid-gesture, so +/// it is named and refused rather than quietly changed. +/// +/// The `+ 1` is the never-empty invariant: the window is open, always, so a +/// growth that would otherwise clamp it shut lands on one microsecond instead. +fn grow_window(in_us: i64, out_us: i64) -> i64 { + out_us.clamp(in_us + 1, in_us.saturating_add(MAX_WINDOW_US)) +} + /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`, /// dropping what is inside the hole, splitting what straddles it and trimming /// what overlaps an edge. The windows follow the cut by proportion @@ -5757,7 +5815,11 @@ fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) { s.start = to; } Edge::End => { - s.out_us = s.out_us.saturating_add(by(s.end(), to)).max(s.in_us + 1); + // `grow_window`, for [`Project::trim_sub`]'s reason: this is the + // group-follow twin and it reaches the same field, so a group trim + // would otherwise be a way round the ceiling the primary trim + // respects. + s.out_us = grow_window(s.in_us, s.out_us.saturating_add(by(s.end(), to))); s.frames = to - s.start; } } diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index 3b77c6c..87a9c26 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -1083,3 +1083,221 @@ fn a_caption_window_no_project_can_hold_is_refused_by_name() { session.sub_lane(s1) ); } + +/// A subtitle track whose last cue ends `hours` in, written into `dir`. +/// +/// A caption's window is a window into SOURCE media, so its width is however +/// long the recording was. Nothing in the format bounds that, and a lecture +/// archive or a conference stream is tens of hours -- which is what a ceiling +/// under a day refuses back at the user. The hour field is parsed as a plain +/// `u32` with no digit limit, so 960 is a legal 40 days. +fn long_srt(dir: &Path, hours: u32) -> PathBuf { + let path = dir.join(format!("subs_{hours}h.srt")); + std::fs::write( + &path, + format!( + "1\n00:00:00,500 --> 00:00:01,500\nfirst line\n\n\ + 2\n00:00:01,600 --> {hours}:00:00,000\nlast line of a long recording\n" + ), + ) + .expect("write a long subtitle track"); + path +} + +/// The round trip a trim has to survive: grow a caption's window to the room +/// the engine itself offers, save, and open the file that was just written. +/// +/// This is the hole a ceiling on its own leaves. Every door -- the parse, +/// `with_subs`, `place_sub` -- only ever sees a window on the way *in*, so a +/// project could load legally and then be trimmed past the ceiling: `save` +/// wrote the line and the loader refused it again, which is the user's own +/// saved work stopping, with no in-app way back (the trim is committed and the +/// undo does not survive a restart). So growth clamps (`grow_window`) and the +/// room the front-end drags against is walled by the same number, and the two +/// agree by construction rather than by luck. +#[test] +fn a_trim_grown_to_the_offered_room_saves_and_reopens() { + use engine::project::{Edge, MAX_WINDOW_US, SubClip}; + + let dir = scratch("caption_growth"); + let media = copy_in(&dir, "test_av.mp4"); + // 40 days of track, comfortably past the 30-day ceiling: only the growth + // clamp can keep this inside it, never the ceiling itself. + let subs = long_srt(&dir, 960); + + let mut session = PlaybackSession::open(&media).expect("open the fixture"); + session + .import_subtitles(&subs) + .expect("a 40-day subtitle track imports"); + let s1 = session.add_lane(engine::project::LaneKind::Subtitle); + session + .place_sub( + s1, + 60, + SubClip { + start: 0, + frames: 90, + track: 0, + in_us: 0, + out_us: 30_000_000, + link: None, + }, + ) + .expect("a 30s window places"); + + // The room the drag is drawn against. It must itself stop at the ceiling: + // an engine that offers a trim its own `trim_sub` would clamp is a drag + // that snaps back at the end of the gesture. + let (_, last) = session + .trim_sub_room(s1, 0, Edge::End) + .expect("a room for a caption that is there"); + session + .trim_sub(s1, 0, Edge::End, last) + .expect("the trim to its own offered room"); + + let grown = session.sub_lane(s1)[0]; + assert!( + (grown.out_us - grown.in_us) <= MAX_WINDOW_US, + "trimming to the offered room gave a {}us window, past the {}us ceiling", + grown.out_us - grown.in_us, + MAX_WINDOW_US + ); + + let saved = dir.join("grown.edith"); + session.save_project(&saved).expect("save"); + let reopened = PlaybackSession::open_project(&saved) + .expect("a project the editor wrote must open again -- that is the whole claim"); + let back = reopened.sub_lane(s1)[0]; + assert_eq!( + (back.in_us, back.out_us), + (grown.in_us, grown.out_us), + "and the window came back as it was saved" + ); +} + +/// The ceiling itself, against the case it was measured on: a 25-hour +/// recording. `place_sub` is the door a palette row's drag comes through +/// (`sub_of_track` builds the placement from the track's own last cue), so a +/// ceiling under the length of real media refuses the user's own file back at +/// them, with nothing placed. +#[test] +fn a_caption_over_a_day_of_track_places_and_round_trips() { + use engine::project::MAX_WINDOW_US; + + let dir = scratch("caption_long_track"); + let media = copy_in(&dir, "test_av.mp4"); + let subs = long_srt(&dir, 25); + let whole_track_us = 25i64 * 60 * 60 * 1_000_000; + assert!( + whole_track_us > 86_400_000_000, + "the fixture must really be over a day, or this test proves nothing" + ); + assert!( + whole_track_us < MAX_WINDOW_US, + "and comfortably under the ceiling, or the test is not about the ceiling" + ); + + let mut session = PlaybackSession::open(&media).expect("open the fixture"); + assert_eq!( + session.import_subtitles(&subs).expect("a 25-hour track imports"), + 1, + "one track" + ); + let s1 = session.add_lane(engine::project::LaneKind::Subtitle); + // Exactly what `Player::sub_of_track` hands `place_sub` for palette row 0. + let fps = f64::from(session.meta().frame_rate); + session + .place_sub( + s1, + 0, + engine::project::SubClip { + start: 0, + frames: ((whole_track_us as f64) * fps / 1e6).round() as u32, + track: 0, + in_us: 0, + out_us: whole_track_us, + link: None, + }, + ) + .expect("a caption over a 25-hour recording places -- that is the claim"); + + let saved = dir.join("long.edith"); + session.save_project(&saved).expect("save"); + let reopened = PlaybackSession::open_project(&saved).expect("and reopens"); + assert_eq!( + reopened.sub_lane(s1)[0].out_us, whole_track_us, + "the whole window survived the round trip" + ); +} + +/// The second writer, which is the one a primary-trim-only fix would miss. +/// +/// [`write_sub_edge`] carries a *sibling* in a group by the same delta as the +/// member that was dragged, and reaches the same `out_us`. `follow_group` +/// skips the dragged member, so the route here is a second caption in the +/// group on another lane: it is not the one whose `trim_sub` arm is fixed, so +/// nothing about the dragged caption's own clamp bounds it. +/// +/// The sibling starts deep into the track -- 20 days in -- so the same delta +/// that stops the dragged caption exactly at the ceiling carries the sibling +/// past it. Without the clamp on this writer the sibling is 20 days wider than +/// the ceiling when `save_project` runs, and the file it writes will not open. +#[test] +fn a_group_trim_carries_its_sibling_no_further_than_the_ceiling() { + use engine::project::{Edge, LaneKind, MAX_WINDOW_US, SubClip}; + + let dir = scratch("caption_group_growth"); + let media = copy_in(&dir, "test_av.mp4"); + let subs = long_srt(&dir, 960); + let day = 24 * 60 * 60 * 1_000_000i64; + + let mut session = PlaybackSession::open(&media).expect("open the fixture"); + session.import_subtitles(&subs).expect("a 40-day track imports"); + let s1 = session.add_lane(LaneKind::Subtitle); + let s2 = session.add_lane(LaneKind::Subtitle); + let caption = |in_us| SubClip { + start: 0, + frames: 90, + track: 0, + in_us, + out_us: in_us + 30_000_000, + link: None, + }; + session + .place_sub(s1, 60, caption(0)) + .expect("the dragged caption places"); + session + .place_sub(s2, 60, caption(20 * day)) + .expect("its sibling, 20 days into the track"); + session + .group_all(&[(s1, 0), (s2, 0)]) + .expect("the two captions into one group"); + + // Drag the first caption's tail out to the room the engine offers it, which + // is a 40-day track so the room is the ceiling, not the track. + let (_, last) = session + .trim_sub_room(s1, 0, Edge::End) + .expect("a room for the dragged caption"); + session + .trim_sub(s1, 0, Edge::End, last) + .expect("the drag commits"); + + let dragged = session.sub_lane(s1)[0]; + assert_eq!( + dragged.out_us - dragged.in_us, + MAX_WINDOW_US, + "the dragged caption stops exactly at the ceiling, which is the room it was offered" + ); + let sibling = session.sub_lane(s2)[0]; + assert!( + (sibling.out_us - sibling.in_us) <= MAX_WINDOW_US, + "the group carried the sibling to a {}us window, past the {}us ceiling -- \ + the dragged caption's own clamp does not reach here", + sibling.out_us - sibling.in_us, + MAX_WINDOW_US + ); + + let saved = dir.join("group.edith"); + session.save_project(&saved).expect("save"); + PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens"); +}