@@ -13,6 +13,7 @@ import { DraftGatewayState } from "./draft-gateway-state.ts";
1313import { DraftPlaceBrowser } from "./draft-place-browser.ts" ;
1414import { DraftPlaceState } from "./draft-place-state.ts" ;
1515import { DraftSubmissionFlow } from "./draft-submission-flow.ts" ;
16+ import type { NewSessionRouteData } from "./location.ts" ;
1617import { patchNewSessionPreference } from "./preferences.ts" ;
1718
1819// The closed list of gates allowed to block without a visible reason: the busy
@@ -39,6 +40,7 @@ type FixtureOptions = {
3940 methods ?: string [ ] ;
4041 scopes ?: string [ ] ;
4142 selfUser ?: { id : string } ;
43+ data ?: NewSessionRouteData ;
4244 request ?: ( method : string ) => Promise < unknown > ;
4345} ;
4446
@@ -57,6 +59,7 @@ function createDraftFixture(options: FixtureOptions = {}) {
5759 snapshot : {
5860 phase,
5961 client : phase === "connected" ? client : null ,
62+ sessionKey : "" ,
6063 ...( options . selfUser ? { selfUser : options . selfUser } : { } ) ,
6164 hello :
6265 phase === "connected"
@@ -69,6 +72,7 @@ function createDraftFixture(options: FixtureOptions = {}) {
6972 }
7073 : null ,
7174 } ,
75+ setSessionKey : vi . fn ( ) ,
7276 } ,
7377 agents : {
7478 state : {
@@ -86,14 +90,20 @@ function createDraftFixture(options: FixtureOptions = {}) {
8690 } ,
8791 } ,
8892 sessions : { state : { result : null } , createResult : vi . fn ( ) } ,
89- config : { current : { } } ,
93+ agentSelection : { state : { selectedId : "main" } , set : vi . fn ( ) } ,
94+ config : { current : { cliAgentsEnabled : true , terminalEnabled : true } } ,
95+ navigateAndWait : vi . fn ( async ( ) => undefined ) ,
96+ preload : vi . fn ( async ( ) => undefined ) ,
9097 } as unknown as ApplicationContext ;
98+ vi . mocked ( context . gateway . setSessionKey ) . mockImplementation ( ( sessionKey ) => {
99+ context . gateway . snapshot . sessionKey = sessionKey ;
100+ } ) ;
91101 const host = new ControllerHost ( ) ;
92102 const gateway = new DraftGatewayState (
93103 host ,
94104 ( ) => ( {
95105 context,
96- data : undefined ,
106+ data : options . data ,
97107 isConnected : phase === "connected" ,
98108 isAdmin : place ?. isAdmin ( ) ?? false ,
99109 canStartAsDraft : flow ?. canStartAsDraft ( ) ?? false ,
@@ -141,7 +151,7 @@ function createDraftFixture(options: FixtureOptions = {}) {
141151 browser ,
142152 ( ) => ( {
143153 context,
144- data : undefined ,
154+ data : options . data ,
145155 submitting : flow ?. submitting ?? false ,
146156 pendingPlacementSessionKey : flow ?. pendingPlacement . sessionKey ?? "" ,
147157 } ) ,
@@ -155,7 +165,7 @@ function createDraftFixture(options: FixtureOptions = {}) {
155165 const flow = new DraftSubmissionFlow (
156166 gateway ,
157167 place ,
158- ( ) => ( { context, data : undefined , isConnected : phase === "connected" } ) ,
168+ ( ) => ( { context, data : options . data , isConnected : phase === "connected" } ) ,
159169 { requestUpdate, closeTransientUi : vi . fn ( ) } ,
160170 ) ;
161171 gateway . synchronize ( context . gateway ) ;
@@ -350,6 +360,150 @@ describe("DraftSubmissionFlow submit gates", () => {
350360} ) ;
351361
352362describe ( "DraftSubmissionFlow" , ( ) => {
363+ it ( "surfaces navigation failure after a session has already been created" , async ( ) => {
364+ const { context, flow } = createDraftFixture ( ) ;
365+ vi . mocked ( context . sessions . createResult ) . mockResolvedValue ( {
366+ key : "agent:main:dashboard:created" ,
367+ initialRun : { status : "idle" } ,
368+ } ) ;
369+ vi . mocked ( context . navigateAndWait )
370+ . mockRejectedValueOnce ( new Error ( "Chat route failed to load" ) )
371+ . mockImplementationOnce ( async ( ) => {
372+ queueMicrotask ( ( ) => document . dispatchEvent ( new Event ( CHAT_ROUTE_READY_EVENT ) ) ) ;
373+ } ) ;
374+ flow . setMessage ( "start this task" ) ;
375+
376+ await flow . submit ( ) ;
377+
378+ expect ( context . sessions . createResult ) . toHaveBeenCalledOnce ( ) ;
379+ expect ( context . navigateAndWait ) . toHaveBeenCalledOnce ( ) ;
380+ expect ( flow . error ) . toBe ( "Chat route failed to load" ) ;
381+ expect ( flow . submitting ) . toBe ( false ) ;
382+
383+ const readSignal = flow . attachmentDraft . readSignal ;
384+ flow . attachmentDraft . updatePending ( readSignal , 1 ) ;
385+ expect ( flow . submitBlock ( ) ?. gate ) . toBe ( "attachment-reads" ) ;
386+ expect ( flow . canSubmit ( ) ) . toBe ( false ) ;
387+ await flow . submit ( ) ;
388+ expect ( context . sessions . createResult ) . toHaveBeenCalledOnce ( ) ;
389+ expect ( context . navigateAndWait ) . toHaveBeenCalledOnce ( ) ;
390+ flow . attachmentDraft . updatePending ( readSignal , - 1 ) ;
391+
392+ expect ( flow . canSubmit ( ) ) . toBe ( true ) ;
393+ await flow . submit ( ) ;
394+
395+ expect ( context . navigateAndWait ) . toHaveBeenCalledTimes ( 2 ) ;
396+ expect ( context . sessions . createResult ) . toHaveBeenCalledOnce ( ) ;
397+ expect ( flow . error ) . toBeNull ( ) ;
398+ } ) ;
399+
400+ it . each ( [
401+ {
402+ scenario : "the user edits the draft" ,
403+ retire : ( { flow } : ReturnType < typeof createDraftFixture > ) => flow . setMessage ( "a new task" ) ,
404+ } ,
405+ {
406+ scenario : "the Gateway lifecycle is invalidated" ,
407+ retire : ( { flow } : ReturnType < typeof createDraftFixture > ) => flow . invalidate ( ) ,
408+ } ,
409+ {
410+ scenario : "the draft attachments change" ,
411+ retire : ( { flow } : ReturnType < typeof createDraftFixture > ) => flow . attachmentDraft . replace ( [ ] ) ,
412+ } ,
413+ {
414+ scenario : "the requested session visibility changes" ,
415+ retire : ( { flow } : ReturnType < typeof createDraftFixture > ) => flow . setVisibility ( "draft" ) ,
416+ } ,
417+ {
418+ scenario : "another session becomes selected" ,
419+ retire : ( { context } : ReturnType < typeof createDraftFixture > ) => {
420+ context . gateway . snapshot . sessionKey = "agent:main:dashboard:elsewhere" ;
421+ } ,
422+ } ,
423+ {
424+ scenario : "the selected agent changes" ,
425+ retire : ( { place } : ReturnType < typeof createDraftFixture > ) => place . selectAgentId ( "other" ) ,
426+ } ,
427+ {
428+ scenario : "the Gateway client changes" ,
429+ retire : ( { context } : ReturnType < typeof createDraftFixture > ) => {
430+ const client = context . gateway . snapshot . client ;
431+ if ( client ) {
432+ context . gateway . snapshot . client = new Proxy ( client , { } ) ;
433+ }
434+ } ,
435+ } ,
436+ ] ) ( "never retries a committed session after $scenario" , async ( { retire } ) => {
437+ const fixture = createDraftFixture ( {
438+ agents : [
439+ { id : "main" , workspace : "/workspace" , model : { primary : "openai/test" } } ,
440+ { id : "other" , workspace : "/workspace" , model : { primary : "openai/test" } } ,
441+ ] ,
442+ } ) ;
443+ const { context, flow } = fixture ;
444+ vi . mocked ( context . sessions . createResult )
445+ . mockResolvedValueOnce ( { key : "agent:main:dashboard:old" , initialRun : { status : "idle" } } )
446+ . mockImplementationOnce ( async ( params ) => ( {
447+ key : `agent:${ params ?. agentId ?? fixture . place . agentId } :dashboard:new` ,
448+ initialRun : { status : "idle" } ,
449+ } ) ) ;
450+ vi . mocked ( context . navigateAndWait )
451+ . mockRejectedValueOnce ( new Error ( "old navigation failed" ) )
452+ . mockImplementationOnce ( async ( ) => {
453+ queueMicrotask ( ( ) => document . dispatchEvent ( new Event ( CHAT_ROUTE_READY_EVENT ) ) ) ;
454+ } ) ;
455+ flow . setMessage ( "the committed task" ) ;
456+ await flow . submit ( ) ;
457+
458+ retire ( fixture ) ;
459+ await flow . submit ( ) ;
460+
461+ expect ( context . sessions . createResult ) . toHaveBeenCalledTimes ( 2 ) ;
462+ expect ( context . gateway . snapshot . sessionKey ) . toBe (
463+ `agent:${ fixture . place . agentId } :dashboard:new` ,
464+ ) ;
465+ expect ( context . navigateAndWait ) . toHaveBeenCalledTimes ( 2 ) ;
466+ } ) ;
467+
468+ it ( "retires failed chat navigation after the same draft starts in a terminal" , async ( ) => {
469+ const fixture = createDraftFixture ( {
470+ scopes : [ "operator.admin" , "operator.read" , "operator.write" ] ,
471+ methods : [ "sessions.create" , "sessions.catalog.startTerminal" , "terminal.open" ] ,
472+ data : {
473+ agentId : "main" ,
474+ requestedAgentId : "main" ,
475+ catalogId : "terminal-agent" ,
476+ model : "openai/test" ,
477+ catalogLabel : "Terminal agent" ,
478+ startTerminal : true ,
479+ } ,
480+ request : async ( method ) =>
481+ method === "sessions.catalog.startTerminal" ? { sessionId : "terminal-created" } : { } ,
482+ } ) ;
483+ const { context, flow, request } = fixture ;
484+ vi . mocked ( context . sessions . createResult ) . mockResolvedValue ( {
485+ key : "agent:main:dashboard:chat-created" ,
486+ initialRun : { status : "idle" } ,
487+ } ) ;
488+ vi . mocked ( context . navigateAndWait ) . mockRejectedValue ( new Error ( "Chat route failed to load" ) ) ;
489+ flow . setMessage ( "start this task" ) ;
490+
491+ await flow . submit ( ) ;
492+ expect ( flow . error ) . toBe ( "Chat route failed to load" ) ;
493+ expect ( flow . showStartInTerminal ( ) ) . toBe ( true ) ;
494+
495+ await flow . startInTerminal ( ) ;
496+
497+ expect ( request ) . toHaveBeenCalledWith (
498+ "sessions.catalog.startTerminal" ,
499+ expect . objectContaining ( { catalogId : "terminal-agent" } ) ,
500+ ) ;
501+ expect ( flow . message ) . toBe ( "" ) ;
502+ expect ( flow . canSubmit ( ) ) . toBe ( false ) ;
503+ expect ( context . sessions . createResult ) . toHaveBeenCalledOnce ( ) ;
504+ expect ( context . navigateAndWait ) . toHaveBeenCalledOnce ( ) ;
505+ } ) ;
506+
353507 it ( "makes attachment restore release only displaced payload ids" , ( ) => {
354508 const revokeObjectURL = stubObjectUrls ( "blob:shared" , "blob:displaced" , "blob:incoming" ) ;
355509 const { flow, requestUpdate } = createDraftFixture ( ) ;
@@ -572,7 +726,13 @@ describe("DraftSubmissionFlow", () => {
572726 expect ( context . sessions . createResult ) . not . toHaveBeenCalled ( ) ;
573727 } ) ;
574728
575- it ( "keeps startup progress active through the navigation handoff" , async ( ) => {
729+ it . each ( [
730+ { scenario : "keeps startup progress active through navigation" , navigationError : null } ,
731+ {
732+ scenario : "surfaces navigation failure after placement startup commits" ,
733+ navigationError : "Placement chat route failed to load" ,
734+ } ,
735+ ] ) ( "$scenario" , async ( { navigationError } ) => {
576736 const createResult = vi . fn ( async ( params : Record < string , unknown > ) => ( {
577737 key : String ( params . key ) ,
578738 initialRun : { status : "idle" as const } ,
@@ -584,17 +744,25 @@ describe("DraftSubmissionFlow", () => {
584744 } ) ,
585745 ) ;
586746 let finishNavigation ! : ( ) => void ;
747+ let failedNavigation = false ;
587748 const navigateAndWait = vi . fn (
588- ( _routeId : string , _options ?: Parameters < ApplicationContext [ "navigateAndWait" ] > [ 1 ] ) =>
589- new Promise < void > ( ( resolve ) => {
749+ ( _routeId : string , _options ?: Parameters < ApplicationContext [ "navigateAndWait" ] > [ 1 ] ) => {
750+ if ( navigationError && ! failedNavigation ) {
751+ failedNavigation = true ;
752+ return Promise . reject ( new Error ( navigationError ) ) ;
753+ }
754+ return new Promise < void > ( ( resolve ) => {
590755 finishNavigation = resolve ;
591- } ) ,
756+ } ) ;
757+ } ,
592758 ) ;
593759 const preload = vi . fn (
594760 async ( _routeId : string , _options ?: Parameters < ApplicationContext [ "preload" ] > [ 1 ] ) =>
595761 undefined ,
596762 ) ;
597- const setSessionKey = vi . fn ( ) ;
763+ const setSessionKey = vi . fn ( ( sessionKey : string ) => {
764+ context . gateway . snapshot . sessionKey = sessionKey ;
765+ } ) ;
598766 const selectAgent = vi . fn ( ) ;
599767 const client = {
600768 recoveryScope : "principal-a" ,
@@ -613,6 +781,7 @@ describe("DraftSubmissionFlow", () => {
613781 snapshot : {
614782 phase : "connected" ,
615783 client,
784+ sessionKey : "" ,
616785 hello : {
617786 auth : {
618787 role : "operator" ,
@@ -744,13 +913,15 @@ describe("DraftSubmissionFlow", () => {
744913 const submission = flow . submit ( ) ;
745914 await vi . waitFor ( ( ) => expect ( navigateAndWait ) . toHaveBeenCalledOnce ( ) ) ;
746915
747- expect ( flow . submitting ) . toBe ( true ) ;
748916 expect ( preload ) . toHaveBeenCalledWith ( "chat" , navigateAndWait . mock . calls [ 0 ] ?. [ 1 ] ) ;
749917 expect ( preload . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
750918 navigateAndWait . mock . invocationCallOrder [ 0 ] ?? Number . POSITIVE_INFINITY ,
751919 ) ;
752- finishNavigation ( ) ;
753- document . dispatchEvent ( new Event ( CHAT_ROUTE_READY_EVENT ) ) ;
920+ if ( ! navigationError ) {
921+ expect ( flow . submitting ) . toBe ( true ) ;
922+ finishNavigation ( ) ;
923+ document . dispatchEvent ( new Event ( CHAT_ROUTE_READY_EVENT ) ) ;
924+ }
754925 await submission ;
755926
756927 expect ( start ) . toHaveBeenCalledOnce ( ) ;
@@ -761,10 +932,25 @@ describe("DraftSubmissionFlow", () => {
761932 } ) ;
762933 expect ( flow . pendingPlacement . capture ( ) ) . toBeNull ( ) ;
763934 expect ( flow . attachmentDraft . attachments ) . toHaveLength ( 0 ) ;
935+ expect ( flow . error ) . toBe ( navigationError ) ;
764936 expect ( flow . submitting ) . toBe ( false ) ;
765937 expect ( createResult ) . toHaveBeenCalledOnce ( ) ;
766938 expect ( setSessionKey ) . toHaveBeenCalledWith ( start . mock . calls [ 0 ] ?. [ 0 ] . recovery . sessionKey ) ;
767939 expect ( selectAgent ) . toHaveBeenCalledWith ( "cloud" ) ;
768940 expect ( preload ) . toHaveBeenCalledOnce ( ) ;
941+
942+ if ( navigationError ) {
943+ expect ( flow . canSubmit ( ) ) . toBe ( true ) ;
944+ const retry = flow . submit ( ) ;
945+ await vi . waitFor ( ( ) => expect ( navigateAndWait ) . toHaveBeenCalledTimes ( 2 ) ) ;
946+ expect ( flow . canSubmit ( ) ) . toBe ( false ) ;
947+ finishNavigation ( ) ;
948+ document . dispatchEvent ( new Event ( CHAT_ROUTE_READY_EVENT ) ) ;
949+ await retry ;
950+
951+ expect ( createResult ) . toHaveBeenCalledOnce ( ) ;
952+ expect ( start ) . toHaveBeenCalledOnce ( ) ;
953+ expect ( flow . error ) . toBeNull ( ) ;
954+ }
769955 } ) ;
770956} ) ;
0 commit comments