@bastani/atomic 0.9.4-alpha.10 → 0.9.4-alpha.11

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/builtin/cursor/package.json +2 -2
  3. package/dist/builtin/intercom/package.json +1 -1
  4. package/dist/builtin/mcp/package.json +1 -1
  5. package/dist/builtin/subagents/CHANGELOG.md +7 -0
  6. package/dist/builtin/subagents/package.json +1 -1
  7. package/dist/builtin/subagents/src/slash/slash-commands.ts +2 -2
  8. package/dist/builtin/subagents/src/tui/render-result-compact.ts +3 -2
  9. package/dist/builtin/subagents/src/tui/render-result.ts +3 -3
  10. package/dist/builtin/subagents/src/tui/render-widget-graph.ts +4 -3
  11. package/dist/builtin/subagents/src/tui/render-widget.ts +10 -4
  12. package/dist/builtin/web-access/package.json +1 -1
  13. package/dist/builtin/workflows/CHANGELOG.md +6 -0
  14. package/dist/builtin/workflows/package.json +1 -1
  15. package/dist/builtin/workflows/src/tui/keybindings-adapter.ts +4 -0
  16. package/dist/builtin/workflows/src/tui/overlay-adapter.ts +4 -0
  17. package/dist/builtin/workflows/src/tui/stage-chat-view-input.ts +12 -1
  18. package/dist/builtin/workflows/src/tui/stage-chat-view-state.ts +2 -0
  19. package/dist/builtin/workflows/src/tui/stage-chat-view-types.ts +6 -0
  20. package/dist/builtin/workflows/src/tui/stage-chat-view.ts +4 -0
  21. package/dist/builtin/workflows/src/tui/workflow-attach-pane-types.ts +4 -0
  22. package/dist/builtin/workflows/src/tui/workflow-attach-pane.ts +8 -9
  23. package/dist/core/agent-session-events.d.ts.map +1 -1
  24. package/dist/core/agent-session-events.js +2 -0
  25. package/dist/core/agent-session-events.js.map +1 -1
  26. package/dist/core/agent-session-methods.d.ts +4 -1
  27. package/dist/core/agent-session-methods.d.ts.map +1 -1
  28. package/dist/core/agent-session-methods.js.map +1 -1
  29. package/dist/core/agent-session-tool-registry.d.ts.map +1 -1
  30. package/dist/core/agent-session-tool-registry.js +4 -0
  31. package/dist/core/agent-session-tool-registry.js.map +1 -1
  32. package/dist/core/agent-session.d.ts +4 -1
  33. package/dist/core/agent-session.d.ts.map +1 -1
  34. package/dist/core/agent-session.js +4 -0
  35. package/dist/core/agent-session.js.map +1 -1
  36. package/dist/core/async/format.d.ts +3 -0
  37. package/dist/core/async/format.d.ts.map +1 -0
  38. package/dist/core/async/format.js +50 -0
  39. package/dist/core/async/format.js.map +1 -0
  40. package/dist/core/async/job-manager.d.ts +39 -0
  41. package/dist/core/async/job-manager.d.ts.map +1 -0
  42. package/dist/core/async/job-manager.js +254 -0
  43. package/dist/core/async/job-manager.js.map +1 -0
  44. package/dist/core/async/session-manager.d.ts +18 -0
  45. package/dist/core/async/session-manager.d.ts.map +1 -0
  46. package/dist/core/async/session-manager.js +61 -0
  47. package/dist/core/async/session-manager.js.map +1 -0
  48. package/dist/core/async/types.d.ts +23 -0
  49. package/dist/core/async/types.d.ts.map +1 -0
  50. package/dist/core/async/types.js +2 -0
  51. package/dist/core/async/types.js.map +1 -0
  52. package/dist/core/tools/bash-async-execution.d.ts +25 -0
  53. package/dist/core/tools/bash-async-execution.d.ts.map +1 -0
  54. package/dist/core/tools/bash-async-execution.js +71 -0
  55. package/dist/core/tools/bash-async-execution.js.map +1 -0
  56. package/dist/core/tools/bash-async-jobs.d.ts +10 -0
  57. package/dist/core/tools/bash-async-jobs.d.ts.map +1 -1
  58. package/dist/core/tools/bash-async-jobs.js +18 -2
  59. package/dist/core/tools/bash-async-jobs.js.map +1 -1
  60. package/dist/core/tools/bash-async-output.d.ts +3 -1
  61. package/dist/core/tools/bash-async-output.d.ts.map +1 -1
  62. package/dist/core/tools/bash-async-output.js +4 -1
  63. package/dist/core/tools/bash-async-output.js.map +1 -1
  64. package/dist/core/tools/bash.d.ts +10 -4
  65. package/dist/core/tools/bash.d.ts.map +1 -1
  66. package/dist/core/tools/bash.js +20 -38
  67. package/dist/core/tools/bash.js.map +1 -1
  68. package/docs/tools.md +4 -2
  69. package/npm-shrinkwrap.json +23 -23
  70. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACX,KAAK,EACL,SAAS,EACT,aAAa,EACb,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAQnE,OAAO,KAAK,EAA+B,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AASzG,OAAO,KAAK,EACX,kBAAkB,EAClB,yBAAyB,EACzB,kBAAkB,EAClB,mBAAmB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,8BAA8B,EAC9B,sBAAsB,EACtB,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,EACd,MAAM,uBAAuB,CAAC;AAE/B,YAAY,EACX,kBAAkB,EAClB,iBAAiB,EACjB,yBAAyB,EACzB,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,YAAY,GACZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gCA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ACnD,SAAS,CAAC,cAAc,EAAE,aAAa,CAAC;IACxC,SAAS,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAa;IAC5D,SAAS,CAAC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAa;IACzE,SAAS,CAAC,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAa;IAC/D,SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAa;IACnE,SAAS,CAAC,iBAAiB,SAAM;IACjC,SAAS,CAAC,wBAAwB,EAAG,wBAAwB,CAAC;IAC9D,SAAS,CAAC,qBAAqB,CAAC,EAAE,MAAM,CAAC;IACzC,SAAS,CAAC,qBAAqB,EAAE,gBAAgB,GAAG,SAAS,CAAa;IAE1E,YAAY,MAAM,EAAE,kBAAkB,EA0BrC;CACD;AAED,MAAM,WAAW,YAAa,SAAQ,yBAAyB;CAAG","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * The historical import path is preserved here as a facade. Responsibilities are\n * implemented in sibling modules by lifecycle area so each authored source file\n * stays below the repository file-length gate.\n */\n\nimport type {\n\tAgent,\n\tAgentTool,\n\tThinkingLevel,\n} from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model } from \"@earendil-works/pi-ai/compat\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport type { ModelRegistry } from \"./model-registry.ts\";\nimport type { ResourceLoader } from \"./resource-loader.ts\";\nimport type { SessionManager } from \"./session-manager.ts\";\nimport type { SettingsManager } from \"./settings-manager.ts\";\nimport type { BuildSystemPromptOptions } from \"./system-prompt.ts\";\nimport { installAgentSessionAccessors } from \"./agent-session-accessors.ts\";\nimport { agentSessionAutoCompactionMethods } from \"./agent-session-auto-compaction.ts\";\nimport { agentSessionBashMethods } from \"./agent-session-bash.ts\";\nimport { agentSessionCompactionMethods } from \"./agent-session-compaction.ts\";\nimport { agentSessionEventsMethods } from \"./agent-session-events.ts\";\nimport { agentSessionExportMethods } from \"./agent-session-export.ts\";\nimport { agentSessionExtensionBindingsMethods } from \"./agent-session-extension-bindings.ts\";\nimport type { AgentSessionInternalSurface, AgentSessionPublicSurface } from \"./agent-session-methods.ts\";\nimport { agentSessionMessageQueueMethods } from \"./agent-session-message-queue.ts\";\nimport { agentSessionModelsMethods } from \"./agent-session-models.ts\";\nimport { agentSessionPromptMethods } from \"./agent-session-prompt.ts\";\nimport { agentSessionRetryMethods } from \"./agent-session-retry.ts\";\nimport { agentSessionStateMethods } from \"./agent-session-state.ts\";\nimport { agentSessionToolHooksMethods } from \"./agent-session-tool-hooks.ts\";\nimport { agentSessionToolRegistryMethods } from \"./agent-session-tool-registry.ts\";\nimport { agentSessionTreeMethods } from \"./agent-session-tree.ts\";\nimport type {\n\tAgentSessionConfig,\n\tAgentSessionEventListener,\n\tInterruptQueueHold,\n\tToolDefinitionEntry,\n} from \"./agent-session-types.ts\";\nimport type {\n\tExtensionCommandContextActions,\n\tExtensionErrorListener,\n\tExtensionMode,\n\tExtensionRunner,\n\tExtensionUIContext,\n\tOrchestrationContext,\n\tSessionStartEvent,\n\tToolDefinition,\n} from \"./extensions/index.ts\";\n\nexport type {\n\tAgentSessionConfig,\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tExtensionBindings,\n\tModelCycleResult,\n\tPromptOptions,\n\tSessionStats,\n} from \"./agent-session-types.ts\";\nexport { parseSkillBlock } from \"./agent-session-skill-block.ts\";\nexport type { ParsedSkillBlock } from \"./agent-session-skill-block.ts\";\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\n\tprotected _scopedModels: Array<{ model: Model<Api>; thinkingLevel?: ThinkingLevel }>;\n\tprotected _unsubscribeAgent?: () => void;\n\tprotected _eventListeners: AgentSessionEventListener[] = [];\n\tprotected _agentEventQueue: Promise<void> = Promise.resolve();\n\tprotected _steeringMessages: string[] = [];\n\tprotected _followUpMessages: string[] = [];\n\tprotected _interruptDeliveryQueue: Promise<void> = Promise.resolve();\n\tprotected _pendingInterruptDeliveries = 0;\n\tprotected _activeInterruptQueueHold: InterruptQueueHold | undefined = undefined;\n\tprotected _activeInterruptAbortMessage: string | undefined = undefined;\n\tprotected _pendingNextTurnMessages: CustomMessage[] = [];\n\tprotected _compactionAbortController: AbortController | undefined = undefined;\n\tprotected _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprotected _overflowRecoveryAttempted = false;\n\tprotected _branchSummaryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAttempt = 0;\n\tprotected _retryPromise: Promise<void> | undefined = undefined;\n\tprotected _retryResolve: (() => void) | undefined = undefined;\n\tprotected _bashAbortController: AbortController | undefined = undefined;\n\tprotected _pendingBashMessages: BashExecutionMessage[] = [];\n\tprotected _extensionRunner!: ExtensionRunner;\n\tprotected _turnIndex = 0;\n\tprotected _resourceLoader: ResourceLoader;\n\tprotected _customTools: ToolDefinition[];\n\tprotected _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprotected _cwd: string;\n\tprotected _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprotected _initialActiveToolNames?: string[];\n\tprotected _allowedToolNames?: Set<string>;\n\tprotected _excludedToolNames?: Set<string>;\n\tprotected _baseToolsOverride?: Record<string, AgentTool>;\n\tprotected _sessionStartEvent: SessionStartEvent;\n\tprotected _orchestrationContext?: OrchestrationContext;\n\tprotected _extensionUIContext?: ExtensionUIContext;\n\tprotected _extensionMode: ExtensionMode = \"print\";\n\tprotected _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprotected _extensionShutdownHandler?: () => void;\n\tprotected _extensionErrorListener?: ExtensionErrorListener;\n\tprotected _extensionErrorUnsubscriber?: () => void;\n\tprotected _modelRegistry: ModelRegistry;\n\tprotected _toolRegistry: Map<string, AgentTool> = new Map();\n\tprotected _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprotected _toolPromptSnippets: Map<string, string> = new Map();\n\tprotected _toolPromptGuidelines: Map<string, string[]> = new Map();\n\tprotected _baseSystemPrompt = \"\";\n\tprotected _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\tprotected _systemPromptOverride?: string;\n\tprotected _lastAssistantMessage: AssistantMessage | undefined = undefined;\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._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._excludedToolNames = config.excludedToolNames ? new Set(config.excludedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\t\tthis._orchestrationContext = config.orchestrationContext;\n\n\t\tconst internals = this as unknown as AgentSessionInternalSurface;\n\t\tinternals._handleAgentEvent = internals._handleAgentEvent.bind(this);\n\t\tthis._unsubscribeAgent = this.agent.subscribe(internals._handleAgentEvent);\n\t\tinternals._installAgentToolHooks();\n\t\tinternals._installAgentNextTurnRefresh();\n\t\tinternals._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n}\n\nexport interface AgentSession extends AgentSessionPublicSurface {}\n\ninstallAgentSessionAccessors(AgentSession.prototype as unknown as AgentSessionInternalSurface);\nObject.assign(\n\tAgentSession.prototype,\n\tagentSessionToolHooksMethods,\n\tagentSessionEventsMethods,\n\tagentSessionStateMethods,\n\tagentSessionPromptMethods,\n\tagentSessionMessageQueueMethods,\n\tagentSessionModelsMethods,\n\tagentSessionCompactionMethods,\n\tagentSessionAutoCompactionMethods,\n\tagentSessionExtensionBindingsMethods,\n\tagentSessionToolRegistryMethods,\n\tagentSessionRetryMethods,\n\tagentSessionBashMethods,\n\tagentSessionTreeMethods,\n\tagentSessionExportMethods,\n);\n"]}
