Initial public release

Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
Evan Whittaker
2026-10-17 12:08:30 -05:00
commit 09a7de2520
1730 changed files with 412952 additions and 0 deletions

View File

@@ -0,0 +1,257 @@
//! Guarded structural search — approve concrete read paths before invoking the matcher.
//!
//! The matcher gets the approved files themselves, never a directory it could read past the
//! policy in, in batches under the platform's command-line limit. It runs in the session's working
//! directory, the one the walk listed them from, so they go as the walk wrote them.
use super::*;
use crate::walk::{StopOnDrop, Walked};
use luxe_core::permission::PermissionAction;
use luxe_core::tool::FileReadHook;
use std::path::Path;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
/// Bytes of path arguments one invocation carries. Windows caps a whole command line at 32K
/// characters; unix allows far more, and fewer batches mean fewer process starts.
#[cfg(windows)]
const ARGUMENT_BUDGET: usize = 24 * 1024;
#[cfg(not(windows))]
const ARGUMENT_BUDGET: usize = 256 * 1024;
pub(super) enum Call {
Search(AstGrepSearchParams),
Replace(AstGrepReplaceParams),
}
impl Call {
pub(super) fn parse(params: serde_json::Value) -> Result<Self, String> {
let action = params.get("action").and_then(|value| value.as_str()).unwrap_or("search");
match action {
"search" => serde_json::from_value(params).map(Self::Search),
"replace" => serde_json::from_value(params).map(Self::Replace),
other => return Err(format!("Unknown action: {other}")),
}
.map_err(|error| format!("Invalid params: {error}"))
}
fn roots(&self) -> Vec<String> {
let paths = match self {
Self::Search(search) => &search.paths,
Self::Replace(replace) => &replace.paths,
};
if paths.is_empty() {
vec![".".into()]
} else {
paths.clone()
}
}
/// Run the matcher once per batch and report every batch's matches as one result.
pub(super) async fn over(
mut self,
tool: &AstGrepTool,
batches: Vec<Vec<String>>,
cx: &ToolContext,
) -> luxe_core::ToolResultData {
let mut matches = Vec::new();
for batch in batches {
let found = match &mut self {
Self::Search(search) => {
search.paths = batch;
AstGrepTool::search(search, &cx.cwd).await
}
Self::Replace(replace) => {
replace.paths = batch;
AstGrepTool::replace(replace, &cx.cwd).await
}
};
if let Err(error) = found.map(|found| matches.extend(found)) {
return self.failed_after(tool, matches, error, cx).await;
}
}
self.outcome(tool, Ok(matches), cx).await
}
/// A later batch failed: an apply says what the earlier batches already rewrote.
async fn failed_after(
&self,
tool: &AstGrepTool,
matches: Vec<AstGrepMatch>,
error: AstGrepError,
cx: &ToolContext,
) -> luxe_core::ToolResultData {
match self {
Self::Replace(replace) if replace.apply && !matches.is_empty() => {
let mut result = super::render::finish_replace(tool, Ok(matches), true, cx).await;
result.content.push(luxe_core::ContentBlock::Text(format!(
"then ast-grep replace error: {error}; the rewrites above are on disk"
)));
result.is_error = true;
result
}
_ => self.outcome(tool, Err(error), cx).await,
}
}
async fn outcome(
&self,
tool: &AstGrepTool,
found: Result<Vec<AstGrepMatch>, AstGrepError>,
cx: &ToolContext,
) -> luxe_core::ToolResultData {
match self {
Self::Search(_) => search_outcome(found),
Self::Replace(replace) => {
super::render::finish_replace(tool, found, replace.apply, cx).await
}
}
}
}
/// Split `paths` into runs whose argument bytes stay within `budget`; one oversized path still
/// gets a batch of its own rather than being dropped.
fn batches(paths: Vec<String>, budget: usize) -> Vec<Vec<String>> {
let (mut batches, mut current, mut used) = (Vec::new(), Vec::new(), 0);
for path in paths {
let cost = path.len() + 1;
if !current.is_empty() && used + cost > budget {
batches.push(std::mem::take(&mut current));
used = 0;
}
used += cost;
current.push(path);
}
if !current.is_empty() {
batches.push(current);
}
batches
}
async fn candidates(
cx: &ToolContext,
roots: Vec<String>,
stopped: Arc<AtomicBool>,
) -> Result<Walked, String> {
let cwd = cx.cwd.clone();
let cancelled = cx.cancelled.clone();
tokio::task::spawn_blocking(move || {
let mut all = Walked { files: Vec::new(), capped: false };
for root in roots {
let stop_walk = stopped.clone();
let cancel_walk = cancelled.clone();
let walked = crate::walk::files_until(&cwd, Path::new(&root), false, None, move || {
stop_walk.load(Ordering::Relaxed) || *cancel_walk.borrow()
});
all.files.extend(walked.files);
all.capped |= walked.capped;
if stopped.load(Ordering::Relaxed) || *cancelled.borrow() {
break;
}
}
all.files.sort();
all.files.dedup();
all
})
.await
.map_err(|error| format!("ast-grep candidate walk failed: {error}"))
}
struct ReadPlan {
allowed: Vec<String>,
denied: usize,
asks: usize,
}
fn classify(files: Vec<PathBuf>, hook: &dyn FileReadHook) -> ReadPlan {
let mut plan = ReadPlan { allowed: Vec::new(), denied: 0, asks: 0 };
for file in files {
match hook.decision(&file) {
PermissionAction::Allow => plan.allowed.push(luxe_core::paths::display(&file)),
PermissionAction::Deny => plan.denied += 1,
PermissionAction::Ask => plan.asks += 1,
}
}
plan
}
pub(super) async fn run(
tool: &AstGrepTool,
params: serde_json::Value,
cx: &ToolContext,
hook: &dyn FileReadHook,
) -> luxe_core::ToolResultData {
let call = match Call::parse(params) {
Ok(call) => call,
Err(error) => return ToolResultData::error(error),
};
let stopped = Arc::new(AtomicBool::new(false));
let _stop_on_drop = StopOnDrop(stopped.clone());
let walked = match candidates(cx, call.roots(), stopped).await {
Ok(walked) => walked,
Err(error) => return ToolResultData::error(error),
};
let plan = classify(walked.files, hook);
let mut result = if plan.allowed.is_empty() {
ToolResultData::text("no policy-approved files to search")
} else {
call.over(tool, batches(plan.allowed, ARGUMENT_BUDGET), cx).await
};
if plan.denied + plan.asks > 0 {
result.content.push(luxe_core::ContentBlock::Text(format!(
"skipped {} files denied by read policy; {} files require approval and were not read",
plan.denied, plan.asks
)));
}
if walked.capped {
result.content.push(luxe_core::ContentBlock::Text(crate::walk::capped_notice()));
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use luxe_core::permission::{PermissionEngine, PermissionMode, Rule};
/// Candidate classification must preserve Ask and user denials even when default guards opt out.
#[test]
fn candidate_reads_honor_yolo_denials_and_approval_requirements() {
let policy = PermissionEngine::assemble(
PermissionMode::Yolo,
vec![
Rule {
permission: "read".into(),
pattern: "blocked.rs".into(),
action: PermissionAction::Deny,
},
Rule {
permission: "read".into(),
pattern: "review.rs".into(),
action: PermissionAction::Ask,
},
],
vec![],
false,
);
let files = [".env", "blocked.rs", "review.rs"].map(PathBuf::from).to_vec();
let plan = classify(files, &policy);
assert_eq!(plan.allowed, [".env"]);
assert_eq!(plan.denied, 1);
assert_eq!(plan.asks, 1);
}
/// One invocation per project file list would overflow a command line on a large tree.
#[test]
fn approved_files_are_batched_within_the_argument_budget() {
let paths: Vec<String> = (0..10).map(|n| format!("/project/src/file_{n}.rs")).collect();
let batched = batches(paths.clone(), 60);
assert!(batched.len() > 1, "{batched:?}");
assert!(batched.iter().all(|batch| batch.iter().map(|p| p.len() + 1).sum::<usize>() <= 60));
assert_eq!(batched.concat(), paths);
let oversized = vec!["x".repeat(100), "y".into()];
assert_eq!(batches(oversized.clone(), 60), [vec![oversized[0].clone()], vec!["y".into()]]);
}
}

View File

@@ -0,0 +1,109 @@
//! ast-grep output — what a call reports, and what an applied rewrite tells the rest of Luxe.
use super::*;
pub(super) fn search_outcome(
found: Result<Vec<AstGrepMatch>, AstGrepError>,
) -> luxe_core::ToolResultData {
match found {
Ok(matches) => ToolResultData::text(render_matches(&matches, false)),
Err(e) => ToolResultData::error(format!("ast-grep search error: {e}")),
}
}
/// Report a replace, first announcing each file an applied rewrite changed.
pub(super) async fn finish_replace(
tool: &AstGrepTool,
found: Result<Vec<AstGrepMatch>, AstGrepError>,
applied: bool,
cx: &ToolContext,
) -> luxe_core::ToolResultData {
let injected = match (&found, applied) {
(Ok(matches), true) => announce(tool, matches, cx).await,
_ => Vec::new(),
};
let mut result = replace_outcome(found, applied);
if !injected.is_empty() {
let diagnostics = format!("\n--- Diagnostics ---\n{}", injected.join("\n"));
result.content.push(luxe_core::ContentBlock::Text(diagnostics));
}
result
}
/// `FileChanged` and the read tracker for every rewritten file, as an `edit` would get.
async fn announce(tool: &AstGrepTool, matches: &[AstGrepMatch], cx: &ToolContext) -> Vec<String> {
let mut files: Vec<&PathBuf> = matches.iter().map(|item| &item.file).collect();
files.sort();
files.dedup();
let mut injected = Vec::new();
for file in files {
let path = cx.cwd.join(file);
injected.extend(cx.file_changed(&path, "edit").await);
if let Some(tracker) = &tool.tracker {
tracker.record(&cx.session_id, &path);
}
}
injected
}
fn replace_outcome(
found: Result<Vec<AstGrepMatch>, AstGrepError>,
applied: bool,
) -> luxe_core::ToolResultData {
match found {
Ok(matches) => {
let mut body = render_matches(&matches, applied);
if !applied && !matches.is_empty() {
body.push_str("\npreview only — re-run with apply=true to write");
}
ToolResultData::text(body)
}
Err(e) => ToolResultData::error(format!("ast-grep replace error: {e}")),
}
}
/// Render matches as a compact, model-friendly listing (not raw JSON).
pub(super) fn render_matches(matches: &[AstGrepMatch], applied: bool) -> String {
if matches.is_empty() {
return "no matches".to_string();
}
let mut out = String::new();
let verb = if applied { "rewrote" } else { "found" };
out.push_str(&format!("{verb} {} match(es)\n", matches.len()));
for item in matches {
out.push_str(&format!(
"{}:{}:{}\n",
luxe_core::paths::display(&item.file),
item.line,
item.column
));
out.push_str(&excerpt(&item.text, " - "));
if let Some(replacement) = &item.replacement {
out.push_str(&excerpt(replacement, " + "));
}
}
// Every other search tool bounds what it hands back; this one did not, and a structural
// pattern matching whole function bodies across a large repo produced a payload that
// *exceeded the model's context window* and killed the turn outright. A search must degrade
// to "here is the head, narrow it" — never to a dead turn.
crate::output_limit::cap_lines(
&out,
MAX_MATCH_LINES,
"lines",
"Narrow the pattern or pass `paths` to scope the search.",
)
}
/// One match's source, capped: a structural pattern can match an entire function body, and the
/// opening lines are what identify it.
fn excerpt(text: &str, prefix: &str) -> String {
let mut out = String::new();
let total = text.lines().count();
for line in text.lines().take(MAX_MATCH_BODY_LINES) {
out.push_str(&format!("{prefix}{line}\n"));
}
if total > MAX_MATCH_BODY_LINES {
out.push_str(&format!("{prefix}… ({} more lines)\n", total - MAX_MATCH_BODY_LINES));
}
out
}

View File

@@ -0,0 +1,320 @@
use super::*;
mod read_policy;
#[cfg(unix)] // the stub CLI is a shell script
mod session_cwd;
/// A stand-in CLI on PATH: `name` becomes an executable shell script printing `body`, so the
/// tool can be driven without a real ast-grep install.
///
/// Writing a script and immediately exec'ing it races the kernel: while any handle is still
/// open for writing, `execve` returns `ETXTBSY` ("Text file busy"). It only shows up under
/// parallel test load, which makes it the worst kind of flake — so the handle is synced and
/// dropped explicitly, before the mode is set.
///
/// Unix-only, like every test that uses it: the stub is a `#!/bin/sh` script.
#[cfg(unix)]
fn fake_binary(dir: &std::path::Path, name: &str, body: &str) {
use std::io::Write as _;
use std::os::unix::fs::PermissionsExt;
let path = dir.join(name);
{
let mut file = std::fs::File::create(&path).expect("create fake binary");
file.write_all(body.as_bytes()).expect("write fake binary");
file.sync_all().expect("flush fake binary");
}
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
.expect("chmod fake binary");
}
/// Run a just-written stub, retrying the one failure that is nobody's fault.
///
/// Linux refuses to exec a file any process holds open for writing (`ETXTBSY`), and in a
/// multithreaded test binary that is reachable without doing anything wrong: the write handle
/// above is closed before the mode is set, but a *sibling* test spawning a process forks the whole
/// descriptor table first and only drops its copy at its own `exec`. Land inside that window and
/// this exec fails with "Text file busy".
///
/// The window is microseconds wide and cannot be closed from this side, so the answer is to try
/// again rather than to let the suite's reliability depend on scheduling.
#[cfg(unix)]
async fn run_fake(binary: &std::path::Path, args: &[String]) -> std::process::Output {
for attempt in 0..50u64 {
match tokio::process::Command::new(binary).args(args).output().await {
Ok(output) => return output,
Err(error) if error.kind() == std::io::ErrorKind::ExecutableFileBusy => {
tokio::time::sleep(std::time::Duration::from_millis(2 * (attempt + 1))).await;
}
Err(error) => panic!("run {}: {error}", binary.display()),
}
}
panic!("{} stayed busy: a sibling test is holding it open", binary.display());
}
/// The scope arrives however a model spells it: an array of `paths`, or one `path` like the
/// sibling tools take. Dropping the second searched the whole project instead.
#[test]
fn a_single_path_scopes_the_search_like_paths_does() {
let one: AstGrepSearchParams = serde_json::from_value(serde_json::json!(
{"pattern": "$X.unwrap()", "lang": "rust", "path": "src/a.rs"}
))
.expect("parses");
assert_eq!(one.paths, ["src/a.rs"]);
let many: AstGrepSearchParams = serde_json::from_value(serde_json::json!(
{"pattern": "$X", "lang": "rust", "paths": ["a", "b"]}
))
.expect("parses");
assert_eq!(many.paths, ["a", "b"]);
let none: AstGrepSearchParams =
serde_json::from_value(serde_json::json!({"pattern": "$X", "lang": "rust"}))
.expect("parses");
assert!(none.paths.is_empty());
let replace: AstGrepReplaceParams = serde_json::from_value(serde_json::json!(
{"pattern": "$X", "rewrite": "$X", "lang": "rust", "path": "src"}
))
.expect("parses");
assert_eq!(replace.paths, ["src"]);
}
#[test]
fn a_match_parses_into_file_position_and_range() {
let value: serde_json::Value = serde_json::from_str(
r#"{"text":"helper(1, 2)","range":{"byteOffset":{"start":37,"end":49},
"start":{"line":2,"column":4},"end":{"line":2,"column":16}},
"file":"src/a.rs","replacement":"assist(1, 2)"}"#,
)
.unwrap();
let parsed = parse_match(&value).expect("parse");
assert_eq!(parsed.file, PathBuf::from("src/a.rs"));
assert_eq!(parsed.range, (37, 49));
assert_eq!(parsed.line, 3, "ast-grep's 0-based line becomes 1-based");
assert_eq!(parsed.column, 4);
assert_eq!(parsed.text, "helper(1, 2)");
assert_eq!(parsed.replacement.as_deref(), Some("assist(1, 2)"));
}
#[test]
fn matches_render_as_a_compact_listing_with_the_rewrite() {
let matches = vec![AstGrepMatch {
file: PathBuf::from("src/a.rs"),
range: (37, 49),
line: 3,
column: 4,
text: "helper(1, 2)".into(),
replacement: Some("assist(1, 2)".into()),
}];
let rendered = render_matches(&matches, false);
assert!(rendered.contains("found 1 match(es)"));
assert!(rendered.contains("src/a.rs:3:4"));
assert!(rendered.contains(" - helper(1, 2)"));
assert!(rendered.contains(" + assist(1, 2)"));
assert_eq!(render_matches(&[], false), "no matches");
}
/// A `sg` on PATH that is the shadow-utils group tool spawns fine and must still be rejected.
#[cfg(unix)]
#[tokio::test]
async fn a_colliding_unix_sg_is_not_mistaken_for_ast_grep() {
let dir = tempfile::tempdir().expect("temp dir");
fake_binary(
dir.path(),
"sg",
"#!/bin/sh\necho 'Usage: sg [-] group [[-c] command]' >&2\nexit 1\n",
);
// Probe the fake directly rather than through PATH (the resolution cache is per-process).
let output = run_fake(&dir.path().join("sg"), &["--version".to_string()]).await;
assert!(!output.status.success(), "the group tool exits non-zero");
assert!(
!String::from_utf8_lossy(&output.stdout).to_ascii_lowercase().contains("ast-grep"),
"and never claims to be ast-grep"
);
}
/// End-to-end against a fake ast-grep that emits real streaming JSON: proves the argument
/// shape and the parser agree without needing the binary installed.
#[cfg(unix)]
#[tokio::test]
async fn a_stub_cli_drives_search_argument_shape_and_parsing() {
let dir = tempfile::tempdir().expect("temp dir");
let args_log = dir.path().join("args.txt");
fake_binary(
dir.path(),
"ast-grep",
&format!(
"#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then echo 'ast-grep 0.45.0'; exit 0; fi\n\
printf '%s\\n' \"$*\" > {log}\n\
echo '{{\"text\":\"foo()\",\"range\":{{\"byteOffset\":{{\"start\":1,\"end\":6}},\
\"start\":{{\"line\":0,\"column\":2}}}},\"file\":\"x.rs\"}}'\n",
log = args_log.display()
),
);
let binary = dir.path().join("ast-grep");
let mut args = base_args("foo()", "rust");
args.push("--json=stream".to_string());
let output = run_fake(&binary, &args).await;
let stdout = String::from_utf8_lossy(&output.stdout);
let value: serde_json::Value =
serde_json::from_str(stdout.lines().next().expect("one match")).expect("json");
let parsed = parse_match(&value).expect("parse");
assert_eq!(parsed.file, PathBuf::from("x.rs"));
assert_eq!(parsed.line, 1);
let logged = std::fs::read_to_string(&args_log).expect("args log");
assert!(logged.contains("run"), "uses the `run` subcommand: {logged}");
assert!(logged.contains("--pattern foo()"), "passes --pattern: {logged}");
assert!(logged.contains("--lang rust"), "passes --lang: {logged}");
assert!(logged.contains("--json=stream"), "asks for streaming JSON: {logged}");
assert!(!logged.contains("search"), "there is no `search` subcommand: {logged}");
}
/// Runs only where a real ast-grep is installed; skipped otherwise so CI stays hermetic.
#[tokio::test]
async fn a_real_ast_grep_finds_a_structural_match() {
let Some(_binary) = resolve_binary().await else {
eprintln!("skipping: no ast-grep on PATH");
return;
};
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(
dir.path().join("a.rs"),
"fn main() {\n helper(1, 2);\n}\nfn helper(a: i32, b: i32) -> i32 { a + b }\n",
)
.expect("write source");
let found = AstGrepTool::search(
&AstGrepSearchParams {
pattern: "helper($$$ARGS)".to_string(),
lang: "rust".to_string(),
paths: vec![dir.path().display().to_string()],
},
dir.path(),
)
.await
.expect("search runs");
assert_eq!(found.len(), 1, "the call site matches structurally: {found:?}");
assert_eq!(found[0].line, 2);
assert_eq!(found[0].text, "helper(1, 2)");
// No matches is an empty result, not an error — ast-grep exits non-zero for it.
let none = AstGrepTool::search(
&AstGrepSearchParams {
pattern: "definitely_not_here($$$A)".to_string(),
lang: "rust".to_string(),
paths: vec![dir.path().display().to_string()],
},
dir.path(),
)
.await
.expect("an empty result is not an error");
assert!(none.is_empty());
// A preview reports the rewrite without touching the file; apply writes it.
let params = AstGrepReplaceParams {
pattern: "helper($$$ARGS)".to_string(),
rewrite: "assist($$$ARGS)".to_string(),
lang: "rust".to_string(),
paths: vec![dir.path().display().to_string()],
apply: false,
};
let preview = AstGrepTool::replace(&params, dir.path()).await.expect("preview runs");
assert_eq!(preview[0].replacement.as_deref(), Some("assist(1, 2)"));
assert!(std::fs::read_to_string(dir.path().join("a.rs")).unwrap().contains("helper(1, 2)"));
let applied = AstGrepReplaceParams { apply: true, ..params };
AstGrepTool::replace(&applied, dir.path()).await.expect("apply runs");
let rewritten = std::fs::read_to_string(dir.path().join("a.rs")).unwrap();
assert!(rewritten.contains("assist(1, 2)"), "the rewrite landed: {rewritten}");
}
/// The failure that killed a real turn: a structural pattern across a large repo returned
/// 232,744 tokens — past the model's context window — because every match carried its whole
/// body and nothing capped the total.
#[test]
fn a_broad_search_is_bounded_instead_of_flooding_context() {
let body: String = (1..=40).map(|n| format!(" line {n}\n")).collect();
let matches: Vec<AstGrepMatch> = (0..300)
.map(|n| AstGrepMatch {
file: PathBuf::from(format!("services/service_{n}.py")),
range: (0, 10),
line: 1,
column: 0,
text: format!("def handler_{n}():\n{body}"),
replacement: None,
})
.collect();
let rendered = render_matches(&matches, false);
let lines = rendered.lines().count();
assert!(lines <= MAX_MATCH_LINES + 3, "bounded, got {lines} lines");
assert!(rendered.contains("found 300 match(es)"), "the true count still leads");
assert!(rendered.contains("more lines not shown"), "and says what it withheld");
assert!(rendered.contains("Narrow the pattern"), "with a way to recover the rest");
}
#[test]
fn one_match_shows_its_head_not_its_whole_body() {
let body: String = (1..=30).map(|n| format!(" line {n}\n")).collect();
let one = vec![AstGrepMatch {
file: PathBuf::from("a.py"),
range: (0, 1),
line: 1,
column: 0,
text: format!("def f():\n{body}"),
replacement: None,
}];
let rendered = render_matches(&one, false);
assert!(rendered.contains("def f():"), "the identifying line survives");
assert!(rendered.contains("… (25 more lines)"), "the rest is accounted for: {rendered}");
}
#[test]
fn a_small_result_is_untouched() {
let one = vec![AstGrepMatch {
file: PathBuf::from("a.py"),
range: (0, 1),
line: 3,
column: 2,
text: "def f(): pass".to_string(),
replacement: None,
}];
let rendered = render_matches(&one, false);
assert!(!rendered.contains("not shown"), "nothing to trim: {rendered}");
assert!(rendered.contains("a.py:3:2"));
}
/// Upstream's parse error ends in a URL a model cannot open, so a rejected pattern was a dead
/// end — observed: two bad patterns, then eight `bash` calls instead.
#[test]
fn a_rejected_pattern_comes_back_with_the_rules_it_broke() {
let upstream = "Error: Cannot parse query as a valid pattern.\n\
Help: see https://ast-grep.github.io/guide/pattern-syntax.html";
let shown = AstGrepError::CommandFailed(upstream.to_string()).to_string();
assert!(shown.contains("ONE syntactically valid snippet"), "{shown}");
assert!(shown.contains("$$$ARGS"), "names the metavariable forms: {shown}");
assert!(shown.contains("use `grep` instead"), "and when to give up: {shown}");
}
/// An environment failure is not a pattern failure; don't lecture about syntax.
#[test]
fn an_unrelated_failure_gets_no_pattern_lecture() {
let shown = AstGrepError::CommandFailed("permission denied".to_string()).to_string();
assert_eq!(shown, "command failed: permission denied");
}
/// A tool that cannot work here should not cost a schema; the identity check is the same one
/// `resolve_binary` applies, because the shadow-utils `sg` on PATH is not this tool.
#[test]
fn availability_matches_whether_a_real_ast_grep_resolves() {
let available = is_available();
let paths = candidate_paths();
if paths.is_empty() {
assert!(!available, "nothing on PATH means nothing to offer");
}
// When it reports available it must have cached the binary it verified.
if available {
assert!(BINARY.get().is_some(), "seeds the cache resolve_binary reads");
}
}

