forge
Explore
Log in

Don't you dare go Hollow

Make the caption-window bound structural, not per-arm

Browse files at this commit

eb3f5714f3bcbc3ced3e7380eaab6959c04115b3Ahmet Kaan GümüşParent: f23dd8f
  • fix
    • make the caption-window bound structural, not per-arm
Show full message
fix(engine): make the caption-window bound structural, not per-arm

Three rounds of review each found one more composer a per-arm check had
missed, and the third is the one that ends the sequence: the fix is not
another arm, it is the invariant's shape.

`grow_window` was not, in fact, the single writer of a window.

**A rejoin SUMS two legal halves.** `Project::regroup` keeps the left
half's `in_us` and takes the right half's `out_us` -- and `sub_joinable`
has just established that the halves are consecutive in the *source*,
so the joined width is the sum of two widths that each passed their own
door. Neither half is illegal; the whole is. Two 20-day windows and one
undo-a-split keystroke gave 40 days, which `save_project` wrote and
`open_project` refused. My sweep had disposed of this as "takes the
neighbour's own out_us" -- true, and beside the point: the ceiling bounds
a SUBTRACTION, not its operands.

**A head drag grows the width too.** The ceiling bounds a difference, so
falling `in_us` makes the window wider exactly as growing `out_us`
does. The Start arms clamped `in_us` from above (`in_us <= out_us - 1`),
which says nothing at all about the ceiling: a caption a year into its
source, dragged back 200 days, was 200 days wide with `out_us` never
moving.

So the bound is now one function every writer routes through:

    set_window(&mut SubClip, in_us, out_us)
    // 0 <= in_us < out_us  &&  out_us - in_us <= MAX_WINDOW_US

and `grow_window` is gone -- it was a helper around one arm, which is
what made the other arms look exempt. Everything goes through
`set_window` now: `trim_sub` both edges, `write_sub_edge` both edges,
`regroup`'s join, and the four halves `sub_clear` and `sub_open_room`
build. The doors still REFUSE on entry (parse, `with_subs`, `place_sub`)
and `set_window` still CLAMPS on composition, because that asymmetry is
the point: a door sees a value nobody was mid-gesture on, a composer is
an edit a person is watching happen.

**The closed set is now proved, not argued.** Reasoning about arms does
not scale to an unknown number of them, so
`project::tests::every_window_write_goes_through_set_window` reads both
files and fails if `in_us`/`out_us` is written anywhere outside
`set_window` and the parser's two door lines. It is deliberately a
source scan rather than a `debug_assert!`, because a `debug_assert!`
only fires on a path some test happens to walk.

That scan was vacuous the first two times and the test says so:
cutting at the first `#[cfg(test)]` scanned 160 lines of a 10,800-line
file (a smuggled writer in `write_sub_edge` sailed through), and
cutting at the last one landed inside the scan's own test module. It now
cuts at the test module and asserts how many lines it read, because a
guard that reads nothing is worse than no guard -- it reads as one.

**The ceiling's stated reason was wrong and is now right.** I had claimed
`window_at` needed the ceiling below 2^53 for f64 exactness. It does not:
it is pure i64 with a `saturating_mul`, so the f64 argument never
applied. The real arithmetic: `window_us() * into` with `into` up to
4.29e9 saturates for any window above `i64::MAX / u32::MAX` = 2.1e9us
(~35 minutes), so 30 days is 1200x past the point where the proportion
stops being exact -- which is the actual precision cost, and the reason
this is not "unlimited". f64 exactness IS the right argument for
`sub_lane_cues`'s cue mapping, where 30 days is ~3.5 orders inside
2^53. Nothing else derives anything from a window, so a wider value
costs no memory: the ceiling is a claim about meaning, not resource.

Witnesses, each with its own mutation:

- `a_rejoin_of_two_legal_windows_stays_inside_the_ceiling` -- two 20-day
  halves, one rejoin. Un-routed: "the rejoin summed two 1296000000000us
  windows into 3456000000000us, past the 2592000000000us ceiling".
- `a_head_drag_no_further_than_the_ceiling` -- a year-deep caption, one
  head drag to the offered room. Start arm reverted to a raw `in_us`
  assignment: "gave a 3000030000000us window".
