Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core/janitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from "zod"
import type { Logger } from "../logger"
import type { PruningStrategy } from "../config"
import type { PluginState } from "../state"
import type { ToolMetadata, PruneReason, SessionStats, GCStats, PruningResult } from "../fetch-wrapper/types"
import type { ToolMetadata, SessionStats, GCStats, PruningResult } from "../fetch-wrapper/types"
import { findCurrentAgent } from "../hooks"
import { buildAnalysisPrompt } from "./prompt"
import { selectModel, extractModelFromSession } from "../model-selector"
Expand Down
6 changes: 6 additions & 0 deletions lib/fetch-wrapper/tool-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ export function createToolTracker(): ToolTracker {
export function resetToolTrackerCount(tracker: ToolTracker): void {
tracker.toolResultCount = 0
}

export function clearToolTracker(tracker: ToolTracker): void {
tracker.seenToolResultIds.clear()
tracker.toolResultCount = 0
tracker.skipNextIdle = false
}
4 changes: 2 additions & 2 deletions lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { JanitorContext } from "./core/janitor"
import { runOnIdle } from "./core/janitor"
import type { PluginConfig, PruningStrategy } from "./config"
import type { ToolTracker } from "./fetch-wrapper/tool-tracker"
import { resetToolTrackerCount } from "./fetch-wrapper/tool-tracker"
import { resetToolTrackerCount, clearToolTracker } from "./fetch-wrapper/tool-tracker"
import { clearAllMappings } from "./state/id-mapping"

export async function isSubagentSession(client: any, sessionID: string): Promise<boolean> {
Expand Down Expand Up @@ -80,7 +80,7 @@ export function createChatParamsHandler(
clearAllMappings()
state.toolParameters.clear()
if (toolTracker) {
resetToolTrackerCount(toolTracker)
clearToolTracker(toolTracker)
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/state/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export async function syncToolCache(
}

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

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