@@ -43,18 +43,41 @@ export class AgentManager extends EventEmitter {
4343 this . slackNotifier = slackNotifier || new SlackNotifier ( ) ;
4444 this . feishuNotifier = feishuNotifier || new FeishuNotifier ( '' , '' ) ;
4545
46- // On startup, mark any monitor-owned agents that were left in running/waiting_input as
47- // stopped — their processes died when the server restarted .
48- // External agents are handled by ExternalAgentScanner (it checks if PID is still alive).
46+ // On startup, collect monitor-owned agents that were left running when the server
47+ // restarted. External agents are handled by ExternalAgentScanner .
48+ const agentsToResume : Agent [ ] = [ ] ;
4949 for ( const agent of this . store . getAllAgents ( ) ) {
5050 if ( agent . source === 'external' ) continue ;
5151 if ( agent . status === 'running' || agent . status === 'waiting_input' ) {
52- agent . status = 'stopped' ;
5352 agent . pid = undefined ;
53+ if ( agent . sessionId ) {
54+ // Has a session — we can resume it automatically.
55+ agentsToResume . push ( agent ) ;
56+ agent . status = 'stopped' ; // temporarily mark stopped until resume kicks in
57+ } else {
58+ agent . status = 'stopped' ;
59+ }
5460 this . store . saveAgent ( agent ) ;
5561 }
5662 }
5763
64+ // Auto-resume interrupted agents after a short delay so the rest of the
65+ // server (routes, sockets) has time to initialise.
66+ if ( agentsToResume . length > 0 ) {
67+ setTimeout ( ( ) => {
68+ for ( const agent of agentsToResume ) {
69+ console . log ( `[AgentManager] Auto-resuming interrupted agent ${ agent . id } (session: ${ agent . sessionId } )` ) ;
70+ agent . messages . push ( {
71+ id : uuid ( ) ,
72+ role : 'system' ,
73+ content : 'Server restarted — automatically resuming session.' ,
74+ timestamp : Date . now ( ) ,
75+ } ) ;
76+ this . resumeAgent ( agent , 'The server was restarted while you were working. Continue where you left off.' ) ;
77+ }
78+ } , 3000 ) ;
79+ }
80+
5881 // Periodically check for stuck agents (sent user message but no response)
5982 this . stuckCheckInterval = setInterval ( ( ) => this . checkStuckAgents ( ) , STUCK_CHECK_INTERVAL_MS ) ;
6083 }
0 commit comments