Skip to content

Commit 0f53637

Browse files
committed
refactor: filter protected tools at cache time
- Skip caching protected tools in syncToolParametersFromOpenCode() - Use missing toolName as protected signal in handler replacement loop - Remove totalCached from log output (internal detail)
1 parent f8895f2 commit 0f53637

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/fetch-wrapper/handler.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ export async function handleFormat(
6767
let modified = false
6868

6969
// Sync tool parameters from OpenCode's session API (single source of truth)
70+
// Also tracks new tool results for nudge injection
7071
const sessionId = ctx.state.lastSeenSessionId
72+
const protectedSet = new Set(ctx.config.protectedTools)
7173
if (sessionId) {
72-
await syncToolParametersFromOpenCode(ctx.client, sessionId, ctx.state, ctx.logger)
74+
await syncToolParametersFromOpenCode(ctx.client, sessionId, ctx.state, ctx.toolTracker, protectedSet, ctx.logger)
7375
}
7476

7577
if (ctx.config.strategies.onTool.length > 0) {
@@ -91,8 +93,6 @@ export async function handleFormat(
9193
)
9294

9395
if (prunableList) {
94-
const protectedSet = new Set(ctx.config.protectedTools)
95-
format.trackNewToolResults(data, ctx.toolTracker, protectedSet)
9696
const includeNudge = ctx.config.nudge_freq > 0 && ctx.toolTracker.toolResultCount > ctx.config.nudge_freq
9797

9898
const endInjection = buildEndInjection(prunableList, includeNudge)
@@ -119,14 +119,12 @@ export async function handleFormat(
119119
}
120120

121121
const toolOutputs = format.extractToolOutputs(data, ctx.state)
122-
const protectedToolsLower = new Set(ctx.config.protectedTools.map(t => t.toLowerCase()))
123122
let replacedCount = 0
124123
let prunableCount = 0
125124

126125
for (const output of toolOutputs) {
127-
if (output.toolName && protectedToolsLower.has(output.toolName.toLowerCase())) {
128-
continue
129-
}
126+
// Skip tools not in cache (protected tools are excluded from cache)
127+
if (!output.toolName) continue
130128
prunableCount++
131129

132130
if (allPrunedIds.has(output.id)) {

lib/state/tool-cache.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PluginState, ToolStatus } from "./index"
22
import type { Logger } from "../logger"
3+
import type { ToolTracker } from "../fetch-wrapper/tool-tracker"
34

45
/** Maximum number of entries to keep in the tool parameters cache */
56
const MAX_TOOL_CACHE_SIZE = 500
@@ -8,11 +9,16 @@ const MAX_TOOL_CACHE_SIZE = 500
89
* Sync tool parameters from OpenCode's session.messages() API.
910
* This is the single source of truth for tool parameters, replacing
1011
* format-specific parsing from LLM API requests.
12+
*
13+
* Also tracks new tool results for nudge injection, consolidating
14+
* what was previously done via format-specific trackNewToolResults().
1115
*/
1216
export async function syncToolParametersFromOpenCode(
1317
client: any,
1418
sessionId: string,
1519
state: PluginState,
20+
tracker?: ToolTracker,
21+
protectedTools?: Set<string>,
1622
logger?: Logger
1723
): Promise<void> {
1824
try {
@@ -36,9 +42,21 @@ export async function syncToolParametersFromOpenCode(
3642

3743
const id = part.callID.toLowerCase()
3844

39-
// Skip if already cached (optimization)
45+
// Track tool results for nudge injection (replaces format-specific trackNewToolResults)
46+
if (tracker && !tracker.seenToolResultIds.has(id)) {
47+
tracker.seenToolResultIds.add(id)
48+
// Only count non-protected tools toward nudge threshold
49+
if (!part.tool || !protectedTools?.has(part.tool)) {
50+
tracker.toolResultCount++
51+
}
52+
}
53+
54+
// Skip if already cached (optimization for parameter caching only)
4055
if (state.toolParameters.has(id)) continue
4156

57+
// Skip protected tools - they shouldn't be in the cache at all
58+
if (part.tool && protectedTools?.has(part.tool)) continue
59+
4260
const status = part.state?.status as ToolStatus | undefined
4361
state.toolParameters.set(id, {
4462
tool: part.tool,
@@ -55,8 +73,7 @@ export async function syncToolParametersFromOpenCode(
5573
if (logger && synced > 0) {
5674
logger.debug("tool-cache", "Synced tool parameters from OpenCode", {
5775
sessionId: sessionId.slice(0, 8),
58-
synced,
59-
totalCached: state.toolParameters.size
76+
synced
6077
})
6178
}
6279
} catch (error) {

0 commit comments

Comments
 (0)