View File

@@ -0,0 +1,140 @@
use super::*;
use luxe_core::permission::PermissionAction::Deny;
#[cfg(unix)]
use luxe_core::permission::{
PermissionAction::{Allow, Ask},
PermissionEngine, PermissionMode, Rule,
};
use luxe_core::tool::FileReadHook;
#[cfg(unix)]
fn policy_rule(
permission: &str,
pattern: &str,
action: luxe_core::permission::PermissionAction,
) -> Rule {
Rule { permission: permission.into(), pattern: pattern.into(), action }
}
fn context(cwd: &std::path::Path) -> ToolContext {
let (_, cancelled) = tokio::sync::watch::channel(false);
let (event_tx, _) = tokio::sync::mpsc::channel(8);
ToolContext {
cwd: cwd.into(),
session_id: "ast-read-policy".into(),
cancelled: std::sync::Arc::new(cancelled),
event_tx,
hook_ctx: None,
question_broker: None,
}
}
#[cfg(unix)]
fn fixture() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("data/nested")).unwrap();
for (path, text) in [
(".env", "ROOT_PRIVATE"),
("nested/.env", "CHILD_PRIVATE"),
("nested/.env.example", "PUBLIC_TEMPLATE"),
("safe.rs", "SAFE_TEXT"),
("review.rs", "REVIEW_TEXT"),
] {
std::fs::write(dir.path().join("data").join(path), text).unwrap();
}
fake_binary(
dir.path(),
"ast-policy-cli",
r#"#!/bin/sh
for arg in "$@"; do
if [ -d "$arg" ]; then find "$arg" -type f
elif [ -f "$arg" ]; then printf '%s\n' "$arg"
fi
done | while IFS= read -r file; do
text=$(cat "$file")
printf '{"file":"%s","text":"%s","range":{"byteOffset":{"start":0,"end":1},"start":{"line":0,"column":0}}}\n' "$file" "$text"
done
"#,
);
BINARY.set(dir.path().join("ast-policy-cli").to_string_lossy().into_owned()).unwrap();
dir
}
/// A directory matcher must not see denied/Ask content before its output is filtered.
#[cfg(unix)]
#[tokio::test]
async fn the_matcher_receives_only_policy_approved_ast_files() {
let dir = fixture();
let data = dir.path().join("data");
let policy = PermissionEngine::assemble(
PermissionMode::Default,
vec![policy_rule("read", ".env", Allow), policy_rule("read", "review.rs", Ask)],
vec![],
false,
);
for action in ["search", "replace"] {
let args = serde_json::json!({"action": action, "pattern": "foo($A)", "rewrite": "bar($A)", "lang": "rust", "paths": [data]});
let result = AstGrepTool::new().run_with_read_hook(args, &context(&data), &policy).await;
assert!(!result.is_error, "{:?}", result.content);
let text = result
.content
.iter()
.filter_map(|block| match block {
luxe_core::ContentBlock::Text(text) => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
assert!(
text.contains("ROOT_PRIVATE")
&& text.contains("PUBLIC_TEMPLATE")
&& text.contains("SAFE_TEXT"),
"{text}"
);
assert!(!text.contains("CHILD_PRIVATE") && !text.contains("REVIEW_TEXT"), "{text}");
assert!(text.contains("skipped") && text.contains("approval"), "{text}");
}
}
/// Approved files go to the matcher absolute, but the model reads them project-relative, as an
/// unguarded run shows them.
#[cfg(unix)]
#[tokio::test]
async fn guarded_matches_are_reported_relative_to_the_working_directory() {
let dir = fixture();
let data = dir.path().join("data");
let args = serde_json::json!({"action": "search", "pattern": "foo($A)", "lang": "rust"});
let result = AstGrepTool::new()
.run_with_read_hook(args, &context(&data), &PermissionEngine::permissive())
.await;
let luxe_core::ContentBlock::Text(text) = &result.content[0] else { panic!("{result:?}") };
assert!(text.contains("\nsafe.rs:1:0"), "{text}");
assert!(!text.contains(&data.display().to_string()), "{text}");
}
struct DenyEveryFile;
impl FileReadHook for DenyEveryFile {
fn decision(&self, _: &std::path::Path) -> luxe_core::permission::PermissionAction {
Deny
}
}
/// With no approved files, no matcher invocation may fall back to the current directory.
#[tokio::test]
async fn a_fully_denied_ast_scope_never_starts_the_matcher() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("safe.rs"), "foo(1)").unwrap();
let args = serde_json::json!({"action": "search", "pattern": "foo($A)", "lang": "rust", "paths": [dir.path()]});
let result =
AstGrepTool::new().run_with_read_hook(args, &context(dir.path()), &DenyEveryFile).await;
assert!(!result.is_error, "{:?}", result.content);
let luxe_core::ContentBlock::Text(body) = &result.content[0] else { panic!("{result:?}") };
assert_eq!(body, "no policy-approved files to search");
assert!(
result.content.iter().any(
|block| matches!(block, luxe_core::ContentBlock::Text(text) if text.contains("skipped"))
),
"{:?}",
result.content
);
}

