Skip to content

🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.

fix(agent): the idle watchdog no longer stops long tool calls, subagents or video jobs

Marlaa73706947cd0
diff --git a/src/agent/host.ts b/src/agent/host.ts
index d2386af..3d944d6 100644
--- a/src/agent/host.ts
+++ b/src/agent/host.ts
@@ -127,7 +127,10 @@ function loadSdk(runtimeDir: string): Promise<SdkModule> {
/** 回合中的可见进度(工具调用等),由渠道批量发送。 */
export interface TurnObserver {
onProgress?(text: string): void;
- /** DSH 会话事件(time 是事件自带的时间);子代理的开始/结束以 subagent/started、subagent/finished 送来 */
+ /**
+ * DSH 会话事件(time 是事件自带的时间);子代理的开始/结束以 subagent/started、subagent/finished 送来,
+ * 子代理会话里的事件以 subagent/event(data = { sessionId, event })送来
+ */
onEvent?(type: string, data: any, time?: number): void;
/** 消息已经交给代理(之前是等进程、等会话) */
onStart?(): void;
@@ -846,7 +849,9 @@ exec "$REAL" "$@"
onNotification: (n) => {
if (process.env.AMB_DSH_TRACE) fs.appendFileSync(process.env.AMB_DSH_TRACE, JSON.stringify(n).slice(0, 2000) + "\n");
if (n.method === "subagent.started" || n.method === "subagent.finished") return input.observer?.onEvent?.(n.method.replace(".", "/"), n.params, Date.now());
- if (n.method !== "session.event" || n.params?.sessionId !== s.id) return;
+ if (n.method !== "session.event") return;
+ // 子代理会话的事件(SDK 只送来本会话和它的子代理的通知):不进本回合的用量和步骤,只说明子代理还在干活
+ if (n.params?.sessionId !== s.id) return input.observer?.onEvent?.("subagent/event", { sessionId: n.params?.sessionId, event: n.params?.event }, Number(n.params?.event?.time) || Date.now());
const ev = n.params.event;
const data = ev?.data ?? {};
if (ev?.type === "tool/call") {
diff --git a/src/agent/manager.ts b/src/agent/manager.ts
index 8434434..0b81175 100644
--- a/src/agent/manager.ts
+++ b/src/agent/manager.ts
@@ -56,11 +56,25 @@ export interface TurnProgress {
abort: AbortController;
/** 已经提示过"后来的消息排在它后面" */
queuedNotice?: boolean;
+ /** 正在运行的工具调用(含子代理的):工具运行期间 DSH 不发事件,看门狗不把它当成卡住 */
+ tools: Set<string>;
+ /** 进行中的生图 / 生视频任务 */
+ media: number;
}
/** 生成确认里"取消"那一项 */
const CANCEL = "取消";
+/**
+ * 记下正在运行的工具调用("session:callId",本会话的 session 记为空):tool/call 加入、tool/result 移除;
+ * 同一会话开始下一步(下一次模型请求要等所有工具结果)或回合结束时,清掉它剩下的。
+ */
+function trackTools(tools: Set<string>, session: string, type: string, data: any): void {
+ if (type === "tool/call") tools.add(`${session}:${data?.callId}`);
+ else if (type === "tool/result") tools.delete(`${session}:${data?.message?.toolCallId}`);
+ else if (type === "step/start" || type === "turn/end") for (const k of tools) if (k.startsWith(`${session}:`)) tools.delete(k);
+}
+
function deferred<T>() {
let resolve!: (v: T) => void;
const promise = new Promise<T>((r) => (resolve = r));
@@ -192,13 +206,14 @@ export class AgentManager {
/** onStep:活动记录里每新增或更新一步就回调一次(网页对话实时显示、回合结束后存进消息) */
async runTurn(botId: string, input: TurnInput & { onStep?: (step: ActivityStep) => void }) {
const conv = input.conversationId;
- const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model), abort: new AbortController() };
+ const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model), abort: new AbortController(), tools: new Set(), media: 0 };
this.turnsInProgress.set(conv, t);
- // 看门狗:代理已经在处理,却长时间没有进展(模型不回、工具不动、只在重试),又不是在等用户批准或回答 → 中止
+ // 看门狗:代理已经在处理,却长时间没有进展(模型不回、只在重试),又不是在等用户批准或回答、
+ // 也没有工具或生成任务在跑(生视频、长命令、子代理里的工具)→ 中止
const idleMs = this.o.idleTimeoutMs ?? 10 * 60_000;
const watchdog = setInterval(() => {
if (!t.lastProgress || t.abort.signal.aborted) return;
- if (this.approvals.pending(conv).length) t.lastProgress = Date.now();
+ if (this.approvals.pending(conv).length || t.tools.size || t.media) t.lastProgress = Date.now();
else if (Date.now() - t.lastProgress > idleMs) {
log.warn("agent turn idle, stopping", { bot: botId, conversationId: conv, idleMs, lastStep: t.activity.last });
this.halt(conv, t, new TurnStopped("idle", idleMs));
@@ -211,7 +226,15 @@ export class AgentManager {
input.observer?.onStart?.();
},
onEvent: (type, data, time) => {
- if (t.activity.apply(type, data, time)) t.lastProgress = Date.now();
+ if (type === "subagent/event") {
+ // 子代理在干活:它的工具调用同样记下;它在重试模型请求不算进展
+ const ev = data?.event ?? {};
+ trackTools(t.tools, String(data?.sessionId ?? ""), String(ev.type), ev.data);
+ if (ev.type !== "llm/retry" && ev.type !== "llm/retry-started") t.lastProgress = Date.now();
+ } else {
+ trackTools(t.tools, "", type, data);
+ if (t.activity.apply(type, data, time)) t.lastProgress = Date.now();
+ }
input.observer?.onEvent?.(type, data, time);
},
};
@@ -469,13 +492,32 @@ export class AgentManager {
model = r.model;
}
}
- return { text: await media.execute(botId, model, args) };
+ // 生成可能要十几分钟,期间没有会话事件:看门狗不把它当成卡住
+ const turn = this.turnsInProgress.get(conv);
+ if (turn) turn.media++;
+ let text: string;
+ try {
+ text = await media.execute(botId, model, args);
+ } finally {
+ if (turn) turn.media--;
+ }
+ // 回合已经中止(/stop、超过上限、代理进程被关):结果到不了代理,已扣费的文件留在工作区,告诉用户
+ if (turn && (turn.abort.signal.aborted || this.turnsInProgress.get(conv) !== turn)) this.orphanedMedia(botId, plan.kind, text);
+ return { text };
} catch (e) {
log.warn("media tool failed", { bot: botId, op, message: (e as Error).message.slice(0, 300) });
return { error: (e as Error).message };
}
}
+ /** 生成完了,请求它的回合却已经结束:文件在工作区 media/,用界面通知告诉用户 */
+ private orphanedMedia(botId: string, kind: string, text: string): void {
+ const files = text.match(/media\/[\w.-]+/g) ?? ["media/"];
+ log.warn("media finished after its turn ended", { bot: botId, files });
+ const name = this.o.store.getBot(botId)?.name ?? botId;
+ this.o.onNotice?.(`机器人「${name}」生成的${kind === "image" ? "图片" : "视频"}已保存在它的工作区(${files.join("、")}),但请求它的那条消息已经中止,没有发到聊天里。需要的话在对话里让机器人把它发出来。`);
+ }
+
/** 生成前的确认:问用户这次用哪个模型(本次的、默认的和几个推荐的,或回复别的模型名),等到回答、取消或过期 */
private async confirmMedia(botId: string, conv: string, sessionId: string, plan: MediaPlan, tool: string): Promise<{ model?: MediaModel; cancel?: boolean; reply?: string }> {
const name = plan.kind === "image" ? "图片" : "视频";
diff --git a/test/agent-manager.unit.test.ts b/test/agent-manager.unit.test.ts
index 913074c..f640885 100644
--- a/test/agent-manager.unit.test.ts
+++ b/test/agent-manager.unit.test.ts
@@ -1,4 +1,4 @@
-/** AgentManager 的回合调度:上下文管家的压缩闸门。代理进程用桩代替(不起 DSH)。 */
+/** AgentManager 的回合调度:上下文管家的压缩闸门、空闲看门狗。代理进程用桩代替(不起 DSH)。 */
import { EventEmitter } from "node:events";
import fs from "node:fs";
import os from "node:os";
@@ -6,7 +6,8 @@ import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Store } from "../src/store/db.js";
import { AgentManager } from "../src/agent/manager.js";
-import type { TurnInput } from "../src/agent/host.js";
+import { TurnStopped, type TurnInput } from "../src/agent/host.js";
+import type { MediaModel, MediaService } from "../src/agent/media.js";
import { STEWARD_SYSTEM } from "../src/agent/steward.js";
const COMPACT = '{"action":"compact","reason":"still working"}';
@@ -147,3 +148,148 @@ describe("context steward: the compaction gate never outlives its compaction", (
expect(s.mgr.stewardLog.map((e) => e.action)).toEqual(["compact-skipped"]);
});
});
+
+/** 一条 DSH 会话事件通知 */
+const ev = (sessionId: string, type: string, data: Record<string, unknown> = {}) => ({ method: "session.event", params: { sessionId, event: { type, data, time: Date.now() } } });
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
+const IDLE = 600;
+const VIDEO: MediaModel = { id: "v1", kind: "video", route: "videos", requiresReference: false, price: "", priority: 0 };
+
+/**
+ * 真的 AgentHost(会话事件经它的过滤送到看门狗),DSH 换成桩:run 按脚本发通知,返回最终回复。
+ * 看门狗阈值 600 ms;生视频任务 2 秒。
+ */
+function setupHost(script: (emit: (n: any) => void, s: { mgr: AgentManager }) => Promise<string>) {
+ const store = new Store(":memory:");
+ store.createBot({ id: "b", name: "小蓝" });
+ store.ensureConversation("b", "web", "console");
+ const notices: string[] = [];
+ const media = {
+ prepare: async () => ({ kind: "video", model: VIDEO, default: VIDEO, candidates: [], all: [VIDEO], custom: false, summary: "" }),
+ execute: async () => {
+ await sleep(2000);
+ return "已用 v1 生成视频,保存在工作区:media/video-20261031-120000.mp4。要发给用户,请用 present 工具交付这个文件。";
+ },
+ } as unknown as MediaService;
+ const mgr = new AgentManager({ store, dataDir: dir, runtimeDir: "/rt", upstream: () => ({ baseUrl: "", apiKey: "" }), enabled: true, idleTimeoutMs: IDLE, media, onNotice: (t) => notices.push(t) });
+ const host = mgr.host("b");
+ const closed = new AbortController();
+ const session = {
+ id: "R",
+ run: async (_prompt: unknown, o: { onNotification: (n: any) => void }) => ({ sessionId: "R", finalResponse: await script(o.onNotification, { mgr }), events: [] }),
+ setWorkingDirectory: async () => undefined,
+ };
+ const harness = { client: {}, start: async () => undefined, session: () => session, resumeSession: async () => session, close: async () => void closed.abort() };
+ Object.assign(host as any, { harness, model: "m", sig: (host as any).settingsSig(), state: "running" });
+ return {
+ mgr,
+ notices,
+ /** 等到 DSH 桩被关掉为止(看门狗、/stop 都会关掉它) */
+ hang: () => new Promise<string>((r) => closed.signal.addEventListener("abort", () => r(""))),
+ turn: () => mgr.runTurn("b", { conversationId: CONV, text: "make a video", model: "m" }),
+ };
+}
+
+/** 回合被看门狗中止所用的时间(毫秒) */
+async function stoppedIdle(p: Promise<unknown>): Promise<number> {
+ const t0 = Date.now();
+ const e = await p.then(
+ () => undefined,
+ (e) => e,
+ );
+ expect(e).toBeInstanceOf(TurnStopped);
+ expect(e.reason).toBe("idle");
+ return Date.now() - t0;
+}
+
+describe("idle watchdog: long tool calls are work, a silent turn is still stopped", () => {
+ it("a tool call that runs longer than the idle limit (a video, a long command) is not stopped", async () => {
+ const s = setupHost(async (emit) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ emit(ev("R", "tool/call", { callId: "c1", name: "generate_video", arguments: "{}" }));
+ await sleep(2000);
+ emit(ev("R", "tool/result", { message: { toolCallId: "c1", content: [], isError: false } }));
+ emit(ev("R", "step/start", { turn: 1, step: 2 }));
+ return "video ready";
+ });
+ expect((await s.turn()).text).toBe("video ready");
+ });
+
+ it("a subagent at work keeps the turn alive: its own events and its long tool calls count", async () => {
+ const s = setupHost(async (emit) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ emit(ev("R", "tool/call", { callId: "c1", name: "agent", arguments: "{}" }));
+ emit(ev("R", "tool/result", { message: { toolCallId: "c1", content: [{ type: "text", text: "started subagent K" }], isError: false } }));
+ emit({ method: "subagent.started", params: { parentSessionId: "R", childSessionId: "K" } });
+ for (let i = 1; i <= 8; i++) {
+ await sleep(150);
+ emit(ev("K", "step/start", { turn: 1, step: i }));
+ }
+ emit(ev("K", "tool/call", { callId: "k1", name: "bash", arguments: '{"command":"make"}' }));
+ await sleep(2000);
+ emit(ev("K", "tool/result", { message: { toolCallId: "k1", content: [], isError: false } }));
+ emit({ method: "subagent.finished", params: { parentSessionId: "R", childSessionId: "K", status: "ok" } });
+ return "subagent done";
+ });
+ expect((await s.turn()).text).toBe("subagent done");
+ });
+
+ it("a generation job in flight counts as work even without a visible tool call", async () => {
+ const s = setupHost(async (emit, { mgr }) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ (mgr as any).mediaGrants.set(CONV, { kind: "video", model: "v1", at: Date.now() });
+ const r = await (mgr as any).mediaOp({ botId: "b", sessionId: "R", op: "video", args: { prompt: "a cat" } });
+ return r.text;
+ });
+ expect((await s.turn()).text).toMatch(/^已用 v1 生成视频/);
+ expect(s.notices).toEqual([]);
+ });
+
+ it("a video that finishes after its turn was stopped is kept, and the user is told where it is", async () => {
+ let finished!: () => void;
+ const done = new Promise<void>((r) => (finished = r));
+ const s = setupHost(async (emit, { mgr }) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ emit(ev("R", "tool/call", { callId: "c1", name: "generate_video", arguments: "{}" }));
+ (mgr as any).mediaGrants.set(CONV, { kind: "video", model: "v1", at: Date.now() });
+ setTimeout(() => mgr.stopTurn(CONV), 300);
+ await (mgr as any).mediaOp({ botId: "b", sessionId: "R", op: "video", args: { prompt: "a cat" } });
+ finished();
+ return "";
+ });
+ await expect(s.turn()).rejects.toMatchObject({ reason: "user" });
+ await done;
+ expect(s.notices).toEqual(["机器人「小蓝」生成的视频已保存在它的工作区(media/video-20261031-120000.mp4),但请求它的那条消息已经中止,没有发到聊天里。需要的话在对话里让机器人把它发出来。"]);
+ });
+
+ it("still stops a turn whose model request hangs after its tools finished", async () => {
+ const s = setupHost(async (emit) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ emit(ev("R", "tool/call", { callId: "c1", name: "bash", arguments: "{}" }));
+ emit(ev("R", "tool/result", { message: { toolCallId: "c1", content: [], isError: false } }));
+ emit(ev("R", "step/start", { turn: 1, step: 2 }));
+ return s.hang();
+ });
+ expect(await stoppedIdle(s.turn())).toBeLessThan(5000);
+ });
+
+ it("still stops a turn whose subagent hangs on its model, or only retries it", async () => {
+ for (const retry of [false, true]) {
+ const s = setupHost(async (emit) => {
+ emit(ev("R", "step/start", { turn: 1, step: 1 }));
+ emit({ method: "subagent.started", params: { parentSessionId: "R", childSessionId: "K" } });
+ emit(ev("K", "step/start", { turn: 1, step: 1 }));
+ emit(ev("K", "tool/call", { callId: "k1", name: "bash", arguments: "{}" }));
+ emit(ev("K", "tool/result", { message: { toolCallId: "k1", content: [], isError: false } }));
+ emit(ev("K", "step/start", { turn: 1, step: 2 }));
+ const hang = s.hang();
+ if (retry) {
+ const t = setInterval(() => emit(ev("K", "llm/retry", { retry: 1, maxRetries: 9, delayMs: 100 })), 100);
+ void hang.then(() => clearInterval(t));
+ }
+ return hang;
+ });
+ expect(await stoppedIdle(s.turn())).toBeLessThan(5000);
+ }
+ });
+});