Skip to main content

supercode_harness/
mail_route.rs

1//! One route for every message: which door reaches a receiver, and delivery
2//! through it.
3//!
4//! Every caller that delivers mail — `supercode message send`,
5//! `harness.v1.sessions.message`, and idle notices — goes through
6//! [`door_for`] and [`deliver`], so the choice of door is made in one place.
7//! The doors, best first:
8//!
9//! 1. **runtime** — a session supercode controls (a hosted runtime of any
10//!    harness): its own `steer`/`send_input` ([`crate::runtime_mail`]). This
11//!    is the default tier.
12//! 2. **native** — a Claude Code session supercode does not control: a Claude
13//!    relay sends with Claude's own `SendMessage` ([`crate::claude_relay`]).
14//! 3. **hook** — a Codex session supercode does not control, with
15//!    supercode's hooks in its hooks file: filed in its mailbox, and the hook
16//!    points the session at it.
17//! 4. **stored** — no door: filed in its mailbox, seen only when the session
18//!    reads it.
19//!
20//! Tiers 2–4 are the degradation tier, for sessions supercode does not
21//! control. An operator (a board, a script) is not a session: its mail is
22//! filed in its mailbox and read with `sessions.inbox`.
23
24use std::path::Path;
25
26use crate::claude_peer::{read_registry, registry_dir, ClaudePeerSession, ClaudePeerStatus};
27use crate::claude_relay::{send_through_relay, supercode_program, RelayReceipt, RELAY_NAME_PREFIX};
28use crate::live_runtime::LiveRuntimeRecord;
29use crate::mailbox::{
30    local_machine_name, mail_root, Envelope, IdleSubscription, MailAddress, Mailbox, ReplyVia,
31};
32use crate::runtime_mail::{deliver_to_runtime, RuntimeDelivery};
33use crate::HarnessHomes;
34
35/// Arguments a Codex hook entry carries, which is how its presence is found.
36pub const CODEX_HOOK_ARGUMENTS: &str = "message hook codex";
37
38/// The door that reaches one receiver.
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum Door {
41    /// A session supercode controls.
42    Runtime(Box<LiveRuntimeRecord>),
43    /// A Claude Code session supercode does not control.
44    Native(Box<ClaudePeerSession>),
45    /// A Codex session supercode does not control, with supercode's hooks:
46    /// they show it its mailbox at its next tool call or when its turn ends.
47    /// An idle one in a daemon pane is woken through its pane.
48    Hook {
49        /// The daemon pane it runs in.
50        pane: Option<String>,
51        /// Whether its last turn has ended.
52        idle: bool,
53    },
54    /// A running session with no door.
55    Stored,
56    /// An operator's mailbox.
57    Operator,
58}
59
60impl Door {
61    /// Stable name of the door, as `message list` and receipts show it.
62    pub fn name(&self) -> &'static str {
63        match self {
64            Self::Runtime(_) => "runtime",
65            Self::Native(_) => "native",
66            Self::Hook { .. } => "hook",
67            Self::Stored => "stored",
68            Self::Operator => "operator",
69        }
70    }
71}
72
73/// Why no door reaches an address on this machine.
74#[derive(Debug, Clone, PartialEq, Eq)]
75pub enum NoDoor {
76    /// The address names another machine.
77    OtherMachine(String),
78    /// No running session has that address.
79    NotRunning,
80}
81
82/// The door that reaches `to` on this machine.
83pub fn door_for(homes: &HarnessHomes, to: &MailAddress) -> Result<Door, NoDoor> {
84    if to.machine != local_machine_name() {
85        return Err(NoDoor::OtherMachine(to.machine.clone()));
86    }
87    if to.harness == "operator" || to.harness == "board" {
88        return Ok(Door::Operator);
89    }
90    LiveSessions::read(homes)
91        .sessions
92        .into_iter()
93        .find(|session| &session.address == to)
94        .map(|session| session.door)
95        .ok_or(NoDoor::NotRunning)
96}
97
98/// One running session on this machine, as the router reaches it.
99#[derive(Debug, Clone, PartialEq, Eq)]
100pub struct LiveSession {
101    /// Its address.
102    pub address: MailAddress,
103    /// The name an agent knows it by (`volter-2c@machine`).
104    pub name: String,
105    /// Its status as its harness reports it (`busy`, `idle`, `hosted`, …).
106    pub status: String,
107    /// The door that reaches it.
108    pub door: Door,
109    /// The process running it, when the harness names one (a hosted
110    /// runtime's host, a Claude session's own process).
111    pub pid: Option<u32>,
112    /// Its working directory.
113    pub cwd: Option<std::path::PathBuf>,
114    /// The tmux session and pane it runs in, when Claude records one.
115    pub tmux: Option<String>,
116    /// Its transcript, when the harness names the file (a Codex rollout).
117    pub transcript: Option<std::path::PathBuf>,
118}
119
120impl LiveSession {
121    /// When its transcript last recorded a message (a prompt, a reply, a tool
122    /// call or result), in epoch milliseconds: what the session last did,
123    /// read from the harness's own records. A queued or injected notice is
124    /// not a message, so this is not the transcript's mtime.
125    pub fn last_message_at_ms(&self, homes: &HarnessHomes) -> Option<u64> {
126        let harness = self.address.harness.as_str();
127        let path = match &self.transcript {
128            Some(path) => path.clone(),
129            None if harness == "claude-code" => {
130                let file = format!("{}.jsonl", self.address.session_id);
131                std::fs::read_dir(&homes.claude_code)
132                    .ok()?
133                    .flatten()
134                    .map(|project| project.path().join(&file))
135                    .find(|path| path.is_file())?
136            }
137            None => return None,
138        };
139        last_message_at_ms(&path, harness)
140    }
141}
142
143impl LiveSession {
144    /// What a session waiting on its user is asking, read from its transcript: the newest tool call with no result
145    /// yet. An `AskUserQuestion` gives its questions, headers and option labels; any other tool its name and a
146    /// shortened input. `None` when the session is not waiting or its transcript shows nothing pending.
147    pub fn pending_request(&self, homes: &HarnessHomes) -> Option<serde_json::Value> {
148        if self.status != "waiting" {
149            return None;
150        }
151        // A Codex prompt writes nothing to its rollout: what it asks is read off its pane.
152        if self.address.harness == "codex" {
153            let prompt = daemon_pane(self).and_then(|pane| pane_prompt(&pane))?;
154            return Some(serde_json::json!({"prompt": prompt, "source": "screen"}));
155        }
156        if self.address.harness != "claude-code" {
157            return None;
158        }
159        let file = format!("{}.jsonl", self.address.session_id);
160        let from_transcript = std::fs::read_dir(&homes.claude_code)
161            .ok()
162            .and_then(|projects| {
163                projects
164                    .flatten()
165                    .map(|project| project.path().join(&file))
166                    .find(|path| path.is_file())
167            })
168            .and_then(|path| pending_request(&path));
169        // Claude Code writes a question's tool call to its transcript only once it is answered
170        // (2.1.280): until then, what it asks is on its pane.
171        from_transcript.or_else(|| {
172            let prompt = daemon_pane(self).and_then(|pane| pane_prompt(&pane))?;
173            Some(serde_json::json!({"prompt": prompt, "source": "screen"}))
174        })
175    }
176}
177
178/// The newest tool call in a Claude transcript that has no result yet, as [`LiveSession::pending_request`] shows it.
179pub fn pending_request(path: &Path) -> Option<serde_json::Value> {
180    use std::io::{Read, Seek, SeekFrom};
181    let mut file = std::fs::File::open(path).ok()?;
182    let length = file.metadata().ok()?.len();
183    let start = length.saturating_sub(512 * 1024);
184    file.seek(SeekFrom::Start(start)).ok()?;
185    let mut bytes = Vec::new();
186    file.read_to_end(&mut bytes).ok()?;
187    let text = String::from_utf8_lossy(&bytes);
188    let mut answered = std::collections::HashSet::new();
189    for line in text.lines().rev() {
190        let Ok(record) = serde_json::from_str::<serde_json::Value>(line) else {
191            continue;
192        };
193        if record["isSidechain"] == true {
194            continue;
195        }
196        let Some(content) = record
197            .pointer("/message/content")
198            .and_then(serde_json::Value::as_array)
199        else {
200            continue;
201        };
202        match record["type"].as_str() {
203            Some("user") => {
204                for item in content.iter().filter(|item| item["type"] == "tool_result") {
205                    if let Some(id) = item["tool_use_id"].as_str() {
206                        answered.insert(id.to_string());
207                    }
208                }
209            }
210            Some("assistant") => {
211                let Some(call) = content.iter().rev().find(|item| item["type"] == "tool_use")
212                else {
213                    continue;
214                };
215                if call["id"].as_str().is_some_and(|id| answered.contains(id)) {
216                    return None;
217                }
218                let tool = call["name"].as_str().unwrap_or_default();
219                if tool == "AskUserQuestion" {
220                    let questions = call["input"]["questions"]
221                        .as_array()
222                        .map(|questions| {
223                            questions
224                                .iter()
225                                .map(|question| {
226                                    serde_json::json!({
227                                        "question": question["question"],
228                                        "header": question["header"],
229                                        "options": question["options"].as_array().map(|options| options.iter().map(|option| option["label"].clone()).collect::<Vec<_>>()).unwrap_or_default(),
230                                    })
231                                })
232                                .collect::<Vec<_>>()
233                        })
234                        .unwrap_or_default();
235                    return Some(serde_json::json!({"tool": tool, "questions": questions}));
236                }
237                let input = call["input"].to_string();
238                let input: String = input.chars().take(500).collect();
239                return Some(serde_json::json!({"tool": tool, "input": input}));
240            }
241            _ => {}
242        }
243    }
244    None
245}
246
247/// The newest message record's timestamp in a Claude or Codex transcript.
248fn last_message_at_ms(path: &Path, harness: &str) -> Option<u64> {
249    use std::io::{Read, Seek, SeekFrom};
250    // A tool result can be large; widen the window once before giving up.
251    for window in [256 * 1024_u64, 8 * 1024 * 1024] {
252        let mut file = std::fs::File::open(path).ok()?;
253        let length = file.metadata().ok()?.len();
254        let start = length.saturating_sub(window);
255        file.seek(SeekFrom::Start(start)).ok()?;
256        let mut bytes = Vec::new();
257        file.read_to_end(&mut bytes).ok()?;
258        let text = String::from_utf8_lossy(&bytes);
259        let mut lines = text.lines().rev().collect::<Vec<_>>();
260        if start > 0 {
261            lines.pop(); // the first line may begin mid-record
262        }
263        for line in lines {
264            let Ok(record) = serde_json::from_str::<serde_json::Value>(line) else {
265                continue;
266            };
267            let message = match harness {
268                "codex" => record["type"] == "response_item",
269                _ => {
270                    matches!(record["type"].as_str(), Some("user" | "assistant"))
271                        && record["isSidechain"] != true
272                }
273            };
274            if !message {
275                continue;
276            }
277            if let Some(at) = record["timestamp"]
278                .as_str()
279                .and_then(supercode_interchange::sidecar::rfc3339_to_ms)
280                .and_then(|at| u64::try_from(at).ok())
281            {
282                return Some(at);
283            }
284        }
285        if start == 0 {
286            return None;
287        }
288    }
289    None
290}
291
292/// Every running session on this machine with its door, read once: what
293/// `message list` shows, what names resolve against, and what discovery
294/// projects onto each discovered session as `delivery`.
295#[derive(Debug, Default)]
296pub struct LiveSessions {
297    sessions: Vec<LiveSession>,
298}
299
300/// Why a receiver named by an agent was not found.
301#[derive(Debug, Clone, PartialEq, Eq)]
302pub enum Unresolved {
303    /// The address names a session that is not running.
304    Stale(String),
305    /// No running session has that name (the text suggests near names).
306    Unknown(String),
307}
308
309impl LiveSessions {
310    /// Read every running session now. Sessions supercode controls come
311    /// first, and a degraded-tier row for the same session is dropped: the
312    /// runtime is the default tier.
313    pub fn read(homes: &HarnessHomes) -> Self {
314        let machine = local_machine_name();
315        let registry = read_registry(&registry_dir(homes));
316        // A hosted Claude session is also in Claude's registry, which knows
317        // its name; a relay is not a session and is not listed.
318        let registered = |record: &crate::live_runtime::LiveRuntimeRecord| {
319            registry.iter().find(|session| {
320                session.session_id == record.source.session_id
321                    || session.session_id == record.runtime_session_id
322            })
323        };
324        let records = crate::runtime_mail::controlled_runtimes();
325        // A runtime supercode hosts says whether a turn runs: its label is that, not only its door.
326        let turns = crate::runtime_mail::runtime_turn_states(&records);
327        let mut sessions: Vec<LiveSession> = records
328            .into_iter()
329            .zip(turns)
330            .filter_map(|(record, turn)| {
331                let registered = registered(&record);
332                if registered.is_some_and(|session| session.name.starts_with(RELAY_NAME_PREFIX)) {
333                    return None;
334                }
335                let address =
336                    MailAddress::new(&machine, &record.source.harness, &record.source.session_id)
337                        .ok()?;
338                let short: String = record.source.session_id.chars().take(8).collect();
339                let name = match registered {
340                    Some(session) if !session.name.is_empty() => session.name.clone(),
341                    _ => format!("{}-{short}", record.source.harness),
342                };
343                Some(LiveSession {
344                    name: format!("{name}@{machine}"),
345                    address,
346                    // `hosted` only when its runtime did not answer: no label rather than a false one.
347                    status: match turn {
348                        Some(crate::frontend::FrontendTurnState::Busy) => "busy".into(),
349                        Some(crate::frontend::FrontendTurnState::Idle) => "idle".into(),
350                        None => "hosted".into(),
351                    },
352                    pid: Some(record.pid),
353                    cwd: Some(record.source.workspace.clone()),
354                    tmux: None,
355                    transcript: None,
356                    door: Door::Runtime(Box::new(record)),
357                })
358            })
359            .collect();
360        let controlled = |address: &MailAddress, sessions: &[LiveSession]| {
361            sessions.iter().any(|session| &session.address == address)
362        };
363        for session in registry {
364            if session.name.starts_with(RELAY_NAME_PREFIX) {
365                continue;
366            }
367            let Ok(address) = MailAddress::new(&machine, "claude-code", &session.session_id) else {
368                continue;
369            };
370            if controlled(&address, &sessions) {
371                continue;
372            }
373            sessions.push(LiveSession {
374                address,
375                name: format!("{}@{machine}", session.name),
376                status: session
377                    .status
378                    .as_ref()
379                    .map(|status| status.as_str().to_string())
380                    .unwrap_or_else(|| "unknown".into()),
381                pid: Some(session.pid),
382                cwd: session.cwd.clone(),
383                tmux: session.tmux.clone(),
384                transcript: None,
385                door: Door::Native(Box::new(session)),
386            });
387        }
388        let user_hook = codex_user_hook_installed();
389        let rollouts = crate::codex_peer::live_rollouts(&homes.codex);
390        let panes = if rollouts.is_empty() {
391            std::collections::HashMap::new()
392        } else {
393            crate::codex_peer::session_panes()
394        };
395        for (path, status) in rollouts {
396            let Some((session_id, None)) = crate::codex_peer::rollout_session(&path) else {
397                continue;
398            };
399            let Ok(address) = MailAddress::new(&machine, "codex", &session_id) else {
400                continue;
401            };
402            if controlled(&address, &sessions) {
403                continue;
404            }
405            let cwd = crate::codex_peer::rollout_cwd(&path);
406            let hooked = user_hook || cwd.as_deref().is_some_and(codex_project_hook_installed);
407            let pane = panes.get(&session_id).cloned();
408            // A Codex turn waiting on an approval or a choice writes nothing to its rollout, which
409            // reads that turn as working; in a daemon pane its screen shows the prompt.
410            let status = match status {
411                crate::codex_peer::CodexPeerStatus::Busy
412                | crate::codex_peer::CodexPeerStatus::Running
413                    if pane.as_deref().and_then(pane_prompt).is_some() =>
414                {
415                    "waiting".to_string()
416                }
417                status => status.as_str().to_string(),
418            };
419            let door = if hooked {
420                Door::Hook {
421                    pane: pane.clone(),
422                    idle: status == "idle",
423                }
424            } else {
425                Door::Stored
426            };
427            sessions.push(LiveSession {
428                name: format!("{}@{machine}", codex_name(&session_id)),
429                address,
430                status,
431                pid: None,
432                cwd,
433                // The daemon pane it runs in, as a Claude session's tmux names its own.
434                tmux: pane,
435                transcript: Some(path),
436                door,
437            });
438        }
439        Self { sessions }
440    }
441
442    /// Every running session.
443    pub fn all(&self) -> &[LiveSession] {
444        &self.sessions
445    }
446
447    /// The door that reaches `harness`'s session `session_id`, when it is
448    /// running.
449    pub fn door(&self, harness: &str, session_id: &str) -> Option<&'static str> {
450        self.sessions
451            .iter()
452            .find(|session| {
453                session.address.harness == harness && session.address.session_id == session_id
454            })
455            .map(|session| session.door.name())
456    }
457
458    /// The running session an agent means by `to`: an address, `name@machine`
459    /// on this machine, or a name unique here.
460    pub fn resolve(&self, to: &str) -> Result<&LiveSession, Unresolved> {
461        let machine = local_machine_name();
462        if let Ok(address) = MailAddress::parse(to) {
463            return self
464                .sessions
465                .iter()
466                .find(|session| session.address == address)
467                .ok_or_else(|| {
468                    Unresolved::Stale(format!(
469                        "{to} is no longer running. Nothing was sent. Run supercode message list \
470                         for the live sessions."
471                    ))
472                });
473        }
474        let wanted = match to.split_once('@') {
475            Some((name, at)) if at == machine => name.to_string(),
476            Some((_, at)) => {
477                return Err(Unresolved::Unknown(format!(
478                    "Not sent: {to} is on machine {at}, not this one. Nothing was sent."
479                )))
480            }
481            None => to.to_string(),
482        };
483        let short = |session: &LiveSession| {
484            session
485                .name
486                .split('@')
487                .next()
488                .unwrap_or_default()
489                .to_string()
490        };
491        let matching: Vec<&LiveSession> = self
492            .sessions
493            .iter()
494            .filter(|session| short(session) == wanted)
495            .collect();
496        if let [only] = matching.as_slice() {
497            return Ok(only);
498        }
499        let hint = if matching.len() > 1 {
500            format!(
501                " {} sessions are named {wanted}; use its address.",
502                matching.len()
503            )
504        } else {
505            let near: Vec<String> = self
506                .sessions
507                .iter()
508                .filter(|session| {
509                    let name = short(session);
510                    name.contains(&wanted)
511                        || wanted.contains(&name)
512                        || name
513                            .chars()
514                            .zip(wanted.chars())
515                            .take_while(|(a, b)| a == b)
516                            .count()
517                            >= 4
518                })
519                .take(3)
520                .map(|session| {
521                    format!(
522                        "{} ({}, {})",
523                        session.name, session.address.harness, session.status
524                    )
525                })
526                .collect();
527            if near.is_empty() {
528                String::new()
529            } else {
530                format!(" Did you mean: {}?", near.join(", "))
531            }
532        };
533        Err(Unresolved::Unknown(format!(
534            "No session named \"{wanted}\" is reachable.{hint} Run supercode message list. Nothing \
535             was sent."
536        )))
537    }
538}
539
540/// Whether the session process `pid` has supercode's messaging tools: a
541/// harness starts each MCP server as a child of the session, so the tools are
542/// loaded exactly when a live `supercode message mcp` is one of its children.
543pub fn has_message_tools(pid: u32) -> bool {
544    let Ok(output) = std::process::Command::new("ps")
545        .args(["-A", "-o", "ppid=,command="])
546        .output()
547    else {
548        return false;
549    };
550    String::from_utf8_lossy(&output.stdout).lines().any(|line| {
551        let line = line.trim_start();
552        let Some((ppid, command)) = line.split_once(' ') else {
553            return false;
554        };
555        ppid.parse::<u32>() == Ok(pid) && command.trim_end().ends_with(" message mcp")
556    })
557}
558
559/// Display name of a Codex conversation: `codex-` and the start of its id.
560pub fn codex_name(session_id: &str) -> String {
561    format!("codex-{}", session_id.chars().take(8).collect::<String>())
562}
563
564/// The session a process belongs to: the sender a message is from.
565#[derive(Debug, Clone, PartialEq, Eq)]
566pub struct Caller {
567    /// Its address, where replies go.
568    pub address: MailAddress,
569    /// The name it is known by.
570    pub name: String,
571}
572
573/// Why no session could be found behind a process.
574pub const CALLER_UNRESOLVED: &str = "Can't tell which session is running this command, so \
575    replies would have nowhere to go. Nothing was sent. Run it from your agent session's own \
576    shell tool.";
577
578/// The session behind a process, from its ancestry `pids` (nearest first):
579/// the first that owns a hosted runtime, a Claude session or a Codex
580/// conversation. Never declared by the caller; an environment id only
581/// corroborates, and a mismatch refuses.
582pub fn resolve_caller(homes: &HarnessHomes, pids: &[u32]) -> Result<Caller, String> {
583    let machine = local_machine_name();
584    let registry = read_registry(&registry_dir(homes));
585    let hosted = crate::runtime_mail::controlled_runtimes();
586    for &pid in pids {
587        if let Some(record) = hosted.iter().find(|record| record.pid == pid) {
588            let short: String = record.source.session_id.chars().take(8).collect();
589            return Ok(Caller {
590                address: MailAddress::new(
591                    &machine,
592                    &record.source.harness,
593                    &record.source.session_id,
594                )
595                .map_err(|error| error.to_string())?,
596                name: format!("{}-{short}@{machine}", record.source.harness),
597            });
598        }
599        if let Some(session) = registry.iter().find(|session| session.pid == pid) {
600            if let Ok(claimed) = std::env::var("CLAUDE_CODE_SESSION_ID") {
601                if !claimed.is_empty() && claimed != session.session_id {
602                    return Err(format!(
603                        "{CALLER_UNRESOLVED} (CLAUDE_CODE_SESSION_ID names {claimed}, but the Claude \
604                         process {pid} above this command is session {})",
605                        session.session_id
606                    ));
607                }
608            }
609            return Ok(Caller {
610                address: MailAddress::new(&machine, "claude-code", &session.session_id)
611                    .map_err(|error| error.to_string())?,
612                name: format!("{}@{machine}", session.name),
613            });
614        }
615        // Codex names the thread a command runs for (`CODEX_THREAD_ID`); the
616        // claim stands when this process holds that thread's rollout, as
617        // Codex's shared app-server daemon does for every thread it runs.
618        if let Some(thread) = std::env::var("CODEX_THREAD_ID")
619            .ok()
620            .filter(|thread| !thread.is_empty())
621            .filter(|thread| crate::codex_peer::holds_session(pid, thread))
622        {
623            return Ok(Caller {
624                address: MailAddress::new(&machine, "codex", &thread)
625                    .map_err(|error| error.to_string())?,
626                name: format!("{}@{machine}", codex_name(&thread)),
627            });
628        }
629        if let Some((session_id, _)) = crate::codex_peer::session_of_process(pid) {
630            return Ok(Caller {
631                address: MailAddress::new(&machine, "codex", &session_id)
632                    .map_err(|error| error.to_string())?,
633                name: format!("{}@{machine}", codex_name(&session_id)),
634            });
635        }
636    }
637    #[cfg(windows)]
638    if let Some(session) = msys_cut_claim(&registry, pids) {
639        return Ok(Caller {
640            address: MailAddress::new(&machine, "claude-code", &session.session_id)
641                .map_err(|error| error.to_string())?,
642            name: format!("{}@{machine}", session.name),
643        });
644    }
645    Err(format!(
646        "{CALLER_UNRESOLVED} (looked for this command's processes {pids:?} among {} Claude \
647         sessions in {} and {} hosted runtimes)",
648        registry.len(),
649        registry_dir(homes).display(),
650        hosted.len()
651    ))
652}
653
654/// Git Bash (MSYS) runs a command in a forked process that hands over to it. When the command is
655/// itself an MSYS program (a `sh` script, as npm's command shims are), the forked process exits once
656/// it has handed over, so the Windows parent chain is cut just above that shell and never reaches
657/// the Claude session running it. When the ancestry ends at exactly such a cut (its last pid is gone,
658/// the one before it is an MSYS shell), the session Claude names in `CLAUDE_CODE_SESSION_ID` stands
659/// if it is a live Claude session on this machine.
660#[cfg(windows)]
661fn msys_cut_claim<'a>(
662    registry: &'a [ClaudePeerSession],
663    pids: &[u32],
664) -> Option<&'a ClaudePeerSession> {
665    let table = process_table();
666    let [.., shell, cut] = pids else {
667        return None;
668    };
669    if table.contains_key(cut) {
670        return None;
671    }
672    let name = table.get(shell)?.1.to_ascii_lowercase();
673    if !matches!(name.as_str(), "sh.exe" | "bash.exe" | "dash.exe") {
674        return None;
675    }
676    let claimed = std::env::var("CLAUDE_CODE_SESSION_ID").ok()?;
677    registry
678        .iter()
679        .find(|session| !claimed.is_empty() && session.session_id == claimed)
680}
681
682/// This process's ancestry, nearest first (itself included).
683pub fn process_ancestry() -> Vec<u32> {
684    ancestry_of(std::process::id())
685}
686
687/// A process's ancestry, nearest first (itself included).
688pub fn ancestry_of(pid: u32) -> Vec<u32> {
689    let parents = parent_pids();
690    let mut chain = vec![pid];
691    let mut current = pid;
692    while let Some(&parent) = parents.get(&current) {
693        if parent <= 1 || chain.contains(&parent) {
694            break;
695        }
696        chain.push(parent);
697        current = parent;
698    }
699    chain
700}
701
702/// Every process's parent: pid → parent pid, from `ps`.
703#[cfg(not(windows))]
704fn parent_pids() -> std::collections::HashMap<u32, u32> {
705    let Ok(output) = std::process::Command::new("ps")
706        .args(["-axo", "pid=,ppid="])
707        .output()
708    else {
709        return Default::default();
710    };
711    String::from_utf8_lossy(&output.stdout)
712        .lines()
713        .filter_map(|line| {
714            let mut fields = line.split_whitespace();
715            Some((fields.next()?.parse().ok()?, fields.next()?.parse().ok()?))
716        })
717        .collect()
718}
719
720/// Every process's parent: pid → parent pid, from a process snapshot.
721#[cfg(windows)]
722fn parent_pids() -> std::collections::HashMap<u32, u32> {
723    process_table()
724        .into_iter()
725        .map(|(pid, (parent, _))| (pid, parent))
726        .collect()
727}
728
729/// Every process: pid → (parent pid, executable file name), from a process snapshot.
730#[cfg(windows)]
731fn process_table() -> std::collections::HashMap<u32, (u32, String)> {
732    use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
733    use windows_sys::Win32::System::Diagnostics::ToolHelp::{
734        CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W,
735        TH32CS_SNAPPROCESS,
736    };
737
738    let mut parents = std::collections::HashMap::new();
739    let snapshot = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) };
740    if snapshot == INVALID_HANDLE_VALUE {
741        return parents;
742    }
743    let mut entry: PROCESSENTRY32W = unsafe { std::mem::zeroed() };
744    entry.dwSize = std::mem::size_of::<PROCESSENTRY32W>() as u32;
745    let mut has_entry = unsafe { Process32FirstW(snapshot, &mut entry) } != 0;
746    while has_entry {
747        let length = entry
748            .szExeFile
749            .iter()
750            .position(|&unit| unit == 0)
751            .unwrap_or(entry.szExeFile.len());
752        let name = String::from_utf16_lossy(&entry.szExeFile[..length]);
753        parents.insert(entry.th32ProcessID, (entry.th32ParentProcessID, name));
754        has_entry = unsafe { Process32NextW(snapshot, &mut entry) } != 0;
755    }
756    unsafe {
757        CloseHandle(snapshot);
758    }
759    parents
760}
761
762/// Codex's user-level hooks file.
763pub fn codex_hooks_path() -> std::path::PathBuf {
764    std::env::var_os("CODEX_HOME")
765        .map(std::path::PathBuf::from)
766        .or_else(|| {
767            supercode_interchange::user_home()
768                .map(std::path::PathBuf::into_os_string)
769                .map(|home| std::path::PathBuf::from(home).join(".codex"))
770        })
771        .unwrap_or_else(|| std::path::PathBuf::from(".codex"))
772        .join("hooks.json")
773}
774
775/// Whether supercode's mail hook is in Codex's user hooks file. (Codex runs
776/// it only once its user has trusted it.)
777pub fn codex_user_hook_installed() -> bool {
778    std::fs::read_to_string(codex_hooks_path())
779        .is_ok_and(|text| text.contains(CODEX_HOOK_ARGUMENTS))
780}
781
782/// Whether supercode's mail hook is in a project's Codex hooks file, in the
783/// session's directory or one above it.
784pub fn codex_project_hook_installed(cwd: &Path) -> bool {
785    cwd.ancestors().any(|directory| {
786        std::fs::read_to_string(directory.join(".codex").join("hooks.json"))
787            .is_ok_and(|text| text.contains(CODEX_HOOK_ARGUMENTS))
788    })
789}
790
791/// How a delivery ended.
792#[derive(Debug, Clone, PartialEq, Eq)]
793pub enum Delivered {
794    /// The receiver has it now: steered into a running turn.
795    Steered,
796    /// The receiver has it now: it started a turn in an idle session.
797    Started,
798    /// Claude reported it in the receiver's inbox; `busy` says whether the
799    /// receiver reads it at its next tool call (true) or it starts a turn.
800    Native {
801        /// True when the receiver was in a turn.
802        busy: bool,
803    },
804    /// Filed in the receiver's mailbox, which a hook points it at.
805    Hooked,
806    /// Filed in an idle receiver's mailbox, and its pane told to read it: a
807    /// turn has started, in which its hook shows it the message.
808    HookWoken,
809    /// Filed in the receiver's mailbox, waiting to be read.
810    Queued,
811    /// Filed with no door to show it.
812    Stored,
813    /// Filed in an operator's mailbox.
814    Operator,
815}
816
817/// A delivery refused before anything was sent.
818#[derive(Debug, Clone, PartialEq, Eq)]
819pub enum Refused {
820    /// `--queue` cannot hold a message for an idle Claude session: Claude
821    /// starts a turn for every message it delivers.
822    CannotQueueNative,
823    /// The relayed text would not fit one relay turn.
824    TooLong(usize),
825}
826
827/// Largest message relayed into a Claude session. The relay copies it into a
828/// model turn byte for byte, so it must fit comfortably in one.
829pub const MAX_RELAYED_BYTES: usize = 100_000;
830
831/// Deliver `envelope` to `to` through `door`. With `wake` false an idle
832/// receiver is not started. With `notify_when_idle` the sender gets one idle
833/// notice after the receiver's next turn ends.
834pub async fn deliver(
835    envelope: &Envelope,
836    to: &MailAddress,
837    door: &Door,
838    wake: bool,
839    notify_when_idle: bool,
840) -> Result<Result<Delivered, Refused>, String> {
841    let mailbox = Mailbox::open(&mail_root(), to).map_err(|error| error.to_string())?;
842    // A runtime answers with its turn's final message, which the watcher
843    // sends back: not every runtime can run a command (a hosted agent may
844    // have no shell), and each can end a turn.
845    let mut final_reply = false;
846    let delivered = match door {
847        Door::Runtime(record) => {
848            let mut sent = envelope.clone();
849            let answers = matches!(envelope.reply_via, ReplyVia::Command);
850            if answers {
851                sent.reply_via = ReplyVia::FinalMessage {
852                    destination: envelope.from_name.clone(),
853                };
854            }
855            match deliver_to_runtime(record, sent.render(), wake).await? {
856                RuntimeDelivery::Steered => {
857                    mailbox
858                        .deliver_read(&sent)
859                        .map_err(|error| error.to_string())?;
860                    final_reply = answers;
861                    Delivered::Steered
862                }
863                RuntimeDelivery::Started => {
864                    mailbox
865                        .deliver_read(&sent)
866                        .map_err(|error| error.to_string())?;
867                    final_reply = answers;
868                    Delivered::Started
869                }
870                RuntimeDelivery::NotWoken => {
871                    mailbox
872                        .deliver(envelope)
873                        .map_err(|error| error.to_string())?;
874                    Delivered::Queued
875                }
876            }
877        }
878        Door::Native(session) => {
879            let busy = session.status != Some(ClaudePeerStatus::Idle);
880            if !wake && !busy {
881                return Ok(Err(Refused::CannotQueueNative));
882            }
883            // The same envelope every door delivers; Claude wraps it in its
884            // own, which names the relay. Its reply line names the door this
885            // session really has: its tools when it runs supercode's MCP server.
886            let mut envelope = envelope.clone();
887            if envelope.reply_via == ReplyVia::Command && has_message_tools(session.pid) {
888                envelope.reply_via = ReplyVia::Tool;
889            }
890            let envelope = &envelope;
891            let text = envelope.render();
892            if text.len() > MAX_RELAYED_BYTES {
893                return Ok(Err(Refused::TooLong(text.len())));
894            }
895            match send_through_relay(
896                &envelope.from,
897                &envelope.from_name,
898                &session.name,
899                text,
900                &envelope.id,
901            )
902            .await
903            {
904                RelayReceipt::Delivered { .. } => {
905                    mailbox
906                        .deliver_read(envelope)
907                        .map_err(|error| error.to_string())?;
908                    Delivered::Native { busy }
909                }
910                RelayReceipt::Failed { detail } => return Err(detail),
911            }
912        }
913        Door::Hook { pane, idle } => {
914            mailbox
915                .deliver(envelope)
916                .map_err(|error| error.to_string())?;
917            // its hooks run only inside a turn: an idle session is told, in its pane, to read its mailbox; the
918            // message itself reaches it as mail, never as its user's words
919            match pane {
920                Some(pane)
921                    if wake
922                        && *idle
923                        && submit_when_composer_empty(pane, CODEX_WAKE).await == Ok(true) =>
924                {
925                    Delivered::HookWoken
926                }
927                _ => Delivered::Hooked,
928            }
929        }
930        Door::Stored => {
931            mailbox
932                .deliver(envelope)
933                .map_err(|error| error.to_string())?;
934            Delivered::Stored
935        }
936        Door::Operator => {
937            mailbox
938                .deliver(envelope)
939                .map_err(|error| error.to_string())?;
940            Delivered::Operator
941        }
942    };
943    let notice = notify_when_idle && !matches!(door, Door::Operator);
944    if notice || final_reply {
945        let mut subscription = IdleSubscription::new(envelope.id.clone(), envelope.from.clone());
946        subscription.notice = notice;
947        subscription.final_reply = final_reply;
948        mailbox
949            .subscribe_idle(&subscription)
950            .map_err(|error| error.to_string())?;
951        // The watcher that settles it runs beside the machine daemon.
952        if let Ok(program) = supercode_program() {
953            crate::claude_relay::ensure_machine_daemon(&program)
954                .await
955                .ok();
956        }
957    }
958    Ok(Ok(delivered))
959}
960
961/// How the user's own turn reached its session.
962#[derive(Debug, Clone, Copy, PartialEq, Eq)]
963pub enum UserTurn {
964    /// A hosted runtime was in a turn; the words were steered into it.
965    Steered,
966    /// A hosted runtime was idle; the words started a turn.
967    Started,
968    /// Typed into the session's pane as its own submitted turn.
969    Typed,
970    /// The pane's composer holds a draft, or its program is not at its
971    /// composer: the turn waits in the session's mailbox and is typed once
972    /// the composer is empty.
973    Waiting,
974}
975
976impl UserTurn {
977    /// Stable wire spelling.
978    pub const fn as_str(self) -> &'static str {
979        match self {
980            Self::Steered => "steered",
981            Self::Started => "started",
982            Self::Typed => "typed",
983            Self::Waiting => "waiting",
984        }
985    }
986}
987
988/// The daemon pane a session runs in, when it runs in one.
989/// The choice or permission prompt a daemon pane's screen shows, as its lines (the question above
990/// it and its options), or `None`. The terminal substrate's own check (`classifyAttentionSignal`):
991/// a selector (❯ or ›) on a numbered option is a prompt blocked on an answer; the idle input line
992/// has no number after its selector.
993pub fn pane_prompt(pane: &str) -> Option<Vec<String>> {
994    #[cfg(unix)]
995    {
996        let output = std::process::Command::new("tmux")
997            .args(["capture-pane", "-p", "-t", &format!("{pane}:")])
998            .output()
999            .ok()?;
1000        if !output.status.success() {
1001            return None;
1002        }
1003        let screen = String::from_utf8_lossy(&output.stdout);
1004        let lines: Vec<&str> = screen.lines().map(str::trim_end).collect();
1005        let is_option = |line: &str| {
1006            let line = line.trim_start();
1007            let rest = line
1008                .strip_prefix('❯')
1009                .or_else(|| line.strip_prefix('›'))
1010                .unwrap_or(line)
1011                .trim_start();
1012            let digits = rest.chars().take_while(char::is_ascii_digit).count();
1013            digits > 0 && matches!(rest[digits..].chars().next(), Some('.' | ')'))
1014        };
1015        let selected = lines.iter().position(|line| {
1016            let line = line.trim_start();
1017            (line.starts_with('❯') || line.starts_with('›')) && is_option(line)
1018        })?;
1019        // The prompt: the options' block of non-empty lines and the block above it (the question).
1020        let block_start = |end: usize| {
1021            lines[..end]
1022                .iter()
1023                .rposition(|line| line.trim().is_empty())
1024                .map_or(0, |blank| blank + 1)
1025        };
1026        let options = block_start(selected);
1027        let above = lines[..options]
1028            .iter()
1029            .rposition(|line| !line.trim().is_empty())
1030            .map(|last| block_start(last));
1031        let start = above.unwrap_or(options);
1032        let end = lines[selected..]
1033            .iter()
1034            .position(|line| line.trim().is_empty())
1035            .map_or(lines.len(), |blank| selected + blank);
1036        Some(
1037            lines[start..end]
1038                .iter()
1039                .map(|line| line.trim().to_string())
1040                .filter(|line| !line.is_empty())
1041                .take(20)
1042                .collect(),
1043        )
1044    }
1045    #[cfg(not(unix))]
1046    {
1047        let _ = pane;
1048        None
1049    }
1050}
1051
1052pub fn daemon_pane(session: &LiveSession) -> Option<String> {
1053    let name = session.tmux.as_deref()?.split(':').next()?;
1054    let rest = name.strip_prefix("p_")?;
1055    (!rest.is_empty()
1056        && rest
1057            .chars()
1058            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'))
1059    .then(|| name.to_string())
1060}
1061
1062/// Deliver the user's own turn to `to` through the one door that carries the
1063/// user's authority: a hosted runtime's own input, or the session's pane.
1064/// A session with neither (running outside supercode) is refused, never
1065/// reached as a peer instead. Callers hold the owner's authority already:
1066/// this is reached only through owner doors (the machine's own harness.v1).
1067pub async fn deliver_user_turn(
1068    homes: &HarnessHomes,
1069    envelope: &Envelope,
1070    to: &MailAddress,
1071) -> Result<UserTurn, String> {
1072    let session = LiveSessions::read(homes)
1073        .sessions
1074        .into_iter()
1075        .find(|session| &session.address == to)
1076        .ok_or_else(|| format!("{to} is not running; nothing was sent"))?;
1077    let mailbox = Mailbox::open(&mail_root(), to).map_err(|error| error.to_string())?;
1078    if let Door::Runtime(record) = &session.door {
1079        let delivered = deliver_to_runtime(record, envelope.body.clone(), true).await?;
1080        mailbox
1081            .deliver_read(envelope)
1082            .map_err(|error| error.to_string())?;
1083        return Ok(match delivered {
1084            RuntimeDelivery::Steered => UserTurn::Steered,
1085            _ => UserTurn::Started,
1086        });
1087    }
1088    let Some(pane) = daemon_pane(&session) else {
1089        let name = session.name.split('@').next().unwrap_or(&session.name);
1090        return Err(format!(
1091            "{name} runs outside supercode, where nothing can speak as its user. Open it in a \
1092             pane with `supercode open {name}`; nothing was sent."
1093        ));
1094    };
1095    mailbox
1096        .deliver(envelope)
1097        .map_err(|error| error.to_string())?;
1098    let typed = type_user_turns(&mailbox, &pane).await;
1099    Ok(if typed.contains(&envelope.id) {
1100        UserTurn::Typed
1101    } else {
1102        UserTurn::Waiting
1103    })
1104}
1105
1106/// Type the user's waiting turns into `pane`, oldest first, each only when
1107/// the pane's composer is empty. Stops at the first that has to wait.
1108/// Returns the ids typed.
1109pub async fn type_user_turns(mailbox: &Mailbox, pane: &str) -> Vec<String> {
1110    let mut typed = Vec::new();
1111    for waiting in mailbox.user_turns().unwrap_or_default() {
1112        // Claimed before it is typed: the machine's watcher and a direct delivery both type waiting turns, and
1113        // two typers filling one composer sent a turn's text twice in one prompt. A turn another typer holds is
1114        // that typer's, and so is every turn after it.
1115        let Ok(Some(stored)) = mailbox.claim_user_turn(&waiting) else {
1116            break;
1117        };
1118        match submit_when_composer_empty(pane, &stored.envelope.body).await {
1119            Ok(true) => {
1120                mailbox.acknowledge(&stored).ok();
1121                typed.push(stored.envelope.id.clone());
1122            }
1123            Ok(false) => {
1124                mailbox.release(&stored).ok();
1125                break;
1126            }
1127            Err(error) => {
1128                mailbox.release(&stored).ok();
1129                eprintln!(
1130                    "supercode: the user's turn {} for {} waits: {error}",
1131                    stored.envelope.id,
1132                    mailbox.address()
1133                );
1134                break;
1135            }
1136        }
1137    }
1138    typed
1139}
1140
1141/// What an idle hooked session's pane is given, to start the turn in which
1142/// its hook shows it the waiting mail.
1143pub const CODEX_WAKE: &str =
1144    "You have unread supercode messages. Read them with: supercode message inbox";
1145
1146/// The daemon's pane door: type `text` as a submitted turn if the composer
1147/// is empty now. `Ok(false)` when it holds a draft or is not on screen.
1148async fn submit_when_composer_empty(pane: &str, text: &str) -> Result<bool, String> {
1149    let entry = crate::teams_entry().map_err(|error| error.to_string())?;
1150    let node = std::env::var(crate::orchestrator_door::NODE_BIN_ENV)
1151        .ok()
1152        .filter(|value| !value.trim().is_empty())
1153        .unwrap_or_else(|| "node".into());
1154    let output = tokio::process::Command::new(node)
1155        .arg(entry)
1156        .args(["input", pane, text, "--when-composer-empty"])
1157        // A message is the session's own door, not a session acting on a pane: the pane doors read that from this
1158        // process's place under the daemon's `message watch`, not from anything named here.
1159        .env_remove("SUPERCODE_CALLER")
1160        .stdin(std::process::Stdio::null())
1161        .output()
1162        .await
1163        .map_err(|error| error.to_string())?;
1164    if !output.status.success() {
1165        return Err(crate::mailbox::error_line(&String::from_utf8_lossy(
1166            &output.stderr,
1167        )));
1168    }
1169    let answer: serde_json::Value =
1170        serde_json::from_slice(&output.stdout).map_err(|error| error.to_string())?;
1171    Ok(answer["delivered"] == true)
1172}