M3.7: lutra CLI and single instance

- crates/control: rendezvous file (<data_dir>/instance.json, port + random
  token, atomic write, 0600 on Unix, stale detection), token-protected
  listener (start_with_token, app.auth first, constant-time compare,
  10 s auth timeout), app.focus, and a blocking client (connect_live,
  request/execute/subscribe/next_event, open_folder/open_file/reveal)
- lutra-desktop: a normal start forwards its path to a running instance
  and exits, otherwise claims the rendezvous; --control instances skip it
  unless LUTRA_RENDEZVOUS_DIR is set; non-Unicode args are usage errors
- lutra-cli (binary lutra, now L6): path[:line[:column]] targets, folder ->
  project.open, file -> file.open (parent opened without a project, outside
  the project refused), editor.reveal, starts the desktop if needed,
  --wait until bufferClosed, --version/--help, exit codes 0/1/2
- tests: proptest target parsing, rendezvous read/write/stale, token check
  over TCP, forwarding against a test backend, --wait end to end
- docs: architecture, G7, ROADMAP (agent automation 18% est.), roadmap,
  README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-18 00:23:09 +02:00
parent 9ccfc1bf6a
commit 9b278099cf
26 changed files with 2162 additions and 37 deletions

156
apps/lutra-cli/tests/cli.rs Normal file
View file

@ -0,0 +1,156 @@
//! The `lutra` binary end to end against a test backend that claims a
//! temporary rendezvous directory (`LUTRA_RENDEZVOUS_DIR`).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use std::path::Path;
use std::process::{Command, Output, Stdio};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use lutra_control::rendezvous::{self, Instance};
use lutra_engine::Event;
use lutra_testkit::EngineBackend;
use serde_json::json;
/// A backend with a token listener and the UI command `editor.reveal`
/// registered, as a loaded window would have it.
fn instance(dir: &Path) -> Arc<EngineBackend> {
use lutra_control::Backend;
let backend = Arc::new(EngineBackend::new());
backend
.execute(
"ui.registerCommands",
json!({"commands": [{"id": "editor.reveal", "labelKey": "command.editor.reveal"}]}),
)
.unwrap();
let token = rendezvous::new_token().unwrap();
let server = lutra_control::start_with_token(0, backend.clone(), &token).unwrap();
rendezvous::write(
dir,
&Instance {
port: server.local_addr().port(),
token,
pid: std::process::id(),
version: "test".into(),
},
)
.unwrap();
backend
}
fn lutra(dir: &Path, args: &[&str]) -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_lutra"));
command
.args(args)
.env(rendezvous::DIR_ENV, dir)
.env("LUTRA_DESKTOP", dir.join("no-such-desktop"))
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command
}
fn run(dir: &Path, args: &[&str]) -> Output {
lutra(dir, args).output().unwrap()
}
fn buffer_ids(backend: &EngineBackend) -> Vec<u64> {
backend.engine.lock().unwrap().state().buffers.ids()
}
#[test]
fn version_help_and_usage_errors() {
let dir = tempfile::tempdir().unwrap();
let out = run(dir.path(), &["--version"]);
assert_eq!(out.status.code(), Some(0));
assert!(String::from_utf8_lossy(&out.stdout).starts_with("lutra "));
let out = run(dir.path(), &["--help"]);
assert_eq!(out.status.code(), Some(0));
assert!(String::from_utf8_lossy(&out.stdout).contains("--wait"));
assert_eq!(run(dir.path(), &["--bogus"]).status.code(), Some(2));
assert_eq!(run(dir.path(), &["a.php:0"]).status.code(), Some(2));
assert_eq!(run(dir.path(), &[""]).status.code(), Some(2));
}
#[test]
fn errors_exit_with_one() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("missing.php");
let out = run(dir.path(), &[missing.to_str().unwrap()]);
assert_eq!(out.status.code(), Some(1));
let out = run(dir.path(), &[]);
assert_eq!(
out.status.code(),
Some(1),
"no instance and no desktop to start"
);
assert!(String::from_utf8_lossy(&out.stderr).contains("no-such-desktop"));
}
#[test]
fn opens_a_file_at_a_line_in_the_running_instance() {
let rv = tempfile::tempdir().unwrap();
let project = tempfile::tempdir().unwrap();
let file = project.path().join("a.php");
std::fs::write(&file, "<?php\n".repeat(20)).unwrap();
let backend = instance(rv.path());
let (sink, events) = std::sync::mpsc::sync_channel(64);
lutra_control::Backend::subscribe(backend.as_ref(), sink);
let arg = format!("{}:10:3", file.display());
let out = run(rv.path(), &[&arg]);
assert_eq!(
out.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(buffer_ids(&backend).len(), 1);
let reveal = events
.try_iter()
.find(|e| matches!(e, Event::UiCommand { .. }))
.unwrap();
assert_eq!(
reveal,
Event::UiCommand {
id: "editor.reveal".into(),
params: json!({"path": "a.php", "line": 9, "column": 2}),
}
);
}
#[test]
fn wait_returns_after_the_tab_is_closed() {
use lutra_control::Backend;
let rv = tempfile::tempdir().unwrap();
let project = tempfile::tempdir().unwrap();
let file = project.path().join("COMMIT_EDITMSG");
std::fs::write(&file, "msg\n").unwrap();
let backend = instance(rv.path());
let mut child = lutra(rv.path(), &["--wait", file.to_str().unwrap()])
.spawn()
.unwrap();
let deadline = Instant::now() + Duration::from_secs(20);
let id = loop {
if let Some(id) = buffer_ids(&backend).first() {
break *id;
}
assert!(Instant::now() < deadline, "the file was never opened");
thread::sleep(Duration::from_millis(50));
};
thread::sleep(Duration::from_millis(300));
assert!(child.try_wait().unwrap().is_none(), "--wait returned early");
backend.execute("buffer.close", json!({"id": id})).unwrap();
let deadline = Instant::now() + Duration::from_secs(20);
let status = loop {
if let Some(status) = child.try_wait().unwrap() {
break status;
}
assert!(Instant::now() < deadline, "--wait did not return");
thread::sleep(Duration::from_millis(50));
};
assert_eq!(status.code(), Some(0));
}