55 */
66
77import type { Config } from '../config/config.js' ;
8+ import { type AgentLoopContext } from '../config/agent-loop-context.js' ;
89import { reportError } from '../utils/errorReporting.js' ;
910import { GeminiChat , StreamEventType } from '../core/geminiChat.js' ;
1011import {
@@ -92,29 +93,33 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
9293
9394 private readonly agentId : string ;
9495 private readonly toolRegistry : ToolRegistry ;
95- private readonly runtimeContext : Config ;
96+ private readonly context : AgentLoopContext ;
9697 private readonly onActivity ?: ActivityCallback ;
9798 private readonly compressionService : ChatCompressionService ;
9899 private readonly parentCallId ?: string ;
99100 private hasFailedCompressionAttempt = false ;
100101
102+ private get config ( ) : Config {
103+ return this . context . config ;
104+ }
105+
101106 /**
102107 * Creates and validates a new `AgentExecutor` instance.
103108 *
104109 * This method ensures that all tools specified in the agent's definition are
105110 * safe for non-interactive use before creating the executor.
106111 *
107112 * @param definition The definition object for the agent.
108- * @param runtimeContext The global runtime configuration .
113+ * @param context The execution context .
109114 * @param onActivity An optional callback to receive activity events.
110115 * @returns A promise that resolves to a new `LocalAgentExecutor` instance.
111116 */
112117 static async create < TOutput extends z . ZodTypeAny > (
113118 definition : LocalAgentDefinition < TOutput > ,
114- runtimeContext : Config ,
119+ context : AgentLoopContext ,
115120 onActivity ?: ActivityCallback ,
116121 ) : Promise < LocalAgentExecutor < TOutput > > {
117- const parentMessageBus = runtimeContext . getMessageBus ( ) ;
122+ const parentMessageBus = context . messageBus ;
118123
119124 // Create an override object to inject the subagent name into tool confirmation requests
120125 // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
@@ -133,12 +138,12 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
133138
134139 // Create an isolated tool registry for this agent instance.
135140 const agentToolRegistry = new ToolRegistry (
136- runtimeContext ,
141+ context . config ,
137142 subagentMessageBus ,
138143 ) ;
139- const parentToolRegistry = runtimeContext . getToolRegistry ( ) ;
144+ const parentToolRegistry = context . toolRegistry ;
140145 const allAgentNames = new Set (
141- runtimeContext . getAgentRegistry ( ) . getAllAgentNames ( ) ,
146+ context . config . getAgentRegistry ( ) . getAllAgentNames ( ) ,
142147 ) ;
143148
144149 const registerToolByName = ( toolName : string ) => {
@@ -190,15 +195,15 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
190195 agentToolRegistry . sortTools ( ) ;
191196
192197 // Get the parent prompt ID from context
193- const parentPromptId = promptIdContext . getStore ( ) ;
198+ const parentPromptId = context . promptId ;
194199
195200 // Get the parent tool call ID from context
196201 const toolContext = getToolCallContext ( ) ;
197202 const parentCallId = toolContext ?. callId ;
198203
199204 return new LocalAgentExecutor (
200205 definition ,
201- runtimeContext ,
206+ context ,
202207 agentToolRegistry ,
203208 parentPromptId ,
204209 parentCallId ,
@@ -214,14 +219,14 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
214219 */
215220 private constructor (
216221 definition : LocalAgentDefinition < TOutput > ,
217- runtimeContext : Config ,
222+ context : AgentLoopContext ,
218223 toolRegistry : ToolRegistry ,
219224 parentPromptId : string | undefined ,
220225 parentCallId : string | undefined ,
221226 onActivity ?: ActivityCallback ,
222227 ) {
223228 this . definition = definition ;
224- this . runtimeContext = runtimeContext ;
229+ this . context = context ;
225230 this . toolRegistry = toolRegistry ;
226231 this . onActivity = onActivity ;
227232 this . compressionService = new ChatCompressionService ( ) ;
@@ -418,7 +423,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
418423 } finally {
419424 clearTimeout ( graceTimeoutId ) ;
420425 logRecoveryAttempt (
421- this . runtimeContext ,
426+ this . config ,
422427 new RecoveryAttemptEvent (
423428 this . agentId ,
424429 this . definition . name ,
@@ -466,7 +471,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
466471 const combinedSignal = AbortSignal . any ( [ signal , deadlineTimer . signal ] ) ;
467472
468473 logAgentStart (
469- this . runtimeContext ,
474+ this . config ,
470475 new AgentStartEvent ( this . agentId , this . definition . name ) ,
471476 ) ;
472477
@@ -477,7 +482,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
477482 const augmentedInputs = {
478483 ...inputs ,
479484 cliVersion : await getVersion ( ) ,
480- activeModel : this . runtimeContext . getActiveModel ( ) ,
485+ activeModel : this . config . getActiveModel ( ) ,
481486 today : new Date ( ) . toLocaleDateString ( ) ,
482487 } ;
483488
@@ -494,13 +499,12 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
494499 // Capture the index of the last hint before starting to avoid re-injecting old hints.
495500 // NOTE: Hints added AFTER this point will be broadcast to all currently running
496501 // local agents via the listener below.
497- const startIndex =
498- this . runtimeContext . userHintService . getLatestHintIndex ( ) ;
499- this . runtimeContext . userHintService . onUserHint ( hintListener ) ;
502+ const startIndex = this . config . userHintService . getLatestHintIndex ( ) ;
503+ this . config . userHintService . onUserHint ( hintListener ) ;
500504
501505 try {
502506 const initialHints =
503- this . runtimeContext . userHintService . getUserHintsAfter ( startIndex ) ;
507+ this . config . userHintService . getUserHintsAfter ( startIndex ) ;
504508 const formattedInitialHints = formatUserHintsForModel ( initialHints ) ;
505509
506510 let currentMessage : Content = formattedInitialHints
@@ -561,7 +565,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
561565 }
562566 }
563567 } finally {
564- this . runtimeContext . userHintService . offUserHint ( hintListener ) ;
568+ this . config . userHintService . offUserHint ( hintListener ) ;
565569 }
566570
567571 // === UNIFIED RECOVERY BLOCK ===
@@ -674,7 +678,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
674678 } finally {
675679 deadlineTimer . abort ( ) ;
676680 logAgentFinish (
677- this . runtimeContext ,
681+ this . config ,
678682 new AgentFinishEvent (
679683 this . agentId ,
680684 this . definition . name ,
@@ -697,7 +701,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
697701 prompt_id ,
698702 false ,
699703 model ,
700- this . runtimeContext ,
704+ this . config ,
701705 this . hasFailedCompressionAttempt ,
702706 ) ;
703707
@@ -735,11 +739,10 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
735739 const modelConfigAlias = getModelConfigAlias ( this . definition ) ;
736740
737741 // Resolve the model config early to get the concrete model string (which may be `auto`).
738- const resolvedConfig =
739- this . runtimeContext . modelConfigService . getResolvedConfig ( {
740- model : modelConfigAlias ,
741- overrideScope : this . definition . name ,
742- } ) ;
742+ const resolvedConfig = this . config . modelConfigService . getResolvedConfig ( {
743+ model : modelConfigAlias ,
744+ overrideScope : this . definition . name ,
745+ } ) ;
743746 const requestedModel = resolvedConfig . model ;
744747
745748 let modelToUse : string ;
@@ -756,7 +759,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
756759 signal,
757760 requestedModel,
758761 } ;
759- const router = this . runtimeContext . getModelRouterService ( ) ;
762+ const router = this . config . getModelRouterService ( ) ;
760763 const decision = await router . route ( routingContext ) ;
761764 modelToUse = decision . model ;
762765 } catch ( error ) {
@@ -844,7 +847,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
844847
845848 try {
846849 return new GeminiChat (
847- this . runtimeContext ,
850+ this . config ,
848851 systemInstruction ,
849852 [ { functionDeclarations : tools } ] ,
850853 startHistory ,
@@ -1092,7 +1095,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
10921095 // Execute standard tool calls using the new scheduler
10931096 if ( toolRequests . length > 0 ) {
10941097 const completedCalls = await scheduleAgentTools (
1095- this . runtimeContext ,
1098+ this . config ,
10961099 toolRequests ,
10971100 {
10981101 schedulerId : this . agentId ,
@@ -1240,7 +1243,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
12401243 let finalPrompt = templateString ( promptConfig . systemPrompt , inputs ) ;
12411244
12421245 // Append environment context (CWD and folder structure).
1243- const dirContext = await getDirectoryContextString ( this . runtimeContext ) ;
1246+ const dirContext = await getDirectoryContextString ( this . config ) ;
12441247 finalPrompt += `\n\n# Environment Context\n${ dirContext } ` ;
12451248
12461249 // Append standard rules for non-interactive execution.
0 commit comments