@leanandmean/coding-agent 0.74.1-scramjet.68 → 0.74.1-scramjet.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +57 -20
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +3 -3
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/settings-manager.d.ts +1 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +47 -1
- package/dist/core/settings-manager.js.map +1 -1
- package/docs/compaction.md +3 -1
- package/docs/custom-provider.md +23 -3
- package/docs/sdk.md +6 -0
- package/docs/settings.md +6 -2
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAIhH,OAAO,KAAK,EAAoB,YAAY,EAAW,KAAK,EAAuB,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAexH,OAAO,EAAE,KAAK,UAAU,EAA6B,MAAM,oBAAoB,CAAC;AAChF,OAAO,EACN,KAAK,gBAAgB,EAQrB,MAAM,uBAAuB,CAAC;AAI/B,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,wBAAwB,EAC7B,KAAK,8BAA8B,EACnC,KAAK,sBAAsB,EAC3B,eAAe,EACf,KAAK,kBAAkB,EAEvB,KAAK,WAAW,EAChB,KAAK,wBAAwB,EAI7B,KAAK,sBAAsB,EAC3B,KAAK,oBAAoB,EAGzB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,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,OAAO,CAAC;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAC;QAAC,YAAY,CAAC,EAAE,kBAAkB,CAAA;KAAE,CAAC,CAqL5G;IAED;;OAEG;IACH,yBAAyB,IAAI,KAAK,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAgBpE;IAGD,sBAAsB,IAAI,MAAM,CA4B/B;IAED;;OAEG;IACH,eAAe,IAAI,YAAY,CA2C9B;IAED,eAAe,IAAI,YAAY,GAAG,SAAS,CA4C1C;IAED;;;;OAIG;IACG,YAAY,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAevD;IAED;;;;;OAKG;IACH,aAAa,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CA4BzC;IAMD;;;;OAIG;IACH,oBAAoB,IAAI,MAAM,GAAG,SAAS,CAsBzC;IAMD,4BAA4B,IAAI,sBAAsB,CASrD;IAED;;OAEG;IACH,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAE/C;IAED;;OAEG;IACH,IAAI,eAAe,IAAI,eAAe,CAErC;CACD","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * This class is shared between all run modes (interactive, print, rpc).\n * It encapsulates:\n * - Agent state access\n * - Event subscription with automatic session persistence\n * - Model and thinking level management\n * - Compaction (manual and auto)\n * - Bash execution\n * - Session switching and branching\n *\n * Modes use this class and add their own I/O layer on top.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { basename, dirname, resolve } from \"node:path\";\nimport type { Agent, AgentEvent, AgentMessage, AgentState, AgentTool, ThinkingLevel } from \"@leanandmean/agent\";\n// SCRAMJET-DIVERGENCE: provider-safe harness tool-call id generation for the persisted-settlement\n// acknowledgement key (#341).\nimport { generateHarnessToolCallId } from \"@leanandmean/agent\";\nimport type { AssistantMessage, ImageContent, Message, Model, SystemPromptSection, TextContent } from \"@leanandmean/ai\";\nimport {\n\tclampThinkingLevel,\n\tcleanupSessionResources,\n\tflattenSystemPrompt,\n\tgetSupportedThinkingLevels,\n\tisContextOverflow,\n\tmodelsAreEqual,\n\tresetApiProviders,\n} from \"@leanandmean/ai\";\nimport { validateResponsesProviderFailure } from \"@leanandmean/ai/openai-responses\";\nimport { theme } from \"../modes/interactive/theme/theme.js\";\nimport { stripFrontmatter } from \"../utils/frontmatter.js\";\nimport { sleep } from \"../utils/sleep.js\";\nimport { formatNoApiKeyFoundMessage, formatNoModelSelectedMessage } from \"./auth-guidance.js\";\nimport { type BashResult, executeBashWithOperations } from \"./bash-executor.js\";\nimport {\n\ttype CompactionResult,\n\tcalculateContextTokens,\n\tcollectEntriesForBranchSummary,\n\tcompact,\n\testimateContextTokens,\n\tgenerateBranchSummary,\n\tprepareCompaction,\n\tshouldCompact,\n} from \"./compaction/index.js\";\nimport { DEFAULT_THINKING_LEVEL } from \"./defaults.js\";\nimport { exportSessionToHtml, type ToolHtmlRenderer } from \"./export-html/index.js\";\nimport { createToolHtmlRenderer } from \"./export-html/tool-renderer.js\";\nimport {\n\ttype ContextUsage,\n\ttype DeliverAs,\n\ttype DispatchUserInputOptions,\n\ttype ExtensionCommandContextActions,\n\ttype ExtensionErrorListener,\n\tExtensionRunner,\n\ttype ExtensionUIContext,\n\ttype InputSessionEntry,\n\ttype InputSource,\n\ttype InvokeHarnessToolOptions,\n\ttype MessageEndEvent,\n\ttype MessageStartEvent,\n\ttype MessageUpdateEvent,\n\ttype ReplacedSessionContext,\n\ttype SendMessageDeliverAs,\n\ttype SessionBeforeCompactResult,\n\ttype SessionBeforeTreeResult,\n\ttype SessionStartEvent,\n\ttype ShutdownHandler,\n\ttype ToolDefinition,\n\ttype ToolExecutionEndEvent,\n\ttype ToolExecutionStartEvent,\n\ttype ToolExecutionUpdateEvent,\n\ttype ToolInfo,\n\ttype TreePreparation,\n\ttype TurnEndEvent,\n\ttype TurnStartEvent,\n\twrapRegisteredTools,\n} from \"./extensions/index.js\";\nimport { emitSessionShutdownEvent, spliceContributedSections } from \"./extensions/runner.js\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.js\";\nimport type { ModelRegistry } from \"./model-registry.js\";\nimport { type OutputThroughputSample, OutputThroughputTracker } from \"./output-throughput.js\";\nimport { OutputThroughputHistoryStore } from \"./output-throughput-history.js\";\nimport { expandPromptTemplate, type PromptTemplate } from \"./prompt-templates.js\";\nimport type { ResourceExtensionPaths, ResourceLoader } from \"./resource-loader.js\";\nimport { indexScramjetCommandStarts, restoreScramjetCommandInvocation } from \"./scramjet-command-parser.js\";\nimport type { BranchSummaryEntry, CompactionEntry, SessionManager } from \"./session-manager.js\";\nimport { CURRENT_SESSION_VERSION, getLatestCompactionEntry, type SessionHeader } from \"./session-manager.js\";\nimport type { SettingsManager } from \"./settings-manager.js\";\nimport type { SlashCommandInfo } from \"./slash-commands.js\";\nimport { createSyntheticSourceInfo, type SourceInfo } from \"./source-info.js\";\nimport { type BuildSystemPromptOptions, buildSystemPromptSections } from \"./system-prompt.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { createAllToolDefinitions } from \"./tools/index.js\";\nimport { createToolDefinitionFromAgentTool } from \"./tools/tool-definition-wrapper.js\";\n\n// ============================================================================\n// Skill Block Parsing\n// ============================================================================\n\n/** Parsed skill block from a user message */\nexport interface ParsedSkillBlock {\n\tname: string;\n\tlocation: string;\n\tcontent: string;\n\tuserMessage: string | undefined;\n}\n\n/**\n * Parse a skill block from message text.\n * Returns null if the text doesn't contain a skill block.\n */\nexport function parseSkillBlock(text: string): ParsedSkillBlock | null {\n\tconst match = text.match(/^<skill name=\"([^\"]+)\" location=\"([^\"]+)\">\\n([\\s\\S]*?)\\n<\\/skill>(?:\\n\\n([\\s\\S]+))?$/);\n\tif (!match) return null;\n\treturn {\n\t\tname: match[1],\n\t\tlocation: match[2],\n\t\tcontent: match[3],\n\t\tuserMessage: match[4]?.trim() || undefined,\n\t};\n}\n\n/** Session-specific events that extend the core AgentEvent */\nexport type AgentSessionEvent =\n\t| AgentEvent\n\t| {\n\t\t\ttype: \"queue_update\";\n\t\t\tsteering: readonly string[];\n\t\t\tfollowUp: readonly string[];\n\t }\n\t| { type: \"compaction_start\"; reason: \"manual\" | \"threshold\" | \"overflow\" }\n\t| { type: \"session_info_changed\"; name: string | undefined }\n\t| { type: \"thinking_level_changed\"; level: ThinkingLevel }\n\t| {\n\t\t\ttype: \"compaction_end\";\n\t\t\treason: \"manual\" | \"threshold\" | \"overflow\";\n\t\t\tresult: CompactionResult | undefined;\n\t\t\taborted: boolean;\n\t\t\twillRetry: boolean;\n\t\t\terrorMessage?: string;\n\t }\n\t| {\n\t\t\ttype: \"auto_retry_start\";\n\t\t\tattempt: number;\n\t\t\tmaxAttempts: number;\n\t\t\tdelayMs: number;\n\t\t\terrorMessage: string;\n\t\t\tcumulativeErrors?: number;\n\t }\n\t| { type: \"auto_retry_end\"; success: boolean; attempt: number; finalError?: string };\n\n/** Listener function for agent session events */\nexport type AgentSessionEventListener = (event: AgentSessionEvent) => void;\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface AgentSessionConfig {\n\tagent: Agent;\n\tsessionManager: SessionManager;\n\tsettingsManager: SettingsManager;\n\tcwd: string;\n\toutputThroughputHistoryPath: string;\n\t/** Models to cycle through with Ctrl+P (from --models flag) */\n\tscopedModels?: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;\n\t/** Resource loader for skills, prompts, themes, context files, system prompt */\n\tresourceLoader: ResourceLoader;\n\t/** SDK custom tools registered outside extensions */\n\tcustomTools?: ToolDefinition[];\n\t/** Model registry for API key resolution and model discovery */\n\tmodelRegistry: ModelRegistry;\n\t/** Initial active built-in tool names. Default: [read, bash, edit, write] */\n\tinitialActiveToolNames?: string[];\n\t/** Optional allowlist of tool names. When provided, only these tool names are exposed. */\n\tallowedToolNames?: string[];\n\t/**\n\t * Override base tools (useful for custom runtimes).\n\t *\n\t * These are synthesized into minimal ToolDefinitions internally so AgentSession can keep\n\t * a definition-first registry even when callers provide plain AgentTool instances.\n\t */\n\tbaseToolsOverride?: Record<string, AgentTool>;\n\t/** Mutable ref used by Agent to access the current ExtensionRunner */\n\textensionRunnerRef?: { current?: ExtensionRunner };\n\t/** Session start event metadata emitted when extensions bind to this runtime. */\n\tsessionStartEvent?: SessionStartEvent;\n}\n\nexport interface ExtensionBindings {\n\tuiContext?: ExtensionUIContext;\n\tcommandContextActions?: ExtensionCommandContextActions;\n\tshutdownHandler?: ShutdownHandler;\n\tonError?: ExtensionErrorListener;\n}\n\ntype RunPromptComposition =\n\t| { kind: \"generated-base\" }\n\t| { kind: \"contributed-sections\"; sections: SystemPromptSection[] }\n\t| { kind: \"authoritative-string\"; systemPrompt: string };\n\n/** Options for AgentSession.prompt() */\nexport interface PromptOptions {\n\t/** Whether to expand file-based prompt templates (default: true) */\n\texpandPromptTemplates?: boolean;\n\t/** Image attachments */\n\timages?: ImageContent[];\n\t/**\n\t * When streaming, how to queue the message: \"steer\" (interrupt) or\n\t * \"followUp\" (wait). Prompting while streaming without it rejects, unless\n\t * the text is handled before it reaches the prompt queue (extension\n\t * commands execute immediately; input consumed by an `input` handler\n\t * returns without rejecting).\n\t */\n\tstreamingBehavior?: DeliverAs;\n\t/** Source of input for extension input event handlers. Defaults to \"interactive\". */\n\tsource?: InputSource;\n\t/** Internal hook used by RPC mode to observe prompt preflight acceptance or rejection. */\n\tpreflightResult?: (success: boolean) => void;\n}\n\n/** Result from cycleModel() */\nexport interface ModelCycleResult {\n\tmodel: Model<any>;\n\tthinkingLevel: ThinkingLevel;\n\t/** Whether cycling through scoped models (--models flag) or all available */\n\tisScoped: boolean;\n}\n\n/** Session statistics for /session command */\nexport interface SessionStats {\n\tsessionFile: string | undefined;\n\tsessionId: string;\n\tuserMessages: number;\n\tassistantMessages: number;\n\ttoolCalls: number;\n\ttoolResults: number;\n\ttotalMessages: number;\n\ttokens: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n\tcost: number;\n\tcontextUsage?: ContextUsage;\n}\n\ninterface ToolDefinitionEntry {\n\tdefinition: ToolDefinition;\n\tsourceInfo: SourceInfo;\n}\n\n// ============================================================================\n// Constants\n// ============================================================================\n\n/** Standard thinking levels */\nconst THINKING_LEVELS: ThinkingLevel[] = [\"off\", \"minimal\", \"low\", \"medium\", \"high\"];\n\n// ============================================================================\n// AgentSession Class\n// ============================================================================\n\ntype Deferred<T = void> = {\n\tpromise: Promise<T>;\n\tresolve: (value: T) => void;\n\treject: (reason: Error) => void;\n\tsettled: boolean;\n};\n\nfunction createDeferred<T = void>(): Deferred<T> {\n\tlet resolve!: (value: T) => void;\n\tlet reject!: (reason: Error) => void;\n\tconst promise = new Promise<T>((res, rej) => {\n\t\tresolve = res;\n\t\treject = rej;\n\t});\n\tpromise.catch(() => {});\n\treturn { promise, resolve, reject, settled: false };\n}\n\nfunction resolveDeferred<T>(deferred: Deferred<T>, value: T): boolean {\n\tif (deferred.settled) return false;\n\tdeferred.settled = true;\n\tdeferred.resolve(value);\n\treturn true;\n}\n\nfunction rejectDeferred(deferred: Pick<Deferred<unknown>, \"reject\" | \"settled\">, error: Error): boolean {\n\tif (deferred.settled) return false;\n\tdeferred.settled = true;\n\tdeferred.reject(error);\n\treturn true;\n}\n\nfunction settleDeferred(deferred: Deferred, error: Error | undefined): boolean {\n\treturn error ? rejectDeferred(deferred, error) : resolveDeferred(deferred, undefined);\n}\n\n// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement acknowledgement (#341). Each invocation\n// retains synchronous event attribution and its queue tail until Agent execution and persistence both\n// settle, so an early write failure cannot leak later invocation failures into ordinary run state.\ntype HarnessPersistenceAck = Deferred & {\n\tpersistenceTail: Promise<void>;\n\tfirstPersistenceError?: Error;\n};\n\ntype RetryChainSettlement = Deferred;\n\ntype AgentRunSettlement = Deferred & {\n\tchain: RetryChainSettlement;\n\tpersistedAssistantSnapshot?: AssistantMessage;\n\tassistantPersistenceError?: Error;\n};\n\ntype AgentRunStartReservation = Deferred<AgentRunSettlement> & { run?: AgentRunSettlement };\n\n// SCRAMJET-DIVERGENCE: one retry-phase value owns \"retry is active\" (#553). Every finish flows through\n// `_finishRetry`, so a stranded controller, attempt counter, or handoff cannot exist independently.\ntype RetryPhase =\n\t| { phase: \"backoff\"; attempt: number; controller: AbortController; run: AgentRunSettlement }\n\t| { phase: \"continuing\"; attempt: number; chain: RetryChainSettlement };\n\ntype AutoRetryEvidence = \"provider_failure\" | \"legacy_text\" | \"context_overflow\" | \"none\";\ntype NotAttemptedCause =\n\t| { reason: \"structured_non_transient\" | \"structured_unknown\"; evidence: \"provider_failure\" }\n\t| {\n\t\t\treason: \"malformed_provider_diagnostic\" | \"duplicate_provider_diagnostic\" | \"missing_error_evidence\";\n\t\t\tevidence: \"none\";\n\t }\n\t| { reason: \"legacy_non_retryable\"; evidence: \"legacy_text\" };\n// SCRAMJET-DIVERGENCE: closed, replay-inert retry decisions and outcomes preserve #553 evidence invariants.\ntype AutoRetryRecord =\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"not_attempted\";\n\t\t\treason: \"retry_disabled\" | \"session_disposed\";\n\t\t\tevidence: AutoRetryEvidence;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"not_attempted\";\n\t\t\treason: \"context_overflow_compaction\";\n\t\t\tevidence: \"context_overflow\";\n\t }\n\t| ({ schemaVersion: 1; outcome: \"not_attempted\" } & NotAttemptedCause)\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"scheduled\";\n\t\t\tevidence: \"provider_failure\" | \"legacy_text\";\n\t\t\tattempt: number;\n\t\t\tmaxAttempts: number;\n\t\t\tcumulativeErrors: number;\n\t\t\tdelayMs: number;\n\t }\n\t| { schemaVersion: 1; outcome: \"succeeded\"; attemptsCompleted: number; cumulativeErrors: number }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"failed\";\n\t\t\treason: \"continuation_rejected\" | \"run_failed\";\n\t\t\tattempt: number;\n\t\t\tcumulativeErrors: number;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"cancelled\";\n\t\t\treason: \"cancelled_during_backoff\" | \"cancelled_during_continuation\" | \"session_disposed\";\n\t\t\tattempt: number;\n\t\t\tcumulativeErrors: number;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"exhausted\";\n\t\t\treason: \"attempt_limit\" | \"cumulative_limit\";\n\t\t\tattemptsCompleted: number;\n\t\t\tmaxAttempts: number;\n\t\t\tcumulativeErrors: number;\n\t };\n\ntype RetryClassification =\n\t| { kind: \"retry\"; evidence: \"provider_failure\" | \"legacy_text\" }\n\t| { kind: \"context_overflow\" }\n\t| ({ kind: \"do_not_retry\" } & NotAttemptedCause);\n\nfunction validRecordedCost(value: unknown): value is number {\n\treturn typeof value === \"number\" && Number.isFinite(value) && value >= 0;\n}\n\ninterface LiveToolCost {\n\tcost?: number;\n}\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\treadonly outputThroughputHistoryPath: string;\n\n\tprivate _scopedModels: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;\n\n\t// Event subscription state\n\tprivate _unsubscribeAgent?: () => void;\n\tprivate _eventListeners: AgentSessionEventListener[] = [];\n\tprivate _agentEventQueue: Promise<void> = Promise.resolve();\n\t// SCRAMJET-DIVERGENCE: execution ownership survives final-event queue latency until append succeeds (#598).\n\tprivate readonly _toolCostExecutions = new Map<string, LiveToolCost>();\n\tprivate readonly _pendingToolCosts = new Set<LiveToolCost>();\n\tprivate _historicalCostDefinitions = new Map<string, ToolDefinition>();\n\tprivate readonly _outputThroughputTracker = new OutputThroughputTracker();\n\tprivate readonly _outputThroughputHistory: OutputThroughputHistoryStore;\n\tprivate _outputThroughputGeneration: number | undefined;\n\tprivate _latestOutputThroughputSample: OutputThroughputSample | undefined;\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement state (#341). `_disposed` gates post-\n\t// teardown invocations; the map keys each in-flight invocation's tool-call id to its persistence\n\t// acknowledgement so `_processAgentEvent` can settle it after the matching tool-result is persisted.\n\tprivate _disposed = false;\n\tprivate readonly _harnessPersistenceAcks = new Map<string, HarnessPersistenceAck>();\n\n\t/** Tracks pending steering messages for UI display. Removed when delivered. */\n\tprivate _steeringMessages: string[] = [];\n\t/** Tracks pending follow-up messages for UI display. Removed when delivered. */\n\tprivate _followUpMessages: string[] = [];\n\t/** Messages queued to be included with the next user prompt as context (\"asides\"). */\n\tprivate _pendingNextTurnMessages: CustomMessage[] = [];\n\tprivate readonly _inputSessionEntries = new WeakMap<AgentMessage, InputSessionEntry[]>();\n\n\t// Compaction state\n\tprivate _compactionAbortController: AbortController | undefined = undefined;\n\tprivate _manualCompactionDrain: { firstError?: Error } | undefined;\n\tprivate _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprivate _overflowRecoveryAttempted = false;\n\n\t// Branch summarization state\n\tprivate _branchSummaryAbortController: AbortController | undefined = undefined;\n\n\t// Retry state\n\tprivate _retry: RetryPhase | undefined = undefined;\n\tprivate _runRetryCount = 0;\n\tprivate _activeAgentRunSettlement: AgentRunSettlement | undefined = undefined;\n\tprivate _pendingAgentRunStart: AgentRunStartReservation | undefined = undefined;\n\tprivate readonly _unsettledAgentRuns = new Set<AgentRunSettlement>();\n\tprivate readonly _unsettledRetryChains = new Set<RetryChainSettlement>();\n\n\t// Bash execution state\n\tprivate _bashAbortController: AbortController | undefined = undefined;\n\tprivate _pendingBashMessages: BashExecutionMessage[] = [];\n\n\t// Extension system\n\tprivate _extensionRunner!: ExtensionRunner;\n\tprivate _turnIndex = 0;\n\n\tprivate _resourceLoader: ResourceLoader;\n\tprivate _customTools: ToolDefinition[];\n\tprivate _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprivate _cwd: string;\n\tprivate _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprivate _initialActiveToolNames?: string[];\n\tprivate _allowedToolNames?: Set<string>;\n\tprivate _baseToolsOverride?: Record<string, AgentTool>;\n\tprivate _sessionStartEvent: SessionStartEvent;\n\tprivate _extensionUIContext?: ExtensionUIContext;\n\tprivate _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprivate _extensionShutdownHandler?: ShutdownHandler;\n\tprivate _extensionErrorListener?: ExtensionErrorListener;\n\tprivate _extensionErrorUnsubscriber?: () => void;\n\n\t// Model registry for API key resolution\n\tprivate _modelRegistry: ModelRegistry;\n\n\t// Tool registry for extension getTools/setTools\n\tprivate _toolRegistry: Map<string, AgentTool> = new Map();\n\tprivate _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprivate _toolPromptSnippets: Map<string, string> = new Map();\n\tprivate _toolPromptGuidelines: Map<string, string[]> = new Map();\n\n\t// Base system prompt as ordered sections (without extension appends) - used to\n\t// apply fresh appends each turn. Sent to the agent as sections so providers\n\t// can apply per-section cache control.\n\tprivate _baseSystemPromptSections: SystemPromptSection[] = [];\n\tprivate _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\t// SCRAMJET-DIVERGENCE: active-tool prompt rebuilds preserve the current top-level run's\n\t// before_agent_start composition while new prompts and reloads establish a fresh composition (#524).\n\tprivate _runPromptComposition: RunPromptComposition = { kind: \"generated-base\" };\n\n\tconstructor(config: AgentSessionConfig) {\n\t\tthis.agent = config.agent;\n\t\tthis.sessionManager = config.sessionManager;\n\t\tthis.settingsManager = config.settingsManager;\n\t\tthis.outputThroughputHistoryPath = config.outputThroughputHistoryPath;\n\t\tthis._outputThroughputHistory = new OutputThroughputHistoryStore(\n\t\t\tthis.outputThroughputHistoryPath,\n\t\t\t(diagnostic) => {\n\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\textensionPath: \"output-throughput-history\",\n\t\t\t\t\tevent: diagnostic.operation,\n\t\t\t\t\terror: `${diagnostic.message} (${diagnostic.path})`,\n\t\t\t\t\tstack: diagnostic.cause instanceof Error ? diagnostic.cause.stack : undefined,\n\t\t\t\t});\n\t\t\t},\n\t\t);\n\t\tthis._scopedModels = config.scopedModels ?? [];\n\t\tthis._resourceLoader = config.resourceLoader;\n\t\tthis._customTools = config.customTools ?? [];\n\t\tthis._cwd = config.cwd;\n\t\tthis._modelRegistry = config.modelRegistry;\n\t\tthis._extensionRunnerRef = config.extensionRunnerRef;\n\t\tthis._initialActiveToolNames = config.initialActiveToolNames;\n\t\tthis._allowedToolNames = config.allowedToolNames ? new Set(config.allowedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\n\t\t// Always subscribe to agent events for internal handling\n\t\t// (session persistence, extensions, auto-compaction, retry logic)\n\t\tthis._unsubscribeAgent = this.agent.subscribe(this._handleAgentEvent);\n\t\tthis._installAgentToolHooks();\n\n\t\tthis._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n\n\t/** Model registry for API key resolution and model discovery */\n\tget modelRegistry(): ModelRegistry {\n\t\treturn this._modelRegistry;\n\t}\n\n\tprivate async _getRequiredRequestAuth(model: Model<any>): Promise<{\n\t\tapiKey: string;\n\t\theaders?: Record<string, string>;\n\t}> {\n\t\tconst result = await this._modelRegistry.getApiKeyAndHeaders(model);\n\t\tif (!result.ok) {\n\t\t\tif (result.error.startsWith(\"No API key found\")) {\n\t\t\t\tthrow new Error(formatNoApiKeyFoundMessage(model.provider));\n\t\t\t}\n\t\t\tthrow new Error(result.error);\n\t\t}\n\t\tif (result.apiKey) {\n\t\t\treturn { apiKey: result.apiKey, headers: result.headers };\n\t\t}\n\n\t\tconst isOAuth = this._modelRegistry.isUsingOAuth(model);\n\t\tif (isOAuth) {\n\t\t\tthrow new Error(\n\t\t\t\t`Authentication failed for \"${model.provider}\". ` +\n\t\t\t\t\t`Credentials may have expired or network is unavailable. ` +\n\t\t\t\t\t`Run '/login ${model.provider}' to re-authenticate.`,\n\t\t\t);\n\t\t}\n\t\tthrow new Error(formatNoApiKeyFoundMessage(model.provider));\n\t}\n\n\t// SCRAMJET-DIVERGENCE: _drainAgentEventQueue ensures all queued async extension event\n\t// processing has settled before proceeding. Used by beforeToolBatch (pre-extraction drain),\n\t// beforeToolCall (per-tool drain), and the awaited turn_end listener barrier.\n\tprivate async _drainAgentEventQueue(): Promise<void> {\n\t\tawait this._agentEventQueue;\n\t}\n\n\t/**\n\t * Install tool hooks once on the Agent instance.\n\t *\n\t * The callbacks read `this._extensionRunner` at execution time, so extension reload swaps in the\n\t * new runner without reinstalling hooks. Extension-specific tool wrappers are still used to adapt\n\t * registered tool execution to the extension context. Tool call and tool result interception now\n\t * happens here instead of in wrappers.\n\t */\n\tprivate _installAgentToolHooks(): void {\n\t\t// SCRAMJET-DIVERGENCE: beforeToolBatch drains queued extension event processing\n\t\t// so async message_end handlers complete before tool-call extraction.\n\t\tthis.agent.beforeToolBatch = async () => {\n\t\t\tawait this._drainAgentEventQueue();\n\t\t};\n\n\t\tthis.agent.beforeToolCall = async ({ toolCall, args }) => {\n\t\t\tconst runner = this._extensionRunner;\n\t\t\tif (!runner.hasHandlers(\"tool_call\")) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tawait this._drainAgentEventQueue();\n\n\t\t\ttry {\n\t\t\t\treturn await runner.emitToolCall({\n\t\t\t\t\ttype: \"tool_call\",\n\t\t\t\t\ttoolName: toolCall.name,\n\t\t\t\t\ttoolCallId: toolCall.id,\n\t\t\t\t\tinput: args as Record<string, unknown>,\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tthrow err;\n\t\t\t\t}\n\t\t\t\tthrow new Error(`Extension failed, blocking execution: ${String(err)}`);\n\t\t\t}\n\t\t};\n\n\t\tthis.agent.afterToolCall = async ({ toolCall, args, result, isError }) => {\n\t\t\tconst runner = this._extensionRunner;\n\t\t\tif (!runner.hasHandlers(\"tool_result\")) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst hookResult = await runner.emitToolResult({\n\t\t\t\ttype: \"tool_result\",\n\t\t\t\ttoolName: toolCall.name,\n\t\t\t\ttoolCallId: toolCall.id,\n\t\t\t\tinput: args as Record<string, unknown>,\n\t\t\t\tcontent: result.content,\n\t\t\t\tdetails: result.details,\n\t\t\t\tisError,\n\t\t\t});\n\n\t\t\tif (!hookResult) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: hookResult.content,\n\t\t\t\tdetails: hookResult.details,\n\t\t\t\tisError: hookResult.isError ?? isError,\n\t\t\t};\n\t\t};\n\t}\n\n\t// =========================================================================\n\t// Event Subscription\n\t// =========================================================================\n\n\t/** Emit an event to all listeners */\n\tprivate _emit(event: AgentSessionEvent): void {\n\t\tfor (const l of this._eventListeners) {\n\t\t\tl(event);\n\t\t}\n\t}\n\n\tprivate _emitQueueUpdate(): void {\n\t\tthis._emit({\n\t\t\ttype: \"queue_update\",\n\t\t\tsteering: [...this._steeringMessages],\n\t\t\tfollowUp: [...this._followUpMessages],\n\t\t});\n\t}\n\n\t/** Internal handler for agent events - shared by subscribe and reconnect */\n\tprivate _handleAgentEvent = (event: AgentEvent): Promise<void> | void => {\n\t\tif (this._disposed) return;\n\t\tthis._captureOutputThroughput(event);\n\t\tconst toolCost = this._captureToolCost(event);\n\n\t\tconst runSettlement = this._captureAgentRunSettlement(event);\n\t\tconst harnessAck = this._harnessPersistenceAckForEvent(event);\n\t\tconst processing = this._agentEventQueue.then(\n\t\t\t() => this._processAgentEventTracked(event, runSettlement, harnessAck, toolCost),\n\t\t\t() => this._processAgentEventTracked(event, runSettlement, harnessAck, toolCost),\n\t\t);\n\t\tthis._agentEventQueue = processing;\n\t\tif (harnessAck) harnessAck.persistenceTail = processing;\n\n\t\t// Keep queue alive if an event handler fails\n\t\tprocessing.catch(() => {});\n\n\t\t// SCRAMJET-DIVERGENCE: turn_end tool/prompt changes must settle before Agent snapshots\n\t\t// live state for the next provider request (#524).\n\t\tif (event.type === \"turn_end\") return this._agentEventQueue;\n\t};\n\n\t// SCRAMJET-DIVERGENCE: scalar reports are captured before mutable events enter async hooks (#598).\n\tprivate _captureToolCost(event: AgentEvent): LiveToolCost | undefined {\n\t\tif (event.type === \"tool_execution_start\") {\n\t\t\tconst execution: LiveToolCost = {};\n\t\t\tthis._toolCostExecutions.set(event.toolCallId, execution);\n\t\t\tthis._pendingToolCosts.add(execution);\n\t\t\treturn;\n\t\t}\n\t\tlet toolCallId: string;\n\t\tlet reported: unknown;\n\t\tconst final = event.type === \"message_end\" && event.message.role === \"toolResult\";\n\t\tif (event.type === \"tool_execution_update\" || event.type === \"tool_execution_end\") {\n\t\t\ttoolCallId = event.toolCallId;\n\t\t\treported = event.type === \"tool_execution_update\" ? event.partialResult?.cost : event.result?.cost;\n\t\t} else if (event.type === \"message_end\" && event.message.role === \"toolResult\") {\n\t\t\ttoolCallId = event.message.toolCallId;\n\t\t\treported = event.message.cost;\n\t\t} else {\n\t\t\treturn;\n\t\t}\n\t\tlet execution = this._toolCostExecutions.get(toolCallId);\n\t\tif (!execution && validRecordedCost(reported)) {\n\t\t\texecution = {};\n\t\t\tthis._toolCostExecutions.set(toolCallId, execution);\n\t\t\tthis._pendingToolCosts.add(execution);\n\t\t}\n\t\tif (execution && validRecordedCost(reported)) execution.cost = reported;\n\t\tif (final) {\n\t\t\tthis._toolCostExecutions.delete(toolCallId);\n\t\t\treturn execution;\n\t\t}\n\t\treturn;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: Capture provider events synchronously and reset stale throughput lifecycles (#476).\n\tprivate _captureOutputThroughput(event: AgentEvent): void {\n\t\tif (event.type === \"message_start\" && event.message.role === \"assistant\" && event.message.origin === \"provider\") {\n\t\t\tthis._outputThroughputGeneration = this._outputThroughputTracker.start({\n\t\t\t\tprovider: event.message.provider,\n\t\t\t\tmodel: event.message.model,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tconst generation = this._outputThroughputGeneration;\n\t\tif (generation === undefined) return;\n\t\tif (event.type === \"message_update\" && event.message.role === \"assistant\") {\n\t\t\tthis._outputThroughputTracker.observe(generation, event.assistantMessageEvent);\n\t\t} else if (event.type === \"message_end\" && event.message.role === \"assistant\") {\n\t\t\tconst sample = this._outputThroughputTracker.finalize(generation, event.message);\n\t\t\tthis._outputThroughputGeneration = undefined;\n\t\t\tif (sample) {\n\t\t\t\tthis._latestOutputThroughputSample = sample;\n\t\t\t\tthis._outputThroughputHistory.submit(sample);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate _resetOutputThroughput(): void {\n\t\tthis._outputThroughputTracker.reset();\n\t\tthis._outputThroughputGeneration = undefined;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: every Agent run owns persistence state and a one-shot settlement token (#553).\n\tprivate _captureAgentRunSettlement(event: AgentEvent): AgentRunSettlement | undefined {\n\t\tif (event.type === \"agent_start\") {\n\t\t\t// A retry continuation's run adopts the chain its originating prompt awaits; the chain stays\n\t\t\t// bound to the retry phase until this start consumes it, however late Agent emits it.\n\t\t\tconst chain = this._retry?.phase === \"continuing\" ? this._retry.chain : createDeferred();\n\t\t\tthis._unsettledRetryChains.add(chain);\n\t\t\tconst run: AgentRunSettlement = { ...createDeferred(), chain };\n\t\t\tthis._activeAgentRunSettlement = run;\n\t\t\tthis._unsettledAgentRuns.add(run);\n\t\t\tconst reservation = this._pendingAgentRunStart;\n\t\t\tif (reservation) {\n\t\t\t\tthis._pendingAgentRunStart = undefined;\n\t\t\t\treservation.run = run;\n\t\t\t\tresolveDeferred(reservation, run);\n\t\t\t}\n\t\t\treturn run;\n\t\t}\n\n\t\tconst run = this._activeAgentRunSettlement;\n\t\tif (event.type === \"agent_end\" && this._activeAgentRunSettlement === run) {\n\t\t\tthis._activeAgentRunSettlement = undefined;\n\t\t}\n\t\treturn run;\n\t}\n\n\tprivate _settleAgentRun(run: AgentRunSettlement, error?: Error): void {\n\t\tif (settleDeferred(run, error)) this._unsettledAgentRuns.delete(run);\n\t}\n\n\tprivate _settleRetryChain(chain: RetryChainSettlement, error?: Error): void {\n\t\tif (settleDeferred(chain, error)) this._unsettledRetryChains.delete(chain);\n\t}\n\n\tprivate _completeAgentRun(run: AgentRunSettlement, error?: Error): void {\n\t\tthis._settleAgentRun(run, error);\n\t\tthis._settleRetryChain(run.chain, error);\n\t}\n\n\tprivate _reserveAgentRunStart(): AgentRunStartReservation {\n\t\tif (this._pendingAgentRunStart) throw new Error(\"An Agent prompt is already waiting to start.\");\n\t\tconst reservation: AgentRunStartReservation = createDeferred<AgentRunSettlement>();\n\t\tthis._pendingAgentRunStart = reservation;\n\t\treturn reservation;\n\t}\n\n\tprivate _rejectAgentRunStart(reservation: AgentRunStartReservation, error: Error): void {\n\t\tif (this._pendingAgentRunStart === reservation) this._pendingAgentRunStart = undefined;\n\t\trejectDeferred(reservation, error);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted settlement (#341). Synchronous ownership keeps queued\n\t// invocation events attributed after early failures; public settlement awaits execution and the final persistence tail.\n\tprivate async _processAgentEventTracked(\n\t\tevent: AgentEvent,\n\t\trun: AgentRunSettlement | undefined,\n\t\tharnessAck?: HarnessPersistenceAck,\n\t\ttoolCost?: LiveToolCost,\n\t): Promise<void> {\n\t\ttry {\n\t\t\tawait this._processAgentEvent(event, run, harnessAck !== undefined, toolCost);\n\t\t} catch (err) {\n\t\t\tconst error = err instanceof Error ? err : new Error(String(err));\n\t\t\tif (this._manualCompactionDrain) this._manualCompactionDrain.firstError ??= error;\n\t\t\tif (harnessAck) {\n\t\t\t\tharnessAck.firstPersistenceError ??= error;\n\t\t\t} else if (run && !this._manualCompactionDrain) {\n\t\t\t\tif (event.type === \"message_end\" && event.message.role === \"assistant\") {\n\t\t\t\t\trun.assistantPersistenceError = error;\n\t\t\t\t}\n\t\t\t\tthis._releaseRetryForFailedRun(error);\n\t\t\t\tthis._completeAgentRun(run, error);\n\t\t\t}\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tprivate _harnessPersistenceAckForEvent(event: AgentEvent): HarnessPersistenceAck | undefined {\n\t\tlet toolCallId: string | undefined;\n\t\tswitch (event.type) {\n\t\t\tcase \"tool_execution_start\":\n\t\t\tcase \"tool_execution_update\":\n\t\t\tcase \"tool_execution_end\":\n\t\t\t\ttoolCallId = event.toolCallId;\n\t\t\t\tbreak;\n\t\t\tcase \"message_start\":\n\t\t\tcase \"message_end\": {\n\t\t\t\tconst message = event.message;\n\t\t\t\tif (message.role === \"toolResult\") {\n\t\t\t\t\ttoolCallId = message.toolCallId;\n\t\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\t\tfor (const block of message.content) {\n\t\t\t\t\t\tif (block.type === \"toolCall\" && this._harnessPersistenceAcks.has(block.id)) {\n\t\t\t\t\t\t\ttoolCallId = block.id;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn toolCallId === undefined ? undefined : this._harnessPersistenceAcks.get(toolCallId);\n\t}\n\n\tprivate async _processAgentEvent(\n\t\tevent: AgentEvent,\n\t\trun: AgentRunSettlement | undefined,\n\t\tfromHarnessInvocation = false,\n\t\ttoolCost?: LiveToolCost,\n\t): Promise<void> {\n\t\tif (this._disposed) return;\n\t\t// SCRAMJET-DIVERGENCE: queued lifecycle events must respect manual compaction ownership (#598).\n\t\tif (this._manualCompactionDrain && event.type !== \"message_end\" && event.type !== \"agent_end\") return;\n\t\tif (this._manualCompactionDrain && event.type === \"agent_end\") {\n\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\tif (run) this._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\treturn;\n\t\t}\n\t\t// When a user message starts, check if it's from either queue and remove it BEFORE emitting\n\t\t// This ensures the UI sees the updated queue state\n\t\tif (event.type === \"message_start\" && event.message.role === \"user\") {\n\t\t\tthis._overflowRecoveryAttempted = false;\n\t\t\tconst messageText = this._getUserMessageText(event.message);\n\t\t\tif (messageText) {\n\t\t\t\t// Check steering queue first\n\t\t\t\tconst steeringIndex = this._steeringMessages.indexOf(messageText);\n\t\t\t\tif (steeringIndex !== -1) {\n\t\t\t\t\tthis._steeringMessages.splice(steeringIndex, 1);\n\t\t\t\t\tthis._emitQueueUpdate();\n\t\t\t\t} else {\n\t\t\t\t\t// Check follow-up queue\n\t\t\t\t\tconst followUpIndex = this._followUpMessages.indexOf(messageText);\n\t\t\t\t\tif (followUpIndex !== -1) {\n\t\t\t\t\t\tthis._followUpMessages.splice(followUpIndex, 1);\n\t\t\t\t\t\tthis._emitQueueUpdate();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Emit to extensions first\n\t\tawait this._emitExtensionEvent(event);\n\t\tif (event.type === \"message_end\" && event.message.role === \"toolResult\" && toolCost?.cost !== undefined) {\n\t\t\tevent.message.cost = toolCost.cost;\n\t\t}\n\n\t\t// An awaited hook may have acquired manual ownership while this event was in flight.\n\t\tif (this._manualCompactionDrain && event.type !== \"message_end\") {\n\t\t\tif (event.type === \"agent_end\" && run) {\n\t\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\t// Notify all listeners\n\t\tthis._emit(event);\n\t\tif (this._disposed) return;\n\n\t\t// Handle session persistence\n\t\tif (event.type === \"message_end\") {\n\t\t\t// Check if this is a custom message from extensions\n\t\t\tif (event.message.role === \"custom\") {\n\t\t\t\t// Persist as CustomMessageEntry\n\t\t\t\tthis.sessionManager.appendCustomMessageEntry(\n\t\t\t\t\tevent.message.customType,\n\t\t\t\t\tevent.message.content,\n\t\t\t\t\tevent.message.display,\n\t\t\t\t\tevent.message.details,\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\tevent.message.role === \"user\" ||\n\t\t\t\tevent.message.role === \"assistant\" ||\n\t\t\t\tevent.message.role === \"toolResult\"\n\t\t\t) {\n\t\t\t\t// SCRAMJET-DIVERGENCE: retry settlement classifies the exact JSON-safe assistant snapshot\n\t\t\t\t// that crossed the persistence boundary, never a later-mutated live Agent message (#553).\n\t\t\t\tconst persistedMessage =\n\t\t\t\t\tevent.message.role === \"assistant\"\n\t\t\t\t\t\t? (JSON.parse(JSON.stringify(event.message)) as AssistantMessage)\n\t\t\t\t\t\t: event.message;\n\t\t\t\tconst messageEntryId = this.sessionManager.appendMessage(persistedMessage);\n\t\t\t\tif (toolCost) this._pendingToolCosts.delete(toolCost);\n\t\t\t\tif (persistedMessage.role === \"assistant\" && !fromHarnessInvocation && run) {\n\t\t\t\t\trun.persistedAssistantSnapshot = persistedMessage;\n\t\t\t\t}\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\tconst sessionEntries = this._inputSessionEntries.get(event.message);\n\t\t\t\t\tthis._inputSessionEntries.delete(event.message);\n\t\t\t\t\t// This try/catch observes only failures thrown synchronously by\n\t\t\t\t\t// appendCustomEntry. On the buffered first-turn path (before any\n\t\t\t\t\t// assistant message) _persist stores the entry in memory with no I/O,\n\t\t\t\t\t// so only metadata *serialization* failures surface here; the deferred\n\t\t\t\t\t// disk write happens later in the unguarded appendMessage(assistant)\n\t\t\t\t\t// flush, whose throw is swallowed by the _agentEventQueue.catch on the\n\t\t\t\t\t// event-handler chain and degrades exact restoration silently.\n\t\t\t\t\tfor (const entry of sessionEntries ?? []) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tthis.sessionManager.appendCustomEntry(entry.customType, entry.data, messageEntryId);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\t\t\t\textensionPath: `session-entry:${entry.customType}`,\n\t\t\t\t\t\t\t\tevent: \"session_entry_persistence\",\n\t\t\t\t\t\t\t\terror: `Failed to persist attached input metadata after its user message; exact input restoration may be unavailable: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\tstack: err instanceof Error ? err.stack : undefined,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Other message types (bashExecution, compactionSummary, branchSummary) are persisted elsewhere\n\n\t\t\tif (\n\t\t\t\tevent.message.role === \"assistant\" &&\n\t\t\t\tevent.message.stopReason !== \"error\" &&\n\t\t\t\tevent.message.stopReason !== \"aborted\" &&\n\t\t\t\t!fromHarnessInvocation &&\n\t\t\t\t!this._manualCompactionDrain\n\t\t\t) {\n\t\t\t\tthis._overflowRecoveryAttempted = false;\n\t\t\t\tif (this._retry) this._retry = { ...this._retry, attempt: 0 };\n\t\t\t}\n\t\t}\n\n\t\tif (event.type === \"agent_end\") {\n\t\t\tif (this._disposed || !run) return;\n\t\t\tif (this._manualCompactionDrain) {\n\t\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (run.assistantPersistenceError) {\n\t\t\t\tconst error = run.assistantPersistenceError;\n\t\t\t\trun.assistantPersistenceError = undefined;\n\t\t\t\trun.persistedAssistantSnapshot = undefined;\n\t\t\t\tthis._completeAgentRun(run, error);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst msg = run.persistedAssistantSnapshot;\n\t\t\trun.persistedAssistantSnapshot = undefined;\n\t\t\tif (!msg) {\n\t\t\t\tthis._releaseRetryForFailedRun(new Error(\"Retry continuation ended without an assistant message.\"));\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (msg.stopReason === \"aborted\" && this._retry) {\n\t\t\t\tthis._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (msg.stopReason !== \"error\") {\n\t\t\t\tif (this._retry) {\n\t\t\t\t\tconst attempt = this._runRetryCount;\n\t\t\t\t\tthis._finishRetry(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\t\toutcome: \"succeeded\",\n\t\t\t\t\t\t\tattemptsCompleted: this._bounded(attempt),\n\t\t\t\t\t\t\tcumulativeErrors: this._bounded(attempt),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ type: \"auto_retry_end\", success: true, attempt },\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst classification = this._classifyRetry(msg);\n\t\t\tif (classification.kind === \"context_overflow\") {\n\t\t\t\tthis._finishRetryWithoutSuccess(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\t\treason: \"context_overflow_compaction\",\n\t\t\t\t\t\tevidence: \"context_overflow\",\n\t\t\t\t\t},\n\t\t\t\t\tmsg,\n\t\t\t\t);\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst settings = this.settingsManager.getRetrySettings();\n\t\t\tif (!settings.enabled) {\n\t\t\t\tthis._finishRetryWithoutSuccess(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\t\treason: \"retry_disabled\",\n\t\t\t\t\t\tevidence: classification.evidence,\n\t\t\t\t\t},\n\t\t\t\t\tmsg,\n\t\t\t\t);\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (classification.kind === \"retry\") {\n\t\t\t\tconst didRetry = await this._handleRetryableError(msg, classification.evidence, run);\n\t\t\t\tif (didRetry) return;\n\t\t\t} else {\n\t\t\t\tconst { kind: _kind, ...cause } = classification;\n\t\t\t\tthis._finishRetryWithoutSuccess({ schemaVersion: 1, outcome: \"not_attempted\", ...cause }, msg);\n\t\t\t}\n\n\t\t\tthis._completeAgentRun(run);\n\t\t\tawait this._checkCompaction(msg);\n\t\t}\n\t}\n\n\t/** Extract text content from a message */\n\tprivate _getUserMessageText(message: Message): string {\n\t\tif (message.role !== \"user\") return \"\";\n\t\tconst content = message.content;\n\t\tif (typeof content === \"string\") return content;\n\t\tconst textBlocks = content.filter((c) => c.type === \"text\");\n\t\treturn textBlocks.map((c) => (c as TextContent).text).join(\"\");\n\t}\n\n\t/** Find the last assistant message in agent state (including aborted ones) */\n\tprivate _findLastAssistantMessage(): AssistantMessage | undefined {\n\t\tconst messages = this.agent.state.messages;\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst msg = messages[i];\n\t\t\tif (msg.role === \"assistant\") {\n\t\t\t\treturn msg as AssistantMessage;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate _replaceMessageInPlace(target: AgentMessage, replacement: AgentMessage): void {\n\t\t// Agent-core stores the finalized message object in its state before emitting message_end.\n\t\t// SessionManager persistence happens later in _processAgentEvent() with event.message.\n\t\t// Mutating this object in place keeps agent state, later turn/agent events, listeners,\n\t\t// and the eventual SessionManager.appendMessage(event.message) persistence in sync.\n\t\tif (target === replacement) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst targetRecord = target as unknown as Record<string, unknown>;\n\t\tfor (const key of Object.keys(targetRecord)) {\n\t\t\tdelete targetRecord[key];\n\t\t}\n\t\tObject.assign(targetRecord, replacement);\n\t}\n\n\t/** Emit extension events based on agent events */\n\tprivate async _emitExtensionEvent(event: AgentEvent): Promise<void> {\n\t\tif (event.type === \"agent_start\") {\n\t\t\tthis._turnIndex = 0;\n\t\t\tawait this._extensionRunner.emit({ type: \"agent_start\" });\n\t\t} else if (event.type === \"agent_end\") {\n\t\t\tawait this._extensionRunner.emit({ type: \"agent_end\", messages: event.messages });\n\t\t} else if (event.type === \"turn_start\") {\n\t\t\tconst extensionEvent: TurnStartEvent = {\n\t\t\t\ttype: \"turn_start\",\n\t\t\t\tturnIndex: this._turnIndex,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"turn_end\") {\n\t\t\tconst extensionEvent: TurnEndEvent = {\n\t\t\t\ttype: \"turn_end\",\n\t\t\t\tturnIndex: this._turnIndex,\n\t\t\t\tmessage: event.message,\n\t\t\t\ttoolResults: event.toolResults,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t\tthis._turnIndex++;\n\t\t} else if (event.type === \"message_start\") {\n\t\t\tconst extensionEvent: MessageStartEvent = {\n\t\t\t\ttype: \"message_start\",\n\t\t\t\tmessage: event.message,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"message_update\") {\n\t\t\tconst extensionEvent: MessageUpdateEvent = {\n\t\t\t\ttype: \"message_update\",\n\t\t\t\tmessage: event.message,\n\t\t\t\tassistantMessageEvent: event.assistantMessageEvent,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"message_end\") {\n\t\t\tconst extensionEvent: MessageEndEvent = {\n\t\t\t\ttype: \"message_end\",\n\t\t\t\tmessage: event.message,\n\t\t\t};\n\t\t\tconst replacement = await this._extensionRunner.emitMessageEnd(extensionEvent);\n\t\t\tif (replacement) {\n\t\t\t\tthis._replaceMessageInPlace(event.message, replacement);\n\t\t\t}\n\t\t} else if (event.type === \"tool_execution_start\") {\n\t\t\tconst extensionEvent: ToolExecutionStartEvent = {\n\t\t\t\ttype: \"tool_execution_start\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\targs: event.args,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"tool_execution_update\") {\n\t\t\tconst extensionEvent: ToolExecutionUpdateEvent = {\n\t\t\t\ttype: \"tool_execution_update\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\targs: event.args,\n\t\t\t\tpartialResult: event.partialResult,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"tool_execution_end\") {\n\t\t\tconst extensionEvent: ToolExecutionEndEvent = {\n\t\t\t\ttype: \"tool_execution_end\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\tresult: event.result,\n\t\t\t\tisError: event.isError,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t}\n\t}\n\n\t/**\n\t * Subscribe to agent events.\n\t * Session persistence is handled internally (saves messages on message_end).\n\t * Multiple listeners can be added. Returns unsubscribe function for this listener.\n\t */\n\tsubscribe(listener: AgentSessionEventListener): () => void {\n\t\tthis._eventListeners.push(listener);\n\n\t\t// Return unsubscribe function for this specific listener\n\t\treturn () => {\n\t\t\tconst index = this._eventListeners.indexOf(listener);\n\t\t\tif (index !== -1) {\n\t\t\t\tthis._eventListeners.splice(index, 1);\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Temporarily disconnect from agent events.\n\t * User listeners are preserved and will receive events again after resubscribe().\n\t * Used internally during operations that need to pause event processing.\n\t */\n\tprivate _disconnectFromAgent(): void {\n\t\tif (this._unsubscribeAgent) {\n\t\t\tthis._unsubscribeAgent();\n\t\t\tthis._unsubscribeAgent = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Reconnect to agent events after _disconnectFromAgent().\n\t * Preserves all existing listeners.\n\t */\n\tprivate _reconnectToAgent(): void {\n\t\tif (this._disposed || this._unsubscribeAgent) return; // Already connected or disposed\n\t\tthis._unsubscribeAgent = this.agent.subscribe(this._handleAgentEvent);\n\t}\n\n\t/**\n\t * Remove all listeners and disconnect from agent.\n\t * Call this when completely done with the session.\n\t */\n\tdispose(): void {\n\t\t// SCRAMJET-DIVERGENCE: idempotent disposal revokes harness invocations and closes retry settlement (#341, #553).\n\t\t// A second call must not re-reject already-settled acknowledgements or re-run teardown.\n\t\tif (this._disposed) return;\n\t\tthis._disposed = true;\n\t\tthis._toolCostExecutions.clear();\n\t\tthis._pendingToolCosts.clear();\n\t\tthis._resetOutputThroughput();\n\t\tconst retryDisposeError = new Error(\"AgentSession disposed before retry settlement completed.\");\n\t\tconst unsettledRuns = [...this._unsettledAgentRuns];\n\t\tconst retry = this._retry;\n\t\tconst unclassifiedError = unsettledRuns.find(\n\t\t\t(run) => run.persistedAssistantSnapshot?.stopReason === \"error\",\n\t\t)?.persistedAssistantSnapshot;\n\t\tif (retry) this.agent.abort();\n\t\ttry {\n\t\t\tif (retry) {\n\t\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"cancelled\",\n\t\t\t\t\treason: \"session_disposed\",\n\t\t\t\t\tattempt: this._bounded(retry.attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t});\n\t\t\t} else if (unclassifiedError) {\n\t\t\t\tconst classification = this._classifyRetry(unclassifiedError);\n\t\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\treason: \"session_disposed\",\n\t\t\t\t\tevidence: classification.kind === \"context_overflow\" ? \"context_overflow\" : classification.evidence,\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis._reportRetryFailure(error);\n\t\t}\n\t\tif (retry?.phase === \"backoff\") retry.controller.abort();\n\t\tthis._retry = undefined;\n\t\tthis._activeAgentRunSettlement = undefined;\n\t\tconst pendingAgentRunStart = this._pendingAgentRunStart;\n\t\tif (pendingAgentRunStart) this._rejectAgentRunStart(pendingAgentRunStart, retryDisposeError);\n\t\tconst pendingRetryContinuation = retry?.phase === \"continuing\" ? retry.chain : undefined;\n\t\tthis._extensionRunner.invalidate(\n\t\t\t\"This extension ctx is stale after session replacement or reload. Do not use a captured pi or command ctx after ctx.newSession(), ctx.fork(), ctx.switchSession(), or ctx.reload(). For newSession, fork, and switchSession, move post-replacement work into withSession and use the ctx passed to withSession. For reload, do not use the old ctx after await ctx.reload().\",\n\t\t);\n\t\t// Reject pending persistence acknowledgements before revoking Agent-side invocations, so a caller\n\t\t// awaiting invokeHarnessTool sees rejection rather than a promise that never settles. Already-\n\t\t// running tool work may finish afterward; teardown revokes the old-session guarantee but cannot\n\t\t// roll back arbitrary side effects.\n\t\tconst disposeError = new Error(\"AgentSession disposed before the harness tool invocation settled.\");\n\t\tfor (const ack of this._harnessPersistenceAcks.values()) {\n\t\t\tthis._settleHarnessPersistenceAck(ack, disposeError);\n\t\t}\n\t\tthis._harnessPersistenceAcks.clear();\n\t\tthis.agent.rejectUnsettledHarnessTools(disposeError);\n\t\tconst retryChains = new Set(this._unsettledRetryChains);\n\t\tif (pendingRetryContinuation) retryChains.add(pendingRetryContinuation);\n\t\tfor (const run of unsettledRuns) this._settleAgentRun(run, retryDisposeError);\n\t\tfor (const chain of retryChains) this._settleRetryChain(chain, retryDisposeError);\n\t\tthis._disconnectFromAgent();\n\t\tthis._eventListeners = [];\n\t\tcleanupSessionResources(this.sessionId);\n\t}\n\n\t// =========================================================================\n\t// Read-only State Access\n\t// =========================================================================\n\n\t/** Full agent state */\n\tget state(): AgentState {\n\t\treturn this.agent.state;\n\t}\n\n\t/** Current model (may be undefined if not yet selected) */\n\tget model(): Model<any> | undefined {\n\t\treturn this.agent.state.model;\n\t}\n\n\t/** Current thinking level */\n\tget thinkingLevel(): ThinkingLevel {\n\t\treturn this.agent.state.thinkingLevel;\n\t}\n\n\t/** Whether agent is currently streaming a response */\n\tget isStreaming(): boolean {\n\t\treturn this.agent.state.isStreaming;\n\t}\n\n\t/** Current effective system prompt (includes any per-turn extension modifications), flattened to a string */\n\tget systemPrompt(): string {\n\t\treturn flattenSystemPrompt(this.agent.state.systemPrompt);\n\t}\n\n\tget liveOutputRate(): number | undefined {\n\t\tconst model = this.state.model;\n\t\tconst identity = this._outputThroughputTracker.activeIdentity;\n\t\tif (!model || identity?.provider !== model.provider || identity.model !== model.id) return undefined;\n\t\treturn this._outputThroughputTracker.liveRate();\n\t}\n\n\tget outputThroughputActive(): boolean {\n\t\treturn this._outputThroughputTracker.outputActive;\n\t}\n\n\tget outputThroughputGeneration(): number {\n\t\treturn this._outputThroughputTracker.generation;\n\t}\n\n\tget medianOutputRate(): number | undefined {\n\t\tconst model = this.state.model;\n\t\tif (!model) return undefined;\n\t\tconst history = this._outputThroughputHistory.snapshot();\n\t\tconst local = this._latestOutputThroughputSample;\n\t\tif (\n\t\t\tlocal?.provider === model.provider &&\n\t\t\tlocal.model === model.id &&\n\t\t\t!history\n\t\t\t\t.samples(model.provider, model.id)\n\t\t\t\t.some(\n\t\t\t\t\t(sample) =>\n\t\t\t\t\t\tsample.provider === local.provider &&\n\t\t\t\t\t\tsample.model === local.model &&\n\t\t\t\t\t\tsample.responseModel === local.responseModel &&\n\t\t\t\t\t\tsample.outputTokens === local.outputTokens &&\n\t\t\t\t\t\tsample.durationMs === local.durationMs &&\n\t\t\t\t\t\tsample.observedAt === local.observedAt,\n\t\t\t\t)\n\t\t) {\n\t\t\thistory.add(local);\n\t\t}\n\t\treturn history.median(model.provider, model.id);\n\t}\n\n\tget outputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.snapshot();\n\t}\n\n\trefreshOutputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.refresh();\n\t}\n\n\tflushOutputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.flush();\n\t}\n\n\t/** Current retry attempt (0 if not retrying) */\n\tget retryAttempt(): number {\n\t\treturn this._retry?.attempt ?? 0;\n\t}\n\n\t/**\n\t * Get the names of currently active tools.\n\t * Returns the names of tools currently set on the agent.\n\t */\n\tgetActiveToolNames(): string[] {\n\t\treturn this.agent.state.tools.map((t) => t.name);\n\t}\n\n\t/**\n\t * Get all configured tools with name, description, parameter schema, and source metadata.\n\t */\n\tgetAllTools(): ToolInfo[] {\n\t\treturn Array.from(this._toolDefinitions.values()).map(({ definition, sourceInfo }) => ({\n\t\t\tname: definition.name,\n\t\t\tdescription: definition.description,\n\t\t\tparameters: definition.parameters,\n\t\t\tsourceInfo,\n\t\t}));\n\t}\n\n\tgetToolDefinition(name: string): ToolDefinition | undefined {\n\t\treturn this._toolDefinitions.get(name)?.definition;\n\t}\n\n\t/**\n\t * Set active tools by name.\n\t * Only tools in the registry can be enabled. Unknown tool names are ignored.\n\t * Also rebuilds the system prompt to reflect the new tool set.\n\t * Changes take effect on the next agent turn.\n\t */\n\tsetActiveToolsByName(toolNames: string[]): void {\n\t\tconst tools: AgentTool[] = [];\n\t\tconst validToolNames: string[] = [];\n\t\tfor (const name of toolNames) {\n\t\t\t// SCRAMJET-DIVERGENCE: harness-only tools are never placed in the provider-visible tool\n\t\t\t// set, even when explicitly requested (#244). This is the single choke point for\n\t\t\t// `agent.state.tools`, so filtering here structurally guarantees a harness-only tool can\n\t\t\t// never masquerade as a model-callable one — while it stays resolvable in `_toolRegistry`\n\t\t\t// for `invokeHarnessTool`.\n\t\t\tif (this._toolDefinitions.get(name)?.definition.activation === \"harness-only\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst tool = this._toolRegistry.get(name);\n\t\t\tif (tool) {\n\t\t\t\ttools.push(tool);\n\t\t\t\tvalidToolNames.push(name);\n\t\t\t}\n\t\t}\n\t\tthis.agent.state.tools = tools;\n\n\t\t// Rebuild base system prompt with new tool set\n\t\tthis._rebuildSystemPrompt(validToolNames);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool invocation (#244), persisted-settlement boundary (#341).\n\t/**\n\t * Execute a registered tool as a harness-originated call (not requested by the model).\n\t *\n\t * Resolves the wrapped tool from the full registry — including `\"harness-only\"` tools that are\n\t * never provider-visible — so its `execute` still receives the extension context and its\n\t * `tool_call`/`tool_result` hooks still fire. Delegates to {@link Agent.runHarnessTool}, which\n\t * runs the call through the real prepare/execute/finalize pipeline: identical live\n\t * `tool_execution_*`/message events, persisted session entries, and extension hooks, but no\n\t * run/turn framing. The idle-vs-mid-run branch lives in `runHarnessTool` (keyed on the agent's\n\t * active run, which tracks `isStreaming`): idle calls execute immediately, mid-run calls queue\n\t * and drain before the next intra-run LLM call.\n\t *\n\t * **Persisted settlement (#341).** This is a strictly stronger boundary than\n\t * {@link Agent.runHarnessTool}: the returned promise resolves only after (1) the Agent-core\n\t * pipeline fulfills *and* (2) `_processAgentEvent` has persisted the matching tool-result\n\t * `message_end` via `SessionManager.appendMessage`. Because the event queue preserves order,\n\t * that also proves the preceding synthetic assistant tool-call message was persisted first. It\n\t * rejects on Agent-core failure, on failure to process/persist a matching event, on disposal\n\t * before settlement, on a post-disposal call, on an unknown tool, or on a duplicate explicit\n\t * tool-call id that is already pending. **Rejection means the promised pipeline did not complete —\n\t * it does not prove artifact absence** (Agent state and persistence may have partially or fully\n\t * completed before a late failure), so callers must not blindly retry. A tool's own `execute()`\n\t * error is *not* a rejection: it becomes a normal `isError: true` result and the promise resolves\n\t * after that error-result pipeline is persisted.\n\t *\n\t * **Reentrancy.** As with `runHarnessTool`, do not `await` this from work whose return is required\n\t * for the Agent to reach the drain point or for the event queue to persist the matching result.\n\t */\n\tasync invokeHarnessTool(name: string, args: unknown, options?: InvokeHarnessToolOptions): Promise<void> {\n\t\tif (this._disposed) {\n\t\t\tthrow new Error(`Cannot invoke harness tool \"${name}\": the session has been disposed.`);\n\t\t}\n\t\t// SCRAMJET-DIVERGENCE: reject new session work before allocating invocation ownership (#598).\n\t\tif (this._compactionAbortController) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot invoke harness tool \"${name}\": manual compaction is in progress. Retry after it settles.`,\n\t\t\t);\n\t\t}\n\t\tconst tool = this._toolRegistry.get(name);\n\t\tif (!tool) {\n\t\t\tthrow new Error(`Cannot invoke harness tool \"${name}\": no tool with that name is registered.`);\n\t\t}\n\t\tconst toolCallId = this._allocateHarnessToolCallId(options?.toolCallId);\n\t\t// Register before Agent execution can emit events. The coordinator retains attribution until Agent\n\t\t// execution and the invocation's final queued persistence task both settle.\n\t\tconst ack = this._registerHarnessPersistenceAck(toolCallId);\n\t\tvoid this._completeHarnessToolInvocation(this.agent.runHarnessTool(tool, args, { toolCallId }), ack);\n\t\ttry {\n\t\t\tawait ack.promise;\n\t\t} finally {\n\t\t\t// Identity-checked cleanup: only drop the entry if it is still this invocation's ack (disposal\n\t\t\t// may have cleared the map, and a later same-id invocation must not have its entry removed).\n\t\t\tif (this._harnessPersistenceAcks.get(toolCallId) === ack) {\n\t\t\t\tthis._harnessPersistenceAcks.delete(toolCallId);\n\t\t\t}\n\t\t}\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement (#341). Allocate the tool-call id used as\n\t// the acknowledgement key: reject a duplicate explicit id that is still pending, retry a generated\n\t// id on the (astronomically unlikely) collision with a pending one.\n\tprivate _allocateHarnessToolCallId(explicit?: string): string {\n\t\tif (explicit !== undefined) {\n\t\t\t// Validate at the public trust boundary (invokeHarnessTool is on ExtensionAPI): an explicit\n\t\t\t// id flows straight into the synthetic assistant message and on to the provider, so it must\n\t\t\t// meet the provider-safe shape generateHarnessToolCallId guarantees (Anthropic's\n\t\t\t// ^[a-zA-Z0-9_-]+$, bounded length).\n\t\t\tif (!/^[a-zA-Z0-9_-]{1,64}$/.test(explicit)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Cannot invoke harness tool with tool-call id \"${explicit}\": ids must be 1-64 characters of [a-zA-Z0-9_-].`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (this._harnessPersistenceAcks.has(explicit)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Cannot invoke harness tool with tool-call id \"${explicit}\": an invocation with that id is already pending.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn explicit;\n\t\t}\n\t\tlet id = generateHarnessToolCallId();\n\t\twhile (this._harnessPersistenceAcks.has(id)) {\n\t\t\tid = generateHarnessToolCallId();\n\t\t}\n\t\treturn id;\n\t}\n\n\tprivate async _completeHarnessToolInvocation(execution: Promise<void>, ack: HarnessPersistenceAck): Promise<void> {\n\t\tlet executionError: Error | undefined;\n\t\ttry {\n\t\t\tawait execution;\n\t\t} catch (error) {\n\t\t\texecutionError = error instanceof Error ? error : new Error(String(error));\n\t\t}\n\t\ttry {\n\t\t\tawait ack.persistenceTail;\n\t\t} catch {}\n\t\tthis._settleHarnessPersistenceAck(ack, ack.firstPersistenceError ?? executionError);\n\t}\n\n\tprivate _settleHarnessPersistenceAck(ack: HarnessPersistenceAck, error?: Error): void {\n\t\tsettleDeferred(ack, error);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement (#341). Register a deferred acknowledgement\n\t// keyed by tool-call id before any invocation event can be emitted.\n\tprivate _registerHarnessPersistenceAck(toolCallId: string): HarnessPersistenceAck {\n\t\tconst ack: HarnessPersistenceAck = { ...createDeferred(), persistenceTail: Promise.resolve() };\n\t\tthis._harnessPersistenceAcks.set(toolCallId, ack);\n\t\treturn ack;\n\t}\n\n\t/** Whether compaction or branch summarization is currently running */\n\tget isCompacting(): boolean {\n\t\treturn (\n\t\t\tthis._autoCompactionAbortController !== undefined ||\n\t\t\tthis._compactionAbortController !== undefined ||\n\t\t\tthis._branchSummaryAbortController !== undefined\n\t\t);\n\t}\n\n\t/** All messages including custom types like BashExecutionMessage */\n\tget messages(): AgentMessage[] {\n\t\treturn this.agent.state.messages;\n\t}\n\n\t/** Current steering mode */\n\tget steeringMode(): \"all\" | \"one-at-a-time\" {\n\t\treturn this.agent.steeringMode;\n\t}\n\n\t/** Current follow-up mode */\n\tget followUpMode(): \"all\" | \"one-at-a-time\" {\n\t\treturn this.agent.followUpMode;\n\t}\n\n\t/** Current session file path, or undefined if sessions are disabled */\n\tget sessionFile(): string | undefined {\n\t\treturn this.sessionManager.getSessionFile();\n\t}\n\n\t/** Current session ID */\n\tget sessionId(): string {\n\t\treturn this.sessionManager.getSessionId();\n\t}\n\n\t/** Current session display name, if set */\n\tget sessionName(): string | undefined {\n\t\treturn this.sessionManager.getSessionName();\n\t}\n\n\t/** Scoped models for cycling (from --models flag) */\n\tget scopedModels(): ReadonlyArray<{ model: Model<any>; thinkingLevel?: ThinkingLevel }> {\n\t\treturn this._scopedModels;\n\t}\n\n\t/** Update scoped models for cycling */\n\tsetScopedModels(scopedModels: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>): void {\n\t\tthis._scopedModels = scopedModels;\n\t}\n\n\t/** File-based prompt templates */\n\tget promptTemplates(): ReadonlyArray<PromptTemplate> {\n\t\treturn this._resourceLoader.getPrompts().prompts;\n\t}\n\n\tprivate _normalizePromptSnippet(text: string | undefined): string | undefined {\n\t\tif (!text) return undefined;\n\t\tconst oneLine = text\n\t\t\t.replace(/[\\r\\n]+/g, \" \")\n\t\t\t.replace(/\\s+/g, \" \")\n\t\t\t.trim();\n\t\treturn oneLine.length > 0 ? oneLine : undefined;\n\t}\n\n\tprivate _normalizePromptGuidelines(guidelines: string[] | undefined): string[] {\n\t\tif (!guidelines || guidelines.length === 0) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst unique = new Set<string>();\n\t\tfor (const guideline of guidelines) {\n\t\t\tconst normalized = guideline.trim();\n\t\t\tif (normalized.length > 0) {\n\t\t\t\tunique.add(normalized);\n\t\t\t}\n\t\t}\n\t\treturn Array.from(unique);\n\t}\n\n\tprivate _applyRunPromptComposition(): void {\n\t\tswitch (this._runPromptComposition.kind) {\n\t\t\tcase \"generated-base\":\n\t\t\t\tthis.agent.state.systemPrompt = this._baseSystemPromptSections.slice();\n\t\t\t\tbreak;\n\t\t\tcase \"contributed-sections\":\n\t\t\t\tthis.agent.state.systemPrompt = spliceContributedSections(\n\t\t\t\t\tthis._baseSystemPromptSections,\n\t\t\t\t\tthis._runPromptComposition.sections,\n\t\t\t\t);\n\t\t\t\tbreak;\n\t\t\tcase \"authoritative-string\":\n\t\t\t\tthis.agent.state.systemPrompt = this._runPromptComposition.systemPrompt;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t/** Rebuild `_baseSystemPromptSections` from the current resources and tool set and apply it to agent state. */\n\tprivate _rebuildSystemPrompt(toolNames: string[]): void {\n\t\tconst validToolNames = toolNames.filter((name) => this._toolRegistry.has(name));\n\t\tconst toolSnippets: Record<string, string> = {};\n\t\tconst promptGuidelines: string[] = [];\n\t\tfor (const name of validToolNames) {\n\t\t\tconst snippet = this._toolPromptSnippets.get(name);\n\t\t\tif (snippet) {\n\t\t\t\ttoolSnippets[name] = snippet;\n\t\t\t}\n\n\t\t\tconst toolGuidelines = this._toolPromptGuidelines.get(name);\n\t\t\tif (toolGuidelines) {\n\t\t\t\tpromptGuidelines.push(...toolGuidelines);\n\t\t\t}\n\t\t}\n\n\t\tconst loaderSystemPrompt = this._resourceLoader.getSystemPrompt();\n\t\tconst loaderAppendSystemPrompt = this._resourceLoader.getAppendSystemPrompt();\n\t\tconst appendSystemPrompt =\n\t\t\tloaderAppendSystemPrompt.length > 0 ? loaderAppendSystemPrompt.join(\"\\n\\n\") : undefined;\n\t\tconst loadedSkills = this._resourceLoader.getSkills().skills;\n\t\tconst loadedContextFiles = this._resourceLoader.getAgentsFiles().agentsFiles;\n\n\t\t// SCRAMJET-DIVERGENCE: pass session file path for volatile environment fact (issue 348)\n\t\tthis._baseSystemPromptOptions = {\n\t\t\tcwd: this._cwd,\n\t\t\tskills: loadedSkills,\n\t\t\tcontextFiles: loadedContextFiles,\n\t\t\tcustomPrompt: loaderSystemPrompt,\n\t\t\tappendSystemPrompt,\n\t\t\tselectedTools: validToolNames,\n\t\t\ttoolSnippets,\n\t\t\tpromptGuidelines,\n\t\t\tsessionFile: this.sessionManager.getSessionFile(),\n\t\t};\n\t\tthis._baseSystemPromptSections = buildSystemPromptSections(this._baseSystemPromptOptions);\n\t\tthis._applyRunPromptComposition();\n\t}\n\n\t// =========================================================================\n\t// Prompting\n\t// =========================================================================\n\n\t/**\n\t * Send a prompt to the agent.\n\t * - Handles extension commands (registered via pi.registerCommand) immediately, even during streaming\n\t * - Expands file-based prompt templates by default\n\t * - During streaming, queues via steer() or followUp() based on streamingBehavior option\n\t * - Validates model and API key before sending (when not streaming)\n\t * @throws Error if streaming and no streamingBehavior specified\n\t * @throws Error if no model selected or no API key available (when not streaming)\n\t */\n\tasync prompt(text: string, options?: PromptOptions): Promise<void> {\n\t\tconst expandPromptTemplates = options?.expandPromptTemplates ?? true;\n\t\tconst preflightResult = options?.preflightResult;\n\t\tlet messages: AgentMessage[] | undefined;\n\t\tlet inputSessionEntries: InputSessionEntry[] | undefined;\n\t\tlet acceptInput: (() => void) | undefined;\n\n\t\ttry {\n\t\t\t// Handle extension commands first (execute immediately, even during streaming)\n\t\t\t// Extension commands manage their own LLM interaction via pi.sendMessage()\n\t\t\tif (expandPromptTemplates && text.startsWith(\"/\")) {\n\t\t\t\tconst handled = await this._tryExecuteExtensionCommand(text);\n\t\t\t\tif (handled) {\n\t\t\t\t\t// Extension command executed, no prompt to send\n\t\t\t\t\tpreflightResult?.(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis._assertRetryNotOwningIdleAgent();\n\n\t\t\t// Emit input event for extension interception (before skill/template expansion)\n\t\t\tlet currentText = text;\n\t\t\tlet currentImages = options?.images;\n\t\t\tif (this._extensionRunner.hasHandlers(\"input\")) {\n\t\t\t\tconst inputResult = await this._extensionRunner.emitInput(\n\t\t\t\t\tcurrentText,\n\t\t\t\t\tcurrentImages,\n\t\t\t\t\toptions?.source ?? \"interactive\",\n\t\t\t\t);\n\t\t\t\tif (inputResult.action === \"handled\") {\n\t\t\t\t\tpreflightResult?.(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (inputResult.action === \"transform\") {\n\t\t\t\t\tcurrentText = inputResult.text;\n\t\t\t\t\tcurrentImages = inputResult.images ?? currentImages;\n\t\t\t\t\tinputSessionEntries = inputResult.sessionEntries;\n\t\t\t\t\tacceptInput = inputResult.onAccepted;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Expand skill commands (/skill:name args) and prompt templates (/template args)\n\t\t\tlet expandedText = currentText;\n\t\t\tif (expandPromptTemplates) {\n\t\t\t\texpandedText = this._expandSkillCommand(expandedText);\n\t\t\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\t\t\t}\n\n\t\t\t// If streaming, queue via steer() or followUp() based on option\n\t\t\tif (this.isStreaming) {\n\t\t\t\tif (!options?.streamingBehavior) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\"Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') — \" +\n\t\t\t\t\t\t\t\"deliverAs when calling dispatchUserInput() or sendUserMessage() — to queue the message.\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tif (options.streamingBehavior === \"followUp\") {\n\t\t\t\t\tawait this._queueFollowUp(expandedText, currentImages, inputSessionEntries);\n\t\t\t\t} else {\n\t\t\t\t\tawait this._queueSteer(expandedText, currentImages, inputSessionEntries);\n\t\t\t\t}\n\t\t\t\tacceptInput?.();\n\t\t\t\tpreflightResult?.(true);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Flush any pending bash messages before the new prompt\n\t\t\tthis._flushPendingBashMessages();\n\n\t\t\t// Validate model\n\t\t\tif (!this.model) {\n\t\t\t\tthrow new Error(formatNoModelSelectedMessage());\n\t\t\t}\n\n\t\t\tif (!this._modelRegistry.hasConfiguredAuth(this.model)) {\n\t\t\t\tconst isOAuth = this._modelRegistry.isUsingOAuth(this.model);\n\t\t\t\tif (isOAuth) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Authentication failed for \"${this.model.provider}\". ` +\n\t\t\t\t\t\t\t`Credentials may have expired or network is unavailable. ` +\n\t\t\t\t\t\t\t`Run '/login ${this.model.provider}' to re-authenticate.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthrow new Error(formatNoApiKeyFoundMessage(this.model.provider));\n\t\t\t}\n\n\t\t\t// Check if we need to compact before sending (catches aborted responses)\n\t\t\tconst lastAssistant = this._findLastAssistantMessage();\n\t\t\tif (lastAssistant) {\n\t\t\t\tawait this._checkCompaction(lastAssistant, false);\n\t\t\t}\n\n\t\t\t// Build messages array (custom message if any, then user message)\n\t\t\tmessages = [];\n\n\t\t\t// Add user message\n\t\t\tconst userContent: (TextContent | ImageContent)[] = [{ type: \"text\", text: expandedText }];\n\t\t\tif (currentImages) {\n\t\t\t\tuserContent.push(...currentImages);\n\t\t\t}\n\t\t\tconst userMessage: AgentMessage = {\n\t\t\t\trole: \"user\",\n\t\t\t\tcontent: userContent,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t};\n\t\t\tmessages.push(userMessage);\n\t\t\tif (inputSessionEntries?.length) {\n\t\t\t\tthis._inputSessionEntries.set(userMessage, inputSessionEntries);\n\t\t\t}\n\n\t\t\t// Inject any pending \"nextTurn\" messages as context alongside the user message\n\t\t\tfor (const msg of this._pendingNextTurnMessages) {\n\t\t\t\tmessages.push(msg);\n\t\t\t}\n\t\t\tthis._pendingNextTurnMessages = [];\n\n\t\t\tthis._runPromptComposition = { kind: \"generated-base\" };\n\n\t\t\t// Emit before_agent_start extension event\n\t\t\tconst result = await this._extensionRunner.emitBeforeAgentStart(\n\t\t\t\texpandedText,\n\t\t\t\tcurrentImages,\n\t\t\t\tthis._baseSystemPromptSections,\n\t\t\t\tthis._baseSystemPromptOptions,\n\t\t\t);\n\t\t\t// Add all custom messages from extensions\n\t\t\tif (result?.messages) {\n\t\t\t\tfor (const msg of result.messages) {\n\t\t\t\t\tmessages.push({\n\t\t\t\t\t\trole: \"custom\",\n\t\t\t\t\t\tcustomType: msg.customType,\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t\tdisplay: msg.display,\n\t\t\t\t\t\tdetails: msg.details,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Apply extension-modified system prompt, or reset to base. A string\n\t\t\t// replacement is authoritative (even an empty string): it wins for the\n\t\t\t// whole prompt and any contributed sections are dropped for this turn.\n\t\t\tif (result?.systemPrompt !== undefined) {\n\t\t\t\tthis._runPromptComposition = {\n\t\t\t\t\tkind: \"authoritative-string\",\n\t\t\t\t\tsystemPrompt: result.systemPrompt,\n\t\t\t\t};\n\t\t\t} else if (result?.systemPromptSections) {\n\t\t\t\tthis._runPromptComposition = {\n\t\t\t\t\tkind: \"contributed-sections\",\n\t\t\t\t\tsections: result.systemPromptSections.map((section) => ({ ...section })),\n\t\t\t\t};\n\t\t\t}\n\t\t\tthis._applyRunPromptComposition();\n\t\t} catch (error) {\n\t\t\tpreflightResult?.(false);\n\t\t\tthrow error;\n\t\t}\n\n\t\tif (!messages) {\n\t\t\treturn;\n\t\t}\n\n\t\tacceptInput?.();\n\t\tpreflightResult?.(true);\n\t\tawait this._runAgentPrompt(messages);\n\t}\n\n\t/**\n\t * Try to execute an extension command. Returns true if command was found and executed.\n\t */\n\tprivate async _tryExecuteExtensionCommand(text: string): Promise<boolean> {\n\t\t// Parse command name and args\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);\n\t\tconst args = spaceIndex === -1 ? \"\" : text.slice(spaceIndex + 1);\n\n\t\tconst command = this._extensionRunner.getCommand(commandName);\n\t\tif (!command) return false;\n\n\t\t// Get command context from extension runner (includes session control methods)\n\t\tconst ctx = this._extensionRunner.createCommandContext();\n\n\t\ttry {\n\t\t\tawait command.handler(args, ctx);\n\t\t\treturn true;\n\t\t} catch (err) {\n\t\t\t// Emit error via extension runner\n\t\t\tthis._extensionRunner.emitError({\n\t\t\t\textensionPath: `command:${commandName}`,\n\t\t\t\tevent: \"command\",\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t\treturn true;\n\t\t}\n\t}\n\n\t/**\n\t * Expand skill commands (/skill:name args) to their full content.\n\t * Returns the expanded text, or the original text if not a skill command or skill not found.\n\t * Emits errors via extension runner if file read fails.\n\t */\n\tprivate _expandSkillCommand(text: string): string {\n\t\tif (!text.startsWith(\"/skill:\")) return text;\n\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst skillName = spaceIndex === -1 ? text.slice(7) : text.slice(7, spaceIndex);\n\t\tconst args = spaceIndex === -1 ? \"\" : text.slice(spaceIndex + 1).trim();\n\n\t\tconst skill = this.resourceLoader.getSkills().skills.find((s) => s.name === skillName);\n\t\tif (!skill) return text; // Unknown skill, pass through\n\n\t\ttry {\n\t\t\tconst content = readFileSync(skill.filePath, \"utf-8\");\n\t\t\tconst body = stripFrontmatter(content).trim();\n\t\t\tconst skillBlock = `<skill name=\"${skill.name}\" location=\"${skill.filePath}\">\\nReferences are relative to ${skill.baseDir}.\\n\\n${body}\\n</skill>`;\n\t\t\treturn args ? `${skillBlock}\\n\\n${args}` : skillBlock;\n\t\t} catch (err) {\n\t\t\t// Emit error like extension commands do\n\t\t\tthis._extensionRunner.emitError({\n\t\t\t\textensionPath: skill.filePath,\n\t\t\t\tevent: \"skill_expansion\",\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t\treturn text; // Return original on error\n\t\t}\n\t}\n\n\t/**\n\t * Queue a steering message while the agent is running.\n\t * Delivered after the current assistant turn finishes executing its tool calls,\n\t * before the next LLM call.\n\t * Expands skill commands and prompt templates. Errors on extension commands.\n\t * @param images Optional image attachments to include with the message\n\t * @throws Error if text is an extension command\n\t */\n\tasync steer(text: string, images?: ImageContent[]): Promise<void> {\n\t\t// Check for extension commands (cannot be queued)\n\t\tif (text.startsWith(\"/\")) {\n\t\t\tthis._throwIfExtensionCommand(text);\n\t\t}\n\n\t\t// Expand skill commands and prompt templates\n\t\tlet expandedText = this._expandSkillCommand(text);\n\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\n\t\tawait this._queueSteer(expandedText, images);\n\t}\n\n\t/**\n\t * Queue a follow-up message to be processed after the agent finishes.\n\t * Delivered only when agent has no more tool calls or steering messages.\n\t * Expands skill commands and prompt templates. Errors on extension commands.\n\t * @param images Optional image attachments to include with the message\n\t * @throws Error if text is an extension command\n\t */\n\tasync followUp(text: string, images?: ImageContent[]): Promise<void> {\n\t\t// Check for extension commands (cannot be queued)\n\t\tif (text.startsWith(\"/\")) {\n\t\t\tthis._throwIfExtensionCommand(text);\n\t\t}\n\n\t\t// Expand skill commands and prompt templates\n\t\tlet expandedText = this._expandSkillCommand(text);\n\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\n\t\tawait this._queueFollowUp(expandedText, images);\n\t}\n\n\t/**\n\t * Internal: Queue a steering message (already expanded, no extension command check).\n\t */\n\tprivate async _queueSteer(\n\t\ttext: string,\n\t\timages?: ImageContent[],\n\t\tsessionEntries?: InputSessionEntry[],\n\t): Promise<void> {\n\t\tthis._steeringMessages.push(text);\n\t\tthis._emitQueueUpdate();\n\t\tconst content: (TextContent | ImageContent)[] = [{ type: \"text\", text }];\n\t\tif (images) {\n\t\t\tcontent.push(...images);\n\t\t}\n\t\tconst message: AgentMessage = {\n\t\t\trole: \"user\",\n\t\t\tcontent,\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\t\tif (sessionEntries?.length) {\n\t\t\tthis._inputSessionEntries.set(message, sessionEntries);\n\t\t}\n\t\tthis.agent.steer(message);\n\t}\n\n\t/**\n\t * Internal: Queue a follow-up message (already expanded, no extension command check).\n\t */\n\tprivate async _queueFollowUp(\n\t\ttext: string,\n\t\timages?: ImageContent[],\n\t\tsessionEntries?: InputSessionEntry[],\n\t): Promise<void> {\n\t\tthis._followUpMessages.push(text);\n\t\tthis._emitQueueUpdate();\n\t\tconst content: (TextContent | ImageContent)[] = [{ type: \"text\", text }];\n\t\tif (images) {\n\t\t\tcontent.push(...images);\n\t\t}\n\t\tconst message: AgentMessage = {\n\t\t\trole: \"user\",\n\t\t\tcontent,\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\t\tif (sessionEntries?.length) {\n\t\t\tthis._inputSessionEntries.set(message, sessionEntries);\n\t\t}\n\t\tthis.agent.followUp(message);\n\t}\n\n\t/**\n\t * Throw an error if the text is an extension command.\n\t */\n\tprivate _throwIfExtensionCommand(text: string): void {\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);\n\t\tconst command = this._extensionRunner.getCommand(commandName);\n\n\t\tif (command) {\n\t\t\tthrow new Error(\n\t\t\t\t`Extension command \"/${commandName}\" cannot be queued. Use prompt() or execute the command when not streaming.`,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Send a custom message to the session. Creates a CustomMessageEntry.\n\t *\n\t * Handles three cases:\n\t * - Streaming: queues message, processed when loop pulls from queue\n\t * - Not streaming + triggerTurn: appends to state/session, starts new turn\n\t * - Not streaming + no trigger: appends to state/session, no turn\n\t *\n\t * @param message Custom message with customType, content, display, details\n\t * @param options.triggerTurn If true and not streaming, triggers a new LLM turn\n\t * @param options.deliverAs Delivery mode: \"steer\", \"followUp\", or \"nextTurn\"\n\t */\n\tasync sendCustomMessage<T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: SendMessageDeliverAs },\n\t): Promise<void> {\n\t\tconst appMessage = {\n\t\t\trole: \"custom\" as const,\n\t\t\tcustomType: message.customType,\n\t\t\tcontent: message.content,\n\t\t\tdisplay: message.display,\n\t\t\tdetails: message.details,\n\t\t\ttimestamp: Date.now(),\n\t\t} satisfies CustomMessage<T>;\n\t\tif (options?.deliverAs === \"nextTurn\") {\n\t\t\tthis._pendingNextTurnMessages.push(appMessage);\n\t\t} else if (this.isStreaming) {\n\t\t\tif (options?.deliverAs === \"followUp\") {\n\t\t\t\tthis.agent.followUp(appMessage);\n\t\t\t} else {\n\t\t\t\tthis.agent.steer(appMessage);\n\t\t\t}\n\t\t} else if (options?.triggerTurn) {\n\t\t\tawait this._runAgentPrompt(appMessage);\n\t\t} else {\n\t\t\tthis.agent.state.messages.push(appMessage);\n\t\t\tthis.sessionManager.appendCustomMessageEntry(\n\t\t\t\tmessage.customType,\n\t\t\t\tmessage.content,\n\t\t\t\tmessage.display,\n\t\t\t\tmessage.details,\n\t\t\t);\n\t\t\tthis._emit({ type: \"message_start\", message: appMessage });\n\t\t\tthis._emit({ type: \"message_end\", message: appMessage });\n\t\t}\n\t}\n\n\t/**\n\t * Send a user message to the agent. Always triggers a turn.\n\t * When the agent is streaming, use deliverAs to specify how to queue the message.\n\t *\n\t * @param content User message content (string or content array)\n\t * @param options.deliverAs Delivery mode when streaming: \"steer\" or \"followUp\"\n\t */\n\tasync sendUserMessage(\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: DeliverAs },\n\t): Promise<void> {\n\t\t// Normalize content to text string + optional images\n\t\tlet text: string;\n\t\tlet images: ImageContent[] | undefined;\n\n\t\tif (typeof content === \"string\") {\n\t\t\ttext = content;\n\t\t} else {\n\t\t\tconst textParts: string[] = [];\n\t\t\timages = [];\n\t\t\tfor (const part of content) {\n\t\t\t\tif (part.type === \"text\") {\n\t\t\t\t\ttextParts.push(part.text);\n\t\t\t\t} else {\n\t\t\t\t\timages.push(part);\n\t\t\t\t}\n\t\t\t}\n\t\t\ttext = textParts.join(\"\\n\");\n\t\t\tif (images.length === 0) images = undefined;\n\t\t}\n\n\t\t// Use prompt() with expandPromptTemplates: false to skip command handling and template expansion\n\t\tawait this.prompt(text, {\n\t\t\texpandPromptTemplates: false,\n\t\t\tstreamingBehavior: options?.deliverAs,\n\t\t\timages,\n\t\t\tsource: \"extension\",\n\t\t});\n\t}\n\n\t/**\n\t * Dispatch input through the same pipeline as typed editor input:\n\t * extension-registered slash commands, prompt templates, and skills all\n\t * resolve as if the user had typed the text (source: \"extension\").\n\t *\n\t * @param options.deliverAs Delivery mode when streaming: \"steer\" or \"followUp\"\n\t * @throws Error if streaming and no deliverAs specified, unless the input is\n\t * handled before reaching the prompt queue (extension commands execute\n\t * immediately; input consumed by an `input` handler returns normally)\n\t */\n\tasync dispatchUserInput(input: string, options?: DispatchUserInputOptions): Promise<void> {\n\t\tawait this.prompt(input, {\n\t\t\texpandPromptTemplates: true,\n\t\t\tstreamingBehavior: options?.deliverAs,\n\t\t\tsource: \"extension\",\n\t\t});\n\t}\n\n\t/**\n\t * Clear all queued messages and return them.\n\t * Useful for restoring to editor when user aborts.\n\t * @returns Object with steering and followUp arrays\n\t */\n\tclearQueue(): { steering: string[]; followUp: string[] } {\n\t\tconst steering = [...this._steeringMessages];\n\t\tconst followUp = [...this._followUpMessages];\n\t\tthis._steeringMessages = [];\n\t\tthis._followUpMessages = [];\n\t\tthis.agent.clearAllQueues();\n\t\tthis._emitQueueUpdate();\n\t\treturn { steering, followUp };\n\t}\n\n\t/** Number of pending messages (includes both steering and follow-up) */\n\tget pendingMessageCount(): number {\n\t\treturn this._steeringMessages.length + this._followUpMessages.length;\n\t}\n\n\t/** Get pending steering messages (read-only) */\n\tgetSteeringMessages(): readonly string[] {\n\t\treturn this._steeringMessages;\n\t}\n\n\t/** Get pending follow-up messages (read-only) */\n\tgetFollowUpMessages(): readonly string[] {\n\t\treturn this._followUpMessages;\n\t}\n\n\tget resourceLoader(): ResourceLoader {\n\t\treturn this._resourceLoader;\n\t}\n\n\t/**\n\t * Abort current operation and wait for agent to become idle.\n\t */\n\tasync abort(): Promise<void> {\n\t\tthis.abortRetry();\n\t\tthis._resetOutputThroughput();\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\t}\n\n\t// =========================================================================\n\t// Model Management\n\t// =========================================================================\n\n\tprivate async _emitModelSelect(\n\t\tnextModel: Model<any>,\n\t\tpreviousModel: Model<any> | undefined,\n\t\tsource: \"set\" | \"cycle\" | \"restore\",\n\t): Promise<void> {\n\t\tif (modelsAreEqual(previousModel, nextModel)) return;\n\t\tawait this._extensionRunner.emit({\n\t\t\ttype: \"model_select\",\n\t\t\tmodel: nextModel,\n\t\t\tpreviousModel,\n\t\t\tsource,\n\t\t});\n\t}\n\n\t/**\n\t * Set model directly.\n\t * Validates that auth is configured, saves to session and settings.\n\t * @throws Error if no auth is configured for the model\n\t */\n\tasync setModel(model: Model<any>): Promise<void> {\n\t\tif (!this._modelRegistry.hasConfiguredAuth(model)) {\n\t\t\tthrow new Error(`No API key for ${model.provider}/${model.id}`);\n\t\t}\n\n\t\tconst previousModel = this.model;\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch();\n\t\tthis.agent.state.model = model;\n\t\tthis.sessionManager.appendModelChange(model.provider, model.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(model.provider, model.id);\n\n\t\t// Re-clamp thinking level for new model's capabilities\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(model, previousModel, \"set\");\n\t}\n\n\t/**\n\t * Cycle to next/previous model.\n\t * Uses scoped models (from --models flag) if available, otherwise all available models.\n\t * @param direction - \"forward\" (default) or \"backward\"\n\t * @returns The new model info, or undefined if only one model available\n\t */\n\tasync cycleModel(direction: \"forward\" | \"backward\" = \"forward\"): Promise<ModelCycleResult | undefined> {\n\t\tif (this._scopedModels.length > 0) {\n\t\t\treturn this._cycleScopedModel(direction);\n\t\t}\n\t\treturn this._cycleAvailableModel(direction);\n\t}\n\n\tprivate async _cycleScopedModel(direction: \"forward\" | \"backward\"): Promise<ModelCycleResult | undefined> {\n\t\tconst scopedModels = this._scopedModels.filter((scoped) => this._modelRegistry.hasConfiguredAuth(scoped.model));\n\t\tif (scopedModels.length <= 1) return undefined;\n\n\t\tconst currentModel = this.model;\n\t\tlet currentIndex = scopedModels.findIndex((sm) => modelsAreEqual(sm.model, currentModel));\n\n\t\tif (currentIndex === -1) currentIndex = 0;\n\t\tconst len = scopedModels.length;\n\t\tconst nextIndex = direction === \"forward\" ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;\n\t\tconst next = scopedModels[nextIndex];\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch(next.thinkingLevel);\n\n\t\t// Apply model\n\t\tthis.agent.state.model = next.model;\n\t\tthis.sessionManager.appendModelChange(next.model.provider, next.model.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(next.model.provider, next.model.id);\n\n\t\t// Apply thinking level.\n\t\t// - Explicit scoped model thinking level overrides current session level\n\t\t// - Undefined scoped model thinking level inherits the current session preference\n\t\t// setThinkingLevel clamps to model capabilities.\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(next.model, currentModel, \"cycle\");\n\n\t\treturn { model: next.model, thinkingLevel: this.thinkingLevel, isScoped: true };\n\t}\n\n\tprivate async _cycleAvailableModel(direction: \"forward\" | \"backward\"): Promise<ModelCycleResult | undefined> {\n\t\tconst availableModels = await this._modelRegistry.getAvailable();\n\t\tif (availableModels.length <= 1) return undefined;\n\n\t\tconst currentModel = this.model;\n\t\tlet currentIndex = availableModels.findIndex((m) => modelsAreEqual(m, currentModel));\n\n\t\tif (currentIndex === -1) currentIndex = 0;\n\t\tconst len = availableModels.length;\n\t\tconst nextIndex = direction === \"forward\" ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;\n\t\tconst nextModel = availableModels[nextIndex];\n\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch();\n\t\tthis.agent.state.model = nextModel;\n\t\tthis.sessionManager.appendModelChange(nextModel.provider, nextModel.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t// Re-clamp thinking level for new model's capabilities\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(nextModel, currentModel, \"cycle\");\n\n\t\treturn { model: nextModel, thinkingLevel: this.thinkingLevel, isScoped: false };\n\t}\n\n\t// =========================================================================\n\t// Thinking Level Management\n\t// =========================================================================\n\n\t/**\n\t * Set thinking level.\n\t * Clamps to model capabilities based on available thinking levels.\n\t * Saves to session and settings only if the level actually changes.\n\t */\n\tsetThinkingLevel(level: ThinkingLevel): void {\n\t\tconst availableLevels = this.getAvailableThinkingLevels();\n\t\tconst effectiveLevel = availableLevels.includes(level) ? level : this._clampThinkingLevel(level, availableLevels);\n\n\t\t// Only persist if actually changing\n\t\tconst previousLevel = this.agent.state.thinkingLevel;\n\t\tconst isChanging = effectiveLevel !== previousLevel;\n\n\t\tthis.agent.state.thinkingLevel = effectiveLevel;\n\n\t\tif (isChanging) {\n\t\t\tthis.sessionManager.appendThinkingLevelChange(effectiveLevel);\n\t\t\tif (this.supportsThinking() || effectiveLevel !== \"off\") {\n\t\t\t\tthis.settingsManager.setDefaultThinkingLevel(effectiveLevel);\n\t\t\t}\n\t\t\tthis._emit({ type: \"thinking_level_changed\", level: effectiveLevel });\n\t\t\tvoid this._extensionRunner.emit({\n\t\t\t\ttype: \"thinking_level_select\",\n\t\t\t\tlevel: effectiveLevel,\n\t\t\t\tpreviousLevel,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Cycle to next thinking level.\n\t * @returns New level, or undefined if model doesn't support thinking\n\t */\n\tcycleThinkingLevel(): ThinkingLevel | undefined {\n\t\tif (!this.supportsThinking()) return undefined;\n\n\t\tconst levels = this.getAvailableThinkingLevels();\n\t\tconst currentIndex = levels.indexOf(this.thinkingLevel);\n\t\tconst nextIndex = (currentIndex + 1) % levels.length;\n\t\tconst nextLevel = levels[nextIndex];\n\n\t\tthis.setThinkingLevel(nextLevel);\n\t\treturn nextLevel;\n\t}\n\n\t/**\n\t * Get available thinking levels for current model.\n\t * The provider will clamp to what the specific model supports internally.\n\t */\n\tgetAvailableThinkingLevels(): ThinkingLevel[] {\n\t\tif (!this.model) return THINKING_LEVELS;\n\t\treturn getSupportedThinkingLevels(this.model) as ThinkingLevel[];\n\t}\n\n\t/**\n\t * Check if current model supports thinking/reasoning.\n\t */\n\tsupportsThinking(): boolean {\n\t\treturn !!this.model?.reasoning;\n\t}\n\n\tprivate _getThinkingLevelForModelSwitch(explicitLevel?: ThinkingLevel): ThinkingLevel {\n\t\tif (explicitLevel !== undefined) {\n\t\t\treturn explicitLevel;\n\t\t}\n\t\tif (!this.supportsThinking()) {\n\t\t\treturn this.settingsManager.getDefaultThinkingLevel() ?? DEFAULT_THINKING_LEVEL;\n\t\t}\n\t\treturn this.thinkingLevel;\n\t}\n\n\tprivate _clampThinkingLevel(level: ThinkingLevel, _availableLevels: ThinkingLevel[]): ThinkingLevel {\n\t\treturn this.model ? (clampThinkingLevel(this.model, level) as ThinkingLevel) : \"off\";\n\t}\n\n\t// =========================================================================\n\t// Queue Mode Management\n\t// =========================================================================\n\n\t/**\n\t * Set steering message mode.\n\t * Saves to settings.\n\t */\n\tsetSteeringMode(mode: \"all\" | \"one-at-a-time\"): void {\n\t\tthis.agent.steeringMode = mode;\n\t\tthis.settingsManager.setSteeringMode(mode);\n\t}\n\n\t/**\n\t * Set follow-up message mode.\n\t * Saves to settings.\n\t */\n\tsetFollowUpMode(mode: \"all\" | \"one-at-a-time\"): void {\n\t\tthis.agent.followUpMode = mode;\n\t\tthis.settingsManager.setFollowUpMode(mode);\n\t}\n\n\t// =========================================================================\n\t// Compaction\n\t// =========================================================================\n\n\t/**\n\t * Manually compact the session context.\n\t * Aborts current agent operation first.\n\t * @param customInstructions Optional instructions for the compaction summary\n\t */\n\tasync compact(customInstructions?: string): Promise<CompactionResult> {\n\t\t// SCRAMJET-DIVERGENCE: acquire one owner before awaits and drain terminal persistence (#598).\n\t\tif (this._disposed) throw new Error(\"Cannot compact a disposed session.\");\n\t\tif (this.isCompacting)\n\t\t\tthrow new Error(\"Compaction or branch summarization is already in progress. Retry after it settles.\");\n\t\tif (this.agent.hasActiveTransientHarnessTools) {\n\t\t\tthrow new Error(\n\t\t\t\t\"A harness tool is still executing outside an Agent run. Retry compaction after its persisted settlement.\",\n\t\t\t);\n\t\t}\n\t\tconst controller = new AbortController();\n\t\tthis._compactionAbortController = controller;\n\t\tconst drain: { firstError?: Error } = {};\n\t\tthis._manualCompactionDrain = drain;\n\t\tconst runs = [...this._unsettledAgentRuns];\n\t\ttry {\n\t\t\tthis._resetOutputThroughput();\n\t\t\tthis._disconnectFromAgent();\n\t\t\tthis._unsubscribeAgent = this.agent.subscribe((event) => this._handleAgentEvent(event));\n\t\t\tthis._emit({ type: \"compaction_start\", reason: \"manual\" });\n\t\t\tawait this.abort();\n\t\t\tawait this._agentEventQueue;\n\t\t\tif (drain.firstError) throw drain.firstError;\n\t\t\tif (this._disposed) throw new Error(\"Session disposed during compaction.\");\n\t\t\tif (controller.signal.aborted) throw new Error(\"Compaction cancelled\");\n\t\t\tif (!this.model) {\n\t\t\t\tthrow new Error(formatNoModelSelectedMessage());\n\t\t\t}\n\n\t\t\tconst { apiKey, headers } = await this._getRequiredRequestAuth(this.model);\n\n\t\t\tconst pathEntries = this.sessionManager.getBranch();\n\t\t\tconst settings = this.settingsManager.getCompactionSettings();\n\n\t\t\tconst preparation = prepareCompaction(pathEntries, settings);\n\t\t\tif (!preparation) {\n\t\t\t\t// Check why we can't compact\n\t\t\t\tconst lastEntry = pathEntries[pathEntries.length - 1];\n\t\t\t\tif (lastEntry?.type === \"compaction\") {\n\t\t\t\t\tthrow new Error(\"Already compacted\");\n\t\t\t\t}\n\t\t\t\tthrow new Error(\"Nothing to compact (session too small)\");\n\t\t\t}\n\n\t\t\tlet extensionCompaction: CompactionResult | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_compact\")) {\n\t\t\t\tconst result = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_compact\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tbranchEntries: pathEntries,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t})) as SessionBeforeCompactResult | undefined;\n\n\t\t\t\tif (result?.cancel) {\n\t\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t\t}\n\n\t\t\t\tif (result?.compaction) {\n\t\t\t\t\textensionCompaction = result.compaction;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet summary: string;\n\t\t\tlet firstKeptEntryId: string;\n\t\t\tlet tokensBefore: number;\n\t\t\tlet details: unknown;\n\n\t\t\tif (extensionCompaction) {\n\t\t\t\t// Extension provided compaction content\n\t\t\t\tsummary = extensionCompaction.summary;\n\t\t\t\tfirstKeptEntryId = extensionCompaction.firstKeptEntryId;\n\t\t\t\ttokensBefore = extensionCompaction.tokensBefore;\n\t\t\t\tdetails = extensionCompaction.details;\n\t\t\t} else {\n\t\t\t\t// Generate compaction result\n\t\t\t\tconst result = await compact(\n\t\t\t\t\tpreparation,\n\t\t\t\t\tthis.model,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\tcontroller.signal,\n\t\t\t\t\tthis.thinkingLevel,\n\t\t\t\t);\n\t\t\t\tsummary = result.summary;\n\t\t\t\tfirstKeptEntryId = result.firstKeptEntryId;\n\t\t\t\ttokensBefore = result.tokensBefore;\n\t\t\t\tdetails = result.details;\n\t\t\t}\n\n\t\t\tif (this._disposed) throw new Error(\"Session disposed during compaction.\");\n\t\t\tif (controller.signal.aborted) {\n\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t}\n\n\t\t\tthis.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);\n\t\t\tconst newEntries = this.sessionManager.getEntries();\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Get the saved compaction entry for the extension event\n\t\t\tconst savedCompactionEntry = newEntries.find((e) => e.type === \"compaction\" && e.summary === summary) as\n\t\t\t\t| CompactionEntry\n\t\t\t\t| undefined;\n\n\t\t\tif (this._extensionRunner && savedCompactionEntry) {\n\t\t\t\tawait this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_compact\",\n\t\t\t\t\tcompactionEntry: savedCompactionEntry,\n\t\t\t\t\tfromExtension,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst compactionResult = {\n\t\t\t\tsummary,\n\t\t\t\tfirstKeptEntryId,\n\t\t\t\ttokensBefore,\n\t\t\t\tdetails,\n\t\t\t};\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason: \"manual\",\n\t\t\t\tresult: compactionResult,\n\t\t\t\taborted: false,\n\t\t\t\twillRetry: false,\n\t\t\t});\n\t\t\treturn compactionResult;\n\t\t} catch (error) {\n\t\t\tconst failure = drain.firstError ?? error;\n\t\t\tconst settlementError = failure instanceof Error ? failure : new Error(String(failure));\n\t\t\tthis._releaseRetryForFailedRun(settlementError);\n\t\t\tfor (const run of runs) this._completeAgentRun(run, settlementError);\n\t\t\tconst message = settlementError.message;\n\t\t\tconst aborted = message === \"Compaction cancelled\" || settlementError.name === \"AbortError\";\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason: \"manual\",\n\t\t\t\tresult: undefined,\n\t\t\t\taborted,\n\t\t\t\twillRetry: false,\n\t\t\t\terrorMessage: aborted ? undefined : `Compaction failed: ${message}`,\n\t\t\t});\n\t\t\tthrow failure;\n\t\t} finally {\n\t\t\tthis._disconnectFromAgent();\n\t\t\tthis._manualCompactionDrain = undefined;\n\t\t\tthis._compactionAbortController = undefined;\n\t\t\tthis._reconnectToAgent();\n\t\t}\n\t}\n\n\t/**\n\t * Cancel in-progress compaction (manual or auto).\n\t */\n\tabortCompaction(): void {\n\t\tthis._compactionAbortController?.abort();\n\t\tthis._autoCompactionAbortController?.abort();\n\t}\n\n\t/**\n\t * Cancel in-progress branch summarization.\n\t */\n\tabortBranchSummary(): void {\n\t\tthis._branchSummaryAbortController?.abort();\n\t}\n\n\t/**\n\t * Check if compaction is needed and run it.\n\t * Called after agent_end and before prompt submission.\n\t *\n\t * Two cases:\n\t * 1. Overflow: LLM returned context overflow error, remove error message from agent state, compact, auto-retry\n\t * 2. Threshold: Context over threshold, compact, NO auto-retry (user continues manually)\n\t *\n\t * @param assistantMessage The assistant message to check\n\t * @param skipAbortedCheck If false, include aborted messages (for pre-prompt check). Default: true\n\t */\n\tprivate async _checkCompaction(assistantMessage: AssistantMessage, skipAbortedCheck = true): Promise<void> {\n\t\tif (this._manualCompactionDrain) return;\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\tif (!settings.enabled) return;\n\n\t\t// Skip if message was aborted (user cancelled) - unless skipAbortedCheck is false\n\t\tif (skipAbortedCheck && assistantMessage.stopReason === \"aborted\") return;\n\n\t\t// SCRAMJET-DIVERGENCE: Total context and independent input limits are separate constraints.\n\t\tconst contextWindow = this.model?.contextWindow ?? 0;\n\n\t\t// Skip overflow check if the message came from a different model.\n\t\t// This handles the case where user switched from a smaller-context model (e.g. opus)\n\t\t// to a larger-context model (e.g. codex) - the overflow error from the old model\n\t\t// shouldn't trigger compaction for the new model.\n\t\tconst sameModel =\n\t\t\tthis.model && assistantMessage.provider === this.model.provider && assistantMessage.model === this.model.id;\n\n\t\t// Skip compaction checks if this assistant message is older than the latest\n\t\t// compaction boundary. This prevents a stale pre-compaction usage/error\n\t\t// from retriggering compaction on the first prompt after compaction.\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.getBranch());\n\t\tconst assistantIsFromBeforeCompaction =\n\t\t\tcompactionEntry !== null && assistantMessage.timestamp <= new Date(compactionEntry.timestamp).getTime();\n\t\tif (assistantIsFromBeforeCompaction) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Case 1: Overflow - LLM returned context overflow error\n\t\tif (\n\t\t\tsameModel &&\n\t\t\tisContextOverflow(assistantMessage, Math.min(contextWindow, this.model?.maxInputTokens ?? Infinity))\n\t\t) {\n\t\t\tif (this._overflowRecoveryAttempted) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason: \"overflow\",\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\"Context overflow recovery failed after one compact-and-retry attempt. Try reducing context or switching to a larger-context model.\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis._overflowRecoveryAttempted = true;\n\t\t\t// Remove the error message from agent state (it IS saved to session for history,\n\t\t\t// but we don't want it in context for the retry)\n\t\t\tconst messages = this.agent.state.messages;\n\t\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t\t}\n\t\t\tawait this._runAutoCompaction(\"overflow\", true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Case 2: Threshold - context is getting large\n\t\t// For error messages (no usage data), estimate from last successful response.\n\t\t// This ensures sessions that hit persistent API errors (e.g. 529) can still compact.\n\t\tlet contextTokens: number;\n\t\tif (assistantMessage.stopReason === \"error\") {\n\t\t\tconst messages = this.agent.state.messages;\n\t\t\tconst estimate = estimateContextTokens(messages);\n\t\t\tif (estimate.lastUsageIndex === null) return; // No usage data at all\n\t\t\t// Verify the usage source is post-compaction. Kept pre-compaction messages\n\t\t\t// have stale usage reflecting the old (larger) context and would falsely\n\t\t\t// trigger compaction right after one just finished.\n\t\t\tconst usageMsg = messages[estimate.lastUsageIndex];\n\t\t\tif (\n\t\t\t\tcompactionEntry &&\n\t\t\t\tusageMsg.role === \"assistant\" &&\n\t\t\t\t(usageMsg as AssistantMessage).timestamp <= new Date(compactionEntry.timestamp).getTime()\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcontextTokens = estimate.tokens;\n\t\t} else {\n\t\t\tcontextTokens = calculateContextTokens(assistantMessage.usage);\n\t\t}\n\t\tif (\n\t\t\tshouldCompact(contextTokens, contextWindow, settings) ||\n\t\t\t(contextWindow > settings.reserveTokens && contextTokens > (this.model?.maxInputTokens ?? Infinity))\n\t\t) {\n\t\t\tawait this._runAutoCompaction(\"threshold\", false);\n\t\t}\n\t}\n\n\t/**\n\t * Internal: Run auto-compaction with events.\n\t */\n\tprivate async _runAutoCompaction(reason: \"overflow\" | \"threshold\", willRetry: boolean): Promise<void> {\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\n\t\t// SCRAMJET-DIVERGENCE: reentrant subscribers must observe compaction ownership (#598).\n\t\tthis._autoCompactionAbortController = new AbortController();\n\n\t\ttry {\n\t\t\tthis._emit({ type: \"compaction_start\", reason });\n\t\t\tif (!this.model) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst authResult = await this._modelRegistry.getApiKeyAndHeaders(this.model);\n\t\t\tif (!authResult.ok || !authResult.apiKey) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst { apiKey, headers } = authResult;\n\n\t\t\tconst pathEntries = this.sessionManager.getBranch();\n\n\t\t\tconst preparation = prepareCompaction(pathEntries, settings);\n\t\t\tif (!preparation) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet extensionCompaction: CompactionResult | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_compact\")) {\n\t\t\t\tconst extensionResult = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_compact\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tbranchEntries: pathEntries,\n\t\t\t\t\tcustomInstructions: undefined,\n\t\t\t\t\tsignal: this._autoCompactionAbortController.signal,\n\t\t\t\t})) as SessionBeforeCompactResult | undefined;\n\n\t\t\t\tif (extensionResult?.cancel) {\n\t\t\t\t\tthis._emit({\n\t\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\t\treason,\n\t\t\t\t\t\tresult: undefined,\n\t\t\t\t\t\taborted: true,\n\t\t\t\t\t\twillRetry: false,\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (extensionResult?.compaction) {\n\t\t\t\t\textensionCompaction = extensionResult.compaction;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet summary: string;\n\t\t\tlet firstKeptEntryId: string;\n\t\t\tlet tokensBefore: number;\n\t\t\tlet details: unknown;\n\n\t\t\tif (extensionCompaction) {\n\t\t\t\t// Extension provided compaction content\n\t\t\t\tsummary = extensionCompaction.summary;\n\t\t\t\tfirstKeptEntryId = extensionCompaction.firstKeptEntryId;\n\t\t\t\ttokensBefore = extensionCompaction.tokensBefore;\n\t\t\t\tdetails = extensionCompaction.details;\n\t\t\t} else {\n\t\t\t\t// Generate compaction result\n\t\t\t\tconst compactResult = await compact(\n\t\t\t\t\tpreparation,\n\t\t\t\t\tthis.model,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tundefined,\n\t\t\t\t\tthis._autoCompactionAbortController.signal,\n\t\t\t\t\tthis.thinkingLevel,\n\t\t\t\t);\n\t\t\t\tsummary = compactResult.summary;\n\t\t\t\tfirstKeptEntryId = compactResult.firstKeptEntryId;\n\t\t\t\ttokensBefore = compactResult.tokensBefore;\n\t\t\t\tdetails = compactResult.details;\n\t\t\t}\n\n\t\t\tif (this._autoCompactionAbortController.signal.aborted) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: true,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);\n\t\t\tconst newEntries = this.sessionManager.getEntries();\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Get the saved compaction entry for the extension event\n\t\t\tconst savedCompactionEntry = newEntries.find((e) => e.type === \"compaction\" && e.summary === summary) as\n\t\t\t\t| CompactionEntry\n\t\t\t\t| undefined;\n\n\t\t\tif (this._extensionRunner && savedCompactionEntry) {\n\t\t\t\tawait this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_compact\",\n\t\t\t\t\tcompactionEntry: savedCompactionEntry,\n\t\t\t\t\tfromExtension,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst result: CompactionResult = {\n\t\t\t\tsummary,\n\t\t\t\tfirstKeptEntryId,\n\t\t\t\ttokensBefore,\n\t\t\t\tdetails,\n\t\t\t};\n\t\t\tthis._emit({ type: \"compaction_end\", reason, result, aborted: false, willRetry });\n\n\t\t\tif (willRetry) {\n\t\t\t\tconst messages = this.agent.state.messages;\n\t\t\t\tconst lastMsg = messages[messages.length - 1];\n\t\t\t\tif (lastMsg?.role === \"assistant\" && (lastMsg as AssistantMessage).stopReason === \"error\") {\n\t\t\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t\t\t}\n\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.agent.continue().catch(() => {});\n\t\t\t\t}, 100);\n\t\t\t} else if (this.agent.hasQueuedMessages()) {\n\t\t\t\t// Auto-compaction can complete while follow-up/steering/custom messages are waiting.\n\t\t\t\t// Kick the loop so queued messages are actually delivered.\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.agent.continue().catch(() => {});\n\t\t\t\t}, 100);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconst errorMessage = error instanceof Error ? error.message : \"compaction failed\";\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason,\n\t\t\t\tresult: undefined,\n\t\t\t\taborted: false,\n\t\t\t\twillRetry: false,\n\t\t\t\terrorMessage:\n\t\t\t\t\treason === \"overflow\"\n\t\t\t\t\t\t? `Context overflow recovery failed: ${errorMessage}`\n\t\t\t\t\t\t: `Auto-compaction failed: ${errorMessage}`,\n\t\t\t});\n\t\t} finally {\n\t\t\tthis._autoCompactionAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Toggle auto-compaction setting.\n\t */\n\tsetAutoCompactionEnabled(enabled: boolean): void {\n\t\tthis.settingsManager.setCompactionEnabled(enabled);\n\t}\n\n\t/** Whether auto-compaction is enabled */\n\tget autoCompactionEnabled(): boolean {\n\t\treturn this.settingsManager.getCompactionEnabled();\n\t}\n\n\tasync bindExtensions(bindings: ExtensionBindings): Promise<void> {\n\t\tif (bindings.uiContext !== undefined) {\n\t\t\tthis._extensionUIContext = bindings.uiContext;\n\t\t}\n\t\tif (bindings.commandContextActions !== undefined) {\n\t\t\tthis._extensionCommandContextActions = bindings.commandContextActions;\n\t\t}\n\t\tif (bindings.shutdownHandler !== undefined) {\n\t\t\tthis._extensionShutdownHandler = bindings.shutdownHandler;\n\t\t}\n\t\tif (bindings.onError !== undefined) {\n\t\t\tthis._extensionErrorListener = bindings.onError;\n\t\t}\n\n\t\tthis._applyExtensionBindings(this._extensionRunner);\n\t\tawait this._extensionRunner.emit(this._sessionStartEvent);\n\t\tawait this.extendResourcesFromExtensions(this._sessionStartEvent.reason === \"reload\" ? \"reload\" : \"startup\");\n\t}\n\n\tprivate async extendResourcesFromExtensions(reason: \"startup\" | \"reload\"): Promise<void> {\n\t\tif (!this._extensionRunner.hasHandlers(\"resources_discover\")) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { skillPaths, promptPaths, themePaths } = await this._extensionRunner.emitResourcesDiscover(\n\t\t\tthis._cwd,\n\t\t\treason,\n\t\t);\n\n\t\tif (skillPaths.length === 0 && promptPaths.length === 0 && themePaths.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst extensionPaths: ResourceExtensionPaths = {\n\t\t\tskillPaths: this.buildExtensionResourcePaths(skillPaths),\n\t\t\tpromptPaths: this.buildExtensionResourcePaths(promptPaths),\n\t\t\tthemePaths: this.buildExtensionResourcePaths(themePaths),\n\t\t};\n\n\t\tthis._resourceLoader.extendResources(extensionPaths);\n\t\tthis._rebuildSystemPrompt(this.getActiveToolNames());\n\t}\n\n\tprivate buildExtensionResourcePaths(entries: Array<{ path: string; extensionPath: string }>): Array<{\n\t\tpath: string;\n\t\tmetadata: { source: string; scope: \"temporary\"; origin: \"top-level\"; baseDir?: string };\n\t}> {\n\t\treturn entries.map((entry) => {\n\t\t\tconst source = this.getExtensionSourceLabel(entry.extensionPath);\n\t\t\tconst baseDir = entry.extensionPath.startsWith(\"<\") ? undefined : dirname(entry.extensionPath);\n\t\t\treturn {\n\t\t\t\tpath: entry.path,\n\t\t\t\tmetadata: {\n\t\t\t\t\tsource,\n\t\t\t\t\tscope: \"temporary\",\n\t\t\t\t\torigin: \"top-level\",\n\t\t\t\t\tbaseDir,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t}\n\n\tprivate getExtensionSourceLabel(extensionPath: string): string {\n\t\tif (extensionPath.startsWith(\"<\")) {\n\t\t\treturn `extension:${extensionPath.replace(/[<>]/g, \"\")}`;\n\t\t}\n\t\tconst base = basename(extensionPath);\n\t\tconst name = base.replace(/\\.(ts|js)$/, \"\");\n\t\treturn `extension:${name}`;\n\t}\n\n\tprivate _applyExtensionBindings(runner: ExtensionRunner): void {\n\t\trunner.setUIContext(this._extensionUIContext);\n\t\trunner.bindCommandContext(this._extensionCommandContextActions);\n\n\t\tthis._extensionErrorUnsubscriber?.();\n\t\tthis._extensionErrorUnsubscriber = this._extensionErrorListener\n\t\t\t? runner.onError(this._extensionErrorListener)\n\t\t\t: undefined;\n\t}\n\n\tprivate _refreshCurrentModelFromRegistry(): void {\n\t\tconst currentModel = this.model;\n\t\tif (!currentModel) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst refreshedModel = this._modelRegistry.find(currentModel.provider, currentModel.id);\n\t\tif (!refreshedModel || refreshedModel === currentModel) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.agent.state.model = refreshedModel;\n\t}\n\n\tprivate _bindExtensionCore(runner: ExtensionRunner): void {\n\t\tconst getCommands = (): SlashCommandInfo[] => {\n\t\t\tconst extensionCommands: SlashCommandInfo[] = runner.getRegisteredCommands().map((command) => ({\n\t\t\t\tname: command.invocationName,\n\t\t\t\tdescription: command.description,\n\t\t\t\tsource: \"extension\",\n\t\t\t\tsourceInfo: command.sourceInfo,\n\t\t\t}));\n\n\t\t\tconst templates: SlashCommandInfo[] = this.promptTemplates.map((template) => ({\n\t\t\t\tname: template.name,\n\t\t\t\tdescription: template.description,\n\t\t\t\tsource: \"prompt\",\n\t\t\t\tsourceInfo: template.sourceInfo,\n\t\t\t}));\n\n\t\t\tconst skills: SlashCommandInfo[] = this._resourceLoader.getSkills().skills.map((skill) => ({\n\t\t\t\tname: `skill:${skill.name}`,\n\t\t\t\tdescription: skill.description,\n\t\t\t\tsource: \"skill\",\n\t\t\t\tsourceInfo: skill.sourceInfo,\n\t\t\t}));\n\n\t\t\treturn [...extensionCommands, ...templates, ...skills];\n\t\t};\n\n\t\trunner.bindCore(\n\t\t\t{\n\t\t\t\tsendMessage: (message, options) => {\n\t\t\t\t\tthis.sendCustomMessage(message, options).catch((err) => {\n\t\t\t\t\t\trunner.emitError({\n\t\t\t\t\t\t\textensionPath: \"<runtime>\",\n\t\t\t\t\t\t\tevent: \"send_message\",\n\t\t\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\tsendUserMessage: (content, options) => {\n\t\t\t\t\tthis.sendUserMessage(content, options).catch((err) => {\n\t\t\t\t\t\trunner.emitError({\n\t\t\t\t\t\t\textensionPath: \"<runtime>\",\n\t\t\t\t\t\t\tevent: \"send_user_message\",\n\t\t\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\tappendEntry: (customType, data) => {\n\t\t\t\t\tthis.sessionManager.appendCustomEntry(customType, data);\n\t\t\t\t},\n\t\t\t\tsetSessionName: (name) => {\n\t\t\t\t\tthis.setSessionName(name);\n\t\t\t\t},\n\t\t\t\tgetSessionName: () => {\n\t\t\t\t\treturn this.sessionManager.getSessionName();\n\t\t\t\t},\n\t\t\t\tsetLabel: (entryId, label) => {\n\t\t\t\t\tthis.sessionManager.appendLabelChange(entryId, label);\n\t\t\t\t},\n\t\t\t\tgetActiveTools: () => this.getActiveToolNames(),\n\t\t\t\tgetAllTools: () => this.getAllTools(),\n\t\t\t\tsetActiveTools: (toolNames) => this.setActiveToolsByName(toolNames),\n\t\t\t\t// SCRAMJET-DIVERGENCE: harness-tool invocation (#244).\n\t\t\t\tinvokeHarnessTool: (name, args, options) => this.invokeHarnessTool(name, args, options),\n\t\t\t\trefreshTools: () => this._refreshToolRegistry(),\n\t\t\t\tgetCommands,\n\t\t\t\tsetModel: async (model) => {\n\t\t\t\t\tif (!this.modelRegistry.hasConfiguredAuth(model)) return false;\n\t\t\t\t\tawait this.setModel(model);\n\t\t\t\t\treturn true;\n\t\t\t\t},\n\t\t\t\tgetThinkingLevel: () => this.thinkingLevel,\n\t\t\t\tsetThinkingLevel: (level) => this.setThinkingLevel(level),\n\t\t\t},\n\t\t\t{\n\t\t\t\tgetModel: () => this.model,\n\t\t\t\tgetScopedModels: () => this.scopedModels,\n\t\t\t\tisIdle: () => !this.isStreaming,\n\t\t\t\tgetSignal: () => this.agent.signal,\n\t\t\t\tabort: () => this.abort(),\n\t\t\t\thasPendingMessages: () => this.pendingMessageCount > 0,\n\t\t\t\tshutdown: () => {\n\t\t\t\t\tthis._extensionShutdownHandler?.();\n\t\t\t\t},\n\t\t\t\tgetContextUsage: () => this.getContextUsage(),\n\t\t\t\tcompact: (options) => {\n\t\t\t\t\tvoid (async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await this.compact(options?.customInstructions);\n\t\t\t\t\t\t\toptions?.onComplete?.(result);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\toptions?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t})();\n\t\t\t\t},\n\t\t\t\tgetSystemPrompt: () => this.systemPrompt,\n\t\t\t\tdispatchUserInput: (input, options) => this.dispatchUserInput(input, options),\n\t\t\t},\n\t\t\t{\n\t\t\t\tregisterProvider: (name, config) => {\n\t\t\t\t\tthis._modelRegistry.registerProvider(name, config);\n\t\t\t\t\tthis._refreshCurrentModelFromRegistry();\n\t\t\t\t},\n\t\t\t\tunregisterProvider: (name) => {\n\t\t\t\t\tthis._modelRegistry.unregisterProvider(name);\n\t\t\t\t\tthis._refreshCurrentModelFromRegistry();\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\tprivate _refreshToolRegistry(options?: { activeToolNames?: string[]; includeAllExtensionTools?: boolean }): void {\n\t\tconst previousRegistryNames = new Set(this._toolRegistry.keys());\n\t\tconst previousActiveToolNames = this.getActiveToolNames();\n\t\tconst allowedToolNames = this._allowedToolNames;\n\t\tconst isAllowedTool = (name: string): boolean => !allowedToolNames || allowedToolNames.has(name);\n\n\t\tconst registeredTools = this._extensionRunner.getAllRegisteredTools();\n\t\tconst allCustomTools = [\n\t\t\t...registeredTools,\n\t\t\t...this._customTools.map((definition) => ({\n\t\t\t\tdefinition,\n\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<sdk:${definition.name}>`, { source: \"sdk\" }),\n\t\t\t})),\n\t\t];\n\t\t// SCRAMJET-DIVERGENCE: historical interpretation follows definition collision winners, not execution allowlists (#598).\n\t\tthis._historicalCostDefinitions = new Map(this._baseToolDefinitions);\n\t\tfor (const tool of allCustomTools) {\n\t\t\tthis._historicalCostDefinitions.set(tool.definition.name, tool.definition);\n\t\t}\n\t\tconst allowedCustomTools = allCustomTools.filter((tool) => isAllowedTool(tool.definition.name));\n\t\tconst definitionRegistry = new Map<string, ToolDefinitionEntry>(\n\t\t\tArray.from(this._baseToolDefinitions.entries())\n\t\t\t\t.filter(([name]) => isAllowedTool(name))\n\t\t\t\t.map(([name, definition]) => [\n\t\t\t\t\tname,\n\t\t\t\t\t{\n\t\t\t\t\t\tdefinition,\n\t\t\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<builtin:${name}>`, { source: \"builtin\" }),\n\t\t\t\t\t},\n\t\t\t\t]),\n\t\t);\n\t\tfor (const tool of allowedCustomTools) {\n\t\t\tdefinitionRegistry.set(tool.definition.name, {\n\t\t\t\tdefinition: tool.definition,\n\t\t\t\tsourceInfo: tool.sourceInfo,\n\t\t\t});\n\t\t}\n\t\tthis._toolDefinitions = definitionRegistry;\n\t\tthis._toolPromptSnippets = new Map(\n\t\t\tArray.from(definitionRegistry.values())\n\t\t\t\t.map(({ definition }) => {\n\t\t\t\t\tconst snippet = this._normalizePromptSnippet(definition.promptSnippet);\n\t\t\t\t\treturn snippet ? ([definition.name, snippet] as const) : undefined;\n\t\t\t\t})\n\t\t\t\t.filter((entry): entry is readonly [string, string] => entry !== undefined),\n\t\t);\n\t\tthis._toolPromptGuidelines = new Map(\n\t\t\tArray.from(definitionRegistry.values())\n\t\t\t\t.map(({ definition }) => {\n\t\t\t\t\tconst guidelines = this._normalizePromptGuidelines(definition.promptGuidelines);\n\t\t\t\t\treturn guidelines.length > 0 ? ([definition.name, guidelines] as const) : undefined;\n\t\t\t\t})\n\t\t\t\t.filter((entry): entry is readonly [string, string[]] => entry !== undefined),\n\t\t);\n\t\tconst runner = this._extensionRunner;\n\t\tconst wrappedExtensionTools = wrapRegisteredTools(allowedCustomTools, runner);\n\t\tconst wrappedBuiltInTools = wrapRegisteredTools(\n\t\t\tArray.from(this._baseToolDefinitions.values())\n\t\t\t\t.filter((definition) => isAllowedTool(definition.name))\n\t\t\t\t.map((definition) => ({\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<builtin:${definition.name}>`, { source: \"builtin\" }),\n\t\t\t\t})),\n\t\t\trunner,\n\t\t);\n\n\t\tconst toolRegistry = new Map(wrappedBuiltInTools.map((tool) => [tool.name, tool]));\n\t\tfor (const tool of wrappedExtensionTools as AgentTool[]) {\n\t\t\ttoolRegistry.set(tool.name, tool);\n\t\t}\n\t\tthis._toolRegistry = toolRegistry;\n\n\t\tconst nextActiveToolNames = (\n\t\t\toptions?.activeToolNames ? [...options.activeToolNames] : [...previousActiveToolNames]\n\t\t).filter((name) => isAllowedTool(name));\n\n\t\tif (allowedToolNames) {\n\t\t\tfor (const toolName of this._toolRegistry.keys()) {\n\t\t\t\tif (allowedToolNames.has(toolName)) {\n\t\t\t\t\tnextActiveToolNames.push(toolName);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (options?.includeAllExtensionTools) {\n\t\t\tfor (const tool of wrappedExtensionTools) {\n\t\t\t\tnextActiveToolNames.push(tool.name);\n\t\t\t}\n\t\t} else if (!options?.activeToolNames) {\n\t\t\tfor (const toolName of this._toolRegistry.keys()) {\n\t\t\t\tif (!previousRegistryNames.has(toolName)) {\n\t\t\t\t\tnextActiveToolNames.push(toolName);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.setActiveToolsByName([...new Set(nextActiveToolNames)]);\n\t}\n\n\tprivate _buildRuntime(options: {\n\t\tactiveToolNames?: string[];\n\t\tflagValues?: Map<string, boolean | string>;\n\t\tincludeAllExtensionTools?: boolean;\n\t}): void {\n\t\tconst autoResizeImages = this.settingsManager.getImageAutoResize();\n\t\tconst shellCommandPrefix = this.settingsManager.getShellCommandPrefix();\n\t\tconst shellPath = this.settingsManager.getShellPath();\n\t\tconst baseToolDefinitions = this._baseToolsOverride\n\t\t\t? Object.fromEntries(\n\t\t\t\t\tObject.entries(this._baseToolsOverride).map(([name, tool]) => [\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tcreateToolDefinitionFromAgentTool(tool),\n\t\t\t\t\t]),\n\t\t\t\t)\n\t\t\t: createAllToolDefinitions(this._cwd, {\n\t\t\t\t\tread: { autoResizeImages },\n\t\t\t\t\tbash: { commandPrefix: shellCommandPrefix, shellPath },\n\t\t\t\t});\n\n\t\tthis._baseToolDefinitions = new Map(\n\t\t\tObject.entries(baseToolDefinitions).map(([name, tool]) => [name, tool as ToolDefinition]),\n\t\t);\n\n\t\tconst extensionsResult = this._resourceLoader.getExtensions();\n\t\tif (options.flagValues) {\n\t\t\tfor (const [name, value] of options.flagValues) {\n\t\t\t\textensionsResult.runtime.flagValues.set(name, value);\n\t\t\t}\n\t\t}\n\n\t\tthis._extensionRunner = new ExtensionRunner(\n\t\t\textensionsResult.extensions,\n\t\t\textensionsResult.runtime,\n\t\t\tthis._cwd,\n\t\t\tthis.sessionManager,\n\t\t\tthis._modelRegistry,\n\t\t);\n\t\tif (this._extensionRunnerRef) {\n\t\t\tthis._extensionRunnerRef.current = this._extensionRunner;\n\t\t}\n\t\tthis._bindExtensionCore(this._extensionRunner);\n\t\tthis._applyExtensionBindings(this._extensionRunner);\n\n\t\tconst defaultActiveToolNames = this._baseToolsOverride\n\t\t\t? Object.keys(this._baseToolsOverride)\n\t\t\t: [\"read\", \"bash\", \"edit\", \"write\"];\n\t\tconst baseActiveToolNames = options.activeToolNames ?? defaultActiveToolNames;\n\t\tthis._refreshToolRegistry({\n\t\t\tactiveToolNames: baseActiveToolNames,\n\t\t\tincludeAllExtensionTools: options.includeAllExtensionTools,\n\t\t});\n\t}\n\n\tasync reload(): Promise<void> {\n\t\tconst previousFlagValues = this._extensionRunner.getFlagValues();\n\t\t// SCRAMJET-DIVERGENCE: the Scramjet builtin is a required product component, so reload must validate\n\t\t// the replacement loader before any irreversible runtime mutation. settingsManager.reload() runs first —\n\t\t// it commits fresh settings before the loader can throw, but the re-read is idempotent, so a failed reload\n\t\t// leaves the runtime itself untouched. It also runs first\n\t\t// because the loader's path resolution reads live settings (the loader defers its own settings/source-map\n\t\t// commit until after the builtin init succeeds, so on this path the shared manager is intentionally read\n\t\t// twice). resourceLoader.reload() then re-runs the required\n\t\t// builtin and throws RequiredBuiltinInitError on failure — before session_shutdown is emitted and before\n\t\t// resetApiProviders(). A throwing builtin therefore leaves the current runner, resources, and providers\n\t\t// intact and fires no session_shutdown; only a validated candidate proceeds to teardown and rebuild.\n\t\tawait this.settingsManager.reload();\n\t\tawait this._resourceLoader.reload();\n\t\tawait emitSessionShutdownEvent(this._extensionRunner, { type: \"session_shutdown\", reason: \"reload\" });\n\t\tresetApiProviders();\n\t\tthis._runPromptComposition = { kind: \"generated-base\" };\n\t\tthis._buildRuntime({\n\t\t\tactiveToolNames: this.getActiveToolNames(),\n\t\t\tflagValues: previousFlagValues,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\n\t\tconst hasBindings =\n\t\t\tthis._extensionUIContext ||\n\t\t\tthis._extensionCommandContextActions ||\n\t\t\tthis._extensionShutdownHandler ||\n\t\t\tthis._extensionErrorListener;\n\t\tif (hasBindings) {\n\t\t\tawait this._extensionRunner.emit({ type: \"session_start\", reason: \"reload\" });\n\t\t\tawait this.extendResourcesFromExtensions(\"reload\");\n\t\t}\n\t}\n\n\t// =========================================================================\n\t// Auto-Retry\n\t// =========================================================================\n\n\t// SCRAMJET-DIVERGENCE: persisted post-extension snapshots authoritatively classify and settle retries (#553).\n\tprivate _classifyRetry(message: AssistantMessage): RetryClassification {\n\t\tconst contextWindow = this.model?.contextWindow ?? 0;\n\t\tif (isContextOverflow(message, Math.min(contextWindow, this.model?.maxInputTokens ?? Infinity))) {\n\t\t\treturn { kind: \"context_overflow\" };\n\t\t}\n\n\t\tconst structured = validateResponsesProviderFailure(message.diagnostics);\n\t\tif (structured.status === \"valid\") {\n\t\t\tif (structured.retryDisposition === \"transient\") return { kind: \"retry\", evidence: \"provider_failure\" };\n\t\t\treturn {\n\t\t\t\tkind: \"do_not_retry\",\n\t\t\t\treason: structured.retryDisposition === \"non_transient\" ? \"structured_non_transient\" : \"structured_unknown\",\n\t\t\t\tevidence: \"provider_failure\",\n\t\t\t};\n\t\t}\n\t\tif (structured.status === \"malformed\") {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"malformed_provider_diagnostic\", evidence: \"none\" };\n\t\t}\n\t\tif (structured.status === \"duplicate\") {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"duplicate_provider_diagnostic\", evidence: \"none\" };\n\t\t}\n\t\tif (!message.errorMessage) {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"missing_error_evidence\", evidence: \"none\" };\n\t\t}\n\t\tif (this._isLegacyRetryableError(message.errorMessage)) return { kind: \"retry\", evidence: \"legacy_text\" };\n\t\treturn { kind: \"do_not_retry\", reason: \"legacy_non_retryable\", evidence: \"legacy_text\" };\n\t}\n\n\tprivate _isLegacyRetryableError(errorMessage: string): boolean {\n\t\treturn /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|network.?error|connection.?error|connection.?refused|connection.?lost|websocket.?closed|websocket.?error|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|ended without|stream ended before message_stop|http2 request did not get a response|timed? out|timeout|terminated|retry delay/i.test(\n\t\t\terrorMessage,\n\t\t);\n\t}\n\n\tprivate _bounded(value: number): number {\n\t\treturn Math.min(65_535, Math.max(0, Number.isFinite(value) ? Math.trunc(value) : 65_535));\n\t}\n\n\tprivate _appendAutoRetryRecord(record: AutoRetryRecord): void {\n\t\tthis.sessionManager.appendCustomEntry(\"coding-agent:auto-retry\", record);\n\t}\n\n\t/**\n\t * Persist a terminal record, emit `auto_retry_end` when a retry phase was active, and clear the phase.\n\t * The phase is released before persistence is attempted so a persistence failure can never strand it.\n\t */\n\tprivate _finishRetry(record: AutoRetryRecord, end?: Extract<AgentSessionEvent, { type: \"auto_retry_end\" }>): void {\n\t\tconst retry = this._retry;\n\t\tthis._retry = undefined;\n\t\ttry {\n\t\t\tthis._appendAutoRetryRecord(record);\n\t\t} catch (error) {\n\t\t\tif (retry && !this._manualCompactionDrain) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: end?.attempt ?? retry.attempt,\n\t\t\t\t\tfinalError: \"Retry outcome persistence failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow this._reportRetryFailure(error);\n\t\t}\n\t\tif (retry && end && !this._manualCompactionDrain) this._emit(end);\n\t}\n\n\tprivate _finishRetryWithoutSuccess(record: AutoRetryRecord, message: AssistantMessage): void {\n\t\tconst retry = this._retry;\n\t\tif (retry?.attempt === 0) {\n\t\t\t// The continuation already produced a successful message, so the retry recovered and this\n\t\t\t// later failure is a fresh, non-retried error; close the retry as succeeded first.\n\t\t\tconst attempt = this._runRetryCount;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"succeeded\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(attempt),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: true, attempt },\n\t\t\t);\n\t\t\tthis._finishRetry(record);\n\t\t\treturn;\n\t\t}\n\t\tthis._finishRetry(\n\t\t\trecord,\n\t\t\tretry\n\t\t\t\t? { type: \"auto_retry_end\", success: false, attempt: retry.attempt, finalError: message.errorMessage }\n\t\t\t\t: undefined,\n\t\t);\n\t}\n\n\t/** A run that ends without a classifiable snapshot cannot continue the retry; close it so nothing stays wedged. */\n\tprivate _releaseRetryForFailedRun(error: Error): void {\n\t\tconst retry = this._retry;\n\t\tif (!retry) return;\n\t\ttry {\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"failed\",\n\t\t\t\t\treason: \"run_failed\",\n\t\t\t\t\tattempt: this._bounded(retry.attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt: retry.attempt, finalError: error.message },\n\t\t\t);\n\t\t} catch {\n\t\t\t// Already reported and the phase is released; the run settles with its own error.\n\t\t}\n\t}\n\n\tprivate _reportRetryFailure(error: unknown): Error {\n\t\tconst normalized = error instanceof Error ? error : new Error(String(error));\n\t\tthis._extensionRunner.emitError({\n\t\t\textensionPath: \"agent-session:auto-retry\",\n\t\t\tevent: \"retry_settlement\",\n\t\t\terror: normalized.message,\n\t\t\tstack: normalized.stack,\n\t\t});\n\t\treturn normalized;\n\t}\n\n\tprivate async _handleRetryableError(\n\t\tmessage: AssistantMessage,\n\t\tevidence: \"provider_failure\" | \"legacy_text\",\n\t\trun: AgentRunSettlement,\n\t): Promise<boolean> {\n\t\tconst settings = this.settingsManager.getRetrySettings();\n\t\tconst attempt = (this._retry?.attempt ?? 0) + 1;\n\t\tthis._runRetryCount++;\n\n\t\tconst cumulativeCap = settings.maxRetries * 2;\n\t\tif (attempt > settings.maxRetries) {\n\t\t\tconst attemptsCompleted = attempt - 1;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"exhausted\",\n\t\t\t\t\treason: \"attempt_limit\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attemptsCompleted),\n\t\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt: attemptsCompleted, finalError: message.errorMessage },\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\n\t\tif (this._runRetryCount > cumulativeCap) {\n\t\t\tconst attemptsCompleted = this._runRetryCount - 1;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"exhausted\",\n\t\t\t\t\treason: \"cumulative_limit\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attemptsCompleted),\n\t\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: attemptsCompleted,\n\t\t\t\t\tfinalError:\n\t\t\t\t\t\t`Repeated retry failures (${attemptsCompleted} total attempts this prompt). ${message.errorMessage ?? \"\"}`.trim(),\n\t\t\t\t},\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\n\t\tconst delayMs = settings.baseDelayMs * 2 ** (attempt - 1);\n\t\tconst previous = this._retry;\n\t\tthis._retry = undefined;\n\t\ttry {\n\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\tschemaVersion: 1,\n\t\t\t\toutcome: \"scheduled\",\n\t\t\t\tevidence,\n\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\tdelayMs: this._bounded(delayMs),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (previous) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: previous.attempt,\n\t\t\t\t\tfinalError: \"Retry outcome persistence failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow this._reportRetryFailure(error);\n\t\t}\n\n\t\tconst controller = new AbortController();\n\t\tconst backoff: RetryPhase = { phase: \"backoff\", attempt, controller, run };\n\t\tthis._retry = backoff;\n\t\tthis._emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt,\n\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\tdelayMs,\n\t\t\terrorMessage: message.errorMessage || \"Unknown error\",\n\t\t\tcumulativeErrors: this._runRetryCount,\n\t\t});\n\t\tif (controller.signal.aborted || this._disposed) {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\n\t\tconst messages = this.agent.state.messages;\n\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t}\n\n\t\ttry {\n\t\t\tawait sleep(delayMs, controller.signal);\n\t\t\tawait sleep(0, controller.signal);\n\t\t} catch {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\t\tif (controller.signal.aborted || this._disposed) {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\n\t\tif (this._manualCompactionDrain) return this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\n\t\t// Hand the chain to the continuation before `continue()`; the run that eventually emits\n\t\t// `agent_start` adopts it (see `_captureAgentRunSettlement`), however long Agent defers that start.\n\t\tconst continuing: RetryPhase = { phase: \"continuing\", attempt, chain: run.chain };\n\t\tthis._retry = continuing;\n\t\tconst continuation = this.agent.continue();\n\t\tthis._settleAgentRun(run);\n\t\tcontinuation.catch((error) => {\n\t\t\tif (this._disposed || this._retry !== continuing) return;\n\t\t\tconst continuationError = this._reportRetryFailure(error);\n\t\t\tlet settlementError = continuationError;\n\t\t\ttry {\n\t\t\t\tthis._finishRetry(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"failed\",\n\t\t\t\t\t\treason: \"continuation_rejected\",\n\t\t\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t\t},\n\t\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt, finalError: continuationError.message },\n\t\t\t\t);\n\t\t\t} catch (persistenceError) {\n\t\t\t\tsettlementError = new AggregateError(\n\t\t\t\t\t[continuationError, persistenceError],\n\t\t\t\t\t\"Retry continuation and outcome persistence both failed.\",\n\t\t\t\t\t{ cause: continuationError },\n\t\t\t\t);\n\t\t\t}\n\t\t\tthis._settleRetryChain(run.chain, settlementError);\n\t\t});\n\n\t\treturn true;\n\t}\n\n\tprivate _finishCancelledRetry(\n\t\treason: \"cancelled_during_backoff\" | \"cancelled_during_continuation\",\n\t\texpected?: RetryPhase,\n\t): false {\n\t\tif (this._disposed) {\n\t\t\tthis._retry = undefined;\n\t\t\tthrow new Error(\"AgentSession disposed before retry settlement completed.\");\n\t\t}\n\t\tconst retry = this._retry;\n\t\tif (expected && retry !== expected) return false;\n\t\tconst attempt = retry?.attempt ?? 0;\n\t\tthis._finishRetry(\n\t\t\t{\n\t\t\t\tschemaVersion: 1,\n\t\t\t\toutcome: \"cancelled\",\n\t\t\t\treason,\n\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t},\n\t\t\t{ type: \"auto_retry_end\", success: false, attempt, finalError: \"Retry cancelled\" },\n\t\t);\n\t\treturn false;\n\t}\n\n\t/**\n\t * Cancel in-progress retry.\n\t */\n\tabortRetry(): void {\n\t\tconst retry = this._retry;\n\t\tif (!retry) return;\n\t\tif (retry.phase === \"backoff\") retry.controller.abort();\n\t\tthis.agent.abort();\n\t}\n\n\t/** Agent's own `activeRun` guard is false during backoff; the retry phase owns the idle Agent then. */\n\tprivate _assertRetryNotOwningIdleAgent(): void {\n\t\tif (this._retry && !this.isStreaming) {\n\t\t\tthrow new Error(\"Automatic retry is in progress. Cancel the retry before starting another prompt.\");\n\t\t}\n\t}\n\n\tprivate async _runAgentPrompt(messages: AgentMessage | AgentMessage[]): Promise<void> {\n\t\tthis._assertRetryNotOwningIdleAgent();\n\t\tconst start = this._reserveAgentRunStart();\n\t\tthis._runRetryCount = 0;\n\t\tlet promptFailure: { error: unknown } | undefined;\n\t\ttry {\n\t\t\tawait this.agent.prompt(messages);\n\t\t} catch (error) {\n\t\t\tpromptFailure = { error };\n\t\t}\n\t\tif (!start.run && this._pendingAgentRunStart === start) {\n\t\t\tconst startError = promptFailure\n\t\t\t\t? promptFailure.error instanceof Error\n\t\t\t\t\t? promptFailure.error\n\t\t\t\t\t: new Error(String(promptFailure.error))\n\t\t\t\t: new Error(\"Agent prompt completed before agent_start.\");\n\t\t\tthis._rejectAgentRunStart(start, startError);\n\t\t\tif (promptFailure) throw promptFailure.error;\n\t\t}\n\t\tlet settlementFailure: { error: unknown } | undefined;\n\t\ttry {\n\t\t\tconst settlement = await start.promise;\n\t\t\tawait this.waitForRetry(settlement);\n\t\t} catch (error) {\n\t\t\tsettlementFailure = { error };\n\t\t}\n\t\tif (promptFailure && settlementFailure) {\n\t\t\tthrow new AggregateError(\n\t\t\t\t[promptFailure.error, settlementFailure.error],\n\t\t\t\t\"Agent prompt and retry settlement both failed.\",\n\t\t\t\t{ cause: promptFailure.error },\n\t\t\t);\n\t\t}\n\t\tif (promptFailure) throw promptFailure.error;\n\t\tif (settlementFailure) throw settlementFailure.error;\n\t}\n\n\tprivate async waitForRetry(settlement: AgentRunSettlement | undefined): Promise<void> {\n\t\tif (!settlement) return;\n\t\tawait settlement.promise;\n\t\tawait settlement.chain.promise;\n\t\tawait this.agent.waitForIdle();\n\t}\n\n\t/** Whether auto-retry is currently in progress */\n\tget isRetrying(): boolean {\n\t\treturn this._retry !== undefined;\n\t}\n\n\t/** Whether auto-retry is enabled */\n\tget autoRetryEnabled(): boolean {\n\t\treturn this.settingsManager.getRetryEnabled();\n\t}\n\n\t/**\n\t * Toggle auto-retry setting.\n\t */\n\tsetAutoRetryEnabled(enabled: boolean): void {\n\t\tthis.settingsManager.setRetryEnabled(enabled);\n\t}\n\n\t// =========================================================================\n\t// Bash Execution\n\t// =========================================================================\n\n\t/**\n\t * Execute a bash command.\n\t * Adds result to agent context and session.\n\t * @param command The bash command to execute\n\t * @param onChunk Optional streaming callback for output\n\t * @param options.excludeFromContext If true, command output won't be sent to LLM (!! prefix)\n\t * @param options.operations Custom BashOperations for remote execution\n\t */\n\tasync executeBash(\n\t\tcommand: string,\n\t\tonChunk?: (chunk: string) => void,\n\t\toptions?: { excludeFromContext?: boolean; operations?: BashOperations },\n\t): Promise<BashResult> {\n\t\tthis._bashAbortController = new AbortController();\n\n\t\t// Apply command prefix if configured (e.g., \"shopt -s expand_aliases\" for alias support)\n\t\tconst prefix = this.settingsManager.getShellCommandPrefix();\n\t\tconst shellPath = this.settingsManager.getShellPath();\n\t\tconst resolvedCommand = prefix ? `${prefix}\\n${command}` : command;\n\n\t\ttry {\n\t\t\tconst result = await executeBashWithOperations(\n\t\t\t\tresolvedCommand,\n\t\t\t\tthis.sessionManager.getCwd(),\n\t\t\t\toptions?.operations ?? createLocalBashOperations({ shellPath }),\n\t\t\t\t{\n\t\t\t\t\tonChunk,\n\t\t\t\t\tsignal: this._bashAbortController.signal,\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tthis.recordBashResult(command, result, options);\n\t\t\treturn result;\n\t\t} finally {\n\t\t\tthis._bashAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Record a bash execution result in session history.\n\t * Used by executeBash and by extensions that handle bash execution themselves.\n\t */\n\trecordBashResult(command: string, result: BashResult, options?: { excludeFromContext?: boolean }): void {\n\t\tconst bashMessage: BashExecutionMessage = {\n\t\t\trole: \"bashExecution\",\n\t\t\tcommand,\n\t\t\toutput: result.output,\n\t\t\texitCode: result.exitCode,\n\t\t\tcancelled: result.cancelled,\n\t\t\ttruncated: result.truncated,\n\t\t\tfullOutputPath: result.fullOutputPath,\n\t\t\ttimestamp: Date.now(),\n\t\t\texcludeFromContext: options?.excludeFromContext,\n\t\t};\n\n\t\t// If agent is streaming, defer adding to avoid breaking tool_use/tool_result ordering\n\t\tif (this.isStreaming) {\n\t\t\t// Queue for later - will be flushed on agent_end\n\t\t\tthis._pendingBashMessages.push(bashMessage);\n\t\t} else {\n\t\t\t// Add to agent state immediately\n\t\t\tthis.agent.state.messages.push(bashMessage);\n\n\t\t\t// Save to session\n\t\t\tthis.sessionManager.appendMessage(bashMessage);\n\t\t}\n\t}\n\n\t/**\n\t * Cancel running bash command.\n\t */\n\tabortBash(): void {\n\t\tthis._bashAbortController?.abort();\n\t}\n\n\t/** Whether a bash command is currently running */\n\tget isBashRunning(): boolean {\n\t\treturn this._bashAbortController !== undefined;\n\t}\n\n\t/** Whether there are pending bash messages waiting to be flushed */\n\tget hasPendingBashMessages(): boolean {\n\t\treturn this._pendingBashMessages.length > 0;\n\t}\n\n\t/**\n\t * Flush pending bash messages to agent state and session.\n\t * Called after agent turn completes to maintain proper message ordering.\n\t */\n\tprivate _flushPendingBashMessages(): void {\n\t\tif (this._pendingBashMessages.length === 0) return;\n\n\t\tfor (const bashMessage of this._pendingBashMessages) {\n\t\t\t// Add to agent state\n\t\t\tthis.agent.state.messages.push(bashMessage);\n\n\t\t\t// Save to session\n\t\t\tthis.sessionManager.appendMessage(bashMessage);\n\t\t}\n\n\t\tthis._pendingBashMessages = [];\n\t}\n\n\t// =========================================================================\n\t// Session Management\n\t// =========================================================================\n\n\t/**\n\t * Set a display name for the current session.\n\t */\n\tsetSessionName(name: string): void {\n\t\tthis.sessionManager.appendSessionInfo(name);\n\t\tthis._emit({ type: \"session_info_changed\", name: this.sessionManager.getSessionName() });\n\t}\n\n\t// =========================================================================\n\t// Tree Navigation\n\t// =========================================================================\n\n\t/**\n\t * Navigate to a different node in the session tree.\n\t * Unlike fork() which creates a new session file, this stays in the same file.\n\t *\n\t * @param targetId The entry ID to navigate to\n\t * @param options.summarize Whether user wants to summarize abandoned branch\n\t * @param options.customInstructions Custom instructions for summarizer\n\t * @param options.replaceInstructions If true, customInstructions replaces the default prompt\n\t * @param options.label Label to attach to the branch summary entry\n\t * @returns Result with editorText (if user message) and cancelled status\n\t */\n\tasync navigateTree(\n\t\ttargetId: string,\n\t\toptions: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string } = {},\n\t): Promise<{ editorText?: string; cancelled: boolean; aborted?: boolean; summaryEntry?: BranchSummaryEntry }> {\n\t\tconst oldLeafId = this.sessionManager.getLeafId();\n\n\t\t// No-op if already at target\n\t\tif (targetId === oldLeafId) {\n\t\t\treturn { cancelled: false };\n\t\t}\n\n\t\t// Model required for summarization\n\t\tif (options.summarize && !this.model) {\n\t\t\tthrow new Error(\"No model available for summarization\");\n\t\t}\n\n\t\tconst targetEntry = this.sessionManager.getEntry(targetId);\n\t\tif (!targetEntry) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\n\t\t// Collect entries to summarize (from old leaf to common ancestor)\n\t\tconst { entries: entriesToSummarize, commonAncestorId } = collectEntriesForBranchSummary(\n\t\t\tthis.sessionManager,\n\t\t\toldLeafId,\n\t\t\ttargetId,\n\t\t);\n\n\t\t// Prepare event data - mutable so extensions can override\n\t\tlet customInstructions = options.customInstructions;\n\t\tlet replaceInstructions = options.replaceInstructions;\n\t\tlet label = options.label;\n\n\t\tconst preparation: TreePreparation = {\n\t\t\ttargetId,\n\t\t\toldLeafId,\n\t\t\tcommonAncestorId,\n\t\t\tentriesToSummarize,\n\t\t\tuserWantsSummary: options.summarize ?? false,\n\t\t\tcustomInstructions,\n\t\t\treplaceInstructions,\n\t\t\tlabel,\n\t\t};\n\n\t\t// Set up abort controller for summarization\n\t\tthis._branchSummaryAbortController = new AbortController();\n\n\t\ttry {\n\t\t\tlet extensionSummary: { summary: string; details?: unknown } | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\t// Emit session_before_tree event\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_tree\")) {\n\t\t\t\tconst result = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_tree\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tsignal: this._branchSummaryAbortController.signal,\n\t\t\t\t})) as SessionBeforeTreeResult | undefined;\n\n\t\t\t\tif (result?.cancel) {\n\t\t\t\t\treturn { cancelled: true };\n\t\t\t\t}\n\n\t\t\t\tif (result?.summary && options.summarize) {\n\t\t\t\t\textensionSummary = result.summary;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\n\t\t\t\t// Allow extensions to override instructions and label\n\t\t\t\tif (result?.customInstructions !== undefined) {\n\t\t\t\t\tcustomInstructions = result.customInstructions;\n\t\t\t\t}\n\t\t\t\tif (result?.replaceInstructions !== undefined) {\n\t\t\t\t\treplaceInstructions = result.replaceInstructions;\n\t\t\t\t}\n\t\t\t\tif (result?.label !== undefined) {\n\t\t\t\t\tlabel = result.label;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Run default summarizer if needed\n\t\t\tlet summaryText: string | undefined;\n\t\t\tlet summaryDetails: unknown;\n\t\t\tif (options.summarize && entriesToSummarize.length > 0 && !extensionSummary) {\n\t\t\t\tconst model = this.model!;\n\t\t\t\tconst { apiKey, headers } = await this._getRequiredRequestAuth(model);\n\t\t\t\tconst branchSummarySettings = this.settingsManager.getBranchSummarySettings();\n\t\t\t\tconst result = await generateBranchSummary(entriesToSummarize, {\n\t\t\t\t\tmodel,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tsignal: this._branchSummaryAbortController.signal,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\treplaceInstructions,\n\t\t\t\t\treserveTokens: branchSummarySettings.reserveTokens,\n\t\t\t\t});\n\t\t\t\tif (result.aborted) {\n\t\t\t\t\treturn { cancelled: true, aborted: true };\n\t\t\t\t}\n\t\t\t\tif (result.error) {\n\t\t\t\t\tthrow new Error(result.error);\n\t\t\t\t}\n\t\t\t\tsummaryText = result.summary;\n\t\t\t\tsummaryDetails = {\n\t\t\t\t\treadFiles: result.readFiles || [],\n\t\t\t\t\tmodifiedFiles: result.modifiedFiles || [],\n\t\t\t\t};\n\t\t\t} else if (extensionSummary) {\n\t\t\t\tsummaryText = extensionSummary.summary;\n\t\t\t\tsummaryDetails = extensionSummary.details;\n\t\t\t}\n\n\t\t\t// Determine the new leaf position based on target type\n\t\t\tlet newLeafId: string | null;\n\t\t\tlet editorText: string | undefined;\n\n\t\t\tif (targetEntry.type === \"message\" && targetEntry.message.role === \"user\") {\n\t\t\t\t// User message: leaf = parent (null if root), text goes to editor\n\t\t\t\tnewLeafId = targetEntry.parentId;\n\t\t\t\teditorText = restoreScramjetCommandInvocation(targetEntry, this.sessionManager.getEntries());\n\t\t\t} else if (targetEntry.type === \"custom_message\") {\n\t\t\t\t// Custom message: leaf = parent (null if root), text goes to editor\n\t\t\t\tnewLeafId = targetEntry.parentId;\n\t\t\t\teditorText =\n\t\t\t\t\ttypeof targetEntry.content === \"string\"\n\t\t\t\t\t\t? targetEntry.content\n\t\t\t\t\t\t: targetEntry.content\n\t\t\t\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t\t\t\t.join(\"\");\n\t\t\t} else {\n\t\t\t\t// Non-user message: leaf = selected node\n\t\t\t\tnewLeafId = targetId;\n\t\t\t}\n\n\t\t\t// Switch leaf (with or without summary)\n\t\t\t// Summary is attached at the navigation target position (newLeafId), not the old branch\n\t\t\tlet summaryEntry: BranchSummaryEntry | undefined;\n\t\t\tif (summaryText) {\n\t\t\t\t// Create summary at target position (can be null for root)\n\t\t\t\tconst summaryId = this.sessionManager.branchWithSummary(\n\t\t\t\t\tnewLeafId,\n\t\t\t\t\tsummaryText,\n\t\t\t\t\tsummaryDetails,\n\t\t\t\t\tfromExtension,\n\t\t\t\t);\n\t\t\t\tsummaryEntry = this.sessionManager.getEntry(summaryId) as BranchSummaryEntry;\n\n\t\t\t\t// Attach label to the summary entry\n\t\t\t\tif (label) {\n\t\t\t\t\tthis.sessionManager.appendLabelChange(summaryId, label);\n\t\t\t\t}\n\t\t\t} else if (newLeafId === null) {\n\t\t\t\t// No summary, navigating to root - reset leaf\n\t\t\t\tthis.sessionManager.resetLeaf();\n\t\t\t} else {\n\t\t\t\t// No summary, navigating to non-root\n\t\t\t\tthis.sessionManager.branch(newLeafId);\n\t\t\t}\n\n\t\t\t// Attach label to target entry when not summarizing (no summary entry to label)\n\t\t\tif (label && !summaryText) {\n\t\t\t\tthis.sessionManager.appendLabelChange(targetId, label);\n\t\t\t}\n\n\t\t\t// Update agent state\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Emit session_tree event\n\t\t\tawait this._extensionRunner.emit({\n\t\t\t\ttype: \"session_tree\",\n\t\t\t\tnewLeafId: this.sessionManager.getLeafId(),\n\t\t\t\toldLeafId,\n\t\t\t\tsummaryEntry,\n\t\t\t\tfromExtension: summaryText ? fromExtension : undefined,\n\t\t\t});\n\n\t\t\t// Emit to custom tools\n\n\t\t\treturn { editorText, cancelled: false, summaryEntry };\n\t\t} finally {\n\t\t\tthis._branchSummaryAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Get all user messages from session for fork selector.\n\t */\n\tgetUserMessagesForForking(): Array<{ entryId: string; text: string }> {\n\t\tconst entries = this.sessionManager.getEntries();\n\t\tconst commandStarts = indexScramjetCommandStarts(entries);\n\t\tconst result: Array<{ entryId: string; text: string }> = [];\n\n\t\tfor (const entry of entries) {\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tif (entry.message.role !== \"user\") continue;\n\n\t\t\tconst text = restoreScramjetCommandInvocation(entry, commandStarts);\n\t\t\tif (text) {\n\t\t\t\tresult.push({ entryId: entry.id, text });\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: whole-journal parent/tool costs plus reports awaiting successful append (#598).\n\tgetRecordedSessionCost(): number {\n\t\tlet total = 0;\n\t\tfor (const entry of this.sessionManager.getEntries()) {\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tconst message = entry.message;\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tif (validRecordedCost(message.usage?.cost?.total)) total += message.usage.cost.total;\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\tif (validRecordedCost(message.cost)) {\n\t\t\t\t\ttotal += message.cost;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst definition = this._historicalCostDefinitions.get(message.toolName);\n\t\t\t\tif (!definition?.getHistoricalCost) continue;\n\t\t\t\ttry {\n\t\t\t\t\tconst cost = definition.getHistoricalCost(message.details);\n\t\t\t\t\tif (validRecordedCost(cost)) total += cost;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\t\textensionPath: `tool:${message.toolName}`,\n\t\t\t\t\t\tevent: \"historical_cost\",\n\t\t\t\t\t\terror: `Failed to interpret historical tool cost: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (const execution of this._pendingToolCosts) total += execution.cost ?? 0;\n\t\treturn total;\n\t}\n\n\t/**\n\t * Get session statistics.\n\t */\n\tgetSessionStats(): SessionStats {\n\t\tconst state = this.state;\n\t\tconst userMessages = state.messages.filter((m) => m.role === \"user\").length;\n\t\tconst assistantMessages = state.messages.filter((m) => m.role === \"assistant\").length;\n\t\tconst toolResults = state.messages.filter((m) => m.role === \"toolResult\").length;\n\n\t\tlet toolCalls = 0;\n\t\tlet totalInput = 0;\n\t\tlet totalOutput = 0;\n\t\tlet totalCacheRead = 0;\n\t\tlet totalCacheWrite = 0;\n\t\tlet totalCost = 0;\n\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttoolCalls += assistantMsg.content.filter((c) => c.type === \"toolCall\").length;\n\t\t\t\ttotalInput += assistantMsg.usage.input;\n\t\t\t\ttotalOutput += assistantMsg.usage.output;\n\t\t\t\ttotalCacheRead += assistantMsg.usage.cacheRead;\n\t\t\t\ttotalCacheWrite += assistantMsg.usage.cacheWrite;\n\t\t\t\ttotalCost += assistantMsg.usage.cost.total;\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tsessionFile: this.sessionFile,\n\t\t\tsessionId: this.sessionId,\n\t\t\tuserMessages,\n\t\t\tassistantMessages,\n\t\t\ttoolCalls,\n\t\t\ttoolResults,\n\t\t\ttotalMessages: state.messages.length,\n\t\t\ttokens: {\n\t\t\t\tinput: totalInput,\n\t\t\t\toutput: totalOutput,\n\t\t\t\tcacheRead: totalCacheRead,\n\t\t\t\tcacheWrite: totalCacheWrite,\n\t\t\t\ttotal: totalInput + totalOutput + totalCacheRead + totalCacheWrite,\n\t\t\t},\n\t\t\tcost: totalCost,\n\t\t\tcontextUsage: this.getContextUsage(),\n\t\t};\n\t}\n\n\tgetContextUsage(): ContextUsage | undefined {\n\t\tconst model = this.model;\n\t\tif (!model) return undefined;\n\n\t\tconst contextWindow = model.contextWindow ?? 0;\n\t\tif (contextWindow <= 0) return undefined;\n\n\t\t// After compaction, the last assistant usage reflects pre-compaction context size.\n\t\t// We can only trust usage from an assistant that responded after the latest compaction.\n\t\t// If no such assistant exists, context token count is unknown until the next LLM response.\n\t\tconst branchEntries = this.sessionManager.getBranch();\n\t\tconst latestCompaction = getLatestCompactionEntry(branchEntries);\n\n\t\tif (latestCompaction) {\n\t\t\t// Check if there's a valid assistant usage after the compaction boundary\n\t\t\tconst compactionIndex = branchEntries.lastIndexOf(latestCompaction);\n\t\t\tlet hasPostCompactionUsage = false;\n\t\t\tfor (let i = branchEntries.length - 1; i > compactionIndex; i--) {\n\t\t\t\tconst entry = branchEntries[i];\n\t\t\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\t\t\tconst assistant = entry.message;\n\t\t\t\t\tif (assistant.stopReason !== \"aborted\" && assistant.stopReason !== \"error\") {\n\t\t\t\t\t\tconst contextTokens = calculateContextTokens(assistant.usage);\n\t\t\t\t\t\tif (contextTokens > 0) {\n\t\t\t\t\t\t\thasPostCompactionUsage = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!hasPostCompactionUsage) {\n\t\t\t\treturn { tokens: null, contextWindow, percent: null };\n\t\t\t}\n\t\t}\n\n\t\tconst estimate = estimateContextTokens(this.messages);\n\t\tconst percent = (estimate.tokens / contextWindow) * 100;\n\n\t\treturn {\n\t\t\ttokens: estimate.tokens,\n\t\t\tcontextWindow,\n\t\t\tpercent,\n\t\t};\n\t}\n\n\t/**\n\t * Export session to HTML.\n\t * @param outputPath Optional output path (defaults to session directory)\n\t * @returns Path to exported file\n\t */\n\tasync exportToHtml(outputPath?: string): Promise<string> {\n\t\tconst themeName = this.settingsManager.getTheme();\n\n\t\t// Create tool renderer if we have an extension runner (for custom tool HTML rendering)\n\t\tconst toolRenderer: ToolHtmlRenderer = createToolHtmlRenderer({\n\t\t\tgetToolDefinition: (name) => this.getToolDefinition(name),\n\t\t\ttheme,\n\t\t\tcwd: this.sessionManager.getCwd(),\n\t\t});\n\n\t\treturn await exportSessionToHtml(this.sessionManager, this.state, {\n\t\t\toutputPath,\n\t\t\tthemeName,\n\t\t\ttoolRenderer,\n\t\t});\n\t}\n\n\t/**\n\t * Export the current session branch to a JSONL file.\n\t * Writes the session header followed by all entries on the current branch path.\n\t * @param outputPath Target file path. If omitted, generates a timestamped file in cwd.\n\t * @returns The resolved output file path.\n\t */\n\texportToJsonl(outputPath?: string): string {\n\t\tconst filePath = resolve(outputPath ?? `session-${new Date().toISOString().replace(/[:.]/g, \"-\")}.jsonl`);\n\t\tconst dir = dirname(filePath);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst header: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: CURRENT_SESSION_VERSION,\n\t\t\tid: this.sessionManager.getSessionId(),\n\t\t\ttimestamp: new Date().toISOString(),\n\t\t\tcwd: this.sessionManager.getCwd(),\n\t\t};\n\n\t\tconst branchEntries = this.sessionManager.getBranch();\n\t\tconst lines = [JSON.stringify(header)];\n\n\t\t// Re-chain parentIds to form a linear sequence\n\t\tlet prevId: string | null = null;\n\t\tfor (const entry of branchEntries) {\n\t\t\tconst linear = { ...entry, parentId: prevId };\n\t\t\tlines.push(JSON.stringify(linear));\n\t\t\tprevId = entry.id;\n\t\t}\n\n\t\twriteFileSync(filePath, `${lines.join(\"\\n\")}\\n`);\n\t\treturn filePath;\n\t}\n\n\t// =========================================================================\n\t// Utilities\n\t// =========================================================================\n\n\t/**\n\t * Get text content of last assistant message.\n\t * Useful for /copy command.\n\t * @returns Text content, or undefined if no assistant message exists\n\t */\n\tgetLastAssistantText(): string | undefined {\n\t\tconst lastAssistant = this.messages\n\t\t\t.slice()\n\t\t\t.reverse()\n\t\t\t.find((m) => {\n\t\t\t\tif (m.role !== \"assistant\") return false;\n\t\t\t\tconst msg = m as AssistantMessage;\n\t\t\t\t// Skip aborted messages with no content\n\t\t\t\tif (msg.stopReason === \"aborted\" && msg.content.length === 0) return false;\n\t\t\t\treturn true;\n\t\t\t});\n\n\t\tif (!lastAssistant) return undefined;\n\n\t\tlet text = \"\";\n\t\tfor (const content of (lastAssistant as AssistantMessage).content) {\n\t\t\tif (content.type === \"text\") {\n\t\t\t\ttext += content.text;\n\t\t\t}\n\t\t}\n\n\t\treturn text.trim() || undefined;\n\t}\n\n\t// =========================================================================\n\t// Extension System\n\t// =========================================================================\n\n\tcreateReplacedSessionContext(): ReplacedSessionContext {\n\t\tconst context = Object.defineProperties(\n\t\t\t{},\n\t\t\tObject.getOwnPropertyDescriptors(this._extensionRunner.createCommandContext()),\n\t\t) as ReplacedSessionContext;\n\t\tcontext.sendMessage = (message, options) => this.sendCustomMessage(message, options);\n\t\tcontext.sendUserMessage = (content, options) => this.sendUserMessage(content, options);\n\t\tcontext.dispatchUserInput = (input, options) => this.dispatchUserInput(input, options);\n\t\treturn context;\n\t}\n\n\t/**\n\t * Check if extensions have handlers for a specific event type.\n\t */\n\thasExtensionHandlers(eventType: string): boolean {\n\t\treturn this._extensionRunner.hasHandlers(eventType);\n\t}\n\n\t/**\n\t * Get the extension runner (for setting UI context and error handlers).\n\t */\n\tget extensionRunner(): ExtensionRunner {\n\t\treturn this._extensionRunner;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAIhH,OAAO,KAAK,EAAoB,YAAY,EAAW,KAAK,EAAuB,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAgBxH,OAAO,EAAE,KAAK,UAAU,EAA6B,MAAM,oBAAoB,CAAC;AAChF,OAAO,EACN,KAAK,gBAAgB,EASrB,MAAM,uBAAuB,CAAC;AAI/B,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,wBA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AY,CACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAC;QAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAO,GAC/G,OAAO,CAAC;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAC;QAAC,YAAY,CAAC,EAAE,kBAAkB,CAAA;KAAE,CAAC,CAqL5G;IAED;;OAEG;IACH,yBAAyB,IAAI,KAAK,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAgBpE;IAGD,sBAAsB,IAAI,MAAM,CA4B/B;IAED;;OAEG;IACH,eAAe,IAAI,YAAY,CA2C9B;IAED,eAAe,IAAI,YAAY,GAAG,SAAS,CA4C1C;IAED;;;;OAIG;IACG,YAAY,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAevD;IAED;;;;;OAKG;IACH,aAAa,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CA4BzC;IAMD;;;;OAIG;IACH,oBAAoB,IAAI,MAAM,GAAG,SAAS,CAsBzC;IAMD,4BAA4B,IAAI,sBAAsB,CASrD;IAED;;OAEG;IACH,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAE/C;IAED;;OAEG;IACH,IAAI,eAAe,IAAI,eAAe,CAErC;CACD","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * This class is shared between all run modes (interactive, print, rpc).\n * It encapsulates:\n * - Agent state access\n * - Event subscription with automatic session persistence\n * - Model and thinking level management\n * - Compaction (manual and auto)\n * - Bash execution\n * - Session switching and branching\n *\n * Modes use this class and add their own I/O layer on top.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { basename, dirname, resolve } from \"node:path\";\nimport type { Agent, AgentEvent, AgentMessage, AgentState, AgentTool, ThinkingLevel } from \"@leanandmean/agent\";\n// SCRAMJET-DIVERGENCE: provider-safe harness tool-call id generation for the persisted-settlement\n// acknowledgement key (#341).\nimport { generateHarnessToolCallId } from \"@leanandmean/agent\";\nimport type { AssistantMessage, ImageContent, Message, Model, SystemPromptSection, TextContent } from \"@leanandmean/ai\";\nimport {\n\tblocksFailureRecovery,\n\tclampThinkingLevel,\n\tcleanupSessionResources,\n\tflattenSystemPrompt,\n\tgetSupportedThinkingLevels,\n\tinspectFailureEvidence,\n\tisContextOverflow,\n\tmodelsAreEqual,\n\tresetApiProviders,\n} from \"@leanandmean/ai\";\nimport { theme } from \"../modes/interactive/theme/theme.js\";\nimport { stripFrontmatter } from \"../utils/frontmatter.js\";\nimport { sleep } from \"../utils/sleep.js\";\nimport { formatNoApiKeyFoundMessage, formatNoModelSelectedMessage } from \"./auth-guidance.js\";\nimport { type BashResult, executeBashWithOperations } from \"./bash-executor.js\";\nimport {\n\ttype CompactionResult,\n\tcalculateContextTokens,\n\tcollectEntriesForBranchSummary,\n\tcompact,\n\testimateContextTokens,\n\testimateTokens,\n\tgenerateBranchSummary,\n\tprepareCompaction,\n\tshouldCompact,\n} from \"./compaction/index.js\";\nimport { DEFAULT_THINKING_LEVEL } from \"./defaults.js\";\nimport { exportSessionToHtml, type ToolHtmlRenderer } from \"./export-html/index.js\";\nimport { createToolHtmlRenderer } from \"./export-html/tool-renderer.js\";\nimport {\n\ttype ContextUsage,\n\ttype DeliverAs,\n\ttype DispatchUserInputOptions,\n\ttype ExtensionCommandContextActions,\n\ttype ExtensionErrorListener,\n\tExtensionRunner,\n\ttype ExtensionUIContext,\n\ttype InputSessionEntry,\n\ttype InputSource,\n\ttype InvokeHarnessToolOptions,\n\ttype MessageEndEvent,\n\ttype MessageStartEvent,\n\ttype MessageUpdateEvent,\n\ttype ReplacedSessionContext,\n\ttype SendMessageDeliverAs,\n\ttype SessionBeforeCompactResult,\n\ttype SessionBeforeTreeResult,\n\ttype SessionStartEvent,\n\ttype ShutdownHandler,\n\ttype ToolDefinition,\n\ttype ToolExecutionEndEvent,\n\ttype ToolExecutionStartEvent,\n\ttype ToolExecutionUpdateEvent,\n\ttype ToolInfo,\n\ttype TreePreparation,\n\ttype TurnEndEvent,\n\ttype TurnStartEvent,\n\twrapRegisteredTools,\n} from \"./extensions/index.js\";\nimport { emitSessionShutdownEvent, spliceContributedSections } from \"./extensions/runner.js\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.js\";\nimport type { ModelRegistry } from \"./model-registry.js\";\nimport { type OutputThroughputSample, OutputThroughputTracker } from \"./output-throughput.js\";\nimport { OutputThroughputHistoryStore } from \"./output-throughput-history.js\";\nimport { expandPromptTemplate, type PromptTemplate } from \"./prompt-templates.js\";\nimport type { ResourceExtensionPaths, ResourceLoader } from \"./resource-loader.js\";\nimport { indexScramjetCommandStarts, restoreScramjetCommandInvocation } from \"./scramjet-command-parser.js\";\nimport type { BranchSummaryEntry, CompactionEntry, SessionManager } from \"./session-manager.js\";\nimport { CURRENT_SESSION_VERSION, getLatestCompactionEntry, type SessionHeader } from \"./session-manager.js\";\nimport type { SettingsManager } from \"./settings-manager.js\";\nimport type { SlashCommandInfo } from \"./slash-commands.js\";\nimport { createSyntheticSourceInfo, type SourceInfo } from \"./source-info.js\";\nimport { type BuildSystemPromptOptions, buildSystemPromptSections } from \"./system-prompt.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { createAllToolDefinitions } from \"./tools/index.js\";\nimport { createToolDefinitionFromAgentTool } from \"./tools/tool-definition-wrapper.js\";\n\n// ============================================================================\n// Skill Block Parsing\n// ============================================================================\n\n/** Parsed skill block from a user message */\nexport interface ParsedSkillBlock {\n\tname: string;\n\tlocation: string;\n\tcontent: string;\n\tuserMessage: string | undefined;\n}\n\n/**\n * Parse a skill block from message text.\n * Returns null if the text doesn't contain a skill block.\n */\nexport function parseSkillBlock(text: string): ParsedSkillBlock | null {\n\tconst match = text.match(/^<skill name=\"([^\"]+)\" location=\"([^\"]+)\">\\n([\\s\\S]*?)\\n<\\/skill>(?:\\n\\n([\\s\\S]+))?$/);\n\tif (!match) return null;\n\treturn {\n\t\tname: match[1],\n\t\tlocation: match[2],\n\t\tcontent: match[3],\n\t\tuserMessage: match[4]?.trim() || undefined,\n\t};\n}\n\n/** Session-specific events that extend the core AgentEvent */\nexport type AgentSessionEvent =\n\t| AgentEvent\n\t| {\n\t\t\ttype: \"queue_update\";\n\t\t\tsteering: readonly string[];\n\t\t\tfollowUp: readonly string[];\n\t }\n\t| { type: \"compaction_start\"; reason: \"manual\" | \"threshold\" | \"overflow\" }\n\t| { type: \"session_info_changed\"; name: string | undefined }\n\t| { type: \"thinking_level_changed\"; level: ThinkingLevel }\n\t| {\n\t\t\ttype: \"compaction_end\";\n\t\t\treason: \"manual\" | \"threshold\" | \"overflow\";\n\t\t\tresult: CompactionResult | undefined;\n\t\t\taborted: boolean;\n\t\t\twillRetry: boolean;\n\t\t\terrorMessage?: string;\n\t }\n\t| {\n\t\t\ttype: \"auto_retry_start\";\n\t\t\tattempt: number;\n\t\t\tmaxAttempts: number;\n\t\t\tdelayMs: number;\n\t\t\terrorMessage: string;\n\t\t\tcumulativeErrors?: number;\n\t }\n\t| { type: \"auto_retry_end\"; success: boolean; attempt: number; finalError?: string };\n\n/** Listener function for agent session events */\nexport type AgentSessionEventListener = (event: AgentSessionEvent) => void;\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface AgentSessionConfig {\n\tagent: Agent;\n\tsessionManager: SessionManager;\n\tsettingsManager: SettingsManager;\n\tcwd: string;\n\toutputThroughputHistoryPath: string;\n\t/** Models to cycle through with Ctrl+P (from --models flag) */\n\tscopedModels?: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;\n\t/** Resource loader for skills, prompts, themes, context files, system prompt */\n\tresourceLoader: ResourceLoader;\n\t/** SDK custom tools registered outside extensions */\n\tcustomTools?: ToolDefinition[];\n\t/** Model registry for API key resolution and model discovery */\n\tmodelRegistry: ModelRegistry;\n\t/** Initial active built-in tool names. Default: [read, bash, edit, write] */\n\tinitialActiveToolNames?: string[];\n\t/** Optional allowlist of tool names. When provided, only these tool names are exposed. */\n\tallowedToolNames?: string[];\n\t/**\n\t * Override base tools (useful for custom runtimes).\n\t *\n\t * These are synthesized into minimal ToolDefinitions internally so AgentSession can keep\n\t * a definition-first registry even when callers provide plain AgentTool instances.\n\t */\n\tbaseToolsOverride?: Record<string, AgentTool>;\n\t/** Mutable ref used by Agent to access the current ExtensionRunner */\n\textensionRunnerRef?: { current?: ExtensionRunner };\n\t/** Session start event metadata emitted when extensions bind to this runtime. */\n\tsessionStartEvent?: SessionStartEvent;\n}\n\nexport interface ExtensionBindings {\n\tuiContext?: ExtensionUIContext;\n\tcommandContextActions?: ExtensionCommandContextActions;\n\tshutdownHandler?: ShutdownHandler;\n\tonError?: ExtensionErrorListener;\n}\n\ntype RunPromptComposition =\n\t| { kind: \"generated-base\" }\n\t| { kind: \"contributed-sections\"; sections: SystemPromptSection[] }\n\t| { kind: \"authoritative-string\"; systemPrompt: string };\n\n/** Options for AgentSession.prompt() */\nexport interface PromptOptions {\n\t/** Whether to expand file-based prompt templates (default: true) */\n\texpandPromptTemplates?: boolean;\n\t/** Image attachments */\n\timages?: ImageContent[];\n\t/**\n\t * When streaming, how to queue the message: \"steer\" (interrupt) or\n\t * \"followUp\" (wait). Prompting while streaming without it rejects, unless\n\t * the text is handled before it reaches the prompt queue (extension\n\t * commands execute immediately; input consumed by an `input` handler\n\t * returns without rejecting).\n\t */\n\tstreamingBehavior?: DeliverAs;\n\t/** Source of input for extension input event handlers. Defaults to \"interactive\". */\n\tsource?: InputSource;\n\t/** Internal hook used by RPC mode to observe prompt preflight acceptance or rejection. */\n\tpreflightResult?: (success: boolean) => void;\n}\n\n/** Result from cycleModel() */\nexport interface ModelCycleResult {\n\tmodel: Model<any>;\n\tthinkingLevel: ThinkingLevel;\n\t/** Whether cycling through scoped models (--models flag) or all available */\n\tisScoped: boolean;\n}\n\n/** Session statistics for /session command */\nexport interface SessionStats {\n\tsessionFile: string | undefined;\n\tsessionId: string;\n\tuserMessages: number;\n\tassistantMessages: number;\n\ttoolCalls: number;\n\ttoolResults: number;\n\ttotalMessages: number;\n\ttokens: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n\tcost: number;\n\tcontextUsage?: ContextUsage;\n}\n\ninterface ToolDefinitionEntry {\n\tdefinition: ToolDefinition;\n\tsourceInfo: SourceInfo;\n}\n\n// ============================================================================\n// Constants\n// ============================================================================\n\n/** Standard thinking levels */\nconst THINKING_LEVELS: ThinkingLevel[] = [\"off\", \"minimal\", \"low\", \"medium\", \"high\"];\n\n// ============================================================================\n// AgentSession Class\n// ============================================================================\n\ntype Deferred<T = void> = {\n\tpromise: Promise<T>;\n\tresolve: (value: T) => void;\n\treject: (reason: Error) => void;\n\tsettled: boolean;\n};\n\nfunction createDeferred<T = void>(): Deferred<T> {\n\tlet resolve!: (value: T) => void;\n\tlet reject!: (reason: Error) => void;\n\tconst promise = new Promise<T>((res, rej) => {\n\t\tresolve = res;\n\t\treject = rej;\n\t});\n\tpromise.catch(() => {});\n\treturn { promise, resolve, reject, settled: false };\n}\n\nfunction resolveDeferred<T>(deferred: Deferred<T>, value: T): boolean {\n\tif (deferred.settled) return false;\n\tdeferred.settled = true;\n\tdeferred.resolve(value);\n\treturn true;\n}\n\nfunction rejectDeferred(deferred: Pick<Deferred<unknown>, \"reject\" | \"settled\">, error: Error): boolean {\n\tif (deferred.settled) return false;\n\tdeferred.settled = true;\n\tdeferred.reject(error);\n\treturn true;\n}\n\nfunction settleDeferred(deferred: Deferred, error: Error | undefined): boolean {\n\treturn error ? rejectDeferred(deferred, error) : resolveDeferred(deferred, undefined);\n}\n\n// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement acknowledgement (#341). Each invocation\n// retains synchronous event attribution and its queue tail until Agent execution and persistence both\n// settle, so an early write failure cannot leak later invocation failures into ordinary run state.\ntype HarnessPersistenceAck = Deferred & {\n\tpersistenceTail: Promise<void>;\n\tfirstPersistenceError?: Error;\n};\n\ntype RetryChainSettlement = Deferred;\n\ntype AgentRunSettlement = Deferred & {\n\tchain: RetryChainSettlement;\n\tpersistedAssistantSnapshot?: AssistantMessage;\n\tassistantPersistenceError?: Error;\n};\n\ntype AgentRunStartReservation = Deferred<AgentRunSettlement> & { run?: AgentRunSettlement };\n\n// SCRAMJET-DIVERGENCE: one retry-phase value owns \"retry is active\" (#553). Every finish flows through\n// `_finishRetry`, so a stranded controller, attempt counter, or handoff cannot exist independently.\ntype RetryPhase =\n\t| { phase: \"backoff\"; attempt: number; controller: AbortController; run: AgentRunSettlement }\n\t| { phase: \"continuing\"; attempt: number; chain: RetryChainSettlement };\n\ntype RetryFailureEvidence = \"provider_failure\" | \"request_failure\";\ntype AutoRetryEvidence = RetryFailureEvidence | \"legacy_text\" | \"context_overflow\" | \"none\";\ntype NotAttemptedCause =\n\t| {\n\t\t\treason:\n\t\t\t\t| \"structured_non_transient\"\n\t\t\t\t| \"structured_unknown\"\n\t\t\t\t| \"server_delay_exceeds_limit\"\n\t\t\t\t| \"invalid_server_delay\";\n\t\t\tevidence: RetryFailureEvidence;\n\t }\n\t| { reason: \"non_provider_failure\" | \"invalid_failure_evidence\"; evidence: \"none\" }\n\t| {\n\t\t\treason: \"malformed_provider_diagnostic\" | \"duplicate_provider_diagnostic\" | \"missing_error_evidence\";\n\t\t\tevidence: \"none\";\n\t }\n\t| { reason: \"legacy_non_retryable\"; evidence: \"legacy_text\" };\n// SCRAMJET-DIVERGENCE: closed, replay-inert retry decisions and outcomes preserve #553 evidence invariants.\ntype AutoRetryRecord =\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"not_attempted\";\n\t\t\treason: \"retry_disabled\" | \"session_disposed\";\n\t\t\tevidence: AutoRetryEvidence;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"not_attempted\";\n\t\t\treason: \"context_overflow_compaction\";\n\t\t\tevidence: \"context_overflow\";\n\t }\n\t| ({ schemaVersion: 1; outcome: \"not_attempted\" } & NotAttemptedCause)\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"scheduled\";\n\t\t\tevidence: RetryFailureEvidence | \"legacy_text\";\n\t\t\tattempt: number;\n\t\t\tmaxAttempts: number;\n\t\t\tcumulativeErrors: number;\n\t\t\tdelayMs: number;\n\t }\n\t| { schemaVersion: 1; outcome: \"succeeded\"; attemptsCompleted: number; cumulativeErrors: number }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"failed\";\n\t\t\treason: \"continuation_rejected\" | \"run_failed\";\n\t\t\tattempt: number;\n\t\t\tcumulativeErrors: number;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"cancelled\";\n\t\t\treason: \"cancelled_during_backoff\" | \"cancelled_during_continuation\" | \"session_disposed\";\n\t\t\tattempt: number;\n\t\t\tcumulativeErrors: number;\n\t }\n\t| {\n\t\t\tschemaVersion: 1;\n\t\t\toutcome: \"exhausted\";\n\t\t\treason: \"attempt_limit\" | \"cumulative_limit\";\n\t\t\tattemptsCompleted: number;\n\t\t\tmaxAttempts: number;\n\t\t\tcumulativeErrors: number;\n\t };\n\ntype RetryClassification =\n\t| { kind: \"retry\"; evidence: RetryFailureEvidence | \"legacy_text\" }\n\t| { kind: \"context_overflow\" }\n\t| ({ kind: \"do_not_retry\" } & NotAttemptedCause);\n\nfunction validRecordedCost(value: unknown): value is number {\n\treturn typeof value === \"number\" && Number.isFinite(value) && value >= 0;\n}\n\ninterface LiveToolCost {\n\tcost?: number;\n}\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\treadonly outputThroughputHistoryPath: string;\n\n\tprivate _scopedModels: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>;\n\n\t// Event subscription state\n\tprivate _unsubscribeAgent?: () => void;\n\tprivate _eventListeners: AgentSessionEventListener[] = [];\n\tprivate _agentEventQueue: Promise<void> = Promise.resolve();\n\t// SCRAMJET-DIVERGENCE: execution ownership survives final-event queue latency until append succeeds (#598).\n\tprivate readonly _toolCostExecutions = new Map<string, LiveToolCost>();\n\tprivate readonly _pendingToolCosts = new Set<LiveToolCost>();\n\tprivate _historicalCostDefinitions = new Map<string, ToolDefinition>();\n\tprivate readonly _outputThroughputTracker = new OutputThroughputTracker();\n\tprivate readonly _outputThroughputHistory: OutputThroughputHistoryStore;\n\tprivate _outputThroughputGeneration: number | undefined;\n\tprivate _latestOutputThroughputSample: OutputThroughputSample | undefined;\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement state (#341). `_disposed` gates post-\n\t// teardown invocations; the map keys each in-flight invocation's tool-call id to its persistence\n\t// acknowledgement so `_processAgentEvent` can settle it after the matching tool-result is persisted.\n\tprivate _disposed = false;\n\tprivate readonly _harnessPersistenceAcks = new Map<string, HarnessPersistenceAck>();\n\n\t/** Tracks pending steering messages for UI display. Removed when delivered. */\n\tprivate _steeringMessages: string[] = [];\n\t/** Tracks pending follow-up messages for UI display. Removed when delivered. */\n\tprivate _followUpMessages: string[] = [];\n\t/** Messages queued to be included with the next user prompt as context (\"asides\"). */\n\tprivate _pendingNextTurnMessages: CustomMessage[] = [];\n\tprivate readonly _inputSessionEntries = new WeakMap<AgentMessage, InputSessionEntry[]>();\n\n\t// Compaction state\n\tprivate _compactionAbortController: AbortController | undefined = undefined;\n\tprivate _manualCompactionDrain: { firstError?: Error } | undefined;\n\tprivate _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprivate _overflowRecoveryAttempted = false;\n\n\t// Branch summarization state\n\tprivate _branchSummaryAbortController: AbortController | undefined = undefined;\n\n\t// Retry state\n\tprivate _retry: RetryPhase | undefined = undefined;\n\tprivate _runRetryCount = 0;\n\tprivate _activeAgentRunSettlement: AgentRunSettlement | undefined = undefined;\n\tprivate _pendingAgentRunStart: AgentRunStartReservation | undefined = undefined;\n\tprivate readonly _unsettledAgentRuns = new Set<AgentRunSettlement>();\n\tprivate readonly _unsettledRetryChains = new Set<RetryChainSettlement>();\n\n\t// Bash execution state\n\tprivate _bashAbortController: AbortController | undefined = undefined;\n\tprivate _pendingBashMessages: BashExecutionMessage[] = [];\n\n\t// Extension system\n\tprivate _extensionRunner!: ExtensionRunner;\n\tprivate _turnIndex = 0;\n\n\tprivate _resourceLoader: ResourceLoader;\n\tprivate _customTools: ToolDefinition[];\n\tprivate _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprivate _cwd: string;\n\tprivate _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprivate _initialActiveToolNames?: string[];\n\tprivate _allowedToolNames?: Set<string>;\n\tprivate _baseToolsOverride?: Record<string, AgentTool>;\n\tprivate _sessionStartEvent: SessionStartEvent;\n\tprivate _extensionUIContext?: ExtensionUIContext;\n\tprivate _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprivate _extensionShutdownHandler?: ShutdownHandler;\n\tprivate _extensionErrorListener?: ExtensionErrorListener;\n\tprivate _extensionErrorUnsubscriber?: () => void;\n\n\t// Model registry for API key resolution\n\tprivate _modelRegistry: ModelRegistry;\n\n\t// Tool registry for extension getTools/setTools\n\tprivate _toolRegistry: Map<string, AgentTool> = new Map();\n\tprivate _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprivate _toolPromptSnippets: Map<string, string> = new Map();\n\tprivate _toolPromptGuidelines: Map<string, string[]> = new Map();\n\n\t// Base system prompt as ordered sections (without extension appends) - used to\n\t// apply fresh appends each turn. Sent to the agent as sections so providers\n\t// can apply per-section cache control.\n\tprivate _baseSystemPromptSections: SystemPromptSection[] = [];\n\tprivate _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\t// SCRAMJET-DIVERGENCE: active-tool prompt rebuilds preserve the current top-level run's\n\t// before_agent_start composition while new prompts and reloads establish a fresh composition (#524).\n\tprivate _runPromptComposition: RunPromptComposition = { kind: \"generated-base\" };\n\n\tconstructor(config: AgentSessionConfig) {\n\t\tthis.agent = config.agent;\n\t\tthis.sessionManager = config.sessionManager;\n\t\tthis.settingsManager = config.settingsManager;\n\t\tthis.outputThroughputHistoryPath = config.outputThroughputHistoryPath;\n\t\tthis._outputThroughputHistory = new OutputThroughputHistoryStore(\n\t\t\tthis.outputThroughputHistoryPath,\n\t\t\t(diagnostic) => {\n\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\textensionPath: \"output-throughput-history\",\n\t\t\t\t\tevent: diagnostic.operation,\n\t\t\t\t\terror: `${diagnostic.message} (${diagnostic.path})`,\n\t\t\t\t\tstack: diagnostic.cause instanceof Error ? diagnostic.cause.stack : undefined,\n\t\t\t\t});\n\t\t\t},\n\t\t);\n\t\tthis._scopedModels = config.scopedModels ?? [];\n\t\tthis._resourceLoader = config.resourceLoader;\n\t\tthis._customTools = config.customTools ?? [];\n\t\tthis._cwd = config.cwd;\n\t\tthis._modelRegistry = config.modelRegistry;\n\t\tthis._extensionRunnerRef = config.extensionRunnerRef;\n\t\tthis._initialActiveToolNames = config.initialActiveToolNames;\n\t\tthis._allowedToolNames = config.allowedToolNames ? new Set(config.allowedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\n\t\t// Always subscribe to agent events for internal handling\n\t\t// (session persistence, extensions, auto-compaction, retry logic)\n\t\tthis._unsubscribeAgent = this.agent.subscribe(this._handleAgentEvent);\n\t\tthis._installAgentToolHooks();\n\n\t\tthis._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n\n\t/** Model registry for API key resolution and model discovery */\n\tget modelRegistry(): ModelRegistry {\n\t\treturn this._modelRegistry;\n\t}\n\n\tprivate async _getRequiredRequestAuth(model: Model<any>): Promise<{\n\t\tapiKey: string;\n\t\theaders?: Record<string, string>;\n\t}> {\n\t\tconst result = await this._modelRegistry.getApiKeyAndHeaders(model);\n\t\tif (!result.ok) {\n\t\t\tif (result.error.startsWith(\"No API key found\")) {\n\t\t\t\tthrow new Error(formatNoApiKeyFoundMessage(model.provider));\n\t\t\t}\n\t\t\tthrow new Error(result.error);\n\t\t}\n\t\tif (result.apiKey) {\n\t\t\treturn { apiKey: result.apiKey, headers: result.headers };\n\t\t}\n\n\t\tconst isOAuth = this._modelRegistry.isUsingOAuth(model);\n\t\tif (isOAuth) {\n\t\t\tthrow new Error(\n\t\t\t\t`Authentication failed for \"${model.provider}\". ` +\n\t\t\t\t\t`Credentials may have expired or network is unavailable. ` +\n\t\t\t\t\t`Run '/login ${model.provider}' to re-authenticate.`,\n\t\t\t);\n\t\t}\n\t\tthrow new Error(formatNoApiKeyFoundMessage(model.provider));\n\t}\n\n\t// SCRAMJET-DIVERGENCE: _drainAgentEventQueue ensures all queued async extension event\n\t// processing has settled before proceeding. Used by beforeToolBatch (pre-extraction drain),\n\t// beforeToolCall (per-tool drain), and the awaited turn_end listener barrier.\n\tprivate async _drainAgentEventQueue(): Promise<void> {\n\t\tawait this._agentEventQueue;\n\t}\n\n\t/**\n\t * Install tool hooks once on the Agent instance.\n\t *\n\t * The callbacks read `this._extensionRunner` at execution time, so extension reload swaps in the\n\t * new runner without reinstalling hooks. Extension-specific tool wrappers are still used to adapt\n\t * registered tool execution to the extension context. Tool call and tool result interception now\n\t * happens here instead of in wrappers.\n\t */\n\tprivate _installAgentToolHooks(): void {\n\t\t// SCRAMJET-DIVERGENCE: beforeToolBatch drains queued extension event processing\n\t\t// so async message_end handlers complete before tool-call extraction.\n\t\tthis.agent.beforeToolBatch = async () => {\n\t\t\tawait this._drainAgentEventQueue();\n\t\t};\n\n\t\tthis.agent.beforeToolCall = async ({ toolCall, args }) => {\n\t\t\tconst runner = this._extensionRunner;\n\t\t\tif (!runner.hasHandlers(\"tool_call\")) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tawait this._drainAgentEventQueue();\n\n\t\t\ttry {\n\t\t\t\treturn await runner.emitToolCall({\n\t\t\t\t\ttype: \"tool_call\",\n\t\t\t\t\ttoolName: toolCall.name,\n\t\t\t\t\ttoolCallId: toolCall.id,\n\t\t\t\t\tinput: args as Record<string, unknown>,\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\tthrow err;\n\t\t\t\t}\n\t\t\t\tthrow new Error(`Extension failed, blocking execution: ${String(err)}`);\n\t\t\t}\n\t\t};\n\n\t\tthis.agent.afterToolCall = async ({ toolCall, args, result, isError }) => {\n\t\t\tconst runner = this._extensionRunner;\n\t\t\tif (!runner.hasHandlers(\"tool_result\")) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst hookResult = await runner.emitToolResult({\n\t\t\t\ttype: \"tool_result\",\n\t\t\t\ttoolName: toolCall.name,\n\t\t\t\ttoolCallId: toolCall.id,\n\t\t\t\tinput: args as Record<string, unknown>,\n\t\t\t\tcontent: result.content,\n\t\t\t\tdetails: result.details,\n\t\t\t\tisError,\n\t\t\t});\n\n\t\t\tif (!hookResult) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: hookResult.content,\n\t\t\t\tdetails: hookResult.details,\n\t\t\t\tisError: hookResult.isError ?? isError,\n\t\t\t};\n\t\t};\n\t}\n\n\t// =========================================================================\n\t// Event Subscription\n\t// =========================================================================\n\n\t/** Emit an event to all listeners */\n\tprivate _emit(event: AgentSessionEvent): void {\n\t\tfor (const l of this._eventListeners) {\n\t\t\tl(event);\n\t\t}\n\t}\n\n\tprivate _emitQueueUpdate(): void {\n\t\tthis._emit({\n\t\t\ttype: \"queue_update\",\n\t\t\tsteering: [...this._steeringMessages],\n\t\t\tfollowUp: [...this._followUpMessages],\n\t\t});\n\t}\n\n\t/** Internal handler for agent events - shared by subscribe and reconnect */\n\tprivate _handleAgentEvent = (event: AgentEvent): Promise<void> | void => {\n\t\tif (this._disposed) return;\n\t\tthis._captureOutputThroughput(event);\n\t\tconst toolCost = this._captureToolCost(event);\n\n\t\tconst runSettlement = this._captureAgentRunSettlement(event);\n\t\tconst harnessAck = this._harnessPersistenceAckForEvent(event);\n\t\tconst processing = this._agentEventQueue.then(\n\t\t\t() => this._processAgentEventTracked(event, runSettlement, harnessAck, toolCost),\n\t\t\t() => this._processAgentEventTracked(event, runSettlement, harnessAck, toolCost),\n\t\t);\n\t\tthis._agentEventQueue = processing;\n\t\tif (harnessAck) harnessAck.persistenceTail = processing;\n\n\t\t// Keep queue alive if an event handler fails\n\t\tprocessing.catch(() => {});\n\n\t\t// SCRAMJET-DIVERGENCE: turn_end tool/prompt changes must settle before Agent snapshots\n\t\t// live state for the next provider request (#524).\n\t\tif (event.type === \"turn_end\") return this._agentEventQueue;\n\t};\n\n\t// SCRAMJET-DIVERGENCE: scalar reports are captured before mutable events enter async hooks (#598).\n\tprivate _captureToolCost(event: AgentEvent): LiveToolCost | undefined {\n\t\tif (event.type === \"tool_execution_start\") {\n\t\t\tconst execution: LiveToolCost = {};\n\t\t\tthis._toolCostExecutions.set(event.toolCallId, execution);\n\t\t\tthis._pendingToolCosts.add(execution);\n\t\t\treturn;\n\t\t}\n\t\tlet toolCallId: string;\n\t\tlet reported: unknown;\n\t\tconst final = event.type === \"message_end\" && event.message.role === \"toolResult\";\n\t\tif (event.type === \"tool_execution_update\" || event.type === \"tool_execution_end\") {\n\t\t\ttoolCallId = event.toolCallId;\n\t\t\treported = event.type === \"tool_execution_update\" ? event.partialResult?.cost : event.result?.cost;\n\t\t} else if (event.type === \"message_end\" && event.message.role === \"toolResult\") {\n\t\t\ttoolCallId = event.message.toolCallId;\n\t\t\treported = event.message.cost;\n\t\t} else {\n\t\t\treturn;\n\t\t}\n\t\tlet execution = this._toolCostExecutions.get(toolCallId);\n\t\tif (!execution && validRecordedCost(reported)) {\n\t\t\texecution = {};\n\t\t\tthis._toolCostExecutions.set(toolCallId, execution);\n\t\t\tthis._pendingToolCosts.add(execution);\n\t\t}\n\t\tif (execution && validRecordedCost(reported)) execution.cost = reported;\n\t\tif (final) {\n\t\t\tthis._toolCostExecutions.delete(toolCallId);\n\t\t\treturn execution;\n\t\t}\n\t\treturn;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: Capture provider events synchronously and reset stale throughput lifecycles (#476).\n\tprivate _captureOutputThroughput(event: AgentEvent): void {\n\t\tif (event.type === \"message_start\" && event.message.role === \"assistant\" && event.message.origin === \"provider\") {\n\t\t\tthis._outputThroughputGeneration = this._outputThroughputTracker.start({\n\t\t\t\tprovider: event.message.provider,\n\t\t\t\tmodel: event.message.model,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tconst generation = this._outputThroughputGeneration;\n\t\tif (generation === undefined) return;\n\t\tif (event.type === \"message_update\" && event.message.role === \"assistant\") {\n\t\t\tthis._outputThroughputTracker.observe(generation, event.assistantMessageEvent);\n\t\t} else if (event.type === \"message_end\" && event.message.role === \"assistant\") {\n\t\t\tconst sample = this._outputThroughputTracker.finalize(generation, event.message);\n\t\t\tthis._outputThroughputGeneration = undefined;\n\t\t\tif (sample) {\n\t\t\t\tthis._latestOutputThroughputSample = sample;\n\t\t\t\tthis._outputThroughputHistory.submit(sample);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate _resetOutputThroughput(): void {\n\t\tthis._outputThroughputTracker.reset();\n\t\tthis._outputThroughputGeneration = undefined;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: every Agent run owns persistence state and a one-shot settlement token (#553).\n\tprivate _captureAgentRunSettlement(event: AgentEvent): AgentRunSettlement | undefined {\n\t\tif (event.type === \"agent_start\") {\n\t\t\t// A retry continuation's run adopts the chain its originating prompt awaits; the chain stays\n\t\t\t// bound to the retry phase until this start consumes it, however late Agent emits it.\n\t\t\tconst chain = this._retry?.phase === \"continuing\" ? this._retry.chain : createDeferred();\n\t\t\tthis._unsettledRetryChains.add(chain);\n\t\t\tconst run: AgentRunSettlement = { ...createDeferred(), chain };\n\t\t\tthis._activeAgentRunSettlement = run;\n\t\t\tthis._unsettledAgentRuns.add(run);\n\t\t\tconst reservation = this._pendingAgentRunStart;\n\t\t\tif (reservation) {\n\t\t\t\tthis._pendingAgentRunStart = undefined;\n\t\t\t\treservation.run = run;\n\t\t\t\tresolveDeferred(reservation, run);\n\t\t\t}\n\t\t\treturn run;\n\t\t}\n\n\t\tconst run = this._activeAgentRunSettlement;\n\t\tif (event.type === \"agent_end\" && this._activeAgentRunSettlement === run) {\n\t\t\tthis._activeAgentRunSettlement = undefined;\n\t\t}\n\t\treturn run;\n\t}\n\n\tprivate _settleAgentRun(run: AgentRunSettlement, error?: Error): void {\n\t\tif (settleDeferred(run, error)) this._unsettledAgentRuns.delete(run);\n\t}\n\n\tprivate _settleRetryChain(chain: RetryChainSettlement, error?: Error): void {\n\t\tif (settleDeferred(chain, error)) this._unsettledRetryChains.delete(chain);\n\t}\n\n\tprivate _completeAgentRun(run: AgentRunSettlement, error?: Error): void {\n\t\tthis._settleAgentRun(run, error);\n\t\tthis._settleRetryChain(run.chain, error);\n\t}\n\n\tprivate _reserveAgentRunStart(): AgentRunStartReservation {\n\t\tif (this._pendingAgentRunStart) throw new Error(\"An Agent prompt is already waiting to start.\");\n\t\tconst reservation: AgentRunStartReservation = createDeferred<AgentRunSettlement>();\n\t\tthis._pendingAgentRunStart = reservation;\n\t\treturn reservation;\n\t}\n\n\tprivate _rejectAgentRunStart(reservation: AgentRunStartReservation, error: Error): void {\n\t\tif (this._pendingAgentRunStart === reservation) this._pendingAgentRunStart = undefined;\n\t\trejectDeferred(reservation, error);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted settlement (#341). Synchronous ownership keeps queued\n\t// invocation events attributed after early failures; public settlement awaits execution and the final persistence tail.\n\tprivate async _processAgentEventTracked(\n\t\tevent: AgentEvent,\n\t\trun: AgentRunSettlement | undefined,\n\t\tharnessAck?: HarnessPersistenceAck,\n\t\ttoolCost?: LiveToolCost,\n\t): Promise<void> {\n\t\ttry {\n\t\t\tawait this._processAgentEvent(event, run, harnessAck !== undefined, toolCost);\n\t\t} catch (err) {\n\t\t\tconst error = err instanceof Error ? err : new Error(String(err));\n\t\t\tif (this._manualCompactionDrain) this._manualCompactionDrain.firstError ??= error;\n\t\t\tif (harnessAck) {\n\t\t\t\tharnessAck.firstPersistenceError ??= error;\n\t\t\t} else if (run && !this._manualCompactionDrain) {\n\t\t\t\tif (event.type === \"message_end\" && event.message.role === \"assistant\") {\n\t\t\t\t\trun.assistantPersistenceError = error;\n\t\t\t\t}\n\t\t\t\tthis._releaseRetryForFailedRun(error);\n\t\t\t\tthis._completeAgentRun(run, error);\n\t\t\t}\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tprivate _harnessPersistenceAckForEvent(event: AgentEvent): HarnessPersistenceAck | undefined {\n\t\tlet toolCallId: string | undefined;\n\t\tswitch (event.type) {\n\t\t\tcase \"tool_execution_start\":\n\t\t\tcase \"tool_execution_update\":\n\t\t\tcase \"tool_execution_end\":\n\t\t\t\ttoolCallId = event.toolCallId;\n\t\t\t\tbreak;\n\t\t\tcase \"message_start\":\n\t\t\tcase \"message_end\": {\n\t\t\t\tconst message = event.message;\n\t\t\t\tif (message.role === \"toolResult\") {\n\t\t\t\t\ttoolCallId = message.toolCallId;\n\t\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\t\tfor (const block of message.content) {\n\t\t\t\t\t\tif (block.type === \"toolCall\" && this._harnessPersistenceAcks.has(block.id)) {\n\t\t\t\t\t\t\ttoolCallId = block.id;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn toolCallId === undefined ? undefined : this._harnessPersistenceAcks.get(toolCallId);\n\t}\n\n\tprivate async _processAgentEvent(\n\t\tevent: AgentEvent,\n\t\trun: AgentRunSettlement | undefined,\n\t\tfromHarnessInvocation = false,\n\t\ttoolCost?: LiveToolCost,\n\t): Promise<void> {\n\t\tif (this._disposed) return;\n\t\t// SCRAMJET-DIVERGENCE: queued lifecycle events must respect manual compaction ownership (#598).\n\t\tif (this._manualCompactionDrain && event.type !== \"message_end\" && event.type !== \"agent_end\") return;\n\t\tif (this._manualCompactionDrain && event.type === \"agent_end\") {\n\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\tif (run) this._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\treturn;\n\t\t}\n\t\t// When a user message starts, check if it's from either queue and remove it BEFORE emitting\n\t\t// This ensures the UI sees the updated queue state\n\t\tif (event.type === \"message_start\" && event.message.role === \"user\") {\n\t\t\tthis._overflowRecoveryAttempted = false;\n\t\t\tconst messageText = this._getUserMessageText(event.message);\n\t\t\tif (messageText) {\n\t\t\t\t// Check steering queue first\n\t\t\t\tconst steeringIndex = this._steeringMessages.indexOf(messageText);\n\t\t\t\tif (steeringIndex !== -1) {\n\t\t\t\t\tthis._steeringMessages.splice(steeringIndex, 1);\n\t\t\t\t\tthis._emitQueueUpdate();\n\t\t\t\t} else {\n\t\t\t\t\t// Check follow-up queue\n\t\t\t\t\tconst followUpIndex = this._followUpMessages.indexOf(messageText);\n\t\t\t\t\tif (followUpIndex !== -1) {\n\t\t\t\t\t\tthis._followUpMessages.splice(followUpIndex, 1);\n\t\t\t\t\t\tthis._emitQueueUpdate();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Emit to extensions first\n\t\tawait this._emitExtensionEvent(event);\n\t\tif (event.type === \"message_end\" && event.message.role === \"toolResult\" && toolCost?.cost !== undefined) {\n\t\t\tevent.message.cost = toolCost.cost;\n\t\t}\n\n\t\t// An awaited hook may have acquired manual ownership while this event was in flight.\n\t\tif (this._manualCompactionDrain && event.type !== \"message_end\") {\n\t\t\tif (event.type === \"agent_end\" && run) {\n\t\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\t// Notify all listeners\n\t\tthis._emit(event);\n\t\tif (this._disposed) return;\n\n\t\t// Handle session persistence\n\t\tif (event.type === \"message_end\") {\n\t\t\t// Check if this is a custom message from extensions\n\t\t\tif (event.message.role === \"custom\") {\n\t\t\t\t// Persist as CustomMessageEntry\n\t\t\t\tthis.sessionManager.appendCustomMessageEntry(\n\t\t\t\t\tevent.message.customType,\n\t\t\t\t\tevent.message.content,\n\t\t\t\t\tevent.message.display,\n\t\t\t\t\tevent.message.details,\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\tevent.message.role === \"user\" ||\n\t\t\t\tevent.message.role === \"assistant\" ||\n\t\t\t\tevent.message.role === \"toolResult\"\n\t\t\t) {\n\t\t\t\t// SCRAMJET-DIVERGENCE: retry settlement classifies the exact JSON-safe assistant snapshot\n\t\t\t\t// that crossed the persistence boundary, never a later-mutated live Agent message (#553).\n\t\t\t\tconst persistedMessage =\n\t\t\t\t\tevent.message.role === \"assistant\"\n\t\t\t\t\t\t? (JSON.parse(JSON.stringify(event.message)) as AssistantMessage)\n\t\t\t\t\t\t: event.message;\n\t\t\t\tconst messageEntryId = this.sessionManager.appendMessage(persistedMessage);\n\t\t\t\tif (toolCost) this._pendingToolCosts.delete(toolCost);\n\t\t\t\tif (persistedMessage.role === \"assistant\" && !fromHarnessInvocation && run) {\n\t\t\t\t\trun.persistedAssistantSnapshot = persistedMessage;\n\t\t\t\t}\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\tconst sessionEntries = this._inputSessionEntries.get(event.message);\n\t\t\t\t\tthis._inputSessionEntries.delete(event.message);\n\t\t\t\t\t// This try/catch observes only failures thrown synchronously by\n\t\t\t\t\t// appendCustomEntry. On the buffered first-turn path (before any\n\t\t\t\t\t// assistant message) _persist stores the entry in memory with no I/O,\n\t\t\t\t\t// so only metadata *serialization* failures surface here; the deferred\n\t\t\t\t\t// disk write happens later in the unguarded appendMessage(assistant)\n\t\t\t\t\t// flush, whose throw is swallowed by the _agentEventQueue.catch on the\n\t\t\t\t\t// event-handler chain and degrades exact restoration silently.\n\t\t\t\t\tfor (const entry of sessionEntries ?? []) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tthis.sessionManager.appendCustomEntry(entry.customType, entry.data, messageEntryId);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\t\t\t\textensionPath: `session-entry:${entry.customType}`,\n\t\t\t\t\t\t\t\tevent: \"session_entry_persistence\",\n\t\t\t\t\t\t\t\terror: `Failed to persist attached input metadata after its user message; exact input restoration may be unavailable: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\tstack: err instanceof Error ? err.stack : undefined,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Other message types (bashExecution, compactionSummary, branchSummary) are persisted elsewhere\n\n\t\t\tif (\n\t\t\t\tevent.message.role === \"assistant\" &&\n\t\t\t\tevent.message.stopReason !== \"error\" &&\n\t\t\t\tevent.message.stopReason !== \"aborted\" &&\n\t\t\t\t!fromHarnessInvocation &&\n\t\t\t\t!this._manualCompactionDrain\n\t\t\t) {\n\t\t\t\tthis._overflowRecoveryAttempted = false;\n\t\t\t\tif (this._retry) this._retry = { ...this._retry, attempt: 0 };\n\t\t\t}\n\t\t}\n\n\t\tif (event.type === \"agent_end\") {\n\t\t\tif (this._disposed || !run) return;\n\t\t\tif (this._manualCompactionDrain) {\n\t\t\t\tif (this._retry) this._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run, this._manualCompactionDrain.firstError);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (run.assistantPersistenceError) {\n\t\t\t\tconst error = run.assistantPersistenceError;\n\t\t\t\trun.assistantPersistenceError = undefined;\n\t\t\t\trun.persistedAssistantSnapshot = undefined;\n\t\t\t\tthis._completeAgentRun(run, error);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst msg = run.persistedAssistantSnapshot;\n\t\t\trun.persistedAssistantSnapshot = undefined;\n\t\t\tif (!msg) {\n\t\t\t\tthis._releaseRetryForFailedRun(new Error(\"Retry continuation ended without an assistant message.\"));\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (msg.stopReason === \"aborted\" && this._retry) {\n\t\t\t\tthis._finishCancelledRetry(\"cancelled_during_continuation\");\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (msg.stopReason !== \"error\") {\n\t\t\t\tif (this._retry) {\n\t\t\t\t\tconst attempt = this._runRetryCount;\n\t\t\t\t\tthis._finishRetry(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\t\toutcome: \"succeeded\",\n\t\t\t\t\t\t\tattemptsCompleted: this._bounded(attempt),\n\t\t\t\t\t\t\tcumulativeErrors: this._bounded(attempt),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ type: \"auto_retry_end\", success: true, attempt },\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst classification = this._classifyRetry(msg);\n\t\t\tif (classification.kind === \"context_overflow\") {\n\t\t\t\tthis._finishRetryWithoutSuccess(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\t\treason: \"context_overflow_compaction\",\n\t\t\t\t\t\tevidence: \"context_overflow\",\n\t\t\t\t\t},\n\t\t\t\t\tmsg,\n\t\t\t\t);\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst settings = this.settingsManager.getRetrySettings();\n\t\t\tif (!settings.enabled) {\n\t\t\t\tthis._finishRetryWithoutSuccess(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\t\treason: \"retry_disabled\",\n\t\t\t\t\t\tevidence: classification.evidence,\n\t\t\t\t\t},\n\t\t\t\t\tmsg,\n\t\t\t\t);\n\t\t\t\tthis._completeAgentRun(run);\n\t\t\t\tawait this._checkCompaction(msg);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (classification.kind === \"retry\") {\n\t\t\t\tconst didRetry = await this._handleRetryableError(msg, classification.evidence, run);\n\t\t\t\tif (didRetry) return;\n\t\t\t} else {\n\t\t\t\tconst { kind: _kind, ...cause } = classification;\n\t\t\t\tthis._finishRetryWithoutSuccess({ schemaVersion: 1, outcome: \"not_attempted\", ...cause }, msg);\n\t\t\t}\n\n\t\t\tthis._completeAgentRun(run);\n\t\t\tawait this._checkCompaction(msg);\n\t\t}\n\t}\n\n\t/** Extract text content from a message */\n\tprivate _getUserMessageText(message: Message): string {\n\t\tif (message.role !== \"user\") return \"\";\n\t\tconst content = message.content;\n\t\tif (typeof content === \"string\") return content;\n\t\tconst textBlocks = content.filter((c) => c.type === \"text\");\n\t\treturn textBlocks.map((c) => (c as TextContent).text).join(\"\");\n\t}\n\n\t/** Find the last assistant message in agent state (including aborted ones) */\n\tprivate _findLastAssistantMessage(): AssistantMessage | undefined {\n\t\tconst messages = this.agent.state.messages;\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst msg = messages[i];\n\t\t\tif (msg.role === \"assistant\") {\n\t\t\t\treturn msg as AssistantMessage;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate _replaceMessageInPlace(target: AgentMessage, replacement: AgentMessage): void {\n\t\t// Agent-core stores the finalized message object in its state before emitting message_end.\n\t\t// SessionManager persistence happens later in _processAgentEvent() with event.message.\n\t\t// Mutating this object in place keeps agent state, later turn/agent events, listeners,\n\t\t// and the eventual SessionManager.appendMessage(event.message) persistence in sync.\n\t\tif (target === replacement) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst targetRecord = target as unknown as Record<string, unknown>;\n\t\tfor (const key of Object.keys(targetRecord)) {\n\t\t\tdelete targetRecord[key];\n\t\t}\n\t\tObject.assign(targetRecord, replacement);\n\t}\n\n\t/** Emit extension events based on agent events */\n\tprivate async _emitExtensionEvent(event: AgentEvent): Promise<void> {\n\t\tif (event.type === \"agent_start\") {\n\t\t\tthis._turnIndex = 0;\n\t\t\tawait this._extensionRunner.emit({ type: \"agent_start\" });\n\t\t} else if (event.type === \"agent_end\") {\n\t\t\tawait this._extensionRunner.emit({ type: \"agent_end\", messages: event.messages });\n\t\t} else if (event.type === \"turn_start\") {\n\t\t\tconst extensionEvent: TurnStartEvent = {\n\t\t\t\ttype: \"turn_start\",\n\t\t\t\tturnIndex: this._turnIndex,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"turn_end\") {\n\t\t\tconst extensionEvent: TurnEndEvent = {\n\t\t\t\ttype: \"turn_end\",\n\t\t\t\tturnIndex: this._turnIndex,\n\t\t\t\tmessage: event.message,\n\t\t\t\ttoolResults: event.toolResults,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t\tthis._turnIndex++;\n\t\t} else if (event.type === \"message_start\") {\n\t\t\tconst extensionEvent: MessageStartEvent = {\n\t\t\t\ttype: \"message_start\",\n\t\t\t\tmessage: event.message,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"message_update\") {\n\t\t\tconst extensionEvent: MessageUpdateEvent = {\n\t\t\t\ttype: \"message_update\",\n\t\t\t\tmessage: event.message,\n\t\t\t\tassistantMessageEvent: event.assistantMessageEvent,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"message_end\") {\n\t\t\tconst extensionEvent: MessageEndEvent = {\n\t\t\t\ttype: \"message_end\",\n\t\t\t\tmessage: event.message,\n\t\t\t};\n\t\t\tconst replacement = await this._extensionRunner.emitMessageEnd(extensionEvent);\n\t\t\tif (replacement) {\n\t\t\t\tthis._replaceMessageInPlace(event.message, replacement);\n\t\t\t}\n\t\t} else if (event.type === \"tool_execution_start\") {\n\t\t\tconst extensionEvent: ToolExecutionStartEvent = {\n\t\t\t\ttype: \"tool_execution_start\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\targs: event.args,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"tool_execution_update\") {\n\t\t\tconst extensionEvent: ToolExecutionUpdateEvent = {\n\t\t\t\ttype: \"tool_execution_update\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\targs: event.args,\n\t\t\t\tpartialResult: event.partialResult,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t} else if (event.type === \"tool_execution_end\") {\n\t\t\tconst extensionEvent: ToolExecutionEndEvent = {\n\t\t\t\ttype: \"tool_execution_end\",\n\t\t\t\ttoolCallId: event.toolCallId,\n\t\t\t\ttoolName: event.toolName,\n\t\t\t\tresult: event.result,\n\t\t\t\tisError: event.isError,\n\t\t\t};\n\t\t\tawait this._extensionRunner.emit(extensionEvent);\n\t\t}\n\t}\n\n\t/**\n\t * Subscribe to agent events.\n\t * Session persistence is handled internally (saves messages on message_end).\n\t * Multiple listeners can be added. Returns unsubscribe function for this listener.\n\t */\n\tsubscribe(listener: AgentSessionEventListener): () => void {\n\t\tthis._eventListeners.push(listener);\n\n\t\t// Return unsubscribe function for this specific listener\n\t\treturn () => {\n\t\t\tconst index = this._eventListeners.indexOf(listener);\n\t\t\tif (index !== -1) {\n\t\t\t\tthis._eventListeners.splice(index, 1);\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Temporarily disconnect from agent events.\n\t * User listeners are preserved and will receive events again after resubscribe().\n\t * Used internally during operations that need to pause event processing.\n\t */\n\tprivate _disconnectFromAgent(): void {\n\t\tif (this._unsubscribeAgent) {\n\t\t\tthis._unsubscribeAgent();\n\t\t\tthis._unsubscribeAgent = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Reconnect to agent events after _disconnectFromAgent().\n\t * Preserves all existing listeners.\n\t */\n\tprivate _reconnectToAgent(): void {\n\t\tif (this._disposed || this._unsubscribeAgent) return; // Already connected or disposed\n\t\tthis._unsubscribeAgent = this.agent.subscribe(this._handleAgentEvent);\n\t}\n\n\t/**\n\t * Remove all listeners and disconnect from agent.\n\t * Call this when completely done with the session.\n\t */\n\tdispose(): void {\n\t\t// SCRAMJET-DIVERGENCE: idempotent disposal revokes harness invocations and closes retry settlement (#341, #553).\n\t\t// A second call must not re-reject already-settled acknowledgements or re-run teardown.\n\t\tif (this._disposed) return;\n\t\tthis._disposed = true;\n\t\tthis._toolCostExecutions.clear();\n\t\tthis._pendingToolCosts.clear();\n\t\tthis._resetOutputThroughput();\n\t\tconst retryDisposeError = new Error(\"AgentSession disposed before retry settlement completed.\");\n\t\tconst unsettledRuns = [...this._unsettledAgentRuns];\n\t\tconst retry = this._retry;\n\t\tconst unclassifiedError = unsettledRuns.find(\n\t\t\t(run) => run.persistedAssistantSnapshot?.stopReason === \"error\",\n\t\t)?.persistedAssistantSnapshot;\n\t\tif (retry) this.agent.abort();\n\t\ttry {\n\t\t\tif (retry) {\n\t\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"cancelled\",\n\t\t\t\t\treason: \"session_disposed\",\n\t\t\t\t\tattempt: this._bounded(retry.attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t});\n\t\t\t} else if (unclassifiedError) {\n\t\t\t\tconst classification = this._classifyRetry(unclassifiedError);\n\t\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"not_attempted\",\n\t\t\t\t\treason: \"session_disposed\",\n\t\t\t\t\tevidence: classification.kind === \"context_overflow\" ? \"context_overflow\" : classification.evidence,\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis._reportRetryFailure(error);\n\t\t}\n\t\tif (retry?.phase === \"backoff\") retry.controller.abort();\n\t\tthis._retry = undefined;\n\t\tthis._activeAgentRunSettlement = undefined;\n\t\tconst pendingAgentRunStart = this._pendingAgentRunStart;\n\t\tif (pendingAgentRunStart) this._rejectAgentRunStart(pendingAgentRunStart, retryDisposeError);\n\t\tconst pendingRetryContinuation = retry?.phase === \"continuing\" ? retry.chain : undefined;\n\t\tthis._extensionRunner.invalidate(\n\t\t\t\"This extension ctx is stale after session replacement or reload. Do not use a captured pi or command ctx after ctx.newSession(), ctx.fork(), ctx.switchSession(), or ctx.reload(). For newSession, fork, and switchSession, move post-replacement work into withSession and use the ctx passed to withSession. For reload, do not use the old ctx after await ctx.reload().\",\n\t\t);\n\t\t// Reject pending persistence acknowledgements before revoking Agent-side invocations, so a caller\n\t\t// awaiting invokeHarnessTool sees rejection rather than a promise that never settles. Already-\n\t\t// running tool work may finish afterward; teardown revokes the old-session guarantee but cannot\n\t\t// roll back arbitrary side effects.\n\t\tconst disposeError = new Error(\"AgentSession disposed before the harness tool invocation settled.\");\n\t\tfor (const ack of this._harnessPersistenceAcks.values()) {\n\t\t\tthis._settleHarnessPersistenceAck(ack, disposeError);\n\t\t}\n\t\tthis._harnessPersistenceAcks.clear();\n\t\tthis.agent.rejectUnsettledHarnessTools(disposeError);\n\t\tconst retryChains = new Set(this._unsettledRetryChains);\n\t\tif (pendingRetryContinuation) retryChains.add(pendingRetryContinuation);\n\t\tfor (const run of unsettledRuns) this._settleAgentRun(run, retryDisposeError);\n\t\tfor (const chain of retryChains) this._settleRetryChain(chain, retryDisposeError);\n\t\tthis._disconnectFromAgent();\n\t\tthis._eventListeners = [];\n\t\tcleanupSessionResources(this.sessionId);\n\t}\n\n\t// =========================================================================\n\t// Read-only State Access\n\t// =========================================================================\n\n\t/** Full agent state */\n\tget state(): AgentState {\n\t\treturn this.agent.state;\n\t}\n\n\t/** Current model (may be undefined if not yet selected) */\n\tget model(): Model<any> | undefined {\n\t\treturn this.agent.state.model;\n\t}\n\n\t/** Current thinking level */\n\tget thinkingLevel(): ThinkingLevel {\n\t\treturn this.agent.state.thinkingLevel;\n\t}\n\n\t/** Whether agent is currently streaming a response */\n\tget isStreaming(): boolean {\n\t\treturn this.agent.state.isStreaming;\n\t}\n\n\t/** Current effective system prompt (includes any per-turn extension modifications), flattened to a string */\n\tget systemPrompt(): string {\n\t\treturn flattenSystemPrompt(this.agent.state.systemPrompt);\n\t}\n\n\tget liveOutputRate(): number | undefined {\n\t\tconst model = this.state.model;\n\t\tconst identity = this._outputThroughputTracker.activeIdentity;\n\t\tif (!model || identity?.provider !== model.provider || identity.model !== model.id) return undefined;\n\t\treturn this._outputThroughputTracker.liveRate();\n\t}\n\n\tget outputThroughputActive(): boolean {\n\t\treturn this._outputThroughputTracker.outputActive;\n\t}\n\n\tget outputThroughputGeneration(): number {\n\t\treturn this._outputThroughputTracker.generation;\n\t}\n\n\tget medianOutputRate(): number | undefined {\n\t\tconst model = this.state.model;\n\t\tif (!model) return undefined;\n\t\tconst history = this._outputThroughputHistory.snapshot();\n\t\tconst local = this._latestOutputThroughputSample;\n\t\tif (\n\t\t\tlocal?.provider === model.provider &&\n\t\t\tlocal.model === model.id &&\n\t\t\t!history\n\t\t\t\t.samples(model.provider, model.id)\n\t\t\t\t.some(\n\t\t\t\t\t(sample) =>\n\t\t\t\t\t\tsample.provider === local.provider &&\n\t\t\t\t\t\tsample.model === local.model &&\n\t\t\t\t\t\tsample.responseModel === local.responseModel &&\n\t\t\t\t\t\tsample.outputTokens === local.outputTokens &&\n\t\t\t\t\t\tsample.durationMs === local.durationMs &&\n\t\t\t\t\t\tsample.observedAt === local.observedAt,\n\t\t\t\t)\n\t\t) {\n\t\t\thistory.add(local);\n\t\t}\n\t\treturn history.median(model.provider, model.id);\n\t}\n\n\tget outputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.snapshot();\n\t}\n\n\trefreshOutputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.refresh();\n\t}\n\n\tflushOutputThroughputHistory() {\n\t\treturn this._outputThroughputHistory.flush();\n\t}\n\n\t/** Current retry attempt (0 if not retrying) */\n\tget retryAttempt(): number {\n\t\treturn this._retry?.attempt ?? 0;\n\t}\n\n\t/**\n\t * Get the names of currently active tools.\n\t * Returns the names of tools currently set on the agent.\n\t */\n\tgetActiveToolNames(): string[] {\n\t\treturn this.agent.state.tools.map((t) => t.name);\n\t}\n\n\t/**\n\t * Get all configured tools with name, description, parameter schema, and source metadata.\n\t */\n\tgetAllTools(): ToolInfo[] {\n\t\treturn Array.from(this._toolDefinitions.values()).map(({ definition, sourceInfo }) => ({\n\t\t\tname: definition.name,\n\t\t\tdescription: definition.description,\n\t\t\tparameters: definition.parameters,\n\t\t\tsourceInfo,\n\t\t}));\n\t}\n\n\tgetToolDefinition(name: string): ToolDefinition | undefined {\n\t\treturn this._toolDefinitions.get(name)?.definition;\n\t}\n\n\t/**\n\t * Set active tools by name.\n\t * Only tools in the registry can be enabled. Unknown tool names are ignored.\n\t * Also rebuilds the system prompt to reflect the new tool set.\n\t * Changes take effect on the next agent turn.\n\t */\n\tsetActiveToolsByName(toolNames: string[]): void {\n\t\tconst tools: AgentTool[] = [];\n\t\tconst validToolNames: string[] = [];\n\t\tfor (const name of toolNames) {\n\t\t\t// SCRAMJET-DIVERGENCE: harness-only tools are never placed in the provider-visible tool\n\t\t\t// set, even when explicitly requested (#244). This is the single choke point for\n\t\t\t// `agent.state.tools`, so filtering here structurally guarantees a harness-only tool can\n\t\t\t// never masquerade as a model-callable one — while it stays resolvable in `_toolRegistry`\n\t\t\t// for `invokeHarnessTool`.\n\t\t\tif (this._toolDefinitions.get(name)?.definition.activation === \"harness-only\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst tool = this._toolRegistry.get(name);\n\t\t\tif (tool) {\n\t\t\t\ttools.push(tool);\n\t\t\t\tvalidToolNames.push(name);\n\t\t\t}\n\t\t}\n\t\tthis.agent.state.tools = tools;\n\n\t\t// Rebuild base system prompt with new tool set\n\t\tthis._rebuildSystemPrompt(validToolNames);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool invocation (#244), persisted-settlement boundary (#341).\n\t/**\n\t * Execute a registered tool as a harness-originated call (not requested by the model).\n\t *\n\t * Resolves the wrapped tool from the full registry — including `\"harness-only\"` tools that are\n\t * never provider-visible — so its `execute` still receives the extension context and its\n\t * `tool_call`/`tool_result` hooks still fire. Delegates to {@link Agent.runHarnessTool}, which\n\t * runs the call through the real prepare/execute/finalize pipeline: identical live\n\t * `tool_execution_*`/message events, persisted session entries, and extension hooks, but no\n\t * run/turn framing. The idle-vs-mid-run branch lives in `runHarnessTool` (keyed on the agent's\n\t * active run, which tracks `isStreaming`): idle calls execute immediately, mid-run calls queue\n\t * and drain before the next intra-run LLM call.\n\t *\n\t * **Persisted settlement (#341).** This is a strictly stronger boundary than\n\t * {@link Agent.runHarnessTool}: the returned promise resolves only after (1) the Agent-core\n\t * pipeline fulfills *and* (2) `_processAgentEvent` has persisted the matching tool-result\n\t * `message_end` via `SessionManager.appendMessage`. Because the event queue preserves order,\n\t * that also proves the preceding synthetic assistant tool-call message was persisted first. It\n\t * rejects on Agent-core failure, on failure to process/persist a matching event, on disposal\n\t * before settlement, on a post-disposal call, on an unknown tool, or on a duplicate explicit\n\t * tool-call id that is already pending. **Rejection means the promised pipeline did not complete —\n\t * it does not prove artifact absence** (Agent state and persistence may have partially or fully\n\t * completed before a late failure), so callers must not blindly retry. A tool's own `execute()`\n\t * error is *not* a rejection: it becomes a normal `isError: true` result and the promise resolves\n\t * after that error-result pipeline is persisted.\n\t *\n\t * **Reentrancy.** As with `runHarnessTool`, do not `await` this from work whose return is required\n\t * for the Agent to reach the drain point or for the event queue to persist the matching result.\n\t */\n\tasync invokeHarnessTool(name: string, args: unknown, options?: InvokeHarnessToolOptions): Promise<void> {\n\t\tif (this._disposed) {\n\t\t\tthrow new Error(`Cannot invoke harness tool \"${name}\": the session has been disposed.`);\n\t\t}\n\t\t// SCRAMJET-DIVERGENCE: reject new session work before allocating invocation ownership (#598).\n\t\tif (this._compactionAbortController) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot invoke harness tool \"${name}\": manual compaction is in progress. Retry after it settles.`,\n\t\t\t);\n\t\t}\n\t\tconst tool = this._toolRegistry.get(name);\n\t\tif (!tool) {\n\t\t\tthrow new Error(`Cannot invoke harness tool \"${name}\": no tool with that name is registered.`);\n\t\t}\n\t\tconst toolCallId = this._allocateHarnessToolCallId(options?.toolCallId);\n\t\t// Register before Agent execution can emit events. The coordinator retains attribution until Agent\n\t\t// execution and the invocation's final queued persistence task both settle.\n\t\tconst ack = this._registerHarnessPersistenceAck(toolCallId);\n\t\tvoid this._completeHarnessToolInvocation(this.agent.runHarnessTool(tool, args, { toolCallId }), ack);\n\t\ttry {\n\t\t\tawait ack.promise;\n\t\t} finally {\n\t\t\t// Identity-checked cleanup: only drop the entry if it is still this invocation's ack (disposal\n\t\t\t// may have cleared the map, and a later same-id invocation must not have its entry removed).\n\t\t\tif (this._harnessPersistenceAcks.get(toolCallId) === ack) {\n\t\t\t\tthis._harnessPersistenceAcks.delete(toolCallId);\n\t\t\t}\n\t\t}\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement (#341). Allocate the tool-call id used as\n\t// the acknowledgement key: reject a duplicate explicit id that is still pending, retry a generated\n\t// id on the (astronomically unlikely) collision with a pending one.\n\tprivate _allocateHarnessToolCallId(explicit?: string): string {\n\t\tif (explicit !== undefined) {\n\t\t\t// Validate at the public trust boundary (invokeHarnessTool is on ExtensionAPI): an explicit\n\t\t\t// id flows straight into the synthetic assistant message and on to the provider, so it must\n\t\t\t// meet the provider-safe shape generateHarnessToolCallId guarantees (Anthropic's\n\t\t\t// ^[a-zA-Z0-9_-]+$, bounded length).\n\t\t\tif (!/^[a-zA-Z0-9_-]{1,64}$/.test(explicit)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Cannot invoke harness tool with tool-call id \"${explicit}\": ids must be 1-64 characters of [a-zA-Z0-9_-].`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (this._harnessPersistenceAcks.has(explicit)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Cannot invoke harness tool with tool-call id \"${explicit}\": an invocation with that id is already pending.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn explicit;\n\t\t}\n\t\tlet id = generateHarnessToolCallId();\n\t\twhile (this._harnessPersistenceAcks.has(id)) {\n\t\t\tid = generateHarnessToolCallId();\n\t\t}\n\t\treturn id;\n\t}\n\n\tprivate async _completeHarnessToolInvocation(execution: Promise<void>, ack: HarnessPersistenceAck): Promise<void> {\n\t\tlet executionError: Error | undefined;\n\t\ttry {\n\t\t\tawait execution;\n\t\t} catch (error) {\n\t\t\texecutionError = error instanceof Error ? error : new Error(String(error));\n\t\t}\n\t\ttry {\n\t\t\tawait ack.persistenceTail;\n\t\t} catch {}\n\t\tthis._settleHarnessPersistenceAck(ack, ack.firstPersistenceError ?? executionError);\n\t}\n\n\tprivate _settleHarnessPersistenceAck(ack: HarnessPersistenceAck, error?: Error): void {\n\t\tsettleDeferred(ack, error);\n\t}\n\n\t// SCRAMJET-DIVERGENCE: harness-tool persisted-settlement (#341). Register a deferred acknowledgement\n\t// keyed by tool-call id before any invocation event can be emitted.\n\tprivate _registerHarnessPersistenceAck(toolCallId: string): HarnessPersistenceAck {\n\t\tconst ack: HarnessPersistenceAck = { ...createDeferred(), persistenceTail: Promise.resolve() };\n\t\tthis._harnessPersistenceAcks.set(toolCallId, ack);\n\t\treturn ack;\n\t}\n\n\t/** Whether compaction or branch summarization is currently running */\n\tget isCompacting(): boolean {\n\t\treturn (\n\t\t\tthis._autoCompactionAbortController !== undefined ||\n\t\t\tthis._compactionAbortController !== undefined ||\n\t\t\tthis._branchSummaryAbortController !== undefined\n\t\t);\n\t}\n\n\t/** All messages including custom types like BashExecutionMessage */\n\tget messages(): AgentMessage[] {\n\t\treturn this.agent.state.messages;\n\t}\n\n\t/** Current steering mode */\n\tget steeringMode(): \"all\" | \"one-at-a-time\" {\n\t\treturn this.agent.steeringMode;\n\t}\n\n\t/** Current follow-up mode */\n\tget followUpMode(): \"all\" | \"one-at-a-time\" {\n\t\treturn this.agent.followUpMode;\n\t}\n\n\t/** Current session file path, or undefined if sessions are disabled */\n\tget sessionFile(): string | undefined {\n\t\treturn this.sessionManager.getSessionFile();\n\t}\n\n\t/** Current session ID */\n\tget sessionId(): string {\n\t\treturn this.sessionManager.getSessionId();\n\t}\n\n\t/** Current session display name, if set */\n\tget sessionName(): string | undefined {\n\t\treturn this.sessionManager.getSessionName();\n\t}\n\n\t/** Scoped models for cycling (from --models flag) */\n\tget scopedModels(): ReadonlyArray<{ model: Model<any>; thinkingLevel?: ThinkingLevel }> {\n\t\treturn this._scopedModels;\n\t}\n\n\t/** Update scoped models for cycling */\n\tsetScopedModels(scopedModels: Array<{ model: Model<any>; thinkingLevel?: ThinkingLevel }>): void {\n\t\tthis._scopedModels = scopedModels;\n\t}\n\n\t/** File-based prompt templates */\n\tget promptTemplates(): ReadonlyArray<PromptTemplate> {\n\t\treturn this._resourceLoader.getPrompts().prompts;\n\t}\n\n\tprivate _normalizePromptSnippet(text: string | undefined): string | undefined {\n\t\tif (!text) return undefined;\n\t\tconst oneLine = text\n\t\t\t.replace(/[\\r\\n]+/g, \" \")\n\t\t\t.replace(/\\s+/g, \" \")\n\t\t\t.trim();\n\t\treturn oneLine.length > 0 ? oneLine : undefined;\n\t}\n\n\tprivate _normalizePromptGuidelines(guidelines: string[] | undefined): string[] {\n\t\tif (!guidelines || guidelines.length === 0) {\n\t\t\treturn [];\n\t\t}\n\n\t\tconst unique = new Set<string>();\n\t\tfor (const guideline of guidelines) {\n\t\t\tconst normalized = guideline.trim();\n\t\t\tif (normalized.length > 0) {\n\t\t\t\tunique.add(normalized);\n\t\t\t}\n\t\t}\n\t\treturn Array.from(unique);\n\t}\n\n\tprivate _applyRunPromptComposition(): void {\n\t\tswitch (this._runPromptComposition.kind) {\n\t\t\tcase \"generated-base\":\n\t\t\t\tthis.agent.state.systemPrompt = this._baseSystemPromptSections.slice();\n\t\t\t\tbreak;\n\t\t\tcase \"contributed-sections\":\n\t\t\t\tthis.agent.state.systemPrompt = spliceContributedSections(\n\t\t\t\t\tthis._baseSystemPromptSections,\n\t\t\t\t\tthis._runPromptComposition.sections,\n\t\t\t\t);\n\t\t\t\tbreak;\n\t\t\tcase \"authoritative-string\":\n\t\t\t\tthis.agent.state.systemPrompt = this._runPromptComposition.systemPrompt;\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t/** Rebuild `_baseSystemPromptSections` from the current resources and tool set and apply it to agent state. */\n\tprivate _rebuildSystemPrompt(toolNames: string[]): void {\n\t\tconst validToolNames = toolNames.filter((name) => this._toolRegistry.has(name));\n\t\tconst toolSnippets: Record<string, string> = {};\n\t\tconst promptGuidelines: string[] = [];\n\t\tfor (const name of validToolNames) {\n\t\t\tconst snippet = this._toolPromptSnippets.get(name);\n\t\t\tif (snippet) {\n\t\t\t\ttoolSnippets[name] = snippet;\n\t\t\t}\n\n\t\t\tconst toolGuidelines = this._toolPromptGuidelines.get(name);\n\t\t\tif (toolGuidelines) {\n\t\t\t\tpromptGuidelines.push(...toolGuidelines);\n\t\t\t}\n\t\t}\n\n\t\tconst loaderSystemPrompt = this._resourceLoader.getSystemPrompt();\n\t\tconst loaderAppendSystemPrompt = this._resourceLoader.getAppendSystemPrompt();\n\t\tconst appendSystemPrompt =\n\t\t\tloaderAppendSystemPrompt.length > 0 ? loaderAppendSystemPrompt.join(\"\\n\\n\") : undefined;\n\t\tconst loadedSkills = this._resourceLoader.getSkills().skills;\n\t\tconst loadedContextFiles = this._resourceLoader.getAgentsFiles().agentsFiles;\n\n\t\t// SCRAMJET-DIVERGENCE: pass session file path for volatile environment fact (issue 348)\n\t\tthis._baseSystemPromptOptions = {\n\t\t\tcwd: this._cwd,\n\t\t\tskills: loadedSkills,\n\t\t\tcontextFiles: loadedContextFiles,\n\t\t\tcustomPrompt: loaderSystemPrompt,\n\t\t\tappendSystemPrompt,\n\t\t\tselectedTools: validToolNames,\n\t\t\ttoolSnippets,\n\t\t\tpromptGuidelines,\n\t\t\tsessionFile: this.sessionManager.getSessionFile(),\n\t\t};\n\t\tthis._baseSystemPromptSections = buildSystemPromptSections(this._baseSystemPromptOptions);\n\t\tthis._applyRunPromptComposition();\n\t}\n\n\t// =========================================================================\n\t// Prompting\n\t// =========================================================================\n\n\t/**\n\t * Send a prompt to the agent.\n\t * - Handles extension commands (registered via pi.registerCommand) immediately, even during streaming\n\t * - Expands file-based prompt templates by default\n\t * - During streaming, queues via steer() or followUp() based on streamingBehavior option\n\t * - Validates model and API key before sending (when not streaming)\n\t * @throws Error if streaming and no streamingBehavior specified\n\t * @throws Error if no model selected or no API key available (when not streaming)\n\t */\n\tasync prompt(text: string, options?: PromptOptions): Promise<void> {\n\t\tconst expandPromptTemplates = options?.expandPromptTemplates ?? true;\n\t\tconst preflightResult = options?.preflightResult;\n\t\tlet messages: AgentMessage[] | undefined;\n\t\tlet inputSessionEntries: InputSessionEntry[] | undefined;\n\t\tlet acceptInput: (() => void) | undefined;\n\n\t\ttry {\n\t\t\t// Handle extension commands first (execute immediately, even during streaming)\n\t\t\t// Extension commands manage their own LLM interaction via pi.sendMessage()\n\t\t\tif (expandPromptTemplates && text.startsWith(\"/\")) {\n\t\t\t\tconst handled = await this._tryExecuteExtensionCommand(text);\n\t\t\t\tif (handled) {\n\t\t\t\t\t// Extension command executed, no prompt to send\n\t\t\t\t\tpreflightResult?.(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis._assertRetryNotOwningIdleAgent();\n\n\t\t\t// Emit input event for extension interception (before skill/template expansion)\n\t\t\tlet currentText = text;\n\t\t\tlet currentImages = options?.images;\n\t\t\tif (this._extensionRunner.hasHandlers(\"input\")) {\n\t\t\t\tconst inputResult = await this._extensionRunner.emitInput(\n\t\t\t\t\tcurrentText,\n\t\t\t\t\tcurrentImages,\n\t\t\t\t\toptions?.source ?? \"interactive\",\n\t\t\t\t);\n\t\t\t\tif (inputResult.action === \"handled\") {\n\t\t\t\t\tpreflightResult?.(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (inputResult.action === \"transform\") {\n\t\t\t\t\tcurrentText = inputResult.text;\n\t\t\t\t\tcurrentImages = inputResult.images ?? currentImages;\n\t\t\t\t\tinputSessionEntries = inputResult.sessionEntries;\n\t\t\t\t\tacceptInput = inputResult.onAccepted;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Expand skill commands (/skill:name args) and prompt templates (/template args)\n\t\t\tlet expandedText = currentText;\n\t\t\tif (expandPromptTemplates) {\n\t\t\t\texpandedText = this._expandSkillCommand(expandedText);\n\t\t\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\t\t\t}\n\n\t\t\t// If streaming, queue via steer() or followUp() based on option\n\t\t\tif (this.isStreaming) {\n\t\t\t\tif (!options?.streamingBehavior) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\"Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') — \" +\n\t\t\t\t\t\t\t\"deliverAs when calling dispatchUserInput() or sendUserMessage() — to queue the message.\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tif (options.streamingBehavior === \"followUp\") {\n\t\t\t\t\tawait this._queueFollowUp(expandedText, currentImages, inputSessionEntries);\n\t\t\t\t} else {\n\t\t\t\t\tawait this._queueSteer(expandedText, currentImages, inputSessionEntries);\n\t\t\t\t}\n\t\t\t\tacceptInput?.();\n\t\t\t\tpreflightResult?.(true);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Flush any pending bash messages before the new prompt\n\t\t\tthis._flushPendingBashMessages();\n\n\t\t\t// Validate model\n\t\t\tif (!this.model) {\n\t\t\t\tthrow new Error(formatNoModelSelectedMessage());\n\t\t\t}\n\n\t\t\tif (!this._modelRegistry.hasConfiguredAuth(this.model)) {\n\t\t\t\tconst isOAuth = this._modelRegistry.isUsingOAuth(this.model);\n\t\t\t\tif (isOAuth) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Authentication failed for \"${this.model.provider}\". ` +\n\t\t\t\t\t\t\t`Credentials may have expired or network is unavailable. ` +\n\t\t\t\t\t\t\t`Run '/login ${this.model.provider}' to re-authenticate.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tthrow new Error(formatNoApiKeyFoundMessage(this.model.provider));\n\t\t\t}\n\n\t\t\t// Check if we need to compact before sending (catches aborted responses)\n\t\t\tconst lastAssistant = this._findLastAssistantMessage();\n\t\t\tif (lastAssistant) {\n\t\t\t\tawait this._checkCompaction(lastAssistant, \"new_prompt\");\n\t\t\t}\n\n\t\t\t// Build messages array (custom message if any, then user message)\n\t\t\tmessages = [];\n\n\t\t\t// Add user message\n\t\t\tconst userContent: (TextContent | ImageContent)[] = [{ type: \"text\", text: expandedText }];\n\t\t\tif (currentImages) {\n\t\t\t\tuserContent.push(...currentImages);\n\t\t\t}\n\t\t\tconst userMessage: AgentMessage = {\n\t\t\t\trole: \"user\",\n\t\t\t\tcontent: userContent,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t};\n\t\t\tmessages.push(userMessage);\n\t\t\tif (inputSessionEntries?.length) {\n\t\t\t\tthis._inputSessionEntries.set(userMessage, inputSessionEntries);\n\t\t\t}\n\n\t\t\t// Inject any pending \"nextTurn\" messages as context alongside the user message\n\t\t\tfor (const msg of this._pendingNextTurnMessages) {\n\t\t\t\tmessages.push(msg);\n\t\t\t}\n\t\t\tthis._pendingNextTurnMessages = [];\n\n\t\t\tthis._runPromptComposition = { kind: \"generated-base\" };\n\n\t\t\t// Emit before_agent_start extension event\n\t\t\tconst result = await this._extensionRunner.emitBeforeAgentStart(\n\t\t\t\texpandedText,\n\t\t\t\tcurrentImages,\n\t\t\t\tthis._baseSystemPromptSections,\n\t\t\t\tthis._baseSystemPromptOptions,\n\t\t\t);\n\t\t\t// Add all custom messages from extensions\n\t\t\tif (result?.messages) {\n\t\t\t\tfor (const msg of result.messages) {\n\t\t\t\t\tmessages.push({\n\t\t\t\t\t\trole: \"custom\",\n\t\t\t\t\t\tcustomType: msg.customType,\n\t\t\t\t\t\tcontent: msg.content,\n\t\t\t\t\t\tdisplay: msg.display,\n\t\t\t\t\t\tdetails: msg.details,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Apply extension-modified system prompt, or reset to base. A string\n\t\t\t// replacement is authoritative (even an empty string): it wins for the\n\t\t\t// whole prompt and any contributed sections are dropped for this turn.\n\t\t\tif (result?.systemPrompt !== undefined) {\n\t\t\t\tthis._runPromptComposition = {\n\t\t\t\t\tkind: \"authoritative-string\",\n\t\t\t\t\tsystemPrompt: result.systemPrompt,\n\t\t\t\t};\n\t\t\t} else if (result?.systemPromptSections) {\n\t\t\t\tthis._runPromptComposition = {\n\t\t\t\t\tkind: \"contributed-sections\",\n\t\t\t\t\tsections: result.systemPromptSections.map((section) => ({ ...section })),\n\t\t\t\t};\n\t\t\t}\n\t\t\tthis._applyRunPromptComposition();\n\t\t} catch (error) {\n\t\t\tpreflightResult?.(false);\n\t\t\tthrow error;\n\t\t}\n\n\t\tif (!messages) {\n\t\t\treturn;\n\t\t}\n\n\t\tacceptInput?.();\n\t\tpreflightResult?.(true);\n\t\tawait this._runAgentPrompt(messages);\n\t}\n\n\t/**\n\t * Try to execute an extension command. Returns true if command was found and executed.\n\t */\n\tprivate async _tryExecuteExtensionCommand(text: string): Promise<boolean> {\n\t\t// Parse command name and args\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);\n\t\tconst args = spaceIndex === -1 ? \"\" : text.slice(spaceIndex + 1);\n\n\t\tconst command = this._extensionRunner.getCommand(commandName);\n\t\tif (!command) return false;\n\n\t\t// Get command context from extension runner (includes session control methods)\n\t\tconst ctx = this._extensionRunner.createCommandContext();\n\n\t\ttry {\n\t\t\tawait command.handler(args, ctx);\n\t\t\treturn true;\n\t\t} catch (err) {\n\t\t\t// Emit error via extension runner\n\t\t\tthis._extensionRunner.emitError({\n\t\t\t\textensionPath: `command:${commandName}`,\n\t\t\t\tevent: \"command\",\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t\treturn true;\n\t\t}\n\t}\n\n\t/**\n\t * Expand skill commands (/skill:name args) to their full content.\n\t * Returns the expanded text, or the original text if not a skill command or skill not found.\n\t * Emits errors via extension runner if file read fails.\n\t */\n\tprivate _expandSkillCommand(text: string): string {\n\t\tif (!text.startsWith(\"/skill:\")) return text;\n\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst skillName = spaceIndex === -1 ? text.slice(7) : text.slice(7, spaceIndex);\n\t\tconst args = spaceIndex === -1 ? \"\" : text.slice(spaceIndex + 1).trim();\n\n\t\tconst skill = this.resourceLoader.getSkills().skills.find((s) => s.name === skillName);\n\t\tif (!skill) return text; // Unknown skill, pass through\n\n\t\ttry {\n\t\t\tconst content = readFileSync(skill.filePath, \"utf-8\");\n\t\t\tconst body = stripFrontmatter(content).trim();\n\t\t\tconst skillBlock = `<skill name=\"${skill.name}\" location=\"${skill.filePath}\">\\nReferences are relative to ${skill.baseDir}.\\n\\n${body}\\n</skill>`;\n\t\t\treturn args ? `${skillBlock}\\n\\n${args}` : skillBlock;\n\t\t} catch (err) {\n\t\t\t// Emit error like extension commands do\n\t\t\tthis._extensionRunner.emitError({\n\t\t\t\textensionPath: skill.filePath,\n\t\t\t\tevent: \"skill_expansion\",\n\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t\treturn text; // Return original on error\n\t\t}\n\t}\n\n\t/**\n\t * Queue a steering message while the agent is running.\n\t * Delivered after the current assistant turn finishes executing its tool calls,\n\t * before the next LLM call.\n\t * Expands skill commands and prompt templates. Errors on extension commands.\n\t * @param images Optional image attachments to include with the message\n\t * @throws Error if text is an extension command\n\t */\n\tasync steer(text: string, images?: ImageContent[]): Promise<void> {\n\t\t// Check for extension commands (cannot be queued)\n\t\tif (text.startsWith(\"/\")) {\n\t\t\tthis._throwIfExtensionCommand(text);\n\t\t}\n\n\t\t// Expand skill commands and prompt templates\n\t\tlet expandedText = this._expandSkillCommand(text);\n\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\n\t\tawait this._queueSteer(expandedText, images);\n\t}\n\n\t/**\n\t * Queue a follow-up message to be processed after the agent finishes.\n\t * Delivered only when agent has no more tool calls or steering messages.\n\t * Expands skill commands and prompt templates. Errors on extension commands.\n\t * @param images Optional image attachments to include with the message\n\t * @throws Error if text is an extension command\n\t */\n\tasync followUp(text: string, images?: ImageContent[]): Promise<void> {\n\t\t// Check for extension commands (cannot be queued)\n\t\tif (text.startsWith(\"/\")) {\n\t\t\tthis._throwIfExtensionCommand(text);\n\t\t}\n\n\t\t// Expand skill commands and prompt templates\n\t\tlet expandedText = this._expandSkillCommand(text);\n\t\texpandedText = expandPromptTemplate(expandedText, [...this.promptTemplates]);\n\n\t\tawait this._queueFollowUp(expandedText, images);\n\t}\n\n\t/**\n\t * Internal: Queue a steering message (already expanded, no extension command check).\n\t */\n\tprivate async _queueSteer(\n\t\ttext: string,\n\t\timages?: ImageContent[],\n\t\tsessionEntries?: InputSessionEntry[],\n\t): Promise<void> {\n\t\tthis._steeringMessages.push(text);\n\t\tthis._emitQueueUpdate();\n\t\tconst content: (TextContent | ImageContent)[] = [{ type: \"text\", text }];\n\t\tif (images) {\n\t\t\tcontent.push(...images);\n\t\t}\n\t\tconst message: AgentMessage = {\n\t\t\trole: \"user\",\n\t\t\tcontent,\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\t\tif (sessionEntries?.length) {\n\t\t\tthis._inputSessionEntries.set(message, sessionEntries);\n\t\t}\n\t\tthis.agent.steer(message);\n\t}\n\n\t/**\n\t * Internal: Queue a follow-up message (already expanded, no extension command check).\n\t */\n\tprivate async _queueFollowUp(\n\t\ttext: string,\n\t\timages?: ImageContent[],\n\t\tsessionEntries?: InputSessionEntry[],\n\t): Promise<void> {\n\t\tthis._followUpMessages.push(text);\n\t\tthis._emitQueueUpdate();\n\t\tconst content: (TextContent | ImageContent)[] = [{ type: \"text\", text }];\n\t\tif (images) {\n\t\t\tcontent.push(...images);\n\t\t}\n\t\tconst message: AgentMessage = {\n\t\t\trole: \"user\",\n\t\t\tcontent,\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\t\tif (sessionEntries?.length) {\n\t\t\tthis._inputSessionEntries.set(message, sessionEntries);\n\t\t}\n\t\tthis.agent.followUp(message);\n\t}\n\n\t/**\n\t * Throw an error if the text is an extension command.\n\t */\n\tprivate _throwIfExtensionCommand(text: string): void {\n\t\tconst spaceIndex = text.indexOf(\" \");\n\t\tconst commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);\n\t\tconst command = this._extensionRunner.getCommand(commandName);\n\n\t\tif (command) {\n\t\t\tthrow new Error(\n\t\t\t\t`Extension command \"/${commandName}\" cannot be queued. Use prompt() or execute the command when not streaming.`,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Send a custom message to the session. Creates a CustomMessageEntry.\n\t *\n\t * Handles three cases:\n\t * - Streaming: queues message, processed when loop pulls from queue\n\t * - Not streaming + triggerTurn: appends to state/session, starts new turn\n\t * - Not streaming + no trigger: appends to state/session, no turn\n\t *\n\t * @param message Custom message with customType, content, display, details\n\t * @param options.triggerTurn If true and not streaming, triggers a new LLM turn\n\t * @param options.deliverAs Delivery mode: \"steer\", \"followUp\", or \"nextTurn\"\n\t */\n\tasync sendCustomMessage<T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: SendMessageDeliverAs },\n\t): Promise<void> {\n\t\tconst appMessage = {\n\t\t\trole: \"custom\" as const,\n\t\t\tcustomType: message.customType,\n\t\t\tcontent: message.content,\n\t\t\tdisplay: message.display,\n\t\t\tdetails: message.details,\n\t\t\ttimestamp: Date.now(),\n\t\t} satisfies CustomMessage<T>;\n\t\tif (options?.deliverAs === \"nextTurn\") {\n\t\t\tthis._pendingNextTurnMessages.push(appMessage);\n\t\t} else if (this.isStreaming) {\n\t\t\tif (options?.deliverAs === \"followUp\") {\n\t\t\t\tthis.agent.followUp(appMessage);\n\t\t\t} else {\n\t\t\t\tthis.agent.steer(appMessage);\n\t\t\t}\n\t\t} else if (options?.triggerTurn) {\n\t\t\tawait this._runAgentPrompt(appMessage);\n\t\t} else {\n\t\t\tthis.agent.state.messages.push(appMessage);\n\t\t\tthis.sessionManager.appendCustomMessageEntry(\n\t\t\t\tmessage.customType,\n\t\t\t\tmessage.content,\n\t\t\t\tmessage.display,\n\t\t\t\tmessage.details,\n\t\t\t);\n\t\t\tthis._emit({ type: \"message_start\", message: appMessage });\n\t\t\tthis._emit({ type: \"message_end\", message: appMessage });\n\t\t}\n\t}\n\n\t/**\n\t * Send a user message to the agent. Always triggers a turn.\n\t * When the agent is streaming, use deliverAs to specify how to queue the message.\n\t *\n\t * @param content User message content (string or content array)\n\t * @param options.deliverAs Delivery mode when streaming: \"steer\" or \"followUp\"\n\t */\n\tasync sendUserMessage(\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: DeliverAs },\n\t): Promise<void> {\n\t\t// Normalize content to text string + optional images\n\t\tlet text: string;\n\t\tlet images: ImageContent[] | undefined;\n\n\t\tif (typeof content === \"string\") {\n\t\t\ttext = content;\n\t\t} else {\n\t\t\tconst textParts: string[] = [];\n\t\t\timages = [];\n\t\t\tfor (const part of content) {\n\t\t\t\tif (part.type === \"text\") {\n\t\t\t\t\ttextParts.push(part.text);\n\t\t\t\t} else {\n\t\t\t\t\timages.push(part);\n\t\t\t\t}\n\t\t\t}\n\t\t\ttext = textParts.join(\"\\n\");\n\t\t\tif (images.length === 0) images = undefined;\n\t\t}\n\n\t\t// Use prompt() with expandPromptTemplates: false to skip command handling and template expansion\n\t\tawait this.prompt(text, {\n\t\t\texpandPromptTemplates: false,\n\t\t\tstreamingBehavior: options?.deliverAs,\n\t\t\timages,\n\t\t\tsource: \"extension\",\n\t\t});\n\t}\n\n\t/**\n\t * Dispatch input through the same pipeline as typed editor input:\n\t * extension-registered slash commands, prompt templates, and skills all\n\t * resolve as if the user had typed the text (source: \"extension\").\n\t *\n\t * @param options.deliverAs Delivery mode when streaming: \"steer\" or \"followUp\"\n\t * @throws Error if streaming and no deliverAs specified, unless the input is\n\t * handled before reaching the prompt queue (extension commands execute\n\t * immediately; input consumed by an `input` handler returns normally)\n\t */\n\tasync dispatchUserInput(input: string, options?: DispatchUserInputOptions): Promise<void> {\n\t\tawait this.prompt(input, {\n\t\t\texpandPromptTemplates: true,\n\t\t\tstreamingBehavior: options?.deliverAs,\n\t\t\tsource: \"extension\",\n\t\t});\n\t}\n\n\t/**\n\t * Clear all queued messages and return them.\n\t * Useful for restoring to editor when user aborts.\n\t * @returns Object with steering and followUp arrays\n\t */\n\tclearQueue(): { steering: string[]; followUp: string[] } {\n\t\tconst steering = [...this._steeringMessages];\n\t\tconst followUp = [...this._followUpMessages];\n\t\tthis._steeringMessages = [];\n\t\tthis._followUpMessages = [];\n\t\tthis.agent.clearAllQueues();\n\t\tthis._emitQueueUpdate();\n\t\treturn { steering, followUp };\n\t}\n\n\t/** Number of pending messages (includes both steering and follow-up) */\n\tget pendingMessageCount(): number {\n\t\treturn this._steeringMessages.length + this._followUpMessages.length;\n\t}\n\n\t/** Get pending steering messages (read-only) */\n\tgetSteeringMessages(): readonly string[] {\n\t\treturn this._steeringMessages;\n\t}\n\n\t/** Get pending follow-up messages (read-only) */\n\tgetFollowUpMessages(): readonly string[] {\n\t\treturn this._followUpMessages;\n\t}\n\n\tget resourceLoader(): ResourceLoader {\n\t\treturn this._resourceLoader;\n\t}\n\n\t/**\n\t * Abort current operation and wait for agent to become idle.\n\t */\n\tasync abort(): Promise<void> {\n\t\tthis.abortRetry();\n\t\tthis._resetOutputThroughput();\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\t}\n\n\t// =========================================================================\n\t// Model Management\n\t// =========================================================================\n\n\tprivate async _emitModelSelect(\n\t\tnextModel: Model<any>,\n\t\tpreviousModel: Model<any> | undefined,\n\t\tsource: \"set\" | \"cycle\" | \"restore\",\n\t): Promise<void> {\n\t\tif (modelsAreEqual(previousModel, nextModel)) return;\n\t\tawait this._extensionRunner.emit({\n\t\t\ttype: \"model_select\",\n\t\t\tmodel: nextModel,\n\t\t\tpreviousModel,\n\t\t\tsource,\n\t\t});\n\t}\n\n\t/**\n\t * Set model directly.\n\t * Validates that auth is configured, saves to session and settings.\n\t * @throws Error if no auth is configured for the model\n\t */\n\tasync setModel(model: Model<any>): Promise<void> {\n\t\tif (!this._modelRegistry.hasConfiguredAuth(model)) {\n\t\t\tthrow new Error(`No API key for ${model.provider}/${model.id}`);\n\t\t}\n\n\t\tconst previousModel = this.model;\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch();\n\t\tthis.agent.state.model = model;\n\t\tthis.sessionManager.appendModelChange(model.provider, model.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(model.provider, model.id);\n\n\t\t// Re-clamp thinking level for new model's capabilities\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(model, previousModel, \"set\");\n\t}\n\n\t/**\n\t * Cycle to next/previous model.\n\t * Uses scoped models (from --models flag) if available, otherwise all available models.\n\t * @param direction - \"forward\" (default) or \"backward\"\n\t * @returns The new model info, or undefined if only one model available\n\t */\n\tasync cycleModel(direction: \"forward\" | \"backward\" = \"forward\"): Promise<ModelCycleResult | undefined> {\n\t\tif (this._scopedModels.length > 0) {\n\t\t\treturn this._cycleScopedModel(direction);\n\t\t}\n\t\treturn this._cycleAvailableModel(direction);\n\t}\n\n\tprivate async _cycleScopedModel(direction: \"forward\" | \"backward\"): Promise<ModelCycleResult | undefined> {\n\t\tconst scopedModels = this._scopedModels.filter((scoped) => this._modelRegistry.hasConfiguredAuth(scoped.model));\n\t\tif (scopedModels.length <= 1) return undefined;\n\n\t\tconst currentModel = this.model;\n\t\tlet currentIndex = scopedModels.findIndex((sm) => modelsAreEqual(sm.model, currentModel));\n\n\t\tif (currentIndex === -1) currentIndex = 0;\n\t\tconst len = scopedModels.length;\n\t\tconst nextIndex = direction === \"forward\" ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;\n\t\tconst next = scopedModels[nextIndex];\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch(next.thinkingLevel);\n\n\t\t// Apply model\n\t\tthis.agent.state.model = next.model;\n\t\tthis.sessionManager.appendModelChange(next.model.provider, next.model.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(next.model.provider, next.model.id);\n\n\t\t// Apply thinking level.\n\t\t// - Explicit scoped model thinking level overrides current session level\n\t\t// - Undefined scoped model thinking level inherits the current session preference\n\t\t// setThinkingLevel clamps to model capabilities.\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(next.model, currentModel, \"cycle\");\n\n\t\treturn { model: next.model, thinkingLevel: this.thinkingLevel, isScoped: true };\n\t}\n\n\tprivate async _cycleAvailableModel(direction: \"forward\" | \"backward\"): Promise<ModelCycleResult | undefined> {\n\t\tconst availableModels = await this._modelRegistry.getAvailable();\n\t\tif (availableModels.length <= 1) return undefined;\n\n\t\tconst currentModel = this.model;\n\t\tlet currentIndex = availableModels.findIndex((m) => modelsAreEqual(m, currentModel));\n\n\t\tif (currentIndex === -1) currentIndex = 0;\n\t\tconst len = availableModels.length;\n\t\tconst nextIndex = direction === \"forward\" ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;\n\t\tconst nextModel = availableModels[nextIndex];\n\n\t\tconst thinkingLevel = this._getThinkingLevelForModelSwitch();\n\t\tthis.agent.state.model = nextModel;\n\t\tthis.sessionManager.appendModelChange(nextModel.provider, nextModel.id);\n\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t// Re-clamp thinking level for new model's capabilities\n\t\tthis.setThinkingLevel(thinkingLevel);\n\n\t\tawait this._emitModelSelect(nextModel, currentModel, \"cycle\");\n\n\t\treturn { model: nextModel, thinkingLevel: this.thinkingLevel, isScoped: false };\n\t}\n\n\t// =========================================================================\n\t// Thinking Level Management\n\t// =========================================================================\n\n\t/**\n\t * Set thinking level.\n\t * Clamps to model capabilities based on available thinking levels.\n\t * Saves to session and settings only if the level actually changes.\n\t */\n\tsetThinkingLevel(level: ThinkingLevel): void {\n\t\tconst availableLevels = this.getAvailableThinkingLevels();\n\t\tconst effectiveLevel = availableLevels.includes(level) ? level : this._clampThinkingLevel(level, availableLevels);\n\n\t\t// Only persist if actually changing\n\t\tconst previousLevel = this.agent.state.thinkingLevel;\n\t\tconst isChanging = effectiveLevel !== previousLevel;\n\n\t\tthis.agent.state.thinkingLevel = effectiveLevel;\n\n\t\tif (isChanging) {\n\t\t\tthis.sessionManager.appendThinkingLevelChange(effectiveLevel);\n\t\t\tif (this.supportsThinking() || effectiveLevel !== \"off\") {\n\t\t\t\tthis.settingsManager.setDefaultThinkingLevel(effectiveLevel);\n\t\t\t}\n\t\t\tthis._emit({ type: \"thinking_level_changed\", level: effectiveLevel });\n\t\t\tvoid this._extensionRunner.emit({\n\t\t\t\ttype: \"thinking_level_select\",\n\t\t\t\tlevel: effectiveLevel,\n\t\t\t\tpreviousLevel,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Cycle to next thinking level.\n\t * @returns New level, or undefined if model doesn't support thinking\n\t */\n\tcycleThinkingLevel(): ThinkingLevel | undefined {\n\t\tif (!this.supportsThinking()) return undefined;\n\n\t\tconst levels = this.getAvailableThinkingLevels();\n\t\tconst currentIndex = levels.indexOf(this.thinkingLevel);\n\t\tconst nextIndex = (currentIndex + 1) % levels.length;\n\t\tconst nextLevel = levels[nextIndex];\n\n\t\tthis.setThinkingLevel(nextLevel);\n\t\treturn nextLevel;\n\t}\n\n\t/**\n\t * Get available thinking levels for current model.\n\t * The provider will clamp to what the specific model supports internally.\n\t */\n\tgetAvailableThinkingLevels(): ThinkingLevel[] {\n\t\tif (!this.model) return THINKING_LEVELS;\n\t\treturn getSupportedThinkingLevels(this.model) as ThinkingLevel[];\n\t}\n\n\t/**\n\t * Check if current model supports thinking/reasoning.\n\t */\n\tsupportsThinking(): boolean {\n\t\treturn !!this.model?.reasoning;\n\t}\n\n\tprivate _getThinkingLevelForModelSwitch(explicitLevel?: ThinkingLevel): ThinkingLevel {\n\t\tif (explicitLevel !== undefined) {\n\t\t\treturn explicitLevel;\n\t\t}\n\t\tif (!this.supportsThinking()) {\n\t\t\treturn this.settingsManager.getDefaultThinkingLevel() ?? DEFAULT_THINKING_LEVEL;\n\t\t}\n\t\treturn this.thinkingLevel;\n\t}\n\n\tprivate _clampThinkingLevel(level: ThinkingLevel, _availableLevels: ThinkingLevel[]): ThinkingLevel {\n\t\treturn this.model ? (clampThinkingLevel(this.model, level) as ThinkingLevel) : \"off\";\n\t}\n\n\t// =========================================================================\n\t// Queue Mode Management\n\t// =========================================================================\n\n\t/**\n\t * Set steering message mode.\n\t * Saves to settings.\n\t */\n\tsetSteeringMode(mode: \"all\" | \"one-at-a-time\"): void {\n\t\tthis.agent.steeringMode = mode;\n\t\tthis.settingsManager.setSteeringMode(mode);\n\t}\n\n\t/**\n\t * Set follow-up message mode.\n\t * Saves to settings.\n\t */\n\tsetFollowUpMode(mode: \"all\" | \"one-at-a-time\"): void {\n\t\tthis.agent.followUpMode = mode;\n\t\tthis.settingsManager.setFollowUpMode(mode);\n\t}\n\n\t// =========================================================================\n\t// Compaction\n\t// =========================================================================\n\n\t/**\n\t * Manually compact the session context.\n\t * Aborts current agent operation first.\n\t * @param customInstructions Optional instructions for the compaction summary\n\t */\n\tasync compact(customInstructions?: string): Promise<CompactionResult> {\n\t\t// SCRAMJET-DIVERGENCE: acquire one owner before awaits and drain terminal persistence (#598).\n\t\tif (this._disposed) throw new Error(\"Cannot compact a disposed session.\");\n\t\tif (this.isCompacting)\n\t\t\tthrow new Error(\"Compaction or branch summarization is already in progress. Retry after it settles.\");\n\t\tif (this.agent.hasActiveTransientHarnessTools) {\n\t\t\tthrow new Error(\n\t\t\t\t\"A harness tool is still executing outside an Agent run. Retry compaction after its persisted settlement.\",\n\t\t\t);\n\t\t}\n\t\tconst controller = new AbortController();\n\t\tthis._compactionAbortController = controller;\n\t\tconst drain: { firstError?: Error } = {};\n\t\tthis._manualCompactionDrain = drain;\n\t\tconst runs = [...this._unsettledAgentRuns];\n\t\ttry {\n\t\t\tthis._resetOutputThroughput();\n\t\t\tthis._disconnectFromAgent();\n\t\t\tthis._unsubscribeAgent = this.agent.subscribe((event) => this._handleAgentEvent(event));\n\t\t\tthis._emit({ type: \"compaction_start\", reason: \"manual\" });\n\t\t\tawait this.abort();\n\t\t\tawait this._agentEventQueue;\n\t\t\tif (drain.firstError) throw drain.firstError;\n\t\t\tif (this._disposed) throw new Error(\"Session disposed during compaction.\");\n\t\t\tif (controller.signal.aborted) throw new Error(\"Compaction cancelled\");\n\t\t\tif (!this.model) {\n\t\t\t\tthrow new Error(formatNoModelSelectedMessage());\n\t\t\t}\n\n\t\t\tconst { apiKey, headers } = await this._getRequiredRequestAuth(this.model);\n\n\t\t\tconst pathEntries = this.sessionManager.getBranch();\n\t\t\tconst settings = this.settingsManager.getCompactionSettings();\n\n\t\t\tconst preparation = prepareCompaction(pathEntries, settings);\n\t\t\tif (!preparation) {\n\t\t\t\t// Check why we can't compact\n\t\t\t\tconst lastEntry = pathEntries[pathEntries.length - 1];\n\t\t\t\tif (lastEntry?.type === \"compaction\") {\n\t\t\t\t\tthrow new Error(\"Already compacted\");\n\t\t\t\t}\n\t\t\t\tthrow new Error(\"Nothing to compact (session too small)\");\n\t\t\t}\n\n\t\t\tlet extensionCompaction: CompactionResult | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_compact\")) {\n\t\t\t\tconst result = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_compact\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tbranchEntries: pathEntries,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t})) as SessionBeforeCompactResult | undefined;\n\n\t\t\t\tif (result?.cancel) {\n\t\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t\t}\n\n\t\t\t\tif (result?.compaction) {\n\t\t\t\t\textensionCompaction = result.compaction;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet summary: string;\n\t\t\tlet firstKeptEntryId: string;\n\t\t\tlet tokensBefore: number;\n\t\t\tlet details: unknown;\n\n\t\t\tif (extensionCompaction) {\n\t\t\t\t// Extension provided compaction content\n\t\t\t\tsummary = extensionCompaction.summary;\n\t\t\t\tfirstKeptEntryId = extensionCompaction.firstKeptEntryId;\n\t\t\t\ttokensBefore = extensionCompaction.tokensBefore;\n\t\t\t\tdetails = extensionCompaction.details;\n\t\t\t} else {\n\t\t\t\t// Generate compaction result\n\t\t\t\tconst result = await compact(\n\t\t\t\t\tpreparation,\n\t\t\t\t\tthis.model,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\tcontroller.signal,\n\t\t\t\t\tthis.thinkingLevel,\n\t\t\t\t);\n\t\t\t\tsummary = result.summary;\n\t\t\t\tfirstKeptEntryId = result.firstKeptEntryId;\n\t\t\t\ttokensBefore = result.tokensBefore;\n\t\t\t\tdetails = result.details;\n\t\t\t}\n\n\t\t\tif (this._disposed) throw new Error(\"Session disposed during compaction.\");\n\t\t\tif (controller.signal.aborted) {\n\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t}\n\n\t\t\tthis.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);\n\t\t\tconst newEntries = this.sessionManager.getEntries();\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Get the saved compaction entry for the extension event\n\t\t\tconst savedCompactionEntry = newEntries.find((e) => e.type === \"compaction\" && e.summary === summary) as\n\t\t\t\t| CompactionEntry\n\t\t\t\t| undefined;\n\n\t\t\tif (this._extensionRunner && savedCompactionEntry) {\n\t\t\t\tawait this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_compact\",\n\t\t\t\t\tcompactionEntry: savedCompactionEntry,\n\t\t\t\t\tfromExtension,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst compactionResult = {\n\t\t\t\tsummary,\n\t\t\t\tfirstKeptEntryId,\n\t\t\t\ttokensBefore,\n\t\t\t\tdetails,\n\t\t\t};\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason: \"manual\",\n\t\t\t\tresult: compactionResult,\n\t\t\t\taborted: false,\n\t\t\t\twillRetry: false,\n\t\t\t});\n\t\t\treturn compactionResult;\n\t\t} catch (error) {\n\t\t\tconst failure = drain.firstError ?? error;\n\t\t\tconst settlementError = failure instanceof Error ? failure : new Error(String(failure));\n\t\t\tthis._releaseRetryForFailedRun(settlementError);\n\t\t\tfor (const run of runs) this._completeAgentRun(run, settlementError);\n\t\t\tconst message = settlementError.message;\n\t\t\tconst aborted = message === \"Compaction cancelled\" || settlementError.name === \"AbortError\";\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason: \"manual\",\n\t\t\t\tresult: undefined,\n\t\t\t\taborted,\n\t\t\t\twillRetry: false,\n\t\t\t\terrorMessage: aborted ? undefined : `Compaction failed: ${message}`,\n\t\t\t});\n\t\t\tthrow failure;\n\t\t} finally {\n\t\t\tthis._disconnectFromAgent();\n\t\t\tthis._manualCompactionDrain = undefined;\n\t\t\tthis._compactionAbortController = undefined;\n\t\t\tthis._reconnectToAgent();\n\t\t}\n\t}\n\n\t/**\n\t * Cancel in-progress compaction (manual or auto).\n\t */\n\tabortCompaction(): void {\n\t\tthis._compactionAbortController?.abort();\n\t\tthis._autoCompactionAbortController?.abort();\n\t}\n\n\t/**\n\t * Cancel in-progress branch summarization.\n\t */\n\tabortBranchSummary(): void {\n\t\tthis._branchSummaryAbortController?.abort();\n\t}\n\n\t/**\n\t * Check if compaction is needed and run it.\n\t * Called after agent_end and before prompt submission.\n\t *\n\t * Two cases:\n\t * 1. Overflow: LLM returned context overflow error, remove error message from agent state, compact, auto-retry\n\t * 2. Threshold: Context over threshold, compact, NO auto-retry (user continues manually)\n\t *\n\t * @param assistantMessage The assistant message to check\n\t */\n\tprivate async _checkCompaction(\n\t\tassistantMessage: AssistantMessage,\n\t\ttrigger: \"settlement\" | \"new_prompt\" = \"settlement\",\n\t): Promise<void> {\n\t\tif (this._manualCompactionDrain) return;\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\tif (!settings.enabled) return;\n\n\t\t// SCRAMJET-DIVERGENCE: New prompts maintain context independently of failed-turn recovery.\n\t\tif (trigger === \"settlement\" && blocksFailureRecovery(assistantMessage)) return;\n\t\tif (trigger === \"new_prompt\") {\n\t\t\tconst boundary = getLatestCompactionEntry(this.sessionManager.getBranch());\n\t\t\tconst messages = this.agent.state.messages;\n\t\t\tconst reverseIndex = [...messages]\n\t\t\t\t.reverse()\n\t\t\t\t.findIndex(\n\t\t\t\t\t(message) =>\n\t\t\t\t\t\tmessage.role === \"assistant\" &&\n\t\t\t\t\t\tmessage.origin !== \"harness\" &&\n\t\t\t\t\t\tmessage.stopReason !== \"error\" &&\n\t\t\t\t\t\tmessage.stopReason !== \"aborted\" &&\n\t\t\t\t\t\tmessage.provider === this.model?.provider &&\n\t\t\t\t\t\tmessage.model === this.model?.id &&\n\t\t\t\t\t\tNumber.isFinite(calculateContextTokens(message.usage)) &&\n\t\t\t\t\t\tcalculateContextTokens(message.usage) >= 0 &&\n\t\t\t\t\t\t(!boundary || message.timestamp > new Date(boundary.timestamp).getTime()) &&\n\t\t\t\t\t\t[message.usage.input, message.usage.output, message.usage.cacheRead, message.usage.cacheWrite].every(\n\t\t\t\t\t\t\t(n) => Number.isFinite(n) && n >= 0,\n\t\t\t\t\t\t),\n\t\t\t\t);\n\t\t\tif (reverseIndex < 0) return;\n\t\t\tconst sourceIndex = messages.length - reverseIndex - 1;\n\t\t\tconst source = messages[sourceIndex] as AssistantMessage;\n\t\t\tconst tokens =\n\t\t\t\tcalculateContextTokens(source.usage) +\n\t\t\t\tmessages.slice(sourceIndex + 1).reduce((total, message) => total + estimateTokens(message), 0);\n\t\t\tconst window = this.model?.contextWindow ?? 0;\n\t\t\tif (\n\t\t\t\tshouldCompact(tokens, window, settings) ||\n\t\t\t\t(window > settings.reserveTokens && tokens > (this.model?.maxInputTokens ?? Infinity))\n\t\t\t)\n\t\t\t\tawait this._runAutoCompaction(\"threshold\", false);\n\t\t\treturn;\n\t\t}\n\n\t\t// SCRAMJET-DIVERGENCE: Total context and independent input limits are separate constraints.\n\t\tconst contextWindow = this.model?.contextWindow ?? 0;\n\n\t\t// Skip overflow check if the message came from a different model.\n\t\t// This handles the case where user switched from a smaller-context model (e.g. opus)\n\t\t// to a larger-context model (e.g. codex) - the overflow error from the old model\n\t\t// shouldn't trigger compaction for the new model.\n\t\tconst sameModel =\n\t\t\tthis.model && assistantMessage.provider === this.model.provider && assistantMessage.model === this.model.id;\n\n\t\t// Skip compaction checks if this assistant message is older than the latest\n\t\t// compaction boundary. This prevents a stale pre-compaction usage/error\n\t\t// from retriggering compaction on the first prompt after compaction.\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.getBranch());\n\t\tconst assistantIsFromBeforeCompaction =\n\t\t\tcompactionEntry !== null && assistantMessage.timestamp <= new Date(compactionEntry.timestamp).getTime();\n\t\tif (assistantIsFromBeforeCompaction) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Case 1: Overflow - LLM returned context overflow error\n\t\tif (\n\t\t\tsameModel &&\n\t\t\tisContextOverflow(assistantMessage, Math.min(contextWindow, this.model?.maxInputTokens ?? Infinity))\n\t\t) {\n\t\t\tif (this._overflowRecoveryAttempted) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason: \"overflow\",\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\"Context overflow recovery failed after one compact-and-retry attempt. Try reducing context or switching to a larger-context model.\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis._overflowRecoveryAttempted = true;\n\t\t\t// Remove the error message from agent state (it IS saved to session for history,\n\t\t\t// but we don't want it in context for the retry)\n\t\t\tconst messages = this.agent.state.messages;\n\t\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t\t}\n\t\t\tawait this._runAutoCompaction(\"overflow\", true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Case 2: Threshold - context is getting large\n\t\t// For error messages (no usage data), estimate from last successful response.\n\t\t// This ensures sessions that hit persistent API errors (e.g. 529) can still compact.\n\t\tlet contextTokens: number;\n\t\tif (assistantMessage.stopReason === \"error\") {\n\t\t\tconst messages = this.agent.state.messages;\n\t\t\tconst estimate = estimateContextTokens(messages);\n\t\t\tif (estimate.lastUsageIndex === null) return; // No usage data at all\n\t\t\t// Verify the usage source is post-compaction. Kept pre-compaction messages\n\t\t\t// have stale usage reflecting the old (larger) context and would falsely\n\t\t\t// trigger compaction right after one just finished.\n\t\t\tconst usageMsg = messages[estimate.lastUsageIndex];\n\t\t\tif (\n\t\t\t\tcompactionEntry &&\n\t\t\t\tusageMsg.role === \"assistant\" &&\n\t\t\t\t(usageMsg as AssistantMessage).timestamp <= new Date(compactionEntry.timestamp).getTime()\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcontextTokens = estimate.tokens;\n\t\t} else {\n\t\t\tcontextTokens = calculateContextTokens(assistantMessage.usage);\n\t\t}\n\t\tif (\n\t\t\tshouldCompact(contextTokens, contextWindow, settings) ||\n\t\t\t(contextWindow > settings.reserveTokens && contextTokens > (this.model?.maxInputTokens ?? Infinity))\n\t\t) {\n\t\t\tawait this._runAutoCompaction(\"threshold\", false);\n\t\t}\n\t}\n\n\t/**\n\t * Internal: Run auto-compaction with events.\n\t */\n\tprivate async _runAutoCompaction(reason: \"overflow\" | \"threshold\", willRetry: boolean): Promise<void> {\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\n\t\t// SCRAMJET-DIVERGENCE: reentrant subscribers must observe compaction ownership (#598).\n\t\tthis._autoCompactionAbortController = new AbortController();\n\n\t\ttry {\n\t\t\tthis._emit({ type: \"compaction_start\", reason });\n\t\t\tif (!this.model) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst authResult = await this._modelRegistry.getApiKeyAndHeaders(this.model);\n\t\t\tif (!authResult.ok || !authResult.apiKey) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst { apiKey, headers } = authResult;\n\n\t\t\tconst pathEntries = this.sessionManager.getBranch();\n\n\t\t\tconst preparation = prepareCompaction(pathEntries, settings);\n\t\t\tif (!preparation) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: false,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet extensionCompaction: CompactionResult | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_compact\")) {\n\t\t\t\tconst extensionResult = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_compact\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tbranchEntries: pathEntries,\n\t\t\t\t\tcustomInstructions: undefined,\n\t\t\t\t\tsignal: this._autoCompactionAbortController.signal,\n\t\t\t\t})) as SessionBeforeCompactResult | undefined;\n\n\t\t\t\tif (extensionResult?.cancel) {\n\t\t\t\t\tthis._emit({\n\t\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\t\treason,\n\t\t\t\t\t\tresult: undefined,\n\t\t\t\t\t\taborted: true,\n\t\t\t\t\t\twillRetry: false,\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (extensionResult?.compaction) {\n\t\t\t\t\textensionCompaction = extensionResult.compaction;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet summary: string;\n\t\t\tlet firstKeptEntryId: string;\n\t\t\tlet tokensBefore: number;\n\t\t\tlet details: unknown;\n\n\t\t\tif (extensionCompaction) {\n\t\t\t\t// Extension provided compaction content\n\t\t\t\tsummary = extensionCompaction.summary;\n\t\t\t\tfirstKeptEntryId = extensionCompaction.firstKeptEntryId;\n\t\t\t\ttokensBefore = extensionCompaction.tokensBefore;\n\t\t\t\tdetails = extensionCompaction.details;\n\t\t\t} else {\n\t\t\t\t// Generate compaction result\n\t\t\t\tconst compactResult = await compact(\n\t\t\t\t\tpreparation,\n\t\t\t\t\tthis.model,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tundefined,\n\t\t\t\t\tthis._autoCompactionAbortController.signal,\n\t\t\t\t\tthis.thinkingLevel,\n\t\t\t\t);\n\t\t\t\tsummary = compactResult.summary;\n\t\t\t\tfirstKeptEntryId = compactResult.firstKeptEntryId;\n\t\t\t\ttokensBefore = compactResult.tokensBefore;\n\t\t\t\tdetails = compactResult.details;\n\t\t\t}\n\n\t\t\tif (this._autoCompactionAbortController.signal.aborted) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"compaction_end\",\n\t\t\t\t\treason,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\taborted: true,\n\t\t\t\t\twillRetry: false,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);\n\t\t\tconst newEntries = this.sessionManager.getEntries();\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Get the saved compaction entry for the extension event\n\t\t\tconst savedCompactionEntry = newEntries.find((e) => e.type === \"compaction\" && e.summary === summary) as\n\t\t\t\t| CompactionEntry\n\t\t\t\t| undefined;\n\n\t\t\tif (this._extensionRunner && savedCompactionEntry) {\n\t\t\t\tawait this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_compact\",\n\t\t\t\t\tcompactionEntry: savedCompactionEntry,\n\t\t\t\t\tfromExtension,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst result: CompactionResult = {\n\t\t\t\tsummary,\n\t\t\t\tfirstKeptEntryId,\n\t\t\t\ttokensBefore,\n\t\t\t\tdetails,\n\t\t\t};\n\t\t\tthis._emit({ type: \"compaction_end\", reason, result, aborted: false, willRetry });\n\n\t\t\tif (willRetry) {\n\t\t\t\tconst messages = this.agent.state.messages;\n\t\t\t\tconst lastMsg = messages[messages.length - 1];\n\t\t\t\tif (lastMsg?.role === \"assistant\" && (lastMsg as AssistantMessage).stopReason === \"error\") {\n\t\t\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t\t\t}\n\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.agent.continue().catch(() => {});\n\t\t\t\t}, 100);\n\t\t\t} else if (this.agent.hasQueuedMessages()) {\n\t\t\t\t// Auto-compaction can complete while follow-up/steering/custom messages are waiting.\n\t\t\t\t// Kick the loop so queued messages are actually delivered.\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.agent.continue().catch(() => {});\n\t\t\t\t}, 100);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconst errorMessage = error instanceof Error ? error.message : \"compaction failed\";\n\t\t\tthis._emit({\n\t\t\t\ttype: \"compaction_end\",\n\t\t\t\treason,\n\t\t\t\tresult: undefined,\n\t\t\t\taborted: false,\n\t\t\t\twillRetry: false,\n\t\t\t\terrorMessage:\n\t\t\t\t\treason === \"overflow\"\n\t\t\t\t\t\t? `Context overflow recovery failed: ${errorMessage}`\n\t\t\t\t\t\t: `Auto-compaction failed: ${errorMessage}`,\n\t\t\t});\n\t\t} finally {\n\t\t\tthis._autoCompactionAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Toggle auto-compaction setting.\n\t */\n\tsetAutoCompactionEnabled(enabled: boolean): void {\n\t\tthis.settingsManager.setCompactionEnabled(enabled);\n\t}\n\n\t/** Whether auto-compaction is enabled */\n\tget autoCompactionEnabled(): boolean {\n\t\treturn this.settingsManager.getCompactionEnabled();\n\t}\n\n\tasync bindExtensions(bindings: ExtensionBindings): Promise<void> {\n\t\tif (bindings.uiContext !== undefined) {\n\t\t\tthis._extensionUIContext = bindings.uiContext;\n\t\t}\n\t\tif (bindings.commandContextActions !== undefined) {\n\t\t\tthis._extensionCommandContextActions = bindings.commandContextActions;\n\t\t}\n\t\tif (bindings.shutdownHandler !== undefined) {\n\t\t\tthis._extensionShutdownHandler = bindings.shutdownHandler;\n\t\t}\n\t\tif (bindings.onError !== undefined) {\n\t\t\tthis._extensionErrorListener = bindings.onError;\n\t\t}\n\n\t\tthis._applyExtensionBindings(this._extensionRunner);\n\t\tawait this._extensionRunner.emit(this._sessionStartEvent);\n\t\tawait this.extendResourcesFromExtensions(this._sessionStartEvent.reason === \"reload\" ? \"reload\" : \"startup\");\n\t}\n\n\tprivate async extendResourcesFromExtensions(reason: \"startup\" | \"reload\"): Promise<void> {\n\t\tif (!this._extensionRunner.hasHandlers(\"resources_discover\")) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { skillPaths, promptPaths, themePaths } = await this._extensionRunner.emitResourcesDiscover(\n\t\t\tthis._cwd,\n\t\t\treason,\n\t\t);\n\n\t\tif (skillPaths.length === 0 && promptPaths.length === 0 && themePaths.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst extensionPaths: ResourceExtensionPaths = {\n\t\t\tskillPaths: this.buildExtensionResourcePaths(skillPaths),\n\t\t\tpromptPaths: this.buildExtensionResourcePaths(promptPaths),\n\t\t\tthemePaths: this.buildExtensionResourcePaths(themePaths),\n\t\t};\n\n\t\tthis._resourceLoader.extendResources(extensionPaths);\n\t\tthis._rebuildSystemPrompt(this.getActiveToolNames());\n\t}\n\n\tprivate buildExtensionResourcePaths(entries: Array<{ path: string; extensionPath: string }>): Array<{\n\t\tpath: string;\n\t\tmetadata: { source: string; scope: \"temporary\"; origin: \"top-level\"; baseDir?: string };\n\t}> {\n\t\treturn entries.map((entry) => {\n\t\t\tconst source = this.getExtensionSourceLabel(entry.extensionPath);\n\t\t\tconst baseDir = entry.extensionPath.startsWith(\"<\") ? undefined : dirname(entry.extensionPath);\n\t\t\treturn {\n\t\t\t\tpath: entry.path,\n\t\t\t\tmetadata: {\n\t\t\t\t\tsource,\n\t\t\t\t\tscope: \"temporary\",\n\t\t\t\t\torigin: \"top-level\",\n\t\t\t\t\tbaseDir,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t}\n\n\tprivate getExtensionSourceLabel(extensionPath: string): string {\n\t\tif (extensionPath.startsWith(\"<\")) {\n\t\t\treturn `extension:${extensionPath.replace(/[<>]/g, \"\")}`;\n\t\t}\n\t\tconst base = basename(extensionPath);\n\t\tconst name = base.replace(/\\.(ts|js)$/, \"\");\n\t\treturn `extension:${name}`;\n\t}\n\n\tprivate _applyExtensionBindings(runner: ExtensionRunner): void {\n\t\trunner.setUIContext(this._extensionUIContext);\n\t\trunner.bindCommandContext(this._extensionCommandContextActions);\n\n\t\tthis._extensionErrorUnsubscriber?.();\n\t\tthis._extensionErrorUnsubscriber = this._extensionErrorListener\n\t\t\t? runner.onError(this._extensionErrorListener)\n\t\t\t: undefined;\n\t}\n\n\tprivate _refreshCurrentModelFromRegistry(): void {\n\t\tconst currentModel = this.model;\n\t\tif (!currentModel) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst refreshedModel = this._modelRegistry.find(currentModel.provider, currentModel.id);\n\t\tif (!refreshedModel || refreshedModel === currentModel) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.agent.state.model = refreshedModel;\n\t}\n\n\tprivate _bindExtensionCore(runner: ExtensionRunner): void {\n\t\tconst getCommands = (): SlashCommandInfo[] => {\n\t\t\tconst extensionCommands: SlashCommandInfo[] = runner.getRegisteredCommands().map((command) => ({\n\t\t\t\tname: command.invocationName,\n\t\t\t\tdescription: command.description,\n\t\t\t\tsource: \"extension\",\n\t\t\t\tsourceInfo: command.sourceInfo,\n\t\t\t}));\n\n\t\t\tconst templates: SlashCommandInfo[] = this.promptTemplates.map((template) => ({\n\t\t\t\tname: template.name,\n\t\t\t\tdescription: template.description,\n\t\t\t\tsource: \"prompt\",\n\t\t\t\tsourceInfo: template.sourceInfo,\n\t\t\t}));\n\n\t\t\tconst skills: SlashCommandInfo[] = this._resourceLoader.getSkills().skills.map((skill) => ({\n\t\t\t\tname: `skill:${skill.name}`,\n\t\t\t\tdescription: skill.description,\n\t\t\t\tsource: \"skill\",\n\t\t\t\tsourceInfo: skill.sourceInfo,\n\t\t\t}));\n\n\t\t\treturn [...extensionCommands, ...templates, ...skills];\n\t\t};\n\n\t\trunner.bindCore(\n\t\t\t{\n\t\t\t\tsendMessage: (message, options) => {\n\t\t\t\t\tthis.sendCustomMessage(message, options).catch((err) => {\n\t\t\t\t\t\trunner.emitError({\n\t\t\t\t\t\t\textensionPath: \"<runtime>\",\n\t\t\t\t\t\t\tevent: \"send_message\",\n\t\t\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\tsendUserMessage: (content, options) => {\n\t\t\t\t\tthis.sendUserMessage(content, options).catch((err) => {\n\t\t\t\t\t\trunner.emitError({\n\t\t\t\t\t\t\textensionPath: \"<runtime>\",\n\t\t\t\t\t\t\tevent: \"send_user_message\",\n\t\t\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t});\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\tappendEntry: (customType, data) => {\n\t\t\t\t\tthis.sessionManager.appendCustomEntry(customType, data);\n\t\t\t\t},\n\t\t\t\tsetSessionName: (name) => {\n\t\t\t\t\tthis.setSessionName(name);\n\t\t\t\t},\n\t\t\t\tgetSessionName: () => {\n\t\t\t\t\treturn this.sessionManager.getSessionName();\n\t\t\t\t},\n\t\t\t\tsetLabel: (entryId, label) => {\n\t\t\t\t\tthis.sessionManager.appendLabelChange(entryId, label);\n\t\t\t\t},\n\t\t\t\tgetActiveTools: () => this.getActiveToolNames(),\n\t\t\t\tgetAllTools: () => this.getAllTools(),\n\t\t\t\tsetActiveTools: (toolNames) => this.setActiveToolsByName(toolNames),\n\t\t\t\t// SCRAMJET-DIVERGENCE: harness-tool invocation (#244).\n\t\t\t\tinvokeHarnessTool: (name, args, options) => this.invokeHarnessTool(name, args, options),\n\t\t\t\trefreshTools: () => this._refreshToolRegistry(),\n\t\t\t\tgetCommands,\n\t\t\t\tsetModel: async (model) => {\n\t\t\t\t\tif (!this.modelRegistry.hasConfiguredAuth(model)) return false;\n\t\t\t\t\tawait this.setModel(model);\n\t\t\t\t\treturn true;\n\t\t\t\t},\n\t\t\t\tgetThinkingLevel: () => this.thinkingLevel,\n\t\t\t\tsetThinkingLevel: (level) => this.setThinkingLevel(level),\n\t\t\t},\n\t\t\t{\n\t\t\t\tgetModel: () => this.model,\n\t\t\t\tgetScopedModels: () => this.scopedModels,\n\t\t\t\tisIdle: () => !this.isStreaming,\n\t\t\t\tgetSignal: () => this.agent.signal,\n\t\t\t\tabort: () => this.abort(),\n\t\t\t\thasPendingMessages: () => this.pendingMessageCount > 0,\n\t\t\t\tshutdown: () => {\n\t\t\t\t\tthis._extensionShutdownHandler?.();\n\t\t\t\t},\n\t\t\t\tgetContextUsage: () => this.getContextUsage(),\n\t\t\t\tcompact: (options) => {\n\t\t\t\t\tvoid (async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await this.compact(options?.customInstructions);\n\t\t\t\t\t\t\toptions?.onComplete?.(result);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\toptions?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t})();\n\t\t\t\t},\n\t\t\t\tgetSystemPrompt: () => this.systemPrompt,\n\t\t\t\tdispatchUserInput: (input, options) => this.dispatchUserInput(input, options),\n\t\t\t},\n\t\t\t{\n\t\t\t\tregisterProvider: (name, config) => {\n\t\t\t\t\tthis._modelRegistry.registerProvider(name, config);\n\t\t\t\t\tthis._refreshCurrentModelFromRegistry();\n\t\t\t\t},\n\t\t\t\tunregisterProvider: (name) => {\n\t\t\t\t\tthis._modelRegistry.unregisterProvider(name);\n\t\t\t\t\tthis._refreshCurrentModelFromRegistry();\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n\n\tprivate _refreshToolRegistry(options?: { activeToolNames?: string[]; includeAllExtensionTools?: boolean }): void {\n\t\tconst previousRegistryNames = new Set(this._toolRegistry.keys());\n\t\tconst previousActiveToolNames = this.getActiveToolNames();\n\t\tconst allowedToolNames = this._allowedToolNames;\n\t\tconst isAllowedTool = (name: string): boolean => !allowedToolNames || allowedToolNames.has(name);\n\n\t\tconst registeredTools = this._extensionRunner.getAllRegisteredTools();\n\t\tconst allCustomTools = [\n\t\t\t...registeredTools,\n\t\t\t...this._customTools.map((definition) => ({\n\t\t\t\tdefinition,\n\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<sdk:${definition.name}>`, { source: \"sdk\" }),\n\t\t\t})),\n\t\t];\n\t\t// SCRAMJET-DIVERGENCE: historical interpretation follows definition collision winners, not execution allowlists (#598).\n\t\tthis._historicalCostDefinitions = new Map(this._baseToolDefinitions);\n\t\tfor (const tool of allCustomTools) {\n\t\t\tthis._historicalCostDefinitions.set(tool.definition.name, tool.definition);\n\t\t}\n\t\tconst allowedCustomTools = allCustomTools.filter((tool) => isAllowedTool(tool.definition.name));\n\t\tconst definitionRegistry = new Map<string, ToolDefinitionEntry>(\n\t\t\tArray.from(this._baseToolDefinitions.entries())\n\t\t\t\t.filter(([name]) => isAllowedTool(name))\n\t\t\t\t.map(([name, definition]) => [\n\t\t\t\t\tname,\n\t\t\t\t\t{\n\t\t\t\t\t\tdefinition,\n\t\t\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<builtin:${name}>`, { source: \"builtin\" }),\n\t\t\t\t\t},\n\t\t\t\t]),\n\t\t);\n\t\tfor (const tool of allowedCustomTools) {\n\t\t\tdefinitionRegistry.set(tool.definition.name, {\n\t\t\t\tdefinition: tool.definition,\n\t\t\t\tsourceInfo: tool.sourceInfo,\n\t\t\t});\n\t\t}\n\t\tthis._toolDefinitions = definitionRegistry;\n\t\tthis._toolPromptSnippets = new Map(\n\t\t\tArray.from(definitionRegistry.values())\n\t\t\t\t.map(({ definition }) => {\n\t\t\t\t\tconst snippet = this._normalizePromptSnippet(definition.promptSnippet);\n\t\t\t\t\treturn snippet ? ([definition.name, snippet] as const) : undefined;\n\t\t\t\t})\n\t\t\t\t.filter((entry): entry is readonly [string, string] => entry !== undefined),\n\t\t);\n\t\tthis._toolPromptGuidelines = new Map(\n\t\t\tArray.from(definitionRegistry.values())\n\t\t\t\t.map(({ definition }) => {\n\t\t\t\t\tconst guidelines = this._normalizePromptGuidelines(definition.promptGuidelines);\n\t\t\t\t\treturn guidelines.length > 0 ? ([definition.name, guidelines] as const) : undefined;\n\t\t\t\t})\n\t\t\t\t.filter((entry): entry is readonly [string, string[]] => entry !== undefined),\n\t\t);\n\t\tconst runner = this._extensionRunner;\n\t\tconst wrappedExtensionTools = wrapRegisteredTools(allowedCustomTools, runner);\n\t\tconst wrappedBuiltInTools = wrapRegisteredTools(\n\t\t\tArray.from(this._baseToolDefinitions.values())\n\t\t\t\t.filter((definition) => isAllowedTool(definition.name))\n\t\t\t\t.map((definition) => ({\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsourceInfo: createSyntheticSourceInfo(`<builtin:${definition.name}>`, { source: \"builtin\" }),\n\t\t\t\t})),\n\t\t\trunner,\n\t\t);\n\n\t\tconst toolRegistry = new Map(wrappedBuiltInTools.map((tool) => [tool.name, tool]));\n\t\tfor (const tool of wrappedExtensionTools as AgentTool[]) {\n\t\t\ttoolRegistry.set(tool.name, tool);\n\t\t}\n\t\tthis._toolRegistry = toolRegistry;\n\n\t\tconst nextActiveToolNames = (\n\t\t\toptions?.activeToolNames ? [...options.activeToolNames] : [...previousActiveToolNames]\n\t\t).filter((name) => isAllowedTool(name));\n\n\t\tif (allowedToolNames) {\n\t\t\tfor (const toolName of this._toolRegistry.keys()) {\n\t\t\t\tif (allowedToolNames.has(toolName)) {\n\t\t\t\t\tnextActiveToolNames.push(toolName);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (options?.includeAllExtensionTools) {\n\t\t\tfor (const tool of wrappedExtensionTools) {\n\t\t\t\tnextActiveToolNames.push(tool.name);\n\t\t\t}\n\t\t} else if (!options?.activeToolNames) {\n\t\t\tfor (const toolName of this._toolRegistry.keys()) {\n\t\t\t\tif (!previousRegistryNames.has(toolName)) {\n\t\t\t\t\tnextActiveToolNames.push(toolName);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.setActiveToolsByName([...new Set(nextActiveToolNames)]);\n\t}\n\n\tprivate _buildRuntime(options: {\n\t\tactiveToolNames?: string[];\n\t\tflagValues?: Map<string, boolean | string>;\n\t\tincludeAllExtensionTools?: boolean;\n\t}): void {\n\t\tconst autoResizeImages = this.settingsManager.getImageAutoResize();\n\t\tconst shellCommandPrefix = this.settingsManager.getShellCommandPrefix();\n\t\tconst shellPath = this.settingsManager.getShellPath();\n\t\tconst baseToolDefinitions = this._baseToolsOverride\n\t\t\t? Object.fromEntries(\n\t\t\t\t\tObject.entries(this._baseToolsOverride).map(([name, tool]) => [\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tcreateToolDefinitionFromAgentTool(tool),\n\t\t\t\t\t]),\n\t\t\t\t)\n\t\t\t: createAllToolDefinitions(this._cwd, {\n\t\t\t\t\tread: { autoResizeImages },\n\t\t\t\t\tbash: { commandPrefix: shellCommandPrefix, shellPath },\n\t\t\t\t});\n\n\t\tthis._baseToolDefinitions = new Map(\n\t\t\tObject.entries(baseToolDefinitions).map(([name, tool]) => [name, tool as ToolDefinition]),\n\t\t);\n\n\t\tconst extensionsResult = this._resourceLoader.getExtensions();\n\t\tif (options.flagValues) {\n\t\t\tfor (const [name, value] of options.flagValues) {\n\t\t\t\textensionsResult.runtime.flagValues.set(name, value);\n\t\t\t}\n\t\t}\n\n\t\tthis._extensionRunner = new ExtensionRunner(\n\t\t\textensionsResult.extensions,\n\t\t\textensionsResult.runtime,\n\t\t\tthis._cwd,\n\t\t\tthis.sessionManager,\n\t\t\tthis._modelRegistry,\n\t\t);\n\t\tif (this._extensionRunnerRef) {\n\t\t\tthis._extensionRunnerRef.current = this._extensionRunner;\n\t\t}\n\t\tthis._bindExtensionCore(this._extensionRunner);\n\t\tthis._applyExtensionBindings(this._extensionRunner);\n\n\t\tconst defaultActiveToolNames = this._baseToolsOverride\n\t\t\t? Object.keys(this._baseToolsOverride)\n\t\t\t: [\"read\", \"bash\", \"edit\", \"write\"];\n\t\tconst baseActiveToolNames = options.activeToolNames ?? defaultActiveToolNames;\n\t\tthis._refreshToolRegistry({\n\t\t\tactiveToolNames: baseActiveToolNames,\n\t\t\tincludeAllExtensionTools: options.includeAllExtensionTools,\n\t\t});\n\t}\n\n\tasync reload(): Promise<void> {\n\t\tconst previousFlagValues = this._extensionRunner.getFlagValues();\n\t\t// SCRAMJET-DIVERGENCE: the Scramjet builtin is a required product component, so reload must validate\n\t\t// the replacement loader before any irreversible runtime mutation. settingsManager.reload() runs first —\n\t\t// it commits fresh settings before the loader can throw, but the re-read is idempotent, so a failed reload\n\t\t// leaves the runtime itself untouched. It also runs first\n\t\t// because the loader's path resolution reads live settings (the loader defers its own settings/source-map\n\t\t// commit until after the builtin init succeeds, so on this path the shared manager is intentionally read\n\t\t// twice). resourceLoader.reload() then re-runs the required\n\t\t// builtin and throws RequiredBuiltinInitError on failure — before session_shutdown is emitted and before\n\t\t// resetApiProviders(). A throwing builtin therefore leaves the current runner, resources, and providers\n\t\t// intact and fires no session_shutdown; only a validated candidate proceeds to teardown and rebuild.\n\t\tawait this.settingsManager.reload();\n\t\tawait this._resourceLoader.reload();\n\t\tawait emitSessionShutdownEvent(this._extensionRunner, { type: \"session_shutdown\", reason: \"reload\" });\n\t\tresetApiProviders();\n\t\tthis._runPromptComposition = { kind: \"generated-base\" };\n\t\tthis._buildRuntime({\n\t\t\tactiveToolNames: this.getActiveToolNames(),\n\t\t\tflagValues: previousFlagValues,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\n\t\tconst hasBindings =\n\t\t\tthis._extensionUIContext ||\n\t\t\tthis._extensionCommandContextActions ||\n\t\t\tthis._extensionShutdownHandler ||\n\t\t\tthis._extensionErrorListener;\n\t\tif (hasBindings) {\n\t\t\tawait this._extensionRunner.emit({ type: \"session_start\", reason: \"reload\" });\n\t\t\tawait this.extendResourcesFromExtensions(\"reload\");\n\t\t}\n\t}\n\n\t// =========================================================================\n\t// Auto-Retry\n\t// =========================================================================\n\n\t// SCRAMJET-DIVERGENCE: persisted post-extension snapshots authoritatively classify and settle retries (#553).\n\tprivate _classifyRetry(message: AssistantMessage): RetryClassification {\n\t\tif ((message.origin === \"harness\" || message.stopReason === \"aborted\") && blocksFailureRecovery(message))\n\t\t\treturn { kind: \"do_not_retry\", reason: \"non_provider_failure\", evidence: \"none\" };\n\t\tconst contextWindow = this.model?.contextWindow ?? 0;\n\t\tif (isContextOverflow(message, Math.min(contextWindow, this.model?.maxInputTokens ?? Infinity))) {\n\t\t\treturn { kind: \"context_overflow\" };\n\t\t}\n\n\t\tconst structured = inspectFailureEvidence(message.diagnostics);\n\t\tif (structured.status === \"valid\") {\n\t\t\tif (structured.source !== \"provider\")\n\t\t\t\treturn { kind: \"do_not_retry\", reason: \"non_provider_failure\", evidence: \"none\" };\n\t\t\tif (structured.suppression)\n\t\t\t\treturn { kind: \"do_not_retry\", reason: structured.suppression, evidence: structured.family };\n\t\t\tif (structured.transient || [\"missing_terminal_event\", \"missing_body\"].includes(structured.category))\n\t\t\t\treturn { kind: \"retry\", evidence: structured.family };\n\t\t\treturn {\n\t\t\t\tkind: \"do_not_retry\",\n\t\t\t\treason: [\"unknown\", \"provider_error\", \"malformed_event\"].includes(structured.category)\n\t\t\t\t\t? \"structured_unknown\"\n\t\t\t\t\t: \"structured_non_transient\",\n\t\t\t\tevidence: structured.family,\n\t\t\t};\n\t\t}\n\t\tif (structured.status === \"conflicting\")\n\t\t\treturn { kind: \"do_not_retry\", reason: \"invalid_failure_evidence\", evidence: \"none\" };\n\t\tif (structured.status === \"malformed\") {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"malformed_provider_diagnostic\", evidence: \"none\" };\n\t\t}\n\t\tif (structured.status === \"duplicate\") {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"duplicate_provider_diagnostic\", evidence: \"none\" };\n\t\t}\n\t\tif (!message.errorMessage) {\n\t\t\treturn { kind: \"do_not_retry\", reason: \"missing_error_evidence\", evidence: \"none\" };\n\t\t}\n\t\tif (this._isLegacyRetryableError(message.errorMessage)) return { kind: \"retry\", evidence: \"legacy_text\" };\n\t\treturn { kind: \"do_not_retry\", reason: \"legacy_non_retryable\", evidence: \"legacy_text\" };\n\t}\n\n\tprivate _isLegacyRetryableError(errorMessage: string): boolean {\n\t\treturn /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|network.?error|connection.?error|connection.?refused|connection.?lost|websocket.?closed|websocket.?error|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|ended without|stream ended before message_stop|http2 request did not get a response|timed? out|timeout|terminated|retry delay/i.test(\n\t\t\terrorMessage,\n\t\t);\n\t}\n\n\tprivate _bounded(value: number): number {\n\t\treturn Math.min(65_535, Math.max(0, Number.isFinite(value) ? Math.trunc(value) : 65_535));\n\t}\n\n\tprivate _appendAutoRetryRecord(record: AutoRetryRecord): void {\n\t\tthis.sessionManager.appendCustomEntry(\"coding-agent:auto-retry\", record);\n\t}\n\n\t/**\n\t * Persist a terminal record, emit `auto_retry_end` when a retry phase was active, and clear the phase.\n\t * The phase is released before persistence is attempted so a persistence failure can never strand it.\n\t */\n\tprivate _finishRetry(record: AutoRetryRecord, end?: Extract<AgentSessionEvent, { type: \"auto_retry_end\" }>): void {\n\t\tconst retry = this._retry;\n\t\tthis._retry = undefined;\n\t\ttry {\n\t\t\tthis._appendAutoRetryRecord(record);\n\t\t} catch (error) {\n\t\t\tif (retry && !this._manualCompactionDrain) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: end?.attempt ?? retry.attempt,\n\t\t\t\t\tfinalError: \"Retry outcome persistence failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow this._reportRetryFailure(error);\n\t\t}\n\t\tif (retry && end && !this._manualCompactionDrain) this._emit(end);\n\t}\n\n\tprivate _finishRetryWithoutSuccess(record: AutoRetryRecord, message: AssistantMessage): void {\n\t\tconst retry = this._retry;\n\t\tif (retry?.attempt === 0) {\n\t\t\t// The continuation already produced a successful message, so the retry recovered and this\n\t\t\t// later failure is a fresh, non-retried error; close the retry as succeeded first.\n\t\t\tconst attempt = this._runRetryCount;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"succeeded\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(attempt),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: true, attempt },\n\t\t\t);\n\t\t\tthis._finishRetry(record);\n\t\t\treturn;\n\t\t}\n\t\tthis._finishRetry(\n\t\t\trecord,\n\t\t\tretry\n\t\t\t\t? { type: \"auto_retry_end\", success: false, attempt: retry.attempt, finalError: message.errorMessage }\n\t\t\t\t: undefined,\n\t\t);\n\t}\n\n\t/** A run that ends without a classifiable snapshot cannot continue the retry; close it so nothing stays wedged. */\n\tprivate _releaseRetryForFailedRun(error: Error): void {\n\t\tconst retry = this._retry;\n\t\tif (!retry) return;\n\t\ttry {\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"failed\",\n\t\t\t\t\treason: \"run_failed\",\n\t\t\t\t\tattempt: this._bounded(retry.attempt),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt: retry.attempt, finalError: error.message },\n\t\t\t);\n\t\t} catch {\n\t\t\t// Already reported and the phase is released; the run settles with its own error.\n\t\t}\n\t}\n\n\tprivate _reportRetryFailure(error: unknown): Error {\n\t\tconst normalized = error instanceof Error ? error : new Error(String(error));\n\t\tthis._extensionRunner.emitError({\n\t\t\textensionPath: \"agent-session:auto-retry\",\n\t\t\tevent: \"retry_settlement\",\n\t\t\terror: normalized.message,\n\t\t\tstack: normalized.stack,\n\t\t});\n\t\treturn normalized;\n\t}\n\n\tprivate async _handleRetryableError(\n\t\tmessage: AssistantMessage,\n\t\tevidence: RetryFailureEvidence | \"legacy_text\",\n\t\trun: AgentRunSettlement,\n\t): Promise<boolean> {\n\t\tconst settings = this.settingsManager.getRetrySettings();\n\t\tconst attempt = (this._retry?.attempt ?? 0) + 1;\n\t\tconst cumulativeCap = Math.min(Number.MAX_SAFE_INTEGER, settings.maxRetries * 2);\n\t\tif ((this._retry?.attempt ?? 0) >= settings.maxRetries) {\n\t\t\tconst attemptsCompleted = attempt - 1;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"exhausted\",\n\t\t\t\t\treason: \"attempt_limit\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attemptsCompleted),\n\t\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t},\n\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt: attemptsCompleted, finalError: message.errorMessage },\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\n\t\tif (this._runRetryCount >= cumulativeCap) {\n\t\t\tconst attemptsCompleted = this._runRetryCount;\n\t\t\tthis._finishRetry(\n\t\t\t\t{\n\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\toutcome: \"exhausted\",\n\t\t\t\t\treason: \"cumulative_limit\",\n\t\t\t\t\tattemptsCompleted: this._bounded(attemptsCompleted),\n\t\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount + 1),\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: attemptsCompleted,\n\t\t\t\t\tfinalError:\n\t\t\t\t\t\t`Repeated retry failures (${attemptsCompleted} total attempts this prompt). ${message.errorMessage ?? \"\"}`.trim(),\n\t\t\t\t},\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\n\t\tthis._runRetryCount++;\n\t\tconst delayMs =\n\t\t\tsettings.baseDelayMs === 0\n\t\t\t\t? 0\n\t\t\t\t: Math.min(2_147_483_647, settings.baseDelayMs * 2 ** Math.min(31, attempt - 1));\n\t\tconst previous = this._retry;\n\t\tthis._retry = undefined;\n\t\ttry {\n\t\t\tthis._appendAutoRetryRecord({\n\t\t\t\tschemaVersion: 1,\n\t\t\t\toutcome: \"scheduled\",\n\t\t\t\tevidence,\n\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\tmaxAttempts: this._bounded(settings.maxRetries),\n\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\tdelayMs: this._bounded(delayMs),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (previous) {\n\t\t\t\tthis._emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: previous.attempt,\n\t\t\t\t\tfinalError: \"Retry outcome persistence failed\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow this._reportRetryFailure(error);\n\t\t}\n\n\t\tconst controller = new AbortController();\n\t\tconst backoff: RetryPhase = { phase: \"backoff\", attempt, controller, run };\n\t\tthis._retry = backoff;\n\t\tthis._emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt,\n\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\tdelayMs,\n\t\t\terrorMessage: message.errorMessage || \"Unknown error\",\n\t\t\tcumulativeErrors: this._runRetryCount,\n\t\t});\n\t\tif (controller.signal.aborted || this._disposed) {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\n\t\tconst messages = this.agent.state.messages;\n\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\tthis.agent.state.messages = messages.slice(0, -1);\n\t\t}\n\n\t\ttry {\n\t\t\tawait sleep(delayMs, controller.signal);\n\t\t\tawait sleep(0, controller.signal);\n\t\t} catch {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\t\tif (controller.signal.aborted || this._disposed) {\n\t\t\treturn this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\t\t}\n\n\t\tif (this._manualCompactionDrain) return this._finishCancelledRetry(\"cancelled_during_backoff\", backoff);\n\n\t\t// Hand the chain to the continuation before `continue()`; the run that eventually emits\n\t\t// `agent_start` adopts it (see `_captureAgentRunSettlement`), however long Agent defers that start.\n\t\tconst continuing: RetryPhase = { phase: \"continuing\", attempt, chain: run.chain };\n\t\tthis._retry = continuing;\n\t\tconst continuation = this.agent.continue();\n\t\tthis._settleAgentRun(run);\n\t\tcontinuation.catch((error) => {\n\t\t\tif (this._disposed || this._retry !== continuing) return;\n\t\t\tconst continuationError = this._reportRetryFailure(error);\n\t\t\tlet settlementError = continuationError;\n\t\t\ttry {\n\t\t\t\tthis._finishRetry(\n\t\t\t\t\t{\n\t\t\t\t\t\tschemaVersion: 1,\n\t\t\t\t\t\toutcome: \"failed\",\n\t\t\t\t\t\treason: \"continuation_rejected\",\n\t\t\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t\t\t},\n\t\t\t\t\t{ type: \"auto_retry_end\", success: false, attempt, finalError: continuationError.message },\n\t\t\t\t);\n\t\t\t} catch (persistenceError) {\n\t\t\t\tsettlementError = new AggregateError(\n\t\t\t\t\t[continuationError, persistenceError],\n\t\t\t\t\t\"Retry continuation and outcome persistence both failed.\",\n\t\t\t\t\t{ cause: continuationError },\n\t\t\t\t);\n\t\t\t}\n\t\t\tthis._settleRetryChain(run.chain, settlementError);\n\t\t});\n\n\t\treturn true;\n\t}\n\n\tprivate _finishCancelledRetry(\n\t\treason: \"cancelled_during_backoff\" | \"cancelled_during_continuation\",\n\t\texpected?: RetryPhase,\n\t): false {\n\t\tif (this._disposed) {\n\t\t\tthis._retry = undefined;\n\t\t\tthrow new Error(\"AgentSession disposed before retry settlement completed.\");\n\t\t}\n\t\tconst retry = this._retry;\n\t\tif (expected && retry !== expected) return false;\n\t\tconst attempt = retry?.attempt ?? 0;\n\t\tthis._finishRetry(\n\t\t\t{\n\t\t\t\tschemaVersion: 1,\n\t\t\t\toutcome: \"cancelled\",\n\t\t\t\treason,\n\t\t\t\tattempt: this._bounded(attempt),\n\t\t\t\tcumulativeErrors: this._bounded(this._runRetryCount),\n\t\t\t},\n\t\t\t{ type: \"auto_retry_end\", success: false, attempt, finalError: \"Retry cancelled\" },\n\t\t);\n\t\treturn false;\n\t}\n\n\t/**\n\t * Cancel in-progress retry.\n\t */\n\tabortRetry(): void {\n\t\tconst retry = this._retry;\n\t\tif (!retry) return;\n\t\tif (retry.phase === \"backoff\") retry.controller.abort();\n\t\tthis.agent.abort();\n\t}\n\n\t/** Agent's own `activeRun` guard is false during backoff; the retry phase owns the idle Agent then. */\n\tprivate _assertRetryNotOwningIdleAgent(): void {\n\t\tif (this._retry && !this.isStreaming) {\n\t\t\tthrow new Error(\"Automatic retry is in progress. Cancel the retry before starting another prompt.\");\n\t\t}\n\t}\n\n\tprivate async _runAgentPrompt(messages: AgentMessage | AgentMessage[]): Promise<void> {\n\t\tthis._assertRetryNotOwningIdleAgent();\n\t\tconst start = this._reserveAgentRunStart();\n\t\tthis._runRetryCount = 0;\n\t\tlet promptFailure: { error: unknown } | undefined;\n\t\ttry {\n\t\t\tawait this.agent.prompt(messages);\n\t\t} catch (error) {\n\t\t\tpromptFailure = { error };\n\t\t}\n\t\tif (!start.run && this._pendingAgentRunStart === start) {\n\t\t\tconst startError = promptFailure\n\t\t\t\t? promptFailure.error instanceof Error\n\t\t\t\t\t? promptFailure.error\n\t\t\t\t\t: new Error(String(promptFailure.error))\n\t\t\t\t: new Error(\"Agent prompt completed before agent_start.\");\n\t\t\tthis._rejectAgentRunStart(start, startError);\n\t\t\tif (promptFailure) throw promptFailure.error;\n\t\t}\n\t\tlet settlementFailure: { error: unknown } | undefined;\n\t\ttry {\n\t\t\tconst settlement = await start.promise;\n\t\t\tawait this.waitForRetry(settlement);\n\t\t} catch (error) {\n\t\t\tsettlementFailure = { error };\n\t\t}\n\t\tif (promptFailure && settlementFailure) {\n\t\t\tthrow new AggregateError(\n\t\t\t\t[promptFailure.error, settlementFailure.error],\n\t\t\t\t\"Agent prompt and retry settlement both failed.\",\n\t\t\t\t{ cause: promptFailure.error },\n\t\t\t);\n\t\t}\n\t\tif (promptFailure) throw promptFailure.error;\n\t\tif (settlementFailure) throw settlementFailure.error;\n\t}\n\n\tprivate async waitForRetry(settlement: AgentRunSettlement | undefined): Promise<void> {\n\t\tif (!settlement) return;\n\t\tawait settlement.promise;\n\t\tawait settlement.chain.promise;\n\t\tawait this.agent.waitForIdle();\n\t}\n\n\t/** Whether auto-retry is currently in progress */\n\tget isRetrying(): boolean {\n\t\treturn this._retry !== undefined;\n\t}\n\n\t/** Whether auto-retry is enabled */\n\tget autoRetryEnabled(): boolean {\n\t\treturn this.settingsManager.getRetryEnabled();\n\t}\n\n\t/**\n\t * Toggle auto-retry setting.\n\t */\n\tsetAutoRetryEnabled(enabled: boolean): void {\n\t\tthis.settingsManager.setRetryEnabled(enabled);\n\t}\n\n\t// =========================================================================\n\t// Bash Execution\n\t// =========================================================================\n\n\t/**\n\t * Execute a bash command.\n\t * Adds result to agent context and session.\n\t * @param command The bash command to execute\n\t * @param onChunk Optional streaming callback for output\n\t * @param options.excludeFromContext If true, command output won't be sent to LLM (!! prefix)\n\t * @param options.operations Custom BashOperations for remote execution\n\t */\n\tasync executeBash(\n\t\tcommand: string,\n\t\tonChunk?: (chunk: string) => void,\n\t\toptions?: { excludeFromContext?: boolean; operations?: BashOperations },\n\t): Promise<BashResult> {\n\t\tthis._bashAbortController = new AbortController();\n\n\t\t// Apply command prefix if configured (e.g., \"shopt -s expand_aliases\" for alias support)\n\t\tconst prefix = this.settingsManager.getShellCommandPrefix();\n\t\tconst shellPath = this.settingsManager.getShellPath();\n\t\tconst resolvedCommand = prefix ? `${prefix}\\n${command}` : command;\n\n\t\ttry {\n\t\t\tconst result = await executeBashWithOperations(\n\t\t\t\tresolvedCommand,\n\t\t\t\tthis.sessionManager.getCwd(),\n\t\t\t\toptions?.operations ?? createLocalBashOperations({ shellPath }),\n\t\t\t\t{\n\t\t\t\t\tonChunk,\n\t\t\t\t\tsignal: this._bashAbortController.signal,\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tthis.recordBashResult(command, result, options);\n\t\t\treturn result;\n\t\t} finally {\n\t\t\tthis._bashAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Record a bash execution result in session history.\n\t * Used by executeBash and by extensions that handle bash execution themselves.\n\t */\n\trecordBashResult(command: string, result: BashResult, options?: { excludeFromContext?: boolean }): void {\n\t\tconst bashMessage: BashExecutionMessage = {\n\t\t\trole: \"bashExecution\",\n\t\t\tcommand,\n\t\t\toutput: result.output,\n\t\t\texitCode: result.exitCode,\n\t\t\tcancelled: result.cancelled,\n\t\t\ttruncated: result.truncated,\n\t\t\tfullOutputPath: result.fullOutputPath,\n\t\t\ttimestamp: Date.now(),\n\t\t\texcludeFromContext: options?.excludeFromContext,\n\t\t};\n\n\t\t// If agent is streaming, defer adding to avoid breaking tool_use/tool_result ordering\n\t\tif (this.isStreaming) {\n\t\t\t// Queue for later - will be flushed on agent_end\n\t\t\tthis._pendingBashMessages.push(bashMessage);\n\t\t} else {\n\t\t\t// Add to agent state immediately\n\t\t\tthis.agent.state.messages.push(bashMessage);\n\n\t\t\t// Save to session\n\t\t\tthis.sessionManager.appendMessage(bashMessage);\n\t\t}\n\t}\n\n\t/**\n\t * Cancel running bash command.\n\t */\n\tabortBash(): void {\n\t\tthis._bashAbortController?.abort();\n\t}\n\n\t/** Whether a bash command is currently running */\n\tget isBashRunning(): boolean {\n\t\treturn this._bashAbortController !== undefined;\n\t}\n\n\t/** Whether there are pending bash messages waiting to be flushed */\n\tget hasPendingBashMessages(): boolean {\n\t\treturn this._pendingBashMessages.length > 0;\n\t}\n\n\t/**\n\t * Flush pending bash messages to agent state and session.\n\t * Called after agent turn completes to maintain proper message ordering.\n\t */\n\tprivate _flushPendingBashMessages(): void {\n\t\tif (this._pendingBashMessages.length === 0) return;\n\n\t\tfor (const bashMessage of this._pendingBashMessages) {\n\t\t\t// Add to agent state\n\t\t\tthis.agent.state.messages.push(bashMessage);\n\n\t\t\t// Save to session\n\t\t\tthis.sessionManager.appendMessage(bashMessage);\n\t\t}\n\n\t\tthis._pendingBashMessages = [];\n\t}\n\n\t// =========================================================================\n\t// Session Management\n\t// =========================================================================\n\n\t/**\n\t * Set a display name for the current session.\n\t */\n\tsetSessionName(name: string): void {\n\t\tthis.sessionManager.appendSessionInfo(name);\n\t\tthis._emit({ type: \"session_info_changed\", name: this.sessionManager.getSessionName() });\n\t}\n\n\t// =========================================================================\n\t// Tree Navigation\n\t// =========================================================================\n\n\t/**\n\t * Navigate to a different node in the session tree.\n\t * Unlike fork() which creates a new session file, this stays in the same file.\n\t *\n\t * @param targetId The entry ID to navigate to\n\t * @param options.summarize Whether user wants to summarize abandoned branch\n\t * @param options.customInstructions Custom instructions for summarizer\n\t * @param options.replaceInstructions If true, customInstructions replaces the default prompt\n\t * @param options.label Label to attach to the branch summary entry\n\t * @returns Result with editorText (if user message) and cancelled status\n\t */\n\tasync navigateTree(\n\t\ttargetId: string,\n\t\toptions: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string } = {},\n\t): Promise<{ editorText?: string; cancelled: boolean; aborted?: boolean; summaryEntry?: BranchSummaryEntry }> {\n\t\tconst oldLeafId = this.sessionManager.getLeafId();\n\n\t\t// No-op if already at target\n\t\tif (targetId === oldLeafId) {\n\t\t\treturn { cancelled: false };\n\t\t}\n\n\t\t// Model required for summarization\n\t\tif (options.summarize && !this.model) {\n\t\t\tthrow new Error(\"No model available for summarization\");\n\t\t}\n\n\t\tconst targetEntry = this.sessionManager.getEntry(targetId);\n\t\tif (!targetEntry) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\n\t\t// Collect entries to summarize (from old leaf to common ancestor)\n\t\tconst { entries: entriesToSummarize, commonAncestorId } = collectEntriesForBranchSummary(\n\t\t\tthis.sessionManager,\n\t\t\toldLeafId,\n\t\t\ttargetId,\n\t\t);\n\n\t\t// Prepare event data - mutable so extensions can override\n\t\tlet customInstructions = options.customInstructions;\n\t\tlet replaceInstructions = options.replaceInstructions;\n\t\tlet label = options.label;\n\n\t\tconst preparation: TreePreparation = {\n\t\t\ttargetId,\n\t\t\toldLeafId,\n\t\t\tcommonAncestorId,\n\t\t\tentriesToSummarize,\n\t\t\tuserWantsSummary: options.summarize ?? false,\n\t\t\tcustomInstructions,\n\t\t\treplaceInstructions,\n\t\t\tlabel,\n\t\t};\n\n\t\t// Set up abort controller for summarization\n\t\tthis._branchSummaryAbortController = new AbortController();\n\n\t\ttry {\n\t\t\tlet extensionSummary: { summary: string; details?: unknown } | undefined;\n\t\t\tlet fromExtension = false;\n\n\t\t\t// Emit session_before_tree event\n\t\t\tif (this._extensionRunner.hasHandlers(\"session_before_tree\")) {\n\t\t\t\tconst result = (await this._extensionRunner.emit({\n\t\t\t\t\ttype: \"session_before_tree\",\n\t\t\t\t\tpreparation,\n\t\t\t\t\tsignal: this._branchSummaryAbortController.signal,\n\t\t\t\t})) as SessionBeforeTreeResult | undefined;\n\n\t\t\t\tif (result?.cancel) {\n\t\t\t\t\treturn { cancelled: true };\n\t\t\t\t}\n\n\t\t\t\tif (result?.summary && options.summarize) {\n\t\t\t\t\textensionSummary = result.summary;\n\t\t\t\t\tfromExtension = true;\n\t\t\t\t}\n\n\t\t\t\t// Allow extensions to override instructions and label\n\t\t\t\tif (result?.customInstructions !== undefined) {\n\t\t\t\t\tcustomInstructions = result.customInstructions;\n\t\t\t\t}\n\t\t\t\tif (result?.replaceInstructions !== undefined) {\n\t\t\t\t\treplaceInstructions = result.replaceInstructions;\n\t\t\t\t}\n\t\t\t\tif (result?.label !== undefined) {\n\t\t\t\t\tlabel = result.label;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Run default summarizer if needed\n\t\t\tlet summaryText: string | undefined;\n\t\t\tlet summaryDetails: unknown;\n\t\t\tif (options.summarize && entriesToSummarize.length > 0 && !extensionSummary) {\n\t\t\t\tconst model = this.model!;\n\t\t\t\tconst { apiKey, headers } = await this._getRequiredRequestAuth(model);\n\t\t\t\tconst branchSummarySettings = this.settingsManager.getBranchSummarySettings();\n\t\t\t\tconst result = await generateBranchSummary(entriesToSummarize, {\n\t\t\t\t\tmodel,\n\t\t\t\t\tapiKey,\n\t\t\t\t\theaders,\n\t\t\t\t\tsignal: this._branchSummaryAbortController.signal,\n\t\t\t\t\tcustomInstructions,\n\t\t\t\t\treplaceInstructions,\n\t\t\t\t\treserveTokens: branchSummarySettings.reserveTokens,\n\t\t\t\t});\n\t\t\t\tif (result.aborted) {\n\t\t\t\t\treturn { cancelled: true, aborted: true };\n\t\t\t\t}\n\t\t\t\tif (result.error) {\n\t\t\t\t\tthrow new Error(result.error);\n\t\t\t\t}\n\t\t\t\tsummaryText = result.summary;\n\t\t\t\tsummaryDetails = {\n\t\t\t\t\treadFiles: result.readFiles || [],\n\t\t\t\t\tmodifiedFiles: result.modifiedFiles || [],\n\t\t\t\t};\n\t\t\t} else if (extensionSummary) {\n\t\t\t\tsummaryText = extensionSummary.summary;\n\t\t\t\tsummaryDetails = extensionSummary.details;\n\t\t\t}\n\n\t\t\t// Determine the new leaf position based on target type\n\t\t\tlet newLeafId: string | null;\n\t\t\tlet editorText: string | undefined;\n\n\t\t\tif (targetEntry.type === \"message\" && targetEntry.message.role === \"user\") {\n\t\t\t\t// User message: leaf = parent (null if root), text goes to editor\n\t\t\t\tnewLeafId = targetEntry.parentId;\n\t\t\t\teditorText = restoreScramjetCommandInvocation(targetEntry, this.sessionManager.getEntries());\n\t\t\t} else if (targetEntry.type === \"custom_message\") {\n\t\t\t\t// Custom message: leaf = parent (null if root), text goes to editor\n\t\t\t\tnewLeafId = targetEntry.parentId;\n\t\t\t\teditorText =\n\t\t\t\t\ttypeof targetEntry.content === \"string\"\n\t\t\t\t\t\t? targetEntry.content\n\t\t\t\t\t\t: targetEntry.content\n\t\t\t\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t\t\t\t.join(\"\");\n\t\t\t} else {\n\t\t\t\t// Non-user message: leaf = selected node\n\t\t\t\tnewLeafId = targetId;\n\t\t\t}\n\n\t\t\t// Switch leaf (with or without summary)\n\t\t\t// Summary is attached at the navigation target position (newLeafId), not the old branch\n\t\t\tlet summaryEntry: BranchSummaryEntry | undefined;\n\t\t\tif (summaryText) {\n\t\t\t\t// Create summary at target position (can be null for root)\n\t\t\t\tconst summaryId = this.sessionManager.branchWithSummary(\n\t\t\t\t\tnewLeafId,\n\t\t\t\t\tsummaryText,\n\t\t\t\t\tsummaryDetails,\n\t\t\t\t\tfromExtension,\n\t\t\t\t);\n\t\t\t\tsummaryEntry = this.sessionManager.getEntry(summaryId) as BranchSummaryEntry;\n\n\t\t\t\t// Attach label to the summary entry\n\t\t\t\tif (label) {\n\t\t\t\t\tthis.sessionManager.appendLabelChange(summaryId, label);\n\t\t\t\t}\n\t\t\t} else if (newLeafId === null) {\n\t\t\t\t// No summary, navigating to root - reset leaf\n\t\t\t\tthis.sessionManager.resetLeaf();\n\t\t\t} else {\n\t\t\t\t// No summary, navigating to non-root\n\t\t\t\tthis.sessionManager.branch(newLeafId);\n\t\t\t}\n\n\t\t\t// Attach label to target entry when not summarizing (no summary entry to label)\n\t\t\tif (label && !summaryText) {\n\t\t\t\tthis.sessionManager.appendLabelChange(targetId, label);\n\t\t\t}\n\n\t\t\t// Update agent state\n\t\t\tconst sessionContext = this.sessionManager.buildSessionContext();\n\t\t\tthis.agent.state.messages = sessionContext.messages;\n\n\t\t\t// Emit session_tree event\n\t\t\tawait this._extensionRunner.emit({\n\t\t\t\ttype: \"session_tree\",\n\t\t\t\tnewLeafId: this.sessionManager.getLeafId(),\n\t\t\t\toldLeafId,\n\t\t\t\tsummaryEntry,\n\t\t\t\tfromExtension: summaryText ? fromExtension : undefined,\n\t\t\t});\n\n\t\t\t// Emit to custom tools\n\n\t\t\treturn { editorText, cancelled: false, summaryEntry };\n\t\t} finally {\n\t\t\tthis._branchSummaryAbortController = undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Get all user messages from session for fork selector.\n\t */\n\tgetUserMessagesForForking(): Array<{ entryId: string; text: string }> {\n\t\tconst entries = this.sessionManager.getEntries();\n\t\tconst commandStarts = indexScramjetCommandStarts(entries);\n\t\tconst result: Array<{ entryId: string; text: string }> = [];\n\n\t\tfor (const entry of entries) {\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tif (entry.message.role !== \"user\") continue;\n\n\t\t\tconst text = restoreScramjetCommandInvocation(entry, commandStarts);\n\t\t\tif (text) {\n\t\t\t\tresult.push({ entryId: entry.id, text });\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t// SCRAMJET-DIVERGENCE: whole-journal parent/tool costs plus reports awaiting successful append (#598).\n\tgetRecordedSessionCost(): number {\n\t\tlet total = 0;\n\t\tfor (const entry of this.sessionManager.getEntries()) {\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tconst message = entry.message;\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tif (validRecordedCost(message.usage?.cost?.total)) total += message.usage.cost.total;\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\tif (validRecordedCost(message.cost)) {\n\t\t\t\t\ttotal += message.cost;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst definition = this._historicalCostDefinitions.get(message.toolName);\n\t\t\t\tif (!definition?.getHistoricalCost) continue;\n\t\t\t\ttry {\n\t\t\t\t\tconst cost = definition.getHistoricalCost(message.details);\n\t\t\t\t\tif (validRecordedCost(cost)) total += cost;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis._extensionRunner.emitError({\n\t\t\t\t\t\textensionPath: `tool:${message.toolName}`,\n\t\t\t\t\t\tevent: \"historical_cost\",\n\t\t\t\t\t\terror: `Failed to interpret historical tool cost: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (const execution of this._pendingToolCosts) total += execution.cost ?? 0;\n\t\treturn total;\n\t}\n\n\t/**\n\t * Get session statistics.\n\t */\n\tgetSessionStats(): SessionStats {\n\t\tconst state = this.state;\n\t\tconst userMessages = state.messages.filter((m) => m.role === \"user\").length;\n\t\tconst assistantMessages = state.messages.filter((m) => m.role === \"assistant\").length;\n\t\tconst toolResults = state.messages.filter((m) => m.role === \"toolResult\").length;\n\n\t\tlet toolCalls = 0;\n\t\tlet totalInput = 0;\n\t\tlet totalOutput = 0;\n\t\tlet totalCacheRead = 0;\n\t\tlet totalCacheWrite = 0;\n\t\tlet totalCost = 0;\n\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttoolCalls += assistantMsg.content.filter((c) => c.type === \"toolCall\").length;\n\t\t\t\ttotalInput += assistantMsg.usage.input;\n\t\t\t\ttotalOutput += assistantMsg.usage.output;\n\t\t\t\ttotalCacheRead += assistantMsg.usage.cacheRead;\n\t\t\t\ttotalCacheWrite += assistantMsg.usage.cacheWrite;\n\t\t\t\ttotalCost += assistantMsg.usage.cost.total;\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tsessionFile: this.sessionFile,\n\t\t\tsessionId: this.sessionId,\n\t\t\tuserMessages,\n\t\t\tassistantMessages,\n\t\t\ttoolCalls,\n\t\t\ttoolResults,\n\t\t\ttotalMessages: state.messages.length,\n\t\t\ttokens: {\n\t\t\t\tinput: totalInput,\n\t\t\t\toutput: totalOutput,\n\t\t\t\tcacheRead: totalCacheRead,\n\t\t\t\tcacheWrite: totalCacheWrite,\n\t\t\t\ttotal: totalInput + totalOutput + totalCacheRead + totalCacheWrite,\n\t\t\t},\n\t\t\tcost: totalCost,\n\t\t\tcontextUsage: this.getContextUsage(),\n\t\t};\n\t}\n\n\tgetContextUsage(): ContextUsage | undefined {\n\t\tconst model = this.model;\n\t\tif (!model) return undefined;\n\n\t\tconst contextWindow = model.contextWindow ?? 0;\n\t\tif (contextWindow <= 0) return undefined;\n\n\t\t// After compaction, the last assistant usage reflects pre-compaction context size.\n\t\t// We can only trust usage from an assistant that responded after the latest compaction.\n\t\t// If no such assistant exists, context token count is unknown until the next LLM response.\n\t\tconst branchEntries = this.sessionManager.getBranch();\n\t\tconst latestCompaction = getLatestCompactionEntry(branchEntries);\n\n\t\tif (latestCompaction) {\n\t\t\t// Check if there's a valid assistant usage after the compaction boundary\n\t\t\tconst compactionIndex = branchEntries.lastIndexOf(latestCompaction);\n\t\t\tlet hasPostCompactionUsage = false;\n\t\t\tfor (let i = branchEntries.length - 1; i > compactionIndex; i--) {\n\t\t\t\tconst entry = branchEntries[i];\n\t\t\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\t\t\tconst assistant = entry.message;\n\t\t\t\t\tif (assistant.stopReason !== \"aborted\" && assistant.stopReason !== \"error\") {\n\t\t\t\t\t\tconst contextTokens = calculateContextTokens(assistant.usage);\n\t\t\t\t\t\tif (contextTokens > 0) {\n\t\t\t\t\t\t\thasPostCompactionUsage = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!hasPostCompactionUsage) {\n\t\t\t\treturn { tokens: null, contextWindow, percent: null };\n\t\t\t}\n\t\t}\n\n\t\tconst estimate = estimateContextTokens(this.messages);\n\t\tconst percent = (estimate.tokens / contextWindow) * 100;\n\n\t\treturn {\n\t\t\ttokens: estimate.tokens,\n\t\t\tcontextWindow,\n\t\t\tpercent,\n\t\t};\n\t}\n\n\t/**\n\t * Export session to HTML.\n\t * @param outputPath Optional output path (defaults to session directory)\n\t * @returns Path to exported file\n\t */\n\tasync exportToHtml(outputPath?: string): Promise<string> {\n\t\tconst themeName = this.settingsManager.getTheme();\n\n\t\t// Create tool renderer if we have an extension runner (for custom tool HTML rendering)\n\t\tconst toolRenderer: ToolHtmlRenderer = createToolHtmlRenderer({\n\t\t\tgetToolDefinition: (name) => this.getToolDefinition(name),\n\t\t\ttheme,\n\t\t\tcwd: this.sessionManager.getCwd(),\n\t\t});\n\n\t\treturn await exportSessionToHtml(this.sessionManager, this.state, {\n\t\t\toutputPath,\n\t\t\tthemeName,\n\t\t\ttoolRenderer,\n\t\t});\n\t}\n\n\t/**\n\t * Export the current session branch to a JSONL file.\n\t * Writes the session header followed by all entries on the current branch path.\n\t * @param outputPath Target file path. If omitted, generates a timestamped file in cwd.\n\t * @returns The resolved output file path.\n\t */\n\texportToJsonl(outputPath?: string): string {\n\t\tconst filePath = resolve(outputPath ?? `session-${new Date().toISOString().replace(/[:.]/g, \"-\")}.jsonl`);\n\t\tconst dir = dirname(filePath);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst header: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: CURRENT_SESSION_VERSION,\n\t\t\tid: this.sessionManager.getSessionId(),\n\t\t\ttimestamp: new Date().toISOString(),\n\t\t\tcwd: this.sessionManager.getCwd(),\n\t\t};\n\n\t\tconst branchEntries = this.sessionManager.getBranch();\n\t\tconst lines = [JSON.stringify(header)];\n\n\t\t// Re-chain parentIds to form a linear sequence\n\t\tlet prevId: string | null = null;\n\t\tfor (const entry of branchEntries) {\n\t\t\tconst linear = { ...entry, parentId: prevId };\n\t\t\tlines.push(JSON.stringify(linear));\n\t\t\tprevId = entry.id;\n\t\t}\n\n\t\twriteFileSync(filePath, `${lines.join(\"\\n\")}\\n`);\n\t\treturn filePath;\n\t}\n\n\t// =========================================================================\n\t// Utilities\n\t// =========================================================================\n\n\t/**\n\t * Get text content of last assistant message.\n\t * Useful for /copy command.\n\t * @returns Text content, or undefined if no assistant message exists\n\t */\n\tgetLastAssistantText(): string | undefined {\n\t\tconst lastAssistant = this.messages\n\t\t\t.slice()\n\t\t\t.reverse()\n\t\t\t.find((m) => {\n\t\t\t\tif (m.role !== \"assistant\") return false;\n\t\t\t\tconst msg = m as AssistantMessage;\n\t\t\t\t// Skip aborted messages with no content\n\t\t\t\tif (msg.stopReason === \"aborted\" && msg.content.length === 0) return false;\n\t\t\t\treturn true;\n\t\t\t});\n\n\t\tif (!lastAssistant) return undefined;\n\n\t\tlet text = \"\";\n\t\tfor (const content of (lastAssistant as AssistantMessage).content) {\n\t\t\tif (content.type === \"text\") {\n\t\t\t\ttext += content.text;\n\t\t\t}\n\t\t}\n\n\t\treturn text.trim() || undefined;\n\t}\n\n\t// =========================================================================\n\t// Extension System\n\t// =========================================================================\n\n\tcreateReplacedSessionContext(): ReplacedSessionContext {\n\t\tconst context = Object.defineProperties(\n\t\t\t{},\n\t\t\tObject.getOwnPropertyDescriptors(this._extensionRunner.createCommandContext()),\n\t\t) as ReplacedSessionContext;\n\t\tcontext.sendMessage = (message, options) => this.sendCustomMessage(message, options);\n\t\tcontext.sendUserMessage = (content, options) => this.sendUserMessage(content, options);\n\t\tcontext.dispatchUserInput = (input, options) => this.dispatchUserInput(input, options);\n\t\treturn context;\n\t}\n\n\t/**\n\t * Check if extensions have handlers for a specific event type.\n\t */\n\thasExtensionHandlers(eventType: string): boolean {\n\t\treturn this._extensionRunner.hasHandlers(eventType);\n\t}\n\n\t/**\n\t * Get the extension runner (for setting UI context and error handlers).\n\t */\n\tget extensionRunner(): ExtensionRunner {\n\t\treturn this._extensionRunner;\n\t}\n}\n"]}
|