- `every_window_write_goes_through_set_window` -- passes clean; fails on
  a smuggled writer in `write_sub_edge`, on on a smuggled writer in
  `edith.rs`, and on `regroup`'s two lines moved out of `set_window`.
- Loosening `set_window` itself (dropping the ceiling clamp) reds all
  four caption witnesses at once, which is the property a per-arm fix
  does not have.

UX note, recorded as the verifier found it and left unchanged: a drag
that reaches the ceiling stops silently. That is consistent with every
other trim wall here -- `trim_to` drops the snap cue, `commit_trim`
toasts only on `Err` -- so the ceiling behaves like a wall, not like an
error.
crates/engine/src/project.rsmodified+217 -94
@@ -97,32 +97,46 @@ pub const MAX_GAIN_DB: f32 = 12.0;
97 ///97 ///
98 /// A window is a stretch of *source media*, not of the timeline, so its width98 /// A window is a stretch of *source media*, not of the timeline, so its width
99 /// is the length of the recording a caption was cut from -- and that is not99 /// is the length of the recording a caption was cut from -- and that is not
100-/// bounded by anything a day. A two-day conference stream, a lecture100+/// bounded by anything a day. A two-day conference stream, a lecture archive, a
101-/// archive, a long interview session: all of them produce a palette row whose101+/// long interview session: all produce a palette row whose `out_us` is tens of
102-/// `out_us` is measured in tens of hours, and a ceiling that refuses those is102+/// hours, and a ceiling that refuses those refuses the user's own media back at
103-/// refusing the user's own media back at them. So the ceiling is set where no103+/// them. The value is therefore "past any recording", and nothing more
104-/// real recording reaches and a typed number does not survive.104+/// sophisticated: 30 days is a round number four orders of magnitude above the
105+/// longest thing anybody captions and three orders below the point where a
106+/// typed number becomes plausible as a duration at all.
105 ///107 ///
106-/// What it has to clear, concretely:108+/// What it has to clear, concretely, and the honest arithmetic for each:
107 ///109 ///
108-/// - [`SubClip::window_at`]'s proportion cut. It saturates rather than110+/// - **The i64 multiply in [`SubClip::window_at`].** `window_us() * into`,
109-/// wrapping, so this is about *precision*, not safety: `f64` holds an111+/// where `into` is a `u32` frame offset, so at most 4.29e9. It is a
110-/// integer exactly to 2^53, and 30 days is 2.6e12 -- nine orders of magnitude112+/// `saturating_mul`, so this is not about safety -- saturation is total --
111-/// inside that, so the cut is exact rather than merely finite.113+/// but about the result still *meaning* something: 30 days x 4.29e9 is
112-/// - The `i64` multiply underneath it, which `saturating_mul` already makes114+/// 1.1e22, which does NOT fit an i64. So the multiply saturates for any
113-/// total. 30 days is 3.5e6x below `i64::MAX`, so it never saturates in115+/// window above `i64::MAX / u32::MAX` = 2.1e9 us (~35 minutes), and 30 days
114-/// practice either.116+/// is 1200x past that. The value is then finite but the proportion is not
115-/// - `sub_lane_cues`'s f64 mapping of cues onto the timeline, likewise exact.117+/// exact, and `set_window`'s clamp on the answer keeps the result inside the
118+/// window rather than trusting it. That is the real precision cost, and it
119+/// is why this is not "unlimited": past 35 minutes the cut degrades, past
120+/// this it has degraded to "the far end of the window".
121+/// - **`sub_lane_cues`'s f64 mapping.** `(t - in_us) as f64 * per`, where `per`
122+/// is `frames / fps * 1e6 / window_us`. Here f64 exactness genuinely does
123+/// apply: an i64 is exact in f64 to 2^53 = 9.0e15, and 30 days is 2.6e12 --
124+/// about 3.5 orders of magnitude inside it, so the cue mapping is exact and
125+/// not merely close.
126+/// - **Nothing else.** There is no allocation, no index and no length derived
127+/// from a window, so a wider value costs no memory. The ceiling is a claim
128+/// about meaning, not about resource.
116 ///129 ///
117 /// Past it, the value is not a caption any project holds -- a `.edith` a hand130 /// Past it, the value is not a caption any project holds -- a `.edith` a hand
118-/// edited (or a hostile actor wrote) reaches this with a line the131+/// edited (or a hostile actor wrote) reaches this with a line the never-empty
119-/// never-empty check at the parser cannot see past, because `out_us > in_us`132+/// check at the parser cannot see past, because `out_us > in_us` says the
120-/// says the window is *open* and never how wide it is. That line is refused by133+/// window is *open* and never how wide it is. Every door -- the parse,
121-/// name, next to every other range check a `.edith` line has to pass. The134+/// [`Project::with_subs`], [`Project::place_sub`] -- refuses such a line by
122-/// bounds that *grow* a window rather than read one -- [`Project::trim_sub`]135+/// name, next to every other range check a `.edith` line has to pass. Every
123-/// and the group-follow twin it calls -- clamp to this rather than refuse,136+/// *composer* instead clamps to it through [`set_window`], which is the only
124-/// because a trim that ran out of window should stop at the window's end, not137+/// function that writes a window; a trim that ran out of window stops at the
125-/// fail: see [`MAX_WINDOW_US`'s growth law](MAX_WINDOW_US).138+/// window's end rather than failing, exactly like the frame walls it already
139+/// has.
126 pub const MAX_WINDOW_US: i64 = 30 * 24 * 60 * 60 * 1_000_000;140 pub const MAX_WINDOW_US: i64 = 30 * 24 * 60 * 60 * 1_000_000;
127 141
128 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it142 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it
@@ -2260,32 +2274,30 @@ impl Project {
2260 // The words that stay are the words that were there, so the window2274 // The words that stay are the words that were there, so the window
2261 // moves with the head -- a trim, not a slip.2275 // moves with the head -- a trim, not a slip.
2262 Edge::Start => {2276 Edge::Start => {
2263- // Clamped to a window that is still a window: the walls above2277+ // The head moves and the words that stay are the words that
2264- // are frames and the rounding between the two clocks is not2278+ // were there, so `in_us` follows it -- a trim, not a slip.
2265- // exact, and neither is a caller's hand-built placement. The2279+ // Falling `in_us` makes the window *wider* (the ceiling bounds
2266- // add saturates for the reason [`SubClip::window_at`]'s does --2280+ // a difference), which is the half of this nothing here can
2267- // this is one of the two places a caption's clock is composed,2281+ // see: `set_window` is what puts the width back inside.
2268- // and a `SubClip` that did not come through a door is the one2282+ set_window(
2269- // whose `in_us` may already be at the top of `i64`.2283+ s,
2270- s.in_us = s2284+ s.in_us
2271- .in_us2285+ .saturating_add(us_of(i64::from(to) - i64::from(s.start), fps)),
2272- .saturating_add(us_of(i64::from(to) - i64::from(s.start), fps))2286+ s.out_us,
2273- .clamp(0, s.out_us - 1);2287+ );
2274 s.frames = s.end() - to;2288 s.frames = s.end() - to;
2275 s.start = to;2289 s.start = to;
2276 }2290 }
2277 Edge::End => {2291 Edge::End => {
2278- // ...and `grow_window`, because this arm is where a caption's2292+ // ...and the tail, for [`set_window`]'s reason: the End room
2279- // clock grows and the doors only ever *read* one. Without it a2293+ // is walled at the ceiling, but `set_window` is what holds it
2280- // trim out to the end of a long track produced a window past2294+ // for every other composer too, including this one when a
2281- // [`MAX_WINDOW_US`], `save_project` wrote it, and the loader2295+ // caller hands `trim_sub` a `to` past its own room.
2282- // then refused the file the editor had just written -- a user's2296+ set_window(
2283- // saved work that stops opening, with no in-app way back. The2297+ s,
2284- // trim stops at the ceiling instead, which is the same answer
2285- // the frame walls above already give for running out of room.
2286- s.out_us = grow_window(
2287 s.in_us,2298 s.in_us,
2288- s.out_us.saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)),2299+ s.out_us
2300+ .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)),
2289 );2301 );
2290 s.frames = to - s.start;2302 s.frames = to - s.start;
2291 }2303 }
@@ -3468,7 +3480,15 @@ impl Project {
3468 for (data, sub) in self.lanes.iter_mut().zip(join_subs) {3480 for (data, sub) in self.lanes.iter_mut().zip(join_subs) {
3469 if let Some(idx) = sub {3481 if let Some(idx) = sub {
3470 data.subs[idx].frames += data.subs[idx + 1].frames;3482 data.subs[idx].frames += data.subs[idx + 1].frames;
3471- data.subs[idx].out_us = data.subs[idx + 1].out_us;3483+ // The left half's `in_us`, the right half's `out_us` -- which
3484+ // is the SUM of two legal widths, because `sub_joinable` has
3485+ // just established that the two halves are consecutive in the
3486+ // *source* (`next.in_us == subs[idx].out_us`). Neither half
3487+ // broke the ceiling and the whole does, so this is a composer
3488+ // a per-arm check on one writer could never have found:
3489+ // `set_window` is what makes the closed set closed.
3490+ let (in_us, out_us) = (data.subs[idx].in_us, data.subs[idx + 1].out_us);
3491+ set_window(&mut data.subs[idx], in_us, out_us);
3472 data.subs.remove(idx + 1);3492 data.subs.remove(idx + 1);
3473 debug_assert!(subs_sorted_disjoint(&data.subs));3493 debug_assert!(subs_sorted_disjoint(&data.subs));
3474 }3494 }
@@ -5677,29 +5697,54 @@ fn us_of(frames: i64, fps: f64) -> i64 {
5677 }5697 }
5678 }5698 }
5679 5699
5680-/// The growth law, and the one function here that may write `out_us` past what5700+/// **The one function in this file that writes a caption's window.** Every
5681-/// a caller asked for: the ceiling a caption's window may reach, given the5701+/// composer -- a trim, a split, a ripple, a group follow, a rejoin -- routes
5682-/// `in_us` it starts from.5702+/// through it, and the invariant it holds is total:
5703+///
5704+/// ```text
5705+/// 0 <= in_us and in_us < out_us and out_us - in_us <= MAX_WINDOW_US
5706+/// ```
5683 ///5707 ///
5684-/// Every *door* -- the parser, [`Project::with_subs`], [`Project::place_sub`]5708+/// which is the never-empty invariant plus the width ceiling, restated as one
5685-/// -- refuses a window wider than [`MAX_WINDOW_US`] by name. A door only sees5709+/// shape both ends of it come from. Note that the ceiling bounds a
5686-/// a value on the way IN, so on its own it is not enough: [`Project::trim_sub`]5710+/// **difference**, which is why a per-arm check is not enough and this is:
5687-/// and the group-follow [`write_sub_edge`] both *grow* `out_us`, and a project
5688-/// could be trimmed past the ceiling after loading legally. That produced a
5689-/// file `save_project` wrote and `open_project` then refused -- the user's own
5690-/// saved work, stopping, with no way back in the app.
5691 ///5711 ///
5692-/// So growth clamps and entry refuses, and that asymmetry is the point: a trim5712+/// - Growing `out_us` (a tail trim) makes the width larger, so the *upper*
5693-/// is a continuous gesture whose every intermediate frame the user sees, so5713+/// wall bites.
5694-/// clamping it to the end of the window is a drag that stops -- exactly like5714+/// - Falling `in_us` (a head trim) makes the width larger too, from the other
5695-/// the frame walls `trim_sub` already applies. A value *arriving* from a file5715+/// side -- so bounding `in_us` alone, which is what a head trim's own clamp
5696-/// or a foreign caller is a different thing, where nobody was mid-gesture, so5716+/// (`in_us <= out_us - 1`) does, says nothing at all about the ceiling.
5697-/// it is named and refused rather than quietly changed.5717+/// - A rejoin takes the left half's `in_us` and the right half's `out_us`, and
5718+/// the two halves are consecutive in the *source* (that is what
5719+/// [`sub_joinable`] requires), so the joined width is the **sum** of two
5720+/// legal widths. Each half passes its own door; the whole does not.
5698 ///5721 ///
5699-/// The `+ 1` is the never-empty invariant: the window is open, always, so a5722+/// Three rounds of review each found one more composer this way, so the
5700-/// growth that would otherwise clamp it shut lands on one microsecond instead.5723+/// function is the fix rather than any single call site: clamping a new
5701-fn grow_window(in_us: i64, out_us: i64) -> i64 {5724+/// caller into shape is a two-line change, and forgetting one is silent.
5702- out_us.clamp(in_us + 1, in_us.saturating_add(MAX_WINDOW_US))5725+/// `every_window_write_goes_through_set_window` (in the tests below) is the
5726+/// other half of that promise -- it scans the file and fails if a write
5727+/// appears anywhere else.
5728+///
5729+/// **Why it clamps where the doors refuse.** The doors -- the parser,
5730+/// [`Project::with_subs`], [`Project::place_sub`] -- see a value on the way
5731+/// IN, from a file or a foreign caller, where nobody was mid-gesture, so a
5732+/// value that wide is named and refused rather than quietly changed. Every
5733+/// composer below is an edit a person is watching happen, so the same value is
5734+/// clamped to the end of the window: a drag that stops, exactly like the frame
5735+/// walls these trims already apply.
5736+fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) {
5737+ let in_us = in_us.max(0);
5738+ // The `+ 1` is the never-empty invariant: the window is open, always, so a
5739+ // composer that would otherwise clamp it shut lands on one microsecond.
5740+ let out_us = out_us.clamp(in_us + 1, in_us.saturating_add(MAX_WINDOW_US));
5741+ s.in_us = in_us;
5742+ s.out_us = out_us;
5743+ debug_assert!(
5744+ s.in_us >= 0 && s.out_us > s.in_us && s.out_us - s.in_us <= MAX_WINDOW_US,
5745+ "set_window left an illegal window: {:?}",
5746+ (s.in_us, s.out_us)
5747+ );
5703 }5748 }
5704 5749
5705 /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`,5750 /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`,
@@ -5714,24 +5759,31 @@ fn sub_clear(subs: &mut Vec<SubClip>, start: u32, end: u32) {
5714 continue;5759 continue;
5715 }5760 }
5716 if s.start < start {5761 if s.start < start {
5717- out.push(SubClip {5762+ let cut = s.window_at(start);
5763+ let mut head = SubClip {
5718 frames: start - s.start,5764 frames: start - s.start,
5719- out_us: s.window_at(start),
5720 // Both pieces lose the group id, for [`clear`]'s reason: one5765 // Both pieces lose the group id, for [`clear`]'s reason: one
5721 // id on two boxes of one lane is the one thing it may never5766 // id on two boxes of one lane is the one thing it may never
5722 // mean.5767 // mean.
5723 link: None,5768 link: None,
5724 ..s5769 ..s
5725- });5770+ };
5771+ // `window_at` already answers inside the window, and `set_window`
5772+ // is the shape that says so once, for every composer, rather than
5773+ // leaving each of them to re-derive it.
5774+ set_window(&mut head, s.in_us, cut);
5775+ out.push(head);
5726 }5776 }
5727 if s.end() > end {5777 if s.end() > end {
5728- out.push(SubClip {5778+ let cut = s.window_at(end);
5779+ let mut tail = SubClip {
5729 start: end,5780 start: end,
5730 frames: s.end() - end,5781 frames: s.end() - end,
5731- in_us: s.window_at(end),
5732 link: None,5782 link: None,
5733 ..s5783 ..s
5734- });5784+ };
5785+ set_window(&mut tail, cut, s.out_us);
5786+ out.push(tail);
5735 }5787 }
5736 }5788 }
5737 *subs = out;5789 *subs = out;
@@ -5745,22 +5797,21 @@ fn sub_open_room(subs: &mut Vec<SubClip>, at: u32, len: u32) {
5745 if let Some(idx) = subs.iter().position(|s| s.start < at && at < s.end()) {5797 if let Some(idx) = subs.iter().position(|s| s.start < at && at < s.end()) {
5746 let s = subs[idx];5798 let s = subs[idx];
5747 let cut = s.window_at(at);5799 let cut = s.window_at(at);
5748- subs[idx] = SubClip {5800+ let mut head = SubClip {
5749 frames: at - s.start,5801 frames: at - s.start,
5750- out_us: cut,
5751 link: None,5802 link: None,
5752 ..s5803 ..s
5753 };5804 };
5754- subs.insert(5805+ set_window(&mut head, s.in_us, cut);
5755- idx + 1,5806+ subs[idx] = head;
5756- SubClip {5807+ let mut tail = SubClip {
5757- start: at,5808+ start: at,
5758- frames: s.end() - at,5809+ frames: s.end() - at,
5759- in_us: cut,5810+ link: None,
5760- link: None,5811+ ..s
5761- ..s5812+ };
5762- },5813+ set_window(&mut tail, cut, s.out_us);
5763- );5814+ subs.insert(idx + 1, tail);
5764 }5815 }
5765 for s in subs.iter_mut().filter(|s| s.start >= at) {5816 for s in subs.iter_mut().filter(|s| s.start >= at) {
5766 s.start += len;5817 s.start += len;
@@ -5801,25 +5852,21 @@ fn sub_edge_room(subs: &[SubClip], idx: usize, edge: Edge) -> (u32, u32) {
5801 /// the window it already shows over the frames it already takes.5852 /// the window it already shows over the frames it already takes.
5802 fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) {5853 fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) {
5803 let per_frame = s.window_us() / i64::from(s.frames.max(1));5854 let per_frame = s.window_us() / i64::from(s.frames.max(1));
5804- // The `saturating_add` beside the `saturating_mul` is the same claim5855+ // The `saturating_mul` keeps a `SubClip` that reached here without a door
5805- // [`SubClip::window_at`]'s arithmetic makes, in the other direction: this5856+ // behind it from wrapping the window negative; `set_window` is what keeps
5806- // one *grows* a caption's clock, and a `SubClip` that reached here without5857+ // the result legal rather than merely finite. This is the group-follow twin
5807- // a door behind it may already be at the top of `i64`, where a plain add5858+ // of [`Project::trim_sub`], and it reaches the same field from both edges --
5808- // wrapped the window negative and the two halves a trim hands back no5859+ // so a fix that only walled the primary trim's End arm left the head (and
5809- // longer met.5860+ // the whole group path) open.
5810 let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from));5861 let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from));
5811 match edge {5862 match edge {
5812 Edge::Start => {5863 Edge::Start => {
5813- s.in_us = s.in_us.saturating_add(by(s.start, to)).clamp(0, s.out_us - 1);5864+ set_window(s, s.in_us.saturating_add(by(s.start, to)), s.out_us);
5814 s.frames = s.end() - to;5865 s.frames = s.end() - to;
5815 s.start = to;5866 s.start = to;
5816 }5867 }
5817 Edge::End => {5868 Edge::End => {
5818- // `grow_window`, for [`Project::trim_sub`]'s reason: this is the5869+ set_window(s, s.in_us, s.out_us.saturating_add(by(s.end(), to)));
5819- // group-follow twin and it reaches the same field, so a group trim
5820- // would otherwise be a way round the ceiling the primary trim
5821- // respects.
5822- s.out_us = grow_window(s.in_us, s.out_us.saturating_add(by(s.end(), to)));
5823 s.frames = to - s.start;5870 s.frames = to - s.start;
5824 }5871 }
5825 }5872 }
@@ -10720,4 +10767,80 @@ fn removing_a_subtitle_column_undoes_with_its_reindex() {
10720 assert_ne!(p.subtitles(), palette.as_slice(), "and it is not back");10767 assert_ne!(p.subtitles(), palette.as_slice(), "and it is not back");
10721 }10768 }
10722 10769
10770+ /// The closed set, proved by reading the files rather than by exercising
10771+ /// every path: a caption's `in_us`/`out_us` is written in exactly two
10772+ /// places, and this fails if a third appears.
10773+ ///
10774+ /// Three rounds of review each found one more composer a per-arm check had
10775+ /// missed -- a tail trim, a head trim, a rejoin that SUMS two legal widths
10776+ /// -- and each produced the same failure: the editor wrote a `.edith` its
10777+ /// own loader then refused, with no in-app way back. Reasoning about arms
10778+ /// does not scale to an unknown number of them, so the invariant is stated
10779+ /// structurally instead: a new composer routed through `set_window` is two
10780+ /// lines and needs no new argument, and one that is not fails here.
10781+ ///
10782+ /// The two allowed writers are `set_window` (every composer) and the
10783+ /// parser's own door, which builds the `SubClip` a `sub` line describes and
10784+ /// range-checks it on the next lines -- a door, not a composer, because a
10785+ /// file is not an edit anybody is watching happen.
10786+ #[test]
10787+ fn every_window_write_goes_through_set_window() {
10788+ // `CARGO_MANIFEST_DIR`, not `file!()`: a test runs with the WORKSPACE
10789+ // root as its working directory, so `file!()`'s crate-relative path
10790+ // does not resolve from where the binary actually starts.
10791+ let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
10792+ // Every line in the two files that names a clock and is not a write:
10793+ // the struct's own fields, the one function that may write, and the
10794+ // parser's two door lines. A new entry here is a claim someone has to
10795+ // defend in review, which is the point of writing the list out.
10796+ let allowed = [
10797+ ("project.rs", "pub in_us: i64,"),
10798+ ("project.rs", "pub out_us: i64,"),
10799+ ("project.rs", "fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) {"),
10800+ ("project.rs", "s.in_us = in_us;"),
10801+ ("project.rs", "s.out_us = out_us;"),
10802+ ("edith.rs", "in_us: micros(f[3], n)?,"),
10803+ ("edith.rs", "out_us: micros(f[4], n)?,"),
10804+ ];
10805+ let mut found: Vec<String> = Vec::new();
10806+ let mut scanned = 0usize;
10807+ for name in ["project.rs", "edith.rs"] {
10808+ let text = std::fs::read_to_string(dir.join(name)).expect("read the file");
10809+ // The test MODULE, not any `cfg(test)`: this file carries several
10810+ // `cfg(test)` attributes on single test-only helpers, and two wrong
10811+ // boundaries were tried before this one -- the FIRST (line 160)
10812+ // scanned 160 lines of a 10,800-line file, and the LAST landed
10813+ // inside this very test, dragging the test module's own
10814+ // hand-built `SubClip`s into scope. A guard that reads nothing,
10815+ // and one that reads the wrong thing, are both worse than no
10816+ // guard because they read as one. The line count asserted below is
10817+ // what keeps this third boundary honest.
10818+ let at = text
10819+ .find("#[cfg(test)]\nmod tests {")
10820+ .expect("this file's test module is still spelled that way");
10821+ let code = &text[..at];
10822+ for line in code.lines() {
10823+ scanned += 1;
10824+ let line = line.trim();
10825+ let writes = line.contains(".in_us = ")
10826+ || line.contains(".out_us = ")
10827+ || line.contains("in_us:")
10828+ || line.contains("out_us:");
10829+ if writes && !allowed.contains(&(name, line)) {
10830+ found.push(format!("{name}: {line}"));
10831+ }
10832+ }
10833+ }
10834+ assert!(
10835+ scanned > 3_000,
10836+ "the scan only read {scanned} lines -- the cfg(test) boundary is wrong, \
10837+ and a guard that reads nothing is worse than no guard"
10838+ );
10839+ assert!(
10840+ found.is_empty(),
10841+ "a caption window is written outside set_window:\n {}",
10842+ found.join("\n ")
10843+ );
10844+ }
10845+
10723 }10846 }
crates/engine/tests/saveload.rsmodified+130 -0
@@ -1301,3 +1301,133 @@ fn a_group_trim_carries_its_sibling_no_further_than_the_ceiling() {
1301 session.save_project(&saved).expect("save");1301 session.save_project(&saved).expect("save");
1302 PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");1302 PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1303 }1303 }
1304+
1305+/// The composer that no per-arm check finds: a **rejoin** takes the left
1306+/// half's `in_us` and the right half's `out_us`, and `sub_joinable` has just
1307+/// established that the two halves are consecutive in the *source* -- so the
1308+/// joined width is the SUM of two widths that each passed their own door.
1309+///
1310+/// Neither half is illegal and the whole is. Two 20-day windows, one ordinary
1311+/// undo-a-split keystroke, a 40-day window, and a file the editor writes and
1312+/// cannot reopen.
1313+#[test]
1314+fn a_rejoin_of_two_legal_windows_stays_inside_the_ceiling() {
1315+ use engine::project::{MAX_WINDOW_US, SubClip};
1316+
1317+ let dir = scratch("caption_rejoin");
1318+ let media = copy_in(&dir, "test_av.mp4");
1319+ let subs = long_srt(&dir, 960);
1320+ let twenty_days = 20 * 24 * 60 * 60 * 1_000_000i64;
1321+
1322+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1323+ session.import_subtitles(&subs).expect("a 40-day track imports");
1324+ let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1325+ // Two adjacent halves, each a legal 20-day window, the second starting
1326+ // exactly where the first's window ends -- which is what `sub_joinable`
1327+ // requires, and what makes the join a sum.
1328+ for (at, frames, in_us) in [(0u32, 10_000u32, 0i64), (10_000, 10_000, twenty_days)] {
1329+ session
1330+ .place_sub(
1331+ s1,
1332+ at,
1333+ SubClip {
1334+ start: 0,
1335+ frames,
1336+ track: 0,
1337+ in_us,
1338+ out_us: in_us + twenty_days,
1339+ link: None,
1340+ },
1341+ )
1342+ .unwrap_or_else(|e| panic!("a 20-day half at {at} places: {e}"));
1343+ }
1344+ let halves: Vec<i64> = session
1345+ .sub_lane(s1)
1346+ .iter()
1347+ .map(|s| s.out_us - s.in_us)
1348+ .collect();
1349+ assert!(
1350+ halves.iter().all(|&w| w <= MAX_WINDOW_US),
1351+ "both halves must be legal for this to be about the join: {halves:?}"
1352+ );
1353+
1354+ // `regroup_at` is in seconds, like every other front-end door; the
1355+ // boundary is the frame the second half starts on.
1356+ let at_secs = 10_000.0 / f64::from(session.meta().frame_rate);
1357+ assert!(
1358+ session.regroup_at(at_secs),
1359+ "the two halves rejoin -- the inverse of the split that made them"
1360+ );
1361+ let joined = &session.sub_lane(s1)[0];
1362+ assert!(
1363+ (joined.out_us - joined.in_us) <= MAX_WINDOW_US,
1364+ "the rejoin summed two {}us windows into {}us, past the {}us ceiling",
1365+ MAX_WINDOW_US / 2,
1366+ joined.out_us - joined.in_us,
1367+ MAX_WINDOW_US
1368+ );
1369+
1370+ let saved = dir.join("rejoined.edith");
1371+ session.save_project(&saved).expect("save");
1372+ PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1373+}
1374+
1375+/// The other half of the ceiling: it bounds a **difference**, so a head trim
1376+/// grows the width as surely as a tail trim does, and from the other side.
1377+///
1378+/// A caption placed a year into its source with a 30-second window is legal.
1379+/// One head drag pulling its start back by 200 days of frames makes the
1380+/// window 200 days wide while `out_us` never moves -- so bounding `in_us`
1381+/// from above, which is all the head arm's own clamp does, says nothing at all
1382+/// about the ceiling.
1383+#[test]
1384+fn a_head_drag_no_further_than_the_ceiling() {
1385+ use engine::project::{Edge, MAX_WINDOW_US, SubClip};
1386+
1387+ let dir = scratch("caption_head_drag");
1388+ let media = copy_in(&dir, "test_av.mp4");
1389+ let subs = long_srt(&dir, 960);
1390+ let day = 24 * 60 * 60 * 1_000_000i64;
1391+
1392+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1393+ session.import_subtitles(&subs).expect("a 40-day track imports");
1394+ let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1395+ // A year into the source, 30 seconds of window: legal, and the only kind
1396+ // of caption a head drag can make enormous.
1397+ session
1398+ .place_sub(
1399+ s1,
1400+ 90_000_000,
1401+ SubClip {
1402+ start: 0,
1403+ frames: 900,
1404+ track: 0,
1405+ in_us: 365 * day,
1406+ out_us: 365 * day + 30_000_000,
1407+ link: None,
1408+ },
1409+ )
1410+ .expect("a year-deep caption places");
1411+
1412+ // The head room the engine offers, then the drag to its far end: 200 days
1413+ // of source at 30 fps, which the Start arm's own `in_us <= out_us - 1`
1414+ // clamp is perfectly happy with.
1415+ let (lo, _) = session
1416+ .trim_sub_room(s1, 0, Edge::Start)
1417+ .expect("a head room");
1418+ session
1419+ .trim_sub(s1, 0, Edge::Start, lo)
1420+ .expect("the head drag commits");
1421+
1422+ let dragged = session.sub_lane(s1)[0];
1423+ assert!(
1424+ (dragged.out_us - dragged.in_us) <= MAX_WINDOW_US,
1425+ "a head drag onto a year-deep caption gave a {}us window, past the {}us ceiling",
1426+ dragged.out_us - dragged.in_us,
1427+ MAX_WINDOW_US
1428+ );
1429+
1430+ let saved = dir.join("head.edith");
1431+ session.save_project(&saved).expect("save");
1432+ PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1433+}