1- import { describe , expect , it } from "vitest" ;
1+ import { describe , expect , it , vi } from "vitest" ;
22import { parseStandalonePlainTextToolCallBlocks } from "./payload.js" ;
33import {
44 normalizePlainTextToolCallStreamEvents ,
@@ -1068,15 +1068,7 @@ describe("normalizePlainTextToolCallStreamEvents over-cap XML", () => {
10681068 } ) ;
10691069
10701070 it ( "keeps a large preceding block's prefix check bounded across many later candidates (#122513)" , async ( ) => {
1071- // codex review: hasUntrackedPrecedingContext's deep (same-length) check materialized
1072- // the tracked prefix via materializeProtectionPrefix, which joins EVERY chunk ever
1073- // pushed -- including the current, still-growing block's own chunks -- before
1074- // slicing. Doing that once per candidate made a large preceding block followed by a
1075- // bracket-dense later block Theta(precedingBlockSize x candidateCount) again. This
1076- // must stay bounded: derive it once per block (cached) using a prefix-bounded
1077- // accumulator, not a full join, on every one of the many candidates that follow.
1078- // Keep the prefix exactly 200,000 characters while ending its line so the next
1079- // block's delimiter is a real CommonMark fence, not a mid-line backtick run.
1071+ // End the preceding line so the next block opens a CommonMark fence.
10801072 const precedingBlock = `${ "x" . repeat ( 199_999 ) } \n` ;
10811073 const fenced = [
10821074 "```toml" ,
@@ -1099,23 +1091,37 @@ describe("normalizePlainTextToolCallStreamEvents over-cap XML", () => {
10991091 ] ;
11001092
11011093 let resolverCalls = 0 ;
1102- const start = performance . now ( ) ;
1103- const normalized = await collectNormalizedEvents ( events , {
1104- matcher,
1105- createPromotedToolCallEvents : ( ) => [ ] ,
1106- normalizeTerminalMessage : ( ) => undefined ,
1107- protectedRangesFenceCompatible : true ,
1108- resolveProtectedRanges : ( text ) => {
1109- resolverCalls += 1 ;
1110- return resolveTestFenceRanges ( text ) ;
1111- } ,
1112- } ) ;
1113- const elapsedMs = performance . now ( ) - start ;
1094+ let precedingPrefixSlices = 0 ;
1095+ // oxlint-disable-next-line typescript/unbound-method -- called below with the intercepted string receiver.
1096+ const originalSlice = String . prototype . slice ;
1097+ const sliceSpy = vi
1098+ . spyOn ( String . prototype , "slice" )
1099+ . mockImplementation ( function ( this : string , start , end ) {
1100+ if ( start === 0 && end === precedingBlock . length && this . length >= end ) {
1101+ precedingPrefixSlices += 1 ;
1102+ }
1103+ return originalSlice . call ( this , start , end ) ;
1104+ } ) ;
1105+ let normalized : Record < string , unknown > [ ] ;
1106+ try {
1107+ normalized = await collectNormalizedEvents ( events , {
1108+ matcher,
1109+ createPromotedToolCallEvents : ( ) => [ ] ,
1110+ normalizeTerminalMessage : ( ) => undefined ,
1111+ protectedRangesFenceCompatible : true ,
1112+ resolveProtectedRanges : ( text ) => {
1113+ resolverCalls += 1 ;
1114+ return resolveTestFenceRanges ( text ) ;
1115+ } ,
1116+ } ) ;
1117+ } finally {
1118+ sliceSpy . mockRestore ( ) ;
1119+ }
11141120
11151121 expect ( resolverCalls ) . toBeLessThanOrEqual ( 3 ) ;
1116- // A large preceding block re-materialized per candidate would push this into the
1117- // hundreds of milliseconds; bounded (cached) prefix validation stays well under it .
1118- expect ( elapsedMs ) . toBeLessThan ( 200 ) ;
1122+ // Rechecking the entire preceding block for every candidate makes work quadratic.
1123+ // Count full-prefix operations so this bound does not depend on runner speed .
1124+ expect ( precedingPrefixSlices ) . toBeLessThanOrEqual ( 4 ) ;
11191125 expect ( textDeltas ( normalized ) . join ( "" ) ) . toBe ( precedingBlock + fenced ) ;
11201126 } ) ;
11211127
0 commit comments