diff --git a/src/auto-reply/reply/commands-tasks.test.ts b/src/auto-reply/reply/commands-tasks.test.ts index c670897b56a..ac60872a715 100644 --- a/src/auto-reply/reply/commands-tasks.test.ts +++ b/src/auto-reply/reply/commands-tasks.test.ts @@ -1,4 +1,5 @@ -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { resolveSessionAgentId } from "../../agents/agent-scope.js"; import type { OpenClawConfig } from "../../config/config.js"; import { completeTaskRunByRunId, @@ -11,6 +12,16 @@ import { configureTaskRegistryRuntime } from "../../tasks/task-registry.store.js import { buildTasksReply, handleTasksCommand } from "./commands-tasks.js"; import { buildCommandTestParams } from "./commands.test-harness.js"; +vi.mock("../../agents/agent-scope.js", async () => { + const actual = await vi.importActual( + "../../agents/agent-scope.js", + ); + return { + ...actual, + resolveSessionAgentId: vi.fn(actual.resolveSessionAgentId), + }; +}); + const baseCfg = { commands: { text: true }, channels: { whatsapp: { allowFrom: ["*"] } }, @@ -46,6 +57,7 @@ function configureInMemoryTaskRegistryStoreForTests(): void { describe("buildTasksReply", () => { beforeEach(() => { + vi.clearAllMocks(); resetTaskRegistryForTests({ persist: false }); configureInMemoryTaskRegistryStoreForTests(); }); @@ -190,6 +202,30 @@ describe("buildTasksReply", () => { expect(reply.text).not.toContain("hidden background task"); expect(reply.text).not.toContain("hidden progress detail"); }); + + it("uses the canonical target session agent for agent-local fallback counts", async () => { + createRunningTaskRun({ + runtime: "subagent", + requesterSessionKey: "agent:target:other-session", + childSessionKey: "agent:target:subagent:tasks-target-fallback", + runId: "run-tasks-target-fallback", + agentId: "target", + task: "target hidden background task", + progressSummary: "hidden target progress detail", + }); + vi.mocked(resolveSessionAgentId).mockReturnValue("target"); + + const commandParams = buildCommandTestParams("/tasks", baseCfg); + const reply = await buildTasksReply({ + ...commandParams, + agentId: "main", + sessionKey: "agent:target:empty-session", + }); + + expect(reply.text).toContain("All clear - nothing linked to this session right now."); + expect(reply.text).toContain("Agent-local: 1 active ยท 1 total"); + expect(reply.text).not.toContain("target hidden background task"); + }); }); describe("handleTasksCommand", () => { diff --git a/src/auto-reply/reply/commands-tasks.ts b/src/auto-reply/reply/commands-tasks.ts index 6dbe95bca33..46788cf08b6 100644 --- a/src/auto-reply/reply/commands-tasks.ts +++ b/src/auto-reply/reply/commands-tasks.ts @@ -108,12 +108,10 @@ export function buildTasksText(params: { sessionKey: string; agentId: string }): } export async function buildTasksReply(params: HandleCommandsParams): Promise { - const agentId = - params.agentId ?? - resolveSessionAgentId({ - sessionKey: params.sessionKey, - config: params.cfg, - }); + const agentId = resolveSessionAgentId({ + sessionKey: params.sessionKey, + config: params.cfg, + }); return { text: buildTasksText({ sessionKey: params.sessionKey,