{"record":{"id":"f79f2306d7cf73f2","repo":"google-gemini/gemini-cli","slug":"agent-executor-not-found-in-context","errorCode":null,"errorMessage":"Agent executor not found in context.","messagePattern":"Agent executor not found in context\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/a2a-server/src/commands/init.ts","lineNumber":86,"sourceCode":"    eventBus.publish(event);\n    return {\n      name: this.name,\n      data: result,\n    };\n  }\n\n  private async handleSubmitPromptResult(\n    result: { content: unknown },\n    context: CommandContext,\n    geminiMdPath: string,\n    eventBus: ExecutionEventBus,\n    taskId: string,\n    contextId: string,\n  ): Promise<CommandExecutionResponse> {\n    fs.writeFileSync(geminiMdPath, '', 'utf8');\n\n    if (!context.agentExecutor) {\n      throw new Error('Agent executor not found in context.');\n    }\n    // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion\n    const agentExecutor = context.agentExecutor as CoderAgentExecutor;\n\n    const agentSettings: AgentSettings = {\n      kind: CoderAgentEvent.StateAgentSettingsEvent,\n      workspacePath: process.env['CODER_AGENT_WORKSPACE_PATH']!,\n      autoExecute: true,\n    };\n\n    if (typeof result.content !== 'string') {\n      throw new Error('Init command content must be a string.');\n    }\n    const promptText = result.content;\n\n    const requestContext: RequestContext = {\n      userMessage: {\n        kind: 'message',","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/google-gemini/gemini-cli/blob/5024443c7217464a66e98f80d73172a26440bd8f/packages/a2a-server/src/commands/init.ts#L68-L104","documentation":"Thrown by InitCommand.handleSubmitPromptResult when context.agentExecutor is falsy. The /init command's submit_prompt branch needs to drive the full agent loop (agentExecutor.execute) to generate GEMINI.md, so it requires a CoderAgentExecutor in the CommandContext. Without it the command cannot run.","triggerScenarios":"Invoking the /init command via a code path that constructs CommandContext without populating agentExecutor - e.g. a test harness, an alternative command dispatcher, or a miswired server bootstrap that omits the executor.","commonSituations":"Running InitCommand.execute from a unit test with a stub context; refactoring the server to add a second command router that forgets to inject agentExecutor; calling /init before the CoderAgentExecutor is initialized.","solutions":["Ensure the CommandContext passed to InitCommand.execute has agentExecutor set to a CoderAgentExecutor instance before invocation.","If running in a context where agentic generation is unavailable, route to the 'message' branch instead of submit_prompt (e.g. pre-create GEMINI.md so performInit returns a message).","In tests, populate context.agentExecutor with a mock executor that implements execute().","Gate the /init command behind a check that the server is fully bootstrapped."],"exampleFix":"// before\nif (!context.agentExecutor) {\n  throw new Error('Agent executor not found in context.');\n}\n\n// after (fail fast at command registration)\nexecute(context) {\n  if (!context.agentExecutor) {\n    return { name: this.name, data: 'Init unavailable: agent executor not initialized.' };\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"function canRunInit(context: CommandContext): boolean {\n  return !!context.agentExecutor;\n}\nif (!canRunInit(context)) {\n  return { name: 'init', data: 'Agent executor unavailable; cannot generate GEMINI.md.' };\n}","typeGuard":"function hasAgentExecutor(c: CommandContext): c is CommandContext & { agentExecutor: NonNullable<CommandContext['agentExecutor']> } {\n  return !!c.agentExecutor;\n}","tryCatchPattern":null,"preventionTips":["Construct CommandContext in exactly one place and assert agentExecutor is set there.","In tests, inject a mock executor that implements execute().","Gate the /init command registration on executor readiness.","If init is optional, fall back to the message branch by pre-creating GEMINI.md."],"tags":["init-command","dependency-injection","command-context"],"backgroundTag":null,"analyzedSha":"5024443c7217464a66e98f80d73172a26440bd8f","analyzedAt":"2026-08-12T06:01:53.711Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}