diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index 23188846..2cfa529b 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -9079,9 +9079,16 @@ directories explicitly observed as missing. `application.cli.planLaunch` accepts `{args: string[]}` (at most four arguments, 32768 bytes each). It returns `{action: "welcome"|"open"|"help"|"version", path, -detach}`. A single path is accepted; `--` protects dash-prefixed paths. `-h` / +detach, newWindow, handOff}`. A single path is accepted; `--` protects dash-prefixed paths. `-h` / `--help` and `-v` / `--version` are supported. The managed launcher prepends -`--cli`, requesting a detached GUI child. Invalid or ambiguous arguments fail +`--cli`, requesting a detached GUI child. Launch markers are recognized only +before the path and only in the order `--cli`, `--new-window`, `--`. +`--new-window` marks a process the host started to show another window; such a +process keeps its own window. `handOff` is true for `welcome` and `open` launches +that carry neither marker: the host hands them to the copy already running on +the same user data, which shows its window and opens the path there, and starts +its own workbench only when no copy answers. The detaching launcher, help and +version never hand off. Invalid or ambiguous arguments fail with `invalid_request`, before a window or plugin service is created. `application.cli.openTarget` accepts the native observation `{kind: "file"| diff --git a/rust/crates/core-rules/src/cli.rs b/rust/crates/core-rules/src/cli.rs index 7715a246..e8e723be 100644 --- a/rust/crates/core-rules/src/cli.rs +++ b/rust/crates/core-rules/src/cli.rs @@ -24,7 +24,14 @@ struct LaunchRequest { args: Vec, } -/// Resolves help, version, a single path and the launcher's internal detach marker. +/// Resolves help, version, a single path and the leading launch markers. +/// +/// Markers are recognized only in this order, before the path: `--cli` (the +/// managed launcher's process, which only starts a detached GUI child), then +/// `--new-window`, then `--`. `handOff` asks the host to give a window launch to +/// the copy already running on the same user data instead of opening a second +/// workbench beside it; `--new-window` launches, and the launcher process +/// itself, keep their own process. pub fn plan_launch_json(payload: Value) -> Result { let request: LaunchRequest = serde_json::from_value(payload).map_err(|_| invalid("Invalid CLI arguments"))?; @@ -37,14 +44,9 @@ pub fn plan_launch_json(payload: Value) -> Result { return Err(invalid("CLI arguments exceed their bounds")); } let mut args = request.args.as_slice(); - let detach = args.first().is_some_and(|s| s == "--cli"); - if detach { - args = &args[1..]; - } - let literal = args.first().is_some_and(|s| s == "--"); - if literal { - args = &args[1..]; - } + let detach = take_marker(&mut args, "--cli"); + let new_window = take_marker(&mut args, "--new-window"); + let literal = take_marker(&mut args, "--"); let (action, path) = match args { [] => ("welcome", ""), [arg] if !literal && (arg == "--help" || arg == "-h") => ("help", ""), @@ -52,7 +54,23 @@ pub fn plan_launch_json(payload: Value) -> Result { [arg] if !arg.is_empty() && (literal || !arg.starts_with('-')) => ("open", arg.as_str()), _ => return Err(invalid("Expected one file or folder path")), }; - Ok(json!({"action": action, "path": path, "detach": detach})) + let hand_off = !detach && !new_window && matches!(action, "welcome" | "open"); + Ok(json!({ + "action": action, + "path": path, + "detach": detach, + "newWindow": new_window, + "handOff": hand_off, + })) +} + +/// Drops `marker` from the front of `args` and reports whether it was there. +fn take_marker(args: &mut &[String], marker: &str) -> bool { + let present = args.first().is_some_and(|arg| arg == marker); + if present { + *args = &args[1..]; + } + present } #[derive(Deserialize)] @@ -273,6 +291,37 @@ mod tests { assert!(!commands.contains(&json!("search.files"))); } + /// A window launch hands off to the running copy unless it asked for a + /// window of its own; a second process on the same user data would open a + /// second workbench and race the first one's saved state. + #[test] + fn window_launches_hand_off_unless_a_new_window_is_requested() { + let plan = |args: &[&str]| plan_launch_json(json!({ "args": args })).unwrap(); + let welcome = plan(&[]); + assert_eq!(welcome["handOff"], true); + assert_eq!(welcome["newWindow"], false); + let opened = plan(&["--", "/work/project"]); + assert_eq!(opened["action"], "open"); + assert_eq!(opened["handOff"], true); + // The launcher's own process only starts the GUI child, which hands off. + let launcher = plan(&["--cli", "/work/project"]); + assert_eq!(launcher["detach"], true); + assert_eq!(launcher["handOff"], false); + let window = plan(&["--new-window", "--", "/work/project"]); + assert_eq!(window["path"], "/work/project"); + assert_eq!(window["newWindow"], true); + assert_eq!(window["handOff"], false); + let typed = plan(&["--cli", "--new-window", "--", "-notes.md"]); + assert_eq!(typed["path"], "-notes.md"); + assert_eq!(typed["detach"], true); + assert_eq!(typed["newWindow"], true); + assert_eq!(plan(&["--help"])["handOff"], false); + assert_eq!(plan(&["--version"])["handOff"], false); + // Markers are positional: after `--` the marker is the path itself. + assert_eq!(plan(&["--", "--new-window"])["path"], "--new-window"); + assert!(plan_launch_json(json!({"args": ["--new-window", "--cli", "x"]})).is_err()); + } + #[test] fn shell_registration_is_idempotent_and_preserves_unrelated_content() { for shell in ["zsh", "bash", "sh", "fish", "windows"] { diff --git a/rust/crates/core-rules/tests/cli_fixture.rs b/rust/crates/core-rules/tests/cli_fixture.rs new file mode 100644 index 00000000..2ac9d841 --- /dev/null +++ b/rust/crates/core-rules/tests/cli_fixture.rs @@ -0,0 +1,34 @@ +//! Checks the CLI launch and open-target contract that native hosts replay. + +use lithe_core_rules::execute; +use serde_json::{json, Value}; + +const FIXTURE: &str = include_str!("../../../fixtures/application/cli-v1.json"); + +/// Every launch case states only the flags it sets; an absent flag must be false, +/// so the hosts' hand-off and new-window decisions stay identical to Core's. +#[test] +fn shared_launch_plans_and_targets_match() { + let fixture: Value = serde_json::from_str(FIXTURE).unwrap(); + assert_eq!(fixture["version"], 1); + for case in fixture["launch"].as_array().unwrap() { + let plan = execute("application.cli.planLaunch", json!({"args": case["args"]})).unwrap(); + assert_eq!(plan["action"], case["action"], "{}", case["args"]); + for flag in ["detach", "newWindow", "handOff"] { + let expected = case[flag].as_bool().unwrap_or(false); + assert_eq!(plan[flag], expected, "{flag} for {}", case["args"]); + } + } + for case in fixture["targets"].as_array().unwrap() { + let target = execute("application.cli.openTarget", json!({"kind": case["kind"]})).unwrap(); + assert_eq!(target["mode"], case["mode"], "{}", case["kind"]); + assert_eq!(target["plugins"], case["plugins"], "{}", case["kind"]); + for command in case["commands"].as_array().into_iter().flatten() { + assert!( + target["commands"].as_array().unwrap().contains(command), + "{command} allowed for {}", + case["kind"] + ); + } + } +} diff --git a/rust/crates/diagnostics/src/catalog_generated.rs b/rust/crates/diagnostics/src/catalog_generated.rs index 94058504..ba17c287 100644 --- a/rust/crates/diagnostics/src/catalog_generated.rs +++ b/rust/crates/diagnostics/src/catalog_generated.rs @@ -228,7 +228,7 @@ pub static EVENTS: &[EventMetadata] = &[ message: "User action {action} performed", fields: &[ FieldMetadata { name: "action", field_type: FieldType::Token, required: true, description: "Stable id of the user action that started the work, never a localized menu or button title", reason: None }, - FieldMetadata { name: "surface", field_type: FieldType::Token, required: true, description: "UI surface that invoked the action: menu, shortcut, palette, toolbar, button, context_menu, notification, plugin or drag_drop", reason: None }, + FieldMetadata { name: "surface", field_type: FieldType::Token, required: true, description: "UI surface that invoked the action (launch is a later start of the app that the running copy took over): menu, shortcut, palette, toolbar, button, context_menu, notification, plugin, drag_drop or launch", reason: None }, ], span: None, rate: 600, diff --git a/rust/fixtures/application/cli-v1.json b/rust/fixtures/application/cli-v1.json index 5a12ace0..1df64e13 100644 --- a/rust/fixtures/application/cli-v1.json +++ b/rust/fixtures/application/cli-v1.json @@ -3,7 +3,8 @@ "launch": [ { "args": [], - "action": "welcome" + "action": "welcome", + "handOff": true }, { "args": [ @@ -12,6 +13,39 @@ ], "action": "open", "detach": true + }, + { + "args": [ + "--", + "/work/project" + ], + "action": "open", + "handOff": true + }, + { + "args": [ + "--new-window", + "--", + "/work/project" + ], + "action": "open", + "newWindow": true + }, + { + "args": [ + "--cli", + "--new-window", + "notes.md" + ], + "action": "open", + "detach": true, + "newWindow": true + }, + { + "args": [ + "--version" + ], + "action": "version" } ], "targets": [ diff --git a/rust/logging/events/app.json b/rust/logging/events/app.json index 1eb80f86..1e3b9fdb 100644 --- a/rust/logging/events/app.json +++ b/rust/logging/events/app.json @@ -543,7 +543,7 @@ "surface": { "type": "token", "required": true, - "description": "UI surface that invoked the action: menu, shortcut, palette, toolbar, button, context_menu, notification, plugin or drag_drop" + "description": "UI surface that invoked the action (launch is a later start of the app that the running copy took over): menu, shortcut, palette, toolbar, button, context_menu, notification, plugin, drag_drop or launch" } }, "rate": 600,