View File

@@ -0,0 +1,144 @@
//! The session's working directory, the preview's files, and what an applied rewrite announces.
use super::*;
use luxe_core::hook::{Hook, HookCtx, HookEvent, HookOutcome, HookPoint, HookRegistry};
use std::sync::{Arc, Mutex};
fn context(cwd: &std::path::Path) -> ToolContext {
let (_, cancelled) = tokio::sync::watch::channel(false);
let (event_tx, _) = tokio::sync::mpsc::channel(8);
ToolContext {
cwd: cwd.into(),
session_id: "ast-cwd".into(),
cancelled: Arc::new(cancelled),
event_tx,
hook_ctx: None,
question_broker: None,
}
}
/// A stub that logs each invocation's directory and arguments, then reports one match in `a.rs`.
fn stub(dir: &std::path::Path) -> std::path::PathBuf {
let log = dir.join("calls.log");
fake_binary(
dir,
"ast-cwd-cli",
&format!(
"#!/bin/sh\nprintf '%s | %s\\n' \"$PWD\" \"$*\" >> {log}\n\
echo '{{\"text\":\"foo(1)\",\"replacement\":\"bar(1)\",\"range\":{{\"byteOffset\":\
{{\"start\":0,\"end\":6}},\"start\":{{\"line\":0,\"column\":0}}}},\"file\":\"a.rs\"}}'\n",
log = log.display()
),
);
BINARY.set(dir.join("ast-cwd-cli").to_string_lossy().into_owned()).unwrap();
log
}
/// A session in a worktree must search that tree, not wherever the process happened to start.
#[tokio::test]
async fn the_matcher_runs_in_the_sessions_working_directory() {
let bin = tempfile::tempdir().unwrap();
let log = stub(bin.path());
let project = tempfile::tempdir().unwrap();
let args = serde_json::json!({"action": "search", "pattern": "foo($A)", "lang": "rust"});
AstGrepTool::new().run(args, &context(project.path())).await;
let calls = std::fs::read_to_string(log).unwrap();
let project = std::fs::canonicalize(project.path()).unwrap();
assert!(calls.starts_with(&format!("{} |", project.display())), "{calls}");
}
struct Announced(Arc<Mutex<Vec<String>>>);
#[async_trait]
impl Hook for Announced {
fn name(&self) -> &'static str {
"announced"
}
fn interest(&self) -> &[HookPoint] {
&[HookPoint::FileChanged]
}
async fn on(&self, _: HookPoint, event: &HookEvent, _: &HookCtx) -> HookOutcome {
if let HookEvent::FileChanged { path, .. } = event {
self.0.lock().unwrap().push(path.clone());
}
HookOutcome::InjectContext("diagnostics: clean".into())
}
}
/// The write pass rewrites what the preview showed, not a fresh scan of the whole scope, and each
/// rewritten file goes through `FileChanged` and the read tracker like an `edit` would.
#[tokio::test]
async fn an_applied_rewrite_writes_the_previewed_files_and_announces_them() {
let bin = tempfile::tempdir().unwrap();
let log = stub(bin.path());
let project = tempfile::tempdir().unwrap();
std::fs::write(project.path().join("a.rs"), "foo(1)").unwrap();
let announced = Arc::new(Mutex::new(Vec::new()));
let mut registry = HookRegistry::new();
registry.register(Arc::new(Announced(announced.clone())));
let mut cx = context(project.path());
cx.hook_ctx = Some(HookCtx {
session_id: cx.session_id.clone(),
event_tx: cx.event_tx.clone(),
hook_registry: registry,
});
let tracker = Arc::new(luxe_core::read_tracker::ReadTracker::new());
let tool = AstGrepTool::new().with_tracker(tracker.clone());
let args = serde_json::json!({"action": "replace", "pattern": "foo($A)", "rewrite": "bar($A)",
"lang": "rust", "paths": ["src", "."], "apply": true});
let result = tool.run(args, &cx).await;
assert!(!result.is_error, "{result:?}");
let calls = std::fs::read_to_string(log).unwrap();
let update = calls.lines().find(|line| line.contains("--update-all")).expect(&calls);
assert!(update.ends_with(" a.rs"), "{update}");
assert!(!update.contains(" src") && !update.contains(" ."), "{update}");
let file = project.path().join("a.rs");
assert_eq!(*announced.lock().unwrap(), [luxe_core::paths::display(&file)]);
assert_eq!(tracker.status("ast-cwd", &file), luxe_core::read_tracker::ReadStatus::Current);
assert!(format!("{result:?}").contains("diagnostics: clean"), "{result:?}");
}
/// A batch that fails after earlier ones were rewritten must not read as nothing having happened.
#[tokio::test]
async fn a_failed_batch_reports_the_rewrites_already_on_disk() {
let bin = tempfile::tempdir().unwrap();
fake_binary(
bin.path(),
"ast-batch-cli",
"#!/bin/sh\ncase \"$*\" in *--update-all*b.rs*) echo 'disk full' >&2; exit 1;; esac\n\
case \"$*\" in *--update-all*) exit 0;; esac\n\
for f in \"$@\"; do case \"$f\" in *.rs) printf '{\"text\":\"foo(1)\",\"replacement\":\"bar(1)\",\
\"range\":{\"byteOffset\":{\"start\":0,\"end\":6},\"start\":{\"line\":0,\"column\":0}},\
\"file\":\"%s\"}\\n' \"$f\";; esac; done\n",
);
BINARY.set(bin.path().join("ast-batch-cli").to_string_lossy().into_owned()).unwrap();
let project = tempfile::tempdir().unwrap();
for file in ["a.rs", "b.rs"] {
std::fs::write(project.path().join(file), "foo(1)").unwrap();
}
let args = serde_json::json!({"action": "replace", "pattern": "foo($A)", "rewrite": "bar($A)",
"lang": "rust", "apply": true});
let call = super::super::guarded::Call::parse(args).unwrap();
let batches = vec![vec!["a.rs".to_string()], vec!["b.rs".to_string()]];
let result = call.over(&AstGrepTool::new(), batches, &context(project.path())).await;
let text = format!("{result:?}");
assert!(result.is_error, "{text}");
assert!(text.contains("rewrote 1 match") && text.contains("a.rs"), "{text}");
assert!(text.contains("disk full") && text.contains("on disk"), "{text}");
}
/// A file named like a flag reaches the matcher as a file: a repository cannot turn a search into a
/// rewrite with a file called `-U`.
#[tokio::test]
async fn paths_follow_an_end_of_options_marker() {
let bin = tempfile::tempdir().unwrap();
let log = stub(bin.path());
let project = tempfile::tempdir().unwrap();
let args = serde_json::json!({"action": "replace", "pattern": "foo($A)", "rewrite": "bar($A)",
"lang": "rust", "paths": ["-U"], "apply": true});
AstGrepTool::new().run(args, &context(project.path())).await;
let calls = std::fs::read_to_string(log).unwrap();
let mut lines = calls.lines();
assert!(lines.next().unwrap().ends_with(" -- -U"), "{calls}");
assert!(lines.next().unwrap().ends_with("--update-all -- a.rs"), "{calls}");
}