Skip to content

Commit 0c1017a

Browse files
committed
chore: remove obvious comments
1 parent 56a13a9 commit 0c1017a

File tree

9 files changed

+4
-116
lines changed

9 files changed

+4
-116
lines changed

lib/api-formats/synth-instruction.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export function createToolTracker(): ToolTracker {
99
return { seenToolResultIds: new Set(), toolResultCount: 0, skipNextIdle: false }
1010
}
1111

12-
/** Reset tool count to 0 (called after a prune event) */
1312
export function resetToolTrackerCount(tracker: ToolTracker): void {
1413
tracker.toolResultCount = 0
1514
}
@@ -25,7 +24,6 @@ export function trackNewToolResults(messages: any[], tracker: ToolTracker, prote
2524
if (m.role === 'tool' && m.tool_call_id) {
2625
if (!tracker.seenToolResultIds.has(m.tool_call_id)) {
2726
tracker.seenToolResultIds.add(m.tool_call_id)
28-
// Skip protected tools for nudge frequency counting
2927
const toolName = tracker.getToolName?.(m.tool_call_id)
3028
if (!toolName || !protectedTools.has(toolName)) {
3129
tracker.toolResultCount++
@@ -37,7 +35,6 @@ export function trackNewToolResults(messages: any[], tracker: ToolTracker, prote
3735
if (part.type === 'tool_result' && part.tool_use_id) {
3836
if (!tracker.seenToolResultIds.has(part.tool_use_id)) {
3937
tracker.seenToolResultIds.add(part.tool_use_id)
40-
// Skip protected tools for nudge frequency counting
4138
const toolName = tracker.getToolName?.(part.tool_use_id)
4239
if (!toolName || !protectedTools.has(toolName)) {
4340
tracker.toolResultCount++
@@ -63,12 +60,10 @@ export function trackNewToolResultsGemini(contents: any[], tracker: ToolTracker,
6360
if (!Array.isArray(content.parts)) continue
6461
for (const part of content.parts) {
6562
if (part.functionResponse) {
66-
// Use position-based ID since Gemini doesn't have tool_call_id
6763
const positionId = `gemini_pos_${positionCounter}`
6864
positionCounter++
6965
if (!tracker.seenToolResultIds.has(positionId)) {
7066
tracker.seenToolResultIds.add(positionId)
71-
// Skip protected tools for nudge frequency counting
7267
const toolName = part.functionResponse.name
7368
if (!toolName || !protectedTools.has(toolName)) {
7469
tracker.toolResultCount++
@@ -91,7 +86,6 @@ export function trackNewToolResultsResponses(input: any[], tracker: ToolTracker,
9186
if (item.type === 'function_call_output' && item.call_id) {
9287
if (!tracker.seenToolResultIds.has(item.call_id)) {
9388
tracker.seenToolResultIds.add(item.call_id)
94-
// Skip protected tools for nudge frequency counting
9589
const toolName = tracker.getToolName?.(item.call_id)
9690
if (!toolName || !protectedTools.has(toolName)) {
9791
tracker.toolResultCount++
@@ -103,11 +97,6 @@ export function trackNewToolResultsResponses(input: any[], tracker: ToolTracker,
10397
return newCount
10498
}
10599

106-
// ============================================================================
107-
// OpenAI Chat / Anthropic Format
108-
// ============================================================================
109-
110-
/** Check if a message content matches nudge text (OpenAI/Anthropic format) */
111100
function isNudgeMessage(msg: any, nudgeText: string): boolean {
112101
if (typeof msg.content === 'string') {
113102
return msg.content === nudgeText
@@ -138,11 +127,6 @@ export function injectSynth(messages: any[], instruction: string, nudgeText: str
138127
return false
139128
}
140129

141-
// ============================================================================
142-
// Google/Gemini Format (body.contents with parts)
143-
// ============================================================================
144-
145-
/** Check if a Gemini content matches nudge text */
146130
function isNudgeContentGemini(content: any, nudgeText: string): boolean {
147131
if (Array.isArray(content.parts) && content.parts.length === 1) {
148132
const part = content.parts[0]
@@ -169,11 +153,6 @@ export function injectSynthGemini(contents: any[], instruction: string, nudgeTex
169153
return false
170154
}
171155

172-
// ============================================================================
173-
// OpenAI Responses API Format (body.input with type-based items)
174-
// ============================================================================
175-
176-
/** Check if a Responses API item matches nudge text */
177156
function isNudgeItemResponses(item: any, nudgeText: string): boolean {
178157
if (typeof item.content === 'string') {
179158
return item.content === nudgeText

lib/fetch-wrapper/gemini.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ export async function handleGemini(
2222

2323
let modified = false
2424

25-
// Inject synthetic instructions if onTool strategies are enabled
2625
if (ctx.config.strategies.onTool.length > 0) {
27-
// Inject base synthetic instructions (appended to last user content)
2826
if (injectSynthGemini(body.contents, ctx.prompts.synthInstruction, ctx.prompts.nudgeInstruction)) {
2927
modified = true
3028
}
3129

32-
// Build and inject prunable tools list at the end
3330
const sessionId = ctx.state.lastSeenSessionId
3431
if (sessionId) {
3532
const toolIds = Array.from(ctx.state.toolParameters.keys())
@@ -45,7 +42,6 @@ export async function handleGemini(
4542
)
4643

4744
if (prunableList) {
48-
// Track new tool results and check if nudge threshold is met
4945
const protectedSet = new Set(ctx.config.protectedTools)
5046
trackNewToolResultsGemini(body.contents, ctx.toolTracker, protectedSet)
5147
const includeNudge = ctx.config.nudge_freq > 0 && ctx.toolTracker.toolResultCount > ctx.config.nudge_freq
@@ -63,7 +59,6 @@ export async function handleGemini(
6359
}
6460
}
6561

66-
// Check for functionResponse parts in any content item
6762
const hasFunctionResponses = body.contents.some((content: any) =>
6863
Array.isArray(content.parts) &&
6964
content.parts.some((part: any) => part.functionResponse)
@@ -79,7 +74,6 @@ export async function handleGemini(
7974
return { modified, body }
8075
}
8176

82-
// Find the active session to get the position mapping
8377
const activeSessions = allSessions.data?.filter((s: any) => !s.parentID) || []
8478
let positionMapping: Map<string, string> | undefined
8579

@@ -112,25 +106,21 @@ export async function handleGemini(
112106
totalFunctionResponses++
113107
const funcName = part.functionResponse.name?.toLowerCase()
114108

115-
// Count as prunable if not a protected tool
116109
if (!funcName || !protectedToolsLower.has(funcName)) {
117110
prunableFunctionResponses++
118111
}
119112

120113
if (funcName) {
121-
// Get current position for this tool name and increment counter
122114
const currentIndex = toolPositionCounters.get(funcName) || 0
123115
toolPositionCounters.set(funcName, currentIndex + 1)
124116

125-
// Look up the tool call ID using position
126117
const positionKey = `${funcName}:${currentIndex}`
127118
const toolCallId = positionMapping!.get(positionKey)
128119

129120
if (toolCallId && allPrunedIds.has(toolCallId)) {
130121
contentModified = true
131122
replacedCount++
132123
// Preserve thoughtSignature if present (required for Gemini 3 Pro)
133-
// response must be a Struct (object), not a plain string
134124
return {
135125
...part,
136126
functionResponse: {

lib/fetch-wrapper/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,21 @@ export function installFetchWrapper(
5555
const inputUrl = typeof input === 'string' ? input : 'URL object'
5656
let modified = false
5757

58-
// Capture tool IDs before handlers run to track what gets cached this request
5958
const toolIdsBefore = new Set(state.toolParameters.keys())
6059

61-
// Try each format handler - mutually exclusive to avoid double-processing
62-
// OpenAI Responses API style (body.input) - check first as it may also have messages
60+
// Mutually exclusive format handlers to avoid double-processing
6361
if (body.input && Array.isArray(body.input)) {
6462
const result = await handleOpenAIResponses(body, ctx, inputUrl)
6563
if (result.modified) {
6664
modified = true
6765
}
6866
}
69-
// OpenAI Chat Completions & Anthropic style (body.messages)
7067
else if (body.messages && Array.isArray(body.messages)) {
7168
const result = await handleOpenAIChatAndAnthropic(body, ctx, inputUrl)
7269
if (result.modified) {
7370
modified = true
7471
}
7572
}
76-
// Google/Gemini style (body.contents)
7773
else if (body.contents && Array.isArray(body.contents)) {
7874
const result = await handleGemini(body, ctx, inputUrl)
7975
if (result.modified) {

lib/fetch-wrapper/openai-chat.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ export async function handleOpenAIChatAndAnthropic(
2222
return { modified: false, body }
2323
}
2424

25-
// Cache tool parameters from messages (OpenAI and Anthropic formats)
2625
cacheToolParametersFromMessages(body.messages, ctx.state, ctx.logger)
2726

2827
let modified = false
2928

30-
// Inject synthetic instructions if onTool strategies are enabled
3129
if (ctx.config.strategies.onTool.length > 0) {
32-
// Inject base synthetic instructions (appended to last user message)
3330
if (injectSynth(body.messages, ctx.prompts.synthInstruction, ctx.prompts.nudgeInstruction)) {
3431
modified = true
3532
}
3633

37-
// Build and inject prunable tools list at the end
3834
const sessionId = ctx.state.lastSeenSessionId
3935
if (sessionId) {
4036
const toolIds = Array.from(ctx.state.toolParameters.keys())
@@ -50,7 +46,6 @@ export async function handleOpenAIChatAndAnthropic(
5046
)
5147

5248
if (prunableList) {
53-
// Track new tool results and check if nudge threshold is met
5449
const protectedSet = new Set(ctx.config.protectedTools)
5550
trackNewToolResults(body.messages, ctx.toolTracker, protectedSet)
5651
const includeNudge = ctx.config.nudge_freq > 0 && ctx.toolTracker.toolResultCount > ctx.config.nudge_freq
@@ -68,12 +63,8 @@ export async function handleOpenAIChatAndAnthropic(
6863
}
6964
}
7065

71-
// Check for tool messages in both formats:
72-
// 1. OpenAI style: role === 'tool'
73-
// 2. Anthropic style: role === 'user' with content containing tool_result
7466
const protectedToolsLower = new Set(ctx.config.protectedTools.map(t => t.toLowerCase()))
7567

76-
// Count all tool messages
7768
const toolMessages = body.messages.filter((m: any) => {
7869
if (m.role === 'tool') return true
7970
if (m.role === 'user' && Array.isArray(m.content)) {
@@ -84,11 +75,9 @@ export async function handleOpenAIChatAndAnthropic(
8475
return false
8576
})
8677

87-
// Count only prunable (non-protected) tool messages for the total
8878
let prunableToolCount = 0
8979
for (const m of body.messages) {
9080
if (m.role === 'tool') {
91-
// Get tool name from cached metadata
9281
const toolId = m.tool_call_id?.toLowerCase()
9382
const metadata = toolId ? ctx.state.toolParameters.get(toolId) : undefined
9483
if (!metadata || !protectedToolsLower.has(metadata.tool.toLowerCase())) {
@@ -116,7 +105,6 @@ export async function handleOpenAIChatAndAnthropic(
116105
let replacedCount = 0
117106

118107
body.messages = body.messages.map((m: any) => {
119-
// OpenAI style: role === 'tool' with tool_call_id
120108
if (m.role === 'tool' && allPrunedIds.has(m.tool_call_id?.toLowerCase())) {
121109
replacedCount++
122110
return {
@@ -125,7 +113,6 @@ export async function handleOpenAIChatAndAnthropic(
125113
}
126114
}
127115

128-
// Anthropic style: role === 'user' with content array containing tool_result
129116
if (m.role === 'user' && Array.isArray(m.content)) {
130117
let messageModified = false
131118
const newContent = m.content.map((part: any) => {

lib/fetch-wrapper/openai-responses.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ export async function handleOpenAIResponses(
2222
return { modified: false, body }
2323
}
2424

25-
// Cache tool parameters from input (OpenAI Responses API format)
2625
cacheToolParametersFromInput(body.input, ctx.state, ctx.logger)
2726

2827
let modified = false
2928

30-
// Inject synthetic instructions if onTool strategies are enabled
3129
if (ctx.config.strategies.onTool.length > 0) {
32-
// Inject base synthetic instructions (appended to last user message)
3330
if (injectSynthResponses(body.input, ctx.prompts.synthInstruction, ctx.prompts.nudgeInstruction)) {
3431
modified = true
3532
}
3633

37-
// Build and inject prunable tools list at the end
3834
const sessionId = ctx.state.lastSeenSessionId
3935
if (sessionId) {
4036
const toolIds = Array.from(ctx.state.toolParameters.keys())
@@ -50,7 +46,6 @@ export async function handleOpenAIResponses(
5046
)
5147

5248
if (prunableList) {
53-
// Track new tool results and check if nudge threshold is met
5449
const protectedSet = new Set(ctx.config.protectedTools)
5550
trackNewToolResultsResponses(body.input, ctx.toolTracker, protectedSet)
5651
const includeNudge = ctx.config.nudge_freq > 0 && ctx.toolTracker.toolResultCount > ctx.config.nudge_freq
@@ -68,7 +63,6 @@ export async function handleOpenAIResponses(
6863
}
6964
}
7065

71-
// Check for function_call_output items
7266
const functionOutputs = body.input.filter((item: any) => item.type === 'function_call_output')
7367

7468
if (functionOutputs.length === 0) {
@@ -81,7 +75,6 @@ export async function handleOpenAIResponses(
8175
return { modified, body }
8276
}
8377

84-
// Count only prunable (non-protected) function outputs for the total
8578
const protectedToolsLower = new Set(ctx.config.protectedTools.map(t => t.toLowerCase()))
8679
let prunableFunctionOutputCount = 0
8780
for (const item of functionOutputs) {

lib/hooks.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export function createEventHandler(
3232
if (await isSubagentSession(client, event.properties.sessionID)) return
3333
if (config.strategies.onIdle.length === 0) return
3434

35-
// Skip idle pruning if the last tool used was prune
36-
// and idle strategies cover the same work as tool strategies
35+
// Skip idle pruning if the last tool used was prune and idle strategies cover tool strategies
3736
if (toolTracker?.skipNextIdle) {
3837
toolTracker.skipNextIdle = false
3938
if (toolStrategiesCoveredByIdle(config.strategies.onIdle, config.strategies.onTool)) {
@@ -73,22 +72,17 @@ export function createChatParamsHandler(
7372
providerID = input.message.model.providerID
7473
}
7574

76-
// Detect session change and reset per-session state
7775
if (state.lastSeenSessionId && state.lastSeenSessionId !== sessionId) {
7876
logger.info("chat.params", "Session changed, resetting state", {
7977
from: state.lastSeenSessionId.substring(0, 8),
8078
to: sessionId.substring(0, 8)
8179
})
82-
// Clear ID mappings from previous session
8380
clearAllMappings()
84-
// Clear tool parameters cache (not session-scoped, so must be cleared)
8581
state.toolParameters.clear()
8682
}
8783

88-
// Track the last seen session ID for fetch wrapper correlation
8984
state.lastSeenSessionId = sessionId
9085

91-
// Check if this is a subagent session
9286
if (!state.checkedSessions.has(sessionId)) {
9387
state.checkedSessions.add(sessionId)
9488
const isSubagent = await isSubagentSession(client, sessionId)
@@ -108,7 +102,6 @@ export function createChatParamsHandler(
108102
const messages = messagesResponse.data || messagesResponse
109103

110104
if (Array.isArray(messages)) {
111-
// Build position mapping: track tool calls by name and occurrence index
112105
const toolCallsByName = new Map<string, string[]>()
113106

114107
for (const msg of messages) {
@@ -125,7 +118,6 @@ export function createChatParamsHandler(
125118
}
126119
}
127120

128-
// Create position mapping: "toolName:index" -> toolCallId
129121
const positionMapping = new Map<string, string>()
130122
for (const [toolName, callIds] of toolCallsByName) {
131123
callIds.forEach((callId, index) => {

0 commit comments

Comments
 (0)