Skip to content
Draft
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
67 changes: 67 additions & 0 deletions scripts/terminal-live-path.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Static routing guards only: these do not establish live SSH/Peer/relay behavior.
import assert from 'node:assert/strict';
import fs from 'node:fs';
import test from 'node:test';

const read = path => fs.readFileSync(new URL(`../${path}`, import.meta.url), 'utf8');
function between(source, start, end) {
const from = source.indexOf(start);
assert.notEqual(from, -1, `missing start marker: ${start}`);
const to = source.indexOf(end, from + start.length);
assert.notEqual(to, -1, `missing end marker: ${end}`);
return source.slice(from, to);
}

for (const path of [
'src/apps/desktop/src/api/terminal_api.rs',
'src/apps/cli/src/main.rs',
'src/apps/sdk-host/src/runtime.rs',
]) {
test(`static host PATH initialization routing: ${path}`, () => {
const source = read(path);
const policy = between(source, 'if let Ok(runtime_manager) = RuntimeManager::new()', 'TerminalApi::new(');
const windows = between(policy, '#[cfg(windows)]', '#[cfg(not(windows))]');
assert.match(windows, /runtime_manager\.terminal_path_entries\(\)/);
assert.match(windows, /set_session_path_entries\(/);
assert.doesNotMatch(windows, /merged_path_env|std::env::var|\.env\.insert/);
const other = policy.slice(policy.indexOf('#[cfg(not(windows))]'));
assert.match(other, /std::env::var\("PATH"\)\.ok\(\)/);
assert.match(other, /merged_path_env\(process_path\.as_deref\(\)\)/);
assert.match(other, /\.env\.insert\("PATH"\.to_string\(\), path\)/);
});
}

test('static local builder routing keeps normalization Windows-only and reads baseline per create', () => {
const source = read('src/crates/services/terminal/src/session/manager.rs');
const create = between(source, '// The OS PATH is read here', '// Inject shell integration');
assert.match(create, /#\[cfg\(windows\)\]\s*let baseline = session_path_baseline\(\)/);
assert.match(create, /env: self\.build_session_env\(env, baseline\.as_deref\(\)\)/);
const builder = between(source, ' fn build_session_env(', ' /// Inject shell integration scripts');
const windows = between(builder, '#[cfg(windows)]', '#[cfg(not(windows))]');
assert.match(windows, /request_path\.or\(config_path\)\.or_else/);
assert.match(windows, /compose_session_path/);
const other = builder.slice(builder.indexOf('#[cfg(not(windows))]'));
assert.match(builder, /let mut session_env = self\.config\.env\.clone\(\)/);
assert.match(other, /session_env\.extend\(request_env\)/);
assert.doesNotMatch(other, /compose_session_path|to_string_lossy|take_session_path_entries/);
assert.match(read('src/crates/services/terminal/src/config/mod.rs'), /#\[cfg\(windows\)\]\s*mod session_path;/);
});

test('static desktop remote branch returns before local init and never forwards environment', () => {
const source = read('src/apps/desktop/src/api/terminal_api.rs');
const create = between(source, 'pub async fn terminal_create(', 'pub async fn terminal_get(');
const remote = between(create, 'if let Some(connection_id) = request', 'let api = state.get_or_init_api()');
assert.match(remote, /return spawn_remote_pty_session\(/);
assert.doesNotMatch(remote, /request\.env|build_session_env|session_path_baseline|merged_path_env|set_session_path_entries/);
const spawn = between(source, 'async fn spawn_remote_pty_session(', '#[tauri::command]');
assert.doesNotMatch(spawn, /request\.env|build_session_env|session_path_baseline|merged_path_env|set_session_path_entries/);
assert.match(spawn, /terminal_manager\s*\.create_session\(/);
});

test('static remote service ShellConfig keeps an empty local environment overlay', () => {
const source = read('src/crates/services/services-integrations/src/remote_ssh/remote_terminal.rs');
const configs = [...source.matchAll(/let shell_config = ShellConfig\s*\{([\s\S]*?)\n\s*\};/g)];
assert.ok(configs.length > 0, 'remote PTY shell configuration must remain visible');
for (const [, config] of configs) assert.match(config, /env:\s*HashMap::new\(\)/);
assert.doesNotMatch(source, /build_session_env|session_path_baseline|set_session_path_entries|terminal_path_entries|merged_path_env/);
});
24 changes: 16 additions & 8 deletions src/apps/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,23 @@ async fn initialize_terminal_service() {
}
}

// Windows refreshes the OS baseline per session; only directory anchors are
// snapshotted here (new managed installations require host restart).
// Other platforms retain their original process-PATH merge.
if let Ok(runtime_manager) = RuntimeManager::new() {
let current_path = std::env::var("PATH").ok();
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref()) {
terminal_config
.env
.insert("PATH".to_string(), merged_path.clone());
#[cfg(windows)]
{
terminal_config.env.insert("Path".to_string(), merged_path);
#[cfg(windows)]
{
let path_entries = runtime_manager.terminal_path_entries();
terminal_config.set_session_path_entries(
path_entries.managed_prefix,
path_entries.platform_suffix,
);
}
#[cfg(not(windows))]
{
let process_path = std::env::var("PATH").ok();
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
terminal_config.env.insert("PATH".to_string(), path);
}
}
} else {
Expand Down
23 changes: 15 additions & 8 deletions src/apps/desktop/src/api/terminal_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,23 @@ impl TerminalState {
}
}

// Prepend OpenBitFun-managed runtime dirs to PATH so Bash/Skill commands can
// run on machines without preinstalled dev tools.
// Windows refreshes the OS baseline per session; only directory anchors are
// snapshotted here (new managed installations require host restart).
// Other platforms retain their original process-PATH merge.
if let Ok(runtime_manager) = RuntimeManager::new() {
let current_path = std::env::var("PATH").ok();
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref())
#[cfg(windows)]
{
config.env.insert("PATH".to_string(), merged_path.clone());
#[cfg(windows)]
{
config.env.insert("Path".to_string(), merged_path);
let path_entries = runtime_manager.terminal_path_entries();
config.set_session_path_entries(
path_entries.managed_prefix,
path_entries.platform_suffix,
);
}
#[cfg(not(windows))]
{
let process_path = std::env::var("PATH").ok();
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
config.env.insert("PATH".to_string(), path);
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/apps/sdk-host/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,24 @@ pub(crate) async fn initialize_terminal_service() {
}
}

// Windows refreshes the OS baseline per session; only directory anchors are
// snapshotted here (new managed installations require host restart).
// Other platforms retain their original process-PATH merge.
if let Ok(runtime_manager) = RuntimeManager::new() {
let current_path = std::env::var("PATH").ok();
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref()) {
config.env.insert("PATH".to_string(), merged_path.clone());
#[cfg(windows)]
config.env.insert("Path".to_string(), merged_path);
#[cfg(windows)]
{
let path_entries = runtime_manager.terminal_path_entries();
config.set_session_path_entries(
path_entries.managed_prefix,
path_entries.platform_suffix,
);
}
#[cfg(not(windows))]
{
let process_path = std::env::var("PATH").ok();
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
config.env.insert("PATH".to_string(), path);
}
}
} else {
tracing::warn!("Failed to initialize SDK Host terminal runtime PATH");
Expand Down
11 changes: 10 additions & 1 deletion src/crates/assembly/core/src/service/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use openbitfun_services_core::managed_runtime::ManagedRuntimeResolver;
use std::path::{Path, PathBuf};

pub use openbitfun_services_core::managed_runtime::{
ResolvedCommand, RuntimeCommandCapability, RuntimeSource,
ResolvedCommand, RuntimeCommandCapability, RuntimeSource, TerminalPathEntries,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -63,6 +63,11 @@ impl RuntimeManager {
self.inner.managed_path_entries()
}

/// PATH priority anchors for newly created terminal sessions.
pub fn terminal_path_entries(&self) -> TerminalPathEntries {
self.inner.terminal_path_entries()
}

pub fn merged_path_env(&self, existing_path: Option<&str>) -> Option<String> {
self.inner.merged_path_env(existing_path)
}
Expand Down Expand Up @@ -100,6 +105,10 @@ mod tests {
.managed_path_entries()
.iter()
.any(|p| p == &node_bin));
assert_eq!(
manager.terminal_path_entries().managed_prefix,
manager.managed_path_entries()
);

let _ = fs::remove_dir_all(root);
}
Expand Down
57 changes: 57 additions & 0 deletions src/crates/services/services-core/src/managed_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ pub struct RuntimeCommandCapability {
pub resolved_path: Option<String>,
}

/// Initialization-time directory anchors for implicit local Windows terminal PATH.
///
/// `managed_prefix` precedes the inherited custom increment and live OS baseline;
/// `platform_suffix` follows it. Explicit config/request PATH overrides this policy.
/// Unlike [`ManagedRuntimeResolver::merged_path_env`], this does not capture the
/// caller's full PATH. Hosts snapshot these directory sets at initialization:
/// managed runtime installations still require host restart; only the Windows OS
/// baseline is refreshed for each new session. Other platforms keep the legacy merge.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TerminalPathEntries {
/// OpenBitFun-managed runtime directories, highest priority.
pub managed_prefix: Vec<PathBuf>,
/// Platform GUI extras the OS PATH does not carry, lowest priority.
pub platform_suffix: Vec<PathBuf>,
}

#[derive(Debug, Clone)]
pub struct ManagedRuntimeResolver {
runtime_root: PathBuf,
Expand Down Expand Up @@ -187,6 +203,17 @@ impl ManagedRuntimeResolver {
.map(|v| v.to_string_lossy().to_string())
}

/// Directory anchors captured by Windows hosts at terminal initialization.
///
/// This does not capture OS PATH. Only that baseline is refreshed per session;
/// new managed installations require restarting the host to refresh these sets.
pub fn terminal_path_entries(&self) -> TerminalPathEntries {
TerminalPathEntries {
managed_prefix: self.managed_path_entries(),
platform_suffix: system::platform_path_entries(),
}
}

fn resolve_system_command(&self, command: &str) -> Option<ResolvedCommand> {
let check = system::check_command(command);
if !check.exists {
Expand Down Expand Up @@ -409,4 +436,34 @@ mod tests {

let _ = fs::remove_dir_all(root);
}

#[test]
fn terminal_path_entries_carry_owner_policy_without_process_path() {
let root = temp_runtime_root();
let node_bin = root.join("node").join("current").join("bin");
let node_root = root.join("node").join("current");
fs::create_dir_all(&node_bin).unwrap();
fs::create_dir_all(&node_root).unwrap();

let manager = ManagedRuntimeResolver::new(root.clone());
let entries = manager.terminal_path_entries();
let platform_entries = system::platform_path_entries();

assert_eq!(entries.managed_prefix, manager.managed_path_entries());
assert_eq!(entries.platform_suffix, platform_entries);
assert!(entries.managed_prefix.contains(&node_bin));
assert!(entries.managed_prefix.contains(&node_root));

// Not a process PATH snapshot: entries are temp-root managed dirs or platform extras.
assert!(
entries.managed_prefix.iter().all(|path| path.starts_with(&root))
&& entries
.platform_suffix
.iter()
.all(|path| platform_entries.contains(path)),
"session path policy must not carry caller PATH entries"
);

let _ = fs::remove_dir_all(root);
}
}
4 changes: 4 additions & 0 deletions src/crates/services/terminal/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Configuration types for the terminal module

#[cfg(windows)]
mod session_path;
mod types;

#[cfg(windows)]
pub(crate) use session_path::*;
pub use types::*;
Loading
Loading