Skip to main content

Mountain/IPC/WindServiceHandlers/Terminal/
AttachToProcess.rs

1
2//! `localPty:attachToProcess` - reconnect the workbench to an existing
3//! Mountain-owned PTY after a window reload.
4//!
5//! VS Code calls this when the workbench learns that a terminal ID from the
6//! previous session is already live (via `localPty:getTerminalLayoutInfo` or
7//! `localPty:reviveTerminalProcesses`). Instead of spawning a new PTY it
8//! attaches to the one that Mountain kept running.
9//!
10//! Wire shape: `Arguments[0]` = id (u64)
11//!
12//! Returns `{ id, pid }` on success or `null` when the id is unknown.
13
14use std::sync::Arc;
15
16use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
17use serde_json::{Value, json};
18
19use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
20
21pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
22	let TerminalId = match Arguments.first() {
23		Some(Value::Number(N)) => N.as_u64().unwrap_or(0),
24
25		Some(Value::Object(Obj)) => Obj.get("id").and_then(Value::as_u64).unwrap_or(0),
26
27		_ => 0,
28	};
29
30	if TerminalId == 0 {
31		dev_log!("terminal", "warn: [AttachToProcess] called with id=0, ignoring");
32
33		return Ok(Value::Null);
34	}
35
36	let Provider:Arc<dyn TerminalProvider> = RunTime.Environment.Require();
37
38	// Fetch the PID so the workbench can bind its tooltip + debug adapter.
39	// If the terminal no longer exists the provider returns None.
40	match Provider.GetTerminalProcessId(TerminalId).await {
41		Ok(Some(Pid)) => {
42			dev_log!("terminal", "[AttachToProcess] attached id={} pid={}", TerminalId, Pid);
43
44			Ok(json!({ "id": TerminalId, "pid": Pid }))
45		},
46
47		Ok(None) => {
48			dev_log!(
49				"terminal",
50				"warn: [AttachToProcess] id={} not found in active terminals",
51				TerminalId
52			);
53
54			Ok(Value::Null)
55		},
56
57		Err(Error) => {
58			dev_log!("terminal", "warn: [AttachToProcess] id={} error: {}", TerminalId, Error);
59
60			Ok(Value::Null)
61		},
62	}
63}