Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent): stop runaway work-queue polling and refuse self-reads
Akarshan Biswasf7199959ea36
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.tsindex d3e200dd9..79e0e38e5 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts+++ b/src-tauri/plugins/tauri-plugin-agent-tools/guest-js/index.ts@@ -352,12 +352,14 @@ export async function workList(threadId: string): Promise<WorkCommandResult> {}/** Read a peer's status + transcript, or the roster (no `run_id`). `args` is- * `{run_id?, tail?}`; the returned string is model-facing. */+ * `{run_id?, tail?}`; the returned string is model-facing. `agentId` is the+ * caller's own collaboration id, so the core can refuse a self-read. */export async function agentRead(threadId: string,+ agentId: string,args: Record<string, unknown>): Promise<string> {- return await invoke('plugin:agent-tools|agent_read', { threadId, args })+ return await invoke('plugin:agent-tools|agent_read', { threadId, agentId, args })}/** An attachment copied into a session workspace. */diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rsindex 17f1792d5..34612bc49 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/commands.rs@@ -533,16 +533,20 @@ pub async fn work_list(thread_id: String) -> Result<WorkCommandResult, AgentTool/// Read a peer's status + transcript, or the roster. `args` is `{run_id?, tail?}`./// Returns the model-facing text; a JS-supplied `run_id` is re-validated inside-/// the core (`is_agent_id`).+/// the core (`is_agent_id`). `agent_id` is the caller's own id, so the core can+/// refuse an agent reading its own log (a no-op loop).#[tauri::command]pub async fn agent_read(thread_id: String,+ agent_id: String,args: serde_json::Value,) -> Result<String, AgentToolsError> {let scratch = workspace::ensure_scratch_dir(&thread_id).await?;- tokio::task::spawn_blocking(move || crate::tools::observ::run_read_agent(&scratch, &args))- .await- .map_err(|e| AgentToolsError::from(format!("agent read failed: {e}")))+ tokio::task::spawn_blocking(move || {+ crate::tools::observ::run_read_agent(&scratch, &args, &agent_id)+ })+ .await+ .map_err(|e| AgentToolsError::from(format!("agent read failed: {e}")))}/// An attachment imported into a session workspace: host paths, which are alsodiff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/observ.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/observ.rsindex 72d97af3c..a7e8aa0c7 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/observ.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/observ.rs@@ -330,14 +330,23 @@ pub fn read_agent_tool_schema() -> serde_json::Value {}/// Run a `read_agent` call and format its model-facing result. Shared by the-/// CLI loop and the desktop/Cowork command layer.-pub fn run_read_agent(scratch: &Path, args: &serde_json::Value) -> String {+/// CLI loop and the desktop/Cowork command layer. `self_id` is the caller's own+/// collaboration id (`"main"` or a `sub-...` run id): an agent reading its own+/// log is a no-op loop -- the last line it gets back is the `read_agent` call it+/// just made -- so any surface passes its caller here and the read is refused.+pub fn run_read_agent(scratch: &Path, args: &serde_json::Value, self_id: &str) -> String {let run_id = args.get("run_id").and_then(|v| v.as_str()).map(str::trim).filter(|s| !s.is_empty());match run_id {+ Some(id) if id == self_id => {+ "ERROR: that run_id is you. read_agent inspects *other* agents; reading your own log \+ tells you nothing. If your task is done, report your result (call complete_work, or \+ give your final answer) instead of polling."+ .to_string()+ }None => {let r = roster(scratch);if r.is_empty() {@@ -542,6 +551,24 @@ mod tests {let _ = std::fs::remove_dir_all(&scratch);}+ #[test]+ fn refuses_reading_your_own_log() {+ let scratch = tmp("selfread");+ assert!(write_status(&scratch, &AgentStatusView::new("sub-a-1", "a", 100)));+ // A peer can read it.+ let peer = run_read_agent(&scratch, &serde_json::json!({ "run_id": "sub-a-1" }), "main");+ assert!(peer.contains("sub-a-1"), "{peer}");+ // Reading yourself is refused, whoever you are.+ let own = run_read_agent(&scratch, &serde_json::json!({ "run_id": "sub-a-1" }), "sub-a-1");+ assert!(own.starts_with("ERROR") && own.contains("you"), "{own}");+ let main = run_read_agent(&scratch, &serde_json::json!({ "run_id": "main" }), "main");+ assert!(main.starts_with("ERROR"), "main cannot read itself either: {main}");+ // The roster (no run_id) is not a self-read and still works.+ let roster = run_read_agent(&scratch, &serde_json::json!({}), "sub-a-1");+ assert!(roster.contains("Agents"), "{roster}");+ let _ = std::fs::remove_dir_all(&scratch);+ }+#[test]fn roster_lists_every_agent_header() {let scratch = tmp("roster");diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rsindex 8c1e96f55..1da6068cf 100644--- a/src-tauri/src/core/agent/loop.rs+++ b/src-tauri/src/core/agent/loop.rs@@ -204,6 +204,31 @@ pub(crate) trait ToolInvoker: Send + Sync {/// agent's `status.json` shows an accurate live step even on a turn that/// calls no tools. Default no-op for invokers with no observability.fn record_step(&self, _index: u32) {}++ /// Whether this run is a dispatched worker (a `sub-...` collaboration id)+ /// rather than the top-level agent. Only a child may be hard-stopped by the+ /// poll dampener: ending the main run would abort the very children it is+ /// waiting on. Default false, so a non-collaborating run is never a child.+ fn collab_is_child(&self) -> bool {+ false+ }+}++/// Work-queue/observability tools that read shared state without doing any work+/// or producing a result. A run whose turns consist only of these is polling,+/// not progressing -- the stuck-worker failure the dampener in [`run_turn_cycle`]+/// catches. `complete_work`/`post_work`/`dispatch_subagent` and every file/exec+/// tool are productive and reset the streak.+fn is_poll_only_tool(name: &str) -> bool {+ matches!(name, "claim_work" | "list_work" | "read_agent")+}++/// The `function.name` of a tool call, or `""` when absent.+fn tool_call_fn_name(tc: &serde_json::Value) -> &str {+ tc.get("function")+ .and_then(|f| f.get("name"))+ .and_then(|v| v.as_str())+ .unwrap_or("")}/// Emit [`StreamEvent::Monitors`] when the set differs from what was last@@ -806,7 +831,9 @@ impl CompositeToolInvoker {return "ERROR: the work queue is not available in this run".to_string();};if name == observ::READ_AGENT_TOOL {- return observ::run_read_agent(scratch, args);+ // The self-read guard lives in the shared core so every surface+ // (this loop and the Cowork command) enforces it identically.+ return observ::run_read_agent(scratch, args, &self.collab_run_id);}let res = workqueue::run_work_tool(scratch, name, args, &self.collab_run_id, epoch_secs());if res.posted {@@ -1325,6 +1352,10 @@ impl ToolInvoker for CompositeToolInvoker {self.flush_collab_status();}+ fn collab_is_child(&self) -> bool {+ self.collab_run_id != "main"+ }+fn background_pending(&self) -> bool {self.subagents.as_ref()@@ -2865,6 +2896,13 @@ async fn run_turn_cycle(let mut mid_run_nudge_count: u32 = 0;// One-shot: asked the model to close out its todos before handing back.let mut closeout_nudged = false;+ // Poll dampener: consecutive turns whose tool calls were *all* work-queue/+ // observability polls (see `is_poll_only_tool`). A worker that only claims,+ // lists and reads without doing or completing anything is stuck; nudge it+ // once, then hard-stop a child that will not stop itself.+ const POLL_STREAK_NUDGE: u32 = 3;+ const POLL_STREAK_STOP: u32 = 6;+ let mut poll_streak: u32 = 0;// The monitor set last published as `StreamEvent::Monitors`.let mut shown_monitors = Vec::new();@@ -3117,6 +3155,16 @@ async fn run_turn_cycle(return Ok(completion);}+ // A turn that calls nothing but poll tools is one more turn of a spin;+ // any productive call resets the count. Decided here, acted on (nudge/+ // stop) after the results are appended so history stays clean.+ let poll_only = tool_calls.iter().all(|tc| is_poll_only_tool(tool_call_fn_name(tc)));+ if poll_only {+ poll_streak += 1;+ } else {+ poll_streak = 0;+ }+// Crossing the session budget is advisory: it is recorded and the run// carries on, tool calls included. Note what this costs -- `max_turns// == 0` is the normal case (see above), so with the budget no longer@@ -3377,6 +3425,42 @@ async fn run_turn_cycle();}}++ // Poll dampener: acted on after the results are in the history, so a+ // nudge or a synthetic stop leaves a well-formed conversation (no+ // dangling tool_calls). Nudge once at the lower threshold; hard-stop a+ // child at the higher one -- the main run is only nudged, since ending+ // it would abort the children it may be waiting on.+ if poll_only && poll_streak == POLL_STREAK_NUDGE {+ crate::core::agent::reminder::attach(+ &mut conversation_messages,+ "You have spent several turns calling only work-queue/observability tools \+ (claim_work/list_work/read_agent) without doing any work or producing a result. \+ Stop polling -- you are pinged automatically when new work is ready. If your task \+ is already done, call complete_work with your result (or, if you are not a worker, \+ give your final answer) instead of calling these tools again.",+ );+ let _ = events.send(StreamEvent::MessagesUpdated {+ messages: conversation_messages.clone(),+ });+ } else if poll_only && poll_streak >= POLL_STREAK_STOP && tools.collab_is_child() {+ let stop = "Stopped after several consecutive turns of polling the work queue \+ (claim_work/list_work/read_agent) with no progress and no result. There \+ was no ready work to claim.";+ conversation_messages.push(serde_json::json!({+ "role": "assistant",+ "content": stop,+ }));+ let _ = events.send(StreamEvent::MessagesUpdated {+ messages: conversation_messages.clone(),+ });+ return Ok(serde_json::json!({+ "choices": [{+ "message": { "role": "assistant", "content": stop },+ "finish_reason": "stop"+ }]+ }));+ }turn += 1;}@@ -4596,6 +4680,148 @@ mod tests {);}+ /// A worker invoker: every claim reports the queue empty, and `child`+ /// decides whether the poll dampener is allowed to hard-stop this run.+ struct PollTool {+ child: bool,+ }+ #[async_trait]+ impl ToolInvoker for PollTool {+ async fn invoke(+ &self,+ tool_calls: &[serde_json::Value],+ ) -> Result<Vec<ToolOutcome>, String> {+ Ok(tool_calls+ .iter()+ .map(|tc| {+ let id = tc.get("id").and_then(|v| v.as_str()).unwrap_or("").to_string();+ ToolOutcome::plain(id, "No work is ready right now. Stop and wait.".to_string())+ })+ .collect())+ }+ fn collab_is_child(&self) -> bool {+ self.child+ }+ }++ fn claim_work_completion() -> serde_json::Value {+ json!({+ "choices": [{+ "message": {+ "content": serde_json::Value::Null,+ "tool_calls": [{+ "id": "call_1",+ "type": "function",+ "function": { "name": "claim_work", "arguments": "{}" }+ }]+ },+ "finish_reason": "tool_calls"+ }]+ })+ }++ fn carries_poll_nudge(request: &serde_json::Value) -> bool {+ request["messages"]+ .as_array()+ .into_iter()+ .flatten()+ .any(|m| {+ m.get("content")+ .and_then(|c| c.as_str())+ .is_some_and(|s| s.contains("Stop polling"))+ })+ }++ /// A child that only ever polls the work queue is hard-stopped by the+ /// dampener well before it can exhaust its budget on the spin, and the run+ /// ends with the explanatory stop answer rather than an error.+ #[tokio::test]+ async fn a_worker_spinning_on_poll_tools_is_stopped() {+ let (tx, _rx) = mpsc::unbounded_channel();+ let model = MockModel::new((0..30).map(|_| claim_work_completion()).collect());+ let tool = PollTool { child: true };+ let mut budget = SessionBudget::new(None);+ let result = run_turn_cycle(+ &tx,+ &json!({}),+ "m",+ &[],+ vec![json!({ "role": "user", "content": "review the file" })],+ 0, // unlimited: only the dampener can end this run+ &mut budget,+ &model,+ &tool,+ crate::core::agent::plan::RunMode::Normal,+ None,+ None,+ None,+ )+ .await+ .unwrap();+ let content = result["choices"][0]["message"]["content"].as_str().unwrap();+ assert!(content.contains("Stopped"), "run ended with the stop answer: {content}");+ // Streak reaches POLL_STREAK_STOP (6) on the 6th turn.+ assert_eq!(model.requests.lock().unwrap().len(), 6);+ }++ /// The nudge lands one turn after the lower threshold and before the stop:+ /// absent on the 3rd request, present on the 4th.+ #[tokio::test]+ async fn a_polling_run_is_nudged_before_it_is_stopped() {+ let (tx, _rx) = mpsc::unbounded_channel();+ let model = MockModel::new((0..30).map(|_| claim_work_completion()).collect());+ let tool = PollTool { child: true };+ let mut budget = SessionBudget::new(None);+ run_turn_cycle(+ &tx,+ &json!({}),+ "m",+ &[],+ vec![json!({ "role": "user", "content": "go" })],+ 0,+ &mut budget,+ &model,+ &tool,+ crate::core::agent::plan::RunMode::Normal,+ None,+ None,+ None,+ )+ .await+ .unwrap();+ let requests = model.requests.lock().unwrap();+ assert!(!carries_poll_nudge(&requests[2]), "not nudged before the 3rd poll turn");+ assert!(carries_poll_nudge(&requests[3]), "nudged on the request after the 3rd");+ }++ /// The main run is nudged like a worker but never hard-stopped: ending it+ /// would abort the children it may be waiting on, so it runs to the turn cap.+ #[tokio::test]+ async fn the_main_run_is_nudged_but_not_stopped() {+ let (tx, _rx) = mpsc::unbounded_channel();+ let model = MockModel::new((0..8).map(|_| claim_work_completion()).collect());+ let tool = PollTool { child: false };+ let mut budget = SessionBudget::new(None);+ let result = run_turn_cycle(+ &tx,+ &json!({}),+ "m",+ &[],+ vec![json!({ "role": "user", "content": "go" })],+ 8, // capped so the test terminates without a stop+ &mut budget,+ &model,+ &tool,+ crate::core::agent::plan::RunMode::Normal,+ None,+ None,+ None,+ )+ .await;+ assert!(result.is_err(), "main hit the turn cap, not the child stop: {result:?}");+ assert_eq!(model.requests.lock().unwrap().len(), 8, "every turn ran");+ }+/// A tool invoker whose one queued ping carries a headline, standing in for/// a monitor match. The headline must surface as `StreamEvent::Notice` at/// delivery; the text must reach the model as a `<SYSTEM>` reminder.diff --git a/web-app/src/lib/__tests__/agentTools.test.ts b/web-app/src/lib/__tests__/agentTools.test.tsindex b6095b9e9..ed2b81a71 100644--- a/web-app/src/lib/__tests__/agentTools.test.ts+++ b/web-app/src/lib/__tests__/agentTools.test.ts@@ -94,9 +94,11 @@ describe('agentTools', () => {it('reads a peer through agent_read and surfaces errors as an ERROR string', async () => {agentRead.mockResolvedValue('reviewer (sub-a-1) [running] step 2')const { readAgent } = await import('../agentTools')- expect(await readAgent('sess-1', { run_id: 'sub-a-1' })).toContain('running')+ expect(await readAgent('sess-1', 'main', { run_id: 'sub-a-1' })).toContain('running')+ // The caller's own id is forwarded so the core can refuse a self-read.+ expect(agentRead).toHaveBeenCalledWith('sess-1', 'main', { run_id: 'sub-a-1' })agentRead.mockRejectedValue(new Error('nope'))- expect(await readAgent('sess-1', {})).toBe('ERROR: nope')+ expect(await readAgent('sess-1', 'main', {})).toBe('ERROR: nope')})})diff --git a/web-app/src/lib/agentTools.ts b/web-app/src/lib/agentTools.tsindex 122b7898f..0a9c38a71 100644--- a/web-app/src/lib/agentTools.ts+++ b/web-app/src/lib/agentTools.ts@@ -503,13 +503,15 @@ export async function listWork(sessionId: string): Promise<WorkResult> {}}-/** Read a peer's status + transcript, or the roster (no `run_id`). */+/** Read a peer's status + transcript, or the roster (no `run_id`). `agentId` is+ * the caller's own id, passed so the core can refuse a self-read. */export async function readAgent(sessionId: string,+ agentId: string,args: Record<string, unknown>): Promise<string> {try {- return await agentRead(sessionId, args)+ return await agentRead(sessionId, agentId, args)} catch (e) {return `ERROR: ${messageOf(e)}`}diff --git a/web-app/src/lib/coworkWorkqueue.ts b/web-app/src/lib/coworkWorkqueue.tsindex f41418e5c..7f491c647 100644--- a/web-app/src/lib/coworkWorkqueue.ts+++ b/web-app/src/lib/coworkWorkqueue.ts@@ -142,7 +142,7 @@ export async function runWorkOp(const args =input && typeof input === 'object' ? (input as Record<string, unknown>) : {}if (toolName === READ_AGENT_TOOL) {- const output = await readAgent(ctx.sessionId, args)+ const output = await readAgent(ctx.sessionId, ctx.agentId, args)return { output, isError: output.startsWith('ERROR') }}let res: WorkCommandResult | null