Skip to content

Commit d2efc1e

Browse files
committed
fix(terminal): refresh PATH for new Windows sessions
1 parent 2ea23bb commit d2efc1e

11 files changed

Lines changed: 828 additions & 30 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Static routing guards only: these do not establish live SSH/Peer/relay behavior.
2+
import assert from 'node:assert/strict';
3+
import fs from 'node:fs';
4+
import test from 'node:test';
5+
6+
const read = path => fs.readFileSync(new URL(`../${path}`, import.meta.url), 'utf8');
7+
function between(source, start, end) {
8+
const from = source.indexOf(start);
9+
assert.notEqual(from, -1, `missing start marker: ${start}`);
10+
const to = source.indexOf(end, from + start.length);
11+
assert.notEqual(to, -1, `missing end marker: ${end}`);
12+
return source.slice(from, to);
13+
}
14+
15+
for (const path of [
16+
'src/apps/desktop/src/api/terminal_api.rs',
17+
'src/apps/cli/src/main.rs',
18+
'src/apps/sdk-host/src/runtime.rs',
19+
]) {
20+
test(`static host PATH initialization routing: ${path}`, () => {
21+
const source = read(path);
22+
const policy = between(source, 'if let Ok(runtime_manager) = RuntimeManager::new()', 'TerminalApi::new(');
23+
const windows = between(policy, '#[cfg(windows)]', '#[cfg(not(windows))]');
24+
assert.match(windows, /runtime_manager\.terminal_path_entries\(\)/);
25+
assert.match(windows, /set_session_path_entries\(/);
26+
assert.doesNotMatch(windows, /merged_path_env|std::env::var|\.env\.insert/);
27+
const other = policy.slice(policy.indexOf('#[cfg(not(windows))]'));
28+
assert.match(other, /std::env::var\("PATH"\)\.ok\(\)/);
29+
assert.match(other, /merged_path_env\(process_path\.as_deref\(\)\)/);
30+
assert.match(other, /\.env\.insert\("PATH"\.to_string\(\), path\)/);
31+
});
32+
}
33+
34+
test('static local builder routing keeps normalization Windows-only and reads baseline per create', () => {
35+
const source = read('src/crates/services/terminal/src/session/manager.rs');
36+
const create = between(source, '// The OS PATH is read here', '// Inject shell integration');
37+
assert.match(create, /#\[cfg\(windows\)\]\s*let baseline = session_path_baseline\(\)/);
38+
assert.match(create, /env: self\.build_session_env\(env, baseline\.as_deref\(\)\)/);
39+
const builder = between(source, ' fn build_session_env(', ' /// Inject shell integration scripts');
40+
const windows = between(builder, '#[cfg(windows)]', '#[cfg(not(windows))]');
41+
assert.match(windows, /request_path\.or\(config_path\)\.or_else/);
42+
assert.match(windows, /compose_session_path/);
43+
const other = builder.slice(builder.indexOf('#[cfg(not(windows))]'));
44+
assert.match(builder, /let mut session_env = self\.config\.env\.clone\(\)/);
45+
assert.match(other, /session_env\.extend\(request_env\)/);
46+
assert.doesNotMatch(other, /compose_session_path|to_string_lossy|take_session_path_entries/);
47+
assert.match(read('src/crates/services/terminal/src/config/mod.rs'), /#\[cfg\(windows\)\]\s*mod session_path;/);
48+
});
49+
50+
test('static desktop remote branch returns before local init and never forwards environment', () => {
51+
const source = read('src/apps/desktop/src/api/terminal_api.rs');
52+
const create = between(source, 'pub async fn terminal_create(', 'pub async fn terminal_get(');
53+
const remote = between(create, 'if let Some(connection_id) = request', 'let api = state.get_or_init_api()');
54+
assert.match(remote, /return spawn_remote_pty_session\(/);
55+
assert.doesNotMatch(remote, /request\.env|build_session_env|session_path_baseline|merged_path_env|set_session_path_entries/);
56+
const spawn = between(source, 'async fn spawn_remote_pty_session(', '#[tauri::command]');
57+
assert.doesNotMatch(spawn, /request\.env|build_session_env|session_path_baseline|merged_path_env|set_session_path_entries/);
58+
assert.match(spawn, /terminal_manager\s*\.create_session\(/);
59+
});
60+
61+
test('static remote service ShellConfig keeps an empty local environment overlay', () => {
62+
const source = read('src/crates/services/services-integrations/src/remote_ssh/remote_terminal.rs');
63+
const configs = [...source.matchAll(/let shell_config = ShellConfig\s*\{([\s\S]*?)\n\s*\};/g)];
64+
assert.ok(configs.length > 0, 'remote PTY shell configuration must remain visible');
65+
for (const [, config] of configs) assert.match(config, /env:\s*HashMap::new\(\)/);
66+
assert.doesNotMatch(source, /build_session_env|session_path_baseline|set_session_path_entries|terminal_path_entries|merged_path_env/);
67+
});

‎src/apps/cli/src/main.rs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,23 @@ async fn initialize_terminal_service() {
868868
}
869869
}
870870

871+
// Windows refreshes the OS baseline per session; only directory anchors are
872+
// snapshotted here (new managed installations require host restart).
873+
// Other platforms retain their original process-PATH merge.
871874
if let Ok(runtime_manager) = RuntimeManager::new() {
872-
let current_path = std::env::var("PATH").ok();
873-
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref()) {
874-
terminal_config
875-
.env
876-
.insert("PATH".to_string(), merged_path.clone());
877-
#[cfg(windows)]
878-
{
879-
terminal_config.env.insert("Path".to_string(), merged_path);
875+
#[cfg(windows)]
876+
{
877+
let path_entries = runtime_manager.terminal_path_entries();
878+
terminal_config.set_session_path_entries(
879+
path_entries.managed_prefix,
880+
path_entries.platform_suffix,
881+
);
882+
}
883+
#[cfg(not(windows))]
884+
{
885+
let process_path = std::env::var("PATH").ok();
886+
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
887+
terminal_config.env.insert("PATH".to_string(), path);
880888
}
881889
}
882890
} else {

‎src/apps/desktop/src/api/terminal_api.rs‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,23 @@ impl TerminalState {
6565
}
6666
}
6767

68-
// Prepend OpenBitFun-managed runtime dirs to PATH so Bash/Skill commands can
69-
// run on machines without preinstalled dev tools.
68+
// Windows refreshes the OS baseline per session; only directory anchors are
69+
// snapshotted here (new managed installations require host restart).
70+
// Other platforms retain their original process-PATH merge.
7071
if let Ok(runtime_manager) = RuntimeManager::new() {
71-
let current_path = std::env::var("PATH").ok();
72-
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref())
72+
#[cfg(windows)]
7373
{
74-
config.env.insert("PATH".to_string(), merged_path.clone());
75-
#[cfg(windows)]
76-
{
77-
config.env.insert("Path".to_string(), merged_path);
74+
let path_entries = runtime_manager.terminal_path_entries();
75+
config.set_session_path_entries(
76+
path_entries.managed_prefix,
77+
path_entries.platform_suffix,
78+
);
79+
}
80+
#[cfg(not(windows))]
81+
{
82+
let process_path = std::env::var("PATH").ok();
83+
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
84+
config.env.insert("PATH".to_string(), path);
7885
}
7986
}
8087
}

‎src/apps/sdk-host/src/runtime.rs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,24 @@ pub(crate) async fn initialize_terminal_service() {
108108
}
109109
}
110110

111+
// Windows refreshes the OS baseline per session; only directory anchors are
112+
// snapshotted here (new managed installations require host restart).
113+
// Other platforms retain their original process-PATH merge.
111114
if let Ok(runtime_manager) = RuntimeManager::new() {
112-
let current_path = std::env::var("PATH").ok();
113-
if let Some(merged_path) = runtime_manager.merged_path_env(current_path.as_deref()) {
114-
config.env.insert("PATH".to_string(), merged_path.clone());
115-
#[cfg(windows)]
116-
config.env.insert("Path".to_string(), merged_path);
115+
#[cfg(windows)]
116+
{
117+
let path_entries = runtime_manager.terminal_path_entries();
118+
config.set_session_path_entries(
119+
path_entries.managed_prefix,
120+
path_entries.platform_suffix,
121+
);
122+
}
123+
#[cfg(not(windows))]
124+
{
125+
let process_path = std::env::var("PATH").ok();
126+
if let Some(path) = runtime_manager.merged_path_env(process_path.as_deref()) {
127+
config.env.insert("PATH".to_string(), path);
128+
}
117129
}
118130
} else {
119131
tracing::warn!("Failed to initialize SDK Host terminal runtime PATH");

‎src/crates/assembly/core/src/service/runtime/mod.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use openbitfun_services_core::managed_runtime::ManagedRuntimeResolver;
99
use std::path::{Path, PathBuf};
1010

1111
pub use openbitfun_services_core::managed_runtime::{
12-
ResolvedCommand, RuntimeCommandCapability, RuntimeSource,
12+
ResolvedCommand, RuntimeCommandCapability, RuntimeSource, TerminalPathEntries,
1313
};
1414

1515
#[derive(Debug, Clone)]
@@ -63,6 +63,11 @@ impl RuntimeManager {
6363
self.inner.managed_path_entries()
6464
}
6565

66+
/// PATH priority anchors for newly created terminal sessions.
67+
pub fn terminal_path_entries(&self) -> TerminalPathEntries {
68+
self.inner.terminal_path_entries()
69+
}
70+
6671
pub fn merged_path_env(&self, existing_path: Option<&str>) -> Option<String> {
6772
self.inner.merged_path_env(existing_path)
6873
}
@@ -100,6 +105,10 @@ mod tests {
100105
.managed_path_entries()
101106
.iter()
102107
.any(|p| p == &node_bin));
108+
assert_eq!(
109+
manager.terminal_path_entries().managed_prefix,
110+
manager.managed_path_entries()
111+
);
103112

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

‎src/crates/services/services-core/src/managed_runtime.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ pub struct RuntimeCommandCapability {
3838
pub resolved_path: Option<String>,
3939
}
4040

41+
/// Initialization-time directory anchors for implicit local Windows terminal PATH.
42+
///
43+
/// `managed_prefix` precedes the inherited custom increment and live OS baseline;
44+
/// `platform_suffix` follows it. Explicit config/request PATH overrides this policy.
45+
/// Unlike [`ManagedRuntimeResolver::merged_path_env`], this does not capture the
46+
/// caller's full PATH. Hosts snapshot these directory sets at initialization:
47+
/// managed runtime installations still require host restart; only the Windows OS
48+
/// baseline is refreshed for each new session. Other platforms keep the legacy merge.
49+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
50+
pub struct TerminalPathEntries {
51+
/// OpenBitFun-managed runtime directories, highest priority.
52+
pub managed_prefix: Vec<PathBuf>,
53+
/// Platform GUI extras the OS PATH does not carry, lowest priority.
54+
pub platform_suffix: Vec<PathBuf>,
55+
}
56+
4157
#[derive(Debug, Clone)]
4258
pub struct ManagedRuntimeResolver {
4359
runtime_root: PathBuf,
@@ -187,6 +203,17 @@ impl ManagedRuntimeResolver {
187203
.map(|v| v.to_string_lossy().to_string())
188204
}
189205

206+
/// Directory anchors captured by Windows hosts at terminal initialization.
207+
///
208+
/// This does not capture OS PATH. Only that baseline is refreshed per session;
209+
/// new managed installations require restarting the host to refresh these sets.
210+
pub fn terminal_path_entries(&self) -> TerminalPathEntries {
211+
TerminalPathEntries {
212+
managed_prefix: self.managed_path_entries(),
213+
platform_suffix: system::platform_path_entries(),
214+
}
215+
}
216+
190217
fn resolve_system_command(&self, command: &str) -> Option<ResolvedCommand> {
191218
let check = system::check_command(command);
192219
if !check.exists {
@@ -409,4 +436,34 @@ mod tests {
409436

410437
let _ = fs::remove_dir_all(root);
411438
}
439+
440+
#[test]
441+
fn terminal_path_entries_carry_owner_policy_without_process_path() {
442+
let root = temp_runtime_root();
443+
let node_bin = root.join("node").join("current").join("bin");
444+
let node_root = root.join("node").join("current");
445+
fs::create_dir_all(&node_bin).unwrap();
446+
fs::create_dir_all(&node_root).unwrap();
447+
448+
let manager = ManagedRuntimeResolver::new(root.clone());
449+
let entries = manager.terminal_path_entries();
450+
let platform_entries = system::platform_path_entries();
451+
452+
assert_eq!(entries.managed_prefix, manager.managed_path_entries());
453+
assert_eq!(entries.platform_suffix, platform_entries);
454+
assert!(entries.managed_prefix.contains(&node_bin));
455+
assert!(entries.managed_prefix.contains(&node_root));
456+
457+
// Not a process PATH snapshot: entries are temp-root managed dirs or platform extras.
458+
assert!(
459+
entries.managed_prefix.iter().all(|path| path.starts_with(&root))
460+
&& entries
461+
.platform_suffix
462+
.iter()
463+
.all(|path| platform_entries.contains(path)),
464+
"session path policy must not carry caller PATH entries"
465+
);
466+
467+
let _ = fs::remove_dir_all(root);
468+
}
412469
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Configuration types for the terminal module
22
3+
#[cfg(windows)]
4+
mod session_path;
35
mod types;
46

7+
#[cfg(windows)]
8+
pub(crate) use session_path::*;
59
pub use types::*;

0 commit comments

Comments
 (0)