@@ -29,6 +29,7 @@ const CONDITION_STARTING: &str = "ContainerStarting";
2929use openshell_core:: driver_utils:: {
3030 CONDITION_EXITED , CONDITION_RUNTIME_RESTART , CONDITION_STOPPED ,
3131 CONDITION_WORKSPACE_VALIDATION_FAILED , SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ,
32+ WORKSPACE_VALIDATION_FAILED_MESSAGE ,
3233} ;
3334
3435pub type WatchStream =
@@ -390,7 +391,12 @@ pub async fn inspect_workload(
390391 // Both containers exist before initial start. A missing or exited
391392 // companion therefore requires containment, including after a
392393 // gateway restart that missed the original Podman exit event.
393- Ok ( _) | Err ( PodmanApiError :: NotFound ( _) ) => {
394+ Ok ( supervisor) => {
395+ client. stop_container ( & workload. id , 0 ) . await ?;
396+ workload = client. inspect_container ( & workload. id ) . await ?;
397+ workload. state . supervisor_exit_code = exited_exit_code ( & supervisor. state ) ;
398+ }
399+ Err ( PodmanApiError :: NotFound ( _) ) => {
394400 client. stop_container ( & workload. id , 0 ) . await ?;
395401 workload = client. inspect_container ( & workload. id ) . await ?;
396402 }
@@ -402,10 +408,17 @@ pub async fn inspect_workload(
402408 . await
403409 . ok ( )
404410 . and_then ( |logs| boundary_startup_termination_marker ( & logs) ) ;
411+ workload. state . supervisor_exit_code = supervisor
412+ . ok ( )
413+ . and_then ( |supervisor| exited_exit_code ( & supervisor. state ) ) ;
405414 }
406415 Ok ( workload)
407416}
408417
418+ fn exited_exit_code ( state : & ContainerState ) -> Option < i64 > {
419+ matches ! ( state. status. as_str( ) , "exited" | "stopped" ) . then_some ( state. exit_code )
420+ }
421+
409422/// Extract only fixed, OpenShell-owned startup diagnostics from container
410423/// output. Workload and supervisor output may contain secrets, so it must not
411424/// be propagated to driver conditions or tracing.
@@ -553,10 +566,12 @@ fn condition_from_state(state: &ContainerState) -> DriverCondition {
553566 "OOMKilled" ,
554567 "Container was killed by the OOM killer" . to_string ( ) ,
555568 )
556- } else if state. exit_code == i64:: from ( SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ) {
569+ } else if state. supervisor_exit_code
570+ == Some ( i64:: from ( SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ) )
571+ {
557572 (
558573 CONDITION_WORKSPACE_VALIDATION_FAILED ,
559- "OCI WorkingDir is not usable by the sandbox identity" . to_string ( ) ,
574+ WORKSPACE_VALIDATION_FAILED_MESSAGE . to_string ( ) ,
560575 )
561576 } else if matches ! ( state. exit_code, 137 | 143 ) {
562577 (
@@ -692,6 +707,7 @@ mod tests {
692707 health : None ,
693708 started_at : Some ( "2026-08-12T16:38:58Z" . to_string ( ) ) ,
694709 finished_at : Some ( "2026-08-12T16:39:13Z" . to_string ( ) ) ,
710+ supervisor_exit_code : None ,
695711 startup_diagnostic : None ,
696712 } ;
697713
@@ -741,6 +757,7 @@ mod tests {
741757 } ) ,
742758 started_at : Some ( "2026-04-14T10:00:00Z" . to_string ( ) ) ,
743759 finished_at : None ,
760+ supervisor_exit_code : None ,
744761 startup_diagnostic : None ,
745762 } ;
746763 let cond = condition_from_state ( & state) ;
@@ -760,6 +777,7 @@ mod tests {
760777 health : None ,
761778 started_at : Some ( "2026-04-14T10:00:00Z" . to_string ( ) ) ,
762779 finished_at : None ,
780+ supervisor_exit_code : None ,
763781 startup_diagnostic : None ,
764782 } ;
765783 let cond = condition_from_state ( & state) ;
@@ -782,6 +800,7 @@ mod tests {
782800 } ) ,
783801 started_at : Some ( "2026-04-14T10:00:00Z" . to_string ( ) ) ,
784802 finished_at : None ,
803+ supervisor_exit_code : None ,
785804 startup_diagnostic : None ,
786805 } ;
787806 let condition = condition_from_state ( & state) ;
@@ -800,6 +819,7 @@ mod tests {
800819 health : None ,
801820 started_at : None ,
802821 finished_at : Some ( "2026-04-14T11:00:00Z" . to_string ( ) ) ,
822+ supervisor_exit_code : None ,
803823 startup_diagnostic : None ,
804824 } ;
805825 let cond = condition_from_state ( & state) ;
@@ -818,6 +838,7 @@ mod tests {
818838 health : None ,
819839 started_at : None ,
820840 finished_at : Some ( "2026-04-14T12:00:00Z" . to_string ( ) ) ,
841+ supervisor_exit_code : None ,
821842 startup_diagnostic : None ,
822843 } ;
823844 let cond = condition_from_state ( & state) ;
@@ -836,6 +857,7 @@ mod tests {
836857 health : None ,
837858 started_at : None ,
838859 finished_at : Some ( "2026-04-14T12:00:00Z" . to_string ( ) ) ,
860+ supervisor_exit_code : None ,
839861 startup_diagnostic : boundary_startup_termination_marker (
840862 b"untrusted workload output\n sandbox boundary received SIGTERM before supervisor confirmation\n " ,
841863 ) ,
@@ -860,21 +882,30 @@ mod tests {
860882
861883 #[ test]
862884 fn condition_workspace_validation_exit_is_reported_explicitly ( ) {
863- let state = ContainerState {
885+ // The supervisor exits with the reserved status and the watcher then
886+ // stops the workload, so the workload itself reports SIGKILL.
887+ let mut state = ContainerState {
864888 status : "exited" . to_string ( ) ,
865889 running : false ,
866- exit_code : i64 :: from ( SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ) ,
890+ exit_code : 137 ,
867891 oom_killed : false ,
868892 health : None ,
869893 started_at : None ,
870894 finished_at : Some ( "2026-04-14T12:00:00Z" . to_string ( ) ) ,
895+ supervisor_exit_code : Some ( i64:: from ( SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ) ) ,
871896 startup_diagnostic : None ,
872897 } ;
873898
874899 let cond = condition_from_state ( & state) ;
875900
876901 assert_eq ! ( cond. reason, CONDITION_WORKSPACE_VALIDATION_FAILED ) ;
877- assert ! ( cond. message. contains( "WorkingDir" ) ) ;
902+ assert_eq ! ( cond. message, WORKSPACE_VALIDATION_FAILED_MESSAGE ) ;
903+
904+ // A workload's own exit with the reserved status is not a supervisor
905+ // workspace rejection.
906+ state. exit_code = i64:: from ( SUPERVISOR_EXIT_WORKSPACE_VALIDATION_FAILED ) ;
907+ state. supervisor_exit_code = Some ( 1 ) ;
908+ assert_eq ! ( condition_from_state( & state) . reason, CONDITION_EXITED ) ;
878909 }
879910
880911 #[ test]
@@ -892,6 +923,7 @@ mod tests {
892923 health : None ,
893924 started_at : None ,
894925 finished_at : Some ( "2026-04-14T12:30:00Z" . to_string ( ) ) ,
926+ supervisor_exit_code : None ,
895927 startup_diagnostic : None ,
896928 } ;
897929 let cond = condition_from_state ( & state) ;
0 commit comments