diff --git a/src/cron/isolated-agent/delivery-dispatch.double-announce.test.ts b/src/cron/isolated-agent/delivery-dispatch.double-announce.test.ts index 1713491f577..15b1a793a95 100644 --- a/src/cron/isolated-agent/delivery-dispatch.double-announce.test.ts +++ b/src/cron/isolated-agent/delivery-dispatch.double-announce.test.ts @@ -15,13 +15,21 @@ import { SILENT_REPLY_TOKEN } from "../../auto-reply/tokens.js"; // --- Module mocks (must be hoisted before imports) --- +const { countActiveDescendantRunsMock } = vi.hoisted(() => ({ + countActiveDescendantRunsMock: vi.fn().mockReturnValue(0), +})); + vi.mock("../../config/sessions.js", () => ({ resolveAgentMainSessionKey: vi.fn(({ agentId }: { agentId: string }) => `agent:${agentId}:main`), resolveMainSessionKey: vi.fn(() => "global"), })); vi.mock("../../agents/subagent-registry-read.js", () => ({ - countActiveDescendantRuns: vi.fn().mockReturnValue(0), + countActiveDescendantRuns: countActiveDescendantRunsMock, +})); + +vi.mock("./delivery-subagent-registry.runtime.js", () => ({ + countActiveDescendantRuns: countActiveDescendantRunsMock, })); vi.mock("../../infra/outbound/deliver.js", () => ({ diff --git a/src/cron/isolated-agent/delivery-dispatch.ts b/src/cron/isolated-agent/delivery-dispatch.ts index 87014a49b89..011d63b4fd0 100644 --- a/src/cron/isolated-agent/delivery-dispatch.ts +++ b/src/cron/isolated-agent/delivery-dispatch.ts @@ -1,4 +1,3 @@ -import { countActiveDescendantRuns } from "../../agents/subagent-registry-read.js"; import type { ReplyPayload } from "../../auto-reply/reply-payload.js"; import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../../auto-reply/tokens.js"; import type { CliDeps } from "../../cli/outbound-send-deps.js"; @@ -131,6 +130,9 @@ let gatewayCallRuntimePromise: Promise | undefined; +let deliverySubagentRegistryRuntimePromise: + | Promise + | undefined; let subagentFollowupRuntimePromise: | Promise | undefined; @@ -149,6 +151,13 @@ async function loadDeliveryOutboundRuntime(): Promise< return await deliveryOutboundRuntimePromise; } +async function loadDeliverySubagentRegistryRuntime(): Promise< + typeof import("./delivery-subagent-registry.runtime.js") +> { + deliverySubagentRegistryRuntimePromise ??= import("./delivery-subagent-registry.runtime.js"); + return await deliverySubagentRegistryRuntimePromise; +} + async function loadSubagentFollowupRuntime(): Promise< typeof import("./subagent-followup.runtime.js") > { @@ -569,8 +578,11 @@ export async function dispatchCronDelivery( return null; } const initialSynthesizedText = synthesizedText.trim(); - let activeSubagentRuns = countActiveDescendantRuns(params.agentSessionKey); const expectedSubagentFollowup = expectsSubagentFollowup(initialSynthesizedText); + const subagentRegistryRuntime = await loadDeliverySubagentRegistryRuntime(); + let activeSubagentRuns = subagentRegistryRuntime.countActiveDescendantRuns( + params.agentSessionKey, + ); const shouldCheckCompletedDescendants = activeSubagentRuns === 0 && isLikelyInterimCronMessage(initialSynthesizedText); const needsSubagentFollowupRuntime = @@ -597,7 +609,9 @@ export async function dispatchCronDelivery( timeoutMs: params.timeoutMs, observedActiveDescendants: activeSubagentRuns > 0 || expectedSubagentFollowup, }); - activeSubagentRuns = countActiveDescendantRuns(params.agentSessionKey); + activeSubagentRuns = subagentRegistryRuntime.countActiveDescendantRuns( + params.agentSessionKey, + ); if (!finalReply && activeSubagentRuns === 0) { finalReply = await subagentFollowupRuntime?.readDescendantSubagentFallbackReply({ sessionKey: params.agentSessionKey, diff --git a/src/cron/isolated-agent/delivery-subagent-registry.runtime.ts b/src/cron/isolated-agent/delivery-subagent-registry.runtime.ts new file mode 100644 index 00000000000..07b4b8295e5 --- /dev/null +++ b/src/cron/isolated-agent/delivery-subagent-registry.runtime.ts @@ -0,0 +1 @@ +export { countActiveDescendantRuns } from "../../agents/subagent-registry-read.js";