Luxe 0.1.2: Windows fixes, working compaction, and compaction progress in the dock

This commit is contained in:
Evan Whittaker
2026-10-18 15:04:51 -05:00
parent 4ed87ee4e4
commit 33b87c78a8
69 changed files with 1470 additions and 424 deletions

View File

@@ -437,9 +437,11 @@ mod tree_teardown {
}
/// A shell that backgrounds a grandchild and waits on it. Killing the shell alone leaves the
/// grandchild touching `marker` — which is how a survivor makes itself known.
/// grandchild touching `marker` — which is how a survivor makes itself known. Forward slashes,
/// which Git Bash takes on Windows, where a backslash would be an escape.
fn script(marker: &std::path::Path) -> String {
format!("(for _ in $(seq 1 40); do touch {}; sleep 0.25; done) & wait", marker.display())
let marker = marker.display().to_string().replace('\\', "/");
format!("(for _ in $(seq 1 40); do touch '{marker}'; sleep 0.25; done) & wait")
}
/// Whether anything is still touching `marker`, the loop, once a touch that was already under
@@ -459,8 +461,9 @@ mod tree_teardown {
#[tokio::test]
async fn a_dropped_call_takes_the_whole_tree_with_it() {
if !cfg!(unix) {
return; // the script is a POSIX one
// The script is a POSIX one: Git Bash runs it on Windows, where the tree is a job's
if !luxe_core::program::has_posix_shell() {
return;
}
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("alive");
@@ -480,7 +483,7 @@ mod tree_teardown {
#[tokio::test]
async fn a_timed_out_call_takes_the_whole_tree_with_it() {
if !cfg!(unix) {
if !luxe_core::program::has_posix_shell() {
return;
}
let dir = tempfile::tempdir().expect("tempdir");

View File

@@ -510,14 +510,17 @@ async fn under_a_terminal() {
/// A suspended command could never finish, and the old advice was to give it longer. A terminal
/// can no longer suspend one (see `a_command_that_touches_the_terminal_still_finishes`), so this
/// stops one by hand.
///
/// The stop is sent a few times over the first quarter second: on macOS a `SIGSTOP` sent the instant
/// a child is spawned can miss it, and the report then rightly lists a sleeper, not a stopped one.
#[cfg(unix)]
#[tokio::test]
async fn a_suspended_command_is_called_stuck_not_slow() {
let tmp_dir = tempfile::tempdir().unwrap();
let ctx = test_context(tmp_dir.path());
let result = BashTool::new()
.run(serde_json::json!({"command": "sleep 9 & kill -STOP $!; sleep 8", "timeout": 1}), &ctx)
.await;
let command = "sleep 9 & for _ in 1 2 3 4 5; do kill -STOP $!; sleep 0.05; done; sleep 8";
let result =
BashTool::new().run(serde_json::json!({"command": command, "timeout": 1}), &ctx).await;
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
assert!(text.contains("State T means suspended, not slow"), "{text}");
assert!(!text.contains("pass `timeout`"), "more time cannot help a suspended command: {text}");

View File

@@ -374,7 +374,7 @@ impl Tool for EditTool {
Err(problem) => return ToolResultData::error(problem.message()),
};
let full_path = cx.cwd.join(&request.path);
let full_path = cx.cwd.join(&*luxe_core::paths::native(&request.path));
if let Err(refusal) = self.check_read_guard(cx, &full_path, &request.path) {
return refusal;

View File

@@ -76,10 +76,22 @@ fn shell_command(command: &str, cwd: &Path) -> Command {
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
if let Some(value) = python_utf8(std::env::var_os("PYTHONUTF8").as_deref(), cfg!(windows)) {
child.env("PYTHONUTF8", value);
}
luxe_core::program::detach_from_terminal(&mut child);
child
}
/// The `PYTHONUTF8` a command gets, when Luxe sets one: on Windows, unless the user chose.
///
/// Python there writes to a pipe in the console's code page, cp1252, so a script printing `✓`
/// fails with `UnicodeEncodeError` under Luxe and works in the user's terminal. UTF-8 mode is what
/// Python does everywhere else.
fn python_utf8(inherited: Option<&std::ffi::OsStr>, windows: bool) -> Option<&'static str> {
(windows && inherited.is_none()).then_some("1")
}
/// Whether commands here run through a POSIX shell, which decides how an argument is quoted.
pub(crate) fn posix_shell() -> bool {
luxe_core::program::shell().1 == "-c"
@@ -106,7 +118,7 @@ pub(crate) fn quote_argument(value: &str, posix: bool) -> String {
/// Runs `command` in `cwd` until it exits or `timeout` passes.
///
/// Every ending but a clean exit takes the whole tree with it, including the ones nobody writes
/// code for: a cancel drops this future, and [`KillTreeOnDrop`] is what answers that.
/// code for: a cancel drops this future, and a [`luxe_core::program::TreeGuard`] answers that.
pub(crate) async fn run_captured(
command: &str,
cwd: &Path,
@@ -115,8 +127,10 @@ pub(crate) async fn run_captured(
let mut child = shell_command(command, cwd).spawn()?;
let pid = child.id().unwrap_or(0);
// Armed from here to the first normal exit: every early return, and every drop this function
// never sees, takes the whole tree with it.
let mut reaper = KillTreeOnDrop::arm(pid);
// never sees, takes the whole tree with it. A cancel ends the call by dropping this future, so
// there is no code path to hang an explicit kill on: pressing Cancel on a `cargo build` left
// the build running, holding its locks and its CPU, with nothing left watching it.
let mut reaper = luxe_core::program::TreeGuard::arm(&child);
let pipes = Pipes::attach(&mut child);
let (stdout, stderr) = pipes.sinks();
// Not `tokio::time::timeout`: that drops the wait on expiry, and with it the shell
@@ -141,35 +155,6 @@ pub(crate) async fn run_captured(
}
}
/// Kills the command's whole process group unless the command finished on its own.
///
/// `kill_on_drop` reaps the direct child — the shell — and orphans everything the shell started.
/// A **cancel** ends the call by *dropping* its future, so there is no code path to hang an
/// explicit kill on: pressing Cancel on a `cargo build` left the build running, holding its locks
/// and its CPU, with nothing left watching it.
///
/// Disarmed once the child has been reaped, because a reaped pid can be reused and killing a
/// *group* by a recycled id is the one mistake worse than leaking one.
struct KillTreeOnDrop(Option<u32>);
impl KillTreeOnDrop {
fn arm(pid: u32) -> Self {
Self((pid != 0).then_some(pid))
}
fn disarm(&mut self) {
self.0 = None;
}
}
impl Drop for KillTreeOnDrop {
fn drop(&mut self) {
if let Some(pid) = self.0 {
luxe_core::program::kill_tree(pid);
}
}
}
/// One stream's bounded capture: its first bytes, its last bytes, and a count of what fell between.
#[derive(Default)]
pub(crate) struct Capture {

View File

@@ -1,5 +1,22 @@
use super::*;
/// Windows Python writes cp1252 into a pipe, so a `✓` in a test's output crashed it under Luxe.
#[test]
fn windows_commands_get_python_utf8_mode() {
assert_eq!(python_utf8(None, true), Some("1"));
}
#[test]
fn a_python_utf8_the_user_set_is_kept() {
assert_eq!(python_utf8(Some(std::ffi::OsStr::new("0")), true), None);
}
/// Elsewhere Python is UTF-8 already, and the environment is left as the user has it.
#[test]
fn unix_commands_get_no_python_setting() {
assert_eq!(python_utf8(None, false), None);
}
/// However much a stream prints, a capture holds its head and its tail and counts the rest.
#[test]
fn a_capture_holds_its_bound_and_counts_what_fell_between() {

View File

@@ -186,8 +186,7 @@ impl ReadTool {
limit: Option<usize>,
cx: &ToolContext,
) -> ToolResultData {
// Resolve path relative to cwd.
let full_path = cx.cwd.join(path_str);
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
debug!(path = %full_path.display(), offset, limit = limit.unwrap_or(usize::MAX), "reading file");

View File

@@ -161,8 +161,7 @@ impl Tool for WriteTool {
}
};
// Resolve path relative to cwd.
let full_path = cx.cwd.join(path_str);
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
if let Err(refusal) = self.check_blind_overwrite(cx, &full_path, path_str) {
return refusal;