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(agent): hand off ownership of a delegated task so the parent stops redoing it

Akarshan Biswasc69fc078ac12
diff --git a/src-tauri/src/core/agent/context.rs b/src-tauri/src/core/agent/context.rs
index 77dd7527f..64e98c659 100644
--- a/src-tauri/src/core/agent/context.rs
+++ b/src-tauri/src/core/agent/context.rs
@@ -141,8 +141,9 @@ that could pull in a lot of file content or tool output (broad codebase search,
multi-step research), prefer `dispatch_subagent` over doing it inline: the subagent absorbs that context \
in its own window and returns only the distilled answer. Dispatch independent subagents in parallel when \
their work doesn't depend on each other; each returns in the background and a note carries its answer when \
-it finishes -- keep working rather than waiting. Do inline work yourself for small, targeted tasks where \
-delegating would cost more than it saves.";
+it finishes. Once you delegate a task it belongs to that subagent -- do not do the same work yourself; \
+spend the wait on other steps, and only `await_subagent` when nothing else is left to do. Do inline work \
+yourself for small, targeted tasks where delegating would cost more than it saves.";
/// The worker half of the work-queue guide, shown to every collaborating agent
/// (main and workers). It leads with the direct-task rule: a subagent handed a
@@ -539,6 +540,19 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}
+ // A delegated task is the child's: the guide must say the dispatcher should
+ // not also do it itself, or a main agent with nothing else queued redoes the
+ // very work it just handed off.
+ #[test]
+ fn subagent_guide_hands_off_ownership_of_a_delegated_task() {
+ let root = scratch_project("subagent-handoff");
+ let with = build_system_prompt(None, &root, None, true, false).expect("prompt");
+ assert!(with.contains("belongs to that subagent"));
+ assert!(with.contains("do not do the same work yourself"));
+ assert!(!with.contains("keep working rather than waiting"));
+ 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");
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rs
index 719857afc..282a226b3 100644
--- a/src-tauri/src/core/agent/loop.rs
+++ b/src-tauri/src/core/agent/loop.rs
@@ -937,14 +937,16 @@ impl CompositeToolInvoker {
) {
Ok(d) => match d.display_path {
Some(path) => format!(
- "Subagent started in the background. run_id={}. Its answer will be \
- written to {path}; you will be told when it lands, so keep working \
- rather than waiting, and read that file when you need the answer.",
+ "Subagent started in the background. run_id={}. This task is now the \
+ subagent's -- do not do it yourself. Its answer will be written to \
+ {path}; you will be told when it lands, so work on other steps \
+ meanwhile and read that file when you need the answer.",
d.run_id
),
None => format!(
- "Subagent started in the background. run_id={}. Keep working; you will \
- be told when it finishes, and that note carries its answer.",
+ "Subagent started in the background. run_id={}. This task is now the \
+ subagent's -- do not do it yourself. Work on other steps; you will be \
+ told when it finishes, and that note carries its answer.",
d.run_id
),
},
diff --git a/web-app/src/lib/__tests__/coworkPrompt.test.ts b/web-app/src/lib/__tests__/coworkPrompt.test.ts
index 05d758bf8..8f11e71a0 100644
--- a/web-app/src/lib/__tests__/coworkPrompt.test.ts
+++ b/web-app/src/lib/__tests__/coworkPrompt.test.ts
@@ -129,6 +129,13 @@ describe('buildCoworkSystemPrompt', () => {
expect(p).toMatch(/prefer a subagent for long or repetitive jobs/i)
})
+ // Parity with the CLI's SUBAGENT_GUIDE: a delegated task is the child's, so
+ // the dispatcher must be told not to redo it while the child runs.
+ it('tells the dispatcher a handed-off task is the subagent to do, not itself', () => {
+ const p = buildCoworkSystemPrompt(opts({ subagentNames: ['researcher'] }))
+ expect(p).toMatch(/do not do that same\s+work yourself/i)
+ })
+
it('describes subagents only when some are available and not planning', () => {
expect(
buildCoworkSystemPrompt(opts({ subagentNames: ['researcher'] }))
diff --git a/web-app/src/lib/coworkPrompt.ts b/web-app/src/lib/coworkPrompt.ts
index 484c032a8..2f68e8b37 100644
--- a/web-app/src/lib/coworkPrompt.ts
+++ b/web-app/src/lib/coworkPrompt.ts
@@ -176,6 +176,9 @@ export function buildCoworkSystemPrompt(opts: CoworkPromptOptions): string {
'research sweep, working through many files, generating and then',
'verifying a large output. Delegating keeps your own context focused on',
'the plan, and a backgrounded subagent works while you continue.',
+ 'Once you hand a task to a subagent it is theirs -- do not do that same',
+ 'work yourself; spend the time on other steps and use its answer when it',
+ 'arrives.',
`Available: ${opts.subagentNames.join(', ')}.`,
].join('\n')
)