Skip to content

Commit eb37c5e

Browse files
authored
Merge pull request #43 from Tarquinen/fix/preserve-agent-context-in-pruning-notifications
fix: preserve agent context when sending pruning notifications
2 parents 50c08ef + ac68e28 commit eb37c5e

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Add to your OpenCode config:
1313
```jsonc
1414
// opencode.jsonc
1515
{
16-
"plugin": ["@tarquinen/[email protected].25"]
16+
"plugin": ["@tarquinen/[email protected].26"]
1717
}
1818
```
1919

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
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://json.schemastore.org/package.json",
33
"name": "@tarquinen/opencode-dcp",
4-
"version": "0.3.25",
4+
"version": "0.3.26",
55
"type": "module",
66
"description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context",
77
"main": "./dist/index.js",

0 commit comments

Comments
 (0)