|
| 1 | +import { PluginConfig } from "../config" |
| 2 | +import { Logger } from "../logger" |
| 3 | +import type { SessionState, WithParts } from "../state" |
| 4 | +import { getFilePathsFromParameters, isFilePathProtected } from "../protected-patterns" |
| 5 | +import { getTotalToolTokens } from "./utils" |
| 6 | + |
| 7 | +/** |
| 8 | + * Supersede Reads strategy - prunes read tool outputs for files that have |
| 9 | + * subsequently been written or edited. When a file is read and later modified, |
| 10 | + * the original read output becomes stale since the file contents have changed. |
| 11 | + * |
| 12 | + * Only prunes reads that are followed by a *successful* write/edit to the same |
| 13 | + * file. Errored writes do not supersede reads because the file was not actually |
| 14 | + * changed. |
| 15 | + * |
| 16 | + * Modifies the session state in place to add pruned tool call IDs. |
| 17 | + */ |
| 18 | +export const supersedeReads = ( |
| 19 | + state: SessionState, |
| 20 | + logger: Logger, |
| 21 | + config: PluginConfig, |
| 22 | + messages: WithParts[], |
| 23 | +): void => { |
| 24 | + if (state.manualMode && !config.manualMode.automaticStrategies) { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + if (!config.strategies.supersedeReads.enabled) { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + const allToolIds = state.toolIdList |
| 33 | + if (allToolIds.length === 0) { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // Filter out IDs already pruned |
| 38 | + const unprunedIds = allToolIds.filter((id) => !state.prune.tools.has(id)) |
| 39 | + if (unprunedIds.length === 0) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Track read tools by file path: filePath -> [{ id, index }] |
| 44 | + // We track index to determine chronological order |
| 45 | + const readsByFile = new Map<string, { id: string; index: number }[]>() |
| 46 | + |
| 47 | + // Track successful write/edit file paths with their index |
| 48 | + const writesByFile = new Map<string, number[]>() |
| 49 | + |
| 50 | + for (let i = 0; i < allToolIds.length; i++) { |
| 51 | + const id = allToolIds[i] |
| 52 | + const metadata = state.toolParameters.get(id) |
| 53 | + if (!metadata) { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + const filePaths = getFilePathsFromParameters(metadata.tool, metadata.parameters) |
| 58 | + if (filePaths.length === 0) { |
| 59 | + continue |
| 60 | + } |
| 61 | + const filePath = filePaths[0] |
| 62 | + |
| 63 | + if (isFilePathProtected(filePaths, config.protectedFilePatterns)) { |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + if (metadata.tool === "read") { |
| 68 | + if (!readsByFile.has(filePath)) { |
| 69 | + readsByFile.set(filePath, []) |
| 70 | + } |
| 71 | + const reads = readsByFile.get(filePath) |
| 72 | + if (reads) { |
| 73 | + reads.push({ id, index: i }) |
| 74 | + } |
| 75 | + } else if ( |
| 76 | + (metadata.tool === "write" || metadata.tool === "edit") && |
| 77 | + metadata.status === "completed" |
| 78 | + ) { |
| 79 | + if (!writesByFile.has(filePath)) { |
| 80 | + writesByFile.set(filePath, []) |
| 81 | + } |
| 82 | + const writes = writesByFile.get(filePath) |
| 83 | + if (writes) { |
| 84 | + writes.push(i) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Find reads that are superseded by subsequent writes/edits |
| 90 | + const newPruneIds: string[] = [] |
| 91 | + |
| 92 | + for (const [filePath, reads] of readsByFile.entries()) { |
| 93 | + const writes = writesByFile.get(filePath) |
| 94 | + if (!writes || writes.length === 0) { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + // For each read, check if there's a write that comes after it |
| 99 | + for (const read of reads) { |
| 100 | + // Skip if already pruned |
| 101 | + if (state.prune.tools.has(read.id)) { |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + // Check if any write comes after this read |
| 106 | + const hasSubsequentWrite = writes.some((writeIndex) => writeIndex > read.index) |
| 107 | + if (hasSubsequentWrite) { |
| 108 | + newPruneIds.push(read.id) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (newPruneIds.length > 0) { |
| 114 | + state.stats.totalPruneTokens += getTotalToolTokens(state, newPruneIds) |
| 115 | + for (const id of newPruneIds) { |
| 116 | + const entry = state.toolParameters.get(id) |
| 117 | + state.prune.tools.set(id, entry?.tokenCount ?? 0) |
| 118 | + } |
| 119 | + logger.debug(`Marked ${newPruneIds.length} superseded read tool calls for pruning`) |
| 120 | + } |
| 121 | +} |
0 commit comments