Skip to content

Commit c539f17

Browse files
committed
fix: preserve agent context when sending pruning notifications
When the context_pruning tool or idle pruning runs while an agent (such as 'build') is active, the notification message should be sent with the same agent context. This prevents the notification from incorrectly appearing outside the agent's message flow. The fix extracts the current agent from the last user message and passes it through the notification chain to sendIgnoredMessage, which includes it in the session.prompt call.
1 parent 50c08ef commit c539f17

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

lib/janitor.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)