Skip to content

Commit af402bd

Browse files
committed
fix model changing when prune invoked
1 parent 5f0520d commit af402bd

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

lib/messages/utils.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UserMessage } from "@opencode-ai/sdk"
2+
import { Logger } from "../logger"
13
import type { WithParts } from "../state"
24

35
/**
@@ -83,10 +85,24 @@ export const getLastUserMessage = (
8385
return null
8486
}
8587

86-
export function findCurrentAgent(messages: WithParts[]): string | undefined {
88+
export function getCurrentParams(
89+
messages: WithParts[],
90+
logger: Logger
91+
): {
92+
providerId: string | undefined,
93+
modelId: string | undefined,
94+
agent: string | undefined
95+
} {
8796
const userMsg = getLastUserMessage(messages)
88-
if (!userMsg) return undefined
89-
return (userMsg.info as any).agent || 'build'
97+
if (!userMsg) {
98+
logger.debug("No user message found when determining current params")
99+
return { providerId: undefined, modelId: undefined, agent: undefined }
100+
}
101+
const agent: string = (userMsg.info as UserMessage).agent
102+
const providerId: string | undefined = (userMsg.info as UserMessage).model.providerID
103+
const modelId: string | undefined = (userMsg.info as UserMessage).model.modelID
104+
105+
return { providerId, modelId, agent }
90106
}
91107

92108
export function buildToolIdList(messages: WithParts[]): string[] {
@@ -101,4 +117,4 @@ export function buildToolIdList(messages: WithParts[]): string[] {
101117
}
102118
}
103119
return toolIds
104-
}
120+
}

lib/prompts/synthetic.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES.
3434
After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `prune` tool also returns a confirmation message listing what was pruned.
3535

3636
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
37+
- NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears.
3738
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune")
3839
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune")
3940
- NEVER acknowledge prune tool output (e.g., "I've pruned 3 tools", "Context pruning complete")

lib/strategies/on-idle.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { SessionState, WithParts, ToolParameterEntry } from "../state"
33
import type { Logger } from "../logger"
44
import type { PluginConfig } from "../config"
55
import { buildAnalysisPrompt } from "../prompt"
6-
import { selectModel, extractModelFromSession, ModelInfo } from "../model-selector"
6+
import { selectModel, ModelInfo } from "../model-selector"
77
import { calculateTokensSaved } from "../utils"
8-
import { findCurrentAgent } from "../messages/utils"
8+
import { getCurrentParams } from "../messages/utils"
99
import { saveSessionState } from "../state/persistence"
1010
import { sendUnifiedNotification } from "../ui/notification"
1111

@@ -224,7 +224,7 @@ export async function runOnIdle(
224224
return null
225225
}
226226

227-
const currentAgent = findCurrentAgent(messages)
227+
const currentParams = getCurrentParams(messages, logger)
228228
const { toolCallIds, toolMetadata } = parseMessages(messages, state.toolParameters)
229229

230230
const alreadyPrunedIds = state.prune.toolIds
@@ -295,7 +295,7 @@ export async function runOnIdle(
295295
newlyPrunedIds,
296296
prunedToolMetadata,
297297
undefined, // reason
298-
currentAgent,
298+
currentParams,
299299
workingDirectory || ""
300300
)
301301

lib/strategies/prune-tool.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tool } from "@opencode-ai/plugin"
22
import type { SessionState, ToolParameterEntry, WithParts } from "../state"
33
import type { PluginConfig } from "../config"
4-
import { findCurrentAgent, buildToolIdList } from "../messages/utils"
4+
import { getCurrentParams, buildToolIdList } from "../messages/utils"
55
import { calculateTokensSaved } from "../utils"
66
import { PruneReason, sendUnifiedNotification } from "../ui/notification"
77
import { formatPruningResultForTool } from "../ui/display-utils"
@@ -68,7 +68,7 @@ export function createPruneTool(
6868
})
6969
const messages: WithParts[] = messagesResponse.data || messagesResponse
7070

71-
const currentAgent: string | undefined = findCurrentAgent(messages)
71+
const currentParams = getCurrentParams(messages, logger)
7272
const toolIdList: string[] = buildToolIdList(messages)
7373

7474
// Validate that all numeric IDs are within bounds
@@ -109,9 +109,10 @@ export function createPruneTool(
109109
pruneToolIds,
110110
toolMetadata,
111111
reason as PruneReason,
112-
currentAgent,
112+
currentParams,
113113
workingDirectory
114114
)
115+
115116
state.stats.totalPruneTokens += state.stats.pruneTokenCounter
116117
state.stats.pruneTokenCounter = 0
117118
state.nudgeCounter = 0

lib/ui/notification.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function sendUnifiedNotification(
6363
pruneToolIds: string[],
6464
toolMetadata: Map<string, ToolParameterEntry>,
6565
reason: PruneReason | undefined,
66-
agent: string | undefined,
66+
params: any,
6767
workingDirectory: string
6868
): Promise<boolean> {
6969
const hasPruned = pruneToolIds.length > 0
@@ -79,23 +79,32 @@ export async function sendUnifiedNotification(
7979
? buildMinimalMessage(state, reason)
8080
: buildDetailedMessage(state, reason, pruneToolIds, toolMetadata, workingDirectory)
8181

82-
await sendIgnoredMessage(client, logger, sessionId, message, agent)
82+
await sendIgnoredMessage(client, sessionId, message, params, logger)
8383
return true
8484
}
8585

8686
export async function sendIgnoredMessage(
8787
client: any,
88-
logger: Logger,
8988
sessionID: string,
9089
text: string,
91-
agent?: string
90+
params: any,
91+
logger: Logger
9292
): Promise<void> {
93+
const agent = params.agent || undefined
94+
const model = params.providerId && params.modelId ? {
95+
providerID: params.providerId,
96+
modelID: params.modelId
97+
} : undefined
98+
9399
try {
94100
await client.session.prompt({
95-
path: { id: sessionID },
101+
path: {
102+
id: sessionID
103+
},
96104
body: {
97105
noReply: true,
98106
agent: agent,
107+
model: model,
99108
parts: [{
100109
type: 'text',
101110
text: text,

0 commit comments

Comments
 (0)