Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent): name a collaborating subagent and stop the phantom-peer wait
Akarshan Biswasc80876a4b971
diff --git a/src-tauri/src/core/agent/context.rs b/src-tauri/src/core/agent/context.rsindex cb4350179..77dd7527f 100644--- a/src-tauri/src/core/agent/context.rs+++ b/src-tauri/src/core/agent/context.rs@@ -145,12 +145,15 @@ it finishes -- keep working rather than waiting. Do inline work yourself for smadelegating would cost more than it saves.";/// The worker half of the work-queue guide, shown to every collaborating agent-/// (main and workers), so a worker knows the claim -> do -> complete -> stop loop-/// even though it cannot dispatch.+/// (main and workers). It leads with the direct-task rule: a subagent handed a+/// concrete task must do that task rather than drain the queue or wait on a+/// peer that isn't there -- the loop a lone subagent otherwise fell into.const WORK_QUEUE_WORKER_GUIDE: &str = "# Shared work queue\n\nYou share a work queue and a live status \-board with the other agents on this run. As a worker: call `claim_work` to take the next ready task, do \-it, then `complete_work` with the result. If `claim_work` reports nothing is ready, stop -- do not loop \-on it; you are re-dispatched when new work appears.";+board with the other agents on this run. If your instructions give you a task, do that task yourself -- \+do not wait on the queue for it. Use `claim_work` only when you were dispatched as a generic worker with \+no task of your own: claim the next ready item, do it, then `complete_work` with the result. If \+`claim_work` reports nothing is ready, stop -- do not loop on it and do not wait for a peer; you are \+re-dispatched when new work appears.";/// The dispatcher half, added only for a run that may dispatch subagents (main),/// so it learns the post -> dispatch-workers -> chain-with-deps pattern and the@@ -536,6 +539,24 @@ mod tests {let _ = std::fs::remove_dir_all(&root);}+ #[test]+ fn work_queue_worker_guide_leads_with_the_direct_task_rule() {+ let root = scratch_project("workqueue-guide");+ let off = build_system_prompt(None, &root, None, false, false).expect("prompt");+ assert!(!off.contains("Shared work queue"));+ // A worker (dispatch disabled): worker guide present, dispatch half not.+ let worker = build_system_prompt(None, &root, None, false, true).expect("prompt");+ assert!(worker.contains("Shared work queue"));+ assert!(worker.contains("do that task yourself"));+ assert!(worker.contains("do not wait for a peer"));+ assert!(!worker.contains("dispatch a generic subagent"));+ // Main (dispatch enabled) also learns the fan-out half.+ let main = build_system_prompt(None, &root, None, true, true).expect("prompt");+ assert!(main.contains("do not wait for a peer"));+ assert!(main.contains("post_work"));+ let _ = std::fs::remove_dir_all(&root);+ }+// ── Runtime environment block ────────────────────────────────────────#[test]diff --git a/src-tauri/src/core/agent/subagent.rs b/src-tauri/src/core/agent/subagent.rsindex 94eb35d17..735a1e9a1 100644--- a/src-tauri/src/core/agent/subagent.rs+++ b/src-tauri/src/core/agent/subagent.rs@@ -909,6 +909,21 @@ fn child_body(serde_json::Value::Object(body)}+/// The child's system prompt: the definition's role, plus a one-line identity+/// when collaboration is on. Without its own name a subagent cannot tell it is+/// a lone agent, so the worker guide's queue loop reads as an invitation to wait+/// on a peer that isn't there. The behavioural rule (do your direct task, don't+/// wait on the queue) lives in the shared worker guide, not here.+fn child_system_prompt(role: &str, name: &str, work_queue_enabled: bool) -> String {+ let mut prompt = role.to_string();+ if work_queue_enabled {+ prompt.push_str(&format!(+ "\n\n# You\n\nYou are the subagent `{name}`, one of the agents on this run."+ ));+ }+ prompt+}+/// Run one resolved subagent to completion, wrapping its events for `run_id` and/// returning its final assistant text. Isolated: fresh history (`description`),/// the definition's system prompt, narrowed tools, dispatch disabled.@@ -925,7 +940,11 @@ async fn run_subagent(let name = resolved.definition.name.clone();let mut child_args = parent_args;- child_args.system_prompt_override = Some(resolved.definition.system_prompt.clone());+ child_args.system_prompt_override = Some(child_system_prompt(+ &resolved.definition.system_prompt,+ &name,+ child_args.work_queue_enabled,+ ));child_args.subagents_enabled = false;// A subagent's own interactive question (if any) belongs to its parent's// conversation, not a client waiting on this child's ask_requests -- and@@ -1917,6 +1936,21 @@ mod tests {assert_eq!(off["send_reasoning"], serde_json::json!(false));}+ /// A collaborating subagent has to know its own name, or the shared worker+ /// guide reads as an invitation to wait on a peer that isn't there. Off the+ /// queue there are no peers, so no identity line is added.+ #[test]+ fn child_system_prompt_names_the_subagent_only_when_collaborating() {+ let solo = child_system_prompt("You review code.", "kv-review", false);+ assert_eq!(solo, "You review code.");+ let collab = child_system_prompt("You review code.", "kv-review", true);+ assert!(collab.starts_with("You review code."));+ assert!(+ collab.contains("subagent `kv-review`"),+ "identity names the child: {collab}"+ );+ }+#[test]fn resolve_unknown_name_without_inline_prompt_errors() {let reg = registry_with("reviewer", None);diff --git a/web-app/src/lib/__tests__/coworkPrompt.test.ts b/web-app/src/lib/__tests__/coworkPrompt.test.tsindex e83149cc4..05d758bf8 100644--- a/web-app/src/lib/__tests__/coworkPrompt.test.ts+++ b/web-app/src/lib/__tests__/coworkPrompt.test.ts@@ -171,6 +171,16 @@ describe('buildSubagentSystemPrompt', () => {expect(out).toContain('cannot dispatch')})+ // Parity with the CLI's child_system_prompt: a named child knows it is a lone+ // agent on one errand, not a worker that waits on the shared queue.+ it('names the child in its scope when a name is given', () => {+ const named = buildSubagentSystemPrompt('p', opts, 'kv-review')+ expect(named).toContain('subagent `kv-review`, running one errand')+ const anon = buildSubagentSystemPrompt('p', opts)+ expect(anon).toContain('a subagent running one errand')+ expect(anon).not.toContain('subagent `')+ })+it('never leaks plan mode, a subagent roster, or memory into a child', () => {const out = buildSubagentSystemPrompt('p', opts)expect(out).not.toContain('PLAN MODE')diff --git a/web-app/src/lib/coworkPrompt.ts b/web-app/src/lib/coworkPrompt.tsindex dacb0c326..484c032a8 100644--- a/web-app/src/lib/coworkPrompt.ts+++ b/web-app/src/lib/coworkPrompt.ts@@ -197,8 +197,15 @@ export function buildSubagentSystemPrompt(definitionPrompt: string,// No memory catalog: a child runs one stated errand, so recall is the// dispatching agent's job -- it reads the note and states what matters.- opts: Omit<CoworkPromptOptions, 'planMode' | 'subagentNames' | 'memoryCatalog'>+ opts: Omit<CoworkPromptOptions, 'planMode' | 'subagentNames' | 'memoryCatalog'>,+ // The child's own name, so it can tell it is a lone agent on one errand+ // rather than a worker that should wait on the shared queue (parity with the+ // CLI's `child_system_prompt`).+ name?: string): string {+ const who = name+ ? `You are the subagent \`${name}\`, running one errand.`+ : 'You are a subagent running one errand.'return [definitionPrompt.trim(),...(opts.environment ? [environmentBlock(opts.environment)] : []),@@ -207,7 +214,7 @@ export function buildSubagentSystemPrompt(['# Scope','',- 'You are a subagent running one errand. You cannot see the conversation',+ `${who} You cannot see the conversation`,'that dispatched you, cannot ask the user questions, and cannot dispatch','subagents of your own. Your final message is the whole answer returned to','the agent that called you, so make it self-contained.',diff --git a/web-app/src/lib/coworkSubagent.ts b/web-app/src/lib/coworkSubagent.tsindex 88ef68df7..b079d75d0 100644--- a/web-app/src/lib/coworkSubagent.ts+++ b/web-app/src/lib/coworkSubagent.ts@@ -502,14 +502,18 @@ export async function runSubagent(events.onStart()const tools = subagentTools(opts.parentTools, resolved.allowedTools)- const system = buildSubagentSystemPrompt(resolved.systemPrompt, {- workspacePath: opts.system.workspacePath,- readOnlyFolder: opts.system.readOnlyFolder,- bashAvailable: opts.system.bashAvailable && 'bash' in tools,- // Derived, not passed: the intersection above may have dropped them.- webSearch: 'web_search' in tools,- environment: opts.system.environment,- })+ const system = buildSubagentSystemPrompt(+ resolved.systemPrompt,+ {+ workspacePath: opts.system.workspacePath,+ readOnlyFolder: opts.system.readOnlyFolder,+ bashAvailable: opts.system.bashAvailable && 'bash' in tools,+ // Derived, not passed: the intersection above may have dropped them.+ webSearch: 'web_search' in tools,+ environment: opts.system.environment,+ },+ resolved.name+ )// A fresh history: the child does not see the parent's conversation, so the// description is the whole brief.