Skip to content

Commit 62802c8

Browse files
committed
build: fix ineffective dynamic imports with lazy boundaries
1 parent 4b17d6d commit 62802c8

30 files changed

+341
-156
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { pruneStaleCommandPolls } from "./command-poll-backoff.js";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { getDiagnosticSessionState } from "../logging/diagnostic-session-state.js";
2+
export { logToolLoopAction } from "../logging/diagnostic.js";
3+
export {
4+
detectToolCallLoop,
5+
recordToolCall,
6+
recordToolCallOutcome,
7+
} from "./tool-loop-detection.js";

src/agents/pi-tools.before-tool-call.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const adjustedParamsByToolCallId = new Map<string, unknown>();
2323
const MAX_TRACKED_ADJUSTED_PARAMS = 1024;
2424
const LOOP_WARNING_BUCKET_SIZE = 10;
2525
const MAX_LOOP_WARNING_KEYS = 256;
26+
let beforeToolCallRuntimePromise: Promise<
27+
typeof import("./pi-tools.before-tool-call.runtime.js")
28+
> | null = null;
29+
30+
function loadBeforeToolCallRuntime() {
31+
beforeToolCallRuntimePromise ??= import("./pi-tools.before-tool-call.runtime.js");
32+
return beforeToolCallRuntimePromise;
33+
}
2634

