@@ -75,10 +75,18 @@ type StreamPerfProbe = {
7575 hostUpdatesInsideFrame : number ;
7676} ;
7777
78+ type ToolProjectionProbe = {
79+ beforeThrottleCardCount : number | null ;
80+ afterThrottleCardCount : number | null ;
81+ afterThrottleText : string | null ;
82+ ready : boolean ;
83+ } ;
84+
7885type ScopedWindow = Window & {
7986 ocStreamPerf ?: StreamPerfProbe ;
8087 ocIdleProbe ?: { longTasks : number ; longTaskMs : number } ;
8188 ocBurstDone ?: boolean ;
89+ ocToolProjectionProbe ?: ToolProjectionProbe ;
8290 openclawControlUiE2eGateway ?: {
8391 emit : ( event : string , payload ?: unknown ) => void ;
8492 } ;
@@ -258,7 +266,106 @@ async function openStreamingTurn(
258266 return runId ;
259267}
260268
261- async function emitToolLifecycleFlood (
269+ async function probeDeferredToolProjection ( page : ChatFlowPage , runId : string ) : Promise < void > {
270+ await page . evaluate (
271+ ( { runId : targetRunId , seqSeed } ) => {
272+ const scope = window as ScopedWindow ;
273+ const gateway = scope . openclawControlUiE2eGateway ;
274+ if ( ! gateway ) {
275+ throw new Error ( "mock gateway handle missing" ) ;
276+ }
277+ const probe : ToolProjectionProbe = {
278+ beforeThrottleCardCount : null ,
279+ afterThrottleCardCount : null ,
280+ afterThrottleText : null ,
281+ ready : false ,
282+ } ;
283+ scope . ocToolProjectionProbe = probe ;
284+ let seq = seqSeed ;
285+ const emitToolPhase = ( phase : string , data : Record < string , unknown > ) => {
286+ gateway . emit ( "agent" , {
287+ data : { name : "edit" , phase, toolCallId : "call-1" , ...data } ,
288+ runId : targetRunId ,
289+ seq : ++ seq ,
290+ sessionKey : "main" ,
291+ stream : "tool" ,
292+ ts : Date . now ( ) ,
293+ } ) ;
294+ } ;
295+ const cardSelector = '[data-message-id^="tool:assistant:call-1:"]' ;
296+ gateway . emit ( "chat" , {
297+ deltaText : " working on step 1" ,
298+ message : {
299+ content : [ { text : " working on step 1" , type : "text" } ] ,
300+ role : "assistant" ,
301+ timestamp : Date . now ( ) ,
302+ } ,
303+ runId : targetRunId ,
304+ sessionKey : "main" ,
305+ state : "delta" ,
306+ } ) ;
307+ emitToolPhase ( "start" , { args : { path : "src/file-1.ts" } } ) ;
308+ setTimeout ( ( ) => emitToolPhase ( "update" , { partialResult : "partial output 1" } ) , 10 ) ;
309+ setTimeout ( ( ) => emitToolPhase ( "input_delta" , { diff : { added : 1 , removed : 1 } } ) , 20 ) ;
310+ // Sample before the 80ms owner timer. An eager non-terminal projection
311+ // makes the card visible here and fails the absence contract.
312+ setTimeout ( ( ) => {
313+ probe . beforeThrottleCardCount = document . querySelectorAll ( cardSelector ) . length ;
314+ } , 50 ) ;
315+ // Leave result un-emitted. After the owner timer and two render frames,
316+ // the running card must expose the latest update/input_delta projection.
317+ setTimeout ( ( ) => {
318+ requestAnimationFrame ( ( ) => {
319+ requestAnimationFrame ( ( ) => {
320+ const cards = document . querySelectorAll ( cardSelector ) ;
321+ probe . afterThrottleCardCount = cards . length ;
322+ probe . afterThrottleText = cards [ 0 ] ?. textContent ?? null ;
323+ probe . ready = true ;
324+ } ) ;
325+ } ) ;
326+ } , 120 ) ;
327+ } ,
328+ { runId, seqSeed : TOOL_FLOOD_SEQ_SEED } ,
329+ ) ;
330+ await page . waitForFunction (
331+ ( ) => ( window as ScopedWindow ) . ocToolProjectionProbe ?. ready === true ,
332+ undefined ,
333+ { timeout : 10_000 , polling : 20 } ,
334+ ) ;
335+ }
336+
337+ async function completeFirstToolLifecycle ( page : ChatFlowPage , runId : string ) : Promise < void > {
338+ await page . evaluate (
339+ ( { runId : targetRunId , seq } ) => {
340+ const gateway = ( window as ScopedWindow ) . openclawControlUiE2eGateway ;
341+ if ( ! gateway ) {
342+ throw new Error ( "mock gateway handle missing" ) ;
343+ }
344+ gateway . emit ( "agent" , {
345+ data : {
346+ name : "edit" ,
347+ phase : "result" ,
348+ result : "tool output 1" ,
349+ toolCallId : "call-1" ,
350+ } ,
351+ runId : targetRunId ,
352+ seq,
353+ sessionKey : "main" ,
354+ stream : "tool" ,
355+ ts : Date . now ( ) ,
356+ } ) ;
357+ } ,
358+ { runId, seq : TOOL_FLOOD_SEQ_SEED + 4 } ,
359+ ) ;
360+ await page . evaluate (
361+ ( ) =>
362+ new Promise < void > ( ( resolve ) => {
363+ requestAnimationFrame ( ( ) => requestAnimationFrame ( ( ) => resolve ( ) ) ) ;
364+ } ) ,
365+ ) ;
366+ }
367+
368+ async function emitRemainingToolLifecycleFlood (
262369 page : ChatFlowPage ,
263370 runId : string ,
264371 pairCount : number ,
@@ -274,8 +381,8 @@ async function emitToolLifecycleFlood(
274381 throw new Error ( "mock gateway handle missing" ) ;
275382 }
276383 scope . ocBurstDone = false ;
277- let emitted = 0 ;
278- let seq = seqSeed ;
384+ let emitted = 1 ;
385+ let seq = seqSeed + 4 ;
279386 const emitToolPhase = ( phase : string , data : Record < string , unknown > ) => {
280387 gateway . emit ( "agent" , {
281388 data : {
@@ -321,7 +428,7 @@ async function emitToolLifecycleFlood(
321428 } , phaseIntervalMs ) ;
322429 } , phaseIntervalMs ) ;
323430 } ;
324- setTimeout ( emitCall , 0 ) ;
431+ setTimeout ( emitCall , phaseIntervalMs ) ;
325432 } ,
326433 {
327434 runId,
@@ -424,7 +531,22 @@ suite.define(() => {
424531 await gateway . waitForRequest ( "chat.startup" ) ;
425532 const runId = await openStreamingTurn ( page , gateway , "tool flood probe" ) ;
426533
427- await emitToolLifecycleFlood ( page , runId , TOOL_FLOOD_PAIR_COUNT ) ;
534+ await probeDeferredToolProjection ( page , runId ) ;
535+ const projection = await page . evaluate (
536+ ( ) => ( window as ScopedWindow ) . ocToolProjectionProbe ! ,
537+ ) ;
538+ expect ( projection . beforeThrottleCardCount ) . toBe ( 0 ) ;
539+ expect ( projection . afterThrottleCardCount ) . toBe ( 1 ) ;
540+ expect ( projection . afterThrottleText ) . toContain ( "Editing" ) ;
541+ expect ( projection . afterThrottleText ) . toContain ( "+1" ) ;
542+ expect ( projection . afterThrottleText ) . toContain ( "-1" ) ;
543+
544+ await completeFirstToolLifecycle ( page , runId ) ;
545+ const firstCard = page . locator ( '[data-message-id^="tool:assistant:call-1:"]' ) ;
546+ expect ( await firstCard . count ( ) ) . toBe ( 1 ) ;
547+ expect ( await firstCard . textContent ( ) ) . toContain ( "Edited" ) ;
548+
549+ await emitRemainingToolLifecycleFlood ( page , runId , TOOL_FLOOD_PAIR_COUNT ) ;
428550 const floodCards = page . locator ( '[data-message-id^="tool:assistant:call-"]' ) ;
429551 // Eviction drops the oldest entries and keeps the freshest ones.
430552 await expect
0 commit comments