Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"|
Expand Down
69 changes: 59 additions & 10 deletions rust/crates/core-rules/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ struct LaunchRequest {
args: Vec<String>,
}

/// 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<Value, RuleError> {
let request: LaunchRequest =
serde_json::from_value(payload).map_err(|_| invalid("Invalid CLI arguments"))?;
Expand All @@ -37,22 +44,33 @@ pub fn plan_launch_json(payload: Value) -> Result<Value, RuleError> {
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", ""),
[arg] if !literal && (arg == "--version" || arg == "-v") => ("version", ""),
[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)]
Expand Down Expand Up @@ -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"] {
Expand Down
34 changes: 34 additions & 0 deletions rust/crates/core-rules/tests/cli_fixture.rs
Original file line number Diff line number Diff line change
@@ -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"]
);
}
}
}
2 changes: 1 addition & 1 deletion rust/crates/diagnostics/src/catalog_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 35 additions & 1 deletion rust/fixtures/application/cli-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"launch": [
{
"args": [],
"action": "welcome"
"action": "welcome",
"handOff": true
},
{
"args": [
Expand All @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion rust/logging/events/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading