Make the window guard match how a composer actually writes
Browse files at this commitedc51b566ff86b8b7ac5b60588c1592cfed89bb5Parent: eb3f571- fix
- make the window guard match how a composer actually writes
1 changed file +236 -54
- crates/engine/src/project.rs +236 -54
crates/engine/src/project.rsmodified+236 -54
| @@ -10767,74 +10767,178 @@ fn removing_a_subtitle_column_undoes_with_its_reindex() { | |||
| 10767 | 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"); |
| 10768 | } | 10768 | } |
| 10769 | 10769 | ||
| 10770 | - /// The closed set, proved by reading the files rather than by exercising | 10770 | + /// Finds every line that **writes** a caption's clock, in three forms: an |
| 10771 | - /// every path: a caption's `in_us`/`out_us` is written in exactly two | 10771 | + /// assignment through any operator (`.out_us = `, `.out_us += `, |
| 10772 | - /// places, and this fails if a third appears. | 10772 | + /// `.out_us <<= `), a struct-literal field (`out_us: ...`) and the |
| 10773 | + /// shorthand form (`out_us,`). A `let` binding is skipped for the literal | ||
| 10774 | + /// forms only, so `let x: &mut SubClip = ..; (*x).in_us = 5;` is still | ||
| 10775 | + /// caught while `let out_us = s.out_us;` is not. | ||
| 10776 | + /// | ||
| 10777 | + /// The operator list is `=` plus one assignment operator each, and | ||
| 10778 | + /// deliberately excludes `<=`: the first version of this matcher counted | ||
| 10779 | + /// `s.out_us <= s.in_us` as a write, which would have flagged every | ||
| 10780 | + /// never-empty check in the file. | ||
| 10781 | + fn window_writes(text: &str) -> Vec<(usize, String)> { | ||
| 10782 | + const FIELDS: [&str; 2] = ["in_us", "out_us"]; | ||
| 10783 | + const OPS: [&str; 11] = [ | ||
| 10784 | + "=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", "<<=", ">>=", | ||
| 10785 | + ]; | ||
| 10786 | + let mut found = Vec::new(); | ||
| 10787 | + for (n, raw) in text.lines().enumerate() { | ||
| 10788 | + let line = raw.trim(); | ||
| 10789 | + if line.starts_with("//") { | ||
| 10790 | + continue; | ||
| 10791 | + } | ||
| 10792 | + let is_binding = line.starts_with("let "); | ||
| 10793 | + for field in FIELDS { | ||
| 10794 | + let access = format!(".{field}"); | ||
| 10795 | + if let Some(at) = line.find(&access) { | ||
| 10796 | + let after = &line[at + access.len()..]; | ||
| 10797 | + let after = after.trim_start(); | ||
| 10798 | + if OPS.iter().any(|op| { | ||
| 10799 | + after.starts_with(op) && !after[op.len()..].starts_with('=') | ||
| 10800 | + }) { | ||
| 10801 | + found.push((n + 1, line.to_owned())); | ||
| 10802 | + continue; | ||
| 10803 | + } | ||
| 10804 | + } | ||
| 10805 | + // A bare name: a literal field, a shorthand, or a binding. | ||
| 10806 | + // Skipped when a `.` or an identifier character precedes it -- | ||
| 10807 | + // that is a read, not a field being set. | ||
| 10808 | + if is_binding { | ||
| 10809 | + continue; | ||
| 10810 | + } | ||
| 10811 | + for (at, _) in line.match_indices(field) { | ||
| 10812 | + let preceded_by_name = line[..at] | ||
| 10813 | + .chars() | ||
| 10814 | + .next_back() | ||
| 10815 | + .is_some_and(|c| c == '.' || c.is_alphanumeric() || c == '_'); | ||
| 10816 | + if preceded_by_name { | ||
| 10817 | + continue; | ||
| 10818 | + } | ||
| 10819 | + let after = line[at + field.len()..].trim_start(); | ||
| 10820 | + if after.starts_with(':') || after.starts_with(',') || after.starts_with('=') { | ||
| 10821 | + found.push((n + 1, line.to_owned())); | ||
| 10822 | + break; | ||
| 10823 | + } | ||
| 10824 | + } | ||
| 10825 | + } | ||
| 10826 | + } | ||
| 10827 | + found | ||
| 10828 | + } | ||
| 10829 | + | ||
| 10830 | + /// A **heuristic guard**, not a proof. The honest reading of a green here | ||
| 10831 | + /// is the residual risk in this comment, not the green itself. | ||
| 10773 | /// | 10832 | /// |
| 10774 | /// Three rounds of review each found one more composer a per-arm check had | 10833 | /// 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 | 10834 | + /// missed -- a tail trim, a head trim, a rejoin that SUMS two legal |
| 10776 | - /// -- and each produced the same failure: the editor wrote a `.edith` its | 10835 | + /// widths -- and each produced the same failure: the editor wrote a |
| 10777 | - /// own loader then refused, with no in-app way back. Reasoning about arms | 10836 | + /// `.edith` its own loader then refused, with no in-app way back. The |
| 10778 | - /// does not scale to an unknown number of them, so the invariant is stated | 10837 | + /// invariant is now structural in the code (every composer calls |
| 10779 | - /// structurally instead: a new composer routed through `set_window` is two | 10838 | + /// [`set_window`]); this is what keeps that structure from quietly losing |
| 10780 | - /// lines and needs no new argument, and one that is not fails here. | 10839 | + /// a caller. |
| 10840 | + /// | ||
| 10841 | + /// **Why this is a scan and not the compiler**, which is the better answer | ||
| 10842 | + /// and was priced rather than skipped: making `in_us`/`out_us` private to | ||
| 10843 | + /// this module turns every outside write into a compile error, which no | ||
| 10844 | + /// one-token edit can defeat. It costs 74 `SubClip` construction sites | ||
| 10845 | + /// (31 of them in this crate's own integration tests) plus a public | ||
| 10846 | + /// constructor, and it reaches `crates/app`'s `timeline_edit.rs` and | ||
| 10847 | + /// `timeline_math.rs`, which other lanes own. That is a refactor of a | ||
| 10848 | + /// door's public shape rather than a bound, and the wrong size for this | ||
| 10849 | + /// lane -- so the guard stands here with its limits written down instead. | ||
| 10781 | /// | 10850 | /// |
| 10782 | - /// The two allowed writers are `set_window` (every composer) and the | 10851 | + /// Residual risk, concretely: a write through a helper taking |
| 10783 | - /// parser's own door, which builds the `SubClip` a `sub` line describes and | 10852 | + /// `&mut SubClip` that assigns by a form the matcher cannot see; a |
| 10784 | - /// range-checks it on the next lines -- a door, not a composer, because a | 10853 | + /// macro-mediated write; a `.rs` under `crates/` the walk does not reach; |
| 10785 | - /// file is not an edit anybody is watching happen. | 10854 | + /// and -- the sharpest one -- renaming the fields themselves, since the |
| 10855 | + /// matcher keys on the literal strings `in_us`/`out_us` and would then | ||
| 10856 | + /// match nothing at all while still passing. | ||
| 10786 | #[test] | 10857 | #[test] |
| 10787 | - fn every_window_write_goes_through_set_window() { | 10858 | + fn every_window_write_outside_set_window_is_named() { |
| 10788 | // `CARGO_MANIFEST_DIR`, not `file!()`: a test runs with the WORKSPACE | 10859 | // `CARGO_MANIFEST_DIR`, not `file!()`: a test runs with the WORKSPACE |
| 10789 | // root as its working directory, so `file!()`'s crate-relative path | 10860 | // root as its working directory, so `file!()`'s crate-relative path |
| 10790 | // does not resolve from where the binary actually starts. | 10861 | // does not resolve from where the binary actually starts. |
| 10791 | - let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); | 10862 | + let workspace = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 10792 | - // Every line in the two files that names a clock and is not a write: | 10863 | + .parent() |
| 10793 | - // the struct's own fields, the one function that may write, and the | 10864 | + .and_then(std::path::Path::parent) |
| 10794 | - // parser's two door lines. A new entry here is a claim someone has to | 10865 | + .expect("crates/engine sits two levels under the workspace root") |
| 10795 | - // defend in review, which is the point of writing the list out. | 10866 | + .to_path_buf(); |
| 10867 | + // Every line in the workspace that names a clock and is not a | ||
| 10868 | + // composer, each with its reason. A new entry is a claim someone has | ||
| 10869 | + // to defend in review, which is the point of writing the list out. | ||
| 10796 | let allowed = [ | 10870 | let allowed = [ |
| 10797 | - ("project.rs", "pub in_us: i64,"), | 10871 | + ("pub in_us: i64,", "the field declaration"), |
| 10798 | - ("project.rs", "pub out_us: i64,"), | 10872 | + ("pub out_us: i64,", "the field declaration"), |
| 10799 | - ("project.rs", "fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) {"), | 10873 | + ( |
| 10800 | - ("project.rs", "s.in_us = in_us;"), | 10874 | + "fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) {", |
| 10801 | - ("project.rs", "s.out_us = out_us;"), | 10875 | + "set_window's own signature", |
| 10802 | - ("edith.rs", "in_us: micros(f[3], n)?,"), | 10876 | + ), |
| 10803 | - ("edith.rs", "out_us: micros(f[4], n)?,"), | 10877 | + ("s.in_us = in_us;", "set_window"), |
| 10878 | + ("s.out_us = out_us;", "set_window"), | ||
| 10879 | + ( | ||
| 10880 | + "set_window(&mut data.subs[idx], in_us, out_us);", | ||
| 10881 | + "regroup naming the two values it is about to set", | ||
| 10882 | + ), | ||
| 10883 | + ("in_us: micros(f[3], n)?,", "edith.rs: the parse door"), | ||
| 10884 | + ("out_us: micros(f[4], n)?,", "edith.rs: the parse door"), | ||
| 10885 | + // Non-test helpers that build a value to hand `place_sub`, which is | ||
| 10886 | + // a door. Not composers: nobody was mid-gesture, which is exactly | ||
| 10887 | + // why a door refuses where a composer clamps. | ||
| 10888 | + ( | ||
| 10889 | + "in_us: 0,", | ||
| 10890 | + "sweep.rs caption helper, and timeline_edit.rs's palette row, -> place_sub", | ||
| 10891 | + ), | ||
| 10892 | + ( | ||
| 10893 | + "out_us: i64::from(frames) * 1_000_000,", | ||
| 10894 | + "sweep.rs caption helper -> place_sub", | ||
| 10895 | + ), | ||
| 10896 | + ( | ||
| 10897 | + "frames: frames_of_us(out_us, self.fps),", | ||
| 10898 | + "timeline_edit.rs's palette row -> place_sub", | ||
| 10899 | + ), | ||
| 10900 | + ("out_us,", "timeline_edit.rs's palette row shorthand -> place_sub"), | ||
| 10804 | ]; | 10901 | ]; |
| 10902 | + | ||
| 10805 | let mut found: Vec<String> = Vec::new(); | 10903 | let mut found: Vec<String> = Vec::new(); |
| 10806 | - let mut scanned = 0usize; | 10904 | + let (mut files, mut lines) = (0usize, 0usize); |
| 10807 | - for name in ["project.rs", "edith.rs"] { | 10905 | + let mut seen_app = false; |
| 10808 | - let text = std::fs::read_to_string(dir.join(name)).expect("read the file"); | 10906 | + for (file, text) in read_workspace_rust(&workspace) { |
| 10809 | - // The test MODULE, not any `cfg(test)`: this file carries several | 10907 | + // Test code, by path or by module: a test builds `SubClip`s by hand |
| 10810 | - // `cfg(test)` attributes on single test-only helpers, and two wrong | 10908 | + // and its value is not one the loader ever reads, so the 31 sites in |
| 10811 | - // boundaries were tried before this one -- the FIRST (line 160) | 10909 | + // `crates/engine/tests/` and three in `crates/app/src/tests/` are |
| 10812 | - // scanned 160 lines of a 10,800-line file, and the LAST landed | 10910 | + // not composers. A real limit rather than a technicality -- the |
| 10813 | - // inside this very test, dragging the test module's own | 10911 | + // guard does not cover a writer hidden inside a test -- but a test |
| 10814 | - // hand-built `SubClip`s into scope. A guard that reads nothing, | 10912 | + // cannot produce a file a user loses, so it is where the line goes. |
| 10815 | - // and one that reads the wrong thing, are both worse than no | 10913 | + let path = file.to_string_lossy().replace('\\', "/"); |
| 10816 | - // guard because they read as one. The line count asserted below is | 10914 | + if path.contains("/tests/") { |
| 10817 | - // what keeps this third boundary honest. | 10915 | + continue; |
| 10818 | - let at = text | 10916 | + } |
| 10819 | - .find("#[cfg(test)]\nmod tests {") | 10917 | + let code = match text.find("\n#[cfg(test)]\nmod tests {") { |
| 10820 | - .expect("this file's test module is still spelled that way"); | 10918 | + Some(at) => &text[..at], |
| 10821 | - let code = &text[..at]; | 10919 | + None => text.as_str(), |
| 10822 | - for line in code.lines() { | 10920 | + }; |
| 10823 | - scanned += 1; | 10921 | + lines += code.lines().count(); |
| 10824 | - let line = line.trim(); | 10922 | + files += 1; |
| 10825 | - let writes = line.contains(".in_us = ") | 10923 | + seen_app |= path.contains("crates/app/"); |
| 10826 | - || line.contains(".out_us = ") | 10924 | + for (n, line) in window_writes(code) { |
| 10827 | - || line.contains("in_us:") | 10925 | + if !allowed.iter().any(|(text, _)| *text == line) { |
| 10828 | - || line.contains("out_us:"); | 10926 | + let at = file.strip_prefix(&workspace).unwrap_or(&file); |
| 10829 | - if writes && !allowed.contains(&(name, line)) { | 10927 | + found.push(format!("{}:{n}: {line}", at.display())); |
| 10830 | - found.push(format!("{name}: {line}")); | ||
| 10831 | } | 10928 | } |
| 10832 | } | 10929 | } |
| 10833 | } | 10930 | } |
| 10931 | + // A floor, not a census: the walk filters to files that can NAME | ||
| 10932 | + // `SubClip`, which today is 20 of them and ~39,000 lines. The point is | ||
| 10933 | + // only to catch a walk rooted in the wrong place, which reads a file | ||
| 10934 | + // or two. The `seen_app` half is the one that matters: the two-file | ||
| 10935 | + // version of this guard read `project.rs` and `edith.rs` and was | ||
| 10936 | + // silently certifying a claim about the app it had never opened. | ||
| 10834 | assert!( | 10937 | assert!( |
| 10835 | - scanned > 3_000, | 10938 | + lines > 20_000 && files >= 10 && seen_app, |
| 10836 | - "the scan only read {scanned} lines -- the cfg(test) boundary is wrong, \ | 10939 | + "the walk read {lines} lines in {files} files (app reached: \ |
| 10837 | - and a guard that reads nothing is worse than no guard" | 10940 | + {seen_app}) -- it is not covering the workspace, and a guard that \ |
| 10941 | + reads nothing is worse than no guard because it reads as one" | ||
| 10838 | ); | 10942 | ); |
| 10839 | assert!( | 10943 | assert!( |
| 10840 | found.is_empty(), | 10944 | found.is_empty(), |
| @@ -10843,4 +10947,82 @@ fn removing_a_subtitle_column_undoes_with_its_reindex() { | |||
| 10843 | ); | 10947 | ); |
| 10844 | } | 10948 | } |
| 10845 | 10949 | ||
| 10950 | + /// The blind spot, pinned as a case. Every one of these is a write, and | ||
| 10951 | + /// the guard exists because its first two versions caught none of them -- | ||
| 10952 | + /// `s.out_us += by(...)` being a one-token edit that made it green while a | ||
| 10953 | + /// grouped caption really did grow past the ceiling. A matcher that has | ||
| 10954 | + /// quietly stopped matching is the failure mode that looks most like a | ||
| 10955 | + /// pass, so it gets its own test rather than a comment. | ||
| 10956 | + #[test] | ||
| 10957 | + fn the_window_write_matcher_catches_the_ways_a_composer_hides() { | ||
| 10958 | + for (what, code) in [ | ||
| 10959 | + ( | ||
| 10960 | + "the one-token compound assignment", | ||
| 10961 | + " s.out_us += by(s.end(), to);", | ||
| 10962 | + ), | ||
| 10963 | + ("a plain compound assignment", " s.in_us -= 1;"), | ||
| 10964 | + ("a shift-assign", " s.out_us <<= 1;"), | ||
| 10965 | + ( | ||
| 10966 | + "a write through a &mut the matcher cannot see past", | ||
| 10967 | + " let x: &mut SubClip = q; (*x).in_us = 5;", | ||
| 10968 | + ), | ||
| 10969 | + ( | ||
| 10970 | + "a struct literal", | ||
| 10971 | + " out.push(SubClip { in_us: 0, out_us: 1, ..s });", | ||
| 10972 | + ), | ||
| 10973 | + ("the shorthand form", " SubClip { in_us, out_us, ..s }"), | ||
| 10974 | + ] { | ||
| 10975 | + assert!( | ||
| 10976 | + !window_writes(code).is_empty(), | ||
| 10977 | + "the matcher misses {what}: {code:?}" | ||
| 10978 | + ); | ||
| 10979 | + } | ||
| 10980 | + // ...and the other side, so the allowlist is not padded with reads. | ||
| 10981 | + for quiet in [ | ||
| 10982 | + " if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 {", | ||
| 10983 | + " debug_assert!(s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0);", | ||
| 10984 | + " let out_us = s.out_us;", | ||
| 10985 | + " (next.start == frame && next.in_us == subs[idx].out_us)", | ||
| 10986 | + " let total = s.out_us - s.in_us;", | ||
| 10987 | + ] { | ||
| 10988 | + assert!( | ||
| 10989 | + window_writes(quiet).is_empty(), | ||
| 10990 | + "the matcher flags a read as a write: {quiet:?}" | ||
| 10991 | + ); | ||
| 10992 | + } | ||
| 10993 | + } | ||
| 10994 | + | ||
| 10995 | + /// Every `.rs` under the workspace, paired with its text. Recursive | ||
| 10996 | + /// because the set of files that can name `SubClip` is a fact about the | ||
| 10997 | + /// tree, and a hardcoded pair of names goes stale the moment a lane adds a | ||
| 10998 | + /// module -- which is how the two-file version of this guard came to | ||
| 10999 | + /// certify a claim about `crates/app` it had never read. | ||
| 11000 | + fn read_workspace_rust(root: &std::path::Path) -> Vec<(std::path::PathBuf, String)> { | ||
| 11001 | + let mut out = Vec::new(); | ||
| 11002 | + let mut stack = vec![root.to_path_buf()]; | ||
| 11003 | + while let Some(dir) = stack.pop() { | ||
| 11004 | + let Ok(entries) = std::fs::read_dir(&dir) else { | ||
| 11005 | + continue; | ||
| 11006 | + }; | ||
| 11007 | + for entry in entries.flatten() { | ||
| 11008 | + let path = entry.path(); | ||
| 11009 | + if path.is_dir() { | ||
| 11010 | + // `target` is not source and `vendor` is not ours to rule on. | ||
| 11011 | + let skip = path | ||
| 11012 | + .file_name() | ||
| 11013 | + .is_some_and(|n| n == "target" || n == ".git" || n == "vendor"); | ||
| 11014 | + if !skip { | ||
| 11015 | + stack.push(path); | ||
| 11016 | + } | ||
| 11017 | + } else if path.extension().is_some_and(|e| e == "rs") | ||
| 11018 | + && let Ok(text) = std::fs::read_to_string(&path) | ||
| 11019 | + && text.contains("SubClip") | ||
| 11020 | + { | ||
| 11021 | + out.push((path, text)); | ||
| 11022 | + } | ||
| 11023 | + } | ||
| 11024 | + } | ||
| 11025 | + out | ||
| 11026 | + } | ||
| 11027 | + | ||
| 10846 | } | 11028 | } |