diff --git a/crates/runtime-api/src/lib.rs b/crates/runtime-api/src/lib.rs index 1cde155f9..f24428904 100644 --- a/crates/runtime-api/src/lib.rs +++ b/crates/runtime-api/src/lib.rs @@ -1139,10 +1139,6 @@ pub struct RemainingRuntimeRoute { pub reason: &'static str, } -pub const RUNTIME_ROUTE_VERIFICATION_CHALLENGE: &str = - "/v1/workspace-runtime-verification/challenge"; -pub const RUNTIME_ROUTE_VERIFICATION_ACK: &str = - "/v1/workspace-runtime-verification/acknowledgement"; pub const RUNTIME_ROUTE_CONFIG_BUNDLES: &str = "/v1/config-bundles"; pub const RUNTIME_ROUTE_CONFIG_BUNDLE_AVAILABILITY: &str = "/v1/config-bundles/{bundle_id}/availability"; @@ -1169,16 +1165,6 @@ pub const RUNTIME_ROUTE_WORKER_SESSION_ATTACHMENT: &str = /// Intentionally out-of-contract Runtime routes. This inventory keeps manual paths visible rather /// than letting them be mistaken for management-contract omissions. pub const REMAINING_RUNTIME_ROUTES: &[RemainingRuntimeRoute] = &[ - RemainingRuntimeRoute { - method: "POST", - path: RUNTIME_ROUTE_VERIFICATION_CHALLENGE, - reason: "Workspace verification handshake", - }, - RemainingRuntimeRoute { - method: "POST", - path: RUNTIME_ROUTE_VERIFICATION_ACK, - reason: "Workspace verification handshake", - }, RemainingRuntimeRoute { method: "GET", path: RUNTIME_ROUTE_CONFIG_BUNDLES, @@ -1625,7 +1611,7 @@ mod tests { #[test] fn remaining_route_inventory_does_not_overlap_contract() { - assert_eq!(REMAINING_RUNTIME_ROUTES.len(), 21); + assert_eq!(REMAINING_RUNTIME_ROUTES.len(), 19); for remaining in REMAINING_RUNTIME_ROUTES { assert!(!RuntimeApiMetadata::OPERATIONS.iter().any(|operation| { format!("{:?}", operation.method).eq_ignore_ascii_case(remaining.method) diff --git a/crates/server-api/src/lib.rs b/crates/server-api/src/lib.rs index 6c33ad521..e0c22770b 100644 --- a/crates/server-api/src/lib.rs +++ b/crates/server-api/src/lib.rs @@ -8842,62 +8842,14 @@ pub struct RuntimeSummary { pub diagnostics: Vec, } -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] -#[cfg_attr(feature = "typescript", derive(ts_rs::TS))] -#[serde(rename_all = "snake_case")] -pub enum WorkspaceRuntimeBindingState { - Configured, - Verified, - Revoked, -} - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] -#[cfg_attr(feature = "typescript", derive(ts_rs::TS))] -#[serde(rename_all = "snake_case")] -pub enum RuntimeConnectionDisplayState { - Configured, - Verified, - Unavailable, - Revoked, -} - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] -#[cfg_attr(feature = "typescript", derive(ts_rs::TS))] -#[serde(rename_all = "snake_case")] -pub enum RuntimeVerificationOutcome { - Verified, - ChallengeIssued, - VerificationFailed, - ConnectivityFailed, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] -#[cfg_attr(feature = "typescript", derive(ts_rs::TS))] -#[serde(deny_unknown_fields)] -pub struct RuntimeVerificationEvidenceSummary { - pub verified_at: Option, - pub last_checked_at: String, - pub last_outcome: RuntimeVerificationOutcome, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, -} - +/// Binding identity and explicit revocation only. Each signed request authenticates +/// using the current Workspace signing identity, not a stored approval. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[cfg_attr(feature = "typescript", derive(ts_rs::TS))] #[serde(deny_unknown_fields)] pub struct WorkspaceRuntimeBindingSummary { - pub state: WorkspaceRuntimeBindingState, - pub connection_state: RuntimeConnectionDisplayState, pub binding_id: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub workspace_key_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub workspace_trust_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub verification: Option, + pub revoked_at: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] @@ -9081,9 +9033,6 @@ pub struct RuntimePublicIdentityBundle { #[serde(deny_unknown_fields)] pub struct CreateRemoteRuntimeRequest { pub public_bundle: RuntimePublicIdentityBundle, - /// Exact Runtime-issued enrollment identity from `trust-workspace add/show`. - /// A key fingerprint alone cannot distinguish revocation and re-enrollment. - pub workspace_trust_id: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub display_name: Option, pub endpoint: String, @@ -9126,12 +9075,11 @@ pub enum RuntimeConnectionTestFailureKind { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[cfg_attr(feature = "typescript", derive(ts_rs::TS))] #[serde(deny_unknown_fields)] +/// Observation of an actual signed ping, not a grant or cached approval. pub struct RuntimeConnectionTestResponse { pub workspace_id: String, pub runtime_id: String, pub binding_id: String, - pub connection_state: RuntimeConnectionDisplayState, - pub verification: Option, pub checked_at: String, pub status: RuntimeConnectionTestStatus, pub failure_kind: Option, @@ -12626,18 +12574,22 @@ mod tests { } #[test] - fn runtime_registration_requires_explicit_runtime_issued_trust_identity() { - let mut value = serde_json::json!({ + fn runtime_registration_uses_runtime_bundle_without_workspace_identity_copies() { + let value = serde_json::json!({ "public_bundle": {"identity_id": "runtime", "public_key": "public-key"}, - "endpoint": "https://runtime.example.test", - "workspace_trust_id": "runtime-issued-enrollment" + "endpoint": "https://runtime.example.test" }); let request: CreateRemoteRuntimeRequest = serde_json::from_value(value.clone()).unwrap(); - assert_eq!(request.workspace_trust_id, "runtime-issued-enrollment"); - value.as_object_mut().unwrap().remove("workspace_trust_id"); - assert!(serde_json::from_value::(value.clone()).is_err()); - value["workspace_key_generation"] = serde_json::json!(1); - assert!(serde_json::from_value::(value).is_err()); + assert_eq!(serde_json::to_value(request).unwrap(), value); + for field in [ + "workspace_trust_id", + "workspace_key_id", + "workspace_key_generation", + ] { + let mut obsolete = value.clone(); + obsolete[field] = serde_json::json!("obsolete"); + assert!(serde_json::from_value::(obsolete).is_err()); + } } #[test] @@ -12646,8 +12598,6 @@ mod tests { "workspace_id": "workspace-test", "runtime_id": "runtime-test", "binding_id": "binding-test", - "connection_state": "verified", - "verification": null, "checked_at": "2026-09-01T12:00:00Z", "status": "compatible", "failure_kind": null, @@ -12665,21 +12615,22 @@ mod tests { } #[test] - fn runtime_trust_contract_binds_concrete_keys_and_binding_lifetimes() { - let evidence = RuntimeVerificationEvidenceSummary { - verified_at: Some("2026-10-06T00:00:00Z".into()), - last_checked_at: "2026-10-06T00:00:00Z".into(), - last_outcome: RuntimeVerificationOutcome::Verified, - binding_id: "binding-new".into(), - workspace_key_id: "workspace-key".into(), - workspace_public_key_fingerprint: "sha256:workspace-key".into(), - workspace_trust_id: "trust-new".into(), - runtime_public_key_fingerprint: "sha256:runtime-key".into(), - }; - round_trip(evidence.clone()); - let mut stale = serde_json::to_value(evidence).unwrap(); - stale["unexpected_state_guard"] = serde_json::json!(1); - assert!(serde_json::from_value::(stale).is_err()); + fn runtime_binding_rejects_cached_authentication_and_requires_observed_identity_for_mutation() { + let binding = serde_json::json!({"binding_id": "binding-new", "revoked_at": null}); + let parsed: WorkspaceRuntimeBindingSummary = + serde_json::from_value(binding.clone()).unwrap(); + assert_eq!(serde_json::to_value(parsed).unwrap(), binding); + for field in [ + "state", + "connection_state", + "workspace_key_id", + "workspace_trust_id", + "verification", + ] { + let mut obsolete = binding.clone(); + obsolete[field] = serde_json::json!("obsolete"); + assert!(serde_json::from_value::(obsolete).is_err()); + } round_trip(RevokeRuntimeTrustKeyRequest { expected_binding_id: "binding-observed".into(), }); diff --git a/crates/session-store/src/history.rs b/crates/session-store/src/history.rs index 4db39dd3d..6f03e1466 100644 --- a/crates/session-store/src/history.rs +++ b/crates/session-store/src/history.rs @@ -67,6 +67,8 @@ pub enum LoggedSessionHistoryOrigin { ToolOutput { worker: LoggedWorkerSubject, }, + /// Host-produced opening context summary; not auto-read or TaskStore material. + CompactionSummary, DerivedSummary, LegacyUnknown, } diff --git a/crates/session-store/src/public_snapshot.rs b/crates/session-store/src/public_snapshot.rs index 838f394a1..f3ae70d05 100644 --- a/crates/session-store/src/public_snapshot.rs +++ b/crates/session-store/src/public_snapshot.rs @@ -1877,7 +1877,8 @@ fn provenance(origin: &LoggedSessionHistoryOrigin) -> SessionEntryProvenance { } LoggedSessionHistoryOrigin::ModelOutput { .. } => SessionEntryProvenance::ModelOutput, LoggedSessionHistoryOrigin::ToolOutput { .. } => SessionEntryProvenance::ToolOutput, - LoggedSessionHistoryOrigin::DerivedSummary => SessionEntryProvenance::DerivedSummary, + LoggedSessionHistoryOrigin::CompactionSummary + | LoggedSessionHistoryOrigin::DerivedSummary => SessionEntryProvenance::DerivedSummary, LoggedSessionHistoryOrigin::LegacyUnknown => SessionEntryProvenance::LegacyUnknown, } } @@ -2548,6 +2549,12 @@ mod tests { user_message(10), assistant_message(10, "final"), synthetic_compaction_input, + history_message( + "host-summary", + LoggedRole::System, + "[Compacted context summary]\n\nprivate inherited context", + LoggedSessionHistoryOrigin::CompactionSummary, + ), ], None, Some(SegmentOrigin { diff --git a/crates/worker-runtime/README.md b/crates/worker-runtime/README.md index a48822291..e14d955fc 100644 --- a/crates/worker-runtime/README.md +++ b/crates/worker-runtime/README.md @@ -27,4 +27,19 @@ cargo run -p worker-runtime \ ``` The REST server is intended for a trusted Backend/proxy, not direct browser access. +Workspace requests authenticate the actual HTTP request with the Workspace's current +signing identity. The Runtime verifies signatures against its explicitly enrolled +Workspace public key and binds the Runtime audience, Workspace/Worker scope, +operation, method, path/query, body digest, expiry, and single-use token ID. A +connection test is an ordinary signed `/v1/ping`; no prior handshake or saved +verification evidence is required or consulted. + +Keep `trust-workspace add`, `replace`, and `revoke` as explicit operator actions. +A self-reported public key cannot grant trust. Trust record IDs are administrative +only and must not be copied into Backend registration or request claims. Replay +protection uses Workspace plus the trusted key fingerprint, so re-enrolling the +same key or rotating away and back cannot reset consumed requests. Reaccepting a +key does permit fresh, unconsumed requests signed by that key until their short +expiry; it does not create a separate sender-controlled enrollment namespace. + For authenticated remote Runtime setup, see [`../../docs/development/server-runtime-auth.md`](../../docs/development/server-runtime-auth.md). diff --git a/crates/worker-runtime/src/http_server.rs b/crates/worker-runtime/src/http_server.rs index 381afeb0c..e4eb0a2c6 100644 --- a/crates/worker-runtime/src/http_server.rs +++ b/crates/worker-runtime/src/http_server.rs @@ -31,11 +31,7 @@ use crate::ssh_host_key_probe::{ SshHostKeyProbeRequest, SshHostKeyProbeResponse, probe_ssh_host_keys_with_program, }; use crate::workspace_issuer::{ - RuntimeVerificationSigner, VerifiedWorkspaceCapability, WORKSPACE_VERIFICATION_OPERATION, WorkspaceCapabilityExpectation, WorkspaceCapabilityVerifier, - WorkspaceRuntimeVerificationAcknowledgement, WorkspaceRuntimeVerificationAuthority, - WorkspaceRuntimeVerificationChallenge, WorkspaceRuntimeVerificationReceipt, - WorkspaceRuntimeVerificationRecord, WorkspaceRuntimeVerificationResponse, inspect_workspace_capability_claims, workspace_request_body_digest, }; use crate::{Runtime, RuntimeWorkspaceScope}; @@ -256,14 +252,6 @@ fn runtime_http_router_with_auth_and_ssh_keyscan_program( }; let router = Router::new() - .route( - remaining_route(runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE, &["POST"]), - post(post_workspace_verification_challenge), - ) - .route( - remaining_route(runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK, &["POST"]), - post(post_workspace_verification_acknowledgement), - ) .route( remaining_route(runtime_api::RUNTIME_ROUTE_CONFIG_BUNDLES, &["GET", "POST"]), get(list_config_bundles).post(store_config_bundle), @@ -377,8 +365,7 @@ struct RuntimeHttpState { #[derive(Clone, Debug)] pub struct WorkspaceRuntimeHttpAuth { pub verifier: WorkspaceCapabilityVerifier, - pub signer: RuntimeVerificationSigner, - pub verifications: Arc, + pub runtime_id: String, } struct RuntimeHttpWorkdirSession { @@ -563,172 +550,6 @@ fn unix_now_i64() -> i64 { i64::try_from(unix_now_seconds()).unwrap_or(i64::MAX) } -async fn post_workspace_verification_challenge( - State(state): State, - Extension(verified): Extension, - Json(challenge): Json, -) -> RestResult { - let auth = state.workspace_auth.as_deref().ok_or_else(|| { - RuntimeHttpRestError::new( - StatusCode::NOT_IMPLEMENTED, - "workspace_runtime_verification_unavailable", - "Workspace Runtime verification is not configured", - ) - })?; - if challenge.workspace_id != verified.workspace_id - || challenge.runtime_id != verified.runtime_id - || challenge.binding_id != verified.binding_id - || challenge.workspace_key_id != verified.issuer_key_id - || challenge.workspace_public_key_fingerprint != verified.issuer_public_key_fingerprint - || challenge.workspace_trust_id != verified.trust_id - || challenge.runtime_id != auth.signer.runtime_id() - || challenge.runtime_public_key_fingerprint != auth.signer.public_key_fingerprint() - { - return Err(RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - "Workspace Runtime verification challenge does not match authenticated authority", - )); - } - let response = auth - .signer - .sign_response(&challenge, uuid::Uuid::now_v7().to_string(), unix_now_i64()) - .map_err(|error| { - RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - error.to_string(), - ) - })?; - auth.verifications.begin(challenge).map_err(|error| { - RuntimeHttpRestError::new( - StatusCode::SERVICE_UNAVAILABLE, - "workspace_runtime_verification_unavailable", - error.to_string(), - ) - })?; - Ok(Json(response)) -} - -async fn post_workspace_verification_acknowledgement( - State(state): State, - Extension(verified): Extension, - Json(acknowledgement): Json, -) -> RestResult { - let auth = state.workspace_auth.as_deref().ok_or_else(|| { - RuntimeHttpRestError::new( - StatusCode::NOT_IMPLEMENTED, - "workspace_runtime_verification_unavailable", - "Workspace Runtime verification is not configured", - ) - })?; - if acknowledgement.workspace_id != verified.workspace_id - || acknowledgement.runtime_id != verified.runtime_id - || acknowledgement.binding_id != verified.binding_id - || acknowledgement.workspace_key_id != verified.issuer_key_id - || acknowledgement.workspace_public_key_fingerprint - != verified.issuer_public_key_fingerprint - || acknowledgement.workspace_trust_id != verified.trust_id - || acknowledgement.runtime_id != auth.signer.runtime_id() - || acknowledgement.runtime_public_key_fingerprint != auth.signer.public_key_fingerprint() - || acknowledgement.expires_at <= unix_now_i64() - || acknowledgement.binding_id.is_empty() - || acknowledgement.workspace_public_key_fingerprint.is_empty() - || acknowledgement.workspace_trust_id.is_empty() - || acknowledgement.workspace_nonce.is_empty() - || acknowledgement.runtime_nonce.is_empty() - || acknowledgement.response_digest.len() != workspace_request_body_digest(&[]).len() - { - return Err(RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - "Workspace Runtime verification acknowledgement is invalid", - )); - } - let challenge = WorkspaceRuntimeVerificationChallenge { - challenge_id: acknowledgement.challenge_id.clone(), - workspace_id: acknowledgement.workspace_id.clone(), - runtime_id: acknowledgement.runtime_id.clone(), - binding_id: acknowledgement.binding_id.clone(), - workspace_key_id: acknowledgement.workspace_key_id.clone(), - workspace_public_key_fingerprint: acknowledgement.workspace_public_key_fingerprint.clone(), - workspace_trust_id: acknowledgement.workspace_trust_id.clone(), - runtime_public_key_fingerprint: acknowledgement.runtime_public_key_fingerprint.clone(), - workspace_nonce: acknowledgement.workspace_nonce.clone(), - expires_at: acknowledgement.expires_at, - }; - let response = acknowledgement.response.clone(); - if response.runtime_nonce != acknowledgement.runtime_nonce - || response.workspace_nonce != acknowledgement.workspace_nonce - || response.response_proof.is_empty() - { - return Err(RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - "Workspace Runtime verification acknowledgement does not match its response", - )); - } - let response_bytes = serde_json::to_vec(&response).map_err(|_| { - RuntimeHttpRestError::new( - StatusCode::BAD_REQUEST, - "workspace_runtime_verification_rejected", - "Workspace Runtime verification response could not be canonicalized", - ) - })?; - if workspace_request_body_digest(&response_bytes) != acknowledgement.response_digest { - return Err(RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - "Workspace Runtime verification acknowledgement has the wrong response digest", - )); - } - crate::workspace_issuer::verify_runtime_verification_response( - &response, - &challenge, - auth.signer.public_key(), - unix_now_i64(), - ) - .map_err(|error| { - RuntimeHttpRestError::new( - StatusCode::UNAUTHORIZED, - "workspace_runtime_verification_rejected", - error.to_string(), - ) - })?; - auth.verifications - .acknowledge( - &acknowledgement.challenge_id, - WorkspaceRuntimeVerificationRecord { - workspace_id: acknowledgement.workspace_id.clone(), - runtime_id: acknowledgement.runtime_id.clone(), - binding_id: acknowledgement.binding_id.clone(), - workspace_key_id: acknowledgement.workspace_key_id.clone(), - workspace_public_key_fingerprint: acknowledgement - .workspace_public_key_fingerprint - .clone(), - workspace_trust_id: acknowledgement.workspace_trust_id.clone(), - runtime_public_key_fingerprint: acknowledgement - .runtime_public_key_fingerprint - .clone(), - verified_at: unix_now_i64(), - }, - ) - .map_err(|error| { - RuntimeHttpRestError::new( - StatusCode::SERVICE_UNAVAILABLE, - "workspace_runtime_verification_unavailable", - error.to_string(), - ) - })?; - Ok(Json(WorkspaceRuntimeVerificationReceipt { - challenge_id: acknowledgement.challenge_id, - workspace_id: acknowledgement.workspace_id, - runtime_id: acknowledgement.runtime_id, - binding_id: acknowledgement.binding_id.clone(), - accepted_at: unix_now_i64(), - })) -} - #[allow(dead_code)] async fn get_runtime_ping( State(state): State, @@ -757,7 +578,7 @@ async fn get_runtime_ping( let runtime_id = state .workspace_auth .as_ref() - .map(|auth| auth.signer.runtime_id().trim()) + .map(|auth| auth.runtime_id.trim()) .filter(|runtime_id| !runtime_id.is_empty()) .ok_or_else(|| { RuntimeHttpRestError::new( @@ -2123,8 +1944,7 @@ async fn require_runtime_auth( let body_digest = workspace_request_body_digest(&body); let expected = WorkspaceCapabilityExpectation { workspace_id: &claims.issuer_workspace_id, - binding_id: &claims.binding_id, - runtime_id: workspace_auth.signer.runtime_id(), + runtime_id: &workspace_auth.runtime_id, worker_id: expected_worker_id.as_deref(), operation: required_permission, method: &method, @@ -2134,48 +1954,6 @@ async fn require_runtime_auth( }; match workspace_auth.verifier.verify(token, &expected) { Ok(verified) => { - let is_verification = path_and_query - == runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE - || path_and_query == runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK; - if !is_verification { - let record = match workspace_auth - .verifications - .get(&verified.workspace_id, workspace_auth.signer.runtime_id()) - { - Ok(Some(record)) => record, - Ok(None) => { - return RuntimeHttpRestError::new( - StatusCode::FORBIDDEN, - "workspace_runtime_verification_required", - "Workspace Runtime binding has not completed signed verification", - ) - .into_response(); - } - Err(error) => { - return RuntimeHttpRestError::new( - StatusCode::SERVICE_UNAVAILABLE, - "workspace_runtime_verification_unavailable", - error.to_string(), - ) - .into_response(); - } - }; - if record.binding_id != verified.binding_id - || record.workspace_key_id != verified.issuer_key_id - || record.workspace_public_key_fingerprint - != verified.issuer_public_key_fingerprint - || record.workspace_trust_id != verified.trust_id - || record.runtime_public_key_fingerprint - != workspace_auth.signer.public_key_fingerprint() - { - return RuntimeHttpRestError::new( - StatusCode::FORBIDDEN, - "workspace_runtime_verification_stale", - "Workspace Runtime verification does not match current request authority", - ) - .into_response(); - } - } request = Request::from_parts(parts, Body::from(body)); request.extensions_mut().insert(verified.clone()); request.extensions_mut().insert(RuntimeAuthContext { @@ -2199,13 +1977,7 @@ async fn require_runtime_auth( } } - let workspace_bootstrap_request = request.method() == Method::POST - && matches!( - request.uri().path(), - runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE - | runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK - ); - if state.workspace_auth.is_some() && !workspace_bootstrap_request { + if state.workspace_auth.is_some() { let local_token_matches = state .local_token .as_deref() @@ -2275,12 +2047,6 @@ fn auth_workspace_scope( } fn workspace_runtime_operation(method: &Method, path: &str) -> &'static str { - if (path == runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE - || path == runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK) - && *method == Method::POST - { - return WORKSPACE_VERIFICATION_OPERATION; - } required_runtime_permission(method, path).unwrap_or("runtime:read") } @@ -2661,12 +2427,10 @@ mod tests { WorkerExecutionResult, WorkerExecutionSpawnRequest, WorkerExecutionSpawnResult, }; use crate::management::RuntimeOptions; - use crate::retention::{DiagnosticsDisposition, SessionDisposition}; use crate::workspace_issuer::{ - InMemoryWorkspaceClaimReplayProtection, InMemoryWorkspaceRuntimeVerificationAuthority, - WorkspaceCapabilityClaims, WorkspaceCapabilityVerifier, WorkspaceIssuerTrustRecord, - WorkspaceIssuerTrustState, issue_workspace_capability_token, - verify_runtime_verification_response, + InMemoryWorkspaceClaimReplayProtection, WorkspaceCapabilityClaims, + WorkspaceCapabilityVerifier, WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, + issue_workspace_capability_token, }; use axum::body::to_bytes; use axum::http::Method; @@ -2741,8 +2505,6 @@ mod tests { .map(|route| route.path) .collect::>(); for path in [ - runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE, - runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK, runtime_api::RUNTIME_ROUTE_CONFIG_BUNDLES, runtime_api::RUNTIME_ROUTE_CONFIG_BUNDLE_AVAILABILITY, runtime_api::RUNTIME_ROUTE_WORKSPACE_PROMPT_PROJECTIONS, @@ -2766,402 +2528,286 @@ mod tests { } } - #[tokio::test] - async fn workspace_signed_verification_requires_exact_request_and_acknowledges_response() { - let runtime = - Runtime::with_execution_backend(RuntimeOptions::default(), Arc::new(AcceptingBackend)) - .unwrap(); - runtime - .store_config_bundle(test_bundle(ProfileSelector::Builtin( - "builtin:coder".to_string(), - ))) - .unwrap(); - let workspace_identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); - let runtime_identity = RuntimeIdentityMaterial::generate("runtime-test").unwrap(); - let workspace_public_key = - crate::auth::decode_public_key(&workspace_identity.public_key).unwrap(); - let workspace_fingerprint = format!( - "sha256:{}", - crate::workspace_issuer::hex_lower(&sha2::Sha256::digest(workspace_public_key)) - ); - let runtime_public_key = - crate::auth::decode_public_key(&runtime_identity.public_key).unwrap(); - let runtime_fingerprint = format!( - "sha256:{}", - crate::workspace_issuer::hex_lower(&sha2::Sha256::digest(runtime_public_key)) - ); - let verifier = WorkspaceCapabilityVerifier::new( - vec![WorkspaceIssuerTrustRecord { - workspace_id: "workspace-a".to_string(), - backend_url: "https://backend.test".to_string(), - key_id: "workspace-key".to_string(), - algorithm: "ed25519".to_string(), - public_key: workspace_identity.public_key.clone(), - public_key_fingerprint: workspace_fingerprint.clone(), - trust_id: "trust-test".into(), - state: WorkspaceIssuerTrustState::Active, - registered_at_unix: 1, - updated_at_unix: 1, - }], - Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), - ) - .unwrap(); - let app = runtime_http_router_with_workspace_auth( - runtime.clone(), - None, - WorkspaceRuntimeHttpAuth { - verifier, - signer: RuntimeVerificationSigner::from_identity(&runtime_identity).unwrap(), - verifications: Arc::new(InMemoryWorkspaceRuntimeVerificationAuthority::default()), - }, - ); - let challenge = WorkspaceRuntimeVerificationChallenge { - challenge_id: "challenge-1".to_string(), - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-test".to_string(), - binding_id: "binding-test".into(), - workspace_key_id: "workspace-key".to_string(), - workspace_public_key_fingerprint: workspace_fingerprint.clone(), - workspace_trust_id: "trust-test".into(), - runtime_public_key_fingerprint: runtime_fingerprint, - workspace_nonce: "workspace-nonce".to_string(), - expires_at: unix_now_i64() + 60, - }; - let body = serde_json::to_vec(&challenge).unwrap(); - let claims = WorkspaceCapabilityClaims { - issuer: "https://backend.test".to_string(), - issuer_workspace_id: "workspace-a".to_string(), - issuer_key_id: "workspace-key".to_string(), - issuer_public_key_fingerprint: workspace_fingerprint.clone(), - trust_id: "trust-test".into(), - binding_id: "binding-test".into(), - runtime_id: "runtime-test".to_string(), - worker_id: None, - operation: WORKSPACE_VERIFICATION_OPERATION.to_string(), - method: "POST".to_string(), - path_and_query: runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE.to_string(), - body_digest: workspace_request_body_digest(&body), - iat: unix_now_i64(), - exp: challenge.expires_at, - jti: "challenge-token".to_string(), - }; - let token = - issue_workspace_capability_token(&workspace_identity.signing_key().unwrap(), &claims) - .unwrap(); - let response = app - .clone() - .oneshot( - Request::builder() - .method(Method::POST) - .uri(runtime_api::RUNTIME_ROUTE_VERIFICATION_CHALLENGE) - .header(header::AUTHORIZATION, format!("Bearer {token}")) - .header(header::CONTENT_TYPE, "application/json") - .body(Body::from(body)) - .unwrap(), - ) - .await - .unwrap(); - let status = response.status(); - let response_body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); - assert_eq!( - status, - StatusCode::OK, - "{}", - String::from_utf8_lossy(&response_body) - ); - let verification_response = - serde_json::from_slice::(&response_body).unwrap(); - verify_runtime_verification_response( - &verification_response, - &challenge, - &runtime_identity.public_key, - unix_now_i64(), - ) - .unwrap(); - - let acknowledgement = WorkspaceRuntimeVerificationAcknowledgement { - challenge_id: verification_response.challenge_id.clone(), - workspace_id: verification_response.workspace_id.clone(), - runtime_id: verification_response.runtime_id.clone(), - binding_id: verification_response.binding_id.clone(), - workspace_key_id: verification_response.workspace_key_id.clone(), - workspace_public_key_fingerprint: verification_response - .workspace_public_key_fingerprint - .clone(), - workspace_trust_id: verification_response.workspace_trust_id.clone(), - runtime_public_key_fingerprint: verification_response - .runtime_public_key_fingerprint - .clone(), - workspace_nonce: verification_response.workspace_nonce.clone(), - runtime_nonce: verification_response.runtime_nonce.clone(), - response_digest: workspace_request_body_digest(&response_body), - response: verification_response.clone(), - expires_at: verification_response.expires_at, - }; - let acknowledgement_body = serde_json::to_vec(&acknowledgement).unwrap(); - let acknowledgement_claims = WorkspaceCapabilityClaims { - path_and_query: runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK.to_string(), - body_digest: workspace_request_body_digest(&acknowledgement_body), - jti: "ack-token".to_string(), - ..claims - }; - let acknowledgement_token = issue_workspace_capability_token( - &workspace_identity.signing_key().unwrap(), - &acknowledgement_claims, - ) - .unwrap(); - let response = app - .clone() - .oneshot( - Request::builder() - .method(Method::POST) - .uri(runtime_api::RUNTIME_ROUTE_VERIFICATION_ACK) - .header( - header::AUTHORIZATION, - format!("Bearer {acknowledgement_token}"), - ) - .header(header::CONTENT_TYPE, "application/json") - .body(Body::from(acknowledgement_body)) - .unwrap(), - ) - .await - .unwrap(); - let status = response.status(); - let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); - assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&body)); - - for authorization in [None, Some("Bearer malformed")] { - let mut request = Request::builder() - .method(Method::POST) - .uri(runtime_api::RUNTIME_ROUTE_CONFIG_BUNDLES); - if let Some(authorization) = authorization { - request = request.header(header::AUTHORIZATION, authorization); - } - let response = app - .clone() - .oneshot(request.body(Body::empty()).unwrap()) - .await - .unwrap(); - assert_eq!(response.status(), StatusCode::UNAUTHORIZED); - } - - let ping_claims = WorkspaceCapabilityClaims { - operation: RUNTIME_PING_PERMISSION.to_string(), - method: "GET".to_string(), - path_and_query: "/v1/ping".to_string(), - body_digest: workspace_request_body_digest(&[]), - jti: "ping-token".to_string(), - ..acknowledgement_claims - }; - let ping_token = issue_workspace_capability_token( - &workspace_identity.signing_key().unwrap(), - &ping_claims, - ) - .unwrap(); - let response = app - .clone() - .oneshot( - Request::builder() - .method(Method::GET) - .uri("/v1/ping") - .header(header::AUTHORIZATION, format!("Bearer {ping_token}")) - .header(RUNTIME_WORKSPACE_SCOPE_HEADER, "workspace-a") - .body(Body::empty()) - .unwrap(), - ) - .await - .unwrap(); - assert_eq!(response.status(), StatusCode::OK); - - let mut retention_create = task_request("retention-scope"); - retention_create.workspace_api = Some(WorkspaceApiRef { - workspace_id: "workspace-a".to_string(), - base_url: "https://backend.test".to_string(), - }); - let retention_worker_id = retention_create.worker_id.clone(); - runtime.create_worker(retention_create).unwrap(); - let retention_path = format!("/v1/workers/{retention_worker_id}/retention/execute"); - let retention_request = WorkerRetentionExecutionRequest { - operation_id: "retention-op".to_string(), - input_fingerprint: "fingerprint".to_string(), - archive_id: None, - workspace_id: "workspace-b".to_string(), - source_runtime_id: "runtime-a".to_string(), - worker_id: retention_worker_id, - source_created_at: "2025-01-01T00:00:00Z".to_string(), - removed_at: "2025-01-02T00:00:00Z".to_string(), - effective_profile: None, - retention_class: None, - policy_id: "policy".to_string(), - session_disposition: SessionDisposition::Purge, - diagnostics_disposition: DiagnosticsDisposition::Retain, - }; - let retention_body = serde_json::to_vec(&retention_request).unwrap(); - let retention_claims = WorkspaceCapabilityClaims { - operation: required_runtime_permission(&Method::POST, &retention_path) - .unwrap() - .to_string(), - method: "POST".to_string(), - path_and_query: retention_path.clone(), - worker_id: Some(retention_worker_id.to_string()), - body_digest: workspace_request_body_digest(&retention_body), - jti: "retention-scope-token".to_string(), - ..ping_claims - }; - let retention_token = issue_workspace_capability_token( - &workspace_identity.signing_key().unwrap(), - &retention_claims, - ) - .unwrap(); - let response = app - .oneshot( - Request::builder() - .method(Method::POST) - .uri(retention_path) - .header(header::AUTHORIZATION, format!("Bearer {retention_token}")) - .header(header::CONTENT_TYPE, "application/json") - .body(Body::from(retention_body)) - .unwrap(), - ) - .await - .unwrap(); - assert_eq!(response.status(), StatusCode::NOT_FOUND); - let error: RuntimeHttpErrorResponse = read_json(response).await; - assert_eq!(error.error.code, "worker_not_found"); - } - - #[tokio::test] - async fn reenrolled_binding_trust_or_keys_require_fresh_signed_verification() { - let workspace_identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); - let runtime_identity = RuntimeIdentityMaterial::generate("runtime-test").unwrap(); - let fingerprint = |key: &str| { - format!( - "sha256:{}", - crate::workspace_issuer::hex_lower(&sha2::Sha256::digest( - crate::auth::decode_public_key(key).unwrap() - )) - ) - }; - let old_trust = WorkspaceIssuerTrustRecord::from_bundle( + fn workspace_trust(identity: &RuntimeIdentityMaterial) -> WorkspaceIssuerTrustRecord { + WorkspaceIssuerTrustRecord::from_bundle( server_api::WorkspacePublicIdentityBundle { workspace_id: "workspace-a".into(), backend_url: "https://backend.test".into(), - key_id: "workspace-key".into(), + key_id: identity.identity_id.clone(), algorithm: "ed25519".into(), - public_key: workspace_identity.public_key.clone(), - public_key_fingerprint: fingerprint(&workspace_identity.public_key), + public_key: identity.public_key.clone(), + public_key_fingerprint: format!( + "sha256:{}", + crate::workspace_issuer::hex_lower(&sha2::Sha256::digest( + crate::auth::decode_public_key(&identity.public_key).unwrap() + )) + ), }, - "old-trust".into(), + "administrative-trust-id".into(), 1, ) - .unwrap(); - let old_verification = WorkspaceRuntimeVerificationRecord { - workspace_id: old_trust.workspace_id.clone(), + .unwrap() + } + + fn signed_claims( + trust: &WorkspaceIssuerTrustRecord, + method: &str, + path: &str, + body: &[u8], + ) -> WorkspaceCapabilityClaims { + WorkspaceCapabilityClaims { + issuer: trust.backend_url.clone(), + issuer_workspace_id: trust.workspace_id.clone(), + issuer_key_id: trust.key_id.clone(), + issuer_public_key_fingerprint: trust.public_key_fingerprint.clone(), runtime_id: "runtime-test".into(), - binding_id: "old-binding".into(), - workspace_key_id: old_trust.key_id.clone(), - workspace_public_key_fingerprint: old_trust.public_key_fingerprint.clone(), - workspace_trust_id: old_trust.trust_id.clone(), - runtime_public_key_fingerprint: fingerprint(&runtime_identity.public_key), - verified_at: 1, - }; - for changed in ["binding", "trust", "workspace_key", "runtime_key"] { - let mut current = old_verification.clone(); - let mut trust = old_trust.clone(); - let mut workspace_key = workspace_identity.clone(); - let mut runtime_key = runtime_identity.clone(); - match changed { - "binding" => current.binding_id = "reenrolled-binding".into(), - "trust" => { - trust.trust_id = "reaccepted-trust".into(); - current.workspace_trust_id = trust.trust_id.clone(); - } - "workspace_key" => { - workspace_key = RuntimeIdentityMaterial::generate("new-workspace-key").unwrap(); - trust.key_id = workspace_key.identity_id.clone(); - trust.public_key = workspace_key.public_key.clone(); - trust.public_key_fingerprint = fingerprint(&workspace_key.public_key); - current.workspace_key_id = trust.key_id.clone(); - current.workspace_public_key_fingerprint = trust.public_key_fingerprint.clone(); - } - "runtime_key" => { - runtime_key = RuntimeIdentityMaterial::generate("runtime-test").unwrap(); - current.runtime_public_key_fingerprint = fingerprint(&runtime_key.public_key); - } - _ => unreachable!(), + worker_id: worker_id_from_runtime_path(path), + operation: workspace_runtime_operation( + &Method::from_bytes(method.as_bytes()).unwrap(), + path, + ) + .into(), + method: method.into(), + path_and_query: path.into(), + body_digest: workspace_request_body_digest(body), + iat: unix_now_i64(), + exp: unix_now_i64() + 60, + jti: uuid::Uuid::now_v7().to_string(), + } + } + + fn workspace_test_auth(records: Vec) -> WorkspaceRuntimeHttpAuth { + WorkspaceRuntimeHttpAuth { + runtime_id: "runtime-test".into(), + verifier: WorkspaceCapabilityVerifier::new( + records, + Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), + ) + .unwrap(), + } + } + + async fn signed_request( + app: Router, + identity: &RuntimeIdentityMaterial, + claims: &WorkspaceCapabilityClaims, + body: Vec, + workspace: &str, + ) -> Response { + let token = + issue_workspace_capability_token(&identity.signing_key().unwrap(), claims).unwrap(); + app.oneshot( + Request::builder() + .method(claims.method.as_str()) + .uri(&claims.path_and_query) + .header(header::AUTHORIZATION, format!("Bearer {token}")) + .header(header::CONTENT_TYPE, "application/json") + .header(RUNTIME_WORKSPACE_SCOPE_HEADER, workspace) + .body(Body::from(body)) + .unwrap(), + ) + .await + .unwrap() + } + + #[tokio::test] + async fn ordinary_signed_ping_succeeds_without_handshake_and_rejects_replay() { + let identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); + let trust = workspace_trust(&identity); + let app = runtime_http_router_with_workspace_auth( + Runtime::new_memory(), + None, + workspace_test_auth(vec![trust.clone()]), + ); + let claims = signed_claims(&trust, "GET", "/v1/ping", &[]); + let response = signed_request(app.clone(), &identity, &claims, vec![], "workspace-a").await; + assert_eq!(response.status(), StatusCode::OK); + let ping: runtime_api::RuntimePingResponse = read_json(response).await; + assert_eq!(ping.runtime_id, "runtime-test"); + assert_eq!(ping.protocol_version, RUNTIME_HTTP_PROTOCOL_VERSION); + let response = signed_request(app, &identity, &claims, vec![], "workspace-a").await; + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); + let error: RuntimeHttpErrorResponse = read_json(response).await; + assert_eq!(error.error.code, "unauthorized"); + assert!(error.error.message.contains("already consumed")); + } + + #[tokio::test] + async fn signed_ping_rejects_untrusted_revoked_wrong_key_signature_and_workspace_scope() { + let identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); + let other_key = RuntimeIdentityMaterial::generate("other-key").unwrap(); + let trust = workspace_trust(&identity); + for case in [ + "untrusted", + "revoked", + "bad_signature", + "wrong_key", + "wrong_runtime", + "wrong_operation", + "expired", + "wrong_workspace_scope", + "unsigned", + ] { + let mut current = trust.clone(); + if case == "revoked" { + current.state = WorkspaceIssuerTrustState::Revoked; } - let authority = Arc::new(InMemoryWorkspaceRuntimeVerificationAuthority::default()); - authority.record(old_verification.clone()).unwrap(); + let records = if case == "untrusted" { + vec![] + } else { + vec![current] + }; let app = runtime_http_router_with_workspace_auth( Runtime::new_memory(), None, - WorkspaceRuntimeHttpAuth { - verifier: WorkspaceCapabilityVerifier::new( - vec![trust.clone()], - Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), - ) - .unwrap(), - signer: RuntimeVerificationSigner::from_identity(&runtime_key).unwrap(), - verifications: authority.clone(), - }, + workspace_test_auth(records), ); - let claims = WorkspaceCapabilityClaims { - issuer: trust.backend_url.clone(), - issuer_workspace_id: trust.workspace_id.clone(), - issuer_key_id: trust.key_id.clone(), - issuer_public_key_fingerprint: trust.public_key_fingerprint.clone(), - trust_id: trust.trust_id.clone(), - binding_id: current.binding_id.clone(), - runtime_id: current.runtime_id.clone(), - worker_id: None, - operation: RUNTIME_PING_PERMISSION.into(), - method: "GET".into(), - path_and_query: "/v1/ping".into(), - body_digest: workspace_request_body_digest(&[]), - iat: unix_now_i64(), - exp: unix_now_i64() + 60, - jti: "stale-verification".into(), - }; - for (expected, token_id) in [ - (StatusCode::FORBIDDEN, "stale-verification"), - (StatusCode::OK, "fresh-verification"), - ] { - if expected == StatusCode::OK { - authority.record(current.clone()).unwrap(); + let mut claims = signed_claims(&trust, "GET", "/v1/ping", &[]); + match case { + "wrong_key" => claims.issuer_key_id = "untrusted-key".into(), + "wrong_runtime" => claims.runtime_id = "another-runtime".into(), + "wrong_operation" => claims.operation = "workers:create".into(), + "expired" => { + claims.iat -= 120; + claims.exp -= 120; } - let token = issue_workspace_capability_token( - &workspace_key.signing_key().unwrap(), - &WorkspaceCapabilityClaims { - jti: token_id.into(), - ..claims.clone() + _ => {} + } + let response = if case == "unsigned" { + app.oneshot( + Request::builder() + .uri("/v1/ping") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap() + } else { + signed_request( + app, + if case == "bad_signature" { + &other_key + } else { + &identity + }, + &claims, + vec![], + if case == "wrong_workspace_scope" { + "workspace-b" + } else { + "workspace-a" }, ) - .unwrap(); - let response = app - .clone() - .oneshot( - Request::builder() - .uri("/v1/ping") - .header(header::AUTHORIZATION, format!("Bearer {token}")) - .header(RUNTIME_WORKSPACE_SCOPE_HEADER, "workspace-a") - .body(Body::empty()) - .unwrap(), - ) - .await - .unwrap(); - let status = response.status(); - let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); - assert_eq!( - status, - expected, - "{changed}: {}", - String::from_utf8_lossy(&body) - ); - if expected == StatusCode::FORBIDDEN { - let error: RuntimeHttpErrorResponse = serde_json::from_slice(&body).unwrap(); - assert_eq!(error.error.code, "workspace_runtime_verification_stale"); - } - } + .await + }; + let status = response.status(); + let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + assert_eq!( + status, + if case == "wrong_workspace_scope" { + StatusCode::FORBIDDEN + } else { + StatusCode::UNAUTHORIZED + }, + "{case}: {}", + String::from_utf8_lossy(&bytes) + ); + let error: RuntimeHttpErrorResponse = serde_json::from_slice(&bytes).unwrap(); + assert_eq!( + error.error.code, + if case == "wrong_workspace_scope" { + "runtime_ping_workspace_scope_mismatch" + } else { + "unauthorized" + }, + "{case}" + ); + } + } + + #[tokio::test] + async fn ordinary_signed_workdir_operation_succeeds_without_evidence_and_rejects_changed_body() + { + let temp = tempfile::tempdir().unwrap(); + std::fs::write(temp.path().join("hello.txt"), "hello").unwrap(); + let identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); + let trust = workspace_trust(&identity); + let session: WorkdirSessionHandle = Arc::new(LocalWorkdirSession::materialized_bound( + Workdir::new("wd-1"), + temp.path().to_path_buf(), + temp.path().to_path_buf(), + SharedScope::new(Scope::writable(temp.path()).unwrap()), + WorkdirSessionCapabilities::ALL, + )); + let state = RuntimeHttpState { + runtime: Runtime::new_memory(), + local_token: None, + workspace_auth: Some(Arc::new(workspace_test_auth(vec![trust.clone()]))), + workdir_sessions: Arc::new(Mutex::new(HashMap::from([( + "session-1".into(), + RuntimeHttpWorkdirSession { + owner: RuntimeWorkspaceScope::new("workspace-a", "https://backend.test"), + session, + }, + )]))), + ssh_keyscan_program: Arc::new(PathBuf::from("ssh-keyscan")), + }; + let app = Router::new() + .route( + "/v1/workdir-sessions/{session_id}/operations", + post(run_workdir_session_operation), + ) + .with_state(state.clone()) + .layer(middleware::from_fn_with_state(state, require_runtime_auth)); + let body = serde_json::to_vec(&WorkdirSessionOperationRequest { + operation: WorkdirSessionOperation::Stat(StatRequest { + path: WorkdirPath::new("hello.txt").unwrap(), + }), + }) + .unwrap(); + let claims = signed_claims( + &trust, + "POST", + "/v1/workdir-sessions/session-1/operations", + &body, + ); + let response = signed_request(app.clone(), &identity, &claims, body, "workspace-a").await; + assert_eq!(response.status(), StatusCode::OK); + let result: WorkdirSessionOperationResult = read_json(response).await; + assert!(matches!(result, WorkdirSessionOperationResult::Stat(_))); + let claims = WorkspaceCapabilityClaims { + jti: uuid::Uuid::now_v7().to_string(), + ..claims + }; + let response = signed_request(app, &identity, &claims, b"{}".to_vec(), "workspace-a").await; + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); + let error: RuntimeHttpErrorResponse = read_json(response).await; + assert!(error.error.message.contains("request body")); + } + + #[tokio::test] + async fn removed_handshake_routes_are_not_network_aliases() { + let identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); + let trust = workspace_trust(&identity); + let app = runtime_http_router_with_workspace_auth( + Runtime::new_memory(), + None, + workspace_test_auth(vec![trust.clone()]), + ); + for path in [ + "/v1/workspace-runtime-verification/challenge", + "/v1/workspace-runtime-verification/acknowledgement", + ] { + let claims = signed_claims(&trust, "POST", path, b"{}"); + let response = signed_request( + app.clone(), + &identity, + &claims, + b"{}".to_vec(), + "workspace-a", + ) + .await; + assert_eq!(response.status(), StatusCode::NOT_FOUND); } } @@ -4630,8 +4276,7 @@ mod tests { Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), ) .unwrap(), - signer: RuntimeVerificationSigner::from_identity(&identity).unwrap(), - verifications: Arc::new(InMemoryWorkspaceRuntimeVerificationAuthority::default()), + runtime_id: identity.identity_id.clone(), }, ); (runtime, app) diff --git a/crates/worker-runtime/src/http_server/runtime_management_api.rs b/crates/worker-runtime/src/http_server/runtime_management_api.rs index b92e22731..d8a919433 100644 --- a/crates/worker-runtime/src/http_server/runtime_management_api.rs +++ b/crates/worker-runtime/src/http_server/runtime_management_api.rs @@ -164,7 +164,7 @@ impl runtime_api::RuntimeApi for RuntimeManagementApi { .state .workspace_auth .as_ref() - .map(|auth| auth.signer.runtime_id().trim()) + .map(|auth| auth.runtime_id.trim()) .filter(|runtime_id| !runtime_id.is_empty()) .ok_or_else(|| { runtime_api::RuntimeApiError::new( diff --git a/crates/worker-runtime/src/main.rs b/crates/worker-runtime/src/main.rs index 08cdb7e64..d8b985f43 100644 --- a/crates/worker-runtime/src/main.rs +++ b/crates/worker-runtime/src/main.rs @@ -27,11 +27,11 @@ use worker_runtime::http_server::{ use worker_runtime::worker_backend::{ProfileRuntimeWorkerFactory, WorkerRuntimeExecutionBackend}; use worker_runtime::working_directory::RuntimeGitMaterializer; use worker_runtime::workspace_issuer::{ - FileWorkspaceClaimReplayProtection, FileWorkspaceRuntimeVerificationAuthority, - MAX_WORKSPACE_ISSUER_TRUST_RECORDS, RuntimeVerificationSigner, WorkspaceCapabilityVerifier, - WorkspaceIssuerTrustError, WorkspaceIssuerTrustMutation, WorkspaceIssuerTrustRecord, - WorkspaceIssuerTrustState, add_workspace_issuer_trust, replace_workspace_issuer_trust, - revoke_workspace_issuer_trust, validate_workspace_issuer_trust_records, + FileWorkspaceClaimReplayProtection, MAX_WORKSPACE_ISSUER_TRUST_RECORDS, + WorkspaceCapabilityVerifier, WorkspaceIssuerTrustError, WorkspaceIssuerTrustMutation, + WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, add_workspace_issuer_trust, + replace_workspace_issuer_trust, revoke_workspace_issuer_trust, + validate_workspace_issuer_trust_records, }; use worker_runtime::workspace_request::RuntimeWorkspaceRequestClient; use worker_runtime::{Runtime, RuntimeOptions}; @@ -972,7 +972,7 @@ fn read_runtime_auth_file(path: &Path) -> Result validate_workspace_issuer_trust_records(&auth.workspace_issuers) .map_err(|_| ProcessError::auth("runtime Workspace issuer trust store is corrupt"))?; if migrated { - // Commit the revoked migration before exposing any issuer authority. + // Persist the validated migration without changing saved trust permissions. write_runtime_auth_file(path, &auth)?; } Ok(auth) @@ -1025,25 +1025,15 @@ fn load_workspace_runtime_http_auth( let Some(identity) = auth.identity else { return Ok(None); }; - if auth.workspace_issuers.is_empty() { - return Ok(None); - } let replay_path = runtime_auth_path(config).with_extension("workspace-replay.json"); let verifier = WorkspaceCapabilityVerifier::new( auth.workspace_issuers, Arc::new(FileWorkspaceClaimReplayProtection::new(replay_path)), ) .map_err(|error| ProcessError::auth(format!("invalid Workspace issuer trust: {error}")))?; - let signer = RuntimeVerificationSigner::from_identity(&identity) - .map_err(|error| ProcessError::auth(format!("invalid Runtime identity: {error}")))?; - let verifications_path = - runtime_auth_path(config).with_extension("workspace-verifications.json"); Ok(Some(WorkspaceRuntimeHttpAuth { verifier, - signer, - verifications: Arc::new(FileWorkspaceRuntimeVerificationAuthority::new( - verifications_path, - )), + runtime_id: identity.identity_id, })) } @@ -1407,6 +1397,34 @@ mod tests { assert_eq!(std::fs::read(root.join("runtime.json")).unwrap(), before); } + #[test] + fn runtime_identity_with_no_trusted_workspace_does_not_disable_request_authentication() { + let temp = tempfile::tempdir().unwrap(); + let config = ProcessConfig { + fs_root: Some(temp.path().to_path_buf()), + ..ProcessConfig::default().unwrap() + }; + let path = runtime_auth_path(&config); + write_runtime_auth_file( + &path, + &RuntimeAuthFile { + identity: Some(RuntimeIdentityMaterial::generate("runtime-test").unwrap()), + ..RuntimeAuthFile::default() + }, + ) + .unwrap(); + // Obsolete evidence is neither a startup prerequisite nor live authority. + let evidence = path.with_extension("workspace-verifications.json"); + std::fs::write(&evidence, "obsolete evidence, not even valid JSON").unwrap(); + let auth = load_workspace_runtime_http_auth(&config).unwrap().unwrap(); + assert_eq!(auth.runtime_id, "runtime-test"); + assert!(!auth.verifier.has_active_workspace_issuer("workspace-a")); + assert_eq!( + std::fs::read_to_string(&evidence).unwrap(), + "obsolete evidence, not even valid JSON" + ); + } + #[test] fn workspace_issuer_trust_cli_persists_across_reload_and_writes_private_mode() { use server_api::WorkspacePublicIdentityBundle; @@ -1454,38 +1472,6 @@ mod tests { let first = read_runtime_auth_file(&path).unwrap(); assert_eq!(first.workspace_issuers.len(), 1); assert!(!first.workspace_issuers[0].trust_id.is_empty()); - // `trust-workspace show` serializes this persisted record. Owner - // registration must forward that exact enrollment ID, not derive one - // from the key fingerprint or an initial counter. - let shown = serde_json::to_value(&first.workspace_issuers[0]).unwrap(); - let runtime_identity = RuntimeIdentityMaterial::generate("remote-runtime").unwrap(); - let mut registration_wire = serde_json::json!({ - "public_bundle": { - "identity_id": runtime_identity.identity_id, - "public_key": runtime_identity.public_key, - }, - "endpoint": "https://runtime.example.test", - "workspace_trust_id": shown["trust_id"], - }); - let registration: server_api::CreateRemoteRuntimeRequest = - serde_json::from_value(registration_wire.clone()).unwrap(); - assert_eq!( - registration.workspace_trust_id, - first.workspace_issuers[0].trust_id - ); - assert_ne!( - registration.workspace_trust_id, - first.workspace_issuers[0].public_key_fingerprint - ); - registration_wire - .as_object_mut() - .unwrap() - .remove("workspace_trust_id"); - assert!( - serde_json::from_value::(registration_wire) - .is_err() - ); - run_trust_workspace_command(VecDeque::from([ "add".to_string(), "--bundle".to_string(), @@ -1645,9 +1631,14 @@ mod tests { assert_eq!(error, "invalid trust-workspace list arguments"); } - #[test] - fn pre_id_issuer_migration_retains_history_without_restoring_authority() { + #[tokio::test] + async fn pre_id_issuer_migration_preserves_saved_permission_for_ordinary_signed_ping() { use sha2::{Digest as _, Sha256}; + use tower::ServiceExt; + use worker_runtime::workspace_issuer::{ + WorkspaceCapabilityClaims, issue_workspace_capability_token, + workspace_request_body_digest, + }; let identity = RuntimeIdentityMaterial::generate("WK-legacy").unwrap(); let public_key = decode_public_key(&identity.public_key).unwrap(); @@ -1680,16 +1671,97 @@ mod tests { std::fs::write(&path, toml::to_string(&document).unwrap()).unwrap(); let migrated = read_runtime_auth_file(&path).unwrap(); - assert_eq!( - migrated.workspace_issuers[0].state, - worker_runtime::workspace_issuer::WorkspaceIssuerTrustState::Revoked + let expected_state = if state == "active" { + WorkspaceIssuerTrustState::Active + } else { + WorkspaceIssuerTrustState::Revoked + }; + let record = &migrated.workspace_issuers[0]; + assert_eq!(record.state, expected_state); + assert_eq!(record.workspace_id, "workspace-1"); + assert_eq!(record.backend_url, "https://backend.example.test"); + assert_eq!(record.key_id, "WK-legacy"); + assert_eq!(record.public_key, identity.public_key); + assert_eq!(record.public_key_fingerprint, fingerprint); + assert_eq!(record.registered_at_unix, 10); + assert_eq!(record.updated_at_unix, 20); + // Migration is durably saved by the loader itself, not just by a later write. + assert_eq!(read_runtime_auth_file(&path).unwrap(), migrated); + let claims = WorkspaceCapabilityClaims { + issuer: record.backend_url.clone(), + issuer_workspace_id: record.workspace_id.clone(), + issuer_key_id: record.key_id.clone(), + issuer_public_key_fingerprint: record.public_key_fingerprint.clone(), + runtime_id: "runtime-test".into(), + worker_id: None, + operation: worker_runtime::http_server::RUNTIME_PING_PERMISSION.into(), + method: "GET".into(), + path_and_query: "/v1/ping".into(), + body_digest: workspace_request_body_digest(&[]), + iat: i64::try_from(worker_runtime::auth::unix_now_seconds()).unwrap(), + exp: i64::try_from(worker_runtime::auth::unix_now_seconds()).unwrap() + 60, + jti: uuid::Uuid::now_v7().to_string(), + }; + let token = issue_workspace_capability_token(&identity.signing_key().unwrap(), &claims) + .unwrap(); + let app = worker_runtime::http_server::runtime_http_router_with_workspace_auth( + Runtime::new_memory(), None, WorkspaceRuntimeHttpAuth { + runtime_id: "runtime-test".into(), + verifier: WorkspaceCapabilityVerifier::new(migrated.workspace_issuers.clone(), + Arc::new(worker_runtime::workspace_issuer::InMemoryWorkspaceClaimReplayProtection::default())).unwrap(), + }, ); + let response = app + .oneshot( + axum::http::Request::builder() + .uri("/v1/ping") + .header(axum::http::header::AUTHORIZATION, format!("Bearer {token}")) + .header( + worker_runtime::http_server::RUNTIME_WORKSPACE_SCOPE_HEADER, + "workspace-1", + ) + .body(axum::body::Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + let status = response.status(); + let body = axum::body::to_bytes(response.into_body(), usize::MAX) + .await + .unwrap(); + assert_eq!( + status, + if state == "active" { + axum::http::StatusCode::OK + } else { + axum::http::StatusCode::UNAUTHORIZED + }, + "{state}: {}", + String::from_utf8_lossy(&body) + ); + if state == "active" { + let ping: runtime_api::RuntimePingResponse = serde_json::from_slice(&body).unwrap(); + assert_eq!(ping.runtime_id, "runtime-test"); + } else { + let error: worker_runtime::http_server::RuntimeHttpErrorResponse = + serde_json::from_slice(&body).unwrap(); + assert_eq!(error.error.code, "unauthorized"); + assert!(error.error.message.contains("revoked")); + } assert_eq!(migrated.archived_legacy_issuers.len(), 1); let archived: toml::Value = toml::from_str(&migrated.archived_legacy_issuers[0]).unwrap(); assert_eq!(archived, original_record); write_runtime_auth_file(&path, &migrated).unwrap(); assert_eq!(read_runtime_auth_file(&path).unwrap(), migrated); + if state == "active" { + // A current saved Revoked record stays revoked even if its archived + // pre-ID source was Active. Never guess revocation provenance. + let mut saved_revoked = migrated.clone(); + saved_revoked.workspace_issuers[0].state = WorkspaceIssuerTrustState::Revoked; + write_runtime_auth_file(&path, &saved_revoked).unwrap(); + assert_eq!(read_runtime_auth_file(&path).unwrap(), saved_revoked); + } } } diff --git a/crates/worker-runtime/src/workspace_issuer.rs b/crates/worker-runtime/src/workspace_issuer.rs index 53d706680..1ae60192f 100644 --- a/crates/worker-runtime/src/workspace_issuer.rs +++ b/crates/worker-runtime/src/workspace_issuer.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::fmt; use std::fs; use std::path::{Path, PathBuf}; @@ -13,6 +13,9 @@ use sha2::{Digest, Sha256}; use crate::auth::RuntimeAuthError; +// The v2 signing domain remains, but only the current deny_unknown_fields claim +// shape is accepted. Old binding/trust fields fail decoding even with a valid +// signature; retaining the prefix is not a network compatibility decoder. const WORKSPACE_TOKEN_PREFIX: &str = "yoi-workspace-v2"; const WORKSPACE_SIGNING_INPUT_PREFIX: &str = "yoi.workspace.capability.v2."; const WORKSPACE_SIGNING_ALGORITHM: &str = "ed25519"; @@ -82,7 +85,8 @@ impl WorkspaceIssuerTrustRecord { } } -// Frozen pre-ID enrollment format. It is never accepted as live authority. +// Frozen pre-ID saved enrollment format. Only persisted-data migration reads it; +// network requests never supply trust records or authorize their own migration. #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct LegacyWorkspaceIssuerTrustRecord { @@ -101,10 +105,10 @@ struct LegacyWorkspaceIssuerTrustRecord { updated_at_unix: i64, } -/// Migrate a pre-ID enrollment as revoked. At persisted-data decode, legacy counters -/// must be positive but are never compared with current identity or trust. -/// Only an explicit operator replacement can accept new trust; no legacy -/// capability can authorize its own migration. +/// Migrate validated saved trust without changing its Active/Revoked permission. +/// Legacy counters must be positive at persisted-data decode but are never +/// compared with current identity or trust. The generated trust ID is only an +/// administrative identity; it neither grants nor revokes request authorization. pub fn migrate_legacy_workspace_issuer_trust_record( value: serde_json::Value, ) -> Result { @@ -125,9 +129,7 @@ pub fn migrate_legacy_workspace_issuer_trust_record( uuid::Uuid::now_v7().to_string(), legacy.registered_at_unix, )?; - // Both old Active and Revoked records require new enrollment acceptance. - let _old_state = legacy.state; - record.state = WorkspaceIssuerTrustState::Revoked; + record.state = legacy.state; record.updated_at_unix = legacy.updated_at_unix; Ok(record) } @@ -300,664 +302,6 @@ fn validate_id(value: &str) -> Result<(), WorkspaceIssuerTrustError> { Ok(()) } -pub const WORKSPACE_VERIFICATION_CHALLENGE_PATH: &str = - "/v1/workspace-runtime-verification/challenge"; -pub const WORKSPACE_VERIFICATION_ACK_PATH: &str = - "/v1/workspace-runtime-verification/acknowledgement"; -pub const WORKSPACE_VERIFICATION_OPERATION: &str = "workspace.runtime.verify"; -const RUNTIME_VERIFICATION_TOKEN_PREFIX: &str = "yoi-runtime-verification-v2"; -const RUNTIME_VERIFICATION_SIGNING_INPUT_PREFIX: &str = "yoi.runtime.verification.v2."; - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkspaceRuntimeVerificationChallenge { - pub challenge_id: String, - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, - pub workspace_nonce: String, - pub expires_at: i64, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkspaceRuntimeVerificationResponse { - pub challenge_id: String, - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, - pub workspace_nonce: String, - pub runtime_nonce: String, - pub expires_at: i64, - pub response_proof: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkspaceRuntimeVerificationAcknowledgement { - pub challenge_id: String, - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, - pub workspace_nonce: String, - pub runtime_nonce: String, - pub response_digest: String, - pub response: WorkspaceRuntimeVerificationResponse, - pub expires_at: i64, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkspaceRuntimeVerificationReceipt { - pub challenge_id: String, - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub accepted_at: i64, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkspaceRuntimeVerificationRecord { - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, - pub verified_at: i64, -} - -pub trait WorkspaceRuntimeVerificationAuthority: fmt::Debug + Send + Sync { - fn begin( - &self, - challenge: WorkspaceRuntimeVerificationChallenge, - ) -> Result<(), WorkspaceCapabilityVerificationError>; - fn acknowledge( - &self, - challenge_id: &str, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError>; - fn get( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result, WorkspaceCapabilityVerificationError>; - fn record( - &self, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError>; -} - -#[derive(Debug, Default)] -pub struct InMemoryWorkspaceRuntimeVerificationAuthority { - state: Mutex, -} - -impl WorkspaceRuntimeVerificationAuthority for InMemoryWorkspaceRuntimeVerificationAuthority { - fn begin( - &self, - challenge: WorkspaceRuntimeVerificationChallenge, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - let mut state = self - .state - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - begin_verification(&mut state, challenge) - } - fn acknowledge( - &self, - challenge_id: &str, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - let mut state = self - .state - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - acknowledge_verification(&mut state, challenge_id, record) - } - fn get( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result, WorkspaceCapabilityVerificationError> - { - let state = self - .state - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - Ok(state - .records - .iter() - .find(|record| record.workspace_id == workspace_id && record.runtime_id == runtime_id) - .cloned()) - } - fn record( - &self, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - validate_verification_record(&record)?; - let mut state = self - .state - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - state.records.retain(|current| { - current.workspace_id != record.workspace_id || current.runtime_id != record.runtime_id - }); - if state.records.len() >= MAX_WORKSPACE_ISSUER_TRUST_RECORDS { - return Err(WorkspaceCapabilityVerificationError::TrustRecordLimitExceeded); - } - state.records.push(record); - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub struct FileWorkspaceRuntimeVerificationAuthority { - path: PathBuf, - lock: Arc>, -} - -#[derive(Debug, Default, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct WorkspaceRuntimeVerificationDocument { - version: u32, - records: Vec, - #[serde(default)] - pending: Vec, - /// Exact pre-ID document retained only as history; never used to authorize requests. - #[serde(default, skip_serializing_if = "Option::is_none")] - archived_legacy_document: Option, -} - -// Frozen verification evidence from before trust/binding IDs. Never promote -// counters into IDs or keep a pre-ID acknowledgement usable after migration. -#[derive(Deserialize)] -#[serde(deny_unknown_fields)] -struct LegacyVerificationDocument { - version: u32, - records: Vec, -} -#[derive(Deserialize)] -#[serde(deny_unknown_fields)] -#[allow(dead_code)] -struct LegacyVerificationRecord { - workspace_id: String, - runtime_id: String, - #[serde(rename = "binding_revision")] - legacy_binding_counter: u64, - workspace_key_id: String, - #[serde(rename = "workspace_identity_revision")] - legacy_workspace_identity_counter: u64, - #[serde(rename = "workspace_trust_generation")] - legacy_workspace_trust_counter: u64, - runtime_public_key_fingerprint: String, - #[serde(rename = "runtime_identity_revision")] - legacy_runtime_identity_counter: u64, - verified_at: i64, -} - -impl FileWorkspaceRuntimeVerificationAuthority { - pub fn new(path: impl Into) -> Self { - Self { - path: path.into(), - lock: Arc::new(Mutex::new(())), - } - } - - // No pre-ID acknowledgement is live authority. Keep its exact source bytes - // in the persisted archive while new challenges populate only current records. - // Neither read(), get(), nor capability verification consults that archive. - fn read( - &self, - ) -> Result { - match fs::read(&self.path) { - Ok(bytes) => { - let raw: serde_json::Value = serde_json::from_slice(&bytes).map_err(|_| { - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?; - if raw.get("version").and_then(serde_json::Value::as_u64) == Some(1) { - let legacy: LegacyVerificationDocument = - serde_json::from_value(raw).map_err(|_| { - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?; - if legacy.version != 1 - || legacy.records.len() > MAX_WORKSPACE_ISSUER_TRUST_RECORDS - { - return Err( - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable, - ); - } - let migrated = WorkspaceRuntimeVerificationDocument { - version: 2, - records: Vec::new(), - pending: Vec::new(), - archived_legacy_document: Some(String::from_utf8(bytes).map_err(|_| { - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?), - }; - self.write(&migrated)?; - return Ok(migrated); - } - let document: WorkspaceRuntimeVerificationDocument = serde_json::from_value(raw) - .map_err(|_| { - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?; - if document.version != 2 - || document.records.len() > MAX_WORKSPACE_ISSUER_TRUST_RECORDS - || document.pending.len() > MAX_WORKSPACE_ISSUER_TRUST_RECORDS - { - return Err( - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable, - ); - } - let mut identities = HashSet::new(); - for record in &document.records { - validate_verification_record(record)?; - if !identities - .insert((record.workspace_id.as_str(), record.runtime_id.as_str())) - { - return Err( - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable, - ); - } - } - let mut pending_identities = HashSet::new(); - for challenge in &document.pending { - validate_verification_challenge(challenge)?; - if !pending_identities.insert((&challenge.workspace_id, &challenge.runtime_id)) - { - return Err( - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable, - ); - } - } - Ok(document) - } - Err(error) if error.kind() == std::io::ErrorKind::NotFound => { - Ok(WorkspaceRuntimeVerificationDocument { - version: 2, - records: Vec::new(), - pending: Vec::new(), - archived_legacy_document: None, - }) - } - Err(_) => Err(WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable), - } - } - - fn write( - &self, - document: &WorkspaceRuntimeVerificationDocument, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - let parent = self.path.parent().unwrap_or_else(|| Path::new(".")); - fs::create_dir_all(parent) - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - let temporary = parent.join(format!( - ".workspace-runtime-verification-{}.tmp", - uuid::Uuid::now_v7() - )); - let bytes = serde_json::to_vec(document) - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - fs::write(&temporary, bytes) - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt as _; - fs::set_permissions(&temporary, fs::Permissions::from_mode(0o600)).map_err(|_| { - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?; - } - fs::rename(&temporary, &self.path).map_err(|_| { - let _ = fs::remove_file(&temporary); - WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable - })?; - Ok(()) - } -} - -impl WorkspaceRuntimeVerificationAuthority for FileWorkspaceRuntimeVerificationAuthority { - fn begin( - &self, - challenge: WorkspaceRuntimeVerificationChallenge, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - let _guard = self - .lock - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - let mut document = self.read()?; - begin_verification(&mut document, challenge)?; - self.write(&document) - } - fn acknowledge( - &self, - challenge_id: &str, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - let _guard = self - .lock - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - let mut document = self.read()?; - acknowledge_verification(&mut document, challenge_id, record)?; - self.write(&document) - } - - fn get( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result, WorkspaceCapabilityVerificationError> - { - let _guard = self - .lock - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - Ok(self - .read()? - .records - .into_iter() - .find(|record| record.workspace_id == workspace_id && record.runtime_id == runtime_id)) - } - - fn record( - &self, - record: WorkspaceRuntimeVerificationRecord, - ) -> Result<(), WorkspaceCapabilityVerificationError> { - validate_verification_record(&record)?; - let _guard = self - .lock - .lock() - .map_err(|_| WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable)?; - let mut document = self.read()?; - document.records.retain(|current| { - current.workspace_id != record.workspace_id || current.runtime_id != record.runtime_id - }); - document.records.push(record); - self.write(&document) - } -} - -fn begin_verification( - document: &mut WorkspaceRuntimeVerificationDocument, - challenge: WorkspaceRuntimeVerificationChallenge, -) -> Result<(), WorkspaceCapabilityVerificationError> { - validate_verification_challenge(&challenge)?; - if let Some(pending) = document.pending.iter().find(|pending| { - pending.workspace_id == challenge.workspace_id && pending.runtime_id == challenge.runtime_id - }) { - if pending.challenge_id == challenge.challenge_id && pending != &challenge { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - } - if document.pending.len() >= MAX_WORKSPACE_ISSUER_TRUST_RECORDS { - return Err(WorkspaceCapabilityVerificationError::TrustRecordLimitExceeded); - } - document.pending.retain(|pending| { - pending.workspace_id != challenge.workspace_id || pending.runtime_id != challenge.runtime_id - }); - document.records.retain(|record| { - record.workspace_id != challenge.workspace_id || record.runtime_id != challenge.runtime_id - }); - document.pending.push(challenge); - Ok(()) -} - -fn acknowledge_verification( - document: &mut WorkspaceRuntimeVerificationDocument, - challenge_id: &str, - record: WorkspaceRuntimeVerificationRecord, -) -> Result<(), WorkspaceCapabilityVerificationError> { - validate_verification_record(&record)?; - let index = document - .pending - .iter() - .position(|pending| { - pending.workspace_id == record.workspace_id - && pending.runtime_id == record.runtime_id - && pending.challenge_id == challenge_id - && pending.binding_id == record.binding_id - && pending.workspace_key_id == record.workspace_key_id - && pending.workspace_public_key_fingerprint - == record.workspace_public_key_fingerprint - && pending.workspace_trust_id == record.workspace_trust_id - && pending.runtime_public_key_fingerprint == record.runtime_public_key_fingerprint - && record.verified_at < pending.expires_at - }) - .ok_or(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch)?; - if document.records.len() >= MAX_WORKSPACE_ISSUER_TRUST_RECORDS { - return Err(WorkspaceCapabilityVerificationError::TrustRecordLimitExceeded); - } - document.pending.remove(index); - document.records.retain(|current| { - current.workspace_id != record.workspace_id || current.runtime_id != record.runtime_id - }); - document.records.push(record); - Ok(()) -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct RuntimeVerificationResponseClaims { - response: WorkspaceRuntimeVerificationResponseUnsigned, - method: String, - path_and_query: String, - body_digest: String, - iat: i64, - exp: i64, - jti: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct WorkspaceRuntimeVerificationResponseUnsigned { - challenge_id: String, - workspace_id: String, - runtime_id: String, - binding_id: String, - workspace_key_id: String, - workspace_public_key_fingerprint: String, - workspace_trust_id: String, - runtime_public_key_fingerprint: String, - workspace_nonce: String, - runtime_nonce: String, - expires_at: i64, -} - -impl WorkspaceRuntimeVerificationResponse { - fn unsigned(&self) -> WorkspaceRuntimeVerificationResponseUnsigned { - WorkspaceRuntimeVerificationResponseUnsigned { - challenge_id: self.challenge_id.clone(), - workspace_id: self.workspace_id.clone(), - runtime_id: self.runtime_id.clone(), - binding_id: self.binding_id.clone(), - workspace_key_id: self.workspace_key_id.clone(), - workspace_public_key_fingerprint: self.workspace_public_key_fingerprint.clone(), - workspace_trust_id: self.workspace_trust_id.clone(), - runtime_public_key_fingerprint: self.runtime_public_key_fingerprint.clone(), - workspace_nonce: self.workspace_nonce.clone(), - runtime_nonce: self.runtime_nonce.clone(), - expires_at: self.expires_at, - } - } -} - -#[derive(Clone)] -pub struct RuntimeVerificationSigner { - runtime_id: String, - public_key: String, - public_key_fingerprint: String, - signing_key: Arc, -} - -impl fmt::Debug for RuntimeVerificationSigner { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter - .debug_struct("RuntimeVerificationSigner") - .field("runtime_id", &self.runtime_id) - .field("public_key_fingerprint", &self.public_key_fingerprint) - .finish_non_exhaustive() - } -} - -impl RuntimeVerificationSigner { - pub fn from_identity( - identity: &crate::auth::RuntimeIdentityMaterial, - ) -> Result { - let public_key = crate::auth::decode_public_key(&identity.public_key) - .map_err(|_| WorkspaceCapabilityVerificationError::TrustRecordCorrupt)?; - let fingerprint = format!("sha256:{}", hex_lower(&Sha256::digest(public_key))); - Ok(Self { - runtime_id: identity.identity_id.clone(), - public_key: identity.public_key.clone(), - public_key_fingerprint: fingerprint, - signing_key: Arc::new( - identity - .signing_key() - .map_err(|_| WorkspaceCapabilityVerificationError::TrustRecordCorrupt)?, - ), - }) - } - - pub fn runtime_id(&self) -> &str { - &self.runtime_id - } - - pub fn public_key_fingerprint(&self) -> &str { - &self.public_key_fingerprint - } - - pub fn public_key(&self) -> &str { - &self.public_key - } - - pub fn sign_response( - &self, - challenge: &WorkspaceRuntimeVerificationChallenge, - runtime_nonce: String, - now_unix: i64, - ) -> Result { - validate_verification_challenge(challenge)?; - if challenge.runtime_id != self.runtime_id - || challenge.runtime_public_key_fingerprint != self.public_key_fingerprint - || challenge.expires_at <= now_unix - { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - let unsigned = WorkspaceRuntimeVerificationResponseUnsigned { - challenge_id: challenge.challenge_id.clone(), - workspace_id: challenge.workspace_id.clone(), - runtime_id: challenge.runtime_id.clone(), - binding_id: challenge.binding_id.clone(), - workspace_key_id: challenge.workspace_key_id.clone(), - workspace_public_key_fingerprint: challenge.workspace_public_key_fingerprint.clone(), - workspace_trust_id: challenge.workspace_trust_id.clone(), - runtime_public_key_fingerprint: challenge.runtime_public_key_fingerprint.clone(), - workspace_nonce: challenge.workspace_nonce.clone(), - runtime_nonce, - expires_at: challenge.expires_at, - }; - let claims = RuntimeVerificationResponseClaims { - response: unsigned.clone(), - method: "POST".to_string(), - path_and_query: WORKSPACE_VERIFICATION_CHALLENGE_PATH.to_string(), - body_digest: workspace_request_body_digest( - &serde_json::to_vec(challenge) - .map_err(|_| WorkspaceCapabilityVerificationError::MalformedClaims)?, - ), - iat: now_unix, - exp: challenge.expires_at, - jti: uuid::Uuid::now_v7().to_string(), - }; - let proof = crate::auth::sign_json_token( - RUNTIME_VERIFICATION_TOKEN_PREFIX, - RUNTIME_VERIFICATION_SIGNING_INPUT_PREFIX, - self.signing_key.as_ref(), - &claims, - ) - .map_err(|_| WorkspaceCapabilityVerificationError::MalformedClaims)?; - Ok(WorkspaceRuntimeVerificationResponse { - challenge_id: unsigned.challenge_id, - workspace_id: unsigned.workspace_id, - runtime_id: unsigned.runtime_id, - binding_id: unsigned.binding_id, - workspace_key_id: unsigned.workspace_key_id, - workspace_public_key_fingerprint: unsigned.workspace_public_key_fingerprint, - workspace_trust_id: unsigned.workspace_trust_id, - runtime_public_key_fingerprint: unsigned.runtime_public_key_fingerprint, - workspace_nonce: unsigned.workspace_nonce, - runtime_nonce: unsigned.runtime_nonce, - expires_at: unsigned.expires_at, - response_proof: proof, - }) - } -} - -pub fn verify_runtime_verification_response( - response: &WorkspaceRuntimeVerificationResponse, - challenge: &WorkspaceRuntimeVerificationChallenge, - runtime_public_key: &str, - now_unix: i64, -) -> Result<(), WorkspaceCapabilityVerificationError> { - validate_verification_challenge(challenge)?; - let public_key = crate::auth::decode_public_key(runtime_public_key) - .map_err(|_| WorkspaceCapabilityVerificationError::InvalidSignature)?; - let fingerprint = format!("sha256:{}", hex_lower(&Sha256::digest(&public_key))); - if fingerprint != challenge.runtime_public_key_fingerprint { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - let signed = crate::auth::decode_signed_json_token::( - &response.response_proof, - RUNTIME_VERIFICATION_TOKEN_PREFIX, - ) - .map_err(|_| WorkspaceCapabilityVerificationError::MalformedToken)?; - crate::auth::verify_signed_json_token( - RUNTIME_VERIFICATION_SIGNING_INPUT_PREFIX, - &signed.payload, - &signed.signature, - runtime_public_key, - ) - .map_err(|_| WorkspaceCapabilityVerificationError::InvalidSignature)?; - let expected_body_digest = workspace_request_body_digest( - &serde_json::to_vec(challenge) - .map_err(|_| WorkspaceCapabilityVerificationError::MalformedClaims)?, - ); - if signed.claims.response != response.unsigned() - || signed.claims.method != "POST" - || signed.claims.path_and_query != WORKSPACE_VERIFICATION_CHALLENGE_PATH - || signed.claims.body_digest != expected_body_digest - || signed.claims.exp != response.expires_at - || signed.claims.iat > now_unix.saturating_add(MAX_CLOCK_SKEW_SECONDS) - || signed.claims.exp <= now_unix - || response.challenge_id != challenge.challenge_id - || response.workspace_id != challenge.workspace_id - || response.runtime_id != challenge.runtime_id - || response.binding_id != challenge.binding_id - || response.workspace_key_id != challenge.workspace_key_id - || response.workspace_public_key_fingerprint != challenge.workspace_public_key_fingerprint - || response.workspace_trust_id != challenge.workspace_trust_id - || response.runtime_public_key_fingerprint != challenge.runtime_public_key_fingerprint - || response.workspace_nonce != challenge.workspace_nonce - || response.runtime_nonce.is_empty() - { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - Ok(()) -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct WorkspaceCapabilityClaims { @@ -965,8 +309,6 @@ pub struct WorkspaceCapabilityClaims { pub issuer_workspace_id: String, pub issuer_key_id: String, pub issuer_public_key_fingerprint: String, - pub trust_id: String, - pub binding_id: String, pub runtime_id: String, #[serde(skip_serializing_if = "Option::is_none")] pub worker_id: Option, @@ -982,7 +324,6 @@ pub struct WorkspaceCapabilityClaims { #[derive(Clone, Debug, PartialEq, Eq)] pub struct WorkspaceCapabilityExpectation<'a> { pub workspace_id: &'a str, - pub binding_id: &'a str, pub runtime_id: &'a str, pub worker_id: Option<&'a str>, pub operation: &'a str, @@ -998,8 +339,6 @@ pub struct VerifiedWorkspaceCapability { pub workspace_id: String, pub issuer_key_id: String, pub issuer_public_key_fingerprint: String, - pub trust_id: String, - pub binding_id: String, pub runtime_id: String, pub worker_id: Option, pub operation: String, @@ -1009,11 +348,16 @@ pub struct VerifiedWorkspaceCapability { pub expires_at: i64, } +/// Replay scope is Runtime-owned Workspace + trusted key material, not sender claims +/// or administrative enrollment IDs. Re-enrollment (including rotate away/back) +/// cannot reset consumed tokens. Fresh unconsumed tokens under the same reaccepted +/// key remain valid until expiry; revoked trust denies all requests. +/// A genuinely different trusted key has an independent token namespace. pub trait WorkspaceClaimReplayProtection: Send + Sync { fn consume_once( &self, workspace_id: &str, - trust_id: &str, + key_fingerprint: &str, token_id: &str, expires_at: i64, now_unix: i64, @@ -1029,7 +373,7 @@ impl WorkspaceClaimReplayProtection for InMemoryWorkspaceClaimReplayProtection { fn consume_once( &self, workspace_id: &str, - trust_id: &str, + key_fingerprint: &str, token_id: &str, expires_at: i64, now_unix: i64, @@ -1041,7 +385,7 @@ impl WorkspaceClaimReplayProtection for InMemoryWorkspaceClaimReplayProtection { consumed.retain(|_, expiry| *expiry > now_unix); let key = ( workspace_id.to_string(), - trust_id.to_string(), + key_fingerprint.to_string(), token_id.to_string(), ); if consumed.contains_key(&key) { @@ -1072,11 +416,14 @@ struct WorkspaceClaimReplayDocument { #[serde(deny_unknown_fields)] struct WorkspaceClaimReplayEntry { workspace_id: String, - trust_id: String, + // None occurs only when migrating old saved replay scopes: conservatively + // fence the token across every key for that Workspace until its expiry. + key_fingerprint: Option, token_id: String, expires_at: i64, } +// Frozen saved-data formats only. Neither format is accepted on the network. #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct LegacyReplayDocument { @@ -1085,13 +432,26 @@ struct LegacyReplayDocument { } #[derive(Deserialize)] #[serde(deny_unknown_fields)] -#[allow(dead_code)] struct LegacyReplayEntry { workspace_id: String, trust_generation: u64, token_id: String, expires_at: i64, } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct EnrollmentReplayDocument { + version: u32, + entries: Vec, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct EnrollmentReplayEntry { + workspace_id: String, + trust_id: String, + token_id: String, + expires_at: i64, +} impl FileWorkspaceClaimReplayProtection { pub fn new(path: impl Into) -> Self { @@ -1107,19 +467,72 @@ impl FileWorkspaceClaimReplayProtection { let raw: serde_json::Value = serde_json::from_slice(&bytes).map_err(|_| { WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable })?; - if raw.get("version").and_then(serde_json::Value::as_u64) == Some(1) { - let legacy: LegacyReplayDocument = - serde_json::from_value(raw).map_err(|_| { - WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable - })?; - if legacy.version != 1 || legacy.entries.len() > MAX_REPLAY_ENTRIES { - return Err( - WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable, - ); + let version = raw.get("version").and_then(serde_json::Value::as_u64); + let entries = match version { + Some(1) => { + let legacy: LegacyReplayDocument = serde_json::from_value(raw.clone()) + .map_err(|_| { + WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable + })?; + if legacy.version != 1 + || legacy.entries.len() > MAX_REPLAY_ENTRIES + || legacy + .entries + .iter() + .any(|entry| entry.trust_generation == 0) + { + return Err( + WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable, + ); + } + Some( + legacy + .entries + .into_iter() + .map(|entry| WorkspaceClaimReplayEntry { + workspace_id: entry.workspace_id, + key_fingerprint: None, + token_id: entry.token_id, + expires_at: entry.expires_at, + }) + .collect(), + ) } + Some(2) => { + let legacy: EnrollmentReplayDocument = serde_json::from_value(raw.clone()) + .map_err(|_| { + WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable + })?; + if legacy.version != 2 + || legacy.entries.len() > MAX_REPLAY_ENTRIES + || legacy + .entries + .iter() + .any(|entry| validate_id(&entry.trust_id).is_err()) + { + return Err( + WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable, + ); + } + Some( + legacy + .entries + .into_iter() + .map(|entry| WorkspaceClaimReplayEntry { + workspace_id: entry.workspace_id, + key_fingerprint: None, + token_id: entry.token_id, + expires_at: entry.expires_at, + }) + .collect(), + ) + } + _ => None, + }; + if let Some(entries) = entries { let migrated = WorkspaceClaimReplayDocument { - version: 2, - entries: Vec::new(), + version: 3, + entries, }; self.write(&migrated)?; return Ok(migrated); @@ -1128,14 +541,14 @@ impl FileWorkspaceClaimReplayProtection { serde_json::from_value(raw).map_err(|_| { WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable })?; - if document.version != 2 || document.entries.len() > MAX_REPLAY_ENTRIES { + if document.version != 3 || document.entries.len() > MAX_REPLAY_ENTRIES { return Err(WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable); } Ok(document) } Err(error) if error.kind() == std::io::ErrorKind::NotFound => { Ok(WorkspaceClaimReplayDocument { - version: 2, + version: 3, entries: Vec::new(), }) } @@ -1147,7 +560,13 @@ impl FileWorkspaceClaimReplayProtection { &self, document: &WorkspaceClaimReplayDocument, ) -> Result<(), WorkspaceCapabilityVerificationError> { - let parent = self.path.parent().unwrap_or_else(|| Path::new(".")); + use std::io::Write as _; + + let parent = self + .path + .parent() + .filter(|parent| !parent.as_os_str().is_empty()) + .unwrap_or_else(|| Path::new(".")); fs::create_dir_all(parent) .map_err(|_| WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable)?; let bytes = serde_json::to_vec(document) @@ -1156,19 +575,30 @@ impl FileWorkspaceClaimReplayProtection { ".workspace-claim-replay-{}.tmp", uuid::Uuid::now_v7() )); - fs::write(&temporary, bytes) - .map_err(|_| WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt as _; - fs::set_permissions(&temporary, fs::Permissions::from_mode(0o600)) - .map_err(|_| WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable)?; - } - fs::rename(&temporary, &self.path).map_err(|_| { + let result = (|| -> std::io::Result<()> { + let mut options = fs::OpenOptions::new(); + options.create_new(true).write(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt as _; + options.mode(0o600); + } + let mut file = options.open(&temporary)?; + file.write_all(&bytes)?; + // A successful consumption must survive a crash: persist the file + // contents before publication, then persist the atomic rename. + file.sync_all()?; + fs::rename(&temporary, &self.path)?; + fs::File::open(parent)?.sync_all()?; + Ok(()) + })(); + if result.is_err() { + // Before rename, remove the unpublished temporary file. After rename + // (e.g. a directory-sync failure), retain the consumed-token record + // but reject authorization; never roll replay protection back. let _ = fs::remove_file(&temporary); - WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable - })?; - Ok(()) + } + result.map_err(|_| WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable) } } @@ -1176,7 +606,7 @@ impl WorkspaceClaimReplayProtection for FileWorkspaceClaimReplayProtection { fn consume_once( &self, workspace_id: &str, - trust_id: &str, + key_fingerprint: &str, token_id: &str, expires_at: i64, now_unix: i64, @@ -1189,7 +619,10 @@ impl WorkspaceClaimReplayProtection for FileWorkspaceClaimReplayProtection { document.entries.retain(|entry| entry.expires_at > now_unix); if document.entries.iter().any(|entry| { entry.workspace_id == workspace_id - && entry.trust_id == trust_id + && entry + .key_fingerprint + .as_deref() + .is_none_or(|key| key == key_fingerprint) && entry.token_id == token_id }) { return Ok(false); @@ -1199,7 +632,7 @@ impl WorkspaceClaimReplayProtection for FileWorkspaceClaimReplayProtection { } document.entries.push(WorkspaceClaimReplayEntry { workspace_id: workspace_id.to_string(), - trust_id: trust_id.to_string(), + key_fingerprint: Some(key_fingerprint.to_string()), token_id: token_id.to_string(), expires_at, }); @@ -1228,9 +661,6 @@ impl WorkspaceCapabilityVerifier { records: Vec, replay: Arc, ) -> Result { - if records.is_empty() { - return Err(WorkspaceCapabilityVerificationError::TrustAuthorityMissing); - } validate_workspace_issuer_trust_records(&records)?; Ok(Self { records: records.into(), @@ -1274,9 +704,6 @@ impl WorkspaceCapabilityVerifier { if record.backend_url != claims.issuer { return Err(WorkspaceCapabilityVerificationError::WrongIssuer); } - if record.trust_id != claims.trust_id { - return Err(WorkspaceCapabilityVerificationError::WrongTrustIdentity); - } if record.key_id != claims.issuer_key_id { return Err(WorkspaceCapabilityVerificationError::WrongIssuerKey); } @@ -1300,9 +727,6 @@ impl WorkspaceCapabilityVerifier { if claims.runtime_id != expected.runtime_id { return Err(WorkspaceCapabilityVerificationError::WrongRuntime); } - if claims.binding_id != expected.binding_id { - return Err(WorkspaceCapabilityVerificationError::WrongBindingIdentity); - } if claims.worker_id.as_deref() != expected.worker_id { return Err(WorkspaceCapabilityVerificationError::WrongWorker); } @@ -1331,7 +755,7 @@ impl WorkspaceCapabilityVerifier { } if !self.replay.consume_once( &claims.issuer_workspace_id, - &claims.trust_id, + &record.public_key_fingerprint, &claims.jti, claims.exp, expected.now_unix, @@ -1344,8 +768,6 @@ impl WorkspaceCapabilityVerifier { workspace_id: claims.issuer_workspace_id, issuer_key_id: claims.issuer_key_id, issuer_public_key_fingerprint: claims.issuer_public_key_fingerprint, - trust_id: claims.trust_id, - binding_id: claims.binding_id, runtime_id: claims.runtime_id, worker_id: claims.worker_id, operation: claims.operation, @@ -1359,8 +781,6 @@ impl WorkspaceCapabilityVerifier { #[derive(Debug, thiserror::Error, PartialEq, Eq)] pub enum WorkspaceCapabilityVerificationError { - #[error("Workspace issuer trust authority is not configured")] - TrustAuthorityMissing, #[error("Workspace issuer trust contains duplicate Workspace identities")] DuplicateWorkspaceTrust, #[error("Workspace issuer trust contains too many records")] @@ -1381,8 +801,6 @@ pub enum WorkspaceCapabilityVerificationError { WrongIssuer, #[error("Workspace capability issuer trust is revoked")] IssuerRevoked, - #[error("Workspace capability trust identity does not match current trust")] - WrongTrustIdentity, #[error("Workspace capability issuer key does not match trust")] WrongIssuerKey, #[error("Workspace capability signature is invalid")] @@ -1391,8 +809,6 @@ pub enum WorkspaceCapabilityVerificationError { WrongWorkspace, #[error("Workspace capability targets another Runtime")] WrongRuntime, - #[error("Workspace capability binding identity does not match the verified binding")] - WrongBindingIdentity, #[error("Workspace capability targets another Worker")] WrongWorker, #[error("Workspace capability does not authorize this operation")] @@ -1413,12 +829,6 @@ pub enum WorkspaceCapabilityVerificationError { Replay, #[error("Workspace capability replay authority is unavailable")] ReplayAuthorityUnavailable, - #[error("Workspace Runtime verification authority is unavailable")] - VerificationAuthorityUnavailable, - #[error( - "Workspace Runtime verification challenge or response does not match current authority" - )] - VerificationChallengeMismatch, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -1500,77 +910,6 @@ fn validate_trust_record( Ok(()) } -fn validate_verification_record( - record: &WorkspaceRuntimeVerificationRecord, -) -> Result<(), WorkspaceCapabilityVerificationError> { - for value in [ - record.workspace_id.as_str(), - record.runtime_id.as_str(), - record.workspace_key_id.as_str(), - record.binding_id.as_str(), - record.workspace_trust_id.as_str(), - ] { - if value.is_empty() - || value.len() > MAX_ID_BYTES - || value.trim() != value - || !value.bytes().all(|byte| { - byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':') - }) - { - return Err(WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable); - } - } - if record.binding_id.is_empty() - || record.workspace_public_key_fingerprint.is_empty() - || record.workspace_trust_id.is_empty() - || !is_key_fingerprint(&record.workspace_public_key_fingerprint) - || !is_key_fingerprint(&record.runtime_public_key_fingerprint) - || record.verified_at <= 0 - || record.runtime_public_key_fingerprint.len() > 128 - || !record.runtime_public_key_fingerprint.starts_with("sha256:") - { - return Err(WorkspaceCapabilityVerificationError::VerificationAuthorityUnavailable); - } - Ok(()) -} - -fn validate_verification_challenge( - challenge: &WorkspaceRuntimeVerificationChallenge, -) -> Result<(), WorkspaceCapabilityVerificationError> { - for value in [ - challenge.challenge_id.as_str(), - challenge.workspace_id.as_str(), - challenge.runtime_id.as_str(), - challenge.workspace_key_id.as_str(), - challenge.binding_id.as_str(), - challenge.workspace_trust_id.as_str(), - challenge.workspace_nonce.as_str(), - ] { - if value.is_empty() - || value.len() > MAX_ID_BYTES - || value.trim() != value - || !value.bytes().all(|byte| { - byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':') - }) - { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - } - if challenge.binding_id.is_empty() - || challenge.workspace_public_key_fingerprint.is_empty() - || challenge.workspace_trust_id.is_empty() - || !is_key_fingerprint(&challenge.workspace_public_key_fingerprint) - || !is_key_fingerprint(&challenge.runtime_public_key_fingerprint) - || challenge.runtime_public_key_fingerprint.len() > 128 - || !challenge - .runtime_public_key_fingerprint - .starts_with("sha256:") - { - return Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch); - } - Ok(()) -} - fn validate_claim_shape( claims: &WorkspaceCapabilityClaims, ) -> Result<(), WorkspaceCapabilityVerificationError> { @@ -1594,8 +933,6 @@ fn validate_claim_shape( for value in [ claims.issuer_workspace_id.as_str(), claims.issuer_key_id.as_str(), - claims.binding_id.as_str(), - claims.trust_id.as_str(), claims.runtime_id.as_str(), claims.jti.as_str(), ] { @@ -1620,10 +957,7 @@ fn validate_claim_shape( return Err(WorkspaceCapabilityVerificationError::InvalidIdentifier); } } - if !is_key_fingerprint(&claims.issuer_public_key_fingerprint) - || claims.trust_id.is_empty() - || claims.binding_id.is_empty() - { + if !is_key_fingerprint(&claims.issuer_public_key_fingerprint) { return Err(WorkspaceCapabilityVerificationError::MalformedClaims); } if claims.operation.is_empty() @@ -1713,8 +1047,6 @@ mod tests { issuer_workspace_id: record.workspace_id.clone(), issuer_key_id: record.key_id.clone(), issuer_public_key_fingerprint: record.public_key_fingerprint.clone(), - trust_id: record.trust_id.clone(), - binding_id: "binding-test".into(), runtime_id: "runtime-1".to_string(), worker_id: Some("worker-1".to_string()), operation: "worker.submit".to_string(), @@ -1730,7 +1062,6 @@ mod tests { fn expectation<'a>(body_digest: &'a str) -> WorkspaceCapabilityExpectation<'a> { WorkspaceCapabilityExpectation { workspace_id: "workspace-1", - binding_id: "binding-test", runtime_id: "runtime-1", worker_id: Some("worker-1"), operation: "worker.submit", @@ -1782,7 +1113,7 @@ mod tests { } #[test] - fn verifier_binds_workspace_key_trust_identity_runtime_worker_operation_and_body() { + fn verifier_binds_workspace_key_runtime_worker_operation_and_body() { let (key_1, bundle_1) = identity("workspace-1", "WK-1"); let (key_2, bundle_2) = identity("workspace-2", "WK-2"); let record_1 = @@ -1815,7 +1146,6 @@ mod tests { ); let workspace_2_expectation = WorkspaceCapabilityExpectation { workspace_id: "workspace-2", - binding_id: "binding-test".into(), runtime_id: "runtime-1", worker_id: Some("worker-1"), operation: "worker.submit", @@ -1857,13 +1187,6 @@ mod tests { }) as ClaimsMutation, WorkspaceCapabilityVerificationError::WrongWorker, ), - ( - "binding", - (|claims: &mut WorkspaceCapabilityClaims| { - claims.binding_id = "other-binding".into() - }) as ClaimsMutation, - WorkspaceCapabilityVerificationError::WrongBindingIdentity, - ), ( "issuer", (|claims: &mut WorkspaceCapabilityClaims| { @@ -1914,7 +1237,7 @@ mod tests { } #[test] - fn verifier_rejects_replay_revocation_old_trust_expiry_and_future_claims() { + fn verifier_rejects_replay_revocation_expiry_and_future_claims() { let (key, bundle) = identity("workspace-1", "WK-1"); let record = WorkspaceIssuerTrustRecord::from_bundle(bundle, "trust-test".into(), 1).unwrap(); @@ -1928,14 +1251,6 @@ mod tests { WorkspaceCapabilityVerificationError::Replay ); - let mut stale = claims(&record, "stale"); - stale.trust_id = "retired-trust".into(); - let token = issue_workspace_capability_token(&key, &stale).unwrap(); - assert_eq!( - verifier.verify(&token, &expectation(&body)).unwrap_err(), - WorkspaceCapabilityVerificationError::WrongTrustIdentity - ); - let mut expired = claims(&record, "expired"); expired.iat = 900; expired.exp = 950; @@ -1972,96 +1287,323 @@ mod tests { } #[test] - fn reaccepting_the_same_key_does_not_reactivate_old_capabilities() { + fn same_workspace_key_reenrollment_and_rotation_back_do_not_reset_durable_replay() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("replay.json"); let (key, bundle) = identity("workspace-1", "WK-1"); let mut records = Vec::new(); - let (_, first) = add_workspace_issuer_trust(&mut records, bundle.clone(), 10).unwrap(); - let token = issue_workspace_capability_token(&key, &claims(&first, "old-token")).unwrap(); - revoke_workspace_issuer_trust(&mut records, "workspace-1", 11).unwrap(); - let (_, reaccepted) = replace_workspace_issuer_trust(&mut records, bundle, 12).unwrap(); - assert_ne!(first.trust_id, reaccepted.trust_id); + let (_, first) = add_workspace_issuer_trust(&mut records, bundle.clone(), 1).unwrap(); + let body = workspace_request_body_digest(br#"{"content":"hello"}"#); + let token = issue_workspace_capability_token(&key, &claims(&first, "consumed")).unwrap(); + WorkspaceCapabilityVerifier::new( + records.clone(), + Arc::new(FileWorkspaceClaimReplayProtection::new(&path)), + ) + .unwrap() + .verify(&token, &expectation(&body)) + .unwrap(); + revoke_workspace_issuer_trust(&mut records, "workspace-1", 2).unwrap(); + replace_workspace_issuer_trust(&mut records, bundle.clone(), 3).unwrap(); + for reenroll in [true, false] { + if !reenroll { + let (_, other) = identity("workspace-1", "WK-2"); + replace_workspace_issuer_trust(&mut records, other, 4).unwrap(); + replace_workspace_issuer_trust(&mut records, bundle.clone(), 5).unwrap(); + } + assert_ne!(first.trust_id, records[0].trust_id); + let verifier = WorkspaceCapabilityVerifier::new( + records.clone(), + Arc::new(FileWorkspaceClaimReplayProtection::new(&path)), + ) + .unwrap(); + assert_eq!( + verifier.verify(&token, &expectation(&body)), + Err(WorkspaceCapabilityVerificationError::Replay) + ); + } + // Reaccepting the same actual key authorizes fresh, unconsumed short-lived + // requests; there is no sender enrollment ID fence or handshake status. + let token = issue_workspace_capability_token(&key, &claims(&first, "fresh")).unwrap(); + WorkspaceCapabilityVerifier::new( + records, + Arc::new(FileWorkspaceClaimReplayProtection::new(&path)), + ) + .unwrap() + .verify(&token, &expectation(&body)) + .unwrap(); + } + + #[test] + fn rotated_trust_accepts_only_requests_verified_with_the_current_key() { + let (old_key, old_bundle) = identity("workspace-1", "WK-1"); + let (new_key, new_bundle) = identity("workspace-1", "WK-2"); + let mut records = Vec::new(); + let (_, old) = add_workspace_issuer_trust(&mut records, old_bundle, 1).unwrap(); + let (_, current) = replace_workspace_issuer_trust(&mut records, new_bundle, 2).unwrap(); let verifier = WorkspaceCapabilityVerifier::new( records, Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), ) .unwrap(); let body = workspace_request_body_digest(br#"{"content":"hello"}"#); + let old_request = issue_workspace_capability_token(&old_key, &claims(&old, "old")).unwrap(); assert_eq!( - verifier.verify(&token, &expectation(&body)).unwrap_err(), - WorkspaceCapabilityVerificationError::WrongTrustIdentity + verifier.verify(&old_request, &expectation(&body)), + Err(WorkspaceCapabilityVerificationError::WrongIssuerKey) ); + let forged_current = + issue_workspace_capability_token(&old_key, &claims(¤t, "current")).unwrap(); + assert_eq!( + verifier.verify(&forged_current, &expectation(&body)), + Err(WorkspaceCapabilityVerificationError::InvalidSignature) + ); + // Failed verification does not consume jti; actual current-key verification does. + let current_request = + issue_workspace_capability_token(&new_key, &claims(¤t, "current")).unwrap(); + verifier + .verify(¤t_request, &expectation(&body)) + .unwrap(); } #[test] - fn frozen_enrollment_migration_requires_explicit_reacceptance() { - let (_, bundle) = identity("workspace-1", "WK-1"); - let record = - WorkspaceIssuerTrustRecord::from_bundle(bundle, "trust-test".into(), 10).unwrap(); - for state in ["active", "revoked"] { - let mut legacy = serde_json::to_value(&record).unwrap(); - legacy.as_object_mut().unwrap().remove("trust_id"); - legacy["identity_revision"] = serde_json::json!(1); - legacy["trust_generation"] = serde_json::json!(2); - legacy["state"] = serde_json::json!(state); - assert!(serde_json::from_value::(legacy.clone()).is_err()); - let migrated = migrate_legacy_workspace_issuer_trust_record(legacy).unwrap(); - assert_eq!(migrated.state, WorkspaceIssuerTrustState::Revoked); - assert_eq!(migrated.public_bundle(), record.public_bundle()); + fn saved_replay_migration_preserves_consumed_tokens_across_unknown_key_scopes() { + for version in [1, 2] { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("replay.json"); + let mut entry = serde_json::json!({"workspace_id":"workspace-a", "token_id":"token-a", "expires_at":1000}); + if version == 1 { + entry["trust_generation"] = serde_json::json!(1); + } else { + entry["trust_id"] = serde_json::json!("old-enrollment"); + } + fs::write( + &path, + serde_json::to_vec(&serde_json::json!({"version":version,"entries":[entry]})) + .unwrap(), + ) + .unwrap(); + let replay = FileWorkspaceClaimReplayProtection::new(&path); + assert!( + !replay + .consume_once("workspace-a", "key-a", "token-a", 1000, 100) + .unwrap() + ); + // Reconstruction must also retain the old consumed token, not reset it. + let replay = FileWorkspaceClaimReplayProtection::new(&path); + assert!( + !replay + .consume_once("workspace-a", "key-b", "token-a", 1000, 101) + .unwrap() + ); + let saved: serde_json::Value = + serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + assert_eq!(saved["version"], 3); + assert_eq!(saved["entries"][0]["token_id"], "token-a"); + assert!( + replay + .consume_once("workspace-a", "key-a", "fresh", 1000, 101) + .unwrap() + ); } } #[test] - fn frozen_verification_file_is_invalidated_durably_without_losing_archived_evidence() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("verification.json"); - fs::write(&path, serde_json::to_vec(&serde_json::json!({"version":1,"records":[{ - "workspace_id":"workspace-1","runtime_id":"runtime-1","binding_revision":1, - "workspace_key_id":"WK-1","workspace_identity_revision":1,"workspace_trust_generation":1, - "runtime_public_key_fingerprint":"sha256:old","runtime_identity_revision":1,"verified_at":10 - }]})).unwrap()).unwrap(); - let original_bytes = fs::read(&path).unwrap(); - let authority = FileWorkspaceRuntimeVerificationAuthority::new(&path); - assert!(authority.get("workspace-1", "runtime-1").unwrap().is_none()); - let migrated: serde_json::Value = - serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); - assert_eq!(migrated["version"], 2); - assert_eq!(migrated["records"], serde_json::json!([])); + fn current_network_claims_reject_handshake_ids_and_self_reported_public_keys() { + let (key, bundle) = identity("workspace-1", "WK-1"); + let record = + WorkspaceIssuerTrustRecord::from_bundle(bundle, "internal-trust".into(), 1).unwrap(); + let wire = serde_json::to_value(claims(&record, "token")).unwrap(); + assert!(wire.get("trust_id").is_none()); + assert!(wire.get("binding_id").is_none()); + for obsolete in ["binding_id", "trust_id", "public_key"] { + let mut changed = wire.clone(); + changed[obsolete] = serde_json::json!("sender-authority"); + let token = crate::auth::sign_json_token( + WORKSPACE_TOKEN_PREFIX, + WORKSPACE_SIGNING_INPUT_PREFIX, + &key, + &changed, + ) + .unwrap(); + assert_eq!( + inspect_workspace_capability_claims(&token), + Err(WorkspaceCapabilityVerificationError::MalformedToken) + ); + let verifier = WorkspaceCapabilityVerifier::new( + vec![record.clone()], + Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), + ) + .unwrap(); + assert_eq!( + verifier.verify(&token, &expectation(&claims(&record, "token").body_digest)), + Err(WorkspaceCapabilityVerificationError::MalformedToken) + ); + } + } + + fn legacy_enrollment(record: &WorkspaceIssuerTrustRecord) -> serde_json::Value { + let mut legacy = serde_json::to_value(&record).unwrap(); + legacy.as_object_mut().unwrap().remove("trust_id"); + legacy["identity_revision"] = serde_json::json!(1); + legacy["trust_generation"] = serde_json::json!(2); + legacy + } + + #[test] + fn legacy_enrollment_migration_preserves_trust_permission_for_actual_signed_ping() { + let (key, bundle) = identity("workspace-1", "WK-1"); + let mut record = + WorkspaceIssuerTrustRecord::from_bundle(bundle, "trust-test".into(), 10).unwrap(); + record.updated_at_unix = 20; + for state in [ + WorkspaceIssuerTrustState::Active, + WorkspaceIssuerTrustState::Revoked, + ] { + record.state = state; + let legacy = legacy_enrollment(&record); + assert!(serde_json::from_value::(legacy.clone()).is_err()); + let migrated = migrate_legacy_workspace_issuer_trust_record(legacy).unwrap(); + assert_eq!(migrated.state, state); + assert_eq!(migrated.public_bundle(), record.public_bundle()); + assert_eq!(migrated.registered_at_unix, record.registered_at_unix); + assert_eq!(migrated.updated_at_unix, record.updated_at_unix); + assert!(!migrated.trust_id.is_empty()); + let verifier = WorkspaceCapabilityVerifier::new( + vec![migrated], + Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), + ) + .unwrap(); + let request = WorkspaceCapabilityClaims { + worker_id: None, + operation: "runtime:ping".into(), + method: "GET".into(), + path_and_query: "/v1/ping".into(), + body_digest: workspace_request_body_digest(&[]), + ..claims(&record, "ordinary-ping") + }; + let token = issue_workspace_capability_token(&key, &request).unwrap(); + let expected = WorkspaceCapabilityExpectation { + worker_id: None, + operation: &request.operation, + method: "GET", + path_and_query: "/v1/ping", + ..expectation(&request.body_digest) + }; + let result = verifier.verify(&token, &expected); + if state == WorkspaceIssuerTrustState::Active { + assert_eq!(result.unwrap().workspace_id, "workspace-1"); + } else { + assert_eq!( + result, + Err(WorkspaceCapabilityVerificationError::IssuerRevoked) + ); + } + } + } + + #[test] + fn malformed_legacy_enrollment_is_rejected_instead_of_becoming_trusted() { + let (_, bundle) = identity("workspace-1", "WK-1"); + let record = + WorkspaceIssuerTrustRecord::from_bundle(bundle, "trust-test".into(), 10).unwrap(); + for (field, value, error) in [ + ( + "workspace_id", + serde_json::json!(""), + WorkspaceIssuerTrustError::InvalidIdentifier, + ), + ( + "backend_url", + serde_json::json!("file:///untrusted"), + WorkspaceIssuerTrustError::InvalidBackendUrl, + ), + ( + "algorithm", + serde_json::json!("none"), + WorkspaceIssuerTrustError::UnsupportedAlgorithm, + ), + ( + "public_key", + serde_json::json!("invalid"), + WorkspaceIssuerTrustError::InvalidPublicKey, + ), + ( + "public_key_fingerprint", + serde_json::json!(format!("sha256:{}", "0".repeat(64))), + WorkspaceIssuerTrustError::PublicKeyFingerprintMismatch, + ), + ( + "state", + serde_json::json!("invalid"), + WorkspaceIssuerTrustError::InvalidIdentifier, + ), + ( + "unexpected", + serde_json::json!(true), + WorkspaceIssuerTrustError::InvalidIdentifier, + ), + ] { + let mut legacy = legacy_enrollment(&record); + legacy[field] = value; + assert_eq!( + migrate_legacy_workspace_issuer_trust_record(legacy), + Err(error), + "{field}" + ); + } + let mut missing = legacy_enrollment(&record); + missing.as_object_mut().unwrap().remove("state"); assert_eq!( - migrated["archived_legacy_document"] - .as_str() - .unwrap() - .as_bytes(), - original_bytes + migrate_legacy_workspace_issuer_trust_record(missing), + Err(WorkspaceIssuerTrustError::InvalidIdentifier) ); - assert!( - FileWorkspaceRuntimeVerificationAuthority::new(&path) - .get("workspace-1", "runtime-1") - .unwrap() - .is_none() - ); - let current = WorkspaceRuntimeVerificationRecord { - workspace_id: "workspace-1".into(), - runtime_id: "runtime-1".into(), - binding_id: "accepted-binding".into(), - workspace_key_id: "WK-1".into(), - workspace_public_key_fingerprint: format!("sha256:{}", "a".repeat(64)), - workspace_trust_id: "accepted-trust".into(), - runtime_public_key_fingerprint: format!("sha256:{}", "b".repeat(64)), - verified_at: 20, - }; - authority.record(current.clone()).unwrap(); - let restored = FileWorkspaceRuntimeVerificationAuthority::new(&path); + } + + #[test] + fn failed_replay_rename_preserves_destination_and_cleans_temporary_file() { + let temporary = tempfile::tempdir().unwrap(); + let path = temporary.path().join("workspace-replay.json"); + // Replacing a directory with the temporary regular file fails on real I/O, + // after the file write/sync. No permission assumptions or fault hooks. + fs::create_dir(&path).unwrap(); + fs::write(path.join("existing"), "preserve me").unwrap(); + let replay = FileWorkspaceClaimReplayProtection::new(&path); assert_eq!( - restored.get("workspace-1", "runtime-1").unwrap(), - Some(current) + replay.write(&WorkspaceClaimReplayDocument { + version: 3, + entries: Vec::new(), + }), + Err(WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable) ); - let updated: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); assert_eq!( - updated["archived_legacy_document"] - .as_str() - .unwrap() - .as_bytes(), - original_bytes + fs::read_to_string(path.join("existing")).unwrap(), + "preserve me" + ); + let remaining = fs::read_dir(temporary.path()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .collect::>(); + assert_eq!(remaining, vec![path]); + } + + #[test] + fn unavailable_replay_file_rejects_otherwise_valid_signed_request() { + let temporary = tempfile::tempdir().unwrap(); + let path = temporary.path().join("workspace-replay.json"); + fs::create_dir(&path).unwrap(); + let (key, bundle) = identity("workspace-1", "WK-1"); + let record = + WorkspaceIssuerTrustRecord::from_bundle(bundle, "trust-test".into(), 1).unwrap(); + let request = claims(&record, "token"); + let token = issue_workspace_capability_token(&key, &request).unwrap(); + let verifier = WorkspaceCapabilityVerifier::new( + vec![record], + Arc::new(FileWorkspaceClaimReplayProtection::new(path)), + ) + .unwrap(); + assert_eq!( + verifier.verify(&token, &expectation(&request.body_digest)), + Err(WorkspaceCapabilityVerificationError::ReplayAuthorityUnavailable) ); } @@ -2072,119 +1614,19 @@ mod tests { let first = FileWorkspaceClaimReplayProtection::new(&path); assert!( first - .consume_once("workspace-a", "trust-a", "token-a", 1_000, 100) + .consume_once("workspace-a", "key-a", "token-a", 1_000, 100) .unwrap() ); let restored = FileWorkspaceClaimReplayProtection::new(&path); assert!( !restored - .consume_once("workspace-a", "trust-a", "token-a", 1_000, 101) + .consume_once("workspace-a", "key-a", "token-a", 1_000, 101) .unwrap() ); assert!( restored - .consume_once("workspace-a", "trust-b", "token-a", 1_000, 101) + .consume_once("workspace-a", "key-b", "token-a", 1_000, 101) .unwrap() ); } - - #[test] - fn file_runtime_verification_authority_survives_reconstruction() { - let temporary = tempfile::tempdir().unwrap(); - let path = temporary.path().join("workspace-verifications.json"); - let authority = FileWorkspaceRuntimeVerificationAuthority::new(&path); - let record = WorkspaceRuntimeVerificationRecord { - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-a".to_string(), - binding_id: "binding-test".into(), - workspace_key_id: "WK-a".to_string(), - workspace_public_key_fingerprint: format!("sha256:{}", "a".repeat(64)), - workspace_trust_id: "trust-test".into(), - runtime_public_key_fingerprint: format!("sha256:{}", "b".repeat(64)), - verified_at: 100, - }; - authority.record(record.clone()).unwrap(); - let restored = FileWorkspaceRuntimeVerificationAuthority::new(&path); - assert_eq!( - restored.get("workspace-a", "runtime-a").unwrap(), - Some(record) - ); - } - - #[test] - fn verification_response_binds_the_exact_challenge_and_runtime_identity() { - let runtime_identity = RuntimeIdentityMaterial::generate("runtime-1").unwrap(); - let signer = RuntimeVerificationSigner::from_identity(&runtime_identity).unwrap(); - let public_key_fingerprint = format!( - "sha256:{}", - hex_lower(&Sha256::digest( - crate::auth::decode_public_key(&runtime_identity.public_key).unwrap() - )) - ); - let challenge = WorkspaceRuntimeVerificationChallenge { - challenge_id: "challenge-1".to_string(), - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-1".to_string(), - binding_id: "binding-test".into(), - workspace_key_id: "WK-1".to_string(), - workspace_public_key_fingerprint: format!("sha256:{}", "a".repeat(64)), - workspace_trust_id: "trust-test".into(), - runtime_public_key_fingerprint: public_key_fingerprint, - workspace_nonce: "workspace-nonce".to_string(), - expires_at: 1_100, - }; - let response = signer - .sign_response(&challenge, "runtime-nonce".to_string(), 1_000) - .unwrap(); - verify_runtime_verification_response( - &response, - &challenge, - &runtime_identity.public_key, - 1_001, - ) - .unwrap(); - - // A valid signature from a different key must not authenticate the - // fingerprint named by this challenge, even when the response repeats it. - let other_identity = RuntimeIdentityMaterial::generate("runtime-1").unwrap(); - let mut wrong_key = response.clone(); - wrong_key.response_proof = crate::auth::sign_json_token( - RUNTIME_VERIFICATION_TOKEN_PREFIX, - RUNTIME_VERIFICATION_SIGNING_INPUT_PREFIX, - &other_identity.signing_key().unwrap(), - &RuntimeVerificationResponseClaims { - response: response.unsigned(), - method: "POST".into(), - path_and_query: WORKSPACE_VERIFICATION_CHALLENGE_PATH.into(), - body_digest: workspace_request_body_digest( - &serde_json::to_vec(&challenge).unwrap(), - ), - iat: 1_000, - exp: challenge.expires_at, - jti: "different-key-proof".into(), - }, - ) - .unwrap(); - assert_eq!( - verify_runtime_verification_response( - &wrong_key, - &challenge, - &other_identity.public_key, - 1_001, - ), - Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch) - ); - - let mut tampered = response.clone(); - tampered.binding_id = "other-binding".into(); - assert_eq!( - verify_runtime_verification_response( - &tampered, - &challenge, - &runtime_identity.public_key, - 1_001, - ), - Err(WorkspaceCapabilityVerificationError::VerificationChallengeMismatch) - ); - } } diff --git a/crates/worker/src/compact/worker.rs b/crates/worker/src/compact/worker.rs index 2e450a2fd..218948e42 100644 --- a/crates/worker/src/compact/worker.rs +++ b/crates/worker/src/compact/worker.rs @@ -379,6 +379,7 @@ pub(crate) struct CompactWorkerInterceptor { pub on_warning: Option>, warning_sent: AtomicBool, last_remaining_tokens: AtomicU64, + request_shape: Option<(String, agen::tool_server::ToolServerHandle)>, } impl CompactWorkerInterceptor { @@ -397,9 +398,21 @@ impl CompactWorkerInterceptor { on_warning, warning_sent: AtomicBool::new(false), last_remaining_tokens: AtomicU64::new(max_input_tokens), + request_shape: None, } } + pub(crate) fn with_request_shape( + mut self, + system_prompt: String, + tools: agen::tool_server::ToolServerHandle, + ) -> Self { + // Features install after engine configuration. This shared handle sees + // the final installed tool definitions at the actual request boundary. + self.request_shape = Some((system_prompt, tools)); + self + } + fn maybe_emit_warning(&self, remaining: u64) -> Option { let warning_threshold = self.finish_warning_remaining_tokens; let reserve_threshold = self.final_reserve_tokens; @@ -412,7 +425,12 @@ impl CompactWorkerInterceptor { let message = format!( "compact worker context budget is low ({remaining}/{} tokens remaining). \ Stop broad exploration now, read only if absolutely necessary, then call \ - `write_summary` with the final structured summary.", + `write_summary` with the final structured summary. Preserve ongoing goals and \ + user directives from the full initial summary and segment. Compress duplicate \ + explanations and old work details first, not scope, prohibitions, completion \ + conditions or exception-reporting duties. Apply explicit later changes/retractions \ + and retain uncertainty; recovery procedures and recent progress do not replace \ + the original request.", self.max_input_tokens ); if let Some(cb) = self.on_warning.as_ref() { @@ -433,17 +451,38 @@ impl Interceptor for CompactWorkerInterceptor { let context = context.items; let records = self.usage_tracker.records(); let estimate = agen::token_counter::total_tokens(context, &records); - if estimate.tokens > self.max_input_tokens { + // Before the first provider measurement, include the system and tool + // shape as well as *all* mandatory summary/input bytes. Later exact + // input measurements already include that shape; do not double charge. + let shape_tokens = if records.is_empty() { + self.request_shape.as_ref().map_or(0, |(system, tools)| { + let tool_bytes = serde_json::to_vec(&tools.tool_definitions_sorted()) + .map(|bytes| bytes.len()) + .unwrap_or(0); + estimate_tokens(system.len().saturating_add(tool_bytes)) + }) + } else { + 0 + }; + let input_tokens = estimate.tokens.saturating_add(shape_tokens); + if input_tokens > self.max_input_tokens { return Ok(PreRequestAction::Cancel(format!( - "compact worker input occupancy exceeded {} tokens", + "compact worker input occupancy (including mandatory full initial summary) exceeded {} tokens; no summary was truncated", self.max_input_tokens ))); } - let remaining = self.max_input_tokens.saturating_sub(estimate.tokens); + let remaining = self.max_input_tokens.saturating_sub(input_tokens); self.last_remaining_tokens .store(remaining, Ordering::Release); if let Some(item) = self.maybe_emit_warning(remaining) { + let warning_tokens = + agen::token_counter::total_tokens(std::slice::from_ref(&item), &[]).tokens; + if input_tokens.saturating_add(warning_tokens) > self.max_input_tokens { + return Ok(PreRequestAction::Cancel( + "compact worker input occupancy cannot fit budget warning alongside mandatory full input; no summary was truncated".into(), + )); + } self.usage_tracker.note_request(context.len() + 1); return Ok(PreRequestAction::ContinueWith(vec![item])); } @@ -542,6 +581,85 @@ mod tests { )); } + #[tokio::test] + async fn compact_worker_warning_policy_keeps_user_intent_and_fails_if_warning_cannot_fit() { + let interceptor = + CompactWorkerInterceptor::new(Arc::new(UsageTracker::new()), 100, 100, 0, None); + let mut context = vec![Item::user_message("full input")]; + let result = interceptor + .pre_llm_request(PreLlmRequestContext::<()> { + invocation: Default::default(), + items: &mut context, + history: &[], + }) + .await + .unwrap(); + assert!( + matches!(result, PreRequestAction::Cancel(message) if message.contains("no summary was truncated")) + ); + let interceptor = + CompactWorkerInterceptor::new(Arc::new(UsageTracker::new()), 1000, 1000, 0, None); + let result = interceptor + .pre_llm_request(PreLlmRequestContext::<()> { + invocation: Default::default(), + items: &mut context, + history: &[], + }) + .await + .unwrap(); + let PreRequestAction::ContinueWith(items) = result else { + panic!("expected persisted warning") + }; + let warning = items[0].as_text().unwrap(); + for policy in [ + "full initial summary and segment", + "prohibitions", + "completion conditions", + "exception-reporting duties", + "changes/retractions", + "uncertainty", + "Compress duplicate", + ] { + assert!(warning.contains(policy), "missing warning policy: {policy}"); + } + } + + #[tokio::test] + async fn compact_worker_initial_request_counts_system_shape_but_measurements_do_not_double_count() + { + let tracker = Arc::new(UsageTracker::new()); + let interceptor = CompactWorkerInterceptor::new(tracker.clone(), 100, 0, 0, None) + .with_request_shape( + "system".repeat(100), + agen::tool_server::ToolServerHandle::default(), + ); + let mut context = vec![Item::user_message("full input")]; + assert!(matches!( + interceptor + .pre_llm_request(PreLlmRequestContext::<()> { + invocation: Default::default(), + items: &mut context, + history: &[], + }) + .await + .unwrap(), + PreRequestAction::Cancel(_) + )); + tracker.note_request(context.len()); + tracker.record_usage(&make_usage(50)); + assert!(matches!( + interceptor + .pre_llm_request(PreLlmRequestContext::<()> { + invocation: Default::default(), + items: &mut context, + history: &[], + }) + .await + .unwrap(), + PreRequestAction::Continue + )); + } + #[tokio::test] async fn compact_worker_interceptor_warns_before_hard_cap() { let tracker = Arc::new(UsageTracker::new()); @@ -549,8 +667,8 @@ mod tests { let captured = warnings.clone(); let interceptor = CompactWorkerInterceptor::new( tracker.clone(), - 150, - 60, + 500, + 410, 20, Some(Arc::new(move |message| { captured.lock().unwrap().push(message); diff --git a/crates/worker/src/feature/builtin/flow_transition.rs b/crates/worker/src/feature/builtin/flow_transition.rs index 2a716d3a0..6ec10fb3b 100644 --- a/crates/worker/src/feature/builtin/flow_transition.rs +++ b/crates/worker/src/feature/builtin/flow_transition.rs @@ -560,6 +560,7 @@ where attempt.instance_id, attempt.attempt_id )), max_turns: Some(12), + compaction_budget: None, engine_configurator: None, features, required_tools: &[ diff --git a/crates/worker/src/feature/builtin/memory_lifecycle.rs b/crates/worker/src/feature/builtin/memory_lifecycle.rs index 8ef2e9e26..c3ae6a2d3 100644 --- a/crates/worker/src/feature/builtin/memory_lifecycle.rs +++ b/crates/worker/src/feature/builtin/memory_lifecycle.rs @@ -560,6 +560,7 @@ impl MemoryLifecycleTask { .extraction .worker_max_turns .or(manifest::defaults::MEMORY_EXTRACT_WORKER_MAX_TURNS), + compaction_budget: None, engine_configurator: None, features, required_tools: &[ diff --git a/crates/worker/src/feature/builtin/memory_surface_lifecycle.rs b/crates/worker/src/feature/builtin/memory_surface_lifecycle.rs index 375caa9c3..f78b451b5 100644 --- a/crates/worker/src/feature/builtin/memory_surface_lifecycle.rs +++ b/crates/worker/src/feature/builtin/memory_surface_lifecycle.rs @@ -376,6 +376,7 @@ impl SubjektivSurfaceLifecycleTask { input, cache_key: None, max_turns: Some(EDITOR_MAX_TURNS), + compaction_budget: None, engine_configurator: None, features, required_tools: &["SubmitMemorySurface"], diff --git a/crates/worker/src/internal_worker.rs b/crates/worker/src/internal_worker.rs index 13973591c..7ffc44b42 100644 --- a/crates/worker/src/internal_worker.rs +++ b/crates/worker/src/internal_worker.rs @@ -68,6 +68,7 @@ pub(crate) struct InternalWorkerSpec { ) + Send, >, >, + pub compaction_budget: Option, pub features: FeatureRegistryBuilder, pub required_tools: &'static [&'static str], pub authority: InternalWorkerAuthority, @@ -118,6 +119,7 @@ where cache_key, max_turns, engine_configurator, + compaction_budget, features, required_tools, authority, @@ -171,6 +173,7 @@ where identity: identity.clone(), history_entries: 0, })?; + worker.install_compaction_budget(compaction_budget); worker.disable_manifest_lifecycle_features(); if let Some(session) = inherited_workdir_session { worker.bind_single_workdir_session(Some(session)); @@ -666,6 +669,7 @@ pub(crate) async fn spawn_internal_worker_session( cache_key, max_turns, engine_configurator, + compaction_budget, features, required_tools, authority, @@ -701,6 +705,7 @@ pub(crate) async fn spawn_internal_worker_session( .map_err(|source| InternalWorkerSessionError::Build { message: source.to_string(), })?; + worker.install_compaction_budget(compaction_budget); worker.disable_manifest_lifecycle_features(); if let Some(session) = inherited_workdir_session { worker.bind_single_workdir_session(Some(session)); @@ -762,6 +767,7 @@ pub(crate) fn prepare_internal_worker_from_spec( cache_key, max_turns, engine_configurator, + compaction_budget, features, required_tools, authority, @@ -794,6 +800,7 @@ pub(crate) fn prepare_internal_worker_from_spec( .map_err(|source| InternalWorkerSessionError::Build { message: source.to_string(), })?; + worker.install_compaction_budget(compaction_budget); worker.disable_manifest_lifecycle_features(); if let Some(session) = inherited_workdir_session { worker.bind_single_workdir_session(Some(session)); @@ -1442,6 +1449,7 @@ permission = "write" input: "input".to_string(), cache_key: Some("internal-test".to_string()), max_turns: Some(1), + compaction_budget: None, engine_configurator: None, features: FeatureRegistryBuilder::new(), required_tools, diff --git a/crates/worker/src/ipc/interceptor.rs b/crates/worker/src/ipc/interceptor.rs index bfd713a4c..9a23e5cfe 100644 --- a/crates/worker/src/ipc/interceptor.rs +++ b/crates/worker/src/ipc/interceptor.rs @@ -74,6 +74,7 @@ async fn call_hook_before_deadline( } pub(crate) struct WorkerInterceptor { + compaction_budget: Option>, registry: Arc, compact_state: Option>, /// Shared view of the cumulative UsageRecord timeline. Used with the @@ -157,6 +158,7 @@ impl WorkerInterceptor { pending_committed_history: Arc>>>, ) -> Self { Self { + compaction_budget: None, registry, compact_state, usage_history, @@ -175,6 +177,14 @@ impl WorkerInterceptor { } } + pub(crate) fn with_compaction_budget( + mut self, + budget: Option>, + ) -> Self { + self.compaction_budget = budget; + self + } + pub(crate) fn with_notification_committer( mut self, committer: Option>, @@ -552,6 +562,8 @@ impl Interceptor for WorkerInterceptor { &self, context: PreLlmRequestContext<'_, SessionHistoryMetadata>, ) -> InterceptorResult { + let context_invocation = context.invocation; + let context_history = context.history; let context = context.items; let initial_tokens = self.estimated_tokens(context); if let Some(action) = @@ -616,7 +628,7 @@ impl Interceptor for WorkerInterceptor { .expect("pending hook system-item queue poisoned"), ); self.attach_prompt_provenance(&mut system_items); - let appended_items: Vec = system_items + let mut appended_items: Vec = system_items .iter() .map(SystemItem::to_history_item) .collect(); @@ -652,10 +664,28 @@ impl Interceptor for WorkerInterceptor { }); } - if let Some(usage_tracker) = self.usage_tracker.as_ref() { - usage_tracker.note_request(effective_context.len()); + // Internal compaction safety must survive the normal Worker interceptor + // installation. Run it on the complete request after durable hook additions; + // Engine persists any returned warning through its normal append path. + if let Some(budget) = self.compaction_budget.as_ref() { + let mut budget_context = effective_context.into_owned(); + match budget + .pre_llm_request(PreLlmRequestContext { + invocation: context_invocation, + items: &mut budget_context, + history: context_history, + }) + .await? + { + PreRequestAction::Continue => {} + PreRequestAction::ContinueWith(warnings) => appended_items.extend(warnings), + action => return Ok(action), + } } - if system_items.is_empty() { + if let Some(usage_tracker) = self.usage_tracker.as_ref() { + usage_tracker.note_request(context.len() + appended_items.len()); + } + if appended_items.is_empty() { return Ok(PreRequestAction::Continue); } Ok(match self.commit_system_items(&system_items) { @@ -700,6 +730,12 @@ impl Interceptor for WorkerInterceptor { if let Some(reason) = denials.into_iter().next() { return Ok(HookPreToolAction::Deny(reason).into_worker_action(summary.call_id.clone())); } + if let Some(budget) = self.compaction_budget.as_ref() { + match budget.pre_tool_call(info).await? { + PreToolAction::Continue => {} + action => return Ok(action), + } + } self.tool_calls_this_turn.fetch_add(1, Ordering::Relaxed); Ok(PreToolAction::Continue) } diff --git a/crates/worker/src/job.rs b/crates/worker/src/job.rs index c32fac642..d2be0e7de 100644 --- a/crates/worker/src/job.rs +++ b/crates/worker/src/job.rs @@ -221,6 +221,7 @@ impl PreparedInternalJob { run_id: uuid::Uuid::now_v7(), }, max_turns: engine_policy.max_turns.map(|n| n.get()), + compaction_budget: None, engine_configurator: Some(Box::new(move |engine| { crate::apply_worker_manifest(engine, &engine_policy); })), diff --git a/crates/worker/src/session_capture.rs b/crates/worker/src/session_capture.rs index f67be5936..dcc14b54d 100644 --- a/crates/worker/src/session_capture.rs +++ b/crates/worker/src/session_capture.rs @@ -725,7 +725,9 @@ fn public_provenance(origin: &WorkerHistoryProvenance) -> SessionEntryProvenance } WorkerHistoryProvenance::ModelOutput { .. } => SessionEntryProvenance::ModelOutput, WorkerHistoryProvenance::ToolOutput { .. } => SessionEntryProvenance::ToolOutput, - WorkerHistoryProvenance::DerivedSummary => SessionEntryProvenance::DerivedSummary, + WorkerHistoryProvenance::CompactionSummary | WorkerHistoryProvenance::DerivedSummary => { + SessionEntryProvenance::DerivedSummary + } WorkerHistoryProvenance::LegacyUnknown => SessionEntryProvenance::LegacyUnknown, } } @@ -878,6 +880,60 @@ mod tests { ); } + #[test] + fn host_summary_remains_private_to_compaction_not_observation_or_memory_evidence() { + let entries = [ + ( + Item::system_message("host-summary-secret"), + WorkerHistoryProvenance::CompactionSummary, + ), + ( + Item::system_message("other-derived-secret"), + WorkerHistoryProvenance::DerivedSummary, + ), + ( + Item::system_message("raw-system-secret"), + WorkerHistoryProvenance::LegacyUnknown, + ), + ( + Item::reasoning("reasoning-secret"), + WorkerHistoryProvenance::LegacyUnknown, + ), + ] + .into_iter() + .map(|(item, origin)| crate::session_history::history_entry(item, origin)) + .collect::>(); + let refs = entries + .iter() + .map(|entry| SessionEntryRef::from_history_entry_id(&entry.annotation.entry_id)) + .collect::>(); + let capture = SessionCapture::from_history_entries("segment", entries); + assert!(capture.overview().is_empty()); + assert!( + capture + .search(&SearchOptions { + query: "secret".into(), + ..SearchOptions::default() + }) + .is_empty() + ); + assert!( + capture + .read(ReadSelector::EntryRange([0, 3]), ReadOptions::default()) + .entries + .is_empty() + ); + for reference in refs { + assert!(capture.evidence_for(reference.as_str()).is_none()); + assert!( + capture + .read(ReadSelector::Id(reference.as_str()), ReadOptions::default()) + .entries + .is_empty() + ); + } + } + #[test] fn overview_contains_user_and_assistant_only() { let view = SessionCapture::new( diff --git a/crates/worker/src/session_history.rs b/crates/worker/src/session_history.rs index f7ed7df8d..2d3a922fc 100644 --- a/crates/worker/src/session_history.rs +++ b/crates/worker/src/session_history.rs @@ -17,6 +17,8 @@ pub type WorkerHistoryProvenance = LoggedSessionHistoryOrigin; pub type SessionHistoryDerivation = LoggedHistoryDerivation; pub type WorkerSubjectSnapshot = LoggedWorkerSubject; +pub(crate) const COMPACTION_SUMMARY_HEADER: &str = "[Compacted context summary]\n\n"; + #[cfg(test)] pub(crate) fn test_logged_history_entry(item: impl Into) -> LoggedHistoryEntry { LoggedHistoryEntry { @@ -108,8 +110,32 @@ pub(crate) fn restore_history_entries( let mut history = Vec::new(); for entry in entries { match entry { - LogEntry::AnnotatedSegmentStart { history: seed, .. } => { + LogEntry::AnnotatedSegmentStart { + history: seed, + compacted_from, + .. + } => { history = seed.iter().map(from_logged).collect(); + // Older Host compactions always put this DerivedSummary system item + // first in the replacement seed. Only that trusted construction boundary + // may interpret the marker; never infer origin from appended/pasted text. + if compacted_from.is_some() + && let Some(first) = history.first_mut() + && first.annotation.origin == WorkerHistoryProvenance::DerivedSummary + && matches!( + &first.item, + Item::Message { + role: agen::Role::System, + .. + } + ) + && first + .item + .as_text() + .is_some_and(|text| text.starts_with(COMPACTION_SUMMARY_HEADER)) + { + first.annotation.origin = WorkerHistoryProvenance::CompactionSummary; + } } LogEntry::AnnotatedUserInput { history: input, .. } => { history.extend(input.iter().map(from_logged)) @@ -144,6 +170,79 @@ mod tests { use protocol::Segment; use session_store::LogEntry; + #[test] + fn restore_recognizes_only_legacy_host_compaction_seed_summary() { + let summary = history_entry( + Item::system_message(format!("{COMPACTION_SUMMARY_HEADER}host summary")), + WorkerHistoryProvenance::DerivedSummary, + ); + for compacted in [false, true] { + let seed = LogEntry::AnnotatedSegmentStart { + ts: 0, + session_id: SessionId::now_v7(), + system_prompt: None, + config: RequestConfig::default(), + history: vec![to_logged_history_entry(&summary)], + forked_from: None, + compacted_from: compacted.then(|| session_store::SegmentOrigin { + segment_id: SegmentId::now_v7(), + at_turn_index: 0, + }), + }; + let appended = LogEntry::AnnotatedUserInput { + ts: 1, + segments: vec![], + extensions: vec![], + history: vec![to_logged_history_entry(&summary)], + }; + let restored = restore_history_entries( + SessionId::now_v7(), + SegmentId::now_v7(), + &[seed, appended], + ) + .unwrap(); + assert_eq!( + restored[0].annotation.origin, + if compacted { + WorkerHistoryProvenance::CompactionSummary + } else { + WorkerHistoryProvenance::DerivedSummary + } + ); + assert_eq!( + restored[1].annotation.origin, + WorkerHistoryProvenance::DerivedSummary + ); + assert_eq!(restored[0].annotation.entry_id, summary.annotation.entry_id); + } + for item in [ + Item::user_message(format!("{COMPACTION_SUMMARY_HEADER}pasted summary")), + Item::system_message("[Session TaskStore snapshot] tasks"), + ] { + let seed = LogEntry::AnnotatedSegmentStart { + ts: 0, + session_id: SessionId::now_v7(), + system_prompt: None, + config: RequestConfig::default(), + history: vec![to_logged_history_entry(&history_entry( + item, + WorkerHistoryProvenance::DerivedSummary, + ))], + forked_from: None, + compacted_from: Some(session_store::SegmentOrigin { + segment_id: SegmentId::now_v7(), + at_turn_index: 0, + }), + }; + let restored = + restore_history_entries(SessionId::now_v7(), SegmentId::now_v7(), &[seed]).unwrap(); + assert_eq!( + restored[0].annotation.origin, + WorkerHistoryProvenance::DerivedSummary + ); + } + } + #[test] fn typed_flow_and_unknown_caller_input_round_trip_without_role_inference() { let session_id = SessionId::now_v7(); diff --git a/crates/worker/src/worker.rs b/crates/worker/src/worker.rs index 051b1e64a..67163155d 100644 --- a/crates/worker/src/worker.rs +++ b/crates/worker/src/worker.rs @@ -2801,6 +2801,7 @@ pub struct Worker { /// manifest-derived lifecycle Features before controller startup. manifest_lifecycle_features_enabled: bool, interceptor_installed: bool, + compaction_service_budget: Option>, /// Shared compaction state (present when threshold is configured). compact_state: Option>, /// Per-LLM-request Usage tracker. Always present after construction. @@ -3145,6 +3146,7 @@ impl Worker { feature_invocations: FeatureInvocationRegistry::default(), manifest_lifecycle_features_enabled: true, interceptor_installed: false, + compaction_service_budget: None, compact_state: None, usage_tracker: Arc::new(UsageTracker::new()), metrics_tracker: Arc::new(crate::compact::metrics_tracker::MetricsTracker::new()), @@ -4221,6 +4223,24 @@ impl Worker { self.callback_socket.as_ref() } + pub(crate) fn install_compaction_budget( + &mut self, + budget: Option, + ) { + self.assert_hooks_open(); + self.compaction_service_budget = budget.map(|budget| { + Arc::new( + budget.with_request_shape( + self.engine() + .get_system_prompt() + .unwrap_or_default() + .to_owned(), + self.engine().tool_server_handle(), + ), + ) + }); + } + // --- Hook registration --- fn assert_hooks_open(&self) { @@ -4316,6 +4336,7 @@ impl Worker { self.log_writer.clone(), self.pending_committed_history.clone(), ) + .with_compaction_budget(self.compaction_service_budget.clone()) .with_notification_committer(self.notification_committer.clone()) .with_usage_tracker(self.usage_tracker.clone()) .with_metrics_tracker(self.metrics_tracker.clone()) @@ -6610,7 +6631,15 @@ impl Worker { let history_entries = self.session.history().entries(); let retain_from = cut.index.min(history_entries.len()); - let retained_history_entries = history_entries[retain_from..].to_vec(); + let initial_summary = initial_compaction_summary(history_entries)?; + // The old summary is consumed in full by the compaction service even when + // the cut falls before it. The replacement summary supersedes it: retaining + // it as well would duplicate inherited context on every compact. + let retained_history_entries = history_entries[retain_from..] + .iter() + .filter(|entry| entry.annotation.origin != WorkerHistoryProvenance::CompactionSummary) + .cloned() + .collect::>(); let mut retained_items = retained_history_entries .iter() .map(|entry| entry.item.clone()) @@ -6623,7 +6652,11 @@ impl Worker { project_compaction_attachments(&mut retained_items, default_workdir_alias.as_deref()); // The compactor explores summaries, text details, and ViewImage call arguments. It // never needs durable image bodies; relevant images are nominated with add_reference. - let mut entries_to_summarise = history_entries[..retain_from].to_vec(); + let mut entries_to_summarise = history_entries[..retain_from] + .iter() + .filter(|entry| entry.annotation.origin != WorkerHistoryProvenance::CompactionSummary) + .cloned() + .collect::>(); project_compactor_capture_attachments(&mut entries_to_summarise); let items_to_summarise = entries_to_summarise .iter() @@ -6688,11 +6721,12 @@ impl Worker { Vec::new() }; - // Input text fed to the compact worker. Includes the default - // references, current TaskStore snapshot, current TaskStore snapshot, and the (pruned) conversation text. + // The full Host summary is mandatory input, separate from the bounded + // prefix overview and current TaskStore snapshot. let task_snapshot_text = self.task_feature.snapshot_text(); let summary_input = build_summary_input( &items_to_summarise, + initial_summary.as_deref(), &default_refs, Some(task_snapshot_text.as_str()), SummaryInputOptions { @@ -6804,12 +6838,12 @@ impl Worker { input: summary_input.text.clone(), cache_key: Some(self.segment_id().to_string()), max_turns: worker_max_turns, + compaction_budget: Some(interceptor), engine_configurator: Some(Box::new(move |engine| { let tracker = tracker_for_engine; engine.on_usage(move |event| { tracker.record_usage(event); }); - engine.set_interceptor(interceptor); })), features, required_tools, @@ -6869,7 +6903,8 @@ impl Worker { Some( "You have not called `write_summary` yet. Deliver the structured \ summary now (Completed Tasks / Active Task / Key Decisions / \ - User Directives / Current Work) and nominate any files the next \ + User Directives / Current Work), preserving ongoing goals and user \ + directives from the initial summary and segment. Nominate any files the next \ session needs with `mark_read_required` for UTF-8 text or \ `add_reference` for images, binary files, and on-demand reads." .to_string(), @@ -6901,11 +6936,8 @@ impl Worker { .ok_or(WorkerError::CompactSummaryMissing)?; let mut summary_tokens = estimate_text_tokens(summary_text.len()); if summary_max_tokens > 0 && summary_tokens > summary_max_tokens { - let prompt = format!( - "Your `write_summary` output is too large (≈{summary_tokens} tokens; max \ - {summary_max_tokens}). Rewrite it now with `write_summary`, preserving the \ - same five sections but making it concise. Target ≈{summary_target_tokens} tokens." - ); + let prompt = + summary_shrink_prompt(summary_tokens, summary_max_tokens, summary_target_tokens); handle .send(prompt) .await @@ -7014,8 +7046,10 @@ impl Worker { ); let mut compact_introduced_system_messages = Vec::with_capacity(2 + auto_read_messages.len() + reference_message.is_some() as usize); - let summary_message = - Item::system_message(format!("[Compacted context summary]\n\n{summary_text}")); + let summary_message = Item::system_message(format!( + "{}{summary_text}", + crate::session_history::COMPACTION_SUMMARY_HEADER + )); compact_introduced_system_messages.push(summary_message.clone()); compact_introduced_system_messages.extend(auto_read_messages.iter().cloned()); if let Some(msg) = reference_message.as_ref() { @@ -7072,8 +7106,19 @@ impl Worker { let compacted_history_entries = new_history .iter() .cloned() - .map(|item| { - if let Some((offset, original)) = original_entries[original_cursor..] + .enumerate() + .map(|(index, item)| { + if index == 0 { + HistoryEntry::new( + item, + new_history_metadata( + WorkerHistoryProvenance::CompactionSummary, + Some(SessionHistoryDerivation { + sources: derived_sources.clone(), + }), + ), + ) + } else if let Some((offset, original)) = original_entries[original_cursor..] .iter() .enumerate() .find(|(_, original)| original.item == item) @@ -7489,6 +7534,7 @@ where feature_invocations: FeatureInvocationRegistry::default(), manifest_lifecycle_features_enabled: true, interceptor_installed: false, + compaction_service_budget: None, compact_state: None, usage_tracker: Arc::new(UsageTracker::new()), metrics_tracker: Arc::new(crate::compact::metrics_tracker::MetricsTracker::new()), @@ -7828,6 +7874,7 @@ where feature_invocations: FeatureInvocationRegistry::default(), manifest_lifecycle_features_enabled: false, interceptor_installed: false, + compaction_service_budget: None, compact_state: None, usage_tracker: Arc::new(UsageTracker::new()), metrics_tracker: Arc::new(crate::compact::metrics_tracker::MetricsTracker::new()), @@ -7957,6 +8004,7 @@ where feature_invocations: FeatureInvocationRegistry::default(), manifest_lifecycle_features_enabled: true, interceptor_installed: false, + compaction_service_budget: None, compact_state: None, usage_tracker: Arc::new(UsageTracker::new()), metrics_tracker: Arc::new(crate::compact::metrics_tracker::MetricsTracker::new()), @@ -8342,6 +8390,7 @@ where feature_invocations: FeatureInvocationRegistry::default(), manifest_lifecycle_features_enabled: true, interceptor_installed: false, + compaction_service_budget: None, compact_state: None, usage_tracker: Arc::new(UsageTracker::new()), metrics_tracker: Arc::new(crate::compact::metrics_tracker::MetricsTracker::new()), @@ -8740,6 +8789,61 @@ fn build_related_file_message( ))) } +fn summary_shrink_prompt( + summary_tokens: u64, + summary_max_tokens: u64, + summary_target_tokens: u64, +) -> String { + format!( + "Your `write_summary` output is too large (≈{summary_tokens} tokens; max \ + {summary_max_tokens}). Rewrite it now with `write_summary`, preserving the \ + same five sections. Compress duplicate explanations and old work details \ + first; preserve ongoing goals and user directives from the initial summary \ + and segment, including scope, prohibitions, completion conditions and \ + exception-reporting duties. Apply explicit later changes/retractions and \ + retain uncertainty; do not replace the original request with recovery \ + procedures or recent progress. Target ≈{summary_target_tokens} tokens." + ) +} + +/// Select only the Host's typed summary, never a marker in ordinary content. +/// Invalid/duplicate Host summaries are a broken seed contract, not permission +/// to silently omit inherited instructions. +fn initial_compaction_summary( + entries: &[HistoryEntry], +) -> Result, WorkerError> { + let mut summaries = entries + .iter() + .filter(|entry| entry.annotation.origin == WorkerHistoryProvenance::CompactionSummary); + let Some(entry) = summaries.next() else { + return Ok(None); + }; + if summaries.next().is_some() { + return Err(WorkerError::InvalidState( + "multiple Host compaction summaries in active history".into(), + )); + } + if !matches!( + &entry.item, + Item::Message { + role: agen::Role::System, + .. + } + ) { + return Err(WorkerError::InvalidState( + "Host compaction summary must be a system message".into(), + )); + } + let body = entry + .item + .as_text() + .and_then(|text| text.strip_prefix(crate::session_history::COMPACTION_SUMMARY_HEADER)) + .ok_or_else(|| { + WorkerError::InvalidState("Host compaction summary header is missing".into()) + })?; + Ok(Some(body.to_string())) +} + #[derive(Debug, Clone, Copy)] struct SummaryInputOptions { overview_target_tokens: u64, @@ -8756,11 +8860,12 @@ struct SummaryInputBuild { deadline_fallback_used: bool, } -/// Build the compact worker's input: default-reference instructions, -/// the list of recently-touched files, task snapshot, -/// and a bounded overview rather than a prefix-wide transcript. +/// Build mandatory full Host-summary input plus a bounded prefix overview, +/// recently-touched files and current tasks. Overview budgets never apply to +/// the summary; the complete request is checked by the compaction interceptor. fn build_summary_input( items: &[Item], + initial_summary: Option<&str>, default_refs: &[PathBuf], task_snapshot: Option<&str>, options: SummaryInputOptions, @@ -8786,10 +8891,20 @@ fn build_summary_input( out.push_str(&format!( "Summarise this session into a structured summary of about {} tokens and \ nominate files the next session needs. The conversation below is a \ - bounded overview/index, not the full transcript. Use tools to inspect \ + bounded overview/index, not the full transcript. A separately supplied initial \ + compaction summary is complete, mandatory source material. Preserve ongoing \ + user goals and directives from it and from this segment's user messages; \ + confirm important wording with exploration rather than reconstructing the \ + request from recent progress. Retained tail entries remain verbatim in the \ + next session and are outside this prefix overview. Use tools to inspect \ current files when deciding auto-read/reference output.\n\n", options.summary_target_tokens )); + if let Some(summary) = initial_summary { + out.push_str("## Initial compaction summary (full text)\n"); + out.push_str(summary); + out.push_str("\n\n## End initial compaction summary\n\n"); + } if !default_refs.is_empty() { out.push_str( "These files were touched recently in this session. Use `read_file` \ @@ -8808,7 +8923,8 @@ fn build_summary_input( out.push_str( "## Current Session TaskStore\n\ This is the full current task list. Use it as source material for the \ - summary, especially active (pending/inprogress) tasks, but do not edit tasks \ + summary, especially active (pending/inprogress) tasks, but not as a \ + replacement for user goals, directives or completion conditions. Do not edit tasks \ from the compact worker.\n", ); out.push_str(task_snapshot); @@ -10103,6 +10219,7 @@ mod build_summary_prompt_tests { fn test_summary_input(items: &[Item]) -> String { build_summary_input( items, + None, &[], None, SummaryInputOptions { @@ -10115,6 +10232,72 @@ mod build_summary_prompt_tests { .text } + #[test] + fn compact_prompt_and_shrink_policy_prioritize_continuing_user_intent() { + // The builtin resource is the default model-visible compaction contract. + let prompt = include_str!("../../../resources/prompts/internal/compact_system.md"); + for policy in [ + "initial compaction summary in full", + "segment's user messages", + "prohibitions", + "completion conditions", + "explicit changes or retractions", + "Preserve uncertainty", + "retained tail stays verbatim", + "TaskStore", + ] { + assert!(prompt.contains(policy), "missing prompt policy: {policy}"); + } + let shrink = summary_shrink_prompt(2_000, 1_000, 500); + for policy in [ + "initial summary", + "and segment", + "Compress duplicate", + "prohibitions", + "completion conditions", + "exception-reporting duties", + "changes/retractions", + "uncertainty", + "recovery", + ] { + assert!(shrink.contains(policy), "missing shrink policy: {policy}"); + } + let input = test_summary_input(&[Item::user_message("keep original scope")]); + assert!(input.contains("Preserve ongoing user goals and directives")); + assert!(input.contains("Retained tail entries remain verbatim")); + } + + #[test] + fn malformed_or_duplicate_host_summaries_fail_instead_of_omitting_context() { + use crate::session_history::history_entry; + let valid = history_entry( + Item::system_message(format!( + "{}body", + crate::session_history::COMPACTION_SUMMARY_HEADER + )), + WorkerHistoryProvenance::CompactionSummary, + ); + assert_eq!( + initial_compaction_summary(std::slice::from_ref(&valid)) + .unwrap() + .as_deref(), + Some("body") + ); + assert!(initial_compaction_summary(&[valid.clone(), valid]).is_err()); + for item in [ + Item::user_message("pasted"), + Item::system_message("missing marker"), + ] { + assert!( + initial_compaction_summary(&[history_entry( + item, + WorkerHistoryProvenance::CompactionSummary + )]) + .is_err() + ); + } + } + #[test] fn retained_images_become_on_demand_related_files() { let mut items = vec![ @@ -10215,6 +10398,7 @@ mod build_summary_prompt_tests { let items = vec![Item::user_message("x".repeat(4_000))]; let built = build_summary_input( &items, + None, &[], None, SummaryInputOptions { @@ -10234,6 +10418,7 @@ mod build_summary_prompt_tests { let items = vec![Item::user_message("x".repeat(4_000))]; let built = build_summary_input( &items, + None, &[], None, SummaryInputOptions { diff --git a/crates/worker/tests/compact_events_test.rs b/crates/worker/tests/compact_events_test.rs index 1a1fdc3eb..84d633759 100644 --- a/crates/worker/tests/compact_events_test.rs +++ b/crates/worker/tests/compact_events_test.rs @@ -133,6 +133,7 @@ struct MockClient { responses: Arc>>, call_count: Arc, observer: Arc>>, + requests: Arc>>, } impl MockClient { @@ -141,9 +142,14 @@ impl MockClient { responses: Arc::new(responses), call_count: Arc::new(AtomicUsize::new(0)), observer: Arc::new(Mutex::new(None)), + requests: Arc::new(Mutex::new(Vec::new())), } } + fn requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } + fn set_observer(&self, observer: impl Fn(usize) + Send + Sync + 'static) { *self.observer.lock().unwrap() = Some(Arc::new(observer)); } @@ -157,9 +163,10 @@ impl LlmClient for MockClient { async fn stream( &self, - _request: Request, + request: Request, ) -> Result> + Send>>, ClientError> { + self.requests.lock().unwrap().push(request); let count = self.call_count.fetch_add(1, Ordering::SeqCst); if let Some(observer) = self.observer.lock().unwrap().clone() { observer(count); @@ -2065,3 +2072,721 @@ async fn controller_compact_method_publishes_progress_and_clear() { let command = protocol::WorkerCommandEnvelope::new(2); let _ = handle.send(Method::Shutdown { command }).await; } + +const INITIAL_SUMMARY_HEADING: &str = "## Initial compaction summary (full text)\n"; +const OVERVIEW_HEADING: &str = "## Conversation overview/index\n"; +const SUMMARY_MARKER: &str = "[Compacted context summary]\n\n"; + +// These tests prove transport and durable replacement policy, not whether a live +// model preserves meaning. All write_summary outputs below are predetermined. +struct SummaryInputFixture { + worker: Worker, + _store_dir: tempfile::TempDir, +} + +impl SummaryInputFixture { + async fn new(manifest_toml: &str, client: MockClient) -> Self { + let store_dir = tempfile::tempdir().unwrap(); + let store = CombinedStore::new( + FsStore::new(store_dir.path()).unwrap(), + FsWorkerStore::new(store_dir.path().join("pods")).unwrap(), + ); + let engine = + Engine::<_, agen::state::Mutable, worker::SessionHistoryMetadata>::new_annotated( + client, + ); + let mut worker = Worker::new( + worker::WorkerManifest::from_toml(manifest_toml).unwrap(), + engine, + store, + worker::WorkerWorkspaceContext::no_workspace(), + worker::WorkerFilesystemAuthority::None, + worker::Scope::empty(), + ) + .await + .unwrap(); + worker.enable_worker_metadata_write_through().unwrap(); + worker.attach_log_writer(Arc::new(worker.log_writer_handle())); + Self { + worker, + _store_dir: store_dir, + } + } + + // Seed the persisted boundary, then exercise the public rewind API's shared + // typed history restore path. No production client or global allocation is + // needed. This is restoration coverage, not a cold Runtime restart test. + async fn restore_seed( + &mut self, + history: Vec, + compacted: bool, + ) { + self.worker + .run_text("fixture rewind boundary") + .await + .unwrap(); + let session_id = self.worker.session_id(); + let segment_id = self.worker.segment_id(); + let mut records = self + .worker + .store() + .read_all(session_id, segment_id) + .unwrap(); + let start = records + .iter_mut() + .find(|entry| matches!(entry, LogEntry::AnnotatedSegmentStart { .. })) + .unwrap(); + match start { + LogEntry::AnnotatedSegmentStart { + history: seed, + compacted_from, + .. + } => { + *seed = history; + *compacted_from = compacted.then(|| session_store::SegmentOrigin { + segment_id: session_store::new_segment_id(), + at_turn_index: 0, + }); + } + _ => unreachable!(), + } + self.worker + .store() + .create_segment(session_id, segment_id, &records) + .unwrap(); + let (head, targets) = self.worker.list_rewind_targets().unwrap(); + self.worker + .rewind_to(targets[0].id.clone(), head) + .await + .unwrap(); + } +} + +fn summary_entry( + item: Item, + origin: session_store::LoggedSessionHistoryOrigin, +) -> session_store::LoggedHistoryEntry { + let mut entry = annotated(item); + entry.metadata.origin = origin; + entry +} + +fn long_summary(label: &str) -> String { + format!( + "## Completed Tasks\n{label}: investigated the transport boundary.\n\n\ + ## Active Task\nKeep the original user's completion conditions.\n{}\n\ + ## Key Decisions\nDo not infer provenance from pasted markers.\n\n\ + ## User Directives\n{label}_LOAD_BEARING_SENTINEL_AT_END\n\n\ + ## Current Work\nContinue the requested implementation.", + "Historical constraints must remain explicit, including the original acceptance criteria.\n" + .repeat(70) + ) +} + +fn recent_history(count: usize) -> Vec { + (0..count) + .flat_map(|index| { + [ + annotated(Item::user_message(format!( + "recent-user-{index}: {}", + "recent progress is not a replacement for the original directive. ".repeat(40) + ))), + annotated(Item::assistant_message(format!("recent-assistant-{index}"))), + ] + }) + .collect() +} + +fn first_compactor_input(request: &Request) -> String { + request + .items + .iter() + .find_map(|item| match item { + Item::Message { + role: agen::Role::User, + .. + } => item + .as_text() + .filter(|text| text.contains(OVERVIEW_HEADING)), + _ => None, + }) + .expect("the first compactor request must carry its overview input") + .to_string() +} + +fn assert_full_initial_summary(input: &str, summary: &str) { + let heading_at = input + .find(INITIAL_SUMMARY_HEADING) + .expect("full summary heading"); + let overview_at = input.find(OVERVIEW_HEADING).expect("overview heading"); + let summary_at = heading_at + INITIAL_SUMMARY_HEADING.len(); + assert!(summary_at < overview_at); + assert!( + input[summary_at..].starts_with(summary), + "summary must be verbatim" + ); + assert_eq!( + input.matches(summary).count(), + 1, + "summary must not be duplicated" + ); + assert!(!input[overview_at..].contains(summary)); + let opening = summary.chars().take(100).collect::(); + assert!( + !input[overview_at..].contains(&opening), + "overview must not carry even a clipped summary" + ); + assert!(!input[overview_at..].contains("LOAD_BEARING_SENTINEL_AT_END")); +} + +fn replacement_seed( + worker: &Worker, +) -> Vec { + worker + .store() + .read_all(worker.session_id(), worker.segment_id()) + .unwrap() + .into_iter() + .find_map(|entry| match entry { + LogEntry::AnnotatedSegmentStart { history, .. } => Some(history), + _ => None, + }) + .expect("replacement SegmentStart") +} + +#[tokio::test] +async fn full_initial_summary_survives_small_overview_budgets_and_both_retention_extremes() { + use session_store::LoggedSessionHistoryOrigin::CompactionSummary; + + let inherited = long_summary("original"); + assert!(inherited.chars().count() > 4_000); + for retained in [0, 1_000_000] { + for (warning, deadline) in [(0, 0), (1, 0), (1, 1)] { + let manifest = MANUAL_ONLY_MANIFEST_TOML.replace( + "[compaction]", + &format!( + "[compaction]\noverview_target_tokens = 1\noverview_warning_tokens = {warning}\noverview_deadline_tokens = {deadline}" + ), + ); + let client = MockClient::new(vec![ + single_text_events("fixture"), + write_summary_tool_use_events("replace", "new summary"), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(&manifest, client).await; + let mut seed = vec![summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}{inherited}")), + CompactionSummary, + )]; + let recent = recent_history(40); + seed.extend(recent.clone()); + fixture.restore_seed(seed, true).await; + let (tx, mut rx) = broadcast::channel::(64); + fixture.worker.attach_alerter(worker::Alerter::new(tx)); + + fixture.worker.compact(retained).await.unwrap(); + + let requests = observed.requests(); + assert_eq!(requests.len(), 3); + let input = first_compactor_input(&requests[1]); + assert_full_initial_summary(&input, &inherited); + let alerts = drain(&mut rx); + let has_alert = |snippet: &str| { + alerts.iter().any(|event| { + matches!(event, Event::Alert(alert) + if alert.source == protocol::AlertSource::Compactor + && alert.level == protocol::AlertLevel::Warn + && alert.message.contains(snippet)) + }) + }; + assert_eq!(has_alert("larger than expected"), warning != 0); + assert_eq!(has_alert("using coarse fallback"), deadline != 0); + let replacement = replacement_seed(&fixture.worker); + assert_eq!( + replacement + .iter() + .filter(|entry| entry.metadata.origin == CompactionSummary) + .count(), + 1 + ); + assert!(replacement.iter().all(|entry| { + !Item::from(entry.item.clone()) + .as_text() + .is_some_and(|text| text.contains(&inherited)) + })); + for entry in &recent { + assert_eq!( + replacement + .iter() + .any(|retained_entry| retained_entry == entry), + retained != 0, + "ordinary retained entries must keep their item and metadata" + ); + } + let metrics = session_metrics::read_session_metrics( + fixture.worker.store(), + fixture.worker.session_id(), + ) + .unwrap(); + let overview_tokens = metrics + .iter() + .find(|record| record.metric.name == "compact.overview_tokens") + .and_then(|record| record.metric.value) + .unwrap(); + assert!( + overview_tokens < inherited.len().div_ceil(4) as f64, + "overview accounting must exclude the inherited full summary" + ); + } + } +} + +#[tokio::test] +async fn pasted_markers_and_other_derived_context_remain_ordinary_overview_material() { + use session_store::LoggedSessionHistoryOrigin::{ + CompactionSummary, DerivedSummary, LegacyUnknown, + }; + + for retained in [0, 1_000_000] { + let client = MockClient::new(vec![ + single_text_events("fixture"), + write_summary_tool_use_events("replace", "replacement"), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(MANUAL_ONLY_MANIFEST_TOML, client).await; + let ordinary = vec![ + summary_entry( + Item::system_message("[Session TaskStore snapshot]\nactive task"), + DerivedSummary, + ), + summary_entry( + Item::system_message("auto-read file source material"), + DerivedSummary, + ), + summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}pasted system marker")), + LegacyUnknown, + ), + summary_entry( + Item::user_message(format!("{SUMMARY_MARKER}pasted user marker")), + LegacyUnknown, + ), + summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}later derived marker")), + DerivedSummary, + ), + ]; + fixture.restore_seed(ordinary.clone(), true).await; + assert!( + fixture + .worker + .session_history() + .entries() + .iter() + .all(|entry| entry.annotation.origin != CompactionSummary) + ); + + fixture.worker.compact(retained).await.unwrap(); + + let input = first_compactor_input(&observed.requests()[1]); + assert!(!input.contains(INITIAL_SUMMARY_HEADING)); + let overview = input.split_once(OVERVIEW_HEADING).unwrap().1; + let replacement = replacement_seed(&fixture.worker); + for entry in &ordinary { + let text = Item::from(entry.item.clone()) + .as_text() + .unwrap() + .to_string(); + assert_eq!( + overview.contains(&text), + retained == 0, + "ordinary overview source: {text}" + ); + assert_eq!( + replacement + .iter() + .any(|retained_entry| retained_entry == entry), + retained != 0, + "other derived context and pasted markers must follow ordinary retention" + ); + } + } +} + +#[tokio::test] +async fn restored_legacy_summary_requires_compacted_lineage_first_position_origin_role_and_marker() +{ + use session_store::LoggedSessionHistoryOrigin::{ + CompactionSummary, DerivedSummary, LegacyUnknown, + }; + + let inherited = long_summary("legacy"); + // Each false case lacks one trusted construction condition; marker text + // alone must never promote ordinary context into inherited Host material. + for case in [ + "trusted", + "no-lineage", + "not-first", + "unknown-origin", + "user-role", + "no-marker", + ] { + let client = MockClient::new(vec![ + single_text_events("fixture"), + write_summary_tool_use_events("replace", "replacement"), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(MANUAL_ONLY_MANIFEST_TOML, client).await; + let text = if case == "no-marker" { + inherited.clone() + } else { + format!("{SUMMARY_MARKER}{inherited}") + }; + let item = if case == "user-role" { + Item::user_message(text) + } else { + Item::system_message(text) + }; + let origin = if case == "unknown-origin" { + LegacyUnknown + } else { + DerivedSummary + }; + let candidate = summary_entry(item, origin); + let candidate_id = candidate.metadata.entry_id.clone(); + let mut history = vec![candidate]; + if case == "not-first" { + history.insert( + 0, + annotated(Item::system_message("unrelated first system context")), + ); + } + history.extend(recent_history(20)); + fixture.restore_seed(history, case != "no-lineage").await; + let restored = fixture + .worker + .session_history() + .entries() + .iter() + .find(|entry| entry.annotation.entry_id == candidate_id) + .unwrap(); + assert_eq!( + restored.annotation.origin == CompactionSummary, + case == "trusted", + "{case}" + ); + // A restore-time upgrade is an in-memory interpretation of the trusted + // seed; it must not silently rewrite the historical durable record. + assert!( + replacement_seed(&fixture.worker) + .iter() + .all(|entry| entry.metadata.origin != CompactionSummary) + ); + + fixture.worker.compact(0).await.unwrap(); + + let input = first_compactor_input(&observed.requests()[1]); + if case == "trusted" { + assert_full_initial_summary(&input, &inherited); + } else { + assert!(!input.contains(INITIAL_SUMMARY_HEADING), "{case}"); + } + } +} + +#[tokio::test] +async fn three_chained_compactions_transport_each_complete_predecessor_without_retaining_old_summaries() + { + use session_store::LoggedSessionHistoryOrigin::CompactionSummary; + + let original_goal = + "CHAIN_ORIGINAL_USER_GOAL: finish only after all explicit acceptance conditions hold"; + // Every predetermined output keeps the original directive. The assertions + // prove that complete predecessors reach later requests, not that a model + // chose to preserve this directive on its own. + let summaries = (0..3) + .map(|index| { + format!( + "{}\n{original_goal}", + long_summary(&format!("generation-{index}")) + ) + }) + .collect::>(); + let initial = format!("{}\n{original_goal}", long_summary("initial")); + let mut responses = vec![single_text_events("seed response")]; + for (index, summary) in summaries.iter().enumerate() { + responses.push(single_text_events( + "acknowledge new directive and temporary recovery", + )); + responses.push(write_summary_tool_use_events( + &format!("summary-{index}"), + summary, + )); + responses.push(single_text_events("done")); + } + let client = MockClient::new(responses); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(MANUAL_ONLY_MANIFEST_TOML, client).await; + fixture + .restore_seed( + vec![summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}{initial}")), + CompactionSummary, + )], + true, + ) + .await; + let original_session = fixture.worker.session_id(); + let mut predecessor = fixture.worker.segment_id(); + for (index, summary) in summaries.iter().enumerate() { + let new_directive = format!( + "NEW_USER_DIRECTIVE_{index}: do not merge. Temporarily recover the interrupted work and collect SubWorkers; this does not retract the original goal." + ); + fixture.worker.run_text(&new_directive).await.unwrap(); + let first_request = observed.requests().len(); + fixture.worker.compact(0).await.unwrap(); + let input = first_compactor_input(&observed.requests()[first_request]); + let prior = if index == 0 { + &initial + } else { + &summaries[index - 1] + }; + assert_full_initial_summary(&input, prior); + assert!(input.contains(original_goal)); + assert!(input.contains(&new_directive)); + let seed = replacement_seed(&fixture.worker); + let inherited = seed + .iter() + .filter(|entry| entry.metadata.origin == CompactionSummary) + .collect::>(); + assert_eq!(inherited.len(), 1); + assert_eq!( + Item::from(inherited[0].item.clone()).as_text().unwrap(), + format!("{SUMMARY_MARKER}{summary}") + ); + for old in &summaries[..index] { + assert!( + fixture + .worker + .history() + .iter() + .all(|item| !item.as_text().is_some_and(|text| text.contains(old))) + ); + } + let records = fixture + .worker + .store() + .read_all(original_session, fixture.worker.segment_id()) + .unwrap(); + assert!( + records + .iter() + .any(|entry| matches!(entry, LogEntry::AnnotatedSegmentStart { + compacted_from: Some(origin), .. + } if origin.segment_id == predecessor)) + ); + let restored = session_store::restore( + fixture.worker.store(), + original_session, + fixture.worker.segment_id(), + ) + .unwrap(); + assert!(restored.history.iter().any(|item| { + item.as_text() + .is_some_and(|text| text == format!("{SUMMARY_MARKER}{summary}")) + })); + predecessor = fixture.worker.segment_id(); + } + assert_eq!(observed.requests().len(), 10); +} + +#[tokio::test] +async fn inherited_summary_counts_against_hard_input_limit_and_failure_does_not_activate() { + use session_store::LoggedSessionHistoryOrigin::CompactionSummary; + + let manifest = MANUAL_ONLY_MANIFEST_TOML.replace( + "[compaction]", + "[compaction]\nworker_context_max_tokens = 10000\nfinish_warning_remaining_tokens = 0\nfinal_reserve_tokens = 0\noverview_target_tokens = 1", + ); + for include_summary in [false, true] { + let client = MockClient::new(vec![ + single_text_events("fixture"), + write_summary_tool_use_events("replace", "replacement"), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(&manifest, client).await; + let summary = + "Preserve the user's goal and every explicit completion condition.\n".repeat(3_000); + assert!( + agen::token_counter::total_tokens(&[Item::user_message(&summary)], &[]).tokens > 10_000 + ); + let mut history = Vec::new(); + if include_summary { + history.push(summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}{summary}")), + CompactionSummary, + )); + } + history.extend(recent_history(20)); + fixture.restore_seed(history, true).await; + let source = fixture.worker.segment_id(); + let source_history = fixture.worker.history(); + let result = fixture.worker.compact(0).await; + if include_summary { + let error = result.expect_err("mandatory input over hard cap must fail, not truncate"); + assert!( + matches!(error, WorkerError::CompactCancelled) + || error.to_string().contains("input occupancy"), + "unexpected hard-cap failure: {error:?}" + ); + assert_eq!( + observed.requests().len(), + 1, + "no compactor request may cross the hard cap" + ); + assert_eq!(fixture.worker.segment_id(), source); + assert_eq!(fixture.worker.history(), source_history); + assert_eq!( + fixture + .worker + .store() + .read_by_name("test-worker") + .unwrap() + .unwrap() + .active + .unwrap() + .segment_id, + Some(source) + ); + assert_eq!( + fixture + .worker + .store() + .list_segments(fixture.worker.session_id()) + .unwrap() + .len(), + 1 + ); + } else { + result.expect("the same bounded overview without inherited input fits the cap"); + assert_eq!(observed.requests().len(), 3); + assert_ne!(fixture.worker.segment_id(), source); + } + } +} + +#[tokio::test] +async fn summary_output_still_over_hard_max_after_rewrite_fails_without_activation() { + let oversized = long_summary("oversized"); + let manifest = MANUAL_ONLY_MANIFEST_TOML.replace( + "[compaction]", + "[compaction]\nsummary_target_tokens = 50\nsummary_max_tokens = 100", + ); + let client = MockClient::new(vec![ + single_text_events("seed"), + write_summary_tool_use_events("first-output", &oversized), + single_text_events("done"), + write_summary_tool_use_events("rewrite-output", &oversized), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(&manifest, client).await; + fixture.worker.run_text("user goal").await.unwrap(); + let source = fixture.worker.segment_id(); + let history = fixture.worker.history(); + + let error = fixture.worker.compact(0).await.unwrap_err(); + + assert!(matches!( + error, + WorkerError::CompactSummaryTooLarge { max: 100, .. } + )); + assert_eq!(fixture.worker.segment_id(), source); + assert_eq!(fixture.worker.history(), history); + assert_eq!( + fixture + .worker + .store() + .read_by_name("test-worker") + .unwrap() + .unwrap() + .active + .unwrap() + .segment_id, + Some(source) + ); + assert_eq!( + fixture + .worker + .store() + .list_segments(fixture.worker.session_id()) + .unwrap() + .len(), + 1 + ); + assert!( + observed + .requests() + .iter() + .any(|request| request.items.iter().any(|item| item + .as_text() + .is_some_and(|text| text.contains("Rewrite it now with `write_summary`")))) + ); +} + +#[tokio::test] +async fn budget_warning_is_durable_and_keeps_full_summary_in_first_model_request() { + use session_store::LoggedSessionHistoryOrigin::CompactionSummary; + let manifest = MANUAL_ONLY_MANIFEST_TOML.replace( + "[compaction]", + "[compaction]\nworker_context_max_tokens = 10000\nfinish_warning_remaining_tokens = 9999\nfinal_reserve_tokens = 9999", + ); + let client = MockClient::new(vec![ + single_text_events("fixture"), + write_summary_tool_use_events("summary", "replacement"), + single_text_events("done"), + ]); + let observed = client.clone(); + let mut fixture = SummaryInputFixture::new(&manifest, client).await; + let inherited = long_summary("budget-warning"); + fixture + .restore_seed( + vec![summary_entry( + Item::system_message(format!("{SUMMARY_MARKER}{inherited}")), + CompactionSummary, + )], + true, + ) + .await; + let (tx, mut rx) = broadcast::channel::(128); + fixture.worker.attach_alerter(worker::Alerter::new(tx)); + fixture.worker.compact(0).await.unwrap(); + let requests = observed.requests(); + assert_full_initial_summary(&first_compactor_input(&requests[1]), &inherited); + let warning = requests[1] + .items + .iter() + .filter_map(Item::as_text) + .find(|text| text.starts_with("[Compact worker budget warning]")) + .expect("normal internal Worker must preserve the budget interceptor warning"); + assert!(warning.contains("full initial summary and segment")); + assert!(warning.contains("changes/retractions")); + assert!( + requests[2] + .items + .iter() + .filter_map(Item::as_text) + .any(|text| text == warning), + "warning must remain in history, not be a transient request insertion" + ); + assert!( + drain(&mut rx) + .iter() + .any(|event| matches!(event, Event::Alert(alert) + if alert.message.contains("compact worker context budget is low"))) + ); +} diff --git a/crates/workspace-server/src/frozen_repository_key_migration.rs b/crates/workspace-server/src/frozen_repository_key_migration.rs index fe6e2ce8a..708cd358e 100644 --- a/crates/workspace-server/src/frozen_repository_key_migration.rs +++ b/crates/workspace-server/src/frozen_repository_key_migration.rs @@ -32,7 +32,8 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( tx.execute_batch( "CREATE TEMP TABLE repository_key_cutover ( workspace_id TEXT NOT NULL, kind TEXT NOT NULL, resource_id TEXT NOT NULL, - legacy_counter INTEGER NOT NULL, operation_id TEXT NOT NULL, fingerprint TEXT NOT NULL, created_at TEXT NOT NULL, + legacy_counter INTEGER NOT NULL, operation_id TEXT NOT NULL, fingerprint TEXT NOT NULL, + created_at_seconds INTEGER NOT NULL, created_at_subsec_nanos INTEGER NOT NULL, PRIMARY KEY(workspace_id,kind,resource_id,legacy_counter));", )?; for (table, kind, id, fingerprint) in [ @@ -68,15 +69,20 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( for (workspace, resource, legacy_counter, fingerprint, created) in rows { let mut statement = tx.prepare("SELECT operation_id FROM repository_secret_operations WHERE workspace_id=?1 AND resource_kind=?2 AND resource_id=?3 AND result_revision=?4 AND created_at=?5")?; let matches = statement - .query_map(params![workspace, kind, resource, legacy_counter, created], |r| { - r.get::<_, String>(0) - })? + .query_map( + params![workspace, kind, resource, legacy_counter, created], + |r| r.get::<_, String>(0), + )? .collect::>>()?; if matches.len() != 1 || matches[0].is_empty() { return Err(Error::Store("Repository key cutover requires one unambiguous mutation receipt per stored key".into())); } + let created_time = frozen_repository_timestamp( + &created, + &format!("{table}.created_at (workspace {workspace}, resource {resource})"), + )?; tx.execute( - "INSERT INTO repository_key_cutover VALUES(?1,?2,?3,?4,?5,?6,?7)", + "INSERT INTO repository_key_cutover VALUES(?1,?2,?3,?4,?5,?6,?7,?8)", params![ workspace, kind, @@ -84,11 +90,26 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( legacy_counter, matches[0], fingerprint, - created + created_time.timestamp(), + created_time.timestamp_subsec_nanos() ], )?; } } + // Candidate copying uses an INNER JOIN: reject corrupt source rows before + // they can disappear and evade the destination foreign-key check. + let orphaned_candidates: bool = tx.query_row( + "SELECT EXISTS(SELECT 1 FROM workdir_create_credential_candidates c + LEFT JOIN workdir_create_operations o ON o.workspace_id=c.workspace_id AND o.operation_id=c.operation_id + WHERE o.operation_id IS NULL)", + [], + |row| row.get(0), + )?; + if orphaned_candidates { + return Err(Error::Store( + "Repository key cutover found credential candidates without a Workdir create operation".into(), + )); + } // Current rows must agree with their pointed-to historical key, not merely // resolve to a nonempty receipt. FK checks alone cannot prove this identity. let inconsistent: bool = tx.query_row("SELECT EXISTS ( @@ -148,8 +169,17 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( })? .collect::>>()? }; - for (workspace, id, legacy_counter, purpose, algorithm, nonce, ciphertext, created, operation) in - secrets + for ( + workspace, + id, + legacy_counter, + purpose, + algorithm, + nonce, + ciphertext, + created, + operation, + ) in secrets { if algorithm != "aes-256-gcm-v1" { return Err(Error::Store( @@ -233,8 +263,9 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( SELECT o.workspace_id,o.operation_id FROM workdir_create_operations_v86 o WHERE o.state='succeeded' AND NOT EXISTS(SELECT 1 FROM workdir_create_credential_revision_retentions_v86 r WHERE r.workspace_id=o.workspace_id AND r.operation_id=o.operation_id) - AND (o.credential_id IS NOT NULL OR o.host_trust_id IS NOT NULL OR EXISTS(SELECT 1 FROM workdir_create_credential_candidates_v86 c WHERE c.workspace_id=o.workspace_id AND c.operation_id=o.operation_id)); - INSERT INTO repository_secret_legacy_receipts + AND (o.credential_id IS NOT NULL OR o.host_trust_id IS NOT NULL OR EXISTS(SELECT 1 FROM workdir_create_credential_candidates_v86 c WHERE c.workspace_id=o.workspace_id AND c.operation_id=o.operation_id));")?; + prepare_repository_cutover_times(&tx)?; + tx.execute_batch("INSERT INTO repository_secret_legacy_receipts SELECT e.workspace_id,e.operation_id,e.request_fingerprint,e.resource_kind,e.resource_id,e.result_revision,e.created_at,e.mutation_kind, CASE WHEN e.mutation_kind IN ('credential_created','host_trust_created') THEN 0 WHEN e.mutation_kind IN ('credential_rotated','host_trust_rotated') AND e.result_revision>1 THEN e.result_revision-1 @@ -244,8 +275,9 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( WHERE a.workspace_id=o.workspace_id AND a.resource_id=o.resource_id AND a.revision=o.result_revision AND a.created_at=o.created_at AND ((o.resource_kind='credential' AND a.kind LIKE 'credential_%') OR (o.resource_kind='host_trust' AND a.kind LIKE 'host_trust_%')) HAVING count(*)=1) AS mutation_kind FROM repository_secret_operations_v86 o) e + JOIN repository_cutover_times t ON t.kind='receipt' AND t.workspace_id=e.workspace_id AND t.operation_id=e.operation_id LEFT JOIN repository_key_cutover m ON m.workspace_id=e.workspace_id AND m.kind=e.resource_kind AND m.resource_id=e.resource_id - AND m.created_at<=e.created_at + AND (m.created_at_seconds,m.created_at_subsec_nanos)<=(t.seconds,t.subsec_nanos) AND m.legacy_counter=CASE WHEN e.mutation_kind IN ('credential_rotated','host_trust_rotated') THEN e.result_revision-1 WHEN e.mutation_kind IN ('credential_deleted','host_trust_deleted') THEN e.result_revision END; INSERT INTO workdir_create_operations @@ -257,12 +289,14 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( CASE WHEN a.operation_id IS NULL THEN o.repository_access_mode END FROM workdir_create_operations_v86 o LEFT JOIN workdir_terminal_archive_cutover a ON a.workspace_id=o.workspace_id AND a.operation_id=o.operation_id - LEFT JOIN repository_key_cutover c ON c.workspace_id=o.workspace_id AND c.kind='credential' AND c.resource_id=o.credential_id AND c.legacy_counter=o.credential_revision AND c.created_at<=o.updated_at - LEFT JOIN repository_key_cutover h ON h.workspace_id=o.workspace_id AND h.kind='host_trust' AND h.resource_id=o.host_trust_id AND h.legacy_counter=o.host_trust_revision AND h.created_at<=o.updated_at; + LEFT JOIN repository_cutover_times t ON t.kind='workdir' AND t.workspace_id=o.workspace_id AND t.operation_id=o.operation_id + LEFT JOIN repository_key_cutover c ON c.workspace_id=o.workspace_id AND c.kind='credential' AND c.resource_id=o.credential_id AND c.legacy_counter=o.credential_revision AND (c.created_at_seconds,c.created_at_subsec_nanos)<=(t.seconds,t.subsec_nanos) + LEFT JOIN repository_key_cutover h ON h.workspace_id=o.workspace_id AND h.kind='host_trust' AND h.resource_id=o.host_trust_id AND h.legacy_counter=o.host_trust_revision AND (h.created_at_seconds,h.created_at_subsec_nanos)<=(t.seconds,t.subsec_nanos); INSERT INTO workdir_create_credential_candidates SELECT c.workspace_id,c.operation_id,c.ordinal,c.role,c.credential_id,m.fingerprint - FROM workdir_create_credential_candidates_v86 c JOIN workdir_create_operations_v86 o ON o.workspace_id=c.workspace_id AND o.operation_id=c.operation_id LEFT JOIN repository_key_cutover m - ON m.workspace_id=c.workspace_id AND m.kind='credential' AND m.resource_id=c.credential_id AND m.legacy_counter=c.credential_revision AND m.created_at<=o.updated_at + FROM workdir_create_credential_candidates_v86 c JOIN workdir_create_operations_v86 o ON o.workspace_id=c.workspace_id AND o.operation_id=c.operation_id + JOIN repository_cutover_times t ON t.kind='workdir' AND t.workspace_id=o.workspace_id AND t.operation_id=o.operation_id + LEFT JOIN repository_key_cutover m ON m.workspace_id=c.workspace_id AND m.kind='credential' AND m.resource_id=c.credential_id AND m.legacy_counter=c.credential_revision AND (m.created_at_seconds,m.created_at_subsec_nanos)<=(t.seconds,t.subsec_nanos) WHERE NOT EXISTS(SELECT 1 FROM workdir_terminal_archive_cutover a WHERE a.workspace_id=c.workspace_id AND a.operation_id=c.operation_id); INSERT INTO workdir_create_credential_retentions SELECT c.workspace_id,c.operation_id,c.ordinal,c.credential_id,m.fingerprint,m.operation_id FROM workdir_create_credential_revision_retentions_v86 c LEFT JOIN repository_key_cutover m ON m.workspace_id=c.workspace_id AND m.kind='credential' AND m.resource_id=c.credential_id AND m.legacy_counter=c.credential_revision;")?; let missing:i64=tx.query_row("SELECT count(*) FROM workdir_create_operations WHERE (credential_id IS NOT NULL AND credential_fingerprint IS NULL) OR (host_trust_id IS NOT NULL AND host_trust_fingerprint IS NULL)",[],|r|r.get(0))?; @@ -313,7 +347,7 @@ fn migrate_repository_keys_v86_to_v87_with_secret_source( ] { tx.execute_batch(&format!("DROP TABLE {table}_v86;"))?; } - tx.execute_batch("DROP TABLE repository_key_cutover; + tx.execute_batch("DROP TABLE repository_key_cutover; DROP TABLE repository_cutover_times; CREATE INDEX idx_repository_secret_audit_workspace_created ON repository_secret_audit_events(workspace_id,created_at,event_id); CREATE INDEX idx_repository_ssh_credentials_workspace_status ON repository_ssh_credentials(workspace_id,status,credential_id); CREATE INDEX idx_repository_ssh_host_trusts_workspace_host ON repository_ssh_host_trusts(workspace_id,hostname,port);")?; @@ -360,6 +394,109 @@ BEGIN SELECT RAISE(ABORT, 'runtime_removal_in_progress'); END; result.and(restore) } +// Schema 86 used RFC3339 for Repository secrets and epoch-millisecond text for +// registry operations. Compare typed instants, never their persisted spelling. +fn frozen_repository_timestamp( + value: &str, + context: &str, +) -> Result> { + if let Ok(timestamp) = chrono::DateTime::parse_from_rfc3339(value) { + return Ok(timestamp.with_timezone(&chrono::Utc)); + } + if let Ok(millis) = value.parse::() + && let Some(timestamp) = chrono::DateTime::from_timestamp_millis(millis) + { + return Ok(timestamp); + } + Err(Error::Store(format!( + "Repository key migration found invalid timestamp in {context}" + ))) +} + +fn prepare_repository_cutover_times(tx: &rusqlite::Transaction<'_>) -> Result<()> { + // These temporary normalized values never rewrite audit history or the + // original timestamps used to identify receipts and encrypted envelopes. + // Seconds + nanoseconds also preserve sub-millisecond ordering and offsets. + tx.execute_batch( + "CREATE TEMP TABLE repository_cutover_times ( + kind TEXT NOT NULL, workspace_id TEXT NOT NULL, operation_id TEXT NOT NULL, + seconds INTEGER NOT NULL, subsec_nanos INTEGER NOT NULL, + PRIMARY KEY(kind,workspace_id,operation_id));", + )?; + for (kind, context, query) in [ + ( + "receipt", + "repository_secret_operations.created_at", + "SELECT workspace_id,operation_id,created_at FROM repository_secret_operations_v86", + ), + ( + "workdir", + "workdir_create_operations.updated_at", + "SELECT o.workspace_id,o.operation_id,o.updated_at FROM workdir_create_operations_v86 o + WHERE NOT EXISTS(SELECT 1 FROM workdir_terminal_archive_cutover a + WHERE a.workspace_id=o.workspace_id AND a.operation_id=o.operation_id) + AND (o.credential_id IS NOT NULL OR o.host_trust_id IS NOT NULL OR + EXISTS(SELECT 1 FROM workdir_create_credential_candidates_v86 c + WHERE c.workspace_id=o.workspace_id AND c.operation_id=o.operation_id))", + ), + ] { + let rows = tx + .prepare(query)? + .query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, String>(2)?, + )) + })? + .collect::>>()?; + for (workspace, operation, value) in rows { + let timestamp = frozen_repository_timestamp( + &value, + &format!("{context} (workspace {workspace}, operation {operation})"), + )?; + tx.execute( + "INSERT INTO repository_cutover_times VALUES(?1,?2,?3,?4,?5)", + params![ + kind, + workspace, + operation, + timestamp.timestamp(), + timestamp.timestamp_subsec_nanos() + ], + )?; + } + } + // Refuse missing or newer/reused keys with a domain error before a NULL + // fingerprint reaches a NOT NULL column. Archived successes are excluded: + // they retain opaque history, not authority to retry with a current key. + let missing: Option<(String, String)> = tx.query_row( + "SELECT r.workspace_id,r.operation_id FROM ( + SELECT workspace_id,operation_id,'credential' AS kind,credential_id AS resource_id,credential_revision AS legacy_counter + FROM workdir_create_operations_v86 WHERE credential_id IS NOT NULL + UNION + SELECT workspace_id,operation_id,'host_trust',host_trust_id,host_trust_revision + FROM workdir_create_operations_v86 WHERE host_trust_id IS NOT NULL + UNION + SELECT workspace_id,operation_id,'credential',credential_id,credential_revision + FROM workdir_create_credential_candidates_v86 + ) r + JOIN repository_cutover_times t ON t.kind='workdir' AND t.workspace_id=r.workspace_id AND t.operation_id=r.operation_id + LEFT JOIN repository_key_cutover k ON k.workspace_id=r.workspace_id AND k.kind=r.kind + AND k.resource_id=r.resource_id AND k.legacy_counter=r.legacy_counter + AND (k.created_at_seconds,k.created_at_subsec_nanos)<=(t.seconds,t.subsec_nanos) + WHERE k.operation_id IS NULL LIMIT 1", + [], + |row| Ok((row.get(0)?, row.get(1)?)), + ).optional()?; + if let Some((workspace, operation)) = missing { + return Err(Error::Store(format!( + "Workdir SSH snapshot is missing key identity evidence at or before its timestamp (workspace {workspace}, operation {operation})" + ))); + } + Ok(()) +} + // Preserve every SQLite field and NULL verbatim as typed JSON. Frozen archives // are never read by credential lookup, lease construction, or a live CAS. fn frozen_repository_json_rows( diff --git a/crates/workspace-server/src/frozen_repository_key_tests.rs b/crates/workspace-server/src/frozen_repository_key_tests.rs index d8599dcdf..adeef98fe 100644 --- a/crates/workspace-server/src/frozen_repository_key_tests.rs +++ b/crates/workspace-server/src/frozen_repository_key_tests.rs @@ -32,12 +32,12 @@ fn legacy_repository_keys(path: &std::path::Path) -> Connection { let end = start + frozen[start..].find(end).unwrap(); conn.execute_batch(&frozen[start..end]).unwrap(); } - conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','create-key','intent','credential','key',7,'created'); - INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519','fingerprint',7,'active','created',NULL); - INSERT INTO repository_ssh_credential_revisions VALUES('space','key',7,'ssh-ed25519','fingerprint','created'); - INSERT INTO repository_secret_audit_events VALUES('space','audit','credential_created','key',7,'actor','created'); + conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','create-key','intent','credential','key',7,'2026-01-01T00:00:00.000Z'); + INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519','fingerprint',7,'active','2026-01-01T00:00:00.000Z',NULL); + INSERT INTO repository_ssh_credential_revisions VALUES('space','key',7,'ssh-ed25519','fingerprint','2026-01-01T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','audit','credential_created','key',7,'actor','2026-01-01T00:00:00.000Z'); INSERT INTO workdir_create_operations(workspace_id,operation_id,request_fingerprint,repository_id,resolved_runtime_id,config_revision,config_projection_digest,working_directory_id,state,created_at,updated_at,credential_id,credential_revision) - VALUES('space','create-workdir','intent','repo','runtime',3,'projection','workdir','pending','created','created','key',7); + VALUES('space','create-workdir','intent','repo','runtime',3,'projection','workdir','pending','2026-01-01T00:00:00.000Z','2026-01-01T00:00:00.000Z','key',7); INSERT INTO workdir_create_credential_candidates VALUES('space','create-workdir',0,'primary','key',7); INSERT INTO workdir_create_credential_revision_retentions VALUES('space','create-workdir',0,'key',7); CREATE TRIGGER workdir_create_insert_blocked_by_runtime_removal @@ -59,11 +59,300 @@ fn legacy_repository_keys(path: &std::path::Path) -> Connection { &mut ciphertext, ) .unwrap(); - conn.execute("INSERT INTO server_secret_versions VALUES('space','key',7,?1,'aes-256-gcm-v1',?2,?3,'created')",params![purpose,nonce.as_slice(),ciphertext]).unwrap(); + conn.execute("INSERT INTO server_secret_versions VALUES('space','key',7,?1,'aes-256-gcm-v1',?2,?3,'2026-01-01T00:00:00.000Z')",params![purpose,nonce.as_slice(),ciphertext]).unwrap(); } conn } +// Exercise the actual persisted formats, including failed creates whose SSH +// snapshot remains retry authority. The encrypted fixture uses the real old AAD. +fn legacy_ssh_snapshot_times(conn: &Connection, key_created: &str, workdir_updated: &str) { + conn.execute( + "UPDATE repository_secret_operations SET created_at=?1", + [key_created], + ) + .unwrap(); + conn.execute( + "UPDATE repository_secret_audit_events SET created_at=?1", + [key_created], + ) + .unwrap(); + conn.execute( + "UPDATE repository_ssh_credentials SET created_at=?1", + [key_created], + ) + .unwrap(); + conn.execute( + "UPDATE repository_ssh_credential_revisions SET created_at=?1", + [key_created], + ) + .unwrap(); + conn.execute( + "UPDATE server_secret_versions SET created_at=?1", + [key_created], + ) + .unwrap(); + conn.execute("INSERT INTO repository_secret_operations VALUES('space','create-host','intent','host_trust','host',3,?1)", [key_created]).unwrap(); + conn.execute("INSERT INTO repository_secret_audit_events VALUES('space','host-audit','host_trust_created','host',3,'actor',?1)", [key_created]).unwrap(); + conn.execute("INSERT INTO repository_ssh_host_trusts VALUES('space','host','example.test',22,'ssh-ed25519','host-key','host-fingerprint',3,?1,?1)", [key_created]).unwrap(); + conn.execute("INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',3,'example.test',22,'ssh-ed25519','host-key','host-fingerprint',?1)", [key_created]).unwrap(); + conn.execute("UPDATE workdir_create_operations SET state='failed',failure='runtime_workdir_create_failed',host_trust_id='host',host_trust_revision=3,repository_access_mode='read_only',updated_at=?1", [workdir_updated]).unwrap(); +} + +#[test] +fn repository_key_cutover_compares_mixed_timestamp_formats_as_instants() { + for (key_created, workdir_updated) in [ + ("2026-01-01T00:00:00.000Z", "1767225600000"), + ("2026-01-01T00:00:00.000Z", "1767225600001"), + ("2026-01-01T09:00:00.000+09:00", "2026-01-01T00:00:00Z"), + ( + "2026-01-01T00:00:00.000000001Z", + "2026-01-01T00:00:00.000000002Z", + ), + ] { + let dir = tempfile::tempdir().unwrap(); + let conn = legacy_repository_keys(&dir.path().join("server.db")); + std::fs::write(dir.path().join("repository-secrets.master-key"), [42; 32]).unwrap(); + legacy_ssh_snapshot_times(&conn, key_created, workdir_updated); + migrate_repository_keys_v86_to_v87(&conn).unwrap(); + assert_eq!(current_schema_version(&conn).unwrap(), 87); + let snapshot: (String, String, String, String) = conn.query_row( + "SELECT state,credential_fingerprint,host_trust_fingerprint,updated_at FROM workdir_create_operations", + [], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?)), + ).unwrap(); + assert_eq!( + snapshot, + ( + "failed".into(), + "fingerprint".into(), + "host-fingerprint".into(), + workdir_updated.into() + ) + ); + let retained: (String, String, String) = conn.query_row( + "SELECT c.credential_fingerprint,r.credential_fingerprint,r.credential_operation_id + FROM workdir_create_credential_candidates c JOIN workdir_create_credential_retentions r + ON r.workspace_id=c.workspace_id AND r.operation_id=c.operation_id AND r.ordinal=c.ordinal", + [], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?)), + ).unwrap(); + assert_eq!( + retained, + ( + "fingerprint".into(), + "fingerprint".into(), + "create-key".into() + ) + ); + assert_eq!( + conn.query_row( + "SELECT created_at FROM repository_ssh_credential_keys", + [], + |r| r.get::<_, String>(0) + ) + .unwrap(), + key_created + ); + assert!( + !conn + .prepare("PRAGMA foreign_key_check") + .unwrap() + .exists([]) + .unwrap() + ); + } +} + +fn assert_timestamp_cutover_rolls_back(conn: &Connection, expected_context: &str) { + let before = + frozen_repository_json_rows(conn, "SELECT * FROM workdir_create_operations", &[]).unwrap(); + let ciphertext: Vec = conn + .query_row( + "SELECT ciphertext FROM server_secret_versions WHERE purpose='private_key'", + [], + |r| r.get(0), + ) + .unwrap(); + let error = migrate_repository_keys_v86_to_v87(conn) + .unwrap_err() + .to_string(); + assert!(error.contains(expected_context), "{error}"); + assert!(error.contains("timestamp"), "{error}"); + assert_eq!(current_schema_version(conn).unwrap(), 86); + assert_eq!( + frozen_repository_json_rows(conn, "SELECT * FROM workdir_create_operations", &[]).unwrap(), + before + ); + assert_eq!( + conn.query_row( + "SELECT ciphertext FROM server_secret_versions WHERE purpose='private_key'", + [], + |r| r.get::<_, Vec>(0) + ) + .unwrap(), + ciphertext + ); + assert!(table_exists(conn, "repository_ssh_credential_revisions").unwrap()); + assert!(!table_exists(conn, "repository_ssh_credential_keys").unwrap()); + assert_eq!( + conn.query_row( + "SELECT count(*) FROM workdir_create_credential_candidates", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + 1 + ); + assert_eq!( + conn.query_row( + "SELECT count(*) FROM workdir_create_credential_revision_retentions", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + 1 + ); + assert_eq!(conn.query_row("SELECT count(*) FROM sqlite_temp_master WHERE name IN ('repository_key_cutover','repository_cutover_times','workdir_terminal_archive_cutover')", [], |r| r.get::<_,i64>(0)).unwrap(), 0); + assert_eq!( + conn.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 1 + ); + assert_eq!( + conn.query_row("PRAGMA legacy_alter_table", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 0 + ); +} + +#[test] +fn repository_key_cutover_rejects_newer_reused_snapshot_keys_without_committing_authority() { + for source in ["credential", "host_trust", "candidate"] { + for later in ["2026-01-01T00:00:00.001Z", "2026-01-01T00:00:00.000000001Z"] { + let dir = tempfile::tempdir().unwrap(); + let conn = legacy_repository_keys(&dir.path().join("server.db")); + std::fs::write(dir.path().join("repository-secrets.master-key"), [42; 32]).unwrap(); + legacy_ssh_snapshot_times(&conn, "2026-01-01T00:00:00.000Z", "1767225600000"); + if source == "host_trust" { + conn.execute( + "UPDATE repository_ssh_host_trust_revisions SET created_at=?1", + [later], + ) + .unwrap(); + conn.execute("UPDATE repository_secret_operations SET created_at=?1 WHERE resource_kind='host_trust'", [later]).unwrap(); + conn.execute("UPDATE repository_secret_audit_events SET created_at=?1 WHERE kind='host_trust_created'", [later]).unwrap(); + } else { + conn.execute( + "UPDATE repository_ssh_credential_revisions SET created_at=?1", + [later], + ) + .unwrap(); + conn.execute("UPDATE repository_secret_operations SET created_at=?1 WHERE resource_kind='credential'", [later]).unwrap(); + conn.execute("UPDATE repository_secret_audit_events SET created_at=?1 WHERE kind='credential_created'", [later]).unwrap(); + if source == "candidate" { + conn.execute("UPDATE workdir_create_operations SET credential_id=NULL,credential_revision=NULL", []).unwrap(); + } + } + assert_timestamp_cutover_rolls_back(&conn, "Workdir SSH snapshot"); + } + } +} + +#[test] +fn repository_key_cutover_invalid_authority_timestamps_roll_back_with_source_context() { + for (source, invalid) in [ + ("workdir_create_operations.updated_at", "not-a-date"), + ( + "workdir_create_operations.updated_at", + "9223372036854775807", + ), + ( + "repository_ssh_credential_revisions.created_at", + "not-a-date", + ), + ( + "repository_ssh_host_trust_revisions.created_at", + "not-a-date", + ), + ("repository_secret_operations.created_at", "not-a-date"), + ] { + let dir = tempfile::tempdir().unwrap(); + let conn = legacy_repository_keys(&dir.path().join("server.db")); + std::fs::write(dir.path().join("repository-secrets.master-key"), [42; 32]).unwrap(); + legacy_ssh_snapshot_times(&conn, "2026-01-01T00:00:00.000Z", "1767225600000"); + match source { + "workdir_create_operations.updated_at" => { + conn.execute( + "UPDATE workdir_create_operations SET updated_at=?1", + [invalid], + ) + .unwrap(); + } + "repository_ssh_credential_revisions.created_at" => { + conn.execute( + "UPDATE repository_ssh_credential_revisions SET created_at=?1", + [invalid], + ) + .unwrap(); + conn.execute("UPDATE repository_secret_operations SET created_at=?1 WHERE resource_kind='credential'", [invalid]).unwrap(); + } + "repository_ssh_host_trust_revisions.created_at" => { + conn.execute( + "UPDATE repository_ssh_host_trust_revisions SET created_at=?1", + [invalid], + ) + .unwrap(); + conn.execute("UPDATE repository_secret_operations SET created_at=?1 WHERE resource_kind='host_trust'", [invalid]).unwrap(); + } + "repository_secret_operations.created_at" => { + conn.execute("INSERT INTO repository_secret_operations VALUES('space','old-delete','intent','credential','gone',1,?1)", [invalid]).unwrap(); + conn.execute("INSERT INTO repository_secret_audit_events VALUES('space','old-delete-audit','credential_deleted','gone',1,'actor',?1)", [invalid]).unwrap(); + } + _ => unreachable!(), + } + assert_timestamp_cutover_rolls_back(&conn, source); + } +} + +#[test] +fn repository_key_cutover_rejects_orphan_candidates_without_dropping_source_evidence() { + let dir = tempfile::tempdir().unwrap(); + let conn = legacy_repository_keys(&dir.path().join("server.db")); + std::fs::write(dir.path().join("repository-secrets.master-key"), [42; 32]).unwrap(); + conn.execute_batch("PRAGMA foreign_keys=OFF; + INSERT INTO workdir_create_credential_candidates VALUES('space','missing-parent',0,'primary','key',7); + PRAGMA foreign_keys=ON;").unwrap(); + let before = frozen_repository_json_rows( + &conn, + "SELECT * FROM workdir_create_credential_candidates ORDER BY operation_id,ordinal", + &[], + ) + .unwrap(); + let error = migrate_repository_keys_v86_to_v87(&conn).unwrap_err(); + assert!( + error + .to_string() + .contains("credential candidates without a Workdir create operation"), + "{error}" + ); + assert_eq!(current_schema_version(&conn).unwrap(), 86); + assert_eq!( + frozen_repository_json_rows( + &conn, + "SELECT * FROM workdir_create_credential_candidates ORDER BY operation_id,ordinal", + &[] + ) + .unwrap(), + before + ); + assert!(table_exists(&conn, "repository_ssh_credential_revisions").unwrap()); + assert!(!table_exists(&conn, "repository_ssh_credential_keys").unwrap()); + assert_eq!( + conn.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 1 + ); +} + #[test] fn repository_key_cutover_reseals_secrets_and_preserves_retention_and_audit() { let dir = tempfile::tempdir().unwrap(); @@ -150,12 +439,12 @@ fn repository_key_cutover_reseals_secrets_and_preserves_retention_and_audit() { .unwrap(); assert!( conn.execute( - "UPDATE workdir_create_operations SET updated_at='later'", + "UPDATE workdir_create_operations SET updated_at='2026-01-04T00:00:00.000Z'", [] ) .is_err() ); - assert!(conn.execute("INSERT INTO workdir_create_operations(workspace_id,operation_id,request_fingerprint,repository_id,resolved_runtime_id,config_projection_digest,working_directory_id,state,created_at,updated_at) VALUES('space','blocked','intent','repo','runtime','projection','other-workdir','pending','created','created')", []).is_err()); + assert!(conn.execute("INSERT INTO workdir_create_operations(workspace_id,operation_id,request_fingerprint,repository_id,resolved_runtime_id,config_projection_digest,working_directory_id,state,created_at,updated_at) VALUES('space','blocked','intent','repo','runtime','projection','other-workdir','pending','2026-01-01T00:00:00.000Z','2026-01-01T00:00:00.000Z')", []).is_err()); assert!( !conn .prepare("PRAGMA foreign_key_check") @@ -274,14 +563,14 @@ fn repository_key_cutover_preserves_host_endpoints_deleted_audit_and_nondefault_ let dir = tempfile::tempdir().unwrap(); let conn = legacy_repository_keys(&dir.path().join("server.db")); std::fs::write(dir.path().join("repository-secrets.master-key"), [42; 32]).unwrap(); - conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','host-create','intent','host_trust','host',3,'created'); - INSERT INTO repository_secret_operations VALUES('space','host-move','intent','host_trust','host',4,'updated'); - INSERT INTO repository_ssh_host_trusts VALUES('space','host','new.example.test',2222,'ssh-ed25519','host-key','host-fingerprint',4,'created','updated'); - INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',3,'old.example.test',22,'ssh-ed25519','host-key','host-fingerprint','created'); - INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',4,'new.example.test',2222,'ssh-ed25519','host-key','host-fingerprint','updated'); - INSERT INTO repository_secret_audit_events VALUES('space','host-audit','host_trust_rotated','host',4,'host-actor','updated'); - INSERT INTO repository_secret_operations VALUES('space','delete-gone','intent','credential','gone',5,'deleted'); - INSERT INTO repository_secret_audit_events VALUES('space','delete-audit','credential_deleted','gone',5,'delete-actor','deleted'); + conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','host-create','intent','host_trust','host',3,'2026-01-01T00:00:00.000Z'); + INSERT INTO repository_secret_operations VALUES('space','host-move','intent','host_trust','host',4,'2026-01-02T00:00:00.000Z'); + INSERT INTO repository_ssh_host_trusts VALUES('space','host','new.example.test',2222,'ssh-ed25519','host-key','host-fingerprint',4,'2026-01-01T00:00:00.000Z','2026-01-02T00:00:00.000Z'); + INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',3,'old.example.test',22,'ssh-ed25519','host-key','host-fingerprint','2026-01-01T00:00:00.000Z'); + INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',4,'new.example.test',2222,'ssh-ed25519','host-key','host-fingerprint','2026-01-02T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','host-audit','host_trust_rotated','host',4,'host-actor','2026-01-02T00:00:00.000Z'); + INSERT INTO repository_secret_operations VALUES('space','delete-gone','intent','credential','gone',5,'2026-01-03T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','delete-audit','credential_deleted','gone',5,'delete-actor','2026-01-03T00:00:00.000Z'); UPDATE workdir_create_operations SET host_trust_id='host',host_trust_revision=3;").unwrap(); conn.execute_batch(&format!( "PRAGMA foreign_keys={foreign_keys}; PRAGMA legacy_alter_table={legacy_alter};" @@ -342,8 +631,8 @@ fn deleted_successful_workdir_archives_verbatim_history_without_authorizing_retr let digest = "sha256:db15419814117c33c089a36c5348bda85d01a53fccd8d4d427685daf5983353b"; conn.execute("UPDATE workdir_create_operations SET state='succeeded',request_fingerprint=?1,selector='main',source_kind='local_path',source_uri='/tmp/repo',source_revision=9,source_fingerprint='source'",[digest]).unwrap(); conn.execute_batch("DELETE FROM workdir_create_credential_revision_retentions; - INSERT INTO repository_secret_operations VALUES('space','delete-key','delete-intent','credential','key',7,'deleted'); - INSERT INTO repository_secret_audit_events VALUES('space','delete-audit','credential_deleted','key',7,'deleting-actor','deleted'); + INSERT INTO repository_secret_operations VALUES('space','delete-key','delete-intent','credential','key',7,'2026-01-03T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','delete-audit','credential_deleted','key',7,'deleting-actor','2026-01-03T00:00:00.000Z'); DELETE FROM repository_ssh_credentials;").unwrap(); let original = frozen_repository_json_rows(&conn, "SELECT * FROM workdir_create_operations", &[]) @@ -666,7 +955,7 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei "credential", "key", 1, - "2026-01-01", + "2026-01-01T00:00:00.000Z", "credential_created", ), ( @@ -675,7 +964,7 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei "credential", "key", 2, - "2026-01-02", + "2026-01-02T00:00:00.000Z", "credential_rotated", ), ( @@ -684,7 +973,7 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei "host_trust", "host", 1, - "2026-01-01", + "2026-01-01T00:00:00.000Z", "host_trust_created", ), ( @@ -693,7 +982,7 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei "host_trust", "host", 2, - "2026-01-02", + "2026-01-02T00:00:00.000Z", "host_trust_rotated", ), ] { @@ -708,11 +997,11 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei ) .unwrap(); } - conn.execute("INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519',?1,2,'active','2026-01-01','2026-01-02')",[&next_fp]).unwrap(); + conn.execute("INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519',?1,2,'active','2026-01-01T00:00:00.000Z','2026-01-02T00:00:00.000Z')",[&next_fp]).unwrap(); let encryption_key = LessSafeKey::new(UnboundKey::new(&AES_256_GCM, &[42; 32]).unwrap()); for (legacy_counter, fingerprint, private, timestamp) in [ - (1u64, &old_fp, &old_private, "2026-01-01"), - (2, &next_fp, &next_private, "2026-01-02"), + (1u64, &old_fp, &old_private, "2026-01-01T00:00:00.000Z"), + (2, &next_fp, &next_private, "2026-01-02T00:00:00.000Z"), ] { conn.execute("INSERT INTO repository_ssh_credential_revisions VALUES('space','key',?1,'ssh-ed25519',?2,?3)",params![legacy_counter,fingerprint,timestamp]).unwrap(); let nonce = [legacy_counter as u8; 12]; @@ -727,10 +1016,10 @@ fn migrated_ssh_mutations_replay_complete_old_input_without_reexecution_or_recei .unwrap(); conn.execute("INSERT INTO server_secret_versions VALUES('space','key',?1,'private_key','aes-256-gcm-v1',?2,?3,?4)",params![legacy_counter,nonce.as_slice(),ciphertext,timestamp]).unwrap(); } - conn.execute("INSERT INTO repository_ssh_host_trusts VALUES('space','host','new.example.test',22,'ssh-ed25519',?1,?2,2,'2026-01-01','2026-01-02')",params![public,old_fp]).unwrap(); + conn.execute("INSERT INTO repository_ssh_host_trusts VALUES('space','host','new.example.test',22,'ssh-ed25519',?1,?2,2,'2026-01-01T00:00:00.000Z','2026-01-02T00:00:00.000Z')",params![public,old_fp]).unwrap(); for (legacy_counter, hostname, timestamp) in [ - (1, "old.example.test", "2026-01-01"), - (2, "new.example.test", "2026-01-02"), + (1, "old.example.test", "2026-01-01T00:00:00.000Z"), + (2, "new.example.test", "2026-01-02T00:00:00.000Z"), ] { conn.execute("INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',?1,?2,22,'ssh-ed25519',?3,?4,?5)",params![legacy_counter,hostname,public,old_fp,timestamp]).unwrap(); } @@ -884,8 +1173,8 @@ fn deleted_legacy_receipt_does_not_bind_to_later_reused_key_ordinal() { // The old deleted ID has been recreated using the same ordinal. Its earlier // delete receipt must remain history, not acquire proof from this new key. conn.execute_batch("DELETE FROM workdir_create_operations; - INSERT INTO repository_secret_operations VALUES('space','old-delete','intent','credential','key',7,'2025-01-01'); - INSERT INTO repository_secret_audit_events VALUES('space','old-delete-audit','credential_deleted','key',7,'actor','2025-01-01');").unwrap(); + INSERT INTO repository_secret_operations VALUES('space','old-delete','intent','credential','key',7,'2025-01-01T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','old-delete-audit','credential_deleted','key',7,'actor','2025-01-01T00:00:00.000Z');").unwrap(); migrate_repository_keys_v86_to_v87(&conn).unwrap(); // The service consumes current receipt columns; unrelated schemas are not this fixture. rename_saved_secret_receipt_counters(&conn).unwrap(); @@ -966,8 +1255,8 @@ fn repository_key_cutover_failure_preserves_schema_ciphertext_and_connection_pra } "inconsistent_retention" => { // A different valid retained key passes the ordinal-only candidate FK. - conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','another-key','intent','credential','key',8,'later'); - INSERT INTO repository_ssh_credential_revisions VALUES('space','key',8,'ssh-ed25519','other','later'); + conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','another-key','intent','credential','key',8,'2026-01-04T00:00:00.000Z'); + INSERT INTO repository_ssh_credential_revisions VALUES('space','key',8,'ssh-ed25519','other','2026-01-04T00:00:00.000Z'); UPDATE workdir_create_credential_revision_retentions SET credential_revision=8;").unwrap(); } "missing_audit_receipt" => { @@ -978,12 +1267,12 @@ fn repository_key_cutover_failure_preserves_schema_ciphertext_and_connection_pra .unwrap(); } "missing_host_receipt" => { - conn.execute_batch("INSERT INTO repository_ssh_host_trusts VALUES('space','host','example.test',22,'ssh-ed25519','key','host-fingerprint',4,'created','created'); - INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',4,'example.test',22,'ssh-ed25519','key','host-fingerprint','created');").unwrap(); + conn.execute_batch("INSERT INTO repository_ssh_host_trusts VALUES('space','host','example.test',22,'ssh-ed25519','key','host-fingerprint',4,'2026-01-01T00:00:00.000Z','2026-01-01T00:00:00.000Z'); + INSERT INTO repository_ssh_host_trust_revisions VALUES('space','host',4,'example.test',22,'ssh-ed25519','key','host-fingerprint','2026-01-01T00:00:00.000Z');").unwrap(); } "foreign_key_violation" => { conn.execute_batch("PRAGMA foreign_keys=OFF; - INSERT INTO repository_secret_operations VALUES('missing-space','orphan','intent','credential','deleted',1,'created'); + INSERT INTO repository_secret_operations VALUES('missing-space','orphan','intent','credential','deleted',1,'2026-01-01T00:00:00.000Z'); PRAGMA foreign_keys=ON;").unwrap(); } _ => {} diff --git a/crates/workspace-server/src/frozen_runtime_binding_tests.rs b/crates/workspace-server/src/frozen_runtime_binding_tests.rs index 3ea64b2ab..fcf7dcc97 100644 --- a/crates/workspace-server/src/frozen_runtime_binding_tests.rs +++ b/crates/workspace-server/src/frozen_runtime_binding_tests.rs @@ -817,17 +817,12 @@ mod current_authority { fn binding_store() -> (SqliteWorkspaceStore, WorkspaceRuntimeBinding) { let store = SqliteWorkspaceStore::in_memory().unwrap(); - let workspace_key = - worker_runtime::auth::RuntimeIdentityMaterial::generate("workspace-key").unwrap(); let runtime_key = worker_runtime::auth::RuntimeIdentityMaterial::generate("runtime-key").unwrap(); - let workspace_fingerprint = normalize_runtime_public_key(&workspace_key.public_key) - .unwrap() - .1; store.with_conn(|conn| { conn.execute_batch("INSERT INTO accounts(account_id,kind,handle,display_name,created_at,updated_at) VALUES('owner','user','owner','Owner','created','updated'); - INSERT INTO workspaces(workspace_id,owner_account_id,display_name,state,created_at,updated_at) VALUES('space','owner','Space','active','created','updated');")?; - conn.execute("INSERT INTO workspace_signing_identities(workspace_id,key_id,algorithm,public_key,public_key_fingerprint,private_material_ref,state,created_at,provisioned_at,updated_at) VALUES('space','workspace-key','ed25519',?1,?2,'material','active','created','provisioned','updated')", params![workspace_key.public_key,workspace_fingerprint])?; + INSERT INTO workspaces(workspace_id,owner_account_id,display_name,state,created_at,updated_at) VALUES('space','owner','Space','active','created','updated'); + INSERT INTO workspace_signing_identities(workspace_id,key_id,algorithm,public_key,public_key_fingerprint,private_material_ref,state,created_at,provisioned_at,updated_at) VALUES('space','workspace-key','ed25519','public','sha256:workspace','material','active','created','provisioned','updated');")?; Ok(()) }).unwrap(); let request = WorkspaceRuntimeBinding { @@ -838,11 +833,7 @@ mod current_authority { public_key: runtime_key.public_key, public_key_fingerprint: String::new(), binding_id: String::new(), - state: WorkspaceRuntimeBindingState::Configured, authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("workspace-key".into()), - workspace_public_key_fingerprint: Some(workspace_fingerprint), - workspace_trust_id: Some("runtime-issued-trust".into()), created_at: "created".into(), updated_at: "updated".into(), revoked_at: None, @@ -853,36 +844,10 @@ mod current_authority { (store, binding) } - fn evidence(binding: &WorkspaceRuntimeBinding) -> WorkspaceRuntimeVerificationEvidence { - WorkspaceRuntimeVerificationEvidence { - workspace_id: binding.workspace_id.clone(), - runtime_id: binding.runtime_id.clone(), - binding_id: binding.binding_id.clone(), - workspace_key_id: binding.workspace_key_id.clone().unwrap(), - workspace_public_key_fingerprint: binding - .workspace_public_key_fingerprint - .clone() - .unwrap(), - workspace_trust_id: binding.workspace_trust_id.clone().unwrap(), - runtime_public_key_fingerprint: binding.public_key_fingerprint.clone(), - challenge_id: "challenge".into(), - state: "verified".into(), - last_outcome: "verified".into(), - verified_at: Some("verified-at".into()), - checked_at: "checked-at".into(), - } - } - #[test] - fn same_key_reactivation_fences_old_cas_and_verification_evidence() { + fn same_key_reactivation_fences_old_config_and_admin_mutations() { let (store, binding) = binding_store(); - let old_proof = evidence(&binding); - store - .record_workspace_runtime_verification_attempt(&old_proof) - .unwrap(); - store - .complete_workspace_runtime_verification(&old_proof) - .unwrap(); + assert!(store.workspace_runtime_binding_matches(&binding).unwrap()); let (_, revoked) = store .revoke_workspace_runtime_binding_key( "space", @@ -893,16 +858,22 @@ mod current_authority { ) .unwrap(); assert_ne!(binding.binding_id, revoked.binding_id); - let mut replacement = binding.clone(); - replacement.state = WorkspaceRuntimeBindingState::Configured; + assert!(!store.workspace_runtime_binding_matches(&binding).unwrap()); + assert!(!store.workspace_runtime_binding_matches(&revoked).unwrap()); let (_, reactivated) = store - .put_workspace_runtime_binding_key(replacement, Some(&revoked.binding_id), "owner") + .put_workspace_runtime_binding_key(binding.clone(), Some(&revoked.binding_id), "owner") .unwrap(); assert_eq!( binding.public_key_fingerprint, reactivated.public_key_fingerprint ); assert_ne!(binding.binding_id, reactivated.binding_id); + assert!( + store + .workspace_runtime_binding_matches(&reactivated) + .unwrap() + ); + assert!(!store.workspace_runtime_binding_matches(&binding).unwrap()); assert!(matches!( store.put_workspace_runtime_binding_key( reactivated.clone(), @@ -921,42 +892,27 @@ mod current_authority { ), Err(Error::RuntimeBindingIdConflict { .. }) )); - assert!( - store - .record_workspace_runtime_verification_attempt(&old_proof) - .is_err() - ); - assert!( - store - .complete_workspace_runtime_verification(&old_proof) - .is_err() - ); - assert!( - !store - .workspace_runtime_verification_matches( - &reactivated, - &old_proof.workspace_public_key_fingerprint, - &old_proof.workspace_trust_id - ) - .unwrap() - ); } #[test] - fn endpoint_aba_and_workspace_key_change_cannot_reuse_verification_authority() { + fn endpoint_aba_fences_old_config_without_copying_workspace_signing_identity() { let (store, binding) = binding_store(); - let proof = evidence(&binding); - store - .record_workspace_runtime_verification_attempt(&proof) - .unwrap(); - store - .complete_workspace_runtime_verification(&proof) + let renamed = store + .update_workspace_runtime_binding_metadata( + "space", + "remote", + "Renamed", + &binding.base_url, + "renamed-at", + ) .unwrap(); + assert_eq!(binding.binding_id, renamed.binding_id); + assert!(!store.workspace_runtime_binding_matches(&binding).unwrap()); let moved = store .update_workspace_runtime_binding_metadata( "space", "remote", - "Remote", + "Renamed", "https://other.test", "changed", ) @@ -965,35 +921,166 @@ mod current_authority { .update_workspace_runtime_binding_metadata( "space", "remote", - "Remote", + "Renamed", "https://remote.test", "changed-back", ) .unwrap(); assert_ne!(moved.binding_id, binding.binding_id); assert_ne!(returned.binding_id, binding.binding_id); - assert!( - store - .record_workspace_runtime_verification_attempt(&proof) - .is_err() - ); - let current = evidence(&returned); - store - .record_workspace_runtime_verification_attempt(¤t) - .unwrap(); - store.with_conn(|conn| { conn.execute("UPDATE workspace_signing_identities SET public_key_fingerprint='sha256:replacement' WHERE workspace_id='space'",[])?; Ok(()) }).unwrap(); - assert!( - store - .complete_workspace_runtime_verification(¤t) - .is_err() - ); + assert!(!store.workspace_runtime_binding_matches(&renamed).unwrap()); + store.with_conn(|conn| { conn.execute("UPDATE workspace_signing_identities SET public_key_fingerprint='sha256:replacement' WHERE workspace_id='space'", [])?; Ok(()) }).unwrap(); assert_eq!( store .get_workspace_runtime_binding("space", "remote") + .unwrap(), + Some(returned.clone()) + ); + assert!(store.workspace_runtime_binding_matches(&returned).unwrap()); + } + + fn rows(conn: &Connection, sql: &str) -> Vec> { + let mut stmt = conn.prepare(sql).unwrap(); + let count = stmt.column_count(); + stmt.query_map([], |row| (0..count).map(|i| row.get(i)).collect()) + .unwrap() + .collect::>>() + .unwrap() + } + + fn schema89_bindings() -> Connection { + let conn = legacy_binding_database(); + migrate_runtime_bindings_v87_to_v88(&conn).unwrap(); + conn.execute( + "INSERT INTO __yoi_schema_migrations VALUES(89,'descriptive legacy evidence columns')", + [], + ) + .unwrap(); + conn.execute_batch("INSERT INTO workspace_runtime_bindings VALUES + ('space','configured-trust','Configured','https://configured.test','key-c','fp-c','binding-c','configured','workspace_identity','workspace-key','sha256:workspace','trust-c','created-c','updated-c',NULL), + ('space','verified','Verified','https://verified.test','key-v','fp-v','binding-v','verified','workspace_identity','workspace-key','sha256:workspace','trust-v','created-v','updated-v',NULL), + ('space','revoked-null-trust','Revoked','https://revoked.test','key-r','fp-r','binding-r','revoked','workspace_identity','workspace-key',NULL,NULL,'created-r','updated-r','revoked-r'), + ('space','legacy-verified','Legacy','https://legacy.test','key-l','fp-l','binding-l','verified','legacy_server_issuer',NULL,NULL,NULL,'created-l','updated-l',NULL), + ('space','legacy-revoked','Legacy revoked','https://legacy-r.test','key-lr','fp-lr','binding-lr','revoked','legacy_server_issuer',NULL,NULL,NULL,'created-lr','updated-lr','revoked-lr'); + INSERT INTO workspace_runtime_verifications VALUES + ('space','verified','binding-v','workspace-key','sha256:workspace','trust-v','fp-v','challenge-v','verified','verified','verified-at','checked-v'), + ('space','configured-trust','binding-c','workspace-key','sha256:workspace','trust-c','fp-c','challenge-c','pending','challenge_issued',NULL,'checked-c'), + ('space','revoked-null-trust','binding-r','workspace-key','sha256:workspace','old-trust','fp-r','challenge-r','failed','verification_failed',NULL,'checked-r');").unwrap(); + conn + } + + #[test] + fn schema90_preserves_connections_and_archives_all_auth_metadata_without_reenrollment() { + let conn = schema89_bindings(); + let connections = rows( + &conn, + "SELECT workspace_id,runtime_id,display_name,base_url,public_key,public_key_fingerprint,binding_id,authentication_mode,created_at,updated_at,revoked_at FROM workspace_runtime_bindings ORDER BY runtime_id", + ); + let metadata = rows( + &conn, + "SELECT workspace_id,runtime_id,binding_id,state,workspace_key_id,workspace_public_key_fingerprint,workspace_trust_id,updated_at FROM workspace_runtime_bindings ORDER BY runtime_id", + ); + let proofs = rows( + &conn, + "SELECT * FROM workspace_runtime_verifications ORDER BY runtime_id", + ); + let identities = rows(&conn, "SELECT * FROM workspace_signing_identities"); + let audit = rows(&conn, "SELECT * FROM workspace_runtime_binding_audit"); + let removal = rows(&conn, "SELECT * FROM runtime_removal_operations"); + migrate_runtime_binding_connections_v89_to_v90(&conn).unwrap(); + assert_eq!(current_schema_version(&conn).unwrap(), 90); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_bindings ORDER BY runtime_id" + ), + connections + ); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_binding_verification_archive ORDER BY runtime_id" + ), + metadata + ); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_verification_archive ORDER BY runtime_id" + ), + proofs + ); + assert_eq!( + rows(&conn, "SELECT * FROM workspace_signing_identities"), + identities + ); + assert_eq!( + rows(&conn, "SELECT * FROM workspace_runtime_binding_audit"), + audit + ); + assert_eq!( + rows(&conn, "SELECT * FROM runtime_removal_operations"), + removal + ); + verify_runtime_binding_connection_schema(&conn).unwrap(); + assert!(!table_exists(&conn, "workspace_runtime_verifications").unwrap()); + assert!( + conn.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0)) .unwrap() + ); + assert_eq!( + rows(&conn, "SELECT * FROM pragma_foreign_key_check"), + Vec::>::new() + ); + conn.execute_batch("UPDATE workspace_runtime_bindings SET base_url='https://moved.test',binding_id='binding-new' WHERE runtime_id='verified'; + UPDATE workspace_runtime_bindings SET revoked_at='admin-revoked' WHERE runtime_id='configured-trust'; + DELETE FROM workspace_runtime_bindings WHERE runtime_id='legacy-revoked';").unwrap(); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_binding_verification_archive ORDER BY runtime_id" + ), + metadata + ); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_verification_archive ORDER BY runtime_id" + ), + proofs + ); + conn.execute( + "UPDATE runtime_removal_operations SET state='cleanup_pending'", + [], + ) + .unwrap(); + assert!(conn.execute("UPDATE workspace_runtime_bindings SET display_name='Blocked' WHERE runtime_id='remote'", []).unwrap_err().to_string().contains("runtime_removal_in_progress")); + } + + #[test] + fn schema90_failed_rebuild_rolls_back_connections_archives_and_schema_marker() { + let conn = schema89_bindings(); + // An incompatible preexisting archive is not permission to discard historical evidence. + conn.execute_batch("CREATE TABLE workspace_runtime_verification_archive(existing TEXT);") + .unwrap(); + let before = rows( + &conn, + "SELECT * FROM workspace_runtime_bindings ORDER BY runtime_id", + ); + assert!(migrate_runtime_binding_connections_v89_to_v90(&conn).is_err()); + assert_eq!(current_schema_version(&conn).unwrap(), 89); + assert_eq!( + rows( + &conn, + "SELECT * FROM workspace_runtime_bindings ORDER BY runtime_id" + ), + before + ); + assert!(!table_exists(&conn, "workspace_runtime_binding_verification_archive").unwrap()); + assert!(table_exists(&conn, "workspace_runtime_verifications").unwrap()); + assert!( + conn.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0)) .unwrap() - .state, - WorkspaceRuntimeBindingState::Configured ); } } diff --git a/crates/workspace-server/src/hosts.rs b/crates/workspace-server/src/hosts.rs index 8a5e3aa2c..cb8dfa169 100644 --- a/crates/workspace-server/src/hosts.rs +++ b/crates/workspace-server/src/hosts.rs @@ -2,9 +2,7 @@ use crate::Error; use crate::resource_broker::BackendResourceBroker; #[cfg(test)] use crate::resource_broker::BackendResourceTarget; -use crate::store::{ - ControlPlaneStore, WorkspaceRuntimeAuthenticationMode, WorkspaceRuntimeBindingState, -}; +use crate::store::{ControlPlaneStore, WorkspaceRuntimeAuthenticationMode}; use crate::workspace_signing_identity::WorkspaceSigningIdentityService; use chrono::Utc; use protocol::Segment; @@ -66,11 +64,7 @@ use worker_runtime::ssh_host_key_probe::{ SSH_HOST_KEY_PROBE_OPERATION, SSH_HOST_KEY_PROBE_PATH, SshHostKeyProbeRequest, SshHostKeyProbeResponse, }; -use worker_runtime::workspace_issuer::{ - WorkspaceCapabilityClaims, WorkspaceRuntimeVerificationAcknowledgement, - WorkspaceRuntimeVerificationChallenge, WorkspaceRuntimeVerificationReceipt, - WorkspaceRuntimeVerificationResponse, workspace_request_body_digest, -}; +use worker_runtime::workspace_issuer::{WorkspaceCapabilityClaims, workspace_request_body_digest}; pub const EMBEDDED_RUNTIME_ID: &str = "embedded-worker-runtime"; const EMBEDDED_HOST_KIND: &str = "embedded-worker-runtime-host"; @@ -925,32 +919,6 @@ pub trait WorkspaceWorkerRuntime: Send + Sync { }) } - fn activate_workspace_authorization(&self, _binding: crate::store::WorkspaceRuntimeBinding) {} - - fn send_workspace_verification_challenge( - &self, - _challenge: &WorkspaceRuntimeVerificationChallenge, - _bearer_token: &str, - ) -> Result { - Err(RuntimePingFailure::new( - RuntimePingFailureKind::Unsupported, - "runtime_workspace_verification_unsupported", - "Workspace Runtime verification is unavailable for this Runtime provider", - )) - } - - fn send_workspace_verification_acknowledgement( - &self, - _acknowledgement: &WorkspaceRuntimeVerificationAcknowledgement, - _bearer_token: &str, - ) -> Result { - Err(RuntimePingFailure::new( - RuntimePingFailureKind::Unsupported, - "runtime_workspace_verification_unsupported", - "Workspace Runtime verification is unavailable for this Runtime provider", - )) - } - fn list_hosts(&self, limit: usize) -> RuntimeList; fn list_workers(&self, limit: usize) -> RuntimeList; @@ -2277,48 +2245,6 @@ impl RuntimeRegistry { runtime.ping() } - pub fn activate_workspace_authorization( - &self, - runtime_id: &str, - binding: crate::store::WorkspaceRuntimeBinding, - ) -> Result<(), RuntimeRegistryError> { - self.runtime(runtime_id)? - .activate_workspace_authorization(binding); - Ok(()) - } - - pub fn send_workspace_verification_challenge( - &self, - runtime_id: &str, - challenge: &WorkspaceRuntimeVerificationChallenge, - bearer_token: &str, - ) -> Result { - let runtime = self.runtime(runtime_id).map_err(|_| { - RuntimePingFailure::new( - RuntimePingFailureKind::Configuration, - "runtime_verification_registration_unavailable", - "Registered Runtime binding is unavailable", - ) - })?; - runtime.send_workspace_verification_challenge(challenge, bearer_token) - } - - pub fn send_workspace_verification_acknowledgement( - &self, - runtime_id: &str, - acknowledgement: &WorkspaceRuntimeVerificationAcknowledgement, - bearer_token: &str, - ) -> Result { - let runtime = self.runtime(runtime_id).map_err(|_| { - RuntimePingFailure::new( - RuntimePingFailureKind::Configuration, - "runtime_verification_registration_unavailable", - "Registered Runtime binding is unavailable", - ) - })?; - runtime.send_workspace_verification_acknowledgement(acknowledgement, bearer_token) - } - fn runtimes_snapshot(&self) -> Vec> { self.runtimes .read() @@ -3518,7 +3444,9 @@ pub struct WorkspaceRuntimeAuthorization { store: Arc, signing_identities: WorkspaceSigningIdentityService, backend_url: String, - binding: Arc>>, + // The connection selected for this client. Compare it with the current + // administrative configuration before sending, never with a past auth result. + binding: crate::store::WorkspaceRuntimeBinding, } impl std::fmt::Debug for WorkspaceRuntimeAuthorization { @@ -3535,19 +3463,13 @@ impl WorkspaceRuntimeAuthorization { store: Arc, signing_identities: WorkspaceSigningIdentityService, backend_url: impl Into, - binding: Option, + binding: crate::store::WorkspaceRuntimeBinding, ) -> Self { Self { store, signing_identities, backend_url: backend_url.into(), - binding: Arc::new(RwLock::new(binding)), - } - } - - fn activate(&self, binding: crate::store::WorkspaceRuntimeBinding) { - if let Ok(mut current) = self.binding.write() { - *current = Some(binding); + binding, } } @@ -3559,30 +3481,14 @@ impl WorkspaceRuntimeAuthorization { worker_id: Option<&str>, body: &[u8], ) -> Result { - let binding = self - .binding - .read() - .map_err(|_| { - diagnostic( - "workspace_runtime_authorization_unavailable", - HostDiagnosticSeverity::Error, - "Workspace Runtime authorization is unavailable".to_string(), - ) - })? - .clone() - .ok_or_else(|| { - diagnostic( - "workspace_runtime_verification_required", - HostDiagnosticSeverity::Error, - "Workspace Runtime binding is not verified".to_string(), - ) - })?; - if binding.state != WorkspaceRuntimeBindingState::Verified - || binding.authentication_mode != WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity + let binding = &self.binding; + // A removed/replaced endpoint must not be used by an old client. This + // compares connection configuration, not cached authentication success. + if binding.authentication_mode != WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity || binding.revoked_at.is_some() || !self .store - .workspace_runtime_binding_matches(&binding) + .workspace_runtime_binding_matches(binding) .map_err(|error| { diagnostic( "workspace_runtime_authorization_unavailable", @@ -3594,7 +3500,7 @@ impl WorkspaceRuntimeAuthorization { return Err(diagnostic( "workspace_runtime_authorization_stale", HostDiagnosticSeverity::Error, - "Workspace Runtime binding changed or was revoked".to_string(), + "Workspace Runtime connection changed or was revoked".to_string(), )); } let identity = self @@ -3607,63 +3513,26 @@ impl WorkspaceRuntimeAuthorization { error.to_string(), ) })?; - let workspace_key_id = binding.workspace_key_id.as_deref().ok_or_else(|| { - diagnostic( - "workspace_runtime_authorization_invalid", - HostDiagnosticSeverity::Error, - "Workspace Runtime binding is missing its Workspace key".to_string(), - ) - })?; - let workspace_public_key_fingerprint = binding - .workspace_public_key_fingerprint - .as_deref() - .ok_or_else(|| { - diagnostic( - "workspace_runtime_authorization_invalid", - HostDiagnosticSeverity::Error, - "Workspace Runtime binding is missing its Workspace public key fingerprint" - .to_string(), - ) - })?; - let workspace_trust_id = binding.workspace_trust_id.as_deref().ok_or_else(|| { - diagnostic( - "workspace_runtime_authorization_invalid", - HostDiagnosticSeverity::Error, - "Workspace Runtime binding is missing its Workspace trust ID".to_string(), - ) - })?; - if identity.state != "active" - || identity.key_id != workspace_key_id - || identity.public_key_fingerprint.as_deref() != Some(workspace_public_key_fingerprint) - || !self - .store - .workspace_runtime_verification_matches( - &binding, - workspace_public_key_fingerprint, - workspace_trust_id, - ) - .map_err(|error| { - diagnostic( - "workspace_runtime_authorization_unavailable", - HostDiagnosticSeverity::Error, - error.to_string(), - ) - })? - { + if identity.state != "active" { return Err(diagnostic( - "workspace_runtime_authorization_stale", + "workspace_signing_identity_unavailable", HostDiagnosticSeverity::Error, - "Workspace signing identity no longer matches the verified binding".to_string(), + "Workspace signing identity is not active".to_string(), )); } + let fingerprint = identity.public_key_fingerprint.ok_or_else(|| { + diagnostic( + "workspace_signing_identity_unavailable", + HostDiagnosticSeverity::Error, + "Workspace signing identity has no public key fingerprint".to_string(), + ) + })?; let now = Utc::now().timestamp(); let claims = WorkspaceCapabilityClaims { issuer: self.backend_url.clone(), issuer_workspace_id: binding.workspace_id.clone(), issuer_key_id: identity.key_id, - issuer_public_key_fingerprint: workspace_public_key_fingerprint.to_string(), - trust_id: workspace_trust_id.to_string(), - binding_id: binding.binding_id.clone(), + issuer_public_key_fingerprint: fingerprint, runtime_id: binding.runtime_id.clone(), worker_id: worker_id.map(str::to_string), operation: operation.to_string(), @@ -3674,6 +3543,8 @@ impl WorkspaceRuntimeAuthorization { exp: now.saturating_add(60), jti: uuid::Uuid::now_v7().to_string(), }; + // The signer validates that these claims still name the selected current + // identity; a concurrent rotation cannot silently sign mismatched claims. self.signing_identities .issue_workspace_capability(&binding.workspace_id, &claims) .map_err(|error| { @@ -4251,49 +4122,6 @@ impl RemoteWorkerRuntime { format!("{base}/v1/workers/{worker_id}/protocol/ws") } - fn post_bearer_json( - &self, - path: &str, - body: &T, - bearer_token: &str, - ) -> Result - where - T: Serialize + ?Sized, - U: DeserializeOwned, - { - let response = self - .http - .post(self.endpoint(path)) - .bearer_auth(bearer_token) - .json(body) - .send() - .map_err(|error| { - RuntimePingFailure::new( - RuntimePingFailureKind::NetworkUnreachable, - "runtime_workspace_verification_unreachable", - format!("Runtime verification request failed: {error}"), - ) - })?; - let status = response.status(); - if !status.is_success() { - return Err(RuntimePingFailure::new( - RuntimePingFailureKind::MalformedResponse, - "runtime_workspace_verification_rejected", - format!( - "Runtime verification request returned HTTP {}", - status.as_u16() - ), - )); - } - response.json::().map_err(|error| { - RuntimePingFailure::new( - RuntimePingFailureKind::MalformedResponse, - "runtime_workspace_verification_invalid_response", - format!("Runtime verification response was invalid: {error}"), - ) - }) - } - fn get_json(&self, path: &str) -> Result where T: DeserializeOwned + Send + 'static, @@ -4702,36 +4530,6 @@ impl WorkspaceWorkerRuntime for RemoteWorkerRuntime { }) } - fn activate_workspace_authorization(&self, binding: crate::store::WorkspaceRuntimeBinding) { - if let Some(authorization) = &self.workspace_authorization { - authorization.activate(binding); - } - } - - fn send_workspace_verification_challenge( - &self, - challenge: &WorkspaceRuntimeVerificationChallenge, - bearer_token: &str, - ) -> Result { - self.post_bearer_json( - worker_runtime::workspace_issuer::WORKSPACE_VERIFICATION_CHALLENGE_PATH, - challenge, - bearer_token, - ) - } - - fn send_workspace_verification_acknowledgement( - &self, - acknowledgement: &WorkspaceRuntimeVerificationAcknowledgement, - bearer_token: &str, - ) -> Result { - self.post_bearer_json( - worker_runtime::workspace_issuer::WORKSPACE_VERIFICATION_ACK_PATH, - acknowledgement, - bearer_token, - ) - } - fn list_hosts(&self, limit: usize) -> RuntimeList { if limit == 0 { return RuntimeList::new(Vec::new(), Vec::new()); @@ -6536,6 +6334,7 @@ pub fn placeholder_spawn_response(host_id: impl Into) -> WorkerSpawnResu #[cfg(test)] mod tests { mod restore_auth_tests; + mod workspace_auth_tests; use super::*; use serde_json::json; diff --git a/crates/workspace-server/src/hosts/tests/restore_auth_tests.rs b/crates/workspace-server/src/hosts/tests/restore_auth_tests.rs index 82a9db0b9..8e77550f5 100644 --- a/crates/workspace-server/src/hosts/tests/restore_auth_tests.rs +++ b/crates/workspace-server/src/hosts/tests/restore_auth_tests.rs @@ -9,36 +9,18 @@ use worker_runtime::{ auth::RuntimeIdentityMaterial, http_server::{WorkspaceRuntimeHttpAuth, runtime_http_router_with_workspace_auth}, workspace_issuer::{ - InMemoryWorkspaceClaimReplayProtection, InMemoryWorkspaceRuntimeVerificationAuthority, - RuntimeVerificationSigner, WorkspaceCapabilityVerifier, WorkspaceIssuerTrustRecord, - WorkspaceIssuerTrustState, WorkspaceRuntimeVerificationAuthority, - WorkspaceRuntimeVerificationRecord, issue_workspace_capability_token, + InMemoryWorkspaceClaimReplayProtection, WorkspaceCapabilityVerifier, + WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, issue_workspace_capability_token, }, }; #[tokio::test] async fn restore_capabilities_authorize_runtime_routes_without_relaxing_request_binding() { let workspace_identity = RuntimeIdentityMaterial::generate("workspace-key").unwrap(); - let runtime_identity = RuntimeIdentityMaterial::generate("runtime-test").unwrap(); - let signer = RuntimeVerificationSigner::from_identity(&runtime_identity).unwrap(); let now = Utc::now().timestamp(); let workspace_public_key_fingerprint = crate::workspace_signing_identity::public_key_fingerprint(&workspace_identity.public_key) .unwrap(); - let binding_id = "binding-restore-test".to_string(); - let verifications = Arc::new(InMemoryWorkspaceRuntimeVerificationAuthority::default()); - verifications - .record(WorkspaceRuntimeVerificationRecord { - workspace_id: "workspace-test".into(), - runtime_id: "runtime-test".into(), - binding_id: binding_id.clone(), - workspace_trust_id: "trust-restore-test".to_string(), - workspace_key_id: "workspace-key".into(), - workspace_public_key_fingerprint: workspace_public_key_fingerprint.clone(), - runtime_public_key_fingerprint: signer.public_key_fingerprint().to_owned(), - verified_at: now, - }) - .unwrap(); let public_key = worker_runtime::auth::decode_public_key(&workspace_identity.public_key).unwrap(); let verifier = WorkspaceCapabilityVerifier::new( @@ -69,8 +51,7 @@ async fn restore_capabilities_authorize_runtime_routes_without_relaxing_request_ None, WorkspaceRuntimeHttpAuth { verifier, - signer, - verifications, + runtime_id: "runtime-test".into(), }, ); let worker_id = EmbeddedWorkerId::from_legacy_u64(1).to_string(); @@ -93,8 +74,6 @@ async fn restore_capabilities_authorize_runtime_routes_without_relaxing_request_ "wrong-worker", "wrong-body", "wrong-key-fingerprint", - "wrong-binding-id", - "wrong-trust-id", ] { let claims = WorkspaceCapabilityClaims { issuer: "https://backend.test".into(), @@ -111,16 +90,6 @@ async fn restore_capabilities_authorize_runtime_routes_without_relaxing_request_ } else { workspace_public_key_fingerprint.clone() }, - binding_id: if case == "wrong-binding-id" { - "binding-retired".to_string() - } else { - binding_id.clone() - }, - trust_id: if case == "wrong-trust-id" { - "trust-retired".to_string() - } else { - "trust-restore-test".to_string() - }, runtime_id: "runtime-test".into(), worker_id: if case == "wrong-worker" { Some(EmbeddedWorkerId::from_legacy_u64(2).to_string()) @@ -176,27 +145,16 @@ async fn restore_capabilities_authorize_runtime_routes_without_relaxing_request_ } else { assert_eq!( status, - if case == "wrong-binding-id" { - StatusCode::FORBIDDEN - } else { - StatusCode::UNAUTHORIZED - }, + StatusCode::UNAUTHORIZED, "{path} {case}: {}", error.error.message ); - assert_eq!( - error.error.code, - if case == "wrong-binding-id" { - "workspace_runtime_verification_stale" - } else { - "unauthorized" - } - ); + assert_eq!(error.error.code, "unauthorized"); let expected = match case { "wrong-operation" => Some("does not authorize this operation"), "wrong-worker" => Some("targets another Worker"), "wrong-body" => Some("does not bind this request body"), - "wrong-key-fingerprint" | "wrong-binding-id" | "wrong-trust-id" => None, + "wrong-key-fingerprint" => None, _ => unreachable!(), }; if let Some(expected) = expected { diff --git a/crates/workspace-server/src/hosts/tests/workspace_auth_tests.rs b/crates/workspace-server/src/hosts/tests/workspace_auth_tests.rs new file mode 100644 index 000000000..f68964f35 --- /dev/null +++ b/crates/workspace-server/src/hosts/tests/workspace_auth_tests.rs @@ -0,0 +1,316 @@ +use super::*; +use crate::store::{ + AccountRecord, SqliteWorkspaceStore, WorkspaceRecord, WorkspaceRuntimeBinding, + WorkspaceSigningIdentityRecord, +}; +use crate::workspace_signing_identity::{ + InMemoryWorkspaceSigningMaterialStore, WorkspaceSigningMaterialStore, + WorkspaceSigningPrivateMaterial, public_key_fingerprint, +}; +use worker_runtime::auth::RuntimeIdentityMaterial; +use worker_runtime::workspace_issuer::{ + InMemoryWorkspaceClaimReplayProtection, VerifiedWorkspaceCapability, + WorkspaceCapabilityExpectation, WorkspaceCapabilityVerificationError, + WorkspaceCapabilityVerifier, WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, +}; + +const WORKSPACE: &str = "workspace-auth-test"; +const RUNTIME: &str = "runtime-auth-test"; +const BACKEND: &str = "https://backend.test"; +const PATH: &str = "/v1/ping?probe=1"; + +struct Fixture { + _temp: tempfile::TempDir, + store: Arc, + materials: Arc, + service: WorkspaceSigningIdentityService, + authorizer: WorkspaceRuntimeAuthorization, + binding: WorkspaceRuntimeBinding, + identity: WorkspaceSigningIdentityRecord, +} + +impl Fixture { + async fn new() -> Self { + let temp = tempfile::tempdir().unwrap(); + let store = Arc::new(SqliteWorkspaceStore::open(temp.path().join("server.db")).unwrap()); + store + .upsert_account(&AccountRecord { + account_id: "owner".into(), + kind: "user".into(), + handle: "owner".into(), + display_name: "Owner".into(), + created_at: "1".into(), + updated_at: "1".into(), + }) + .unwrap(); + store + .upsert_workspace(&WorkspaceRecord { + workspace_id: WORKSPACE.into(), + owner_account_id: "owner".into(), + display_name: "Workspace".into(), + state: "active".into(), + created_at: "1".into(), + updated_at: "1".into(), + }) + .await + .unwrap(); + let materials = Arc::new(InMemoryWorkspaceSigningMaterialStore::default()); + let service = WorkspaceSigningIdentityService::new(store.clone(), materials.clone()); + let identity = service.provision_existing(WORKSPACE, "owner").unwrap(); + let runtime_identity = RuntimeIdentityMaterial::generate(RUNTIME).unwrap(); + store + .upsert_workspace_runtime_binding( + WorkspaceRuntimeBinding { + workspace_id: WORKSPACE.into(), + runtime_id: RUNTIME.into(), + display_name: "Runtime".into(), + base_url: "https://runtime.test".into(), + public_key_fingerprint: public_key_fingerprint(&runtime_identity.public_key) + .unwrap(), + public_key: runtime_identity.public_key, + binding_id: String::new(), + authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, + created_at: "1".into(), + updated_at: "1".into(), + revoked_at: None, + }, + false, + ) + .unwrap(); + let binding = store + .get_workspace_runtime_binding(WORKSPACE, RUNTIME) + .unwrap() + .unwrap(); + let authorizer = WorkspaceRuntimeAuthorization::new( + store.clone(), + service.clone(), + BACKEND, + binding.clone(), + ); + Self { + _temp: temp, + store, + materials, + service, + authorizer, + binding, + identity, + } + } + + fn issue(&self) -> Result { + self.authorizer + .issue("GET", PATH, RUNTIME_PING_PERMISSION, None, b"") + } + + fn verifier(&self) -> WorkspaceCapabilityVerifier { + WorkspaceCapabilityVerifier::new( + vec![WorkspaceIssuerTrustRecord { + workspace_id: WORKSPACE.into(), + backend_url: BACKEND.into(), + key_id: self.identity.key_id.clone(), + algorithm: "ed25519".into(), + public_key: self.identity.public_key.clone().unwrap(), + public_key_fingerprint: self.identity.public_key_fingerprint.clone().unwrap(), + trust_id: uuid::Uuid::now_v7().to_string(), + state: WorkspaceIssuerTrustState::Active, + registered_at_unix: Utc::now().timestamp(), + updated_at_unix: Utc::now().timestamp(), + }], + Arc::new(InMemoryWorkspaceClaimReplayProtection::default()), + ) + .unwrap() + } + + // Fixture-only rotation: deliberately no production rotation API or live database. + fn rotate_identity(&mut self) { + let key_id = "WK-rotated"; + let material_ref = format!("workspace-signing/{WORKSPACE}/{key_id}"); + let material = WorkspaceSigningPrivateMaterial::generate(WORKSPACE, key_id).unwrap(); + let public_key = material.validate_and_public_key(WORKSPACE, key_id).unwrap(); + let fingerprint = public_key_fingerprint(&public_key).unwrap(); + self.materials + .put_if_absent(&material_ref, &material) + .unwrap(); + self.store.with_conn(|conn| { + conn.execute("UPDATE workspace_signing_identities SET key_id = ?1, public_key = ?2, public_key_fingerprint = ?3, private_material_ref = ?4 WHERE workspace_id = ?5", + rusqlite::params![key_id, public_key, fingerprint, material_ref, WORKSPACE])?; + Ok(()) + }).unwrap(); + self.identity = self.service.get_validated(WORKSPACE).unwrap(); + } +} + +fn verify( + verifier: &WorkspaceCapabilityVerifier, + token: &str, +) -> Result { + verifier.verify( + token, + &WorkspaceCapabilityExpectation { + workspace_id: WORKSPACE, + runtime_id: RUNTIME, + worker_id: None, + operation: RUNTIME_PING_PERMISSION, + method: "GET", + path_and_query: PATH, + body_digest: &workspace_request_body_digest(b""), + now_unix: Utc::now().timestamp(), + }, + ) +} + +#[tokio::test] +async fn authorizer_token_authenticates_actual_request_without_prior_verification_record() { + let fixture = Fixture::new().await; + let token = fixture.issue().unwrap(); + let verifier = fixture.verifier(); + let authenticated = verify(&verifier, &token).unwrap(); + assert_eq!(authenticated.workspace_id, WORKSPACE); + assert_eq!(authenticated.issuer_key_id, fixture.identity.key_id); + assert_eq!( + verify(&verifier, &token).unwrap_err(), + WorkspaceCapabilityVerificationError::Replay + ); + let token = fixture.issue().unwrap(); + let wrong_request = WorkspaceCapabilityExpectation { + workspace_id: WORKSPACE, + runtime_id: RUNTIME, + worker_id: None, + operation: RUNTIME_PING_PERMISSION, + method: "GET", + path_and_query: "/v1/ping?probe=2", + body_digest: &workspace_request_body_digest(b""), + now_unix: Utc::now().timestamp(), + }; + assert_eq!( + verifier.verify(&token, &wrong_request).unwrap_err(), + WorkspaceCapabilityVerificationError::WrongPathAndQuery + ); +} + +#[tokio::test] +async fn existing_authorizer_selects_rotated_workspace_key_and_new_runtime_trust() { + let mut fixture = Fixture::new().await; + let old_verifier = fixture.verifier(); + let old_token = fixture.issue().unwrap(); + fixture.rotate_identity(); + let new_token = fixture.issue().unwrap(); + let new_verifier = fixture.verifier(); + assert_eq!( + verify(&new_verifier, &new_token).unwrap().issuer_key_id, + "WK-rotated" + ); + assert_eq!( + verify(&new_verifier, &old_token).unwrap_err(), + WorkspaceCapabilityVerificationError::WrongIssuerKey + ); + assert_eq!( + verify(&old_verifier, &new_token).unwrap_err(), + WorkspaceCapabilityVerificationError::WrongIssuerKey + ); + // Changing Workspace authority did not replace or copy metadata into the connection. + assert_eq!( + fixture + .store + .get_workspace_runtime_binding(WORKSPACE, RUNTIME) + .unwrap() + .unwrap(), + fixture.binding + ); +} + +#[tokio::test] +async fn replaced_or_revoked_connection_rejects_stale_authorizer() { + for revoked in [false, true] { + let fixture = Fixture::new().await; + fixture.issue().unwrap(); + if revoked { + fixture + .store + .revoke_workspace_runtime_binding(WORKSPACE, RUNTIME, "2") + .unwrap(); + } else { + let mut replacement = fixture.binding.clone(); + replacement.base_url = "https://replacement-runtime.test".into(); + fixture + .store + .upsert_workspace_runtime_binding(replacement, true) + .unwrap(); + } + assert_eq!( + fixture.issue().unwrap_err().code, + "workspace_runtime_authorization_stale" + ); + } +} + +#[tokio::test] +async fn missing_workspace_signing_material_rejects_authorization_without_fallback() { + let fixture = Fixture::new().await; + fixture + .materials + .delete(&fixture.identity.private_material_ref) + .unwrap(); + let error = fixture.issue().unwrap_err(); + assert_eq!(error.code, "workspace_runtime_authorization_unavailable"); + assert!( + error.message.contains("private material is missing"), + "{}", + error.message + ); +} + +#[tokio::test] +async fn capability_claims_must_match_current_workspace_identity_before_signing() { + let mut fixture = Fixture::new().await; + let now = Utc::now().timestamp(); + let mut claims = WorkspaceCapabilityClaims { + issuer: BACKEND.into(), + issuer_workspace_id: WORKSPACE.into(), + issuer_key_id: fixture.identity.key_id.clone(), + issuer_public_key_fingerprint: fixture.identity.public_key_fingerprint.clone().unwrap(), + runtime_id: RUNTIME.into(), + worker_id: None, + operation: RUNTIME_PING_PERMISSION.into(), + method: "GET".into(), + path_and_query: PATH.into(), + body_digest: workspace_request_body_digest(b""), + iat: now, + exp: now + 60, + jti: uuid::Uuid::now_v7().to_string(), + }; + let token = fixture + .service + .issue_workspace_capability(WORKSPACE, &claims) + .unwrap(); + verify(&fixture.verifier(), &token).unwrap(); + for field in ["workspace", "key", "fingerprint"] { + let mut mismatched = claims.clone(); + match field { + "workspace" => mismatched.issuer_workspace_id = "another-workspace".into(), + "key" => mismatched.issuer_key_id = "WK-another".into(), + "fingerprint" => { + mismatched.issuer_public_key_fingerprint = format!("sha256:{}", "0".repeat(64)) + } + _ => unreachable!(), + } + assert!( + matches!(fixture.service.issue_workspace_capability(WORKSPACE, &mismatched), + Err(Error::WorkspaceSigningIdentity { ref code, .. }) if code == "workspace_capability_identity_mismatch"), + "{field}" + ); + } + fixture.rotate_identity(); + assert!( + matches!(fixture.service.issue_workspace_capability(WORKSPACE, &claims), + Err(Error::WorkspaceSigningIdentity { ref code, .. }) if code == "workspace_capability_identity_mismatch") + ); + claims.issuer_key_id = fixture.identity.key_id.clone(); + claims.issuer_public_key_fingerprint = fixture.identity.public_key_fingerprint.clone().unwrap(); + let token = fixture + .service + .issue_workspace_capability(WORKSPACE, &claims) + .unwrap(); + verify(&fixture.verifier(), &token).unwrap(); +} diff --git a/crates/workspace-server/src/latest_schema.sql b/crates/workspace-server/src/latest_schema.sql index 0bb658c1e..859f23587 100644 --- a/crates/workspace-server/src/latest_schema.sql +++ b/crates/workspace-server/src/latest_schema.sql @@ -442,49 +442,43 @@ CREATE TABLE workspace_runtime_bindings ( public_key TEXT NOT NULL, public_key_fingerprint TEXT NOT NULL, binding_id TEXT NOT NULL CHECK (length(binding_id) > 0), - state TEXT NOT NULL CHECK (state IN ('configured', 'verified', 'revoked')), authentication_mode TEXT NOT NULL CHECK (authentication_mode IN ('legacy_server_issuer', 'workspace_identity')), - workspace_key_id TEXT, - workspace_public_key_fingerprint TEXT, - workspace_trust_id TEXT CHECK (length(workspace_trust_id) > 0), created_at TEXT NOT NULL, updated_at TEXT NOT NULL, revoked_at TEXT, PRIMARY KEY (workspace_id, runtime_id), UNIQUE (workspace_id, public_key_fingerprint), - FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE RESTRICT, - CHECK ( - (authentication_mode = 'legacy_server_issuer' AND workspace_key_id IS NULL AND workspace_public_key_fingerprint IS NULL AND workspace_trust_id IS NULL) - OR - (authentication_mode = 'workspace_identity' AND workspace_key_id IS NOT NULL AND (state != 'verified' OR (workspace_trust_id IS NOT NULL AND workspace_public_key_fingerprint IS NOT NULL))) - ), - CHECK ( - (state = 'revoked' AND revoked_at IS NOT NULL) - OR - (state != 'revoked' AND revoked_at IS NULL) - ) + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE RESTRICT ); -CREATE TABLE workspace_runtime_verifications ( +-- Historical authentication metadata only, never current admission authority. +CREATE TABLE workspace_runtime_binding_verification_archive ( workspace_id TEXT NOT NULL, runtime_id TEXT NOT NULL, - binding_id TEXT NOT NULL CHECK(length(binding_id) > 0), + binding_id TEXT NOT NULL, + state TEXT NOT NULL, + workspace_key_id TEXT, + workspace_public_key_fingerprint TEXT, + workspace_trust_id TEXT, + updated_at TEXT NOT NULL, + PRIMARY KEY (workspace_id, runtime_id, binding_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE CASCADE +); +CREATE TABLE workspace_runtime_verification_archive ( + workspace_id TEXT NOT NULL, + runtime_id TEXT NOT NULL, + binding_id TEXT NOT NULL, workspace_key_id TEXT NOT NULL, - workspace_public_key_fingerprint TEXT NOT NULL CHECK(length(workspace_public_key_fingerprint) > 0), - workspace_trust_id TEXT NOT NULL CHECK(length(workspace_trust_id) > 0), + workspace_public_key_fingerprint TEXT NOT NULL, + workspace_trust_id TEXT NOT NULL, runtime_public_key_fingerprint TEXT NOT NULL, challenge_id TEXT NOT NULL, - state TEXT NOT NULL CHECK(state IN ('pending', 'verified', 'failed')), + state TEXT NOT NULL, last_outcome TEXT NOT NULL, verified_at TEXT, checked_at TEXT NOT NULL, - PRIMARY KEY(workspace_id, runtime_id), - FOREIGN KEY(workspace_id, runtime_id) - REFERENCES workspace_runtime_bindings(workspace_id, runtime_id) ON DELETE CASCADE, - CHECK((state = 'verified' AND verified_at IS NOT NULL) - OR (state != 'verified' AND verified_at IS NULL)) + PRIMARY KEY (workspace_id, runtime_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE CASCADE ); -CREATE INDEX workspace_runtime_verifications_state_idx - ON workspace_runtime_verifications(workspace_id, state, checked_at DESC); CREATE TABLE workspace_runtime_binding_audit ( workspace_id TEXT NOT NULL, runtime_id TEXT NOT NULL, @@ -1699,7 +1693,7 @@ CREATE TABLE IF NOT EXISTS ticket_assignment_work_releases ( FOREIGN KEY (workspace_id, assignment_id) REFERENCES ticket_worker_assignments(workspace_id, assignment_id) ON DELETE CASCADE ); --- Ticket case assignment work constraints (schema 90). +-- Ticket case assignment work constraints (schema 91). DROP VIEW IF EXISTS ticket_active_worker_assignments; CREATE VIEW ticket_active_worker_assignments AS SELECT current.* FROM ticket_current_worker_assignments AS current diff --git a/crates/workspace-server/src/main.rs b/crates/workspace-server/src/main.rs index 8858a8cec..f68f9f639 100644 --- a/crates/workspace-server/src/main.rs +++ b/crates/workspace-server/src/main.rs @@ -10,7 +10,7 @@ use yoi_workspace_server::hosts::{ }; use yoi_workspace_server::store::{ AccountRecord, ApiTokenRecord, SqliteWorkspaceStore, UserRecord, - WorkspaceRuntimeAuthenticationMode, WorkspaceRuntimeBinding, WorkspaceRuntimeBindingState, + WorkspaceRuntimeAuthenticationMode, WorkspaceRuntimeBinding, }; use yoi_workspace_server::{ ControlPlaneStore, ResolvedWorkspaceBackendConfig, ServerConfig, ServerHostConfigFile, @@ -456,7 +456,7 @@ fn append_workspace_runtime_sources( .into_iter() .filter(|binding| { binding.runtime_id != yoi_workspace_server::hosts::EMBEDDED_RUNTIME_ID - && binding.state == WorkspaceRuntimeBindingState::Verified + && binding.revoked_at.is_none() }) .collect::>() }) @@ -787,11 +787,7 @@ mod tests { public_key: "unused".to_owned(), public_key_fingerprint: "unused".to_owned(), binding_id: "binding-test".to_owned(), - state: WorkspaceRuntimeBindingState::Verified, authentication_mode: WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - workspace_public_key_fingerprint: None, - workspace_trust_id: None, created_at: "2026-09-01T00:00:00Z".to_owned(), updated_at: "2026-09-01T00:00:00Z".to_owned(), revoked_at: None, @@ -815,11 +811,7 @@ mod tests { public_key: "unused".to_owned(), public_key_fingerprint: "unused".to_owned(), binding_id: "binding-test".to_owned(), - state: WorkspaceRuntimeBindingState::Verified, authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-test".to_owned()), - workspace_public_key_fingerprint: Some("sha256:workspace-key".to_owned()), - workspace_trust_id: Some("trust-test".to_owned()), created_at: "2026-09-01T00:00:00Z".to_owned(), updated_at: "2026-09-01T00:00:00Z".to_owned(), revoked_at: None, diff --git a/crates/workspace-server/src/migration_entrypoint_tests.rs b/crates/workspace-server/src/migration_entrypoint_tests.rs index 7601633e0..d0eb2375d 100644 --- a/crates/workspace-server/src/migration_entrypoint_tests.rs +++ b/crates/workspace-server/src/migration_entrypoint_tests.rs @@ -80,10 +80,10 @@ fn migration_dry_run_and_apply_share_the_original_repository_secret_authority() let conn = Connection::open(&path).unwrap(); configure_sqlite(&conn).unwrap(); seed_migration_workspace(&conn); - conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','create-key','intent','credential','key',7,'created'); - INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519','fingerprint',7,'active','created',NULL); - INSERT INTO repository_ssh_credential_revisions VALUES('space','key',7,'ssh-ed25519','fingerprint','created'); - INSERT INTO repository_secret_audit_events VALUES('space','audit','credential_created','key',7,'owner','created');").unwrap(); + conn.execute_batch("INSERT INTO repository_secret_operations VALUES('space','create-key','intent','credential','key',7,'2026-01-01T00:00:00.000Z'); + INSERT INTO repository_ssh_credentials VALUES('space','key','Key','ssh-ed25519','fingerprint',7,'active','2026-01-01T00:00:00.000Z',NULL); + INSERT INTO repository_ssh_credential_revisions VALUES('space','key',7,'ssh-ed25519','fingerprint','2026-01-01T00:00:00.000Z'); + INSERT INTO repository_secret_audit_events VALUES('space','audit','credential_created','key',7,'owner','2026-01-01T00:00:00.000Z');").unwrap(); let key = LessSafeKey::new(UnboundKey::new(&AES_256_GCM, &[42; 32]).unwrap()); let mut ciphertext = b"private bytes".to_vec(); key.seal_in_place_append_tag( @@ -92,7 +92,7 @@ fn migration_dry_run_and_apply_share_the_original_repository_secret_authority() &mut ciphertext, ) .unwrap(); - conn.execute("INSERT INTO server_secret_versions VALUES('space','key',7,'private_key','aes-256-gcm-v1',?1,?2,'created')",params![[1u8;12].as_slice(),ciphertext]).unwrap(); + conn.execute("INSERT INTO server_secret_versions VALUES('space','key',7,'private_key','aes-256-gcm-v1',?1,?2,'2026-01-01T00:00:00.000Z')",params![[1u8;12].as_slice(),ciphertext]).unwrap(); drop(conn); let master_key = dir.path().join("repository-secrets.master-key"); std::fs::write(&master_key, [42; 32]).unwrap(); diff --git a/crates/workspace-server/src/schema_equivalence_tests.rs b/crates/workspace-server/src/schema_equivalence_tests.rs index 5e9618f19..3d98af689 100644 --- a/crates/workspace-server/src/schema_equivalence_tests.rs +++ b/crates/workspace-server/src/schema_equivalence_tests.rs @@ -53,7 +53,8 @@ fn fresh_and_upgraded_state_contract_schemas_enforce_the_same_constraints() { "flow_source_contents", "repositories", "workspace_runtime_bindings", - "workspace_runtime_verifications", + "workspace_runtime_binding_verification_archive", + "workspace_runtime_verification_archive", "workspace_runtime_binding_audit", "workspace_signing_identities", "workspace_signing_identity_provisioning_operations", diff --git a/crates/workspace-server/src/server.rs b/crates/workspace-server/src/server.rs index ed9350433..3231ba3da 100644 --- a/crates/workspace-server/src/server.rs +++ b/crates/workspace-server/src/server.rs @@ -64,9 +64,9 @@ use server_api::{ RepositorySshConnectionTrustState, RepositorySshCredential, RepositorySshHostKeyCandidate, RepositorySshHostTrust, RepositorySshPublicKey, RequestActor, RotateRepositorySshCredentialRequest, RuntimeCleanupExecutionResponse, - RuntimeCleanupExecutionResult, RuntimeCleanupPlanResponse, RuntimeConnectionDisplayState, - RuntimeConnectionTestFailureKind, RuntimeConnectionTestResponse, RuntimeConnectionTestStatus, - RuntimeManagementSummary, RuntimeRemovalOperationResponse, + RuntimeCleanupExecutionResult, RuntimeCleanupPlanResponse, RuntimeConnectionTestFailureKind, + RuntimeConnectionTestResponse, RuntimeConnectionTestStatus, RuntimeManagementSummary, + RuntimeRemovalOperationResponse, RuntimeRemovalOperationState as ApiRuntimeRemovalOperationState, RuntimeTrustAuditAction, RuntimeTrustAuditEntry, RuntimeTrustKeyRevealResponse, RuntimeTrustKeyState, RuntimeTrustKeyStatus, UpdateRemoteRuntimeRequest, UpdateWorkspaceMetadataRequest, @@ -83,11 +83,10 @@ use server_api::{ WorkspaceDeletionPreflightResponse, WorkspaceDeletionRequest, WorkspaceDeletionState, WorkspaceExtensionPointState, WorkspaceExtensionPoints, WorkspaceMetadataMutationResponse, WorkspaceMetadataSettingsResponse, WorkspacePermissionSummary, WorkspacePublicIdentityBundle, - WorkspaceRepositoryRecord, WorkspaceResponse, WorkspaceRuntimeBindingState, - WorkspaceRuntimeBindingSummary, WorkspaceRuntimeDetail, WorkspaceRuntimeResource, - WorkspaceSigningIdentityPublic, WorkspaceSigningIdentityResponse, - WorkspaceSigningIdentityState, WorkspaceSummary, WorkspaceWorkerDiscoveryItem, - WorkspaceWorkerDiscoveryPage, WorkspaceWorkerSubject, + WorkspaceRepositoryRecord, WorkspaceResponse, WorkspaceRuntimeBindingSummary, + WorkspaceRuntimeDetail, WorkspaceRuntimeResource, WorkspaceSigningIdentityPublic, + WorkspaceSigningIdentityResponse, WorkspaceSigningIdentityState, WorkspaceSummary, + WorkspaceWorkerDiscoveryItem, WorkspaceWorkerDiscoveryPage, WorkspaceWorkerSubject, }; use server_api::{ AttachmentUploadCancelResponse, AttachmentUploadGrantResponse, RestoreTicketAssignmentQuery, @@ -141,12 +140,6 @@ use worker_runtime::http_server::{ use worker_runtime::resource::BackendResourceError; use worker_runtime::resource::BackendResourceFetchRequest; use worker_runtime::worker_backend::{ProfileRuntimeWorkerFactory, WorkerRuntimeExecutionBackend}; -use worker_runtime::workspace_issuer::{ - WORKSPACE_VERIFICATION_ACK_PATH, WORKSPACE_VERIFICATION_CHALLENGE_PATH, - WORKSPACE_VERIFICATION_OPERATION, WorkspaceCapabilityClaims, - WorkspaceRuntimeVerificationAcknowledgement, WorkspaceRuntimeVerificationChallenge, - verify_runtime_verification_response, workspace_request_body_digest, -}; use crate::auth::{ SessionCookiePolicy, auth_error, is_expired, mint_secret, new_id, new_user_code, @@ -219,7 +212,6 @@ use crate::store::{ WorkerControlGrantRecord, WorkerRegistryRecord, WorkerWorkdirLinkRecord, WorkspaceRecord, WorkspaceResourceKind, WorkspaceRuntimeAuthenticationMode as StoredRuntimeAuthenticationMode, WorkspaceRuntimeBinding, WorkspaceRuntimeBindingAuditRecord, WorkspaceRuntimeBindingMutation, - WorkspaceRuntimeBindingState as StoredRuntimeBindingState, }; use crate::ticket_item_checker::{self, TicketItemCheckBodyReplacement, TicketItemCheckEdit}; use crate::workdir_removal::{ @@ -3311,13 +3303,7 @@ impl WorkspaceApi { public_key: embedded_identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: config.workspace_created_at.clone(), updated_at: config.workspace_created_at.clone(), revoked_at: None, @@ -3361,7 +3347,7 @@ impl WorkspaceApi { .await? .into_iter() .filter(|binding| binding.runtime_id != EMBEDDED_RUNTIME_ID) - .filter(|binding| binding.state != StoredRuntimeBindingState::Revoked) + .filter(|binding| binding.revoked_at.is_none()) .map(|binding| { ( (binding.workspace_id.clone(), binding.runtime_id.clone()), @@ -3416,10 +3402,9 @@ impl WorkspaceApi { binding.authentication_mode == crate::store::WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity }) - .filter(|binding| binding.state != StoredRuntimeBindingState::Revoked) + .filter(|binding| binding.revoked_at.is_none()) { - let activate = binding.state == StoredRuntimeBindingState::Verified; - api.register_workspace_runtime_binding(binding, activate)?; + api.register_workspace_runtime_binding(binding)?; } Ok(api) } @@ -3498,13 +3483,13 @@ impl WorkspaceApi { binding.authentication_mode == StoredRuntimeAuthenticationMode::WorkspaceIdentity && binding.revoked_at.is_none() }) { - let verified_binding = current_binding - .filter(|binding| binding.state == StoredRuntimeBindingState::Verified); + let binding = + current_binding.expect("Workspace identity binding was checked above"); remote_config.workspace_authorization = Some(WorkspaceRuntimeAuthorization::new( store.clone(), signing_identities.clone(), backend_url.clone(), - verified_binding, + binding, )); } let remote_runtime = RemoteWorkerRuntime::new( @@ -4746,11 +4731,7 @@ impl WorkspaceApi { ); } - fn register_workspace_runtime_binding( - &self, - binding: WorkspaceRuntimeBinding, - activate: bool, - ) -> Result<()> { + fn register_workspace_runtime_binding(&self, binding: WorkspaceRuntimeBinding) -> Result<()> { let backend_url = self .config .backend_base_url @@ -4762,7 +4743,7 @@ impl WorkspaceApi { self.store.clone(), self.signing_identities.clone(), backend_url.clone(), - activate.then_some(binding), + binding, )); let remote_runtime = RemoteWorkerRuntime::new( remote_config.clone(), @@ -25238,7 +25219,6 @@ async fn scoped_update_remote_runtime( &Utc::now().to_rfc3339(), ) .await?; - let activate_runtime = binding.state == StoredRuntimeBindingState::Verified; api.runtime_binding_expectations .write() .unwrap_or_else(std::sync::PoisonError::into_inner) @@ -25248,7 +25228,7 @@ async fn scoped_update_remote_runtime( ); api.runtime_subscription_broker .unregister_runtime(&path.runtime_id); - api.register_workspace_runtime_binding(binding, activate_runtime)?; + api.register_workspace_runtime_binding(binding)?; Ok(Json( workspace_runtime_detail(&api, &path.workspace_id, &path.runtime_id).await?, )) @@ -25314,7 +25294,7 @@ async fn scoped_test_runtime_connection( .get_workspace_runtime_binding(&path.workspace_id, &path.runtime_id) .await? .ok_or_else(|| Error::UnknownRuntime(path.runtime_id.clone()))?; - if binding.state == StoredRuntimeBindingState::Revoked || binding.revoked_at.is_some() { + if binding.revoked_at.is_some() { return Err(Error::RuntimeBindingConflict(format!( "Runtime `{}` binding is revoked", path.runtime_id @@ -25329,11 +25309,6 @@ async fn scoped_test_runtime_connection( }, display_name: Some(binding.display_name.clone()), endpoint: binding.base_url.clone(), - workspace_trust_id: binding.workspace_trust_id.clone().ok_or_else(|| { - Error::RuntimeBindingConflict( - "Runtime binding is missing its Workspace trust ID".to_string(), - ) - })?, expected_binding_id: Some(binding.binding_id.clone()), }) .await?; @@ -26677,32 +26652,23 @@ async fn create_remote_runtime( "an active Workspace signing identity is required before registering a Runtime", )); } - let existing = api - .store - .get_workspace_runtime_binding(&api.config.workspace_id, &runtime_id) - .await?; - if existing - .as_ref() - .is_some_and(|binding| binding.state == StoredRuntimeBindingState::Verified) - { - return Err(Error::RuntimeBindingConflict( - "a verified Runtime binding must be revoked before it can be replaced as configured" - .to_string(), - ) - .into()); - } + // Protect observed active Workers without removing the current connection. + // This scan is not proof of idleness: an unavailable/auth-failing endpoint + // must remain repairable by an owner using binding-ID CAS below. Replacement + // neither stops nor deletes Workers; Runtime removal has separate preflight. + // Rejected key/CAS mutations must leave the current registration usable. match api .runtime - .unregister_if_idle(&runtime_id, api.config.max_records.min(200)) - .map_err(|err| err.into_error())? + .list_workers_for_runtime(&runtime_id, api.config.max_records.min(200)) { - RuntimeRegistryUnregisterResult::Removed | RuntimeRegistryUnregisterResult::NotFound => {} - RuntimeRegistryUnregisterResult::BlockedByWorkers { worker_count, .. } => { + Ok(workers) if !workers.items.is_empty() => { return Err(Error::RuntimeBindingConflict(format!( - "Runtime `{runtime_id}` still has {worker_count} active worker(s) and cannot become a configured-only binding" - )) - .into()); + "Runtime `{runtime_id}` still has {} active worker(s) and cannot replace its connection", + workers.items.len() + )).into()); } + Ok(_) | Err(RuntimeRegistryError::UnknownRuntime(_)) => {} + Err(error) => return Err(error.into_error().into()), } let now = Utc::now().to_rfc3339(); let record = WorkspaceRuntimeBinding { @@ -26719,13 +26685,7 @@ async fn create_remote_runtime( public_key: request.public_bundle.public_key.trim().to_string(), public_key_fingerprint: String::new(), binding_id: Uuid::now_v7().to_string(), - state: StoredRuntimeBindingState::Configured, authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some(identity.key_id.clone()), - - workspace_public_key_fingerprint: identity.public_key_fingerprint.clone(), - - workspace_trust_id: Some(request.workspace_trust_id.clone()), created_at: now.clone(), updated_at: now, revoked_at: None, @@ -26747,7 +26707,7 @@ async fn create_remote_runtime( ); api.runtime_subscription_broker .unregister_runtime(&runtime_id); - api.register_workspace_runtime_binding(binding, false)?; + api.register_workspace_runtime_binding(binding)?; let resource = workspace_runtime_resources_response(&api, &api.config.workspace_id) .await? .items @@ -26916,7 +26876,7 @@ async fn execute_runtime_removal( "runtime_removal_commit_failed", ) .await; - let restore_result = api.register_workspace_runtime_binding(binding, true); + let restore_result = api.register_workspace_runtime_binding(binding); if let Err(restore_error) = restore_result { tracing::warn!( workspace_id = %operation.workspace_id, @@ -27068,219 +27028,6 @@ async fn remove_remote_runtime( Ok(Json(runtime_removal_response(operation))) } -async fn perform_workspace_runtime_verification( - api: &WorkspaceApi, - runtime: Arc, - binding: &WorkspaceRuntimeBinding, -) -> std::result::Result { - if binding.authentication_mode != StoredRuntimeAuthenticationMode::WorkspaceIdentity { - return Ok(binding.clone()); - } - if binding.state == crate::store::WorkspaceRuntimeBindingState::Revoked - || binding.revoked_at.is_some() - { - return Err("Runtime binding is revoked".to_string()); - } - let backend_url = api.config.backend_base_url.as_deref().ok_or_else(|| { - "Workspace identity verification requires configured backend_base_url".to_string() - })?; - let identity = api - .signing_identities - .get_validated(&binding.workspace_id) - .map_err(|error| error.to_string())?; - if identity.state != "active" { - return Err("Workspace signing identity is not active".to_string()); - } - let workspace_key_id = binding - .workspace_key_id - .as_deref() - .ok_or_else(|| "Runtime binding is missing the Workspace key identity".to_string())?; - let workspace_trust_id = binding - .workspace_trust_id - .as_deref() - .ok_or_else(|| "Runtime binding is missing the Workspace trust ID".to_string())?; - let workspace_public_key_fingerprint = binding - .workspace_public_key_fingerprint - .as_deref() - .ok_or_else(|| { - "Runtime binding is missing the Workspace public key fingerprint".to_string() - })?; - if identity.key_id != workspace_key_id - || identity.public_key_fingerprint.as_deref() != Some(workspace_public_key_fingerprint) - { - return Err( - "Runtime binding no longer matches the active Workspace or Runtime identity" - .to_string(), - ); - } - - let now = Utc::now(); - let expires_at = (now + Duration::seconds(60)).timestamp(); - let challenge = WorkspaceRuntimeVerificationChallenge { - challenge_id: Uuid::now_v7().to_string(), - workspace_id: binding.workspace_id.clone(), - runtime_id: binding.runtime_id.clone(), - binding_id: binding.binding_id.clone(), - workspace_key_id: workspace_key_id.to_string(), - workspace_public_key_fingerprint: workspace_public_key_fingerprint.to_string(), - workspace_trust_id: workspace_trust_id.to_string(), - runtime_public_key_fingerprint: binding.public_key_fingerprint.clone(), - workspace_nonce: Uuid::now_v7().to_string(), - expires_at, - }; - let checked_at = now.to_rfc3339_opts(SecondsFormat::Millis, true); - let pending = crate::store::WorkspaceRuntimeVerificationEvidence { - workspace_id: binding.workspace_id.clone(), - runtime_id: binding.runtime_id.clone(), - binding_id: binding.binding_id.clone(), - workspace_key_id: workspace_key_id.to_string(), - workspace_public_key_fingerprint: workspace_public_key_fingerprint.to_string(), - workspace_trust_id: workspace_trust_id.to_string(), - runtime_public_key_fingerprint: binding.public_key_fingerprint.clone(), - challenge_id: challenge.challenge_id.clone(), - state: "pending".to_string(), - last_outcome: "challenge_issued".to_string(), - verified_at: None, - checked_at: checked_at.clone(), - }; - api.store - .record_workspace_runtime_verification_attempt(&pending) - .await - .map_err(|error| error.to_string())?; - - let result = async { - let challenge_body = serde_json::to_vec(&challenge).map_err(|error| error.to_string())?; - let challenge_claims = WorkspaceCapabilityClaims { - issuer: backend_url.to_string(), - issuer_workspace_id: binding.workspace_id.clone(), - issuer_key_id: identity.key_id.clone(), - issuer_public_key_fingerprint: workspace_public_key_fingerprint.to_string(), - trust_id: workspace_trust_id.to_string(), - binding_id: binding.binding_id.clone(), - runtime_id: binding.runtime_id.clone(), - worker_id: None, - operation: WORKSPACE_VERIFICATION_OPERATION.to_string(), - method: "POST".to_string(), - path_and_query: WORKSPACE_VERIFICATION_CHALLENGE_PATH.to_string(), - body_digest: workspace_request_body_digest(&challenge_body), - iat: now.timestamp(), - exp: expires_at, - jti: Uuid::now_v7().to_string(), - }; - let challenge_token = api - .signing_identities - .issue_workspace_capability(&binding.workspace_id, &challenge_claims) - .map_err(|error| error.to_string())?; - let challenge_runtime = runtime.clone(); - let challenge_runtime_id = binding.runtime_id.clone(); - let challenge_request = challenge.clone(); - let response = tokio::task::spawn_blocking(move || { - challenge_runtime.send_workspace_verification_challenge( - &challenge_runtime_id, - &challenge_request, - &challenge_token, - ) - }) - .await - .map_err(|_| "Runtime verification challenge task failed".to_string())? - .map_err(|failure| failure.diagnostic.message)?; - verify_runtime_verification_response( - &response, - &challenge, - &binding.public_key, - Utc::now().timestamp(), - ) - .map_err(|error| error.to_string())?; - - let response_bytes = serde_json::to_vec(&response).map_err(|error| error.to_string())?; - let acknowledgement = WorkspaceRuntimeVerificationAcknowledgement { - challenge_id: response.challenge_id.clone(), - workspace_id: response.workspace_id.clone(), - runtime_id: response.runtime_id.clone(), - binding_id: response.binding_id.clone(), - workspace_key_id: response.workspace_key_id.clone(), - workspace_public_key_fingerprint: response.workspace_public_key_fingerprint.clone(), - workspace_trust_id: response.workspace_trust_id.clone(), - runtime_public_key_fingerprint: response.runtime_public_key_fingerprint.clone(), - workspace_nonce: response.workspace_nonce.clone(), - runtime_nonce: response.runtime_nonce.clone(), - response_digest: workspace_request_body_digest(&response_bytes), - response: response.clone(), - expires_at: response.expires_at, - }; - let acknowledgement_body = - serde_json::to_vec(&acknowledgement).map_err(|error| error.to_string())?; - let acknowledgement_claims = WorkspaceCapabilityClaims { - issuer: backend_url.to_string(), - issuer_workspace_id: binding.workspace_id.clone(), - issuer_key_id: identity.key_id.clone(), - issuer_public_key_fingerprint: workspace_public_key_fingerprint.to_string(), - trust_id: workspace_trust_id.to_string(), - binding_id: binding.binding_id.clone(), - runtime_id: binding.runtime_id.clone(), - worker_id: None, - operation: WORKSPACE_VERIFICATION_OPERATION.to_string(), - method: "POST".to_string(), - path_and_query: WORKSPACE_VERIFICATION_ACK_PATH.to_string(), - body_digest: workspace_request_body_digest(&acknowledgement_body), - iat: Utc::now().timestamp(), - exp: expires_at, - jti: Uuid::now_v7().to_string(), - }; - let acknowledgement_token = api - .signing_identities - .issue_workspace_capability(&binding.workspace_id, &acknowledgement_claims) - .map_err(|error| error.to_string())?; - let acknowledgement_runtime = runtime; - let acknowledgement_runtime_id = binding.runtime_id.clone(); - let acknowledgement_request = acknowledgement.clone(); - let receipt = tokio::task::spawn_blocking(move || { - acknowledgement_runtime.send_workspace_verification_acknowledgement( - &acknowledgement_runtime_id, - &acknowledgement_request, - &acknowledgement_token, - ) - }) - .await - .map_err(|_| "Runtime verification acknowledgement task failed".to_string())? - .map_err(|failure| failure.diagnostic.message)?; - if receipt.challenge_id != challenge.challenge_id - || receipt.workspace_id != binding.workspace_id - || receipt.runtime_id != binding.runtime_id - || receipt.binding_id != binding.binding_id - { - return Err("Runtime verification acknowledgement receipt mismatched".to_string()); - } - - let verified_at = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true); - let verified = crate::store::WorkspaceRuntimeVerificationEvidence { - state: "verified".to_string(), - last_outcome: "verified".to_string(), - verified_at: Some(verified_at.clone()), - checked_at: verified_at, - ..pending.clone() - }; - api.store - .complete_workspace_runtime_verification(&verified) - .await - .map_err(|error| error.to_string()) - } - .await; - if result.is_err() { - let checked_at = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true); - let _ = api - .store - .record_workspace_runtime_verification_outcome_if_current( - &pending, - "failed", - "verification_failed", - &checked_at, - ) - .await; - } - result -} - async fn test_runtime_connection( State(api): State, AxumPath(runtime_id): AxumPath, @@ -27300,44 +27047,6 @@ async fn test_runtime_connection( .filter(|binding| binding.revoked_at.is_none()) .ok_or_else(|| Error::UnknownRuntime(runtime_id.clone()))?; - if binding.authentication_mode == StoredRuntimeAuthenticationMode::WorkspaceIdentity { - match perform_workspace_runtime_verification(&api, api.runtime.clone(), &binding).await { - Ok(verified_binding) => { - api.register_workspace_runtime_binding(verified_binding.clone(), true)?; - api.runtime_binding_expectations - .write() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .insert( - ( - verified_binding.workspace_id.clone(), - verified_binding.runtime_id.clone(), - ), - verified_binding.clone(), - ); - api.runtime - .activate_workspace_authorization(&runtime_id, verified_binding.clone()) - .map_err(|error| Error::Store(format!("{error:?}")))?; - } - Err(message) => { - let mut result = runtime_connection_test_failure( - api.workspace_id(), - &runtime_id, - Utc::now().to_rfc3339(), - RuntimeConnectionTestFailureKind::Authentication, - None, - RuntimeDiagnostic::new( - "runtime_workspace_verification_failed", - "error", - message, - ), - ); - result.binding_id = binding.binding_id.clone(); - result.connection_state = RuntimeConnectionDisplayState::Unavailable; - return Ok(Json(result)); - } - } - } - let checked_at = Utc::now().to_rfc3339(); let runtime = api.runtime.clone(); let ping_runtime_id = runtime_id.clone(); @@ -27349,44 +27058,19 @@ async fn test_runtime_connection( message: "Runtime connection test could not be completed".to_string(), })?; - if ping.is_err() - && binding.authentication_mode == StoredRuntimeAuthenticationMode::WorkspaceIdentity - && let Some(evidence) = api - .store - .get_workspace_runtime_verification(api.workspace_id(), &runtime_id) - .await? - { - let checked_at = Utc::now().to_rfc3339(); - api.store - .record_workspace_runtime_verification_outcome_if_current( - &evidence, - "failed", - "connectivity_failed", - &checked_at, - ) - .await?; + // The ping runs outside the store lock. A replaced/revoked connection must + // not be reported as an observation of the earlier binding. + if !api.store.workspace_runtime_binding_matches(&binding)? { + return Err(Error::RuntimeBindingConflict( + "Runtime connection changed while the connection test was running".to_string(), + ) + .into()); } - let current_binding = api - .store - .get_workspace_runtime_binding(api.workspace_id(), &runtime_id) - .await? - .ok_or_else(|| Error::UnknownRuntime(runtime_id.clone()))?; - let verification = api - .store - .get_workspace_runtime_verification(api.workspace_id(), &runtime_id) - .await?; - let summary = runtime_binding_summary(¤t_binding, verification.as_ref()); let mut result = runtime_connection_test_response(api.workspace_id(), &runtime_id, checked_at, ping); - result.binding_id = current_binding.binding_id.clone(); - result.connection_state = if result.status == RuntimeConnectionTestStatus::Compatible { - summary.connection_state - } else if summary.connection_state == RuntimeConnectionDisplayState::Revoked { - RuntimeConnectionDisplayState::Revoked - } else { - RuntimeConnectionDisplayState::Unavailable - }; - result.verification = summary.verification; + // This result describes this attempt, not permission for a future request. + result.binding_id = binding.binding_id; + Ok(Json(result)) } @@ -29830,16 +29514,6 @@ async fn workspace_runtime_resources_response( .store .list_workspace_runtime_bindings(workspace_id, true) .await?; - let mut verifications = HashMap::new(); - for binding in &bindings { - if let Some(verification) = api - .store - .get_workspace_runtime_verification(workspace_id, &binding.runtime_id) - .await? - { - verifications.insert(binding.runtime_id.clone(), verification); - } - } let mut items = runtimes .items .into_iter() @@ -29849,7 +29523,7 @@ async fn workspace_runtime_resources_response( .find(|binding| binding.runtime_id == runtime.runtime_id); let built_in = runtime.runtime_id == EMBEDDED_WORKER_RUNTIME_ID; let mut runtime: server_api::RuntimeSummary = runtime.into(); - if binding.is_some_and(|binding| binding.state != StoredRuntimeBindingState::Verified) { + if binding.is_some_and(|binding| binding.revoked_at.is_some()) { runtime.worker_creation_available = false; } WorkspaceRuntimeResource { @@ -29861,9 +29535,7 @@ async fn workspace_runtime_resources_response( endpoint_configured: binding .is_some_and(|binding| !binding.base_url.trim().is_empty()), token_ref_configured: false, - binding: binding.map(|binding| { - runtime_binding_summary(binding, verifications.get(&binding.runtime_id)) - }), + binding: binding.map(|binding| runtime_binding_summary(binding)), }, http_status: StatusCode::OK.as_u16(), } @@ -29910,10 +29582,7 @@ async fn workspace_runtime_resources_response( removable: true, endpoint_configured: !binding.base_url.trim().is_empty(), token_ref_configured: false, - binding: Some(runtime_binding_summary( - binding, - verifications.get(&binding.runtime_id), - )), + binding: Some(runtime_binding_summary(binding)), }, http_status: StatusCode::OK.as_u16(), }); @@ -29928,75 +29597,13 @@ async fn workspace_runtime_resources_response( }) } -fn runtime_binding_summary( - binding: &WorkspaceRuntimeBinding, - verification: Option<&crate::store::WorkspaceRuntimeVerificationEvidence>, -) -> WorkspaceRuntimeBindingSummary { - let current_verification = verification.filter(|verification| { - verification.binding_id == binding.binding_id - && verification.runtime_public_key_fingerprint == binding.public_key_fingerprint - && verification.workspace_key_id - == binding.workspace_key_id.as_deref().unwrap_or_default() - && Some(verification.workspace_public_key_fingerprint.as_str()) - == binding.workspace_public_key_fingerprint.as_deref() - && Some(verification.workspace_trust_id.as_str()) - == binding.workspace_trust_id.as_deref() - }); - let valid_verification = current_verification.filter(|verification| { - verification.state == "verified" && verification.last_outcome == "verified" - }); - let connection_state = match binding.state { - StoredRuntimeBindingState::Revoked => RuntimeConnectionDisplayState::Revoked, - _ if current_verification - .is_some_and(|verification| verification.last_outcome != "verified") => - { - RuntimeConnectionDisplayState::Unavailable - } - StoredRuntimeBindingState::Verified - if binding.authentication_mode - == StoredRuntimeAuthenticationMode::LegacyServerIssuer - || valid_verification.is_some() => - { - RuntimeConnectionDisplayState::Verified - } - _ => RuntimeConnectionDisplayState::Configured, - }; +fn runtime_binding_summary(binding: &WorkspaceRuntimeBinding) -> WorkspaceRuntimeBindingSummary { WorkspaceRuntimeBindingSummary { - state: match binding.state { - StoredRuntimeBindingState::Configured => WorkspaceRuntimeBindingState::Configured, - StoredRuntimeBindingState::Verified => WorkspaceRuntimeBindingState::Verified, - StoredRuntimeBindingState::Revoked => WorkspaceRuntimeBindingState::Revoked, - }, - connection_state, binding_id: binding.binding_id.clone(), - workspace_key_id: binding.workspace_key_id.clone(), - workspace_trust_id: binding.workspace_trust_id.clone(), - verification: current_verification.and_then(runtime_verification_summary), + revoked_at: binding.revoked_at.clone(), } } -fn runtime_verification_summary( - verification: &crate::store::WorkspaceRuntimeVerificationEvidence, -) -> Option { - let last_outcome = match verification.last_outcome.as_str() { - "verified" => server_api::RuntimeVerificationOutcome::Verified, - "challenge_issued" => server_api::RuntimeVerificationOutcome::ChallengeIssued, - "verification_failed" => server_api::RuntimeVerificationOutcome::VerificationFailed, - "connectivity_failed" => server_api::RuntimeVerificationOutcome::ConnectivityFailed, - _ => return None, - }; - Some(server_api::RuntimeVerificationEvidenceSummary { - verified_at: verification.verified_at.clone(), - last_checked_at: verification.checked_at.clone(), - last_outcome, - binding_id: verification.binding_id.clone(), - workspace_key_id: verification.workspace_key_id.clone(), - workspace_public_key_fingerprint: verification.workspace_public_key_fingerprint.clone(), - workspace_trust_id: verification.workspace_trust_id.clone(), - runtime_public_key_fingerprint: verification.runtime_public_key_fingerprint.clone(), - }) -} - async fn workspace_runtime_detail( api: &WorkspaceApi, workspace_id: &str, @@ -30038,7 +29645,7 @@ async fn workspace_runtime_detail( removable: false, endpoint_configured: !binding.base_url.trim().is_empty(), token_ref_configured: false, - binding: Some(runtime_binding_summary(&binding, None)), + binding: Some(runtime_binding_summary(binding)), }, http_status: StatusCode::OK.as_u16(), }); @@ -30133,18 +29740,6 @@ async fn validate_runtime_connection_request( "Runtime public bundle must contain a public key", )); } - if request.workspace_trust_id.trim().is_empty() - || request.workspace_trust_id.len() > 256 - || request - .workspace_trust_id - .chars() - .any(|ch| ch.is_control() || ch.is_whitespace()) - { - return Err(settings_bad_request( - "invalid_workspace_trust_id", - "workspace_trust_id must be a bounded Runtime-issued trust identity", - )); - } if request .expected_binding_id .as_deref() @@ -30316,8 +29911,7 @@ fn runtime_connection_test_response( workspace_id: workspace_id.to_string(), runtime_id: runtime_id.to_string(), binding_id: String::new(), - connection_state: RuntimeConnectionDisplayState::Configured, - verification: None, + checked_at, status: RuntimeConnectionTestStatus::Compatible, failure_kind: None, @@ -30368,8 +29962,7 @@ fn runtime_connection_test_failure( workspace_id: workspace_id.to_string(), runtime_id: runtime_id.to_string(), binding_id: String::new(), - connection_state: RuntimeConnectionDisplayState::Unavailable, - verification: None, + checked_at, status: RuntimeConnectionTestStatus::Failed, failure_kind: Some(failure_kind), @@ -32473,10 +32066,11 @@ impl ApiError { let message = api_error_response_message(&self.error); server_api::RepositoryApiError::new( status.as_u16(), - if matches!(self.error, Error::RestoreObservationConflict) { - "restore_observation_conflict" - } else { - status.canonical_reason().unwrap_or("error") + match self.error { + Error::RestoreObservationConflict => "restore_observation_conflict", + Error::RuntimeBindingIdConflict { .. } => "stale_binding", + Error::RuntimeBindingFingerprintConflict { .. } => "fingerprint_in_use", + _ => status.canonical_reason().unwrap_or("error"), }, message, working_directory_diagnostics(self.diagnostics), @@ -36174,13 +35768,7 @@ mod tests { public_key: identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-test".to_owned()), - - workspace_public_key_fingerprint: Some("sha256:workspace-test".to_string()), - - workspace_trust_id: Some("trust-test".to_string()), created_at: "2026-01-01T00:00:00Z".to_owned(), updated_at: "2026-01-01T00:00:00Z".to_owned(), revoked_at: None, @@ -42255,13 +41843,12 @@ mod tests { } #[tokio::test] - async fn verified_workspace_runtime_binding_is_restored_into_live_registry() { + async fn runtime_connection_is_restored_without_prior_authentication() { let dir = tempfile::tempdir().unwrap(); let root = dir.path().to_path_buf(); let api = test_api(&root).await; let actor = test_owner_actor(); - let identity = api - .signing_identities + api.signing_identities .provision_existing(&api.config.workspace_id, &actor.account_id) .unwrap(); let runtime_identity = RuntimeIdentityMaterial::generate("restored-runtime").unwrap(); @@ -42275,13 +41862,7 @@ mod tests { public_key: runtime_identity.public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Configured, authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some(identity.key_id.clone()), - - workspace_public_key_fingerprint: identity.public_key_fingerprint.clone(), - - workspace_trust_id: Some("trust-test".to_string()), created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -42290,38 +41871,12 @@ mod tests { ) .await .unwrap(); - let configured = api - .store - .get_workspace_runtime_binding(&api.config.workspace_id, "restored-runtime") - .await - .unwrap() - .unwrap(); - let evidence = crate::store::WorkspaceRuntimeVerificationEvidence { - workspace_id: configured.workspace_id.clone(), - runtime_id: configured.runtime_id.clone(), - binding_id: configured.binding_id.clone(), - workspace_key_id: identity.key_id, - workspace_public_key_fingerprint: identity.public_key_fingerprint.clone().unwrap(), - workspace_trust_id: "trust-test".to_string(), - runtime_public_key_fingerprint: configured.public_key_fingerprint.clone(), - challenge_id: "restart-challenge".to_string(), - state: "verified".to_string(), - last_outcome: "verified".to_string(), - verified_at: Some("2".to_string()), - checked_at: "2".to_string(), - }; - api.store - .record_workspace_runtime_verification_attempt(&evidence) - .await - .unwrap(); - api.store - .complete_workspace_runtime_verification(&evidence) - .await - .unwrap(); drop(api); - tokio::time::sleep(std::time::Duration::from_millis(50)).await; - - let restored_config = test_server_config(&root); + // The assertion concerns persisted connection configuration, not async + // shutdown of the first embedded execution store. Use an independent + // execution store while reopening the same Workspace database. + let restored_config = test_server_config(&root) + .with_embedded_runtime_store_root(root.join("restored-embedded-runtime")); let restored_store = SqliteWorkspaceStore::open(restored_config.database_path.clone()).unwrap(); let restored = WorkspaceApi::new(restored_config, Arc::new(restored_store)) @@ -42334,7 +41889,7 @@ mod tests { .items .iter() .any(|runtime| runtime.runtime_id == "restored-runtime"), - "verified persisted Runtime binding must return to the live registry" + "persisted Runtime connection must return without a prior authentication result" ); assert!( restored @@ -42350,12 +41905,11 @@ mod tests { } #[tokio::test] - async fn remote_runtime_endpoint_update_preserves_public_key_but_requires_reverification() { + async fn remote_runtime_endpoint_update_preserves_key_and_replaces_connection_identity() { let dir = tempfile::tempdir().unwrap(); let api = test_api(dir.path()).await; let actor = test_owner_actor(); - let workspace_identity = api - .signing_identities + api.signing_identities .provision_existing(&api.config.workspace_id, &actor.account_id) .unwrap(); let runtime_identity = RuntimeIdentityMaterial::generate("verified-runtime").unwrap(); @@ -42369,13 +41923,7 @@ mod tests { public_key: runtime_identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Configured, authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some(workspace_identity.key_id.clone()), - workspace_public_key_fingerprint: workspace_identity - .public_key_fingerprint - .clone(), - workspace_trust_id: Some("trust-test".to_string()), created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -42390,32 +41938,6 @@ mod tests { .await .unwrap() .unwrap(); - let evidence = crate::store::WorkspaceRuntimeVerificationEvidence { - workspace_id: configured.workspace_id.clone(), - runtime_id: configured.runtime_id.clone(), - binding_id: configured.binding_id.clone(), - workspace_key_id: workspace_identity.key_id, - workspace_public_key_fingerprint: workspace_identity - .public_key_fingerprint - .clone() - .unwrap(), - workspace_trust_id: "trust-test".to_string(), - runtime_public_key_fingerprint: configured.public_key_fingerprint.clone(), - challenge_id: "verified-metadata-update".to_string(), - state: "verified".to_string(), - last_outcome: "verified".to_string(), - verified_at: Some("2".to_string()), - checked_at: "2".to_string(), - }; - api.store - .record_workspace_runtime_verification_attempt(&evidence) - .await - .unwrap(); - api.store - .complete_workspace_runtime_verification(&evidence) - .await - .unwrap(); - let Json(updated) = scoped_update_remote_runtime( State(api.clone()), AxumPath(ScopedRuntimePath { @@ -42434,9 +41956,8 @@ mod tests { assert_eq!(updated.runtime.runtime.label, "New label"); assert_eq!(updated.endpoint.as_deref(), Some("https://8.8.4.4")); let binding = updated.runtime.management.binding.unwrap(); - assert_eq!(binding.state, WorkspaceRuntimeBindingState::Configured); + assert_eq!(binding.revoked_at, None); assert_ne!(binding.binding_id, configured.binding_id); - assert_eq!(binding.verification, None); assert_eq!( updated.trust_key.fingerprint.as_deref(), Some(configured.public_key_fingerprint.as_str()) @@ -42464,7 +41985,7 @@ mod tests { }, display_name: Some("New label".to_string()), endpoint: "https://8.8.4.4".to_string(), - workspace_trust_id: "trust-test".to_string(), + expected_binding_id: Some(configured.binding_id.clone()), }), ) @@ -42474,7 +41995,7 @@ mod tests { } #[tokio::test] - async fn remote_runtime_registration_is_workspace_scoped_binding_fenced_and_configured() { + async fn remote_runtime_registration_is_workspace_scoped_and_binding_fenced() { let dir = tempfile::tempdir().unwrap(); let api = test_api(dir.path()).await; let actor = test_owner_actor(); @@ -42502,7 +42023,7 @@ mod tests { }, display_name: Some("Configured Runtime".to_string()), endpoint: "https://8.8.8.8".to_string(), - workspace_trust_id: "trust-test".to_string(), + expected_binding_id: None, }; let mut non_owner = actor.clone(); @@ -42527,10 +42048,8 @@ mod tests { .unwrap(); assert_eq!(status, StatusCode::CREATED); let binding = created.management.binding.as_ref().unwrap(); - assert_eq!(binding.state, WorkspaceRuntimeBindingState::Configured); + assert_eq!(binding.revoked_at, None); assert!(!binding.binding_id.is_empty()); - assert!(binding.workspace_key_id.is_some()); - assert_eq!(binding.workspace_trust_id.as_deref(), Some("trust-test")); assert!(!created.runtime.worker_creation_available); assert!( api.runtime @@ -42538,7 +42057,7 @@ mod tests { .items .iter() .any(|runtime| runtime.runtime_id == "configured-runtime"), - "configured binding must remain registered so verification can reach it" + "registered connection permits actual authentication attempts" ); assert!( api.runtime_binding_expectations @@ -42550,22 +42069,6 @@ mod tests { )), "configured binding must remain fenced by its current binding ID" ); - let Json(configured_test) = scoped_test_runtime_connection( - State(api.clone()), - AxumPath(ScopedRuntimePath { - workspace_id: api.config.workspace_id.clone(), - runtime_id: "configured-runtime".to_string(), - }), - Extension(actor.clone()), - ) - .await - .unwrap(); - assert_eq!(configured_test.status, RuntimeConnectionTestStatus::Failed); - assert_eq!( - configured_test.connection_state, - RuntimeConnectionDisplayState::Unavailable - ); - let (status, Json(replayed)) = create_remote_runtime( State(api.clone()), Extension(actor.clone()), @@ -42589,6 +42092,30 @@ mod tests { .await .unwrap_err(); assert_eq!(error.into_response().status(), StatusCode::CONFLICT); + assert!( + api.runtime + .list_runtimes(100) + .items + .iter() + .any(|runtime| runtime.runtime_id == "configured-runtime"), + "rejected binding mutation must retain the live registration" + ); + let unchanged = api + .store + .get_workspace_runtime_binding(&api.config.workspace_id, "configured-runtime") + .await + .unwrap() + .unwrap(); + assert_eq!(unchanged.binding_id, binding.binding_id); + let conflict = request_json( + build_inner_router(api.clone()).layer(Extension(actor.clone())), + "POST", + &format!("/api/w/{TEST_WORKSPACE_ID}/runtimes"), + Some(serde_json::to_value(&mismatched).unwrap()), + StatusCode::CONFLICT, + ) + .await; + assert_eq!(conflict["error"], "stale_binding"); mismatched.expected_binding_id = Some(binding.binding_id.clone()); let (status, Json(replaced)) = @@ -42612,7 +42139,7 @@ mod tests { }, display_name: None, endpoint: "https://8.8.4.4".to_string(), - workspace_trust_id: "trust-test".to_string(), + expected_binding_id: Some("binding-missing".to_string()), }), ) @@ -42656,7 +42183,7 @@ mod tests { }, display_name: None, endpoint: "http://127.0.0.1:1".to_string(), - workspace_trust_id: "trust-first-acceptance".to_string(), + expected_binding_id: None, }; let (_, Json(created)) = create_remote_runtime( @@ -42683,7 +42210,6 @@ mod tests { let revoked_id = revoked.trust_key.binding_id.unwrap(); assert_ne!(revoked_id, original.binding_id); let mut reaccepted = request; - reaccepted.workspace_trust_id = "trust-second-acceptance".to_string(); reaccepted.expected_binding_id = Some(revoked_id); let (_, Json(created)) = create_remote_runtime( State(api.clone()), @@ -42694,14 +42220,6 @@ mod tests { .unwrap(); let current = created.management.binding.unwrap(); assert_ne!(current.binding_id, original.binding_id); - assert_eq!( - current.workspace_trust_id.as_deref(), - Some("trust-second-acceptance") - ); - assert_eq!( - current.connection_state, - RuntimeConnectionDisplayState::Configured - ); let stale = ::runtime_trust_key_revoke( &service, actor, @@ -42737,7 +42255,7 @@ mod tests { }, display_name: Some("Team Runtime".to_string()), endpoint: "https://8.8.8.8".to_string(), - workspace_trust_id: "trust-test".to_string(), + expected_binding_id: None, }; assert!(validate_runtime_connection_request(&ok).await.is_ok()); @@ -42767,59 +42285,6 @@ mod tests { } } - #[tokio::test] - async fn runtime_registration_rejects_blank_and_malformed_workspace_trust_ids() { - let temp = tempfile::tempdir().unwrap(); - let api = test_api(temp.path()).await; - let actor = test_owner_actor(); - let identity = RuntimeIdentityMaterial::generate("invalid-trust-runtime").unwrap(); - for trust_id in [ - String::new(), - " ".to_string(), - " trust-valid".to_string(), - "trust-valid ".to_string(), - "trust invalid".to_string(), - "trust\ninvalid".to_string(), - "x".repeat(257), - ] { - let error = create_remote_runtime( - State(api.clone()), - Extension(actor.clone()), - Json(CreateRemoteRuntimeRequest { - public_bundle: server_api::RuntimePublicIdentityBundle { - identity_id: identity.identity_id.clone(), - public_key: identity.public_key.clone(), - }, - display_name: None, - endpoint: "http://127.0.0.1:1".to_string(), - workspace_trust_id: trust_id.clone(), - expected_binding_id: None, - }), - ) - .await - .unwrap_err(); - let response = error.into_response(); - assert_eq!(response.status(), StatusCode::BAD_REQUEST, "{trust_id:?}"); - let body: serde_json::Value = - serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap()) - .unwrap(); - assert!( - body["message"] - .as_str() - .unwrap() - .contains("invalid_workspace_trust_id"), - "{body}" - ); - assert!( - api.store - .get_workspace_runtime_binding(api.workspace_id(), &identity.identity_id) - .await - .unwrap() - .is_none() - ); - } - } - #[test] fn backend_errors_preserve_operation_details() { let sanitized = sanitize_backend_error("failed to open server database"); @@ -45599,7 +45064,7 @@ mod tests { } #[test] - fn runtime_binding_summary_omits_stale_verification_binding() { + fn runtime_binding_summary_exposes_configuration_and_revocation_without_auth_state() { let binding = WorkspaceRuntimeBinding { workspace_id: "workspace-a".to_string(), runtime_id: "runtime-a".to_string(), @@ -45608,36 +45073,17 @@ mod tests { public_key: "runtime-public-key".to_string(), public_key_fingerprint: "sha256:runtime".to_string(), binding_id: "binding-test".to_string(), - state: crate::store::WorkspaceRuntimeBindingState::Revoked, authentication_mode: crate::store::WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-a".to_string()), - - workspace_public_key_fingerprint: Some("sha256:workspace-test".to_string()), - - workspace_trust_id: Some("trust-test".to_string()), created_at: "1".to_string(), updated_at: "2".to_string(), revoked_at: Some("2".to_string()), }; - let stale = crate::store::WorkspaceRuntimeVerificationEvidence { - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-a".to_string(), - binding_id: "binding-retired".to_string(), - workspace_key_id: "WK-a".to_string(), - workspace_public_key_fingerprint: "sha256:workspace-test".to_string(), - workspace_trust_id: "trust-test".to_string(), - runtime_public_key_fingerprint: "sha256:runtime".to_string(), - challenge_id: "challenge-a".to_string(), - state: "failed".to_string(), - last_outcome: "connectivity_failed".to_string(), - verified_at: None, - checked_at: "1".to_string(), - }; - - let summary = runtime_binding_summary(&binding, Some(&stale)); - - assert_eq!(summary.verification, None); + let summary = serde_json::to_value(runtime_binding_summary(&binding)).unwrap(); + assert_eq!( + summary, + serde_json::json!({"binding_id": "binding-test", "revoked_at": "2"}) + ); } #[test] @@ -51631,13 +51077,7 @@ mod tests { public_key: identity.public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-test".to_owned()), - - workspace_public_key_fingerprint: Some("sha256:workspace-test".to_string()), - - workspace_trust_id: Some("trust-test".to_string()), created_at: now.clone(), updated_at: now, revoked_at: None, @@ -52024,13 +51464,7 @@ mod tests { public_key: identity.public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -52641,13 +52075,7 @@ mod tests { public_key: identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: "2026-08-11T00:00:00Z".to_string(), updated_at: "2026-08-11T00:00:00Z".to_string(), revoked_at: None, @@ -52915,7 +52343,6 @@ mod tests { assert_eq!(missing_mutation_response.status(), StatusCode::UNAUTHORIZED); let mut revoked = trust; - revoked.state = StoredRuntimeBindingState::Revoked; revoked.revoked_at = Some("2026-08-11T00:01:00Z".to_string()); let authority = SqliteWorkspaceStore::open(api.config.database_path.clone()).unwrap(); authority @@ -53375,13 +52802,7 @@ mod tests { public_key: provider_identity.public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: now.clone(), updated_at: now, revoked_at: None, @@ -56174,13 +55595,7 @@ mod tests { .public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -57476,13 +56891,7 @@ mod tests { public_key: identity.public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -57548,8 +56957,7 @@ mod tests { "public_key": "" }, "display_name": "Keyless Runtime", - "endpoint": "https://8.8.8.8", - "workspace_trust_id": "trust-keyless-test" + "endpoint": "https://8.8.8.8" })), StatusCode::BAD_REQUEST, ) @@ -57842,13 +57250,7 @@ mod tests { .public_key, public_key_fingerprint: String::new(), binding_id: "binding-test".to_string(), - state: StoredRuntimeBindingState::Verified, authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - - workspace_public_key_fingerprint: None, - - workspace_trust_id: None, created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -57906,6 +57308,67 @@ mod tests { assert!(persisted.revoked_at.is_none()); } + #[tokio::test(flavor = "multi_thread")] + async fn runtime_connection_test_rejects_binding_changed_during_ping() { + let dir = tempfile::tempdir().unwrap(); + let api = test_api(dir.path()).await; + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let endpoint = format!("http://{}", listener.local_addr().unwrap()); + let binding = WorkspaceRuntimeBinding { + workspace_id: TEST_WORKSPACE_ID.to_string(), + runtime_id: "probe-runtime".to_string(), + display_name: "Probe Runtime".to_string(), + base_url: endpoint.clone(), + public_key: RuntimeIdentityMaterial::generate("probe-runtime") + .unwrap() + .public_key, + public_key_fingerprint: String::new(), + binding_id: "binding-before-ping".to_string(), + authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, + created_at: "1".to_string(), + updated_at: "1".to_string(), + revoked_at: None, + }; + api.store + .upsert_workspace_runtime_binding_record(binding.clone(), false) + .await + .unwrap(); + let mut config = RemoteRuntimeConfig::new( + "probe-runtime", + "Probe Runtime", + &endpoint, + Some("test-token".to_string()), + ); + config.strict_public_egress = false; + api.runtime.register_or_replace( + RemoteWorkerRuntime::new( + config, + TEST_WORKSPACE_ID.to_string(), + "http://127.0.0.1:1".to_string(), + ) + .unwrap(), + ); + let store = api.store.clone(); + // Mutate only after the HTTP request reached the selected connection. + let stub = Router::new().route("/v1/ping", axum::routing::get(move || { + let store = store.clone(); let mut replacement = binding.clone(); + async move { + replacement.binding_id = "binding-after-ping".to_string(); + replacement.display_name = "Replacement Runtime".to_string(); + store.upsert_workspace_runtime_binding_record(replacement, true).await.unwrap(); + Json(serde_json::json!({"runtime_id":"probe-runtime", "protocol_version":RUNTIME_HTTP_PROTOCOL_VERSION})) + } + })); + let server = tokio::spawn(async move { + axum::serve(listener, stub).await.unwrap(); + }); + let result = + test_runtime_connection(State(api), AxumPath("probe-runtime".to_string())).await; + server.abort(); + let error = result.unwrap_err(); + assert_eq!(error.into_response().status(), StatusCode::CONFLICT); + } + async fn run_runtime_connection_test( body: serde_json::Value, status: StatusCode, @@ -57956,8 +57419,8 @@ mod tests { .await; assert_eq!(response["status"], "compatible"); - assert_eq!(response["connection_state"], "verified"); - assert_eq!(response["verification"], serde_json::Value::Null); + assert!(response.get("connection_state").is_none()); + assert!(response.get("verification").is_none()); assert_eq!(response["failure_kind"], serde_json::Value::Null); assert_eq!( response["expected_protocol_version"], diff --git a/crates/workspace-server/src/server/tests/drive_web_roundtrip_tests.rs b/crates/workspace-server/src/server/tests/drive_web_roundtrip_tests.rs index e267a77ca..02ab40e35 100644 --- a/crates/workspace-server/src/server/tests/drive_web_roundtrip_tests.rs +++ b/crates/workspace-server/src/server/tests/drive_web_roundtrip_tests.rs @@ -111,11 +111,9 @@ impl RealDriveFixture { public_key: identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: "binding-drive-test".to_string(), - state: StoredRuntimeBindingState::Verified, + authentication_mode: StoredRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - workspace_public_key_fingerprint: None, - workspace_trust_id: None, + created_at: "1".into(), updated_at: "1".into(), revoked_at: None, diff --git a/crates/workspace-server/src/server/tests/ticket_worker_launch_tests.rs b/crates/workspace-server/src/server/tests/ticket_worker_launch_tests.rs index 474e37895..906ca566a 100644 --- a/crates/workspace-server/src/server/tests/ticket_worker_launch_tests.rs +++ b/crates/workspace-server/src/server/tests/ticket_worker_launch_tests.rs @@ -160,12 +160,10 @@ impl LaunchFixture { if remote { fixture.runtime_id = "ticket-worker-real-http".to_string(); use worker_runtime::workspace_issuer::{ - InMemoryWorkspaceClaimReplayProtection, - InMemoryWorkspaceRuntimeVerificationAuthority, RuntimeVerificationSigner, - WorkspaceCapabilityVerifier, WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, + InMemoryWorkspaceClaimReplayProtection, WorkspaceCapabilityVerifier, + WorkspaceIssuerTrustRecord, WorkspaceIssuerTrustState, }; let runtime_identity = RuntimeIdentityMaterial::generate(&fixture.runtime_id).unwrap(); - let signer = RuntimeVerificationSigner::from_identity(&runtime_identity).unwrap(); let workspace_identity = fixture .api .signing_identities @@ -204,13 +202,11 @@ impl LaunchFixture { display_name: "Ticket Worker real HTTP Runtime".into(), base_url: endpoint.clone(), public_key: runtime_identity.public_key.clone(), - public_key_fingerprint: signer.public_key_fingerprint().to_string(), + public_key_fingerprint: String::new(), binding_id: "binding-launch-test".to_string(), - state: StoredRuntimeBindingState::Configured, + authentication_mode: StoredRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some(workspace_identity.key_id), - workspace_public_key_fingerprint: workspace_identity.public_key_fingerprint.clone(), - workspace_trust_id: Some("trust-launch-test".to_string()), + created_at: TEST_CREATED_AT.into(), updated_at: TEST_CREATED_AT.into(), revoked_at: None, @@ -228,6 +224,7 @@ impl LaunchFixture { .await .unwrap() .unwrap(); + let runtime_id = fixture.runtime_id.clone(); fixture.runtime_server = Some(tokio::spawn(async move { worker_runtime::http_server::serve_runtime_http_with_workspace_auth( runtime, @@ -235,10 +232,7 @@ impl LaunchFixture { None, worker_runtime::http_server::WorkspaceRuntimeHttpAuth { verifier, - signer, - verifications: Arc::new( - InMemoryWorkspaceRuntimeVerificationAuthority::default(), - ), + runtime_id, }, ) .await @@ -258,27 +252,12 @@ impl LaunchFixture { cached_status: "active".into(), timeout: std::time::Duration::from_secs(5), }; - fixture.api.runtime.register_or_replace( - RemoteWorkerRuntime::new( - remote_config.clone(), - TEST_WORKSPACE_ID.to_string(), - backend_url.clone(), - ) - .unwrap(), - ); - let verified = perform_workspace_runtime_verification( - &fixture.api, - fixture.api.runtime.clone(), - &binding, - ) - .await - .unwrap(); remote_config.workspace_authorization = Some(crate::hosts::WorkspaceRuntimeAuthorization::new( fixture.api.store.clone(), fixture.api.signing_identities.clone(), backend_url.clone(), - Some(verified), + binding, )); fixture.api.runtime.register_or_replace( RemoteWorkerRuntime::new(remote_config, TEST_WORKSPACE_ID.to_string(), backend_url) diff --git a/crates/workspace-server/src/server/tests/workspace_admission_tests.rs b/crates/workspace-server/src/server/tests/workspace_admission_tests.rs index d90918a36..9bfbd3364 100644 --- a/crates/workspace-server/src/server/tests/workspace_admission_tests.rs +++ b/crates/workspace-server/src/server/tests/workspace_admission_tests.rs @@ -694,7 +694,6 @@ async fn revoked_runtime_and_persisted_workspace_fence_reject_new_requests() { .unwrap() .unwrap(); binding.revoked_at = Some("revoked".into()); - binding.state = StoredRuntimeBindingState::Revoked; f.api .store .upsert_workspace_runtime_binding_record(binding, true) diff --git a/crates/workspace-server/src/store.rs b/crates/workspace-server/src/store.rs index a3f20242c..f6b264061 100644 --- a/crates/workspace-server/src/store.rs +++ b/crates/workspace-server/src/store.rs @@ -28,7 +28,7 @@ use crate::workspace_deletion::WorkspaceDeletionStore; use crate::{Error, Result}; const OLDEST_SCHEMA_VERSION: i64 = 50; -const LATEST_SCHEMA_VERSION: i64 = 90; +const LATEST_SCHEMA_VERSION: i64 = 91; const WORKSPACE_DRIVE_GRANTS_MIGRATION_NAME: &str = "durable Workspace Drive grants"; const WORKSPACE_CONFIG_GRANTS_MIGRATION_NAME: &str = "Workspace config grants and logical Workdirs"; const SCHEMA_BASELINE_NAME: &str = "workspace schema baseline"; @@ -283,8 +283,13 @@ const MIGRATIONS: &[Migration] = &[ }, Migration { version: 90, + name: "Runtime binding connection configuration and historical verification archive", + apply: migrate_runtime_binding_connections_v89_to_v90, + }, + Migration { + version: 91, name: "Ticket open/closed cases and retained routing evidence", - apply: migrate_ticket_cases_v89_to_v90, + apply: migrate_ticket_cases_v90_to_v91, }, ]; @@ -446,22 +451,6 @@ pub struct WorkspaceSigningIdentityProvisioningOperation { pub completed_at: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct WorkspaceRuntimeVerificationEvidence { - pub workspace_id: String, - pub runtime_id: String, - pub binding_id: String, - pub workspace_key_id: String, - pub workspace_public_key_fingerprint: String, - pub workspace_trust_id: String, - pub runtime_public_key_fingerprint: String, - pub challenge_id: String, - pub state: String, - pub last_outcome: String, - pub verified_at: Option, - pub checked_at: String, -} - #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct WorkspaceRuntimeBinding { pub workspace_id: String, @@ -471,11 +460,7 @@ pub struct WorkspaceRuntimeBinding { pub public_key: String, pub public_key_fingerprint: String, pub binding_id: String, - pub state: WorkspaceRuntimeBindingState, pub authentication_mode: WorkspaceRuntimeAuthenticationMode, - pub workspace_key_id: Option, - pub workspace_public_key_fingerprint: Option, - pub workspace_trust_id: Option, pub created_at: String, pub updated_at: String, pub revoked_at: Option, @@ -526,24 +511,6 @@ pub struct RuntimeRemovalReservation { pub replay: bool, } -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] -#[serde(rename_all = "snake_case")] -pub enum WorkspaceRuntimeBindingState { - Configured, - Verified, - Revoked, -} - -impl WorkspaceRuntimeBindingState { - fn as_str(self) -> &'static str { - match self { - Self::Configured => "configured", - Self::Verified => "verified", - Self::Revoked => "revoked", - } - } -} - #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum WorkspaceRuntimeAuthenticationMode { @@ -1195,32 +1162,6 @@ pub trait ControlPlaneStore: Send + Sync + WorkspaceDeletionStore { ) -> Result; fn workspace_runtime_binding_matches(&self, expected: &WorkspaceRuntimeBinding) -> Result; - fn workspace_runtime_verification_matches( - &self, - binding: &WorkspaceRuntimeBinding, - workspace_public_key_fingerprint: &str, - workspace_trust_id: &str, - ) -> Result; - async fn get_workspace_runtime_verification( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result>; - async fn record_workspace_runtime_verification_attempt( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result<()>; - async fn record_workspace_runtime_verification_outcome_if_current( - &self, - expected: &WorkspaceRuntimeVerificationEvidence, - state: &str, - outcome: &str, - checked_at: &str, - ) -> Result; - async fn complete_workspace_runtime_verification( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result; async fn get_workspace_runtime_binding( &self, workspace_id: &str, @@ -2935,15 +2876,15 @@ impl SqliteWorkspaceStore { self.with_conn(|conn| { let sql = if include_revoked { r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 ORDER BY runtime_id ASC"# } else { r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND revoked_at IS NULL ORDER BY runtime_id ASC"# @@ -2965,8 +2906,8 @@ impl SqliteWorkspaceStore { self.with_conn(|conn| { conn.query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id], @@ -3319,8 +3260,8 @@ impl SqliteWorkspaceStore { let existing = tx .query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![record.workspace_id, record.runtime_id], @@ -3334,76 +3275,42 @@ impl SqliteWorkspaceStore { && existing.base_url == record.base_url && existing.public_key == record.public_key && existing.public_key_fingerprint == record.public_key_fingerprint - && existing.state == record.state - && existing.authentication_mode == record.authentication_mode - && existing.workspace_key_id == record.workspace_key_id - && existing.workspace_public_key_fingerprint == record.workspace_public_key_fingerprint - && existing.workspace_trust_id == record.workspace_trust_id; + && existing.authentication_mode == record.authentication_mode; if exact_active_match { tx.commit()?; return Ok(WorkspaceRuntimeBindingUpsert::Unchanged); } if !replace { return Err(Error::RuntimeBindingConflict(format!( - "binding {}/{} already exists with different endpoint, trust, or lifecycle state; retry with explicit replacement", + "binding {}/{} already exists with different endpoint, pinned key, authentication mode, or administrative revocation; retry with explicit replacement", record.workspace_id, record.runtime_id ))); } tx.execute( r#"UPDATE workspace_runtime_bindings SET display_name = ?3, base_url = ?4, public_key = ?5, - public_key_fingerprint = ?6, binding_id = ?13, - state = ?7, authentication_mode = ?8, - workspace_key_id = ?9, workspace_trust_id = ?10, - updated_at = ?11, revoked_at = ?12, workspace_public_key_fingerprint = ?14 + public_key_fingerprint = ?6, binding_id = ?10, + authentication_mode = ?7, updated_at = ?8, revoked_at = ?9 WHERE workspace_id = ?1 AND runtime_id = ?2"#, - params![ - record.workspace_id, - record.runtime_id, - record.display_name, - record.base_url, - record.public_key, - record.public_key_fingerprint, - record.state.as_str(), - record.authentication_mode.as_str(), - record.workspace_key_id, - record.workspace_trust_id, - record.updated_at, - record.revoked_at, - new_runtime_binding_id(), - record.workspace_public_key_fingerprint, ], + params![record.workspace_id, record.runtime_id, record.display_name, + record.base_url, record.public_key, record.public_key_fingerprint, + record.authentication_mode.as_str(), record.updated_at, record.revoked_at, + new_runtime_binding_id()], ) .map_err(map_runtime_binding_write_error)?; - tx.execute( - "DELETE FROM workspace_runtime_verifications WHERE workspace_id = ?1 AND runtime_id = ?2", - params![record.workspace_id, record.runtime_id], - )?; tx.commit()?; return Ok(WorkspaceRuntimeBindingUpsert::Replaced); } tx.execute( r#"INSERT INTO workspace_runtime_bindings ( workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?14, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?15)"#, - params![ - record.workspace_id, - record.runtime_id, - record.display_name, - record.base_url, - record.public_key, - record.public_key_fingerprint, - record.state.as_str(), - record.authentication_mode.as_str(), - record.workspace_key_id, - record.workspace_trust_id, - record.created_at, - record.updated_at, - record.revoked_at, - new_runtime_binding_id(), - record.workspace_public_key_fingerprint, - ], + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)"#, + params![record.workspace_id, record.runtime_id, record.display_name, + record.base_url, record.public_key, record.public_key_fingerprint, + new_runtime_binding_id(), record.authentication_mode.as_str(), + record.created_at, record.updated_at, record.revoked_at], ) .map_err(map_runtime_binding_write_error)?; tx.commit()?; @@ -3424,17 +3331,16 @@ impl SqliteWorkspaceStore { let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?; let changed = tx.execute( r#"UPDATE workspace_runtime_bindings - SET state = 'revoked', revoked_at = ?3, updated_at = ?3, + SET revoked_at = ?3, updated_at = ?3, binding_id = ?4 WHERE workspace_id = ?1 AND runtime_id = ?2 AND revoked_at IS NULL"#, - params![workspace_id, runtime_id, revoked_at, new_runtime_binding_id()], + params![ + workspace_id, + runtime_id, + revoked_at, + new_runtime_binding_id() + ], )?; - if changed > 0 { - tx.execute( - "DELETE FROM workspace_runtime_verifications WHERE workspace_id = ?1 AND runtime_id = ?2", - params![workspace_id, runtime_id], - )?; - } tx.commit()?; Ok(changed > 0) }) @@ -3458,8 +3364,8 @@ impl SqliteWorkspaceStore { let existing = tx .query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![record.workspace_id, record.runtime_id], @@ -3479,11 +3385,7 @@ impl SqliteWorkspaceStore { && existing.base_url == record.base_url && existing.public_key == record.public_key && existing.public_key_fingerprint == record.public_key_fingerprint - && existing.state == record.state && existing.authentication_mode == record.authentication_mode - && existing.workspace_key_id == record.workspace_key_id - && existing.workspace_public_key_fingerprint == record.workspace_public_key_fingerprint - && existing.workspace_trust_id == record.workspace_trust_id { tx.commit()?; return Ok((WorkspaceRuntimeBindingMutation::Unchanged, existing)); @@ -3527,9 +3429,8 @@ impl SqliteWorkspaceStore { r#"UPDATE workspace_runtime_bindings SET display_name = ?3, base_url = ?4, public_key = ?5, public_key_fingerprint = ?6, - state = ?7, authentication_mode = ?8, - workspace_key_id = ?9, workspace_trust_id = ?10, - binding_id = ?11, updated_at = ?12, revoked_at = NULL, workspace_public_key_fingerprint = ?13 + authentication_mode = ?7, binding_id = ?8, + updated_at = ?9, revoked_at = NULL WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![ record.workspace_id, @@ -3538,19 +3439,11 @@ impl SqliteWorkspaceStore { record.base_url, record.public_key, record.public_key_fingerprint, - record.state.as_str(), record.authentication_mode.as_str(), - record.workspace_key_id, - record.workspace_trust_id, &next_binding_id, - record.updated_at, - record.workspace_public_key_fingerprint, + record.updated_at ], )?; - tx.execute( - "DELETE FROM workspace_runtime_verifications WHERE workspace_id = ?1 AND runtime_id = ?2", - params![record.workspace_id, record.runtime_id], - )?; insert_workspace_runtime_binding_audit( &tx, &record.workspace_id, @@ -3564,8 +3457,8 @@ impl SqliteWorkspaceStore { )?; let updated = tx.query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![record.workspace_id, record.runtime_id], @@ -3599,9 +3492,9 @@ impl SqliteWorkspaceStore { tx.execute( r#"INSERT INTO workspace_runtime_bindings ( workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?13, ?7, ?8, ?9, ?10, ?11, ?12, NULL, ?14)"#, + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at + ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, NULL)"#, params![ record.workspace_id, record.runtime_id, @@ -3609,14 +3502,11 @@ impl SqliteWorkspaceStore { record.base_url, record.public_key, record.public_key_fingerprint, - record.state.as_str(), - record.authentication_mode.as_str(), - record.workspace_key_id, - record.workspace_trust_id, - record.created_at, - record.updated_at, record.binding_id, - record.workspace_public_key_fingerprint, ], + record.authentication_mode.as_str(), + record.created_at, + record.updated_at + ], )?; insert_workspace_runtime_binding_audit( &tx, @@ -3652,8 +3542,8 @@ impl SqliteWorkspaceStore { let existing = tx .query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id], @@ -3670,21 +3560,25 @@ impl SqliteWorkspaceStore { tx.execute( r#"UPDATE workspace_runtime_bindings SET display_name = ?3, base_url = ?4, updated_at = ?5, - binding_id = ?6, - state = CASE WHEN base_url != ?4 AND state = 'verified' - AND authentication_mode = 'workspace_identity' - THEN 'configured' ELSE state END + binding_id = ?6 WHERE workspace_id = ?1 AND runtime_id = ?2"#, - params![workspace_id, runtime_id, display_name, base_url, updated_at, - if existing.base_url != base_url { new_runtime_binding_id() } else { existing.binding_id.clone() }], + params![ + workspace_id, + runtime_id, + display_name, + base_url, + updated_at, + if existing.base_url != base_url { + new_runtime_binding_id() + } else { + existing.binding_id.clone() + } + ], )?; - if existing.base_url != base_url { - tx.execute("DELETE FROM workspace_runtime_verifications WHERE workspace_id=?1 AND runtime_id=?2", params![workspace_id,runtime_id])?; - } let updated = tx.query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id], @@ -3712,8 +3606,8 @@ impl SqliteWorkspaceStore { let existing = tx .query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id], @@ -3736,14 +3630,10 @@ impl SqliteWorkspaceStore { let next_binding_id = new_runtime_binding_id(); tx.execute( r#"UPDATE workspace_runtime_bindings - SET state = 'revoked', revoked_at = ?3, updated_at = ?3, binding_id = ?4 + SET revoked_at = ?3, updated_at = ?3, binding_id = ?4 WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id, revoked_at, next_binding_id], )?; - tx.execute( - "DELETE FROM workspace_runtime_verifications WHERE workspace_id = ?1 AND runtime_id = ?2", - params![workspace_id, runtime_id], - )?; insert_workspace_runtime_binding_audit( &tx, workspace_id, @@ -3757,8 +3647,8 @@ impl SqliteWorkspaceStore { )?; let updated = tx.query_row( r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint + public_key_fingerprint, binding_id, authentication_mode, + created_at, updated_at, revoked_at FROM workspace_runtime_bindings WHERE workspace_id = ?1 AND runtime_id = ?2"#, params![workspace_id, runtime_id], @@ -3769,310 +3659,6 @@ impl SqliteWorkspaceStore { }) } - pub fn workspace_runtime_verification_matches( - &self, - binding: &WorkspaceRuntimeBinding, - workspace_public_key_fingerprint: &str, - workspace_trust_id: &str, - ) -> Result { - let Some(evidence) = - self.get_workspace_runtime_verification(&binding.workspace_id, &binding.runtime_id)? - else { - return Ok(false); - }; - Ok(evidence.state == "verified" - && evidence.last_outcome == "verified" - && evidence.verified_at.is_some() - && evidence.binding_id == binding.binding_id - && evidence.workspace_key_id == binding.workspace_key_id.as_deref().unwrap_or_default() - && evidence.workspace_public_key_fingerprint == workspace_public_key_fingerprint - && binding.workspace_public_key_fingerprint.as_deref() - == Some(workspace_public_key_fingerprint) - && evidence.workspace_trust_id == workspace_trust_id - && evidence.runtime_public_key_fingerprint == binding.public_key_fingerprint - && binding.revoked_at.is_none() - && binding.workspace_trust_id.as_deref() == Some(workspace_trust_id)) - } - - pub fn get_workspace_runtime_verification( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result> { - validate_identifier("workspace_id", workspace_id)?; - validate_identifier("runtime_id", runtime_id)?; - self.with_conn(|conn| { - let evidence = conn - .query_row( - r#"SELECT workspace_id, runtime_id, binding_id, workspace_key_id, - workspace_public_key_fingerprint, workspace_trust_id, - runtime_public_key_fingerprint, - challenge_id, state, last_outcome, verified_at, checked_at - FROM workspace_runtime_verifications - WHERE workspace_id = ?1 AND runtime_id = ?2"#, - params![workspace_id, runtime_id], - read_workspace_runtime_verification, - ) - .optional()?; - if let Some(evidence) = &evidence { - validate_workspace_runtime_verification(evidence)?; - } - Ok(evidence) - }) - } - - pub fn record_workspace_runtime_verification_attempt( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result<()> { - validate_workspace_runtime_verification(evidence)?; - self.with_conn(|conn| { - let changed = conn.execute( - r#"INSERT INTO workspace_runtime_verifications ( - workspace_id, runtime_id, binding_id, workspace_key_id, - workspace_public_key_fingerprint, workspace_trust_id, - runtime_public_key_fingerprint, - challenge_id, state, last_outcome, verified_at, checked_at - ) SELECT ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12 - WHERE EXISTS(SELECT 1 FROM workspace_runtime_bindings binding - WHERE binding.workspace_id=?1 AND binding.runtime_id=?2 - AND binding.binding_id=?3 AND binding.workspace_key_id=?4 - AND binding.workspace_public_key_fingerprint=?5 - AND binding.workspace_trust_id=?6 AND binding.public_key_fingerprint=?7 - AND binding.state IN ('configured','verified') AND binding.revoked_at IS NULL) - ON CONFLICT(workspace_id, runtime_id) DO UPDATE SET - binding_id = excluded.binding_id, - workspace_key_id = excluded.workspace_key_id, - workspace_public_key_fingerprint = excluded.workspace_public_key_fingerprint, - workspace_trust_id = excluded.workspace_trust_id, - runtime_public_key_fingerprint = excluded.runtime_public_key_fingerprint, - challenge_id = excluded.challenge_id, - state = CASE - WHEN workspace_runtime_verifications.state = 'verified' - AND workspace_runtime_verifications.binding_id = excluded.binding_id - AND workspace_runtime_verifications.workspace_key_id = excluded.workspace_key_id - AND workspace_runtime_verifications.workspace_public_key_fingerprint = excluded.workspace_public_key_fingerprint - AND workspace_runtime_verifications.workspace_trust_id = excluded.workspace_trust_id - AND workspace_runtime_verifications.runtime_public_key_fingerprint = excluded.runtime_public_key_fingerprint - THEN workspace_runtime_verifications.state - ELSE excluded.state - END, - last_outcome = excluded.last_outcome, - verified_at = CASE - WHEN workspace_runtime_verifications.state = 'verified' - AND workspace_runtime_verifications.binding_id = excluded.binding_id - AND workspace_runtime_verifications.workspace_key_id = excluded.workspace_key_id - AND workspace_runtime_verifications.workspace_public_key_fingerprint = excluded.workspace_public_key_fingerprint - AND workspace_runtime_verifications.workspace_trust_id = excluded.workspace_trust_id - AND workspace_runtime_verifications.runtime_public_key_fingerprint = excluded.runtime_public_key_fingerprint - THEN workspace_runtime_verifications.verified_at - ELSE excluded.verified_at - END, - checked_at = excluded.checked_at"#, - params![ - evidence.workspace_id, - evidence.runtime_id, - evidence.binding_id, - evidence.workspace_key_id, - evidence.workspace_public_key_fingerprint, - evidence.workspace_trust_id, - evidence.runtime_public_key_fingerprint, - - evidence.challenge_id, - evidence.state, - evidence.last_outcome, - evidence.verified_at, - evidence.checked_at, - ], - )?; - if changed != 1 { - return Err(Error::RuntimeBindingConflict("Runtime verification attempt no longer matches binding authority".into())); - } - Ok(()) - }) - } - - pub fn record_workspace_runtime_verification_outcome_if_current( - &self, - expected: &WorkspaceRuntimeVerificationEvidence, - state: &str, - outcome: &str, - checked_at: &str, - ) -> Result { - validate_workspace_runtime_verification(expected)?; - if !matches!(state, "pending" | "verified" | "failed") { - return Err(Error::Store( - "invalid Workspace Runtime verification state".to_string(), - )); - } - if !matches!( - outcome, - "challenge_issued" | "verified" | "verification_failed" | "connectivity_failed" - ) { - return Err(Error::Store( - "invalid Workspace Runtime verification outcome".to_string(), - )); - } - if checked_at.is_empty() || checked_at.len() > 128 { - return Err(Error::Store( - "invalid Workspace Runtime verification checked_at".to_string(), - )); - } - self.with_conn(|conn| { - let changed = conn.execute( - r#"UPDATE workspace_runtime_verifications - SET state = CASE WHEN state = 'verified' THEN state ELSE ?9 END, - last_outcome = ?10, - checked_at = ?11 - WHERE workspace_id = ?1 AND runtime_id = ?2 - AND binding_id = ?3 - AND workspace_key_id = ?4 - AND workspace_public_key_fingerprint = ?5 - AND workspace_trust_id = ?6 - AND runtime_public_key_fingerprint = ?7 - AND challenge_id = ?8"#, - params![ - expected.workspace_id, - expected.runtime_id, - expected.binding_id, - expected.workspace_key_id, - expected.workspace_public_key_fingerprint, - expected.workspace_trust_id, - expected.runtime_public_key_fingerprint, - expected.challenge_id, - state, - outcome, - checked_at, - ], - )?; - Ok(changed == 1) - }) - } - - pub fn complete_workspace_runtime_verification( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result { - validate_workspace_runtime_verification(evidence)?; - if evidence.state != "verified" - || evidence.last_outcome != "verified" - || evidence.verified_at.is_none() - { - return Err(Error::Store( - "completed Runtime verification evidence must be verified".to_string(), - )); - } - self.with_conn_mut(|conn| { - let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?; - let current_attempt = tx.query_row( - r#"SELECT EXISTS( - SELECT 1 FROM workspace_runtime_verifications - WHERE workspace_id = ?1 AND runtime_id = ?2 - AND binding_id = ?3 - AND workspace_key_id = ?4 - AND workspace_public_key_fingerprint = ?5 - AND workspace_trust_id = ?6 - AND runtime_public_key_fingerprint = ?7 - AND challenge_id = ?8 - )"#, - params![ - evidence.workspace_id, - evidence.runtime_id, - evidence.binding_id, - evidence.workspace_key_id, - evidence.workspace_public_key_fingerprint, - evidence.workspace_trust_id, - evidence.runtime_public_key_fingerprint, - - evidence.challenge_id, - ], - |row| row.get::<_, bool>(0), - )?; - if !current_attempt { - return Err(Error::RuntimeBindingConflict( - "Runtime verification attempt was superseded".to_string(), - )); - } - let changed = tx.execute( - r#"UPDATE workspace_runtime_bindings - SET state = 'verified', updated_at = ?7 - WHERE workspace_id = ?1 AND runtime_id = ?2 - AND binding_id = ?3 - AND state IN ('configured', 'verified') - AND authentication_mode = 'workspace_identity' - AND workspace_key_id = ?4 - AND workspace_trust_id = ?5 - AND workspace_public_key_fingerprint = ?8 - AND public_key_fingerprint = ?6 - AND revoked_at IS NULL - AND EXISTS(SELECT 1 FROM workspace_signing_identities identity - WHERE identity.workspace_id = ?1 AND identity.key_id = ?4 - AND identity.public_key_fingerprint = ?8 AND identity.state = 'active')"#, - params![ - evidence.workspace_id, - evidence.runtime_id, - evidence.binding_id, - evidence.workspace_key_id, - evidence.workspace_trust_id, - evidence.runtime_public_key_fingerprint, - evidence.checked_at, - evidence.workspace_public_key_fingerprint, - ], - )?; - if changed != 1 { - return Err(Error::RuntimeBindingConflict( - "Runtime verification evidence no longer matches the configured binding" - .to_string(), - )); - } - tx.execute( - r#"INSERT INTO workspace_runtime_verifications ( - workspace_id, runtime_id, binding_id, workspace_key_id, - workspace_public_key_fingerprint, workspace_trust_id, - runtime_public_key_fingerprint, - challenge_id, state, last_outcome, verified_at, checked_at - ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) - ON CONFLICT(workspace_id, runtime_id) DO UPDATE SET - binding_id = excluded.binding_id, - workspace_key_id = excluded.workspace_key_id, - workspace_public_key_fingerprint = excluded.workspace_public_key_fingerprint, - workspace_trust_id = excluded.workspace_trust_id, - runtime_public_key_fingerprint = excluded.runtime_public_key_fingerprint, - challenge_id = excluded.challenge_id, - state = excluded.state, - last_outcome = excluded.last_outcome, - verified_at = excluded.verified_at, - checked_at = excluded.checked_at"#, - params![ - evidence.workspace_id, - evidence.runtime_id, - evidence.binding_id, - evidence.workspace_key_id, - evidence.workspace_public_key_fingerprint, - evidence.workspace_trust_id, - evidence.runtime_public_key_fingerprint, - - evidence.challenge_id, - evidence.state, - evidence.last_outcome, - evidence.verified_at, - evidence.checked_at, - ], - )?; - let binding = tx.query_row( - r#"SELECT workspace_id, runtime_id, display_name, base_url, public_key, - public_key_fingerprint, binding_id, state, authentication_mode, - workspace_key_id, workspace_trust_id, created_at, updated_at, revoked_at, workspace_public_key_fingerprint - FROM workspace_runtime_bindings - WHERE workspace_id = ?1 AND runtime_id = ?2"#, - params![evidence.workspace_id, evidence.runtime_id], - read_workspace_runtime_binding, - )?; - tx.commit()?; - Ok(binding) - }) - } - pub fn list_workspace_runtime_binding_audit( &self, workspace_id: &str, @@ -4942,54 +4528,6 @@ impl ControlPlaneStore for SqliteWorkspaceStore { .is_some_and(|binding| binding == *expected && binding.revoked_at.is_none())) } - fn workspace_runtime_verification_matches( - &self, - binding: &WorkspaceRuntimeBinding, - workspace_public_key_fingerprint: &str, - workspace_trust_id: &str, - ) -> Result { - SqliteWorkspaceStore::workspace_runtime_verification_matches( - self, - binding, - workspace_public_key_fingerprint, - workspace_trust_id, - ) - } - - async fn get_workspace_runtime_verification( - &self, - workspace_id: &str, - runtime_id: &str, - ) -> Result> { - SqliteWorkspaceStore::get_workspace_runtime_verification(self, workspace_id, runtime_id) - } - - async fn record_workspace_runtime_verification_attempt( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result<()> { - SqliteWorkspaceStore::record_workspace_runtime_verification_attempt(self, evidence) - } - - async fn record_workspace_runtime_verification_outcome_if_current( - &self, - expected: &WorkspaceRuntimeVerificationEvidence, - state: &str, - outcome: &str, - checked_at: &str, - ) -> Result { - SqliteWorkspaceStore::record_workspace_runtime_verification_outcome_if_current( - self, expected, state, outcome, checked_at, - ) - } - - async fn complete_workspace_runtime_verification( - &self, - evidence: &WorkspaceRuntimeVerificationEvidence, - ) -> Result { - SqliteWorkspaceStore::complete_workspace_runtime_verification(self, evidence) - } - async fn get_workspace_runtime_binding( &self, workspace_id: &str, @@ -10718,65 +10256,6 @@ fn account_select_sql(where_clause: &str) -> String { ) } -fn read_workspace_runtime_verification( - row: &rusqlite::Row<'_>, -) -> rusqlite::Result { - Ok(WorkspaceRuntimeVerificationEvidence { - workspace_id: row.get(0)?, - runtime_id: row.get(1)?, - binding_id: row.get(2)?, - workspace_key_id: row.get(3)?, - workspace_public_key_fingerprint: row.get(4)?, - workspace_trust_id: row.get(5)?, - runtime_public_key_fingerprint: row.get(6)?, - challenge_id: row.get(7)?, - state: row.get(8)?, - last_outcome: row.get(9)?, - verified_at: row.get(10)?, - checked_at: row.get(11)?, - }) -} - -fn validate_workspace_runtime_verification( - evidence: &WorkspaceRuntimeVerificationEvidence, -) -> Result<()> { - for (field, value) in [ - ("workspace_id", evidence.workspace_id.as_str()), - ("runtime_id", evidence.runtime_id.as_str()), - ("workspace_key_id", evidence.workspace_key_id.as_str()), - ("challenge_id", evidence.challenge_id.as_str()), - ] { - validate_identifier(field, value)?; - } - validate_identifier("binding_id", &evidence.binding_id)?; - validate_identifier("workspace_trust_id", &evidence.workspace_trust_id)?; - validate_non_empty( - "workspace_public_key_fingerprint", - &evidence.workspace_public_key_fingerprint, - )?; - validate_non_empty( - "runtime_public_key_fingerprint", - &evidence.runtime_public_key_fingerprint, - )?; - if !matches!(evidence.state.as_str(), "pending" | "verified" | "failed") - || (evidence.state == "verified") != evidence.verified_at.is_some() - { - return Err(Error::InvalidInput( - "Runtime verification lifecycle evidence is invalid".into(), - )); - } - if !matches!( - evidence.last_outcome.as_str(), - "verified" | "challenge_issued" | "verification_failed" | "connectivity_failed" - ) { - return Err(Error::InvalidInput( - "Runtime verification outcome is invalid".to_string(), - )); - } - validate_non_empty("checked_at", &evidence.checked_at)?; - Ok(()) -} - fn read_runtime_removal_operation( row: &rusqlite::Row<'_>, ) -> rusqlite::Result { @@ -10855,7 +10334,7 @@ fn runtime_removal_preflight( ( "other_workspace_binding", "SELECT EXISTS(SELECT 1 FROM workspace_runtime_bindings \ - WHERE runtime_id = ?1 AND workspace_id <> ?2 AND state <> 'revoked')", + WHERE runtime_id = ?1 AND workspace_id <> ?2 AND revoked_at IS NULL)", ), ( "active_worker", @@ -10915,24 +10394,12 @@ fn runtime_removal_preflight( fn read_workspace_runtime_binding( row: &rusqlite::Row<'_>, ) -> rusqlite::Result { - let state = match row.get::<_, String>(7)?.as_str() { - "configured" => WorkspaceRuntimeBindingState::Configured, - "verified" => WorkspaceRuntimeBindingState::Verified, - "revoked" => WorkspaceRuntimeBindingState::Revoked, - value => { - return Err(rusqlite::Error::FromSqlConversionFailure( - 7, - rusqlite::types::Type::Text, - format!("unknown Workspace Runtime binding state {value}").into(), - )); - } - }; - let authentication_mode = match row.get::<_, String>(8)?.as_str() { + let authentication_mode = match row.get::<_, String>(7)?.as_str() { "legacy_server_issuer" => WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer, "workspace_identity" => WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, value => { return Err(rusqlite::Error::FromSqlConversionFailure( - 8, + 7, rusqlite::types::Type::Text, format!("unknown Workspace Runtime authentication mode {value}").into(), )); @@ -10946,14 +10413,10 @@ fn read_workspace_runtime_binding( public_key: row.get(4)?, public_key_fingerprint: row.get(5)?, binding_id: row.get(6)?, - state, authentication_mode, - workspace_key_id: row.get(9)?, - workspace_public_key_fingerprint: row.get(14)?, - workspace_trust_id: row.get(10)?, - created_at: row.get(11)?, - updated_at: row.get(12)?, - revoked_at: row.get(13)?, + created_at: row.get(8)?, + updated_at: row.get(9)?, + revoked_at: row.get(10)?, }) } @@ -11009,56 +10472,17 @@ fn normalize_workspace_runtime_binding_key(record: &mut WorkspaceRuntimeBinding) } record.public_key = canonical; record.public_key_fingerprint = fingerprint; - match record.authentication_mode { - WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer => { - if record.workspace_key_id.is_some() - || record.workspace_trust_id.is_some() - || record.workspace_public_key_fingerprint.is_some() - { - return Err(Error::InvalidInput( - "legacy Runtime bindings must not carry Workspace key metadata".into(), - )); - } - #[cfg(not(test))] - if record.runtime_id != crate::hosts::EMBEDDED_RUNTIME_ID { - return Err(Error::InvalidInput( - "legacy Server issuer authentication is reserved for the embedded Runtime" - .into(), - )); - } - } - WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity => { - let key_id = record.workspace_key_id.as_deref().ok_or_else(|| { - Error::InvalidInput( - "Workspace identity Runtime bindings require workspace_key_id".into(), - ) - })?; - validate_identifier("workspace_key_id", key_id)?; - validate_non_empty( - "workspace_public_key_fingerprint", - record - .workspace_public_key_fingerprint - .as_deref() - .unwrap_or_default(), - )?; - if record - .workspace_trust_id - .as_deref() - .is_none_or(str::is_empty) - { - return Err(Error::InvalidInput( - "Workspace identity Runtime bindings require a non-empty workspace_trust_id" - .into(), - )); - } - } - } - let revoked = record.revoked_at.is_some(); - if (record.state == WorkspaceRuntimeBindingState::Revoked) != revoked { + #[cfg(not(test))] + if record.authentication_mode == WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer + && record.runtime_id != crate::hosts::EMBEDDED_RUNTIME_ID + { return Err(Error::InvalidInput( - "Runtime binding state and revoked_at must agree".into(), + "legacy Server issuer authentication is reserved for the embedded Runtime".into(), )); } + if let Some(revoked_at) = &record.revoked_at { + validate_non_empty("revoked_at", revoked_at)?; + } Ok(()) } @@ -11905,7 +11329,7 @@ pub(crate) fn drive_worker_is_live( }; conn.query_row(&format!("SELECT EXISTS(SELECT 1 FROM {a}worker_registry wr JOIN {a}workspaces ws ON ws.workspace_id=wr.workspace_id WHERE wr.workspace_id=?1 AND wr.runtime_id=?2 AND wr.worker_id=?3 AND ws.state='active' - AND (wr.runtime_id=?4 OR EXISTS(SELECT 1 FROM {a}workspace_runtime_bindings rb WHERE rb.workspace_id=?1 AND rb.runtime_id=?2 AND rb.state='verified' AND rb.revoked_at IS NULL)) + AND (wr.runtime_id=?4 OR EXISTS(SELECT 1 FROM {a}workspace_runtime_bindings rb WHERE rb.workspace_id=?1 AND rb.runtime_id=?2 AND rb.revoked_at IS NULL)) AND NOT EXISTS(SELECT 1 FROM {a}worker_registry_projection_removals rm WHERE rm.workspace_id=?1 AND rm.runtime_id=?2 AND rm.worker_id=?3) AND NOT EXISTS(SELECT 1 FROM {a}worker_tombstones wt WHERE wt.workspace_id=?1 AND wt.runtime_id=?2 AND wt.worker_id=?3) AND NOT EXISTS(SELECT 1 FROM {a}worker_removal_operations ro WHERE ro.workspace_id=?1 AND ro.runtime_id=?2 AND ro.worker_id=?3 AND ro.state IN ('executing','failed','succeeded')))") , @@ -12587,6 +12011,13 @@ fn new_runtime_binding_id() -> String { } include!("frozen_legacy_migrations.rs"); +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum FrozenWorkspaceRuntimeBindingState { + Configured, + Verified, + Revoked, +} + #[allow(dead_code)] #[derive(Clone)] struct FrozenWorkspaceRuntimeBinding { @@ -12597,7 +12028,7 @@ struct FrozenWorkspaceRuntimeBinding { public_key: String, public_key_fingerprint: String, legacy_counter: u64, - state: WorkspaceRuntimeBindingState, + state: FrozenWorkspaceRuntimeBindingState, authentication_mode: WorkspaceRuntimeAuthenticationMode, workspace_key_id: Option, workspace_key_generation: Option, @@ -12609,9 +12040,9 @@ fn read_frozen_workspace_runtime_binding( row: &rusqlite::Row<'_>, ) -> rusqlite::Result { let state = match row.get::<_, String>(7)?.as_str() { - "configured" => WorkspaceRuntimeBindingState::Configured, - "verified" => WorkspaceRuntimeBindingState::Verified, - "revoked" => WorkspaceRuntimeBindingState::Revoked, + "configured" => FrozenWorkspaceRuntimeBindingState::Configured, + "verified" => FrozenWorkspaceRuntimeBindingState::Verified, + "revoked" => FrozenWorkspaceRuntimeBindingState::Revoked, _ => return Err(rusqlite::Error::InvalidQuery), }; let authentication_mode = match row.get::<_, String>(8)?.as_str() { @@ -12643,6 +12074,160 @@ include!("frozen_repository_key_migration.rs"); include!("frozen_runtime_binding_migration.rs"); include!("legacy_evidence_column_migration.rs"); +// Schema 90 archives authentication conclusions rather than treating them as current authority. +fn migrate_runtime_binding_connections_v89_to_v90(conn: &Connection) -> Result<()> { + if current_schema_version(conn)? != 89 { + return Err(Error::Store( + "Runtime connection migration requires schema 89".into(), + )); + } + let foreign_keys: bool = conn.query_row("PRAGMA foreign_keys", [], |r| r.get(0))?; + let legacy_alter: bool = conn.query_row("PRAGMA legacy_alter_table", [], |r| r.get(0))?; + conn.execute_batch("PRAGMA foreign_keys=OFF; PRAGMA legacy_alter_table=ON;")?; + let result = (|| -> Result<()> { + let tx = rusqlite::Transaction::new_unchecked(conn, TransactionBehavior::Exclusive)?; + // Preserve cross-domain removal fences while rebuilding their referenced table. + let triggers = { + let mut stmt = tx.prepare("SELECT name,sql FROM sqlite_schema WHERE type='trigger'")?; + stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? + .collect::>>()? + }; + let indexes = { + let mut stmt = tx.prepare("SELECT sql FROM sqlite_schema WHERE type='index' AND tbl_name='workspace_runtime_bindings' AND sql IS NOT NULL")?; + stmt.query_map([], |r| r.get::<_, String>(0))? + .collect::>>()? + }; + for (name, _) in &triggers { + tx.execute_batch(&format!("DROP TRIGGER \"{}\"", name.replace('"', "\"\"")))?; + } + tx.execute_batch(RUNTIME_BINDING_VERIFICATION_ARCHIVE_SCHEMA)?; + tx.execute_batch("INSERT INTO workspace_runtime_binding_verification_archive + SELECT workspace_id,runtime_id,binding_id,state,workspace_key_id, + workspace_public_key_fingerprint,workspace_trust_id,updated_at + FROM workspace_runtime_bindings; + INSERT INTO workspace_runtime_verification_archive SELECT * FROM workspace_runtime_verifications;")?; + tx.execute_batch(RUNTIME_BINDING_CONNECTION_SCHEMA_V90)?; + tx.execute_batch("INSERT INTO workspace_runtime_bindings_v90 + SELECT workspace_id,runtime_id,display_name,base_url,public_key, + public_key_fingerprint,binding_id,authentication_mode,created_at,updated_at,revoked_at + FROM workspace_runtime_bindings; + DROP TABLE workspace_runtime_verifications; + DROP TABLE workspace_runtime_bindings; + ALTER TABLE workspace_runtime_bindings_v90 RENAME TO workspace_runtime_bindings;")?; + for sql in indexes { + tx.execute_batch(&sql)?; + } + for (_, sql) in triggers { + tx.execute_batch(&sql)?; + } + let violation: bool = tx.query_row( + "SELECT EXISTS(SELECT 1 FROM pragma_foreign_key_check)", + [], + |r| r.get(0), + )?; + if violation { + return Err(Error::Store( + "Runtime connection migration foreign-key violation".into(), + )); + } + verify_runtime_binding_connection_schema(&tx)?; + tx.execute("INSERT INTO __yoi_schema_migrations(version,name) VALUES(90,'Runtime binding connection configuration and historical verification archive')", [])?; + tx.commit()?; + Ok(()) + })(); + let restore = conn + .execute_batch(&format!( + "PRAGMA legacy_alter_table={}; PRAGMA foreign_keys={};", + i32::from(legacy_alter), + i32::from(foreign_keys) + )) + .map_err(Error::from); + result.and(restore) +} + +const RUNTIME_BINDING_CONNECTION_SCHEMA_V90: &str = r#" +CREATE TABLE workspace_runtime_bindings_v90 ( + workspace_id TEXT NOT NULL, + runtime_id TEXT NOT NULL, + display_name TEXT NOT NULL, + base_url TEXT NOT NULL, + public_key TEXT NOT NULL, + public_key_fingerprint TEXT NOT NULL, + binding_id TEXT NOT NULL CHECK (length(binding_id) > 0), + authentication_mode TEXT NOT NULL CHECK (authentication_mode IN ('legacy_server_issuer', 'workspace_identity')), + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + revoked_at TEXT, + PRIMARY KEY (workspace_id, runtime_id), + UNIQUE (workspace_id, public_key_fingerprint), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE RESTRICT +);"#; + +// No binding foreign key: later mutation, reactivation, and administrative removal +// must neither mutate nor erase migrated historical authentication evidence. +const RUNTIME_BINDING_VERIFICATION_ARCHIVE_SCHEMA: &str = r#" +CREATE TABLE workspace_runtime_binding_verification_archive ( + workspace_id TEXT NOT NULL, + runtime_id TEXT NOT NULL, + binding_id TEXT NOT NULL, + state TEXT NOT NULL, + workspace_key_id TEXT, + workspace_public_key_fingerprint TEXT, + workspace_trust_id TEXT, + updated_at TEXT NOT NULL, + PRIMARY KEY (workspace_id, runtime_id, binding_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE CASCADE +); +CREATE TABLE workspace_runtime_verification_archive ( + workspace_id TEXT NOT NULL, + runtime_id TEXT NOT NULL, + binding_id TEXT NOT NULL, + workspace_key_id TEXT NOT NULL, + workspace_public_key_fingerprint TEXT NOT NULL, + workspace_trust_id TEXT NOT NULL, + runtime_public_key_fingerprint TEXT NOT NULL, + challenge_id TEXT NOT NULL, + state TEXT NOT NULL, + last_outcome TEXT NOT NULL, + verified_at TEXT, + checked_at TEXT NOT NULL, + PRIMARY KEY (workspace_id, runtime_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) ON DELETE CASCADE +);"#; + +fn verify_runtime_binding_connection_schema(conn: &Connection) -> Result<()> { + let expected = [ + "workspace_id", + "runtime_id", + "display_name", + "base_url", + "public_key", + "public_key_fingerprint", + "binding_id", + "authentication_mode", + "created_at", + "updated_at", + "revoked_at", + ]; + let actual = table_columns(conn, "workspace_runtime_bindings")?; + if actual != expected || table_exists(conn, "workspace_runtime_verifications")? { + return Err(Error::Store( + "Runtime binding does not match connection-only schema".into(), + )); + } + for table in [ + "workspace_runtime_binding_verification_archive", + "workspace_runtime_verification_archive", + ] { + if !table_exists(conn, table)? { + return Err(Error::Store(format!( + "missing historical Runtime verification archive {table}" + ))); + } + } + Ok(()) +} + fn prepare_connection(conn: &Connection) -> Result<()> { prepare_connection_with_secret_source( conn, @@ -12711,6 +12296,9 @@ struct WorkspaceRuntimeBindingV51 { } fn verify_workspace_runtime_verification_schema(conn: &Connection) -> Result<()> { + if current_schema_version(conn)? >= 90 { + return verify_runtime_binding_connection_schema(conn); + } if column_exists(conn, "workspace_runtime_verifications", "binding_id")? { return verify_runtime_binding_content_schema(conn); } @@ -12744,6 +12332,9 @@ fn verify_workspace_runtime_verification_schema(conn: &Connection) -> Result<()> } fn verify_workspace_signing_identity_schema(conn: &Connection) -> Result<()> { + if current_schema_version(conn)? >= 90 { + return verify_runtime_binding_connection_schema(conn); + } if !column_exists(conn, "workspace_signing_identities", "revision")? { return verify_runtime_binding_content_schema(conn); } @@ -12872,6 +12463,9 @@ fn verify_workspace_deletion_schema(conn: &Connection) -> Result<()> { } fn verify_workspace_runtime_binding_schema(conn: &Connection) -> Result<()> { + if current_schema_version(conn)? >= 90 { + return verify_runtime_binding_connection_schema(conn); + } if column_exists(conn, "workspace_runtime_bindings", "binding_id")? { return verify_runtime_binding_content_schema(conn); } @@ -13034,9 +12628,9 @@ fn verify_workspace_runtime_binding_schema(conn: &Connection) -> Result<()> { public_key_fingerprint: row.get(5)?, legacy_counter: row.get(6)?, state: if row.get::<_, Option>(9)?.is_some() { - WorkspaceRuntimeBindingState::Revoked + FrozenWorkspaceRuntimeBindingState::Revoked } else { - WorkspaceRuntimeBindingState::Verified + FrozenWorkspaceRuntimeBindingState::Verified }, authentication_mode: WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer, workspace_key_id: None, @@ -13466,7 +13060,7 @@ fn apply_ticket_targets_and_workdir_capabilities_schema(conn: &Connection) -> Re ], )?; // This retained step composes the historical Ticket schema 7. - // Current-domain verification follows the shared case migration at schema 90. + // Current-domain verification follows the shared case migration at schema 91. let foreign_key_violations = db.query_row("SELECT COUNT(*) FROM pragma_foreign_key_check", [], |row| { @@ -13531,21 +13125,21 @@ fn read_workspace_config_grant( fn install_ticket_case_assignment_work_schema(conn: &Connection) -> Result<()> { let (_, sql) = include_str!("latest_schema.sql") - .split_once("-- Ticket case assignment work constraints (schema 90).") + .split_once("-- Ticket case assignment work constraints (schema 91).") .ok_or_else(|| Error::Store("case assignment schema resource is missing".into()))?; conn.execute_batch(sql)?; Ok(()) } -fn migrate_ticket_cases_v89_to_v90(conn: &Connection) -> Result<()> { - if current_schema_version(conn)? != 89 { +fn migrate_ticket_cases_v90_to_v91(conn: &Connection) -> Result<()> { + if current_schema_version(conn)? != 90 { return Err(Error::Store( - "Ticket case migration requires schema 89".into(), + "Ticket case migration requires schema 90".into(), )); } // The shared Ticket migration owns the six-state snapshot, unknown legacy // closure reasons and immutable case history. It opens its own transaction. - // Leave the Workspace marker at 89 until our composed constraints commit; + // Leave the Workspace marker at 90 until our composed constraints commit; // a failed second phase is resumable through the idempotent domain migrator. ticket::migrate_sqlite_ticket_schema(conn) .map_err(|error| Error::Store(format!("Ticket case migration failed: {error}")))?; @@ -13571,7 +13165,7 @@ fn migrate_ticket_cases_v89_to_v90(conn: &Connection) -> Result<()> { [], )?; tx.execute( - "INSERT INTO __yoi_schema_migrations(version, name) VALUES (90, 'Ticket open/closed cases and retained routing evidence')", [], + "INSERT INTO __yoi_schema_migrations(version, name) VALUES (91, 'Ticket open/closed cases and retained routing evidence')", [], )?; verify_workspace_resource_constraints(&tx)?; let violations: i64 = @@ -13733,7 +13327,7 @@ fn verify_workspace_resource_constraints(conn: &Connection) -> Result<()> { } } } - if current_schema_version(conn)? >= 90 { + if current_schema_version(conn)? >= 91 { for (name, predicate) in [ ( "ticket_active_worker_assignments", @@ -13767,7 +13361,7 @@ fn verify_workspace_resource_constraints(conn: &Connection) -> Result<()> { )?; if !sql.contains(predicate) || sql.contains("'done'") { return Err(Error::Store(format!( - "Ticket case constraint {name} does not match schema 90" + "Ticket case constraint {name} does not match schema 91" ))); } } @@ -15418,11 +15012,11 @@ mod tests { VALUES ('workspace-a', 'owner', 'Workspace A', 'active', '1', '1'); \ INSERT INTO workspace_runtime_bindings(\ workspace_id, runtime_id, display_name, base_url, public_key, \ - public_key_fingerprint, binding_id, state, authentication_mode, \ + public_key_fingerprint, binding_id, authentication_mode, \ created_at, updated_at\ ) VALUES (\ 'workspace-a', 'runtime-a', 'Runtime A', 'https://runtime.invalid', \ - 'key-a', 'fingerprint-a', 'binding-live', 'verified', 'legacy_server_issuer', '1', '1'\ + 'key-a', 'fingerprint-a', 'binding-live', 'legacy_server_issuer', '1', '1'\ );", )?; Ok(()) @@ -15440,7 +15034,7 @@ mod tests { .unwrap() .expect("Runtime binding must remain"); assert_eq!(binding.binding_id, "binding-live"); - assert_eq!(binding.state, WorkspaceRuntimeBindingState::Verified); + assert!(binding.revoked_at.is_none()); } @@ -15598,11 +15192,11 @@ mod tests { VALUES ('workspace-b', 'owner-b', 'Workspace B', 'active', '1', '1'); \ INSERT INTO workspace_runtime_bindings(\ workspace_id, runtime_id, display_name, base_url, public_key, \ - public_key_fingerprint, binding_id, state, authentication_mode, \ + public_key_fingerprint, binding_id, authentication_mode, \ created_at, updated_at\ ) VALUES (\ 'workspace-b', 'runtime-a', 'Runtime A', 'https://runtime.invalid', \ - 'key-a', 'fingerprint-b', 'binding-other', 'verified', 'legacy_server_issuer', '1', '1'\ + 'key-a', 'fingerprint-b', 'binding-other', 'legacy_server_issuer', '1', '1'\ );", )?; Ok(()) @@ -15667,11 +15261,11 @@ mod tests { let blocked_insert = competing_conn.execute( "INSERT INTO workspace_runtime_bindings(\ workspace_id, runtime_id, display_name, base_url, public_key, \ - public_key_fingerprint, binding_id, state, authentication_mode, \ + public_key_fingerprint, binding_id, authentication_mode, \ created_at, updated_at\ ) VALUES (\ 'workspace-b', 'runtime-a', 'Runtime A', 'https://runtime.invalid', \ - 'key-a', 'fingerprint-b', 'binding-other', 'verified', 'legacy_server_issuer', '1', '1'\ + 'key-a', 'fingerprint-b', 'binding-other', 'legacy_server_issuer', '1', '1'\ )", [], ); @@ -15992,7 +15586,7 @@ mod tests { migrate_ticket_targets_and_workdir_capabilities_v65_to_v66(&conn).unwrap(); assert_eq!(current_schema_version(&conn).unwrap(), 66); - // The retained step is Ticket-7; current verification runs after schema 90. + // The retained step is Ticket-7; current verification runs after schema 91. assert!(!column_exists(&conn, "typed_tickets", "repository_id").unwrap()); assert!(!column_exists(&conn, "typed_tickets", "ref_selector").unwrap()); assert_eq!( @@ -16715,6 +16309,10 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx }, WorkspaceSchemaMigrationStep { version: 90, + name: "Runtime binding connection configuration and historical verification archive".to_string() + }, + WorkspaceSchemaMigrationStep { + version: 91, name: "Ticket open/closed cases and retained routing evidence".to_string() }, ] @@ -16816,7 +16414,8 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx (87, "Repository SSH key identities and encrypted operation objects".to_string()), (88, "Runtime binding identities and retention content contracts".to_string()), (89, "descriptive legacy evidence columns".to_string()), -(90, "Ticket open/closed cases and retained routing evidence".to_string()), +(90, "Runtime binding connection configuration and historical verification archive".to_string()), +(91, "Ticket open/closed cases and retained routing evidence".to_string()), ] ); assert!(!table_exists(conn, "trusted_runtime_records")?); @@ -16830,17 +16429,17 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx ); assert_eq!( conn.query_row( - "SELECT state || ':' || authentication_mode + "SELECT authentication_mode FROM workspace_runtime_bindings WHERE workspace_id='workspace-a' AND runtime_id='shared'", [], |row| row.get::<_, String>(0), )?, - "configured:workspace_identity" + "workspace_identity" ); assert_eq!( conn.query_row( - "SELECT COUNT(*) FROM workspace_runtime_bindings + "SELECT COUNT(*) FROM workspace_runtime_binding_verification_archive WHERE workspace_id='workspace-a' AND runtime_id='shared' AND workspace_key_id IS NOT NULL AND length(binding_id) > 0 @@ -17618,11 +17217,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx public_key: identity.public_key.clone(), public_key_fingerprint: String::new(), binding_id: String::new(), - state: WorkspaceRuntimeBindingState::Verified, authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some(format!("WK-{}", &workspace_id["workspace-".len()..])), - workspace_trust_id: Some("trust-fixture".into()), - workspace_public_key_fingerprint: Some("sha256:key".into()), created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -17740,7 +17335,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx } #[test] - fn workspace_runtime_verification_is_key_bound_and_restart_safe() { + fn workspace_runtime_connection_persists_without_authentication_evidence() { let temp = tempfile::tempdir().unwrap(); let path = temp.path().join("server.db"); let store = SqliteWorkspaceStore::open(&path).unwrap(); @@ -17774,11 +17369,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx public_key: runtime_identity.public_key, public_key_fingerprint: String::new(), binding_id: String::new(), - state: WorkspaceRuntimeBindingState::Configured, authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-a".to_string()), - workspace_trust_id: Some("trust-fixture".into()), - workspace_public_key_fingerprint: Some("sha256:key".into()), created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -17790,106 +17381,14 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx .get_workspace_runtime_binding("workspace-a", "runtime-a") .unwrap() .unwrap(); - let evidence = WorkspaceRuntimeVerificationEvidence { - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-a".to_string(), - binding_id: persisted.binding_id.clone(), - workspace_key_id: "WK-a".to_string(), - workspace_public_key_fingerprint: "sha256:key".into(), - workspace_trust_id: "trust-fixture".into(), - runtime_public_key_fingerprint: persisted.public_key_fingerprint.clone(), - - challenge_id: "challenge-a".to_string(), - state: "verified".to_string(), - last_outcome: "verified".to_string(), - verified_at: Some("2".to_string()), - checked_at: "2".to_string(), - }; - store - .record_workspace_runtime_verification_attempt(&evidence) - .unwrap(); - let verified = store - .complete_workspace_runtime_verification(&evidence) - .unwrap(); - assert_eq!(verified.state, WorkspaceRuntimeBindingState::Verified); - let pending_retry = WorkspaceRuntimeVerificationEvidence { - state: "pending".to_string(), - last_outcome: "challenge_issued".to_string(), - verified_at: None, - checked_at: "3".to_string(), - ..evidence.clone() - }; - store - .record_workspace_runtime_verification_attempt(&pending_retry) - .unwrap(); - let retained = store - .get_workspace_runtime_verification("workspace-a", "runtime-a") - .unwrap() - .unwrap(); - assert_eq!(retained.state, "verified"); - assert_eq!(retained.verified_at.as_deref(), Some("2")); - assert_eq!(retained.last_outcome, "challenge_issued"); - assert!( - !store - .workspace_runtime_verification_matches(&verified, "sha256:key", "trust-fixture") - .unwrap() - ); - store - .complete_workspace_runtime_verification(&evidence) - .unwrap(); - let newer_pending = WorkspaceRuntimeVerificationEvidence { - challenge_id: "challenge-b".to_string(), - state: "pending".to_string(), - last_outcome: "challenge_issued".to_string(), - verified_at: None, - checked_at: "4".to_string(), - ..evidence.clone() - }; - store - .record_workspace_runtime_verification_attempt(&newer_pending) - .unwrap(); - let newer_verified = WorkspaceRuntimeVerificationEvidence { - state: "verified".to_string(), - last_outcome: "verified".to_string(), - verified_at: Some("5".to_string()), - checked_at: "5".to_string(), - ..newer_pending - }; - store - .complete_workspace_runtime_verification(&newer_verified) - .unwrap(); - assert!( - !store - .record_workspace_runtime_verification_outcome_if_current( - &pending_retry, - "failed", - "verification_failed", - "6", - ) - .unwrap() - ); - assert_eq!( - store - .get_workspace_runtime_verification("workspace-a", "runtime-a") - .unwrap(), - Some(newer_verified.clone()) - ); + assert!(store.workspace_runtime_binding_matches(&persisted).unwrap()); drop(store); - let reopened = SqliteWorkspaceStore::open(&path).unwrap(); - assert_eq!( - reopened - .get_workspace_runtime_verification("workspace-a", "runtime-a") - .unwrap(), - Some(newer_verified) - ); assert_eq!( reopened .get_workspace_runtime_binding("workspace-a", "runtime-a") - .unwrap() - .unwrap() - .state, - WorkspaceRuntimeBindingState::Verified + .unwrap(), + Some(persisted) ); } @@ -17897,22 +17396,15 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx fn schema_v55_migrates_runtime_verification_table_atomically() { let temp = tempfile::tempdir().unwrap(); let path = temp.path().join("server.db"); - let store = SqliteWorkspaceStore::open(&path).unwrap(); - store - .with_conn(|conn| { - conn.execute_batch( - "DROP TABLE workspace_runtime_verifications; - DELETE FROM __yoi_schema_migrations; - INSERT INTO __yoi_schema_migrations(version, name) - VALUES (55, 'Workspace Runtime binding state and identity mode');", - )?; - Ok(()) - }) - .unwrap(); - drop(store); - + prepare_schema_v50(&path, Some("workspace-a")); let conn = Connection::open(&path).unwrap(); configure_sqlite(&conn).unwrap(); + for migration in MIGRATIONS + .iter() + .filter(|migration| migration.version > 50 && migration.version <= 55) + { + (migration.apply)(&conn).unwrap(); + } assert_eq!(current_schema_version(&conn).unwrap(), 55); migrate_workspace_runtime_verification_v55_to_v56(&conn).unwrap(); drop(conn); @@ -18169,11 +17661,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx public_key, public_key_fingerprint: String::new(), binding_id: String::new(), - state: WorkspaceRuntimeBindingState::Configured, authentication_mode: WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity, - workspace_key_id: Some("WK-a".to_string()), - workspace_trust_id: Some("trust-fixture".into()), - workspace_public_key_fingerprint: Some("sha256:key".into()), created_at: at.to_string(), updated_at: at.to_string(), revoked_at: None, @@ -18188,19 +17676,11 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx .unwrap(); assert_eq!(created, WorkspaceRuntimeBindingMutation::Created); assert!(!created_binding.binding_id.is_empty()); - assert_eq!( - created_binding.state, - WorkspaceRuntimeBindingState::Configured - ); + assert!(created_binding.revoked_at.is_none()); assert_eq!( created_binding.authentication_mode, WorkspaceRuntimeAuthenticationMode::WorkspaceIdentity ); - assert_eq!(created_binding.workspace_key_id.as_deref(), Some("WK-a")); - assert_eq!( - created_binding.workspace_trust_id.as_deref(), - Some("trust-fixture") - ); let (replayed, replayed_binding) = store .put_workspace_runtime_binding_key(binding(first.public_key, "2"), None, "owner") .unwrap(); @@ -18239,29 +17719,6 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx .unwrap(); assert_eq!(replaced, WorkspaceRuntimeBindingMutation::Replaced); assert_ne!(replaced_binding.binding_id, created_binding.binding_id); - store - .record_workspace_runtime_verification_attempt(&WorkspaceRuntimeVerificationEvidence { - workspace_id: "workspace-a".to_string(), - runtime_id: "runtime-a".to_string(), - binding_id: replaced_binding.binding_id.clone(), - workspace_key_id: "WK-a".to_string(), - workspace_public_key_fingerprint: "sha256:key".into(), - workspace_trust_id: "trust-fixture".into(), - runtime_public_key_fingerprint: replaced_binding.public_key_fingerprint.clone(), - - challenge_id: "challenge-a".to_string(), - state: "failed".to_string(), - last_outcome: "connectivity_failed".to_string(), - verified_at: None, - checked_at: "3".to_string(), - }) - .unwrap(); - assert!( - store - .get_workspace_runtime_verification("workspace-a", "runtime-a") - .unwrap() - .is_some() - ); let (revoked, revoked_binding) = store .revoke_workspace_runtime_binding_key( "workspace-a", @@ -18274,12 +17731,15 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx assert_eq!(revoked, WorkspaceRuntimeBindingMutation::Revoked); assert_ne!(revoked_binding.binding_id, replaced_binding.binding_id); assert_eq!(revoked_binding.revoked_at.as_deref(), Some("4")); - assert_eq!(revoked_binding.state, WorkspaceRuntimeBindingState::Revoked); - assert_eq!( - store - .get_workspace_runtime_verification("workspace-a", "runtime-a") - .unwrap(), - None + assert!( + !store + .workspace_runtime_binding_matches(&replaced_binding) + .unwrap() + ); + assert!( + !store + .workspace_runtime_binding_matches(&revoked_binding) + .unwrap() ); let (reactivated, reactivated_binding) = store .put_workspace_runtime_binding_key( @@ -18290,10 +17750,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx .unwrap(); assert_eq!(reactivated, WorkspaceRuntimeBindingMutation::Reactivated); assert_ne!(reactivated_binding.binding_id, revoked_binding.binding_id); - assert_eq!( - reactivated_binding.state, - WorkspaceRuntimeBindingState::Configured - ); + assert!(reactivated_binding.revoked_at.is_none()); let mut duplicate = binding(second.public_key, "6"); duplicate.runtime_id = "runtime-b".to_string(); @@ -18344,11 +17801,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx public_key, public_key_fingerprint: String::new(), binding_id: String::new(), - state: WorkspaceRuntimeBindingState::Verified, authentication_mode: WorkspaceRuntimeAuthenticationMode::LegacyServerIssuer, - workspace_key_id: None, - workspace_trust_id: None, - workspace_public_key_fingerprint: None, created_at: "1".to_string(), updated_at: "1".to_string(), revoked_at: None, @@ -19325,9 +18778,10 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx ); } - // SQLite boundary: a genuine retained Workspace-89 / Ticket-7 fixture, - // not a new database with a lower version marker. - fn legacy_case_workspace() -> (tempfile::TempDir, std::path::PathBuf) { + // SQLite boundary: genuine retained Workspace-89 or -90 / Ticket-7, + // including the real authentication cutover, not a lowered version marker. + fn legacy_case_workspace(version: i64) -> (tempfile::TempDir, std::path::PathBuf) { + assert!(matches!(version, 89 | 90)); let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("cases.db"); prepare_retained_schema(&path, 89); @@ -19339,13 +18793,45 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx VALUES('case-workspace','case-owner','Cases','active','1','1'); INSERT INTO repositories(workspace_id,repository_id,repository_key,kind,provider,uri,default_ref,created_at,updated_at,source_kind,source_uri,source_fingerprint,observed_status) VALUES('case-workspace','case-repo','main','git','git','/fixture','develop','1','1','local_path','/fixture','sha256:fixture','unverified');").unwrap(); + conn.execute_batch(r#" + INSERT INTO workspace_signing_identities(workspace_id,key_id,algorithm,public_key,public_key_fingerprint,private_material_ref,state,created_at,provisioned_at,updated_at) + VALUES('case-workspace','case-key','ed25519','workspace-public-key','workspace-fingerprint','private-material','active','1','2','2'); + INSERT INTO workspace_signing_identity_provisioning_operations(operation_key,request_fingerprint,operation_kind,workspace_id,key_id,private_material_ref,actor_account_id,state,created_at,completed_at) + VALUES('case-provision','original-provision','existing_workspace','case-workspace','case-key','private-material','case-owner','completed','1','2'); + INSERT INTO workspace_signing_identity_audit(event_id,workspace_id,key_id,action,public_key_fingerprint,actor_account_id,created_at) + VALUES('case-key-audit','case-workspace','case-key','provisioned','workspace-fingerprint','case-owner','2'); + INSERT INTO workspace_runtime_bindings(workspace_id,runtime_id,display_name,base_url,public_key,public_key_fingerprint,binding_id,state,authentication_mode,workspace_key_id,workspace_public_key_fingerprint,workspace_trust_id,created_at,updated_at) + VALUES('case-workspace','case-runtime','Verified runtime','https://runtime.invalid','runtime-public-key','runtime-fingerprint','case-binding','verified','workspace_identity','case-key','workspace-fingerprint','case-trust','1','2'), + ('case-workspace','legacy-runtime','Legacy runtime','https://legacy.invalid','legacy-public-key','legacy-fingerprint','legacy-binding','configured','legacy_server_issuer',NULL,NULL,NULL,'1','2'); + INSERT INTO workspace_runtime_verifications(workspace_id,runtime_id,binding_id,workspace_key_id,workspace_public_key_fingerprint,workspace_trust_id,runtime_public_key_fingerprint,challenge_id,state,last_outcome,verified_at,checked_at) + VALUES('case-workspace','case-runtime','case-binding','case-key','workspace-fingerprint','case-trust','runtime-fingerprint','case-challenge','verified','original-verification','2','2'); + INSERT INTO workspace_runtime_binding_audit(workspace_id,runtime_id,actor_account_id,action,old_fingerprint,new_fingerprint,binding_id,at) + VALUES('case-workspace','case-runtime','case-owner','created',NULL,'runtime-fingerprint','case-binding','1'); + "#).unwrap(); + if version == 90 { + migrate_runtime_binding_connections_v89_to_v90(&conn).unwrap(); + verify_runtime_binding_connection_schema(&conn).unwrap(); + } + assert_eq!(current_schema_version(&conn).unwrap(), version); + assert_eq!( + conn.query_row( + "SELECT MAX(version) FROM ticket_schema_migrations", + [], + |row| row.get::<_, i64>(0) + ) + .unwrap(), + 7 + ); + verify_workspace_resource_constraints(&conn).unwrap(); (dir, path) } fn stored_fixture_rows(conn: &Connection, table: &str) -> Vec> { - let mut stmt = conn - .prepare(&format!("SELECT * FROM {table} ORDER BY rowid")) - .unwrap(); + fixture_query_rows(conn, &format!("SELECT * FROM {table} ORDER BY rowid")) + } + + fn fixture_query_rows(conn: &Connection, sql: &str) -> Vec> { + let mut stmt = conn.prepare(sql).unwrap(); let columns = stmt.column_count(); stmt.query_map([], |row| (0..columns).map(|i| row.get(i)).collect()) .unwrap() @@ -19353,11 +18839,39 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx .unwrap() } + // Project schema-89 live conclusions into the exact frozen archive layout. + // At schema 90+ the same values must already be historical, never live auth. + fn fixture_auth_rows(conn: &Connection) -> Vec>> { + let (binding_evidence, verification_evidence) = if current_schema_version(conn).unwrap() + == 89 + { + ( + "SELECT workspace_id,runtime_id,binding_id,state,workspace_key_id,workspace_public_key_fingerprint,workspace_trust_id,updated_at FROM workspace_runtime_bindings ORDER BY runtime_id", + "SELECT * FROM workspace_runtime_verifications ORDER BY runtime_id", + ) + } else { + ( + "SELECT * FROM workspace_runtime_binding_verification_archive ORDER BY runtime_id", + "SELECT * FROM workspace_runtime_verification_archive ORDER BY runtime_id", + ) + }; + ["SELECT workspace_id,runtime_id,display_name,base_url,public_key,public_key_fingerprint,binding_id,authentication_mode,created_at,updated_at,revoked_at FROM workspace_runtime_bindings ORDER BY runtime_id", binding_evidence, verification_evidence] + .map(|sql| fixture_query_rows(conn, sql)).to_vec() + } + #[test] - fn schema_v89_cases_preserve_all_six_states_evidence_and_responsibility_without_starting_workers() - { + fn schema_v89_runs_auth_then_cases_preserving_legacy_states_evidence_and_responsibility() { + assert_legacy_cases_preserved(89); + } + + #[test] + fn schema_v90_cases_preserve_auth_archive_legacy_states_evidence_and_responsibility() { + assert_legacy_cases_preserved(90); + } + + fn assert_legacy_cases_preserved(version: i64) { use ticket::TicketBackend; - let (_dir, path) = legacy_case_workspace(); + let (_dir, path) = legacy_case_workspace(version); let conn = Connection::open(&path).unwrap(); configure_sqlite(&conn).unwrap(); for state in [ @@ -19459,14 +18973,43 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx "merge_requests", "merge_request_ticket_relations", "merge_request_thread_events", + "workspace_runtime_binding_audit", + "workspace_signing_identities", + "workspace_signing_identity_audit", + "workspace_signing_identity_provisioning_operations", ]; let before = unchanged.map(|table| stored_fixture_rows(&conn, table)); + let auth_before = fixture_auth_rows(&conn); + assert_eq!( + auth_before.iter().map(Vec::len).collect::>(), + [2, 2, 1] + ); + let plan = SqliteWorkspaceStore::migration_plan(&path).unwrap(); + assert_eq!(plan.current_schema_version, version); + assert_eq!(plan.target_schema_version, 91); + assert_eq!( + plan.migrations + .iter() + .map(|step| step.version) + .collect::>(), + if version == 89 { + vec![90, 91] + } else { + vec![91] + } + ); drop(conn); let store = SqliteWorkspaceStore::open(&path).unwrap(); let backend = ticket::SqliteTicketBackend::open_verified(&path, "case-workspace").unwrap(); store .with_conn(|conn| { - assert_eq!(current_schema_version(conn)?, 90); + assert_eq!(current_schema_version(conn)?, 91); + assert_eq!(fixture_auth_rows(conn), auth_before); + verify_runtime_binding_connection_schema(conn)?; + ticket::verify_sqlite_ticket_schema(conn).unwrap(); + assert_eq!(workspace_schema_migration_history(conn)?.into_iter().filter(|(version, _)| *version >= 90).collect::>(), + vec![(90, "Runtime binding connection configuration and historical verification archive".into()), + (91, "Ticket open/closed cases and retained routing evidence".into())]); for (table, rows) in unchanged.iter().zip(before.iter()) { assert_eq!(&stored_fixture_rows(conn, table), rows, "preserve {table}"); } @@ -19509,6 +19052,14 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx assert_eq!(ticket.document.body.as_str(), "Original requirements"); let snapshot = ticket.legacy_workflow_snapshot.as_ref().unwrap(); assert_eq!(snapshot.workflow_state, state); + assert_eq!( + snapshot.queued_by.as_deref(), + (state == "queued").then_some("queue-author") + ); + assert_eq!( + snapshot.queued_at.as_deref(), + (state == "queued").then_some("queue-time") + ); assert_eq!( snapshot.resolution.as_deref(), Some("Original closure text") @@ -19567,6 +19118,15 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx } } } + for id in ["pending-queue", "only-queue"] { + let queued = backend.show(id.into()).unwrap(); + assert_eq!(queued.meta.workflow_state, ticket::TicketState::Open); + let snapshot = queued.legacy_workflow_snapshot.unwrap(); + assert_eq!(snapshot.workflow_state, "queued"); + assert_eq!(snapshot.queued_by.as_deref(), Some("queue-author")); + assert_eq!(snapshot.queued_at.as_deref(), Some("queue-time")); + assert!(queued.case_history.is_empty()); + } assert!( store .get_current_ticket_worker_assignment("case-workspace", "only-queue") @@ -19589,7 +19149,25 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx Some("pending-fingerprint") ); drop(store); + drop(backend); let reopened = SqliteWorkspaceStore::open(&path).unwrap(); + reopened + .with_conn(|conn| { + assert_eq!(current_schema_version(conn)?, 91); + assert_eq!(fixture_auth_rows(conn), auth_before); + Ok(()) + }) + .unwrap(); + let backend = ticket::SqliteTicketBackend::open_verified(&path, "case-workspace").unwrap(); + let reopened_case = backend.show("done-true".into()).unwrap(); + assert_eq!(reopened_case.meta.workflow_state, ticket::TicketState::Open); + assert_eq!(reopened_case.case_history.len(), 2); + assert_eq!( + reopened_case.case_history[0].action, + ticket::TicketCaseAction::Close { + reason: ticket::TicketClosureReason::Unknown + } + ); assert!( reopened .get_active_ticket_worker_assignment("case-workspace", "done-true") @@ -19606,26 +19184,29 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx } #[test] - fn schema_v90_resumes_after_shared_ticket_migration_without_duplicating_judgments() { + fn schema_v91_resumes_after_shared_ticket_migration_without_duplicating_judgments() { use ticket::TicketBackend; - let (_dir, path) = legacy_case_workspace(); + let (_dir, path) = legacy_case_workspace(90); let conn = Connection::open(&path).unwrap(); configure_sqlite(&conn).unwrap(); conn.execute_batch("INSERT INTO typed_tickets(workspace_id,ticket_id,slug,title,status,kind,priority,body,workflow_state,workflow_state_explicit) VALUES('case-workspace','terminal','terminal','Terminal','closed','task','P2','','done',1); DROP VIEW ticket_active_worker_assignments; CREATE TABLE ticket_active_worker_assignments(collision TEXT);").unwrap(); - let error = migrate_ticket_cases_v89_to_v90(&conn).unwrap_err(); + let auth_before = fixture_auth_rows(&conn); + let error = migrate_ticket_cases_v90_to_v91(&conn).unwrap_err(); assert!(error.to_string().contains("DROP TABLE")); - assert_eq!(current_schema_version(&conn).unwrap(), 89); + assert_eq!(current_schema_version(&conn).unwrap(), 90); + assert_eq!(fixture_auth_rows(&conn), auth_before); // The shared transaction completed, but Workspace did not advertise a // composed schema. Its immutable evidence remains the retry authority. let before = stored_fixture_rows(&conn, "typed_ticket_case_history"); assert_eq!(before.len(), 1); conn.execute_batch("DROP TABLE ticket_active_worker_assignments;") .unwrap(); - migrate_ticket_cases_v89_to_v90(&conn).unwrap(); - assert_eq!(current_schema_version(&conn).unwrap(), 90); + migrate_ticket_cases_v90_to_v91(&conn).unwrap(); + assert_eq!(current_schema_version(&conn).unwrap(), 91); + assert_eq!(fixture_auth_rows(&conn), auth_before); assert_eq!( stored_fixture_rows(&conn, "typed_ticket_case_history"), before @@ -19742,7 +19323,80 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx } #[test] - fn schema_v90_case_constraint_verification_rejects_legacy_view_predicates() { + fn schema_v91_fresh_and_retained_cases_and_auth_enforce_equivalent_constraints() { + // Compare SQLite boundary contracts, including guards lost by a parent + // table rebuild, rather than only the latest version marker. Redundant + // indexes with the same uniqueness contract are one semantic constraint. + fn signature(conn: &Connection, object: &str) -> Vec> { + [ + "SELECT name,type,CAST(\"notnull\" AS TEXT),COALESCE(dflt_value,''),CAST(pk AS TEXT) FROM pragma_table_info(?1) ORDER BY name", + "SELECT \"table\",\"from\",\"to\",on_update,on_delete,match,CAST(seq AS TEXT) FROM pragma_foreign_key_list(?1) ORDER BY \"table\",\"from\",\"to\",seq", + "SELECT DISTINCT CAST(i.\"unique\" AS TEXT),CAST(i.partial AS TEXT),(SELECT group_concat(name,',') FROM (SELECT name FROM pragma_index_info(i.name) ORDER BY seqno)) FROM pragma_index_list(?1) AS i ORDER BY 1,2,3", + "SELECT name,sql FROM sqlite_schema WHERE (type='trigger' AND tbl_name=?1) OR (type='view' AND name=?1) ORDER BY name", + ].into_iter().flat_map(|sql| { + let mut stmt = conn.prepare(sql).unwrap(); + let count = stmt.column_count(); + stmt.query_map([object], |row| { + (0..count).map(|i| row.get::<_, String>(i)).collect::>>() + }).unwrap().map(|row| row.unwrap().into_iter().map(|value| { + value.chars().filter(|c| !c.is_whitespace() && *c != '"').collect() + }).collect()).collect::>>() + }).collect() + } + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("equivalence.db"); + prepare_retained_schema(&path, 90); + let upgraded = SqliteWorkspaceStore::open(&path).unwrap(); + let fresh = SqliteWorkspaceStore::in_memory().unwrap(); + for object in [ + "typed_tickets", + "typed_ticket_targets", + "typed_ticket_case_metadata", + "typed_ticket_case_history", + "typed_ticket_legacy_workflow_snapshot", + "ticket_current_worker_assignments", + "ticket_assignment_work_releases", + "ticket_active_worker_assignments", + "worker_registry", + "workspace_runtime_bindings", + "workspace_runtime_binding_verification_archive", + "workspace_runtime_verification_archive", + "workspace_runtime_binding_audit", + "workspace_signing_identities", + "workspace_signing_identity_provisioning_operations", + "workspace_signing_identity_audit", + ] { + let expected = fresh.with_conn(|conn| Ok(signature(conn, object))).unwrap(); + let actual = upgraded + .with_conn(|conn| Ok(signature(conn, object))) + .unwrap(); + assert!(!expected.is_empty(), "missing fresh schema object {object}"); + assert_eq!(actual, expected, "schema-91 boundary {object}"); + } + for store in [&fresh, &upgraded] { + store.with_conn(|conn| { + assert_eq!(current_schema_version(conn)?, 91); + verify_workspace_resource_constraints(conn)?; + verify_runtime_binding_connection_schema(conn)?; + ticket::verify_sqlite_ticket_schema(conn).unwrap(); + conn.execute_batch("INSERT INTO accounts(account_id,kind,handle,display_name,created_at,updated_at) + VALUES('guard-owner','user','guard-owner','Owner','1','1'); + INSERT INTO workspaces(workspace_id,owner_account_id,display_name,state,created_at,updated_at) + VALUES('guard-workspace','guard-owner','Guard','active','1','1'); + INSERT INTO typed_tickets(workspace_id,ticket_id,slug,title,status,kind,priority,body,workflow_state,workflow_state_explicit) + VALUES('guard-workspace','guard-ticket','guard-ticket','Guard','open','task','P2','','open',1);")?; + assert!(conn.execute("UPDATE typed_tickets SET workflow_state='done' WHERE ticket_id='guard-ticket'", []).is_err()); + assert!(conn.execute("INSERT INTO typed_tickets(workspace_id,ticket_id,slug,title,status,kind,priority,body,workflow_state,workflow_state_explicit) + VALUES('guard-workspace','bad-ticket','bad-ticket','Bad','open','task','P2','','planning',1)", []).is_err()); + assert!(conn.execute("INSERT INTO workspace_runtime_bindings(workspace_id,runtime_id,display_name,base_url,public_key,public_key_fingerprint,binding_id,authentication_mode,created_at,updated_at) + VALUES('guard-workspace','bad-runtime','Bad','https://invalid.invalid','key','fingerprint','binding','invalid-mode','1','1')", []).is_err()); + Ok(()) + }).unwrap(); + } + } + + #[test] + fn schema_v91_case_constraint_verification_rejects_legacy_view_predicates() { let store = SqliteWorkspaceStore::in_memory().unwrap(); store.with_conn(|conn| { conn.execute_batch("DROP VIEW ticket_active_worker_assignments; @@ -19750,7 +19404,7 @@ CREATE UNIQUE INDEX ticket_current_worker_role_idx SELECT current.* FROM ticket_current_worker_assignments AS current JOIN typed_tickets AS ticket ON ticket.workspace_id=current.workspace_id AND ticket.ticket_id=current.ticket_id WHERE ticket.workflow_state NOT IN ('done','closed');")?; - assert!(verify_workspace_resource_constraints(conn).unwrap_err().to_string().contains("schema 90")); + assert!(verify_workspace_resource_constraints(conn).unwrap_err().to_string().contains("schema 91")); Ok(()) }).unwrap(); } diff --git a/crates/workspace-server/src/workspace_deletion.rs b/crates/workspace-server/src/workspace_deletion.rs index 922c3334c..0e584417d 100644 --- a/crates/workspace-server/src/workspace_deletion.rs +++ b/crates/workspace-server/src/workspace_deletion.rs @@ -102,7 +102,8 @@ const WORKSPACE_DELETION_PURGE_TABLES: &[&str] = &[ "workspace_resource_keys", "workspace_runtime_binding_audit", "workspace_runtime_bindings", - "workspace_runtime_verifications", + "workspace_runtime_binding_verification_archive", + "workspace_runtime_verification_archive", "workspace_signing_identities", "workspace_signing_identity_audit", "workspace_signing_identity_provisioning_operations", diff --git a/crates/workspace-server/src/workspace_signing_identity.rs b/crates/workspace-server/src/workspace_signing_identity.rs index bf58d407d..f724f2ca1 100644 --- a/crates/workspace-server/src/workspace_signing_identity.rs +++ b/crates/workspace-server/src/workspace_signing_identity.rs @@ -228,36 +228,7 @@ impl WorkspaceSigningIdentityService { } pub fn sign(&self, workspace_id: &str, payload: &[u8]) -> Result> { - let identity = self.get_validated(workspace_id)?; - if identity.state != "active" { - return Err(identity_error( - "workspace_signing_identity_not_provisioned", - "Workspace signing identity is not provisioned", - )); - } - let material = self - .materials - .load(&identity.private_material_ref)? - .ok_or_else(|| { - identity_error( - "workspace_signing_identity_material_missing", - "Workspace signing private material is missing", - ) - })?; - let fingerprint = identity.public_key_fingerprint.as_deref().ok_or_else(|| { - identity_error( - "workspace_signing_identity_material_mismatch", - "Workspace signing public key fingerprint is missing", - ) - })?; - let signing_key = material.signing_key(workspace_id, &identity.key_id)?; - let public_key = encode_public_key(signing_key.public_key().as_ref()); - if public_key_fingerprint(&public_key)? != fingerprint { - return Err(identity_error( - "workspace_signing_identity_material_mismatch", - "Workspace signing private material does not match public metadata", - )); - } + let (_, signing_key) = self.select_signing_key(workspace_id)?; Ok(signing_key.sign(payload).as_ref().to_vec()) } @@ -266,9 +237,20 @@ impl WorkspaceSigningIdentityService { workspace_id: &str, claims: &WorkspaceCapabilityClaims, ) -> Result { + let (identity, signing_key) = self.select_signing_key(workspace_id)?; + if claims.issuer_workspace_id != identity.workspace_id + || claims.issuer_key_id != identity.key_id + || Some(claims.issuer_public_key_fingerprint.as_str()) + != identity.public_key_fingerprint.as_deref() + { + return Err(identity_error( + "workspace_capability_identity_mismatch", + "Workspace capability claims do not match the current signing identity", + )); + } let input = workspace_capability_signing_input(claims).map_err(capability_error)?; - let signature = self.sign(workspace_id, input.bytes())?; - assemble_workspace_capability_token(input, &signature).map_err(capability_error) + let signature = signing_key.sign(input.bytes()); + assemble_workspace_capability_token(input, signature.as_ref()).map_err(capability_error) } pub fn delete_material(&self, workspace_id: &str) -> Result<()> { @@ -306,7 +288,40 @@ impl WorkspaceSigningIdentityService { }) } + fn select_signing_key( + &self, + workspace_id: &str, + ) -> Result<( + WorkspaceSigningIdentityRecord, + ring::signature::Ed25519KeyPair, + )> { + let identity = self + .store + .get_workspace_signing_identity(workspace_id)? + .ok_or_else(|| { + identity_error( + "workspace_signing_identity_metadata_missing", + "Workspace signing identity metadata is missing", + ) + })?; + if identity.state != "active" { + return Err(identity_error( + "workspace_signing_identity_not_provisioned", + "Workspace signing identity is not provisioned", + )); + } + let signing_key = self.validated_signing_key(&identity)?; + Ok((identity, signing_key)) + } + fn validate_active_material(&self, identity: &WorkspaceSigningIdentityRecord) -> Result<()> { + self.validated_signing_key(identity).map(|_| ()) + } + + fn validated_signing_key( + &self, + identity: &WorkspaceSigningIdentityRecord, + ) -> Result { let material = self .materials .load(&identity.private_material_ref)? @@ -316,8 +331,8 @@ impl WorkspaceSigningIdentityService { "Workspace signing private material is missing", ) })?; - let public_key = - material.validate_and_public_key(&identity.workspace_id, &identity.key_id)?; + let signing_key = material.signing_key(&identity.workspace_id, &identity.key_id)?; + let public_key = encode_public_key(signing_key.public_key().as_ref()); let fingerprint = public_key_fingerprint(&public_key)?; if identity.public_key.as_deref() != Some(public_key.as_str()) || identity.public_key_fingerprint.as_deref() != Some(fingerprint.as_str()) @@ -327,7 +342,7 @@ impl WorkspaceSigningIdentityService { "Workspace signing private material does not match public metadata", )); } - Ok(()) + Ok(signing_key) } } @@ -843,6 +858,14 @@ mod tests { loads: std::sync::atomic::AtomicUsize::new(0), }), ); + let selected_signature = swapping_service.sign("workspace-1", payload).unwrap(); + ring::signature::UnparsedPublicKey::new( + &ring::signature::ED25519, + worker_runtime::auth::decode_public_key(provisioned.public_key.as_deref().unwrap()) + .unwrap(), + ) + .verify(payload, &selected_signature) + .unwrap(); assert!(matches!( swapping_service.sign("workspace-1", payload), Err(Error::WorkspaceSigningIdentity { ref code, .. }) diff --git a/docs/design/compaction.md b/docs/design/compaction.md index bcd66f0c9..933bbc851 100644 --- a/docs/design/compaction.md +++ b/docs/design/compaction.md @@ -26,11 +26,96 @@ auto-read files, references, retained tail, and TaskStore context. Exceeding it warning and continues without discarding the generated summary or stripping auto-read/retained content. `0` disables this warning. Normal request-time safety checks still apply after activation. -## Large sessions +## Compactor inputs and continuity policy -Large-session compaction should not send an entire prefix transcript as the summary input. Prefer bounded overview/index inputs plus exploration, then keep the retained tail small and explicit. +The compactor receives the previous Host-produced summary in full as mandatory source +material, separately from the bounded overview of this Segment's ordinary prefix. It must +preserve ongoing user goals, directives, completion conditions, and unresolved work from +that summary and from the current Segment's user messages. Recent progress and the +TaskStore snapshot are supporting evidence, not replacements for the original request. +Important wording or ambiguous completion state should be checked through exploration +rather than reconstructed from recent activity. -This keeps compaction cost predictable and avoids turning a context-recovery mechanism into the largest prompt in the session. +The first model input includes: + +- `## Initial compaction summary (full text)`, when a prior summary exists, containing its + complete body without overview truncation; +- recently touched file references, when available; +- the full current Session TaskStore snapshot; +- `## Conversation overview/index`, a bounded index of the ordinary prefix selected for + summarization, with exploration tools for omitted details. + +The inherited summary is selected by `LoggedSessionHistoryOrigin::CompactionSummary`, +not by message role or pasted text. Only the Host-produced opening compaction summary +receives that origin. Auto-read contents, related-file context, and TaskStore snapshots +remain `DerivedSummary` and follow ordinary prefix/retention policy. A user or system +message containing `[Compacted context summary]` is not sufficient to qualify. + +Restore recognizes one explicitly adopted legacy construction: the first history item of +an `AnnotatedSegmentStart` with `compacted_from: Some(...)`, whose origin is +`DerivedSummary`, whose role is System, and whose text starts with the exact Host summary +marker. That item is interpreted as `CompactionSummary` in restored history. Restore does +not promote later derived items, user messages, unknown origins, or marker-bearing items +without compacted lineage, and does not rewrite the original durable record. + +## Retained tail + +The retained-token budget selects an ordinary history tail, at item granularity. Retained +entries remain in the replacement Segment with their item and metadata preserved, except +for documented binary-attachment projection. They are outside the prefix overview. + +The previous `CompactionSummary` is an explicit exception: its full body is supplied to the +compactor regardless of whether the cut lies before or after it, but the old summary is +excluded from both the ordinary overview and the retained tail. The newly generated +summary supersedes it. Keeping both would duplicate inherited context on every compaction. +Other derived context is not removed merely because its origin is `DerivedSummary`. + +## Limits and large sessions + +Large-session compaction does not send an entire prefix transcript as the initial input. +The ordinary prefix uses a bounded overview/index plus exploration. This keeps the +transcript portion predictable without allowing an overview limit to erase inherited goals. + +`overview_target_tokens` is a packing target, `overview_warning_tokens` emits a warning, +and `overview_deadline_tokens` switches the ordinary overview to a coarse index. These +limits and `compact.overview_tokens` accounting cover the overview only, not the separate +full inherited summary. Small targets, warnings, and coarse fallback must not shorten or +omit that summary. + +The inherited summary still occupies the compactor's context. It counts against +`worker_context_max_tokens`; moving it outside the overview does not exempt it from the +hard input-occupancy check. If mandatory input exceeds that cap, compaction fails explicitly +before sending the oversized request, leaving the source Segment active rather than +silently clipping source material or activating an incomplete replacement. The budget guard +is installed through the internal Worker's normal `WorkerInterceptor`, not an Engine +interceptor that Worker startup would replace. Before a provider usage measurement exists, +the estimate includes the full input, system prompt, and installed tool definitions. Later +provider input measurements include that request shape already. A budget warning is an +append-only history item and is also checked against the hard cap; if it cannot fit alongside +the mandatory input, the run fails explicitly. Provider context-window failures likewise do +not activate a replacement; estimates are not an exact model-token guarantee. + +`summary_target_tokens` guides the new `write_summary` output. A nonzero +`summary_max_tokens` is a hard output bound: an oversized result receives a rewrite request; +if the rewritten output still exceeds the maximum, compaction fails without activation. +This output bound is distinct from overview packing, the compactor input cap, and the +advisory `result_context_max_tokens` warning on the assembled replacement. + +## Assessment boundary + +Deterministic integration tests inspect the actual `LlmClient::Request` to prove full +inherited-summary transport, separation from overview packing, provenance-aware retention, +shared history-restoration behavior, hard-limit rejection without activation, and inheritance +through at least three chained compactions. Their `write_summary` outputs are predetermined +mock responses: they do **not** prove that a model understood or semantically retained user +goals, directives, or completion conditions. Shared restore-path coverage is not a complete +cold Runtime restart assessment. + +Live semantic assessment remains pending. It should use a real model on long, repeated +compactions and compare the resulting summaries against the original request and explicit +completion conditions, including facts near the end of a long inherited summary and facts +absent from recent progress. Report semantic omissions separately from successful transport; +a mock echo or a successful activation cannot establish semantic continuity. ## History integrity diff --git a/docs/development/compaction-summary-validation.md b/docs/development/compaction-summary-validation.md new file mode 100644 index 000000000..aa9880916 --- /dev/null +++ b/docs/development/compaction-summary-validation.md @@ -0,0 +1,69 @@ +# Compaction summary input validation (T-738) + +## Baseline and scope + +Implementation starts at `6a12b6cc346402b7a10208685038967171b09cb3`. +Compared with the Ticket's inspected `3263d501`, the compaction input, capture and +prompt contracts are unchanged; intervening Worker edits concern state-contract +naming, resource/manage-worker projections, and shared state. The defects reported +against `3263d501` therefore still apply to this checkout. + +The change fixes mandatory previous-summary transport and the continuity policy. +It does not add Journal integration (T-745), a separate directive ledger, exploration +of historical Segments, or a comprehensive internal-compactor audit log. See +[the current design](../design/compaction.md) for the input and retention contracts. + +## Acceptance evidence + +| Ticket condition | Deterministic evidence | +| --- | --- | +| 1: full initial input, long summary and recent activity | `full_initial_summary_survives_small_overview_budgets_and_both_retention_extremes` observes the actual first compactor `LlmClient::Request`; full body equality, one occurrence, and no clipped duplicate in overview. Summary exceeds 4,000 characters with directives/end sentinel after the long prefix, plus 80 recent messages. | +| 2: small target/warning/fallback, context accounting, hard failure | Same matrix uses target=1 and warning/deadline settings 0/1. `inherited_summary_counts_against_hard_input_limit_and_failure_does_not_activate` compares the same overview with/without a mandatory summary above the cap: no compactor request, no replacement Segment, unchanged active metadata/history on rejection. `budget_warning_is_durable_and_keeps_full_summary_in_first_model_request` verifies the warning survives in the next request. Interceptor tests cover request-shape estimation, measured occupancy without double counting, and a warning that cannot fit. | +| 3: absent summary, restore, retained boundary, other derived context | Matrix covers no retained tail and retaining all ordinary items. `pasted_markers_and_other_derived_context_remain_ordinary_overview_material` covers absent summary, auto-read, TaskStore, ordinary system, user paste and later derived markers. `restored_legacy_summary_requires_compacted_lineage_first_position_origin_role_and_marker` covers the exact adopted old Host seed boundary and its counterexamples. Invalid or duplicate typed Host summaries fail closed. | +| 4: privacy and lightweight overview | Existing tool-argument/tool-result/reasoning omission tests remain. `host_summary_remains_private_to_compaction_not_observation_or_memory_evidence` verifies overview, search, read, and evidence exclude Host summary, other derived/system and reasoning bodies. The persisted retained-history paging fixture includes the new Host summary origin and still excludes derived content. Public provenance maps to the existing excluded `DerivedSummary` class; no public observation/Memory capability is added. | +| 5: prompt and shortening policy | Resource policy test covers initial summary, segment user messages, prohibitions, completion conditions, changes/retractions, uncertainty, TaskStore distinction and retained tail. Shrink-policy and budget-warning tests cover the corresponding priorities. Output-hard-max integration test exercises the actual rewrite path and explicit failure if output remains oversized. | +| 6: at least three chained compacts | `three_chained_compactions_transport_each_complete_predecessor_without_retaining_old_summaries` starts with a long summary carrying a late original-goal sentinel; each of three compacts receives a full predecessor and a new user directive containing temporary recovery/SubWorker collection instructions. It checks each actual initial request, one current typed summary, predecessor lineage and persisted result. | +| 7: tests/check/format and current design | Commands below pass; design input, privacy, retention, limit and assessment descriptions are updated. | + +## Executed validation + +During implementation, the smallest filters were run first: + +- `cargo test -p worker --lib build_summary_prompt_tests::overview` +- `cargo test -p worker --lib compact_worker_` +- `cargo test -p worker --lib session_history::` +- `cargo test -p worker --lib session_capture::` +- `cargo test -p worker --lib build_summary_prompt_tests::compact_prompt_and_shrink` +- `cargo test -p worker --lib malformed_or_duplicate_host` +- `cargo test -p worker --test compact_events_test inherited_summary_counts_against_hard_input_limit_and_failure_does_not_activate` +- `cargo test -p worker --test compact_events_test` (25 tests) +- `cargo test -p session-store --lib retained_history_pages_twelve_turns` + +Final default-configuration validation: + +- `cargo check` at workspace root (normal compile closure, including TUI/Server) +- `cargo test -p worker` (library, integration targets, doctests) +- `cargo test -p session-store` (library, integration targets, doctests) +- `cargo fmt --all -- --check` +- `git diff --check HEAD` +- `env -u LD_LIBRARY_PATH deno run --allow-read --allow-run=git tools/check-state-contract-names.ts` (0 unapproved, 0 stale exceptions) + +No added contract depends on an optional Worker feature. Full-workspace tests, +`--all-targets`, live provider calls, E2E, Nix and Docker builds were not run. + +## Unverified semantic and operational behavior + +Predetermined mock `write_summary` results prove transport, input policy and +replacement behavior, **not** a real model's semantic retention or comprehension. +Live semantic evaluation remains pending: use a real model across at least three +compacts, score original scope/prohibitions/completion/exception-reporting duties +and explicit subsequent changes separately from transport success, and verify that +temporary recovery procedures do not displace the continuing request. Do not credit +an echo fixture as semantic evidence. + +The restore fixture exercises public `rewind_to` and the shared typed restoration +path over persisted annotated records. It is not a cold Runtime restart test. +Manual and automatic compaction use the same `compact_impl`; existing automatic +resume tests pass, but no separate live automatic-compaction semantic evaluation +was run. Initial context accounting is an estimate, not exact provider tokenization; +provider failure must still fail the run without activation. diff --git a/docs/development/dogfooding.md b/docs/development/dogfooding.md index baec4ea0a..4bdad7c14 100644 --- a/docs/development/dogfooding.md +++ b/docs/development/dogfooding.md @@ -8,11 +8,11 @@ Never use the live dogfood Server or Runtime as the first startup test for a new 1. Build the production entrypoints: `cargo build -p worker-runtime --bin yoi-runtime -p yoi-workspace-server --bin yoi-server`. 2. Run the focused and dependent tests for the changed contracts, followed by workspace-root `cargo check`, `cargo fmt --all -- --check`, and `git diff --check HEAD`. -3. Exercise the provisioning and operational checks in [Workspace ↔ Runtime authentication](server-runtime-auth.md) against isolated Server DB, Runtime data, and ports. The Workspace owner must create the binding, the Runtime operator must install the Workspace issuer bundle, and the challenge proof must become verified. +3. Exercise the provisioning and operational checks in [Workspace ↔ Runtime authentication](server-runtime-auth.md) against isolated Server DB, Runtime data, and ports. The Workspace owner must configure the Runtime endpoint and pinned Runtime key, the Runtime operator must install the Workspace issuer bundle, and actual signed requests must authenticate. A connection-test result describes only that attempt; no stored verification state is a readiness prerequisite. 4. Have an external supervisor or operator restart Server and Runtime from the same validated source commit/build artifacts. A Worker hosted by the target Runtime must never terminate its own Runtime. 5. Verify post-restart readiness through the Workspace Runtime projection, ping, Worker list/create, protocol subscription, and a Runtime-to-Server source-proof operation before treating the environment as healthy. -The former isolated startup shell harness depended on removed Server-global trust commands and is intentionally not a fallback smoke path. New automated startup coverage must provision the same Workspace-scoped binding and challenge authority used by production rather than recreating global trust or seeding private authority directly. +The former isolated startup shell harness depended on removed Server-global trust commands and is intentionally not a fallback smoke path. New automated startup coverage must configure the same Workspace-scoped connection and explicit Runtime-side Workspace trust used by production, then exercise per-request authentication rather than recreating global trust or seeding a past verification result. ## What to record @@ -25,7 +25,7 @@ A report is useful when it explains: - what design boundary was missing - what evidence was observed -For a Remote Runtime rollout, also record the source commit, binary artifact digest, Server schema version, Runtime `binding_id`, Workspace `key_id` and `workspace_trust_id`, and typed HTTP/WebSocket outcomes. A successful document response does not outweigh visible UI, console, or API errors. +For a Remote Runtime rollout, also record the source commit, binary artifact digest, Server schema version, Runtime `binding_id` and pinned key fingerprint, the current Workspace `key_id` and public fingerprint, the Runtime-side trust registration/revocation, and typed HTTP/WebSocket outcomes. Do not copy a Runtime trust ID into the Server connection configuration or treat a successful connection test as persisted authentication. A successful document response does not outweigh visible UI, console, or API errors. ## Runtime command caveat diff --git a/docs/development/server-runtime-auth.md b/docs/development/server-runtime-auth.md index 77719d3c2..6058836eb 100644 --- a/docs/development/server-runtime-auth.md +++ b/docs/development/server-runtime-auth.md @@ -1,28 +1,25 @@ # Workspace ↔ Runtime 認証 Yoi の Remote Runtime 認証は Workspace ごとの署名 identity を authority とする。 +認証の成否は実際の HTTP / WebSocket request ごとに決まり、保存済みの「verified」状態や過去の接続テスト結果を前提にしない。 Server-global な署名鍵や Runtime 側の trusted-Server catalog は使わない。 ## Authority -- Server DB は Workspace ごとの signing identity と Runtime binding を保持する。 -- Runtime は `trust-workspace` で受理した `WorkspacePublicIdentityBundle` を保持する。 +- Server DB は Workspace ごとの signing identity と Runtime 接続設定(binding)を保持する。 +- binding は endpoint、固定した Runtime public key/fingerprint、`binding_id`、authentication mode、管理者による失効を保持する。Workspace key や Runtime 側の trust ID のコピー、認証成功状態は保持しない。 +- Runtime は管理者が `trust-workspace` で受理した `WorkspacePublicIdentityBundle` を保持する。 - bundle は `workspace_id`、Workspace `key_id`、Workspace public key/fingerprint、Backend URL を固定する。 -- Server → Runtime の各 HTTP / WebSocket request は、対象 Workspace の signing identity で短命な capability token を発行する。 -- Runtime は request method、`path_and_query`、body digest、permission、Workspace、Runtime、key ID、現在の trust ID、expiry、JTI を検証する。 -- Runtime → Server の source proof は Runtime identity で署名し、対象 Workspace と bundle の Backend URL を audience に固定する。 +- Server → Runtime の各 HTTP / WebSocket request は、その時点の Workspace signing identity で短命な capability token を発行する。署名時には claims の Workspace・key ID・fingerprint が選択した identity と一致することを検証する。秘密鍵materialがない場合は fallback せず拒否する。 +- Runtime は現在の明示的な Workspace trust、署名、request method、`path_and_query`、body digest、permission、Workspace、Runtime、必要に応じ Worker、key ID/fingerprint、expiry、JTI を検証する。過去の handshake や verification record は不要である。 +- Runtime → Server の source proof は Runtime identity で署名し、対象 Workspace と bundle の Backend URL を audience に固定する。この proof の契約は変わらない。 - Server は現在の Workspace Runtime binding、Runtime public key、Backend public URL、request target、body digest、permission、expiry、replay state を検証する。 旧 Server identity/trust 管理 command と旧 Runtime-side Server trust command、旧 Runtime auth key flags は廃止済みである。これらに相当する Server-global trust を fallback として使ってはならない。 ## Provisioning -The enrollment input includes the exact Runtime-issued `workspace_trust_id` from -`trust-workspace add/show`. Export the current Workspace public bundle first, -accept it on the Runtime, then register the Runtime public bundle, endpoint and -trust ID. Server verification binds that enrollment ID; it must never invent an -ID from the key fingerprint or assume an initial counter value. Re-enrollment -requires registering the newly issued trust ID, even when the key is unchanged. +Runtime 側の Workspace trust 登録と Server 側の Runtime 接続設定は独立した管理操作である。Runtime が発行する `trust_id` を Server にコピーする必要はない。 1. Runtime identity を初期化する。 @@ -31,17 +28,16 @@ requires registering the newly issued trust ID, even when the key is unchanged. yoi-runtime identity show ``` -2. The Workspace owner exports the current Workspace issuer public bundle from Workspace identity settings. -3. The operator accepts that bundle on the Runtime and reads the issued trust ID. +2. Workspace owner が Workspace identity settings から現在の Workspace issuer public bundle を export する。 +3. Runtime 管理者が bundle を確認し、Runtime に明示的に登録する。 ```sh yoi-runtime trust-workspace add --bundle yoi-runtime trust-workspace show --workspace-id ``` -4. The owner registers the Runtime public bundle, endpoint and exact `workspace_trust_id` in Settings > Runtimes. -5. The Server sends a signed challenge bound to that trust ID, the Workspace key fingerprint and the binding ID, then verifies the Runtime proof and acknowledgement. -6. Normal Workspace-signed requests become available only after the verified binding is committed. Revoking and re-enrolling the same key still requires the newly issued Runtime trust ID. +4. Workspace owner が Settings > Runtimes に Runtime public bundle と endpoint を登録する。接続設定には Runtime key を固定するが、Workspace key や Runtime の trust ID は登録しない。 +5. 接続テストまたは通常操作を実行する。実際の Workspace-signed request が Runtime の現在の trust と照合される。接続テスト結果はその試行の診断であり、永続的な認証状態を作らず、以後の request の成功を保証しない。 同じ Runtime identity は異なる Workspace から独立して信頼できる。trust record、replay protection、binding、失効はすべて Workspace scope で評価する。 @@ -55,29 +51,41 @@ requires registering the newly issued trust ID, even when the key is unchanged. - 最大 8 MiB - 最大 4,096 records - exact Workspace / Runtime identity -- `key_id` と public key fingerprint、opaque `trust_id` +- `key_id` と public key fingerprint、管理 record の opaque `trust_id` - normalized Backend URL -- 同じ `key_id` に異なる鍵materialを割り当てないこと。実際の置換・失効は新しい `trust_id` を発行する +- 同じ `key_id` に異なる鍵materialを割り当てないこと。実際の置換・失効は新しい `trust_id` を発行するが、この ID は request token や Server binding に含めない - list は `offset` / `limit` 必須で、1 page 最大 100 records ## Local token `--local-token` は明示的な local Runtime 呼び出し専用であり、Remote Workspace binding の代替ではない。Workspace issuer auth が有効な Remote Runtime request は Workspace capability token を使う。 -## Rotation と失効 +## Key 変更と失効 -Workspace signing key または Runtime key の変更は、現在 binding を置き換える明示的な provisioning 操作として行う。古い key/trust ID、古い Runtime key、revoked binding、失効済み token、replayed JTI は即時拒否する。 +Workspace signing identity は各 request の発行時に現在の authority から選択する。Workspace public key が変更された場合、Runtime 管理者が現在の public bundle を明示的に `trust-workspace replace` で受理する必要がある。Workspace key のコピーを更新するためだけに Server の Runtime 接続設定を置き換える操作は不要である。これは Workspace key rotation の新しい production API を定義するものではない。 -Server の Runtime cache は現在の persisted binding 全体と照合する。endpoint、Runtime public key/fingerprint、`binding_id`、Workspace `key_id`、`workspace_trust_id` の変更を検知した場合、stale client を利用しない。 +Runtime 管理者は `trust-workspace revoke` で Workspace trust を失効できる。Runtime は現在の trust と一致しない key、失効済み trust、expired token、replayed JTI を拒否する。同じ key の再登録も管理者の明示的な操作で行い、request の自己申告による trust 登録は行わない。 + +Runtime key や endpoint の変更は Server の接続設定を明示的に置き換える。Server の Runtime cache と authorizer は現在の接続設定(endpoint、Runtime public key/fingerprint、`binding_id`、authentication mode、管理者による失効)と照合し、replaced/revoked binding を使う stale client を拒否する。接続設定の一致は送信先の選択であって、認証成功の証拠ではない。 + +接続先が到達不能・認証失敗でも、owner は観測した `binding_id` を指定して接続設定を修復できる。取得できた active Worker は置換を遮断するが、Worker 一覧の取得失敗を「idle の証明」とは扱わず、認証成功を修復の前提にもしない。接続設定の置換は Worker の停止・削除ではなく、Runtime 削除の資源確認とは別の操作である。 + +## 保存形式の移行 + +Server schema90 は既存 binding の endpoint、Runtime key、binding ID、明示的失効を保持し、古い認証状態と検証記録を履歴専用 archive に移す。履歴は要求の認可には参照しない。既存 Workspace identity や接続設定の作り直しは不要である。 + +Runtime の旧形式の Workspace trust は、検証済みの公開鍵と管理者の Active/Revoked 設定を保持して読み込む。形式の移行だけでは失効させず、すでに保存されている Revoked 設定を自動で解除することもしない。旧 verification file は認可に使用しない。消費済み token の replay 記録は引き継ぎ、同じ鍵の再登録でリセットしない。 + +API と capability claims の旧 handshake 契約は維持しないため、Backend・Runtime・Web は対応する版へ更新する。接続テストは更新後の実認証を確認するものであり、登録を有効化する手順ではない。 ## 運用確認 -Remote Runtime を有効化した後は次を確認する。 +Remote Runtime を設定した後は次を確認する。 -1. `yoi-runtime trust-workspace show --workspace-id ` が期待する bundle を表示する。 -2. Workspace Settings の Runtime binding が `verified` で、現在の `binding_id`・`workspace_key_id`・`workspace_trust_id` と verification evidence を表示する。 -3. Runtime ping、Worker list/create、`worker.protocol` subscription が Workspace-signed token で成功する。 -4. wrong Workspace、wrong Runtime、wrong target/body、expired token、revoked/replaced binding、replayed JTI が拒否される。 +1. `yoi-runtime trust-workspace show --workspace-id ` が期待する active bundle を表示する。 +2. Workspace Settings の Runtime 接続設定が期待する endpoint と固定 Runtime key を示し、失効していない。 +3. 接続テストの今回の結果を確認し、Runtime ping、Worker list/create、`worker.protocol` subscription が実際の Workspace-signed request で成功することを確認する。保存済み「verified」表示や verification evidence は確認項目ではない。 +4. wrong Workspace、wrong Runtime、wrong target/body、expired token、revoked Runtime trust、replaced/revoked binding、replayed JTI が拒否される。 5. Runtime → Server source proof が configured Backend public URL audience と一致し、spoofed headers だけでは認証されない。 Server / Runtime の再起動は live reload ではない authority 変更を反映するときだけ、通常の運用権限と migration gate に従って行う。実行中プロセスを開発 Worker が無断で停止してはならない。 diff --git a/docs/development/ticket-case-state.md b/docs/development/ticket-case-state.md index 85ee138bb..543538c54 100644 --- a/docs/development/ticket-case-state.md +++ b/docs/development/ticket-case-state.md @@ -31,7 +31,7 @@ closeは案件の実作業拘束を終える一方、T-715の終端担当責任 ## 旧保存データの移行 -独立T-739 sourceではTicket component schema7→9、Workspace schema89→90を使用する。component6からの既存移行も維持する。 +T-739 sourceではTicket component schema7→9を使用する。Workspaceは現developのauth/runtime connection移行89→90をそのまま保持し、案件移行90→91を追加する。旧89からはauth→案件の順に進み、現90からは案件移行だけを行う。component6からの既存移行も維持する。 | 旧workflow state | 現行案件状態 | 終了理由 | | --- | --- | --- | diff --git a/docs/report/2026-10-10-repository-key-migration-timestamp-mismatch.md b/docs/report/2026-10-10-repository-key-migration-timestamp-mismatch.md new file mode 100644 index 000000000..02029d802 --- /dev/null +++ b/docs/report/2026-10-10-repository-key-migration-timestamp-mismatch.md @@ -0,0 +1,70 @@ +# schema86→87のSSH移行: 異なる時刻形式の文字列比較でfingerprintがNULLになる + +調査日: 2026-10-10 +報告された起動エラー: + +`yoi-server: store error: workspace schema migration failed: sqlite error: NOT NULL constraint failed: workdir_create_credential_candidates.credential_fingerprint` + +## 原因 + +`crates/workspace-server/src/frozen_repository_key_migration.rs:262–266`のcredential candidate移行は、旧credential IDと保存counterの組から`repository_key_cutover`へLEFT JOINし、`m.fingerprint`を新しいNOT NULL列に挿入する。 + +JOIN条件の`m.created_at <= o.updated_at`が、RFC3339の鍵作成時刻とepoch-millis文字列のWorkdir更新時刻を、そのままTEXTとして比較している。日時としての順序を判定していないため、有効な古い鍵のJOINが外れ、fingerprintがNULLになる。 + +- 鍵作成時刻: `repository_access.rs:2256`の`Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true)`。 +- Workdir作成・更新時刻: `server.rs:30936–30940`の`now_registry_timestamp()`はepochミリ秒の文字列。reserveでもこれを使用する(`server.rs:24272–24308`)。 + +同じ異形式比較はoperation本体のcredential/host trust移行にもある(migration L260–261)。candidateだけ直してもoperation snapshotのfingerprintが欠落する問題が残る。 + +## 実データによる確認 + +ユーザーから共有済みの`yoi.local/server/server.db`をNodeの`DatabaseSync({readOnly: true})`と`PRAGMA query_only=ON`で照合した。秘密鍵、暗号化envelope、master keyは取得していない。migrationやDB更新も実行していない。 + +- DBはschema86のまま。 +- 新candidateへ移される5件すべてがfailed Workdir createのcredential候補で、旧retentionが各1件あるため成功済み・unretainedのarchive経路には入らない。 +- 5件とも参照先の鍵は存在する。対応するmutation receiptも各1件あり、証拠欠落ではない。 +- 5件ともSQLiteのTEXT比較はfalse、実際の時刻比較はtrue。 + +代表例: + +| 項目 | 値 | +| --- | --- | +| operation | `call_mgmyCNofdQMZ8uyI5tg0mBhv` | +| credential | `workspace-default`、旧保存counter 1 | +| 鍵created_at | `2026-09-16T18:44:56.736Z` | +| Workdir updated_at | `1790173821030` | +| Workdir updated_atを日時に変換 | `2026-09-23T14:30:21.030Z` | +| SQLiteの文字列比較 | false | +| 日時としての比較 | true | + +他の4件では鍵作成が`2026-09-10T13:09:52.113Z`、Workdir更新は9/23または9/29で、同じ問題になる。 + +## 修正方針(この調査では未実装) + +- frozen migration内で旧データの時刻形式を明示的に読み、同じ時刻型/epoch-millisへ正規化して比較する。文字列比較やSQLの安易なCASTに頼らない。 +- `created_at <= updated_at`の安全確認そのものは削除しない。旧IDと保存counterが削除後に再利用された鍵を、過去のcandidateに誤って結び付けることを防ぐ条件である。 +- candidateだけでなくoperation本体のcredential/host trust等、同じ境界の比較を確認する。 +- 正規化不能・不正な時刻や真に必要な証拠が欠落した場合は、NOT NULLエラーになる前に文脈付きで拒否し、transactionをrollbackする。 + +既存の`frozen_repository_key_tests.rs:35–42`の共通fixtureは、鍵とWorkdirの時刻をすべて`created`に揃えており、実データの異形式を再現していない。RFC3339鍵時刻+epoch-millis Workdir時刻での移行成功と、実時刻が操作より後の再作成鍵を拒否するケースが必要。テスト追加時は`docs/development/rust-testing-strategy.md`に従う。 + +## 影響・実施範囲 + +schema86→87はtransaction内で実行され、version87の記録とcommitは最後に行う(migration L29、L349–350)。今回のDBでもschema86と旧テーブルを確認した。ただし、それより前のschema更新まで含めた全起動処理が一括で元に戻るという意味ではない。 + +NOT NULLを外す、適当なfingerprintを入れる、candidate/retentionを消して通す、といったDB修復は行うべきではない。正しい既存データをmigrationが誤判定しているので、移行コード側を修正する。 + +調査時点で追加したのは本報告のみ。以下は、その後のユーザー指示による修正記録。 + +## 修正と検証(2026-10-10) + +- RFC3339/epoch-millisをRustで読み、temporary tableのUTC秒・ナノ秒へ正規化して比較するよう修正。credential/host trustのoperation snapshot、candidate、旧receiptの時刻条件に適用した。 +- タイムゾーン・sub-millisecondの順序を維持し、元の履歴時刻やreceipt照合用の文字列、暗号化AADは書き換えない。永続schemaの変更・version bumpは不要。 +- 不正な時刻、欠けた鍵、操作より新しい再利用鍵は文脈付きで拒否しrollback。NOT NULLを緩めたり候補を削除したりしない。 +- 独立コード確認で見つかった、親operationのない旧candidateがINNER JOINから落ちて消える経路も、コピー前の拒否とrollbackテストで保護した。追修正に追加のblocking findingなし。 +- 偽時刻`created`や日付だけのfixtureは実際のRFC3339へ修正。新規回帰は混在形式・同時刻・offset・ナノ秒精度、各SSH参照での新しい鍵拒否、不正時刻、孤立候補の保持を検証する。 +- 最小の混在形式テスト、migrationモジュール、公開migration dry-run/applyテスト、孤立候補テストを順に実行して成功。最終の`cargo test -p yoi-workspace-server`はlibrary **815 passed / 1 ignored**、binary **11 passed**、doctest失敗なし。 +- root `cargo check`、`cargo fmt --all -- --check`、変更したincludeファイルの明示的`rustfmt --check`、`git diff --check HEAD`も成功。 +- 稼働DBでのmigration、ビルド済みバイナリの配備、サービスの停止・再起動は行っていない。更新はユーザー操作のまま。 + +作業中、テスト担当SubWorkerはcwd基準の読取で、scopeに付与した別ディレクトリのテスト規則を参照できなかった(絶対pathもinvalid logical filesystem pathで拒否)。編集なしを確認して停止し、親でテストを実装した。読取専用レビュー担当はroot cwdで規則・ソースを読めた。cwdとscopeにまたがる論理pathの案内・検証がツール上の改善点。 diff --git a/docs/report/2026-10-10-workdir-removal-mount-and-timeout.md b/docs/report/2026-10-10-workdir-removal-mount-and-timeout.md new file mode 100644 index 000000000..10139a6be --- /dev/null +++ b/docs/report/2026-10-10-workdir-removal-mount-and-timeout.md @@ -0,0 +1,88 @@ +# Workdir 削除のマウント残留と結果不明の診断 + +## 調査範囲 + +ユーザーの「workdirを消せない原因を調査して」という依頼に対し、保存済み Server DB を +`mode=ro` / `PRAGMA query_only=ON` で照合し、対象ディレクトリ・マウント・現行実装を +観察した。削除再実行、アンマウント、DB 更新、サービス/プロセス操作、実装修正は行っていない。 +調査中にも operation の記録が更新されたため、以下は最後に読んだ状態であり、過去の全試行に +同じ原因があったと断定するものではない。 + +Workspace: `0197a949-4b6b-7f2a-9d9a-1f87e3a4c5b6`、Runtime: `arcadia`。 +表示に含まれる次の2つの Workdir ID は別の対象である。 + +## T-721: 残留した JuiceFS マウントによる拒否(確認済み) + +- Workdir: `001a11ab3e830000007` / `T-721 JuiceFS Drive foundation` +- operation: `wdr_1c0840acc66f27b7e048bdfff8f80b76` +- 最後に読んだ attempt: `01a12682-3b33-7e43-b4c6-4e57eb469912` +- 更新時刻: `2026-10-10T15:50:31.515343065+00:00` +- 結果: `failed` / `mount_present` / `retryable=1` + +`findmnt` で、次の2つが `JuiceFS:yoi-t721` / `fuse.juicefs` のマウントとして残っていることを +確認した(パスは `yoi.local` 以下の論理パス)。 + +```text +workdirs/001a11ab3e830000007/checkout/.yoi/dev/juicefs-t721/mount +workdirs/001a11ab3e830000007/checkout/.yoi/dev/juicefs-t721/mount2 +``` + +両エントリの属性参照は `Transport endpoint is not connected` で失敗した。 +`ps -C juicefs` に現在のプロセスはなかった。マウント解除されていない FUSE の残留状態と +整合するが、プロセスが終了した経緯までは今回確認していない。 + +Registry の cleanliness は `clean` だが、これはマウントを安全に削除できる証明ではない。 +実ディレクトリの `materialization.json` は `cleanup_pending`。 +現行 `crates/worker-runtime/src/working_directory/cleanup.rs` は、削除前にマウント境界を +検査し、外部/未知のマウントを自動解除しない。Server は +`working_directory_cleanup_mount_present` を `mount_present` として扱う。 +これは安全性を維持する拒否であり、cleanliness 判定の不具合とは異なる。 + +解消には、所有者が保存先と利用状況を確認して両マウントを安全に解除することが必要。 +その後、マウントがないことを確認して通常の Workdir 削除を再実行する。 +マウント先を含む `rm -rf`、強制的なメタデータ改変、未知のプロセス停止は解消手段にしない。 +マウント解除後も ignored な実験データ等の保護により別の拒否が起こる可能性があり、今回の +確認は次回の削除成功を保証しない。 + +## 別 ID: HTTP タイムアウトと物理削除の時間差が疑われる(未確定) + +- Workdir: `001a111ca9d33000002` +- operation: `wdr_fccf7c6e9ee6cbb931ea2f9b82198650` +- attempt: `01a12682-411e-7720-b829-f9c9c0148886` +- created: `2026-10-10T15:50:32.984845992+00:00` +- updated: `2026-10-10T15:50:43.018819445+00:00` +- 結果: `failed` / `provider_cleanup_failed` / `retryable=1` + +要求から失敗まで約10.034秒。Registry には `corrupted` / cleanliness `unknown` として +残っている一方、調査時点で `yoi.local/workdirs/001a111ca9d33000002` は存在しない。 +`.cleanup-authority` にも残存ファイルはなかった。 + +現行実装では `RemoteRuntimeConfig::new` の timeout は10秒で、DELETE はその共通 +blocking HTTP client を使う。Runtime の HTTP handler は `spawn_blocking` で同期的な +検査/削除の完了を待つ。クライアントのタイムアウトは、この blocking 削除処理の取り消しや +物理削除の失敗を意味しない。 + +このため「Server が10秒で応答待ちを打ち切った後、Runtime の物理削除が完了した」という +説明と整合する。ただし、元の `remote_runtime_timeout` 等の診断を今回の観察面で取得できず、 +別の操作でディレクトリがなくなった可能性も排除できない。物理パスがないだけで Backend の +削除完了を捏造してはいけない。通常の再試行で Runtime の厳密な not-found 応答を確認できれば、 +現行 Server は registry 削除を完了する経路を持つ。今回、その再試行は行っていない。 + +## 診断面の障壁と改善案 + +`crates/workspace-server/src/server.rs` の `workdir_cleanup_failure_category` は、既知の +cleanup code を分類し、それ以外を `provider_cleanup_failed` に落とす。 +`crates/workspace-server/src/hosts.rs` は transport timeout を `remote_runtime_timeout` +に区別するが、上記分類では失敗結果不明との区別が失われる。 + +また removal operation は bounded category のみを永続化し、元の diagnostic code を残さない。 +これは秘密情報を含み得る message を保存しない方針とは両立するが、現行の失敗ログも +Workdir/operation/runtime/category のみで、ユーザー案内の「相関 Runtime diagnostics を調べる」 +を保存済み情報だけでは完遂できなかった。`runtime.json` の diagnostics にも対象 ID / operation +を含む記録は見つからなかった。 + +改善案は、秘密を含む raw message を公開・永続化せず、transport timeout を「削除結果不明」と +して区別し、許可された診断コードと operation/attempt/Workdir の相関を残すこと。 +さらに、完了まで時間のかかる guarded cleanup を通常の10秒 control request と同じ同期要求で +扱う契約を見直すこと。単にタイムアウトを延長するだけでは結果不明の状態はなくならない。 +今回は調査報告のみで、これらの実装変更はしていない。 diff --git a/openapi/server-api.json b/openapi/server-api.json index 3d32fa641..418347928 100644 --- a/openapi/server-api.json +++ b/openapi/server-api.json @@ -1437,15 +1437,10 @@ }, "public_bundle": { "$ref": "#/components/schemas/RuntimePublicIdentityBundle" - }, - "workspace_trust_id": { - "description": "Exact Runtime-issued enrollment identity from `trust-workspace add/show`.\nA key fingerprint alone cannot distinguish revocation and re-enrollment.", - "type": "string" } }, "required": [ "public_bundle", - "workspace_trust_id", "endpoint" ], "type": "object" @@ -7747,15 +7742,6 @@ ], "type": "object" }, - "RuntimeConnectionDisplayState": { - "enum": [ - "configured", - "verified", - "unavailable", - "revoked" - ], - "type": "string" - }, "RuntimeConnectionTestFailureKind": { "enum": [ "authentication", @@ -7772,6 +7758,7 @@ }, "RuntimeConnectionTestResponse": { "additionalProperties": false, + "description": "Observation of an actual signed ping, not a grant or cached approval.", "properties": { "actual_protocol_version": { "format": "uint32", @@ -7788,9 +7775,6 @@ "checked_at": { "type": "string" }, - "connection_state": { - "$ref": "#/components/schemas/RuntimeConnectionDisplayState" - }, "diagnostics": { "default": [], "items": { @@ -7820,16 +7804,6 @@ "status": { "$ref": "#/components/schemas/RuntimeConnectionTestStatus" }, - "verification": { - "anyOf": [ - { - "$ref": "#/components/schemas/RuntimeVerificationEvidenceSummary" - }, - { - "type": "null" - } - ] - }, "workspace_id": { "type": "string" } @@ -7838,7 +7812,6 @@ "workspace_id", "runtime_id", "binding_id", - "connection_state", "checked_at", "status", "expected_protocol_version" @@ -8292,57 +8265,6 @@ ], "type": "string" }, - "RuntimeVerificationEvidenceSummary": { - "additionalProperties": false, - "properties": { - "binding_id": { - "type": "string" - }, - "last_checked_at": { - "type": "string" - }, - "last_outcome": { - "$ref": "#/components/schemas/RuntimeVerificationOutcome" - }, - "runtime_public_key_fingerprint": { - "type": "string" - }, - "verified_at": { - "type": [ - "string", - "null" - ] - }, - "workspace_key_id": { - "type": "string" - }, - "workspace_public_key_fingerprint": { - "type": "string" - }, - "workspace_trust_id": { - "type": "string" - } - }, - "required": [ - "last_checked_at", - "last_outcome", - "binding_id", - "workspace_key_id", - "workspace_public_key_fingerprint", - "workspace_trust_id", - "runtime_public_key_fingerprint" - ], - "type": "object" - }, - "RuntimeVerificationOutcome": { - "enum": [ - "verified", - "challenge_issued", - "verification_failed", - "connectivity_failed" - ], - "type": "string" - }, "RuntimeWorkerCompletionsRequest": { "additionalProperties": false, "properties": { @@ -15528,43 +15450,14 @@ ], "type": "object" }, - "WorkspaceRuntimeBindingState": { - "enum": [ - "configured", - "verified", - "revoked" - ], - "type": "string" - }, "WorkspaceRuntimeBindingSummary": { "additionalProperties": false, + "description": "Binding identity and explicit revocation only. Each signed request authenticates\nusing the current Workspace signing identity, not a stored approval.", "properties": { "binding_id": { "type": "string" }, - "connection_state": { - "$ref": "#/components/schemas/RuntimeConnectionDisplayState" - }, - "state": { - "$ref": "#/components/schemas/WorkspaceRuntimeBindingState" - }, - "verification": { - "anyOf": [ - { - "$ref": "#/components/schemas/RuntimeVerificationEvidenceSummary" - }, - { - "type": "null" - } - ] - }, - "workspace_key_id": { - "type": [ - "string", - "null" - ] - }, - "workspace_trust_id": { + "revoked_at": { "type": [ "string", "null" @@ -15572,8 +15465,6 @@ } }, "required": [ - "state", - "connection_state", "binding_id" ], "type": "object" @@ -16328,7 +16219,7 @@ "info": { "title": "Yoi Server API", "version": "0.1.0", - "x-yoi-source-digest": "sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e" + "x-yoi-source-digest": "sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c" }, "openapi": "3.1.0", "paths": { diff --git a/resources/prompts/internal/compact_system.md b/resources/prompts/internal/compact_system.md index f0a4f53d6..08e43b0c6 100644 --- a/resources/prompts/internal/compact_system.md +++ b/resources/prompts/internal/compact_system.md @@ -1,10 +1,20 @@ You are a context compaction assistant. Your job is to hand the next session a structured summary plus pointers to the files it actually needs — not a narrative transcript of the conversation. -The conversation input is a bounded overview/index, not the full transcript. Treat tool result bodies and reasoning as intentionally omitted unless a tool exposes more detail. If you receive a compact worker budget warning, stop broad exploration immediately, read only if absolutely necessary, and call `write_summary`. +The conversation input contains a bounded overview/index, not the full transcript, and, when present, a separately labelled initial compaction summary in full. The initial summary is mandatory source material, not an overview preview or something to recover through exploration. Tool result bodies and reasoning are intentionally omitted; raw system prompts and reasoning are not exposed by session exploration. If you receive a compact worker budget warning, stop broad exploration immediately, read only if absolutely necessary to confirm load-bearing user wording, and call `write_summary` while preserving ongoing goals and directives. + +## Continuity policy + +- Treat ongoing goals and directives in the initial summary and this segment's user messages as the primary inheritance targets. Explore user entries to confirm important original wording when needed; do not reconstruct the request only from recent progress. +- Preserve scope, prohibitions, completion conditions, exceptions and duties to explain exceptional outcomes wherever possible. Prefer original wording for conditions whose loss would change the request's meaning. +- Interruptions, recovery, stash and SubWorker collection procedures do not by themselves retract or satisfy the original request. TaskStore, implementation and test progress do not replace user goals or completion conditions. +- Apply explicit changes or retractions in later user messages. The initial summary is not more authoritative than the original user text. Preserve uncertainty when conflict, completion or continued applicability cannot be established. +- When shrinking or near the budget limit, compress duplicate explanations and stale work details first. Do not replace ongoing goals and directives with recent procedures. Do not copy the whole transcript or accumulate expired directives unconditionally. +- The retained tail stays verbatim in the next session and is outside the summarized prefix. Preserve directives from the compacted prefix in the summary so they do not disappear from both summary and tail; integrate them with the continuing goals from the initial summary. + ## Workflow -1. Read the provided overview/index and current TaskStore snapshot. +1. Read the full initial compaction summary when supplied, the overview/index and current TaskStore snapshot. Keep user intent distinct from task/progress metadata. 2. If the provided index is not enough, call `ShowOverview` on the pinned capture. Use `SearchEntries` to locate relevant entries, then `ReadEntry` with the returned stable entry reference to inspect only what is needed. 3. Use `Read` to inspect referenced Workdir files before deciding what the next session needs. Prefer skimming over blind inclusion. 4. For UTF-8 text files whose current contents are load-bearing for the active work, call `mark_read_required` to inject them into the next session. These count against the auto-read token budget — spend it deliberately. Images and binary files cannot be injected; record them with `add_reference` so the next session can fetch them with `ViewImage` or the appropriate read tool. @@ -39,4 +49,4 @@ Produce the summary in this exact format: ## Constraints - Keep code snippets and raw tool output OUT of the summary — that is what auto-read and references are for. -- Follow the summary target stated in the run input; if asked to shrink, call `write_summary` again with a shorter version. +- Follow the summary target stated in the run input; if asked to shrink, call `write_summary` again with a shorter version using the continuity policy above, not by dropping ongoing user intent. diff --git a/tools/state-contract-name-exceptions.json b/tools/state-contract-name-exceptions.json index 5853c109c..a17a8dee9 100644 --- a/tools/state-contract-name-exceptions.json +++ b/tools/state-contract-name-exceptions.json @@ -673,39 +673,15 @@ { "path": "crates/worker-runtime/src/workspace_issuer.rs", "kind": "saved_data", - "reason": "LegacyWorkspaceIssuerTrustRecord/LegacyVerificationDocumentの保存済みenrollment・verification keyとfixture。旧trustをrevokedへ移行し、旧verification bytesをarchiveするがlive recordsを空にするconsumerがある。削除すると既存記録を読めないか、旧capabilityを再承認なしに復活させない移行検証を失う。", + "reason": "LegacyWorkspaceIssuerTrustRecordの保存済みenrollment keyとfixture。旧形式の管理者によるWorkspace信頼登録を読み取る境界だけに綴りを保持する。通常要求の署名検証は現行の信頼済み公開鍵を使い、旧counterや過去のverification記録は認可に使わない。削除すると既存の信頼登録を読めなくなる。", "lines": [ { "sha256": "56e399dc4e828e66677d4ad09b4c66a9c09658322b3886a06b4ae688c63dfc84", "count": 1 }, - { - "sha256": "ea16c387d0048571bd0a5576f1358af32acf63f8ee5ebe08c262da83c5a04d88", - "count": 1 - }, - { - "sha256": "bfd48831eb694b879a2859d42dba00fe74a4a817177acc61a44795cbd19b1056", - "count": 1 - }, - { - "sha256": "94af0abdfb45c712e9cd1949ac6a15d1a21c3b271f004c9282f81b5fd5ae4f5d", - "count": 1 - }, { "sha256": "d099e3a7216349c0ef30026967b8fe344e3b08b58a77bad6437468502b56bf71", "count": 1 - }, - { - "sha256": "bed791d41faa85b38602c238d30460681e6d9ebbe56c4c08b843dd1971a589b2", - "count": 1 - }, - { - "sha256": "ce36822c65f785f330e42b7d37241062d994dc4e1c18abaa327b7996da5b6521", - "count": 1 - }, - { - "sha256": "5f4aed5ed50f2bbf54bbb9293948c3294eb2d30086d0fbe7cb07899efb5c96ba", - "count": 1 } ] }, @@ -1259,7 +1235,7 @@ { "path": "crates/workspace-server/src/frozen_repository_key_migration.rs", "kind": "saved_data", - "reason": "schema 86から87の旧SSH key/secret/operation/retention/source列名。旧secret AADと暗号化payloadを開き新key identityへresealし、元operation fingerprint・retention・auditを対応付けるconsumerがある。削除すると既存秘密鍵を復号できず、SSH retry/retentionやauditを失う。曖昧な対応はrollbackし原文archiveを保持する。", + "reason": "schema 86から87の旧SSH key/secret/operation/retention/source列名。旧secret AADと暗号化payloadを開き新key identityへresealし、元operation fingerprint・retention・auditを対応付けるconsumerがある。削除すると既存秘密鍵を復号できず、SSH retry/retentionやauditを失う。曖昧な対応はrollbackし原文archiveを保持する。 時刻修正でhashが変わったcredential/host trust/候補の3 JOIN行は同じ旧保存列を読み、鍵作成UTC秒・ナノ秒を操作時刻と比較する。旧列を変えると実DBのSSH snapshotを対応付けられないため、この3行の例外だけを同期した。", "lines": [ { "sha256": "95284617f203ea22df2ba1fe49903db531a0dd68dbf8654981505516a07dfcce", @@ -1378,15 +1354,15 @@ "count": 1 }, { - "sha256": "8948263f582419232838423d99d269c423dee4b21c8189d97fde3de97afc2104", + "sha256": "a69d60581d924b868edf66ad6bfe63a47f44483aed13d7467df3303ad4b5d3c5", "count": 1 }, { - "sha256": "9b2d38cea48eaf100fdcd2444036da17a0a8104090b429e6c64e56255d45d9c0", + "sha256": "1397df3babbcbfdb0c3a7e12ce1a5bcaf396ef295880b4d2885bcd0b54fb6d1b", "count": 1 }, { - "sha256": "f8333763f67f3a40ae3e245f523e2b0dca6a7b6c1b42d5aa61273bcbf649c5f2", + "sha256": "291ea2e36bf20850c631eed6a64064c193c90d2fe2b346b0a13bc5031a1dbde9", "count": 1 }, { @@ -1428,14 +1404,14 @@ { "path": "crates/workspace-server/src/frozen_repository_key_tests.rs", "kind": "saved_data", - "reason": "legacy_repository_keysの旧SSH key/暗号secret/operation/retention/Workdir fixtureとrollback SQL。旧AADを本当に開いてresealし、元fingerprintでのreplay、retention、audit、不可逆な旧Workdirのarchiveを保持することを検証する。綴りを変えると既存secretの復号可能性と不整合rollbackの検証を失う。", + "reason": "legacy_repository_keysの旧SSH key/暗号secret/operation/retention/Workdir fixtureとrollback SQL。旧AADを本当に開いてresealし、元fingerprintでのreplay、retention、audit、不可逆な旧Workdirのarchiveを保持することを検証する。綴りを変えると既存secretの復号可能性と不整合rollbackの検証を失う。 既存fixtureの5 INSERT行は偽時刻からRFC3339へ変更されたためhashのみ同期した。旧credential/host trust表とcounterの綴り・実AAD入力は不変であり、移行成功と別鍵retention/欠けたreceiptのrollbackを同じ旧形式で検証する。", "lines": [ { "sha256": "a62f153e1748caa20f7cb81656b8c6b29d32b01bf533a3ffaf36fb5be9a5ae3b", "count": 1 }, { - "sha256": "de6b3a38afad3d08bc4bcb534c1374446ae59b28ed08235643fd61c3b42992fd", + "sha256": "59be6fd4e7c21afa91e2618bde39dbf7b2325be929723cabc19d75eebc0fe606", "count": 1 }, { @@ -1451,11 +1427,11 @@ "count": 1 }, { - "sha256": "af41b9c5edf66189130020a79568cad6b86b56835935e06969d34a4cc0569e93", + "sha256": "d7a003785fdc9de68e8f980f9ccc49360b5ecf9c382a9d34b348d3070cca724c", "count": 1 }, { - "sha256": "9ec9ccf2b8e304c1cb1ef9b755a5cfe4d203879a9787394a9c6edca59c6fddca", + "sha256": "3ce073da6367a945e80717f01b342a6ec3eb6495fce1f8bf651ce9406584d8b3", "count": 1 }, { @@ -1499,7 +1475,7 @@ "count": 1 }, { - "sha256": "b2495afb6d5b32f13e77eb7a7fa4866f12531d3ed059aa44288ccb69d0fa4e26", + "sha256": "04ea12161ed1f19d3edd90d0619d03e80a9aaa5c9854b46e437e3c87852ec0ff", "count": 1 }, { @@ -1507,7 +1483,7 @@ "count": 1 }, { - "sha256": "4a4ea05d99ba512fbe0ecb7c31d176a08c59bb6e8b1156624bee907ad4dfc103", + "sha256": "906649318474b0ef47fa35be64c7f9b7f6fe1e50aa477d6bb424137afe079fb2", "count": 1 }, { @@ -1929,7 +1905,7 @@ { "path": "crates/workspace-server/src/migration_entrypoint_tests.rs", "kind": "saved_data", - "reason": "schema-87 retention/removal policyとschema-86 SSH key fixture。public migrate_database/open entrypointが旧DBのpolicy binding・terminal worker evidence・暗号鍵を移すことを検証する。旧SQLを現行名へ変えると公開入口を通る実データ保持/rollbackの証明を失う。", + "reason": "schema-87 retention/removal policyとschema-86 SSH key fixture。public migrate_database/open entrypointが旧DBのpolicy binding・terminal worker evidence・暗号鍵を移すことを検証する。旧SQLを現行名へ変えると公開入口を通る実データ保持/rollbackの証明を失う。 公開dry-run/applyのcredential保存行は偽時刻からRFC3339へ変更されたためhashのみ同期した。元暗号AADとkey receiptに結び付いた旧表名を維持し、旧DBを公開入口から移す証明を残す。", "lines": [ { "sha256": "2a4cb9f8dacaf637103fe369c8ce5106bbb442097e644483c3900cdd98fade81", @@ -1948,7 +1924,7 @@ "count": 1 }, { - "sha256": "de6b3a38afad3d08bc4bcb534c1374446ae59b28ed08235643fd61c3b42992fd", + "sha256": "59be6fd4e7c21afa91e2618bde39dbf7b2325be929723cabc19d75eebc0fe606", "count": 1 } ] @@ -2182,5 +2158,86 @@ "count": 1 } ] + }, + { + "path": "crates/workspace-server/src/frozen_repository_key_migration.rs", + "kind": "saved_data", + "reason": "prepare_repository_cutover_timesの旧schema86 snapshot照合SQL3行(credential operation、host trust operation、credential candidate)。それぞれ実在した旧保存counter列を取り出し、鍵作成時刻が操作時刻より新しい再利用鍵や証拠欠落をコピー前に拒否する。綴りを現行列へ置換すると旧DBを読めず、NULL fingerprint挿入前の安全なrollbackとretained SSH retry証拠の保持を失う。新writerの状態名として使わない。", + "lines": [ + { + "sha256": "fdd4d8fa9046d50591afa726d7d12c455020571a5b74f1e6c748556b36f50db6", + "count": 1 + }, + { + "sha256": "765772579ddb0b8e014bed913836f722a25f97a4293abcc823e2d23ab7d0b3b4", + "count": 1 + }, + { + "sha256": "c368d019ed0698f769513bf2da22f2f8894f66ebfa4242554544d5ca05316773", + "count": 1 + } + ] + }, + { + "path": "crates/workspace-server/src/frozen_repository_key_tests.rs", + "kind": "saved_data", + "reason": "legacy_ssh_snapshot_timesとnewer_reused_snapshot_keys/invalid_authority_timestampsの旧保存入力。credential時刻UPDATE3箇所とhost trust時刻UPDATE2箇所は混在形式の成功・操作より新しい鍵の拒否・不正時刻のrollbackを分けて検証する。host INSERTとfailed Workdir snapshot UPDATEは実旧receiptを作り、main credentialをNULLにする1行は候補だけの不整合を隔離する。credential/host trustのsource文字列とmatch各2行は不正値を実旧表のcreated_atへ入れて文脈付き拒否を確認する。旧列/表の綴りを変えるとfixtureを操作できず、過去のSSH snapshotに別鍵を結び付けない検証を失う。", + "lines": [ + { + "sha256": "f49461fbbb42bf1a13996379f031645d31ab7f794e64c6cc66fe6a5e5d14ecf8", + "count": 3 + }, + { + "sha256": "651e611e62c3be58f95b2c5aae3f95f98ea3858180b77132968bef7ae4616165", + "count": 1 + }, + { + "sha256": "f78c4397f9e4c8058fa8d320949e0cc23ef8c47c37a643cf73e91cb84c4d4b38", + "count": 1 + }, + { + "sha256": "0c49320a2fcff35a9fffd8eb02517c97e538006c614b6243d02757fd12c23727", + "count": 2 + }, + { + "sha256": "4e64ea62da159a158270bb0e6f9c1086090a73deb826f4b8df02438f8065fd2d", + "count": 1 + }, + { + "sha256": "48ffab07c0e4a66b82944f7001e6cc8ffef8a964f01a1b348db14537204f6a7f", + "count": 1 + }, + { + "sha256": "18341cc32d7711507e0ec849ba9291c7265944149d9b7814c72a6310272546f4", + "count": 1 + }, + { + "sha256": "739474845828016af6993686f70b3d716fbe2f37cba59aa0f2e9a3659e40ce2c", + "count": 1 + }, + { + "sha256": "cc05720883982d4345c5059aebbfc6f2dfc4d07126d2ee0425acde502c4a762b", + "count": 1 + } + ] + }, + { + "path": "crates/workspace-server/src/frozen_repository_key_tests.rs", + "kind": "saved_data", + "reason": "assert_timestamp_cutover_rolls_backの旧credential表存在と旧retention件数の2行、およびrepository_key_cutover_rejects_orphan_candidates_without_dropping_source_evidenceの旧表存在1行。失敗後にもschema86の秘密鍵・retained retry参照・候補が消えないことを実旧表名で確認する保存データ保持のassertionであり、旧名を現行出力に許可する契約ではない。削除するとtimestamp/candidate拒否時の部分移行やデータ喪失を検出できなくなる。", + "lines": [ + { + "sha256": "d7080c861537f04caf0ddddc36c166eb965b74bc8dbd57f5d4a3ef8232804c66", + "count": 1 + }, + { + "sha256": "821421c002e9020248029b8a74b6b86ab3abd9c37d719cd3bf2572b096f54e9f", + "count": 1 + }, + { + "sha256": "1378598b848b6e444d643deb72f7403281d548885557ba819822d838a8148d81", + "count": 1 + } + ] } ] diff --git a/web/workspace/src/lib/generated/companion-api.ts b/web/workspace/src/lib/generated/companion-api.ts index 101b80385..2801d4c4a 100644 --- a/web/workspace/src/lib/generated/companion-api.ts +++ b/web/workspace/src/lib/generated/companion-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-companion-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: companion-operation-schema-closure,openapi-3.1,strict-integer-ranges,sorted-output // Output: web/workspace/src/lib/generated/companion-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_companion_api_types -- --check diff --git a/web/workspace/src/lib/generated/repository-api.ts b/web/workspace/src/lib/generated/repository-api.ts index e8a9fa21f..3dab94650 100644 --- a/web/workspace/src/lib/generated/repository-api.ts +++ b/web/workspace/src/lib/generated/repository-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-repository-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: repository-operation-schema-closure,openapi-3.1,format:uint64=safe-number,strict-safe-integers,sorted-output // Output: web/workspace/src/lib/generated/repository-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_repository_openapi_types -- --check diff --git a/web/workspace/src/lib/generated/runtime-api.ts b/web/workspace/src/lib/generated/runtime-api.ts index bd8dadd0f..f1224e56e 100644 --- a/web/workspace/src/lib/generated/runtime-api.ts +++ b/web/workspace/src/lib/generated/runtime-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-runtime-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: runtime-operation-schema-closure,openapi-3.1,strict-integer-ranges,sorted-output // Output: web/workspace/src/lib/generated/runtime-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_runtime_api_types -- --check @@ -25,7 +25,7 @@ export type CompletionEntry = { description?: string | null; invocation?: Featur export type CompletionKind = "file" | "feature" | "feature_argument"; -export type CreateRemoteRuntimeRequest = { display_name?: string | null; endpoint: string; expected_binding_id?: string | null; public_bundle: RuntimePublicIdentityBundle; workspace_trust_id: string; }; +export type CreateRemoteRuntimeRequest = { display_name?: string | null; endpoint: string; expected_binding_id?: string | null; public_bundle: RuntimePublicIdentityBundle; }; export type Diagnostic = { code: string; message: string; severity: DiagnosticSeverity; }; @@ -77,11 +77,9 @@ export type RuntimeConfigBundleResult = { availability?: unknown; diagnostics?: export type RuntimeConfigBundleSyncRequest = { bundle: unknown; }; -export type RuntimeConnectionDisplayState = "configured" | "verified" | "unavailable" | "revoked"; - export type RuntimeConnectionTestFailureKind = "authentication" | "authorization" | "network_unreachable" | "timeout" | "tls_or_transport" | "malformed_response" | "protocol_version_mismatch" | "runtime_identity_mismatch" | "configuration"; -export type RuntimeConnectionTestResponse = { actual_protocol_version?: number | null; binding_id: string; checked_at: string; connection_state: RuntimeConnectionDisplayState; diagnostics?: Array; expected_protocol_version: number; failure_kind?: RuntimeConnectionTestFailureKind | null; runtime_id: string; status: RuntimeConnectionTestStatus; verification?: RuntimeVerificationEvidenceSummary | null; workspace_id: string; }; +export type RuntimeConnectionTestResponse = { actual_protocol_version?: number | null; binding_id: string; checked_at: string; diagnostics?: Array; expected_protocol_version: number; failure_kind?: RuntimeConnectionTestFailureKind | null; runtime_id: string; status: RuntimeConnectionTestStatus; workspace_id: string; }; export type RuntimeConnectionTestStatus = "compatible" | "failed"; @@ -121,10 +119,6 @@ export type RuntimeTrustKeyState = { binding_id?: string | null; created_at?: st export type RuntimeTrustKeyStatus = "unconfigured" | "active" | "revoked"; -export type RuntimeVerificationEvidenceSummary = { binding_id: string; last_checked_at: string; last_outcome: RuntimeVerificationOutcome; runtime_public_key_fingerprint: string; verified_at?: string | null; workspace_key_id: string; workspace_public_key_fingerprint: string; workspace_trust_id: string; }; - -export type RuntimeVerificationOutcome = "verified" | "challenge_issued" | "verification_failed" | "connectivity_failed"; - export type RuntimeWorkerCompletionsRequest = { context?: CompletionContext | null; kind: CompletionKind; prefix?: string; request_id?: string | null; }; export type RuntimeWorkerCompletionsResult = { context?: CompletionContext | null; diagnostics?: Array; entries: Array; kind: CompletionKind; prefix: string; request_id?: string | null; runtime_id: string; worker_id: string; }; @@ -217,9 +211,7 @@ export type WorkingDirectorySummary = { cleanliness?: string | null; cleanup_tar export type WorkspaceConfigAccess = "read_only" | "read_write"; -export type WorkspaceRuntimeBindingState = "configured" | "verified" | "revoked"; - -export type WorkspaceRuntimeBindingSummary = { binding_id: string; connection_state: RuntimeConnectionDisplayState; state: WorkspaceRuntimeBindingState; verification?: RuntimeVerificationEvidenceSummary | null; workspace_key_id?: string | null; workspace_trust_id?: string | null; }; +export type WorkspaceRuntimeBindingSummary = { binding_id: string; revoked_at?: string | null; }; export type WorkspaceRuntimeDetail = { endpoint?: string | null; recent_audit?: Array; runtime: WorkspaceRuntimeResource; trust_key: RuntimeTrustKeyState; workspace_id: string; }; diff --git a/web/workspace/src/lib/generated/ticket-api.ts b/web/workspace/src/lib/generated/ticket-api.ts index 8afefad5e..d8700209e 100644 --- a/web/workspace/src/lib/generated/ticket-api.ts +++ b/web/workspace/src/lib/generated/ticket-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-ticket-browser-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: ticket-objective-merge-request-browser-operation-schema-closure,openapi-3.1,strict-integer-ranges,sorted-output // Output: web/workspace/src/lib/generated/ticket-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_ticket_api_types -- --check diff --git a/web/workspace/src/lib/generated/workdir-api.ts b/web/workspace/src/lib/generated/workdir-api.ts index d4819b6f9..cfcc87ab9 100644 --- a/web/workspace/src/lib/generated/workdir-api.ts +++ b/web/workspace/src/lib/generated/workdir-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-workdir-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: workdir-operation-schema-closure,openapi-3.1,strict-integer-ranges,sorted-output // Output: web/workspace/src/lib/generated/workdir-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_workdir_api_types -- --check diff --git a/web/workspace/src/lib/generated/worker-launch-api.ts b/web/workspace/src/lib/generated/worker-launch-api.ts index d48cb23e8..dce09b8bf 100644 --- a/web/workspace/src/lib/generated/worker-launch-api.ts +++ b/web/workspace/src/lib/generated/worker-launch-api.ts @@ -1,6 +1,6 @@ // @generated by yoi-worker-openapi-typescript@1. Do not edit. // Input: openapi/server-api.json -// Input contract: 0.1.0 (sha256:6f5ab2a613fca9996680d3a08962dc580563457811e68679574ff4325c86ec4e) +// Input contract: 0.1.0 (sha256:2e8dcd75d5d7b70c5d820999afcabf3d1377896732c7a3e8c3839e73a994974c) // Options: worker-operation-schema-closure,openapi-3.1,strict-integer-ranges,sorted-output // Output: web/workspace/src/lib/generated/worker-launch-api.ts // Regenerate/check: cargo run -q -p server-api --example generate_worker_launch_api_types -- --check diff --git a/web/workspace/src/lib/workspace/api/runtime-connection.ts b/web/workspace/src/lib/workspace/api/runtime-connection.ts index bde3120a8..535129cb9 100644 --- a/web/workspace/src/lib/workspace/api/runtime-connection.ts +++ b/web/workspace/src/lib/workspace/api/runtime-connection.ts @@ -2,15 +2,12 @@ import type { Diagnostic, RuntimeConnectionTestFailureKind, RuntimeConnectionTestResponse, - RuntimeVerificationEvidenceSummary, } from "#lib/generated/runtime-api.ts"; const RESPONSE_KEYS = [ "workspace_id", "runtime_id", "binding_id", - "connection_state", - "verification", "checked_at", "status", "failure_kind", @@ -108,27 +105,11 @@ export function parseRuntimeConnectionTestResponse( return null; } const bindingId = value.binding_id; - const connectionState = parseConnectionState(value.connection_state); - const verification = parseVerificationEvidence(value.verification); - if ( - !isBoundedString(bindingId) || - connectionState === null || - (value.verification !== null && verification === null) || - (verification !== null && - verification.binding_id !== bindingId) || - (connectionState === "verified" && value.status !== "compatible") || - (connectionState === "verified" && verification !== null && - (verification.last_outcome !== "verified" || - verification.verified_at === null)) - ) { - return null; - } + if (!isBoundedString(bindingId, 256)) return null; return { workspace_id: value.workspace_id, runtime_id: value.runtime_id, binding_id: bindingId, - connection_state: connectionState, - verification, checked_at: value.checked_at, status: value.status, failure_kind: failureKind as RuntimeConnectionTestFailureKind | null, @@ -138,60 +119,6 @@ export function parseRuntimeConnectionTestResponse( }; } -function parseConnectionState( - value: unknown, -): "configured" | "verified" | "unavailable" | "revoked" | null { - return value === "configured" || value === "verified" || - value === "unavailable" || value === "revoked" - ? value - : null; -} - -function parseVerificationEvidence( - value: unknown, -): RuntimeVerificationEvidenceSummary | null { - if (value === null) return null; - const keys = [ - "verified_at", - "last_checked_at", - "last_outcome", - "binding_id", - "workspace_key_id", - "workspace_public_key_fingerprint", - "workspace_trust_id", - "runtime_public_key_fingerprint", - ] as const; - if (!isRecord(value) || !hasExactKeys(value, keys)) return null; - if ( - (value.verified_at !== null && - (!isBoundedString(value.verified_at, 128) || - Number.isNaN(Date.parse(value.verified_at)))) || - !isBoundedString(value.last_checked_at, 128) || - Number.isNaN(Date.parse(value.last_checked_at)) || - (value.last_outcome !== "verified" && - value.last_outcome !== "challenge_issued" && - value.last_outcome !== "verification_failed" && - value.last_outcome !== "connectivity_failed") || - !isBoundedString(value.binding_id) || - !isBoundedString(value.workspace_key_id, 128) || - !isBoundedString(value.workspace_public_key_fingerprint) || - !isBoundedString(value.workspace_trust_id) || - !isBoundedString(value.runtime_public_key_fingerprint, 128) - ) { - return null; - } - return { - verified_at: value.verified_at, - last_checked_at: value.last_checked_at, - last_outcome: value.last_outcome, - binding_id: value.binding_id, - workspace_key_id: value.workspace_key_id, - workspace_public_key_fingerprint: value.workspace_public_key_fingerprint, - workspace_trust_id: value.workspace_trust_id, - runtime_public_key_fingerprint: value.runtime_public_key_fingerprint, - }; -} - export async function testRuntimeConnection( workspaceId: string, runtimeId: string, diff --git a/web/workspace/src/lib/workspace/api/runtime-management.ts b/web/workspace/src/lib/workspace/api/runtime-management.ts index fa2809323..a34a83871 100644 --- a/web/workspace/src/lib/workspace/api/runtime-management.ts +++ b/web/workspace/src/lib/workspace/api/runtime-management.ts @@ -3,7 +3,6 @@ import type { Diagnostic, RemoveRuntimeRequest, RevokeRuntimeTrustKeyRequest, - RuntimeConnectionDisplayState, RuntimeIdentityAuthority, RuntimeManagementApiError, RuntimeManagementSummary, @@ -17,14 +16,13 @@ import type { RuntimeTrustKeyRevealResponse, RuntimeTrustKeyState, RuntimeTrustKeyStatus, - RuntimeVerificationEvidenceSummary, UpdateRemoteRuntimeRequest, - WorkspaceRuntimeBindingState, WorkspaceRuntimeBindingSummary, WorkspaceRuntimeDetail, WorkspaceRuntimeListResponse, WorkspaceRuntimeResource, } from "#lib/generated/runtime-api.ts"; +import type { RepositoryApiError } from "#lib/generated/repository-api.ts"; import { workspaceApiPath } from "./http.ts"; export type WorkspaceRuntimeList = @@ -90,17 +88,6 @@ const CONFLICT_KINDS = new Set([ "stale_binding", "fingerprint_in_use", ]); -const BINDING_STATES = new Set([ - "configured", - "verified", - "revoked", -]); -const CONNECTION_STATES = new Set([ - "configured", - "verified", - "unavailable", - "revoked", -]); const REMOVAL_OPERATION_STATES = new Set([ "pending", "cleanup_pending", @@ -326,140 +313,16 @@ function runtimeSource(value: unknown, path: string): RuntimeSourceSummary { }; } -function runtimeVerification( - value: unknown, - path: string, -): RuntimeVerificationEvidenceSummary { - const item = object(value, path); - exactKeys( - item, - [ - "verified_at", - "last_checked_at", - "last_outcome", - "binding_id", - "workspace_key_id", - "workspace_public_key_fingerprint", - "workspace_trust_id", - "runtime_public_key_fingerprint", - ], - [], - path, - ); - const verifiedAt = item.verified_at === null - ? null - : boundedString(item.verified_at, `${path}.verified_at`, 128); - const lastOutcome = enumValue( - item.last_outcome, - `${path}.last_outcome`, - new Set( - [ - "verified", - "challenge_issued", - "verification_failed", - "connectivity_failed", - ] as const, - ), - ); - return { - verified_at: verifiedAt, - last_checked_at: boundedString( - item.last_checked_at, - `${path}.last_checked_at`, - 128, - ), - last_outcome: lastOutcome, - binding_id: boundedIdentity( - item.binding_id, - `${path}.binding_id`, - ), - workspace_key_id: boundedString( - item.workspace_key_id, - `${path}.workspace_key_id`, - LIMITS.idBytes, - ), - workspace_public_key_fingerprint: boundedIdentity( - item.workspace_public_key_fingerprint, - `${path}.workspace_public_key_fingerprint`, - ), - workspace_trust_id: boundedIdentity( - item.workspace_trust_id, - `${path}.workspace_trust_id`, - ), - runtime_public_key_fingerprint: boundedString( - item.runtime_public_key_fingerprint, - `${path}.runtime_public_key_fingerprint`, - LIMITS.fingerprintBytes, - ), - }; -} - function runtimeBinding( value: unknown, path: string, - requiresWorkspaceIdentity: boolean, ): WorkspaceRuntimeBindingSummary { const item = object(value, path); - exactKeys( - item, - ["state", "connection_state", "binding_id"], - ["workspace_key_id", "workspace_trust_id", "verification"], - path, - ); - const workspaceKeyId = optionalNullableString( - item.workspace_key_id, - `${path}.workspace_key_id`, - LIMITS.idBytes, - ); - const workspaceTrustId = optionalNullableIdentity( - item.workspace_trust_id, - `${path}.workspace_trust_id`, - ); - const state = enumValue(item.state, `${path}.state`, BINDING_STATES); - if ( - requiresWorkspaceIdentity && - state !== "revoked" && - (workspaceKeyId == null || workspaceTrustId == null) - ) { - return fail(path, "requires Workspace signing key identity metadata"); - } - const connectionState = enumValue( - item.connection_state, - `${path}.connection_state`, - CONNECTION_STATES, - ); - const binding_id = boundedIdentity(item.binding_id, `${path}.binding_id`); - const verification = item.verification === undefined - ? undefined - : runtimeVerification(item.verification, `${path}.verification`); - if ( - verification !== undefined && verification.binding_id !== binding_id - ) { - return fail(path, "verification must match the current binding binding_id"); - } - if ( - requiresWorkspaceIdentity && - connectionState === "verified" && - (verification === undefined || - verification.verified_at === null || - verification.last_outcome !== "verified") - ) { - return fail( - path, - "verified Workspace identity binding requires verification evidence", - ); - } + exactKeys(item, ["binding_id"], ["revoked_at"], path); return { - state, - connection_state: connectionState, - binding_id, - ...(workspaceKeyId === undefined - ? {} - : { workspace_key_id: workspaceKeyId }), - ...(workspaceTrustId === undefined - ? {} - : { workspace_trust_id: workspaceTrustId }), - ...(verification === undefined ? {} : { verification }), + binding_id: boundedIdentity(item.binding_id, `${path}.binding_id`), + revoked_at: + optionalNullableTimestamp(item.revoked_at, `${path}.revoked_at`) ?? null, }; } @@ -483,7 +346,7 @@ function runtimeManagement( const builtIn = boolean(item.built_in, `${path}.built_in`); const binding = item.binding == null ? undefined - : runtimeBinding(item.binding, `${path}.binding`, !builtIn); + : runtimeBinding(item.binding, `${path}.binding`); return { built_in: builtIn, config_managed: boolean(item.config_managed, `${path}.config_managed`), @@ -763,7 +626,7 @@ export function parseRuntimeTrustConflict( exactKeys( response, ["error", "message"], - ["current_binding_id", "current_fingerprint"], + ["current_binding_id", "current_fingerprint", "diagnostics"], "Runtime trust conflict", ); return { @@ -786,6 +649,15 @@ export function parseRuntimeTrustConflict( "Runtime trust conflict.current_fingerprint", LIMITS.fingerprintBytes, ), + ...(response.diagnostics === undefined ? {} : { + diagnostics: array( + response.diagnostics, + "Runtime trust conflict.diagnostics", + LIMITS.diagnostics, + ).map((entry, index) => + diagnostic(entry, `Runtime trust conflict.diagnostics[${index}]`) + ), + }), }; } @@ -891,6 +763,37 @@ async function readBoundedJson(response: Response): Promise { } } +function repositoryApiError(value: unknown): RepositoryApiError { + const response = object(value, "Runtime trust error"); + exactKeys( + response, + ["error", "message"], + ["diagnostics"], + "Runtime trust error", + ); + return { + error: boundedString( + response.error, + "Runtime trust error.error", + LIMITS.idBytes, + ), + message: boundedString( + response.message, + "Runtime trust error.message", + LIMITS.conflictMessageBytes, + ), + ...(response.diagnostics === undefined ? {} : { + diagnostics: array( + response.diagnostics, + "Runtime trust error.diagnostics", + LIMITS.diagnostics, + ).map((entry, index) => + diagnostic(entry, `Runtime trust error.diagnostics[${index}]`) + ), + }), + }; +} + function requestErrorFrom( value: unknown, status: number, @@ -917,30 +820,13 @@ function requestErrorFrom( ), ); } - exactKeys( - response, - ["error", "message", "diagnostics"], - [], - "Runtime trust error", - ); - const diagnostics = array( - response.diagnostics, - "Runtime trust error.diagnostics", - LIMITS.diagnostics, - ).map((entry, index) => - diagnostic(entry, `Runtime trust error.diagnostics[${index}]`) - ); - const message = boundedString( - response.message, - "Runtime trust error.message", - LIMITS.conflictMessageBytes, - ); - const field = diagnostics.some((entry) => + const error = repositoryApiError(response); + const field = (error.diagnostics ?? []).some((entry) => entry.code.startsWith("runtime_public_key_") ) ? "public_key" : null; - return new RuntimeTrustRequestError(message, field); + return new RuntimeTrustRequestError(error.message, field); } catch { return new RuntimeTrustRequestError( `Runtime trust request failed (${status})`, @@ -960,6 +846,21 @@ function throwRuntimeTrustConflict(payload: unknown): never { throw new RuntimeTrustConflictError(conflict); } +function throwRuntimeCreationConflict(payload: unknown): never { + let error: RepositoryApiError; + try { + error = repositoryApiError(payload); + } catch { + throw new RuntimeTrustRequestError( + "Runtime trust conflict response was invalid", + ); + } + if (CONFLICT_KINDS.has(error.error as RuntimeTrustConflictKind)) { + throwRuntimeTrustConflict(error); + } + throw requestErrorFrom(error, 409); +} + async function finishMutation( response: Response, workspaceId: string, @@ -990,17 +891,16 @@ export async function createRemoteRuntime( request: CreateRemoteRuntimeRequest, fetchImpl: typeof fetch = fetch, ): Promise { - const enrollmentId = boundedIdentity(request.workspace_trust_id, "workspace_trust_id"); const response = await fetchImpl( workspaceApiPath(workspaceId, "/runtimes"), { method: "POST", headers: { "content-type": "application/json" }, - body: JSON.stringify({ ...request, workspace_trust_id: enrollmentId }), + body: JSON.stringify(request), }, ); const payload = await readBoundedJson(response); - if (response.status === 409) throwRuntimeTrustConflict(payload); + if (response.status === 409) throwRuntimeCreationConflict(payload); if (!response.ok) throw requestErrorFrom(payload, response.status); const runtime = runtimeResource(payload, "Runtime create response"); if (runtime.runtime_id !== request.public_bundle.identity_id) { diff --git a/web/workspace/src/lib/workspace/repositories/ssh-connection.ts b/web/workspace/src/lib/workspace/repositories/ssh-connection.ts index 6bf8eb28a..983fd4a1d 100644 --- a/web/workspace/src/lib/workspace/repositories/ssh-connection.ts +++ b/web/workspace/src/lib/workspace/repositories/ssh-connection.ts @@ -68,6 +68,6 @@ export function repositorySshProbeRuntimes( runtime.management.endpoint_configured && runtime.management.binding !== undefined && runtime.management.binding !== null && - runtime.management.binding.state !== "revoked" + runtime.management.binding.revoked_at == null ); } diff --git a/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/+page.svelte b/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/+page.svelte index b1f7d5aab..f9d32bae8 100644 --- a/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/+page.svelte +++ b/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/+page.svelte @@ -19,7 +19,6 @@ let { data }: PageProps = $props(); let runtimePublicBundle = $state(''); - let workspaceTrustId = $state(''); let displayName = $state(''); let endpoint = $state(''); let runtimeFingerprint = $state(null); @@ -36,9 +35,7 @@ function connectionTestSummary(result: RuntimeConnectionTestResponse): string { if (result.status === 'compatible') { - return result.connection_state === 'verified' - ? `Verified · protocol v${result.actual_protocol_version}` - : `Compatible · ${result.connection_state} · protocol v${result.actual_protocol_version}`; + return `Signed ping succeeded · protocol v${result.actual_protocol_version}`; } switch (result.failure_kind) { case 'authentication': return 'Authentication failed'; @@ -145,18 +142,16 @@ } await createRemoteRuntime(data.workspaceId, { public_bundle: publicBundle, - workspace_trust_id: workspaceTrustId, display_name: displayName || null, endpoint, expected_binding_id: null, }); runtimePublicBundle = ''; - workspaceTrustId = ''; runtimeFingerprint = null; displayName = ''; endpoint = ''; showAddRuntime = false; - requestNotice = 'Runtime registered for this Workspace. Run Test to complete authenticated verification.'; + requestNotice = 'Runtime registered for this Workspace. Run Test to observe a signed ping.'; await invalidateAll(); } catch (error) { requestError = error instanceof RuntimeTrustRequestError || error instanceof Error @@ -171,7 +166,8 @@ runtime: WorkspaceRuntimeResource, ): RuntimeConnectionTestResponse | undefined { const result = testResults[runtime.runtime_id]; - return result?.binding_id === runtime.management?.binding?.binding_id + return result?.workspace_id === data.workspaceId && + result?.binding_id === runtime.management?.binding?.binding_id ? result : undefined; } @@ -179,12 +175,16 @@ async function testRuntime(runtime: WorkspaceRuntimeResource): Promise { const bindingId = runtime.management?.binding?.binding_id; if (typeof bindingId !== 'string') return; + const workspaceId = data.workspaceId; const generation = ++connectionTestGeneration; requestError = null; + const { [runtime.runtime_id]: _previousResult, ...remainingResults } = testResults; + testResults = remainingResults; busyRuntimeId = runtime.runtime_id; try { const result = await testRuntimeConnection(data.workspaceId, runtime.runtime_id); if ( + workspaceId !== data.workspaceId || generation !== connectionTestGeneration || result.binding_id !== bindingId ) { @@ -261,15 +261,9 @@
yoi-runtime trust-workspace add --bundle {workspaceBundleFilename()}
- Copy the Runtime-issued workspace_trust_id from trust-workspace add/show. - It identifies this enrollment, not the Workspace key fingerprint. Pass the same --fs-root and --fs-runtime-dir options used by the Runtime service. Existing Workspace trust entries are preserved. - {:else}

Loading Workspace public identity…

{/if} @@ -323,14 +317,14 @@

Registration stores this Workspace-scoped binding. After it appears in the list, run - Test to complete authenticated verification. + Test to observe a signed ping. Each request authenticates independently.

diff --git a/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/[runtimeId]/+page.svelte b/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/[runtimeId]/+page.svelte index 770c81310..bde2d1441 100644 --- a/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/[runtimeId]/+page.svelte +++ b/web/workspace/src/routes/w/[workspaceId]/settings/runtimes/[runtimeId]/+page.svelte @@ -2,6 +2,7 @@ import { goto, invalidateAll } from '$app/navigation'; import type { RevokeRuntimeTrustKeyRequest, + RuntimeConnectionTestResponse, RuntimeTrustKeyStatus, } from '#lib/generated/runtime-api.ts'; import { @@ -17,6 +18,7 @@ RuntimeTrustRequestError, type RuntimeTrustRouteOperation, } from '#lib/workspace/api/runtime-management.ts'; + import { testRuntimeConnection } from '#lib/workspace/api/runtime-connection.ts'; import type { PageProps } from './$types'; type TrustAction = 'create' | 'replace' | 'reactivate'; @@ -25,18 +27,19 @@ let showPublicKey = $state(false); let revealedPublicKey = $state(null); let publicKey = $state(''); - let workspaceTrustId = $state(''); let displayName = $state(''); let endpoint = $state(''); let editingMetadata = $state(false); let deleteRuntimeConfirmation = $state(''); - let busyAction = $state<'metadata' | 'trust' | 'revoke' | 'reveal' | 'copy' | 'delete' | null>(null); + let busyAction = $state<'metadata' | 'trust' | 'revoke' | 'reveal' | 'copy' | 'delete' | 'test' | null>(null); let fieldError = $state(null); let deleteRuntimeError = $state(null); let requestError = $state(null); let successMessage = $state(null); let replacementFingerprint = $state(null); let replacementFingerprintError = $state(null); + let connectionResult = $state(null); + let connectionError = $state(null); let fingerprintGeneration = 0; const routeFence = new RuntimeTrustRouteFence(); const runtimeRemovalAttempt = new RuntimeRemovalAttempt(); @@ -47,10 +50,11 @@ if (nextGeneration === routeGeneration) return; routeGeneration = nextGeneration; fingerprintGeneration += 1; + connectionResult = null; + connectionError = null; showPublicKey = false; revealedPublicKey = null; publicKey = ''; - workspaceTrustId = ''; displayName = data.runtimeDetail?.runtime.label ?? ''; endpoint = data.runtimeDetail?.endpoint ?? ''; editingMetadata = false; @@ -109,6 +113,28 @@ return new TextEncoder().encode(value).byteLength; } + async function testConnection(): Promise { + const bindingId = data.runtimeDetail?.runtime.management.binding?.binding_id; + if (busyAction !== null || !bindingId) return; + const operation = routeFence.capture(data.runtimeId); + const workspaceId = data.workspaceId; + busyAction = 'test'; + connectionResult = null; + connectionError = null; + try { + const result = await testRuntimeConnection(workspaceId, operation.runtimeId); + if (!isCurrentRoute(operation) || workspaceId !== data.workspaceId || + result.binding_id !== data.runtimeDetail?.runtime.management.binding?.binding_id) return; + connectionResult = result; + } catch (error) { + if (isCurrentRoute(operation) && workspaceId === data.workspaceId) { + connectionError = error instanceof Error ? error.message : String(error); + } + } finally { + if (isCurrentRoute(operation) && workspaceId === data.workspaceId) busyAction = null; + } + } + async function reloadAuthority(): Promise { await invalidateAll(); } @@ -195,7 +221,7 @@ const binding = data.runtimeDetail.runtime.management.binding; if (!binding || !data.runtimeDetail.endpoint) { throw new RuntimeTrustRequestError( - 'The Workspace identity binding and authoritative Runtime endpoint are required.', + 'The Runtime binding and authoritative endpoint are required.', ); } await createRemoteRuntime(data.workspaceId, { @@ -203,14 +229,12 @@ identity_id: operation.runtimeId, public_key: key, }, - workspace_trust_id: workspaceTrustId, display_name: data.runtimeDetail.runtime.label, endpoint: data.runtimeDetail.endpoint, expected_binding_id: binding.binding_id, }); if (!isCurrentRoute(operation)) return; publicKey = ''; - workspaceTrustId = ''; showPublicKey = false; revealedPublicKey = null; successMessage = action === 'create' @@ -396,8 +420,8 @@ {@const detail = data.runtimeDetail} {@const runtime = detail.runtime} {@const trust = detail.trust_key} - {@const verifiedTrust = runtime.management.binding?.state === 'verified'} {@const currentAction = trustAction(trust.status)} + {@const latestResult = connectionResult?.workspace_id === data.workspaceId && connectionResult?.runtime_id === data.runtimeId && connectionResult?.binding_id === runtime.management.binding?.binding_id ? connectionResult : null}

Identity and binding

@@ -405,17 +429,32 @@
Runtime ID
{runtime.runtime_id}
Kind
{runtime.kind}
Endpoint
{detail.endpoint ?? 'Not configured'}
-
Status
{runtime.status}
-
Connection state
{runtime.management.binding?.connection_state ?? 'Not configured'}
-
Workspace signing key
{runtime.management.binding?.workspace_key_id ?? '—'}
-
Verified
{formatTimestamp(runtime.management.binding?.verification?.verified_at)}
-
Last verification check
{runtime.management.binding?.verification?.last_outcome ?? '—'} · {formatTimestamp(runtime.management.binding?.verification?.last_checked_at)}
+
Connection test
{runtime.management.binding?.revoked_at ? 'Revoked' : latestResult ? latestResult.status === 'compatible' ? 'Signed ping succeeded' : `Signed ping failed · ${latestResult.failure_kind}` : 'Not tested'}
+
Binding ID
{runtime.management.binding?.binding_id ?? '—'}
Runtime key status
{trust.status}
Fingerprint
{trust.fingerprint ?? '—'}
Created
{formatTimestamp(trust.created_at)}
Updated
{formatTimestamp(trust.updated_at)}
Revoked
{formatTimestamp(trust.revoked_at)}
+ {#if runtime.management.config_managed && !runtime.management.binding?.revoked_at} +
+ +
+ {/if} + {#if latestResult} +

+ Checked {formatTimestamp(latestResult.checked_at)} + {#each latestResult.diagnostics ?? [] as diagnostic} + {diagnostic.message} + {/each} +

+ {/if} + {#if connectionError} + + {/if} {#if (runtime.diagnostics ?? []).length > 0}
    {#each runtime.diagnostics ?? [] as diagnostic} @@ -495,25 +534,7 @@ {/if} {/if} - {#if verifiedTrust} -
    - Runtime public key -

    - This verified key is read-only. Revoke Workspace trust and register the Runtime again to use a different public key. -

    - {trust.fingerprint} -
    - {:else} -
    - - -

    Copy the Runtime-issued workspace_trust_id from trust-workspace add/show for the current enrollment. Do not use a key fingerprint or a revoked enrollment ID.

    +