diff --git a/codex-rs/windows-sandbox-rs/src/identity.rs b/codex-rs/windows-sandbox-rs/src/identity.rs index 022c9f8ee5..e730e34c05 100644 --- a/codex-rs/windows-sandbox-rs/src/identity.rs +++ b/codex-rs/windows-sandbox-rs/src/identity.rs @@ -320,6 +320,10 @@ pub(crate) fn require_sandbox_account( proxy_settings_mode, run_automatic_setup, local_user_flags, + |home| { + crate::registered_core_requested() + && !crate::app_package::registered_setup_is_ready(home).unwrap_or(false) + }, ) } @@ -328,6 +332,7 @@ fn require_sandbox_account_with_setup( proxy_settings_mode: crate::WindowsSandboxProxySettingsMode, run_full_setup: impl FnOnce(SandboxSetupRequest<'_>, &OfflineProxySettings) -> Result<()>, read_local_user_flags: impl Fn(&str) -> Result>, + registration_required: impl Fn(&Path) -> bool, ) -> Result<(SandboxCreds, OfflineProxySettings)> { let &SandboxSetupRequest { permissions, @@ -391,6 +396,11 @@ fn require_sandbox_account_with_setup( } } + if identity.is_some() && registration_required(codex_home) { + setup_reason = Some("protected Core registration is missing or out of date".to_string()); + identity = None; + } + if identity.is_none() { if let Some(reason) = &setup_reason { crate::logging::log_note( @@ -410,6 +420,10 @@ fn require_sandbox_account_with_setup( }, &desired_offline_proxy_settings, )?; + ensure!( + !registration_required(codex_home), + "protected Core setup did not publish ready runtime ownership" + ); for username in [OFFLINE_USERNAME, ONLINE_USERNAME] { if let Ok(Some(flags)) = read_local_user_flags(username) { anyhow::ensure!( diff --git a/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs b/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs index 9e9bfe4c2c..6d83d5b739 100644 --- a/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs @@ -69,12 +69,14 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R let permissions = ResolvedWindowsSandboxPermissions::try_from_permission_profile( &PermissionProfile::read_only(), )?; - for (expired_username, repair_succeeds) in [ - (None, true), - (Some(OFFLINE_USERNAME), true), - (Some(ONLINE_USERNAME), true), - (Some(OFFLINE_USERNAME), false), - (Some(ONLINE_USERNAME), false), + for (expired_username, repair_succeeds, needs_registration) in [ + (None, true, false), + (None, true, true), + (None, false, true), + (Some(OFFLINE_USERNAME), true, false), + (Some(ONLINE_USERNAME), true, false), + (Some(OFFLINE_USERNAME), false, false), + (Some(ONLINE_USERNAME), false, false), ] { let home = tempfile::tempdir()?; let marker = SetupMarker { @@ -116,7 +118,10 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R WindowsSandboxProxySettingsMode::Preserve, |_, _| { // Older services must not accept stale credentials instead of repairing expiry. - assert!(!sandbox_users_path(home.path()).exists()); + assert_eq!( + sandbox_users_path(home.path()).exists(), + expired_username.is_none() + ); setups.set(setups.get() + 1); users.offline.password = BASE64.encode(crate::dpapi::protect(b"new-test-password")?); @@ -134,18 +139,24 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R }, )) }, + |_| needs_registration && (setups.get() == 0 || !repair_succeeds), + ); + assert_eq!( + setups.get(), + usize::from(expired_username.is_some() || needs_registration) ); - assert_eq!(setups.get(), usize::from(expired_username.is_some())); assert_eq!( result .map(|(creds, _)| (creds.username, creds.password)) .map_err(|err| err.to_string()), - if !repair_succeeds { + if needs_registration && !repair_succeeds { + Err("protected Core setup did not publish ready runtime ownership".into()) + } else if !repair_succeeds { Err("Windows sandbox account password is still expired after setup".into()) } else { Ok(( OFFLINE_USERNAME.into(), - if expired_username.is_some() { + if expired_username.is_some() || needs_registration { "new-test-password" } else { "old-test-password" @@ -246,6 +257,7 @@ fn credential_setup_reconciles_policy_and_repairs_accounts() -> Result<()> { Ok(()) }, |_| Ok(account_flags.get()), + |_| false, ) }; for _ in 0..2 {