99 ORCHESTRATION_PROTOCOL_HEADER ,
1010 ORCHESTRATION_PROTOCOL_VERSION_TEXT ,
1111 OrchestrationV2AppThread ,
12+ OrchestrationV2ShellSnapshot ,
13+ OrchestrationV2ShellSnapshotJson ,
14+ type ThreadPullRequestLink ,
1215 OrchestrationV2TurnItem ,
1316 OrchestrationV2ThreadTranscript ,
1417 PROVIDER_SEND_TURN_MAX_FILE_BYTES ,
@@ -41,13 +44,21 @@ import * as ThreadManagementService from "./ThreadManagementService.ts";
4144import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts" ;
4245import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts" ;
4346
44- class TranscriptTestApi extends HttpApi . make ( "environment" ) . add (
47+ class OrchestrationTestApi extends HttpApi . make ( "environment" ) . add (
4548 EnvironmentHttpApi . groups . orchestration ,
4649) { }
4750const decodeTranscript = Schema . decodeUnknownEffect (
4851 Schema . toCodecJson ( OrchestrationV2ThreadTranscript ) ,
4952) ;
5053
54+ const decodeShellSnapshot = Schema . decodeUnknownEffect (
55+ Schema . toCodecJson ( OrchestrationV2ShellSnapshot ) ,
56+ ) ;
57+
58+ const decodeShellSnapshotWire = Schema . decodeUnknownEffect (
59+ Schema . toEncoded ( OrchestrationV2ShellSnapshotJson ) ,
60+ ) ;
61+
5162const now = DateTime . makeUnsafe ( "2026-09-29T00:00:00.000Z" ) ;
5263const environmentId = EnvironmentId . make ( "source-بيئة" ) ;
5364const output = "Full tool output. " . repeat ( 4_000 ) ;
@@ -96,27 +107,69 @@ const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
96107 } ) ) ,
97108) ;
98109
99- it . effect ( "exports full history with size, scope, and missing-thread checks" , ( ) =>
100- Effect . gen ( function * ( ) {
101- let allowed = true ;
102- const runtime = ProviderReplayHarness . layerWithRegistry (
103- { name : "thread-transcript" } ,
104- ProviderAdapterRegistry . layerFromAdapters ( [ ] ) ,
105- { runEffectWorker : false } ,
106- ) ;
107- const services = yield * Layer . build (
108- Layer . mergeAll (
109- runtime ,
110- ThreadManagementService . layer . pipe (
111- Layer . provide (
112- Layer . mergeAll ( runtime , CommandReceiptStore . layer , ServerSettings . layerTest ( { } ) ) ,
110+ const makeHttpHarness = Effect . fnUntraced ( function * ( allowed = ( ) => true ) {
111+ const runtime = ProviderReplayHarness . layerWithRegistry (
112+ { name : "orchestration-http" } ,
113+ ProviderAdapterRegistry . layerFromAdapters ( [ ] ) ,
114+ { runEffectWorker : false } ,
115+ ) ;
116+ const services = yield * Layer . build (
117+ Layer . mergeAll (
118+ runtime ,
119+ ThreadManagementService . layer . pipe (
120+ Layer . provide (
121+ Layer . mergeAll ( runtime , CommandReceiptStore . layer , ServerSettings . layerTest ( { } ) ) ,
122+ ) ,
123+ ) ,
124+ OrchestrationEventStore . layer ,
125+ ProjectStore . layer ,
126+ ) . pipe ( Layer . provideMerge ( SqlitePersistence . layerMemory ) ) ,
127+ ) ;
128+ const eventSink = Context . get ( services , EventSinkV2 ) ;
129+ const routes = HttpApiBuilder . layer ( OrchestrationTestApi ) . pipe (
130+ Layer . provide ( OrchestrationHttp . layer ) ,
131+ Layer . provide (
132+ Layer . succeed ( ServerEnvironment . ServerEnvironmentIdentity , {
133+ getEnvironmentId : Effect . succeed ( environmentId ) ,
134+ } ) ,
135+ ) ,
136+ Layer . provide (
137+ Layer . succeed ( EnvironmentAuthenticatedAuth , ( effect ) =>
138+ Effect . suspend ( ( ) =>
139+ effect . pipe (
140+ Effect . provideService ( EnvironmentAuthenticatedPrincipal , {
141+ sessionId : AuthSessionId . make ( "test-session" ) ,
142+ subject : "test" ,
143+ method : "bearer-access-token" ,
144+ scopes : new Set < AuthEnvironmentScope > ( allowed ( ) ? [ "orchestration:read" ] : [ ] ) ,
145+ } ) ,
113146 ) ,
114147 ) ,
115- OrchestrationEventStore . layer ,
116- ProjectStore . layer ,
117- ) . pipe ( Layer . provideMerge ( SqlitePersistence . layerMemory ) ) ,
148+ ) ,
149+ ) ,
150+ Layer . provide ( Layer . succeedContext ( services ) ) ,
151+ Layer . provide ( Layer . mock ( ProjectEnrichmentService ) ( { } ) ) ,
152+ Layer . provide ( HttpPlatform . layer ) ,
153+ Layer . provide ( Etag . layerWeak ) ,
154+ Layer . provide ( NodeServices . layer ) ,
155+ ) ;
156+ const { handler, dispose } = HttpRouter . toWebHandler ( routes , { disableLogger : true } ) ;
157+ yield * Effect . addFinalizer ( ( ) => Effect . promise ( dispose ) ) ;
158+ const request = ( path : string ) =>
159+ Effect . promise ( ( ) =>
160+ handler (
161+ new Request ( `http://localhost${ path } ` , {
162+ headers : { [ ORCHESTRATION_PROTOCOL_HEADER ] : ORCHESTRATION_PROTOCOL_VERSION_TEXT } ,
163+ } ) ,
164+ ) ,
118165 ) ;
119- const eventSink = Context . get ( services , EventSinkV2 ) ;
166+ return { services, eventSink, request } ;
167+ } ) ;
168+
169+ it . effect ( "exports full history with size, scope, and missing-thread checks" , ( ) =>
170+ Effect . gen ( function * ( ) {
171+ let allowed = true ;
172+ const { services, eventSink, request } = yield * makeHttpHarness ( ( ) => allowed ) ;
120173 yield * eventSink . write ( {
121174 events : [
122175 {
@@ -128,43 +181,8 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
128181 } ,
129182 ] ,
130183 } ) ;
131- const routes = HttpApiBuilder . layer ( TranscriptTestApi ) . pipe (
132- Layer . provide ( OrchestrationHttp . layer ) ,
133- Layer . provide (
134- Layer . succeed ( ServerEnvironment . ServerEnvironmentIdentity , {
135- getEnvironmentId : Effect . succeed ( environmentId ) ,
136- } ) ,
137- ) ,
138- Layer . provide (
139- Layer . succeed ( EnvironmentAuthenticatedAuth , ( effect ) =>
140- Effect . suspend ( ( ) =>
141- effect . pipe (
142- Effect . provideService ( EnvironmentAuthenticatedPrincipal , {
143- sessionId : AuthSessionId . make ( "test-session" ) ,
144- subject : "test" ,
145- method : "bearer-access-token" ,
146- scopes : new Set < AuthEnvironmentScope > ( allowed ? [ "orchestration:read" ] : [ ] ) ,
147- } ) ,
148- ) ,
149- ) ,
150- ) ,
151- ) ,
152- Layer . provide ( Layer . succeedContext ( services ) ) ,
153- Layer . provide ( Layer . mock ( ProjectEnrichmentService ) ( { } ) ) ,
154- Layer . provide ( HttpPlatform . layer ) ,
155- Layer . provide ( Etag . layerWeak ) ,
156- Layer . provide ( NodeServices . layer ) ,
157- ) ;
158- const { handler, dispose } = HttpRouter . toWebHandler ( routes , { disableLogger : true } ) ;
159- yield * Effect . addFinalizer ( ( ) => Effect . promise ( dispose ) ) ;
160184 const read = ( threadId : ThreadId ) =>
161- Effect . promise ( ( ) =>
162- handler (
163- new Request ( `http://localhost/api/orchestration/threads/${ threadId } /transcript` , {
164- headers : { [ ORCHESTRATION_PROTOCOL_HEADER ] : ORCHESTRATION_PROTOCOL_VERSION_TEXT } ,
165- } ) ,
166- ) ,
167- ) ;
185+ request ( `/api/orchestration/threads/${ threadId } /transcript` ) ;
168186
169187 const emptyResponse = yield * read ( thread . id ) ;
170188 expect ( emptyResponse . status ) . toBe ( 200 ) ;
@@ -242,3 +260,66 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
242260 expect ( ( yield * read ( thread . id ) ) . status ) . toBe ( 403 ) ;
243261 } ) ,
244262) ;
263+
264+ it . effect ( "deduplicates equal pull request lists only in opted-in HTTP shell snapshots" , ( ) =>
265+ Effect . gen ( function * ( ) {
266+ const { eventSink, request } = yield * makeHttpHarness ( ) ;
267+ const link = ( number : number ) : ThreadPullRequestLink => ( {
268+ host : "github.com" ,
269+ repository : "pingdotgg/t3code" ,
270+ number,
271+ url : `https://github.com/pingdotgg/t3code/pull/${ number } ` ,
272+ source : "agent" ,
273+ linkedAt : "2026-09-29T00:00:00.000Z" ,
274+ snapshot : null ,
275+ stack : null ,
276+ } ) ;
277+ const lists = [ [ link ( 1 ) , link ( 2 ) ] , [ link ( 1 ) , link ( 2 ) ] , [ link ( 1 ) ] ] ;
278+ yield * eventSink . write ( {
279+ events : lists . map ( ( pullRequests , index ) => {
280+ const id = ThreadId . make ( `shell-${ index } ` ) ;
281+ return {
282+ id : EventId . make ( `create-${ id } ` ) ,
283+ type : "thread.created" as const ,
284+ threadId : id ,
285+ occurredAt : now ,
286+ payload : { ...thread , id, pullRequests } ,
287+ } ;
288+ } ) ,
289+ } ) ;
290+
291+ const defaultResponse = yield * request ( "/api/orchestration/shell" ) ;
292+ expect ( defaultResponse . status ) . toBe ( 200 ) ;
293+ const json = yield * Effect . promise ( ( ) => defaultResponse . json ( ) ) ;
294+ const full = yield * decodeShellSnapshotWire ( json ) ;
295+ const decoded = yield * decodeShellSnapshot ( json ) ;
296+ expect ( decoded . threads ) . toHaveLength ( 3 ) ;
297+ for ( const [ index , links ] of lists . entries ( ) ) {
298+ expect ( decoded . threads . find ( ( row ) => row . id === `shell-${ index } ` ) ?. pullRequests ) . toEqual (
299+ links ,
300+ ) ;
301+ }
302+ expect ( full . threads . every ( ( row ) => ! ( "pullRequestReference" in row ) ) ) . toBe ( true ) ;
303+
304+ const response = yield * request ( "/api/orchestration/shell?deduplicatePullRequests=true" ) ;
305+ expect ( response . status ) . toBe ( 200 ) ;
306+ const compact = yield * Effect . promise ( ( ) => response . json ( ) ) ;
307+ const duplicateIndices = decoded . threads . flatMap ( ( row , index ) =>
308+ row . id === "shell-0" || row . id === "shell-1" ? [ index ] : [ ] ,
309+ ) ;
310+ const [ first , duplicate ] = duplicateIndices ;
311+ expect ( first ) . toBeDefined ( ) ;
312+ expect ( duplicate ) . toBeDefined ( ) ;
313+ expect ( compact ) . toEqual ( {
314+ ...full ,
315+ threads : full . threads . map ( ( row , index ) => {
316+ if ( index !== duplicate ) return row ;
317+ const { pullRequests : _links , ...rest } = row ;
318+ return { ...rest , pullRequestReference : first } ;
319+ } ) ,
320+ } ) ;
321+ const disabled = yield * request ( "/api/orchestration/shell?deduplicatePullRequests=false" ) ;
322+ expect ( disabled . status ) . toBe ( 200 ) ;
323+ expect ( yield * Effect . promise ( ( ) => disabled . json ( ) ) ) . toEqual ( full ) ;
324+ } ) ,
325+ ) ;
0 commit comments