1
+ {"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACX,KAAK,EACL,SAAS,EACT,aAAa,EACb,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAS9D,OAAO,KAAK,EAA+B,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AASzG,OAAO,KAAK,EACX,kBAAkB,EAClB,yBAAyB,EACzB,kBAAkB,EAClB,mBAAmB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,8BAA8B,EAC9B,sBAAsB,EACtB,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,oBAAoB,EACpB,i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CAAC;IACnD,SAAS,CAAC,cAAc,EAAE,aAAa,CAAW;IAClD,SAAS,CAAC,+BAA+B,CAAC,EAAE,8BAA8B,CAAC;IAC3E,SAAS,CAAC,yBAAyB,CAAC,EAAE,MAAM,IAAI,CAAC;IACjD,SAAS,CAAC,uBAAuB,CAAC,EAAE,sBAAsB,CAAC;IAC3D,SAAS,CAAC,2BAA2B,CAAC,EAAE,MAAM,IAAI,CAAC;IACnD,SAAS,CAAC,cAAc,EAAE,aAAa,CAAC;IACxC,SAAS,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAa;IAC5D,SAAS,CAAC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAa;IACzE,SAAS,CAAC,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAa;IAC/D,SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAa;IACnE,SAAS,CAAC,iBAAiB,SAAM;IACjC,SAAS,CAAC,wBAAwB,EAAG,wBAAwB,CAAC;IAC9D,SAAS,CAAC,qBAAqB,CAAC,EAAE,MAAM,CAAC;IACzC,SAAS,CAAC,qBAAqB,EAAE,gBAAgB,GAAG,SAAS,CAAa;IAC1E,SAAS,CAAC,gBAAgB,EAAE,eAAe,CAAC;IAC5C,SAAS,CAAC,yBAAyB,EAAE,MAAM,CAAC;IAC5C,YAAY,MAAM,EAAE,kBAAkB,EA4BrC;CACD;AAED,MAAM,WAAW,YAAa,SAAQ,yBAAyB;CAAG","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * The historical import path is preserved here as a facade. Responsibilities are\n * implemented in sibling modules by lifecycle area so each authored source file\n * stays below the repository file-length gate.\n */\n\nimport type {\n\tAgent,\n\tAgentTool,\n\tThinkingLevel,\n} from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model } from \"@earendil-works/pi-ai/compat\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport type { ModelRegistry } from \"./model-registry.ts\";\nimport type { ResourceLoader } from \"./resource-loader.ts\";\nimport type { SessionManager } from \"./session-manager.js\";\nimport type { SettingsManager } from \"./settings-manager.ts\";\nimport type { BuildSystemPromptOptions } from \"./system-prompt.ts\";\nimport type { AsyncJobManager } from \"./async/job-manager.js\";\nimport { createSessionAsyncJobManager } from \"./async/session-manager.js\";\nimport { installAgentSessionAccessors } from \"./agent-session-accessors.ts\";\nimport { agentSessionAutoCompactionMethods } from \"./agent-session-auto-compaction.ts\";\nimport { agentSessionBashMethods } from \"./agent-session-bash.ts\";\nimport { agentSessionCompactionMethods } from \"./agent-session-compaction.ts\";\nimport { agentSessionEventsMethods } from \"./agent-session-events.ts\";\nimport { agentSessionExportMethods } from \"./agent-session-export.ts\";\nimport { agentSessionExtensionBindingsMethods } from \"./agent-session-extension-bindings.ts\";\nimport type { AgentSessionInternalSurface, AgentSessionPublicSurface } from \"./agent-session-methods.ts\";\nimport { agentSessionMessageQueueMethods } from \"./agent-session-message-queue.ts\";\nimport { agentSessionModelsMethods } from \"./agent-session-models.ts\";\nimport { agentSessionPromptMethods } from \"./agent-session-prompt.ts\";\nimport { agentSessionRetryMethods } from \"./agent-session-retry.ts\";\nimport { agentSessionStateMethods } from \"./agent-session-state.ts\";\nimport { agentSessionToolHooksMethods } from \"./agent-session-tool-hooks.ts\";\nimport { agentSessionToolRegistryMethods } from \"./agent-session-tool-registry.ts\";\nimport { agentSessionTreeMethods } from \"./agent-session-tree.ts\";\nimport type {\n\tAgentSessionConfig,\n\tAgentSessionEventListener,\n\tInterruptQueueHold,\n\tToolDefinitionEntry,\n} from \"./agent-session-types.ts\";\nimport type {\n\tExtensionCommandContextActions,\n\tExtensionErrorListener,\n\tExtensionMode,\n\tExtensionRunner,\n\tExtensionUIContext,\n\tOrchestrationContext,\n\tSessionStartEvent,\n\tToolDefinition,\n} from \"./extensions/index.ts\";\n\nexport type {\n\tAgentSessionConfig,\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tExtensionBindings,\n\tModelCycleResult,\n\tPromptOptions,\n\tSessionStats,\n} from \"./agent-session-types.ts\";\nexport { parseSkillBlock } from \"./agent-session-skill-block.ts\";\nexport type { ParsedSkillBlock } from \"./agent-session-skill-block.ts\";\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\n\tprotected _scopedModels: Array<{ model: Model<Api>; thinkingLevel?: ThinkingLevel }>;\n\tprotected _unsubscribeAgent?: () => void;\n\tprotected _eventListeners: AgentSessionEventListener[] = [];\n\tprotected _agentEventQueue: Promise<void> = Promise.resolve();\n\tprotected _steeringMessages: string[] = [];\n\tprotected _followUpMessages: string[] = [];\n\tprotected _interruptDeliveryQueue: Promise<void> = Promise.resolve();\n\tprotected _pendingInterruptDeliveries = 0;\n\tprotected _activeInterruptQueueHold: InterruptQueueHold | undefined = undefined;\n\tprotected _activeInterruptAbortMessage: string | undefined = undefined;\n\tprotected _pendingNextTurnMessages: CustomMessage[] = [];\n\tprotected _compactionAbortController: AbortController | undefined = undefined;\n\tprotected _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprotected _overflowRecoveryAttempted = false;\n\tprotected _branchSummaryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAttempt = 0;\n\tprotected _retryPromise: Promise<void> | undefined = undefined;\n\tprotected _retryResolve: (() => void) | undefined = undefined;\n\tprotected _bashAbortController: AbortController | undefined = undefined;\n\tprotected _pendingBashMessages: BashExecutionMessage[] = [];\n\tprotected _extensionRunner!: ExtensionRunner;\n\tprotected _turnIndex = 0;\n\tprotected _resourceLoader: ResourceLoader;\n\tprotected _customTools: ToolDefinition[];\n\tprotected _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprotected _cwd: string;\n\tprotected _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprotected _initialActiveToolNames?: string[];\n\tprotected _allowedToolNames?: Set<string>;\n\tprotected _excludedToolNames?: Set<string>;\n\tprotected _baseToolsOverride?: Record<string, AgentTool>;\n\tprotected _sessionStartEvent: SessionStartEvent;\n\tprotected _orchestrationContext?: OrchestrationContext;\n\tprotected _extensionUIContext?: ExtensionUIContext;\n\tprotected _extensionMode: ExtensionMode = \"print\";\n\tprotected _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprotected _extensionShutdownHandler?: () => void;\n\tprotected _extensionErrorListener?: ExtensionErrorListener;\n\tprotected _extensionErrorUnsubscriber?: () => void;\n\tprotected _modelRegistry: ModelRegistry;\n\tprotected _toolRegistry: Map<string, AgentTool> = new Map();\n\tprotected _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprotected _toolPromptSnippets: Map<string, string> = new Map();\n\tprotected _toolPromptGuidelines: Map<string, string[]> = new Map();\n\tprotected _baseSystemPrompt = \"\";\n\tprotected _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\tprotected _systemPromptOverride?: string;\n\tprotected _lastAssistantMessage: AssistantMessage | undefined = undefined;\n\tprotected _asyncJobManager: AsyncJobManager;\n\tprotected _asyncJobManagerSessionId: symbol;\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._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._excludedToolNames = config.excludedToolNames ? new Set(config.excludedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\t\tthis._orchestrationContext = config.orchestrationContext;\n\t\tconst internals = this as unknown as AgentSessionInternalSurface;\n\t\tconst asyncJobManagerHandle = createSessionAsyncJobManager(internals);\n\t\tthis._asyncJobManager = asyncJobManagerHandle.manager;\n\t\tthis._asyncJobManagerSessionId = asyncJobManagerHandle.sessionId;\n\t\tinternals._handleAgentEvent = internals._handleAgentEvent.bind(this);\n\t\tthis._unsubscribeAgent = this.agent.subscribe(internals._handleAgentEvent);\n\t\tinternals._installAgentToolHooks();\n\t\tinternals._installAgentNextTurnRefresh();\n\t\tinternals._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n}\n\nexport interface AgentSession extends AgentSessionPublicSurface {}\n\ninstallAgentSessionAccessors(AgentSession.prototype as unknown as AgentSessionInternalSurface);\nObject.assign(\n\tAgentSession.prototype,\n\tagentSessionToolHooksMethods,\n\tagentSessionEventsMethods,\n\tagentSessionStateMethods,\n\tagentSessionPromptMethods,\n\tagentSessionMessageQueueMethods,\n\tagentSessionModelsMethods,\n\tagentSessionCompactionMethods,\n\tagentSessionAutoCompactionMethods,\n\tagentSessionExtensionBindingsMethods,\n\tagentSessionToolRegistryMethods,\n\tagentSessionRetryMethods,\n\tagentSessionBashMethods,\n\tagentSessionTreeMethods,\n\tagentSessionExportMethods,\n);\n"]}
@@ -5,6 +5,7 @@
5
5
  * implemented in sibling modules by lifecycle area so each authored source file
6
6
  * stays below the repository file-length gate.
7
7
  */
8
+ import { createSessionAsyncJobManager } from "./async/session-manager.js";
8
9
  import { installAgentSessionAccessors } from "./agent-session-accessors.js";
9
10
  import { agentSessionAutoCompactionMethods } from "./agent-session-auto-compaction.js";
10
11
  import { agentSessionBashMethods } from "./agent-session-bash.js";
