Skip to content

Commit cadc113

Browse files
authored
Merge pull request #113 from Tarquinen/fix/tool-count-on-restart
Fix tool result counting on session restart
2 parents bc0c5e5 + e8348c3 commit cadc113

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

lib/core/janitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { z } from "zod"
22
import type { Logger } from "../logger"
33
import type { PruningStrategy } from "../config"
44
import type { PluginState } from "../state"
5-
import type { ToolMetadata, PruneReason, SessionStats, GCStats, PruningResult } from "../fetch-wrapper/types"
5+
import type { ToolMetadata, SessionStats, GCStats, PruningResult } from "../fetch-wrapper/types"
66
import { findCurrentAgent } from "../hooks"
77
import { buildAnalysisPrompt } from "./prompt"
88
import { selectModel, extractModelFromSession } from "../model-selector"

lib/fetch-wrapper/tool-tracker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ export function createToolTracker(): ToolTracker {
1111
export function resetToolTrackerCount(tracker: ToolTracker): void {
1212
tracker.toolResultCount = 0
1313
}
14+
15+
export function clearToolTracker(tracker: ToolTracker): void {
16+
tracker.seenToolResultIds.clear()
17+
tracker.toolResultCount = 0
18+
tracker.skipNextIdle = false
19+
}

lib/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { JanitorContext } from "./core/janitor"
44
import { runOnIdle } from "./core/janitor"
55
import type { PluginConfig, PruningStrategy } from "./config"
66
import type { ToolTracker } from "./fetch-wrapper/tool-tracker"
7-
import { resetToolTrackerCount } from "./fetch-wrapper/tool-tracker"
7+
import { resetToolTrackerCount, clearToolTracker } from "./fetch-wrapper/tool-tracker"
88
import { clearAllMappings } from "./state/id-mapping"
99

1010
export async function isSubagentSession(client: any, sessionID: string): Promise<boolean> {
@@ -80,7 +80,7 @@ export function createChatParamsHandler(
8080
clearAllMappings()
8181
state.toolParameters.clear()
8282
if (toolTracker) {
83-
resetToolTrackerCount(toolTracker)
83+
clearToolTracker(toolTracker)
8484
}
8585
}
8686

lib/state/tool-cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export async function syncToolCache(
3030
}
3131

3232
let synced = 0
33+
// Build lowercase set of pruned IDs for comparison (IDs in state may be mixed case)
34+
const prunedIdsLower = tracker
35+
? new Set((state.prunedIds.get(sessionId) ?? []).map(id => id.toLowerCase()))
36+
: null
3337

3438
for (const msg of messages) {
3539
if (!msg.parts) continue
@@ -43,7 +47,8 @@ export async function syncToolCache(
4347
if (tracker && !tracker.seenToolResultIds.has(id)) {
4448
tracker.seenToolResultIds.add(id)
4549
// Only count non-protected tools toward nudge threshold
46-
if (!part.tool || !protectedTools?.has(part.tool)) {
50+
// Also skip already-pruned tools to avoid re-counting on restart
51+
if ((!part.tool || !protectedTools?.has(part.tool)) && !prunedIdsLower?.has(id)) {
4752
tracker.toolResultCount++
4853
}
4954
}

0 commit comments

Comments
 (0)