@@ -44,12 +44,13 @@ export class Janitor {
4444 private workingDirectory ?: string
4545 ) { }
4646
47- private async sendIgnoredMessage ( sessionID : string , text : string ) {
47+ private async sendIgnoredMessage ( sessionID : string , text : string , agent ?: string ) {
4848 try {
4949 await this . client . session . prompt ( {
5050 path : { id : sessionID } ,
5151 body : {
5252 noReply : true ,
53+ agent : agent ,
5354 parts : [ {
5455 type : 'text' ,
5556 text : text ,
@@ -96,6 +97,18 @@ export class Janitor {
9697 return null
9798 }
9899
100+ // Extract the current agent from the last user message to preserve agent context
101+ // Following the same pattern as OpenCode's server.ts
102+ let currentAgent : string | undefined = undefined
103+ for ( let i = messages . length - 1 ; i >= 0 ; i -- ) {
104+ const msg = messages [ i ]
105+ const info = msg . info
106+ if ( info ?. role === 'user' ) {
107+ currentAgent = info . agent || 'build'
108+ break
109+ }
110+ }
111+
99112 const toolCallIds : string [ ] = [ ]
100113 const toolOutputs = new Map < string , string > ( )
101114 const toolMetadata = new Map < string , { tool : string , parameters ?: any } > ( )
@@ -299,15 +312,17 @@ export class Janitor {
299312 expandedLlmPrunedIds ,
300313 toolMetadata ,
301314 tokensSaved ,
302- sessionStats
315+ sessionStats ,
316+ currentAgent
303317 )
304318 } else {
305319 await this . sendAutoModeNotification (
306320 sessionID ,
307321 deduplicatedIds ,
308322 deduplicationDetails ,
309323 tokensSaved ,
310- sessionStats
324+ sessionStats ,
325+ currentAgent
311326 )
312327 }
313328
@@ -534,7 +549,8 @@ export class Janitor {
534549 sessionID : string ,
535550 totalPruned : number ,
536551 tokensSaved : number ,
537- sessionStats : SessionStats
552+ sessionStats : SessionStats ,
553+ agent ?: string
538554 ) {
539555 if ( totalPruned === 0 ) return
540556
@@ -547,21 +563,22 @@ export class Janitor {
547563 message += ` │ Session: ~${ formatTokenCount ( sessionStats . totalTokensSaved ) } tokens, ${ sessionStats . totalToolsPruned } tools`
548564 }
549565
550- await this . sendIgnoredMessage ( sessionID , message )
566+ await this . sendIgnoredMessage ( sessionID , message , agent )
551567 }
552568
553569 private async sendAutoModeNotification (
554570 sessionID : string ,
555571 deduplicatedIds : string [ ] ,
556572 deduplicationDetails : Map < string , any > ,
557573 tokensSaved : number ,
558- sessionStats : SessionStats
574+ sessionStats : SessionStats ,
575+ agent ?: string
559576 ) {
560577 if ( deduplicatedIds . length === 0 ) return
561578 if ( this . pruningSummary === 'off' ) return
562579
563580 if ( this . pruningSummary === 'minimal' ) {
564- await this . sendMinimalNotification ( sessionID , deduplicatedIds . length , tokensSaved , sessionStats )
581+ await this . sendMinimalNotification ( sessionID , deduplicatedIds . length , tokensSaved , sessionStats , agent )
565582 return
566583 }
567584
@@ -590,7 +607,7 @@ export class Janitor {
590607 }
591608 }
592609
593- await this . sendIgnoredMessage ( sessionID , message . trim ( ) )
610+ await this . sendIgnoredMessage ( sessionID , message . trim ( ) , agent )
594611 }
595612
596613 formatPruningResultForTool ( result : PruningResult ) : string {
@@ -621,14 +638,15 @@ export class Janitor {
621638 llmPrunedIds : string [ ] ,
622639 toolMetadata : Map < string , any > ,
623640 tokensSaved : number ,
624- sessionStats : SessionStats
641+ sessionStats : SessionStats ,
642+ agent ?: string
625643 ) {
626644 const totalPruned = deduplicatedIds . length + llmPrunedIds . length
627645 if ( totalPruned === 0 ) return
628646 if ( this . pruningSummary === 'off' ) return
629647
630648 if ( this . pruningSummary === 'minimal' ) {
631- await this . sendMinimalNotification ( sessionID , totalPruned , tokensSaved , sessionStats )
649+ await this . sendMinimalNotification ( sessionID , totalPruned , tokensSaved , sessionStats , agent )
632650 return
633651 }
634652
@@ -680,6 +698,6 @@ export class Janitor {
680698 }
681699 }
682700
683- await this . sendIgnoredMessage ( sessionID , message . trim ( ) )
701+ await this . sendIgnoredMessage ( sessionID , message . trim ( ) , agent )
684702 }
685703}
0 commit comments