Skip to content

Commit b323912

Browse files
committed
fix: properly reset nudge counter after prune tool is used
The nudge counter now correctly resets to zero when prune is used and only counts tools invoked since the last prune. Previously, the counter could become inaccurate across session reloads. Also increased tool cache size to 1000.
1 parent 968d656 commit b323912

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/state/tool-cache.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SessionState, ToolStatus, WithParts } from "./index"
22
import type { Logger } from "../logger"
33
import { PluginConfig } from "../config"
44

5-
const MAX_TOOL_CACHE_SIZE = 500
5+
const MAX_TOOL_CACHE_SIZE = 1000
66

77
/**
88
* Sync tool parameters from OpenCode's session.messages() API.
@@ -16,13 +16,24 @@ export async function syncToolCache(
1616
try {
1717
logger.info("Syncing tool parameters from OpenCode messages")
1818

19+
state.nudgeCounter = 0
20+
1921
for (const msg of messages) {
2022
for (const part of msg.parts) {
21-
if (part.type !== "tool" || !part.callID || state.toolParameters.has(part.callID)) {
23+
if (part.type !== "tool" || !part.callID) {
2224
continue
2325
}
2426

25-
const alreadyPruned = state.prune.toolIds.includes(part.callID)
27+
if (part.tool === "prune") {
28+
state.nudgeCounter = 0
29+
} else if (!config.strategies.pruneTool.protectedTools.includes(part.tool)) {
30+
state.nudgeCounter++
31+
}
32+
state.lastToolPrune = part.tool === "prune"
33+
34+
if (state.toolParameters.has(part.callID)) {
35+
continue
36+
}
2637

2738
state.toolParameters.set(
2839
part.callID,
@@ -34,16 +45,11 @@ export async function syncToolCache(
3445
compacted: part.state.status === "completed" && !!part.state.time.compacted,
3546
}
3647
)
37-
38-
if (!alreadyPruned && !config.strategies.pruneTool.protectedTools.includes(part.tool)) {
39-
state.nudgeCounter++
40-
}
41-
42-
state.lastToolPrune = part.tool === "prune"
43-
logger.info("lastToolPrune=" + String(state.lastToolPrune))
4448
}
4549
}
4650

51+
// logger.info(`nudgeCounter=${state.nudgeCounter}, lastToolPrune=${state.lastToolPrune}`)
52+
4753
trimToolParametersCache(state)
4854
} catch (error) {
4955
logger.warn("Failed to sync tool parameters from OpenCode", {

0 commit comments

Comments
 (0)