Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cli): deny a subagent's permission prompt when nobody can answer (#9259)

Thinh Leb28fefe12393
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rs
index 42b973747..429c484e7 100644
--- a/src-tauri/src/core/cli/mod.rs
+++ b/src-tauri/src/core/cli/mod.rs
@@ -2915,6 +2915,14 @@ async fn resolve_permission_silently(
registry: &PermissionRegistry,
duplex: bool,
) -> Option<(String, PermissionDecision)> {
+ // A background subagent's prompt arrives wrapped in `Subagent` (see
+ // `StreamEvent::Subagent`) and waits on the same registry as the parent's, so
+ // it needs the same answer. Left unanswered it parks the run forever: the
+ // loop is waiting on a reply no one was ever going to send.
+ let (from, ev) = match ev {
+ StreamEvent::Subagent { name, event, .. } => (Some(name), *event),
+ other => (None, other),
+ };
let StreamEvent::PermissionRequest {
request_id,
tool_name,
@@ -2933,7 +2941,7 @@ async fn resolve_permission_silently(
return None;
}
let detail = permission_detail(command, path, justification);
- let decision = prompt_permission(tool_name, capability, detail).await;
+ let decision = prompt_permission(tool_name, capability, detail, from).await;
if let Some(sender) = registry.lock().await.remove(&request_id) {
let _ = sender.send(decision);
}
@@ -3271,6 +3279,11 @@ async fn print_event(ev: StreamEvent, registry: &PermissionRegistry, duplex: boo
} => eprintln!(
"\x1b[2m[subagent:{name}] [retry] {reason}; attempt {attempt}/{max_attempts} in {delay_ms}ms\x1b[0m"
),
+ // The same prompt the parent's request gets: auto-denied with no
+ // terminal, answered on stdin when a client owns the decision.
+ permission @ StreamEvent::PermissionRequest { .. } => {
+ text_permission(permission, Some(name), registry, duplex).await
+ }
_ => {}
},
StreamEvent::Done { stop_reason, usage } => {
@@ -3289,30 +3302,8 @@ async fn print_event(ev: StreamEvent, registry: &PermissionRegistry, duplex: boo
StreamEvent::TodoUpdate { .. } => {}
// The event collector adopts this history before it reaches the printer.
StreamEvent::MessagesUpdated { .. } => {}
- StreamEvent::PermissionRequest {
- request_id,
- tool_name,
- capability,
- path,
- command,
- diff,
- justification,
- ..
- } => {
- let detail = permission_detail(command, path, justification);
- if let Some(diff) = diff {
- eprintln!("\x1b[2m{diff}\x1b[0m");
- }
- if duplex {
- eprintln!(
- "\x1b[33m[permission] {capability} via '{tool_name}'{detail} - awaiting '{request_id}' on stdin\x1b[0m"
- );
- return;
- }
- let decision = prompt_permission(tool_name, capability, detail).await;
- if let Some(sender) = registry.lock().await.remove(&request_id) {
- let _ = sender.send(decision);
- }
+ permission @ StreamEvent::PermissionRequest { .. } => {
+ text_permission(permission, None, registry, duplex).await
}
// Only a host process can answer this, and only over the duplex
// channel; a text-format run cannot declare host tools at all (the
@@ -3334,6 +3325,53 @@ async fn print_event(ev: StreamEvent, registry: &PermissionRegistry, duplex: boo
}
}
+/// ` from subagent 'name'` for a prompt a subagent raised, empty for the
+/// parent's own, so a person at the terminal can tell whose call they are
+/// being asked to approve.
+fn from_subagent(from: Option<&str>) -> String {
+ from.map(|name| format!(" from subagent '{name}'"))
+ .unwrap_or_default()
+}
+
+/// The text format's handling of a permission prompt: shown with its diff,
+/// then either left to a client on stdin (`duplex`) or answered at the terminal
+/// (denied with none). `from` names the subagent that raised it, if any.
+async fn text_permission(
+ ev: StreamEvent,
+ from: Option<String>,
+ registry: &PermissionRegistry,
+ duplex: bool,
+) {
+ let StreamEvent::PermissionRequest {
+ request_id,
+ tool_name,
+ capability,
+ path,
+ command,
+ diff,
+ justification,
+ ..
+ } = ev
+ else {
+ return;
+ };
+ let detail = permission_detail(command, path, justification);
+ if let Some(diff) = diff {
+ eprintln!("\x1b[2m{diff}\x1b[0m");
+ }
+ if duplex {
+ let origin = from_subagent(from.as_deref());
+ eprintln!(
+ "\x1b[33m[permission] {capability} via '{tool_name}'{detail}{origin} - awaiting '{request_id}' on stdin\x1b[0m"
+ );
+ return;
+ }
+ let decision = prompt_permission(tool_name, capability, detail, from).await;
+ if let Some(sender) = registry.lock().await.remove(&request_id) {
+ let _ = sender.send(decision);
+ }
+}
+
/// What a headless permission line names after the tool: the command or the
/// path, then an escalation's reason, so a user approving a command outside
/// the sandbox reads why the model wants it.
@@ -3359,14 +3397,16 @@ async fn prompt_permission(
tool_name: String,
capability: String,
detail: String,
+ from: Option<String>,
) -> PermissionDecision {
use std::io::IsTerminal;
+ let origin = from_subagent(from.as_deref());
if !std::io::stdin().is_terminal() {
- eprintln!("\x1b[33m[permission] auto-denied {capability} via '{tool_name}' (non-interactive)\x1b[0m");
+ eprintln!("\x1b[33m[permission] auto-denied {capability} via '{tool_name}'{origin} (non-interactive)\x1b[0m");
return PermissionDecision::Deny;
}
tokio::task::spawn_blocking(move || {
- eprint!("\x1b[33m[permission] allow {capability} via '{tool_name}'{detail}? [y/N] \x1b[0m");
+ eprint!("\x1b[33m[permission] allow {capability} via '{tool_name}'{detail}{origin}? [y/N] \x1b[0m");
let _ = std::io::stderr().flush();
let mut line = String::new();
if std::io::stdin().read_line(&mut line).is_err() {
@@ -3398,6 +3438,90 @@ mod tests {
assert_eq!(permission_detail(None, None, None), "");
}
+ /// A prompt a subagent raised, as the printer receives it: wrapped in
+ /// `Subagent`, waiting on the same registry as the parent's own prompts.
+ fn subagent_prompt() -> StreamEvent {
+ StreamEvent::Subagent {
+ run_id: "sub-explore-1".to_string(),
+ name: "explore".to_string(),
+ event: Box::new(StreamEvent::PermissionRequest {
+ request_id: "perm-1".to_string(),
+ tool_name: "write".to_string(),
+ capability: "write".to_string(),
+ path: Some("child.txt".to_string()),
+ command: None,
+ diff: None,
+ prompt_kind: "write".to_string(),
+ offers_always: true,
+ justification: None,
+ }),
+ }
+ }
+
+ /// With no terminal and no client a subagent's prompt has to be denied like
+ /// the parent's own: the child's loop is waiting on that reply, and a run
+ /// that never gets one parks forever. `prompt_permission` would block on a
+ /// developer's terminal, so a test run from one stands down.
+ #[tokio::test]
+ async fn a_subagent_permission_prompt_is_denied_when_nobody_can_answer() {
+ use std::io::IsTerminal;
+ if std::io::stdin().is_terminal() {
+ return;
+ }
+ let registry: PermissionRegistry = Arc::new(Mutex::new(HashMap::new()));
+ let (answer_tx, answer) = tokio::sync::oneshot::channel();
+ registry.lock().await.insert("perm-1".to_string(), answer_tx);
+
+ // A client on stdin owns the decision: nothing is answered here.
+ assert!(resolve_permission_silently(subagent_prompt(), &registry, true)
+ .await
+ .is_none());
+ assert!(registry.lock().await.contains_key("perm-1"));
+
+ let decided = resolve_permission_silently(subagent_prompt(), &registry, false).await;
+ assert_eq!(decided, Some(("perm-1".to_string(), PermissionDecision::Deny)));
+ assert_eq!(
+ tokio::time::timeout(std::time::Duration::from_secs(10), answer)
+ .await
+ .expect("the child's wait was never answered")
+ .expect("the child's wait is answered"),
+ PermissionDecision::Deny
+ );
+ }
+
+ /// The text format reaches the same answer through `print_event`.
+ #[tokio::test]
+ async fn the_text_printer_denies_a_subagent_prompt_when_nobody_can_answer() {
+ use std::io::IsTerminal;
+ if std::io::stdin().is_terminal() {
+ return;
+ }
+ let registry: PermissionRegistry = Arc::new(Mutex::new(HashMap::new()));
+ let (answer_tx, answer) = tokio::sync::oneshot::channel();
+ registry.lock().await.insert("perm-1".to_string(), answer_tx);
+
+ // A client on stdin owns the decision: the request stays pending.
+ print_event(subagent_prompt(), &registry, true).await;
+ assert!(registry.lock().await.contains_key("perm-1"));
+
+ print_event(subagent_prompt(), &registry, false).await;
+ assert_eq!(
+ tokio::time::timeout(std::time::Duration::from_secs(10), answer)
+ .await
+ .expect("the child's wait was never answered")
+ .expect("the child's wait is answered"),
+ PermissionDecision::Deny
+ );
+ }
+
+ /// The prompt says whose call it is, so a person at the terminal is not
+ /// asked to approve a write without knowing a child raised it.
+ #[test]
+ fn a_subagent_prompt_names_the_subagent_and_the_parents_does_not() {
+ assert_eq!(from_subagent(None), "");
+ assert_eq!(from_subagent(Some("explore")), " from subagent 'explore'");
+ }
+
/// The duplex channel end to end over a pipe: a follow-up is queued for the
/// steering handshake, a permission reply reaches the waiting run, a
/// malformed line is survivable, and `abort` stops the reader.