@@ -49,6 +49,15 @@ const FAST_TEST_RETRY_INTERVAL_MS = 8;
4949const FAST_TEST_REPLY_CHANGE_WAIT_MS = 20 ;
5050const DEFAULT_SUBAGENT_ANNOUNCE_TIMEOUT_MS = 60_000 ;
5151const 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+
5261const 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 ) ,
0 commit comments