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

View file

@ -4,12 +4,15 @@
//! first paint,
//! bridges frontend IPC to engine commands and engine events to the window
//! (see [`host`]), opens the project given on the command line, and
//! optionally serves the JSON control channel (`--control PORT`).
//! optionally serves the JSON control channel (`--control PORT`). A normal
//! start is a single instance (see [`instance`]): a second start forwards
//! its path to the running app and exits.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod args;
mod host;
mod instance;
mod screenshot;
use std::process::ExitCode;
@ -25,7 +28,7 @@ use crate::host::{Host, MAIN_WINDOW, WINDOW_TITLE};
/// Parses the arguments, starts the control channel if asked, and runs the
/// app until it quits.
fn main() -> ExitCode {
let args = match args::parse(std::env::args().skip(1)) {
let args = match args::parse_os(std::env::args_os().skip(1)) {
Ok(args) => args,
Err(message) => {
eprintln!("lutra: {message}\n\n{}", args::USAGE);
@ -49,15 +52,24 @@ fn main() -> ExitCode {
}
}
/// Builds the Tauri app, starts the engine with the app's config and data
/// directories, the host, the control channel, and runs the event loop.
/// The window is created in the setup hook, which runs once the event loop
/// has started.
/// Forwards to a running instance if there is one; otherwise builds the
/// Tauri app, starts the engine with the app's config and data directories,
/// the host, the control channel and the single-instance listener, and runs
/// the event loop. The window is created in the setup hook, which runs once
/// the event loop has started. A single-instance listener or rendezvous
/// file that cannot be set up is reported and the app runs without it.
///
/// # Errors
/// A message if Tauri cannot build the app or resolve its directories, the
/// engine cannot start, or the control port cannot be bound.
/// A message if a running instance refused the forwarded path, Tauri cannot
/// build the app or resolve its directories, the engine cannot start, or
/// the control port cannot be bound.
fn run(args: &args::Args) -> Result<(), String> {
let instance_dir = instance::rendezvous_dir(args);
if let Some(dir) = &instance_dir
&& instance::forward(dir, args.path.as_deref())?
{
return Ok(());
}
let slot: Arc<OnceLock<Arc<Host>>> = Arc::new(OnceLock::new());
let setup_slot = slot.clone();
let initial_path = args.path.clone();
@ -106,9 +118,24 @@ fn run(args: &args::Args) -> Result<(), String> {
.map_err(|e| format!("cannot serve the control channel on port {port}: {e}"))?;
eprintln!("lutra: control channel on {}", server.local_addr());
}
let claim = instance_dir
.as_deref()
.and_then(|dir| match instance::Claim::take(dir, &host) {
Ok(claim) => Some(claim),
Err(message) => {
eprintln!("lutra: running without single instance: {message}");
None
}
});
host.start_pump()
.map_err(|e| format!("cannot start watching for changes: {e}"))?;
app.run(|_, _| {});
app.run(move |_, event| {
if let tauri::RunEvent::Exit = event
&& let Some(claim) = &claim
{
claim.release();
}
});
Ok(())
}