aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
//! Retry only the service's authenticated refusal before dispatch.
|
||||
//! A lost response may follow a mutation, so the retry's transport errors stay errors.
|
||||
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::FramedProvisioningMessage;
|
||||
use crate::SandboxProvisioningResponse;
|
||||
|
||||
pub(super) fn retry(
|
||||
request: &FramedProvisioningMessage,
|
||||
deadline: Instant,
|
||||
) -> anyhow::Result<SandboxProvisioningResponse> {
|
||||
let mut pipe = loop {
|
||||
if let Some(pipe) = super::connect(deadline)? {
|
||||
break pipe;
|
||||
}
|
||||
anyhow::ensure!(
|
||||
Instant::now() < deadline,
|
||||
"refreshed sandbox service pipe is unavailable"
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(25));
|
||||
};
|
||||
super::exchange_request(&mut pipe, request, deadline)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
//! Resume gated registration refresh only after an authenticated service restart.
|
||||
//! Only response-side pipe disconnections qualify, never failed authentication,
|
||||
//! failed request writes, explicit replies, or protocol errors.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Context;
|
||||
use windows_sys::Win32::Foundation::ERROR_BROKEN_PIPE;
|
||||
use windows_sys::Win32::Foundation::ERROR_NO_DATA;
|
||||
use windows_sys::Win32::Foundation::ERROR_PIPE_NOT_CONNECTED;
|
||||
use windows_sys::Win32::Foundation::HANDLE;
|
||||
use windows_sys::Win32::System::Services::SERVICE_START_PENDING;
|
||||
|
||||
use crate::FramedProvisioningMessage;
|
||||
use crate::SandboxProvisioningResponse;
|
||||
|
||||
// Registering the service-bearing package can restart the service once for each
|
||||
// of the two managed accounts. Keep both retries inside the original deadline.
|
||||
const MAX_SERVICE_RESTARTS: usize = 2;
|
||||
const RECONNECT_POLL_INTERVAL: Duration = Duration::from_millis(25);
|
||||
|
||||
pub(super) fn send(
|
||||
mut pipe: File,
|
||||
request: &FramedProvisioningMessage,
|
||||
deadline: Instant,
|
||||
) -> anyhow::Result<SandboxProvisioningResponse> {
|
||||
let mut restarts_remaining = MAX_SERVICE_RESTARTS;
|
||||
loop {
|
||||
remaining_time(deadline)?;
|
||||
let server_pid = super::verify_server(pipe.as_raw_handle() as HANDLE)
|
||||
.context("authenticate provisioning pipe server")?;
|
||||
crate::write_provisioning_frame(&mut pipe, request)
|
||||
.context("send sandbox provisioning request")?;
|
||||
let response = super::read_response(&mut pipe, deadline);
|
||||
if !take_restart(&response, &mut restarts_remaining, deadline) {
|
||||
return response;
|
||||
}
|
||||
drop(pipe);
|
||||
pipe = connect_after_restart(server_pid, deadline)?;
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_after_restart(previous_pid: u32, deadline: Instant) -> anyhow::Result<File> {
|
||||
loop {
|
||||
remaining_time(deadline)?;
|
||||
if let Some(pipe) = super::connect(deadline)?
|
||||
&& super::pipe_server_process_id(pipe.as_raw_handle() as HANDLE)? != previous_pid
|
||||
// The service creates its listener before reporting RUNNING.
|
||||
&& super::query_service_status()?.dwCurrentState != SERVICE_START_PENDING
|
||||
{
|
||||
// The caller authenticates this new PID against the running SCM
|
||||
// service before writing. A same-PID reuse is conservatively refused.
|
||||
return Ok(pipe);
|
||||
}
|
||||
std::thread::sleep(remaining_time(deadline)?.min(RECONNECT_POLL_INTERVAL));
|
||||
}
|
||||
}
|
||||
|
||||
fn remaining_time(deadline: Instant) -> io::Result<Duration> {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"timed out waiting for restarted sandbox provisioning service",
|
||||
));
|
||||
}
|
||||
Ok(remaining)
|
||||
}
|
||||
|
||||
fn take_restart(
|
||||
response: &anyhow::Result<SandboxProvisioningResponse>,
|
||||
restarts_remaining: &mut usize,
|
||||
deadline: Instant,
|
||||
) -> bool {
|
||||
let Err(error) = response else {
|
||||
return false;
|
||||
};
|
||||
if *restarts_remaining == 0
|
||||
|| Instant::now() >= deadline
|
||||
|| !error.downcast_ref::<io::Error>().is_some_and(|error| {
|
||||
matches!(
|
||||
error.kind(),
|
||||
io::ErrorKind::UnexpectedEof | io::ErrorKind::BrokenPipe
|
||||
) || matches!(
|
||||
error.raw_os_error(),
|
||||
Some(code)
|
||||
if code == ERROR_BROKEN_PIPE as i32
|
||||
|| code == ERROR_NO_DATA as i32
|
||||
|| code == ERROR_PIPE_NOT_CONNECTED as i32
|
||||
)
|
||||
})
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*restarts_remaining -= 1;
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "refresh_retry_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,121 @@
|
||||
//! Regression coverage for reply classification and the shared restart deadline/budget.
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn authentication_failure_cannot_write_or_replay_the_request() -> anyhow::Result<()> {
|
||||
let pipe = tempfile::tempfile()?;
|
||||
let observation = pipe.try_clone()?;
|
||||
let request = FramedProvisioningMessage {
|
||||
version: crate::PROVISIONING_PROTOCOL_VERSION,
|
||||
message: crate::ProvisioningMessage::ProvisionSandboxRequest {
|
||||
payload: crate::SandboxProvisioningRequest {
|
||||
codex_home: r"C:\test-home".to_owned(),
|
||||
registered_core: true,
|
||||
refresh_only: true,
|
||||
settings: crate::WindowsSandboxProvisioningSettings {
|
||||
proxy_ports: Vec::new(),
|
||||
allow_local_binding: false,
|
||||
},
|
||||
listeners: crate::WindowsSandboxProxyListeners::default(),
|
||||
},
|
||||
},
|
||||
};
|
||||
let error = send(pipe, &request, Instant::now() + Duration::from_secs(60)).unwrap_err();
|
||||
assert_eq!(error.to_string(), "authenticate provisioning pipe server");
|
||||
assert_eq!(observation.metadata()?.len(), 0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permits_two_response_disconnects_but_not_a_third() {
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
let response = Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
|
||||
let mut remaining = MAX_SERVICE_RESTARTS;
|
||||
let decisions = (0..3)
|
||||
.map(|_| take_restart(&response, &mut remaining, deadline))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!((decisions, remaining), (vec![true, true, false], 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_only_response_pipe_disconnect_errors() {
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
for error in [
|
||||
io::Error::from(io::ErrorKind::UnexpectedEof),
|
||||
io::Error::from(io::ErrorKind::BrokenPipe),
|
||||
io::Error::from_raw_os_error(ERROR_BROKEN_PIPE as i32),
|
||||
io::Error::from_raw_os_error(ERROR_NO_DATA as i32),
|
||||
io::Error::from_raw_os_error(ERROR_PIPE_NOT_CONNECTED as i32),
|
||||
] {
|
||||
let response = Err(anyhow::Error::from(error).context("read response"));
|
||||
let mut remaining = MAX_SERVICE_RESTARTS;
|
||||
assert_eq!(
|
||||
(take_restart(&response, &mut remaining, deadline), remaining),
|
||||
(true, 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_replies_never_consume_a_restart() {
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
for response in [
|
||||
SandboxProvisioningResponse::Ok,
|
||||
SandboxProvisioningResponse::Unavailable,
|
||||
SandboxProvisioningResponse::Error {
|
||||
message: "refused".to_owned(),
|
||||
},
|
||||
SandboxProvisioningResponse::Error {
|
||||
message: crate::SANDBOX_GROUP_CHANGED.to_owned(),
|
||||
},
|
||||
] {
|
||||
let mut remaining = MAX_SERVICE_RESTARTS;
|
||||
assert_eq!(
|
||||
(
|
||||
take_restart(&Ok(response), &mut remaining, deadline),
|
||||
remaining
|
||||
),
|
||||
(false, MAX_SERVICE_RESTARTS)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeout_permission_and_protocol_errors_do_not_consume_a_restart() {
|
||||
let deadline = Instant::now() + Duration::from_secs(60);
|
||||
for error in [
|
||||
io::Error::from(io::ErrorKind::TimedOut).into(),
|
||||
io::Error::from(io::ErrorKind::PermissionDenied).into(),
|
||||
io::Error::from(io::ErrorKind::InvalidData).into(),
|
||||
anyhow::anyhow!("unexpected sandbox provisioning response message"),
|
||||
serde_json::from_str::<FramedProvisioningMessage>("not JSON")
|
||||
.unwrap_err()
|
||||
.into(),
|
||||
] {
|
||||
let mut remaining = MAX_SERVICE_RESTARTS;
|
||||
assert_eq!(
|
||||
(
|
||||
take_restart(&Err(error), &mut remaining, deadline),
|
||||
remaining
|
||||
),
|
||||
(false, MAX_SERVICE_RESTARTS)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_after_original_deadline_is_not_retried() {
|
||||
let deadline = Instant::now();
|
||||
let response = Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
|
||||
let mut remaining = MAX_SERVICE_RESTARTS;
|
||||
assert_eq!(
|
||||
(take_restart(&response, &mut remaining, deadline), remaining),
|
||||
(false, MAX_SERVICE_RESTARTS)
|
||||
);
|
||||
assert_eq!(
|
||||
remaining_time(deadline).unwrap_err().kind(),
|
||||
io::ErrorKind::TimedOut
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user