@@ -67,6 +68,9 @@ export class AgentSession {
67
68
  this._sessionStartEvent = config.sessionStartEvent ?? { type: "session_start", reason: "startup" };
68
69
  this._orchestrationContext = config.orchestrationContext;
69
70
  const internals = this;
71
+ const asyncJobManagerHandle = createSessionAsyncJobManager(internals);
72
+ this._asyncJobManager = asyncJobManagerHandle.manager;
73
+ this._asyncJobManagerSessionId = asyncJobManagerHandle.sessionId;
70
74
  internals._handleAgentEvent = internals._handleAgentEvent.bind(this);
71
75
  this._unsubscribeAgent = this.agent.subscribe(internals._handleAgentEvent);
72
76
  internals._installAgentToolHooks();
@@ -1 +1 @@
1
- {"version":3,"file":"agent-session.js","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAcH,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAC5E,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,oCAAoC,EAAE,MAAM,uCAAuC,CAAC;AAE7F,OAAO,EAAE,+BAA+B,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAC;AAC7E,OAAO,EAAE,+BAA+B,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AA2BlE,OAAO,EAAE,eAAe,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CAAC,sBAAsB,EAAE,CAAC;QACnC,SAAS,CAAC,4BAA4B,EAAE,CAAC;QACzC,SAAS,CAAC,aAAa,CAAC;YACvB,eAAe,EAAE,IAAI,CAAC,uBAAuB;YAC7C,wBAAwB,EAAE,IAAI;SAC9B,CAAC,CAAC;IACJ,CAAC;CACD;AAID,4BAA4B,CAAC,YAAY,CAAC,SAAmD,CAAC,CAAC;AAC/F,MAAM,CAAC,MAAM,CACZ,YAAY,CAAC,SAAS,EACtB,4BAA4B,EAC5B,yBAAyB,EACzB,wBAAwB,EACxB,yBAAyB,EACzB,+BAA+B,EAC/B,yBAAyB,EACzB,6BAA6B,EAC7B,iCAAiC,EACjC,oCAAoC,EACpC,+BAA+B,EAC/B,wBAAwB,EACxB,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,CACzB,CAAC","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * The historical import path is preserved here as a facade. Responsibilities are\n * implemented in sibling modules by lifecycle area so each authored source file\n * stays below the repository file-length gate.\n */\n\nimport type {\n\tAgent,\n\tAgentTool,\n\tThinkingLevel,\n} from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model } from \"@earendil-works/pi-ai/compat\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport type { ModelRegistry } from \"./model-registry.ts\";\nimport type { ResourceLoader } from \"./resource-loader.ts\";\nimport type { SessionManager } from \"./session-manager.ts\";\nimport type { SettingsManager } from \"./settings-manager.ts\";\nimport type { BuildSystemPromptOptions } from \"./system-prompt.ts\";\nimport { installAgentSessionAccessors } from \"./agent-session-accessors.ts\";\nimport { agentSessionAutoCompactionMethods } from \"./agent-session-auto-compaction.ts\";\nimport { agentSessionBashMethods } from \"./agent-session-bash.ts\";\nimport { agentSessionCompactionMethods } from \"./agent-session-compaction.ts\";\nimport { agentSessionEventsMethods } from \"./agent-session-events.ts\";\nimport { agentSessionExportMethods } from \"./agent-session-export.ts\";\nimport { agentSessionExtensionBindingsMethods } from \"./agent-session-extension-bindings.ts\";\nimport type { AgentSessionInternalSurface, AgentSessionPublicSurface } from \"./agent-session-methods.ts\";\nimport { agentSessionMessageQueueMethods } from \"./agent-session-message-queue.ts\";\nimport { agentSessionModelsMethods } from \"./agent-session-models.ts\";\nimport { agentSessionPromptMethods } from \"./agent-session-prompt.ts\";\nimport { agentSessionRetryMethods } from \"./agent-session-retry.ts\";\nimport { agentSessionStateMethods } from \"./agent-session-state.ts\";\nimport { agentSessionToolHooksMethods } from \"./agent-session-tool-hooks.ts\";\nimport { agentSessionToolRegistryMethods } from \"./agent-session-tool-registry.ts\";\nimport { agentSessionTreeMethods } from \"./agent-session-tree.ts\";\nimport type {\n\tAgentSessionConfig,\n\tAgentSessionEventListener,\n\tInterruptQueueHold,\n\tToolDefinitionEntry,\n} from \"./agent-session-types.ts\";\nimport type {\n\tExtensionCommandContextActions,\n\tExtensionErrorListener,\n\tExtensionMode,\n\tExtensionRunner,\n\tExtensionUIContext,\n\tOrchestrationContext,\n\tSessionStartEvent,\n\tToolDefinition,\n} from \"./extensions/index.ts\";\n\nexport type {\n\tAgentSessionConfig,\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tExtensionBindings,\n\tModelCycleResult,\n\tPromptOptions,\n\tSessionStats,\n} from \"./agent-session-types.ts\";\nexport { parseSkillBlock } from \"./agent-session-skill-block.ts\";\nexport type { ParsedSkillBlock } from \"./agent-session-skill-block.ts\";\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\n\tprotected _scopedModels: Array<{ model: Model<Api>; thinkingLevel?: ThinkingLevel }>;\n\tprotected _unsubscribeAgent?: () => void;\n\tprotected _eventListeners: AgentSessionEventListener[] = [];\n\tprotected _agentEventQueue: Promise<void> = Promise.resolve();\n\tprotected _steeringMessages: string[] = [];\n\tprotected _followUpMessages: string[] = [];\n\tprotected _interruptDeliveryQueue: Promise<void> = Promise.resolve();\n\tprotected _pendingInterruptDeliveries = 0;\n\tprotected _activeInterruptQueueHold: InterruptQueueHold | undefined = undefined;\n\tprotected _activeInterruptAbortMessage: string | undefined = undefined;\n\tprotected _pendingNextTurnMessages: CustomMessage[] = [];\n\tprotected _compactionAbortController: AbortController | undefined = undefined;\n\tprotected _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprotected _overflowRecoveryAttempted = false;\n\tprotected _branchSummaryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAttempt = 0;\n\tprotected _retryPromise: Promise<void> | undefined = undefined;\n\tprotected _retryResolve: (() => void) | undefined = undefined;\n\tprotected _bashAbortController: AbortController | undefined = undefined;\n\tprotected _pendingBashMessages: BashExecutionMessage[] = [];\n\tprotected _extensionRunner!: ExtensionRunner;\n\tprotected _turnIndex = 0;\n\tprotected _resourceLoader: ResourceLoader;\n\tprotected _customTools: ToolDefinition[];\n\tprotected _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprotected _cwd: string;\n\tprotected _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprotected _initialActiveToolNames?: string[];\n\tprotected _allowedToolNames?: Set<string>;\n\tprotected _excludedToolNames?: Set<string>;\n\tprotected _baseToolsOverride?: Record<string, AgentTool>;\n\tprotected _sessionStartEvent: SessionStartEvent;\n\tprotected _orchestrationContext?: OrchestrationContext;\n\tprotected _extensionUIContext?: ExtensionUIContext;\n\tprotected _extensionMode: ExtensionMode = \"print\";\n\tprotected _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprotected _extensionShutdownHandler?: () => void;\n\tprotected _extensionErrorListener?: ExtensionErrorListener;\n\tprotected _extensionErrorUnsubscriber?: () => void;\n\tprotected _modelRegistry: ModelRegistry;\n\tprotected _toolRegistry: Map<string, AgentTool> = new Map();\n\tprotected _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprotected _toolPromptSnippets: Map<string, string> = new Map();\n\tprotected _toolPromptGuidelines: Map<string, string[]> = new Map();\n\tprotected _baseSystemPrompt = \"\";\n\tprotected _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\tprotected _systemPromptOverride?: string;\n\tprotected _lastAssistantMessage: AssistantMessage | undefined = undefined;\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._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._excludedToolNames = config.excludedToolNames ? new Set(config.excludedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\t\tthis._orchestrationContext = config.orchestrationContext;\n\n\t\tconst internals = this as unknown as AgentSessionInternalSurface;\n\t\tinternals._handleAgentEvent = internals._handleAgentEvent.bind(this);\n\t\tthis._unsubscribeAgent = this.agent.subscribe(internals._handleAgentEvent);\n\t\tinternals._installAgentToolHooks();\n\t\tinternals._installAgentNextTurnRefresh();\n\t\tinternals._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n}\n\nexport interface AgentSession extends AgentSessionPublicSurface {}\n\ninstallAgentSessionAccessors(AgentSession.prototype as unknown as AgentSessionInternalSurface);\nObject.assign(\n\tAgentSession.prototype,\n\tagentSessionToolHooksMethods,\n\tagentSessionEventsMethods,\n\tagentSessionStateMethods,\n\tagentSessionPromptMethods,\n\tagentSessionMessageQueueMethods,\n\tagentSessionModelsMethods,\n\tagentSessionCompactionMethods,\n\tagentSessionAutoCompactionMethods,\n\tagentSessionExtensionBindingsMethods,\n\tagentSessionToolRegistryMethods,\n\tagentSessionRetryMethods,\n\tagentSessionBashMethods,\n\tagentSessionTreeMethods,\n\tagentSessionExportMethods,\n);\n"]}
1
+ {"version":3,"file":"agent-session.js","sourceRoot":"","sources":["../../src/core/agent-session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAeH,OAAO,EAAE,4BAA4B,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAC5E,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,oCAAoC,EAAE,MAAM,uCAAuC,CAAC;AAE7F,OAAO,EAAE,+BAA+B,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAC;AAC7E,OAAO,EAAE,+BAA+B,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AA2BlE,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAGjE,MAAM,OAAO,YAAY;IAwDxB,YAAY,MAA0B;QAj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C,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC;QAC3E,SAAS,CAAC,sBAAsB,EAAE,CAAC;QACnC,SAAS,CAAC,4BAA4B,EAAE,CAAC;QACzC,SAAS,CAAC,aAAa,CAAC;YACvB,eAAe,EAAE,IAAI,CAAC,uBAAuB;YAC7C,wBAAwB,EAAE,IAAI;SAC9B,CAAC,CAAC;IACJ,CAAC;CACD;AAID,4BAA4B,CAAC,YAAY,CAAC,SAAmD,CAAC,CAAC;AAC/F,MAAM,CAAC,MAAM,CACZ,YAAY,CAAC,SAAS,EACtB,4BAA4B,EAC5B,yBAAyB,EACzB,wBAAwB,EACxB,yBAAyB,EACzB,+BAA+B,EAC/B,yBAAyB,EACzB,6BAA6B,EAC7B,iCAAiC,EACjC,oCAAoC,EACpC,+BAA+B,EAC/B,wBAAwB,EACxB,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,CACzB,CAAC","sourcesContent":["/**\n * AgentSession - Core abstraction for agent lifecycle and session management.\n *\n * The historical import path is preserved here as a facade. Responsibilities are\n * implemented in sibling modules by lifecycle area so each authored source file\n * stays below the repository file-length gate.\n */\n\nimport type {\n\tAgent,\n\tAgentTool,\n\tThinkingLevel,\n} from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model } from \"@earendil-works/pi-ai/compat\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport type { ModelRegistry } from \"./model-registry.ts\";\nimport type { ResourceLoader } from \"./resource-loader.ts\";\nimport type { SessionManager } from \"./session-manager.js\";\nimport type { SettingsManager } from \"./settings-manager.ts\";\nimport type { BuildSystemPromptOptions } from \"./system-prompt.ts\";\nimport type { AsyncJobManager } from \"./async/job-manager.js\";\nimport { createSessionAsyncJobManager } from \"./async/session-manager.js\";\nimport { installAgentSessionAccessors } from \"./agent-session-accessors.ts\";\nimport { agentSessionAutoCompactionMethods } from \"./agent-session-auto-compaction.ts\";\nimport { agentSessionBashMethods } from \"./agent-session-bash.ts\";\nimport { agentSessionCompactionMethods } from \"./agent-session-compaction.ts\";\nimport { agentSessionEventsMethods } from \"./agent-session-events.ts\";\nimport { agentSessionExportMethods } from \"./agent-session-export.ts\";\nimport { agentSessionExtensionBindingsMethods } from \"./agent-session-extension-bindings.ts\";\nimport type { AgentSessionInternalSurface, AgentSessionPublicSurface } from \"./agent-session-methods.ts\";\nimport { agentSessionMessageQueueMethods } from \"./agent-session-message-queue.ts\";\nimport { agentSessionModelsMethods } from \"./agent-session-models.ts\";\nimport { agentSessionPromptMethods } from \"./agent-session-prompt.ts\";\nimport { agentSessionRetryMethods } from \"./agent-session-retry.ts\";\nimport { agentSessionStateMethods } from \"./agent-session-state.ts\";\nimport { agentSessionToolHooksMethods } from \"./agent-session-tool-hooks.ts\";\nimport { agentSessionToolRegistryMethods } from \"./agent-session-tool-registry.ts\";\nimport { agentSessionTreeMethods } from \"./agent-session-tree.ts\";\nimport type {\n\tAgentSessionConfig,\n\tAgentSessionEventListener,\n\tInterruptQueueHold,\n\tToolDefinitionEntry,\n} from \"./agent-session-types.ts\";\nimport type {\n\tExtensionCommandContextActions,\n\tExtensionErrorListener,\n\tExtensionMode,\n\tExtensionRunner,\n\tExtensionUIContext,\n\tOrchestrationContext,\n\tSessionStartEvent,\n\tToolDefinition,\n} from \"./extensions/index.ts\";\n\nexport type {\n\tAgentSessionConfig,\n\tAgentSessionEvent,\n\tAgentSessionEventListener,\n\tExtensionBindings,\n\tModelCycleResult,\n\tPromptOptions,\n\tSessionStats,\n} from \"./agent-session-types.ts\";\nexport { parseSkillBlock } from \"./agent-session-skill-block.ts\";\nexport type { ParsedSkillBlock } from \"./agent-session-skill-block.ts\";\n\nexport class AgentSession {\n\treadonly agent: Agent;\n\treadonly sessionManager: SessionManager;\n\treadonly settingsManager: SettingsManager;\n\n\tprotected _scopedModels: Array<{ model: Model<Api>; thinkingLevel?: ThinkingLevel }>;\n\tprotected _unsubscribeAgent?: () => void;\n\tprotected _eventListeners: AgentSessionEventListener[] = [];\n\tprotected _agentEventQueue: Promise<void> = Promise.resolve();\n\tprotected _steeringMessages: string[] = [];\n\tprotected _followUpMessages: string[] = [];\n\tprotected _interruptDeliveryQueue: Promise<void> = Promise.resolve();\n\tprotected _pendingInterruptDeliveries = 0;\n\tprotected _activeInterruptQueueHold: InterruptQueueHold | undefined = undefined;\n\tprotected _activeInterruptAbortMessage: string | undefined = undefined;\n\tprotected _pendingNextTurnMessages: CustomMessage[] = [];\n\tprotected _compactionAbortController: AbortController | undefined = undefined;\n\tprotected _autoCompactionAbortController: AbortController | undefined = undefined;\n\tprotected _overflowRecoveryAttempted = false;\n\tprotected _branchSummaryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAbortController: AbortController | undefined = undefined;\n\tprotected _retryAttempt = 0;\n\tprotected _retryPromise: Promise<void> | undefined = undefined;\n\tprotected _retryResolve: (() => void) | undefined = undefined;\n\tprotected _bashAbortController: AbortController | undefined = undefined;\n\tprotected _pendingBashMessages: BashExecutionMessage[] = [];\n\tprotected _extensionRunner!: ExtensionRunner;\n\tprotected _turnIndex = 0;\n\tprotected _resourceLoader: ResourceLoader;\n\tprotected _customTools: ToolDefinition[];\n\tprotected _baseToolDefinitions: Map<string, ToolDefinition> = new Map();\n\tprotected _cwd: string;\n\tprotected _extensionRunnerRef?: { current?: ExtensionRunner };\n\tprotected _initialActiveToolNames?: string[];\n\tprotected _allowedToolNames?: Set<string>;\n\tprotected _excludedToolNames?: Set<string>;\n\tprotected _baseToolsOverride?: Record<string, AgentTool>;\n\tprotected _sessionStartEvent: SessionStartEvent;\n\tprotected _orchestrationContext?: OrchestrationContext;\n\tprotected _extensionUIContext?: ExtensionUIContext;\n\tprotected _extensionMode: ExtensionMode = \"print\";\n\tprotected _extensionCommandContextActions?: ExtensionCommandContextActions;\n\tprotected _extensionShutdownHandler?: () => void;\n\tprotected _extensionErrorListener?: ExtensionErrorListener;\n\tprotected _extensionErrorUnsubscriber?: () => void;\n\tprotected _modelRegistry: ModelRegistry;\n\tprotected _toolRegistry: Map<string, AgentTool> = new Map();\n\tprotected _toolDefinitions: Map<string, ToolDefinitionEntry> = new Map();\n\tprotected _toolPromptSnippets: Map<string, string> = new Map();\n\tprotected _toolPromptGuidelines: Map<string, string[]> = new Map();\n\tprotected _baseSystemPrompt = \"\";\n\tprotected _baseSystemPromptOptions!: BuildSystemPromptOptions;\n\tprotected _systemPromptOverride?: string;\n\tprotected _lastAssistantMessage: AssistantMessage | undefined = undefined;\n\tprotected _asyncJobManager: AsyncJobManager;\n\tprotected _asyncJobManagerSessionId: symbol;\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._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._excludedToolNames = config.excludedToolNames ? new Set(config.excludedToolNames) : undefined;\n\t\tthis._baseToolsOverride = config.baseToolsOverride;\n\t\tthis._sessionStartEvent = config.sessionStartEvent ?? { type: \"session_start\", reason: \"startup\" };\n\t\tthis._orchestrationContext = config.orchestrationContext;\n\t\tconst internals = this as unknown as AgentSessionInternalSurface;\n\t\tconst asyncJobManagerHandle = createSessionAsyncJobManager(internals);\n\t\tthis._asyncJobManager = asyncJobManagerHandle.manager;\n\t\tthis._asyncJobManagerSessionId = asyncJobManagerHandle.sessionId;\n\t\tinternals._handleAgentEvent = internals._handleAgentEvent.bind(this);\n\t\tthis._unsubscribeAgent = this.agent.subscribe(internals._handleAgentEvent);\n\t\tinternals._installAgentToolHooks();\n\t\tinternals._installAgentNextTurnRefresh();\n\t\tinternals._buildRuntime({\n\t\t\tactiveToolNames: this._initialActiveToolNames,\n\t\t\tincludeAllExtensionTools: true,\n\t\t});\n\t}\n}\n\nexport interface AgentSession extends AgentSessionPublicSurface {}\n\ninstallAgentSessionAccessors(AgentSession.prototype as unknown as AgentSessionInternalSurface);\nObject.assign(\n\tAgentSession.prototype,\n\tagentSessionToolHooksMethods,\n\tagentSessionEventsMethods,\n\tagentSessionStateMethods,\n\tagentSessionPromptMethods,\n\tagentSessionMessageQueueMethods,\n\tagentSessionModelsMethods,\n\tagentSessionCompactionMethods,\n\tagentSessionAutoCompactionMethods,\n\tagentSessionExtensionBindingsMethods,\n\tagentSessionToolRegistryMethods,\n\tagentSessionRetryMethods,\n\tagentSessionBashMethods,\n\tagentSessionTreeMethods,\n\tagentSessionExportMethods,\n);\n"]}
@@ -0,0 +1,3 @@
1
+ import type { ManagedAsyncBashJob, AsyncJobDeliveryMessage } from "./types.js";
2
+ export declare function formatAsyncResultForFollowUp(job: ManagedAsyncBashJob): AsyncJobDeliveryMessage;
3
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/core/async/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAwB/E,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,mBAAmB,GAAG,uBAAuB,CA2B9F","sourcesContent":["import type { ManagedAsyncBashJob, AsyncJobDeliveryMessage } from \"./types.js\";\n\nconst INLINE_OUTPUT_LIMIT = 12_000;\nconst LARGE_OUTPUT_PREVIEW_LIMIT = 4_000;\n\nfunction truncateText(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn text.slice(0, maxChars).trimEnd();\n}\n\nfunction jobOutput(job: ManagedAsyncBashJob): string {\n\tconst output = job.output.trimEnd();\n\tif (output.length > 0) return output;\n\treturn job.error ? `(no output)\\n\\n${job.error}` : \"(no output)\";\n}\n\nfunction statusLine(job: ManagedAsyncBashJob): string | undefined {\n\tif (job.error) return `Error: ${job.error}`;\n\tif (job.exitCode !== undefined && job.exitCode !== null && job.exitCode !== 0) {\n\t\treturn `Command exited with code ${job.exitCode}`;\n\t}\n\treturn undefined;\n}\n\nexport function formatAsyncResultForFollowUp(job: ManagedAsyncBashJob): AsyncJobDeliveryMessage {\n\tconst elapsedMs = (job.endedAt ?? Date.now()) - job.startedAt;\n\tconst header = `Async bash job ${job.jobId} ${job.status}: ${job.command}`;\n\tconst body = jobOutput(job);\n\tconst status = statusLine(job);\n\tconst inline = [header, body, status].filter((part): part is string => part !== undefined && part.length > 0).join(\"\\n\\n\");\n\tlet content = inline;\n\tif (inline.length > INLINE_OUTPUT_LIMIT && job.fullOutputPath) {\n\t\tconst preview = truncateText(body, LARGE_OUTPUT_PREVIEW_LIMIT);\n\t\tcontent = [header, preview, `[Output truncated for async follow-up. Full output: ${job.fullOutputPath}]`, status]\n\t\t\t.filter((part): part is string => part !== undefined && part.length > 0)\n\t\t\t.join(\"\\n\\n\");\n\t}\n\treturn {\n\t\tcustomType: \"async-job-result\",\n\t\tcontent,\n\t\tdisplay: true,\n\t\tdetails: {\n\t\t\tjobId: job.jobId,\n\t\t\ttype: \"bash\",\n\t\t\tstatus: job.status,\n\t\t\tcommand: job.command,\n\t\t\texitCode: job.exitCode,\n\t\t\tfullOutputPath: job.fullOutputPath,\n\t\t\twallTimeMs: elapsedMs,\n\t\t},\n\t};\n}\n"]}
@@ -0,0 +1,50 @@
1
+ const INLINE_OUTPUT_LIMIT = 12_000;
2
+ const LARGE_OUTPUT_PREVIEW_LIMIT = 4_000;
3
+ function truncateText(text, maxChars) {
4
+ if (text.length <= maxChars)
5
+ return text;
6
+ return text.slice(0, maxChars).trimEnd();
7
+ }
8
+ function jobOutput(job) {
9
+ const output = job.output.trimEnd();
10
+ if (output.length > 0)
11
+ return output;
12
+ return job.error ? `(no output)\n\n${job.error}` : "(no output)";
13
+ }
14
+ function statusLine(job) {
15
+ if (job.error)
16
+ return `Error: ${job.error}`;
17
+ if (job.exitCode !== undefined && job.exitCode !== null && job.exitCode !== 0) {
18
+ return `Command exited with code ${job.exitCode}`;
19
+ }
20
+ return undefined;
21
+ }
22
+ export function formatAsyncResultForFollowUp(job) {
23
+ const elapsedMs = (job.endedAt ?? Date.now()) - job.startedAt;
24
+ const header = `Async bash job ${job.jobId} ${job.status}: ${job.command}`;
25
+ const body = jobOutput(job);
26
+ const status = statusLine(job);
27
+ const inline = [header, body, status].filter((part) => part !== undefined && part.length > 0).join("\n\n");
28
+ let content = inline;
29
+ if (inline.length > INLINE_OUTPUT_LIMIT && job.fullOutputPath) {
30
+ const preview = truncateText(body, LARGE_OUTPUT_PREVIEW_LIMIT);
31
+ content = [header, preview, `[Output truncated for async follow-up. Full output: ${job.fullOutputPath}]`, status]
32
+ .filter((part) => part !== undefined && part.length > 0)
33
+ .join("\n\n");
34
+ }
35
+ return {
36
+ customType: "async-job-result",
37
+ content,
38
+ display: true,
39
+ details: {
40
+ jobId: job.jobId,
41
+ type: "bash",
42
+ status: job.status,
43
+ command: job.command,
44
+ exitCode: job.exitCode,
45
+ fullOutputPath: job.fullOutputPath,
46
+ wallTimeMs: elapsedMs,
47
+ },
48
+ };
49
+ }
50
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","sourceRoot":"","sources":["../../../src/core/async/format.ts"],"names":[],"mappings":"AAEA,MAAM,mBAAmB,GAAG,MAAM,CAAC;AACnC,MAAM,0BAA0B,GAAG,KAAK,CAAC;AAEzC,SAAS,YAAY,CAAC,IAAY,EAAE,QAAgB;IACnD,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,IAAI,CAAC;IACzC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;AAC1C,CA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OAAO,GAAG,MAAM,CAAC;IACrB,IAAI,MAAM,CAAC,MAAM,GAAG,mBAAmB,IAAI,GAAG,CAAC,cAAc,EAAE,CAAC;QAC/D,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,EAAE,0BAA0B,CAAC,CAAC;QAC/D,OAAO,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,uDAAuD,GAAG,CAAC,cAAc,GAAG,EAAE,MAAM,CAAC;aAC/G,MAAM,CAAC,CAAC,IAAI,EAAkB,EAAE,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;aACvE,IAAI,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC;IACD,OAAO;QACN,UAAU,EAAE,kBAAkB;QAC9B,OAAO;QACP,OAAO,EAAE,IAAI;QACb,OAAO,EAAE;YACR,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,IAAI,EAAE,MAAM;YACZ,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,cAAc,EAAE,GAAG,CAAC,cAAc;YAClC,UAAU,EAAE,SAAS;SACrB;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { ManagedAsyncBashJob, AsyncJobDeliveryMessage } from \"./types.js\";\n\nconst INLINE_OUTPUT_LIMIT = 12_000;\nconst LARGE_OUTPUT_PREVIEW_LIMIT = 4_000;\n\nfunction truncateText(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn text.slice(0, maxChars).trimEnd();\n}\n\nfunction jobOutput(job: ManagedAsyncBashJob): string {\n\tconst output = job.output.trimEnd();\n\tif (output.length > 0) return output;\n\treturn job.error ? `(no output)\\n\\n${job.error}` : \"(no output)\";\n}\n\nfunction statusLine(job: ManagedAsyncBashJob): string | undefined {\n\tif (job.error) return `Error: ${job.error}`;\n\tif (job.exitCode !== undefined && job.exitCode !== null && job.exitCode !== 0) {\n\t\treturn `Command exited with code ${job.exitCode}`;\n\t}\n\treturn undefined;\n}\n\nexport function formatAsyncResultForFollowUp(job: ManagedAsyncBashJob): AsyncJobDeliveryMessage {\n\tconst elapsedMs = (job.endedAt ?? Date.now()) - job.startedAt;\n\tconst header = `Async bash job ${job.jobId} ${job.status}: ${job.command}`;\n\tconst body = jobOutput(job);\n\tconst status = statusLine(job);\n\tconst inline = [header, body, status].filter((part): part is string => part !== undefined && part.length > 0).join(\"\\n\\n\");\n\tlet content = inline;\n\tif (inline.length > INLINE_OUTPUT_LIMIT && job.fullOutputPath) {\n\t\tconst preview = truncateText(body, LARGE_OUTPUT_PREVIEW_LIMIT);\n\t\tcontent = [header, preview, `[Output truncated for async follow-up. Full output: ${job.fullOutputPath}]`, status]\n\t\t\t.filter((part): part is string => part !== undefined && part.length > 0)\n\t\t\t.join(\"\\n\\n\");\n\t}\n\treturn {\n\t\tcustomType: \"async-job-result\",\n\t\tcontent,\n\t\tdisplay: true,\n\t\tdetails: {\n\t\t\tjobId: job.jobId,\n\t\t\ttype: \"bash\",\n\t\t\tstatus: job.status,\n\t\t\tcommand: job.command,\n\t\t\texitCode: job.exitCode,\n\t\t\tfullOutputPath: job.fullOutputPath,\n\t\t\twallTimeMs: elapsedMs,\n\t\t},\n\t};\n}\n"]}
@@ -0,0 +1,39 @@
1
+ import { type ManagedBashJob } from "../tools/bash-async-jobs.js";
2
+ import type { AsyncJobDeliveryCallback, AsyncJobDeliveryHandler } from "./types.js";
3
+ interface AsyncJobManagerOptions {
4
+ onJobComplete: AsyncJobDeliveryCallback;
5
+ maxRunningJobs?: number;
6
+ maxRetainedJobs?: number;
7
+ completedJobTtlMs?: number;
8
+ }
9
+ export declare class AsyncJobManager {
10
+ #private;
11
+ static instance(): AsyncJobManager | undefined;
12
+ static setInstance(value: AsyncJobManager | undefined): void;
13
+ static resetForTests(): void;
14
+ constructor(options: AsyncJobManagerOptions);
15
+ get disposed(): boolean;
16
+ get atCapacity(): boolean;
17
+ registerSession(): symbol;
18
+ releaseSession(sessionId: symbol): void;
19
+ isSessionDisposed(sessionId: symbol): boolean;
20
+ registerBashJob(job: ManagedBashJob, onComplete?: AsyncJobDeliveryHandler, sessionId?: symbol): void;
21
+ completeBashJob(job: ManagedBashJob): void;
22
+ acknowledgeDeliveries(jobIds: readonly string[]): void;
23
+ isDeliverySuppressed(jobId: string): boolean;
24
+ deliveryState(): {
25
+ queued: number;
26
+ delivering: boolean;
27
+ pendingJobIds: string[];
28
+ };
29
+ retentionState(): {
30
+ jobs: number;
31
+ suppressions: number;
32
+ handlers: number;
33
+ queued: number;
34
+ sessions: number;
35
+ };
36
+ dispose(): void;
37
+ }
38
+ export {};
39
+ //# sourceMappingURL=job-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-manager.d.ts","sourceRoot":"","sources":["../../../src/core/async/job-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE/G,OAAO,KAAK,EAAE,wBAAwB,EAAE,uBAAuB,EAAgD,MAAM,YAAY,CAAC;AAOlI,UAAU,sBAAsB;IAC/B,aAAa,EAAE,wBAAwB,CAAC;IACxC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAeD,qBAAa,eAAe;;IAG3B,MAAM,CAAC,QAAQ,IAAI,eAAe,GAAG,SAAS,CAE7C;IAED,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,eAAe,GAAG,SAAS,GAAG,IAAI,CAE3D;IAED,MAAM,CAAC,aAAa,IAAI,IAAI,CAE3B;IAiBD,YAAY,OAAO,EAAE,sBAAsB,EAK1C;IAED,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,IAAI,UAAU,IAAI,OAAO,CAIxB;IAGD,eAAe,IAAI,MAAM,CAKxB;IAED,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAOtC;IAED,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAE5C;IACD,eAAe,CAAC,GAAG,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,uBAAuB,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAcnG;IAED,eAAe,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAMzC;IAED,qBAAqB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAUrD;IAED,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE3C;IAED,aAAa,IAAI;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,OAAO,CAAC;QAAC,aAAa,EAAE,MAAM,EAAE,CAAA;KAAE,CAOhF;IAED,cAAc,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAG3G;IAED,OAAO,IAAI,IAAI,CAWd;CAqGD","sourcesContent":["import { COMPLETED_JOB_TTL_MS, MAX_MANAGED_BASH_JOBS, type ManagedBashJob } from \"../tools/bash-async-jobs.js\";\nimport { formatAsyncResultForFollowUp } from \"./format.js\";\nimport type { AsyncJobDeliveryCallback, AsyncJobDeliveryHandler, AsyncJobDeliveryMessage, ManagedAsyncBashJob } from \"./types.js\";\n\nconst DEFAULT_MAX_RUNNING_JOBS = 15;\nconst DELIVERY_RETRY_BASE_MS = 500;\nconst DELIVERY_RETRY_MAX_MS = 30_000;\nconst DELIVERY_RETRY_JITTER_MS = 200;\n\ninterface AsyncJobManagerOptions {\n\tonJobComplete: AsyncJobDeliveryCallback;\n\tmaxRunningJobs?: number;\n\tmaxRetainedJobs?: number;\n\tcompletedJobTtlMs?: number;\n}\n\ninterface Delivery {\n\tjobId: string;\n\tmessage: AsyncJobDeliveryMessage;\n\tattempt: number;\n\tnextAttemptAt: number;\n\tpromise?: Promise<void>;\n}\n\ninterface RegisteredSession {\n\tdisposed: boolean;\n\tactiveJobIds: Set<string>;\n}\n\nexport class AsyncJobManager {\n\tstatic #instance: AsyncJobManager | undefined;\n\n\tstatic instance(): AsyncJobManager | undefined {\n\t\treturn AsyncJobManager.#instance;\n\t}\n\n\tstatic setInstance(value: AsyncJobManager | undefined): void {\n\t\tAsyncJobManager.#instance = value;\n\t}\n\n\tstatic resetForTests(): void {\n\t\tAsyncJobManager.#instance = undefined;\n\t}\n\n\treadonly #jobs = new Map<string, ManagedBashJob>();\n\treadonly #deliveries: Delivery[] = [];\n\treadonly #suppressedDeliveries = new Set<string>();\n\treadonly #inFlightDeliveries = new Map<string, Delivery>();\n\treadonly #deliveryHandlers = new Map<string, AsyncJobDeliveryHandler>();\n\treadonly #jobSessions = new Map<string, symbol>();\n\treadonly #sessions = new Map<symbol, RegisteredSession>();\n\treadonly #onJobComplete: AsyncJobDeliveryCallback;\n\treadonly #maxRunningJobs: number;\n\treadonly #maxRetainedJobs: number;\n\treadonly #completedJobTtlMs: number;\n\t#timer: NodeJS.Timeout | undefined;\n\t#disposed = false;\n\t#runningDeliveryLoop = false;\n\n\tconstructor(options: AsyncJobManagerOptions) {\n\t\tthis.#onJobComplete = options.onJobComplete;\n\t\tthis.#maxRunningJobs = Math.max(1, Math.floor(options.maxRunningJobs ?? DEFAULT_MAX_RUNNING_JOBS));\n\t\tthis.#maxRetainedJobs = Math.max(1, Math.floor(options.maxRetainedJobs ?? MAX_MANAGED_BASH_JOBS));\n\t\tthis.#completedJobTtlMs = Math.max(0, Math.floor(options.completedJobTtlMs ?? COMPLETED_JOB_TTL_MS));\n\t}\n\n\tget disposed(): boolean {\n\t\treturn this.#disposed;\n\t}\n\n\tget atCapacity(): boolean {\n\t\tlet running = 0;\n\t\tfor (const job of this.#jobs.values()) if (job.status === \"running\") running += 1;\n\t\treturn running >= this.#maxRunningJobs;\n\t}\n\n\n\tregisterSession(): symbol {\n\t\tif (this.#disposed) throw new Error(\"Async job manager is disposed\");\n\t\tconst sessionId = Symbol(\"async-job-session\");\n\t\tthis.#sessions.set(sessionId, { disposed: false, activeJobIds: new Set() });\n\t\treturn sessionId;\n\t}\n\n\treleaseSession(sessionId: symbol): void {\n\t\tconst session = this.#sessions.get(sessionId);\n\t\tif (!session) return;\n\t\tsession.disposed = true;\n\t\tthis.acknowledgeDeliveries([...session.activeJobIds]);\n\t\tthis.#sessions.delete(sessionId);\n\t\tif (this.#sessions.size === 0) this.dispose();\n\t}\n\n\tisSessionDisposed(sessionId: symbol): boolean {\n\t\treturn this.#disposed || this.#sessions.get(sessionId)?.disposed !== false;\n\t}\n\tregisterBashJob(job: ManagedBashJob, onComplete?: AsyncJobDeliveryHandler, sessionId?: symbol): void {\n\t\tif (this.#disposed) throw new Error(\"Async job manager is disposed\");\n\t\tthis.#pruneRetention();\n\t\tif (this.atCapacity) throw new Error(`Background job limit reached (${this.#maxRunningJobs}). Wait for running jobs to finish or cancel one.`);\n\t\tif (sessionId !== undefined && this.isSessionDisposed(sessionId)) throw new Error(\"Async job session is disposed\");\n\t\tthis.#suppressedDeliveries.delete(job.jobId);\n\t\tif (onComplete) this.#deliveryHandlers.set(job.jobId, onComplete);\n\t\telse this.#deliveryHandlers.delete(job.jobId);\n\t\tif (sessionId !== undefined) {\n\t\t\tthis.#jobSessions.set(job.jobId, sessionId);\n\t\t\tthis.#sessions.get(sessionId)?.activeJobIds.add(job.jobId);\n\t\t} else this.#jobSessions.delete(job.jobId);\n\t\tthis.#jobs.set(job.jobId, job);\n\t\tthis.#pruneRetention();\n\t}\n\n\tcompleteBashJob(job: ManagedBashJob): void {\n\t\tif (this.#disposed || this.#suppressedDeliveries.has(job.jobId)) return;\n\t\tif (job.status === \"running\") return;\n\t\tthis.#jobs.set(job.jobId, job);\n\t\tthis.#pruneRetention();\n\t\tif (this.#jobs.has(job.jobId)) this.#enqueueDelivery(job as ManagedAsyncBashJob);\n\t}\n\n\tacknowledgeDeliveries(jobIds: readonly string[]): void {\n\t\tfor (const jobId of jobIds) {\n\t\t\tthis.#suppressedDeliveries.add(jobId);\n\t\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {\n\t\t\t\tif (this.#deliveries[index]?.jobId === jobId) this.#deliveries.splice(index, 1);\n\t\t\t}\n\t\t\tthis.#deliveryHandlers.delete(jobId);\n\t\t\tthis.#completeTrackedJob(jobId);\n\t\t}\n\t\tthis.#pruneRetention();\n\t}\n\n\tisDeliverySuppressed(jobId: string): boolean {\n\t\treturn this.#suppressedDeliveries.has(jobId);\n\t}\n\n\tdeliveryState(): { queued: number; delivering: boolean; pendingJobIds: string[] } {\n\t\tthis.#pruneRetention();\n\t\treturn {\n\t\t\tqueued: this.#deliveries.length,\n\t\t\tdelivering: this.#runningDeliveryLoop || this.#inFlightDeliveries.size > 0,\n\t\t\tpendingJobIds: [...this.#deliveries.map((delivery) => delivery.jobId), ...this.#inFlightDeliveries.keys()],\n\t\t};\n\t}\n\n\tretentionState(): { jobs: number; suppressions: number; handlers: number; queued: number; sessions: number } {\n\t\tthis.#pruneRetention();\n\t\treturn { jobs: this.#jobs.size, suppressions: this.#suppressedDeliveries.size, handlers: this.#deliveryHandlers.size, queued: this.#deliveries.length + this.#inFlightDeliveries.size, sessions: this.#sessions.size };\n\t}\n\n\tdispose(): void {\n\t\tthis.#disposed = true;\n\t\tif (this.#timer) clearTimeout(this.#timer);\n\t\tthis.#timer = undefined;\n\t\tthis.#deliveries.length = 0;\n\t\tthis.#inFlightDeliveries.clear();\n\t\tthis.#deliveryHandlers.clear();\n\t\tthis.#jobSessions.clear();\n\t\tthis.#sessions.clear();\n\t\tthis.#jobs.clear();\n\t\tthis.#suppressedDeliveries.clear();\n\t}\n\n\t#enqueueDelivery(job: ManagedAsyncBashJob): void {\n\t\tif (this.#inFlightDeliveries.has(job.jobId)) return;\n\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) if (this.#deliveries[index]?.jobId === job.jobId) this.#deliveries.splice(index, 1);\n\t\tthis.#deliveries.push({ jobId: job.jobId, message: formatAsyncResultForFollowUp(job), attempt: 0, nextAttemptAt: Date.now() });\n\t\tthis.#pruneRetention();\n\t\tthis.#scheduleDeliveryLoop(25);\n\t}\n\n\n\t#completeTrackedJob(jobId: string): void {\n\t\tconst sessionId = this.#jobSessions.get(jobId);\n\t\tif (sessionId !== undefined) this.#sessions.get(sessionId)?.activeJobIds.delete(jobId);\n\t\tthis.#jobSessions.delete(jobId);\n\t}\n\t#pruneRetention(now = Date.now()): void {\n\t\tfor (const [jobId, job] of this.#jobs) {\n\t\t\tif (job.status !== \"running\" && job.endedAt !== undefined && now - job.endedAt > this.#completedJobTtlMs) {\n\t\t\t\tthis.#jobs.delete(jobId);\n\t\t\t\tthis.#completeTrackedJob(jobId);\n\t\t\t}\n\t\t}\n\t\tfor (const jobId of this.#suppressedDeliveries) {\n\t\t\tif (!this.#jobs.has(jobId)) this.#suppressedDeliveries.delete(jobId);\n\t\t}\n\t\tconst oldestTerminalJobs = [...this.#jobs.values()]\n\t\t\t.filter((job) => job.status !== \"running\")\n\t\t\t.sort((a, b) => (a.endedAt ?? a.startedAt) - (b.endedAt ?? b.startedAt));\n\t\twhile (this.#jobs.size > this.#maxRetainedJobs && oldestTerminalJobs.length > 0) {\n\t\t\tconst job = oldestTerminalJobs.shift();\n\t\t\tif (job) {\n\t\t\t\tthis.#jobs.delete(job.jobId);\n\t\t\t\tthis.#completeTrackedJob(job.jobId);\n\t\t\t}\n\t\t}\n\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {\n\t\t\tconst delivery = this.#deliveries[index];\n\t\t\tif (!delivery || !this.#jobs.has(delivery.jobId) || this.#suppressedDeliveries.has(delivery.jobId)) this.#deliveries.splice(index, 1);\n\t\t}\n\t\twhile (this.#deliveries.length + this.#inFlightDeliveries.size > this.#maxRetainedJobs && this.#deliveries.length > 0) this.#deliveries.shift();\n\t\tfor (const jobId of this.#deliveryHandlers.keys()) if (!this.#jobs.has(jobId) || this.#suppressedDeliveries.has(jobId)) this.#deliveryHandlers.delete(jobId);\n\t\tfor (const session of this.#sessions.values()) for (const jobId of session.activeJobIds) if (!this.#jobs.has(jobId)) session.activeJobIds.delete(jobId);\n\t}\n\n\t#scheduleDeliveryLoop(delayMs: number): void {\n\t\tif (this.#disposed) return;\n\t\tif (this.#timer) clearTimeout(this.#timer);\n\t\tthis.#timer = setTimeout(() => {\n\t\t\tthis.#timer = undefined;\n\t\t\tvoid this.#runDeliveryLoop();\n\t\t}, delayMs);\n\t\tthis.#timer.unref?.();\n\t}\n\n\tasync #runDeliveryLoop(): Promise<void> {\n\t\tif (this.#runningDeliveryLoop || this.#disposed) return;\n\t\tthis.#runningDeliveryLoop = true;\n\t\ttry {\n\t\t\twhile (!this.#disposed) {\n\t\t\t\tconst now = Date.now();\n\t\t\t\tconst delivery = this.#deliveries.find((candidate) => candidate.nextAttemptAt <= now);\n\t\t\t\tif (!delivery) break;\n\t\t\t\tthis.#deliveries.splice(this.#deliveries.indexOf(delivery), 1);\n\t\t\t\tthis.#startDelivery(delivery);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.#runningDeliveryLoop = false;\n\t\t}\n\t\tthis.#scheduleNextDelivery();\n\t}\n\n\t#startDelivery(delivery: Delivery): void {\n\t\tif (this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId) || this.#inFlightDeliveries.has(delivery.jobId)) return;\n\t\tconst handler = this.#deliveryHandlers.get(delivery.jobId) ?? this.#onJobComplete;\n\t\tthis.#inFlightDeliveries.set(delivery.jobId, delivery);\n\t\tdelivery.promise = (async () => handler(delivery.message))()\n\t\t\t.then(() => {\n\t\t\t\tif (!this.#suppressedDeliveries.has(delivery.jobId) && this.#jobs.has(delivery.jobId)) {\n\t\t\t\t\tthis.#deliveryHandlers.delete(delivery.jobId);\n\t\t\t\t\tthis.#completeTrackedJob(delivery.jobId);\n\t\t\t\t}\n\t\t\t})\n\t\t\t.catch(() => {\n\t\t\t\tif (this.#disposed || this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId)) return;\n\t\t\t\tdelivery.attempt += 1;\n\t\t\t\tconst jitter = Math.floor(Math.random() * DELIVERY_RETRY_JITTER_MS);\n\t\t\t\tdelivery.nextAttemptAt = Date.now() + Math.min(DELIVERY_RETRY_MAX_MS, DELIVERY_RETRY_BASE_MS * 2 ** delivery.attempt) + jitter;\n\t\t\t\tthis.#deliveries.push(delivery);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tthis.#inFlightDeliveries.delete(delivery.jobId);\n\t\t\t\tthis.#pruneRetention();\n\t\t\t\tthis.#scheduleNextDelivery();\n\t\t\t});\n\t}\n\n\t#scheduleNextDelivery(): void {\n\t\tconst next = this.#deliveries.reduce<number | undefined>((soonest, delivery) => soonest === undefined ? delivery.nextAttemptAt : Math.min(soonest, delivery.nextAttemptAt), undefined);\n\t\tif (next !== undefined) this.#scheduleDeliveryLoop(Math.max(0, next - Date.now()));\n\t}\n}\n"]}
@@ -0,0 +1,254 @@
1
+ import { COMPLETED_JOB_TTL_MS, MAX_MANAGED_BASH_JOBS } from "../tools/bash-async-jobs.js";
2
+ import { formatAsyncResultForFollowUp } from "./format.js";
3
+ const DEFAULT_MAX_RUNNING_JOBS = 15;
4
+ const DELIVERY_RETRY_BASE_MS = 500;
5
+ const DELIVERY_RETRY_MAX_MS = 30_000;
6
+ const DELIVERY_RETRY_JITTER_MS = 200;
7
+ export class AsyncJobManager {
8
+ static #instance;
9
+ static instance() {
10
+ return AsyncJobManager.#instance;
11
+ }
12
+ static setInstance(value) {
13
+ AsyncJobManager.#instance = value;
14
+ }
15
+ static resetForTests() {
16
+ AsyncJobManager.#instance = undefined;
17
+ }
18
+ #jobs = new Map();
19
+ #deliveries = [];
20
+ #suppressedDeliveries = new Set();
21
+ #inFlightDeliveries = new Map();
22
+ #deliveryHandlers = new Map();
23
+ #jobSessions = new Map();
24
+ #sessions = new Map();
25
+ #onJobComplete;
26
+ #maxRunningJobs;
27
+ #maxRetainedJobs;
28
+ #completedJobTtlMs;
29
+ #timer;
30
+ #disposed = false;
31
+ #runningDeliveryLoop = false;
32
+ constructor(options) {
33
+ this.#onJobComplete = options.onJobComplete;
34
+ this.#maxRunningJobs = Math.max(1, Math.floor(options.maxRunningJobs ?? DEFAULT_MAX_RUNNING_JOBS));
35
+ this.#maxRetainedJobs = Math.max(1, Math.floor(options.maxRetainedJobs ?? MAX_MANAGED_BASH_JOBS));
36
+ this.#completedJobTtlMs = Math.max(0, Math.floor(options.completedJobTtlMs ?? COMPLETED_JOB_TTL_MS));
37
+ }
38
+ get disposed() {
39
+ return this.#disposed;
40
+ }
41
+ get atCapacity() {
42
+ let running = 0;
43
+ for (const job of this.#jobs.values())
44
+ if (job.status === "running")
45
+ running += 1;
46
+ return running >= this.#maxRunningJobs;
47
+ }
48
+ registerSession() {
49
+ if (this.#disposed)
50
+ throw new Error("Async job manager is disposed");
51
+ const sessionId = Symbol("async-job-session");
52
+ this.#sessions.set(sessionId, { disposed: false, activeJobIds: new Set() });
53
+ return sessionId;
54
+ }
55
+ releaseSession(sessionId) {
56
+ const session = this.#sessions.get(sessionId);
57
+ if (!session)
58
+ return;
59
+ session.disposed = true;
60
+ this.acknowledgeDeliveries([...session.activeJobIds]);
61
+ this.#sessions.delete(sessionId);
62
+ if (this.#sessions.size === 0)
63
+ this.dispose();
64
+ }
65
+ isSessionDisposed(sessionId) {
66
+ return this.#disposed || this.#sessions.get(sessionId)?.disposed !== false;
67
+ }
68
+ registerBashJob(job, onComplete, sessionId) {
69
+ if (this.#disposed)
70
+ throw new Error("Async job manager is disposed");
71
+ this.#pruneRetention();
72
+ if (this.atCapacity)
73
+ throw new Error(`Background job limit reached (${this.#maxRunningJobs}). Wait for running jobs to finish or cancel one.`);
74
+ if (sessionId !== undefined && this.isSessionDisposed(sessionId))
75
+ throw new Error("Async job session is disposed");
76
+ this.#suppressedDeliveries.delete(job.jobId);
77
+ if (onComplete)
78
+ this.#deliveryHandlers.set(job.jobId, onComplete);
79
+ else
80
+ this.#deliveryHandlers.delete(job.jobId);
81
+ if (sessionId !== undefined) {
82
+ this.#jobSessions.set(job.jobId, sessionId);
83
+ this.#sessions.get(sessionId)?.activeJobIds.add(job.jobId);
84
+ }
85
+ else
86
+ this.#jobSessions.delete(job.jobId);
87
+ this.#jobs.set(job.jobId, job);
88
+ this.#pruneRetention();
89
+ }
90
+ completeBashJob(job) {
91
+ if (this.#disposed || this.#suppressedDeliveries.has(job.jobId))
92
+ return;
93
+ if (job.status === "running")
94
+ return;
95
+ this.#jobs.set(job.jobId, job);
96
+ this.#pruneRetention();
97
+ if (this.#jobs.has(job.jobId))
98
+ this.#enqueueDelivery(job);
99
+ }
100
+ acknowledgeDeliveries(jobIds) {
101
+ for (const jobId of jobIds) {
102
+ this.#suppressedDeliveries.add(jobId);
103
+ for (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {
104
+ if (this.#deliveries[index]?.jobId === jobId)
105
+ this.#deliveries.splice(index, 1);
106
+ }
107
+ this.#deliveryHandlers.delete(jobId);
108
+ this.#completeTrackedJob(jobId);
109
+ }
110
+ this.#pruneRetention();
111
+ }
112
+ isDeliverySuppressed(jobId) {
113
+ return this.#suppressedDeliveries.has(jobId);
114
+ }
115
+ deliveryState() {
116
+ this.#pruneRetention();
117
+ return {
118
+ queued: this.#deliveries.length,
119
+ delivering: this.#runningDeliveryLoop || this.#inFlightDeliveries.size > 0,
120
+ pendingJobIds: [...this.#deliveries.map((delivery) => delivery.jobId), ...this.#inFlightDeliveries.keys()],
121
+ };
122
+ }
123
+ retentionState() {
124
+ this.#pruneRetention();
125
+ return { jobs: this.#jobs.size, suppressions: this.#suppressedDeliveries.size, handlers: this.#deliveryHandlers.size, queued: this.#deliveries.length + this.#inFlightDeliveries.size, sessions: this.#sessions.size };
126
+ }
127
+ dispose() {
128
+ this.#disposed = true;
129
+ if (this.#timer)
130
+ clearTimeout(this.#timer);
131
+ this.#timer = undefined;
132
+ this.#deliveries.length = 0;
133
+ this.#inFlightDeliveries.clear();
134
+ this.#deliveryHandlers.clear();
135
+ this.#jobSessions.clear();
136
+ this.#sessions.clear();
137
+ this.#jobs.clear();
138
+ this.#suppressedDeliveries.clear();
139
+ }
140
+ #enqueueDelivery(job) {
141
+ if (this.#inFlightDeliveries.has(job.jobId))
142
+ return;
143
+ for (let index = this.#deliveries.length - 1; index >= 0; index -= 1)
144
+ if (this.#deliveries[index]?.jobId === job.jobId)
145
+ this.#deliveries.splice(index, 1);
146
+ this.#deliveries.push({ jobId: job.jobId, message: formatAsyncResultForFollowUp(job), attempt: 0, nextAttemptAt: Date.now() });
147
+ this.#pruneRetention();
148
+ this.#scheduleDeliveryLoop(25);
149
+ }
150
+ #completeTrackedJob(jobId) {
151
+ const sessionId = this.#jobSessions.get(jobId);
152
+ if (sessionId !== undefined)
153
+ this.#sessions.get(sessionId)?.activeJobIds.delete(jobId);
154
+ this.#jobSessions.delete(jobId);
155
+ }
156
+ #pruneRetention(now = Date.now()) {
157
+ for (const [jobId, job] of this.#jobs) {
158
+ if (job.status !== "running" && job.endedAt !== undefined && now - job.endedAt > this.#completedJobTtlMs) {
159
+ this.#jobs.delete(jobId);
160
+ this.#completeTrackedJob(jobId);
161
+ }
162
+ }
163
+ for (const jobId of this.#suppressedDeliveries) {
164
+ if (!this.#jobs.has(jobId))
165
+ this.#suppressedDeliveries.delete(jobId);
166
+ }
167
+ const oldestTerminalJobs = [...this.#jobs.values()]
168
+ .filter((job) => job.status !== "running")
169
+ .sort((a, b) => (a.endedAt ?? a.startedAt) - (b.endedAt ?? b.startedAt));
170
+ while (this.#jobs.size > this.#maxRetainedJobs && oldestTerminalJobs.length > 0) {
171
+ const job = oldestTerminalJobs.shift();
172
+ if (job) {
173
+ this.#jobs.delete(job.jobId);
174
+ this.#completeTrackedJob(job.jobId);
175
+ }
176
+ }
177
+ for (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {
178
+ const delivery = this.#deliveries[index];
179
+ if (!delivery || !this.#jobs.has(delivery.jobId) || this.#suppressedDeliveries.has(delivery.jobId))
180
+ this.#deliveries.splice(index, 1);
181
+ }
182
+ while (this.#deliveries.length + this.#inFlightDeliveries.size > this.#maxRetainedJobs && this.#deliveries.length > 0)
183
+ this.#deliveries.shift();
184
+ for (const jobId of this.#deliveryHandlers.keys())
185
+ if (!this.#jobs.has(jobId) || this.#suppressedDeliveries.has(jobId))
186
+ this.#deliveryHandlers.delete(jobId);
187
+ for (const session of this.#sessions.values())
188
+ for (const jobId of session.activeJobIds)
189
+ if (!this.#jobs.has(jobId))
190
+ session.activeJobIds.delete(jobId);
191
+ }
192
+ #scheduleDeliveryLoop(delayMs) {
193
+ if (this.#disposed)
194
+ return;
195
+ if (this.#timer)
196
+ clearTimeout(this.#timer);
197
+ this.#timer = setTimeout(() => {
198
+ this.#timer = undefined;
199
+ void this.#runDeliveryLoop();
200
+ }, delayMs);
201
+ this.#timer.unref?.();
202
+ }
203
+ async #runDeliveryLoop() {
204
+ if (this.#runningDeliveryLoop || this.#disposed)
205
+ return;
206
+ this.#runningDeliveryLoop = true;
207
+ try {
208
+ while (!this.#disposed) {
209
+ const now = Date.now();
210
+ const delivery = this.#deliveries.find((candidate) => candidate.nextAttemptAt <= now);
211
+ if (!delivery)
212
+ break;
213
+ this.#deliveries.splice(this.#deliveries.indexOf(delivery), 1);
214
+ this.#startDelivery(delivery);
215
+ }
216
+ }
217
+ finally {
218
+ this.#runningDeliveryLoop = false;
219
+ }
220
+ this.#scheduleNextDelivery();
221
+ }
222
+ #startDelivery(delivery) {
223
+ if (this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId) || this.#inFlightDeliveries.has(delivery.jobId))
224
+ return;
225
+ const handler = this.#deliveryHandlers.get(delivery.jobId) ?? this.#onJobComplete;
226
+ this.#inFlightDeliveries.set(delivery.jobId, delivery);
227
+ delivery.promise = (async () => handler(delivery.message))()
228
+ .then(() => {
229
+ if (!this.#suppressedDeliveries.has(delivery.jobId) && this.#jobs.has(delivery.jobId)) {
230
+ this.#deliveryHandlers.delete(delivery.jobId);
231
+ this.#completeTrackedJob(delivery.jobId);
232
+ }
233
+ })
234
+ .catch(() => {
235
+ if (this.#disposed || this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId))
236
+ return;
237
+ delivery.attempt += 1;
238
+ const jitter = Math.floor(Math.random() * DELIVERY_RETRY_JITTER_MS);
239
+ delivery.nextAttemptAt = Date.now() + Math.min(DELIVERY_RETRY_MAX_MS, DELIVERY_RETRY_BASE_MS * 2 ** delivery.attempt) + jitter;
240
+ this.#deliveries.push(delivery);
241
+ })
242
+ .finally(() => {
243
+ this.#inFlightDeliveries.delete(delivery.jobId);
244
+ this.#pruneRetention();
245
+ this.#scheduleNextDelivery();
246
+ });
247
+ }
248
+ #scheduleNextDelivery() {
249
+ const next = this.#deliveries.reduce((soonest, delivery) => soonest === undefined ? delivery.nextAttemptAt : Math.min(soonest, delivery.nextAttemptAt), undefined);
250
+ if (next !== undefined)
251
+ this.#scheduleDeliveryLoop(Math.max(0, next - Date.now()));
252
+ }
253
+ }
254
+ //# sourceMappingURL=job-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-manager.js","sourceRoot":"","sources":["../../../src/core/async/job-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAuB,MAAM,6BAA6B,CAAC;AAC/G,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAG3D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AACpC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AACrC,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAsBrC,MAAM,OAAO,eAAe;IAC3B,MAAM,CAAC,SAAS,CAA8B;IAE9C,MAAM,CAAC,Q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C;YAChD,IAAI,CAAC,eAAe,EAAE,CAAC;YACvB,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC9B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,qBAAqB;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAqB,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,CAAC,CAAC;QACvL,IAAI,IAAI,KAAK,SAAS;YAAE,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACpF,CAAC;CACD","sourcesContent":["import { COMPLETED_JOB_TTL_MS, MAX_MANAGED_BASH_JOBS, type ManagedBashJob } from \"../tools/bash-async-jobs.js\";\nimport { formatAsyncResultForFollowUp } from \"./format.js\";\nimport type { AsyncJobDeliveryCallback, AsyncJobDeliveryHandler, AsyncJobDeliveryMessage, ManagedAsyncBashJob } from \"./types.js\";\n\nconst DEFAULT_MAX_RUNNING_JOBS = 15;\nconst DELIVERY_RETRY_BASE_MS = 500;\nconst DELIVERY_RETRY_MAX_MS = 30_000;\nconst DELIVERY_RETRY_JITTER_MS = 200;\n\ninterface AsyncJobManagerOptions {\n\tonJobComplete: AsyncJobDeliveryCallback;\n\tmaxRunningJobs?: number;\n\tmaxRetainedJobs?: number;\n\tcompletedJobTtlMs?: number;\n}\n\ninterface Delivery {\n\tjobId: string;\n\tmessage: AsyncJobDeliveryMessage;\n\tattempt: number;\n\tnextAttemptAt: number;\n\tpromise?: Promise<void>;\n}\n\ninterface RegisteredSession {\n\tdisposed: boolean;\n\tactiveJobIds: Set<string>;\n}\n\nexport class AsyncJobManager {\n\tstatic #instance: AsyncJobManager | undefined;\n\n\tstatic instance(): AsyncJobManager | undefined {\n\t\treturn AsyncJobManager.#instance;\n\t}\n\n\tstatic setInstance(value: AsyncJobManager | undefined): void {\n\t\tAsyncJobManager.#instance = value;\n\t}\n\n\tstatic resetForTests(): void {\n\t\tAsyncJobManager.#instance = undefined;\n\t}\n\n\treadonly #jobs = new Map<string, ManagedBashJob>();\n\treadonly #deliveries: Delivery[] = [];\n\treadonly #suppressedDeliveries = new Set<string>();\n\treadonly #inFlightDeliveries = new Map<string, Delivery>();\n\treadonly #deliveryHandlers = new Map<string, AsyncJobDeliveryHandler>();\n\treadonly #jobSessions = new Map<string, symbol>();\n\treadonly #sessions = new Map<symbol, RegisteredSession>();\n\treadonly #onJobComplete: AsyncJobDeliveryCallback;\n\treadonly #maxRunningJobs: number;\n\treadonly #maxRetainedJobs: number;\n\treadonly #completedJobTtlMs: number;\n\t#timer: NodeJS.Timeout | undefined;\n\t#disposed = false;\n\t#runningDeliveryLoop = false;\n\n\tconstructor(options: AsyncJobManagerOptions) {\n\t\tthis.#onJobComplete = options.onJobComplete;\n\t\tthis.#maxRunningJobs = Math.max(1, Math.floor(options.maxRunningJobs ?? DEFAULT_MAX_RUNNING_JOBS));\n\t\tthis.#maxRetainedJobs = Math.max(1, Math.floor(options.maxRetainedJobs ?? MAX_MANAGED_BASH_JOBS));\n\t\tthis.#completedJobTtlMs = Math.max(0, Math.floor(options.completedJobTtlMs ?? COMPLETED_JOB_TTL_MS));\n\t}\n\n\tget disposed(): boolean {\n\t\treturn this.#disposed;\n\t}\n\n\tget atCapacity(): boolean {\n\t\tlet running = 0;\n\t\tfor (const job of this.#jobs.values()) if (job.status === \"running\") running += 1;\n\t\treturn running >= this.#maxRunningJobs;\n\t}\n\n\n\tregisterSession(): symbol {\n\t\tif (this.#disposed) throw new Error(\"Async job manager is disposed\");\n\t\tconst sessionId = Symbol(\"async-job-session\");\n\t\tthis.#sessions.set(sessionId, { disposed: false, activeJobIds: new Set() });\n\t\treturn sessionId;\n\t}\n\n\treleaseSession(sessionId: symbol): void {\n\t\tconst session = this.#sessions.get(sessionId);\n\t\tif (!session) return;\n\t\tsession.disposed = true;\n\t\tthis.acknowledgeDeliveries([...session.activeJobIds]);\n\t\tthis.#sessions.delete(sessionId);\n\t\tif (this.#sessions.size === 0) this.dispose();\n\t}\n\n\tisSessionDisposed(sessionId: symbol): boolean {\n\t\treturn this.#disposed || this.#sessions.get(sessionId)?.disposed !== false;\n\t}\n\tregisterBashJob(job: ManagedBashJob, onComplete?: AsyncJobDeliveryHandler, sessionId?: symbol): void {\n\t\tif (this.#disposed) throw new Error(\"Async job manager is disposed\");\n\t\tthis.#pruneRetention();\n\t\tif (this.atCapacity) throw new Error(`Background job limit reached (${this.#maxRunningJobs}). Wait for running jobs to finish or cancel one.`);\n\t\tif (sessionId !== undefined && this.isSessionDisposed(sessionId)) throw new Error(\"Async job session is disposed\");\n\t\tthis.#suppressedDeliveries.delete(job.jobId);\n\t\tif (onComplete) this.#deliveryHandlers.set(job.jobId, onComplete);\n\t\telse this.#deliveryHandlers.delete(job.jobId);\n\t\tif (sessionId !== undefined) {\n\t\t\tthis.#jobSessions.set(job.jobId, sessionId);\n\t\t\tthis.#sessions.get(sessionId)?.activeJobIds.add(job.jobId);\n\t\t} else this.#jobSessions.delete(job.jobId);\n\t\tthis.#jobs.set(job.jobId, job);\n\t\tthis.#pruneRetention();\n\t}\n\n\tcompleteBashJob(job: ManagedBashJob): void {\n\t\tif (this.#disposed || this.#suppressedDeliveries.has(job.jobId)) return;\n\t\tif (job.status === \"running\") return;\n\t\tthis.#jobs.set(job.jobId, job);\n\t\tthis.#pruneRetention();\n\t\tif (this.#jobs.has(job.jobId)) this.#enqueueDelivery(job as ManagedAsyncBashJob);\n\t}\n\n\tacknowledgeDeliveries(jobIds: readonly string[]): void {\n\t\tfor (const jobId of jobIds) {\n\t\t\tthis.#suppressedDeliveries.add(jobId);\n\t\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {\n\t\t\t\tif (this.#deliveries[index]?.jobId === jobId) this.#deliveries.splice(index, 1);\n\t\t\t}\n\t\t\tthis.#deliveryHandlers.delete(jobId);\n\t\t\tthis.#completeTrackedJob(jobId);\n\t\t}\n\t\tthis.#pruneRetention();\n\t}\n\n\tisDeliverySuppressed(jobId: string): boolean {\n\t\treturn this.#suppressedDeliveries.has(jobId);\n\t}\n\n\tdeliveryState(): { queued: number; delivering: boolean; pendingJobIds: string[] } {\n\t\tthis.#pruneRetention();\n\t\treturn {\n\t\t\tqueued: this.#deliveries.length,\n\t\t\tdelivering: this.#runningDeliveryLoop || this.#inFlightDeliveries.size > 0,\n\t\t\tpendingJobIds: [...this.#deliveries.map((delivery) => delivery.jobId), ...this.#inFlightDeliveries.keys()],\n\t\t};\n\t}\n\n\tretentionState(): { jobs: number; suppressions: number; handlers: number; queued: number; sessions: number } {\n\t\tthis.#pruneRetention();\n\t\treturn { jobs: this.#jobs.size, suppressions: this.#suppressedDeliveries.size, handlers: this.#deliveryHandlers.size, queued: this.#deliveries.length + this.#inFlightDeliveries.size, sessions: this.#sessions.size };\n\t}\n\n\tdispose(): void {\n\t\tthis.#disposed = true;\n\t\tif (this.#timer) clearTimeout(this.#timer);\n\t\tthis.#timer = undefined;\n\t\tthis.#deliveries.length = 0;\n\t\tthis.#inFlightDeliveries.clear();\n\t\tthis.#deliveryHandlers.clear();\n\t\tthis.#jobSessions.clear();\n\t\tthis.#sessions.clear();\n\t\tthis.#jobs.clear();\n\t\tthis.#suppressedDeliveries.clear();\n\t}\n\n\t#enqueueDelivery(job: ManagedAsyncBashJob): void {\n\t\tif (this.#inFlightDeliveries.has(job.jobId)) return;\n\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) if (this.#deliveries[index]?.jobId === job.jobId) this.#deliveries.splice(index, 1);\n\t\tthis.#deliveries.push({ jobId: job.jobId, message: formatAsyncResultForFollowUp(job), attempt: 0, nextAttemptAt: Date.now() });\n\t\tthis.#pruneRetention();\n\t\tthis.#scheduleDeliveryLoop(25);\n\t}\n\n\n\t#completeTrackedJob(jobId: string): void {\n\t\tconst sessionId = this.#jobSessions.get(jobId);\n\t\tif (sessionId !== undefined) this.#sessions.get(sessionId)?.activeJobIds.delete(jobId);\n\t\tthis.#jobSessions.delete(jobId);\n\t}\n\t#pruneRetention(now = Date.now()): void {\n\t\tfor (const [jobId, job] of this.#jobs) {\n\t\t\tif (job.status !== \"running\" && job.endedAt !== undefined && now - job.endedAt > this.#completedJobTtlMs) {\n\t\t\t\tthis.#jobs.delete(jobId);\n\t\t\t\tthis.#completeTrackedJob(jobId);\n\t\t\t}\n\t\t}\n\t\tfor (const jobId of this.#suppressedDeliveries) {\n\t\t\tif (!this.#jobs.has(jobId)) this.#suppressedDeliveries.delete(jobId);\n\t\t}\n\t\tconst oldestTerminalJobs = [...this.#jobs.values()]\n\t\t\t.filter((job) => job.status !== \"running\")\n\t\t\t.sort((a, b) => (a.endedAt ?? a.startedAt) - (b.endedAt ?? b.startedAt));\n\t\twhile (this.#jobs.size > this.#maxRetainedJobs && oldestTerminalJobs.length > 0) {\n\t\t\tconst job = oldestTerminalJobs.shift();\n\t\t\tif (job) {\n\t\t\t\tthis.#jobs.delete(job.jobId);\n\t\t\t\tthis.#completeTrackedJob(job.jobId);\n\t\t\t}\n\t\t}\n\t\tfor (let index = this.#deliveries.length - 1; index >= 0; index -= 1) {\n\t\t\tconst delivery = this.#deliveries[index];\n\t\t\tif (!delivery || !this.#jobs.has(delivery.jobId) || this.#suppressedDeliveries.has(delivery.jobId)) this.#deliveries.splice(index, 1);\n\t\t}\n\t\twhile (this.#deliveries.length + this.#inFlightDeliveries.size > this.#maxRetainedJobs && this.#deliveries.length > 0) this.#deliveries.shift();\n\t\tfor (const jobId of this.#deliveryHandlers.keys()) if (!this.#jobs.has(jobId) || this.#suppressedDeliveries.has(jobId)) this.#deliveryHandlers.delete(jobId);\n\t\tfor (const session of this.#sessions.values()) for (const jobId of session.activeJobIds) if (!this.#jobs.has(jobId)) session.activeJobIds.delete(jobId);\n\t}\n\n\t#scheduleDeliveryLoop(delayMs: number): void {\n\t\tif (this.#disposed) return;\n\t\tif (this.#timer) clearTimeout(this.#timer);\n\t\tthis.#timer = setTimeout(() => {\n\t\t\tthis.#timer = undefined;\n\t\t\tvoid this.#runDeliveryLoop();\n\t\t}, delayMs);\n\t\tthis.#timer.unref?.();\n\t}\n\n\tasync #runDeliveryLoop(): Promise<void> {\n\t\tif (this.#runningDeliveryLoop || this.#disposed) return;\n\t\tthis.#runningDeliveryLoop = true;\n\t\ttry {\n\t\t\twhile (!this.#disposed) {\n\t\t\t\tconst now = Date.now();\n\t\t\t\tconst delivery = this.#deliveries.find((candidate) => candidate.nextAttemptAt <= now);\n\t\t\t\tif (!delivery) break;\n\t\t\t\tthis.#deliveries.splice(this.#deliveries.indexOf(delivery), 1);\n\t\t\t\tthis.#startDelivery(delivery);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.#runningDeliveryLoop = false;\n\t\t}\n\t\tthis.#scheduleNextDelivery();\n\t}\n\n\t#startDelivery(delivery: Delivery): void {\n\t\tif (this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId) || this.#inFlightDeliveries.has(delivery.jobId)) return;\n\t\tconst handler = this.#deliveryHandlers.get(delivery.jobId) ?? this.#onJobComplete;\n\t\tthis.#inFlightDeliveries.set(delivery.jobId, delivery);\n\t\tdelivery.promise = (async () => handler(delivery.message))()\n\t\t\t.then(() => {\n\t\t\t\tif (!this.#suppressedDeliveries.has(delivery.jobId) && this.#jobs.has(delivery.jobId)) {\n\t\t\t\t\tthis.#deliveryHandlers.delete(delivery.jobId);\n\t\t\t\t\tthis.#completeTrackedJob(delivery.jobId);\n\t\t\t\t}\n\t\t\t})\n\t\t\t.catch(() => {\n\t\t\t\tif (this.#disposed || this.#suppressedDeliveries.has(delivery.jobId) || !this.#jobs.has(delivery.jobId)) return;\n\t\t\t\tdelivery.attempt += 1;\n\t\t\t\tconst jitter = Math.floor(Math.random() * DELIVERY_RETRY_JITTER_MS);\n\t\t\t\tdelivery.nextAttemptAt = Date.now() + Math.min(DELIVERY_RETRY_MAX_MS, DELIVERY_RETRY_BASE_MS * 2 ** delivery.attempt) + jitter;\n\t\t\t\tthis.#deliveries.push(delivery);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tthis.#inFlightDeliveries.delete(delivery.jobId);\n\t\t\t\tthis.#pruneRetention();\n\t\t\t\tthis.#scheduleNextDelivery();\n\t\t\t});\n\t}\n\n\t#scheduleNextDelivery(): void {\n\t\tconst next = this.#deliveries.reduce<number | undefined>((soonest, delivery) => soonest === undefined ? delivery.nextAttemptAt : Math.min(soonest, delivery.nextAttemptAt), undefined);\n\t\tif (next !== undefined) this.#scheduleDeliveryLoop(Math.max(0, next - Date.now()));\n\t}\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import type { CustomMessage } from "../messages.js";
2
+ import type { SendMessageOptions } from "../extensions/index.js";
3
+ import { AsyncJobManager } from "./job-manager.js";
4
+ import type { AsyncJobDeliveryHandler } from "./types.js";
5
+ export interface SessionAsyncJobManagerHandle {
6
+ manager: AsyncJobManager;
7
+ owns: boolean;
8
+ sessionId: symbol;
9
+ }
10
+ interface AsyncDeliverySession {
11
+ readonly isStreaming?: boolean;
12
+ sendCustomMessage<T>(message: Pick<CustomMessage<T>, "customType" | "content" | "display" | "details">, options?: SendMessageOptions): Promise<void>;
13
+ }
14
+ export declare function createSessionAsyncDeliveryHandler(session: AsyncDeliverySession, manager?: AsyncJobManager, sessionId?: symbol): AsyncJobDeliveryHandler;
15
+ export declare function createSessionAsyncJobManager(session: AsyncDeliverySession): SessionAsyncJobManagerHandle;
16
+ export declare function disposeSessionAsyncJobManager(manager: AsyncJobManager | undefined, sessionId: symbol | undefined): void;
17
+ export {};
18
+ //# sourceMappingURL=session-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../../../src/core/async/session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,uBAAuB,EAA2B,MAAM,YAAY,CAAC;AAEnF,MAAM,WAAW,4BAA4B;IAC5C,OAAO,EAAE,eAAe,CAAC;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CAClB;AACD,UAAU,oBAAoB;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAC/B,iBAAiB,CAAC,CAAC,EAClB,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,YAAY,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC,EACjF,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAAC;CACjB;AAiCD,wBAAgB,iCAAiC,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,uBAAuB,CAUvJ;AAED,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,oBAAoB,GAAG,4BAA4B,CASxG;AAED,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,eAAe,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAIvH","sourcesContent":["import type { CustomMessage } from \"../messages.js\";\nimport type { SendMessageOptions } from \"../extensions/index.js\";\nimport { AsyncJobManager } from \"./job-manager.js\";\nimport type { AsyncJobDeliveryHandler, AsyncJobDeliveryMessage } from \"./types.js\";\n\nexport interface SessionAsyncJobManagerHandle {\n\tmanager: AsyncJobManager;\n\towns: boolean;\n\tsessionId: symbol;\n}\ninterface AsyncDeliverySession {\n\treadonly isStreaming?: boolean;\n\tsendCustomMessage<T>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: SendMessageOptions,\n\t): Promise<void>;\n}\n\nconst STREAMING_DELIVERY_POLL_MS = 10;\n\nfunction scheduleBoundaryCheck(callback: () => void): NodeJS.Timeout {\n\tconst timer = setTimeout(callback, STREAMING_DELIVERY_POLL_MS);\n\ttimer.unref?.();\n\treturn timer;\n}\n\nfunction waitForStreamingBoundary(session: AsyncDeliverySession, isStale: () => boolean): Promise<\"ready\" | \"stale\"> {\n\treturn new Promise((resolve) => {\n\t\tlet timer: NodeJS.Timeout | undefined;\n\t\tconst settle = (value: \"ready\" | \"stale\") => {\n\t\t\tif (timer) clearTimeout(timer);\n\t\t\tresolve(value);\n\t\t};\n\t\tconst check = () => {\n\t\t\ttimer = undefined;\n\t\t\tif (isStale()) {\n\t\t\t\tsettle(\"stale\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (session.isStreaming !== true) {\n\t\t\t\tsettle(\"ready\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\ttimer = scheduleBoundaryCheck(check);\n\t\t};\n\t\tcheck();\n\t});\n}\n\nexport function createSessionAsyncDeliveryHandler(session: AsyncDeliverySession, manager?: AsyncJobManager, sessionId?: symbol): AsyncJobDeliveryHandler {\n\treturn async (message: AsyncJobDeliveryMessage) => {\n\t\tconst isStale = () =>\n\t\t\tmanager?.disposed === true ||\n\t\t\tmanager?.isDeliverySuppressed(message.details.jobId) === true ||\n\t\t\t(sessionId !== undefined && manager?.isSessionDisposed(sessionId) === true);\n\t\tif (await waitForStreamingBoundary(session, isStale) === \"stale\") return;\n\t\tif (isStale()) return;\n\t\tawait session.sendCustomMessage(message, { deliverAs: \"followUp\", triggerTurn: true });\n\t};\n}\n\nexport function createSessionAsyncJobManager(session: AsyncDeliverySession): SessionAsyncJobManagerHandle {\n\tconst existing = AsyncJobManager.instance();\n\tif (existing) return { manager: existing, owns: false, sessionId: existing.registerSession() };\n\tlet manager: AsyncJobManager;\n\tmanager = new AsyncJobManager({\n\t\tonJobComplete: (message) => createSessionAsyncDeliveryHandler(session, manager)(message),\n\t});\n\tAsyncJobManager.setInstance(manager);\n\treturn { manager, owns: true, sessionId: manager.registerSession() };\n}\n\nexport function disposeSessionAsyncJobManager(manager: AsyncJobManager | undefined, sessionId: symbol | undefined): void {\n\tif (!manager || !sessionId) return;\n\tmanager.releaseSession(sessionId);\n\tif (manager.disposed && AsyncJobManager.instance() === manager) AsyncJobManager.setInstance(undefined);\n}\n\n"]}