@@ -15,6 +15,7 @@ import {
1515 ORCHESTRATION_PROTOCOL_HEADER ,
1616 ORCHESTRATION_PROTOCOL_VERSION ,
1717 OrchestrationV2ThreadBoundedSnapshot ,
18+ OrchestrationV2ThreadHistoryPage ,
1819 OrchestrationV2ThreadStreamItem ,
1920 ProjectId ,
2021 ProviderDriverKind ,
@@ -53,6 +54,9 @@ import * as ThreadManagementService from "./ThreadManagementService.ts";
5354const decodeBounded = Schema . decodeUnknownSync (
5455 Schema . fromJsonString ( Schema . toCodecJson ( OrchestrationV2ThreadBoundedSnapshot ) ) ,
5556) ;
57+ const decodeHistoryPage = Schema . decodeUnknownSync (
58+ Schema . fromJsonString ( Schema . toCodecJson ( OrchestrationV2ThreadHistoryPage ) ) ,
59+ ) ;
5660const decodeStreamItem = Schema . decodeUnknownSync (
5761 Schema . toCodecJson ( OrchestrationV2ThreadStreamItem ) ,
5862) ;
@@ -81,6 +85,10 @@ const management = Layer.unwrap(
8185 getThreadSnapshot : ( id ) => projections . getThreadSnapshot ( id ) . pipe ( Effect . orDie ) ,
8286 getThreadSnapshotWindow : ( id , options ) =>
8387 projections . getThreadSnapshotWindow ( id , options ) . pipe ( Effect . orDie ) ,
88+ getThreadHistoryPage : ( id , cursor , throughEntryId , conversationOnly ) =>
89+ projections
90+ . getThreadHistoryPage ( id , cursor , throughEntryId , conversationOnly )
91+ . pipe ( Effect . orDie ) ,
8492 streamStoredEventsFrom : ( input ) =>
8593 input ?. threadId === BODIES ? Stream . fromIterable ( bodiesLiveTail ( ) ) : Stream . empty ,
8694 } ) ;
@@ -492,6 +500,46 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
492500 ) ,
493501 ) ;
494502
503+ it . effect ( "HTTP: the ID table is opt-in and decodes to the same snapshot" , ( ) =>
504+ withHttp ( async ( get ) => {
505+ const base = `/api/orchestration/threads/${ encodeURIComponent ( threadId ) } /bounded?compactTurnItems=1` ;
506+ const plainBody = await ( await get ( base ) ) . text ( ) ;
507+ const tableBody = await ( await get ( `${ base } &compactIds=1` ) ) . text ( ) ;
508+ const wire = JSON . parse ( tableBody ) ;
509+
510+ assert . notProperty ( JSON . parse ( plainBody ) , "ids" ) ;
511+ assert . isArray ( wire . ids ) ;
512+ assert . isNumber ( wire . projection . visibleTurnItems [ 0 ] . item . id ) ;
513+ assert . isNumber ( wire . projection . thread . id ) ;
514+ assert . isBelow ( tableBody . length , plainBody . length ) ;
515+ const decoded = decodeBounded ( tableBody ) ;
516+ assert . deepStrictEqual ( decoded , decodeBounded ( plainBody ) ) ;
517+
518+ // Only the long thread has older history to page.
519+ if ( threadId !== LONG ) return ;
520+ assert . isNotNull ( decoded . historyCursor ) ;
521+ const history = `/api/orchestration/threads/${ encodeURIComponent ( threadId ) } /history?cursor=${ encodeURIComponent ( decoded . historyCursor ?? "" ) } ` ;
522+ const plainPage = await ( await get ( history ) ) . text ( ) ;
523+ const tablePage = await ( await get ( `${ history } &compactIds=1` ) ) . text ( ) ;
524+ assert . isArray ( JSON . parse ( tablePage ) . ids ) ;
525+ const page = decodeHistoryPage ( tablePage ) ;
526+ assert . isAbove ( page . items . length , 0 ) ;
527+ assert . deepStrictEqual ( page , decodeHistoryPage ( plainPage ) ) ;
528+ } ) ,
529+ ) ;
530+
531+ it . effect ( "WS: the ID table is opt-in and decodes to the same snapshot" , ( ) =>
532+ Effect . gen ( function * ( ) {
533+ const input = { threadId, acceptBoundedSnapshot : true , acceptCompactTurnItems : true } ;
534+ const plain = yield * firstSnapshot ( input ) ;
535+ const table = yield * firstSnapshot ( { ...input , acceptCompactIds : true } ) ;
536+
537+ assert . notProperty ( encodeStreamItem ( plain . raw ) , "ids" ) ;
538+ assert . property ( encodeStreamItem ( table . raw ) , "ids" ) ;
539+ assert . deepStrictEqual ( table . decoded , plain . decoded ) ;
540+ } ) ,
541+ ) ;
542+
495543 it . effect ( "WS: bounded snapshots stay unchanged unless the client opts in" , ( ) =>
496544 Effect . gen ( function * ( ) {
497545 const legacy = yield * firstSnapshot ( { threadId, acceptBoundedSnapshot : true } ) ;
@@ -546,6 +594,15 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
546594 boundedSnapshotProjection ( decodeBounded ( dedupedBody ) ) ,
547595 boundedSnapshotProjection ( decodeBounded ( compactBody ) ) ,
548596 ) ;
597+ // The client sends both opt-ins: bodies restore after the ID table resolves.
598+ const tabledBody = await (
599+ await get ( `${ base } ?compactTurnItems=1&dedupeBodies=1&compactIds=1` )
600+ ) . text ( ) ;
601+ assert . property ( JSON . parse ( tabledBody ) , "ids" ) ;
602+ assert . deepStrictEqual (
603+ boundedSnapshotProjection ( decodeBounded ( tabledBody ) ) ,
604+ boundedSnapshotProjection ( decodeBounded ( compactBody ) ) ,
605+ ) ;
549606 } ) ,
550607 ) ;
551608
0 commit comments