@@ -46,6 +46,7 @@ export interface AgentSession {
4646 events : AcpMessage [ ] ;
4747 startedAt : number ;
4848 status : "connecting" | "connected" | "disconnected" | "error" ;
49+ errorMessage ?: string ;
4950 isPromptPending : boolean ;
5051 isCloud : boolean ;
5152 logUrl ?: string ;
@@ -91,6 +92,7 @@ interface SessionActions {
9192 customInput ?: string ,
9293 ) => Promise < void > ;
9394 cancelPermission : ( taskId : string , toolCallId : string ) => Promise < void > ;
95+ clearSessionError : ( taskId : string ) => void ;
9496}
9597
9698interface AuthCredentials {
@@ -160,6 +162,14 @@ function subscribeToChannel(taskRunId: string) {
160162 } ,
161163 onError : ( err ) => {
162164 log . error ( "Session subscription error" , { taskRunId, error : err } ) ;
165+ useStore . setState ( ( state ) => {
166+ const session = state . sessions [ taskRunId ] ;
167+ if ( session ) {
168+ session . status = "error" ;
169+ session . errorMessage =
170+ "Lost connection to the agent. Please restart the task." ;
171+ }
172+ } ) ;
163173 } ,
164174 } ,
165175 ) ;
@@ -640,7 +650,11 @@ const useStore = create<SessionStore>()(
640650 }
641651 } else {
642652 unsubscribeFromChannel ( taskRunId ) ;
643- removeSession ( taskRunId ) ;
653+ updateSession ( taskRunId , {
654+ status : "error" ,
655+ errorMessage :
656+ "Failed to reconnect to the agent. Please restart the task." ,
657+ } ) ;
644658 }
645659 } ;
646660
@@ -652,14 +666,14 @@ const useStore = create<SessionStore>()(
652666 executionMode ?: "plan" | "acceptEdits" ,
653667 ) => {
654668 if ( ! auth . client ) {
655- log . error ( "API client not available" ) ;
656- return ;
669+ throw new Error (
670+ "Unable to reach server. Please check your connection." ,
671+ ) ;
657672 }
658673
659674 const taskRun = await auth . client . createTaskRun ( taskId ) ;
660675 if ( ! taskRun ?. id ) {
661- log . error ( "Task run created without ID" ) ;
662- return ;
676+ throw new Error ( "Failed to create task run. Please try again." ) ;
663677 }
664678
665679 const persistedMode = getPersistedTaskMode ( taskId ) ;
@@ -776,6 +790,12 @@ const useStore = create<SessionStore>()(
776790 const auth = getAuthCredentials ( ) ;
777791 if ( ! auth ) {
778792 log . error ( "Missing auth credentials" ) ;
793+ const taskRunId = latestRun ?. id ?? `error-${ taskId } ` ;
794+ const session = createBaseSession ( taskRunId , taskId , isCloud ) ;
795+ session . status = "error" ;
796+ session . errorMessage =
797+ "Authentication required. Please sign in to continue." ;
798+ addSession ( session ) ;
779799 return ;
780800 }
781801
@@ -787,6 +807,32 @@ const useStore = create<SessionStore>()(
787807 taskDescription ,
788808 ) ;
789809 } else if ( latestRun ?. id && latestRun ?. log_url ) {
810+ // Check if workspace still exists before attempting reconnect
811+ const workspaceExists = await trpcVanilla . workspace . verify . query ( {
812+ taskId,
813+ } ) ;
814+
815+ if ( ! workspaceExists ) {
816+ // Workspace was deleted - show historical logs in error state
817+ log . warn ( "Workspace no longer exists, showing error state" , {
818+ taskId,
819+ } ) ;
820+ const { rawEntries } = await fetchSessionLogs (
821+ latestRun . log_url ,
822+ ) ;
823+ const events = convertStoredEntriesToEvents ( rawEntries ) ;
824+
825+ const session = createBaseSession ( latestRun . id , taskId , false ) ;
826+ session . events = events ;
827+ session . logUrl = latestRun . log_url ;
828+ session . status = "error" ;
829+ session . errorMessage =
830+ "The working directory for this task no longer exists. Please start a new task." ;
831+
832+ addSession ( session ) ;
833+ return ;
834+ }
835+
790836 await reconnectToLocalSession (
791837 taskId ,
792838 latestRun . id ,
@@ -807,6 +853,27 @@ const useStore = create<SessionStore>()(
807853 const message =
808854 error instanceof Error ? error . message : String ( error ) ;
809855 log . error ( "Failed to connect to task" , { message } ) ;
856+
857+ // Create session in error state so user sees what happened
858+ const taskRunId = latestRun ?. id ?? `error-${ taskId } ` ;
859+ const session = createBaseSession ( taskRunId , taskId , isCloud ) ;
860+ session . status = "error" ;
861+ session . errorMessage = `Failed to connect to the agent: ${ message } ` ;
862+
863+ // Try to load historical logs if available
864+ if ( latestRun ?. log_url ) {
865+ try {
866+ const { rawEntries } = await fetchSessionLogs (
867+ latestRun . log_url ,
868+ ) ;
869+ session . events = convertStoredEntriesToEvents ( rawEntries ) ;
870+ session . logUrl = latestRun . log_url ;
871+ } catch {
872+ // Ignore log fetch errors - just show error state without logs
873+ }
874+ }
875+
876+ addSession ( session ) ;
810877 } finally {
811878 connectAttempts . delete ( taskId ) ;
812879 }
@@ -1106,6 +1173,14 @@ const useStore = create<SessionStore>()(
11061173 } ) ;
11071174 }
11081175 } ,
1176+
1177+ clearSessionError : ( taskId : string ) => {
1178+ const session = getSessionByTaskId ( taskId ) ;
1179+ if ( session ) {
1180+ removeSession ( session . taskRunId ) ;
1181+ }
1182+ connectAttempts . delete ( taskId ) ;
1183+ } ,
11091184 } ,
11101185 } ;
11111186 } ) ,
0 commit comments