Skip to content

Commit 2878798

Browse files
committed
perf(cron): lazy-load delivery runtime helpers
1 parent fbdbd99 commit 2878798

5 files changed

Lines changed: 72 additions & 44 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { getLoadedChannelPluginForRead } from "../../channels/plugins/registry-loaded-read.js";
2+
export { readChannelAllowFromStoreEntriesSync } from "../../pairing/allow-from-store-read.js";
3+
export { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js";
4+
export { resolveFirstBoundAccountId } from "../../routing/bound-account-read.js";

src/cron/isolated-agent/delivery-target.ts

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getLoadedChannelPluginForRead } from "../../channels/plugins/registry-loaded-read.js";
21
import type { ChannelId } from "../../channels/plugins/types.public.js";
32
import { resolveAgentMainSessionKey } from "../../config/sessions/main-session.js";
43
import { resolveStorePath } from "../../config/sessions/paths.js";
@@ -9,9 +8,6 @@ import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-id-resolut
98
import { tryResolveLoadedOutboundTarget } from "../../infra/outbound/targets-loaded.js";
109
import { resolveSessionDeliveryTarget } from "../../infra/outbound/targets-session.js";
1110
import type { OutboundChannel } from "../../infra/outbound/targets.js";
12-
import { readChannelAllowFromStoreEntriesSync } from "../../pairing/allow-from-store-read.js";
13-
import { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js";
14-
import { resolveFirstBoundAccountId } from "../../routing/bound-account-read.js";
1511
import { normalizeAccountId } from "../../routing/session-key.js";
1612

1713
export type DeliveryTargetResolution =
@@ -56,11 +52,19 @@ async function resolveOutboundTargetWithRuntime(
5652
let channelSelectionRuntimePromise:
5753
| Promise<typeof import("../../infra/outbound/channel-selection.runtime.js")>
5854
| undefined;
55+
let deliveryTargetRuntimePromise:
56+
| Promise<typeof import("./delivery-target.runtime.js")>
57+
| undefined;
5958

6059
async function loadChannelSelectionRuntime() {
6160
channelSelectionRuntimePromise ??= import("../../infra/outbound/channel-selection.runtime.js");
6261
return await channelSelectionRuntimePromise;
6362
}
63+
64+
async function loadDeliveryTargetRuntime() {
65+
deliveryTargetRuntimePromise ??= import("./delivery-target.runtime.js");
66+
return await deliveryTargetRuntimePromise;
67+
}
6468
export async function resolveDeliveryTarget(
6569
cfg: OpenClawConfig,
6670
agentId: string,
@@ -140,6 +144,7 @@ export async function resolveDeliveryTarget(
140144
: undefined;
141145
let accountId = explicitAccountId ?? resolved.accountId;
142146
if (!accountId && channel) {
147+
const { resolveFirstBoundAccountId } = await loadDeliveryTargetRuntime();
143148
accountId = resolveFirstBoundAccountId({ cfg, channelId: channel, agentId });
144149
}
145150

@@ -172,34 +177,42 @@ export async function resolveDeliveryTarget(
172177
};
173178
}
174179

175-
const channelPlugin = getLoadedChannelPluginForRead(channel);
176-
const resolvedAccountId = normalizeAccountId(accountId);
177-
const configuredAllowFromRaw = channelPlugin?.config.resolveAllowFrom?.({
178-
cfg,
179-
accountId: resolvedAccountId,
180-
});
181-
const configuredAllowFrom = configuredAllowFromRaw
182-
? mapAllowFromEntries(configuredAllowFromRaw)
183-
: [];
184-
const storeAllowFrom = readChannelAllowFromStoreEntriesSync(
185-
channel,
186-
process.env,
187-
resolvedAccountId,
188-
);
189-
const allowFromOverride = [...new Set([...configuredAllowFrom, ...storeAllowFrom])];
190-
const effectiveAllowFrom = mode === "implicit" ? allowFromOverride : undefined;
191-
192-
if (toCandidate && mode === "implicit" && allowFromOverride.length > 0) {
193-
const currentTargetResolution = await resolveOutboundTargetWithRuntime({
194-
channel,
195-
to: toCandidate,
180+
let effectiveAllowFrom: string[] | undefined;
181+
if (mode === "implicit") {
182+
const {
183+
getLoadedChannelPluginForRead,
184+
mapAllowFromEntries,
185+
readChannelAllowFromStoreEntriesSync,
186+
} = await loadDeliveryTargetRuntime();
187+
const channelPlugin = getLoadedChannelPluginForRead(channel);
188+
const resolvedAccountId = normalizeAccountId(accountId);
189+
const configuredAllowFromRaw = channelPlugin?.config.resolveAllowFrom?.({
196190
cfg,
197-
accountId,
198-
mode,
199-
allowFrom: effectiveAllowFrom,
191+
accountId: resolvedAccountId,
200192
});
201-
if (!currentTargetResolution.ok) {
202-
toCandidate = allowFromOverride[0];
193+
const configuredAllowFrom = configuredAllowFromRaw
194+
? mapAllowFromEntries(configuredAllowFromRaw)
195+
: [];
196+
const storeAllowFrom = readChannelAllowFromStoreEntriesSync(
197+
channel,
198+
process.env,
199+
resolvedAccountId,
200+
);
201+
const allowFromOverride = [...new Set([...configuredAllowFrom, ...storeAllowFrom])];
202+
effectiveAllowFrom = allowFromOverride;
203+
204+
if (toCandidate && allowFromOverride.length > 0) {
205+
const currentTargetResolution = await resolveOutboundTargetWithRuntime({
206+
channel,
207+
to: toCandidate,
208+
cfg,
209+
accountId,
210+
mode,
211+
allowFrom: effectiveAllowFrom,
212+
});
213+
if (!currentTargetResolution.ok) {
214+
toCandidate = allowFromOverride[0];
215+
}
203216
}
204217
}
205218

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { resolveDeliveryTarget } from "./delivery-target.js";
2+
export {
3+
dispatchCronDelivery,
4+
matchesMessagingToolDeliveryTarget,
5+
resolveCronDeliveryBestEffort,
6+
} from "./delivery-dispatch.js";

src/cron/isolated-agent/run.test-harness.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ vi.mock("../delivery-plan.js", () => ({
199199
resolveCronDeliveryPlan: resolveCronDeliveryPlanMock,
200200
}));
201201

202-
vi.mock("./delivery-target.js", () => ({
203-
resolveDeliveryTarget: resolveDeliveryTargetMock,
204-
}));
205-
206-
vi.mock("./delivery-dispatch.js", async () => {
207-
const actual =
208-
await vi.importActual<typeof import("./delivery-dispatch.js")>("./delivery-dispatch.js");
202+
vi.mock("./run-delivery.runtime.js", async () => {
203+
const actual = await vi.importActual<typeof import("./run-delivery.runtime.js")>(
204+
"./run-delivery.runtime.js",
205+
);
209206
return {
210207
...actual,
208+
resolveDeliveryTarget: resolveDeliveryTargetMock,
211209
dispatchCronDelivery: dispatchCronDeliveryMock,
212210
};
213211
});

src/cron/isolated-agent/run.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import type { AgentDefaultsConfig } from "../../config/types.agent-defaults.js";
66
import type { OpenClawConfig } from "../../config/types.openclaw.js";
77
import { resolveCronDeliveryPlan } from "../delivery-plan.js";
88
import type { CronJob, CronRunTelemetry } from "../types.js";
9-
import {
10-
dispatchCronDelivery,
11-
matchesMessagingToolDeliveryTarget,
12-
resolveCronDeliveryBestEffort,
13-
} from "./delivery-dispatch.js";
14-
import { resolveDeliveryTarget } from "./delivery-target.js";
159
import {
1610
isHeartbeatOnlyResponse,
1711
resolveCronPayloadOutcome,
@@ -69,6 +63,7 @@ let cronContextRuntimePromise: Promise<typeof import("./run-context.runtime.js")
6963
let cronModelCatalogRuntimePromise:
7064
| Promise<typeof import("./run-model-catalog.runtime.js")>
7165
| undefined;
66+
let cronDeliveryRuntimePromise: Promise<typeof import("./run-delivery.runtime.js")> | undefined;
7267

7368
async function loadSessionStoreRuntime() {
7469
sessionStoreRuntimePromise ??= import("../../config/sessions/store.runtime.js");
@@ -100,6 +95,11 @@ async function loadCronModelCatalogRuntime() {
10095
return await cronModelCatalogRuntimePromise;
10196
}
10297

98+
async function loadCronDeliveryRuntime() {
99+
cronDeliveryRuntimePromise ??= import("./run-delivery.runtime.js");
100+
return await cronDeliveryRuntimePromise;
101+
}
102+
103103
function hasConfiguredAuthProfiles(cfg: OpenClawConfig): boolean {
104104
return (
105105
Boolean(cfg.auth?.profiles && Object.keys(cfg.auth.profiles).length > 0) ||
@@ -115,8 +115,9 @@ export type { RunCronAgentTurnResult } from "./run.types.js";
115115

116116
type CronExecutionRuntime = typeof import("./run-executor.runtime.js");
117117
type CronExecutionResult = Awaited<ReturnType<CronExecutionRuntime["executeCronRun"]>>;
118-
type ResolvedCronDeliveryTarget = Awaited<ReturnType<typeof resolveDeliveryTarget>>;
119118
type CronModelCatalogRuntime = typeof import("./run-model-catalog.runtime.js");
119+
type CronDeliveryRuntime = typeof import("./run-delivery.runtime.js");
120+
type ResolvedCronDeliveryTarget = Awaited<ReturnType<CronDeliveryRuntime["resolveDeliveryTarget"]>>;
120121

121122
type IsolatedDeliveryContract = "cron-owned" | "shared";
122123

@@ -165,6 +166,7 @@ async function resolveCronDeliveryContext(params: {
165166
}),
166167
};
167168
}
169+
const { resolveDeliveryTarget } = await loadCronDeliveryRuntime();
168170
const resolvedDelivery = await resolveDeliveryTarget(params.cfg, params.agentId, {
169171
channel: deliveryPlan.channel ?? "last",
170172
to: deliveryPlan.to,
@@ -644,6 +646,11 @@ async function finalizeCronRun(params: {
644646
const skipHeartbeatDelivery =
645647
prepared.deliveryRequested &&
646648
isHeartbeatOnlyResponse(payloads, resolveHeartbeatAckMaxChars(prepared.agentCfg));
649+
const {
650+
dispatchCronDelivery,
651+
matchesMessagingToolDeliveryTarget,
652+
resolveCronDeliveryBestEffort,
653+
} = await loadCronDeliveryRuntime();
647654
const skipMessagingToolDelivery =
648655
(prepared.input.deliveryContract ?? "cron-owned") === "shared" &&
649656
prepared.deliveryRequested &&

0 commit comments

Comments
 (0)