2735
function buildAdjustedParamsKey(params: { runId?: string; toolCallId: string }): string {
2836
if (params.runId && params.runId.trim()) {
@@ -62,8 +70,7 @@ async function recordLoopOutcome(args: {
6270
return;
6371
}
6472
try {
65-
const { getDiagnosticSessionState } = await import("../logging/diagnostic-session-state.js");
66-
const { recordToolCallOutcome } = await import("./tool-loop-detection.js");
73+
const { getDiagnosticSessionState, recordToolCallOutcome } = await loadBeforeToolCallRuntime();
6774
const sessionState = getDiagnosticSessionState({
6875
sessionKey: args.ctx.sessionKey,
6976
sessionId: args.ctx?.agentId,
@@ -91,10 +98,8 @@ export async function runBeforeToolCallHook(args: {
9198
const params = args.params;
9299

93100
if (args.ctx?.sessionKey) {
94-
const { getDiagnosticSessionState } = await import("../logging/diagnostic-session-state.js");
95-
const { logToolLoopAction } = await import("../logging/diagnostic.js");
96-
const { detectToolCallLoop, recordToolCall } = await import("./tool-loop-detection.js");
97-
101+
const { getDiagnosticSessionState, logToolLoopAction, detectToolCallLoop, recordToolCall } =
102+
await loadBeforeToolCallRuntime();
98103
const sessionState = getDiagnosticSessionState({
99104
sessionKey: args.ctx.sessionKey,
100105
sessionId: args.ctx?.agentId,

src/agents/subagent-announce.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ const FAST_TEST_RETRY_INTERVAL_MS = 8;
4949
const FAST_TEST_REPLY_CHANGE_WAIT_MS = 20;
5050
const DEFAULT_SUBAGENT_ANNOUNCE_TIMEOUT_MS = 60_000;
5151
const MAX_TIMER_SAFE_TIMEOUT_MS = 2_147_000_000;
52+
let subagentRegistryRuntimePromise: Promise<
53+
typeof import("./subagent-registry-runtime.js")
54+
> | null = null;
55+
56+
function loadSubagentRegistryRuntime() {
57+
subagentRegistryRuntimePromise ??= import("./subagent-registry-runtime.js");
58+
return subagentRegistryRuntimePromise;
59+
}
60+
5261
const DIRECT_ANNOUNCE_TRANSIENT_RETRY_DELAYS_MS = FAST_TEST_MODE
5362
? ([8, 16, 32] as const)
5463
: ([5_000, 10_000, 20_000] as const);
@@ -773,12 +782,9 @@ async function sendSubagentAnnounceDirectly(params: {
773782
if (!forceBoundSessionDirectDelivery) {
774783
let pendingDescendantRuns = 0;
775784
try {
776-
const {
777-
countPendingDescendantRuns,
778-
countPendingDescendantRunsExcludingRun,
779-
countActiveDescendantRuns,
780-
} = await import("./subagent-registry.js");
781-
if (params.currentRunId && typeof countPendingDescendantRunsExcludingRun === "function") {
785+
const { countPendingDescendantRuns, countPendingDescendantRunsExcludingRun } =
786+
await loadSubagentRegistryRuntime();
787+
if (params.currentRunId) {
782788
pendingDescendantRuns = Math.max(
783789
0,
784790
countPendingDescendantRunsExcludingRun(
@@ -789,9 +795,7 @@ async function sendSubagentAnnounceDirectly(params: {
789795
} else {
790796
pendingDescendantRuns = Math.max(
791797
0,
792-
typeof countPendingDescendantRuns === "function"
793-
? countPendingDescendantRuns(canonicalRequesterSessionKey)
794-
: countActiveDescendantRuns(canonicalRequesterSessionKey),
798+
countPendingDescendantRuns(canonicalRequesterSessionKey),
795799
);
796800
}
797801
} catch {
@@ -1224,14 +1228,8 @@ export async function runSubagentAnnounceFlow(params: {
12241228

12251229
let pendingChildDescendantRuns = 0;
12261230
try {
1227-
const { countPendingDescendantRuns, countActiveDescendantRuns } =
1228-
await import("./subagent-registry.js");
1229-
pendingChildDescendantRuns = Math.max(
1230-
0,
1231-
typeof countPendingDescendantRuns === "function"
1232-
? countPendingDescendantRuns(params.childSessionKey)
1233-
: countActiveDescendantRuns(params.childSessionKey),
1234-
);
1231+
const { countPendingDescendantRuns } = await loadSubagentRegistryRuntime();
1232+
pendingChildDescendantRuns = Math.max(0, countPendingDescendantRuns(params.childSessionKey));
12351233
} catch {
12361234
// Best-effort only; fall back to direct announce behavior when unavailable.
12371235
}
@@ -1281,7 +1279,7 @@ export async function runSubagentAnnounceFlow(params: {
12811279
// still receive the announce — injecting will start a new agent turn.
12821280
if (requesterIsSubagent) {
12831281
const { isSubagentSessionRunActive, resolveRequesterForChildSession } =
1284-
await import("./subagent-registry.js");
1282+
await loadSubagentRegistryRuntime();
12851283
if (!isSubagentSessionRunActive(targetRequesterSessionKey)) {
12861284
// Parent run has ended. Check if parent SESSION still exists.
12871285
// If it does, the parent may be waiting for child results — inject there.
@@ -1314,7 +1312,7 @@ export async function runSubagentAnnounceFlow(params: {
13141312

13151313
let remainingActiveSubagentRuns = 0;
13161314
try {
1317-
const { countActiveDescendantRuns } = await import("./subagent-registry.js");
1315+
const { countActiveDescendantRuns } = await loadSubagentRegistryRuntime();
13181316
remainingActiveSubagentRuns = Math.max(
13191317
0,
13201318
countActiveDescendantRuns(targetRequesterSessionKey),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {
2+
countActiveDescendantRuns,
3+
countPendingDescendantRuns,
4+
countPendingDescendantRunsExcludingRun,
5+
isSubagentSessionRunActive,
6+
resolveRequesterForChildSession,
7+
} from "./subagent-registry.js";

src/auto-reply/reply/route-reply.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import type { ReplyPayload } from "../types.js";
1818
import { normalizeReplyPayload } from "./normalize-reply.js";
1919
import { shouldSuppressReasoningPayload } from "./reply-payloads.js";
2020

21+
let deliverRuntimePromise: Promise<
22+
typeof import("../../infra/outbound/deliver-runtime.js")
23+
> | null = null;
24+
25+
function loadDeliverRuntime() {
26+
deliverRuntimePromise ??= import("../../infra/outbound/deliver-runtime.js");
27+
return deliverRuntimePromise;
28+
}
29+
2130
export type RouteReplyParams = {
2231
/** The reply payload to send. */
2332
payload: ReplyPayload;
@@ -126,7 +135,7 @@ export async function routeReply(params: RouteReplyParams): Promise<RouteReplyRe
126135
try {
127136
// Provider docking: this is an execution boundary (we're about to send).
128137
// Keep the module cheap to import by loading outbound plumbing lazily.
129-
const { deliverOutboundPayloads } = await import("../../infra/outbound/deliver.js");
138+
const { deliverOutboundPayloads } = await loadDeliverRuntime();
130139
const outboundSession = buildOutboundSessionContext({
131140
cfg,
132141
agentId: resolvedAgentId,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { sendMessageDiscord } from "../discord/send.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { sendMessageIMessage } from "../imessage/send.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { sendMessageSignal } from "../signal/send.js";

src/cli/deps-send-slack.runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { sendMessageSlack } from "../slack/send.js";

0 commit comments

Comments
 (0)