From 1b178d2d43a5e0a17e3f967b36a18030fc0bfe50 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Dec 2026 20:08:40 +0100 Subject: [PATCH] Connector port: an invocation past its expiry is renewed under a new request id, so a retry after a timeout is not refused Expired Co-Authored-By: Claude Sonnet 5.5 --- crates/ghostlight/src/controllers.rs | 72 ++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/crates/ghostlight/src/controllers.rs b/crates/ghostlight/src/controllers.rs index b6131ee..d202d08 100644 --- a/crates/ghostlight/src/controllers.rs +++ b/crates/ghostlight/src/controllers.rs @@ -603,14 +603,43 @@ impl ConnectorInferencePort { "persisted inference caller does not match the configured runtime identity", )); } - lift_result( - self.client - .execute(&request.invocation) - .map_err(client_fault)?, - ) + let invocation = renewed_invocation(request.invocation, unix_ms()?)?; + lift_result(self.client.execute(&invocation).map_err(client_fault)?) } } +/// How close to its expiry an invocation is renewed rather than sent. +const RENEWAL_MARGIN: Duration = Duration::from_secs(30); + +/// The invocation this port sends. A prepared invocation is valid for +/// `REQUEST_EXPIRY`, but a generation may take `RESPONSE_TIMEOUT` and a flow +/// resumes its persisted invocation whenever it is next ticked, so a retry +/// after a timeout would otherwise meet `Expired`, which is terminal. The +/// connector checks expiry before it looks a request up and binds a request id +/// to the exact invocation bytes, expiry included, so an expired invocation can +/// no longer reach its own replay record: renewing it is a new request under a +/// new id and a new expiry, and loses nothing. The persisted invocation is +/// never rewritten, so the owners' identity checks still hold, and an +/// invocation that is still live is sent as it is and replays by its own id. +fn renewed_invocation( + invocation: TransportInvocation, + now_unix_ms: u64, +) -> Result { + let margin = RENEWAL_MARGIN.as_millis() as u64; + if invocation.expires_at_unix_ms > now_unix_ms.saturating_add(margin) { + return Ok(invocation); + } + let mut request = invocation.request; + request.request_id = format!("{}-renewed-{now_unix_ms}", request.request_id); + TransportInvocation::new( + invocation.caller_runtime_id, + now_unix_ms.saturating_add(REQUEST_EXPIRY.as_millis() as u64), + invocation.native_request_sha256, + request, + ) + .map_err(|_| InferenceFault::new("an expired inference invocation could not be renewed")) +} + /// What a connector result becomes: events and reported usage, or the typed /// fault its refusal or failure kind names. fn lift_result(result: TransportResult) -> Result { @@ -7526,6 +7555,39 @@ mod tests { } } + /// A retry after a timeout resumes an invocation older than its expiry. + /// The connector refuses that as `Expired`; the port renews it, and sends a + /// live one untouched. + #[test] + fn a_stale_invocation_is_renewed_and_a_live_one_is_sent_as_it_is() { + let skew = REQUEST_EXPIRY.as_millis() as u64; + let prepared = persona_request(CommandId::new(), "claude-haiku-5-5", "Speak.".into()) + .and_then(|request| { + PreparedInference::prepare("runtime", 1_000_000 + skew, request).map_err(|_| unreachable!()) + }) + .unwrap(); + let live = prepared.invocation.clone(); + let sent = renewed_invocation(live.clone(), 1_000_000).unwrap(); + assert_eq!(sent, live, "a live invocation replays by its own id"); + + let now = 1_000_000 + skew + 600_000; + assert!(live.validate(now, skew).is_err(), "the persisted invocation is expired"); + let renewed = renewed_invocation(live.clone(), now).unwrap(); + renewed.validate(now, skew).expect("the renewed invocation is admissible"); + assert_ne!(renewed.request_id(), live.request_id()); + assert!(renewed.request_id().starts_with(live.request_id())); + assert_eq!(renewed.native_request_sha256, live.native_request_sha256); + assert_eq!( + renewed.request.input, live.request.input, + "a renewal changes the identity and the expiry, never the request" + ); + assert_eq!( + renewed_invocation(live.clone(), now).unwrap(), + renewed, + "renewing the same persisted invocation at one instant is one request" + ); + } + /// The whole port against a peer that accepts and hangs up: the closed /// connection is the retryable, named fault, not an integrity breach. #[tokio::test]