@@ -35,6 +35,31 @@ function estimateMessageBytes(message: AgentMessage): number {
|
35 | 35 | return Buffer.byteLength(JSON.stringify(message), "utf8"); |
36 | 36 | } |
37 | 37 | |
| 38 | +function findTranscriptRewriteMatches( |
| 39 | +branch: readonly SessionBranchEntry[], |
| 40 | +replacementsById: ReadonlyMap<string, AgentMessage>, |
| 41 | +): { matchedIndices: number[]; bytesFreed: number } { |
| 42 | +const matchedIndices: number[] = []; |
| 43 | +let bytesFreed = 0; |
| 44 | + |
| 45 | +for (let index = 0; index < branch.length; index++) { |
| 46 | +const entry = branch[index]; |
| 47 | +if (entry.type !== "message") { |
| 48 | +continue; |
| 49 | +} |
| 50 | +const replacement = replacementsById.get(entry.id); |
| 51 | +if (!replacement) { |
| 52 | +continue; |
| 53 | +} |
| 54 | +const originalBytes = estimateMessageBytes(entry.message); |
| 55 | +const replacementBytes = estimateMessageBytes(replacement); |
| 56 | +matchedIndices.push(index); |
| 57 | +bytesFreed += Math.max(0, originalBytes - replacementBytes); |
| 58 | +} |
| 59 | + |
| 60 | +return { matchedIndices, bytesFreed }; |
| 61 | +} |
| 62 | + |
38 | 63 | function remapEntryId( |
39 | 64 | entryId: string | null | undefined, |
40 | 65 | rewrittenEntryIds: ReadonlyMap<string, string>, |
@@ -185,23 +210,7 @@ export function rewriteTranscriptEntriesInSessionManager(params: {
|
185 | 210 | }; |
186 | 211 | } |
187 | 212 | |
188 | | -const matchedIndices: number[] = []; |
189 | | -let bytesFreed = 0; |
190 | | - |
191 | | -for (let index = 0; index < branch.length; index++) { |
192 | | -const entry = branch[index]; |
193 | | -if (entry.type !== "message") { |
194 | | -continue; |
195 | | -} |
196 | | -const replacement = replacementsById.get(entry.id); |
197 | | -if (!replacement) { |
198 | | -continue; |
199 | | -} |
200 | | -const originalBytes = estimateMessageBytes(entry.message); |
201 | | -const replacementBytes = estimateMessageBytes(replacement); |
202 | | -matchedIndices.push(index); |
203 | | -bytesFreed += Math.max(0, originalBytes - replacementBytes); |
204 | | -} |
| 213 | +const { matchedIndices, bytesFreed } = findTranscriptRewriteMatches(branch, replacementsById); |
205 | 214 | |
206 | 215 | if (matchedIndices.length === 0) { |
207 | 216 | return { |
@@ -339,23 +348,7 @@ export function rewriteTranscriptEntriesInState(params: {
|
339 | 348 | }; |
340 | 349 | } |
341 | 350 | |
342 | | -const matchedIndices: number[] = []; |
343 | | -let bytesFreed = 0; |
344 | | - |
345 | | -for (let index = 0; index < branch.length; index++) { |
346 | | -const entry = branch[index]; |
347 | | -if (entry.type !== "message") { |
348 | | -continue; |
349 | | -} |
350 | | -const replacement = replacementsById.get(entry.id); |
351 | | -if (!replacement) { |
352 | | -continue; |
353 | | -} |
354 | | -const originalBytes = estimateMessageBytes(entry.message); |
355 | | -const replacementBytes = estimateMessageBytes(replacement); |
356 | | -matchedIndices.push(index); |
357 | | -bytesFreed += Math.max(0, originalBytes - replacementBytes); |
358 | | -} |
| 351 | +const { matchedIndices, bytesFreed } = findTranscriptRewriteMatches(branch, replacementsById); |
359 | 352 | |
360 | 353 | if (matchedIndices.length === 0) { |
361 | 354 | return { |
|