@@ -2,6 +2,8 @@ import { presentThreadShell } from "./models.ts";
22import { threadKey } from "./entities.ts" ;
33import {
44 EnvironmentId ,
5+ EventId ,
6+ MessageId ,
57 ThreadId ,
68 type ScopedThreadRef ,
79 NodeId ,
@@ -13,6 +15,7 @@ import {
1315import { describe , expect , it , vi } from "vite-plus/test" ;
1416import { Atom , AtomRegistry , AsyncResult } from "effect/reactivity" ;
1517import * as Cause from "effect/Cause" ;
18+ import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts" ;
1619import { v2Now , v2Projection , v2ThreadShell } from "./orchestrationV2TestFixtures.ts" ;
1720import {
1821 createQuestionHistoryProjector ,
@@ -360,6 +363,210 @@ it("restores old text answers without mutating history or replacing unchanged ro
360363 expect ( project ( { ...answered , visibleTurnItems : [ ...rows ] } ) [ 0 ] ) . toBe ( result [ 0 ] ) ;
361364} ) ;
362365
366+ type QuestionItem = Extract <
367+ OrchestrationV2ThreadProjection [ "turnItems" ] [ number ] ,
368+ { readonly type : "user_input_request" }
369+ > ;
370+
371+ describe ( "question history through exact row replacements" , ( ) => {
372+ const baseItem = {
373+ threadId : v2Projection . thread . id ,
374+ runId : null ,
375+ nodeId,
376+ providerThreadId : null ,
377+ providerTurnId : null ,
378+ nativeItemRef : null ,
379+ parentItemId : null ,
380+ status : "running" as const ,
381+ title : null ,
382+ startedAt : v2Now ,
383+ completedAt : null ,
384+ updatedAt : v2Now ,
385+ } ;
386+ const assistantItem = ( ordinal : number , text = "Hel" ) => ( {
387+ ...baseItem ,
388+ id : TurnItemId . make ( `assistant-${ ordinal } ` ) ,
389+ ordinal,
390+ type : "assistant_message" as const ,
391+ messageId : MessageId . make ( `message-${ ordinal } ` ) ,
392+ text,
393+ streaming : true ,
394+ } ) ;
395+ const localRow = (
396+ item : OrchestrationV2ThreadProjection [ "turnItems" ] [ number ] ,
397+ position : number ,
398+ ) => ( {
399+ item,
400+ position,
401+ sourceItemId : item . id ,
402+ sourceThreadId : item . threadId ,
403+ visibility : "local" as const ,
404+ } ) ;
405+ const updated = (
406+ current : OrchestrationV2ThreadProjection ,
407+ item : OrchestrationV2ThreadProjection [ "turnItems" ] [ number ] ,
408+ ) =>
409+ applyOrchestrationV2ProjectionEvent ( current , {
410+ id : EventId . make ( `event-${ item . id } ` ) ,
411+ type : "turn-item.updated" ,
412+ threadId : current . thread . id ,
413+ occurredAt : v2Now ,
414+ payload : item ,
415+ } ) ! ;
416+
417+ it ( "reads only the replaced row and leaves the request index alone" , ( ) => {
418+ const size = 1_000 ;
419+ const counts = { items : 0 , requestIds : 0 } ;
420+ const items = Array . from ( { length : size } , ( _ , index ) => assistantItem ( index ) ) ;
421+ const countedRows = items . map ( ( item , index ) => {
422+ const row = localRow ( item , index ) ;
423+ return Object . defineProperty ( row , "item" , {
424+ enumerable : true ,
425+ get : ( ) => {
426+ counts . items += 1 ;
427+ return item ;
428+ } ,
429+ } ) ;
430+ } ) ;
431+ const countedRequests = Array . from ( { length : 400 } , ( _ , index ) => {
432+ const request = {
433+ ...projection . runtimeRequests [ 0 ] ! ,
434+ id : RuntimeRequestId . make ( `request-${ index } ` ) ,
435+ } ;
436+ return Object . defineProperty ( request , "id" , {
437+ enumerable : true ,
438+ get : ( ) => {
439+ counts . requestIds += 1 ;
440+ return RuntimeRequestId . make ( `request-${ index } ` ) ;
441+ } ,
442+ } ) ;
443+ } ) ;
444+ const initial : OrchestrationV2ThreadProjection = {
445+ ...v2Projection ,
446+ turnItems : items ,
447+ visibleTurnItems : countedRows ,
448+ runtimeRequests : countedRequests ,
449+ } ;
450+ const project = createQuestionHistoryProjector ( ) ;
451+ const scan = ( current : OrchestrationV2ThreadProjection ) => {
452+ counts . items = 0 ;
453+ counts . requestIds = 0 ;
454+ const result = project ( current ) ;
455+ return { result, ...counts } ;
456+ } ;
457+ expect ( scan ( initial ) ) . toMatchObject ( { items : size , requestIds : 400 } ) ;
458+
459+ const adjacent = updated ( initial , assistantItem ( size - 1 , "Hello" ) ) ;
460+ const hinted = scan ( adjacent ) ;
461+ expect ( hinted . result ) . toBe ( adjacent . visibleTurnItems ) ;
462+ expect ( hinted ) . toMatchObject ( { items : 0 , requestIds : 0 } ) ;
463+
464+ // A skipped delta has no hint for this exact pair, so it scans again.
465+ const skipped = updated (
466+ updated ( adjacent , assistantItem ( size - 1 , "Hello t" ) ) ,
467+ assistantItem ( size - 1 , "Hello there" ) ,
468+ ) ;
469+ const fallback = scan ( skipped ) ;
470+ expect ( fallback . result ) . toBe ( skipped . visibleTurnItems ) ;
471+ expect ( fallback ) . toMatchObject ( { items : size - 1 , requestIds : 0 } ) ;
472+ } ) ;
473+
474+ it ( "matches a full projection through streamed rows, request changes and answers" , ( ) => {
475+ const question = projection . turnItems [ 0 ] as QuestionItem ;
476+ const resolved = ( answers : Record < string , string > ) => ( {
477+ ...projection . runtimeRequests [ 0 ] ! ,
478+ status : "resolved" as const ,
479+ answers,
480+ } ) ;
481+ const explicitRequestId = RuntimeRequestId . make ( "explicit-question" ) ;
482+ const explicitAnswer = {
483+ requestId : explicitRequestId ,
484+ answers : { next : "Explicit" } ,
485+ attachmentsByQuestionId : { } ,
486+ questionTextById : { next : "What should happen next?" } ,
487+ } ;
488+ const unanswered : QuestionItem = {
489+ ...question ,
490+ id : TurnItemId . make ( "explicit-question-item" ) ,
491+ ordinal : 1 ,
492+ requestId : explicitRequestId ,
493+ } ;
494+ const explicit : QuestionItem = { ...unanswered , questionAnswer : explicitAnswer } ;
495+ const explicitRequest = {
496+ ...resolved ( { next : "From request" } ) ,
497+ id : explicitRequestId ,
498+ } ;
499+ const stream = assistantItem ( 2 ) ;
500+ const inherited = {
501+ ...localRow ( { ...question , id : TurnItemId . make ( "inherited-question-item" ) } , 3 ) ,
502+ visibility : "inherited" as const ,
503+ sourceThreadId : ThreadId . make ( "parent-thread" ) ,
504+ } ;
505+ const turnItems = [ question , explicit , stream ] ;
506+ let current : OrchestrationV2ThreadProjection = {
507+ ...v2Projection ,
508+ turnItems,
509+ visibleTurnItems : [ ...turnItems . map ( localRow ) , inherited ] ,
510+ runtimeRequests : [ projection . runtimeRequests [ 0 ] ! , explicitRequest ] ,
511+ } ;
512+ const project = createQuestionHistoryProjector ( ) ;
513+ const step = ( next : OrchestrationV2ThreadProjection ) => {
514+ current = next ;
515+ const result = project ( next ) ;
516+ expect ( result ) . toEqual ( createQuestionHistoryProjector ( ) ( next ) ) ;
517+ return result ;
518+ } ;
519+ const answerOf = ( rows : ReadonlyArray < { readonly item : unknown } > , index : number ) =>
520+ ( rows [ index ] ! . item as { readonly questionAnswer ?: { readonly answers : unknown } } )
521+ . questionAnswer ?. answers ;
522+
523+ expect ( step ( current ) ) . toBe ( current . visibleTurnItems ) ;
524+ expect ( step ( updated ( current , assistantItem ( 2 , "Hello" ) ) ) ) . toBe ( current . visibleTurnItems ) ;
525+
526+ // The last duplicate request wins, and every row asking it gets the answer.
527+ const answered = step ( {
528+ ...current ,
529+ runtimeRequests : [
530+ projection . runtimeRequests [ 0 ] ! ,
531+ resolved ( { next : "Continue" } ) ,
532+ explicitRequest ,
533+ ] ,
534+ } ) ;
535+ expect ( answerOf ( answered , 0 ) ) . toEqual ( { next : "Continue" } ) ;
536+ expect ( answerOf ( answered , 1 ) ) . toEqual ( { next : "Explicit" } ) ;
537+ expect ( answerOf ( answered , 3 ) ) . toEqual ( { next : "Continue" } ) ;
538+
539+ const whileOverlaid = step ( updated ( current , assistantItem ( 2 , "Hello there" ) ) ) ;
540+ expect ( whileOverlaid [ 0 ] ) . toBe ( answered [ 0 ] ) ;
541+ expect ( whileOverlaid [ 3 ] ) . toBe ( answered [ 3 ] ) ;
542+
543+ const changedAnswer = step ( {
544+ ...current ,
545+ runtimeRequests : [ resolved ( { next : "Stop" } ) , explicitRequest ] ,
546+ } ) ;
547+ expect ( answerOf ( changedAnswer , 0 ) ) . toEqual ( { next : "Stop" } ) ;
548+
549+ const reopened = step ( {
550+ ...current ,
551+ runtimeRequests : [ projection . runtimeRequests [ 0 ] ! , explicitRequest ] ,
552+ } ) ;
553+ expect ( reopened ) . toBe ( current . visibleTurnItems ) ;
554+
555+ // Dropping an explicit answer falls back to its resolved request.
556+ const restored = step ( updated ( current , unanswered ) ) ;
557+ expect ( answerOf ( restored , 1 ) ) . toEqual ( { next : "From request" } ) ;
558+
559+ const retitled : QuestionItem = {
560+ ...unanswered ,
561+ questions : [ { ...question . questions [ 0 ] ! , question : "Now what?" } ] ,
562+ } ;
563+ const retitledRows = step ( updated ( current , retitled ) ) ;
564+ expect ( retitledRows [ 1 ] ! . item ) . toMatchObject ( {
565+ questionAnswer : { questionTextById : { next : "Now what?" } } ,
566+ } ) ;
567+ } ) ;
568+ } ) ;
569+
363570it ( "loads only child approvals and retains their destination across environments" , ( ) => {
364571 const local = EnvironmentId . make ( "local" ) ;
365572 const remote = EnvironmentId . make ( "remote" ) ;
0 commit comments