Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/main/services/AgentBootstrapService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseService, DependsOn, Injectable, Phase, ServicePhase } from '@main/core/lifecycle'
import { BaseService, Injectable, Phase, ServicePhase } from '@main/core/lifecycle'

import { bootstrapBuiltinAgents } from './agents/services/builtin/BuiltinAgentBootstrap'
import { channelManager } from './agents/services/channels'
Expand All @@ -11,26 +11,23 @@ const logger = loggerService.withContext('AgentBootstrapService')
/**
* Lifecycle-managed service that orchestrates agent subsystem initialization.
*
* Wraps the non-lifecycle agent singletons (schedulerService, channelManager,
* bootstrapBuiltinAgents) so their startup/shutdown is managed by the
* application lifecycle instead of manual calls in index.ts.
* Uses Phase.Background — fire-and-forget, never blocks other phases.
* All operations are idempotent and non-critical for UI startup.
*/
@Injectable('AgentBootstrapService')
@ServicePhase(Phase.WhenReady)
@DependsOn(['ApiServerService'])
@ServicePhase(Phase.Background)
export class AgentBootstrapService extends BaseService {
protected async onReady(): Promise<void> {
await bootstrapBuiltinAgents()
logger.info('Built-in agents bootstrapped')

await schedulerService.restoreSchedulers()
logger.info('Schedulers restored')

protected onInit(): void {
registerSessionStreamIpc()
logger.info('Session stream IPC registered')
}

await channelManager.start()
logger.info('Channel manager started')
protected async onReady(): Promise<void> {
await Promise.all([
bootstrapBuiltinAgents().then(() => logger.info('Built-in agents bootstrapped')),
schedulerService.restoreSchedulers().then(() => logger.info('Schedulers restored')),
channelManager.start().then(() => logger.info('Channel manager started'))
])
}

protected async onDestroy(): Promise<void> {
Expand Down
4 changes: 1 addition & 3 deletions src/main/services/agents/services/channels/ChannelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class ChannelManager {
const channels = await channelService.listChannels()
const activeChannels = channels.filter((ch) => ch.isActive && ch.agentId)

for (const channel of activeChannels) {
await this.connectChannelFromRow(channel)
}
await Promise.all(activeChannels.map((channel) => this.connectChannelFromRow(channel)))

logger.info('Channel manager started', { adapterCount: this.adapters.size })
} catch (error) {
Expand Down