Repository navigation
fix(rtk): skip content-based filter matching for non-shell tool results #2642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 2 commits into
cheaper-inference:release/v3.8.3
from
HALDRO:fix/rtk-skip-filters-non-shell-tools-v2
May 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,7 +268,7 @@ function processRtkCodeBlocksOnly( | |
|
|
||
| export function processRtkText( | ||
| text: string, | ||
| options: { command?: string | null; config?: Partial<RtkConfig> } = {} | ||
| options: { command?: string | null; config?: Partial<RtkConfig>; skipFilters?: boolean } = {} | ||
| ): RtkProcessResult { | ||
| const config = mergeRtkConfig(options.config); | ||
| const originalTokens = estimateCompressionTokens(text); | ||
|
|
@@ -278,20 +278,22 @@ export function processRtkText( | |
| let result = text; | ||
|
|
||
| const detection = detectCommandType(text, options.command); | ||
| const filter = matchRtkFilter(text, detection.command, { | ||
| customFiltersEnabled: config.customFiltersEnabled, | ||
| trustProjectFilters: config.trustProjectFilters, | ||
| }); | ||
| if (filter && !config.disabledFilters.includes(filter.id)) { | ||
| if (config.enabledFilters.length === 0 || config.enabledFilters.includes(filter.id)) { | ||
| const filtered = applyLineFilter(result, { | ||
| ...filter, | ||
| maxLines: filter.maxLines || config.maxLinesPerResult, | ||
| }); | ||
| result = filtered.text; | ||
| if (filtered.appliedRules.length > 0) { | ||
| techniquesUsed.push("rtk-filter"); | ||
| rulesApplied.push(...filtered.appliedRules); | ||
| if (!options.skipFilters) { | ||
| const filter = matchRtkFilter(text, detection.command, { | ||
| customFiltersEnabled: config.customFiltersEnabled, | ||
| trustProjectFilters: config.trustProjectFilters, | ||
| }); | ||
| if (filter && !config.disabledFilters.includes(filter.id)) { | ||
| if (config.enabledFilters.length === 0 || config.enabledFilters.includes(filter.id)) { | ||
| const filtered = applyLineFilter(result, { | ||
| ...filter, | ||
| maxLines: filter.maxLines || config.maxLinesPerResult, | ||
| }); | ||
| result = filtered.text; | ||
| if (filtered.appliedRules.length > 0) { | ||
| techniquesUsed.push("rtk-filter"); | ||
| rulesApplied.push(...filtered.appliedRules); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -362,7 +364,8 @@ export function processRtkText( | |
|
|
||
| function processRtkContent( | ||
| content: Message["content"], | ||
| config: RtkConfig | ||
| config: RtkConfig, | ||
| options?: { command?: string | null; skipFilters?: boolean } | ||
| ): { | ||
| content: Message["content"]; | ||
| compressed: boolean; | ||
|
|
@@ -387,7 +390,11 @@ function processRtkContent( | |
| if (!content) { | ||
| return { content, compressed: false, techniquesUsed, rulesApplied, rawOutputPointers }; | ||
| } | ||
| const processed = processRtkText(content, { config }); | ||
| const processed = processRtkText(content, { | ||
| config, | ||
| command: options?.command, | ||
| skipFilters: options?.skipFilters, | ||
| }); | ||
| collect(processed); | ||
| return { | ||
| content: processed.compressed ? processed.text : content, | ||
|
|
@@ -405,7 +412,11 @@ function processRtkContent( | |
| let compressed = false; | ||
| const nextContent = content.map((part) => { | ||
| if (!isTextBlock(part) || !part.text) return part; | ||
| const processed = processRtkText(part.text, { config }); | ||
| const processed = processRtkText(part.text, { | ||
| config, | ||
| command: options?.command, | ||
| skipFilters: options?.skipFilters, | ||
| }); | ||
| collect(processed); | ||
| if (!processed.compressed) return part; | ||
| compressed = true; | ||
|
|
@@ -438,9 +449,64 @@ export function applyRtkCompression( | |
| const allTechniques: string[] = []; | ||
| const allRules: string[] = []; | ||
| const rawOutputPointers: RtkRawOutputPointer[] = []; | ||
|
|
||
| // Build tool_call_id → tool metadata lookup from assistant messages. | ||
| // This lets us distinguish bash tool results (which RTK filters are designed for) | ||
| // from non-shell tool results (read, grep, glob, etc.) that should skip filters. | ||
| const toolCallLookup = new Map<string, { toolName: string; command: string | null }>(); | ||
| for (const msg of messages) { | ||
| if (msg.role !== "assistant") continue; | ||
| const toolCalls = msg.tool_calls; | ||
| if (!Array.isArray(toolCalls)) continue; | ||
| for (const tc of toolCalls as Array<Record<string, unknown>>) { | ||
| if (!tc || typeof tc !== "object") continue; | ||
| const id = typeof tc.id === "string" ? tc.id : null; | ||
| if (!id) continue; | ||
| const fn = tc.function as Record<string, unknown> | undefined; | ||
| if (!fn || typeof fn !== "object") continue; | ||
| const toolName = typeof fn.name === "string" ? fn.name : ""; | ||
| let command: string | null = null; | ||
| if (typeof fn.arguments === "string") { | ||
| try { | ||
| const args = JSON.parse(fn.arguments); | ||
| command = | ||
| typeof args.command === "string" | ||
| ? args.command | ||
| : typeof args.cmd === "string" | ||
| ? args.cmd | ||
| : null; | ||
| } catch { | ||
| // non-JSON arguments | ||
| } | ||
| } | ||
| toolCallLookup.set(id, { toolName, command }); | ||
| } | ||
| } | ||
|
|
||
| const compressedMessages = messages.map((message) => { | ||
| if (!shouldCompressMessage(message, config)) return message; | ||
| const processed = processRtkContent(message.content, config); | ||
|
|
||
| // Resolve tool metadata from the preceding assistant tool_calls entry. | ||
| let command: string | null = null; | ||
| let skipFilters = false; | ||
| if (message.role === "tool") { | ||
| const callId = typeof message.tool_call_id === "string" ? message.tool_call_id : null; | ||
| const meta = callId ? toolCallLookup.get(callId) : null; | ||
| if (meta) { | ||
| const name = meta.toolName.toLowerCase(); | ||
| // Match the same terminal tool pattern used in grok-web.ts isTerminalTool(). | ||
| // Only apply RTK filters to bash/shell tool results. | ||
| // Non-shell tools (read, glob, grep, edit, write, etc.) skip filter matching | ||
| // to prevent content-based false positives (e.g. a TS file matching build-typescript). | ||
| if (/\b(bash|shell|terminal|run_command|execute_command|exec|command)\b/.test(name)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| command = meta.command; | ||
| } else { | ||
| skipFilters = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const processed = processRtkContent(message.content, config, { command, skipFilters }); | ||
| allTechniques.push(...processed.techniquesUsed); | ||
| allRules.push(...processed.rulesApplied); | ||
| rawOutputPointers.push(...processed.rawOutputPointers); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for extracting the
commandargument has two potential issues:fn.argumentswhen it is a string. Some providers or internal transformations may pass arguments as a pre-parsed object.JSON.parsecan returnnull(e.g., if the input is the string"null"), which would cause an error when accessingargs.commandorargs.cmd. Although wrapped in atry-catch, it's better to explicitly check the type and value ofargsfor robustness.