Skip to content

Commit a5750d6

Browse files
committed
test(cli-rs): run nothing when a save lands before the approved copy
1 parent 128e1b0 commit a5750d6

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎packages/cli-rs/crates/plugin/src/pipe/mod.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ impl Scripts {
658658
self.waiting.borrow().values().all(|waiting| matches!(waiting, Waiting::Ping))
659659
}
660660

661+
/// Holds the copies of approved trees before they read anything while `paused` ([`tree::Trees`]).
662+
#[doc(hidden)]
663+
pub fn pause_stages(&self, paused: bool) {
664+
self.trees.pause_stages(paused);
665+
}
666+
661667
/// Whether a load of `target` was sent and the JS host has not answered it.
662668
#[doc(hidden)]
663669
pub fn loading(&self, target: &str) -> bool {

‎packages/cli-rs/crates/plugin/src/pipe/tree.rs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::collections::HashMap;
1717
use std::hash::{DefaultHasher, Hash, Hasher};
1818
use std::path::{Path, PathBuf};
1919
use std::sync::atomic::{AtomicU64, Ordering};
20-
use std::sync::{Arc, Mutex, MutexGuard};
20+
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
2121

2222
use crate::loader::approval::{self, Artifact};
2323

@@ -31,6 +31,8 @@ pub(crate) struct Trees {
3131
/// The bytes of plugin files the walks and copies read.
3232
hashed: Arc<AtomicU64>,
3333
wake: Arc<dyn Fn() + Send + Sync>,
34+
/// While set, a copy waits before it reads anything ([`Trees::pause_stages`]).
35+
paused: Arc<(Mutex<bool>, Condvar)>,
3436
}
3537

3638
/// Each copy by the tree and its hash: the tree's [`Entry::changes`] when it began, and its result until it is taken.
@@ -59,7 +61,13 @@ struct Entry {
5961
impl Trees {
6062
/// `wake` runs on a walk's thread when a walk found something new, and on a copy's when it ended.
6163
pub(crate) fn new(wake: impl Fn() + Send + Sync + 'static) -> Self {
62-
Self { state: Arc::default(), stages: Arc::default(), hashed: Arc::default(), wake: Arc::new(wake) }
64+
Self {
65+
state: Arc::default(),
66+
stages: Arc::default(),
67+
hashed: Arc::default(),
68+
wake: Arc::new(wake),
69+
paused: Arc::default(),
70+
}
6371
}
6472

6573
/// The build of the global plugin `dir` as the last walk found it, or `None` before the first walk ended.
@@ -101,8 +109,10 @@ impl Trees {
101109
stages.insert(key, (changes, None));
102110
drop(stages);
103111
let (stages, hashed, wake) = (self.stages.clone(), self.hashed.clone(), self.wake.clone());
104-
let (owned, to) = (artifact.clone(), root.to_owned());
112+
let (owned, to, paused) = (artifact.clone(), root.to_owned(), self.paused.clone());
105113
let spawned = std::thread::Builder::new().name("plugin-stage".to_owned()).spawn(move || {
114+
let (flag, resumed) = &*paused;
115+
drop(resumed.wait_while(lock(flag), |paused| *paused).unwrap_or_else(|poisoned| poisoned.into_inner()));
106116
stage(&stages, &hashed, &owned, &to, changes);
107117
wake();
108118
});
@@ -114,6 +124,14 @@ impl Trees {
114124
None
115125
}
116126

127+
/// Holds every copy of an approved tree before it reads anything while `paused`, and lets them go on when not: a
128+
/// test saves a file between the approval and the copy.
129+
pub(crate) fn pause_stages(&self, paused: bool) {
130+
let (flag, resumed) = &*self.paused;
131+
*lock(flag) = paused;
132+
resumed.notify_all();
133+
}
134+
117135
/// The bytes of plugin files the walks and copies read so far.
118136
pub(crate) fn hashed(&self) -> u64 {
119137
self.hashed.load(Ordering::Relaxed)

‎packages/cli-rs/crates/tui/tests/all/plugin_compat.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,36 @@ fn a_save_after_approval_runs_nothing_unapproved() {
534534
assert_eq!(listed(&pipe, &dir, PROBE), Some(Status::Pending));
535535
}
536536

537+
// Expected (bigboi P8-1, js-followups): a save after the approval but before the approved files are copied, which
538+
// happens off the UI thread, runs nothing: the copy is refused as the files changed since they were approved, the
539+
// approved module never runs, and the saved build waits for a decision of its own; approved, it runs. This is the race
540+
// `a_save_after_approval_runs_nothing_unapproved` lost under load before it waited for the load, made certain by
541+
// holding the copy until the save is made.
542+
#[test]
543+
fn a_save_before_the_approved_copy_runs_nothing() {
544+
let Some(config) = config() else { return };
545+
let (pipe, dir, marker) = project(&[], config);
546+
let tui = &pipe.harness.tui;
547+
let target = dir.to_string_lossy().into_owned();
548+
let Some((Ok(shown), _)) = opencode_tui::plugins::host::held(tui, &target) else { panic!("nothing is held") };
549+
pipe.scripts().pause_stages(true);
550+
assert_eq!(crate::plugin_pipe::decide(&pipe.harness, &target, &shown, Decision::Approved), Ok(true));
551+
let file = dir.join("tui.tsx");
552+
let source = std::fs::read_to_string(&file).unwrap();
553+
std::fs::write(&file, source.replace("const MODULE = true", "const MODULE = \"saved\"")).unwrap();
554+
pipe.scripts().pause_stages(false);
555+
pipe.harness.wait("the saved build is held", || {
556+
pipe.settle();
557+
matches!(opencode_tui::plugins::host::held(tui, &target), Some((Ok(held), _)) if held.sha256 != shown.sha256)
558+
});
559+
assert_eq!(std::fs::read_to_string(&marker).unwrap_or_default(), "", "a module ran before its approval");
560+
assert_eq!(listed(&pipe, &dir, PROBE), Some(Status::Pending));
561+
approve(&pipe, &dir);
562+
pipe.active(PROBE);
563+
let ran = std::fs::read_to_string(&marker).unwrap();
564+
assert_eq!(ran.lines().collect::<Vec<_>>(), [r#"{"module":"saved"}"#]);
565+
}
566+
537567
/// The status of the project plugin `dir`: listed by its directory until it loads, then by `id`.
538568
fn listed(pipe: &Pipe, dir: &Path, id: &str) -> Option<Status> {
539569
let target = dir.to_string_lossy();

0 commit comments

Comments
 (0)