perf(cron): lazy-load delivery subagent registry

This commit is contained in:
Vincent Koc
2026-04-13 16:57:46 +01:00
parent 7daa0d047a
commit a8977cde64
3 changed files with 27 additions and 4 deletions

View File

@@ -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", () => ({

View File

@@ -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<typeof import("../../gateway/call.runtime
let deliveryOutboundRuntimePromise:
| Promise<typeof import("./delivery-outbound.runtime.js")>
| undefined;
let deliverySubagentRegistryRuntimePromise:
| Promise<typeof import("./delivery-subagent-registry.runtime.js")>
| undefined;
let subagentFollowupRuntimePromise:
| Promise<typeof import("./subagent-followup.runtime.js")>
| 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,

View File

@@ -0,0 +1 @@
export { countActiveDescendantRuns } from "../../agents/subagent-registry-read.js";