File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { EventEmitter , on } from "node:events" ;
22
33export class TypedEventEmitter < TEvents > extends EventEmitter {
4+ constructor ( ) {
5+ super ( ) ;
6+ this . setMaxListeners ( 50 ) ;
7+ }
8+
49 emit < K extends keyof TEvents & string > (
510 event : K ,
611 payload : TEvents [ K ] ,
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ import { MAIN_TOKENS } from "../../di/tokens.js";
2626import { logger } from "../../lib/logger.js" ;
2727import { TypedEventEmitter } from "../../lib/typed-event-emitter.js" ;
2828import type { ProcessTrackingService } from "../process-tracking/service.js" ;
29- import { SleepService } from "../sleep/service.js" ;
29+ import type { SleepService } from "../sleep/service.js" ;
3030import {
3131 AgentServiceEvent ,
3232 type AgentServiceEvents ,
@@ -873,6 +873,14 @@ For git operations while detached:
873873 sessionCount : sessionIds . length ,
874874 } ) ;
875875
876+ for ( const session of this . sessions . values ( ) ) {
877+ try {
878+ await session . agent . flushAllLogs ( ) ;
879+ } catch {
880+ log . debug ( "Failed to flush session logs during shutdown" ) ;
881+ }
882+ }
883+
876884 for ( const taskRunId of sessionIds ) {
877885 await this . cleanupSession ( taskRunId ) ;
878886 }
Original file line number Diff line number Diff line change @@ -99,7 +99,14 @@ export class Agent {
9999 } ) ;
100100 }
101101
102+ async flushAllLogs ( ) : Promise < void > {
103+ await this . sessionLogWriter ?. flushAll ( ) ;
104+ }
105+
102106 async cleanup ( ) : Promise < void > {
107+ if ( this . sessionLogWriter && this . taskRunId ) {
108+ await this . sessionLogWriter . flush ( this . taskRunId ) ;
109+ }
103110 await this . acpConnection ?. cleanup ( ) ;
104111 }
105112}
Original file line number Diff line number Diff line change @@ -18,25 +18,14 @@ export class SessionLogWriter {
1818 this . posthogAPI = posthogAPI ;
1919 this . logger =
2020 logger ?? new Logger ( { debug : false , prefix : "[SessionLogWriter]" } ) ;
21+ }
2122
22- const flushAllAndExit = async ( ) => {
23- const flushPromises : Promise < void > [ ] = [ ] ;
24- for ( const sessionId of this . configs . keys ( ) ) {
25- flushPromises . push ( this . flush ( sessionId ) ) ;
26- }
27- await Promise . all ( flushPromises ) ;
28- process . exit ( 0 ) ;
29- } ;
30-
31- process . on ( "beforeExit" , ( ) => {
32- flushAllAndExit ( ) . catch ( ( e ) => this . logger . error ( "Flush failed:" , e ) ) ;
33- } ) ;
34- process . on ( "SIGINT" , ( ) => {
35- flushAllAndExit ( ) . catch ( ( e ) => this . logger . error ( "Flush failed:" , e ) ) ;
36- } ) ;
37- process . on ( "SIGTERM" , ( ) => {
38- flushAllAndExit ( ) . catch ( ( e ) => this . logger . error ( "Flush failed:" , e ) ) ;
39- } ) ;
23+ async flushAll ( ) : Promise < void > {
24+ const flushPromises : Promise < void > [ ] = [ ] ;
25+ for ( const sessionId of this . configs . keys ( ) ) {
26+ flushPromises . push ( this . flush ( sessionId ) ) ;
27+ }
28+ await Promise . all ( flushPromises ) ;
4029 }
4130
4231 register ( sessionId : string , config : SessionLogConfig ) : void {
You can’t perform that action at this time.
0 commit comments