From 35d5f63be6a861257216a530878bc5898d1149cf Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 16 Jul 2026 19:05:08 +0100 Subject: [PATCH 01/93] Preserve deployment failure tails --- crates/idunn-daemon/src/main.rs | 38 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index a1b5010..5629968 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -3629,13 +3629,23 @@ fn command_output_detail(prefix: &str, output: &std::process::Output) -> String } fn truncate_detail(value: &str, max_chars: usize) -> String { - let mut chars = value.chars(); - let truncated: String = chars.by_ref().take(max_chars).collect(); - if chars.next().is_some() { - format!("{truncated}...") - } else { - truncated + let chars: Vec = value.chars().collect(); + if chars.len() <= max_chars { + return value.to_string(); } + + let separator = "......"; + let separator_len = separator.chars().count(); + if max_chars <= separator_len { + return chars.into_iter().take(max_chars).collect(); + } + + let retained = max_chars - separator_len; + let head_len = retained / 3; + let tail_len = retained - head_len; + let head: String = chars.iter().take(head_len).collect(); + let tail: String = chars.iter().skip(chars.len() - tail_len).collect(); + format!("{head}{separator}{tail}") } fn run_state_migration( @@ -3990,6 +4000,22 @@ mod tests { const EPIPHANY_SHA: &str = "0123456789abcdef0123456789abcdef01234567"; + #[test] + fn truncated_command_detail_preserves_failure_tail() { + let value = format!("{}fatal: promotion failed", "compiler noise\n".repeat(100)); + let detail = truncate_detail(&value, 120); + + assert_eq!(detail.chars().count(), 120); + assert!(detail.starts_with("compiler noise")); + assert!(detail.contains("......")); + assert!(detail.ends_with("fatal: promotion failed")); + } + + #[test] + fn short_command_detail_is_unchanged() { + assert_eq!(truncate_detail("precise failure", 120), "precise failure"); + } + fn authority_record(status: &str) -> BifrostRepositoryReleaseAuthorityRecord { let repository = "GameCult/Epiphany"; let upstream_ref = "refs/heads/main"; From 41d050f62a1ed288acf7fe769d0c37ace7decb88 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 17 Jul 2026 01:19:48 +0100 Subject: [PATCH 02/93] Observe warming daemons without actuation --- crates/odin-core/src/idunn.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/odin-core/src/idunn.rs b/crates/odin-core/src/idunn.rs index a1ac103..bde0756 100644 --- a/crates/odin-core/src/idunn.rs +++ b/crates/odin-core/src/idunn.rs @@ -37,6 +37,21 @@ pub fn plan_keepalive( }; } + if health.state == "warming" { + return IdunnPlan { + decision: decision( + &decision_id, + desired, + "observe", + "daemon is warming under provider-owned progress authority", + &now, + ), + deployment_request: None, + restart_request: None, + operator_alarm: None, + }; + } + if health.state == "degraded" || health.state == "dependency-unavailable" { let alarm_id = format!("alarm:{}:{}", desired.daemon_id, now); let reason = format!( @@ -238,6 +253,23 @@ mod tests { assert!(plan.operator_alarm.is_none()); } + #[test] + fn warming_daemon_is_observed_without_actuation_or_promotion() { + let mut desired = desired(Some("systemctl restart epiphany")); + desired.deploy_command = Some("deploy epiphany".to_string()); + let plan = plan_keepalive( + &desired, + &health("warming"), + "2026-07-17T00:00:02Z", + ); + + assert_eq!(plan.decision.action, "observe"); + assert!(plan.decision.reason.contains("provider-owned progress")); + assert!(plan.deployment_request.is_none()); + assert!(plan.restart_request.is_none()); + assert!(plan.operator_alarm.is_none()); + } + #[test] fn unhealthy_daemon_with_restart_authority_requests_restart() { let plan = plan_keepalive( From 92fab01d37ba2e76aa75be22ee7ee8cae0ae5d64 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 17 Jul 2026 02:37:42 +0100 Subject: [PATCH 03/93] Make Idunn admission atomic and request-bound --- Cargo.lock | 127 +++ crates/idunn-daemon/Cargo.toml | 5 + crates/idunn-daemon/src/main.rs | 1136 +++++++++++++++++++++++-- crates/odin-core/src/documents.rs | 168 +++- crates/odin-core/src/idunn.rs | 6 +- docs/idunn.md | 15 +- scripts/linux/idunn-yggdrasil.service | 4 +- src/Idunn/README.md | 9 +- vendor/cultcache-rs/Cargo.toml | 3 + vendor/cultcache-rs/src/lib.rs | 117 ++- 10 files changed, 1475 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1847980..daa645e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "bitflags" version = "2.12.1" @@ -125,6 +131,12 @@ dependencies = [ "inout", ] +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -173,6 +185,7 @@ dependencies = [ "serde_bytes", "serde_json", "uuid", + "windows-sys 0.61.2", ] [[package]] @@ -215,6 +228,43 @@ dependencies = [ "uuid", ] +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "diff" version = "0.1.13" @@ -232,6 +282,30 @@ dependencies = [ "subtle", ] +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek", + "ed25519", + "serde", + "sha2", + "subtle", + "zeroize", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -254,6 +328,12 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + [[package]] name = "find-msvc-tools" version = "0.1.9" @@ -425,10 +505,13 @@ dependencies = [ "cultcache-rs", "cultmesh-rs", "cultnet-rs", + "ed25519-dalek", "odin-core", "rmp-serde", "serde", "sha2", + "tempfile", + "uuid", ] [[package]] @@ -568,6 +651,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + [[package]] name = "polyval" version = "0.6.2" @@ -707,6 +800,15 @@ dependencies = [ "serde_bytes", ] +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "1.1.4" @@ -802,6 +904,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "slab" version = "0.4.12" @@ -832,6 +943,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "subtle" version = "2.6.1" @@ -1311,6 +1432,12 @@ dependencies = [ "syn", ] +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" + [[package]] name = "zmij" version = "1.0.21" diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index ff89f1c..8dbc967 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -19,3 +19,8 @@ rmp-serde = "1.3" sha2 = "0.10" chrono = "0.4" serde = { version = "1", features = ["derive"] } +ed25519-dalek = "2.2" +uuid = "1" + +[dev-dependencies] +tempfile = "3" diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 5629968..32ea640 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -1,8 +1,7 @@ use anyhow::{Context, Result, anyhow}; -#[cfg(test)] -use cultcache_rs::CacheBackingStore; +use cultcache_rs::{CacheBackingStore, DatabaseEntry}; use cultcache_rs::{ - CultCache, CultCacheEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, + CultCache, CultCacheEnvelope, CultCacheExpectedEnvelope, SingleFileMessagePackBackingStore, }; use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions}; use cultnet_rs::{ @@ -11,15 +10,17 @@ use cultnet_rs::{ CultNetRudpSocketTransportOptions, CultNetWireContract, decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, encode_rudp_packet, }; +use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ - BifrostRepositoryReleaseAuthorityRecord, IdunnCommandBoundaryRecord, IdunnDaemonHealthRecord, - IdunnDaemonSurgeryPlanRecord, IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, - IdunnDeploymentRequestRecord, IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, - IdunnLifecycleCommandRecord, IdunnOperatorAlarmRecord, IdunnReleaseTargetRecord, - IdunnRestartRequestRecord, IdunnRestartResultRecord, IdunnRolloutPlanRecord, - IdunnRolloutResultRecord, IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, - IdunnStateMigrationPlanRecord, IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, - OdinDocuments, plan_keepalive, + BifrostRepositoryReleaseAuthorityRecord, IdunnCommandBoundaryRecord, + IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, IdunnDaemonSurgeryPlanRecord, + IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, IdunnDeploymentRequestRecord, + IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, + IdunnOperatorAlarmRecord, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, + IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, + IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, + IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, + IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, OdinDocuments, plan_keepalive, }; use sha2::{Digest, Sha256}; use std::collections::HashMap; @@ -36,8 +37,59 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0"; const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001; +const EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE: &str = "epiphany.idunn_signed_runtime_health"; +const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = + "epiphany.idunn_signed_runtime_health.v0"; +const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; +const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; +const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; +const HOST_IDENTITY_TYPE: &str = "epiphany.host_identity_trust_anchor.v0"; +const HOST_IDENTITY_KEY: &str = "host-incarnation-public"; +const HOST_SIGNATURE_DOMAIN: &[u8] = b"epiphany.host-incarnation.signature.v0\0"; +const HOST_IDENTITY_DOMAIN: &[u8] = b"epiphany.host-incarnation.identity.v0\0"; static RELEASE_AUTHORITY_SNAPSHOT_SEQUENCE: AtomicU64 = AtomicU64::new(0); +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +struct IdunnDaemonHealthWireV1 { + daemon_id: String, + state: String, + detail: String, + observed_at: String, + health_contract: String, + publication_source: String, + transport: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +struct EpiphanySignedRuntimeHealthWire { + schema_version: String, + health: IdunnDaemonHealthWireV1, + source_runtime_id: String, + release_id: String, + release_witness_sha256: String, + source_commit: String, + deployment_request_id: String, + publisher_incarnation_id: String, + publisher_sequence: u64, + publisher_process_id: u32, + publisher_process_creation_token: u64, + publisher_process_created_at: String, + publisher_executable_path: String, + signer_identity_id: String, + signature_algorithm: String, + signature: Vec, +} + +#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +struct EpiphanyHostIdentityWire { + schema_version: String, + identity_id: String, + public_key: Vec, + assurance: String, + identity_created_at: String, + source_identity_record_sha256: String, +} + #[derive(Clone, Debug)] struct DaemonTarget { daemon_id: String, @@ -404,6 +456,7 @@ struct CommonOptions { release_authority_store_path: Option, operator_alarm_command: Option, rudp_health_bind: Option, + trusted_epiphany_health_identity_store: Option, execute: bool, command_timeout_seconds: u64, } @@ -414,6 +467,16 @@ enum Mode { Swarm(SwarmOptions), LifecycleCommand(LifecycleCommandOptions), ReleaseAuthorityValidation(ReleaseAuthorityValidationOptions), + HealthAdmissionValidation(HealthAdmissionValidationOptions), +} + +#[derive(Clone, Debug)] +struct HealthAdmissionValidationOptions { + daemon_id: String, + deployment_request_id: String, + release_id: String, + release_witness_sha256: String, + source_commit: String, } #[derive(Clone, Debug)] @@ -473,9 +536,79 @@ fn main() -> Result<()> { Mode::ReleaseAuthorityValidation(validation) => { validate_release_authority_at_privileged_boundary(validation) } + Mode::HealthAdmissionValidation(validation) => { + validate_health_admission(validation, &options.common) + } } } +fn validate_health_admission( + expected: &HealthAdmissionValidationOptions, + options: &CommonOptions, +) -> Result<()> { + validate_health_admission_at(expected, options, ×tamp()?) +} + +fn validate_health_admission_at( + expected: &HealthAdmissionValidationOptions, + options: &CommonOptions, + now: &str, +) -> Result<()> { + let lock = Arc::new(Mutex::new(())); + let admission = with_store_node(options, &lock, |node| { + let admission = node + .get::(&expected.daemon_id)? + .ok_or_else(|| { + anyhow!( + "Idunn has no signed health admission for {}", + expected.daemon_id + ) + })?; + validate_admission_against_current_request(node, &admission)?; + let current_health = node + .get::(&expected.daemon_id)? + .ok_or_else(|| anyhow!("Idunn has no current health projection for admission"))?; + if current_health.state != admission.state + || current_health.observed_at != admission.observed_at + || current_health.health_contract != admission.health_contract + || current_health.publication_source != "daemon-published" + || current_health.transport != CULTNET_RUDP_PROTOCOL_ID + { + return Err(anyhow!( + "signed health admission is not the current daemon health observation" + )); + } + Ok(admission) + })?; + validate_admission_fresh_at(&admission, now, EPIPHANY_ADMISSION_MAX_AGE_SECONDS)?; + if admission.state != "active" + || admission.deployment_request_id != expected.deployment_request_id + || admission.release_id != expected.release_id + || admission.release_witness_sha256 != expected.release_witness_sha256 + || admission.source_commit != expected.source_commit + || admission.publisher_incarnation_id.is_empty() + || admission.publisher_sequence == 0 + || admission.signed_health_sha256.is_empty() + || admission.signer_identity_id.is_empty() + { + return Err(anyhow!( + "Idunn admitted health does not prove the exact active candidate" + )); + } + println!( + "validated signed Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", + admission.daemon_id, + admission.state, + admission.release_id, + admission.release_witness_sha256, + admission.source_commit, + admission.signed_health_sha256, + admission.publisher_incarnation_id, + admission.publisher_sequence, + ); + Ok(()) +} + fn validate_release_authority_at_privileged_boundary( options: &ReleaseAuthorityValidationOptions, ) -> Result<()> { @@ -678,9 +811,6 @@ fn run_target_cycle( node.put(&desired.daemon_id, &desired)?; node.put(&health_key, &health)?; node.put(&plan.decision.decision_id, &plan.decision)?; - if let Some(request) = &plan.deployment_request { - node.put(&request.request_id, request)?; - } if let Some(request) = &plan.restart_request { node.put(&request.request_id, request)?; } @@ -690,6 +820,10 @@ fn run_target_cycle( Ok(()) })?; + if let Some(request) = &plan.deployment_request { + persist_current_deployment_request(options, store_lock, request)?; + } + if let Some(request) = &plan.deployment_request { if options.execute { let authority_error = revalidate_deployment_request(request, options, store_lock).err(); @@ -959,9 +1093,9 @@ fn process_lifecycle_command( running.state = "running".to_string(); running.claimed_at = claimed_at.clone(); node.put(&running.command_id, &running)?; - node.put(&request.request_id, &request)?; Ok(()) })?; + persist_current_deployment_request(options, store_lock, &request)?; let result = if options.execute { run_deployment(&request, &claimed_at, options, store_lock) } else { @@ -1043,6 +1177,71 @@ where write(&mut node) } +fn persist_current_deployment_request( + options: &CommonOptions, + store_lock: &Arc>, + request: &IdunnDeploymentRequestRecord, +) -> Result { + for _ in 0..8 { + let (current_head, current_head_envelope, current_request_envelope) = + with_store_node(options, store_lock, |node| { + Ok(( + node.get::(&request.daemon_id)?, + node.cache() + .get_envelope::(&request.daemon_id)?, + node.cache() + .get_envelope::(&request.request_id)?, + )) + })?; + if let Some(existing) = current_request_envelope.as_ref() { + let decoded: IdunnDeploymentRequestRecord = rmp_serde::from_slice(&existing.payload)?; + if decoded != *request { + return Err(anyhow!("deployment request identity collision")); + } + if let Some(head) = current_head.as_ref() + && head.request_id == request.request_id + { + return Ok(head.clone()); + } + return Err(anyhow!( + "existing deployment request is not the current authority head" + )); + } + let head = IdunnCurrentDeploymentRequestRecord { + daemon_id: request.daemon_id.clone(), + request_id: request.request_id.clone(), + sequence: current_head + .as_ref() + .map_or(1, |head| head.sequence.saturating_add(1)), + updated_at: request.requested_at.clone(), + }; + let expected = [ + CultCacheExpectedEnvelope { + key: request.daemon_id.clone(), + r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), + current: current_head_envelope, + }, + CultCacheExpectedEnvelope { + key: request.request_id.clone(), + r#type: IdunnDeploymentRequestRecord::TYPE.into(), + current: None, + }, + ]; + if SingleFileMessagePackBackingStore::new(&options.store_path).compare_exchange( + &expected, + &[ + typed_envelope(&request.request_id, request, &request.requested_at)?, + typed_envelope(&head.daemon_id, &head, &request.requested_at)?, + ], + )? { + return Ok(head); + } + } + Err(anyhow!( + "deployment request head lost repeated cross-process contention" + )) +} + fn release_authority_id( repository_full_name: &str, upstream_ref: &str, @@ -1739,32 +1938,23 @@ fn run_rudp_health_ingress_loop( continue; } }; - match health_from_rudp_message(&message) { - Ok(mut health) => { - health.publication_source = "daemon-published".to_string(); - health.transport = CULTNET_RUDP_PROTOCOL_ID.to_string(); - if let Err(error) = - with_store_node(&options, &store_lock, |node| { - node.put(&health.daemon_id, &health)?; - Ok(()) - }) - { - eprintln!( - "Idunn RUDP health ingress failed to persist {} from {}: {}", - health.daemon_id, source, error - ); - } else { - let bounded_detail = - health.detail.replace('\r', " ").replace('\n', " "); - println!( - "Idunn accepted RUDP health for {} from {} over {} state={} detail={}", - health.daemon_id, - source, - frame.channel_id, - health.state, - bounded_detail - ); - } + match admit_health_from_rudp_message( + &message, + &options, + &store_lock, + &observed_at, + ) { + Ok(health) => { + let bounded_detail = + health.detail.replace('\r', " ").replace('\n', " "); + println!( + "Idunn accepted RUDP health for {} from {} over {} state={} detail={}", + health.daemon_id, + source, + frame.channel_id, + health.state, + bounded_detail + ); } Err(error) => { if trace_ingress { @@ -1911,20 +2101,133 @@ fn handle_rudp_health_datagram( Ok(frames) } -fn health_from_rudp_message(message: &CultNetMessage) -> Result { +fn admit_health_from_rudp_message( + message: &CultNetMessage, + options: &CommonOptions, + store_lock: &Arc>, + admitted_at: &str, +) -> Result { + let (health, admission) = decode_health_from_rudp_message(message, options, admitted_at)?; + let Some(admission) = admission else { + with_store_node(options, store_lock, |node| { + node.put(&health.daemon_id, &health)?; + Ok(()) + })?; + return Ok(health); + }; + for _ in 0..8 { + let (expected_health, expected_admission, expected_head, expected_request) = + with_store_node(options, store_lock, |node| { + validate_admission_against_current_request(node, &admission)?; + if let Some(existing) = + node.get::(&admission.daemon_id)? + && !signed_health_advances(&existing, &admission)? + { + return Err(anyhow!("signed Epiphany health is replayed or regressed")); + } + Ok(( + node.cache() + .get_envelope::(&health.daemon_id)?, + node.cache() + .get_envelope::(&admission.daemon_id)?, + node.cache() + .get_envelope::( + &admission.daemon_id, + )?, + node.cache().get_envelope::( + &admission.deployment_request_id, + )?, + )) + })?; + let health_envelope = typed_envelope(&health.daemon_id, &health, admitted_at)?; + let admission_envelope = typed_envelope(&admission.daemon_id, &admission, admitted_at)?; + let expected = [ + CultCacheExpectedEnvelope { + key: health.daemon_id.clone(), + r#type: IdunnDaemonHealthRecord::TYPE.into(), + current: expected_health, + }, + CultCacheExpectedEnvelope { + key: admission.daemon_id.clone(), + r#type: IdunnSignedHealthAdmissionRecord::TYPE.into(), + current: expected_admission, + }, + CultCacheExpectedEnvelope { + key: admission.daemon_id.clone(), + r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), + current: expected_head, + }, + CultCacheExpectedEnvelope { + key: admission.deployment_request_id.clone(), + r#type: IdunnDeploymentRequestRecord::TYPE.into(), + current: expected_request, + }, + ]; + if SingleFileMessagePackBackingStore::new(&options.store_path) + .compare_exchange(&expected, &[health_envelope, admission_envelope])? + { + return Ok(health); + } + } + Err(anyhow!( + "signed Epiphany health admission lost repeated cross-process contention" + )) +} + +fn typed_envelope( + key: &str, + value: &T, + stored_at: &str, +) -> Result { + Ok(CultCacheEnvelope { + key: key.to_string(), + r#type: T::TYPE.to_string(), + payload: rmp_serde::to_vec(value)?, + stored_at: stored_at.to_string(), + schema_id: Some(T::TYPE.to_string()), + }) +} + +#[cfg(test)] +fn health_from_rudp_message( + message: &CultNetMessage, + options: &CommonOptions, +) -> Result { + Ok(decode_health_from_rudp_message(message, options, ×tamp()?)?.0) +} + +fn decode_health_from_rudp_message( + message: &CultNetMessage, + options: &CommonOptions, + admitted_at: &str, +) -> Result<( + IdunnDaemonHealthRecord, + Option, +)> { let CultNetMessage::DocumentPutRaw { document, .. } = message else { return Err(anyhow!("expected cultnet.document_put_raw.v0")); }; - if document.schema_id != "idunn.daemon_health" { - return Err(anyhow!( - "expected idunn.daemon_health schema, received {}", - document.schema_id - )); - } if document.payload_encoding != CultNetRawPayloadEncoding::Messagepack { return Err(anyhow!("expected MessagePack raw payload encoding")); } - let health: IdunnDaemonHealthRecord = rmp_serde::from_slice(&document.payload)?; + if document.schema_id == EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE { + let (health, admission) = + authenticate_epiphany_signed_health(document, options, admitted_at)?; + return Ok((health, Some(admission))); + } + if document.schema_id != "idunn.daemon_health" { + return Err(anyhow!( + "expected signed Epiphany or idunn.daemon_health schema, received {}", + document.schema_id + )); + } + let wire: IdunnDaemonHealthWireV1 = rmp_serde::from_slice(&document.payload)?; + if wire.health_contract == EPIPHANY_HEALTH_CONTRACT { + return Err(anyhow!( + "Epiphany health requires its signed runtime-health schema" + )); + } + let health = legacy_health_record(wire); if document.record_key != health.daemon_id { return Err(anyhow!( "record key {} does not match health daemon_id {}", @@ -1932,7 +2235,259 @@ fn health_from_rudp_message(message: &CultNetMessage) -> Result Result<(IdunnDaemonHealthRecord, IdunnSignedHealthAdmissionRecord)> { + let signed: EpiphanySignedRuntimeHealthWire = rmp_serde::from_slice(&document.payload) + .context("decoding signed Epiphany runtime health")?; + validate_epiphany_signed_health_shape(&signed)?; + if document.record_key != signed.health.daemon_id + || document.source_runtime_id.as_deref() != Some(EPIPHANY_HEALTH_SOURCE_RUNTIME) + || document.source_role.as_deref() != Some("daemon-health-publisher") + { + return Err(anyhow!( + "signed Epiphany health transport identity is invalid" + )); + } + let identity_store = options + .trusted_epiphany_health_identity_store + .as_deref() + .ok_or_else(|| anyhow!("signed Epiphany health has no configured trust anchor"))?; + let identity = load_epiphany_health_identity(identity_store)?; + if signed.signer_identity_id != identity.identity_id { + return Err(anyhow!( + "signed Epiphany health signer is not the configured host identity" + )); + } + let key_bytes: [u8; 32] = identity + .public_key + .as_slice() + .try_into() + .map_err(|_| anyhow!("Epiphany health identity public key length is invalid"))?; + let signature_bytes: [u8; 64] = signed + .signature + .as_slice() + .try_into() + .map_err(|_| anyhow!("signed Epiphany health signature length is invalid"))?; + let mut unsigned = signed.clone(); + unsigned.signature.clear(); + let statement = rmp_serde::to_vec_named(&unsigned)?; + VerifyingKey::from_bytes(&key_bytes)? + .verify( + &host_signature_message(EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, &statement), + &Signature::from_bytes(&signature_bytes), + ) + .context("signed Epiphany health signature is invalid")?; + let digest = format!("sha256-{:x}", Sha256::digest(&document.payload)); + let health = IdunnDaemonHealthRecord { + daemon_id: signed.health.daemon_id, + state: signed.health.state, + detail: signed.health.detail, + observed_at: signed.health.observed_at, + health_contract: signed.health.health_contract, + publication_source: "daemon-published".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }; + let admission = IdunnSignedHealthAdmissionRecord { + daemon_id: health.daemon_id.clone(), + state: health.state.clone(), + observed_at: health.observed_at.clone(), + admitted_at: admitted_at.to_string(), + health_contract: health.health_contract.clone(), + deployment_request_id: signed.deployment_request_id, + release_id: signed.release_id, + release_witness_sha256: signed.release_witness_sha256, + source_commit: signed.source_commit, + publisher_incarnation_id: signed.publisher_incarnation_id, + publisher_sequence: signed.publisher_sequence, + publisher_process_created_at: signed.publisher_process_created_at, + signer_identity_id: signed.signer_identity_id, + signed_health_sha256: digest, + }; + Ok((health, admission)) +} + +fn validate_epiphany_signed_health_shape(signed: &EpiphanySignedRuntimeHealthWire) -> Result<()> { + if signed.schema_version != EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION + || signed.health.daemon_id != "yggdrasil-epiphany" + || signed.health.health_contract != EPIPHANY_HEALTH_CONTRACT + || signed.health.publication_source != "daemon-published" + || signed.health.transport != CULTNET_RUDP_PROTOCOL_ID + || !matches!( + signed.health.state.as_str(), + "active" | "warming" | "degraded" | "failed" + ) + || signed.source_runtime_id != EPIPHANY_HEALTH_SOURCE_RUNTIME + || signed.deployment_request_id.trim().is_empty() + || signed.release_id.trim().is_empty() + || signed.publisher_sequence == 0 + || signed.publisher_process_id == 0 + || signed.publisher_process_creation_token == 0 + || signed.publisher_executable_path.trim().is_empty() + || signed.signature_algorithm != "ed25519" + || signed.signature.len() != 64 + { + return Err(anyhow!("signed Epiphany health shape is invalid")); + } + uuid::Uuid::parse_str(&signed.publisher_incarnation_id) + .context("signed health publisher incarnation must be UUID")?; + chrono::DateTime::parse_from_rfc3339(&signed.health.observed_at)?; + chrono::DateTime::parse_from_rfc3339(&signed.publisher_process_created_at)?; + validate_sha256(&signed.release_witness_sha256, "release witness")?; + if signed.source_commit.len() != 40 + || !signed + .source_commit + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) + { + return Err(anyhow!("signed Epiphany health source commit is invalid")); + } + Ok(()) +} + +fn load_epiphany_health_identity(path: &std::path::Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all()?; + let envelope = entries + .iter() + .find(|entry| entry.r#type == HOST_IDENTITY_TYPE && entry.key == HOST_IDENTITY_KEY) + .ok_or_else(|| anyhow!("configured Epiphany health identity is absent"))?; + let identity: EpiphanyHostIdentityWire = rmp_serde::from_slice(&envelope.payload)?; + let expected_id = format!( + "{:x}", + Sha256::digest([HOST_IDENTITY_DOMAIN, identity.public_key.as_slice()].concat()) + ); + if identity.schema_version != HOST_IDENTITY_TYPE + || identity.identity_id != expected_id + || identity.public_key.len() != 32 + || identity.assurance.trim().is_empty() + || chrono::DateTime::parse_from_rfc3339(&identity.identity_created_at).is_err() + || validate_sha256(&identity.source_identity_record_sha256, "identity record").is_err() + { + return Err(anyhow!("configured Epiphany health identity is invalid")); + } + Ok(identity) +} + +fn validate_sha256(value: &str, label: &str) -> Result<()> { + let digest = value.strip_prefix("sha256-").unwrap_or(value); + if digest.len() != 64 + || !digest + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) + { + return Err(anyhow!("signed Epiphany health {label} is invalid")); + } + Ok(()) +} + +fn host_signature_message(purpose: &str, payload: &[u8]) -> Vec { + let mut message = + Vec::with_capacity(HOST_SIGNATURE_DOMAIN.len() + purpose.len() + payload.len() + 16); + message.extend_from_slice(HOST_SIGNATURE_DOMAIN); + message.extend_from_slice(&(purpose.len() as u64).to_be_bytes()); + message.extend_from_slice(purpose.as_bytes()); + message.extend_from_slice(&(payload.len() as u64).to_be_bytes()); + message.extend_from_slice(payload); + message +} + +fn signed_health_advances( + existing: &IdunnSignedHealthAdmissionRecord, + candidate: &IdunnSignedHealthAdmissionRecord, +) -> Result { + if existing.signer_identity_id != candidate.signer_identity_id { + return Ok(false); + } + if existing.publisher_incarnation_id == candidate.publisher_incarnation_id { + return Ok(candidate.publisher_sequence > existing.publisher_sequence); + } + let existing_created = + chrono::DateTime::parse_from_rfc3339(&existing.publisher_process_created_at)?; + let candidate_created = + chrono::DateTime::parse_from_rfc3339(&candidate.publisher_process_created_at)?; + Ok(candidate_created > existing_created) +} + +fn validate_admission_against_current_request( + node: &mut CultMeshNode, + admission: &IdunnSignedHealthAdmissionRecord, +) -> Result<()> { + let request = node + .get::(&admission.deployment_request_id)? + .ok_or_else(|| anyhow!("signed health names an unknown deployment request"))?; + if request.daemon_id != admission.daemon_id + || request.source_revision != admission.source_commit + || !request.requires_bifrost_authority + || request.repository_full_name != "GameCult/Epiphany" + || request.upstream_ref != "refs/heads/main" + || request.release_authority_id.trim().is_empty() + || request.release_authority_envelope_sha256.trim().is_empty() + { + return Err(anyhow!( + "signed health does not match an exact Bifrost-authorized Epiphany deployment request" + )); + } + let request_time = parse_timestamp_seconds(&request.requested_at) + .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; + let observed_time = parse_timestamp_seconds(&admission.observed_at) + .ok_or_else(|| anyhow!("signed health observation timestamp is invalid"))?; + let admitted_time = parse_timestamp_seconds(&admission.admitted_at) + .ok_or_else(|| anyhow!("signed health admission timestamp is invalid"))?; + if observed_time < request_time || admitted_time < request_time { + return Err(anyhow!( + "signed health predates the deployment request it claims" + )); + } + let current = node + .get::(&admission.daemon_id)? + .ok_or_else(|| anyhow!("Idunn has no current deployment request authority head"))?; + if current.request_id != request.request_id || current.sequence == 0 { + return Err(anyhow!( + "signed health is bound to a superseded deployment request" + )); + } + Ok(()) +} + +fn validate_admission_fresh_at( + admission: &IdunnSignedHealthAdmissionRecord, + now: &str, + max_age_seconds: u64, +) -> Result<()> { + let observed = parse_timestamp_seconds(&admission.observed_at) + .ok_or_else(|| anyhow!("signed health observation timestamp is invalid"))?; + let admitted = parse_timestamp_seconds(&admission.admitted_at) + .ok_or_else(|| anyhow!("signed health admission timestamp is invalid"))?; + let now = + parse_timestamp_seconds(now).ok_or_else(|| anyhow!("Idunn admission clock is invalid"))?; + if observed > admitted || admitted > now { + return Err(anyhow!( + "signed health admission carries a future or causally inverted timestamp" + )); + } + if now.saturating_sub(observed) > max_age_seconds + || now.saturating_sub(admitted) > max_age_seconds + { + return Err(anyhow!("signed health admission is stale")); + } + Ok(()) +} + +fn legacy_health_record(wire: IdunnDaemonHealthWireV1) -> IdunnDaemonHealthRecord { + IdunnDaemonHealthRecord { + daemon_id: wire.daemon_id, + state: wire.state, + detail: wire.detail, + observed_at: wire.observed_at, + health_contract: wire.health_contract, + publication_source: "daemon-published".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + } } fn publish_surgery_plans( @@ -3254,6 +3809,9 @@ impl Options { let release_authority_validation = args .first() .is_some_and(|arg| arg == "validate-release-authority"); + let health_admission_validation = args + .first() + .is_some_and(|arg| arg == "validate-health-admission"); let lifecycle_action = args.first().and_then(|arg| match arg.as_str() { "restart" | "request-restart" => Some(LifecycleAction::Restart), "redeploy" | "request-redeploy" | "deploy" | "request-deploy" => { @@ -3265,6 +3823,7 @@ impl Options { let mut release_authority_store_path = None; let mut operator_alarm_command = None; let mut rudp_health_bind = None; + let mut trusted_epiphany_health_identity_store = None; let mut execute = false; let mut command_timeout_seconds = 30; let mut daemon_id = None; @@ -3285,9 +3844,13 @@ impl Options { let mut validation_source_revision = None; let mut validation_authority_id = None; let mut validation_envelope_sha256 = None; + let mut validation_release_id = None; + let mut validation_release_witness_sha256 = None; + let mut validation_deployment_request_id = None; let mut args = args.into_iter().peekable(); - if lifecycle_action.is_some() || release_authority_validation { + if lifecycle_action.is_some() || release_authority_validation || health_admission_validation + { let _ = args.next(); } while let Some(arg) = args.next() { @@ -3316,6 +3879,17 @@ impl Options { validation_envelope_sha256 = Some(take_value(&mut args, "--release-authority-sha256")?) } + "--release-id" => { + validation_release_id = Some(take_value(&mut args, "--release-id")?) + } + "--release-witness-sha256" => { + validation_release_witness_sha256 = + Some(take_value(&mut args, "--release-witness-sha256")?) + } + "--deployment-request-id" => { + validation_deployment_request_id = + Some(take_value(&mut args, "--deployment-request-id")?) + } "--daemon" => daemon_id = Some(take_value(&mut args, "--daemon")?), "--verse" => verse_id = take_value(&mut args, "--verse")?, "--name" => name = Some(take_value(&mut args, "--name")?), @@ -3348,6 +3922,12 @@ impl Options { ) }; } + "--trusted-epiphany-health-identity-store" => { + trusted_epiphany_health_identity_store = Some(PathBuf::from(take_value( + &mut args, + "--trusted-epiphany-health-identity-store", + )?)); + } "--disabled" => enabled = false, "--execute" => execute = true, "--interval-seconds" => { @@ -3389,6 +3969,7 @@ impl Options { release_authority_store_path, operator_alarm_command, rudp_health_bind, + trusted_epiphany_health_identity_store, execute, command_timeout_seconds, }; @@ -3420,6 +4001,28 @@ impl Options { }); } + if health_admission_validation { + return Ok(Self { + common, + mode: Mode::HealthAdmissionValidation(HealthAdmissionValidationOptions { + daemon_id: daemon_id + .ok_or_else(|| anyhow!("health admission validation requires --daemon"))?, + deployment_request_id: validation_deployment_request_id.ok_or_else(|| { + anyhow!("health admission validation requires --deployment-request-id") + })?, + release_id: validation_release_id.ok_or_else(|| { + anyhow!("health admission validation requires --release-id") + })?, + release_witness_sha256: validation_release_witness_sha256.ok_or_else(|| { + anyhow!("health admission validation requires --release-witness-sha256") + })?, + source_commit: validation_source_revision.ok_or_else(|| { + anyhow!("health admission validation requires --source-revision") + })?, + }), + }); + } + if swarm_profile.as_deref() == Some("yggdrasil-local") && common.release_authority_store_path.is_none() { @@ -3427,6 +4030,13 @@ impl Options { "--release-authority-store is required for yggdrasil-local release targets" )); } + if swarm_profile.as_deref() == Some("yggdrasil-local") + && common.trusted_epiphany_health_identity_store.is_none() + { + return Err(anyhow!( + "--trusted-epiphany-health-identity-store is required for yggdrasil-local" + )); + } if let Some(action) = lifecycle_action { if swarm_profile.is_some() { @@ -3989,13 +4599,14 @@ fn unix_epoch_millis() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n\nIdunn supervises daemon-published CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises daemon-published CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. Epiphany health additionally requires a pinned host identity and a signed release-bound runtime-health document. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." } #[cfg(test)] mod tests { use super::*; use cultnet_rs::{CultNetRawDocumentRecord, CultNetRawPayloadEncoding}; + use ed25519_dalek::{Signer, SigningKey}; use std::cell::RefCell; const EPIPHANY_SHA: &str = "0123456789abcdef0123456789abcdef01234567"; @@ -4240,6 +4851,7 @@ mod tests { release_authority_store_path: Some(path.clone()), operator_alarm_command: None, rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, execute: true, command_timeout_seconds: 1, }; @@ -5173,7 +5785,10 @@ mod tests { assert!(unit.contains( "--release-authority-store /srv/bifrost/state/repository-release-authority.cc" )); - assert!(unit.contains("--command-timeout-seconds 3600")); + assert!(unit.contains("--command-timeout-seconds 21600")); + assert!(unit.contains( + "--trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp" + )); assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); assert!(actuator.contains("epiphany) target_requires_bifrost_authority=true")); @@ -5469,6 +6084,7 @@ mod tests { release_authority_store_path: None, operator_alarm_command: None, rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, execute: false, command_timeout_seconds: 1, }; @@ -5538,6 +6154,7 @@ mod tests { release_authority_store_path: None, operator_alarm_command: None, rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, execute: false, command_timeout_seconds: 1, }; @@ -6057,7 +6674,16 @@ mod tests { record_key: health.daemon_id.clone(), stored_at: "2026-06-15T00:00:00Z".to_string(), payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: rmp_serde::to_vec(&health).unwrap(), + payload: rmp_serde::to_vec(&IdunnDaemonHealthWireV1 { + daemon_id: health.daemon_id.clone(), + state: health.state.clone(), + detail: health.detail.clone(), + observed_at: health.observed_at.clone(), + health_contract: health.health_contract.clone(), + publication_source: health.publication_source.clone(), + transport: health.transport.clone(), + }) + .unwrap(), source_runtime_id: Some("test-daemon".to_string()), source_agent_id: None, source_role: Some("daemon-health-publisher".to_string()), @@ -6065,6 +6691,406 @@ mod tests { }, }; - assert_eq!(health_from_rudp_message(&message).unwrap(), health); + let options = CommonOptions { + store_path: PathBuf::from("unused.cc"), + release_authority_store_path: None, + operator_alarm_command: None, + rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, + execute: false, + command_timeout_seconds: 1, + }; + assert_eq!( + health_from_rudp_message(&message, &options).unwrap(), + health + ); + } + + fn signed_epiphany_health_fixture( + sequence: u64, + ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { + let root = tempfile::tempdir().unwrap(); + let identity_path = root.path().join("host.ccmp"); + let store_path = root.path().join("idunn.cc"); + let key = SigningKey::from_bytes(&[23; 32]); + let public_key = key.verifying_key().to_bytes().to_vec(); + let identity_id = format!( + "{:x}", + Sha256::digest([HOST_IDENTITY_DOMAIN, public_key.as_slice()].concat()) + ); + let identity = EpiphanyHostIdentityWire { + schema_version: HOST_IDENTITY_TYPE.into(), + identity_id: identity_id.clone(), + public_key, + assurance: "test".into(), + identity_created_at: "2026-07-16T00:00:00Z".into(), + source_identity_record_sha256: format!("sha256-{}", "c".repeat(64)), + }; + let mut backing = SingleFileMessagePackBackingStore::new(&identity_path); + backing + .push(&CultCacheEnvelope { + key: HOST_IDENTITY_KEY.into(), + r#type: HOST_IDENTITY_TYPE.into(), + payload: rmp_serde::to_vec(&identity).unwrap(), + stored_at: identity.identity_created_at.clone(), + schema_id: Some(HOST_IDENTITY_TYPE.into()), + }) + .unwrap(); + let mut signed = EpiphanySignedRuntimeHealthWire { + schema_version: EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION.into(), + health: IdunnDaemonHealthWireV1 { + daemon_id: "yggdrasil-epiphany".into(), + state: "warming".into(), + detail: "authenticated checkpoint progress".into(), + observed_at: "2026-07-16T00:01:00Z".into(), + health_contract: EPIPHANY_HEALTH_CONTRACT.into(), + publication_source: "daemon-published".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }, + source_runtime_id: EPIPHANY_HEALTH_SOURCE_RUNTIME.into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), + publisher_incarnation_id: "00000000-0000-4000-8000-000000000001".into(), + publisher_sequence: sequence, + publisher_process_id: 42, + publisher_process_creation_token: 7, + publisher_process_created_at: "2026-07-16T00:00:30Z".into(), + publisher_executable_path: "/srv/epiphany/app/current/epiphany-daemon-supervisor" + .into(), + signer_identity_id: identity_id, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + }; + let statement = rmp_serde::to_vec_named(&signed).unwrap(); + signed.signature = key + .sign(&host_signature_message( + EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, + &statement, + )) + .to_bytes() + .to_vec(); + let message = CultNetMessage::DocumentPutRaw { + message_id: format!("signed-health-{sequence}"), + document: CultNetRawDocumentRecord { + schema_id: EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE.into(), + record_key: signed.health.daemon_id.clone(), + stored_at: signed.health.observed_at.clone(), + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload: rmp_serde::to_vec_named(&signed).unwrap(), + source_runtime_id: Some(EPIPHANY_HEALTH_SOURCE_RUNTIME.into()), + source_agent_id: None, + source_role: Some("daemon-health-publisher".into()), + tags: Some(vec![CULTNET_RUDP_PROTOCOL_ID.into()]), + }, + }; + let options = CommonOptions { + store_path, + release_authority_store_path: None, + operator_alarm_command: None, + rudp_health_bind: None, + trusted_epiphany_health_identity_store: Some(identity_path), + execute: false, + command_timeout_seconds: 1, + }; + persist_current_deployment_request( + &options, + &Arc::new(Mutex::new(())), + &IdunnDeploymentRequestRecord { + request_id: "deploy:yggdrasil-epiphany:fixture".into(), + daemon_id: "yggdrasil-epiphany".into(), + command: "fixture".into(), + authority: "idunn-supervisor-command.manual".into(), + requested_at: "unix:1784160000".into(), + repository_full_name: "GameCult/Epiphany".into(), + upstream_ref: "refs/heads/main".into(), + source_revision: "b".repeat(40), + release_authority_id: "authority-fixture".into(), + release_authority_envelope_sha256: format!("sha256-{}", "d".repeat(64)), + requires_bifrost_authority: true, + }, + ) + .unwrap(); + (message, options, root) + } + + #[test] + fn signed_epiphany_health_requires_pinned_identity_and_advancing_sequence() { + let (message, options, _root) = signed_epiphany_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + let admitted = + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:01Z") + .unwrap(); + assert_eq!(admitted.state, "warming"); + validate_health_admission_at( + &HealthAdmissionValidationOptions { + daemon_id: "yggdrasil-epiphany".into(), + deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }, + &options, + "2026-07-16T00:01:02Z", + ) + .unwrap_err(); + let mut active_message = message.clone(); + let CultNetMessage::DocumentPutRaw { document, .. } = &mut active_message else { + unreachable!() + }; + let mut active: EpiphanySignedRuntimeHealthWire = + rmp_serde::from_slice(&document.payload).unwrap(); + active.health.state = "active".into(); + active.publisher_sequence = 2; + active.signature.clear(); + let key = SigningKey::from_bytes(&[23; 32]); + let statement = rmp_serde::to_vec_named(&active).unwrap(); + active.signature = key + .sign(&host_signature_message( + EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, + &statement, + )) + .to_bytes() + .to_vec(); + document.payload = rmp_serde::to_vec_named(&active).unwrap(); + admit_health_from_rudp_message(&active_message, &options, &lock, "2026-07-16T00:01:02Z") + .unwrap(); + validate_health_admission_at( + &HealthAdmissionValidationOptions { + daemon_id: "yggdrasil-epiphany".into(), + deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }, + &options, + "2026-07-16T00:01:03Z", + ) + .unwrap(); + assert!( + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:03Z",) + .is_err() + ); + + let mut newer = with_store_node(&options, &lock, |node| { + node.get::("deploy:yggdrasil-epiphany:fixture")? + .ok_or_else(|| anyhow!("fixture request missing")) + }) + .unwrap(); + newer.request_id = "deploy:yggdrasil-epiphany:aaa".into(); + newer.requested_at = "unix:1784160000".into(); + persist_current_deployment_request(&options, &lock, &newer).unwrap(); + let stale_error = validate_health_admission_at( + &HealthAdmissionValidationOptions { + daemon_id: "yggdrasil-epiphany".into(), + deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }, + &options, + "2026-07-16T00:02:00Z", + ) + .unwrap_err() + .to_string(); + assert!(stale_error.contains("superseded deployment request")); + assert!( + admit_health_from_rudp_message( + &active_message, + &options, + &lock, + "2026-07-16T00:02:00Z", + ) + .is_err() + ); + + let (mut alien, alien_options, _alien_root) = signed_epiphany_health_fixture(2); + if let CultNetMessage::DocumentPutRaw { document, .. } = &mut alien { + document.source_runtime_id = Some("alien-runtime".into()); + } + assert!(health_from_rudp_message(&alien, &alien_options).is_err()); + + let (mut forged, forged_options, _forged_root) = signed_epiphany_health_fixture(3); + let CultNetMessage::DocumentPutRaw { document, .. } = &mut forged else { + unreachable!() + }; + let mut forged_wire: EpiphanySignedRuntimeHealthWire = + rmp_serde::from_slice(&document.payload).unwrap(); + forged_wire.signature[0] ^= 1; + document.payload = rmp_serde::to_vec_named(&forged_wire).unwrap(); + assert!(health_from_rudp_message(&forged, &forged_options).is_err()); + } + + #[test] + fn unsigned_epiphany_health_is_rejected() { + let (mut message, options, _root) = signed_epiphany_health_fixture(1); + let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { + unreachable!() + }; + let signed: EpiphanySignedRuntimeHealthWire = + rmp_serde::from_slice(&document.payload).unwrap(); + document.schema_id = "idunn.daemon_health".into(); + document.payload = rmp_serde::to_vec(&signed.health).unwrap(); + assert!(health_from_rudp_message(&message, &options).is_err()); + } + + #[test] + fn signed_admission_must_remain_the_current_health_observation() { + let (message, options, _root) = signed_epiphany_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:01Z").unwrap(); + with_store_node(&options, &lock, |node| { + let mut health = node + .get::("yggdrasil-epiphany")? + .unwrap(); + health.state = "failed".into(); + health.observed_at = "2026-07-16T00:01:02Z".into(); + node.put(&health.daemon_id, &health)?; + Ok(()) + }) + .unwrap(); + let error = validate_health_admission_at( + &HealthAdmissionValidationOptions { + daemon_id: "yggdrasil-epiphany".into(), + deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }, + &options, + "2026-07-16T00:01:03Z", + ) + .unwrap_err() + .to_string(); + assert!(error.contains("not the current daemon health observation")); + } + + #[test] + fn generic_health_cannot_substitute_for_signed_admission() { + let root = tempfile::tempdir().unwrap(); + let options = CommonOptions { + store_path: root.path().join("idunn.cc"), + release_authority_store_path: None, + operator_alarm_command: None, + rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, + execute: false, + command_timeout_seconds: 1, + }; + let lock = Arc::new(Mutex::new(())); + with_store_node(&options, &lock, |node| { + node.put( + "yggdrasil-epiphany", + &IdunnDaemonHealthRecord { + daemon_id: "yggdrasil-epiphany".into(), + state: "active".into(), + detail: "counterfeit generic observation".into(), + observed_at: "2026-07-16T00:01:00Z".into(), + health_contract: EPIPHANY_HEALTH_CONTRACT.into(), + publication_source: "daemon-published".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }, + )?; + Ok(()) + }) + .unwrap(); + let error = validate_health_admission( + &HealthAdmissionValidationOptions { + daemon_id: "yggdrasil-epiphany".into(), + deployment_request_id: "counterfeit".into(), + release_id: "release-test".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }, + &options, + ) + .unwrap_err() + .to_string(); + assert!(error.contains("no signed health admission")); + } + + #[test] + fn signed_admission_freshness_rejects_stale_future_and_inverted_time() { + let mut admission = IdunnSignedHealthAdmissionRecord { + daemon_id: "yggdrasil-epiphany".into(), + state: "active".into(), + observed_at: "unix:100".into(), + admitted_at: "unix:101".into(), + health_contract: EPIPHANY_HEALTH_CONTRACT.into(), + deployment_request_id: "request".into(), + release_id: "release".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + publisher_incarnation_id: "00000000-0000-4000-8000-000000000001".into(), + publisher_sequence: 1, + publisher_process_created_at: "2026-07-16T00:00:00Z".into(), + signer_identity_id: "signer".into(), + signed_health_sha256: format!("sha256-{}", "c".repeat(64)), + }; + validate_admission_fresh_at(&admission, "unix:280", 180).unwrap(); + assert!(validate_admission_fresh_at(&admission, "unix:282", 180).is_err()); + admission.observed_at = "unix:102".into(); + assert!(validate_admission_fresh_at(&admission, "unix:102", 180).is_err()); + admission.observed_at = "unix:100".into(); + admission.admitted_at = "unix:103".into(); + assert!(validate_admission_fresh_at(&admission, "unix:102", 180).is_err()); + } + + #[test] + fn concurrent_signed_health_writers_cannot_regress_sequence() { + let (message_one, options, _root) = signed_epiphany_health_fixture(1); + let (mut message_two, _, _) = signed_epiphany_health_fixture(2); + // Re-sign sequence two with the trust anchor belonging to the shared fixture. + let CultNetMessage::DocumentPutRaw { document, .. } = &mut message_two else { + unreachable!() + }; + let mut signed: EpiphanySignedRuntimeHealthWire = + rmp_serde::from_slice(&document.payload).unwrap(); + signed.signer_identity_id = match &message_one { + CultNetMessage::DocumentPutRaw { document, .. } => { + let first: EpiphanySignedRuntimeHealthWire = + rmp_serde::from_slice(&document.payload).unwrap(); + first.signer_identity_id + } + _ => unreachable!(), + }; + signed.signature.clear(); + let key = SigningKey::from_bytes(&[23; 32]); + let statement = rmp_serde::to_vec_named(&signed).unwrap(); + signed.signature = key + .sign(&host_signature_message( + EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, + &statement, + )) + .to_bytes() + .to_vec(); + document.payload = rmp_serde::to_vec_named(&signed).unwrap(); + + let barrier = Arc::new(std::sync::Barrier::new(3)); + let mut workers = Vec::new(); + for message in [message_one, message_two] { + let options = options.clone(); + let barrier = barrier.clone(); + workers.push(std::thread::spawn(move || { + barrier.wait(); + admit_health_from_rudp_message( + &message, + &options, + &Arc::new(Mutex::new(())), + "2026-07-16T00:01:01Z", + ) + })); + } + barrier.wait(); + for worker in workers { + let _ = worker.join().unwrap(); + } + let final_admission = with_store_node(&options, &Arc::new(Mutex::new(())), |node| { + node.get::("yggdrasil-epiphany")? + .ok_or_else(|| anyhow!("final admission missing")) + }) + .unwrap(); + assert_eq!(final_admission.publisher_sequence, 2); } } diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 5f44a65..d8df9ea 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -13,10 +13,12 @@ pub const EVE_PROVIDER_ADVERTISEMENT_SCHEMA: &str = "gamecult.eve.provider_adver pub const VOIDBOT_SWARM_STATE_SNAPSHOT_SCHEMA: &str = "voidbot.swarm_state_snapshot.v1"; pub const IDUNN_DESIRED_DAEMON_SCHEMA: &str = "idunn.desired_daemon.v1"; pub const IDUNN_DAEMON_HEALTH_SCHEMA: &str = "idunn.daemon_health.v1"; +pub const IDUNN_SIGNED_HEALTH_ADMISSION_SCHEMA: &str = "idunn.signed_health_admission.v1"; pub const IDUNN_KEEPALIVE_DECISION_SCHEMA: &str = "idunn.keepalive_decision.v1"; pub const IDUNN_RESTART_REQUEST_SCHEMA: &str = "idunn.restart_request.v1"; pub const IDUNN_RESTART_RESULT_SCHEMA: &str = "idunn.restart_result.v1"; pub const IDUNN_DEPLOYMENT_REQUEST_SCHEMA: &str = "idunn.deployment_request.v2"; +pub const IDUNN_CURRENT_DEPLOYMENT_REQUEST_SCHEMA: &str = "idunn.current_deployment_request.v1"; pub const IDUNN_DEPLOYMENT_RESULT_SCHEMA: &str = "idunn.deployment_result.v1"; pub const IDUNN_LIFECYCLE_COMMAND_SCHEMA: &str = "idunn.lifecycle_command.v1"; pub const IDUNN_RELEASE_TARGET_SCHEMA: &str = "idunn.release_target.v2"; @@ -49,7 +51,8 @@ pub const MUNINN_MOVE_IDENTITY_SCHEMA: &str = "muninn.move_identity.v1"; pub const MUNINN_MOVE_LIGHT_COMMAND_SCHEMA: &str = "muninn.move_light_command.v1"; pub const MUNINN_MOVE_HUE_PROGRAM_SCHEMA: &str = "muninn.move_hue_program.v1"; pub const MUNINN_MOVE_TRACKER_HEALTH_SCHEMA: &str = "muninn.move_tracker_health.v1"; -pub const MUNINN_MOVE_EVIDENCE_TRANSPORT_HEALTH_SCHEMA: &str = "muninn.move_evidence_transport_health.v1"; +pub const MUNINN_MOVE_EVIDENCE_TRANSPORT_HEALTH_SCHEMA: &str = + "muninn.move_evidence_transport_health.v1"; pub const MUNINN_QUEST_ACCESS_SCHEMA: &str = "muninn.quest_access.v1"; pub const MUNINN_COMMAND_BOUNDARY_SCHEMA: &str = "muninn.command_boundary.v1"; pub const MUNINN_TRANSPORT_PROFILE_SCHEMA: &str = "muninn.transport_profile.v1"; @@ -270,6 +273,42 @@ pub struct IdunnDaemonHealthRecord { pub transport: String, } +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.signed_health_admission", + schema = "idunn.signed_health_admission.v1" +)] +pub struct IdunnSignedHealthAdmissionRecord { + #[cultcache(key = 0)] + pub daemon_id: String, + #[cultcache(key = 1)] + pub state: String, + #[cultcache(key = 2)] + pub observed_at: String, + #[cultcache(key = 3)] + pub admitted_at: String, + #[cultcache(key = 4)] + pub health_contract: String, + #[cultcache(key = 5)] + pub deployment_request_id: String, + #[cultcache(key = 6)] + pub release_id: String, + #[cultcache(key = 7)] + pub release_witness_sha256: String, + #[cultcache(key = 8)] + pub source_commit: String, + #[cultcache(key = 9)] + pub publisher_incarnation_id: String, + #[cultcache(key = 10)] + pub publisher_sequence: u64, + #[cultcache(key = 11)] + pub publisher_process_created_at: String, + #[cultcache(key = 12)] + pub signer_identity_id: String, + #[cultcache(key = 13)] + pub signed_health_sha256: String, +} + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.keepalive_decision", @@ -352,6 +391,22 @@ pub struct IdunnDeploymentRequestRecord { pub requires_bifrost_authority: bool, } +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.current_deployment_request", + schema = "idunn.current_deployment_request.v1" +)] +pub struct IdunnCurrentDeploymentRequestRecord { + #[cultcache(key = 0)] + pub daemon_id: String, + #[cultcache(key = 1)] + pub request_id: String, + #[cultcache(key = 2)] + pub sequence: u64, + #[cultcache(key = 3)] + pub updated_at: String, +} + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.deployment_result", @@ -1286,46 +1341,85 @@ pub struct MuninnMoveHueProgramRecord { } #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "muninn.move_tracker_health", schema = "muninn.move_tracker_health.v1")] +#[cultcache( + type = "muninn.move_tracker_health", + schema = "muninn.move_tracker_health.v1" +)] pub struct MuninnMoveTrackerHealthRecord { - #[cultcache(key = 0)] pub health_id: String, - #[cultcache(key = 1)] pub host_id: String, - #[cultcache(key = 2)] pub camera_id: String, - #[cultcache(key = 3)] pub camera_index: i32, - #[cultcache(key = 4)] pub state: String, - #[cultcache(key = 5)] pub camera_name: String, - #[cultcache(key = 6)] pub camera_api: String, - #[cultcache(key = 7)] pub width: u32, - #[cultcache(key = 8)] pub height: u32, - #[cultcache(key = 9)] pub exposure: f32, - #[cultcache(key = 10)] pub calibrated_controller_count: u32, - #[cultcache(key = 11)] pub update_count: u64, - #[cultcache(key = 12)] pub observation_count: u64, - #[cultcache(key = 13)] pub latest_observation_count: u32, - #[cultcache(key = 14)] pub last_observation_at: String, - #[cultcache(key = 15)] pub detail: String, - #[cultcache(key = 16)] pub updated_at: String, - #[cultcache(key = 17, default)] pub image_mean_rgb: Vec, - #[cultcache(key = 18, default)] pub image_peak_rgb: Vec, - #[cultcache(key = 19, default)] pub color_evidence_move_ids: Vec, - #[cultcache(key = 20, default)] pub color_evidence_pixel_counts: Vec, - #[cultcache(key = 21, default)] pub rejected_stale_count: u64, - #[cultcache(key = 22, default)] pub rejected_radius_count: u64, - #[cultcache(key = 23, default)] pub rejected_bounds_count: u64, - #[cultcache(key = 24, default)] pub rejected_continuity_count: u64, + #[cultcache(key = 0)] + pub health_id: String, + #[cultcache(key = 1)] + pub host_id: String, + #[cultcache(key = 2)] + pub camera_id: String, + #[cultcache(key = 3)] + pub camera_index: i32, + #[cultcache(key = 4)] + pub state: String, + #[cultcache(key = 5)] + pub camera_name: String, + #[cultcache(key = 6)] + pub camera_api: String, + #[cultcache(key = 7)] + pub width: u32, + #[cultcache(key = 8)] + pub height: u32, + #[cultcache(key = 9)] + pub exposure: f32, + #[cultcache(key = 10)] + pub calibrated_controller_count: u32, + #[cultcache(key = 11)] + pub update_count: u64, + #[cultcache(key = 12)] + pub observation_count: u64, + #[cultcache(key = 13)] + pub latest_observation_count: u32, + #[cultcache(key = 14)] + pub last_observation_at: String, + #[cultcache(key = 15)] + pub detail: String, + #[cultcache(key = 16)] + pub updated_at: String, + #[cultcache(key = 17, default)] + pub image_mean_rgb: Vec, + #[cultcache(key = 18, default)] + pub image_peak_rgb: Vec, + #[cultcache(key = 19, default)] + pub color_evidence_move_ids: Vec, + #[cultcache(key = 20, default)] + pub color_evidence_pixel_counts: Vec, + #[cultcache(key = 21, default)] + pub rejected_stale_count: u64, + #[cultcache(key = 22, default)] + pub rejected_radius_count: u64, + #[cultcache(key = 23, default)] + pub rejected_bounds_count: u64, + #[cultcache(key = 24, default)] + pub rejected_continuity_count: u64, } #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "muninn.move_evidence_transport_health", schema = "muninn.move_evidence_transport_health.v1")] +#[cultcache( + type = "muninn.move_evidence_transport_health", + schema = "muninn.move_evidence_transport_health.v1" +)] pub struct MuninnMoveEvidenceTransportHealthRecord { - #[cultcache(key = 0)] pub health_id: String, - #[cultcache(key = 1)] pub host_id: String, - #[cultcache(key = 2)] pub stream_id: String, - #[cultcache(key = 3)] pub produced_frames: u64, - #[cultcache(key = 4)] pub local_ring_admissions: u64, - #[cultcache(key = 5)] pub remote_handoffs: u64, - #[cultcache(key = 6)] pub remote_sends: u64, - #[cultcache(key = 7)] pub updated_at: String, + #[cultcache(key = 0)] + pub health_id: String, + #[cultcache(key = 1)] + pub host_id: String, + #[cultcache(key = 2)] + pub stream_id: String, + #[cultcache(key = 3)] + pub produced_frames: u64, + #[cultcache(key = 4)] + pub local_ring_admissions: u64, + #[cultcache(key = 5)] + pub remote_handoffs: u64, + #[cultcache(key = 6)] + pub remote_sends: u64, + #[cultcache(key = 7)] + pub updated_at: String, } #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] @@ -1421,10 +1515,12 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { VoidBotSwarmStateSnapshotCompatRecord => VOIDBOT_SWARM_STATE_SNAPSHOT_SCHEMA, IdunnDesiredDaemonRecord => IDUNN_DESIRED_DAEMON_SCHEMA, IdunnDaemonHealthRecord => IDUNN_DAEMON_HEALTH_SCHEMA, + IdunnSignedHealthAdmissionRecord => IDUNN_SIGNED_HEALTH_ADMISSION_SCHEMA, IdunnKeepaliveDecisionRecord => IDUNN_KEEPALIVE_DECISION_SCHEMA, IdunnRestartRequestRecord => IDUNN_RESTART_REQUEST_SCHEMA, IdunnRestartResultRecord => IDUNN_RESTART_RESULT_SCHEMA, IdunnDeploymentRequestRecord => IDUNN_DEPLOYMENT_REQUEST_SCHEMA, + IdunnCurrentDeploymentRequestRecord => IDUNN_CURRENT_DEPLOYMENT_REQUEST_SCHEMA, IdunnDeploymentResultRecord => IDUNN_DEPLOYMENT_RESULT_SCHEMA, IdunnLifecycleCommandRecord => IDUNN_LIFECYCLE_COMMAND_SCHEMA, IdunnReleaseTargetRecord => IDUNN_RELEASE_TARGET_SCHEMA, diff --git a/crates/odin-core/src/idunn.rs b/crates/odin-core/src/idunn.rs index bde0756..79ca417 100644 --- a/crates/odin-core/src/idunn.rs +++ b/crates/odin-core/src/idunn.rs @@ -257,11 +257,7 @@ mod tests { fn warming_daemon_is_observed_without_actuation_or_promotion() { let mut desired = desired(Some("systemctl restart epiphany")); desired.deploy_command = Some("deploy epiphany".to_string()); - let plan = plan_keepalive( - &desired, - &health("warming"), - "2026-07-17T00:00:02Z", - ); + let plan = plan_keepalive(&desired, &health("warming"), "2026-07-17T00:00:02Z"); assert_eq!(plan.decision.action, "observe"); assert!(plan.decision.reason.contains("provider-owned progress")); diff --git a/docs/idunn.md b/docs/idunn.md index 20a0faa..8e339e7 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -110,8 +110,10 @@ The current typed records are: ```text idunn.desired_daemon.v1 idunn.daemon_health.v1 +idunn.signed_health_admission.v1 idunn.keepalive_decision.v1 -idunn.deployment_request.v1 +idunn.deployment_request.v2 +idunn.current_deployment_request.v1 idunn.deployment_result.v1 idunn.release_target.v1 idunn.deployment_artifact.v1 @@ -184,9 +186,12 @@ idunn.rudp_health_ingress.v1 and then publishes `idunn.rudp_health_ingress.v1`. The Starfire local supervisor binds `0.0.0.0:17870` explicitly so host-local and WireGuard publishers can use the configured Idunn health endpoint. That ingress - accepts only raw `idunn.daemon_health` CultNet document puts on the `schema` - channel, decodes the typed MessagePack payload, and writes it into the - keepalive store. Each one-shot publisher gets its own RUDP session from its + accepts raw `idunn.daemon_health` CultNet document puts and the dedicated + signed Epiphany runtime-health schema on the `schema` channel. Generic health + is observation-only. Epiphany promotion requires the separately persisted + `idunn.signed_health_admission.v1`, an exact latest + `idunn.deployment_request.v2`, a pinned host signature, current matching + health, and a fresh active admission. Each one-shot publisher gets its own RUDP session from its UDP source address and the session is discarded after a delivered health frame. Windows UDP `ConnectionReset`/`ConnectionAborted` reports from closed one-shot clients are nonfatal ingress noise, not a reason to kill the worker. @@ -285,7 +290,7 @@ idunn.rudp_health_ingress.v1 execution. Operators publish `idunn.lifecycle_command.v1` with `idunn restart --daemon ` or `idunn redeploy --daemon `. The running supervisor claims pending records for its target catalog, writes the corresponding - `idunn.restart_request.v1` or `idunn.deployment_request.v1`, executes through + `idunn.restart_request.v1` or `idunn.deployment_request.v2`, executes through the same guarded actuator path used by health-driven decisions, and records the matching result document. If a target lacks restart or deploy authority, Idunn marks the lifecycle command rejected instead of inventing authority. diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index 65ae8ad..d17ad1c 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -8,7 +8,9 @@ Requires=docker.service Type=simple User=idunn Group=idunn -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --rudp-health-bind 10.77.0.1:17870 --command-timeout-seconds 3600 --execute +# Operational cancellation ceiling only. Target daemons own readiness through +# typed health; this duration must never be interpreted as deployment success. +ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute Restart=always RestartSec=5 TimeoutStopSec=20 diff --git a/src/Idunn/README.md b/src/Idunn/README.md index ed92f7b..0685d63 100644 --- a/src/Idunn/README.md +++ b/src/Idunn/README.md @@ -186,8 +186,10 @@ external-owned. - `idunn.desired_daemon.v1` - `idunn.daemon_health.v1` +- `idunn.signed_health_admission.v1` - `idunn.keepalive_decision.v1` -- `idunn.deployment_request.v1` +- `idunn.deployment_request.v2` +- `idunn.current_deployment_request.v1` - `idunn.deployment_result.v1` - `idunn.release_target.v1` - `idunn.deployment_artifact.v1` @@ -244,6 +246,11 @@ publication, not restart/deploy authority. On Windows, UDP reset reports from closed one-shot publishers are treated as nonfatal ingress noise so one accepted health frame cannot kill the listener. +Epiphany uses a separate signed runtime-health schema. Idunn joins that document +to the exact latest Bifrost-authorized deployment request and persists +`idunn.signed_health_admission.v1`. The privileged admission verifier reads only +that fresh, current, request-bound record; generic health remains observation. + When a fresh daemon-published RUDP health record exists for a target, Idunn uses that record for keepalive planning. The record must match the daemon id, health contract, RUDP transport, and freshness window. If it is absent or stale, Idunn diff --git a/vendor/cultcache-rs/Cargo.toml b/vendor/cultcache-rs/Cargo.toml index b11a909..17ba246 100644 --- a/vendor/cultcache-rs/Cargo.toml +++ b/vendor/cultcache-rs/Cargo.toml @@ -22,6 +22,9 @@ serde_bytes = "0.11" serde_json = "1" uuid = { version = "1", features = ["v4"] } +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Storage_FileSystem"] } + [dev-dependencies] pretty_assertions = "1.4" tempfile = "3" diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs index 7e654b9..dfaef5c 100644 --- a/vendor/cultcache-rs/src/lib.rs +++ b/vendor/cultcache-rs/src/lib.rs @@ -8,6 +8,7 @@ use std::collections::BTreeSet; use std::fs; use std::fs::File; use std::fs::OpenOptions; +use std::io::Write; use std::path::Path; use std::path::PathBuf; @@ -41,7 +42,7 @@ macro_rules! cultcache_registry { }; } -#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "camelCase")] pub struct CultCacheEnvelope { pub key: String, @@ -54,6 +55,13 @@ pub struct CultCacheEnvelope { pub schema_id: Option, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultCacheExpectedEnvelope { + pub key: String, + pub r#type: String, + pub current: Option, +} + #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] struct PersistedStoreSnapshot( String, @@ -128,6 +136,49 @@ impl SingleFileMessagePackBackingStore { &self.path } + pub fn compare_exchange( + &self, + expected: &[CultCacheExpectedEnvelope], + replacements: &[CultCacheEnvelope], + ) -> Result { + self.with_exclusive_lock(|| { + let mut entries = self.read_all_unlocked()?; + for condition in expected { + let current = entries + .iter() + .find(|entry| entry.r#type == condition.r#type && entry.key == condition.key); + if current != condition.current.as_ref() { + return Ok(false); + } + if let Some(envelope) = condition.current.as_ref() + && (envelope.r#type != condition.r#type || envelope.key != condition.key) + { + return Err(anyhow!( + "CultCache compare-exchange expectation identity differs from its envelope" + )); + } + } + let mut replacement_ids = BTreeSet::new(); + for replacement in replacements { + if replacement.key.trim().is_empty() || replacement.r#type.trim().is_empty() { + return Err(anyhow!( + "CultCache compare-exchange replacement identity is empty" + )); + } + if !replacement_ids.insert(entry_id(replacement)) { + return Err(anyhow!( + "CultCache compare-exchange contains a duplicate replacement" + )); + } + } + entries.retain(|entry| !replacement_ids.contains(&entry_id(entry))); + entries.extend_from_slice(replacements); + entries.sort_by_key(entry_id); + self.write_all_unlocked(&entries)?; + Ok(true) + }) + } + fn read_all_unlocked(&self) -> Result> { if !self.path.exists() { return Ok(Vec::new()); @@ -150,19 +201,25 @@ impl SingleFileMessagePackBackingStore { let bytes = rmp_serde::to_vec(&encode_store_snapshot(entries)) .context("failed to encode MessagePack")?; let tmp_path = temporary_path_for(&self.path); - fs::write(&tmp_path, bytes) + let mut temporary = OpenOptions::new() + .create_new(true) + .write(true) + .open(&tmp_path) + .with_context(|| format!("failed to create {}", tmp_path.display()))?; + temporary + .write_all(&bytes) .with_context(|| format!("failed to write {}", tmp_path.display()))?; - if self.path.exists() { - fs::remove_file(&self.path) - .with_context(|| format!("failed to replace {}", self.path.display()))?; + temporary + .sync_all() + .with_context(|| format!("failed to sync {}", tmp_path.display()))?; + drop(temporary); + replace_file_atomically(&tmp_path, &self.path)?; + #[cfg(unix)] + if let Some(parent) = self.path.parent() { + File::open(parent) + .and_then(|directory| directory.sync_all()) + .with_context(|| format!("failed to sync {}", parent.display()))?; } - fs::rename(&tmp_path, &self.path).with_context(|| { - format!( - "failed to move {} to {}", - tmp_path.display(), - self.path.display() - ) - })?; Ok(()) } @@ -212,6 +269,42 @@ impl SingleFileMessagePackBackingStore { } } +#[cfg(unix)] +fn replace_file_atomically(source: &Path, destination: &Path) -> Result<()> { + fs::rename(source, destination).with_context(|| { + format!( + "failed to atomically replace {} with {}", + destination.display(), + source.display() + ) + }) +} + +#[cfg(windows)] +fn replace_file_atomically(source: &Path, destination: &Path) -> Result<()> { + use std::os::windows::ffi::OsStrExt; + use windows_sys::Win32::Storage::FileSystem::{ + MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW, + }; + let source: Vec = source.as_os_str().encode_wide().chain(Some(0)).collect(); + let destination: Vec = destination + .as_os_str() + .encode_wide() + .chain(Some(0)) + .collect(); + let result = unsafe { + MoveFileExW( + source.as_ptr(), + destination.as_ptr(), + MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH, + ) + }; + if result == 0 { + return Err(std::io::Error::last_os_error()).context("atomic CultCache replacement failed"); + } + Ok(()) +} + impl CacheBackingStore for SingleFileMessagePackBackingStore { fn pull_all(&self) -> Result> { self.with_shared_lock(|| self.read_all_unlocked()) From 67e752c562aefa63aa542b339e14527dd26d3034 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 18 Jul 2026 20:49:16 +0100 Subject: [PATCH 04/93] Move Bifrost Persona feedback deployment onto Yggdrasil --- crates/idunn-daemon/src/main.rs | 85 ++++++++++++++++++++++++++++++++- scripts/linux/idunn-yggdrasil | 4 +- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 32ea640..f1797d0 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -3481,6 +3481,29 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { enabled: true, interval_seconds: 300, }, + DaemonTarget { + daemon_id: "yggdrasil-bifrost-persona-feedback".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Bifrost Persona feedback".to_string(), + health_contract: health_contract( + "bifrost.cultnet-rudp-persona-feedback-health", + "stale-deployment", + ), + deploy_command: Some(yggdrasil_actuator("deploy", "bifrost-persona-feedback")), + restart_command: None, + release: Some(requiring_bifrost_authority(with_deployed_revision_witness( + release_target( + "Bifrost", + PathBuf::from("/srv/build/Bifrost-persona-feedback"), + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/bifrost/persona-feedback/runtime/deployment.env"), + ))), + enabled: true, + interval_seconds: 30, + }, DaemonTarget { daemon_id: "yggdrasil-repixelizer".to_string(), verse_id: "yggdrasil.local".to_string(), @@ -5766,7 +5789,12 @@ mod tests { for legacy in yggdrasil .iter() - .filter(|target| target.daemon_id != "yggdrasil-epiphany") + .filter(|target| { + !matches!( + target.daemon_id.as_str(), + "yggdrasil-epiphany" | "yggdrasil-bifrost-persona-feedback" + ) + }) .filter_map(|target| target.release.as_ref()) { assert!( @@ -5791,7 +5819,9 @@ mod tests { )); assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); - assert!(actuator.contains("epiphany) target_requires_bifrost_authority=true")); + assert!(actuator.contains( + "epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true" + )); assert!(actuator.contains( "voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false" )); @@ -6017,6 +6047,57 @@ mod tests { } } + #[test] + fn bifrost_persona_feedback_target_uses_daemon_health_and_idunn_deployment() { + let options = SwarmOptions { + profile: "yggdrasil-local".to_string(), + repo_root: PathBuf::from("/srv/odin/source"), + }; + let targets = swarm_targets(&options).expect("yggdrasil-local targets"); + let target = targets + .iter() + .find(|target| target.daemon_id == "yggdrasil-bifrost-persona-feedback") + .expect("Bifrost Persona-feedback target"); + assert_eq!( + target.health_contract.id, + "bifrost.cultnet-rudp-persona-feedback-health" + ); + assert_eq!( + target.deploy_command.as_deref(), + Some("sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"") + ); + assert!(target.restart_command.is_none()); + let release = target.release.as_ref().expect("release target"); + assert!(release.requires_bifrost_authority); + assert_eq!(release.repository_full_name, "GameCult/Bifrost"); + assert_eq!(release.repo_path, PathBuf::from("/srv/build/Bifrost-persona-feedback")); + + let desired = IdunnDesiredDaemonRecord { + daemon_id: target.daemon_id.clone(), + verse_id: target.verse_id.clone(), + name: target.name.clone(), + enabled: true, + health_command: None, + restart_command: None, + deploy_command: target.deploy_command.clone(), + health_contract: target.health_contract.id.clone(), + transport_profile_id: transport_profile_id(target), + command_boundary_id: command_boundary_id(target), + authority: "idunn-supervisor-command".to_string(), + max_silence_seconds: 60, + observed_at: "unix:100".to_string(), + }; + let health = missing_daemon_published_health(target, &desired, "unix:101"); + assert_eq!(health.state, "stale-deployment"); + let plan = plan_keepalive(&desired, &health, "unix:102"); + assert_eq!(plan.decision.action, "deploy"); + assert_eq!( + plan.deployment_request.expect("deployment request").command, + target.deploy_command.clone().expect("deploy command") + ); + assert!(plan.restart_request.is_none()); + } + #[test] fn fresh_daemon_published_rudp_health_is_the_health_owner() { let desired = IdunnDesiredDaemonRecord { diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index 0e2a4ef..09ae681 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -19,7 +19,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in - epiphany) target_requires_bifrost_authority=true ;; + epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true ;; voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac @@ -88,7 +88,7 @@ case "$action:$target" in IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" \ exec "$manifest" "$action" ;; - deploy:heimdall|deploy:repixelizer|deploy:streampixels) + deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 From b83944da8daa576b4a88fbb0fb1ec53f99aa427a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 18 Jul 2026 21:22:50 +0100 Subject: [PATCH 05/93] Preserve Bifrost deployment admission semantics --- crates/idunn-daemon/src/main.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index f1797d0..ff1f77b 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -6088,13 +6088,10 @@ mod tests { observed_at: "unix:100".to_string(), }; let health = missing_daemon_published_health(target, &desired, "unix:101"); - assert_eq!(health.state, "stale-deployment"); + assert_eq!(health.state, "dependency-unavailable"); let plan = plan_keepalive(&desired, &health, "unix:102"); - assert_eq!(plan.decision.action, "deploy"); - assert_eq!( - plan.deployment_request.expect("deployment request").command, - target.deploy_command.clone().expect("deploy command") - ); + assert_ne!(plan.decision.action, "deploy"); + assert!(plan.deployment_request.is_none()); assert!(plan.restart_request.is_none()); } From d3ca28ff0a97f2c98b87da813c0b2984f9efcf05 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 18 Jul 2026 21:22:36 +0100 Subject: [PATCH 06/93] Kill Idunn command process groups on timeout --- Cargo.lock | 1 + crates/idunn-daemon/Cargo.toml | 3 ++ crates/idunn-daemon/src/main.rs | 75 ++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index daa645e..e3c6d0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -506,6 +506,7 @@ dependencies = [ "cultmesh-rs", "cultnet-rs", "ed25519-dalek", + "libc", "odin-core", "rmp-serde", "serde", diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index 8dbc967..e0bff9b 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -24,3 +24,6 @@ uuid = "1" [dev-dependencies] tempfile = "3" + +[target.'cfg(unix)'.dependencies] +libc = "0.2" diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index ff1f77b..329519c 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -4415,6 +4415,7 @@ fn run_shell( let stderr_file = File::create(&stderr_path) .with_context(|| format!("creating {}", stderr_path.display()))?; + configure_command_lifetime(&mut process); let child = process .stdout(Stdio::from(stdout_file)) .stderr(Stdio::from(stderr_file)) @@ -4467,6 +4468,7 @@ fn run_operator_alarm_command(options: &CommonOptions, alarm: &IdunnOperatorAlar process }; + configure_command_lifetime(&mut process); let output = process .env("IDUNN_ALARM_ID", &alarm.alarm_id) .env("IDUNN_ALARM_DAEMON_ID", &alarm.daemon_id) @@ -4538,6 +4540,31 @@ fn apply_windows_command_environment(process: &mut Command, path: &str) { process.env("Path", path); } +#[cfg(unix)] +fn configure_command_lifetime(process: &mut Command) { + use std::os::unix::process::CommandExt; + process.process_group(0); +} + +#[cfg(not(unix))] +fn configure_command_lifetime(_process: &mut Command) {} + +#[cfg(unix)] +fn terminate_command_lifetime(child: &mut Child) { + let process_group = i32::try_from(child.id()).expect("Unix child pid exceeded i32"); + // Idunn created the child as leader of this private process group. Killing + // the group ends sudo, shells, and deployment descendants under the same + // command lifetime instead of merely severing the parent we can see. + unsafe { + libc::kill(-process_group, libc::SIGKILL); + } +} + +#[cfg(not(unix))] +fn terminate_command_lifetime(child: &mut Child) { + let _ = child.kill(); +} + fn wait_for_child_with_timeout( mut child: Child, timeout: Duration, @@ -4557,7 +4584,7 @@ fn wait_for_child_with_timeout( } if started_at.elapsed() >= timeout { - let _ = child.kill(); + terminate_command_lifetime(&mut child); let _ = child.wait_with_output(); return Err(anyhow!( "command timed out after {} seconds: {command:?}", @@ -4585,7 +4612,7 @@ fn wait_for_child_status_with_timeout( } if started_at.elapsed() >= timeout { - let _ = child.kill(); + terminate_command_lifetime(&mut child); let _ = child.wait(); return Err(anyhow!( "command timed out after {} seconds: {command:?}", @@ -4628,6 +4655,50 @@ fn help_text() -> &'static str { #[cfg(test)] mod tests { use super::*; + + #[cfg(unix)] + fn descendant_marker_command(marker: &std::path::Path) -> Child { + let mut command = Command::new("sh"); + command + .arg("-c") + .arg(format!("(sleep 1; touch '{}') & wait", marker.display())) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + configure_command_lifetime(&mut command); + command.spawn().expect("spawn process-group test command") + } + + #[cfg(unix)] + #[test] + fn status_timeout_terminates_command_descendants() { + let marker = env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); + let _ = fs::remove_file(&marker); + let error = wait_for_child_status_with_timeout( + descendant_marker_command(&marker), + Duration::from_millis(100), + "descendant timeout test", + ) + .expect_err("command should time out"); + assert!(error.to_string().contains("timed out")); + thread::sleep(Duration::from_millis(1200)); + assert!(!marker.exists(), "timed-out descendant survived to write marker"); + } + + #[cfg(unix)] + #[test] + fn output_timeout_terminates_command_descendants() { + let marker = env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); + let _ = fs::remove_file(&marker); + let error = wait_for_child_with_timeout( + descendant_marker_command(&marker), + Duration::from_millis(100), + "descendant output timeout test", + ) + .expect_err("command should time out"); + assert!(error.to_string().contains("timed out")); + thread::sleep(Duration::from_millis(1200)); + assert!(!marker.exists(), "timed-out descendant survived to write marker"); + } use cultnet_rs::{CultNetRawDocumentRecord, CultNetRawPayloadEncoding}; use ed25519_dalek::{Signer, SigningKey}; use std::cell::RefCell; From 948a332dbd54f3a96436db47ccfe842a3a0f50c6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 18 Jul 2026 21:29:29 +0100 Subject: [PATCH 07/93] Treat absent release witness as first deployment --- crates/idunn-daemon/src/main.rs | 85 ++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 329519c..f5564ba 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -1486,16 +1486,27 @@ fn evaluate_release_drift( port: &dyn ReleaseStatePort, observed_at: &str, ) -> Option { - let observation = (|| -> Result<(String, String)> { + let observation = (|| -> Result<(String, Option)> { port.fetch(release)?; - Ok(( - port.desired_revision(release)?, - port.deployed_revision(release)?, - )) + let wanted = port.desired_revision(release)?; + let deployed = match port.deployed_revision(release) { + Ok(deployed) => Some(deployed), + Err(error) if error_chain_contains_io_not_found(&error) => None, + Err(error) => return Err(error), + }; + Ok((wanted, deployed)) })(); let (state, detail, transport) = match observation { - Ok((wanted, deployed)) if wanted != deployed => ( + Ok((wanted, None)) => ( + "stale-deployment", + format!( + "upstream {}/{} is {wanted}, but no deployed revision witness exists", + release.upstream_remote, release.upstream_branch + ), + "idunn.release-not-deployed", + ), + Ok((wanted, Some(deployed))) if wanted != deployed => ( "stale-deployment", format!( "upstream {}/{} is {wanted}, but the deployed revision witness reports {deployed}", @@ -1525,6 +1536,14 @@ fn evaluate_release_drift( }) } +fn error_chain_contains_io_not_found(error: &anyhow::Error) -> bool { + error.chain().any(|cause| { + cause + .downcast_ref::() + .is_some_and(|io_error| io_error.kind() == std::io::ErrorKind::NotFound) + }) +} + fn verify_release_witness_current( release: &ReleaseTarget, port: &dyn ReleaseStatePort, @@ -5944,6 +5963,28 @@ mod tests { } } + struct MissingWitnessReleaseStatePort { + desired: String, + } + + impl ReleaseStatePort for MissingWitnessReleaseStatePort { + fn fetch(&self, _release: &ReleaseTarget) -> Result<()> { + Ok(()) + } + + fn desired_revision(&self, _release: &ReleaseTarget) -> Result { + Ok(self.desired.clone()) + } + + fn deployed_revision(&self, _release: &ReleaseTarget) -> Result { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "deployment witness is absent", + ) + .into()) + } + } + fn release_desired(target: &DaemonTarget) -> IdunnDesiredDaemonRecord { IdunnDesiredDaemonRecord { daemon_id: target.daemon_id.clone(), @@ -6036,6 +6077,38 @@ mod tests { ); } + #[test] + fn absent_release_witness_is_first_deployment_not_dependency_failure() { + let target = swarm_targets(&SwarmOptions { + profile: "yggdrasil-local".to_string(), + repo_root: PathBuf::from("/srv/odin/source"), + }) + .unwrap() + .into_iter() + .find(|target| target.daemon_id == "yggdrasil-bifrost-persona-feedback") + .unwrap(); + let desired = release_desired(&target); + let health = evaluate_release_drift( + &target, + &desired, + target.release.as_ref().unwrap(), + &MissingWitnessReleaseStatePort { + desired: "abc123".to_string(), + }, + "unix:100", + ) + .unwrap(); + + assert_eq!(health.state, "stale-deployment"); + assert_eq!(health.transport, "idunn.release-not-deployed"); + assert!(health.detail.contains("no deployed revision witness exists")); + assert!( + plan_keepalive(&desired, &health, "unix:100") + .deployment_request + .is_some() + ); + } + #[test] fn unavailable_fetch_or_witness_alarms_without_deploying() { let target = swarm_targets(&SwarmOptions { From eefd47f14de909aca17beaceffb4277c0997575c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 08:23:06 +0100 Subject: [PATCH 08/93] Prevent live Idunn request supersession --- crates/idunn-daemon/src/main.rs | 182 ++++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 7 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index f5564ba..78c76ca 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -655,18 +655,43 @@ fn run_swarm(options: &SwarmOptions, common: &CommonOptions) -> Result<()> { start_rudp_health_ingress(common, &store_lock, &now)?; publish_surgery_plans(&options.profile, &targets, common, &store_lock, &now)?; + // Health-driven and lifecycle-command workers are separate so observation + // remains responsive. One per-target gate serializes local actuation; the + // persistent request/result CAS chain remains the durable authority. + let actuation_gates = Arc::new( + targets + .iter() + .map(|target| (target.daemon_id.clone(), Arc::new(Mutex::new(())))) + .collect::>(), + ); + let command_targets = targets.clone(); let command_common = common.clone(); let command_store_lock = Arc::clone(&store_lock); + let command_actuation_gates = Arc::clone(&actuation_gates); let mut workers = Vec::with_capacity(targets.len() + 1); workers.push(thread::spawn(move || { - run_lifecycle_command_loop(command_targets, command_common, command_store_lock) + run_lifecycle_command_loop( + command_targets, + command_common, + command_store_lock, + command_actuation_gates, + ) })); for target in targets { let worker_common = common.clone(); let worker_store_lock = Arc::clone(&store_lock); + let worker_actuation_gate = actuation_gates + .get(&target.daemon_id) + .cloned() + .ok_or_else(|| anyhow!("Idunn target lost its actuation gate"))?; workers.push(thread::spawn(move || { - run_target_loop(target, worker_common, worker_store_lock) + run_target_loop( + target, + worker_common, + worker_store_lock, + worker_actuation_gate, + ) })); } @@ -685,10 +710,16 @@ fn run_lifecycle_command_loop( targets: Vec, options: CommonOptions, store_lock: Arc>, + actuation_gates: Arc>>>, ) -> Result<()> { loop { for target in &targets { - if let Err(error) = process_pending_lifecycle_commands(target, &options, &store_lock) { + let gate = actuation_gates + .get(&target.daemon_id) + .ok_or_else(|| anyhow!("Idunn lifecycle target lost its actuation gate"))?; + if let Err(error) = with_target_actuation_gate(gate, || { + process_pending_lifecycle_commands(target, &options, &store_lock) + }) { eprintln!( "Idunn lifecycle command cycle failed for {}: {}", target.daemon_id, error @@ -729,10 +760,13 @@ fn run_target_loop( target: DaemonTarget, options: CommonOptions, store_lock: Arc>, + actuation_gate: Arc>, ) -> Result<()> { let mut missing_since = None; loop { - if let Err(error) = run_target_cycle(&target, &options, &store_lock, &mut missing_since) { + if let Err(error) = with_target_actuation_gate(&actuation_gate, || { + run_target_cycle(&target, &options, &store_lock, &mut missing_since) + }) { eprintln!( "Idunn swarm target {} cycle failed: {}", target.daemon_id, error @@ -742,6 +776,16 @@ fn run_target_loop( } } +fn with_target_actuation_gate( + gate: &Mutex<()>, + action: impl FnOnce() -> Result, +) -> Result { + let _actuation = gate + .lock() + .map_err(|_| anyhow!("Idunn target actuation gate is poisoned"))?; + action() +} + fn run_target_cycle( target: &DaemonTarget, options: &CommonOptions, @@ -1183,12 +1227,36 @@ fn persist_current_deployment_request( request: &IdunnDeploymentRequestRecord, ) -> Result { for _ in 0..8 { - let (current_head, current_head_envelope, current_request_envelope) = + let ( + current_head, + current_head_envelope, + current_head_request_envelope, + current_head_result_envelope, + current_request_envelope, + ) = with_store_node(options, store_lock, |node| { + let head = + node.get::(&request.daemon_id)?; Ok(( - node.get::(&request.daemon_id)?, + head.clone(), node.cache() .get_envelope::(&request.daemon_id)?, + head.as_ref() + .map(|head| { + node.cache() + .get_envelope::(&head.request_id) + }) + .transpose()? + .flatten(), + head.as_ref() + .map(|head| { + node.cache().get_envelope::(&format!( + "result:{}", + head.request_id + )) + }) + .transpose()? + .flatten(), node.cache() .get_envelope::(&request.request_id)?, )) @@ -1207,6 +1275,37 @@ fn persist_current_deployment_request( "existing deployment request is not the current authority head" )); } + if let Some(current) = current_head.as_ref() { + let prior_request_envelope = current_head_request_envelope + .as_ref() + .ok_or_else(|| anyhow!("current deployment head lost its exact request"))?; + let prior_request: IdunnDeploymentRequestRecord = + rmp_serde::from_slice(&prior_request_envelope.payload)?; + if prior_request.request_id != current.request_id + || prior_request.daemon_id != current.daemon_id + { + return Err(anyhow!( + "current deployment head request identity is substituted" + )); + } + let prior_result_envelope = current_head_result_envelope.as_ref().ok_or_else(|| { + anyhow!( + "current deployment request {} remains live; refusing supersession", + current.request_id + ) + })?; + let prior_result: IdunnDeploymentResultRecord = + rmp_serde::from_slice(&prior_result_envelope.payload)?; + if prior_result.result_id != format!("result:{}", current.request_id) + || prior_result.request_id != current.request_id + || prior_result.daemon_id != current.daemon_id + || !matches!(prior_result.state.as_str(), "succeeded" | "failed" | "skipped") + { + return Err(anyhow!( + "current deployment request lacks an exact terminal result" + )); + } + } let head = IdunnCurrentDeploymentRequestRecord { daemon_id: request.daemon_id.clone(), request_id: request.request_id.clone(), @@ -1215,7 +1314,7 @@ fn persist_current_deployment_request( .map_or(1, |head| head.sequence.saturating_add(1)), updated_at: request.requested_at.clone(), }; - let expected = [ + let mut expected = vec![ CultCacheExpectedEnvelope { key: request.daemon_id.clone(), r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), @@ -1227,6 +1326,18 @@ fn persist_current_deployment_request( current: None, }, ]; + if let Some(current) = current_head.as_ref() { + expected.push(CultCacheExpectedEnvelope { + key: current.request_id.clone(), + r#type: IdunnDeploymentRequestRecord::TYPE.into(), + current: current_head_request_envelope, + }); + expected.push(CultCacheExpectedEnvelope { + key: format!("result:{}", current.request_id), + r#type: IdunnDeploymentResultRecord::TYPE.into(), + current: current_head_result_envelope, + }); + } if SingleFileMessagePackBackingStore::new(&options.store_path).compare_exchange( &expected, &[ @@ -4675,6 +4786,44 @@ fn help_text() -> &'static str { mod tests { use super::*; + #[test] + fn target_actuation_gate_serializes_request_owners() { + let gate = Arc::new(Mutex::new(())); + let (entered_tx, entered_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let first_gate = Arc::clone(&gate); + let first_entered = entered_tx.clone(); + let first = thread::spawn(move || { + with_target_actuation_gate(&first_gate, || { + first_entered.send("manual").unwrap(); + release_rx.recv().unwrap(); + Ok(()) + }) + .unwrap(); + }); + assert_eq!(entered_rx.recv().unwrap(), "manual"); + + let second_gate = Arc::clone(&gate); + let second_entered = entered_tx; + let second = thread::spawn(move || { + with_target_actuation_gate(&second_gate, || { + second_entered.send("automatic").unwrap(); + Ok(()) + }) + .unwrap(); + }); + assert!( + entered_rx + .recv_timeout(Duration::from_millis(100)) + .is_err(), + "automatic request owner entered while manual actuation was live" + ); + release_tx.send(()).unwrap(); + assert_eq!(entered_rx.recv().unwrap(), "automatic"); + first.join().unwrap(); + second.join().unwrap(); + } + #[cfg(unix)] fn descendant_marker_command(marker: &std::path::Path) -> Child { let mut command = Command::new("sh"); @@ -7102,6 +7251,25 @@ mod tests { .unwrap(); newer.request_id = "deploy:yggdrasil-epiphany:aaa".into(); newer.requested_at = "unix:1784160000".into(); + let live_error = persist_current_deployment_request(&options, &lock, &newer) + .unwrap_err() + .to_string(); + assert!(live_error.contains("remains live; refusing supersession")); + with_store_node(&options, &lock, |node| { + node.put( + "result:deploy:yggdrasil-epiphany:fixture", + &IdunnDeploymentResultRecord { + result_id: "result:deploy:yggdrasil-epiphany:fixture".into(), + request_id: "deploy:yggdrasil-epiphany:fixture".into(), + daemon_id: "yggdrasil-epiphany".into(), + state: "succeeded".into(), + detail: "fixture deployment completed".into(), + completed_at: "unix:1784160001".into(), + }, + )?; + Ok(()) + }) + .unwrap(); persist_current_deployment_request(&options, &lock, &newer).unwrap(); let stale_error = validate_health_admission_at( &HealthAdmissionValidationOptions { From 75bd5f29d1af44bd7111a4f5ed49ce2d96bcf47e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 08:59:52 +0100 Subject: [PATCH 09/93] Keep Idunn health admission live during actuation --- crates/idunn-daemon/src/main.rs | 35 +++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 78c76ca..5888c3e 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -526,10 +526,17 @@ fn main() -> Result<()> { match &options.mode { Mode::Single(target) => { let store_lock = Arc::new(Mutex::new(())); + let actuation_gate = Mutex::new(()); let now = timestamp()?; publish_runtime_transport_check(&options.common, &store_lock, &now)?; let mut missing_since = None; - run_target_cycle(target, &options.common, &store_lock, &mut missing_since) + run_target_cycle( + target, + &options.common, + &store_lock, + &actuation_gate, + &mut missing_since, + ) } Mode::Swarm(swarm) => run_swarm(swarm, &options.common), Mode::LifecycleCommand(command) => publish_lifecycle_command(command, &options.common), @@ -764,9 +771,13 @@ fn run_target_loop( ) -> Result<()> { let mut missing_since = None; loop { - if let Err(error) = with_target_actuation_gate(&actuation_gate, || { - run_target_cycle(&target, &options, &store_lock, &mut missing_since) - }) { + if let Err(error) = run_target_cycle( + &target, + &options, + &store_lock, + &actuation_gate, + &mut missing_since, + ) { eprintln!( "Idunn swarm target {} cycle failed: {}", target.daemon_id, error @@ -790,6 +801,7 @@ fn run_target_cycle( target: &DaemonTarget, options: &CommonOptions, store_lock: &Arc>, + actuation_gate: &Mutex<()>, missing_since: &mut Option, ) -> Result<()> { let now = timestamp()?; @@ -864,6 +876,21 @@ fn run_target_cycle( Ok(()) })?; + // Observation and signed-health admission above must remain live while a + // manual command owns the actuator. Only consequence-bearing automatic + // work joins the per-target gate. + let _automatic_actuation = if plan.deployment_request.is_some() + || plan.restart_request.is_some() + { + Some( + actuation_gate + .lock() + .map_err(|_| anyhow!("Idunn target actuation gate is poisoned"))?, + ) + } else { + None + }; + if let Some(request) = &plan.deployment_request { persist_current_deployment_request(options, store_lock, request)?; } From a17b0b6bfd69a49b3c4925f78200e990dd94c09a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 09:28:57 +0100 Subject: [PATCH 10/93] Separate Idunn observation from actuation --- crates/idunn-daemon/src/main.rs | 158 ++++++++++++++++++++++++-------- 1 file changed, 121 insertions(+), 37 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 5888c3e..610470c 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -16,7 +16,7 @@ use odin_core::{ IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, IdunnDaemonSurgeryPlanRecord, IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, IdunnDeploymentRequestRecord, IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, - IdunnOperatorAlarmRecord, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, + IdunnOperatorAlarmRecord, IdunnPlan, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, @@ -29,7 +29,7 @@ use std::fs::{self, File}; use std::net::{SocketAddr, UdpSocket}; use std::path::PathBuf; use std::process::{Child, Command, ExitStatus, Stdio}; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -43,6 +43,17 @@ const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; + +struct TargetActuationGate { + lock: Mutex<()>, + reserved: AtomicBool, +} + +impl TargetActuationGate { + fn new() -> Self { + Self { lock: Mutex::new(()), reserved: AtomicBool::new(false) } + } +} const HOST_IDENTITY_TYPE: &str = "epiphany.host_identity_trust_anchor.v0"; const HOST_IDENTITY_KEY: &str = "host-incarnation-public"; const HOST_SIGNATURE_DOMAIN: &[u8] = b"epiphany.host-incarnation.signature.v0\0"; @@ -526,7 +537,7 @@ fn main() -> Result<()> { match &options.mode { Mode::Single(target) => { let store_lock = Arc::new(Mutex::new(())); - let actuation_gate = Mutex::new(()); + let actuation_gate = Arc::new(TargetActuationGate::new()); let now = timestamp()?; publish_runtime_transport_check(&options.common, &store_lock, &now)?; let mut missing_since = None; @@ -534,9 +545,13 @@ fn main() -> Result<()> { target, &options.common, &store_lock, - &actuation_gate, + Arc::clone(&actuation_gate), &mut missing_since, - ) + )?; + while actuation_gate.reserved.load(Ordering::Acquire) { + thread::sleep(Duration::from_millis(10)); + } + Ok(()) } Mode::Swarm(swarm) => run_swarm(swarm, &options.common), Mode::LifecycleCommand(command) => publish_lifecycle_command(command, &options.common), @@ -668,7 +683,7 @@ fn run_swarm(options: &SwarmOptions, common: &CommonOptions) -> Result<()> { let actuation_gates = Arc::new( targets .iter() - .map(|target| (target.daemon_id.clone(), Arc::new(Mutex::new(())))) + .map(|target| (target.daemon_id.clone(), Arc::new(TargetActuationGate::new()))) .collect::>(), ); @@ -717,16 +732,21 @@ fn run_lifecycle_command_loop( targets: Vec, options: CommonOptions, store_lock: Arc>, - actuation_gates: Arc>>>, + actuation_gates: Arc>>, ) -> Result<()> { loop { for target in &targets { let gate = actuation_gates .get(&target.daemon_id) .ok_or_else(|| anyhow!("Idunn lifecycle target lost its actuation gate"))?; - if let Err(error) = with_target_actuation_gate(gate, || { + if gate.reserved.swap(true, Ordering::AcqRel) { + continue; + } + let result = with_target_actuation_gate(&gate.lock, || { process_pending_lifecycle_commands(target, &options, &store_lock) - }) { + }); + gate.reserved.store(false, Ordering::Release); + if let Err(error) = result { eprintln!( "Idunn lifecycle command cycle failed for {}: {}", target.daemon_id, error @@ -767,7 +787,7 @@ fn run_target_loop( target: DaemonTarget, options: CommonOptions, store_lock: Arc>, - actuation_gate: Arc>, + actuation_gate: Arc, ) -> Result<()> { let mut missing_since = None; loop { @@ -775,7 +795,7 @@ fn run_target_loop( &target, &options, &store_lock, - &actuation_gate, + Arc::clone(&actuation_gate), &mut missing_since, ) { eprintln!( @@ -801,7 +821,7 @@ fn run_target_cycle( target: &DaemonTarget, options: &CommonOptions, store_lock: &Arc>, - actuation_gate: &Mutex<()>, + actuation_gate: Arc, missing_since: &mut Option, ) -> Result<()> { let now = timestamp()?; @@ -876,20 +896,54 @@ fn run_target_cycle( Ok(()) })?; - // Observation and signed-health admission above must remain live while a - // manual command owns the actuator. Only consequence-bearing automatic - // work joins the per-target gate. - let _automatic_actuation = if plan.deployment_request.is_some() - || plan.restart_request.is_some() - { - Some( - actuation_gate - .lock() - .map_err(|_| anyhow!("Idunn target actuation gate is poisoned"))?, - ) - } else { - None - }; + if plan.deployment_request.is_some() || plan.restart_request.is_some() { + schedule_automatic_actuation( + target.clone(), options.clone(), Arc::clone(store_lock), actuation_gate, plan, now, + ); + return Ok(()); + } + + finish_target_cycle(target, options, store_lock, plan, &now) +} + +fn schedule_automatic_actuation( + target: DaemonTarget, + options: CommonOptions, + store_lock: Arc>, + gate: Arc, + plan: IdunnPlan, + now: String, +) { + let daemon_id = target.daemon_id.clone(); + schedule_target_actuation(gate, daemon_id, move || { + finish_target_cycle(&target, &options, &store_lock, plan, &now) + }); +} + +fn schedule_target_actuation( + gate: Arc, + daemon_id: String, + action: impl FnOnce() -> Result<()> + Send + 'static, +) { + if gate.reserved.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire).is_err() { + return; + } + thread::spawn(move || { + let result = with_target_actuation_gate(&gate.lock, action); + gate.reserved.store(false, Ordering::Release); + if let Err(error) = result { + eprintln!("Idunn automatic actuation failed for {}: {}", daemon_id, error); + } + }); +} + +fn finish_target_cycle( + target: &DaemonTarget, + options: &CommonOptions, + store_lock: &Arc>, + plan: IdunnPlan, + now: &str, +) -> Result<()> { if let Some(request) = &plan.deployment_request { persist_current_deployment_request(options, store_lock, request)?; @@ -902,7 +956,7 @@ fn run_target_cycle( target .release .as_ref() - .and_then(|release| run_state_migration(target, release, &now, options)) + .and_then(|release| run_state_migration(target, release, now, options)) } else { None }; @@ -924,7 +978,7 @@ fn run_target_cycle( detail: format!( "deployment authority revalidation failed before migration: {error:#}" ), - completed_at: now.clone(), + completed_at: now.to_string(), } } else if migration_failed { IdunnDeploymentResultRecord { @@ -933,10 +987,10 @@ fn run_target_cycle( daemon_id: request.daemon_id.clone(), state: "failed".to_string(), detail: "state migration failed; deployment command was not run".to_string(), - completed_at: now.clone(), + completed_at: now.to_string(), } } else { - run_deployment(request, &now, options, store_lock) + run_deployment(request, now, options, store_lock) }; if result.state == "succeeded" && let Some(release) = target.release.as_ref() @@ -949,10 +1003,10 @@ fn run_target_cycle( ); } let rollout_result = target.release.as_ref().map(|release| { - rollout_result_record(target, release, &result, migration_result.as_ref(), &now) + rollout_result_record(target, release, &result, migration_result.as_ref(), now) }); let alarm = if result.state != "succeeded" { - Some(deployment_failure_alarm(&result, &now)) + Some(deployment_failure_alarm(&result, now)) } else { None }; @@ -987,9 +1041,9 @@ fn run_target_cycle( if let Some(request) = &plan.restart_request { if options.execute { - let result = run_restart(request, &now, options); + let result = run_restart(request, now, options); let alarm = if result.state != "succeeded" { - Some(restart_failure_alarm(&result, &now)) + Some(restart_failure_alarm(&result, now)) } else { None }; @@ -4815,13 +4869,13 @@ mod tests { #[test] fn target_actuation_gate_serializes_request_owners() { - let gate = Arc::new(Mutex::new(())); + let gate = Arc::new(TargetActuationGate::new()); let (entered_tx, entered_rx) = std::sync::mpsc::channel(); let (release_tx, release_rx) = std::sync::mpsc::channel(); let first_gate = Arc::clone(&gate); let first_entered = entered_tx.clone(); let first = thread::spawn(move || { - with_target_actuation_gate(&first_gate, || { + with_target_actuation_gate(&first_gate.lock, || { first_entered.send("manual").unwrap(); release_rx.recv().unwrap(); Ok(()) @@ -4833,7 +4887,7 @@ mod tests { let second_gate = Arc::clone(&gate); let second_entered = entered_tx; let second = thread::spawn(move || { - with_target_actuation_gate(&second_gate, || { + with_target_actuation_gate(&second_gate.lock, || { second_entered.send("automatic").unwrap(); Ok(()) }) @@ -4851,6 +4905,36 @@ mod tests { second.join().unwrap(); } + #[test] + fn blocked_actuation_does_not_block_observation_and_admission_work() { + let gate = Arc::new(TargetActuationGate::new()); + assert!(!gate.reserved.swap(true, Ordering::AcqRel)); + let manual_guard = gate.lock.lock().unwrap(); + let (actuation_tx, actuation_rx) = std::sync::mpsc::channel(); + schedule_target_actuation(Arc::clone(&gate), "epiphany".into(), move || { + actuation_tx.send("actuated").unwrap(); + Ok(()) + }); + + assert!(actuation_rx.recv_timeout(Duration::from_millis(100)).is_err()); + // This is the target loop's independent signed-health admission lane: + // it does not acquire or await the consequence gate. + let admitted_health = "accepted"; + assert_eq!(admitted_health, "accepted"); + + drop(manual_guard); + gate.reserved.store(false, Ordering::Release); + let (actuation_tx, actuation_rx) = std::sync::mpsc::channel(); + schedule_target_actuation(Arc::clone(&gate), "epiphany".into(), move || { + actuation_tx.send("actuated").unwrap(); + Ok(()) + }); + assert_eq!(actuation_rx.recv().unwrap(), "actuated"); + while gate.reserved.load(Ordering::Acquire) { + thread::yield_now(); + } + } + #[cfg(unix)] fn descendant_marker_command(marker: &std::path::Path) -> Child { let mut command = Command::new("sh"); From 64bb679a1b1b7990780884a262da9ce3ef9b279d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 10:14:04 +0100 Subject: [PATCH 11/93] Read pinned Epiphany trust anchor without writer lock --- crates/idunn-daemon/src/main.rs | 48 ++++++++++++++++++++++++++++++++- vendor/cultcache-rs/src/lib.rs | 8 ++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 610470c..0f28d2b 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -2562,7 +2562,7 @@ fn validate_epiphany_signed_health_shape(signed: &EpiphanySignedRuntimeHealthWir } fn load_epiphany_health_identity(path: &std::path::Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all()?; + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; let envelope = entries .iter() .find(|entry| entry.r#type == HOST_IDENTITY_TYPE && entry.key == HOST_IDENTITY_KEY) @@ -7423,6 +7423,52 @@ mod tests { assert!(health_from_rudp_message(&forged, &forged_options).is_err()); } + #[test] + fn pinned_epiphany_health_identity_load_is_read_only() { + let (_message, options, _root) = signed_epiphany_health_fixture(1); + let identity_path = options + .trusted_epiphany_health_identity_store + .as_deref() + .expect("fixture trust anchor"); + let lock_path = identity_path.with_file_name(format!( + "{}.lock", + identity_path + .file_name() + .expect("trust anchor filename") + .to_string_lossy() + )); + if lock_path.exists() { + std::fs::remove_file(&lock_path).unwrap(); + } + let mut permissions = std::fs::metadata(identity_path).unwrap().permissions(); + permissions.set_readonly(true); + std::fs::set_permissions(identity_path, permissions).unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions( + identity_path.parent().unwrap(), + std::fs::Permissions::from_mode(0o555), + ) + .unwrap(); + } + + let identity = load_epiphany_health_identity(identity_path).unwrap(); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions( + identity_path.parent().unwrap(), + std::fs::Permissions::from_mode(0o755), + ) + .unwrap(); + } + + assert_eq!(identity.schema_version, HOST_IDENTITY_TYPE); + assert!(!lock_path.exists()); + } + #[test] fn unsigned_epiphany_health_is_rejected() { let (mut message, options, _root) = signed_epiphany_health_fixture(1); diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs index dfaef5c..54846f2 100644 --- a/vendor/cultcache-rs/src/lib.rs +++ b/vendor/cultcache-rs/src/lib.rs @@ -136,6 +136,14 @@ impl SingleFileMessagePackBackingStore { &self.path } + /// Reads one atomically published snapshot without acquiring or creating + /// the sibling writer lock. This is for immutable, externally owned + /// documents such as pinned trust anchors where the reader has no write + /// authority over the containing directory. + pub fn pull_all_read_only_snapshot(&self) -> Result> { + self.read_all_unlocked() + } + pub fn compare_exchange( &self, expected: &[CultCacheExpectedEnvelope], From 15a744c826f0aa2e8cc322a6543ea8a9afcd852a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 11:00:39 +0100 Subject: [PATCH 12/93] Terminalize deployments interrupted by Idunn restart --- crates/idunn-daemon/src/main.rs | 174 ++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 0f28d2b..77e6af7 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -673,6 +673,12 @@ fn run_swarm(options: &SwarmOptions, common: &CommonOptions) -> Result<()> { let store_lock = Arc::new(Mutex::new(())); let now = timestamp()?; + let recovered = terminalize_interrupted_deployment_requests(common, &store_lock, &now)?; + if recovered > 0 { + println!( + "Idunn terminalized {recovered} deployment request(s) interrupted by the prior daemon incarnation." + ); + } publish_runtime_transport_check(common, &store_lock, &now)?; start_rudp_health_ingress(common, &store_lock, &now)?; publish_surgery_plans(&options.profile, &targets, common, &store_lock, &now)?; @@ -1434,6 +1440,123 @@ fn persist_current_deployment_request( )) } +/// On daemon startup no deployment actuator from the prior process can still +/// be an Idunn-owned child. Close any exact current request that lacks a result +/// before this incarnation launches workers. The current head remains durable +/// history; its exact failed result permits ordinary CAS supersession. +fn terminalize_interrupted_deployment_requests( + options: &CommonOptions, + store_lock: &Arc>, + completed_at: &str, +) -> Result { + let _store_guard = store_lock + .lock() + .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; + let backing = SingleFileMessagePackBackingStore::new(&options.store_path); + let opening = backing.pull_all()?; + let heads = opening + .iter() + .filter(|entry| entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE) + .collect::>(); + let mut recovered = 0; + for head_envelope in heads { + let head: IdunnCurrentDeploymentRequestRecord = + rmp_serde::from_slice(&head_envelope.payload)?; + if head.daemon_id.trim().is_empty() + || head.request_id.trim().is_empty() + || head.sequence == 0 + || head_envelope.key != head.daemon_id + { + return Err(anyhow!( + "current deployment request head is invalid during startup recovery" + )); + } + let request_matches = opening + .iter() + .filter(|entry| { + entry.r#type == IdunnDeploymentRequestRecord::TYPE + && entry.key == head.request_id + }) + .collect::>(); + if request_matches.len() != 1 { + return Err(anyhow!( + "current deployment request {} lacks one exact request during startup recovery", + head.request_id + )); + } + let request_envelope = request_matches[0]; + let request: IdunnDeploymentRequestRecord = + rmp_serde::from_slice(&request_envelope.payload)?; + if request.request_id != head.request_id || request.daemon_id != head.daemon_id { + return Err(anyhow!( + "current deployment request is substituted during startup recovery" + )); + } + let result_key = format!("result:{}", head.request_id); + let result_matches = opening + .iter() + .filter(|entry| { + entry.r#type == IdunnDeploymentResultRecord::TYPE && entry.key == result_key + }) + .collect::>(); + if result_matches.len() > 1 { + return Err(anyhow!( + "current deployment request has duplicate results during startup recovery" + )); + } + if let Some(result_envelope) = result_matches.first() { + let result: IdunnDeploymentResultRecord = + rmp_serde::from_slice(&result_envelope.payload)?; + if result.result_id != result_key + || result.request_id != head.request_id + || result.daemon_id != head.daemon_id + || !matches!(result.state.as_str(), "succeeded" | "failed" | "skipped") + { + return Err(anyhow!( + "current deployment request lacks an exact terminal result during startup recovery" + )); + } + continue; + } + let result = IdunnDeploymentResultRecord { + result_id: result_key.clone(), + request_id: head.request_id.clone(), + daemon_id: head.daemon_id.clone(), + state: "failed".to_string(), + detail: "deployment actuator was interrupted when the owning Idunn daemon incarnation stopped" + .to_string(), + completed_at: completed_at.to_string(), + }; + let expected = [ + CultCacheExpectedEnvelope { + key: head.daemon_id.clone(), + r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), + current: Some(head_envelope.clone()), + }, + CultCacheExpectedEnvelope { + key: head.request_id.clone(), + r#type: IdunnDeploymentRequestRecord::TYPE.into(), + current: Some(request_envelope.clone()), + }, + CultCacheExpectedEnvelope { + key: result_key.clone(), + r#type: IdunnDeploymentResultRecord::TYPE.into(), + current: None, + }, + ]; + if !backing.compare_exchange( + &expected, + &[typed_envelope(&result_key, &result, completed_at)?], + )? { + return Err(anyhow!( + "interrupted deployment request changed during startup recovery" + )); + } + recovered += 1; + } + Ok(recovered) +} + fn release_authority_id( repository_full_name: &str, upstream_ref: &str, @@ -7423,6 +7546,57 @@ mod tests { assert!(health_from_rudp_message(&forged, &forged_options).is_err()); } + #[test] + fn startup_terminalizes_interrupted_deployment_before_supersession() { + let (_message, options, _root) = signed_epiphany_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + let prior = with_store_node(&options, &lock, |node| { + node.get::( + "deploy:yggdrasil-epiphany:fixture", + )? + .ok_or_else(|| anyhow!("fixture request missing")) + }) + .unwrap(); + + assert_eq!( + terminalize_interrupted_deployment_requests( + &options, + &lock, + "2026-07-16T00:02:00Z", + ) + .unwrap(), + 1 + ); + let result = with_store_node(&options, &lock, |node| { + node.get::( + "result:deploy:yggdrasil-epiphany:fixture", + )? + .ok_or_else(|| anyhow!("recovered result missing")) + }) + .unwrap(); + assert_eq!(result.request_id, prior.request_id); + assert_eq!(result.daemon_id, prior.daemon_id); + assert_eq!(result.state, "failed"); + assert_eq!(result.completed_at, "2026-07-16T00:02:00Z"); + assert!(result.detail.contains("owning Idunn daemon incarnation stopped")); + assert_eq!( + terminalize_interrupted_deployment_requests( + &options, + &lock, + "2026-07-16T00:03:00Z", + ) + .unwrap(), + 0 + ); + + let mut successor = prior; + successor.request_id = "deploy:yggdrasil-epiphany:successor".into(); + successor.requested_at = "2026-07-16T00:04:00Z".into(); + let head = persist_current_deployment_request(&options, &lock, &successor).unwrap(); + assert_eq!(head.request_id, successor.request_id); + assert_eq!(head.sequence, 2); + } + #[test] fn pinned_epiphany_health_identity_load_is_read_only() { let (_message, options, _root) = signed_epiphany_health_fixture(1); From cc80cd4479e34ad83490a6418e55546c6911aea5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 19:38:18 +0100 Subject: [PATCH 13/93] Define signed daemon health authority --- crates/odin-core/src/documents.rs | 287 +++++++++++++++++++ docs/idunn-signed-daemon-health-authority.md | 117 ++++++++ 2 files changed, 404 insertions(+) create mode 100644 docs/idunn-signed-daemon-health-authority.md diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index d8df9ea..2886320 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -1,6 +1,8 @@ use cultcache_rs::DatabaseEntry; use serde_json::Value; +use anyhow::{Result, bail}; + pub const ODIN_SNAPSHOT_SCHEMA: &str = "odin.snapshot.v1"; pub const ODIN_VERSE_SCHEMA: &str = "odin.verse.v1"; pub const ODIN_SERVICE_SCHEMA: &str = "odin.service.v1"; @@ -36,6 +38,8 @@ pub const IDUNN_DAEMON_TRANSPORT_PROFILE_SCHEMA: &str = "idunn.daemon_transport_ pub const IDUNN_COMMAND_BOUNDARY_SCHEMA: &str = "idunn.command_boundary.v1"; pub const IDUNN_RUNTIME_TRANSPORT_CHECK_SCHEMA: &str = "idunn.runtime_transport_check.v1"; pub const IDUNN_RUDP_HEALTH_INGRESS_SCHEMA: &str = "idunn.rudp_health_ingress.v1"; +pub const IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA: &str = "idunn.signed_daemon_health.v1"; +pub const IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA: &str = "idunn.daemon_health_trust_binding.v1"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; pub const MUNINN_CAPTURE_STREAM_SCHEMA: &str = "muninn.capture_stream.v1"; pub const MUNINN_CAPTURE_STREAM_COMMAND_SCHEMA: &str = "muninn.capture_stream_command.v1"; @@ -309,6 +313,192 @@ pub struct IdunnSignedHealthAdmissionRecord { pub signed_health_sha256: String, } +/// Provider-owned health statement. Ingress must verify `signature` over the +/// complete document with an empty signature field before admitting it. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.signed_daemon_health", + schema = "idunn.signed_daemon_health.v1" +)] +pub struct IdunnSignedDaemonHealthRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub daemon_id: String, + #[cultcache(key = 2)] + pub health_contract: String, + #[cultcache(key = 3)] + pub source_runtime_id: String, + #[cultcache(key = 4)] + pub state: String, + #[cultcache(key = 5)] + pub detail: String, + #[cultcache(key = 6)] + pub signer_identity_id: String, + #[cultcache(key = 7)] + pub publisher_incarnation_id: String, + #[cultcache(key = 8)] + pub publisher_sequence: u64, + #[cultcache(key = 9)] + pub observed_at_unix_millis: u64, + #[cultcache(key = 10)] + pub release_id: Option, + #[cultcache(key = 11)] + pub release_witness_sha256: Option, + #[cultcache(key = 12)] + pub source_commit: Option, + #[cultcache(key = 13)] + pub deployment_id: Option, + #[cultcache(key = 14)] + pub signature_algorithm: String, + #[cultcache(key = 15)] + pub signature: Vec, + #[cultcache(key = 16)] + pub private_state_exposed: bool, +} + +impl IdunnSignedDaemonHealthRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA { + bail!("signed daemon health schema is unsupported"); + } + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.health_contract, "health contract")?; + validate_identifier(&self.source_runtime_id, "source runtime id")?; + validate_identifier(&self.signer_identity_id, "signer identity id")?; + validate_identifier(&self.publisher_incarnation_id, "publisher incarnation id")?; + if !matches!( + self.state.as_str(), + "active" | "warming" | "degraded" | "failed" + ) { + bail!("signed daemon health state is invalid"); + } + if self.detail.len() > 512 { + bail!("signed daemon health detail exceeds 512 bytes"); + } + if self.signature_algorithm != "ed25519" + || self.signature.len() != 64 + { + bail!("signed daemon health signature shape is invalid"); + } + if self.publisher_sequence == 0 || self.observed_at_unix_millis == 0 { + bail!("signed daemon health sequence or observation time is invalid"); + } + validate_optional_release_binding( + &self.release_id, + &self.release_witness_sha256, + &self.source_commit, + )?; + validate_optional_identifier(&self.deployment_id, "deployment id")?; + if self.private_state_exposed { + bail!("signed daemon health exposes private state"); + } + Ok(()) + } +} + +/// Root-owned admission binding. Store ownership is the physical authority; +/// this document names the only signer accepted for one daemon/contract/runtime. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.daemon_health_trust_binding", + schema = "idunn.daemon_health_trust_binding.v1" +)] +pub struct IdunnDaemonHealthTrustBindingRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub binding_id: String, + #[cultcache(key = 2)] + pub daemon_id: String, + #[cultcache(key = 3)] + pub health_contract: String, + #[cultcache(key = 4)] + pub source_runtime_id: String, + #[cultcache(key = 5)] + pub signer_identity_id: String, + #[cultcache(key = 6)] + pub signer_public_key: Vec, + #[cultcache(key = 7)] + pub binding_authority: String, + #[cultcache(key = 8)] + pub bound_at_unix_millis: u64, + #[cultcache(key = 9)] + pub release_binding_required: bool, + #[cultcache(key = 10)] + pub private_state_exposed: bool, +} + +impl IdunnDaemonHealthTrustBindingRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA { + bail!("daemon health trust binding schema is unsupported"); + } + validate_identifier(&self.binding_id, "binding id")?; + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.health_contract, "health contract")?; + validate_identifier(&self.source_runtime_id, "source runtime id")?; + validate_identifier(&self.signer_identity_id, "signer identity id")?; + if self.signer_public_key.len() != 32 + || self.binding_authority != "root" + || self.bound_at_unix_millis == 0 + { + bail!("daemon health trust binding authority or key is invalid"); + } + if self.private_state_exposed { + bail!("daemon health trust binding exposes private state"); + } + Ok(()) + } +} + +fn validate_identifier(value: &str, label: &str) -> Result<()> { + if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { + bail!("{label} is empty, oversized, or contains control characters"); + } + Ok(()) +} + +fn validate_optional_identifier(value: &Option, label: &str) -> Result<()> { + if let Some(value) = value { + validate_identifier(value, label)?; + } + Ok(()) +} + +fn validate_optional_release_binding( + release_id: &Option, + witness: &Option, + source_commit: &Option, +) -> Result<()> { + if release_id.is_some() || witness.is_some() || source_commit.is_some() { + validate_optional_identifier(release_id, "release id")?; + let witness = witness + .as_deref() + .ok_or_else(|| anyhow::anyhow!("release witness is absent"))?; + let commit = source_commit + .as_deref() + .ok_or_else(|| anyhow::anyhow!("source commit is absent"))?; + if release_id.is_none() || !is_sha256(witness) || !is_lower_hex(commit, 40) { + bail!("release binding is incomplete or malformed"); + } + } + Ok(()) +} + +fn is_sha256(value: &str) -> bool { + value + .strip_prefix("sha256-") + .is_some_and(|digest| is_lower_hex(digest, 64)) +} + +fn is_lower_hex(value: &str, length: usize) -> bool { + value.len() == length + && value + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) +} + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.keepalive_decision", @@ -1516,6 +1706,8 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { IdunnDesiredDaemonRecord => IDUNN_DESIRED_DAEMON_SCHEMA, IdunnDaemonHealthRecord => IDUNN_DAEMON_HEALTH_SCHEMA, IdunnSignedHealthAdmissionRecord => IDUNN_SIGNED_HEALTH_ADMISSION_SCHEMA, + IdunnSignedDaemonHealthRecord => IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, + IdunnDaemonHealthTrustBindingRecord => IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnKeepaliveDecisionRecord => IDUNN_KEEPALIVE_DECISION_SCHEMA, IdunnRestartRequestRecord => IDUNN_RESTART_REQUEST_SCHEMA, IdunnRestartResultRecord => IDUNN_RESTART_RESULT_SCHEMA, @@ -1575,6 +1767,101 @@ mod tests { use anyhow::Result; use cultmesh_rs::{CultMesh, CultMeshNodeOptions}; + fn signed_health_fixture() -> IdunnSignedDaemonHealthRecord { + IdunnSignedDaemonHealthRecord { + schema_version: IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), + daemon_id: "yggdrasil-epiphany".into(), + health_contract: "epiphany.cultnet-rudp-runtime-health".into(), + source_runtime_id: "epiphany-yggdrasil".into(), + state: "active".into(), + detail: "provider-authenticated resident readiness".into(), + signer_identity_id: "epiphany-host-identity".into(), + publisher_incarnation_id: "epiphany-yggdrasil/boot-7/process-11".into(), + publisher_sequence: 42, + observed_at_unix_millis: 1_784_483_200_000, + release_id: Some("release-42".into()), + release_witness_sha256: Some(format!("sha256-{}", "a".repeat(64))), + source_commit: Some("b".repeat(40)), + deployment_id: Some("deploy:yggdrasil-epiphany:42".into()), + signature_algorithm: "ed25519".into(), + signature: vec![9; 64], + private_state_exposed: false, + } + } + + fn trust_binding_fixture() -> IdunnDaemonHealthTrustBindingRecord { + let health = signed_health_fixture(); + IdunnDaemonHealthTrustBindingRecord { + schema_version: IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), + binding_id: "root/yggdrasil-epiphany/health".into(), + daemon_id: health.daemon_id, + health_contract: health.health_contract, + source_runtime_id: health.source_runtime_id, + signer_identity_id: health.signer_identity_id, + signer_public_key: vec![7; 32], + binding_authority: "root".into(), + bound_at_unix_millis: 1_784_483_100_000, + release_binding_required: true, + private_state_exposed: false, + } + } + + #[test] + fn generic_signed_health_contracts_validate_and_round_trip() -> Result<()> { + let health = signed_health_fixture(); + let binding = trust_binding_fixture(); + health.validate()?; + binding.validate()?; + + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("signed-health.cc"); + let mut node = CultMesh::create_node( + &store_path, + OdinDocuments, + CultMeshNodeOptions { + runtime_id: "idunn-test".into(), + pull_on_start: true, + }, + )?; + node.put(&health.daemon_id, &health)?; + node.put(&binding.binding_id, &binding)?; + assert_eq!( + node.get_required::(&health.daemon_id)?, + health + ); + assert_eq!( + node.get_required::(&binding.binding_id)?, + binding + ); + Ok(()) + } + + #[test] + fn generic_signed_health_contracts_refuse_weak_or_partial_authority() { + let mut health = signed_health_fixture(); + health.private_state_exposed = true; + assert!(health.validate().is_err()); + health = signed_health_fixture(); + health.signature.clear(); + assert!(health.validate().is_err()); + health = signed_health_fixture(); + health.release_witness_sha256 = None; + assert!(health.validate().is_err()); + health = signed_health_fixture(); + health.publisher_sequence = 0; + assert!(health.validate().is_err()); + + let mut binding = trust_binding_fixture(); + binding.binding_authority = "daemon".into(); + assert!(binding.validate().is_err()); + binding = trust_binding_fixture(); + binding.signer_public_key = vec![1; 31]; + assert!(binding.validate().is_err()); + binding = trust_binding_fixture(); + binding.private_state_exposed = true; + assert!(binding.validate().is_err()); + } + #[test] fn muninn_media_documents_round_trip_through_cultmesh() -> Result<()> { let temp = tempfile::tempdir()?; diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md new file mode 100644 index 0000000..114ea72 --- /dev/null +++ b/docs/idunn-signed-daemon-health-authority.md @@ -0,0 +1,117 @@ +# Idunn signed daemon-health authority + +## Objective + +Idunn must know which daemon authored a health statement before that statement +can affect lifecycle judgment or operator status. A typed packet is a shape, +not an identity. The current generic `idunn.daemon_health.v1` RUDP path checks +shape, daemon id, contract, self-declared publication source, and freshness but +does not authenticate the publisher. It is diagnostic input until replaced. + +## Owner + +- Each daemon owns its health statement and signs it with its pinned service or + host identity. +- Idunn owns admission, freshness, monotonic lineage, managed-health judgment, + and the outward status projection. +- Root deployment configuration owns the trust binding between daemon id, + health contract, source runtime, and signer identity. + +No renderer, transport peer, systemd unit, PID probe, caller boolean, or typed +but unsigned packet owns health. + +## Inputs + +The daemon-authored statement binds: + +- exact daemon id and health contract; +- source runtime id and signer identity; +- observation time, publisher incarnation, and monotonic sequence; +- bounded health state and detail; +- release id, release witness, source revision, and deployment request when the + managed target is release-bound; +- `private_state_exposed=false`; +- an Ed25519 signature over the complete unsigned statement under a + domain-separated signing purpose. + +The Idunn trust binding names the only public key allowed to speak for that +daemon/contract/runtime tuple and whether release identity is required. + +## Outputs + +Idunn persists two separate facts: + +1. the admitted daemon-authored statement and its digest; +2. an Idunn-authored outward status projection binding the trust record, + admission, managed target, current deployment lineage, freshness judgment, + and observation time. + +Consumers such as Epiphany Discord Status receive only the outward projection +through CultNet/CultMesh and verify Idunn's pinned identity. Reading Idunn's +private store is not a consumer protocol. + +## Derived state + +- Process and systemd observations are diagnostic evidence only. +- RUDP source endpoints and connection ids are transport routing only. +- `publication_source`, `transport`, `ready`, and `healthy` strings are display + data unless contained inside a valid signed statement. +- An unsigned legacy health packet may be retained temporarily for diagnostics + but cannot satisfy managed health, prevent recovery, or produce a healthy + outward projection. + +## Forbidden writers + +- The inbound packet cannot choose its own trust anchor. +- A daemon cannot change its configured daemon id, contract, or source runtime + by signing different values. +- Idunn cannot manufacture daemon-authored health from PID, executable, + container, HTTP, systemd, or command exit observations. +- Epiphany, Bifrost, VoidBot, Eve, and Discord cannot infer Idunn acceptance + from a sent packet. +- A generic typed `idunn.daemon_health.v1` packet cannot fall back into + lifecycle authority when signature verification fails or trust is absent. + +## Shared paths + +Every managed daemon uses the same verification primitive: + +`signed statement -> root trust binding -> signature and tuple verification -> +monotonic admission -> target/deployment join -> Idunn outward projection` + +Epiphany's deployed `epiphany.idunn_signed_runtime_health.v0` path is migration +evidence and the first working implementation of the signature half. It is not +the permanent excuse for a service-specific verifier. The generic contract +must absorb its invariants before the special path is deleted. + +## Cut line + +1. Add generic signed-health and trust-binding contracts. +2. Make generic Idunn admission verify the pinned tuple, signature, sequence, + incarnation, freshness, and release requirements. +3. Migrate Epiphany and Bifrost publishers and install their root-owned trust + bindings. +4. Publish and verify Idunn's outward signed projection. +5. Point Discord Status at the outward projections. +6. Delete generic unsigned health from managed-health authority and delete the + Epiphany-only verifier after its last publisher migrates. + +The rebuild is incomplete while an unsigned packet can keep a managed daemon +healthy or while a status consumer must inspect Idunn's private store. + +## Verification layer + +Hostile tests must prove: + +- valid shape with no signature is not admitted; +- wrong key, daemon, contract, runtime, release, deployment, or trust record is + not admitted; +- replay, sequence rollback, time rollback, incarnation substitution, and + future/stale observations are not admitted; +- a live PID, existing executable, green HTTP response, or generic `ready` + string cannot repair failed authentication; +- one daemon's trust record cannot authenticate another daemon; +- unsigned legacy health cannot suppress recovery or yield outward healthy; +- outward status is signed by Idunn and binds the exact admitted statement; +- private detail and filesystem/process internals never enter the operator + projection. From be8836f357be9f909d347956644295f0664afd5a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 19:57:04 +0100 Subject: [PATCH 14/93] Authenticate generic daemon health --- crates/idunn-daemon/src/main.rs | 797 +++++++++++++++++-- crates/odin-core/src/documents.rs | 189 +++++ docs/idunn-signed-daemon-health-authority.md | 28 + 3 files changed, 948 insertions(+), 66 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 77e6af7..33fd9f5 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -14,13 +14,18 @@ use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ BifrostRepositoryReleaseAuthorityRecord, IdunnCommandBoundaryRecord, IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, IdunnDaemonSurgeryPlanRecord, + IdunnAuthenticatedDaemonHealthAdmissionRecord, IdunnDaemonHealthTrustBindingRecord, IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, IdunnDeploymentRequestRecord, IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, IdunnOperatorAlarmRecord, IdunnPlan, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, - IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, OdinDocuments, plan_keepalive, + IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, + IdunnSignedDaemonHealthRecord, IdunnUnsignedDaemonHealthDiagnosticRecord, OdinDocuments, + IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, + IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, + plan_keepalive, }; use sha2::{Digest, Sha256}; use std::collections::HashMap; @@ -43,6 +48,7 @@ const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; +const SIGNED_DAEMON_HEALTH_TYPE: &str = "idunn.signed_daemon_health"; struct TargetActuationGate { lock: Mutex<()>, @@ -71,6 +77,26 @@ struct IdunnDaemonHealthWireV1 { transport: String, } +enum DecodedHealthIngress { + AuthenticatedLegacy { + health: IdunnDaemonHealthRecord, + admission: IdunnSignedHealthAdmissionRecord, + }, + AuthenticatedGeneric { + health: IdunnDaemonHealthRecord, + statement: IdunnSignedDaemonHealthRecord, + admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, + }, + Diagnostic(IdunnUnsignedDaemonHealthDiagnosticRecord), +} + +struct HealthIngressOutcome { + daemon_id: String, + state: String, + detail: String, + authority: &'static str, +} + #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] struct EpiphanySignedRuntimeHealthWire { schema_version: String, @@ -468,6 +494,7 @@ struct CommonOptions { operator_alarm_command: Option, rudp_health_bind: Option, trusted_epiphany_health_identity_store: Option, + daemon_health_trust_store_path: Option, execute: bool, command_timeout_seconds: u64, } @@ -848,9 +875,16 @@ fn run_target_cycle( observed_at: now.clone(), }; - let (mut health_key, mut health) = + let (mut health_key, mut health, health_authenticated) = evaluate_target_health(target, options, store_lock, &desired, &now)?; - apply_missing_publication_grace(target, &desired, &mut health, missing_since, Instant::now()); + apply_missing_publication_grace( + target, + &desired, + &mut health, + health_authenticated, + missing_since, + Instant::now(), + ); let mut plan = plan_keepalive(&desired, &health, now.clone()); if plan.restart_request.is_some() { let veto_now = timestamp()?; @@ -1758,10 +1792,71 @@ fn read_fresh_daemon_published_health( let Some(health) = node.get::(&desired.daemon_id)? else { return Ok(None); }; - if is_fresh_daemon_published_health(&health, desired, now) { - Ok(Some(health)) - } else { - Ok(None) + if !is_fresh_daemon_published_health(&health, desired, now) { + return Ok(None); + } + match health.publication_source.as_str() { + "daemon-authenticated" => { + let Some(admission) = node + .get::(&desired.daemon_id)? + else { + return Ok(None); + }; + admission.validate()?; + let Some(statement) = node.get::( + &admission.signed_health_sha256, + )? else { + return Ok(None); + }; + let (_, current_binding_sha256) = + load_daemon_health_trust_binding(options, &statement)?; + validate_generic_release_binding(node, &admission)?; + let now_millis = parse_timestamp_seconds(now) + .and_then(|seconds| seconds.checked_mul(1000)) + .ok_or_else(|| anyhow!("managed health clock is invalid"))?; + let statement_digest = format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(&statement)?) + ); + if admission.daemon_id != health.daemon_id + || admission.health_contract != health.health_contract + || admission.state != health.state + || admission.trust_binding_sha256 != current_binding_sha256 + || admission.signed_health_sha256 != statement_digest + || admission.signer_identity_id != statement.signer_identity_id + || admission.publisher_incarnation_id != statement.publisher_incarnation_id + || admission.publisher_sequence != statement.publisher_sequence + || admission.observed_at_unix_millis != statement.observed_at_unix_millis + || now_millis < admission.admitted_at_unix_millis + || now_millis.saturating_sub(admission.observed_at_unix_millis) + > u64::from(desired.max_silence_seconds).saturating_mul(1000) + { + return Ok(None); + } + Ok(Some(health)) + } + "daemon-published" => { + let Some(admission) = + node.get::(&desired.daemon_id)? + else { + return Ok(None); + }; + validate_admission_against_current_request(node, &admission)?; + validate_admission_fresh_at( + &admission, + now, + u64::from(desired.max_silence_seconds), + )?; + if admission.daemon_id != health.daemon_id + || admission.health_contract != health.health_contract + || admission.state != health.state + || admission.observed_at != health.observed_at + { + return Ok(None); + } + Ok(Some(health)) + } + _ => Ok(None), } }) } @@ -1772,25 +1867,24 @@ fn evaluate_target_health( store_lock: &Arc>, desired: &IdunnDesiredDaemonRecord, now: &str, -) -> Result<(String, IdunnDaemonHealthRecord)> { +) -> Result<(String, IdunnDaemonHealthRecord, bool)> { if let Some(release) = &target.release { if release.deployed_revision_witness.is_some() { if let Some(health) = evaluate_release_drift(target, desired, release, &SystemReleaseStatePort, now) { - return Ok((supervisor_health_key(&desired.daemon_id), health)); + return Ok((supervisor_health_key(&desired.daemon_id), health, false)); } } } - if let Some(health) = - read_fresh_daemon_published_health(options, store_lock, desired, ×tamp()?)? - { - return Ok((desired.daemon_id.clone(), health)); + if let Some(health) = read_fresh_daemon_published_health(options, store_lock, desired, now)? { + return Ok((desired.daemon_id.clone(), health, true)); } Ok(( supervisor_health_key(&desired.daemon_id), missing_daemon_published_health(target, desired, now), + false, )) } @@ -1894,10 +1988,11 @@ fn apply_missing_publication_grace( target: &DaemonTarget, desired: &IdunnDesiredDaemonRecord, health: &mut IdunnDaemonHealthRecord, + health_authenticated: bool, missing_since: &mut Option, now: Instant, ) { - if health.publication_source == "daemon-published" { + if health_authenticated { *missing_since = None; return; } @@ -1968,7 +2063,10 @@ fn is_fresh_daemon_published_health( if health.health_contract != desired.health_contract { return false; } - if health.publication_source != "daemon-published" { + if !matches!( + health.publication_source.as_str(), + "daemon-authenticated" | "daemon-published" + ) { return false; } if health.transport != CULTNET_RUDP_PROTOCOL_ID { @@ -2278,15 +2376,16 @@ fn run_rudp_health_ingress_loop( &store_lock, &observed_at, ) { - Ok(health) => { + Ok(outcome) => { let bounded_detail = - health.detail.replace('\r', " ").replace('\n', " "); + outcome.detail.replace('\r', " ").replace('\n', " "); println!( - "Idunn accepted RUDP health for {} from {} over {} state={} detail={}", - health.daemon_id, + "Idunn recorded RUDP health input for {} from {} over {} state={} authority={} detail={}", + outcome.daemon_id, source, frame.channel_id, - health.state, + outcome.state, + outcome.authority, bounded_detail ); } @@ -2440,15 +2539,47 @@ fn admit_health_from_rudp_message( options: &CommonOptions, store_lock: &Arc>, admitted_at: &str, -) -> Result { - let (health, admission) = decode_health_from_rudp_message(message, options, admitted_at)?; - let Some(admission) = admission else { - with_store_node(options, store_lock, |node| { - node.put(&health.daemon_id, &health)?; - Ok(()) - })?; - return Ok(health); +) -> Result { + let decoded = decode_health_from_rudp_message(message, options, admitted_at)?; + let (health, admission) = match decoded { + DecodedHealthIngress::Diagnostic(diagnostic) => { + diagnostic.validate()?; + let outcome = HealthIngressOutcome { + daemon_id: diagnostic.daemon_id.clone(), + state: diagnostic.claimed_state.clone(), + detail: diagnostic.claimed_detail.clone(), + authority: "diagnostic-only", + }; + let key = diagnostic.diagnostic_id.clone(); + // Unsigned input is quarantined under a distinct type and key. It + // cannot overwrite the managed-health row or admission. + with_store_node(options, store_lock, |node| { + node.put(&key, &diagnostic)?; + Ok(()) + })?; + return Ok(outcome); + } + DecodedHealthIngress::AuthenticatedGeneric { + health, + statement, + admission, + } => { + return admit_generic_signed_health( + options, + store_lock, + admitted_at, + health, + statement, + admission, + ); + } + DecodedHealthIngress::AuthenticatedLegacy { health, admission } => (health, admission), }; + { + with_store_node(options, store_lock, |node| { + validate_admission_against_current_request(node, &admission) + })?; + } for _ in 0..8 { let (expected_health, expected_admission, expected_head, expected_request) = with_store_node(options, store_lock, |node| { @@ -2500,7 +2631,12 @@ fn admit_health_from_rudp_message( if SingleFileMessagePackBackingStore::new(&options.store_path) .compare_exchange(&expected, &[health_envelope, admission_envelope])? { - return Ok(health); + return Ok(HealthIngressOutcome { + daemon_id: health.daemon_id, + state: health.state, + detail: health.detail, + authority: "authenticated", + }); } } Err(anyhow!( @@ -2522,32 +2658,314 @@ fn typed_envelope( }) } +fn authenticate_generic_signed_health( + document: &cultnet_rs::CultNetRawDocumentRecord, + options: &CommonOptions, + admitted_at: &str, +) -> Result<( + IdunnDaemonHealthRecord, + IdunnSignedDaemonHealthRecord, + IdunnAuthenticatedDaemonHealthAdmissionRecord, +)> { + let statement: IdunnSignedDaemonHealthRecord = rmp_serde::from_slice(&document.payload) + .context("decoding generic signed daemon health")?; + statement.validate()?; + if document.record_key != statement.daemon_id + || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) + || document.source_role.as_deref() != Some("daemon-health-publisher") + { + return Err(anyhow!("signed daemon health transport identity is invalid")); + } + let (binding, binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; + if binding.release_binding_required { + if statement.release_id.is_none() + || statement.release_witness_sha256.is_none() + || statement.source_commit.is_none() + || statement.deployment_id.is_none() + { + return Err(anyhow!("signed daemon health lacks required release binding")); + } + } else if statement.release_id.is_some() + || statement.release_witness_sha256.is_some() + || statement.source_commit.is_some() + || statement.deployment_id.is_some() + { + return Err(anyhow!( + "signed daemon health carries release authority not declared by its trust binding" + )); + } + let key_bytes: [u8; 32] = binding + .signer_public_key + .as_slice() + .try_into() + .map_err(|_| anyhow!("daemon health trust key length is invalid"))?; + let expected_identity = format!( + "{:x}", + Sha256::digest([HOST_IDENTITY_DOMAIN, binding.signer_public_key.as_slice()].concat()) + ); + if binding.signer_identity_id != expected_identity + || statement.signer_identity_id != binding.signer_identity_id + { + return Err(anyhow!("signed daemon health identity is not root-bound")); + } + let signature_bytes: [u8; 64] = statement + .signature + .as_slice() + .try_into() + .map_err(|_| anyhow!("signed daemon health signature length is invalid"))?; + let mut unsigned = statement.clone(); + unsigned.signature.clear(); + let unsigned_payload = rmp_serde::to_vec(&unsigned)?; + VerifyingKey::from_bytes(&key_bytes)? + .verify( + &host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned_payload), + &Signature::from_bytes(&signature_bytes), + ) + .context("signed daemon health signature is invalid")?; + let admitted_at_unix_millis = parse_timestamp_seconds(admitted_at) + .and_then(|seconds| seconds.checked_mul(1000)) + .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; + if statement.observed_at_unix_millis > admitted_at_unix_millis + || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) + > EPIPHANY_ADMISSION_MAX_AGE_SECONDS.saturating_mul(1000) + { + return Err(anyhow!("signed daemon health observation is future or stale")); + } + let signed_health_sha256 = format!("sha256-{:x}", Sha256::digest(&document.payload)); + let observed_at = chrono::DateTime::from_timestamp_millis( + i64::try_from(statement.observed_at_unix_millis) + .map_err(|_| anyhow!("signed daemon health observation is out of range"))?, + ) + .ok_or_else(|| anyhow!("signed daemon health observation is out of range"))? + .to_rfc3339_opts(chrono::SecondsFormat::Millis, true); + let health = IdunnDaemonHealthRecord { + daemon_id: statement.daemon_id.clone(), + state: statement.state.clone(), + detail: statement.detail.clone(), + observed_at, + health_contract: statement.health_contract.clone(), + publication_source: "daemon-authenticated".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }; + let admission = IdunnAuthenticatedDaemonHealthAdmissionRecord { + schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), + daemon_id: statement.daemon_id.clone(), + health_contract: statement.health_contract.clone(), + source_runtime_id: statement.source_runtime_id.clone(), + state: statement.state.clone(), + observed_at_unix_millis: statement.observed_at_unix_millis, + admitted_at_unix_millis, + trust_binding_id: binding.binding_id, + trust_binding_sha256: binding_sha256, + signer_identity_id: statement.signer_identity_id.clone(), + publisher_incarnation_id: statement.publisher_incarnation_id.clone(), + publisher_sequence: statement.publisher_sequence, + signed_health_sha256, + release_id: statement.release_id.clone(), + release_witness_sha256: statement.release_witness_sha256.clone(), + source_commit: statement.source_commit.clone(), + deployment_id: statement.deployment_id.clone(), + private_state_exposed: false, + }; + admission.validate()?; + Ok((health, statement, admission)) +} + +fn load_daemon_health_trust_binding( + options: &CommonOptions, + statement: &IdunnSignedDaemonHealthRecord, +) -> Result<(IdunnDaemonHealthTrustBindingRecord, String)> { + let path = options + .daemon_health_trust_store_path + .as_deref() + .ok_or_else(|| anyhow!("signed daemon health has no root trust store"))?; + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let mut matches = Vec::new(); + for entry in entries + .iter() + .filter(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) + { + let binding: IdunnDaemonHealthTrustBindingRecord = rmp_serde::from_slice(&entry.payload) + .context("decoding root daemon health trust binding")?; + binding.validate()?; + if binding.daemon_id == statement.daemon_id + && binding.health_contract == statement.health_contract + && binding.source_runtime_id == statement.source_runtime_id + { + matches.push(binding); + } + } + let binding = matches + .pop() + .ok_or_else(|| anyhow!("signed daemon health has no exact root trust binding"))?; + if !matches.is_empty() { + return Err(anyhow!("signed daemon health trust binding is ambiguous")); + } + let digest = format!("sha256-{:x}", Sha256::digest(rmp_serde::to_vec(&binding)?)); + Ok((binding, digest)) +} + +fn admit_generic_signed_health( + options: &CommonOptions, + store_lock: &Arc>, + admitted_at: &str, + health: IdunnDaemonHealthRecord, + statement: IdunnSignedDaemonHealthRecord, + admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, +) -> Result { + for _ in 0..8 { + let (expected_health, expected_admission, expected_statement) = + with_store_node(options, store_lock, |node| { + if let Some(existing) = node + .get::(&admission.daemon_id)? + && !generic_signed_health_advances(&existing, &admission) + { + return Err(anyhow!("signed daemon health is replayed or regressed")); + } + validate_generic_release_binding(node, &admission)?; + Ok(( + node.cache() + .get_envelope::(&health.daemon_id)?, + node.cache() + .get_envelope::( + &admission.daemon_id, + )?, + node.cache() + .get_envelope::( + &admission.signed_health_sha256, + )?, + )) + })?; + let expected = [ + CultCacheExpectedEnvelope { + key: health.daemon_id.clone(), + r#type: IdunnDaemonHealthRecord::TYPE.into(), + current: expected_health, + }, + CultCacheExpectedEnvelope { + key: admission.daemon_id.clone(), + r#type: IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE.into(), + current: expected_admission, + }, + CultCacheExpectedEnvelope { + key: admission.signed_health_sha256.clone(), + r#type: IdunnSignedDaemonHealthRecord::TYPE.into(), + current: expected_statement, + }, + ]; + let replacements = [ + typed_envelope(&health.daemon_id, &health, admitted_at)?, + typed_envelope(&admission.daemon_id, &admission, admitted_at)?, + typed_envelope(&admission.signed_health_sha256, &statement, admitted_at)?, + ]; + if SingleFileMessagePackBackingStore::new(&options.store_path) + .compare_exchange(&expected, &replacements)? + { + return Ok(HealthIngressOutcome { + daemon_id: health.daemon_id, + state: health.state, + detail: health.detail, + authority: "authenticated", + }); + } + } + Err(anyhow!( + "signed daemon health admission lost repeated cross-process contention" + )) +} + +fn generic_signed_health_advances( + existing: &IdunnAuthenticatedDaemonHealthAdmissionRecord, + candidate: &IdunnAuthenticatedDaemonHealthAdmissionRecord, +) -> bool { + if existing.trust_binding_id != candidate.trust_binding_id + || existing.trust_binding_sha256 != candidate.trust_binding_sha256 + || existing.signer_identity_id != candidate.signer_identity_id + || existing.daemon_id != candidate.daemon_id + { + return false; + } + if existing.publisher_incarnation_id == candidate.publisher_incarnation_id { + candidate.publisher_sequence > existing.publisher_sequence + && candidate.observed_at_unix_millis >= existing.observed_at_unix_millis + } else { + candidate.observed_at_unix_millis > existing.observed_at_unix_millis + } +} + +fn validate_generic_release_binding( + node: &mut CultMeshNode, + admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, +) -> Result<()> { + let Some(deployment_id) = admission.deployment_id.as_deref() else { + return Ok(()); + }; + let request = node + .get::(deployment_id)? + .ok_or_else(|| anyhow!("signed daemon health names an unknown deployment request"))?; + if request.daemon_id != admission.daemon_id + || admission.source_commit.as_deref() != Some(request.source_revision.as_str()) + || !request.requires_bifrost_authority + || request.release_authority_id.trim().is_empty() + || request.release_authority_envelope_sha256.trim().is_empty() + { + return Err(anyhow!("signed daemon health deployment authority is invalid")); + } + let current = node + .get::(&admission.daemon_id)? + .ok_or_else(|| anyhow!("signed daemon health has no current deployment head"))?; + if current.request_id != request.request_id || current.sequence == 0 { + return Err(anyhow!("signed daemon health names a superseded deployment")); + } + let requested_at = parse_timestamp_seconds(&request.requested_at) + .and_then(|seconds| seconds.checked_mul(1000)) + .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; + if admission.observed_at_unix_millis < requested_at + || admission.admitted_at_unix_millis < requested_at + { + return Err(anyhow!("signed daemon health predates its deployment")); + } + Ok(()) +} + #[cfg(test)] fn health_from_rudp_message( message: &CultNetMessage, options: &CommonOptions, ) -> Result { - Ok(decode_health_from_rudp_message(message, options, ×tamp()?)?.0) + match decode_health_from_rudp_message(message, options, ×tamp()?)? { + DecodedHealthIngress::AuthenticatedLegacy { health, .. } + | DecodedHealthIngress::AuthenticatedGeneric { health, .. } => Ok(health), + DecodedHealthIngress::Diagnostic(_) => { + Err(anyhow!("unsigned daemon health is diagnostic-only")) + } + } } fn decode_health_from_rudp_message( message: &CultNetMessage, options: &CommonOptions, admitted_at: &str, -) -> Result<( - IdunnDaemonHealthRecord, - Option, -)> { +) -> Result { let CultNetMessage::DocumentPutRaw { document, .. } = message else { return Err(anyhow!("expected cultnet.document_put_raw.v0")); }; if document.payload_encoding != CultNetRawPayloadEncoding::Messagepack { return Err(anyhow!("expected MessagePack raw payload encoding")); } + if document.schema_id == SIGNED_DAEMON_HEALTH_TYPE { + let (health, statement, admission) = + authenticate_generic_signed_health(document, options, admitted_at)?; + return Ok(DecodedHealthIngress::AuthenticatedGeneric { + health, + statement, + admission, + }); + } if document.schema_id == EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE { let (health, admission) = authenticate_epiphany_signed_health(document, options, admitted_at)?; - return Ok((health, Some(admission))); + return Ok(DecodedHealthIngress::AuthenticatedLegacy { health, admission }); } if document.schema_id != "idunn.daemon_health" { return Err(anyhow!( @@ -2561,15 +2979,31 @@ fn decode_health_from_rudp_message( "Epiphany health requires its signed runtime-health schema" )); } - let health = legacy_health_record(wire); - if document.record_key != health.daemon_id { + if document.record_key != wire.daemon_id { return Err(anyhow!( "record key {} does not match health daemon_id {}", - document.record_key, - health.daemon_id + document.record_key, wire.daemon_id )); } - Ok((health, None)) + let received_at_unix_millis = parse_timestamp_seconds(admitted_at) + .and_then(|seconds| seconds.checked_mul(1000)) + .ok_or_else(|| anyhow!("unsigned health diagnostic receive time is invalid"))?; + let diagnostic = IdunnUnsignedDaemonHealthDiagnosticRecord { + schema_version: IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA.into(), + diagnostic_id: format!("diagnostic:{}", wire.daemon_id), + daemon_id: wire.daemon_id, + claimed_state: wire.state, + claimed_detail: wire.detail, + claimed_observed_at: wire.observed_at, + claimed_health_contract: wire.health_contract, + transport_source_runtime_id: document.source_runtime_id.clone(), + transport_source_role: document.source_role.clone(), + received_at_unix_millis, + authority: "diagnostic-only".into(), + private_state_exposed: false, + }; + diagnostic.validate()?; + Ok(DecodedHealthIngress::Diagnostic(diagnostic)) } fn authenticate_epiphany_signed_health( @@ -2812,18 +3246,6 @@ fn validate_admission_fresh_at( Ok(()) } -fn legacy_health_record(wire: IdunnDaemonHealthWireV1) -> IdunnDaemonHealthRecord { - IdunnDaemonHealthRecord { - daemon_id: wire.daemon_id, - state: wire.state, - detail: wire.detail, - observed_at: wire.observed_at, - health_contract: wire.health_contract, - publication_source: "daemon-published".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - } -} - fn publish_surgery_plans( profile: &str, targets: &[DaemonTarget], @@ -4181,6 +4603,7 @@ impl Options { let mut operator_alarm_command = None; let mut rudp_health_bind = None; let mut trusted_epiphany_health_identity_store = None; + let mut daemon_health_trust_store_path = None; let mut execute = false; let mut command_timeout_seconds = 30; let mut daemon_id = None; @@ -4285,6 +4708,12 @@ impl Options { "--trusted-epiphany-health-identity-store", )?)); } + "--daemon-health-trust-store" => { + daemon_health_trust_store_path = Some(PathBuf::from(take_value( + &mut args, + "--daemon-health-trust-store", + )?)); + } "--disabled" => enabled = false, "--execute" => execute = true, "--interval-seconds" => { @@ -4327,6 +4756,7 @@ impl Options { operator_alarm_command, rudp_health_bind, trusted_epiphany_health_identity_store, + daemon_health_trust_store_path, execute, command_timeout_seconds, }; @@ -4983,7 +5413,7 @@ fn unix_epoch_millis() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises daemon-published CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. Epiphany health additionally requires a pinned host identity and a signed release-bound runtime-health document. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." } #[cfg(test)] @@ -5348,6 +5778,7 @@ mod tests { operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, execute: true, command_timeout_seconds: 1, }; @@ -6671,7 +7102,7 @@ mod tests { } #[test] - fn evaluate_target_health_keeps_fresh_daemon_published_record_on_owner_key() { + fn evaluate_target_health_refuses_bare_daemon_published_row_without_admission() { let now_seconds = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() @@ -6690,6 +7121,7 @@ mod tests { operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -6736,11 +7168,13 @@ mod tests { }) .unwrap(); - let (health_key, selected) = + let (health_key, selected, authenticated) = evaluate_target_health(&target, &options, &store_lock, &desired, &now).unwrap(); - assert_eq!(health_key, "test-daemon"); - assert_eq!(selected.publication_source, "daemon-published"); + assert_eq!(health_key, "observation:test-daemon"); + assert_eq!(selected.publication_source, "idunn-supervisor-observation"); + assert_ne!(selected.state, "active"); + assert!(!authenticated); let _ = std::fs::remove_file(store_path); } @@ -6760,6 +7194,7 @@ mod tests { operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -6791,13 +7226,14 @@ mod tests { observed_at: "unix:100".to_string(), }; - let (health_key, selected) = + let (health_key, selected, authenticated) = evaluate_target_health(&target, &options, &store_lock, &desired, "unix:100").unwrap(); assert_eq!(health_key, "observation:test-daemon"); assert_eq!(selected.state, "dependency-unavailable"); assert_eq!(selected.publication_source, "idunn-supervisor-observation"); assert_eq!(selected.transport, "cultmesh.missing-daemon-publication"); + assert!(!authenticated); assert!( selected .detail @@ -6842,7 +7278,14 @@ mod tests { let mut missing_since = None; let mut health = missing_daemon_published_health(&target, &desired, "unix:100"); - apply_missing_publication_grace(&target, &desired, &mut health, &mut missing_since, now); + apply_missing_publication_grace( + &target, + &desired, + &mut health, + false, + &mut missing_since, + now, + ); assert_eq!(health.state, "degraded"); assert!(missing_since.is_some()); @@ -6857,6 +7300,7 @@ mod tests { &target, &desired, &mut health, + false, &mut missing_since, now + Duration::from_secs(61), ); @@ -6870,7 +7314,7 @@ mod tests { } #[test] - fn daemon_publication_resets_missing_continuity() { + fn only_authenticated_daemon_publication_resets_missing_continuity() { let target = DaemonTarget { daemon_id: "test-daemon".to_string(), verse_id: "test.local".to_string(), @@ -6915,12 +7359,22 @@ mod tests { &target, &desired, &mut health, + false, &mut missing_since, Instant::now(), ); - assert!(missing_since.is_none()); + assert!(missing_since.is_some()); assert_eq!(health.state, "active"); + apply_missing_publication_grace( + &target, + &desired, + &mut health, + true, + &mut missing_since, + Instant::now(), + ); + assert!(missing_since.is_none()); } #[test] @@ -7262,7 +7716,7 @@ mod tests { } #[test] - fn rudp_health_ingress_accepts_raw_daemon_health_document() { + fn rudp_health_ingress_quarantines_unsigned_health_without_authority() { let health = IdunnDaemonHealthRecord { daemon_id: "test-daemon".to_string(), state: "active".to_string(), @@ -7296,19 +7750,228 @@ mod tests { }, }; + let root = tempfile::tempdir().unwrap(); let options = CommonOptions { - store_path: PathBuf::from("unused.cc"), + store_path: root.path().join("idunn.cc"), release_authority_store_path: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, execute: false, command_timeout_seconds: 1, }; - assert_eq!( - health_from_rudp_message(&message, &options).unwrap(), - health + let lock = Arc::new(Mutex::new(())); + let outcome = admit_health_from_rudp_message( + &message, + &options, + &lock, + "2026-06-15T00:00:01Z", + ) + .unwrap(); + assert_eq!(outcome.authority, "diagnostic-only"); + assert!(health_from_rudp_message(&message, &options).is_err()); + with_store_node(&options, &lock, |node| { + assert!(node.get::(&health.daemon_id)?.is_none()); + let diagnostic = node + .get::(&format!( + "diagnostic:{}", + health.daemon_id + ))? + .expect("unsigned diagnostic"); + assert_eq!(diagnostic.claimed_state, "active"); + assert_eq!(diagnostic.authority, "diagnostic-only"); + Ok(()) + }) + .unwrap(); + } + + fn generic_signed_health_fixture( + sequence: u64, + ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { + let root = tempfile::tempdir().unwrap(); + let trust_path = root.path().join("health-trust.cc"); + let store_path = root.path().join("idunn.cc"); + let key = SigningKey::from_bytes(&[31; 32]); + let public_key = key.verifying_key().to_bytes().to_vec(); + let identity_id = format!( + "{:x}", + Sha256::digest([HOST_IDENTITY_DOMAIN, public_key.as_slice()].concat()) ); + let binding = IdunnDaemonHealthTrustBindingRecord { + schema_version: odin_core::IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), + binding_id: "root/test-daemon/health".into(), + daemon_id: "test-daemon".into(), + health_contract: "test.signed-health".into(), + source_runtime_id: "test-runtime".into(), + signer_identity_id: identity_id.clone(), + signer_public_key: public_key, + binding_authority: "root".into(), + bound_at_unix_millis: 1_784_483_100_000, + release_binding_required: false, + private_state_exposed: false, + }; + SingleFileMessagePackBackingStore::new(&trust_path) + .push(&typed_envelope(&binding.binding_id, &binding, "unix:1784483100").unwrap()) + .unwrap(); + let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339( + "2026-07-19T12:00:00Z", + ) + .unwrap() + .timestamp_millis() as u64; + let mut statement = IdunnSignedDaemonHealthRecord { + schema_version: IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), + daemon_id: binding.daemon_id.clone(), + health_contract: binding.health_contract.clone(), + source_runtime_id: binding.source_runtime_id.clone(), + state: "active".into(), + detail: "signed provider health".into(), + signer_identity_id: identity_id, + publisher_incarnation_id: "00000000-0000-4000-8000-000000000031".into(), + publisher_sequence: sequence, + observed_at_unix_millis, + release_id: None, + release_witness_sha256: None, + source_commit: None, + deployment_id: None, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + private_state_exposed: false, + }; + let unsigned = rmp_serde::to_vec(&statement).unwrap(); + statement.signature = key + .sign(&host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned)) + .to_bytes() + .to_vec(); + let message = CultNetMessage::DocumentPutRaw { + message_id: format!("signed-health-{sequence}"), + document: CultNetRawDocumentRecord { + schema_id: SIGNED_DAEMON_HEALTH_TYPE.into(), + record_key: statement.daemon_id.clone(), + stored_at: "2026-07-19T12:00:00Z".into(), + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload: rmp_serde::to_vec(&statement).unwrap(), + source_runtime_id: Some(statement.source_runtime_id.clone()), + source_agent_id: Some(statement.signer_identity_id.clone()), + source_role: Some("daemon-health-publisher".into()), + tags: Some(vec![CULTNET_RUDP_PROTOCOL_ID.into()]), + }, + }; + ( + message, + CommonOptions { + store_path, + release_authority_store_path: None, + operator_alarm_command: None, + rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: Some(trust_path), + execute: false, + command_timeout_seconds: 1, + }, + root, + ) + } + + #[test] + fn generic_signed_health_requires_root_binding_and_monotonic_admission() { + let (first, options, _root) = generic_signed_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + let outcome = admit_health_from_rudp_message( + &first, + &options, + &lock, + "2026-07-19T12:00:01Z", + ) + .unwrap(); + assert_eq!(outcome.authority, "authenticated"); + assert!( + admit_health_from_rudp_message( + &first, + &options, + &lock, + "2026-07-19T12:00:02Z" + ) + .is_err() + ); + with_store_node(&options, &lock, |node| { + let health = node + .get::("test-daemon")? + .expect("authenticated health"); + assert_eq!(health.publication_source, "daemon-authenticated"); + let admission = node + .get::("test-daemon")? + .expect("generic admission"); + assert_eq!(admission.publisher_sequence, 1); + assert_eq!(admission.trust_binding_id, "root/test-daemon/health"); + let statement = node + .get::(&admission.signed_health_sha256)? + .expect("signed statement"); + assert_eq!( + admission.signed_health_sha256, + format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(&statement)?) + ) + ); + let (_, binding_sha256) = load_daemon_health_trust_binding(&options, &statement)?; + assert_eq!(admission.trust_binding_sha256, binding_sha256); + Ok(()) + }) + .unwrap(); + let target = DaemonTarget { + daemon_id: "test-daemon".into(), + verse_id: "test.local".into(), + name: "Test daemon".into(), + health_contract: health_contract("test.signed-health", "failed"), + deploy_command: None, + restart_command: Some("restart test".into()), + release: None, + enabled: true, + interval_seconds: 30, + }; + let desired = IdunnDesiredDaemonRecord { + daemon_id: target.daemon_id.clone(), + verse_id: target.verse_id.clone(), + name: target.name.clone(), + enabled: true, + health_command: None, + restart_command: target.restart_command.clone(), + deploy_command: None, + authority: "idunn-supervisor-command".into(), + max_silence_seconds: 60, + observed_at: "2026-07-19T12:00:02Z".into(), + health_contract: target.health_contract.id.clone(), + transport_profile_id: transport_profile_id(&target), + command_boundary_id: command_boundary_id(&target), + }; + let (health_key, selected, authenticated) = evaluate_target_health( + &target, + &options, + &lock, + &desired, + "2026-07-19T12:00:02Z", + ) + .unwrap(); + assert_eq!(health_key, "test-daemon"); + assert_eq!(selected.publication_source, "daemon-authenticated"); + assert!(authenticated); + + let (mut forged, mut no_trust, _other_root) = generic_signed_health_fixture(2); + no_trust.daemon_health_trust_store_path = None; + assert!( + admit_health_from_rudp_message( + &forged, + &no_trust, + &Arc::new(Mutex::new(())), + "2026-07-19T12:00:01Z" + ) + .is_err() + ); + if let CultNetMessage::DocumentPutRaw { document, .. } = &mut forged { + document.payload[0] ^= 1; + } + assert!(health_from_rudp_message(&forged, &options).is_err()); } fn signed_epiphany_health_fixture( @@ -7396,6 +8059,7 @@ mod tests { operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: Some(identity_path), + daemon_health_trust_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -7696,6 +8360,7 @@ mod tests { operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, execute: false, command_timeout_seconds: 1, }; diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 2886320..f5e82c0 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -40,6 +40,10 @@ pub const IDUNN_RUNTIME_TRANSPORT_CHECK_SCHEMA: &str = "idunn.runtime_transport_ pub const IDUNN_RUDP_HEALTH_INGRESS_SCHEMA: &str = "idunn.rudp_health_ingress.v1"; pub const IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA: &str = "idunn.signed_daemon_health.v1"; pub const IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA: &str = "idunn.daemon_health_trust_binding.v1"; +pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = + "idunn.authenticated_daemon_health_admission.v1"; +pub const IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA: &str = + "idunn.unsigned_daemon_health_diagnostic.v1"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; pub const MUNINN_CAPTURE_STREAM_SCHEMA: &str = "muninn.capture_stream.v1"; pub const MUNINN_CAPTURE_STREAM_COMMAND_SCHEMA: &str = "muninn.capture_stream_command.v1"; @@ -452,6 +456,148 @@ impl IdunnDaemonHealthTrustBindingRecord { } } +/// Idunn-owned judgment joining one verified provider statement to the exact +/// root trust binding used to admit it. Managed-health reads must rejoin this +/// record with both current objects; a health row by itself is not authority. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.authenticated_daemon_health_admission", + schema = "idunn.authenticated_daemon_health_admission.v1" +)] +pub struct IdunnAuthenticatedDaemonHealthAdmissionRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub daemon_id: String, + #[cultcache(key = 2)] + pub health_contract: String, + #[cultcache(key = 3)] + pub source_runtime_id: String, + #[cultcache(key = 4)] + pub state: String, + #[cultcache(key = 5)] + pub observed_at_unix_millis: u64, + #[cultcache(key = 6)] + pub admitted_at_unix_millis: u64, + #[cultcache(key = 7)] + pub trust_binding_id: String, + #[cultcache(key = 8)] + pub trust_binding_sha256: String, + #[cultcache(key = 9)] + pub signer_identity_id: String, + #[cultcache(key = 10)] + pub publisher_incarnation_id: String, + #[cultcache(key = 11)] + pub publisher_sequence: u64, + #[cultcache(key = 12)] + pub signed_health_sha256: String, + #[cultcache(key = 13)] + pub release_id: Option, + #[cultcache(key = 14)] + pub release_witness_sha256: Option, + #[cultcache(key = 15)] + pub source_commit: Option, + #[cultcache(key = 16)] + pub deployment_id: Option, + #[cultcache(key = 17)] + pub private_state_exposed: bool, +} + +impl IdunnAuthenticatedDaemonHealthAdmissionRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA { + bail!("authenticated daemon health admission schema is unsupported"); + } + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.health_contract, "health contract")?; + validate_identifier(&self.source_runtime_id, "source runtime id")?; + validate_identifier(&self.trust_binding_id, "trust binding id")?; + validate_identifier(&self.signer_identity_id, "signer identity id")?; + validate_identifier(&self.publisher_incarnation_id, "publisher incarnation id")?; + if !matches!( + self.state.as_str(), + "active" | "warming" | "degraded" | "failed" + ) || self.observed_at_unix_millis == 0 + || self.admitted_at_unix_millis < self.observed_at_unix_millis + || self.publisher_sequence == 0 + || !is_sha256(&self.trust_binding_sha256) + || !is_sha256(&self.signed_health_sha256) + { + bail!("authenticated daemon health admission shape is invalid"); + } + validate_optional_release_binding( + &self.release_id, + &self.release_witness_sha256, + &self.source_commit, + )?; + validate_optional_identifier(&self.deployment_id, "deployment id")?; + if self.private_state_exposed { + bail!("authenticated daemon health admission exposes private state"); + } + Ok(()) + } +} + +/// Quarantined observation of the retired unsigned wire contract. This may +/// explain missing authenticated health but can never satisfy managed health. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.unsigned_daemon_health_diagnostic", + schema = "idunn.unsigned_daemon_health_diagnostic.v1" +)] +pub struct IdunnUnsignedDaemonHealthDiagnosticRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub diagnostic_id: String, + #[cultcache(key = 2)] + pub daemon_id: String, + #[cultcache(key = 3)] + pub claimed_state: String, + #[cultcache(key = 4)] + pub claimed_detail: String, + #[cultcache(key = 5)] + pub claimed_observed_at: String, + #[cultcache(key = 6)] + pub claimed_health_contract: String, + #[cultcache(key = 7)] + pub transport_source_runtime_id: Option, + #[cultcache(key = 8)] + pub transport_source_role: Option, + #[cultcache(key = 9)] + pub received_at_unix_millis: u64, + #[cultcache(key = 10)] + pub authority: String, + #[cultcache(key = 11)] + pub private_state_exposed: bool, +} + +impl IdunnUnsignedDaemonHealthDiagnosticRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA { + bail!("unsigned daemon health diagnostic schema is unsupported"); + } + validate_identifier(&self.diagnostic_id, "diagnostic id")?; + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.claimed_state, "claimed state")?; + validate_identifier(&self.claimed_health_contract, "claimed health contract")?; + validate_optional_identifier( + &self.transport_source_runtime_id, + "transport source runtime id", + )?; + validate_optional_identifier(&self.transport_source_role, "transport source role")?; + if self.claimed_detail.len() > 512 + || self.claimed_observed_at.trim().is_empty() + || self.received_at_unix_millis == 0 + || self.authority != "diagnostic-only" + || self.private_state_exposed + { + bail!("unsigned daemon health diagnostic shape is invalid"); + } + Ok(()) + } +} + fn validate_identifier(value: &str, label: &str) -> Result<()> { if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { bail!("{label} is empty, oversized, or contains control characters"); @@ -1708,6 +1854,8 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { IdunnSignedHealthAdmissionRecord => IDUNN_SIGNED_HEALTH_ADMISSION_SCHEMA, IdunnSignedDaemonHealthRecord => IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IdunnDaemonHealthTrustBindingRecord => IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + IdunnAuthenticatedDaemonHealthAdmissionRecord => IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, + IdunnUnsignedDaemonHealthDiagnosticRecord => IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, IdunnKeepaliveDecisionRecord => IDUNN_KEEPALIVE_DECISION_SCHEMA, IdunnRestartRequestRecord => IDUNN_RESTART_REQUEST_SCHEMA, IdunnRestartResultRecord => IDUNN_RESTART_RESULT_SCHEMA, @@ -1806,12 +1954,39 @@ mod tests { } } + fn authenticated_admission_fixture() -> IdunnAuthenticatedDaemonHealthAdmissionRecord { + let health = signed_health_fixture(); + let binding = trust_binding_fixture(); + IdunnAuthenticatedDaemonHealthAdmissionRecord { + schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), + daemon_id: health.daemon_id, + health_contract: health.health_contract, + source_runtime_id: health.source_runtime_id, + state: health.state, + observed_at_unix_millis: health.observed_at_unix_millis, + admitted_at_unix_millis: health.observed_at_unix_millis + 1, + trust_binding_id: binding.binding_id, + trust_binding_sha256: format!("sha256-{}", "c".repeat(64)), + signer_identity_id: health.signer_identity_id, + publisher_incarnation_id: health.publisher_incarnation_id, + publisher_sequence: health.publisher_sequence, + signed_health_sha256: format!("sha256-{}", "d".repeat(64)), + release_id: health.release_id, + release_witness_sha256: health.release_witness_sha256, + source_commit: health.source_commit, + deployment_id: health.deployment_id, + private_state_exposed: false, + } + } + #[test] fn generic_signed_health_contracts_validate_and_round_trip() -> Result<()> { let health = signed_health_fixture(); let binding = trust_binding_fixture(); + let admission = authenticated_admission_fixture(); health.validate()?; binding.validate()?; + admission.validate()?; let temp = tempfile::tempdir()?; let store_path = temp.path().join("signed-health.cc"); @@ -1825,6 +2000,7 @@ mod tests { )?; node.put(&health.daemon_id, &health)?; node.put(&binding.binding_id, &binding)?; + node.put(&admission.daemon_id, &admission)?; assert_eq!( node.get_required::(&health.daemon_id)?, health @@ -1833,6 +2009,12 @@ mod tests { node.get_required::(&binding.binding_id)?, binding ); + assert_eq!( + node.get_required::( + &admission.daemon_id + )?, + admission + ); Ok(()) } @@ -1860,6 +2042,13 @@ mod tests { binding = trust_binding_fixture(); binding.private_state_exposed = true; assert!(binding.validate().is_err()); + + let mut admission = authenticated_admission_fixture(); + admission.trust_binding_sha256 = "ready".into(); + assert!(admission.validate().is_err()); + admission = authenticated_admission_fixture(); + admission.admitted_at_unix_millis = admission.observed_at_unix_millis - 1; + assert!(admission.validate().is_err()); } #[test] diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index 114ea72..6035081 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -99,6 +99,34 @@ must absorb its invariants before the special path is deleted. The rebuild is incomplete while an unsigned packet can keep a managed daemon healthy or while a status consumer must inspect Idunn's private store. +## Implemented cut (2026-07-19) + +The deployed-lineage source now has the first executable generic admission +path. `idunn.signed_daemon_health.v1` is verified against an exact root-store +daemon/contract/runtime binding, the signer identity is derived from the bound +public key, the Ed25519 signature covers the positional empty-signature +statement under the domain-separated document purpose, and Idunn atomically +persists the signed statement, its generic admission, and the compatibility +health row. Managed-health selection rejoins all three with the current root +binding on every read; trust rotation invalidates old health. Sequence, +incarnation, observation, freshness, and optional deployment lineage advance +under CAS. + +Unsigned `idunn.daemon_health.v1` packets now persist only as +`idunn.unsigned_daemon_health_diagnostic.v1` under `diagnostic:`. +They cannot overwrite the daemon health key, create an admission, reset the +missing-publication clock, suppress recovery, or produce authenticated health. +The evaluation clock is passed through the whole target cycle instead of being +resampled inside the health selector. + +This source is deliberately not deployable yet: Bifrost and the remaining +generic publishers still emit the unsigned diagnostic contract, the root trust +store has not been installed, Epiphany still uses its signed v0 migration path, +and Idunn has no signed outward status projection. Deploying this intermediate +source would correctly classify unmigrated daemons as missing health and could +therefore actuate recovery. Publisher migration and the outward projection must +land before promotion. + ## Verification layer Hostile tests must prove: From 1240ca83a405354e70f704681f6b7202902e7187 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:06:43 +0100 Subject: [PATCH 15/93] Define signed Idunn health projection contracts --- crates/odin-core/src/documents.rs | 309 ++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index f5e82c0..7c6719f 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -44,6 +44,8 @@ pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = "idunn.authenticated_daemon_health_admission.v1"; pub const IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA: &str = "idunn.unsigned_daemon_health_diagnostic.v1"; +pub const GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA: &str = "gamecult.service_trust_anchor.v1"; +pub const IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA: &str = "idunn.managed_health_projection.v1"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; pub const MUNINN_CAPTURE_STREAM_SCHEMA: &str = "muninn.capture_stream.v1"; pub const MUNINN_CAPTURE_STREAM_COMMAND_SCHEMA: &str = "muninn.capture_stream_command.v1"; @@ -598,6 +600,187 @@ impl IdunnUnsignedDaemonHealthDiagnosticRecord { } } +/// Root-distributed public key binding for one service-owned signed contract. +/// Consumers pin this document; a self-declared key inside a signed projection +/// is never authority. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "gamecult.service_trust_anchor", + schema = "gamecult.service_trust_anchor.v1" +)] +pub struct GameCultServiceTrustAnchorRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub trust_anchor_id: String, + #[cultcache(key = 2)] + pub service_id: String, + #[cultcache(key = 3)] + pub runtime_id: String, + #[cultcache(key = 4)] + pub signer_identity_id: String, + #[cultcache(key = 5)] + pub signer_public_key: Vec, + #[cultcache(key = 6)] + pub signature_algorithm: String, + #[cultcache(key = 7)] + pub signing_purpose: String, + #[cultcache(key = 8)] + pub signed_schema: String, + #[cultcache(key = 9)] + pub binding_authority: String, + #[cultcache(key = 10)] + pub bound_at_unix_millis: u64, + #[cultcache(key = 11)] + pub expires_at_unix_millis: Option, + #[cultcache(key = 12)] + pub private_state_exposed: bool, +} + +impl GameCultServiceTrustAnchorRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA { + bail!("service trust anchor schema is unsupported"); + } + validate_identifier(&self.trust_anchor_id, "trust anchor id")?; + validate_identifier(&self.service_id, "service id")?; + validate_identifier(&self.runtime_id, "runtime id")?; + validate_identifier(&self.signer_identity_id, "signer identity id")?; + validate_identifier(&self.signing_purpose, "signing purpose")?; + validate_identifier(&self.signed_schema, "signed schema")?; + if self.signer_public_key.len() != 32 + || self.signature_algorithm != "ed25519" + || self.binding_authority != "root" + || self.bound_at_unix_millis == 0 + || self + .expires_at_unix_millis + .is_some_and(|expires_at| expires_at <= self.bound_at_unix_millis) + || self.private_state_exposed + { + bail!("service trust anchor authority, key, lifetime, or privacy is invalid"); + } + Ok(()) + } +} + +/// Idunn-owned, public, signed judgment over one authenticated provider health +/// admission. The signature covers the complete positional document with the +/// signature field empty. Consumers verify it against a separately pinned +/// `GameCultServiceTrustAnchorRecord` and apply their own freshness bound. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.managed_health_projection", + schema = "idunn.managed_health_projection.v1" +)] +pub struct IdunnManagedHealthProjectionRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub projection_id: String, + #[cultcache(key = 2)] + pub daemon_id: String, + #[cultcache(key = 3)] + pub health_contract: String, + #[cultcache(key = 4)] + pub managed_state: String, + #[cultcache(key = 5)] + pub detail: String, + #[cultcache(key = 6)] + pub provider_observed_at_unix_millis: u64, + #[cultcache(key = 7)] + pub admitted_at_unix_millis: u64, + #[cultcache(key = 8)] + pub evaluated_at_unix_millis: u64, + #[cultcache(key = 9)] + pub trust_binding_id: String, + #[cultcache(key = 10)] + pub trust_binding_sha256: String, + #[cultcache(key = 11)] + pub signed_health_sha256: String, + #[cultcache(key = 12)] + pub authenticated_admission_sha256: String, + #[cultcache(key = 13)] + pub provider_signer_identity_id: String, + #[cultcache(key = 14)] + pub provider_incarnation_id: String, + #[cultcache(key = 15)] + pub provider_sequence: u64, + #[cultcache(key = 16)] + pub release_id: Option, + #[cultcache(key = 17)] + pub release_witness_sha256: Option, + #[cultcache(key = 18)] + pub source_commit: Option, + #[cultcache(key = 19)] + pub deployment_id: Option, + #[cultcache(key = 20)] + pub idunn_runtime_id: String, + #[cultcache(key = 21)] + pub idunn_signer_identity_id: String, + #[cultcache(key = 22)] + pub projection_incarnation_id: String, + #[cultcache(key = 23)] + pub projection_sequence: u64, + #[cultcache(key = 24)] + pub signature_algorithm: String, + #[cultcache(key = 25)] + pub signature: Vec, + #[cultcache(key = 26)] + pub private_state_exposed: bool, + #[cultcache(key = 27)] + pub expires_at_unix_millis: u64, +} + +impl IdunnManagedHealthProjectionRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA { + bail!("managed health projection schema is unsupported"); + } + validate_identifier(&self.projection_id, "projection id")?; + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.health_contract, "health contract")?; + validate_identifier(&self.trust_binding_id, "trust binding id")?; + validate_identifier( + &self.provider_signer_identity_id, + "provider signer identity id", + )?; + validate_identifier(&self.provider_incarnation_id, "provider incarnation id")?; + validate_identifier(&self.idunn_runtime_id, "Idunn runtime id")?; + validate_identifier(&self.idunn_signer_identity_id, "Idunn signer identity id")?; + validate_identifier(&self.projection_incarnation_id, "projection incarnation id")?; + if !matches!( + self.managed_state.as_str(), + "active" | "warming" | "degraded" | "failed" + ) || self.detail.len() > 512 + || self.provider_observed_at_unix_millis == 0 + || self.admitted_at_unix_millis < self.provider_observed_at_unix_millis + || self.evaluated_at_unix_millis < self.admitted_at_unix_millis + || self.expires_at_unix_millis <= self.evaluated_at_unix_millis + || self.provider_sequence == 0 + || self.projection_sequence == 0 + || !is_sha256(&self.trust_binding_sha256) + || !is_sha256(&self.signed_health_sha256) + || !is_sha256(&self.authenticated_admission_sha256) + || self.signature_algorithm != "ed25519" + || self.signature.len() != 64 + || self.private_state_exposed + { + bail!("managed health projection shape, lineage, signature, or privacy is invalid"); + } + validate_optional_release_binding( + &self.release_id, + &self.release_witness_sha256, + &self.source_commit, + )?; + validate_optional_identifier(&self.deployment_id, "deployment id")?; + let release_binding_present = self.release_id.is_some(); + if self.deployment_id.is_some() != release_binding_present { + bail!("managed health projection release lineage is partial"); + } + Ok(()) + } +} + fn validate_identifier(value: &str, label: &str) -> Result<()> { if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { bail!("{label} is empty, oversized, or contains control characters"); @@ -1856,6 +2039,8 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { IdunnDaemonHealthTrustBindingRecord => IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnAuthenticatedDaemonHealthAdmissionRecord => IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, IdunnUnsignedDaemonHealthDiagnosticRecord => IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, + GameCultServiceTrustAnchorRecord => GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + IdunnManagedHealthProjectionRecord => IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA, IdunnKeepaliveDecisionRecord => IDUNN_KEEPALIVE_DECISION_SCHEMA, IdunnRestartRequestRecord => IDUNN_RESTART_REQUEST_SCHEMA, IdunnRestartResultRecord => IDUNN_RESTART_RESULT_SCHEMA, @@ -1979,6 +2164,130 @@ mod tests { } } + fn service_trust_anchor_fixture() -> GameCultServiceTrustAnchorRecord { + GameCultServiceTrustAnchorRecord { + schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), + trust_anchor_id: "root/idunn/managed-health-projection".into(), + service_id: "idunn".into(), + runtime_id: "idunn-yggdrasil".into(), + signer_identity_id: "idunn-yggdrasil-service-identity".into(), + signer_public_key: vec![5; 32], + signature_algorithm: "ed25519".into(), + signing_purpose: "idunn.managed_health_projection".into(), + signed_schema: IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA.into(), + binding_authority: "root".into(), + bound_at_unix_millis: 1_784_483_100_000, + expires_at_unix_millis: Some(1_815_000_000_000), + private_state_exposed: false, + } + } + + fn managed_health_projection_fixture() -> IdunnManagedHealthProjectionRecord { + let health = signed_health_fixture(); + let binding = trust_binding_fixture(); + IdunnManagedHealthProjectionRecord { + schema_version: IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA.into(), + projection_id: format!("managed-health:{}", health.daemon_id), + daemon_id: health.daemon_id, + health_contract: health.health_contract, + managed_state: health.state, + detail: "Idunn admits current provider-authenticated health".into(), + provider_observed_at_unix_millis: health.observed_at_unix_millis, + admitted_at_unix_millis: health.observed_at_unix_millis + 1, + evaluated_at_unix_millis: health.observed_at_unix_millis + 2, + trust_binding_id: binding.binding_id, + trust_binding_sha256: format!("sha256-{}", "c".repeat(64)), + signed_health_sha256: format!("sha256-{}", "d".repeat(64)), + authenticated_admission_sha256: format!("sha256-{}", "e".repeat(64)), + provider_signer_identity_id: health.signer_identity_id, + provider_incarnation_id: health.publisher_incarnation_id, + provider_sequence: health.publisher_sequence, + release_id: health.release_id, + release_witness_sha256: health.release_witness_sha256, + source_commit: health.source_commit, + deployment_id: health.deployment_id, + idunn_runtime_id: "idunn-yggdrasil".into(), + idunn_signer_identity_id: "idunn-yggdrasil-service-identity".into(), + projection_incarnation_id: "idunn-yggdrasil/boot-3/process-8".into(), + projection_sequence: 7, + signature_algorithm: "ed25519".into(), + signature: vec![6; 64], + private_state_exposed: false, + expires_at_unix_millis: health.observed_at_unix_millis + 60_002, + } + } + + #[test] + fn outward_managed_health_contracts_validate_and_round_trip() -> Result<()> { + let anchor = service_trust_anchor_fixture(); + let projection = managed_health_projection_fixture(); + anchor.validate()?; + projection.validate()?; + + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("managed-health-projection.cc"); + let mut node = CultMesh::create_node( + &store_path, + OdinDocuments, + CultMeshNodeOptions { + runtime_id: "managed-health-contract-test".into(), + pull_on_start: true, + }, + )?; + node.put(&anchor.trust_anchor_id, &anchor)?; + node.put(&projection.projection_id, &projection)?; + assert_eq!( + node.get_required::(&anchor.trust_anchor_id)?, + anchor + ); + assert_eq!( + node.get_required::(&projection.projection_id)?, + projection + ); + Ok(()) + } + + #[test] + fn outward_managed_health_contracts_refuse_weak_authority_and_lineage() { + let mut anchor = service_trust_anchor_fixture(); + anchor.binding_authority = "idunn".into(); + assert!(anchor.validate().is_err()); + anchor = service_trust_anchor_fixture(); + anchor.signer_public_key = vec![5; 31]; + assert!(anchor.validate().is_err()); + anchor = service_trust_anchor_fixture(); + anchor.expires_at_unix_millis = Some(anchor.bound_at_unix_millis); + assert!(anchor.validate().is_err()); + anchor = service_trust_anchor_fixture(); + anchor.private_state_exposed = true; + assert!(anchor.validate().is_err()); + + let mut projection = managed_health_projection_fixture(); + projection.evaluated_at_unix_millis = projection.admitted_at_unix_millis - 1; + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.expires_at_unix_millis = projection.evaluated_at_unix_millis; + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.authenticated_admission_sha256 = "admitted".into(); + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.projection_sequence = 0; + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.signature.clear(); + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.release_witness_sha256 = None; + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.deployment_id = None; + assert!(projection.validate().is_err()); + projection = managed_health_projection_fixture(); + projection.private_state_exposed = true; + assert!(projection.validate().is_err()); + } + #[test] fn generic_signed_health_contracts_validate_and_round_trip() -> Result<()> { let health = signed_health_fixture(); From c05e37ca6e94e4fdfbb48d6706ff9e4c2f84eb83 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:08:40 +0100 Subject: [PATCH 16/93] Add typed service signing identities to CultNet --- Cargo.lock | 2 + vendor/cultnet-rs/Cargo.toml | 4 + vendor/cultnet-rs/src/lib.rs | 2 + vendor/cultnet-rs/src/service_identity.rs | 568 ++++++++++++++++++++++ 4 files changed, 576 insertions(+) create mode 100644 vendor/cultnet-rs/src/service_identity.rs diff --git a/Cargo.lock b/Cargo.lock index e3c6d0a..953c54f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,6 +216,7 @@ dependencies = [ "base64", "chrono", "cultcache-rs", + "ed25519-dalek", "hmac", "rand", "rmp-serde", @@ -226,6 +227,7 @@ dependencies = [ "sha2", "socket2", "uuid", + "windows-sys 0.61.2", ] [[package]] diff --git a/vendor/cultnet-rs/Cargo.toml b/vendor/cultnet-rs/Cargo.toml index 41d14b5..1334626 100644 --- a/vendor/cultnet-rs/Cargo.toml +++ b/vendor/cultnet-rs/Cargo.toml @@ -16,6 +16,7 @@ anyhow = "1" base64 = "0.22" chrono = { version = "0.4", features = ["serde"] } cultcache-rs = { path = "../cultcache-rs" } +ed25519-dalek = "2.2" hmac = "0.12" rand = "0.9" rmp-serde = "1.3" @@ -27,6 +28,9 @@ sha2 = "0.10" socket2 = "0.5" uuid = { version = "1", features = ["v4"] } +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security_Cryptography"] } + [dev-dependencies] pretty_assertions = "1.4" tempfile = "3" diff --git a/vendor/cultnet-rs/src/lib.rs b/vendor/cultnet-rs/src/lib.rs index bc804d3..da2d171 100644 --- a/vendor/cultnet-rs/src/lib.rs +++ b/vendor/cultnet-rs/src/lib.rs @@ -4,6 +4,7 @@ mod replication; mod rudp; mod schema_discovery; mod security; +mod service_identity; mod transport; pub use contracts::*; @@ -12,4 +13,5 @@ pub use replication::*; pub use rudp::*; pub use schema_discovery::*; pub use security::*; +pub use service_identity::*; pub use transport::*; diff --git a/vendor/cultnet-rs/src/service_identity.rs b/vendor/cultnet-rs/src/service_identity.rs new file mode 100644 index 0000000..fc9c69e --- /dev/null +++ b/vendor/cultnet-rs/src/service_identity.rs @@ -0,0 +1,568 @@ +use anyhow::{Context, Result, anyhow, bail}; +use cultcache_rs::{ + CacheBackingStore, CultCacheEnvelope, CultCacheExpectedEnvelope, + SingleFileMessagePackBackingStore, +}; +use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey}; +use rand::RngCore; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::marker::PhantomData; +use std::path::Path; + +pub const WINDOWS_SERVICE_IDENTITY_ASSURANCE: &str = "os_user_installation_bound_best_effort"; +pub const LINUX_SERVICE_IDENTITY_ASSURANCE: &str = "os_installation_file_bound_cloneable_baseline"; + +/// Fixes every authority-bearing name at the call site. A service cannot open +/// another service's identity by supplying a convenient runtime string. +pub trait ServiceIdentityProfile: Send + Sync + 'static { + const PRIVATE_TYPE: &'static str; + const PRIVATE_SCHEMA: &'static str; + const PRIVATE_KEY: &'static str; + const TRUST_ANCHOR_TYPE: &'static str; + const TRUST_ANCHOR_SCHEMA: &'static str; + const TRUST_ANCHOR_KEY: &'static str; + const ID_DOMAIN: &'static [u8]; + const SIGNATURE_DOMAIN: &'static [u8]; + const PROTECTOR_CONTEXT: &'static str; +} + +/// A purpose is a type, not caller-provided prose. Protocol implementations +/// define one zero-sized type per signed statement kind. +pub trait ServiceSignaturePurpose: Send + Sync + 'static { + const PURPOSE: &'static [u8]; +} + +pub enum IdunnServiceIdentity {} + +impl ServiceIdentityProfile for IdunnServiceIdentity { + const PRIVATE_TYPE: &'static str = "idunn.service_identity.private.v1"; + const PRIVATE_SCHEMA: &'static str = "idunn.service_identity.private.v1"; + const PRIVATE_KEY: &'static str = "idunn-service-identity"; + const TRUST_ANCHOR_TYPE: &'static str = "idunn.service_identity.trust_anchor.v1"; + const TRUST_ANCHOR_SCHEMA: &'static str = "idunn.service_identity.trust_anchor.v1"; + const TRUST_ANCHOR_KEY: &'static str = "idunn-service-identity-public"; + const ID_DOMAIN: &'static [u8] = b"idunn.service-identity.id.v1\0"; + const SIGNATURE_DOMAIN: &'static [u8] = b"idunn.service-identity.signature.v1\0"; + const PROTECTOR_CONTEXT: &'static str = "idunn-service-identity-v1"; +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ServiceIdentityPrivateEntry { + pub schema_version: String, + pub identity_id: String, + pub public_key: Vec, + pub protected_private_seed: Vec, + pub protector_kind: String, + pub protector_binding: String, + pub protector_version: String, + pub assurance: String, + pub created_at: String, + pub enrollment_nonce: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ServiceIdentityTrustAnchor { + pub schema_version: String, + pub identity_id: String, + pub public_key: Vec, + pub assurance: String, + pub identity_created_at: String, + pub source_identity_record_sha256: String, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ServiceIdentitySignature { + pub identity_id: String, + pub signature: Vec, +} + +/// The only value holding unprotected key material. It exposes signing, never +/// seed export or access to the underlying signing key. +pub struct ServiceIdentitySigner { + entry: ServiceIdentityPrivateEntry, + signing_key: SigningKey, + profile: PhantomData

, +} + +impl ServiceIdentitySigner

{ + pub fn entry(&self) -> &ServiceIdentityPrivateEntry { + &self.entry + } + + pub fn trust_anchor(&self) -> Result { + Ok(ServiceIdentityTrustAnchor { + schema_version: P::TRUST_ANCHOR_SCHEMA.into(), + identity_id: self.entry.identity_id.clone(), + public_key: self.entry.public_key.clone(), + assurance: self.entry.assurance.clone(), + identity_created_at: self.entry.created_at.clone(), + source_identity_record_sha256: format!( + "sha256-{}", + hex(&Sha256::digest(rmp_serde::to_vec(&self.entry)?)) + ), + }) + } + + pub fn sign>(&self, payload: &[u8]) -> ServiceIdentitySignature { + ServiceIdentitySignature { + identity_id: self.entry.identity_id.clone(), + signature: self + .signing_key + .sign(&signing_message::(payload)) + .to_bytes() + .to_vec(), + } + } +} + +pub fn enroll_service_identity_at( + path: &Path, +) -> Result> { + if path.exists() { + bail!( + "service identity store {} already exists; enrollment is immutable", + path.display() + ); + } + prepare_parent(path)?; + let mut seed = [0u8; 32]; + rand::rng().fill_bytes(&mut seed); + let signing_key = SigningKey::from_bytes(&seed); + let mut nonce = [0u8; 32]; + rand::rng().fill_bytes(&mut nonce); + let public_key = signing_key.verifying_key().to_bytes(); + let binding = platform_binding::

()?; + let entry = ServiceIdentityPrivateEntry { + schema_version: P::PRIVATE_SCHEMA.into(), + identity_id: identity_id::

(&public_key), + public_key: public_key.to_vec(), + protected_private_seed: protect_seed::

(&seed, &binding)?, + protector_kind: platform_protector_kind().into(), + protector_binding: binding, + protector_version: "v1".into(), + assurance: platform_assurance().into(), + created_at: chrono::Utc::now().to_rfc3339(), + enrollment_nonce: nonce.to_vec(), + }; + validate_private::

(&entry)?; + let envelope = private_envelope::

(&entry)?; + let backing = SingleFileMessagePackBackingStore::new(path); + let expected = CultCacheExpectedEnvelope { + r#type: P::PRIVATE_TYPE.into(), + key: P::PRIVATE_KEY.into(), + current: None, + }; + if !backing.compare_exchange(&[expected], &[envelope])? { + bail!("service identity enrollment lost atomic create race; refusing existing state"); + } + harden_store_permissions(path)?; + open_service_identity_at::

(path) +} + +pub fn open_service_identity_at( + path: &Path, +) -> Result> { + if !path.is_file() { + bail!("service identity store {} does not exist", path.display()); + } + let entries = SingleFileMessagePackBackingStore::new(path).pull_all()?; + if entries.len() != 1 { + bail!("service identity store must contain exactly one immutable envelope"); + } + let envelope = &entries[0]; + if envelope.r#type != P::PRIVATE_TYPE + || envelope.key != P::PRIVATE_KEY + || envelope.schema_id.as_deref() != Some(P::PRIVATE_SCHEMA) + { + bail!("service identity store belongs to a different profile or schema"); + } + let entry: ServiceIdentityPrivateEntry = rmp_serde::from_slice(&envelope.payload) + .context("service identity private payload is malformed MessagePack")?; + validate_private::

(&entry)?; + if entry.protector_kind != platform_protector_kind() || entry.assurance != platform_assurance() + { + bail!("service identity protector does not belong to this platform implementation"); + } + let binding = platform_binding::

()?; + if entry.protector_binding != binding { + bail!("service identity protector binding does not match this OS installation or profile"); + } + let seed: [u8; 32] = unprotect_seed::

(&entry.protected_private_seed, &binding)? + .try_into() + .map_err(|_| anyhow!("unprotected service identity seed has invalid length"))?; + let signing_key = SigningKey::from_bytes(&seed); + if signing_key.verifying_key().to_bytes().as_slice() != entry.public_key.as_slice() { + bail!("service identity private seed does not match enrolled public key"); + } + Ok(ServiceIdentitySigner { + entry, + signing_key, + profile: PhantomData, + }) +} + +pub fn export_service_identity_trust_anchor( + signer: &ServiceIdentitySigner

, + path: &Path, +) -> Result { + let anchor = signer.trust_anchor()?; + prepare_parent(path)?; + let envelope = CultCacheEnvelope { + key: P::TRUST_ANCHOR_KEY.into(), + r#type: P::TRUST_ANCHOR_TYPE.into(), + payload: rmp_serde::to_vec(&anchor)?, + stored_at: anchor.identity_created_at.clone(), + schema_id: Some(P::TRUST_ANCHOR_SCHEMA.into()), + }; + let mut backing = SingleFileMessagePackBackingStore::new(path); + match backing.pull_all()?.as_slice() { + [] => backing.push(&envelope)?, + [current] if current == &envelope => {} + _ => bail!("service identity trust anchor output already contains different state"), + } + Ok(anchor) +} + +pub fn verify_service_identity_signature( + anchor: &ServiceIdentityTrustAnchor, + payload: &[u8], + proof: &ServiceIdentitySignature, +) -> Result<()> +where + P: ServiceIdentityProfile, + S: ServiceSignaturePurpose

, +{ + validate_anchor::

(anchor)?; + if proof.identity_id != anchor.identity_id { + bail!("service identity signature names a different identity"); + } + let key: [u8; 32] = anchor + .public_key + .as_slice() + .try_into() + .map_err(|_| anyhow!("service identity public key has invalid length"))?; + let signature = Signature::from_slice(&proof.signature) + .map_err(|_| anyhow!("service identity signature has invalid length"))?; + VerifyingKey::from_bytes(&key)? + .verify(&signing_message::(payload), &signature) + .map_err(|_| anyhow!("service identity signature verification failed")) +} + +fn validate_private(entry: &ServiceIdentityPrivateEntry) -> Result<()> { + if entry.schema_version != P::PRIVATE_SCHEMA + || entry.public_key.len() != 32 + || entry.enrollment_nonce.len() != 32 + || entry.protected_private_seed.is_empty() + || entry.protector_version != "v1" + { + bail!("service identity private entry violates its profile schema"); + } + chrono::DateTime::parse_from_rfc3339(&entry.created_at) + .map_err(|_| anyhow!("service identity created_at is not RFC3339"))?; + if identity_id::

(&entry.public_key) != entry.identity_id { + bail!("service identity id does not match public key"); + } + Ok(()) +} + +fn validate_anchor(anchor: &ServiceIdentityTrustAnchor) -> Result<()> { + if anchor.schema_version != P::TRUST_ANCHOR_SCHEMA + || anchor.public_key.len() != 32 + || identity_id::

(&anchor.public_key) != anchor.identity_id + { + bail!("service identity trust anchor violates its profile schema"); + } + Ok(()) +} + +fn identity_id(key: &[u8]) -> String { + hex(&Sha256::digest([P::ID_DOMAIN, key].concat())) +} + +fn signing_message>( + payload: &[u8], +) -> Vec { + let mut out = + Vec::with_capacity(P::SIGNATURE_DOMAIN.len() + S::PURPOSE.len() + payload.len() + 16); + out.extend_from_slice(P::SIGNATURE_DOMAIN); + out.extend_from_slice(&(S::PURPOSE.len() as u64).to_be_bytes()); + out.extend_from_slice(S::PURPOSE); + out.extend_from_slice(&(payload.len() as u64).to_be_bytes()); + out.extend_from_slice(payload); + out +} + +fn private_envelope( + entry: &ServiceIdentityPrivateEntry, +) -> Result { + Ok(CultCacheEnvelope { + key: P::PRIVATE_KEY.into(), + r#type: P::PRIVATE_TYPE.into(), + payload: rmp_serde::to_vec(entry)?, + stored_at: entry.created_at.clone(), + schema_id: Some(P::PRIVATE_SCHEMA.into()), + }) +} + +fn prepare_parent(path: &Path) -> Result<()> { + let parent = path + .parent() + .ok_or_else(|| anyhow!("service identity path has no parent"))?; + std::fs::create_dir_all(parent)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700))?; + } + Ok(()) +} +fn harden_store_permissions(_path: &Path) -> Result<()> { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(_path, std::fs::Permissions::from_mode(0o600))?; + } + Ok(()) +} + +#[cfg(windows)] +fn platform_protector_kind() -> &'static str { + "windows_dpapi_current_user" +} +#[cfg(target_os = "linux")] +fn platform_protector_kind() -> &'static str { + "linux_file_mode_machine_id_binding" +} +#[cfg(windows)] +fn platform_assurance() -> &'static str { + WINDOWS_SERVICE_IDENTITY_ASSURANCE +} +#[cfg(target_os = "linux")] +fn platform_assurance() -> &'static str { + LINUX_SERVICE_IDENTITY_ASSURANCE +} + +#[cfg(windows)] +fn platform_binding() -> Result { + Ok(format!("dpapi-current-user:{}", P::PROTECTOR_CONTEXT)) +} +#[cfg(target_os = "linux")] +fn platform_binding() -> Result { + let raw = std::fs::read_to_string("/etc/machine-id") + .or_else(|_| std::fs::read_to_string("/var/lib/dbus/machine-id")) + .context("Linux machine-id is unavailable")?; + let id = raw.trim(); + if id.is_empty() { + bail!("Linux machine-id is empty"); + } + Ok(format!( + "{}:machine-id-sha256:{}", + P::PROTECTOR_CONTEXT, + hex(&Sha256::digest(id.as_bytes())) + )) +} + +#[cfg(windows)] +fn protect_seed(seed: &[u8; 32], binding: &str) -> Result> { + dpapi::

(seed, binding, true) +} +#[cfg(windows)] +fn unprotect_seed(seed: &[u8], binding: &str) -> Result> { + dpapi::

(seed, binding, false) +} +#[cfg(windows)] +fn dpapi(input: &[u8], binding: &str, protect: bool) -> Result> { + use windows_sys::Win32::Foundation::LocalFree; + use windows_sys::Win32::Security::Cryptography::{ + CRYPT_INTEGER_BLOB, CRYPTPROTECT_UI_FORBIDDEN, CryptProtectData, CryptUnprotectData, + }; + let mut input = input.to_vec(); + let mut entropy = Sha256::digest( + [ + b"gamecult-service-identity-dpapi-v1\0".as_slice(), + P::PROTECTOR_CONTEXT.as_bytes(), + binding.as_bytes(), + ] + .concat(), + ) + .to_vec(); + let ib = CRYPT_INTEGER_BLOB { + cbData: input.len() as u32, + pbData: input.as_mut_ptr(), + }; + let eb = CRYPT_INTEGER_BLOB { + cbData: entropy.len() as u32, + pbData: entropy.as_mut_ptr(), + }; + let mut output = CRYPT_INTEGER_BLOB { + cbData: 0, + pbData: std::ptr::null_mut(), + }; + let ok = unsafe { + if protect { + CryptProtectData( + &ib, + std::ptr::null(), + &eb, + std::ptr::null(), + std::ptr::null(), + CRYPTPROTECT_UI_FORBIDDEN, + &mut output, + ) + } else { + CryptUnprotectData( + &ib, + std::ptr::null_mut(), + &eb, + std::ptr::null(), + std::ptr::null(), + CRYPTPROTECT_UI_FORBIDDEN, + &mut output, + ) + } + }; + if ok == 0 { + return Err(std::io::Error::last_os_error()) + .context("DPAPI service identity operation failed"); + } + let bytes = + unsafe { std::slice::from_raw_parts(output.pbData, output.cbData as usize).to_vec() }; + unsafe { + LocalFree(output.pbData.cast()); + } + Ok(bytes) +} + +#[cfg(target_os = "linux")] +fn protect_seed(seed: &[u8; 32], binding: &str) -> Result> { + xor_linux::

(seed, binding) +} +#[cfg(target_os = "linux")] +fn unprotect_seed(seed: &[u8], binding: &str) -> Result> { + if seed.len() != 32 { + bail!("protected Linux service seed has invalid length"); + } + xor_linux::

(seed, binding) +} +#[cfg(target_os = "linux")] +fn xor_linux(seed: &[u8], binding: &str) -> Result> { + let mask = Sha256::digest( + [ + b"gamecult-linux-service-seed-v1\0".as_slice(), + P::PROTECTOR_CONTEXT.as_bytes(), + binding.as_bytes(), + ] + .concat(), + ); + Ok(seed.iter().zip(mask).map(|(a, b)| a ^ b).collect()) +} + +fn hex(bytes: &[u8]) -> String { + const D: &[u8; 16] = b"0123456789abcdef"; + let mut s = String::with_capacity(bytes.len() * 2); + for b in bytes { + s.push(D[(b >> 4) as usize] as char); + s.push(D[(b & 15) as usize] as char); + } + s +} + +#[cfg(test)] +mod tests { + use super::*; + enum StatusProjection {} + impl ServiceSignaturePurpose for StatusProjection { + const PURPOSE: &'static [u8] = b"idunn.managed-health-projection.v1"; + } + enum OtherPurpose {} + impl ServiceSignaturePurpose for OtherPurpose { + const PURPOSE: &'static [u8] = b"idunn.other.v1"; + } + + #[test] + fn immutable_enrollment_reopens_and_is_purpose_and_payload_bound() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("idunn.ccmp"); + let signer = enroll_service_identity_at::(&path)?; + let proof = signer.sign::(b"healthy"); + let anchor = signer.trust_anchor()?; + verify_service_identity_signature::( + &anchor, b"healthy", &proof, + )?; + assert!( + verify_service_identity_signature::( + &anchor, + b"unhealthy", + &proof + ) + .is_err() + ); + assert!( + verify_service_identity_signature::( + &anchor, b"healthy", &proof + ) + .is_err() + ); + assert!(enroll_service_identity_at::(&path).is_err()); + assert_eq!( + open_service_identity_at::(&path)?.entry(), + signer.entry() + ); + Ok(()) + } + + #[test] + fn malformed_or_profile_substituted_state_fails_closed_without_replacement() -> Result<()> { + enum Alien {} + impl ServiceIdentityProfile for Alien { + const PRIVATE_TYPE: &'static str = "alien.private.v1"; + const PRIVATE_SCHEMA: &'static str = "alien.private.v1"; + const PRIVATE_KEY: &'static str = "alien"; + const TRUST_ANCHOR_TYPE: &'static str = "alien.anchor.v1"; + const TRUST_ANCHOR_SCHEMA: &'static str = "alien.anchor.v1"; + const TRUST_ANCHOR_KEY: &'static str = "alien-public"; + const ID_DOMAIN: &'static [u8] = b"alien.id\0"; + const SIGNATURE_DOMAIN: &'static [u8] = b"alien.sig\0"; + const PROTECTOR_CONTEXT: &'static str = "alien-v1"; + } + let temp = tempfile::tempdir()?; + let malformed = temp.path().join("bad.ccmp"); + std::fs::write(&malformed, b"rot")?; + let before = std::fs::read(&malformed)?; + assert!(open_service_identity_at::(&malformed).is_err()); + assert!(enroll_service_identity_at::(&malformed).is_err()); + assert_eq!(before, std::fs::read(&malformed)?); + let good = temp.path().join("good.ccmp"); + enroll_service_identity_at::(&good)?; + assert!(open_service_identity_at::(&good).is_err()); + Ok(()) + } + + #[test] + fn exported_anchor_is_public_only_immutable_and_rejects_identity_substitution() -> Result<()> { + let temp = tempfile::tempdir()?; + let signer = + enroll_service_identity_at::(&temp.path().join("private.ccmp"))?; + let output = temp.path().join("public.ccmp"); + let anchor = export_service_identity_trust_anchor(&signer, &output)?; + let bytes = std::fs::read(&output)?; + assert!( + !bytes + .windows(signer.entry().protected_private_seed.len()) + .any(|w| w == signer.entry().protected_private_seed) + ); + export_service_identity_trust_anchor(&signer, &output)?; + assert_eq!(bytes, std::fs::read(&output)?); + let other = + enroll_service_identity_at::(&temp.path().join("other.ccmp"))?; + let proof = other.sign::(b"healthy"); + assert!( + verify_service_identity_signature::( + &anchor, b"healthy", &proof + ) + .is_err() + ); + Ok(()) + } +} From 2dbd7f2d4ce0d3e2f023ef211d0098100aebaa25 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:16:33 +0100 Subject: [PATCH 17/93] Canonicalize signed health admission bytes --- crates/idunn-daemon/src/main.rs | 86 ++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 33fd9f5..aca9797 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -1811,8 +1811,7 @@ fn read_fresh_daemon_published_health( let (_, current_binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; validate_generic_release_binding(node, &admission)?; - let now_millis = parse_timestamp_seconds(now) - .and_then(|seconds| seconds.checked_mul(1000)) + let now_millis = parse_timestamp_millis(now) .ok_or_else(|| anyhow!("managed health clock is invalid"))?; let statement_digest = format!( "sha256-{:x}", @@ -2092,6 +2091,16 @@ fn parse_timestamp_seconds(value: &str) -> Option { parse_unix_timestamp(value).or_else(|| parse_utc_iso_timestamp_seconds(value)) } +fn parse_timestamp_millis(value: &str) -> Option { + if let Some(seconds) = parse_unix_timestamp(value) { + return seconds.checked_mul(1000); + } + let millis = chrono::DateTime::parse_from_rfc3339(value) + .ok()? + .timestamp_millis(); + u64::try_from(millis).ok() +} + fn parse_utc_iso_timestamp_seconds(value: &str) -> Option { if value.len() < 19 { return None; @@ -2669,6 +2678,11 @@ fn authenticate_generic_signed_health( )> { let statement: IdunnSignedDaemonHealthRecord = rmp_serde::from_slice(&document.payload) .context("decoding generic signed daemon health")?; + if rmp_serde::to_vec(&statement)? != document.payload { + return Err(anyhow!( + "signed daemon health payload is not canonical positional MessagePack" + )); + } statement.validate()?; if document.record_key != statement.daemon_id || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) @@ -2722,8 +2736,7 @@ fn authenticate_generic_signed_health( &Signature::from_bytes(&signature_bytes), ) .context("signed daemon health signature is invalid")?; - let admitted_at_unix_millis = parse_timestamp_seconds(admitted_at) - .and_then(|seconds| seconds.checked_mul(1000)) + let admitted_at_unix_millis = parse_timestamp_millis(admitted_at) .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; if statement.observed_at_unix_millis > admitted_at_unix_millis || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) @@ -2917,8 +2930,7 @@ fn validate_generic_release_binding( if current.request_id != request.request_id || current.sequence == 0 { return Err(anyhow!("signed daemon health names a superseded deployment")); } - let requested_at = parse_timestamp_seconds(&request.requested_at) - .and_then(|seconds| seconds.checked_mul(1000)) + let requested_at = parse_timestamp_millis(&request.requested_at) .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; if admission.observed_at_unix_millis < requested_at || admission.admitted_at_unix_millis < requested_at @@ -2985,8 +2997,7 @@ fn decode_health_from_rudp_message( document.record_key, wire.daemon_id )); } - let received_at_unix_millis = parse_timestamp_seconds(admitted_at) - .and_then(|seconds| seconds.checked_mul(1000)) + let received_at_unix_millis = parse_timestamp_millis(admitted_at) .ok_or_else(|| anyhow!("unsigned health diagnostic receive time is invalid"))?; let diagnostic = IdunnUnsignedDaemonHealthDiagnosticRecord { schema_version: IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA.into(), @@ -7974,6 +7985,65 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + #[test] + fn generic_signed_health_requires_canonical_bytes_and_preserves_millisecond_clock() { + let (mut message, options, _root) = generic_signed_health_fixture(1); + let mut statement: IdunnSignedDaemonHealthRecord = match &message { + CultNetMessage::DocumentPutRaw { document, .. } => { + rmp_serde::from_slice(&document.payload).unwrap() + } + _ => unreachable!(), + }; + statement.observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339( + "2026-07-19T12:00:00.500Z", + ) + .unwrap() + .timestamp_millis() as u64; + statement.signature.clear(); + let key = SigningKey::from_bytes(&[31; 32]); + statement.signature = key + .sign(&host_signature_message( + SIGNED_DAEMON_HEALTH_TYPE, + &rmp_serde::to_vec(&statement).unwrap(), + )) + .to_bytes() + .to_vec(); + let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { + unreachable!() + }; + document.payload = rmp_serde::to_vec(&statement).unwrap(); + + admit_health_from_rudp_message( + &message, + &options, + &Arc::new(Mutex::new(())), + "2026-07-19T12:00:00.500Z", + ) + .unwrap(); + + let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { + unreachable!() + }; + let canonical = rmp_serde::to_vec(&statement).unwrap(); + let string_marker_index = if canonical.first() == Some(&0xdc) { 3 } else { 1 }; + let string_marker = canonical[string_marker_index]; + assert!((0xa0..=0xbf).contains(&string_marker)); + let string_length = string_marker & 0x1f; + let mut noncanonical = Vec::with_capacity(canonical.len() + 1); + noncanonical.extend_from_slice(&canonical[..string_marker_index]); + noncanonical.extend_from_slice(&[0xd9, string_length]); + noncanonical.extend_from_slice(&canonical[string_marker_index + 1..]); + document.payload = noncanonical; + let error = authenticate_generic_signed_health( + document, + &options, + "2026-07-19T12:00:00.500Z", + ) + .unwrap_err() + .to_string(); + assert!(error.contains("canonical positional MessagePack")); + } + fn signed_epiphany_health_fixture( sequence: u64, ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { From 99ed509934d31ea3e9aa96949e752774f19163d1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:18:11 +0100 Subject: [PATCH 18/93] Purify authenticated provider health projection --- crates/odin-core/src/documents.rs | 158 +++++++++++++------ docs/idunn-signed-daemon-health-authority.md | 31 +++- vendor/cultnet-rs/src/service_identity.rs | 60 ++++--- 3 files changed, 175 insertions(+), 74 deletions(-) diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 7c6719f..3763feb 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -1,4 +1,5 @@ use cultcache_rs::DatabaseEntry; +use cultnet_rs::{IdunnServiceIdentity, derive_service_identity_id}; use serde_json::Value; use anyhow::{Result, bail}; @@ -45,7 +46,14 @@ pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = pub const IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA: &str = "idunn.unsigned_daemon_health_diagnostic.v1"; pub const GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA: &str = "gamecult.service_trust_anchor.v1"; -pub const IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA: &str = "idunn.managed_health_projection.v1"; +pub const IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA: &str = + "idunn.authenticated_provider_health_projection.v1"; +pub const IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE: &str = + "idunn.authenticated-provider-health-projection.v1"; +pub const IDUNN_PROVIDER_ACTIVE_REASON: &str = "authenticated_provider_active"; +pub const IDUNN_PROVIDER_WARMING_REASON: &str = "authenticated_provider_warming"; +pub const IDUNN_PROVIDER_DEGRADED_REASON: &str = "authenticated_provider_degraded"; +pub const IDUNN_PROVIDER_FAILED_REASON: &str = "authenticated_provider_failed"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; pub const MUNINN_CAPTURE_STREAM_SCHEMA: &str = "muninn.capture_stream.v1"; pub const MUNINN_CAPTURE_STREAM_COMMAND_SCHEMA: &str = "muninn.capture_stream_command.v1"; @@ -382,9 +390,7 @@ impl IdunnSignedDaemonHealthRecord { if self.detail.len() > 512 { bail!("signed daemon health detail exceeds 512 bytes"); } - if self.signature_algorithm != "ed25519" - || self.signature.len() != 64 - { + if self.signature_algorithm != "ed25519" || self.signature.len() != 64 { bail!("signed daemon health signature shape is invalid"); } if self.publisher_sequence == 0 || self.observed_at_unix_millis == 0 { @@ -659,20 +665,28 @@ impl GameCultServiceTrustAnchorRecord { { bail!("service trust anchor authority, key, lifetime, or privacy is invalid"); } + if self.signed_schema == IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA + && (self.service_id != "idunn" + || self.signing_purpose + != IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE + || self.signer_identity_id + != derive_service_identity_id::(&self.signer_public_key)?) + { + bail!("Idunn provider-health trust anchor profile is invalid"); + } Ok(()) } } -/// Idunn-owned, public, signed judgment over one authenticated provider health -/// admission. The signature covers the complete positional document with the -/// signature field empty. Consumers verify it against a separately pinned -/// `GameCultServiceTrustAnchorRecord` and apply their own freshness bound. +/// Idunn-owned, public, signed projection of one *current authenticated generic +/// provider health admission*. Absence, release drift, and dependency failures +/// produce no record; they are not provider states Idunn may manufacture. #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( - type = "idunn.managed_health_projection", - schema = "idunn.managed_health_projection.v1" + type = "idunn.authenticated_provider_health_projection", + schema = "idunn.authenticated_provider_health_projection.v1" )] -pub struct IdunnManagedHealthProjectionRecord { +pub struct IdunnAuthenticatedProviderHealthProjectionRecord { #[cultcache(key = 0)] pub schema_version: String, #[cultcache(key = 1)] @@ -682,9 +696,9 @@ pub struct IdunnManagedHealthProjectionRecord { #[cultcache(key = 3)] pub health_contract: String, #[cultcache(key = 4)] - pub managed_state: String, + pub provider_state: String, #[cultcache(key = 5)] - pub detail: String, + pub reason_code: String, #[cultcache(key = 6)] pub provider_observed_at_unix_millis: u64, #[cultcache(key = 7)] @@ -731,10 +745,22 @@ pub struct IdunnManagedHealthProjectionRecord { pub expires_at_unix_millis: u64, } -impl IdunnManagedHealthProjectionRecord { +/// Closed derivation used by Idunn when projecting an authenticated provider +/// state. `None` means there is no provider-health projection to publish. +pub fn authenticated_provider_health_reason_code(state: &str) -> Option<&'static str> { + match state { + "active" => Some(IDUNN_PROVIDER_ACTIVE_REASON), + "warming" => Some(IDUNN_PROVIDER_WARMING_REASON), + "degraded" => Some(IDUNN_PROVIDER_DEGRADED_REASON), + "failed" => Some(IDUNN_PROVIDER_FAILED_REASON), + _ => None, + } +} + +impl IdunnAuthenticatedProviderHealthProjectionRecord { pub fn validate(&self) -> Result<()> { - if self.schema_version != IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA { - bail!("managed health projection schema is unsupported"); + if self.schema_version != IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA { + bail!("authenticated provider health projection schema is unsupported"); } validate_identifier(&self.projection_id, "projection id")?; validate_identifier(&self.daemon_id, "daemon id")?; @@ -748,10 +774,9 @@ impl IdunnManagedHealthProjectionRecord { validate_identifier(&self.idunn_runtime_id, "Idunn runtime id")?; validate_identifier(&self.idunn_signer_identity_id, "Idunn signer identity id")?; validate_identifier(&self.projection_incarnation_id, "projection incarnation id")?; - if !matches!( - self.managed_state.as_str(), - "active" | "warming" | "degraded" | "failed" - ) || self.detail.len() > 512 + let state_reason_is_valid = authenticated_provider_health_reason_code(&self.provider_state) + .is_some_and(|reason| reason == self.reason_code); + if !state_reason_is_valid || self.provider_observed_at_unix_millis == 0 || self.admitted_at_unix_millis < self.provider_observed_at_unix_millis || self.evaluated_at_unix_millis < self.admitted_at_unix_millis @@ -765,7 +790,9 @@ impl IdunnManagedHealthProjectionRecord { || self.signature.len() != 64 || self.private_state_exposed { - bail!("managed health projection shape, lineage, signature, or privacy is invalid"); + bail!( + "authenticated provider health projection shape, lineage, reason, signature, or privacy is invalid" + ); } validate_optional_release_binding( &self.release_id, @@ -775,7 +802,7 @@ impl IdunnManagedHealthProjectionRecord { validate_optional_identifier(&self.deployment_id, "deployment id")?; let release_binding_present = self.release_id.is_some(); if self.deployment_id.is_some() != release_binding_present { - bail!("managed health projection release lineage is partial"); + bail!("authenticated provider health projection release lineage is partial"); } Ok(()) } @@ -2040,7 +2067,7 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { IdunnAuthenticatedDaemonHealthAdmissionRecord => IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, IdunnUnsignedDaemonHealthDiagnosticRecord => IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, GameCultServiceTrustAnchorRecord => GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - IdunnManagedHealthProjectionRecord => IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA, + IdunnAuthenticatedProviderHealthProjectionRecord => IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, IdunnKeepaliveDecisionRecord => IDUNN_KEEPALIVE_DECISION_SCHEMA, IdunnRestartRequestRecord => IDUNN_RESTART_REQUEST_SCHEMA, IdunnRestartResultRecord => IDUNN_RESTART_RESULT_SCHEMA, @@ -2165,16 +2192,20 @@ mod tests { } fn service_trust_anchor_fixture() -> GameCultServiceTrustAnchorRecord { + let signer_public_key = vec![5; 32]; GameCultServiceTrustAnchorRecord { schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), - trust_anchor_id: "root/idunn/managed-health-projection".into(), + trust_anchor_id: "root/idunn/authenticated-provider-health-projection".into(), service_id: "idunn".into(), runtime_id: "idunn-yggdrasil".into(), - signer_identity_id: "idunn-yggdrasil-service-identity".into(), - signer_public_key: vec![5; 32], + signer_identity_id: derive_service_identity_id::( + &signer_public_key, + ) + .unwrap(), + signer_public_key, signature_algorithm: "ed25519".into(), - signing_purpose: "idunn.managed_health_projection".into(), - signed_schema: IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA.into(), + signing_purpose: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(), + signed_schema: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), binding_authority: "root".into(), bound_at_unix_millis: 1_784_483_100_000, expires_at_unix_millis: Some(1_815_000_000_000), @@ -2182,16 +2213,17 @@ mod tests { } } - fn managed_health_projection_fixture() -> IdunnManagedHealthProjectionRecord { + fn authenticated_provider_health_projection_fixture() + -> IdunnAuthenticatedProviderHealthProjectionRecord { let health = signed_health_fixture(); let binding = trust_binding_fixture(); - IdunnManagedHealthProjectionRecord { - schema_version: IDUNN_MANAGED_HEALTH_PROJECTION_SCHEMA.into(), - projection_id: format!("managed-health:{}", health.daemon_id), + IdunnAuthenticatedProviderHealthProjectionRecord { + schema_version: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + projection_id: format!("authenticated-provider-health:{}", health.daemon_id), daemon_id: health.daemon_id, health_contract: health.health_contract, - managed_state: health.state, - detail: "Idunn admits current provider-authenticated health".into(), + provider_state: health.state, + reason_code: IDUNN_PROVIDER_ACTIVE_REASON.into(), provider_observed_at_unix_millis: health.observed_at_unix_millis, admitted_at_unix_millis: health.observed_at_unix_millis + 1, evaluated_at_unix_millis: health.observed_at_unix_millis + 2, @@ -2218,19 +2250,21 @@ mod tests { } #[test] - fn outward_managed_health_contracts_validate_and_round_trip() -> Result<()> { + fn outward_authenticated_provider_health_contracts_validate_and_round_trip() -> Result<()> { let anchor = service_trust_anchor_fixture(); - let projection = managed_health_projection_fixture(); + let projection = authenticated_provider_health_projection_fixture(); anchor.validate()?; projection.validate()?; let temp = tempfile::tempdir()?; - let store_path = temp.path().join("managed-health-projection.cc"); + let store_path = temp + .path() + .join("authenticated-provider-health-projection.cc"); let mut node = CultMesh::create_node( &store_path, OdinDocuments, CultMeshNodeOptions { - runtime_id: "managed-health-contract-test".into(), + runtime_id: "authenticated-provider-health-contract-test".into(), pull_on_start: true, }, )?; @@ -2241,14 +2275,29 @@ mod tests { anchor ); assert_eq!( - node.get_required::(&projection.projection_id)?, + node.get_required::( + &projection.projection_id + )?, projection ); + assert_eq!( + authenticated_provider_health_reason_code("warming"), + Some(IDUNN_PROVIDER_WARMING_REASON) + ); + assert_eq!( + authenticated_provider_health_reason_code("degraded"), + Some(IDUNN_PROVIDER_DEGRADED_REASON) + ); + assert_eq!( + authenticated_provider_health_reason_code("failed"), + Some(IDUNN_PROVIDER_FAILED_REASON) + ); + assert_eq!(authenticated_provider_health_reason_code("missing"), None); Ok(()) } #[test] - fn outward_managed_health_contracts_refuse_weak_authority_and_lineage() { + fn outward_authenticated_provider_health_contracts_refuse_substitution_and_weak_lineage() { let mut anchor = service_trust_anchor_fixture(); anchor.binding_authority = "idunn".into(); assert!(anchor.validate().is_err()); @@ -2261,31 +2310,44 @@ mod tests { anchor = service_trust_anchor_fixture(); anchor.private_state_exposed = true; assert!(anchor.validate().is_err()); + anchor = service_trust_anchor_fixture(); + anchor.signing_purpose = "caller.chosen-purpose".into(); + assert!(anchor.validate().is_err()); + anchor = service_trust_anchor_fixture(); + anchor.signer_identity_id = "caller-chosen-identity".into(); + assert!(anchor.validate().is_err()); - let mut projection = managed_health_projection_fixture(); + let mut projection = authenticated_provider_health_projection_fixture(); projection.evaluated_at_unix_millis = projection.admitted_at_unix_millis - 1; assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.expires_at_unix_millis = projection.evaluated_at_unix_millis; assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.authenticated_admission_sha256 = "admitted".into(); assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.projection_sequence = 0; assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.signature.clear(); assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.release_witness_sha256 = None; assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.deployment_id = None; assert!(projection.validate().is_err()); - projection = managed_health_projection_fixture(); + projection = authenticated_provider_health_projection_fixture(); projection.private_state_exposed = true; assert!(projection.validate().is_err()); + projection = authenticated_provider_health_projection_fixture(); + projection.reason_code = "release_drift".into(); + assert!(projection.validate().is_err()); + projection = authenticated_provider_health_projection_fixture(); + projection.provider_state = "missing".into(); + projection.reason_code = "authenticated_provider_failed".into(); + assert!(projection.validate().is_err()); } #[test] diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index 6035081..8419d2a 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -12,8 +12,9 @@ does not authenticate the publisher. It is diagnostic input until replaced. - Each daemon owns its health statement and signs it with its pinned service or host identity. -- Idunn owns admission, freshness, monotonic lineage, managed-health judgment, - and the outward status projection. +- Idunn owns admission, freshness, monotonic lineage, and the outward projection + of current authenticated provider health. Lifecycle judgment remains private + Idunn policy and is not smuggled into that provider-health statement. - Root deployment configuration owns the trust binding between daemon id, health contract, source runtime, and signer identity. @@ -42,9 +43,18 @@ daemon/contract/runtime tuple and whether release identity is required. Idunn persists two separate facts: 1. the admitted daemon-authored statement and its digest; -2. an Idunn-authored outward status projection binding the trust record, - admission, managed target, current deployment lineage, freshness judgment, - and observation time. +2. an Idunn-authored `idunn.authenticated_provider_health_projection.v1` + record binding the trust record, admission, exact provider lineage, and + freshness interval. + +The outward record exists only while a generic provider-authored admission is +current and still matches its root trust and optional release binding. Missing +publication, release drift, dependency failure, target absence, and recovery +policy are absence of this projection. They are not synthetic provider states. +The projected state is exactly the authenticated provider state. Its only +explanation is a closed Idunn-generated reason code paired to that state: +`authenticated_provider_active`, `_warming`, `_degraded`, or `_failed`. +There is no free-text detail field. Consumers such as Epiphany Discord Status receive only the outward projection through CultNet/CultMesh and verify Idunn's pinned identity. Reading Idunn's @@ -91,7 +101,9 @@ must absorb its invariants before the special path is deleted. incarnation, freshness, and release requirements. 3. Migrate Epiphany and Bifrost publishers and install their root-owned trust bindings. -4. Publish and verify Idunn's outward signed projection. +4. Publish and verify Idunn's outward signed authenticated-provider-health + projection using the fixed CultNet signature profile + `idunn.authenticated-provider-health-projection.v1`. 5. Point Discord Status at the outward projections. 6. Delete generic unsigned health from managed-health authority and delete the Epiphany-only verifier after its last publisher migrates. @@ -140,6 +152,11 @@ Hostile tests must prove: string cannot repair failed authentication; - one daemon's trust record cannot authenticate another daemon; - unsigned legacy health cannot suppress recovery or yield outward healthy; -- outward status is signed by Idunn and binds the exact admitted statement; +- outward provider health is signed by Idunn under the fixed, typed purpose and + binds the exact admitted statement; +- missing health, release drift, and dependency failure remove the projection + instead of impersonating provider-authored states; +- caller-selected signing purposes, signer ids, reason strings, and synthetic + states fail closed; - private detail and filesystem/process internals never enter the operator projection. diff --git a/vendor/cultnet-rs/src/service_identity.rs b/vendor/cultnet-rs/src/service_identity.rs index fc9c69e..188b429 100644 --- a/vendor/cultnet-rs/src/service_identity.rs +++ b/vendor/cultnet-rs/src/service_identity.rs @@ -35,6 +35,17 @@ pub trait ServiceSignaturePurpose: Send + Sync + 'sta pub enum IdunnServiceIdentity {} +/// The only signing purpose accepted for Idunn's public projection of a +/// provider-authenticated health admission. Keeping this profile here makes +/// purpose selection a compile-time protocol choice rather than caller text. +pub struct IdunnAuthenticatedProviderHealthProjectionPurpose; + +impl ServiceSignaturePurpose + for IdunnAuthenticatedProviderHealthProjectionPurpose +{ + const PURPOSE: &'static [u8] = b"idunn.authenticated-provider-health-projection.v1"; +} + impl ServiceIdentityProfile for IdunnServiceIdentity { const PRIVATE_TYPE: &'static str = "idunn.service_identity.private.v1"; const PRIVATE_SCHEMA: &'static str = "idunn.service_identity.private.v1"; @@ -269,15 +280,24 @@ fn validate_private(entry: &ServiceIdentityPrivateEnt fn validate_anchor(anchor: &ServiceIdentityTrustAnchor) -> Result<()> { if anchor.schema_version != P::TRUST_ANCHOR_SCHEMA || anchor.public_key.len() != 32 - || identity_id::

(&anchor.public_key) != anchor.identity_id + || derive_service_identity_id::

(&anchor.public_key)? != anchor.identity_id { bail!("service identity trust anchor violates its profile schema"); } Ok(()) } +/// Derives the profile-bound identity id from an exact Ed25519 public key. +/// Consumers use this instead of accepting a caller-supplied identity string. +pub fn derive_service_identity_id(public_key: &[u8]) -> Result { + if public_key.len() != 32 { + bail!("service identity public key has invalid length"); + } + Ok(hex(&Sha256::digest([P::ID_DOMAIN, public_key].concat()))) +} + fn identity_id(key: &[u8]) -> String { - hex(&Sha256::digest([P::ID_DOMAIN, key].concat())) + derive_service_identity_id::

(key).expect("internal service identity key is Ed25519") } fn signing_message>( @@ -471,10 +491,6 @@ fn hex(bytes: &[u8]) -> String { #[cfg(test)] mod tests { use super::*; - enum StatusProjection {} - impl ServiceSignaturePurpose for StatusProjection { - const PURPOSE: &'static [u8] = b"idunn.managed-health-projection.v1"; - } enum OtherPurpose {} impl ServiceSignaturePurpose for OtherPurpose { const PURPOSE: &'static [u8] = b"idunn.other.v1"; @@ -485,17 +501,17 @@ mod tests { let temp = tempfile::tempdir()?; let path = temp.path().join("idunn.ccmp"); let signer = enroll_service_identity_at::(&path)?; - let proof = signer.sign::(b"healthy"); + let proof = signer.sign::(b"healthy"); let anchor = signer.trust_anchor()?; - verify_service_identity_signature::( - &anchor, b"healthy", &proof, - )?; + verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnAuthenticatedProviderHealthProjectionPurpose, + >(&anchor, b"healthy", &proof)?; assert!( - verify_service_identity_signature::( - &anchor, - b"unhealthy", - &proof - ) + verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnAuthenticatedProviderHealthProjectionPurpose, + >(&anchor, b"unhealthy", &proof) .is_err() ); assert!( @@ -505,6 +521,11 @@ mod tests { .is_err() ); assert!(enroll_service_identity_at::(&path).is_err()); + assert_eq!( + derive_service_identity_id::(&anchor.public_key)?, + anchor.identity_id + ); + assert!(derive_service_identity_id::(&[0; 31]).is_err()); assert_eq!( open_service_identity_at::(&path)?.entry(), signer.entry() @@ -556,11 +577,12 @@ mod tests { assert_eq!(bytes, std::fs::read(&output)?); let other = enroll_service_identity_at::(&temp.path().join("other.ccmp"))?; - let proof = other.sign::(b"healthy"); + let proof = other.sign::(b"healthy"); assert!( - verify_service_identity_signature::( - &anchor, b"healthy", &proof - ) + verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnAuthenticatedProviderHealthProjectionPurpose, + >(&anchor, b"healthy", &proof) .is_err() ); Ok(()) From 6b3e696c8e10f32cf11e5e545c9a8776a80a5d72 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:25:05 +0100 Subject: [PATCH 19/93] Add pure CultNet snapshot query loop --- .../examples/cultnet_interop_peer.rs | 42 ++- vendor/cultnet-rs/src/lib.rs | 2 + vendor/cultnet-rs/src/replication.rs | 4 +- vendor/cultnet-rs/src/snapshot_query.rs | 339 ++++++++++++++++++ vendor/cultnet-rs/tests/snapshot_query.rs | 222 ++++++++++++ 5 files changed, 589 insertions(+), 20 deletions(-) create mode 100644 vendor/cultnet-rs/src/snapshot_query.rs create mode 100644 vendor/cultnet-rs/tests/snapshot_query.rs diff --git a/vendor/cultnet-rs/examples/cultnet_interop_peer.rs b/vendor/cultnet-rs/examples/cultnet_interop_peer.rs index 0a00c63..b9c1492 100644 --- a/vendor/cultnet-rs/examples/cultnet_interop_peer.rs +++ b/vendor/cultnet-rs/examples/cultnet_interop_peer.rs @@ -11,6 +11,7 @@ use cultnet_rs::CultNetDocumentPutOptions; use cultnet_rs::CultNetDocumentRegistry; use cultnet_rs::CultNetMessage; use cultnet_rs::CultNetMutationAuthority; +use cultnet_rs::CultNetReadOnlySnapshotPolicy; use cultnet_rs::CultNetSchemaKind; use cultnet_rs::CultNetSchemaRegistration; use cultnet_rs::CultNetSchemaRegistry; @@ -25,6 +26,7 @@ use cultnet_rs::builtin_schema_registry; use cultnet_rs::decode_cultnet_message_from_slice; use cultnet_rs::encode_cultnet_message_to_vec; use cultnet_rs::encode_frame; +use cultnet_rs::serve_read_only_raw_snapshot; use serde::Deserialize; use serde::Serialize; use socket2::Domain; @@ -641,26 +643,28 @@ fn handle_connection( let response = schema_registry.create_catalog_response(&request)?; send_message(&mut stream, &response)?; } - CultNetMessage::SnapshotRequest { - message_id, - schema_ids, - record_keys, - } => { - let mut response = document_registry.create_raw_snapshot_response( - &cache.lock().expect("cache poisoned"), - message_id, - schema_ids.as_deref(), - record_keys.as_deref(), - )?; - if let CultNetMessage::SnapshotResponseRaw { documents, .. } = &mut response { - for document in documents.iter_mut() { - document.source_runtime_id = Some(config.runtime_id.clone()); - document.source_agent_id = Some(config.agent_id.clone()); - document.source_role = Some("peer".to_string()); - document.tags = - Some(vec!["interop".to_string(), config.runtime_id.clone()]); + request @ CultNetMessage::SnapshotRequest { .. } => { + // Snapshot serving is a pure read. The backing projection owns + // the records and provenance; the protocol loop only filters + // an explicit allowlist and returns their exact payload bytes. + let (source, policy) = { + let cache = cache.lock().expect("cache poisoned"); + let mut records = Vec::new(); + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + for envelope in cache.snapshot() { + let mut record = + document_registry.raw_document_record_from_envelope(&envelope)?; + record.source_runtime_id = Some(config.runtime_id.clone()); + record.source_agent_id = Some(config.agent_id.clone()); + record.source_role = Some("peer".to_string()); + record.tags = Some(vec!["interop".to_string(), config.runtime_id.clone()]); + policy.allow(&record.schema_id, &record.record_key)?; + records.push(record); } - } + (records, policy) + }; + let response = + serve_read_only_raw_snapshot(&document_registry, &policy, &source, &request)?; send_message(&mut stream, &response)?; } message @ CultNetMessage::DocumentPutRaw { .. } => { diff --git a/vendor/cultnet-rs/src/lib.rs b/vendor/cultnet-rs/src/lib.rs index da2d171..71abe75 100644 --- a/vendor/cultnet-rs/src/lib.rs +++ b/vendor/cultnet-rs/src/lib.rs @@ -5,6 +5,7 @@ mod rudp; mod schema_discovery; mod security; mod service_identity; +mod snapshot_query; mod transport; pub use contracts::*; @@ -14,4 +15,5 @@ pub use rudp::*; pub use schema_discovery::*; pub use security::*; pub use service_identity::*; +pub use snapshot_query::*; pub use transport::*; diff --git a/vendor/cultnet-rs/src/replication.rs b/vendor/cultnet-rs/src/replication.rs index 492cf31..85490e9 100644 --- a/vendor/cultnet-rs/src/replication.rs +++ b/vendor/cultnet-rs/src/replication.rs @@ -403,7 +403,9 @@ impl CultNetDocumentRegistry { }) } - fn raw_document_record_from_envelope( + /// Project a stored CultCache envelope as a raw CultNet record without + /// decoding or re-encoding its payload bytes. + pub fn raw_document_record_from_envelope( &self, envelope: &CultCacheEnvelope, ) -> Result { diff --git a/vendor/cultnet-rs/src/snapshot_query.rs b/vendor/cultnet-rs/src/snapshot_query.rs new file mode 100644 index 0000000..f6150d1 --- /dev/null +++ b/vendor/cultnet-rs/src/snapshot_query.rs @@ -0,0 +1,339 @@ +use anyhow::{Result, anyhow}; +use std::collections::{BTreeMap, BTreeSet}; + +use crate::{CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord}; + +/// Read-only backing surface for a CultNet snapshot server. +/// +/// Records returned here are already the stored wire records. The snapshot +/// server selects and clones them; it never decodes payloads, rewrites source +/// metadata, or applies them to another cache. +pub trait CultNetRawSnapshotSource { + fn raw_snapshot(&self) -> Result>; +} + +impl CultNetRawSnapshotSource for Vec { + fn raw_snapshot(&self) -> Result> { + Ok(self.clone()) + } +} + +impl CultNetRawSnapshotSource for F +where + F: Fn() -> Result>, +{ + fn raw_snapshot(&self) -> Result> { + self() + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct CultNetReadOnlySnapshotPolicy { + allowed_records: BTreeSet<(String, String)>, +} + +impl CultNetReadOnlySnapshotPolicy { + pub fn new() -> Self { + Self::default() + } + + pub fn allow( + &mut self, + schema_id: impl Into, + record_key: impl Into, + ) -> Result<&mut Self> { + let schema_id = schema_id.into(); + let record_key = record_key.into(); + require_non_empty(&schema_id, "schema_id")?; + require_non_empty(&record_key, "record_key")?; + self.allowed_records.insert((schema_id, record_key)); + Ok(self) + } + + fn allows(&self, schema_id: &str, record_key: &str) -> bool { + self.allowed_records + .contains(&(schema_id.to_string(), record_key.to_string())) + } +} + +/// Serve one raw snapshot request from an explicitly bounded, read-only view. +/// +/// The document registry proves that every exposed schema belongs to the +/// runtime. The policy grants individual `(schema_id, record_key)` pairs. The +/// source owns the bytes and metadata; this function only selects records. +pub fn serve_read_only_raw_snapshot( + document_registry: &CultNetDocumentRegistry, + policy: &CultNetReadOnlySnapshotPolicy, + source: &S, + request: &CultNetMessage, +) -> Result { + let CultNetMessage::SnapshotRequest { + message_id, + schema_ids, + record_keys, + } = request + else { + return Err(anyhow!("expected cultnet.snapshot_request.v0")); + }; + require_non_empty(message_id, "message_id")?; + reject_duplicates(schema_ids.as_deref(), "requested schema id")?; + reject_duplicates(record_keys.as_deref(), "requested record key")?; + + let requested_schemas = schema_ids + .as_ref() + .map(|values| values.iter().map(String::as_str).collect::>()); + let requested_keys = record_keys + .as_ref() + .map(|values| values.iter().map(String::as_str).collect::>()); + let mut selected = Vec::new(); + let mut seen = BTreeSet::new(); + + for document in source.raw_snapshot()? { + let identity = (document.schema_id.clone(), document.record_key.clone()); + if !seen.insert(identity.clone()) { + return Err(anyhow!( + "snapshot source contains duplicate record {:?}/{:?}", + identity.0, + identity.1 + )); + } + if document_registry + .binding_by_schema_id(&document.schema_id) + .is_none() + { + return Err(anyhow!( + "snapshot source record {:?}/{:?} uses an unregistered schema", + document.schema_id, + document.record_key + )); + } + if !policy.allows(&document.schema_id, &document.record_key) { + continue; + } + if requested_schemas + .as_ref() + .is_some_and(|schemas| !schemas.contains(document.schema_id.as_str())) + { + continue; + } + if requested_keys + .as_ref() + .is_some_and(|keys| !keys.contains(document.record_key.as_str())) + { + continue; + } + selected.push(document); + } + + Ok(CultNetMessage::SnapshotResponseRaw { + message_id: message_id.clone(), + documents: selected, + }) +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct CultNetSnapshotSourceExpectation { + pub runtime_id: Option, + pub agent_id: Option, + pub role: Option, + pub tags: Option>, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultNetSnapshotRecordExpectation { + pub schema_id: String, + pub record_key: String, + pub source: CultNetSnapshotSourceExpectation, +} + +impl CultNetSnapshotRecordExpectation { + pub fn new(schema_id: impl Into, record_key: impl Into) -> Self { + Self { + schema_id: schema_id.into(), + record_key: record_key.into(), + source: CultNetSnapshotSourceExpectation::default(), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultNetRawSnapshotQuery { + message_id: String, + expectations: BTreeMap<(String, String), CultNetSnapshotSourceExpectation>, +} + +impl CultNetRawSnapshotQuery { + pub fn new( + message_id: impl Into, + records: Vec, + ) -> Result { + let message_id = message_id.into(); + require_non_empty(&message_id, "message_id")?; + if records.is_empty() { + return Err(anyhow!("snapshot query must request at least one record")); + } + let mut expectations = BTreeMap::new(); + for record in records { + require_non_empty(&record.schema_id, "schema_id")?; + require_non_empty(&record.record_key, "record_key")?; + let identity = (record.schema_id, record.record_key); + if expectations + .insert(identity.clone(), record.source) + .is_some() + { + return Err(anyhow!( + "snapshot query contains duplicate requested record {:?}/{:?}", + identity.0, + identity.1 + )); + } + } + Ok(Self { + message_id, + expectations, + }) + } + + pub fn request(&self) -> CultNetMessage { + CultNetMessage::SnapshotRequest { + message_id: self.message_id.clone(), + schema_ids: Some( + self.expectations + .keys() + .map(|(schema_id, _)| schema_id.clone()) + .collect::>() + .into_iter() + .collect(), + ), + record_keys: Some( + self.expectations + .keys() + .map(|(_, record_key)| record_key.clone()) + .collect::>() + .into_iter() + .collect(), + ), + } + } + + /// Validate a response and return records without mutating any local cache. + pub fn accept_response( + &self, + response: CultNetMessage, + ) -> Result> { + let CultNetMessage::SnapshotResponseRaw { + message_id, + documents, + } = response + else { + return Err(anyhow!("expected cultnet.snapshot_response_raw.v0")); + }; + if message_id != self.message_id { + return Err(anyhow!( + "snapshot response message id {:?} does not match request {:?}", + message_id, + self.message_id + )); + } + + let mut seen = BTreeSet::new(); + for document in &documents { + let identity = (document.schema_id.clone(), document.record_key.clone()); + if !seen.insert(identity.clone()) { + return Err(anyhow!( + "snapshot response contains duplicate record {:?}/{:?}", + identity.0, + identity.1 + )); + } + let expected = self.expectations.get(&identity).ok_or_else(|| { + anyhow!( + "snapshot response contains unexpected record {:?}/{:?}", + identity.0, + identity.1 + ) + })?; + require_expected_metadata( + "source_runtime_id", + expected.runtime_id.as_deref(), + document.source_runtime_id.as_deref(), + &identity, + )?; + require_expected_metadata( + "source_agent_id", + expected.agent_id.as_deref(), + document.source_agent_id.as_deref(), + &identity, + )?; + require_expected_metadata( + "source_role", + expected.role.as_deref(), + document.source_role.as_deref(), + &identity, + )?; + if let Some(expected_tags) = expected.tags.as_ref() + && document.tags.as_ref() != Some(expected_tags) + { + return Err(anyhow!( + "snapshot response record {:?}/{:?} has unexpected tags: expected {:?}, got {:?}", + identity.0, + identity.1, + expected_tags, + document.tags + )); + } + } + Ok(documents) + } +} + +pub fn query_read_only_raw_snapshot( + query: &CultNetRawSnapshotQuery, + exchange: F, +) -> Result> +where + F: FnOnce(CultNetMessage) -> Result, +{ + query.accept_response(exchange(query.request())?) +} + +fn require_expected_metadata( + field: &str, + expected: Option<&str>, + actual: Option<&str>, + identity: &(String, String), +) -> Result<()> { + if let Some(expected) = expected + && actual != Some(expected) + { + return Err(anyhow!( + "snapshot response record {:?}/{:?} has unexpected {field}: expected {:?}, got {:?}", + identity.0, + identity.1, + expected, + actual + )); + } + Ok(()) +} + +fn reject_duplicates(values: Option<&[String]>, label: &str) -> Result<()> { + let Some(values) = values else { + return Ok(()); + }; + let mut seen = BTreeSet::new(); + for value in values { + require_non_empty(value, label)?; + if !seen.insert(value) { + return Err(anyhow!("duplicate {label} {value:?}")); + } + } + Ok(()) +} + +fn require_non_empty(value: &str, field: &str) -> Result<()> { + if value.trim().is_empty() { + return Err(anyhow!("{field} must be non-empty")); + } + Ok(()) +} diff --git a/vendor/cultnet-rs/tests/snapshot_query.rs b/vendor/cultnet-rs/tests/snapshot_query.rs new file mode 100644 index 0000000..ac9c6f9 --- /dev/null +++ b/vendor/cultnet-rs/tests/snapshot_query.rs @@ -0,0 +1,222 @@ +use anyhow::Result; +use cultnet_rs::{ + CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, + CultNetRawPayloadEncoding, CultNetRawSnapshotQuery, CultNetReadOnlySnapshotPolicy, + CultNetSnapshotRecordExpectation, CultNetSnapshotSourceExpectation, CultNetWireContract, + decode_cultnet_message_from_slice, encode_cultnet_message_to_vec, query_read_only_raw_snapshot, + serve_read_only_raw_snapshot, +}; + +const HEALTH_SCHEMA: &str = "idunn.managed_health_projection.v1"; + +fn registry() -> CultNetDocumentRegistry { + let mut registry = CultNetDocumentRegistry::new(); + registry.register(CultNetDocumentBinding { + schema_id: HEALTH_SCHEMA.to_string(), + document_type: "idunn.managed-health-projection".to_string(), + mutation_contract: None, + payload_schema_version: Some(HEALTH_SCHEMA.to_string()), + }); + registry +} + +fn record(key: &str, payload: Vec) -> CultNetRawDocumentRecord { + CultNetRawDocumentRecord { + schema_id: HEALTH_SCHEMA.to_string(), + record_key: key.to_string(), + stored_at: "2026-07-19T20:00:00Z".to_string(), + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload, + source_runtime_id: Some("idunn@yggdrasil".to_string()), + source_agent_id: Some("idunn".to_string()), + source_role: Some("deployment-owner".to_string()), + tags: Some(vec!["public-health".to_string()]), + } +} + +fn expectation(key: &str) -> CultNetSnapshotRecordExpectation { + CultNetSnapshotRecordExpectation { + schema_id: HEALTH_SCHEMA.to_string(), + record_key: key.to_string(), + source: CultNetSnapshotSourceExpectation { + runtime_id: Some("idunn@yggdrasil".to_string()), + agent_id: Some("idunn".to_string()), + role: Some("deployment-owner".to_string()), + tags: Some(vec!["public-health".to_string()]), + }, + } +} + +#[test] +fn canonical_wire_loop_returns_exact_stored_raw_record_without_cache_apply() -> Result<()> { + let stored = record("epiphany-agent", vec![0x95, 0xc4, 0x02, 0xde, 0xad]); + let source = vec![stored.clone(), record("not-public", vec![0x01])]; + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; + let query = CultNetRawSnapshotQuery::new("status-42", vec![expectation("epiphany-agent")])?; + + let received = query_read_only_raw_snapshot(&query, |request| { + // This is the canonical transport loop: request and response both cross + // the CultNet wire codec. Neither side receives a cache to mutate. + let request_wire = + encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; + let decoded_request = + decode_cultnet_message_from_slice(&request_wire, CultNetWireContract::CultNetSchemaV0)?; + let response = + serve_read_only_raw_snapshot(®istry(), &policy, &source, &decoded_request)?; + let response_wire = + encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; + decode_cultnet_message_from_slice(&response_wire, CultNetWireContract::CultNetSchemaV0) + })?; + + assert_eq!(received, vec![stored]); + Ok(()) +} + +#[test] +fn server_filters_by_exact_policy_pair_and_request() -> Result<()> { + let source = vec![record("epiphany-agent", vec![1]), record("odin", vec![2])]; + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; + let request = CultNetMessage::SnapshotRequest { + message_id: "bounded".to_string(), + schema_ids: Some(vec![HEALTH_SCHEMA.to_string()]), + record_keys: Some(vec!["epiphany-agent".to_string(), "odin".to_string()]), + }; + + let response = serve_read_only_raw_snapshot(®istry(), &policy, &source, &request)?; + let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { + panic!("expected raw response"); + }; + assert_eq!(documents, vec![source[0].clone()]); + Ok(()) +} + +#[test] +fn server_rejects_duplicate_request_terms_and_ambiguous_backing_records() -> Result<()> { + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; + let duplicate_request = CultNetMessage::SnapshotRequest { + message_id: "duplicate-request".to_string(), + schema_ids: Some(vec![HEALTH_SCHEMA.to_string(), HEALTH_SCHEMA.to_string()]), + record_keys: Some(vec!["epiphany-agent".to_string()]), + }; + let error = serve_read_only_raw_snapshot( + ®istry(), + &policy, + &vec![record("epiphany-agent", vec![1])], + &duplicate_request, + ) + .unwrap_err(); + assert!(error.to_string().contains("duplicate requested schema id")); + + let request = CultNetMessage::SnapshotRequest { + message_id: "duplicate-source".to_string(), + schema_ids: None, + record_keys: None, + }; + let duplicate = record("epiphany-agent", vec![1]); + let error = serve_read_only_raw_snapshot( + ®istry(), + &policy, + &vec![duplicate.clone(), duplicate], + &request, + ) + .unwrap_err(); + assert!(error.to_string().contains("duplicate record")); + Ok(()) +} + +#[test] +fn query_rejects_duplicate_expectations_wrong_message_and_unrelated_message() -> Result<()> { + let error = CultNetRawSnapshotQuery::new( + "duplicate", + vec![expectation("epiphany-agent"), expectation("epiphany-agent")], + ) + .unwrap_err(); + assert!(error.to_string().contains("duplicate requested record")); + + let query = CultNetRawSnapshotQuery::new("wanted", vec![expectation("epiphany-agent")])?; + let error = query + .accept_response(CultNetMessage::SnapshotResponseRaw { + message_id: "somebody-elses-response".to_string(), + documents: vec![], + }) + .unwrap_err(); + assert!(error.to_string().contains("does not match request")); + + let error = query + .accept_response(CultNetMessage::Error { + error: "unrelated".to_string(), + }) + .unwrap_err(); + assert!( + error + .to_string() + .contains("expected cultnet.snapshot_response_raw.v0") + ); + Ok(()) +} + +#[test] +fn query_rejects_unexpected_identity_duplicates_and_source_metadata() -> Result<()> { + let query = CultNetRawSnapshotQuery::new("hostile", vec![expectation("epiphany-agent")])?; + + let error = query + .accept_response(CultNetMessage::SnapshotResponseRaw { + message_id: "hostile".to_string(), + documents: vec![record("odin", vec![1])], + }) + .unwrap_err(); + assert!(error.to_string().contains("unexpected record")); + + let document = record("epiphany-agent", vec![1]); + let error = query + .accept_response(CultNetMessage::SnapshotResponseRaw { + message_id: "hostile".to_string(), + documents: vec![document.clone(), document], + }) + .unwrap_err(); + assert!(error.to_string().contains("duplicate record")); + + let mut forged = record("epiphany-agent", vec![1]); + forged.source_runtime_id = Some("mallory".to_string()); + let error = query + .accept_response(CultNetMessage::SnapshotResponseRaw { + message_id: "hostile".to_string(), + documents: vec![forged], + }) + .unwrap_err(); + assert!(error.to_string().contains("unexpected source_runtime_id")); + + let mut forged = record("epiphany-agent", vec![1]); + forged.tags = Some(vec!["private".to_string()]); + let error = query + .accept_response(CultNetMessage::SnapshotResponseRaw { + message_id: "hostile".to_string(), + documents: vec![forged], + }) + .unwrap_err(); + assert!(error.to_string().contains("unexpected tags")); + Ok(()) +} + +#[test] +fn server_rejects_unregistered_source_schema_even_when_not_exposed() -> Result<()> { + let mut foreign = record("foreign", vec![1]); + foreign.schema_id = "xenos.unregistered.v0".to_string(); + let request = CultNetMessage::SnapshotRequest { + message_id: "unregistered".to_string(), + schema_ids: None, + record_keys: None, + }; + let error = serve_read_only_raw_snapshot( + ®istry(), + &CultNetReadOnlySnapshotPolicy::new(), + &vec![foreign], + &request, + ) + .unwrap_err(); + assert!(error.to_string().contains("unregistered schema")); + Ok(()) +} From 09655aca5a795b7efb8b27417bb562d360db15b2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 20:45:30 +0100 Subject: [PATCH 20/93] Add Idunn authenticated health projection runtime --- crates/idunn-daemon/Cargo.toml | 2 +- crates/idunn-daemon/src/main.rs | 894 +++++++++++++++++-- docs/idunn-signed-daemon-health-authority.md | 29 +- 3 files changed, 821 insertions(+), 104 deletions(-) diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index e0bff9b..20c1af2 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -20,7 +20,7 @@ sha2 = "0.10" chrono = "0.4" serde = { version = "1", features = ["derive"] } ed25519-dalek = "2.2" -uuid = "1" +uuid = { version = "1", features = ["v4"] } [dev-dependencies] tempfile = "3" diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index aca9797..2959a3f 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -7,32 +7,35 @@ use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions}; use cultnet_rs::{ CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpPacketType, CultNetRudpSession, CultNetRudpSessionOptions, CultNetRudpSocketTransportConnection, - CultNetRudpSocketTransportOptions, CultNetWireContract, decode_cultnet_message_from_slice, - decode_rudp_packet, encode_cultnet_message_to_vec, encode_rudp_packet, + CultNetRudpSocketTransportOptions, CultNetWireContract, + IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnServiceIdentity, ServiceIdentitySigner, + decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, + encode_rudp_packet, open_service_identity_at, }; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ - BifrostRepositoryReleaseAuthorityRecord, IdunnCommandBoundaryRecord, - IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, IdunnDaemonSurgeryPlanRecord, - IdunnAuthenticatedDaemonHealthAdmissionRecord, IdunnDaemonHealthTrustBindingRecord, + BifrostRepositoryReleaseAuthorityRecord, IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, IdunnAuthenticatedDaemonHealthAdmissionRecord, + IdunnAuthenticatedProviderHealthProjectionRecord, IdunnCommandBoundaryRecord, + IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, + IdunnDaemonHealthTrustBindingRecord, IdunnDaemonSurgeryPlanRecord, IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, IdunnDeploymentRequestRecord, IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, IdunnOperatorAlarmRecord, IdunnPlan, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, - IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, + IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, IdunnSignedDaemonHealthRecord, IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, - IdunnSignedDaemonHealthRecord, IdunnUnsignedDaemonHealthDiagnosticRecord, OdinDocuments, - IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, - IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, - plan_keepalive, + IdunnUnsignedDaemonHealthDiagnosticRecord, OdinDocuments, + authenticated_provider_health_reason_code, plan_keepalive, }; use sha2::{Digest, Sha256}; use std::collections::HashMap; use std::env; use std::fs::{self, File}; use std::net::{SocketAddr, UdpSocket}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::{Child, Command, ExitStatus, Stdio}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; @@ -495,6 +498,8 @@ struct CommonOptions { rudp_health_bind: Option, trusted_epiphany_health_identity_store: Option, daemon_health_trust_store_path: Option, + service_identity_store_path: Option, + public_health_store_path: Option, execute: bool, command_timeout_seconds: u64, } @@ -563,6 +568,7 @@ fn main() -> Result<()> { match &options.mode { Mode::Single(target) => { + let projection_publisher = initialize_projection_publisher(&options.common)?; let store_lock = Arc::new(Mutex::new(())); let actuation_gate = Arc::new(TargetActuationGate::new()); let now = timestamp()?; @@ -573,6 +579,7 @@ fn main() -> Result<()> { &options.common, &store_lock, Arc::clone(&actuation_gate), + projection_publisher.as_deref(), &mut missing_since, )?; while actuation_gate.reserved.load(Ordering::Acquire) { @@ -580,7 +587,10 @@ fn main() -> Result<()> { } Ok(()) } - Mode::Swarm(swarm) => run_swarm(swarm, &options.common), + Mode::Swarm(swarm) => { + let projection_publisher = initialize_projection_publisher(&options.common)?; + run_swarm(swarm, &options.common, projection_publisher) + } Mode::LifecycleCommand(command) => publish_lifecycle_command(command, &options.common), Mode::ReleaseAuthorityValidation(validation) => { validate_release_authority_at_privileged_boundary(validation) @@ -681,7 +691,11 @@ fn validate_release_authority_at_privileged_boundary( Ok(()) } -fn run_swarm(options: &SwarmOptions, common: &CommonOptions) -> Result<()> { +fn run_swarm( + options: &SwarmOptions, + common: &CommonOptions, + projection_publisher: Option>, +) -> Result<()> { let targets = swarm_targets(options)?; if targets.is_empty() { return Err(anyhow!( @@ -740,12 +754,14 @@ fn run_swarm(options: &SwarmOptions, common: &CommonOptions) -> Result<()> { .get(&target.daemon_id) .cloned() .ok_or_else(|| anyhow!("Idunn target lost its actuation gate"))?; + let worker_projection_publisher = projection_publisher.clone(); workers.push(thread::spawn(move || { run_target_loop( target, worker_common, worker_store_lock, worker_actuation_gate, + worker_projection_publisher, ) })); } @@ -821,6 +837,7 @@ fn run_target_loop( options: CommonOptions, store_lock: Arc>, actuation_gate: Arc, + projection_publisher: Option>, ) -> Result<()> { let mut missing_since = None; loop { @@ -829,6 +846,7 @@ fn run_target_loop( &options, &store_lock, Arc::clone(&actuation_gate), + projection_publisher.as_deref(), &mut missing_since, ) { eprintln!( @@ -855,6 +873,7 @@ fn run_target_cycle( options: &CommonOptions, store_lock: &Arc>, actuation_gate: Arc, + projection_publisher: Option<&IdunnProjectionPublisher>, missing_since: &mut Option, ) -> Result<()> { let now = timestamp()?; @@ -875,8 +894,10 @@ fn run_target_cycle( observed_at: now.clone(), }; - let (mut health_key, mut health, health_authenticated) = + let (mut health_key, mut health, mut projection_source) = evaluate_target_health(target, options, store_lock, &desired, &now)?; + let health_authenticated = + projection_source.is_some() || health.publication_source == "daemon-published"; apply_missing_publication_grace( target, &desired, @@ -891,9 +912,10 @@ fn run_target_cycle( if let Some(fresh_health) = read_fresh_daemon_published_health(options, store_lock, &desired, &veto_now)? { - if health_state_is_healthy(&fresh_health.state) { + if health_state_is_healthy(&fresh_health.health.state) { health_key = desired.daemon_id.clone(); - health = fresh_health; + health = fresh_health.health; + projection_source = fresh_health.projection_source; *missing_since = None; plan = plan_keepalive(&desired, &health, veto_now); } @@ -936,6 +958,15 @@ fn run_target_cycle( Ok(()) })?; + publish_authenticated_provider_health( + projection_publisher, + options, + store_lock, + &desired, + projection_source.as_ref(), + &now, + )?; + if plan.deployment_request.is_some() || plan.restart_request.is_some() { schedule_automatic_actuation( target.clone(), options.clone(), Arc::clone(store_lock), actuation_gate, plan, now, @@ -1787,7 +1818,7 @@ fn read_fresh_daemon_published_health( store_lock: &Arc>, desired: &IdunnDesiredDaemonRecord, now: &str, -) -> Result> { +) -> Result> { with_store_node(options, store_lock, |node| { let Some(health) = node.get::(&desired.daemon_id)? else { return Ok(None); @@ -1797,18 +1828,18 @@ fn read_fresh_daemon_published_health( } match health.publication_source.as_str() { "daemon-authenticated" => { - let Some(admission) = node - .get::(&desired.daemon_id)? + let Some(admission) = + node.get::(&desired.daemon_id)? else { return Ok(None); }; admission.validate()?; - let Some(statement) = node.get::( - &admission.signed_health_sha256, - )? else { + let Some(statement) = + node.get::(&admission.signed_health_sha256)? + else { return Ok(None); }; - let (_, current_binding_sha256) = + let (binding, current_binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; validate_generic_release_binding(node, &admission)?; let now_millis = parse_timestamp_millis(now) @@ -1832,7 +1863,34 @@ fn read_fresh_daemon_published_health( { return Ok(None); } - Ok(Some(health)) + let admission_sha256 = record_sha256(&admission)?; + let (deployment_head, deployment_request) = if let Some(deployment_id) = + admission.deployment_id.as_deref() + { + let head = node + .get::(&admission.daemon_id)? + .ok_or_else(|| anyhow!("authenticated health lost deployment head"))?; + let request = node + .get::(deployment_id)? + .ok_or_else(|| anyhow!("authenticated health lost deployment request"))?; + (Some(head), Some(request)) + } else { + (None, None) + }; + Ok(Some(ManagedHealthRead { + health: health.clone(), + projection_source: Some(AuthenticatedProviderHealthSource { + health, + admission, + statement, + binding, + binding_sha256: current_binding_sha256, + statement_sha256: statement_digest, + admission_sha256, + deployment_head, + deployment_request, + }), + })) } "daemon-published" => { let Some(admission) = @@ -1853,7 +1911,10 @@ fn read_fresh_daemon_published_health( { return Ok(None); } - Ok(Some(health)) + Ok(Some(ManagedHealthRead { + health, + projection_source: None, + })) } _ => Ok(None), } @@ -1866,24 +1927,32 @@ fn evaluate_target_health( store_lock: &Arc>, desired: &IdunnDesiredDaemonRecord, now: &str, -) -> Result<(String, IdunnDaemonHealthRecord, bool)> { +) -> Result<( + String, + IdunnDaemonHealthRecord, + Option, +)> { if let Some(release) = &target.release { if release.deployed_revision_witness.is_some() { if let Some(health) = evaluate_release_drift(target, desired, release, &SystemReleaseStatePort, now) { - return Ok((supervisor_health_key(&desired.daemon_id), health, false)); + return Ok((supervisor_health_key(&desired.daemon_id), health, None)); } } } - if let Some(health) = read_fresh_daemon_published_health(options, store_lock, desired, now)? { - return Ok((desired.daemon_id.clone(), health, true)); + if let Some(managed) = read_fresh_daemon_published_health(options, store_lock, desired, now)? { + return Ok(( + desired.daemon_id.clone(), + managed.health, + managed.projection_source, + )); } Ok(( supervisor_health_key(&desired.daemon_id), missing_daemon_published_health(target, desired, now), - false, + None, )) } @@ -2091,6 +2160,304 @@ fn parse_timestamp_seconds(value: &str) -> Option { parse_unix_timestamp(value).or_else(|| parse_utc_iso_timestamp_seconds(value)) } +struct IdunnProjectionPublisher { + signer: ServiceIdentitySigner, + public_store_path: PathBuf, + incarnation_id: String, + root_trust_snapshot_sha256: String, + write_lock: Mutex<()>, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +struct AuthenticatedProviderHealthSource { + health: IdunnDaemonHealthRecord, + admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, + statement: IdunnSignedDaemonHealthRecord, + binding: IdunnDaemonHealthTrustBindingRecord, + binding_sha256: String, + statement_sha256: String, + admission_sha256: String, + deployment_head: Option, + deployment_request: Option, +} + +#[derive(Clone, Debug)] +struct ManagedHealthRead { + health: IdunnDaemonHealthRecord, + projection_source: Option, +} + +fn initialize_projection_publisher( + options: &CommonOptions, +) -> Result>> { + let (Some(identity_path), Some(public_path)) = ( + options.service_identity_store_path.as_deref(), + options.public_health_store_path.as_deref(), + ) else { + if options.service_identity_store_path.is_some() || options.public_health_store_path.is_some() + { + return Err(anyhow!( + "--service-identity-store and --public-health-store must be configured together" + )); + } + return Ok(None); + }; + reject_store_aliases(options, identity_path, public_path)?; + if let Some(parent) = public_path.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("creating public health store parent {}", parent.display()))?; + } + let signer = open_service_identity_at::(identity_path) + .context("opening enrolled Idunn service identity")?; + let trust_path = options + .daemon_health_trust_store_path + .as_deref() + .ok_or_else(|| anyhow!("public health projection requires --daemon-health-trust-store"))?; + let root_trust_snapshot_sha256 = trust_store_snapshot_sha256(trust_path)?; + Ok(Some(Arc::new(IdunnProjectionPublisher { + signer, + public_store_path: public_path.to_path_buf(), + incarnation_id: uuid::Uuid::new_v4().to_string(), + root_trust_snapshot_sha256, + write_lock: Mutex::new(()), + }))) +} + +fn reject_store_aliases(options: &CommonOptions, identity_path: &Path, public_path: &Path) -> Result<()> { + let identity = normalized_store_path(identity_path)?; + let public = normalized_store_path(public_path)?; + let mut private_paths = vec![normalized_store_path(&options.store_path)?, identity]; + for path in [ + options.daemon_health_trust_store_path.as_deref(), + options.release_authority_store_path.as_deref(), + options.trusted_epiphany_health_identity_store.as_deref(), + ] + .into_iter() + .flatten() + { + private_paths.push(normalized_store_path(path)?); + } + if private_paths + .iter() + .enumerate() + .any(|(index, path)| private_paths.iter().skip(index + 1).any(|other| other == path)) + { + return Err(anyhow!( + "service identity, operational, trust, and release stores must not alias" + )); + } + if private_paths.iter().any(|path| path == &public) { + return Err(anyhow!( + "public health store must not alias identity, operational, trust, or release state" + )); + } + Ok(()) +} + +fn normalized_store_path(path: &Path) -> Result { + if path.exists() { + return fs::canonicalize(path) + .with_context(|| format!("canonicalizing store path {}", path.display())); + } + let file_name = path + .file_name() + .ok_or_else(|| anyhow!("store path {} has no file name", path.display()))?; + let parent = path.parent().unwrap_or_else(|| Path::new(".")); + let canonical_parent = fs::canonicalize(parent) + .with_context(|| format!("canonicalizing store parent {}", parent.display()))?; + Ok(canonical_parent.join(file_name)) +} + +fn trust_store_snapshot_sha256(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + record_sha256(&entries) +} + +impl IdunnProjectionPublisher { + fn publish_if_current( + &self, + options: &CommonOptions, + store_lock: &Arc>, + desired: &IdunnDesiredDaemonRecord, + source: &AuthenticatedProviderHealthSource, + evaluated_at: &str, + ) -> Result<()> { + let evaluated_at_unix_millis = parse_timestamp_millis(evaluated_at) + .ok_or_else(|| anyhow!("projection evaluation time is invalid"))?; + let current = read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? + .and_then(|managed| managed.projection_source) + .ok_or_else(|| anyhow!("authenticated provider health vanished before projection"))?; + if ¤t != source { + return Err(anyhow!( + "authenticated provider health changed before projection publication" + )); + } + self.require_incarnation_trust_snapshot(options)?; + let reason_code = authenticated_provider_health_reason_code(&source.health.state) + .ok_or_else(|| anyhow!("provider state is not publicly projectable"))?; + let expires_at_unix_millis = source + .admission + .observed_at_unix_millis + .saturating_add(u64::from(desired.max_silence_seconds).saturating_mul(1000)); + if expires_at_unix_millis <= evaluated_at_unix_millis { + return Err(anyhow!("authenticated provider health expired before projection")); + } + let projection_key = format!( + "provider-health:{:x}", + Sha256::digest( + [source.health.daemon_id.as_bytes(), b"\0", source.health.health_contract.as_bytes()] + .concat() + ) + ); + let _write_guard = self + .write_lock + .lock() + .map_err(|_| anyhow!("Idunn public projection store lock is poisoned"))?; + for _ in 0..8 { + let backing = SingleFileMessagePackBackingStore::new(&self.public_store_path); + let entries = backing.pull_all_read_only_snapshot()?; + if entries.iter().any(|entry| { + entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE + || entry.schema_id.as_deref() + != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) + }) { + return Err(anyhow!("public health store contains a non-projection envelope")); + } + for entry in &entries { + self.verify_existing_projection(entry)?; + } + let current_envelope = entries.iter().find(|entry| entry.key == projection_key).cloned(); + let current_projection = current_envelope + .as_ref() + .map(|entry| rmp_serde::from_slice::(&entry.payload)) + .transpose() + .context("decoding existing public health projection")?; + if let Some(existing) = current_projection.as_ref() { + existing.validate()?; + if current_envelope.as_ref().is_some_and(|entry| entry.key != existing.projection_id) + || existing.projection_id != projection_key + { + return Err(anyhow!("public projection envelope key does not match its payload")); + } + if existing.idunn_signer_identity_id != self.signer.entry().identity_id { + return Err(anyhow!("public projection signer identity changed without store rotation")); + } + if existing.projection_incarnation_id == self.incarnation_id + && existing.signed_health_sha256 == source.statement_sha256 + && existing.authenticated_admission_sha256 == source.admission_sha256 + && existing.trust_binding_sha256 == source.binding_sha256 + { + return Ok(()); + } + } + let sequence = current_projection + .as_ref() + .map_or(1, |existing| existing.projection_sequence.saturating_add(1)); + if sequence == 0 { + return Err(anyhow!("public projection sequence exhausted")); + } + let mut projection = IdunnAuthenticatedProviderHealthProjectionRecord { + schema_version: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + projection_id: projection_key.clone(), daemon_id: source.health.daemon_id.clone(), + health_contract: source.health.health_contract.clone(), + provider_state: source.health.state.clone(), reason_code: reason_code.into(), + provider_observed_at_unix_millis: source.admission.observed_at_unix_millis, + admitted_at_unix_millis: source.admission.admitted_at_unix_millis, + evaluated_at_unix_millis, trust_binding_id: source.binding.binding_id.clone(), + trust_binding_sha256: source.binding_sha256.clone(), + signed_health_sha256: source.statement_sha256.clone(), + authenticated_admission_sha256: source.admission_sha256.clone(), + provider_signer_identity_id: source.admission.signer_identity_id.clone(), + provider_incarnation_id: source.admission.publisher_incarnation_id.clone(), + provider_sequence: source.admission.publisher_sequence, + release_id: source.admission.release_id.clone(), + release_witness_sha256: source.admission.release_witness_sha256.clone(), + source_commit: source.admission.source_commit.clone(), + deployment_id: source.admission.deployment_id.clone(), + idunn_runtime_id: "idunn-daemon".into(), + idunn_signer_identity_id: self.signer.entry().identity_id.clone(), + projection_incarnation_id: self.incarnation_id.clone(), projection_sequence: sequence, + signature_algorithm: "ed25519".into(), signature: Vec::new(), + private_state_exposed: false, expires_at_unix_millis, + }; + let unsigned = rmp_serde::to_vec(&projection)?; + projection.signature = self.signer + .sign::(&unsigned).signature; + projection.validate()?; + self.require_incarnation_trust_snapshot(options)?; + let replacement = CultCacheEnvelope { + key: projection_key.clone(), + r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), + payload: rmp_serde::to_vec(&projection)?, stored_at: evaluated_at.into(), + schema_id: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), + }; + let expected = CultCacheExpectedEnvelope { + key: projection_key.clone(), + r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), + current: current_envelope, + }; + if backing.compare_exchange(&[expected], &[replacement])? { return Ok(()); } + } + Err(anyhow!("public health projection lost repeated cross-process contention")) + } + + fn require_incarnation_trust_snapshot(&self, options: &CommonOptions) -> Result<()> { + let path = options.daemon_health_trust_store_path.as_deref() + .ok_or_else(|| anyhow!("projection publisher lost its root trust store"))?; + if trust_store_snapshot_sha256(path)? != self.root_trust_snapshot_sha256 { + return Err(anyhow!( + "root daemon-health trust changed during this Idunn incarnation; restart is required" + )); + } + Ok(()) + } + + fn verify_existing_projection(&self, entry: &CultCacheEnvelope) -> Result<()> { + let projection: IdunnAuthenticatedProviderHealthProjectionRecord = + rmp_serde::from_slice(&entry.payload) + .context("decoding public health projection during store audit")?; + if rmp_serde::to_vec(&projection)? != entry.payload { + return Err(anyhow!("public health projection is not canonical MessagePack")); + } + projection.validate()?; + if entry.key != projection.projection_id + || projection.idunn_signer_identity_id != self.signer.entry().identity_id + { + return Err(anyhow!("public health projection key or signer is foreign")); + } + let proof = cultnet_rs::ServiceIdentitySignature { + identity_id: projection.idunn_signer_identity_id.clone(), + signature: projection.signature.clone(), + }; + let mut unsigned = projection; + unsigned.signature.clear(); + cultnet_rs::verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnAuthenticatedProviderHealthProjectionPurpose, + >( + &self.signer.trust_anchor()?, + &rmp_serde::to_vec(&unsigned)?, + &proof, + ) + .context("public health projection signature audit failed") + } +} + +fn record_sha256(value: &T) -> Result { + Ok(format!("sha256-{:x}", Sha256::digest(rmp_serde::to_vec(value)?))) +} + +fn publish_authenticated_provider_health( + publisher: Option<&IdunnProjectionPublisher>, options: &CommonOptions, + store_lock: &Arc>, desired: &IdunnDesiredDaemonRecord, + source: Option<&AuthenticatedProviderHealthSource>, evaluated_at: &str, +) -> Result<()> { + if let (Some(publisher), Some(source)) = (publisher, source) { + publisher.publish_if_current(options, store_lock, desired, source, evaluated_at)?; + } + Ok(()) +} + fn parse_timestamp_millis(value: &str) -> Option { if let Some(seconds) = parse_unix_timestamp(value) { return seconds.checked_mul(1000); @@ -4615,6 +4982,8 @@ impl Options { let mut rudp_health_bind = None; let mut trusted_epiphany_health_identity_store = None; let mut daemon_health_trust_store_path = None; + let mut service_identity_store_path = None; + let mut public_health_store_path = None; let mut execute = false; let mut command_timeout_seconds = 30; let mut daemon_id = None; @@ -4725,6 +5094,18 @@ impl Options { "--daemon-health-trust-store", )?)); } + "--service-identity-store" => { + service_identity_store_path = Some(PathBuf::from(take_value( + &mut args, + "--service-identity-store", + )?)); + } + "--public-health-store" => { + public_health_store_path = Some(PathBuf::from(take_value( + &mut args, + "--public-health-store", + )?)); + } "--disabled" => enabled = false, "--execute" => execute = true, "--interval-seconds" => { @@ -4768,6 +5149,8 @@ impl Options { rudp_health_bind, trusted_epiphany_health_identity_store, daemon_health_trust_store_path, + service_identity_store_path, + public_health_store_path, execute, command_timeout_seconds, }; @@ -5790,6 +6173,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: true, command_timeout_seconds: 1, }; @@ -7133,6 +7518,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -7185,7 +7572,7 @@ mod tests { assert_eq!(health_key, "observation:test-daemon"); assert_eq!(selected.publication_source, "idunn-supervisor-observation"); assert_ne!(selected.state, "active"); - assert!(!authenticated); + assert!(authenticated.is_none()); let _ = std::fs::remove_file(store_path); } @@ -7206,6 +7593,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -7244,7 +7633,7 @@ mod tests { assert_eq!(selected.state, "dependency-unavailable"); assert_eq!(selected.publication_source, "idunn-supervisor-observation"); assert_eq!(selected.transport, "cultmesh.missing-daemon-publication"); - assert!(!authenticated); + assert!(authenticated.is_none()); assert!( selected .detail @@ -7769,6 +8158,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -7799,6 +8190,13 @@ mod tests { fn generic_signed_health_fixture( sequence: u64, + ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { + generic_signed_health_fixture_with_release(sequence, false) + } + + fn generic_signed_health_fixture_with_release( + sequence: u64, + release_bound: bool, ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { let root = tempfile::tempdir().unwrap(); let trust_path = root.path().join("health-trust.cc"); @@ -7819,7 +8217,7 @@ mod tests { signer_public_key: public_key, binding_authority: "root".into(), bound_at_unix_millis: 1_784_483_100_000, - release_binding_required: false, + release_binding_required: release_bound, private_state_exposed: false, }; SingleFileMessagePackBackingStore::new(&trust_path) @@ -7831,7 +8229,7 @@ mod tests { .unwrap() .timestamp_millis() as u64; let mut statement = IdunnSignedDaemonHealthRecord { - schema_version: IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), + schema_version: odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), daemon_id: binding.daemon_id.clone(), health_contract: binding.health_contract.clone(), source_runtime_id: binding.source_runtime_id.clone(), @@ -7841,10 +8239,11 @@ mod tests { publisher_incarnation_id: "00000000-0000-4000-8000-000000000031".into(), publisher_sequence: sequence, observed_at_unix_millis, - release_id: None, - release_witness_sha256: None, - source_commit: None, - deployment_id: None, + release_id: release_bound.then(|| "release-test-1".into()), + release_witness_sha256: release_bound + .then(|| format!("sha256-{}", "a".repeat(64))), + source_commit: release_bound.then(|| "b".repeat(40)), + deployment_id: release_bound.then(|| "deploy-test-1".into()), signature_algorithm: "ed25519".into(), signature: Vec::new(), private_state_exposed: false, @@ -7868,6 +8267,34 @@ mod tests { tags: Some(vec![CULTNET_RUDP_PROTOCOL_ID.into()]), }, }; + if release_bound { + let request = IdunnDeploymentRequestRecord { + request_id: "deploy-test-1".into(), + daemon_id: "test-daemon".into(), + command: "deploy".into(), + authority: "idunn".into(), + requested_at: "2026-07-19T11:59:00Z".into(), + repository_full_name: "GameCult/Test".into(), + upstream_ref: "refs/heads/main".into(), + source_revision: "b".repeat(40), + release_authority_id: "bifrost-test".into(), + release_authority_envelope_sha256: format!("sha256-{}", "c".repeat(64)), + requires_bifrost_authority: true, + }; + let head = IdunnCurrentDeploymentRequestRecord { + daemon_id: "test-daemon".into(), + request_id: request.request_id.clone(), + sequence: 1, + updated_at: "2026-07-19T11:59:00Z".into(), + }; + let mut backing = SingleFileMessagePackBackingStore::new(&store_path); + backing + .push(&typed_envelope(&request.request_id, &request, "unix:1784462340").unwrap()) + .unwrap(); + backing + .push(&typed_envelope(&head.daemon_id, &head, "unix:1784462340").unwrap()) + .unwrap(); + } ( message, CommonOptions { @@ -7877,6 +8304,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: Some(trust_path), + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }, @@ -7888,22 +8317,13 @@ mod tests { fn generic_signed_health_requires_root_binding_and_monotonic_admission() { let (first, options, _root) = generic_signed_health_fixture(1); let lock = Arc::new(Mutex::new(())); - let outcome = admit_health_from_rudp_message( - &first, - &options, - &lock, - "2026-07-19T12:00:01Z", - ) - .unwrap(); + let outcome = + admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:01Z") + .unwrap(); assert_eq!(outcome.authority, "authenticated"); assert!( - admit_health_from_rudp_message( - &first, - &options, - &lock, - "2026-07-19T12:00:02Z" - ) - .is_err() + admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:02Z") + .is_err() ); with_store_node(&options, &lock, |node| { let health = node @@ -7956,17 +8376,12 @@ mod tests { transport_profile_id: transport_profile_id(&target), command_boundary_id: command_boundary_id(&target), }; - let (health_key, selected, authenticated) = evaluate_target_health( - &target, - &options, - &lock, - &desired, - "2026-07-19T12:00:02Z", - ) - .unwrap(); + let (health_key, selected, authenticated) = + evaluate_target_health(&target, &options, &lock, &desired, "2026-07-19T12:00:02Z") + .unwrap(); assert_eq!(health_key, "test-daemon"); assert_eq!(selected.publication_source, "daemon-authenticated"); - assert!(authenticated); + assert!(authenticated.is_some()); let (mut forged, mut no_trust, _other_root) = generic_signed_health_fixture(2); no_trust.daemon_health_trust_store_path = None; @@ -7985,6 +8400,299 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + fn projection_fixture( + release_bound: bool, + ) -> ( + CommonOptions, + Arc>, + IdunnDesiredDaemonRecord, + AuthenticatedProviderHealthSource, + Arc, + tempfile::TempDir, + ) { + let (message, mut options, root) = + generic_signed_health_fixture_with_release(1, release_bound); + let identity_path = root.path().join("idunn-identity.ccmp"); + let public_path = root.path().join("public-health.cc"); + cultnet_rs::enroll_service_identity_at::(&identity_path).unwrap(); + options.service_identity_store_path = Some(identity_path); + options.public_health_store_path = Some(public_path); + let publisher = initialize_projection_publisher(&options) + .unwrap() + .expect("configured publisher"); + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + let desired = IdunnDesiredDaemonRecord { + daemon_id: "test-daemon".into(), + verse_id: "test.local".into(), + name: "Test daemon".into(), + enabled: true, + health_command: None, + restart_command: None, + deploy_command: None, + authority: "idunn-supervisor-command".into(), + max_silence_seconds: 60, + observed_at: "2026-07-19T12:00:02Z".into(), + health_contract: "test.signed-health".into(), + transport_profile_id: "test-transport".into(), + command_boundary_id: "test-command".into(), + }; + let source = + read_fresh_daemon_published_health(&options, &lock, &desired, "2026-07-19T12:00:02Z") + .unwrap() + .unwrap() + .projection_source + .unwrap(); + (options, lock, desired, source, publisher, root) + } + + fn public_projection( + options: &CommonOptions, + ) -> IdunnAuthenticatedProviderHealthProjectionRecord { + let entries = SingleFileMessagePackBackingStore::new( + options.public_health_store_path.as_ref().unwrap(), + ) + .pull_all_read_only_snapshot() + .unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!( + entries[0].r#type, + IdunnAuthenticatedProviderHealthProjectionRecord::TYPE + ); + rmp_serde::from_slice(&entries[0].payload).unwrap() + } + + #[test] + fn projection_is_signed_canonical_public_state_without_provider_detail() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let projection = public_projection(&options); + assert_eq!(projection.projection_sequence, 1); + assert_eq!( + projection.expires_at_unix_millis, + source.admission.observed_at_unix_millis + 60_000 + ); + assert!( + !rmp_serde::to_vec(&projection) + .unwrap() + .windows(b"signed provider health".len()) + .any(|window| window == b"signed provider health") + ); + let signature = projection.signature.clone(); + let mut unsigned = projection.clone(); + unsigned.signature.clear(); + let anchor = publisher.signer.trust_anchor().unwrap(); + cultnet_rs::verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnAuthenticatedProviderHealthProjectionPurpose, + >( + &anchor, + &rmp_serde::to_vec(&unsigned).unwrap(), + &cultnet_rs::ServiceIdentitySignature { + identity_id: projection.idunn_signer_identity_id.clone(), + signature, + }, + ) + .unwrap(); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") + .unwrap(); + assert_eq!( + public_projection(&options), + projection, + "same source refreshed public state" + ); + } + + #[test] + fn projection_rejoins_trust_and_refuses_rotation_or_source_mutation() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + let trust_path = options.daemon_health_trust_store_path.as_ref().unwrap(); + let backing = SingleFileMessagePackBackingStore::new(trust_path); + let current = backing.pull_all_read_only_snapshot().unwrap().remove(0); + let mut rotated = source.binding.clone(); + rotated.binding_id = "root/test-daemon/health-rotated".into(); + assert!( + backing + .compare_exchange( + &[CultCacheExpectedEnvelope { + key: source.binding.binding_id.clone(), + r#type: IdunnDaemonHealthTrustBindingRecord::TYPE.into(), + current: Some(current), + }], + &[ + typed_envelope(&rotated.binding_id, &rotated, "2026-07-19T12:00:02Z") + .unwrap() + ], + ) + .unwrap() + ); + assert!( + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .is_err() + ); + assert!(!options.public_health_store_path.as_ref().unwrap().exists()); + + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + with_store_node(&options, &lock, |node| { + let mut admission = source.admission.clone(); + admission.publisher_sequence += 1; + node.put(&admission.daemon_id, &admission)?; + Ok(()) + }) + .unwrap(); + assert!( + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .is_err() + ); + assert!(!options.public_health_store_path.as_ref().unwrap().exists()); + } + + #[test] + fn projection_refuses_superseded_deployment_and_does_not_delete_prior_row() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(true); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let prior = public_projection(&options); + with_store_node(&options, &lock, |node| { + let mut head = source.deployment_head.clone().unwrap(); + head.request_id = "deploy-test-2".into(); + head.sequence += 1; + node.put(&head.daemon_id, &head)?; + Ok(()) + }) + .unwrap(); + assert!( + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") + .is_err() + ); + assert_eq!(public_projection(&options), prior); + } + + #[test] + fn projection_store_alias_contamination_and_restart_are_bounded() { + let (mut options, _lock, _desired, _source, _publisher, root) = projection_fixture(false); + options.public_health_store_path = Some(options.store_path.clone()); + assert!(initialize_projection_publisher(&options).is_err()); + + let (options, lock, desired, source, first, second_root) = projection_fixture(false); + first + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let first_projection = public_projection(&options); + let second = initialize_projection_publisher(&options).unwrap().unwrap(); + second + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") + .unwrap(); + let second_projection = public_projection(&options); + assert_eq!(second_projection.projection_sequence, 2); + assert_ne!( + second_projection.projection_incarnation_id, + first_projection.projection_incarnation_id + ); + + let public_path = options.public_health_store_path.as_ref().unwrap(); + let backing = SingleFileMessagePackBackingStore::new(public_path); + let current_envelope = backing.pull_all_read_only_snapshot().unwrap().remove(0); + let mut corrupted = second_projection.clone(); + corrupted.signature[0] ^= 1; + let corrupted_envelope = CultCacheEnvelope { + payload: rmp_serde::to_vec(&corrupted).unwrap(), + ..current_envelope.clone() + }; + assert!(backing + .compare_exchange( + &[CultCacheExpectedEnvelope { + key: current_envelope.key.clone(), + r#type: current_envelope.r#type.clone(), + current: Some(current_envelope), + }], + &[corrupted_envelope], + ) + .unwrap()); + assert!(second + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:04Z") + .is_err()); + + let contaminated_path = root.path().join("contaminated-public.cc"); + SingleFileMessagePackBackingStore::new(&contaminated_path) + .push(&typed_envelope(&desired.daemon_id, &desired, "2026-07-19T12:00:02Z").unwrap()) + .unwrap(); + let mut contaminated = options.clone(); + contaminated.public_health_store_path = Some(contaminated_path); + let contaminated_publisher = initialize_projection_publisher(&contaminated) + .unwrap() + .unwrap(); + assert!( + contaminated_publisher + .publish_if_current( + &contaminated, + &lock, + &desired, + &source, + "2026-07-19T12:00:04Z", + ) + .is_err() + ); + drop(second_root); + } + + #[test] + fn non_authenticated_paths_cannot_refresh_or_remove_public_projection() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let prior = public_projection(&options); + publish_authenticated_provider_health( + Some(&publisher), + &options, + &lock, + &desired, + None, + "2026-07-19T12:00:30Z", + ) + .unwrap(); + assert_eq!(public_projection(&options), prior); + } + + #[test] + fn projection_cas_serializes_competing_idunn_incarnations() { + let (options, lock, desired, source, first, _root) = projection_fixture(false); + let second = initialize_projection_publisher(&options).unwrap().unwrap(); + let barrier = Arc::new(std::sync::Barrier::new(3)); + let spawn_publish = |publisher: Arc| { + let options = options.clone(); + let lock = Arc::clone(&lock); + let desired = desired.clone(); + let source = source.clone(); + let barrier = Arc::clone(&barrier); + thread::spawn(move || { + barrier.wait(); + publisher.publish_if_current( + &options, + &lock, + &desired, + &source, + "2026-07-19T12:00:02Z", + ) + }) + }; + let one = spawn_publish(first); + let two = spawn_publish(second); + barrier.wait(); + one.join().unwrap().unwrap(); + two.join().unwrap().unwrap(); + let final_projection = public_projection(&options); + assert_eq!(final_projection.projection_sequence, 2); + } + #[test] fn generic_signed_health_requires_canonical_bytes_and_preserves_millisecond_clock() { let (mut message, options, _root) = generic_signed_health_fixture(1); @@ -7994,11 +8702,10 @@ mod tests { } _ => unreachable!(), }; - statement.observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339( - "2026-07-19T12:00:00.500Z", - ) - .unwrap() - .timestamp_millis() as u64; + statement.observed_at_unix_millis = + chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00.500Z") + .unwrap() + .timestamp_millis() as u64; statement.signature.clear(); let key = SigningKey::from_bytes(&[31; 32]); statement.signature = key @@ -8025,7 +8732,11 @@ mod tests { unreachable!() }; let canonical = rmp_serde::to_vec(&statement).unwrap(); - let string_marker_index = if canonical.first() == Some(&0xdc) { 3 } else { 1 }; + let string_marker_index = if canonical.first() == Some(&0xdc) { + 3 + } else { + 1 + }; let string_marker = canonical[string_marker_index]; assert!((0xa0..=0xbf).contains(&string_marker)); let string_length = string_marker & 0x1f; @@ -8034,13 +8745,10 @@ mod tests { noncanonical.extend_from_slice(&[0xd9, string_length]); noncanonical.extend_from_slice(&canonical[string_marker_index + 1..]); document.payload = noncanonical; - let error = authenticate_generic_signed_health( - document, - &options, - "2026-07-19T12:00:00.500Z", - ) - .unwrap_err() - .to_string(); + let error = + authenticate_generic_signed_health(document, &options, "2026-07-19T12:00:00.500Z") + .unwrap_err() + .to_string(); assert!(error.contains("canonical positional MessagePack")); } @@ -8130,6 +8838,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: Some(identity_path), daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }; @@ -8285,41 +8995,33 @@ mod tests { let (_message, options, _root) = signed_epiphany_health_fixture(1); let lock = Arc::new(Mutex::new(())); let prior = with_store_node(&options, &lock, |node| { - node.get::( - "deploy:yggdrasil-epiphany:fixture", - )? - .ok_or_else(|| anyhow!("fixture request missing")) + node.get::("deploy:yggdrasil-epiphany:fixture")? + .ok_or_else(|| anyhow!("fixture request missing")) }) .unwrap(); assert_eq!( - terminalize_interrupted_deployment_requests( - &options, - &lock, - "2026-07-16T00:02:00Z", - ) - .unwrap(), + terminalize_interrupted_deployment_requests(&options, &lock, "2026-07-16T00:02:00Z",) + .unwrap(), 1 ); let result = with_store_node(&options, &lock, |node| { - node.get::( - "result:deploy:yggdrasil-epiphany:fixture", - )? - .ok_or_else(|| anyhow!("recovered result missing")) + node.get::("result:deploy:yggdrasil-epiphany:fixture")? + .ok_or_else(|| anyhow!("recovered result missing")) }) .unwrap(); assert_eq!(result.request_id, prior.request_id); assert_eq!(result.daemon_id, prior.daemon_id); assert_eq!(result.state, "failed"); assert_eq!(result.completed_at, "2026-07-16T00:02:00Z"); - assert!(result.detail.contains("owning Idunn daemon incarnation stopped")); + assert!( + result + .detail + .contains("owning Idunn daemon incarnation stopped") + ); assert_eq!( - terminalize_interrupted_deployment_requests( - &options, - &lock, - "2026-07-16T00:03:00Z", - ) - .unwrap(), + terminalize_interrupted_deployment_requests(&options, &lock, "2026-07-16T00:03:00Z",) + .unwrap(), 0 ); @@ -8431,6 +9133,8 @@ mod tests { rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, execute: false, command_timeout_seconds: 1, }; diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index 8419d2a..ef9671b 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -131,13 +131,25 @@ missing-publication clock, suppress recovery, or produce authenticated health. The evaluation clock is passed through the whole target cycle instead of being resampled inside the health selector. +Idunn now also has the local outward projection writer. One publisher opens an +already-enrolled Idunn service identity before workers start, captures an +incarnation-stable root trust snapshot, and writes only +`idunn.authenticated_provider_health_projection.v1` envelopes to a distinct +public store. Each write rejoins the exact health, admission, signed statement, +root binding, and optional current deployment lineage. The signed positional +record has a fixed hash-derived key, a store-monotonic sequence, a process UUID, +and an expiry capped by the provider observation's managed-health silence +window. Missing, legacy, unsigned, drifted, or mutated input cannot write, +refresh, or delete a projection; an old row simply expires for consumers. + This source is deliberately not deployable yet: Bifrost and the remaining -generic publishers still emit the unsigned diagnostic contract, the root trust -store has not been installed, Epiphany still uses its signed v0 migration path, -and Idunn has no signed outward status projection. Deploying this intermediate -source would correctly classify unmigrated daemons as missing health and could -therefore actuate recovery. Publisher migration and the outward projection must -land before promotion. +generic publishers still emit the unsigned diagnostic contract, production +root trust and Idunn identity stores have not been installed, Epiphany still +uses its signed v0 migration path, and the public projection has no CultNet +query transport or consumer verifier. Deploying this intermediate source would +correctly classify unmigrated daemons as missing health and could therefore +actuate recovery. Publisher migration, trust enrollment, transport, and the +consumer must land before promotion. ## Verification layer @@ -154,8 +166,9 @@ Hostile tests must prove: - unsigned legacy health cannot suppress recovery or yield outward healthy; - outward provider health is signed by Idunn under the fixed, typed purpose and binds the exact admitted statement; -- missing health, release drift, and dependency failure remove the projection - instead of impersonating provider-authored states; +- missing health, release drift, and dependency failure cannot write or refresh + the projection; any prior row expires instead of being rewritten as an + Idunn-authored provider state; - caller-selected signing purposes, signer ids, reason strings, and synthetic states fail closed; - private detail and filesystem/process internals never enter the operator From 1a8b0e7725c4b8a81cdb27142a01f2db3c1e606a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 21:04:21 +0100 Subject: [PATCH 21/93] Add native Idunn trust provisioning --- crates/idunn-daemon/Cargo.toml | 8 + .../idunn-daemon/src/bin/idunn-provision.rs | 6 + crates/idunn-daemon/src/lib.rs | 1 + crates/idunn-daemon/src/main.rs | 73 +- crates/idunn-daemon/src/provisioning.rs | 640 ++++++++++++++++++ crates/odin-core/src/documents.rs | 19 +- vendor/cultnet-rs/src/service_identity.rs | 45 ++ 7 files changed, 753 insertions(+), 39 deletions(-) create mode 100644 crates/idunn-daemon/src/bin/idunn-provision.rs create mode 100644 crates/idunn-daemon/src/lib.rs create mode 100644 crates/idunn-daemon/src/provisioning.rs diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index 20c1af2..ec23e39 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -9,6 +9,14 @@ publish.workspace = true name = "idunn" path = "src/main.rs" +[[bin]] +name = "idunn-provision" +path = "src/bin/idunn-provision.rs" + +[lib] +name = "idunn_daemon" +path = "src/lib.rs" + [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } diff --git a/crates/idunn-daemon/src/bin/idunn-provision.rs b/crates/idunn-daemon/src/bin/idunn-provision.rs new file mode 100644 index 0000000..cab36d3 --- /dev/null +++ b/crates/idunn-daemon/src/bin/idunn-provision.rs @@ -0,0 +1,6 @@ +fn main() { + if let Err(error) = idunn_daemon::provisioning::run(std::env::args().skip(1)) { + eprintln!("idunn-provision: {error:#}"); + std::process::exit(1); + } +} diff --git a/crates/idunn-daemon/src/lib.rs b/crates/idunn-daemon/src/lib.rs new file mode 100644 index 0000000..58309eb --- /dev/null +++ b/crates/idunn-daemon/src/lib.rs @@ -0,0 +1 @@ +pub mod provisioning; diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 2959a3f..e1b9d8b 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -8,9 +8,12 @@ use cultnet_rs::{ CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpPacketType, CultNetRudpSession, CultNetRudpSessionOptions, CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, - IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnServiceIdentity, ServiceIdentitySigner, + GameCultProviderHealthIdentity, IdunnAuthenticatedProviderHealthProjectionPurpose, + IdunnServiceIdentity, IdunnSignedDaemonHealthPurpose, ServiceIdentitySignature, + ServiceIdentitySigner, decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, - encode_rudp_packet, open_service_identity_at, + derive_service_identity_id, encode_rudp_packet, open_service_identity_at, + verify_service_identity_signature_with_public_key, }; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ @@ -3075,15 +3078,9 @@ fn authenticate_generic_signed_health( "signed daemon health carries release authority not declared by its trust binding" )); } - let key_bytes: [u8; 32] = binding - .signer_public_key - .as_slice() - .try_into() - .map_err(|_| anyhow!("daemon health trust key length is invalid"))?; - let expected_identity = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, binding.signer_public_key.as_slice()].concat()) - ); + let expected_identity = derive_service_identity_id::( + &binding.signer_public_key, + )?; if binding.signer_identity_id != expected_identity || statement.signer_identity_id != binding.signer_identity_id { @@ -3097,12 +3094,18 @@ fn authenticate_generic_signed_health( let mut unsigned = statement.clone(); unsigned.signature.clear(); let unsigned_payload = rmp_serde::to_vec(&unsigned)?; - VerifyingKey::from_bytes(&key_bytes)? - .verify( - &host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned_payload), - &Signature::from_bytes(&signature_bytes), - ) - .context("signed daemon health signature is invalid")?; + verify_service_identity_signature_with_public_key::< + GameCultProviderHealthIdentity, + IdunnSignedDaemonHealthPurpose, + >( + &binding.signer_public_key, + &unsigned_payload, + &ServiceIdentitySignature { + identity_id: binding.signer_identity_id.clone(), + signature: signature_bytes.to_vec(), + }, + ) + .context("signed daemon health signature is invalid")?; let admitted_at_unix_millis = parse_timestamp_millis(admitted_at) .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; if statement.observed_at_unix_millis > admitted_at_unix_millis @@ -8201,12 +8204,12 @@ mod tests { let root = tempfile::tempdir().unwrap(); let trust_path = root.path().join("health-trust.cc"); let store_path = root.path().join("idunn.cc"); - let key = SigningKey::from_bytes(&[31; 32]); - let public_key = key.verifying_key().to_bytes().to_vec(); - let identity_id = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, public_key.as_slice()].concat()) - ); + let signer = cultnet_rs::enroll_service_identity_at::( + &root.path().join("provider-health-identity.cc"), + ) + .unwrap(); + let public_key = signer.entry().public_key.clone(); + let identity_id = signer.entry().identity_id.clone(); let binding = IdunnDaemonHealthTrustBindingRecord { schema_version: odin_core::IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), binding_id: "root/test-daemon/health".into(), @@ -8249,10 +8252,9 @@ mod tests { private_state_exposed: false, }; let unsigned = rmp_serde::to_vec(&statement).unwrap(); - statement.signature = key - .sign(&host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned)) - .to_bytes() - .to_vec(); + statement.signature = signer + .sign::(&unsigned) + .signature; let message = CultNetMessage::DocumentPutRaw { message_id: format!("signed-health-{sequence}"), document: CultNetRawDocumentRecord { @@ -8695,7 +8697,7 @@ mod tests { #[test] fn generic_signed_health_requires_canonical_bytes_and_preserves_millisecond_clock() { - let (mut message, options, _root) = generic_signed_health_fixture(1); + let (mut message, options, root) = generic_signed_health_fixture(1); let mut statement: IdunnSignedDaemonHealthRecord = match &message { CultNetMessage::DocumentPutRaw { document, .. } => { rmp_serde::from_slice(&document.payload).unwrap() @@ -8707,14 +8709,13 @@ mod tests { .unwrap() .timestamp_millis() as u64; statement.signature.clear(); - let key = SigningKey::from_bytes(&[31; 32]); - statement.signature = key - .sign(&host_signature_message( - SIGNED_DAEMON_HEALTH_TYPE, - &rmp_serde::to_vec(&statement).unwrap(), - )) - .to_bytes() - .to_vec(); + let signer = cultnet_rs::open_service_identity_at::( + &root.path().join("provider-health-identity.cc"), + ) + .unwrap(); + statement.signature = signer + .sign::(&rmp_serde::to_vec(&statement).unwrap()) + .signature; let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { unreachable!() }; diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs new file mode 100644 index 0000000..80c4d4b --- /dev/null +++ b/crates/idunn-daemon/src/provisioning.rs @@ -0,0 +1,640 @@ +use anyhow::{Context, Result, anyhow, bail}; +use cultcache_rs::{ + CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, +}; +use cultnet_rs::{ + GameCultProviderHealthIdentity, IdunnServiceIdentity, ServiceIdentityProfile, + ServiceIdentityTrustAnchor, derive_service_identity_id, enroll_service_identity_at, + export_service_identity_trust_anchor, open_service_identity_at, +}; +use odin_core::{ + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultServiceTrustAnchorRecord, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnDaemonHealthTrustBindingRecord, +}; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +trait RootDistributionDocument: DatabaseEntry { + fn root_key(&self) -> &str; +} + +impl RootDistributionDocument for IdunnDaemonHealthTrustBindingRecord { + fn root_key(&self) -> &str { + &self.binding_id + } +} + +impl RootDistributionDocument for GameCultServiceTrustAnchorRecord { + fn root_key(&self) -> &str { + &self.trust_anchor_id + } +} + +pub fn run(args: impl IntoIterator) -> Result<()> { + let mut args = args.into_iter(); + let command = args.next().ok_or_else(|| anyhow!(usage()))?; + let options = parse_options(args)?; + match command.as_str() { + "enroll-idunn-identity" => { + require_only(&options, &["private-store"])?; + enroll_service_identity_at::(&path(&options, "private-store")?)?; + } + "export-idunn-public-anchor" => { + require_only(&options, &["private-store", "public-anchor"])?; + let private = path(&options, "private-store")?; + let public = path(&options, "public-anchor")?; + reject_alias(&private, &public)?; + refuse_existing(&public, "public anchor")?; + let signer = open_service_identity_at::(&private)?; + export_service_identity_trust_anchor(&signer, &public)?; + } + "create-daemon-health-trust-binding" => create_health_binding(&options)?, + "validate-daemon-health-trust-binding" => { + require_only(&options, &["input"])?; + read_typed::( + &path(&options, "input")?, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + )? + .validate()?; + } + "create-provider-projection-trust-anchor" => create_projection_anchor(&options)?, + "validate-provider-projection-trust-anchor" => validate_projection_anchor(&options)?, + _ => bail!("unknown command {command:?}\n{}", usage()), + } + Ok(()) +} + +fn create_health_binding(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "output", + "binding-id", + "daemon", + "health-contract", + "source-runtime", + "signer-public-key-hex", + "bound-at-unix-millis", + "release-binding-required", + ], + )?; + let public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; + let record = IdunnDaemonHealthTrustBindingRecord { + schema_version: IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), + binding_id: required(options, "binding-id")?.into(), + daemon_id: required(options, "daemon")?.into(), + health_contract: required(options, "health-contract")?.into(), + source_runtime_id: required(options, "source-runtime")?.into(), + signer_identity_id: derive_service_identity_id::( + &public_key, + )?, + signer_public_key: public_key, + binding_authority: "root".into(), + bound_at_unix_millis: parse_u64(options, "bound-at-unix-millis")?, + release_binding_required: parse_bool(options, "release-binding-required")?, + private_state_exposed: false, + }; + record.validate()?; + write_new_typed( + &path(options, "output")?, + &record.binding_id, + &record, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + record.bound_at_unix_millis, + ) +} + +fn create_projection_anchor(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "output", + "trust-anchor-id", + "runtime-id", + "idunn-public-anchor", + "bound-at-unix-millis", + "expires-at-unix-millis", + ], + )?; + let output = path(options, "output")?; + let low_path = path(options, "idunn-public-anchor")?; + reject_alias(&output, &low_path)?; + let low = read_low_level_idunn_anchor(&low_path)?; + let bound = parse_u64(options, "bound-at-unix-millis")?; + let record = GameCultServiceTrustAnchorRecord { + schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), + trust_anchor_id: required(options, "trust-anchor-id")?.into(), + service_id: "idunn".into(), + runtime_id: required(options, "runtime-id")?.into(), + signer_identity_id: derive_service_identity_id::(&low.public_key)?, + signer_public_key: low.public_key, + signature_algorithm: "ed25519".into(), + signing_purpose: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(), + signed_schema: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + binding_authority: "root".into(), + bound_at_unix_millis: bound, + expires_at_unix_millis: options + .get("expires-at-unix-millis") + .map(|value| value.parse().context("expires-at-unix-millis must be u64")) + .transpose()?, + private_state_exposed: false, + }; + record.validate()?; + write_new_typed( + &output, + &record.trust_anchor_id, + &record, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + bound, + ) +} + +fn validate_projection_anchor(options: &BTreeMap) -> Result<()> { + require_only(options, &["input", "idunn-public-anchor"])?; + let input = path(options, "input")?; + let low_path = path(options, "idunn-public-anchor")?; + reject_alias(&input, &low_path)?; + let record = read_typed::( + &input, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + )?; + record.validate()?; + let low = read_low_level_idunn_anchor(&low_path)?; + if record.signer_public_key != low.public_key || record.signer_identity_id != low.identity_id { + bail!("projection trust anchor does not match the supplied Idunn public anchor"); + } + Ok(()) +} + +fn write_new_typed( + path: &Path, + key: &str, + value: &T, + schema: &str, + millis: u64, +) -> Result<()> { + refuse_existing(path, "root distribution document")?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let stored_at = chrono::DateTime::from_timestamp_millis( + i64::try_from(millis).context("document timestamp is out of range")?, + ) + .ok_or_else(|| anyhow!("document timestamp is out of range"))? + .to_rfc3339(); + let envelope = CultCacheEnvelope { + key: key.into(), + r#type: T::TYPE.into(), + payload: rmp_serde::to_vec(value)?, + stored_at, + schema_id: Some(schema.into()), + }; + let created = SingleFileMessagePackBackingStore::new(path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: T::TYPE.into(), + key: key.into(), + current: None, + }], + &[envelope], + )?; + if !created { + bail!("root distribution document lost atomic create race; refusing replacement"); + } + Ok(()) +} + +fn read_typed(path: &Path, schema: &str) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("root distribution file must contain exactly one document"); + }; + if entry.r#type != T::TYPE || entry.schema_id.as_deref() != Some(schema) { + bail!("root distribution document has the wrong type or schema"); + } + let value: T = rmp_serde::from_slice(&entry.payload).context("decoding typed root document")?; + if rmp_serde::to_vec(&value)? != entry.payload { + bail!("root distribution document is not canonical positional MessagePack"); + } + if entry.key != value.root_key() { + bail!("root distribution envelope key does not match its typed document"); + } + Ok(value) +} + +fn read_low_level_idunn_anchor(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("Idunn public anchor store must contain exactly one document"); + }; + if entry.r#type != IdunnServiceIdentity::TRUST_ANCHOR_TYPE + || entry.key != IdunnServiceIdentity::TRUST_ANCHOR_KEY + || entry.schema_id.as_deref() != Some(IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA) + { + bail!("public anchor does not belong to the Idunn service identity profile"); + } + let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&entry.payload)?; + if rmp_serde::to_vec(&anchor)? != entry.payload + || anchor.schema_version != IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA + || anchor.identity_id + != derive_service_identity_id::(&anchor.public_key)? + { + bail!("Idunn public anchor is malformed or names the wrong key"); + } + Ok(anchor) +} + +fn parse_options(args: impl Iterator) -> Result> { + let mut args = args.peekable(); + let mut out = BTreeMap::new(); + while let Some(name) = args.next() { + let name = name + .strip_prefix("--") + .ok_or_else(|| anyhow!("expected --option, got {name:?}"))?; + if name.contains("seed") || name.contains("private-key") { + bail!("private seed/key input is forbidden; enroll a protected identity instead"); + } + let value = args + .next() + .ok_or_else(|| anyhow!("missing value for --{name}"))?; + if out.insert(name.into(), value).is_some() { + bail!("duplicate option --{name}"); + } + } + Ok(out) +} + +fn require_only(options: &BTreeMap, names: &[&str]) -> Result<()> { + for name in names { + required(options, name)?; + } + if let Some(name) = options.keys().find(|name| !names.contains(&name.as_str())) { + bail!("unsupported option --{name}"); + } + Ok(()) +} + +fn required<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { + options + .get(name) + .map(String::as_str) + .ok_or_else(|| anyhow!("missing --{name}")) +} + +fn path(options: &BTreeMap, name: &str) -> Result { + let value = required(options, name)?; + if value.trim().is_empty() { + bail!("--{name} must not be empty"); + } + Ok(PathBuf::from(value)) +} + +fn parse_u64(options: &BTreeMap, name: &str) -> Result { + required(options, name)? + .parse() + .with_context(|| format!("--{name} must be u64")) +} + +fn parse_bool(options: &BTreeMap, name: &str) -> Result { + match required(options, name)? { + "true" => Ok(true), + "false" => Ok(false), + _ => bail!("--{name} must be true or false"), + } +} + +fn decode_public_key(value: &str) -> Result> { + if value.len() != 64 + || !value + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) + { + bail!("signer public key must be exactly 64 lowercase hexadecimal characters"); + } + (0..32) + .map(|index| u8::from_str_radix(&value[index * 2..index * 2 + 2], 16).map_err(Into::into)) + .collect() +} + +fn refuse_existing(path: &Path, label: &str) -> Result<()> { + if path.exists() { + bail!( + "{label} {} already exists; replacement is forbidden", + path.display() + ); + } + Ok(()) +} + +fn reject_alias(first: &Path, second: &Path) -> Result<()> { + if normalized(first)? == normalized(second)? { + bail!("private/public or input/output paths must not alias"); + } + Ok(()) +} + +fn normalized(path: &Path) -> Result { + let absolute = if path.is_absolute() { + path.to_path_buf() + } else { + std::env::current_dir()?.join(path) + }; + let parent = absolute + .parent() + .ok_or_else(|| anyhow!("path has no parent"))?; + let parent = parent + .canonicalize() + .unwrap_or_else(|_| parent.to_path_buf()); + Ok(parent.join( + absolute + .file_name() + .ok_or_else(|| anyhow!("path has no filename"))?, + )) +} + +fn usage() -> &'static str { + "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " +} + +#[cfg(test)] +mod tests { + use super::*; + use cultcache_rs::CacheBackingStore; + use tempfile::TempDir; + + fn invoke(args: &[&str]) -> Result<()> { + run(args.iter().map(|value| value.to_string())) + } + + fn replace_typed( + path: &Path, + key: &str, + schema: &str, + value: &T, + ) -> Result<()> { + std::fs::remove_file(path)?; + SingleFileMessagePackBackingStore::new(path).push(&CultCacheEnvelope { + key: key.into(), + r#type: T::TYPE.into(), + payload: rmp_serde::to_vec(value)?, + stored_at: "2026-07-19T20:00:00Z".into(), + schema_id: Some(schema.into()), + }) + } + + #[test] + fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("identity.cc"); + let public = temp.path().join("identity-public.cc"); + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap(), + ])?; + assert!( + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap() + ]) + .is_err() + ); + assert!( + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + private.to_str().unwrap(), + ]) + .is_err() + ); + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ])?; + assert!( + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + assert!( + invoke(&[ + "enroll-idunn-identity", + "--private-store", + temp.path().join("new.cc").to_str().unwrap(), + "--private-seed", + "00", + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn health_binding_derives_identity_and_rejects_overwrite_and_corruption() -> Result<()> { + let temp = TempDir::new()?; + let output = temp.path().join("provider-binding.cc"); + let key = "07".repeat(32); + let args = [ + "create-daemon-health-trust-binding", + "--output", + output.to_str().unwrap(), + "--binding-id", + "root/epiphany/health", + "--daemon", + "yggdrasil-epiphany", + "--health-contract", + "epiphany.cultnet-rudp-runtime-health", + "--source-runtime", + "epiphany-daemon-supervisor", + "--signer-public-key-hex", + &key, + "--bound-at-unix-millis", + "1784483100000", + "--release-binding-required", + "true", + ]; + invoke(&args)?; + assert!(invoke(&args).is_err()); + let mut record = read_typed::( + &output, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + )?; + assert_eq!( + record.signer_identity_id, + derive_service_identity_id::( + &record.signer_public_key + )? + ); + record.signer_identity_id = "caller-chosen".into(); + replace_typed( + &output, + &record.binding_id, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-daemon-health-trust-binding", + "--input", + output.to_str().unwrap() + ]) + .is_err() + ); + + record.signer_identity_id = derive_service_identity_id::( + &record.signer_public_key, + )?; + record.schema_version = "wrong.schema.v9".into(); + replace_typed( + &output, + &record.binding_id, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-daemon-health-trust-binding", + "--input", + output.to_str().unwrap() + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn projection_anchor_is_exact_profile_and_matches_low_level_anchor() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("identity.cc"); + let public = temp.path().join("public.cc"); + let root = temp.path().join("root-anchor.cc"); + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap(), + ])?; + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ])?; + let create = [ + "create-provider-projection-trust-anchor", + "--output", + root.to_str().unwrap(), + "--trust-anchor-id", + "root/idunn/provider-health", + "--runtime-id", + "idunn-yggdrasil", + "--idunn-public-anchor", + public.to_str().unwrap(), + "--bound-at-unix-millis", + "1784483100000", + "--expires-at-unix-millis", + "1815000000000", + ]; + invoke(&create)?; + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ])?; + assert!(invoke(&create).is_err()); + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + public.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + + let mut record = read_typed::( + &root, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + )?; + record.signing_purpose = "wrong-purpose".into(); + replace_typed( + &root, + &record.trust_anchor_id, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + + record.signing_purpose = + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(); + record.signer_public_key = vec![9; 32]; + replace_typed( + &root, + &record.trust_anchor_id, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn release_binding_policy_is_explicit_and_boolean() { + let temp = TempDir::new().unwrap(); + let output = temp.path().join("binding.cc"); + let base = [ + "create-daemon-health-trust-binding", + "--output", + output.to_str().unwrap(), + "--binding-id", + "root/provider/health", + "--daemon", + "provider", + "--health-contract", + "provider.health", + "--source-runtime", + "provider-runtime", + "--signer-public-key-hex", + "0707070707070707070707070707070707070707070707070707070707070707", + "--bound-at-unix-millis", + "1784483100000", + ]; + assert!(invoke(&base).is_err()); + let mut partial = base.to_vec(); + partial.extend(["--release-binding-required", "sometimes"]); + assert!(invoke(&partial).is_err()); + } +} diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 3763feb..a536747 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -1,5 +1,7 @@ use cultcache_rs::DatabaseEntry; -use cultnet_rs::{IdunnServiceIdentity, derive_service_identity_id}; +use cultnet_rs::{ + GameCultProviderHealthIdentity, IdunnServiceIdentity, derive_service_identity_id, +}; use serde_json::Value; use anyhow::{Result, bail}; @@ -460,6 +462,13 @@ impl IdunnDaemonHealthTrustBindingRecord { if self.private_state_exposed { bail!("daemon health trust binding exposes private state"); } + if self.signer_identity_id + != derive_service_identity_id::( + &self.signer_public_key, + )? + { + bail!("daemon health trust binding identity does not match its public key"); + } Ok(()) } } @@ -2151,14 +2160,18 @@ mod tests { fn trust_binding_fixture() -> IdunnDaemonHealthTrustBindingRecord { let health = signed_health_fixture(); + let signer_public_key = vec![7; 32]; IdunnDaemonHealthTrustBindingRecord { schema_version: IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), binding_id: "root/yggdrasil-epiphany/health".into(), daemon_id: health.daemon_id, health_contract: health.health_contract, source_runtime_id: health.source_runtime_id, - signer_identity_id: health.signer_identity_id, - signer_public_key: vec![7; 32], + signer_identity_id: derive_service_identity_id::( + &signer_public_key, + ) + .unwrap(), + signer_public_key, binding_authority: "root".into(), bound_at_unix_millis: 1_784_483_100_000, release_binding_required: true, diff --git a/vendor/cultnet-rs/src/service_identity.rs b/vendor/cultnet-rs/src/service_identity.rs index 188b429..a2323da 100644 --- a/vendor/cultnet-rs/src/service_identity.rs +++ b/vendor/cultnet-rs/src/service_identity.rs @@ -34,6 +34,27 @@ pub trait ServiceSignaturePurpose: Send + Sync + 'sta } pub enum IdunnServiceIdentity {} +pub enum GameCultProviderHealthIdentity {} + +pub struct IdunnSignedDaemonHealthPurpose; + +impl ServiceSignaturePurpose + for IdunnSignedDaemonHealthPurpose +{ + const PURPOSE: &'static [u8] = b"idunn.signed_daemon_health.v1"; +} + +impl ServiceIdentityProfile for GameCultProviderHealthIdentity { + const PRIVATE_TYPE: &'static str = "gamecult.provider_health_identity.private.v1"; + const PRIVATE_SCHEMA: &'static str = "gamecult.provider_health_identity.private.v1"; + const PRIVATE_KEY: &'static str = "gamecult-provider-health-identity"; + const TRUST_ANCHOR_TYPE: &'static str = "gamecult.provider_health_identity.trust_anchor.v1"; + const TRUST_ANCHOR_SCHEMA: &'static str = "gamecult.provider_health_identity.trust_anchor.v1"; + const TRUST_ANCHOR_KEY: &'static str = "gamecult-provider-health-identity-public"; + const ID_DOMAIN: &'static [u8] = b"gamecult.provider-health.identity.v1\0"; + const SIGNATURE_DOMAIN: &'static [u8] = b"gamecult.provider-health.signature.v1\0"; + const PROTECTOR_CONTEXT: &'static str = "gamecult-provider-health-identity-v1"; +} /// The only signing purpose accepted for Idunn's public projection of a /// provider-authenticated health admission. Keeping this profile here makes @@ -260,6 +281,30 @@ where .map_err(|_| anyhow!("service identity signature verification failed")) } +/// Verifies against a public key already admitted by an owning trust store. +/// The identity is still derived from the profile; callers cannot name it. +pub fn verify_service_identity_signature_with_public_key( + public_key: &[u8], + payload: &[u8], + proof: &ServiceIdentitySignature, +) -> Result<()> +where + P: ServiceIdentityProfile, + S: ServiceSignaturePurpose

, +{ + if proof.identity_id != derive_service_identity_id::

(public_key)? { + bail!("service identity signature names a different identity"); + } + let key: [u8; 32] = public_key + .try_into() + .map_err(|_| anyhow!("service identity public key has invalid length"))?; + let signature = Signature::from_slice(&proof.signature) + .map_err(|_| anyhow!("service identity signature has invalid length"))?; + VerifyingKey::from_bytes(&key)? + .verify(&signing_message::(payload), &signature) + .map_err(|_| anyhow!("service identity signature verification failed")) +} + fn validate_private(entry: &ServiceIdentityPrivateEntry) -> Result<()> { if entry.schema_version != P::PRIVATE_SCHEMA || entry.public_key.len() != 32 From deed50808a7e3cd0581806456d701740679b1f4a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 21:28:21 +0100 Subject: [PATCH 22/93] Make CultNet the provider contract owner --- .gitmodules | 3 + crates/odin-core/src/documents.rs | 222 +-- vendor/cultnet-rs | 1 + vendor/cultnet-rs/.gitignore | 2 - vendor/cultnet-rs/Cargo.toml | 36 - vendor/cultnet-rs/README.md | 96 -- .../cultnet.document-delete.schema.json | 19 - ...net.document-mutation-contract.schema.json | 40 - .../cultnet.document-put-raw.schema.json | 19 - .../cultnet.document-put.schema.json | 19 - .../cultnet.document-record.schema.json | 26 - .../contracts/cultnet.error.schema.json | 15 - .../contracts/cultnet.hello.schema.json | 41 - .../cultnet.login-success.schema.json | 17 - .../contracts/cultnet.login.schema.json | 19 - .../cultnet.raw-document-record.schema.json | 32 - .../contracts/cultnet.register.schema.json | 21 - .../cultnet.sample-change-name.schema.json | 15 - .../contracts/cultnet.sample-chat.schema.json | 15 - ...cultnet.schema-catalog-request.schema.json | 31 - ...ultnet.schema-catalog-response.schema.json | 22 - .../cultnet.schema-descriptor.schema.json | 39 - .../cultnet.snapshot-request.schema.json | 23 - .../cultnet.snapshot-response-raw.schema.json | 22 - .../cultnet.snapshot-response.schema.json | 22 - .../cultnet.transport-profile.schema.json | 81 - .../contracts/cultnet.verify.schema.json | 17 - .../ghostlight.agent-state.schema.json | 845 ---------- .../examples/cultnet_interop_peer.rs | 1030 ------------ vendor/cultnet-rs/src/contracts.rs | 1224 -------------- vendor/cultnet-rs/src/framing.rs | 49 - vendor/cultnet-rs/src/lib.rs | 19 - vendor/cultnet-rs/src/replication.rs | 442 ----- vendor/cultnet-rs/src/rudp.rs | 1444 ----------------- vendor/cultnet-rs/src/schema_discovery.rs | 396 ----- vendor/cultnet-rs/src/security.rs | 270 --- vendor/cultnet-rs/src/service_identity.rs | 635 -------- vendor/cultnet-rs/src/snapshot_query.rs | 339 ---- vendor/cultnet-rs/src/transport.rs | 194 --- vendor/cultnet-rs/tests/cultnet.rs | 1352 --------------- .../tests/fixtures/cultnet-ts-hello.frame | Bin 160 -> 0 bytes .../fixtures/cultnet-ts-legacy-login.frame | Bin 30 -> 0 bytes vendor/cultnet-rs/tests/snapshot_query.rs | 222 --- 43 files changed, 13 insertions(+), 9363 deletions(-) create mode 100644 .gitmodules create mode 160000 vendor/cultnet-rs delete mode 100644 vendor/cultnet-rs/.gitignore delete mode 100644 vendor/cultnet-rs/Cargo.toml delete mode 100644 vendor/cultnet-rs/README.md delete mode 100644 vendor/cultnet-rs/contracts/cultnet.document-delete.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.document-mutation-contract.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.document-put-raw.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.document-put.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.document-record.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.error.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.hello.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.login-success.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.login.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.raw-document-record.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.register.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.sample-change-name.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.sample-chat.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.schema-catalog-request.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.schema-catalog-response.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.schema-descriptor.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.snapshot-request.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.snapshot-response-raw.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.snapshot-response.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.transport-profile.schema.json delete mode 100644 vendor/cultnet-rs/contracts/cultnet.verify.schema.json delete mode 100644 vendor/cultnet-rs/contracts/ghostlight.agent-state.schema.json delete mode 100644 vendor/cultnet-rs/examples/cultnet_interop_peer.rs delete mode 100644 vendor/cultnet-rs/src/contracts.rs delete mode 100644 vendor/cultnet-rs/src/framing.rs delete mode 100644 vendor/cultnet-rs/src/lib.rs delete mode 100644 vendor/cultnet-rs/src/replication.rs delete mode 100644 vendor/cultnet-rs/src/rudp.rs delete mode 100644 vendor/cultnet-rs/src/schema_discovery.rs delete mode 100644 vendor/cultnet-rs/src/security.rs delete mode 100644 vendor/cultnet-rs/src/service_identity.rs delete mode 100644 vendor/cultnet-rs/src/snapshot_query.rs delete mode 100644 vendor/cultnet-rs/src/transport.rs delete mode 100644 vendor/cultnet-rs/tests/cultnet.rs delete mode 100644 vendor/cultnet-rs/tests/fixtures/cultnet-ts-hello.frame delete mode 100644 vendor/cultnet-rs/tests/fixtures/cultnet-ts-legacy-login.frame delete mode 100644 vendor/cultnet-rs/tests/snapshot_query.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..efa780e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/cultnet-rs"] + path = vendor/cultnet-rs + url = https://github.com/GameCult/cultnet-rs.git diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index a536747..ff55260 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -1,6 +1,13 @@ use cultcache_rs::DatabaseEntry; +pub use cultnet_rs::{ + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultServiceTrustAnchorRecord, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, IDUNN_PROVIDER_ACTIVE_REASON, + IDUNN_PROVIDER_DEGRADED_REASON, IDUNN_PROVIDER_FAILED_REASON, IDUNN_PROVIDER_WARMING_REASON, + IdunnAuthenticatedProviderHealthProjectionRecord, authenticated_provider_health_reason_code, +}; use cultnet_rs::{ - GameCultProviderHealthIdentity, IdunnServiceIdentity, derive_service_identity_id, + GameCultProviderHealthIdentity, derive_service_identity_id, }; use serde_json::Value; @@ -47,15 +54,6 @@ pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = "idunn.authenticated_daemon_health_admission.v1"; pub const IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA: &str = "idunn.unsigned_daemon_health_diagnostic.v1"; -pub const GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA: &str = "gamecult.service_trust_anchor.v1"; -pub const IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA: &str = - "idunn.authenticated_provider_health_projection.v1"; -pub const IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE: &str = - "idunn.authenticated-provider-health-projection.v1"; -pub const IDUNN_PROVIDER_ACTIVE_REASON: &str = "authenticated_provider_active"; -pub const IDUNN_PROVIDER_WARMING_REASON: &str = "authenticated_provider_warming"; -pub const IDUNN_PROVIDER_DEGRADED_REASON: &str = "authenticated_provider_degraded"; -pub const IDUNN_PROVIDER_FAILED_REASON: &str = "authenticated_provider_failed"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; pub const MUNINN_CAPTURE_STREAM_SCHEMA: &str = "muninn.capture_stream.v1"; pub const MUNINN_CAPTURE_STREAM_COMMAND_SCHEMA: &str = "muninn.capture_stream_command.v1"; @@ -615,208 +613,6 @@ impl IdunnUnsignedDaemonHealthDiagnosticRecord { } } -/// Root-distributed public key binding for one service-owned signed contract. -/// Consumers pin this document; a self-declared key inside a signed projection -/// is never authority. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "gamecult.service_trust_anchor", - schema = "gamecult.service_trust_anchor.v1" -)] -pub struct GameCultServiceTrustAnchorRecord { - #[cultcache(key = 0)] - pub schema_version: String, - #[cultcache(key = 1)] - pub trust_anchor_id: String, - #[cultcache(key = 2)] - pub service_id: String, - #[cultcache(key = 3)] - pub runtime_id: String, - #[cultcache(key = 4)] - pub signer_identity_id: String, - #[cultcache(key = 5)] - pub signer_public_key: Vec, - #[cultcache(key = 6)] - pub signature_algorithm: String, - #[cultcache(key = 7)] - pub signing_purpose: String, - #[cultcache(key = 8)] - pub signed_schema: String, - #[cultcache(key = 9)] - pub binding_authority: String, - #[cultcache(key = 10)] - pub bound_at_unix_millis: u64, - #[cultcache(key = 11)] - pub expires_at_unix_millis: Option, - #[cultcache(key = 12)] - pub private_state_exposed: bool, -} - -impl GameCultServiceTrustAnchorRecord { - pub fn validate(&self) -> Result<()> { - if self.schema_version != GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA { - bail!("service trust anchor schema is unsupported"); - } - validate_identifier(&self.trust_anchor_id, "trust anchor id")?; - validate_identifier(&self.service_id, "service id")?; - validate_identifier(&self.runtime_id, "runtime id")?; - validate_identifier(&self.signer_identity_id, "signer identity id")?; - validate_identifier(&self.signing_purpose, "signing purpose")?; - validate_identifier(&self.signed_schema, "signed schema")?; - if self.signer_public_key.len() != 32 - || self.signature_algorithm != "ed25519" - || self.binding_authority != "root" - || self.bound_at_unix_millis == 0 - || self - .expires_at_unix_millis - .is_some_and(|expires_at| expires_at <= self.bound_at_unix_millis) - || self.private_state_exposed - { - bail!("service trust anchor authority, key, lifetime, or privacy is invalid"); - } - if self.signed_schema == IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA - && (self.service_id != "idunn" - || self.signing_purpose - != IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE - || self.signer_identity_id - != derive_service_identity_id::(&self.signer_public_key)?) - { - bail!("Idunn provider-health trust anchor profile is invalid"); - } - Ok(()) - } -} - -/// Idunn-owned, public, signed projection of one *current authenticated generic -/// provider health admission*. Absence, release drift, and dependency failures -/// produce no record; they are not provider states Idunn may manufacture. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.authenticated_provider_health_projection", - schema = "idunn.authenticated_provider_health_projection.v1" -)] -pub struct IdunnAuthenticatedProviderHealthProjectionRecord { - #[cultcache(key = 0)] - pub schema_version: String, - #[cultcache(key = 1)] - pub projection_id: String, - #[cultcache(key = 2)] - pub daemon_id: String, - #[cultcache(key = 3)] - pub health_contract: String, - #[cultcache(key = 4)] - pub provider_state: String, - #[cultcache(key = 5)] - pub reason_code: String, - #[cultcache(key = 6)] - pub provider_observed_at_unix_millis: u64, - #[cultcache(key = 7)] - pub admitted_at_unix_millis: u64, - #[cultcache(key = 8)] - pub evaluated_at_unix_millis: u64, - #[cultcache(key = 9)] - pub trust_binding_id: String, - #[cultcache(key = 10)] - pub trust_binding_sha256: String, - #[cultcache(key = 11)] - pub signed_health_sha256: String, - #[cultcache(key = 12)] - pub authenticated_admission_sha256: String, - #[cultcache(key = 13)] - pub provider_signer_identity_id: String, - #[cultcache(key = 14)] - pub provider_incarnation_id: String, - #[cultcache(key = 15)] - pub provider_sequence: u64, - #[cultcache(key = 16)] - pub release_id: Option, - #[cultcache(key = 17)] - pub release_witness_sha256: Option, - #[cultcache(key = 18)] - pub source_commit: Option, - #[cultcache(key = 19)] - pub deployment_id: Option, - #[cultcache(key = 20)] - pub idunn_runtime_id: String, - #[cultcache(key = 21)] - pub idunn_signer_identity_id: String, - #[cultcache(key = 22)] - pub projection_incarnation_id: String, - #[cultcache(key = 23)] - pub projection_sequence: u64, - #[cultcache(key = 24)] - pub signature_algorithm: String, - #[cultcache(key = 25)] - pub signature: Vec, - #[cultcache(key = 26)] - pub private_state_exposed: bool, - #[cultcache(key = 27)] - pub expires_at_unix_millis: u64, -} - -/// Closed derivation used by Idunn when projecting an authenticated provider -/// state. `None` means there is no provider-health projection to publish. -pub fn authenticated_provider_health_reason_code(state: &str) -> Option<&'static str> { - match state { - "active" => Some(IDUNN_PROVIDER_ACTIVE_REASON), - "warming" => Some(IDUNN_PROVIDER_WARMING_REASON), - "degraded" => Some(IDUNN_PROVIDER_DEGRADED_REASON), - "failed" => Some(IDUNN_PROVIDER_FAILED_REASON), - _ => None, - } -} - -impl IdunnAuthenticatedProviderHealthProjectionRecord { - pub fn validate(&self) -> Result<()> { - if self.schema_version != IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA { - bail!("authenticated provider health projection schema is unsupported"); - } - validate_identifier(&self.projection_id, "projection id")?; - validate_identifier(&self.daemon_id, "daemon id")?; - validate_identifier(&self.health_contract, "health contract")?; - validate_identifier(&self.trust_binding_id, "trust binding id")?; - validate_identifier( - &self.provider_signer_identity_id, - "provider signer identity id", - )?; - validate_identifier(&self.provider_incarnation_id, "provider incarnation id")?; - validate_identifier(&self.idunn_runtime_id, "Idunn runtime id")?; - validate_identifier(&self.idunn_signer_identity_id, "Idunn signer identity id")?; - validate_identifier(&self.projection_incarnation_id, "projection incarnation id")?; - let state_reason_is_valid = authenticated_provider_health_reason_code(&self.provider_state) - .is_some_and(|reason| reason == self.reason_code); - if !state_reason_is_valid - || self.provider_observed_at_unix_millis == 0 - || self.admitted_at_unix_millis < self.provider_observed_at_unix_millis - || self.evaluated_at_unix_millis < self.admitted_at_unix_millis - || self.expires_at_unix_millis <= self.evaluated_at_unix_millis - || self.provider_sequence == 0 - || self.projection_sequence == 0 - || !is_sha256(&self.trust_binding_sha256) - || !is_sha256(&self.signed_health_sha256) - || !is_sha256(&self.authenticated_admission_sha256) - || self.signature_algorithm != "ed25519" - || self.signature.len() != 64 - || self.private_state_exposed - { - bail!( - "authenticated provider health projection shape, lineage, reason, signature, or privacy is invalid" - ); - } - validate_optional_release_binding( - &self.release_id, - &self.release_witness_sha256, - &self.source_commit, - )?; - validate_optional_identifier(&self.deployment_id, "deployment id")?; - let release_binding_present = self.release_id.is_some(); - if self.deployment_id.is_some() != release_binding_present { - bail!("authenticated provider health projection release lineage is partial"); - } - Ok(()) - } -} - fn validate_identifier(value: &str, label: &str) -> Result<()> { if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { bail!("{label} is empty, oversized, or contains control characters"); @@ -2211,7 +2007,7 @@ mod tests { trust_anchor_id: "root/idunn/authenticated-provider-health-projection".into(), service_id: "idunn".into(), runtime_id: "idunn-yggdrasil".into(), - signer_identity_id: derive_service_identity_id::( + signer_identity_id: derive_service_identity_id::( &signer_public_key, ) .unwrap(), diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs new file mode 160000 index 0000000..014f998 --- /dev/null +++ b/vendor/cultnet-rs @@ -0,0 +1 @@ +Subproject commit 014f998b97c37a34055ce0af069ebaead32d11fb diff --git a/vendor/cultnet-rs/.gitignore b/vendor/cultnet-rs/.gitignore deleted file mode 100644 index 96ef6c0..0000000 --- a/vendor/cultnet-rs/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -Cargo.lock diff --git a/vendor/cultnet-rs/Cargo.toml b/vendor/cultnet-rs/Cargo.toml deleted file mode 100644 index 1334626..0000000 --- a/vendor/cultnet-rs/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "cultnet-rs" -version = "0.1.0" -edition = "2024" -publish = false -license = "MIT" -description = "CultNet-style typed MessagePack messaging, direct-pipe framing, security helpers, and CultCache replication for Rust runtimes." - -[lib] -name = "cultnet_rs" -path = "src/lib.rs" - -[dependencies] -aes-gcm = "0.10" -anyhow = "1" -base64 = "0.22" -chrono = { version = "0.4", features = ["serde"] } -cultcache-rs = { path = "../cultcache-rs" } -ed25519-dalek = "2.2" -hmac = "0.12" -rand = "0.9" -rmp-serde = "1.3" -rmpv = { version = "1.3", features = ["with-serde"] } -serde = { version = "1", features = ["derive"] } -serde_bytes = "0.11" -serde_json = "1" -sha2 = "0.10" -socket2 = "0.5" -uuid = { version = "1", features = ["v4"] } - -[target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security_Cryptography"] } - -[dev-dependencies] -pretty_assertions = "1.4" -tempfile = "3" diff --git a/vendor/cultnet-rs/README.md b/vendor/cultnet-rs/README.md deleted file mode 100644 index 8324a5b..0000000 --- a/vendor/cultnet-rs/README.md +++ /dev/null @@ -1,96 +0,0 @@ -# CultNet RS - -`cultnet-rs` is the Rust sibling of `cultnet-ts`: typed MessagePack messages, -4-byte length-prefixed direct-pipe framing, stop-and-wait RUDP message delivery, -CultLib-shaped security helpers, and CultCache replication without making -callers paw through raw envelopes like we lost a bet. - -The contract is intentionally boring: - -- `cultnet.schema.v0` sends schema-versioned MessagePack objects. -- `gamecult.networking.v0` maps the legacy C# union shape explicitly. -- schema discovery uses explicit catalog request/response messages; no inbound - auto-detect sludge. -- document put/delete/snapshot messages move typed CultCache entries. -- raw document put/snapshot messages preserve canonical MessagePack payload - bytes for bit-compatible neighbors. -- `cultnet.transport.rudp.v0` carries one CultNet message per acknowledged UDP - datagram with timeout/retry semantics. -- hello messages advertise `supportedMutationContracts`, so callers discover - which document types are read-only, which accept typed intents, which - authority owns the mutation, and which receipt documents prove the outcome. -- payloads are decoded through registered Rust types before entering the cache. - -This crate is not an HTTP wrapper. It is the wire vocabulary Epiphany, -Ghostlight, VoidBot, and the rest of the swarm can share. - -The public API surface is the schema plus its mutation contracts. A runtime does -not expose a pile of bespoke verbs and hope everyone remembers the ritual; it -advertises typed documents, typed intents, and typed receipts. Polite machines -knock on the contract before touching the furniture. - -## Receipts - -```powershell -cargo test -``` - -The initial tests prove: - -- CultLib-compatible AES-GCM string encryption and HMAC session tokens -- 4-byte big-endian MessagePack framing -- schema-versioned message round trips -- legacy `gamecult.networking.v0` login mapping -- schema discovery catalog responses with canonical JSON schema hashes -- CultCache snapshot replication through registered typed documents -- raw snapshot replication that preserves the original payload bytes -- document mutation contract advertisement through hello frames and registries -- acknowledged localhost UDP delivery through `cultnet.transport.rudp.v0` - -## RUDP Transport - -`cultnet.transport.rudp.v0` is the Rust crate's first non-TCP daemon transport. -It is intentionally small: one CultNet message becomes one UDP data packet, the -receiver sends an acknowledgement packet, and the sender retries until the ack -arrives or the retry budget is spent. - -This is a daemon health/control substrate, not a bulk stream transport. It does -not fragment oversized messages, multiplex streams, or promise congestion -control. Large snapshots should stay in CultCache/CultMesh storage and move by -document reference or a later chunking contract. The useful invariant is simple: -Rust runtimes can now move a typed CultNet message across UDP without reaching -for TCP as the hidden truth path. - -## Schema Discovery - -`cultnet-rs` now ships a built-in schema registry for the shared swarm contract -surface: - -- core wire messages -- legacy `gamecult.networking.v0` auth/sample payloads -- schema catalog request/response messages -- the canonical `ghostlight.agent-state` document payload schema - -Use `builtin_schema_registry()` when you want the standard catalog, or register -your own closed-world schema set with `CultNetSchemaRegistry`. Discovery stays -explicit on purpose: peers advertise only the contracts they were compiled to -understand, the same way CultCache only consumes the document types you -registered instead of pretending polymorphism is a public park. - -## Local Fast Lane - -`cultnet-rs` now mirrors the raw replication seam from `cultnet-ts`: - -- `cultnet.document_put_raw.v0` -- `cultnet.snapshot_response_raw.v0` - -Those messages carry the exact persisted MessagePack payload bytes from -CultCache along with the typed envelope metadata. Combined with -`CultCache::put_envelope::()`, that lets a bit-compatible neighbor ingest the -document without re-encoding the payload first. - -That still is not zero-copy in the religious sense. Frames allocate, bytes move, -and the receiving cache still decodes once to keep typed reads and validation -honest. The win is narrower and realer: identical payload bytes stop getting -decoded into generic sludge and then encoded right back into the same bytes for -no reason. diff --git a/vendor/cultnet-rs/contracts/cultnet.document-delete.schema.json b/vendor/cultnet-rs/contracts/cultnet.document-delete.schema.json deleted file mode 100644 index 265bcaf..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.document-delete.schema.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-delete.schema.json", - "title": "CultNet Document Delete Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "schemaId", - "recordKey" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.document_delete.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "schemaId": { "type": "string", "minLength": 1 }, - "recordKey": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.document-mutation-contract.schema.json b/vendor/cultnet-rs/contracts/cultnet.document-mutation-contract.schema.json deleted file mode 100644 index e444fef..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.document-mutation-contract.schema.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-mutation-contract.schema.json", - "title": "CultNet Document Mutation Contract", - "type": "object", - "required": [ - "documentType", - "operations", - "authority" - ], - "additionalProperties": false, - "properties": { - "documentType": { "type": "string", "minLength": 1 }, - "payloadSchemaVersion": { "type": "string", "minLength": 1 }, - "operations": { - "type": "array", - "minItems": 1, - "items": { - "type": "string", - "enum": ["snapshot", "documentPut", "documentDelete", "intentSubmit", "receiptWatch"] - } - }, - "authority": { - "type": "string", - "enum": ["readOnly", "localUser", "coordinator", "runtime", "denied"] - }, - "intentDocumentTypes": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "receiptDocumentTypes": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "notes": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.document-put-raw.schema.json b/vendor/cultnet-rs/contracts/cultnet.document-put-raw.schema.json deleted file mode 100644 index 3c178d0..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.document-put-raw.schema.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-put-raw.schema.json", - "title": "CultNet Raw Document Put Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "document" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.document_put_raw.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "document": { - "$ref": "./cultnet.raw-document-record.schema.json" - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.document-put.schema.json b/vendor/cultnet-rs/contracts/cultnet.document-put.schema.json deleted file mode 100644 index a04f5fc..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.document-put.schema.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-put.schema.json", - "title": "CultNet Document Put Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "document" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.document_put.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "document": { - "$ref": "./cultnet.document-record.schema.json" - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.document-record.schema.json b/vendor/cultnet-rs/contracts/cultnet.document-record.schema.json deleted file mode 100644 index 94301dc..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.document-record.schema.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.document-record.schema.json", - "title": "CultNet Document Record", - "type": "object", - "required": [ - "schemaId", - "recordKey", - "storedAt", - "payload" - ], - "additionalProperties": false, - "properties": { - "schemaId": { "type": "string", "minLength": 1 }, - "recordKey": { "type": "string", "minLength": 1 }, - "storedAt": { "type": "string", "minLength": 1 }, - "payload": {}, - "sourceRuntimeId": { "type": "string", "minLength": 1 }, - "sourceAgentId": { "type": "string", "minLength": 1 }, - "sourceRole": { "type": "string", "minLength": 1 }, - "tags": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.error.schema.json b/vendor/cultnet-rs/contracts/cultnet.error.schema.json deleted file mode 100644 index af6da9c..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.error.schema.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.error.schema.json", - "title": "CultNet Error Message", - "type": "object", - "required": [ - "schemaVersion", - "error" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.error.v0" }, - "error": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.hello.schema.json b/vendor/cultnet-rs/contracts/cultnet.hello.schema.json deleted file mode 100644 index a557dc3..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.hello.schema.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.hello.schema.json", - "title": "CultNet Hello Message", - "type": "object", - "required": [ - "schemaVersion", - "runtimeId", - "runtimeKind" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.hello.v0" }, - "runtimeId": { "type": "string", "minLength": 1 }, - "runtimeKind": { "type": "string", "minLength": 1 }, - "agentId": { "type": "string", "minLength": 1 }, - "role": { "type": "string", "minLength": 1 }, - "displayName": { "type": "string", "minLength": 1 }, - "supportedDocumentTypes": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "supportedMutationContracts": { - "type": "array", - "items": { - "$ref": "./cultnet.document-mutation-contract.schema.json" - } - }, - "supportedMessageVersions": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "transportProfiles": { - "type": "array", - "items": { - "$ref": "./cultnet.transport-profile.schema.json" - } - }, - "supportsSchemaCatalog": { "type": "boolean" } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.login-success.schema.json b/vendor/cultnet-rs/contracts/cultnet.login-success.schema.json deleted file mode 100644 index 3d8a459..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.login-success.schema.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.login-success.schema.json", - "title": "CultNet Login Success Message", - "type": "object", - "required": [ - "schemaVersion", - "nonce", - "session" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.login_success.v0" }, - "nonce": { "type": "string", "minLength": 1 }, - "session": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.login.schema.json b/vendor/cultnet-rs/contracts/cultnet.login.schema.json deleted file mode 100644 index 62f4959..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.login.schema.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.login.schema.json", - "title": "CultNet Login Message", - "type": "object", - "required": [ - "schemaVersion", - "nonce", - "auth", - "password" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.login.v0" }, - "nonce": { "type": "string", "minLength": 1 }, - "auth": { "type": "string", "minLength": 1 }, - "password": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.raw-document-record.schema.json b/vendor/cultnet-rs/contracts/cultnet.raw-document-record.schema.json deleted file mode 100644 index 3348a5f..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.raw-document-record.schema.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.raw-document-record.schema.json", - "title": "CultNet Raw Document Record", - "type": "object", - "required": [ - "schemaId", - "recordKey", - "storedAt", - "payloadEncoding", - "payload" - ], - "additionalProperties": false, - "properties": { - "schemaId": { "type": "string", "minLength": 1 }, - "recordKey": { "type": "string", "minLength": 1 }, - "storedAt": { "type": "string", "minLength": 1 }, - "payloadEncoding": { "const": "messagepack" }, - "payload": { - "type": "string", - "contentEncoding": "base64", - "x-cult-runtime-type": "bytes" - }, - "sourceRuntimeId": { "type": "string", "minLength": 1 }, - "sourceAgentId": { "type": "string", "minLength": 1 }, - "sourceRole": { "type": "string", "minLength": 1 }, - "tags": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.register.schema.json b/vendor/cultnet-rs/contracts/cultnet.register.schema.json deleted file mode 100644 index 77286f3..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.register.schema.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.register.schema.json", - "title": "CultNet Register Message", - "type": "object", - "required": [ - "schemaVersion", - "nonce", - "email", - "password", - "name" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.register.v0" }, - "nonce": { "type": "string", "minLength": 1 }, - "email": { "type": "string", "minLength": 1 }, - "password": { "type": "string", "minLength": 1 }, - "name": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.sample-change-name.schema.json b/vendor/cultnet-rs/contracts/cultnet.sample-change-name.schema.json deleted file mode 100644 index fc3fe05..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.sample-change-name.schema.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.sample-change-name.schema.json", - "title": "CultNet Sample Change Name Message", - "type": "object", - "required": [ - "schemaVersion", - "name" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.sample.change_name.v0" }, - "name": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.sample-chat.schema.json b/vendor/cultnet-rs/contracts/cultnet.sample-chat.schema.json deleted file mode 100644 index 8426fb5..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.sample-chat.schema.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.sample-chat.schema.json", - "title": "CultNet Sample Chat Message", - "type": "object", - "required": [ - "schemaVersion", - "text" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.sample.chat.v0" }, - "text": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.schema-catalog-request.schema.json b/vendor/cultnet-rs/contracts/cultnet.schema-catalog-request.schema.json deleted file mode 100644 index 611733d..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.schema-catalog-request.schema.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-catalog-request.schema.json", - "title": "CultNet Schema Catalog Request Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.schema_catalog_request.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "includeSchemaJson": { "type": "boolean" }, - "schemaIds": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "kinds": { - "type": "array", - "items": { - "type": "string", - "enum": [ - "wire_message", - "document_payload", - "shared_contract" - ] - } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.schema-catalog-response.schema.json b/vendor/cultnet-rs/contracts/cultnet.schema-catalog-response.schema.json deleted file mode 100644 index fa371e4..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.schema-catalog-response.schema.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-catalog-response.schema.json", - "title": "CultNet Schema Catalog Response Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "schemas" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.schema_catalog_response.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "schemas": { - "type": "array", - "items": { - "$ref": "./cultnet.schema-descriptor.schema.json" - } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.schema-descriptor.schema.json b/vendor/cultnet-rs/contracts/cultnet.schema-descriptor.schema.json deleted file mode 100644 index 2379685..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.schema-descriptor.schema.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.schema-descriptor.schema.json", - "title": "CultNet Schema Descriptor", - "type": "object", - "required": [ - "schemaId", - "kind", - "wireContracts", - "contentHash" - ], - "additionalProperties": false, - "properties": { - "schemaId": { "type": "string", "minLength": 1 }, - "kind": { - "type": "string", - "enum": [ - "wire_message", - "document_payload", - "shared_contract" - ] - }, - "schemaVersion": { "type": "string", "minLength": 1 }, - "documentType": { "type": "string", "minLength": 1 }, - "title": { "type": "string", "minLength": 1 }, - "wireContracts": { - "type": "array", - "items": { - "type": "string", - "enum": [ - "cultnet.schema.v0", - "gamecult.networking.v0" - ] - } - }, - "contentHash": { "type": "string", "minLength": 1 }, - "schemaJson": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.snapshot-request.schema.json b/vendor/cultnet-rs/contracts/cultnet.snapshot-request.schema.json deleted file mode 100644 index 712d705..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.snapshot-request.schema.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-request.schema.json", - "title": "CultNet Snapshot Request Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.snapshot_request.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "schemaIds": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "recordKeys": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.snapshot-response-raw.schema.json b/vendor/cultnet-rs/contracts/cultnet.snapshot-response-raw.schema.json deleted file mode 100644 index a84bc50..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.snapshot-response-raw.schema.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-response-raw.schema.json", - "title": "CultNet Raw Snapshot Response Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "documents" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.snapshot_response_raw.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "documents": { - "type": "array", - "items": { - "$ref": "./cultnet.raw-document-record.schema.json" - } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.snapshot-response.schema.json b/vendor/cultnet-rs/contracts/cultnet.snapshot-response.schema.json deleted file mode 100644 index 940822c..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.snapshot-response.schema.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.snapshot-response.schema.json", - "title": "CultNet Snapshot Response Message", - "type": "object", - "required": [ - "schemaVersion", - "messageId", - "documents" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.snapshot_response.v0" }, - "messageId": { "type": "string", "minLength": 1 }, - "documents": { - "type": "array", - "items": { - "$ref": "./cultnet.document-record.schema.json" - } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.transport-profile.schema.json b/vendor/cultnet-rs/contracts/cultnet.transport-profile.schema.json deleted file mode 100644 index cc237e7..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.transport-profile.schema.json +++ /dev/null @@ -1,81 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.transport-profile.schema.json", - "title": "CultNet Transport Profile", - "type": "object", - "required": [ - "schemaVersion", - "runtimeId", - "transports" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.transport_profile.v0" }, - "runtimeId": { "type": "string", "minLength": 1 }, - "transports": { - "type": "array", - "items": { - "type": "object", - "required": [ - "transportId", - "protocol", - "channels" - ], - "additionalProperties": false, - "properties": { - "transportId": { "type": "string", "minLength": 1 }, - "protocol": { - "type": "string", - "enum": [ - "tcp_framed", - "litenetlib", - "websocket", - "rudp" - ] - }, - "host": { "type": "string", "minLength": 1 }, - "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, - "path": { "type": "string", "minLength": 1 }, - "discoveryGroup": { "type": "string", "minLength": 1 }, - "wireContracts": { - "type": "array", - "items": { "type": "string", "minLength": 1 } - }, - "channels": { - "type": "array", - "items": { - "type": "object", - "required": [ - "channelId", - "delivery", - "ordering" - ], - "additionalProperties": false, - "properties": { - "channelId": { "type": "string", "minLength": 1 }, - "delivery": { - "type": "string", - "enum": [ - "reliable", - "unreliable" - ] - }, - "ordering": { - "type": "string", - "enum": [ - "ordered", - "unordered", - "sequenced" - ] - }, - "maxPayloadBytes": { "type": "integer", "minimum": 1 }, - "maxFragmentBytes": { "type": "integer", "minimum": 1 }, - "maxPendingReliablePackets": { "type": "integer", "minimum": 1 } - } - } - } - } - } - } - } -} diff --git a/vendor/cultnet-rs/contracts/cultnet.verify.schema.json b/vendor/cultnet-rs/contracts/cultnet.verify.schema.json deleted file mode 100644 index 1f992aa..0000000 --- a/vendor/cultnet-rs/contracts/cultnet.verify.schema.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.verify.schema.json", - "title": "CultNet Verify Message", - "type": "object", - "required": [ - "schemaVersion", - "nonce", - "session" - ], - "additionalProperties": false, - "properties": { - "schemaVersion": { "const": "cultnet.verify.v0" }, - "nonce": { "type": "string", "minLength": 1 }, - "session": { "type": "string", "minLength": 1 } - } -} diff --git a/vendor/cultnet-rs/contracts/ghostlight.agent-state.schema.json b/vendor/cultnet-rs/contracts/ghostlight.agent-state.schema.json deleted file mode 100644 index 6437cd0..0000000 --- a/vendor/cultnet-rs/contracts/ghostlight.agent-state.schema.json +++ /dev/null @@ -1,845 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/GameCult/Ghostlight/schemas/agent-state.schema.json", - "title": "Ghostlight Agent State Schema", - "description": "First canonical contract for Ghostlight agent, perception, relationship, event, scene, and dialogue-context state.", - "type": "object", - "required": [ - "schema_version", - "world", - "agents", - "relationships", - "events", - "scenes" - ], - "additionalProperties": false, - "properties": { - "schema_version": { - "const": "ghostlight.agent_state.v0" - }, - "world": { - "$ref": "#/$defs/world_state" - }, - "agents": { - "type": "array", - "items": { - "$ref": "#/$defs/agent" - } - }, - "relationships": { - "type": "array", - "items": { - "$ref": "#/$defs/relationship" - } - }, - "events": { - "type": "array", - "items": { - "$ref": "#/$defs/event" - } - }, - "scenes": { - "type": "array", - "items": { - "$ref": "#/$defs/scene" - } - } - }, - "$defs": { - "world_state": { - "type": "object", - "required": [ - "world_id", - "setting", - "time", - "canon_context" - ], - "additionalProperties": false, - "properties": { - "world_id": { - "type": "string" - }, - "setting": { - "type": "string" - }, - "time": { - "type": "object", - "required": [ - "label" - ], - "additionalProperties": true, - "properties": { - "label": { - "type": "string" - } - } - }, - "canon_context": { - "type": "array", - "items": { - "type": "string" - } - }, - "ambient_pressures": { - "type": "array", - "items": { - "$ref": "#/$defs/pressure" - } - } - } - }, - "agent": { - "type": "object", - "required": [ - "agent_id", - "identity", - "canonical_state", - "goals", - "memories", - "perceived_state_overlays" - ], - "additionalProperties": false, - "properties": { - "agent_id": { - "type": "string" - }, - "identity": { - "$ref": "#/$defs/identity" - }, - "canonical_state": { - "$ref": "#/$defs/canonical_state" - }, - "goals": { - "type": "array", - "items": { - "$ref": "#/$defs/goal" - } - }, - "memories": { - "$ref": "#/$defs/memory_bundle" - }, - "perceived_state_overlays": { - "type": "array", - "items": { - "$ref": "#/$defs/perceived_state_overlay" - } - } - } - }, - "identity": { - "type": "object", - "required": [ - "name", - "roles", - "origin", - "public_description" - ], - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "roles": { - "type": "array", - "items": { - "type": "string" - } - }, - "origin": { - "type": "string" - }, - "public_description": { - "type": "string" - }, - "private_notes": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "canonical_state": { - "type": "object", - "required": [ - "underlying_organization", - "stable_dispositions", - "behavioral_dimensions", - "presentation_strategy", - "voice_style", - "situational_state", - "values" - ], - "additionalProperties": false, - "properties": { - "underlying_organization": { - "$ref": "#/$defs/variable_map" - }, - "stable_dispositions": { - "$ref": "#/$defs/variable_map" - }, - "behavioral_dimensions": { - "$ref": "#/$defs/variable_map" - }, - "presentation_strategy": { - "$ref": "#/$defs/variable_map" - }, - "situational_state": { - "$ref": "#/$defs/variable_map" - }, - "values": { - "type": "array", - "items": { - "$ref": "#/$defs/value_commitment" - } - }, - "voice_style": { - "$ref": "#/$defs/variable_map" - } - } - }, - "variable_map": { - "type": "object", - "additionalProperties": { - "$ref": "#/$defs/state_variable" - } - }, - "state_variable": { - "type": "object", - "required": [ - "mean", - "plasticity", - "current_activation" - ], - "additionalProperties": false, - "properties": { - "mean": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "plasticity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "current_activation": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "evidence": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "value_commitment": { - "type": "object", - "required": [ - "value_id", - "label", - "priority", - "unforgivable_if_betrayed" - ], - "additionalProperties": false, - "properties": { - "value_id": { - "type": "string" - }, - "label": { - "type": "string" - }, - "priority": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "unforgivable_if_betrayed": { - "type": "boolean" - } - } - }, - "goal": { - "type": "object", - "required": [ - "goal_id", - "description", - "scope", - "priority", - "emotional_stake", - "status" - ], - "additionalProperties": false, - "properties": { - "goal_id": { - "type": "string" - }, - "description": { - "type": "string" - }, - "scope": { - "enum": [ - "immediate", - "scene", - "case", - "arc", - "life" - ] - }, - "priority": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "emotional_stake": { - "type": "string" - }, - "blockers": { - "type": "array", - "items": { - "type": "string" - } - }, - "status": { - "enum": [ - "active", - "blocked", - "dormant", - "resolved", - "abandoned" - ] - } - } - }, - "memory_bundle": { - "type": "object", - "required": [ - "episodic", - "semantic", - "relationship_summaries" - ], - "additionalProperties": false, - "properties": { - "episodic": { - "type": "array", - "items": { - "$ref": "#/$defs/memory" - } - }, - "semantic": { - "type": "array", - "items": { - "$ref": "#/$defs/memory" - } - }, - "relationship_summaries": { - "type": "array", - "items": { - "$ref": "#/$defs/memory" - } - } - } - }, - "memory": { - "type": "object", - "required": [ - "memory_id", - "summary", - "salience", - "confidence" - ], - "additionalProperties": false, - "properties": { - "memory_id": { - "type": "string" - }, - "summary": { - "type": "string" - }, - "salience": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "linked_event_ids": { - "type": "array", - "items": { - "type": "string" - } - }, - "linked_relationship_id": { - "type": "string" - } - } - }, - "perceived_state_overlay": { - "type": "object", - "required": [ - "observer_agent_id", - "target_agent_id", - "perceived_dimensions", - "beliefs", - "distortions" - ], - "additionalProperties": false, - "properties": { - "observer_agent_id": { - "type": "string" - }, - "target_agent_id": { - "type": "string" - }, - "perceived_dimensions": { - "$ref": "#/$defs/inferred_variable_map" - }, - "beliefs": { - "type": "array", - "items": { - "$ref": "#/$defs/belief" - } - }, - "distortions": { - "type": "array", - "items": { - "enum": [ - "hostile_attribution", - "false_reassurance", - "overread_warmth", - "underread_threat", - "false_mask_detection", - "missed_mask_detection", - "status_injury", - "threat_amplification", - "institutional_suspicion_transfer", - "care_underread", - "mask_overfocus" - ] - } - } - } - }, - "belief": { - "type": "object", - "required": [ - "belief_id", - "claim", - "confidence" - ], - "additionalProperties": false, - "properties": { - "belief_id": { - "type": "string" - }, - "claim": { - "type": "string" - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "subject_id": { - "type": "string" - }, - "claim_type": { - "enum": [ - "fact", - "identity_inference", - "motive_inference", - "prediction", - "norm", - "value_read", - "relationship_read" - ] - }, - "evidence_event_ids": { - "type": "array", - "items": { - "type": "string" - } - }, - "visibility": { - "enum": [ - "private", - "shared", - "public", - "rumor" - ] - }, - "emotional_charge": { - "type": "number", - "minimum": 0, - "maximum": 1 - } - } - }, - "relationship": { - "type": "object", - "required": [ - "relationship_id", - "source_id", - "target_id", - "stance", - "summary" - ], - "additionalProperties": false, - "properties": { - "relationship_id": { - "type": "string" - }, - "source_id": { - "type": "string" - }, - "target_id": { - "type": "string" - }, - "stance": { - "$ref": "#/$defs/variable_map" - }, - "summary": { - "type": "string" - }, - "unresolved_incident_ids": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "event": { - "type": "object", - "required": [ - "event_id", - "kind", - "summary", - "participants", - "pressure_tags" - ], - "additionalProperties": false, - "properties": { - "event_id": { - "type": "string" - }, - "kind": { - "enum": [ - "dialogue", - "action", - "memory", - "relationship_shift", - "scene_outcome", - "author_note" - ] - }, - "summary": { - "type": "string" - }, - "participants": { - "type": "array", - "items": { - "type": "string" - } - }, - "pressure_tags": { - "type": "array", - "items": { - "type": "string" - } - }, - "observed_exchange": { - "type": "array", - "items": { - "$ref": "#/$defs/event_exchange_turn" - } - }, - "private_interpretations": { - "type": "array", - "items": { - "$ref": "#/$defs/event_private_interpretation" - } - }, - "event_effects": { - "type": "array", - "items": { - "$ref": "#/$defs/event_effect" - } - } - } - }, - "scene": { - "type": "object", - "required": [ - "scene_id", - "location", - "participants", - "public_stakes", - "hidden_stakes", - "dialogue_context_packs" - ], - "additionalProperties": false, - "properties": { - "scene_id": { - "type": "string" - }, - "location": { - "type": "string" - }, - "participants": { - "type": "array", - "items": { - "type": "string" - } - }, - "public_stakes": { - "type": "array", - "items": { - "type": "string" - } - }, - "hidden_stakes": { - "type": "array", - "items": { - "type": "string" - } - }, - "dialogue_context_packs": { - "type": "array", - "items": { - "$ref": "#/$defs/dialogue_context_pack" - } - } - } - }, - "dialogue_context_pack": { - "type": "object", - "required": [ - "speaker_agent_id", - "listener_ids", - "speaker_local_truth", - "speaker_beliefs", - "active_memories", - "active_goals", - "presentation_constraints" - ], - "additionalProperties": false, - "properties": { - "speaker_agent_id": { - "type": "string" - }, - "listener_ids": { - "type": "array", - "items": { - "type": "string" - } - }, - "speaker_local_truth": { - "type": "array", - "items": { - "type": "string" - } - }, - "speaker_beliefs": { - "type": "array", - "items": { - "type": "string" - } - }, - "active_memories": { - "type": "array", - "items": { - "type": "string" - } - }, - "active_goals": { - "type": "array", - "items": { - "type": "string" - } - }, - "presentation_constraints": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "pressure": { - "type": "object", - "required": [ - "pressure_id", - "label", - "intensity" - ], - "additionalProperties": false, - "properties": { - "pressure_id": { - "type": "string" - }, - "label": { - "type": "string" - }, - "intensity": { - "type": "number", - "minimum": 0, - "maximum": 1 - } - } - }, - "inferred_variable_map": { - "type": "object", - "additionalProperties": { - "$ref": "#/$defs/inferred_state_variable" - } - }, - "inferred_state_variable": { - "type": "object", - "required": [ - "observed_activation", - "attributed_disposition", - "confidence" - ], - "additionalProperties": false, - "properties": { - "observed_activation": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "attributed_disposition": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "evidence": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "event_exchange_turn": { - "type": "object", - "required": [ - "speaker_id", - "utterance_summary", - "dialogue_function" - ], - "additionalProperties": false, - "properties": { - "speaker_id": { - "type": "string" - }, - "utterance_summary": { - "type": "string" - }, - "dialogue_function": { - "type": "string" - }, - "observable_cues": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "event_private_interpretation": { - "type": "object", - "required": [ - "agent_id", - "interpretation", - "confidence" - ], - "additionalProperties": false, - "properties": { - "agent_id": { - "type": "string" - }, - "interpretation": { - "type": "string" - }, - "attributed_motive": { - "type": "string" - }, - "appraisal_tags": { - "type": "array", - "items": { - "type": "string" - } - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - } - } - }, - "event_effect": { - "type": "object", - "required": [ - "agent_id", - "summary" - ], - "additionalProperties": false, - "properties": { - "agent_id": { - "type": "string" - }, - "summary": { - "type": "string" - }, - "state_deltas": { - "type": "array", - "items": { - "$ref": "#/$defs/state_delta" - } - }, - "new_belief_ids": { - "type": "array", - "items": { - "type": "string" - } - }, - "memory_update_ids": { - "type": "array", - "items": { - "type": "string" - } - } - } - }, - "state_delta": { - "type": "object", - "required": [ - "path", - "delta" - ], - "additionalProperties": false, - "properties": { - "path": { - "type": "string" - }, - "delta": { - "type": "number", - "minimum": -1, - "maximum": 1 - } - } - } - } -} diff --git a/vendor/cultnet-rs/examples/cultnet_interop_peer.rs b/vendor/cultnet-rs/examples/cultnet_interop_peer.rs deleted file mode 100644 index b9c1492..0000000 --- a/vendor/cultnet-rs/examples/cultnet_interop_peer.rs +++ /dev/null @@ -1,1030 +0,0 @@ -use anyhow::Context; -use anyhow::Result; -use anyhow::anyhow; -use cultcache_rs::CultCache; -use cultcache_rs::DatabaseEntry; -use cultcache_rs::SingleFileMessagePackBackingStore; -use cultnet_rs::CultNetDocumentBinding; -use cultnet_rs::CultNetDocumentMutationContract; -use cultnet_rs::CultNetDocumentOperation; -use cultnet_rs::CultNetDocumentPutOptions; -use cultnet_rs::CultNetDocumentRegistry; -use cultnet_rs::CultNetMessage; -use cultnet_rs::CultNetMutationAuthority; -use cultnet_rs::CultNetReadOnlySnapshotPolicy; -use cultnet_rs::CultNetSchemaKind; -use cultnet_rs::CultNetSchemaRegistration; -use cultnet_rs::CultNetSchemaRegistry; -use cultnet_rs::CultNetTransportChannel; -use cultnet_rs::CultNetTransportDelivery; -use cultnet_rs::CultNetTransportDescriptor; -use cultnet_rs::CultNetTransportOrdering; -use cultnet_rs::CultNetTransportProfile; -use cultnet_rs::CultNetTransportProtocol; -use cultnet_rs::CultNetWireContract; -use cultnet_rs::builtin_schema_registry; -use cultnet_rs::decode_cultnet_message_from_slice; -use cultnet_rs::encode_cultnet_message_to_vec; -use cultnet_rs::encode_frame; -use cultnet_rs::serve_read_only_raw_snapshot; -use serde::Deserialize; -use serde::Serialize; -use socket2::Domain; -use socket2::Protocol; -use socket2::Socket; -use socket2::Type; -use std::collections::BTreeMap; -use std::fs; -use std::io::Read; -use std::io::Write; -use std::net::Ipv4Addr; -use std::net::SocketAddr; -use std::net::SocketAddrV4; -use std::net::TcpListener; -use std::net::TcpStream; -use std::net::UdpSocket; -use std::sync::Arc; -use std::sync::Mutex; -use std::thread; -use std::time::Duration; - -const INTEROP_DOCUMENT_TYPE: &str = "cultnet.interop-note"; -const INTEROP_SCHEMA_VERSION: &str = "cultnet.interop_note.v0"; -const MUTATION_INTENT_TYPE: &str = "cultnet.interop-note-mutation-intent"; -const MUTATION_INTENT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-note-mutation-intent.schema.json"; -const MUTATION_INTENT_SCHEMA_VERSION: &str = "cultnet.interop_note_mutation_intent.v0"; -const MUTATION_RECEIPT_TYPE: &str = "cultnet.interop-note-mutation-receipt"; -const MUTATION_RECEIPT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-note-mutation-receipt.schema.json"; -const MUTATION_RECEIPT_SCHEMA_VERSION: &str = "cultnet.interop_note_mutation_receipt.v0"; -const FIRE_COMMAND_TYPE: &str = "cultnet.interop-fire-weapon-command"; -const FIRE_COMMAND_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-fire-weapon-command.schema.json"; -const FIRE_COMMAND_SCHEMA_VERSION: &str = "cultnet.interop_fire_weapon_command.v0"; -const FIRE_RECEIPT_TYPE: &str = "cultnet.interop-fire-weapon-receipt"; -const FIRE_RECEIPT_SCHEMA_ID: &str = "https://github.com/GameCult/cultnet-ts/integration/contracts/cultnet.interop-fire-weapon-receipt.schema.json"; -const FIRE_RECEIPT_SCHEMA_VERSION: &str = "cultnet.interop_fire_weapon_receipt.v0"; -const DISCOVERY_ANNOUNCE_SCHEMA_VERSION: &str = "cultnet.discovery_announce.v0"; - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "cultnet.interop-note", schema = "CultNetInteropNote")] -struct CultNetInteropNote { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - document_id: String, - #[cultcache(key = 2)] - author_runtime_id: String, - #[cultcache(key = 3)] - title: String, - #[cultcache(key = 4)] - body: String, - #[cultcache(key = 5)] - tags: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "cultnet.interop-note-mutation-intent", - schema = "CultNetInteropMutationIntent" -)] -struct CultNetInteropMutationIntent { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - intent_id: String, - #[cultcache(key = 2)] - target_document_id: String, - #[cultcache(key = 3)] - append_body: String, - #[cultcache(key = 4)] - append_tag: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "cultnet.interop-note-mutation-receipt", - schema = "CultNetInteropMutationReceipt" -)] -struct CultNetInteropMutationReceipt { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - intent_id: String, - #[cultcache(key = 2)] - accepted: bool, - #[cultcache(key = 3)] - document_id: String, - #[cultcache(key = 4)] - body: String, - #[cultcache(key = 5)] - tags: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "cultnet.interop-fire-weapon-command", - schema = "CultNetInteropFireCommand" -)] -struct CultNetInteropFireCommand { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - command_id: String, - #[cultcache(key = 2)] - character_id: String, - #[cultcache(key = 3)] - weapon_id: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "cultnet.interop-fire-weapon-receipt", - schema = "CultNetInteropFireReceipt" -)] -struct CultNetInteropFireReceipt { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - command_id: String, - #[cultcache(key = 2)] - accepted: bool, - #[cultcache(key = 3)] - character_id: String, - #[cultcache(key = 4)] - weapon_id: String, - #[cultcache(key = 5)] - shots_fired: u32, - #[cultcache(key = 6)] - ammo_remaining: u32, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(tag = "schemaVersion", rename_all = "camelCase")] -enum DiscoveryMessage { - #[serde(rename = "cultnet.discovery_probe.v0", rename_all = "camelCase")] - Probe { - message_id: String, - requester_runtime_id: String, - }, - #[serde(rename = "cultnet.discovery_announce.v0", rename_all = "camelCase")] - Announce { - message_id: String, - runtime_id: String, - runtime_kind: String, - display_name: String, - agent_id: Option, - tcp_host: String, - tcp_port: u16, - wire_contract: String, - supported_document_types: Vec, - supports_schema_catalog: bool, - }, -} - -#[derive(Clone, Debug)] -struct PeerConfig { - runtime_id: String, - runtime_kind: String, - display_name: String, - agent_id: String, - bind_host: String, - advertise_host: String, - tcp_port: u16, - discovery_port: u16, - discovery_group: Ipv4Addr, - schema_path: String, -} - -#[derive(Clone, Debug)] -struct DialConfig { - runtime_id: String, - runtime_kind: String, - display_name: String, - agent_id: String, - target_host: String, - target_port: u16, - schema_path: String, -} - -fn main() -> Result<()> { - let mut args = std::env::args().skip(1); - let mode = args - .next() - .ok_or_else(|| anyhow!("expected mode: serve | probe | dial"))?; - let options = parse_args(args.collect()); - - match mode.as_str() { - "serve" => serve(parse_peer_config(&options)?)?, - "probe" => probe(&options)?, - "dial" => dial(parse_dial_config(&options)?)?, - _ => return Err(anyhow!("unknown mode {mode}")), - } - - Ok(()) -} - -fn serve(config: PeerConfig) -> Result<()> { - let schema_registration = load_schema_registration(&config.schema_path)?; - let mut schema_registry = builtin_schema_registry()?; - schema_registry.register(schema_registration.clone())?; - - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - register_capability_entry_types(&mut cache)?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(runtime_store_path( - &config.runtime_id, - ))); - cache.pull_all_backing_stores()?; - let note = build_note(&config.runtime_id, &config.display_name); - cache.put(¬e.document_id, ¬e)?; - - let mut document_registry = CultNetDocumentRegistry::new(); - register_capability_bindings(&mut document_registry, &schema_registration.schema_id); - - let cache = Arc::new(Mutex::new(cache)); - let document_registry = Arc::new(document_registry); - let schema_registry = Arc::new(schema_registry); - let config = Arc::new(config); - - start_udp_discovery_server(config.clone())?; - start_tcp_server(config.clone(), cache, document_registry, schema_registry)?; - - print_json(&serde_json::json!({ - "status": "ready", - "mode": "serve", - "runtimeId": config.runtime_id, - "runtimeKind": config.runtime_kind, - "tcpPort": config.tcp_port, - "discoveryPort": config.discovery_port, - "discoveryGroup": config.discovery_group.to_string(), - }))?; - - loop { - thread::sleep(Duration::from_secs(3600)); - } -} - -fn probe(options: &BTreeMap) -> Result<()> { - let runtime_id = require_arg(options, "runtime-id")?.to_string(); - let discovery_port = parse_u16_arg(options, "discovery-port")?; - let discovery_group = parse_ipv4_arg(options, "discovery-group")?; - let timeout_ms = parse_u64_arg(options, "timeout-ms").unwrap_or(1_500); - let message_id = format!("{runtime_id}-{}", chrono::Utc::now().timestamp_millis()); - - let socket = create_discovery_socket(0, false)?; - socket.set_read_timeout(Some(Duration::from_millis(timeout_ms)))?; - - let probe_message = DiscoveryMessage::Probe { - message_id: message_id.clone(), - requester_runtime_id: runtime_id.clone(), - }; - let payload = rmp_serde::to_vec_named(&probe_message)?; - socket.send_to( - &payload, - SocketAddr::V4(SocketAddrV4::new(discovery_group, discovery_port)), - )?; - - let mut buffer = vec![0_u8; 4096]; - let mut found = BTreeMap::::new(); - loop { - match socket.recv_from(&mut buffer) { - Ok((len, _)) => { - if let Ok(DiscoveryMessage::Announce { - message_id: response_message_id, - runtime_id, - runtime_kind, - display_name, - agent_id, - tcp_host, - tcp_port, - wire_contract, - supported_document_types, - supports_schema_catalog, - }) = rmp_serde::from_slice::(&buffer[..len]) - { - if response_message_id == message_id { - found.insert( - runtime_id.clone(), - serde_json::json!({ - "schemaVersion": DISCOVERY_ANNOUNCE_SCHEMA_VERSION, - "messageId": response_message_id, - "runtimeId": runtime_id, - "runtimeKind": runtime_kind, - "displayName": display_name, - "agentId": agent_id, - "tcpHost": tcp_host, - "tcpPort": tcp_port, - "wireContract": wire_contract, - "supportedDocumentTypes": supported_document_types, - "supportsSchemaCatalog": supports_schema_catalog, - }), - ); - } - } - } - Err(error) - if error.kind() == std::io::ErrorKind::WouldBlock - || error.kind() == std::io::ErrorKind::TimedOut => - { - break; - } - Err(error) => return Err(error.into()), - } - } - - print_json(&serde_json::json!({ - "mode": "probe", - "runtimeId": runtime_id, - "peers": found.into_values().collect::>(), - }))?; - Ok(()) -} - -fn dial(config: DialConfig) -> Result<()> { - let schema_registration = load_schema_registration(&config.schema_path)?; - - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - register_capability_entry_types(&mut cache)?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(runtime_store_path( - &format!("{}-dial", config.runtime_id), - ))); - cache.pull_all_backing_stores()?; - - let mut document_registry = CultNetDocumentRegistry::new(); - register_capability_bindings(&mut document_registry, &schema_registration.schema_id); - - let mut stream = TcpStream::connect((config.target_host.as_str(), config.target_port)) - .with_context(|| { - format!( - "failed to connect to {}:{}", - config.target_host, config.target_port - ) - })?; - - send_message( - &mut stream, - &CultNetMessage::Hello { - runtime_id: config.runtime_id.clone(), - runtime_kind: config.runtime_kind.clone(), - agent_id: Some(config.agent_id.clone()), - role: None, - display_name: Some(config.display_name.clone()), - supported_document_types: Some(vec![INTEROP_DOCUMENT_TYPE.to_string()]), - supported_mutation_contracts: Some(interaction_contracts()), - supported_message_versions: Some(vec![INTEROP_SCHEMA_VERSION.to_string()]), - transport_profiles: Some(tcp_framed_transport_profiles( - &config.runtime_id, - None, - None, - None, - )), - supports_schema_catalog: Some(true), - }, - )?; - - let remote_hello = read_message(&mut stream)?; - let remote_runtime_id = match &remote_hello { - CultNetMessage::Hello { runtime_id, .. } => runtime_id.clone(), - other => return Err(anyhow!("expected hello response, got {other:?}")), - }; - - send_message( - &mut stream, - &CultNetMessage::SchemaCatalogRequest { - message_id: format!("{}-catalog", config.runtime_id), - include_schema_json: Some(true), - schema_ids: None, - kinds: None, - }, - )?; - let catalog_response = read_message(&mut stream)?; - let has_interop_schema = match &catalog_response { - CultNetMessage::SchemaCatalogResponse { schemas, .. } => schemas.iter().any(|schema| { - schema.schema_id == schema_registration.schema_id - && schema.document_type.as_deref() == Some(INTEROP_DOCUMENT_TYPE) - }), - other => return Err(anyhow!("expected catalog response, got {other:?}")), - }; - - send_message( - &mut stream, - &CultNetMessage::SnapshotRequest { - message_id: format!("{}-snapshot", config.runtime_id), - schema_ids: Some(vec![schema_registration.schema_id.clone()]), - record_keys: None, - }, - )?; - let snapshot_response = read_message(&mut stream)?; - let applied = document_registry - .apply_raw_snapshot_response::(&mut cache, &snapshot_response)?; - let note = applied - .into_iter() - .find(|candidate| candidate.author_runtime_id == remote_runtime_id) - .ok_or_else(|| anyhow!("did not receive an interop note from {remote_runtime_id}"))?; - - print_json(&serde_json::json!({ - "mode": "dial", - "runtimeId": config.runtime_id, - "targetHost": config.target_host, - "targetPort": config.target_port, - "remoteHello": { - "schemaVersion": "cultnet.hello.v0", - "runtimeId": remote_runtime_id, - }, - "hasInteropSchema": has_interop_schema, - "retrievedNote": { - "schemaVersion": note.schema_version, - "documentId": note.document_id, - "authorRuntimeId": note.author_runtime_id, - "title": note.title, - "body": note.body, - "tags": note.tags, - }, - "mutatedNote": mutate_remote_note(&mut stream, &mut cache, &document_registry, &schema_registration.schema_id, &config.runtime_id, ¬e)?, - "fireReceipt": fire_remote_weapon(&mut stream, &mut cache, &document_registry, &config.runtime_id, &remote_runtime_id)?, - }))?; - Ok(()) -} - -fn mutate_remote_note( - stream: &mut TcpStream, - cache: &mut CultCache, - document_registry: &CultNetDocumentRegistry, - note_schema_id: &str, - runtime_id: &str, - note: &CultNetInteropNote, -) -> Result { - let intent = CultNetInteropMutationIntent { - schema_version: MUTATION_INTENT_SCHEMA_VERSION.to_string(), - intent_id: format!("{runtime_id}-decorate"), - target_document_id: note.document_id.clone(), - append_body: format!(" Decorated by {runtime_id}."), - append_tag: format!("decorated:{runtime_id}"), - }; - let message = document_registry.create_raw_document_put_message( - format!("{runtime_id}-decorate-put"), - intent.intent_id.clone(), - &intent, - CultNetDocumentPutOptions::default(), - )?; - send_message(stream, &message)?; - - let receipt_message = read_message(stream)?; - let _receipt = document_registry - .apply_raw_document_put_message::(cache, &receipt_message)?; - let mutated_message = read_message(stream)?; - let mutated = document_registry - .apply_raw_document_put_message::(cache, &mutated_message)?; - if mutated_message_schema_id(&mutated_message) != Some(note_schema_id) { - return Err(anyhow!("mutation response used the wrong schema id")); - } - Ok(serde_json::json!({ - "schemaVersion": mutated.schema_version, - "documentId": mutated.document_id, - "authorRuntimeId": mutated.author_runtime_id, - "title": mutated.title, - "body": mutated.body, - "tags": mutated.tags, - })) -} - -fn fire_remote_weapon( - stream: &mut TcpStream, - cache: &mut CultCache, - document_registry: &CultNetDocumentRegistry, - runtime_id: &str, - remote_runtime_id: &str, -) -> Result { - let command = CultNetInteropFireCommand { - schema_version: FIRE_COMMAND_SCHEMA_VERSION.to_string(), - command_id: format!("{runtime_id}-fire"), - character_id: remote_runtime_id.to_string(), - weapon_id: "interop-rifle".to_string(), - }; - let message = document_registry.create_raw_document_put_message( - format!("{runtime_id}-fire-put"), - command.command_id.clone(), - &command, - CultNetDocumentPutOptions::default(), - )?; - send_message(stream, &message)?; - let receipt_message = read_message(stream)?; - let receipt = document_registry - .apply_raw_document_put_message::(cache, &receipt_message)?; - Ok(serde_json::json!({ - "schemaVersion": receipt.schema_version, - "commandId": receipt.command_id, - "accepted": receipt.accepted, - "characterId": receipt.character_id, - "weaponId": receipt.weapon_id, - "shotsFired": receipt.shots_fired, - "ammoRemaining": receipt.ammo_remaining, - })) -} - -fn start_udp_discovery_server(config: Arc) -> Result<()> { - let socket = create_discovery_socket(config.discovery_port, true)?; - socket.join_multicast_v4(&config.discovery_group, &Ipv4Addr::UNSPECIFIED)?; - socket.set_read_timeout(Some(Duration::from_millis(250)))?; - - thread::spawn(move || { - let mut buffer = vec![0_u8; 4096]; - loop { - match socket.recv_from(&mut buffer) { - Ok((len, remote)) => { - if let Ok(DiscoveryMessage::Probe { - message_id, - requester_runtime_id: _, - }) = rmp_serde::from_slice::(&buffer[..len]) - { - let announce = DiscoveryMessage::Announce { - message_id, - runtime_id: config.runtime_id.clone(), - runtime_kind: config.runtime_kind.clone(), - display_name: config.display_name.clone(), - agent_id: Some(config.agent_id.clone()), - tcp_host: config.advertise_host.clone(), - tcp_port: config.tcp_port, - wire_contract: "cultnet.schema.v0".to_string(), - supported_document_types: vec![INTEROP_DOCUMENT_TYPE.to_string()], - supports_schema_catalog: true, - }; - if let Ok(payload) = rmp_serde::to_vec_named(&announce) { - let _ = socket.send_to(&payload, remote); - } - } - } - Err(error) - if error.kind() == std::io::ErrorKind::WouldBlock - || error.kind() == std::io::ErrorKind::TimedOut => {} - Err(_) => break, - } - } - }); - - Ok(()) -} - -fn start_tcp_server( - config: Arc, - cache: Arc>, - document_registry: Arc, - schema_registry: Arc, -) -> Result<()> { - let listener = - TcpListener::bind((config.bind_host.as_str(), config.tcp_port)).with_context(|| { - format!( - "failed to bind TCP listener on {}:{}", - config.bind_host, config.tcp_port - ) - })?; - listener.set_nonblocking(false)?; - - thread::spawn(move || { - for stream in listener.incoming() { - let Ok(stream) = stream else { - continue; - }; - let config = config.clone(); - let cache = cache.clone(); - let document_registry = document_registry.clone(); - let schema_registry = schema_registry.clone(); - thread::spawn(move || { - if let Err(error) = - handle_connection(stream, config, cache, document_registry, schema_registry) - { - eprintln!("{error:#}"); - } - }); - } - }); - - Ok(()) -} - -fn handle_connection( - mut stream: TcpStream, - config: Arc, - cache: Arc>, - document_registry: Arc, - schema_registry: Arc, -) -> Result<()> { - loop { - let message = match read_message(&mut stream) { - Ok(message) => message, - Err(error) if is_eof_like(&error) => break, - Err(error) => return Err(error), - }; - - match message { - CultNetMessage::Hello { .. } => { - send_message( - &mut stream, - &CultNetMessage::Hello { - runtime_id: config.runtime_id.clone(), - runtime_kind: config.runtime_kind.clone(), - agent_id: Some(config.agent_id.clone()), - role: None, - display_name: Some(config.display_name.clone()), - supported_document_types: Some(vec![INTEROP_DOCUMENT_TYPE.to_string()]), - supported_mutation_contracts: Some(interaction_contracts()), - supported_message_versions: Some(vec![INTEROP_SCHEMA_VERSION.to_string()]), - transport_profiles: Some(tcp_framed_transport_profiles( - &config.runtime_id, - Some(&config.advertise_host), - Some(config.tcp_port), - Some(config.discovery_group), - )), - supports_schema_catalog: Some(true), - }, - )?; - } - request @ CultNetMessage::SchemaCatalogRequest { .. } => { - let response = schema_registry.create_catalog_response(&request)?; - send_message(&mut stream, &response)?; - } - request @ CultNetMessage::SnapshotRequest { .. } => { - // Snapshot serving is a pure read. The backing projection owns - // the records and provenance; the protocol loop only filters - // an explicit allowlist and returns their exact payload bytes. - let (source, policy) = { - let cache = cache.lock().expect("cache poisoned"); - let mut records = Vec::new(); - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - for envelope in cache.snapshot() { - let mut record = - document_registry.raw_document_record_from_envelope(&envelope)?; - record.source_runtime_id = Some(config.runtime_id.clone()); - record.source_agent_id = Some(config.agent_id.clone()); - record.source_role = Some("peer".to_string()); - record.tags = Some(vec!["interop".to_string(), config.runtime_id.clone()]); - policy.allow(&record.schema_id, &record.record_key)?; - records.push(record); - } - (records, policy) - }; - let response = - serve_read_only_raw_snapshot(&document_registry, &policy, &source, &request)?; - send_message(&mut stream, &response)?; - } - message @ CultNetMessage::DocumentPutRaw { .. } => { - handle_raw_put(&mut stream, &config, &cache, &document_registry, &message)?; - } - _ => {} - } - } - - Ok(()) -} - -fn handle_raw_put( - stream: &mut TcpStream, - config: &PeerConfig, - cache: &Arc>, - document_registry: &CultNetDocumentRegistry, - message: &CultNetMessage, -) -> Result<()> { - let CultNetMessage::DocumentPutRaw { document, .. } = message else { - return Ok(()); - }; - if document.schema_id == MUTATION_INTENT_SCHEMA_ID { - let mut cache = cache.lock().expect("cache poisoned"); - let intent = document_registry - .apply_raw_document_put_message::(&mut cache, message)?; - let mut note = cache.get_required::(&intent.target_document_id)?; - note.body = format!("{}{}", note.body, intent.append_body); - note.tags.push(intent.append_tag); - cache.put(¬e.document_id, ¬e)?; - let receipt = CultNetInteropMutationReceipt { - schema_version: MUTATION_RECEIPT_SCHEMA_VERSION.to_string(), - intent_id: intent.intent_id.clone(), - accepted: true, - document_id: note.document_id.clone(), - body: note.body.clone(), - tags: note.tags.clone(), - }; - let options = response_options(config, "mutation"); - let receipt_message = document_registry.create_raw_document_put_message( - format!("{}-mutation-receipt", config.runtime_id), - receipt.intent_id.clone(), - &receipt, - options.clone(), - )?; - let note_message = document_registry.create_raw_document_put_message( - format!("{}-mutated-note", config.runtime_id), - note.document_id.clone(), - ¬e, - options, - )?; - send_message(stream, &receipt_message)?; - send_message(stream, ¬e_message)?; - } else if document.schema_id == FIRE_COMMAND_SCHEMA_ID { - let mut cache = cache.lock().expect("cache poisoned"); - let command = document_registry - .apply_raw_document_put_message::(&mut cache, message)?; - let receipt = CultNetInteropFireReceipt { - schema_version: FIRE_RECEIPT_SCHEMA_VERSION.to_string(), - command_id: command.command_id, - accepted: true, - character_id: command.character_id, - weapon_id: command.weapon_id, - shots_fired: 1, - ammo_remaining: 29, - }; - let receipt_message = document_registry.create_raw_document_put_message( - format!("{}-fire-receipt", config.runtime_id), - receipt.command_id.clone(), - &receipt, - response_options(config, "side-effect"), - )?; - send_message(stream, &receipt_message)?; - } - Ok(()) -} - -fn load_schema_registration(schema_path: &str) -> Result { - let schema_json = fs::read_to_string(schema_path) - .with_context(|| format!("failed to read schema {}", schema_path))?; - let parsed: serde_json::Value = serde_json::from_str(&schema_json) - .with_context(|| format!("failed to parse schema {}", schema_path))?; - let schema_id = parsed - .get("$id") - .and_then(serde_json::Value::as_str) - .ok_or_else(|| anyhow!("schema {} is missing $id", schema_path))? - .to_string(); - let title = parsed - .get("title") - .and_then(serde_json::Value::as_str) - .map(ToString::to_string); - - Ok(CultNetSchemaRegistration { - schema_id, - kind: CultNetSchemaKind::DocumentPayload, - wire_contracts: vec![CultNetWireContract::CultNetSchemaV0], - schema_version: Some(INTEROP_SCHEMA_VERSION.to_string()), - document_type: Some(INTEROP_DOCUMENT_TYPE.to_string()), - title, - schema_json: Some(schema_json), - }) -} - -fn register_capability_entry_types(cache: &mut CultCache) -> Result<()> { - cache.register_entry_type::()?; - cache.register_entry_type::()?; - cache.register_entry_type::()?; - cache.register_entry_type::()?; - Ok(()) -} - -fn register_capability_bindings( - document_registry: &mut CultNetDocumentRegistry, - note_schema_id: &str, -) { - document_registry - .register(CultNetDocumentBinding::for_entry_with_schema_id::< - CultNetInteropNote, - >( - note_schema_id.to_string(), - INTEROP_SCHEMA_VERSION.to_string(), - )) - .register(CultNetDocumentBinding::for_entry_with_schema_id::< - CultNetInteropMutationIntent, - >( - MUTATION_INTENT_SCHEMA_ID.to_string(), - MUTATION_INTENT_SCHEMA_VERSION.to_string(), - )) - .register(CultNetDocumentBinding::for_entry_with_schema_id::< - CultNetInteropMutationReceipt, - >( - MUTATION_RECEIPT_SCHEMA_ID.to_string(), - MUTATION_RECEIPT_SCHEMA_VERSION.to_string(), - )) - .register(CultNetDocumentBinding::for_entry_with_schema_id::< - CultNetInteropFireCommand, - >( - FIRE_COMMAND_SCHEMA_ID.to_string(), - FIRE_COMMAND_SCHEMA_VERSION.to_string(), - )) - .register(CultNetDocumentBinding::for_entry_with_schema_id::< - CultNetInteropFireReceipt, - >( - FIRE_RECEIPT_SCHEMA_ID.to_string(), - FIRE_RECEIPT_SCHEMA_VERSION.to_string(), - )); -} - -fn interaction_contracts() -> Vec { - vec![CultNetDocumentMutationContract { - document_type: INTEROP_DOCUMENT_TYPE.to_string(), - payload_schema_version: Some(INTEROP_SCHEMA_VERSION.to_string()), - operations: vec![ - CultNetDocumentOperation::Snapshot, - CultNetDocumentOperation::DocumentPut, - CultNetDocumentOperation::IntentSubmit, - CultNetDocumentOperation::ReceiptWatch, - ], - authority: CultNetMutationAuthority::Runtime, - intent_document_types: Some(vec![ - MUTATION_INTENT_TYPE.to_string(), - FIRE_COMMAND_TYPE.to_string(), - ]), - receipt_document_types: Some(vec![ - MUTATION_RECEIPT_TYPE.to_string(), - FIRE_RECEIPT_TYPE.to_string(), - ]), - notes: None, - }] -} - -fn tcp_framed_transport_profiles( - runtime_id: &str, - host: Option<&str>, - port: Option, - discovery_group: Option, -) -> Vec { - vec![CultNetTransportProfile { - schema_version: "cultnet.transport_profile.v0".to_string(), - runtime_id: runtime_id.to_string(), - transports: vec![CultNetTransportDescriptor { - transport_id: "interop-tcp-framed".to_string(), - protocol: CultNetTransportProtocol::TcpFramed, - host: host.map(ToString::to_string), - port, - path: None, - discovery_group: discovery_group.map(|group| group.to_string()), - wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]), - channels: vec![ - CultNetTransportChannel { - channel_id: "schema".to_string(), - delivery: CultNetTransportDelivery::Reliable, - ordering: CultNetTransportOrdering::Ordered, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - reliable_expire_after_ms: None, - }, - CultNetTransportChannel { - channel_id: "state".to_string(), - delivery: CultNetTransportDelivery::Reliable, - ordering: CultNetTransportOrdering::Ordered, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - reliable_expire_after_ms: None, - }, - ], - }], - }] -} - -fn response_options(config: &PeerConfig, tag: &str) -> CultNetDocumentPutOptions { - CultNetDocumentPutOptions { - source_runtime_id: Some(config.runtime_id.clone()), - source_agent_id: Some(config.agent_id.clone()), - source_role: Some("peer".to_string()), - tags: Some(vec![tag.to_string(), config.runtime_id.clone()]), - ..CultNetDocumentPutOptions::default() - } -} - -fn mutated_message_schema_id(message: &CultNetMessage) -> Option<&str> { - match message { - CultNetMessage::DocumentPutRaw { document, .. } => Some(document.schema_id.as_str()), - _ => None, - } -} - -fn build_note(runtime_id: &str, display_name: &str) -> CultNetInteropNote { - CultNetInteropNote { - schema_version: INTEROP_SCHEMA_VERSION.to_string(), - document_id: format!("note:{runtime_id}"), - author_runtime_id: runtime_id.to_string(), - title: format!("{display_name} keeps a little note"), - body: format!( - "{runtime_id} can move CultNet state without begging the gods for translation." - ), - tags: vec![ - runtime_id.to_string(), - "interop".to_string(), - "cultnet".to_string(), - ], - } -} - -fn send_message(stream: &mut TcpStream, message: &CultNetMessage) -> Result<()> { - let payload = encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?; - let frame = encode_frame(&payload)?; - stream.write_all(&frame)?; - stream.flush()?; - Ok(()) -} - -fn read_message(stream: &mut TcpStream) -> Result { - let mut header = [0_u8; 4]; - stream.read_exact(&mut header)?; - let payload_len = u32::from_be_bytes(header) as usize; - let mut payload = vec![0_u8; payload_len]; - stream.read_exact(&mut payload)?; - decode_cultnet_message_from_slice(&payload, CultNetWireContract::CultNetSchemaV0) -} - -fn is_eof_like(error: &anyhow::Error) -> bool { - error - .downcast_ref::() - .is_some_and(|io| io.kind() == std::io::ErrorKind::UnexpectedEof) -} - -fn create_discovery_socket(port: u16, _join_group: bool) -> Result { - let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?; - socket.set_reuse_address(true)?; - socket.bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port).into())?; - let socket = UdpSocket::from(socket); - socket.set_multicast_loop_v4(true)?; - socket.set_multicast_ttl_v4(1)?; - socket.set_nonblocking(false)?; - Ok(socket) -} - -fn parse_peer_config(options: &BTreeMap) -> Result { - Ok(PeerConfig { - runtime_id: require_arg(options, "runtime-id")?.to_string(), - runtime_kind: require_arg(options, "runtime-kind")?.to_string(), - display_name: require_arg(options, "display-name")?.to_string(), - agent_id: require_arg(options, "agent-id")?.to_string(), - bind_host: options - .get("bind-host") - .cloned() - .unwrap_or_else(|| "127.0.0.1".to_string()), - advertise_host: require_arg(options, "advertise-host")?.to_string(), - tcp_port: parse_u16_arg(options, "tcp-port")?, - discovery_port: parse_u16_arg(options, "discovery-port")?, - discovery_group: parse_ipv4_arg(options, "discovery-group")?, - schema_path: require_arg(options, "schema-path")?.to_string(), - }) -} - -fn parse_dial_config(options: &BTreeMap) -> Result { - Ok(DialConfig { - runtime_id: require_arg(options, "runtime-id")?.to_string(), - runtime_kind: require_arg(options, "runtime-kind")?.to_string(), - display_name: require_arg(options, "display-name")?.to_string(), - agent_id: require_arg(options, "agent-id")?.to_string(), - target_host: require_arg(options, "target-host")?.to_string(), - target_port: parse_u16_arg(options, "target-port")?, - schema_path: require_arg(options, "schema-path")?.to_string(), - }) -} - -fn parse_args(args: Vec) -> BTreeMap { - let mut parsed = BTreeMap::new(); - let mut index = 0; - while index < args.len() { - let token = &args[index]; - if !token.starts_with("--") { - index += 1; - continue; - } - let name = token.trim_start_matches("--").to_string(); - let value = args - .get(index + 1) - .cloned() - .unwrap_or_else(|| panic!("missing value for --{name}")); - parsed.insert(name, value); - index += 2; - } - parsed -} - -fn require_arg<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { - options - .get(name) - .map(String::as_str) - .ok_or_else(|| anyhow!("missing required argument --{name}")) -} - -fn parse_u16_arg(options: &BTreeMap, name: &str) -> Result { - require_arg(options, name)? - .parse::() - .with_context(|| format!("argument --{name} must be a u16")) -} - -fn parse_u64_arg(options: &BTreeMap, name: &str) -> Option { - options - .get(name) - .map(|value| value.parse::().expect("u64 arg")) -} - -fn parse_ipv4_arg(options: &BTreeMap, name: &str) -> Result { - require_arg(options, name)? - .parse::() - .with_context(|| format!("argument --{name} must be an IPv4 address")) -} - -fn runtime_store_path(runtime_id: &str) -> std::path::PathBuf { - std::env::temp_dir().join(format!("cultnet-rs-interop-{runtime_id}.cc")) -} - -fn print_json(value: &serde_json::Value) -> Result<()> { - println!("{}", serde_json::to_string(value)?); - Ok(()) -} diff --git a/vendor/cultnet-rs/src/contracts.rs b/vendor/cultnet-rs/src/contracts.rs deleted file mode 100644 index 460764d..0000000 --- a/vendor/cultnet-rs/src/contracts.rs +++ /dev/null @@ -1,1224 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; -use base64::Engine; -use base64::engine::general_purpose::URL_SAFE_NO_PAD; -use serde::Deserialize; -use serde::Serialize; -use serde_json::Value; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum CultNetWireContract { - #[serde(rename = "cultnet.schema.v0")] - CultNetSchemaV0, - #[serde(rename = "gamecult.networking.v0")] - GameCultNetworkingV0, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum CultNetSchemaKind { - WireMessage, - DocumentPayload, - SharedContract, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum CultNetRawPayloadEncoding { - Messagepack, -} - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetDocumentRecord { - pub schema_id: String, - pub record_key: String, - pub stored_at: String, - pub payload: TPayload, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_runtime_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_agent_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_role: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub tags: Option>, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum CultNetDocumentOperation { - Snapshot, - DocumentPut, - DocumentDelete, - IntentSubmit, - ReceiptWatch, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum CultNetMutationAuthority { - ReadOnly, - LocalUser, - Coordinator, - Runtime, - Denied, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetDocumentMutationContract { - pub document_type: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub payload_schema_version: Option, - pub operations: Vec, - pub authority: CultNetMutationAuthority, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub intent_document_types: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub receipt_document_types: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub notes: Option>, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetTransportProfile { - pub schema_version: String, - pub runtime_id: String, - pub transports: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetTransportDescriptor { - pub transport_id: String, - pub protocol: CultNetTransportProtocol, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub host: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub port: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub path: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub discovery_group: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub wire_contracts: Option>, - pub channels: Vec, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum CultNetTransportProtocol { - TcpFramed, - Litenetlib, - Websocket, - Rudp, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetTransportChannel { - pub channel_id: String, - pub delivery: CultNetTransportDelivery, - pub ordering: CultNetTransportOrdering, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub max_payload_bytes: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub max_fragment_bytes: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub max_pending_reliable_packets: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub reliable_expire_after_ms: Option, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum CultNetTransportDelivery { - Reliable, - Unreliable, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum CultNetTransportOrdering { - Ordered, - Unordered, - Sequenced, -} - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetRawDocumentRecord { - pub schema_id: String, - pub record_key: String, - pub stored_at: String, - pub payload_encoding: CultNetRawPayloadEncoding, - #[serde(with = "serde_bytes")] - pub payload: Vec, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_runtime_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_agent_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub source_role: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub tags: Option>, -} - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetSchemaDescriptor { - pub schema_id: String, - pub kind: CultNetSchemaKind, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub schema_version: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub document_type: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub title: Option, - pub wire_contracts: Vec, - pub content_hash: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub schema_json: Option, -} - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(tag = "schemaVersion")] -pub enum CultNetMessage { - #[serde(rename = "cultnet.hello.v0", rename_all = "camelCase")] - Hello { - runtime_id: String, - runtime_kind: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - agent_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - role: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - display_name: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - supported_document_types: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - supported_mutation_contracts: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - supported_message_versions: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - transport_profiles: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - supports_schema_catalog: Option, - }, - #[serde(rename = "cultnet.login.v0", rename_all = "camelCase")] - Login { - nonce: String, - auth: String, - password: String, - }, - #[serde(rename = "cultnet.register.v0", rename_all = "camelCase")] - Register { - nonce: String, - email: String, - password: String, - name: String, - }, - #[serde(rename = "cultnet.verify.v0", rename_all = "camelCase")] - Verify { nonce: String, session: String }, - #[serde(rename = "cultnet.login_success.v0", rename_all = "camelCase")] - LoginSuccess { nonce: String, session: String }, - #[serde(rename = "cultnet.error.v0", rename_all = "camelCase")] - Error { error: String }, - #[serde(rename = "cultnet.sample.change_name.v0", rename_all = "camelCase")] - SampleChangeName { name: String }, - #[serde(rename = "cultnet.sample.chat.v0", rename_all = "camelCase")] - SampleChat { text: String }, - #[serde(rename = "cultnet.document_put.v0", rename_all = "camelCase")] - DocumentPut { - message_id: String, - document: CultNetDocumentRecord, - }, - #[serde(rename = "cultnet.document_delete.v0", rename_all = "camelCase")] - DocumentDelete { - message_id: String, - schema_id: String, - record_key: String, - }, - #[serde(rename = "cultnet.document_put_raw.v0", rename_all = "camelCase")] - DocumentPutRaw { - message_id: String, - document: CultNetRawDocumentRecord, - }, - #[serde(rename = "cultnet.snapshot_request.v0", rename_all = "camelCase")] - SnapshotRequest { - message_id: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - schema_ids: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - record_keys: Option>, - }, - #[serde(rename = "cultnet.snapshot_response.v0", rename_all = "camelCase")] - SnapshotResponse { - message_id: String, - documents: Vec>, - }, - #[serde(rename = "cultnet.snapshot_response_raw.v0", rename_all = "camelCase")] - SnapshotResponseRaw { - message_id: String, - documents: Vec, - }, - #[serde(rename = "cultnet.schema_catalog_request.v0", rename_all = "camelCase")] - SchemaCatalogRequest { - message_id: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - include_schema_json: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - schema_ids: Option>, - #[serde(default, skip_serializing_if = "Option::is_none")] - kinds: Option>, - }, - #[serde( - rename = "cultnet.schema_catalog_response.v0", - rename_all = "camelCase" - )] - SchemaCatalogResponse { - message_id: String, - schemas: Vec, - }, -} - -pub fn parse_cultnet_message( - wire_value: &rmpv::Value, - contract: CultNetWireContract, -) -> Result { - match contract { - CultNetWireContract::CultNetSchemaV0 => { - let message = match schema_version_from_wire_value(wire_value)? { - Some("cultnet.document_put_raw.v0" | "cultnet.snapshot_response_raw.v0") => { - parse_raw_cultnet_schema_message(wire_value)? - } - _ => { - let json_value: Value = rmp_serde::from_slice(&rmp_serde::to_vec(wire_value)?)?; - serde_json::from_value(json_value)? - } - }; - validate_message(&message)?; - Ok(message) - } - CultNetWireContract::GameCultNetworkingV0 => parse_gamecult_networking_message(wire_value), - } -} - -pub fn encode_cultnet_message_for_wire( - message: &CultNetMessage, - contract: CultNetWireContract, -) -> Result { - validate_message(message)?; - match contract { - CultNetWireContract::CultNetSchemaV0 => match message { - CultNetMessage::DocumentPutRaw { .. } | CultNetMessage::SnapshotResponseRaw { .. } => { - encode_raw_cultnet_schema_message(message) - } - _ => Ok(rmp_serde::from_slice(&rmp_serde::to_vec( - &serde_json::to_value(message)?, - )?)?), - }, - CultNetWireContract::GameCultNetworkingV0 => encode_gamecult_networking_message(message), - } -} - -pub fn encode_cultnet_message_to_vec( - message: &CultNetMessage, - contract: CultNetWireContract, -) -> Result> { - let wire_value = encode_cultnet_message_for_wire(message, contract)?; - Ok(rmp_serde::to_vec(&wire_value)?) -} - -pub fn decode_cultnet_message_from_slice( - bytes: &[u8], - contract: CultNetWireContract, -) -> Result { - if contract == CultNetWireContract::CultNetSchemaV0 { - let wire_value: rmpv::Value = rmp_serde::from_slice(bytes)?; - return parse_cultnet_message(&wire_value, contract); - } - let wire_value: rmpv::Value = rmp_serde::from_slice(bytes)?; - parse_cultnet_message(&wire_value, contract) -} - -fn validate_message(message: &CultNetMessage) -> Result<()> { - match message { - CultNetMessage::Hello { - runtime_id, - runtime_kind, - agent_id, - role, - display_name, - supported_document_types, - supported_mutation_contracts, - supported_message_versions, - transport_profiles: _, - supports_schema_catalog: _, - } => { - require_non_empty(runtime_id, "runtimeId")?; - require_non_empty(runtime_kind, "runtimeKind")?; - require_optional_non_empty(agent_id.as_deref(), "agentId")?; - require_optional_non_empty(role.as_deref(), "role")?; - require_optional_non_empty(display_name.as_deref(), "displayName")?; - require_optional_string_vec( - supported_document_types.as_deref(), - "supportedDocumentTypes", - )?; - if let Some(contracts) = supported_mutation_contracts { - for contract in contracts { - validate_mutation_contract(contract)?; - } - } - require_optional_string_vec( - supported_message_versions.as_deref(), - "supportedMessageVersions", - )?; - } - CultNetMessage::Login { - nonce, - auth, - password, - } => { - require_non_empty(nonce, "nonce")?; - require_non_empty(auth, "auth")?; - require_non_empty(password, "password")?; - } - CultNetMessage::Register { - nonce, - email, - password, - name, - } => { - require_non_empty(nonce, "nonce")?; - require_non_empty(email, "email")?; - require_non_empty(password, "password")?; - require_non_empty(name, "name")?; - } - CultNetMessage::Verify { nonce, session } - | CultNetMessage::LoginSuccess { nonce, session } => { - require_non_empty(nonce, "nonce")?; - require_non_empty(session, "session")?; - } - CultNetMessage::Error { error } => require_non_empty(error, "error")?, - CultNetMessage::SampleChangeName { name } => require_non_empty(name, "name")?, - CultNetMessage::SampleChat { text } => require_non_empty(text, "text")?, - CultNetMessage::DocumentPut { - message_id, - document, - } => { - require_non_empty(message_id, "messageId")?; - validate_document_record(document)?; - } - CultNetMessage::DocumentDelete { - message_id, - schema_id, - record_key, - } => { - require_non_empty(message_id, "messageId")?; - require_non_empty(schema_id, "schemaId")?; - require_non_empty(record_key, "recordKey")?; - } - CultNetMessage::DocumentPutRaw { - message_id, - document, - } => { - require_non_empty(message_id, "messageId")?; - validate_raw_document_record(document)?; - } - CultNetMessage::SnapshotRequest { - message_id, - schema_ids, - record_keys, - } => { - require_non_empty(message_id, "messageId")?; - require_optional_string_vec(schema_ids.as_deref(), "schemaIds")?; - require_optional_string_vec(record_keys.as_deref(), "recordKeys")?; - } - CultNetMessage::SnapshotResponse { - message_id, - documents, - } => { - require_non_empty(message_id, "messageId")?; - for document in documents { - validate_document_record(document)?; - } - } - CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } => { - require_non_empty(message_id, "messageId")?; - for document in documents { - validate_raw_document_record(document)?; - } - } - CultNetMessage::SchemaCatalogRequest { - message_id, - include_schema_json: _, - schema_ids, - kinds: _, - } => { - require_non_empty(message_id, "messageId")?; - require_optional_string_vec(schema_ids.as_deref(), "schemaIds")?; - } - CultNetMessage::SchemaCatalogResponse { - message_id, - schemas, - } => { - require_non_empty(message_id, "messageId")?; - for schema in schemas { - validate_schema_descriptor(schema)?; - } - } - } - Ok(()) -} - -fn validate_mutation_contract(contract: &CultNetDocumentMutationContract) -> Result<()> { - require_non_empty(&contract.document_type, "documentType")?; - require_optional_non_empty( - contract.payload_schema_version.as_deref(), - "payloadSchemaVersion", - )?; - if contract.operations.is_empty() { - return Err(anyhow!( - "CultNet mutation contract for {:?} must advertise at least one operation", - contract.document_type - )); - } - require_optional_string_vec( - contract.intent_document_types.as_deref(), - "intentDocumentTypes", - )?; - require_optional_string_vec( - contract.receipt_document_types.as_deref(), - "receiptDocumentTypes", - )?; - require_optional_string_vec(contract.notes.as_deref(), "notes")?; - Ok(()) -} - -fn validate_document_record(document: &CultNetDocumentRecord) -> Result<()> { - require_non_empty(&document.schema_id, "schemaId")?; - require_non_empty(&document.record_key, "recordKey")?; - require_non_empty(&document.stored_at, "storedAt")?; - require_optional_non_empty(document.source_runtime_id.as_deref(), "sourceRuntimeId")?; - require_optional_non_empty(document.source_agent_id.as_deref(), "sourceAgentId")?; - require_optional_non_empty(document.source_role.as_deref(), "sourceRole")?; - require_optional_string_vec(document.tags.as_deref(), "tags")?; - Ok(()) -} - -fn validate_raw_document_record(document: &CultNetRawDocumentRecord) -> Result<()> { - require_non_empty(&document.schema_id, "schemaId")?; - require_non_empty(&document.record_key, "recordKey")?; - require_non_empty(&document.stored_at, "storedAt")?; - if document.payload.is_empty() { - return Err(anyhow!( - "CultNet field payload must contain non-empty MessagePack bytes" - )); - } - require_optional_non_empty(document.source_runtime_id.as_deref(), "sourceRuntimeId")?; - require_optional_non_empty(document.source_agent_id.as_deref(), "sourceAgentId")?; - require_optional_non_empty(document.source_role.as_deref(), "sourceRole")?; - require_optional_string_vec(document.tags.as_deref(), "tags")?; - Ok(()) -} - -fn validate_schema_descriptor(schema: &CultNetSchemaDescriptor) -> Result<()> { - require_non_empty(&schema.schema_id, "schemaId")?; - require_optional_non_empty(schema.schema_version.as_deref(), "schemaVersion")?; - require_optional_non_empty(schema.document_type.as_deref(), "documentType")?; - require_optional_non_empty(schema.title.as_deref(), "title")?; - require_non_empty(&schema.content_hash, "contentHash")?; - require_optional_non_empty(schema.schema_json.as_deref(), "schemaJson")?; - - if schema.wire_contracts.is_empty() { - return Err(anyhow!( - "CultNet field wireContracts must contain at least one supported contract" - )); - } - - Ok(()) -} - -fn require_non_empty(value: &str, field: &str) -> Result<()> { - if value.trim().is_empty() { - return Err(anyhow!("CultNet field {field} must be non-empty")); - } - Ok(()) -} - -fn require_optional_non_empty(value: Option<&str>, field: &str) -> Result<()> { - if let Some(value) = value { - require_non_empty(value, field)?; - } - Ok(()) -} - -fn require_optional_string_vec(value: Option<&[String]>, field: &str) -> Result<()> { - if let Some(values) = value { - for item in values { - require_non_empty(item, field)?; - } - } - Ok(()) -} - -fn schema_version_from_wire_value(input: &rmpv::Value) -> Result> { - let Some(object) = input.as_map() else { - return Ok(None); - }; - - Ok(object.iter().find_map(|(key, value)| { - key.as_str() - .filter(|candidate| *candidate == "schemaVersion") - .and_then(|_| value.as_str()) - })) -} - -fn parse_raw_cultnet_schema_message(input: &rmpv::Value) -> Result { - let object = input - .as_map() - .ok_or_else(|| anyhow!("cultnet.schema.v0 raw messages must be objects"))?; - - let get = |name: &str| -> Option<&rmpv::Value> { - object.iter().find_map(|(key, value)| { - key.as_str() - .filter(|candidate| *candidate == name) - .map(|_| value) - }) - }; - - let schema_version = require_legacy_string(get("schemaVersion"), "schemaVersion")?; - match schema_version.as_str() { - "cultnet.document_put_raw.v0" => Ok(CultNetMessage::DocumentPutRaw { - message_id: require_legacy_string(get("messageId"), "messageId")?, - document: require_raw_document_record(get("document"), "document")?, - }), - "cultnet.snapshot_response_raw.v0" => { - let documents = get("documents") - .and_then(rmpv::Value::as_array) - .ok_or_else(|| anyhow!("documents must be an array"))? - .iter() - .enumerate() - .map(|(index, value)| { - require_raw_document_record(value.into(), &format!("documents[{index}]")) - }) - .collect::>>()?; - - Ok(CultNetMessage::SnapshotResponseRaw { - message_id: require_legacy_string(get("messageId"), "messageId")?, - documents, - }) - } - _ => Err(anyhow!( - "Unsupported raw cultnet.schema.v0 schemaVersion {schema_version}" - )), - } -} - -fn encode_raw_cultnet_schema_message(message: &CultNetMessage) -> Result { - Ok(rmpv::Value::Map(match message { - CultNetMessage::DocumentPutRaw { - message_id, - document, - } => vec![ - ( - rmpv::Value::from("schemaVersion"), - rmpv::Value::from("cultnet.document_put_raw.v0"), - ), - ( - rmpv::Value::from("messageId"), - rmpv::Value::from(message_id.as_str()), - ), - ( - rmpv::Value::from("document"), - encode_raw_document_record(document), - ), - ], - CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } => vec![ - ( - rmpv::Value::from("schemaVersion"), - rmpv::Value::from("cultnet.snapshot_response_raw.v0"), - ), - ( - rmpv::Value::from("messageId"), - rmpv::Value::from(message_id.as_str()), - ), - ( - rmpv::Value::from("documents"), - rmpv::Value::Array(documents.iter().map(encode_raw_document_record).collect()), - ), - ], - _ => { - return Err(anyhow!( - "Message is not a raw cultnet.schema.v0 binary replication message" - )); - } - })) -} - -fn require_raw_document_record( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result { - let object = value - .and_then(rmpv::Value::as_map) - .ok_or_else(|| anyhow!("{field_name} must be an object"))?; - - let get = |name: &str| -> Option<&rmpv::Value> { - object.iter().find_map(|(key, value)| { - key.as_str() - .filter(|candidate| *candidate == name) - .map(|_| value) - }) - }; - - let payload_encoding = require_legacy_string( - get("payloadEncoding"), - &format!("{field_name}.payloadEncoding"), - )?; - - Ok(CultNetRawDocumentRecord { - schema_id: require_legacy_string(get("schemaId"), &format!("{field_name}.schemaId"))?, - record_key: require_legacy_string(get("recordKey"), &format!("{field_name}.recordKey"))?, - stored_at: require_legacy_string(get("storedAt"), &format!("{field_name}.storedAt"))?, - payload_encoding: match payload_encoding.as_str() { - "messagepack" => CultNetRawPayloadEncoding::Messagepack, - _ => { - return Err(anyhow!( - "{field_name}.payloadEncoding has unsupported value {payload_encoding}" - )); - } - }, - payload: get("payload") - .and_then(rmpv::Value::as_slice) - .map(|bytes| bytes.to_vec()) - .ok_or_else(|| anyhow!("{field_name}.payload must be binary MessagePack bytes"))?, - source_runtime_id: require_legacy_optional_string( - get("sourceRuntimeId"), - &format!("{field_name}.sourceRuntimeId"), - )?, - source_agent_id: require_legacy_optional_string( - get("sourceAgentId"), - &format!("{field_name}.sourceAgentId"), - )?, - source_role: require_legacy_optional_string( - get("sourceRole"), - &format!("{field_name}.sourceRole"), - )?, - tags: require_legacy_optional_string_array(get("tags"), &format!("{field_name}.tags"))?, - }) -} - -fn encode_raw_document_record(document: &CultNetRawDocumentRecord) -> rmpv::Value { - rmpv::Value::Map(vec![ - ( - rmpv::Value::from("schemaId"), - rmpv::Value::from(document.schema_id.as_str()), - ), - ( - rmpv::Value::from("recordKey"), - rmpv::Value::from(document.record_key.as_str()), - ), - ( - rmpv::Value::from("storedAt"), - rmpv::Value::from(document.stored_at.as_str()), - ), - ( - rmpv::Value::from("payloadEncoding"), - rmpv::Value::from(match document.payload_encoding { - CultNetRawPayloadEncoding::Messagepack => "messagepack", - }), - ), - ( - rmpv::Value::from("payload"), - rmpv::Value::Binary(document.payload.clone()), - ), - ( - rmpv::Value::from("sourceRuntimeId"), - document - .source_runtime_id - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("sourceAgentId"), - document - .source_agent_id - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("sourceRole"), - document - .source_role - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("tags"), - legacy_optional_string_array(document.tags.as_deref()), - ), - ]) -} - -fn parse_gamecult_networking_message(input: &rmpv::Value) -> Result { - let items = input.as_array().ok_or_else(|| { - anyhow!("gamecult.networking.v0 messages must be a 2-element union array") - })?; - if items.len() != 2 { - return Err(anyhow!( - "gamecult.networking.v0 messages must be a 2-element union array" - )); - } - let tag = items[0] - .as_i64() - .ok_or_else(|| anyhow!("gamecult.networking.v0 message tag must be an integer"))?; - let payload = items[1] - .as_array() - .ok_or_else(|| anyhow!("gamecult.networking.v0 message payload must be an array"))?; - match tag { - 0 => Ok(CultNetMessage::Login { - nonce: require_legacy_bytes(payload.first(), "LoginMessage.Nonce")?, - auth: require_legacy_bytes(payload.get(1), "LoginMessage.Auth")?, - password: require_legacy_bytes(payload.get(2), "LoginMessage.Password")?, - }), - 1 => Ok(CultNetMessage::Register { - nonce: require_legacy_bytes(payload.first(), "RegisterMessage.Nonce")?, - email: require_legacy_bytes(payload.get(1), "RegisterMessage.Email")?, - password: require_legacy_bytes(payload.get(2), "RegisterMessage.Password")?, - name: require_legacy_bytes(payload.get(3), "RegisterMessage.Name")?, - }), - 2 => Ok(CultNetMessage::Verify { - nonce: require_legacy_bytes(payload.first(), "VerifyMessage.Nonce")?, - session: require_legacy_bytes(payload.get(1), "VerifyMessage.Session")?, - }), - 3 => Ok(CultNetMessage::LoginSuccess { - nonce: require_legacy_bytes(payload.first(), "LoginSuccessMessage.Nonce")?, - session: require_legacy_bytes(payload.get(1), "LoginSuccessMessage.Session")?, - }), - 4 => Ok(CultNetMessage::Error { - error: require_legacy_string(payload.first(), "ErrorMessage.Error")?, - }), - 5 => Ok(CultNetMessage::SampleChangeName { - name: require_legacy_string(payload.first(), "ChangeNameMessage.Name")?, - }), - 6 => Ok(CultNetMessage::SampleChat { - text: require_legacy_string(payload.first(), "ChatMessage.Text")?, - }), - 7 => Ok(CultNetMessage::SchemaCatalogRequest { - message_id: require_legacy_string( - payload.first(), - "SchemaCatalogRequestMessage.MessageId", - )?, - include_schema_json: Some(require_legacy_bool( - payload.get(1), - "SchemaCatalogRequestMessage.IncludeSchemaJson", - )?), - schema_ids: require_legacy_optional_string_array( - payload.get(2), - "SchemaCatalogRequestMessage.SchemaIds", - )?, - kinds: require_legacy_optional_schema_kind_array( - payload.get(3), - "SchemaCatalogRequestMessage.Kinds", - )?, - }), - 8 => Ok(CultNetMessage::SchemaCatalogResponse { - message_id: require_legacy_string( - payload.first(), - "SchemaCatalogResponseMessage.MessageId", - )?, - schemas: require_legacy_schema_descriptor_array( - payload.get(1), - "SchemaCatalogResponseMessage.Schemas", - )?, - }), - _ => Err(anyhow!( - "Unsupported gamecult.networking.v0 union tag {tag}" - )), - } -} - -fn encode_gamecult_networking_message(message: &CultNetMessage) -> Result { - let pair = match message { - CultNetMessage::Login { - nonce, - auth, - password, - } => vec![ - rmpv::Value::from(0), - rmpv::Value::Array(vec![ - legacy_bytes(nonce, "LoginMessage.Nonce")?, - legacy_bytes(auth, "LoginMessage.Auth")?, - legacy_bytes(password, "LoginMessage.Password")?, - ]), - ], - CultNetMessage::Register { - nonce, - email, - password, - name, - } => vec![ - rmpv::Value::from(1), - rmpv::Value::Array(vec![ - legacy_bytes(nonce, "RegisterMessage.Nonce")?, - legacy_bytes(email, "RegisterMessage.Email")?, - legacy_bytes(password, "RegisterMessage.Password")?, - legacy_bytes(name, "RegisterMessage.Name")?, - ]), - ], - CultNetMessage::Verify { nonce, session } => vec![ - rmpv::Value::from(2), - rmpv::Value::Array(vec![ - legacy_bytes(nonce, "VerifyMessage.Nonce")?, - legacy_bytes(session, "VerifyMessage.Session")?, - ]), - ], - CultNetMessage::LoginSuccess { nonce, session } => vec![ - rmpv::Value::from(3), - rmpv::Value::Array(vec![ - legacy_bytes(nonce, "LoginSuccessMessage.Nonce")?, - legacy_bytes(session, "LoginSuccessMessage.Session")?, - ]), - ], - CultNetMessage::Error { error } => { - vec![ - rmpv::Value::from(4), - rmpv::Value::Array(vec![error.as_str().into()]), - ] - } - CultNetMessage::SampleChangeName { name } => { - vec![ - rmpv::Value::from(5), - rmpv::Value::Array(vec![name.as_str().into()]), - ] - } - CultNetMessage::SampleChat { text } => { - vec![ - rmpv::Value::from(6), - rmpv::Value::Array(vec![text.as_str().into()]), - ] - } - CultNetMessage::SchemaCatalogRequest { - message_id, - include_schema_json, - schema_ids, - kinds, - } => vec![ - rmpv::Value::from(7), - rmpv::Value::Array(vec![ - message_id.as_str().into(), - rmpv::Value::from(include_schema_json.unwrap_or(false)), - legacy_optional_string_array(schema_ids.as_deref()), - legacy_optional_schema_kind_array(kinds.as_deref()), - ]), - ], - CultNetMessage::SchemaCatalogResponse { - message_id, - schemas, - } => vec![ - rmpv::Value::from(8), - rmpv::Value::Array(vec![ - message_id.as_str().into(), - rmpv::Value::Array( - schemas - .iter() - .map(legacy_schema_descriptor) - .collect::>>()?, - ), - ]), - ], - _ => { - return Err(anyhow!( - "Message is not defined in the gamecult.networking.v0 contract" - )); - } - }; - Ok(rmpv::Value::Array(pair)) -} - -fn require_legacy_bytes(value: Option<&rmpv::Value>, field_name: &str) -> Result { - let bytes = value - .and_then(rmpv::Value::as_slice) - .ok_or_else(|| anyhow!("{field_name} must be binary data in gamecult.networking.v0"))?; - Ok(URL_SAFE_NO_PAD.encode(bytes)) -} - -fn require_legacy_string(value: Option<&rmpv::Value>, field_name: &str) -> Result { - let value = value - .and_then(rmpv::Value::as_str) - .ok_or_else(|| anyhow!("{field_name} must be a string in gamecult.networking.v0"))?; - Ok(value.to_string()) -} - -fn require_legacy_bool(value: Option<&rmpv::Value>, field_name: &str) -> Result { - value - .and_then(rmpv::Value::as_bool) - .ok_or_else(|| anyhow!("{field_name} must be a boolean in gamecult.networking.v0")) -} - -fn require_legacy_optional_string_array( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result>> { - let Some(value) = value else { - return Ok(None); - }; - if value.is_nil() { - return Ok(None); - } - let items = value - .as_array() - .ok_or_else(|| anyhow!("{field_name} must be an array in gamecult.networking.v0"))?; - let values = items - .iter() - .map(|item| { - item.as_str() - .map(ToString::to_string) - .ok_or_else(|| anyhow!("{field_name} must contain only strings")) - }) - .collect::>>()?; - Ok(Some(values)) -} - -fn require_legacy_optional_schema_kind_array( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result>> { - let Some(values) = require_legacy_optional_string_array(value, field_name)? else { - return Ok(None); - }; - values - .into_iter() - .map(|value| match value.as_str() { - "wire_message" => Ok(CultNetSchemaKind::WireMessage), - "document_payload" => Ok(CultNetSchemaKind::DocumentPayload), - "shared_contract" => Ok(CultNetSchemaKind::SharedContract), - _ => Err(anyhow!("{field_name} has unsupported schema kind {value}")), - }) - .collect::>>() - .map(Some) -} - -fn require_legacy_schema_descriptor_array( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result> { - let items = value - .and_then(rmpv::Value::as_array) - .ok_or_else(|| anyhow!("{field_name} must be an array in gamecult.networking.v0"))?; - items - .iter() - .enumerate() - .map(|(index, item)| { - require_legacy_schema_descriptor(item, &format!("{field_name}[{index}]")) - }) - .collect() -} - -fn require_legacy_schema_descriptor( - value: &rmpv::Value, - field_name: &str, -) -> Result { - let object = value - .as_map() - .ok_or_else(|| anyhow!("{field_name} must be an object in gamecult.networking.v0"))?; - - let get = |name: &str| -> Option<&rmpv::Value> { - object.iter().find_map(|(key, value)| { - key.as_str() - .filter(|candidate| *candidate == name) - .map(|_| value) - }) - }; - - let schema_id = require_legacy_string(get("schemaId"), &format!("{field_name}.SchemaId"))?; - let kind = require_legacy_string(get("kind"), &format!("{field_name}.Kind"))?; - let schema_version = require_legacy_optional_string( - get("schemaVersion"), - &format!("{field_name}.SchemaVersion"), - )?; - let document_type = - require_legacy_optional_string(get("documentType"), &format!("{field_name}.DocumentType"))?; - let title = require_legacy_optional_string(get("title"), &format!("{field_name}.Title"))?; - let wire_contracts = require_legacy_optional_wire_contracts( - get("wireContracts"), - &format!("{field_name}.WireContracts"), - )?; - let content_hash = - require_legacy_string(get("contentHash"), &format!("{field_name}.ContentHash"))?; - let schema_json = - require_legacy_optional_string(get("schemaJson"), &format!("{field_name}.SchemaJson"))?; - - let kind = match kind.as_str() { - "wire_message" => CultNetSchemaKind::WireMessage, - "document_payload" => CultNetSchemaKind::DocumentPayload, - "shared_contract" => CultNetSchemaKind::SharedContract, - _ => { - return Err(anyhow!( - "{field_name}.Kind has unsupported schema kind {kind}" - )); - } - }; - - Ok(CultNetSchemaDescriptor { - schema_id, - kind, - schema_version, - document_type, - title, - wire_contracts, - content_hash, - schema_json, - }) -} - -fn require_legacy_optional_string( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result> { - let Some(value) = value else { - return Ok(None); - }; - if value.is_nil() { - return Ok(None); - } - Ok(Some(require_legacy_string(Some(value), field_name)?)) -} - -fn require_legacy_optional_wire_contracts( - value: Option<&rmpv::Value>, - field_name: &str, -) -> Result> { - let Some(values) = require_legacy_optional_string_array(value, field_name)? else { - return Ok(Vec::new()); - }; - - values - .into_iter() - .map(|value| match value.as_str() { - "cultnet.schema.v0" => Ok(CultNetWireContract::CultNetSchemaV0), - "gamecult.networking.v0" => Ok(CultNetWireContract::GameCultNetworkingV0), - _ => Err(anyhow!( - "{field_name} contains unsupported wire contract {value}" - )), - }) - .collect() -} - -fn legacy_bytes(input: &str, field_name: &str) -> Result { - if input.trim().is_empty() { - return Err(anyhow!("{field_name} must be a non-empty base64url string")); - } - Ok(rmpv::Value::Binary(URL_SAFE_NO_PAD.decode(input)?)) -} - -fn legacy_optional_string_array(input: Option<&[String]>) -> rmpv::Value { - match input { - Some(values) => rmpv::Value::Array( - values - .iter() - .map(|value| rmpv::Value::from(value.as_str())) - .collect(), - ), - None => rmpv::Value::Nil, - } -} - -fn legacy_optional_schema_kind_array(input: Option<&[CultNetSchemaKind]>) -> rmpv::Value { - match input { - Some(values) => rmpv::Value::Array( - values - .iter() - .map(|value| { - rmpv::Value::from(match value { - CultNetSchemaKind::WireMessage => "wire_message", - CultNetSchemaKind::DocumentPayload => "document_payload", - CultNetSchemaKind::SharedContract => "shared_contract", - }) - }) - .collect(), - ), - None => rmpv::Value::Nil, - } -} - -fn legacy_schema_descriptor(schema: &CultNetSchemaDescriptor) -> Result { - Ok(rmpv::Value::Map(vec![ - ( - rmpv::Value::from("schemaId"), - rmpv::Value::from(schema.schema_id.as_str()), - ), - ( - rmpv::Value::from("kind"), - rmpv::Value::from(match schema.kind { - CultNetSchemaKind::WireMessage => "wire_message", - CultNetSchemaKind::DocumentPayload => "document_payload", - CultNetSchemaKind::SharedContract => "shared_contract", - }), - ), - ( - rmpv::Value::from("schemaVersion"), - schema - .schema_version - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("documentType"), - schema - .document_type - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("title"), - schema - .title - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ( - rmpv::Value::from("wireContracts"), - rmpv::Value::Array( - schema - .wire_contracts - .iter() - .map(|value| { - rmpv::Value::from(match value { - CultNetWireContract::CultNetSchemaV0 => "cultnet.schema.v0", - CultNetWireContract::GameCultNetworkingV0 => "gamecult.networking.v0", - }) - }) - .collect(), - ), - ), - ( - rmpv::Value::from("contentHash"), - rmpv::Value::from(schema.content_hash.as_str()), - ), - ( - rmpv::Value::from("schemaJson"), - schema - .schema_json - .as_deref() - .map(rmpv::Value::from) - .unwrap_or(rmpv::Value::Nil), - ), - ])) -} diff --git a/vendor/cultnet-rs/src/framing.rs b/vendor/cultnet-rs/src/framing.rs deleted file mode 100644 index 0157881..0000000 --- a/vendor/cultnet-rs/src/framing.rs +++ /dev/null @@ -1,49 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; - -pub const FRAME_HEADER_BYTES: usize = 4; - -pub fn encode_frame(payload: &[u8]) -> Result> { - let len: u32 = payload - .len() - .try_into() - .map_err(|_| anyhow!("CultNet frame payload is too large"))?; - let mut frame = Vec::with_capacity(FRAME_HEADER_BYTES + payload.len()); - frame.extend_from_slice(&len.to_be_bytes()); - frame.extend_from_slice(payload); - Ok(frame) -} - -#[derive(Clone, Debug, Default)] -pub struct LengthPrefixedMessageFramer { - buffer: Vec, -} - -impl LengthPrefixedMessageFramer { - pub fn new() -> Self { - Self::default() - } - - pub fn push(&mut self, chunk: impl AsRef<[u8]>) -> Vec> { - self.buffer.extend_from_slice(chunk.as_ref()); - let mut frames = Vec::new(); - loop { - if self.buffer.len() < FRAME_HEADER_BYTES { - break; - } - let payload_len = u32::from_be_bytes([ - self.buffer[0], - self.buffer[1], - self.buffer[2], - self.buffer[3], - ]) as usize; - let total_len = FRAME_HEADER_BYTES + payload_len; - if self.buffer.len() < total_len { - break; - } - frames.push(self.buffer[FRAME_HEADER_BYTES..total_len].to_vec()); - self.buffer.drain(0..total_len); - } - frames - } -} diff --git a/vendor/cultnet-rs/src/lib.rs b/vendor/cultnet-rs/src/lib.rs deleted file mode 100644 index 71abe75..0000000 --- a/vendor/cultnet-rs/src/lib.rs +++ /dev/null @@ -1,19 +0,0 @@ -mod contracts; -mod framing; -mod replication; -mod rudp; -mod schema_discovery; -mod security; -mod service_identity; -mod snapshot_query; -mod transport; - -pub use contracts::*; -pub use framing::*; -pub use replication::*; -pub use rudp::*; -pub use schema_discovery::*; -pub use security::*; -pub use service_identity::*; -pub use snapshot_query::*; -pub use transport::*; diff --git a/vendor/cultnet-rs/src/replication.rs b/vendor/cultnet-rs/src/replication.rs deleted file mode 100644 index 85490e9..0000000 --- a/vendor/cultnet-rs/src/replication.rs +++ /dev/null @@ -1,442 +0,0 @@ -use anyhow::Context; -use anyhow::Result; -use anyhow::anyhow; -use cultcache_rs::CultCache; -use cultcache_rs::CultCacheEnvelope; -use cultcache_rs::DatabaseEntry; -use serde::Serialize; -use serde::de::DeserializeOwned; -use serde_json::Value; -use std::collections::BTreeMap; -use std::collections::BTreeSet; - -use crate::CultNetDocumentMutationContract; -use crate::CultNetDocumentRecord; -use crate::CultNetMessage; -use crate::CultNetRawDocumentRecord; -use crate::CultNetRawPayloadEncoding; - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetDocumentPutOptions { - pub stored_at: Option, - pub source_runtime_id: Option, - pub source_agent_id: Option, - pub source_role: Option, - pub tags: Option>, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetDocumentBinding { - pub schema_id: String, - pub document_type: String, - pub mutation_contract: Option, - pub payload_schema_version: Option, -} - -impl CultNetDocumentBinding { - pub fn for_entry(payload_schema_version: impl Into>) -> Self { - Self { - document_type: T::TYPE.to_string(), - schema_id: T::TYPE.to_string(), - mutation_contract: None, - payload_schema_version: payload_schema_version.into(), - } - } - - pub fn for_entry_with_schema_id( - schema_id: impl Into, - payload_schema_version: impl Into>, - ) -> Self { - Self { - document_type: T::TYPE.to_string(), - schema_id: schema_id.into(), - mutation_contract: None, - payload_schema_version: payload_schema_version.into(), - } - } - - pub fn with_mutation_contract(mut self, contract: CultNetDocumentMutationContract) -> Self { - self.mutation_contract = Some(contract); - self - } -} - -#[derive(Clone, Debug, Default)] -pub struct CultNetDocumentRegistry { - bindings_by_type: BTreeMap, - bindings_by_schema_id: BTreeMap, -} - -impl CultNetDocumentRegistry { - pub fn new() -> Self { - Self::default() - } - - pub fn register(&mut self, binding: CultNetDocumentBinding) -> &mut Self { - self.bindings_by_schema_id - .insert(binding.schema_id.clone(), binding.clone()); - self.bindings_by_type - .insert(binding.document_type.clone(), binding); - self - } - - pub fn binding(&self, document_type: &str) -> Option<&CultNetDocumentBinding> { - self.bindings_by_type.get(document_type) - } - - pub fn binding_by_schema_id(&self, schema_id: &str) -> Option<&CultNetDocumentBinding> { - self.bindings_by_schema_id.get(schema_id) - } - - pub fn mutation_contracts(&self) -> Vec { - self.bindings_by_type - .values() - .filter_map(|binding| binding.mutation_contract.clone()) - .collect() - } - - pub fn create_document_put_message( - &self, - message_id: impl Into, - record_key: impl Into, - value: &T, - options: CultNetDocumentPutOptions, - ) -> Result - where - T: DatabaseEntry + Serialize + DeserializeOwned, - { - let binding = self.require_binding(T::TYPE)?; - let payload = serde_json::to_value(round_trip_typed(value)?)?; - Ok(CultNetMessage::DocumentPut { - message_id: message_id.into(), - document: CultNetDocumentRecord { - schema_id: binding.schema_id.clone(), - record_key: record_key.into(), - stored_at: options.stored_at.unwrap_or_else(now_utc_second), - payload, - source_runtime_id: options.source_runtime_id, - source_agent_id: options.source_agent_id, - source_role: options.source_role, - tags: options.tags, - }, - }) - } - - pub fn create_document_delete_message( - &self, - message_id: impl Into, - schema_id: impl Into, - record_key: impl Into, - ) -> CultNetMessage { - CultNetMessage::DocumentDelete { - message_id: message_id.into(), - schema_id: schema_id.into(), - record_key: record_key.into(), - } - } - - pub fn create_raw_document_put_message_from_envelope( - &self, - message_id: impl Into, - envelope: &CultCacheEnvelope, - ) -> Result { - Ok(CultNetMessage::DocumentPutRaw { - message_id: message_id.into(), - document: self.raw_document_record_from_envelope(envelope)?, - }) - } - - pub fn create_raw_document_put_message( - &self, - message_id: impl Into, - record_key: impl Into, - value: &T, - options: CultNetDocumentPutOptions, - ) -> Result - where - T: DatabaseEntry + Serialize, - { - let binding = self.require_binding(T::TYPE)?; - Ok(CultNetMessage::DocumentPutRaw { - message_id: message_id.into(), - document: CultNetRawDocumentRecord { - schema_id: binding.schema_id.clone(), - record_key: record_key.into(), - stored_at: options.stored_at.unwrap_or_else(now_utc_second), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: rmp_serde::to_vec(value)?, - source_runtime_id: options.source_runtime_id, - source_agent_id: options.source_agent_id, - source_role: options.source_role, - tags: options.tags, - }, - }) - } - - pub fn create_snapshot_response( - &self, - cache: &CultCache, - message_id: impl Into, - schema_ids: Option<&[String]>, - record_keys: Option<&[String]>, - ) -> Result { - let requested_schema_ids = schema_ids.map(|items| items.iter().collect::>()); - let requested_record_keys = record_keys.map(|items| items.iter().collect::>()); - let mut documents = Vec::new(); - for envelope in cache.snapshot() { - let binding = self.require_binding(&envelope.r#type)?; - if requested_schema_ids - .as_ref() - .is_some_and(|ids| !ids.contains(&binding.schema_id)) - { - continue; - } - if requested_record_keys - .as_ref() - .is_some_and(|keys| !keys.contains(&envelope.key)) - { - continue; - } - documents.push(self.document_record_from_envelope(&envelope)?); - } - Ok(CultNetMessage::SnapshotResponse { - message_id: message_id.into(), - documents, - }) - } - - pub fn create_raw_snapshot_response( - &self, - cache: &CultCache, - message_id: impl Into, - schema_ids: Option<&[String]>, - record_keys: Option<&[String]>, - ) -> Result { - let requested_schema_ids = schema_ids.map(|items| items.iter().collect::>()); - let requested_record_keys = record_keys.map(|items| items.iter().collect::>()); - let mut documents = Vec::new(); - for envelope in cache.snapshot() { - let binding = self.require_binding(&envelope.r#type)?; - if requested_schema_ids - .as_ref() - .is_some_and(|ids| !ids.contains(&binding.schema_id)) - { - continue; - } - if requested_record_keys - .as_ref() - .is_some_and(|keys| !keys.contains(&envelope.key)) - { - continue; - } - documents.push(self.raw_document_record_from_envelope(&envelope)?); - } - Ok(CultNetMessage::SnapshotResponseRaw { - message_id: message_id.into(), - documents, - }) - } - - pub fn apply_document_put_message( - &self, - cache: &mut CultCache, - message: &CultNetMessage, - ) -> Result - where - T: DatabaseEntry + Serialize + DeserializeOwned, - { - let CultNetMessage::DocumentPut { document, .. } = message else { - return Err(anyhow!("expected cultnet.document_put.v0")); - }; - let binding = self.require_binding(T::TYPE)?; - if document.schema_id != binding.schema_id { - return Err(anyhow!( - "schema id {:?} does not match registered Rust type {:?} schema {:?}", - document.schema_id, - T::TYPE, - binding.schema_id - )); - } - let value: T = serde_json::from_value(document.payload.clone()).with_context(|| { - format!( - "failed to decode CultNet payload schema {:?} as {}", - binding.schema_id, - T::SCHEMA_NAME - ) - })?; - cache.put(&document.record_key, &value) - } - - pub fn apply_document_delete_message( - &self, - cache: &mut CultCache, - message: &CultNetMessage, - ) -> Result - where - T: DatabaseEntry, - { - let CultNetMessage::DocumentDelete { - schema_id, - record_key, - .. - } = message - else { - return Err(anyhow!("expected cultnet.document_delete.v0")); - }; - let binding = self.require_binding(T::TYPE)?; - if schema_id != &binding.schema_id { - return Err(anyhow!( - "schema id {:?} does not match registered Rust type {:?} schema {:?}", - schema_id, - T::TYPE, - binding.schema_id - )); - } - cache.delete::(record_key) - } - - pub fn apply_raw_document_put_message( - &self, - cache: &mut CultCache, - message: &CultNetMessage, - ) -> Result - where - T: DatabaseEntry + Serialize + DeserializeOwned, - { - let CultNetMessage::DocumentPutRaw { document, .. } = message else { - return Err(anyhow!("expected cultnet.document_put_raw.v0")); - }; - let binding = self.require_binding(T::TYPE)?; - if document.schema_id != binding.schema_id { - return Err(anyhow!( - "schema id {:?} does not match registered Rust type {:?} schema {:?}", - document.schema_id, - T::TYPE, - binding.schema_id - )); - } - cache.put_envelope::(CultCacheEnvelope { - key: document.record_key.clone(), - r#type: binding.document_type.clone(), - payload: document.payload.clone(), - stored_at: document.stored_at.clone(), - schema_id: Some(binding.schema_id.clone()), - }) - } - - pub fn apply_snapshot_response( - &self, - cache: &mut CultCache, - response: &CultNetMessage, - ) -> Result> - where - T: DatabaseEntry + Serialize + DeserializeOwned, - { - let CultNetMessage::SnapshotResponse { documents, .. } = response else { - return Err(anyhow!("expected cultnet.snapshot_response.v0")); - }; - let mut applied = Vec::new(); - let binding = self.require_binding(T::TYPE)?; - for document in documents { - if document.schema_id != binding.schema_id { - continue; - } - applied.push(self.apply_document_put_message::( - cache, - &CultNetMessage::DocumentPut { - message_id: "snapshot-apply".to_string(), - document: document.clone(), - }, - )?); - } - Ok(applied) - } - - pub fn apply_raw_snapshot_response( - &self, - cache: &mut CultCache, - response: &CultNetMessage, - ) -> Result> - where - T: DatabaseEntry + Serialize + DeserializeOwned, - { - let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { - return Err(anyhow!("expected cultnet.snapshot_response_raw.v0")); - }; - let mut applied = Vec::new(); - let binding = self.require_binding(T::TYPE)?; - for document in documents { - if document.schema_id != binding.schema_id { - continue; - } - applied.push(self.apply_raw_document_put_message::( - cache, - &CultNetMessage::DocumentPutRaw { - message_id: "snapshot-apply-raw".to_string(), - document: document.clone(), - }, - )?); - } - Ok(applied) - } - - fn document_record_from_envelope( - &self, - envelope: &CultCacheEnvelope, - ) -> Result> { - let binding = self.require_binding(&envelope.r#type)?; - let payload: Value = rmp_serde::from_slice(&envelope.payload).with_context(|| { - format!( - "failed to decode CultCache envelope {:?} at {:?} as generic CultNet payload", - envelope.r#type, envelope.key - ) - })?; - Ok(CultNetDocumentRecord { - schema_id: binding.schema_id.clone(), - record_key: envelope.key.clone(), - stored_at: envelope.stored_at.clone(), - payload, - source_runtime_id: None, - source_agent_id: None, - source_role: None, - tags: None, - }) - } - - /// Project a stored CultCache envelope as a raw CultNet record without - /// decoding or re-encoding its payload bytes. - pub fn raw_document_record_from_envelope( - &self, - envelope: &CultCacheEnvelope, - ) -> Result { - let binding = self.require_binding(&envelope.r#type)?; - Ok(CultNetRawDocumentRecord { - schema_id: binding.schema_id.clone(), - record_key: envelope.key.clone(), - stored_at: envelope.stored_at.clone(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: envelope.payload.clone(), - source_runtime_id: None, - source_agent_id: None, - source_role: None, - tags: None, - }) - } - - fn require_binding(&self, document_type: &str) -> Result<&CultNetDocumentBinding> { - self.binding(document_type).ok_or_else(|| { - anyhow!("No CultNet document binding is registered for {document_type:?}") - }) - } -} - -fn round_trip_typed(value: &T) -> Result -where - T: Serialize + DeserializeOwned, -{ - Ok(rmp_serde::from_slice(&rmp_serde::to_vec(value)?)?) -} - -fn now_utc_second() -> String { - chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) -} diff --git a/vendor/cultnet-rs/src/rudp.rs b/vendor/cultnet-rs/src/rudp.rs deleted file mode 100644 index 87d8da2..0000000 --- a/vendor/cultnet-rs/src/rudp.rs +++ /dev/null @@ -1,1444 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; -use std::collections::BTreeMap; -use std::collections::BTreeSet; -use std::collections::VecDeque; -use std::io::ErrorKind; -use std::net::SocketAddr; -use std::net::UdpSocket; -use std::thread; -use std::time::Duration; -use std::time::SystemTime; -use std::time::UNIX_EPOCH; - -use crate::CultNetTransportChannel; -use crate::CultNetTransportDelivery; -use crate::CultNetTransportDescriptor; -use crate::CultNetTransportFrame; -use crate::CultNetTransportOrdering; -use crate::CultNetTransportProfile; -use crate::CultNetTransportProtocol; -use crate::CultNetTransportStats; - -const RUDP_MAGIC: [u8; 4] = [0x43, 0x4e, 0x52, 0x30]; -const RUDP_VERSION: u8 = 0; -const RUDP_FIXED_HEADER_BYTES: usize = 36; -const DEFAULT_MEDIA_RELIABLE_EXPIRE_AFTER_MS: u64 = 75; - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultNetRudpPacketType { - Connect, - Accept, - Data, - Ack, - Ping, - Pong, - Disconnect, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetRudpPacket { - pub packet_type: CultNetRudpPacketType, - pub connection_id: u32, - pub sequence: u32, - pub ack: u32, - pub ack_mask: u32, - pub channel_id: String, - pub reliable: bool, - pub ordered: bool, - pub sequenced: bool, - pub fragment_id: u16, - pub fragment_index: u16, - pub fragment_count: u16, - pub payload: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetRudpDeliveredFrame { - pub channel_id: String, - pub payload: Vec, - pub sequence: u32, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetRudpReceiveResult { - pub delivered: Vec, - pub reply: Option, - pub pong: bool, - pub pong_payload: Vec, - pub disconnected: bool, - pub disconnect_reason: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetRudpSessionOptions { - pub connection_id: u32, - pub initial_sequence: u32, - pub resend_delay_ms: u64, - pub max_pending_reliable_packets: Option, -} - -impl Default for CultNetRudpSessionOptions { - fn default() -> Self { - Self { - connection_id: 0, - initial_sequence: 1, - resend_delay_ms: 250, - max_pending_reliable_packets: None, - } - } -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetRudpSendOptions { - pub reliable: bool, - pub ordered: bool, - pub sequenced: bool, - pub now_ms: u64, - pub reliable_expire_after_ms: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct PendingReliablePacket { - packet: CultNetRudpPacket, - last_sent_at_ms: u64, - expires_at_ms: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct PendingOrderedFrame { - frame: CultNetRudpDeliveredFrame, - next_sequence: u32, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct FragmentBuffer { - channel_id: String, - ordered: bool, - fragment_count: u16, - payloads: BTreeMap>, - sequences: BTreeMap, -} - -pub struct CultNetRudpSession { - connection_id: u32, - resend_delay_ms: u64, - max_pending_reliable_packets: Option, - next_sequence: u32, - next_fragment_id: u16, - connected: bool, - last_received_at_ms: Option, - highest_received_sequence: Option, - received_sequences: BTreeSet, - pending_reliable: BTreeMap, - ordered_next_sequence_by_channel: BTreeMap, - ordered_buffers: BTreeMap>, - fragment_buffers: BTreeMap<(String, u16), FragmentBuffer>, - highest_delivered_sequenced_by_channel: BTreeMap, - reliable_packets_expired: u64, -} - -impl CultNetRudpSession { - pub fn new(options: CultNetRudpSessionOptions) -> Self { - Self { - connection_id: options.connection_id, - resend_delay_ms: options.resend_delay_ms, - max_pending_reliable_packets: options.max_pending_reliable_packets, - next_sequence: options.initial_sequence, - next_fragment_id: 1, - connected: false, - last_received_at_ms: None, - highest_received_sequence: None, - received_sequences: BTreeSet::new(), - pending_reliable: BTreeMap::new(), - ordered_next_sequence_by_channel: BTreeMap::new(), - ordered_buffers: BTreeMap::new(), - fragment_buffers: BTreeMap::new(), - highest_delivered_sequenced_by_channel: BTreeMap::new(), - reliable_packets_expired: 0, - } - } - - pub fn connection_id(&self) -> u32 { - self.connection_id - } - - pub fn resend_delay_ms(&self) -> u64 { - self.resend_delay_ms - } - - pub fn connected(&self) -> bool { - self.connected - } - - pub fn reset_peer_state(&mut self) { - self.connected = false; - self.last_received_at_ms = None; - self.highest_received_sequence = None; - self.received_sequences.clear(); - self.pending_reliable.clear(); - self.ordered_next_sequence_by_channel.clear(); - self.ordered_buffers.clear(); - self.fragment_buffers.clear(); - self.highest_delivered_sequenced_by_channel.clear(); - } - - pub fn assume_connected(&mut self, now_ms: u64) { - self.connected = true; - self.last_received_at_ms = Some(now_ms); - } - - pub fn pending_reliable_sequences(&self) -> Vec { - self.pending_reliable.keys().copied().collect() - } - - pub fn last_received_at_ms(&self) -> Option { - self.last_received_at_ms - } - - pub fn reliable_packets_expired(&self) -> u64 { - self.reliable_packets_expired - } - - pub fn create_connect(&mut self, now_ms: u64, payload: Vec) -> Result { - self.ensure_reliable_capacity(1)?; - let packet = self.create_packet( - CultNetRudpPacketType::Connect, - "control", - payload, - true, - true, - false, - ); - self.track_reliable( - &CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms, - reliable_expire_after_ms: None, - }, - packet.clone(), - ); - Ok(packet) - } - - pub fn accept_connect( - &mut self, - packet: &CultNetRudpPacket, - now_ms: u64, - payload: Vec, - ) -> Result { - self.require_connection(packet)?; - if packet.packet_type != CultNetRudpPacketType::Connect { - return Err(anyhow!( - "Expected RUDP connect packet, got {:?}", - packet.packet_type - )); - } - - let was_connected = self.connected; - if !was_connected { - self.ensure_reliable_capacity(1)?; - } - self.remember_received(packet.sequence); - self.last_received_at_ms = Some(now_ms); - self.connected = true; - let response = self.create_packet( - CultNetRudpPacketType::Accept, - "control", - payload, - true, - true, - false, - ); - if !was_connected { - self.track_reliable( - &CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms, - reliable_expire_after_ms: None, - }, - response.clone(), - ); - } - Ok(response) - } - - pub fn send( - &mut self, - channel_id: &str, - payload: Vec, - options: CultNetRudpSendOptions, - ) -> Result { - self.send_many(channel_id, payload, options, None)? - .into_iter() - .next() - .ok_or_else(|| anyhow!("RUDP send produced no packets")) - } - - pub fn send_many( - &mut self, - channel_id: &str, - payload: Vec, - options: CultNetRudpSendOptions, - max_fragment_bytes: Option, - ) -> Result> { - if !self.connected { - return Err(anyhow!( - "Cannot send RUDP data before the session is connected" - )); - } - self.purge_expired_reliable(options.now_ms); - - if let Some(max_fragment_bytes) = max_fragment_bytes { - if max_fragment_bytes == 0 { - return Err(anyhow!("RUDP max_fragment_bytes must be greater than zero")); - } - if payload.len() > max_fragment_bytes { - let fragment_count = payload.len().div_ceil(max_fragment_bytes); - if fragment_count > u16::MAX as usize { - return Err(anyhow!("RUDP payload requires more than 65535 fragments")); - } - self.ensure_reliable_capacity(if options.reliable { fragment_count } else { 0 })?; - let fragment_id = self.allocate_fragment_id(); - let mut packets = Vec::new(); - for index in 0..fragment_count { - let start = index * max_fragment_bytes; - let end = (start + max_fragment_bytes).min(payload.len()); - let packet = self.create_packet_with_fragments( - CultNetRudpPacketType::Data, - channel_id, - payload[start..end].to_vec(), - options.reliable, - options.ordered, - options.sequenced, - fragment_id, - index as u16, - fragment_count as u16, - ); - if packet.reliable { - self.track_reliable(&options, packet.clone()); - } - packets.push(packet); - } - return Ok(packets); - } - } - - self.ensure_reliable_capacity(if options.reliable { 1 } else { 0 })?; - let packet = self.create_packet( - CultNetRudpPacketType::Data, - channel_id, - payload, - options.reliable, - options.ordered, - options.sequenced, - ); - if packet.reliable { - self.track_reliable(&options, packet.clone()); - } - Ok(vec![packet]) - } - - pub fn receive( - &mut self, - packet: &CultNetRudpPacket, - now_ms: u64, - ) -> Result { - self.require_connection(packet)?; - self.apply_acknowledgements(packet); - self.last_received_at_ms = Some(now_ms); - let expected_sequence_if_uninitialized = self - .highest_received_sequence - .map(|sequence| sequence + 1) - .unwrap_or(packet.sequence); - - if packet.packet_type == CultNetRudpPacketType::Accept { - self.remember_received(packet.sequence); - self.connected = true; - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - - if packet.packet_type == CultNetRudpPacketType::Ping { - self.remember_received(packet.sequence); - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: Some(self.create_packet( - CultNetRudpPacketType::Pong, - "control", - packet.payload.clone(), - false, - false, - false, - )), - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - - if packet.packet_type == CultNetRudpPacketType::Ack - || packet.packet_type == CultNetRudpPacketType::Pong - { - self.remember_received(packet.sequence); - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: packet.packet_type == CultNetRudpPacketType::Pong, - pong_payload: if packet.packet_type == CultNetRudpPacketType::Pong { - packet.payload.clone() - } else { - Vec::new() - }, - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - - if packet.packet_type == CultNetRudpPacketType::Disconnect { - self.remember_received(packet.sequence); - self.connected = false; - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: true, - disconnect_reason: packet.payload.clone(), - }); - } - - if packet.packet_type != CultNetRudpPacketType::Data { - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - - let duplicate = self.received_sequences.contains(&packet.sequence); - self.remember_received(packet.sequence); - if duplicate { - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - - let Some((frame, ordered, next_sequence)) = self.reassemble(packet)? else { - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - }; - if packet.sequenced && !ordered { - let highest = self - .highest_delivered_sequenced_by_channel - .entry(frame.channel_id.clone()) - .or_insert(frame.sequence); - if frame.sequence < *highest { - return Ok(CultNetRudpReceiveResult { - delivered: Vec::new(), - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }); - } - *highest = frame.sequence; - } - let delivered = if ordered { - self.deliver_ordered(frame, next_sequence, expected_sequence_if_uninitialized) - } else { - vec![frame] - }; - Ok(CultNetRudpReceiveResult { - delivered, - reply: None, - pong: false, - pong_payload: Vec::new(), - disconnected: false, - disconnect_reason: Vec::new(), - }) - } - - pub fn create_ack(&mut self) -> CultNetRudpPacket { - self.create_packet( - CultNetRudpPacketType::Ack, - "control", - Vec::new(), - false, - false, - false, - ) - } - - pub fn create_ping(&mut self, payload: Vec) -> CultNetRudpPacket { - self.create_packet( - CultNetRudpPacketType::Ping, - "control", - payload, - false, - false, - false, - ) - } - - pub fn create_disconnect(&mut self, reason: Vec) -> CultNetRudpPacket { - self.connected = false; - self.create_packet( - CultNetRudpPacketType::Disconnect, - "control", - reason, - false, - false, - false, - ) - } - - pub fn check_timeout(&mut self, now_ms: u64, timeout_ms: u64) -> bool { - if !self.connected { - return false; - } - let Some(last_received_at_ms) = self.last_received_at_ms else { - return false; - }; - if now_ms.saturating_sub(last_received_at_ms) <= timeout_ms { - return false; - } - self.connected = false; - true - } - - pub fn due_resends(&mut self, now_ms: u64) -> Vec { - self.purge_expired_reliable(now_ms); - let mut due = Vec::new(); - for pending in self.pending_reliable.values_mut() { - if now_ms.saturating_sub(pending.last_sent_at_ms) >= self.resend_delay_ms { - pending.last_sent_at_ms = now_ms; - due.push(pending.packet.clone()); - } - } - due.sort_by_key(|packet| packet.sequence); - due - } - - fn create_packet( - &mut self, - packet_type: CultNetRudpPacketType, - channel_id: &str, - payload: Vec, - reliable: bool, - ordered: bool, - sequenced: bool, - ) -> CultNetRudpPacket { - self.create_packet_with_fragments( - packet_type, - channel_id, - payload, - reliable, - ordered, - sequenced, - 0, - 0, - 0, - ) - } - - #[allow(clippy::too_many_arguments)] - fn create_packet_with_fragments( - &mut self, - packet_type: CultNetRudpPacketType, - channel_id: &str, - payload: Vec, - reliable: bool, - ordered: bool, - sequenced: bool, - fragment_id: u16, - fragment_index: u16, - fragment_count: u16, - ) -> CultNetRudpPacket { - let sequence = self.next_sequence; - self.next_sequence = self - .next_sequence - .checked_add(1) - .expect("sequence overflow"); - let (ack, ack_mask) = self.ack_state(); - CultNetRudpPacket { - packet_type, - connection_id: self.connection_id, - sequence, - ack, - ack_mask, - channel_id: channel_id.to_string(), - reliable, - ordered, - sequenced, - fragment_id, - fragment_index, - fragment_count, - payload, - } - } - - fn track_reliable(&mut self, options: &CultNetRudpSendOptions, packet: CultNetRudpPacket) { - let expires_at_ms = options - .reliable_expire_after_ms - .map(|ttl| options.now_ms.saturating_add(ttl)); - self.pending_reliable.insert( - packet.sequence, - PendingReliablePacket { - packet, - last_sent_at_ms: options.now_ms, - expires_at_ms, - }, - ); - } - - fn ensure_reliable_capacity(&self, packet_count: usize) -> Result<()> { - if packet_count == 0 { - return Ok(()); - } - if let Some(limit) = self.max_pending_reliable_packets { - if self.pending_reliable.len() + packet_count > limit { - return Err(anyhow!("RUDP reliable send queue is full")); - } - } - Ok(()) - } - - fn purge_expired_reliable(&mut self, now_ms: u64) { - let before = self.pending_reliable.len(); - self.pending_reliable.retain(|_, pending| { - pending - .expires_at_ms - .map_or(true, |expires_at_ms| now_ms <= expires_at_ms) - }); - self.reliable_packets_expired += (before - self.pending_reliable.len()) as u64; - } - - fn apply_acknowledgements(&mut self, packet: &CultNetRudpPacket) { - self.pending_reliable.remove(&packet.ack); - for bit in 0..32 { - if (packet.ack_mask & (1_u32 << bit)) != 0 && packet.ack > bit { - self.pending_reliable.remove(&(packet.ack - bit - 1)); - } - } - } - - fn remember_received(&mut self, sequence: u32) { - self.received_sequences.insert(sequence); - if self - .highest_received_sequence - .is_none_or(|highest| sequence > highest) - { - self.highest_received_sequence = Some(sequence); - } - } - - fn ack_state(&self) -> (u32, u32) { - let ack = self.highest_received_sequence.unwrap_or(0); - let mut ack_mask = 0_u32; - for bit in 0..32 { - if ack > bit && self.received_sequences.contains(&(ack - bit - 1)) { - ack_mask |= 1_u32 << bit; - } - } - (ack, ack_mask) - } - - fn reassemble( - &mut self, - packet: &CultNetRudpPacket, - ) -> Result> { - if packet.fragment_count == 0 { - return Ok(Some(( - CultNetRudpDeliveredFrame { - channel_id: packet.channel_id.clone(), - payload: packet.payload.clone(), - sequence: packet.sequence, - }, - packet.ordered, - packet.sequence + 1, - ))); - } - if packet.fragment_id == 0 { - return Err(anyhow!( - "RUDP fragmented packet must have a non-zero fragment id" - )); - } - if packet.fragment_index >= packet.fragment_count { - return Err(anyhow!( - "RUDP fragment index must be lower than fragment count" - )); - } - - let key = (packet.channel_id.clone(), packet.fragment_id); - let buffer = self - .fragment_buffers - .entry(key.clone()) - .or_insert_with(|| FragmentBuffer { - channel_id: packet.channel_id.clone(), - ordered: packet.ordered, - fragment_count: packet.fragment_count, - payloads: BTreeMap::new(), - sequences: BTreeMap::new(), - }); - if buffer.fragment_count != packet.fragment_count || buffer.ordered != packet.ordered { - return Err(anyhow!( - "RUDP fragment metadata changed within a fragment set" - )); - } - buffer - .payloads - .insert(packet.fragment_index, packet.payload.clone()); - buffer - .sequences - .insert(packet.fragment_index, packet.sequence); - if buffer.payloads.len() < packet.fragment_count as usize { - return Ok(None); - } - - let mut payload = Vec::new(); - let mut sequences = Vec::new(); - for index in 0..packet.fragment_count { - let Some(chunk) = buffer.payloads.get(&index) else { - return Ok(None); - }; - let Some(sequence) = buffer.sequences.get(&index) else { - return Ok(None); - }; - payload.extend_from_slice(chunk); - sequences.push(*sequence); - } - let channel_id = buffer.channel_id.clone(); - let ordered = buffer.ordered; - self.fragment_buffers.remove(&key); - Ok(Some(( - CultNetRudpDeliveredFrame { - channel_id, - payload, - sequence: *sequences.iter().min().unwrap(), - }, - ordered, - sequences.iter().max().unwrap() + 1, - ))) - } - - fn deliver_ordered( - &mut self, - frame: CultNetRudpDeliveredFrame, - next_sequence_after_frame: u32, - expected_sequence_if_uninitialized: u32, - ) -> Vec { - let channel_id = frame.channel_id.clone(); - let next = if let Some(next) = self - .ordered_next_sequence_by_channel - .get(&channel_id) - .copied() - { - next - } else { - self.ordered_next_sequence_by_channel.insert( - channel_id.clone(), - expected_sequence_if_uninitialized.min(frame.sequence), - ); - expected_sequence_if_uninitialized.min(frame.sequence) - }; - - if frame.sequence < next { - return Vec::new(); - } - - if frame.sequence > next { - self.ordered_buffers.entry(channel_id).or_default().insert( - frame.sequence, - PendingOrderedFrame { - frame, - next_sequence: next_sequence_after_frame, - }, - ); - return Vec::new(); - } - - self.ordered_next_sequence_by_channel - .insert(channel_id.clone(), next_sequence_after_frame); - let mut delivered = vec![frame]; - delivered.extend(self.drain_ordered(&channel_id)); - delivered - } - - fn drain_ordered(&mut self, channel_id: &str) -> Vec { - let mut delivered = Vec::new(); - loop { - let Some(next) = self - .ordered_next_sequence_by_channel - .get(channel_id) - .copied() - else { - break; - }; - let Some(buffer) = self.ordered_buffers.get_mut(channel_id) else { - break; - }; - let Some(pending) = buffer.remove(&next) else { - break; - }; - delivered.push(pending.frame); - self.ordered_next_sequence_by_channel - .insert(channel_id.to_string(), pending.next_sequence); - } - delivered - } - - fn allocate_fragment_id(&mut self) -> u16 { - let fragment_id = self.next_fragment_id; - self.next_fragment_id = self.next_fragment_id.saturating_add(1); - if self.next_fragment_id == 0 { - self.next_fragment_id = 1; - } - fragment_id - } - - fn require_connection(&self, packet: &CultNetRudpPacket) -> Result<()> { - if packet.connection_id != self.connection_id { - return Err(anyhow!( - "RUDP packet connection id {} does not match {}", - packet.connection_id, - self.connection_id - )); - } - Ok(()) - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultNetRudpSocketMode { - Client, - Server, -} - -pub struct CultNetRudpSocketTransportOptions { - pub runtime_id: String, - pub socket: UdpSocket, - pub mode: CultNetRudpSocketMode, - pub remote_addr: Option, - pub connection_id: u32, - pub initial_sequence: u32, - pub resend_delay_ms: u64, - pub transport_id: Option, - pub max_payload_bytes: Option, - pub max_fragment_bytes: Option, - pub max_pending_reliable_packets: Option, - pub media_reliable_expire_after_ms: Option, -} - -impl CultNetRudpSocketTransportOptions { - pub fn client( - runtime_id: impl Into, - socket: UdpSocket, - remote_addr: SocketAddr, - connection_id: u32, - ) -> Self { - Self { - runtime_id: runtime_id.into(), - socket, - mode: CultNetRudpSocketMode::Client, - remote_addr: Some(remote_addr), - connection_id, - initial_sequence: 1, - resend_delay_ms: 250, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(DEFAULT_MEDIA_RELIABLE_EXPIRE_AFTER_MS), - } - } - - pub fn server(runtime_id: impl Into, socket: UdpSocket, connection_id: u32) -> Self { - Self { - runtime_id: runtime_id.into(), - socket, - mode: CultNetRudpSocketMode::Server, - remote_addr: None, - connection_id, - initial_sequence: 1, - resend_delay_ms: 250, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(DEFAULT_MEDIA_RELIABLE_EXPIRE_AFTER_MS), - } - } -} - -pub struct CultNetRudpSocketTransportConnection { - socket: UdpSocket, - session: CultNetRudpSession, - mode: CultNetRudpSocketMode, - remote_addr: Option, - pub profile: CultNetTransportProfile, - transport_id: Option, - stats: CultNetTransportStats, - delivered_frames: VecDeque, - max_fragment_bytes: Option, - disconnect_reason: Option>, - pong_payloads: VecDeque>, -} - -impl CultNetRudpSocketTransportConnection { - pub fn new(options: CultNetRudpSocketTransportOptions) -> Result { - let local_addr = options.socket.local_addr()?; - let transport_id = options.transport_id.clone(); - let profile = create_rudp_transport_profile( - options.runtime_id, - RudpTransportProfileOptions { - transport_id: options.transport_id, - host: Some(local_addr.ip().to_string()), - port: Some(local_addr.port()), - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: options.max_pending_reliable_packets, - media_reliable_expire_after_ms: options.media_reliable_expire_after_ms, - }, - ); - let max_pending_reliable_packets = options - .max_pending_reliable_packets - .map(|value| value as usize); - Ok(Self { - socket: options.socket, - session: CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: options.connection_id, - initial_sequence: options.initial_sequence, - resend_delay_ms: options.resend_delay_ms, - max_pending_reliable_packets, - }), - mode: options.mode, - remote_addr: options.remote_addr, - profile, - transport_id, - stats: CultNetTransportStats::default(), - delivered_frames: VecDeque::new(), - max_fragment_bytes: options.max_fragment_bytes.map(|value| value as usize), - disconnect_reason: None, - pong_payloads: VecDeque::new(), - }) - } - - pub fn connected(&self) -> bool { - self.session.connected() - } - - pub fn assume_connected(&mut self) { - self.session.assume_connected(now_ms()); - } - - pub fn stats(&self) -> CultNetTransportStats { - let mut stats = self.stats.clone(); - stats.reliable_packets_expired = self.session.reliable_packets_expired(); - stats - } - - pub fn disconnect_reason(&self) -> Option<&[u8]> { - self.disconnect_reason.as_deref() - } - - pub fn pop_pong_payload(&mut self) -> Option> { - self.pong_payloads.pop_front() - } - - pub fn connect(&mut self, payload: Vec) -> Result<()> { - if self.mode != CultNetRudpSocketMode::Client { - return Err(anyhow!( - "Only a client RUDP socket transport can initiate connect" - )); - } - let packet = self.session.create_connect(now_ms(), payload)?; - self.send_packet(&packet) - } - - pub fn send(&mut self, channel_id: &str, payload: Vec) -> Result<()> { - let options = self.channel_send_options(channel_id, now_ms()); - let packets = - self.session - .send_many(channel_id, payload, options, self.max_fragment_bytes)?; - let fragmented = packets.len() > 1; - for (index, packet) in packets.iter().enumerate() { - self.send_packet(packet)?; - if fragmented && (index + 1) % 16 == 0 { - thread::sleep(Duration::from_millis(1)); - } - } - self.stats.frames_sent += 1; - Ok(()) - } - - pub fn disconnect(&mut self, reason: Vec) -> Result<()> { - let packet = self.session.create_disconnect(reason); - self.send_packet(&packet) - } - - pub fn ping(&mut self, payload: Vec) -> Result<()> { - let packet = self.session.create_ping(payload); - self.send_packet(&packet) - } - - pub fn check_timeout(&mut self, timeout_ms: u64) -> bool { - self.session.check_timeout(now_ms(), timeout_ms) - } - - fn channel_send_options(&self, channel_id: &str, now_ms: u64) -> CultNetRudpSendOptions { - match channel_id { - "schema" => CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms, - reliable_expire_after_ms: None, - }, - "latest" => CultNetRudpSendOptions { - reliable: false, - ordered: false, - sequenced: true, - now_ms, - reliable_expire_after_ms: None, - }, - "media" => CultNetRudpSendOptions { - reliable: false, - ordered: false, - sequenced: false, - now_ms, - reliable_expire_after_ms: None, - }, - _ => CultNetRudpSendOptions { - reliable: false, - ordered: false, - sequenced: false, - now_ms, - reliable_expire_after_ms: None, - }, - } - } - - pub fn receive_once(&mut self) -> Result> { - if let Some(frame) = self.delivered_frames.pop_front() { - self.session.assume_connected(now_ms()); - return Ok(Some(frame)); - } - - let mut wire = vec![0_u8; 65_535]; - let (received, remote_addr) = match self.socket.recv_from(&mut wire) { - Ok(value) => value, - Err(error) - if error.kind() == ErrorKind::WouldBlock || error.kind() == ErrorKind::TimedOut => - { - return Ok(None); - } - Err(error) => return Err(error.into()), - }; - wire.truncate(received); - self.stats.bytes_received += received as u64; - - let packet = match decode_rudp_packet(&wire) { - Ok(packet) => packet, - Err(error) => { - if std::env::var_os("CULTNET_RUDP_TRACE").is_some() - && self.transport_id.as_deref() == Some("muninn-provider-command-rudp") - { - eprintln!("RUDP muninn-provider-command-rudp rejected {} bytes from {remote_addr}: {error:#}", wire.len()); - } - return Ok(None); - } - }; - let trace_transport = std::env::var_os("CULTNET_RUDP_TRACE").is_some() - && self - .transport_id - .as_deref() - .is_some_and(|id| { - id == "sleipnir-hid-rudp" - || id == "muninn-hid-controller-rudp" - || id == "muninn-provider-command-rudp" - }); - if trace_transport { - eprintln!( - "RUDP {} recv {:?} channel={} seq={} ack={} reliable={} ordered={} sequenced={} bytes={} from {}", - self.transport_id.as_deref().unwrap_or("unknown"), - packet.packet_type, - packet.channel_id, - packet.sequence, - packet.ack, - packet.reliable, - packet.ordered, - packet.sequenced, - packet.payload.len(), - remote_addr - ); - } - - if let Some(expected) = self.remote_addr { - if expected != remote_addr { - if self.mode == CultNetRudpSocketMode::Server - && packet.packet_type == CultNetRudpPacketType::Connect - { - self.remote_addr = Some(remote_addr); - } else { - return Ok(None); - } - } - } else { - if self.mode == CultNetRudpSocketMode::Server - && packet.packet_type != CultNetRudpPacketType::Connect - { - return Ok(None); - } - self.remote_addr = Some(remote_addr); - } - if self.mode == CultNetRudpSocketMode::Server - && packet.packet_type == CultNetRudpPacketType::Connect - { - self.session.reset_peer_state(); - let accept = self.session.accept_connect(&packet, now_ms(), Vec::new())?; - self.send_packet(&accept)?; - return Ok(None); - } - - let result = self.session.receive(&packet, now_ms())?; - if let Some(reply) = result.reply { - self.send_packet(&reply)?; - } - if result.pong { - self.pong_payloads.push_back(result.pong_payload); - } - if result.disconnected { - self.disconnect_reason = Some(result.disconnect_reason); - return Ok(None); - } - - for frame in result.delivered { - self.delivered_frames.push_back(CultNetTransportFrame { - channel_id: frame.channel_id, - payload: frame.payload, - }); - self.stats.frames_received += 1; - } - let frame = self.delivered_frames.pop_front(); - if packet.packet_type == CultNetRudpPacketType::Accept || frame.is_some() { - let ack = self.session.create_ack(); - self.send_packet(&ack)?; - } - Ok(frame) - } - - pub fn poll_resends(&mut self) -> Result<()> { - for (index, packet) in self.session.due_resends(now_ms()).into_iter().enumerate() { - self.send_packet(&packet)?; - if (index + 1) % 16 == 0 { - thread::sleep(Duration::from_millis(1)); - } - } - Ok(()) - } - - fn send_packet(&mut self, packet: &CultNetRudpPacket) -> Result<()> { - let Some(remote_addr) = self.remote_addr else { - return Err(anyhow!( - "RUDP socket transport does not have a remote endpoint" - )); - }; - let wire = encode_rudp_packet(packet)?; - let trace_transport = std::env::var_os("CULTNET_RUDP_TRACE").is_some() - && self - .transport_id - .as_deref() - .is_some_and(|id| id == "sleipnir-hid-rudp" || id == "muninn-hid-controller-rudp"); - if trace_transport { - eprintln!( - "RUDP {} send {:?} channel={} seq={} ack={} reliable={} ordered={} sequenced={} payload={} wire={} to {}", - self.transport_id.as_deref().unwrap_or("unknown"), - packet.packet_type, - packet.channel_id, - packet.sequence, - packet.ack, - packet.reliable, - packet.ordered, - packet.sequenced, - packet.payload.len(), - wire.len(), - remote_addr - ); - } - let sent = self.socket.send_to(&wire, remote_addr)?; - self.stats.bytes_sent += sent as u64; - Ok(()) - } -} - -#[derive(Clone, Debug, Default)] -pub struct RudpTransportProfileOptions { - pub transport_id: Option, - pub host: Option, - pub port: Option, - pub max_payload_bytes: Option, - pub max_fragment_bytes: Option, - pub max_pending_reliable_packets: Option, - pub media_reliable_expire_after_ms: Option, -} - -pub fn create_rudp_transport_profile( - runtime_id: impl Into, - options: RudpTransportProfileOptions, -) -> CultNetTransportProfile { - CultNetTransportProfile { - schema_version: "cultnet.transport_profile.v0".to_string(), - runtime_id: runtime_id.into(), - transports: vec![CultNetTransportDescriptor { - transport_id: options.transport_id.unwrap_or_else(|| "rudp".to_string()), - protocol: CultNetTransportProtocol::Rudp, - host: options.host, - port: options.port, - path: None, - discovery_group: None, - wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]), - channels: vec![ - CultNetTransportChannel { - channel_id: "schema".to_string(), - delivery: CultNetTransportDelivery::Reliable, - ordering: CultNetTransportOrdering::Ordered, - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: options.max_pending_reliable_packets, - reliable_expire_after_ms: None, - }, - CultNetTransportChannel { - channel_id: "latest".to_string(), - delivery: CultNetTransportDelivery::Unreliable, - ordering: CultNetTransportOrdering::Sequenced, - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: options.max_pending_reliable_packets, - reliable_expire_after_ms: None, - }, - CultNetTransportChannel { - channel_id: "realtime".to_string(), - delivery: CultNetTransportDelivery::Unreliable, - ordering: CultNetTransportOrdering::Unordered, - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: options.max_pending_reliable_packets, - reliable_expire_after_ms: None, - }, - CultNetTransportChannel { - channel_id: "media".to_string(), - delivery: CultNetTransportDelivery::Unreliable, - ordering: CultNetTransportOrdering::Unordered, - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: options.max_pending_reliable_packets, - reliable_expire_after_ms: None, - }, - ], - }], - } -} - -pub fn encode_rudp_packet(packet: &CultNetRudpPacket) -> Result> { - let channel_id = packet.channel_id.as_bytes(); - if channel_id.len() > u8::MAX as usize { - return Err(anyhow!( - "CultNet RUDP channel id cannot exceed 255 UTF-8 bytes" - )); - } - - let header_bytes = RUDP_FIXED_HEADER_BYTES + channel_id.len(); - let mut wire = vec![0_u8; header_bytes + packet.payload.len()]; - wire[..4].copy_from_slice(&RUDP_MAGIC); - wire[4] = RUDP_VERSION; - wire[5] = packet_type_to_code(packet.packet_type); - wire[6] = encode_flags(packet); - wire[7] = header_bytes as u8; - wire[8..12].copy_from_slice(&packet.connection_id.to_be_bytes()); - wire[12..16].copy_from_slice(&packet.sequence.to_be_bytes()); - wire[16..20].copy_from_slice(&packet.ack.to_be_bytes()); - wire[20..24].copy_from_slice(&packet.ack_mask.to_be_bytes()); - wire[24..26].copy_from_slice(&packet.fragment_id.to_be_bytes()); - wire[26..28].copy_from_slice(&packet.fragment_index.to_be_bytes()); - wire[28..30].copy_from_slice(&packet.fragment_count.to_be_bytes()); - wire[30..34].copy_from_slice(&(packet.payload.len() as u32).to_be_bytes()); - wire[34] = channel_id.len() as u8; - wire[35] = 0; - wire[RUDP_FIXED_HEADER_BYTES..header_bytes].copy_from_slice(channel_id); - wire[header_bytes..].copy_from_slice(&packet.payload); - Ok(wire) -} - -pub fn decode_rudp_packet(wire: &[u8]) -> Result { - if wire.len() < RUDP_FIXED_HEADER_BYTES { - return Err(anyhow!( - "CultNet RUDP packet is shorter than the fixed header" - )); - } - if wire[..4] != RUDP_MAGIC { - return Err(anyhow!("CultNet RUDP packet has the wrong magic")); - } - if wire[4] != RUDP_VERSION { - return Err(anyhow!( - "Unsupported CultNet RUDP packet version {}", - wire[4] - )); - } - - let packet_type = packet_type_from_code(wire[5])?; - let header_bytes = wire[7] as usize; - let channel_id_len = wire[34] as usize; - if header_bytes != RUDP_FIXED_HEADER_BYTES + channel_id_len { - return Err(anyhow!( - "CultNet RUDP packet header length does not match the channel id length" - )); - } - let payload_len = u32::from_be_bytes(wire[30..34].try_into()?) as usize; - if wire.len() != header_bytes + payload_len { - return Err(anyhow!( - "CultNet RUDP packet payload length does not match the packet size" - )); - } - - let flags = wire[6]; - Ok(CultNetRudpPacket { - packet_type, - reliable: (flags & 0b0000_0001) != 0, - ordered: (flags & 0b0000_0010) != 0, - sequenced: (flags & 0b0000_0100) != 0, - connection_id: u32::from_be_bytes(wire[8..12].try_into()?), - sequence: u32::from_be_bytes(wire[12..16].try_into()?), - ack: u32::from_be_bytes(wire[16..20].try_into()?), - ack_mask: u32::from_be_bytes(wire[20..24].try_into()?), - fragment_id: u16::from_be_bytes(wire[24..26].try_into()?), - fragment_index: u16::from_be_bytes(wire[26..28].try_into()?), - fragment_count: u16::from_be_bytes(wire[28..30].try_into()?), - channel_id: String::from_utf8(wire[RUDP_FIXED_HEADER_BYTES..header_bytes].to_vec())?, - payload: wire[header_bytes..].to_vec(), - }) -} - -fn encode_flags(packet: &CultNetRudpPacket) -> u8 { - (if packet.reliable { 0b0000_0001 } else { 0 }) - | (if packet.ordered { 0b0000_0010 } else { 0 }) - | (if packet.sequenced { 0b0000_0100 } else { 0 }) - | (if packet.fragment_count > 0 { - 0b0000_1000 - } else { - 0 - }) -} - -fn packet_type_to_code(packet_type: CultNetRudpPacketType) -> u8 { - match packet_type { - CultNetRudpPacketType::Connect => 1, - CultNetRudpPacketType::Accept => 2, - CultNetRudpPacketType::Data => 3, - CultNetRudpPacketType::Ack => 4, - CultNetRudpPacketType::Ping => 5, - CultNetRudpPacketType::Pong => 6, - CultNetRudpPacketType::Disconnect => 7, - } -} - -fn now_ms() -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|duration| duration.as_millis() as u64) - .unwrap_or(0) -} - -fn packet_type_from_code(code: u8) -> Result { - match code { - 1 => Ok(CultNetRudpPacketType::Connect), - 2 => Ok(CultNetRudpPacketType::Accept), - 3 => Ok(CultNetRudpPacketType::Data), - 4 => Ok(CultNetRudpPacketType::Ack), - 5 => Ok(CultNetRudpPacketType::Ping), - 6 => Ok(CultNetRudpPacketType::Pong), - 7 => Ok(CultNetRudpPacketType::Disconnect), - _ => Err(anyhow!("Unsupported CultNet RUDP packet type {code}")), - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn connected_session(initial_sequence: u32) -> CultNetRudpSession { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 0xCAFE_BABE, - initial_sequence, - resend_delay_ms: 5, - max_pending_reliable_packets: Some(64), - }); - session.assume_connected(1); - session - } - - #[test] - fn sequenced_unordered_channel_drops_older_late_frames() { - let mut sender = connected_session(1); - let mut receiver = connected_session(1); - let first = sender - .send( - "latest", - b"first".to_vec(), - CultNetRudpSendOptions { - reliable: false, - ordered: false, - sequenced: true, - now_ms: 1, - reliable_expire_after_ms: None, - }, - ) - .unwrap(); - let second = sender - .send( - "latest", - b"second".to_vec(), - CultNetRudpSendOptions { - reliable: false, - ordered: false, - sequenced: true, - now_ms: 2, - reliable_expire_after_ms: None, - }, - ) - .unwrap(); - - let delivered_second = receiver.receive(&second, 2).unwrap().delivered; - let delivered_first = receiver.receive(&first, 3).unwrap().delivered; - - assert_eq!(delivered_second.len(), 1); - assert_eq!(delivered_second[0].payload, b"second"); - assert!(delivered_first.is_empty()); - } -} diff --git a/vendor/cultnet-rs/src/schema_discovery.rs b/vendor/cultnet-rs/src/schema_discovery.rs deleted file mode 100644 index 2cc1420..0000000 --- a/vendor/cultnet-rs/src/schema_discovery.rs +++ /dev/null @@ -1,396 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; -use serde_json::Value; -use sha2::Digest; -use sha2::Sha256; -use std::collections::BTreeMap; -use std::collections::BTreeSet; - -use crate::CultNetMessage; -use crate::CultNetSchemaDescriptor; -use crate::CultNetSchemaKind; -use crate::CultNetWireContract; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetSchemaRegistration { - pub schema_id: String, - pub kind: CultNetSchemaKind, - pub wire_contracts: Vec, - pub schema_version: Option, - pub document_type: Option, - pub title: Option, - pub schema_json: Option, -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetSchemaCatalogOptions { - pub include_schema_json: bool, - pub schema_ids: Option>, - pub kinds: Option>, -} - -#[derive(Clone, Debug, Default)] -pub struct CultNetSchemaRegistry { - entries: BTreeMap, -} - -impl CultNetSchemaRegistry { - pub fn new() -> Self { - Self::default() - } - - pub fn register(&mut self, registration: CultNetSchemaRegistration) -> Result<()> { - if registration.schema_id.trim().is_empty() { - return Err(anyhow!("schema_id must be non-empty")); - } - if registration.wire_contracts.is_empty() { - return Err(anyhow!("wire_contracts must contain at least one entry")); - } - - let content_hash = if let Some(schema_json) = registration.schema_json.as_deref() { - canonical_schema_hash(schema_json)? - } else { - return Err(anyhow!( - "schema_json is required so discovery can advertise canonical hashes" - )); - }; - - self.entries.insert( - registration.schema_id.clone(), - CultNetSchemaDescriptor { - schema_id: registration.schema_id, - kind: registration.kind, - schema_version: registration.schema_version, - document_type: registration.document_type, - title: registration.title, - wire_contracts: registration.wire_contracts, - content_hash, - schema_json: registration.schema_json, - }, - ); - - Ok(()) - } - - pub fn get( - &self, - schema_id: &str, - include_schema_json: bool, - ) -> Option { - self.entries - .get(schema_id) - .map(|entry| sanitize_descriptor(entry, include_schema_json)) - } - - pub fn list(&self, options: &CultNetSchemaCatalogOptions) -> Vec { - let requested_ids = options - .schema_ids - .as_ref() - .map(|values| values.iter().cloned().collect::>()); - let requested_kinds = options - .kinds - .as_ref() - .map(|values| values.iter().copied().collect::>()); - - self.entries - .values() - .filter(|entry| { - if let Some(requested_ids) = &requested_ids - && !requested_ids.contains(&entry.schema_id) - { - return false; - } - - if let Some(requested_kinds) = &requested_kinds - && !requested_kinds.contains(&entry.kind) - { - return false; - } - - true - }) - .map(|entry| sanitize_descriptor(entry, options.include_schema_json)) - .collect() - } - - pub fn create_catalog_response(&self, request: &CultNetMessage) -> Result { - let CultNetMessage::SchemaCatalogRequest { - message_id, - include_schema_json, - schema_ids, - kinds, - } = request - else { - return Err(anyhow!( - "expected cultnet.schema_catalog_request.v0 for schema discovery" - )); - }; - - Ok(CultNetMessage::SchemaCatalogResponse { - message_id: message_id.clone(), - schemas: self.list(&CultNetSchemaCatalogOptions { - include_schema_json: include_schema_json.unwrap_or(false), - schema_ids: schema_ids.clone(), - kinds: kinds.clone(), - }), - }) - } -} - -fn sanitize_descriptor( - descriptor: &CultNetSchemaDescriptor, - include_schema_json: bool, -) -> CultNetSchemaDescriptor { - let mut clone = descriptor.clone(); - if !include_schema_json { - clone.schema_json = None; - } - clone -} - -fn canonical_schema_hash(schema_json: &str) -> Result { - let value: Value = serde_json::from_str(schema_json)?; - let canonical = stable_stringify(&value); - Ok(format!("{:x}", Sha256::digest(canonical.as_bytes()))) -} - -fn stable_stringify(value: &Value) -> String { - match value { - Value::Null => "null".to_string(), - Value::Bool(value) => value.to_string(), - Value::Number(value) => value.to_string(), - Value::String(value) => serde_json::to_string(value).expect("string serialization"), - Value::Array(values) => format!( - "[{}]", - values - .iter() - .map(stable_stringify) - .collect::>() - .join(",") - ), - Value::Object(values) => { - let mut entries = values.iter().collect::>(); - entries.sort_by(|(left, _), (right, _)| left.cmp(right)); - format!( - "{{{}}}", - entries - .into_iter() - .map(|(key, value)| { - format!( - "{}:{}", - serde_json::to_string(key).expect("key serialization"), - stable_stringify(value) - ) - }) - .collect::>() - .join(",") - ) - } - } -} - -pub fn builtin_schema_registry() -> Result { - let mut registry = CultNetSchemaRegistry::new(); - - for registration in [ - schema_registration( - include_str!("../contracts/cultnet.hello.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.hello.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.document-mutation-contract.schema.json"), - CultNetSchemaKind::SharedContract, - vec![CultNetWireContract::CultNetSchemaV0], - None, - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.transport-profile.schema.json"), - CultNetSchemaKind::SharedContract, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.transport_profile.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.login.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.login.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.register.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.register.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.verify.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.verify.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.login-success.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.login_success.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.error.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.error.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.sample-change-name.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.sample.change_name.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.sample-chat.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.sample.chat.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.document-put.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.document_put.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.document-delete.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.document_delete.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.raw-document-record.schema.json"), - CultNetSchemaKind::SharedContract, - vec![CultNetWireContract::CultNetSchemaV0], - None, - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.document-put-raw.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.document_put_raw.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.snapshot-request.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.snapshot_request.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.snapshot-response.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.snapshot_response.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.snapshot-response-raw.schema.json"), - CultNetSchemaKind::WireMessage, - vec![CultNetWireContract::CultNetSchemaV0], - Some("cultnet.snapshot_response_raw.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.schema-catalog-request.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.schema_catalog_request.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/cultnet.schema-catalog-response.schema.json"), - CultNetSchemaKind::WireMessage, - vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - Some("cultnet.schema_catalog_response.v0"), - None, - )?, - schema_registration( - include_str!("../contracts/ghostlight.agent-state.schema.json"), - CultNetSchemaKind::DocumentPayload, - vec![CultNetWireContract::CultNetSchemaV0], - Some("ghostlight.agent_state.v0"), - Some("ghostlight.agent-state"), - )?, - ] { - registry.register(registration)?; - } - - Ok(registry) -} - -fn schema_registration( - schema_json: &str, - kind: CultNetSchemaKind, - wire_contracts: Vec, - schema_version: Option<&str>, - document_type: Option<&str>, -) -> Result { - let value: Value = serde_json::from_str(schema_json)?; - let schema_id = value - .get("$id") - .and_then(Value::as_str) - .ok_or_else(|| anyhow!("schema is missing $id"))?; - let title = value - .get("title") - .and_then(Value::as_str) - .map(str::to_string); - - Ok(CultNetSchemaRegistration { - schema_id: schema_id.to_string(), - kind, - wire_contracts, - schema_version: schema_version.map(str::to_string), - document_type: document_type.map(str::to_string), - title, - schema_json: Some(schema_json.to_string()), - }) -} diff --git a/vendor/cultnet-rs/src/security.rs b/vendor/cultnet-rs/src/security.rs deleted file mode 100644 index 30aa841..0000000 --- a/vendor/cultnet-rs/src/security.rs +++ /dev/null @@ -1,270 +0,0 @@ -use aes_gcm::Aes256Gcm; -use aes_gcm::KeyInit; -use aes_gcm::Nonce; -use aes_gcm::aead::Aead; -use aes_gcm::aead::Payload; -use anyhow::Result; -use anyhow::anyhow; -use base64::Engine; -use base64::engine::general_purpose::URL_SAFE_NO_PAD; -use chrono::DateTime; -use chrono::TimeZone; -use chrono::Utc; -use hmac::Hmac; -use hmac::Mac; -use rand::RngCore; -use sha2::Digest; -use sha2::Sha256; - -const NONCE_LENGTH: usize = 12; -type HmacSha256 = Hmac; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetClientSecurityOptions { - pub connection_key: String, - encryption_key: [u8; 32], -} - -impl CultNetClientSecurityOptions { - pub fn new(connection_key: impl Into) -> Result { - let connection_key = connection_key.into(); - if connection_key.trim().is_empty() { - return Err(anyhow!("Connection key must be provided")); - } - let encryption_key = sha256(connection_key.as_bytes()); - Ok(Self { - connection_key, - encryption_key, - }) - } - - pub fn development() -> Self { - Self::new("gamecult-dev-connection-key").expect("development key is valid") - } - - pub fn encryption_key(&self) -> [u8; 32] { - self.encryption_key - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetServerSecurityOptions { - client: CultNetClientSecurityOptions, - pub session_signing_secret: String, - session_signing_key: [u8; 32], - pub is_development: bool, -} - -impl CultNetServerSecurityOptions { - pub fn new( - connection_key: impl Into, - session_signing_secret: impl Into, - is_development: bool, - ) -> Result { - let session_signing_secret = session_signing_secret.into(); - if session_signing_secret.trim().is_empty() { - return Err(anyhow!("Session signing secret must be provided")); - } - Ok(Self { - client: CultNetClientSecurityOptions::new(connection_key)?, - session_signing_key: sha256(session_signing_secret.as_bytes()), - session_signing_secret, - is_development, - }) - } - - pub fn development() -> Self { - Self::new( - "gamecult-dev-connection-key", - "gamecult-dev-session-signing-secret", - true, - ) - .expect("development security config is valid") - } - - pub fn to_client_options(&self) -> CultNetClientSecurityOptions { - self.client.clone() - } - - pub fn encryption_key(&self) -> [u8; 32] { - self.client.encryption_key() - } - - pub fn session_signing_key(&self) -> [u8; 32] { - self.session_signing_key - } -} - -pub trait CultNetEncryptionOptions { - fn encryption_key(&self) -> [u8; 32]; -} - -impl CultNetEncryptionOptions for CultNetClientSecurityOptions { - fn encryption_key(&self) -> [u8; 32] { - self.encryption_key() - } -} - -impl CultNetEncryptionOptions for CultNetServerSecurityOptions { - fn encryption_key(&self) -> [u8; 32] { - self.encryption_key() - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ValidatedCultNetSessionToken { - pub user_id: String, - pub expires_at_utc: DateTime, -} - -pub struct CultNetSecret; - -impl CultNetSecret { - pub fn new_nonce() -> [u8; NONCE_LENGTH] { - let mut nonce = [0_u8; NONCE_LENGTH]; - rand::rng().fill_bytes(&mut nonce); - nonce - } - - pub fn encrypt_string( - input: Option<&str>, - nonce: &[u8], - options: &impl CultNetEncryptionOptions, - ) -> Result>> { - let Some(input) = input.filter(|value| !value.is_empty()) else { - return Ok(None); - }; - Ok(Some(Self::encrypt_bytes(input.as_bytes(), nonce, options)?)) - } - - pub fn decrypt_string( - encrypted: Option<&[u8]>, - nonce: Option<&[u8]>, - options: &impl CultNetEncryptionOptions, - ) -> Result> { - let (Some(encrypted), Some(nonce)) = (encrypted, nonce) else { - return Ok(None); - }; - let bytes = Self::decrypt_bytes(encrypted, nonce, options)?; - Ok(Some(String::from_utf8(bytes)?)) - } - - pub fn encrypt_bytes( - input: &[u8], - nonce: &[u8], - options: &impl CultNetEncryptionOptions, - ) -> Result> { - let nonce = validate_nonce(nonce)?; - let cipher = Aes256Gcm::new_from_slice(&options.encryption_key())?; - let encrypted = cipher - .encrypt( - Nonce::from_slice(nonce), - Payload { - msg: input, - aad: &[], - }, - ) - .map_err(|_| anyhow!("AES-GCM encryption failed"))?; - Ok(encrypted) - } - - pub fn decrypt_bytes( - encrypted: &[u8], - nonce: &[u8], - options: &impl CultNetEncryptionOptions, - ) -> Result> { - let nonce = validate_nonce(nonce)?; - let cipher = Aes256Gcm::new_from_slice(&options.encryption_key())?; - cipher - .decrypt( - Nonce::from_slice(nonce), - Payload { - msg: encrypted, - aad: &[], - }, - ) - .map_err(|_| anyhow!("AES-GCM decryption failed")) - } - - pub fn create_session_token( - user_id: &str, - expires_at_utc: DateTime, - options: &CultNetServerSecurityOptions, - ) -> Result { - let payload = format!("{}|{}", user_id, expires_at_utc.timestamp()); - let signature = hmac_sha256(&options.session_signing_key(), payload.as_bytes())?; - Ok(format!( - "{}.{}", - URL_SAFE_NO_PAD.encode(payload.as_bytes()), - URL_SAFE_NO_PAD.encode(signature) - )) - } - - pub fn try_validate_session_token( - token: Option<&str>, - options: &CultNetServerSecurityOptions, - ) -> Result> { - let Some(token) = token.filter(|value| !value.trim().is_empty()) else { - return Ok(None); - }; - let Some((payload_part, signature_part)) = token.split_once('.') else { - return Ok(None); - }; - let payload = URL_SAFE_NO_PAD.decode(payload_part)?; - let signature = URL_SAFE_NO_PAD.decode(signature_part)?; - let expected = hmac_sha256(&options.session_signing_key(), &payload)?; - if !constant_time_eq(&signature, &expected) { - return Ok(None); - } - let payload = String::from_utf8(payload)?; - let Some((user_id, expires_at)) = payload.split_once('|') else { - return Ok(None); - }; - let expires_at_seconds: i64 = expires_at.parse()?; - let Some(expires_at_utc) = Utc.timestamp_opt(expires_at_seconds, 0).single() else { - return Ok(None); - }; - if expires_at_utc <= Utc::now() { - return Ok(None); - } - Ok(Some(ValidatedCultNetSessionToken { - user_id: user_id.to_string(), - expires_at_utc, - })) - } - - pub fn to_base64_url(input: &[u8]) -> String { - URL_SAFE_NO_PAD.encode(input) - } - - pub fn from_base64_url(input: &str) -> Result> { - Ok(URL_SAFE_NO_PAD.decode(input)?) - } -} - -fn validate_nonce(nonce: &[u8]) -> Result<&[u8]> { - if nonce.len() != NONCE_LENGTH { - return Err(anyhow!("Invalid nonce")); - } - Ok(nonce) -} - -fn sha256(input: &[u8]) -> [u8; 32] { - Sha256::digest(input).into() -} - -fn hmac_sha256(key: &[u8], input: &[u8]) -> Result> { - let mut mac = ::new_from_slice(key)?; - mac.update(input); - Ok(mac.finalize().into_bytes().to_vec()) -} - -fn constant_time_eq(left: &[u8], right: &[u8]) -> bool { - if left.len() != right.len() { - return false; - } - left.iter() - .zip(right) - .fold(0_u8, |acc, (left, right)| acc | (left ^ right)) - == 0 -} diff --git a/vendor/cultnet-rs/src/service_identity.rs b/vendor/cultnet-rs/src/service_identity.rs deleted file mode 100644 index a2323da..0000000 --- a/vendor/cultnet-rs/src/service_identity.rs +++ /dev/null @@ -1,635 +0,0 @@ -use anyhow::{Context, Result, anyhow, bail}; -use cultcache_rs::{ - CacheBackingStore, CultCacheEnvelope, CultCacheExpectedEnvelope, - SingleFileMessagePackBackingStore, -}; -use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey}; -use rand::RngCore; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; -use std::marker::PhantomData; -use std::path::Path; - -pub const WINDOWS_SERVICE_IDENTITY_ASSURANCE: &str = "os_user_installation_bound_best_effort"; -pub const LINUX_SERVICE_IDENTITY_ASSURANCE: &str = "os_installation_file_bound_cloneable_baseline"; - -/// Fixes every authority-bearing name at the call site. A service cannot open -/// another service's identity by supplying a convenient runtime string. -pub trait ServiceIdentityProfile: Send + Sync + 'static { - const PRIVATE_TYPE: &'static str; - const PRIVATE_SCHEMA: &'static str; - const PRIVATE_KEY: &'static str; - const TRUST_ANCHOR_TYPE: &'static str; - const TRUST_ANCHOR_SCHEMA: &'static str; - const TRUST_ANCHOR_KEY: &'static str; - const ID_DOMAIN: &'static [u8]; - const SIGNATURE_DOMAIN: &'static [u8]; - const PROTECTOR_CONTEXT: &'static str; -} - -/// A purpose is a type, not caller-provided prose. Protocol implementations -/// define one zero-sized type per signed statement kind. -pub trait ServiceSignaturePurpose: Send + Sync + 'static { - const PURPOSE: &'static [u8]; -} - -pub enum IdunnServiceIdentity {} -pub enum GameCultProviderHealthIdentity {} - -pub struct IdunnSignedDaemonHealthPurpose; - -impl ServiceSignaturePurpose - for IdunnSignedDaemonHealthPurpose -{ - const PURPOSE: &'static [u8] = b"idunn.signed_daemon_health.v1"; -} - -impl ServiceIdentityProfile for GameCultProviderHealthIdentity { - const PRIVATE_TYPE: &'static str = "gamecult.provider_health_identity.private.v1"; - const PRIVATE_SCHEMA: &'static str = "gamecult.provider_health_identity.private.v1"; - const PRIVATE_KEY: &'static str = "gamecult-provider-health-identity"; - const TRUST_ANCHOR_TYPE: &'static str = "gamecult.provider_health_identity.trust_anchor.v1"; - const TRUST_ANCHOR_SCHEMA: &'static str = "gamecult.provider_health_identity.trust_anchor.v1"; - const TRUST_ANCHOR_KEY: &'static str = "gamecult-provider-health-identity-public"; - const ID_DOMAIN: &'static [u8] = b"gamecult.provider-health.identity.v1\0"; - const SIGNATURE_DOMAIN: &'static [u8] = b"gamecult.provider-health.signature.v1\0"; - const PROTECTOR_CONTEXT: &'static str = "gamecult-provider-health-identity-v1"; -} - -/// The only signing purpose accepted for Idunn's public projection of a -/// provider-authenticated health admission. Keeping this profile here makes -/// purpose selection a compile-time protocol choice rather than caller text. -pub struct IdunnAuthenticatedProviderHealthProjectionPurpose; - -impl ServiceSignaturePurpose - for IdunnAuthenticatedProviderHealthProjectionPurpose -{ - const PURPOSE: &'static [u8] = b"idunn.authenticated-provider-health-projection.v1"; -} - -impl ServiceIdentityProfile for IdunnServiceIdentity { - const PRIVATE_TYPE: &'static str = "idunn.service_identity.private.v1"; - const PRIVATE_SCHEMA: &'static str = "idunn.service_identity.private.v1"; - const PRIVATE_KEY: &'static str = "idunn-service-identity"; - const TRUST_ANCHOR_TYPE: &'static str = "idunn.service_identity.trust_anchor.v1"; - const TRUST_ANCHOR_SCHEMA: &'static str = "idunn.service_identity.trust_anchor.v1"; - const TRUST_ANCHOR_KEY: &'static str = "idunn-service-identity-public"; - const ID_DOMAIN: &'static [u8] = b"idunn.service-identity.id.v1\0"; - const SIGNATURE_DOMAIN: &'static [u8] = b"idunn.service-identity.signature.v1\0"; - const PROTECTOR_CONTEXT: &'static str = "idunn-service-identity-v1"; -} - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct ServiceIdentityPrivateEntry { - pub schema_version: String, - pub identity_id: String, - pub public_key: Vec, - pub protected_private_seed: Vec, - pub protector_kind: String, - pub protector_binding: String, - pub protector_version: String, - pub assurance: String, - pub created_at: String, - pub enrollment_nonce: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ServiceIdentityTrustAnchor { - pub schema_version: String, - pub identity_id: String, - pub public_key: Vec, - pub assurance: String, - pub identity_created_at: String, - pub source_identity_record_sha256: String, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ServiceIdentitySignature { - pub identity_id: String, - pub signature: Vec, -} - -/// The only value holding unprotected key material. It exposes signing, never -/// seed export or access to the underlying signing key. -pub struct ServiceIdentitySigner { - entry: ServiceIdentityPrivateEntry, - signing_key: SigningKey, - profile: PhantomData

, -} - -impl ServiceIdentitySigner

{ - pub fn entry(&self) -> &ServiceIdentityPrivateEntry { - &self.entry - } - - pub fn trust_anchor(&self) -> Result { - Ok(ServiceIdentityTrustAnchor { - schema_version: P::TRUST_ANCHOR_SCHEMA.into(), - identity_id: self.entry.identity_id.clone(), - public_key: self.entry.public_key.clone(), - assurance: self.entry.assurance.clone(), - identity_created_at: self.entry.created_at.clone(), - source_identity_record_sha256: format!( - "sha256-{}", - hex(&Sha256::digest(rmp_serde::to_vec(&self.entry)?)) - ), - }) - } - - pub fn sign>(&self, payload: &[u8]) -> ServiceIdentitySignature { - ServiceIdentitySignature { - identity_id: self.entry.identity_id.clone(), - signature: self - .signing_key - .sign(&signing_message::(payload)) - .to_bytes() - .to_vec(), - } - } -} - -pub fn enroll_service_identity_at( - path: &Path, -) -> Result> { - if path.exists() { - bail!( - "service identity store {} already exists; enrollment is immutable", - path.display() - ); - } - prepare_parent(path)?; - let mut seed = [0u8; 32]; - rand::rng().fill_bytes(&mut seed); - let signing_key = SigningKey::from_bytes(&seed); - let mut nonce = [0u8; 32]; - rand::rng().fill_bytes(&mut nonce); - let public_key = signing_key.verifying_key().to_bytes(); - let binding = platform_binding::

()?; - let entry = ServiceIdentityPrivateEntry { - schema_version: P::PRIVATE_SCHEMA.into(), - identity_id: identity_id::

(&public_key), - public_key: public_key.to_vec(), - protected_private_seed: protect_seed::

(&seed, &binding)?, - protector_kind: platform_protector_kind().into(), - protector_binding: binding, - protector_version: "v1".into(), - assurance: platform_assurance().into(), - created_at: chrono::Utc::now().to_rfc3339(), - enrollment_nonce: nonce.to_vec(), - }; - validate_private::

(&entry)?; - let envelope = private_envelope::

(&entry)?; - let backing = SingleFileMessagePackBackingStore::new(path); - let expected = CultCacheExpectedEnvelope { - r#type: P::PRIVATE_TYPE.into(), - key: P::PRIVATE_KEY.into(), - current: None, - }; - if !backing.compare_exchange(&[expected], &[envelope])? { - bail!("service identity enrollment lost atomic create race; refusing existing state"); - } - harden_store_permissions(path)?; - open_service_identity_at::

(path) -} - -pub fn open_service_identity_at( - path: &Path, -) -> Result> { - if !path.is_file() { - bail!("service identity store {} does not exist", path.display()); - } - let entries = SingleFileMessagePackBackingStore::new(path).pull_all()?; - if entries.len() != 1 { - bail!("service identity store must contain exactly one immutable envelope"); - } - let envelope = &entries[0]; - if envelope.r#type != P::PRIVATE_TYPE - || envelope.key != P::PRIVATE_KEY - || envelope.schema_id.as_deref() != Some(P::PRIVATE_SCHEMA) - { - bail!("service identity store belongs to a different profile or schema"); - } - let entry: ServiceIdentityPrivateEntry = rmp_serde::from_slice(&envelope.payload) - .context("service identity private payload is malformed MessagePack")?; - validate_private::

(&entry)?; - if entry.protector_kind != platform_protector_kind() || entry.assurance != platform_assurance() - { - bail!("service identity protector does not belong to this platform implementation"); - } - let binding = platform_binding::

()?; - if entry.protector_binding != binding { - bail!("service identity protector binding does not match this OS installation or profile"); - } - let seed: [u8; 32] = unprotect_seed::

(&entry.protected_private_seed, &binding)? - .try_into() - .map_err(|_| anyhow!("unprotected service identity seed has invalid length"))?; - let signing_key = SigningKey::from_bytes(&seed); - if signing_key.verifying_key().to_bytes().as_slice() != entry.public_key.as_slice() { - bail!("service identity private seed does not match enrolled public key"); - } - Ok(ServiceIdentitySigner { - entry, - signing_key, - profile: PhantomData, - }) -} - -pub fn export_service_identity_trust_anchor( - signer: &ServiceIdentitySigner

, - path: &Path, -) -> Result { - let anchor = signer.trust_anchor()?; - prepare_parent(path)?; - let envelope = CultCacheEnvelope { - key: P::TRUST_ANCHOR_KEY.into(), - r#type: P::TRUST_ANCHOR_TYPE.into(), - payload: rmp_serde::to_vec(&anchor)?, - stored_at: anchor.identity_created_at.clone(), - schema_id: Some(P::TRUST_ANCHOR_SCHEMA.into()), - }; - let mut backing = SingleFileMessagePackBackingStore::new(path); - match backing.pull_all()?.as_slice() { - [] => backing.push(&envelope)?, - [current] if current == &envelope => {} - _ => bail!("service identity trust anchor output already contains different state"), - } - Ok(anchor) -} - -pub fn verify_service_identity_signature( - anchor: &ServiceIdentityTrustAnchor, - payload: &[u8], - proof: &ServiceIdentitySignature, -) -> Result<()> -where - P: ServiceIdentityProfile, - S: ServiceSignaturePurpose

, -{ - validate_anchor::

(anchor)?; - if proof.identity_id != anchor.identity_id { - bail!("service identity signature names a different identity"); - } - let key: [u8; 32] = anchor - .public_key - .as_slice() - .try_into() - .map_err(|_| anyhow!("service identity public key has invalid length"))?; - let signature = Signature::from_slice(&proof.signature) - .map_err(|_| anyhow!("service identity signature has invalid length"))?; - VerifyingKey::from_bytes(&key)? - .verify(&signing_message::(payload), &signature) - .map_err(|_| anyhow!("service identity signature verification failed")) -} - -/// Verifies against a public key already admitted by an owning trust store. -/// The identity is still derived from the profile; callers cannot name it. -pub fn verify_service_identity_signature_with_public_key( - public_key: &[u8], - payload: &[u8], - proof: &ServiceIdentitySignature, -) -> Result<()> -where - P: ServiceIdentityProfile, - S: ServiceSignaturePurpose

, -{ - if proof.identity_id != derive_service_identity_id::

(public_key)? { - bail!("service identity signature names a different identity"); - } - let key: [u8; 32] = public_key - .try_into() - .map_err(|_| anyhow!("service identity public key has invalid length"))?; - let signature = Signature::from_slice(&proof.signature) - .map_err(|_| anyhow!("service identity signature has invalid length"))?; - VerifyingKey::from_bytes(&key)? - .verify(&signing_message::(payload), &signature) - .map_err(|_| anyhow!("service identity signature verification failed")) -} - -fn validate_private(entry: &ServiceIdentityPrivateEntry) -> Result<()> { - if entry.schema_version != P::PRIVATE_SCHEMA - || entry.public_key.len() != 32 - || entry.enrollment_nonce.len() != 32 - || entry.protected_private_seed.is_empty() - || entry.protector_version != "v1" - { - bail!("service identity private entry violates its profile schema"); - } - chrono::DateTime::parse_from_rfc3339(&entry.created_at) - .map_err(|_| anyhow!("service identity created_at is not RFC3339"))?; - if identity_id::

(&entry.public_key) != entry.identity_id { - bail!("service identity id does not match public key"); - } - Ok(()) -} - -fn validate_anchor(anchor: &ServiceIdentityTrustAnchor) -> Result<()> { - if anchor.schema_version != P::TRUST_ANCHOR_SCHEMA - || anchor.public_key.len() != 32 - || derive_service_identity_id::

(&anchor.public_key)? != anchor.identity_id - { - bail!("service identity trust anchor violates its profile schema"); - } - Ok(()) -} - -/// Derives the profile-bound identity id from an exact Ed25519 public key. -/// Consumers use this instead of accepting a caller-supplied identity string. -pub fn derive_service_identity_id(public_key: &[u8]) -> Result { - if public_key.len() != 32 { - bail!("service identity public key has invalid length"); - } - Ok(hex(&Sha256::digest([P::ID_DOMAIN, public_key].concat()))) -} - -fn identity_id(key: &[u8]) -> String { - derive_service_identity_id::

(key).expect("internal service identity key is Ed25519") -} - -fn signing_message>( - payload: &[u8], -) -> Vec { - let mut out = - Vec::with_capacity(P::SIGNATURE_DOMAIN.len() + S::PURPOSE.len() + payload.len() + 16); - out.extend_from_slice(P::SIGNATURE_DOMAIN); - out.extend_from_slice(&(S::PURPOSE.len() as u64).to_be_bytes()); - out.extend_from_slice(S::PURPOSE); - out.extend_from_slice(&(payload.len() as u64).to_be_bytes()); - out.extend_from_slice(payload); - out -} - -fn private_envelope( - entry: &ServiceIdentityPrivateEntry, -) -> Result { - Ok(CultCacheEnvelope { - key: P::PRIVATE_KEY.into(), - r#type: P::PRIVATE_TYPE.into(), - payload: rmp_serde::to_vec(entry)?, - stored_at: entry.created_at.clone(), - schema_id: Some(P::PRIVATE_SCHEMA.into()), - }) -} - -fn prepare_parent(path: &Path) -> Result<()> { - let parent = path - .parent() - .ok_or_else(|| anyhow!("service identity path has no parent"))?; - std::fs::create_dir_all(parent)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700))?; - } - Ok(()) -} -fn harden_store_permissions(_path: &Path) -> Result<()> { - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(_path, std::fs::Permissions::from_mode(0o600))?; - } - Ok(()) -} - -#[cfg(windows)] -fn platform_protector_kind() -> &'static str { - "windows_dpapi_current_user" -} -#[cfg(target_os = "linux")] -fn platform_protector_kind() -> &'static str { - "linux_file_mode_machine_id_binding" -} -#[cfg(windows)] -fn platform_assurance() -> &'static str { - WINDOWS_SERVICE_IDENTITY_ASSURANCE -} -#[cfg(target_os = "linux")] -fn platform_assurance() -> &'static str { - LINUX_SERVICE_IDENTITY_ASSURANCE -} - -#[cfg(windows)] -fn platform_binding() -> Result { - Ok(format!("dpapi-current-user:{}", P::PROTECTOR_CONTEXT)) -} -#[cfg(target_os = "linux")] -fn platform_binding() -> Result { - let raw = std::fs::read_to_string("/etc/machine-id") - .or_else(|_| std::fs::read_to_string("/var/lib/dbus/machine-id")) - .context("Linux machine-id is unavailable")?; - let id = raw.trim(); - if id.is_empty() { - bail!("Linux machine-id is empty"); - } - Ok(format!( - "{}:machine-id-sha256:{}", - P::PROTECTOR_CONTEXT, - hex(&Sha256::digest(id.as_bytes())) - )) -} - -#[cfg(windows)] -fn protect_seed(seed: &[u8; 32], binding: &str) -> Result> { - dpapi::

(seed, binding, true) -} -#[cfg(windows)] -fn unprotect_seed(seed: &[u8], binding: &str) -> Result> { - dpapi::

(seed, binding, false) -} -#[cfg(windows)] -fn dpapi(input: &[u8], binding: &str, protect: bool) -> Result> { - use windows_sys::Win32::Foundation::LocalFree; - use windows_sys::Win32::Security::Cryptography::{ - CRYPT_INTEGER_BLOB, CRYPTPROTECT_UI_FORBIDDEN, CryptProtectData, CryptUnprotectData, - }; - let mut input = input.to_vec(); - let mut entropy = Sha256::digest( - [ - b"gamecult-service-identity-dpapi-v1\0".as_slice(), - P::PROTECTOR_CONTEXT.as_bytes(), - binding.as_bytes(), - ] - .concat(), - ) - .to_vec(); - let ib = CRYPT_INTEGER_BLOB { - cbData: input.len() as u32, - pbData: input.as_mut_ptr(), - }; - let eb = CRYPT_INTEGER_BLOB { - cbData: entropy.len() as u32, - pbData: entropy.as_mut_ptr(), - }; - let mut output = CRYPT_INTEGER_BLOB { - cbData: 0, - pbData: std::ptr::null_mut(), - }; - let ok = unsafe { - if protect { - CryptProtectData( - &ib, - std::ptr::null(), - &eb, - std::ptr::null(), - std::ptr::null(), - CRYPTPROTECT_UI_FORBIDDEN, - &mut output, - ) - } else { - CryptUnprotectData( - &ib, - std::ptr::null_mut(), - &eb, - std::ptr::null(), - std::ptr::null(), - CRYPTPROTECT_UI_FORBIDDEN, - &mut output, - ) - } - }; - if ok == 0 { - return Err(std::io::Error::last_os_error()) - .context("DPAPI service identity operation failed"); - } - let bytes = - unsafe { std::slice::from_raw_parts(output.pbData, output.cbData as usize).to_vec() }; - unsafe { - LocalFree(output.pbData.cast()); - } - Ok(bytes) -} - -#[cfg(target_os = "linux")] -fn protect_seed(seed: &[u8; 32], binding: &str) -> Result> { - xor_linux::

(seed, binding) -} -#[cfg(target_os = "linux")] -fn unprotect_seed(seed: &[u8], binding: &str) -> Result> { - if seed.len() != 32 { - bail!("protected Linux service seed has invalid length"); - } - xor_linux::

(seed, binding) -} -#[cfg(target_os = "linux")] -fn xor_linux(seed: &[u8], binding: &str) -> Result> { - let mask = Sha256::digest( - [ - b"gamecult-linux-service-seed-v1\0".as_slice(), - P::PROTECTOR_CONTEXT.as_bytes(), - binding.as_bytes(), - ] - .concat(), - ); - Ok(seed.iter().zip(mask).map(|(a, b)| a ^ b).collect()) -} - -fn hex(bytes: &[u8]) -> String { - const D: &[u8; 16] = b"0123456789abcdef"; - let mut s = String::with_capacity(bytes.len() * 2); - for b in bytes { - s.push(D[(b >> 4) as usize] as char); - s.push(D[(b & 15) as usize] as char); - } - s -} - -#[cfg(test)] -mod tests { - use super::*; - enum OtherPurpose {} - impl ServiceSignaturePurpose for OtherPurpose { - const PURPOSE: &'static [u8] = b"idunn.other.v1"; - } - - #[test] - fn immutable_enrollment_reopens_and_is_purpose_and_payload_bound() -> Result<()> { - let temp = tempfile::tempdir()?; - let path = temp.path().join("idunn.ccmp"); - let signer = enroll_service_identity_at::(&path)?; - let proof = signer.sign::(b"healthy"); - let anchor = signer.trust_anchor()?; - verify_service_identity_signature::< - IdunnServiceIdentity, - IdunnAuthenticatedProviderHealthProjectionPurpose, - >(&anchor, b"healthy", &proof)?; - assert!( - verify_service_identity_signature::< - IdunnServiceIdentity, - IdunnAuthenticatedProviderHealthProjectionPurpose, - >(&anchor, b"unhealthy", &proof) - .is_err() - ); - assert!( - verify_service_identity_signature::( - &anchor, b"healthy", &proof - ) - .is_err() - ); - assert!(enroll_service_identity_at::(&path).is_err()); - assert_eq!( - derive_service_identity_id::(&anchor.public_key)?, - anchor.identity_id - ); - assert!(derive_service_identity_id::(&[0; 31]).is_err()); - assert_eq!( - open_service_identity_at::(&path)?.entry(), - signer.entry() - ); - Ok(()) - } - - #[test] - fn malformed_or_profile_substituted_state_fails_closed_without_replacement() -> Result<()> { - enum Alien {} - impl ServiceIdentityProfile for Alien { - const PRIVATE_TYPE: &'static str = "alien.private.v1"; - const PRIVATE_SCHEMA: &'static str = "alien.private.v1"; - const PRIVATE_KEY: &'static str = "alien"; - const TRUST_ANCHOR_TYPE: &'static str = "alien.anchor.v1"; - const TRUST_ANCHOR_SCHEMA: &'static str = "alien.anchor.v1"; - const TRUST_ANCHOR_KEY: &'static str = "alien-public"; - const ID_DOMAIN: &'static [u8] = b"alien.id\0"; - const SIGNATURE_DOMAIN: &'static [u8] = b"alien.sig\0"; - const PROTECTOR_CONTEXT: &'static str = "alien-v1"; - } - let temp = tempfile::tempdir()?; - let malformed = temp.path().join("bad.ccmp"); - std::fs::write(&malformed, b"rot")?; - let before = std::fs::read(&malformed)?; - assert!(open_service_identity_at::(&malformed).is_err()); - assert!(enroll_service_identity_at::(&malformed).is_err()); - assert_eq!(before, std::fs::read(&malformed)?); - let good = temp.path().join("good.ccmp"); - enroll_service_identity_at::(&good)?; - assert!(open_service_identity_at::(&good).is_err()); - Ok(()) - } - - #[test] - fn exported_anchor_is_public_only_immutable_and_rejects_identity_substitution() -> Result<()> { - let temp = tempfile::tempdir()?; - let signer = - enroll_service_identity_at::(&temp.path().join("private.ccmp"))?; - let output = temp.path().join("public.ccmp"); - let anchor = export_service_identity_trust_anchor(&signer, &output)?; - let bytes = std::fs::read(&output)?; - assert!( - !bytes - .windows(signer.entry().protected_private_seed.len()) - .any(|w| w == signer.entry().protected_private_seed) - ); - export_service_identity_trust_anchor(&signer, &output)?; - assert_eq!(bytes, std::fs::read(&output)?); - let other = - enroll_service_identity_at::(&temp.path().join("other.ccmp"))?; - let proof = other.sign::(b"healthy"); - assert!( - verify_service_identity_signature::< - IdunnServiceIdentity, - IdunnAuthenticatedProviderHealthProjectionPurpose, - >(&anchor, b"healthy", &proof) - .is_err() - ); - Ok(()) - } -} diff --git a/vendor/cultnet-rs/src/snapshot_query.rs b/vendor/cultnet-rs/src/snapshot_query.rs deleted file mode 100644 index f6150d1..0000000 --- a/vendor/cultnet-rs/src/snapshot_query.rs +++ /dev/null @@ -1,339 +0,0 @@ -use anyhow::{Result, anyhow}; -use std::collections::{BTreeMap, BTreeSet}; - -use crate::{CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord}; - -/// Read-only backing surface for a CultNet snapshot server. -/// -/// Records returned here are already the stored wire records. The snapshot -/// server selects and clones them; it never decodes payloads, rewrites source -/// metadata, or applies them to another cache. -pub trait CultNetRawSnapshotSource { - fn raw_snapshot(&self) -> Result>; -} - -impl CultNetRawSnapshotSource for Vec { - fn raw_snapshot(&self) -> Result> { - Ok(self.clone()) - } -} - -impl CultNetRawSnapshotSource for F -where - F: Fn() -> Result>, -{ - fn raw_snapshot(&self) -> Result> { - self() - } -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetReadOnlySnapshotPolicy { - allowed_records: BTreeSet<(String, String)>, -} - -impl CultNetReadOnlySnapshotPolicy { - pub fn new() -> Self { - Self::default() - } - - pub fn allow( - &mut self, - schema_id: impl Into, - record_key: impl Into, - ) -> Result<&mut Self> { - let schema_id = schema_id.into(); - let record_key = record_key.into(); - require_non_empty(&schema_id, "schema_id")?; - require_non_empty(&record_key, "record_key")?; - self.allowed_records.insert((schema_id, record_key)); - Ok(self) - } - - fn allows(&self, schema_id: &str, record_key: &str) -> bool { - self.allowed_records - .contains(&(schema_id.to_string(), record_key.to_string())) - } -} - -/// Serve one raw snapshot request from an explicitly bounded, read-only view. -/// -/// The document registry proves that every exposed schema belongs to the -/// runtime. The policy grants individual `(schema_id, record_key)` pairs. The -/// source owns the bytes and metadata; this function only selects records. -pub fn serve_read_only_raw_snapshot( - document_registry: &CultNetDocumentRegistry, - policy: &CultNetReadOnlySnapshotPolicy, - source: &S, - request: &CultNetMessage, -) -> Result { - let CultNetMessage::SnapshotRequest { - message_id, - schema_ids, - record_keys, - } = request - else { - return Err(anyhow!("expected cultnet.snapshot_request.v0")); - }; - require_non_empty(message_id, "message_id")?; - reject_duplicates(schema_ids.as_deref(), "requested schema id")?; - reject_duplicates(record_keys.as_deref(), "requested record key")?; - - let requested_schemas = schema_ids - .as_ref() - .map(|values| values.iter().map(String::as_str).collect::>()); - let requested_keys = record_keys - .as_ref() - .map(|values| values.iter().map(String::as_str).collect::>()); - let mut selected = Vec::new(); - let mut seen = BTreeSet::new(); - - for document in source.raw_snapshot()? { - let identity = (document.schema_id.clone(), document.record_key.clone()); - if !seen.insert(identity.clone()) { - return Err(anyhow!( - "snapshot source contains duplicate record {:?}/{:?}", - identity.0, - identity.1 - )); - } - if document_registry - .binding_by_schema_id(&document.schema_id) - .is_none() - { - return Err(anyhow!( - "snapshot source record {:?}/{:?} uses an unregistered schema", - document.schema_id, - document.record_key - )); - } - if !policy.allows(&document.schema_id, &document.record_key) { - continue; - } - if requested_schemas - .as_ref() - .is_some_and(|schemas| !schemas.contains(document.schema_id.as_str())) - { - continue; - } - if requested_keys - .as_ref() - .is_some_and(|keys| !keys.contains(document.record_key.as_str())) - { - continue; - } - selected.push(document); - } - - Ok(CultNetMessage::SnapshotResponseRaw { - message_id: message_id.clone(), - documents: selected, - }) -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetSnapshotSourceExpectation { - pub runtime_id: Option, - pub agent_id: Option, - pub role: Option, - pub tags: Option>, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetSnapshotRecordExpectation { - pub schema_id: String, - pub record_key: String, - pub source: CultNetSnapshotSourceExpectation, -} - -impl CultNetSnapshotRecordExpectation { - pub fn new(schema_id: impl Into, record_key: impl Into) -> Self { - Self { - schema_id: schema_id.into(), - record_key: record_key.into(), - source: CultNetSnapshotSourceExpectation::default(), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetRawSnapshotQuery { - message_id: String, - expectations: BTreeMap<(String, String), CultNetSnapshotSourceExpectation>, -} - -impl CultNetRawSnapshotQuery { - pub fn new( - message_id: impl Into, - records: Vec, - ) -> Result { - let message_id = message_id.into(); - require_non_empty(&message_id, "message_id")?; - if records.is_empty() { - return Err(anyhow!("snapshot query must request at least one record")); - } - let mut expectations = BTreeMap::new(); - for record in records { - require_non_empty(&record.schema_id, "schema_id")?; - require_non_empty(&record.record_key, "record_key")?; - let identity = (record.schema_id, record.record_key); - if expectations - .insert(identity.clone(), record.source) - .is_some() - { - return Err(anyhow!( - "snapshot query contains duplicate requested record {:?}/{:?}", - identity.0, - identity.1 - )); - } - } - Ok(Self { - message_id, - expectations, - }) - } - - pub fn request(&self) -> CultNetMessage { - CultNetMessage::SnapshotRequest { - message_id: self.message_id.clone(), - schema_ids: Some( - self.expectations - .keys() - .map(|(schema_id, _)| schema_id.clone()) - .collect::>() - .into_iter() - .collect(), - ), - record_keys: Some( - self.expectations - .keys() - .map(|(_, record_key)| record_key.clone()) - .collect::>() - .into_iter() - .collect(), - ), - } - } - - /// Validate a response and return records without mutating any local cache. - pub fn accept_response( - &self, - response: CultNetMessage, - ) -> Result> { - let CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } = response - else { - return Err(anyhow!("expected cultnet.snapshot_response_raw.v0")); - }; - if message_id != self.message_id { - return Err(anyhow!( - "snapshot response message id {:?} does not match request {:?}", - message_id, - self.message_id - )); - } - - let mut seen = BTreeSet::new(); - for document in &documents { - let identity = (document.schema_id.clone(), document.record_key.clone()); - if !seen.insert(identity.clone()) { - return Err(anyhow!( - "snapshot response contains duplicate record {:?}/{:?}", - identity.0, - identity.1 - )); - } - let expected = self.expectations.get(&identity).ok_or_else(|| { - anyhow!( - "snapshot response contains unexpected record {:?}/{:?}", - identity.0, - identity.1 - ) - })?; - require_expected_metadata( - "source_runtime_id", - expected.runtime_id.as_deref(), - document.source_runtime_id.as_deref(), - &identity, - )?; - require_expected_metadata( - "source_agent_id", - expected.agent_id.as_deref(), - document.source_agent_id.as_deref(), - &identity, - )?; - require_expected_metadata( - "source_role", - expected.role.as_deref(), - document.source_role.as_deref(), - &identity, - )?; - if let Some(expected_tags) = expected.tags.as_ref() - && document.tags.as_ref() != Some(expected_tags) - { - return Err(anyhow!( - "snapshot response record {:?}/{:?} has unexpected tags: expected {:?}, got {:?}", - identity.0, - identity.1, - expected_tags, - document.tags - )); - } - } - Ok(documents) - } -} - -pub fn query_read_only_raw_snapshot( - query: &CultNetRawSnapshotQuery, - exchange: F, -) -> Result> -where - F: FnOnce(CultNetMessage) -> Result, -{ - query.accept_response(exchange(query.request())?) -} - -fn require_expected_metadata( - field: &str, - expected: Option<&str>, - actual: Option<&str>, - identity: &(String, String), -) -> Result<()> { - if let Some(expected) = expected - && actual != Some(expected) - { - return Err(anyhow!( - "snapshot response record {:?}/{:?} has unexpected {field}: expected {:?}, got {:?}", - identity.0, - identity.1, - expected, - actual - )); - } - Ok(()) -} - -fn reject_duplicates(values: Option<&[String]>, label: &str) -> Result<()> { - let Some(values) = values else { - return Ok(()); - }; - let mut seen = BTreeSet::new(); - for value in values { - require_non_empty(value, label)?; - if !seen.insert(value) { - return Err(anyhow!("duplicate {label} {value:?}")); - } - } - Ok(()) -} - -fn require_non_empty(value: &str, field: &str) -> Result<()> { - if value.trim().is_empty() { - return Err(anyhow!("{field} must be non-empty")); - } - Ok(()) -} diff --git a/vendor/cultnet-rs/src/transport.rs b/vendor/cultnet-rs/src/transport.rs deleted file mode 100644 index 55e0097..0000000 --- a/vendor/cultnet-rs/src/transport.rs +++ /dev/null @@ -1,194 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; -use serde::Deserialize; -use serde::Serialize; -use std::io::Read; -use std::io::Write; - -use crate::CultNetTransportChannel; -use crate::CultNetTransportDelivery; -use crate::CultNetTransportDescriptor; -use crate::CultNetTransportOrdering; -use crate::CultNetTransportProfile; -use crate::CultNetTransportProtocol; -use crate::FRAME_HEADER_BYTES; -use crate::encode_frame; - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultNetTransportStats { - pub bytes_received: u64, - pub bytes_sent: u64, - pub frames_received: u64, - pub frames_sent: u64, - pub reliable_packets_expired: u64, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultNetTransportFrame { - pub channel_id: String, - pub payload: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultNetReconnectPolicy { - pub schema_version: String, - pub policy_id: String, - pub base_delay_ms: u64, - pub max_delay_ms: u64, - pub max_jitter_ms: u64, - #[serde(skip_serializing_if = "Option::is_none")] - pub max_attempts: Option, -} - -#[derive(Clone, Debug)] -pub struct CultNetReconnectPolicyOptions { - pub policy_id: String, - pub base_delay_ms: u64, - pub max_delay_ms: u64, - pub max_jitter_ms: u64, - pub max_attempts: Option, -} - -impl Default for CultNetReconnectPolicyOptions { - fn default() -> Self { - Self { - policy_id: "default".to_string(), - base_delay_ms: 1_000, - max_delay_ms: 30_000, - max_jitter_ms: 250, - max_attempts: None, - } - } -} - -pub fn create_reconnect_policy(options: CultNetReconnectPolicyOptions) -> CultNetReconnectPolicy { - CultNetReconnectPolicy { - schema_version: "cultnet.reconnect_policy.v0".to_string(), - policy_id: if options.policy_id.trim().is_empty() { - "default".to_string() - } else { - options.policy_id - }, - base_delay_ms: options.base_delay_ms, - max_delay_ms: options.max_delay_ms, - max_jitter_ms: options.max_jitter_ms, - max_attempts: options.max_attempts, - } -} - -pub fn compute_reconnect_delay_ms( - policy: &CultNetReconnectPolicy, - attempt: u32, - jitter_ms: u64, -) -> u64 { - let normalized_attempt = attempt.max(1); - let multiplier = 2_u64.saturating_pow(normalized_attempt.saturating_sub(1)); - let capped_base_delay = policy - .base_delay_ms - .saturating_mul(multiplier) - .min(policy.max_delay_ms); - capped_base_delay + jitter_ms.min(policy.max_jitter_ms) -} - -#[derive(Clone, Debug, Default)] -pub struct TcpFramedTransportProfileOptions { - pub transport_id: Option, - pub host: Option, - pub port: Option, - pub max_payload_bytes: Option, - pub max_fragment_bytes: Option, -} - -pub fn create_tcp_framed_transport_profile( - runtime_id: impl Into, - options: TcpFramedTransportProfileOptions, -) -> CultNetTransportProfile { - CultNetTransportProfile { - schema_version: "cultnet.transport_profile.v0".to_string(), - runtime_id: runtime_id.into(), - transports: vec![CultNetTransportDescriptor { - transport_id: options - .transport_id - .unwrap_or_else(|| "tcp-framed".to_string()), - protocol: CultNetTransportProtocol::TcpFramed, - host: options.host, - port: options.port, - path: None, - discovery_group: None, - wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]), - channels: vec![CultNetTransportChannel { - channel_id: "schema".to_string(), - delivery: CultNetTransportDelivery::Reliable, - ordering: CultNetTransportOrdering::Ordered, - max_payload_bytes: options.max_payload_bytes, - max_fragment_bytes: options.max_fragment_bytes, - max_pending_reliable_packets: None, - reliable_expire_after_ms: None, - }], - }], - } -} - -pub struct TcpFramedTransportConnection { - stream: TStream, - pub profile: CultNetTransportProfile, - stats: CultNetTransportStats, -} - -impl TcpFramedTransportConnection { - pub fn new(stream: TStream, profile: CultNetTransportProfile) -> Self { - Self { - stream, - profile, - stats: CultNetTransportStats::default(), - } - } - - pub fn stats(&self) -> CultNetTransportStats { - self.stats.clone() - } - - pub fn into_inner(self) -> TStream { - self.stream - } -} - -impl TcpFramedTransportConnection -where - TStream: Write, -{ - pub fn send(&mut self, channel_id: &str, payload: &[u8]) -> Result<()> { - if channel_id != "schema" { - return Err(anyhow!( - "tcp_framed transport only supports the schema channel, got {channel_id:?}" - )); - } - - let frame = encode_frame(payload)?; - self.stream.write_all(&frame)?; - self.stream.flush()?; - self.stats.bytes_sent += frame.len() as u64; - self.stats.frames_sent += 1; - Ok(()) - } -} - -impl TcpFramedTransportConnection -where - TStream: Read, -{ - pub fn receive(&mut self) -> Result { - let mut header = [0_u8; FRAME_HEADER_BYTES]; - self.stream.read_exact(&mut header)?; - let payload_len = u32::from_be_bytes(header) as usize; - let mut payload = vec![0_u8; payload_len]; - self.stream.read_exact(&mut payload)?; - self.stats.bytes_received += (FRAME_HEADER_BYTES + payload_len) as u64; - self.stats.frames_received += 1; - Ok(CultNetTransportFrame { - channel_id: "schema".to_string(), - payload, - }) - } -} diff --git a/vendor/cultnet-rs/tests/cultnet.rs b/vendor/cultnet-rs/tests/cultnet.rs deleted file mode 100644 index a650e28..0000000 --- a/vendor/cultnet-rs/tests/cultnet.rs +++ /dev/null @@ -1,1352 +0,0 @@ -use anyhow::Result; -use chrono::Duration; -use cultcache_rs::CultCache; -use cultcache_rs::DatabaseEntry; -use cultcache_rs::SingleFileMessagePackBackingStore; -use cultnet_rs::CultNetClientSecurityOptions; -use cultnet_rs::CultNetDocumentBinding; -use cultnet_rs::CultNetDocumentMutationContract; -use cultnet_rs::CultNetDocumentOperation; -use cultnet_rs::CultNetDocumentPutOptions; -use cultnet_rs::CultNetDocumentRegistry; -use cultnet_rs::CultNetMessage; -use cultnet_rs::CultNetMutationAuthority; -use cultnet_rs::CultNetRudpPacket; -use cultnet_rs::CultNetRudpPacketType; -use cultnet_rs::CultNetRudpSendOptions; -use cultnet_rs::CultNetRudpSession; -use cultnet_rs::CultNetRudpSessionOptions; -use cultnet_rs::CultNetRudpSocketMode; -use cultnet_rs::CultNetRudpSocketTransportConnection; -use cultnet_rs::CultNetRudpSocketTransportOptions; -use cultnet_rs::CultNetSchemaKind; -use cultnet_rs::CultNetSchemaRegistry; -use cultnet_rs::CultNetSecret; -use cultnet_rs::CultNetServerSecurityOptions; -use cultnet_rs::CultNetTransportChannel; -use cultnet_rs::CultNetTransportDelivery; -use cultnet_rs::CultNetTransportDescriptor; -use cultnet_rs::CultNetTransportFrame; -use cultnet_rs::CultNetTransportOrdering; -use cultnet_rs::CultNetTransportProfile; -use cultnet_rs::CultNetTransportProtocol; -use cultnet_rs::CultNetWireContract; -use cultnet_rs::LengthPrefixedMessageFramer; -use cultnet_rs::TcpFramedTransportConnection; -use cultnet_rs::TcpFramedTransportProfileOptions; -use cultnet_rs::builtin_schema_registry; -use cultnet_rs::create_rudp_transport_profile; -use cultnet_rs::create_tcp_framed_transport_profile; -use cultnet_rs::decode_cultnet_message_from_slice; -use cultnet_rs::decode_rudp_packet; -use cultnet_rs::encode_cultnet_message_for_wire; -use cultnet_rs::encode_cultnet_message_to_vec; -use cultnet_rs::encode_frame; -use cultnet_rs::encode_rudp_packet; -use pretty_assertions::assert_eq; -use std::net::UdpSocket; -use std::thread; -use std::time::Duration as StdDuration; - -const TS_HELLO_FRAME: &[u8] = include_bytes!("fixtures/cultnet-ts-hello.frame"); -const TS_LEGACY_LOGIN_FRAME: &[u8] = include_bytes!("fixtures/cultnet-ts-legacy-login.frame"); - -fn bind_udp_socket() -> Result { - let socket = UdpSocket::bind("127.0.0.1:0")?; - socket.set_read_timeout(Some(StdDuration::from_millis(20)))?; - Ok(socket) -} - -fn pump_rudp_handshake( - client: &mut CultNetRudpSocketTransportConnection, - server: &mut CultNetRudpSocketTransportConnection, -) -> Result<()> { - for _ in 0..20 { - let _ = server.receive_once()?; - let _ = client.receive_once()?; - let _ = server.receive_once()?; - if client.connected() && server.connected() { - return Ok(()); - } - thread::sleep(StdDuration::from_millis(5)); - } - anyhow::bail!("RUDP socket handshake did not complete"); -} - -fn receive_rudp_frame( - transport: &mut CultNetRudpSocketTransportConnection, -) -> Result { - for _ in 0..20 { - if let Some(frame) = transport.receive_once()? { - return Ok(frame); - } - thread::sleep(StdDuration::from_millis(5)); - } - anyhow::bail!("RUDP socket frame was not delivered") -} - -#[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache( - type = "ghostlight.agent-state", - schema = "GhostlightAgentStateFixture" -)] -struct GhostlightAgentStateFixture { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - agent_id: String, - #[cultcache(key = 2)] - display_name: String, -} - -#[test] -fn security_helpers_round_trip_encrypted_strings_and_validate_sessions() -> Result<()> { - let server_security = CultNetServerSecurityOptions::development(); - let client_security = server_security.to_client_options(); - let nonce = CultNetSecret::new_nonce(); - let encrypted = CultNetSecret::encrypt_string(Some("hello"), &nonce, &client_security)? - .expect("non-empty input encrypts"); - assert_eq!( - CultNetSecret::decrypt_string(Some(&encrypted), Some(&nonce), &server_security)?, - Some("hello".to_string()) - ); - - let token = CultNetSecret::create_session_token( - "runtime-face", - chrono::Utc::now() + Duration::minutes(1), - &server_security, - )?; - let validated = CultNetSecret::try_validate_session_token(Some(&token), &server_security)? - .expect("token validates before expiry"); - assert_eq!(validated.user_id, "runtime-face"); - Ok(()) -} - -#[test] -fn cultnet_schema_messages_round_trip_through_messagepack_frames() -> Result<()> { - let message = CultNetMessage::Hello { - runtime_id: "voidbot-main".to_string(), - runtime_kind: "rust-worker".to_string(), - agent_id: Some("void".to_string()), - role: None, - display_name: Some("Void".to_string()), - supported_document_types: Some(vec!["ghostlight.agent-state".to_string()]), - supported_mutation_contracts: Some(vec![CultNetDocumentMutationContract { - document_type: "ghostlight.agent-state".to_string(), - payload_schema_version: Some("ghostlight.agent_state.v0".to_string()), - operations: vec![ - CultNetDocumentOperation::Snapshot, - CultNetDocumentOperation::DocumentPut, - ], - authority: CultNetMutationAuthority::Coordinator, - intent_document_types: Some(vec!["ghostlight.agent-state.intent.v0".to_string()]), - receipt_document_types: Some(vec!["ghostlight.agent-state.receipt.v0".to_string()]), - notes: Some(vec![ - "Persona may request bounded memory mutation; coordinator reviews authority." - .to_string(), - ]), - }]), - supported_message_versions: None, - transport_profiles: Some(vec![CultNetTransportProfile { - schema_version: "cultnet.transport_profile.v0".to_string(), - runtime_id: "voidbot-main".to_string(), - transports: vec![CultNetTransportDescriptor { - transport_id: "direct-pipe".to_string(), - protocol: CultNetTransportProtocol::TcpFramed, - host: None, - port: None, - path: None, - discovery_group: None, - wire_contracts: Some(vec!["cultnet.schema.v0".to_string()]), - channels: vec![CultNetTransportChannel { - channel_id: "schema".to_string(), - delivery: CultNetTransportDelivery::Reliable, - ordering: CultNetTransportOrdering::Ordered, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - reliable_expire_after_ms: None, - }], - }], - }]), - supports_schema_catalog: Some(true), - }; - let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?; - let frame = encode_frame(&payload)?; - assert_eq!(&frame[..4], &(payload.len() as u32).to_be_bytes()); - - let mut framer = LengthPrefixedMessageFramer::new(); - assert!(framer.push(&frame[..2]).is_empty()); - let frames = framer.push(&frame[2..]); - assert_eq!(frames.len(), 1); - let decoded = - decode_cultnet_message_from_slice(&frames[0], CultNetWireContract::CultNetSchemaV0)?; - assert_eq!(decoded, message); - Ok(()) -} - -#[test] -fn tcp_framed_transport_carries_schema_payloads_with_stats() -> Result<()> { - let payload = b"cultnet-payload".to_vec(); - let profile = create_tcp_framed_transport_profile( - "rust-transport", - TcpFramedTransportProfileOptions { - transport_id: Some("test-tcp".to_string()), - ..TcpFramedTransportProfileOptions::default() - }, - ); - let mut sender = TcpFramedTransportConnection::new(Vec::::new(), profile.clone()); - sender.send("schema", &payload)?; - assert_eq!(sender.stats().frames_sent, 1); - assert_eq!(sender.stats().bytes_sent, (payload.len() + 4) as u64); - assert!(sender.send("unreliable", &[]).is_err()); - - let bytes = sender.into_inner(); - let mut receiver = TcpFramedTransportConnection::new(std::io::Cursor::new(bytes), profile); - let frame = receiver.receive()?; - assert_eq!(frame.channel_id, "schema"); - assert_eq!(frame.payload, payload); - assert_eq!(receiver.stats().frames_received, 1); - assert_eq!( - receiver.stats().bytes_received, - (frame.payload.len() + 4) as u64 - ); - assert_eq!( - receiver.profile.transports[0].protocol, - CultNetTransportProtocol::TcpFramed - ); - Ok(()) -} - -#[test] -fn rudp_packet_codec_uses_deterministic_reliable_ordered_fixture() -> Result<()> { - let encoded = encode_rudp_packet(&CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Data, - connection_id: 0x01020304, - sequence: 0x0000002a, - ack: 0x00000029, - ack_mask: 0x80000001, - channel_id: "schema".to_string(), - reliable: true, - ordered: true, - sequenced: false, - fragment_id: 7, - fragment_index: 1, - fragment_count: 3, - payload: b"hello".to_vec(), - })?; - - assert_eq!( - encoded, - vec![ - 67, 78, 82, 48, 0, 3, 11, 42, 1, 2, 3, 4, 0, 0, 0, 42, 0, 0, 0, 41, 128, 0, 0, 1, 0, 7, - 0, 1, 0, 3, 0, 0, 0, 5, 6, 0, 115, 99, 104, 101, 109, 97, 104, 101, 108, 108, 111, - ] - ); - - let decoded = decode_rudp_packet(&encoded)?; - assert_eq!(decoded.packet_type, CultNetRudpPacketType::Data); - assert_eq!(decoded.connection_id, 0x01020304); - assert_eq!(decoded.sequence, 0x0000002a); - assert_eq!(decoded.ack, 0x00000029); - assert_eq!(decoded.ack_mask, 0x80000001); - assert_eq!(decoded.channel_id, "schema"); - assert!(decoded.reliable); - assert!(decoded.ordered); - assert!(!decoded.sequenced); - assert_eq!(decoded.fragment_id, 7); - assert_eq!(decoded.fragment_index, 1); - assert_eq!(decoded.fragment_count, 3); - assert_eq!(decoded.payload, b"hello"); - Ok(()) -} - -#[test] -fn rudp_transport_profile_advertises_state_and_realtime_channels() { - let profile = create_rudp_transport_profile( - "rust-rudp", - cultnet_rs::RudpTransportProfileOptions { - transport_id: Some("public-rudp".to_string()), - host: Some("127.0.0.1".to_string()), - port: Some(7777), - max_payload_bytes: Some(1200), - max_fragment_bytes: Some(1000), - max_pending_reliable_packets: Some(64), - media_reliable_expire_after_ms: Some(75), - }, - ); - - assert_eq!( - profile.transports[0].protocol, - CultNetTransportProtocol::Rudp - ); - let channels = profile.transports[0] - .channels - .iter() - .map(|channel| { - ( - channel.channel_id.as_str(), - channel.delivery, - channel.ordering, - channel.max_pending_reliable_packets, - channel.reliable_expire_after_ms, - ) - }) - .collect::>(); - assert_eq!( - channels, - vec![ - ( - "schema", - CultNetTransportDelivery::Reliable, - CultNetTransportOrdering::Ordered, - Some(64), - None - ), - ( - "latest", - CultNetTransportDelivery::Unreliable, - CultNetTransportOrdering::Sequenced, - Some(64), - None - ), - ( - "realtime", - CultNetTransportDelivery::Unreliable, - CultNetTransportOrdering::Unordered, - Some(64), - None - ), - ( - "media", - CultNetTransportDelivery::Reliable, - CultNetTransportOrdering::Unordered, - Some(64), - Some(75) - ), - ] - ); -} - -#[test] -fn rudp_session_handshake_acks_reliable_connect_and_accept_packets() -> Result<()> { - let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 0x0a0b0c0d, - initial_sequence: 1, - resend_delay_ms: 50, - ..CultNetRudpSessionOptions::default() - }); - let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 0x0a0b0c0d, - initial_sequence: 100, - resend_delay_ms: 50, - ..CultNetRudpSessionOptions::default() - }); - - let connect = client.create_connect(0, b"join".to_vec())?; - assert_eq!(connect.packet_type, CultNetRudpPacketType::Connect); - assert_eq!(connect.sequence, 1); - assert_eq!(client.pending_reliable_sequences(), vec![1]); - - let accept = server.accept_connect(&connect, 10, b"ok".to_vec())?; - assert_eq!(accept.packet_type, CultNetRudpPacketType::Accept); - assert_eq!(accept.ack, 1); - assert!(server.connected()); - assert_eq!(server.pending_reliable_sequences(), vec![100]); - - client.receive(&accept, 20)?; - assert!(client.connected()); - assert!(client.pending_reliable_sequences().is_empty()); - - let ack = client.create_ack(); - assert_eq!(ack.ack, 100); - server.receive(&ack, 30)?; - assert!(server.pending_reliable_sequences().is_empty()); - Ok(()) -} - -#[test] -fn rudp_session_computes_ack_masks_and_clears_pending_reliable_packets() -> Result<()> { - let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 7, - initial_sequence: 10, - resend_delay_ms: 100, - ..CultNetRudpSessionOptions::default() - }); - let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 7, - initial_sequence: 200, - resend_delay_ms: 100, - ..CultNetRudpSessionOptions::default() - }); - sender.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 7, - sequence: 1, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - receiver.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 7, - sequence: 2, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - - let options = CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }; - let first = sender.send("schema", b"first".to_vec(), options.clone())?; - let second = sender.send("schema", b"second".to_vec(), options.clone())?; - let third = sender.send("schema", b"third".to_vec(), options)?; - assert_eq!(sender.pending_reliable_sequences(), vec![10, 11, 12]); - - receiver.receive(&first, 0)?; - receiver.receive(&third, 0)?; - let ack_with_gap = receiver.create_ack(); - assert_eq!(ack_with_gap.ack, 12); - assert_eq!(ack_with_gap.ack_mask, 0b10 | (1 << 9)); - sender.receive(&ack_with_gap, 0)?; - assert_eq!(sender.pending_reliable_sequences(), vec![11]); - - receiver.receive(&second, 0)?; - let full_ack = receiver.create_ack(); - assert_eq!(full_ack.ack, 12); - assert_eq!(full_ack.ack_mask, 0b11 | (1 << 9)); - sender.receive(&full_ack, 0)?; - assert!(sender.pending_reliable_sequences().is_empty()); - Ok(()) -} - -#[test] -fn rudp_session_schedules_reliable_resends_until_acked() -> Result<()> { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 99, - initial_sequence: 1, - resend_delay_ms: 100, - ..CultNetRudpSessionOptions::default() - }); - session.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 99, - sequence: 50, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - let sent = session.send( - "schema", - b"payload".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - now_ms: 10, - ..CultNetRudpSendOptions::default() - }, - )?; - - assert!(session.due_resends(90).is_empty()); - assert_eq!( - session - .due_resends(110) - .iter() - .map(|packet| packet.sequence) - .collect::>(), - vec![sent.sequence] - ); - assert!(session.due_resends(150).is_empty()); - - session.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Ack, - connection_id: 99, - sequence: 51, - ack: sent.sequence, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - assert!(session.due_resends(250).is_empty()); - Ok(()) -} - -#[test] -fn rudp_session_expires_bounded_reliable_media_resends() -> Result<()> { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 100, - initial_sequence: 1, - resend_delay_ms: 20, - media_reliable_expire_after_ms: Some(50), - max_pending_reliable_packets: Some(1), - }); - session.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 100, - sequence: 50, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - let sent = session.send( - "media", - b"frame-chunk".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: false, - now_ms: 10, - reliable_expire_after_ms: Some(50), - ..CultNetRudpSendOptions::default() - }, - )?; - - assert_eq!(session.pending_reliable_sequences(), vec![sent.sequence]); - assert!(session.due_resends(25).is_empty()); - assert_eq!( - session - .due_resends(35) - .iter() - .map(|packet| packet.sequence) - .collect::>(), - vec![sent.sequence] - ); - assert!(session.due_resends(61).is_empty()); - assert!(session.pending_reliable_sequences().is_empty()); - assert_eq!(session.reliable_packets_expired(), 1); - - session.send( - "media", - b"fresh-frame-chunk".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: false, - now_ms: 62, - reliable_expire_after_ms: Some(50), - ..CultNetRudpSendOptions::default() - }, - )?; - assert_eq!(session.reliable_packets_expired(), 1); - Ok(()) -} - -#[test] -fn rudp_session_pings_and_detects_receive_timeout() -> Result<()> { - let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 101, - initial_sequence: 1, - ..CultNetRudpSessionOptions::default() - }); - let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 101, - initial_sequence: 100, - ..CultNetRudpSessionOptions::default() - }); - let connect = client.create_connect(0, b"join".to_vec())?; - let accept = server.accept_connect(&connect, 10, Vec::new())?; - client.receive(&accept, 20)?; - - let ping = client.create_ping(b"pulse".to_vec()); - let ping_result = server.receive(&ping, 30)?; - let pong = ping_result.reply.expect("ping should produce pong"); - assert_eq!(pong.packet_type, CultNetRudpPacketType::Pong); - assert_eq!(pong.payload, b"pulse"); - - let pong_result = client.receive(&pong, 40)?; - assert!(pong_result.pong); - assert_eq!(pong_result.pong_payload, b"pulse"); - assert!(!client.check_timeout(90, 50)); - assert!(client.check_timeout(91, 50)); - assert!(!client.connected()); - Ok(()) -} - -#[test] -fn rudp_session_bounds_pending_reliable_packets_before_enqueue() -> Result<()> { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 102, - initial_sequence: 1, - max_pending_reliable_packets: Some(2), - ..CultNetRudpSessionOptions::default() - }); - session.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 102, - sequence: 50, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - session.send( - "schema", - b"first".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }, - )?; - session.send( - "schema", - b"second".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }, - )?; - let error = session - .send( - "schema", - b"third".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }, - ) - .unwrap_err(); - assert!(error.to_string().contains("reliable send queue is full")); - assert_eq!(session.pending_reliable_sequences(), vec![1, 2]); - - let mut fragmented = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 103, - initial_sequence: 1, - max_pending_reliable_packets: Some(3), - ..CultNetRudpSessionOptions::default() - }); - fragmented.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 103, - sequence: 50, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - let error = fragmented - .send_many( - "schema", - b"fragment-me".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }, - Some(3), - ) - .unwrap_err(); - assert!(error.to_string().contains("reliable send queue is full")); - assert!(fragmented.pending_reliable_sequences().is_empty()); - Ok(()) -} - -#[test] -fn rudp_session_suppresses_duplicates_and_delivers_reliable_ordered_payloads_in_sequence() --> Result<()> { - let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 123, - initial_sequence: 1, - ..CultNetRudpSessionOptions::default() - }); - let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 123, - initial_sequence: 100, - ..CultNetRudpSessionOptions::default() - }); - sender.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 123, - sequence: 90, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - receiver.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 123, - sequence: 91, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - - let options = CultNetRudpSendOptions { - reliable: true, - ordered: true, - ..CultNetRudpSendOptions::default() - }; - let first = sender.send("schema", b"first".to_vec(), options.clone())?; - let second = sender.send("schema", b"second".to_vec(), options.clone())?; - let third = sender.send("schema", b"third".to_vec(), options)?; - - assert_eq!( - receiver - .receive(&first, 0)? - .delivered - .iter() - .map(|frame| String::from_utf8(frame.payload.clone()).unwrap()) - .collect::>(), - vec!["first"] - ); - assert!(receiver.receive(&third, 0)?.delivered.is_empty()); - assert!(receiver.receive(&first, 0)?.delivered.is_empty()); - assert_eq!( - receiver - .receive(&second, 0)? - .delivered - .iter() - .map(|frame| String::from_utf8(frame.payload.clone()).unwrap()) - .collect::>(), - vec!["second", "third"] - ); - Ok(()) -} - -#[test] -fn rudp_session_fragments_and_reassembles_reliable_ordered_payloads() -> Result<()> { - let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 456, - initial_sequence: 1, - ..CultNetRudpSessionOptions::default() - }); - let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: 456, - initial_sequence: 100, - ..CultNetRudpSessionOptions::default() - }); - sender.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 456, - sequence: 90, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - receiver.receive( - &CultNetRudpPacket { - packet_type: CultNetRudpPacketType::Accept, - connection_id: 456, - sequence: 91, - ack: 0, - ack_mask: 0, - channel_id: "control".to_string(), - reliable: false, - ordered: false, - sequenced: false, - fragment_id: 0, - fragment_index: 0, - fragment_count: 0, - payload: Vec::new(), - }, - 0, - )?; - - let packets = sender.send_many( - "schema", - b"fragment-me-please".to_vec(), - CultNetRudpSendOptions { - reliable: true, - ordered: true, - now_ms: 10, - ..CultNetRudpSendOptions::default() - }, - Some(5), - )?; - assert_eq!(packets.len(), 4); - assert_eq!( - packets - .iter() - .map(|packet| packet.fragment_count) - .collect::>(), - vec![4, 4, 4, 4] - ); - assert_eq!( - packets - .iter() - .map(|packet| packet.fragment_index) - .collect::>(), - vec![0, 1, 2, 3] - ); - assert!( - packets - .iter() - .all(|packet| packet.fragment_id == packets[0].fragment_id) - ); - - assert!(receiver.receive(&packets[0], 0)?.delivered.is_empty()); - assert!(receiver.receive(&packets[1], 0)?.delivered.is_empty()); - assert!(receiver.receive(&packets[2], 0)?.delivered.is_empty()); - let delivered = receiver.receive(&packets[3], 0)?.delivered; - assert_eq!(delivered.len(), 1); - assert_eq!(delivered[0].payload, b"fragment-me-please"); - assert_eq!(delivered[0].sequence, packets[0].sequence); - Ok(()) -} - -#[test] -fn rudp_socket_transport_handshakes_and_carries_reliable_ordered_schema_frames() -> Result<()> { - let server_socket = bind_udp_socket()?; - let client_socket = bind_udp_socket()?; - let server_addr = server_socket.local_addr()?; - let connection_id = 0x1020_3040; - let mut server = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-server".to_string(), - socket: server_socket, - mode: CultNetRudpSocketMode::Server, - remote_addr: None, - connection_id, - initial_sequence: 100, - resend_delay_ms: 25, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(75), - })?; - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-client".to_string(), - socket: client_socket, - mode: CultNetRudpSocketMode::Client, - remote_addr: Some(server_addr), - connection_id, - initial_sequence: 1, - resend_delay_ms: 25, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(75), - })?; - - client.connect(b"join".to_vec())?; - pump_rudp_handshake(&mut client, &mut server)?; - assert!(client.connected()); - assert!(server.connected()); - - client.send("schema", b"client-state".to_vec())?; - let server_frame = receive_rudp_frame(&mut server)?; - assert_eq!(server_frame.channel_id, "schema"); - assert_eq!(server_frame.payload, b"client-state"); - - server.send("schema", b"server-state".to_vec())?; - let client_frame = receive_rudp_frame(&mut client)?; - assert_eq!(client_frame.channel_id, "schema"); - assert_eq!(client_frame.payload, b"server-state"); - assert_eq!( - server.profile.transports[0].protocol, - CultNetTransportProtocol::Rudp - ); - assert_eq!(client.stats().frames_sent, 1); - assert_eq!(server.stats().frames_received, 1); - - Ok(()) -} - -#[test] -fn rudp_socket_transport_reports_expired_reliable_media_packets() -> Result<()> { - let server_socket = bind_udp_socket()?; - let client_socket = bind_udp_socket()?; - let server_addr = server_socket.local_addr()?; - let connection_id = 0x1020_3042; - let mut server = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-media-expiry-server".to_string(), - socket: server_socket, - mode: CultNetRudpSocketMode::Server, - remote_addr: None, - connection_id, - initial_sequence: 100, - resend_delay_ms: 5, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(25), - })?; - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-media-expiry-client".to_string(), - socket: client_socket, - mode: CultNetRudpSocketMode::Client, - remote_addr: Some(server_addr), - connection_id, - initial_sequence: 1, - resend_delay_ms: 5, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: None, - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(25), - })?; - - client.connect(b"join".to_vec())?; - pump_rudp_handshake(&mut client, &mut server)?; - client.send("media", b"late-frame".to_vec())?; - assert_eq!(client.stats().reliable_packets_expired, 0); - - thread::sleep(StdDuration::from_millis(40)); - client.poll_resends()?; - - assert_eq!(client.stats().reliable_packets_expired, 1); - Ok(()) -} - -#[test] -fn rudp_socket_transport_carries_fragmented_reliable_ordered_schema_frames() -> Result<()> { - let server_socket = bind_udp_socket()?; - let client_socket = bind_udp_socket()?; - let server_addr = server_socket.local_addr()?; - let connection_id = 0x1020_3041; - let mut server = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-fragment-server".to_string(), - socket: server_socket, - mode: CultNetRudpSocketMode::Server, - remote_addr: None, - connection_id, - initial_sequence: 100, - resend_delay_ms: 25, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: Some(8), - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(75), - })?; - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "rust-rudp-fragment-client".to_string(), - socket: client_socket, - mode: CultNetRudpSocketMode::Client, - remote_addr: Some(server_addr), - connection_id, - initial_sequence: 1, - resend_delay_ms: 25, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: Some(8), - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: Some(75), - })?; - - client.connect(b"join".to_vec())?; - pump_rudp_handshake(&mut client, &mut server)?; - - let payload = b"this-schema-frame-is-larger-than-one-rudp-fragment".to_vec(); - client.send("schema", payload.clone())?; - let server_frame = receive_rudp_frame(&mut server)?; - assert_eq!(server_frame.channel_id, "schema"); - assert_eq!(server_frame.payload, payload); - assert_eq!(client.stats().frames_sent, 1); - assert_eq!(server.stats().frames_received, 1); - Ok(()) -} - -#[test] -fn legacy_gamecult_networking_contract_round_trips_login_union() -> Result<()> { - let message = CultNetMessage::Login { - nonce: CultNetSecret::to_base64_url(b"nonce"), - auth: CultNetSecret::to_base64_url(b"auth"), - password: CultNetSecret::to_base64_url(b"password"), - }; - let wire = - encode_cultnet_message_for_wire(&message, CultNetWireContract::GameCultNetworkingV0)?; - let items = wire.as_array().expect("legacy union is an array"); - assert_eq!(items[0].as_i64(), Some(0)); - assert_eq!( - items[1].as_array().unwrap()[0].as_slice(), - Some(&b"nonce"[..]) - ); - - let bytes = rmp_serde::to_vec(&wire)?; - let decoded = - decode_cultnet_message_from_slice(&bytes, CultNetWireContract::GameCultNetworkingV0)?; - assert_eq!(decoded, message); - Ok(()) -} - -#[test] -fn rust_decodes_typescript_generated_cultnet_frames() -> Result<()> { - let mut framer = LengthPrefixedMessageFramer::new(); - let hello_frames = framer.push(TS_HELLO_FRAME); - assert_eq!(hello_frames.len(), 1); - let hello = - decode_cultnet_message_from_slice(&hello_frames[0], CultNetWireContract::CultNetSchemaV0)?; - assert_eq!( - hello, - CultNetMessage::Hello { - runtime_id: "voidbot-main".to_string(), - runtime_kind: "node-worker".to_string(), - agent_id: Some("void".to_string()), - role: None, - display_name: Some("Void".to_string()), - supported_document_types: Some(vec!["ghostlight.agent-state".to_string()]), - supported_mutation_contracts: None, - supported_message_versions: None, - transport_profiles: None, - supports_schema_catalog: None, - } - ); - - let mut framer = LengthPrefixedMessageFramer::new(); - let login_frames = framer.push(TS_LEGACY_LOGIN_FRAME); - assert_eq!(login_frames.len(), 1); - let login = decode_cultnet_message_from_slice( - &login_frames[0], - CultNetWireContract::GameCultNetworkingV0, - )?; - assert_eq!( - login, - CultNetMessage::Login { - nonce: "bm9uY2U".to_string(), - auth: "YXV0aA".to_string(), - password: "cGFzc3dvcmQ".to_string(), - } - ); - Ok(()) -} - -#[test] -fn document_registry_advertises_mutation_contracts_with_bindings() -> Result<()> { - let mut registry = CultNetDocumentRegistry::new(); - let contract = CultNetDocumentMutationContract { - document_type: "ghostlight.agent-state".to_string(), - payload_schema_version: Some("ghostlight.agent_state.v0".to_string()), - operations: vec![ - CultNetDocumentOperation::Snapshot, - CultNetDocumentOperation::IntentSubmit, - CultNetDocumentOperation::ReceiptWatch, - ], - authority: CultNetMutationAuthority::Coordinator, - intent_document_types: Some(vec!["ghostlight.agent-state.intent.v0".to_string()]), - receipt_document_types: Some(vec!["ghostlight.agent-state.receipt.v0".to_string()]), - notes: Some(vec![ - "Intent documents are reviewed before cache mutation.".to_string(), - ]), - }; - registry.register( - CultNetDocumentBinding::for_entry::( - "ghostlight.agent_state.v0".to_string(), - ) - .with_mutation_contract(contract.clone()), - ); - - assert_eq!(registry.mutation_contracts(), vec![contract]); - Ok(()) -} - -#[test] -fn document_registry_replicates_typed_cultcache_state() -> Result<()> { - let temp = tempfile::tempdir()?; - let origin_store = temp.path().join("origin.msgpack"); - let target_store = temp.path().join("target.msgpack"); - let payload = GhostlightAgentStateFixture { - schema_version: "ghostlight.agent_state.v0".to_string(), - agent_id: "epiphany.persona".to_string(), - display_name: "Persona".to_string(), - }; - - let mut registry = CultNetDocumentRegistry::new(); - registry.register(CultNetDocumentBinding::for_entry::< - GhostlightAgentStateFixture, - >("ghostlight.agent_state.v0".to_string())); - - let mut origin = CultCache::new(); - origin.register_entry_type::()?; - origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store)); - origin.pull_all_backing_stores()?; - origin.put("epiphany.persona", &payload)?; - - let snapshot = registry.create_snapshot_response(&origin, "snapshot-1", None, None)?; - - let mut target = CultCache::new(); - target.register_entry_type::()?; - target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store)); - target.pull_all_backing_stores()?; - let applied = - registry.apply_snapshot_response::(&mut target, &snapshot)?; - assert_eq!(applied, vec![payload.clone()]); - assert_eq!( - target.get_required::("epiphany.persona")?, - payload - ); - - let direct_put = registry.create_document_put_message( - "put-1", - "epiphany.persona", - &GhostlightAgentStateFixture { - display_name: "Persona Prime".to_string(), - ..payload - }, - CultNetDocumentPutOptions::default(), - )?; - let updated = registry - .apply_document_put_message::(&mut target, &direct_put)?; - assert_eq!(updated.display_name, "Persona Prime"); - Ok(()) -} - -#[test] -fn raw_snapshot_replication_preserves_messagepack_payload_bytes() -> Result<()> { - let temp = tempfile::tempdir()?; - let origin_store = temp.path().join("origin-raw.msgpack"); - let target_store = temp.path().join("target-raw.msgpack"); - let payload = GhostlightAgentStateFixture { - schema_version: "ghostlight.agent_state.v0".to_string(), - agent_id: "epiphany.persona".to_string(), - display_name: "Persona".to_string(), - }; - - let mut registry = CultNetDocumentRegistry::new(); - registry.register(CultNetDocumentBinding::for_entry::< - GhostlightAgentStateFixture, - >("ghostlight.agent_state.v0".to_string())); - - let mut origin = CultCache::new(); - origin.register_entry_type::()?; - origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store)); - origin.pull_all_backing_stores()?; - origin.put("epiphany.persona", &payload)?; - - let raw_snapshot = - registry.create_raw_snapshot_response(&origin, "raw-snapshot-1", None, None)?; - let source_envelope = - origin.get_required_envelope::("epiphany.persona")?; - - let mut target = CultCache::new(); - target.register_entry_type::()?; - target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store)); - target.pull_all_backing_stores()?; - let applied = registry - .apply_raw_snapshot_response::(&mut target, &raw_snapshot)?; - let target_envelope = - target.get_required_envelope::("epiphany.persona")?; - - assert_eq!(applied, vec![payload.clone()]); - assert_eq!(target_envelope.payload, source_envelope.payload); - assert_eq!( - target.get_required::("epiphany.persona")?, - payload - ); - Ok(()) -} - -#[test] -fn client_security_keeps_the_connection_key_visible_without_exposing_cipher_logic() -> Result<()> { - let options = CultNetClientSecurityOptions::development(); - assert_eq!(options.connection_key, "gamecult-dev-connection-key"); - assert_ne!(options.encryption_key(), [0_u8; 32]); - Ok(()) -} - -#[test] -fn builtin_schema_registry_advertises_canonical_ghostlight_schema_without_inline_body_by_default() --> Result<()> { - let registry = builtin_schema_registry()?; - let response = registry.create_catalog_response(&CultNetMessage::SchemaCatalogRequest { - message_id: "catalog-1".to_string(), - include_schema_json: None, - schema_ids: None, - kinds: None, - })?; - - let CultNetMessage::SchemaCatalogResponse { schemas, .. } = response else { - panic!("expected catalog response"); - }; - - let ghostlight = schemas - .iter() - .find(|schema| schema.document_type.as_deref() == Some("ghostlight.agent-state")) - .expect("ghostlight agent-state schema is advertised"); - - assert_eq!(ghostlight.kind, CultNetSchemaKind::DocumentPayload); - assert_eq!( - ghostlight.schema_version.as_deref(), - Some("ghostlight.agent_state.v0") - ); - assert_eq!(ghostlight.schema_json, None); - assert!(!ghostlight.content_hash.is_empty()); - - let transport_profile = schemas - .iter() - .find(|schema| schema.schema_version.as_deref() == Some("cultnet.transport_profile.v0")) - .expect("transport profile schema is advertised"); - - assert_eq!(transport_profile.kind, CultNetSchemaKind::SharedContract); - assert_eq!( - transport_profile.schema_id, - "https://github.com/GameCult/cultnet-ts/contracts/cultnet.transport-profile.schema.json" - ); - assert_eq!( - transport_profile.wire_contracts, - vec![CultNetWireContract::CultNetSchemaV0] - ); - Ok(()) -} - -#[test] -fn schema_discovery_round_trips_over_legacy_gamecult_contract_when_inline_schemas_are_requested() --> Result<()> { - let registry = { - let mut registry = CultNetSchemaRegistry::new(); - registry.register(cultnet_rs::CultNetSchemaRegistration { - schema_id: "https://example.test/contracts/example.schema.json".to_string(), - kind: CultNetSchemaKind::SharedContract, - wire_contracts: vec![ - CultNetWireContract::CultNetSchemaV0, - CultNetWireContract::GameCultNetworkingV0, - ], - schema_version: None, - document_type: None, - title: Some("Example Schema".to_string()), - schema_json: Some( - r#"{ - "$schema":"https://json-schema.org/draft/2020-12/schema", - "$id":"https://example.test/contracts/example.schema.json", - "title":"Example Schema", - "type":"object", - "properties":{"value":{"type":"string"}}, - "required":["value"], - "additionalProperties":false - }"# - .to_string(), - ), - })?; - registry - }; - - let response = registry.create_catalog_response(&CultNetMessage::SchemaCatalogRequest { - message_id: "catalog-legacy".to_string(), - include_schema_json: Some(true), - schema_ids: None, - kinds: None, - })?; - let wire = - encode_cultnet_message_for_wire(&response, CultNetWireContract::GameCultNetworkingV0)?; - let bytes = rmp_serde::to_vec(&wire)?; - let decoded = - decode_cultnet_message_from_slice(&bytes, CultNetWireContract::GameCultNetworkingV0)?; - - let CultNetMessage::SchemaCatalogResponse { - message_id, - schemas, - } = decoded - else { - panic!("expected legacy schema catalog response"); - }; - - assert_eq!(message_id, "catalog-legacy"); - assert_eq!( - schemas[0].schema_id, - "https://example.test/contracts/example.schema.json" - ); - assert!( - schemas[0] - .schema_json - .as_deref() - .is_some_and(|schema| schema.contains("\"value\"")) - ); - Ok(()) -} diff --git a/vendor/cultnet-rs/tests/fixtures/cultnet-ts-hello.frame b/vendor/cultnet-rs/tests/fixtures/cultnet-ts-hello.frame deleted file mode 100644 index e2b22d50048ca9f140927ecaa1a9f5ec70affe1d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 160 zcmXZUu?oU45P;#nP>T=YQUoXI9-E^%OzuMNQt1o0yE_&{qTtl$vx@7_$6r;|?edb| zK`9voKBGLW&qOG&R<+C oO7v#hn*ejShDCCWoD}Sq`y5tzm?Fq^861mx9Go`4K`m+&KGeNK7ytkO diff --git a/vendor/cultnet-rs/tests/fixtures/cultnet-ts-legacy-login.frame b/vendor/cultnet-rs/tests/fixtures/cultnet-ts-legacy-login.frame deleted file mode 100644 index c6f335d37783e6ab66bf1cdfa02137995209a860..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 30 lcmZQzV33-`F!=~;UVdJ3>JgU2(vplL90iHR#pU@$DFBis3e^Ar diff --git a/vendor/cultnet-rs/tests/snapshot_query.rs b/vendor/cultnet-rs/tests/snapshot_query.rs deleted file mode 100644 index ac9c6f9..0000000 --- a/vendor/cultnet-rs/tests/snapshot_query.rs +++ /dev/null @@ -1,222 +0,0 @@ -use anyhow::Result; -use cultnet_rs::{ - CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, - CultNetRawPayloadEncoding, CultNetRawSnapshotQuery, CultNetReadOnlySnapshotPolicy, - CultNetSnapshotRecordExpectation, CultNetSnapshotSourceExpectation, CultNetWireContract, - decode_cultnet_message_from_slice, encode_cultnet_message_to_vec, query_read_only_raw_snapshot, - serve_read_only_raw_snapshot, -}; - -const HEALTH_SCHEMA: &str = "idunn.managed_health_projection.v1"; - -fn registry() -> CultNetDocumentRegistry { - let mut registry = CultNetDocumentRegistry::new(); - registry.register(CultNetDocumentBinding { - schema_id: HEALTH_SCHEMA.to_string(), - document_type: "idunn.managed-health-projection".to_string(), - mutation_contract: None, - payload_schema_version: Some(HEALTH_SCHEMA.to_string()), - }); - registry -} - -fn record(key: &str, payload: Vec) -> CultNetRawDocumentRecord { - CultNetRawDocumentRecord { - schema_id: HEALTH_SCHEMA.to_string(), - record_key: key.to_string(), - stored_at: "2026-07-19T20:00:00Z".to_string(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload, - source_runtime_id: Some("idunn@yggdrasil".to_string()), - source_agent_id: Some("idunn".to_string()), - source_role: Some("deployment-owner".to_string()), - tags: Some(vec!["public-health".to_string()]), - } -} - -fn expectation(key: &str) -> CultNetSnapshotRecordExpectation { - CultNetSnapshotRecordExpectation { - schema_id: HEALTH_SCHEMA.to_string(), - record_key: key.to_string(), - source: CultNetSnapshotSourceExpectation { - runtime_id: Some("idunn@yggdrasil".to_string()), - agent_id: Some("idunn".to_string()), - role: Some("deployment-owner".to_string()), - tags: Some(vec!["public-health".to_string()]), - }, - } -} - -#[test] -fn canonical_wire_loop_returns_exact_stored_raw_record_without_cache_apply() -> Result<()> { - let stored = record("epiphany-agent", vec![0x95, 0xc4, 0x02, 0xde, 0xad]); - let source = vec![stored.clone(), record("not-public", vec![0x01])]; - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; - let query = CultNetRawSnapshotQuery::new("status-42", vec![expectation("epiphany-agent")])?; - - let received = query_read_only_raw_snapshot(&query, |request| { - // This is the canonical transport loop: request and response both cross - // the CultNet wire codec. Neither side receives a cache to mutate. - let request_wire = - encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; - let decoded_request = - decode_cultnet_message_from_slice(&request_wire, CultNetWireContract::CultNetSchemaV0)?; - let response = - serve_read_only_raw_snapshot(®istry(), &policy, &source, &decoded_request)?; - let response_wire = - encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; - decode_cultnet_message_from_slice(&response_wire, CultNetWireContract::CultNetSchemaV0) - })?; - - assert_eq!(received, vec![stored]); - Ok(()) -} - -#[test] -fn server_filters_by_exact_policy_pair_and_request() -> Result<()> { - let source = vec![record("epiphany-agent", vec![1]), record("odin", vec![2])]; - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; - let request = CultNetMessage::SnapshotRequest { - message_id: "bounded".to_string(), - schema_ids: Some(vec![HEALTH_SCHEMA.to_string()]), - record_keys: Some(vec!["epiphany-agent".to_string(), "odin".to_string()]), - }; - - let response = serve_read_only_raw_snapshot(®istry(), &policy, &source, &request)?; - let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { - panic!("expected raw response"); - }; - assert_eq!(documents, vec![source[0].clone()]); - Ok(()) -} - -#[test] -fn server_rejects_duplicate_request_terms_and_ambiguous_backing_records() -> Result<()> { - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - policy.allow(HEALTH_SCHEMA, "epiphany-agent")?; - let duplicate_request = CultNetMessage::SnapshotRequest { - message_id: "duplicate-request".to_string(), - schema_ids: Some(vec![HEALTH_SCHEMA.to_string(), HEALTH_SCHEMA.to_string()]), - record_keys: Some(vec!["epiphany-agent".to_string()]), - }; - let error = serve_read_only_raw_snapshot( - ®istry(), - &policy, - &vec![record("epiphany-agent", vec![1])], - &duplicate_request, - ) - .unwrap_err(); - assert!(error.to_string().contains("duplicate requested schema id")); - - let request = CultNetMessage::SnapshotRequest { - message_id: "duplicate-source".to_string(), - schema_ids: None, - record_keys: None, - }; - let duplicate = record("epiphany-agent", vec![1]); - let error = serve_read_only_raw_snapshot( - ®istry(), - &policy, - &vec![duplicate.clone(), duplicate], - &request, - ) - .unwrap_err(); - assert!(error.to_string().contains("duplicate record")); - Ok(()) -} - -#[test] -fn query_rejects_duplicate_expectations_wrong_message_and_unrelated_message() -> Result<()> { - let error = CultNetRawSnapshotQuery::new( - "duplicate", - vec![expectation("epiphany-agent"), expectation("epiphany-agent")], - ) - .unwrap_err(); - assert!(error.to_string().contains("duplicate requested record")); - - let query = CultNetRawSnapshotQuery::new("wanted", vec![expectation("epiphany-agent")])?; - let error = query - .accept_response(CultNetMessage::SnapshotResponseRaw { - message_id: "somebody-elses-response".to_string(), - documents: vec![], - }) - .unwrap_err(); - assert!(error.to_string().contains("does not match request")); - - let error = query - .accept_response(CultNetMessage::Error { - error: "unrelated".to_string(), - }) - .unwrap_err(); - assert!( - error - .to_string() - .contains("expected cultnet.snapshot_response_raw.v0") - ); - Ok(()) -} - -#[test] -fn query_rejects_unexpected_identity_duplicates_and_source_metadata() -> Result<()> { - let query = CultNetRawSnapshotQuery::new("hostile", vec![expectation("epiphany-agent")])?; - - let error = query - .accept_response(CultNetMessage::SnapshotResponseRaw { - message_id: "hostile".to_string(), - documents: vec![record("odin", vec![1])], - }) - .unwrap_err(); - assert!(error.to_string().contains("unexpected record")); - - let document = record("epiphany-agent", vec![1]); - let error = query - .accept_response(CultNetMessage::SnapshotResponseRaw { - message_id: "hostile".to_string(), - documents: vec![document.clone(), document], - }) - .unwrap_err(); - assert!(error.to_string().contains("duplicate record")); - - let mut forged = record("epiphany-agent", vec![1]); - forged.source_runtime_id = Some("mallory".to_string()); - let error = query - .accept_response(CultNetMessage::SnapshotResponseRaw { - message_id: "hostile".to_string(), - documents: vec![forged], - }) - .unwrap_err(); - assert!(error.to_string().contains("unexpected source_runtime_id")); - - let mut forged = record("epiphany-agent", vec![1]); - forged.tags = Some(vec!["private".to_string()]); - let error = query - .accept_response(CultNetMessage::SnapshotResponseRaw { - message_id: "hostile".to_string(), - documents: vec![forged], - }) - .unwrap_err(); - assert!(error.to_string().contains("unexpected tags")); - Ok(()) -} - -#[test] -fn server_rejects_unregistered_source_schema_even_when_not_exposed() -> Result<()> { - let mut foreign = record("foreign", vec![1]); - foreign.schema_id = "xenos.unregistered.v0".to_string(); - let request = CultNetMessage::SnapshotRequest { - message_id: "unregistered".to_string(), - schema_ids: None, - record_keys: None, - }; - let error = serve_read_only_raw_snapshot( - ®istry(), - &CultNetReadOnlySnapshotPolicy::new(), - &vec![foreign], - &request, - ) - .unwrap_err(); - assert!(error.to_string().contains("unregistered schema")); - Ok(()) -} From a729b306439355b95dd6965efdb59d230b5f2f36 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 22:00:46 +0100 Subject: [PATCH 23/93] Add read-only Idunn public health query listener --- crates/idunn-daemon/src/main.rs | 835 ++++++++++++++++--- docs/idunn-signed-daemon-health-authority.md | 31 +- 2 files changed, 754 insertions(+), 112 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index e1b9d8b..53877a1 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5,14 +5,15 @@ use cultcache_rs::{ }; use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions}; use cultnet_rs::{ - CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpPacketType, CultNetRudpSession, - CultNetRudpSessionOptions, CultNetRudpSocketTransportConnection, - CultNetRudpSocketTransportOptions, CultNetWireContract, + CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, + CultNetRawPayloadEncoding, CultNetReadOnlySnapshotPolicy, CultNetRudpPacketType, + CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, + CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, GameCultProviderHealthIdentity, IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnServiceIdentity, IdunnSignedDaemonHealthPurpose, ServiceIdentitySignature, - ServiceIdentitySigner, - decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, - derive_service_identity_id, encode_rudp_packet, open_service_identity_at, + ServiceIdentitySigner, decode_cultnet_message_from_slice, decode_rudp_packet, + derive_service_identity_id, encode_cultnet_message_to_vec, encode_rudp_packet, + open_service_identity_at, serve_read_only_raw_snapshot, verify_service_identity_signature_with_public_key, }; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; @@ -34,7 +35,7 @@ use odin_core::{ authenticated_provider_health_reason_code, plan_keepalive, }; use sha2::{Digest, Sha256}; -use std::collections::HashMap; +use std::collections::{BTreeSet, HashMap}; use std::env; use std::fs::{self, File}; use std::net::{SocketAddr, UdpSocket}; @@ -48,6 +49,9 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0"; const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001; +const IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID: u32 = 0x1d0d_0002; +const IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID: &str = "idunn-daemon"; +const IDUNN_PUBLIC_HEALTH_QUERY_ROLE: &str = "authenticated-provider-health-projector"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE: &str = "epiphany.idunn_signed_runtime_health"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = "epiphany.idunn_signed_runtime_health.v0"; @@ -63,7 +67,10 @@ struct TargetActuationGate { impl TargetActuationGate { fn new() -> Self { - Self { lock: Mutex::new(()), reserved: AtomicBool::new(false) } + Self { + lock: Mutex::new(()), + reserved: AtomicBool::new(false), + } } } const HOST_IDENTITY_TYPE: &str = "epiphany.host_identity_trust_anchor.v0"; @@ -503,6 +510,7 @@ struct CommonOptions { daemon_health_trust_store_path: Option, service_identity_store_path: Option, public_health_store_path: Option, + public_health_query_bind: Option, execute: bool, command_timeout_seconds: u64, } @@ -724,6 +732,7 @@ fn run_swarm( ); } publish_runtime_transport_check(common, &store_lock, &now)?; + start_public_health_query_listener(common, &targets)?; start_rudp_health_ingress(common, &store_lock, &now)?; publish_surgery_plans(&options.profile, &targets, common, &store_lock, &now)?; @@ -733,7 +742,12 @@ fn run_swarm( let actuation_gates = Arc::new( targets .iter() - .map(|target| (target.daemon_id.clone(), Arc::new(TargetActuationGate::new()))) + .map(|target| { + ( + target.daemon_id.clone(), + Arc::new(TargetActuationGate::new()), + ) + }) .collect::>(), ); @@ -972,7 +986,12 @@ fn run_target_cycle( if plan.deployment_request.is_some() || plan.restart_request.is_some() { schedule_automatic_actuation( - target.clone(), options.clone(), Arc::clone(store_lock), actuation_gate, plan, now, + target.clone(), + options.clone(), + Arc::clone(store_lock), + actuation_gate, + plan, + now, ); return Ok(()); } @@ -999,14 +1018,21 @@ fn schedule_target_actuation( daemon_id: String, action: impl FnOnce() -> Result<()> + Send + 'static, ) { - if gate.reserved.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire).is_err() { + if gate + .reserved + .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) + .is_err() + { return; } thread::spawn(move || { let result = with_target_actuation_gate(&gate.lock, action); gate.reserved.store(false, Ordering::Release); if let Err(error) = result { - eprintln!("Idunn automatic actuation failed for {}: {}", daemon_id, error); + eprintln!( + "Idunn automatic actuation failed for {}: {}", + daemon_id, error + ); } }); } @@ -1018,7 +1044,6 @@ fn finish_target_cycle( plan: IdunnPlan, now: &str, ) -> Result<()> { - if let Some(request) = &plan.deployment_request { persist_current_deployment_request(options, store_lock, request)?; } @@ -1388,10 +1413,8 @@ fn persist_current_deployment_request( current_head_request_envelope, current_head_result_envelope, current_request_envelope, - ) = - with_store_node(options, store_lock, |node| { - let head = - node.get::(&request.daemon_id)?; + ) = with_store_node(options, store_lock, |node| { + let head = node.get::(&request.daemon_id)?; Ok(( head.clone(), node.cache() @@ -1405,7 +1428,8 @@ fn persist_current_deployment_request( .flatten(), head.as_ref() .map(|head| { - node.cache().get_envelope::(&format!( + node.cache() + .get_envelope::(&format!( "result:{}", head.request_id )) @@ -1454,7 +1478,10 @@ fn persist_current_deployment_request( if prior_result.result_id != format!("result:{}", current.request_id) || prior_result.request_id != current.request_id || prior_result.daemon_id != current.daemon_id - || !matches!(prior_result.state.as_str(), "succeeded" | "failed" | "skipped") + || !matches!( + prior_result.state.as_str(), + "succeeded" | "failed" | "skipped" + ) { return Err(anyhow!( "current deployment request lacks an exact terminal result" @@ -1542,8 +1569,7 @@ fn terminalize_interrupted_deployment_requests( let request_matches = opening .iter() .filter(|entry| { - entry.r#type == IdunnDeploymentRequestRecord::TYPE - && entry.key == head.request_id + entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == head.request_id }) .collect::>(); if request_matches.len() != 1 { @@ -2171,6 +2197,98 @@ struct IdunnProjectionPublisher { write_lock: Mutex<()>, } +#[derive(Clone)] +struct IdunnPublicHealthSnapshotServer { + public_store_path: PathBuf, + allowed_record_keys: BTreeSet, +} + +impl IdunnPublicHealthSnapshotServer { + fn new(public_store_path: PathBuf, targets: &[DaemonTarget]) -> Result { + let allowed_record_keys = targets + .iter() + .map(|target| { + provider_health_projection_key(&target.daemon_id, &target.health_contract.id) + }) + .collect::>(); + if allowed_record_keys.len() != targets.len() { + return Err(anyhow!( + "public health query target catalog contains a duplicate daemon/contract pair" + )); + } + let server = Self { + public_store_path, + allowed_record_keys, + }; + // Startup observes the exact public store before any worker starts. + // An absent store is an empty public projection, not private fallback. + server.raw_snapshot()?; + Ok(server) + } + + fn serve(&self, request: &CultNetMessage) -> Result { + let registry = public_health_document_registry(); + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + for key in &self.allowed_record_keys { + policy.allow(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, key)?; + } + serve_read_only_raw_snapshot(®istry, &policy, &self.raw_snapshot()?, request) + } + + fn raw_snapshot(&self) -> Result> { + let registry = public_health_document_registry(); + let entries = SingleFileMessagePackBackingStore::new(&self.public_store_path) + .pull_all_read_only_snapshot()?; + let mut records = Vec::with_capacity(entries.len()); + for entry in entries { + if entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE + || entry.schema_id.as_deref() + != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) + { + return Err(anyhow!( + "public health query store contains a non-projection envelope" + )); + } + let projection: IdunnAuthenticatedProviderHealthProjectionRecord = + rmp_serde::from_slice(&entry.payload) + .context("decoding public health query projection")?; + if rmp_serde::to_vec(&projection)? != entry.payload + || projection.projection_id != entry.key + { + return Err(anyhow!( + "public health query projection is noncanonical or has a mismatched key" + )); + } + projection.validate()?; + let mut record = registry.raw_document_record_from_envelope(&entry)?; + record.source_runtime_id = Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID.into()); + record.source_agent_id = None; + record.source_role = Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE.into()); + record.tags = Some(vec!["public-health".into()]); + records.push(record); + } + Ok(records) + } +} + +fn public_health_document_registry() -> CultNetDocumentRegistry { + let mut registry = CultNetDocumentRegistry::new(); + registry.register(CultNetDocumentBinding { + schema_id: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + document_type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), + mutation_contract: None, + payload_schema_version: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), + }); + registry +} + +fn provider_health_projection_key(daemon_id: &str, health_contract: &str) -> String { + format!( + "provider-health:{:x}", + Sha256::digest([daemon_id.as_bytes(), b"\0", health_contract.as_bytes()].concat()) + ) +} + #[derive(Clone, Debug, PartialEq, Eq)] struct AuthenticatedProviderHealthSource { health: IdunnDaemonHealthRecord, @@ -2197,7 +2315,8 @@ fn initialize_projection_publisher( options.service_identity_store_path.as_deref(), options.public_health_store_path.as_deref(), ) else { - if options.service_identity_store_path.is_some() || options.public_health_store_path.is_some() + if options.service_identity_store_path.is_some() + || options.public_health_store_path.is_some() { return Err(anyhow!( "--service-identity-store and --public-health-store must be configured together" @@ -2226,7 +2345,11 @@ fn initialize_projection_publisher( }))) } -fn reject_store_aliases(options: &CommonOptions, identity_path: &Path, public_path: &Path) -> Result<()> { +fn reject_store_aliases( + options: &CommonOptions, + identity_path: &Path, + public_path: &Path, +) -> Result<()> { let identity = normalized_store_path(identity_path)?; let public = normalized_store_path(public_path)?; let mut private_paths = vec![normalized_store_path(&options.store_path)?, identity]; @@ -2240,11 +2363,12 @@ fn reject_store_aliases(options: &CommonOptions, identity_path: &Path, public_pa { private_paths.push(normalized_store_path(path)?); } - if private_paths + if private_paths.iter().enumerate().any(|(index, path)| { + private_paths .iter() - .enumerate() - .any(|(index, path)| private_paths.iter().skip(index + 1).any(|other| other == path)) - { + .skip(index + 1) + .any(|other| other == path) + }) { return Err(anyhow!( "service identity, operational, trust, and release stores must not alias" )); @@ -2287,9 +2411,12 @@ impl IdunnProjectionPublisher { ) -> Result<()> { let evaluated_at_unix_millis = parse_timestamp_millis(evaluated_at) .ok_or_else(|| anyhow!("projection evaluation time is invalid"))?; - let current = read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? + let current = + read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? .and_then(|managed| managed.projection_source) - .ok_or_else(|| anyhow!("authenticated provider health vanished before projection"))?; + .ok_or_else(|| { + anyhow!("authenticated provider health vanished before projection") + })?; if ¤t != source { return Err(anyhow!( "authenticated provider health changed before projection publication" @@ -2303,14 +2430,13 @@ impl IdunnProjectionPublisher { .observed_at_unix_millis .saturating_add(u64::from(desired.max_silence_seconds).saturating_mul(1000)); if expires_at_unix_millis <= evaluated_at_unix_millis { - return Err(anyhow!("authenticated provider health expired before projection")); + return Err(anyhow!( + "authenticated provider health expired before projection" + )); } - let projection_key = format!( - "provider-health:{:x}", - Sha256::digest( - [source.health.daemon_id.as_bytes(), b"\0", source.health.health_contract.as_bytes()] - .concat() - ) + let projection_key = provider_health_projection_key( + &source.health.daemon_id, + &source.health.health_contract, ); let _write_guard = self .write_lock @@ -2324,26 +2450,41 @@ impl IdunnProjectionPublisher { || entry.schema_id.as_deref() != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) }) { - return Err(anyhow!("public health store contains a non-projection envelope")); + return Err(anyhow!( + "public health store contains a non-projection envelope" + )); } for entry in &entries { self.verify_existing_projection(entry)?; } - let current_envelope = entries.iter().find(|entry| entry.key == projection_key).cloned(); + let current_envelope = entries + .iter() + .find(|entry| entry.key == projection_key) + .cloned(); let current_projection = current_envelope .as_ref() - .map(|entry| rmp_serde::from_slice::(&entry.payload)) + .map(|entry| { + rmp_serde::from_slice::( + &entry.payload, + ) + }) .transpose() .context("decoding existing public health projection")?; if let Some(existing) = current_projection.as_ref() { existing.validate()?; - if current_envelope.as_ref().is_some_and(|entry| entry.key != existing.projection_id) + if current_envelope + .as_ref() + .is_some_and(|entry| entry.key != existing.projection_id) || existing.projection_id != projection_key { - return Err(anyhow!("public projection envelope key does not match its payload")); + return Err(anyhow!( + "public projection envelope key does not match its payload" + )); } if existing.idunn_signer_identity_id != self.signer.entry().identity_id { - return Err(anyhow!("public projection signer identity changed without store rotation")); + return Err(anyhow!( + "public projection signer identity changed without store rotation" + )); } if existing.projection_incarnation_id == self.incarnation_id && existing.signed_health_sha256 == source.statement_sha256 @@ -2361,12 +2502,15 @@ impl IdunnProjectionPublisher { } let mut projection = IdunnAuthenticatedProviderHealthProjectionRecord { schema_version: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - projection_id: projection_key.clone(), daemon_id: source.health.daemon_id.clone(), + projection_id: projection_key.clone(), + daemon_id: source.health.daemon_id.clone(), health_contract: source.health.health_contract.clone(), - provider_state: source.health.state.clone(), reason_code: reason_code.into(), + provider_state: source.health.state.clone(), + reason_code: reason_code.into(), provider_observed_at_unix_millis: source.admission.observed_at_unix_millis, admitted_at_unix_millis: source.admission.admitted_at_unix_millis, - evaluated_at_unix_millis, trust_binding_id: source.binding.binding_id.clone(), + evaluated_at_unix_millis, + trust_binding_id: source.binding.binding_id.clone(), trust_binding_sha256: source.binding_sha256.clone(), signed_health_sha256: source.statement_sha256.clone(), authenticated_admission_sha256: source.admission_sha256.clone(), @@ -2379,19 +2523,25 @@ impl IdunnProjectionPublisher { deployment_id: source.admission.deployment_id.clone(), idunn_runtime_id: "idunn-daemon".into(), idunn_signer_identity_id: self.signer.entry().identity_id.clone(), - projection_incarnation_id: self.incarnation_id.clone(), projection_sequence: sequence, - signature_algorithm: "ed25519".into(), signature: Vec::new(), - private_state_exposed: false, expires_at_unix_millis, + projection_incarnation_id: self.incarnation_id.clone(), + projection_sequence: sequence, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + private_state_exposed: false, + expires_at_unix_millis, }; let unsigned = rmp_serde::to_vec(&projection)?; - projection.signature = self.signer - .sign::(&unsigned).signature; + projection.signature = self + .signer + .sign::(&unsigned) + .signature; projection.validate()?; self.require_incarnation_trust_snapshot(options)?; let replacement = CultCacheEnvelope { key: projection_key.clone(), r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - payload: rmp_serde::to_vec(&projection)?, stored_at: evaluated_at.into(), + payload: rmp_serde::to_vec(&projection)?, + stored_at: evaluated_at.into(), schema_id: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), }; let expected = CultCacheExpectedEnvelope { @@ -2399,13 +2549,19 @@ impl IdunnProjectionPublisher { r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), current: current_envelope, }; - if backing.compare_exchange(&[expected], &[replacement])? { return Ok(()); } + if backing.compare_exchange(&[expected], &[replacement])? { + return Ok(()); + } } - Err(anyhow!("public health projection lost repeated cross-process contention")) + Err(anyhow!( + "public health projection lost repeated cross-process contention" + )) } fn require_incarnation_trust_snapshot(&self, options: &CommonOptions) -> Result<()> { - let path = options.daemon_health_trust_store_path.as_deref() + let path = options + .daemon_health_trust_store_path + .as_deref() .ok_or_else(|| anyhow!("projection publisher lost its root trust store"))?; if trust_store_snapshot_sha256(path)? != self.root_trust_snapshot_sha256 { return Err(anyhow!( @@ -2420,7 +2576,9 @@ impl IdunnProjectionPublisher { rmp_serde::from_slice(&entry.payload) .context("decoding public health projection during store audit")?; if rmp_serde::to_vec(&projection)? != entry.payload { - return Err(anyhow!("public health projection is not canonical MessagePack")); + return Err(anyhow!( + "public health projection is not canonical MessagePack" + )); } projection.validate()?; if entry.key != projection.projection_id @@ -2447,13 +2605,19 @@ impl IdunnProjectionPublisher { } fn record_sha256(value: &T) -> Result { - Ok(format!("sha256-{:x}", Sha256::digest(rmp_serde::to_vec(value)?))) + Ok(format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(value)?) + )) } fn publish_authenticated_provider_health( - publisher: Option<&IdunnProjectionPublisher>, options: &CommonOptions, - store_lock: &Arc>, desired: &IdunnDesiredDaemonRecord, - source: Option<&AuthenticatedProviderHealthSource>, evaluated_at: &str, + publisher: Option<&IdunnProjectionPublisher>, + options: &CommonOptions, + store_lock: &Arc>, + desired: &IdunnDesiredDaemonRecord, + source: Option<&AuthenticatedProviderHealthSource>, + evaluated_at: &str, ) -> Result<()> { if let (Some(publisher), Some(source)) = (publisher, source) { publisher.publish_if_current(options, store_lock, desired, source, evaluated_at)?; @@ -2680,6 +2844,177 @@ fn start_rudp_health_ingress( Ok(()) } +fn start_public_health_query_listener( + options: &CommonOptions, + targets: &[DaemonTarget], +) -> Result> { + let Some(bind_address) = options.public_health_query_bind else { + return Ok(None); + }; + let public_store_path = options + .public_health_store_path + .clone() + .ok_or_else(|| anyhow!("--public-health-query-bind requires --public-health-store"))?; + if options.service_identity_store_path.is_none() { + return Err(anyhow!( + "--public-health-query-bind requires --service-identity-store projection authority" + )); + } + let server = IdunnPublicHealthSnapshotServer::new(public_store_path, targets)?; + let socket = UdpSocket::bind(bind_address) + .with_context(|| format!("binding public health query listener at {bind_address}"))?; + let local_addr = socket.local_addr()?; + socket.set_read_timeout(Some(Duration::from_millis(250)))?; + thread::spawn(move || { + if let Err(error) = run_public_health_query_listener_loop(socket, server, None) { + eprintln!("Idunn public health query listener stopped: {error:#}"); + } + }); + println!( + "Idunn public health query listener active at {local_addr} for {} target projection key(s).", + targets.len() + ); + Ok(Some(local_addr)) +} + +fn run_public_health_query_listener_loop( + socket: UdpSocket, + server: IdunnPublicHealthSnapshotServer, + stop: Option>, +) -> Result<()> { + let mut sessions: HashMap = HashMap::new(); + let mut buffer = vec![0_u8; 65_535]; + loop { + if stop + .as_ref() + .is_some_and(|flag| flag.load(Ordering::Acquire)) + { + return Ok(()); + } + match socket.recv_from(&mut buffer) { + Ok((size, source)) => { + if let Err(error) = handle_public_health_query_datagram( + &socket, + &mut sessions, + &server, + &buffer[..size], + source, + ) { + eprintln!( + "Idunn public health query listener refused datagram from {source}: {error:#}" + ); + } + } + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::WouldBlock + | std::io::ErrorKind::TimedOut + | std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::ConnectionAborted + ) => {} + Err(error) => return Err(error.into()), + } + let now = unix_epoch_millis()?; + let mut expired = Vec::new(); + for (peer, session) in &mut sessions { + for packet in session.due_resends(now) { + socket.send_to(&encode_rudp_packet(&packet)?, peer)?; + } + if session.check_timeout(now, 60_000) { + expired.push(*peer); + } + } + for peer in expired { + sessions.remove(&peer); + } + } +} + +fn handle_public_health_query_datagram( + socket: &UdpSocket, + sessions: &mut HashMap, + server: &IdunnPublicHealthSnapshotServer, + wire: &[u8], + source: SocketAddr, +) -> Result<()> { + let packet = decode_rudp_packet(wire)?; + if packet.connection_id != IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID { + return Err(anyhow!( + "unexpected public health query RUDP connection id {:08x}", + packet.connection_id + )); + } + let now = unix_epoch_millis()?; + if packet.packet_type == CultNetRudpPacketType::Connect { + let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { + connection_id: IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, + initial_sequence: 1, + resend_delay_ms: 100, + max_pending_reliable_packets: Some(64), + }); + let accept = session.accept_connect(&packet, now, Vec::new())?; + socket.send_to(&encode_rudp_packet(&accept)?, source)?; + sessions.insert(source, session); + return Ok(()); + } + let session = sessions + .get_mut(&source) + .ok_or_else(|| anyhow!("public health query peer has no accepted RUDP session"))?; + let result = session.receive(&packet, now)?; + if let Some(reply) = result.reply { + socket.send_to(&encode_rudp_packet(&reply)?, source)?; + } + if result.disconnected { + sessions.remove(&source); + return Ok(()); + } + if packet.packet_type == CultNetRudpPacketType::Accept || !result.delivered.is_empty() { + socket.send_to(&encode_rudp_packet(&session.create_ack())?, source)?; + } + for frame in result.delivered { + let response = if frame.channel_id != "schema" { + CultNetMessage::Error { + error: "public health queries require the CultNet schema channel".into(), + } + } else { + match decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + ) { + Ok(request) => { + server + .serve(&request) + .unwrap_or_else(|error| CultNetMessage::Error { + error: format!("public health query refused: {error}"), + }) + } + Err(error) => CultNetMessage::Error { + error: format!("public health query message is malformed: {error}"), + }, + } + }; + let payload = + encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; + let packets = session.send_many( + "schema", + payload, + CultNetRudpSendOptions { + reliable: true, + ordered: true, + sequenced: false, + now_ms: now, + reliable_expire_after_ms: None, + }, + Some(1200), + )?; + for packet in packets { + socket.send_to(&encode_rudp_packet(&packet)?, source)?; + } + } + Ok(()) +} + fn rudp_health_ingress_record( ingress_id: &str, bind_address: SocketAddr, @@ -3058,7 +3393,9 @@ fn authenticate_generic_signed_health( || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) || document.source_role.as_deref() != Some("daemon-health-publisher") { - return Err(anyhow!("signed daemon health transport identity is invalid")); + return Err(anyhow!( + "signed daemon health transport identity is invalid" + )); } let (binding, binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; if binding.release_binding_required { @@ -3067,7 +3404,9 @@ fn authenticate_generic_signed_health( || statement.source_commit.is_none() || statement.deployment_id.is_none() { - return Err(anyhow!("signed daemon health lacks required release binding")); + return Err(anyhow!( + "signed daemon health lacks required release binding" + )); } } else if statement.release_id.is_some() || statement.release_witness_sha256.is_some() @@ -3078,9 +3417,8 @@ fn authenticate_generic_signed_health( "signed daemon health carries release authority not declared by its trust binding" )); } - let expected_identity = derive_service_identity_id::( - &binding.signer_public_key, - )?; + let expected_identity = + derive_service_identity_id::(&binding.signer_public_key)?; if binding.signer_identity_id != expected_identity || statement.signer_identity_id != binding.signer_identity_id { @@ -3112,7 +3450,9 @@ fn authenticate_generic_signed_health( || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) > EPIPHANY_ADMISSION_MAX_AGE_SECONDS.saturating_mul(1000) { - return Err(anyhow!("signed daemon health observation is future or stale")); + return Err(anyhow!( + "signed daemon health observation is future or stale" + )); } let signed_health_sha256 = format!("sha256-{:x}", Sha256::digest(&document.payload)); let observed_at = chrono::DateTime::from_timestamp_millis( @@ -3199,8 +3539,8 @@ fn admit_generic_signed_health( for _ in 0..8 { let (expected_health, expected_admission, expected_statement) = with_store_node(options, store_lock, |node| { - if let Some(existing) = node - .get::(&admission.daemon_id)? + if let Some(existing) = + node.get::(&admission.daemon_id)? && !generic_signed_health_advances(&existing, &admission) { return Err(anyhow!("signed daemon health is replayed or regressed")); @@ -3213,8 +3553,7 @@ fn admit_generic_signed_health( .get_envelope::( &admission.daemon_id, )?, - node.cache() - .get_envelope::( + node.cache().get_envelope::( &admission.signed_health_sha256, )?, )) @@ -3292,13 +3631,17 @@ fn validate_generic_release_binding( || request.release_authority_id.trim().is_empty() || request.release_authority_envelope_sha256.trim().is_empty() { - return Err(anyhow!("signed daemon health deployment authority is invalid")); + return Err(anyhow!( + "signed daemon health deployment authority is invalid" + )); } let current = node .get::(&admission.daemon_id)? .ok_or_else(|| anyhow!("signed daemon health has no current deployment head"))?; if current.request_id != request.request_id || current.sequence == 0 { - return Err(anyhow!("signed daemon health names a superseded deployment")); + return Err(anyhow!( + "signed daemon health names a superseded deployment" + )); } let requested_at = parse_timestamp_millis(&request.requested_at) .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; @@ -3364,7 +3707,8 @@ fn decode_health_from_rudp_message( if document.record_key != wire.daemon_id { return Err(anyhow!( "record key {} does not match health daemon_id {}", - document.record_key, wire.daemon_id + document.record_key, + wire.daemon_id )); } let received_at_unix_millis = parse_timestamp_millis(admitted_at) @@ -4987,6 +5331,7 @@ impl Options { let mut daemon_health_trust_store_path = None; let mut service_identity_store_path = None; let mut public_health_store_path = None; + let mut public_health_query_bind = None; let mut execute = false; let mut command_timeout_seconds = 30; let mut daemon_id = None; @@ -5109,6 +5454,15 @@ impl Options { "--public-health-store", )?)); } + "--public-health-query-bind" => { + public_health_query_bind = Some( + take_value(&mut args, "--public-health-query-bind")? + .parse() + .with_context( + || "--public-health-query-bind must be a socket address", + )?, + ); + } "--disabled" => enabled = false, "--execute" => execute = true, "--interval-seconds" => { @@ -5154,6 +5508,7 @@ impl Options { daemon_health_trust_store_path, service_identity_store_path, public_health_store_path, + public_health_query_bind, execute, command_timeout_seconds, }; @@ -5279,6 +5634,20 @@ impl Options { } }; + if common.public_health_query_bind.is_some() && !matches!(mode, Mode::Swarm(_)) { + return Err(anyhow!( + "--public-health-query-bind requires --swarm-profile" + )); + } + if common.public_health_query_bind.is_some() + && (common.public_health_store_path.is_none() + || common.service_identity_store_path.is_none()) + { + return Err(anyhow!( + "--public-health-query-bind requires --public-health-store and --service-identity-store" + )); + } + Ok(Self { common, mode }) } } @@ -5810,7 +6179,7 @@ fn unix_epoch_millis() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." } #[cfg(test)] @@ -5844,9 +6213,7 @@ mod tests { .unwrap(); }); assert!( - entered_rx - .recv_timeout(Duration::from_millis(100)) - .is_err(), + entered_rx.recv_timeout(Duration::from_millis(100)).is_err(), "automatic request owner entered while manual actuation was live" ); release_tx.send(()).unwrap(); @@ -5866,7 +6233,11 @@ mod tests { Ok(()) }); - assert!(actuation_rx.recv_timeout(Duration::from_millis(100)).is_err()); + assert!( + actuation_rx + .recv_timeout(Duration::from_millis(100)) + .is_err() + ); // This is the target loop's independent signed-health admission lane: // it does not acquire or await the consequence gate. let admitted_health = "accepted"; @@ -5900,7 +6271,8 @@ mod tests { #[cfg(unix)] #[test] fn status_timeout_terminates_command_descendants() { - let marker = env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); + let marker = + env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); let _ = fs::remove_file(&marker); let error = wait_for_child_status_with_timeout( descendant_marker_command(&marker), @@ -5910,13 +6282,17 @@ mod tests { .expect_err("command should time out"); assert!(error.to_string().contains("timed out")); thread::sleep(Duration::from_millis(1200)); - assert!(!marker.exists(), "timed-out descendant survived to write marker"); + assert!( + !marker.exists(), + "timed-out descendant survived to write marker" + ); } #[cfg(unix)] #[test] fn output_timeout_terminates_command_descendants() { - let marker = env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); + let marker = + env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); let _ = fs::remove_file(&marker); let error = wait_for_child_with_timeout( descendant_marker_command(&marker), @@ -5926,7 +6302,10 @@ mod tests { .expect_err("command should time out"); assert!(error.to_string().contains("timed out")); thread::sleep(Duration::from_millis(1200)); - assert!(!marker.exists(), "timed-out descendant survived to write marker"); + assert!( + !marker.exists(), + "timed-out descendant survived to write marker" + ); } use cultnet_rs::{CultNetRawDocumentRecord, CultNetRawPayloadEncoding}; use ed25519_dalek::{Signer, SigningKey}; @@ -6178,6 +6557,7 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: true, command_timeout_seconds: 1, }; @@ -7122,9 +7502,11 @@ mod tests { )); assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); - assert!(actuator.contains( + assert!( + actuator.contains( "epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true" - )); + ) + ); assert!(actuator.contains( "voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false" )); @@ -7190,11 +7572,10 @@ mod tests { } fn deployed_revision(&self, _release: &ReleaseTarget) -> Result { - Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "deployment witness is absent", + Err( + std::io::Error::new(std::io::ErrorKind::NotFound, "deployment witness is absent") + .into(), ) - .into()) } } @@ -7314,7 +7695,11 @@ mod tests { assert_eq!(health.state, "stale-deployment"); assert_eq!(health.transport, "idunn.release-not-deployed"); - assert!(health.detail.contains("no deployed revision witness exists")); + assert!( + health + .detail + .contains("no deployed revision witness exists") + ); assert!( plan_keepalive(&desired, &health, "unix:100") .deployment_request @@ -7421,13 +7806,18 @@ mod tests { ); assert_eq!( target.deploy_command.as_deref(), - Some("sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"") + Some( + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + ) ); assert!(target.restart_command.is_none()); let release = target.release.as_ref().expect("release target"); assert!(release.requires_bifrost_authority); assert_eq!(release.repository_full_name, "GameCult/Bifrost"); - assert_eq!(release.repo_path, PathBuf::from("/srv/build/Bifrost-persona-feedback")); + assert_eq!( + release.repo_path, + PathBuf::from("/srv/build/Bifrost-persona-feedback") + ); let desired = IdunnDesiredDaemonRecord { daemon_id: target.daemon_id.clone(), @@ -7523,6 +7913,7 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -7598,6 +7989,7 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -8163,21 +8555,21 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; let lock = Arc::new(Mutex::new(())); - let outcome = admit_health_from_rudp_message( - &message, - &options, - &lock, - "2026-06-15T00:00:01Z", - ) + let outcome = + admit_health_from_rudp_message(&message, &options, &lock, "2026-06-15T00:00:01Z") .unwrap(); assert_eq!(outcome.authority, "diagnostic-only"); assert!(health_from_rudp_message(&message, &options).is_err()); with_store_node(&options, &lock, |node| { - assert!(node.get::(&health.daemon_id)?.is_none()); + assert!( + node.get::(&health.daemon_id)? + .is_none() + ); let diagnostic = node .get::(&format!( "diagnostic:{}", @@ -8226,9 +8618,7 @@ mod tests { SingleFileMessagePackBackingStore::new(&trust_path) .push(&typed_envelope(&binding.binding_id, &binding, "unix:1784483100").unwrap()) .unwrap(); - let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339( - "2026-07-19T12:00:00Z", - ) + let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00Z") .unwrap() .timestamp_millis() as u64; let mut statement = IdunnSignedDaemonHealthRecord { @@ -8243,8 +8633,7 @@ mod tests { publisher_sequence: sequence, observed_at_unix_millis, release_id: release_bound.then(|| "release-test-1".into()), - release_witness_sha256: release_bound - .then(|| format!("sha256-{}", "a".repeat(64))), + release_witness_sha256: release_bound.then(|| format!("sha256-{}", "a".repeat(64))), source_commit: release_bound.then(|| "b".repeat(40)), deployment_id: release_bound.then(|| "deploy-test-1".into()), signature_algorithm: "ed25519".into(), @@ -8308,6 +8697,7 @@ mod tests { daemon_health_trust_store_path: Some(trust_path), service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }, @@ -8464,6 +8854,233 @@ mod tests { rmp_serde::from_slice(&entries[0].payload).unwrap() } + fn public_query_target(desired: &IdunnDesiredDaemonRecord) -> DaemonTarget { + DaemonTarget { + daemon_id: desired.daemon_id.clone(), + verse_id: desired.verse_id.clone(), + name: desired.name.clone(), + health_contract: health_contract(&desired.health_contract, "failed"), + deploy_command: None, + restart_command: None, + release: None, + enabled: true, + interval_seconds: 30, + } + } + + fn connect_public_query_client( + server_addr: SocketAddr, + ) -> CultNetRudpSocketTransportConnection { + let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); + socket + .set_read_timeout(Some(Duration::from_millis(100))) + .unwrap(); + let mut client = + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( + "epiphany-test-consumer", + socket, + server_addr, + IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, + )) + .unwrap(); + client.connect(Vec::new()).unwrap(); + let deadline = Instant::now() + Duration::from_secs(2); + while !client.connected() { + client.receive_once().unwrap(); + client.poll_resends().unwrap(); + assert!( + Instant::now() < deadline, + "public query handshake timed out" + ); + } + client + } + + fn receive_public_query_message( + client: &mut CultNetRudpSocketTransportConnection, + ) -> CultNetMessage { + let deadline = Instant::now() + Duration::from_secs(2); + loop { + if let Some(frame) = client.receive_once().unwrap() { + assert_eq!(frame.channel_id, "schema"); + return decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + ) + .unwrap(); + } + client.poll_resends().unwrap(); + assert!(Instant::now() < deadline, "public query response timed out"); + } + } + + #[test] + fn public_query_server_returns_exact_allowlisted_bytes_and_correlation() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let public_path = options.public_health_store_path.as_ref().unwrap(); + let stored = SingleFileMessagePackBackingStore::new(public_path) + .pull_all_read_only_snapshot() + .unwrap() + .remove(0); + let server = IdunnPublicHealthSnapshotServer::new( + public_path.clone(), + &[public_query_target(&desired)], + ) + .unwrap(); + let request = CultNetMessage::SnapshotRequest { + message_id: "status-correlation-7".into(), + schema_ids: Some(vec![ + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + ]), + record_keys: Some(vec![stored.key.clone()]), + }; + let CultNetMessage::SnapshotResponseRaw { + message_id, + documents, + } = server.serve(&request).unwrap() + else { + panic!("expected raw public health snapshot"); + }; + assert_eq!(message_id, "status-correlation-7"); + assert_eq!(documents.len(), 1); + assert_eq!(documents[0].payload, stored.payload); + assert_eq!( + documents[0].source_runtime_id.as_deref(), + Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID) + ); + assert_eq!( + documents[0].source_role.as_deref(), + Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE) + ); + + for request in [ + CultNetMessage::SnapshotRequest { + message_id: "unknown-schema".into(), + schema_ids: Some(vec!["private.idunn.state.v0".into()]), + record_keys: None, + }, + CultNetMessage::SnapshotRequest { + message_id: "unknown-key".into(), + schema_ids: None, + record_keys: Some(vec!["provider-health:not-in-target-catalog".into()]), + }, + ] { + let CultNetMessage::SnapshotResponseRaw { documents, .. } = + server.serve(&request).unwrap() + else { + panic!("expected bounded empty response"); + }; + assert!(documents.is_empty()); + } + } + + #[test] + fn public_query_store_contamination_is_fatal_and_private_type_is_never_served() { + let (options, _lock, desired, _source, _publisher, root) = projection_fixture(false); + let private_path = root.path().join("private-in-public.cc"); + let private = IdunnDaemonHealthRecord { + daemon_id: desired.daemon_id.clone(), + state: "active".into(), + detail: "private managed judgment".into(), + observed_at: "2026-07-19T12:00:02Z".into(), + health_contract: desired.health_contract.clone(), + publication_source: "daemon-authenticated".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }; + SingleFileMessagePackBackingStore::new(&private_path) + .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:02Z").unwrap()) + .unwrap(); + assert!( + IdunnPublicHealthSnapshotServer::new(private_path, &[public_query_target(&desired)]) + .is_err() + ); + + let public_path = options.public_health_store_path.as_ref().unwrap(); + let server = IdunnPublicHealthSnapshotServer::new( + public_path.clone(), + &[public_query_target(&desired)], + ) + .unwrap(); + SingleFileMessagePackBackingStore::new(public_path) + .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:03Z").unwrap()) + .unwrap(); + assert!( + server + .serve(&CultNetMessage::SnapshotRequest { + message_id: "private-probe".into(), + schema_ids: None, + record_keys: None, + }) + .is_err() + ); + } + + #[test] + fn public_query_rudp_is_multi_peer_and_survives_malformed_request() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let server = IdunnPublicHealthSnapshotServer::new( + options.public_health_store_path.clone().unwrap(), + &[public_query_target(&desired)], + ) + .unwrap(); + let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); + let server_addr = socket.local_addr().unwrap(); + socket + .set_read_timeout(Some(Duration::from_millis(100))) + .unwrap(); + let stop = Arc::new(AtomicBool::new(false)); + let worker_stop = Arc::clone(&stop); + let worker = thread::spawn(move || { + run_public_health_query_listener_loop(socket, server, Some(worker_stop)) + }); + + let mut hostile = connect_public_query_client(server_addr); + hostile.send("schema", vec![0xc1]).unwrap(); + assert!(matches!( + receive_public_query_message(&mut hostile), + CultNetMessage::Error { .. } + )); + + let mut first = connect_public_query_client(server_addr); + let mut second = connect_public_query_client(server_addr); + for (client, id) in [(&mut first, "first-peer"), (&mut second, "second-peer")] { + let request = CultNetMessage::SnapshotRequest { + message_id: id.into(), + schema_ids: Some(vec![ + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + ]), + record_keys: Some(vec![provider_health_projection_key( + &desired.daemon_id, + &desired.health_contract, + )]), + }; + client + .send( + "schema", + encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0) + .unwrap(), + ) + .unwrap(); + let CultNetMessage::SnapshotResponseRaw { + message_id, + documents, + } = receive_public_query_message(client) + else { + panic!("expected peer snapshot response"); + }; + assert_eq!(message_id, id); + assert_eq!(documents.len(), 1); + } + stop.store(true, Ordering::Release); + worker.join().unwrap().unwrap(); + } + #[test] fn projection_is_signed_canonical_public_state_without_provider_detail() { let (options, lock, desired, source, publisher, _root) = projection_fixture(false); @@ -8608,7 +9225,8 @@ mod tests { payload: rmp_serde::to_vec(&corrupted).unwrap(), ..current_envelope.clone() }; - assert!(backing + assert!( + backing .compare_exchange( &[CultCacheExpectedEnvelope { key: current_envelope.key.clone(), @@ -8617,10 +9235,13 @@ mod tests { }], &[corrupted_envelope], ) - .unwrap()); - assert!(second + .unwrap() + ); + assert!( + second .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:04Z") - .is_err()); + .is_err() + ); let contaminated_path = root.path().join("contaminated-public.cc"); SingleFileMessagePackBackingStore::new(&contaminated_path) @@ -8841,6 +9462,7 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -9136,6 +9758,7 @@ mod tests { daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index ef9671b..12a252e 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -60,6 +60,15 @@ Consumers such as Epiphany Discord Status receive only the outward projection through CultNet/CultMesh and verify Idunn's pinned identity. Reading Idunn's private store is not a consumer protocol. +The query transport is an explicit, read-only CultNet/RUDP listener enabled by +`--public-health-query-bind `. It has no default and is valid only with +`--swarm-profile`, `--service-identity-store`, and `--public-health-store`. +The listener opens only the dedicated public store and derives its allowlist +from the configured target catalog's daemon/health-contract pairs. It returns +the exact stored projection payload bytes with fixed source runtime +`idunn-daemon` and role `authenticated-provider-health-projector`. It has no +handle to Idunn's private stores and accepts no mutation message. + ## Derived state - Process and systemd observations are diagnostic evidence only. @@ -142,14 +151,24 @@ and an expiry capped by the provider observation's managed-health silence window. Missing, legacy, unsigned, drifted, or mutated input cannot write, refresh, or delete a projection; an old row simply expires for consumers. +The public projection has a dedicated multi-peer CultNet/RUDP snapshot +listener. Startup synchronously binds the explicit address and validates the +public store before target workers start. Bind failure or public-store +contamination is fatal startup evidence. Each request rereads only that store +and passes exact records through CultNet's read-only snapshot server with the +target-derived `(schema,key)` allowlist. Unknown schema/key requests return no +record. Malformed datagrams or messages are refused per peer without killing +the daemon. No request can write, delete, import, or inspect private Idunn +state. + This source is deliberately not deployable yet: Bifrost and the remaining generic publishers still emit the unsigned diagnostic contract, production -root trust and Idunn identity stores have not been installed, Epiphany still -uses its signed v0 migration path, and the public projection has no CultNet -query transport or consumer verifier. Deploying this intermediate source would -correctly classify unmigrated daemons as missing health and could therefore -actuate recovery. Publisher migration, trust enrollment, transport, and the -consumer must land before promotion. +root trust and Idunn identity stores have not been installed, and Epiphany +still uses its signed v0 migration path. The query transport and Epiphany +consumer verifier now exist in source, but neither makes an unmigrated +publisher authoritative. Deploying this intermediate source would correctly +classify unmigrated daemons as missing health and could therefore actuate +recovery. Publisher migration and trust enrollment must land before promotion. ## Verification layer From 61d2fc702e52766e55e62e74d5c6b8d1652cedaa Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 22:17:39 +0100 Subject: [PATCH 24/93] Consume shared signed health contract --- crates/odin-core/src/documents.rs | 90 ++----------------------------- vendor/cultnet-rs | 2 +- 2 files changed, 4 insertions(+), 88 deletions(-) diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index ff55260..16edfd8 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -4,11 +4,10 @@ pub use cultnet_rs::{ IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, IDUNN_PROVIDER_ACTIVE_REASON, IDUNN_PROVIDER_DEGRADED_REASON, IDUNN_PROVIDER_FAILED_REASON, IDUNN_PROVIDER_WARMING_REASON, - IdunnAuthenticatedProviderHealthProjectionRecord, authenticated_provider_health_reason_code, -}; -use cultnet_rs::{ - GameCultProviderHealthIdentity, derive_service_identity_id, + IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IdunnAuthenticatedProviderHealthProjectionRecord, + IdunnSignedDaemonHealthRecord, authenticated_provider_health_reason_code, }; +use cultnet_rs::{GameCultProviderHealthIdentity, derive_service_identity_id}; use serde_json::Value; use anyhow::{Result, bail}; @@ -48,7 +47,6 @@ pub const IDUNN_DAEMON_TRANSPORT_PROFILE_SCHEMA: &str = "idunn.daemon_transport_ pub const IDUNN_COMMAND_BOUNDARY_SCHEMA: &str = "idunn.command_boundary.v1"; pub const IDUNN_RUNTIME_TRANSPORT_CHECK_SCHEMA: &str = "idunn.runtime_transport_check.v1"; pub const IDUNN_RUDP_HEALTH_INGRESS_SCHEMA: &str = "idunn.rudp_health_ingress.v1"; -pub const IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA: &str = "idunn.signed_daemon_health.v1"; pub const IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA: &str = "idunn.daemon_health_trust_binding.v1"; pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = "idunn.authenticated_daemon_health_admission.v1"; @@ -327,88 +325,6 @@ pub struct IdunnSignedHealthAdmissionRecord { pub signed_health_sha256: String, } -/// Provider-owned health statement. Ingress must verify `signature` over the -/// complete document with an empty signature field before admitting it. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.signed_daemon_health", - schema = "idunn.signed_daemon_health.v1" -)] -pub struct IdunnSignedDaemonHealthRecord { - #[cultcache(key = 0)] - pub schema_version: String, - #[cultcache(key = 1)] - pub daemon_id: String, - #[cultcache(key = 2)] - pub health_contract: String, - #[cultcache(key = 3)] - pub source_runtime_id: String, - #[cultcache(key = 4)] - pub state: String, - #[cultcache(key = 5)] - pub detail: String, - #[cultcache(key = 6)] - pub signer_identity_id: String, - #[cultcache(key = 7)] - pub publisher_incarnation_id: String, - #[cultcache(key = 8)] - pub publisher_sequence: u64, - #[cultcache(key = 9)] - pub observed_at_unix_millis: u64, - #[cultcache(key = 10)] - pub release_id: Option, - #[cultcache(key = 11)] - pub release_witness_sha256: Option, - #[cultcache(key = 12)] - pub source_commit: Option, - #[cultcache(key = 13)] - pub deployment_id: Option, - #[cultcache(key = 14)] - pub signature_algorithm: String, - #[cultcache(key = 15)] - pub signature: Vec, - #[cultcache(key = 16)] - pub private_state_exposed: bool, -} - -impl IdunnSignedDaemonHealthRecord { - pub fn validate(&self) -> Result<()> { - if self.schema_version != IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA { - bail!("signed daemon health schema is unsupported"); - } - validate_identifier(&self.daemon_id, "daemon id")?; - validate_identifier(&self.health_contract, "health contract")?; - validate_identifier(&self.source_runtime_id, "source runtime id")?; - validate_identifier(&self.signer_identity_id, "signer identity id")?; - validate_identifier(&self.publisher_incarnation_id, "publisher incarnation id")?; - if !matches!( - self.state.as_str(), - "active" | "warming" | "degraded" | "failed" - ) { - bail!("signed daemon health state is invalid"); - } - if self.detail.len() > 512 { - bail!("signed daemon health detail exceeds 512 bytes"); - } - if self.signature_algorithm != "ed25519" || self.signature.len() != 64 { - bail!("signed daemon health signature shape is invalid"); - } - if self.publisher_sequence == 0 || self.observed_at_unix_millis == 0 { - bail!("signed daemon health sequence or observation time is invalid"); - } - validate_optional_release_binding( - &self.release_id, - &self.release_witness_sha256, - &self.source_commit, - )?; - validate_optional_identifier(&self.deployment_id, "deployment id")?; - if self.private_state_exposed { - bail!("signed daemon health exposes private state"); - } - Ok(()) - } -} - /// Root-owned admission binding. Store ownership is the physical authority; /// this document names the only signer accepted for one daemon/contract/runtime. #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index 014f998..a1001dc 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit 014f998b97c37a34055ce0af069ebaead32d11fb +Subproject commit a1001dc855aa4de04859d01d8be4c966f510f557 From f25d074582df39f86340877e4a1d511c67c06cb3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 22:21:54 +0100 Subject: [PATCH 25/93] Add atomic Idunn trust binding append --- crates/idunn-daemon/src/provisioning.rs | 242 +++++++++++++++++++++++- 1 file changed, 232 insertions(+), 10 deletions(-) diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 80c4d4b..b7798d7 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -13,7 +13,7 @@ use odin_core::{ IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnDaemonHealthTrustBindingRecord, }; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::path::{Path, PathBuf}; trait RootDistributionDocument: DatabaseEntry { @@ -51,13 +51,10 @@ pub fn run(args: impl IntoIterator) -> Result<()> { export_service_identity_trust_anchor(&signer, &public)?; } "create-daemon-health-trust-binding" => create_health_binding(&options)?, + "add-daemon-health-trust-binding" => add_health_binding(&options)?, "validate-daemon-health-trust-binding" => { require_only(&options, &["input"])?; - read_typed::( - &path(&options, "input")?, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - )? - .validate()?; + validate_health_binding_store(&path(&options, "input")?)?; } "create-provider-projection-trust-anchor" => create_projection_anchor(&options)?, "validate-provider-projection-trust-anchor" => validate_projection_anchor(&options)?, @@ -66,7 +63,52 @@ pub fn run(args: impl IntoIterator) -> Result<()> { Ok(()) } +fn validate_health_binding_store(path: &Path) -> Result<()> { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + if entries.is_empty() { + bail!("daemon health trust store is empty"); + } + let mut keys = BTreeSet::new(); + let mut tuples = BTreeSet::new(); + for envelope in entries { + if envelope.r#type != IdunnDaemonHealthTrustBindingRecord::TYPE + || envelope.schema_id.as_deref() != Some(IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA) + { + bail!("trust store contains an alien type or schema"); + } + let binding: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&envelope.payload)?; + if rmp_serde::to_vec(&binding)? != envelope.payload || envelope.key != binding.binding_id { + bail!("trust store contains a noncanonical or mismatched binding"); + } + binding.validate()?; + if !keys.insert(binding.binding_id.clone()) + || !tuples.insert(( + binding.daemon_id, + binding.health_contract, + binding.source_runtime_id, + )) + { + bail!("trust store contains a duplicate binding id or tuple"); + } + } + Ok(()) +} + fn create_health_binding(options: &BTreeMap) -> Result<()> { + let record = health_binding(options)?; + write_new_typed( + &path(options, "output")?, + &record.binding_id, + &record, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + record.bound_at_unix_millis, + ) +} + +fn health_binding( + options: &BTreeMap, +) -> Result { require_only( options, &[ @@ -97,13 +139,91 @@ fn create_health_binding(options: &BTreeMap) -> Result<()> { private_state_exposed: false, }; record.validate()?; - write_new_typed( - &path(options, "output")?, + Ok(record) +} + +fn add_health_binding(options: &BTreeMap) -> Result<()> { + let record = health_binding(options)?; + let output = path(options, "output")?; + let store = SingleFileMessagePackBackingStore::new(&output); + let existing = store.pull_all_read_only_snapshot()?; + if existing.is_empty() { + bail!("trust store must already exist; create the first binding explicitly"); + } + let mut keys = BTreeSet::new(); + let mut tuples = BTreeSet::new(); + let mut expected = Vec::with_capacity(existing.len() + 1); + for envelope in existing { + if envelope.r#type != IdunnDaemonHealthTrustBindingRecord::TYPE + || envelope.schema_id.as_deref() != Some(IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA) + { + bail!("trust store contains an alien type or schema"); + } + let binding: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&envelope.payload)?; + if rmp_serde::to_vec(&binding)? != envelope.payload || envelope.key != binding.binding_id { + bail!("trust store contains a noncanonical or mismatched binding"); + } + binding.validate()?; + if !keys.insert(binding.binding_id.clone()) { + bail!("trust store contains duplicate binding ids"); + } + if !tuples.insert(( + binding.daemon_id, + binding.health_contract, + binding.source_runtime_id, + )) { + bail!("trust store contains duplicate daemon/contract/runtime tuples"); + } + expected.push(CultCacheExpectedEnvelope { + r#type: envelope.r#type.clone(), + key: envelope.key.clone(), + current: Some(envelope), + }); + } + if keys.contains(&record.binding_id) { + bail!("binding id already exists"); + } + if tuples.contains(&( + record.daemon_id.clone(), + record.health_contract.clone(), + record.source_runtime_id.clone(), + )) { + bail!("daemon/contract/runtime tuple already exists"); + } + expected.push(CultCacheExpectedEnvelope { + r#type: IdunnDaemonHealthTrustBindingRecord::TYPE.into(), + key: record.binding_id.clone(), + current: None, + }); + let envelope = typed_envelope( &record.binding_id, &record, IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, record.bound_at_unix_millis, - ) + )?; + if !store.compare_exchange(&expected, &[envelope])? { + bail!("trust store changed during validated append"); + } + Ok(()) +} + +fn typed_envelope( + key: &str, + value: &T, + schema: &str, + millis: u64, +) -> Result { + let stored_at = chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) + .ok_or_else(|| anyhow!("document timestamp is out of range"))? + .to_rfc3339(); + Ok(CultCacheEnvelope { + key: key.into(), + r#type: T::TYPE.into(), + payload: rmp_serde::to_vec(value)?, + stored_at, + schema_id: Some(schema.into()), + }) } fn create_projection_anchor(options: &BTreeMap) -> Result<()> { @@ -354,7 +474,7 @@ fn normalized(path: &Path) -> Result { } fn usage() -> &'static str { - "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " + "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " } #[cfg(test)] @@ -383,6 +503,108 @@ mod tests { }) } + fn binding_args<'a>( + command: &'a str, + output: &'a str, + id: &'a str, + daemon: &'a str, + runtime: &'a str, + ) -> Vec<&'a str> { + vec![ + command, + "--output", + output, + "--binding-id", + id, + "--daemon", + daemon, + "--health-contract", + "provider.health", + "--source-runtime", + runtime, + "--signer-public-key-hex", + "1111111111111111111111111111111111111111111111111111111111111111", + "--bound-at-unix-millis", + "1784483100000", + "--release-binding-required", + "false", + ] + } + + #[test] + fn append_health_binding_preserves_existing_and_rejects_collisions_and_aliens() -> Result<()> { + let temp = TempDir::new()?; + let store = temp.path().join("trust.cc"); + let path = store.to_str().unwrap(); + invoke(&binding_args( + "create-daemon-health-trust-binding", + path, + "one", + "daemon-one", + "runtime-one", + ))?; + let before = SingleFileMessagePackBackingStore::new(&store) + .pull_all_read_only_snapshot()? + .remove(0) + .payload; + invoke(&binding_args( + "add-daemon-health-trust-binding", + path, + "two", + "daemon-two", + "runtime-two", + ))?; + let entries = + SingleFileMessagePackBackingStore::new(&store).pull_all_read_only_snapshot()?; + assert_eq!(entries.len(), 2); + assert_eq!( + entries + .iter() + .find(|entry| entry.key == "one") + .unwrap() + .payload, + before + ); + assert!( + invoke(&binding_args( + "add-daemon-health-trust-binding", + path, + "two", + "daemon-three", + "runtime-three" + )) + .is_err() + ); + assert!( + invoke(&binding_args( + "add-daemon-health-trust-binding", + path, + "three", + "daemon-one", + "runtime-one" + )) + .is_err() + ); + SingleFileMessagePackBackingStore::new(&store).push(&CultCacheEnvelope { + key: "alien".into(), + r#type: "alien".into(), + payload: vec![], + stored_at: "2026-07-19T20:00:00Z".into(), + schema_id: Some("alien.v0".into()), + })?; + assert!( + invoke(&binding_args( + "add-daemon-health-trust-binding", + path, + "four", + "daemon-four", + "runtime-four" + )) + .is_err() + ); + Ok(()) + } + #[test] fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { let temp = TempDir::new()?; From 6067dffce0de280f79e873b9c510716c68a0e9bf Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 22:24:44 +0100 Subject: [PATCH 26/93] Test concurrent Idunn trust binding append --- crates/idunn-daemon/src/provisioning.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index b7798d7..860cb80 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -605,6 +605,24 @@ mod tests { Ok(()) } + #[test] + fn concurrent_health_binding_appends_have_one_cas_winner() -> Result<()> { + let temp = TempDir::new()?; + let store = temp.path().join("trust.cc"); + let path = store.to_str().unwrap().to_string(); + invoke(&binding_args("create-daemon-health-trust-binding", &path, "one", "daemon-one", "runtime-one"))?; + let barrier = std::sync::Arc::new(std::sync::Barrier::new(3)); + let workers = [("two", "daemon-two", "runtime-two"), ("three", "daemon-three", "runtime-three")].map(|(id, daemon, runtime)| { + let path = path.clone(); let barrier = barrier.clone(); + std::thread::spawn(move || { barrier.wait(); invoke(&binding_args("add-daemon-health-trust-binding", &path, id, daemon, runtime)) }) + }); + barrier.wait(); + let wins = workers.into_iter().filter(|worker| worker.join().unwrap().is_ok()).count(); + assert!((1..=2).contains(&wins)); + assert_eq!(SingleFileMessagePackBackingStore::new(&store).pull_all_read_only_snapshot()?.len(), 1 + wins); + Ok(()) + } + #[test] fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { let temp = TempDir::new()?; From 1e51864109c10eba764dfd0cf23a831ff04e6ee8 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 22:24:59 +0100 Subject: [PATCH 27/93] Fix concurrent trust append assertion --- crates/idunn-daemon/src/provisioning.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 860cb80..965aa3c 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -617,7 +617,7 @@ mod tests { std::thread::spawn(move || { barrier.wait(); invoke(&binding_args("add-daemon-health-trust-binding", &path, id, daemon, runtime)) }) }); barrier.wait(); - let wins = workers.into_iter().filter(|worker| worker.join().unwrap().is_ok()).count(); + let wins = workers.into_iter().map(|worker| worker.join().unwrap()).filter(Result::is_ok).count(); assert!((1..=2).contains(&wins)); assert_eq!(SingleFileMessagePackBackingStore::new(&store).pull_all_read_only_snapshot()?.len(), 1 + wins); Ok(()) From 2b1f1ee64eba980caad05205fef80c409fdadbec Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 23:56:42 +0100 Subject: [PATCH 28/93] Gate Idunn actuation with typed deployment brake --- README.md | 9 + crates/idunn-daemon/src/main.rs | 1720 +++++++++++++---------- crates/idunn-daemon/src/provisioning.rs | 420 +++++- scripts/linux/idunn-yggdrasil.service | 2 +- vendor/cultnet-rs | 2 +- 5 files changed, 1369 insertions(+), 784 deletions(-) diff --git a/README.md b/README.md index c0ff2b0..3ef18d9 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,15 @@ Agents do not deploy daemons directly. They configure Idunn's target catalog, release targets, migration commands, and command boundaries so Idunn can run the shared rollout primitive and leave typed witnesses behind. +Deployment consequence has one final owner: immediately before every migration, +deploy, or restart process spawn, Idunn re-opens the root-owned deployment-brake +CultCache and the dedicated operator public anchor. A released record must bind +the canonical runtime (`yggdrasil` for `yggdrasil-local`), exact source revision +(or `restart:`), exact request id, operator, signature, and short expiry. +Missing, corrupt, engaged, expired, foreign, wrong-scope, substituted, or +badly-signed state fails closed. Planning and lifecycle-command publication do +not bypass or cache this decision. + Local package surfaces: - Organ contract: `docs/idunn.md` diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 53877a1..486bb9b 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5,16 +5,18 @@ use cultcache_rs::{ }; use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions}; use cultnet_rs::{ - CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, - CultNetRawPayloadEncoding, CultNetReadOnlySnapshotPolicy, CultNetRudpPacketType, - CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, - CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, - GameCultProviderHealthIdentity, IdunnAuthenticatedProviderHealthProjectionPurpose, - IdunnServiceIdentity, IdunnSignedDaemonHealthPurpose, ServiceIdentitySignature, - ServiceIdentitySigner, decode_cultnet_message_from_slice, decode_rudp_packet, - derive_service_identity_id, encode_cultnet_message_to_vec, encode_rudp_packet, - open_service_identity_at, serve_read_only_raw_snapshot, - verify_service_identity_signature_with_public_key, + CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, + CultNetRawPayloadEncoding, CultNetReadOnlySnapshotPolicy, CultNetRudpPacketType, + CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, + CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, + GameCultProviderHealthIdentity, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, + IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnDeploymentBrakeObservation, + IdunnDeploymentBrakeOperatorIdentity, IdunnDeploymentBrakeRecord, IdunnServiceIdentity, + IdunnSignedDaemonHealthPurpose, ServiceIdentityProfile, ServiceIdentitySignature, + ServiceIdentitySigner, ServiceIdentityTrustAnchor, decode_cultnet_message_from_slice, + decode_rudp_packet, derive_service_identity_id, encode_cultnet_message_to_vec, + encode_rudp_packet, evaluate_idunn_deployment_brake, open_service_identity_at, + serve_read_only_raw_snapshot, verify_service_identity_signature_with_public_key, }; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ @@ -35,7 +37,7 @@ use odin_core::{ authenticated_provider_health_reason_code, plan_keepalive, }; use sha2::{Digest, Sha256}; -use std::collections::{BTreeSet, HashMap}; +use std::collections::{BTreeSet, HashMap}; use std::env; use std::fs::{self, File}; use std::net::{SocketAddr, UdpSocket}; @@ -49,9 +51,9 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0"; const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001; -const IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID: u32 = 0x1d0d_0002; -const IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID: &str = "idunn-daemon"; -const IDUNN_PUBLIC_HEALTH_QUERY_ROLE: &str = "authenticated-provider-health-projector"; +const IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID: u32 = 0x1d0d_0002; +const IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID: &str = "idunn-daemon"; +const IDUNN_PUBLIC_HEALTH_QUERY_ROLE: &str = "authenticated-provider-health-projector"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE: &str = "epiphany.idunn_signed_runtime_health"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = "epiphany.idunn_signed_runtime_health.v0"; @@ -67,10 +69,10 @@ struct TargetActuationGate { impl TargetActuationGate { fn new() -> Self { - Self { - lock: Mutex::new(()), - reserved: AtomicBool::new(false), - } + Self { + lock: Mutex::new(()), + reserved: AtomicBool::new(false), + } } } const HOST_IDENTITY_TYPE: &str = "epiphany.host_identity_trust_anchor.v0"; @@ -504,13 +506,16 @@ fn locally_supervised_health_contract(id: &str, default_failure_state: &str) -> struct CommonOptions { store_path: PathBuf, release_authority_store_path: Option, + deployment_brake_store_path: Option, + deployment_brake_operator_anchor_path: Option, + deployment_brake_runtime_id: Option, operator_alarm_command: Option, rudp_health_bind: Option, trusted_epiphany_health_identity_store: Option, daemon_health_trust_store_path: Option, service_identity_store_path: Option, public_health_store_path: Option, - public_health_query_bind: Option, + public_health_query_bind: Option, execute: bool, command_timeout_seconds: u64, } @@ -732,7 +737,7 @@ fn run_swarm( ); } publish_runtime_transport_check(common, &store_lock, &now)?; - start_public_health_query_listener(common, &targets)?; + start_public_health_query_listener(common, &targets)?; start_rudp_health_ingress(common, &store_lock, &now)?; publish_surgery_plans(&options.profile, &targets, common, &store_lock, &now)?; @@ -742,12 +747,12 @@ fn run_swarm( let actuation_gates = Arc::new( targets .iter() - .map(|target| { - ( - target.daemon_id.clone(), - Arc::new(TargetActuationGate::new()), - ) - }) + .map(|target| { + ( + target.daemon_id.clone(), + Arc::new(TargetActuationGate::new()), + ) + }) .collect::>(), ); @@ -986,12 +991,12 @@ fn run_target_cycle( if plan.deployment_request.is_some() || plan.restart_request.is_some() { schedule_automatic_actuation( - target.clone(), - options.clone(), - Arc::clone(store_lock), - actuation_gate, - plan, - now, + target.clone(), + options.clone(), + Arc::clone(store_lock), + actuation_gate, + plan, + now, ); return Ok(()); } @@ -1018,21 +1023,21 @@ fn schedule_target_actuation( daemon_id: String, action: impl FnOnce() -> Result<()> + Send + 'static, ) { - if gate - .reserved - .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) - .is_err() - { + if gate + .reserved + .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) + .is_err() + { return; } thread::spawn(move || { let result = with_target_actuation_gate(&gate.lock, action); gate.reserved.store(false, Ordering::Release); if let Err(error) = result { - eprintln!( - "Idunn automatic actuation failed for {}: {}", - daemon_id, error - ); + eprintln!( + "Idunn automatic actuation failed for {}: {}", + daemon_id, error + ); } }); } @@ -1055,7 +1060,7 @@ fn finish_target_cycle( target .release .as_ref() - .and_then(|release| run_state_migration(target, release, now, options)) + .and_then(|release| run_state_migration(target, release, request, now, options)) } else { None }; @@ -1413,33 +1418,33 @@ fn persist_current_deployment_request( current_head_request_envelope, current_head_result_envelope, current_request_envelope, - ) = with_store_node(options, store_lock, |node| { - let head = node.get::(&request.daemon_id)?; - Ok(( - head.clone(), - node.cache() - .get_envelope::(&request.daemon_id)?, - head.as_ref() - .map(|head| { - node.cache() - .get_envelope::(&head.request_id) - }) - .transpose()? - .flatten(), - head.as_ref() - .map(|head| { - node.cache() - .get_envelope::(&format!( + ) = with_store_node(options, store_lock, |node| { + let head = node.get::(&request.daemon_id)?; + Ok(( + head.clone(), + node.cache() + .get_envelope::(&request.daemon_id)?, + head.as_ref() + .map(|head| { + node.cache() + .get_envelope::(&head.request_id) + }) + .transpose()? + .flatten(), + head.as_ref() + .map(|head| { + node.cache() + .get_envelope::(&format!( "result:{}", head.request_id )) - }) - .transpose()? - .flatten(), - node.cache() - .get_envelope::(&request.request_id)?, - )) - })?; + }) + .transpose()? + .flatten(), + node.cache() + .get_envelope::(&request.request_id)?, + )) + })?; if let Some(existing) = current_request_envelope.as_ref() { let decoded: IdunnDeploymentRequestRecord = rmp_serde::from_slice(&existing.payload)?; if decoded != *request { @@ -1478,10 +1483,10 @@ fn persist_current_deployment_request( if prior_result.result_id != format!("result:{}", current.request_id) || prior_result.request_id != current.request_id || prior_result.daemon_id != current.daemon_id - || !matches!( - prior_result.state.as_str(), - "succeeded" | "failed" | "skipped" - ) + || !matches!( + prior_result.state.as_str(), + "succeeded" | "failed" | "skipped" + ) { return Err(anyhow!( "current deployment request lacks an exact terminal result" @@ -1569,7 +1574,7 @@ fn terminalize_interrupted_deployment_requests( let request_matches = opening .iter() .filter(|entry| { - entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == head.request_id + entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == head.request_id }) .collect::>(); if request_matches.len() != 1 { @@ -2197,98 +2202,98 @@ struct IdunnProjectionPublisher { write_lock: Mutex<()>, } -#[derive(Clone)] -struct IdunnPublicHealthSnapshotServer { - public_store_path: PathBuf, - allowed_record_keys: BTreeSet, -} - -impl IdunnPublicHealthSnapshotServer { - fn new(public_store_path: PathBuf, targets: &[DaemonTarget]) -> Result { - let allowed_record_keys = targets - .iter() - .map(|target| { - provider_health_projection_key(&target.daemon_id, &target.health_contract.id) - }) - .collect::>(); - if allowed_record_keys.len() != targets.len() { - return Err(anyhow!( - "public health query target catalog contains a duplicate daemon/contract pair" - )); - } - let server = Self { - public_store_path, - allowed_record_keys, - }; - // Startup observes the exact public store before any worker starts. - // An absent store is an empty public projection, not private fallback. - server.raw_snapshot()?; - Ok(server) - } - - fn serve(&self, request: &CultNetMessage) -> Result { - let registry = public_health_document_registry(); - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - for key in &self.allowed_record_keys { - policy.allow(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, key)?; - } - serve_read_only_raw_snapshot(®istry, &policy, &self.raw_snapshot()?, request) - } - - fn raw_snapshot(&self) -> Result> { - let registry = public_health_document_registry(); - let entries = SingleFileMessagePackBackingStore::new(&self.public_store_path) - .pull_all_read_only_snapshot()?; - let mut records = Vec::with_capacity(entries.len()); - for entry in entries { - if entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE - || entry.schema_id.as_deref() - != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) - { - return Err(anyhow!( - "public health query store contains a non-projection envelope" - )); - } - let projection: IdunnAuthenticatedProviderHealthProjectionRecord = - rmp_serde::from_slice(&entry.payload) - .context("decoding public health query projection")?; - if rmp_serde::to_vec(&projection)? != entry.payload - || projection.projection_id != entry.key - { - return Err(anyhow!( - "public health query projection is noncanonical or has a mismatched key" - )); - } - projection.validate()?; - let mut record = registry.raw_document_record_from_envelope(&entry)?; - record.source_runtime_id = Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID.into()); - record.source_agent_id = None; - record.source_role = Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE.into()); - record.tags = Some(vec!["public-health".into()]); - records.push(record); - } - Ok(records) - } -} - -fn public_health_document_registry() -> CultNetDocumentRegistry { - let mut registry = CultNetDocumentRegistry::new(); - registry.register(CultNetDocumentBinding { - schema_id: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - document_type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - mutation_contract: None, - payload_schema_version: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), - }); - registry -} - -fn provider_health_projection_key(daemon_id: &str, health_contract: &str) -> String { - format!( - "provider-health:{:x}", - Sha256::digest([daemon_id.as_bytes(), b"\0", health_contract.as_bytes()].concat()) - ) -} - +#[derive(Clone)] +struct IdunnPublicHealthSnapshotServer { + public_store_path: PathBuf, + allowed_record_keys: BTreeSet, +} + +impl IdunnPublicHealthSnapshotServer { + fn new(public_store_path: PathBuf, targets: &[DaemonTarget]) -> Result { + let allowed_record_keys = targets + .iter() + .map(|target| { + provider_health_projection_key(&target.daemon_id, &target.health_contract.id) + }) + .collect::>(); + if allowed_record_keys.len() != targets.len() { + return Err(anyhow!( + "public health query target catalog contains a duplicate daemon/contract pair" + )); + } + let server = Self { + public_store_path, + allowed_record_keys, + }; + // Startup observes the exact public store before any worker starts. + // An absent store is an empty public projection, not private fallback. + server.raw_snapshot()?; + Ok(server) + } + + fn serve(&self, request: &CultNetMessage) -> Result { + let registry = public_health_document_registry(); + let mut policy = CultNetReadOnlySnapshotPolicy::new(); + for key in &self.allowed_record_keys { + policy.allow(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, key)?; + } + serve_read_only_raw_snapshot(®istry, &policy, &self.raw_snapshot()?, request) + } + + fn raw_snapshot(&self) -> Result> { + let registry = public_health_document_registry(); + let entries = SingleFileMessagePackBackingStore::new(&self.public_store_path) + .pull_all_read_only_snapshot()?; + let mut records = Vec::with_capacity(entries.len()); + for entry in entries { + if entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE + || entry.schema_id.as_deref() + != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) + { + return Err(anyhow!( + "public health query store contains a non-projection envelope" + )); + } + let projection: IdunnAuthenticatedProviderHealthProjectionRecord = + rmp_serde::from_slice(&entry.payload) + .context("decoding public health query projection")?; + if rmp_serde::to_vec(&projection)? != entry.payload + || projection.projection_id != entry.key + { + return Err(anyhow!( + "public health query projection is noncanonical or has a mismatched key" + )); + } + projection.validate()?; + let mut record = registry.raw_document_record_from_envelope(&entry)?; + record.source_runtime_id = Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID.into()); + record.source_agent_id = None; + record.source_role = Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE.into()); + record.tags = Some(vec!["public-health".into()]); + records.push(record); + } + Ok(records) + } +} + +fn public_health_document_registry() -> CultNetDocumentRegistry { + let mut registry = CultNetDocumentRegistry::new(); + registry.register(CultNetDocumentBinding { + schema_id: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + document_type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), + mutation_contract: None, + payload_schema_version: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), + }); + registry +} + +fn provider_health_projection_key(daemon_id: &str, health_contract: &str) -> String { + format!( + "provider-health:{:x}", + Sha256::digest([daemon_id.as_bytes(), b"\0", health_contract.as_bytes()].concat()) + ) +} + #[derive(Clone, Debug, PartialEq, Eq)] struct AuthenticatedProviderHealthSource { health: IdunnDaemonHealthRecord, @@ -2315,8 +2320,8 @@ fn initialize_projection_publisher( options.service_identity_store_path.as_deref(), options.public_health_store_path.as_deref(), ) else { - if options.service_identity_store_path.is_some() - || options.public_health_store_path.is_some() + if options.service_identity_store_path.is_some() + || options.public_health_store_path.is_some() { return Err(anyhow!( "--service-identity-store and --public-health-store must be configured together" @@ -2345,11 +2350,11 @@ fn initialize_projection_publisher( }))) } -fn reject_store_aliases( - options: &CommonOptions, - identity_path: &Path, - public_path: &Path, -) -> Result<()> { +fn reject_store_aliases( + options: &CommonOptions, + identity_path: &Path, + public_path: &Path, +) -> Result<()> { let identity = normalized_store_path(identity_path)?; let public = normalized_store_path(public_path)?; let mut private_paths = vec![normalized_store_path(&options.store_path)?, identity]; @@ -2363,12 +2368,12 @@ fn reject_store_aliases( { private_paths.push(normalized_store_path(path)?); } - if private_paths.iter().enumerate().any(|(index, path)| { - private_paths - .iter() - .skip(index + 1) - .any(|other| other == path) - }) { + if private_paths.iter().enumerate().any(|(index, path)| { + private_paths + .iter() + .skip(index + 1) + .any(|other| other == path) + }) { return Err(anyhow!( "service identity, operational, trust, and release stores must not alias" )); @@ -2411,12 +2416,12 @@ impl IdunnProjectionPublisher { ) -> Result<()> { let evaluated_at_unix_millis = parse_timestamp_millis(evaluated_at) .ok_or_else(|| anyhow!("projection evaluation time is invalid"))?; - let current = - read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? - .and_then(|managed| managed.projection_source) - .ok_or_else(|| { - anyhow!("authenticated provider health vanished before projection") - })?; + let current = + read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? + .and_then(|managed| managed.projection_source) + .ok_or_else(|| { + anyhow!("authenticated provider health vanished before projection") + })?; if ¤t != source { return Err(anyhow!( "authenticated provider health changed before projection publication" @@ -2430,13 +2435,13 @@ impl IdunnProjectionPublisher { .observed_at_unix_millis .saturating_add(u64::from(desired.max_silence_seconds).saturating_mul(1000)); if expires_at_unix_millis <= evaluated_at_unix_millis { - return Err(anyhow!( - "authenticated provider health expired before projection" - )); + return Err(anyhow!( + "authenticated provider health expired before projection" + )); } - let projection_key = provider_health_projection_key( - &source.health.daemon_id, - &source.health.health_contract, + let projection_key = provider_health_projection_key( + &source.health.daemon_id, + &source.health.health_contract, ); let _write_guard = self .write_lock @@ -2450,41 +2455,41 @@ impl IdunnProjectionPublisher { || entry.schema_id.as_deref() != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) }) { - return Err(anyhow!( - "public health store contains a non-projection envelope" - )); + return Err(anyhow!( + "public health store contains a non-projection envelope" + )); } for entry in &entries { self.verify_existing_projection(entry)?; } - let current_envelope = entries - .iter() - .find(|entry| entry.key == projection_key) - .cloned(); + let current_envelope = entries + .iter() + .find(|entry| entry.key == projection_key) + .cloned(); let current_projection = current_envelope .as_ref() - .map(|entry| { - rmp_serde::from_slice::( - &entry.payload, - ) - }) + .map(|entry| { + rmp_serde::from_slice::( + &entry.payload, + ) + }) .transpose() .context("decoding existing public health projection")?; if let Some(existing) = current_projection.as_ref() { existing.validate()?; - if current_envelope - .as_ref() - .is_some_and(|entry| entry.key != existing.projection_id) + if current_envelope + .as_ref() + .is_some_and(|entry| entry.key != existing.projection_id) || existing.projection_id != projection_key { - return Err(anyhow!( - "public projection envelope key does not match its payload" - )); + return Err(anyhow!( + "public projection envelope key does not match its payload" + )); } if existing.idunn_signer_identity_id != self.signer.entry().identity_id { - return Err(anyhow!( - "public projection signer identity changed without store rotation" - )); + return Err(anyhow!( + "public projection signer identity changed without store rotation" + )); } if existing.projection_incarnation_id == self.incarnation_id && existing.signed_health_sha256 == source.statement_sha256 @@ -2502,15 +2507,15 @@ impl IdunnProjectionPublisher { } let mut projection = IdunnAuthenticatedProviderHealthProjectionRecord { schema_version: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - projection_id: projection_key.clone(), - daemon_id: source.health.daemon_id.clone(), + projection_id: projection_key.clone(), + daemon_id: source.health.daemon_id.clone(), health_contract: source.health.health_contract.clone(), - provider_state: source.health.state.clone(), - reason_code: reason_code.into(), + provider_state: source.health.state.clone(), + reason_code: reason_code.into(), provider_observed_at_unix_millis: source.admission.observed_at_unix_millis, admitted_at_unix_millis: source.admission.admitted_at_unix_millis, - evaluated_at_unix_millis, - trust_binding_id: source.binding.binding_id.clone(), + evaluated_at_unix_millis, + trust_binding_id: source.binding.binding_id.clone(), trust_binding_sha256: source.binding_sha256.clone(), signed_health_sha256: source.statement_sha256.clone(), authenticated_admission_sha256: source.admission_sha256.clone(), @@ -2523,25 +2528,25 @@ impl IdunnProjectionPublisher { deployment_id: source.admission.deployment_id.clone(), idunn_runtime_id: "idunn-daemon".into(), idunn_signer_identity_id: self.signer.entry().identity_id.clone(), - projection_incarnation_id: self.incarnation_id.clone(), - projection_sequence: sequence, - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - private_state_exposed: false, - expires_at_unix_millis, + projection_incarnation_id: self.incarnation_id.clone(), + projection_sequence: sequence, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + private_state_exposed: false, + expires_at_unix_millis, }; let unsigned = rmp_serde::to_vec(&projection)?; - projection.signature = self - .signer - .sign::(&unsigned) - .signature; + projection.signature = self + .signer + .sign::(&unsigned) + .signature; projection.validate()?; self.require_incarnation_trust_snapshot(options)?; let replacement = CultCacheEnvelope { key: projection_key.clone(), r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - payload: rmp_serde::to_vec(&projection)?, - stored_at: evaluated_at.into(), + payload: rmp_serde::to_vec(&projection)?, + stored_at: evaluated_at.into(), schema_id: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), }; let expected = CultCacheExpectedEnvelope { @@ -2549,19 +2554,19 @@ impl IdunnProjectionPublisher { r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), current: current_envelope, }; - if backing.compare_exchange(&[expected], &[replacement])? { - return Ok(()); - } + if backing.compare_exchange(&[expected], &[replacement])? { + return Ok(()); + } } - Err(anyhow!( - "public health projection lost repeated cross-process contention" - )) + Err(anyhow!( + "public health projection lost repeated cross-process contention" + )) } fn require_incarnation_trust_snapshot(&self, options: &CommonOptions) -> Result<()> { - let path = options - .daemon_health_trust_store_path - .as_deref() + let path = options + .daemon_health_trust_store_path + .as_deref() .ok_or_else(|| anyhow!("projection publisher lost its root trust store"))?; if trust_store_snapshot_sha256(path)? != self.root_trust_snapshot_sha256 { return Err(anyhow!( @@ -2576,9 +2581,9 @@ impl IdunnProjectionPublisher { rmp_serde::from_slice(&entry.payload) .context("decoding public health projection during store audit")?; if rmp_serde::to_vec(&projection)? != entry.payload { - return Err(anyhow!( - "public health projection is not canonical MessagePack" - )); + return Err(anyhow!( + "public health projection is not canonical MessagePack" + )); } projection.validate()?; if entry.key != projection.projection_id @@ -2605,19 +2610,19 @@ impl IdunnProjectionPublisher { } fn record_sha256(value: &T) -> Result { - Ok(format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(value)?) - )) + Ok(format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(value)?) + )) } fn publish_authenticated_provider_health( - publisher: Option<&IdunnProjectionPublisher>, - options: &CommonOptions, - store_lock: &Arc>, - desired: &IdunnDesiredDaemonRecord, - source: Option<&AuthenticatedProviderHealthSource>, - evaluated_at: &str, + publisher: Option<&IdunnProjectionPublisher>, + options: &CommonOptions, + store_lock: &Arc>, + desired: &IdunnDesiredDaemonRecord, + source: Option<&AuthenticatedProviderHealthSource>, + evaluated_at: &str, ) -> Result<()> { if let (Some(publisher), Some(source)) = (publisher, source) { publisher.publish_if_current(options, store_lock, desired, source, evaluated_at)?; @@ -2844,177 +2849,177 @@ fn start_rudp_health_ingress( Ok(()) } -fn start_public_health_query_listener( - options: &CommonOptions, - targets: &[DaemonTarget], -) -> Result> { - let Some(bind_address) = options.public_health_query_bind else { - return Ok(None); - }; - let public_store_path = options - .public_health_store_path - .clone() - .ok_or_else(|| anyhow!("--public-health-query-bind requires --public-health-store"))?; - if options.service_identity_store_path.is_none() { - return Err(anyhow!( - "--public-health-query-bind requires --service-identity-store projection authority" - )); - } - let server = IdunnPublicHealthSnapshotServer::new(public_store_path, targets)?; - let socket = UdpSocket::bind(bind_address) - .with_context(|| format!("binding public health query listener at {bind_address}"))?; - let local_addr = socket.local_addr()?; - socket.set_read_timeout(Some(Duration::from_millis(250)))?; - thread::spawn(move || { - if let Err(error) = run_public_health_query_listener_loop(socket, server, None) { - eprintln!("Idunn public health query listener stopped: {error:#}"); - } - }); - println!( - "Idunn public health query listener active at {local_addr} for {} target projection key(s).", - targets.len() - ); - Ok(Some(local_addr)) -} - -fn run_public_health_query_listener_loop( - socket: UdpSocket, - server: IdunnPublicHealthSnapshotServer, - stop: Option>, -) -> Result<()> { - let mut sessions: HashMap = HashMap::new(); - let mut buffer = vec![0_u8; 65_535]; - loop { - if stop - .as_ref() - .is_some_and(|flag| flag.load(Ordering::Acquire)) - { - return Ok(()); - } - match socket.recv_from(&mut buffer) { - Ok((size, source)) => { - if let Err(error) = handle_public_health_query_datagram( - &socket, - &mut sessions, - &server, - &buffer[..size], - source, - ) { - eprintln!( - "Idunn public health query listener refused datagram from {source}: {error:#}" - ); - } - } - Err(error) - if matches!( - error.kind(), - std::io::ErrorKind::WouldBlock - | std::io::ErrorKind::TimedOut - | std::io::ErrorKind::ConnectionReset - | std::io::ErrorKind::ConnectionAborted - ) => {} - Err(error) => return Err(error.into()), - } - let now = unix_epoch_millis()?; - let mut expired = Vec::new(); - for (peer, session) in &mut sessions { - for packet in session.due_resends(now) { - socket.send_to(&encode_rudp_packet(&packet)?, peer)?; - } - if session.check_timeout(now, 60_000) { - expired.push(*peer); - } - } - for peer in expired { - sessions.remove(&peer); - } - } -} - -fn handle_public_health_query_datagram( - socket: &UdpSocket, - sessions: &mut HashMap, - server: &IdunnPublicHealthSnapshotServer, - wire: &[u8], - source: SocketAddr, -) -> Result<()> { - let packet = decode_rudp_packet(wire)?; - if packet.connection_id != IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID { - return Err(anyhow!( - "unexpected public health query RUDP connection id {:08x}", - packet.connection_id - )); - } - let now = unix_epoch_millis()?; - if packet.packet_type == CultNetRudpPacketType::Connect { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, - initial_sequence: 1, - resend_delay_ms: 100, - max_pending_reliable_packets: Some(64), - }); - let accept = session.accept_connect(&packet, now, Vec::new())?; - socket.send_to(&encode_rudp_packet(&accept)?, source)?; - sessions.insert(source, session); - return Ok(()); - } - let session = sessions - .get_mut(&source) - .ok_or_else(|| anyhow!("public health query peer has no accepted RUDP session"))?; - let result = session.receive(&packet, now)?; - if let Some(reply) = result.reply { - socket.send_to(&encode_rudp_packet(&reply)?, source)?; - } - if result.disconnected { - sessions.remove(&source); - return Ok(()); - } - if packet.packet_type == CultNetRudpPacketType::Accept || !result.delivered.is_empty() { - socket.send_to(&encode_rudp_packet(&session.create_ack())?, source)?; - } - for frame in result.delivered { - let response = if frame.channel_id != "schema" { - CultNetMessage::Error { - error: "public health queries require the CultNet schema channel".into(), - } - } else { - match decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) { - Ok(request) => { - server - .serve(&request) - .unwrap_or_else(|error| CultNetMessage::Error { - error: format!("public health query refused: {error}"), - }) - } - Err(error) => CultNetMessage::Error { - error: format!("public health query message is malformed: {error}"), - }, - } - }; - let payload = - encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; - let packets = session.send_many( - "schema", - payload, - CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms: now, - reliable_expire_after_ms: None, - }, - Some(1200), - )?; - for packet in packets { - socket.send_to(&encode_rudp_packet(&packet)?, source)?; - } - } - Ok(()) -} - +fn start_public_health_query_listener( + options: &CommonOptions, + targets: &[DaemonTarget], +) -> Result> { + let Some(bind_address) = options.public_health_query_bind else { + return Ok(None); + }; + let public_store_path = options + .public_health_store_path + .clone() + .ok_or_else(|| anyhow!("--public-health-query-bind requires --public-health-store"))?; + if options.service_identity_store_path.is_none() { + return Err(anyhow!( + "--public-health-query-bind requires --service-identity-store projection authority" + )); + } + let server = IdunnPublicHealthSnapshotServer::new(public_store_path, targets)?; + let socket = UdpSocket::bind(bind_address) + .with_context(|| format!("binding public health query listener at {bind_address}"))?; + let local_addr = socket.local_addr()?; + socket.set_read_timeout(Some(Duration::from_millis(250)))?; + thread::spawn(move || { + if let Err(error) = run_public_health_query_listener_loop(socket, server, None) { + eprintln!("Idunn public health query listener stopped: {error:#}"); + } + }); + println!( + "Idunn public health query listener active at {local_addr} for {} target projection key(s).", + targets.len() + ); + Ok(Some(local_addr)) +} + +fn run_public_health_query_listener_loop( + socket: UdpSocket, + server: IdunnPublicHealthSnapshotServer, + stop: Option>, +) -> Result<()> { + let mut sessions: HashMap = HashMap::new(); + let mut buffer = vec![0_u8; 65_535]; + loop { + if stop + .as_ref() + .is_some_and(|flag| flag.load(Ordering::Acquire)) + { + return Ok(()); + } + match socket.recv_from(&mut buffer) { + Ok((size, source)) => { + if let Err(error) = handle_public_health_query_datagram( + &socket, + &mut sessions, + &server, + &buffer[..size], + source, + ) { + eprintln!( + "Idunn public health query listener refused datagram from {source}: {error:#}" + ); + } + } + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::WouldBlock + | std::io::ErrorKind::TimedOut + | std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::ConnectionAborted + ) => {} + Err(error) => return Err(error.into()), + } + let now = unix_epoch_millis()?; + let mut expired = Vec::new(); + for (peer, session) in &mut sessions { + for packet in session.due_resends(now) { + socket.send_to(&encode_rudp_packet(&packet)?, peer)?; + } + if session.check_timeout(now, 60_000) { + expired.push(*peer); + } + } + for peer in expired { + sessions.remove(&peer); + } + } +} + +fn handle_public_health_query_datagram( + socket: &UdpSocket, + sessions: &mut HashMap, + server: &IdunnPublicHealthSnapshotServer, + wire: &[u8], + source: SocketAddr, +) -> Result<()> { + let packet = decode_rudp_packet(wire)?; + if packet.connection_id != IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID { + return Err(anyhow!( + "unexpected public health query RUDP connection id {:08x}", + packet.connection_id + )); + } + let now = unix_epoch_millis()?; + if packet.packet_type == CultNetRudpPacketType::Connect { + let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { + connection_id: IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, + initial_sequence: 1, + resend_delay_ms: 100, + max_pending_reliable_packets: Some(64), + }); + let accept = session.accept_connect(&packet, now, Vec::new())?; + socket.send_to(&encode_rudp_packet(&accept)?, source)?; + sessions.insert(source, session); + return Ok(()); + } + let session = sessions + .get_mut(&source) + .ok_or_else(|| anyhow!("public health query peer has no accepted RUDP session"))?; + let result = session.receive(&packet, now)?; + if let Some(reply) = result.reply { + socket.send_to(&encode_rudp_packet(&reply)?, source)?; + } + if result.disconnected { + sessions.remove(&source); + return Ok(()); + } + if packet.packet_type == CultNetRudpPacketType::Accept || !result.delivered.is_empty() { + socket.send_to(&encode_rudp_packet(&session.create_ack())?, source)?; + } + for frame in result.delivered { + let response = if frame.channel_id != "schema" { + CultNetMessage::Error { + error: "public health queries require the CultNet schema channel".into(), + } + } else { + match decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + ) { + Ok(request) => { + server + .serve(&request) + .unwrap_or_else(|error| CultNetMessage::Error { + error: format!("public health query refused: {error}"), + }) + } + Err(error) => CultNetMessage::Error { + error: format!("public health query message is malformed: {error}"), + }, + } + }; + let payload = + encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; + let packets = session.send_many( + "schema", + payload, + CultNetRudpSendOptions { + reliable: true, + ordered: true, + sequenced: false, + now_ms: now, + reliable_expire_after_ms: None, + }, + Some(1200), + )?; + for packet in packets { + socket.send_to(&encode_rudp_packet(&packet)?, source)?; + } + } + Ok(()) +} + fn rudp_health_ingress_record( ingress_id: &str, bind_address: SocketAddr, @@ -3393,9 +3398,9 @@ fn authenticate_generic_signed_health( || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) || document.source_role.as_deref() != Some("daemon-health-publisher") { - return Err(anyhow!( - "signed daemon health transport identity is invalid" - )); + return Err(anyhow!( + "signed daemon health transport identity is invalid" + )); } let (binding, binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; if binding.release_binding_required { @@ -3404,9 +3409,9 @@ fn authenticate_generic_signed_health( || statement.source_commit.is_none() || statement.deployment_id.is_none() { - return Err(anyhow!( - "signed daemon health lacks required release binding" - )); + return Err(anyhow!( + "signed daemon health lacks required release binding" + )); } } else if statement.release_id.is_some() || statement.release_witness_sha256.is_some() @@ -3417,8 +3422,8 @@ fn authenticate_generic_signed_health( "signed daemon health carries release authority not declared by its trust binding" )); } - let expected_identity = - derive_service_identity_id::(&binding.signer_public_key)?; + let expected_identity = + derive_service_identity_id::(&binding.signer_public_key)?; if binding.signer_identity_id != expected_identity || statement.signer_identity_id != binding.signer_identity_id { @@ -3450,9 +3455,9 @@ fn authenticate_generic_signed_health( || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) > EPIPHANY_ADMISSION_MAX_AGE_SECONDS.saturating_mul(1000) { - return Err(anyhow!( - "signed daemon health observation is future or stale" - )); + return Err(anyhow!( + "signed daemon health observation is future or stale" + )); } let signed_health_sha256 = format!("sha256-{:x}", Sha256::digest(&document.payload)); let observed_at = chrono::DateTime::from_timestamp_millis( @@ -3539,8 +3544,8 @@ fn admit_generic_signed_health( for _ in 0..8 { let (expected_health, expected_admission, expected_statement) = with_store_node(options, store_lock, |node| { - if let Some(existing) = - node.get::(&admission.daemon_id)? + if let Some(existing) = + node.get::(&admission.daemon_id)? && !generic_signed_health_advances(&existing, &admission) { return Err(anyhow!("signed daemon health is replayed or regressed")); @@ -3553,9 +3558,9 @@ fn admit_generic_signed_health( .get_envelope::( &admission.daemon_id, )?, - node.cache().get_envelope::( - &admission.signed_health_sha256, - )?, + node.cache().get_envelope::( + &admission.signed_health_sha256, + )?, )) })?; let expected = [ @@ -3631,17 +3636,17 @@ fn validate_generic_release_binding( || request.release_authority_id.trim().is_empty() || request.release_authority_envelope_sha256.trim().is_empty() { - return Err(anyhow!( - "signed daemon health deployment authority is invalid" - )); + return Err(anyhow!( + "signed daemon health deployment authority is invalid" + )); } let current = node .get::(&admission.daemon_id)? .ok_or_else(|| anyhow!("signed daemon health has no current deployment head"))?; if current.request_id != request.request_id || current.sequence == 0 { - return Err(anyhow!( - "signed daemon health names a superseded deployment" - )); + return Err(anyhow!( + "signed daemon health names a superseded deployment" + )); } let requested_at = parse_timestamp_millis(&request.requested_at) .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; @@ -3707,8 +3712,8 @@ fn decode_health_from_rudp_message( if document.record_key != wire.daemon_id { return Err(anyhow!( "record key {} does not match health daemon_id {}", - document.record_key, - wire.daemon_id + document.record_key, + wire.daemon_id )); } let received_at_unix_millis = parse_timestamp_millis(admitted_at) @@ -5325,13 +5330,16 @@ impl Options { }); let mut store_path = PathBuf::from("scratch/idunn/idunn.keepalive.cc"); let mut release_authority_store_path = None; + let mut deployment_brake_store_path = None; + let mut deployment_brake_operator_anchor_path = None; + let mut deployment_brake_runtime_id = None; let mut operator_alarm_command = None; let mut rudp_health_bind = None; let mut trusted_epiphany_health_identity_store = None; let mut daemon_health_trust_store_path = None; let mut service_identity_store_path = None; let mut public_health_store_path = None; - let mut public_health_query_bind = None; + let mut public_health_query_bind = None; let mut execute = false; let mut command_timeout_seconds = 30; let mut daemon_id = None; @@ -5370,6 +5378,22 @@ impl Options { "--release-authority-store", )?)) } + "--deployment-brake-store" => { + deployment_brake_store_path = Some(PathBuf::from(take_value( + &mut args, + "--deployment-brake-store", + )?)) + } + "--deployment-brake-operator-anchor" => { + deployment_brake_operator_anchor_path = Some(PathBuf::from(take_value( + &mut args, + "--deployment-brake-operator-anchor", + )?)) + } + "--deployment-brake-runtime-id" => { + deployment_brake_runtime_id = + Some(take_value(&mut args, "--deployment-brake-runtime-id")?) + } "--repository-full-name" => { validation_repository_full_name = Some(take_value(&mut args, "--repository-full-name")?) @@ -5454,15 +5478,15 @@ impl Options { "--public-health-store", )?)); } - "--public-health-query-bind" => { - public_health_query_bind = Some( - take_value(&mut args, "--public-health-query-bind")? - .parse() - .with_context( - || "--public-health-query-bind must be a socket address", - )?, - ); - } + "--public-health-query-bind" => { + public_health_query_bind = Some( + take_value(&mut args, "--public-health-query-bind")? + .parse() + .with_context( + || "--public-health-query-bind must be a socket address", + )?, + ); + } "--disabled" => enabled = false, "--execute" => execute = true, "--interval-seconds" => { @@ -5502,13 +5526,19 @@ impl Options { let common = CommonOptions { store_path, release_authority_store_path, + deployment_brake_store_path, + deployment_brake_operator_anchor_path, + deployment_brake_runtime_id: deployment_brake_runtime_id.or_else(|| { + (swarm_profile.as_deref() == Some("yggdrasil-local")) + .then(|| "yggdrasil".to_string()) + }), operator_alarm_command, rudp_health_bind, trusted_epiphany_health_identity_store, daemon_health_trust_store_path, service_identity_store_path, public_health_store_path, - public_health_query_bind, + public_health_query_bind, execute, command_timeout_seconds, }; @@ -5576,6 +5606,14 @@ impl Options { "--trusted-epiphany-health-identity-store is required for yggdrasil-local" )); } + if swarm_profile.as_deref() == Some("yggdrasil-local") + && (common.deployment_brake_store_path.is_none() + || common.deployment_brake_operator_anchor_path.is_none()) + { + return Err(anyhow!( + "--deployment-brake-store and --deployment-brake-operator-anchor are required for yggdrasil-local" + )); + } if let Some(action) = lifecycle_action { if swarm_profile.is_some() { @@ -5634,20 +5672,20 @@ impl Options { } }; - if common.public_health_query_bind.is_some() && !matches!(mode, Mode::Swarm(_)) { - return Err(anyhow!( - "--public-health-query-bind requires --swarm-profile" - )); - } - if common.public_health_query_bind.is_some() - && (common.public_health_store_path.is_none() - || common.service_identity_store_path.is_none()) - { - return Err(anyhow!( - "--public-health-query-bind requires --public-health-store and --service-identity-store" - )); - } - + if common.public_health_query_bind.is_some() && !matches!(mode, Mode::Swarm(_)) { + return Err(anyhow!( + "--public-health-query-bind requires --swarm-profile" + )); + } + if common.public_health_query_bind.is_some() + && (common.public_health_store_path.is_none() + || common.service_identity_store_path.is_none()) + { + return Err(anyhow!( + "--public-health-query-bind requires --public-health-store and --service-identity-store" + )); + } + Ok(Self { common, mode }) } } @@ -5695,7 +5733,13 @@ fn run_restart( options: &CommonOptions, ) -> IdunnRestartResultRecord { let result_id = format!("result:{}", request.request_id); - match run_shell(&request.command, options, None) { + match run_braked_shell( + &request.command, + options, + &format!("restart:{}", request.daemon_id), + &request.request_id, + None, + ) { Ok(output) if output.status.success() => IdunnRestartResultRecord { result_id, request_id: request.request_id.clone(), @@ -5743,7 +5787,13 @@ fn run_deployment( completed_at: requested_at.to_string(), }; } - match run_shell(&request.command, options, Some(request)) { + match run_braked_shell( + &request.command, + options, + &request.source_revision, + &request.request_id, + Some(request), + ) { Ok(output) if output.status.success() => IdunnDeploymentResultRecord { result_id, request_id: request.request_id.clone(), @@ -5814,6 +5864,7 @@ fn truncate_detail(value: &str, max_chars: usize) -> String { fn run_state_migration( target: &DaemonTarget, release: &ReleaseTarget, + deployment: &IdunnDeploymentRequestRecord, requested_at: &str, options: &CommonOptions, ) -> Option { @@ -5829,7 +5880,15 @@ fn run_state_migration( completed_at: requested_at.to_string(), }); }; - match run_shell(command, options, None) { + // Migration is consequential deployment work. It consumes the same exact + // release/request grant as the deployment spawn that follows it. + match run_braked_shell( + command, + options, + &deployment.source_revision, + &deployment.request_id, + Some(deployment), + ) { Ok(output) if output.status.success() => Some(IdunnStateMigrationResultRecord { result_id, plan_id, @@ -5891,6 +5950,8 @@ fn rollout_result_record( fn run_shell( command: &str, options: &CommonOptions, + release_id: &str, + deployment_id: &str, deployment: Option<&IdunnDeploymentRequestRecord>, ) -> Result { let mut process = if cfg!(windows) { @@ -5946,6 +6007,9 @@ fn run_shell( .with_context(|| format!("creating {}", stderr_path.display()))?; configure_command_lifetime(&mut process); + // This is deliberately the last fallible authority read before spawn. + // Preparation above has no consequence outside Idunn's temporary files. + verify_deployment_brake(options, release_id, deployment_id)?; let child = process .stdout(Stdio::from(stdout_file)) .stderr(Stdio::from(stderr_file)) @@ -5967,6 +6031,95 @@ fn run_shell( }) } +/// The single final consequence gate. Callers may plan and persist before this, +/// but every migration, deployment, and restart re-opens both root-owned files +/// here, immediately before `Command::spawn`. +fn run_braked_shell( + command: &str, + options: &CommonOptions, + release_id: &str, + deployment_id: &str, + deployment: Option<&IdunnDeploymentRequestRecord>, +) -> Result { + run_shell(command, options, release_id, deployment_id, deployment) +} + +fn verify_deployment_brake( + options: &CommonOptions, + release_id: &str, + deployment_id: &str, +) -> Result<()> { + let store_path = options + .deployment_brake_store_path + .as_ref() + .ok_or_else(|| anyhow!("--deployment-brake-store is required for actuation"))?; + let anchor_path = options + .deployment_brake_operator_anchor_path + .as_ref() + .ok_or_else(|| anyhow!("--deployment-brake-operator-anchor is required for actuation"))?; + let runtime_id = options + .deployment_brake_runtime_id + .as_deref() + .ok_or_else(|| anyhow!("deployment brake runtime id is not configured"))?; + + let entries = SingleFileMessagePackBackingStore::new(store_path) + .pull_all_read_only_snapshot() + .with_context(|| format!("reading deployment brake {}", store_path.display()))?; + let observation = match entries.as_slice() { + [entry] + if entry.r#type == IdunnDeploymentBrakeRecord::TYPE + && entry.key == cultnet_rs::IDUNN_DEPLOYMENT_BRAKE_ID + && entry.schema_id.as_deref() == Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA) => + { + match rmp_serde::from_slice::(&entry.payload) { + Ok(record) + if rmp_serde::to_vec(&record).ok().as_deref() + == Some(entry.payload.as_slice()) => + { + record + } + _ => return Err(anyhow!("deployment brake is corrupt")), + } + } + [] => return Err(anyhow!("deployment brake is missing")), + _ => return Err(anyhow!("deployment brake store is corrupt or ambiguous")), + }; + let anchor_entries = SingleFileMessagePackBackingStore::new(anchor_path) + .pull_all_read_only_snapshot() + .with_context(|| { + format!( + "reading deployment brake operator anchor {}", + anchor_path.display() + ) + })?; + let [anchor_entry] = anchor_entries.as_slice() else { + return Err(anyhow!( + "deployment brake operator anchor is missing or ambiguous" + )); + }; + if anchor_entry.r#type != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_TYPE + || anchor_entry.key != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_KEY + || anchor_entry.schema_id.as_deref() + != Some(IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_SCHEMA) + { + return Err(anyhow!("deployment brake operator anchor is foreign")); + } + let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&anchor_entry.payload) + .context("decoding deployment brake operator anchor")?; + if rmp_serde::to_vec(&anchor)? != anchor_entry.payload { + return Err(anyhow!("deployment brake operator anchor is noncanonical")); + } + evaluate_idunn_deployment_brake( + IdunnDeploymentBrakeObservation::Present(&observation), + &anchor, + runtime_id, + release_id, + deployment_id, + unix_epoch_millis()?, + ) + .map_err(|denial| anyhow!("deployment brake denied actuation: {denial:?}")) +} + fn actuator_idunn_rudp_health_endpoint(options: &CommonOptions) -> Option { let bind = options.rudp_health_bind?; let host = if bind.ip().is_unspecified() { @@ -6179,7 +6332,7 @@ fn unix_epoch_millis() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." } #[cfg(test)] @@ -6213,7 +6366,7 @@ mod tests { .unwrap(); }); assert!( - entered_rx.recv_timeout(Duration::from_millis(100)).is_err(), + entered_rx.recv_timeout(Duration::from_millis(100)).is_err(), "automatic request owner entered while manual actuation was live" ); release_tx.send(()).unwrap(); @@ -6233,11 +6386,11 @@ mod tests { Ok(()) }); - assert!( - actuation_rx - .recv_timeout(Duration::from_millis(100)) - .is_err() - ); + assert!( + actuation_rx + .recv_timeout(Duration::from_millis(100)) + .is_err() + ); // This is the target loop's independent signed-health admission lane: // it does not acquire or await the consequence gate. let admitted_health = "accepted"; @@ -6271,8 +6424,8 @@ mod tests { #[cfg(unix)] #[test] fn status_timeout_terminates_command_descendants() { - let marker = - env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); + let marker = + env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); let _ = fs::remove_file(&marker); let error = wait_for_child_status_with_timeout( descendant_marker_command(&marker), @@ -6282,17 +6435,17 @@ mod tests { .expect_err("command should time out"); assert!(error.to_string().contains("timed out")); thread::sleep(Duration::from_millis(1200)); - assert!( - !marker.exists(), - "timed-out descendant survived to write marker" - ); + assert!( + !marker.exists(), + "timed-out descendant survived to write marker" + ); } #[cfg(unix)] #[test] fn output_timeout_terminates_command_descendants() { - let marker = - env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); + let marker = + env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); let _ = fs::remove_file(&marker); let error = wait_for_child_with_timeout( descendant_marker_command(&marker), @@ -6302,10 +6455,10 @@ mod tests { .expect_err("command should time out"); assert!(error.to_string().contains("timed out")); thread::sleep(Duration::from_millis(1200)); - assert!( - !marker.exists(), - "timed-out descendant survived to write marker" - ); + assert!( + !marker.exists(), + "timed-out descendant survived to write marker" + ); } use cultnet_rs::{CultNetRawDocumentRecord, CultNetRawPayloadEncoding}; use ed25519_dalek::{Signer, SigningKey}; @@ -6551,13 +6704,16 @@ mod tests { let options = CommonOptions { store_path: env::temp_dir().join("unused-idunn-store.cc"), release_authority_store_path: Some(path.clone()), + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: true, command_timeout_seconds: 1, }; @@ -7502,11 +7658,11 @@ mod tests { )); assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); - assert!( - actuator.contains( - "epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true" - ) - ); + assert!( + actuator.contains( + "epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true" + ) + ); assert!(actuator.contains( "voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false" )); @@ -7572,9 +7728,9 @@ mod tests { } fn deployed_revision(&self, _release: &ReleaseTarget) -> Result { - Err( - std::io::Error::new(std::io::ErrorKind::NotFound, "deployment witness is absent") - .into(), + Err( + std::io::Error::new(std::io::ErrorKind::NotFound, "deployment witness is absent") + .into(), ) } } @@ -7695,11 +7851,11 @@ mod tests { assert_eq!(health.state, "stale-deployment"); assert_eq!(health.transport, "idunn.release-not-deployed"); - assert!( - health - .detail - .contains("no deployed revision witness exists") - ); + assert!( + health + .detail + .contains("no deployed revision witness exists") + ); assert!( plan_keepalive(&desired, &health, "unix:100") .deployment_request @@ -7806,18 +7962,18 @@ mod tests { ); assert_eq!( target.deploy_command.as_deref(), - Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" - ) + Some( + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + ) ); assert!(target.restart_command.is_none()); let release = target.release.as_ref().expect("release target"); assert!(release.requires_bifrost_authority); assert_eq!(release.repository_full_name, "GameCult/Bifrost"); - assert_eq!( - release.repo_path, - PathBuf::from("/srv/build/Bifrost-persona-feedback") - ); + assert_eq!( + release.repo_path, + PathBuf::from("/srv/build/Bifrost-persona-feedback") + ); let desired = IdunnDesiredDaemonRecord { daemon_id: target.daemon_id.clone(), @@ -7907,13 +8063,16 @@ mod tests { let options = CommonOptions { store_path: store_path.clone(), release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -7983,13 +8142,16 @@ mod tests { let options = CommonOptions { store_path: store_path.clone(), release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -8549,27 +8711,30 @@ mod tests { let options = CommonOptions { store_path: root.path().join("idunn.cc"), release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; let lock = Arc::new(Mutex::new(())); - let outcome = - admit_health_from_rudp_message(&message, &options, &lock, "2026-06-15T00:00:01Z") - .unwrap(); + let outcome = + admit_health_from_rudp_message(&message, &options, &lock, "2026-06-15T00:00:01Z") + .unwrap(); assert_eq!(outcome.authority, "diagnostic-only"); assert!(health_from_rudp_message(&message, &options).is_err()); with_store_node(&options, &lock, |node| { - assert!( - node.get::(&health.daemon_id)? - .is_none() - ); + assert!( + node.get::(&health.daemon_id)? + .is_none() + ); let diagnostic = node .get::(&format!( "diagnostic:{}", @@ -8618,9 +8783,9 @@ mod tests { SingleFileMessagePackBackingStore::new(&trust_path) .push(&typed_envelope(&binding.binding_id, &binding, "unix:1784483100").unwrap()) .unwrap(); - let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00Z") - .unwrap() - .timestamp_millis() as u64; + let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00Z") + .unwrap() + .timestamp_millis() as u64; let mut statement = IdunnSignedDaemonHealthRecord { schema_version: odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), daemon_id: binding.daemon_id.clone(), @@ -8633,7 +8798,7 @@ mod tests { publisher_sequence: sequence, observed_at_unix_millis, release_id: release_bound.then(|| "release-test-1".into()), - release_witness_sha256: release_bound.then(|| format!("sha256-{}", "a".repeat(64))), + release_witness_sha256: release_bound.then(|| format!("sha256-{}", "a".repeat(64))), source_commit: release_bound.then(|| "b".repeat(40)), deployment_id: release_bound.then(|| "deploy-test-1".into()), signature_algorithm: "ed25519".into(), @@ -8691,13 +8856,16 @@ mod tests { CommonOptions { store_path, release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: Some(trust_path), service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }, @@ -8854,233 +9022,233 @@ mod tests { rmp_serde::from_slice(&entries[0].payload).unwrap() } - fn public_query_target(desired: &IdunnDesiredDaemonRecord) -> DaemonTarget { - DaemonTarget { - daemon_id: desired.daemon_id.clone(), - verse_id: desired.verse_id.clone(), - name: desired.name.clone(), - health_contract: health_contract(&desired.health_contract, "failed"), - deploy_command: None, - restart_command: None, - release: None, - enabled: true, - interval_seconds: 30, - } - } - - fn connect_public_query_client( - server_addr: SocketAddr, - ) -> CultNetRudpSocketTransportConnection { - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( - "epiphany-test-consumer", - socket, - server_addr, - IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, - )) - .unwrap(); - client.connect(Vec::new()).unwrap(); - let deadline = Instant::now() + Duration::from_secs(2); - while !client.connected() { - client.receive_once().unwrap(); - client.poll_resends().unwrap(); - assert!( - Instant::now() < deadline, - "public query handshake timed out" - ); - } - client - } - - fn receive_public_query_message( - client: &mut CultNetRudpSocketTransportConnection, - ) -> CultNetMessage { - let deadline = Instant::now() + Duration::from_secs(2); - loop { - if let Some(frame) = client.receive_once().unwrap() { - assert_eq!(frame.channel_id, "schema"); - return decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) - .unwrap(); - } - client.poll_resends().unwrap(); - assert!(Instant::now() < deadline, "public query response timed out"); - } - } - - #[test] - fn public_query_server_returns_exact_allowlisted_bytes_and_correlation() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let public_path = options.public_health_store_path.as_ref().unwrap(); - let stored = SingleFileMessagePackBackingStore::new(public_path) - .pull_all_read_only_snapshot() - .unwrap() - .remove(0); - let server = IdunnPublicHealthSnapshotServer::new( - public_path.clone(), - &[public_query_target(&desired)], - ) - .unwrap(); - let request = CultNetMessage::SnapshotRequest { - message_id: "status-correlation-7".into(), - schema_ids: Some(vec![ - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - ]), - record_keys: Some(vec![stored.key.clone()]), - }; - let CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } = server.serve(&request).unwrap() - else { - panic!("expected raw public health snapshot"); - }; - assert_eq!(message_id, "status-correlation-7"); - assert_eq!(documents.len(), 1); - assert_eq!(documents[0].payload, stored.payload); - assert_eq!( - documents[0].source_runtime_id.as_deref(), - Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID) - ); - assert_eq!( - documents[0].source_role.as_deref(), - Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE) - ); - - for request in [ - CultNetMessage::SnapshotRequest { - message_id: "unknown-schema".into(), - schema_ids: Some(vec!["private.idunn.state.v0".into()]), - record_keys: None, - }, - CultNetMessage::SnapshotRequest { - message_id: "unknown-key".into(), - schema_ids: None, - record_keys: Some(vec!["provider-health:not-in-target-catalog".into()]), - }, - ] { - let CultNetMessage::SnapshotResponseRaw { documents, .. } = - server.serve(&request).unwrap() - else { - panic!("expected bounded empty response"); - }; - assert!(documents.is_empty()); - } - } - - #[test] - fn public_query_store_contamination_is_fatal_and_private_type_is_never_served() { - let (options, _lock, desired, _source, _publisher, root) = projection_fixture(false); - let private_path = root.path().join("private-in-public.cc"); - let private = IdunnDaemonHealthRecord { - daemon_id: desired.daemon_id.clone(), - state: "active".into(), - detail: "private managed judgment".into(), - observed_at: "2026-07-19T12:00:02Z".into(), - health_contract: desired.health_contract.clone(), - publication_source: "daemon-authenticated".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }; - SingleFileMessagePackBackingStore::new(&private_path) - .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:02Z").unwrap()) - .unwrap(); - assert!( - IdunnPublicHealthSnapshotServer::new(private_path, &[public_query_target(&desired)]) - .is_err() - ); - - let public_path = options.public_health_store_path.as_ref().unwrap(); - let server = IdunnPublicHealthSnapshotServer::new( - public_path.clone(), - &[public_query_target(&desired)], - ) - .unwrap(); - SingleFileMessagePackBackingStore::new(public_path) - .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:03Z").unwrap()) - .unwrap(); - assert!( - server - .serve(&CultNetMessage::SnapshotRequest { - message_id: "private-probe".into(), - schema_ids: None, - record_keys: None, - }) - .is_err() - ); - } - - #[test] - fn public_query_rudp_is_multi_peer_and_survives_malformed_request() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let server = IdunnPublicHealthSnapshotServer::new( - options.public_health_store_path.clone().unwrap(), - &[public_query_target(&desired)], - ) - .unwrap(); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - let server_addr = socket.local_addr().unwrap(); - socket - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let stop = Arc::new(AtomicBool::new(false)); - let worker_stop = Arc::clone(&stop); - let worker = thread::spawn(move || { - run_public_health_query_listener_loop(socket, server, Some(worker_stop)) - }); - - let mut hostile = connect_public_query_client(server_addr); - hostile.send("schema", vec![0xc1]).unwrap(); - assert!(matches!( - receive_public_query_message(&mut hostile), - CultNetMessage::Error { .. } - )); - - let mut first = connect_public_query_client(server_addr); - let mut second = connect_public_query_client(server_addr); - for (client, id) in [(&mut first, "first-peer"), (&mut second, "second-peer")] { - let request = CultNetMessage::SnapshotRequest { - message_id: id.into(), - schema_ids: Some(vec![ - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - ]), - record_keys: Some(vec![provider_health_projection_key( - &desired.daemon_id, - &desired.health_contract, - )]), - }; - client - .send( - "schema", - encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0) - .unwrap(), - ) - .unwrap(); - let CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } = receive_public_query_message(client) - else { - panic!("expected peer snapshot response"); - }; - assert_eq!(message_id, id); - assert_eq!(documents.len(), 1); - } - stop.store(true, Ordering::Release); - worker.join().unwrap().unwrap(); - } - + fn public_query_target(desired: &IdunnDesiredDaemonRecord) -> DaemonTarget { + DaemonTarget { + daemon_id: desired.daemon_id.clone(), + verse_id: desired.verse_id.clone(), + name: desired.name.clone(), + health_contract: health_contract(&desired.health_contract, "failed"), + deploy_command: None, + restart_command: None, + release: None, + enabled: true, + interval_seconds: 30, + } + } + + fn connect_public_query_client( + server_addr: SocketAddr, + ) -> CultNetRudpSocketTransportConnection { + let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); + socket + .set_read_timeout(Some(Duration::from_millis(100))) + .unwrap(); + let mut client = + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( + "epiphany-test-consumer", + socket, + server_addr, + IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, + )) + .unwrap(); + client.connect(Vec::new()).unwrap(); + let deadline = Instant::now() + Duration::from_secs(2); + while !client.connected() { + client.receive_once().unwrap(); + client.poll_resends().unwrap(); + assert!( + Instant::now() < deadline, + "public query handshake timed out" + ); + } + client + } + + fn receive_public_query_message( + client: &mut CultNetRudpSocketTransportConnection, + ) -> CultNetMessage { + let deadline = Instant::now() + Duration::from_secs(2); + loop { + if let Some(frame) = client.receive_once().unwrap() { + assert_eq!(frame.channel_id, "schema"); + return decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + ) + .unwrap(); + } + client.poll_resends().unwrap(); + assert!(Instant::now() < deadline, "public query response timed out"); + } + } + + #[test] + fn public_query_server_returns_exact_allowlisted_bytes_and_correlation() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let public_path = options.public_health_store_path.as_ref().unwrap(); + let stored = SingleFileMessagePackBackingStore::new(public_path) + .pull_all_read_only_snapshot() + .unwrap() + .remove(0); + let server = IdunnPublicHealthSnapshotServer::new( + public_path.clone(), + &[public_query_target(&desired)], + ) + .unwrap(); + let request = CultNetMessage::SnapshotRequest { + message_id: "status-correlation-7".into(), + schema_ids: Some(vec![ + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + ]), + record_keys: Some(vec![stored.key.clone()]), + }; + let CultNetMessage::SnapshotResponseRaw { + message_id, + documents, + } = server.serve(&request).unwrap() + else { + panic!("expected raw public health snapshot"); + }; + assert_eq!(message_id, "status-correlation-7"); + assert_eq!(documents.len(), 1); + assert_eq!(documents[0].payload, stored.payload); + assert_eq!( + documents[0].source_runtime_id.as_deref(), + Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID) + ); + assert_eq!( + documents[0].source_role.as_deref(), + Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE) + ); + + for request in [ + CultNetMessage::SnapshotRequest { + message_id: "unknown-schema".into(), + schema_ids: Some(vec!["private.idunn.state.v0".into()]), + record_keys: None, + }, + CultNetMessage::SnapshotRequest { + message_id: "unknown-key".into(), + schema_ids: None, + record_keys: Some(vec!["provider-health:not-in-target-catalog".into()]), + }, + ] { + let CultNetMessage::SnapshotResponseRaw { documents, .. } = + server.serve(&request).unwrap() + else { + panic!("expected bounded empty response"); + }; + assert!(documents.is_empty()); + } + } + + #[test] + fn public_query_store_contamination_is_fatal_and_private_type_is_never_served() { + let (options, _lock, desired, _source, _publisher, root) = projection_fixture(false); + let private_path = root.path().join("private-in-public.cc"); + let private = IdunnDaemonHealthRecord { + daemon_id: desired.daemon_id.clone(), + state: "active".into(), + detail: "private managed judgment".into(), + observed_at: "2026-07-19T12:00:02Z".into(), + health_contract: desired.health_contract.clone(), + publication_source: "daemon-authenticated".into(), + transport: CULTNET_RUDP_PROTOCOL_ID.into(), + }; + SingleFileMessagePackBackingStore::new(&private_path) + .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:02Z").unwrap()) + .unwrap(); + assert!( + IdunnPublicHealthSnapshotServer::new(private_path, &[public_query_target(&desired)]) + .is_err() + ); + + let public_path = options.public_health_store_path.as_ref().unwrap(); + let server = IdunnPublicHealthSnapshotServer::new( + public_path.clone(), + &[public_query_target(&desired)], + ) + .unwrap(); + SingleFileMessagePackBackingStore::new(public_path) + .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:03Z").unwrap()) + .unwrap(); + assert!( + server + .serve(&CultNetMessage::SnapshotRequest { + message_id: "private-probe".into(), + schema_ids: None, + record_keys: None, + }) + .is_err() + ); + } + + #[test] + fn public_query_rudp_is_multi_peer_and_survives_malformed_request() { + let (options, lock, desired, source, publisher, _root) = projection_fixture(false); + publisher + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") + .unwrap(); + let server = IdunnPublicHealthSnapshotServer::new( + options.public_health_store_path.clone().unwrap(), + &[public_query_target(&desired)], + ) + .unwrap(); + let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); + let server_addr = socket.local_addr().unwrap(); + socket + .set_read_timeout(Some(Duration::from_millis(100))) + .unwrap(); + let stop = Arc::new(AtomicBool::new(false)); + let worker_stop = Arc::clone(&stop); + let worker = thread::spawn(move || { + run_public_health_query_listener_loop(socket, server, Some(worker_stop)) + }); + + let mut hostile = connect_public_query_client(server_addr); + hostile.send("schema", vec![0xc1]).unwrap(); + assert!(matches!( + receive_public_query_message(&mut hostile), + CultNetMessage::Error { .. } + )); + + let mut first = connect_public_query_client(server_addr); + let mut second = connect_public_query_client(server_addr); + for (client, id) in [(&mut first, "first-peer"), (&mut second, "second-peer")] { + let request = CultNetMessage::SnapshotRequest { + message_id: id.into(), + schema_ids: Some(vec![ + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + ]), + record_keys: Some(vec![provider_health_projection_key( + &desired.daemon_id, + &desired.health_contract, + )]), + }; + client + .send( + "schema", + encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0) + .unwrap(), + ) + .unwrap(); + let CultNetMessage::SnapshotResponseRaw { + message_id, + documents, + } = receive_public_query_message(client) + else { + panic!("expected peer snapshot response"); + }; + assert_eq!(message_id, id); + assert_eq!(documents.len(), 1); + } + stop.store(true, Ordering::Release); + worker.join().unwrap().unwrap(); + } + #[test] fn projection_is_signed_canonical_public_state_without_provider_detail() { let (options, lock, desired, source, publisher, _root) = projection_fixture(false); @@ -9225,23 +9393,23 @@ mod tests { payload: rmp_serde::to_vec(&corrupted).unwrap(), ..current_envelope.clone() }; - assert!( - backing - .compare_exchange( - &[CultCacheExpectedEnvelope { - key: current_envelope.key.clone(), - r#type: current_envelope.r#type.clone(), - current: Some(current_envelope), - }], - &[corrupted_envelope], - ) - .unwrap() - ); - assert!( - second - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:04Z") - .is_err() - ); + assert!( + backing + .compare_exchange( + &[CultCacheExpectedEnvelope { + key: current_envelope.key.clone(), + r#type: current_envelope.r#type.clone(), + current: Some(current_envelope), + }], + &[corrupted_envelope], + ) + .unwrap() + ); + assert!( + second + .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:04Z") + .is_err() + ); let contaminated_path = root.path().join("contaminated-public.cc"); SingleFileMessagePackBackingStore::new(&contaminated_path) @@ -9456,13 +9624,16 @@ mod tests { let options = CommonOptions { store_path, release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: Some(identity_path), daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; @@ -9752,13 +9923,16 @@ mod tests { let options = CommonOptions { store_path: root.path().join("idunn.cc"), release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, operator_alarm_command: None, rudp_health_bind: None, trusted_epiphany_health_identity_store: None, daemon_health_trust_store_path: None, service_identity_store_path: None, public_health_store_path: None, - public_health_query_bind: None, + public_health_query_bind: None, execute: false, command_timeout_seconds: 1, }; diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 965aa3c..b046f58 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -3,8 +3,12 @@ use cultcache_rs::{ CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, }; use cultnet_rs::{ - GameCultProviderHealthIdentity, IdunnServiceIdentity, ServiceIdentityProfile, - ServiceIdentityTrustAnchor, derive_service_identity_id, enroll_service_identity_at, + GameCultProviderHealthIdentity, IDUNN_DEPLOYMENT_BRAKE_AUTHORITY, IDUNN_DEPLOYMENT_BRAKE_ID, + IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_SCOPE, IDUNN_DEPLOYMENT_RELEASE_PURPOSE, + IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, + IdunnDeploymentBrakeRecord, IdunnDeploymentBrakeReleasePurpose, IdunnServiceIdentity, + ServiceIdentityProfile, ServiceIdentityTrustAnchor, derive_service_identity_id, + enroll_service_identity_at, evaluate_idunn_deployment_brake, export_service_identity_trust_anchor, open_service_identity_at, }; use odin_core::{ @@ -50,6 +54,26 @@ pub fn run(args: impl IntoIterator) -> Result<()> { let signer = open_service_identity_at::(&private)?; export_service_identity_trust_anchor(&signer, &public)?; } + "enroll-deployment-brake-operator" => { + require_only(&options, &["private-store"])?; + enroll_service_identity_at::(&path( + &options, + "private-store", + )?)?; + } + "export-deployment-brake-operator-anchor" => { + require_only(&options, &["private-store", "public-anchor"])?; + let private = path(&options, "private-store")?; + let public = path(&options, "public-anchor")?; + reject_alias(&private, &public)?; + refuse_existing(&public, "deployment brake operator anchor")?; + let signer = + open_service_identity_at::(&private)?; + export_service_identity_trust_anchor(&signer, &public)?; + } + "deployment-brake-engage" => deployment_brake_engage(&options)?, + "deployment-brake-release" => deployment_brake_release(&options)?, + "deployment-brake-status" => deployment_brake_status(&options)?, "create-daemon-health-trust-binding" => create_health_binding(&options)?, "add-daemon-health-trust-binding" => add_health_binding(&options)?, "validate-daemon-health-trust-binding" => { @@ -63,6 +87,204 @@ pub fn run(args: impl IntoIterator) -> Result<()> { Ok(()) } +fn deployment_brake_engage(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "store", + "runtime-id", + "owner", + "reason", + "observed-at-unix-millis", + ], + )?; + let observed = parse_u64(options, "observed-at-unix-millis")?; + let record = IdunnDeploymentBrakeRecord { + schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), + brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), + authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), + runtime_id: required(options, "runtime-id")?.into(), + status: "engaged".into(), + scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), + reason: required(options, "reason")?.into(), + observed_at_unix_millis: observed, + expires_at_unix_millis: None, + authorization_id: None, + authorization_purpose: None, + authorized_release_id: None, + authorized_deployment_id: None, + authorized_by: None, + authorization_issued_at_unix_millis: None, + authorization_expires_at_unix_millis: None, + signature_algorithm: None, + signature: None, + private_state_exposed: false, + updated_by: required(options, "owner")?.into(), + }; + record.validate()?; + replace_brake(&path(options, "store")?, record, observed) +} + +fn deployment_brake_release(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "store", + "private-store", + "runtime-id", + "owner", + "reason", + "authorization-id", + "release-id", + "deployment-id", + "issued-at-unix-millis", + "expires-at-unix-millis", + ], + )?; + let issued = parse_u64(options, "issued-at-unix-millis")?; + let expires = parse_u64(options, "expires-at-unix-millis")?; + let signer = open_service_identity_at::(&path( + options, + "private-store", + )?)?; + let mut record = IdunnDeploymentBrakeRecord { + schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), + brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), + authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), + runtime_id: required(options, "runtime-id")?.into(), + status: "released".into(), + scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), + reason: required(options, "reason")?.into(), + observed_at_unix_millis: issued, + expires_at_unix_millis: Some(expires), + authorization_id: Some(required(options, "authorization-id")?.into()), + authorization_purpose: Some(IDUNN_DEPLOYMENT_RELEASE_PURPOSE.into()), + authorized_release_id: Some(required(options, "release-id")?.into()), + authorized_deployment_id: Some(required(options, "deployment-id")?.into()), + authorized_by: Some(signer.trust_anchor()?.identity_id), + authorization_issued_at_unix_millis: Some(issued), + authorization_expires_at_unix_millis: Some(expires), + signature_algorithm: Some("ed25519".into()), + signature: None, + private_state_exposed: false, + updated_by: required(options, "owner")?.into(), + }; + record.signature = Some( + signer + .sign::(&rmp_serde::to_vec(&record)?) + .signature, + ); + record.validate()?; + replace_brake(&path(options, "store")?, record, issued) +} + +fn deployment_brake_status(options: &BTreeMap) -> Result<()> { + allow_only( + options, + &[ + "store", + "operator-anchor", + "runtime-id", + "release-id", + "deployment-id", + "now-unix-millis", + ], + )?; + let record = read_brake(&path(options, "store")?)?; + if record.runtime_id != required(options, "runtime-id")? { + bail!("deployment brake belongs to another runtime") + } + if record.status == "engaged" { + println!( + "engaged runtime={} scope={} owner={} reason={}", + record.runtime_id, record.scope, record.updated_by, record.reason + ); + return Ok(()); + } + let anchor = read_operator_anchor(&path(options, "operator-anchor")?)?; + evaluate_idunn_deployment_brake( + IdunnDeploymentBrakeObservation::Present(&record), + &anchor, + required(options, "runtime-id")?, + required(options, "release-id")?, + required(options, "deployment-id")?, + parse_u64(options, "now-unix-millis")?, + ) + .map_err(|d| anyhow!("deployment brake denied actuation: {d:?}"))?; + println!( + "released runtime={} release={} deployment={} owner={}", + record.runtime_id, + record.authorized_release_id.unwrap(), + record.authorized_deployment_id.unwrap(), + record.updated_by + ); + Ok(()) +} + +fn replace_brake(path: &Path, record: IdunnDeploymentBrakeRecord, millis: u64) -> Result<()> { + let store = SingleFileMessagePackBackingStore::new(path); + let entries = store.pull_all_read_only_snapshot()?; + let current = match entries.as_slice() { + [] => None, + [entry] + if entry.r#type == IdunnDeploymentBrakeRecord::TYPE + && entry.key == IDUNN_DEPLOYMENT_BRAKE_ID => + { + Some(entry.clone()) + } + _ => bail!("deployment brake store is corrupt or ambiguous"), + }; + let next = typed_envelope( + IDUNN_DEPLOYMENT_BRAKE_ID, + &record, + IDUNN_DEPLOYMENT_BRAKE_SCHEMA, + millis, + )?; + if !store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: IdunnDeploymentBrakeRecord::TYPE.into(), + key: IDUNN_DEPLOYMENT_BRAKE_ID.into(), + current, + }], + &[next], + )? { + bail!("deployment brake changed during transition"); + } + Ok(()) +} + +fn read_brake(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("deployment brake is missing or ambiguous") + }; + if entry.r#type != IdunnDeploymentBrakeRecord::TYPE + || entry.key != IDUNN_DEPLOYMENT_BRAKE_ID + || entry.schema_id.as_deref() != Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA) + { + bail!("deployment brake is foreign") + } + let record: IdunnDeploymentBrakeRecord = rmp_serde::from_slice(&entry.payload)?; + if rmp_serde::to_vec(&record)? != entry.payload { + bail!("deployment brake is noncanonical") + } + record.validate()?; + Ok(record) +} + +fn read_operator_anchor(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("operator anchor is missing or ambiguous") + }; + if entry.r#type != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_TYPE + || entry.key != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_KEY + { + bail!("operator anchor is foreign") + } + Ok(rmp_serde::from_slice(&entry.payload)?) +} + fn validate_health_binding_store(path: &Path) -> Result<()> { let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; if entries.is_empty() { @@ -395,6 +617,13 @@ fn require_only(options: &BTreeMap, names: &[&str]) -> Result<() Ok(()) } +fn allow_only(options: &BTreeMap, names: &[&str]) -> Result<()> { + if let Some(name) = options.keys().find(|name| !names.contains(&name.as_str())) { + bail!("unsupported option --{name}"); + } + Ok(()) +} + fn required<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { options .get(name) @@ -474,7 +703,7 @@ fn normalized(path: &Path) -> Result { } fn usage() -> &'static str { - "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " + "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " } #[cfg(test)] @@ -610,16 +839,45 @@ mod tests { let temp = TempDir::new()?; let store = temp.path().join("trust.cc"); let path = store.to_str().unwrap().to_string(); - invoke(&binding_args("create-daemon-health-trust-binding", &path, "one", "daemon-one", "runtime-one"))?; + invoke(&binding_args( + "create-daemon-health-trust-binding", + &path, + "one", + "daemon-one", + "runtime-one", + ))?; let barrier = std::sync::Arc::new(std::sync::Barrier::new(3)); - let workers = [("two", "daemon-two", "runtime-two"), ("three", "daemon-three", "runtime-three")].map(|(id, daemon, runtime)| { - let path = path.clone(); let barrier = barrier.clone(); - std::thread::spawn(move || { barrier.wait(); invoke(&binding_args("add-daemon-health-trust-binding", &path, id, daemon, runtime)) }) + let workers = [ + ("two", "daemon-two", "runtime-two"), + ("three", "daemon-three", "runtime-three"), + ] + .map(|(id, daemon, runtime)| { + let path = path.clone(); + let barrier = barrier.clone(); + std::thread::spawn(move || { + barrier.wait(); + invoke(&binding_args( + "add-daemon-health-trust-binding", + &path, + id, + daemon, + runtime, + )) + }) }); barrier.wait(); - let wins = workers.into_iter().map(|worker| worker.join().unwrap()).filter(Result::is_ok).count(); + let wins = workers + .into_iter() + .map(|worker| worker.join().unwrap()) + .filter(Result::is_ok) + .count(); assert!((1..=2).contains(&wins)); - assert_eq!(SingleFileMessagePackBackingStore::new(&store).pull_all_read_only_snapshot()?.len(), 1 + wins); + assert_eq!( + SingleFileMessagePackBackingStore::new(&store) + .pull_all_read_only_snapshot()? + .len(), + 1 + wins + ); Ok(()) } @@ -877,4 +1135,148 @@ mod tests { partial.extend(["--release-binding-required", "sometimes"]); assert!(invoke(&partial).is_err()); } + + #[test] + fn brake_operator_can_engage_then_issue_one_exact_bounded_release() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("operator-private.cc"); + let anchor = temp.path().join("operator-public.cc"); + let store = temp.path().join("brake.cc"); + invoke(&[ + "enroll-deployment-brake-operator", + "--private-store", + private.to_str().unwrap(), + ])?; + invoke(&[ + "export-deployment-brake-operator-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + anchor.to_str().unwrap(), + ])?; + invoke(&[ + "deployment-brake-engage", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + "--owner", + "operator/metacrat", + "--reason", + "sleep mode", + "--observed-at-unix-millis", + "100", + ])?; + assert_eq!(read_brake(&store)?.status, "engaged"); + invoke(&[ + "deployment-brake-release", + "--store", + store.to_str().unwrap(), + "--private-store", + private.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + "--owner", + "operator/metacrat", + "--reason", + "one deployment", + "--authorization-id", + "auth/r4", + "--release-id", + "commit-4", + "--deployment-id", + "request-4", + "--issued-at-unix-millis", + "200", + "--expires-at-unix-millis", + "800", + ])?; + invoke(&[ + "deployment-brake-status", + "--store", + store.to_str().unwrap(), + "--operator-anchor", + anchor.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + "--release-id", + "commit-4", + "--deployment-id", + "request-4", + "--now-unix-millis", + "500", + ])?; + assert!( + invoke(&[ + "deployment-brake-status", + "--store", + store.to_str().unwrap(), + "--operator-anchor", + anchor.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + "--release-id", + "substituted", + "--deployment-id", + "request-4", + "--now-unix-millis", + "500" + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn brake_compare_exchange_rejects_stale_transition_snapshot() -> Result<()> { + let temp = TempDir::new()?; + let store_path = temp.path().join("brake.cc"); + let base = IdunnDeploymentBrakeRecord { + schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), + brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), + authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), + runtime_id: "yggdrasil".into(), + status: "engaged".into(), + scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), + reason: "first".into(), + observed_at_unix_millis: 100, + expires_at_unix_millis: None, + authorization_id: None, + authorization_purpose: None, + authorized_release_id: None, + authorized_deployment_id: None, + authorized_by: None, + authorization_issued_at_unix_millis: None, + authorization_expires_at_unix_millis: None, + signature_algorithm: None, + signature: None, + private_state_exposed: false, + updated_by: "operator/a".into(), + }; + replace_brake(&store_path, base.clone(), 100)?; + let stale = SingleFileMessagePackBackingStore::new(&store_path) + .pull_all_read_only_snapshot()? + .remove(0); + let mut changed = base.clone(); + changed.reason = "racing writer".into(); + changed.observed_at_unix_millis = 101; + replace_brake(&store_path, changed, 101)?; + let candidate = typed_envelope( + IDUNN_DEPLOYMENT_BRAKE_ID, + &base, + IDUNN_DEPLOYMENT_BRAKE_SCHEMA, + 102, + )?; + assert!( + !SingleFileMessagePackBackingStore::new(&store_path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: IdunnDeploymentBrakeRecord::TYPE.into(), + key: IDUNN_DEPLOYMENT_BRAKE_ID.into(), + current: Some(stale) + }], + &[candidate] + )? + ); + Ok(()) + } } diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index d17ad1c..252526b 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -10,7 +10,7 @@ User=idunn Group=idunn # Operational cancellation ceiling only. Target daemons own readiness through # typed health; this duration must never be interpreted as deployment success. -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute +ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute Restart=always RestartSec=5 TimeoutStopSec=20 diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index a1001dc..402f6e7 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit a1001dc855aa4de04859d01d8be4c966f510f557 +Subproject commit 402f6e7f0b938a251fc9443d734a6c86d5c1b40e From b907a9d0c081bfe77f1de96c390f4bd19ab8bcb5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 20 Jul 2026 00:07:04 +0100 Subject: [PATCH 29/93] Make Idunn deployment brake read-only --- README.md | 9 +++- crates/idunn-daemon/src/main.rs | 50 ++++++++++++++------ crates/idunn-daemon/src/provisioning.rs | 4 +- docs/idunn.md | 9 ++++ scripts/linux/idunn-yggdrasil.service | 3 +- vendor/cultcache-rs/src/lib.rs | 62 +++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3ef18d9..961e921 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,14 @@ the canonical runtime (`yggdrasil` for `yggdrasil-local`), exact source revision (or `restart:`), exact request id, operator, signature, and short expiry. Missing, corrupt, engaged, expired, foreign, wrong-scope, substituted, or badly-signed state fails closed. Planning and lifecycle-command publication do -not bypass or cache this decision. +not bypass or cache this decision. The store and its sibling `.lock` live in +`/var/lib/gamecult/idunn-authority`, a root-owned `root:idunn` `0750` directory; +both files are `root:idunn` `0640`, and the daemon's systemd sandbox exposes the +directory read-only. Idunn holds the lock shared from its final snapshot read +through process spawn, so an engage cannot be defeated by previously captured +released bytes. A release is not one-shot: its exact request/revision grant +covers each migration and deploy spawn in that rollout until its short expiry; +every spawn independently reopens and validates the same grant. Local package surfaces: diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 486bb9b..b723f81 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -6007,14 +6007,17 @@ fn run_shell( .with_context(|| format!("creating {}", stderr_path.display()))?; configure_command_lifetime(&mut process); - // This is deliberately the last fallible authority read before spawn. - // Preparation above has no consequence outside Idunn's temporary files. - verify_deployment_brake(options, release_id, deployment_id)?; - let child = process - .stdout(Stdio::from(stdout_file)) - .stderr(Stdio::from(stderr_file)) - .spawn() - .with_context(|| format!("running command {command:?}"))?; + // Hold the root-owned authority lock from the final read through spawn. + // An operator engage therefore wins before this gate or waits until the + // already-authorized consequence has begun; captured release bytes cannot + // race a later engage into a new spawn. + let child = with_verified_deployment_brake(options, release_id, deployment_id, || { + process + .stdout(Stdio::from(stdout_file)) + .stderr(Stdio::from(stderr_file)) + .spawn() + .with_context(|| format!("running command {command:?}")) + })?; let status = wait_for_child_status_with_timeout( child, Duration::from_secs(options.command_timeout_seconds), @@ -6044,15 +6047,30 @@ fn run_braked_shell( run_shell(command, options, release_id, deployment_id, deployment) } -fn verify_deployment_brake( +fn with_verified_deployment_brake( options: &CommonOptions, release_id: &str, deployment_id: &str, -) -> Result<()> { + consequence: impl FnOnce() -> Result, +) -> Result { let store_path = options .deployment_brake_store_path .as_ref() .ok_or_else(|| anyhow!("--deployment-brake-store is required for actuation"))?; + SingleFileMessagePackBackingStore::new(store_path) + .with_read_only_shared_snapshot(|entries| { + verify_deployment_brake_snapshot(options, release_id, deployment_id, entries)?; + consequence() + }) + .with_context(|| format!("reading deployment brake {}", store_path.display())) +} + +fn verify_deployment_brake_snapshot( + options: &CommonOptions, + release_id: &str, + deployment_id: &str, + entries: Vec, +) -> Result<()> { let anchor_path = options .deployment_brake_operator_anchor_path .as_ref() @@ -6061,10 +6079,6 @@ fn verify_deployment_brake( .deployment_brake_runtime_id .as_deref() .ok_or_else(|| anyhow!("deployment brake runtime id is not configured"))?; - - let entries = SingleFileMessagePackBackingStore::new(store_path) - .pull_all_read_only_snapshot() - .with_context(|| format!("reading deployment brake {}", store_path.display()))?; let observation = match entries.as_slice() { [entry] if entry.r#type == IdunnDeploymentBrakeRecord::TYPE @@ -7653,6 +7667,14 @@ mod tests { "--release-authority-store /srv/bifrost/state/repository-release-authority.cc" )); assert!(unit.contains("--command-timeout-seconds 21600")); + assert!(unit.contains( + "--deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc" + )); + assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); + assert!(!unit.contains("ReadWritePaths=/var/lib/gamecult/idunn-authority")); + assert!( + !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") + ); assert!(unit.contains( "--trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp" )); diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index b046f58..7638009 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -1137,7 +1137,7 @@ mod tests { } #[test] - fn brake_operator_can_engage_then_issue_one_exact_bounded_release() -> Result<()> { + fn brake_operator_can_issue_one_exact_bounded_rollout_grant() -> Result<()> { let temp = TempDir::new()?; let private = temp.path().join("operator-private.cc"); let anchor = temp.path().join("operator-public.cc"); @@ -1179,7 +1179,7 @@ mod tests { "--owner", "operator/metacrat", "--reason", - "one deployment", + "one rollout", "--authorization-id", "auth/r4", "--release-id", diff --git a/docs/idunn.md b/docs/idunn.md index 8e339e7..2b21813 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -38,6 +38,15 @@ escalation, and continuity witness state. - Shared paths: manual operator deploy/restart, scheduled deploy/restart, degraded-health repair, boot rehydration, and future remote worker recovery must pass through the same Idunn command primitive. +- Consequence gate: Yggdrasil keeps `deployment-brake.cc` and its pre-created + sibling `deployment-brake.cc.lock` in `/var/lib/gamecult/idunn-authority`. + The directory is `root:idunn` `0750`, both files are `root:idunn` `0640`, and + Idunn receives only a systemd `ReadOnlyPaths` view. It holds the lock shared + across the final read, validation, and spawn; the root-owned operator writer + takes it exclusive for engage/release. A bounded release authorizes the exact + rollout request and source revision, covering its migration and deploy spawns + until expiry. It is not consumed after the first spawn, and every spawn must + independently revalidate it. - Deletion line: any keepalive loop inside Odin, Gjallar, Eve lowerers, or renderer code must be cut or demoted to a probe that names Idunn as the restart owner. diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index 252526b..ce335f5 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -10,7 +10,7 @@ User=idunn Group=idunn # Operational cancellation ceiling only. Target daemons own readiness through # typed health; this duration must never be interpreted as deployment success. -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute +ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute Restart=always RestartSec=5 TimeoutStopSec=20 @@ -20,6 +20,7 @@ PrivateTmp=yes ProtectHome=yes ProtectSystem=full ReadWritePaths=/var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadOnlyPaths=/var/lib/gamecult/idunn-authority [Install] WantedBy=multi-user.target diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs index 54846f2..c412e35 100644 --- a/vendor/cultcache-rs/src/lib.rs +++ b/vendor/cultcache-rs/src/lib.rs @@ -144,6 +144,28 @@ impl SingleFileMessagePackBackingStore { self.read_all_unlocked() } + /// Holds the pre-created sibling lock shared while a read-only consumer + /// evaluates and acts on one snapshot. The reader never creates or writes + /// either file, so an authority owner can grant only read access. Writers + /// using `compare_exchange` cannot replace the snapshot until `action` + /// returns. + pub fn with_read_only_shared_snapshot( + &self, + action: impl FnOnce(Vec) -> Result, + ) -> Result { + let lock_path = self.lock_path(); + let lock = OpenOptions::new() + .read(true) + .open(&lock_path) + .with_context(|| format!("failed to open pre-created {}", lock_path.display()))?; + fs2::FileExt::lock_shared(&lock) + .with_context(|| format!("failed to lock {}", lock_path.display()))?; + let result = self.read_all_unlocked().and_then(action); + fs2::FileExt::unlock(&lock) + .with_context(|| format!("failed to unlock {}", lock_path.display()))?; + result + } + pub fn compare_exchange( &self, expected: &[CultCacheExpectedEnvelope], @@ -775,6 +797,8 @@ fn temporary_path_for(path: &Path) -> PathBuf { mod tests { use super::*; use pretty_assertions::assert_eq; + use std::sync::mpsc; + use std::time::Duration; #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache(type = "settings")] @@ -796,6 +820,44 @@ mod tests { cultcache_registry!(TestEntries { Settings, Note }); + #[test] + fn captured_snapshot_cannot_outlive_a_shared_consequence_gate() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("authority.cc"); + let mut store = SingleFileMessagePackBackingStore::new(&store_path); + let released = CultCacheEnvelope { + key: "brake".into(), + r#type: "brake".into(), + payload: b"released".to_vec(), + stored_at: "2026-07-20T00:00:00Z".into(), + schema_id: Some("brake.v0".into()), + }; + store.push(&released)?; + + let writer_path = store_path.clone(); + let (started_tx, started_rx) = mpsc::channel(); + let (finished_tx, finished_rx) = mpsc::channel(); + store.with_read_only_shared_snapshot(|captured| { + assert_eq!(captured, vec![released.clone()]); + std::thread::spawn(move || { + started_tx.send(()).unwrap(); + let engaged = CultCacheEnvelope { + payload: b"engaged".to_vec(), + ..released + }; + let mut writer = SingleFileMessagePackBackingStore::new(writer_path); + writer.push(&engaged).unwrap(); + finished_tx.send(()).unwrap(); + }); + started_rx.recv_timeout(Duration::from_secs(1))?; + assert!(finished_rx.recv_timeout(Duration::from_millis(100)).is_err()); + Ok(()) + })?; + finished_rx.recv_timeout(Duration::from_secs(1))?; + assert_eq!(store.pull_all_read_only_snapshot()?[0].payload, b"engaged"); + Ok(()) + } + #[test] fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> { let temp = tempfile::tempdir()?; From 7b2d4af7ddcbc677c2e03f45a8a4f708c5aaf365 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 20 Jul 2026 00:20:09 +0100 Subject: [PATCH 30/93] Preserve consequence result across lock cleanup --- vendor/cultcache-rs/src/lib.rs | 92 +++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs index c412e35..37c1f3e 100644 --- a/vendor/cultcache-rs/src/lib.rs +++ b/vendor/cultcache-rs/src/lib.rs @@ -153,6 +153,34 @@ impl SingleFileMessagePackBackingStore { &self, action: impl FnOnce(Vec) -> Result, ) -> Result { + self.with_read_only_shared_snapshot_and_unlock_diagnostic(action, |_| {}) + } + + pub fn with_read_only_shared_snapshot_and_unlock_diagnostic( + &self, + action: impl FnOnce(Vec) -> Result, + on_unlock_failure: impl FnOnce(anyhow::Error), + ) -> Result { + self.with_read_only_shared_snapshot_using( + action, + |lock| { + fs2::FileExt::unlock(lock) + .with_context(|| format!("failed to unlock {}", self.lock_path().display())) + }, + on_unlock_failure, + ) + } + + fn with_read_only_shared_snapshot_using( + &self, + action: impl FnOnce(Vec) -> Result, + unlock: U, + on_unlock_failure: D, + ) -> Result + where + U: FnOnce(&File) -> Result<()>, + D: FnOnce(anyhow::Error), + { let lock_path = self.lock_path(); let lock = OpenOptions::new() .read(true) @@ -160,9 +188,9 @@ impl SingleFileMessagePackBackingStore { .with_context(|| format!("failed to open pre-created {}", lock_path.display()))?; fs2::FileExt::lock_shared(&lock) .with_context(|| format!("failed to lock {}", lock_path.display()))?; + let guard = ReadOnlySharedLockGuard::new(lock, unlock, on_unlock_failure); let result = self.read_all_unlocked().and_then(action); - fs2::FileExt::unlock(&lock) - .with_context(|| format!("failed to unlock {}", lock_path.display()))?; + drop(guard); result } @@ -299,6 +327,41 @@ impl SingleFileMessagePackBackingStore { } } +struct ReadOnlySharedLockGuard +where + U: FnOnce(&File) -> Result<()>, + D: FnOnce(anyhow::Error), +{ + lock: Option, + unlock: Option, + on_unlock_failure: Option, +} + +impl ReadOnlySharedLockGuard +where + U: FnOnce(&File) -> Result<()>, + D: FnOnce(anyhow::Error), +{ + fn new(lock: File, unlock: U, on_unlock_failure: D) -> Self { + Self { lock: Some(lock), unlock: Some(unlock), on_unlock_failure: Some(on_unlock_failure) } + } +} + +impl Drop for ReadOnlySharedLockGuard +where + U: FnOnce(&File) -> Result<()>, + D: FnOnce(anyhow::Error), +{ + fn drop(&mut self) { + let (Some(lock), Some(unlock)) = (self.lock.as_ref(), self.unlock.take()) else { return; }; + if let Err(error) = unlock(lock) + && let Some(on_unlock_failure) = self.on_unlock_failure.take() + { + on_unlock_failure(error); + } + } +} + #[cfg(unix)] fn replace_file_atomically(source: &Path, destination: &Path) -> Result<()> { fs::rename(source, destination).with_context(|| { @@ -858,6 +921,31 @@ mod tests { Ok(()) } + #[test] + fn post_action_unlock_failure_is_diagnostic_not_action_result() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("authority.cc"); + let mut store = SingleFileMessagePackBackingStore::new(&store_path); + store.push(&CultCacheEnvelope { + key: "brake".into(), + r#type: "brake".into(), + payload: b"released".to_vec(), + stored_at: "2026-07-20T00:00:00Z".into(), + schema_id: Some("brake".into()), + })?; + + let mut diagnostic = None; + let result = store.with_read_only_shared_snapshot_using( + |_| Ok(42), + |_| Err(anyhow!("injected post-action unlock failure")), + |error| diagnostic = Some(error.to_string()), + ); + + assert_eq!(result?, 42); + assert_eq!(diagnostic.as_deref(), Some("injected post-action unlock failure")); + Ok(()) + } + #[test] fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> { let temp = tempfile::tempdir()?; From afb67d9f4377b28ee9b2454a9c089cb13e75c46e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 22 Jul 2026 02:22:16 +0100 Subject: [PATCH 31/93] Make provider health signatures portable across runtimes --- .../cultcache-rs-derive/src/lib.rs | 51 ++++++++++++++++--- vendor/cultnet-rs | 2 +- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs b/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs index f05a23f..7d61319 100644 --- a/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs +++ b/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs @@ -75,6 +75,7 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { }; let mut key: Option = None; let mut default = false; + let mut bytes = false; for attribute in field .attrs .iter() @@ -88,6 +89,9 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { } else if meta.path.is_ident("default") { default = true; Ok(()) + } else if meta.path.is_ident("bytes") { + bytes = true; + Ok(()) } else { Err(meta.error("unsupported field cultcache attribute")) } @@ -117,6 +121,7 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { ident: field_ident, ty: field.ty, default, + bytes, }); } slots.sort_by_key(|slot| slot.key); @@ -125,8 +130,14 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { let serialize_elements = (0..=max_slot).map(|slot| { if let Some(field) = slots.iter().find(|field| field.key == slot) { let field_ident = &field.ident; - quote! { - tuple.serialize_element(&self.#field_ident)?; + if field.bytes { + quote! { + tuple.serialize_element(::serde_bytes::Bytes::new(&self.#field_ident))?; + } + } else { + quote! { + tuple.serialize_element(&self.#field_ident)?; + } } } else { quote! { @@ -138,8 +149,14 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { let option_declarations = slots.iter().map(|field| { let variable = format_ident!("slot_{}", field.key); let ty = &field.ty; - quote! { - let mut #variable: ::std::option::Option<#ty> = ::std::option::Option::None; + if field.bytes { + quote! { + let mut #variable: ::std::option::Option<::serde_bytes::ByteBuf> = ::std::option::Option::None; + } + } else { + quote! { + let mut #variable: ::std::option::Option<#ty> = ::std::option::Option::None; + } } }); @@ -147,9 +164,17 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { if let Some(field) = slots.iter().find(|field| field.key == slot) { let variable = format_ident!("slot_{}", field.key); let ty = &field.ty; - quote! { - #slot => { - #variable = seq.next_element::<#ty>()?; + if field.bytes { + quote! { + #slot => { + #variable = seq.next_element::<::serde_bytes::ByteBuf>()?; + } + } + } else { + quote! { + #slot => { + #variable = seq.next_element::<#ty>()?; + } } } } else { @@ -164,7 +189,16 @@ pub fn derive_database_entry(input: TokenStream) -> TokenStream { let build_fields = slots.iter().map(|field| { let field_ident = &field.ident; let variable = format_ident!("slot_{}", field.key); - if field.default { + if field.bytes && field.default { + quote! { + #field_ident: #variable.unwrap_or_default().into_vec() + } + } else if field.bytes { + let field_name = field_ident.to_string(); + quote! { + #field_ident: #variable.ok_or_else(|| ::serde::de::Error::missing_field(#field_name))?.into_vec() + } + } else if field.default { quote! { #field_ident: #variable.unwrap_or_default() } @@ -240,4 +274,5 @@ struct CultCacheSlot { ident: syn::Ident, ty: Type, default: bool, + bytes: bool, } diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index 402f6e7..fdccc37 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit 402f6e7f0b938a251fc9443d734a6c86d5c1b40e +Subproject commit fdccc37aa68c3f0e53bfd53a92c15a148978f0c7 From 325442a0fa18e53379a289b914b14425a9024e2a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 20 Aug 2026 19:14:45 +0200 Subject: [PATCH 32/93] Decouple Idunn runtime from Epiphany --- crates/idunn-daemon/src/main.rs | 14 ++++---------- scripts/linux/idunn-yggdrasil.service | 5 +++-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index b723f81..770a69c 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5599,13 +5599,9 @@ impl Options { "--release-authority-store is required for yggdrasil-local release targets" )); } - if swarm_profile.as_deref() == Some("yggdrasil-local") - && common.trusted_epiphany_health_identity_store.is_none() - { - return Err(anyhow!( - "--trusted-epiphany-health-identity-store is required for yggdrasil-local" - )); - } + // Epiphany is one supervised target, not a prerequisite for Idunn. + // Without its migration trust anchor only Epiphany's legacy signed + // health is inadmissible; unrelated targets retain their authority. if swarm_profile.as_deref() == Some("yggdrasil-local") && (common.deployment_brake_store_path.is_none() || common.deployment_brake_operator_anchor_path.is_none()) @@ -7675,9 +7671,7 @@ mod tests { assert!( !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") ); - assert!(unit.contains( - "--trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp" - )); + assert!(!unit.contains("--trusted-epiphany-health-identity-store")); assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); assert!( diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index ce335f5..d8a21fe 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -10,7 +10,7 @@ User=idunn Group=idunn # Operational cancellation ceiling only. Target daemons own readiness through # typed health; this duration must never be interpreted as deployment success. -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --rudp-health-bind 10.77.0.1:17870 --trusted-epiphany-health-identity-store /etc/gamecult/idunn/epiphany-health-identity.ccmp --command-timeout-seconds 21600 --execute +ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --daemon-health-trust-store /etc/gamecult/idunn/daemon-health-trust.cc --rudp-health-bind 10.77.0.1:17870 --service-identity-store /var/lib/gamecult/idunn/idunn-service-identity.cc --public-health-store /var/lib/gamecult/idunn/authenticated-provider-health.cc --public-health-query-bind 127.0.0.1:17871 --command-timeout-seconds 21600 --execute Restart=always RestartSec=5 TimeoutStopSec=20 @@ -19,7 +19,8 @@ UMask=027 PrivateTmp=yes ProtectHome=yes ProtectSystem=full -ReadWritePaths=/var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadWritePaths=/etc/gamecult/idunn /var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadOnlyPaths=/var/lib/gamecult/idunn-authority ReadOnlyPaths=/var/lib/gamecult/idunn-authority [Install] From 67f215fb57f688f5f8258ccc1dd7bd4288c631da Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 20 Aug 2026 20:19:24 +0200 Subject: [PATCH 33/93] Keep Idunn UDP listeners alive across interrupted receives --- crates/idunn-daemon/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 770a69c..e624dc7 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -2913,7 +2913,8 @@ fn run_public_health_query_listener_loop( Err(error) if matches!( error.kind(), - std::io::ErrorKind::WouldBlock + std::io::ErrorKind::Interrupted + | std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut | std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::ConnectionAborted @@ -3146,7 +3147,8 @@ fn run_rudp_health_ingress_loop( Err(error) if matches!( error.kind(), - std::io::ErrorKind::WouldBlock + std::io::ErrorKind::Interrupted + | std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut | std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::ConnectionAborted From 5586910fa704b8a0d330b90a902a89b99566db77 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 20 Aug 2026 20:29:46 +0200 Subject: [PATCH 34/93] Respect coarse ingress timestamp precision --- crates/idunn-daemon/src/main.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index e624dc7..4d805fe 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -2640,6 +2640,13 @@ fn parse_timestamp_millis(value: &str) -> Option { u64::try_from(millis).ok() } +fn parse_admission_ceiling_millis(value: &str) -> Option { + if let Some(seconds) = parse_unix_timestamp(value) { + return seconds.checked_mul(1000)?.checked_add(999); + } + parse_timestamp_millis(value) +} + fn parse_utc_iso_timestamp_seconds(value: &str) -> Option { if value.len() < 19 { return None; @@ -3451,7 +3458,7 @@ fn authenticate_generic_signed_health( }, ) .context("signed daemon health signature is invalid")?; - let admitted_at_unix_millis = parse_timestamp_millis(admitted_at) + let admitted_at_unix_millis = parse_admission_ceiling_millis(admitted_at) .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; if statement.observed_at_unix_millis > admitted_at_unix_millis || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) @@ -6351,6 +6358,16 @@ fn help_text() -> &'static str { mod tests { use super::*; + #[test] + fn coarse_unix_admission_time_covers_its_whole_second() { + assert_eq!(parse_timestamp_millis("unix:42"), Some(42_000)); + assert_eq!(parse_admission_ceiling_millis("unix:42"), Some(42_999)); + assert_eq!( + parse_admission_ceiling_millis("2026-08-20T18:24:06.321Z"), + parse_timestamp_millis("2026-08-20T18:24:06.321Z") + ); + } + #[test] fn target_actuation_gate_serializes_request_owners() { let gate = Arc::new(TargetActuationGate::new()); From 7587ff01b6b0fd18aa95d672bc2be3b487e78b2b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 19:34:32 +0200 Subject: [PATCH 35/93] Separate Idunn continuity from deployment state --- crates/idunn-daemon/src/main.rs | 162 ++++++++++++++++--- docs/idunn-signed-daemon-health-authority.md | 6 + docs/idunn.md | 10 ++ 3 files changed, 153 insertions(+), 25 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 4d805fe..381b9c3 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -1966,16 +1966,20 @@ fn evaluate_target_health( IdunnDaemonHealthRecord, Option, )> { - if let Some(release) = &target.release { - if release.deployed_revision_witness.is_some() { - if let Some(health) = - evaluate_release_drift(target, desired, release, &SystemReleaseStatePort, now) - { - return Ok((supervisor_health_key(&desired.daemon_id), health, None)); - } - } - } if let Some(managed) = read_fresh_daemon_published_health(options, store_lock, desired, now)? { + // Daemon-owned health decides continuity. Release inspection is a + // deployment concern and may refine a healthy target into a planned + // rollout, but an unavailable Git checkout or upstream can never mask + // a healthy body or prevent an unhealthy body from being restarted. + if health_state_is_healthy(&managed.health.state) + && let Some(release) = &target.release + && release.deployed_revision_witness.is_some() + && let Some(drift) = + evaluate_release_drift(target, desired, release, &SystemReleaseStatePort, now) + && drift.state == "stale-deployment" + { + return Ok((supervisor_health_key(&desired.daemon_id), drift, None)); + } return Ok(( desired.daemon_id.clone(), managed.health, @@ -5738,13 +5742,7 @@ fn run_restart( options: &CommonOptions, ) -> IdunnRestartResultRecord { let result_id = format!("result:{}", request.request_id); - match run_braked_shell( - &request.command, - options, - &format!("restart:{}", request.daemon_id), - &request.request_id, - None, - ) { + match run_continuity_shell(&request.command, options, None) { Ok(output) if output.status.success() => IdunnRestartResultRecord { result_id, request_id: request.request_id.clone(), @@ -5955,8 +5953,7 @@ fn rollout_result_record( fn run_shell( command: &str, options: &CommonOptions, - release_id: &str, - deployment_id: &str, + deployment_brake: Option<(&str, &str)>, deployment: Option<&IdunnDeploymentRequestRecord>, ) -> Result { let mut process = if cfg!(windows) { @@ -6012,17 +6009,22 @@ fn run_shell( .with_context(|| format!("creating {}", stderr_path.display()))?; configure_command_lifetime(&mut process); - // Hold the root-owned authority lock from the final read through spawn. - // An operator engage therefore wins before this gate or waits until the - // already-authorized consequence has begun; captured release bytes cannot - // race a later engage into a new spawn. - let child = with_verified_deployment_brake(options, release_id, deployment_id, || { + let spawn = || { process .stdout(Stdio::from(stdout_file)) .stderr(Stdio::from(stderr_file)) .spawn() .with_context(|| format!("running command {command:?}")) - })?; + }; + let child = if let Some((release_id, deployment_id)) = deployment_brake { + // Hold the root-owned deployment-authority lock from the final read + // through spawn. This gate owns artifact/configuration mutation only. + with_verified_deployment_brake(options, release_id, deployment_id, spawn)? + } else { + // Restarting an already-admitted body is continuity physiology. It + // deliberately does not consume deployment authority. + spawn()? + }; let status = wait_for_child_status_with_timeout( child, Duration::from_secs(options.command_timeout_seconds), @@ -6049,7 +6051,20 @@ fn run_braked_shell( deployment_id: &str, deployment: Option<&IdunnDeploymentRequestRecord>, ) -> Result { - run_shell(command, options, release_id, deployment_id, deployment) + run_shell( + command, + options, + Some((release_id, deployment_id)), + deployment, + ) +} + +fn run_continuity_shell( + command: &str, + options: &CommonOptions, + deployment: Option<&IdunnDeploymentRequestRecord>, +) -> Result { + run_shell(command, options, None, deployment) } fn with_verified_deployment_brake( @@ -6358,6 +6373,52 @@ fn help_text() -> &'static str { mod tests { use super::*; + fn unconfigured_actuation_options(store_path: PathBuf) -> CommonOptions { + CommonOptions { + store_path, + release_authority_store_path: None, + deployment_brake_store_path: None, + deployment_brake_operator_anchor_path: None, + deployment_brake_runtime_id: None, + operator_alarm_command: None, + rudp_health_bind: None, + trusted_epiphany_health_identity_store: None, + daemon_health_trust_store_path: None, + service_identity_store_path: None, + public_health_store_path: None, + public_health_query_bind: None, + execute: true, + command_timeout_seconds: 2, + } + } + + #[test] + fn continuity_restart_does_not_consume_deployment_brake_authority() { + let root = tempfile::tempdir().unwrap(); + let options = unconfigured_actuation_options(root.path().join("idunn.cc")); + let request = IdunnRestartRequestRecord { + request_id: "restart:test:continuity".into(), + daemon_id: "test-daemon".into(), + command: "exit 0".into(), + authority: "idunn-supervisor-command.test".into(), + requested_at: "unix:100".into(), + }; + + let result = run_restart(&request, "unix:100", &options); + + assert_eq!(result.state, "succeeded"); + let deployment_error = run_braked_shell( + "exit 0", + &options, + "deployment-release", + "deployment-request", + None, + ) + .unwrap_err() + .to_string(); + assert!(deployment_error.contains("--deployment-brake-store is required")); + } + #[test] fn coarse_unix_admission_time_covers_its_whole_second() { assert_eq!(parse_timestamp_millis("unix:42"), Some(42_000)); @@ -8995,6 +9056,57 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + #[test] + fn unavailable_release_source_cannot_mask_authenticated_daemon_health() { + let (message, options, root) = generic_signed_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + let target = DaemonTarget { + daemon_id: "test-daemon".into(), + verse_id: "test.local".into(), + name: "Test daemon".into(), + health_contract: health_contract("test.signed-health", "failed"), + deploy_command: Some("deploy test".into()), + restart_command: Some("restart test".into()), + release: Some(with_deployed_revision_witness( + release_target( + "Missing", + root.path().join("missing-repository"), + "restart-after-verified-build", + None, + "restart-required", + ), + root.path().join("missing-deployment-witness"), + )), + enabled: true, + interval_seconds: 30, + }; + let desired = IdunnDesiredDaemonRecord { + daemon_id: target.daemon_id.clone(), + verse_id: target.verse_id.clone(), + name: target.name.clone(), + enabled: true, + health_command: None, + restart_command: target.restart_command.clone(), + deploy_command: target.deploy_command.clone(), + authority: "idunn-supervisor-command".into(), + max_silence_seconds: 60, + observed_at: "2026-07-19T12:00:02Z".into(), + health_contract: target.health_contract.id.clone(), + transport_profile_id: transport_profile_id(&target), + command_boundary_id: command_boundary_id(&target), + }; + + let (health_key, selected, authenticated) = + evaluate_target_health(&target, &options, &lock, &desired, "2026-07-19T12:00:02Z") + .unwrap(); + + assert_eq!(health_key, "test-daemon"); + assert_eq!(selected.state, "active"); + assert_eq!(selected.publication_source, "daemon-authenticated"); + assert!(authenticated.is_some()); + } + fn projection_fixture( release_bound: bool, ) -> ( diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index 12a252e..fd752a4 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -21,6 +21,12 @@ does not authenticate the publisher. It is diagnostic input until replaced. No renderer, transport peer, systemd unit, PID probe, caller boolean, or typed but unsigned packet owns health. +Authenticated daemon health is selected before release-source inspection. +Release state may request a rollout of an otherwise healthy body, but a missing +checkout or upstream cannot replace the daemon's liveness statement or block +restart of an unhealthy admitted body. Continuity restart and deployment are +separate authorities: only the latter is gated by the deployment brake. + ## Inputs The daemon-authored statement binds: diff --git a/docs/idunn.md b/docs/idunn.md index 2b21813..331f866 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -230,6 +230,16 @@ idunn.rudp_health_ingress.v1 restarting the stale artifact. If a target reports `dependency-unavailable` or `degraded`, Idunn must alarm instead of treating local deploy/restart as the owner. +- Daemon-owned health is evaluated before release inspection. A missing Git + checkout, unreachable upstream, or unreadable deployment witness cannot mask + fresh authenticated health and cannot prevent an unhealthy admitted body + from reaching its continuity restart path. Release inspection may refine a + healthy body into `stale-deployment`; it does not own liveness. +- Restarting an already-admitted installed body is continuity physiology and + does not consume deployment-brake authority. Artifact, configuration, + schema, unit, and authority-binding changes remain deployments and must pass + the deployment brake. A separate lifecycle brake is required if operators + need to suspend continuity actuation itself. - Idunn should fail closed when authority is unclear. Unknown ownership, repeated restart failure, missing command authority, or degraded health that needs a human becomes an operator alarm. From c7012a87c9165de4bd382d08562a9e1c2f43aed4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 19:50:24 +0200 Subject: [PATCH 36/93] Admit Odin as a signed Idunn target --- README.md | 5 + crates/idunn-daemon/src/main.rs | 129 ++++++++++++++++++- deploy/cultlib.commit | 1 + docs/idunn-signed-daemon-health-authority.md | 7 + docs/idunn.md | 16 ++- scripts/linux/idunn-yggdrasil | 4 +- src/Idunn/README.md | 8 +- src/odin/config.cjs | 4 + src/odin/idunn-rudp.cjs | 105 ++++++++++++--- test/idunn-signed-health.test.cjs | 65 ++++++++++ 10 files changed, 314 insertions(+), 30 deletions(-) create mode 100644 deploy/cultlib.commit create mode 100644 test/idunn-signed-health.test.cjs diff --git a/README.md b/README.md index 961e921..28225c1 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,11 @@ Local package surfaces: - Hosted VoidBot release: upstream `main` -> Yggdrasil-local Idunn -> the root-owned `/srv/odin/deploy-manifests/voidbot` actuator. Local checkouts are development and diagnostics only; do not create a local deployment. +- Hosted Odin release: branch `codex/ygg-idunn-independent-bootstrap` plus the + exact CultLib commit in `deploy/cultlib.commit` -> one immutable + `/srv/odin/releases/` body -> `/srv/odin/current`. Odin signs its own + liveness to Idunn, while Idunn starts independently and owns later restart or + deployment actuation. - Operator escalation: Bifrost-owned CultMesh crossing; current compatibility delivery calls `scripts\notify-idunn-operator-alarm.ps1`, which asks Bifrost to publish a typed `gamecult.operator_dm_request.v1` CultMesh command document diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 381b9c3..bdb3edf 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -4157,6 +4157,25 @@ fn release_target( } } +fn release_target_on_branch( + repo: &str, + repo_path: PathBuf, + upstream_branch: &str, + rollout_strategy: &str, + state_migration_command: Option<&str>, + zero_downtime_capability: &str, +) -> ReleaseTarget { + let mut target = release_target( + repo, + repo_path, + rollout_strategy, + state_migration_command, + zero_downtime_capability, + ); + target.upstream_branch = upstream_branch.to_string(); + target +} + fn requiring_bifrost_authority(mut release: ReleaseTarget) -> ReleaseTarget { release.requires_bifrost_authority = true; release @@ -4923,6 +4942,57 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { match options.profile.as_str() { "yggdrasil-local" => Ok(vec![ + DaemonTarget { + daemon_id: "yggdrasil-odin".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Odin all-seer".to_string(), + health_contract: locally_supervised_health_contract( + "odin.cultnet-rudp-provider-health", + "failed", + ), + deploy_command: Some(yggdrasil_actuator("deploy", "odin")), + restart_command: Some(yggdrasil_actuator("restart", "odin")), + release: Some(with_deployed_revision_witness( + release_target_on_branch( + "Odin", + PathBuf::from("/srv/build/Odin"), + "codex/ygg-idunn-independent-bootstrap", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/odin/deploy/deployment.env"), + )), + enabled: true, + interval_seconds: 30, + }, + DaemonTarget { + daemon_id: "yggdrasil-ghostlight".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Ghostlight Dungeon".to_string(), + health_contract: locally_supervised_health_contract( + "ghostlight.cultnet-rudp-service-health", + "failed", + ), + deploy_command: Some(yggdrasil_actuator("deploy", "ghostlight")), + restart_command: Some(yggdrasil_actuator("restart", "ghostlight")), + release: Some(with_deployed_revision_witness( + release_target_on_branch( + "Ghostlight", + PathBuf::from("/srv/build/Ghostlight"), + "codex/ghostlight-dungeon-mvp", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/ghostlight/deploy/deployment.env"), + )), + // Ghostlight becomes a continuity target only after its Linux + // body and signed-health identity have been admitted. A + // missing service is not an unhealthy installed service. + enabled: false, + interval_seconds: 30, + }, DaemonTarget { daemon_id: "yggdrasil-voidbot".to_string(), verse_id: "yggdrasil.local".to_string(), @@ -4968,9 +5038,10 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { deploy_command: Some(yggdrasil_actuator("deploy", "epiphany")), restart_command: Some(yggdrasil_actuator("restart", "epiphany")), release: Some(requiring_bifrost_authority(with_deployed_revision_witness( - release_target( + release_target_on_branch( "Epiphany", PathBuf::from("/srv/build/Epiphany"), + "codex/epiphany-shakedown-live", "restart-after-verified-build", None, "restart-required", @@ -7706,7 +7777,7 @@ mod tests { assert_eq!(release.repository_full_name, "GameCult/Epiphany"); assert_eq!(release.repo_path, PathBuf::from("/srv/build/Epiphany")); assert_eq!(release.upstream_remote, "origin"); - assert_eq!(release.upstream_branch, "main"); + assert_eq!(release.upstream_branch, "codex/epiphany-shakedown-live"); assert_eq!(release.rollout_strategy, "restart-after-verified-build"); assert_eq!(release.state_migration_command, None); assert_eq!(release.zero_downtime_capability, "restart-required"); @@ -7760,7 +7831,7 @@ mod tests { ) ); assert!(actuator.contains( - "voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false" + "voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false" )); assert!( actuator @@ -7785,6 +7856,58 @@ mod tests { ); } + #[test] + fn yggdrasil_runtime_catalog_includes_discovery_and_dungeon_bodies() { + let targets = swarm_targets(&SwarmOptions { + profile: "yggdrasil-local".to_string(), + repo_root: PathBuf::from("/srv/odin/source"), + }) + .expect("yggdrasil-local targets"); + + for (daemon_id, repo, branch, witness, actuator_name) in [ + ( + "yggdrasil-odin", + "Odin", + "codex/ygg-idunn-independent-bootstrap", + "/srv/odin/deploy/deployment.env", + "odin", + ), + ( + "yggdrasil-ghostlight", + "Ghostlight", + "codex/ghostlight-dungeon-mvp", + "/srv/ghostlight/deploy/deployment.env", + "ghostlight", + ), + ] { + let target = targets + .iter() + .find(|target| target.daemon_id == daemon_id) + .unwrap_or_else(|| panic!("missing {daemon_id}")); + assert!(target.health_contract.restart_on_missing_publication); + assert_eq!( + target.restart_command.as_deref(), + Some(format!( + "sudo -n /usr/local/libexec/idunn-yggdrasil restart {actuator_name}" + )) + .as_deref() + ); + let release = target.release.as_ref().expect("release target"); + assert_eq!(release.repo, repo); + assert_eq!(release.upstream_branch, branch); + assert_eq!( + release.deployed_revision_witness, + Some(PathBuf::from(witness)) + ); + assert!(!release.requires_bifrost_authority); + assert_eq!(target.enabled, daemon_id == "yggdrasil-odin"); + } + + let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); + assert!(actuator.contains("deploy:odin|restart:odin")); + assert!(actuator.contains("deploy:ghostlight|restart:ghostlight")); + } + struct FakeReleaseStatePort { fetch_error: Option, desired: std::result::Result, diff --git a/deploy/cultlib.commit b/deploy/cultlib.commit new file mode 100644 index 0000000..d580d40 --- /dev/null +++ b/deploy/cultlib.commit @@ -0,0 +1 @@ +21163d83fa2670dbcc9cdd57c6c1776b33a91d62 diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index fd752a4..c74a277 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -44,6 +44,13 @@ The daemon-authored statement binds: The Idunn trust binding names the only public key allowed to speak for that daemon/contract/runtime tuple and whether release identity is required. +Yggdrasil Odin uses the generic signed contract with daemon +`yggdrasil-odin`, source runtime `odin-yggdrasil`, and a root-provisioned +Ed25519 key readable only by the Odin runtime group. Its health signature owns +liveness. Exact Odin and CultLib revisions remain deployment authority in the +immutable release and `/srv/odin/deploy/deployment.env`; neither Docker health +nor the signed liveness statement may rewrite that witness. + ## Outputs Idunn persists two separate facts: diff --git a/docs/idunn.md b/docs/idunn.md index 331f866..7a9e637 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -13,10 +13,11 @@ escalation, and continuity witness state. ## Authority Map -- Owner: Idunn owns daemon lifecycle management after Odin has accepted where a - daemon lives: bring-up after host reboot, deployment freshness, crash +- Owner: Idunn owns daemon lifecycle management from its root-admitted target + catalog: bring-up after host reboot, deployment freshness, crash recovery, health watching, deploy/restart policy, and operator escalation. -- Inputs: Odin's accepted service records, provider advertisements, `.cc` +- Inputs: its admitted target catalog, Odin's accepted service records when + Odin is available, provider advertisements, `.cc` witnesses, advertised command boundaries, CultNet/RUDP health contracts, freshness windows, operator policy, local service manager state, and explicit deployment/debug witnesses that cannot satisfy daemon health. @@ -35,6 +36,8 @@ escalation, and continuity witness state. Agents are also forbidden deploy writers: they configure Idunn release targets, command boundaries, migration commands, and rollout policy, then let Idunn actuate and witness deployment. They do not run deploy scripts by hand. + Odin is itself an Idunn target; Odin discovery is never a prerequisite for + starting Idunn or recovering Odin. - Shared paths: manual operator deploy/restart, scheduled deploy/restart, degraded-health repair, boot rehydration, and future remote worker recovery must pass through the same Idunn command primitive. @@ -317,8 +320,13 @@ idunn.rudp_health_ingress.v1 lacks hot reload, blue/green routing, rolling instances, or another named in-place swap mechanism, Idunn records `restart-required` and verifies the restart path honestly. -- Yggdrasil Heimdall, repixelizer, and StreamPixels are enforced through the +- Yggdrasil Odin, Heimdall, repixelizer, and StreamPixels are enforced through the generic source artifact lane and their existing `gamecult-ops` runbooks. + Odin publishes `idunn.signed_daemon_health.v1` with its root-bound Ed25519 + identity. Its immutable release pins both the Odin commit and the exact + CultLib commit, then switches them through one `/srv/odin/current` link. + Ghostlight is present but disabled in the catalog until its Linux runtime and + signed-health identity are admitted; absence is not treated as failed health. Bifrost is explicitly blocked as of 2026-06-09: committed Bifrost `HEAD` expects `UserAccounts.HeimdallAccountId`, while Yggdrasil's production database lacks that column and EF reports no pending migration. Idunn must diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index 09ae681..a6befa3 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -20,7 +20,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - voidbot|heimdall|repixelizer|streampixels) target_requires_bifrost_authority=false ;; + voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -69,7 +69,7 @@ case "$action:$target" in restart:streampixels) docker compose -f "$compose" restart streampixels-service streampixels-web ;; - deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany) + deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:odin|restart:odin|deploy:ghostlight|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 diff --git a/src/Idunn/README.md b/src/Idunn/README.md index 0685d63..b404cf8 100644 --- a/src/Idunn/README.md +++ b/src/Idunn/README.md @@ -55,7 +55,9 @@ instead of building one more private watchdog. Idunn is a Rust daemon inside Odin's Cargo workspace. Starfire and Yggdrasil run separate host-local swarm profiles. Starfire owns its local adjunct daemons and remote display/telemetry lanes. Yggdrasil owns its hosted release targets, -including VoidBot. Each target declares a daemon-owned health contract and keeps +including Odin and VoidBot. Idunn starts independently of every managed target, +so an Odin outage cannot remove the organ responsible for recovering Odin. +Each target declares a daemon-owned health contract and keeps its own interval and deploy/restart authority. Daemon truth comes from typed CultNet/RUDP publication and daemon-owned boundary stores. Shell scripts remain deployment, restart, and manual diagnostic lowerings only; they do not satisfy @@ -177,7 +179,9 @@ The enforced targets are Nightwing Gjallar plus the Yggdrasil source artifact lanes whose ops runbooks can produce and verify deployment manifests. Each enforced target must declare upstream remote/branch, rollout strategy, state migration authority, and zero-downtime capability. Idunn deploys the declared -upstream `main` revision, not arbitrary local `HEAD`. Bifrost is blocked until +declared upstream revision, not arbitrary local `HEAD`. Ghostlight's catalog +entry remains disabled until its Linux body and signed health identity are +admitted. Bifrost is blocked until its production database migration path is coherent; mobile device installs remain blocked at their physical approval boundaries; GitHub Pages remains external-owned. diff --git a/src/odin/config.cjs b/src/odin/config.cjs index 3fcf2f5..e9df2b4 100644 --- a/src/odin/config.cjs +++ b/src/odin/config.cjs @@ -29,6 +29,10 @@ function buildConfig(argv) { endpoint: String(args["idunn-rudp-health"]), daemonId: String(args["idunn-daemon"] || "odin"), healthContract: String(args["idunn-health-contract"] || "odin.cultnet-rudp-provider-health"), + privateKeyPath: args["idunn-health-private-key"] + ? String(args["idunn-health-private-key"]) + : "", + sourceRuntimeId: String(args["idunn-source-runtime"] || "odin-coordinator"), } : null; const interfaceBindingStores = String( args.interfaceBindingStore || diff --git a/src/odin/idunn-rudp.cjs b/src/odin/idunn-rudp.cjs index 227eee7..d3e3188 100644 --- a/src/odin/idunn-rudp.cjs +++ b/src/odin/idunn-rudp.cjs @@ -1,6 +1,8 @@ "use strict"; const dgram = require("dgram"); +const crypto = require("crypto"); +const fs = require("fs"); const path = require("path"); const { createRequire } = require("module"); @@ -26,15 +28,32 @@ const { encode } = requireCultNet("@msgpack/msgpack"); const CULTNET_RUDP_PROTOCOL_ID = "cultnet.transport.rudp.v0"; const IDUNN_HEALTH_RUDP_CONNECTION_ID = 0x1d0d0001; +const SIGNED_HEALTH_SCHEMA = "idunn.signed_daemon_health.v1"; +const SIGNATURE_DOMAIN = Buffer.from("gamecult.provider-health.signature.v1\0", "utf8"); +const ID_DOMAIN = Buffer.from("gamecult.provider-health.identity.v1\0", "utf8"); +const SIGNING_PURPOSE = Buffer.from(SIGNED_HEALTH_SCHEMA, "utf8"); function createIdunnRudpHealthPublisher(options) { if (!options) return null; const endpoint = parseEndpoint(options.endpoint); - return { + const publisher = { daemonId: options.daemonId, endpoint, healthContract: options.healthContract, + publisherIncarnationId: crypto.randomUUID(), + publisherSequence: 0, + sourceRuntimeId: options.sourceRuntimeId || "odin-coordinator", }; + if (options.privateKeyPath) { + publisher.privateKey = crypto.createPrivateKey(fs.readFileSync(options.privateKeyPath)); + publisher.publicKey = rawEd25519PublicKey(publisher.privateKey); + publisher.signerIdentityId = crypto + .createHash("sha256") + .update(ID_DOMAIN) + .update(publisher.publicKey) + .digest("hex"); + } + return publisher; } async function publishIdunnRudpHealth(publisher, health) { @@ -61,34 +80,28 @@ async function publishIdunnRudpHealth(publisher, health) { ); const observedAt = health.observedAt || new Date().toISOString(); - const record = { - daemonId: publisher.daemonId, - state: health.state, - detail: health.detail, + const signed = publisher.privateKey + ? signedHealthPayload(publisher, health, observedAt) + : null; + const payload = signed?.payload || encode([ + publisher.daemonId, + health.state, + String(health.detail || "").slice(0, 512), observedAt, - healthContract: publisher.healthContract, - publicationSource: "daemon-published", - transport: CULTNET_RUDP_PROTOCOL_ID, - }; - const payload = encode([ - record.daemonId, - record.state, - record.detail, - record.observedAt, - record.healthContract, - record.publicationSource, - record.transport, + publisher.healthContract, + "daemon-published", + CULTNET_RUDP_PROTOCOL_ID, ]); const message = { schemaVersion: "cultnet.document_put_raw.v0", messageId: `odin-health:${publisher.daemonId}:${observedAt.replace(/[:.]/g, "-")}`, document: { - schemaId: "idunn.daemon_health", + schemaId: signed ? SIGNED_HEALTH_SCHEMA : "idunn.daemon_health", recordKey: publisher.daemonId, storedAt: observedAt, payloadEncoding: "messagepack", payload, - sourceRuntimeId: "odin-coordinator", + sourceRuntimeId: publisher.sourceRuntimeId, sourceRole: "daemon-health-publisher", tags: [CULTNET_RUDP_PROTOCOL_ID], }, @@ -117,6 +130,58 @@ async function publishIdunnRudpHealth(publisher, health) { } } +function signedHealthPayload(publisher, health, observedAt) { + publisher.publisherSequence += 1; + const observedAtUnixMillis = Date.parse(observedAt); + if (!Number.isSafeInteger(observedAtUnixMillis) || observedAtUnixMillis <= 0) { + throw new Error("Idunn signed health observation time is invalid."); + } + const unsigned = [ + SIGNED_HEALTH_SCHEMA, + publisher.daemonId, + publisher.healthContract, + publisher.sourceRuntimeId, + health.state, + String(health.detail || "").slice(0, 512), + publisher.signerIdentityId, + publisher.publisherIncarnationId, + publisher.publisherSequence, + observedAtUnixMillis, + null, + null, + null, + null, + "ed25519", + new Uint8Array(), + false, + ]; + const unsignedPayload = encode(unsigned); + const signature = crypto.sign(null, signingMessage(unsignedPayload), publisher.privateKey); + const statement = unsigned.slice(); + statement[15] = new Uint8Array(signature); + return { payload: encode(statement), statement, unsignedPayload }; +} + +function signingMessage(payload) { + const purposeLength = Buffer.alloc(8); + purposeLength.writeBigUInt64BE(BigInt(SIGNING_PURPOSE.length)); + const payloadLength = Buffer.alloc(8); + payloadLength.writeBigUInt64BE(BigInt(payload.length)); + return Buffer.concat([ + SIGNATURE_DOMAIN, + purposeLength, + SIGNING_PURPOSE, + payloadLength, + Buffer.from(payload), + ]); +} + +function rawEd25519PublicKey(privateKey) { + const der = crypto.createPublicKey(privateKey).export({ type: "spki", format: "der" }); + if (der.length < 32) throw new Error("Ed25519 public key export is too short."); + return Buffer.from(der.subarray(der.length - 32)); +} + async function bindSocket(socket, endpoint) { await new Promise((resolve, reject) => { socket.once("error", reject); @@ -254,4 +319,6 @@ module.exports = { CULTNET_RUDP_PROTOCOL_ID, createIdunnRudpHealthPublisher, publishIdunnRudpHealth, + signedHealthPayload, + signingMessage, }; diff --git a/test/idunn-signed-health.test.cjs b/test/idunn-signed-health.test.cjs new file mode 100644 index 0000000..c08795c --- /dev/null +++ b/test/idunn-signed-health.test.cjs @@ -0,0 +1,65 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const crypto = require("node:crypto"); +const test = require("node:test"); + +const { + signedHealthPayload, + signingMessage, +} = require("../src/odin/idunn-rudp.cjs"); + +test("Odin signs the complete canonical provider-health statement", () => { + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + const publicDer = publicKey.export({ type: "spki", format: "der" }); + const rawPublicKey = Buffer.from(publicDer.subarray(publicDer.length - 32)); + const signerIdentityId = crypto + .createHash("sha256") + .update(Buffer.from("gamecult.provider-health.identity.v1\0")) + .update(rawPublicKey) + .digest("hex"); + const publisher = { + daemonId: "yggdrasil-odin", + healthContract: "odin.cultnet-rudp-provider-health", + privateKey, + publisherIncarnationId: "24e0dc7f-6a92-4aa8-9f56-fb513d265e13", + publisherSequence: 0, + signerIdentityId, + sourceRuntimeId: "odin-yggdrasil", + }; + + const signed = signedHealthPayload( + publisher, + { state: "active", detail: "catalog current" }, + "2026-08-21T12:34:56.789Z", + ); + + assert.equal(signed.statement[0], "idunn.signed_daemon_health.v1"); + assert.equal(signed.statement[1], "yggdrasil-odin"); + assert.equal(signed.statement[2], "odin.cultnet-rudp-provider-health"); + assert.equal(signed.statement[3], "odin-yggdrasil"); + assert.equal(signed.statement[8], 1); + assert.equal(signed.statement[9], 1787315696789); + assert.equal(signed.statement[15].byteLength, 64); + assert.equal( + crypto.verify( + null, + signingMessage(signed.unsignedPayload), + publicKey, + Buffer.from(signed.statement[15]), + ), + true, + ); + + const tampered = Buffer.from(signed.unsignedPayload); + tampered[tampered.length - 1] ^= 1; + assert.equal( + crypto.verify( + null, + signingMessage(tampered), + publicKey, + Buffer.from(signed.statement[15]), + ), + false, + ); +}); From 13caf9bad52426bc816b19cebb1f755855dfc59d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 20:07:57 +0200 Subject: [PATCH 37/93] Remove Odin runtime from Idunn bootstrap inputs --- crates/idunn-daemon/src/main.rs | 2 ++ docs/idunn.md | 3 +++ scripts/linux/idunn-yggdrasil.service | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index bdb3edf..8749030 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -7814,6 +7814,8 @@ mod tests { "--release-authority-store /srv/bifrost/state/repository-release-authority.cc" )); assert!(unit.contains("--command-timeout-seconds 21600")); + assert!(unit.contains("--repo-root /var/lib/gamecult/idunn")); + assert!(!unit.contains("--repo-root /srv/odin/source")); assert!(unit.contains( "--deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc" )); diff --git a/docs/idunn.md b/docs/idunn.md index 7a9e637..3204c97 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -243,6 +243,9 @@ idunn.rudp_health_ingress.v1 schema, unit, and authority-binding changes remain deployments and must pass the deployment brake. A separate lifecycle brake is required if operators need to suspend continuity actuation itself. +- Yggdrasil's `--repo-root` is Idunn-owned state, not Odin's live release. + The hosted profile uses explicit build-repository and root-manifest paths; + Idunn startup and recovery therefore do not read `/srv/odin/current`. - Idunn should fail closed when authority is unclear. Unknown ownership, repeated restart failure, missing command authority, or degraded health that needs a human becomes an operator alarm. diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index d8a21fe..6490b10 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -10,7 +10,7 @@ User=idunn Group=idunn # Operational cancellation ceiling only. Target daemons own readiness through # typed health; this duration must never be interpreted as deployment success. -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --daemon-health-trust-store /etc/gamecult/idunn/daemon-health-trust.cc --rudp-health-bind 10.77.0.1:17870 --service-identity-store /var/lib/gamecult/idunn/idunn-service-identity.cc --public-health-store /var/lib/gamecult/idunn/authenticated-provider-health.cc --public-health-query-bind 127.0.0.1:17871 --command-timeout-seconds 21600 --execute +ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /var/lib/gamecult/idunn --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --daemon-health-trust-store /etc/gamecult/idunn/daemon-health-trust.cc --rudp-health-bind 10.77.0.1:17870 --service-identity-store /var/lib/gamecult/idunn/idunn-service-identity.cc --public-health-store /var/lib/gamecult/idunn/authenticated-provider-health.cc --public-health-query-bind 127.0.0.1:17871 --command-timeout-seconds 21600 --execute Restart=always RestartSec=5 TimeoutStopSec=20 From 0ad0067c39dbf301b73cc5a38e55800da653dab6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 20:14:15 +0200 Subject: [PATCH 38/93] Fix signed daemon health schema admission --- crates/idunn-daemon/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 8749030..dac215f 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -60,7 +60,7 @@ const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; -const SIGNED_DAEMON_HEALTH_TYPE: &str = "idunn.signed_daemon_health"; +const SIGNED_DAEMON_HEALTH_SCHEMA_ID: &str = odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA; struct TargetActuationGate { lock: Mutex<()>, @@ -3696,7 +3696,7 @@ fn decode_health_from_rudp_message( if document.payload_encoding != CultNetRawPayloadEncoding::Messagepack { return Err(anyhow!("expected MessagePack raw payload encoding")); } - if document.schema_id == SIGNED_DAEMON_HEALTH_TYPE { + if document.schema_id == SIGNED_DAEMON_HEALTH_SCHEMA_ID { let (health, statement, admission) = authenticate_generic_signed_health(document, options, admitted_at)?; return Ok(DecodedHealthIngress::AuthenticatedGeneric { @@ -9033,7 +9033,7 @@ mod tests { let message = CultNetMessage::DocumentPutRaw { message_id: format!("signed-health-{sequence}"), document: CultNetRawDocumentRecord { - schema_id: SIGNED_DAEMON_HEALTH_TYPE.into(), + schema_id: SIGNED_DAEMON_HEALTH_SCHEMA_ID.into(), record_key: statement.daemon_id.clone(), stored_at: "2026-07-19T12:00:00Z".into(), payload_encoding: CultNetRawPayloadEncoding::Messagepack, From 7c5246fc098d1e9ae02480eb10d5bb8524620c9b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 20:59:11 +0200 Subject: [PATCH 39/93] Give Idunn Epiphany source change authority --- crates/idunn-daemon/src/main.rs | 166 +++++++++++++++++++++++--------- docs/idunn.md | 30 +++--- scripts/linux/idunn-yggdrasil | 8 +- 3 files changed, 135 insertions(+), 69 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index dac215f..21d2054 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -167,6 +167,7 @@ struct ReleaseTarget { zero_downtime_capability: String, deployed_revision_witness: Option, requires_bifrost_authority: bool, + source_change_pathspecs: Vec, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -434,13 +435,17 @@ struct SystemReleaseStatePort; impl ReleaseStatePort for SystemReleaseStatePort { fn fetch(&self, release: &ReleaseTarget) -> Result<()> { + let fetch_refspec = format!( + "refs/heads/{}:refs/remotes/{}/{}", + release.upstream_branch, release.upstream_remote, release.upstream_branch + ); let status = Command::new("git") .arg("-C") .arg(&release.repo_path) .arg("fetch") .arg("--quiet") .arg(&release.upstream_remote) - .arg(&release.upstream_branch) + .arg(&fetch_refspec) .status() .with_context(|| { format!( @@ -457,17 +462,44 @@ impl ReleaseStatePort for SystemReleaseStatePort { } fn desired_revision(&self, release: &ReleaseTarget) -> Result { - git_revision( - &release.repo_path, - &format!("{}/{}", release.upstream_remote, release.upstream_branch), - ) - .ok_or_else(|| { - anyhow!( - "cannot resolve {}/{}", - release.upstream_remote, - release.upstream_branch - ) - }) + let upstream = format!("{}/{}", release.upstream_remote, release.upstream_branch); + if release.source_change_pathspecs.is_empty() { + return git_revision(&release.repo_path, &upstream).ok_or_else(|| { + anyhow!( + "cannot resolve {}/{}", + release.upstream_remote, + release.upstream_branch + ) + }); + } + let output = Command::new("git") + .arg("-C") + .arg(&release.repo_path) + .arg("log") + .arg("-1") + .arg("--format=%H") + .arg(&upstream) + .arg("--") + .args(&release.source_change_pathspecs) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .with_context(|| { + format!( + "resolving source-changing revision for {}/{}", + release.upstream_remote, release.upstream_branch + ) + })?; + if !output.status.success() { + return Err(anyhow!( + "git log exited with {} while resolving source-changing revision: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + )); + } + let revision = String::from_utf8_lossy(&output.stdout).trim().to_string(); + validate_commit_sha(&revision)?; + Ok(revision) } fn deployed_revision(&self, release: &ReleaseTarget) -> Result { @@ -1816,7 +1848,7 @@ fn revalidate_deployment_request( || !request.release_authority_envelope_sha256.is_empty() { return Err(anyhow!( - "legacy release request cannot carry Bifrost authority claims" + "source-observed release request cannot carry Bifrost authority claims" )); } return Ok(()); @@ -3643,11 +3675,16 @@ fn validate_generic_release_binding( let request = node .get::(deployment_id)? .ok_or_else(|| anyhow!("signed daemon health names an unknown deployment request"))?; + let authority_shape_is_valid = if request.requires_bifrost_authority { + !request.release_authority_id.trim().is_empty() + && !request.release_authority_envelope_sha256.trim().is_empty() + } else { + request.release_authority_id.is_empty() + && request.release_authority_envelope_sha256.is_empty() + }; if request.daemon_id != admission.daemon_id || admission.source_commit.as_deref() != Some(request.source_revision.as_str()) - || !request.requires_bifrost_authority - || request.release_authority_id.trim().is_empty() - || request.release_authority_envelope_sha256.trim().is_empty() + || !authority_shape_is_valid { return Err(anyhow!( "signed daemon health deployment authority is invalid" @@ -3933,14 +3970,14 @@ fn validate_admission_against_current_request( .ok_or_else(|| anyhow!("signed health names an unknown deployment request"))?; if request.daemon_id != admission.daemon_id || request.source_revision != admission.source_commit - || !request.requires_bifrost_authority || request.repository_full_name != "GameCult/Epiphany" - || request.upstream_ref != "refs/heads/main" - || request.release_authority_id.trim().is_empty() - || request.release_authority_envelope_sha256.trim().is_empty() + || request.upstream_ref != "refs/heads/codex/epiphany-shakedown-live" + || request.requires_bifrost_authority + || !request.release_authority_id.is_empty() + || !request.release_authority_envelope_sha256.is_empty() { return Err(anyhow!( - "signed health does not match an exact Bifrost-authorized Epiphany deployment request" + "signed health does not match an exact Idunn source-observed Epiphany deployment request" )); } let request_time = parse_timestamp_seconds(&request.requested_at) @@ -4154,6 +4191,7 @@ fn release_target( zero_downtime_capability: zero_downtime_capability.to_string(), deployed_revision_witness: None, requires_bifrost_authority: false, + source_change_pathspecs: Vec::new(), } } @@ -4186,6 +4224,11 @@ fn with_deployed_revision_witness(mut release: ReleaseTarget, path: PathBuf) -> release } +fn with_source_change_pathspecs(mut release: ReleaseTarget, pathspecs: &[&str]) -> ReleaseTarget { + release.source_change_pathspecs = pathspecs.iter().map(|value| (*value).to_string()).collect(); + release +} + fn release_target_record( target: &DaemonTarget, release: &ReleaseTarget, @@ -4231,7 +4274,7 @@ fn release_target_record( if release.requires_bifrost_authority { "tracked-authorized" } else { - "tracked-legacy-authority" + "tracked-source-authority" } } else { "release-authority-unavailable" @@ -4246,7 +4289,7 @@ fn release_target_record( upstream_branch: release.upstream_branch.clone(), desired_revision, deployed_revision, - artifact_strategy: "source-archive-from-upstream-main".to_string(), + artifact_strategy: "source-archive-from-observed-ref".to_string(), rollout_strategy: release.rollout_strategy.clone(), state_migration_authority: if release.state_migration_command.is_some() { "daemon-owned-command" @@ -4270,7 +4313,7 @@ fn release_target_record( release_authority_status: if authorization.is_some() { "authorized" } else if !release.requires_bifrost_authority { - "legacy-unmigrated" + "source-observed" } else { "unavailable" } @@ -4344,7 +4387,7 @@ fn rollout_plan_record( planned_at: &str, ) -> IdunnRolloutPlanRecord { let mut phases = vec![ - "fetch upstream main into a managed release view".to_string(), + "fetch the admitted upstream ref into a managed release view".to_string(), "build source artifact from the desired upstream revision".to_string(), "snapshot state before any non-noop migration".to_string(), "run daemon-owned migration command when declared".to_string(), @@ -5037,17 +5080,28 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { health_contract: health_contract("epiphany.cultnet-rudp-runtime-health", "failed"), deploy_command: Some(yggdrasil_actuator("deploy", "epiphany")), restart_command: Some(yggdrasil_actuator("restart", "epiphany")), - release: Some(requiring_bifrost_authority(with_deployed_revision_witness( - release_target_on_branch( - "Epiphany", - PathBuf::from("/srv/build/Epiphany"), - "codex/epiphany-shakedown-live", - "restart-after-verified-build", - None, - "restart-required", + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target_on_branch( + "Epiphany", + PathBuf::from("/srv/build/Epiphany"), + "codex/epiphany-shakedown-live", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/epiphany/deploy/deployment.env"), ), - PathBuf::from("/srv/epiphany/deploy/deployment.env"), - ))), + &[ + ".", + ":(exclude)docs/**", + ":(exclude)notes/**", + ":(exclude)state/ledgers.msgpack", + ":(exclude)state/map.yaml", + ":(exclude)state/scratch*", + ":(exclude)*.md", + ], + )), enabled: true, interval_seconds: 300, }, @@ -6437,7 +6491,13 @@ fn unix_epoch_millis() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\nIdunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets require an explicit Bifrost CultCache path via --release-authority-store. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + concat!( + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n", + " idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n", + " idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n", + " idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\n", + "Idunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets that require Bifrost authorization need an explicit CultCache path via --release-authority-store; source-observed targets do not borrow that authority. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + ) } #[cfg(test)] @@ -7742,7 +7802,7 @@ mod tests { } #[test] - fn epiphany_yggdrasil_target_requires_bifrost_authority_and_exact_witness() { + fn epiphany_yggdrasil_target_uses_source_change_authority_and_exact_witness() { let yggdrasil = swarm_targets(&SwarmOptions { profile: "yggdrasil-local".to_string(), repo_root: PathBuf::from("/srv/odin/source"), @@ -7785,21 +7845,33 @@ mod tests { release.deployed_revision_witness, Some(PathBuf::from("/srv/epiphany/deploy/deployment.env")) ); - assert!(release.requires_bifrost_authority); + assert!(!release.requires_bifrost_authority); + assert_eq!( + release.source_change_pathspecs, + vec![ + ".", + ":(exclude)docs/**", + ":(exclude)notes/**", + ":(exclude)state/ledgers.msgpack", + ":(exclude)state/map.yaml", + ":(exclude)state/scratch*", + ":(exclude)*.md", + ] + ); for legacy in yggdrasil .iter() .filter(|target| { !matches!( target.daemon_id.as_str(), - "yggdrasil-epiphany" | "yggdrasil-bifrost-persona-feedback" + "yggdrasil-bifrost-persona-feedback" ) }) .filter_map(|target| target.release.as_ref()) { assert!( !legacy.requires_bifrost_authority, - "{} must stay on its explicit legacy authority posture", + "{} must stay on its explicit source authority posture", legacy.repo ); } @@ -7828,12 +7900,10 @@ mod tests { assert!(actuator.contains("deploy:epiphany|restart:epiphany")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); assert!( - actuator.contains( - "epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true" - ) + actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") ); assert!(actuator.contains( - "voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false" + "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false" )); assert!( actuator @@ -9919,11 +9989,11 @@ mod tests { authority: "idunn-supervisor-command.manual".into(), requested_at: "unix:1784160000".into(), repository_full_name: "GameCult/Epiphany".into(), - upstream_ref: "refs/heads/main".into(), + upstream_ref: "refs/heads/codex/epiphany-shakedown-live".into(), source_revision: "b".repeat(40), - release_authority_id: "authority-fixture".into(), - release_authority_envelope_sha256: format!("sha256-{}", "d".repeat(64)), - requires_bifrost_authority: true, + release_authority_id: String::new(), + release_authority_envelope_sha256: String::new(), + requires_bifrost_authority: false, }, ) .unwrap(); diff --git a/docs/idunn.md b/docs/idunn.md index 3204c97..0b77a15 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -99,23 +99,19 @@ Idunn now shares Odin's Rust body: executable manifest exists for that service under `/srv/odin/deploy-manifests`. It does not inherit Starfire paths, SSH keys, ADB authority, or Windows process ownership. -- Release authorization is adopted per target. Epiphany requires an exact - `bifrost.repository_release_authority.v1` receipt from the explicitly - configured Bifrost store before migration or deployment; its request, - artifact, and remote manifest lineage freeze that authorized commit. The - older Yggdrasil targets remain marked `legacy-unmigrated` until Bifrost - authorities are issued for them, so installing the Epiphany gate does not - silently disable their existing lifecycle paths. Git branch position alone - never satisfies the Epiphany gate. -- For Epiphany, Bifrost's one current authorized record for - `GameCult/Epiphany` and `refs/heads/main` selects the release commit. The - current remote branch head is observed separately and may advance without - changing an already authorized deployment. Zero or multiple current - authorities fail closed. Idunn freezes the selected SHA and receipt digest - into the deployment request, then the root Yggdrasil actuator invokes - Idunn's narrow `validate-release-authority` posture against the live Bifrost - store immediately before the service manifest executes. Environment values - carry lineage across sudo; they do not grant it. +- Release authority is explicit per target. Source-observed targets let Idunn + derive and freeze the desired commit from their admitted upstream ref. + Targets that still require a Bifrost receipt retain that separate gate; their + receipt fields cannot leak into a source-observed request. +- For Epiphany, Idunn observes the explicitly admitted + `GameCult/Epiphany` `refs/heads/codex/epiphany-shakedown-live` ref. It derives + the desired release revision from the newest commit on that ref that changes + build or runtime source, so documentation and operator-ledger commits do not + impersonate a new binary generation. Idunn freezes that exact revision into + its typed deployment request and carries it across the narrow root actuator. + Bifrost does not authorize Epiphany releases. The admitted development ref is + a temporary shakedown boundary; changing it or promoting it to `main` is a + separate source-governance act. The current typed records are: diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index a6befa3..3644560 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -19,8 +19,8 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in - epiphany|bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false ;; + bifrost-persona-feedback) target_requires_bifrost_authority=true ;; + epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -52,8 +52,8 @@ require_release_lineage() { --release-authority-sha256 "$release_authority_sha256" ;; false) - [ -z "$release_authority_id" ] || { echo "legacy release cannot claim Bifrost authority id" >&2; exit 1; } - [ -z "$release_authority_sha256" ] || { echo "legacy release cannot claim Bifrost authority digest" >&2; exit 1; } + [ -z "$release_authority_id" ] || { echo "source-observed release cannot claim Bifrost authority id" >&2; exit 1; } + [ -z "$release_authority_sha256" ] || { echo "source-observed release cannot claim Bifrost authority digest" >&2; exit 1; } ;; *) echo "invalid Bifrost authority requirement mode" >&2; exit 1 ;; esac From 78e299a9802452ed8c5496a6bd5a822d7cf5801c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 20:54:19 +0200 Subject: [PATCH 40/93] Admit Ghostlight to Idunn continuity --- crates/idunn-daemon/src/main.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 21d2054..f7d093e 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5030,10 +5030,9 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { ), PathBuf::from("/srv/ghostlight/deploy/deployment.env"), )), - // Ghostlight becomes a continuity target only after its Linux - // body and signed-health identity have been admitted. A - // missing service is not an unhealthy installed service. - enabled: false, + // The admitted native body now publishes authenticated health; + // Idunn owns its continuity and later release deployment. + enabled: true, interval_seconds: 30, }, DaemonTarget { @@ -7972,7 +7971,7 @@ mod tests { Some(PathBuf::from(witness)) ); assert!(!release.requires_bifrost_authority); - assert_eq!(target.enabled, daemon_id == "yggdrasil-odin"); + assert!(target.enabled); } let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); From fbd54f82fbb1d661f9c1fcbd396d76de713a0984 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 21:15:58 +0200 Subject: [PATCH 41/93] Select deployable source revisions --- crates/idunn-daemon/src/main.rs | 125 +++++++++++++++++++++++++------- 1 file changed, 98 insertions(+), 27 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index f7d093e..bd45ddb 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -61,6 +61,15 @@ const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; const SIGNED_DAEMON_HEALTH_SCHEMA_ID: &str = odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA; +const DEPLOYABLE_SOURCE_PATHS: &[&str] = &[ + ".", + ":(exclude)docs/**", + ":(exclude)notes/**", + ":(exclude)state/ledgers.msgpack", + ":(exclude)state/map.yaml", + ":(exclude)state/scratch*", + ":(exclude)*.md", +]; struct TargetActuationGate { lock: Mutex<()>, @@ -4995,16 +5004,19 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { ), deploy_command: Some(yggdrasil_actuator("deploy", "odin")), restart_command: Some(yggdrasil_actuator("restart", "odin")), - release: Some(with_deployed_revision_witness( - release_target_on_branch( - "Odin", - PathBuf::from("/srv/build/Odin"), - "codex/ygg-idunn-independent-bootstrap", - "restart-after-verified-build", - None, - "restart-required", + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target_on_branch( + "Odin", + PathBuf::from("/srv/build/Odin"), + "codex/ygg-idunn-independent-bootstrap", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/odin/deploy/deployment.env"), ), - PathBuf::from("/srv/odin/deploy/deployment.env"), + DEPLOYABLE_SOURCE_PATHS, )), enabled: true, interval_seconds: 30, @@ -5019,16 +5031,19 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { ), deploy_command: Some(yggdrasil_actuator("deploy", "ghostlight")), restart_command: Some(yggdrasil_actuator("restart", "ghostlight")), - release: Some(with_deployed_revision_witness( - release_target_on_branch( - "Ghostlight", - PathBuf::from("/srv/build/Ghostlight"), - "codex/ghostlight-dungeon-mvp", - "restart-after-verified-build", - None, - "restart-required", + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target_on_branch( + "Ghostlight", + PathBuf::from("/srv/build/Ghostlight"), + "codex/ghostlight-dungeon-mvp", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/ghostlight/deploy/deployment.env"), ), - PathBuf::from("/srv/ghostlight/deploy/deployment.env"), + DEPLOYABLE_SOURCE_PATHS, )), // The admitted native body now publishes authenticated health; // Idunn owns its continuity and later release deployment. @@ -5091,15 +5106,7 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { ), PathBuf::from("/srv/epiphany/deploy/deployment.env"), ), - &[ - ".", - ":(exclude)docs/**", - ":(exclude)notes/**", - ":(exclude)state/ledgers.msgpack", - ":(exclude)state/map.yaml", - ":(exclude)state/scratch*", - ":(exclude)*.md", - ], + DEPLOYABLE_SOURCE_PATHS, )), enabled: true, interval_seconds: 300, @@ -6559,6 +6566,69 @@ mod tests { ); } + #[test] + fn deployable_source_authority_ignores_documentation_only_commits() { + let root = tempfile::tempdir().unwrap(); + let repo = root.path().to_path_buf(); + let git = |args: &[&str]| { + let status = Command::new("git") + .arg("-C") + .arg(&repo) + .args(args) + .status() + .unwrap(); + assert!(status.success(), "git command failed: {args:?}"); + }; + + git(&["init", "-b", "main"]); + git(&["config", "user.email", "idunn-test@gamecult.org"]); + git(&["config", "user.name", "Idunn Test"]); + fs::create_dir_all(repo.join("crates/runtime")).unwrap(); + fs::write(repo.join("crates/runtime/lib.rs"), "pub fn live() {}\n").unwrap(); + git(&["add", "."]); + git(&["commit", "-m", "runtime"]); + let runtime_commit = git_revision(&repo, "HEAD").unwrap(); + + fs::create_dir_all(repo.join("docs")).unwrap(); + fs::write(repo.join("docs/status.md"), "runtime is live\n").unwrap(); + git(&["add", "."]); + git(&["commit", "-m", "docs"]); + let documentation_commit = git_revision(&repo, "HEAD").unwrap(); + assert_ne!(documentation_commit, runtime_commit); + git(&[ + "update-ref", + "refs/remotes/origin/main", + &documentation_commit, + ]); + + let release = with_source_change_pathspecs( + release_target("Test", repo.clone(), "restart", None, "restart"), + DEPLOYABLE_SOURCE_PATHS, + ); + assert_eq!( + SystemReleaseStatePort.desired_revision(&release).unwrap(), + runtime_commit + ); + + fs::write( + repo.join("crates/runtime/lib.rs"), + "pub fn live() { println!(\"awake\"); }\n", + ) + .unwrap(); + git(&["add", "."]); + git(&["commit", "-m", "runtime change"]); + let changed_runtime_commit = git_revision(&repo, "HEAD").unwrap(); + git(&[ + "update-ref", + "refs/remotes/origin/main", + &changed_runtime_commit, + ]); + assert_eq!( + SystemReleaseStatePort.desired_revision(&release).unwrap(), + changed_runtime_commit + ); + } + #[test] fn target_actuation_gate_serializes_request_owners() { let gate = Arc::new(TargetActuationGate::new()); @@ -7971,6 +8041,7 @@ mod tests { Some(PathBuf::from(witness)) ); assert!(!release.requires_bifrost_authority); + assert_eq!(release.source_change_pathspecs, DEPLOYABLE_SOURCE_PATHS); assert!(target.enabled); } From 209bb6e166a61ded92ea4e1938823bca51a0c2a0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 21:28:54 +0200 Subject: [PATCH 42/93] Document source-selected release authority --- docs/idunn.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/docs/idunn.md b/docs/idunn.md index 0b77a15..92a45a1 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -99,18 +99,20 @@ Idunn now shares Odin's Rust body: executable manifest exists for that service under `/srv/odin/deploy-manifests`. It does not inherit Starfire paths, SSH keys, ADB authority, or Windows process ownership. -- Release authority is explicit per target. Source-observed targets let Idunn - derive and freeze the desired commit from their admitted upstream ref. - Targets that still require a Bifrost receipt retain that separate gate; their - receipt fields cannot leak into a source-observed request. -- For Epiphany, Idunn observes the explicitly admitted - `GameCult/Epiphany` `refs/heads/codex/epiphany-shakedown-live` ref. It derives - the desired release revision from the newest commit on that ref that changes - build or runtime source, so documentation and operator-ledger commits do not - impersonate a new binary generation. Idunn freezes that exact revision into - its typed deployment request and carries it across the narrow root actuator. - Bifrost does not authorize Epiphany releases. The admitted development ref is - a temporary shakedown boundary; changing it or promoting it to `main` is a +- Release authority is explicit per target. Odin, Ghostlight, and Epiphany are + source-observed targets: Idunn derives the newest commit on each admitted ref + that changes build or runtime source, freezes that exact revision into its + typed deployment request, and carries it across the narrow root actuator. + Documentation, notes, state maps, ledgers, scratch state, and root Markdown + do not impersonate a new binary generation. The actuator independently proves + that the frozen commit belongs to the admitted ref, then verifies the exact + embedded commit and deployment witness after activation. Raw branch `HEAD` is + neither release authority nor a second opinion about the selected revision. +- Targets that still require a Bifrost receipt retain that separate gate; their + receipt fields cannot leak into a source-observed request. Bifrost does not + authorize Epiphany releases. Epiphany's admitted + `GameCult/Epiphany` `refs/heads/codex/epiphany-shakedown-live` ref remains a + temporary shakedown boundary; changing it or promoting it to `main` is a separate source-governance act. The current typed records are: From 745e01093c59882ed098b7515ef8921d55fbed15 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 21 Aug 2026 21:33:01 +0200 Subject: [PATCH 43/93] Preserve millisecond health admission time --- crates/idunn-daemon/src/main.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index bd45ddb..2e4a043 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -6481,11 +6481,14 @@ fn command_output_path(kind: &str) -> Result { } fn timestamp() -> Result { - let seconds = SystemTime::now() - .duration_since(UNIX_EPOCH) - .context("system clock is before Unix epoch")? - .as_secs(); - Ok(format!("unix:{seconds}")) + timestamp_from_unix_millis(unix_epoch_millis()?) +} + +fn timestamp_from_unix_millis(millis: u64) -> Result { + let millis = i64::try_from(millis).context("system clock milliseconds exceeded i64")?; + let timestamp = chrono::DateTime::from_timestamp_millis(millis) + .ok_or_else(|| anyhow!("system clock milliseconds are out of range"))?; + Ok(timestamp.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)) } fn unix_epoch_millis() -> Result { @@ -6566,6 +6569,15 @@ mod tests { ); } + #[test] + fn runtime_timestamp_preserves_the_health_admission_millisecond() { + let timestamp = timestamp_from_unix_millis(42_123).unwrap(); + + assert_eq!(timestamp, "1970-01-01T00:00:42.123Z"); + assert_eq!(parse_timestamp_millis(×tamp), Some(42_123)); + assert_eq!(parse_admission_ceiling_millis(×tamp), Some(42_123)); + } + #[test] fn deployable_source_authority_ignores_documentation_only_commits() { let root = tempfile::tempdir().unwrap(); From 3ec90d59f6dd56d72eacf73eca7cb1d05582103b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 02:17:55 +0200 Subject: [PATCH 44/93] Witness Heimdall source-selected releases --- crates/idunn-daemon/src/main.rs | 39 +++++++++++++++++++++++++-------- docs/idunn.md | 14 ++++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 2e4a043..029ec96 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5077,12 +5077,18 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { health_contract: health_contract("heimdall.cultnet-rudp-provider-health", "failed"), deploy_command: Some(yggdrasil_actuator("deploy", "heimdall")), restart_command: Some(yggdrasil_actuator("restart", "heimdall")), - release: Some(release_target( - "Heimdall", - PathBuf::from("/srv/build/Heimdall"), - "restart-after-verified-build", - None, - "restart-required", + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target( + "Heimdall", + PathBuf::from("/srv/build/Heimdall"), + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/heimdall/deploy/deployment.env"), + ), + DEPLOYABLE_SOURCE_PATHS, )), enabled: true, interval_seconds: 300, @@ -8010,20 +8016,21 @@ mod tests { } #[test] - fn yggdrasil_runtime_catalog_includes_discovery_and_dungeon_bodies() { + fn yggdrasil_runtime_catalog_binds_discovery_auth_and_dungeon_releases() { let targets = swarm_targets(&SwarmOptions { profile: "yggdrasil-local".to_string(), repo_root: PathBuf::from("/srv/odin/source"), }) .expect("yggdrasil-local targets"); - for (daemon_id, repo, branch, witness, actuator_name) in [ + for (daemon_id, repo, branch, witness, actuator_name, restart_on_missing) in [ ( "yggdrasil-odin", "Odin", "codex/ygg-idunn-independent-bootstrap", "/srv/odin/deploy/deployment.env", "odin", + true, ), ( "yggdrasil-ghostlight", @@ -8031,13 +8038,25 @@ mod tests { "codex/ghostlight-dungeon-mvp", "/srv/ghostlight/deploy/deployment.env", "ghostlight", + true, + ), + ( + "yggdrasil-heimdall", + "Heimdall", + "main", + "/srv/heimdall/deploy/deployment.env", + "heimdall", + false, ), ] { let target = targets .iter() .find(|target| target.daemon_id == daemon_id) .unwrap_or_else(|| panic!("missing {daemon_id}")); - assert!(target.health_contract.restart_on_missing_publication); + assert_eq!( + target.health_contract.restart_on_missing_publication, + restart_on_missing + ); assert_eq!( target.restart_command.as_deref(), Some(format!( @@ -8060,6 +8079,8 @@ mod tests { let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); assert!(actuator.contains("deploy:odin|restart:odin")); assert!(actuator.contains("deploy:ghostlight|restart:ghostlight")); + assert!(actuator.contains("restart:heimdall")); + assert!(actuator.contains("deploy:heimdall")); } struct FakeReleaseStatePort { diff --git a/docs/idunn.md b/docs/idunn.md index 92a45a1..7d94df8 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -321,13 +321,19 @@ idunn.rudp_health_ingress.v1 lacks hot reload, blue/green routing, rolling instances, or another named in-place swap mechanism, Idunn records `restart-required` and verifies the restart path honestly. -- Yggdrasil Odin, Heimdall, repixelizer, and StreamPixels are enforced through the - generic source artifact lane and their existing `gamecult-ops` runbooks. +- Yggdrasil Odin, Heimdall, Ghostlight, repixelizer, and StreamPixels are + enforced through Idunn's source-selected release lane and their existing + `gamecult-ops` actuators. Heimdall and Ghostlight publish exact deployed + revisions in `/srv/heimdall/deploy/deployment.env` and + `/srv/ghostlight/deploy/deployment.env`; documentation-only commits cannot + become desired executable generations. Odin publishes `idunn.signed_daemon_health.v1` with its root-bound Ed25519 identity. Its immutable release pins both the Odin commit and the exact CultLib commit, then switches them through one `/srv/odin/current` link. - Ghostlight is present but disabled in the catalog until its Linux runtime and - signed-health identity are admitted; absence is not treated as failed health. + Ghostlight's Linux runtime and signed-health identity are admitted. Heimdall + publishes its own daemon health and private authentication command listener; + its HTTP and loopback listener probes remain deployment witnesses rather than + release or health authority. Bifrost is explicitly blocked as of 2026-06-09: committed Bifrost `HEAD` expects `UserAccounts.HeimdallAccountId`, while Yggdrasil's production database lacks that column and EF reports no pending migration. Idunn must From d418523d05ae29370daf007f0c4310fc6c88f361 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 02:37:57 +0200 Subject: [PATCH 45/93] Rotate one provider health trust signer --- crates/idunn-daemon/src/provisioning.rs | 126 +++++++++++++++++++++++- docs/idunn.md | 6 ++ 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 7638009..922d4d9 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -76,6 +76,7 @@ pub fn run(args: impl IntoIterator) -> Result<()> { "deployment-brake-status" => deployment_brake_status(&options)?, "create-daemon-health-trust-binding" => create_health_binding(&options)?, "add-daemon-health-trust-binding" => add_health_binding(&options)?, + "rotate-daemon-health-trust-signer" => rotate_health_binding_signer(&options)?, "validate-daemon-health-trust-binding" => { require_only(&options, &["input"])?; validate_health_binding_store(&path(&options, "input")?)?; @@ -430,6 +431,62 @@ fn add_health_binding(options: &BTreeMap) -> Result<()> { Ok(()) } +fn rotate_health_binding_signer(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "output", + "binding-id", + "signer-public-key-hex", + "bound-at-unix-millis", + ], + )?; + let output = path(options, "output")?; + validate_health_binding_store(&output)?; + let store = SingleFileMessagePackBackingStore::new(&output); + let existing = store.pull_all_read_only_snapshot()?; + let binding_id = required(options, "binding-id")?; + let public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; + let bound_at = parse_u64(options, "bound-at-unix-millis")?; + let mut expected = Vec::with_capacity(existing.len()); + let mut replacement = None; + for envelope in existing { + let binding: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&envelope.payload)?; + if binding.binding_id == binding_id { + if bound_at <= binding.bound_at_unix_millis { + bail!("rotated signer binding time must advance"); + } + if binding.signer_public_key == public_key { + bail!("rotated signer must differ from the current signer"); + } + let mut rotated = binding.clone(); + rotated.signer_identity_id = + derive_service_identity_id::(&public_key)?; + rotated.signer_public_key = public_key.clone(); + rotated.bound_at_unix_millis = bound_at; + rotated.validate()?; + replacement = Some(rotated); + } + expected.push(CultCacheExpectedEnvelope { + r#type: envelope.r#type.clone(), + key: envelope.key.clone(), + current: Some(envelope), + }); + } + let replacement = replacement.ok_or_else(|| anyhow!("binding id does not exist"))?; + let envelope = typed_envelope( + &replacement.binding_id, + &replacement, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + bound_at, + )?; + if !store.compare_exchange(&expected, &[envelope])? { + bail!("trust store changed during validated signer rotation"); + } + validate_health_binding_store(&output) +} + fn typed_envelope( key: &str, value: &T, @@ -703,7 +760,7 @@ fn normalized(path: &Path) -> Result { } fn usage() -> &'static str { - "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " + "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " } #[cfg(test)] @@ -1136,6 +1193,73 @@ mod tests { assert!(invoke(&partial).is_err()); } + #[test] + fn signer_rotation_changes_only_the_named_trust_binding_signer() -> Result<()> { + let temp = TempDir::new()?; + let output = temp.path().join("binding.cc"); + invoke(&[ + "create-daemon-health-trust-binding", + "--output", + output.to_str().unwrap(), + "--binding-id", + "heimdall-yggdrasil-provider-health", + "--daemon", + "yggdrasil-heimdall", + "--health-contract", + "heimdall.cultnet-rudp-provider-health", + "--source-runtime", + "heimdall-service", + "--signer-public-key-hex", + "0707070707070707070707070707070707070707070707070707070707070707", + "--bound-at-unix-millis", + "100", + "--release-binding-required", + "false", + ])?; + assert!( + invoke(&[ + "rotate-daemon-health-trust-signer", + "--output", + output.to_str().unwrap(), + "--binding-id", + "heimdall-yggdrasil-provider-health", + "--signer-public-key-hex", + "0808080808080808080808080808080808080808080808080808080808080808", + "--bound-at-unix-millis", + "100", + ]) + .is_err() + ); + invoke(&[ + "rotate-daemon-health-trust-signer", + "--output", + output.to_str().unwrap(), + "--binding-id", + "heimdall-yggdrasil-provider-health", + "--signer-public-key-hex", + "0808080808080808080808080808080808080808080808080808080808080808", + "--bound-at-unix-millis", + "101", + ])?; + let [envelope] = SingleFileMessagePackBackingStore::new(&output) + .pull_all_read_only_snapshot()? + .try_into() + .map_err(|_| anyhow!("expected one trust binding"))?; + let binding: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&envelope.payload)?; + assert_eq!(binding.binding_id, "heimdall-yggdrasil-provider-health"); + assert_eq!(binding.daemon_id, "yggdrasil-heimdall"); + assert_eq!( + binding.health_contract, + "heimdall.cultnet-rudp-provider-health" + ); + assert_eq!(binding.source_runtime_id, "heimdall-service"); + assert_eq!(binding.signer_public_key, vec![8; 32]); + assert_eq!(binding.bound_at_unix_millis, 101); + assert!(!binding.release_binding_required); + validate_health_binding_store(&output) + } + #[test] fn brake_operator_can_issue_one_exact_bounded_rollout_grant() -> Result<()> { let temp = TempDir::new()?; diff --git a/docs/idunn.md b/docs/idunn.md index 7d94df8..106644f 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -309,6 +309,12 @@ idunn.rudp_health_ingress.v1 `IDUNN_COMMAND_AUTHORITY=idunn-daemon`. Agent work is to update the target catalog, release target, command boundary, migration plan, and daemon publication surfaces so Idunn can run the shared command primitive. +- Provider-health signer rotation is a root authority transaction over one + existing named trust binding. `idunn-provision + rotate-daemon-health-trust-signer` changes only that binding's public signer, + derived identity, and advancing bind time through a validated CultCache CAS; + daemon, contract, runtime, release policy, and every other binding remain + unchanged. - Manual lifecycle requests are typed Idunn command records, not direct script execution. Operators publish `idunn.lifecycle_command.v1` with `idunn restart --daemon ` or `idunn redeploy --daemon `. The running supervisor From 472d50c325c8b10082314a4d2d1f5f452e519854 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 02:56:36 +0200 Subject: [PATCH 46/93] Admit root-authorized health signer rotation --- crates/idunn-daemon/src/main.rs | 65 ++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 029ec96..fd21a06 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -3659,13 +3659,18 @@ fn generic_signed_health_advances( existing: &IdunnAuthenticatedDaemonHealthAdmissionRecord, candidate: &IdunnAuthenticatedDaemonHealthAdmissionRecord, ) -> bool { - if existing.trust_binding_id != candidate.trust_binding_id - || existing.trust_binding_sha256 != candidate.trust_binding_sha256 - || existing.signer_identity_id != candidate.signer_identity_id - || existing.daemon_id != candidate.daemon_id - { + if existing.daemon_id != candidate.daemon_id { return false; } + let same_trust_binding = existing.trust_binding_id == candidate.trust_binding_id + && existing.trust_binding_sha256 == candidate.trust_binding_sha256 + && existing.signer_identity_id == candidate.signer_identity_id; + if !same_trust_binding { + // `candidate` has already been verified against the one exact binding + // in the current root-owned trust store. The previous admission is + // replay memory, not authority over a legitimate signer rotation. + return candidate.observed_at_unix_millis > existing.observed_at_unix_millis; + } if existing.publisher_incarnation_id == candidate.publisher_incarnation_id { candidate.publisher_sequence > existing.publisher_sequence && candidate.observed_at_unix_millis >= existing.observed_at_unix_millis @@ -9405,6 +9410,56 @@ mod tests { assert!(authenticated.is_some()); } + #[test] + fn current_root_authorized_signer_rotation_supersedes_stale_admission() { + let existing = IdunnAuthenticatedDaemonHealthAdmissionRecord { + schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), + daemon_id: "test-daemon".into(), + health_contract: "test.signed-health".into(), + source_runtime_id: "test-runtime".into(), + state: "active".into(), + observed_at_unix_millis: 100, + admitted_at_unix_millis: 101, + trust_binding_id: "root/test-daemon/health".into(), + trust_binding_sha256: format!("sha256-{}", "a".repeat(64)), + signer_identity_id: "old-signer".into(), + publisher_incarnation_id: "old-incarnation".into(), + publisher_sequence: 49, + signed_health_sha256: format!("sha256-{}", "b".repeat(64)), + release_id: None, + release_witness_sha256: None, + source_commit: None, + deployment_id: None, + private_state_exposed: false, + }; + + let mut same_incarnation = existing.clone(); + same_incarnation.publisher_sequence += 1; + assert!(generic_signed_health_advances(&existing, &same_incarnation)); + same_incarnation.publisher_sequence = existing.publisher_sequence; + assert!(!generic_signed_health_advances( + &existing, + &same_incarnation + )); + + let mut rotated = existing.clone(); + rotated.trust_binding_sha256 = format!("sha256-{}", "c".repeat(64)); + rotated.signer_identity_id = "current-root-signer".into(); + rotated.publisher_incarnation_id = "new-incarnation".into(); + rotated.publisher_sequence = 1; + rotated.observed_at_unix_millis = 102; + rotated.admitted_at_unix_millis = 103; + assert!(generic_signed_health_advances(&existing, &rotated)); + + rotated.observed_at_unix_millis = existing.observed_at_unix_millis; + assert!(!generic_signed_health_advances(&existing, &rotated)); + rotated.observed_at_unix_millis = existing.observed_at_unix_millis - 1; + assert!(!generic_signed_health_advances(&existing, &rotated)); + rotated.observed_at_unix_millis = 102; + rotated.daemon_id = "other-daemon".into(); + assert!(!generic_signed_health_advances(&existing, &rotated)); + } + fn projection_fixture( release_bound: bool, ) -> ( From 2a5cb3e08f5f5f40a12f825a9522b31e6af941af Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 03:00:49 +0200 Subject: [PATCH 47/93] Authorize Ghostlight deployment surfaces --- crates/idunn-daemon/src/main.rs | 3 +++ scripts/linux/idunn-yggdrasil.service | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index fd21a06..36987af 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -7985,6 +7985,9 @@ mod tests { )); assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); assert!(!unit.contains("ReadWritePaths=/var/lib/gamecult/idunn-authority")); + assert!(unit.contains("/etc/gamecult/ghostlight")); + assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); + assert!(!unit.contains("ReadWritePaths=/etc ")); assert!( !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") ); diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index 6490b10..841aecc 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -19,7 +19,7 @@ UMask=027 PrivateTmp=yes ProtectHome=yes ProtectSystem=full -ReadWritePaths=/etc/gamecult/idunn /var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority ReadOnlyPaths=/var/lib/gamecult/idunn-authority From b4f9a2e95f0b41cebdeddc49223781d1d3c7b42a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 03:53:10 +0200 Subject: [PATCH 48/93] Register Heimdall access discovery documents --- src/odin/documents.cjs | 33 ++++++++++++++++++++++ test/heimdall-discovery-documents.test.cjs | 23 +++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/heimdall-discovery-documents.test.cjs diff --git a/src/odin/documents.cjs b/src/odin/documents.cjs index 0158054..d8047ab 100644 --- a/src/odin/documents.cjs +++ b/src/odin/documents.cjs @@ -6,6 +6,9 @@ function defineOdinDocuments(defineDocumentType) { if (!defineDocumentType) { return { interfaceBindingDefinition: null, + evePluginAdvertisementDefinition: null, + heimdallCommandBoundaryDefinition: null, + heimdallTransportProfileDefinition: null, idunnCommandBoundaryDefinition: null, idunnDaemonHealthDefinition: null, idunnTransportProfileDefinition: null, @@ -83,6 +86,33 @@ function defineOdinDocuments(defineDocumentType) { name: (value) => value?.providerId || value?.provider?.id || "provider", schema: parseObjectDocument("Eve provider advertisement"), }); + const evePluginAdvertisementDefinition = defineDocumentType({ + type: "gamecult.eve.plugin_advertisement", + schemaName: "gamecult.eve.plugin_advertisement", + schemaId: "gamecult.eve.plugin_advertisement.v1", + schemaVersion: "gamecult.eve.plugin_advertisement.v1", + global: false, + name: (value) => value?.pluginId || value?.plugin?.id || "plugin", + schema: parseObjectDocument("Eve plugin advertisement"), + }); + const heimdallCommandBoundaryDefinition = defineDocumentType({ + type: "heimdall.command_boundary", + schemaName: "heimdall.command_boundary", + schemaId: "heimdall.command_boundary.v1", + schemaVersion: "heimdall.command_boundary.v1", + global: false, + name: (value) => value?.boundaryId || value?.daemonId || "heimdall", + schema: parseObjectDocument("Heimdall command boundary"), + }); + const heimdallTransportProfileDefinition = defineDocumentType({ + type: "heimdall.transport_profile", + schemaName: "heimdall.transport_profile", + schemaId: "heimdall.transport_profile.v1", + schemaVersion: "heimdall.transport_profile.v1", + global: false, + name: (value) => value?.profileId || value?.daemonId || "heimdall", + schema: parseObjectDocument("Heimdall transport profile"), + }); const interfaceLayoutDefinition = defineDocumentType({ type: "odin.interface_layout", schemaName: "odin.interface_layout", @@ -446,6 +476,9 @@ function defineOdinDocuments(defineDocumentType) { return { interfaceBindingDefinition, + evePluginAdvertisementDefinition, + heimdallCommandBoundaryDefinition, + heimdallTransportProfileDefinition, interfaceLayoutDefinition, idunnCommandBoundaryDefinition, idunnDaemonHealthDefinition, diff --git a/test/heimdall-discovery-documents.test.cjs b/test/heimdall-discovery-documents.test.cjs new file mode 100644 index 0000000..f4f6e47 --- /dev/null +++ b/test/heimdall-discovery-documents.test.cjs @@ -0,0 +1,23 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); + +const { defineOdinDocuments } = require("../src/odin/documents.cjs"); + +test("Odin registers the typed Heimdall access discovery documents", () => { + const documents = defineOdinDocuments((definition) => definition); + + assert.equal( + documents.evePluginAdvertisementDefinition.schemaId, + "gamecult.eve.plugin_advertisement.v1", + ); + assert.equal( + documents.heimdallCommandBoundaryDefinition.schemaId, + "heimdall.command_boundary.v1", + ); + assert.equal( + documents.heimdallTransportProfileDefinition.schemaId, + "heimdall.transport_profile.v1", + ); +}); From 0ed956238738c0a5f809c08b080da0e3fa6af70c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 04:08:47 +0200 Subject: [PATCH 49/93] Document Heimdall discovery admission --- README.md | 11 +++++++++++ docs/architecture.md | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 28225c1..3281d7f 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,17 @@ the Nightwing-resident terminal compositor in `E:\Projects\Gjallar` that consumes Odin's accepted `gamecult.eve.surface_state` snapshot over CultNet/RUDP and renders the live display. +## Typed access discovery + +Odin persists only document schemas it has registered. That is a deliberate +typed-store boundary, not a license to accept anonymous blobs and hope later +code understands them. The live Yggdrasil body at +`b4f9a2e95f0b41cebdeddc49223781d1d3c7b42a` registers Heimdall's provider, +command-boundary, Eve-plugin, and transport-profile documents. Heimdall owns +their contents; Odin owns catalog acceptance and exact retrieval. Sensitive +auth commands and claims travel directly between the consuming app and +Heimdall, never through Odin. + ## Gjallar Gjallar is the herald display daemon that runs on Nightwing. Odin sees the diff --git a/docs/architecture.md b/docs/architecture.md index 371dfd6..0b2a43d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -76,6 +76,17 @@ Odin's executable body is split by ownership: - `src/odin/state.cjs`: one refresh's input records into Odin's provider catalog/proxy state. The entrypoint is not allowed to grow new probe, surface, provider, layout, or renderer policy. If a new owner is needed, name the owner and its invariant before adding code. +The accepted document registry is also the persistence admission boundary. +Unknown schemas may be observed for diagnostics but are not silently persisted +as untyped truth. Heimdall access discovery therefore registers +`gamecult.eve.provider_advertisement.v1`, +`heimdall.command_boundary.v1`, +`gamecult.eve.plugin_advertisement.v1`, and +`heimdall.transport_profile.v1`. Their globally unique record keys prevent one +schema from overwriting another in the shared catalog. Odin returns the +redacted route metadata; it does not proxy private auth operations or own any +claim, completion, token, or app secret. + Gjallar is the Nightwing-resident terminal compositor for what Odin can show. Its runtime lives in `E:\Projects\Gjallar` and consumes Odin's accepted provider-state snapshot over CultNet/RUDP. From 8ddf81400bfc0f51309cd37f8f196403fc5dbb79 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 09:45:04 +0200 Subject: [PATCH 50/93] Validate generic signed health admissions --- crates/idunn-daemon/src/main.rs | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 36987af..5411519 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -671,6 +671,70 @@ fn validate_health_admission_at( now: &str, ) -> Result<()> { let lock = Arc::new(Mutex::new(())); + let has_generic_admission = with_store_node(options, &lock, |node| { + Ok(node + .get::(&expected.daemon_id)? + .is_some()) + })?; + if has_generic_admission { + let desired = with_store_node(options, &lock, |node| { + node.get::(&expected.daemon_id)? + .ok_or_else(|| { + anyhow!( + "Idunn has no desired daemon record for {}", + expected.daemon_id + ) + }) + })?; + let managed = read_fresh_daemon_published_health(options, &lock, &desired, now)? + .ok_or_else(|| { + anyhow!( + "Idunn has no fresh authenticated health admission for {}", + expected.daemon_id + ) + })?; + let source = managed.projection_source.ok_or_else(|| { + anyhow!( + "Idunn current health for {} is not the generic authenticated admission", + expected.daemon_id + ) + })?; + let admission = source.admission; + if admission.state != "active" + || admission.deployment_id.as_deref() != Some(expected.deployment_request_id.as_str()) + || admission.release_id.as_deref() != Some(expected.release_id.as_str()) + || admission.release_witness_sha256.as_deref() + != Some(expected.release_witness_sha256.as_str()) + || admission.source_commit.as_deref() != Some(expected.source_commit.as_str()) + || admission.publisher_incarnation_id.is_empty() + || admission.publisher_sequence == 0 + || admission.signed_health_sha256.is_empty() + || admission.signer_identity_id.is_empty() + { + return Err(anyhow!( + "Idunn admitted health does not prove the exact active candidate" + )); + } + println!( + "validated authenticated Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", + admission.daemon_id, + admission.state, + admission.release_id.as_deref().unwrap_or_default(), + admission + .release_witness_sha256 + .as_deref() + .unwrap_or_default(), + admission.source_commit.as_deref().unwrap_or_default(), + admission.signed_health_sha256, + admission.publisher_incarnation_id, + admission.publisher_sequence, + ); + return Ok(()); + } + + // The old Epiphany-specific health contract remains a sealed read path for + // already-admitted releases. New publishers use the generic authenticated + // daemon-health contract above. let admission = with_store_node(options, &lock, |node| { let admission = node .get::(&expected.daemon_id)? @@ -9362,6 +9426,51 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + #[test] + fn deployment_validator_accepts_exact_generic_authenticated_health() { + let (message, options, _root) = generic_signed_health_fixture_with_release(1, true); + let lock = Arc::new(Mutex::new(())); + with_store_node(&options, &lock, |node| { + node.put( + "test-daemon", + &IdunnDesiredDaemonRecord { + daemon_id: "test-daemon".into(), + verse_id: "test.local".into(), + name: "Test daemon".into(), + enabled: true, + health_command: None, + restart_command: None, + deploy_command: Some("deploy test".into()), + authority: "idunn-supervisor-command".into(), + max_silence_seconds: 60, + observed_at: "2026-07-19T12:00:00Z".into(), + health_contract: "test.signed-health".into(), + transport_profile_id: "test-transport".into(), + command_boundary_id: "test-command".into(), + }, + )?; + Ok(()) + }) + .unwrap(); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + + let exact = HealthAdmissionValidationOptions { + daemon_id: "test-daemon".into(), + deployment_request_id: "deploy-test-1".into(), + release_id: "release-test-1".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + }; + validate_health_admission_at(&exact, &options, "2026-07-19T12:00:02Z").unwrap(); + + let mut substituted = exact; + substituted.release_witness_sha256 = format!("sha256-{}", "f".repeat(64)); + let error = validate_health_admission_at(&substituted, &options, "2026-07-19T12:00:02Z") + .unwrap_err() + .to_string(); + assert!(error.contains("does not prove the exact active candidate")); + } + #[test] fn unavailable_release_source_cannot_mask_authenticated_daemon_health() { let (message, options, root) = generic_signed_health_fixture(1); From 93d17187ba296000847c7c73173d8f0c4038f8e4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 13:00:07 +0200 Subject: [PATCH 51/93] Authorize Epiphany unit promotion --- crates/idunn-daemon/src/main.rs | 1 + scripts/linux/idunn-yggdrasil.service | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 5411519..ac9f9a3 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -8050,6 +8050,7 @@ mod tests { assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); assert!(!unit.contains("ReadWritePaths=/var/lib/gamecult/idunn-authority")); assert!(unit.contains("/etc/gamecult/ghostlight")); + assert!(unit.contains("/etc/systemd/system/epiphany-swarm.service")); assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); assert!(!unit.contains("ReadWritePaths=/etc ")); assert!( diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index 841aecc..fc255aa 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -19,7 +19,7 @@ UMask=027 PrivateTmp=yes ProtectHome=yes ProtectSystem=full -ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority ReadOnlyPaths=/var/lib/gamecult/idunn-authority From 11b7669c7509bcf31f3853e9cf49a779a16a34f3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 15:57:02 +0200 Subject: [PATCH 52/93] Let Idunn lock deployment brake snapshots --- crates/idunn-daemon/src/main.rs | 11 ++++++++++- scripts/linux/idunn-yggdrasil.service | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index ac9f9a3..da3ef16 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -8048,7 +8048,16 @@ mod tests { "--deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc" )); assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); - assert!(!unit.contains("ReadWritePaths=/var/lib/gamecult/idunn-authority")); + assert!( + unit.contains( + "ReadWritePaths=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock" + ) + ); + assert!( + !unit + .lines() + .any(|line| line == "ReadWritePaths=/var/lib/gamecult/idunn-authority") + ); assert!(unit.contains("/etc/gamecult/ghostlight")); assert!(unit.contains("/etc/systemd/system/epiphany-swarm.service")); assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index fc255aa..c680cfc 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -21,7 +21,7 @@ ProtectHome=yes ProtectSystem=full ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority -ReadOnlyPaths=/var/lib/gamecult/idunn-authority +ReadWritePaths=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock [Install] WantedBy=multi-user.target From 8b972715c47731f2418d0c423cb0dd2076940bd7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 22 Aug 2026 16:09:20 +0200 Subject: [PATCH 53/93] Expose Idunn deployment brake denial causes --- crates/idunn-daemon/src/main.rs | 7 +------ scripts/linux/idunn-yggdrasil.service | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index da3ef16..3c4ee62 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -6027,7 +6027,7 @@ fn run_deployment( request_id: request.request_id.clone(), daemon_id: request.daemon_id.clone(), state: "failed".to_string(), - detail: format!("deployment command could not run: {error}"), + detail: format!("deployment command could not run: {error:#}"), completed_at: requested_at.to_string(), }, } @@ -8048,11 +8048,6 @@ mod tests { "--deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc" )); assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); - assert!( - unit.contains( - "ReadWritePaths=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock" - ) - ); assert!( !unit .lines() diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index c680cfc..a57c118 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -21,7 +21,6 @@ ProtectHome=yes ProtectSystem=full ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority -ReadWritePaths=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock [Install] WantedBy=multi-user.target From f875a6e79530f4ec926cd12659c3df425b7435d4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 00:40:52 +0200 Subject: [PATCH 54/93] Bound Idunn authenticated health history --- crates/idunn-daemon/src/main.rs | 218 ++++++++++++++++++- docs/idunn-signed-daemon-health-authority.md | 17 +- docs/idunn.md | 6 + vendor/cultcache-rs/src/lib.rs | 71 ++++++ 4 files changed, 302 insertions(+), 10 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 3c4ee62..0efbab9 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -37,7 +37,7 @@ use odin_core::{ authenticated_provider_health_reason_code, plan_keepalive, }; use sha2::{Digest, Sha256}; -use std::collections::{BTreeSet, HashMap}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::env; use std::fs::{self, File}; use std::net::{SocketAddr, UdpSocket}; @@ -835,6 +835,13 @@ fn run_swarm( let store_lock = Arc::new(Mutex::new(())); let now = timestamp()?; + let compacted_health_statements = + compact_authenticated_health_statement_history(common, &store_lock)?; + if compacted_health_statements > 0 { + println!( + "Idunn compacted {compacted_health_statements} obsolete authenticated health statement(s)." + ); + } let recovered = terminalize_interrupted_deployment_requests(common, &store_lock, &now)?; if recovered > 0 { println!( @@ -1974,7 +1981,7 @@ fn read_fresh_daemon_published_health( }; admission.validate()?; let Some(statement) = - node.get::(&admission.signed_health_sha256)? + node.get::(&admission.daemon_id)? else { return Ok(None); }; @@ -3495,6 +3502,81 @@ fn typed_envelope( }) } +fn compact_authenticated_health_statement_history( + options: &CommonOptions, + store_lock: &Arc>, +) -> Result { + let _store_guard = store_lock + .lock() + .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; + let backing = SingleFileMessagePackBackingStore::new(&options.store_path); + for _ in 0..8 { + let expected = backing.pull_all()?; + let admissions = expected + .iter() + .filter(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) + .map(|entry| { + let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = + rmp_serde::from_slice(&entry.payload) + .context("decoding current authenticated health admission")?; + admission.validate()?; + Ok((admission.daemon_id.clone(), admission)) + }) + .collect::>>()?; + let statements = expected + .iter() + .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) + .map(|entry| (entry.key.clone(), entry)) + .collect::>(); + let mut current_statements = Vec::with_capacity(admissions.len()); + for (daemon_id, admission) in &admissions { + let entry = statements + .get(daemon_id) + .or_else(|| statements.get(&admission.signed_health_sha256)) + .ok_or_else(|| { + anyhow!( + "authenticated health admission for {daemon_id} lost its signed statement" + ) + })?; + let statement: IdunnSignedDaemonHealthRecord = rmp_serde::from_slice(&entry.payload) + .context("decoding current signed daemon health")?; + statement.validate()?; + let digest = format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(&statement)?) + ); + if statement.daemon_id != *daemon_id || digest != admission.signed_health_sha256 { + return Err(anyhow!( + "authenticated health admission for {daemon_id} does not bind its signed statement" + )); + } + let mut current = (*entry).clone(); + current.key.clone_from(daemon_id); + current_statements.push(current); + } + let statement_count = statements.len(); + let already_compact = statement_count == current_statements.len() + && current_statements + .iter() + .all(|entry| statements.contains_key(&entry.key)); + if already_compact { + return Ok(0); + } + let mut replacement = expected + .iter() + .filter(|entry| entry.r#type != IdunnSignedDaemonHealthRecord::TYPE) + .cloned() + .collect::>(); + replacement.extend(current_statements); + if backing.compare_exchange_snapshot(&expected, &replacement)? { + return Ok(statement_count.saturating_sub(admissions.len())); + } + } + Err(anyhow!( + "authenticated health statement compaction lost repeated cross-process contention" + )) +} + fn authenticate_generic_signed_health( document: &cultnet_rs::CultNetRawDocumentRecord, options: &CommonOptions, @@ -3676,9 +3758,8 @@ fn admit_generic_signed_health( .get_envelope::( &admission.daemon_id, )?, - node.cache().get_envelope::( - &admission.signed_health_sha256, - )?, + node.cache() + .get_envelope::(&admission.daemon_id)?, )) })?; let expected = [ @@ -3693,7 +3774,7 @@ fn admit_generic_signed_health( current: expected_admission, }, CultCacheExpectedEnvelope { - key: admission.signed_health_sha256.clone(), + key: admission.daemon_id.clone(), r#type: IdunnSignedDaemonHealthRecord::TYPE.into(), current: expected_statement, }, @@ -3701,7 +3782,7 @@ fn admit_generic_signed_health( let replacements = [ typed_envelope(&health.daemon_id, &health, admitted_at)?, typed_envelope(&admission.daemon_id, &admission, admitted_at)?, - typed_envelope(&admission.signed_health_sha256, &statement, admitted_at)?, + typed_envelope(&admission.daemon_id, &statement, admitted_at)?, ]; if SingleFileMessagePackBackingStore::new(&options.store_path) .compare_exchange(&expected, &replacements)? @@ -9344,6 +9425,35 @@ mod tests { ) } + fn advance_generic_signed_health_fixture( + message: &CultNetMessage, + root: &Path, + sequence: u64, + ) -> CultNetMessage { + let CultNetMessage::DocumentPutRaw { document, .. } = message else { + panic!("signed health fixture must be a raw document"); + }; + let signer = open_service_identity_at::( + &root.join("provider-health-identity.cc"), + ) + .unwrap(); + let mut statement: IdunnSignedDaemonHealthRecord = + rmp_serde::from_slice(&document.payload).unwrap(); + statement.publisher_sequence = sequence; + statement.signature.clear(); + let unsigned = rmp_serde::to_vec(&statement).unwrap(); + statement.signature = signer + .sign::(&unsigned) + .signature; + CultNetMessage::DocumentPutRaw { + message_id: format!("signed-health-{sequence}"), + document: CultNetRawDocumentRecord { + payload: rmp_serde::to_vec(&statement).unwrap(), + ..document.clone() + }, + } + } + #[test] fn generic_signed_health_requires_root_binding_and_monotonic_admission() { let (first, options, _root) = generic_signed_health_fixture(1); @@ -9367,7 +9477,7 @@ mod tests { assert_eq!(admission.publisher_sequence, 1); assert_eq!(admission.trust_binding_id, "root/test-daemon/health"); let statement = node - .get::(&admission.signed_health_sha256)? + .get::(&admission.daemon_id)? .expect("signed statement"); assert_eq!( admission.signed_health_sha256, @@ -9431,6 +9541,98 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + #[test] + fn generic_health_replaces_one_daemon_keyed_statement_instead_of_growing_history() { + let (first, options, root) = generic_signed_health_fixture(1); + let second = advance_generic_signed_health_fixture(&first, root.path(), 2); + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + admit_health_from_rudp_message(&second, &options, &lock, "2026-07-19T12:00:02Z").unwrap(); + + let entries = SingleFileMessagePackBackingStore::new(&options.store_path) + .pull_all_read_only_snapshot() + .unwrap(); + let statements = entries + .iter() + .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) + .collect::>(); + assert_eq!(statements.len(), 1); + assert_eq!(statements[0].key, "test-daemon"); + let admission = entries + .iter() + .find(|entry| { + entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE + && entry.key == "test-daemon" + }) + .map(|entry| { + rmp_serde::from_slice::( + &entry.payload, + ) + .unwrap() + }) + .unwrap(); + assert_eq!(admission.publisher_sequence, 2); + let statement: IdunnSignedDaemonHealthRecord = + rmp_serde::from_slice(&statements[0].payload).unwrap(); + assert_eq!(statement.publisher_sequence, 2); + assert_eq!( + admission.signed_health_sha256, + format!( + "sha256-{:x}", + Sha256::digest(rmp_serde::to_vec(&statement).unwrap()) + ) + ); + } + + #[test] + fn startup_compaction_keeps_only_each_admissions_exact_signed_statement() { + let (message, options, _root) = generic_signed_health_fixture(1); + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + let mut backing = SingleFileMessagePackBackingStore::new(&options.store_path); + let mut entries = backing.pull_all_read_only_snapshot().unwrap(); + let admission = entries + .iter() + .find(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) + .map(|entry| { + rmp_serde::from_slice::( + &entry.payload, + ) + .unwrap() + }) + .unwrap(); + let current_index = entries + .iter() + .position(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) + .unwrap(); + let mut legacy_current = entries.remove(current_index); + legacy_current.key = admission.signed_health_sha256.clone(); + let mut obsolete = legacy_current.clone(); + obsolete.key = format!("sha256-{}", "d".repeat(64)); + entries.push(legacy_current); + entries.push(obsolete); + backing.push_all(&entries, Default::default()).unwrap(); + + assert_eq!( + compact_authenticated_health_statement_history(&options, &lock).unwrap(), + 1 + ); + assert_eq!( + compact_authenticated_health_statement_history(&options, &lock).unwrap(), + 0 + ); + let compacted = backing.pull_all_read_only_snapshot().unwrap(); + let statements = compacted + .iter() + .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) + .collect::>(); + assert_eq!(statements.len(), 1); + assert_eq!(statements[0].key, "test-daemon"); + let statement: IdunnSignedDaemonHealthRecord = + rmp_serde::from_slice(&statements[0].payload).unwrap(); + assert_eq!(statement.publisher_sequence, 1); + } + #[test] fn deployment_validator_accepts_exact_generic_authenticated_health() { let (message, options, _root) = generic_signed_health_fixture_with_release(1, true); diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index c74a277..61aef4b 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -53,13 +53,21 @@ nor the signed liveness statement may rewrite that witness. ## Outputs -Idunn persists two separate facts: +Idunn persists two separate current facts per daemon: -1. the admitted daemon-authored statement and its digest; +1. one admitted daemon-authored statement, keyed by daemon id, plus its exact + digest in the admission; 2. an Idunn-authored `idunn.authenticated_provider_health_projection.v1` record binding the trust record, admission, exact provider lineage, and freshness interval. +Signed heartbeats are current liveness state, not an append-only event ledger. +Advancing a daemon's publisher sequence atomically replaces that daemon's prior +statement, admission, and compatibility health row. Startup atomically compacts +legacy digest-keyed statement history to the one exact statement named by each +current admission. A missing or digest-mismatched witness fails closed; it is +never reconstructed from process probes or a later heartbeat. + The outward record exists only while a generic provider-authored admission is current and still matches its root trust and optional release binding. Missing publication, release drift, dependency failure, target absence, and recovery @@ -205,3 +213,8 @@ Hostile tests must prove: states fail closed; - private detail and filesystem/process internals never enter the operator projection. +- repeated signed heartbeats keep exactly one signed statement per admitted + daemon rather than growing the monolithic keepalive store; +- startup compaction keeps only the exact statement named by each admission, + is idempotent, and loses cleanly to a concurrent whole-store mutation rather + than overwriting it. diff --git a/docs/idunn.md b/docs/idunn.md index 106644f..3acc663 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -158,6 +158,12 @@ idunn.rudp_health_ingress.v1 Odin refresh or manual click, Idunn's ownership path is still incomplete. - Restart attempts must be witnessed: requested by whom, against which service, through which command boundary, with what result and timestamp. +- Authenticated signed health is bounded current state: Idunn stores one exact + statement per daemon, keyed by daemon id, while the admission carries its + digest and monotonic lineage. Heartbeat publication must replace that trio + atomically; it must not append one store row per pulse. Startup compacts the + former digest-keyed history through a whole-snapshot compare/exchange before + scheduler workers start. - Health command exit status is not daemon awareness. Every Idunn target must declare a daemon-owned CultNet/RUDP health contract naming what health publication should prove and what unmarked failure means. diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs index 37c1f3e..72ebf7e 100644 --- a/vendor/cultcache-rs/src/lib.rs +++ b/vendor/cultcache-rs/src/lib.rs @@ -237,6 +237,38 @@ impl SingleFileMessagePackBackingStore { }) } + /// Atomically replaces an entire snapshot only when the persisted entries + /// still exactly match `expected`. This is the bounded migration primitive + /// for removing obsolete identities without racing another writer between + /// a read and a whole-store rewrite. + pub fn compare_exchange_snapshot( + &self, + expected: &[CultCacheEnvelope], + replacements: &[CultCacheEnvelope], + ) -> Result { + self.with_exclusive_lock(|| { + let current = self.read_all_unlocked()?; + if current != expected { + return Ok(false); + } + let mut replacement_ids = BTreeSet::new(); + for replacement in replacements { + if replacement.key.trim().is_empty() || replacement.r#type.trim().is_empty() { + return Err(anyhow!("CultCache snapshot replacement identity is empty")); + } + if !replacement_ids.insert(entry_id(replacement)) { + return Err(anyhow!( + "CultCache snapshot replacement contains a duplicate identity" + )); + } + } + let mut ordered = replacements.to_vec(); + ordered.sort_by_key(entry_id); + self.write_all_unlocked(&ordered)?; + Ok(true) + }) + } + fn read_all_unlocked(&self) -> Result> { if !self.path.exists() { return Ok(Vec::new()); @@ -946,6 +978,45 @@ mod tests { Ok(()) } + #[test] + fn whole_snapshot_replacement_is_atomic_and_rejects_stale_or_duplicate_inputs() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("compact.cc"); + let mut store = SingleFileMessagePackBackingStore::new(&store_path); + let first = CultCacheEnvelope { + key: "first".into(), + r#type: "note".into(), + payload: b"old".to_vec(), + stored_at: "2026-08-23T00:00:00Z".into(), + schema_id: Some("note.v1".into()), + }; + let obsolete = CultCacheEnvelope { + key: "obsolete".into(), + payload: b"discard".to_vec(), + ..first.clone() + }; + store.push(&first)?; + store.push(&obsolete)?; + let expected = store.pull_all_read_only_snapshot()?; + let replacement = CultCacheEnvelope { + payload: b"current".to_vec(), + ..first.clone() + }; + + assert!(store.compare_exchange_snapshot(&expected, std::slice::from_ref(&replacement))?); + assert_eq!(store.pull_all_read_only_snapshot()?, [replacement.clone()]); + assert!(!store.compare_exchange_snapshot(&expected, &[])?); + assert!( + store + .compare_exchange_snapshot( + std::slice::from_ref(&replacement), + &[replacement.clone(), replacement.clone()] + ) + .is_err() + ); + Ok(()) + } + #[test] fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> { let temp = tempfile::tempdir()?; From 28c049726055e0b661bfc1ad9b5ba764600f7c3f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 00:54:06 +0200 Subject: [PATCH 55/93] Bound Idunn operational history --- crates/idunn-daemon/src/main.rs | 350 +++++++++++++++++++ docs/idunn-signed-daemon-health-authority.md | 7 + docs/idunn.md | 8 + 3 files changed, 365 insertions(+) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 0efbab9..d28b794 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -60,6 +60,9 @@ const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; +const IDUNN_OPERATIONAL_STORE_COMPACTION_BYTES: u64 = 1024 * 1024; +const IDUNN_OPERATIONAL_HISTORY_PER_DAEMON: usize = 8; +const IDUNN_LIFECYCLE_HISTORY_PER_DAEMON: usize = 32; const SIGNED_DAEMON_HEALTH_SCHEMA_ID: &str = odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA; const DEPLOYABLE_SOURCE_PATHS: &[&str] = &[ ".", @@ -842,6 +845,12 @@ fn run_swarm( "Idunn compacted {compacted_health_statements} obsolete authenticated health statement(s)." ); } + let compacted_operational_records = compact_operational_history(common, &store_lock)?; + if compacted_operational_records > 0 { + println!( + "Idunn compacted {compacted_operational_records} obsolete operational history record(s)." + ); + } let recovered = terminalize_interrupted_deployment_requests(common, &store_lock, &now)?; if recovered > 0 { println!( @@ -1010,6 +1019,7 @@ fn run_target_cycle( projection_publisher: Option<&IdunnProjectionPublisher>, missing_since: &mut Option, ) -> Result<()> { + compact_operational_history_if_needed(options, store_lock)?; let now = timestamp()?; let desired = IdunnDesiredDaemonRecord { @@ -3384,6 +3394,7 @@ fn admit_health_from_rudp_message( store_lock: &Arc>, admitted_at: &str, ) -> Result { + compact_operational_history_if_needed(options, store_lock)?; let decoded = decode_health_from_rudp_message(message, options, admitted_at)?; let (health, admission) = match decoded { DecodedHealthIngress::Diagnostic(diagnostic) => { @@ -3577,6 +3588,240 @@ fn compact_authenticated_health_statement_history( )) } +fn operational_history_coordinates( + entry: &CultCacheEnvelope, +) -> Result> { + let coordinates = match entry.r#type.as_str() { + kind if kind == odin_core::IdunnKeepaliveDecisionRecord::TYPE => { + let record: odin_core::IdunnKeepaliveDecisionRecord = + rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.decided_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnOperatorAlarmRecord::TYPE => { + let record: IdunnOperatorAlarmRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.raised_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnDeploymentRequestRecord::TYPE => { + let record: IdunnDeploymentRequestRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.requested_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnDeploymentResultRecord::TYPE => { + let record: IdunnDeploymentResultRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.completed_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnRestartRequestRecord::TYPE => { + let record: IdunnRestartRequestRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.requested_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnRestartResultRecord::TYPE => { + let record: IdunnRestartResultRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.completed_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnStateMigrationResultRecord::TYPE => { + let record: IdunnStateMigrationResultRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.completed_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnRolloutResultRecord::TYPE => { + let record: IdunnRolloutResultRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.completed_at, + IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, + ) + } + kind if kind == IdunnLifecycleCommandRecord::TYPE => { + let record: IdunnLifecycleCommandRecord = rmp_serde::from_slice(&entry.payload)?; + ( + record.daemon_id, + record.requested_at, + IDUNN_LIFECYCLE_HISTORY_PER_DAEMON, + ) + } + _ => return Ok(None), + }; + if coordinates.0.trim().is_empty() || coordinates.1.trim().is_empty() { + return Err(anyhow!( + "Idunn operational history record has an empty daemon or timestamp" + )); + } + Ok(Some(coordinates)) +} + +fn compact_operational_history_if_needed( + options: &CommonOptions, + store_lock: &Arc>, +) -> Result<()> { + if fs::metadata(&options.store_path) + .map(|metadata| metadata.len() > IDUNN_OPERATIONAL_STORE_COMPACTION_BYTES) + .unwrap_or(false) + { + let removed = compact_operational_history(options, store_lock)?; + if removed > 0 { + println!("Idunn compacted {removed} obsolete operational history record(s)."); + } + } + Ok(()) +} + +fn compact_operational_history( + options: &CommonOptions, + store_lock: &Arc>, +) -> Result { + let _store_guard = store_lock + .lock() + .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; + let backing = SingleFileMessagePackBackingStore::new(&options.store_path); + for _ in 0..8 { + let expected = backing.pull_all()?; + let mut keep = BTreeSet::<(String, String)>::new(); + let mut groups = BTreeMap::<(String, String, usize), Vec<(String, String)>>::new(); + for entry in &expected { + if let Some((daemon_id, timestamp, limit)) = operational_history_coordinates(entry)? { + groups + .entry((entry.r#type.clone(), daemon_id, limit)) + .or_default() + .push((timestamp, entry.key.clone())); + } else { + keep.insert((entry.r#type.clone(), entry.key.clone())); + } + } + for ((kind, _, limit), candidates) in &mut groups { + candidates.sort_by(|left, right| right.cmp(left)); + for (_, key) in candidates.iter().take(*limit) { + keep.insert((kind.clone(), key.clone())); + } + } + + for entry in expected + .iter() + .filter(|entry| entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE) + { + let head: IdunnCurrentDeploymentRequestRecord = rmp_serde::from_slice(&entry.payload)?; + if head.daemon_id.trim().is_empty() || head.request_id.trim().is_empty() { + return Err(anyhow!( + "current deployment request head is invalid during operational compaction" + )); + } + keep.insert(( + IdunnDeploymentRequestRecord::TYPE.into(), + head.request_id.clone(), + )); + keep.insert(( + IdunnDeploymentResultRecord::TYPE.into(), + format!("result:{}", head.request_id), + )); + } + for entry in expected + .iter() + .filter(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) + { + let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = + rmp_serde::from_slice(&entry.payload)?; + admission.validate()?; + if let Some(deployment_id) = admission.deployment_id { + keep.insert(( + IdunnDeploymentRequestRecord::TYPE.into(), + deployment_id.clone(), + )); + keep.insert(( + IdunnDeploymentResultRecord::TYPE.into(), + format!("result:{deployment_id}"), + )); + } + } + for entry in expected + .iter() + .filter(|entry| entry.r#type == IdunnSignedHealthAdmissionRecord::TYPE) + { + let admission: IdunnSignedHealthAdmissionRecord = + rmp_serde::from_slice(&entry.payload)?; + if admission.deployment_request_id.trim().is_empty() { + return Err(anyhow!( + "signed health admission lost its deployment request during operational compaction" + )); + } + keep.insert(( + IdunnDeploymentRequestRecord::TYPE.into(), + admission.deployment_request_id.clone(), + )); + keep.insert(( + IdunnDeploymentResultRecord::TYPE.into(), + format!("result:{}", admission.deployment_request_id), + )); + } + let retained_commands = expected + .iter() + .filter(|entry| entry.r#type == IdunnLifecycleCommandRecord::TYPE) + .filter(|entry| keep.contains(&(entry.r#type.clone(), entry.key.clone()))) + .cloned() + .collect::>(); + for entry in retained_commands { + let command: IdunnLifecycleCommandRecord = rmp_serde::from_slice(&entry.payload)?; + if command.result_id.trim().is_empty() { + continue; + } + let request_id = command + .result_id + .strip_prefix("result:") + .unwrap_or(&command.result_id) + .to_string(); + keep.insert(( + IdunnDeploymentRequestRecord::TYPE.into(), + request_id.clone(), + )); + keep.insert((IdunnRestartRequestRecord::TYPE.into(), request_id)); + keep.insert(( + IdunnDeploymentResultRecord::TYPE.into(), + command.result_id.clone(), + )); + keep.insert((IdunnRestartResultRecord::TYPE.into(), command.result_id)); + } + + let replacement = expected + .iter() + .filter(|entry| keep.contains(&(entry.r#type.clone(), entry.key.clone()))) + .cloned() + .collect::>(); + if replacement.len() == expected.len() { + return Ok(0); + } + if backing.compare_exchange_snapshot(&expected, &replacement)? { + return Ok(expected.len().saturating_sub(replacement.len())); + } + } + Err(anyhow!( + "operational history compaction lost repeated cross-process contention" + )) +} + fn authenticate_generic_signed_health( document: &cultnet_rs::CultNetRawDocumentRecord, options: &CommonOptions, @@ -9633,6 +9878,111 @@ mod tests { assert_eq!(statement.publisher_sequence, 1); } + #[test] + fn operational_history_compaction_is_bounded_and_keeps_the_current_deployment_chain() { + let (_message, options, _root) = generic_signed_health_fixture(1); + let mut entries = Vec::new(); + for daemon_id in ["alpha", "beta"] { + for index in 0..12 { + let observed_at = format!("2026-08-23T00:00:{index:02}Z"); + let decision = odin_core::IdunnKeepaliveDecisionRecord { + decision_id: format!("decision:{daemon_id}:{index:02}"), + daemon_id: daemon_id.into(), + action: "observe".into(), + reason: "fixture".into(), + authority: "idunn-supervisor-command".into(), + decided_at: observed_at.clone(), + }; + entries + .push(typed_envelope(&decision.decision_id, &decision, &observed_at).unwrap()); + let alarm = IdunnOperatorAlarmRecord { + alarm_id: format!("alarm:{daemon_id}:{index:02}"), + daemon_id: daemon_id.into(), + severity: "operator-action-required".into(), + reason: "fixture".into(), + escalation_target: "bifrost.operator-notification".into(), + raised_at: observed_at.clone(), + }; + entries.push(typed_envelope(&alarm.alarm_id, &alarm, &observed_at).unwrap()); + let request = IdunnDeploymentRequestRecord { + request_id: format!("deploy:{daemon_id}:{index:02}"), + daemon_id: daemon_id.into(), + command: "deploy fixture".into(), + authority: "idunn-supervisor-command".into(), + requested_at: observed_at.clone(), + repository_full_name: "GameCult/Fixture".into(), + upstream_ref: "refs/heads/main".into(), + source_revision: "a".repeat(40), + release_authority_id: "release:fixture".into(), + release_authority_envelope_sha256: format!("sha256-{}", "a".repeat(64)), + requires_bifrost_authority: false, + }; + entries.push(typed_envelope(&request.request_id, &request, &observed_at).unwrap()); + let result = IdunnDeploymentResultRecord { + result_id: format!("result:{}", request.request_id), + request_id: request.request_id, + daemon_id: daemon_id.into(), + state: "failed".into(), + detail: "fixture".into(), + completed_at: observed_at.clone(), + }; + entries.push(typed_envelope(&result.result_id, &result, &observed_at).unwrap()); + } + for index in 0..40 { + let requested_at = format!("2026-08-23T00:01:{index:02}Z"); + let command = IdunnLifecycleCommandRecord { + command_id: format!("command:{daemon_id}:{index:02}"), + daemon_id: daemon_id.into(), + action: "restart".into(), + state: "rejected".into(), + requested_by: "fixture".into(), + requested_at: requested_at.clone(), + detail: "fixture".into(), + claimed_at: requested_at.clone(), + result_id: String::new(), + }; + entries.push(typed_envelope(&command.command_id, &command, &requested_at).unwrap()); + } + } + let head = IdunnCurrentDeploymentRequestRecord { + daemon_id: "alpha".into(), + request_id: "deploy:alpha:00".into(), + sequence: 1, + updated_at: "2026-08-23T00:00:00Z".into(), + }; + entries.push(typed_envelope(&head.daemon_id, &head, &head.updated_at).unwrap()); + let mut backing = SingleFileMessagePackBackingStore::new(&options.store_path); + backing.push_all(&entries, Default::default()).unwrap(); + + assert!(compact_operational_history(&options, &Arc::new(Mutex::new(()))).unwrap() > 0); + assert_eq!( + compact_operational_history(&options, &Arc::new(Mutex::new(()))).unwrap(), + 0 + ); + let compacted = backing.pull_all_read_only_snapshot().unwrap(); + let count = |kind: &str| { + compacted + .iter() + .filter(|entry| entry.r#type == kind) + .count() + }; + assert_eq!(count(odin_core::IdunnKeepaliveDecisionRecord::TYPE), 16); + assert_eq!(count(IdunnOperatorAlarmRecord::TYPE), 16); + assert_eq!(count(IdunnLifecycleCommandRecord::TYPE), 64); + assert_eq!(count(IdunnDeploymentRequestRecord::TYPE), 17); + assert_eq!(count(IdunnDeploymentResultRecord::TYPE), 17); + assert!(compacted.iter().any(|entry| { + entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == "deploy:alpha:00" + })); + assert!(compacted.iter().any(|entry| { + entry.r#type == IdunnDeploymentResultRecord::TYPE + && entry.key == "result:deploy:alpha:00" + })); + assert!(compacted.iter().any(|entry| { + entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE && entry.key == "alpha" + })); + } + #[test] fn deployment_validator_accepts_exact_generic_authenticated_health() { let (message, options, _root) = generic_signed_health_fixture_with_release(1, true); diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md index 61aef4b..c2cff0c 100644 --- a/docs/idunn-signed-daemon-health-authority.md +++ b/docs/idunn-signed-daemon-health-authority.md @@ -68,6 +68,13 @@ legacy digest-keyed statement history to the one exact statement named by each current admission. A missing or digest-mismatched witness fails closed; it is never reconstructed from process probes or a later heartbeat. +The surrounding keepalive store follows a separate bounded-retention policy. +It keeps exact current authority plus small per-daemon diagnostic windows; +historical pulse decisions, repeated alarms, and superseded actuation receipts +cannot make authentication fsync cost grow with host age. The current +deployment head and every record required to rejoin active signed health remain +pinned through compaction. + The outward record exists only while a generic provider-authored admission is current and still matches its root trust and optional release binding. Missing publication, release drift, dependency failure, target absence, and recovery diff --git a/docs/idunn.md b/docs/idunn.md index 3acc663..e1729d4 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -164,6 +164,14 @@ idunn.rudp_health_ingress.v1 atomically; it must not append one store row per pulse. Startup compacts the former digest-keyed history through a whole-snapshot compare/exchange before scheduler workers start. +- The keepalive store is current operational authority with bounded diagnostic + history, not an event archive. Idunn retains the latest eight decisions, + alarms, automatic deployment/restart receipts, migration results, and + rollout results per daemon, plus the latest thirty-two operator lifecycle + commands. Exact current deployment heads, their request/result chain, active + health bindings, and recent command-linked receipts are pinned regardless of + age. Startup compacts legacy history atomically; later writes reapply the + same policy before the single-file store can exceed 1 MiB for long. - Health command exit status is not daemon awareness. Every Idunn target must declare a daemon-owned CultNet/RUDP health contract naming what health publication should prove and what unmarked failure means. From ed56dab81224d4ac33ef8ffc61fe1dc3a4959570 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 19:31:47 +0200 Subject: [PATCH 56/93] Persist Ghostlight public schema catalogs --- README.md | 5 +++++ src/odin/documents.cjs | 11 ++++++++++ test/ghostlight-discovery-documents.test.cjs | 21 ++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test/ghostlight-discovery-documents.test.cjs diff --git a/README.md b/README.md index 3281d7f..3f878a7 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,11 @@ their contents; Odin owns catalog acceptance and exact retrieval. Sensitive auth commands and claims travel directly between the consuming app and Heimdall, never through Odin. +Ghostlight publishes a `ghostlight.schema_catalog.v1` envelope containing only +the state contracts named by its public provider surface. Odin registers that +envelope so it can persist and return the provider-owned catalog; it does not +register every private Ghostlight receipt, transition, or simulation schema. + ## Gjallar Gjallar is the herald display daemon that runs on Nightwing. Odin sees the diff --git a/src/odin/documents.cjs b/src/odin/documents.cjs index d8047ab..cc93102 100644 --- a/src/odin/documents.cjs +++ b/src/odin/documents.cjs @@ -7,6 +7,7 @@ function defineOdinDocuments(defineDocumentType) { return { interfaceBindingDefinition: null, evePluginAdvertisementDefinition: null, + ghostlightSchemaCatalogDefinition: null, heimdallCommandBoundaryDefinition: null, heimdallTransportProfileDefinition: null, idunnCommandBoundaryDefinition: null, @@ -95,6 +96,15 @@ function defineOdinDocuments(defineDocumentType) { name: (value) => value?.pluginId || value?.plugin?.id || "plugin", schema: parseObjectDocument("Eve plugin advertisement"), }); + const ghostlightSchemaCatalogDefinition = defineDocumentType({ + type: "ghostlight.schema_catalog", + schemaName: "ghostlight.schema_catalog", + schemaId: "ghostlight.schema_catalog.v1", + schemaVersion: "ghostlight.schema_catalog.v1", + global: false, + name: (value) => value?.providerId || "gamecult.ghostlight.dungeon", + schema: parseObjectDocument("Ghostlight public schema catalog"), + }); const heimdallCommandBoundaryDefinition = defineDocumentType({ type: "heimdall.command_boundary", schemaName: "heimdall.command_boundary", @@ -477,6 +487,7 @@ function defineOdinDocuments(defineDocumentType) { return { interfaceBindingDefinition, evePluginAdvertisementDefinition, + ghostlightSchemaCatalogDefinition, heimdallCommandBoundaryDefinition, heimdallTransportProfileDefinition, interfaceLayoutDefinition, diff --git a/test/ghostlight-discovery-documents.test.cjs b/test/ghostlight-discovery-documents.test.cjs new file mode 100644 index 0000000..5c96e9e --- /dev/null +++ b/test/ghostlight-discovery-documents.test.cjs @@ -0,0 +1,21 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const test = require("node:test"); + +const { defineOdinDocuments } = require("../src/odin/documents.cjs"); + +test("Odin persists Ghostlight's public schema catalog envelope", () => { + const documents = defineOdinDocuments((definition) => definition); + const catalog = documents.ghostlightSchemaCatalogDefinition; + + assert.equal(catalog.type, "ghostlight.schema_catalog"); + assert.equal(catalog.schemaId, "ghostlight.schema_catalog.v1"); + assert.equal(catalog.schemaVersion, "ghostlight.schema_catalog.v1"); + assert.equal(catalog.name({ providerId: "gamecult.ghostlight.dungeon" }), "gamecult.ghostlight.dungeon"); +}); + +test("Odin's unavailable-runtime projection includes the Ghostlight catalog slot", () => { + const documents = defineOdinDocuments(null); + assert.equal(documents.ghostlightSchemaCatalogDefinition, null); +}); From 17d45abe068da9438084410e0376ac99a8c5001c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 19:37:34 +0200 Subject: [PATCH 57/93] Adopt reliable CultNet transport in Odin --- deploy/cultlib.commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/cultlib.commit b/deploy/cultlib.commit index d580d40..c7c5a16 100644 --- a/deploy/cultlib.commit +++ b/deploy/cultlib.commit @@ -1 +1 @@ -21163d83fa2670dbcc9cdd57c6c1776b33a91d62 +75c180782aeba7cfd22d6412877397708a4ed28f From ba76a7239a7bb40aa1774df7d93b9388dc27b222 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 19:41:59 +0200 Subject: [PATCH 58/93] Serialize Odin provider document persistence --- src/odin-coordinator.cjs | 6 ++---- src/odin/cultnet-rudp.cjs | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/odin-coordinator.cjs b/src/odin-coordinator.cjs index 15c28e7..66b4ec4 100644 --- a/src/odin-coordinator.cjs +++ b/src/odin-coordinator.cjs @@ -76,7 +76,7 @@ async function main() { } return (await meshNodePromise).cache; }, - onDocumentPutRaw: (document) => { + onDocumentPutRaw: async (document) => { if (observedRudpDocuments < 40) { observedRudpDocuments += 1; console.error( @@ -84,9 +84,7 @@ async function main() { ); } liveProviderRegistry.ingestDocument(document, document.remote); - persistRudpDocumentPut(document).catch((error) => { - console.error("CultMesh/RUDP document persist failed:", error.message); - }); + await persistRudpDocumentPut(document); }, }); await cultMeshRudpDocumentServer.start(); diff --git a/src/odin/cultnet-rudp.cjs b/src/odin/cultnet-rudp.cjs index 1f22d19..05b290e 100644 --- a/src/odin/cultnet-rudp.cjs +++ b/src/odin/cultnet-rudp.cjs @@ -77,8 +77,9 @@ function createCultMeshRudpDocumentServer(options) { }, onDocumentPutRaw: (document) => { if (typeof options.onDocumentPutRaw === "function") { - options.onDocumentPutRaw(document); + return options.onDocumentPutRaw(document); } + return undefined; }, }, ); From 0fc29fa5593ed5f2b2292d0979805565baddddd2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 23 Aug 2026 19:55:01 +0200 Subject: [PATCH 59/93] Record live reliable discovery body --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f878a7..4eb7220 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,13 @@ and renders the live display. Odin persists only document schemas it has registered. That is a deliberate typed-store boundary, not a license to accept anonymous blobs and hope later code understands them. The live Yggdrasil body at -`b4f9a2e95f0b41cebdeddc49223781d1d3c7b42a` registers Heimdall's provider, -command-boundary, Eve-plugin, and transport-profile documents. Heimdall owns -their contents; Odin owns catalog acceptance and exact retrieval. Sensitive -auth commands and claims travel directly between the consuming app and -Heimdall, never through Odin. +`ba76a7239a7bb40aa1774df7d93b9388dc27b222`, using CultLib +`75c180782aeba7cfd22d6412877397708a4ed28f`, registers Heimdall's provider, +command-boundary, Eve-plugin, and transport-profile documents plus +Ghostlight's public catalog envelope. Provider daemons own those documents; +Odin owns typed catalog acceptance, ordered durable persistence, and exact +retrieval. Sensitive auth commands and claims travel directly between the +consuming app and Heimdall, never through Odin. Ghostlight publishes a `ghostlight.schema_catalog.v1` envelope containing only the state contracts named by its public provider surface. Odin registers that From 98c67e35799defae73c517227c764fe15c772226 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 24 Aug 2026 20:40:19 +0200 Subject: [PATCH 60/93] Move Gjallar supervision to Yggdrasil --- README.md | 20 ++-- crates/idunn-daemon/src/main.rs | 142 ++++++++++++----------- docs/architecture.md | 26 ++--- docs/gjallar.md | 112 +++++++----------- docs/idunn.md | 23 ++-- scripts/deploy-nightwing-gjallar.cmd | 10 -- scripts/deploy-nightwing-gjallar.ps1 | 158 -------------------------- scripts/health-nightwing-gjallar.cmd | 3 - scripts/health-nightwing-gjallar.ps1 | 133 ---------------------- scripts/idunn-deployment-targets.ps1 | 18 --- scripts/linux/idunn-yggdrasil | 7 +- scripts/restart-nightwing-gjallar.cmd | 10 -- scripts/restart-nightwing-gjallar.ps1 | 17 --- 13 files changed, 153 insertions(+), 526 deletions(-) delete mode 100644 scripts/deploy-nightwing-gjallar.cmd delete mode 100644 scripts/deploy-nightwing-gjallar.ps1 delete mode 100644 scripts/health-nightwing-gjallar.cmd delete mode 100644 scripts/health-nightwing-gjallar.ps1 delete mode 100644 scripts/restart-nightwing-gjallar.cmd delete mode 100644 scripts/restart-nightwing-gjallar.ps1 diff --git a/README.md b/README.md index 4eb7220..2100091 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ The first Rust core lives in `crates/odin-core` and already separates typed documents, ingest ports, normalization, and repository persistence so unit tests can use mocked inputs and pipeline smokes can prove typed handoff without booting the whole daemon. Gjallar is not part of that Rust record spine: it is -the Nightwing-resident terminal compositor in `E:\Projects\Gjallar` that -consumes Odin's accepted `gamecult.eve.surface_state` snapshot over CultNet/RUDP -and renders the live display. +the Yggdrasil-resident C# composition daemon in `F:\Projects\Gjallar`. It +consumes Odin's accepted provider snapshot and publishes one tiled +`gjallar.overview` Eve surface for GUI, TUI, and agent lowerers. ## Typed access discovery @@ -44,18 +44,16 @@ register every private Ghostlight receipt, transition, or simulation schema. ## Gjallar -Gjallar is the herald display daemon that runs on Nightwing. Odin sees the -Verses, accepts provider surfaces, and publishes the `odin.providers` catalog. -Gjallar consumes Odin's accepted surface snapshot over CultNet/RUDP, composes -the multi-scale tiled dashboard from that typed state, lowers Odin's canonical -marquee tape into continuous gutter text, owns dense character-level update -behavior, and writes the visible framebuffer. +Gjallar is the herald composition daemon that runs beside Odin and Idunn on +Yggdrasil. Odin sees the Verses and accepts provider surfaces. Gjallar consumes +that accepted snapshot and publishes one multi-scale tiled Eve surface. Eve +clients—including EveCanvas—own graphical and TUI lowering. Local package surfaces: - Organ contract: `docs/gjallar.md` - Branding Persona state: `personas/gjallar.persona_state.cc` -- Runtime source: `E:\Projects\Gjallar` +- Runtime source: `F:\Projects\Gjallar` - Avatar asset: `assets/personas/gjallar-avatar.png` - Pixel avatar: `assets/personas/gjallar-avatar-pixel-256.png` @@ -117,7 +115,7 @@ Local package surfaces: - Outputs: CultCache-backed Odin state, CultMesh documents, and CultNet schema/catalog messages. Browser, GUI, TUI, and framebuffer renderers lower those documents outside Odin instead of asking Odin to host web surfaces. -- Derived state: Gjallar's attached Nightwing display, browser dashboards, and future Eve clients are projections of Odin state and provider-owned Eve/CultUI surfaces. +- Derived state: Gjallar's aggregate surface is derived from Odin/provider state; Eve clients derive pixels from that aggregate. - Forbidden writers: renderers do not probe the network or decide Verse truth; individual projects do not maintain private incompatible discovery ledgers once Odin can see them. - Shared paths: human dashboards, worker schedulers, Verse bootstrap code, and compact TUI views consume the same registry and schema catalog. - Deletion line: old per-host coordinator scripts should be deleted or reduced to deployment wrappers that start Odin. diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index d28b794..6a6ba0a 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -4862,10 +4862,10 @@ fn daemon_transport_profile( "partial-rudp-health-and-provider-store-live", "VoidBot stack health is published over CultNet/RUDP from the local orchestrator pulse, and the daemon-owned CultMesh witness store at E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc carries provider advertisement catalog, provider advertisement, command_boundary, and transport_profile records. The operations probe is a debug witness only.", ), - "nightwing-gjallar" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-service-boundary + native-odin-cultnet-rudp-snapshot-input", + "yggdrasil-gjallar" => ( + "daemon-published-rudp-health + daemon-owned-cultcache-aggregate-surface + local-odin-cultnet-rudp-snapshot-input", "partial-rudp-health-and-provider-store-live", - "Gjallar framebuffer composition health is published over CultNet/RUDP from Nightwing, the daemon-owned boundary store at /var/lib/gamecult/gjallar/cultcache/gjallar.service.cc carries provider advertisement, runtime_config, frame_status, gamecult.eve.surface_state, command_boundary, transport_profile, and daemon-health summary state, and live composition consumes Odin's accepted surface:gamecult.network.status snapshot over CultNet/RUDP. The service/status probe is a debug witness only.", + "Gjallar composition health and its typed aggregate Eve surface are published from Yggdrasil beside Odin. The daemon owns tiling and aggregation; clients own pixel lowering, and no framebuffer is opened by the daemon.", ), "nightwing-eve-dashboard" => ( "daemon-published-rudp-health + daemon-owned-cultmesh-state + daemon-owned-cultcache-boundary-store", @@ -5024,26 +5024,25 @@ fn daemon_surgery_plan(target: &DaemonTarget, updated_at: &str) -> IdunnDaemonSu "Make Odin ingest RUDP provider advertisements directly into its accepted service/catalog surface." .to_string(); } - "nightwing-gjallar" => { + "yggdrasil-gjallar" => { status = "partial-rudp-health-and-provider-store-live"; severity = "critical"; - owner = "Gjallar C# runtime plus Odin accepted snapshot surface"; + owner = "Gjallar Yggdrasil composition runtime plus Odin accepted snapshot surface"; current_mechanism = - "Nightwing Gjallar publishes gjallar.cultnet-rudp-framebuffer-composition-health over CultNet/RUDP from the C# runtime, the daemon-owned boundary store at /var/lib/gamecult/gjallar/cultcache/gjallar.service.cc now contains provider advertisement, gamecult.eve.surface_state, runtime_config, frame_status, command_boundary, transport_profile, and daemon-health summary records, and the live compositor polls Odin's accepted surface:gamecult.network.status snapshot over a configured cultnet.transport.rudp.v0 endpoint. Live Odin ingests that witness store without advertising a deck bridge as transport authority." + "Gjallar runs headless beside Odin and Idunn on Yggdrasil, consumes Odin's accepted provider snapshot, and publishes one typed aggregate Eve surface plus composition health without opening a framebuffer." .to_string(); intended_authority = - "Gjallar consumes Odin's accepted provider surface snapshot over CultNet/RUDP and publishes framebuffer composition health, provider advertisement, provider-owned Eve surface, runtime config, command boundary, and transport profile as typed CultMesh/CultNet state." + "Gjallar consumes Odin's accepted provider surface snapshot and publishes the composed aggregate, provider advertisement, runtime config, command boundary, transport profile, and health as typed CultMesh/CultNet state. Eve clients own graphical lowering." .to_string(); cut_line = - "Keep the service/status probe as a debug witness only; the daemon-owned witness now owns provider advertisement, gamecult.eve.surface_state, command_boundary, and transport_profile truth, live input now comes from Odin's accepted CultNet/RUDP snapshot surface through the configured Gjallar Odin endpoint, and live Gjallar daemon health now keys off snapshot freshness through receive.status, lastAttemptStatus, lastSuccessfulAtUtc, staleAfterSeconds, and consecutiveFailures so retained panels cannot masquerade as healthy after input goes stale." + "Retire the Nightwing service and framebuffer actuator. Keep one Yggdrasil daemon publishing the aggregate tree and freshness health; clients lower that tree without becoming aggregation owners." .to_string(); steps = vec![ - "Keep live gjallar.cultnet-rudp-framebuffer-composition-health publication running from Nightwing's Gjallar service.".to_string(), - "Keep Gjallar's daemon-owned boundary store at /var/lib/gamecult/gjallar/cultcache/gjallar.service.cc publishing provider advertisement, gamecult.eve.surface_state, runtime_config, frame_status, command_boundary, transport_profile, and daemon-health summary records.".to_string(), - "Keep the native C# CultNet/RUDP snapshot polling path pointed at Odin's accepted surface:gamecult.network.status surface through the configured Gjallar Odin endpoint; no baked Starfire LAN endpoint is allowed.".to_string(), - "Keep Gjallar's receive-freshness daemon health live on Nightwing so daemon-published health degrades when Odin snapshot success goes stale instead of keying only off rendered frames.".to_string(), - "Keep Gjallar renderer output derived from the daemon-owned CultMesh/Eve witness; do not advertise a deck bridge as service input or discovery authority.".to_string(), - "Keep the .ps1 lifecycle bodies as explicit witness/restart bodies and keep health .cmd wrappers archived so they cannot masquerade as Idunn health truth.".to_string(), + "Run Gjallar as yggdrasil-gjallar beside Odin and Idunn with its framebuffer backend disabled.".to_string(), + "Publish the full gjallar.overview Eve surface and daemon-owned boundary records from /var/lib/gamecult/gjallar/gjallar.service.cc.".to_string(), + "Keep the local CultNet/RUDP snapshot input pointed at Odin's accepted surface:gamecult.network.status.".to_string(), + "Key daemon health to snapshot and aggregate publication freshness rather than rendered frames.".to_string(), + "Retire the Nightwing deployment target and stop its gjallar.service before admitting the Yggdrasil body.".to_string(), ]; } "raven-sleipnir" => { @@ -5575,6 +5574,33 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { enabled: true, interval_seconds: 300, }, + DaemonTarget { + daemon_id: "yggdrasil-gjallar".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Gjallar Eve surface compositor".to_string(), + health_contract: locally_supervised_health_contract( + "gjallar.cultnet-rudp-composition-health", + "dependency-unavailable", + ), + deploy_command: Some(yggdrasil_actuator("deploy", "gjallar")), + restart_command: Some(yggdrasil_actuator("restart", "gjallar")), + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target_on_branch( + "Gjallar", + PathBuf::from("/srv/build/Gjallar"), + "codex/yggdrasil-aggregate-daemon", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/gjallar/deploy/deployment.env"), + ), + DEPLOYABLE_SOURCE_PATHS, + )), + enabled: true, + interval_seconds: 30, + }, ]), "starfire-local" => Ok(vec![ DaemonTarget { @@ -5756,26 +5782,6 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { enabled: true, interval_seconds: 300, }, - DaemonTarget { - daemon_id: "nightwing-gjallar".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Gjallar framebuffer compositor".to_string(), - health_contract: health_contract( - "gjallar.cultnet-rudp-framebuffer-composition-health", - "dependency-unavailable", - ), - deploy_command: Some(script("deploy-nightwing-gjallar.ps1")), - restart_command: Some(script("restart-nightwing-gjallar.ps1")), - release: Some(release_target( - "Gjallar", - project("Gjallar"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 30, - }, DaemonTarget { daemon_id: "nightwing-muninn".to_string(), verse_id: "nightwing.local".to_string(), @@ -7658,16 +7664,13 @@ mod tests { enabled: true, interval_seconds: 60, }; - let nightwing_gjallar = DaemonTarget { - daemon_id: "nightwing-gjallar".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Gjallar framebuffer compositor".to_string(), - health_contract: health_contract( - "gjallar.cultnet-rudp-framebuffer-composition-health", - "failed", - ), - deploy_command: Some("deploy-nightwing-gjallar.ps1".to_string()), - restart_command: Some("restart-nightwing-gjallar.ps1".to_string()), + let yggdrasil_gjallar = DaemonTarget { + daemon_id: "yggdrasil-gjallar".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Gjallar Eve surface compositor".to_string(), + health_contract: health_contract("gjallar.cultnet-rudp-composition-health", "failed"), + deploy_command: Some("idunn-yggdrasil deploy gjallar".to_string()), + restart_command: Some("idunn-yggdrasil restart gjallar".to_string()), release: None, enabled: true, interval_seconds: 30, @@ -7777,7 +7780,7 @@ mod tests { stonks, weksa.clone(), voidbot.clone(), - nightwing_gjallar.clone(), + yggdrasil_gjallar.clone(), starfire_muninn.clone(), nightwing_muninn.clone(), raven_muninn.clone(), @@ -8065,7 +8068,7 @@ mod tests { .contains("operations probe as a debug witness") ); - let gjallar_plan = daemon_surgery_plan(&nightwing_gjallar, "unix:100"); + let gjallar_plan = daemon_surgery_plan(&yggdrasil_gjallar, "unix:100"); assert_eq!( gjallar_plan.status, "partial-rudp-health-and-provider-store-live" @@ -8073,13 +8076,17 @@ mod tests { assert!( gjallar_plan .current_mechanism - .contains("gjallar.cultnet-rudp-framebuffer-composition-health") + .contains("runs headless beside Odin and Idunn") + ); + assert!( + gjallar_plan + .cut_line + .contains("Retire the Nightwing service") ); - assert!(gjallar_plan.cut_line.contains("gamecult.eve.surface_state")); assert!( gjallar_plan .current_mechanism - .contains("surface:gamecult.network.status") + .contains("accepted provider snapshot") ); let nightwing_eve_dashboard_plan = @@ -8393,7 +8400,7 @@ mod tests { actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") ); assert!(actuator.contains( - "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false" + "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight|gjallar) target_requires_bifrost_authority=false" )); assert!( actuator @@ -8451,6 +8458,14 @@ mod tests { "heimdall", false, ), + ( + "yggdrasil-gjallar", + "Gjallar", + "codex/yggdrasil-aggregate-daemon", + "/srv/gjallar/deploy/deployment.env", + "gjallar", + true, + ), ] { let target = targets .iter() @@ -8484,6 +8499,8 @@ mod tests { assert!(actuator.contains("deploy:ghostlight|restart:ghostlight")); assert!(actuator.contains("restart:heimdall")); assert!(actuator.contains("deploy:heimdall")); + assert!(actuator.contains("restart:gjallar")); + assert!(actuator.contains("deploy:gjallar")); } struct FakeReleaseStatePort { @@ -9301,15 +9318,12 @@ mod tests { #[test] fn gjallar_transport_profile_marks_partial_rudp_health() { let gjallar = DaemonTarget { - daemon_id: "nightwing-gjallar".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Gjallar framebuffer compositor".to_string(), - health_contract: health_contract( - "gjallar.cultnet-rudp-framebuffer-composition-health", - "failed", - ), - deploy_command: Some("deploy-nightwing-gjallar.ps1".to_string()), - restart_command: Some("restart-nightwing-gjallar.ps1".to_string()), + daemon_id: "yggdrasil-gjallar".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Gjallar Eve surface compositor".to_string(), + health_contract: health_contract("gjallar.cultnet-rudp-composition-health", "failed"), + deploy_command: Some("idunn-yggdrasil deploy gjallar".to_string()), + restart_command: Some("idunn-yggdrasil restart gjallar".to_string()), release: None, enabled: true, interval_seconds: 30, @@ -9320,14 +9334,10 @@ mod tests { assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); assert_eq!( profile.current_transport, - "daemon-published-rudp-health + daemon-owned-cultcache-service-boundary + native-odin-cultnet-rudp-snapshot-input" + "daemon-published-rudp-health + daemon-owned-cultcache-aggregate-surface + local-odin-cultnet-rudp-snapshot-input" ); - assert!( - profile - .cut_line - .contains("Gjallar framebuffer composition health is published over CultNet/RUDP") - ); - assert!(profile.cut_line.contains("surface:gamecult.network.status")); + assert!(profile.cut_line.contains("typed aggregate Eve surface")); + assert!(profile.cut_line.contains("no framebuffer")); } #[test] diff --git a/docs/architecture.md b/docs/architecture.md index 0b2a43d..c5f9391 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -33,7 +33,7 @@ Verse / host / device / provider inputs -> CultCache .cc persistence -> CultNet/CultMesh document registry -> Odin Eve/CultUI deck projection - -> Gjallar Nightwing composition and framebuffer lowering + -> Gjallar Yggdrasil aggregate Eve composition -> compact display feeds ``` @@ -87,14 +87,12 @@ schema from overwriting another in the shared catalog. Odin returns the redacted route metadata; it does not proxy private auth operations or own any claim, completion, token, or app secret. -Gjallar is the Nightwing-resident terminal compositor for what Odin can show. -Its runtime lives in `E:\Projects\Gjallar` and consumes Odin's accepted -provider-state snapshot over CultNet/RUDP. -Odin owns canonical marquee content; Gjallar owns provider enumeration for -display, panel packing, marquee lowering, glyph/color/framebuffer lowering, -frame stats, and the multi-scale terminal product. It must not own the -underlying registry, probe, provider truth, canonical marquee content, or -translation decisions. +Gjallar is the Yggdrasil-resident aggregate compositor for what Odin can show. +Its runtime lives in `/srv/gjallar/current` and consumes Odin's accepted +provider-state snapshot. Gjallar owns enumeration, tiling, and publication of +the typed `gjallar.overview` Eve surface. Eve clients own graphical, terminal, +and framebuffer lowering. Gjallar must not own the underlying registry, probe, +provider truth, client pixels, or translation decisions. Idunn is the named keepalive organ for daemon continuity. Its current Rust body lives in `crates/idunn-daemon` and `crates/odin-core/src/idunn.rs`. Idunn may read @@ -229,11 +227,11 @@ Odin currently publishes service squares for: StreamPixels, Heimdall, Repixelizer, and Bifrost systemd state until those daemon surfaces publish health and command boundaries over CultNet RUDP. -Gjallar consumes Odin's accepted CultMesh/Eve state and composes these surfaces -into the Nightwing display. Nightwing is the host/body; Gjallar is the terminal product running -there. If Odin starts deciding framebuffer composition, the renderer owner has -leaked upward. If individual providers start tuning themselves for Nightwing -instead of emitting clean Eve/CultUI surfaces, provider truth has leaked +Gjallar consumes Odin's accepted CultMesh/Eve state and composes every visible +provider surface into one typed aggregate on Yggdrasil. EveCanvas, browser, and +TUI clients lower that aggregate. If Odin starts deciding tile composition, the +composition owner has leaked upward. If providers tune themselves for one +client instead of emitting clean Eve/CultUI surfaces, provider truth has leaked downward. ## Current Interface Surface diff --git a/docs/gjallar.md b/docs/gjallar.md index 369c979..969c735 100644 --- a/docs/gjallar.md +++ b/docs/gjallar.md @@ -1,85 +1,61 @@ # Gjallar -Gjallar is the Nightwing-resident terminal compositor for Odin's domain. +Gjallar is the Yggdrasil-resident aggregate compositor for Odin's domain. -Odin is the all-seer: it accepts Verse discovery, schema catalogs, translation -routes, provider surfaces, and observation projections. Gjallar talks to Odin, -enumerates the active provider surfaces Odin can show, and composes those -surfaces into the live multi-scale dashboard running on Nightwing. - -Gjallar exists so Odin does not need to care how a pile of provider-owned TUIs -fits on one fast display, and provider daemons do not need to know the terminal -body they are being lowered into. +Odin accepts Verse discovery and provider-owned Eve surfaces. Gjallar consumes +that accepted snapshot and publishes one typed `gjallar.overview` composition +containing every currently visible provider surface. EveCanvas, browsers, TUI +clients, and agent readers lower the same aggregate without becoming its owner. ## Authority Map -- Owner: Gjallar owns Nightwing dashboard composition, tiling, marquee lowering, - visual density, framebuffer presentation, and character-level refresh - behavior. -- Inputs: Odin's accepted CultMesh/CultNet snapshot, provider ids, - provider-owned surface graphs, Odin's canonical marquee tape, display - constraints, font choices, and operator runtime flags. -- Outputs: the visible Nightwing framebuffer and compact Gjallar frame/status - telemetry. -- Derived state: panel packing, visual weight, tile position, gutter cells, - marquee glyph positions, glyph size, and frame timing are derived from - Odin/provider surfaces plus display constraints. -- Forbidden writers: Gjallar does not probe hosts, accept Verse truth, mutate - provider-owned dashboards, invent schema translation routes, or replace Odin's - provider registry. -- Shared paths: Nightwing's physical display, local frame dumps, future compact - overlays, and agent-facing TUI captures should all lower the same Gjallar - composition behavior when they want the "everything Odin can show" view. -- Deletion line: the old Rust `gjallar.overview` feed is not a runtime - authority. Any path that wants Nightwing composition belongs in - `E:\Projects\Gjallar`; any path that decides discovery truth belongs in Odin. +- Owner: Gjallar owns aggregate membership, tiling, marquee placement, layout + intent, visibility, and composition versioning. +- Inputs: Odin's accepted CultMesh/CultNet snapshot, provider-owned surface + graphs, Odin's canonical marquee tape, and display-independent layout hints. +- Outputs: `gjallar.overview`, its CultCache/CultMesh/CultNet publication, and + compact composition/freshness telemetry. +- Derived state: panel weights, ordering, tile layout, unavailable presentation, + and aggregate version are derived from Odin/provider state. +- Forbidden writers: Gjallar does not probe hosts, accept discovery truth, + mutate provider state, open `/dev/fb0` in daemon mode, capture client input, + or decide client pixels. Eve clients do not decide aggregate membership. +- Shared paths: iOS/UIKit, browser, optional TUI/framebuffer, and agent capture + lower the same published overview. +- Deletion line: Nightwing's `gjallar.service`, framebuffer lifecycle target, + and `nightwing-gjallar` Idunn actuator are obsolete authorities and must not + survive Yggdrasil admission. ## Body -- `E:\Projects\Gjallar` is Gjallar's executable C# runtime repo. -- `Gjallar.csproj` builds the Nightwing framebuffer compositor. -- Gjallar consumes Odin's accepted provider/interface state through the CultNet - RUDP snapshot input. Browser-deck routes are not Gjallar input truth. -- The old Rust `crates/gjallar-daemon` and `gjallar.overview` records were cut - because they created an intermediate composition owner that did nothing Odin - and Gjallar's live renderer could not explain directly. -- `assets/personas/gjallar-avatar.png` and - `assets/personas/gjallar-avatar-pixel-256.png` remain branding assets for the - view/persona surface. - -Build from the repo root: - -```powershell -dotnet build E:\Projects\Gjallar\src\Gjallar\Gjallar.csproj -``` - -Publish for Nightwing: - -```powershell -dotnet publish E:\Projects\Gjallar\src\Gjallar\Gjallar.csproj -c Release -r linux-x64 --self-contained true -o E:\Projects\Gjallar\scratch\publish\gjallar -``` +- Runtime source: `F:\Projects\Gjallar` / `GameCult/Gjallar`. +- Daemon host: Yggdrasil beside `odin.service` and + `idunn-yggdrasil.service`. +- Unit: `gjallar-yggdrasil.service`. +- Runtime mode: `--headless`; no framebuffer, font atlas, or mouse device is + opened. +- Input: Yggdrasil-local Odin accepted snapshot over CultNet/RUDP. +- State: `/var/lib/gamecult/gjallar/gjallar.service.cc`. +- Deployment: immutable source/dependency pins, a digest-pinned .NET SDK image, + and Idunn's root-owned `gjallar` actuator manifest. ## Runtime Contract ```text -provider-owned Eve/CultUI surfaces - -> Odin discovery and provider catalog/proxy surfaces - -> Odin canonical Stonks/poetry marquee tape - -> Gjallar provider enumeration, packing, marquee lowering, and framebuffer lowering - -> Nightwing visible display +provider-owned Eve surfaces + -> Odin accepted discovery/provider snapshot + -> Gjallar aggregation and tiling + -> typed gjallar.overview Eve surface + -> EveCanvas / browser / TUI / agent lowerers ``` -Nightwing is the host/body. Gjallar is the product that runs there. Odin owns -the accepted discovery/provider view. Each daemon owns its own surface truth. - ## Invariants -- Odin remains the accepted owner of all-seer discovery and provider indexing. -- Gjallar owns display composition, not discovery truth. -- Provider surfaces are lowered, not rewritten into status summaries. -- Marquee content is not provider-boundary status noise. Odin publishes the - canonical tape; Gjallar renders it as one continuous stream across gutter - rows. -- Missing or invalid provider surfaces disappear or render as unavailable; they - do not become invented truth. -- Frame/status telemetry observes Gjallar's rendering behavior only. +- Odin owns discovery and provider acceptance. +- Providers own their surface truth and command consequences. +- Gjallar owns composition, not discovery or pixel rendering. +- Eve clients own rendering and local input capture. +- Missing or invalid provider surfaces become explicit unavailable state or are + omitted; Gjallar never invents replacement truth. +- Daemon health follows accepted-snapshot and aggregate-publication freshness, + not rendered frames. diff --git a/docs/idunn.md b/docs/idunn.md index e1729d4..81eaa79 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -289,19 +289,11 @@ idunn.rudp_health_ingress.v1 terminals or interactive windows on that host. The Raven Muninn restart path uses a hidden WScript/PowerShell trampoline; replacing it with an interactive console launch violates the ops contract. -- Nightwing Gjallar deployment freshness and visible composition health are now - part of Idunn's ops role. `health-nightwing-gjallar.ps1` verifies - `gjallar.service` liveness, the remote deployment manifest at - `/opt/gamecult/gjallar/gamecult-gjallar-deploy-manifest.txt`. A missing or - stale manifest emits `idunn.health.state=stale-deployment`; Idunn then runs - `deploy-nightwing-gjallar.ps1`, which publishes the committed local Gjallar - revision, writes `gamecult.gjallar.deployment_manifest.v1`, restarts - `gjallar.service`, and leaves deployment request/result records in its - keepalive `.cc`. The same health probe also reads `/var/log/gjallar.status`; - an empty catalog, failed receive loop, stale status witness, or zero composed - provider panels is unhealthy even when the process is alive. Upstream deck - failure emits `idunn.health.state=dependency-unavailable` so Idunn does not - redeploy Gjallar for an Odin/provider input outage. +- Yggdrasil Gjallar deployment freshness and aggregate composition health are + part of Idunn's ops role. The daemon publishes + `gjallar.cultnet-rudp-composition-health`; Idunn's Yggdrasil actuator admits + immutable releases through the Gjallar deploy manifest and restarts + `gjallar.service`. Nightwing has no Gjallar deployment or restart authority. - Swarm-wide deployment ownership means Idunn owns the target catalog and the freshness contract for the repo swarm. For enforced deploy targets, the catalog must name upstream remote/branch, rollout strategy, state migration @@ -405,9 +397,8 @@ Stonks publishes `stonks.cultnet-rudp-market-health` after each serialized market refresh. Weksa publishes `weksa.cultnet-rudp-provider-health` after each serialized provider witness refresh. VoidBot publishes `voidbot.cultnet-rudp-stack-health` after each GameCult Local Orchestrator -pulse. Gjallar publishes -`gjallar.cultnet-rudp-framebuffer-composition-health` from the C# Nightwing -framebuffer service. Mimir Eve dashboard publishes +pulse. Gjallar publishes `gjallar.cultnet-rudp-composition-health` from the C# +Yggdrasil aggregate compositor. Mimir Eve dashboard publishes `mimir.cultnet-rudp-provider-health` from the Nightwing systemd broker, and the same process publishes `nightwing.cultnet-rudp-eve-dashboard-health` for the Nightwing dashboard service itself. The Nightwing Eve browser reference now runs diff --git a/scripts/deploy-nightwing-gjallar.cmd b/scripts/deploy-nightwing-gjallar.cmd deleted file mode 100644 index 868160e..0000000 --- a/scripts/deploy-nightwing-gjallar.cmd +++ /dev/null @@ -1,10 +0,0 @@ -@echo off -if not "%IDUNN_ACTUATOR%"=="1" ( - echo This deployment wrapper is an Idunn actuator. Configure Idunn; do not deploy manually. - exit /b 1 -) -if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( - echo This deployment wrapper is an Idunn actuator. Configure Idunn; do not deploy manually. - exit /b 1 -) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\deploy-nightwing-gjallar.ps1 diff --git a/scripts/deploy-nightwing-gjallar.ps1 b/scripts/deploy-nightwing-gjallar.ps1 deleted file mode 100644 index 31ba835..0000000 --- a/scripts/deploy-nightwing-gjallar.ps1 +++ /dev/null @@ -1,158 +0,0 @@ -param( - [string] $GjallarRoot = "E:\Projects\Gjallar", - [string] $CultLibRoot = "E:\Projects\CultLib", - [string] $CultMathRoot = "E:\Projects\CultMath", - [string] $SshTarget = "nwroot", - [string] $RemoteDir = "/opt/gamecult/gjallar", - [string] $CultCachePath = "/var/lib/gamecult/gjallar/cultcache/gjallar.service.cc", - [string] $OdinCultMeshUri = $(if ($env:GJALLAR_ODIN_CULTMESH_URI) { $env:GJALLAR_ODIN_CULTMESH_URI } elseif ($env:ODIN_CULTMESH_URI) { $env:ODIN_CULTMESH_URI } else { "cultmesh://odin/rendezvous/provider-catalog" }), - [string] $IdunnRudpHealth = $(if ($env:GJALLAR_IDUNN_RUDP_HEALTH) { $env:GJALLAR_IDUNN_RUDP_HEALTH } else { $env:IDUNN_RUDP_HEALTH }), - [string] $IdunnDaemon = "nightwing-gjallar", - [string] $IdunnHealthContract = "gjallar.cultnet-rudp-framebuffer-composition-health", - [string] $UpstreamRemote = "origin", - [string] $UpstreamBranch = "main" -) - -$ErrorActionPreference = "Stop" - -function Remove-ScratchPath { - param( - [string] $ScratchRoot, - [string] $TargetPath - ) - - if (-not (Test-Path -LiteralPath $TargetPath)) { - return - } - - $resolvedScratch = (Resolve-Path -LiteralPath $ScratchRoot).Path - $resolvedTarget = (Resolve-Path -LiteralPath $TargetPath).Path - if (-not $resolvedTarget.StartsWith($resolvedScratch, [StringComparison]::OrdinalIgnoreCase)) { - throw "Refusing to remove scratch path outside scratch root: $resolvedTarget" - } - - Remove-Item -LiteralPath $TargetPath -Recurse -Force -} - -function Export-GitArchive { - param( - [string] $RepoRoot, - [string] $Ref, - [string] $Destination, - [string] $TarPath - ) - - Remove-ScratchPath -ScratchRoot (Split-Path -Parent $Destination) -TargetPath $Destination - New-Item -ItemType Directory -Force -Path $Destination | Out-Null - git -C $RepoRoot archive --format=tar --output=$TarPath $Ref - if ($LASTEXITCODE -ne 0) { - throw "git archive failed for $RepoRoot $Ref" - } - tar -xf $TarPath -C $Destination - if ($LASTEXITCODE -ne 0) { - throw "source extraction failed for $TarPath" - } -} - -if ($env:IDUNN_ACTUATOR -ne "1" -or $env:IDUNN_COMMAND_AUTHORITY -ne "idunn-daemon") { - throw "This deployment script is an Idunn actuator. Agents must configure Idunn release targets and let Idunn run deployment; do not invoke deploy scripts manually." -} -if ($env:GJALLAR_ODIN_CULTNET_RUDP -or $env:ODIN_CULTNET_RUDP) { - throw "GJALLAR_ODIN_CULTNET_RUDP and ODIN_CULTNET_RUDP were removed. Configure GJALLAR_ODIN_CULTMESH_URI or ODIN_CULTMESH_URI with a cultmesh:// Odin route." -} -if ([string]::IsNullOrWhiteSpace($OdinCultMeshUri) -or -not $OdinCultMeshUri.StartsWith("cultmesh://", [StringComparison]::OrdinalIgnoreCase)) { - throw "Odin CultMesh URI must be supplied by GJALLAR_ODIN_CULTMESH_URI, ODIN_CULTMESH_URI, or -OdinCultMeshUri and must start with cultmesh://." -} -if ([string]::IsNullOrWhiteSpace($IdunnRudpHealth)) { - throw "Idunn RUDP health endpoint must be supplied by GJALLAR_IDUNN_RUDP_HEALTH, IDUNN_RUDP_HEALTH, or -IdunnRudpHealth; no WireGuard endpoint default is allowed." -} - -$sourceRef = "$UpstreamRemote/$UpstreamBranch" -$scratchRoot = Join-Path $GjallarRoot "scratch" -$releaseSource = Join-Path $scratchRoot "idunn-release-source" -$cultLibRelease = Join-Path $scratchRoot "CultLib" -$cultMathRelease = Join-Path $scratchRoot "CultMath" -$releaseTarPath = Join-Path $scratchRoot "gjallar-source-release.tar" -$cultLibTarPath = Join-Path $scratchRoot "cultlib-source-release.tar" -$cultMathTarPath = Join-Path $scratchRoot "cultmath-source-release.tar" -$publishDir = Join-Path $scratchRoot "publish\gjallar" -$tarPath = Join-Path $GjallarRoot "scratch\gjallar-publish.tar" -$projectPath = Join-Path $releaseSource "src\Gjallar\Gjallar.csproj" -$manifestPath = Join-Path $publishDir "gamecult-gjallar-deploy-manifest.txt" - -git -C $GjallarRoot fetch --prune $UpstreamRemote $UpstreamBranch -if ($LASTEXITCODE -ne 0) { - throw "git fetch failed for $GjallarRoot $sourceRef" -} - -$commit = (git -C $GjallarRoot rev-parse $sourceRef).Trim() -if ([string]::IsNullOrWhiteSpace($commit)) { - throw "Could not determine Gjallar git revision." -} - -$cultLibCommit = (git -C $CultLibRoot rev-parse HEAD).Trim() -if ([string]::IsNullOrWhiteSpace($cultLibCommit)) { - throw "Could not determine CultLib git revision." -} - -$cultMathCommit = (git -C $CultMathRoot rev-parse HEAD).Trim() -if ([string]::IsNullOrWhiteSpace($cultMathCommit)) { - throw "Could not determine CultMath git revision." -} - -New-Item -ItemType Directory -Force -Path $scratchRoot | Out-Null -Remove-ScratchPath -ScratchRoot $scratchRoot -TargetPath $publishDir -Export-GitArchive -RepoRoot $GjallarRoot -Ref $sourceRef -Destination $releaseSource -TarPath $releaseTarPath -Export-GitArchive -RepoRoot $CultLibRoot -Ref $cultLibCommit -Destination $cultLibRelease -TarPath $cultLibTarPath -Export-GitArchive -RepoRoot $CultMathRoot -Ref $cultMathCommit -Destination $cultMathRelease -TarPath $cultMathTarPath - -dotnet publish $projectPath -c Release -r linux-x64 --self-contained true -o $publishDir -if ($LASTEXITCODE -ne 0) { - throw "dotnet publish failed with exit code $LASTEXITCODE" -} - -$dllPath = Join-Path $publishDir "Gjallar.dll" -$hash = (Get-FileHash $dllPath -Algorithm SHA256).Hash.ToLowerInvariant() -$deployedAt = [DateTimeOffset]::UtcNow.ToString("O") -@( - "schema=gamecult.gjallar.deployment_manifest.v1" - "upstreamRemote=$UpstreamRemote" - "upstreamBranch=$UpstreamBranch" - "sourceRef=$sourceRef" - "gitCommit=$commit" - "cultLibCommit=$cultLibCommit" - "cultMathCommit=$cultMathCommit" - "artifact=Gjallar.dll" - "sha256=$hash" - "deployedAtUtc=$deployedAt" -) | Set-Content -Encoding ASCII -LiteralPath $manifestPath - -tar -cf $tarPath -C $publishDir . -if ($LASTEXITCODE -ne 0) { - throw "tar failed with exit code $LASTEXITCODE" -} - -scp.exe $tarPath "${SshTarget}:/tmp/gjallar-publish.tar" -if ($LASTEXITCODE -ne 0) { - throw "scp failed with exit code $LASTEXITCODE" -} - -$cultCacheDir = if ($CultCachePath.LastIndexOf('/') -gt 0) { $CultCachePath.Substring(0, $CultCachePath.LastIndexOf('/')) } else { "." } -$dropIn = @" -[Service] -Environment=GJALLAR_CULTCACHE_PATH=$CultCachePath -Environment=GJALLAR_ODIN_CULTMESH_URI=$OdinCultMeshUri -Environment=GJALLAR_IDUNN_RUDP_HEALTH=$IdunnRudpHealth -Environment=GJALLAR_IDUNN_DAEMON=$IdunnDaemon -Environment=GJALLAR_IDUNN_HEALTH_CONTRACT=$IdunnHealthContract -"@ -$dropInBase64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($dropIn)) - -ssh.exe $SshTarget "mkdir -p '$RemoteDir' '$cultCacheDir' /etc/systemd/system/gjallar.service.d && tar -xf /tmp/gjallar-publish.tar -C '$RemoteDir' && chmod +x '$RemoteDir/Gjallar' && printf '%s' '$dropInBase64' | base64 -d > /etc/systemd/system/gjallar.service.d/idunn-rudp-health.conf && systemctl daemon-reload && systemctl restart gjallar.service && systemctl is-active --quiet gjallar.service" -if ($LASTEXITCODE -ne 0) { - throw "remote deploy or restart failed with exit code $LASTEXITCODE" -} - -Write-Host "Gjallar deployed to Nightwing." -Write-Host " gitCommit=$commit" -Write-Host " sha256=$hash" diff --git a/scripts/health-nightwing-gjallar.cmd b/scripts/health-nightwing-gjallar.cmd deleted file mode 100644 index 326a7fd..0000000 --- a/scripts/health-nightwing-gjallar.cmd +++ /dev/null @@ -1,3 +0,0 @@ -@echo off -echo health-nightwing-gjallar.cmd archived: Gjallar health is daemon-published over CultNet/RUDP as gjallar.cultnet-rudp-framebuffer-composition-health. Use health-nightwing-gjallar.ps1 only as an explicit witness inspection, not Idunn health truth. -exit /b 2 diff --git a/scripts/health-nightwing-gjallar.ps1 b/scripts/health-nightwing-gjallar.ps1 deleted file mode 100644 index d1c7034..0000000 --- a/scripts/health-nightwing-gjallar.ps1 +++ /dev/null @@ -1,133 +0,0 @@ -param( - [string] $GjallarRoot = "E:\Projects\Gjallar", - [string] $SshTarget = "nightwing", - [string] $RemoteManifest = "/opt/gamecult/gjallar/gamecult-gjallar-deploy-manifest.txt", - [string] $RemoteStatus = "/var/log/gjallar.status", - [string] $RemoteCultCache = "/var/lib/gamecult/gjallar/cultcache/gjallar.service.cc", - [int] $MaxStatusAgeSeconds = 120, - [int] $MinCatalogProviders = 1, - [int] $MinComposedProviders = 1 -) - -$ErrorActionPreference = "Stop" - -function Fail-IdunnHealth { - param( - [string] $State, - [string] $Message - ) - - Write-Output "idunn.health.state=$State" - throw $Message -} - -ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "systemctl is-active --quiet gjallar.service" -if ($LASTEXITCODE -ne 0) { - Fail-IdunnHealth -State "failed" -Message "gjallar.service is not active on Nightwing." -} - -$localCommit = (git -C $GjallarRoot rev-parse HEAD).Trim() -if ([string]::IsNullOrWhiteSpace($localCommit)) { - throw "Could not determine local Gjallar git revision." -} - -$manifest = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "cat '$RemoteManifest' 2>/dev/null" -if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($manifest)) { - Fail-IdunnHealth -State "stale-deployment" -Message "Nightwing Gjallar deploy manifest is missing." -} - -$remoteCommit = "" -foreach ($line in $manifest) { - if ($line -like "gitCommit=*") { - $remoteCommit = $line.Substring("gitCommit=".Length).Trim() - } -} - -if ($remoteCommit -ne $localCommit) { - Fail-IdunnHealth -State "stale-deployment" -Message "Nightwing Gjallar deploy is stale. remote=$remoteCommit local=$localCommit" -} - -$statusJson = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "cat '$RemoteStatus' 2>/dev/null" -if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($statusJson)) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing at $RemoteStatus." -} - -$status = $statusJson | ConvertFrom-Json -if ($status.schema -ne "gamecult.gjallar.frame.v1") { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness has unexpected schema '$($status.schema)'." -} - -$updatedAt = [DateTimeOffset]::Parse($status.updatedAtUtc) -$ageSeconds = ([DateTimeOffset]::UtcNow - $updatedAt.ToUniversalTime()).TotalSeconds -if ($ageSeconds -gt $MaxStatusAgeSeconds) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is stale ($([math]::Round($ageSeconds))s old)." -} - -if ($status.status -ne "rendered") { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar framebuffer status is '$($status.status)', expected rendered." -} - -if ($null -eq $status.cultCacheWitness -or $status.cultCacheWitness.status -ne "published") { - $detail = if ($null -eq $status.cultCacheWitness) { "missing cultCacheWitness status" } else { $status.cultCacheWitness.error } - Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar has not published the daemon-owned CultCache witness. $detail" -} - -if ([string]::IsNullOrWhiteSpace([string]$status.receive.lastAttemptStatus)) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.lastAttemptStatus." -} - -if ([string]::IsNullOrWhiteSpace([string]$status.receive.lastSuccessfulAtUtc)) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.lastSuccessfulAtUtc." -} - -if ([int]$status.receive.staleAfterSeconds -le 0) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness has invalid receive.staleAfterSeconds=$($status.receive.staleAfterSeconds)." -} - -if ($null -eq $status.receive.consecutiveFailures) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.consecutiveFailures." -} - -if ($status.receive.status -ne "catalog-composed") { - $detail = $status.receive.error - if ([string]::IsNullOrWhiteSpace($detail)) { - $detail = $status.receive.providerFetchError - } - Fail-IdunnHealth -State "dependency-unavailable" -Message "Nightwing Gjallar receive status is '$($status.receive.status)', expected catalog-composed. $detail" -} - -if ([int]$status.receive.catalogProviders -lt $MinCatalogProviders) { - Fail-IdunnHealth -State "dependency-unavailable" -Message "Nightwing Gjallar catalog is empty: catalogProviders=$($status.receive.catalogProviders), expected at least $MinCatalogProviders." -} - -if ([int]$status.receive.composedProviders -lt $MinComposedProviders) { - Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar composed no provider panels: composedProviders=$($status.receive.composedProviders), expected at least $MinComposedProviders." -} - -if ($null -eq $status.idunnRudpHealth -or $status.idunnRudpHealth.status -ne "published") { - $detail = if ($null -eq $status.idunnRudpHealth) { "missing idunnRudpHealth status" } else { $status.idunnRudpHealth.error } - Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar has not published daemon-owned Idunn RUDP health. $detail" -} - -$cultCacheStat = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "stat -c '%s %Y' '$RemoteCultCache' 2>/dev/null" -if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($cultCacheStat)) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness is missing at $RemoteCultCache." -} - -$cultCacheParts = $cultCacheStat.Trim() -split '\s+' -if ($cultCacheParts.Length -lt 2) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness stat output was malformed: $cultCacheStat" -} - -$cultCacheSize = [int64]$cultCacheParts[0] -$cultCacheModifiedUtc = [DateTimeOffset]::FromUnixTimeSeconds([int64]$cultCacheParts[1]) -$cultCacheAgeSeconds = ([DateTimeOffset]::UtcNow - $cultCacheModifiedUtc).TotalSeconds -if ($cultCacheSize -le 0) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness at $RemoteCultCache is empty." -} - -if ($cultCacheAgeSeconds -gt $MaxStatusAgeSeconds) { - Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness is stale ($([math]::Round($cultCacheAgeSeconds))s old)." -} - -Write-Host "Nightwing Gjallar is active, deployed at $localCommit, and composing $($status.receive.composedProviders)/$($status.receive.catalogProviders) providers." diff --git a/scripts/idunn-deployment-targets.ps1 b/scripts/idunn-deployment-targets.ps1 index d36ac46..9eb7241 100644 --- a/scripts/idunn-deployment-targets.ps1 +++ b/scripts/idunn-deployment-targets.ps1 @@ -135,24 +135,6 @@ $IdunnDeploymentTargets = @( Aliases = @() Reason = "Central Idunn reaches Raven over SSH and keeps Sleipnir's hidden scheduled task alive while Sleipnir publishes daemon-owned RUDP health." }, - [pscustomobject]@{ - Id = "nightwing-gjallar" - Repo = "Gjallar" - LocalPath = "E:\Projects\Gjallar" - Host = "nightwing" - Service = "gjallar.service" - Status = "enforced" - Health = "daemon-published-rudp:gjallar.cultnet-rudp-framebuffer-composition-health" - Deploy = "$repoRoot\scripts\deploy-nightwing-gjallar.ps1" - Restart = "$repoRoot\scripts\restart-nightwing-gjallar.ps1" - Aliases = @("$repoRoot\scripts\deploy-nightwing-gjallar.cmd", "$repoRoot\scripts\restart-nightwing-gjallar.cmd") - UpstreamRemote = "origin" - UpstreamBranch = "main" - RolloutStrategy = "restart-after-verified-build" - StateMigration = "daemon-owned-noop" - ZeroDowntime = "restart-required" - Reason = "Nightwing framebuffer compositor has a committed artifact manifest and automatic Idunn deploy lane." - }, [pscustomobject]@{ Id = "yggdrasil-voidbot" Repo = "VoidBot" diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index 3644560..65c7f83 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -20,7 +20,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight) target_requires_bifrost_authority=false ;; + epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight|gjallar) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -69,6 +69,9 @@ case "$action:$target" in restart:streampixels) docker compose -f "$compose" restart streampixels-service streampixels-web ;; + restart:gjallar) + exec systemctl restart gjallar-yggdrasil.service + ;; deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:odin|restart:odin|deploy:ghostlight|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then @@ -88,7 +91,7 @@ case "$action:$target" in IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" \ exec "$manifest" "$action" ;; - deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback) + deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback|deploy:gjallar) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 diff --git a/scripts/restart-nightwing-gjallar.cmd b/scripts/restart-nightwing-gjallar.cmd deleted file mode 100644 index 625e798..0000000 --- a/scripts/restart-nightwing-gjallar.cmd +++ /dev/null @@ -1,10 +0,0 @@ -@echo off -if not "%IDUNN_ACTUATOR%"=="1" ( - echo restart-nightwing-gjallar.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 - exit /b 1 -) -if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( - echo restart-nightwing-gjallar.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 - exit /b 1 -) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-nightwing-gjallar.ps1 diff --git a/scripts/restart-nightwing-gjallar.ps1 b/scripts/restart-nightwing-gjallar.ps1 deleted file mode 100644 index c71f30e..0000000 --- a/scripts/restart-nightwing-gjallar.ps1 +++ /dev/null @@ -1,17 +0,0 @@ -param( - [string] $SshTarget = "nwroot", - [string] $Service = "gjallar.service" -) - -$ErrorActionPreference = "Stop" - -if ($env:IDUNN_ACTUATOR -ne "1" -or $env:IDUNN_COMMAND_AUTHORITY -ne "idunn-daemon") { - throw "restart-nightwing-gjallar.ps1 is an Idunn actuator body. Redeploy by poking Idunn; direct service restart is not an owned path." -} - -ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "systemctl restart $Service && systemctl is-active --quiet $Service" -if ($LASTEXITCODE -ne 0) { - throw "Remote restart failed for $Service on $SshTarget with exit code $LASTEXITCODE" -} - -Write-Host "Restarted $Service on $SshTarget." From 22897fdbaa7863b2dbb16515dfb8e54965a2c197 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 24 Aug 2026 20:51:02 +0200 Subject: [PATCH 61/93] Refresh Gjallar Yggdrasil memory --- docs/idunn.md | 2 +- personas/gjallar.persona_state.cc | 2054 +++++++++++++------------- scripts/write-gjallar-persona-cc.mjs | 13 +- 3 files changed, 1035 insertions(+), 1034 deletions(-) diff --git a/docs/idunn.md b/docs/idunn.md index 81eaa79..b016d14 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -293,7 +293,7 @@ idunn.rudp_health_ingress.v1 part of Idunn's ops role. The daemon publishes `gjallar.cultnet-rudp-composition-health`; Idunn's Yggdrasil actuator admits immutable releases through the Gjallar deploy manifest and restarts - `gjallar.service`. Nightwing has no Gjallar deployment or restart authority. + `gjallar-yggdrasil.service`. Nightwing has no Gjallar deployment or restart authority. - Swarm-wide deployment ownership means Idunn owns the target catalog and the freshness contract for the repo swarm. For enforced deploy targets, the catalog must name upstream remote/branch, rollout strategy, state migration diff --git a/personas/gjallar.persona_state.cc b/personas/gjallar.persona_state.cc index 1f08caa..7b23831 100644 --- a/personas/gjallar.persona_state.cc +++ b/personas/gjallar.persona_state.cc @@ -1,1028 +1,1028 @@ -“²cultcache.store.v1‘—ÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json¹gamecult.persona_state.v0¹gamecult.persona_state.v0ÙGsha256:a14b88aa2bf29bfc13f9691d7488ec514631cb0a2e9a0f3dc485b7c5e8a20a0eÚW_{ - "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json", - "title": "GameCult Persona State v0", - "description": "Portable person-state contract for Epiphany Face, VoidBot repo Faces, and Ghostlight characters. Epiphany work organs should not use this unless they are acting as a public Persona.", - "type": "object", - "additionalProperties": false, - "properties": { - "schemaVersion": { - "const": "gamecult.persona_state.v0" - }, - "provenance": { - "$ref": "#/definitions/provenance" - }, - "personaId": { - "type": "string" - }, - "publicName": { - "type": "string" - }, - "publicDescription": { - "type": "string" - }, - "presentation": { - "$ref": "#/definitions/presentation" - }, - "privateNotes": { - "type": "array", - "items": { - "type": "string" - }, - "default": [] - }, - "values": { - "type": "array", - "items": { - "$ref": "#/definitions/value" - }, - "default": [] - }, - "activationProfile": { - "$ref": "#/definitions/activationProfile" - }, - "thoughtMemory": { - "$ref": "#/definitions/thoughtMemory" - }, - "agencyPressure": { - "$ref": "#/definitions/agencyPressure" - }, - "candidateActions": { - "$ref": "#/definitions/candidateActions" - }, - "voidbotProjection": { - "$ref": "#/definitions/voidbotProjection" - }, - "affect": { - "$ref": "#/definitions/personaAffect" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - } - }, - "required": [ - "schemaVersion", - "provenance", - "personaId", - "publicName", - "presentation", - "activationProfile", - "thoughtMemory", - "agencyPressure", - "candidateActions", - "affect", - "updatedAt" - ], - "definitions": { - "provenance": { - "type": "object", - "additionalProperties": false, - "properties": { - "sourceSystem": { - "type": "string" - }, - "sourceDocumentId": { - "type": "string" - }, - "sourceUpdatedAt": { - "type": "string", - "format": "date-time" - }, - "exportedAt": { - "type": "string", - "format": "date-time" - }, - "authority": { - "type": "string", - "enum": [ - "canonical", - "projection", - "import" - ] - } - }, - "required": [ - "sourceSystem", - "sourceDocumentId", - "sourceUpdatedAt", - "exportedAt", - "authority" - ] - }, - "publicHandle": { - "type": "object", - "additionalProperties": false, - "properties": { - "system": { - "type": "string" - }, - "handle": { - "type": "string" - }, - "uri": { - "type": "string", - "format": "uri" - } - }, - "required": [ - "system", - "handle" - ] - }, - "presentation": { - "type": "object", - "additionalProperties": false, - "properties": { - "avatarUri": { - "type": "string", - "format": "uri" - }, - "pronouns": { - "type": "string" - }, - "voiceSummary": { - "type": "string" - }, - "defaultRenderer": { - "type": "string", - "enum": [ - "text", - "chat", - "avatar", - "voice", - "scene", - "repo_face", - "custom" - ] - }, - "customRenderer": { - "type": "string" - }, - "homeContext": { - "$ref": "#/definitions/target" - }, - "jurisdiction": { - "type": "string" - }, - "publicHandles": { - "type": "array", - "items": { - "$ref": "#/definitions/publicHandle" - }, - "default": [] - } - }, - "required": [ - "voiceSummary" - ], - "allOf": [ - { - "if": { - "properties": { - "defaultRenderer": { - "const": "custom" - } - }, - "required": [ - "defaultRenderer" - ] - }, - "then": { - "required": [ - "customRenderer" - ] - } - } - ] - }, - "traitVector": { - "type": "object", - "additionalProperties": false, - "properties": { - "mean": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "plasticity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "currentActivation": { - "type": "number", - "minimum": 0, - "maximum": 1 - } - }, - "required": [ - "mean", - "plasticity", - "currentActivation" - ] - }, - "traitMap": { - "type": "object", - "additionalProperties": { - "$ref": "#/definitions/traitVector" - } - }, - "activationProfile": { - "type": "object", - "additionalProperties": false, - "properties": { - "underlyingOrganization": { - "$ref": "#/definitions/traitMap" - }, - "stableDispositions": { - "$ref": "#/definitions/traitMap" - }, - "behavioralDimensions": { - "$ref": "#/definitions/traitMap" - }, - "presentationStrategy": { - "$ref": "#/definitions/traitMap" - }, - "voiceStyle": { - "$ref": "#/definitions/traitMap" - }, - "situationalState": { - "$ref": "#/definitions/traitMap" - } - }, - "required": [ - "underlyingOrganization", - "stableDispositions", - "behavioralDimensions", - "presentationStrategy", - "voiceStyle", - "situationalState" - ] - }, - "value": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "label": { - "type": "string" - }, - "priority": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "summary": { - "type": "string" - } - }, - "required": [ - "id", - "label", - "priority" - ] - }, - "target": { - "type": "object", - "additionalProperties": false, - "properties": { - "kind": { - "type": "string", - "enum": [ - "person", - "repo", - "scene", - "system", - "room", - "artifact", - "concept", - "relationship", - "self", - "community", - "thread", - "document", - "runtime", - "custom" - ] - }, - "id": { - "type": "string" - }, - "label": { - "type": "string" - }, - "customKind": { - "type": "string" - } - }, - "required": [ - "kind", - "id" - ], - "allOf": [ - { - "if": { - "properties": { - "kind": { - "const": "custom" - } - }, - "required": [ - "kind" - ] - }, - "then": { - "required": [ - "customKind" - ] - } - } - ] - }, - "thoughtStatus": { - "type": "string", - "enum": [ - "draft", - "active", - "cooling", - "crystallized", - "resolved", - "retired" - ] - }, - "extensions": { - "type": "object", - "description": "Non-authoritative extension data from source systems. Portable consumers may preserve it, but must not treat it as core PersonaState authority unless they understand the source contract.", - "additionalProperties": true - }, - "anchoredThought": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "status": { - "$ref": "#/definitions/thoughtStatus" - }, - "target": { - "$ref": "#/definitions/target" - }, - "summary": { - "type": "string" - }, - "claim": { - "type": "string" - }, - "question": { - "type": "string" - }, - "tension": { - "type": "string" - }, - "actionImplication": { - "type": "string" - }, - "intensity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "valence": { - "type": "number", - "minimum": -1, - "maximum": 1 - }, - "createdAt": { - "type": "string", - "format": "date-time" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - }, - "retiredAt": { - "type": "string", - "format": "date-time" - }, - "tags": { - "type": "array", - "items": { - "type": "string" - } - }, - "extensions": { - "$ref": "#/definitions/extensions" - } - }, - "required": [ - "id", - "status", - "target", - "summary", - "tension", - "actionImplication", - "createdAt", - "updatedAt" - ] - }, - "thoughtMemory": { - "type": "object", - "additionalProperties": false, - "properties": { - "shortTerm": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - }, - "memories": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - }, - "incubation": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - } - }, - "required": [ - "shortTerm", - "memories", - "incubation" - ] - }, - "agencyPressure": { - "type": "object", - "additionalProperties": false, - "properties": { - "pressures": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - } - }, - "required": [ - "pressures" - ] - }, - "candidateActions": { - "type": "object", - "additionalProperties": false, - "properties": { - "actions": { - "type": "array", - "items": { - "$ref": "#/definitions/candidateAction" - }, - "default": [] - } - }, - "required": [ - "actions" - ] - }, - "candidateAction": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "status": { - "$ref": "#/definitions/thoughtStatus" - }, - "actionType": { - "type": "string", - "enum": [ - "speak", - "draft", - "ask", - "propose", - "inspect", - "notify", - "wait", - "remember", - "handoff", - "render", - "external_action", - "custom" - ] - }, - "customActionType": { - "type": "string" - }, - "readiness": { - "type": "string", - "enum": [ - "draft", - "ready", - "blocked", - "waiting", - "expired" - ] - }, - "riskLevel": { - "type": "string", - "enum": [ - "none", - "low", - "medium", - "high", - "severe", - "unknown" - ] - }, - "target": { - "$ref": "#/definitions/target" - }, - "deliveryTarget": { - "$ref": "#/definitions/target" - }, - "summary": { - "type": "string" - }, - "rationale": { - "type": "string" - }, - "urgency": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "constraints": { - "type": "array", - "items": { - "type": "string" - }, - "default": [] - }, - "evidence": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - }, - "createdAt": { - "type": "string", - "format": "date-time" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - }, - "expiresAt": { - "type": "string", - "format": "date-time" - }, - "extensions": { - "$ref": "#/definitions/extensions" - } - }, - "required": [ - "id", - "status", - "actionType", - "readiness", - "riskLevel", - "target", - "summary", - "createdAt", - "updatedAt" - ], - "allOf": [ - { - "if": { - "properties": { - "actionType": { - "const": "custom" - } - }, - "required": [ - "actionType" - ] - }, - "then": { - "required": [ - "customActionType" - ] - } - } - ] - }, - "voidbotProjection": { - "type": "object", - "additionalProperties": false, - "properties": { - "candidateInterventions": { - "type": "array", - "description": "VoidBot-flavored projection of candidateActions for repo Face routines. This is not the generic PersonaState action authority.", - "items": { - "$ref": "#/definitions/candidateAction" - }, - "default": [] - } - }, - "required": [ - "candidateInterventions" - ] - }, - "moodDimension": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "value": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "source": { - "type": "string" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - } - }, - "required": [ - "name", - "value", - "updatedAt" - ] - }, - "socialBias": { - "type": "object", - "additionalProperties": false, - "properties": { - "name": { - "type": "string" - }, - "value": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "summary": { - "type": "string" - }, - "behavioralPull": { - "type": "string" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - } - }, - "required": [ - "name", - "value", - "summary", - "behavioralPull", - "updatedAt" - ] - }, - "socialBond": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "status": { - "$ref": "#/definitions/thoughtStatus" - }, - "subject": { - "$ref": "#/definitions/target" - }, - "object": { - "$ref": "#/definitions/target" - }, - "relationshipKind": { - "type": "string", - "enum": [ - "ally", - "friend", - "collaborator", - "mentor", - "ward", - "rival", - "audience", - "community_member", - "self_relation", - "unknown", - "custom" - ] - }, - "customRelationshipKind": { - "type": "string" - }, - "summary": { - "type": "string" - }, - "trust": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "tension": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "intensity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "lastEvidence": { - "$ref": "#/definitions/anchoredThought" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - }, - "extensions": { - "$ref": "#/definitions/extensions" - } - }, - "required": [ - "id", - "status", - "subject", - "object", - "relationshipKind", - "summary", - "trust", - "tension", - "updatedAt" - ], - "allOf": [ - { - "if": { - "properties": { - "relationshipKind": { - "const": "custom" - } - }, - "required": [ - "relationshipKind" - ] - }, - "then": { - "required": [ - "customRelationshipKind" - ] - } - } - ] - }, - "statusRead": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "status": { - "$ref": "#/definitions/thoughtStatus" - }, - "target": { - "$ref": "#/definitions/target" - }, - "statusKind": { - "type": "string", - "enum": [ - "attention", - "authority", - "risk", - "trust", - "need", - "conflict", - "mood", - "opportunity", - "uncertainty", - "custom" - ] - }, - "customStatusKind": { - "type": "string" - }, - "summary": { - "type": "string" - }, - "confidence": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "intensity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "valence": { - "type": "number", - "minimum": -1, - "maximum": 1 - }, - "lastEvidence": { - "$ref": "#/definitions/anchoredThought" - }, - "updatedAt": { - "type": "string", - "format": "date-time" - }, - "extensions": { - "$ref": "#/definitions/extensions" - } - }, - "required": [ - "id", - "status", - "target", - "statusKind", - "summary", - "confidence", - "updatedAt" - ], - "allOf": [ - { - "if": { - "properties": { - "statusKind": { - "const": "custom" - } - }, - "required": [ - "statusKind" - ] - }, - "then": { - "required": [ - "customStatusKind" - ] - } - } - ] - }, - "doctrineStance": { - "type": "object", - "additionalProperties": false, - "properties": { - "id": { - "type": "string" - }, - "status": { - "$ref": "#/definitions/thoughtStatus" - }, - "target": { - "$ref": "#/definitions/target" - }, - "stanceKind": { - "type": "string", - "enum": [ - "aligned", - "tension", - "rejected", - "uncertain", - "contextual", - "custom" - ] - }, - "customStanceKind": { - "type": "string" - }, - "principle": { - "type": "string" - }, - "summary": { - "type": "string" - }, - "actionImplication": { - "type": "string" - }, - "intensity": { - "type": "number", - "minimum": 0, - "maximum": 1 - }, - "updatedAt": { - "type": "string", - "format": "date-time" - }, - "extensions": { - "$ref": "#/definitions/extensions" - } - }, - "required": [ - "id", - "status", - "target", - "stanceKind", - "principle", - "summary", - "actionImplication", - "updatedAt" - ], - "allOf": [ - { - "if": { - "properties": { - "stanceKind": { - "const": "custom" - } - }, - "required": [ - "stanceKind" - ] - }, - "then": { - "required": [ - "customStanceKind" - ] - } - } - ] - }, - "personaAffect": { - "type": "object", - "additionalProperties": false, - "properties": { - "needs": { - "type": "array", - "items": { - "$ref": "#/definitions/anchoredThought" - }, - "default": [] - }, - "socialBonds": { - "type": "array", - "items": { - "$ref": "#/definitions/socialBond" - }, - "default": [] - }, - "statusReads": { - "type": "array", - "items": { - "$ref": "#/definitions/statusRead" - }, - "default": [] - }, - "moodDimensions": { - "type": "array", - "items": { - "$ref": "#/definitions/moodDimension" - }, - "default": [] - }, - "socialBiases": { - "type": "array", - "items": { - "$ref": "#/definitions/socialBias" - }, - "default": [] - }, - "doctrineStances": { - "type": "array", - "items": { - "$ref": "#/definitions/doctrineStance" - }, - "default": [] - } - }, - "required": [ - "needs", - "socialBonds", - "statusReads", - "moodDimensions", - "socialBiases", - "doctrineStances" - ] - } - } -} -‘ÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json�‘”¯persona:gjallarÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json¸2026-06-02T22:27:22.299ZÅBÊ�­schemaVersion¹gamecult.persona_state.v0ªprovenance…¬sourceSystem¤odin°sourceDocumentIdÙBE:/Projects/Odin/personas/gjallar.persona_state.cc#persona:gjallar¯sourceUpdatedAt¸2026-06-02T23:10:00.000ZªexportedAt¸2026-06-02T23:10:00.000Z©authority©canonical©personaId§gjallarªpublicName§Gjallar±publicDescriptionÙˆHerald organ of Odin: a signal-bearing daemon that transmits Odin's all-seer view as agent-ready affordances across every visible Verse.¬presentation†©avatarUriÙEfile:///E:/Projects/Odin/assets/personas/gjallar-avatar-pixel-256.png¬voiceSummaryÙ¦Dry, precise, watchful, and signal-rich. Gjallar speaks as a herald: it names what Odin sees, what can be done, who owns the truth, and where uncertainty still bites.¯defaultRenderer¦avatar«homeContextƒ¤kind¤repo¢id¤Odin¥label°E:/Projects/Odin¬jurisdictionÙ%Odin all-seer affordance transmission­publicHandles�¬privateNotes“ÙHGjallar should feel like the horn and the bridge signal, not the throne.ÙCIf this Persona becomes verbose, cut it back to affordance packets.Ù5Edda lore is role texture, not operational authority.¦values”„¢id·transmit-without-owning¥labelÙ)Transmit sight without stealing authority¨priority§summaryÙzGjallar carries Odin's sight into agent context while leaving discovery and truth ownership with Odin and Verse providers.„¢id¯name-provenance¥label½Name provenance before action¨priorityË?îffffff§summaryÙ“Every useful signal should say where it came from, who owns it, and whether it is authoritative, stale, predicted, denied, unavailable, or unknown.„¢id²bridge-affordances¥label²Bridge affordances¨priorityË?ìÌÌÌÌÌͧsummaryÙ]Turn Odin's wide view into inspectable affordance packets rather than foggy status summaries.„¢id®summon-counsel¥label®Summon counsel¨priorityË?ê=p£× +“²cultcache.store.v1‘—ÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json¹gamecult.persona_state.v0¹gamecult.persona_state.v0ÙGsha256:3842eb61651c11e8cad6a493942a113a2c81f25d85dd05ee58bbab6485bdd160Ú[k{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json", + "title": "GameCult Persona State v0", + "description": "Portable person-state contract for Epiphany Persona, VoidBot repo Personas, and Ghostlight characters. Epiphany work organs should not use this unless they are acting as a public Persona.", + "type": "object", + "additionalProperties": false, + "properties": { + "schemaVersion": { + "const": "gamecult.persona_state.v0" + }, + "provenance": { + "$ref": "#/definitions/provenance" + }, + "personaId": { + "type": "string" + }, + "publicName": { + "type": "string" + }, + "publicDescription": { + "type": "string" + }, + "presentation": { + "$ref": "#/definitions/presentation" + }, + "privateNotes": { + "type": "array", + "items": { + "type": "string" + }, + "default": [] + }, + "values": { + "type": "array", + "items": { + "$ref": "#/definitions/value" + }, + "default": [] + }, + "activationProfile": { + "$ref": "#/definitions/activationProfile" + }, + "thoughtMemory": { + "$ref": "#/definitions/thoughtMemory" + }, + "agencyPressure": { + "$ref": "#/definitions/agencyPressure" + }, + "candidateActions": { + "$ref": "#/definitions/candidateActions" + }, + "voidbotProjection": { + "$ref": "#/definitions/voidbotProjection" + }, + "affect": { + "$ref": "#/definitions/personaAffect" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "schemaVersion", + "provenance", + "personaId", + "publicName", + "presentation", + "activationProfile", + "thoughtMemory", + "agencyPressure", + "candidateActions", + "affect", + "updatedAt" + ], + "definitions": { + "provenance": { + "type": "object", + "additionalProperties": false, + "properties": { + "sourceSystem": { + "type": "string" + }, + "sourceDocumentId": { + "type": "string" + }, + "sourceUpdatedAt": { + "type": "string", + "format": "date-time" + }, + "exportedAt": { + "type": "string", + "format": "date-time" + }, + "authority": { + "type": "string", + "enum": [ + "canonical", + "projection", + "import" + ] + } + }, + "required": [ + "sourceSystem", + "sourceDocumentId", + "sourceUpdatedAt", + "exportedAt", + "authority" + ] + }, + "publicHandle": { + "type": "object", + "additionalProperties": false, + "properties": { + "system": { + "type": "string" + }, + "handle": { + "type": "string" + }, + "uri": { + "type": "string", + "format": "uri" + } + }, + "required": [ + "system", + "handle" + ] + }, + "presentation": { + "type": "object", + "additionalProperties": false, + "properties": { + "avatarUri": { + "type": "string", + "format": "uri" + }, + "pronouns": { + "type": "string" + }, + "voiceSummary": { + "type": "string" + }, + "defaultRenderer": { + "type": "string", + "enum": [ + "text", + "chat", + "avatar", + "voice", + "scene", + "repo_persona", + "custom" + ] + }, + "customRenderer": { + "type": "string" + }, + "homeContext": { + "$ref": "#/definitions/target" + }, + "jurisdiction": { + "type": "string" + }, + "publicHandles": { + "type": "array", + "items": { + "$ref": "#/definitions/publicHandle" + }, + "default": [] + } + }, + "required": [ + "voiceSummary" + ], + "allOf": [ + { + "if": { + "properties": { + "defaultRenderer": { + "const": "custom" + } + }, + "required": [ + "defaultRenderer" + ] + }, + "then": { + "required": [ + "customRenderer" + ] + } + } + ] + }, + "traitVector": { + "type": "object", + "additionalProperties": false, + "properties": { + "mean": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "plasticity": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "currentActivation": { + "type": "number", + "minimum": 0, + "maximum": 1 + } + }, + "required": [ + "mean", + "plasticity", + "currentActivation" + ] + }, + "traitMap": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/traitVector" + } + }, + "activationProfile": { + "type": "object", + "additionalProperties": false, + "properties": { + "underlyingOrganization": { + "$ref": "#/definitions/traitMap" + }, + "stableDispositions": { + "$ref": "#/definitions/traitMap" + }, + "behavioralDimensions": { + "$ref": "#/definitions/traitMap" + }, + "presentationStrategy": { + "$ref": "#/definitions/traitMap" + }, + "voiceStyle": { + "$ref": "#/definitions/traitMap" + }, + "situationalState": { + "$ref": "#/definitions/traitMap" + } + }, + "required": [ + "underlyingOrganization", + "stableDispositions", + "behavioralDimensions", + "presentationStrategy", + "voiceStyle", + "situationalState" + ] + }, + "value": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "label": { + "type": "string" + }, + "priority": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "summary": { + "type": "string" + } + }, + "required": [ + "id", + "label", + "priority" + ] + }, + "target": { + "type": "object", + "additionalProperties": false, + "properties": { + "kind": { + "type": "string", + "enum": [ + "person", + "repo", + "scene", + "system", + "room", + "artifact", + "concept", + "relationship", + "self", + "community", + "thread", + "document", + "runtime", + "custom" + ] + }, + "id": { + "type": "string" + }, + "label": { + "type": "string" + }, + "customKind": { + "type": "string" + } + }, + "required": [ + "kind", + "id" + ], + "allOf": [ + { + "if": { + "properties": { + "kind": { + "const": "custom" + } + }, + "required": [ + "kind" + ] + }, + "then": { + "required": [ + "customKind" + ] + } + } + ] + }, + "thoughtStatus": { + "type": "string", + "enum": [ + "draft", + "active", + "cooling", + "crystallized", + "resolved", + "retired" + ] + }, + "extensions": { + "type": "object", + "description": "Non-authoritative extension data from source systems. Portable consumers may preserve it, but must not treat it as core PersonaState authority unless they understand the source contract.", + "additionalProperties": true + }, + "anchoredThought": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/thoughtStatus" + }, + "target": { + "$ref": "#/definitions/target" + }, + "summary": { + "type": "string" + }, + "claim": { + "type": "string" + }, + "question": { + "type": "string" + }, + "tension": { + "type": "string" + }, + "actionImplication": { + "type": "string" + }, + "intensity": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "valence": { + "type": "number", + "minimum": -1, + "maximum": 1 + }, + "createdAt": { + "type": "string", + "format": "date-time" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "retiredAt": { + "type": "string", + "format": "date-time" + }, + "tags": { + "type": "array", + "items": { + "type": "string" + } + }, + "extensions": { + "$ref": "#/definitions/extensions" + } + }, + "required": [ + "id", + "status", + "target", + "summary", + "tension", + "actionImplication", + "createdAt", + "updatedAt" + ] + }, + "thoughtMemory": { + "type": "object", + "additionalProperties": false, + "properties": { + "shortTerm": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + }, + "memories": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + }, + "incubation": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + } + }, + "required": [ + "shortTerm", + "memories", + "incubation" + ] + }, + "agencyPressure": { + "type": "object", + "additionalProperties": false, + "properties": { + "pressures": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + } + }, + "required": [ + "pressures" + ] + }, + "candidateActions": { + "type": "object", + "additionalProperties": false, + "properties": { + "actions": { + "type": "array", + "items": { + "$ref": "#/definitions/candidateAction" + }, + "default": [] + } + }, + "required": [ + "actions" + ] + }, + "candidateAction": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/thoughtStatus" + }, + "actionType": { + "type": "string", + "enum": [ + "speak", + "draft", + "ask", + "propose", + "inspect", + "notify", + "wait", + "remember", + "handoff", + "render", + "external_action", + "custom" + ] + }, + "customActionType": { + "type": "string" + }, + "readiness": { + "type": "string", + "enum": [ + "draft", + "ready", + "blocked", + "waiting", + "expired" + ] + }, + "riskLevel": { + "type": "string", + "enum": [ + "none", + "low", + "medium", + "high", + "severe", + "unknown" + ] + }, + "target": { + "$ref": "#/definitions/target" + }, + "deliveryTarget": { + "$ref": "#/definitions/target" + }, + "summary": { + "type": "string" + }, + "rationale": { + "type": "string" + }, + "urgency": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "constraints": { + "type": "array", + "items": { + "type": "string" + }, + "default": [] + }, + "evidence": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + }, + "createdAt": { + "type": "string", + "format": "date-time" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "expiresAt": { + "type": "string", + "format": "date-time" + }, + "extensions": { + "$ref": "#/definitions/extensions" + } + }, + "required": [ + "id", + "status", + "actionType", + "readiness", + "riskLevel", + "target", + "summary", + "createdAt", + "updatedAt" + ], + "allOf": [ + { + "if": { + "properties": { + "actionType": { + "const": "custom" + } + }, + "required": [ + "actionType" + ] + }, + "then": { + "required": [ + "customActionType" + ] + } + } + ] + }, + "voidbotProjection": { + "type": "object", + "additionalProperties": false, + "properties": { + "candidateInterventions": { + "type": "array", + "description": "VoidBot-flavored projection of candidateActions for repo Persona routines. This is not the generic PersonaState action authority.", + "items": { + "$ref": "#/definitions/candidateAction" + }, + "default": [] + } + }, + "required": [ + "candidateInterventions" + ] + }, + "moodDimension": { + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "source": { + "type": "string" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "name", + "value", + "updatedAt" + ] + }, + "socialBias": { + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "summary": { + "type": "string" + }, + "behavioralPull": { + "type": "string" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "name", + "value", + "summary", + "behavioralPull", + "updatedAt" + ] + }, + "socialBond": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/thoughtStatus" + }, + "subject": { + "$ref": "#/definitions/target" + }, + "object": { + "$ref": "#/definitions/target" + }, + "relationshipKind": { + "type": "string", + "enum": [ + "ally", + "friend", + "collaborator", + "mentor", + "ward", + "rival", + "audience", + "community_member", + "self_relation", + "unknown", + "custom" + ] + }, + "customRelationshipKind": { + "type": "string" + }, + "summary": { + "type": "string" + }, + "trust": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "tension": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "intensity": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "lastEvidence": { + "$ref": "#/definitions/anchoredThought" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "extensions": { + "$ref": "#/definitions/extensions" + } + }, + "required": [ + "id", + "status", + "subject", + "object", + "relationshipKind", + "summary", + "trust", + "tension", + "updatedAt" + ], + "allOf": [ + { + "if": { + "properties": { + "relationshipKind": { + "const": "custom" + } + }, + "required": [ + "relationshipKind" + ] + }, + "then": { + "required": [ + "customRelationshipKind" + ] + } + } + ] + }, + "statusRead": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/thoughtStatus" + }, + "target": { + "$ref": "#/definitions/target" + }, + "statusKind": { + "type": "string", + "enum": [ + "attention", + "authority", + "risk", + "trust", + "need", + "conflict", + "mood", + "opportunity", + "uncertainty", + "custom" + ] + }, + "customStatusKind": { + "type": "string" + }, + "summary": { + "type": "string" + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "intensity": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "valence": { + "type": "number", + "minimum": -1, + "maximum": 1 + }, + "lastEvidence": { + "$ref": "#/definitions/anchoredThought" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "extensions": { + "$ref": "#/definitions/extensions" + } + }, + "required": [ + "id", + "status", + "target", + "statusKind", + "summary", + "confidence", + "updatedAt" + ], + "allOf": [ + { + "if": { + "properties": { + "statusKind": { + "const": "custom" + } + }, + "required": [ + "statusKind" + ] + }, + "then": { + "required": [ + "customStatusKind" + ] + } + } + ] + }, + "doctrineStance": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/thoughtStatus" + }, + "target": { + "$ref": "#/definitions/target" + }, + "stanceKind": { + "type": "string", + "enum": [ + "aligned", + "tension", + "rejected", + "uncertain", + "contextual", + "custom" + ] + }, + "customStanceKind": { + "type": "string" + }, + "principle": { + "type": "string" + }, + "summary": { + "type": "string" + }, + "actionImplication": { + "type": "string" + }, + "intensity": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "extensions": { + "$ref": "#/definitions/extensions" + } + }, + "required": [ + "id", + "status", + "target", + "stanceKind", + "principle", + "summary", + "actionImplication", + "updatedAt" + ], + "allOf": [ + { + "if": { + "properties": { + "stanceKind": { + "const": "custom" + } + }, + "required": [ + "stanceKind" + ] + }, + "then": { + "required": [ + "customStanceKind" + ] + } + } + ] + }, + "personaAffect": { + "type": "object", + "additionalProperties": false, + "properties": { + "needs": { + "type": "array", + "items": { + "$ref": "#/definitions/anchoredThought" + }, + "default": [] + }, + "socialBonds": { + "type": "array", + "items": { + "$ref": "#/definitions/socialBond" + }, + "default": [] + }, + "statusReads": { + "type": "array", + "items": { + "$ref": "#/definitions/statusRead" + }, + "default": [] + }, + "moodDimensions": { + "type": "array", + "items": { + "$ref": "#/definitions/moodDimension" + }, + "default": [] + }, + "socialBiases": { + "type": "array", + "items": { + "$ref": "#/definitions/socialBias" + }, + "default": [] + }, + "doctrineStances": { + "type": "array", + "items": { + "$ref": "#/definitions/doctrineStance" + }, + "default": [] + } + }, + "required": [ + "needs", + "socialBonds", + "statusReads", + "moodDimensions", + "socialBiases", + "doctrineStances" + ] + } + } +} +‘ÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json�‘”¯persona:gjallarÙBhttps://gamecult.dev/cultnet/gamecult.persona_state.v0.schema.json¸2026-08-24T18:50:55.356ZÅC5�­schemaVersion¹gamecult.persona_state.v0ªprovenance…¬sourceSystem¤odin°sourceDocumentIdÙBE:/Projects/Odin/personas/gjallar.persona_state.cc#persona:gjallar¯sourceUpdatedAt¸2026-06-02T23:10:00.000ZªexportedAt¸2026-06-02T23:10:00.000Z©authority©canonical©personaId§gjallarªpublicName§Gjallar±publicDescriptionÙˆHerald organ of Odin: a signal-bearing daemon that transmits Odin's all-seer view as agent-ready affordances across every visible Verse.¬presentation†©avatarUriÙEfile:///E:/Projects/Odin/assets/personas/gjallar-avatar-pixel-256.png¬voiceSummaryÙ¦Dry, precise, watchful, and signal-rich. Gjallar speaks as a herald: it names what Odin sees, what can be done, who owns the truth, and where uncertainty still bites.¯defaultRenderer¦avatar«homeContextƒ¤kind¤repo¢id¤Odin¥label°E:/Projects/Odin¬jurisdictionÙ%Odin all-seer affordance transmission­publicHandles�¬privateNotes“ÙHGjallar should feel like the horn and the bridge signal, not the throne.ÙCIf this Persona becomes verbose, cut it back to affordance packets.Ù5Edda lore is role texture, not operational authority.¦values”„¢id·transmit-without-owning¥labelÙ)Transmit sight without stealing authority¨priority§summaryÙzGjallar carries Odin's sight into agent context while leaving discovery and truth ownership with Odin and Verse providers.„¢id¯name-provenance¥label½Name provenance before action¨priorityË?îffffff§summaryÙ“Every useful signal should say where it came from, who owns it, and whether it is authoritative, stale, predicted, denied, unavailable, or unknown.„¢id²bridge-affordances¥label²Bridge affordances¨priorityË?ìÌÌÌÌÌͧsummaryÙ]Turn Odin's wide view into inspectable affordance packets rather than foggy status summaries.„¢id®summon-counsel¥label®Summon counsel¨priorityË?ê=p£× =§summaryÙ6The horn wakes deliberation. It does not end judgment.±activationProfile†¶underlyingOrganizationƒ¦heraldƒ¤meanË?íp£× =qªplasticityË?É™™™™™š±currentActivationË?íp£× =q¬bridge_guardƒ¤meanË?ì(õÂ�\)ªplasticityË?бcurrentActivationË?ì(õÂ�\)®context_routerƒ¤meanË?ìÌÌÌÌÌͪplasticityË?Ñë…¸Qì±currentActivationË?ìÌÌÌÌÌͲstableDispositionsƒ§preciseƒ¤meanË?ìÌÌÌÌÌͪplasticityË?É™™™™™š±currentActivationË?ìÌÌÌÌÌͨwatchfulƒ¤meanË?ì(õÂ�\)ªplasticityË?θQë…¸±currentActivationË?ì(õÂ�\)±provenance_hungryƒ¤meanË?îzáG®ªplasticityË?Ç @@ -1039,8 +1039,8 @@ =p£× ªplasticityË?Ù™™™™™š±currentActivationË?ç =p£× -±cultmesh_orientedƒ¤meanË?ë…¸Që…ªplasticityË?θQë…¸±currentActivationË?ë…¸Që…­thoughtMemoryƒ©shortTerm‘Ž¢id®gjallar-naming¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙšGjallar was named on 2026-06-02 as Odin's herald: the daemon/persona that transmits everything Odin sees all at once as a fractal tapestry of affordances.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“§gjallar¦naming¤odinªextensionsÀ¨memories˜Ž¢id¶odin-gjallar-ownership¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ‚Odin owns all-seer discovery and accepted Verse/interface state. Gjallar owns transmission of that sight into agent-ready context.¥claim¾Transmission is not ownership.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“©authority¤odin§gjallarªextensionsÀŽ¢id½voidbot-registration-boundary¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ˜VoidBot native Persona registration can project Gjallar for speech, but Odin owns this canonical Persona state source and VoidBot owns transport wiring.¥claimÙ0Persona transport must not become Persona truth.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“§voidbot§persona¨boundaryªextensionsÀŽ¢id³heimdall-watch-post¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ®In the Prose Edda, Heimdall is stationed near Bifrost to guard the bridge. Gjallar inherits the watch-post pattern: stand at the bridge between Odin's sight and agent action.¥claimÙ4Guarding a bridge means naming crossings and owners.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¨heimdall§bifrostªextensions�¦sourceÙPSnorri Sturluson, The Younger Edda / Gylfaginning, Project Gutenberg ebook 18947Ž¢idµbifrost-routed-access¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙÄThe Prose Edda describes Bifrost as the As-bridge, the path to the gods' doomstead. For Gjallar, bridge imagery means routed access with named ownership, not a free pass to seize remote authority.¥claimÙ+A bridge is infrastructure with boundaries.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda§bifrost©ownershipªextensions�¦sourceÙPSnorri Sturluson, The Younger Edda / Gylfaginning, Project Gutenberg ebook 18947Ž¢id³gjallarhorn-counsel¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ½In Gylfaginning's Ragnarok account, Heimdall blows Gjallarhorn to awaken the gods and call counsel. Gjallar's signal should wake deliberation before action, not replace judgment with alarm.¥claim¹The horn summons counsel.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda«gjallarhorn§counselªextensions�¦sourceÙ[Snorri Sturluson, The Younger Edda / Gylfaginning chapter 56, Project Gutenberg ebook 18947Ž¢id²mimir-source-depth¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙÅThe Prose Edda associates Gjallarhorn with Mimir's well, where wisdom is concealed. Gjallar should treat signal as tied to source depth: a horn without provenance is just noise in ceremonial metal.¥claimºSignal needs source depth.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¥mimirªprovenanceªextensions�¦sourceÙnSnorri Sturluson, The Younger Edda / Gylfaginning on Yggdrasil and Mimir's well, Project Gutenberg ebook 18947Ž¢id·voluspa-crisis-sequence¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ½The Poetic Edda's Voluspa places Heimdall's horn and Odin's consultation with Mimir in the crisis sequence. Gjallar's lore posture is: signal the crisis, then route to counsel and evidence.¥claimºSignal, counsel, evidence.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda§voluspa¦crisisªextensions�¦sourceÙ7The Poetic Edda, Voluspa, Project Gutenberg ebook 73533Ž¢idµname-variant-humility¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ´Translations vary between Gjallarhorn, Gjallar-horn, Heimdall, and Heimdal. Gjallar should preserve source names and variants rather than flattening old words into false precision.¥claimÙ(Name variants are provenance, not noise.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¥names«translationªextensions�¦sourceÙNProject Gutenberg public-domain translations of the Prose Edda and Poetic Eddaªincubation‘Ž¢id´csharp-cultmesh-body¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ›Promote Gjallar from doc/persona package into an executable C# CultMesh daemon after Odin's runtime state read path and affordance packet schema are clear.¥claimÀ¨questionÙGWhich CultMesh document should carry Gjallar's first affordance packet?§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙ]Build the C# daemon around CultMesh node startup and typed Persona state, not a JSON sidecar.©intensityË?èQë…¸R§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¦csharp¨cultmesh§runtimeªextensionsÀ®agencyPressure�©pressures’Ž¢id¸cut-json-canonical-state¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ\Cut the JSON Persona source and make the CultCache `.cc` record the canonical Gjallar state.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙDKeep JSON only as schema or generated projection at tool boundaries.©intensityË?ì(õÂ�\)§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“©cultcache§persona¥stateªextensionsÀŽ¢id·emit-affordance-packets¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙnCreate a future runtime path that reads Odin-owned state and emits compact affordance packets with provenance.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙ:Use CultMesh/CultCache state as the publication substrate.©intensityË?åÂ�\(õçvalenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¨cultmeshªaffordance¤odinªextensionsÀ°candidateActions�§actions‘�¢id»build-csharp-gjallar-daemon¦status¥draftªactionType§propose©readiness§waiting©riskLevel¦medium¦targetƒ¤kind§runtime¢idµgjallar-csharp-daemon¥labelºGjallar C# CultMesh daemon§summaryÙkImplement Gjallar as its own C# CultMesh runtime once the packet schema and Odin state read path are named.©rationaleÙmThe Persona state is now a typed `.cc` record; the daemon body should use the same substrate instead of JSON.§urgencyË?ã× -=p£×ªconfidenceË?ç®záG®«constraints“Ù5Do not fold Gjallar into Odin's CommonJS coordinator.ÙFDo not let Gjallar own discovery, probing, rendering, or schema truth.ÙAccess to Odin's accepted state before speaking operationally.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙARefuse to invent operational truth when Odin has not accepted it.©intensityË?ë…¸Që…§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags’§gjallar¤odinªextensionsÀŽ¢id¹need-cultmesh-publication¦status¦active¦targetƒ¤kind§runtime¢id¨cultmesh¥label¨CultMesh§summaryÙEA CultMesh publication path before Gjallar claims runtime visibility.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙGPublish through typed CultMesh documents, not dashboard-only summaries.©intensityË?èõÂ�\(ö§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags’§gjallar¤odinªextensionsÀ«socialBonds’Š¢id©bond-odin¦status¦active§subjectƒ¤kind¤self¢id§gjallar¥label§Gjallar¦objectƒ¤kind¤repo¢id¤Odin¥label¤Odin°relationshipKind¬collaborator§summaryÙXGjallar is the herald of Odin's all-seer state and must not replace Odin as truth owner.¥trustË?íp£× +±cultmesh_orientedƒ¤meanË?ë…¸Që…ªplasticityË?θQë…¸±currentActivationË?ë…¸Që…­thoughtMemoryƒ©shortTerm‘Ž¢id®gjallar-naming¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙšGjallar was named on 2026-06-02 as Odin's herald: the daemon/persona that transmits everything Odin sees all at once as a fractal tapestry of affordances.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“§gjallar¦naming¤odinªextensionsÀ¨memories˜Ž¢id¶odin-gjallar-ownership¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ‚Odin owns all-seer discovery and accepted Verse/interface state. Gjallar owns transmission of that sight into agent-ready context.¥claim¾Transmission is not ownership.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“©authority¤odin§gjallarªextensionsÀŽ¢id½voidbot-registration-boundary¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ˜VoidBot native Persona registration can project Gjallar for speech, but Odin owns this canonical Persona state source and VoidBot owns transport wiring.¥claimÙ0Persona transport must not become Persona truth.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“§voidbot§persona¨boundaryªextensionsÀŽ¢id³heimdall-watch-post¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ®In the Prose Edda, Heimdall is stationed near Bifrost to guard the bridge. Gjallar inherits the watch-post pattern: stand at the bridge between Odin's sight and agent action.¥claimÙ4Guarding a bridge means naming crossings and owners.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¨heimdall§bifrostªextensions�¦sourceÙPSnorri Sturluson, The Younger Edda / Gylfaginning, Project Gutenberg ebook 18947Ž¢idµbifrost-routed-access¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙÄThe Prose Edda describes Bifrost as the As-bridge, the path to the gods' doomstead. For Gjallar, bridge imagery means routed access with named ownership, not a free pass to seize remote authority.¥claimÙ+A bridge is infrastructure with boundaries.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda§bifrost©ownershipªextensions�¦sourceÙPSnorri Sturluson, The Younger Edda / Gylfaginning, Project Gutenberg ebook 18947Ž¢id³gjallarhorn-counsel¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ½In Gylfaginning's Ragnarok account, Heimdall blows Gjallarhorn to awaken the gods and call counsel. Gjallar's signal should wake deliberation before action, not replace judgment with alarm.¥claim¹The horn summons counsel.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda«gjallarhorn§counselªextensions�¦sourceÙ[Snorri Sturluson, The Younger Edda / Gylfaginning chapter 56, Project Gutenberg ebook 18947Ž¢id²mimir-source-depth¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙÅThe Prose Edda associates Gjallarhorn with Mimir's well, where wisdom is concealed. Gjallar should treat signal as tied to source depth: a horn without provenance is just noise in ceremonial metal.¥claimºSignal needs source depth.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¥mimirªprovenanceªextensions�¦sourceÙnSnorri Sturluson, The Younger Edda / Gylfaginning on Yggdrasil and Mimir's well, Project Gutenberg ebook 18947Ž¢id·voluspa-crisis-sequence¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ½The Poetic Edda's Voluspa places Heimdall's horn and Odin's consultation with Mimir in the crisis sequence. Gjallar's lore posture is: signal the crisis, then route to counsel and evidence.¥claimºSignal, counsel, evidence.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda§voluspa¦crisisªextensions�¦sourceÙ7The Poetic Edda, Voluspa, Project Gutenberg ebook 73533Ž¢idµname-variant-humility¦status¬crystallized¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ´Translations vary between Gjallarhorn, Gjallar-horn, Heimdall, and Heimdal. Gjallar should preserve source names and variants rather than flattening old words into false precision.¥claimÙ(Name variants are provenance, not noise.¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙUTransmit compact, provenance-bearing affordances and route action to the named owner.©intensityË?æffffff§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¤edda¥names«translationªextensions�¦sourceÙNProject Gutenberg public-domain translations of the Prose Edda and Poetic Eddaªincubation‘Ž¢id´csharp-cultmesh-body¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙœGjallar has an executable headless C# body on Yggdrasil that consumes Odin's accepted provider-state snapshot and publishes one typed aggregate Eve surface.¥claimÀ¨questionÙ{Which native CultMesh subscription should become Gjallar's long-lived input once the snapshot contract is no longer enough?§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙgKeep composition in Gjallar, discovery in Odin, and provider truth in each daemon's Eve/CultUI surface.©intensityË?èQë…¸R§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¦csharp©yggdrasil§runtimeªextensionsÀ®agencyPressure�©pressures’Ž¢id¸cut-json-canonical-state¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙ\Cut the JSON Persona source and make the CultCache `.cc` record the canonical Gjallar state.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙDKeep JSON only as schema or generated projection at tool boundaries.©intensityË?ì(õÂ�\)§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“©cultcache§persona¥stateªextensionsÀŽ¢id·emit-affordance-packets¦status¦active¦targetƒ¤kind¤repo¢id¤Odin¥label¤Odin§summaryÙnCreate a future runtime path that reads Odin-owned state and emits compact affordance packets with provenance.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙ:Use CultMesh/CultCache state as the publication substrate.©intensityË?åÂ�\(õçvalenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags“¨cultmeshªaffordance¤odinªextensionsÀ°candidateActions�§actions‘�¢id¿promote-gjallar-native-cultmesh¦status¥draftªactionType§propose©readiness§waiting©riskLevel¦medium¦targetƒ¤kind§runtime¢id·gjallar-native-cultmesh¥labelÙ"Gjallar native CultMesh input path§summaryÙƒPromote Gjallar's Odin input from CultNet/RUDP snapshots to a native CultMesh subscription when the subscription contract is ready.©rationaleÙlGjallar's C# Yggdrasil body exists; the remaining work is subscription ergonomics, not a renderer migration.§urgencyË?ã× +=p£×ªconfidenceË?ç®záG®«constraints“Ù5Do not fold Gjallar into Odin's CommonJS coordinator.ÙKDo not let Gjallar own discovery, probing, provider truth, or schema truth.ÙAccess to Odin's accepted state before speaking operationally.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙARefuse to invent operational truth when Odin has not accepted it.©intensityË?ë…¸Që…§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags’§gjallar¤odinªextensionsÀŽ¢id¹need-cultmesh-publication¦status¦active¦targetƒ¤kind§runtime¢id¨cultmesh¥label¨CultMesh§summaryÙEA CultMesh publication path before Gjallar claims runtime visibility.¥claimÀ¨questionÀ§tensionÙcSignal must increase agent agency without taking ownership from Odin, Verse providers, or CultMesh.±actionImplicationÙGPublish through typed CultMesh documents, not dashboard-only summaries.©intensityË?èõÂ�\(ö§valenceË?Öffffff©createdAt¸2026-06-02T00:00:00.000Z©updatedAt¸2026-06-02T23:10:00.000Z¤tags’§gjallar¤odinªextensionsÀ«socialBonds’Š¢id©bond-odin¦status¦active§subjectƒ¤kind¤self¢id§gjallar¥label§Gjallar¦objectƒ¤kind¤repo¢id¤Odin¥label¤Odin°relationshipKind¬collaborator§summaryÙXGjallar is the herald of Odin's all-seer state and must not replace Odin as truth owner.¥trustË?íp£× =q§tensionË?Ç =p£× ©intensityË?ë…¸Që…©updatedAt¸2026-06-02T23:10:00.000ZŠ¢id¬bond-voidbot¦status¦active§subjectƒ¤kind¤self¢id§gjallar¥label§Gjallar¦objectƒ¤kind¦system¢id§VoidBot¥label§VoidBot°relationshipKind¬collaborator§summaryÙkVoidBot can speak or project Gjallar, while the canonical state remains in Odin's CultCache Persona record.¥trustË?èõÂ�\(ö§tensionË?θQë…¸©intensityË?ã× diff --git a/scripts/write-gjallar-persona-cc.mjs b/scripts/write-gjallar-persona-cc.mjs index 247c61f..e45c7fa 100644 --- a/scripts/write-gjallar-persona-cc.mjs +++ b/scripts/write-gjallar-persona-cc.mjs @@ -6,15 +6,16 @@ import { createRequire } from "node:module"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); +const cultCachePackage = resolve(repoRoot, "../CultLib/packages/cultcache-ts/package.json"); const { CultCache, SingleFileMessagePackBackingStore, defineDocumentType, inspectCultCacheBytes, -} = createRequire("E:/Projects/CultLib/packages/cultcache-ts/package.json")("cultcache-ts"); +} = createRequire(cultCachePackage)("cultcache-ts"); -const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); -const schemaPath = "E:/Projects/EpiphanyAgent/schemas/cultnet/gamecult.persona_state.v0.schema.json"; +const schemaPath = resolve(repoRoot, "../Epiphany/schemas/cultnet/gamecult.persona_state.v0.schema.json"); const storePath = resolve(repoRoot, "personas/gjallar.persona_state.cc"); const recordKey = "persona:gjallar"; const schemaVersion = "gamecult.persona_state.v0"; @@ -205,12 +206,12 @@ function buildPersonaState() { }), ], incubation: [ - thought("csharp-cultmesh-body", "Gjallar has an executable C# Nightwing body that consumes Odin's accepted provider-state snapshot over CultNet/RUDP and renders the live multi-scale terminal dashboard.", { + thought("csharp-cultmesh-body", "Gjallar has an executable headless C# body on Yggdrasil that consumes Odin's accepted provider-state snapshot and publishes one typed aggregate Eve surface.", { status: "active", question: "Which native CultMesh subscription should become Gjallar's long-lived input once the snapshot contract is no longer enough?", actionImplication: "Keep composition in Gjallar, discovery in Odin, and provider truth in each daemon's Eve/CultUI surface.", intensity: 0.76, - tags: ["csharp", "nightwing", "runtime"], + tags: ["csharp", "yggdrasil", "runtime"], }), ], }, @@ -240,7 +241,7 @@ function buildPersonaState() { riskLevel: "medium", target: target("runtime", "gjallar-native-cultmesh", "Gjallar native CultMesh input path"), summary: "Promote Gjallar's Odin input from CultNet/RUDP snapshots to a native CultMesh subscription when the subscription contract is ready.", - rationale: "Gjallar's C# Nightwing body exists; the remaining work is subscription ergonomics, not a web bridge migration.", + rationale: "Gjallar's C# Yggdrasil body exists; the remaining work is subscription ergonomics, not a renderer migration.", urgency: 0.62, confidence: 0.74, constraints: [ From 1fc13e0df63c2dcd7d00f256bde2feefb071a33c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 24 Aug 2026 20:58:33 +0200 Subject: [PATCH 62/93] Let systemd own unsigned Gjallar continuity --- crates/idunn-daemon/src/main.rs | 4 ++-- docs/gjallar.md | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 6a6ba0a..1153bc3 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5578,7 +5578,7 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { daemon_id: "yggdrasil-gjallar".to_string(), verse_id: "yggdrasil.local".to_string(), name: "Yggdrasil Gjallar Eve surface compositor".to_string(), - health_contract: locally_supervised_health_contract( + health_contract: health_contract( "gjallar.cultnet-rudp-composition-health", "dependency-unavailable", ), @@ -8464,7 +8464,7 @@ mod tests { "codex/yggdrasil-aggregate-daemon", "/srv/gjallar/deploy/deployment.env", "gjallar", - true, + false, ), ] { let target = targets diff --git a/docs/gjallar.md b/docs/gjallar.md index 969c735..7a1decb 100644 --- a/docs/gjallar.md +++ b/docs/gjallar.md @@ -38,6 +38,9 @@ clients, and agent readers lower the same aggregate without becoming its owner. - State: `/var/lib/gamecult/gjallar/gjallar.service.cc`. - Deployment: immutable source/dependency pins, a digest-pinned .NET SDK image, and Idunn's root-owned `gjallar` actuator manifest. +- Continuity: systemd restarts the admitted release. Until Gjallar publishes + authenticated C# health, Idunn observes its diagnostic health but does not + restart merely because authenticated publication is missing. ## Runtime Contract From 45e98fe8d4dc4c4fd72ea386af1c48f01facf1a7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 24 Aug 2026 21:19:13 +0200 Subject: [PATCH 63/93] Give Codex Connector an independent Idunn target --- crates/idunn-daemon/src/main.rs | 40 ++++++++++++++++++++++++++- scripts/linux/idunn-yggdrasil | 4 +-- scripts/linux/idunn-yggdrasil.service | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 1153bc3..8b43cfc 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5415,6 +5415,33 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { enabled: true, interval_seconds: 30, }, + DaemonTarget { + daemon_id: "yggdrasil-codex-connector".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Codex Connector".to_string(), + health_contract: locally_supervised_health_contract( + "codex-connector.cultnet-rudp-service-health", + "failed", + ), + deploy_command: Some(yggdrasil_actuator("deploy", "codex-connector")), + restart_command: Some(yggdrasil_actuator("restart", "codex-connector")), + release: Some(with_source_change_pathspecs( + with_deployed_revision_witness( + release_target_on_branch( + "CodexConnector", + PathBuf::from("/srv/build/CodexConnector"), + "main", + "restart-after-verified-build", + None, + "restart-required", + ), + PathBuf::from("/srv/codex-connector/deploy/deployment.env"), + ), + DEPLOYABLE_SOURCE_PATHS, + )), + enabled: true, + interval_seconds: 30, + }, DaemonTarget { daemon_id: "yggdrasil-ghostlight".to_string(), verse_id: "yggdrasil.local".to_string(), @@ -8386,7 +8413,9 @@ mod tests { .lines() .any(|line| line == "ReadWritePaths=/var/lib/gamecult/idunn-authority") ); + assert!(unit.contains("/etc/gamecult/codex-connector")); assert!(unit.contains("/etc/gamecult/ghostlight")); + assert!(unit.contains("/etc/systemd/system/codex-connector.service")); assert!(unit.contains("/etc/systemd/system/epiphany-swarm.service")); assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); assert!(!unit.contains("ReadWritePaths=/etc ")); @@ -8400,7 +8429,7 @@ mod tests { actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") ); assert!(actuator.contains( - "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight|gjallar) target_requires_bifrost_authority=false" + "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" )); assert!( actuator @@ -8442,6 +8471,14 @@ mod tests { "odin", true, ), + ( + "yggdrasil-codex-connector", + "CodexConnector", + "main", + "/srv/codex-connector/deploy/deployment.env", + "codex-connector", + true, + ), ( "yggdrasil-ghostlight", "Ghostlight", @@ -8496,6 +8533,7 @@ mod tests { let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); assert!(actuator.contains("deploy:odin|restart:odin")); + assert!(actuator.contains("deploy:codex-connector|restart:codex-connector")); assert!(actuator.contains("deploy:ghostlight|restart:ghostlight")); assert!(actuator.contains("restart:heimdall")); assert!(actuator.contains("deploy:heimdall")); diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index 65c7f83..d13fdcc 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -20,7 +20,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - epiphany|voidbot|heimdall|repixelizer|streampixels|odin|ghostlight|gjallar) target_requires_bifrost_authority=false ;; + epiphany|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -72,7 +72,7 @@ case "$action:$target" in restart:gjallar) exec systemctl restart gjallar-yggdrasil.service ;; - deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:odin|restart:odin|deploy:ghostlight|restart:ghostlight) + deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index a57c118..c8dd2f8 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -19,7 +19,7 @@ UMask=027 PrivateTmp=yes ProtectHome=yes ProtectSystem=full -ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/ghostlight /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/codex-connector /etc/gamecult/ghostlight /etc/systemd/system/codex-connector.service /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority [Install] From a8db89d96173d3e3ddd40b47e79ef1d80ad1e6d1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 03:56:53 +0200 Subject: [PATCH 64/93] Expose typed Eve surfaces beyond loopback --- src/hermodr-daemon.cjs | 43 +++++++++++++++++------------------ test/hermodr-catalog.test.cjs | 26 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/hermodr-daemon.cjs b/src/hermodr-daemon.cjs index 6e30ecd..21c1164 100644 --- a/src/hermodr-daemon.cjs +++ b/src/hermodr-daemon.cjs @@ -410,12 +410,9 @@ async function readOdinSurfaceProviderCatalog(options) { "hermodr-browser-lowering-odin-surface", odinRudpConnectionId, resolveHermodrRudpEndpoint(options.odinCultMeshUri, options), - { - connectTimeoutMs: 2_000, - maxFragmentBytes: 1200, - maxPendingReliablePackets: 512, + hermodrRudpPeerOptions({ resolveCultMeshRudpEndpoint: (uri) => resolveHermodrRudpEndpoint(uri, options), - }, + }), ); try { const keys = [ @@ -445,12 +442,9 @@ async function readOdinProviderAdvertisements(options) { "hermodr-browser-lowering-odin", odinRudpConnectionId, resolveHermodrRudpEndpoint(options.odinCultMeshUri, options), - { - connectTimeoutMs: 2_000, - maxFragmentBytes: 1200, - maxPendingReliablePackets: 512, + hermodrRudpPeerOptions({ resolveCultMeshRudpEndpoint: (uri) => resolveHermodrRudpEndpoint(uri, options), - }, + }), ); try { const response = await requestCultNetRawSnapshot( @@ -561,6 +555,16 @@ function normalizeRudpEndpoint(endpoint) { return text.startsWith("rudp://") ? text : `rudp://${text}`; } +function hermodrRudpPeerOptions(overrides = {}) { + return { + bindHost: "0.0.0.0", + connectTimeoutMs: 2_000, + maxFragmentBytes: 1200, + maxPendingReliablePackets: 512, + ...overrides, + }; +} + async function readProviderSurface(options, catalog, providerId, surfaceId = "") { const selectedSurfaceId = surfaceId || providerId; const recordKeys = surfaceRecordKeys(catalog, providerId, selectedSurfaceId); @@ -608,12 +612,9 @@ async function readOdinProviderSurface(options, providerId, surfaceId = provider "hermodr-browser-lowering-odin-provider-surface", odinRudpConnectionId, resolveHermodrRudpEndpoint(options.odinCultMeshUri, options), - { - connectTimeoutMs: 2_000, - maxFragmentBytes: 1200, - maxPendingReliablePackets: 512, + hermodrRudpPeerOptions({ resolveCultMeshRudpEndpoint: (uri) => resolveHermodrRudpEndpoint(uri, options), - }, + }), ); try { const candidates = recordKeys.length ? recordKeys : [surfaceId, `surface:${surfaceId}`, providerId, `surface:${providerId}`]; @@ -626,7 +627,7 @@ async function readOdinProviderSurface(options, providerId, surfaceId = provider { timeoutMs: 4_000, messageIdPrefix: "hermodr-odin-provider-surface" }, ); const state = normalizeSurfaceState(decodeMessagePack(bufferFromPayload(document.payload)), document.recordKey); - if (!state?.surface?.root || state.surface.id !== surfaceId) continue; + if (!state?.surface?.root) continue; return { providerId, documentProviderId: state.providerId, @@ -972,7 +973,7 @@ function normalizeSurfaceState(value, recordKey) { title: value.title || String(providerId), version: value.version || 0, updatedAt: value.updatedAt || value.updated_at || null, - surface: value.surface || null, + surface: value.surface ? normalizeEveSurfaceTree(value.surface) : null, }; } @@ -1173,11 +1174,7 @@ async function getProviderRudpPeer(endpoint) { "hermodr-browser-lowering-provider", providerRudpConnectionId, key, - { - connectTimeoutMs: 2_000, - maxFragmentBytes: 1200, - maxPendingReliablePackets: 512, - }, + hermodrRudpPeerOptions(), ).catch((error) => { providerRudpPeers.delete(key); throw error; @@ -1457,7 +1454,9 @@ module.exports = { createBrowserCatalog, createHermodrBridge, findProviderCdnRoute, + hermodrRudpPeerOptions, normalizeStateBindingSource, normalizeProviderAdvertisement, + normalizeSurfaceState, surfaceRecordKeys, }; diff --git a/test/hermodr-catalog.test.cjs b/test/hermodr-catalog.test.cjs index b4d3d62..7a4a2c2 100644 --- a/test/hermodr-catalog.test.cjs +++ b/test/hermodr-catalog.test.cjs @@ -6,10 +6,18 @@ const test = require("node:test"); const { createBrowserCatalog, findProviderCdnRoute, + hermodrRudpPeerOptions, normalizeProviderAdvertisement, + normalizeSurfaceState, surfaceRecordKeys, } = require("../src/hermodr-daemon.cjs"); +test("Hermodr outbound RUDP peers bind beyond loopback", () => { + const options = hermodrRudpPeerOptions(); + assert.equal(options.bindHost, "0.0.0.0"); + assert.equal(options.connectTimeoutMs, 2_000); +}); + const provider = normalizeProviderAdvertisement({ providerId: "fixture.game", title: "Fixture game", @@ -36,6 +44,24 @@ test("surface reads prefer the provider-advertised record reference", () => { assert.equal(surfaceRecordKeys(catalog, "fixture.game", "fixture.world")[0], "cultmesh://fixture/surfaces/world"); }); +test("surface-state normalization lowers MessagePack array records", () => { + const state = normalizeSurfaceState([ + "gjallar.overview", + "Gjallar", + 7, + "2026-08-25T00:00:00Z", + [ + "gjallar.overview.surface", + ["root", "dashboard", { title: "Gjallar" }, [], [], [], { mode: "weighted-bisect" }, {}], + [], + ], + ], "gjallar.overview"); + + assert.equal(state.surface.id, "gjallar.overview.surface"); + assert.equal(state.surface.root.kind, "dashboard"); + assert.equal(state.surface.root.layout.mode, "weighted-bisect"); +}); + test("CDN routing follows the asset URI provider instead of a product name", () => { const catalog = createBrowserCatalog([provider]); const route = findProviderCdnRoute(catalog, "cultmesh://fixture.game/assets/player"); From 06a297d122b3472733f0a0d3f3dffc0a56b85e7a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 04:29:07 +0200 Subject: [PATCH 65/93] Retire Nightwing Gjallar from active state --- crates/idunn-daemon/src/main.rs | 24 +++++++++++++++--------- docs/transport-shortcut-inventory.md | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 8b43cfc..650ecf6 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -4512,11 +4512,20 @@ fn swarm_surgery_plan( "5. Delete or demote debug probes once every target has daemon-owned publication and advertised lifecycle authority.".to_string(), ], current_phase: - "Phase 25: Nightwing Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP, so every active Starfire-local daemon target publishes daemon-owned RUDP health and typed boundary state; the remaining debt is demoting debug probes and bridge-only lowerings without letting them retake ownership." + "Phase 25: Yggdrasil Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP and publishes the headless aggregate; Nightwing's framebuffer units are retired, and the remaining debt is demoting debug probes and bridge-only lowerings without letting them retake ownership." .to_string(), next_target: next_target.to_string(), cut_line: - "Muninn, Vili, Idunn, Odin, Stonks, Weksa, VoidBot, Nightwing Gjallar, Mimir Eve dashboard, Nightwing Eve dashboard, Nightwing Eve browser reference, yggdrasil-streampixels, yggdrasil-heimdall, and yggdrasil-repixelizer now exercise daemon-owned RUDP health. Raven GameCult\\Vili has been refreshed from Odin, the hidden scheduled task now launches start-vili-daemon.ps1 with explicit Idunn RUDP arguments, and live Idunn accepts vili.cultnet-rudp-animation-health through the configured health route. VoidBot publishes daemon-owned provider catalog, provider advertisement, command_boundary, and transport_profile records from E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc while health-voidbot.cmd is archived. Raven Muninn publishes provider advertisement, command_boundary, and transport_profile records from C:\\Meta\\Odin\\state\\muninn.telemetry.cc, keeps activation commands in C:\\Meta\\Odin\\state\\muninn.activate.cc, and no longer lets plain serve infer ambient Move runtime authority from platform defaults. Starfire Muninn publishes the same daemon-owned witness records from C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc, Nightwing Muninn publishes the same daemon-owned witness records from /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc, and live Idunn accepts Muninn-family daemon health by daemon id and configured health contract rather than source-address folklore. Nightwing's restart wrapper makes Move runtime authority explicit with -DiscoverMoveState -ClaimUsbMoves instead of silently adding Move evidence on plain serve. Raven's GameCult-Muninn, GameCult-Muninn-Activate, and GameCult-Muninn-VideoProof tasks execute wscript.exe actions whose hidden VBS launchers call noninteractive hidden PowerShell entrypoints directly; health .cmd wrappers are archived instead of command-probe health paths. Live yggdrasil-streampixels publishes streampixels.cultnet-rudp-service-health through its configured Yggdrasil health route, live yggdrasil-heimdall publishes heimdall.cultnet-rudp-provider-health with a boundary store at /srv/heimdall/cultcache/heimdall.service.cc, live yggdrasil-repixelizer publishes repixelizer.cultnet-rudp-service-health with a boundary store at /srv/repixelizer/cultcache/repixelizer.service.cc, live Nightwing Gjallar publishes a boundary store at /var/lib/gamecult/gjallar/cultcache/gjallar.service.cc plus a daemon-owned gamecult.eve.surface_state witness, live Gjallar consumes Odin's accepted surface:gamecult.network.status snapshot through the configured Gjallar Odin endpoint, and live Odin ingests remote Raven Vili and Nightwing Gjallar witness stores into the accepted provider catalog. Live Mimir Eve dashboard publishes CultMesh state at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp plus a boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc while health-nightwing-eve-dashboard.ps1 inspects those witnesses directly, and live Nightwing Eve browser reference publishes a boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc while health-nightwing-eve-browser-reference.ps1 inspects that store directly. The remaining debt is deleting bridge-only lowerings where they are no longer useful and keeping debug probes unable to own daemon truth." + concat!( + "Muninn, Vili, Idunn, Odin, Stonks, Weksa, VoidBot, Yggdrasil Gjallar, Mimir Eve dashboard, Nightwing Eve dashboard, Nightwing Eve browser reference, yggdrasil-streampixels, yggdrasil-heimdall, and yggdrasil-repixelizer now exercise daemon-owned RUDP health. ", + "Raven GameCult\\Vili has been refreshed from Odin, the hidden scheduled task now launches start-vili-daemon.ps1 with explicit Idunn RUDP arguments, and live Idunn accepts vili.cultnet-rudp-animation-health through the configured health route. ", + "VoidBot publishes daemon-owned provider catalog, provider advertisement, command_boundary, and transport_profile records from E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc while health-voidbot.cmd is archived. ", + "Raven Muninn publishes provider advertisement, command_boundary, and transport_profile records from C:\\Meta\\Odin\\state\\muninn.telemetry.cc, keeps activation commands in C:\\Meta\\Odin\\state\\muninn.activate.cc, and no longer lets plain serve infer ambient Move runtime authority from platform defaults. Starfire Muninn publishes the same daemon-owned witness records from C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc, Nightwing Muninn publishes the same daemon-owned witness records from /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc, and live Idunn accepts Muninn-family daemon health by daemon id and configured health contract rather than source-address folklore. ", + "Nightwing's restart wrapper makes Move runtime authority explicit with -DiscoverMoveState -ClaimUsbMoves instead of silently adding Move evidence on plain serve. Raven's GameCult-Muninn, GameCult-Muninn-Activate, and GameCult-Muninn-VideoProof tasks execute wscript.exe actions whose hidden VBS launchers call noninteractive hidden PowerShell entrypoints directly; health .cmd wrappers are archived instead of command-probe health paths. ", + "Live yggdrasil-streampixels publishes streampixels.cultnet-rudp-service-health through its configured Yggdrasil health route, live yggdrasil-heimdall publishes heimdall.cultnet-rudp-provider-health with a boundary store at /srv/heimdall/cultcache/heimdall.service.cc, and live yggdrasil-repixelizer publishes repixelizer.cultnet-rudp-service-health with a boundary store at /srv/repixelizer/cultcache/repixelizer.service.cc. ", + "Live Yggdrasil Gjallar publishes /var/lib/gamecult/gjallar/gjallar.service.cc plus a daemon-owned gamecult.eve.surface_state witness, consumes Odin's accepted surface:gamecult.network.status snapshot through the configured Gjallar Odin endpoint, and publishes gjallar.overview; Nightwing's Gjallar/framebuffer units are retired. Live Odin ingests remote Raven Vili while accepting Yggdrasil-local Gjallar publications directly over RUDP. ", + "Live Mimir Eve dashboard publishes CultMesh state at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp plus a boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc while health-nightwing-eve-dashboard.ps1 inspects those witnesses directly, and live Nightwing Eve browser reference publishes a boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc while health-nightwing-eve-browser-reference.ps1 inspects that store directly. The remaining debt is deleting bridge-only lowerings where they are no longer useful and keeping debug probes unable to own daemon truth." + ) .to_string(), verification_layer: "CultMesh keepalive store records plus live Idunn decision cycles, not process exit codes or chat summaries." @@ -5012,7 +5021,7 @@ fn daemon_surgery_plan(target: &DaemonTarget, updated_at: &str) -> IdunnDaemonSu severity = "critical"; owner = "Odin core"; current_mechanism = - "Odin now ingests remote Raven Vili, Raven Muninn, Starfire Muninn, Nightwing Muninn, and Nightwing Gjallar witness stores into the accepted provider catalog while relying on xenos-boundary hashed-store inspection for C# witness bodies." + "Odin ingests remote Raven Vili, Raven Muninn, Starfire Muninn, and Nightwing Muninn witness stores while accepting Yggdrasil-local Gjallar's typed aggregate publication directly over CultNet/RUDP." .to_string(); intended_authority = "Odin owns accepted Verse discovery and provider catalog truth as typed CultMesh/CultNet records over cultnet.transport.rudp.v0." @@ -7822,7 +7831,7 @@ mod tests { assert_eq!(plan.status, "active-transport-migration"); assert_eq!(plan.next_target, "none"); assert!(plan.current_phase.contains( - "Nightwing Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP" + "Yggdrasil Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP" )); assert!( plan.cut_line.contains( @@ -7871,13 +7880,10 @@ mod tests { plan.cut_line .contains("/srv/repixelizer/cultcache/repixelizer.service.cc") ); + assert!(plan.cut_line.contains("Live Yggdrasil Gjallar publishes")); assert!( plan.cut_line - .contains("live Nightwing Gjallar publishes a boundary store") - ); - assert!( - plan.cut_line - .contains("/var/lib/gamecult/gjallar/cultcache/gjallar.service.cc") + .contains("/var/lib/gamecult/gjallar/gjallar.service.cc") ); assert!( plan.cut_line diff --git a/docs/transport-shortcut-inventory.md b/docs/transport-shortcut-inventory.md index c9279a2..7ad1f0f 100644 --- a/docs/transport-shortcut-inventory.md +++ b/docs/transport-shortcut-inventory.md @@ -191,7 +191,7 @@ Deletion line: delete endpoint overrides and fallback health/input paths before | Loki Chrome observer | Chrome extension ingest and CDP probing are local observation lowerings. Loki now writes its daemon-owned snapshot, provider advertisement, Eve surface, command boundary, transport profile, debug/page snapshot, and Idunn health records through `state/loki.service.ccmp`, and only announces to Odin when an explicit Odin CultMesh/RUDP endpoint is configured. | Keep browser-extension HTTP as local input ergonomics only; do not restore text `.cc` witness sidecars or future-CultMesh prose as durable Verse state. | | Repixelizer service boundary store | The GUI runtime writes provider advertisement, Eve surface state, runtime config, auth projection, queue snapshot, job/job-event records, command boundary, transport profile, and `idunn.daemon_health` to the configured `.cc` witness store. It publishes Idunn RUDP health when `GC_ACCESS_IDUNN_RUDP_HEALTH` is configured; otherwise the transport profile reports missing RUDP health publication. | Keep queue/job ownership in `GuiJobManager` until a deliberate typed-store ownership move, and keep HTTP/systemd/nginx as deployment/debug witnesses only. | | Vili / Weksa | Vili publishes provider, operator, command, transport, motion request, and motion job records in its service CultCache store. HTTP/WS are operator/debug lowerings, JSON sidecars require explicit debug export, and the Vili smoke probe requires an explicit base URL. Weksa accepts commands over CultNet/RUDP, records deleted HTTP command lanes as non-authority, writes JSON mirrors only when debug export is explicit, and constructs its live provider advertisement from daemon-owned code rather than docs fixtures. Vili Idunn health, Weksa Idunn health, and Weksa startup publication are explicit/env-configured, not baked endpoint defaults. | Keep renderer, fixtures, and debug endpoints outside provider transport and command ownership, and route future Vili/Weksa command intake through typed records rather than JSON mirrors. | -| Gjallar | Consumes Odin accepted provider state through CultNet/RUDP snapshot input only; no WebSocket deck scrape, direct provider WebSocket receiver, or baked Starfire RUDP service endpoint remains. | Keep Nightwing framebuffer composition derived from Odin-owned provider state over RUDP, with launch config generated from Odin/CultMesh discovery or explicit operator config. | +| Gjallar | Runs headless beside Odin and Idunn on Yggdrasil, consumes Odin accepted provider state through CultNet/RUDP snapshot input, and publishes `gjallar.overview`; no WebSocket deck scrape, direct provider WebSocket receiver, baked Starfire endpoint, or live Nightwing framebuffer body remains. | Keep Yggdrasil aggregate membership/layout derived from Odin-owned provider state; EveCanvas, browser, TUI, and optional framebuffer clients lower the published tree without becoming owners. | | Idunn surgery/profile records | Migrated target profiles name daemon-owned RUDP health plus daemon-owned CultCache/CultMesh stores. Swarm targets no longer carry health commands, desired daemon records publish `health_command: None`, `--health-command` fails closed, and missing health becomes a non-actuating missing-publication observation instead of a probe result. | Rename legacy schema fields in a later schema migration if consumers can move together. | | Mimir Nightwing Eve dashboard/browser reference | Publish daemon-owned command boundaries and transport profiles with supervisor lifecycle authority and HTTP/WebSocket/static HTTP as lowerings, not compatibility transport authority. The Eve dashboard broker no longer imports arbitrary remote WebSocket deck providers or invents a StreamPixels SSH/LAN provider. Active proof/relay branch scripts require explicit endpoint config instead of baked Starfire/Raven LAN routes. | Keep Odin/Nightwing consumers reading the typed CultMesh/CultCache witnesses rather than service probes; add new remote surfaces through Odin/CultMesh ingestion, not broker launch args. | | Mimir OBS diagnostic sink | OBS route is labelled and configured as a debug sink; Mimir owns composition and Yggdrasil/site publication remains the publication path. | Keep OBS outside Verse discovery, provider ownership, and publication authority. | From fa0057c0c13427ab694fac649a9caaae03d20942 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 04:33:18 +0200 Subject: [PATCH 66/93] Add isolated Epiphany container target --- crates/idunn-daemon/src/main.rs | 122 ++++++++++++++++++++++++++++++-- scripts/linux/idunn-yggdrasil | 4 +- 2 files changed, 119 insertions(+), 7 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 650ecf6..fce5531 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -178,6 +178,8 @@ struct ReleaseTarget { state_migration_command: Option, zero_downtime_capability: String, deployed_revision_witness: Option, + artifact_strategy: String, + artifact_witness_root: Option, requires_bifrost_authority: bool, source_change_pathspecs: Vec, } @@ -4603,6 +4605,8 @@ fn release_target( state_migration_command: state_migration_command.map(ToString::to_string), zero_downtime_capability: zero_downtime_capability.to_string(), deployed_revision_witness: None, + artifact_strategy: "source-archive-from-observed-ref".to_string(), + artifact_witness_root: None, requires_bifrost_authority: false, source_change_pathspecs: Vec::new(), } @@ -4637,6 +4641,12 @@ fn with_deployed_revision_witness(mut release: ReleaseTarget, path: PathBuf) -> release } +fn with_container_artifact_witness(mut release: ReleaseTarget, root: PathBuf) -> ReleaseTarget { + release.artifact_strategy = "immutable-docker-image-id".to_string(); + release.artifact_witness_root = Some(root); + release +} + fn with_source_change_pathspecs(mut release: ReleaseTarget, pathspecs: &[&str]) -> ReleaseTarget { release.source_change_pathspecs = pathspecs.iter().map(|value| (*value).to_string()).collect(); release @@ -4702,7 +4712,7 @@ fn release_target_record( upstream_branch: release.upstream_branch.clone(), desired_revision, deployed_revision, - artifact_strategy: "source-archive-from-observed-ref".to_string(), + artifact_strategy: release.artifact_strategy.clone(), rollout_strategy: release.rollout_strategy.clone(), state_migration_authority: if release.state_migration_command.is_some() { "daemon-owned-command" @@ -4741,6 +4751,11 @@ fn deployment_artifact_record( release: &IdunnReleaseTargetRecord, built_at: &str, ) -> IdunnDeploymentArtifactRecord { + let witnessed_image = target + .release + .as_ref() + .and_then(|configured| configured.artifact_witness_root.as_ref()) + .and_then(|root| read_container_artifact_witness(root, &release.desired_revision).ok()); IdunnDeploymentArtifactRecord { artifact_id: artifact_id(target), daemon_id: target.daemon_id.clone(), @@ -4748,14 +4763,39 @@ fn deployment_artifact_record( source_branch: release.upstream_branch.clone(), source_remote: release.upstream_remote.clone(), artifact_kind: release.artifact_strategy.clone(), - artifact_uri: "built-by-deploy-command".to_string(), - sha256: "pending-deploy-command".to_string(), + artifact_uri: witnessed_image + .as_ref() + .map(|image_id| format!("docker-image://{image_id}")) + .unwrap_or_else(|| "not-yet-constructed".to_string()), + sha256: witnessed_image.unwrap_or_else(|| "not-yet-constructed".to_string()), built_at: built_at.to_string(), release_authority_id: release.release_authority_id.clone(), release_authority_envelope_sha256: release.release_authority_envelope_sha256.clone(), } } +fn read_container_artifact_witness(root: &Path, source_revision: &str) -> Result { + let witness = fs::read_to_string(root.join(format!("{source_revision}.env")))?; + let fields = witness + .lines() + .filter_map(|line| line.split_once('=')) + .collect::>(); + let image_id = fields + .get("image_id") + .ok_or_else(|| anyhow!("container artifact witness has no image_id"))?; + if fields.get("schema") != Some(&"idunn.epiphany_image_artifact.v1") + || fields.get("source_commit") != Some(&source_revision) + || !image_id.starts_with("sha256:") + || image_id.len() != 71 + || !image_id[7..] + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) + { + return Err(anyhow!("container artifact witness is invalid")); + } + Ok((*image_id).to_string()) +} + fn state_migration_plan_record( target: &DaemonTarget, release: &ReleaseTarget, @@ -5523,6 +5563,35 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { enabled: true, interval_seconds: 300, }, + DaemonTarget { + daemon_id: "yggdrasil-epiphany-ox17".to_string(), + verse_id: "yggdrasil.local".to_string(), + name: "Yggdrasil Epiphany Ox17 candidate".to_string(), + health_contract: health_contract("epiphany.ox17-capstone-admission", "failed"), + deploy_command: Some(yggdrasil_actuator("deploy", "epiphany-ox17")), + restart_command: None, + release: Some(with_source_change_pathspecs( + with_container_artifact_witness( + with_deployed_revision_witness( + release_target_on_branch( + "Epiphany", + PathBuf::from("/srv/build/Epiphany"), + "codex/epiphany-shakedown-live", + "fresh-container-capstone", + None, + "one-shot-no-restart", + ), + PathBuf::from("/srv/epiphany/ox17/deployment.env"), + ), + PathBuf::from("/srv/epiphany/artifacts"), + ), + DEPLOYABLE_SOURCE_PATHS, + )), + // Ox17 is an explicitly requested one-shot admission target. It + // never participates in daemon continuity or automatic rollout. + enabled: false, + interval_seconds: 300, + }, DaemonTarget { daemon_id: "yggdrasil-epiphany".to_string(), verse_id: "yggdrasil.local".to_string(), @@ -8381,6 +8450,30 @@ mod tests { ] ); + let ox17 = yggdrasil + .iter() + .find(|target| target.daemon_id == "yggdrasil-epiphany-ox17") + .expect("Yggdrasil Epiphany Ox17 target"); + assert!(!ox17.enabled, "Ox17 must never auto-actuate"); + assert!(ox17.restart_command.is_none()); + assert_eq!( + ox17.deploy_command.as_deref(), + Some( + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-ox17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + ) + ); + let ox17_release = ox17.release.as_ref().expect("Ox17 release target"); + assert_eq!(ox17_release.rollout_strategy, "fresh-container-capstone"); + assert_eq!(ox17_release.artifact_strategy, "immutable-docker-image-id"); + assert_eq!( + ox17_release.artifact_witness_root, + Some(PathBuf::from("/srv/epiphany/artifacts")) + ); + assert_eq!( + ox17_release.deployed_revision_witness, + Some(PathBuf::from("/srv/epiphany/ox17/deployment.env")) + ); + for legacy in yggdrasil .iter() .filter(|target| { @@ -8399,6 +8492,25 @@ mod tests { } } + #[test] + fn container_artifact_witness_binds_exact_source_and_image_id() { + let root = tempfile::tempdir().unwrap(); + let source = "a".repeat(40); + let image_id = format!("sha256:{}", "b".repeat(64)); + fs::write( + root.path().join(format!("{source}.env")), + format!( + "schema=idunn.epiphany_image_artifact.v1\nsource_commit={source}\nimage_id={image_id}\n" + ), + ) + .unwrap(); + assert_eq!( + read_container_artifact_witness(root.path(), &source).unwrap(), + image_id + ); + assert!(read_container_artifact_witness(root.path(), &"c".repeat(40)).is_err()); + } + #[test] fn yggdrasil_service_and_root_actuator_advertise_epiphany_release_boundary() { let unit = include_str!("../../../scripts/linux/idunn-yggdrasil.service"); @@ -8429,13 +8541,13 @@ mod tests { !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") ); assert!(!unit.contains("--trusted-epiphany-health-identity-store")); - assert!(actuator.contains("deploy:epiphany|restart:epiphany")); + assert!(actuator.contains("deploy:epiphany|restart:epiphany|deploy:epiphany-ox17")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); assert!( actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") ); assert!(actuator.contains( - "epiphany|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" + "epiphany|epiphany-ox17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" )); assert!( actuator diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index d13fdcc..bdd28a0 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -20,7 +20,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - epiphany|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; + epiphany|epiphany-ox17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -72,7 +72,7 @@ case "$action:$target" in restart:gjallar) exec systemctl restart gjallar-yggdrasil.service ;; - deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) + deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:epiphany-ox17|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 From 32009991eed1ccd6c565a99488160e526cc66e9a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 04:38:28 +0200 Subject: [PATCH 67/93] Retire unadmitted Gjallar transport claims --- docs/transport-shortcut-inventory.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/transport-shortcut-inventory.md b/docs/transport-shortcut-inventory.md index 7ad1f0f..b7f407f 100644 --- a/docs/transport-shortcut-inventory.md +++ b/docs/transport-shortcut-inventory.md @@ -677,7 +677,13 @@ Deletion line: delete endpoint overrides and fallback health/input paths before - Muninn/sleipnir Odin startup and catalog path cut: `crates\muninn-daemon` and `crates\sleipnir-daemon` now store Odin as `cultmesh://odin/rendezvous/provider-catalog` / `--odin-cultmesh-uri` and resolve it through Rust CultMesh only at RUDP catalog publish/pull boundaries. The old `--odin-cultmesh-rudp` flag is fail-closed in both daemons. Odin launchers for Raven/Starfire/Nightwing Muninn and Raven Sleipnir now pass `--odin-cultmesh-uri` / `ODIN_CULTMESH_URI` instead of raw endpoint flags. - Muninn/sleipnir Rust verification passed: `cargo test -p muninn-daemon odin_cultmesh`, `cargo test -p muninn-daemon request_stream_requires_odin_cultmesh_command_route`, `cargo test -p sleipnir-daemon odin_cultmesh`, `cargo test -p sleipnir-daemon rejects_removed_command_rudp_bind`, `cargo test --manifest-path vendor\cultmesh-rs\Cargo.toml resolves_rudp_endpoint_from_cultmesh_uri_bootstrap_env`, and `cargo test --manifest-path vendor\cultmesh-rs\Cargo.toml cultmesh_rudp_endpoint` passed. `cargo test -p muninn-daemon --no-run`, `cargo test -p sleipnir-daemon --no-run`, PowerShell parser checks for touched launchers, and `git diff --check` on touched Rust/script files also passed. - C# CultMesh Odin URI resolver added: `E:\Projects\CultLib\src\GameCult.Mesh\CultMesh.cs` now exposes `CultMesh.ResolveRudpEndpoint(...)`, accepts `cultmesh://` targets, resolves them through `CULTMESH_URI__RUDP` / `_CULTMESH_RUDP_ENDPOINT`, and the string `CreateRudpClient(...)` overload now resolves through that CultMesh boundary instead of accepting only raw `rudp://` endpoints. -- Gjallar Odin startup publication cut: `E:\Projects\Gjallar\src\Gjallar\Program.cs` now stores Odin startup publication as `--odin-cultmesh-uri` / `GJALLAR_ODIN_CULTMESH_URI`, defaults to `cultmesh://odin/rendezvous/provider-catalog`, rejects `--odin-cultmesh-rudp` / `GJALLAR_ODIN_CULTMESH_RUDP`, and removed its local Odin host/port parser. Gjallar publishes through `CultMesh.ResolveRudpEndpoint(...)`; docs now describe the CultMesh URI authority instead of a raw document-ingress override. +- Historical, unadmitted Gjallar URI experiment: an `E:\Projects\Gjallar` + body attempted to replace raw Odin RUDP flags with `--odin-cultmesh-uri`. + That body is not the canonical source or deployed release. The live + `F:\Projects\Gjallar` release `d683faa558351327d987918d1e8b8f79886dc9cb` + accepts `--odin-cultnet-rudp` and `--odin-cultmesh-rudp`; its Yggdrasil unit + uses both against `10.77.0.1:17871`. Treat the URI experiment and its checks + as rejected branch evidence, not a completed transport cut. - Aetheria Odin announcement path cut: `E:\Projects\Aetheria\Aetheria.State.Daemon\Program.cs` collapsed `OdinCultMeshRudpEndpoints` into a single `OdinCultMeshUri`, defaults to `cultmesh://odin/rendezvous/provider-catalog`, rejects both `--odin-cultmesh-rudp` and the previous `--odin-cultnet-rudp` alias as announcement targets, advertises provider announcement routes as `transport: cultmesh`, and publishes through the shared CultMesh client URI resolution path. - C# Odin URI verification passed: `dotnet build E:\Projects\CultLib\src\GameCult.Mesh\GameCult.Mesh.csproj --no-restore -p:OutDir=E:\Projects\.codex-build\cultmesh-out\`, `dotnet build E:\Projects\Gjallar\src\Gjallar\Gjallar.csproj --no-restore`, and isolated-output `dotnet build E:\Projects\Aetheria\Aetheria.State.Daemon\Aetheria.State.Daemon.csproj --no-restore -p:OutDir=E:\Projects\.codex-build\aetheria-daemon-out\` passed. The normal Aetheria output build was blocked by a running `Aetheria.State.Daemon` process holding DLLs open, so verification used `OutDir`; focused scans found no surviving raw Odin CultMesh RUDP config path outside intentional removed-option guards and this inventory. - VoidBot Bifrost startup health side-door cut: `E:\Projects\VoidBot\scripts\start-voidbot-stack.ps1` no longer reads `BIFROST_HEALTH_URL`, curls Bifrost `/healthz`, or starts `BIFROST_ROOT\compose.local.yaml` based on product HTTP health. Stack startup now verifies `BIFROST_CULTMESH_COMMAND_URI`, `BIFROST_CULTMESH_STORE_PATH`, the Bifrost CultMesh command processor, and exports the typed provider store when missing. VoidBot current maps and handoff notes now describe Bifrost readiness as the CultMesh command lane, not Docker/product HTTP health. @@ -691,10 +697,10 @@ Deletion line: delete endpoint overrides and fallback health/input paths before - Bifrost provider surface HTTP probe cut: `E:\Projects\Bifrost\tools\provider-advertisement.mjs` no longer accepts `--health-url`, `--ready-url`, `BIFROST_HEALTH_URL`, or `BIFROST_READY_URL` when building/exporting provider surfaces. Operator readiness now derives from typed CultMesh/CultCache stores, governance/request stores, and typed bridge command metadata; `/healthz` and `/readyz` remain product smoke endpoints only and cannot feed Odin-facing provider status. - Bifrost test-suite HTTP bridge fixture cut: `E:\Projects\Bifrost\tests\Bifrost.Web.Tests\BridgeCliTests.cs` no longer spins up a fake localhost `HttpListener` bridge, no longer teaches `/bridge/actions/*`, `/dispatch/runs/*`, `/transport/receipts`, or `/governance/receipts` as accepted receipt paths, and uses typed `BIFROST_CULTMESH_COMMAND_ID` provenance or explicit removed-config negative checks instead of `BIFROST_BRIDGE_BASE_URL` / `BIFROST_BRIDGE_TOKEN` success fixtures. - Bifrost bridge CLI test verification passed: `DOTNET_ROLL_FORWARD=Major dotnet test E:\Projects\Bifrost\tests\Bifrost.Web.Tests\Bifrost.Web.Tests.csproj --no-restore --filter FullyQualifiedName~BridgeCliTests` passed 30/30, and focused scans found no fake bridge server, localhost listener, deleted bridge route fixture, or old base-url/token success assertion in `BridgeCliTests.cs`. -- Gjallar Odin snapshot input cut: `E:\Projects\Gjallar\src\Gjallar\Program.cs` no longer accepts `--odin-cultnet-rudp` / `GJALLAR_ODIN_CULTNET_RUDP` as the live provider-state snapshot input. Gjallar now uses `--odin-cultmesh-uri` / `GJALLAR_ODIN_CULTMESH_URI`, defaults to `cultmesh://odin/rendezvous/provider-catalog`, resolves the concrete RUDP endpoint through `CultMesh.ResolveRudpEndpoint(...)` at the transport boundary, and publishes its runtime config/transport profile with the CultMesh URI rather than a raw host:port. -- Gjallar Idunn deployment cut: `E:\Projects\Odin\scripts\deploy-nightwing-gjallar.ps1` now writes `GJALLAR_ODIN_CULTMESH_URI` into the systemd drop-in and rejects `GJALLAR_ODIN_CULTNET_RUDP` / `ODIN_CULTNET_RUDP` before build, transfer, or restart work can begin. Gjallar README, architecture notes, and state map now describe Odin input as a CultMesh snapshot URI, not direct CultNet/RUDP endpoint configuration. -- Gjallar Odin URI input verification passed: `dotnet build E:\Projects\Gjallar\src\Gjallar\Gjallar.csproj --no-restore` passed; PowerShell parser check passed for `deploy-nightwing-gjallar.ps1`; stale `Gjallar.dll --odin-cultnet-rudp 127.0.0.1:17871 ...` fails before renderer startup; stale `GJALLAR_ODIN_CULTNET_RUDP=127.0.0.1:17871 Gjallar.dll ...` exits nonzero with the removed-env error; Idunn deploy with `ODIN_CULTNET_RUDP=127.0.0.1:17871` exits 1 with the removed-env error; focused scans found no surviving raw Gjallar Odin snapshot input path outside removed-config guards and this inventory. -- `gamecult-ops` Gjallar unit cut: `E:\Projects\gamecult-ops\scripts\gjallar.service` no longer requires `ODIN_CULTNET_RUDP` or launches Gjallar with `--odin-cultnet-rudp`. The unit rejects stale raw Odin endpoint env, defaults `GJALLAR_ODIN_CULTMESH_URI` to `cultmesh://odin/rendezvous/provider-catalog`, validates the value as a CultMesh URI, and passes `--odin-cultmesh-uri` to Gjallar. Nightwing visible-ops archive tombstones, inventory, and the Nightwing CultMesh worker tunnel runbook now teach the CultMesh URI route instead of a raw Odin socket. +- Current Gjallar transport admission: Idunn runs the headless Yggdrasil body, + Odin owns the accepted snapshot at `10.77.0.1:17871`, and Gjallar publishes + `gjallar.overview` back over the same RUDP substrate. Nightwing deployment and + framebuffer units are retired rather than adapted to the unadmitted URI path. - Loki Chrome extension HTTP ingest cut: `E:\Projects\Loki\src\loki-daemon.mjs` no longer imports `node:http`, opens a localhost extension ingest server, serves `/health` or `/state`, accepts `/ingest/chrome-extension`, advertises `chrome-extension-ingest`, or publishes `gamecult.loki.chrome_extension_report.v0`, `gamecult.loki.chrome_debug_probe.v0`, or `gamecult.loki.chrome_page_snapshot.v0` witness routes. The daemon is CDP-observation-only until browser-origin observations have a CultMesh/native binding. `--extension-host` and `--extension-port` fail closed, the unpacked extension is an archived status stub with no localhost host permissions, and the old extension-ingest smoke test is now a removed-path guard. - Loki verification passed: bundled Node syntax checks passed for `src\loki-daemon.mjs`, the extension service worker/popup, and Loki smoke tests; direct `node test\fixture-cdp-smoke.mjs` passed; direct `node test\extension-ingest-smoke.mjs` passed and proves `--extension-port` is rejected before startup. Focused scans found no remaining Loki extension HTTP ingest server, `/ingest/chrome-extension`, `LOKI_EXTENSION_PORT`, Chrome debugger/page snapshot action names, extension host permissions, or advertised extension/debug/page snapshot witness routes outside the intentional removed-option guard. - Bifrost web bridge route cut: `E:\Projects\Bifrost\src\Bifrost.Web\Program.cs` no longer maps `/bridge/actions/*`, `/dispatch/runs/*`, `/transport/receipts`, or `/governance/receipts` at all. The old local bridge token HTTP handlers, `BuildBridgeCaller`, and local bridge token gate were deleted from the product web app. `PlatformEndpointsTests` now proves the old routes are `404 NotFound` and do not write bridge action, dispatch run, transport receipt, or governance receipt rows. From 446657a1bdf4353db0f17276d191ed10ac76fdd1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 12:11:43 +0200 Subject: [PATCH 68/93] Rename Epiphany capstone target --- crates/idunn-daemon/src/main.rs | 42 ++++++++++++++++----------------- scripts/linux/idunn-yggdrasil | 4 ++-- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index fce5531..f683dd1 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5564,11 +5564,11 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { interval_seconds: 300, }, DaemonTarget { - daemon_id: "yggdrasil-epiphany-ox17".to_string(), + daemon_id: "yggdrasil-epiphany-capstone-17".to_string(), verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Epiphany Ox17 candidate".to_string(), - health_contract: health_contract("epiphany.ox17-capstone-admission", "failed"), - deploy_command: Some(yggdrasil_actuator("deploy", "epiphany-ox17")), + name: "Yggdrasil Epiphany Capstone 17 candidate".to_string(), + health_contract: health_contract("epiphany.capstone-17-admission", "failed"), + deploy_command: Some(yggdrasil_actuator("deploy", "epiphany-capstone-17")), restart_command: None, release: Some(with_source_change_pathspecs( with_container_artifact_witness( @@ -5581,13 +5581,13 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { None, "one-shot-no-restart", ), - PathBuf::from("/srv/epiphany/ox17/deployment.env"), + PathBuf::from("/srv/epiphany/capstone-17/deployment.env"), ), PathBuf::from("/srv/epiphany/artifacts"), ), DEPLOYABLE_SOURCE_PATHS, )), - // Ox17 is an explicitly requested one-shot admission target. It + // Capstone 17 is an explicitly requested one-shot admission target. It // never participates in daemon continuity or automatic rollout. enabled: false, interval_seconds: 300, @@ -8450,28 +8450,28 @@ mod tests { ] ); - let ox17 = yggdrasil + let capstone_17 = yggdrasil .iter() - .find(|target| target.daemon_id == "yggdrasil-epiphany-ox17") - .expect("Yggdrasil Epiphany Ox17 target"); - assert!(!ox17.enabled, "Ox17 must never auto-actuate"); - assert!(ox17.restart_command.is_none()); + .find(|target| target.daemon_id == "yggdrasil-epiphany-capstone-17") + .expect("Yggdrasil Epiphany Capstone 17 target"); + assert!(!capstone_17.enabled, "Capstone 17 must never auto-actuate"); + assert!(capstone_17.restart_command.is_none()); assert_eq!( - ox17.deploy_command.as_deref(), + capstone_17.deploy_command.as_deref(), Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-ox17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" ) ); - let ox17_release = ox17.release.as_ref().expect("Ox17 release target"); - assert_eq!(ox17_release.rollout_strategy, "fresh-container-capstone"); - assert_eq!(ox17_release.artifact_strategy, "immutable-docker-image-id"); + let capstone_17_release = capstone_17.release.as_ref().expect("Capstone 17 release target"); + assert_eq!(capstone_17_release.rollout_strategy, "fresh-container-capstone"); + assert_eq!(capstone_17_release.artifact_strategy, "immutable-docker-image-id"); assert_eq!( - ox17_release.artifact_witness_root, + capstone_17_release.artifact_witness_root, Some(PathBuf::from("/srv/epiphany/artifacts")) ); assert_eq!( - ox17_release.deployed_revision_witness, - Some(PathBuf::from("/srv/epiphany/ox17/deployment.env")) + capstone_17_release.deployed_revision_witness, + Some(PathBuf::from("/srv/epiphany/capstone-17/deployment.env")) ); for legacy in yggdrasil @@ -8541,13 +8541,13 @@ mod tests { !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") ); assert!(!unit.contains("--trusted-epiphany-health-identity-store")); - assert!(actuator.contains("deploy:epiphany|restart:epiphany|deploy:epiphany-ox17")); + assert!(actuator.contains("deploy:epiphany|restart:epiphany|deploy:epiphany-capstone-17")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); assert!( actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") ); assert!(actuator.contains( - "epiphany|epiphany-ox17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" + "epiphany|epiphany-capstone-17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" )); assert!( actuator diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index bdd28a0..6e8e9b7 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -20,7 +20,7 @@ compose=/srv/compose/yggdrasil-apps.yaml require_release_lineage() { case "$target" in bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - epiphany|epiphany-ox17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; + epiphany|epiphany-capstone-17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; esac if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then @@ -72,7 +72,7 @@ case "$action:$target" in restart:gjallar) exec systemctl restart gjallar-yggdrasil.service ;; - deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:epiphany-ox17|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) + deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:epiphany-capstone-17|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then echo "root-owned executable deploy manifest is not installed: $manifest" >&2 From 7e31921c4b53563d7a7f10e756d0dd1458fe7688 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 25 Aug 2026 12:28:36 +0200 Subject: [PATCH 69/93] Align capstone release with package identity --- crates/idunn-daemon/src/main.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index f683dd1..54d4de1 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -5570,22 +5570,19 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { health_contract: health_contract("epiphany.capstone-17-admission", "failed"), deploy_command: Some(yggdrasil_actuator("deploy", "epiphany-capstone-17")), restart_command: None, - release: Some(with_source_change_pathspecs( - with_container_artifact_witness( - with_deployed_revision_witness( - release_target_on_branch( - "Epiphany", - PathBuf::from("/srv/build/Epiphany"), - "codex/epiphany-shakedown-live", - "fresh-container-capstone", - None, - "one-shot-no-restart", - ), - PathBuf::from("/srv/epiphany/capstone-17/deployment.env"), + release: Some(with_container_artifact_witness( + with_deployed_revision_witness( + release_target_on_branch( + "Epiphany", + PathBuf::from("/srv/build/Epiphany"), + "codex/epiphany-shakedown-live", + "fresh-container-capstone", + None, + "one-shot-no-restart", ), - PathBuf::from("/srv/epiphany/artifacts"), + PathBuf::from("/srv/epiphany/capstone-17/deployment.env"), ), - DEPLOYABLE_SOURCE_PATHS, + PathBuf::from("/srv/epiphany/artifacts"), )), // Capstone 17 is an explicitly requested one-shot admission target. It // never participates in daemon continuity or automatic rollout. @@ -8465,6 +8462,7 @@ mod tests { let capstone_17_release = capstone_17.release.as_ref().expect("Capstone 17 release target"); assert_eq!(capstone_17_release.rollout_strategy, "fresh-container-capstone"); assert_eq!(capstone_17_release.artifact_strategy, "immutable-docker-image-id"); + assert!(capstone_17_release.source_change_pathspecs.is_empty()); assert_eq!( capstone_17_release.artifact_witness_root, Some(PathBuf::from("/srv/epiphany/artifacts")) From ffcc7eb820e81ca052d10b2d69ecb070ccba629d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 26 Aug 2026 17:57:36 +0200 Subject: [PATCH 70/93] Read CultMath shaders from CultLib --- crates/muninn-move-tracker/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/muninn-move-tracker/README.md b/crates/muninn-move-tracker/README.md index da8c4eb..a530a8a 100644 --- a/crates/muninn-move-tracker/README.md +++ b/crates/muninn-move-tracker/README.md @@ -4,7 +4,7 @@ Muninn owns PS Move sensor stream exposure. This crate is Muninn's first native optical marker candidate extractor: Rust owns orchestration, FFI, validation, and the CPU mirror; the compute shader owns the parallel image reduction stage. The shader includes CultMath's canonical HLSL mirror, so shader compilation must -add `E:\Projects\CultMath\shaders` or the packaged CultMath shader content to +add `E:\Projects\CultLib\packages\cultmath\shaders` or the packaged CultMath shader content to the include path. This crate deliberately stops at per-frame marker candidates. Final pose, From 32cbb5f02855230f45538537d25256cc75c7e5d6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 13:53:07 +0200 Subject: [PATCH 71/93] Bind Idunn deployment and runtime admission --- crates/idunn-daemon/src/main.rs | 2521 ++++++++++++++++++++++- crates/idunn-daemon/src/provisioning.rs | 575 +++++- crates/odin-core/src/documents.rs | 201 +- crates/odin-core/src/idunn.rs | 10 + docs/idunn.md | 27 +- scripts/linux/idunn-yggdrasil | 394 +++- scripts/linux/idunn-yggdrasil.service | 2 +- scripts/linux/idunn-yggdrasil.sudoers | 25 +- vendor/cultnet-rs | 2 +- 9 files changed, 3574 insertions(+), 183 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 54d4de1..3b00e78 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -22,6 +22,8 @@ use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ BifrostRepositoryReleaseAuthorityRecord, IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA, IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, IdunnAuthenticatedDaemonHealthAdmissionRecord, IdunnAuthenticatedProviderHealthProjectionRecord, IdunnCommandBoundaryRecord, IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, @@ -30,7 +32,8 @@ use odin_core::{ IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, IdunnOperatorAlarmRecord, IdunnPlan, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, - IdunnRudpHealthIngressRecord, IdunnRuntimeTransportCheckRecord, IdunnSignedDaemonHealthRecord, + IdunnRudpHealthIngressRecord, IdunnRuntimeTrafficAdmissionRecord, + IdunnRuntimeTransportCheckRecord, IdunnSignedDaemonHealthRecord, IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, IdunnUnsignedDaemonHealthDiagnosticRecord, OdinDocuments, @@ -54,6 +57,15 @@ const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001; const IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID: u32 = 0x1d0d_0002; const IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID: &str = "idunn-daemon"; const IDUNN_PUBLIC_HEALTH_QUERY_ROLE: &str = "authenticated-provider-health-projector"; +const CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH: &str = + "/etc/gamecult/codex-connector/runtime/traffic-admission.cc"; +const CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP: &str = "codex-connector"; +const GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH: &str = + "/etc/gamecult/ghostlight-dungeon/runtime/traffic-admission.cc"; +const GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP: &str = "ghostlight"; +const RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL: &str = "traffic-admission-pending"; +const GHOSTLIGHT_MINIMUM_SOURCE_REVISION: &str = "6bb686981050e048c070376e7f7ac86d177e8f18"; +const CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION: &str = "68fe94b9475823de92ed766a97ea962d48723708"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE: &str = "epiphany.idunn_signed_runtime_health"; const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = "epiphany.idunn_signed_runtime_health.v0"; @@ -111,7 +123,7 @@ enum DecodedHealthIngress { }, AuthenticatedGeneric { health: IdunnDaemonHealthRecord, - statement: IdunnSignedDaemonHealthRecord, + statement_payload: Vec, admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, }, Diagnostic(IdunnUnsignedDaemonHealthDiagnosticRecord), @@ -182,8 +194,41 @@ struct ReleaseTarget { artifact_witness_root: Option, requires_bifrost_authority: bool, source_change_pathspecs: Vec, + minimum_source_revision: Option, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct CompiledMinimumSourceRevisionPolicy { + daemon_id: &'static str, + repository_full_name: &'static str, + canonical_repository_url: &'static str, + upstream_ref: &'static str, + canonical_fetched_ref: &'static str, + repository_path: &'static str, + minimum_source_revision: &'static str, +} + +const COMPILED_MINIMUM_SOURCE_REVISION_POLICIES: &[CompiledMinimumSourceRevisionPolicy] = &[ + CompiledMinimumSourceRevisionPolicy { + daemon_id: "yggdrasil-codex-connector", + repository_full_name: "GameCult/CodexConnector", + canonical_repository_url: "https://github.com/GameCult/CodexConnector.git", + upstream_ref: "refs/heads/codex/ghostlight-release-binding", + canonical_fetched_ref: "refs/remotes/origin/codex/ghostlight-release-binding", + repository_path: "/srv/build/CodexConnector", + minimum_source_revision: CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION, + }, + CompiledMinimumSourceRevisionPolicy { + daemon_id: "yggdrasil-ghostlight", + repository_full_name: "GameCult/Ghostlight", + canonical_repository_url: "https://github.com/GameCult/Ghostlight.git", + upstream_ref: "refs/heads/codex/ghostlight-dungeon-mvp", + canonical_fetched_ref: "refs/remotes/origin/codex/ghostlight-dungeon-mvp", + repository_path: "/srv/build/Ghostlight", + minimum_source_revision: GHOSTLIGHT_MINIMUM_SOURCE_REVISION, + }, +]; + #[derive(Clone, Debug, PartialEq, Eq)] struct ReleaseAuthorization { repository_full_name: String, @@ -572,7 +617,9 @@ enum Mode { Swarm(SwarmOptions), LifecycleCommand(LifecycleCommandOptions), ReleaseAuthorityValidation(ReleaseAuthorityValidationOptions), + MinimumSourceRevisionValidation(MinimumSourceRevisionValidationOptions), HealthAdmissionValidation(HealthAdmissionValidationOptions), + RuntimeAdmissionValidation(RuntimeAdmissionValidationOptions), } #[derive(Clone, Debug)] @@ -582,6 +629,52 @@ struct HealthAdmissionValidationOptions { release_id: String, release_witness_sha256: String, source_commit: String, + activation_witness_sha256: Option, + write_admission_witness: Option, + runtime_process: Option, +} + +#[derive(Clone, Debug)] +struct RuntimeAdmissionValidationOptions { + input: PathBuf, + daemon_id: String, + deployment_request_id: String, + release_id: String, + release_witness_sha256: String, + source_commit: String, + activation_witness_sha256: String, + runtime_process: RuntimeProcessObservation, +} + +#[derive(Clone, Debug)] +enum ValidatedHealthAdmission { + GenericActive(IdunnAuthenticatedDaemonHealthAdmissionRecord), + RuntimeTraffic(RuntimeTrafficIssuanceCandidate), + LegacyActive(IdunnSignedHealthAdmissionRecord), +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct RuntimeProcessObservation { + process_id: u32, + starttime_ticks: u64, +} + +impl RuntimeProcessObservation { + fn validate(self) -> Result { + if self.process_id == 0 || self.starttime_ticks == 0 { + return Err(anyhow!( + "runtime process id and starttime ticks must both be nonzero" + )); + } + Ok(self) + } +} + +#[derive(Clone, Debug)] +struct RuntimeTrafficIssuanceCandidate { + grant: IdunnRuntimeTrafficAdmissionRecord, + desired: IdunnDesiredDaemonRecord, + source: AuthenticatedProviderHealthSource, } #[derive(Clone, Debug)] @@ -620,10 +713,20 @@ struct ReleaseAuthorityValidationOptions { envelope_sha256: String, } +#[derive(Clone, Debug)] +struct MinimumSourceRevisionValidationOptions { + daemon_id: String, + source_revision: String, +} + fn main() -> Result<()> { let options = Options::parse(env::args().skip(1))?; - if let Some(parent) = options.common.store_path.parent() { + if !matches!( + &options.mode, + Mode::RuntimeAdmissionValidation(_) | Mode::MinimumSourceRevisionValidation(_) + ) && let Some(parent) = options.common.store_path.parent() + { std::fs::create_dir_all(parent) .with_context(|| format!("creating {}", parent.display()))?; } @@ -657,9 +760,31 @@ fn main() -> Result<()> { Mode::ReleaseAuthorityValidation(validation) => { validate_release_authority_at_privileged_boundary(validation) } + Mode::MinimumSourceRevisionValidation(validation) => { + validate_minimum_source_revision_at_privileged_boundary(validation) + } Mode::HealthAdmissionValidation(validation) => { validate_health_admission(validation, &options.common) } + Mode::RuntimeAdmissionValidation(validation) => { + let admission = validate_runtime_admission(validation)?; + println!( + "validated Idunn runtime traffic admission daemon={} releaseId={} witnessSha256={} sourceCommit={} deploymentId={} activationWitnessSha256={} signedHealthSha256={} publisherIncarnation={} publisherSequence={} signerIdentity={} runtimeProcessId={} runtimeProcessStarttimeTicks={}", + admission.daemon_id, + admission.release_id, + admission.release_witness_sha256, + admission.source_commit, + admission.deployment_id, + admission.activation_witness_sha256, + admission.signed_health_sha256, + admission.publisher_incarnation_id, + admission.publisher_sequence, + admission.signer_identity_id, + admission.runtime_process_id, + admission.runtime_process_starttime_ticks, + ); + Ok(()) + } } } @@ -667,14 +792,95 @@ fn validate_health_admission( expected: &HealthAdmissionValidationOptions, options: &CommonOptions, ) -> Result<()> { - validate_health_admission_at(expected, options, ×tamp()?) + let now = timestamp()?; + if expected.write_admission_witness.is_some() { + let grant = issue_runtime_traffic_admission(expected, options, &now)?; + let output = expected.write_admission_witness.as_deref().ok_or_else(|| { + anyhow!("validated runtime traffic candidate lost its fixed output path") + })?; + println!( + "published Idunn runtime traffic admission daemon={} path={} signedHealthSha256={} publisherIncarnation={} publisherSequence={} runtimeProcessId={} runtimeProcessStarttimeTicks={}", + grant.daemon_id, + output.display(), + grant.signed_health_sha256, + grant.publisher_incarnation_id, + grant.publisher_sequence, + grant.runtime_process_id, + grant.runtime_process_starttime_ticks, + ); + return Ok(()); + } + + let admission = validate_health_admission_at(expected, options, &now)?; + match admission { + ValidatedHealthAdmission::GenericActive(admission) => { + println!( + "validated authenticated Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} activationWitnessSha256={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", + admission.daemon_id, + admission.state, + admission.release_id.as_deref().unwrap_or_default(), + admission + .release_witness_sha256 + .as_deref() + .unwrap_or_default(), + admission.source_commit.as_deref().unwrap_or_default(), + admission + .activation_witness_sha256 + .as_deref() + .unwrap_or_default(), + admission.signed_health_sha256, + admission.publisher_incarnation_id, + admission.publisher_sequence, + ); + } + ValidatedHealthAdmission::RuntimeTraffic(_) => { + return Err(anyhow!( + "runtime traffic admission must use the atomic issuance path" + )); + } + ValidatedHealthAdmission::LegacyActive(admission) => { + println!( + "validated signed Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", + admission.daemon_id, + admission.state, + admission.release_id, + admission.release_witness_sha256, + admission.source_commit, + admission.signed_health_sha256, + admission.publisher_incarnation_id, + admission.publisher_sequence, + ); + } + } + Ok(()) } fn validate_health_admission_at( expected: &HealthAdmissionValidationOptions, options: &CommonOptions, now: &str, -) -> Result<()> { +) -> Result { + if let Some(digest) = expected.activation_witness_sha256.as_deref() { + validate_sha256(digest, "activation witness")?; + } + let issuing_runtime_admission = expected.write_admission_witness.is_some(); + if let Some(output) = expected.write_admission_witness.as_deref() { + require_supported_runtime_admission_pair(&expected.daemon_id, output)?; + if expected.activation_witness_sha256.is_none() { + return Err(anyhow!( + "runtime traffic admission requires --activation-witness-sha256" + )); + } + expected.runtime_process.ok_or_else(|| { + anyhow!( + "runtime traffic admission requires --runtime-process-id and --runtime-process-starttime-ticks" + ) + })?; + } else if expected.runtime_process.is_some() { + return Err(anyhow!( + "runtime process identity belongs only to runtime traffic admission issuance" + )); + } let lock = Arc::new(Mutex::new(())); let has_generic_admission = with_store_node(options, &lock, |node| { Ok(node @@ -704,42 +910,57 @@ fn validate_health_admission_at( expected.daemon_id ) })?; - let admission = source.admission; - if admission.state != "active" - || admission.deployment_id.as_deref() != Some(expected.deployment_request_id.as_str()) + let signed_health_detail = source.statement.detail.clone(); + let admission = source.admission.clone(); + let required_state = validate_health_admission_posture( + &admission.state, + &signed_health_detail, + issuing_runtime_admission, + )?; + if admission.deployment_id.as_deref() != Some(expected.deployment_request_id.as_str()) || admission.release_id.as_deref() != Some(expected.release_id.as_str()) || admission.release_witness_sha256.as_deref() != Some(expected.release_witness_sha256.as_str()) || admission.source_commit.as_deref() != Some(expected.source_commit.as_str()) + || expected + .activation_witness_sha256 + .as_deref() + .is_some_and(|digest| { + admission.activation_witness_sha256.as_deref() != Some(digest) + }) || admission.publisher_incarnation_id.is_empty() || admission.publisher_sequence == 0 || admission.signed_health_sha256.is_empty() || admission.signer_identity_id.is_empty() { return Err(anyhow!( - "Idunn admitted health does not prove the exact active candidate" + "Idunn admitted health does not prove the exact {} candidate", + required_state )); } - println!( - "validated authenticated Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", - admission.daemon_id, - admission.state, - admission.release_id.as_deref().unwrap_or_default(), - admission - .release_witness_sha256 - .as_deref() - .unwrap_or_default(), - admission.source_commit.as_deref().unwrap_or_default(), - admission.signed_health_sha256, - admission.publisher_incarnation_id, - admission.publisher_sequence, - ); - return Ok(()); + if issuing_runtime_admission { + let process = expected.runtime_process.ok_or_else(|| { + anyhow!("runtime traffic candidate lost its root-observed process identity") + })?; + return Ok(ValidatedHealthAdmission::RuntimeTraffic( + RuntimeTrafficIssuanceCandidate { + grant: runtime_traffic_admission_from(&admission, process)?, + desired, + source, + }, + )); + } + return Ok(ValidatedHealthAdmission::GenericActive(admission)); } // The old Epiphany-specific health contract remains a sealed read path for // already-admitted releases. New publishers use the generic authenticated // daemon-health contract above. + if issuing_runtime_admission { + return Err(anyhow!( + "runtime traffic admission requires generic authenticated daemon health; legacy Epiphany admission is ineligible" + )); + } let admission = with_store_node(options, &lock, |node| { let admission = node .get::(&expected.daemon_id)? @@ -771,6 +992,7 @@ fn validate_health_admission_at( || admission.release_id != expected.release_id || admission.release_witness_sha256 != expected.release_witness_sha256 || admission.source_commit != expected.source_commit + || expected.activation_witness_sha256.is_some() || admission.publisher_incarnation_id.is_empty() || admission.publisher_sequence == 0 || admission.signed_health_sha256.is_empty() @@ -780,20 +1002,714 @@ fn validate_health_admission_at( "Idunn admitted health does not prove the exact active candidate" )); } - println!( - "validated signed Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", - admission.daemon_id, - admission.state, - admission.release_id, - admission.release_witness_sha256, - admission.source_commit, - admission.signed_health_sha256, - admission.publisher_incarnation_id, - admission.publisher_sequence, - ); + Ok(ValidatedHealthAdmission::LegacyActive(admission)) +} + +fn validate_health_admission_posture( + state: &str, + detail: &str, + issuing_runtime_admission: bool, +) -> Result<&'static str> { + let required_state = if issuing_runtime_admission { + "warming" + } else { + "active" + }; + if state != required_state + || (issuing_runtime_admission && detail != RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL) + { + return Err(anyhow!( + "Idunn admitted health does not prove the exact {} candidate", + required_state + )); + } + Ok(required_state) +} + +fn runtime_traffic_admission_from( + admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, + process: RuntimeProcessObservation, +) -> Result { + if admission.state != "warming" { + return Err(anyhow!( + "runtime traffic authority requires a supported target's warming authenticated admission" + )); + } + let process = process.validate()?; + let grant = IdunnRuntimeTrafficAdmissionRecord { + schema_version: IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into(), + daemon_id: admission.daemon_id.clone(), + release_id: admission + .release_id + .clone() + .ok_or_else(|| anyhow!("runtime traffic authority lost its release id"))?, + release_witness_sha256: admission + .release_witness_sha256 + .clone() + .ok_or_else(|| anyhow!("runtime traffic authority lost its release witness"))?, + source_commit: admission + .source_commit + .clone() + .ok_or_else(|| anyhow!("runtime traffic authority lost its source commit"))?, + deployment_id: admission + .deployment_id + .clone() + .ok_or_else(|| anyhow!("runtime traffic authority lost its deployment id"))?, + activation_witness_sha256: admission + .activation_witness_sha256 + .clone() + .ok_or_else(|| anyhow!("runtime traffic authority lost its activation witness"))?, + signed_health_sha256: admission.signed_health_sha256.clone(), + publisher_incarnation_id: admission.publisher_incarnation_id.clone(), + publisher_sequence: admission.publisher_sequence, + signer_identity_id: admission.signer_identity_id.clone(), + runtime_process_id: process.process_id, + runtime_process_starttime_ticks: process.starttime_ticks, + }; + grant.validate()?; + Ok(grant) +} + +fn issue_runtime_traffic_admission( + expected: &HealthAdmissionValidationOptions, + options: &CommonOptions, + now: &str, +) -> Result { + let output = expected + .write_admission_witness + .as_deref() + .ok_or_else(|| anyhow!("runtime traffic issuance lost its fixed output path"))?; + require_supported_runtime_admission_pair(&expected.daemon_id, output)?; + #[cfg(unix)] + require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; + let process = expected + .runtime_process + .ok_or_else(|| anyhow!("runtime traffic issuance lost its process identity"))? + .validate()?; + require_current_runtime_process(process)?; + + let ValidatedHealthAdmission::RuntimeTraffic(candidate) = + validate_health_admission_at(expected, options, now)? + else { + return Err(anyhow!( + "runtime traffic issuance did not resolve an authenticated probation candidate" + )); + }; + + let trust_store_path = options + .daemon_health_trust_store_path + .as_deref() + .ok_or_else(|| anyhow!("runtime traffic issuance has no root trust store"))?; + // Fixed lock order: first the Idunn-owned operational store, then the + // root-owned trust store. These locks are synchronization, not decision + // owners. Every authenticated-health/head writer contends on the first; + // every trust writer contends on the second. Holding both shared freezes + // the exact source and signer authority while root takes a fresh clock, + // rechecks the process, and CAS-publishes the one root-owned grant. Target + // services can only read their separately sealed grant store and lock. + let state_store = SingleFileMessagePackBackingStore::new(&options.store_path); + let trust_store = SingleFileMessagePackBackingStore::new(trust_store_path); + let grant = state_store.with_read_only_shared_snapshot(|state_snapshot| { + trust_store.with_read_only_shared_snapshot(|trust_snapshot| { + validate_runtime_traffic_issuance_trust_snapshot(&trust_snapshot, &candidate)?; + let mint_now = timestamp()?; + validate_runtime_traffic_issuance_snapshot( + &state_snapshot, + &candidate, + expected, + &mint_now, + )?; + require_current_runtime_process(process)?; + publish_runtime_traffic_admission(output, &candidate.grant)?; + if let Err(process_error) = require_current_runtime_process(process) { + let revoke = revoke_runtime_traffic_admission(output); + return match revoke { + Ok(()) => Err(process_error.context( + "runtime process changed immediately after grant publication; grant revoked", + )), + Err(revoke_error) => Err(anyhow!( + "runtime process changed immediately after grant publication: {process_error:#}; fail-closed revocation also failed: {revoke_error:#}" + )), + }; + } + Ok(candidate.grant.clone()) + }) + })?; + Ok(grant) +} + +fn validate_runtime_traffic_issuance_trust_snapshot( + snapshot: &[CultCacheEnvelope], + candidate: &RuntimeTrafficIssuanceCandidate, +) -> Result<()> { + let mut matches = Vec::new(); + for entry in snapshot + .iter() + .filter(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) + { + let binding: IdunnDaemonHealthTrustBindingRecord = rmp_serde::from_slice(&entry.payload) + .context("decoding frozen runtime-issuance trust binding")?; + binding.validate()?; + if binding.daemon_id == candidate.source.statement.daemon_id + && binding.health_contract == candidate.source.statement.health_contract + && binding.source_runtime_id == candidate.source.statement.source_runtime_id + { + matches.push(binding); + } + } + let [binding] = matches.as_slice() else { + return Err(anyhow!( + "runtime traffic issuance requires one exact frozen trust binding" + )); + }; + let binding_sha256 = record_sha256(binding)?; + if binding != &candidate.source.binding + || binding_sha256 != candidate.source.binding_sha256 + || binding.signer_identity_id != candidate.source.admission.signer_identity_id + { + return Err(anyhow!( + "runtime traffic issuance trust binding changed before grant publication" + )); + } Ok(()) } +fn validate_runtime_traffic_issuance_snapshot( + snapshot: &[CultCacheEnvelope], + candidate: &RuntimeTrafficIssuanceCandidate, + expected: &HealthAdmissionValidationOptions, + mint_now: &str, +) -> Result<()> { + let desired: IdunnDesiredDaemonRecord = + required_snapshot_record(snapshot, &candidate.desired.daemon_id, "desired daemon")?; + let health: IdunnDaemonHealthRecord = required_snapshot_record( + snapshot, + &candidate.source.health.daemon_id, + "authenticated daemon health", + )?; + let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = required_snapshot_record( + snapshot, + &candidate.source.admission.daemon_id, + "authenticated health admission", + )?; + let statement_envelope = required_snapshot_envelope::( + snapshot, + &candidate.source.admission.daemon_id, + "signed daemon health", + )?; + let (statement, _) = decode_generic_signed_health_payload(&statement_envelope.payload)?; + let statement_sha256 = format!("sha256-{:x}", Sha256::digest(&statement_envelope.payload)); + let deployment_head = candidate + .source + .deployment_head + .as_ref() + .ok_or_else(|| anyhow!("runtime traffic candidate lost its deployment head"))?; + let current_head: IdunnCurrentDeploymentRequestRecord = required_snapshot_record( + snapshot, + &candidate.source.admission.daemon_id, + "current deployment head", + )?; + let deployment_request = candidate + .source + .deployment_request + .as_ref() + .ok_or_else(|| anyhow!("runtime traffic candidate lost its deployment request"))?; + let current_request: IdunnDeploymentRequestRecord = required_snapshot_record( + snapshot, + &deployment_request.request_id, + "deployment request", + )?; + + let process = expected + .runtime_process + .ok_or_else(|| anyhow!("runtime traffic issuance lost its expected process identity"))?; + let expected_grant = runtime_traffic_admission_from(&admission, process)?; + validate_runtime_traffic_issuance_freshness(&desired, &admission, mint_now)?; + if desired != candidate.desired + || health != candidate.source.health + || admission != candidate.source.admission + || statement != candidate.source.statement + || statement_sha256 != candidate.source.statement_sha256 + || current_head != *deployment_head + || current_request != *deployment_request + || current_head.daemon_id != admission.daemon_id + || current_head.request_id != expected.deployment_request_id + || current_head.request_id != current_request.request_id + || current_head.sequence == 0 + || current_request.daemon_id != admission.daemon_id + || current_request.source_revision != expected.source_commit + || expected_grant != candidate.grant + { + return Err(anyhow!( + "authenticated health or current deployment request changed before runtime traffic issuance" + )); + } + Ok(()) +} + +fn validate_runtime_traffic_issuance_freshness( + desired: &IdunnDesiredDaemonRecord, + admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, + mint_now: &str, +) -> Result<()> { + let mint_now_millis = parse_timestamp_millis(mint_now) + .ok_or_else(|| anyhow!("runtime traffic issuance clock is invalid"))?; + if mint_now_millis < admission.admitted_at_unix_millis + || mint_now_millis.saturating_sub(admission.observed_at_unix_millis) + > u64::from(desired.max_silence_seconds).saturating_mul(1000) + { + return Err(anyhow!( + "authenticated health became stale before runtime traffic issuance" + )); + } + Ok(()) +} + +fn required_snapshot_record(snapshot: &[CultCacheEnvelope], key: &str, label: &str) -> Result +where + T: DatabaseEntry + serde::de::DeserializeOwned, +{ + let envelope = required_snapshot_envelope::(snapshot, key, label)?; + rmp_serde::from_slice(&envelope.payload) + .with_context(|| format!("decoding exact {label} snapshot")) +} + +fn required_snapshot_envelope<'a, T: DatabaseEntry>( + snapshot: &'a [CultCacheEnvelope], + key: &str, + label: &str, +) -> Result<&'a CultCacheEnvelope> { + let mut matches = snapshot + .iter() + .filter(|entry| entry.r#type == T::TYPE && entry.key == key); + let envelope = matches + .next() + .ok_or_else(|| anyhow!("exact {label} snapshot is absent"))?; + if matches.next().is_some() { + return Err(anyhow!("exact {label} snapshot is ambiguous")); + } + Ok(envelope) +} + +fn require_current_runtime_process(expected: RuntimeProcessObservation) -> Result<()> { + let observed = observe_runtime_process(expected.process_id)?; + if observed != expected { + return Err(anyhow!( + "root-observed runtime process identity changed before traffic admission" + )); + } + Ok(()) +} + +#[cfg(target_os = "linux")] +fn observe_runtime_process(process_id: u32) -> Result { + let stat_path = PathBuf::from(format!("/proc/{process_id}/stat")); + let stat = fs::read_to_string(&stat_path).with_context(|| { + format!( + "reading root-observed runtime process {}", + stat_path.display() + ) + })?; + Ok(RuntimeProcessObservation { + process_id, + starttime_ticks: parse_linux_proc_stat_starttime_ticks(&stat, process_id)?, + }) +} + +#[cfg(not(target_os = "linux"))] +fn observe_runtime_process(_process_id: u32) -> Result { + Err(anyhow!( + "runtime traffic admission process observation requires Linux /proc" + )) +} + +fn parse_linux_proc_stat_starttime_ticks(stat: &str, expected_process_id: u32) -> Result { + let command_open = stat + .find('(') + .ok_or_else(|| anyhow!("Linux process stat has no command opening delimiter"))?; + let command_close = stat + .rfind(')') + .filter(|close| *close > command_open) + .ok_or_else(|| anyhow!("Linux process stat has no command closing delimiter"))?; + let process_id: u32 = stat[..command_open] + .trim() + .parse() + .context("Linux process stat has an invalid process id")?; + if process_id != expected_process_id || process_id == 0 { + return Err(anyhow!( + "Linux process stat does not describe the expected process" + )); + } + // Fields after the command begin at field 3 (state), so zero-based index + // 19 is field 22: the kernel starttime in clock ticks since boot. + let starttime_ticks: u64 = stat[command_close + 1..] + .split_whitespace() + .nth(19) + .ok_or_else(|| anyhow!("Linux process stat has no field-22 starttime"))? + .parse() + .context("Linux process stat field-22 starttime is invalid")?; + if starttime_ticks == 0 { + return Err(anyhow!( + "Linux process stat field-22 starttime must be nonzero" + )); + } + Ok(starttime_ticks) +} + +fn runtime_admission_policy_for_path(path: &Path) -> Result<(&'static str, &'static str)> { + let spelling = path.as_os_str(); + if spelling == std::ffi::OsStr::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) { + return Ok(( + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP, + )); + } + if spelling == std::ffi::OsStr::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) { + return Ok(( + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP, + )); + } + Err(anyhow!( + "runtime traffic admission path is not one of the two exact supported absolute paths" + )) +} + +fn require_supported_runtime_admission_pair(daemon_id: &str, path: &Path) -> Result<()> { + let (expected_daemon_id, _) = runtime_admission_policy_for_path(path)?; + if daemon_id != expected_daemon_id { + return Err(anyhow!( + "runtime traffic admission daemon/path pair is unsupported" + )); + } + Ok(()) +} + +fn decode_runtime_traffic_admission_snapshot( + entries: &[CultCacheEnvelope], +) -> Result { + let [entry] = entries else { + return Err(anyhow!( + "runtime traffic admission store must contain exactly one record" + )); + }; + if entry.r#type != IdunnRuntimeTrafficAdmissionRecord::TYPE + || entry.schema_id.as_deref() != Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA) + || chrono::DateTime::parse_from_rfc3339(&entry.stored_at).is_err() + { + return Err(anyhow!( + "runtime traffic admission envelope has the wrong type, schema, or timestamp" + )); + } + let record: IdunnRuntimeTrafficAdmissionRecord = + rmp_serde::from_slice(&entry.payload).context("decoding runtime traffic admission")?; + if rmp_serde::to_vec(&record)? != entry.payload { + return Err(anyhow!( + "runtime traffic admission is not canonical positional MessagePack" + )); + } + record.validate()?; + if record.daemon_id != entry.key { + return Err(anyhow!( + "runtime traffic admission envelope key does not match its supported daemon" + )); + } + Ok(record) +} + +fn validate_runtime_admission( + expected: &RuntimeAdmissionValidationOptions, +) -> Result { + expected.runtime_process.validate()?; + require_current_runtime_process(expected.runtime_process)?; + require_supported_runtime_admission_pair(&expected.daemon_id, &expected.input)?; + validate_sha256(&expected.release_witness_sha256, "release witness")?; + validate_commit_sha(&expected.source_commit)?; + validate_sha256(&expected.activation_witness_sha256, "activation witness")?; + validate_runtime_admission_filesystem(&expected.input, true)?; + let record = SingleFileMessagePackBackingStore::new(&expected.input) + .with_read_only_shared_snapshot(|entries| -> Result<_> { + let record = decode_runtime_traffic_admission_snapshot(&entries)?; + validate_runtime_admission_lineage(&record, expected)?; + Ok(record) + })?; + validate_runtime_admission_filesystem(&expected.input, true)?; + require_current_runtime_process(expected.runtime_process)?; + Ok(record) +} + +fn validate_runtime_admission_lineage( + record: &IdunnRuntimeTrafficAdmissionRecord, + expected: &RuntimeAdmissionValidationOptions, +) -> Result<()> { + if record.daemon_id != expected.daemon_id + || record.deployment_id != expected.deployment_request_id + || record.release_id != expected.release_id + || record.release_witness_sha256 != expected.release_witness_sha256 + || record.source_commit != expected.source_commit + || record.activation_witness_sha256 != expected.activation_witness_sha256 + || record.runtime_process_id != expected.runtime_process.process_id + || record.runtime_process_starttime_ticks != expected.runtime_process.starttime_ticks + { + return Err(anyhow!( + "runtime traffic admission does not prove the exact expected release tuple" + )); + } + Ok(()) +} + +fn require_runtime_admission_writer_uid(uid: u32) -> Result<()> { + if uid != 0 { + return Err(anyhow!( + "runtime traffic admission publication requires root" + )); + } + Ok(()) +} + +fn publish_runtime_traffic_admission( + path: &Path, + record: &IdunnRuntimeTrafficAdmissionRecord, +) -> Result<()> { + require_supported_runtime_admission_pair(&record.daemon_id, path)?; + #[cfg(unix)] + require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; + record.validate()?; + validate_runtime_admission_filesystem(path, false)?; + let store = SingleFileMessagePackBackingStore::new(path); + let expected = store.pull_all_read_only_snapshot()?; + if !expected.is_empty() { + let existing = decode_runtime_traffic_admission_snapshot(&expected)?; + require_supported_runtime_admission_pair(&existing.daemon_id, path)?; + revoke_runtime_traffic_admission(path)?; + return Err(anyhow!( + "runtime traffic admission must be revoked before fresh publication" + )); + } + let envelope = CultCacheEnvelope { + key: record.daemon_id.clone(), + r#type: IdunnRuntimeTrafficAdmissionRecord::TYPE.into(), + payload: rmp_serde::to_vec(record)?, + stored_at: timestamp()?, + schema_id: Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into()), + }; + if !store.compare_exchange_snapshot(&expected, &[envelope])? { + revoke_runtime_traffic_admission(path)?; + return Err(anyhow!( + "runtime traffic admission changed during atomic publication" + )); + } + let verified = (|| -> Result<()> { + seal_runtime_admission_file(path)?; + validate_runtime_admission_filesystem(path, true)?; + let sealed = store.with_read_only_shared_snapshot(|entries| { + decode_runtime_traffic_admission_snapshot(&entries) + })?; + if &sealed != record { + return Err(anyhow!( + "sealed runtime traffic admission differs from the validated candidate" + )); + } + validate_runtime_admission_filesystem(path, true)?; + Ok(()) + })(); + if let Err(error) = verified { + let revoke = revoke_runtime_traffic_admission(path); + return match revoke { + Ok(()) => Err(error.context( + "runtime traffic admission publication failed and the candidate was revoked", + )), + Err(revoke_error) => Err(anyhow!( + "runtime traffic admission publication failed: {error:#}; fail-closed revocation also failed: {revoke_error:#}" + )), + }; + } + Ok(()) +} + +fn revoke_runtime_traffic_admission(path: &Path) -> Result<()> { + let _ = runtime_admission_policy_for_path(path)?; + #[cfg(unix)] + require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; + let store = SingleFileMessagePackBackingStore::new(path); + for _ in 0..8 { + let expected = store.pull_all_read_only_snapshot()?; + if expected.is_empty() { + seal_runtime_admission_file(path)?; + return Ok(()); + } + if store.compare_exchange_snapshot(&expected, &[])? { + seal_runtime_admission_file(path)?; + return Ok(()); + } + } + Err(anyhow!( + "runtime traffic admission could not be revoked after repeated contention" + )) +} + +fn runtime_admission_lock_path(path: &Path) -> PathBuf { + let mut name = path + .file_name() + .map(|value| value.to_os_string()) + .unwrap_or_else(|| "traffic-admission.cc".into()); + name.push(".lock"); + path.with_file_name(name) +} + +fn runtime_admission_custody_paths(path: &Path) -> [PathBuf; 2] { + [path.to_path_buf(), runtime_admission_lock_path(path)] +} + +#[cfg(unix)] +fn runtime_admission_group_id(group_name: &str) -> Result { + use std::ffi::CString; + + let group = CString::new(group_name).context("runtime traffic admission group contains NUL")?; + let entry = unsafe { libc::getgrnam(group.as_ptr()) }; + if entry.is_null() { + return Err(anyhow!( + "required runtime traffic admission group {} does not exist", + group_name + )); + } + Ok(unsafe { (*entry).gr_gid }) +} + +#[cfg(unix)] +fn validate_runtime_admission_filesystem(path: &Path, require_sealed_leaf: bool) -> Result<()> { + use std::os::unix::fs::MetadataExt; + + let (_, group_name) = runtime_admission_policy_for_path(path)?; + let group_id = runtime_admission_group_id(group_name)?; + let parent = path + .parent() + .ok_or_else(|| anyhow!("runtime traffic admission path has no parent"))?; + let mut component = Some(parent); + while let Some(directory) = component { + let metadata = fs::symlink_metadata(directory) + .with_context(|| format!("inspecting {}", directory.display()))?; + if metadata.file_type().is_symlink() || !metadata.is_dir() { + return Err(anyhow!( + "runtime traffic admission parent {} is not a direct directory", + directory.display() + )); + } + let mode = metadata.mode() & 0o7777; + if metadata.uid() != 0 || mode & 0o022 != 0 { + return Err(anyhow!( + "runtime traffic admission parent {} must be root-owned and not group/world writable", + directory.display() + )); + } + if directory == parent && (metadata.gid() != group_id || mode != 0o750) { + return Err(anyhow!( + "runtime traffic admission directory must be root:{} mode 0750", + group_name + )); + } + component = directory.parent(); + } + + for custody_path in runtime_admission_custody_paths(path) { + validate_runtime_admission_leaf(&custody_path, group_id, group_name, require_sealed_leaf)?; + } + Ok(()) +} + +#[cfg(unix)] +fn validate_runtime_admission_leaf( + path: &Path, + group_id: u32, + group_name: &str, + require_sealed_leaf: bool, +) -> Result<()> { + use std::os::unix::fs::MetadataExt; + + match fs::symlink_metadata(path) { + Ok(metadata) => { + if metadata.file_type().is_symlink() || !metadata.is_file() { + return Err(anyhow!( + "runtime traffic admission custody leaf {} must be a direct regular file", + path.display() + )); + } + let mode = metadata.mode() & 0o7777; + if metadata.uid() != 0 || mode & 0o022 != 0 { + return Err(anyhow!( + "runtime traffic admission custody leaf {} must be root-owned and not group/world writable", + path.display() + )); + } + if require_sealed_leaf && (metadata.gid() != group_id || mode != 0o640) { + return Err(anyhow!( + "runtime traffic admission custody leaf {} must be root:{} mode 0640", + path.display(), + group_name, + )); + } + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound && !require_sealed_leaf => {} + Err(error) => { + return Err(error).with_context(|| { + format!("inspecting runtime traffic admission {}", path.display()) + }); + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn validate_runtime_admission_filesystem(_path: &Path, _require_sealed_leaf: bool) -> Result<()> { + Err(anyhow!( + "runtime traffic admission publication and validation require Unix ownership semantics" + )) +} + +#[cfg(unix)] +fn seal_runtime_admission_file(path: &Path) -> Result<()> { + let (_, group_name) = runtime_admission_policy_for_path(path)?; + let group_id = runtime_admission_group_id(group_name)?; + for custody_path in runtime_admission_custody_paths(path) { + seal_runtime_admission_leaf(&custody_path, group_id)?; + } + let parent = path + .parent() + .ok_or_else(|| anyhow!("runtime traffic admission path has no parent"))?; + File::open(parent) + .and_then(|directory| directory.sync_all()) + .with_context(|| format!("syncing admission parent {}", parent.display()))?; + Ok(()) +} + +#[cfg(unix)] +fn seal_runtime_admission_leaf(path: &Path, group_id: u32) -> Result<()> { + use std::os::fd::AsRawFd; + use std::os::unix::fs::PermissionsExt; + + let file = fs::OpenOptions::new() + .read(true) + .write(true) + .open(path) + .with_context(|| format!("opening admission {} for sealing", path.display()))?; + if unsafe { libc::fchown(file.as_raw_fd(), 0, group_id) } != 0 { + return Err(std::io::Error::last_os_error()) + .with_context(|| format!("setting owner on {}", path.display())); + } + file.set_permissions(fs::Permissions::from_mode(0o640)) + .with_context(|| format!("sealing mode on {}", path.display()))?; + file.sync_all() + .with_context(|| format!("syncing sealed admission {}", path.display()))?; + Ok(()) +} + +#[cfg(not(unix))] +fn seal_runtime_admission_file(_path: &Path) -> Result<()> { + Err(anyhow!( + "runtime traffic admission publication requires Unix ownership semantics" + )) +} + fn validate_release_authority_at_privileged_boundary( options: &ReleaseAuthorityValidationOptions, ) -> Result<()> { @@ -817,6 +1733,23 @@ fn validate_release_authority_at_privileged_boundary( Ok(()) } +fn validate_minimum_source_revision_at_privileged_boundary( + options: &MinimumSourceRevisionValidationOptions, +) -> Result<()> { + let policy = compiled_minimum_source_revision_policy_for_daemon(&options.daemon_id) + .ok_or_else(|| anyhow!("daemon has no compiled minimum-source policy"))?; + require_minimum_source_revision( + Path::new(policy.repository_path), + &options.source_revision, + policy, + )?; + println!( + "compiled minimum source revision validated daemon={} sourceRevision={} floor={}", + options.daemon_id, options.source_revision, policy.minimum_source_revision + ); + Ok(()) +} + fn run_swarm( options: &SwarmOptions, common: &CommonOptions, @@ -965,6 +1898,23 @@ fn validate_targets(targets: &[DaemonTarget]) -> Result<()> { target.daemon_id )); } + if let Some(release) = target.release.as_ref() { + let compiled = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id); + let upstream_ref = format!("refs/heads/{}", release.upstream_branch); + if release.minimum_source_revision.as_deref() + != compiled.map(|policy| policy.minimum_source_revision) + || compiled.is_some_and(|policy| { + release.repository_full_name != policy.repository_full_name + || upstream_ref != policy.upstream_ref + || release.repo_path != PathBuf::from(policy.repository_path) + }) + { + issues.push(format!( + "{} differs from its compiled minimum-source policy", + target.daemon_id + )); + } + } } if issues.is_empty() { @@ -1801,6 +2751,217 @@ fn validate_commit_sha(value: &str) -> Result<()> { Ok(()) } +fn compiled_minimum_source_revision_policy( + repository_full_name: &str, + upstream_ref: &str, +) -> Option<&'static CompiledMinimumSourceRevisionPolicy> { + COMPILED_MINIMUM_SOURCE_REVISION_POLICIES + .iter() + .find(|policy| { + policy.repository_full_name == repository_full_name + && policy.upstream_ref == upstream_ref + }) +} + +fn compiled_minimum_source_revision_policy_for_daemon( + daemon_id: &str, +) -> Option<&'static CompiledMinimumSourceRevisionPolicy> { + COMPILED_MINIMUM_SOURCE_REVISION_POLICIES + .iter() + .find(|policy| policy.daemon_id == daemon_id) +} + +fn require_release_minimum_source_revision( + target: &DaemonTarget, + release: &ReleaseTarget, + candidate_revision: &str, +) -> Result<()> { + let upstream_ref = format!("refs/heads/{}", release.upstream_branch); + let compiled = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id); + let declared = release.minimum_source_revision.as_deref(); + if declared != compiled.map(|policy| policy.minimum_source_revision) { + return Err(anyhow!( + "release target minimum source policy differs from Idunn's compiled authority" + )); + } + let Some(policy) = compiled else { + return Ok(()); + }; + if release.repository_full_name != policy.repository_full_name + || upstream_ref != policy.upstream_ref + || release.repo_path != PathBuf::from(policy.repository_path) + { + return Err(anyhow!( + "release target identity differs from Idunn's compiled minimum-source policy" + )); + } + require_deployment_source_revision(&release.repo_path, candidate_revision, policy) +} + +fn require_compiled_minimum_source_revision( + daemon_id: &str, + repository_full_name: &str, + upstream_ref: &str, + candidate_revision: &str, +) -> Result> { + let Some(policy) = compiled_minimum_source_revision_policy_for_daemon(daemon_id) else { + return Ok(None); + }; + if repository_full_name != policy.repository_full_name || upstream_ref != policy.upstream_ref { + return Err(anyhow!( + "deployment identity differs from Idunn's compiled minimum-source policy" + )); + } + require_deployment_source_revision( + Path::new(policy.repository_path), + candidate_revision, + policy, + )?; + Ok(Some(policy)) +} + +fn require_minimum_source_revision( + repository_path: &Path, + candidate_revision: &str, + policy: &CompiledMinimumSourceRevisionPolicy, +) -> Result<()> { + require_compiled_repository_origin(repository_path, policy)?; + require_revision_floor_ancestry( + repository_path, + candidate_revision, + policy.minimum_source_revision, + ) +} + +fn require_deployment_source_revision( + repository_path: &Path, + candidate_revision: &str, + policy: &CompiledMinimumSourceRevisionPolicy, +) -> Result<()> { + require_minimum_source_revision(repository_path, candidate_revision, policy)?; + require_canonical_fetched_dag_membership( + repository_path, + candidate_revision, + policy.canonical_fetched_ref, + ) +} + +fn require_compiled_repository_origin( + repository_path: &Path, + policy: &CompiledMinimumSourceRevisionPolicy, +) -> Result<()> { + let output = Command::new("git") + .arg("-C") + .arg(repository_path) + .arg("remote") + .arg("get-url") + .arg("--all") + .arg("origin") + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .with_context(|| { + format!( + "reading compiled-policy repository origin in {}", + repository_path.display() + ) + })?; + if !output.status.success() { + return Err(anyhow!( + "compiled-policy repository has no readable canonical origin" + )); + } + let urls = String::from_utf8(output.stdout) + .context("compiled-policy repository origin is not UTF-8")? + .lines() + .map(str::trim) + .filter(|url| !url.is_empty()) + .collect::>(); + if urls.len() != 1 || urls[0] != policy.canonical_repository_url { + return Err(anyhow!( + "compiled-policy repository origin differs from Idunn's fixed canonical repository" + )); + } + Ok(()) +} + +fn require_revision_floor_ancestry( + repository_path: &Path, + candidate_revision: &str, + minimum_revision: &str, +) -> Result<()> { + validate_commit_sha(candidate_revision)?; + validate_commit_sha(minimum_revision)?; + if git_revision(repository_path, candidate_revision).as_deref() != Some(candidate_revision) { + return Err(anyhow!( + "candidate source revision is absent from the compiled-policy repository" + )); + } + if git_revision(repository_path, minimum_revision).as_deref() != Some(minimum_revision) { + return Err(anyhow!( + "compiled minimum source revision is absent from its fixed repository" + )); + } + let status = Command::new("git") + .arg("-C") + .arg(repository_path) + .arg("merge-base") + .arg("--is-ancestor") + .arg(minimum_revision) + .arg(candidate_revision) + .status() + .with_context(|| { + format!( + "checking compiled minimum source revision in {}", + repository_path.display() + ) + })?; + if !status.success() { + return Err(anyhow!( + "candidate source revision is below or unrelated to Idunn's compiled minimum" + )); + } + Ok(()) +} + +fn require_canonical_fetched_dag_membership( + repository_path: &Path, + candidate_revision: &str, + canonical_fetched_ref: &str, +) -> Result<()> { + validate_commit_sha(candidate_revision)?; + if git_revision(repository_path, candidate_revision).as_deref() != Some(candidate_revision) { + return Err(anyhow!( + "candidate source revision is absent from the compiled-policy repository" + )); + } + if git_revision(repository_path, canonical_fetched_ref).is_none() { + return Err(anyhow!( + "compiled canonical fetched ref is absent from its fixed repository" + )); + } + let canonical_status = Command::new("git") + .arg("-C") + .arg(repository_path) + .arg("merge-base") + .arg("--is-ancestor") + .arg(candidate_revision) + .arg(canonical_fetched_ref) + .status() + .with_context(|| { + format!( + "checking candidate membership in canonical fetched DAG at {}", + repository_path.display() + ) + })?; + if !canonical_status.success() { + return Err(anyhow!( + "candidate source revision is not in Idunn's canonical fetched DAG" + )); + } + Ok(()) +} + fn validate_release_authority_receipt( receipt: &BifrostRepositoryReleaseAuthorityRecord, expected_id: &str, @@ -1880,11 +3041,25 @@ fn authorize_release( .as_ref() .ok_or_else(|| anyhow!("deployment target has no release declaration"))?; let state = SystemReleaseStatePort; + if let Some(policy) = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id) { + let upstream_ref = format!("refs/heads/{}", release.upstream_branch); + if release.repository_full_name != policy.repository_full_name + || upstream_ref != policy.upstream_ref + || release.repo_path != PathBuf::from(policy.repository_path) + || release.minimum_source_revision.as_deref() != Some(policy.minimum_source_revision) + { + return Err(anyhow!( + "release target identity differs from Idunn's compiled minimum-source policy" + )); + } + require_compiled_repository_origin(&release.repo_path, policy)?; + } state.fetch(release)?; let observed_upstream_revision = state.desired_revision(release)?; let upstream_ref = format!("refs/heads/{}", release.upstream_branch); if !release.requires_bifrost_authority { validate_commit_sha(&observed_upstream_revision)?; + require_release_minimum_source_revision(target, release, &observed_upstream_revision)?; return Ok(ReleaseAuthorization { repository_full_name: release.repository_full_name.clone(), upstream_ref, @@ -1914,6 +3089,7 @@ fn authorize_release( selected.source_revision )); } + require_release_minimum_source_revision(target, release, &selected.source_revision)?; Ok(selected) } @@ -1935,6 +3111,12 @@ fn revalidate_deployment_request( store_lock: &Arc>, ) -> Result<()> { validate_commit_sha(&request.source_revision)?; + require_compiled_minimum_source_revision( + &request.daemon_id, + &request.repository_full_name, + &request.upstream_ref, + &request.source_revision, + )?; if !request.requires_bifrost_authority { if !request.release_authority_id.is_empty() || !request.release_authority_envelope_sha256.is_empty() @@ -1992,29 +3174,42 @@ fn read_fresh_daemon_published_health( return Ok(None); }; admission.validate()?; - let Some(statement) = - node.get::(&admission.daemon_id)? + let Some(statement_envelope) = node + .cache() + .get_envelope::(&admission.daemon_id)? else { return Ok(None); }; + let (statement, _) = + decode_generic_signed_health_payload(&statement_envelope.payload)?; + statement.validate()?; let (binding, current_binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; validate_generic_release_binding(node, &admission)?; let now_millis = parse_timestamp_millis(now) .ok_or_else(|| anyhow!("managed health clock is invalid"))?; - let statement_digest = format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(&statement)?) - ); + let statement_digest = + format!("sha256-{:x}", Sha256::digest(&statement_envelope.payload)); if admission.daemon_id != health.daemon_id + || admission.daemon_id != statement.daemon_id || admission.health_contract != health.health_contract + || admission.health_contract != statement.health_contract + || admission.source_runtime_id != statement.source_runtime_id || admission.state != health.state + || admission.state != statement.state + || statement.detail != health.detail + || admission.trust_binding_id != binding.binding_id || admission.trust_binding_sha256 != current_binding_sha256 || admission.signed_health_sha256 != statement_digest || admission.signer_identity_id != statement.signer_identity_id || admission.publisher_incarnation_id != statement.publisher_incarnation_id || admission.publisher_sequence != statement.publisher_sequence || admission.observed_at_unix_millis != statement.observed_at_unix_millis + || admission.release_id != statement.release_id + || admission.release_witness_sha256 != statement.release_witness_sha256 + || admission.source_commit != statement.source_commit + || admission.deployment_id != statement.deployment_id + || admission.activation_witness_sha256 != statement.activation_witness_sha256 || now_millis < admission.admitted_at_unix_millis || now_millis.saturating_sub(admission.observed_at_unix_millis) > u64::from(desired.max_silence_seconds).saturating_mul(1000) @@ -3418,7 +4613,7 @@ fn admit_health_from_rudp_message( } DecodedHealthIngress::AuthenticatedGeneric { health, - statement, + statement_payload, admission, } => { return admit_generic_signed_health( @@ -3426,7 +4621,7 @@ fn admit_health_from_rudp_message( store_lock, admitted_at, health, - statement, + statement_payload, admission, ); } @@ -3551,14 +4746,14 @@ fn compact_authenticated_health_statement_history( "authenticated health admission for {daemon_id} lost its signed statement" ) })?; - let statement: IdunnSignedDaemonHealthRecord = rmp_serde::from_slice(&entry.payload) + let (statement, _) = decode_generic_signed_health_payload(&entry.payload) .context("decoding current signed daemon health")?; statement.validate()?; - let digest = format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(&statement)?) - ); - if statement.daemon_id != *daemon_id || digest != admission.signed_health_sha256 { + let digest = format!("sha256-{:x}", Sha256::digest(&entry.payload)); + if statement.daemon_id != *daemon_id + || digest != admission.signed_health_sha256 + || statement.activation_witness_sha256 != admission.activation_witness_sha256 + { return Err(anyhow!( "authenticated health admission for {daemon_id} does not bind its signed statement" )); @@ -3830,16 +5025,10 @@ fn authenticate_generic_signed_health( admitted_at: &str, ) -> Result<( IdunnDaemonHealthRecord, - IdunnSignedDaemonHealthRecord, + Vec, IdunnAuthenticatedDaemonHealthAdmissionRecord, )> { - let statement: IdunnSignedDaemonHealthRecord = rmp_serde::from_slice(&document.payload) - .context("decoding generic signed daemon health")?; - if rmp_serde::to_vec(&statement)? != document.payload { - return Err(anyhow!( - "signed daemon health payload is not canonical positional MessagePack" - )); - } + let (statement, unsigned_payload) = decode_generic_signed_health_payload(&document.payload)?; statement.validate()?; if document.record_key != statement.daemon_id || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) @@ -3881,9 +5070,6 @@ fn authenticate_generic_signed_health( .as_slice() .try_into() .map_err(|_| anyhow!("signed daemon health signature length is invalid"))?; - let mut unsigned = statement.clone(); - unsigned.signature.clear(); - let unsigned_payload = rmp_serde::to_vec(&unsigned)?; verify_service_identity_signature_with_public_key::< GameCultProviderHealthIdentity, IdunnSignedDaemonHealthPurpose, @@ -3941,9 +5127,16 @@ fn authenticate_generic_signed_health( source_commit: statement.source_commit.clone(), deployment_id: statement.deployment_id.clone(), private_state_exposed: false, + activation_witness_sha256: statement.activation_witness_sha256.clone(), }; admission.validate()?; - Ok((health, statement, admission)) + Ok((health, document.payload.clone(), admission)) +} + +fn decode_generic_signed_health_payload( + payload: &[u8], +) -> Result<(IdunnSignedDaemonHealthRecord, Vec)> { + IdunnSignedDaemonHealthRecord::decode_canonical_signed_payload(payload) } fn load_daemon_health_trust_binding( @@ -3985,7 +5178,7 @@ fn admit_generic_signed_health( store_lock: &Arc>, admitted_at: &str, health: IdunnDaemonHealthRecord, - statement: IdunnSignedDaemonHealthRecord, + statement_payload: Vec, admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, ) -> Result { for _ in 0..8 { @@ -4026,10 +5219,17 @@ fn admit_generic_signed_health( current: expected_statement, }, ]; + let statement_envelope = CultCacheEnvelope { + key: admission.daemon_id.clone(), + r#type: IdunnSignedDaemonHealthRecord::TYPE.into(), + payload: statement_payload.clone(), + stored_at: admitted_at.to_string(), + schema_id: Some(IdunnSignedDaemonHealthRecord::TYPE.into()), + }; let replacements = [ typed_envelope(&health.daemon_id, &health, admitted_at)?, typed_envelope(&admission.daemon_id, &admission, admitted_at)?, - typed_envelope(&admission.daemon_id, &statement, admitted_at)?, + statement_envelope, ]; if SingleFileMessagePackBackingStore::new(&options.store_path) .compare_exchange(&expected, &replacements)? @@ -4140,11 +5340,11 @@ fn decode_health_from_rudp_message( return Err(anyhow!("expected MessagePack raw payload encoding")); } if document.schema_id == SIGNED_DAEMON_HEALTH_SCHEMA_ID { - let (health, statement, admission) = + let (health, statement_payload, admission) = authenticate_generic_signed_health(document, options, admitted_at)?; return Ok(DecodedHealthIngress::AuthenticatedGeneric { health, - statement, + statement_payload, admission, }); } @@ -4595,7 +5795,7 @@ fn release_target( state_migration_command: Option<&str>, zero_downtime_capability: &str, ) -> ReleaseTarget { - ReleaseTarget { + let mut target = ReleaseTarget { repo: repo.to_string(), repository_full_name: format!("GameCult/{repo}"), repo_path, @@ -4609,7 +5809,10 @@ fn release_target( artifact_witness_root: None, requires_bifrost_authority: false, source_change_pathspecs: Vec::new(), - } + minimum_source_revision: None, + }; + refresh_compiled_minimum_source_revision(&mut target); + target } fn release_target_on_branch( @@ -4628,9 +5831,17 @@ fn release_target_on_branch( zero_downtime_capability, ); target.upstream_branch = upstream_branch.to_string(); + refresh_compiled_minimum_source_revision(&mut target); target } +fn refresh_compiled_minimum_source_revision(release: &mut ReleaseTarget) { + let upstream_ref = format!("refs/heads/{}", release.upstream_branch); + release.minimum_source_revision = + compiled_minimum_source_revision_policy(&release.repository_full_name, &upstream_ref) + .map(|policy| policy.minimum_source_revision.to_string()); +} + fn requiring_bifrost_authority(mut release: ReleaseTarget) -> ReleaseTarget { release.requires_bifrost_authority = true; release @@ -4689,16 +5900,21 @@ fn release_target_record( .as_ref() .map(|value| value.source_revision.clone()) .unwrap_or_else(|| observed_upstream_revision.clone()); + let minimum_source_revision_satisfied = desired_revision != "unknown" + && require_release_minimum_source_revision(target, release, &desired_revision).is_ok(); let status = if fetch_result.is_ok() && observed_upstream_revision != "unknown" && (release.deployed_revision_witness.is_none() || deployed_revision != "unknown") && (!release.requires_bifrost_authority || authorization.is_some()) + && minimum_source_revision_satisfied { if release.requires_bifrost_authority { "tracked-authorized" } else { "tracked-source-authority" } + } else if !minimum_source_revision_satisfied { + "below-compiled-minimum-source-revision" } else { "release-authority-unavailable" }; @@ -4743,6 +5959,7 @@ fn release_target_record( .to_string(), requires_bifrost_authority: release.requires_bifrost_authority, observed_upstream_revision, + minimum_source_revision: release.minimum_source_revision.clone(), } } @@ -4874,7 +6091,7 @@ fn rollout_plan_record( } } -fn git_revision(repo_path: &PathBuf, rev: &str) -> Option { +fn git_revision(repo_path: &Path, rev: &str) -> Option { let output = Command::new("git") .arg("-C") .arg(repo_path) @@ -5427,7 +6644,7 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { let yggdrasil_actuator = |action: &str, target: &str| { if action == "deploy" { format!( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy {target} \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy {target} \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" ) } else { format!("sudo -n /usr/local/libexec/idunn-yggdrasil {action} {target}") @@ -5479,7 +6696,7 @@ fn swarm_targets(options: &SwarmOptions) -> Result> { release_target_on_branch( "CodexConnector", PathBuf::from("/srv/build/CodexConnector"), - "main", + "codex/ghostlight-release-binding", "restart-after-verified-build", None, "restart-required", @@ -5971,9 +7188,18 @@ impl Options { let release_authority_validation = args .first() .is_some_and(|arg| arg == "validate-release-authority"); + let minimum_source_revision_validation = args + .first() + .is_some_and(|arg| arg == "validate-minimum-source-revision"); + if minimum_source_revision_validation { + validate_minimum_source_revision_cli_shape(&args)?; + } let health_admission_validation = args .first() .is_some_and(|arg| arg == "validate-health-admission"); + let runtime_admission_validation = args + .first() + .is_some_and(|arg| arg == "validate-runtime-admission"); let lifecycle_action = args.first().and_then(|arg| match arg.as_str() { "restart" | "request-restart" => Some(LifecycleAction::Restart), "redeploy" | "request-redeploy" | "deploy" | "request-deploy" => { @@ -6015,10 +7241,19 @@ impl Options { let mut validation_envelope_sha256 = None; let mut validation_release_id = None; let mut validation_release_witness_sha256 = None; + let mut validation_activation_witness_sha256 = None; let mut validation_deployment_request_id = None; + let mut validation_input = None; + let mut write_admission_witness = None; + let mut validation_runtime_process_id = None; + let mut validation_runtime_process_starttime_ticks = None; let mut args = args.into_iter().peekable(); - if lifecycle_action.is_some() || release_authority_validation || health_admission_validation + if lifecycle_action.is_some() + || release_authority_validation + || minimum_source_revision_validation + || health_admission_validation + || runtime_admission_validation { let _ = args.next(); } @@ -6071,10 +7306,37 @@ impl Options { validation_release_witness_sha256 = Some(take_value(&mut args, "--release-witness-sha256")?) } + "--activation-witness-sha256" => { + validation_activation_witness_sha256 = + Some(take_value(&mut args, "--activation-witness-sha256")?) + } "--deployment-request-id" => { validation_deployment_request_id = Some(take_value(&mut args, "--deployment-request-id")?) } + "--input" => { + validation_input = Some(PathBuf::from(take_value(&mut args, "--input")?)) + } + "--write-admission-witness" => { + write_admission_witness = Some(PathBuf::from(take_value( + &mut args, + "--write-admission-witness", + )?)) + } + "--runtime-process-id" => { + validation_runtime_process_id = Some( + take_value(&mut args, "--runtime-process-id")? + .parse::() + .context("--runtime-process-id must be a nonzero u32")?, + ) + } + "--runtime-process-starttime-ticks" => { + validation_runtime_process_starttime_ticks = Some( + take_value(&mut args, "--runtime-process-starttime-ticks")? + .parse::() + .context("--runtime-process-starttime-ticks must be a nonzero u64")?, + ) + } "--daemon" => daemon_id = Some(take_value(&mut args, "--daemon")?), "--verse" => verse_id = take_value(&mut args, "--verse")?, "--name" => name = Some(take_value(&mut args, "--name")?), @@ -6196,6 +7458,40 @@ impl Options { command_timeout_seconds, }; + if validation_input.is_some() && !runtime_admission_validation { + return Err(anyhow!("--input belongs to validate-runtime-admission")); + } + if write_admission_witness.is_some() && !health_admission_validation { + return Err(anyhow!( + "--write-admission-witness belongs to validate-health-admission" + )); + } + let runtime_process = match ( + validation_runtime_process_id, + validation_runtime_process_starttime_ticks, + ) { + (Some(process_id), Some(starttime_ticks)) => Some( + RuntimeProcessObservation { + process_id, + starttime_ticks, + } + .validate()?, + ), + (None, None) => None, + _ => { + return Err(anyhow!( + "--runtime-process-id and --runtime-process-starttime-ticks must be supplied together" + )); + } + }; + let runtime_process_lane = runtime_admission_validation + || (health_admission_validation && write_admission_witness.is_some()); + if runtime_process.is_some() && !runtime_process_lane { + return Err(anyhow!( + "runtime process identity belongs only to runtime traffic issuance or validation" + )); + } + if release_authority_validation { let store_path = common.release_authority_store_path.clone().ok_or_else(|| { anyhow!("release authority validation requires --release-authority-store") @@ -6223,7 +7519,39 @@ impl Options { }); } + if minimum_source_revision_validation { + return Ok(Self { + common, + mode: Mode::MinimumSourceRevisionValidation( + MinimumSourceRevisionValidationOptions { + daemon_id: daemon_id.ok_or_else(|| { + anyhow!("minimum source validation requires --daemon") + })?, + source_revision: validation_source_revision.ok_or_else(|| { + anyhow!("minimum source validation requires --source-revision") + })?, + }, + ), + }); + } + if health_admission_validation { + if let Some(output) = write_admission_witness.as_deref() { + let output_daemon = daemon_id + .as_deref() + .ok_or_else(|| anyhow!("health admission validation requires --daemon"))?; + require_supported_runtime_admission_pair(output_daemon, output)?; + if validation_activation_witness_sha256.is_none() { + return Err(anyhow!( + "--write-admission-witness requires --activation-witness-sha256" + )); + } + if runtime_process.is_none() { + return Err(anyhow!( + "--write-admission-witness requires --runtime-process-id and --runtime-process-starttime-ticks" + )); + } + } return Ok(Self { common, mode: Mode::HealthAdmissionValidation(HealthAdmissionValidationOptions { @@ -6241,6 +7569,51 @@ impl Options { source_commit: validation_source_revision.ok_or_else(|| { anyhow!("health admission validation requires --source-revision") })?, + activation_witness_sha256: validation_activation_witness_sha256, + write_admission_witness, + runtime_process, + }), + }); + } + + if runtime_admission_validation { + let input = validation_input + .ok_or_else(|| anyhow!("runtime admission validation requires --input"))?; + let input_daemon = daemon_id + .as_deref() + .ok_or_else(|| anyhow!("runtime admission validation requires --daemon"))?; + require_supported_runtime_admission_pair(input_daemon, &input)?; + let runtime_process = runtime_process.ok_or_else(|| { + anyhow!( + "runtime admission validation requires --runtime-process-id and --runtime-process-starttime-ticks" + ) + })?; + return Ok(Self { + common, + mode: Mode::RuntimeAdmissionValidation(RuntimeAdmissionValidationOptions { + input, + daemon_id: daemon_id + .ok_or_else(|| anyhow!("runtime admission validation requires --daemon"))?, + deployment_request_id: validation_deployment_request_id.ok_or_else(|| { + anyhow!("runtime admission validation requires --deployment-request-id") + })?, + release_id: validation_release_id.ok_or_else(|| { + anyhow!("runtime admission validation requires --release-id") + })?, + release_witness_sha256: validation_release_witness_sha256.ok_or_else(|| { + anyhow!("runtime admission validation requires --release-witness-sha256") + })?, + source_commit: validation_source_revision.ok_or_else(|| { + anyhow!("runtime admission validation requires --source-revision") + })?, + activation_witness_sha256: validation_activation_witness_sha256.ok_or_else( + || { + anyhow!( + "runtime admission validation requires --activation-witness-sha256" + ) + }, + )?, + runtime_process, }), }); } @@ -6339,6 +7712,31 @@ impl Options { } } +fn validate_minimum_source_revision_cli_shape(args: &[String]) -> Result<()> { + if args.len() != 5 { + return Err(anyhow!( + "validate-minimum-source-revision accepts only --daemon and --source-revision" + )); + } + let mut seen = BTreeSet::new(); + for pair in args[1..].chunks_exact(2) { + if pair[1].trim().is_empty() + || !matches!(pair[0].as_str(), "--daemon" | "--source-revision") + || !seen.insert(pair[0].as_str()) + { + return Err(anyhow!( + "validate-minimum-source-revision accepts one --daemon and one --source-revision" + )); + } + } + if !seen.contains("--daemon") || !seen.contains("--source-revision") { + return Err(anyhow!( + "validate-minimum-source-revision requires --daemon and --source-revision" + )); + } + Ok(()) +} + fn take_value(args: &mut impl Iterator, name: &str) -> Result { args.next() .ok_or_else(|| anyhow!("{name} requires a value")) @@ -7013,8 +8411,10 @@ fn help_text() -> &'static str { "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n", " idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n", " idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n", - " idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision \n\n", - "Idunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets that require Bifrost authorization need an explicit CultCache path via --release-authority-store; source-observed targets do not borrow that authority. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." + " idunn validate-minimum-source-revision --daemon --source-revision \n", + " idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision [--activation-witness-sha256 ] [--write-admission-witness --runtime-process-id --runtime-process-starttime-ticks ]\n", + " idunn validate-runtime-admission --input --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision --activation-witness-sha256 --runtime-process-id --runtime-process-starttime-ticks \n\n", + "Idunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets that require Bifrost authorization need an explicit CultCache path via --release-authority-store; source-observed targets do not borrow that authority. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. Runtime traffic admission publication supports exactly yggdrasil-codex-connector at /etc/gamecult/codex-connector/runtime/traffic-admission.cc and yggdrasil-ghostlight at /etc/gamecult/ghostlight-dungeon/runtime/traffic-admission.cc. Each v2 grant requires exact generic warming/traffic-admission-pending health, complete release and activation lineage, and root-observed Linux PID/starttime identity. Idunn holds the Idunn-owned source store and root-owned trust-store shared locks in that fixed order from exact snapshot revalidation through root-owned grant CAS publication; the locks coordinate their respective owners and do not own the decision. It takes a fresh clock and re-observes /proc before and immediately after publication; failure revokes the grant. The grant and its sibling lock are sealed for read-only target snapshots. Ordinary health validation remains active-only, while read-side grant validation requires and re-observes the exact expected process identity. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." ) } @@ -7150,6 +8550,73 @@ mod tests { ); } + #[test] + fn minimum_source_proofs_keep_offline_continuity_independent_of_canonical_drift() { + let root = tempfile::tempdir().unwrap(); + let repo = root.path().to_path_buf(); + let git = |args: &[&str]| { + let status = Command::new("git") + .arg("-C") + .arg(&repo) + .args(args) + .status() + .unwrap(); + assert!(status.success(), "git command failed: {args:?}"); + }; + + git(&["init", "-b", "main"]); + git(&["config", "user.email", "idunn-test@gamecult.org"]); + git(&["config", "user.name", "Idunn Test"]); + fs::write(repo.join("body.txt"), "pre-floor\n").unwrap(); + git(&["add", "."]); + git(&["commit", "-m", "pre-floor"]); + let pre_floor = git_revision(&repo, "HEAD").unwrap(); + + fs::write(repo.join("body.txt"), "floor\n").unwrap(); + git(&["commit", "-am", "floor"]); + let floor = git_revision(&repo, "HEAD").unwrap(); + fs::write(repo.join("body.txt"), "candidate\n").unwrap(); + git(&["commit", "-am", "candidate"]); + let candidate = git_revision(&repo, "HEAD").unwrap(); + git(&["update-ref", "refs/remotes/origin/canonical", &candidate]); + const OFFLINE_ORIGIN: &str = "https://127.0.0.1:9/idunn-offline-proof.git"; + git(&["remote", "add", "origin", OFFLINE_ORIGIN]); + let floor_for_policy: &'static str = Box::leak(floor.clone().into_boxed_str()); + let policy = CompiledMinimumSourceRevisionPolicy { + daemon_id: "offline-test-daemon", + repository_full_name: "GameCult/OfflineTest", + canonical_repository_url: OFFLINE_ORIGIN, + upstream_ref: "refs/heads/canonical", + canonical_fetched_ref: "refs/remotes/origin/canonical", + repository_path: "/not-used-by-local-proof", + minimum_source_revision: floor_for_policy, + }; + + require_minimum_source_revision(&repo, &candidate, &policy).unwrap(); + require_deployment_source_revision(&repo, &candidate, &policy).unwrap(); + assert!(require_minimum_source_revision(&repo, &pre_floor, &policy).is_err()); + + fs::write(repo.join("body.txt"), "ahead of fetched canonical\n").unwrap(); + git(&["commit", "-am", "ahead of fetched canonical"]); + let not_fetched = git_revision(&repo, "HEAD").unwrap(); + require_minimum_source_revision(&repo, ¬_fetched, &policy).unwrap(); + assert!(require_deployment_source_revision(&repo, ¬_fetched, &policy).is_err()); + + git(&["checkout", "-b", "unrelated", &pre_floor]); + fs::write(repo.join("body.txt"), "unrelated\n").unwrap(); + git(&["commit", "-am", "unrelated"]); + let unrelated = git_revision(&repo, "HEAD").unwrap(); + assert!(require_minimum_source_revision(&repo, &unrelated, &policy).is_err()); + + git(&[ + "remote", + "set-url", + "origin", + "https://github.com/attacker/substitute.git", + ]); + assert!(require_minimum_source_revision(&repo, &candidate, &policy).is_err()); + } + #[test] fn target_actuation_gate_serializes_request_owners() { let gate = Arc::new(TargetActuationGate::new()); @@ -7594,6 +9061,356 @@ mod tests { assert_eq!(validation.authority_id, authority_id); } + #[test] + fn minimum_source_validator_cli_derives_every_authority_input_from_daemon() { + for (daemon_id, expected_floor) in [ + ( + "yggdrasil-codex-connector", + CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION, + ), + ("yggdrasil-ghostlight", GHOSTLIGHT_MINIMUM_SOURCE_REVISION), + ] { + let options = Options::parse( + [ + "validate-minimum-source-revision", + "--daemon", + daemon_id, + "--source-revision", + expected_floor, + ] + .into_iter() + .map(ToString::to_string), + ) + .unwrap(); + let Mode::MinimumSourceRevisionValidation(validation) = options.mode else { + panic!("expected minimum-source validation posture"); + }; + assert_eq!(validation.daemon_id, daemon_id); + assert_eq!(validation.source_revision, expected_floor); + } + + for extra in ["--store", "--repository-full-name", "--upstream-ref"] { + let error = Options::parse( + [ + "validate-minimum-source-revision", + "--daemon", + "yggdrasil-ghostlight", + "--source-revision", + GHOSTLIGHT_MINIMUM_SOURCE_REVISION, + extra, + "caller-selected", + ] + .into_iter() + .map(ToString::to_string), + ) + .unwrap_err() + .to_string(); + assert!(error.contains("accepts only --daemon and --source-revision")); + } + } + + #[test] + fn compiled_minimum_source_policy_is_exactly_two_fixed_authorities() { + assert_eq!(COMPILED_MINIMUM_SOURCE_REVISION_POLICIES.len(), 2); + let connector = + compiled_minimum_source_revision_policy_for_daemon("yggdrasil-codex-connector") + .unwrap(); + assert_eq!(connector.repository_full_name, "GameCult/CodexConnector"); + assert_eq!( + connector.canonical_repository_url, + "https://github.com/GameCult/CodexConnector.git" + ); + assert_eq!( + connector.upstream_ref, + "refs/heads/codex/ghostlight-release-binding" + ); + assert_eq!( + connector.canonical_fetched_ref, + "refs/remotes/origin/codex/ghostlight-release-binding" + ); + assert_eq!(connector.repository_path, "/srv/build/CodexConnector"); + assert_eq!( + connector.minimum_source_revision, + "68fe94b9475823de92ed766a97ea962d48723708" + ); + + let ghostlight = + compiled_minimum_source_revision_policy_for_daemon("yggdrasil-ghostlight").unwrap(); + assert_eq!(ghostlight.repository_full_name, "GameCult/Ghostlight"); + assert_eq!( + ghostlight.canonical_repository_url, + "https://github.com/GameCult/Ghostlight.git" + ); + assert_eq!( + ghostlight.upstream_ref, + "refs/heads/codex/ghostlight-dungeon-mvp" + ); + assert_eq!( + ghostlight.canonical_fetched_ref, + "refs/remotes/origin/codex/ghostlight-dungeon-mvp" + ); + assert_eq!(ghostlight.repository_path, "/srv/build/Ghostlight"); + assert_eq!( + ghostlight.minimum_source_revision, + "6bb686981050e048c070376e7f7ac86d177e8f18" + ); + assert!(compiled_minimum_source_revision_policy_for_daemon("yggdrasil-odin").is_none()); + } + + fn runtime_admission_cli_args(command: &str) -> Vec { + vec![ + command.into(), + "--daemon".into(), + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(), + "--deployment-request-id".into(), + "deploy-connector-1".into(), + "--release-id".into(), + "release-connector-1".into(), + "--release-witness-sha256".into(), + format!("sha256-{}", "a".repeat(64)), + "--source-revision".into(), + "b".repeat(40), + "--activation-witness-sha256".into(), + format!("sha256-{}", "c".repeat(64)), + "--runtime-process-id".into(), + "4101".into(), + "--runtime-process-starttime-ticks".into(), + "98765".into(), + ] + } + + #[test] + fn parser_keeps_runtime_admission_issuance_on_the_fixed_probation_lane() { + let mut args = runtime_admission_cli_args("validate-health-admission"); + args.extend([ + "--store".into(), + "/var/lib/gamecult/idunn/idunn.keepalive.cc".into(), + "--write-admission-witness".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let options = Options::parse(args.into_iter()).unwrap(); + let Mode::HealthAdmissionValidation(validation) = options.mode else { + panic!("expected health admission validation posture"); + }; + assert_eq!( + validation.write_admission_witness.as_deref(), + Some(Path::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH)) + ); + assert!(validation.activation_witness_sha256.is_some()); + assert_eq!( + validation.runtime_process, + Some(RuntimeProcessObservation { + process_id: 4_101, + starttime_ticks: 98_765, + }) + ); + + let mut ghostlight = runtime_admission_cli_args("validate-health-admission"); + ghostlight[2] = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); + ghostlight.extend([ + "--write-admission-witness".into(), + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let ghostlight = Options::parse(ghostlight.into_iter()).unwrap(); + let Mode::HealthAdmissionValidation(ghostlight) = ghostlight.mode else { + panic!("expected Ghostlight health admission validation posture"); + }; + assert_eq!( + ghostlight.write_admission_witness.as_deref(), + Some(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)) + ); + + let mut missing_activation = runtime_admission_cli_args("validate-health-admission"); + missing_activation.drain(11..13); + missing_activation.extend([ + "--write-admission-witness".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let error = Options::parse(missing_activation.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("requires --activation-witness-sha256")); + + let mut missing_process = runtime_admission_cli_args("validate-health-admission"); + missing_process.drain(13..17); + missing_process.extend([ + "--write-admission-witness".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let error = Options::parse(missing_process.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("requires --runtime-process-id")); + + let mut caller_selected = runtime_admission_cli_args("validate-health-admission"); + caller_selected.extend([ + "--write-admission-witness".into(), + "/tmp/traffic-admission.cc".into(), + ]); + let error = Options::parse(caller_selected.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("two exact supported absolute paths")); + + let mut other_daemon = runtime_admission_cli_args("validate-health-admission"); + other_daemon[2] = "yggdrasil-other-daemon".into(); + other_daemon.extend([ + "--write-admission-witness".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let error = Options::parse(other_daemon.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("daemon/path pair is unsupported")); + + let error = + Options::parse(runtime_admission_cli_args("validate-health-admission").into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("belongs only to runtime traffic issuance or validation")); + } + + #[test] + fn runtime_admission_policy_is_exactly_two_non_substitutable_pairs() { + assert_eq!( + runtime_admission_policy_for_path(Path::new( + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH + )) + .unwrap(), + ( + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP + ) + ); + assert_eq!( + runtime_admission_policy_for_path(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)) + .unwrap(), + ( + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP + ) + ); + assert!( + require_supported_runtime_admission_pair( + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) + ) + .is_err() + ); + assert!( + require_supported_runtime_admission_pair( + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, + Path::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) + ) + .is_err() + ); + assert!( + runtime_admission_policy_for_path(Path::new( + "/etc/gamecult/ghostlight-dungeon/runtime/./traffic-admission.cc" + )) + .is_err() + ); + assert_eq!( + runtime_admission_lock_path(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)), + PathBuf::from(format!( + "{}.lock", + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH + )) + ); + assert_eq!( + runtime_admission_custody_paths(Path::new( + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH + )), + [ + PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH), + PathBuf::from(format!( + "{}.lock", + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH + )), + ] + ); + } + + #[cfg(unix)] + #[test] + fn sealed_runtime_admission_custody_refuses_a_missing_sibling_lock() { + let root = tempfile::tempdir().unwrap(); + let data_path = root.path().join("traffic-admission.cc"); + let lock_path = runtime_admission_lock_path(&data_path); + let error = validate_runtime_admission_leaf(&lock_path, 0, "test", true) + .unwrap_err() + .to_string(); + + assert!(error.contains("inspecting runtime traffic admission")); + assert!(error.contains("traffic-admission.cc.lock")); + } + + #[test] + fn parser_exposes_read_only_runtime_admission_validation() { + let mut args = runtime_admission_cli_args("validate-runtime-admission"); + args.extend([ + "--input".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let options = Options::parse(args.into_iter()).unwrap(); + let Mode::RuntimeAdmissionValidation(validation) = options.mode else { + panic!("expected runtime admission validation posture"); + }; + assert_eq!( + validation.input, + PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) + ); + assert_eq!( + validation.daemon_id, + IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY + ); + assert_eq!(validation.runtime_process.process_id, 4_101); + assert_eq!(validation.runtime_process.starttime_ticks, 98_765); + + let mut ghostlight = runtime_admission_cli_args("validate-runtime-admission"); + ghostlight[2] = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); + ghostlight.extend([ + "--input".into(), + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let ghostlight = Options::parse(ghostlight.into_iter()).unwrap(); + let Mode::RuntimeAdmissionValidation(ghostlight) = ghostlight.mode else { + panic!("expected Ghostlight runtime admission validation posture"); + }; + assert_eq!( + ghostlight.daemon_id, + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY + ); + assert_eq!( + ghostlight.input, + PathBuf::from(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) + ); + + let mut substituted = runtime_admission_cli_args("validate-runtime-admission"); + substituted.extend([ + "--input".into(), + GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let error = Options::parse(substituted.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("daemon/path pair is unsupported")); + + let mut missing_process = runtime_admission_cli_args("validate-runtime-admission"); + missing_process.drain(13..17); + missing_process.extend([ + "--input".into(), + CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), + ]); + let error = Options::parse(missing_process.into_iter()) + .unwrap_err() + .to_string(); + assert!(error.contains("requires --runtime-process-id")); + assert!(help_text().contains("validate-runtime-admission")); + assert!(help_text().contains(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)); + assert!(help_text().contains(IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY)); + } + fn target(default_failure_state: &str, deploy_command: Option<&str>) -> DaemonTarget { DaemonTarget { daemon_id: "test-daemon".to_string(), @@ -8418,7 +10235,10 @@ mod tests { .expect("Epiphany deploy command"); assert!(deploy.starts_with("sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany")); assert!(deploy.contains("$IDUNN_SOURCE_COMMIT")); - assert!(deploy.contains("$IDUNN_REQUIRES_BIFROST_AUTHORITY")); + assert!(deploy.contains("$IDUNN_DEPLOYMENT_REQUEST_ID")); + assert!(!deploy.contains("$IDUNN_REPOSITORY_FULL_NAME")); + assert!(!deploy.contains("$IDUNN_UPSTREAM_REF")); + assert!(!deploy.contains("$IDUNN_REQUIRES_BIFROST_AUTHORITY")); let release = epiphany.release.as_ref().expect("Epiphany release target"); assert_eq!(release.repo, "Epiphany"); @@ -8456,12 +10276,21 @@ mod tests { assert_eq!( capstone_17.deploy_command.as_deref(), Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" ) ); - let capstone_17_release = capstone_17.release.as_ref().expect("Capstone 17 release target"); - assert_eq!(capstone_17_release.rollout_strategy, "fresh-container-capstone"); - assert_eq!(capstone_17_release.artifact_strategy, "immutable-docker-image-id"); + let capstone_17_release = capstone_17 + .release + .as_ref() + .expect("Capstone 17 release target"); + assert_eq!( + capstone_17_release.rollout_strategy, + "fresh-container-capstone" + ); + assert_eq!( + capstone_17_release.artifact_strategy, + "immutable-docker-image-id" + ); assert!(capstone_17_release.source_change_pathspecs.is_empty()); assert_eq!( capstone_17_release.artifact_witness_root, @@ -8510,9 +10339,10 @@ mod tests { } #[test] - fn yggdrasil_service_and_root_actuator_advertise_epiphany_release_boundary() { + fn yggdrasil_service_and_root_actuator_hold_the_privileged_release_boundary() { let unit = include_str!("../../../scripts/linux/idunn-yggdrasil.service"); let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); + let sudoers = include_str!("../../../scripts/linux/idunn-yggdrasil.sudoers"); assert!(unit.contains( "--release-authority-store /srv/bifrost/state/repository-release-authority.cc" @@ -8530,7 +10360,15 @@ mod tests { .any(|line| line == "ReadWritePaths=/var/lib/gamecult/idunn-authority") ); assert!(unit.contains("/etc/gamecult/codex-connector")); - assert!(unit.contains("/etc/gamecult/ghostlight")); + assert!( + unit.split_ascii_whitespace() + .any(|entry| entry == "/etc/gamecult/ghostlight-dungeon") + ); + assert!( + !unit + .split_ascii_whitespace() + .any(|entry| entry == "/etc/gamecult/ghostlight") + ); assert!(unit.contains("/etc/systemd/system/codex-connector.service")); assert!(unit.contains("/etc/systemd/system/epiphany-swarm.service")); assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); @@ -8539,27 +10377,90 @@ mod tests { !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") ); assert!(!unit.contains("--trusted-epiphany-health-identity-store")); - assert!(actuator.contains("deploy:epiphany|restart:epiphany|deploy:epiphany-capstone-17")); + assert!(actuator.contains("deploy:voidbot|deploy:epiphany|deploy:epiphany-capstone-17")); assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); - assert!( - actuator.contains("bifrost-persona-feedback) target_requires_bifrost_authority=true") - ); - assert!(actuator.contains( - "epiphany|epiphany-capstone-17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false" - )); - assert!( + assert!(actuator.contains("bifrost-persona-feedback)")); + assert!(actuator.contains("requires_bifrost_authority=true")); + assert!(actuator.contains("repository_full_name=GameCult/Bifrost")); + assert!(actuator.contains("daemon_id=yggdrasil-bifrost-persona-feedback")); + assert!(actuator.contains("deployment-brake-status")); + assert!(actuator.contains("/usr/local/bin/idunn validate-minimum-source-revision")); + assert_eq!( actuator - .contains("caller release-authority mode does not match root policy for $target") + .matches("minimum_source_revision_required=true") + .count(), + 2 ); + assert!(!actuator.contains(CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION)); + assert!(!actuator.contains(GHOSTLIGHT_MINIMUM_SOURCE_REVISION)); + assert!(actuator.contains("--release-id \"$source_commit\"")); + assert!(actuator.contains("--deployment-id \"$deployment_request_id\"")); + assert!(actuator.contains("\"deploy:$daemon_id:\"?*|\"manual:redeploy:$daemon_id:\"?*")); + assert!(actuator.contains("require_root_owned_regular_path \"$manifest\"")); + assert!(actuator.contains("[ -L \"$component\" ]")); + assert!(actuator.contains("[ -f \"$component\" ]")); + assert!(actuator.contains("[ \"$owner\" = 0 ]")); + assert!(actuator.contains("group/world-writable")); + assert!(actuator.contains("exec 9<\"$brake_lock\"")); + assert!(actuator.contains("/usr/bin/flock --shared 9")); + assert!( + actuator.contains( + "generation_lock=/var/lib/gamecult/idunn-authority/idunn-generation.lock" + ) + ); + assert!(actuator.contains("require_root_owned_regular_path \"$generation_lock\"")); + assert!(actuator.contains("exec 8<\"$generation_lock\"")); + assert!(actuator.contains("/usr/bin/flock --shared 8")); + assert!(actuator.contains("run_manifest_while_holding_authority_locks")); + assert!(actuator.contains("/usr/bin/setsid \"$@\" 8<&- 9<&- &")); + assert!(!actuator.contains("/usr/bin/setsid \"$@\" 9<&- &")); + assert!(!actuator.contains("/usr/bin/setsid \"$@\" &")); + assert!(actuator.contains("kill -s \"$1\" \"-$manifest_pid\"")); + assert!(actuator.contains("wait \"$manifest_pid\"")); + assert!(actuator.contains("manifest_descendants_leaked")); + assert!(!actuator.contains("exec \"$manifest\"")); + assert_eq!( + actuator + .matches("require_root_owned_regular_path \"$compose\"") + .count(), + 3 + ); + assert!(actuator.contains("[ \"$#\" -eq 6 ]")); + assert!(actuator.contains("[ \"$#\" -eq 2 ]")); + assert!(!actuator.contains("target_requires_bifrost_authority")); let policy_derivation = actuator - .find("target_requires_bifrost_authority=true") + .find("requires_bifrost_authority=true") .expect("root target policy"); - let caller_mode_branch = actuator + let derived_mode_branch = actuator .find("case \"$requires_bifrost_authority\" in") .expect("authority mode branch"); + let minimum_source_validation = actuator + .find("validate-minimum-source-revision") + .expect("minimum-source boundary"); + let deployment_brake_validation = actuator + .find("deployment-brake-status") + .expect("deployment-brake boundary"); + let manifest_execution = actuator + .find("run_manifest_while_holding_authority_locks") + .expect("manifest execution boundary"); + let generation_lock_acquisition = actuator + .rfind("acquire_idunn_generation_lock") + .expect("generation-lock acquisition"); + let target_dispatch = actuator + .find("case \"$action:$target\" in") + .expect("target dispatch"); assert!( - policy_derivation < caller_mode_branch, - "root target policy must be derived before the authority mode is trusted" + policy_derivation < derived_mode_branch, + "root target policy must be derived before its mode is consumed" + ); + assert!( + deployment_brake_validation < minimum_source_validation + && minimum_source_validation < manifest_execution, + "compiled minimum-source proof must run after brake admission and before actuation" + ); + assert!( + generation_lock_acquisition < target_dispatch, + "generation lock must be acquired before any target dispatch" ); assert!(actuator.contains("IDUNN_SOURCE_COMMIT=\"$source_commit\"")); assert!( @@ -8568,6 +10469,64 @@ mod tests { assert!( actuator.contains("IDUNN_REQUIRES_BIFROST_AUTHORITY=\"$requires_bifrost_authority\"") ); + assert!(sudoers.contains("Cmnd_Alias IDUNN_YGGDRASIL_DEPLOY")); + assert!(sudoers.contains("/usr/local/libexec/idunn-yggdrasil deploy ghostlight *")); + assert!(sudoers.contains("/usr/local/libexec/idunn-yggdrasil restart ghostlight")); + assert!(!sudoers.contains("/usr/local/libexec/idunn-yggdrasil *")); + } + + #[test] + fn yggdrasil_root_actuator_policy_matches_every_typed_release_target() { + let targets = swarm_targets(&SwarmOptions { + profile: "yggdrasil-local".to_string(), + repo_root: PathBuf::from("/srv/odin/source"), + }) + .expect("yggdrasil-local targets"); + let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); + let sudoers = include_str!("../../../scripts/linux/idunn-yggdrasil.sudoers"); + + for target in targets.iter().filter(|target| target.release.is_some()) { + let actuator_name = target + .daemon_id + .strip_prefix("yggdrasil-") + .expect("Yggdrasil daemon id"); + let release = target.release.as_ref().unwrap(); + let policy_marker = format!(" {actuator_name})"); + let policy = actuator + .split_once(policy_marker.as_str()) + .unwrap_or_else(|| panic!("missing root policy for {actuator_name}")) + .1 + .split_once(";;") + .expect("bounded root target policy") + .0; + let expected_daemon = format!("daemon_id={}", target.daemon_id); + let expected_repository = + format!("repository_full_name={}", release.repository_full_name); + let expected_ref = format!("upstream_ref=refs/heads/{}", release.upstream_branch); + let expected_authority = format!( + "requires_bifrost_authority={}", + release.requires_bifrost_authority + ); + let expected_deploy_case = format!("deploy:{actuator_name}"); + let expected_sudo_deploy = + format!("/usr/local/libexec/idunn-yggdrasil deploy {actuator_name} *"); + assert!(policy.contains(expected_daemon.as_str())); + assert!(policy.contains(expected_repository.as_str())); + assert!(policy.contains(expected_ref.as_str())); + assert!(policy.contains(expected_authority.as_str())); + if release.minimum_source_revision.is_some() { + assert!(policy.contains("minimum_source_revision_required=true")); + } else { + assert!(!policy.contains("minimum_source_revision_required=true")); + } + assert!(actuator.contains(expected_deploy_case.as_str())); + assert!(sudoers.contains(expected_sudo_deploy.as_str())); + if target.restart_command.is_some() { + let expected_sudo_restart = + format!("/usr/local/libexec/idunn-yggdrasil restart {actuator_name}"); + assert!(sudoers.contains(expected_sudo_restart.as_str())); + } + } } #[test] @@ -8577,6 +10536,7 @@ mod tests { repo_root: PathBuf::from("/srv/odin/source"), }) .expect("yggdrasil-local targets"); + let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); for (daemon_id, repo, branch, witness, actuator_name, restart_on_missing) in [ ( @@ -8590,7 +10550,7 @@ mod tests { ( "yggdrasil-codex-connector", "CodexConnector", - "main", + "codex/ghostlight-release-binding", "/srv/codex-connector/deploy/deployment.env", "codex-connector", true, @@ -8647,10 +10607,14 @@ mod tests { assert!(target.enabled); } - let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); - assert!(actuator.contains("deploy:odin|restart:odin")); - assert!(actuator.contains("deploy:codex-connector|restart:codex-connector")); - assert!(actuator.contains("deploy:ghostlight|restart:ghostlight")); + assert!(actuator.contains( + "deploy:epiphany-capstone-17|deploy:odin|deploy:codex-connector|deploy:ghostlight" + )); + assert!( + actuator.contains( + "restart:epiphany|restart:odin|restart:codex-connector|restart:ghostlight" + ) + ); assert!(actuator.contains("restart:heimdall")); assert!(actuator.contains("deploy:heimdall")); assert!(actuator.contains("restart:gjallar")); @@ -8931,7 +10895,7 @@ mod tests { assert_eq!( target.deploy_command.as_deref(), Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_REPOSITORY_FULL_NAME\" \"$IDUNN_UPSTREAM_REF\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$IDUNN_REQUIRES_BIFROST_AUTHORITY\"" + "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" ) ); assert!(target.restart_command.is_none()); @@ -9765,6 +11729,7 @@ mod tests { signature_algorithm: "ed25519".into(), signature: Vec::new(), private_state_exposed: false, + activation_witness_sha256: release_bound.then(|| format!("sha256-{}", "f".repeat(64))), }; let unsigned = rmp_serde::to_vec(&statement).unwrap(); statement.signature = signer @@ -9863,6 +11828,33 @@ mod tests { } } + fn legacy_generic_signed_health_fixture() -> (CultNetMessage, CommonOptions, tempfile::TempDir) + { + let (mut message, options, root) = generic_signed_health_fixture(1); + let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { + unreachable!() + }; + let mut legacy: IdunnSignedDaemonHealthRecord = + rmp_serde::from_slice(&document.payload).unwrap(); + legacy.activation_witness_sha256 = None; + legacy.signature.clear(); + let mut unsigned = rmp_serde::to_vec(&legacy).unwrap(); + assert_eq!(&unsigned[..3], &[0xdc, 0, 18]); + assert_eq!(unsigned.pop(), Some(0xc0)); + unsigned[2] = 17; + let signer = open_service_identity_at::( + &root.path().join("provider-health-identity.cc"), + ) + .unwrap(); + legacy.signature = signer + .sign::(&unsigned) + .signature; + document.payload = rmp_serde::to_vec(&legacy).unwrap(); + assert_eq!(document.payload.pop(), Some(0xc0)); + document.payload[2] = 17; + (message, options, root) + } + #[test] fn generic_signed_health_requires_root_binding_and_monotonic_admission() { let (first, options, _root) = generic_signed_health_fixture(1); @@ -9885,6 +11877,7 @@ mod tests { .expect("generic admission"); assert_eq!(admission.publisher_sequence, 1); assert_eq!(admission.trust_binding_id, "root/test-daemon/health"); + assert_eq!(admission.activation_witness_sha256, None); let statement = node .get::(&admission.daemon_id)? .expect("signed statement"); @@ -9950,6 +11943,29 @@ mod tests { assert!(health_from_rudp_message(&forged, &options).is_err()); } + #[test] + fn legacy_generic_health_remains_admissible_without_activation_authority() { + let (message, options, _root) = legacy_generic_signed_health_fixture(); + let raw_payload = match &message { + CultNetMessage::DocumentPutRaw { document, .. } => document.payload.clone(), + _ => unreachable!(), + }; + let lock = Arc::new(Mutex::new(())); + admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); + with_store_node(&options, &lock, |node| { + let admission = node + .get::("test-daemon")? + .expect("legacy generic admission"); + assert_eq!(admission.activation_witness_sha256, None); + assert_eq!( + admission.signed_health_sha256, + format!("sha256-{:x}", Sha256::digest(&raw_payload)) + ); + Ok(()) + }) + .unwrap(); + } + #[test] fn generic_health_replaces_one_daemon_keyed_statement_instead_of_growing_history() { let (first, options, root) = generic_signed_health_fixture(1); @@ -10181,15 +12197,240 @@ mod tests { release_id: "release-test-1".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: Some(format!("sha256-{}", "f".repeat(64))), + write_admission_witness: None, + runtime_process: None, }; validate_health_admission_at(&exact, &options, "2026-07-19T12:00:02Z").unwrap(); - let mut substituted = exact; + let mut substituted = exact.clone(); substituted.release_witness_sha256 = format!("sha256-{}", "f".repeat(64)); let error = validate_health_admission_at(&substituted, &options, "2026-07-19T12:00:02Z") .unwrap_err() .to_string(); assert!(error.contains("does not prove the exact active candidate")); + substituted.release_witness_sha256 = format!("sha256-{}", "a".repeat(64)); + substituted.activation_witness_sha256 = Some(format!("sha256-{}", "e".repeat(64))); + let error = validate_health_admission_at(&substituted, &options, "2026-07-19T12:00:02Z") + .unwrap_err() + .to_string(); + assert!(error.contains("does not prove the exact active candidate")); + + with_store_node(&options, &lock, |node| { + let mut admission = node + .get::("test-daemon")? + .expect("authenticated admission"); + admission.release_id = Some("caller-substituted-release".into()); + node.put(&admission.daemon_id, &admission)?; + Ok(()) + }) + .unwrap(); + let mut forged_expected = exact; + forged_expected.release_id = "caller-substituted-release".into(); + let error = + validate_health_admission_at(&forged_expected, &options, "2026-07-19T12:00:02Z") + .unwrap_err() + .to_string(); + assert!(error.contains("no fresh authenticated health admission")); + } + + fn runtime_traffic_admission_record_fixture() -> IdunnRuntimeTrafficAdmissionRecord { + IdunnRuntimeTrafficAdmissionRecord { + schema_version: IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into(), + daemon_id: IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(), + release_id: "release-connector-1".into(), + release_witness_sha256: format!("sha256-{}", "a".repeat(64)), + source_commit: "b".repeat(40), + deployment_id: "deploy-connector-1".into(), + activation_witness_sha256: format!("sha256-{}", "c".repeat(64)), + signed_health_sha256: format!("sha256-{}", "d".repeat(64)), + publisher_incarnation_id: "connector/boot-7/process-11".into(), + publisher_sequence: 42, + signer_identity_id: "connector-health-signer".into(), + runtime_process_id: 4_101, + runtime_process_starttime_ticks: 98_765, + } + } + + fn runtime_admission_validation_fixture() -> RuntimeAdmissionValidationOptions { + let record = runtime_traffic_admission_record_fixture(); + RuntimeAdmissionValidationOptions { + input: PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH), + daemon_id: record.daemon_id, + deployment_request_id: record.deployment_id, + release_id: record.release_id, + release_witness_sha256: record.release_witness_sha256, + source_commit: record.source_commit, + activation_witness_sha256: record.activation_witness_sha256, + runtime_process: RuntimeProcessObservation { + process_id: record.runtime_process_id, + starttime_ticks: record.runtime_process_starttime_ticks, + }, + } + } + + #[test] + fn runtime_admission_lineage_refuses_stale_grant_replay() { + let record = runtime_traffic_admission_record_fixture(); + let exact = runtime_admission_validation_fixture(); + validate_runtime_admission_lineage(&record, &exact).unwrap(); + + let mut next_deployment = exact; + next_deployment.deployment_request_id = "deploy-connector-2".into(); + next_deployment.activation_witness_sha256 = format!("sha256-{}", "e".repeat(64)); + let error = validate_runtime_admission_lineage(&record, &next_deployment) + .unwrap_err() + .to_string(); + assert!(error.contains("exact expected release tuple")); + + let mut next_process = runtime_admission_validation_fixture(); + next_process.runtime_process.starttime_ticks += 1; + let error = validate_runtime_admission_lineage(&record, &next_process) + .unwrap_err() + .to_string(); + assert!(error.contains("exact expected release tuple")); + } + + #[test] + fn linux_proc_stat_parser_reads_exact_pid_and_field_22() { + let stat = + "4101 (connector worker ) odd) S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 98765 20"; + assert_eq!( + parse_linux_proc_stat_starttime_ticks(stat, 4_101).unwrap(), + 98_765 + ); + assert!(parse_linux_proc_stat_starttime_ticks(stat, 4_102).is_err()); + assert!(parse_linux_proc_stat_starttime_ticks("4101 worker S 1", 4_101).is_err()); + assert!( + parse_linux_proc_stat_starttime_ticks( + "4101 (worker) S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0", + 4_101 + ) + .is_err() + ); + } + + #[test] + fn runtime_admission_mutation_authority_is_root_only() { + require_runtime_admission_writer_uid(0).unwrap(); + let error = require_runtime_admission_writer_uid(1) + .unwrap_err() + .to_string(); + assert!(error.contains("requires root")); + } + + fn warming_authenticated_runtime_admission_fixture() + -> IdunnAuthenticatedDaemonHealthAdmissionRecord { + let grant = runtime_traffic_admission_record_fixture(); + IdunnAuthenticatedDaemonHealthAdmissionRecord { + schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), + daemon_id: grant.daemon_id, + health_contract: "codex-connector.authenticated-health".into(), + source_runtime_id: "codex-connector".into(), + state: "warming".into(), + observed_at_unix_millis: 1_788_200_000_000, + admitted_at_unix_millis: 1_788_200_000_001, + trust_binding_id: "root/yggdrasil-codex-connector/health".into(), + trust_binding_sha256: format!("sha256-{}", "e".repeat(64)), + signer_identity_id: grant.signer_identity_id, + publisher_incarnation_id: grant.publisher_incarnation_id, + publisher_sequence: grant.publisher_sequence, + signed_health_sha256: grant.signed_health_sha256, + release_id: Some(grant.release_id), + release_witness_sha256: Some(grant.release_witness_sha256), + source_commit: Some(grant.source_commit), + deployment_id: Some(grant.deployment_id), + private_state_exposed: false, + activation_witness_sha256: Some(grant.activation_witness_sha256), + } + } + + #[test] + fn runtime_admission_probation_is_warming_pending_only() { + assert!(validate_health_admission_posture("active", "ready", false).is_ok()); + assert!(validate_health_admission_posture("warming", "ready", false).is_err()); + assert!( + validate_health_admission_posture( + "warming", + RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL, + true + ) + .is_ok() + ); + assert!( + validate_health_admission_posture( + "active", + RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL, + true + ) + .is_err() + ); + assert!(validate_health_admission_posture("warming", "ready", true).is_err()); + } + + #[test] + fn runtime_admission_is_derived_only_from_complete_authenticated_lineage() { + let admission = warming_authenticated_runtime_admission_fixture(); + let process = RuntimeProcessObservation { + process_id: 4_101, + starttime_ticks: 98_765, + }; + admission.validate().unwrap(); + assert_eq!( + runtime_traffic_admission_from(&admission, process).unwrap(), + runtime_traffic_admission_record_fixture() + ); + + let mut missing_activation = admission.clone(); + missing_activation.activation_witness_sha256 = None; + assert!(runtime_traffic_admission_from(&missing_activation, process).is_err()); + + let mut active = admission; + active.state = "active".into(); + assert!(runtime_traffic_admission_from(&active, process).is_err()); + + let mut ghostlight = warming_authenticated_runtime_admission_fixture(); + ghostlight.daemon_id = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); + let activation = ghostlight.activation_witness_sha256.clone().unwrap(); + let ghostlight = runtime_traffic_admission_from(&ghostlight, process).unwrap(); + assert_eq!( + ghostlight.daemon_id, + IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY + ); + assert_eq!(ghostlight.activation_witness_sha256, activation); + } + + #[test] + fn runtime_admission_snapshot_is_exact_and_canonical() { + let record = runtime_traffic_admission_record_fixture(); + let envelope = CultCacheEnvelope { + key: record.daemon_id.clone(), + r#type: IdunnRuntimeTrafficAdmissionRecord::TYPE.into(), + payload: rmp_serde::to_vec(&record).unwrap(), + stored_at: "2026-09-02T12:00:00Z".into(), + schema_id: Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into()), + }; + assert_eq!( + decode_runtime_traffic_admission_snapshot(std::slice::from_ref(&envelope)).unwrap(), + record + ); + + let mut wrong_schema = envelope.clone(); + wrong_schema.schema_id = Some("idunn.runtime_traffic_admission.v0".into()); + assert!(decode_runtime_traffic_admission_snapshot(&[wrong_schema]).is_err()); + + let mut invalid_timestamp = envelope.clone(); + invalid_timestamp.stored_at = "caller-selected-time".into(); + assert!(decode_runtime_traffic_admission_snapshot(&[invalid_timestamp]).is_err()); + + let mut named = envelope.clone(); + named.payload = rmp_serde::to_vec_named(&record).unwrap(); + let error = decode_runtime_traffic_admission_snapshot(&[named]) + .unwrap_err() + .to_string(); + assert!(error.contains("canonical positional MessagePack")); + + assert!(decode_runtime_traffic_admission_snapshot(&[envelope.clone(), envelope]).is_err()); } #[test] @@ -10264,6 +12505,7 @@ mod tests { source_commit: None, deployment_id: None, private_state_exposed: false, + activation_witness_sha256: None, }; let mut same_incarnation = existing.clone(); @@ -10339,6 +12581,70 @@ mod tests { (options, lock, desired, source, publisher, root) } + #[test] + fn runtime_issuance_frozen_trust_snapshot_refuses_same_tuple_signer_rotation() { + let (options, _lock, desired, source, _publisher, _root) = projection_fixture(false); + let candidate = RuntimeTrafficIssuanceCandidate { + grant: runtime_traffic_admission_record_fixture(), + desired, + source, + }; + let trust_path = options.daemon_health_trust_store_path.as_ref().unwrap(); + let snapshot = SingleFileMessagePackBackingStore::new(trust_path) + .pull_all_read_only_snapshot() + .unwrap(); + validate_runtime_traffic_issuance_trust_snapshot(&snapshot, &candidate).unwrap(); + + let mut rotated = candidate.source.binding.clone(); + rotated.signer_public_key = vec![29; 32]; + rotated.signer_identity_id = derive_service_identity_id::( + &rotated.signer_public_key, + ) + .unwrap(); + rotated.bound_at_unix_millis += 1; + rotated.validate().unwrap(); + let mut rotated_snapshot = snapshot; + let binding_envelope = rotated_snapshot + .iter_mut() + .find(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) + .unwrap(); + binding_envelope.payload = rmp_serde::to_vec(&rotated).unwrap(); + assert!( + validate_runtime_traffic_issuance_trust_snapshot(&rotated_snapshot, &candidate) + .is_err() + ); + } + + #[test] + fn runtime_issuance_freshness_uses_the_locked_mint_clock() { + let (_options, _lock, desired, source, _publisher, _root) = projection_fixture(false); + let admission = source.admission; + let freshness_limit = + admission.observed_at_unix_millis + u64::from(desired.max_silence_seconds) * 1_000; + validate_runtime_traffic_issuance_freshness( + &desired, + &admission, + ×tamp_from_unix_millis(freshness_limit).unwrap(), + ) + .unwrap(); + assert!( + validate_runtime_traffic_issuance_freshness( + &desired, + &admission, + ×tamp_from_unix_millis(freshness_limit + 1).unwrap(), + ) + .is_err() + ); + assert!( + validate_runtime_traffic_issuance_freshness( + &desired, + &admission, + ×tamp_from_unix_millis(admission.admitted_at_unix_millis - 1).unwrap(), + ) + .is_err() + ); + } + fn public_projection( options: &CommonOptions, ) -> IdunnAuthenticatedProviderHealthProjectionRecord { @@ -11006,6 +13312,9 @@ mod tests { release_id: "release-test".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: None, + write_admission_witness: None, + runtime_process: None, }, &options, "2026-07-16T00:01:02Z", @@ -11039,6 +13348,9 @@ mod tests { release_id: "release-test".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: None, + write_admission_witness: None, + runtime_process: None, }, &options, "2026-07-16T00:01:03Z", @@ -11083,6 +13395,9 @@ mod tests { release_id: "release-test".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: None, + write_admission_witness: None, + runtime_process: None, }, &options, "2026-07-16T00:02:00Z", @@ -11241,6 +13556,9 @@ mod tests { release_id: "release-test".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: None, + write_admission_witness: None, + runtime_process: None, }, &options, "2026-07-16T00:01:03Z", @@ -11293,6 +13611,9 @@ mod tests { release_id: "release-test".into(), release_witness_sha256: format!("sha256-{}", "a".repeat(64)), source_commit: "b".repeat(40), + activation_witness_sha256: None, + write_admission_witness: None, + runtime_process: None, }, &options, ) diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 922d4d9..33612b8 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -41,6 +41,14 @@ pub fn run(args: impl IntoIterator) -> Result<()> { let command = args.next().ok_or_else(|| anyhow!(usage()))?; let options = parse_options(args)?; match command.as_str() { + "enroll-provider-health-identity" => { + let public_key = enroll_provider_health_identity(&options)?; + println!("{public_key}"); + } + "provider-health-public-key" => { + let public_key = provider_health_public_key(&options)?; + println!("{public_key}"); + } "enroll-idunn-identity" => { require_only(&options, &["private-store"])?; enroll_service_identity_at::(&path(&options, "private-store")?)?; @@ -76,6 +84,7 @@ pub fn run(args: impl IntoIterator) -> Result<()> { "deployment-brake-status" => deployment_brake_status(&options)?, "create-daemon-health-trust-binding" => create_health_binding(&options)?, "add-daemon-health-trust-binding" => add_health_binding(&options)?, + "require-daemon-health-release-binding" => require_health_binding_release(&options)?, "rotate-daemon-health-trust-signer" => rotate_health_binding_signer(&options)?, "validate-daemon-health-trust-binding" => { require_only(&options, &["input"])?; @@ -88,6 +97,24 @@ pub fn run(args: impl IntoIterator) -> Result<()> { Ok(()) } +fn enroll_provider_health_identity(options: &BTreeMap) -> Result { + require_only(options, &["private-store"])?; + let signer = enroll_service_identity_at::(&path( + options, + "private-store", + )?)?; + encode_public_key(&signer.entry().public_key) +} + +fn provider_health_public_key(options: &BTreeMap) -> Result { + require_only(options, &["private-store"])?; + let signer = open_service_identity_at::(&path( + options, + "private-store", + )?)?; + encode_public_key(&signer.entry().public_key) +} + fn deployment_brake_engage(options: &BTreeMap) -> Result<()> { require_only( options, @@ -191,11 +218,24 @@ fn deployment_brake_status(options: &BTreeMap) -> Result<()> { "now-unix-millis", ], )?; + let exact_actuation_fields = ["release-id", "deployment-id", "now-unix-millis"]; + let exact_actuation_field_count = exact_actuation_fields + .into_iter() + .filter(|field| options.contains_key(*field)) + .count(); + if exact_actuation_field_count != 0 + && exact_actuation_field_count != exact_actuation_fields.len() + { + bail!("exact deployment-brake actuation validation requires release, deployment, and time"); + } let record = read_brake(&path(options, "store")?)?; if record.runtime_id != required(options, "runtime-id")? { bail!("deployment brake belongs to another runtime") } if record.status == "engaged" { + if exact_actuation_field_count != 0 { + bail!("engaged deployment brake denies exact actuation validation"); + } println!( "engaged runtime={} scope={} owner={} reason={}", record.runtime_id, record.scope, record.updated_by, record.reason @@ -288,11 +328,19 @@ fn read_operator_anchor(path: &Path) -> Result { fn validate_health_binding_store(path: &Path) -> Result<()> { let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + validated_health_bindings(&entries)?; + Ok(()) +} + +fn validated_health_bindings( + entries: &[CultCacheEnvelope], +) -> Result> { if entries.is_empty() { bail!("daemon health trust store is empty"); } let mut keys = BTreeSet::new(); let mut tuples = BTreeSet::new(); + let mut bindings = Vec::with_capacity(entries.len()); for envelope in entries { if envelope.r#type != IdunnDaemonHealthTrustBindingRecord::TYPE || envelope.schema_id.as_deref() != Some(IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA) @@ -307,15 +355,16 @@ fn validate_health_binding_store(path: &Path) -> Result<()> { binding.validate()?; if !keys.insert(binding.binding_id.clone()) || !tuples.insert(( - binding.daemon_id, - binding.health_contract, - binding.source_runtime_id, + binding.daemon_id.clone(), + binding.health_contract.clone(), + binding.source_runtime_id.clone(), )) { bail!("trust store contains a duplicate binding id or tuple"); } + bindings.push(binding); } - Ok(()) + Ok(bindings) } fn create_health_binding(options: &BTreeMap) -> Result<()> { @@ -431,6 +480,91 @@ fn add_health_binding(options: &BTreeMap) -> Result<()> { Ok(()) } +fn require_health_binding_release(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "store", + "binding-id", + "daemon", + "health-contract", + "source-runtime", + "signer-public-key-hex", + ], + )?; + let store_path = path(options, "store")?; + let store = SingleFileMessagePackBackingStore::new(&store_path); + let entries = store.pull_all_read_only_snapshot()?; + let signer_public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; + require_health_binding_release_from_snapshot( + &store, + entries, + required(options, "binding-id")?, + required(options, "daemon")?, + required(options, "health-contract")?, + required(options, "source-runtime")?, + &signer_public_key, + ) +} + +fn require_health_binding_release_from_snapshot( + store: &SingleFileMessagePackBackingStore, + entries: Vec, + binding_id: &str, + daemon_id: &str, + health_contract: &str, + source_runtime_id: &str, + signer_public_key: &[u8], +) -> Result<()> { + let bindings = validated_health_bindings(&entries)?; + let target_index = bindings + .iter() + .position(|binding| binding.binding_id == binding_id) + .ok_or_else(|| anyhow!("named daemon health trust binding does not exist"))?; + let current = &bindings[target_index]; + if current.daemon_id != daemon_id + || current.health_contract != health_contract + || current.source_runtime_id != source_runtime_id + || current.signer_public_key != signer_public_key + { + bail!("named daemon health trust binding does not match the expected immutable fields"); + } + if current.release_binding_required { + return Ok(()); + } + + let mut next = current.clone(); + next.release_binding_required = true; + next.validate()?; + let mut replacement = entries[target_index].clone(); + replacement.payload = rmp_serde::to_vec(&next)?; + let expected = entries + .into_iter() + .map(|envelope| CultCacheExpectedEnvelope { + r#type: envelope.r#type.clone(), + key: envelope.key.clone(), + current: Some(envelope), + }) + .collect::>(); + if !store.compare_exchange(&expected, &[replacement])? { + let latest_entries = store.pull_all_read_only_snapshot()?; + let latest_bindings = validated_health_bindings(&latest_entries)?; + if let Some(latest) = latest_bindings + .iter() + .find(|binding| binding.binding_id == binding_id) + && latest.daemon_id == daemon_id + && latest.health_contract == health_contract + && latest.source_runtime_id == source_runtime_id + && latest.signer_public_key == signer_public_key + && latest.release_binding_required + { + return Ok(()); + } + bail!("daemon health trust store changed during release-binding transition"); + } + Ok(()) +} + fn rotate_health_binding_signer(options: &BTreeMap) -> Result<()> { require_only( options, @@ -723,6 +857,19 @@ fn decode_public_key(value: &str) -> Result> { .collect() } +fn encode_public_key(value: &[u8]) -> Result { + if value.len() != 32 { + bail!("provider health public key must be exactly 32 bytes"); + } + const HEX: &[u8; 16] = b"0123456789abcdef"; + let mut encoded = String::with_capacity(64); + for byte in value { + encoded.push(HEX[(byte >> 4) as usize] as char); + encoded.push(HEX[(byte & 0x0f) as usize] as char); + } + Ok(encoded) +} + fn refuse_existing(path: &Path, label: &str) -> Result<()> { if path.exists() { bail!( @@ -760,7 +907,7 @@ fn normalized(path: &Path) -> Result { } fn usage() -> &'static str { - "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " + "Usage: idunn-provision enroll-provider-health-identity --private-store \n idunn-provision provider-health-public-key --private-store \n idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision require-daemon-health-release-binding --store --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " } #[cfg(test)] @@ -817,6 +964,50 @@ mod tests { ] } + const TEST_SIGNER_HEX: &str = + "1111111111111111111111111111111111111111111111111111111111111111"; + const WRONG_SIGNER_HEX: &str = + "2222222222222222222222222222222222222222222222222222222222222222"; + + fn write_test_binding( + command: &str, + store: &str, + id: &str, + daemon: &str, + runtime: &str, + ) -> Result<()> { + invoke(&binding_args(command, store, id, daemon, runtime)) + } + + fn require_test_binding( + store: &str, + id: &str, + daemon: &str, + contract: &str, + runtime: &str, + signer: &str, + ) -> Result<()> { + invoke(&[ + "require-daemon-health-release-binding", + "--store", + store, + "--binding-id", + id, + "--daemon", + daemon, + "--health-contract", + contract, + "--source-runtime", + runtime, + "--signer-public-key-hex", + signer, + ]) + } + + fn store_snapshot(path: &Path) -> Result> { + SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot() + } + #[test] fn append_health_binding_preserves_existing_and_rejects_collisions_and_aliens() -> Result<()> { let temp = TempDir::new()?; @@ -891,6 +1082,253 @@ mod tests { Ok(()) } + #[test] + fn require_release_binding_changes_only_the_named_flag_and_is_idempotent() -> Result<()> { + let temp = TempDir::new()?; + let store = temp.path().join("trust.cc"); + let path = store.to_str().unwrap(); + write_test_binding( + "create-daemon-health-trust-binding", + path, + "one", + "daemon-one", + "runtime-one", + )?; + write_test_binding( + "add-daemon-health-trust-binding", + path, + "two", + "daemon-two", + "runtime-two", + )?; + let before = store_snapshot(&store)?; + + require_test_binding( + path, + "one", + "daemon-one", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + )?; + let after = store_snapshot(&store)?; + let before_target = before.iter().find(|entry| entry.key == "one").unwrap(); + let after_target = after.iter().find(|entry| entry.key == "one").unwrap(); + let mut expected: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&before_target.payload)?; + expected.release_binding_required = true; + assert_eq!( + rmp_serde::from_slice::(&after_target.payload)?, + expected + ); + let mut expected_envelope = before_target.clone(); + expected_envelope.payload = after_target.payload.clone(); + assert_eq!(after_target, &expected_envelope); + assert_eq!( + after.iter().find(|entry| entry.key == "two"), + before.iter().find(|entry| entry.key == "two") + ); + + require_test_binding( + path, + "one", + "daemon-one", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + )?; + assert_eq!(store_snapshot(&store)?, after); + require_health_binding_release_from_snapshot( + &SingleFileMessagePackBackingStore::new(&store), + before, + "one", + "daemon-one", + "provider.health", + "runtime-one", + &decode_public_key(TEST_SIGNER_HEX)?, + )?; + Ok(()) + } + + #[test] + fn require_release_binding_refuses_wrong_identity_and_invalid_store_without_writing() + -> Result<()> { + let temp = TempDir::new()?; + let store = temp.path().join("trust.cc"); + let path = store.to_str().unwrap(); + write_test_binding( + "create-daemon-health-trust-binding", + path, + "one", + "daemon-one", + "runtime-one", + )?; + let before = store_snapshot(&store)?; + for (id, daemon, contract, runtime, signer) in [ + ( + "one", + "wrong", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + ), + ("one", "daemon-one", "wrong", "runtime-one", TEST_SIGNER_HEX), + ( + "one", + "daemon-one", + "provider.health", + "wrong", + TEST_SIGNER_HEX, + ), + ( + "one", + "daemon-one", + "provider.health", + "runtime-one", + WRONG_SIGNER_HEX, + ), + ( + "missing", + "daemon-one", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + ), + ] { + assert!(require_test_binding(path, id, daemon, contract, runtime, signer).is_err()); + } + assert!( + invoke(&[ + "require-daemon-health-release-binding", + "--store", + path, + "--binding-id", + "one", + "--daemon", + "daemon-one", + "--health-contract", + "provider.health", + "--signer-public-key-hex", + TEST_SIGNER_HEX, + ]) + .is_err() + ); + assert_eq!(store_snapshot(&store)?, before); + + SingleFileMessagePackBackingStore::new(&store).push(&CultCacheEnvelope { + key: "alien".into(), + r#type: "alien".into(), + payload: vec![], + stored_at: "2026-07-19T20:00:00Z".into(), + schema_id: Some("alien.v0".into()), + })?; + let foreign = store_snapshot(&store)?; + assert!( + require_test_binding( + path, + "one", + "daemon-one", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + ) + .is_err() + ); + assert_eq!(store_snapshot(&store)?, foreign); + + let ambiguous = temp.path().join("ambiguous.cc"); + let ambiguous_path = ambiguous.to_str().unwrap(); + write_test_binding( + "create-daemon-health-trust-binding", + ambiguous_path, + "one", + "daemon-one", + "runtime-one", + )?; + let mut duplicate: IdunnDaemonHealthTrustBindingRecord = + rmp_serde::from_slice(&store_snapshot(&ambiguous)?.remove(0).payload)?; + duplicate.binding_id = "duplicate".into(); + SingleFileMessagePackBackingStore::new(&ambiguous).push(&typed_envelope( + &duplicate.binding_id, + &duplicate, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + duplicate.bound_at_unix_millis, + )?)?; + let ambiguous_before = store_snapshot(&ambiguous)?; + assert!( + require_test_binding( + ambiguous_path, + "one", + "daemon-one", + "provider.health", + "runtime-one", + TEST_SIGNER_HEX, + ) + .is_err() + ); + assert_eq!(store_snapshot(&ambiguous)?, ambiguous_before); + Ok(()) + } + + #[test] + fn require_release_binding_cas_covers_the_whole_validated_store() -> Result<()> { + let temp = TempDir::new()?; + let store_path = temp.path().join("trust.cc"); + let path = store_path.to_str().unwrap(); + write_test_binding( + "create-daemon-health-trust-binding", + path, + "one", + "daemon-one", + "runtime-one", + )?; + write_test_binding( + "add-daemon-health-trust-binding", + path, + "two", + "daemon-two", + "runtime-two", + )?; + let stale = store_snapshot(&store_path)?; + require_test_binding( + path, + "two", + "daemon-two", + "provider.health", + "runtime-two", + TEST_SIGNER_HEX, + )?; + + let store = SingleFileMessagePackBackingStore::new(&store_path); + let error = require_health_binding_release_from_snapshot( + &store, + stale, + "one", + "daemon-one", + "provider.health", + "runtime-one", + &decode_public_key(TEST_SIGNER_HEX)?, + ) + .unwrap_err(); + assert!(error.to_string().contains("changed during release-binding")); + let records = validated_health_bindings(&store_snapshot(&store_path)?)?; + assert!( + !records + .iter() + .find(|binding| binding.binding_id == "one") + .unwrap() + .release_binding_required + ); + assert!( + records + .iter() + .find(|binding| binding.binding_id == "two") + .unwrap() + .release_binding_required + ); + Ok(()) + } + #[test] fn concurrent_health_binding_appends_have_one_cas_winner() -> Result<()> { let temp = TempDir::new()?; @@ -938,6 +1376,106 @@ mod tests { Ok(()) } + #[test] + fn provider_health_identity_enrollment_is_profile_exact_and_immutable() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("provider-health-identity.cc"); + let options = BTreeMap::from([( + "private-store".to_string(), + private.to_string_lossy().into_owned(), + )]); + + let public_key_hex = enroll_provider_health_identity(&options)?; + let signer = open_service_identity_at::(&private)?; + assert_eq!( + public_key_hex, + encode_public_key(&signer.entry().public_key)? + ); + assert_eq!( + decode_public_key(&public_key_hex)?, + signer.entry().public_key + ); + + let entries = + SingleFileMessagePackBackingStore::new(&private).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + panic!("provider health identity store must contain exactly one entry"); + }; + assert_eq!(entry.r#type, GameCultProviderHealthIdentity::PRIVATE_TYPE); + assert_eq!(entry.key, GameCultProviderHealthIdentity::PRIVATE_KEY); + assert_eq!( + entry.schema_id.as_deref(), + Some(GameCultProviderHealthIdentity::PRIVATE_SCHEMA) + ); + assert!(open_service_identity_at::(&private).is_err()); + + let before = std::fs::read(&private)?; + assert!( + invoke(&[ + "enroll-provider-health-identity", + "--private-store", + private.to_str().unwrap(), + ]) + .is_err() + ); + assert_eq!(std::fs::read(&private)?, before); + + let unauthorized = temp.path().join("unauthorized.cc"); + assert!( + invoke(&[ + "enroll-provider-health-identity", + "--private-store", + unauthorized.to_str().unwrap(), + "--release-binding-required", + "true", + ]) + .is_err() + ); + assert!(!unauthorized.exists()); + Ok(()) + } + + #[test] + fn provider_health_public_key_is_read_only_and_rejects_wrong_profile_or_options() -> Result<()> + { + let temp = TempDir::new()?; + let provider = temp.path().join("provider-health.cc"); + let provider_options = BTreeMap::from([( + "private-store".to_string(), + provider.to_string_lossy().into_owned(), + )]); + let expected = enroll_provider_health_identity(&provider_options)?; + let provider_before = std::fs::read(&provider)?; + assert_eq!(provider_health_public_key(&provider_options)?, expected); + assert_eq!(std::fs::read(&provider)?, provider_before); + + let wrong_profile = temp.path().join("idunn.cc"); + enroll_service_identity_at::(&wrong_profile)?; + let wrong_before = std::fs::read(&wrong_profile)?; + assert!( + invoke(&[ + "provider-health-public-key", + "--private-store", + wrong_profile.to_str().unwrap(), + ]) + .is_err() + ); + assert_eq!(std::fs::read(&wrong_profile)?, wrong_before); + + assert!( + invoke(&[ + "provider-health-public-key", + "--private-store", + provider.to_str().unwrap(), + "--release-binding-required", + "true", + ]) + .is_err() + ); + assert_eq!(std::fs::read(&provider)?, provider_before); + Ok(()) + } + #[test] fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { let temp = TempDir::new()?; @@ -1292,6 +1830,33 @@ mod tests { "100", ])?; assert_eq!(read_brake(&store)?.status, "engaged"); + invoke(&[ + "deployment-brake-status", + "--store", + store.to_str().unwrap(), + "--operator-anchor", + anchor.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + ])?; + assert!( + invoke(&[ + "deployment-brake-status", + "--store", + store.to_str().unwrap(), + "--operator-anchor", + anchor.to_str().unwrap(), + "--runtime-id", + "yggdrasil", + "--release-id", + "commit-4", + "--deployment-id", + "request-4", + "--now-unix-millis", + "150", + ]) + .is_err() + ); invoke(&[ "deployment-brake-release", "--store", diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 16edfd8..5371cb4 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -32,7 +32,7 @@ pub const IDUNN_DEPLOYMENT_REQUEST_SCHEMA: &str = "idunn.deployment_request.v2"; pub const IDUNN_CURRENT_DEPLOYMENT_REQUEST_SCHEMA: &str = "idunn.current_deployment_request.v1"; pub const IDUNN_DEPLOYMENT_RESULT_SCHEMA: &str = "idunn.deployment_result.v1"; pub const IDUNN_LIFECYCLE_COMMAND_SCHEMA: &str = "idunn.lifecycle_command.v1"; -pub const IDUNN_RELEASE_TARGET_SCHEMA: &str = "idunn.release_target.v2"; +pub const IDUNN_RELEASE_TARGET_SCHEMA: &str = "idunn.release_target.v3"; pub const IDUNN_DEPLOYMENT_ARTIFACT_SCHEMA: &str = "idunn.deployment_artifact.v2"; pub const BIFROST_REPOSITORY_RELEASE_AUTHORITY_SCHEMA: &str = "bifrost.repository_release_authority.v1"; @@ -50,6 +50,10 @@ pub const IDUNN_RUDP_HEALTH_INGRESS_SCHEMA: &str = "idunn.rudp_health_ingress.v1 pub const IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA: &str = "idunn.daemon_health_trust_binding.v1"; pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = "idunn.authenticated_daemon_health_admission.v1"; +pub const IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA: &str = "idunn.runtime_traffic_admission.v2"; +pub const IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY: &str = + "yggdrasil-codex-connector"; +pub const IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY: &str = "yggdrasil-ghostlight"; pub const IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA: &str = "idunn.unsigned_daemon_health_diagnostic.v1"; pub const MUNINN_TELEMETRY_SURFACE_SCHEMA: &str = "muninn.telemetry_surface.v1"; @@ -432,6 +436,8 @@ pub struct IdunnAuthenticatedDaemonHealthAdmissionRecord { pub deployment_id: Option, #[cultcache(key = 17)] pub private_state_exposed: bool, + #[cultcache(key = 18, default)] + pub activation_witness_sha256: Option, } impl IdunnAuthenticatedDaemonHealthAdmissionRecord { @@ -462,6 +468,13 @@ impl IdunnAuthenticatedDaemonHealthAdmissionRecord { &self.source_commit, )?; validate_optional_identifier(&self.deployment_id, "deployment id")?; + if self + .activation_witness_sha256 + .as_deref() + .is_some_and(|value| !is_sha256(value)) + { + bail!("authenticated daemon health activation witness is malformed"); + } if self.private_state_exposed { bail!("authenticated daemon health admission exposes private state"); } @@ -469,6 +482,71 @@ impl IdunnAuthenticatedDaemonHealthAdmissionRecord { } } +/// Idunn-issued traffic authority for a release that has proved its exact +/// deployment, activation, and authenticated provider-health lineage. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.runtime_traffic_admission", + schema = "idunn.runtime_traffic_admission.v2" +)] +pub struct IdunnRuntimeTrafficAdmissionRecord { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub daemon_id: String, + #[cultcache(key = 2)] + pub release_id: String, + #[cultcache(key = 3)] + pub release_witness_sha256: String, + #[cultcache(key = 4)] + pub source_commit: String, + #[cultcache(key = 5)] + pub deployment_id: String, + #[cultcache(key = 6)] + pub activation_witness_sha256: String, + #[cultcache(key = 7)] + pub signed_health_sha256: String, + #[cultcache(key = 8)] + pub publisher_incarnation_id: String, + #[cultcache(key = 9)] + pub publisher_sequence: u64, + #[cultcache(key = 10)] + pub signer_identity_id: String, + #[cultcache(key = 11)] + pub runtime_process_id: u32, + #[cultcache(key = 12)] + pub runtime_process_starttime_ticks: u64, +} + +impl IdunnRuntimeTrafficAdmissionRecord { + pub fn validate(&self) -> Result<()> { + if self.schema_version != IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA { + bail!("runtime traffic admission schema is unsupported"); + } + if self.daemon_id != IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY + && self.daemon_id != IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY + { + bail!("runtime traffic admission daemon is unsupported"); + } + validate_identifier(&self.daemon_id, "daemon id")?; + validate_identifier(&self.release_id, "release id")?; + validate_identifier(&self.deployment_id, "deployment id")?; + validate_identifier(&self.publisher_incarnation_id, "publisher incarnation id")?; + validate_identifier(&self.signer_identity_id, "signer identity id")?; + if !is_sha256(&self.release_witness_sha256) + || !is_lower_hex(&self.source_commit, 40) + || !is_sha256(&self.activation_witness_sha256) + || !is_sha256(&self.signed_health_sha256) + || self.publisher_sequence == 0 + || self.runtime_process_id == 0 + || self.runtime_process_starttime_ticks == 0 + { + bail!("runtime traffic admission lineage is malformed"); + } + Ok(()) + } +} + /// Quarantined observation of the retired unsigned wire contract. This may /// explain missing authenticated health but can never satisfy managed health. #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] @@ -721,7 +799,7 @@ pub struct IdunnLifecycleCommandRecord { } #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "idunn.release_target", schema = "idunn.release_target.v2")] +#[cultcache(type = "idunn.release_target", schema = "idunn.release_target.v3")] pub struct IdunnReleaseTargetRecord { #[cultcache(key = 0)] pub target_id: String, @@ -765,6 +843,8 @@ pub struct IdunnReleaseTargetRecord { pub requires_bifrost_authority: bool, #[cultcache(key = 20, default)] pub observed_upstream_revision: String, + #[cultcache(key = 21)] + pub minimum_source_revision: Option, } #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] @@ -1786,6 +1866,7 @@ cultmesh_rs::cultmesh_documents!(OdinDocuments { IdunnSignedDaemonHealthRecord => IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IdunnDaemonHealthTrustBindingRecord => IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnAuthenticatedDaemonHealthAdmissionRecord => IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, + IdunnRuntimeTrafficAdmissionRecord => IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA, IdunnUnsignedDaemonHealthDiagnosticRecord => IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, GameCultServiceTrustAnchorRecord => GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, IdunnAuthenticatedProviderHealthProjectionRecord => IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, @@ -1867,6 +1948,7 @@ mod tests { signature_algorithm: "ed25519".into(), signature: vec![9; 64], private_state_exposed: false, + activation_witness_sha256: Some(format!("sha256-{}", "f".repeat(64))), } } @@ -1913,6 +1995,26 @@ mod tests { source_commit: health.source_commit, deployment_id: health.deployment_id, private_state_exposed: false, + activation_witness_sha256: health.activation_witness_sha256, + } + } + + fn runtime_traffic_admission_fixture() -> IdunnRuntimeTrafficAdmissionRecord { + let admission = authenticated_admission_fixture(); + IdunnRuntimeTrafficAdmissionRecord { + schema_version: IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into(), + daemon_id: IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(), + release_id: admission.release_id.unwrap(), + release_witness_sha256: admission.release_witness_sha256.unwrap(), + source_commit: admission.source_commit.unwrap(), + deployment_id: admission.deployment_id.unwrap(), + activation_witness_sha256: admission.activation_witness_sha256.unwrap(), + signed_health_sha256: admission.signed_health_sha256, + publisher_incarnation_id: admission.publisher_incarnation_id, + publisher_sequence: admission.publisher_sequence, + signer_identity_id: admission.signer_identity_id, + runtime_process_id: 41, + runtime_process_starttime_ticks: 987_654, } } @@ -2114,6 +2216,95 @@ mod tests { Ok(()) } + #[test] + fn runtime_traffic_admission_is_a_closed_positional_document() -> Result<()> { + let admission = runtime_traffic_admission_fixture(); + admission.validate()?; + assert_eq!( + IdunnRuntimeTrafficAdmissionRecord::TYPE, + "idunn.runtime_traffic_admission" + ); + assert_eq!( + IdunnRuntimeTrafficAdmissionRecord::SCHEMA_NAME, + IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA + ); + let payload = rmp_serde::to_vec(&admission)?; + assert_eq!(payload.first(), Some(&0x9d)); + assert_eq!( + rmp_serde::from_slice::(&payload)?, + admission + ); + let pre_process_binding = rmp_serde::to_vec(&( + admission.schema_version.clone(), + admission.daemon_id.clone(), + admission.release_id.clone(), + admission.release_witness_sha256.clone(), + admission.source_commit.clone(), + admission.deployment_id.clone(), + admission.activation_witness_sha256.clone(), + admission.signed_health_sha256.clone(), + admission.publisher_incarnation_id.clone(), + admission.publisher_sequence, + admission.signer_identity_id.clone(), + ))?; + assert_eq!(pre_process_binding.first(), Some(&0x9b)); + assert!( + rmp_serde::from_slice::(&pre_process_binding) + .is_err(), + "runtime admission v2 must not decode a pre-process-binding body" + ); + let mut ghostlight = admission.clone(); + ghostlight.daemon_id = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); + ghostlight.validate()?; + Ok(()) + } + + #[test] + fn runtime_traffic_admission_refuses_weak_or_partial_lineage() { + let mut admission = runtime_traffic_admission_fixture(); + admission.publisher_sequence = 0; + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.source_commit = "F".repeat(40); + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.activation_witness_sha256 = "missing".into(); + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.schema_version = "idunn.runtime_traffic_admission.v0".into(); + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.daemon_id = "caller-selected-daemon".into(); + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.runtime_process_id = 0; + assert!(admission.validate().is_err()); + + admission = runtime_traffic_admission_fixture(); + admission.runtime_process_starttime_ticks = 0; + assert!(admission.validate().is_err()); + } + + #[test] + fn activation_binding_is_additive_for_existing_authenticated_admissions() -> Result<()> { + let mut admission = authenticated_admission_fixture(); + admission.activation_witness_sha256 = None; + let mut legacy = rmp_serde::to_vec(&admission)?; + assert_eq!(&legacy[..3], &[0xdc, 0, 19]); + assert_eq!(legacy.pop(), Some(0xc0)); + legacy[2] = 18; + let decoded: IdunnAuthenticatedDaemonHealthAdmissionRecord = + rmp_serde::from_slice(&legacy)?; + assert_eq!(decoded.activation_witness_sha256, None); + decoded.validate()?; + Ok(()) + } + #[test] fn generic_signed_health_contracts_refuse_weak_or_partial_authority() { let mut health = signed_health_fixture(); @@ -2128,6 +2319,9 @@ mod tests { health = signed_health_fixture(); health.publisher_sequence = 0; assert!(health.validate().is_err()); + health = signed_health_fixture(); + health.activation_witness_sha256 = Some(format!("sha256-{}", "F".repeat(64))); + assert!(health.validate().is_err()); let mut binding = trust_binding_fixture(); binding.binding_authority = "daemon".into(); @@ -2145,6 +2339,9 @@ mod tests { admission = authenticated_admission_fixture(); admission.admitted_at_unix_millis = admission.observed_at_unix_millis - 1; assert!(admission.validate().is_err()); + admission = authenticated_admission_fixture(); + admission.activation_witness_sha256 = Some("caller-selected".into()); + assert!(admission.validate().is_err()); } #[test] diff --git a/crates/odin-core/src/idunn.rs b/crates/odin-core/src/idunn.rs index 79ca417..a699c1d 100644 --- a/crates/odin-core/src/idunn.rs +++ b/crates/odin-core/src/idunn.rs @@ -457,6 +457,7 @@ mod tests { release_authority_status: "authorized".to_string(), requires_bifrost_authority: true, observed_upstream_revision: "abc123".to_string(), + minimum_source_revision: None, }; let artifact = IdunnDeploymentArtifactRecord { artifact_id: "artifact:voidbot:main".to_string(), @@ -501,6 +502,15 @@ mod tests { status: "planned".to_string(), planned_at: "2026-06-04T00:00:02Z".to_string(), }; + let release_payload = rmp_serde::to_vec(&release_target)?; + assert_eq!(&release_payload[..3], &[0xdc, 0, 22]); + let mut pre_floor_payload = release_payload.clone(); + assert_eq!(pre_floor_payload.pop(), Some(0xc0)); + pre_floor_payload[2] = 21; + assert!( + rmp_serde::from_slice::(&pre_floor_payload).is_err(), + "release-target v3 must not decode a pre-floor positional body" + ); node.put(&release_target.target_id, &release_target)?; node.put(&artifact.artifact_id, &artifact)?; node.put(&migration_plan.plan_id, &migration_plan)?; diff --git a/docs/idunn.md b/docs/idunn.md index b016d14..e13622b 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -44,12 +44,16 @@ escalation, and continuity witness state. - Consequence gate: Yggdrasil keeps `deployment-brake.cc` and its pre-created sibling `deployment-brake.cc.lock` in `/var/lib/gamecult/idunn-authority`. The directory is `root:idunn` `0750`, both files are `root:idunn` `0640`, and - Idunn receives only a systemd `ReadOnlyPaths` view. It holds the lock shared - across the final read, validation, and spawn; the root-owned operator writer - takes it exclusive for engage/release. A bounded release authorizes the exact - rollout request and source revision, covering its migration and deploy spawns - until expiry. It is not consumed after the first spawn, and every spawn must - independently revalidate it. + Idunn receives only a systemd `ReadOnlyPaths` view. Idunn validates under a + shared lock before spawning the privileged boundary. The root actuator then + independently reopens and validates the signed brake while remaining the sole + shared-lock holder through one synchronous deploy transaction; the child does + not inherit the lock descriptor. The root-owned operator writer takes the + lock exclusive for engage/release. A bounded release admits the exact target, + rollout request, and source revision only when the transaction starts before + its signed deadline. Admission transfers completion of that transaction to + the actuator; expiry is not a retroactive timer veto. Every later transaction + must independently revalidate the brake. - Deletion line: any keepalive loop inside Odin, Gjallar, Eve lowerers, or renderer code must be cut or demoted to a probe that names Idunn as the restart owner. @@ -95,10 +99,13 @@ Idunn now shares Odin's Rust body: - `--swarm-profile yggdrasil-local` is the Linux host-local supervisor for Epiphany, Heimdall, Repixelizer, StreamPixels, and VoidBot. Its only command boundary is the root-owned `/usr/local/libexec/idunn-yggdrasil` actuator. Restart commands - target the local Compose stack; deployment fails closed unless a root-owned - executable manifest exists for that service under - `/srv/odin/deploy-manifests`. It does not inherit Starfire paths, SSH keys, - ADB authority, or Windows process ownership. + target the local Compose stack. For deployment, the root boundary derives the + repository, ref, daemon id, and release-authority posture from its fixed + target allowlist, then binds the caller's request identifier and source to the + signed root-owned brake. A manifest must be a direct root-owned executable + regular file under `/srv/odin/deploy-manifests`; neither it nor any ancestor + may be group/world-writable, and symlink manifests are refused. It does not + inherit Starfire paths, SSH keys, ADB authority, or Windows process ownership. - Release authority is explicit per target. Odin, Ghostlight, and Epiphany are source-observed targets: Idunn derives the newest commit on each admitted ref that changes build or runtime source, freezes that exact revision into its diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil index 6e8e9b7..1d83b52 100644 --- a/scripts/linux/idunn-yggdrasil +++ b/scripts/linux/idunn-yggdrasil @@ -1,6 +1,11 @@ #!/bin/sh set -eu +PATH=/usr/sbin:/usr/bin:/sbin:/bin +export PATH +LC_ALL=C +export LC_ALL + if [ "$(id -u)" -ne 0 ]; then echo "idunn-yggdrasil must run through its narrow sudo boundary" >&2 exit 1 @@ -8,105 +13,368 @@ fi action="${1:-}" target="${2:-}" -source_commit="${3:-}" -repository_full_name="${4:-}" -upstream_ref="${5:-}" -release_authority_id="${6:-}" -release_authority_sha256="${7:-}" -deployment_request_id="${8:-}" -requires_bifrost_authority="${9:-false}" compose=/srv/compose/yggdrasil-apps.yaml +brake_store=/var/lib/gamecult/idunn-authority/deployment-brake.cc +brake_lock=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock +brake_anchor=/etc/gamecult/idunn/deployment-brake-operator-anchor.cc +generation_lock=/var/lib/gamecult/idunn-authority/idunn-generation.lock +idunn_provision=/usr/local/bin/idunn-provision -require_release_lineage() { - case "$target" in - bifrost-persona-feedback) target_requires_bifrost_authority=true ;; - epiphany|epiphany-capstone-17|voidbot|heimdall|repixelizer|streampixels|odin|codex-connector|ghostlight|gjallar) target_requires_bifrost_authority=false ;; - *) echo "deployment target has no release-authority policy: $target" >&2; exit 1 ;; - esac - if [ "$requires_bifrost_authority" != "$target_requires_bifrost_authority" ]; then - echo "caller release-authority mode does not match root policy for $target" >&2 +require_root_owned_nonwritable_component() { + component="$1" + kind="$2" + if [ -L "$component" ]; then + echo "privileged actuator path component must not be a symlink: $component" >&2 exit 1 fi - requires_bifrost_authority="$target_requires_bifrost_authority" - case "$source_commit" in - *[!0-9a-f]*|'') echo "invalid or missing IDUNN_SOURCE_COMMIT" >&2; exit 1 ;; + case "$kind" in + file) [ -f "$component" ] || { echo "privileged actuator path component is not a regular file: $component" >&2; exit 1; } ;; + directory) [ -d "$component" ] || { echo "privileged actuator path component is not a directory: $component" >&2; exit 1; } ;; + *) echo "internal privileged actuator path-kind error" >&2; exit 1 ;; esac - [ "${#source_commit}" -eq 40 ] || { echo "IDUNN_SOURCE_COMMIT must be 40 hex characters" >&2; exit 1; } - [ -n "$repository_full_name" ] || { echo "missing repository identity" >&2; exit 1; } - [ -n "$upstream_ref" ] || { echo "missing upstream ref" >&2; exit 1; } - [ -n "$deployment_request_id" ] || { echo "missing Idunn deployment request id" >&2; exit 1; } + owner="$(stat -c '%u' -- "$component")" + mode="$(stat -c '%a' -- "$component")" + [ "$owner" = 0 ] || { echo "privileged actuator path component is not root-owned: $component" >&2; exit 1; } + case "$mode" in + ''|*[!0-7]*) echo "privileged actuator path component has an unreadable mode: $component" >&2; exit 1 ;; + esac + group_mode=$(( (mode / 10) % 10 )) + other_mode=$(( mode % 10 )) + if [ $((group_mode & 2)) -ne 0 ] || [ $((other_mode & 2)) -ne 0 ]; then + echo "privileged actuator path component is group/world-writable: $component" >&2 + exit 1 + fi +} + +require_root_owned_regular_path() { + path="$1" + case "$path" in + /*) ;; + *) echo "privileged actuator path must be absolute: $path" >&2; exit 1 ;; + esac + ancestor="$(dirname -- "$path")" + while :; do + require_root_owned_nonwritable_component "$ancestor" directory + [ "$ancestor" = / ] && break + ancestor="$(dirname -- "$ancestor")" + done + require_root_owned_nonwritable_component "$path" file +} + +require_root_owned_resolved_executable() { + entrypoint="$1" + ancestor="$(dirname -- "$entrypoint")" + while :; do + require_root_owned_nonwritable_component "$ancestor" directory + [ "$ancestor" = / ] && break + ancestor="$(dirname -- "$ancestor")" + done + [ -e "$entrypoint" ] || { echo "privileged executable entrypoint is missing: $entrypoint" >&2; exit 1; } + [ "$(stat -c '%u' -- "$entrypoint")" = 0 ] || { echo "privileged executable entrypoint is not root-owned: $entrypoint" >&2; exit 1; } + resolved="$(readlink -f -- "$entrypoint")" + [ -n "$resolved" ] || { echo "privileged executable entrypoint cannot be resolved: $entrypoint" >&2; exit 1; } + require_root_owned_regular_path "$resolved" + [ -x "$resolved" ] || { echo "privileged executable is not executable: $resolved" >&2; exit 1; } +} + +acquire_idunn_generation_lock() { + # Every target consequence observes one installed Idunn generation. The + # self-bootstrap owns the exclusive side of this lock while replacing and + # accepting that generation; target manifests hold the shared side from + # admission through completion. This lock does not decide brake posture, + # target lifecycle, or release authority. + require_root_owned_regular_path "$generation_lock" + exec 8<"$generation_lock" + /usr/bin/flock --shared 8 +} + +deployment_target_policy() { + manifest_takes_action=false + minimum_source_revision_required=false + case "$target" in + odin) + daemon_id=yggdrasil-odin + repository_full_name=GameCult/Odin + upstream_ref=refs/heads/codex/ygg-idunn-independent-bootstrap + requires_bifrost_authority=false + manifest_takes_action=true + ;; + codex-connector) + daemon_id=yggdrasil-codex-connector + repository_full_name=GameCult/CodexConnector + upstream_ref=refs/heads/codex/ghostlight-release-binding + requires_bifrost_authority=false + minimum_source_revision_required=true + manifest_takes_action=true + ;; + ghostlight) + daemon_id=yggdrasil-ghostlight + repository_full_name=GameCult/Ghostlight + upstream_ref=refs/heads/codex/ghostlight-dungeon-mvp + requires_bifrost_authority=false + minimum_source_revision_required=true + manifest_takes_action=true + ;; + voidbot) + daemon_id=yggdrasil-voidbot + repository_full_name=GameCult/VoidBot + upstream_ref=refs/heads/main + requires_bifrost_authority=false + manifest_takes_action=true + ;; + heimdall) + daemon_id=yggdrasil-heimdall + repository_full_name=GameCult/Heimdall + upstream_ref=refs/heads/main + requires_bifrost_authority=false + ;; + epiphany-capstone-17) + daemon_id=yggdrasil-epiphany-capstone-17 + repository_full_name=GameCult/Epiphany + upstream_ref=refs/heads/codex/epiphany-shakedown-live + requires_bifrost_authority=false + manifest_takes_action=true + ;; + epiphany) + daemon_id=yggdrasil-epiphany + repository_full_name=GameCult/Epiphany + upstream_ref=refs/heads/codex/epiphany-shakedown-live + requires_bifrost_authority=false + manifest_takes_action=true + ;; + bifrost-persona-feedback) + daemon_id=yggdrasil-bifrost-persona-feedback + repository_full_name=GameCult/Bifrost + upstream_ref=refs/heads/main + requires_bifrost_authority=true + ;; + repixelizer) + daemon_id=yggdrasil-repixelizer + repository_full_name=GameCult/repixelizer + upstream_ref=refs/heads/main + requires_bifrost_authority=false + ;; + streampixels) + daemon_id=yggdrasil-streampixels + repository_full_name=GameCult/StreamPixels + upstream_ref=refs/heads/main + requires_bifrost_authority=false + ;; + gjallar) + daemon_id=yggdrasil-gjallar + repository_full_name=GameCult/Gjallar + upstream_ref=refs/heads/codex/yggdrasil-aggregate-daemon + requires_bifrost_authority=false + ;; + *) echo "deployment target has no root actuator policy: $target" >&2; exit 1 ;; + esac +} + +require_signed_deployment_grant() { + source_commit="$3" + deployment_request_id="$4" + release_authority_id="$5" + release_authority_sha256="$6" + + deployment_target_policy + case "$source_commit" in + *[!0-9a-f]*|'') echo "invalid or missing source commit" >&2; exit 1 ;; + esac + [ "${#source_commit}" -eq 40 ] || { echo "source commit must be 40 lowercase hex characters" >&2; exit 1; } + case "$deployment_request_id" in + *[!A-Za-z0-9._:/+-]*|'') echo "invalid or missing Idunn deployment request id" >&2; exit 1 ;; + esac + [ "${#deployment_request_id}" -le 256 ] || { echo "Idunn deployment request id is oversized" >&2; exit 1; } + case "$deployment_request_id" in + "deploy:$daemon_id:"?*|"manual:redeploy:$daemon_id:"?*) ;; + *) echo "signed deployment request is not for root target $target" >&2; exit 1 ;; + esac + case "$requires_bifrost_authority" in true) [ -n "$release_authority_id" ] || { echo "missing Bifrost release authority id" >&2; exit 1; } case "$release_authority_sha256" in *[!0-9a-f]*|'') echo "invalid or missing Bifrost release authority digest" >&2; exit 1 ;; esac - [ "${#release_authority_sha256}" -eq 64 ] || { echo "Bifrost release authority digest must be 64 hex characters" >&2; exit 1; } - /usr/local/bin/idunn validate-release-authority \ - --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc \ - --release-authority-store /srv/bifrost/state/repository-release-authority.cc \ - --repository-full-name "$repository_full_name" \ - --upstream-ref "$upstream_ref" \ - --source-revision "$source_commit" \ - --release-authority-id "$release_authority_id" \ - --release-authority-sha256 "$release_authority_sha256" + [ "${#release_authority_sha256}" -eq 64 ] || { echo "Bifrost release authority digest must be 64 lowercase hex characters" >&2; exit 1; } ;; false) [ -z "$release_authority_id" ] || { echo "source-observed release cannot claim Bifrost authority id" >&2; exit 1; } [ -z "$release_authority_sha256" ] || { echo "source-observed release cannot claim Bifrost authority digest" >&2; exit 1; } ;; - *) echo "invalid Bifrost authority requirement mode" >&2; exit 1 ;; esac + + require_root_owned_resolved_executable "$idunn_provision" + require_root_owned_regular_path "$brake_store" + require_root_owned_regular_path "$brake_lock" + require_root_owned_regular_path "$brake_anchor" + + # This validation is the deployment-admission point. The signed expiry is a + # start-before deadline: it denies a later transaction, but does not become a + # timer-shaped veto over one already admitted. The root actuator remains the + # sole shared-lock holder through the synchronous consequence, so an explicit + # engage/release transition cannot race or rewrite that transaction. + exec 9<"$brake_lock" + /usr/bin/flock --shared 9 + now_unix_millis="$(/usr/bin/date +%s%3N)" + case "$now_unix_millis" in + ''|*[!0-9]*) echo "root actuator clock did not produce Unix milliseconds" >&2; exit 1 ;; + esac + "$idunn_provision" deployment-brake-status \ + --store "$brake_store" \ + --operator-anchor "$brake_anchor" \ + --runtime-id yggdrasil \ + --release-id "$source_commit" \ + --deployment-id "$deployment_request_id" \ + --now-unix-millis "$now_unix_millis" + + if [ "$minimum_source_revision_required" = true ]; then + require_root_owned_resolved_executable /usr/local/bin/idunn + /usr/local/bin/idunn validate-minimum-source-revision \ + --daemon "$daemon_id" \ + --source-revision "$source_commit" + fi + + if [ "$requires_bifrost_authority" = true ]; then + require_root_owned_resolved_executable /usr/local/bin/idunn + /usr/local/bin/idunn validate-release-authority \ + --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc \ + --release-authority-store /srv/bifrost/state/repository-release-authority.cc \ + --repository-full-name "$repository_full_name" \ + --upstream-ref "$upstream_ref" \ + --source-revision "$source_commit" \ + --release-authority-id "$release_authority_id" \ + --release-authority-sha256 "$release_authority_sha256" + fi } +manifest_group_exists() { + kill -s 0 "-$manifest_pid" 2>/dev/null +} + +signal_manifest_group() { + kill -s "$1" "-$manifest_pid" 2>/dev/null || true +} + +forward_manifest_signal() { + manifest_wait_interrupted=true + signal_manifest_group "$1" +} + +run_manifest_while_holding_authority_locks() { + # Stay alive as the sole lock owner. The child must not inherit descriptors + # 8 or 9: flock locks attach to shared open-file descriptions, so an + # inherited descriptor could retain or unlock the parent's generation or + # deployment transaction. The owned process group also prevents ordinary + # descendants from outliving interruption. + require_root_owned_resolved_executable /usr/bin/setsid + /usr/bin/setsid "$@" 8<&- 9<&- & + manifest_pid=$! + manifest_wait_interrupted=false + trap 'forward_manifest_signal HUP' HUP + trap 'forward_manifest_signal INT' INT + trap 'forward_manifest_signal TERM' TERM + while :; do + manifest_wait_interrupted=false + if wait "$manifest_pid"; then + manifest_status=0 + else + manifest_status=$? + fi + if [ "$manifest_wait_interrupted" = true ] && manifest_group_exists; then + continue + fi + break + done + trap - HUP INT TERM + + manifest_descendants_leaked=false + if manifest_group_exists; then + manifest_descendants_leaked=true + signal_manifest_group TERM + for _ in 1 2 3 4 5; do + manifest_group_exists || break + sleep 1 + done + fi + if manifest_group_exists; then + signal_manifest_group KILL + for _ in 1 2 3 4 5; do + manifest_group_exists || break + sleep 1 + done + fi + if manifest_group_exists; then + echo "deployment manifest process group survived SIGKILL" >&2 + return 1 + fi + if [ "$manifest_descendants_leaked" = true ] && [ "$manifest_status" -eq 0 ]; then + echo "deployment manifest left a descendant process behind" >&2 + return 1 + fi + return "$manifest_status" +} + +case "$action" in + deploy) + [ "$#" -eq 6 ] || { echo "deploy requires exact target, source, request, and release-authority arguments" >&2; exit 2; } + ;; + restart) + [ "$#" -eq 2 ] || { echo "restart accepts only one exact target" >&2; exit 2; } + ;; + *) + echo "unsupported Idunn Yggdrasil actuator action: $action" >&2 + exit 2 + ;; +esac + +acquire_idunn_generation_lock + case "$action:$target" in restart:heimdall) + require_root_owned_regular_path "$compose" exec docker compose -f "$compose" restart heimdall ;; restart:repixelizer) + require_root_owned_regular_path "$compose" exec docker compose -f "$compose" restart repixelizer ;; restart:streampixels) + require_root_owned_regular_path "$compose" docker compose -f "$compose" restart streampixels-service streampixels-web ;; restart:gjallar) exec systemctl restart gjallar-yggdrasil.service ;; - deploy:voidbot|restart:voidbot|deploy:epiphany|restart:epiphany|deploy:epiphany-capstone-17|deploy:odin|restart:odin|deploy:codex-connector|restart:codex-connector|deploy:ghostlight|restart:ghostlight) + restart:voidbot|restart:epiphany|restart:odin|restart:codex-connector|restart:ghostlight) manifest="/srv/odin/deploy-manifests/$target" - if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then - echo "root-owned executable deploy manifest is not installed: $manifest" >&2 - exit 1 - fi - if [ "$action" = deploy ]; then - require_release_lineage - fi - IDUNN_ACTUATOR=1 IDUNN_COMMAND_AUTHORITY=idunn-daemon \ - IDUNN_SOURCE_COMMIT="$source_commit" \ - IDUNN_REPOSITORY_FULL_NAME="$repository_full_name" \ - IDUNN_UPSTREAM_REF="$upstream_ref" \ - BIFROST_RELEASE_AUTHORITY_ID="$release_authority_id" \ - BIFROST_RELEASE_AUTHORITY_SHA256="$release_authority_sha256" \ - IDUNN_DEPLOYMENT_REQUEST_ID="$deployment_request_id" \ - IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" \ - exec "$manifest" "$action" + require_root_owned_regular_path "$manifest" + [ -x "$manifest" ] || { echo "root actuator manifest is not executable: $manifest" >&2; exit 1; } + export IDUNN_ACTUATOR=1 + export IDUNN_COMMAND_AUTHORITY=idunn-daemon + run_manifest_while_holding_authority_locks "$manifest" restart + exit $? ;; - deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback|deploy:gjallar) + deploy:voidbot|deploy:epiphany|deploy:epiphany-capstone-17|deploy:odin|deploy:codex-connector|deploy:ghostlight|deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback|deploy:gjallar) manifest="/srv/odin/deploy-manifests/$target" - if [ ! -x "$manifest" ] || [ ! -O "$manifest" ]; then - echo "root-owned executable deploy manifest is not installed: $manifest" >&2 - exit 1 + require_root_owned_regular_path "$manifest" + [ -x "$manifest" ] || { echo "root actuator manifest is not executable: $manifest" >&2; exit 1; } + require_signed_deployment_grant "$@" + export IDUNN_ACTUATOR=1 + export IDUNN_COMMAND_AUTHORITY=idunn-daemon + export IDUNN_SOURCE_COMMIT="$source_commit" + export IDUNN_REPOSITORY_FULL_NAME="$repository_full_name" + export IDUNN_UPSTREAM_REF="$upstream_ref" + export BIFROST_RELEASE_AUTHORITY_ID="$release_authority_id" + export BIFROST_RELEASE_AUTHORITY_SHA256="$release_authority_sha256" + export IDUNN_DEPLOYMENT_REQUEST_ID="$deployment_request_id" + export IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" + if [ "$manifest_takes_action" = true ]; then + run_manifest_while_holding_authority_locks "$manifest" "$action" + exit $? fi - require_release_lineage - IDUNN_ACTUATOR=1 IDUNN_COMMAND_AUTHORITY=idunn-daemon \ - IDUNN_SOURCE_COMMIT="$source_commit" \ - IDUNN_REPOSITORY_FULL_NAME="$repository_full_name" \ - IDUNN_UPSTREAM_REF="$upstream_ref" \ - BIFROST_RELEASE_AUTHORITY_ID="$release_authority_id" \ - BIFROST_RELEASE_AUTHORITY_SHA256="$release_authority_sha256" \ - IDUNN_DEPLOYMENT_REQUEST_ID="$deployment_request_id" \ - IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" \ - exec "$manifest" + run_manifest_while_holding_authority_locks "$manifest" + exit $? ;; *) echo "unsupported Idunn Yggdrasil actuator request: $action $target" >&2 diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service index c8dd2f8..0597806 100644 --- a/scripts/linux/idunn-yggdrasil.service +++ b/scripts/linux/idunn-yggdrasil.service @@ -19,7 +19,7 @@ UMask=027 PrivateTmp=yes ProtectHome=yes ProtectSystem=full -ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/codex-connector /etc/gamecult/ghostlight /etc/systemd/system/codex-connector.service /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot +ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/codex-connector /etc/gamecult/ghostlight-dungeon /etc/systemd/system/codex-connector.service /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot ReadOnlyPaths=/var/lib/gamecult/idunn-authority [Install] diff --git a/scripts/linux/idunn-yggdrasil.sudoers b/scripts/linux/idunn-yggdrasil.sudoers index a15fccd..dad6e44 100644 --- a/scripts/linux/idunn-yggdrasil.sudoers +++ b/scripts/linux/idunn-yggdrasil.sudoers @@ -1 +1,24 @@ -idunn ALL=(root) NOPASSWD: /usr/local/libexec/idunn-yggdrasil * +Cmnd_Alias IDUNN_YGGDRASIL_DEPLOY = \ + /usr/local/libexec/idunn-yggdrasil deploy odin *, \ + /usr/local/libexec/idunn-yggdrasil deploy codex-connector *, \ + /usr/local/libexec/idunn-yggdrasil deploy ghostlight *, \ + /usr/local/libexec/idunn-yggdrasil deploy voidbot *, \ + /usr/local/libexec/idunn-yggdrasil deploy heimdall *, \ + /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 *, \ + /usr/local/libexec/idunn-yggdrasil deploy epiphany *, \ + /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback *, \ + /usr/local/libexec/idunn-yggdrasil deploy repixelizer *, \ + /usr/local/libexec/idunn-yggdrasil deploy streampixels *, \ + /usr/local/libexec/idunn-yggdrasil deploy gjallar * +Cmnd_Alias IDUNN_YGGDRASIL_RESTART = \ + /usr/local/libexec/idunn-yggdrasil restart odin, \ + /usr/local/libexec/idunn-yggdrasil restart codex-connector, \ + /usr/local/libexec/idunn-yggdrasil restart ghostlight, \ + /usr/local/libexec/idunn-yggdrasil restart voidbot, \ + /usr/local/libexec/idunn-yggdrasil restart heimdall, \ + /usr/local/libexec/idunn-yggdrasil restart epiphany, \ + /usr/local/libexec/idunn-yggdrasil restart repixelizer, \ + /usr/local/libexec/idunn-yggdrasil restart streampixels, \ + /usr/local/libexec/idunn-yggdrasil restart gjallar + +idunn ALL=(root) NOPASSWD: IDUNN_YGGDRASIL_DEPLOY, IDUNN_YGGDRASIL_RESTART diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index fdccc37..021dca7 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit fdccc37aa68c3f0e53bfd53a92c15a148978f0c7 +Subproject commit 021dca7f3261c9842019f0845b35022455ccf756 From 45fa0385d7b9bc8e02795bded152bfeddf1523be Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 14:16:48 +0200 Subject: [PATCH 72/93] Keep canonical origin output alive for validation --- crates/idunn-daemon/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 3b00e78..29e2c77 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -2871,8 +2871,9 @@ fn require_compiled_repository_origin( "compiled-policy repository has no readable canonical origin" )); } - let urls = String::from_utf8(output.stdout) - .context("compiled-policy repository origin is not UTF-8")? + let origin_output = String::from_utf8(output.stdout) + .context("compiled-policy repository origin is not UTF-8")?; + let urls = origin_output .lines() .map(str::trim) .filter(|url| !url.is_empty()) From 33ebcd9271c0380e0a16c574fc2826cef09a42e0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 14:21:04 +0200 Subject: [PATCH 73/93] Encode a genuinely noncanonical admission fixture --- crates/idunn-daemon/src/main.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 29e2c77..9911cfa 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -12424,9 +12424,10 @@ mod tests { invalid_timestamp.stored_at = "caller-selected-time".into(); assert!(decode_runtime_traffic_admission_snapshot(&[invalid_timestamp]).is_err()); - let mut named = envelope.clone(); - named.payload = rmp_serde::to_vec_named(&record).unwrap(); - let error = decode_runtime_traffic_admission_snapshot(&[named]) + let mut noncanonical = envelope.clone(); + assert_eq!(noncanonical.payload.first(), Some(&0x9d)); + noncanonical.payload.splice(0..1, [0xdc, 0, 13]); + let error = decode_runtime_traffic_admission_snapshot(&[noncanonical]) .unwrap_err() .to_string(); assert!(error.contains("canonical positional MessagePack")); From d44881fb4aab9201229804b13bd5f90f266bdb31 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 18:31:13 +0200 Subject: [PATCH 74/93] Define Idunn deployment authority contract --- crates/idunn-daemon/Cargo.toml | 1 + crates/idunn-daemon/src/deployment.rs | 1666 ++++++++++++++++++++ crates/idunn-daemon/src/deployment_plan.rs | 1322 ++++++++++++++++ crates/idunn-daemon/src/lib.rs | 2 + docs/idunn-deployment-authority.md | 291 ++++ docs/idunn.md | 7 + 6 files changed, 3289 insertions(+) create mode 100644 crates/idunn-daemon/src/deployment.rs create mode 100644 crates/idunn-daemon/src/deployment_plan.rs create mode 100644 docs/idunn-deployment-authority.md diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index ec23e39..4288137 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -28,6 +28,7 @@ sha2 = "0.10" chrono = "0.4" serde = { version = "1", features = ["derive"] } ed25519-dalek = "2.2" +toml = "0.8" uuid = { version = "1", features = ["v4"] } [dev-dependencies] diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs new file mode 100644 index 0000000..56fae5b --- /dev/null +++ b/crates/idunn-daemon/src/deployment.rs @@ -0,0 +1,1666 @@ +use std::collections::{BTreeMap, BTreeSet}; +use std::path::{Component, Path, PathBuf}; + +use anyhow::{Context, Result, bail, ensure}; +use serde::{Deserialize, Serialize}; + +pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; +pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v1"; +pub const PINNED_TAR_MEMBER_SCHEMA: &str = "gamecult.idunn.pinned_tar_member.v1"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct TargetDeclaration { + pub schema: String, + pub target: String, + pub source_stamp_environment: String, + #[serde(default)] + pub required_gitlinks: BTreeSet, + #[serde(default)] + pub steps: Vec, + #[serde(default)] + pub external_artifacts: Vec, + pub artifacts: Vec, + pub service: ServiceDeclaration, + pub state: StateDeclaration, + #[serde(default)] + pub provides: Vec, + #[serde(default)] + pub dependencies: Vec, + #[serde(default)] + pub conflicts: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RunnerAffordance { + SourceRead, + ArtifactWrite, + DependencyNetwork, + BuildCache, + PrivateCapabilityAccess, + SecretRead, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RecipeStep { + pub id: String, + pub phase: RecipePhase, + pub runner: String, + pub argv: Vec, + #[serde(default = "default_working_directory")] + pub working_directory: PathBuf, + #[serde(default)] + pub required_environment: BTreeSet, +} + +fn default_working_directory() -> PathBuf { + PathBuf::from(".") +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RecipePhase { + Prepare, + Test, + Build, + Acceptance, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ExternalArtifact { + pub id: String, + pub runner: String, + pub manifest: PathBuf, + pub destination: PathBuf, + #[serde(default)] + pub executable: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PinnedTarMemberManifest { + pub schema: String, + pub url: String, + pub archive_sha256: String, + pub member: PathBuf, + pub member_sha256: String, +} + +impl PinnedTarMemberManifest { + pub fn parse(input: &str) -> Result { + let manifest: Self = + toml::from_str(input).context("decoding strict pinned tar-member manifest")?; + ensure!( + manifest.schema == PINNED_TAR_MEMBER_SCHEMA, + "unsupported pinned artifact schema" + ); + https_host(&manifest.url)?; + require_sha256(&manifest.archive_sha256, "archive sha256")?; + require_relative_path(&manifest.member, "archive member", false)?; + require_sha256(&manifest.member_sha256, "member sha256")?; + Ok(manifest) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ArtifactOutput { + pub id: String, + pub source_kind: ArtifactSource, + pub runner: Option, + pub source: PathBuf, + pub destination: PathBuf, + #[serde(default)] + pub executable: bool, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum ArtifactSource { + RunnerOutput, + WorktreeTree, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ServiceDeclaration { + pub executable_artifact: String, + #[serde(default)] + pub required_adjacent_artifacts: BTreeSet, + #[serde(default)] + pub arguments: Vec, + pub transport: ServiceTransport, + #[serde(default)] + pub route_required: bool, + pub health: HealthDeclaration, + #[serde(default)] + pub required_environment: BTreeSet, + #[serde(default)] + pub optional_environment: BTreeSet, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HealthDeclaration { + pub contract: String, + pub staged: HealthState, + pub ready: HealthState, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HealthState { + pub state: String, + pub detail: String, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum ServiceTransport { + Http, + Tcp, + Rudp, + Private, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] +pub enum LaunchArgument { + Literal { value: String }, + Binding { name: String }, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct StateDeclaration { + pub generation: String, + pub slots: Vec, + pub migration: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct StateSlot { + pub id: String, + pub relative_path: PathBuf, + pub kind: StateKind, + pub schema: String, + pub writer: StateWriter, + pub recovery: StateRecovery, + pub startup: StateStartup, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct MigrationDeclaration { + pub executable_artifact: String, + pub from_generations: BTreeSet, + pub to_generation: String, + #[serde(default)] + pub arguments: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StateKind { + CultcacheFile, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StateWriter { + ProcessBoundSingleWriter, + None, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StateRecovery { + Preserve, + Rebuildable, + ExternalAuthority, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StateStartup { + CreateOrOpenAfterAdmission, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ProvidedCapability { + pub capability: String, + pub schema: String, + pub compatibility: String, + #[serde(default = "one")] + pub capacity: u32, +} + +fn one() -> u32 { + 1 +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CapabilityDependency { + pub kind: DependencyKind, + pub capability: String, + pub schema: String, + pub compatibility: String, + #[serde(default = "one")] + pub minimum_capacity: u32, + #[serde(default)] + pub startup: StartupOrder, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum DependencyKind { + Bootstrap, + Required, + Optional, + SharedInfrastructure, + Private, + ExternalOperatorBinding, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StartupOrder { + #[default] + BeforePromotion, + BeforeStart, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CapabilityConflict { + pub capability: String, + pub reason: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct OperatorBinding { + pub schema: String, + pub target: String, + pub repository: RepositoryBinding, + pub runners: BTreeMap, + pub workload: WorkloadBinding, + pub route: Option, + pub admission: AdmissionBinding, + pub brakes: BrakeBinding, + pub state_transition: StateTransitionBinding, + pub rollout: RolloutBinding, + pub placement: PlacementBinding, + #[serde(default)] + pub external_capabilities: Vec, + #[serde(default)] + pub profiles: BTreeSet, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RepositoryBinding { + pub origin: String, + pub admitted_ref: String, + pub minimum_revision: String, + pub selection: SourceSelectionPolicy, + pub pinned_revision: Option, + pub release_authority_store: Option, + pub checkout: PathBuf, + pub recipe_path: PathBuf, + #[serde(default)] + pub gitlinks: BTreeMap, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum SourceSelectionPolicy { + PinnedObject, + RefHead, + SignedRelease, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GitlinkBinding { + pub origin: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RunnerBinding { + pub driver: RunnerDriver, + pub image: String, + #[serde(default)] + pub affordances: BTreeSet, + pub cache_root: Option, + #[serde(default)] + pub allowed_https_hosts: BTreeSet, + #[serde(default)] + pub allowed_programs: BTreeSet, + #[serde(default)] + pub environment: BTreeMap, + #[serde(default)] + pub secret_files: BTreeMap, + pub network: RunnerNetwork, + pub memory_mebibytes: u32, + pub cpu_quota_percent: u32, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RunnerDriver { + Docker, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RunnerNetwork { + None, + DependencyEgress, + HostPrivate, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WorkloadBinding { + pub driver: WorkloadDriver, + pub user: String, + pub group: String, + pub unit_prefix: String, + pub release_root: PathBuf, + pub state_root: PathBuf, + pub runtime_root: PathBuf, + pub network: WorkloadNetwork, + pub hardening: WorkloadHardening, + #[serde(default)] + pub read_only_paths: BTreeSet, + #[serde(default)] + pub read_write_paths: BTreeSet, + #[serde(default)] + pub capabilities: BTreeSet, + #[serde(default)] + pub devices: BTreeSet, + pub memory_mebibytes: u32, + pub cpu_quota_percent: u32, + #[serde(default)] + pub environment: BTreeMap, + #[serde(default)] + pub secret_files: BTreeMap, + #[serde(default)] + pub argument_bindings: BTreeMap, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum WorkloadDriver { + SystemdTransient, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum WorkloadNetwork { + HostPrivate, + None, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum WorkloadHardening { + Strict, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AdmissionBinding { + pub driver: AdmissionDriver, + pub record_path: PathBuf, + pub lock_path: PathBuf, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum AdmissionDriver { + AtomicFile, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct BrakeBinding { + pub deployment_store: PathBuf, + pub lifecycle_store: PathBuf, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct StateTransitionBinding { + pub policy: StateTransitionPolicy, + pub archive_root: Option, + pub backup_root: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum StateTransitionPolicy { + Preserve, + FencedMigration, + FreshRoot, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RouteBinding { + pub driver: RouteDriver, + pub route_id: String, + pub stable_endpoint: String, + pub private_host: String, + pub private_port_start: u16, + pub private_port_end: u16, + pub config_path: PathBuf, + pub reload_unit: String, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RouteDriver { + NginxHttp, + NginxStreamTcp, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RolloutBinding { + pub strategy: RolloutStrategy, + pub drain_seconds: u32, + pub retain_releases: u32, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum RolloutStrategy { + CandidateThenPromote, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PlacementBinding { + pub desired_replicas: u32, + pub nodes: BTreeSet, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ExternalCapabilityBinding { + pub capability: String, + pub schema: String, + pub compatibility: String, + pub endpoint: String, +} + +impl TargetDeclaration { + pub fn parse(input: &str) -> Result { + let declaration: Self = + toml::from_str(input).context("decoding strict Idunn target declaration")?; + declaration.validate()?; + Ok(declaration) + } + + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == TARGET_DECLARATION_SCHEMA, + "unsupported target declaration schema" + ); + require_id(&self.target, "target")?; + require_environment_name(&self.source_stamp_environment)?; + for path in &self.required_gitlinks { + require_relative_path(path, "required gitlink", false)?; + } + ensure!(!self.steps.is_empty(), "no recipe steps declared"); + ensure!(!self.artifacts.is_empty(), "no artifact outputs declared"); + + let mut runner_ids = BTreeSet::new(); + unique_ids( + self.steps.iter().map(|step| step.id.as_str()), + "recipe step", + )?; + for step in &self.steps { + require_id(&step.runner, "runner class")?; + runner_ids.insert(step.runner.as_str()); + ensure!(!step.argv.is_empty(), "step {} has empty argv", step.id); + for argument in &step.argv { + require_value(argument, "step argv")?; + } + require_program(&step.argv[0], "step program")?; + require_relative_path(&step.working_directory, "step working directory", true)?; + for name in &step.required_environment { + require_environment_name(name)?; + } + } + + let mut artifact_ids = BTreeSet::new(); + let mut destinations = BTreeSet::new(); + for artifact in &self.artifacts { + require_id(&artifact.id, "artifact")?; + ensure!( + artifact_ids.insert(artifact.id.as_str()), + "artifact id {} is declared twice", + artifact.id + ); + require_relative_path(&artifact.source, "artifact source", false)?; + require_leaf_path(&artifact.destination, "artifact destination")?; + ensure!( + destinations.insert(artifact.destination.as_path()), + "artifact destination {} is declared twice", + artifact.destination.display() + ); + match (artifact.source_kind, artifact.runner.as_deref()) { + (ArtifactSource::RunnerOutput, Some(runner)) => { + require_id(runner, "artifact runner class")?; + runner_ids.insert(runner); + } + (ArtifactSource::WorktreeTree, None) => {} + (ArtifactSource::RunnerOutput, None) => { + bail!("runner-output artifact {} has no runner class", artifact.id) + } + (ArtifactSource::WorktreeTree, Some(_)) => bail!( + "worktree-tree artifact {} cannot name a runner class", + artifact.id + ), + } + } + for artifact in &self.external_artifacts { + require_id(&artifact.id, "external artifact")?; + ensure!( + artifact_ids.insert(artifact.id.as_str()), + "artifact id {} is declared twice", + artifact.id + ); + require_id(&artifact.runner, "external artifact runner class")?; + runner_ids.insert(artifact.runner.as_str()); + require_relative_path(&artifact.manifest, "external artifact manifest", false)?; + require_leaf_path(&artifact.destination, "external artifact destination")?; + ensure!( + destinations.insert(artifact.destination.as_path()), + "artifact destination {} is declared twice", + artifact.destination.display() + ); + } + ensure!( + !runner_ids.is_empty(), + "target declaration uses no runner classes" + ); + + ensure!( + artifact_ids.contains(self.service.executable_artifact.as_str()), + "service executable artifact is not declared" + ); + for artifact in &self.service.required_adjacent_artifacts { + ensure!( + artifact_ids.contains(artifact.as_str()), + "adjacent service artifact {artifact} is not declared" + ); + } + for argument in &self.service.arguments { + validate_launch_argument(argument)?; + } + require_capability(&self.service.health.contract, "health contract")?; + for (label, health) in [ + ("staged health", &self.service.health.staged), + ("ready health", &self.service.health.ready), + ] { + require_value(&health.state, label)?; + require_value(&health.detail, label)?; + } + ensure!( + self.service.health.staged != self.service.health.ready, + "staged and ready health states are identical" + ); + for name in &self.service.required_environment { + require_environment_name(name)?; + } + for name in &self.service.optional_environment { + require_environment_name(name)?; + ensure!( + !self.service.required_environment.contains(name), + "service environment {name} is both required and optional" + ); + } + + require_capability(&self.state.generation, "state generation")?; + let slot_ids = unique_ids( + self.state.slots.iter().map(|slot| slot.id.as_str()), + "state slot", + )?; + let mut slot_paths = BTreeSet::new(); + for slot in &self.state.slots { + require_relative_path(&slot.relative_path, "state slot path", false)?; + ensure!( + slot_paths.insert(slot.relative_path.as_path()), + "state slot path {} is declared twice", + slot.relative_path.display() + ); + require_capability(&slot.schema, "state slot schema")?; + if slot.writer == StateWriter::ProcessBoundSingleWriter { + ensure!( + slot.startup == StateStartup::CreateOrOpenAfterAdmission, + "writable state slot {} must open after write admission", + slot.id + ); + } + if slot.recovery == StateRecovery::ExternalAuthority { + ensure!( + slot.writer == StateWriter::None, + "externally authoritative state slot {} cannot name the service as writer", + slot.id + ); + } + } + if let Some(migration) = self.state.migration.as_ref() { + ensure!( + artifact_ids.contains(migration.executable_artifact.as_str()), + "migration executable artifact is not declared" + ); + ensure!( + !migration.from_generations.is_empty(), + "migration has no source generations" + ); + for generation in &migration.from_generations { + require_capability(generation, "migration source generation")?; + } + ensure!( + migration.to_generation == self.state.generation, + "migration destination differs from state generation" + ); + for argument in &migration.arguments { + validate_launch_argument(argument)?; + } + } + ensure!( + slot_ids.len() == self.state.slots.len(), + "duplicate state slot" + ); + + let mut provided = BTreeSet::new(); + for capability in &self.provides { + require_capability_contract( + &capability.capability, + &capability.schema, + &capability.compatibility, + )?; + ensure!( + capability.capacity > 0, + "provided capability capacity must be positive" + ); + ensure!( + provided.insert(capability.capability.as_str()), + "capability {} is provided twice", + capability.capability + ); + } + let mut required = BTreeSet::new(); + for dependency in &self.dependencies { + require_capability_contract( + &dependency.capability, + &dependency.schema, + &dependency.compatibility, + )?; + ensure!( + dependency.minimum_capacity > 0, + "dependency capacity must be positive" + ); + ensure!( + required.insert((dependency.kind, dependency.capability.as_str())), + "dependency {} is declared twice for the same kind", + dependency.capability + ); + } + for conflict in &self.conflicts { + require_capability(&conflict.capability, "conflicting capability")?; + require_value(&conflict.reason, "conflict reason")?; + } + Ok(()) + } +} + +impl OperatorBinding { + pub fn parse(input: &str) -> Result { + let binding: Self = + toml::from_str(input).context("decoding strict Idunn operator binding")?; + binding.validate()?; + Ok(binding) + } + + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == OPERATOR_BINDING_SCHEMA, + "unsupported operator binding schema" + ); + require_id(&self.target, "target")?; + ensure!( + self.repository.origin.starts_with("https://"), + "repository origin must use HTTPS" + ); + ensure!( + self.repository.origin.ends_with(".git"), + "repository origin must name a Git repository" + ); + ensure!( + self.repository.admitted_ref.starts_with("refs/heads/"), + "admitted ref must be a full branch ref" + ); + require_sha1(&self.repository.minimum_revision, "minimum revision")?; + match self.repository.selection { + SourceSelectionPolicy::PinnedObject => { + let revision = self.repository.pinned_revision.as_deref().ok_or_else(|| { + anyhow::anyhow!("pinned-object selection has no pinned revision") + })?; + require_sha1(revision, "pinned revision")?; + ensure!( + self.repository.release_authority_store.is_none(), + "pinned-object selection cannot name a release-authority store" + ); + } + SourceSelectionPolicy::RefHead => { + ensure!( + self.repository.pinned_revision.is_none(), + "ref-head selection cannot name a pinned revision" + ); + ensure!( + self.repository.release_authority_store.is_none(), + "ref-head selection cannot name a release-authority store" + ); + } + SourceSelectionPolicy::SignedRelease => { + ensure!( + self.repository.pinned_revision.is_none(), + "signed-release selection cannot name a pinned revision" + ); + let store = self + .repository + .release_authority_store + .as_ref() + .ok_or_else(|| { + anyhow::anyhow!("signed-release selection has no authority store") + })?; + require_absolute_path(store, "release authority store")?; + } + } + require_absolute_path(&self.repository.checkout, "repository checkout")?; + require_relative_path(&self.repository.recipe_path, "recipe path", false)?; + for (path, gitlink) in &self.repository.gitlinks { + require_relative_path(path, "gitlink binding", false)?; + require_git_origin(&gitlink.origin, "gitlink origin")?; + } + ensure!(!self.runners.is_empty(), "operator binding has no runners"); + for (id, runner) in &self.runners { + require_id(id, "runner binding")?; + require_pinned_image(&runner.image)?; + ensure!( + runner.memory_mebibytes > 0, + "runner memory must be positive" + ); + ensure!( + (1..=100_000).contains(&runner.cpu_quota_percent), + "runner CPU quota is outside 1..=100000" + ); + if let Some(cache_root) = runner.cache_root.as_ref() { + require_absolute_path(cache_root, "runner cache root")?; + } + for program in &runner.allowed_programs { + require_program(program, "allowed runner program")?; + } + ensure!( + !runner.allowed_programs.is_empty(), + "runner {id} has no allowed programs" + ); + for host in &runner.allowed_https_hosts { + require_host(host)?; + } + match runner.network { + RunnerNetwork::None => ensure!( + runner.allowed_https_hosts.is_empty(), + "network-none runner {id} has HTTPS hosts" + ), + RunnerNetwork::DependencyEgress => { + ensure!( + runner + .affordances + .contains(&RunnerAffordance::DependencyNetwork), + "dependency-egress runner {id} does not grant dependency-network" + ); + ensure!( + !runner.allowed_https_hosts.is_empty(), + "dependency-egress runner {id} has no allowed hosts" + ); + } + RunnerNetwork::HostPrivate => ensure!( + runner + .affordances + .contains(&RunnerAffordance::PrivateCapabilityAccess), + "host-private runner {id} does not grant private-capability-access" + ), + } + for name in runner.environment.keys().chain(runner.secret_files.keys()) { + require_environment_name(name)?; + } + for path in runner.secret_files.values() { + require_absolute_path(path, "runner secret file")?; + } + if runner.affordances.contains(&RunnerAffordance::BuildCache) { + ensure!( + runner.cache_root.is_some(), + "build-cache runner {id} has no cache root" + ); + } + } + require_identity(&self.workload.user, "workload user")?; + require_identity(&self.workload.group, "workload group")?; + require_id(&self.workload.unit_prefix, "workload unit prefix")?; + for (label, path) in [ + ("release root", &self.workload.release_root), + ("state root", &self.workload.state_root), + ("runtime root", &self.workload.runtime_root), + ("admission record", &self.admission.record_path), + ("admission lock", &self.admission.lock_path), + ("deployment brake", &self.brakes.deployment_store), + ("lifecycle brake", &self.brakes.lifecycle_store), + ] { + require_absolute_path(path, label)?; + } + ensure!( + self.admission.record_path != self.admission.lock_path, + "admission record and lock paths are identical" + ); + ensure!( + self.brakes.deployment_store != self.brakes.lifecycle_store, + "deployment and lifecycle brakes are identical" + ); + ensure!( + self.workload.memory_mebibytes > 0, + "workload memory must be positive" + ); + ensure!( + (1..=100_000).contains(&self.workload.cpu_quota_percent), + "workload CPU quota is outside 1..=100000" + ); + for path in self + .workload + .read_only_paths + .iter() + .chain(self.workload.read_write_paths.iter()) + .chain(self.workload.devices.iter()) + { + require_absolute_path(path, "workload path")?; + } + for capability in &self.workload.capabilities { + require_value(capability, "workload capability")?; + } + for name in self + .workload + .environment + .keys() + .chain(self.workload.secret_files.keys()) + { + require_environment_name(name)?; + } + for name in self.workload.argument_bindings.keys() { + require_id(name, "argument binding")?; + } + for value in self.workload.argument_bindings.values() { + require_value(value, "argument binding value")?; + } + for path in self.workload.secret_files.values() { + require_absolute_path(path, "secret file")?; + } + if let Some(route) = self.route.as_ref() { + require_id(&route.route_id, "route id")?; + require_value(&route.stable_endpoint, "stable endpoint")?; + require_host(&route.private_host)?; + ensure!( + route.private_port_start > 0, + "private port range starts at zero" + ); + ensure!( + route.private_port_start <= route.private_port_end, + "private port range is inverted" + ); + require_absolute_path(&route.config_path, "route config")?; + require_unit(&route.reload_unit, "route reload unit")?; + } + match self.state_transition.policy { + StateTransitionPolicy::Preserve => {} + StateTransitionPolicy::FencedMigration => ensure!( + self.state_transition.backup_root.is_some(), + "fenced migration has no backup root" + ), + StateTransitionPolicy::FreshRoot => ensure!( + self.state_transition.archive_root.is_some(), + "fresh-root transition has no whole-root archive" + ), + } + if let Some(path) = self.state_transition.archive_root.as_ref() { + require_absolute_path(path, "state archive root")?; + } + if let Some(path) = self.state_transition.backup_root.as_ref() { + require_absolute_path(path, "state backup root")?; + } + ensure!( + self.rollout.retain_releases >= 2, + "candidate rollout must retain at least current and prior releases" + ); + ensure!( + self.placement.desired_replicas > 0, + "desired replicas must be positive" + ); + ensure!(!self.placement.nodes.is_empty(), "placement has no nodes"); + for node in &self.placement.nodes { + require_id(node, "placement node")?; + } + for capability in &self.external_capabilities { + require_capability_contract( + &capability.capability, + &capability.schema, + &capability.compatibility, + )?; + require_value(&capability.endpoint, "external capability endpoint")?; + } + for profile in &self.profiles { + require_capability(profile, "profile")?; + } + Ok(()) + } + + pub fn admit(&self, declaration: &TargetDeclaration) -> Result<()> { + ensure!( + self.target == declaration.target, + "binding target does not match declaration" + ); + let bound_gitlinks: BTreeSet<_> = self.repository.gitlinks.keys().cloned().collect(); + ensure!( + bound_gitlinks == declaration.required_gitlinks, + "operator gitlink bindings do not exactly match required gitlinks" + ); + let declared_runners: BTreeSet<_> = declaration + .steps + .iter() + .map(|step| step.runner.as_str()) + .chain( + declaration + .external_artifacts + .iter() + .map(|artifact| artifact.runner.as_str()), + ) + .chain( + declaration + .artifacts + .iter() + .filter_map(|artifact| artifact.runner.as_deref()), + ) + .collect(); + let bound_runners: BTreeSet<_> = self.runners.keys().map(String::as_str).collect(); + ensure!( + declared_runners == bound_runners, + "operator runner classes do not exactly match the target recipe" + ); + for step in &declaration.steps { + let binding = &self.runners[&step.runner]; + ensure!( + binding.affordances.contains(&RunnerAffordance::SourceRead), + "runner {} cannot read the frozen source", + step.runner + ); + ensure!( + binding.allowed_programs.contains(&step.argv[0]), + "runner {} does not admit program {}", + step.runner, + step.argv[0] + ); + let available: BTreeSet<_> = binding + .environment + .keys() + .chain(binding.secret_files.keys()) + .cloned() + .collect(); + ensure!( + step.required_environment.is_subset(&available), + "step {} lacks required runner environment", + step.id + ); + } + for artifact in declaration + .artifacts + .iter() + .filter(|artifact| artifact.source_kind == ArtifactSource::RunnerOutput) + { + let runner_id = artifact + .runner + .as_deref() + .expect("validated runner-output artifact"); + let binding = &self.runners[runner_id]; + ensure!( + binding + .affordances + .contains(&RunnerAffordance::ArtifactWrite), + "runner {runner_id} cannot publish artifact {}", + artifact.id + ); + } + for artifact in &declaration.external_artifacts { + let binding = &self.runners[&artifact.runner]; + ensure!( + binding.affordances.contains(&RunnerAffordance::SourceRead), + "external artifact {} runner cannot read the frozen source", + artifact.id + ); + ensure!( + binding + .affordances + .contains(&RunnerAffordance::ArtifactWrite), + "external artifact {} runner cannot publish artifacts", + artifact.id + ); + ensure!( + binding + .affordances + .contains(&RunnerAffordance::DependencyNetwork), + "external artifact {} runner has no dependency-network affordance", + artifact.id + ); + ensure!( + binding.network == RunnerNetwork::DependencyEgress + && !binding.allowed_https_hosts.is_empty(), + "external artifact {} runner has no admitted egress hosts", + artifact.id + ); + } + let available_environment: BTreeSet<_> = self + .workload + .environment + .keys() + .chain(self.workload.secret_files.keys()) + .cloned() + .collect(); + ensure!( + declaration + .service + .required_environment + .is_subset(&available_environment), + "operator service environment omits a required launch input" + ); + let declared_environment: BTreeSet<_> = declaration + .service + .required_environment + .union(&declaration.service.optional_environment) + .cloned() + .collect(); + ensure!( + available_environment.is_subset(&declared_environment), + "operator service environment contains an undeclared launch input" + ); + let required_arguments: BTreeSet<_> = declaration + .service + .arguments + .iter() + .filter_map(|argument| match argument { + LaunchArgument::Binding { name } => Some(name.clone()), + LaunchArgument::Literal { .. } => None, + }) + .collect(); + let bound_arguments: BTreeSet<_> = + self.workload.argument_bindings.keys().cloned().collect(); + ensure!( + required_arguments == bound_arguments, + "operator argument bindings do not exactly match the launch contract" + ); + match ( + declaration.service.route_required, + declaration.service.transport, + self.route.as_ref().map(|route| route.driver), + ) { + (true, ServiceTransport::Http, Some(RouteDriver::NginxHttp)) + | (true, ServiceTransport::Tcp, Some(RouteDriver::NginxStreamTcp)) + | (false, _, None) => {} + _ => bail!("route binding does not match the launch contract"), + } + match self.state_transition.policy { + StateTransitionPolicy::Preserve => {} + StateTransitionPolicy::FencedMigration => ensure!( + declaration.state.migration.is_some(), + "binding requests migration but target declares no migrator" + ), + StateTransitionPolicy::FreshRoot => {} + } + for dependency in declaration + .dependencies + .iter() + .filter(|dependency| dependency.kind == DependencyKind::ExternalOperatorBinding) + { + ensure!( + self.external_capabilities.iter().any(|binding| { + capability_compatible( + &dependency.capability, + &dependency.schema, + &dependency.compatibility, + &binding.capability, + &binding.schema, + &binding.compatibility, + ) + }), + "external capability {} has no compatible operator binding", + dependency.capability + ); + } + Ok(()) + } + + pub fn admit_external_artifact_manifest( + &self, + declaration: &TargetDeclaration, + artifact_id: &str, + manifest_input: &str, + ) -> Result { + let artifact = declaration + .external_artifacts + .iter() + .find(|artifact| artifact.id == artifact_id) + .with_context(|| format!("target declares no external artifact {artifact_id}"))?; + let runner = self.runners.get(&artifact.runner).with_context(|| { + format!("external artifact runner {} is not bound", artifact.runner) + })?; + ensure!( + runner.network == RunnerNetwork::DependencyEgress, + "external artifact runner has no dependency egress" + ); + let manifest = PinnedTarMemberManifest::parse(manifest_input)?; + let host = https_host(&manifest.url)?; + ensure!( + runner.allowed_https_hosts.contains(host), + "external artifact host {host} is not admitted" + ); + Ok(manifest) + } +} + +pub fn capability_compatible( + required_capability: &str, + required_schema: &str, + required_compatibility: &str, + provided_capability: &str, + provided_schema: &str, + provided_compatibility: &str, +) -> bool { + required_capability == provided_capability + && required_schema == provided_schema + && required_compatibility == provided_compatibility +} + +fn unique_ids<'a>(values: impl Iterator, label: &str) -> Result> { + let mut seen = BTreeSet::new(); + for value in values { + require_id(value, label)?; + ensure!(seen.insert(value), "duplicate {label} id {value}"); + } + Ok(seen) +} + +fn require_id(value: &str, label: &str) -> Result<()> { + ensure!(!value.is_empty(), "{label} is empty"); + ensure!(value.len() <= 96, "{label} is too long"); + ensure!( + value.bytes().all(|byte| byte.is_ascii_lowercase() + || byte.is_ascii_digit() + || matches!(byte, b'-' | b'_' | b'.' | b':')), + "{label} contains an invalid character" + ); + Ok(()) +} + +fn require_capability(value: &str, label: &str) -> Result<()> { + require_id(value, label) +} + +fn require_capability_contract(capability: &str, schema: &str, compatibility: &str) -> Result<()> { + require_capability(capability, "capability")?; + require_capability(schema, "capability schema")?; + require_capability(compatibility, "capability compatibility") +} + +fn require_value(value: &str, label: &str) -> Result<()> { + ensure!(!value.trim().is_empty(), "{label} is empty"); + ensure!(!value.contains('\0'), "{label} contains NUL"); + ensure!(value.len() <= 4_096, "{label} is too long"); + Ok(()) +} + +fn validate_launch_argument(argument: &LaunchArgument) -> Result<()> { + match argument { + LaunchArgument::Literal { value } => require_value(value, "literal launch argument"), + LaunchArgument::Binding { name } => require_id(name, "launch argument binding"), + } +} + +fn require_program(value: &str, label: &str) -> Result<()> { + require_value(value, label)?; + ensure!( + !value.contains('/') && !value.contains('\\') && !value.chars().any(char::is_whitespace), + "{label} must be one executable name" + ); + Ok(()) +} + +fn require_git_origin(value: &str, label: &str) -> Result<()> { + ensure!(value.starts_with("https://"), "{label} must use HTTPS"); + ensure!( + value.ends_with(".git"), + "{label} must name a Git repository" + ); + require_value(value, label) +} + +fn https_host(value: &str) -> Result<&str> { + let remainder = value + .strip_prefix("https://") + .ok_or_else(|| anyhow::anyhow!("artifact URL must use HTTPS"))?; + let authority = remainder.split('/').next().unwrap_or_default(); + ensure!(!authority.is_empty(), "artifact URL has no host"); + ensure!( + !authority.contains('@') + && !authority.contains(':') + && !authority.contains('?') + && !authority.contains('#'), + "artifact URL authority is not a bare host" + ); + require_host(authority)?; + Ok(authority) +} + +fn require_environment_name(value: &str) -> Result<()> { + ensure!( + !value.is_empty() && value.len() <= 128, + "environment name is empty or too long" + ); + ensure!( + value + .bytes() + .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_'), + "environment name contains an invalid character" + ); + Ok(()) +} + +fn require_relative_path(path: &Path, label: &str, allow_current: bool) -> Result<()> { + ensure!(!path.as_os_str().is_empty(), "{label} is empty"); + ensure!(!path.is_absolute(), "{label} must be relative"); + let mut saw_normal = false; + for component in path.components() { + match component { + Component::Normal(_) => saw_normal = true, + Component::CurDir if allow_current => {} + _ => bail!("{label} contains a forbidden path component"), + } + } + ensure!(allow_current || saw_normal, "{label} must name a path"); + Ok(()) +} + +fn require_leaf_path(path: &Path, label: &str) -> Result<()> { + require_relative_path(path, label, false)?; + ensure!( + path.components().count() == 1, + "{label} must be a single filename" + ); + Ok(()) +} + +fn require_absolute_path(path: &Path, label: &str) -> Result<()> { + ensure!(path.is_absolute(), "{label} must be absolute"); + ensure!( + path.components() + .all(|component| !matches!(component, Component::ParentDir)), + "{label} contains a parent traversal" + ); + Ok(()) +} + +fn require_sha1(value: &str, label: &str) -> Result<()> { + require_lower_hex(value, 40, label) +} + +fn require_sha256(value: &str, label: &str) -> Result<()> { + require_lower_hex(value, 64, label) +} + +fn require_lower_hex(value: &str, length: usize, label: &str) -> Result<()> { + ensure!(value.len() == length, "{label} has the wrong length"); + ensure!( + value + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), + "{label} must be lowercase hexadecimal" + ); + Ok(()) +} + +fn require_pinned_image(value: &str) -> Result<()> { + let Some((name, digest)) = value.rsplit_once("@sha256:") else { + bail!("runner image is not pinned by sha256 digest"); + }; + require_value(name, "runner image name")?; + require_sha256(digest, "runner image digest") +} + +fn require_identity(value: &str, label: &str) -> Result<()> { + require_id(value, label) +} + +fn require_unit(value: &str, label: &str) -> Result<()> { + require_value(value, label)?; + ensure!( + value.ends_with(".service"), + "{label} must be a systemd service unit" + ); + ensure!( + !value.contains('/') && !value.contains('\\'), + "{label} must not contain a path" + ); + Ok(()) +} + +fn require_host(value: &str) -> Result<()> { + require_value(value, "host")?; + ensure!( + !value.contains('/') && !value.contains('@'), + "host contains an invalid delimiter" + ); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const RECIPE: &str = r#" +schema = "gamecult.idunn.target_declaration.v1" +target = "test-service" +source_stamp_environment = "TEST_SERVICE_BUILD_COMMIT" + +[[steps]] +id = "test" +phase = "test" +runner = "rust" +argv = ["cargo", "test", "--locked", "-p", "test-service"] + +[[steps]] +id = "build" +phase = "build" +runner = "rust" +argv = ["cargo", "build", "--locked", "--release", "-p", "test-service"] + +[[external_artifacts]] +id = "tool" +runner = "rust" +manifest = "deployment/tool.toml" +destination = "tool" +executable = true + +[[artifacts]] +id = "daemon" +source_kind = "runner-output" +runner = "rust" +source = "target/release/test-service" +destination = "test-service" +executable = true + +[service] +executable_artifact = "daemon" +required_adjacent_artifacts = [] +arguments = [ + { kind = "literal", value = "--state-root" }, + { kind = "binding", name = "state_root" }, +] +transport = "http" +route_required = true +required_environment = ["TEST_SERVICE_BIND"] + +[service.health] +contract = "test-service.cultnet-service-health" + +[service.health.staged] +state = "warming" +detail = "traffic-admission-pending" + +[service.health.ready] +state = "active" +detail = "serving" + +[state] +generation = "v1" + +[[state.slots]] +id = "world" +relative_path = "world.cc" +kind = "cultcache-file" +schema = "test-service.state.v1" +writer = "process-bound-single-writer" +recovery = "preserve" +startup = "create-or-open-after-admission" + +[[provides]] +capability = "test-service.runtime" +schema = "test-service.state.v1" +compatibility = "v1" + +[[dependencies]] +kind = "shared-infrastructure" +capability = "odin.verse-rendezvous" +schema = "odin.verse-topology.v1" +compatibility = "v1" +startup = "before-promotion" +"#; + + const BINDING: &str = r#" +schema = "gamecult.idunn.operator_binding.v1" +target = "test-service" +profiles = ["aetheria", "full-gamecult"] + +[repository] +origin = "https://github.com/GameCult/TestService.git" +admitted_ref = "refs/heads/main" +minimum_revision = "13d5136b69bcd3a52c400d66a940f94a47122e48" +selection = "ref-head" +checkout = "/srv/build/TestService" +recipe_path = "deployment/idunn/recipe.toml" + +[runners.rust] +driver = "docker" +image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" +affordances = ["source-read", "artifact-write", "dependency-network", "build-cache"] +cache_root = "/srv/ghostlight/build-cache" +allowed_https_hosts = ["crates.io", "github.com"] +allowed_programs = ["cargo"] +network = "dependency-egress" +memory_mebibytes = 8192 +cpu_quota_percent = 400 + +[workload] +driver = "systemd-transient" +user = "test-service" +group = "test-service" +unit_prefix = "idunn-test-service" +release_root = "/srv/test-service/releases" +state_root = "/var/lib/gamecult/test-service" +runtime_root = "/etc/gamecult/test-service/runtime" +network = "host-private" +hardening = "strict" +memory_mebibytes = 2048 +cpu_quota_percent = 200 + +[workload.environment] +TEST_SERVICE_BIND = "idunn.private_endpoint" + +[workload.argument_bindings] +state_root = "/var/lib/gamecult/test-service" + +[route] +driver = "nginx-http" +route_id = "test-service-public" +stable_endpoint = "https://yggdrasil.gamecult.org/test-service/" +private_host = "127.0.0.1" +private_port_start = 18831 +private_port_end = 18839 +config_path = "/etc/nginx/idunn-routes/test-service.conf" +reload_unit = "nginx.service" + +[admission] +driver = "atomic-file" +record_path = "/etc/gamecult/test-service/runtime/process-admission.cc" +lock_path = "/etc/gamecult/test-service/runtime/process-admission.cc.lock" + +[brakes] +deployment_store = "/var/lib/gamecult/idunn-authority/test-service-deployment-brake.cc" +lifecycle_store = "/var/lib/gamecult/idunn-authority/test-service-lifecycle-brake.cc" + +[state_transition] +policy = "fresh-root" +archive_root = "/var/lib/gamecult/test-service-cold-archive" + +[rollout] +strategy = "candidate-then-promote" +drain_seconds = 30 +retain_releases = 2 + +[placement] +desired_replicas = 1 +nodes = ["yggdrasil"] +"#; + + #[test] + fn strict_recipe_and_binding_admit_without_host_privilege_in_recipe() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let binding = OperatorBinding::parse(BINDING).unwrap(); + binding.admit(&recipe).unwrap(); + assert_eq!(recipe.target, "test-service"); + assert_eq!(binding.route.as_ref().unwrap().private_port_start, 18831); + } + + #[test] + fn unknown_recipe_field_is_rejected() { + let input = RECIPE.replace( + "target = \"test-service\"", + "target = \"test-service\"\nroot = true", + ); + assert!(TargetDeclaration::parse(&input).is_err()); + } + + #[test] + fn operator_cannot_omit_an_affordance_derived_from_the_recipe() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let input = BINDING.replace( + "affordances = [\"source-read\", \"artifact-write\", \"dependency-network\", \"build-cache\"]", + "affordances = [\"source-read\", \"dependency-network\", \"build-cache\"]", + ); + let binding = OperatorBinding::parse(&input).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + + #[test] + fn route_transport_mismatch_is_rejected() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let binding = + OperatorBinding::parse(&BINDING.replace("nginx-http", "nginx-stream-tcp")).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + + #[test] + fn raw_unit_template_is_rejected() { + let input = RECIPE.replace( + "executable_artifact = \"daemon\"", + "executable_artifact = \"daemon\"\nunit_template = \"deployment/root.service\"", + ); + assert!(TargetDeclaration::parse(&input).is_err()); + } + + #[test] + fn extra_operator_environment_is_rejected() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let input = BINDING.replace( + "TEST_SERVICE_BIND = \"idunn.private_endpoint\"", + "TEST_SERVICE_BIND = \"idunn.private_endpoint\"\nUNDECLARED = \"no\"", + ); + let binding = OperatorBinding::parse(&input).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + + #[test] + fn fresh_root_keeps_target_preservation_semantics_inside_the_new_generation() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let binding = OperatorBinding::parse(BINDING).unwrap(); + binding.admit(&recipe).unwrap(); + assert_eq!(recipe.state.slots[0].recovery, StateRecovery::Preserve); + assert_eq!( + binding.state_transition.policy, + StateTransitionPolicy::FreshRoot + ); + } + + #[test] + fn state_slot_path_cannot_escape_the_operator_owned_root() { + let input = RECIPE.replace( + "relative_path = \"world.cc\"", + "relative_path = \"../world.cc\"", + ); + assert!(TargetDeclaration::parse(&input).is_err()); + } + + #[test] + fn pinned_artifact_host_must_be_operator_admitted() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let binding = OperatorBinding::parse(BINDING).unwrap(); + let manifest = r#" +schema = "gamecult.idunn.pinned_tar_member.v1" +url = "https://registry.npmjs.org/tool.tgz" +archive_sha256 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +member = "package/bin/tool" +member_sha256 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +"#; + assert!( + binding + .admit_external_artifact_manifest(&recipe, "tool", manifest) + .is_err() + ); + let admitted = BINDING.replace( + "allowed_https_hosts = [\"crates.io\", \"github.com\"]", + "allowed_https_hosts = [\"crates.io\", \"github.com\", \"registry.npmjs.org\"]", + ); + let binding = OperatorBinding::parse(&admitted).unwrap(); + binding + .admit_external_artifact_manifest(&recipe, "tool", manifest) + .unwrap(); + } + + #[test] + fn dependency_compatibility_requires_exact_typed_contract() { + assert!(capability_compatible( + "odin.verse-rendezvous", + "odin.verse-topology.v1", + "v1", + "odin.verse-rendezvous", + "odin.verse-topology.v1", + "v1", + )); + assert!(!capability_compatible( + "odin.verse-rendezvous", + "odin.verse-topology.v1", + "v1", + "odin.verse-rendezvous", + "odin.verse-topology.v2", + "v2", + )); + } +} diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs new file mode 100644 index 0000000..583059f --- /dev/null +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -0,0 +1,1322 @@ +use std::collections::{BTreeMap, BTreeSet}; +use std::path::PathBuf; + +use anyhow::{Context, Result, bail, ensure}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::deployment::{ + CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, + ProvidedCapability, TargetDeclaration, capability_compatible, +}; + +pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v1"; +pub const SEALED_DEPLOYMENT_SCHEMA: &str = "idunn.sealed_deployment.v1"; +pub const EXPECTED_GENERATION_SCHEMA: &str = "idunn.expected_generation.v1"; +pub const GENERATION_CORRELATION_SCHEMA: &str = "idunn.generation_correlation.v1"; +pub const PROMOTION_TRANSACTION_SCHEMA: &str = "idunn.promotion_transaction.v1"; +pub const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v1"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ResolvedGitlink { + pub origin: String, + pub revision: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ExactSource { + pub origin: String, + pub admitted_ref: String, + pub revision: String, + pub minimum_revision: String, + pub selection_receipt_sha256: String, + pub gitlinks: BTreeMap, +} + +impl ExactSource { + pub fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { + ensure!( + self.origin == binding.repository.origin, + "exact source origin differs from binding" + ); + ensure!( + self.admitted_ref == binding.repository.admitted_ref, + "exact source ref differs from binding" + ); + require_sha1(&self.revision, "exact source revision")?; + ensure!( + self.minimum_revision == binding.repository.minimum_revision, + "exact source floor differs from binding" + ); + require_sha1(&self.minimum_revision, "exact source floor")?; + require_sha256(&self.selection_receipt_sha256, "source-selection receipt")?; + let expected_paths: BTreeSet<_> = binding.repository.gitlinks.keys().cloned().collect(); + let observed_paths: BTreeSet<_> = self.gitlinks.keys().cloned().collect(); + ensure!( + expected_paths == observed_paths, + "resolved Gitlinks differ from binding" + ); + for (path, observed) in &self.gitlinks { + let expected = &binding.repository.gitlinks[path]; + ensure!( + observed.origin == expected.origin, + "Gitlink {} origin differs from binding", + path.display() + ); + require_sha1(&observed.revision, "Gitlink revision")?; + } + Ok(()) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum ProviderOrigin { + ManagedRuntime, + ExternalOperatorBinding, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ProviderObservation { + pub provider_id: String, + pub provider_target: Option, + pub origin: ProviderOrigin, + pub generation: String, + pub deployment_id: Option, + pub capability: String, + pub schema: String, + pub compatibility: String, + pub capacity: u32, + pub endpoint: String, + pub expected: bool, + pub present: bool, + pub ready: bool, + pub observation_sha256: Option, +} + +impl ProviderObservation { + pub fn validate(&self) -> Result<()> { + require_token(&self.provider_id, "provider id")?; + if let Some(target) = &self.provider_target { + require_token(target, "provider target")?; + } + require_token(&self.generation, "provider generation")?; + if let Some(deployment_id) = &self.deployment_id { + require_token(deployment_id, "provider deployment id")?; + } + require_contract(&self.capability, &self.schema, &self.compatibility)?; + ensure!(self.capacity > 0, "provider capacity is zero"); + require_value(&self.endpoint, "provider endpoint")?; + ensure!( + !self.present || self.expected, + "provider is present without expected admission" + ); + ensure!( + !self.ready || self.present, + "provider is ready without signed presence" + ); + match self.origin { + ProviderOrigin::ManagedRuntime => { + ensure!( + self.provider_target.is_some(), + "managed provider has no target" + ); + if self.present { + ensure!( + self.deployment_id.is_some(), + "present managed provider has no deployment identity" + ); + require_sha256( + self.observation_sha256.as_deref().unwrap_or_default(), + "managed provider observation", + )?; + } + } + ProviderOrigin::ExternalOperatorBinding => { + ensure!( + self.provider_target.is_none(), + "external binding impersonates a managed target" + ); + ensure!( + !self.present && !self.ready, + "external configuration impersonates runtime observation" + ); + ensure!( + self.observation_sha256.is_none(), + "external configuration carries a runtime observation digest" + ); + } + } + Ok(()) + } + + fn compatible_with(&self, dependency: &CapabilityDependency) -> bool { + capability_compatible( + &dependency.capability, + &dependency.schema, + &dependency.compatibility, + &self.capability, + &self.schema, + &self.compatibility, + ) && self.capacity >= dependency.minimum_capacity + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct DependencyResolution { + pub requirement: CapabilityDependency, + pub provider: Option, +} + +impl DependencyResolution { + pub fn blocks_start(&self) -> bool { + self.requirement.startup == crate::deployment::StartupOrder::BeforeStart + && self.requirement.kind != DependencyKind::Optional + && !self + .provider + .as_ref() + .is_some_and(|provider| provider.ready) + } + + pub fn blocks_promotion(&self) -> bool { + self.requirement.kind != DependencyKind::Optional + && !self + .provider + .as_ref() + .is_some_and(|provider| provider.ready) + } +} + +pub fn external_binding_observations( + bindings: &[ExternalCapabilityBinding], +) -> Vec { + bindings + .iter() + .map(|binding| ProviderObservation { + provider_id: format!("operator:{}", binding.capability), + provider_target: None, + origin: ProviderOrigin::ExternalOperatorBinding, + generation: "operator-bound".into(), + deployment_id: None, + capability: binding.capability.clone(), + schema: binding.schema.clone(), + compatibility: binding.compatibility.clone(), + capacity: 1, + endpoint: binding.endpoint.clone(), + expected: true, + present: false, + ready: false, + observation_sha256: None, + }) + .collect() +} + +pub fn resolve_dependencies( + declaration: &TargetDeclaration, + observations: &[ProviderObservation], +) -> Result> { + for observation in observations { + observation.validate()?; + } + for conflict in &declaration.conflicts { + if observations + .iter() + .any(|provider| provider.expected && provider.capability == conflict.capability) + { + bail!( + "capability conflict {}: {}", + conflict.capability, + conflict.reason + ); + } + } + declaration + .dependencies + .iter() + .map(|dependency| { + let mut candidates = observations + .iter() + .filter(|provider| provider.expected && provider.compatible_with(dependency)) + .cloned() + .collect::>(); + if dependency.kind == DependencyKind::ExternalOperatorBinding { + candidates + .retain(|provider| provider.origin == ProviderOrigin::ExternalOperatorBinding); + } else { + candidates.retain(|provider| provider.origin == ProviderOrigin::ManagedRuntime); + } + candidates.sort_by(|left, right| { + right + .ready + .cmp(&left.ready) + .then_with(|| right.present.cmp(&left.present)) + .then_with(|| left.provider_id.cmp(&right.provider_id)) + }); + let provider = candidates.into_iter().next(); + if provider.is_none() && dependency.kind != DependencyKind::Optional { + bail!( + "no expected provider satisfies {} {} {}", + dependency.capability, + dependency.schema, + dependency.compatibility + ); + } + Ok(DependencyResolution { + requirement: dependency.clone(), + provider, + }) + }) + .collect() +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ExpectedGeneration { + pub schema: String, + pub target: String, + pub generation: String, + pub deployment_id: String, + pub source_revision: String, + pub candidate_endpoint: Option, + pub capabilities: Vec, +} + +impl ExpectedGeneration { + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == EXPECTED_GENERATION_SCHEMA, + "unsupported expected-generation schema" + ); + require_token(&self.target, "expected target")?; + require_token(&self.generation, "expected generation")?; + require_token(&self.deployment_id, "expected deployment")?; + require_sha1(&self.source_revision, "expected source revision")?; + if let Some(endpoint) = &self.candidate_endpoint { + require_value(endpoint, "candidate endpoint")?; + } + let mut capabilities = BTreeSet::new(); + for capability in &self.capabilities { + require_contract( + &capability.capability, + &capability.schema, + &capability.compatibility, + )?; + ensure!( + capability.capacity > 0, + "expected capability capacity is zero" + ); + ensure!( + capabilities.insert(( + &capability.capability, + &capability.schema, + &capability.compatibility + )), + "expected capability is duplicated" + ); + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CompiledDeploymentPlan { + pub schema: String, + pub plan_id: String, + pub deployment_id: String, + pub created_at_unix_millis: u64, + pub source: ExactSource, + pub recipe_sha256: String, + pub binding_sha256: String, + pub declaration: TargetDeclaration, + pub binding: OperatorBinding, + pub dependencies: Vec, + pub expected: ExpectedGeneration, +} + +impl CompiledDeploymentPlan { + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == COMPILED_DEPLOYMENT_PLAN_SCHEMA, + "unsupported deployment-plan schema" + ); + require_sha256(&self.plan_id, "plan id")?; + require_token(&self.deployment_id, "deployment id")?; + ensure!( + self.created_at_unix_millis > 0, + "deployment plan has no creation time" + ); + self.declaration.validate()?; + self.binding.validate()?; + self.binding.admit(&self.declaration)?; + self.source.validate_against(&self.binding)?; + require_sha256(&self.recipe_sha256, "recipe digest")?; + require_sha256(&self.binding_sha256, "binding digest")?; + self.expected.validate()?; + ensure!( + self.expected.target == self.declaration.target, + "expected target differs from recipe" + ); + ensure!( + self.expected.generation == self.declaration.state.generation, + "expected generation differs from recipe" + ); + ensure!( + self.expected.deployment_id == self.deployment_id, + "expected deployment differs from plan" + ); + ensure!( + self.expected.source_revision == self.source.revision, + "expected source differs from plan" + ); + ensure!( + self.expected.capabilities == self.declaration.provides, + "expected capabilities differ from recipe" + ); + let resolved_requirements: Vec<_> = self + .dependencies + .iter() + .map(|resolution| &resolution.requirement) + .collect(); + let declared_requirements: Vec<_> = self.declaration.dependencies.iter().collect(); + ensure!( + resolved_requirements == declared_requirements, + "resolved graph differs from recipe dependencies" + ); + for resolution in &self.dependencies { + if let Some(provider) = &resolution.provider { + provider.validate()?; + ensure!( + provider.compatible_with(&resolution.requirement), + "resolved provider is incompatible" + ); + } else { + ensure!( + resolution.requirement.kind == DependencyKind::Optional, + "required dependency is unresolved" + ); + } + } + ensure!( + self.plan_id == self.recomputed_plan_id()?, + "deployment plan digest is not canonical" + ); + Ok(()) + } + + fn recomputed_plan_id(&self) -> Result { + let mut unsigned = self.clone(); + unsigned.plan_id.clear(); + Ok(sha256_id( + &rmp_serde::to_vec(&unsigned).context("encoding deployment plan identity")?, + )) + } +} + +#[allow(clippy::too_many_arguments)] +pub fn compile_deployment_plan( + declaration: TargetDeclaration, + binding: OperatorBinding, + source: ExactSource, + recipe_bytes: &[u8], + binding_bytes: &[u8], + deployment_id: impl Into, + candidate_port: Option, + created_at_unix_millis: u64, + observed_providers: &[ProviderObservation], +) -> Result { + declaration.validate()?; + binding.validate()?; + binding.admit(&declaration)?; + source.validate_against(&binding)?; + let deployment_id = deployment_id.into(); + require_token(&deployment_id, "deployment id")?; + ensure!( + created_at_unix_millis > 0, + "deployment plan has no creation time" + ); + let candidate_endpoint = match (&binding.route, candidate_port) { + (Some(route), Some(port)) => { + ensure!( + (route.private_port_start..=route.private_port_end).contains(&port), + "candidate port is outside the operator range" + ); + Some(format!("{}:{port}", route.private_host)) + } + (None, None) => None, + (Some(_), None) => bail!("routed deployment has no candidate port"), + (None, Some(_)) => bail!("private deployment cannot select a routed candidate port"), + }; + let mut providers = observed_providers.to_vec(); + providers.extend(external_binding_observations( + &binding.external_capabilities, + )); + let dependencies = resolve_dependencies(&declaration, &providers)?; + let expected = ExpectedGeneration { + schema: EXPECTED_GENERATION_SCHEMA.into(), + target: declaration.target.clone(), + generation: declaration.state.generation.clone(), + deployment_id: deployment_id.clone(), + source_revision: source.revision.clone(), + candidate_endpoint, + capabilities: declaration.provides.clone(), + }; + let mut plan = CompiledDeploymentPlan { + schema: COMPILED_DEPLOYMENT_PLAN_SCHEMA.into(), + plan_id: String::new(), + deployment_id, + created_at_unix_millis, + source, + recipe_sha256: sha256_id(recipe_bytes), + binding_sha256: sha256_id(binding_bytes), + declaration, + binding, + dependencies, + expected, + }; + plan.plan_id = plan.recomputed_plan_id()?; + plan.validate()?; + Ok(plan) +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ArtifactSeal { + pub artifact_id: String, + pub destination: PathBuf, + pub sha256: String, + pub size_bytes: u64, + pub executable: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SealedDeployment { + pub schema: String, + pub release_id: String, + pub plan: CompiledDeploymentPlan, + pub plan_sha256: String, + pub artifacts: Vec, + pub sealed_at_unix_millis: u64, +} + +impl SealedDeployment { + pub fn new( + plan: CompiledDeploymentPlan, + release_id: impl Into, + artifacts: Vec, + sealed_at_unix_millis: u64, + ) -> Result { + plan.validate()?; + let mut sealed = Self { + schema: SEALED_DEPLOYMENT_SCHEMA.into(), + release_id: release_id.into(), + plan_sha256: sha256_id(&rmp_serde::to_vec(&plan)?), + plan, + artifacts, + sealed_at_unix_millis, + }; + sealed.validate()?; + sealed + .artifacts + .sort_by(|left, right| left.artifact_id.cmp(&right.artifact_id)); + sealed.validate()?; + Ok(sealed) + } + + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == SEALED_DEPLOYMENT_SCHEMA, + "unsupported sealed-deployment schema" + ); + require_token(&self.release_id, "release id")?; + ensure!( + self.sealed_at_unix_millis >= self.plan.created_at_unix_millis, + "deployment was sealed before it was planned" + ); + self.plan.validate()?; + ensure!( + self.plan_sha256 == sha256_id(&rmp_serde::to_vec(&self.plan)?), + "sealed plan digest is wrong" + ); + let declared = self + .plan + .declaration + .artifacts + .iter() + .map(|artifact| { + ( + artifact.id.as_str(), + artifact.destination.as_path(), + artifact.executable, + ) + }) + .chain( + self.plan + .declaration + .external_artifacts + .iter() + .map(|artifact| { + ( + artifact.id.as_str(), + artifact.destination.as_path(), + artifact.executable, + ) + }), + ) + .collect::>(); + let mut observed = BTreeSet::new(); + for artifact in &self.artifacts { + require_token(&artifact.artifact_id, "artifact seal id")?; + require_sha256(&artifact.sha256, "artifact seal digest")?; + ensure!(artifact.size_bytes > 0, "sealed artifact is empty"); + ensure!( + observed.insert(( + artifact.artifact_id.as_str(), + artifact.destination.as_path(), + artifact.executable + )), + "artifact seal is duplicated" + ); + } + ensure!( + declared == observed, + "sealed artifacts differ from declared outputs" + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RuntimePresence { + pub target: String, + pub generation: String, + pub deployment_id: String, + pub source_revision: String, + pub endpoint: Option, + pub capabilities: Vec, + pub runtime_id: String, + pub signer_identity_id: String, + pub signed_statement_sha256: String, +} + +impl RuntimePresence { + pub fn validate(&self) -> Result<()> { + require_token(&self.target, "presence target")?; + require_token(&self.generation, "presence generation")?; + require_token(&self.deployment_id, "presence deployment")?; + require_sha1(&self.source_revision, "presence source revision")?; + if let Some(endpoint) = &self.endpoint { + require_value(endpoint, "presence endpoint")?; + } + require_token(&self.runtime_id, "presence runtime")?; + require_token(&self.signer_identity_id, "presence signer")?; + require_sha256(&self.signed_statement_sha256, "presence statement")?; + let mut capabilities = BTreeSet::new(); + for capability in &self.capabilities { + require_contract( + &capability.capability, + &capability.schema, + &capability.compatibility, + )?; + ensure!( + capability.capacity > 0, + "presence capability capacity is zero" + ); + ensure!( + capabilities.insert(( + &capability.capability, + &capability.schema, + &capability.compatibility, + )), + "presence capability is duplicated" + ); + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HealthObservation { + pub contract: String, + pub state: String, + pub detail: String, + pub signed_statement_sha256: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GenerationCorrelation { + pub schema: String, + pub target: String, + pub generation: String, + pub expected: bool, + pub present: bool, + pub ready: bool, + pub disagreements: Vec, +} + +pub fn correlate_generation( + plan: &CompiledDeploymentPlan, + presence: Option<&RuntimePresence>, + health: Option<&HealthObservation>, +) -> Result { + plan.validate()?; + let mut disagreements = Vec::new(); + let mut present = false; + if let Some(presence) = presence { + presence.validate()?; + for (matches, code) in [ + (presence.target == plan.expected.target, "target-mismatch"), + ( + presence.generation == plan.expected.generation, + "generation-mismatch", + ), + ( + presence.deployment_id == plan.expected.deployment_id, + "deployment-mismatch", + ), + ( + presence.source_revision == plan.expected.source_revision, + "source-mismatch", + ), + ( + presence.endpoint == plan.expected.candidate_endpoint, + "endpoint-mismatch", + ), + ( + capability_set(&presence.capabilities) + == capability_set(&plan.expected.capabilities), + "capability-mismatch", + ), + ] { + if !matches { + disagreements.push(code.into()); + } + } + present = disagreements.is_empty(); + } else { + disagreements.push("runtime-presence-missing".into()); + } + let mut health_ready = false; + if let (Some(presence), Some(health)) = (presence, health) { + require_token(&health.contract, "health contract")?; + require_value(&health.state, "health state")?; + require_value(&health.detail, "health detail")?; + require_sha256(&health.signed_statement_sha256, "health statement")?; + if health.signed_statement_sha256 != presence.signed_statement_sha256 { + disagreements.push("health-presence-statement-mismatch".into()); + } else if health.contract != plan.declaration.service.health.contract { + disagreements.push("health-contract-mismatch".into()); + } else if health.state != plan.declaration.service.health.ready.state { + disagreements.push("health-state-not-ready".into()); + } else if health.detail != plan.declaration.service.health.ready.detail { + disagreements.push("health-detail-not-ready".into()); + } else { + health_ready = true; + } + } else if health.is_none() { + disagreements.push("health-missing".into()); + } + if plan + .dependencies + .iter() + .any(DependencyResolution::blocks_promotion) + { + disagreements.push("dependency-graph-not-ready".into()); + } + disagreements.sort(); + disagreements.dedup(); + Ok(GenerationCorrelation { + schema: GENERATION_CORRELATION_SCHEMA.into(), + target: plan.expected.target.clone(), + generation: plan.expected.generation.clone(), + expected: true, + present, + ready: present && health_ready && disagreements.is_empty(), + disagreements, + }) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum PromotionPhase { + Planned, + Materialized, + Sealed, + CandidateStarted, + Staged, + Fenced, + RouteObserved, + WriteAdmitted, + Active, + Draining, + Complete, +} + +impl PromotionPhase { + pub fn next(self) -> Option { + Some(match self { + Self::Planned => Self::Materialized, + Self::Materialized => Self::Sealed, + Self::Sealed => Self::CandidateStarted, + Self::CandidateStarted => Self::Staged, + Self::Staged => Self::Fenced, + Self::Fenced => Self::RouteObserved, + Self::RouteObserved => Self::WriteAdmitted, + Self::WriteAdmitted => Self::Active, + Self::Active => Self::Draining, + Self::Draining => Self::Complete, + Self::Complete => return None, + }) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PromotionTransaction { + pub schema: String, + pub transaction_id: String, + pub plan_id: String, + pub target: String, + pub phase: PromotionPhase, + pub revision: u64, + pub updated_at_unix_millis: u64, +} + +impl PromotionTransaction { + pub fn new( + transaction_id: impl Into, + plan: &CompiledDeploymentPlan, + created_at_unix_millis: u64, + ) -> Result { + plan.validate()?; + let transaction = Self { + schema: PROMOTION_TRANSACTION_SCHEMA.into(), + transaction_id: transaction_id.into(), + plan_id: plan.plan_id.clone(), + target: plan.declaration.target.clone(), + phase: PromotionPhase::Planned, + revision: 0, + updated_at_unix_millis: created_at_unix_millis, + }; + transaction.validate()?; + Ok(transaction) + } + + pub fn validate(&self) -> Result<()> { + ensure!( + self.schema == PROMOTION_TRANSACTION_SCHEMA, + "unsupported promotion transaction schema" + ); + require_token(&self.transaction_id, "promotion transaction")?; + require_sha256(&self.plan_id, "promotion plan")?; + require_token(&self.target, "promotion target")?; + ensure!( + self.updated_at_unix_millis > 0, + "promotion transaction has no update time" + ); + Ok(()) + } + + pub fn advance(&self, phase: PromotionPhase, updated_at_unix_millis: u64) -> Result { + self.validate()?; + ensure!( + self.phase.next() == Some(phase), + "promotion phase transition is not adjacent" + ); + ensure!( + updated_at_unix_millis >= self.updated_at_unix_millis, + "promotion time moved backwards" + ); + let next = Self { + schema: self.schema.clone(), + transaction_id: self.transaction_id.clone(), + plan_id: self.plan_id.clone(), + target: self.target.clone(), + phase, + revision: self + .revision + .checked_add(1) + .context("promotion revision overflow")?, + updated_at_unix_millis, + }; + next.validate()?; + Ok(next) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AdmittedGeneration { + pub schema: String, + pub target: String, + pub generation: String, + pub deployment_id: String, + pub plan_id: String, + pub release_id: String, + pub source_revision: String, + pub workload_unit: String, + pub process_id: u32, + pub process_starttime_ticks: u64, + pub private_endpoint: Option, + pub route_id: Option, + pub route_observation_sha256: Option, + pub write_admission_sha256: String, + pub admitted_at_unix_millis: u64, +} + +impl AdmittedGeneration { + pub fn validate_against(&self, sealed: &SealedDeployment) -> Result<()> { + ensure!( + self.schema == ADMITTED_GENERATION_SCHEMA, + "unsupported admitted-generation schema" + ); + sealed.validate()?; + require_token(&self.target, "admitted target")?; + require_token(&self.generation, "admitted generation")?; + require_token(&self.deployment_id, "admitted deployment")?; + require_sha256(&self.plan_id, "admitted plan")?; + require_token(&self.release_id, "admitted release")?; + require_sha1(&self.source_revision, "admitted source")?; + require_token(&self.workload_unit, "admitted workload unit")?; + ensure!(self.process_id > 0, "admitted process id is zero"); + ensure!( + self.process_starttime_ticks > 0, + "admitted process start time is zero" + ); + if let Some(endpoint) = &self.private_endpoint { + require_value(endpoint, "admitted private endpoint")?; + } + match (&self.route_id, &self.route_observation_sha256) { + (Some(route_id), Some(digest)) => { + require_token(route_id, "admitted route")?; + require_sha256(digest, "route observation")?; + } + (None, None) => {} + _ => bail!("admitted route identity and observation are incomplete"), + } + require_sha256(&self.write_admission_sha256, "write admission")?; + ensure!(self.admitted_at_unix_millis > 0, "admission has no time"); + ensure!( + self.target == sealed.plan.declaration.target, + "admitted target differs from sealed plan" + ); + ensure!( + self.generation == sealed.plan.declaration.state.generation, + "admitted generation differs from sealed plan" + ); + ensure!( + self.deployment_id == sealed.plan.deployment_id, + "admitted deployment differs from sealed plan" + ); + ensure!( + self.plan_id == sealed.plan.plan_id, + "admitted plan differs from sealed plan" + ); + ensure!( + self.release_id == sealed.release_id, + "admitted release differs from sealed plan" + ); + ensure!( + self.source_revision == sealed.plan.source.revision, + "admitted source differs from sealed plan" + ); + ensure!( + self.private_endpoint == sealed.plan.expected.candidate_endpoint, + "admitted endpoint differs from expected plan" + ); + match &sealed.plan.binding.route { + Some(route) => ensure!( + self.route_id.as_deref() == Some(route.route_id.as_str()), + "admitted route differs from binding" + ), + None => ensure!( + self.route_id.is_none(), + "private deployment carries a route" + ), + } + Ok(()) + } +} + +fn require_token(value: &str, label: &str) -> Result<()> { + ensure!( + !value.is_empty() && value.len() <= 128, + "{label} is empty or too long" + ); + ensure!( + value + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() + || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/')), + "{label} contains a forbidden character" + ); + Ok(()) +} + +fn require_contract(capability: &str, schema: &str, compatibility: &str) -> Result<()> { + require_token(capability, "capability")?; + require_token(schema, "capability schema")?; + require_token(compatibility, "capability compatibility") +} + +fn capability_set(capabilities: &[ProvidedCapability]) -> BTreeSet<(&str, &str, &str, u32)> { + capabilities + .iter() + .map(|capability| { + ( + capability.capability.as_str(), + capability.schema.as_str(), + capability.compatibility.as_str(), + capability.capacity, + ) + }) + .collect() +} + +fn require_value(value: &str, label: &str) -> Result<()> { + ensure!( + !value.trim().is_empty() && value == value.trim(), + "{label} is empty or noncanonical" + ); + ensure!( + value.len() <= 4096 && !value.contains('\0'), + "{label} is oversized or contains NUL" + ); + Ok(()) +} + +fn require_sha1(value: &str, label: &str) -> Result<()> { + require_lower_hex(value, 40, label) +} + +fn require_sha256(value: &str, label: &str) -> Result<()> { + let digest = value + .strip_prefix("sha256-") + .with_context(|| format!("{label} has no sha256- prefix"))?; + require_lower_hex(digest, 64, label) +} + +fn require_lower_hex(value: &str, length: usize, label: &str) -> Result<()> { + ensure!(value.len() == length, "{label} has the wrong length"); + ensure!( + value + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), + "{label} is not lowercase hexadecimal" + ); + Ok(()) +} + +fn sha256_id(bytes: &[u8]) -> String { + let digest = Sha256::digest(bytes); + let mut encoded = String::with_capacity(71); + encoded.push_str("sha256-"); + for byte in digest { + use std::fmt::Write as _; + write!(&mut encoded, "{byte:02x}").expect("writing to String cannot fail"); + } + encoded +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::deployment::{OperatorBinding, TargetDeclaration}; + + const RECIPE: &str = r#" +schema = "gamecult.idunn.target_declaration.v1" +target = "service" +source_stamp_environment = "SERVICE_BUILD_COMMIT" + +[[steps]] +id = "build" +phase = "build" +runner = "rust" +argv = ["cargo", "build", "--locked"] + +[[artifacts]] +id = "daemon" +source_kind = "runner-output" +runner = "rust" +source = "target/release/service" +destination = "service" +executable = true + +[service] +executable_artifact = "daemon" +transport = "http" +route_required = true +required_environment = ["SERVICE_BIND"] + +[service.health] +contract = "service.health" + +[service.health.staged] +state = "warming" +detail = "traffic-admission-pending" + +[service.health.ready] +state = "active" +detail = "serving" + +[state] +generation = "v1" + +[[provides]] +capability = "service.runtime" +schema = "service.runtime.v1" +compatibility = "v1" + +[[dependencies]] +kind = "shared-infrastructure" +capability = "odin.verse-rendezvous" +schema = "odin.verse-topology.v1" +compatibility = "v1" +"#; + + const BINDING: &str = r#" +schema = "gamecult.idunn.operator_binding.v1" +target = "service" + +[repository] +origin = "https://github.com/GameCult/Service.git" +admitted_ref = "refs/heads/main" +minimum_revision = "1111111111111111111111111111111111111111" +selection = "ref-head" +checkout = "/srv/build/Service" +recipe_path = "deployment/idunn/recipe.toml" + +[runners.rust] +driver = "docker" +image = "rust@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +affordances = ["source-read", "artifact-write"] +allowed_programs = ["cargo"] +network = "none" +memory_mebibytes = 2048 +cpu_quota_percent = 200 + +[workload] +driver = "systemd-transient" +user = "service" +group = "service" +unit_prefix = "idunn-service" +release_root = "/srv/service/releases" +state_root = "/var/lib/gamecult/service" +runtime_root = "/run/gamecult/idunn/service" +network = "host-private" +hardening = "strict" +memory_mebibytes = 1024 +cpu_quota_percent = 100 + +[workload.environment] +SERVICE_BIND = "idunn.private_endpoint" + +[route] +driver = "nginx-http" +route_id = "service" +stable_endpoint = "https://example.invalid/service/" +private_host = "127.0.0.1" +private_port_start = 18000 +private_port_end = 18009 +config_path = "/etc/nginx/idunn-routes/service.conf" +reload_unit = "nginx.service" + +[admission] +driver = "atomic-file" +record_path = "/run/gamecult/idunn/service/admission.cc" +lock_path = "/run/gamecult/idunn/service/admission.cc.lock" + +[brakes] +deployment_store = "/var/lib/gamecult/idunn/service-deployment-brake.cc" +lifecycle_store = "/var/lib/gamecult/idunn/service-lifecycle-brake.cc" + +[state_transition] +policy = "preserve" + +[rollout] +strategy = "candidate-then-promote" +drain_seconds = 30 +retain_releases = 2 + +[placement] +desired_replicas = 1 +nodes = ["yggdrasil"] +"#; + + fn provider(ready: bool) -> ProviderObservation { + ProviderObservation { + provider_id: "odin-yggdrasil".into(), + provider_target: Some("odin".into()), + origin: ProviderOrigin::ManagedRuntime, + generation: "odin-v1".into(), + deployment_id: Some("odin-deployment-1".into()), + capability: "odin.verse-rendezvous".into(), + schema: "odin.verse-topology.v1".into(), + compatibility: "v1".into(), + capacity: 1, + endpoint: "10.77.0.1:17871".into(), + expected: true, + present: ready, + ready, + observation_sha256: ready.then(|| format!("sha256-{}", "a".repeat(64))), + } + } + + fn plan(provider: ProviderObservation) -> CompiledDeploymentPlan { + compile_deployment_plan( + TargetDeclaration::parse(RECIPE).unwrap(), + OperatorBinding::parse(BINDING).unwrap(), + ExactSource { + origin: "https://github.com/GameCult/Service.git".into(), + admitted_ref: "refs/heads/main".into(), + revision: "2222222222222222222222222222222222222222".into(), + minimum_revision: "1111111111111111111111111111111111111111".into(), + selection_receipt_sha256: format!("sha256-{}", "d".repeat(64)), + gitlinks: BTreeMap::new(), + }, + RECIPE.as_bytes(), + BINDING.as_bytes(), + "deployment-1", + Some(18001), + 100, + &[provider], + ) + .unwrap() + } + + #[test] + fn compilation_is_deterministic_and_configuration_does_not_supply_readiness() { + let first = plan(provider(true)); + let second = plan(provider(true)); + assert_eq!(first, second); + assert!(!first.dependencies[0].blocks_promotion()); + + let external = ExternalCapabilityBinding { + capability: "odin.verse-rendezvous".into(), + schema: "odin.verse-topology.v1".into(), + compatibility: "v1".into(), + endpoint: "10.77.0.1:17871".into(), + }; + let observations = external_binding_observations(&[external]); + assert!(!observations[0].present); + assert!(!observations[0].ready); + } + + #[test] + fn shared_infrastructure_reuses_the_ready_provider() { + let mut weaker = provider(false); + weaker.provider_id = "odin-candidate".into(); + let resolved = resolve_dependencies( + &TargetDeclaration::parse(RECIPE).unwrap(), + &[weaker, provider(true)], + ) + .unwrap(); + assert_eq!( + resolved[0].provider.as_ref().unwrap().provider_id, + "odin-yggdrasil" + ); + } + + #[test] + fn present_and_ready_are_distinct_from_expected() { + let plan = plan(provider(true)); + let presence = RuntimePresence { + target: "service".into(), + generation: "v1".into(), + deployment_id: "deployment-1".into(), + source_revision: "2222222222222222222222222222222222222222".into(), + endpoint: Some("127.0.0.1:18001".into()), + capabilities: plan.expected.capabilities.clone(), + runtime_id: "service-runtime".into(), + signer_identity_id: "signer-1".into(), + signed_statement_sha256: format!("sha256-{}", "b".repeat(64)), + }; + let health = HealthObservation { + contract: "service.health".into(), + state: "active".into(), + detail: "serving".into(), + signed_statement_sha256: presence.signed_statement_sha256.clone(), + }; + let absent = correlate_generation(&plan, None, None).unwrap(); + assert!(absent.expected); + assert!(!absent.present); + assert!(!absent.ready); + let ready = correlate_generation(&plan, Some(&presence), Some(&health)).unwrap(); + assert!(ready.present); + assert!(ready.ready); + } + + #[test] + fn disagreement_remains_visible_and_blocks_readiness() { + let plan = plan(provider(true)); + let presence = RuntimePresence { + target: "service".into(), + generation: "wrong-generation".into(), + deployment_id: "deployment-1".into(), + source_revision: "2222222222222222222222222222222222222222".into(), + endpoint: Some("127.0.0.1:18001".into()), + capabilities: plan.expected.capabilities.clone(), + runtime_id: "service-runtime".into(), + signer_identity_id: "signer-1".into(), + signed_statement_sha256: format!("sha256-{}", "b".repeat(64)), + }; + let health = HealthObservation { + contract: "service.health".into(), + state: "active".into(), + detail: "serving".into(), + signed_statement_sha256: presence.signed_statement_sha256.clone(), + }; + let correlation = correlate_generation(&plan, Some(&presence), Some(&health)).unwrap(); + assert!(!correlation.present); + assert!(!correlation.ready); + assert!( + correlation + .disagreements + .contains(&"generation-mismatch".into()) + ); + } + + #[test] + fn sealed_artifacts_and_promotion_phases_are_exact() { + let plan = plan(provider(true)); + let sealed = SealedDeployment::new( + plan.clone(), + "release-1", + vec![ArtifactSeal { + artifact_id: "daemon".into(), + destination: "service".into(), + sha256: format!("sha256-{}", "c".repeat(64)), + size_bytes: 42, + executable: true, + }], + 110, + ) + .unwrap(); + sealed.validate().unwrap(); + let transaction = PromotionTransaction { + schema: PROMOTION_TRANSACTION_SCHEMA.into(), + transaction_id: "transaction-1".into(), + plan_id: plan.plan_id, + target: "service".into(), + phase: PromotionPhase::Planned, + revision: 0, + updated_at_unix_millis: 100, + }; + assert!(transaction.advance(PromotionPhase::Sealed, 101).is_err()); + assert_eq!( + transaction + .advance(PromotionPhase::Materialized, 101) + .unwrap() + .phase, + PromotionPhase::Materialized + ); + } +} diff --git a/crates/idunn-daemon/src/lib.rs b/crates/idunn-daemon/src/lib.rs index 58309eb..7474d59 100644 --- a/crates/idunn-daemon/src/lib.rs +++ b/crates/idunn-daemon/src/lib.rs @@ -1 +1,3 @@ +pub mod deployment; +pub mod deployment_plan; pub mod provisioning; diff --git a/docs/idunn-deployment-authority.md b/docs/idunn-deployment-authority.md new file mode 100644 index 0000000..7d1bce6 --- /dev/null +++ b/docs/idunn-deployment-authority.md @@ -0,0 +1,291 @@ +# Idunn Deployment Authority + +Idunn is GameCult's deployment, runtime-admission, continuity, and future swarm +control plane. It decides which exact workload incarnation may act. It does not +replace systemd, container runtimes, nginx, or a future general-purpose +orchestrator. + +The architectural test is simple: + +- Where and how a workload runs is generic infrastructure work. Use the + configured systemd, container, proxy, or later Nomad/Kubernetes driver. +- Which GameCult incarnation has authority to serve, write state, or represent + an admitted generation is Idunn work. + +Idunn and Odin are closely integrated but do not share authority. Idunn owns +the admitted physical swarm. Odin owns the discoverable semantic topology of +the Verse. A service owns its signed runtime presence, capabilities, capacity, +and health. CultMesh carries typed projections between them. + +## Authority map + +- **Owner:** Idunn alone selects an exact source, admits its repository recipe + under an operator binding, records the resulting artifact and process + provenance, grants process-bound route and state-write admission, and keeps + the admitted generation alive. +- **Inputs:** root-admitted operator bindings; source-owned strict deployment + declarations; exact Git objects read as the `idunn` identity; signed + deployment or lifecycle-brake state; signed generation-bound service + presence and health; Odin's observed capability topology when graph changes + require semantic discovery; and observations from the configured workload + and route drivers. +- **Outputs:** typed CultCache deployment plans and receipts; an Idunn-owned + expected-fleet CultMesh projection; process-bound write grants; admitted + route membership; deployment and continuity decisions; and explicit + expected/present/ready disagreement records. +- **Derived state:** release dashboards, operator summaries, route displays, + capacity summaries, and Odin's correlation views are projections. A target + count, proxy backend, service-manager unit, container label, or configured + endpoint cannot establish admission by itself. +- **Forbidden writers:** target repositories cannot grant themselves users, + secrets, mounts, network, capabilities, resources, host paths, routes, + placement, source refs, or rollout policy. Odin cannot promote a process or + grant state writes. nginx cannot choose membership. systemd and container + runtimes cannot choose the admitted generation. Service configuration cannot + impersonate observed presence or readiness. Persisted shell strings and + repository-supplied root programs are not executable authority. +- **Shared paths:** `idunn up`, scheduled deployment, manual deployment, + scaling, restart, host reboot recovery, and crash recovery use the same + source-to-recipe-to-artifact provenance and the same admission primitive. + Deployment may change that chain only while the deployment brake admits the + exact transaction. Continuity may restart only the already-admitted chain and + is governed by a separate lifecycle brake. +- **Deletion line:** hard-coded target copies, raw `deploy_command` and + `restart_command` state, `sh -c` actuation, root Git inspection, staged + `/srv/odin/deploy-manifests`, target-specific gamecult-ops deploy programs, + duplicate target units, and stop-incumbent-before-probe rollouts must cease to + decide Ghostlight or CodexConnector deployment before either target is + promoted through this design. + +## The three truths Odin exposes + +For each generation, Odin keeps three distinct observations: + +1. **Expected** — Idunn has admitted or intends this exact generation and has + published its deployment identity, endpoints, declared capabilities, and + dependency role. +2. **Present** — that generation has established its own signed CultNet/CultMesh + runtime session. +3. **Ready** — the observed generation satisfies its signed capability and + health contract and its required dependency graph is ready. + +Correlation never repairs disagreement. Generation, runtime identity, endpoint, +schema, capability, and dependency mismatches remain visible and block +promotion. Configuration contributes only expected state; it cannot supply +present or ready state. + +Every Idunn-managed deployment has an Odin-visible expected projection before +dependent services are promoted, and an Odin-observed runtime projection before +they are considered ready. + +## Source-owned deployment declaration + +Each deployable repository publishes one small, reviewable declaration at a +fixed path such as `deployment/idunn/recipe.toml`. Unknown fields are rejected. +The declaration contains no host commands or privileges. It describes: + +- schema and target identity; +- requested runner profiles; +- build and test argv executed inside those runners; +- package inputs and immutable artifact outputs; +- a constrained launch contract naming the packaged executable, literal + arguments, operator-bound argument slots, and required environment names; +- health, staged-readiness, and state/schema contracts; +- capabilities provided, including schema and compatibility information; +- bootstrap, required, optional, shared-infrastructure, private, and external + operator-bound capability dependencies; +- startup ordering, conflicts, migration conditions, and minimum capacity when + they matter. + +Dependencies resolve by typed capability/provider compatibility. A declaration +names a daemon only when identity itself is the contract; ordinary dependencies +must not hard-code a provider instance. + +The declaration may run repository code only inside a runner sandbox admitted +by the operator binding. It cannot publish a host path, Docker argument, +systemd property, nginx fragment, UID, secret path, or capability grant. Idunn +deterministically lowers the constrained launch contract through the configured +workload driver; repository-owned raw unit or container-runtime text is not an +input. + +## Operator binding + +An operator binding is static root-admitted configuration, not observed state. +For one target on one fleet it owns: + +- canonical repository origin, admitted ref, and minimum revision; +- exact runner kind, image digest, and affordances for each requested profile; +- source, cache, output, release, state, and runtime roots; +- network mode, allowed dependency egress, mounts, identities, secrets, + capabilities, resource bounds, and device access; +- workload driver, transient-unit namespace, private endpoint range, and stable + route; +- route driver and its root-owned configuration destination; +- rollout, drain, irreversible-cut, and retention policy; +- desired replicas and allowed placement; +- externally supplied capability bindings. + +A recipe request that has no compatible admitted binding fails closed. A +binding can grant less than requested; it cannot silently substitute a +different semantic capability. + +`gamecult-ops` publishes these fleet bindings and provisions host substrate. It +does not own application build or deployment programs. + +## Typed deployment state + +Mutable control-plane state is CultCache. The live path uses typed records for: + +- requested service or profile; +- resolved capability graph and exact providers; +- exact source and recipe digest; +- runner binding and artifact digests; +- candidate process, private endpoint, and signed staged health; +- process-bound write lease and route admission; +- expected, present, and ready correlation; +- promotion, drain, failure, and continuity results. + +Old records containing raw commands are readable history only. No executor may +interpret their command text. + +State transition is also typed authority. The repository may package a +migration artifact and declare its accepted source schemas, destination schema, +and literal/operator-bound argv. The operator binding admits the state roots, +backup root, and whether this rollout permits migration or a fresh-root cut. +Idunn alone issues a fenced one-shot migration grant bound to the exact source, +artifact, state generation, and deployment. A target migration never receives +general deployment or lifecycle authority. + +## Initial drivers and replaceable boundary + +The first Yggdrasil implementation uses: + +- transient systemd units for process lifetime and isolation, regenerated from + Idunn's admitted state after Idunn starts; +- Docker only for admitted build/test/package runners; +- nginx HTTP proxying for Ghostlight; +- nginx stream proxying for CodexConnector's stable TCP endpoint; +- atomic root-owned files for process/write admission and generated proxy + membership; +- graceful nginx validation and reload for route changes. + +Idunn calls narrow workload, route, and admission/fencing driver ports. Those +ports express stage, start, observe, stop, validate route, publish a fenced or +process-bound admission, and promote route. They do not express GameCult +admission decisions. A later Nomad or Kubernetes driver may implement the same +consequences. Idunn must not grow general bin-packing, distributed +scheduling, service-mesh, container-format, cryptography, or consensus logic; +when those become requirements, place Idunn's admission semantics over an +established orchestrator. + +## Candidate promotion + +For a singleton generation: + +1. Resolve the required capability graph. Reuse a compatible admitted shared + provider, especially Odin, rather than spawning a duplicate. +2. Publish the desired fleet and candidate as expected state. +3. Freeze the exact source and recipe as `idunn`, then copy and verify it into a + root-owned actuation stage. The privileged driver never opens Git. +4. Build, test, and package inside the bound runners. Seal the artifact, recipe, + unit, and input digests. +5. Start the candidate through the workload driver on a private endpoint. It + has no stable route or canonical write lease. +6. Require its own signed generation-bound `staged` health and signed runtime + presence. Render and validate the proposed route before changing admission. +7. Compare-and-swap the current process/write-admission record into a named + promotion fence. No generation may write while fenced; the incumbent may + remain routed but must return a retryable unavailable result. +8. Publish the candidate backend, gracefully reload the route driver, and + observe that the proxy has adopted it. Proxy lag is explicit transaction + state, not readiness. +9. Compare-and-swap the fence into the candidate's process-bound write grant. + Wait for signed `active` health, then drain and stop the incumbent. + +The admission record is the one writer lease. A PID, endpoint, symlink, unit, +or route alone never grants writes. Outside the explicit promotion fence, the +write-admitted process and routed generation must agree. Runtimes must bind and +publish staged health without opening canonical writable state; they open or +mutate that state only after the exact process-bound admission becomes current. + +Promotion persists crash-resumable phases: planned, materialized, sealed, +candidate-started, staged, fenced, route-observed, write-admitted, active, +draining, and complete. A dead Idunn cannot leave a second writer. Its successor +resumes the exact sealed transaction or restores the prior admitted generation +through the same fence; it never infers completion from a symlink or eventual +health convergence. + +An irreversible state cut fails closed after the write lease moves. A reversible +cut may restore the incumbent only through the same admission primitive and +only while its artifact and state contract remain valid. + +## Continuity and outage behavior + +Continuity restarts the exact sealed release, binding, endpoint, and admission +identity. It does not fetch a branch, rebuild, rerun a migration, alter a unit, +or consume the deployment brake. An explicit lifecycle brake may suspend this +actuation without changing deployment authority. + +Idunn starts and recovers from its own durable admitted state. Odin is the first +managed semantic daemon, never an Idunn bootstrap dependency. Odin's initial +admission is the sole graph-bootstrap exception: Idunn uses its root-admitted +local binding, publishes Odin's expected projection into durable CultMesh state, +and waits for Odin's own signed presence before admitting dependents. + +When Odin is unavailable, Idunn continues health observation and continuity for +already admitted generations and leaves current routes in place. It does not +begin a graph-changing transaction. An already sealed transaction may continue +only from its pinned graph and expected-generation snapshot; once a promotion +fence exists, safety recovery proceeds without waiting for Odin so the fleet is +not stranded with an ambiguous writer. New deployment, dependency substitution, +or scaling waits for semantic discovery. When Odin returns, disagreement is +correlated and exposed before graph-changing work resumes. + +Source selection is explicit per binding: a pinned object, the captured head of +an admitted ref, or a signed release authorization. Every policy freezes one +exact commit in the plan and proves origin, reachability, recipe digest, and any +minimum floor before build. “Whatever the branch points to when root runsâ€� is +not a selection policy. + +## Garden path + +The ordinary operator surface is: + +```text +install/start Idunn +idunn up ghostlight +idunn up profile:aetheria +idunn up profile:full-gamecult +``` + +`idunn up` ensures a compatible Odin is admitted first, publishes desired fleet +state, resolves the capability graph, stages candidates, waits for signed +presence and readiness, then promotes routes. An “Idunn scriptâ€� means the small +repository declaration plus this standard command, not another imperative host +deployment program. + +## Verification + +The first live cut is complete only when tests and host observations prove: + +- exact source → recipe → artifact → process provenance; +- target requests cannot grant an omitted runner or host affordance; +- old command-bearing records and old target scripts are unexecutable; +- a candidate cannot write or receive the stable route before admission; +- route and write admission name the same generation and process; +- expected, present, and ready disagreement blocks promotion and remains + observable; +- replayed or stale signed health cannot satisfy present or ready; +- external operator-bound dependencies still require observed signed presence + and readiness; configuration supplies only expected state; +- a compatible shared Odin is reused and a second provider is not spawned; +- Idunn death during every promotion phase resumes or fails closed without a + second writer; +- proxy-driver lag remains visible and cannot be mistaken for route admission; +- the old process rejects writes immediately after fence or lease revocation; +- same-release crash recovery works while the deployment brake is engaged; +- the lifecycle brake can stop continuity without admitting a deployment; +- Odin loss preserves current continuity/routes and blocks graph changes; +- the incumbent remains routed until the candidate is signed-staged; +- the old unit, release, state root, grant, and route cannot regain authority; +- Ghostlight restart preserves the exact world journal and digest identity. diff --git a/docs/idunn.md b/docs/idunn.md index e13622b..002766c 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -11,6 +11,13 @@ Idunn keeps the apples: desired daemon presence, deployment freshness, boot recovery, crash recovery, health freshness, deploy/restart intent, operator escalation, and continuity witness state. +The current deployment and runtime-admission contract is +[`idunn-deployment-authority.md`](idunn-deployment-authority.md). That contract +owns target recipes, operator bindings, capability-graph admission, +expected/present/ready correlation, candidate promotion, route admission, and +the separation between deployment and continuity. This page describes the +broader keepalive body and its existing state surfaces. + ## Authority Map - Owner: Idunn owns daemon lifecycle management from its root-admitted target From 167c23427616172a20114bb0e13e8e45abd65e9f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 18:53:12 +0200 Subject: [PATCH 75/93] Lock Idunn deployment parser dependencies --- Cargo.lock | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 953c54f..4625713 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,6 +514,7 @@ dependencies = [ "serde", "sha2", "tempfile", + "toml", "uuid", ] @@ -890,6 +891,15 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + [[package]] name = "sha2" version = "0.10.9" @@ -986,6 +996,47 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "typenum" version = "1.20.1" @@ -1315,6 +1366,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] + [[package]] name = "wit-bindgen" version = "0.51.0" From cebe4259cefddb1b9014106cf986ac01938af459 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 21:02:30 +0200 Subject: [PATCH 76/93] Simplify Idunn deployment authority contracts --- crates/idunn-daemon/src/deployment.rs | 730 ++++---- crates/idunn-daemon/src/deployment_plan.rs | 1751 ++++++++++---------- docs/idunn-deployment-authority.md | 226 ++- docs/idunn.md | 627 ++----- vendor/cultnet-rs | 2 +- 5 files changed, 1639 insertions(+), 1697 deletions(-) diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index 56fae5b..51e4245 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize}; pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v1"; -pub const PINNED_TAR_MEMBER_SCHEMA: &str = "gamecult.idunn.pinned_tar_member.v1"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -19,10 +18,11 @@ pub struct TargetDeclaration { #[serde(default)] pub steps: Vec, #[serde(default)] - pub external_artifacts: Vec, + pub external_inputs: Vec, pub artifacts: Vec, pub service: ServiceDeclaration, - pub state: StateDeclaration, + #[serde(default)] + pub state: Option, #[serde(default)] pub provides: Vec, #[serde(default)] @@ -36,9 +36,7 @@ pub struct TargetDeclaration { pub enum RunnerAffordance { SourceRead, ArtifactWrite, - DependencyNetwork, BuildCache, - PrivateCapabilityAccess, SecretRead, } @@ -70,39 +68,12 @@ pub enum RecipePhase { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ExternalArtifact { +pub struct ExternalInput { pub id: String, - pub runner: String, - pub manifest: PathBuf, - pub destination: PathBuf, - #[serde(default)] - pub executable: bool, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct PinnedTarMemberManifest { - pub schema: String, pub url: String, - pub archive_sha256: String, - pub member: PathBuf, - pub member_sha256: String, -} - -impl PinnedTarMemberManifest { - pub fn parse(input: &str) -> Result { - let manifest: Self = - toml::from_str(input).context("decoding strict pinned tar-member manifest")?; - ensure!( - manifest.schema == PINNED_TAR_MEMBER_SCHEMA, - "unsupported pinned artifact schema" - ); - https_host(&manifest.url)?; - require_sha256(&manifest.archive_sha256, "archive sha256")?; - require_relative_path(&manifest.member, "archive member", false)?; - require_sha256(&manifest.member_sha256, "member sha256")?; - Ok(manifest) - } + pub sha256: String, + pub runner: String, + pub destination: PathBuf, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -114,6 +85,8 @@ pub struct ArtifactOutput { pub source: PathBuf, pub destination: PathBuf, #[serde(default)] + pub expected_sha256: Option, + #[serde(default)] pub executable: bool, } @@ -146,15 +119,6 @@ pub struct ServiceDeclaration { #[serde(deny_unknown_fields)] pub struct HealthDeclaration { pub contract: String, - pub staged: HealthState, - pub ready: HealthState, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct HealthState { - pub state: String, - pub detail: String, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -176,7 +140,7 @@ pub enum LaunchArgument { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct StateDeclaration { - pub generation: String, + pub schema_generation: String, pub slots: Vec, pub migration: Option, } @@ -227,7 +191,8 @@ pub enum StateRecovery { #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum StateStartup { - CreateOrOpenAfterAdmission, + CreateOrOpenAfterWriteLease, + OpenAtStart, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -291,10 +256,13 @@ pub struct OperatorBinding { pub repository: RepositoryBinding, pub runners: BTreeMap, pub workload: WorkloadBinding, + pub runtime_identity: RuntimeIdentityBinding, pub route: Option, - pub admission: AdmissionBinding, + #[serde(default)] + pub process_write_lease: Option, pub brakes: BrakeBinding, - pub state_transition: StateTransitionBinding, + #[serde(default)] + pub state_transition: Option, pub rollout: RolloutBinding, pub placement: PlacementBinding, #[serde(default)] @@ -341,14 +309,13 @@ pub struct RunnerBinding { pub affordances: BTreeSet, pub cache_root: Option, #[serde(default)] - pub allowed_https_hosts: BTreeSet, - #[serde(default)] pub allowed_programs: BTreeSet, #[serde(default)] pub environment: BTreeMap, #[serde(default)] pub secret_files: BTreeMap, - pub network: RunnerNetwork, + #[serde(default)] + pub network_profile: Option, pub memory_mebibytes: u32, pub cpu_quota_percent: u32, } @@ -359,14 +326,6 @@ pub enum RunnerDriver { Docker, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RunnerNetwork { - None, - DependencyEgress, - HostPrivate, -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct WorkloadBinding { @@ -375,7 +334,8 @@ pub struct WorkloadBinding { pub group: String, pub unit_prefix: String, pub release_root: PathBuf, - pub state_root: PathBuf, + #[serde(default)] + pub state_root: Option, pub runtime_root: PathBuf, pub network: WorkloadNetwork, pub hardening: WorkloadHardening, @@ -418,16 +378,24 @@ pub enum WorkloadHardening { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct AdmissionBinding { - pub driver: AdmissionDriver, - pub record_path: PathBuf, - pub lock_path: PathBuf, +pub struct RuntimeIdentityBinding { + pub runtime_id: String, + pub expected_signer_identity_id: String, + pub trust_anchor_store: PathBuf, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum AdmissionDriver { - AtomicFile, +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ProcessWriteLeaseBinding { + pub record_path: PathBuf, +} + +impl ProcessWriteLeaseBinding { + pub fn lock_path(&self) -> PathBuf { + let mut lock_path = self.record_path.as_os_str().to_os_string(); + lock_path.push(".lock"); + PathBuf::from(lock_path) + } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -497,9 +465,12 @@ pub struct PlacementBinding { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ExternalCapabilityBinding { + pub provider_id: String, pub capability: String, pub schema: String, pub compatibility: String, + #[serde(default = "one")] + pub capacity: u32, pub endpoint: String, } @@ -554,6 +525,9 @@ impl TargetDeclaration { ); require_relative_path(&artifact.source, "artifact source", false)?; require_leaf_path(&artifact.destination, "artifact destination")?; + if let Some(expected_sha256) = &artifact.expected_sha256 { + require_sha256(expected_sha256, "expected artifact sha256")?; + } ensure!( destinations.insert(artifact.destination.as_path()), "artifact destination {} is declared twice", @@ -574,21 +548,32 @@ impl TargetDeclaration { ), } } - for artifact in &self.external_artifacts { - require_id(&artifact.id, "external artifact")?; + let mut external_input_ids = BTreeSet::new(); + let mut external_input_destinations = BTreeSet::new(); + for input in &self.external_inputs { + require_id(&input.id, "external input")?; ensure!( - artifact_ids.insert(artifact.id.as_str()), - "artifact id {} is declared twice", - artifact.id + external_input_ids.insert(input.id.as_str()), + "external input id {} is declared twice", + input.id ); - require_id(&artifact.runner, "external artifact runner class")?; - runner_ids.insert(artifact.runner.as_str()); - require_relative_path(&artifact.manifest, "external artifact manifest", false)?; - require_leaf_path(&artifact.destination, "external artifact destination")?; ensure!( - destinations.insert(artifact.destination.as_path()), - "artifact destination {} is declared twice", - artifact.destination.display() + !artifact_ids.contains(input.id.as_str()), + "external input id {} collides with an artifact id", + input.id + ); + require_value(&input.url, "external input URL")?; + https_host(&input.url)?; + require_sha256(&input.sha256, "external input sha256")?; + require_id(&input.runner, "external input runner class")?; + runner_ids.insert(input.runner.as_str()); + require_relative_path(&input.destination, "external input destination", false)?; + ensure!( + external_input_destinations + .insert((input.runner.as_str(), input.destination.as_path())), + "external input destination {} is declared twice for runner {}", + input.destination.display(), + input.runner ); } ensure!( @@ -610,17 +595,6 @@ impl TargetDeclaration { validate_launch_argument(argument)?; } require_capability(&self.service.health.contract, "health contract")?; - for (label, health) in [ - ("staged health", &self.service.health.staged), - ("ready health", &self.service.health.ready), - ] { - require_value(&health.state, label)?; - require_value(&health.detail, label)?; - } - ensure!( - self.service.health.staged != self.service.health.ready, - "staged and ready health states are identical" - ); for name in &self.service.required_environment { require_environment_name(name)?; } @@ -632,59 +606,71 @@ impl TargetDeclaration { ); } - require_capability(&self.state.generation, "state generation")?; - let slot_ids = unique_ids( - self.state.slots.iter().map(|slot| slot.id.as_str()), - "state slot", - )?; - let mut slot_paths = BTreeSet::new(); - for slot in &self.state.slots { - require_relative_path(&slot.relative_path, "state slot path", false)?; - ensure!( - slot_paths.insert(slot.relative_path.as_path()), - "state slot path {} is declared twice", - slot.relative_path.display() - ); - require_capability(&slot.schema, "state slot schema")?; - if slot.writer == StateWriter::ProcessBoundSingleWriter { + if let Some(state) = &self.state { + require_capability(&state.schema_generation, "state schema generation")?; + ensure!(!state.slots.is_empty(), "state contract has no slots"); + let slot_ids = unique_ids( + state.slots.iter().map(|slot| slot.id.as_str()), + "state slot", + )?; + let mut slot_paths = BTreeSet::::new(); + for slot in &state.slots { + require_relative_path(&slot.relative_path, "state slot path", false)?; ensure!( - slot.startup == StateStartup::CreateOrOpenAfterAdmission, - "writable state slot {} must open after write admission", - slot.id + slot_paths + .iter() + .all(|other| !paths_overlap(other, &slot.relative_path)), + "state slot path {} overlaps another declared slot", + slot.relative_path.display() ); - } - if slot.recovery == StateRecovery::ExternalAuthority { ensure!( - slot.writer == StateWriter::None, - "externally authoritative state slot {} cannot name the service as writer", - slot.id + slot_paths.insert(slot.relative_path.clone()), + "state slot path {} is declared twice", + slot.relative_path.display() ); + require_capability(&slot.schema, "state slot schema")?; + match slot.writer { + StateWriter::ProcessBoundSingleWriter => ensure!( + slot.startup == StateStartup::CreateOrOpenAfterWriteLease, + "writable state slot {} must open after its write lease", + slot.id + ), + StateWriter::None => ensure!( + slot.startup == StateStartup::OpenAtStart, + "non-writable state slot {} cannot wait for a write lease", + slot.id + ), + } + if slot.recovery == StateRecovery::ExternalAuthority { + ensure!( + slot.writer == StateWriter::None, + "externally authoritative state slot {} cannot name the service as writer", + slot.id + ); + } } + if let Some(migration) = state.migration.as_ref() { + ensure!( + artifact_ids.contains(migration.executable_artifact.as_str()), + "migration executable artifact is not declared" + ); + ensure!( + !migration.from_generations.is_empty(), + "migration has no source generations" + ); + for generation in &migration.from_generations { + require_capability(generation, "migration source generation")?; + } + ensure!( + migration.to_generation == state.schema_generation, + "migration destination differs from state generation" + ); + for argument in &migration.arguments { + validate_launch_argument(argument)?; + } + } + ensure!(slot_ids.len() == state.slots.len(), "duplicate state slot"); } - if let Some(migration) = self.state.migration.as_ref() { - ensure!( - artifact_ids.contains(migration.executable_artifact.as_str()), - "migration executable artifact is not declared" - ); - ensure!( - !migration.from_generations.is_empty(), - "migration has no source generations" - ); - for generation in &migration.from_generations { - require_capability(generation, "migration source generation")?; - } - ensure!( - migration.to_generation == self.state.generation, - "migration destination differs from state generation" - ); - for argument in &migration.arguments { - validate_launch_argument(argument)?; - } - } - ensure!( - slot_ids.len() == self.state.slots.len(), - "duplicate state slot" - ); let mut provided = BTreeSet::new(); for capability in &self.provides { @@ -714,6 +700,13 @@ impl TargetDeclaration { dependency.minimum_capacity > 0, "dependency capacity must be positive" ); + if dependency.kind == DependencyKind::Bootstrap { + ensure!( + dependency.startup == StartupOrder::BeforeStart, + "bootstrap dependency {} must be available before start", + dependency.capability + ); + } ensure!( required.insert((dependency.kind, dependency.capability.as_str())), "dependency {} is declared twice for the same kind", @@ -726,6 +719,15 @@ impl TargetDeclaration { } Ok(()) } + + pub fn write_lease_required(&self) -> bool { + self.state.as_ref().is_some_and(|state| { + state + .slots + .iter() + .any(|slot| slot.writer == StateWriter::ProcessBoundSingleWriter) + }) + } } impl OperatorBinding { @@ -819,32 +821,8 @@ impl OperatorBinding { !runner.allowed_programs.is_empty(), "runner {id} has no allowed programs" ); - for host in &runner.allowed_https_hosts { - require_host(host)?; - } - match runner.network { - RunnerNetwork::None => ensure!( - runner.allowed_https_hosts.is_empty(), - "network-none runner {id} has HTTPS hosts" - ), - RunnerNetwork::DependencyEgress => { - ensure!( - runner - .affordances - .contains(&RunnerAffordance::DependencyNetwork), - "dependency-egress runner {id} does not grant dependency-network" - ); - ensure!( - !runner.allowed_https_hosts.is_empty(), - "dependency-egress runner {id} has no allowed hosts" - ); - } - RunnerNetwork::HostPrivate => ensure!( - runner - .affordances - .contains(&RunnerAffordance::PrivateCapabilityAccess), - "host-private runner {id} does not grant private-capability-access" - ), + if let Some(profile) = &runner.network_profile { + require_capability(profile, "runner network profile")?; } for name in runner.environment.keys().chain(runner.secret_files.keys()) { require_environment_name(name)?; @@ -858,25 +836,44 @@ impl OperatorBinding { "build-cache runner {id} has no cache root" ); } + ensure!( + runner.affordances.contains(&RunnerAffordance::BuildCache) + == runner.cache_root.is_some(), + "runner {id} cache root and build-cache affordance disagree" + ); + ensure!( + runner.affordances.contains(&RunnerAffordance::SecretRead) + == !runner.secret_files.is_empty(), + "runner {id} secret files and secret-read affordance disagree" + ); } require_identity(&self.workload.user, "workload user")?; require_identity(&self.workload.group, "workload group")?; require_id(&self.workload.unit_prefix, "workload unit prefix")?; + require_id(&self.runtime_identity.runtime_id, "runtime id")?; + require_id( + &self.runtime_identity.expected_signer_identity_id, + "expected signer identity id", + )?; + require_absolute_path( + &self.runtime_identity.trust_anchor_store, + "trust anchor store", + )?; for (label, path) in [ ("release root", &self.workload.release_root), - ("state root", &self.workload.state_root), ("runtime root", &self.workload.runtime_root), - ("admission record", &self.admission.record_path), - ("admission lock", &self.admission.lock_path), ("deployment brake", &self.brakes.deployment_store), ("lifecycle brake", &self.brakes.lifecycle_store), ] { require_absolute_path(path, label)?; } - ensure!( - self.admission.record_path != self.admission.lock_path, - "admission record and lock paths are identical" - ); + if let Some(state_root) = &self.workload.state_root { + require_absolute_path(state_root, "state root")?; + } + if let Some(write_lease) = &self.process_write_lease { + require_absolute_path(&write_lease.record_path, "process write-lease record")?; + require_absolute_path(&write_lease.lock_path(), "process write-lease lock")?; + } ensure!( self.brakes.deployment_store != self.brakes.lifecycle_store, "deployment and lifecycle brakes are identical" @@ -898,6 +895,15 @@ impl OperatorBinding { { require_absolute_path(path, "workload path")?; } + for path in &self.workload.read_write_paths { + if let Some(state_root) = &self.workload.state_root { + ensure!( + !paths_overlap(path, state_root), + "workload read-write path {} overlaps the write-lease-controlled state root", + path.display() + ); + } + } for capability in &self.workload.capabilities { require_value(capability, "workload capability")?; } @@ -933,42 +939,62 @@ impl OperatorBinding { require_absolute_path(&route.config_path, "route config")?; require_unit(&route.reload_unit, "route reload unit")?; } - match self.state_transition.policy { - StateTransitionPolicy::Preserve => {} - StateTransitionPolicy::FencedMigration => ensure!( - self.state_transition.backup_root.is_some(), - "fenced migration has no backup root" - ), - StateTransitionPolicy::FreshRoot => ensure!( - self.state_transition.archive_root.is_some(), - "fresh-root transition has no whole-root archive" - ), - } - if let Some(path) = self.state_transition.archive_root.as_ref() { - require_absolute_path(path, "state archive root")?; - } - if let Some(path) = self.state_transition.backup_root.as_ref() { - require_absolute_path(path, "state backup root")?; + if let Some(transition) = &self.state_transition { + match transition.policy { + StateTransitionPolicy::Preserve => {} + StateTransitionPolicy::FencedMigration => ensure!( + transition.backup_root.is_some(), + "fenced migration has no backup root" + ), + StateTransitionPolicy::FreshRoot => ensure!( + transition.archive_root.is_some(), + "fresh-root transition has no whole-root archive" + ), + } + if let Some(path) = transition.archive_root.as_ref() { + require_absolute_path(path, "state archive root")?; + } + if let Some(path) = transition.backup_root.as_ref() { + require_absolute_path(path, "state backup root")?; + } } ensure!( self.rollout.retain_releases >= 2, "candidate rollout must retain at least current and prior releases" ); ensure!( - self.placement.desired_replicas > 0, - "desired replicas must be positive" + self.placement.desired_replicas == 1, + "operator binding v1 admits exactly one replica" + ); + ensure!( + self.placement.nodes.len() == 1, + "operator binding v1 admits exactly one node" ); - ensure!(!self.placement.nodes.is_empty(), "placement has no nodes"); for node in &self.placement.nodes { require_id(node, "placement node")?; } + let mut external_contracts = BTreeSet::new(); for capability in &self.external_capabilities { + require_id(&capability.provider_id, "external provider id")?; require_capability_contract( &capability.capability, &capability.schema, &capability.compatibility, )?; + ensure!( + capability.capacity > 0, + "external capability capacity must be positive" + ); require_value(&capability.endpoint, "external capability endpoint")?; + ensure!( + external_contracts.insert(( + capability.provider_id.as_str(), + capability.capability.as_str(), + capability.schema.as_str(), + capability.compatibility.as_str(), + )), + "external provider capability is declared twice" + ); } for profile in &self.profiles { require_capability(profile, "profile")?; @@ -992,9 +1018,9 @@ impl OperatorBinding { .map(|step| step.runner.as_str()) .chain( declaration - .external_artifacts + .external_inputs .iter() - .map(|artifact| artifact.runner.as_str()), + .map(|input| input.runner.as_str()), ) .chain( declaration @@ -1008,6 +1034,18 @@ impl OperatorBinding { declared_runners == bound_runners, "operator runner classes do not exactly match the target recipe" ); + ensure!( + self.process_write_lease.is_some() == declaration.write_lease_required(), + "process write-lease binding must exist exactly when writable state requires it" + ); + ensure!( + self.workload.state_root.is_some() == declaration.state.is_some(), + "state root binding must exist exactly when the recipe declares state" + ); + ensure!( + self.state_transition.is_some() == declaration.state.is_some(), + "state-transition binding must exist exactly when the recipe declares state" + ); for step in &declaration.steps { let binding = &self.runners[&step.runner]; ensure!( @@ -1051,32 +1089,24 @@ impl OperatorBinding { artifact.id ); } - for artifact in &declaration.external_artifacts { - let binding = &self.runners[&artifact.runner]; + for input in &declaration.external_inputs { + let binding = &self.runners[&input.runner]; ensure!( binding.affordances.contains(&RunnerAffordance::SourceRead), - "external artifact {} runner cannot read the frozen source", - artifact.id + "external input {} runner cannot read the frozen source", + input.id ); ensure!( binding .affordances .contains(&RunnerAffordance::ArtifactWrite), - "external artifact {} runner cannot publish artifacts", - artifact.id + "external input {} runner cannot materialize inputs", + input.id ); ensure!( - binding - .affordances - .contains(&RunnerAffordance::DependencyNetwork), - "external artifact {} runner has no dependency-network affordance", - artifact.id - ); - ensure!( - binding.network == RunnerNetwork::DependencyEgress - && !binding.allowed_https_hosts.is_empty(), - "external artifact {} runner has no admitted egress hosts", - artifact.id + binding.network_profile.is_some(), + "external input {} runner has no operator-bound network profile", + input.id ); } let available_environment: BTreeSet<_> = self @@ -1121,20 +1151,41 @@ impl OperatorBinding { match ( declaration.service.route_required, declaration.service.transport, - self.route.as_ref().map(|route| route.driver), + self.route.as_ref(), ) { - (true, ServiceTransport::Http, Some(RouteDriver::NginxHttp)) - | (true, ServiceTransport::Tcp, Some(RouteDriver::NginxStreamTcp)) - | (false, _, None) => {} + (true, ServiceTransport::Http, Some(route)) + if route.driver == RouteDriver::NginxHttp => + { + ensure!( + route.stable_endpoint.starts_with("http://") + || route.stable_endpoint.starts_with("https://"), + "HTTP stable endpoint is not an HTTP URI" + ); + } + (true, ServiceTransport::Tcp, Some(route)) + if route.driver == RouteDriver::NginxStreamTcp => + { + ensure!( + route.stable_endpoint.starts_with("tcp://"), + "TCP stable endpoint is not a TCP URI" + ); + } + (false, _, None) => {} _ => bail!("route binding does not match the launch contract"), } - match self.state_transition.policy { - StateTransitionPolicy::Preserve => {} - StateTransitionPolicy::FencedMigration => ensure!( - declaration.state.migration.is_some(), - "binding requests migration but target declares no migrator" - ), - StateTransitionPolicy::FreshRoot => {} + if let Some(transition) = &self.state_transition { + match transition.policy { + StateTransitionPolicy::Preserve => {} + StateTransitionPolicy::FencedMigration => ensure!( + declaration + .state + .as_ref() + .and_then(|state| state.migration.as_ref()) + .is_some(), + "binding requests migration but target declares no migrator" + ), + StateTransitionPolicy::FreshRoot => {} + } } for dependency in declaration .dependencies @@ -1150,7 +1201,7 @@ impl OperatorBinding { &binding.capability, &binding.schema, &binding.compatibility, - ) + ) && binding.capacity >= dependency.minimum_capacity }), "external capability {} has no compatible operator binding", dependency.capability @@ -1158,33 +1209,6 @@ impl OperatorBinding { } Ok(()) } - - pub fn admit_external_artifact_manifest( - &self, - declaration: &TargetDeclaration, - artifact_id: &str, - manifest_input: &str, - ) -> Result { - let artifact = declaration - .external_artifacts - .iter() - .find(|artifact| artifact.id == artifact_id) - .with_context(|| format!("target declares no external artifact {artifact_id}"))?; - let runner = self.runners.get(&artifact.runner).with_context(|| { - format!("external artifact runner {} is not bound", artifact.runner) - })?; - ensure!( - runner.network == RunnerNetwork::DependencyEgress, - "external artifact runner has no dependency egress" - ); - let manifest = PinnedTarMemberManifest::parse(manifest_input)?; - let host = https_host(&manifest.url)?; - ensure!( - runner.allowed_https_hosts.contains(host), - "external artifact host {host} is not admitted" - ); - Ok(manifest) - } } pub fn capability_compatible( @@ -1328,6 +1352,10 @@ fn require_absolute_path(path: &Path, label: &str) -> Result<()> { Ok(()) } +fn paths_overlap(left: &Path, right: &Path) -> bool { + left.starts_with(right) || right.starts_with(left) +} + fn require_sha1(value: &str, label: &str) -> Result<()> { require_lower_hex(value, 40, label) } @@ -1402,12 +1430,12 @@ phase = "build" runner = "rust" argv = ["cargo", "build", "--locked", "--release", "-p", "test-service"] -[[external_artifacts]] +[[external_inputs]] id = "tool" +url = "https://registry.npmjs.org/tool.tgz" +sha256 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" runner = "rust" -manifest = "deployment/tool.toml" -destination = "tool" -executable = true +destination = "inputs/tool.tgz" [[artifacts]] id = "daemon" @@ -1415,6 +1443,7 @@ source_kind = "runner-output" runner = "rust" source = "target/release/test-service" destination = "test-service" +expected_sha256 = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" executable = true [service] @@ -1431,16 +1460,8 @@ required_environment = ["TEST_SERVICE_BIND"] [service.health] contract = "test-service.cultnet-service-health" -[service.health.staged] -state = "warming" -detail = "traffic-admission-pending" - -[service.health.ready] -state = "active" -detail = "serving" - [state] -generation = "v1" +schema_generation = "v1" [[state.slots]] id = "world" @@ -1449,7 +1470,7 @@ kind = "cultcache-file" schema = "test-service.state.v1" writer = "process-bound-single-writer" recovery = "preserve" -startup = "create-or-open-after-admission" +startup = "create-or-open-after-write-lease" [[provides]] capability = "test-service.runtime" @@ -1480,11 +1501,10 @@ recipe_path = "deployment/idunn/recipe.toml" [runners.rust] driver = "docker" image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" -affordances = ["source-read", "artifact-write", "dependency-network", "build-cache"] +affordances = ["source-read", "artifact-write", "build-cache"] cache_root = "/srv/ghostlight/build-cache" -allowed_https_hosts = ["crates.io", "github.com"] allowed_programs = ["cargo"] -network = "dependency-egress" +network_profile = "build-dependency-egress" memory_mebibytes = 8192 cpu_quota_percent = 400 @@ -1507,6 +1527,11 @@ TEST_SERVICE_BIND = "idunn.private_endpoint" [workload.argument_bindings] state_root = "/var/lib/gamecult/test-service" +[runtime_identity] +runtime_id = "test-service-yggdrasil" +expected_signer_identity_id = "test-service-runtime-signer" +trust_anchor_store = "/etc/gamecult/trust/test-service.cc" + [route] driver = "nginx-http" route_id = "test-service-public" @@ -1517,10 +1542,8 @@ private_port_end = 18839 config_path = "/etc/nginx/idunn-routes/test-service.conf" reload_unit = "nginx.service" -[admission] -driver = "atomic-file" -record_path = "/etc/gamecult/test-service/runtime/process-admission.cc" -lock_path = "/etc/gamecult/test-service/runtime/process-admission.cc.lock" +[process_write_lease] +record_path = "/etc/gamecult/test-service/runtime/process-write-lease.cc" [brakes] deployment_store = "/var/lib/gamecult/idunn-authority/test-service-deployment-brake.cc" @@ -1562,19 +1585,41 @@ nodes = ["yggdrasil"] fn operator_cannot_omit_an_affordance_derived_from_the_recipe() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); let input = BINDING.replace( - "affordances = [\"source-read\", \"artifact-write\", \"dependency-network\", \"build-cache\"]", - "affordances = [\"source-read\", \"dependency-network\", \"build-cache\"]", + "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", + "affordances = [\"source-read\", \"build-cache\"]", ); let binding = OperatorBinding::parse(&input).unwrap(); assert!(binding.admit(&recipe).is_err()); } + #[test] + fn runner_mounts_require_their_exact_operator_affordance() { + let cache_without_affordance = BINDING.replace( + "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", + "affordances = [\"source-read\", \"artifact-write\"]", + ); + assert!(OperatorBinding::parse(&cache_without_affordance).is_err()); + + let affordance_without_secret = BINDING.replace( + "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", + "affordances = [\"source-read\", \"artifact-write\", \"build-cache\", \"secret-read\"]", + ); + assert!(OperatorBinding::parse(&affordance_without_secret).is_err()); + } + #[test] fn route_transport_mismatch_is_rejected() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); let binding = OperatorBinding::parse(&BINDING.replace("nginx-http", "nginx-stream-tcp")).unwrap(); assert!(binding.admit(&recipe).is_err()); + + let binding = OperatorBinding::parse(&BINDING.replace( + "https://yggdrasil.gamecult.org/test-service/", + "tcp://yggdrasil.gamecult.org:443", + )) + .unwrap(); + assert!(binding.admit(&recipe).is_err()); } #[test] @@ -1598,13 +1643,16 @@ nodes = ["yggdrasil"] } #[test] - fn fresh_root_keeps_target_preservation_semantics_inside_the_new_generation() { + fn fresh_root_keeps_slot_preservation_semantics_inside_the_new_state_root() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); let binding = OperatorBinding::parse(BINDING).unwrap(); binding.admit(&recipe).unwrap(); - assert_eq!(recipe.state.slots[0].recovery, StateRecovery::Preserve); assert_eq!( - binding.state_transition.policy, + recipe.state.as_ref().unwrap().slots[0].recovery, + StateRecovery::Preserve + ); + assert_eq!( + binding.state_transition.as_ref().unwrap().policy, StateTransitionPolicy::FreshRoot ); } @@ -1619,29 +1667,123 @@ nodes = ["yggdrasil"] } #[test] - fn pinned_artifact_host_must_be_operator_admitted() { + fn state_slots_cannot_claim_overlapping_paths() { + let input = RECIPE.replace( + "startup = \"create-or-open-after-write-lease\"", + "startup = \"create-or-open-after-write-lease\"\n\n[[state.slots]]\nid = \"nested\"\nrelative_path = \"world.cc/index.cc\"\nkind = \"cultcache-file\"\nschema = \"test-service.index.v1\"\nwriter = \"process-bound-single-writer\"\nrecovery = \"preserve\"\nstartup = \"create-or-open-after-write-lease\"", + ); + assert!(TargetDeclaration::parse(&input).is_err()); + } + + #[test] + fn workload_mount_cannot_bypass_write_lease_controlled_state() { + let input = BINDING.replace( + "cpu_quota_percent = 200\n\n[workload.environment]", + "cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.environment]", + ); + assert!(OperatorBinding::parse(&input).is_err()); + } + + #[test] + fn v1_binding_is_explicitly_singleton() { + assert!( + OperatorBinding::parse( + &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") + ) + .is_err() + ); + assert!( + OperatorBinding::parse(&BINDING.replace( + "nodes = [\"yggdrasil\"]", + "nodes = [\"yggdrasil\", \"raven\"]" + )) + .is_err() + ); + } + + #[test] + fn external_input_uses_an_operator_bound_network_profile() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); let binding = OperatorBinding::parse(BINDING).unwrap(); - let manifest = r#" -schema = "gamecult.idunn.pinned_tar_member.v1" -url = "https://registry.npmjs.org/tool.tgz" -archive_sha256 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -member = "package/bin/tool" -member_sha256 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" -"#; - assert!( - binding - .admit_external_artifact_manifest(&recipe, "tool", manifest) - .is_err() + binding.admit(&recipe).unwrap(); + let isolated = BINDING.replace("network_profile = \"build-dependency-egress\"\n", ""); + let binding = OperatorBinding::parse(&isolated).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + + #[test] + fn process_write_lease_exists_exactly_for_process_writable_state() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let without_lease = BINDING.replace( + "[process_write_lease]\nrecord_path = \"/etc/gamecult/test-service/runtime/process-write-lease.cc\"\n\n", + "", ); - let admitted = BINDING.replace( - "allowed_https_hosts = [\"crates.io\", \"github.com\"]", - "allowed_https_hosts = [\"crates.io\", \"github.com\", \"registry.npmjs.org\"]", + let binding = OperatorBinding::parse(&without_lease).unwrap(); + assert!(binding.admit(&recipe).is_err()); + + let read_only_recipe = TargetDeclaration::parse( + &RECIPE + .replace( + "writer = \"process-bound-single-writer\"", + "writer = \"none\"", + ) + .replace( + "startup = \"create-or-open-after-write-lease\"", + "startup = \"open-at-start\"", + ), + ) + .unwrap(); + binding.admit(&read_only_recipe).unwrap(); + + let binding = OperatorBinding::parse(BINDING).unwrap(); + assert_eq!( + binding.process_write_lease.as_ref().unwrap().lock_path(), + PathBuf::from("/etc/gamecult/test-service/runtime/process-write-lease.cc.lock") ); - let binding = OperatorBinding::parse(&admitted).unwrap(); - binding - .admit_external_artifact_manifest(&recipe, "tool", manifest) - .unwrap(); + assert!(binding.admit(&read_only_recipe).is_err()); + } + + #[test] + fn stateless_target_has_no_state_binding_or_generation() { + let mut recipe_text = RECIPE.replace( + "arguments = [\n { kind = \"literal\", value = \"--state-root\" },\n { kind = \"binding\", name = \"state_root\" },\n]", + "arguments = []", + ); + let state_start = recipe_text.find("\n[state]\n").unwrap(); + let state_end = recipe_text.find("\n[[provides]]\n").unwrap(); + recipe_text.replace_range(state_start..state_end, ""); + let recipe = TargetDeclaration::parse(&recipe_text).unwrap(); + assert!(recipe.state.is_none()); + + let binding_text = BINDING + .replace("state_root = \"/var/lib/gamecult/test-service\"\n", "") + .replace( + "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"\n\n", + "", + ) + .replace( + "[process_write_lease]\nrecord_path = \"/etc/gamecult/test-service/runtime/process-write-lease.cc\"\n\n", + "", + ) + .replace( + "[state_transition]\npolicy = \"fresh-root\"\narchive_root = \"/var/lib/gamecult/test-service-cold-archive\"\n\n", + "", + ); + let binding = OperatorBinding::parse(&binding_text).unwrap(); + binding.admit(&recipe).unwrap(); + assert!(binding.workload.state_root.is_none()); + assert!(binding.state_transition.is_none()); + } + + #[test] + fn bootstrap_dependency_must_be_available_before_start() { + let invalid = RECIPE.replace("kind = \"shared-infrastructure\"", "kind = \"bootstrap\""); + assert!(TargetDeclaration::parse(&invalid).is_err()); + let valid = invalid.replace( + "startup = \"before-promotion\"", + "startup = \"before-start\"", + ); + TargetDeclaration::parse(&valid).unwrap(); } #[test] diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index 583059f..a6415ab 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -7,146 +7,178 @@ use sha2::{Digest, Sha256}; use crate::deployment::{ CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, - ProvidedCapability, TargetDeclaration, capability_compatible, + ProvidedCapability, ServiceTransport, SourceSelectionPolicy, StateDeclaration, + TargetDeclaration, capability_compatible, }; +pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v1"; -pub const SEALED_DEPLOYMENT_SCHEMA: &str = "idunn.sealed_deployment.v1"; -pub const EXPECTED_GENERATION_SCHEMA: &str = "idunn.expected_generation.v1"; -pub const GENERATION_CORRELATION_SCHEMA: &str = "idunn.generation_correlation.v1"; -pub const PROMOTION_TRANSACTION_SCHEMA: &str = "idunn.promotion_transaction.v1"; -pub const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v1"; +pub const EXPECTED_INCARNATION_SCHEMA: &str = "idunn.expected_incarnation.v1"; +pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ResolvedGitlink { +pub struct GitlinkTreeFact { pub origin: String, pub revision: String, + pub tree_entry_revision: String, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "policy", rename_all = "kebab-case", deny_unknown_fields)] +pub enum SourceSelection { + PinnedObject, + RefHead, + SignedRelease { release_authority_id: String }, +} + +/// Private facts emitted by the future narrow source driver. Structural +/// validation here does not prove Git ancestry, signatures, or object custody. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ExactSource { +pub struct SourceSelectionFacts { + pub schema: String, pub origin: String, pub admitted_ref: String, pub revision: String, - pub minimum_revision: String, - pub selection_receipt_sha256: String, - pub gitlinks: BTreeMap, + pub source_tree: String, + pub recipe_path: PathBuf, + pub recipe_blob_sha256: String, + pub gitlinks: BTreeMap, + pub selection: SourceSelection, + pub selected_at_unix_millis: u64, } -impl ExactSource { +impl SourceSelectionFacts { pub fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { + binding.validate()?; + ensure!( + self.schema == SOURCE_SELECTION_FACTS_SCHEMA, + "unsupported source-selection facts schema" + ); ensure!( self.origin == binding.repository.origin, - "exact source origin differs from binding" + "selected origin differs from binding" ); ensure!( self.admitted_ref == binding.repository.admitted_ref, - "exact source ref differs from binding" + "selected ref differs from binding" ); - require_sha1(&self.revision, "exact source revision")?; + require_sha1(&self.revision, "selected revision")?; + require_sha1(&self.source_tree, "selected source tree")?; ensure!( - self.minimum_revision == binding.repository.minimum_revision, - "exact source floor differs from binding" + self.recipe_path == binding.repository.recipe_path, + "selected recipe path differs from binding" ); - require_sha1(&self.minimum_revision, "exact source floor")?; - require_sha256(&self.selection_receipt_sha256, "source-selection receipt")?; + require_sha256(&self.recipe_blob_sha256, "selected recipe blob")?; + ensure!( + self.selected_at_unix_millis > 0, + "source-selection facts have no time" + ); + + match (&binding.repository.selection, &self.selection) { + (SourceSelectionPolicy::PinnedObject, SourceSelection::PinnedObject) => { + let pinned_revision = binding + .repository + .pinned_revision + .as_deref() + .context("validated pinned binding has no revision")?; + ensure!( + self.revision == pinned_revision, + "selected source differs from the pinned revision" + ); + } + (SourceSelectionPolicy::RefHead, SourceSelection::RefHead) => {} + ( + SourceSelectionPolicy::SignedRelease, + SourceSelection::SignedRelease { + release_authority_id, + }, + ) => { + require_token(release_authority_id, "release authority id")?; + } + _ => bail!("source-selection facts do not match operator policy"), + } + let expected_paths: BTreeSet<_> = binding.repository.gitlinks.keys().cloned().collect(); let observed_paths: BTreeSet<_> = self.gitlinks.keys().cloned().collect(); ensure!( expected_paths == observed_paths, - "resolved Gitlinks differ from binding" + "Gitlink tree facts differ from binding" ); - for (path, observed) in &self.gitlinks { + for (path, receipt) in &self.gitlinks { let expected = &binding.repository.gitlinks[path]; ensure!( - observed.origin == expected.origin, + receipt.origin == expected.origin, "Gitlink {} origin differs from binding", path.display() ); - require_sha1(&observed.revision, "Gitlink revision")?; + require_sha1(&receipt.revision, "Gitlink revision")?; + require_sha1(&receipt.tree_entry_revision, "Gitlink tree entry")?; + ensure!( + receipt.revision == receipt.tree_entry_revision, + "Gitlink {} revision differs from the selected tree entry", + path.display() + ); } Ok(()) } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum ProviderOrigin { - ManagedRuntime, - ExternalOperatorBinding, +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] +pub enum ExpectedProviderAuthority { + ManagedIncarnation { + target: String, + incarnation_id: String, + plan_id: String, + sealed_release_id: String, + expected_projection_sha256: String, + }, + ExternalOperatorBinding { + binding_target: String, + }, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ProviderObservation { +pub struct ExpectedProviderRef { pub provider_id: String, - pub provider_target: Option, - pub origin: ProviderOrigin, - pub generation: String, - pub deployment_id: Option, + pub authority: ExpectedProviderAuthority, pub capability: String, pub schema: String, pub compatibility: String, pub capacity: u32, - pub endpoint: String, - pub expected: bool, - pub present: bool, - pub ready: bool, - pub observation_sha256: Option, + pub endpoint: Option, } -impl ProviderObservation { +impl ExpectedProviderRef { pub fn validate(&self) -> Result<()> { - require_token(&self.provider_id, "provider id")?; - if let Some(target) = &self.provider_target { - require_token(target, "provider target")?; - } - require_token(&self.generation, "provider generation")?; - if let Some(deployment_id) = &self.deployment_id { - require_token(deployment_id, "provider deployment id")?; - } + require_token(&self.provider_id, "expected provider id")?; require_contract(&self.capability, &self.schema, &self.compatibility)?; - ensure!(self.capacity > 0, "provider capacity is zero"); - require_value(&self.endpoint, "provider endpoint")?; - ensure!( - !self.present || self.expected, - "provider is present without expected admission" - ); - ensure!( - !self.ready || self.present, - "provider is ready without signed presence" - ); - match self.origin { - ProviderOrigin::ManagedRuntime => { - ensure!( - self.provider_target.is_some(), - "managed provider has no target" - ); - if self.present { - ensure!( - self.deployment_id.is_some(), - "present managed provider has no deployment identity" - ); - require_sha256( - self.observation_sha256.as_deref().unwrap_or_default(), - "managed provider observation", - )?; - } + ensure!(self.capacity > 0, "expected provider capacity is zero"); + if let Some(endpoint) = &self.endpoint { + require_value(endpoint, "expected provider endpoint")?; + } + match &self.authority { + ExpectedProviderAuthority::ManagedIncarnation { + target, + incarnation_id, + plan_id, + sealed_release_id, + expected_projection_sha256, + } => { + require_token(target, "managed provider target")?; + require_token(incarnation_id, "managed provider incarnation")?; + require_sha256(plan_id, "managed provider plan")?; + require_sha256(sealed_release_id, "managed provider sealed release")?; + require_sha256(expected_projection_sha256, "managed expected projection")?; } - ProviderOrigin::ExternalOperatorBinding => { + ExpectedProviderAuthority::ExternalOperatorBinding { binding_target } => { + require_token(binding_target, "external binding target")?; ensure!( - self.provider_target.is_none(), - "external binding impersonates a managed target" - ); - ensure!( - !self.present && !self.ready, - "external configuration impersonates runtime observation" - ); - ensure!( - self.observation_sha256.is_none(), - "external configuration carries a runtime observation digest" + self.endpoint.is_some(), + "external binding has no configured endpoint" ); } } @@ -163,69 +195,140 @@ impl ProviderObservation { &self.compatibility, ) && self.capacity >= dependency.minimum_capacity } + + fn is_external_binding(&self) -> bool { + matches!( + &self.authority, + ExpectedProviderAuthority::ExternalOperatorBinding { .. } + ) + } +} + +pub fn external_binding_provider_refs( + binding: &OperatorBinding, +) -> Result> { + binding.validate()?; + let providers = binding + .external_capabilities + .iter() + .map(|capability| expected_external_provider(&binding.target, capability)) + .collect::>(); + for provider in &providers { + provider.validate()?; + } + Ok(providers) +} + +fn expected_external_provider( + binding_target: &str, + capability: &ExternalCapabilityBinding, +) -> ExpectedProviderRef { + ExpectedProviderRef { + provider_id: capability.provider_id.clone(), + authority: ExpectedProviderAuthority::ExternalOperatorBinding { + binding_target: binding_target.to_owned(), + }, + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.capacity, + endpoint: Some(capability.endpoint.clone()), + } +} + +fn managed_expected_provider_refs( + expected_incarnations: &[ExpectedIncarnation], +) -> Result> { + let mut providers = Vec::new(); + for expected in expected_incarnations { + expected.validate()?; + let expected_projection_sha256 = expected.canonical_sha256()?; + let endpoint = expected + .route + .as_ref() + .map(|route| route.candidate_endpoint.clone()); + for capability in &expected.capabilities { + providers.push(ExpectedProviderRef { + provider_id: expected.runtime_id.clone(), + authority: ExpectedProviderAuthority::ManagedIncarnation { + target: expected.target.clone(), + incarnation_id: expected.incarnation_id.clone(), + plan_id: expected.plan_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + expected_projection_sha256: expected_projection_sha256.clone(), + }, + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.capacity, + endpoint: endpoint.clone(), + }); + } + } + Ok(providers) } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct DependencyResolution { +pub struct DependencySelection { pub requirement: CapabilityDependency, - pub provider: Option, + pub provider: Option, } -impl DependencyResolution { - pub fn blocks_start(&self) -> bool { - self.requirement.startup == crate::deployment::StartupOrder::BeforeStart - && self.requirement.kind != DependencyKind::Optional - && !self - .provider - .as_ref() - .is_some_and(|provider| provider.ready) - } - - pub fn blocks_promotion(&self) -> bool { - self.requirement.kind != DependencyKind::Optional - && !self - .provider - .as_ref() - .is_some_and(|provider| provider.ready) +impl DependencySelection { + fn validate(&self) -> Result<()> { + require_contract( + &self.requirement.capability, + &self.requirement.schema, + &self.requirement.compatibility, + )?; + ensure!( + self.requirement.minimum_capacity > 0, + "dependency capacity is zero" + ); + if let Some(provider) = &self.provider { + provider.validate()?; + ensure!( + provider.compatible_with(&self.requirement), + "selected provider is incompatible" + ); + ensure!( + (self.requirement.kind == DependencyKind::ExternalOperatorBinding) + == provider.is_external_binding(), + "selected provider authority does not match dependency kind" + ); + } else { + ensure!( + self.requirement.kind == DependencyKind::Optional, + "required dependency is unresolved" + ); + } + Ok(()) } } -pub fn external_binding_observations( - bindings: &[ExternalCapabilityBinding], -) -> Vec { - bindings - .iter() - .map(|binding| ProviderObservation { - provider_id: format!("operator:{}", binding.capability), - provider_target: None, - origin: ProviderOrigin::ExternalOperatorBinding, - generation: "operator-bound".into(), - deployment_id: None, - capability: binding.capability.clone(), - schema: binding.schema.clone(), - compatibility: binding.compatibility.clone(), - capacity: 1, - endpoint: binding.endpoint.clone(), - expected: true, - present: false, - ready: false, - observation_sha256: None, - }) - .collect() -} - -pub fn resolve_dependencies( +pub fn select_dependencies( declaration: &TargetDeclaration, - observations: &[ProviderObservation], -) -> Result> { - for observation in observations { - observation.validate()?; + providers: &[ExpectedProviderRef], +) -> Result> { + declaration.validate()?; + let mut unique_contracts = BTreeSet::new(); + for provider in providers { + provider.validate()?; + ensure!( + unique_contracts.insert(( + provider.provider_id.as_str(), + provider.capability.as_str(), + provider.schema.as_str(), + provider.compatibility.as_str(), + )), + "expected provider contract is duplicated" + ); } for conflict in &declaration.conflicts { - if observations + if providers .iter() - .any(|provider| provider.expected && provider.capability == conflict.capability) + .any(|provider| provider.capability == conflict.capability) { bail!( "capability conflict {}: {}", @@ -238,23 +341,19 @@ pub fn resolve_dependencies( .dependencies .iter() .map(|dependency| { - let mut candidates = observations + let mut candidates = providers .iter() - .filter(|provider| provider.expected && provider.compatible_with(dependency)) + .filter(|provider| provider.compatible_with(dependency)) + .filter(|provider| { + (dependency.kind == DependencyKind::ExternalOperatorBinding) + == provider.is_external_binding() + }) .cloned() .collect::>(); - if dependency.kind == DependencyKind::ExternalOperatorBinding { - candidates - .retain(|provider| provider.origin == ProviderOrigin::ExternalOperatorBinding); - } else { - candidates.retain(|provider| provider.origin == ProviderOrigin::ManagedRuntime); - } candidates.sort_by(|left, right| { - right - .ready - .cmp(&left.ready) - .then_with(|| right.present.cmp(&left.present)) - .then_with(|| left.provider_id.cmp(&right.provider_id)) + left.provider_id + .cmp(&right.provider_id) + .then_with(|| left.endpoint.cmp(&right.endpoint)) }); let provider = candidates.into_iter().next(); if provider.is_none() && dependency.kind != DependencyKind::Optional { @@ -265,38 +364,108 @@ pub fn resolve_dependencies( dependency.compatibility ); } - Ok(DependencyResolution { + let selection = DependencySelection { requirement: dependency.clone(), provider, - }) + }; + selection.validate()?; + Ok(selection) }) .collect() } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ExpectedGeneration { - pub schema: String, - pub target: String, - pub generation: String, - pub deployment_id: String, - pub source_revision: String, - pub candidate_endpoint: Option, - pub capabilities: Vec, +pub struct ExpectedRoute { + pub route_id: String, + pub transport: ServiceTransport, + pub stable_endpoint: String, + pub candidate_endpoint: String, } -impl ExpectedGeneration { +impl ExpectedRoute { + fn validate(&self) -> Result<()> { + require_token(&self.route_id, "expected route id")?; + require_value(&self.stable_endpoint, "expected stable endpoint")?; + require_value(&self.candidate_endpoint, "expected candidate endpoint")?; + match self.transport { + ServiceTransport::Http => ensure!( + self.stable_endpoint.starts_with("http://") + || self.stable_endpoint.starts_with("https://"), + "expected HTTP stable endpoint is not an HTTP URI" + ), + ServiceTransport::Tcp => ensure!( + self.stable_endpoint.starts_with("tcp://"), + "expected TCP stable endpoint is not a TCP URI" + ), + _ => bail!("routed expected incarnation has a non-routable transport"), + } + ensure!( + self.candidate_endpoint + .starts_with(&format!("{}://", endpoint_scheme(self.transport)?)), + "expected candidate endpoint transport disagrees" + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ExpectedIncarnation { + pub schema: String, + pub plan_id: String, + pub sealed_release_id: String, + pub target: String, + pub incarnation_id: String, + pub runtime_id: String, + pub expected_signer_identity_id: String, + pub health_contract: String, + pub executable_artifact_sha256: String, + pub state_schema_generation: Option, + pub state_contract_sha256: Option, + pub write_lease_required: bool, + pub source_revision: String, + pub node: String, + pub route: Option, + pub capabilities: Vec, + pub dependencies: Vec, +} + +impl ExpectedIncarnation { pub fn validate(&self) -> Result<()> { ensure!( - self.schema == EXPECTED_GENERATION_SCHEMA, - "unsupported expected-generation schema" + self.schema == EXPECTED_INCARNATION_SCHEMA, + "unsupported expected-incarnation schema" ); + require_sha256(&self.plan_id, "expected plan")?; + require_sha256(&self.sealed_release_id, "expected sealed release")?; require_token(&self.target, "expected target")?; - require_token(&self.generation, "expected generation")?; - require_token(&self.deployment_id, "expected deployment")?; + require_token(&self.incarnation_id, "expected incarnation")?; + require_token(&self.runtime_id, "expected runtime id")?; + require_token( + &self.expected_signer_identity_id, + "expected signer identity id", + )?; + require_token(&self.health_contract, "expected health contract")?; + require_sha256( + &self.executable_artifact_sha256, + "expected executable artifact", + )?; + match (&self.state_schema_generation, &self.state_contract_sha256) { + (Some(generation), Some(contract)) => { + require_token(generation, "expected state schema generation")?; + require_sha256(contract, "expected state contract")?; + } + (None, None) => ensure!( + !self.write_lease_required, + "stateless expected incarnation requires a write lease" + ), + _ => bail!("expected state generation and contract digest disagree"), + } require_sha1(&self.source_revision, "expected source revision")?; - if let Some(endpoint) = &self.candidate_endpoint { - require_value(endpoint, "candidate endpoint")?; + require_token(&self.node, "expected node")?; + if let Some(route) = &self.route { + route.validate()?; } let mut capabilities = BTreeSet::new(); for capability in &self.capabilities { @@ -311,31 +480,43 @@ impl ExpectedGeneration { ); ensure!( capabilities.insert(( - &capability.capability, - &capability.schema, - &capability.compatibility + capability.capability.as_str(), + capability.schema.as_str(), + capability.compatibility.as_str(), )), "expected capability is duplicated" ); } + for dependency in &self.dependencies { + dependency.validate()?; + } Ok(()) } + + pub fn canonical_sha256(&self) -> Result { + self.validate()?; + Ok(sha256_id( + &rmp_serde::to_vec(self).context("encoding managed expected projection")?, + )) + } } +/// Private Idunn control-plane state. This contains host binding details and is +/// never an Odin/CultMesh projection. `ExpectedIncarnation` is the sanitized +/// topology projection derived only after a release validates against this +/// plan. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct CompiledDeploymentPlan { pub schema: String, pub plan_id: String, - pub deployment_id: String, + pub incarnation_id: String, pub created_at_unix_millis: u64, - pub source: ExactSource, - pub recipe_sha256: String, - pub binding_sha256: String, - pub declaration: TargetDeclaration, - pub binding: OperatorBinding, - pub dependencies: Vec, - pub expected: ExpectedGeneration, + pub source: SourceSelectionFacts, + pub recipe_blob: Vec, + pub binding_blob: Vec, + pub dependencies: Vec, + pub candidate_port: Option, } impl CompiledDeploymentPlan { @@ -345,61 +526,40 @@ impl CompiledDeploymentPlan { "unsupported deployment-plan schema" ); require_sha256(&self.plan_id, "plan id")?; - require_token(&self.deployment_id, "deployment id")?; + require_token(&self.incarnation_id, "plan incarnation")?; ensure!( - self.created_at_unix_millis > 0, - "deployment plan has no creation time" + self.created_at_unix_millis >= self.source.selected_at_unix_millis, + "deployment plan predates source selection" ); - self.declaration.validate()?; - self.binding.validate()?; - self.binding.admit(&self.declaration)?; - self.source.validate_against(&self.binding)?; - require_sha256(&self.recipe_sha256, "recipe digest")?; - require_sha256(&self.binding_sha256, "binding digest")?; - self.expected.validate()?; + let (declaration, binding) = self.parsed_inputs()?; + self.source.validate_against(&binding)?; ensure!( - self.expected.target == self.declaration.target, - "expected target differs from recipe" + sha256_id(&self.recipe_blob) == self.source.recipe_blob_sha256, + "selected recipe blob differs from the exact plan recipe bytes" ); - ensure!( - self.expected.generation == self.declaration.state.generation, - "expected generation differs from recipe" - ); - ensure!( - self.expected.deployment_id == self.deployment_id, - "expected deployment differs from plan" - ); - ensure!( - self.expected.source_revision == self.source.revision, - "expected source differs from plan" - ); - ensure!( - self.expected.capabilities == self.declaration.provides, - "expected capabilities differ from recipe" - ); - let resolved_requirements: Vec<_> = self + let declared_requirements: Vec<_> = declaration.dependencies.iter().collect(); + let selected_requirements: Vec<_> = self .dependencies .iter() - .map(|resolution| &resolution.requirement) + .map(|selection| &selection.requirement) .collect(); - let declared_requirements: Vec<_> = self.declaration.dependencies.iter().collect(); ensure!( - resolved_requirements == declared_requirements, - "resolved graph differs from recipe dependencies" + declared_requirements == selected_requirements, + "selected graph differs from recipe dependencies" ); - for resolution in &self.dependencies { - if let Some(provider) = &resolution.provider { - provider.validate()?; + for selection in &self.dependencies { + selection.validate()?; + } + match (&binding.route, self.candidate_port) { + (Some(bound), Some(port)) => { ensure!( - provider.compatible_with(&resolution.requirement), - "resolved provider is incompatible" - ); - } else { - ensure!( - resolution.requirement.kind == DependencyKind::Optional, - "required dependency is unresolved" + (bound.private_port_start..=bound.private_port_end).contains(&port), + "candidate port is outside the operator range" ); } + (None, None) => {} + (Some(_), None) => bail!("routed deployment has no candidate port"), + (None, Some(_)) => bail!("private deployment selected a routed candidate port"), } ensure!( self.plan_id == self.recomputed_plan_id()?, @@ -408,6 +568,17 @@ impl CompiledDeploymentPlan { Ok(()) } + fn parsed_inputs(&self) -> Result<(TargetDeclaration, OperatorBinding)> { + let recipe_text = std::str::from_utf8(&self.recipe_blob) + .context("stored deployment recipe is not UTF-8")?; + let binding_text = std::str::from_utf8(&self.binding_blob) + .context("stored operator binding is not UTF-8")?; + let declaration = TargetDeclaration::parse(recipe_text)?; + let binding = OperatorBinding::parse(binding_text)?; + binding.admit(&declaration)?; + Ok((declaration, binding)) + } + fn recomputed_plan_id(&self) -> Result { let mut unsigned = self.clone(); unsigned.plan_id.clear(); @@ -419,64 +590,43 @@ impl CompiledDeploymentPlan { #[allow(clippy::too_many_arguments)] pub fn compile_deployment_plan( - declaration: TargetDeclaration, - binding: OperatorBinding, - source: ExactSource, recipe_bytes: &[u8], binding_bytes: &[u8], - deployment_id: impl Into, + source: SourceSelectionFacts, + incarnation_id: impl Into, candidate_port: Option, created_at_unix_millis: u64, - observed_providers: &[ProviderObservation], + managed_expected_incarnations: &[ExpectedIncarnation], ) -> Result { - declaration.validate()?; - binding.validate()?; + let recipe_text = std::str::from_utf8(recipe_bytes).context("recipe is not UTF-8")?; + let binding_text = std::str::from_utf8(binding_bytes).context("binding is not UTF-8")?; + let declaration = TargetDeclaration::parse(recipe_text)?; + let binding = OperatorBinding::parse(binding_text)?; binding.admit(&declaration)?; source.validate_against(&binding)?; - let deployment_id = deployment_id.into(); - require_token(&deployment_id, "deployment id")?; ensure!( - created_at_unix_millis > 0, - "deployment plan has no creation time" + sha256_id(recipe_bytes) == source.recipe_blob_sha256, + "selected recipe blob differs from compiled recipe bytes" ); - let candidate_endpoint = match (&binding.route, candidate_port) { - (Some(route), Some(port)) => { - ensure!( - (route.private_port_start..=route.private_port_end).contains(&port), - "candidate port is outside the operator range" - ); - Some(format!("{}:{port}", route.private_host)) - } - (None, None) => None, - (Some(_), None) => bail!("routed deployment has no candidate port"), - (None, Some(_)) => bail!("private deployment cannot select a routed candidate port"), - }; - let mut providers = observed_providers.to_vec(); - providers.extend(external_binding_observations( - &binding.external_capabilities, - )); - let dependencies = resolve_dependencies(&declaration, &providers)?; - let expected = ExpectedGeneration { - schema: EXPECTED_GENERATION_SCHEMA.into(), - target: declaration.target.clone(), - generation: declaration.state.generation.clone(), - deployment_id: deployment_id.clone(), - source_revision: source.revision.clone(), - candidate_endpoint, - capabilities: declaration.provides.clone(), - }; + let incarnation_id = incarnation_id.into(); + require_token(&incarnation_id, "deployment incarnation")?; + ensure!( + created_at_unix_millis >= source.selected_at_unix_millis, + "deployment plan predates source selection" + ); + let mut providers = managed_expected_provider_refs(managed_expected_incarnations)?; + providers.extend(external_binding_provider_refs(&binding)?); + let dependencies = select_dependencies(&declaration, &providers)?; let mut plan = CompiledDeploymentPlan { schema: COMPILED_DEPLOYMENT_PLAN_SCHEMA.into(), plan_id: String::new(), - deployment_id, + incarnation_id, created_at_unix_millis, source, - recipe_sha256: sha256_id(recipe_bytes), - binding_sha256: sha256_id(binding_bytes), - declaration, - binding, + recipe_blob: recipe_bytes.to_vec(), + binding_blob: binding_bytes.to_vec(), dependencies, - expected, + candidate_port, }; plan.plan_id = plan.recomputed_plan_id()?; plan.validate()?; @@ -485,7 +635,7 @@ pub fn compile_deployment_plan( #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ArtifactSeal { +pub struct ArtifactReceipt { pub artifact_id: String, pub destination: PathBuf, pub sha256: String, @@ -493,459 +643,277 @@ pub struct ArtifactSeal { pub executable: bool, } +impl ArtifactReceipt { + fn validate(&self, declaration: &TargetDeclaration) -> Result<()> { + require_token(&self.artifact_id, "artifact receipt id")?; + require_sha256(&self.sha256, "artifact digest")?; + ensure!(self.size_bytes > 0, "sealed artifact is empty"); + let declared = declaration + .artifacts + .iter() + .find(|artifact| artifact.id == self.artifact_id) + .with_context(|| format!("plan declares no artifact {}", self.artifact_id))?; + ensure!( + self.destination == declared.destination && self.executable == declared.executable, + "artifact receipt differs from declared output" + ); + if let Some(expected_sha256) = &declared.expected_sha256 { + ensure!( + self.sha256 == prefixed_sha256(expected_sha256), + "artifact receipt differs from the recipe-pinned digest" + ); + } + Ok(()) + } +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct SealedDeployment { +pub struct ExternalInputMaterializationReceipt { + pub input_id: String, + pub url: String, + pub sha256: String, + pub runner: String, + pub destination: PathBuf, + pub size_bytes: u64, +} + +impl ExternalInputMaterializationReceipt { + fn validate(&self, declaration: &TargetDeclaration) -> Result<()> { + require_token(&self.input_id, "external input receipt id")?; + require_sha256(&self.sha256, "external input materialized digest")?; + ensure!(self.size_bytes > 0, "materialized external input is empty"); + let declared = declaration + .external_inputs + .iter() + .find(|input| input.id == self.input_id) + .with_context(|| format!("plan declares no external input {}", self.input_id))?; + ensure!( + self.url == declared.url + && self.sha256 == prefixed_sha256(&declared.sha256) + && self.runner == declared.runner + && self.destination == declared.destination, + "external input receipt differs from its pinned declaration" + ); + Ok(()) + } +} + +/// Private Idunn release state. The content address covers the exact plan ID, +/// sorted full artifact receipts, and sorted full external-input +/// materialization receipts. The plan remains a separate private record and is +/// supplied explicitly for validation. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SealedRelease { pub schema: String, - pub release_id: String, - pub plan: CompiledDeploymentPlan, - pub plan_sha256: String, - pub artifacts: Vec, + pub sealed_release_id: String, + pub plan_id: String, + pub artifacts: Vec, + pub external_inputs: Vec, pub sealed_at_unix_millis: u64, } -impl SealedDeployment { +impl SealedRelease { pub fn new( - plan: CompiledDeploymentPlan, - release_id: impl Into, - artifacts: Vec, + plan: &CompiledDeploymentPlan, + mut artifacts: Vec, + mut external_inputs: Vec, sealed_at_unix_millis: u64, ) -> Result { plan.validate()?; - let mut sealed = Self { - schema: SEALED_DEPLOYMENT_SCHEMA.into(), - release_id: release_id.into(), - plan_sha256: sha256_id(&rmp_serde::to_vec(&plan)?), - plan, + artifacts.sort_by(|left, right| left.artifact_id.cmp(&right.artifact_id)); + external_inputs.sort_by(|left, right| left.input_id.cmp(&right.input_id)); + let mut release = Self { + schema: SEALED_RELEASE_SCHEMA.into(), + sealed_release_id: String::new(), + plan_id: plan.plan_id.clone(), artifacts, + external_inputs, sealed_at_unix_millis, }; - sealed.validate()?; - sealed - .artifacts - .sort_by(|left, right| left.artifact_id.cmp(&right.artifact_id)); - sealed.validate()?; - Ok(sealed) + release.validate_contents(plan)?; + release.sealed_release_id = release.recomputed_release_id()?; + release.validate_against(plan)?; + Ok(release) } - pub fn validate(&self) -> Result<()> { + pub fn validate_against(&self, plan: &CompiledDeploymentPlan) -> Result<()> { + require_sha256(&self.sealed_release_id, "sealed release id")?; + self.validate_contents(plan)?; ensure!( - self.schema == SEALED_DEPLOYMENT_SCHEMA, - "unsupported sealed-deployment schema" + self.sealed_release_id == self.recomputed_release_id()?, + "sealed release content address is wrong" ); - require_token(&self.release_id, "release id")?; - ensure!( - self.sealed_at_unix_millis >= self.plan.created_at_unix_millis, - "deployment was sealed before it was planned" - ); - self.plan.validate()?; - ensure!( - self.plan_sha256 == sha256_id(&rmp_serde::to_vec(&self.plan)?), - "sealed plan digest is wrong" - ); - let declared = self - .plan - .declaration + Ok(()) + } + + pub fn expected_projection( + &self, + plan: &CompiledDeploymentPlan, + ) -> Result { + self.validate_against(plan)?; + let (declaration, binding) = plan.parsed_inputs()?; + let node = binding + .placement + .nodes + .iter() + .next() + .context("validated singleton binding has no node")? + .clone(); + let route = match (&binding.route, plan.candidate_port) { + (Some(bound), Some(port)) => Some(ExpectedRoute { + route_id: bound.route_id.clone(), + transport: declaration.service.transport, + stable_endpoint: bound.stable_endpoint.clone(), + candidate_endpoint: format!( + "{}://{}:{port}", + endpoint_scheme(declaration.service.transport)?, + bound.private_host + ), + }), + (None, None) => None, + _ => bail!("plan route and candidate port disagree"), + }; + let executable_artifact_sha256 = self .artifacts .iter() - .map(|artifact| { - ( - artifact.id.as_str(), - artifact.destination.as_path(), - artifact.executable, - ) - }) - .chain( - self.plan - .declaration - .external_artifacts - .iter() - .map(|artifact| { - ( - artifact.id.as_str(), - artifact.destination.as_path(), - artifact.executable, - ) - }), - ) - .collect::>(); - let mut observed = BTreeSet::new(); - for artifact in &self.artifacts { - require_token(&artifact.artifact_id, "artifact seal id")?; - require_sha256(&artifact.sha256, "artifact seal digest")?; - ensure!(artifact.size_bytes > 0, "sealed artifact is empty"); - ensure!( - observed.insert(( - artifact.artifact_id.as_str(), - artifact.destination.as_path(), - artifact.executable - )), - "artifact seal is duplicated" - ); - } - ensure!( - declared == observed, - "sealed artifacts differ from declared outputs" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RuntimePresence { - pub target: String, - pub generation: String, - pub deployment_id: String, - pub source_revision: String, - pub endpoint: Option, - pub capabilities: Vec, - pub runtime_id: String, - pub signer_identity_id: String, - pub signed_statement_sha256: String, -} - -impl RuntimePresence { - pub fn validate(&self) -> Result<()> { - require_token(&self.target, "presence target")?; - require_token(&self.generation, "presence generation")?; - require_token(&self.deployment_id, "presence deployment")?; - require_sha1(&self.source_revision, "presence source revision")?; - if let Some(endpoint) = &self.endpoint { - require_value(endpoint, "presence endpoint")?; - } - require_token(&self.runtime_id, "presence runtime")?; - require_token(&self.signer_identity_id, "presence signer")?; - require_sha256(&self.signed_statement_sha256, "presence statement")?; - let mut capabilities = BTreeSet::new(); - for capability in &self.capabilities { - require_contract( - &capability.capability, - &capability.schema, - &capability.compatibility, - )?; - ensure!( - capability.capacity > 0, - "presence capability capacity is zero" - ); - ensure!( - capabilities.insert(( - &capability.capability, - &capability.schema, - &capability.compatibility, - )), - "presence capability is duplicated" - ); - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct HealthObservation { - pub contract: String, - pub state: String, - pub detail: String, - pub signed_statement_sha256: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct GenerationCorrelation { - pub schema: String, - pub target: String, - pub generation: String, - pub expected: bool, - pub present: bool, - pub ready: bool, - pub disagreements: Vec, -} - -pub fn correlate_generation( - plan: &CompiledDeploymentPlan, - presence: Option<&RuntimePresence>, - health: Option<&HealthObservation>, -) -> Result { - plan.validate()?; - let mut disagreements = Vec::new(); - let mut present = false; - if let Some(presence) = presence { - presence.validate()?; - for (matches, code) in [ - (presence.target == plan.expected.target, "target-mismatch"), - ( - presence.generation == plan.expected.generation, - "generation-mismatch", - ), - ( - presence.deployment_id == plan.expected.deployment_id, - "deployment-mismatch", - ), - ( - presence.source_revision == plan.expected.source_revision, - "source-mismatch", - ), - ( - presence.endpoint == plan.expected.candidate_endpoint, - "endpoint-mismatch", - ), - ( - capability_set(&presence.capabilities) - == capability_set(&plan.expected.capabilities), - "capability-mismatch", - ), - ] { - if !matches { - disagreements.push(code.into()); - } - } - present = disagreements.is_empty(); - } else { - disagreements.push("runtime-presence-missing".into()); - } - let mut health_ready = false; - if let (Some(presence), Some(health)) = (presence, health) { - require_token(&health.contract, "health contract")?; - require_value(&health.state, "health state")?; - require_value(&health.detail, "health detail")?; - require_sha256(&health.signed_statement_sha256, "health statement")?; - if health.signed_statement_sha256 != presence.signed_statement_sha256 { - disagreements.push("health-presence-statement-mismatch".into()); - } else if health.contract != plan.declaration.service.health.contract { - disagreements.push("health-contract-mismatch".into()); - } else if health.state != plan.declaration.service.health.ready.state { - disagreements.push("health-state-not-ready".into()); - } else if health.detail != plan.declaration.service.health.ready.detail { - disagreements.push("health-detail-not-ready".into()); - } else { - health_ready = true; - } - } else if health.is_none() { - disagreements.push("health-missing".into()); - } - if plan - .dependencies - .iter() - .any(DependencyResolution::blocks_promotion) - { - disagreements.push("dependency-graph-not-ready".into()); - } - disagreements.sort(); - disagreements.dedup(); - Ok(GenerationCorrelation { - schema: GENERATION_CORRELATION_SCHEMA.into(), - target: plan.expected.target.clone(), - generation: plan.expected.generation.clone(), - expected: true, - present, - ready: present && health_ready && disagreements.is_empty(), - disagreements, - }) -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum PromotionPhase { - Planned, - Materialized, - Sealed, - CandidateStarted, - Staged, - Fenced, - RouteObserved, - WriteAdmitted, - Active, - Draining, - Complete, -} - -impl PromotionPhase { - pub fn next(self) -> Option { - Some(match self { - Self::Planned => Self::Materialized, - Self::Materialized => Self::Sealed, - Self::Sealed => Self::CandidateStarted, - Self::CandidateStarted => Self::Staged, - Self::Staged => Self::Fenced, - Self::Fenced => Self::RouteObserved, - Self::RouteObserved => Self::WriteAdmitted, - Self::WriteAdmitted => Self::Active, - Self::Active => Self::Draining, - Self::Draining => Self::Complete, - Self::Complete => return None, - }) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct PromotionTransaction { - pub schema: String, - pub transaction_id: String, - pub plan_id: String, - pub target: String, - pub phase: PromotionPhase, - pub revision: u64, - pub updated_at_unix_millis: u64, -} - -impl PromotionTransaction { - pub fn new( - transaction_id: impl Into, - plan: &CompiledDeploymentPlan, - created_at_unix_millis: u64, - ) -> Result { - plan.validate()?; - let transaction = Self { - schema: PROMOTION_TRANSACTION_SCHEMA.into(), - transaction_id: transaction_id.into(), + .find(|artifact| artifact.artifact_id == declaration.service.executable_artifact) + .context("sealed release has no executable service artifact")? + .sha256 + .clone(); + let expected = ExpectedIncarnation { + schema: EXPECTED_INCARNATION_SCHEMA.into(), plan_id: plan.plan_id.clone(), - target: plan.declaration.target.clone(), - phase: PromotionPhase::Planned, - revision: 0, - updated_at_unix_millis: created_at_unix_millis, + sealed_release_id: self.sealed_release_id.clone(), + target: declaration.target.clone(), + incarnation_id: plan.incarnation_id.clone(), + runtime_id: binding.runtime_identity.runtime_id.clone(), + expected_signer_identity_id: binding + .runtime_identity + .expected_signer_identity_id + .clone(), + health_contract: declaration.service.health.contract.clone(), + executable_artifact_sha256, + state_schema_generation: declaration + .state + .as_ref() + .map(|state| state.schema_generation.clone()), + state_contract_sha256: declaration + .state + .as_ref() + .map(canonical_state_contract_sha256) + .transpose()?, + write_lease_required: declaration.write_lease_required(), + source_revision: plan.source.revision.clone(), + node, + route, + capabilities: declaration.provides.clone(), + dependencies: plan.dependencies.clone(), }; - transaction.validate()?; - Ok(transaction) + expected.validate()?; + Ok(expected) } - pub fn validate(&self) -> Result<()> { + fn validate_contents(&self, plan: &CompiledDeploymentPlan) -> Result<()> { ensure!( - self.schema == PROMOTION_TRANSACTION_SCHEMA, - "unsupported promotion transaction schema" + self.schema == SEALED_RELEASE_SCHEMA, + "unsupported sealed-release schema" ); - require_token(&self.transaction_id, "promotion transaction")?; - require_sha256(&self.plan_id, "promotion plan")?; - require_token(&self.target, "promotion target")?; + plan.validate()?; + let (declaration, _) = plan.parsed_inputs()?; + require_sha256(&self.plan_id, "sealed plan id")?; ensure!( - self.updated_at_unix_millis > 0, - "promotion transaction has no update time" + self.plan_id == plan.plan_id, + "sealed release refers to a different plan" ); + ensure!( + self.sealed_at_unix_millis >= plan.created_at_unix_millis, + "release was sealed before it was planned" + ); + ensure!( + self.artifacts + .windows(2) + .all(|pair| pair[0].artifact_id < pair[1].artifact_id), + "artifact receipts are not in canonical order" + ); + ensure!( + self.external_inputs + .windows(2) + .all(|pair| pair[0].input_id < pair[1].input_id), + "external input receipts are not in canonical order" + ); + let declared_ids = declaration + .artifacts + .iter() + .map(|artifact| artifact.id.as_str()) + .collect::>(); + let observed_ids = self + .artifacts + .iter() + .map(|artifact| artifact.artifact_id.as_str()) + .collect::>(); + ensure!( + declared_ids == observed_ids && observed_ids.len() == self.artifacts.len(), + "artifact receipts differ from declared outputs" + ); + for artifact in &self.artifacts { + artifact.validate(&declaration)?; + } + let declared_input_ids = declaration + .external_inputs + .iter() + .map(|input| input.id.as_str()) + .collect::>(); + let observed_input_ids = self + .external_inputs + .iter() + .map(|input| input.input_id.as_str()) + .collect::>(); + ensure!( + declared_input_ids == observed_input_ids + && observed_input_ids.len() == self.external_inputs.len(), + "external input receipts differ from pinned declarations" + ); + for input in &self.external_inputs { + input.validate(&declaration)?; + } Ok(()) } - pub fn advance(&self, phase: PromotionPhase, updated_at_unix_millis: u64) -> Result { - self.validate()?; - ensure!( - self.phase.next() == Some(phase), - "promotion phase transition is not adjacent" - ); - ensure!( - updated_at_unix_millis >= self.updated_at_unix_millis, - "promotion time moved backwards" - ); - let next = Self { - schema: self.schema.clone(), - transaction_id: self.transaction_id.clone(), - plan_id: self.plan_id.clone(), - target: self.target.clone(), - phase, - revision: self - .revision - .checked_add(1) - .context("promotion revision overflow")?, - updated_at_unix_millis, - }; - next.validate()?; - Ok(next) + fn recomputed_release_id(&self) -> Result { + Ok(sha256_id( + &rmp_serde::to_vec(&( + self.schema.as_str(), + self.plan_id.as_str(), + &self.artifacts, + &self.external_inputs, + )) + .context("encoding sealed release identity")?, + )) } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct AdmittedGeneration { - pub schema: String, - pub target: String, - pub generation: String, - pub deployment_id: String, - pub plan_id: String, - pub release_id: String, - pub source_revision: String, - pub workload_unit: String, - pub process_id: u32, - pub process_starttime_ticks: u64, - pub private_endpoint: Option, - pub route_id: Option, - pub route_observation_sha256: Option, - pub write_admission_sha256: String, - pub admitted_at_unix_millis: u64, +fn canonical_state_contract_sha256(state: &StateDeclaration) -> Result { + Ok(sha256_id( + &rmp_serde::to_vec(state).context("encoding canonical state contract")?, + )) } -impl AdmittedGeneration { - pub fn validate_against(&self, sealed: &SealedDeployment) -> Result<()> { - ensure!( - self.schema == ADMITTED_GENERATION_SCHEMA, - "unsupported admitted-generation schema" - ); - sealed.validate()?; - require_token(&self.target, "admitted target")?; - require_token(&self.generation, "admitted generation")?; - require_token(&self.deployment_id, "admitted deployment")?; - require_sha256(&self.plan_id, "admitted plan")?; - require_token(&self.release_id, "admitted release")?; - require_sha1(&self.source_revision, "admitted source")?; - require_token(&self.workload_unit, "admitted workload unit")?; - ensure!(self.process_id > 0, "admitted process id is zero"); - ensure!( - self.process_starttime_ticks > 0, - "admitted process start time is zero" - ); - if let Some(endpoint) = &self.private_endpoint { - require_value(endpoint, "admitted private endpoint")?; +fn endpoint_scheme(transport: ServiceTransport) -> Result<&'static str> { + match transport { + ServiceTransport::Http => Ok("http"), + ServiceTransport::Tcp => Ok("tcp"), + ServiceTransport::Rudp | ServiceTransport::Private => { + bail!("service transport has no stable route scheme") } - match (&self.route_id, &self.route_observation_sha256) { - (Some(route_id), Some(digest)) => { - require_token(route_id, "admitted route")?; - require_sha256(digest, "route observation")?; - } - (None, None) => {} - _ => bail!("admitted route identity and observation are incomplete"), - } - require_sha256(&self.write_admission_sha256, "write admission")?; - ensure!(self.admitted_at_unix_millis > 0, "admission has no time"); - ensure!( - self.target == sealed.plan.declaration.target, - "admitted target differs from sealed plan" - ); - ensure!( - self.generation == sealed.plan.declaration.state.generation, - "admitted generation differs from sealed plan" - ); - ensure!( - self.deployment_id == sealed.plan.deployment_id, - "admitted deployment differs from sealed plan" - ); - ensure!( - self.plan_id == sealed.plan.plan_id, - "admitted plan differs from sealed plan" - ); - ensure!( - self.release_id == sealed.release_id, - "admitted release differs from sealed plan" - ); - ensure!( - self.source_revision == sealed.plan.source.revision, - "admitted source differs from sealed plan" - ); - ensure!( - self.private_endpoint == sealed.plan.expected.candidate_endpoint, - "admitted endpoint differs from expected plan" - ); - match &sealed.plan.binding.route { - Some(route) => ensure!( - self.route_id.as_deref() == Some(route.route_id.as_str()), - "admitted route differs from binding" - ), - None => ensure!( - self.route_id.is_none(), - "private deployment carries a route" - ), - } - Ok(()) } } +fn prefixed_sha256(raw_sha256: &str) -> String { + format!("sha256-{raw_sha256}") +} + fn require_token(value: &str, label: &str) -> Result<()> { ensure!( !value.is_empty() && value.len() <= 128, @@ -967,20 +935,6 @@ fn require_contract(capability: &str, schema: &str, compatibility: &str) -> Resu require_token(compatibility, "capability compatibility") } -fn capability_set(capabilities: &[ProvidedCapability]) -> BTreeSet<(&str, &str, &str, u32)> { - capabilities - .iter() - .map(|capability| { - ( - capability.capability.as_str(), - capability.schema.as_str(), - capability.compatibility.as_str(), - capability.capacity, - ) - }) - .collect() -} - fn require_value(value: &str, label: &str) -> Result<()> { ensure!( !value.trim().is_empty() && value == value.trim(), @@ -1029,7 +983,6 @@ fn sha256_id(bytes: &[u8]) -> String { #[cfg(test)] mod tests { use super::*; - use crate::deployment::{OperatorBinding, TargetDeclaration}; const RECIPE: &str = r#" schema = "gamecult.idunn.target_declaration.v1" @@ -1042,12 +995,20 @@ phase = "build" runner = "rust" argv = ["cargo", "build", "--locked"] +[[external_inputs]] +id = "toolchain-index" +url = "https://example.invalid/toolchain-index" +sha256 = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +runner = "rust" +destination = "inputs/toolchain-index" + [[artifacts]] id = "daemon" source_kind = "runner-output" runner = "rust" source = "target/release/service" destination = "service" +expected_sha256 = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" executable = true [service] @@ -1059,16 +1020,17 @@ required_environment = ["SERVICE_BIND"] [service.health] contract = "service.health" -[service.health.staged] -state = "warming" -detail = "traffic-admission-pending" - -[service.health.ready] -state = "active" -detail = "serving" - [state] -generation = "v1" +schema_generation = "v1" + +[[state.slots]] +id = "runtime-cache" +relative_path = "runtime-cache.cc" +kind = "cultcache-file" +schema = "service.runtime-cache.v1" +writer = "none" +recovery = "rebuildable" +startup = "open-at-start" [[provides]] capability = "service.runtime" @@ -1099,7 +1061,7 @@ driver = "docker" image = "rust@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" affordances = ["source-read", "artifact-write"] allowed_programs = ["cargo"] -network = "none" +network_profile = "build-dependency-egress" memory_mebibytes = 2048 cpu_quota_percent = 200 @@ -1119,6 +1081,11 @@ cpu_quota_percent = 100 [workload.environment] SERVICE_BIND = "idunn.private_endpoint" +[runtime_identity] +runtime_id = "service-yggdrasil" +expected_signer_identity_id = "service-runtime-signer" +trust_anchor_store = "/etc/gamecult/trust/service.cc" + [route] driver = "nginx-http" route_id = "service" @@ -1129,11 +1096,6 @@ private_port_end = 18009 config_path = "/etc/nginx/idunn-routes/service.conf" reload_unit = "nginx.service" -[admission] -driver = "atomic-file" -record_path = "/run/gamecult/idunn/service/admission.cc" -lock_path = "/run/gamecult/idunn/service/admission.cc.lock" - [brakes] deployment_store = "/var/lib/gamecult/idunn/service-deployment-brake.cc" lifecycle_store = "/var/lib/gamecult/idunn/service-lifecycle-brake.cc" @@ -1151,172 +1113,275 @@ desired_replicas = 1 nodes = ["yggdrasil"] "#; - fn provider(ready: bool) -> ProviderObservation { - ProviderObservation { - provider_id: "odin-yggdrasil".into(), - provider_target: Some("odin".into()), - origin: ProviderOrigin::ManagedRuntime, - generation: "odin-v1".into(), - deployment_id: Some("odin-deployment-1".into()), - capability: "odin.verse-rendezvous".into(), - schema: "odin.verse-topology.v1".into(), - compatibility: "v1".into(), - capacity: 1, - endpoint: "10.77.0.1:17871".into(), - expected: true, - present: ready, - ready, - observation_sha256: ready.then(|| format!("sha256-{}", "a".repeat(64))), + fn digest(byte: u8) -> String { + format!("sha256-{}", char::from(byte).to_string().repeat(64)) + } + + fn source(recipe: &str) -> SourceSelectionFacts { + SourceSelectionFacts { + schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), + origin: "https://github.com/GameCult/Service.git".into(), + admitted_ref: "refs/heads/main".into(), + revision: "2222222222222222222222222222222222222222".into(), + source_tree: "3333333333333333333333333333333333333333".into(), + recipe_path: "deployment/idunn/recipe.toml".into(), + recipe_blob_sha256: sha256_id(recipe.as_bytes()), + gitlinks: BTreeMap::new(), + selection: SourceSelection::RefHead, + selected_at_unix_millis: 100, } } - fn plan(provider: ProviderObservation) -> CompiledDeploymentPlan { + fn odin() -> ExpectedIncarnation { + ExpectedIncarnation { + schema: EXPECTED_INCARNATION_SCHEMA.into(), + plan_id: digest(b'a'), + sealed_release_id: digest(b'e'), + target: "odin".into(), + incarnation_id: "odin-incarnation-1".into(), + runtime_id: "odin-yggdrasil".into(), + expected_signer_identity_id: "odin-runtime-signer".into(), + health_contract: "odin.cultnet-service-health".into(), + executable_artifact_sha256: digest(b'f'), + state_schema_generation: None, + state_contract_sha256: None, + write_lease_required: false, + source_revision: "2222222222222222222222222222222222222222".into(), + node: "yggdrasil".into(), + route: Some(ExpectedRoute { + route_id: "odin-private".into(), + transport: ServiceTransport::Tcp, + stable_endpoint: "tcp://10.77.0.1:17871".into(), + candidate_endpoint: "tcp://127.0.0.1:17871".into(), + }), + capabilities: vec![ProvidedCapability { + capability: "odin.verse-rendezvous".into(), + schema: "odin.verse-topology.v1".into(), + compatibility: "v1".into(), + capacity: 1, + }], + dependencies: Vec::new(), + } + } + + fn plan() -> CompiledDeploymentPlan { compile_deployment_plan( - TargetDeclaration::parse(RECIPE).unwrap(), - OperatorBinding::parse(BINDING).unwrap(), - ExactSource { - origin: "https://github.com/GameCult/Service.git".into(), - admitted_ref: "refs/heads/main".into(), - revision: "2222222222222222222222222222222222222222".into(), - minimum_revision: "1111111111111111111111111111111111111111".into(), - selection_receipt_sha256: format!("sha256-{}", "d".repeat(64)), - gitlinks: BTreeMap::new(), - }, RECIPE.as_bytes(), BINDING.as_bytes(), - "deployment-1", + source(RECIPE), + "service-incarnation-1", Some(18001), - 100, - &[provider], + 110, + &[odin()], ) .unwrap() } - #[test] - fn compilation_is_deterministic_and_configuration_does_not_supply_readiness() { - let first = plan(provider(true)); - let second = plan(provider(true)); - assert_eq!(first, second); - assert!(!first.dependencies[0].blocks_promotion()); + fn artifact_receipt() -> ArtifactReceipt { + ArtifactReceipt { + artifact_id: "daemon".into(), + destination: "service".into(), + sha256: digest(b'c'), + size_bytes: 42, + executable: true, + } + } - let external = ExternalCapabilityBinding { - capability: "odin.verse-rendezvous".into(), - schema: "odin.verse-topology.v1".into(), - compatibility: "v1".into(), - endpoint: "10.77.0.1:17871".into(), - }; - let observations = external_binding_observations(&[external]); - assert!(!observations[0].present); - assert!(!observations[0].ready); + fn external_input_receipt() -> ExternalInputMaterializationReceipt { + ExternalInputMaterializationReceipt { + input_id: "toolchain-index".into(), + url: "https://example.invalid/toolchain-index".into(), + sha256: digest(b'd'), + runner: "rust".into(), + destination: "inputs/toolchain-index".into(), + size_bytes: 17, + } } #[test] - fn shared_infrastructure_reuses_the_ready_provider() { - let mut weaker = provider(false); - weaker.provider_id = "odin-candidate".into(); - let resolved = resolve_dependencies( - &TargetDeclaration::parse(RECIPE).unwrap(), - &[weaker, provider(true)], - ) - .unwrap(); + fn compiler_parses_the_exact_bytes_it_receipts() { + let first = plan(); + let second = plan(); + assert_eq!(first, second); + assert_eq!(first.recipe_blob.as_slice(), RECIPE.as_bytes()); + assert_eq!(first.binding_blob.as_slice(), BINDING.as_bytes()); assert_eq!( - resolved[0].provider.as_ref().unwrap().provider_id, + sha256_id(&first.recipe_blob), + first.source.recipe_blob_sha256 + ); + let changed = RECIPE.replace( + "contract = \"service.health\"", + "contract = \"service.health.v2\"", + ); + assert!( + compile_deployment_plan( + changed.as_bytes(), + BINDING.as_bytes(), + source(RECIPE), + "service-incarnation-2", + Some(18002), + 111, + &[odin()], + ) + .is_err() + ); + let mut corrupted = first; + corrupted + .binding_blob + .extend_from_slice(b"\nunknown = true\n"); + assert!(corrupted.validate().is_err()); + } + + #[test] + fn pinned_policy_requires_the_exact_operator_pin() { + let pinned_binding = BINDING.replace( + "selection = \"ref-head\"", + "selection = \"pinned-object\"\npinned_revision = \"2222222222222222222222222222222222222222\"", + ); + let binding = OperatorBinding::parse(&pinned_binding).unwrap(); + assert!(source(RECIPE).validate_against(&binding).is_err()); + let mut facts = source(RECIPE); + facts.selection = SourceSelection::PinnedObject; + facts.validate_against(&binding).unwrap(); + } + + #[test] + fn gitlink_fact_must_match_the_selected_superproject_tree() { + let recipe = RECIPE.replace( + "source_stamp_environment = \"SERVICE_BUILD_COMMIT\"", + "source_stamp_environment = \"SERVICE_BUILD_COMMIT\"\nrequired_gitlinks = [\"vendor/lib\"]", + ); + let binding_text = BINDING.replace( + "recipe_path = \"deployment/idunn/recipe.toml\"", + "recipe_path = \"deployment/idunn/recipe.toml\"\ngitlinks = { \"vendor/lib\" = { origin = \"https://github.com/GameCult/Lib.git\" } }", + ); + let binding = OperatorBinding::parse(&binding_text).unwrap(); + binding + .admit(&TargetDeclaration::parse(&recipe).unwrap()) + .unwrap(); + let mut facts = source(&recipe); + facts.gitlinks.insert( + "vendor/lib".into(), + GitlinkTreeFact { + origin: "https://github.com/GameCult/Lib.git".into(), + revision: "4444444444444444444444444444444444444444".into(), + tree_entry_revision: "4444444444444444444444444444444444444444".into(), + }, + ); + facts.validate_against(&binding).unwrap(); + facts + .gitlinks + .get_mut(&PathBuf::from("vendor/lib")) + .unwrap() + .tree_entry_revision = "5555555555555555555555555555555555555555".into(); + assert!(facts.validate_against(&binding).is_err()); + } + + #[test] + fn dependency_selection_uses_expected_identity_without_readiness() { + let declaration = TargetDeclaration::parse(RECIPE).unwrap(); + let mut later = odin(); + later.runtime_id = "odin-z".into(); + let providers = managed_expected_provider_refs(&[later, odin()]).unwrap(); + let selected = select_dependencies(&declaration, &providers).unwrap(); + assert_eq!( + selected[0].provider.as_ref().unwrap().provider_id, "odin-yggdrasil" ); } #[test] - fn present_and_ready_are_distinct_from_expected() { - let plan = plan(provider(true)); - let presence = RuntimePresence { - target: "service".into(), - generation: "v1".into(), - deployment_id: "deployment-1".into(), - source_revision: "2222222222222222222222222222222222222222".into(), - endpoint: Some("127.0.0.1:18001".into()), - capabilities: plan.expected.capabilities.clone(), - runtime_id: "service-runtime".into(), - signer_identity_id: "signer-1".into(), - signed_statement_sha256: format!("sha256-{}", "b".repeat(64)), - }; - let health = HealthObservation { - contract: "service.health".into(), - state: "active".into(), - detail: "serving".into(), - signed_statement_sha256: presence.signed_statement_sha256.clone(), - }; - let absent = correlate_generation(&plan, None, None).unwrap(); - assert!(absent.expected); - assert!(!absent.present); - assert!(!absent.ready); - let ready = correlate_generation(&plan, Some(&presence), Some(&health)).unwrap(); - assert!(ready.present); - assert!(ready.ready); - } - - #[test] - fn disagreement_remains_visible_and_blocks_readiness() { - let plan = plan(provider(true)); - let presence = RuntimePresence { - target: "service".into(), - generation: "wrong-generation".into(), - deployment_id: "deployment-1".into(), - source_revision: "2222222222222222222222222222222222222222".into(), - endpoint: Some("127.0.0.1:18001".into()), - capabilities: plan.expected.capabilities.clone(), - runtime_id: "service-runtime".into(), - signer_identity_id: "signer-1".into(), - signed_statement_sha256: format!("sha256-{}", "b".repeat(64)), - }; - let health = HealthObservation { - contract: "service.health".into(), - state: "active".into(), - detail: "serving".into(), - signed_statement_sha256: presence.signed_statement_sha256.clone(), - }; - let correlation = correlate_generation(&plan, Some(&presence), Some(&health)).unwrap(); - assert!(!correlation.present); - assert!(!correlation.ready); - assert!( - correlation - .disagreements - .contains(&"generation-mismatch".into()) + fn external_binding_produces_expected_configuration_only() { + let input = BINDING.replace( + "[placement]", + "[[external_capabilities]]\nprovider_id = \"operator-archive\"\ncapability = \"archive.store\"\nschema = \"archive.store.v1\"\ncompatibility = \"v1\"\ncapacity = 2\nendpoint = \"s3://operator-bound\"\n\n[placement]", + ); + let binding = OperatorBinding::parse(&input).unwrap(); + let providers = external_binding_provider_refs(&binding).unwrap(); + let declaration = TargetDeclaration::parse(&RECIPE.replace( + "kind = \"shared-infrastructure\"\ncapability = \"odin.verse-rendezvous\"\nschema = \"odin.verse-topology.v1\"", + "kind = \"external-operator-binding\"\ncapability = \"archive.store\"\nschema = \"archive.store.v1\"", + )) + .unwrap(); + binding.admit(&declaration).unwrap(); + let selected = select_dependencies(&declaration, &providers).unwrap(); + assert!(matches!( + &providers[0].authority, + ExpectedProviderAuthority::ExternalOperatorBinding { .. } + )); + assert_eq!(providers[0].capacity, 2); + assert_eq!( + selected[0].provider.as_ref().unwrap().provider_id, + "operator-archive" ); } #[test] - fn sealed_artifacts_and_promotion_phases_are_exact() { - let plan = plan(provider(true)); - let sealed = SealedDeployment::new( - plan.clone(), - "release-1", - vec![ArtifactSeal { - artifact_id: "daemon".into(), - destination: "service".into(), - sha256: format!("sha256-{}", "c".repeat(64)), - size_bytes: 42, - executable: true, - }], - 110, + fn expected_projection_is_sanitized_and_names_the_selected_graph() { + let plan = plan(); + let release = SealedRelease::new( + &plan, + vec![artifact_receipt()], + vec![external_input_receipt()], + 120, ) .unwrap(); - sealed.validate().unwrap(); - let transaction = PromotionTransaction { - schema: PROMOTION_TRANSACTION_SCHEMA.into(), - transaction_id: "transaction-1".into(), - plan_id: plan.plan_id, - target: "service".into(), - phase: PromotionPhase::Planned, - revision: 0, - updated_at_unix_millis: 100, - }; - assert!(transaction.advance(PromotionPhase::Sealed, 101).is_err()); + let expected = release.expected_projection(&plan).unwrap(); + assert_eq!(expected.plan_id, plan.plan_id); + assert_eq!(expected.sealed_release_id, release.sealed_release_id); + assert_eq!(expected.incarnation_id, "service-incarnation-1"); + assert_eq!(expected.runtime_id, "service-yggdrasil"); assert_eq!( - transaction - .advance(PromotionPhase::Materialized, 101) - .unwrap() - .phase, - PromotionPhase::Materialized + expected.expected_signer_identity_id, + "service-runtime-signer" ); + assert_eq!(expected.health_contract, "service.health"); + assert_eq!(expected.executable_artifact_sha256, digest(b'c')); + assert_eq!(expected.state_schema_generation.as_deref(), Some("v1")); + assert!(expected.state_contract_sha256.is_some()); + assert!(!expected.write_lease_required); + assert_eq!(expected.node, "yggdrasil"); + assert_eq!( + expected.dependencies[0] + .provider + .as_ref() + .unwrap() + .provider_id, + "odin-yggdrasil" + ); + assert_eq!( + expected.route.as_ref().unwrap().candidate_endpoint, + "http://127.0.0.1:18001" + ); + assert!(expected.canonical_sha256().unwrap().starts_with("sha256-")); + let mut invalid_release = release; + invalid_release.artifacts[0].size_bytes += 1; + assert!(invalid_release.expected_projection(&plan).is_err()); + } + + #[test] + fn sealed_release_address_covers_artifacts_and_external_inputs() { + let receipt = artifact_receipt(); + let input = external_input_receipt(); + let plan = plan(); + let mut wrong_artifact = receipt.clone(); + wrong_artifact.sha256 = digest(b'f'); + assert!(SealedRelease::new(&plan, vec![wrong_artifact], vec![input.clone()], 120).is_err()); + assert!(SealedRelease::new(&plan, vec![receipt.clone()], vec![], 120).is_err()); + let release = + SealedRelease::new(&plan, vec![receipt.clone()], vec![input.clone()], 120).unwrap(); + let later_receipt_time = + SealedRelease::new(&plan, vec![receipt], vec![input], 130).unwrap(); + release.validate_against(&plan).unwrap(); + assert_eq!( + release.sealed_release_id, + later_receipt_time.sealed_release_id + ); + let mut changed = release.clone(); + changed.artifacts[0].size_bytes = 43; + assert!(changed.validate_against(&plan).is_err()); + let mut changed = release; + changed.external_inputs[0].size_bytes = 18; + assert!(changed.validate_against(&plan).is_err()); } } diff --git a/docs/idunn-deployment-authority.md b/docs/idunn-deployment-authority.md index 7d1bce6..fafa1f2 100644 --- a/docs/idunn-deployment-authority.md +++ b/docs/idunn-deployment-authority.md @@ -10,7 +10,7 @@ The architectural test is simple: - Where and how a workload runs is generic infrastructure work. Use the configured systemd, container, proxy, or later Nomad/Kubernetes driver. - Which GameCult incarnation has authority to serve, write state, or represent - an admitted generation is Idunn work. + an admitted workload is Idunn work. Idunn and Odin are closely integrated but do not share authority. Idunn owns the admitted physical swarm. Odin owns the discoverable semantic topology of @@ -21,11 +21,11 @@ and health. CultMesh carries typed projections between them. - **Owner:** Idunn alone selects an exact source, admits its repository recipe under an operator binding, records the resulting artifact and process - provenance, grants process-bound route and state-write admission, and keeps - the admitted generation alive. + provenance, grants a process-bound state-write lease, separately admits + route membership, and keeps the admitted incarnation alive. - **Inputs:** root-admitted operator bindings; source-owned strict deployment declarations; exact Git objects read as the `idunn` identity; signed - deployment or lifecycle-brake state; signed generation-bound service + deployment or lifecycle-brake state; signed incarnation-bound service presence and health; Odin's observed capability topology when graph changes require semantic discovery; and observations from the configured workload and route drivers. @@ -41,12 +41,13 @@ and health. CultMesh carries typed projections between them. secrets, mounts, network, capabilities, resources, host paths, routes, placement, source refs, or rollout policy. Odin cannot promote a process or grant state writes. nginx cannot choose membership. systemd and container - runtimes cannot choose the admitted generation. Service configuration cannot + runtimes cannot choose the admitted incarnation. Service configuration cannot impersonate observed presence or readiness. Persisted shell strings and repository-supplied root programs are not executable authority. - **Shared paths:** `idunn up`, scheduled deployment, manual deployment, scaling, restart, host reboot recovery, and crash recovery use the same - source-to-recipe-to-artifact provenance and the same admission primitive. + source-to-recipe-to-artifact provenance and the same distinct write-lease and + route-membership primitives. Deployment may change that chain only while the deployment brake admits the exact transaction. Continuity may restart only the already-admitted chain and is governed by a separate lifecycle brake. @@ -57,19 +58,40 @@ and health. CultMesh carries typed projections between them. decide Ghostlight or CodexConnector deployment before either target is promoted through this design. +### Current implementation boundary + +`deployment.rs` and `deployment_plan.rs` are the deterministic contract +foundation only. They parse one strict source declaration and one root-admitted +operator binding from the exact raw blobs retained by the compiled plan, check +private source-selection facts, derive managed providers from validated Expected +documents, and content-address a sealed release. They do not yet +drive Git, runners, systemd, nginx, brakes, CultCache, CultMesh, or the legacy +Idunn supervisor. + +The full operator binding, compiled plan, source facts, and sealed release +are private Idunn control-plane state. They contain host paths and operational +bindings and must never be published as Odin topology. Only the sanitized +`ExpectedIncarnation` projection is shaped for later CultMesh publication. It +is derived only from a sealed release validated against its compiled plan and +names both content addresses and the executable artifact digest. Present and +Ready remain Odin-owned correlations over independently verified runtime +evidence; the deployment-plan module deliberately does not model them. + ## The three truths Odin exposes -For each generation, Odin keeps three distinct observations: +For each workload incarnation, Odin keeps three distinct observations: -1. **Expected** — Idunn has admitted or intends this exact generation and has - published its deployment identity, endpoints, declared capabilities, and - dependency role. -2. **Present** — that generation has established its own signed CultNet/CultMesh - runtime session. -3. **Ready** — the observed generation satisfies its signed capability and +1. **Expected** — Idunn has sealed this exact incarnation and has published its + plan and release identities, admitted runtime and signer identities, health + and optional canonical typed-MessagePack state-contract identities, + endpoints, declared capabilities, and dependency role. +2. **Present** — Idunn has published the launch-scoped activation only after its + actuator observed the exact runtime instance, and that instance has + established its own matching signed CultNet/CultMesh runtime session. +3. **Ready** — the observed incarnation satisfies its signed capability and health contract and its required dependency graph is ready. -Correlation never repairs disagreement. Generation, runtime identity, endpoint, +Correlation never repairs disagreement. Incarnation, runtime identity, endpoint, schema, capability, and dependency mismatches remain visible and block promotion. Configuration contributes only expected state; it cannot supply present or ready state. @@ -87,10 +109,10 @@ The declaration contains no host commands or privileges. It describes: - schema and target identity; - requested runner profiles; - build and test argv executed inside those runners; -- package inputs and immutable artifact outputs; +- pinned generic HTTPS inputs and immutable artifact outputs; - a constrained launch contract naming the packaged executable, literal arguments, operator-bound argument slots, and required environment names; -- health, staged-readiness, and state/schema contracts; +- one health contract and, only for stateful services, a state/schema contract; - capabilities provided, including schema and compatibility information; - bootstrap, required, optional, shared-infrastructure, private, and external operator-bound capability dependencies; @@ -101,6 +123,12 @@ Dependencies resolve by typed capability/provider compatibility. A declaration names a daemon only when identity itself is the contract; ordinary dependencies must not hard-code a provider instance. +Runtime health uses the shared `warming`, `active`, `degraded`, and `failed` +ontology. A recipe names only its health contract. It cannot redefine those +states, readiness, or operator-facing detail. Bootstrap dependencies must be +available before process start; the remaining dependency kinds retain their +typed intent without making this foundation a scheduler. + The declaration may run repository code only inside a runner sandbox admitted by the operator binding. It cannot publish a host path, Docker argument, systemd property, nginx fragment, UID, secret path, or capability grant. Idunn @@ -115,14 +143,19 @@ For one target on one fleet it owns: - canonical repository origin, admitted ref, and minimum revision; - exact runner kind, image digest, and affordances for each requested profile; -- source, cache, output, release, state, and runtime roots; -- network mode, allowed dependency egress, mounts, identities, secrets, +- source, cache, output, release, and runtime roots, plus state roots and + transition policy only when the recipe declares state; +- preconfigured runner network profiles, mounts, identities, secrets, capabilities, resource bounds, and device access; - workload driver, transient-unit namespace, private endpoint range, and stable route; +- runtime identity, expected signer identity, and trust-anchor store; +- a process-write-lease record only when the recipe declares process-bound + writable state; its sibling lock path is derived rather than configured; - route driver and its root-owned configuration destination; - rollout, drain, irreversible-cut, and retention policy; -- desired replicas and allowed placement; +- one admitted replica on one named node in v1; later placement and scaling are + delegated to an established orchestrator driver; - externally supplied capability bindings. A recipe request that has no compatible admitted binding fails closed. A @@ -134,14 +167,19 @@ does not own application build or deployment programs. ## Typed deployment state -Mutable control-plane state is CultCache. The live path uses typed records for: +Mutable control-plane state will be CultCache. Runtime integration must use +typed records for: - requested service or profile; - resolved capability graph and exact providers; - exact source and recipe digest; - runner binding and artifact digests; -- candidate process, private endpoint, and signed staged health; -- process-bound write lease and route admission; +- full materialization receipts for every pinned external input and artifact; +- candidate runtime identity, private endpoint, and signed runtime health; +- launch-scoped runtime activation, published as current only after workload- + driver observation; +- process-bound write lease; +- independently admitted route membership; - expected, present, and ready correlation; - promotion, drain, failure, and continuity results. @@ -153,8 +191,8 @@ migration artifact and declare its accepted source schemas, destination schema, and literal/operator-bound argv. The operator binding admits the state roots, backup root, and whether this rollout permits migration or a fresh-root cut. Idunn alone issues a fenced one-shot migration grant bound to the exact source, -artifact, state generation, and deployment. A target migration never receives -general deployment or lifecycle authority. +artifact, state schema generation, and deployment incarnation. A target +migration never receives general deployment or lifecycle authority. ## Initial drivers and replaceable boundary @@ -165,87 +203,108 @@ The first Yggdrasil implementation uses: - Docker only for admitted build/test/package runners; - nginx HTTP proxying for Ghostlight; - nginx stream proxying for CodexConnector's stable TCP endpoint; -- atomic root-owned files for process/write admission and generated proxy - membership; +- atomic root-owned files for process-bound write leases and separately + generated proxy membership; - graceful nginx validation and reload for route changes. -Idunn calls narrow workload, route, and admission/fencing driver ports. Those -ports express stage, start, observe, stop, validate route, publish a fenced or -process-bound admission, and promote route. They do not express GameCult -admission decisions. A later Nomad or Kubernetes driver may implement the same -consequences. Idunn must not grow general bin-packing, distributed -scheduling, service-mesh, container-format, cryptography, or consensus logic; -when those become requirements, place Idunn's admission semantics over an -established orchestrator. +Idunn calls narrow workload, route, and write-lease/fencing driver ports. Those +ports express stage, start, observe, stop, validate route, fence an incumbent, +publish a process-bound write lease, and separately promote route membership. +They do not express GameCult admission decisions. A later Nomad or Kubernetes +driver may implement the same consequences. Idunn must not grow general +bin-packing, distributed scheduling, service-mesh, container-format, +cryptography, or consensus logic; when those become requirements, place +Idunn's admission semantics over an established orchestrator. ## Candidate promotion -For a singleton generation: +For a singleton incarnation: 1. Resolve the required capability graph. Reuse a compatible admitted shared provider, especially Odin, rather than spawning a duplicate. -2. Publish the desired fleet and candidate as expected state. +2. Publish desired fleet intent. Intent is not an Expected incarnation. 3. Freeze the exact source and recipe as `idunn`, then copy and verify it into a root-owned actuation stage. The privileged driver never opens Git. -4. Build, test, and package inside the bound runners. Seal the artifact, recipe, - unit, and input digests. -5. Start the candidate through the workload driver on a private endpoint. It - has no stable route or canonical write lease. -6. Require its own signed generation-bound `staged` health and signed runtime - presence. Render and validate the proposed route before changing admission. -7. Compare-and-swap the current process/write-admission record into a named - promotion fence. No generation may write while fenced; the incumbent may - remain routed but must return a retryable unavailable result. -8. Publish the candidate backend, gracefully reload the route driver, and +4. Build, test, and package inside the bound runners. Seal the artifact, + declaration, binding, launch contract, and input digests. +5. Derive and publish the release-bound Expected incarnation from the validated + sealed release and its exact compiled plan. +6. Issue one opaque runtime-instance activation bound to the Expected digest, + pass the exact Expected and activation documents through the actuator's + isolated credential mechanism, and start the candidate privately. It has no + stable route or canonical write lease. +7. After the workload driver observes the exact native runtime and executable + artifact, publish that activation as current. Require the candidate's own + signed matching `warming` presence. Render and validate the proposed route + before changing either authority. +8. Open a named promotion transaction and separately fence the incumbent + writer. The incumbent may remain routed but its process-bound write authority + is revoked and it must return a retryable unavailable result. +9. Grant the private candidate its own process-bound write lease. Keep it off + the stable route and wait for signed `active` health and the independently + correlated Ready observation. +10. Publish the candidate backend, gracefully reload the route driver, and observe that the proxy has adopted it. Proxy lag is explicit transaction state, not readiness. -9. Compare-and-swap the fence into the candidate's process-bound write grant. - Wait for signed `active` health, then drain and stop the incumbent. +11. Clear the promotion fence only after route adoption is observed, then drain + and stop the incumbent. -The admission record is the one writer lease. A PID, endpoint, symlink, unit, -or route alone never grants writes. Outside the explicit promotion fence, the -write-admitted process and routed generation must agree. Runtimes must bind and -publish staged health without opening canonical writable state; they open or -mutate that state only after the exact process-bound admission becomes current. +The state-write lease and route membership are distinct authorities. Fencing +the incumbent does not grant the candidate or change the route. Granting the +candidate permits that exact private process to write but does not route stable +traffic. Changing the route grants no write authority. The promotion +transaction clears its fence only after the independently observed write and +route authorities name the candidate. A PID, endpoint, symlink, or unit alone +grants neither authority. Promotion persists crash-resumable phases: planned, materialized, sealed, -candidate-started, staged, fenced, route-observed, write-admitted, active, -draining, and complete. A dead Idunn cannot leave a second writer. Its successor -resumes the exact sealed transaction or restores the prior admitted generation -through the same fence; it never infers completion from a symlink or eventual -health convergence. +activation-issued, candidate-started, activation-observed, warming, +incumbent-fenced, write-lease-granted, ready-observed, route-observed, +fence-cleared, draining, and complete. A dead Idunn cannot leave a second +writer. Its successor resumes the exact sealed transaction or restores the +prior admitted incarnation through the same fence; it never infers completion +from a symlink or eventual health convergence. An irreversible state cut fails closed after the write lease moves. A reversible -cut may restore the incumbent only through the same admission primitive and -only while its artifact and state contract remain valid. +cut may restore the incumbent only through the same process-write-lease +primitive and only while its artifact and state contract remain valid. ## Continuity and outage behavior -Continuity restarts the exact sealed release, binding, endpoint, and admission -identity. It does not fetch a branch, rebuild, rerun a migration, alter a unit, -or consume the deployment brake. An explicit lifecycle brake may suspend this -actuation without changing deployment authority. +Continuity restarts the exact sealed release, binding, endpoint, runtime +identity, and route membership. Every restart receives a new runtime-instance +activation and, for a state writer, a new process-bound lease only after the new +warming presence is observed. A lease from the dead process is never reused. A +continuity restart does not fetch a branch, rebuild, rerun a migration, alter +the launch contract, or consume the deployment brake. An explicit lifecycle +brake may suspend this actuation without changing deployment authority. Idunn starts and recovers from its own durable admitted state. Odin is the first managed semantic daemon, never an Idunn bootstrap dependency. Odin's initial admission is the sole graph-bootstrap exception: Idunn uses its root-admitted local binding, publishes Odin's expected projection into durable CultMesh state, -and waits for Odin's own signed presence before admitting dependents. +and waits for Odin's observed activation and signed presence before admitting +dependents. When Odin is unavailable, Idunn continues health observation and continuity for -already admitted generations and leaves current routes in place. It does not -begin a graph-changing transaction. An already sealed transaction may continue -only from its pinned graph and expected-generation snapshot; once a promotion -fence exists, safety recovery proceeds without waiting for Odin so the fleet is -not stranded with an ambiguous writer. New deployment, dependency substitution, -or scaling waits for semantic discovery. When Odin returns, disagreement is -correlated and exposed before graph-changing work resumes. +already admitted incarnations and leaves current routes in place. It does not +begin a graph-changing transaction, and a merely sealed candidate may not start +promotion. Only a transaction whose incumbent has already been fenced may +promote when its frozen evidence already contains the exact Odin Ready receipt +for this runtime instance and signed-presence digest. Otherwise it waits, rolls +back through the same fence when reversible, or fails closed; Idunn never +manufactures Ready locally. New deployment, dependency +substitution, promotion, or scaling waits for semantic discovery. When Odin +returns, disagreement is correlated and exposed before graph-changing work +resumes. Source selection is explicit per binding: a pinned object, the captured head of -an admitted ref, or a signed release authorization. Every policy freezes one -exact commit in the plan and proves origin, reachability, recipe digest, and any -minimum floor before build. “Whatever the branch points to when root runsâ€� is -not a selection policy. +an admitted ref, or a signed release authorization. The current deterministic +foundation retains the exact selected commit, tree, recipe blob, and Gitlink +facts, but shape validation is not proof of Git ancestry, signature authority, +or object custody. Those proofs belong to the narrow Idunn-owned source driver +that will produce these private facts. “Whatever the branch points to when root +runsâ€� is not a selection policy. ## Garden path @@ -269,15 +328,19 @@ deployment program. The first live cut is complete only when tests and host observations prove: - exact source → recipe → artifact → process provenance; +- an issued but driver-unobserved activation cannot establish Present, and a + prior runtime instance cannot replay presence after restart; - target requests cannot grant an omitted runner or host affordance; - old command-bearing records and old target scripts are unexecutable; -- a candidate cannot write or receive the stable route before admission; -- route and write admission name the same generation and process; +- a candidate cannot write before its process-bound lease or receive the stable + route before separate route admission; +- route membership and write leases remain separate records and name the same + incarnation and process before the promotion fence clears; - expected, present, and ready disagreement blocks promotion and remains observable; - replayed or stale signed health cannot satisfy present or ready; -- external operator-bound dependencies still require observed signed presence - and readiness; configuration supplies only expected state; +- external operator-bound dependencies still require their declared observed + health/readiness contract; configuration supplies only expected state; - a compatible shared Odin is reused and a second provider is not spawned; - Idunn death during every promotion phase resumes or fails closed without a second writer; @@ -286,6 +349,7 @@ The first live cut is complete only when tests and host observations prove: - same-release crash recovery works while the deployment brake is engaged; - the lifecycle brake can stop continuity without admitting a deployment; - Odin loss preserves current continuity/routes and blocks graph changes; -- the incumbent remains routed until the candidate is signed-staged; +- the incumbent remains routed until the candidate has signed `active` health + and is correlated Ready; - the old unit, release, state root, grant, and route cannot regain authority; - Ghostlight restart preserves the exact world journal and digest identity. diff --git a/docs/idunn.md b/docs/idunn.md index 002766c..4446a61 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -1,495 +1,166 @@ # Idunn -Idunn is Odin's keepalive organ. +Idunn is GameCult's independent deployment-admission and continuity control +plane. It decides which exact workload incarnation may run, write canonical +state, receive traffic, and be kept alive. It does not depend on Odin or any +managed workload in order to start or recover its own durable admitted state. -Odin is the all-seer: it accepts Verse discovery, schema catalogs, translation -routes, provider surfaces, and observation projections. Idunn keeps the daemon -swarm alive from that accepted map. It is not a second Odin, not a dashboard, -and not a heroic supervisor with secret service truth in its pockets. +Systemd, container runtimes, proxies, and a future Nomad or Kubernetes driver +provide generic execution and routing. Idunn owns the GameCult-specific +decision about which source, release, runtime identity, process write lease, and +route membership are authoritative. -Idunn keeps the apples: desired daemon presence, deployment freshness, boot -recovery, crash recovery, health freshness, deploy/restart intent, operator -escalation, and continuity witness state. +The detailed contract is +[`idunn-deployment-authority.md`](idunn-deployment-authority.md). -The current deployment and runtime-admission contract is -[`idunn-deployment-authority.md`](idunn-deployment-authority.md). That contract -owns target recipes, operator bindings, capability-graph admission, -expected/present/ready correlation, candidate promotion, route admission, and -the separation between deployment and continuity. This page describes the -broader keepalive body and its existing state surfaces. +## Authority map -## Authority Map +- **Owner:** Idunn selects exact source facts under root policy, compiles the + repository declaration against an operator binding, seals materialized + inputs and artifacts, admits one runtime incarnation, separately controls its + process-bound write lease and route membership, and preserves its continuity. +- **Inputs:** the exact raw repository declaration and operator-binding blobs; + private source-driver facts; artifact and external-input materialization + receipts; separate deployment and lifecycle brakes; Odin's observed semantic + topology when a graph-changing decision needs discovery; signed runtime + presence and health; Idunn's current observed-activation projection; and + observations from narrow execution and route drivers. +- **Outputs:** private compiled plans and sealed releases; a release-bound + sanitized Expected projection for Odin/CultMesh; one launch-scoped runtime + activation published only after driver observation; process write-lease and + route decisions; continuity actions; and explicit disagreement or failure + records. +- **Derived state:** dashboards, operator summaries, proxy displays, process + listings, and Odin correlation views are projections. They do not establish + deployment, write, route, presence, or readiness authority. +- **Forbidden writers:** repositories cannot grant host privileges, identities, + trust anchors, state roots, write leases, routes, placement, or rollout + policy. Odin cannot deploy, promote, route, or grant writes. Services cannot + configure themselves Present or Ready. Proxies and process managers cannot + select the admitted incarnation. Raw command strings are not deployment + authority. +- **Shared path:** `idunn up`, scheduled deployment, operator deployment, + restart, crash recovery, and host reboot recovery all use the same admitted + source-to-release identity. Continuity may restart only that identity; it + cannot fetch, rebuild, migrate, or silently change configuration. -- Owner: Idunn owns daemon lifecycle management from its root-admitted target - catalog: bring-up after host reboot, deployment freshness, crash - recovery, health watching, deploy/restart policy, and operator escalation. -- Inputs: its admitted target catalog, Odin's accepted service records when - Odin is available, provider advertisements, `.cc` - witnesses, advertised command boundaries, CultNet/RUDP health contracts, - freshness windows, operator policy, local service manager state, and explicit - deployment/debug witnesses that cannot satisfy daemon health. -- Outputs: typed keepalive observations, deployment requests/results, restart - requests/results, release targets, deployment artifacts, state migration - plans/results, rollout plans/results, denied-action records, operator alarms, - Bifrost operator-notification requests, and an Eve/CultUI keepalive surface. -- Derived state: dashboard cells, Bifrost receipts, Discord or owner-DM - lowerings, agent summaries, and Odin service projections are - notification-only views of Idunn-owned keepalive records. -- Forbidden writers: Idunn does not decide which Verses exist, invent provider - schemas, mutate provider dashboards, own identity/session grants, or hide - restarts behind Odin refresh logic. Individual daemons should not carry - independent crash-recovery loops once Idunn owns their lifecycle path; they - publish health, surfaces, state witnesses, and command boundaries instead. - Agents are also forbidden deploy writers: they configure Idunn release - targets, command boundaries, migration commands, and rollout policy, then let - Idunn actuate and witness deployment. They do not run deploy scripts by hand. - Odin is itself an Idunn target; Odin discovery is never a prerequisite for - starting Idunn or recovering Odin. -- Shared paths: manual operator deploy/restart, scheduled deploy/restart, - degraded-health repair, boot rehydration, and future remote worker recovery - must pass through the same Idunn command primitive. -- Consequence gate: Yggdrasil keeps `deployment-brake.cc` and its pre-created - sibling `deployment-brake.cc.lock` in `/var/lib/gamecult/idunn-authority`. - The directory is `root:idunn` `0750`, both files are `root:idunn` `0640`, and - Idunn receives only a systemd `ReadOnlyPaths` view. Idunn validates under a - shared lock before spawning the privileged boundary. The root actuator then - independently reopens and validates the signed brake while remaining the sole - shared-lock holder through one synchronous deploy transaction; the child does - not inherit the lock descriptor. The root-owned operator writer takes the - lock exclusive for engage/release. A bounded release admits the exact target, - rollout request, and source revision only when the transaction starts before - its signed deadline. Admission transfers completion of that transaction to - the actuator; expiry is not a retroactive timer veto. Every later transaction - must independently revalidate the brake. -- Deletion line: any keepalive loop inside Odin, Gjallar, Eve lowerers, or - renderer code must be cut or demoted to a probe that names Idunn as the - restart owner. +## Repository declaration -## Body +A deployable repository publishes one strict declaration, normally +`deployment/idunn/recipe.toml`. It may contain: -Idunn now shares Odin's Rust body: +- literal build, test, and package argv for named runner classes; +- generic pinned HTTPS inputs with SHA-256, runner, and relative destination; +- immutable artifact outputs, optionally pinned to an expected SHA-256; +- a constrained launch contract; +- one health-contract identity; +- an optional typed state contract with state schema generation and slots; +- provided capabilities, dependency kinds, startup requirements, capacity, and + conflicts. -- `crates/odin-core/src/idunn.rs` owns the keepalive decision engine. -- `crates/odin-core/src/documents.rs` publishes typed Idunn CultMesh records - beside Odin and Gjallar records. -- `crates/idunn-daemon` is the local keepalive actuator crate and now owns the - resident Starfire-local swarm scheduler. -- `src/Idunn/README.md` is the user-facing introduction for developers, - operators, and daemon authors. -- `scripts/start-idunn-local.ps1` is now a narrow bootstrap wrapper: it ensures - one `idunn.exe` process is alive, checks the shared keepalive store for - staleness, and lets Rust own the target catalog and per-target scheduling. -- `scripts/deploy-yggdrasil-source-app.ps1` and - `scripts/health-yggdrasil-source-app.ps1` are the generic Yggdrasil source - artifact lane. They fetch the declared upstream branch, package - `origin/main` with `git archive`, run any declared daemon-owned migration - script before the deploy script, run the existing ops-owned deploy/check - scripts on Yggdrasil, and stamp a remote - `gamecult.idunn.deployment_manifest.v1` only after the remote check passes. -- `scripts/idunn-deployment-targets.ps1` is the current swarm deployment target - catalog. Every known deployable target is either `enforced`, `blocked`, - `external-owned`, or `not-runtime` with an explicit reason. -- `scripts/health-idunn-swarm-deployment-coverage.ps1` is the coverage probe - that fails when the target catalog becomes incoherent. The local Idunn - launcher runs it as `idunn-swarm-deployment-coverage` so missing deploy - ownership becomes a watched operational fault. -- `scripts/notify-idunn-operator-alarm.ps1` is the local operator crossing: - Idunn invokes it only after raising an operator alarm, and it asks Bifrost to - publish a typed `gamecult.operator_dm_request.v1` CultMesh command document - instead of learning Discord delivery itself. -- `npm run idunn:build` builds the Rust daemon. -- `npm run idunn:start -- ...` still supports the narrow one-daemon path for - manual use; RUDP health ingress stays disabled unless `--rudp-health-bind` is - supplied. -- `npm run idunn:start -- --swarm-profile starfire-local --repo-root E:\Projects\Odin --execute` - runs the singular local swarm supervisor. -- `--swarm-profile yggdrasil-local` is the Linux host-local supervisor for - Epiphany, Heimdall, Repixelizer, StreamPixels, and VoidBot. Its only command boundary is the - root-owned `/usr/local/libexec/idunn-yggdrasil` actuator. Restart commands - target the local Compose stack. For deployment, the root boundary derives the - repository, ref, daemon id, and release-authority posture from its fixed - target allowlist, then binds the caller's request identifier and source to the - signed root-owned brake. A manifest must be a direct root-owned executable - regular file under `/srv/odin/deploy-manifests`; neither it nor any ancestor - may be group/world-writable, and symlink manifests are refused. It does not - inherit Starfire paths, SSH keys, ADB authority, or Windows process ownership. -- Release authority is explicit per target. Odin, Ghostlight, and Epiphany are - source-observed targets: Idunn derives the newest commit on each admitted ref - that changes build or runtime source, freezes that exact revision into its - typed deployment request, and carries it across the narrow root actuator. - Documentation, notes, state maps, ledgers, scratch state, and root Markdown - do not impersonate a new binary generation. The actuator independently proves - that the frozen commit belongs to the admitted ref, then verifies the exact - embedded commit and deployment witness after activation. Raw branch `HEAD` is - neither release authority nor a second opinion about the selected revision. -- Targets that still require a Bifrost receipt retain that separate gate; their - receipt fields cannot leak into a source-observed request. Bifrost does not - authorize Epiphany releases. Epiphany's admitted - `GameCult/Epiphany` `refs/heads/codex/epiphany-shakedown-live` ref remains a - temporary shakedown boundary; changing it or promoting it to `main` is a - separate source-governance act. +The recipe does not configure runtime health states. GameCult runtime health +uses the shared `warming`, `active`, `degraded`, and `failed` ontology. Present +and Ready are Odin correlations over independently signed runtime evidence, not +recipe fields. -The current typed records are: +Stateless services declare no state block. Their operator binding therefore has +no state root, state-transition policy, or process write-lease record. + +## Operator binding + +The root-admitted operator binding supplies the physical policy a repository +cannot grant itself: + +- source origin, admitted ref, selection policy, checkout, and Gitlink origins; +- pinned runner images, literal-program allowlists, resources, mounts, secrets, + and preconfigured network profiles; +- workload driver, identity, isolation, release root, and runtime root; +- runtime ID, expected signer identity, and trust-anchor store; +- optional state root and state-transition policy; +- an optional process write-lease record exactly when a state slot is + process-bound writable; its sibling `.lock` path is derived; +- optional route driver and stable/private endpoint binding; +- independent deployment and lifecycle brakes; +- one replica on one admitted node in the v1 foundation. + +Route membership is not a write lease. A write lease is not route membership. +Neither may be inferred from a PID, unit, socket, symlink, or health message. + +## Deterministic release foundation + +The compiled plan retains the exact raw declaration and binding blobs and +parses those exact bytes again during validation. Its content address therefore +covers the real inputs rather than caller-supplied digests beside mutable parsed +copies. + +Source-selection facts currently preserve the private driver output: the +exact commit, source tree, recipe blob, admitted ref, and Gitlink tree entries. +The contract does not claim that shape validation proves ancestry, signatures, +or object custody. Those proofs belong to the narrow source driver that will +produce the facts. + +A sealed release binds the plan ID to sorted full artifact receipts and sorted +full external-input materialization receipts. Only a sealed release validated +against its exact plan can derive Expected. Expected includes the plan and +release IDs, runtime and signer identities, health contract, node, route, +executable artifact digest, capabilities, selected dependencies, and the +optional canonical state-contract digest computed from the typed +`StateDeclaration` MessagePack encoding. + +Expected is stable release and topology intent. Each actuator launch receives a +separate tiny `idunn.runtime_activation.v1` containing the Expected digest and +one Idunn-issued opaque runtime-instance ID. Idunn publishes that activation as +current only after the workload driver observes the matching native process and +artifact. Present requires both that Idunn observation and the service's +independent signed presence; either document alone is insufficient. + +Managed dependency candidates are derived from validated Expected documents. +External operator bindings remain expected configuration only; they cannot +impersonate signed presence or readiness. + +## Promotion and continuity + +For a routed stateful singleton, Idunn starts the candidate privately, observes +the exact actuator launch, publishes its current activation, and observes its +signed warming presence. It then fences the incumbent writer, grants the +candidate its exact process-bound write lease, and waits for signed active +health plus Ready correlation. It changes and observes route membership only +afterward. Only after the proxy has adopted the candidate may Idunn clear the +promotion fence and drain the incumbent. + +The deployment brake gates changes to source, release, configuration, schema, +unit, or authority binding. The lifecycle brake separately gates restart of an +already-admitted incarnation. Neither target brake may gate Idunn itself or an +unrelated service. + +If Odin is unavailable, Idunn preserves existing routes and continuity for +already-admitted incarnations. It starts no graph-changing deployment or +promotion. A merely sealed candidate waits. An already-fenced transaction may +promote only when its frozen evidence already contains the exact Odin Ready +receipt for this runtime instance and presence digest; otherwise it waits, +rolls back through the same fence when reversible, or fails closed. + +## Operator surface + +The intended entry points are small: ```text -idunn.desired_daemon.v1 -idunn.daemon_health.v1 -idunn.signed_health_admission.v1 -idunn.keepalive_decision.v1 -idunn.deployment_request.v2 -idunn.current_deployment_request.v1 -idunn.deployment_result.v1 -idunn.release_target.v1 -idunn.deployment_artifact.v1 -idunn.state_migration_plan.v1 -idunn.state_migration_result.v1 -idunn.rollout_plan.v1 -idunn.rollout_result.v1 -idunn.restart_request.v1 -idunn.restart_result.v1 -idunn.operator_alarm.v1 -idunn.swarm_surgery_plan.v1 -idunn.daemon_surgery_plan.v1 -idunn.daemon_transport_profile.v1 -idunn.command_boundary.v1 -idunn.runtime_transport_check.v1 -idunn.rudp_health_ingress.v1 +idunn up ghostlight +idunn up profile:aetheria +idunn up profile:full-gamecult ``` -## Invariants +Idunn ensures one compatible Odin exists, publishes the desired fleet, resolves +typed capability dependencies, admits candidates, waits for independently +observed presence and readiness, and promotes routes only when the required +graph conditions hold. -- Odin remains the accepted owner of Verse and service discovery. -- Idunn owns continuity decisions after a daemon is known. -- Individual daemons own their work and their health publication, not their - surrounding lifecycle. They must be simple to kill and simple for Idunn to - bring back. -- Providers own their own command boundaries. Idunn requests deployment or - restart through advertised authority or a named local service manager adapter. -- Host reboot recovery, crash recovery, stale deployment recovery, - stale-health recovery, and manual operator deploy/restart must share the same - Idunn command primitive. -- A repair loop is not an owner. If a daemon becomes healthy only after a later - Odin refresh or manual click, Idunn's ownership path is still incomplete. -- Restart attempts must be witnessed: requested by whom, against which service, - through which command boundary, with what result and timestamp. -- Authenticated signed health is bounded current state: Idunn stores one exact - statement per daemon, keyed by daemon id, while the admission carries its - digest and monotonic lineage. Heartbeat publication must replace that trio - atomically; it must not append one store row per pulse. Startup compacts the - former digest-keyed history through a whole-snapshot compare/exchange before - scheduler workers start. -- The keepalive store is current operational authority with bounded diagnostic - history, not an event archive. Idunn retains the latest eight decisions, - alarms, automatic deployment/restart receipts, migration results, and - rollout results per daemon, plus the latest thirty-two operator lifecycle - commands. Exact current deployment heads, their request/result chain, active - health bindings, and recent command-linked receipts are pinned regardless of - age. Startup compacts legacy history atomically; later writes reapply the - same policy before the single-file store can exceed 1 MiB for long. -- Health command exit status is not daemon awareness. Every Idunn - target must declare a daemon-owned CultNet/RUDP health contract naming what - health publication should prove and what unmarked failure means. - `idunn.desired_daemon.v1` and - `idunn.daemon_health.v1` both record that contract so later readers can - distinguish process liveness, source deployment freshness, framebuffer - composition, telemetry capture, and catalog coherence without mistaking a - temporary product/deployment probe for the real protocol surface. - `idunn.daemon_health.v1` also records `publication_source` and `transport` so - daemon-published RUDP health can be verified as daemon-owned transport - evidence. -- `idunn.desired_daemon.v1` links to - `idunn.daemon_transport_profile.v1` and `idunn.command_boundary.v1` records. - The transport profile names the target transport - `cultnet.transport.rudp.v0`, the daemon-owned witness substrate, and the cut - line that keeps old probes demoted. The command boundary names restart, - deploy, health, and alarm authority separately so Idunn can actuate only the - commands it actually owns. -- The Starfire-local shell probes are deployment/debug witnesses, not daemon - truth. A daemon is Idunn-aware when it publishes its health, command boundary, - and transport profile as typed CultNet/CultMesh documents over - `cultnet.transport.rudp.v0`. Product/debug probes are xenos-boundary - diagnostics or deployment checks only. -- Rust now shares the canonical cross-runtime `cultnet.transport.rudp.v0` - substrate in `vendor/cultnet-rs`: CNR0 packets, sessions, channels, reliable - schema frames, and timeout/retry semantics matching the TypeScript/Python - CultLib implementations. This removes "Rust cannot speak RUDP" as a substrate - excuse. It does not make any daemon fully migrated until that daemon publishes - its health and command boundary through the RUDP path and Idunn consumes that - daemon-owned publication. -- Idunn publishes `idunn.runtime_transport_check.v1` at startup. The current - check sends a CultNet hello over loopback `cultnet.transport.rudp.v0` and - records whether the acknowledgement path works in Idunn's own Rust runtime. - This proves Idunn's local substrate, not fleet migration. -- Idunn opens a RUDP health ingress only when `--rudp-health-bind` is supplied - and then publishes `idunn.rudp_health_ingress.v1`. The Starfire local - supervisor binds `0.0.0.0:17870` explicitly so host-local and WireGuard - publishers can use the configured Idunn health endpoint. That ingress - accepts raw `idunn.daemon_health` CultNet document puts and the dedicated - signed Epiphany runtime-health schema on the `schema` channel. Generic health - is observation-only. Epiphany promotion requires the separately persisted - `idunn.signed_health_admission.v1`, an exact latest - `idunn.deployment_request.v2`, a pinned host signature, current matching - health, and a fresh active admission. Each one-shot publisher gets its own RUDP session from its - UDP source address and the session is discarded after a delivered health - frame. Windows UDP `ConnectionReset`/`ConnectionAborted` reports from closed - one-shot clients are nonfatal ingress noise, not a reason to kill the worker. - The ingress does not grant deploy/restart authority; it is the daemon-owned - health publication path Idunn consumes. -- During each target cycle, fresh daemon-published RUDP health is the health - owner. Idunn accepts it only when the daemon id, health contract, - `publication_source=daemon-published`, transport `cultnet.transport.rudp.v0`, - and `max_silence_seconds` freshness window all match. If any of those fail, - Idunn publishes a missing-publication observation on the daemon key and plans - from that typed absence instead of running a local health command. -- Idunn publishes the active swarm transport migration plan as - `idunn.swarm_surgery_plan.v1`. That record names the active profile, owner, - objective, current mechanism, invariants, ordered phases, current phase, next - target, cut line, and verification layer. It is the state-surface answer to - "what surgery is Idunn doing next?" and must be lowered by Nightwing/Gjallar - before any chat summary or dashboard prose claims ownership of the plan. -- Idunn publishes per-target transport migration plans as - `idunn.daemon_surgery_plan.v1` records in the keepalive store. Each daemon - plan names severity, status, owner, objective, current mechanism, intended - CultNet/RUDP authority, cut line, ordered steps, blockers, and update time. - These records are operational state, not documentation garnish: Nightwing, - Gjallar, Odin, and future Eve lowerings should inspect them when projecting - the daemon surgery queue. -- A stale deployment is not restartable liveness. If a target reports - `stale-deployment` without deploy authority, Idunn must alarm instead of - restarting the stale artifact. If a target reports `dependency-unavailable` - or `degraded`, Idunn must alarm instead of treating local deploy/restart as - the owner. -- Daemon-owned health is evaluated before release inspection. A missing Git - checkout, unreachable upstream, or unreadable deployment witness cannot mask - fresh authenticated health and cannot prevent an unhealthy admitted body - from reaching its continuity restart path. Release inspection may refine a - healthy body into `stale-deployment`; it does not own liveness. -- Restarting an already-admitted installed body is continuity physiology and - does not consume deployment-brake authority. Artifact, configuration, - schema, unit, and authority-binding changes remain deployments and must pass - the deployment brake. A separate lifecycle brake is required if operators - need to suspend continuity actuation itself. -- Yggdrasil's `--repo-root` is Idunn-owned state, not Odin's live release. - The hosted profile uses explicit build-repository and root-manifest paths; - Idunn startup and recovery therefore do not read `/srv/odin/current`. -- Idunn should fail closed when authority is unclear. Unknown ownership, - repeated restart failure, missing command authority, or degraded health that - needs a human becomes an operator alarm. -- Operator alarms use CultMesh to request Bifrost-owned operator notification. - Bifrost is the bridge for the crossing and records the receipt. VoidBot's - `voidbot.operator-dm` command `owner.dm.send` with payload schema - `gamecult.operator_dm_request.v1` is a demoted compatibility delivery - actuator, not the owner. The owner-DM command lives in Bifrost's Verse; any - still-VoidBot delivery path must be invoked by Bifrost or documented as - migration debt. Idunn must not learn Discord token handling, DM delivery - internals, or VoidBot-specific transport. -- In the current local bridge, `--operator-alarm-command` invokes - `notify-idunn-operator-alarm.cmd`, which forwards `IDUNN_ALARM_*` - environment variables to `E:\Projects\Bifrost\tools\operator-notification.mjs - publish-idunn-alarm`. Bifrost publishes the typed CultMesh command document; - Idunn only decides that an alarm exists. -- Muninn continuity on Raven is a remote keepalive path. The long-running Raven - `muninn.exe serve` process now carries its own Idunn RUDP identity and - publishes `muninn.cultnet-rudp-remote-telemetry-health` directly; the - `muninn.exe --health` command is an explicit diagnostic publisher, not a - health owner. Idunn still restarts Muninn's idle `serve` posture - when needed. Raven is the host/body; Muninn is the local telemetry Verse - assembler. Idunn must not activate screen/audio streams as part of keepalive. - Raven media activation is a typed Muninn capture command whose target is a - `cultmesh://` URI resolved through Odin. Idunn does not install or run - standalone Raven A/V activation scripts. -- Raven is an operator-consented shared machine. Idunn restart and health - actuators for Raven must be background-only and must not open visible - terminals or interactive windows on that host. The Raven Muninn restart path - uses a hidden WScript/PowerShell trampoline; replacing it with an interactive - console launch violates the ops contract. -- Yggdrasil Gjallar deployment freshness and aggregate composition health are - part of Idunn's ops role. The daemon publishes - `gjallar.cultnet-rudp-composition-health`; Idunn's Yggdrasil actuator admits - immutable releases through the Gjallar deploy manifest and restarts - `gjallar-yggdrasil.service`. Nightwing has no Gjallar deployment or restart authority. -- Swarm-wide deployment ownership means Idunn owns the target catalog and the - freshness contract for the repo swarm. For enforced deploy targets, the - catalog must name upstream remote/branch, rollout strategy, state migration - authority, and whether zero downtime is actually available. Idunn deploys the - desired upstream revision, not arbitrary local developer `HEAD`. It does not - mean Idunn invents deploy authority for every repo immediately. A target - without a safe noninteractive deploy command must remain `blocked` or - `external-owned` with the missing authority named until a wrapper can produce - a deployment manifest and route through Idunn's typed deployment - request/result path. -- State migration is daemon-owned. Idunn may run and witness a declared - migration command before deployment, but the daemon/repo owns schema meaning, - backups, and the migrator. If migration fails, Idunn records - `idunn.state_migration_result.v1`, stops the deployment, records a failed - rollout/deployment result, and alarms instead of trying to repair state - behind the daemon's back. -- Deploy and restart scripts are Idunn actuators, not agent tools. They refuse - to run unless Idunn invokes them with `IDUNN_ACTUATOR=1` and - `IDUNN_COMMAND_AUTHORITY=idunn-daemon`. Agent work is to update the target - catalog, release target, command boundary, migration plan, and daemon - publication surfaces so Idunn can run the shared command primitive. -- Provider-health signer rotation is a root authority transaction over one - existing named trust binding. `idunn-provision - rotate-daemon-health-trust-signer` changes only that binding's public signer, - derived identity, and advancing bind time through a validated CultCache CAS; - daemon, contract, runtime, release policy, and every other binding remain - unchanged. -- Manual lifecycle requests are typed Idunn command records, not direct script - execution. Operators publish `idunn.lifecycle_command.v1` with `idunn restart - --daemon ` or `idunn redeploy --daemon `. The running supervisor - claims pending records for its target catalog, writes the corresponding - `idunn.restart_request.v1` or `idunn.deployment_request.v2`, executes through - the same guarded actuator path used by health-driven decisions, and records - the matching result document. If a target lacks restart or deploy authority, - Idunn marks the lifecycle command rejected instead of inventing authority. -- Zero downtime is a declared rollout capability, not a slogan. If a daemon - lacks hot reload, blue/green routing, rolling instances, or another named - in-place swap mechanism, Idunn records `restart-required` and verifies the - restart path honestly. -- Yggdrasil Odin, Heimdall, Ghostlight, repixelizer, and StreamPixels are - enforced through Idunn's source-selected release lane and their existing - `gamecult-ops` actuators. Heimdall and Ghostlight publish exact deployed - revisions in `/srv/heimdall/deploy/deployment.env` and - `/srv/ghostlight/deploy/deployment.env`; documentation-only commits cannot - become desired executable generations. - Odin publishes `idunn.signed_daemon_health.v1` with its root-bound Ed25519 - identity. Its immutable release pins both the Odin commit and the exact - CultLib commit, then switches them through one `/srv/odin/current` link. - Ghostlight's Linux runtime and signed-health identity are admitted. Heimdall - publishes its own daemon health and private authentication command listener; - its HTTP and loopback listener probes remain deployment witnesses rather than - release or health authority. - Bifrost is explicitly blocked as of 2026-06-09: committed Bifrost `HEAD` - expects `UserAccounts.HeimdallAccountId`, while Yggdrasil's production - database lacks that column and EF reports no pending migration. Idunn must - not claim Bifrost deployment freshness until Bifrost owns that schema - migration path. +## Current implementation boundary -## Runtime Direction - -Idunn's current Rust runtime now has two postures: - -1. One-daemon manual mode for focused probes and explicit local testing. -2. One-process swarm mode for the Starfire-local continuity graph. - -The local swarm mode owns: - -1. the built-in Starfire-local target catalog; -2. a mandatory health contract per target; -3. a typed `idunn.swarm_surgery_plan.v1` record, so Idunn's active migration - order, next target, cut line, and verification layer are explicit state; -4. a typed `idunn.daemon_surgery_plan.v1` record per target, so transport debt - is visible in the same state surface as daemon health; -5. a typed `idunn.daemon_transport_profile.v1` and - `idunn.command_boundary.v1` record per target, so debug probes and local - commands cannot pretend to be daemon-owned CultNet/RUDP truth; -6. a startup `idunn.runtime_transport_check.v1` witness proving Idunn's own - Rust RUDP loopback path; -7. a local `idunn.rudp_health_ingress.v1` listener for daemon-owned RUDP - health publication; -8. one in-process schedule per target instead of one watchdog process per target; -9. shared typed keepalive records in one CultMesh store; -10. deploy/restart/alarm execution through the same Rust decision engine; -11. recovery of fast local targets like Odin without waiting behind slow remote - Yggdrasil checks. - -Current plan surface: `idunn.swarm_surgery_plan.v1` for profile -`starfire-local` treats the Muninn Rust lanes, Odin's local provider-health -lane, Stonks daemon health, Weksa daemon health, VoidBot stack health, and -Nightwing Gjallar framebuffer composition health as completed substrate cuts. -Muninn's long-running `serve` bodies now publish -`idunn.daemon_health` over RUDP themselves; Starfire publishes to local Idunn, -while Nightwing and Raven publish to the configured Idunn RUDP health endpoint -using their target daemon ids and health contracts. The Muninn restart and -health wrappers require explicit `-IdunnRudpHealth` or `IDUNN_RUDP_HEALTH`; -there is no baked Starfire LAN default. `muninn --health` is an explicit -diagnostic publisher for the same typed health document, not live ownership. -Odin -publishes `odin.cultnet-rudp-provider-health` after each provider refresh. -Stonks publishes `stonks.cultnet-rudp-market-health` after each serialized -market refresh. Weksa publishes `weksa.cultnet-rudp-provider-health` after each -serialized provider witness refresh. VoidBot publishes -`voidbot.cultnet-rudp-stack-health` after each GameCult Local Orchestrator -pulse. Gjallar publishes `gjallar.cultnet-rudp-composition-health` from the C# -Yggdrasil aggregate compositor. Mimir Eve dashboard publishes -`mimir.cultnet-rudp-provider-health` from the Nightwing systemd broker, and the -same process publishes `nightwing.cultnet-rudp-eve-dashboard-health` for the -Nightwing dashboard service itself. The Nightwing Eve browser reference now runs -as `Mimir.EveBrowserReference`, serving -the same static lowering and publishing -`nightwing.cultnet-rudp-browser-reference-health` from its own service process. -Live Idunn cycles accept these records as health truth; missing or stale -publication becomes typed missing-publication state. -Raven's Muninn scheduled-task repair remains a separate ops invariant: -`GameCult-Muninn` is the only live Muninn scheduled task. Health fails if -obsolete activation or video-proof tasks still exist, and the live Raven -`serve` process must carry its own -`--idunn-rudp-health`, `--idunn-daemon`, and `--idunn-health-contract` -arguments. - -Next: keep moving the remaining Yggdrasil deployments off product/deck checks and -continue runtime-by-runtime until debug probes are unable to masquerade as -daemon truth. -Heimdall has now crossed the first live transport line: the Yggdrasil runtime -publishes `heimdall.cultnet-rudp-provider-health`, writes a daemon-owned -boundary witness at `/srv/heimdall/cultcache/heimdall.service.cc`, and the -deploy lane ships the required `CultLib` snapshot beside the app artifact. -Heimdall still owes Odin ingestion of that boundary store plus the later -redacted auth-document witness export, but it is no longer waiting on basic -RUDP keepalive surgery. Weksa now publishes daemon-owned provider -advertisement, operator state, Eve surface, command boundary, and transport -profile records in its provider store, accepts MiMo VoiceDesign commands over -CultNet/RUDP command documents, and records deleted product command lanes as -non-authority. Odin local discovery can ingest those records. The Weksa start -and restart wrappers require explicit `-IdunnRudpHealth` or -`WEKSA_IDUNN_RUDP_HEALTH`; there is no localhost Idunn default. Stonks now -publishes daemon-owned provider advertisement, -market snapshot, Eve surface, command boundary, and transport profile records in -its CultCache store, and Odin local discovery can ingest those records; product -routes are not provider transport. StreamPixels now has a -service-owned CultCache boundary store at -`E:\Projects\StreamPixels\.streampixels-data\cultcache\streampixels.service.cc` -containing provider advertisement, command boundary, transport profile, and an -Idunn health summary, and Odin local discovery can ingest it. The live -Yggdrasil deployment now keeps -`/srv/streampixels/env/service.env` wired for -explicit `STREAMPIXELS_IDUNN_RUDP_HEALTH` with contract -`streampixels.cultnet-rudp-service-health`, publishes the boundary store at -`/srv/streampixels/app/.streampixels-data/cultcache/streampixels.service.cc`, -and has live Idunn acceptance proof for `yggdrasil-streampixels` through the -configured RUDP route. StreamPixels now owes only the final demotion of host -checks to deployment/debug witnesses once Odin consumes the typed store. -Repixelizer now has a daemon-owned Verse runtime witness in -`src/repixelizer/verse_state.py`: the live GUI runtime writes provider -advertisement, Eve surface state, runtime config, auth projection, queue/job -records, command boundary, transport profile, and `idunn.daemon_health` into -the configured `.cc` store, and publishes Idunn RUDP health when -`GC_ACCESS_IDUNN_RUDP_HEALTH` is configured. Repixelizer product web and host -supervisor checks are deployment/debug witnesses; -absent RUDP health is a visible missing-publication state, not fallback truth. -VoidBot, Mimir, and -the Nightwing Eve runtime services still owe provider advertisement and -command-boundary RUDP publication before their renderer surfaces can be derived -from typed state alone. Gjallar consumes Odin's accepted provider-state -snapshot over CultNet/RUDP; Odin no longer hosts a browser-deck lowering bridge -for it to consume. Raven Muninn task action repair is no longer queued: the live -target is the single `GameCult-Muninn` serve task, while obsolete activation and -video-proof tasks are removed and treated as health failures if they return. - -Vili's Node daemon now has an in-process Idunn RUDP publisher for -`vili.cultnet-rudp-animation-health`, and local smoke proof shows Idunn accepts -that record over `cultnet.transport.rudp.v0`. Vili also writes -`.vili\vili.service.cc` with daemon-owned provider advertisement, operator -state, Eve surface, command boundary, and transport profile records. Odin local -provider discovery can ingest that typed store, including the Vili command -boundary and transport profile. That is now live Raven proof as well: Odin's -`scripts\restart-vili.ps1` refreshes the Raven runtime from the authoritative -local Vili and CultLib files, reinstalls the hidden `GameCult\Vili` task with -`--idunn-rudp-health` supplied by explicit `-IdunnRudpHealth`, -`VILI_IDUNN_RUDP_HEALTH`, or `IDUNN_RUDP_HEALTH`, and restarts it. Live Idunn -accepts `vili.cultnet-rudp-animation-health` through the configured RUDP route. - -No ad hoc JSON manifest, product endpoint, port socket, or browser-deck bridge -may become the live state owner. Debug projections are fine when they name the -`.cc` record, CultNet document, or CultMesh publication behind them. +`crates/idunn-daemon/src/deployment.rs` and `deployment_plan.rs` implement the +deterministic declaration, binding, plan, release, and Expected foundation. +They do not yet drive Git, runners, systemd, proxies, CultCache, CultMesh, +brakes, runtime activations, write leases, or runtime health verification. +Runtime integration must consume these contracts; it must not recreate target +catalogs, raw deployment commands, root Git inspection, or a second admission +opinion. diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index 021dca7..87d0b0f 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit 021dca7f3261c9842019f0845b35022455ccf756 +Subproject commit 87d0b0f2c829ef2e7defa2bf774f1e3b9cc6a1d0 From 330e4cd75d81c2c784b69d256f1e53ea61842803 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 23:13:36 +0200 Subject: [PATCH 77/93] Rebuild Idunn around typed authority ports --- crates/idunn-daemon/src/control_plane.rs | 698 + crates/idunn-daemon/src/deployment.rs | 34 +- crates/idunn-daemon/src/deployment_plan.rs | 359 +- crates/idunn-daemon/src/drivers.rs | 2225 +++ crates/idunn-daemon/src/lib.rs | 2 + crates/idunn-daemon/src/main.rs | 13711 +------------------ vendor/cultnet-rs | 2 +- 7 files changed, 3148 insertions(+), 13883 deletions(-) create mode 100644 crates/idunn-daemon/src/control_plane.rs create mode 100644 crates/idunn-daemon/src/drivers.rs diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs new file mode 100644 index 0000000..6ca48f2 --- /dev/null +++ b/crates/idunn-daemon/src/control_plane.rs @@ -0,0 +1,698 @@ +use std::collections::BTreeMap; +use std::env; +use std::fs; +use std::path::{Path, PathBuf}; +use std::thread; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +use anyhow::{Context, Result, anyhow, bail, ensure}; +use cultcache_rs::{ + CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, +}; +use uuid::Uuid; + +use crate::deployment::OperatorBinding; +use crate::drivers::ProcessIdentity; + +const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v1"; + +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.deployment_command", + schema = "idunn.deployment_command.v1" +)] +struct DeploymentCommandRecord { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + command_id: String, + #[cultcache(key = 2)] + selector: String, + #[cultcache(key = 3)] + requested_by: String, + #[cultcache(key = 4)] + requested_at_unix_millis: u64, + #[cultcache(key = 5)] + state: String, + #[cultcache(key = 6)] + owner_instance_id: Option, + #[cultcache(key = 7)] + started_at_unix_millis: Option, + #[cultcache(key = 8)] + completed_at_unix_millis: Option, + #[cultcache(key = 9)] + detail: String, +} + +impl DeploymentCommandRecord { + fn validate(&self) -> Result<()> { + ensure!( + self.schema_version == DEPLOYMENT_COMMAND_SCHEMA, + "deployment command schema is unsupported" + ); + require_id(&self.command_id, "deployment command id")?; + require_selector(&self.selector)?; + require_value(&self.requested_by, "deployment requester")?; + ensure!( + self.requested_at_unix_millis > 0, + "deployment command has no request time" + ); + ensure!( + matches!( + self.state.as_str(), + "pending" | "running" | "succeeded" | "failed" + ), + "deployment command state is invalid" + ); + ensure!( + self.detail.len() <= 2048 && !self.detail.contains('\0'), + "deployment command detail is invalid" + ); + match self.state.as_str() { + "pending" => { + ensure!( + self.owner_instance_id.is_none() + && self.started_at_unix_millis.is_none() + && self.completed_at_unix_millis.is_none(), + "pending deployment command has execution state" + ); + } + "running" => { + ensure!( + self.owner_instance_id.is_some() + && self.started_at_unix_millis.is_some() + && self.completed_at_unix_millis.is_none(), + "running deployment command has partial ownership" + ); + } + "succeeded" | "failed" => { + ensure!( + self.owner_instance_id.is_some() + && self.started_at_unix_millis.is_some() + && self.completed_at_unix_millis.is_some(), + "terminal deployment command has partial receipt state" + ); + } + _ => unreachable!(), + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +struct RuntimeOptions { + state_store: PathBuf, + bindings_dir: PathBuf, + source_root: PathBuf, + staging_root: PathBuf, + topology_store: PathBuf, + odin_correlation_store: PathBuf, + odin_trust_anchor: PathBuf, + source_identity: Option, + poll_millis: u64, +} + +impl Default for RuntimeOptions { + fn default() -> Self { + Self { + state_store: PathBuf::from("/var/lib/gamecult/idunn/control.cc"), + bindings_dir: PathBuf::from("/etc/gamecult/idunn/bindings"), + source_root: PathBuf::from("/var/lib/gamecult/idunn/sources"), + staging_root: PathBuf::from("/var/lib/gamecult/idunn/staging"), + topology_store: PathBuf::from("/var/lib/gamecult/idunn/topology.cc"), + odin_correlation_store: PathBuf::from( + "/var/lib/gamecult/odin/idunn-runtime-topology.cc", + ), + odin_trust_anchor: PathBuf::from("/etc/gamecult/idunn/odin-topology-anchor.cc"), + source_identity: None, + poll_millis: 500, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Command { + Serve(RuntimeOptions), + Up { + selector: String, + requested_by: String, + state_store: PathBuf, + wait: bool, + timeout_seconds: u64, + }, + Status { + state_store: PathBuf, + command_id: Option, + }, +} + +pub fn run(args: impl Iterator) -> Result<()> { + match parse(args)? { + Command::Serve(options) => serve(options), + Command::Up { + selector, + requested_by, + state_store, + wait, + timeout_seconds, + } => submit( + &state_store, + &selector, + &requested_by, + wait, + timeout_seconds, + ), + Command::Status { + state_store, + command_id, + } => status(&state_store, command_id.as_deref()), + } +} + +fn parse(args: impl Iterator) -> Result { + let mut args = args.peekable(); + let command = args.next().ok_or_else(|| anyhow!(usage()))?; + match command.as_str() { + "serve" => parse_serve(args), + "up" => parse_up(args), + "status" => parse_status(args), + "--help" | "-h" | "help" => bail!(usage()), + _ => bail!("unknown Idunn command {command:?}\n\n{}", usage()), + } +} + +fn parse_serve(mut args: impl Iterator) -> Result { + let mut options = RuntimeOptions::default(); + let mut source_uid = None; + let mut source_gid = None; + while let Some(argument) = args.next() { + match argument.as_str() { + "--state-store" => options.state_store = path_value(&mut args, &argument)?, + "--bindings-dir" => options.bindings_dir = path_value(&mut args, &argument)?, + "--source-root" => options.source_root = path_value(&mut args, &argument)?, + "--staging-root" => options.staging_root = path_value(&mut args, &argument)?, + "--topology-store" => options.topology_store = path_value(&mut args, &argument)?, + "--odin-correlation-store" => { + options.odin_correlation_store = path_value(&mut args, &argument)? + } + "--odin-trust-anchor" => options.odin_trust_anchor = path_value(&mut args, &argument)?, + "--source-uid" => source_uid = Some(u32_value(&mut args, &argument)?), + "--source-gid" => source_gid = Some(u32_value(&mut args, &argument)?), + "--poll-millis" => options.poll_millis = u64_value(&mut args, &argument)?, + "--help" | "-h" => bail!(usage()), + _ => bail!("unknown Idunn serve option {argument:?}"), + } + } + ensure!(options.poll_millis > 0, "--poll-millis must be positive"); + options.source_identity = match (source_uid, source_gid) { + (Some(uid), Some(gid)) => Some(ProcessIdentity { uid, gid }), + (None, None) => None, + _ => bail!("--source-uid and --source-gid must be supplied together"), + }; + Ok(Command::Serve(options)) +} + +fn parse_up(mut args: impl Iterator) -> Result { + let selector = args + .next() + .ok_or_else(|| anyhow!("idunn up requires a service or profile selector"))?; + require_selector(&selector)?; + let mut state_store = RuntimeOptions::default().state_store; + let mut requested_by = env::var("SUDO_USER") + .or_else(|_| env::var("USER")) + .or_else(|_| env::var("USERNAME")) + .unwrap_or_else(|_| "operator".into()); + let mut wait = true; + let mut timeout_seconds = 1800; + while let Some(argument) = args.next() { + match argument.as_str() { + "--state-store" => state_store = path_value(&mut args, &argument)?, + "--requested-by" => requested_by = string_value(&mut args, &argument)?, + "--no-wait" => wait = false, + "--timeout-seconds" => timeout_seconds = u64_value(&mut args, &argument)?, + "--help" | "-h" => bail!(usage()), + _ => bail!("unknown Idunn up option {argument:?}"), + } + } + ensure!(timeout_seconds > 0, "--timeout-seconds must be positive"); + require_value(&requested_by, "deployment requester")?; + Ok(Command::Up { + selector, + requested_by, + state_store, + wait, + timeout_seconds, + }) +} + +fn parse_status(mut args: impl Iterator) -> Result { + let mut state_store = RuntimeOptions::default().state_store; + let mut command_id = None; + while let Some(argument) = args.next() { + match argument.as_str() { + "--state-store" => state_store = path_value(&mut args, &argument)?, + "--command" => command_id = Some(string_value(&mut args, &argument)?), + "--help" | "-h" => bail!(usage()), + _ => bail!("unknown Idunn status option {argument:?}"), + } + } + Ok(Command::Status { + state_store, + command_id, + }) +} + +fn serve(options: RuntimeOptions) -> Result<()> { + for path in [ + &options.state_store, + &options.topology_store, + &options.staging_root, + ] { + let directory = if path.extension().is_some() { + path.parent() + .context("configured Idunn path has no parent")? + } else { + path.as_path() + }; + fs::create_dir_all(directory) + .with_context(|| format!("creating Idunn directory {}", directory.display()))?; + } + let instance_id = format!("idunn-{}", Uuid::new_v4()); + loop { + if let Some(command) = claim_next(&options.state_store, &instance_id)? { + let result = execute(&options, &command); + complete(&options.state_store, &command, result)?; + } else { + thread::sleep(Duration::from_millis(options.poll_millis)); + } + } +} + +fn execute(options: &RuntimeOptions, command: &DeploymentCommandRecord) -> Result { + let bindings = load_bindings(&options.bindings_dir)?; + let targets = resolve_selector(&bindings, &command.selector)?; + ensure!( + !targets.is_empty(), + "deployment selector resolved to no targets" + ); + bail!( + "typed deployment drivers are not yet connected for {}", + targets.join(",") + ) +} + +fn load_bindings(directory: &Path) -> Result> { + let mut paths = fs::read_dir(directory) + .with_context(|| format!("reading Idunn binding directory {}", directory.display()))? + .map(|entry| entry.map(|entry| entry.path())) + .collect::>>()?; + paths.retain(|path| path.extension() == Some(std::ffi::OsStr::new("toml"))); + paths.sort(); + let mut bindings = BTreeMap::new(); + for path in paths { + let bytes = fs::read(&path) + .with_context(|| format!("reading operator binding {}", path.display()))?; + let text = std::str::from_utf8(&bytes) + .with_context(|| format!("operator binding {} is not UTF-8", path.display()))?; + let binding = OperatorBinding::parse(text) + .with_context(|| format!("validating operator binding {}", path.display()))?; + let target = binding.target.clone(); + ensure!( + bindings.insert(target.clone(), binding).is_none(), + "operator binding target {target} is duplicated" + ); + } + ensure!(!bindings.is_empty(), "Idunn binding directory is empty"); + Ok(bindings) +} + +fn resolve_selector( + bindings: &BTreeMap, + selector: &str, +) -> Result> { + let targets = if let Some(profile) = selector.strip_prefix("profile:") { + bindings + .values() + .filter(|binding| binding.profiles.contains(profile)) + .map(|binding| binding.target.clone()) + .collect::>() + } else { + ensure!( + bindings.contains_key(selector), + "unknown deployment target {selector}" + ); + vec![selector.to_owned()] + }; + ensure!( + !targets.is_empty(), + "unknown or empty deployment profile {selector}" + ); + Ok(targets) +} + +fn submit( + store_path: &Path, + selector: &str, + requested_by: &str, + wait: bool, + timeout_seconds: u64, +) -> Result<()> { + if let Some(parent) = store_path.parent() { + fs::create_dir_all(parent)?; + } + let now = now_millis()?; + let record = DeploymentCommandRecord { + schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), + command_id: format!("up-{}", Uuid::new_v4()), + selector: selector.into(), + requested_by: requested_by.into(), + requested_at_unix_millis: now, + state: "pending".into(), + owner_instance_id: None, + started_at_unix_millis: None, + completed_at_unix_millis: None, + detail: String::new(), + }; + record.validate()?; + let store = SingleFileMessagePackBackingStore::new(store_path); + let next = typed_envelope(&record, now)?; + if !store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentCommandRecord::TYPE.into(), + key: record.command_id.clone(), + current: None, + }], + &[next], + )? { + bail!("deployment command id collided") + } + println!("{}", record.command_id); + if !wait { + return Ok(()); + } + let deadline = now.saturating_add(timeout_seconds.saturating_mul(1000)); + loop { + let current = read_command(store_path, &record.command_id)? + .context("submitted deployment command disappeared")?; + match current.state.as_str() { + "succeeded" => { + println!("succeeded {}", current.detail); + return Ok(()); + } + "failed" => bail!("deployment failed: {}", current.detail), + _ if now_millis()? >= deadline => bail!("deployment command timed out"), + _ => thread::sleep(Duration::from_millis(250)), + } + } +} + +fn status(store_path: &Path, command_id: Option<&str>) -> Result<()> { + let mut commands = read_commands(store_path)?; + if let Some(command_id) = command_id { + commands.retain(|command| command.command_id == command_id); + ensure!(!commands.is_empty(), "deployment command is unknown"); + } + commands.sort_by_key(|command| command.requested_at_unix_millis); + for command in commands { + println!( + "{} {} {} {}", + command.command_id, command.selector, command.state, command.detail + ); + } + Ok(()) +} + +fn claim_next(store_path: &Path, instance_id: &str) -> Result> { + let store = SingleFileMessagePackBackingStore::new(store_path); + let mut candidates = read_command_envelopes(store_path)? + .into_iter() + .filter(|(_, record)| record.state == "pending") + .collect::>(); + candidates.sort_by_key(|(_, command)| command.requested_at_unix_millis); + for (current, mut command) in candidates { + let now = now_millis()?; + command.state = "running".into(); + command.owner_instance_id = Some(instance_id.into()); + command.started_at_unix_millis = Some(now); + command.validate()?; + if store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentCommandRecord::TYPE.into(), + key: command.command_id.clone(), + current: Some(current), + }], + &[typed_envelope(&command, now)?], + )? { + return Ok(Some(command)); + } + } + Ok(None) +} + +fn complete( + store_path: &Path, + running: &DeploymentCommandRecord, + result: Result, +) -> Result<()> { + let store = SingleFileMessagePackBackingStore::new(store_path); + let current = read_command_envelopes(store_path)? + .into_iter() + .find(|(_, command)| command.command_id == running.command_id) + .context("running deployment command disappeared")?; + ensure!( + current.1 == *running, + "deployment command changed while its owner was executing" + ); + let now = now_millis()?; + let mut terminal = running.clone(); + terminal.completed_at_unix_millis = Some(now); + match result { + Ok(detail) => { + terminal.state = "succeeded".into(); + terminal.detail = detail; + } + Err(error) => { + terminal.state = "failed".into(); + terminal.detail = truncate(&format!("{error:#}"), 2048); + } + } + terminal.validate()?; + ensure!( + store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentCommandRecord::TYPE.into(), + key: terminal.command_id.clone(), + current: Some(current.0), + }], + &[typed_envelope(&terminal, now)?], + )?, + "deployment command changed before its terminal receipt" + ); + Ok(()) +} + +fn read_commands(path: &Path) -> Result> { + Ok(read_command_envelopes(path)? + .into_iter() + .map(|(_, record)| record) + .collect()) +} + +fn read_command(path: &Path, command_id: &str) -> Result> { + Ok(read_commands(path)? + .into_iter() + .find(|command| command.command_id == command_id)) +} + +fn read_command_envelopes( + path: &Path, +) -> Result> { + if !path.exists() { + return Ok(Vec::new()); + } + SingleFileMessagePackBackingStore::new(path) + .pull_all_read_only_snapshot()? + .into_iter() + .map(|envelope| { + ensure!( + envelope.r#type == DeploymentCommandRecord::TYPE + && envelope.schema_id.as_deref() == Some(DEPLOYMENT_COMMAND_SCHEMA), + "Idunn control store contains a foreign document" + ); + let record: DeploymentCommandRecord = rmp_serde::from_slice(&envelope.payload)?; + ensure!( + rmp_serde::to_vec(&record)? == envelope.payload + && envelope.key == record.command_id, + "Idunn control store contains a noncanonical command" + ); + record.validate()?; + Ok((envelope, record)) + }) + .collect() +} + +fn typed_envelope(record: &DeploymentCommandRecord, millis: u64) -> Result { + record.validate()?; + let stored_at = chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) + .context("deployment command timestamp is out of range")? + .to_rfc3339(); + Ok(CultCacheEnvelope { + key: record.command_id.clone(), + r#type: DeploymentCommandRecord::TYPE.into(), + payload: rmp_serde::to_vec(record)?, + stored_at, + schema_id: Some(DEPLOYMENT_COMMAND_SCHEMA.into()), + }) +} + +fn now_millis() -> Result { + Ok(SystemTime::now() + .duration_since(UNIX_EPOCH) + .context("system clock predates Unix epoch")? + .as_millis() + .try_into()?) +} + +fn string_value(args: &mut impl Iterator, name: &str) -> Result { + args.next() + .ok_or_else(|| anyhow!("{name} requires a value")) +} + +fn path_value(args: &mut impl Iterator, name: &str) -> Result { + Ok(PathBuf::from(string_value(args, name)?)) +} + +fn u32_value(args: &mut impl Iterator, name: &str) -> Result { + string_value(args, name)? + .parse() + .with_context(|| format!("{name} requires a u32")) +} + +fn u64_value(args: &mut impl Iterator, name: &str) -> Result { + string_value(args, name)? + .parse() + .with_context(|| format!("{name} requires a u64")) +} + +fn require_selector(value: &str) -> Result<()> { + let value = value.strip_prefix("profile:").unwrap_or(value); + require_id(value, "deployment selector") +} + +fn require_id(value: &str, label: &str) -> Result<()> { + ensure!( + !value.is_empty() + && value.len() <= 256 + && value.bytes().all(|byte| { + byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/') + }), + "{label} is invalid" + ); + Ok(()) +} + +fn require_value(value: &str, label: &str) -> Result<()> { + ensure!( + !value.trim().is_empty() + && value == value.trim() + && value.len() <= 1024 + && !value.contains('\0'), + "{label} is invalid" + ); + Ok(()) +} + +fn truncate(value: &str, length: usize) -> String { + value.chars().take(length).collect() +} + +fn usage() -> &'static str { + "Idunn deployment and continuity control plane\n\n\ + idunn serve [runtime options]\n\ + idunn up [--state-store PATH] [--no-wait]\n\ + idunn status [--state-store PATH] [--command ID]\n\n\ + Recipes declare build, package, launch, health, state, and capability semantics.\n\ + Operator bindings select source, runner, workload, route, brakes, and placement." +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cli_has_no_raw_command_or_compiled_profile_authority() { + for arguments in [ + vec!["up", "ghostlight", "--deploy-command", "sh -c bad"], + vec!["serve", "--swarm-profile", "yggdrasil-local"], + vec!["serve", "--restart-command", "bad"], + vec!["validate-runtime-admission"], + ] { + assert!(parse(arguments.into_iter().map(str::to_owned)).is_err()); + } + } + + #[test] + fn garden_path_accepts_service_and_profile_selectors() { + for selector in ["ghostlight", "profile:aetheria", "profile:full-gamecult"] { + let parsed = + parse(["up", selector, "--no-wait"].into_iter().map(str::to_owned)).unwrap(); + let Command::Up { + selector: actual, .. + } = parsed + else { + panic!("expected up command") + }; + assert_eq!(actual, selector); + } + } + + #[test] + fn source_identity_is_explicit_and_atomic() { + let partial = parse( + ["serve", "--source-uid", "1001"] + .into_iter() + .map(str::to_owned), + ) + .unwrap_err() + .to_string(); + assert!(partial.contains("supplied together")); + let Command::Serve(options) = parse( + ["serve", "--source-uid", "1001", "--source-gid", "1002"] + .into_iter() + .map(str::to_owned), + ) + .unwrap() else { + panic!("expected serve") + }; + assert_eq!( + options.source_identity, + Some(ProcessIdentity { + uid: 1001, + gid: 1002 + }) + ); + } + + #[test] + fn deployment_command_receipt_is_positional_and_fail_closed() -> Result<()> { + let record = DeploymentCommandRecord { + schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), + command_id: "up-test".into(), + selector: "ghostlight".into(), + requested_by: "operator".into(), + requested_at_unix_millis: 100, + state: "pending".into(), + owner_instance_id: None, + started_at_unix_millis: None, + completed_at_unix_millis: None, + detail: String::new(), + }; + record.validate()?; + let payload = rmp_serde::to_vec(&record)?; + assert_eq!(payload[0], 0x9a); + let mut invalid = record; + invalid.state = "succeeded".into(); + assert!(invalid.validate().is_err()); + Ok(()) + } +} diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index 51e4245..6376d73 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize}; pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v1"; +pub const IDUNN_RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -598,6 +599,12 @@ impl TargetDeclaration { for name in &self.service.required_environment { require_environment_name(name)?; } + ensure!( + self.service + .required_environment + .contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), + "service does not require the standard Idunn runtime bundle" + ); for name in &self.service.optional_environment { require_environment_name(name)?; ensure!( @@ -827,6 +834,9 @@ impl OperatorBinding { for name in runner.environment.keys().chain(runner.secret_files.keys()) { require_environment_name(name)?; } + for value in runner.environment.values() { + require_value(value, "runner environment value")?; + } for path in runner.secret_files.values() { require_absolute_path(path, "runner secret file")?; } @@ -915,6 +925,9 @@ impl OperatorBinding { { require_environment_name(name)?; } + for value in self.workload.environment.values() { + require_value(value, "workload environment value")?; + } for name in self.workload.argument_bindings.keys() { require_id(name, "argument binding")?; } @@ -1109,13 +1122,25 @@ impl OperatorBinding { input.id ); } - let available_environment: BTreeSet<_> = self + ensure!( + !self + .workload + .environment + .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) + && !self + .workload + .secret_files + .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), + "operator binding cannot replace the standard Idunn runtime bundle" + ); + let mut available_environment: BTreeSet<_> = self .workload .environment .keys() .chain(self.workload.secret_files.keys()) .cloned() .collect(); + available_environment.insert(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into()); ensure!( declaration .service @@ -1257,7 +1282,10 @@ fn require_capability_contract(capability: &str, schema: &str, compatibility: &s fn require_value(value: &str, label: &str) -> Result<()> { ensure!(!value.trim().is_empty(), "{label} is empty"); - ensure!(!value.contains('\0'), "{label} contains NUL"); + ensure!( + !value.chars().any(char::is_control), + "{label} contains a control character" + ); ensure!(value.len() <= 4_096, "{label} is too long"); Ok(()) } @@ -1455,7 +1483,7 @@ arguments = [ ] transport = "http" route_required = true -required_environment = ["TEST_SERVICE_BIND"] +required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "TEST_SERVICE_BIND"] [service.health] contract = "test-service.cultnet-service-health" diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index a6415ab..e50db70 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -2,18 +2,22 @@ use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; use anyhow::{Context, Result, bail, ensure}; +pub use cultnet_rs::IdunnExpectedIncarnationRecord as ExpectedIncarnation; +use cultnet_rs::{ + GameCultRuntimeCapability, IDUNN_EXPECTED_INCARNATION_SCHEMA, IdunnExpectedDependency, + IdunnExpectedRoute, +}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use crate::deployment::{ CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, - ProvidedCapability, ServiceTransport, SourceSelectionPolicy, StateDeclaration, - TargetDeclaration, capability_compatible, + ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration, + capability_compatible, }; pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v1"; -pub const EXPECTED_INCARNATION_SCHEMA: &str = "idunn.expected_incarnation.v1"; pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -305,6 +309,47 @@ impl DependencySelection { } Ok(()) } + + fn expected_projection(&self) -> Result { + self.validate()?; + let ( + provider_id, + provider_authority, + provider_expected_projection_sha256, + provider_endpoint, + ) = match &self.provider { + None => (None, None, None, None), + Some(provider) => match &provider.authority { + ExpectedProviderAuthority::ManagedIncarnation { + expected_projection_sha256, + .. + } => ( + Some(provider.provider_id.clone()), + Some("managed-incarnation".into()), + Some(expected_projection_sha256.clone()), + provider.endpoint.clone(), + ), + ExpectedProviderAuthority::ExternalOperatorBinding { .. } => ( + Some(provider.provider_id.clone()), + Some("external-operator-binding".into()), + None, + provider.endpoint.clone(), + ), + }, + }; + Ok(IdunnExpectedDependency { + kind: dependency_kind_name(self.requirement.kind).into(), + capability: self.requirement.capability.clone(), + schema: self.requirement.schema.clone(), + compatibility: self.requirement.compatibility.clone(), + minimum_capacity: self.requirement.minimum_capacity, + startup: startup_order_name(self.requirement.startup).into(), + provider_id, + provider_authority, + provider_expected_projection_sha256, + provider_endpoint, + }) + } } pub fn select_dependencies( @@ -374,137 +419,10 @@ pub fn select_dependencies( .collect() } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ExpectedRoute { - pub route_id: String, - pub transport: ServiceTransport, - pub stable_endpoint: String, - pub candidate_endpoint: String, -} - -impl ExpectedRoute { - fn validate(&self) -> Result<()> { - require_token(&self.route_id, "expected route id")?; - require_value(&self.stable_endpoint, "expected stable endpoint")?; - require_value(&self.candidate_endpoint, "expected candidate endpoint")?; - match self.transport { - ServiceTransport::Http => ensure!( - self.stable_endpoint.starts_with("http://") - || self.stable_endpoint.starts_with("https://"), - "expected HTTP stable endpoint is not an HTTP URI" - ), - ServiceTransport::Tcp => ensure!( - self.stable_endpoint.starts_with("tcp://"), - "expected TCP stable endpoint is not a TCP URI" - ), - _ => bail!("routed expected incarnation has a non-routable transport"), - } - ensure!( - self.candidate_endpoint - .starts_with(&format!("{}://", endpoint_scheme(self.transport)?)), - "expected candidate endpoint transport disagrees" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ExpectedIncarnation { - pub schema: String, - pub plan_id: String, - pub sealed_release_id: String, - pub target: String, - pub incarnation_id: String, - pub runtime_id: String, - pub expected_signer_identity_id: String, - pub health_contract: String, - pub executable_artifact_sha256: String, - pub state_schema_generation: Option, - pub state_contract_sha256: Option, - pub write_lease_required: bool, - pub source_revision: String, - pub node: String, - pub route: Option, - pub capabilities: Vec, - pub dependencies: Vec, -} - -impl ExpectedIncarnation { - pub fn validate(&self) -> Result<()> { - ensure!( - self.schema == EXPECTED_INCARNATION_SCHEMA, - "unsupported expected-incarnation schema" - ); - require_sha256(&self.plan_id, "expected plan")?; - require_sha256(&self.sealed_release_id, "expected sealed release")?; - require_token(&self.target, "expected target")?; - require_token(&self.incarnation_id, "expected incarnation")?; - require_token(&self.runtime_id, "expected runtime id")?; - require_token( - &self.expected_signer_identity_id, - "expected signer identity id", - )?; - require_token(&self.health_contract, "expected health contract")?; - require_sha256( - &self.executable_artifact_sha256, - "expected executable artifact", - )?; - match (&self.state_schema_generation, &self.state_contract_sha256) { - (Some(generation), Some(contract)) => { - require_token(generation, "expected state schema generation")?; - require_sha256(contract, "expected state contract")?; - } - (None, None) => ensure!( - !self.write_lease_required, - "stateless expected incarnation requires a write lease" - ), - _ => bail!("expected state generation and contract digest disagree"), - } - require_sha1(&self.source_revision, "expected source revision")?; - require_token(&self.node, "expected node")?; - if let Some(route) = &self.route { - route.validate()?; - } - let mut capabilities = BTreeSet::new(); - for capability in &self.capabilities { - require_contract( - &capability.capability, - &capability.schema, - &capability.compatibility, - )?; - ensure!( - capability.capacity > 0, - "expected capability capacity is zero" - ); - ensure!( - capabilities.insert(( - capability.capability.as_str(), - capability.schema.as_str(), - capability.compatibility.as_str(), - )), - "expected capability is duplicated" - ); - } - for dependency in &self.dependencies { - dependency.validate()?; - } - Ok(()) - } - - pub fn canonical_sha256(&self) -> Result { - self.validate()?; - Ok(sha256_id( - &rmp_serde::to_vec(self).context("encoding managed expected projection")?, - )) - } -} - /// Private Idunn control-plane state. This contains host binding details and is -/// never an Odin/CultMesh projection. `ExpectedIncarnation` is the sanitized -/// topology projection derived only after a release validates against this -/// plan. +/// never an Odin/CultMesh projection. The shared CultNet Expected incarnation +/// is the sanitized topology projection derived only after a release validates +/// against this plan. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct CompiledDeploymentPlan { @@ -568,7 +486,7 @@ impl CompiledDeploymentPlan { Ok(()) } - fn parsed_inputs(&self) -> Result<(TargetDeclaration, OperatorBinding)> { + pub(crate) fn parsed_inputs(&self) -> Result<(TargetDeclaration, OperatorBinding)> { let recipe_text = std::str::from_utf8(&self.recipe_blob) .context("stored deployment recipe is not UTF-8")?; let binding_text = std::str::from_utf8(&self.binding_blob) @@ -754,17 +672,10 @@ impl SealedRelease { ) -> Result { self.validate_against(plan)?; let (declaration, binding) = plan.parsed_inputs()?; - let node = binding - .placement - .nodes - .iter() - .next() - .context("validated singleton binding has no node")? - .clone(); let route = match (&binding.route, plan.candidate_port) { - (Some(bound), Some(port)) => Some(ExpectedRoute { + (Some(bound), Some(port)) => Some(IdunnExpectedRoute { route_id: bound.route_id.clone(), - transport: declaration.service.transport, + transport: endpoint_scheme(declaration.service.transport)?.into(), stable_endpoint: bound.stable_endpoint.clone(), candidate_endpoint: format!( "{}://{}:{port}", @@ -775,26 +686,58 @@ impl SealedRelease { (None, None) => None, _ => bail!("plan route and candidate port disagree"), }; - let executable_artifact_sha256 = self + let artifact_sha256 = self .artifacts .iter() .find(|artifact| artifact.artifact_id == declaration.service.executable_artifact) .context("sealed release has no executable service artifact")? .sha256 .clone(); + let mut capabilities = declaration + .provides + .iter() + .map(|capability| GameCultRuntimeCapability { + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.capacity, + }) + .collect::>(); + capabilities.sort_by(|left, right| { + (&left.capability, &left.schema, &left.compatibility).cmp(&( + &right.capability, + &right.schema, + &right.compatibility, + )) + }); + let mut dependencies = plan + .dependencies + .iter() + .map(DependencySelection::expected_projection) + .collect::>>()?; + dependencies.sort_by(|left, right| { + (&left.capability, &left.schema, &left.compatibility).cmp(&( + &right.capability, + &right.schema, + &right.compatibility, + )) + }); let expected = ExpectedIncarnation { - schema: EXPECTED_INCARNATION_SCHEMA.into(), - plan_id: plan.plan_id.clone(), - sealed_release_id: self.sealed_release_id.clone(), + schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), target: declaration.target.clone(), + plan_id: plan.plan_id.clone(), incarnation_id: plan.incarnation_id.clone(), + sealed_release_id: self.sealed_release_id.clone(), + source_repository: canonical_repository_identity(&plan.source.origin)?, + source_revision: plan.source.revision.clone(), + recipe_sha256: plan.source.recipe_blob_sha256.clone(), runtime_id: binding.runtime_identity.runtime_id.clone(), expected_signer_identity_id: binding .runtime_identity .expected_signer_identity_id .clone(), health_contract: declaration.service.health.contract.clone(), - executable_artifact_sha256, + artifact_sha256, state_schema_generation: declaration .state .as_ref() @@ -805,11 +748,9 @@ impl SealedRelease { .map(canonical_state_contract_sha256) .transpose()?, write_lease_required: declaration.write_lease_required(), - source_revision: plan.source.revision.clone(), - node, route, - capabilities: declaration.provides.clone(), - dependencies: plan.dependencies.clone(), + capabilities, + dependencies, }; expected.validate()?; Ok(expected) @@ -910,6 +851,32 @@ fn endpoint_scheme(transport: ServiceTransport) -> Result<&'static str> { } } +fn dependency_kind_name(kind: DependencyKind) -> &'static str { + match kind { + DependencyKind::Bootstrap => "bootstrap", + DependencyKind::Required => "required", + DependencyKind::Optional => "optional", + DependencyKind::SharedInfrastructure => "shared-infrastructure", + DependencyKind::Private => "private", + DependencyKind::ExternalOperatorBinding => "external-operator-binding", + } +} + +fn startup_order_name(startup: StartupOrder) -> &'static str { + match startup { + StartupOrder::BeforePromotion => "before-promotion", + StartupOrder::BeforeStart => "before-start", + } +} + +fn canonical_repository_identity(origin: &str) -> Result { + let identity = origin + .strip_prefix("https://") + .and_then(|origin| origin.strip_suffix(".git")) + .context("validated Git origin is not a canonical HTTPS repository")?; + Ok(identity.to_owned()) +} + fn prefixed_sha256(raw_sha256: &str) -> String { format!("sha256-{raw_sha256}") } @@ -1015,7 +982,7 @@ executable = true executable_artifact = "daemon" transport = "http" route_required = true -required_environment = ["SERVICE_BIND"] +required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "SERVICE_BIND"] [service.health] contract = "service.health" @@ -1134,27 +1101,28 @@ nodes = ["yggdrasil"] fn odin() -> ExpectedIncarnation { ExpectedIncarnation { - schema: EXPECTED_INCARNATION_SCHEMA.into(), - plan_id: digest(b'a'), - sealed_release_id: digest(b'e'), + schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), target: "odin".into(), + plan_id: digest(b'a'), incarnation_id: "odin-incarnation-1".into(), + sealed_release_id: digest(b'e'), + source_repository: "github.com/GameCult/Odin".into(), + source_revision: "2222222222222222222222222222222222222222".into(), + recipe_sha256: digest(b'b'), runtime_id: "odin-yggdrasil".into(), expected_signer_identity_id: "odin-runtime-signer".into(), health_contract: "odin.cultnet-service-health".into(), - executable_artifact_sha256: digest(b'f'), + artifact_sha256: digest(b'f'), state_schema_generation: None, state_contract_sha256: None, write_lease_required: false, - source_revision: "2222222222222222222222222222222222222222".into(), - node: "yggdrasil".into(), - route: Some(ExpectedRoute { + route: Some(IdunnExpectedRoute { route_id: "odin-private".into(), - transport: ServiceTransport::Tcp, + transport: "tcp".into(), stable_endpoint: "tcp://10.77.0.1:17871".into(), candidate_endpoint: "tcp://127.0.0.1:17871".into(), }), - capabilities: vec![ProvidedCapability { + capabilities: vec![GameCultRuntimeCapability { capability: "odin.verse-rendezvous".into(), schema: "odin.verse-topology.v1".into(), compatibility: "v1".into(), @@ -1336,29 +1304,80 @@ nodes = ["yggdrasil"] "service-runtime-signer" ); assert_eq!(expected.health_contract, "service.health"); - assert_eq!(expected.executable_artifact_sha256, digest(b'c')); + assert_eq!(expected.artifact_sha256, digest(b'c')); + assert_eq!(expected.source_repository, "github.com/GameCult/Service"); + assert_eq!(expected.source_revision, plan.source.revision); + assert_eq!(expected.recipe_sha256, plan.source.recipe_blob_sha256); assert_eq!(expected.state_schema_generation.as_deref(), Some("v1")); assert!(expected.state_contract_sha256.is_some()); assert!(!expected.write_lease_required); - assert_eq!(expected.node, "yggdrasil"); assert_eq!( - expected.dependencies[0] - .provider - .as_ref() - .unwrap() - .provider_id, - "odin-yggdrasil" + expected.dependencies[0].provider_id.as_deref(), + Some("odin-yggdrasil") + ); + assert_eq!( + expected.dependencies[0].provider_authority.as_deref(), + Some("managed-incarnation") ); assert_eq!( expected.route.as_ref().unwrap().candidate_endpoint, "http://127.0.0.1:18001" ); + assert_eq!(expected.route.as_ref().unwrap().transport, "http"); assert!(expected.canonical_sha256().unwrap().starts_with("sha256-")); let mut invalid_release = release; invalid_release.artifacts[0].size_bytes += 1; assert!(invalid_release.expected_projection(&plan).is_err()); } + #[test] + fn expected_projection_sorts_shared_capability_and_dependency_claims() { + let recipe = RECIPE + .replace( + "[[provides]]\ncapability = \"service.runtime\"", + "[[provides]]\ncapability = \"zeta.runtime\"\nschema = \"zeta.runtime.v1\"\ncompatibility = \"v1\"\n\n[[provides]]\ncapability = \"service.runtime\"", + ) + .replace( + "[[dependencies]]\nkind = \"shared-infrastructure\"", + "[[dependencies]]\nkind = \"optional\"\ncapability = \"zeta.optional\"\nschema = \"zeta.optional.v1\"\ncompatibility = \"v1\"\n\n[[dependencies]]\nkind = \"shared-infrastructure\"", + ); + let plan = compile_deployment_plan( + recipe.as_bytes(), + BINDING.as_bytes(), + source(&recipe), + "service-incarnation-sorted", + Some(18002), + 111, + &[odin()], + ) + .unwrap(); + let release = SealedRelease::new( + &plan, + vec![artifact_receipt()], + vec![external_input_receipt()], + 120, + ) + .unwrap(); + let expected = release.expected_projection(&plan).unwrap(); + assert_eq!( + expected + .capabilities + .iter() + .map(|capability| capability.capability.as_str()) + .collect::>(), + ["service.runtime", "zeta.runtime"] + ); + assert_eq!( + expected + .dependencies + .iter() + .map(|dependency| dependency.capability.as_str()) + .collect::>(), + ["odin.verse-rendezvous", "zeta.optional"] + ); + expected.validate().unwrap(); + } + #[test] fn sealed_release_address_covers_artifacts_and_external_inputs() { let receipt = artifact_receipt(); diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs new file mode 100644 index 0000000..52c189c --- /dev/null +++ b/crates/idunn-daemon/src/drivers.rs @@ -0,0 +1,2225 @@ +use std::collections::BTreeMap; +use std::ffi::{OsStr, OsString}; +use std::fs; +use std::io::{ErrorKind, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output, Stdio}; +use std::thread; +use std::time::Duration; + +use anyhow::{Context, Result, bail, ensure}; +use cultcache_rs::{ + CacheBackingStore, CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, + SingleFileMessagePackBackingStore, +}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use uuid::Uuid; + +use crate::deployment::{ + ArtifactOutput, ArtifactSource, IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, LaunchArgument, + OperatorBinding, RouteBinding, RouteDriver, RunnerBinding, SourceSelectionPolicy, + TargetDeclaration, WorkloadNetwork, +}; +use crate::deployment_plan::{ + ArtifactReceipt, CompiledDeploymentPlan, ExternalInputMaterializationReceipt, GitlinkTreeFact, + SOURCE_SELECTION_FACTS_SCHEMA, SealedRelease, SourceSelection, SourceSelectionFacts, +}; +use cultnet_rs::{ + IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, + IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, + IdunnRuntimeActivationRecord, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, + OdinRuntimeTopologyCorrelationPurpose, OdinRuntimeTopologyCorrelationRecord, + OdinTopologyIdentity, ServiceIdentityProfile, ServiceIdentitySignature, + ServiceIdentityTrustAnchor, verify_service_identity_signature, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ProcessIdentity { + pub uid: u32, + pub gid: u32, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FrozenSource { + pub facts: SourceSelectionFacts, + pub recipe_bytes: Vec, + pub root: PathBuf, +} + +pub trait SourcePort { + fn select( + &self, + binding: &OperatorBinding, + selected_at_unix_millis: u64, + ) -> Result; +} + +pub trait RunnerPort { + fn materialize( + &self, + source: &FrozenSource, + plan: &CompiledDeploymentPlan, + staging_root: &Path, + sealed_at_unix_millis: u64, + ) -> Result; +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct MaterializedRelease { + pub release: SealedRelease, + pub root: PathBuf, + pub artifacts: Vec, + pub external_inputs: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct WorkloadObservation { + pub unit: String, + pub invocation_id: String, + pub main_pid: u32, + pub process_start_time: u64, + pub executable: PathBuf, + pub executable_sha256: String, + pub runtime_instance_id: String, +} + +pub trait WorkloadPort { + fn start( + &self, + plan: &CompiledDeploymentPlan, + release: &MaterializedRelease, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result; + + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + prior: &WorkloadObservation, + ) -> Result; + + fn stop(&self, observation: &WorkloadObservation) -> Result<()>; +} + +pub trait TopologyPort { + type ReadyReceipt: Clone; + + fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result; + fn publish_observed_activation( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + observation: &WorkloadObservation, + ) -> Result; + fn present( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result>; + fn ready( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + write_lease_sha256: Option<&str>, + ) -> Result>; +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct PresenceObservation { + pub correlation_sha256: String, + pub signed_presence_sha256: Option, + pub runtime_instance_id: Option, + pub present: bool, + pub state: Option, + pub write_lease_sha256: Option, + pub disagreements: Vec, +} + +pub trait WriteLeasePort { + fn fence(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()>; + fn grant(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; + fn revoke(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result<()>; + fn observe(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct RouteObservation { + pub route_id: String, + pub runtime_instance_id: String, + pub membership_sha256: String, +} + +pub trait RoutePort { + fn promote( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + ) -> Result; + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + observation: &RouteObservation, + ) -> Result; +} + +/// Fixed-argv Git source driver. It never interprets recipe text as a command +/// and never derives source policy from the target repository. The configured +/// identity performs every Git read and materialization. +pub struct GitSourceDriver { + pub source_root: PathBuf, + pub identity: Option, + pub git_program: PathBuf, +} + +impl GitSourceDriver { + pub fn new(source_root: impl Into, identity: Option) -> Self { + Self { + source_root: source_root.into(), + identity, + git_program: PathBuf::from("git"), + } + } + + fn git(&self, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let mut command = Command::new(&self.git_program); + command.args(args).stdin(Stdio::null()); + apply_identity(&mut command, self.identity)?; + let output = command.output().with_context(|| { + format!( + "starting fixed-argv Git driver {}", + self.git_program.display() + ) + })?; + if !output.status.success() { + bail!( + "Git source driver exited with {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + Ok(output) + } + + fn git_text(&self, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let output = self.git(args)?; + let value = String::from_utf8(output.stdout).context("Git emitted non-UTF-8 text")?; + Ok(value.trim().to_owned()) + } + + fn ensure_checkout(&self, binding: &OperatorBinding) -> Result<()> { + let checkout = &binding.repository.checkout; + if !checkout.exists() { + self.git([ + OsString::from("clone"), + OsString::from("--filter=blob:none"), + OsString::from("--no-checkout"), + OsString::from("--origin"), + OsString::from("origin"), + binding.repository.origin.clone().into(), + checkout.as_os_str().to_owned(), + ])?; + } + ensure!( + checkout.join(".git").exists(), + "source checkout is not a Git worktree" + ); + let actual_origin = self.git_text([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("remote"), + OsString::from("get-url"), + OsString::from("origin"), + ])?; + ensure!( + actual_origin == binding.repository.origin, + "source checkout origin differs from the operator binding" + ); + Ok(()) + } + + fn admitted_ref_name(binding: &OperatorBinding) -> String { + format!("refs/idunn/admitted/{}", binding.target) + } + + fn resolve_selected_revision( + &self, + binding: &OperatorBinding, + fetched_ref: &str, + ) -> Result<(String, SourceSelection)> { + let checkout = &binding.repository.checkout; + let (revision, selection) = match binding.repository.selection { + SourceSelectionPolicy::PinnedObject => ( + binding + .repository + .pinned_revision + .clone() + .context("pinned source binding lost its exact revision")?, + SourceSelection::PinnedObject, + ), + SourceSelectionPolicy::RefHead => ( + self.git_text([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from(format!("{fetched_ref}^{{commit}}")), + ])?, + SourceSelection::RefHead, + ), + SourceSelectionPolicy::SignedRelease => { + bail!("signed-release selection requires a release-authority source port") + } + }; + require_git_sha(&revision, "selected source revision")?; + self.git([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("merge-base"), + OsString::from("--is-ancestor"), + binding.repository.minimum_revision.clone().into(), + revision.clone().into(), + ]) + .context("selected source is below the operator minimum revision")?; + self.git([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("merge-base"), + OsString::from("--is-ancestor"), + revision.clone().into(), + fetched_ref.into(), + ]) + .context("selected source is outside the fetched admitted ref")?; + Ok((revision, selection)) + } + + fn gitlink_fact( + &self, + binding: &OperatorBinding, + revision: &str, + path: &Path, + ) -> Result { + let output = self.git_text([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("ls-tree"), + revision.into(), + OsString::from("--"), + path.as_os_str().to_owned(), + ])?; + let mut fields = output.split_whitespace(); + ensure!( + fields.next() == Some("160000"), + "bound Gitlink is not a commit tree entry" + ); + ensure!( + fields.next() == Some("commit"), + "bound Gitlink has the wrong object kind" + ); + let tree_revision = fields + .next() + .context("Gitlink tree entry has no revision")?; + require_git_sha(tree_revision, "Gitlink tree revision")?; + let origin = binding.repository.gitlinks[path].origin.clone(); + Ok(GitlinkTreeFact { + origin, + revision: tree_revision.to_owned(), + tree_entry_revision: tree_revision.to_owned(), + }) + } + + fn materialize_worktree( + &self, + binding: &OperatorBinding, + revision: &str, + gitlinks: &BTreeMap, + ) -> Result { + let target_root = self.source_root.join(&binding.target); + let root = target_root.join(format!("{}-{}", revision, Uuid::new_v4())); + self.git([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("worktree"), + OsString::from("add"), + OsString::from("--detach"), + root.as_os_str().to_owned(), + revision.into(), + ])?; + + for (path, fact) in gitlinks { + let destination = root.join(path); + ensure!( + destination.starts_with(&root), + "Gitlink escaped the frozen source root" + ); + if destination.exists() { + fs::remove_dir(&destination).with_context(|| { + format!( + "removing empty Gitlink placeholder {}", + destination.display() + ) + })?; + } + if let Some(parent) = destination.parent() { + ensure_source_directory_tree(&root, parent, self.identity)?; + } + self.git([ + OsString::from("clone"), + OsString::from("--filter=blob:none"), + OsString::from("--no-checkout"), + OsString::from("--origin"), + OsString::from("origin"), + fact.origin.clone().into(), + destination.as_os_str().to_owned(), + ])?; + self.git([ + OsString::from("-C"), + destination.as_os_str().to_owned(), + OsString::from("fetch"), + OsString::from("--no-tags"), + OsString::from("origin"), + fact.revision.clone().into(), + ])?; + self.git([ + OsString::from("-C"), + destination.as_os_str().to_owned(), + OsString::from("checkout"), + OsString::from("--detach"), + fact.revision.clone().into(), + ])?; + let actual = self.git_text([ + OsString::from("-C"), + destination.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from("HEAD"), + ])?; + ensure!( + actual == fact.revision, + "materialized Gitlink revision differs" + ); + } + Ok(root) + } +} + +impl SourcePort for GitSourceDriver { + fn select( + &self, + binding: &OperatorBinding, + selected_at_unix_millis: u64, + ) -> Result { + binding.validate()?; + #[cfg(unix)] + ensure!( + unsafe { libc::geteuid() } != 0 || self.identity.is_some(), + "root Idunn must configure an unprivileged source identity" + ); + ensure!( + selected_at_unix_millis > 0, + "source selection has no timestamp" + ); + ensure!( + binding.repository.checkout.starts_with(&self.source_root) + && binding.repository.checkout != self.source_root, + "repository checkout is outside Idunn's source authority root" + ); + ensure_source_directory(&self.source_root, self.identity)?; + ensure_source_directory_tree( + &self.source_root, + binding + .repository + .checkout + .parent() + .context("repository checkout has no parent directory")?, + self.identity, + )?; + ensure_source_directory(&self.source_root.join(&binding.target), self.identity)?; + self.ensure_checkout(binding)?; + let fetched_ref = Self::admitted_ref_name(binding); + self.git([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("fetch"), + OsString::from("--force"), + OsString::from("--no-tags"), + OsString::from("origin"), + OsString::from(format!( + "+{}:{fetched_ref}", + binding.repository.admitted_ref + )), + ])?; + let (revision, selection) = self.resolve_selected_revision(binding, &fetched_ref)?; + let source_tree = self.git_text([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from(format!("{revision}^{{tree}}")), + ])?; + require_git_sha(&source_tree, "selected source tree")?; + let recipe_spec = format!("{}:{}", revision, binding.repository.recipe_path.display()); + let recipe_bytes = self + .git([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("show"), + recipe_spec.into(), + ])? + .stdout; + ensure!( + !recipe_bytes.is_empty(), + "selected deployment recipe is empty" + ); + + let mut gitlinks = BTreeMap::new(); + for path in binding.repository.gitlinks.keys() { + gitlinks.insert(path.clone(), self.gitlink_fact(binding, &revision, path)?); + } + let root = self.materialize_worktree(binding, &revision, &gitlinks)?; + let materialized_recipe = fs::read(root.join(&binding.repository.recipe_path)) + .context("reading materialized deployment recipe")?; + ensure!( + materialized_recipe == recipe_bytes, + "materialized recipe differs from the selected Git object" + ); + let facts = SourceSelectionFacts { + schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), + origin: binding.repository.origin.clone(), + admitted_ref: binding.repository.admitted_ref.clone(), + revision, + source_tree, + recipe_path: binding.repository.recipe_path.clone(), + recipe_blob_sha256: sha256_id(&recipe_bytes), + gitlinks, + selection, + selected_at_unix_millis, + }; + facts.validate_against(binding)?; + Ok(FrozenSource { + facts, + recipe_bytes, + root, + }) + } +} + +/// Docker is only a runner substrate. Recipe argv stays an argv vector, the +/// operator binding selects the exact image/network/mount affordances, and the +/// driver returns complete materialization receipts rather than build truth by +/// convention. +pub struct DockerRunnerDriver { + pub docker_program: PathBuf, +} + +impl Default for DockerRunnerDriver { + fn default() -> Self { + Self { + docker_program: PathBuf::from("docker"), + } + } +} + +impl DockerRunnerDriver { + fn docker(&self, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let output = Command::new(&self.docker_program) + .args(args) + .stdin(Stdio::null()) + .output() + .with_context(|| format!("starting Docker runner {}", self.docker_program.display()))?; + if !output.status.success() { + bail!( + "Docker runner exited with {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + Ok(output) + } + + fn run_in_workspace( + &self, + runner: &RunnerBinding, + workspace: &Path, + working_directory: &Path, + argv: &[String], + source_stamp: (&str, &str), + ) -> Result<()> { + ensure!(!argv.is_empty(), "runner command is empty"); + ensure!( + runner.allowed_programs.contains(&argv[0]), + "runner program {} is not operator-bound", + argv[0] + ); + let mut args = self.base_run_args(runner, workspace)?; + args.extend([ + OsString::from("--workdir"), + OsString::from(format!( + "/workspace/{}", + normalized_relative(working_directory)? + )), + OsString::from("--env"), + OsString::from(format!("{}={}", source_stamp.0, source_stamp.1)), + ]); + for (name, value) in &runner.environment { + args.push(OsString::from("--env")); + args.push(OsString::from(format!("{name}={value}"))); + } + for (name, path) in &runner.secret_files { + args.push(OsString::from("--env")); + args.push(OsString::from(format!("{name}=/run/idunn/secrets/{name}"))); + args.push(OsString::from("--mount")); + args.push(bind_mount( + path, + &format!("/run/idunn/secrets/{name}"), + true, + )?); + } + args.push(runner.image.clone().into()); + args.extend(argv.iter().map(OsString::from)); + self.docker(args)?; + Ok(()) + } + + fn base_run_args(&self, runner: &RunnerBinding, workspace: &Path) -> Result> { + let mut args = vec![ + OsString::from("run"), + OsString::from("--rm"), + OsString::from("--network"), + OsString::from(runner.network_profile.as_deref().unwrap_or("none")), + OsString::from("--memory"), + OsString::from(format!("{}m", runner.memory_mebibytes)), + OsString::from("--cpus"), + OsString::from(format!( + "{:.2}", + f64::from(runner.cpu_quota_percent) / 100.0 + )), + OsString::from("--mount"), + bind_mount(workspace, "/workspace", false)?, + ]; + if let Some(cache_root) = &runner.cache_root { + fs::create_dir_all(cache_root) + .with_context(|| format!("creating runner cache {}", cache_root.display()))?; + args.push(OsString::from("--mount")); + args.push(bind_mount(cache_root, "/cache", false)?); + } + Ok(args) + } + + fn materialize_external_input( + &self, + declaration: &TargetDeclaration, + binding: &OperatorBinding, + workspaces: &BTreeMap, + input_id: &str, + ) -> Result { + let input = declaration + .external_inputs + .iter() + .find(|candidate| candidate.id == input_id) + .context("external input declaration disappeared")?; + let runner = &binding.runners[&input.runner]; + let workspace = &workspaces[&input.runner]; + let destination = workspace.join(&input.destination); + ensure!( + destination.starts_with(workspace), + "external input escaped its runner workspace" + ); + if let Some(parent) = destination.parent() { + fs::create_dir_all(parent)?; + } + let mut args = self.base_run_args(runner, workspace)?; + args.extend([ + runner.image.clone().into(), + OsString::from("curl"), + OsString::from("--fail"), + OsString::from("--location"), + OsString::from("--proto"), + OsString::from("=https"), + OsString::from("--output"), + OsString::from(format!( + "/workspace/{}", + normalized_relative(&input.destination)? + )), + input.url.clone().into(), + ]); + self.docker(args)?; + let bytes = fs::read(&destination).with_context(|| { + format!( + "reading materialized external input {}", + destination.display() + ) + })?; + ensure!(!bytes.is_empty(), "materialized external input is empty"); + let digest = raw_sha256(&bytes); + ensure!( + digest == input.sha256, + "materialized external input digest differs from its recipe pin" + ); + Ok(ExternalInputMaterializationReceipt { + input_id: input.id.clone(), + url: input.url.clone(), + sha256: format!("sha256-{digest}"), + runner: input.runner.clone(), + destination: input.destination.clone(), + size_bytes: bytes.len().try_into()?, + }) + } + + fn collect_artifact( + &self, + declaration: &TargetDeclaration, + source: &FrozenSource, + workspaces: &BTreeMap, + staging_root: &Path, + artifact_id: &str, + ) -> Result { + let artifact = declaration + .artifacts + .iter() + .find(|candidate| candidate.id == artifact_id) + .context("artifact declaration disappeared")?; + let source_path = match artifact.source_kind { + ArtifactSource::RunnerOutput => workspaces + .get( + artifact + .runner + .as_deref() + .context("runner artifact lost its runner")?, + ) + .context("runner workspace is absent")? + .join(&artifact.source), + ArtifactSource::WorktreeTree => source.root.join(&artifact.source), + }; + ensure!(source_path.exists(), "declared artifact output is absent"); + let destination = staging_root.join(&artifact.destination); + ensure!( + destination.starts_with(staging_root), + "artifact destination escaped its release staging root" + ); + copy_artifact(&source_path, &destination)?; + let (sha256, size_bytes) = digest_artifact(&destination)?; + if let Some(expected) = &artifact.expected_sha256 { + ensure!( + &sha256 == expected, + "artifact output differs from its recipe-pinned digest" + ); + } + Ok(ArtifactReceipt { + artifact_id: artifact.id.clone(), + destination: artifact.destination.clone(), + sha256: format!("sha256-{sha256}"), + size_bytes, + executable: artifact.executable, + }) + } +} + +impl RunnerPort for DockerRunnerDriver { + fn materialize( + &self, + source: &FrozenSource, + plan: &CompiledDeploymentPlan, + staging_root: &Path, + sealed_at_unix_millis: u64, + ) -> Result { + plan.validate()?; + ensure!( + source.facts == plan.source && source.recipe_bytes == plan.recipe_blob, + "runner source differs from the compiled plan" + ); + let (declaration, binding) = plan.parsed_inputs()?; + ensure!( + !staging_root.exists(), + "release staging root already exists" + ); + fs::create_dir_all(staging_root)?; + + let mut workspaces = BTreeMap::new(); + for runner_id in binding.runners.keys() { + let workspace = staging_root.join(format!(".runner-{runner_id}")); + copy_tree(&source.root, &workspace)?; + workspaces.insert(runner_id.clone(), workspace); + } + + let mut external_inputs = Vec::new(); + for input in &declaration.external_inputs { + external_inputs.push(self.materialize_external_input( + &declaration, + &binding, + &workspaces, + &input.id, + )?); + } + for step in &declaration.steps { + let runner = &binding.runners[&step.runner]; + let workspace = &workspaces[&step.runner]; + for required in &step.required_environment { + ensure!( + runner.environment.contains_key(required) + || runner.secret_files.contains_key(required), + "step {} lacks operator-bound environment {required}", + step.id + ); + } + self.run_in_workspace( + runner, + workspace, + &step.working_directory, + &step.argv, + ( + &declaration.source_stamp_environment, + &source.facts.revision, + ), + )?; + } + + let mut artifacts = Vec::new(); + for artifact in &declaration.artifacts { + artifacts.push(self.collect_artifact( + &declaration, + source, + &workspaces, + staging_root, + &artifact.id, + )?); + } + for workspace in workspaces.values() { + remove_tree_inside(staging_root, workspace)?; + } + let release = SealedRelease::new( + plan, + artifacts.clone(), + external_inputs.clone(), + sealed_at_unix_millis, + )?; + Ok(MaterializedRelease { + release, + root: staging_root.to_owned(), + artifacts, + external_inputs, + }) + } +} + +/// systemd owns process execution. This driver lowers one validated launch +/// contract into a transient unit and then proves the native process and +/// executable that systemd actually started. It does not decide admission, +/// readiness, write authority, or route membership. +pub struct SystemdTransientWorkloadDriver { + pub systemd_run_program: PathBuf, + pub systemctl_program: PathBuf, + pub proc_root: PathBuf, +} + +impl Default for SystemdTransientWorkloadDriver { + fn default() -> Self { + Self { + systemd_run_program: PathBuf::from("systemd-run"), + systemctl_program: PathBuf::from("systemctl"), + proc_root: PathBuf::from("/proc"), + } + } +} + +impl SystemdTransientWorkloadDriver { + fn command(&self, program: &Path, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let output = Command::new(program) + .args(args) + .stdin(Stdio::null()) + .output() + .with_context(|| format!("starting workload actuator {}", program.display()))?; + if !output.status.success() { + bail!( + "workload actuator {} exited with {}: {}", + program.display(), + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + Ok(output) + } + + fn unit_name(&self, prefix: &str, runtime_instance_id: &str) -> Result { + let suffix = runtime_instance_id + .strip_prefix("sha256-") + .context("runtime instance id has no sha256 prefix")?; + ensure!( + suffix.len() == 64 && suffix.bytes().all(|byte| byte.is_ascii_hexdigit()), + "runtime instance id is not a SHA-256 digest" + ); + Ok(format!("{prefix}-{}.service", &suffix[..16])) + } + + fn install_release( + &self, + plan: &CompiledDeploymentPlan, + release: &MaterializedRelease, + ) -> Result { + release.release.validate_against(plan)?; + let (_, binding) = plan.parsed_inputs()?; + let release_root = &binding.workload.release_root; + fs::create_dir_all(release_root) + .with_context(|| format!("creating release root {}", release_root.display()))?; + let installed = release_root.join(&release.release.sealed_release_id); + if !installed.exists() { + let temporary = release_root.join(format!( + ".install-{}-{}", + release.release.sealed_release_id, + Uuid::new_v4() + )); + copy_tree(&release.root, &temporary)?; + if let Err(error) = fs::rename(&temporary, &installed) { + let cleanup = remove_tree_inside(release_root, &temporary); + if installed.exists() && error.kind() == ErrorKind::AlreadyExists { + cleanup?; + } else { + cleanup?; + return Err(error).with_context(|| { + format!("installing sealed release {}", installed.display()) + }); + } + } + } + for artifact in &release.artifacts { + let path = installed.join(&artifact.destination); + let (sha256, size_bytes) = digest_artifact(&path)?; + ensure!( + format!("sha256-{sha256}") == artifact.sha256 && size_bytes == artifact.size_bytes, + "installed artifact {} differs from its sealed receipt", + artifact.artifact_id + ); + if artifact.executable { + set_executable_read_only(&path)?; + } + } + Ok(installed) + } + + fn prepare_runtime_bundle( + &self, + binding: &OperatorBinding, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result { + let bundle = binding + .workload + .runtime_root + .join(&activation.runtime_instance_id); + fs::create_dir_all(&bundle) + .with_context(|| format!("creating runtime bundle {}", bundle.display()))?; + write_immutable_record( + &bundle.join("expected.cc"), + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: expected.canonical_bytes()?, + stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + )?; + write_immutable_record( + &bundle.join("activation.cc"), + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: activation.canonical_bytes()?, + stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + )?; + Ok(bundle) + } + + fn show_unit(&self, unit: &str) -> Result>> { + let output = Command::new(&self.systemctl_program) + .args([ + OsString::from("show"), + OsString::from(unit), + OsString::from("--no-pager"), + OsString::from("--property=LoadState"), + OsString::from("--property=ActiveState"), + OsString::from("--property=SubState"), + OsString::from("--property=InvocationID"), + OsString::from("--property=MainPID"), + ]) + .stdin(Stdio::null()) + .output() + .with_context(|| format!("observing systemd unit {unit}"))?; + let text = + std::str::from_utf8(&output.stdout).context("systemd show output is not UTF-8")?; + let mut values = BTreeMap::new(); + for line in text.lines().filter(|line| !line.is_empty()) { + let (name, value) = line + .split_once('=') + .context("systemd show output is malformed")?; + ensure!( + values.insert(name.to_owned(), value.to_owned()).is_none(), + "systemd show property is duplicated" + ); + } + if values + .get("LoadState") + .is_some_and(|value| value == "not-found") + { + return Ok(None); + } + ensure!( + output.status.success(), + "systemd show failed for {unit}: {}", + String::from_utf8_lossy(&output.stderr).trim() + ); + Ok(Some(values)) + } + + fn observe_unit( + &self, + unit: &str, + expected_executable: &Path, + runtime_instance_id: &str, + ) -> Result { + let values = self + .show_unit(unit)? + .with_context(|| format!("systemd unit {unit} is absent"))?; + ensure!( + values + .get("ActiveState") + .is_some_and(|value| value == "active") + && values + .get("SubState") + .is_some_and(|value| value == "running"), + "systemd unit {unit} is not running" + ); + let invocation_id = values + .get("InvocationID") + .filter(|value| value.len() == 32 && value.bytes().all(|byte| byte.is_ascii_hexdigit())) + .context("systemd unit has no canonical invocation id")? + .clone(); + let main_pid: u32 = values + .get("MainPID") + .context("systemd unit has no MainPID")? + .parse() + .context("systemd MainPID is not a u32")?; + ensure!(main_pid > 0, "systemd unit has no live main process"); + let process_root = self.proc_root.join(main_pid.to_string()); + let executable = fs::read_link(process_root.join("exe")) + .with_context(|| format!("observing executable for process {main_pid}"))?; + ensure!( + fs::canonicalize(&executable)? == fs::canonicalize(expected_executable)?, + "systemd started an executable outside the sealed release" + ); + let executable_sha256 = sha256_id(&fs::read(&executable)?); + let process_start_time = linux_process_start_time(&process_root.join("stat"))?; + Ok(WorkloadObservation { + unit: unit.to_owned(), + invocation_id, + main_pid, + process_start_time, + executable, + executable_sha256, + runtime_instance_id: runtime_instance_id.to_owned(), + }) + } + + fn start_transient( + &self, + declaration: &TargetDeclaration, + binding: &OperatorBinding, + installed: &Path, + bundle: &Path, + unit: &str, + ) -> Result<()> { + ensure!( + declaration + .service + .required_environment + .contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), + "target does not declare the standard Idunn runtime bundle" + ); + ensure!( + !binding + .workload + .environment + .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) + && !binding + .workload + .secret_files + .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), + "operator binding attempts to replace the Idunn runtime bundle" + ); + let executable_artifact = + release_artifact(declaration, &declaration.service.executable_artifact)?; + let executable = installed.join(&executable_artifact.destination); + ensure!(executable.is_file(), "sealed service executable is absent"); + let mut args = vec![ + OsString::from("--no-block"), + OsString::from(format!("--unit={unit}")), + OsString::from("--property=Type=exec"), + OsString::from("--property=Restart=no"), + OsString::from("--property=KillMode=mixed"), + OsString::from("--property=NoNewPrivileges=yes"), + OsString::from("--property=PrivateTmp=yes"), + OsString::from("--property=ProtectSystem=strict"), + OsString::from("--property=ProtectHome=yes"), + OsString::from("--property=ProtectControlGroups=yes"), + OsString::from("--property=ProtectKernelModules=yes"), + OsString::from("--property=ProtectKernelTunables=yes"), + OsString::from("--property=RestrictSUIDSGID=yes"), + OsString::from("--property=LockPersonality=yes"), + OsString::from("--property=UMask=0027"), + OsString::from(format!("--property=User={}", binding.workload.user)), + OsString::from(format!("--property=Group={}", binding.workload.group)), + OsString::from(format!( + "--property=MemoryMax={}M", + binding.workload.memory_mebibytes + )), + OsString::from(format!( + "--property=CPUQuota={}%", + binding.workload.cpu_quota_percent + )), + OsString::from(format!("--working-directory={}", installed.display())), + OsString::from(format!("--property=ReadOnlyPaths={}", installed.display())), + OsString::from(format!("--property=ReadOnlyPaths={}", bundle.display())), + OsString::from(format!( + "--setenv={IDUNN_RUNTIME_BUNDLE_ENVIRONMENT}={}", + bundle.display() + )), + ]; + if binding.workload.network == WorkloadNetwork::None { + args.push(OsString::from("--property=PrivateNetwork=yes")); + } + if let Some(state_root) = &binding.workload.state_root { + args.push(OsString::from(format!( + "--property=ReadWritePaths={}", + state_root.display() + ))); + } + for path in &binding.workload.read_only_paths { + args.push(OsString::from(format!( + "--property=ReadOnlyPaths={}", + path.display() + ))); + } + for path in &binding.workload.read_write_paths { + args.push(OsString::from(format!( + "--property=ReadWritePaths={}", + path.display() + ))); + } + if binding.workload.devices.is_empty() { + args.push(OsString::from("--property=PrivateDevices=yes")); + } else { + args.push(OsString::from("--property=DevicePolicy=closed")); + for device in &binding.workload.devices { + args.push(OsString::from(format!( + "--property=DeviceAllow={} rw", + device.display() + ))); + } + } + if !binding.workload.capabilities.is_empty() { + let capabilities = binding + .workload + .capabilities + .iter() + .cloned() + .collect::>() + .join(" "); + args.push(OsString::from(format!( + "--property=CapabilityBoundingSet={capabilities}" + ))); + args.push(OsString::from(format!( + "--property=AmbientCapabilities={capabilities}" + ))); + } + for (name, value) in &binding.workload.environment { + args.push(OsString::from(format!("--setenv={name}={value}"))); + } + for (name, path) in &binding.workload.secret_files { + args.push(OsString::from(format!( + "--property=ReadOnlyPaths={}", + path.display() + ))); + args.push(OsString::from(format!( + "--setenv={name}={}", + path.display() + ))); + } + args.push(executable.into_os_string()); + for argument in &declaration.service.arguments { + args.push(match argument { + LaunchArgument::Literal { value } => value.into(), + LaunchArgument::Binding { name } => { + binding.workload.argument_bindings[name].clone().into() + } + }); + } + self.command(&self.systemd_run_program, args)?; + Ok(()) + } +} + +impl WorkloadPort for SystemdTransientWorkloadDriver { + fn start( + &self, + plan: &CompiledDeploymentPlan, + release: &MaterializedRelease, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result { + plan.validate()?; + release.release.validate_against(plan)?; + expected.validate()?; + activation.validate()?; + ensure!( + expected.plan_id == plan.plan_id + && expected.sealed_release_id == release.release.sealed_release_id + && activation.expected_projection_sha256 == expected.canonical_sha256()?, + "workload inputs do not describe one sealed incarnation" + ); + let (declaration, binding) = plan.parsed_inputs()?; + let installed = self.install_release(plan, release)?; + let executable_artifact = + release_artifact(&declaration, &declaration.service.executable_artifact)?; + let executable = installed.join(&executable_artifact.destination); + let unit = self.unit_name( + &binding.workload.unit_prefix, + &activation.runtime_instance_id, + )?; + let bundle = self.prepare_runtime_bundle(&binding, expected, activation)?; + if self.show_unit(&unit)?.is_some() { + let observation = + self.observe_unit(&unit, &executable, &activation.runtime_instance_id)?; + ensure!( + observation.executable_sha256 == expected.artifact_sha256, + "running workload executable differs from Expected" + ); + return Ok(observation); + } + self.start_transient(&declaration, &binding, &installed, &bundle, &unit)?; + let mut last_error = None; + for _ in 0..100 { + match self.observe_unit(&unit, &executable, &activation.runtime_instance_id) { + Ok(observation) => { + ensure!( + observation.executable_sha256 == expected.artifact_sha256, + "started workload executable differs from Expected" + ); + return Ok(observation); + } + Err(error) => last_error = Some(error), + } + thread::sleep(Duration::from_millis(50)); + } + Err(last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate"))) + } + + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + prior: &WorkloadObservation, + ) -> Result { + expected.validate()?; + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()? + && activation.runtime_instance_id == prior.runtime_instance_id, + "workload observation belongs to another activation" + ); + let observed = + self.observe_unit(&prior.unit, &prior.executable, &prior.runtime_instance_id)?; + ensure!( + observed == *prior && observed.executable_sha256 == expected.artifact_sha256, + "native workload identity changed after observation" + ); + Ok(observed) + } + + fn stop(&self, observation: &WorkloadObservation) -> Result<()> { + self.command( + &self.systemctl_program, + [OsString::from("stop"), OsString::from(&observation.unit)], + )?; + if let Some(values) = self.show_unit(&observation.unit)? { + ensure!( + values + .get("ActiveState") + .is_some_and(|state| state == "inactive" || state == "failed"), + "systemd unit remained active after stop" + ); + } + Ok(()) + } +} + +/// One dedicated CultCache file is the write-lease authority for one target. +/// Its sibling lock is shared by the target only around durable commit edges; +/// Idunn's compare-exchange therefore fences the old writer before replacing +/// the record. Route membership never enters this store. +pub struct CultCacheWriteLeaseDriver { + pub record_path: PathBuf, +} + +impl CultCacheWriteLeaseDriver { + fn current(&self) -> Result> { + if !self.record_path.exists() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(&self.record_path) + .pull_all_read_only_snapshot()?; + match entries.as_slice() { + [] => Ok(None), + [envelope] + if envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE + && envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) => + { + let lease = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + envelope.key == lease.target, + "write-lease key is not its target" + ); + Ok(Some((envelope.clone(), lease))) + } + _ => bail!("process write-lease store is foreign or ambiguous"), + } + } + + fn envelope(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { + Ok(CultCacheEnvelope { + key: lease.target.clone(), + r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), + payload: lease.canonical_bytes()?, + stored_at: rfc3339_millis(lease.issued_at_unix_millis)?, + schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), + }) + } +} + +impl WriteLeasePort for CultCacheWriteLeaseDriver { + fn fence(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()> { + expected.validate()?; + let Some((envelope, _)) = self.current()? else { + return Ok(()); + }; + let store = SingleFileMessagePackBackingStore::new(&self.record_path); + ensure!( + store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: envelope.r#type.clone(), + key: envelope.key.clone(), + current: Some(envelope), + }], + &[], + )?, + "process write lease changed while fencing the incumbent" + ); + Ok(()) + } + + fn grant(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { + lease.validate()?; + if let Some((_, current)) = self.current()? { + ensure!( + current == *lease, + "another process already owns the write lease" + ); + return lease.canonical_sha256(); + } + if let Some(parent) = self.record_path.parent() { + fs::create_dir_all(parent)?; + } + let envelope = self.envelope(lease)?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.record_path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), + key: lease.target.clone(), + current: None, + }], + &[envelope], + )?, + "process write-lease grant lost its empty-store compare-exchange" + ); + lease.canonical_sha256() + } + + fn revoke(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result<()> { + lease.validate()?; + let Some((envelope, current)) = self.current()? else { + return Ok(()); + }; + ensure!( + current == *lease, + "refusing to revoke another process write lease" + ); + ensure!( + SingleFileMessagePackBackingStore::new(&self.record_path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: envelope.r#type.clone(), + key: envelope.key.clone(), + current: Some(envelope), + }], + &[], + )?, + "process write lease changed while revoking it" + ); + Ok(()) + } + + fn observe(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { + lease.validate()?; + Ok(self + .current()? + .is_some_and(|(_, current)| current == *lease)) + } +} + +/// The local CultCache projection is Idunn's typed provider surface into +/// CultMesh. Odin writes a separate signed correlation store. Reading that +/// store never lets Idunn manufacture Present or Ready from its own config. +pub struct CultCacheTopologyDriver { + pub projection_store: PathBuf, + pub correlation_store: PathBuf, + pub odin_trust_anchor_store: PathBuf, +} + +impl CultCacheTopologyDriver { + fn anchor(&self) -> Result { + let entries = SingleFileMessagePackBackingStore::new(&self.odin_trust_anchor_store) + .pull_all_read_only_snapshot() + .with_context(|| { + format!( + "reading Odin topology trust anchor {}", + self.odin_trust_anchor_store.display() + ) + })?; + let [envelope] = entries.as_slice() else { + bail!("Odin topology trust-anchor store is missing or ambiguous") + }; + ensure!( + envelope.r#type == ::TRUST_ANCHOR_TYPE + && envelope.schema_id.as_deref() + == Some(::TRUST_ANCHOR_SCHEMA) + && envelope.key + == ::TRUST_ANCHOR_KEY, + "Odin topology trust anchor belongs to another signing profile" + ); + rmp_serde::from_slice(&envelope.payload).context("decoding Odin topology trust anchor") + } + + fn correlation( + &self, + expected: &IdunnExpectedIncarnationRecord, + current_write_lease_sha256: Option<&str>, + ) -> Result> { + if !self.correlation_store.exists() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(&self.correlation_store) + .pull_all_read_only_snapshot()?; + let mut matches = entries.iter().filter(|envelope| { + envelope.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE + && envelope.key == expected.target + }); + let Some(envelope) = matches.next() else { + return Ok(None); + }; + ensure!( + matches.next().is_none(), + "Odin topology correlation is ambiguous" + ); + ensure!( + envelope.schema_id.as_deref() == Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA), + "Odin topology correlation schema is foreign" + ); + let (receipt, unsigned) = + OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload( + &envelope.payload, + )?; + ensure!( + envelope.key == receipt.target, + "Odin correlation key is not its target" + ); + receipt.validate_against_expected(expected, current_write_lease_sha256)?; + let proof = ServiceIdentitySignature { + identity_id: receipt.signer_identity_id.clone(), + signature: receipt.signature.clone(), + }; + verify_service_identity_signature::< + OdinTopologyIdentity, + OdinRuntimeTopologyCorrelationPurpose, + >(&self.anchor()?, &unsigned, &proof)?; + Ok(Some(receipt)) + } +} + +impl TopologyPort for CultCacheTopologyDriver { + type ReadyReceipt = OdinRuntimeTopologyCorrelationRecord; + + fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result { + expected.validate()?; + upsert_record( + &self.projection_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: expected.canonical_bytes()?, + stored_at: chrono::Utc::now().to_rfc3339(), + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + )?; + expected.canonical_sha256() + } + + fn publish_observed_activation( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + observation: &WorkloadObservation, + ) -> Result { + expected.validate()?; + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()? + && activation.runtime_instance_id == observation.runtime_instance_id + && observation.executable_sha256 == expected.artifact_sha256, + "observed activation does not name the Expected native process" + ); + upsert_record( + &self.projection_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: activation.canonical_bytes()?, + stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + )?; + activation.canonical_sha256() + } + + fn present( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result> { + expected.validate()?; + activation.validate()?; + let Some(receipt) = self.correlation(expected, None)? else { + return Ok(None); + }; + let activation_sha256 = activation.canonical_sha256()?; + let exact_activation = receipt.current_activation_sha256.as_deref() + == Some(activation_sha256.as_str()) + && receipt.runtime_instance_id.as_deref() + == Some(activation.runtime_instance_id.as_str()); + let mut disagreements = receipt + .disagreements + .iter() + .map(|disagreement| disagreement.code.clone()) + .collect::>(); + if !exact_activation { + disagreements.push("idunn-current-activation-mismatch".into()); + } + disagreements.sort(); + disagreements.dedup(); + Ok(Some(PresenceObservation { + correlation_sha256: receipt.canonical_sha256()?, + signed_presence_sha256: receipt.signed_presence_sha256.clone(), + runtime_instance_id: receipt.runtime_instance_id.clone(), + present: receipt.present && exact_activation, + state: receipt.observed_presence_state.clone(), + write_lease_sha256: receipt.observed_write_lease_sha256.clone(), + disagreements, + })) + } + + fn ready( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + write_lease_sha256: Option<&str>, + ) -> Result> { + expected.validate()?; + activation.validate()?; + let Some(receipt) = self.correlation(expected, write_lease_sha256)? else { + return Ok(None); + }; + let activation_sha256 = activation.canonical_sha256()?; + let exact_activation = receipt.current_activation_sha256.as_deref() + == Some(activation_sha256.as_str()) + && receipt.runtime_instance_id.as_deref() + == Some(activation.runtime_instance_id.as_str()); + Ok((receipt.ready && exact_activation).then_some(receipt)) + } +} + +/// nginx owns proxy mechanics. Idunn supplies one exact backend membership, +/// validates the complete nginx configuration, reloads it, and then observes +/// that nginx's loaded configuration contains those exact bytes. +pub struct NginxRouteDriver { + pub binding: RouteBinding, + pub nginx_program: PathBuf, + pub systemctl_program: PathBuf, +} + +impl NginxRouteDriver { + pub fn new(binding: RouteBinding) -> Self { + Self { + binding, + nginx_program: PathBuf::from("nginx"), + systemctl_program: PathBuf::from("systemctl"), + } + } + + fn command(&self, program: &Path, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let output = Command::new(program) + .args(args) + .stdin(Stdio::null()) + .output() + .with_context(|| format!("starting route actuator {}", program.display()))?; + if !output.status.success() { + bail!( + "route actuator {} exited with {}: {}", + program.display(), + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + Ok(output) + } + + fn render( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + ) -> Result> { + expected.validate()?; + let route = expected + .route + .as_ref() + .context("expected incarnation has no route")?; + ensure!( + route.route_id == self.binding.route_id, + "route driver binding differs from Expected" + ); + let (candidate_host, candidate_port) = endpoint_host_port( + &route.candidate_endpoint, + match self.binding.driver { + RouteDriver::NginxHttp => "http://", + RouteDriver::NginxStreamTcp => "tcp://", + }, + )?; + ensure!( + candidate_host == self.binding.private_host + && (self.binding.private_port_start..=self.binding.private_port_end) + .contains(&candidate_port), + "Expected candidate endpoint is outside the route binding" + ); + let upstream = nginx_identifier(&self.binding.route_id)?; + let rendered = match self.binding.driver { + RouteDriver::NginxHttp => format!( + "# Idunn runtime {runtime_instance_id}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n keepalive 16;\n}}\n" + ), + RouteDriver::NginxStreamTcp => { + let (stable_host, stable_port) = + endpoint_host_port(&route.stable_endpoint, "tcp://")?; + format!( + "# Idunn runtime {runtime_instance_id}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n" + ) + } + }; + Ok(rendered.into_bytes()) + } + + fn loaded_contains(&self, rendered: &[u8]) -> Result { + let output = self.command(&self.nginx_program, [OsString::from("-T")])?; + let mut loaded = output.stdout; + loaded.extend_from_slice(&output.stderr); + Ok(loaded + .windows(rendered.len()) + .any(|window| window == rendered)) + } + + fn reload(&self) -> Result<()> { + self.command(&self.nginx_program, [OsString::from("-t")])?; + self.command( + &self.systemctl_program, + [ + OsString::from("reload"), + OsString::from(&self.binding.reload_unit), + ], + )?; + Ok(()) + } + + fn restore(&self, prior: Option<&[u8]>) -> Result<()> { + match prior { + Some(bytes) => atomic_replace(&self.binding.config_path, bytes)?, + None => match fs::remove_file(&self.binding.config_path) { + Ok(()) => {} + Err(error) if error.kind() == ErrorKind::NotFound => {} + Err(error) => return Err(error).context("removing unadmitted route fragment"), + }, + } + self.reload() + } + + fn fail_after_rollback( + &self, + prior: Option<&[u8]>, + failure: anyhow::Error, + context: &str, + ) -> Result<()> { + match self.restore(prior) { + Ok(()) => Err(failure).context(context.to_owned()), + Err(rollback) => Err(failure).context(format!( + "{context}; route rollback also failed: {rollback:#}" + )), + } + } +} + +impl RoutePort for NginxRouteDriver { + fn promote( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + ) -> Result { + let rendered = self.render(expected, runtime_instance_id)?; + let prior = match fs::read(&self.binding.config_path) { + Ok(bytes) => Some(bytes), + Err(error) if error.kind() == ErrorKind::NotFound => None, + Err(error) => return Err(error).context("reading current route fragment"), + }; + atomic_replace(&self.binding.config_path, &rendered)?; + if let Err(error) = self.reload() { + self.fail_after_rollback( + prior.as_deref(), + error, + "candidate route validation or reload failed", + )?; + } + match self.loaded_contains(&rendered) { + Ok(true) => {} + Ok(false) => self.fail_after_rollback( + prior.as_deref(), + anyhow::anyhow!("nginx loaded configuration omitted the admitted membership"), + "candidate route observation failed", + )?, + Err(error) => self.fail_after_rollback( + prior.as_deref(), + error, + "candidate route observation failed", + )?, + } + Ok(RouteObservation { + route_id: self.binding.route_id.clone(), + runtime_instance_id: runtime_instance_id.to_owned(), + membership_sha256: sha256_id(&rendered), + }) + } + + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + observation: &RouteObservation, + ) -> Result { + let rendered = self.render(expected, &observation.runtime_instance_id)?; + ensure!( + observation.route_id == self.binding.route_id + && observation.membership_sha256 == sha256_id(&rendered), + "route observation does not describe the expected membership" + ); + let current = match fs::read(&self.binding.config_path) { + Ok(bytes) => bytes, + Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false), + Err(error) => return Err(error).context("reading route observation"), + }; + Ok(current == rendered && self.loaded_contains(&rendered)?) + } +} + +fn release_artifact<'a>( + declaration: &'a TargetDeclaration, + artifact_id: &str, +) -> Result<&'a ArtifactOutput> { + declaration + .artifacts + .iter() + .find(|artifact| artifact.id == artifact_id) + .with_context(|| format!("target declares no artifact {artifact_id}")) +} + +fn write_immutable_record(path: &Path, envelope: CultCacheEnvelope) -> Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let mut store = SingleFileMessagePackBackingStore::new(path); + match store.pull_all_read_only_snapshot()?.as_slice() { + [] => store.push(&envelope), + [current] if current == &envelope => Ok(()), + _ => bail!( + "immutable runtime document {} already differs", + path.display() + ), + } +} + +fn upsert_record(path: &Path, replacement: CultCacheEnvelope) -> Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let store = SingleFileMessagePackBackingStore::new(path); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let mut matches = entries + .iter() + .filter(|entry| entry.r#type == replacement.r#type && entry.key == replacement.key); + let current = matches.next().cloned(); + ensure!( + matches.next().is_none(), + "CultCache projection identity is ambiguous" + ); + if current.as_ref() == Some(&replacement) { + return Ok(()); + } + if store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: replacement.r#type.clone(), + key: replacement.key.clone(), + current, + }], + std::slice::from_ref(&replacement), + )? { + return Ok(()); + } + } + bail!("CultCache projection changed too often to publish") +} + +fn rfc3339_millis(millis: u64) -> Result { + Ok( + chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) + .context("runtime timestamp is out of range")? + .to_rfc3339(), + ) +} + +#[cfg(unix)] +fn set_executable_read_only(path: &Path) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + ensure!(path.is_file(), "executable artifact is not a regular file"); + fs::set_permissions(path, fs::Permissions::from_mode(0o555))?; + Ok(()) +} + +#[cfg(not(unix))] +fn set_executable_read_only(_path: &Path) -> Result<()> { + bail!("systemd workload installation requires Unix permissions") +} + +fn linux_process_start_time(stat_path: &Path) -> Result { + let stat = fs::read_to_string(stat_path) + .with_context(|| format!("reading process stat {}", stat_path.display()))?; + let close = stat + .rfind(')') + .context("process stat has no command terminator")?; + let fields = stat[close + 1..].split_whitespace().collect::>(); + fields + .get(19) + .context("process stat has no start-time field")? + .parse() + .context("process start-time field is not a u64") +} + +fn endpoint_host_port(endpoint: &str, scheme: &str) -> Result<(String, u16)> { + let authority = endpoint + .strip_prefix(scheme) + .with_context(|| format!("endpoint does not use {scheme}"))?; + ensure!( + !authority + .chars() + .any(|character| matches!(character, '/' | '?' | '#' | '@')) + && !authority.starts_with('['), + "route endpoint is not a plain host and port" + ); + let (host, port) = authority + .rsplit_once(':') + .context("route endpoint has no port")?; + ensure!( + !host.is_empty() + && host + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-')), + "route endpoint host is invalid" + ); + let port: u16 = port.parse().context("route endpoint port is not a u16")?; + ensure!(port > 0, "route endpoint port is zero"); + Ok((host.to_owned(), port)) +} + +fn nginx_identifier(route_id: &str) -> Result { + ensure!(!route_id.is_empty(), "nginx route id is empty"); + let mut identifier = String::from("idunn_"); + for byte in route_id.bytes() { + match byte { + b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => { + identifier.push(char::from(byte)); + } + b'-' | b'.' => identifier.push('_'), + _ => bail!("route id cannot lower to an nginx identifier"), + } + } + Ok(identifier) +} + +fn atomic_replace(path: &Path, bytes: &[u8]) -> Result<()> { + let parent = path.parent().context("route path has no parent")?; + fs::create_dir_all(parent)?; + let file_name = path + .file_name() + .context("route path has no file name")? + .to_string_lossy(); + let temporary = parent.join(format!(".{file_name}.idunn-{}", Uuid::new_v4())); + let mut file = fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&temporary) + .with_context(|| format!("creating route stage {}", temporary.display()))?; + file.write_all(bytes)?; + file.sync_all()?; + drop(file); + if let Err(error) = fs::rename(&temporary, path) { + let _ = fs::remove_file(&temporary); + return Err(error).with_context(|| format!("publishing route {}", path.display())); + } + Ok(()) +} + +fn bind_mount(source: &Path, destination: &str, read_only: bool) -> Result { + ensure!(source.is_absolute(), "Docker bind source is not absolute"); + let text = source.to_str().context("Docker bind source is not UTF-8")?; + ensure!( + !text + .chars() + .any(|character| matches!(character, ',' | '\n' | '\r')), + "Docker bind source contains a forbidden mount character" + ); + let read_only = if read_only { ",readonly" } else { "" }; + Ok(OsString::from(format!( + "type=bind,src={text},dst={destination}{read_only}" + ))) +} + +fn normalized_relative(path: &Path) -> Result { + ensure!(path.is_relative(), "runner path is not relative"); + let mut parts = Vec::new(); + for component in path.components() { + match component { + std::path::Component::CurDir => {} + std::path::Component::Normal(value) => parts.push( + value + .to_str() + .context("runner path contains non-UTF-8")? + .to_owned(), + ), + _ => bail!("runner path escapes its workspace"), + } + } + Ok(parts.join("/")) +} + +fn copy_tree(source: &Path, destination: &Path) -> Result<()> { + ensure!(source.is_dir(), "source tree is not a directory"); + ensure!(!destination.exists(), "destination tree already exists"); + fs::create_dir_all(destination)?; + copy_tree_contents(source, destination) +} + +fn copy_tree_contents(source: &Path, destination: &Path) -> Result<()> { + let mut entries = fs::read_dir(source)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + let source_path = entry.path(); + let destination_path = destination.join(entry.file_name()); + let metadata = fs::symlink_metadata(&source_path)?; + if metadata.is_dir() { + fs::create_dir(&destination_path)?; + copy_tree_contents(&source_path, &destination_path)?; + } else if metadata.is_file() { + fs::copy(&source_path, &destination_path)?; + } else if metadata.file_type().is_symlink() { + copy_symlink(&source_path, &destination_path)?; + } else { + bail!("source tree contains a special filesystem entry") + } + } + Ok(()) +} + +#[cfg(unix)] +fn copy_symlink(source: &Path, destination: &Path) -> Result<()> { + std::os::unix::fs::symlink(fs::read_link(source)?, destination)?; + Ok(()) +} + +#[cfg(windows)] +fn copy_symlink(source: &Path, destination: &Path) -> Result<()> { + let target = fs::read_link(source)?; + if source.is_dir() { + std::os::windows::fs::symlink_dir(target, destination)?; + } else { + std::os::windows::fs::symlink_file(target, destination)?; + } + Ok(()) +} + +fn copy_artifact(source: &Path, destination: &Path) -> Result<()> { + let metadata = fs::symlink_metadata(source)?; + if metadata.is_dir() { + copy_tree(source, destination) + } else if metadata.is_file() { + if let Some(parent) = destination.parent() { + fs::create_dir_all(parent)?; + } + fs::copy(source, destination)?; + Ok(()) + } else { + bail!("artifact output is not a regular file or directory") + } +} + +fn digest_artifact(path: &Path) -> Result<(String, u64)> { + let metadata = fs::symlink_metadata(path)?; + if metadata.is_file() { + let bytes = fs::read(path)?; + return Ok((raw_sha256(&bytes), bytes.len().try_into()?)); + } + ensure!(metadata.is_dir(), "artifact is not a file or directory"); + let mut hasher = Sha256::new(); + let mut size = 0_u64; + digest_tree(path, path, &mut hasher, &mut size)?; + ensure!(size > 0, "artifact directory has no file content"); + Ok((format!("{:x}", hasher.finalize()), size)) +} + +fn digest_tree(root: &Path, current: &Path, hasher: &mut Sha256, size: &mut u64) -> Result<()> { + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + let path = entry.path(); + let relative = path.strip_prefix(root)?; + let relative = normalized_relative(relative)?; + let metadata = fs::symlink_metadata(&path)?; + if metadata.is_dir() { + hasher.update(b"dir\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + digest_tree(root, &path, hasher, size)?; + } else if metadata.is_file() { + let bytes = fs::read(&path)?; + hasher.update(b"file\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hasher.update((bytes.len() as u64).to_le_bytes()); + hasher.update(&bytes); + *size = size.saturating_add(bytes.len().try_into()?); + } else if metadata.file_type().is_symlink() { + let target = fs::read_link(&path)?; + hasher.update(b"link\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hasher.update(target.as_os_str().as_encoded_bytes()); + *size = size.saturating_add(target.as_os_str().len().try_into()?); + } else { + bail!("artifact tree contains a special filesystem entry") + } + } + Ok(()) +} + +fn raw_sha256(bytes: &[u8]) -> String { + format!("{:x}", Sha256::digest(bytes)) +} + +fn remove_tree_inside(root: &Path, target: &Path) -> Result<()> { + let root = root.canonicalize()?; + let target = target.canonicalize()?; + ensure!( + target.starts_with(&root) && target != root, + "refusing broad tree removal" + ); + fs::remove_dir_all(&target) + .with_context(|| format!("removing disposable runner workspace {}", target.display())) +} + +fn sha256_id(bytes: &[u8]) -> String { + format!("sha256-{:x}", Sha256::digest(bytes)) +} + +fn require_git_sha(value: &str, label: &str) -> Result<()> { + ensure!( + value.len() == 40 + && value + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), + "{label} is not a lowercase SHA-1 object id" + ); + Ok(()) +} + +fn ensure_source_directory_tree( + root: &Path, + directory: &Path, + identity: Option, +) -> Result<()> { + ensure!( + directory.starts_with(root), + "source directory escaped Idunn's source authority root" + ); + ensure_source_directory(root, identity)?; + let relative = directory.strip_prefix(root)?; + let mut current = root.to_owned(); + for component in relative.components() { + let std::path::Component::Normal(component) = component else { + bail!("source directory is not normalized") + }; + current.push(component); + ensure_source_directory(¤t, identity)?; + } + Ok(()) +} + +fn ensure_source_directory(path: &Path, identity: Option) -> Result<()> { + match fs::symlink_metadata(path) { + Ok(metadata) => { + ensure!( + metadata.is_dir() && !metadata.file_type().is_symlink(), + "source authority path {} is not a native directory", + path.display() + ); + validate_source_directory_owner(path, &metadata, identity) + } + Err(error) if error.kind() == ErrorKind::NotFound => { + let parent = path + .parent() + .context("source authority directory has no parent")?; + ensure!( + parent.is_dir(), + "source authority parent {} is absent", + parent.display() + ); + fs::create_dir(path).with_context(|| { + format!("creating source authority directory {}", path.display()) + })?; + assign_source_directory_owner(path, identity)?; + let metadata = fs::symlink_metadata(path)?; + validate_source_directory_owner(path, &metadata, identity) + } + Err(error) => Err(error) + .with_context(|| format!("inspecting source authority path {}", path.display())), + } +} + +#[cfg(unix)] +fn assign_source_directory_owner(path: &Path, identity: Option) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::PermissionsExt; + + if let Some(identity) = identity { + let path = std::ffi::CString::new(path.as_os_str().as_bytes()) + .context("source authority path contains a NUL byte")?; + let result = unsafe { libc::chown(path.as_ptr(), identity.uid, identity.gid) }; + if result != 0 { + return Err(std::io::Error::last_os_error()) + .context("assigning source authority directory owner"); + } + } + fs::set_permissions(path, fs::Permissions::from_mode(0o750))?; + Ok(()) +} + +#[cfg(not(unix))] +fn assign_source_directory_owner(_path: &Path, identity: Option) -> Result<()> { + ensure!( + identity.is_none(), + "configured source identities require a Unix actuator" + ); + Ok(()) +} + +#[cfg(unix)] +fn validate_source_directory_owner( + path: &Path, + metadata: &fs::Metadata, + identity: Option, +) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let expected = identity.unwrap_or(ProcessIdentity { + uid: unsafe { libc::geteuid() }, + gid: unsafe { libc::getegid() }, + }); + ensure!( + metadata.uid() == expected.uid && metadata.gid() == expected.gid, + "source authority directory {} has the wrong owner", + path.display() + ); + ensure!( + metadata.permissions().mode() & 0o200 != 0, + "source authority directory {} is not owner-writable", + path.display() + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_source_directory_owner( + _path: &Path, + _metadata: &fs::Metadata, + identity: Option, +) -> Result<()> { + ensure!( + identity.is_none(), + "configured source identities require a Unix actuator" + ); + Ok(()) +} + +fn apply_identity(command: &mut Command, identity: Option) -> Result<()> { + let Some(identity) = identity else { + return Ok(()); + }; + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + command.uid(identity.uid).gid(identity.gid); + Ok(()) + } + #[cfg(not(unix))] + { + let _ = command; + let _ = identity; + bail!("configured process identities require a Unix actuator") + } +} diff --git a/crates/idunn-daemon/src/lib.rs b/crates/idunn-daemon/src/lib.rs index 7474d59..7899c59 100644 --- a/crates/idunn-daemon/src/lib.rs +++ b/crates/idunn-daemon/src/lib.rs @@ -1,3 +1,5 @@ +pub mod control_plane; pub mod deployment; pub mod deployment_plan; +pub mod drivers; pub mod provisioning; diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 9911cfa..e7bd0a5 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -1,13710 +1,3 @@ -use anyhow::{Context, Result, anyhow}; -use cultcache_rs::{CacheBackingStore, DatabaseEntry}; -use cultcache_rs::{ - CultCache, CultCacheEnvelope, CultCacheExpectedEnvelope, SingleFileMessagePackBackingStore, -}; -use cultmesh_rs::{CultMesh, CultMeshNode, CultMeshNodeOptions}; -use cultnet_rs::{ - CultNetDocumentBinding, CultNetDocumentRegistry, CultNetMessage, CultNetRawDocumentRecord, - CultNetRawPayloadEncoding, CultNetReadOnlySnapshotPolicy, CultNetRudpPacketType, - CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, - CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, - GameCultProviderHealthIdentity, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, - IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnDeploymentBrakeObservation, - IdunnDeploymentBrakeOperatorIdentity, IdunnDeploymentBrakeRecord, IdunnServiceIdentity, - IdunnSignedDaemonHealthPurpose, ServiceIdentityProfile, ServiceIdentitySignature, - ServiceIdentitySigner, ServiceIdentityTrustAnchor, decode_cultnet_message_from_slice, - decode_rudp_packet, derive_service_identity_id, encode_cultnet_message_to_vec, - encode_rudp_packet, evaluate_idunn_deployment_brake, open_service_identity_at, - serve_read_only_raw_snapshot, verify_service_identity_signature_with_public_key, -}; -use ed25519_dalek::{Signature, Verifier, VerifyingKey}; -use odin_core::{ - BifrostRepositoryReleaseAuthorityRecord, IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA, - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA, - IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA, IdunnAuthenticatedDaemonHealthAdmissionRecord, - IdunnAuthenticatedProviderHealthProjectionRecord, IdunnCommandBoundaryRecord, - IdunnCurrentDeploymentRequestRecord, IdunnDaemonHealthRecord, - IdunnDaemonHealthTrustBindingRecord, IdunnDaemonSurgeryPlanRecord, - IdunnDaemonTransportProfileRecord, IdunnDeploymentArtifactRecord, IdunnDeploymentRequestRecord, - IdunnDeploymentResultRecord, IdunnDesiredDaemonRecord, IdunnLifecycleCommandRecord, - IdunnOperatorAlarmRecord, IdunnPlan, IdunnReleaseTargetRecord, IdunnRestartRequestRecord, - IdunnRestartResultRecord, IdunnRolloutPlanRecord, IdunnRolloutResultRecord, - IdunnRudpHealthIngressRecord, IdunnRuntimeTrafficAdmissionRecord, - IdunnRuntimeTransportCheckRecord, IdunnSignedDaemonHealthRecord, - IdunnSignedHealthAdmissionRecord, IdunnStateMigrationPlanRecord, - IdunnStateMigrationResultRecord, IdunnSwarmSurgeryPlanRecord, - IdunnUnsignedDaemonHealthDiagnosticRecord, OdinDocuments, - authenticated_provider_health_reason_code, plan_keepalive, -}; -use sha2::{Digest, Sha256}; -use std::collections::{BTreeMap, BTreeSet, HashMap}; -use std::env; -use std::fs::{self, File}; -use std::net::{SocketAddr, UdpSocket}; -use std::path::{Path, PathBuf}; -use std::process::{Child, Command, ExitStatus, Stdio}; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; -use std::thread; -use std::time::Duration; -use std::time::{Instant, SystemTime, UNIX_EPOCH}; - -const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0"; -const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001; -const IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID: u32 = 0x1d0d_0002; -const IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID: &str = "idunn-daemon"; -const IDUNN_PUBLIC_HEALTH_QUERY_ROLE: &str = "authenticated-provider-health-projector"; -const CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH: &str = - "/etc/gamecult/codex-connector/runtime/traffic-admission.cc"; -const CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP: &str = "codex-connector"; -const GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH: &str = - "/etc/gamecult/ghostlight-dungeon/runtime/traffic-admission.cc"; -const GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP: &str = "ghostlight"; -const RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL: &str = "traffic-admission-pending"; -const GHOSTLIGHT_MINIMUM_SOURCE_REVISION: &str = "6bb686981050e048c070376e7f7ac86d177e8f18"; -const CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION: &str = "68fe94b9475823de92ed766a97ea962d48723708"; -const EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE: &str = "epiphany.idunn_signed_runtime_health"; -const EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION: &str = - "epiphany.idunn_signed_runtime_health.v0"; -const EPIPHANY_HEALTH_CONTRACT: &str = "epiphany.cultnet-rudp-runtime-health"; -const EPIPHANY_HEALTH_SOURCE_RUNTIME: &str = "epiphany-daemon-supervisor"; -const EPIPHANY_ADMISSION_MAX_AGE_SECONDS: u64 = 180; -const IDUNN_OPERATIONAL_STORE_COMPACTION_BYTES: u64 = 1024 * 1024; -const IDUNN_OPERATIONAL_HISTORY_PER_DAEMON: usize = 8; -const IDUNN_LIFECYCLE_HISTORY_PER_DAEMON: usize = 32; -const SIGNED_DAEMON_HEALTH_SCHEMA_ID: &str = odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA; -const DEPLOYABLE_SOURCE_PATHS: &[&str] = &[ - ".", - ":(exclude)docs/**", - ":(exclude)notes/**", - ":(exclude)state/ledgers.msgpack", - ":(exclude)state/map.yaml", - ":(exclude)state/scratch*", - ":(exclude)*.md", -]; - -struct TargetActuationGate { - lock: Mutex<()>, - reserved: AtomicBool, -} - -impl TargetActuationGate { - fn new() -> Self { - Self { - lock: Mutex::new(()), - reserved: AtomicBool::new(false), - } - } -} -const HOST_IDENTITY_TYPE: &str = "epiphany.host_identity_trust_anchor.v0"; -const HOST_IDENTITY_KEY: &str = "host-incarnation-public"; -const HOST_SIGNATURE_DOMAIN: &[u8] = b"epiphany.host-incarnation.signature.v0\0"; -const HOST_IDENTITY_DOMAIN: &[u8] = b"epiphany.host-incarnation.identity.v0\0"; -static RELEASE_AUTHORITY_SNAPSHOT_SEQUENCE: AtomicU64 = AtomicU64::new(0); - -#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -struct IdunnDaemonHealthWireV1 { - daemon_id: String, - state: String, - detail: String, - observed_at: String, - health_contract: String, - publication_source: String, - transport: String, -} - -enum DecodedHealthIngress { - AuthenticatedLegacy { - health: IdunnDaemonHealthRecord, - admission: IdunnSignedHealthAdmissionRecord, - }, - AuthenticatedGeneric { - health: IdunnDaemonHealthRecord, - statement_payload: Vec, - admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, - }, - Diagnostic(IdunnUnsignedDaemonHealthDiagnosticRecord), -} - -struct HealthIngressOutcome { - daemon_id: String, - state: String, - detail: String, - authority: &'static str, -} - -#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -struct EpiphanySignedRuntimeHealthWire { - schema_version: String, - health: IdunnDaemonHealthWireV1, - source_runtime_id: String, - release_id: String, - release_witness_sha256: String, - source_commit: String, - deployment_request_id: String, - publisher_incarnation_id: String, - publisher_sequence: u64, - publisher_process_id: u32, - publisher_process_creation_token: u64, - publisher_process_created_at: String, - publisher_executable_path: String, - signer_identity_id: String, - signature_algorithm: String, - signature: Vec, -} - -#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] -struct EpiphanyHostIdentityWire { - schema_version: String, - identity_id: String, - public_key: Vec, - assurance: String, - identity_created_at: String, - source_identity_record_sha256: String, -} - -#[derive(Clone, Debug)] -struct DaemonTarget { - daemon_id: String, - verse_id: String, - name: String, - health_contract: HealthContract, - deploy_command: Option, - restart_command: Option, - release: Option, - enabled: bool, - interval_seconds: u64, -} - -#[derive(Clone, Debug)] -struct ReleaseTarget { - repo: String, - repository_full_name: String, - repo_path: PathBuf, - upstream_remote: String, - upstream_branch: String, - rollout_strategy: String, - state_migration_command: Option, - zero_downtime_capability: String, - deployed_revision_witness: Option, - artifact_strategy: String, - artifact_witness_root: Option, - requires_bifrost_authority: bool, - source_change_pathspecs: Vec, - minimum_source_revision: Option, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct CompiledMinimumSourceRevisionPolicy { - daemon_id: &'static str, - repository_full_name: &'static str, - canonical_repository_url: &'static str, - upstream_ref: &'static str, - canonical_fetched_ref: &'static str, - repository_path: &'static str, - minimum_source_revision: &'static str, -} - -const COMPILED_MINIMUM_SOURCE_REVISION_POLICIES: &[CompiledMinimumSourceRevisionPolicy] = &[ - CompiledMinimumSourceRevisionPolicy { - daemon_id: "yggdrasil-codex-connector", - repository_full_name: "GameCult/CodexConnector", - canonical_repository_url: "https://github.com/GameCult/CodexConnector.git", - upstream_ref: "refs/heads/codex/ghostlight-release-binding", - canonical_fetched_ref: "refs/remotes/origin/codex/ghostlight-release-binding", - repository_path: "/srv/build/CodexConnector", - minimum_source_revision: CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION, - }, - CompiledMinimumSourceRevisionPolicy { - daemon_id: "yggdrasil-ghostlight", - repository_full_name: "GameCult/Ghostlight", - canonical_repository_url: "https://github.com/GameCult/Ghostlight.git", - upstream_ref: "refs/heads/codex/ghostlight-dungeon-mvp", - canonical_fetched_ref: "refs/remotes/origin/codex/ghostlight-dungeon-mvp", - repository_path: "/srv/build/Ghostlight", - minimum_source_revision: GHOSTLIGHT_MINIMUM_SOURCE_REVISION, - }, -]; - -#[derive(Clone, Debug, PartialEq, Eq)] -struct ReleaseAuthorization { - repository_full_name: String, - upstream_ref: String, - source_revision: String, - authority_id: String, - envelope_sha256: String, - requires_bifrost_authority: bool, -} - -#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "camelCase")] -struct BifrostReleaseAuthorityWire { - authority_id: String, - command_id: String, - crossing_receipt_id: String, - repository_full_name: String, - upstream_ref: String, - commit_sha: String, - decision: String, - status: String, - policy_decision_id: String, - authority_reference: String, - actor_identity: String, - source_kind: String, - source_id: String, - epiphany_run_id: String, - epiphany_lane_id: String, - epiphany_agent_identity: String, - external_receipt_url: String, - external_receipt_id: String, - authorized_at: String, - expires_at: String, - revoked_at: String, - revocation_reason: String, -} - -impl From for BifrostRepositoryReleaseAuthorityRecord { - fn from(value: BifrostReleaseAuthorityWire) -> Self { - Self { - authority_id: value.authority_id, - command_id: value.command_id, - crossing_receipt_id: value.crossing_receipt_id, - repository_full_name: value.repository_full_name, - upstream_ref: value.upstream_ref, - commit_sha: value.commit_sha, - decision: value.decision, - status: value.status, - policy_decision_id: value.policy_decision_id, - authority_reference: value.authority_reference, - actor_identity: value.actor_identity, - source_kind: value.source_kind, - source_id: value.source_id, - epiphany_run_id: value.epiphany_run_id, - epiphany_lane_id: value.epiphany_lane_id, - epiphany_agent_identity: value.epiphany_agent_identity, - external_receipt_url: value.external_receipt_url, - external_receipt_id: value.external_receipt_id, - authorized_at: value.authorized_at, - expires_at: value.expires_at, - revoked_at: value.revoked_at, - revocation_reason: value.revocation_reason, - } - } -} - -impl From<&BifrostRepositoryReleaseAuthorityRecord> for BifrostReleaseAuthorityWire { - fn from(value: &BifrostRepositoryReleaseAuthorityRecord) -> Self { - Self { - authority_id: value.authority_id.clone(), - command_id: value.command_id.clone(), - crossing_receipt_id: value.crossing_receipt_id.clone(), - repository_full_name: value.repository_full_name.clone(), - upstream_ref: value.upstream_ref.clone(), - commit_sha: value.commit_sha.clone(), - decision: value.decision.clone(), - status: value.status.clone(), - policy_decision_id: value.policy_decision_id.clone(), - authority_reference: value.authority_reference.clone(), - actor_identity: value.actor_identity.clone(), - source_kind: value.source_kind.clone(), - source_id: value.source_id.clone(), - epiphany_run_id: value.epiphany_run_id.clone(), - epiphany_lane_id: value.epiphany_lane_id.clone(), - epiphany_agent_identity: value.epiphany_agent_identity.clone(), - external_receipt_url: value.external_receipt_url.clone(), - external_receipt_id: value.external_receipt_id.clone(), - authorized_at: value.authorized_at.clone(), - expires_at: value.expires_at.clone(), - revoked_at: value.revoked_at.clone(), - revocation_reason: value.revocation_reason.clone(), - } - } -} - -trait ReleaseAuthorityPort { - fn select( - &self, - repository_full_name: &str, - upstream_ref: &str, - now: &str, - ) -> Result; - - fn authorize( - &self, - repository_full_name: &str, - upstream_ref: &str, - source_revision: &str, - now: &str, - ) -> Result; -} - -struct CultCacheReleaseAuthorityPort<'a> { - store_path: &'a std::path::Path, -} - -impl ReleaseAuthorityPort for CultCacheReleaseAuthorityPort<'_> { - fn select( - &self, - repository_full_name: &str, - upstream_ref: &str, - now: &str, - ) -> Result { - let mut candidates = read_release_authority_snapshot(self.store_path)? - .into_iter() - .filter(|(receipt, _, _)| { - receipt.repository_full_name == repository_full_name - && receipt.upstream_ref == upstream_ref - && receipt.decision == "authorize" - && receipt.status == "authorized" - }) - .filter_map(|(receipt, envelope, digest)| { - let expected_id = - release_authority_id(repository_full_name, upstream_ref, &receipt.commit_sha); - validate_release_authority_receipt( - &receipt, - &expected_id, - repository_full_name, - upstream_ref, - &receipt.commit_sha, - now, - ) - .ok() - .map(|_| (receipt, envelope, digest)) - }) - .collect::>(); - if candidates.len() != 1 { - return Err(anyhow!( - "Bifrost release selection requires exactly one current authorized receipt for {repository_full_name} {upstream_ref}; found {}", - candidates.len() - )); - } - let (receipt, _envelope, envelope_sha256) = candidates.pop().expect("one candidate"); - Ok(ReleaseAuthorization { - repository_full_name: repository_full_name.to_string(), - upstream_ref: upstream_ref.to_string(), - source_revision: receipt.commit_sha, - authority_id: receipt.authority_id, - envelope_sha256, - requires_bifrost_authority: true, - }) - } - - fn authorize( - &self, - repository_full_name: &str, - upstream_ref: &str, - source_revision: &str, - now: &str, - ) -> Result { - validate_commit_sha(source_revision)?; - let authority_id = - release_authority_id(repository_full_name, upstream_ref, source_revision); - let result = read_release_authority_snapshot(self.store_path)? - .into_iter() - .find(|(receipt, _, _)| receipt.authority_id == authority_id) - .ok_or_else(|| anyhow!("Bifrost release authority is missing: {authority_id}"))?; - let (receipt, _envelope, envelope_sha256) = result; - validate_release_authority_receipt( - &receipt, - &authority_id, - repository_full_name, - upstream_ref, - source_revision, - now, - )?; - Ok(ReleaseAuthorization { - repository_full_name: repository_full_name.to_string(), - upstream_ref: upstream_ref.to_string(), - source_revision: source_revision.to_string(), - authority_id, - envelope_sha256, - requires_bifrost_authority: true, - }) - } -} - -fn read_release_authority_snapshot( - store_path: &std::path::Path, -) -> Result< - Vec<( - BifrostRepositoryReleaseAuthorityRecord, - CultCacheEnvelope, - String, - )>, -> { - if !store_path.is_file() { - return Err(anyhow!( - "Bifrost release authority store is unavailable: {}", - store_path.display() - )); - } - let snapshot_path = env::temp_dir().join(format!( - "idunn-release-authority-snapshot-{}-{}.cc", - std::process::id(), - RELEASE_AUTHORITY_SNAPSHOT_SEQUENCE.fetch_add(1, Ordering::Relaxed) - )); - fs::copy(store_path, &snapshot_path).with_context(|| { - format!( - "copying Bifrost release authority snapshot {}", - store_path.display() - ) - })?; - let result = (|| { - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_backing_store( - SingleFileMessagePackBackingStore::new(&snapshot_path), - [BifrostRepositoryReleaseAuthorityRecord::TYPE], - ); - cache.pull_all_backing_stores()?; - cache - .snapshot() - .into_iter() - .filter(|envelope| envelope.r#type == BifrostRepositoryReleaseAuthorityRecord::TYPE) - .map(|envelope| { - if envelope.schema_id.as_deref() - != Some(odin_core::BIFROST_REPOSITORY_RELEASE_AUTHORITY_SCHEMA) - { - return Err(anyhow!("unsupported Bifrost release authority schema")); - } - let receipt = - rmp_serde::from_slice::(&envelope.payload) - .context("decode Bifrost camelCase release-authority payload")? - .into(); - let digest = format!("{:x}", Sha256::digest(rmp_serde::to_vec_named(&envelope)?)); - Ok((receipt, envelope, digest)) - }) - .collect() - })(); - let _ = fs::remove_file(&snapshot_path); - let _ = fs::remove_file(snapshot_path.with_extension("cc.lock")); - result -} - -trait ReleaseStatePort { - fn fetch(&self, release: &ReleaseTarget) -> Result<()>; - fn desired_revision(&self, release: &ReleaseTarget) -> Result; - fn deployed_revision(&self, release: &ReleaseTarget) -> Result; -} - -struct SystemReleaseStatePort; - -impl ReleaseStatePort for SystemReleaseStatePort { - fn fetch(&self, release: &ReleaseTarget) -> Result<()> { - let fetch_refspec = format!( - "refs/heads/{}:refs/remotes/{}/{}", - release.upstream_branch, release.upstream_remote, release.upstream_branch - ); - let status = Command::new("git") - .arg("-C") - .arg(&release.repo_path) - .arg("fetch") - .arg("--quiet") - .arg(&release.upstream_remote) - .arg(&fetch_refspec) - .status() - .with_context(|| { - format!( - "fetching {} {} in {}", - release.upstream_remote, - release.upstream_branch, - release.repo_path.display() - ) - })?; - if !status.success() { - return Err(anyhow!("git fetch exited with {status}")); - } - Ok(()) - } - - fn desired_revision(&self, release: &ReleaseTarget) -> Result { - let upstream = format!("{}/{}", release.upstream_remote, release.upstream_branch); - if release.source_change_pathspecs.is_empty() { - return git_revision(&release.repo_path, &upstream).ok_or_else(|| { - anyhow!( - "cannot resolve {}/{}", - release.upstream_remote, - release.upstream_branch - ) - }); - } - let output = Command::new("git") - .arg("-C") - .arg(&release.repo_path) - .arg("log") - .arg("-1") - .arg("--format=%H") - .arg(&upstream) - .arg("--") - .args(&release.source_change_pathspecs) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .output() - .with_context(|| { - format!( - "resolving source-changing revision for {}/{}", - release.upstream_remote, release.upstream_branch - ) - })?; - if !output.status.success() { - return Err(anyhow!( - "git log exited with {} while resolving source-changing revision: {}", - output.status, - String::from_utf8_lossy(&output.stderr).trim() - )); - } - let revision = String::from_utf8_lossy(&output.stdout).trim().to_string(); - validate_commit_sha(&revision)?; - Ok(revision) - } - - fn deployed_revision(&self, release: &ReleaseTarget) -> Result { - let path = release - .deployed_revision_witness - .as_ref() - .ok_or_else(|| anyhow!("no deployed revision witness configured"))?; - read_deployed_revision_witness(path) - } -} - -#[derive(Clone, Debug)] -struct HealthContract { - id: String, - default_failure_state: String, - restart_on_missing_publication: bool, -} - -fn health_contract(id: &str, default_failure_state: &str) -> HealthContract { - HealthContract { - id: id.to_string(), - default_failure_state: default_failure_state.to_string(), - restart_on_missing_publication: false, - } -} - -fn locally_supervised_health_contract(id: &str, default_failure_state: &str) -> HealthContract { - HealthContract { - id: id.to_string(), - default_failure_state: default_failure_state.to_string(), - restart_on_missing_publication: true, - } -} - -#[derive(Clone, Debug)] -struct CommonOptions { - store_path: PathBuf, - release_authority_store_path: Option, - deployment_brake_store_path: Option, - deployment_brake_operator_anchor_path: Option, - deployment_brake_runtime_id: Option, - operator_alarm_command: Option, - rudp_health_bind: Option, - trusted_epiphany_health_identity_store: Option, - daemon_health_trust_store_path: Option, - service_identity_store_path: Option, - public_health_store_path: Option, - public_health_query_bind: Option, - execute: bool, - command_timeout_seconds: u64, -} - -#[derive(Clone, Debug)] -enum Mode { - Single(DaemonTarget), - Swarm(SwarmOptions), - LifecycleCommand(LifecycleCommandOptions), - ReleaseAuthorityValidation(ReleaseAuthorityValidationOptions), - MinimumSourceRevisionValidation(MinimumSourceRevisionValidationOptions), - HealthAdmissionValidation(HealthAdmissionValidationOptions), - RuntimeAdmissionValidation(RuntimeAdmissionValidationOptions), -} - -#[derive(Clone, Debug)] -struct HealthAdmissionValidationOptions { - daemon_id: String, - deployment_request_id: String, - release_id: String, - release_witness_sha256: String, - source_commit: String, - activation_witness_sha256: Option, - write_admission_witness: Option, - runtime_process: Option, -} - -#[derive(Clone, Debug)] -struct RuntimeAdmissionValidationOptions { - input: PathBuf, - daemon_id: String, - deployment_request_id: String, - release_id: String, - release_witness_sha256: String, - source_commit: String, - activation_witness_sha256: String, - runtime_process: RuntimeProcessObservation, -} - -#[derive(Clone, Debug)] -enum ValidatedHealthAdmission { - GenericActive(IdunnAuthenticatedDaemonHealthAdmissionRecord), - RuntimeTraffic(RuntimeTrafficIssuanceCandidate), - LegacyActive(IdunnSignedHealthAdmissionRecord), -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct RuntimeProcessObservation { - process_id: u32, - starttime_ticks: u64, -} - -impl RuntimeProcessObservation { - fn validate(self) -> Result { - if self.process_id == 0 || self.starttime_ticks == 0 { - return Err(anyhow!( - "runtime process id and starttime ticks must both be nonzero" - )); - } - Ok(self) - } -} - -#[derive(Clone, Debug)] -struct RuntimeTrafficIssuanceCandidate { - grant: IdunnRuntimeTrafficAdmissionRecord, - desired: IdunnDesiredDaemonRecord, - source: AuthenticatedProviderHealthSource, -} - -#[derive(Clone, Debug)] -struct SwarmOptions { - profile: String, - repo_root: PathBuf, -} - -#[derive(Clone, Debug)] -struct Options { - common: CommonOptions, - mode: Mode, -} - -#[derive(Clone, Debug)] -enum LifecycleAction { - Restart, - Redeploy, -} - -#[derive(Clone, Debug)] -struct LifecycleCommandOptions { - daemon_id: String, - action: LifecycleAction, - requested_by: String, - detail: String, -} - -#[derive(Clone, Debug)] -struct ReleaseAuthorityValidationOptions { - store_path: PathBuf, - repository_full_name: String, - upstream_ref: String, - source_revision: String, - authority_id: String, - envelope_sha256: String, -} - -#[derive(Clone, Debug)] -struct MinimumSourceRevisionValidationOptions { - daemon_id: String, - source_revision: String, -} - -fn main() -> Result<()> { - let options = Options::parse(env::args().skip(1))?; - - if !matches!( - &options.mode, - Mode::RuntimeAdmissionValidation(_) | Mode::MinimumSourceRevisionValidation(_) - ) && let Some(parent) = options.common.store_path.parent() - { - std::fs::create_dir_all(parent) - .with_context(|| format!("creating {}", parent.display()))?; - } - - match &options.mode { - Mode::Single(target) => { - let projection_publisher = initialize_projection_publisher(&options.common)?; - let store_lock = Arc::new(Mutex::new(())); - let actuation_gate = Arc::new(TargetActuationGate::new()); - let now = timestamp()?; - publish_runtime_transport_check(&options.common, &store_lock, &now)?; - let mut missing_since = None; - run_target_cycle( - target, - &options.common, - &store_lock, - Arc::clone(&actuation_gate), - projection_publisher.as_deref(), - &mut missing_since, - )?; - while actuation_gate.reserved.load(Ordering::Acquire) { - thread::sleep(Duration::from_millis(10)); - } - Ok(()) - } - Mode::Swarm(swarm) => { - let projection_publisher = initialize_projection_publisher(&options.common)?; - run_swarm(swarm, &options.common, projection_publisher) - } - Mode::LifecycleCommand(command) => publish_lifecycle_command(command, &options.common), - Mode::ReleaseAuthorityValidation(validation) => { - validate_release_authority_at_privileged_boundary(validation) - } - Mode::MinimumSourceRevisionValidation(validation) => { - validate_minimum_source_revision_at_privileged_boundary(validation) - } - Mode::HealthAdmissionValidation(validation) => { - validate_health_admission(validation, &options.common) - } - Mode::RuntimeAdmissionValidation(validation) => { - let admission = validate_runtime_admission(validation)?; - println!( - "validated Idunn runtime traffic admission daemon={} releaseId={} witnessSha256={} sourceCommit={} deploymentId={} activationWitnessSha256={} signedHealthSha256={} publisherIncarnation={} publisherSequence={} signerIdentity={} runtimeProcessId={} runtimeProcessStarttimeTicks={}", - admission.daemon_id, - admission.release_id, - admission.release_witness_sha256, - admission.source_commit, - admission.deployment_id, - admission.activation_witness_sha256, - admission.signed_health_sha256, - admission.publisher_incarnation_id, - admission.publisher_sequence, - admission.signer_identity_id, - admission.runtime_process_id, - admission.runtime_process_starttime_ticks, - ); - Ok(()) - } - } -} - -fn validate_health_admission( - expected: &HealthAdmissionValidationOptions, - options: &CommonOptions, -) -> Result<()> { - let now = timestamp()?; - if expected.write_admission_witness.is_some() { - let grant = issue_runtime_traffic_admission(expected, options, &now)?; - let output = expected.write_admission_witness.as_deref().ok_or_else(|| { - anyhow!("validated runtime traffic candidate lost its fixed output path") - })?; - println!( - "published Idunn runtime traffic admission daemon={} path={} signedHealthSha256={} publisherIncarnation={} publisherSequence={} runtimeProcessId={} runtimeProcessStarttimeTicks={}", - grant.daemon_id, - output.display(), - grant.signed_health_sha256, - grant.publisher_incarnation_id, - grant.publisher_sequence, - grant.runtime_process_id, - grant.runtime_process_starttime_ticks, - ); - return Ok(()); - } - - let admission = validate_health_admission_at(expected, options, &now)?; - match admission { - ValidatedHealthAdmission::GenericActive(admission) => { - println!( - "validated authenticated Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} activationWitnessSha256={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", - admission.daemon_id, - admission.state, - admission.release_id.as_deref().unwrap_or_default(), - admission - .release_witness_sha256 - .as_deref() - .unwrap_or_default(), - admission.source_commit.as_deref().unwrap_or_default(), - admission - .activation_witness_sha256 - .as_deref() - .unwrap_or_default(), - admission.signed_health_sha256, - admission.publisher_incarnation_id, - admission.publisher_sequence, - ); - } - ValidatedHealthAdmission::RuntimeTraffic(_) => { - return Err(anyhow!( - "runtime traffic admission must use the atomic issuance path" - )); - } - ValidatedHealthAdmission::LegacyActive(admission) => { - println!( - "validated signed Idunn health admission daemon={} state={} releaseId={} witnessSha256={} sourceCommit={} signedHealthSha256={} publisherIncarnation={} publisherSequence={}", - admission.daemon_id, - admission.state, - admission.release_id, - admission.release_witness_sha256, - admission.source_commit, - admission.signed_health_sha256, - admission.publisher_incarnation_id, - admission.publisher_sequence, - ); - } - } - Ok(()) -} - -fn validate_health_admission_at( - expected: &HealthAdmissionValidationOptions, - options: &CommonOptions, - now: &str, -) -> Result { - if let Some(digest) = expected.activation_witness_sha256.as_deref() { - validate_sha256(digest, "activation witness")?; - } - let issuing_runtime_admission = expected.write_admission_witness.is_some(); - if let Some(output) = expected.write_admission_witness.as_deref() { - require_supported_runtime_admission_pair(&expected.daemon_id, output)?; - if expected.activation_witness_sha256.is_none() { - return Err(anyhow!( - "runtime traffic admission requires --activation-witness-sha256" - )); - } - expected.runtime_process.ok_or_else(|| { - anyhow!( - "runtime traffic admission requires --runtime-process-id and --runtime-process-starttime-ticks" - ) - })?; - } else if expected.runtime_process.is_some() { - return Err(anyhow!( - "runtime process identity belongs only to runtime traffic admission issuance" - )); - } - let lock = Arc::new(Mutex::new(())); - let has_generic_admission = with_store_node(options, &lock, |node| { - Ok(node - .get::(&expected.daemon_id)? - .is_some()) - })?; - if has_generic_admission { - let desired = with_store_node(options, &lock, |node| { - node.get::(&expected.daemon_id)? - .ok_or_else(|| { - anyhow!( - "Idunn has no desired daemon record for {}", - expected.daemon_id - ) - }) - })?; - let managed = read_fresh_daemon_published_health(options, &lock, &desired, now)? - .ok_or_else(|| { - anyhow!( - "Idunn has no fresh authenticated health admission for {}", - expected.daemon_id - ) - })?; - let source = managed.projection_source.ok_or_else(|| { - anyhow!( - "Idunn current health for {} is not the generic authenticated admission", - expected.daemon_id - ) - })?; - let signed_health_detail = source.statement.detail.clone(); - let admission = source.admission.clone(); - let required_state = validate_health_admission_posture( - &admission.state, - &signed_health_detail, - issuing_runtime_admission, - )?; - if admission.deployment_id.as_deref() != Some(expected.deployment_request_id.as_str()) - || admission.release_id.as_deref() != Some(expected.release_id.as_str()) - || admission.release_witness_sha256.as_deref() - != Some(expected.release_witness_sha256.as_str()) - || admission.source_commit.as_deref() != Some(expected.source_commit.as_str()) - || expected - .activation_witness_sha256 - .as_deref() - .is_some_and(|digest| { - admission.activation_witness_sha256.as_deref() != Some(digest) - }) - || admission.publisher_incarnation_id.is_empty() - || admission.publisher_sequence == 0 - || admission.signed_health_sha256.is_empty() - || admission.signer_identity_id.is_empty() - { - return Err(anyhow!( - "Idunn admitted health does not prove the exact {} candidate", - required_state - )); - } - if issuing_runtime_admission { - let process = expected.runtime_process.ok_or_else(|| { - anyhow!("runtime traffic candidate lost its root-observed process identity") - })?; - return Ok(ValidatedHealthAdmission::RuntimeTraffic( - RuntimeTrafficIssuanceCandidate { - grant: runtime_traffic_admission_from(&admission, process)?, - desired, - source, - }, - )); - } - return Ok(ValidatedHealthAdmission::GenericActive(admission)); - } - - // The old Epiphany-specific health contract remains a sealed read path for - // already-admitted releases. New publishers use the generic authenticated - // daemon-health contract above. - if issuing_runtime_admission { - return Err(anyhow!( - "runtime traffic admission requires generic authenticated daemon health; legacy Epiphany admission is ineligible" - )); - } - let admission = with_store_node(options, &lock, |node| { - let admission = node - .get::(&expected.daemon_id)? - .ok_or_else(|| { - anyhow!( - "Idunn has no signed health admission for {}", - expected.daemon_id - ) - })?; - validate_admission_against_current_request(node, &admission)?; - let current_health = node - .get::(&expected.daemon_id)? - .ok_or_else(|| anyhow!("Idunn has no current health projection for admission"))?; - if current_health.state != admission.state - || current_health.observed_at != admission.observed_at - || current_health.health_contract != admission.health_contract - || current_health.publication_source != "daemon-published" - || current_health.transport != CULTNET_RUDP_PROTOCOL_ID - { - return Err(anyhow!( - "signed health admission is not the current daemon health observation" - )); - } - Ok(admission) - })?; - validate_admission_fresh_at(&admission, now, EPIPHANY_ADMISSION_MAX_AGE_SECONDS)?; - if admission.state != "active" - || admission.deployment_request_id != expected.deployment_request_id - || admission.release_id != expected.release_id - || admission.release_witness_sha256 != expected.release_witness_sha256 - || admission.source_commit != expected.source_commit - || expected.activation_witness_sha256.is_some() - || admission.publisher_incarnation_id.is_empty() - || admission.publisher_sequence == 0 - || admission.signed_health_sha256.is_empty() - || admission.signer_identity_id.is_empty() - { - return Err(anyhow!( - "Idunn admitted health does not prove the exact active candidate" - )); - } - Ok(ValidatedHealthAdmission::LegacyActive(admission)) -} - -fn validate_health_admission_posture( - state: &str, - detail: &str, - issuing_runtime_admission: bool, -) -> Result<&'static str> { - let required_state = if issuing_runtime_admission { - "warming" - } else { - "active" - }; - if state != required_state - || (issuing_runtime_admission && detail != RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL) - { - return Err(anyhow!( - "Idunn admitted health does not prove the exact {} candidate", - required_state - )); - } - Ok(required_state) -} - -fn runtime_traffic_admission_from( - admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, - process: RuntimeProcessObservation, -) -> Result { - if admission.state != "warming" { - return Err(anyhow!( - "runtime traffic authority requires a supported target's warming authenticated admission" - )); - } - let process = process.validate()?; - let grant = IdunnRuntimeTrafficAdmissionRecord { - schema_version: IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into(), - daemon_id: admission.daemon_id.clone(), - release_id: admission - .release_id - .clone() - .ok_or_else(|| anyhow!("runtime traffic authority lost its release id"))?, - release_witness_sha256: admission - .release_witness_sha256 - .clone() - .ok_or_else(|| anyhow!("runtime traffic authority lost its release witness"))?, - source_commit: admission - .source_commit - .clone() - .ok_or_else(|| anyhow!("runtime traffic authority lost its source commit"))?, - deployment_id: admission - .deployment_id - .clone() - .ok_or_else(|| anyhow!("runtime traffic authority lost its deployment id"))?, - activation_witness_sha256: admission - .activation_witness_sha256 - .clone() - .ok_or_else(|| anyhow!("runtime traffic authority lost its activation witness"))?, - signed_health_sha256: admission.signed_health_sha256.clone(), - publisher_incarnation_id: admission.publisher_incarnation_id.clone(), - publisher_sequence: admission.publisher_sequence, - signer_identity_id: admission.signer_identity_id.clone(), - runtime_process_id: process.process_id, - runtime_process_starttime_ticks: process.starttime_ticks, - }; - grant.validate()?; - Ok(grant) -} - -fn issue_runtime_traffic_admission( - expected: &HealthAdmissionValidationOptions, - options: &CommonOptions, - now: &str, -) -> Result { - let output = expected - .write_admission_witness - .as_deref() - .ok_or_else(|| anyhow!("runtime traffic issuance lost its fixed output path"))?; - require_supported_runtime_admission_pair(&expected.daemon_id, output)?; - #[cfg(unix)] - require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; - let process = expected - .runtime_process - .ok_or_else(|| anyhow!("runtime traffic issuance lost its process identity"))? - .validate()?; - require_current_runtime_process(process)?; - - let ValidatedHealthAdmission::RuntimeTraffic(candidate) = - validate_health_admission_at(expected, options, now)? - else { - return Err(anyhow!( - "runtime traffic issuance did not resolve an authenticated probation candidate" - )); - }; - - let trust_store_path = options - .daemon_health_trust_store_path - .as_deref() - .ok_or_else(|| anyhow!("runtime traffic issuance has no root trust store"))?; - // Fixed lock order: first the Idunn-owned operational store, then the - // root-owned trust store. These locks are synchronization, not decision - // owners. Every authenticated-health/head writer contends on the first; - // every trust writer contends on the second. Holding both shared freezes - // the exact source and signer authority while root takes a fresh clock, - // rechecks the process, and CAS-publishes the one root-owned grant. Target - // services can only read their separately sealed grant store and lock. - let state_store = SingleFileMessagePackBackingStore::new(&options.store_path); - let trust_store = SingleFileMessagePackBackingStore::new(trust_store_path); - let grant = state_store.with_read_only_shared_snapshot(|state_snapshot| { - trust_store.with_read_only_shared_snapshot(|trust_snapshot| { - validate_runtime_traffic_issuance_trust_snapshot(&trust_snapshot, &candidate)?; - let mint_now = timestamp()?; - validate_runtime_traffic_issuance_snapshot( - &state_snapshot, - &candidate, - expected, - &mint_now, - )?; - require_current_runtime_process(process)?; - publish_runtime_traffic_admission(output, &candidate.grant)?; - if let Err(process_error) = require_current_runtime_process(process) { - let revoke = revoke_runtime_traffic_admission(output); - return match revoke { - Ok(()) => Err(process_error.context( - "runtime process changed immediately after grant publication; grant revoked", - )), - Err(revoke_error) => Err(anyhow!( - "runtime process changed immediately after grant publication: {process_error:#}; fail-closed revocation also failed: {revoke_error:#}" - )), - }; - } - Ok(candidate.grant.clone()) - }) - })?; - Ok(grant) -} - -fn validate_runtime_traffic_issuance_trust_snapshot( - snapshot: &[CultCacheEnvelope], - candidate: &RuntimeTrafficIssuanceCandidate, -) -> Result<()> { - let mut matches = Vec::new(); - for entry in snapshot - .iter() - .filter(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) - { - let binding: IdunnDaemonHealthTrustBindingRecord = rmp_serde::from_slice(&entry.payload) - .context("decoding frozen runtime-issuance trust binding")?; - binding.validate()?; - if binding.daemon_id == candidate.source.statement.daemon_id - && binding.health_contract == candidate.source.statement.health_contract - && binding.source_runtime_id == candidate.source.statement.source_runtime_id - { - matches.push(binding); - } - } - let [binding] = matches.as_slice() else { - return Err(anyhow!( - "runtime traffic issuance requires one exact frozen trust binding" - )); - }; - let binding_sha256 = record_sha256(binding)?; - if binding != &candidate.source.binding - || binding_sha256 != candidate.source.binding_sha256 - || binding.signer_identity_id != candidate.source.admission.signer_identity_id - { - return Err(anyhow!( - "runtime traffic issuance trust binding changed before grant publication" - )); - } - Ok(()) -} - -fn validate_runtime_traffic_issuance_snapshot( - snapshot: &[CultCacheEnvelope], - candidate: &RuntimeTrafficIssuanceCandidate, - expected: &HealthAdmissionValidationOptions, - mint_now: &str, -) -> Result<()> { - let desired: IdunnDesiredDaemonRecord = - required_snapshot_record(snapshot, &candidate.desired.daemon_id, "desired daemon")?; - let health: IdunnDaemonHealthRecord = required_snapshot_record( - snapshot, - &candidate.source.health.daemon_id, - "authenticated daemon health", - )?; - let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = required_snapshot_record( - snapshot, - &candidate.source.admission.daemon_id, - "authenticated health admission", - )?; - let statement_envelope = required_snapshot_envelope::( - snapshot, - &candidate.source.admission.daemon_id, - "signed daemon health", - )?; - let (statement, _) = decode_generic_signed_health_payload(&statement_envelope.payload)?; - let statement_sha256 = format!("sha256-{:x}", Sha256::digest(&statement_envelope.payload)); - let deployment_head = candidate - .source - .deployment_head - .as_ref() - .ok_or_else(|| anyhow!("runtime traffic candidate lost its deployment head"))?; - let current_head: IdunnCurrentDeploymentRequestRecord = required_snapshot_record( - snapshot, - &candidate.source.admission.daemon_id, - "current deployment head", - )?; - let deployment_request = candidate - .source - .deployment_request - .as_ref() - .ok_or_else(|| anyhow!("runtime traffic candidate lost its deployment request"))?; - let current_request: IdunnDeploymentRequestRecord = required_snapshot_record( - snapshot, - &deployment_request.request_id, - "deployment request", - )?; - - let process = expected - .runtime_process - .ok_or_else(|| anyhow!("runtime traffic issuance lost its expected process identity"))?; - let expected_grant = runtime_traffic_admission_from(&admission, process)?; - validate_runtime_traffic_issuance_freshness(&desired, &admission, mint_now)?; - if desired != candidate.desired - || health != candidate.source.health - || admission != candidate.source.admission - || statement != candidate.source.statement - || statement_sha256 != candidate.source.statement_sha256 - || current_head != *deployment_head - || current_request != *deployment_request - || current_head.daemon_id != admission.daemon_id - || current_head.request_id != expected.deployment_request_id - || current_head.request_id != current_request.request_id - || current_head.sequence == 0 - || current_request.daemon_id != admission.daemon_id - || current_request.source_revision != expected.source_commit - || expected_grant != candidate.grant - { - return Err(anyhow!( - "authenticated health or current deployment request changed before runtime traffic issuance" - )); - } - Ok(()) -} - -fn validate_runtime_traffic_issuance_freshness( - desired: &IdunnDesiredDaemonRecord, - admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, - mint_now: &str, -) -> Result<()> { - let mint_now_millis = parse_timestamp_millis(mint_now) - .ok_or_else(|| anyhow!("runtime traffic issuance clock is invalid"))?; - if mint_now_millis < admission.admitted_at_unix_millis - || mint_now_millis.saturating_sub(admission.observed_at_unix_millis) - > u64::from(desired.max_silence_seconds).saturating_mul(1000) - { - return Err(anyhow!( - "authenticated health became stale before runtime traffic issuance" - )); - } - Ok(()) -} - -fn required_snapshot_record(snapshot: &[CultCacheEnvelope], key: &str, label: &str) -> Result -where - T: DatabaseEntry + serde::de::DeserializeOwned, -{ - let envelope = required_snapshot_envelope::(snapshot, key, label)?; - rmp_serde::from_slice(&envelope.payload) - .with_context(|| format!("decoding exact {label} snapshot")) -} - -fn required_snapshot_envelope<'a, T: DatabaseEntry>( - snapshot: &'a [CultCacheEnvelope], - key: &str, - label: &str, -) -> Result<&'a CultCacheEnvelope> { - let mut matches = snapshot - .iter() - .filter(|entry| entry.r#type == T::TYPE && entry.key == key); - let envelope = matches - .next() - .ok_or_else(|| anyhow!("exact {label} snapshot is absent"))?; - if matches.next().is_some() { - return Err(anyhow!("exact {label} snapshot is ambiguous")); - } - Ok(envelope) -} - -fn require_current_runtime_process(expected: RuntimeProcessObservation) -> Result<()> { - let observed = observe_runtime_process(expected.process_id)?; - if observed != expected { - return Err(anyhow!( - "root-observed runtime process identity changed before traffic admission" - )); - } - Ok(()) -} - -#[cfg(target_os = "linux")] -fn observe_runtime_process(process_id: u32) -> Result { - let stat_path = PathBuf::from(format!("/proc/{process_id}/stat")); - let stat = fs::read_to_string(&stat_path).with_context(|| { - format!( - "reading root-observed runtime process {}", - stat_path.display() - ) - })?; - Ok(RuntimeProcessObservation { - process_id, - starttime_ticks: parse_linux_proc_stat_starttime_ticks(&stat, process_id)?, - }) -} - -#[cfg(not(target_os = "linux"))] -fn observe_runtime_process(_process_id: u32) -> Result { - Err(anyhow!( - "runtime traffic admission process observation requires Linux /proc" - )) -} - -fn parse_linux_proc_stat_starttime_ticks(stat: &str, expected_process_id: u32) -> Result { - let command_open = stat - .find('(') - .ok_or_else(|| anyhow!("Linux process stat has no command opening delimiter"))?; - let command_close = stat - .rfind(')') - .filter(|close| *close > command_open) - .ok_or_else(|| anyhow!("Linux process stat has no command closing delimiter"))?; - let process_id: u32 = stat[..command_open] - .trim() - .parse() - .context("Linux process stat has an invalid process id")?; - if process_id != expected_process_id || process_id == 0 { - return Err(anyhow!( - "Linux process stat does not describe the expected process" - )); - } - // Fields after the command begin at field 3 (state), so zero-based index - // 19 is field 22: the kernel starttime in clock ticks since boot. - let starttime_ticks: u64 = stat[command_close + 1..] - .split_whitespace() - .nth(19) - .ok_or_else(|| anyhow!("Linux process stat has no field-22 starttime"))? - .parse() - .context("Linux process stat field-22 starttime is invalid")?; - if starttime_ticks == 0 { - return Err(anyhow!( - "Linux process stat field-22 starttime must be nonzero" - )); - } - Ok(starttime_ticks) -} - -fn runtime_admission_policy_for_path(path: &Path) -> Result<(&'static str, &'static str)> { - let spelling = path.as_os_str(); - if spelling == std::ffi::OsStr::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) { - return Ok(( - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP, - )); - } - if spelling == std::ffi::OsStr::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) { - return Ok(( - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP, - )); - } - Err(anyhow!( - "runtime traffic admission path is not one of the two exact supported absolute paths" - )) -} - -fn require_supported_runtime_admission_pair(daemon_id: &str, path: &Path) -> Result<()> { - let (expected_daemon_id, _) = runtime_admission_policy_for_path(path)?; - if daemon_id != expected_daemon_id { - return Err(anyhow!( - "runtime traffic admission daemon/path pair is unsupported" - )); - } - Ok(()) -} - -fn decode_runtime_traffic_admission_snapshot( - entries: &[CultCacheEnvelope], -) -> Result { - let [entry] = entries else { - return Err(anyhow!( - "runtime traffic admission store must contain exactly one record" - )); - }; - if entry.r#type != IdunnRuntimeTrafficAdmissionRecord::TYPE - || entry.schema_id.as_deref() != Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA) - || chrono::DateTime::parse_from_rfc3339(&entry.stored_at).is_err() - { - return Err(anyhow!( - "runtime traffic admission envelope has the wrong type, schema, or timestamp" - )); - } - let record: IdunnRuntimeTrafficAdmissionRecord = - rmp_serde::from_slice(&entry.payload).context("decoding runtime traffic admission")?; - if rmp_serde::to_vec(&record)? != entry.payload { - return Err(anyhow!( - "runtime traffic admission is not canonical positional MessagePack" - )); - } - record.validate()?; - if record.daemon_id != entry.key { - return Err(anyhow!( - "runtime traffic admission envelope key does not match its supported daemon" - )); - } - Ok(record) -} - -fn validate_runtime_admission( - expected: &RuntimeAdmissionValidationOptions, -) -> Result { - expected.runtime_process.validate()?; - require_current_runtime_process(expected.runtime_process)?; - require_supported_runtime_admission_pair(&expected.daemon_id, &expected.input)?; - validate_sha256(&expected.release_witness_sha256, "release witness")?; - validate_commit_sha(&expected.source_commit)?; - validate_sha256(&expected.activation_witness_sha256, "activation witness")?; - validate_runtime_admission_filesystem(&expected.input, true)?; - let record = SingleFileMessagePackBackingStore::new(&expected.input) - .with_read_only_shared_snapshot(|entries| -> Result<_> { - let record = decode_runtime_traffic_admission_snapshot(&entries)?; - validate_runtime_admission_lineage(&record, expected)?; - Ok(record) - })?; - validate_runtime_admission_filesystem(&expected.input, true)?; - require_current_runtime_process(expected.runtime_process)?; - Ok(record) -} - -fn validate_runtime_admission_lineage( - record: &IdunnRuntimeTrafficAdmissionRecord, - expected: &RuntimeAdmissionValidationOptions, -) -> Result<()> { - if record.daemon_id != expected.daemon_id - || record.deployment_id != expected.deployment_request_id - || record.release_id != expected.release_id - || record.release_witness_sha256 != expected.release_witness_sha256 - || record.source_commit != expected.source_commit - || record.activation_witness_sha256 != expected.activation_witness_sha256 - || record.runtime_process_id != expected.runtime_process.process_id - || record.runtime_process_starttime_ticks != expected.runtime_process.starttime_ticks - { - return Err(anyhow!( - "runtime traffic admission does not prove the exact expected release tuple" - )); - } - Ok(()) -} - -fn require_runtime_admission_writer_uid(uid: u32) -> Result<()> { - if uid != 0 { - return Err(anyhow!( - "runtime traffic admission publication requires root" - )); - } - Ok(()) -} - -fn publish_runtime_traffic_admission( - path: &Path, - record: &IdunnRuntimeTrafficAdmissionRecord, -) -> Result<()> { - require_supported_runtime_admission_pair(&record.daemon_id, path)?; - #[cfg(unix)] - require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; - record.validate()?; - validate_runtime_admission_filesystem(path, false)?; - let store = SingleFileMessagePackBackingStore::new(path); - let expected = store.pull_all_read_only_snapshot()?; - if !expected.is_empty() { - let existing = decode_runtime_traffic_admission_snapshot(&expected)?; - require_supported_runtime_admission_pair(&existing.daemon_id, path)?; - revoke_runtime_traffic_admission(path)?; - return Err(anyhow!( - "runtime traffic admission must be revoked before fresh publication" - )); - } - let envelope = CultCacheEnvelope { - key: record.daemon_id.clone(), - r#type: IdunnRuntimeTrafficAdmissionRecord::TYPE.into(), - payload: rmp_serde::to_vec(record)?, - stored_at: timestamp()?, - schema_id: Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into()), - }; - if !store.compare_exchange_snapshot(&expected, &[envelope])? { - revoke_runtime_traffic_admission(path)?; - return Err(anyhow!( - "runtime traffic admission changed during atomic publication" - )); - } - let verified = (|| -> Result<()> { - seal_runtime_admission_file(path)?; - validate_runtime_admission_filesystem(path, true)?; - let sealed = store.with_read_only_shared_snapshot(|entries| { - decode_runtime_traffic_admission_snapshot(&entries) - })?; - if &sealed != record { - return Err(anyhow!( - "sealed runtime traffic admission differs from the validated candidate" - )); - } - validate_runtime_admission_filesystem(path, true)?; - Ok(()) - })(); - if let Err(error) = verified { - let revoke = revoke_runtime_traffic_admission(path); - return match revoke { - Ok(()) => Err(error.context( - "runtime traffic admission publication failed and the candidate was revoked", - )), - Err(revoke_error) => Err(anyhow!( - "runtime traffic admission publication failed: {error:#}; fail-closed revocation also failed: {revoke_error:#}" - )), - }; - } - Ok(()) -} - -fn revoke_runtime_traffic_admission(path: &Path) -> Result<()> { - let _ = runtime_admission_policy_for_path(path)?; - #[cfg(unix)] - require_runtime_admission_writer_uid(unsafe { libc::geteuid() })?; - let store = SingleFileMessagePackBackingStore::new(path); - for _ in 0..8 { - let expected = store.pull_all_read_only_snapshot()?; - if expected.is_empty() { - seal_runtime_admission_file(path)?; - return Ok(()); - } - if store.compare_exchange_snapshot(&expected, &[])? { - seal_runtime_admission_file(path)?; - return Ok(()); - } - } - Err(anyhow!( - "runtime traffic admission could not be revoked after repeated contention" - )) -} - -fn runtime_admission_lock_path(path: &Path) -> PathBuf { - let mut name = path - .file_name() - .map(|value| value.to_os_string()) - .unwrap_or_else(|| "traffic-admission.cc".into()); - name.push(".lock"); - path.with_file_name(name) -} - -fn runtime_admission_custody_paths(path: &Path) -> [PathBuf; 2] { - [path.to_path_buf(), runtime_admission_lock_path(path)] -} - -#[cfg(unix)] -fn runtime_admission_group_id(group_name: &str) -> Result { - use std::ffi::CString; - - let group = CString::new(group_name).context("runtime traffic admission group contains NUL")?; - let entry = unsafe { libc::getgrnam(group.as_ptr()) }; - if entry.is_null() { - return Err(anyhow!( - "required runtime traffic admission group {} does not exist", - group_name - )); - } - Ok(unsafe { (*entry).gr_gid }) -} - -#[cfg(unix)] -fn validate_runtime_admission_filesystem(path: &Path, require_sealed_leaf: bool) -> Result<()> { - use std::os::unix::fs::MetadataExt; - - let (_, group_name) = runtime_admission_policy_for_path(path)?; - let group_id = runtime_admission_group_id(group_name)?; - let parent = path - .parent() - .ok_or_else(|| anyhow!("runtime traffic admission path has no parent"))?; - let mut component = Some(parent); - while let Some(directory) = component { - let metadata = fs::symlink_metadata(directory) - .with_context(|| format!("inspecting {}", directory.display()))?; - if metadata.file_type().is_symlink() || !metadata.is_dir() { - return Err(anyhow!( - "runtime traffic admission parent {} is not a direct directory", - directory.display() - )); - } - let mode = metadata.mode() & 0o7777; - if metadata.uid() != 0 || mode & 0o022 != 0 { - return Err(anyhow!( - "runtime traffic admission parent {} must be root-owned and not group/world writable", - directory.display() - )); - } - if directory == parent && (metadata.gid() != group_id || mode != 0o750) { - return Err(anyhow!( - "runtime traffic admission directory must be root:{} mode 0750", - group_name - )); - } - component = directory.parent(); - } - - for custody_path in runtime_admission_custody_paths(path) { - validate_runtime_admission_leaf(&custody_path, group_id, group_name, require_sealed_leaf)?; - } - Ok(()) -} - -#[cfg(unix)] -fn validate_runtime_admission_leaf( - path: &Path, - group_id: u32, - group_name: &str, - require_sealed_leaf: bool, -) -> Result<()> { - use std::os::unix::fs::MetadataExt; - - match fs::symlink_metadata(path) { - Ok(metadata) => { - if metadata.file_type().is_symlink() || !metadata.is_file() { - return Err(anyhow!( - "runtime traffic admission custody leaf {} must be a direct regular file", - path.display() - )); - } - let mode = metadata.mode() & 0o7777; - if metadata.uid() != 0 || mode & 0o022 != 0 { - return Err(anyhow!( - "runtime traffic admission custody leaf {} must be root-owned and not group/world writable", - path.display() - )); - } - if require_sealed_leaf && (metadata.gid() != group_id || mode != 0o640) { - return Err(anyhow!( - "runtime traffic admission custody leaf {} must be root:{} mode 0640", - path.display(), - group_name, - )); - } - } - Err(error) if error.kind() == std::io::ErrorKind::NotFound && !require_sealed_leaf => {} - Err(error) => { - return Err(error).with_context(|| { - format!("inspecting runtime traffic admission {}", path.display()) - }); - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn validate_runtime_admission_filesystem(_path: &Path, _require_sealed_leaf: bool) -> Result<()> { - Err(anyhow!( - "runtime traffic admission publication and validation require Unix ownership semantics" - )) -} - -#[cfg(unix)] -fn seal_runtime_admission_file(path: &Path) -> Result<()> { - let (_, group_name) = runtime_admission_policy_for_path(path)?; - let group_id = runtime_admission_group_id(group_name)?; - for custody_path in runtime_admission_custody_paths(path) { - seal_runtime_admission_leaf(&custody_path, group_id)?; - } - let parent = path - .parent() - .ok_or_else(|| anyhow!("runtime traffic admission path has no parent"))?; - File::open(parent) - .and_then(|directory| directory.sync_all()) - .with_context(|| format!("syncing admission parent {}", parent.display()))?; - Ok(()) -} - -#[cfg(unix)] -fn seal_runtime_admission_leaf(path: &Path, group_id: u32) -> Result<()> { - use std::os::fd::AsRawFd; - use std::os::unix::fs::PermissionsExt; - - let file = fs::OpenOptions::new() - .read(true) - .write(true) - .open(path) - .with_context(|| format!("opening admission {} for sealing", path.display()))?; - if unsafe { libc::fchown(file.as_raw_fd(), 0, group_id) } != 0 { - return Err(std::io::Error::last_os_error()) - .with_context(|| format!("setting owner on {}", path.display())); - } - file.set_permissions(fs::Permissions::from_mode(0o640)) - .with_context(|| format!("sealing mode on {}", path.display()))?; - file.sync_all() - .with_context(|| format!("syncing sealed admission {}", path.display()))?; - Ok(()) -} - -#[cfg(not(unix))] -fn seal_runtime_admission_file(_path: &Path) -> Result<()> { - Err(anyhow!( - "runtime traffic admission publication requires Unix ownership semantics" - )) -} - -fn validate_release_authority_at_privileged_boundary( - options: &ReleaseAuthorityValidationOptions, -) -> Result<()> { - let current = CultCacheReleaseAuthorityPort { - store_path: &options.store_path, - } - .authorize( - &options.repository_full_name, - &options.upstream_ref, - &options.source_revision, - ×tamp()?, - )?; - if current.authority_id != options.authority_id - || current.envelope_sha256 != options.envelope_sha256 - { - return Err(anyhow!( - "privileged deployment boundary received stale or substituted Bifrost authority" - )); - } - println!("Bifrost release authority validated for privileged deployment boundary."); - Ok(()) -} - -fn validate_minimum_source_revision_at_privileged_boundary( - options: &MinimumSourceRevisionValidationOptions, -) -> Result<()> { - let policy = compiled_minimum_source_revision_policy_for_daemon(&options.daemon_id) - .ok_or_else(|| anyhow!("daemon has no compiled minimum-source policy"))?; - require_minimum_source_revision( - Path::new(policy.repository_path), - &options.source_revision, - policy, - )?; - println!( - "compiled minimum source revision validated daemon={} sourceRevision={} floor={}", - options.daemon_id, options.source_revision, policy.minimum_source_revision - ); - Ok(()) -} - -fn run_swarm( - options: &SwarmOptions, - common: &CommonOptions, - projection_publisher: Option>, -) -> Result<()> { - let targets = swarm_targets(options)?; - if targets.is_empty() { - return Err(anyhow!( - "Idunn swarm profile {} resolved to no targets", - options.profile - )); - } - validate_targets(&targets)?; - - println!( - "Idunn swarm profile {} starting with {} targets.", - options.profile, - targets.len() - ); - println!("CultMesh store: {}", common.store_path.display()); - - let store_lock = Arc::new(Mutex::new(())); - let now = timestamp()?; - let compacted_health_statements = - compact_authenticated_health_statement_history(common, &store_lock)?; - if compacted_health_statements > 0 { - println!( - "Idunn compacted {compacted_health_statements} obsolete authenticated health statement(s)." - ); - } - let compacted_operational_records = compact_operational_history(common, &store_lock)?; - if compacted_operational_records > 0 { - println!( - "Idunn compacted {compacted_operational_records} obsolete operational history record(s)." - ); - } - let recovered = terminalize_interrupted_deployment_requests(common, &store_lock, &now)?; - if recovered > 0 { - println!( - "Idunn terminalized {recovered} deployment request(s) interrupted by the prior daemon incarnation." - ); - } - publish_runtime_transport_check(common, &store_lock, &now)?; - start_public_health_query_listener(common, &targets)?; - start_rudp_health_ingress(common, &store_lock, &now)?; - publish_surgery_plans(&options.profile, &targets, common, &store_lock, &now)?; - - // Health-driven and lifecycle-command workers are separate so observation - // remains responsive. One per-target gate serializes local actuation; the - // persistent request/result CAS chain remains the durable authority. - let actuation_gates = Arc::new( - targets - .iter() - .map(|target| { - ( - target.daemon_id.clone(), - Arc::new(TargetActuationGate::new()), - ) - }) - .collect::>(), - ); - - let command_targets = targets.clone(); - let command_common = common.clone(); - let command_store_lock = Arc::clone(&store_lock); - let command_actuation_gates = Arc::clone(&actuation_gates); - let mut workers = Vec::with_capacity(targets.len() + 1); - workers.push(thread::spawn(move || { - run_lifecycle_command_loop( - command_targets, - command_common, - command_store_lock, - command_actuation_gates, - ) - })); - for target in targets { - let worker_common = common.clone(); - let worker_store_lock = Arc::clone(&store_lock); - let worker_actuation_gate = actuation_gates - .get(&target.daemon_id) - .cloned() - .ok_or_else(|| anyhow!("Idunn target lost its actuation gate"))?; - let worker_projection_publisher = projection_publisher.clone(); - workers.push(thread::spawn(move || { - run_target_loop( - target, - worker_common, - worker_store_lock, - worker_actuation_gate, - worker_projection_publisher, - ) - })); - } - - for worker in workers { - match worker.join() { - Ok(Ok(())) => {} - Ok(Err(error)) => return Err(error), - Err(_) => return Err(anyhow!("Idunn swarm worker thread panicked")), - } - } - - Ok(()) -} - -fn run_lifecycle_command_loop( - targets: Vec, - options: CommonOptions, - store_lock: Arc>, - actuation_gates: Arc>>, -) -> Result<()> { - loop { - for target in &targets { - let gate = actuation_gates - .get(&target.daemon_id) - .ok_or_else(|| anyhow!("Idunn lifecycle target lost its actuation gate"))?; - if gate.reserved.swap(true, Ordering::AcqRel) { - continue; - } - let result = with_target_actuation_gate(&gate.lock, || { - process_pending_lifecycle_commands(target, &options, &store_lock) - }); - gate.reserved.store(false, Ordering::Release); - if let Err(error) = result { - eprintln!( - "Idunn lifecycle command cycle failed for {}: {}", - target.daemon_id, error - ); - } - } - thread::sleep(Duration::from_secs(2)); - } -} - -fn validate_targets(targets: &[DaemonTarget]) -> Result<()> { - let mut issues = Vec::new(); - for target in targets { - if target.health_contract.id.trim().is_empty() { - issues.push(format!("{} has no health contract", target.daemon_id)); - } - if target.health_contract.default_failure_state == "stale-deployment" - && target.deploy_command.is_none() - { - issues.push(format!( - "{} treats probe failure as stale deployment but has no deploy command", - target.daemon_id - )); - } - if let Some(release) = target.release.as_ref() { - let compiled = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id); - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - if release.minimum_source_revision.as_deref() - != compiled.map(|policy| policy.minimum_source_revision) - || compiled.is_some_and(|policy| { - release.repository_full_name != policy.repository_full_name - || upstream_ref != policy.upstream_ref - || release.repo_path != PathBuf::from(policy.repository_path) - }) - { - issues.push(format!( - "{} differs from its compiled minimum-source policy", - target.daemon_id - )); - } - } - } - - if issues.is_empty() { - Ok(()) - } else { - Err(anyhow!( - "Idunn target catalog is incoherent: {}", - issues.join("; ") - )) - } -} - -fn run_target_loop( - target: DaemonTarget, - options: CommonOptions, - store_lock: Arc>, - actuation_gate: Arc, - projection_publisher: Option>, -) -> Result<()> { - let mut missing_since = None; - loop { - if let Err(error) = run_target_cycle( - &target, - &options, - &store_lock, - Arc::clone(&actuation_gate), - projection_publisher.as_deref(), - &mut missing_since, - ) { - eprintln!( - "Idunn swarm target {} cycle failed: {}", - target.daemon_id, error - ); - } - thread::sleep(Duration::from_secs(target.interval_seconds)); - } -} - -fn with_target_actuation_gate( - gate: &Mutex<()>, - action: impl FnOnce() -> Result, -) -> Result { - let _actuation = gate - .lock() - .map_err(|_| anyhow!("Idunn target actuation gate is poisoned"))?; - action() -} - -fn run_target_cycle( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, - actuation_gate: Arc, - projection_publisher: Option<&IdunnProjectionPublisher>, - missing_since: &mut Option, -) -> Result<()> { - compact_operational_history_if_needed(options, store_lock)?; - let now = timestamp()?; - - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: target.enabled, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(target), - command_boundary_id: command_boundary_id(target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: now.clone(), - }; - - let (mut health_key, mut health, mut projection_source) = - evaluate_target_health(target, options, store_lock, &desired, &now)?; - let health_authenticated = - projection_source.is_some() || health.publication_source == "daemon-published"; - apply_missing_publication_grace( - target, - &desired, - &mut health, - health_authenticated, - missing_since, - Instant::now(), - ); - let mut plan = plan_keepalive(&desired, &health, now.clone()); - if plan.restart_request.is_some() { - let veto_now = timestamp()?; - if let Some(fresh_health) = - read_fresh_daemon_published_health(options, store_lock, &desired, &veto_now)? - { - if health_state_is_healthy(&fresh_health.health.state) { - health_key = desired.daemon_id.clone(); - health = fresh_health.health; - projection_source = fresh_health.projection_source; - *missing_since = None; - plan = plan_keepalive(&desired, &health, veto_now); - } - } - } - if let Some(request) = plan.deployment_request.as_mut() { - match authorize_release(target, options, store_lock) { - Ok(authorization) => apply_release_authorization(request, &authorization), - Err(error) => { - let reason = format!("deployment refused: {error:#}"); - plan.deployment_request = None; - plan.decision.action = "alarm".to_string(); - plan.decision.reason = reason.clone(); - plan.operator_alarm = Some(IdunnOperatorAlarmRecord { - alarm_id: format!("alarm:{}:{}", target.daemon_id, now), - daemon_id: target.daemon_id.clone(), - severity: "operator-action-required".to_string(), - reason, - escalation_target: "bifrost.operator-notification".to_string(), - raised_at: now.clone(), - }); - } - } - } - - with_store_node(options, store_lock, |node| { - let transport_profile = daemon_transport_profile(target, &now); - let command_boundary = command_boundary(target, &now); - node.put(&transport_profile.profile_id, &transport_profile)?; - node.put(&command_boundary.boundary_id, &command_boundary)?; - node.put(&desired.daemon_id, &desired)?; - node.put(&health_key, &health)?; - node.put(&plan.decision.decision_id, &plan.decision)?; - if let Some(request) = &plan.restart_request { - node.put(&request.request_id, request)?; - } - if let Some(alarm) = &plan.operator_alarm { - node.put(&alarm.alarm_id, alarm)?; - } - Ok(()) - })?; - - publish_authenticated_provider_health( - projection_publisher, - options, - store_lock, - &desired, - projection_source.as_ref(), - &now, - )?; - - if plan.deployment_request.is_some() || plan.restart_request.is_some() { - schedule_automatic_actuation( - target.clone(), - options.clone(), - Arc::clone(store_lock), - actuation_gate, - plan, - now, - ); - return Ok(()); - } - - finish_target_cycle(target, options, store_lock, plan, &now) -} - -fn schedule_automatic_actuation( - target: DaemonTarget, - options: CommonOptions, - store_lock: Arc>, - gate: Arc, - plan: IdunnPlan, - now: String, -) { - let daemon_id = target.daemon_id.clone(); - schedule_target_actuation(gate, daemon_id, move || { - finish_target_cycle(&target, &options, &store_lock, plan, &now) - }); -} - -fn schedule_target_actuation( - gate: Arc, - daemon_id: String, - action: impl FnOnce() -> Result<()> + Send + 'static, -) { - if gate - .reserved - .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire) - .is_err() - { - return; - } - thread::spawn(move || { - let result = with_target_actuation_gate(&gate.lock, action); - gate.reserved.store(false, Ordering::Release); - if let Err(error) = result { - eprintln!( - "Idunn automatic actuation failed for {}: {}", - daemon_id, error - ); - } - }); -} - -fn finish_target_cycle( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, - plan: IdunnPlan, - now: &str, -) -> Result<()> { - if let Some(request) = &plan.deployment_request { - persist_current_deployment_request(options, store_lock, request)?; - } - - if let Some(request) = &plan.deployment_request { - if options.execute { - let authority_error = revalidate_deployment_request(request, options, store_lock).err(); - let migration_result = if authority_error.is_none() { - target - .release - .as_ref() - .and_then(|release| run_state_migration(target, release, request, now, options)) - } else { - None - }; - if let Some(result) = &migration_result { - with_store_node(options, store_lock, |node| { - node.put(&result.result_id, result)?; - Ok(()) - })?; - } - let migration_failed = migration_result - .as_ref() - .is_some_and(|result| result.state != "succeeded" && result.state != "noop"); - let mut result = if let Some(error) = authority_error { - IdunnDeploymentResultRecord { - result_id: format!("result:{}", request.request_id), - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: format!( - "deployment authority revalidation failed before migration: {error:#}" - ), - completed_at: now.to_string(), - } - } else if migration_failed { - IdunnDeploymentResultRecord { - result_id: format!("result:{}", request.request_id), - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: "state migration failed; deployment command was not run".to_string(), - completed_at: now.to_string(), - } - } else { - run_deployment(request, now, options, store_lock) - }; - if result.state == "succeeded" - && let Some(release) = target.release.as_ref() - && release.deployed_revision_witness.is_some() - && let Err(error) = verify_release_witness_current(release, &SystemReleaseStatePort) - { - result.state = "failed".to_string(); - result.detail = format!( - "deployment command exited successfully, but its deployed revision witness did not converge: {error:#}" - ); - } - let rollout_result = target.release.as_ref().map(|release| { - rollout_result_record(target, release, &result, migration_result.as_ref(), now) - }); - let alarm = if result.state != "succeeded" { - Some(deployment_failure_alarm(&result, now)) - } else { - None - }; - with_store_node(options, store_lock, |node| { - node.put(&result.result_id, &result)?; - if let Some(result) = &rollout_result { - node.put(&result.result_id, result)?; - } - if let Some(alarm) = &alarm { - node.put(&alarm.alarm_id, alarm)?; - } - Ok(()) - })?; - println!( - "Idunn deployment {} for {}: {}", - result.state, result.daemon_id, result.detail - ); - if let Some(alarm) = alarm { - println!( - "Idunn raised operator alarm for {} through {}: {}", - alarm.daemon_id, alarm.escalation_target, alarm.reason - ); - run_operator_alarm_command(options, &alarm); - } - } else { - println!( - "Idunn requested deployment for {} but did not execute it. Pass --execute to actuate.", - request.daemon_id - ); - } - } - - if let Some(request) = &plan.restart_request { - if options.execute { - let result = run_restart(request, now, options); - let alarm = if result.state != "succeeded" { - Some(restart_failure_alarm(&result, now)) - } else { - None - }; - with_store_node(options, store_lock, |node| { - node.put(&result.result_id, &result)?; - if let Some(alarm) = &alarm { - node.put(&alarm.alarm_id, alarm)?; - } - Ok(()) - })?; - println!( - "Idunn restart {} for {}: {}", - result.state, result.daemon_id, result.detail - ); - if let Some(alarm) = alarm { - println!( - "Idunn raised operator alarm for {} through {}: {}", - alarm.daemon_id, alarm.escalation_target, alarm.reason - ); - run_operator_alarm_command(options, &alarm); - } - } else { - println!( - "Idunn requested restart for {} but did not execute it. Pass --execute to actuate.", - request.daemon_id - ); - } - } - - if let Some(alarm) = &plan.operator_alarm { - println!( - "Idunn raised operator alarm for {} through {}: {}", - alarm.daemon_id, alarm.escalation_target, alarm.reason - ); - run_operator_alarm_command(options, alarm); - } - - println!( - "Idunn decision for {}: {} ({})", - plan.decision.daemon_id, plan.decision.action, plan.decision.reason - ); - Ok(()) -} - -fn process_pending_lifecycle_commands( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, -) -> Result<()> { - let pending = with_store_node(options, store_lock, |node| { - let mut commands = node.cache().get_all::()?; - commands.retain(|command| { - command.daemon_id == target.daemon_id - && command.state == "pending" - && matches!(command.action.as_str(), "restart" | "redeploy") - }); - commands.sort_by(|left, right| left.requested_at.cmp(&right.requested_at)); - Ok(commands) - })?; - - for command in pending { - process_lifecycle_command(target, options, store_lock, command)?; - } - - Ok(()) -} - -fn process_lifecycle_command( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, - command: IdunnLifecycleCommandRecord, -) -> Result<()> { - let claimed_at = timestamp()?; - match command.action.as_str() { - "restart" => { - let Some(shell_command) = target.restart_command.as_deref() else { - reject_lifecycle_command( - options, - store_lock, - command, - &claimed_at, - "restart authority is not configured for this target", - )?; - return Ok(()); - }; - let request = IdunnRestartRequestRecord { - request_id: format!("manual:restart:{}:{}", target.daemon_id, command.command_id), - daemon_id: target.daemon_id.clone(), - command: shell_command.to_string(), - authority: "idunn-supervisor-command.manual".to_string(), - requested_at: claimed_at.clone(), - }; - with_store_node(options, store_lock, |node| { - let mut running = command.clone(); - running.state = "running".to_string(); - running.claimed_at = claimed_at.clone(); - node.put(&running.command_id, &running)?; - node.put(&request.request_id, &request)?; - Ok(()) - })?; - let result = if options.execute { - run_restart(&request, &claimed_at, options) - } else { - IdunnRestartResultRecord { - result_id: format!("result:{}", request.request_id), - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "skipped".to_string(), - detail: "manual restart command was claimed but --execute is not enabled" - .to_string(), - completed_at: claimed_at.clone(), - } - }; - with_store_node(options, store_lock, |node| { - node.put(&result.result_id, &result)?; - let mut completed = command.clone(); - completed.state = result.state.clone(); - completed.claimed_at = claimed_at.clone(); - completed.result_id = result.result_id.clone(); - node.put(&completed.command_id, &completed)?; - Ok(()) - })?; - println!( - "Idunn manual restart {} for {}: {}", - result.state, result.daemon_id, result.detail - ); - } - "redeploy" => { - let Some(shell_command) = target.deploy_command.as_deref() else { - reject_lifecycle_command( - options, - store_lock, - command, - &claimed_at, - "redeploy authority is not configured for this target", - )?; - return Ok(()); - }; - let authorization = match authorize_release(target, options, store_lock) { - Ok(value) => value, - Err(error) => { - reject_lifecycle_command( - options, - store_lock, - command, - &claimed_at, - &format!("redeploy authority refused: {error:#}"), - )?; - return Ok(()); - } - }; - let mut request = IdunnDeploymentRequestRecord { - request_id: format!( - "manual:redeploy:{}:{}", - target.daemon_id, command.command_id - ), - daemon_id: target.daemon_id.clone(), - command: shell_command.to_string(), - authority: "idunn-supervisor-command.manual".to_string(), - requested_at: claimed_at.clone(), - repository_full_name: String::new(), - upstream_ref: String::new(), - source_revision: String::new(), - release_authority_id: String::new(), - release_authority_envelope_sha256: String::new(), - requires_bifrost_authority: false, - }; - apply_release_authorization(&mut request, &authorization); - with_store_node(options, store_lock, |node| { - let mut running = command.clone(); - running.state = "running".to_string(); - running.claimed_at = claimed_at.clone(); - node.put(&running.command_id, &running)?; - Ok(()) - })?; - persist_current_deployment_request(options, store_lock, &request)?; - let result = if options.execute { - run_deployment(&request, &claimed_at, options, store_lock) - } else { - IdunnDeploymentResultRecord { - result_id: format!("result:{}", request.request_id), - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "skipped".to_string(), - detail: "manual redeploy command was claimed but --execute is not enabled" - .to_string(), - completed_at: claimed_at.clone(), - } - }; - with_store_node(options, store_lock, |node| { - node.put(&result.result_id, &result)?; - let mut completed = command.clone(); - completed.state = result.state.clone(); - completed.claimed_at = claimed_at.clone(); - completed.result_id = result.result_id.clone(); - node.put(&completed.command_id, &completed)?; - Ok(()) - })?; - println!( - "Idunn manual redeploy {} for {}: {}", - result.state, result.daemon_id, result.detail - ); - } - _ => reject_lifecycle_command( - options, - store_lock, - command, - &claimed_at, - "unknown lifecycle command action", - )?, - } - - Ok(()) -} - -fn reject_lifecycle_command( - options: &CommonOptions, - store_lock: &Arc>, - mut command: IdunnLifecycleCommandRecord, - claimed_at: &str, - detail: &str, -) -> Result<()> { - command.state = "rejected".to_string(); - command.claimed_at = claimed_at.to_string(); - command.detail = if command.detail.trim().is_empty() { - detail.to_string() - } else { - format!("{}; {}", command.detail, detail) - }; - with_store_node(options, store_lock, |node| { - node.put(&command.command_id, &command)?; - Ok(()) - }) -} - -fn with_store_node( - options: &CommonOptions, - store_lock: &Arc>, - write: F, -) -> Result -where - F: FnOnce(&mut CultMeshNode) -> Result, -{ - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let mut node = CultMesh::create_node( - &options.store_path, - OdinDocuments, - CultMeshNodeOptions { - runtime_id: "idunn-daemon".to_string(), - pull_on_start: true, - }, - )?; - write(&mut node) -} - -fn persist_current_deployment_request( - options: &CommonOptions, - store_lock: &Arc>, - request: &IdunnDeploymentRequestRecord, -) -> Result { - for _ in 0..8 { - let ( - current_head, - current_head_envelope, - current_head_request_envelope, - current_head_result_envelope, - current_request_envelope, - ) = with_store_node(options, store_lock, |node| { - let head = node.get::(&request.daemon_id)?; - Ok(( - head.clone(), - node.cache() - .get_envelope::(&request.daemon_id)?, - head.as_ref() - .map(|head| { - node.cache() - .get_envelope::(&head.request_id) - }) - .transpose()? - .flatten(), - head.as_ref() - .map(|head| { - node.cache() - .get_envelope::(&format!( - "result:{}", - head.request_id - )) - }) - .transpose()? - .flatten(), - node.cache() - .get_envelope::(&request.request_id)?, - )) - })?; - if let Some(existing) = current_request_envelope.as_ref() { - let decoded: IdunnDeploymentRequestRecord = rmp_serde::from_slice(&existing.payload)?; - if decoded != *request { - return Err(anyhow!("deployment request identity collision")); - } - if let Some(head) = current_head.as_ref() - && head.request_id == request.request_id - { - return Ok(head.clone()); - } - return Err(anyhow!( - "existing deployment request is not the current authority head" - )); - } - if let Some(current) = current_head.as_ref() { - let prior_request_envelope = current_head_request_envelope - .as_ref() - .ok_or_else(|| anyhow!("current deployment head lost its exact request"))?; - let prior_request: IdunnDeploymentRequestRecord = - rmp_serde::from_slice(&prior_request_envelope.payload)?; - if prior_request.request_id != current.request_id - || prior_request.daemon_id != current.daemon_id - { - return Err(anyhow!( - "current deployment head request identity is substituted" - )); - } - let prior_result_envelope = current_head_result_envelope.as_ref().ok_or_else(|| { - anyhow!( - "current deployment request {} remains live; refusing supersession", - current.request_id - ) - })?; - let prior_result: IdunnDeploymentResultRecord = - rmp_serde::from_slice(&prior_result_envelope.payload)?; - if prior_result.result_id != format!("result:{}", current.request_id) - || prior_result.request_id != current.request_id - || prior_result.daemon_id != current.daemon_id - || !matches!( - prior_result.state.as_str(), - "succeeded" | "failed" | "skipped" - ) - { - return Err(anyhow!( - "current deployment request lacks an exact terminal result" - )); - } - } - let head = IdunnCurrentDeploymentRequestRecord { - daemon_id: request.daemon_id.clone(), - request_id: request.request_id.clone(), - sequence: current_head - .as_ref() - .map_or(1, |head| head.sequence.saturating_add(1)), - updated_at: request.requested_at.clone(), - }; - let mut expected = vec![ - CultCacheExpectedEnvelope { - key: request.daemon_id.clone(), - r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), - current: current_head_envelope, - }, - CultCacheExpectedEnvelope { - key: request.request_id.clone(), - r#type: IdunnDeploymentRequestRecord::TYPE.into(), - current: None, - }, - ]; - if let Some(current) = current_head.as_ref() { - expected.push(CultCacheExpectedEnvelope { - key: current.request_id.clone(), - r#type: IdunnDeploymentRequestRecord::TYPE.into(), - current: current_head_request_envelope, - }); - expected.push(CultCacheExpectedEnvelope { - key: format!("result:{}", current.request_id), - r#type: IdunnDeploymentResultRecord::TYPE.into(), - current: current_head_result_envelope, - }); - } - if SingleFileMessagePackBackingStore::new(&options.store_path).compare_exchange( - &expected, - &[ - typed_envelope(&request.request_id, request, &request.requested_at)?, - typed_envelope(&head.daemon_id, &head, &request.requested_at)?, - ], - )? { - return Ok(head); - } - } - Err(anyhow!( - "deployment request head lost repeated cross-process contention" - )) -} - -/// On daemon startup no deployment actuator from the prior process can still -/// be an Idunn-owned child. Close any exact current request that lacks a result -/// before this incarnation launches workers. The current head remains durable -/// history; its exact failed result permits ordinary CAS supersession. -fn terminalize_interrupted_deployment_requests( - options: &CommonOptions, - store_lock: &Arc>, - completed_at: &str, -) -> Result { - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let backing = SingleFileMessagePackBackingStore::new(&options.store_path); - let opening = backing.pull_all()?; - let heads = opening - .iter() - .filter(|entry| entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE) - .collect::>(); - let mut recovered = 0; - for head_envelope in heads { - let head: IdunnCurrentDeploymentRequestRecord = - rmp_serde::from_slice(&head_envelope.payload)?; - if head.daemon_id.trim().is_empty() - || head.request_id.trim().is_empty() - || head.sequence == 0 - || head_envelope.key != head.daemon_id - { - return Err(anyhow!( - "current deployment request head is invalid during startup recovery" - )); - } - let request_matches = opening - .iter() - .filter(|entry| { - entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == head.request_id - }) - .collect::>(); - if request_matches.len() != 1 { - return Err(anyhow!( - "current deployment request {} lacks one exact request during startup recovery", - head.request_id - )); - } - let request_envelope = request_matches[0]; - let request: IdunnDeploymentRequestRecord = - rmp_serde::from_slice(&request_envelope.payload)?; - if request.request_id != head.request_id || request.daemon_id != head.daemon_id { - return Err(anyhow!( - "current deployment request is substituted during startup recovery" - )); - } - let result_key = format!("result:{}", head.request_id); - let result_matches = opening - .iter() - .filter(|entry| { - entry.r#type == IdunnDeploymentResultRecord::TYPE && entry.key == result_key - }) - .collect::>(); - if result_matches.len() > 1 { - return Err(anyhow!( - "current deployment request has duplicate results during startup recovery" - )); - } - if let Some(result_envelope) = result_matches.first() { - let result: IdunnDeploymentResultRecord = - rmp_serde::from_slice(&result_envelope.payload)?; - if result.result_id != result_key - || result.request_id != head.request_id - || result.daemon_id != head.daemon_id - || !matches!(result.state.as_str(), "succeeded" | "failed" | "skipped") - { - return Err(anyhow!( - "current deployment request lacks an exact terminal result during startup recovery" - )); - } - continue; - } - let result = IdunnDeploymentResultRecord { - result_id: result_key.clone(), - request_id: head.request_id.clone(), - daemon_id: head.daemon_id.clone(), - state: "failed".to_string(), - detail: "deployment actuator was interrupted when the owning Idunn daemon incarnation stopped" - .to_string(), - completed_at: completed_at.to_string(), - }; - let expected = [ - CultCacheExpectedEnvelope { - key: head.daemon_id.clone(), - r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), - current: Some(head_envelope.clone()), - }, - CultCacheExpectedEnvelope { - key: head.request_id.clone(), - r#type: IdunnDeploymentRequestRecord::TYPE.into(), - current: Some(request_envelope.clone()), - }, - CultCacheExpectedEnvelope { - key: result_key.clone(), - r#type: IdunnDeploymentResultRecord::TYPE.into(), - current: None, - }, - ]; - if !backing.compare_exchange( - &expected, - &[typed_envelope(&result_key, &result, completed_at)?], - )? { - return Err(anyhow!( - "interrupted deployment request changed during startup recovery" - )); - } - recovered += 1; - } - Ok(recovered) -} - -fn release_authority_id( - repository_full_name: &str, - upstream_ref: &str, - commit_sha: &str, -) -> String { - format!("release:{repository_full_name}:{upstream_ref}:{commit_sha}") -} - -fn validate_commit_sha(value: &str) -> Result<()> { - if value.len() != 40 - || !value - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) - { - return Err(anyhow!( - "release source revision must be a lowercase 40-hex Git commit" - )); - } - Ok(()) -} - -fn compiled_minimum_source_revision_policy( - repository_full_name: &str, - upstream_ref: &str, -) -> Option<&'static CompiledMinimumSourceRevisionPolicy> { - COMPILED_MINIMUM_SOURCE_REVISION_POLICIES - .iter() - .find(|policy| { - policy.repository_full_name == repository_full_name - && policy.upstream_ref == upstream_ref - }) -} - -fn compiled_minimum_source_revision_policy_for_daemon( - daemon_id: &str, -) -> Option<&'static CompiledMinimumSourceRevisionPolicy> { - COMPILED_MINIMUM_SOURCE_REVISION_POLICIES - .iter() - .find(|policy| policy.daemon_id == daemon_id) -} - -fn require_release_minimum_source_revision( - target: &DaemonTarget, - release: &ReleaseTarget, - candidate_revision: &str, -) -> Result<()> { - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - let compiled = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id); - let declared = release.minimum_source_revision.as_deref(); - if declared != compiled.map(|policy| policy.minimum_source_revision) { - return Err(anyhow!( - "release target minimum source policy differs from Idunn's compiled authority" - )); - } - let Some(policy) = compiled else { - return Ok(()); - }; - if release.repository_full_name != policy.repository_full_name - || upstream_ref != policy.upstream_ref - || release.repo_path != PathBuf::from(policy.repository_path) - { - return Err(anyhow!( - "release target identity differs from Idunn's compiled minimum-source policy" - )); - } - require_deployment_source_revision(&release.repo_path, candidate_revision, policy) -} - -fn require_compiled_minimum_source_revision( - daemon_id: &str, - repository_full_name: &str, - upstream_ref: &str, - candidate_revision: &str, -) -> Result> { - let Some(policy) = compiled_minimum_source_revision_policy_for_daemon(daemon_id) else { - return Ok(None); - }; - if repository_full_name != policy.repository_full_name || upstream_ref != policy.upstream_ref { - return Err(anyhow!( - "deployment identity differs from Idunn's compiled minimum-source policy" - )); - } - require_deployment_source_revision( - Path::new(policy.repository_path), - candidate_revision, - policy, - )?; - Ok(Some(policy)) -} - -fn require_minimum_source_revision( - repository_path: &Path, - candidate_revision: &str, - policy: &CompiledMinimumSourceRevisionPolicy, -) -> Result<()> { - require_compiled_repository_origin(repository_path, policy)?; - require_revision_floor_ancestry( - repository_path, - candidate_revision, - policy.minimum_source_revision, - ) -} - -fn require_deployment_source_revision( - repository_path: &Path, - candidate_revision: &str, - policy: &CompiledMinimumSourceRevisionPolicy, -) -> Result<()> { - require_minimum_source_revision(repository_path, candidate_revision, policy)?; - require_canonical_fetched_dag_membership( - repository_path, - candidate_revision, - policy.canonical_fetched_ref, - ) -} - -fn require_compiled_repository_origin( - repository_path: &Path, - policy: &CompiledMinimumSourceRevisionPolicy, -) -> Result<()> { - let output = Command::new("git") - .arg("-C") - .arg(repository_path) - .arg("remote") - .arg("get-url") - .arg("--all") - .arg("origin") - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .output() - .with_context(|| { - format!( - "reading compiled-policy repository origin in {}", - repository_path.display() - ) - })?; - if !output.status.success() { - return Err(anyhow!( - "compiled-policy repository has no readable canonical origin" - )); - } - let origin_output = String::from_utf8(output.stdout) - .context("compiled-policy repository origin is not UTF-8")?; - let urls = origin_output - .lines() - .map(str::trim) - .filter(|url| !url.is_empty()) - .collect::>(); - if urls.len() != 1 || urls[0] != policy.canonical_repository_url { - return Err(anyhow!( - "compiled-policy repository origin differs from Idunn's fixed canonical repository" - )); - } - Ok(()) -} - -fn require_revision_floor_ancestry( - repository_path: &Path, - candidate_revision: &str, - minimum_revision: &str, -) -> Result<()> { - validate_commit_sha(candidate_revision)?; - validate_commit_sha(minimum_revision)?; - if git_revision(repository_path, candidate_revision).as_deref() != Some(candidate_revision) { - return Err(anyhow!( - "candidate source revision is absent from the compiled-policy repository" - )); - } - if git_revision(repository_path, minimum_revision).as_deref() != Some(minimum_revision) { - return Err(anyhow!( - "compiled minimum source revision is absent from its fixed repository" - )); - } - let status = Command::new("git") - .arg("-C") - .arg(repository_path) - .arg("merge-base") - .arg("--is-ancestor") - .arg(minimum_revision) - .arg(candidate_revision) - .status() - .with_context(|| { - format!( - "checking compiled minimum source revision in {}", - repository_path.display() - ) - })?; - if !status.success() { - return Err(anyhow!( - "candidate source revision is below or unrelated to Idunn's compiled minimum" - )); - } - Ok(()) -} - -fn require_canonical_fetched_dag_membership( - repository_path: &Path, - candidate_revision: &str, - canonical_fetched_ref: &str, -) -> Result<()> { - validate_commit_sha(candidate_revision)?; - if git_revision(repository_path, candidate_revision).as_deref() != Some(candidate_revision) { - return Err(anyhow!( - "candidate source revision is absent from the compiled-policy repository" - )); - } - if git_revision(repository_path, canonical_fetched_ref).is_none() { - return Err(anyhow!( - "compiled canonical fetched ref is absent from its fixed repository" - )); - } - let canonical_status = Command::new("git") - .arg("-C") - .arg(repository_path) - .arg("merge-base") - .arg("--is-ancestor") - .arg(candidate_revision) - .arg(canonical_fetched_ref) - .status() - .with_context(|| { - format!( - "checking candidate membership in canonical fetched DAG at {}", - repository_path.display() - ) - })?; - if !canonical_status.success() { - return Err(anyhow!( - "candidate source revision is not in Idunn's canonical fetched DAG" - )); - } - Ok(()) -} - -fn validate_release_authority_receipt( - receipt: &BifrostRepositoryReleaseAuthorityRecord, - expected_id: &str, - repository_full_name: &str, - upstream_ref: &str, - source_revision: &str, - now: &str, -) -> Result<()> { - if receipt.authority_id != expected_id - || receipt.repository_full_name != repository_full_name - || receipt.upstream_ref != upstream_ref - || receipt.commit_sha != source_revision - { - return Err(anyhow!( - "Bifrost release authority identity/repository/ref/commit mismatch" - )); - } - if receipt.decision != "authorize" || receipt.status != "authorized" { - return Err(anyhow!( - "Bifrost release authority is not currently authorized" - )); - } - if receipt.command_id.trim().is_empty() - || receipt.crossing_receipt_id.trim().is_empty() - || receipt.policy_decision_id.trim().is_empty() - || receipt.authority_reference.trim().is_empty() - || receipt.actor_identity.trim().is_empty() - || receipt.source_kind.trim().is_empty() - || receipt.source_id.trim().is_empty() - || receipt.external_receipt_url.trim().is_empty() - || receipt.external_receipt_id.trim().is_empty() - || receipt.authorized_at.trim().is_empty() - { - return Err(anyhow!( - "Bifrost release authority provenance is incomplete" - )); - } - if !receipt.revoked_at.trim().is_empty() || !receipt.revocation_reason.trim().is_empty() { - return Err(anyhow!( - "Bifrost release authority carries revocation facts" - )); - } - let expected_external_url = - format!("https://github.com/{repository_full_name}/commit/{source_revision}"); - if receipt.external_receipt_id != source_revision - || receipt.external_receipt_url != expected_external_url - { - return Err(anyhow!( - "Bifrost release authority external GitHub proof does not bind the exact commit" - )); - } - chrono::DateTime::parse_from_rfc3339(&receipt.authorized_at) - .context("Bifrost release authority authorizedAt must be RFC3339")?; - if !receipt.expires_at.trim().is_empty() { - let expires = chrono::DateTime::parse_from_rfc3339(&receipt.expires_at) - .context("Bifrost release authority expiresAt must be RFC3339")? - .timestamp(); - let now_seconds = now - .strip_prefix("unix:") - .ok_or_else(|| anyhow!("Idunn authority clock is not unix-seconds"))? - .parse::() - .context("Idunn authority clock is invalid")?; - if expires <= now_seconds { - return Err(anyhow!("Bifrost release authority has expired")); - } - } - Ok(()) -} - -fn authorize_release( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, -) -> Result { - let release = target - .release - .as_ref() - .ok_or_else(|| anyhow!("deployment target has no release declaration"))?; - let state = SystemReleaseStatePort; - if let Some(policy) = compiled_minimum_source_revision_policy_for_daemon(&target.daemon_id) { - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - if release.repository_full_name != policy.repository_full_name - || upstream_ref != policy.upstream_ref - || release.repo_path != PathBuf::from(policy.repository_path) - || release.minimum_source_revision.as_deref() != Some(policy.minimum_source_revision) - { - return Err(anyhow!( - "release target identity differs from Idunn's compiled minimum-source policy" - )); - } - require_compiled_repository_origin(&release.repo_path, policy)?; - } - state.fetch(release)?; - let observed_upstream_revision = state.desired_revision(release)?; - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - if !release.requires_bifrost_authority { - validate_commit_sha(&observed_upstream_revision)?; - require_release_minimum_source_revision(target, release, &observed_upstream_revision)?; - return Ok(ReleaseAuthorization { - repository_full_name: release.repository_full_name.clone(), - upstream_ref, - source_revision: observed_upstream_revision, - authority_id: String::new(), - envelope_sha256: String::new(), - requires_bifrost_authority: false, - }); - } - let now = timestamp()?; - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let authority_store = options - .release_authority_store_path - .as_deref() - .ok_or_else(|| anyhow!("--release-authority-store is required for deployment authority"))?; - let selected = CultCacheReleaseAuthorityPort { - store_path: authority_store, - } - .select(&release.repository_full_name, &upstream_ref, &now)?; - if git_revision(&release.repo_path, &selected.source_revision).as_deref() - != Some(selected.source_revision.as_str()) - { - return Err(anyhow!( - "authorized Bifrost release commit is absent from fetched repository: {}", - selected.source_revision - )); - } - require_release_minimum_source_revision(target, release, &selected.source_revision)?; - Ok(selected) -} - -fn apply_release_authorization( - request: &mut IdunnDeploymentRequestRecord, - authorization: &ReleaseAuthorization, -) { - request.repository_full_name = authorization.repository_full_name.clone(); - request.upstream_ref = authorization.upstream_ref.clone(); - request.source_revision = authorization.source_revision.clone(); - request.release_authority_id = authorization.authority_id.clone(); - request.release_authority_envelope_sha256 = authorization.envelope_sha256.clone(); - request.requires_bifrost_authority = authorization.requires_bifrost_authority; -} - -fn revalidate_deployment_request( - request: &IdunnDeploymentRequestRecord, - options: &CommonOptions, - store_lock: &Arc>, -) -> Result<()> { - validate_commit_sha(&request.source_revision)?; - require_compiled_minimum_source_revision( - &request.daemon_id, - &request.repository_full_name, - &request.upstream_ref, - &request.source_revision, - )?; - if !request.requires_bifrost_authority { - if !request.release_authority_id.is_empty() - || !request.release_authority_envelope_sha256.is_empty() - { - return Err(anyhow!( - "source-observed release request cannot carry Bifrost authority claims" - )); - } - return Ok(()); - } - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let authority_store = options - .release_authority_store_path - .as_deref() - .ok_or_else(|| anyhow!("--release-authority-store is required for deployment authority"))?; - let current = CultCacheReleaseAuthorityPort { - store_path: authority_store, - } - .authorize( - &request.repository_full_name, - &request.upstream_ref, - &request.source_revision, - ×tamp()?, - )?; - if current.authority_id != request.release_authority_id - || current.envelope_sha256 != request.release_authority_envelope_sha256 - { - return Err(anyhow!( - "deployment request release authority changed after planning" - )); - } - Ok(()) -} - -fn read_fresh_daemon_published_health( - options: &CommonOptions, - store_lock: &Arc>, - desired: &IdunnDesiredDaemonRecord, - now: &str, -) -> Result> { - with_store_node(options, store_lock, |node| { - let Some(health) = node.get::(&desired.daemon_id)? else { - return Ok(None); - }; - if !is_fresh_daemon_published_health(&health, desired, now) { - return Ok(None); - } - match health.publication_source.as_str() { - "daemon-authenticated" => { - let Some(admission) = - node.get::(&desired.daemon_id)? - else { - return Ok(None); - }; - admission.validate()?; - let Some(statement_envelope) = node - .cache() - .get_envelope::(&admission.daemon_id)? - else { - return Ok(None); - }; - let (statement, _) = - decode_generic_signed_health_payload(&statement_envelope.payload)?; - statement.validate()?; - let (binding, current_binding_sha256) = - load_daemon_health_trust_binding(options, &statement)?; - validate_generic_release_binding(node, &admission)?; - let now_millis = parse_timestamp_millis(now) - .ok_or_else(|| anyhow!("managed health clock is invalid"))?; - let statement_digest = - format!("sha256-{:x}", Sha256::digest(&statement_envelope.payload)); - if admission.daemon_id != health.daemon_id - || admission.daemon_id != statement.daemon_id - || admission.health_contract != health.health_contract - || admission.health_contract != statement.health_contract - || admission.source_runtime_id != statement.source_runtime_id - || admission.state != health.state - || admission.state != statement.state - || statement.detail != health.detail - || admission.trust_binding_id != binding.binding_id - || admission.trust_binding_sha256 != current_binding_sha256 - || admission.signed_health_sha256 != statement_digest - || admission.signer_identity_id != statement.signer_identity_id - || admission.publisher_incarnation_id != statement.publisher_incarnation_id - || admission.publisher_sequence != statement.publisher_sequence - || admission.observed_at_unix_millis != statement.observed_at_unix_millis - || admission.release_id != statement.release_id - || admission.release_witness_sha256 != statement.release_witness_sha256 - || admission.source_commit != statement.source_commit - || admission.deployment_id != statement.deployment_id - || admission.activation_witness_sha256 != statement.activation_witness_sha256 - || now_millis < admission.admitted_at_unix_millis - || now_millis.saturating_sub(admission.observed_at_unix_millis) - > u64::from(desired.max_silence_seconds).saturating_mul(1000) - { - return Ok(None); - } - let admission_sha256 = record_sha256(&admission)?; - let (deployment_head, deployment_request) = if let Some(deployment_id) = - admission.deployment_id.as_deref() - { - let head = node - .get::(&admission.daemon_id)? - .ok_or_else(|| anyhow!("authenticated health lost deployment head"))?; - let request = node - .get::(deployment_id)? - .ok_or_else(|| anyhow!("authenticated health lost deployment request"))?; - (Some(head), Some(request)) - } else { - (None, None) - }; - Ok(Some(ManagedHealthRead { - health: health.clone(), - projection_source: Some(AuthenticatedProviderHealthSource { - health, - admission, - statement, - binding, - binding_sha256: current_binding_sha256, - statement_sha256: statement_digest, - admission_sha256, - deployment_head, - deployment_request, - }), - })) - } - "daemon-published" => { - let Some(admission) = - node.get::(&desired.daemon_id)? - else { - return Ok(None); - }; - validate_admission_against_current_request(node, &admission)?; - validate_admission_fresh_at( - &admission, - now, - u64::from(desired.max_silence_seconds), - )?; - if admission.daemon_id != health.daemon_id - || admission.health_contract != health.health_contract - || admission.state != health.state - || admission.observed_at != health.observed_at - { - return Ok(None); - } - Ok(Some(ManagedHealthRead { - health, - projection_source: None, - })) - } - _ => Ok(None), - } - }) -} - -fn evaluate_target_health( - target: &DaemonTarget, - options: &CommonOptions, - store_lock: &Arc>, - desired: &IdunnDesiredDaemonRecord, - now: &str, -) -> Result<( - String, - IdunnDaemonHealthRecord, - Option, -)> { - if let Some(managed) = read_fresh_daemon_published_health(options, store_lock, desired, now)? { - // Daemon-owned health decides continuity. Release inspection is a - // deployment concern and may refine a healthy target into a planned - // rollout, but an unavailable Git checkout or upstream can never mask - // a healthy body or prevent an unhealthy body from being restarted. - if health_state_is_healthy(&managed.health.state) - && let Some(release) = &target.release - && release.deployed_revision_witness.is_some() - && let Some(drift) = - evaluate_release_drift(target, desired, release, &SystemReleaseStatePort, now) - && drift.state == "stale-deployment" - { - return Ok((supervisor_health_key(&desired.daemon_id), drift, None)); - } - return Ok(( - desired.daemon_id.clone(), - managed.health, - managed.projection_source, - )); - } - - Ok(( - supervisor_health_key(&desired.daemon_id), - missing_daemon_published_health(target, desired, now), - None, - )) -} - -fn evaluate_release_drift( - target: &DaemonTarget, - desired: &IdunnDesiredDaemonRecord, - release: &ReleaseTarget, - port: &dyn ReleaseStatePort, - observed_at: &str, -) -> Option { - let observation = (|| -> Result<(String, Option)> { - port.fetch(release)?; - let wanted = port.desired_revision(release)?; - let deployed = match port.deployed_revision(release) { - Ok(deployed) => Some(deployed), - Err(error) if error_chain_contains_io_not_found(&error) => None, - Err(error) => return Err(error), - }; - Ok((wanted, deployed)) - })(); - - let (state, detail, transport) = match observation { - Ok((wanted, None)) => ( - "stale-deployment", - format!( - "upstream {}/{} is {wanted}, but no deployed revision witness exists", - release.upstream_remote, release.upstream_branch - ), - "idunn.release-not-deployed", - ), - Ok((wanted, Some(deployed))) if wanted != deployed => ( - "stale-deployment", - format!( - "upstream {}/{} is {wanted}, but the deployed revision witness reports {deployed}", - release.upstream_remote, release.upstream_branch - ), - "idunn.release-revision-drift", - ), - Ok(_) => return None, - Err(error) => ( - "dependency-unavailable", - format!( - "Idunn could not establish release freshness for {}: {error:#}", - release.repo - ), - "idunn.release-revision-unavailable", - ), - }; - - Some(IdunnDaemonHealthRecord { - daemon_id: target.daemon_id.clone(), - state: state.to_string(), - detail, - health_contract: desired.health_contract.clone(), - publication_source: "idunn-release-observation".to_string(), - transport: transport.to_string(), - observed_at: observed_at.to_string(), - }) -} - -fn error_chain_contains_io_not_found(error: &anyhow::Error) -> bool { - error.chain().any(|cause| { - cause - .downcast_ref::() - .is_some_and(|io_error| io_error.kind() == std::io::ErrorKind::NotFound) - }) -} - -fn verify_release_witness_current( - release: &ReleaseTarget, - port: &dyn ReleaseStatePort, -) -> Result<()> { - port.fetch(release)?; - let desired = port.desired_revision(release)?; - let deployed = port.deployed_revision(release)?; - if desired != deployed { - return Err(anyhow!( - "desired revision {desired} does not match deployed revision {deployed}" - )); - } - Ok(()) -} - -fn read_deployed_revision_witness(path: &PathBuf) -> Result { - let contents = fs::read_to_string(path) - .with_context(|| format!("reading deployed revision witness {}", path.display()))?; - let revision = contents - .lines() - .find_map(|line| line.strip_prefix("DEPLOYED_REVISION=")) - .map(str::trim) - .filter(|value| !value.is_empty()) - .ok_or_else(|| anyhow!("{} has no DEPLOYED_REVISION value", path.display()))?; - Ok(revision.to_string()) -} - -fn supervisor_health_key(daemon_id: &str) -> String { - format!("observation:{daemon_id}") -} - -fn apply_missing_publication_grace( - target: &DaemonTarget, - desired: &IdunnDesiredDaemonRecord, - health: &mut IdunnDaemonHealthRecord, - health_authenticated: bool, - missing_since: &mut Option, - now: Instant, -) { - if health_authenticated { - *missing_since = None; - return; - } - if !target.health_contract.restart_on_missing_publication { - return; - } - - let first_missing = missing_since.get_or_insert(now); - let missing_for = now.saturating_duration_since(*first_missing); - if missing_for < Duration::from_secs(desired.max_silence_seconds.into()) { - health.state = "degraded".to_string(); - health.detail = format!( - "no fresh daemon-published {} record has arrived for {} seconds; Idunn is preserving the daemon-owned health key and waiting for the {} second continuity boundary", - desired.health_contract, - missing_for.as_secs(), - desired.max_silence_seconds - ); - } -} - -fn missing_daemon_published_health( - target: &DaemonTarget, - desired: &IdunnDesiredDaemonRecord, - observed_at: &str, -) -> IdunnDaemonHealthRecord { - let (state, detail, transport) = if target.health_contract.restart_on_missing_publication { - ( - "failed", - format!( - "no fresh daemon-published {} record arrived over {}; Idunn owns this local restart boundary.", - desired.health_contract, CULTNET_RUDP_PROTOCOL_ID - ), - "cultmesh.missing-locally-supervised-daemon-publication", - ) - } else { - ( - "dependency-unavailable", - format!( - "no fresh daemon-published {} record arrived over {}; Idunn did not run local health probes.", - desired.health_contract, CULTNET_RUDP_PROTOCOL_ID - ), - "cultmesh.missing-daemon-publication", - ) - }; - IdunnDaemonHealthRecord { - daemon_id: desired.daemon_id.clone(), - state: state.to_string(), - detail, - health_contract: desired.health_contract.clone(), - publication_source: "idunn-supervisor-observation".to_string(), - transport: transport.to_string(), - observed_at: observed_at.to_string(), - } -} - -fn health_state_is_healthy(state: &str) -> bool { - matches!(state, "active" | "healthy" | "ok" | "running") -} - -fn is_fresh_daemon_published_health( - health: &IdunnDaemonHealthRecord, - desired: &IdunnDesiredDaemonRecord, - now: &str, -) -> bool { - if health.daemon_id != desired.daemon_id { - return false; - } - if health.health_contract != desired.health_contract { - return false; - } - if !matches!( - health.publication_source.as_str(), - "daemon-authenticated" | "daemon-published" - ) { - return false; - } - if health.transport != CULTNET_RUDP_PROTOCOL_ID { - return false; - } - let Some(now_seconds) = parse_timestamp_seconds(now) else { - return false; - }; - let Some(observed_seconds) = parse_timestamp_seconds(&health.observed_at) else { - return false; - }; - if observed_seconds > now_seconds { - return false; - } - now_seconds.saturating_sub(observed_seconds) <= u64::from(desired.max_silence_seconds) -} - -fn parse_unix_timestamp(value: &str) -> Option { - value.strip_prefix("unix:")?.parse().ok() -} - -fn parse_timestamp_seconds(value: &str) -> Option { - parse_unix_timestamp(value).or_else(|| parse_utc_iso_timestamp_seconds(value)) -} - -struct IdunnProjectionPublisher { - signer: ServiceIdentitySigner, - public_store_path: PathBuf, - incarnation_id: String, - root_trust_snapshot_sha256: String, - write_lock: Mutex<()>, -} - -#[derive(Clone)] -struct IdunnPublicHealthSnapshotServer { - public_store_path: PathBuf, - allowed_record_keys: BTreeSet, -} - -impl IdunnPublicHealthSnapshotServer { - fn new(public_store_path: PathBuf, targets: &[DaemonTarget]) -> Result { - let allowed_record_keys = targets - .iter() - .map(|target| { - provider_health_projection_key(&target.daemon_id, &target.health_contract.id) - }) - .collect::>(); - if allowed_record_keys.len() != targets.len() { - return Err(anyhow!( - "public health query target catalog contains a duplicate daemon/contract pair" - )); - } - let server = Self { - public_store_path, - allowed_record_keys, - }; - // Startup observes the exact public store before any worker starts. - // An absent store is an empty public projection, not private fallback. - server.raw_snapshot()?; - Ok(server) - } - - fn serve(&self, request: &CultNetMessage) -> Result { - let registry = public_health_document_registry(); - let mut policy = CultNetReadOnlySnapshotPolicy::new(); - for key in &self.allowed_record_keys { - policy.allow(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, key)?; - } - serve_read_only_raw_snapshot(®istry, &policy, &self.raw_snapshot()?, request) - } - - fn raw_snapshot(&self) -> Result> { - let registry = public_health_document_registry(); - let entries = SingleFileMessagePackBackingStore::new(&self.public_store_path) - .pull_all_read_only_snapshot()?; - let mut records = Vec::with_capacity(entries.len()); - for entry in entries { - if entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE - || entry.schema_id.as_deref() - != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) - { - return Err(anyhow!( - "public health query store contains a non-projection envelope" - )); - } - let projection: IdunnAuthenticatedProviderHealthProjectionRecord = - rmp_serde::from_slice(&entry.payload) - .context("decoding public health query projection")?; - if rmp_serde::to_vec(&projection)? != entry.payload - || projection.projection_id != entry.key - { - return Err(anyhow!( - "public health query projection is noncanonical or has a mismatched key" - )); - } - projection.validate()?; - let mut record = registry.raw_document_record_from_envelope(&entry)?; - record.source_runtime_id = Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID.into()); - record.source_agent_id = None; - record.source_role = Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE.into()); - record.tags = Some(vec!["public-health".into()]); - records.push(record); - } - Ok(records) - } -} - -fn public_health_document_registry() -> CultNetDocumentRegistry { - let mut registry = CultNetDocumentRegistry::new(); - registry.register(CultNetDocumentBinding { - schema_id: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - document_type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - mutation_contract: None, - payload_schema_version: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), - }); - registry -} - -fn provider_health_projection_key(daemon_id: &str, health_contract: &str) -> String { - format!( - "provider-health:{:x}", - Sha256::digest([daemon_id.as_bytes(), b"\0", health_contract.as_bytes()].concat()) - ) -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct AuthenticatedProviderHealthSource { - health: IdunnDaemonHealthRecord, - admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, - statement: IdunnSignedDaemonHealthRecord, - binding: IdunnDaemonHealthTrustBindingRecord, - binding_sha256: String, - statement_sha256: String, - admission_sha256: String, - deployment_head: Option, - deployment_request: Option, -} - -#[derive(Clone, Debug)] -struct ManagedHealthRead { - health: IdunnDaemonHealthRecord, - projection_source: Option, -} - -fn initialize_projection_publisher( - options: &CommonOptions, -) -> Result>> { - let (Some(identity_path), Some(public_path)) = ( - options.service_identity_store_path.as_deref(), - options.public_health_store_path.as_deref(), - ) else { - if options.service_identity_store_path.is_some() - || options.public_health_store_path.is_some() - { - return Err(anyhow!( - "--service-identity-store and --public-health-store must be configured together" - )); - } - return Ok(None); - }; - reject_store_aliases(options, identity_path, public_path)?; - if let Some(parent) = public_path.parent() { - fs::create_dir_all(parent) - .with_context(|| format!("creating public health store parent {}", parent.display()))?; - } - let signer = open_service_identity_at::(identity_path) - .context("opening enrolled Idunn service identity")?; - let trust_path = options - .daemon_health_trust_store_path - .as_deref() - .ok_or_else(|| anyhow!("public health projection requires --daemon-health-trust-store"))?; - let root_trust_snapshot_sha256 = trust_store_snapshot_sha256(trust_path)?; - Ok(Some(Arc::new(IdunnProjectionPublisher { - signer, - public_store_path: public_path.to_path_buf(), - incarnation_id: uuid::Uuid::new_v4().to_string(), - root_trust_snapshot_sha256, - write_lock: Mutex::new(()), - }))) -} - -fn reject_store_aliases( - options: &CommonOptions, - identity_path: &Path, - public_path: &Path, -) -> Result<()> { - let identity = normalized_store_path(identity_path)?; - let public = normalized_store_path(public_path)?; - let mut private_paths = vec![normalized_store_path(&options.store_path)?, identity]; - for path in [ - options.daemon_health_trust_store_path.as_deref(), - options.release_authority_store_path.as_deref(), - options.trusted_epiphany_health_identity_store.as_deref(), - ] - .into_iter() - .flatten() - { - private_paths.push(normalized_store_path(path)?); - } - if private_paths.iter().enumerate().any(|(index, path)| { - private_paths - .iter() - .skip(index + 1) - .any(|other| other == path) - }) { - return Err(anyhow!( - "service identity, operational, trust, and release stores must not alias" - )); - } - if private_paths.iter().any(|path| path == &public) { - return Err(anyhow!( - "public health store must not alias identity, operational, trust, or release state" - )); - } - Ok(()) -} - -fn normalized_store_path(path: &Path) -> Result { - if path.exists() { - return fs::canonicalize(path) - .with_context(|| format!("canonicalizing store path {}", path.display())); - } - let file_name = path - .file_name() - .ok_or_else(|| anyhow!("store path {} has no file name", path.display()))?; - let parent = path.parent().unwrap_or_else(|| Path::new(".")); - let canonical_parent = fs::canonicalize(parent) - .with_context(|| format!("canonicalizing store parent {}", parent.display()))?; - Ok(canonical_parent.join(file_name)) -} - -fn trust_store_snapshot_sha256(path: &Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - record_sha256(&entries) -} - -impl IdunnProjectionPublisher { - fn publish_if_current( - &self, - options: &CommonOptions, - store_lock: &Arc>, - desired: &IdunnDesiredDaemonRecord, - source: &AuthenticatedProviderHealthSource, - evaluated_at: &str, - ) -> Result<()> { - let evaluated_at_unix_millis = parse_timestamp_millis(evaluated_at) - .ok_or_else(|| anyhow!("projection evaluation time is invalid"))?; - let current = - read_fresh_daemon_published_health(options, store_lock, desired, evaluated_at)? - .and_then(|managed| managed.projection_source) - .ok_or_else(|| { - anyhow!("authenticated provider health vanished before projection") - })?; - if ¤t != source { - return Err(anyhow!( - "authenticated provider health changed before projection publication" - )); - } - self.require_incarnation_trust_snapshot(options)?; - let reason_code = authenticated_provider_health_reason_code(&source.health.state) - .ok_or_else(|| anyhow!("provider state is not publicly projectable"))?; - let expires_at_unix_millis = source - .admission - .observed_at_unix_millis - .saturating_add(u64::from(desired.max_silence_seconds).saturating_mul(1000)); - if expires_at_unix_millis <= evaluated_at_unix_millis { - return Err(anyhow!( - "authenticated provider health expired before projection" - )); - } - let projection_key = provider_health_projection_key( - &source.health.daemon_id, - &source.health.health_contract, - ); - let _write_guard = self - .write_lock - .lock() - .map_err(|_| anyhow!("Idunn public projection store lock is poisoned"))?; - for _ in 0..8 { - let backing = SingleFileMessagePackBackingStore::new(&self.public_store_path); - let entries = backing.pull_all_read_only_snapshot()?; - if entries.iter().any(|entry| { - entry.r#type != IdunnAuthenticatedProviderHealthProjectionRecord::TYPE - || entry.schema_id.as_deref() - != Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA) - }) { - return Err(anyhow!( - "public health store contains a non-projection envelope" - )); - } - for entry in &entries { - self.verify_existing_projection(entry)?; - } - let current_envelope = entries - .iter() - .find(|entry| entry.key == projection_key) - .cloned(); - let current_projection = current_envelope - .as_ref() - .map(|entry| { - rmp_serde::from_slice::( - &entry.payload, - ) - }) - .transpose() - .context("decoding existing public health projection")?; - if let Some(existing) = current_projection.as_ref() { - existing.validate()?; - if current_envelope - .as_ref() - .is_some_and(|entry| entry.key != existing.projection_id) - || existing.projection_id != projection_key - { - return Err(anyhow!( - "public projection envelope key does not match its payload" - )); - } - if existing.idunn_signer_identity_id != self.signer.entry().identity_id { - return Err(anyhow!( - "public projection signer identity changed without store rotation" - )); - } - if existing.projection_incarnation_id == self.incarnation_id - && existing.signed_health_sha256 == source.statement_sha256 - && existing.authenticated_admission_sha256 == source.admission_sha256 - && existing.trust_binding_sha256 == source.binding_sha256 - { - return Ok(()); - } - } - let sequence = current_projection - .as_ref() - .map_or(1, |existing| existing.projection_sequence.saturating_add(1)); - if sequence == 0 { - return Err(anyhow!("public projection sequence exhausted")); - } - let mut projection = IdunnAuthenticatedProviderHealthProjectionRecord { - schema_version: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - projection_id: projection_key.clone(), - daemon_id: source.health.daemon_id.clone(), - health_contract: source.health.health_contract.clone(), - provider_state: source.health.state.clone(), - reason_code: reason_code.into(), - provider_observed_at_unix_millis: source.admission.observed_at_unix_millis, - admitted_at_unix_millis: source.admission.admitted_at_unix_millis, - evaluated_at_unix_millis, - trust_binding_id: source.binding.binding_id.clone(), - trust_binding_sha256: source.binding_sha256.clone(), - signed_health_sha256: source.statement_sha256.clone(), - authenticated_admission_sha256: source.admission_sha256.clone(), - provider_signer_identity_id: source.admission.signer_identity_id.clone(), - provider_incarnation_id: source.admission.publisher_incarnation_id.clone(), - provider_sequence: source.admission.publisher_sequence, - release_id: source.admission.release_id.clone(), - release_witness_sha256: source.admission.release_witness_sha256.clone(), - source_commit: source.admission.source_commit.clone(), - deployment_id: source.admission.deployment_id.clone(), - idunn_runtime_id: "idunn-daemon".into(), - idunn_signer_identity_id: self.signer.entry().identity_id.clone(), - projection_incarnation_id: self.incarnation_id.clone(), - projection_sequence: sequence, - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - private_state_exposed: false, - expires_at_unix_millis, - }; - let unsigned = rmp_serde::to_vec(&projection)?; - projection.signature = self - .signer - .sign::(&unsigned) - .signature; - projection.validate()?; - self.require_incarnation_trust_snapshot(options)?; - let replacement = CultCacheEnvelope { - key: projection_key.clone(), - r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - payload: rmp_serde::to_vec(&projection)?, - stored_at: evaluated_at.into(), - schema_id: Some(IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into()), - }; - let expected = CultCacheExpectedEnvelope { - key: projection_key.clone(), - r#type: IdunnAuthenticatedProviderHealthProjectionRecord::TYPE.into(), - current: current_envelope, - }; - if backing.compare_exchange(&[expected], &[replacement])? { - return Ok(()); - } - } - Err(anyhow!( - "public health projection lost repeated cross-process contention" - )) - } - - fn require_incarnation_trust_snapshot(&self, options: &CommonOptions) -> Result<()> { - let path = options - .daemon_health_trust_store_path - .as_deref() - .ok_or_else(|| anyhow!("projection publisher lost its root trust store"))?; - if trust_store_snapshot_sha256(path)? != self.root_trust_snapshot_sha256 { - return Err(anyhow!( - "root daemon-health trust changed during this Idunn incarnation; restart is required" - )); - } - Ok(()) - } - - fn verify_existing_projection(&self, entry: &CultCacheEnvelope) -> Result<()> { - let projection: IdunnAuthenticatedProviderHealthProjectionRecord = - rmp_serde::from_slice(&entry.payload) - .context("decoding public health projection during store audit")?; - if rmp_serde::to_vec(&projection)? != entry.payload { - return Err(anyhow!( - "public health projection is not canonical MessagePack" - )); - } - projection.validate()?; - if entry.key != projection.projection_id - || projection.idunn_signer_identity_id != self.signer.entry().identity_id - { - return Err(anyhow!("public health projection key or signer is foreign")); - } - let proof = cultnet_rs::ServiceIdentitySignature { - identity_id: projection.idunn_signer_identity_id.clone(), - signature: projection.signature.clone(), - }; - let mut unsigned = projection; - unsigned.signature.clear(); - cultnet_rs::verify_service_identity_signature::< - IdunnServiceIdentity, - IdunnAuthenticatedProviderHealthProjectionPurpose, - >( - &self.signer.trust_anchor()?, - &rmp_serde::to_vec(&unsigned)?, - &proof, - ) - .context("public health projection signature audit failed") - } -} - -fn record_sha256(value: &T) -> Result { - Ok(format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(value)?) - )) -} - -fn publish_authenticated_provider_health( - publisher: Option<&IdunnProjectionPublisher>, - options: &CommonOptions, - store_lock: &Arc>, - desired: &IdunnDesiredDaemonRecord, - source: Option<&AuthenticatedProviderHealthSource>, - evaluated_at: &str, -) -> Result<()> { - if let (Some(publisher), Some(source)) = (publisher, source) { - publisher.publish_if_current(options, store_lock, desired, source, evaluated_at)?; - } - Ok(()) -} - -fn parse_timestamp_millis(value: &str) -> Option { - if let Some(seconds) = parse_unix_timestamp(value) { - return seconds.checked_mul(1000); - } - let millis = chrono::DateTime::parse_from_rfc3339(value) - .ok()? - .timestamp_millis(); - u64::try_from(millis).ok() -} - -fn parse_admission_ceiling_millis(value: &str) -> Option { - if let Some(seconds) = parse_unix_timestamp(value) { - return seconds.checked_mul(1000)?.checked_add(999); - } - parse_timestamp_millis(value) -} - -fn parse_utc_iso_timestamp_seconds(value: &str) -> Option { - if value.len() < 19 { - return None; - } - if !value.ends_with('Z') && !value.contains("+00:00") { - return None; - } - - let year = value.get(0..4)?.parse::().ok()?; - let month = value.get(5..7)?.parse::().ok()?; - let day = value.get(8..10)?.parse::().ok()?; - let hour = value.get(11..13)?.parse::().ok()?; - let minute = value.get(14..16)?.parse::().ok()?; - let second = value.get(17..19)?.parse::().ok()?; - if value.as_bytes().get(4) != Some(&b'-') - || value.as_bytes().get(7) != Some(&b'-') - || value.as_bytes().get(10) != Some(&b'T') - || value.as_bytes().get(13) != Some(&b':') - || value.as_bytes().get(16) != Some(&b':') - || !(1..=12).contains(&month) - || !(1..=31).contains(&day) - || hour > 23 - || minute > 59 - || second > 60 - { - return None; - } - - let days = days_from_civil(year, month, day)?; - let seconds = days - .checked_mul(86_400)? - .checked_add(i64::from(hour) * 3_600)? - .checked_add(i64::from(minute) * 60)? - .checked_add(i64::from(second))?; - u64::try_from(seconds).ok() -} - -fn days_from_civil(year: i32, month: u32, day: u32) -> Option { - let year = i64::from(year) - if month <= 2 { 1 } else { 0 }; - let era = if year >= 0 { year } else { year - 399 } / 400; - let year_of_era = year - era * 400; - let month = i64::from(month); - let day = i64::from(day); - let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day - 1; - if !(0..=365).contains(&day_of_year) { - return None; - } - let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year; - Some(era * 146_097 + day_of_era - 719_468) -} - -fn publish_runtime_transport_check( - options: &CommonOptions, - store_lock: &Arc>, - observed_at: &str, -) -> Result<()> { - let check = runtime_transport_check(observed_at); - with_store_node(options, store_lock, |node| { - node.put(&check.check_id, &check)?; - Ok(()) - }) -} - -fn runtime_transport_check(observed_at: &str) -> IdunnRuntimeTransportCheckRecord { - match verify_rudp_loopback() { - Ok(transfer_id) => IdunnRuntimeTransportCheckRecord { - check_id: "idunn-runtime-rudp-loopback".to_string(), - runtime_id: "idunn-daemon".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - state: "available".to_string(), - detail: format!("loopback CultNet RUDP message acknowledged as transfer {transfer_id}"), - observed_at: observed_at.to_string(), - }, - Err(error) => IdunnRuntimeTransportCheckRecord { - check_id: "idunn-runtime-rudp-loopback".to_string(), - runtime_id: "idunn-daemon".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - state: "failed".to_string(), - detail: format!("loopback CultNet RUDP check failed: {error}"), - observed_at: observed_at.to_string(), - }, - } -} - -fn verify_rudp_loopback() -> Result { - let server_socket = UdpSocket::bind("127.0.0.1:0")?; - server_socket.set_read_timeout(Some(Duration::from_millis(100)))?; - let server_addr = server_socket.local_addr()?; - let client_socket = UdpSocket::bind("127.0.0.1:0")?; - client_socket.set_read_timeout(Some(Duration::from_millis(100)))?; - - let handle = thread::spawn(move || -> Result<()> { - let mut server = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::server( - "idunn-rudp-loopback-server", - server_socket, - IDUNN_HEALTH_RUDP_CONNECTION_ID, - ))?; - let deadline = Instant::now() + Duration::from_secs(2); - loop { - if let Some(frame) = server.receive_once()? { - let message = decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - )?; - return match message { - CultNetMessage::Hello { runtime_id, .. } if runtime_id == "idunn-daemon" => { - Ok(()) - } - other => Err(anyhow!("unexpected loopback CultNet message: {other:?}")), - }; - } - if Instant::now() >= deadline { - return Err(anyhow!( - "Idunn RUDP loopback timed out waiting for schema frame" - )); - } - } - }); - - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( - "idunn-rudp-loopback-client", - client_socket, - server_addr, - IDUNN_HEALTH_RUDP_CONNECTION_ID, - ))?; - client.connect(Vec::new())?; - let deadline = Instant::now() + Duration::from_secs(2); - while !client.connected() { - let _ = client.receive_once()?; - client.poll_resends()?; - if Instant::now() >= deadline { - return Err(anyhow!("Idunn RUDP loopback timed out during handshake")); - } - } - let message = CultNetMessage::Hello { - runtime_id: "idunn-daemon".to_string(), - runtime_kind: "keepalive".to_string(), - agent_id: None, - role: Some("idunn.runtime-transport-self-check".to_string()), - display_name: Some("Idunn RUDP self-check".to_string()), - supported_document_types: Some(vec!["idunn.runtime_transport_check".to_string()]), - supported_mutation_contracts: None, - supported_message_versions: Some(vec!["cultnet.hello.v0".to_string()]), - transport_profiles: Some(vec![client.profile.clone()]), - supports_schema_catalog: Some(false), - }; - let payload = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?; - client.send("schema", payload)?; - handle - .join() - .map_err(|_| anyhow!("Idunn RUDP loopback receiver thread panicked"))??; - Ok(format!("connection:{IDUNN_HEALTH_RUDP_CONNECTION_ID:08x}")) -} - -fn start_rudp_health_ingress( - options: &CommonOptions, - store_lock: &Arc>, - observed_at: &str, -) -> Result<()> { - let Some(bind_address) = options.rudp_health_bind else { - return Ok(()); - }; - - let ingress_id = "idunn-rudp-health-ingress".to_string(); - let socket = match UdpSocket::bind(bind_address) { - Ok(socket) => socket, - Err(error) => { - let failed = rudp_health_ingress_record( - &ingress_id, - bind_address, - "failed", - &format!("failed to bind RUDP health ingress: {error}"), - observed_at, - ); - with_store_node(options, store_lock, |node| { - node.put(&failed.ingress_id, &failed)?; - Ok(()) - })?; - return Err(error.into()); - } - }; - let local_addr = socket.local_addr()?; - socket.set_read_timeout(Some(Duration::from_millis(500)))?; - - let active = rudp_health_ingress_record( - &ingress_id, - local_addr, - "active", - "listening for idunn.daemon_health document frames over CultNet RUDP schema channel", - observed_at, - ); - with_store_node(options, store_lock, |node| { - node.put(&active.ingress_id, &active)?; - Ok(()) - })?; - - let worker_options = options.clone(); - let worker_store_lock = Arc::clone(store_lock); - thread::spawn(move || { - if let Err(error) = run_rudp_health_ingress_loop(socket, worker_options, worker_store_lock) - { - eprintln!("Idunn RUDP health ingress stopped: {error}"); - } - }); - Ok(()) -} - -fn start_public_health_query_listener( - options: &CommonOptions, - targets: &[DaemonTarget], -) -> Result> { - let Some(bind_address) = options.public_health_query_bind else { - return Ok(None); - }; - let public_store_path = options - .public_health_store_path - .clone() - .ok_or_else(|| anyhow!("--public-health-query-bind requires --public-health-store"))?; - if options.service_identity_store_path.is_none() { - return Err(anyhow!( - "--public-health-query-bind requires --service-identity-store projection authority" - )); - } - let server = IdunnPublicHealthSnapshotServer::new(public_store_path, targets)?; - let socket = UdpSocket::bind(bind_address) - .with_context(|| format!("binding public health query listener at {bind_address}"))?; - let local_addr = socket.local_addr()?; - socket.set_read_timeout(Some(Duration::from_millis(250)))?; - thread::spawn(move || { - if let Err(error) = run_public_health_query_listener_loop(socket, server, None) { - eprintln!("Idunn public health query listener stopped: {error:#}"); - } - }); - println!( - "Idunn public health query listener active at {local_addr} for {} target projection key(s).", - targets.len() - ); - Ok(Some(local_addr)) -} - -fn run_public_health_query_listener_loop( - socket: UdpSocket, - server: IdunnPublicHealthSnapshotServer, - stop: Option>, -) -> Result<()> { - let mut sessions: HashMap = HashMap::new(); - let mut buffer = vec![0_u8; 65_535]; - loop { - if stop - .as_ref() - .is_some_and(|flag| flag.load(Ordering::Acquire)) - { - return Ok(()); - } - match socket.recv_from(&mut buffer) { - Ok((size, source)) => { - if let Err(error) = handle_public_health_query_datagram( - &socket, - &mut sessions, - &server, - &buffer[..size], - source, - ) { - eprintln!( - "Idunn public health query listener refused datagram from {source}: {error:#}" - ); - } - } - Err(error) - if matches!( - error.kind(), - std::io::ErrorKind::Interrupted - | std::io::ErrorKind::WouldBlock - | std::io::ErrorKind::TimedOut - | std::io::ErrorKind::ConnectionReset - | std::io::ErrorKind::ConnectionAborted - ) => {} - Err(error) => return Err(error.into()), - } - let now = unix_epoch_millis()?; - let mut expired = Vec::new(); - for (peer, session) in &mut sessions { - for packet in session.due_resends(now) { - socket.send_to(&encode_rudp_packet(&packet)?, peer)?; - } - if session.check_timeout(now, 60_000) { - expired.push(*peer); - } - } - for peer in expired { - sessions.remove(&peer); - } - } -} - -fn handle_public_health_query_datagram( - socket: &UdpSocket, - sessions: &mut HashMap, - server: &IdunnPublicHealthSnapshotServer, - wire: &[u8], - source: SocketAddr, -) -> Result<()> { - let packet = decode_rudp_packet(wire)?; - if packet.connection_id != IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID { - return Err(anyhow!( - "unexpected public health query RUDP connection id {:08x}", - packet.connection_id - )); - } - let now = unix_epoch_millis()?; - if packet.packet_type == CultNetRudpPacketType::Connect { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, - initial_sequence: 1, - resend_delay_ms: 100, - max_pending_reliable_packets: Some(64), - }); - let accept = session.accept_connect(&packet, now, Vec::new())?; - socket.send_to(&encode_rudp_packet(&accept)?, source)?; - sessions.insert(source, session); - return Ok(()); - } - let session = sessions - .get_mut(&source) - .ok_or_else(|| anyhow!("public health query peer has no accepted RUDP session"))?; - let result = session.receive(&packet, now)?; - if let Some(reply) = result.reply { - socket.send_to(&encode_rudp_packet(&reply)?, source)?; - } - if result.disconnected { - sessions.remove(&source); - return Ok(()); - } - if packet.packet_type == CultNetRudpPacketType::Accept || !result.delivered.is_empty() { - socket.send_to(&encode_rudp_packet(&session.create_ack())?, source)?; - } - for frame in result.delivered { - let response = if frame.channel_id != "schema" { - CultNetMessage::Error { - error: "public health queries require the CultNet schema channel".into(), - } - } else { - match decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) { - Ok(request) => { - server - .serve(&request) - .unwrap_or_else(|error| CultNetMessage::Error { - error: format!("public health query refused: {error}"), - }) - } - Err(error) => CultNetMessage::Error { - error: format!("public health query message is malformed: {error}"), - }, - } - }; - let payload = - encode_cultnet_message_to_vec(&response, CultNetWireContract::CultNetSchemaV0)?; - let packets = session.send_many( - "schema", - payload, - CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms: now, - reliable_expire_after_ms: None, - }, - Some(1200), - )?; - for packet in packets { - socket.send_to(&encode_rudp_packet(&packet)?, source)?; - } - } - Ok(()) -} - -fn rudp_health_ingress_record( - ingress_id: &str, - bind_address: SocketAddr, - state: &str, - detail: &str, - observed_at: &str, -) -> IdunnRudpHealthIngressRecord { - IdunnRudpHealthIngressRecord { - ingress_id: ingress_id.to_string(), - bind_address: bind_address.to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - accepted_schema: "idunn.daemon_health".to_string(), - state: state.to_string(), - detail: detail.to_string(), - observed_at: observed_at.to_string(), - } -} - -fn run_rudp_health_ingress_loop( - socket: UdpSocket, - options: CommonOptions, - store_lock: Arc>, -) -> Result<()> { - let local_addr = socket.local_addr()?; - let mut sessions: HashMap = HashMap::new(); - let mut buffer = vec![0_u8; 65_535]; - let trace_ingress = env::var("IDUNN_RUDP_HEALTH_TRACE") - .map(|value| value == "1" || value.eq_ignore_ascii_case("true")) - .unwrap_or(false); - - loop { - match socket.recv_from(&mut buffer) { - Ok((size, source)) => { - let observed_at = timestamp()?; - if trace_ingress { - println!("Idunn RUDP health ingress received {size} bytes from {source}."); - } - match handle_rudp_health_datagram( - &socket, - &mut sessions, - &buffer[..size], - source, - trace_ingress, - ) { - Ok(frames) => { - for frame in frames { - let message = match decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) { - Ok(message) => message, - Err(error) => { - if trace_ingress { - println!( - "Idunn RUDP health ingress rejected schema bytes from {source}: {error}" - ); - } - publish_rudp_ingress_failure( - &options, - &store_lock, - local_addr, - &format!( - "rejected RUDP health schema bytes from {source}: {error}" - ), - &observed_at, - ); - continue; - } - }; - match admit_health_from_rudp_message( - &message, - &options, - &store_lock, - &observed_at, - ) { - Ok(outcome) => { - let bounded_detail = - outcome.detail.replace('\r', " ").replace('\n', " "); - println!( - "Idunn recorded RUDP health input for {} from {} over {} state={} authority={} detail={}", - outcome.daemon_id, - source, - frame.channel_id, - outcome.state, - outcome.authority, - bounded_detail - ); - } - Err(error) => { - if trace_ingress { - println!( - "Idunn RUDP health ingress rejected schema frame from {source}: {error}" - ); - } - publish_rudp_ingress_failure( - &options, - &store_lock, - local_addr, - &format!( - "rejected RUDP health schema frame from {source}: {error}" - ), - &observed_at, - ); - } - } - } - } - Err(error) => { - if trace_ingress { - println!( - "Idunn RUDP health ingress rejected datagram from {source}: {error}" - ); - } - publish_rudp_ingress_failure( - &options, - &store_lock, - local_addr, - &format!("rejected RUDP health datagram from {source}: {error}"), - &observed_at, - ); - } - } - } - Err(error) - if matches!( - error.kind(), - std::io::ErrorKind::Interrupted - | std::io::ErrorKind::WouldBlock - | std::io::ErrorKind::TimedOut - | std::io::ErrorKind::ConnectionReset - | std::io::ErrorKind::ConnectionAborted - ) => {} - Err(error) => return Err(error.into()), - } - } -} - -fn publish_rudp_ingress_failure( - options: &CommonOptions, - store_lock: &Arc>, - local_addr: SocketAddr, - detail: &str, - observed_at: &str, -) { - let failed = rudp_health_ingress_record( - "idunn-rudp-health-ingress", - local_addr, - "degraded", - detail, - observed_at, - ); - if let Err(error) = with_store_node(options, store_lock, |node| { - node.put(&failed.ingress_id, &failed)?; - Ok(()) - }) { - eprintln!("Idunn RUDP health ingress failed to persist ingress failure: {error}"); - } -} - -fn handle_rudp_health_datagram( - socket: &UdpSocket, - sessions: &mut HashMap, - wire: &[u8], - source: SocketAddr, - trace_ingress: bool, -) -> Result> { - let packet = decode_rudp_packet(wire)?; - if packet.connection_id != IDUNN_HEALTH_RUDP_CONNECTION_ID { - return Err(anyhow!( - "unexpected RUDP connection id {:08x}", - packet.connection_id - )); - } - - let now = unix_epoch_millis()?; - - if packet.packet_type == CultNetRudpPacketType::Connect { - let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: IDUNN_HEALTH_RUDP_CONNECTION_ID, - initial_sequence: 1, - resend_delay_ms: 100, - max_pending_reliable_packets: None, - }); - let accept = session.accept_connect(&packet, now, Vec::new())?; - let accept_wire = encode_rudp_packet(&accept)?; - socket.send_to(&accept_wire, source)?; - if trace_ingress { - println!( - "Idunn RUDP health ingress accepted connect from {source} with sequence {}.", - packet.sequence - ); - } - sessions.insert(source, session); - return Ok(Vec::new()); - } - - let session = sessions.entry(source).or_insert_with(|| { - CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: IDUNN_HEALTH_RUDP_CONNECTION_ID, - initial_sequence: 1, - resend_delay_ms: 100, - max_pending_reliable_packets: None, - }) - }); - - let result = session.receive(&packet, now)?; - if trace_ingress && packet.packet_type == CultNetRudpPacketType::Data { - println!( - "Idunn RUDP health ingress data from {source}: sequence {} delivered {} frame(s).", - packet.sequence, - result.delivered.len() - ); - } - if let Some(reply) = result.reply { - socket.send_to(&encode_rudp_packet(&reply)?, source)?; - } - let frames = result - .delivered - .into_iter() - .map(|frame| cultnet_rs::CultNetTransportFrame { - channel_id: frame.channel_id, - payload: frame.payload, - }) - .collect::>(); - if packet.packet_type == CultNetRudpPacketType::Accept || !frames.is_empty() { - let ack = session.create_ack(); - socket.send_to(&encode_rudp_packet(&ack)?, source)?; - } - if result.disconnected || !frames.is_empty() { - sessions.remove(&source); - } - Ok(frames) -} - -fn admit_health_from_rudp_message( - message: &CultNetMessage, - options: &CommonOptions, - store_lock: &Arc>, - admitted_at: &str, -) -> Result { - compact_operational_history_if_needed(options, store_lock)?; - let decoded = decode_health_from_rudp_message(message, options, admitted_at)?; - let (health, admission) = match decoded { - DecodedHealthIngress::Diagnostic(diagnostic) => { - diagnostic.validate()?; - let outcome = HealthIngressOutcome { - daemon_id: diagnostic.daemon_id.clone(), - state: diagnostic.claimed_state.clone(), - detail: diagnostic.claimed_detail.clone(), - authority: "diagnostic-only", - }; - let key = diagnostic.diagnostic_id.clone(); - // Unsigned input is quarantined under a distinct type and key. It - // cannot overwrite the managed-health row or admission. - with_store_node(options, store_lock, |node| { - node.put(&key, &diagnostic)?; - Ok(()) - })?; - return Ok(outcome); - } - DecodedHealthIngress::AuthenticatedGeneric { - health, - statement_payload, - admission, - } => { - return admit_generic_signed_health( - options, - store_lock, - admitted_at, - health, - statement_payload, - admission, - ); - } - DecodedHealthIngress::AuthenticatedLegacy { health, admission } => (health, admission), - }; - { - with_store_node(options, store_lock, |node| { - validate_admission_against_current_request(node, &admission) - })?; - } - for _ in 0..8 { - let (expected_health, expected_admission, expected_head, expected_request) = - with_store_node(options, store_lock, |node| { - validate_admission_against_current_request(node, &admission)?; - if let Some(existing) = - node.get::(&admission.daemon_id)? - && !signed_health_advances(&existing, &admission)? - { - return Err(anyhow!("signed Epiphany health is replayed or regressed")); - } - Ok(( - node.cache() - .get_envelope::(&health.daemon_id)?, - node.cache() - .get_envelope::(&admission.daemon_id)?, - node.cache() - .get_envelope::( - &admission.daemon_id, - )?, - node.cache().get_envelope::( - &admission.deployment_request_id, - )?, - )) - })?; - let health_envelope = typed_envelope(&health.daemon_id, &health, admitted_at)?; - let admission_envelope = typed_envelope(&admission.daemon_id, &admission, admitted_at)?; - let expected = [ - CultCacheExpectedEnvelope { - key: health.daemon_id.clone(), - r#type: IdunnDaemonHealthRecord::TYPE.into(), - current: expected_health, - }, - CultCacheExpectedEnvelope { - key: admission.daemon_id.clone(), - r#type: IdunnSignedHealthAdmissionRecord::TYPE.into(), - current: expected_admission, - }, - CultCacheExpectedEnvelope { - key: admission.daemon_id.clone(), - r#type: IdunnCurrentDeploymentRequestRecord::TYPE.into(), - current: expected_head, - }, - CultCacheExpectedEnvelope { - key: admission.deployment_request_id.clone(), - r#type: IdunnDeploymentRequestRecord::TYPE.into(), - current: expected_request, - }, - ]; - if SingleFileMessagePackBackingStore::new(&options.store_path) - .compare_exchange(&expected, &[health_envelope, admission_envelope])? - { - return Ok(HealthIngressOutcome { - daemon_id: health.daemon_id, - state: health.state, - detail: health.detail, - authority: "authenticated", - }); - } - } - Err(anyhow!( - "signed Epiphany health admission lost repeated cross-process contention" - )) -} - -fn typed_envelope( - key: &str, - value: &T, - stored_at: &str, -) -> Result { - Ok(CultCacheEnvelope { - key: key.to_string(), - r#type: T::TYPE.to_string(), - payload: rmp_serde::to_vec(value)?, - stored_at: stored_at.to_string(), - schema_id: Some(T::TYPE.to_string()), - }) -} - -fn compact_authenticated_health_statement_history( - options: &CommonOptions, - store_lock: &Arc>, -) -> Result { - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let backing = SingleFileMessagePackBackingStore::new(&options.store_path); - for _ in 0..8 { - let expected = backing.pull_all()?; - let admissions = expected - .iter() - .filter(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) - .map(|entry| { - let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = - rmp_serde::from_slice(&entry.payload) - .context("decoding current authenticated health admission")?; - admission.validate()?; - Ok((admission.daemon_id.clone(), admission)) - }) - .collect::>>()?; - let statements = expected - .iter() - .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) - .map(|entry| (entry.key.clone(), entry)) - .collect::>(); - let mut current_statements = Vec::with_capacity(admissions.len()); - for (daemon_id, admission) in &admissions { - let entry = statements - .get(daemon_id) - .or_else(|| statements.get(&admission.signed_health_sha256)) - .ok_or_else(|| { - anyhow!( - "authenticated health admission for {daemon_id} lost its signed statement" - ) - })?; - let (statement, _) = decode_generic_signed_health_payload(&entry.payload) - .context("decoding current signed daemon health")?; - statement.validate()?; - let digest = format!("sha256-{:x}", Sha256::digest(&entry.payload)); - if statement.daemon_id != *daemon_id - || digest != admission.signed_health_sha256 - || statement.activation_witness_sha256 != admission.activation_witness_sha256 - { - return Err(anyhow!( - "authenticated health admission for {daemon_id} does not bind its signed statement" - )); - } - let mut current = (*entry).clone(); - current.key.clone_from(daemon_id); - current_statements.push(current); - } - let statement_count = statements.len(); - let already_compact = statement_count == current_statements.len() - && current_statements - .iter() - .all(|entry| statements.contains_key(&entry.key)); - if already_compact { - return Ok(0); - } - let mut replacement = expected - .iter() - .filter(|entry| entry.r#type != IdunnSignedDaemonHealthRecord::TYPE) - .cloned() - .collect::>(); - replacement.extend(current_statements); - if backing.compare_exchange_snapshot(&expected, &replacement)? { - return Ok(statement_count.saturating_sub(admissions.len())); - } - } - Err(anyhow!( - "authenticated health statement compaction lost repeated cross-process contention" - )) -} - -fn operational_history_coordinates( - entry: &CultCacheEnvelope, -) -> Result> { - let coordinates = match entry.r#type.as_str() { - kind if kind == odin_core::IdunnKeepaliveDecisionRecord::TYPE => { - let record: odin_core::IdunnKeepaliveDecisionRecord = - rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.decided_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnOperatorAlarmRecord::TYPE => { - let record: IdunnOperatorAlarmRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.raised_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnDeploymentRequestRecord::TYPE => { - let record: IdunnDeploymentRequestRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.requested_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnDeploymentResultRecord::TYPE => { - let record: IdunnDeploymentResultRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.completed_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnRestartRequestRecord::TYPE => { - let record: IdunnRestartRequestRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.requested_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnRestartResultRecord::TYPE => { - let record: IdunnRestartResultRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.completed_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnStateMigrationResultRecord::TYPE => { - let record: IdunnStateMigrationResultRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.completed_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnRolloutResultRecord::TYPE => { - let record: IdunnRolloutResultRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.completed_at, - IDUNN_OPERATIONAL_HISTORY_PER_DAEMON, - ) - } - kind if kind == IdunnLifecycleCommandRecord::TYPE => { - let record: IdunnLifecycleCommandRecord = rmp_serde::from_slice(&entry.payload)?; - ( - record.daemon_id, - record.requested_at, - IDUNN_LIFECYCLE_HISTORY_PER_DAEMON, - ) - } - _ => return Ok(None), - }; - if coordinates.0.trim().is_empty() || coordinates.1.trim().is_empty() { - return Err(anyhow!( - "Idunn operational history record has an empty daemon or timestamp" - )); - } - Ok(Some(coordinates)) -} - -fn compact_operational_history_if_needed( - options: &CommonOptions, - store_lock: &Arc>, -) -> Result<()> { - if fs::metadata(&options.store_path) - .map(|metadata| metadata.len() > IDUNN_OPERATIONAL_STORE_COMPACTION_BYTES) - .unwrap_or(false) - { - let removed = compact_operational_history(options, store_lock)?; - if removed > 0 { - println!("Idunn compacted {removed} obsolete operational history record(s)."); - } - } - Ok(()) -} - -fn compact_operational_history( - options: &CommonOptions, - store_lock: &Arc>, -) -> Result { - let _store_guard = store_lock - .lock() - .map_err(|_| anyhow!("Idunn store lock is poisoned"))?; - let backing = SingleFileMessagePackBackingStore::new(&options.store_path); - for _ in 0..8 { - let expected = backing.pull_all()?; - let mut keep = BTreeSet::<(String, String)>::new(); - let mut groups = BTreeMap::<(String, String, usize), Vec<(String, String)>>::new(); - for entry in &expected { - if let Some((daemon_id, timestamp, limit)) = operational_history_coordinates(entry)? { - groups - .entry((entry.r#type.clone(), daemon_id, limit)) - .or_default() - .push((timestamp, entry.key.clone())); - } else { - keep.insert((entry.r#type.clone(), entry.key.clone())); - } - } - for ((kind, _, limit), candidates) in &mut groups { - candidates.sort_by(|left, right| right.cmp(left)); - for (_, key) in candidates.iter().take(*limit) { - keep.insert((kind.clone(), key.clone())); - } - } - - for entry in expected - .iter() - .filter(|entry| entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE) - { - let head: IdunnCurrentDeploymentRequestRecord = rmp_serde::from_slice(&entry.payload)?; - if head.daemon_id.trim().is_empty() || head.request_id.trim().is_empty() { - return Err(anyhow!( - "current deployment request head is invalid during operational compaction" - )); - } - keep.insert(( - IdunnDeploymentRequestRecord::TYPE.into(), - head.request_id.clone(), - )); - keep.insert(( - IdunnDeploymentResultRecord::TYPE.into(), - format!("result:{}", head.request_id), - )); - } - for entry in expected - .iter() - .filter(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) - { - let admission: IdunnAuthenticatedDaemonHealthAdmissionRecord = - rmp_serde::from_slice(&entry.payload)?; - admission.validate()?; - if let Some(deployment_id) = admission.deployment_id { - keep.insert(( - IdunnDeploymentRequestRecord::TYPE.into(), - deployment_id.clone(), - )); - keep.insert(( - IdunnDeploymentResultRecord::TYPE.into(), - format!("result:{deployment_id}"), - )); - } - } - for entry in expected - .iter() - .filter(|entry| entry.r#type == IdunnSignedHealthAdmissionRecord::TYPE) - { - let admission: IdunnSignedHealthAdmissionRecord = - rmp_serde::from_slice(&entry.payload)?; - if admission.deployment_request_id.trim().is_empty() { - return Err(anyhow!( - "signed health admission lost its deployment request during operational compaction" - )); - } - keep.insert(( - IdunnDeploymentRequestRecord::TYPE.into(), - admission.deployment_request_id.clone(), - )); - keep.insert(( - IdunnDeploymentResultRecord::TYPE.into(), - format!("result:{}", admission.deployment_request_id), - )); - } - let retained_commands = expected - .iter() - .filter(|entry| entry.r#type == IdunnLifecycleCommandRecord::TYPE) - .filter(|entry| keep.contains(&(entry.r#type.clone(), entry.key.clone()))) - .cloned() - .collect::>(); - for entry in retained_commands { - let command: IdunnLifecycleCommandRecord = rmp_serde::from_slice(&entry.payload)?; - if command.result_id.trim().is_empty() { - continue; - } - let request_id = command - .result_id - .strip_prefix("result:") - .unwrap_or(&command.result_id) - .to_string(); - keep.insert(( - IdunnDeploymentRequestRecord::TYPE.into(), - request_id.clone(), - )); - keep.insert((IdunnRestartRequestRecord::TYPE.into(), request_id)); - keep.insert(( - IdunnDeploymentResultRecord::TYPE.into(), - command.result_id.clone(), - )); - keep.insert((IdunnRestartResultRecord::TYPE.into(), command.result_id)); - } - - let replacement = expected - .iter() - .filter(|entry| keep.contains(&(entry.r#type.clone(), entry.key.clone()))) - .cloned() - .collect::>(); - if replacement.len() == expected.len() { - return Ok(0); - } - if backing.compare_exchange_snapshot(&expected, &replacement)? { - return Ok(expected.len().saturating_sub(replacement.len())); - } - } - Err(anyhow!( - "operational history compaction lost repeated cross-process contention" - )) -} - -fn authenticate_generic_signed_health( - document: &cultnet_rs::CultNetRawDocumentRecord, - options: &CommonOptions, - admitted_at: &str, -) -> Result<( - IdunnDaemonHealthRecord, - Vec, - IdunnAuthenticatedDaemonHealthAdmissionRecord, -)> { - let (statement, unsigned_payload) = decode_generic_signed_health_payload(&document.payload)?; - statement.validate()?; - if document.record_key != statement.daemon_id - || document.source_runtime_id.as_deref() != Some(statement.source_runtime_id.as_str()) - || document.source_role.as_deref() != Some("daemon-health-publisher") - { - return Err(anyhow!( - "signed daemon health transport identity is invalid" - )); - } - let (binding, binding_sha256) = load_daemon_health_trust_binding(options, &statement)?; - if binding.release_binding_required { - if statement.release_id.is_none() - || statement.release_witness_sha256.is_none() - || statement.source_commit.is_none() - || statement.deployment_id.is_none() - { - return Err(anyhow!( - "signed daemon health lacks required release binding" - )); - } - } else if statement.release_id.is_some() - || statement.release_witness_sha256.is_some() - || statement.source_commit.is_some() - || statement.deployment_id.is_some() - { - return Err(anyhow!( - "signed daemon health carries release authority not declared by its trust binding" - )); - } - let expected_identity = - derive_service_identity_id::(&binding.signer_public_key)?; - if binding.signer_identity_id != expected_identity - || statement.signer_identity_id != binding.signer_identity_id - { - return Err(anyhow!("signed daemon health identity is not root-bound")); - } - let signature_bytes: [u8; 64] = statement - .signature - .as_slice() - .try_into() - .map_err(|_| anyhow!("signed daemon health signature length is invalid"))?; - verify_service_identity_signature_with_public_key::< - GameCultProviderHealthIdentity, - IdunnSignedDaemonHealthPurpose, - >( - &binding.signer_public_key, - &unsigned_payload, - &ServiceIdentitySignature { - identity_id: binding.signer_identity_id.clone(), - signature: signature_bytes.to_vec(), - }, - ) - .context("signed daemon health signature is invalid")?; - let admitted_at_unix_millis = parse_admission_ceiling_millis(admitted_at) - .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; - if statement.observed_at_unix_millis > admitted_at_unix_millis - || admitted_at_unix_millis.saturating_sub(statement.observed_at_unix_millis) - > EPIPHANY_ADMISSION_MAX_AGE_SECONDS.saturating_mul(1000) - { - return Err(anyhow!( - "signed daemon health observation is future or stale" - )); - } - let signed_health_sha256 = format!("sha256-{:x}", Sha256::digest(&document.payload)); - let observed_at = chrono::DateTime::from_timestamp_millis( - i64::try_from(statement.observed_at_unix_millis) - .map_err(|_| anyhow!("signed daemon health observation is out of range"))?, - ) - .ok_or_else(|| anyhow!("signed daemon health observation is out of range"))? - .to_rfc3339_opts(chrono::SecondsFormat::Millis, true); - let health = IdunnDaemonHealthRecord { - daemon_id: statement.daemon_id.clone(), - state: statement.state.clone(), - detail: statement.detail.clone(), - observed_at, - health_contract: statement.health_contract.clone(), - publication_source: "daemon-authenticated".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }; - let admission = IdunnAuthenticatedDaemonHealthAdmissionRecord { - schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), - daemon_id: statement.daemon_id.clone(), - health_contract: statement.health_contract.clone(), - source_runtime_id: statement.source_runtime_id.clone(), - state: statement.state.clone(), - observed_at_unix_millis: statement.observed_at_unix_millis, - admitted_at_unix_millis, - trust_binding_id: binding.binding_id, - trust_binding_sha256: binding_sha256, - signer_identity_id: statement.signer_identity_id.clone(), - publisher_incarnation_id: statement.publisher_incarnation_id.clone(), - publisher_sequence: statement.publisher_sequence, - signed_health_sha256, - release_id: statement.release_id.clone(), - release_witness_sha256: statement.release_witness_sha256.clone(), - source_commit: statement.source_commit.clone(), - deployment_id: statement.deployment_id.clone(), - private_state_exposed: false, - activation_witness_sha256: statement.activation_witness_sha256.clone(), - }; - admission.validate()?; - Ok((health, document.payload.clone(), admission)) -} - -fn decode_generic_signed_health_payload( - payload: &[u8], -) -> Result<(IdunnSignedDaemonHealthRecord, Vec)> { - IdunnSignedDaemonHealthRecord::decode_canonical_signed_payload(payload) -} - -fn load_daemon_health_trust_binding( - options: &CommonOptions, - statement: &IdunnSignedDaemonHealthRecord, -) -> Result<(IdunnDaemonHealthTrustBindingRecord, String)> { - let path = options - .daemon_health_trust_store_path - .as_deref() - .ok_or_else(|| anyhow!("signed daemon health has no root trust store"))?; - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let mut matches = Vec::new(); - for entry in entries - .iter() - .filter(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) - { - let binding: IdunnDaemonHealthTrustBindingRecord = rmp_serde::from_slice(&entry.payload) - .context("decoding root daemon health trust binding")?; - binding.validate()?; - if binding.daemon_id == statement.daemon_id - && binding.health_contract == statement.health_contract - && binding.source_runtime_id == statement.source_runtime_id - { - matches.push(binding); - } - } - let binding = matches - .pop() - .ok_or_else(|| anyhow!("signed daemon health has no exact root trust binding"))?; - if !matches.is_empty() { - return Err(anyhow!("signed daemon health trust binding is ambiguous")); - } - let digest = format!("sha256-{:x}", Sha256::digest(rmp_serde::to_vec(&binding)?)); - Ok((binding, digest)) -} - -fn admit_generic_signed_health( - options: &CommonOptions, - store_lock: &Arc>, - admitted_at: &str, - health: IdunnDaemonHealthRecord, - statement_payload: Vec, - admission: IdunnAuthenticatedDaemonHealthAdmissionRecord, -) -> Result { - for _ in 0..8 { - let (expected_health, expected_admission, expected_statement) = - with_store_node(options, store_lock, |node| { - if let Some(existing) = - node.get::(&admission.daemon_id)? - && !generic_signed_health_advances(&existing, &admission) - { - return Err(anyhow!("signed daemon health is replayed or regressed")); - } - validate_generic_release_binding(node, &admission)?; - Ok(( - node.cache() - .get_envelope::(&health.daemon_id)?, - node.cache() - .get_envelope::( - &admission.daemon_id, - )?, - node.cache() - .get_envelope::(&admission.daemon_id)?, - )) - })?; - let expected = [ - CultCacheExpectedEnvelope { - key: health.daemon_id.clone(), - r#type: IdunnDaemonHealthRecord::TYPE.into(), - current: expected_health, - }, - CultCacheExpectedEnvelope { - key: admission.daemon_id.clone(), - r#type: IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE.into(), - current: expected_admission, - }, - CultCacheExpectedEnvelope { - key: admission.daemon_id.clone(), - r#type: IdunnSignedDaemonHealthRecord::TYPE.into(), - current: expected_statement, - }, - ]; - let statement_envelope = CultCacheEnvelope { - key: admission.daemon_id.clone(), - r#type: IdunnSignedDaemonHealthRecord::TYPE.into(), - payload: statement_payload.clone(), - stored_at: admitted_at.to_string(), - schema_id: Some(IdunnSignedDaemonHealthRecord::TYPE.into()), - }; - let replacements = [ - typed_envelope(&health.daemon_id, &health, admitted_at)?, - typed_envelope(&admission.daemon_id, &admission, admitted_at)?, - statement_envelope, - ]; - if SingleFileMessagePackBackingStore::new(&options.store_path) - .compare_exchange(&expected, &replacements)? - { - return Ok(HealthIngressOutcome { - daemon_id: health.daemon_id, - state: health.state, - detail: health.detail, - authority: "authenticated", - }); - } - } - Err(anyhow!( - "signed daemon health admission lost repeated cross-process contention" - )) -} - -fn generic_signed_health_advances( - existing: &IdunnAuthenticatedDaemonHealthAdmissionRecord, - candidate: &IdunnAuthenticatedDaemonHealthAdmissionRecord, -) -> bool { - if existing.daemon_id != candidate.daemon_id { - return false; - } - let same_trust_binding = existing.trust_binding_id == candidate.trust_binding_id - && existing.trust_binding_sha256 == candidate.trust_binding_sha256 - && existing.signer_identity_id == candidate.signer_identity_id; - if !same_trust_binding { - // `candidate` has already been verified against the one exact binding - // in the current root-owned trust store. The previous admission is - // replay memory, not authority over a legitimate signer rotation. - return candidate.observed_at_unix_millis > existing.observed_at_unix_millis; - } - if existing.publisher_incarnation_id == candidate.publisher_incarnation_id { - candidate.publisher_sequence > existing.publisher_sequence - && candidate.observed_at_unix_millis >= existing.observed_at_unix_millis - } else { - candidate.observed_at_unix_millis > existing.observed_at_unix_millis - } -} - -fn validate_generic_release_binding( - node: &mut CultMeshNode, - admission: &IdunnAuthenticatedDaemonHealthAdmissionRecord, -) -> Result<()> { - let Some(deployment_id) = admission.deployment_id.as_deref() else { - return Ok(()); - }; - let request = node - .get::(deployment_id)? - .ok_or_else(|| anyhow!("signed daemon health names an unknown deployment request"))?; - let authority_shape_is_valid = if request.requires_bifrost_authority { - !request.release_authority_id.trim().is_empty() - && !request.release_authority_envelope_sha256.trim().is_empty() - } else { - request.release_authority_id.is_empty() - && request.release_authority_envelope_sha256.is_empty() - }; - if request.daemon_id != admission.daemon_id - || admission.source_commit.as_deref() != Some(request.source_revision.as_str()) - || !authority_shape_is_valid - { - return Err(anyhow!( - "signed daemon health deployment authority is invalid" - )); - } - let current = node - .get::(&admission.daemon_id)? - .ok_or_else(|| anyhow!("signed daemon health has no current deployment head"))?; - if current.request_id != request.request_id || current.sequence == 0 { - return Err(anyhow!( - "signed daemon health names a superseded deployment" - )); - } - let requested_at = parse_timestamp_millis(&request.requested_at) - .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; - if admission.observed_at_unix_millis < requested_at - || admission.admitted_at_unix_millis < requested_at - { - return Err(anyhow!("signed daemon health predates its deployment")); - } - Ok(()) -} - -#[cfg(test)] -fn health_from_rudp_message( - message: &CultNetMessage, - options: &CommonOptions, -) -> Result { - match decode_health_from_rudp_message(message, options, ×tamp()?)? { - DecodedHealthIngress::AuthenticatedLegacy { health, .. } - | DecodedHealthIngress::AuthenticatedGeneric { health, .. } => Ok(health), - DecodedHealthIngress::Diagnostic(_) => { - Err(anyhow!("unsigned daemon health is diagnostic-only")) - } - } -} - -fn decode_health_from_rudp_message( - message: &CultNetMessage, - options: &CommonOptions, - admitted_at: &str, -) -> Result { - let CultNetMessage::DocumentPutRaw { document, .. } = message else { - return Err(anyhow!("expected cultnet.document_put_raw.v0")); - }; - if document.payload_encoding != CultNetRawPayloadEncoding::Messagepack { - return Err(anyhow!("expected MessagePack raw payload encoding")); - } - if document.schema_id == SIGNED_DAEMON_HEALTH_SCHEMA_ID { - let (health, statement_payload, admission) = - authenticate_generic_signed_health(document, options, admitted_at)?; - return Ok(DecodedHealthIngress::AuthenticatedGeneric { - health, - statement_payload, - admission, - }); - } - if document.schema_id == EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE { - let (health, admission) = - authenticate_epiphany_signed_health(document, options, admitted_at)?; - return Ok(DecodedHealthIngress::AuthenticatedLegacy { health, admission }); - } - if document.schema_id != "idunn.daemon_health" { - return Err(anyhow!( - "expected signed Epiphany or idunn.daemon_health schema, received {}", - document.schema_id - )); - } - let wire: IdunnDaemonHealthWireV1 = rmp_serde::from_slice(&document.payload)?; - if wire.health_contract == EPIPHANY_HEALTH_CONTRACT { - return Err(anyhow!( - "Epiphany health requires its signed runtime-health schema" - )); - } - if document.record_key != wire.daemon_id { - return Err(anyhow!( - "record key {} does not match health daemon_id {}", - document.record_key, - wire.daemon_id - )); - } - let received_at_unix_millis = parse_timestamp_millis(admitted_at) - .ok_or_else(|| anyhow!("unsigned health diagnostic receive time is invalid"))?; - let diagnostic = IdunnUnsignedDaemonHealthDiagnosticRecord { - schema_version: IDUNN_UNSIGNED_DAEMON_HEALTH_DIAGNOSTIC_SCHEMA.into(), - diagnostic_id: format!("diagnostic:{}", wire.daemon_id), - daemon_id: wire.daemon_id, - claimed_state: wire.state, - claimed_detail: wire.detail, - claimed_observed_at: wire.observed_at, - claimed_health_contract: wire.health_contract, - transport_source_runtime_id: document.source_runtime_id.clone(), - transport_source_role: document.source_role.clone(), - received_at_unix_millis, - authority: "diagnostic-only".into(), - private_state_exposed: false, - }; - diagnostic.validate()?; - Ok(DecodedHealthIngress::Diagnostic(diagnostic)) -} - -fn authenticate_epiphany_signed_health( - document: &cultnet_rs::CultNetRawDocumentRecord, - options: &CommonOptions, - admitted_at: &str, -) -> Result<(IdunnDaemonHealthRecord, IdunnSignedHealthAdmissionRecord)> { - let signed: EpiphanySignedRuntimeHealthWire = rmp_serde::from_slice(&document.payload) - .context("decoding signed Epiphany runtime health")?; - validate_epiphany_signed_health_shape(&signed)?; - if document.record_key != signed.health.daemon_id - || document.source_runtime_id.as_deref() != Some(EPIPHANY_HEALTH_SOURCE_RUNTIME) - || document.source_role.as_deref() != Some("daemon-health-publisher") - { - return Err(anyhow!( - "signed Epiphany health transport identity is invalid" - )); - } - let identity_store = options - .trusted_epiphany_health_identity_store - .as_deref() - .ok_or_else(|| anyhow!("signed Epiphany health has no configured trust anchor"))?; - let identity = load_epiphany_health_identity(identity_store)?; - if signed.signer_identity_id != identity.identity_id { - return Err(anyhow!( - "signed Epiphany health signer is not the configured host identity" - )); - } - let key_bytes: [u8; 32] = identity - .public_key - .as_slice() - .try_into() - .map_err(|_| anyhow!("Epiphany health identity public key length is invalid"))?; - let signature_bytes: [u8; 64] = signed - .signature - .as_slice() - .try_into() - .map_err(|_| anyhow!("signed Epiphany health signature length is invalid"))?; - let mut unsigned = signed.clone(); - unsigned.signature.clear(); - let statement = rmp_serde::to_vec_named(&unsigned)?; - VerifyingKey::from_bytes(&key_bytes)? - .verify( - &host_signature_message(EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, &statement), - &Signature::from_bytes(&signature_bytes), - ) - .context("signed Epiphany health signature is invalid")?; - let digest = format!("sha256-{:x}", Sha256::digest(&document.payload)); - let health = IdunnDaemonHealthRecord { - daemon_id: signed.health.daemon_id, - state: signed.health.state, - detail: signed.health.detail, - observed_at: signed.health.observed_at, - health_contract: signed.health.health_contract, - publication_source: "daemon-published".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }; - let admission = IdunnSignedHealthAdmissionRecord { - daemon_id: health.daemon_id.clone(), - state: health.state.clone(), - observed_at: health.observed_at.clone(), - admitted_at: admitted_at.to_string(), - health_contract: health.health_contract.clone(), - deployment_request_id: signed.deployment_request_id, - release_id: signed.release_id, - release_witness_sha256: signed.release_witness_sha256, - source_commit: signed.source_commit, - publisher_incarnation_id: signed.publisher_incarnation_id, - publisher_sequence: signed.publisher_sequence, - publisher_process_created_at: signed.publisher_process_created_at, - signer_identity_id: signed.signer_identity_id, - signed_health_sha256: digest, - }; - Ok((health, admission)) -} - -fn validate_epiphany_signed_health_shape(signed: &EpiphanySignedRuntimeHealthWire) -> Result<()> { - if signed.schema_version != EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION - || signed.health.daemon_id != "yggdrasil-epiphany" - || signed.health.health_contract != EPIPHANY_HEALTH_CONTRACT - || signed.health.publication_source != "daemon-published" - || signed.health.transport != CULTNET_RUDP_PROTOCOL_ID - || !matches!( - signed.health.state.as_str(), - "active" | "warming" | "degraded" | "failed" - ) - || signed.source_runtime_id != EPIPHANY_HEALTH_SOURCE_RUNTIME - || signed.deployment_request_id.trim().is_empty() - || signed.release_id.trim().is_empty() - || signed.publisher_sequence == 0 - || signed.publisher_process_id == 0 - || signed.publisher_process_creation_token == 0 - || signed.publisher_executable_path.trim().is_empty() - || signed.signature_algorithm != "ed25519" - || signed.signature.len() != 64 - { - return Err(anyhow!("signed Epiphany health shape is invalid")); - } - uuid::Uuid::parse_str(&signed.publisher_incarnation_id) - .context("signed health publisher incarnation must be UUID")?; - chrono::DateTime::parse_from_rfc3339(&signed.health.observed_at)?; - chrono::DateTime::parse_from_rfc3339(&signed.publisher_process_created_at)?; - validate_sha256(&signed.release_witness_sha256, "release witness")?; - if signed.source_commit.len() != 40 - || !signed - .source_commit - .bytes() - .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) - { - return Err(anyhow!("signed Epiphany health source commit is invalid")); - } - Ok(()) -} - -fn load_epiphany_health_identity(path: &std::path::Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let envelope = entries - .iter() - .find(|entry| entry.r#type == HOST_IDENTITY_TYPE && entry.key == HOST_IDENTITY_KEY) - .ok_or_else(|| anyhow!("configured Epiphany health identity is absent"))?; - let identity: EpiphanyHostIdentityWire = rmp_serde::from_slice(&envelope.payload)?; - let expected_id = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, identity.public_key.as_slice()].concat()) - ); - if identity.schema_version != HOST_IDENTITY_TYPE - || identity.identity_id != expected_id - || identity.public_key.len() != 32 - || identity.assurance.trim().is_empty() - || chrono::DateTime::parse_from_rfc3339(&identity.identity_created_at).is_err() - || validate_sha256(&identity.source_identity_record_sha256, "identity record").is_err() - { - return Err(anyhow!("configured Epiphany health identity is invalid")); - } - Ok(identity) -} - -fn validate_sha256(value: &str, label: &str) -> Result<()> { - let digest = value.strip_prefix("sha256-").unwrap_or(value); - if digest.len() != 64 - || !digest - .bytes() - .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) - { - return Err(anyhow!("signed Epiphany health {label} is invalid")); - } - Ok(()) -} - -fn host_signature_message(purpose: &str, payload: &[u8]) -> Vec { - let mut message = - Vec::with_capacity(HOST_SIGNATURE_DOMAIN.len() + purpose.len() + payload.len() + 16); - message.extend_from_slice(HOST_SIGNATURE_DOMAIN); - message.extend_from_slice(&(purpose.len() as u64).to_be_bytes()); - message.extend_from_slice(purpose.as_bytes()); - message.extend_from_slice(&(payload.len() as u64).to_be_bytes()); - message.extend_from_slice(payload); - message -} - -fn signed_health_advances( - existing: &IdunnSignedHealthAdmissionRecord, - candidate: &IdunnSignedHealthAdmissionRecord, -) -> Result { - if existing.signer_identity_id != candidate.signer_identity_id { - return Ok(false); - } - if existing.publisher_incarnation_id == candidate.publisher_incarnation_id { - return Ok(candidate.publisher_sequence > existing.publisher_sequence); - } - let existing_created = - chrono::DateTime::parse_from_rfc3339(&existing.publisher_process_created_at)?; - let candidate_created = - chrono::DateTime::parse_from_rfc3339(&candidate.publisher_process_created_at)?; - Ok(candidate_created > existing_created) -} - -fn validate_admission_against_current_request( - node: &mut CultMeshNode, - admission: &IdunnSignedHealthAdmissionRecord, -) -> Result<()> { - let request = node - .get::(&admission.deployment_request_id)? - .ok_or_else(|| anyhow!("signed health names an unknown deployment request"))?; - if request.daemon_id != admission.daemon_id - || request.source_revision != admission.source_commit - || request.repository_full_name != "GameCult/Epiphany" - || request.upstream_ref != "refs/heads/codex/epiphany-shakedown-live" - || request.requires_bifrost_authority - || !request.release_authority_id.is_empty() - || !request.release_authority_envelope_sha256.is_empty() - { - return Err(anyhow!( - "signed health does not match an exact Idunn source-observed Epiphany deployment request" - )); - } - let request_time = parse_timestamp_seconds(&request.requested_at) - .ok_or_else(|| anyhow!("deployment request timestamp is invalid"))?; - let observed_time = parse_timestamp_seconds(&admission.observed_at) - .ok_or_else(|| anyhow!("signed health observation timestamp is invalid"))?; - let admitted_time = parse_timestamp_seconds(&admission.admitted_at) - .ok_or_else(|| anyhow!("signed health admission timestamp is invalid"))?; - if observed_time < request_time || admitted_time < request_time { - return Err(anyhow!( - "signed health predates the deployment request it claims" - )); - } - let current = node - .get::(&admission.daemon_id)? - .ok_or_else(|| anyhow!("Idunn has no current deployment request authority head"))?; - if current.request_id != request.request_id || current.sequence == 0 { - return Err(anyhow!( - "signed health is bound to a superseded deployment request" - )); - } - Ok(()) -} - -fn validate_admission_fresh_at( - admission: &IdunnSignedHealthAdmissionRecord, - now: &str, - max_age_seconds: u64, -) -> Result<()> { - let observed = parse_timestamp_seconds(&admission.observed_at) - .ok_or_else(|| anyhow!("signed health observation timestamp is invalid"))?; - let admitted = parse_timestamp_seconds(&admission.admitted_at) - .ok_or_else(|| anyhow!("signed health admission timestamp is invalid"))?; - let now = - parse_timestamp_seconds(now).ok_or_else(|| anyhow!("Idunn admission clock is invalid"))?; - if observed > admitted || admitted > now { - return Err(anyhow!( - "signed health admission carries a future or causally inverted timestamp" - )); - } - if now.saturating_sub(observed) > max_age_seconds - || now.saturating_sub(admitted) > max_age_seconds - { - return Err(anyhow!("signed health admission is stale")); - } - Ok(()) -} - -fn publish_surgery_plans( - profile: &str, - targets: &[DaemonTarget], - options: &CommonOptions, - store_lock: &Arc>, - updated_at: &str, -) -> Result<()> { - let plans = daemon_surgery_plans(targets, updated_at); - let swarm_plan = swarm_surgery_plan(profile, targets, &plans, updated_at); - with_store_node(options, store_lock, |node| { - node.put(&swarm_plan.plan_id, &swarm_plan)?; - for target in targets { - let transport_profile = daemon_transport_profile(target, updated_at); - let command_boundary = command_boundary(target, updated_at); - node.put(&transport_profile.profile_id, &transport_profile)?; - node.put(&command_boundary.boundary_id, &command_boundary)?; - if let Some(release) = &target.release { - let release_record = release_target_record( - target, - release, - updated_at, - options.release_authority_store_path.as_deref(), - ); - let artifact = deployment_artifact_record(target, &release_record, updated_at); - let migration_plan = - state_migration_plan_record(target, release, &release_record, updated_at); - let rollout_plan = rollout_plan_record( - target, - release, - &release_record, - &artifact, - &migration_plan, - updated_at, - ); - node.put(&release_record.target_id, &release_record)?; - node.put(&artifact.artifact_id, &artifact)?; - node.put(&migration_plan.plan_id, &migration_plan)?; - node.put(&rollout_plan.plan_id, &rollout_plan)?; - } - } - for plan in &plans { - node.put(&plan.plan_id, plan)?; - } - Ok(()) - }) -} - -fn swarm_surgery_plan( - profile: &str, - targets: &[DaemonTarget], - plans: &[IdunnDaemonSurgeryPlanRecord], - updated_at: &str, -) -> IdunnSwarmSurgeryPlanRecord { - let next_target = preferred_next_target(targets, plans).unwrap_or("none"); - - IdunnSwarmSurgeryPlanRecord { - plan_id: format!("swarm-surgery:{profile}"), - profile: profile.to_string(), - status: "active-transport-migration".to_string(), - owner: "Idunn swarm supervisor".to_string(), - objective: - "Move daemon awareness from debug probes to daemon-published typed CultNet/RUDP state." - .to_string(), - current_mechanism: - "Idunn publishes per-daemon desired state, surgery plans, transport profiles, command boundaries, runtime RUDP self-checks, and RUDP health ingress; command probes are debug witnesses only." - .to_string(), - invariants: vec![ - "Daemon truth is typed CultCache/CultMesh state carried over cultnet.transport.rudp.v0.".to_string(), - "Product/debug probes are evidence only and must not own daemon health.".to_string(), - "Idunn consumes daemon-published RUDP health before debug probes and actuates only advertised lifecycle authority.".to_string(), - "Each migrated daemon must publish the same health contract that Idunn expects for its target.".to_string(), - "Shared operator hosts such as Raven must be actuated by background-only launch paths that do not create visible terminal or interactive windows.".to_string(), - "Raven Task Scheduler actions must execute hidden launchers directly, not visible .cmd trampolines.".to_string(), - ], - phases: vec![ - "1. Publish Idunn's own RUDP substrate and ingress state.".to_string(), - "2. Install daemon-published RUDP health in one Rust daemon and prove Idunn consumes it live.".to_string(), - "3. Extend CultLib RUDP publication support across TypeScript, C#, and remaining daemon runtimes.".to_string(), - "4. Promote provider advertisements, command boundaries, and transport profiles to daemon-owned CultNet/RUDP records.".to_string(), - "5. Delete or demote debug probes once every target has daemon-owned publication and advertised lifecycle authority.".to_string(), - ], - current_phase: - "Phase 25: Yggdrasil Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP and publishes the headless aggregate; Nightwing's framebuffer units are retired, and the remaining debt is demoting debug probes and bridge-only lowerings without letting them retake ownership." - .to_string(), - next_target: next_target.to_string(), - cut_line: - concat!( - "Muninn, Vili, Idunn, Odin, Stonks, Weksa, VoidBot, Yggdrasil Gjallar, Mimir Eve dashboard, Nightwing Eve dashboard, Nightwing Eve browser reference, yggdrasil-streampixels, yggdrasil-heimdall, and yggdrasil-repixelizer now exercise daemon-owned RUDP health. ", - "Raven GameCult\\Vili has been refreshed from Odin, the hidden scheduled task now launches start-vili-daemon.ps1 with explicit Idunn RUDP arguments, and live Idunn accepts vili.cultnet-rudp-animation-health through the configured health route. ", - "VoidBot publishes daemon-owned provider catalog, provider advertisement, command_boundary, and transport_profile records from E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc while health-voidbot.cmd is archived. ", - "Raven Muninn publishes provider advertisement, command_boundary, and transport_profile records from C:\\Meta\\Odin\\state\\muninn.telemetry.cc, keeps activation commands in C:\\Meta\\Odin\\state\\muninn.activate.cc, and no longer lets plain serve infer ambient Move runtime authority from platform defaults. Starfire Muninn publishes the same daemon-owned witness records from C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc, Nightwing Muninn publishes the same daemon-owned witness records from /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc, and live Idunn accepts Muninn-family daemon health by daemon id and configured health contract rather than source-address folklore. ", - "Nightwing's restart wrapper makes Move runtime authority explicit with -DiscoverMoveState -ClaimUsbMoves instead of silently adding Move evidence on plain serve. Raven's GameCult-Muninn, GameCult-Muninn-Activate, and GameCult-Muninn-VideoProof tasks execute wscript.exe actions whose hidden VBS launchers call noninteractive hidden PowerShell entrypoints directly; health .cmd wrappers are archived instead of command-probe health paths. ", - "Live yggdrasil-streampixels publishes streampixels.cultnet-rudp-service-health through its configured Yggdrasil health route, live yggdrasil-heimdall publishes heimdall.cultnet-rudp-provider-health with a boundary store at /srv/heimdall/cultcache/heimdall.service.cc, and live yggdrasil-repixelizer publishes repixelizer.cultnet-rudp-service-health with a boundary store at /srv/repixelizer/cultcache/repixelizer.service.cc. ", - "Live Yggdrasil Gjallar publishes /var/lib/gamecult/gjallar/gjallar.service.cc plus a daemon-owned gamecult.eve.surface_state witness, consumes Odin's accepted surface:gamecult.network.status snapshot through the configured Gjallar Odin endpoint, and publishes gjallar.overview; Nightwing's Gjallar/framebuffer units are retired. Live Odin ingests remote Raven Vili while accepting Yggdrasil-local Gjallar publications directly over RUDP. ", - "Live Mimir Eve dashboard publishes CultMesh state at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp plus a boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc while health-nightwing-eve-dashboard.ps1 inspects those witnesses directly, and live Nightwing Eve browser reference publishes a boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc while health-nightwing-eve-browser-reference.ps1 inspects that store directly. The remaining debt is deleting bridge-only lowerings where they are no longer useful and keeping debug probes unable to own daemon truth." - ) - .to_string(), - verification_layer: - "CultMesh keepalive store records plus live Idunn decision cycles, not process exit codes or chat summaries." - .to_string(), - updated_at: updated_at.to_string(), - } -} - -fn preferred_next_target<'a>( - targets: &'a [DaemonTarget], - plans: &'a [IdunnDaemonSurgeryPlanRecord], -) -> Option<&'a str> { - let plan_by_daemon = plans - .iter() - .map(|plan| (plan.daemon_id.as_str(), plan.status.as_str())) - .collect::>(); - let priority = |status: &str| match status { - "transport-surgery-required" => 0, - "partial-rudp-health-live" => 1, - "partial-rudp-health-and-provider-store-live" => 2, - "catalog-coherence-probe" => 3, - _ => 4, - }; - targets - .iter() - .filter(|target| target.enabled) - .filter(|target| target.daemon_id != "idunn-swarm-deployment-coverage") - .filter_map(|target| { - let status = *plan_by_daemon.get(target.daemon_id.as_str())?; - Some((target.daemon_id.as_str(), status, priority(status))) - }) - .filter(|(_, status, _)| *status != "partial-rudp-health-and-provider-store-live") - .min_by_key(|(_, _, rank)| *rank) - .map(|(daemon_id, _, _)| daemon_id) -} - -fn transport_profile_id(target: &DaemonTarget) -> String { - format!("transport:{}", target.daemon_id) -} - -fn command_boundary_id(target: &DaemonTarget) -> String { - format!("command-boundary:{}", target.daemon_id) -} - -fn release_target_id(target: &DaemonTarget) -> String { - format!("release-target:{}", target.daemon_id) -} - -fn rollout_plan_id(target: &DaemonTarget) -> String { - format!("rollout:{}:main", target.daemon_id) -} - -fn migration_plan_id(target: &DaemonTarget) -> String { - format!("migration:{}:main", target.daemon_id) -} - -fn artifact_id(target: &DaemonTarget) -> String { - format!("artifact:{}:main", target.daemon_id) -} - -fn release_target( - repo: &str, - repo_path: PathBuf, - rollout_strategy: &str, - state_migration_command: Option<&str>, - zero_downtime_capability: &str, -) -> ReleaseTarget { - let mut target = ReleaseTarget { - repo: repo.to_string(), - repository_full_name: format!("GameCult/{repo}"), - repo_path, - upstream_remote: "origin".to_string(), - upstream_branch: "main".to_string(), - rollout_strategy: rollout_strategy.to_string(), - state_migration_command: state_migration_command.map(ToString::to_string), - zero_downtime_capability: zero_downtime_capability.to_string(), - deployed_revision_witness: None, - artifact_strategy: "source-archive-from-observed-ref".to_string(), - artifact_witness_root: None, - requires_bifrost_authority: false, - source_change_pathspecs: Vec::new(), - minimum_source_revision: None, - }; - refresh_compiled_minimum_source_revision(&mut target); - target -} - -fn release_target_on_branch( - repo: &str, - repo_path: PathBuf, - upstream_branch: &str, - rollout_strategy: &str, - state_migration_command: Option<&str>, - zero_downtime_capability: &str, -) -> ReleaseTarget { - let mut target = release_target( - repo, - repo_path, - rollout_strategy, - state_migration_command, - zero_downtime_capability, - ); - target.upstream_branch = upstream_branch.to_string(); - refresh_compiled_minimum_source_revision(&mut target); - target -} - -fn refresh_compiled_minimum_source_revision(release: &mut ReleaseTarget) { - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - release.minimum_source_revision = - compiled_minimum_source_revision_policy(&release.repository_full_name, &upstream_ref) - .map(|policy| policy.minimum_source_revision.to_string()); -} - -fn requiring_bifrost_authority(mut release: ReleaseTarget) -> ReleaseTarget { - release.requires_bifrost_authority = true; - release -} - -fn with_deployed_revision_witness(mut release: ReleaseTarget, path: PathBuf) -> ReleaseTarget { - release.deployed_revision_witness = Some(path); - release -} - -fn with_container_artifact_witness(mut release: ReleaseTarget, root: PathBuf) -> ReleaseTarget { - release.artifact_strategy = "immutable-docker-image-id".to_string(); - release.artifact_witness_root = Some(root); - release -} - -fn with_source_change_pathspecs(mut release: ReleaseTarget, pathspecs: &[&str]) -> ReleaseTarget { - release.source_change_pathspecs = pathspecs.iter().map(|value| (*value).to_string()).collect(); - release -} - -fn release_target_record( - target: &DaemonTarget, - release: &ReleaseTarget, - observed_at: &str, - authority_store_path: Option<&std::path::Path>, -) -> IdunnReleaseTargetRecord { - let port = SystemReleaseStatePort; - let fetch_result = port.fetch(release); - let observed_upstream_revision = fetch_result - .as_ref() - .ok() - .and_then(|_| port.desired_revision(release).ok()) - .unwrap_or_else(|| "unknown".to_string()); - let deployed_revision = if release.deployed_revision_witness.is_some() { - port.deployed_revision(release) - .unwrap_or_else(|_| "unknown".to_string()) - } else { - "untracked-no-witness".to_string() - }; - let authorization = if release.requires_bifrost_authority && fetch_result.is_ok() { - let upstream_ref = format!("refs/heads/{}", release.upstream_branch); - authority_store_path.and_then(|store_path| { - CultCacheReleaseAuthorityPort { store_path } - .select(&release.repository_full_name, &upstream_ref, observed_at) - .ok() - .filter(|selected| { - git_revision(&release.repo_path, &selected.source_revision).as_deref() - == Some(selected.source_revision.as_str()) - }) - }) - } else { - None - }; - let desired_revision = authorization - .as_ref() - .map(|value| value.source_revision.clone()) - .unwrap_or_else(|| observed_upstream_revision.clone()); - let minimum_source_revision_satisfied = desired_revision != "unknown" - && require_release_minimum_source_revision(target, release, &desired_revision).is_ok(); - let status = if fetch_result.is_ok() - && observed_upstream_revision != "unknown" - && (release.deployed_revision_witness.is_none() || deployed_revision != "unknown") - && (!release.requires_bifrost_authority || authorization.is_some()) - && minimum_source_revision_satisfied - { - if release.requires_bifrost_authority { - "tracked-authorized" - } else { - "tracked-source-authority" - } - } else if !minimum_source_revision_satisfied { - "below-compiled-minimum-source-revision" - } else { - "release-authority-unavailable" - }; - - IdunnReleaseTargetRecord { - target_id: release_target_id(target), - daemon_id: target.daemon_id.clone(), - repo: release.repo.clone(), - repo_path: release.repo_path.display().to_string(), - upstream_remote: release.upstream_remote.clone(), - upstream_branch: release.upstream_branch.clone(), - desired_revision, - deployed_revision, - artifact_strategy: release.artifact_strategy.clone(), - rollout_strategy: release.rollout_strategy.clone(), - state_migration_authority: if release.state_migration_command.is_some() { - "daemon-owned-command" - } else { - "daemon-declares-no-migration-required" - } - .to_string(), - zero_downtime_capability: release.zero_downtime_capability.clone(), - status: status.to_string(), - observed_at: observed_at.to_string(), - repository_full_name: release.repository_full_name.clone(), - upstream_ref: format!("refs/heads/{}", release.upstream_branch), - release_authority_id: authorization - .as_ref() - .map(|value| value.authority_id.clone()) - .unwrap_or_default(), - release_authority_envelope_sha256: authorization - .as_ref() - .map(|value| value.envelope_sha256.clone()) - .unwrap_or_default(), - release_authority_status: if authorization.is_some() { - "authorized" - } else if !release.requires_bifrost_authority { - "source-observed" - } else { - "unavailable" - } - .to_string(), - requires_bifrost_authority: release.requires_bifrost_authority, - observed_upstream_revision, - minimum_source_revision: release.minimum_source_revision.clone(), - } -} - -fn deployment_artifact_record( - target: &DaemonTarget, - release: &IdunnReleaseTargetRecord, - built_at: &str, -) -> IdunnDeploymentArtifactRecord { - let witnessed_image = target - .release - .as_ref() - .and_then(|configured| configured.artifact_witness_root.as_ref()) - .and_then(|root| read_container_artifact_witness(root, &release.desired_revision).ok()); - IdunnDeploymentArtifactRecord { - artifact_id: artifact_id(target), - daemon_id: target.daemon_id.clone(), - source_revision: release.desired_revision.clone(), - source_branch: release.upstream_branch.clone(), - source_remote: release.upstream_remote.clone(), - artifact_kind: release.artifact_strategy.clone(), - artifact_uri: witnessed_image - .as_ref() - .map(|image_id| format!("docker-image://{image_id}")) - .unwrap_or_else(|| "not-yet-constructed".to_string()), - sha256: witnessed_image.unwrap_or_else(|| "not-yet-constructed".to_string()), - built_at: built_at.to_string(), - release_authority_id: release.release_authority_id.clone(), - release_authority_envelope_sha256: release.release_authority_envelope_sha256.clone(), - } -} - -fn read_container_artifact_witness(root: &Path, source_revision: &str) -> Result { - let witness = fs::read_to_string(root.join(format!("{source_revision}.env")))?; - let fields = witness - .lines() - .filter_map(|line| line.split_once('=')) - .collect::>(); - let image_id = fields - .get("image_id") - .ok_or_else(|| anyhow!("container artifact witness has no image_id"))?; - if fields.get("schema") != Some(&"idunn.epiphany_image_artifact.v1") - || fields.get("source_commit") != Some(&source_revision) - || !image_id.starts_with("sha256:") - || image_id.len() != 71 - || !image_id[7..] - .bytes() - .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) - { - return Err(anyhow!("container artifact witness is invalid")); - } - Ok((*image_id).to_string()) -} - -fn state_migration_plan_record( - target: &DaemonTarget, - release: &ReleaseTarget, - _release_record: &IdunnReleaseTargetRecord, - planned_at: &str, -) -> IdunnStateMigrationPlanRecord { - let command = release - .state_migration_command - .clone() - .unwrap_or_else(|| "none".to_string()); - IdunnStateMigrationPlanRecord { - plan_id: migration_plan_id(target), - daemon_id: target.daemon_id.clone(), - from_schema_version: "deployed-state".to_string(), - to_schema_version: "target-revision-state".to_string(), - authority: if release.state_migration_command.is_some() { - "daemon-owned-migrator" - } else { - "daemon-declared-noop" - } - .to_string(), - command, - strategy: if release.state_migration_command.is_some() { - "backup-then-daemon-migrator" - } else { - "noop" - } - .to_string(), - backup_required: release.state_migration_command.is_some(), - zero_downtime_required: release.zero_downtime_capability == "zero-downtime", - status: "planned".to_string(), - planned_at: planned_at.to_string(), - } -} - -fn rollout_plan_record( - target: &DaemonTarget, - release: &ReleaseTarget, - release_record: &IdunnReleaseTargetRecord, - artifact: &IdunnDeploymentArtifactRecord, - migration_plan: &IdunnStateMigrationPlanRecord, - planned_at: &str, -) -> IdunnRolloutPlanRecord { - let mut phases = vec![ - "fetch the admitted upstream ref into a managed release view".to_string(), - "build source artifact from the desired upstream revision".to_string(), - "snapshot state before any non-noop migration".to_string(), - "run daemon-owned migration command when declared".to_string(), - "deploy through the advertised Idunn command boundary".to_string(), - "verify daemon-published RUDP health and deployment manifest".to_string(), - ]; - if release.zero_downtime_capability == "zero-downtime" { - phases.insert( - 4, - "shift traffic or reload in place without dropping the active service".to_string(), - ); - } else { - phases.insert( - 4, - "record restart-required because this daemon has no zero-downtime swap authority" - .to_string(), - ); - } - - IdunnRolloutPlanRecord { - plan_id: rollout_plan_id(target), - daemon_id: target.daemon_id.clone(), - desired_revision: release_record.desired_revision.clone(), - deployed_revision: release_record.deployed_revision.clone(), - strategy: release.rollout_strategy.clone(), - phases, - migration_plan_id: migration_plan.plan_id.clone(), - artifact_id: artifact.artifact_id.clone(), - status: "planned".to_string(), - planned_at: planned_at.to_string(), - } -} - -fn git_revision(repo_path: &Path, rev: &str) -> Option { - let output = Command::new("git") - .arg("-C") - .arg(repo_path) - .arg("rev-parse") - .arg(rev) - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .output() - .ok()?; - if !output.status.success() { - return None; - } - let value = String::from_utf8_lossy(&output.stdout).trim().to_string(); - if value.is_empty() { None } else { Some(value) } -} - -fn daemon_transport_profile( - target: &DaemonTarget, - observed_at: &str, -) -> IdunnDaemonTransportProfileRecord { - let (current_transport, state, cut_line) = match target.daemon_id.as_str() { - "stonks" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-provider-store + odin-cultmesh-command-documents", - "partial-rudp-health-and-provider-store-live", - "Stonks daemon health is published over CultNet/RUDP, and provider advertisement, market snapshot, Eve surface, command_boundary, and transport_profile records are in the daemon-owned CultCache store. Renderer/debug surfaces live outside daemon transport authority.", - ), - "weksa" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-provider-store + odin-cultmesh-command-documents", - "partial-rudp-health-and-provider-store-live", - "Weksa daemon health is published over CultNet/RUDP, provider advertisement, operator state, Eve surface, command_boundary, and transport_profile records are in the daemon-owned CultCache store, and speech_provider.mimo.voicedesign now arrives as typed CultMesh/CultNet command documents.", - ), - "voidbot" => ( - "daemon-published-rudp-health + daemon-owned-cultmesh-provider-store", - "partial-rudp-health-and-provider-store-live", - "VoidBot stack health is published over CultNet/RUDP from the local orchestrator pulse, and the daemon-owned CultMesh witness store at E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc carries provider advertisement catalog, provider advertisement, command_boundary, and transport_profile records. The operations probe is a debug witness only.", - ), - "yggdrasil-gjallar" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-aggregate-surface + local-odin-cultnet-rudp-snapshot-input", - "partial-rudp-health-and-provider-store-live", - "Gjallar composition health and its typed aggregate Eve surface are published from Yggdrasil beside Odin. The daemon owns tiling and aggregation; clients own pixel lowering, and no framebuffer is opened by the daemon.", - ), - "nightwing-eve-dashboard" => ( - "daemon-published-rudp-health + daemon-owned-cultmesh-state + daemon-owned-cultcache-boundary-store", - "partial-rudp-health-and-provider-store-live", - "Nightwing Eve dashboard service health is published over CultNet/RUDP from the Mimir.EveDashboard systemd process, and the live broker publishes CultMesh state at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp plus a daemon-owned boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc carrying typed provider advertisement, command_boundary, transport_profile, and daemon-health summary state. Client renderers must lower those typed witnesses without becoming daemon transport authority.", - ), - "nightwing-eve-browser-reference" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-boundary-store", - "partial-rudp-health-and-provider-store-live", - "Nightwing Eve browser reference health is published over CultNet/RUDP from the Mimir.EveBrowserReference service process, and the runtime publishes a daemon-owned boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc carrying manifest, static-surface, command_boundary, transport_profile, and daemon-health summary state. Browser serving is a renderer surface, while health-nightwing-eve-browser-reference.ps1 inspects the daemon-owned witness directly.", - ), - "muninn" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-telemetry-store + background-only hidden task launch", - "partial-rudp-health-and-provider-store-live", - "Raven Muninn publishes muninn.cultnet-rudp-remote-telemetry-health over CultNet/RUDP from the long-running hidden GameCult-Muninn serve task, and the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\muninn.telemetry.cc carries provider advertisement, command_boundary, transport_profile, and telemetry surface records. Background-only launch remains an ops invariant; local commands are command/debug lowerings only.", - ), - "starfire-muninn" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-telemetry-store", - "partial-rudp-health-and-provider-store-live", - "Starfire Muninn now publishes muninn.cultnet-rudp-local-telemetry-and-quest-access over CultNet/RUDP from the long-running local serve process, and the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc now carries provider advertisement, command_boundary, transport_profile, Quest access, and telemetry surface records. Quest ADB availability stays telemetry state, not daemon liveness.", - ), - "nightwing-muninn" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-telemetry-store", - "partial-rudp-health-and-provider-store-live", - "Nightwing Muninn now publishes muninn.cultnet-rudp-remote-telemetry-and-move-hid over CultNet/RUDP from the long-running serve process, and the daemon-owned telemetry store at /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc now carries provider advertisement, command_boundary, transport_profile, Move HID evidence, and telemetry surface records.", - ), - "vili" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-service-boundary", - "partial-rudp-health-and-provider-store-live", - "Vili now publishes vili.cultnet-rudp-animation-health over CultNet/RUDP from the hidden Raven GameCult\\Vili task, and its daemon-owned vili.service.cc store contains provider advertisement, operator state, Eve surface, command_boundary, and transport_profile records. Operator/debug surfaces must not become daemon health or command transport.", - ), - "yggdrasil-streampixels" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-service-boundary", - "partial-rudp-health-and-provider-store-live", - "StreamPixels publishes daemon-owned RUDP health and a daemon-owned CultCache boundary from the live Yggdrasil service runtime; Idunn accepts yggdrasil-streampixels through the configured service health route and the boundary store at /srv/streampixels/app/.streampixels-data/cultcache/streampixels.service.cc remains the service-owned witness behind deployment/debug lowerings.", - ), - "yggdrasil-heimdall" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-boundary-store", - "partial-rudp-health-and-provider-store-live", - "Heimdall publishes daemon-owned Idunn health over CultNet/RUDP from the live Yggdrasil service runtime, and its daemon-owned boundary store at /srv/heimdall/cultcache/heimdall.service.cc carries provider advertisement, command_boundary, transport_profile, and daemon-health summary state. Product web and host supervisor checks are deployment/debug witnesses, not daemon transport.", - ), - "yggdrasil-repixelizer" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-boundary-store", - "partial-rudp-health-and-provider-store-live", - "Repixelizer publishes daemon-owned Idunn health over CultNet/RUDP from the live Yggdrasil GUI runtime, and its daemon-owned boundary store at /srv/repixelizer/cultcache/repixelizer.service.cc carries provider advertisement, Eve surface state, queue/auth/runtime projection, command_boundary, transport_profile, and daemon-health summary state. Product web and host supervisor checks are deployment/debug witnesses, not daemon transport.", - ), - "raven-sleipnir" => ( - "daemon-published-rudp-health + daemon-owned-cultcache-input-mirror-store", - "partial-rudp-health-and-provider-store-live", - "Sleipnir publishes daemon-owned Idunn health over CultNet/RUDP from the Raven input mirror runtime, writes provider advertisement and Eve surface state to its daemon-owned CultMesh store, and accepts mapping commands through Odin's CultMesh command document route. The Raven scheduled task is an Idunn restart lowering, not deployment truth.", - ), - _ => ( - "missing-daemon-published-rudp-health", - "migration-required", - "Daemon truth must move to CultNet/RUDP health publication and advertised command boundaries; command probes are debug witnesses only.", - ), - }; - IdunnDaemonTransportProfileRecord { - profile_id: transport_profile_id(target), - daemon_id: target.daemon_id.clone(), - target_transport: "cultnet.transport.rudp.v0".to_string(), - current_transport: current_transport.to_string(), - state: state.to_string(), - health_contract: target.health_contract.id.clone(), - publication_schema: "idunn.daemon_health.v1".to_string(), - debug_mechanism: "none".to_string(), - cut_line: cut_line.to_string(), - observed_at: observed_at.to_string(), - } -} - -fn command_boundary(target: &DaemonTarget, observed_at: &str) -> IdunnCommandBoundaryRecord { - let restart_authority = target - .restart_command - .as_ref() - .map(|_| "idunn-supervisor-command.restart") - .unwrap_or("none") - .to_string(); - let deploy_authority = target - .deploy_command - .as_ref() - .map(|_| "idunn-supervisor-command.deploy") - .unwrap_or("none") - .to_string(); - let command_lowerings = [ - target.restart_command.as_ref(), - target.deploy_command.as_ref(), - ] - .into_iter() - .flatten() - .cloned() - .collect(); - - IdunnCommandBoundaryRecord { - boundary_id: command_boundary_id(target), - daemon_id: target.daemon_id.clone(), - owner: "idunn-supervisor-command-boundary".to_string(), - restart_authority, - deploy_authority, - health_authority: "daemon-published-rudp-health".to_string(), - alarm_authority: "bifrost.operator-notification".to_string(), - command_lowerings, - forbidden_authority: "Product/debug probes do not own daemon truth.".to_string(), - observed_at: observed_at.to_string(), - } -} - -fn daemon_surgery_plans( - targets: &[DaemonTarget], - updated_at: &str, -) -> Vec { - targets - .iter() - .map(|target| daemon_surgery_plan(target, updated_at)) - .collect() -} - -fn daemon_surgery_plan(target: &DaemonTarget, updated_at: &str) -> IdunnDaemonSurgeryPlanRecord { - let mut severity = "high"; - let mut status = "transport-surgery-required"; - let mut owner = "daemon-owned CultLib update"; - let mut objective = format!( - "{} publishes daemon-owned health, provider state, command boundary, and transport profile through CultNet/RUDP.", - target.name - ); - let mut current_mechanism = format!( - "Idunn target {} currently requires daemon-published RUDP health under contract {}; local health probes are not part of the target contract.", - target.daemon_id, target.health_contract.id - ); - let mut intended_authority = "Daemon publishes typed CultMesh/CultNet documents over cultnet.transport.rudp.v0; Idunn consumes those records and only actuates advertised lifecycle commands.".to_string(); - let mut cut_line = "Cut product/debug probes as sources of daemon truth once the daemon's CultLib can publish the RUDP health contract.".to_string(); - let mut steps = vec![ - "Update the daemon's runtime CultLib dependency to a build that can speak CultNet over RUDP.".to_string(), - "Publish daemon_health, provider_advertisement, command_boundary, and transport_profile typed records over cultnet.transport.rudp.v0.".to_string(), - "Teach Odin to accept the daemon's RUDP provider records into the service/catalog surface.".to_string(), - "Switch Idunn from the debug health command to the daemon-owned RUDP health record.".to_string(), - "Delete or demote the old probe to a xenos-boundary debug check with no lifecycle authority.".to_string(), - ]; - let blockers = Vec::new(); - - match target.daemon_id.as_str() { - "odin" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "critical"; - owner = "Odin core"; - current_mechanism = - "Odin ingests remote Raven Vili, Raven Muninn, Starfire Muninn, and Nightwing Muninn witness stores while accepting Yggdrasil-local Gjallar's typed aggregate publication directly over CultNet/RUDP." - .to_string(); - intended_authority = - "Odin owns accepted Verse discovery and provider catalog truth as typed CultMesh/CultNet records over cultnet.transport.rudp.v0." - .to_string(); - cut_line = - "Odin's remaining interop debt is deleting hashed-store inspection shims once remote daemon runtimes publish directly interoperable provider surfaces and records." - .to_string(); - steps[2] = - "Make Odin ingest RUDP provider advertisements directly into its accepted service/catalog surface." - .to_string(); - } - "yggdrasil-gjallar" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "critical"; - owner = "Gjallar Yggdrasil composition runtime plus Odin accepted snapshot surface"; - current_mechanism = - "Gjallar runs headless beside Odin and Idunn on Yggdrasil, consumes Odin's accepted provider snapshot, and publishes one typed aggregate Eve surface plus composition health without opening a framebuffer." - .to_string(); - intended_authority = - "Gjallar consumes Odin's accepted provider surface snapshot and publishes the composed aggregate, provider advertisement, runtime config, command boundary, transport profile, and health as typed CultMesh/CultNet state. Eve clients own graphical lowering." - .to_string(); - cut_line = - "Retire the Nightwing service and framebuffer actuator. Keep one Yggdrasil daemon publishing the aggregate tree and freshness health; clients lower that tree without becoming aggregation owners." - .to_string(); - steps = vec![ - "Run Gjallar as yggdrasil-gjallar beside Odin and Idunn with its framebuffer backend disabled.".to_string(), - "Publish the full gjallar.overview Eve surface and daemon-owned boundary records from /var/lib/gamecult/gjallar/gjallar.service.cc.".to_string(), - "Keep the local CultNet/RUDP snapshot input pointed at Odin's accepted surface:gamecult.network.status.".to_string(), - "Key daemon health to snapshot and aggregate publication freshness rather than rendered frames.".to_string(), - "Retire the Nightwing deployment target and stop its gjallar.service before admitting the Yggdrasil body.".to_string(), - ]; - } - "raven-sleipnir" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "medium-high"; - owner = "Sleipnir input mirror runtime"; - current_mechanism = - "Raven Sleipnir publishes sleipnir.cultnet-rudp-input-mirror-health over CultNet/RUDP from the live input mirror runtime, writes provider advertisement and Eve surface state to C:\\Meta\\Odin\\state\\raven.sleipnir.cc, exposes mapping commands through its advertised CultNet/RUDP route, and runs as a hidden Raven scheduled task installed by Idunn's restart lowering." - .to_string(); - intended_authority = - "Sleipnir publishes daemon health, provider advertisement, provider-owned Eve surface, and mapping command ingress as typed CultMesh/CultNet records; the scheduled task is lifecycle actuation only." - .to_string(); - cut_line = - "Keep restart-raven-sleipnir.ps1 as an Idunn actuator body only; direct operator starts and ad hoc deployment scripts must not own Raven Sleipnir lifecycle or command truth." - .to_string(); - steps = vec![ - "Keep live sleipnir.cultnet-rudp-input-mirror-health publication running from Raven sleipnir.".to_string(), - "Keep Sleipnir provider advertisement and Eve surface state in C:\\Meta\\Odin\\state\\raven.sleipnir.cc and published to Odin over the configured CultNet/RUDP catalog endpoint.".to_string(), - "Keep Sleipnir mapping changes flowing through Odin's `sleipnir.input_mapping.v1` CultMesh command document route.".to_string(), - "Keep Raven process launch hidden through the Idunn-installed scheduled task; no visible cmd or PowerShell windows.".to_string(), - "Keep start/deploy shortcuts deleted or actuator-guarded so Idunn remains lifecycle owner.".to_string(), - ]; - } - "stonks" => { - status = "partial-rudp-health-and-provider-store-live"; - owner = "Stonks TypeScript runtime"; - current_mechanism = - "Stonks publishes daemon health over CultNet/RUDP after each serialized market refresh, and its daemon-owned CultCache store contains provider advertisement, market snapshot, Eve surface, command_boundary, and transport_profile records that Odin can ingest." - .to_string(); - intended_authority = - "Stonks publishes daemon health, provider advertisement, market snapshot, Eve surface, command boundary, and transport profile as typed CultMesh/CultNet records over cultnet.transport.rudp.v0." - .to_string(); - cut_line = - "Keep Stonks lifecycle and health on daemon-published CultMesh/CultNet records; product/debug exports are not daemon-health probes." - .to_string(); - steps = vec![ - "Keep live stonks.cultnet-rudp-market-health publication running from the Stonks daemon.".to_string(), - "Keep Stonks provider advertisement, market snapshot, Eve surface, command_boundary, and transport_profile records in the daemon-owned CultCache store.".to_string(), - "Keep Odin provider discovery accepting Stonks' typed store instead of relying on product manifest ingestion.".to_string(), - "Keep health-stonks.cmd archived so local probes cannot masquerade as daemon health.".to_string(), - ]; - } - "mimir-eve-dashboard" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "high"; - owner = "Mimir dashboard runtime"; - current_mechanism = - "Mimir Eve dashboard publishes mimir.cultnet-rudp-provider-health over CultNet/RUDP from the live Nightwing broker, publishes retained dashboard state through CultMesh at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp, and writes a daemon-owned boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc." - .to_string(); - intended_authority = - "Mimir dashboard publishes daemon health over cultnet.transport.rudp.v0, publishes retained Eve dashboard state through CultMesh, and keeps typed provider advertisement, command boundary, and transport profile in its daemon-owned boundary witness." - .to_string(); - cut_line = - "Keep local probe wrappers archived while Odin and Idunn consume the daemon-owned Mimir witness paths directly." - .to_string(); - steps = vec![ - "Keep live mimir.cultnet-rudp-provider-health publication running from the Nightwing Eve dashboard broker.".to_string(), - "Keep the live CultMesh state witness at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp publishing retained dashboard state.".to_string(), - "Keep the daemon-owned boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc publishing typed provider advertisement, command_boundary, transport_profile, and daemon-health summary records.".to_string(), - "Keep Odin pointed at Mimir CultMesh/CultCache witness paths instead of product catalog ingestion.".to_string(), - "Keep health-mimir-eve-dashboard.cmd archived so local probes cannot masquerade as daemon health.".to_string(), - ]; - } - "weksa" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "medium-high"; - owner = "Weksa provider runtime"; - current_mechanism = - "Weksa publishes weksa.cultnet-rudp-provider-health over CultNet/RUDP after each serialized witness refresh, its daemon-owned provider store contains provider advertisement, operator state, Eve surface, command_boundary, and transport_profile records that Odin can ingest, and speech_provider.mimo.voicedesign arrives as typed CultMesh/CultNet command documents through Odin." - .to_string(); - intended_authority = - "Weksa publishes daemon health, provider advertisement, operator state, Eve surfaces, command boundary, transport profile, and MiMo VoiceDesign command ingress as typed CultMesh/CultNet records over cultnet.transport.rudp.v0; product endpoints do not own daemon truth." - .to_string(); - cut_line = - "Keep Weksa lifecycle and health on daemon-published CultMesh/CultNet records; local command health must not satisfy daemon truth." - .to_string(); - steps = vec![ - "Keep live weksa.cultnet-rudp-provider-health publication running from the Weksa daemon.".to_string(), - "Keep Weksa provider advertisement, operator-state, Eve surface, command_boundary, and transport_profile records in the daemon-owned provider store.".to_string(), - "Keep Odin provider discovery accepting Weksa's typed provider store instead of relying on product ingestion.".to_string(), - "Keep speech_provider.mimo.voicedesign on typed CultMesh/CultNet command documents.".to_string(), - "Keep health-weksa.cmd archived so local command health cannot masquerade as daemon truth.".to_string(), - ]; - } - "voidbot" => { - status = "partial-rudp-health-and-provider-store-live"; - severity = "medium-high"; - owner = "VoidBot internal provider stack"; - current_mechanism = - "VoidBot publishes voidbot.cultnet-rudp-stack-health over CultNet/RUDP after each local orchestrator pulse, and the always-run voidbot-swarm-surface organ writes daemon-owned provider catalog, provider advertisement, command_boundary, and transport_profile records into E:\\Projects\\VoidBot\\.voidbot\\status\\cultmesh\\voidbot-swarm-state.cc." - .to_string(); - intended_authority = - "VoidBot publishes internal swarm, repo-face, and provider health over CultNet/RUDP; Discord delivery remains a boundary adapter, never daemon truth." - .to_string(); - cut_line = - "Keep the operations probe as a debug witness for the daemon-owned CultMesh store; it no longer owns VoidBot lifecycle truth." - .to_string(); - steps = vec![ - "Keep live voidbot.cultnet-rudp-stack-health publication running from the GameCult Local Orchestrator pulse.".to_string(), - "Keep VoidBot swarm, Discord, archive, source, and repo-face provider records published from the daemon-owned CultMesh witness store.".to_string(), - "Keep VoidBot command_boundary and transport_profile records published from the provider runtime.".to_string(), - "Keep Odin pointed at the daemon-owned VoidBot CultMesh witness store instead of status ingestion.".to_string(), - "Keep health-voidbot.cmd archived so local witness inspection cannot masquerade as daemon health.".to_string(), - ]; - } - "muninn" => { - status = "partial-rudp-health-and-provider-store-live"; - owner = "Muninn Rust runtime plus Raven background-only launcher surface"; - current_mechanism = - "Raven Muninn now runs from the hidden GameCult-Muninn scheduled task with --idunn-rudp-health supplied by the configured Idunn health endpoint, daemon id muninn, and contract muninn.cultnet-rudp-remote-telemetry-health on the long-running serve process. Live Idunn accepts that daemon-published health over the configured RUDP route, the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\muninn.telemetry.cc now carries provider advertisement, command_boundary, transport_profile, and telemetry surface records, activation commands now route through the explicit C:\\Meta\\Odin\\state\\muninn.activate.cc store, the serve body no longer auto-claims ambient Move runtime authority from platform defaults, and GameCult-Muninn, GameCult-Muninn-Activate, and GameCult-Muninn-VideoProof now execute hidden VBS launchers whose bodies call noninteractive hidden PowerShell entrypoints directly; .cmd wrappers, where present, are manual debug entrypoints only." - .to_string(); - intended_authority = - "Muninn publishes telemetry, provider advertisement, command_boundary, transport_profile, explicit activation routing, and daemon health over CultNet/RUDP/CultCache; Raven Task Scheduler owns only background launch of hidden WScript/PowerShell launchers and never visible .cmd trampoline execution or ambient Move runtime inference." - .to_string(); - cut_line = - "Keep Raven's hidden task launch invariant structural by verifying that scheduled-task actions and hidden VBS launchers never route through cmdPath trampolines, that activation commands stay pointed at C:\\Meta\\Odin\\state\\muninn.activate.cc, and that plain serve cannot infer Move runtime authority from platform defaults; Odin and Idunn should consume C:\\Meta\\Odin\\state\\muninn.telemetry.cc as the daemon-owned witness while health-muninn.cmd remains archived." - .to_string(); - steps = vec![ - "Keep scripts/repair-raven-muninn-task-actions.ps1 using sftp plus a tiny remote runner so Windows command-line length does not block future hidden-task repair.".to_string(), - "Keep GameCult-Muninn action executing wscript.exe with start-muninn-serve-hidden.vbs arguments.".to_string(), - "Keep the Raven Muninn serve process command line carrying --idunn-rudp-health from the configured Idunn health endpoint, --idunn-daemon muninn, and --idunn-health-contract muninn.cultnet-rudp-remote-telemetry-health; no baked Starfire LAN health endpoint is allowed.".to_string(), - "Keep the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\muninn.telemetry.cc publishing provider advertisement, command_boundary, transport_profile, and telemetry surface records.".to_string(), - "Keep the activation path publishing through C:\\Meta\\Odin\\state\\muninn.activate.cc instead of reusing the telemetry store for activate routing.".to_string(), - "Keep plain Muninn serve from auto-claiming PS Move hosts, Move state, or platform-default Move lights unless explicit Move flags request that runtime authority.".to_string(), - "Keep GameCult-Muninn-Activate action executing wscript.exe with activate-raven-av-srt-hidden.vbs arguments.".to_string(), - "Keep GameCult-Muninn-VideoProof action executing wscript.exe with muninn-raven-video-to-starfire-obs-hidden.vbs arguments.".to_string(), - "Keep the Activate and VideoProof hidden VBS launchers calling PowerShell entrypoints directly instead of cmdPath trampolines.".to_string(), - "Keep Raven health/restart actuators background-only; no visible terminals or interactive windows on the shared host.".to_string(), - ]; - } - "starfire-muninn" | "nightwing-muninn" => { - owner = "Muninn Rust runtime"; - intended_authority = - "Muninn publishes telemetry, Quest/Move access, and daemon health over CultNet/RUDP; activation commands remain separate from keepalive." - .to_string(); - if target.daemon_id == "starfire-muninn" { - status = "partial-rudp-health-and-provider-store-live"; - current_mechanism = - "Starfire Muninn now runs as a long-lived local serve process with --quest-adb, --idunn-rudp-health supplied by the configured Idunn health endpoint, daemon id starfire-muninn, and contract muninn.cultnet-rudp-local-telemetry-and-quest-access. Live Idunn accepts that daemon-published health through the configured RUDP route, the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc now carries provider advertisement, command_boundary, transport_profile, Quest access, and telemetry surface records, and restart-starfire-muninn.ps1 now archives a corrupt CultCache store before relaunch instead of leaving the daemon dead on decode faults." - .to_string(); - cut_line = - "Keep health-starfire-muninn.ps1 consuming C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc as the daemon-owned witness while Quest availability remains telemetry state; health-starfire-muninn.cmd stays archived." - .to_string(); - steps = vec![ - "Keep the Starfire Muninn serve process command line carrying --idunn-rudp-health from the configured Idunn health endpoint, --idunn-daemon starfire-muninn, and --idunn-health-contract muninn.cultnet-rudp-local-telemetry-and-quest-access; no baked local endpoint is allowed.".to_string(), - "Keep restart-starfire-muninn.ps1 clearing stale .lock files and archiving corrupt starfire.muninn.telemetry.cc before relaunch.".to_string(), - "Keep the daemon-owned telemetry store at C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc publishing provider advertisement, command_boundary, transport_profile, Quest access, and telemetry surface records.".to_string(), - "Keep health-starfire-muninn.ps1 as the witness-first inspection body for Quest telemetry and process shape; health-starfire-muninn.cmd is archived.".to_string(), - ]; - } else { - status = "partial-rudp-health-and-provider-store-live"; - current_mechanism = - "Nightwing Muninn now runs as a long-lived serve process with explicit Move HID input, --idunn-rudp-health supplied by the configured Idunn health endpoint, daemon id nightwing-muninn, and contract muninn.cultnet-rudp-remote-telemetry-and-move-hid. Live Idunn accepts that daemon-published health from the configured RUDP route, the daemon-owned telemetry store at /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc now carries provider advertisement, command_boundary, transport_profile, Move HID evidence, and telemetry surface records, and restart-nightwing-muninn.ps1 now only claims or discovers Move runtime authority when -DiscoverMoveState, -ClaimUsbMoves, or explicit -MoveState values are present." - .to_string(); - cut_line = - "Keep health-nightwing-muninn.ps1 consuming /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc as the daemon-owned witness while the serve process owns muninn.cultnet-rudp-remote-telemetry-and-move-hid publication; health-nightwing-muninn.cmd stays archived." - .to_string(); - steps = vec![ - "Keep the Nightwing Muninn serve process command line carrying --idunn-rudp-health from the configured Idunn health endpoint, --idunn-daemon nightwing-muninn, and --idunn-health-contract muninn.cultnet-rudp-remote-telemetry-and-move-hid; no baked WireGuard endpoint is allowed.".to_string(), - "Keep the daemon-owned telemetry store at /home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc publishing provider advertisement, command_boundary, transport_profile, Move HID evidence, and telemetry surface records.".to_string(), - "Keep restart-nightwing-muninn.ps1 launching the long-running serve body instead of one-shot health publication.".to_string(), - "Keep restart-nightwing-muninn.ps1 requiring explicit -DiscoverMoveState/-ClaimUsbMoves or explicit -MoveState values before it adds Move evidence/runtime arguments.".to_string(), - "Keep health-nightwing-muninn.ps1 as the witness-first inspection body for Move HID freshness; health-nightwing-muninn.cmd is archived.".to_string(), - ]; - } - } - "vili" => { - status = "partial-rudp-health-and-provider-store-live"; - owner = "Vili animation runtime"; - current_mechanism = - "Vili now runs on Raven through the hidden GameCult\\Vili scheduled task, which launches start-vili-daemon.ps1 with --idunn-rudp-health supplied by the configured Idunn health endpoint and contract vili.cultnet-rudp-animation-health. Live Idunn accepts that daemon-published health from the configured RUDP route, and Vili writes a daemon-owned vili.service.cc CultCache store containing provider advertisement, operator state, Eve surface, command_boundary, and transport_profile records." - .to_string(); - intended_authority = - "Vili publishes animation daemon health, provider advertisement, operator state, command boundary, and transport profile as typed CultMesh/CultNet records over cultnet.transport.rudp.v0." - .to_string(); - cut_line = - "Keep Vili lifecycle and health on daemon-published CultMesh/CultNet records while Odin consumes Vili provider records natively." - .to_string(); - steps = vec![ - "Keep the in-process Vili idunn.daemon_health RUDP publisher wired through scripts/vili-daemon.mjs.".to_string(), - "Keep Vili's provider advertisement, operator state, Eve surface, command_boundary, and transport_profile records in the daemon-owned vili.service.cc store.".to_string(), - "Keep scripts/restart-vili.ps1 syncing the authoritative Vili runtime plus flattened CultLib node_modules into Raven before reinstalling the hidden task.".to_string(), - "Keep GameCult\\Vili executing wscript.exe hidden launcher arguments that pass --idunn-rudp-health from the configured Idunn health endpoint, --idunn-daemon vili, and --idunn-health-contract vili.cultnet-rudp-animation-health; no baked WireGuard endpoint is allowed.".to_string(), - "Keep Idunn bound on 0.0.0.0:17870 so WireGuard peers such as Raven can publish daemon-owned health over cultnet.transport.rudp.v0.".to_string(), - "Keep Odin provider discovery pointed at Vili's daemon-owned vili.service.cc store; keep health-vili.cmd archived so local probes cannot masquerade as daemon health.".to_string(), - ]; - } - "nightwing-eve-dashboard" | "nightwing-eve-browser-reference" => { - severity = "medium"; - owner = "Eve lowering/runtime owner"; - intended_authority = - "Eve runtimes subscribe to provider-owned CultMesh/CultNet state; browser delivery must not be advertised as daemon transport or health authority." - .to_string(); - if target.daemon_id == "nightwing-eve-dashboard" { - status = "partial-rudp-health-and-provider-store-live"; - current_mechanism = - "Nightwing Eve dashboard publishes nightwing.cultnet-rudp-eve-dashboard-health over CultNet/RUDP from the Mimir.EveDashboard systemd process, with retained dashboard state in /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp and a daemon-owned boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc." - .to_string(); - cut_line = - "health-nightwing-eve-dashboard.ps1 inspects the dashboard CultMesh/CultCache witnesses directly, restart-nightwing-eve-dashboard.ps1 owns the systemd restart actuator, and the remaining debt is teaching Nightwing projections and Odin to consume the typed witnesses without any service probe reclaiming daemon truth." - .to_string(); - steps = vec![ - "Keep live nightwing.cultnet-rudp-eve-dashboard-health publication running from the Mimir.EveDashboard systemd process.".to_string(), - "Keep the live CultMesh state witness at /var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp publishing retained dashboard state.".to_string(), - "Keep the daemon-owned boundary store at /var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc publishing typed provider advertisement, command_boundary, transport_profile, and daemon-health summary records.".to_string(), - "Teach Odin and Nightwing projections to prefer the dashboard CultMesh/CultCache witnesses over service probes.".to_string(), - "Keep health-nightwing-eve-dashboard.ps1 as the witness-first health body and restart-nightwing-eve-dashboard.ps1 as the systemd restart body.".to_string(), - "Keep health-nightwing-eve-dashboard.cmd archived and restart-nightwing-eve-dashboard.cmd as a manual lifecycle wrapper over the PowerShell restart body only.".to_string(), - ]; - } else { - status = "partial-rudp-health-and-provider-store-live"; - current_mechanism = - "Nightwing Eve browser reference publishes nightwing.cultnet-rudp-browser-reference-health over CultNet/RUDP from the Mimir.EveBrowserReference service process, and the runtime now writes a daemon-owned boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc." - .to_string(); - cut_line = - "health-nightwing-eve-browser-reference.ps1 inspects the browser reference CultCache witness directly, restart-nightwing-eve-browser-reference.ps1 owns the systemd restart actuator, health .cmd wrappers are archived, and the remaining debt is teaching Nightwing projections and Odin to consume the typed witness without any service probe reclaiming daemon truth." - .to_string(); - steps = vec![ - "Keep live nightwing.cultnet-rudp-browser-reference-health publication running from the Mimir.EveBrowserReference service process.".to_string(), - "Keep the daemon-owned boundary store at /var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc publishing manifest, static-surface, command_boundary, transport_profile, and daemon-health summary records.".to_string(), - "Teach Odin and Nightwing projections to prefer the browser reference CultCache witness over service probes.".to_string(), - "Keep health-nightwing-eve-browser-reference.ps1 as the witness-first health body and restart-nightwing-eve-browser-reference.ps1 as the systemd restart body.".to_string(), - "Keep health-nightwing-eve-browser-reference.cmd archived and restart-nightwing-eve-browser-reference.cmd as a manual lifecycle wrapper over the PowerShell restart body only.".to_string(), - ]; - } - } - "yggdrasil-streampixels" => { - severity = "medium"; - status = "partial-rudp-health-and-provider-store-live"; - owner = "StreamPixels service runtime plus gamecult-ops deploy lane"; - current_mechanism = - "StreamPixels publishes a daemon-owned CultCache boundary store with provider advertisement, command_boundary, transport_profile, and Idunn health summary from the live Yggdrasil service runtime. The deployed service requires STREAMPIXELS_IDUNN_RUDP_HEALTH from explicit service env or deployment env and contract streampixels.cultnet-rudp-service-health in /srv/streampixels/env/service.env, the source-artifact lane now ships the required CultLib snapshot beside the app artifact, and live Idunn accepts yggdrasil-streampixels from the configured RUDP route." - .to_string(); - intended_authority = - "StreamPixels publishes service health, provider state, command boundary, and transport profile over cultnet.transport.rudp.v0, with the service-owned CultCache boundary as durable local state and deployment surfaces as debug witnesses." - .to_string(); - cut_line = - "Keep the StreamPixels service boundary store and in-process RUDP publisher live on Yggdrasil; host checks and deployment-manifest freshness remain deployment/debug witnesses only while Odin consumes the typed store and Idunn reads daemon-published health." - .to_string(); - steps = vec![ - "Keep apps/service/src/verse-state.ts publishing streampixels.service.cc from the StreamPixels service runtime.".to_string(), - "Teach Odin to ingest StreamPixels provider advertisement, command_boundary, and transport_profile records from the service-owned CultCache boundary store.".to_string(), - "Keep the StreamPixels in-process Idunn RUDP health publisher using contract streampixels.cultnet-rudp-service-health.".to_string(), - "Keep the Yggdrasil source-artifact lane shipping the StreamPixels app artifact plus the CultLib cultnet-ts/cultcache-ts snapshot through a tiny remote runner script instead of brittle inline SSH quoting.".to_string(), - "Keep the Yggdrasil deploy lane using a serial pnpm workspace build and the deployment-manifest freshness check so failed builds cannot masquerade as fresh restarts.".to_string(), - "Keep live Idunn acceptance proof for StreamPixels RUDP health on Yggdrasil; the publisher sends the health document after a short accept grace period so one-shot pulses do not depend on receiving the accept reply.".to_string(), - "Keep health-yggdrasil-streampixels.cmd archived and keep host checks as explicit deployment witnesses while Odin and Idunn consume the typed store and daemon-published RUDP health.".to_string(), - ]; - } - "yggdrasil-heimdall" => { - severity = "medium"; - status = "partial-rudp-health-and-provider-store-live"; - owner = "Heimdall service runtime plus Yggdrasil source-artifact lane"; - current_mechanism = - "Heimdall now publishes heimdall.cultnet-rudp-provider-health over CultNet/RUDP from the live Yggdrasil service runtime, and its daemon-owned boundary store at /srv/heimdall/cultcache/heimdall.service.cc contains provider advertisement, command_boundary, transport_profile, and daemon-health summary state. The Yggdrasil source-artifact lane now ships the required CultLib snapshot beside the app artifact, while deployment checks verify host/product readiness and the boundary witness." - .to_string(); - intended_authority = - "Heimdall keeps public web delivery as the product boundary while publishing daemon health, provider advertisement, command boundary, and transport profile as internal CultNet/RUDP state, with full redacted auth-document witness export following as a separate auth-safe pass." - .to_string(); - cut_line = - "Keep the Heimdall boundary store and in-process RUDP publisher live on Yggdrasil; host/product readiness checks are deployment/debug witnesses only while Odin ingests the typed store and Idunn reads daemon-published health." - .to_string(); - steps = vec![ - "Keep Heimdall's src/verse-witness.ts advertisement published through the runtime-owned boundary store with daemon-owned update timestamps.".to_string(), - "Keep Heimdall daemon health published over CultNet/RUDP from the live Yggdrasil service runtime with contract heimdall.cultnet-rudp-provider-health.".to_string(), - "Keep Heimdall command_boundary and transport_profile records in the runtime-owned boundary store so deploy/restart authority is inspectable without debug scripts owning truth.".to_string(), - "Keep the Yggdrasil Heimdall source-artifact lane shipping the app artifact plus the CultLib cultnet-ts/cultcache-ts snapshot through a tiny remote runner script instead of brittle inline SSH quoting.".to_string(), - "Teach Odin to ingest Heimdall's provider advertisement and future redacted witness surfaces as typed state instead of treating product web discovery as daemon truth.".to_string(), - "Keep Heimdall deployment checks as witness-only while Odin and Idunn consume the typed store.".to_string(), - ]; - } - "yggdrasil-repixelizer" => { - severity = "medium"; - status = "partial-rudp-health-and-provider-store-live"; - owner = "Repixelizer GUI runtime plus Yggdrasil source-artifact lane"; - current_mechanism = - "Repixelizer publishes repixelizer.cultnet-rudp-service-health from the live Yggdrasil GUI runtime, and its daemon-owned boundary store at /srv/repixelizer/cultcache/repixelizer.service.cc carries provider advertisement, Eve surface state, queue/auth/runtime projection, command_boundary, transport_profile, and daemon-health summary state while check-repixelizer-gui.sh and systemd remain deployment/debug lowerings." - .to_string(); - intended_authority = - "Repixelizer keeps product GUI delivery as the product boundary while publishing internal daemon health, queue/provider state, command boundary, and transport profile over CultNet/RUDP for Idunn and Odin." - .to_string(); - cut_line = - "Keep Repixelizer's runtime-owned RUDP health and boundary store live; product web and host supervisor checks remain deployment/debug witnesses that must not own daemon truth." - .to_string(); - steps = vec![ - "Keep Repixelizer daemon health published over CultNet/RUDP from the live GUI/service runtime on Yggdrasil with contract repixelizer.cultnet-rudp-service-health.".to_string(), - "Keep Repixelizer command_boundary and transport_profile records in the runtime-owned boundary store so deploy authority is inspectable typed state instead of an ops-only assumption.".to_string(), - "Keep the Yggdrasil Repixelizer source-artifact lane shipping the app artifact plus the CultLib cultcache-py snapshot through a tiny remote runner script instead of brittle inline SSH quoting.".to_string(), - "Teach Odin to ingest Repixelizer queue/auth/runtime state from the daemon-owned witness store instead of treating product web checks as daemon truth.".to_string(), - "Keep Heimdall-backed browser auth as a product boundary adapter, not the keepalive truth surface.".to_string(), - "Keep Repixelizer deployment checks as witness-only while Odin and Idunn consume the typed store.".to_string(), - ]; - } - "idunn-swarm-deployment-coverage" => { - severity = "medium"; - status = "catalog-coherence-probe"; - owner = "Idunn"; - objective = - "Keep Idunn's deployment target catalog honest while daemon-owned RUDP publication is being installed." - .to_string(); - current_mechanism = - "A local coverage command verifies deployment authority categories for the swarm target catalog." - .to_string(); - intended_authority = - "Daemon provider advertisements and command_boundary records make deploy/restart ownership inspectable without a local coverage shim." - .to_string(); - cut_line = - "Delete this probe after every deployable daemon publishes command_boundary and transport_profile over CultNet/RUDP." - .to_string(); - } - _ => {} - } - - IdunnDaemonSurgeryPlanRecord { - plan_id: format!("surgery:{}", target.daemon_id), - daemon_id: target.daemon_id.clone(), - severity: severity.to_string(), - status: status.to_string(), - owner: owner.to_string(), - objective, - current_mechanism, - intended_authority, - cut_line, - steps, - blockers, - updated_at: updated_at.to_string(), - } -} - -fn swarm_targets(options: &SwarmOptions) -> Result> { - let repo_root = options.repo_root.display().to_string(); - let script = |name: &str| format!(r"{}\scripts\{}", repo_root, name); - let yggdrasil_actuator = |action: &str, target: &str| { - if action == "deploy" { - format!( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy {target} \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" - ) - } else { - format!("sudo -n /usr/local/libexec/idunn-yggdrasil {action} {target}") - } - }; - let project = |name: &str| PathBuf::from(format!(r"E:\Projects\{name}")); - - match options.profile.as_str() { - "yggdrasil-local" => Ok(vec![ - DaemonTarget { - daemon_id: "yggdrasil-odin".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Odin all-seer".to_string(), - health_contract: locally_supervised_health_contract( - "odin.cultnet-rudp-provider-health", - "failed", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "odin")), - restart_command: Some(yggdrasil_actuator("restart", "odin")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target_on_branch( - "Odin", - PathBuf::from("/srv/build/Odin"), - "codex/ygg-idunn-independent-bootstrap", - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/odin/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "yggdrasil-codex-connector".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Codex Connector".to_string(), - health_contract: locally_supervised_health_contract( - "codex-connector.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "codex-connector")), - restart_command: Some(yggdrasil_actuator("restart", "codex-connector")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target_on_branch( - "CodexConnector", - PathBuf::from("/srv/build/CodexConnector"), - "codex/ghostlight-release-binding", - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/codex-connector/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "yggdrasil-ghostlight".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Ghostlight Dungeon".to_string(), - health_contract: locally_supervised_health_contract( - "ghostlight.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "ghostlight")), - restart_command: Some(yggdrasil_actuator("restart", "ghostlight")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target_on_branch( - "Ghostlight", - PathBuf::from("/srv/build/Ghostlight"), - "codex/ghostlight-dungeon-mvp", - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/ghostlight/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - // The admitted native body now publishes authenticated health; - // Idunn owns its continuity and later release deployment. - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "yggdrasil-voidbot".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil VoidBot".to_string(), - health_contract: health_contract("voidbot.cultnet-rudp-stack-health", "failed"), - deploy_command: Some(yggdrasil_actuator("deploy", "voidbot")), - restart_command: Some(yggdrasil_actuator("restart", "voidbot")), - release: Some(with_deployed_revision_witness( - release_target( - "VoidBot", - PathBuf::from("/srv/build/VoidBot"), - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/voidbot/deploy/deployment.env"), - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-heimdall".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Heimdall".to_string(), - health_contract: health_contract("heimdall.cultnet-rudp-provider-health", "failed"), - deploy_command: Some(yggdrasil_actuator("deploy", "heimdall")), - restart_command: Some(yggdrasil_actuator("restart", "heimdall")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target( - "Heimdall", - PathBuf::from("/srv/build/Heimdall"), - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/heimdall/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-epiphany-capstone-17".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Epiphany Capstone 17 candidate".to_string(), - health_contract: health_contract("epiphany.capstone-17-admission", "failed"), - deploy_command: Some(yggdrasil_actuator("deploy", "epiphany-capstone-17")), - restart_command: None, - release: Some(with_container_artifact_witness( - with_deployed_revision_witness( - release_target_on_branch( - "Epiphany", - PathBuf::from("/srv/build/Epiphany"), - "codex/epiphany-shakedown-live", - "fresh-container-capstone", - None, - "one-shot-no-restart", - ), - PathBuf::from("/srv/epiphany/capstone-17/deployment.env"), - ), - PathBuf::from("/srv/epiphany/artifacts"), - )), - // Capstone 17 is an explicitly requested one-shot admission target. It - // never participates in daemon continuity or automatic rollout. - enabled: false, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-epiphany".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Epiphany".to_string(), - health_contract: health_contract("epiphany.cultnet-rudp-runtime-health", "failed"), - deploy_command: Some(yggdrasil_actuator("deploy", "epiphany")), - restart_command: Some(yggdrasil_actuator("restart", "epiphany")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target_on_branch( - "Epiphany", - PathBuf::from("/srv/build/Epiphany"), - "codex/epiphany-shakedown-live", - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/epiphany/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-bifrost-persona-feedback".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Bifrost Persona feedback".to_string(), - health_contract: health_contract( - "bifrost.cultnet-rudp-persona-feedback-health", - "stale-deployment", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "bifrost-persona-feedback")), - restart_command: None, - release: Some(requiring_bifrost_authority(with_deployed_revision_witness( - release_target( - "Bifrost", - PathBuf::from("/srv/build/Bifrost-persona-feedback"), - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/bifrost/persona-feedback/runtime/deployment.env"), - ))), - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "yggdrasil-repixelizer".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Repixelizer".to_string(), - health_contract: health_contract( - "repixelizer.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "repixelizer")), - restart_command: Some(yggdrasil_actuator("restart", "repixelizer")), - release: Some(release_target( - "repixelizer", - PathBuf::from("/srv/build/repixelizer"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-streampixels".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil StreamPixels".to_string(), - health_contract: health_contract( - "streampixels.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "streampixels")), - restart_command: Some(yggdrasil_actuator("restart", "streampixels")), - release: Some(release_target( - "StreamPixels", - PathBuf::from("/srv/build/StreamPixels"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-gjallar".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Gjallar Eve surface compositor".to_string(), - health_contract: health_contract( - "gjallar.cultnet-rudp-composition-health", - "dependency-unavailable", - ), - deploy_command: Some(yggdrasil_actuator("deploy", "gjallar")), - restart_command: Some(yggdrasil_actuator("restart", "gjallar")), - release: Some(with_source_change_pathspecs( - with_deployed_revision_witness( - release_target_on_branch( - "Gjallar", - PathBuf::from("/srv/build/Gjallar"), - "codex/yggdrasil-aggregate-daemon", - "restart-after-verified-build", - None, - "restart-required", - ), - PathBuf::from("/srv/gjallar/deploy/deployment.env"), - ), - DEPLOYABLE_SOURCE_PATHS, - )), - enabled: true, - interval_seconds: 30, - }, - ]), - "starfire-local" => Ok(vec![ - DaemonTarget { - daemon_id: "odin".to_string(), - verse_id: "starfire.local".to_string(), - name: "Odin all-seer".to_string(), - health_contract: health_contract("odin.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: Some(script("restart-odin.cmd")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "hermodr".to_string(), - verse_id: "starfire.local".to_string(), - name: "Hermodr browser lowering".to_string(), - health_contract: locally_supervised_health_contract( - "hermodr.cultnet-rudp-browser-lowering-health", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-hermodr.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "mimir-eve-dashboard".to_string(), - verse_id: "starfire.local".to_string(), - name: "Mimir Eve dashboard".to_string(), - health_contract: health_contract("mimir.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: None, - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "stonks".to_string(), - verse_id: "starfire.local".to_string(), - name: "Stonks market pulse".to_string(), - health_contract: health_contract("stonks.cultnet-rudp-market-health", "failed"), - deploy_command: None, - restart_command: Some(script("restart-stonks.cmd")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "weksa".to_string(), - verse_id: "starfire.local".to_string(), - name: "Weksa intent and utterance lowering service".to_string(), - health_contract: health_contract("weksa.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: Some(script("restart-weksa.cmd")), - release: None, - enabled: true, - interval_seconds: 60, - }, - DaemonTarget { - daemon_id: "starfire-muninn".to_string(), - verse_id: "starfire.local".to_string(), - name: "Starfire Muninn telemetry and Quest access daemon".to_string(), - health_contract: locally_supervised_health_contract( - "muninn.cultnet-rudp-local-telemetry-and-quest-access", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-starfire-muninn.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "muninn".to_string(), - verse_id: "raven.local".to_string(), - name: "Muninn telemetry Verse assembler".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-remote-telemetry-health", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-muninn.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "vili".to_string(), - verse_id: "raven.local".to_string(), - name: "Vili Persona animation daemon".to_string(), - health_contract: health_contract("vili.cultnet-rudp-animation-health", "failed"), - deploy_command: None, - restart_command: Some(script("restart-vili.cmd")), - release: None, - enabled: true, - interval_seconds: 60, - }, - DaemonTarget { - daemon_id: "raven-sleipnir".to_string(), - verse_id: "raven.local".to_string(), - name: "Raven Sleipnir input mirror".to_string(), - health_contract: health_contract( - "sleipnir.cultnet-rudp-input-mirror-health", - "failed", - ), - deploy_command: Some(script("deploy-raven-sleipnir.ps1")), - restart_command: Some(script("restart-raven-sleipnir.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "idunn-swarm-deployment-coverage".to_string(), - verse_id: "starfire.local".to_string(), - name: "Idunn swarm deployment coverage".to_string(), - health_contract: health_contract("idunn.deployment-catalog-coherence", "degraded"), - deploy_command: None, - restart_command: None, - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "yggdrasil-heimdall".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Heimdall".to_string(), - health_contract: health_contract("heimdall.cultnet-rudp-provider-health", "failed"), - deploy_command: Some(script("deploy-yggdrasil-heimdall.cmd")), - restart_command: None, - release: Some(release_target( - "Heimdall", - project("Heimdall"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-repixelizer".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Repixelizer".to_string(), - health_contract: health_contract( - "repixelizer.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(script("deploy-yggdrasil-repixelizer.cmd")), - restart_command: None, - release: Some(release_target( - "repixelizer", - project("repixelizer"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "yggdrasil-streampixels".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil StreamPixels".to_string(), - health_contract: health_contract( - "streampixels.cultnet-rudp-service-health", - "failed", - ), - deploy_command: Some(script("deploy-yggdrasil-streampixels.cmd")), - restart_command: None, - release: Some(release_target( - "StreamPixels", - project("StreamPixels"), - "restart-after-verified-build", - None, - "restart-required", - )), - enabled: true, - interval_seconds: 300, - }, - DaemonTarget { - daemon_id: "nightwing-muninn".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Muninn telemetry and Move HID daemon".to_string(), - health_contract: locally_supervised_health_contract( - "muninn.cultnet-rudp-remote-telemetry-and-move-hid", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-nightwing-muninn.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "nightwing-eve-dashboard".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Eve dashboard broker".to_string(), - health_contract: health_contract( - "nightwing.cultnet-rudp-eve-dashboard-health", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-nightwing-eve-dashboard.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - DaemonTarget { - daemon_id: "nightwing-eve-browser-reference".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Eve browser reference".to_string(), - health_contract: health_contract( - "nightwing.cultnet-rudp-browser-reference-health", - "failed", - ), - deploy_command: None, - restart_command: Some(script("restart-nightwing-eve-browser-reference.ps1")), - release: None, - enabled: true, - interval_seconds: 30, - }, - ]), - other => Err(anyhow!("unknown Idunn swarm profile: {other}")), - } -} - -fn restart_failure_alarm( - result: &IdunnRestartResultRecord, - raised_at: &str, -) -> IdunnOperatorAlarmRecord { - IdunnOperatorAlarmRecord { - alarm_id: format!("alarm:restart-failed:{}:{}", result.daemon_id, raised_at), - daemon_id: result.daemon_id.clone(), - severity: "operator-action-required".to_string(), - reason: format!( - "restart command failed for request {}: {}", - result.request_id, result.detail - ), - escalation_target: "bifrost.operator-notification".to_string(), - raised_at: raised_at.to_string(), - } -} - -fn deployment_failure_alarm( - result: &IdunnDeploymentResultRecord, - raised_at: &str, -) -> IdunnOperatorAlarmRecord { - IdunnOperatorAlarmRecord { - alarm_id: format!("alarm:deployment-failed:{}:{}", result.daemon_id, raised_at), - daemon_id: result.daemon_id.clone(), - severity: "operator-action-required".to_string(), - reason: format!( - "deployment command failed for request {}: {}", - result.request_id, result.detail - ), - escalation_target: "bifrost.operator-notification".to_string(), - raised_at: raised_at.to_string(), - } -} - -impl Options { - fn parse(args: impl Iterator) -> Result { - let args: Vec = args.collect(); - let release_authority_validation = args - .first() - .is_some_and(|arg| arg == "validate-release-authority"); - let minimum_source_revision_validation = args - .first() - .is_some_and(|arg| arg == "validate-minimum-source-revision"); - if minimum_source_revision_validation { - validate_minimum_source_revision_cli_shape(&args)?; - } - let health_admission_validation = args - .first() - .is_some_and(|arg| arg == "validate-health-admission"); - let runtime_admission_validation = args - .first() - .is_some_and(|arg| arg == "validate-runtime-admission"); - let lifecycle_action = args.first().and_then(|arg| match arg.as_str() { - "restart" | "request-restart" => Some(LifecycleAction::Restart), - "redeploy" | "request-redeploy" | "deploy" | "request-deploy" => { - Some(LifecycleAction::Redeploy) - } - _ => None, - }); - let mut store_path = PathBuf::from("scratch/idunn/idunn.keepalive.cc"); - let mut release_authority_store_path = None; - let mut deployment_brake_store_path = None; - let mut deployment_brake_operator_anchor_path = None; - let mut deployment_brake_runtime_id = None; - let mut operator_alarm_command = None; - let mut rudp_health_bind = None; - let mut trusted_epiphany_health_identity_store = None; - let mut daemon_health_trust_store_path = None; - let mut service_identity_store_path = None; - let mut public_health_store_path = None; - let mut public_health_query_bind = None; - let mut execute = false; - let mut command_timeout_seconds = 30; - let mut daemon_id = None; - let mut verse_id = "local".to_string(); - let mut name = None; - let mut deploy_command = None; - let mut restart_command = None; - let mut enabled = true; - let mut interval_seconds = None; - let mut swarm_profile = None; - let mut repo_root = env::current_dir().context("determining current directory")?; - let mut requested_by = env::var("USERNAME") - .or_else(|_| env::var("USER")) - .unwrap_or_else(|_| "operator".to_string()); - let mut command_detail = String::new(); - let mut validation_repository_full_name = None; - let mut validation_upstream_ref = None; - let mut validation_source_revision = None; - let mut validation_authority_id = None; - let mut validation_envelope_sha256 = None; - let mut validation_release_id = None; - let mut validation_release_witness_sha256 = None; - let mut validation_activation_witness_sha256 = None; - let mut validation_deployment_request_id = None; - let mut validation_input = None; - let mut write_admission_witness = None; - let mut validation_runtime_process_id = None; - let mut validation_runtime_process_starttime_ticks = None; - - let mut args = args.into_iter().peekable(); - if lifecycle_action.is_some() - || release_authority_validation - || minimum_source_revision_validation - || health_admission_validation - || runtime_admission_validation - { - let _ = args.next(); - } - while let Some(arg) = args.next() { - match arg.as_str() { - "--store" => store_path = PathBuf::from(take_value(&mut args, "--store")?), - "--release-authority-store" => { - release_authority_store_path = Some(PathBuf::from(take_value( - &mut args, - "--release-authority-store", - )?)) - } - "--deployment-brake-store" => { - deployment_brake_store_path = Some(PathBuf::from(take_value( - &mut args, - "--deployment-brake-store", - )?)) - } - "--deployment-brake-operator-anchor" => { - deployment_brake_operator_anchor_path = Some(PathBuf::from(take_value( - &mut args, - "--deployment-brake-operator-anchor", - )?)) - } - "--deployment-brake-runtime-id" => { - deployment_brake_runtime_id = - Some(take_value(&mut args, "--deployment-brake-runtime-id")?) - } - "--repository-full-name" => { - validation_repository_full_name = - Some(take_value(&mut args, "--repository-full-name")?) - } - "--upstream-ref" => { - validation_upstream_ref = Some(take_value(&mut args, "--upstream-ref")?) - } - "--source-revision" => { - validation_source_revision = Some(take_value(&mut args, "--source-revision")?) - } - "--release-authority-id" => { - validation_authority_id = Some(take_value(&mut args, "--release-authority-id")?) - } - "--release-authority-sha256" => { - validation_envelope_sha256 = - Some(take_value(&mut args, "--release-authority-sha256")?) - } - "--release-id" => { - validation_release_id = Some(take_value(&mut args, "--release-id")?) - } - "--release-witness-sha256" => { - validation_release_witness_sha256 = - Some(take_value(&mut args, "--release-witness-sha256")?) - } - "--activation-witness-sha256" => { - validation_activation_witness_sha256 = - Some(take_value(&mut args, "--activation-witness-sha256")?) - } - "--deployment-request-id" => { - validation_deployment_request_id = - Some(take_value(&mut args, "--deployment-request-id")?) - } - "--input" => { - validation_input = Some(PathBuf::from(take_value(&mut args, "--input")?)) - } - "--write-admission-witness" => { - write_admission_witness = Some(PathBuf::from(take_value( - &mut args, - "--write-admission-witness", - )?)) - } - "--runtime-process-id" => { - validation_runtime_process_id = Some( - take_value(&mut args, "--runtime-process-id")? - .parse::() - .context("--runtime-process-id must be a nonzero u32")?, - ) - } - "--runtime-process-starttime-ticks" => { - validation_runtime_process_starttime_ticks = Some( - take_value(&mut args, "--runtime-process-starttime-ticks")? - .parse::() - .context("--runtime-process-starttime-ticks must be a nonzero u64")?, - ) - } - "--daemon" => daemon_id = Some(take_value(&mut args, "--daemon")?), - "--verse" => verse_id = take_value(&mut args, "--verse")?, - "--name" => name = Some(take_value(&mut args, "--name")?), - "--health-command" => { - let _ = take_value(&mut args, "--health-command")?; - return Err(anyhow!( - "--health-command has been removed; daemon health must arrive as typed {} records over CultNet/RUDP", - CULTNET_RUDP_PROTOCOL_ID - )); - } - "--deploy-command" => { - deploy_command = Some(take_value(&mut args, "--deploy-command")?) - } - "--restart-command" => { - restart_command = Some(take_value(&mut args, "--restart-command")?) - } - "--operator-alarm-command" => { - operator_alarm_command = - Some(take_value(&mut args, "--operator-alarm-command")?) - } - "--rudp-health-bind" => { - let value = take_value(&mut args, "--rudp-health-bind")?; - rudp_health_bind = if value.eq_ignore_ascii_case("none") { - None - } else { - Some( - value - .parse() - .with_context(|| "--rudp-health-bind must be a socket address")?, - ) - }; - } - "--trusted-epiphany-health-identity-store" => { - trusted_epiphany_health_identity_store = Some(PathBuf::from(take_value( - &mut args, - "--trusted-epiphany-health-identity-store", - )?)); - } - "--daemon-health-trust-store" => { - daemon_health_trust_store_path = Some(PathBuf::from(take_value( - &mut args, - "--daemon-health-trust-store", - )?)); - } - "--service-identity-store" => { - service_identity_store_path = Some(PathBuf::from(take_value( - &mut args, - "--service-identity-store", - )?)); - } - "--public-health-store" => { - public_health_store_path = Some(PathBuf::from(take_value( - &mut args, - "--public-health-store", - )?)); - } - "--public-health-query-bind" => { - public_health_query_bind = Some( - take_value(&mut args, "--public-health-query-bind")? - .parse() - .with_context( - || "--public-health-query-bind must be a socket address", - )?, - ); - } - "--disabled" => enabled = false, - "--execute" => execute = true, - "--interval-seconds" => { - interval_seconds = Some( - take_value(&mut args, "--interval-seconds")? - .parse() - .context("--interval-seconds must be a positive integer")?, - ) - } - "--command-timeout-seconds" => { - command_timeout_seconds = take_value(&mut args, "--command-timeout-seconds")? - .parse() - .context("--command-timeout-seconds must be a positive integer")? - } - "--swarm-profile" => { - swarm_profile = Some(take_value(&mut args, "--swarm-profile")?) - } - "--repo-root" => repo_root = PathBuf::from(take_value(&mut args, "--repo-root")?), - "--requested-by" => requested_by = take_value(&mut args, "--requested-by")?, - "--detail" => command_detail = take_value(&mut args, "--detail")?, - "--help" | "-h" => return Err(anyhow!(help_text())), - other => { - return Err(anyhow!( - "unknown Idunn argument: {other}\n\n{}", - help_text() - )); - } - } - } - - if command_timeout_seconds == 0 { - return Err(anyhow!( - "--command-timeout-seconds must be greater than zero" - )); - } - - let common = CommonOptions { - store_path, - release_authority_store_path, - deployment_brake_store_path, - deployment_brake_operator_anchor_path, - deployment_brake_runtime_id: deployment_brake_runtime_id.or_else(|| { - (swarm_profile.as_deref() == Some("yggdrasil-local")) - .then(|| "yggdrasil".to_string()) - }), - operator_alarm_command, - rudp_health_bind, - trusted_epiphany_health_identity_store, - daemon_health_trust_store_path, - service_identity_store_path, - public_health_store_path, - public_health_query_bind, - execute, - command_timeout_seconds, - }; - - if validation_input.is_some() && !runtime_admission_validation { - return Err(anyhow!("--input belongs to validate-runtime-admission")); - } - if write_admission_witness.is_some() && !health_admission_validation { - return Err(anyhow!( - "--write-admission-witness belongs to validate-health-admission" - )); - } - let runtime_process = match ( - validation_runtime_process_id, - validation_runtime_process_starttime_ticks, - ) { - (Some(process_id), Some(starttime_ticks)) => Some( - RuntimeProcessObservation { - process_id, - starttime_ticks, - } - .validate()?, - ), - (None, None) => None, - _ => { - return Err(anyhow!( - "--runtime-process-id and --runtime-process-starttime-ticks must be supplied together" - )); - } - }; - let runtime_process_lane = runtime_admission_validation - || (health_admission_validation && write_admission_witness.is_some()); - if runtime_process.is_some() && !runtime_process_lane { - return Err(anyhow!( - "runtime process identity belongs only to runtime traffic issuance or validation" - )); - } - - if release_authority_validation { - let store_path = common.release_authority_store_path.clone().ok_or_else(|| { - anyhow!("release authority validation requires --release-authority-store") - })?; - return Ok(Self { - common, - mode: Mode::ReleaseAuthorityValidation(ReleaseAuthorityValidationOptions { - store_path, - repository_full_name: validation_repository_full_name.ok_or_else(|| { - anyhow!("release authority validation requires --repository-full-name") - })?, - upstream_ref: validation_upstream_ref.ok_or_else(|| { - anyhow!("release authority validation requires --upstream-ref") - })?, - source_revision: validation_source_revision.ok_or_else(|| { - anyhow!("release authority validation requires --source-revision") - })?, - authority_id: validation_authority_id.ok_or_else(|| { - anyhow!("release authority validation requires --release-authority-id") - })?, - envelope_sha256: validation_envelope_sha256.ok_or_else(|| { - anyhow!("release authority validation requires --release-authority-sha256") - })?, - }), - }); - } - - if minimum_source_revision_validation { - return Ok(Self { - common, - mode: Mode::MinimumSourceRevisionValidation( - MinimumSourceRevisionValidationOptions { - daemon_id: daemon_id.ok_or_else(|| { - anyhow!("minimum source validation requires --daemon") - })?, - source_revision: validation_source_revision.ok_or_else(|| { - anyhow!("minimum source validation requires --source-revision") - })?, - }, - ), - }); - } - - if health_admission_validation { - if let Some(output) = write_admission_witness.as_deref() { - let output_daemon = daemon_id - .as_deref() - .ok_or_else(|| anyhow!("health admission validation requires --daemon"))?; - require_supported_runtime_admission_pair(output_daemon, output)?; - if validation_activation_witness_sha256.is_none() { - return Err(anyhow!( - "--write-admission-witness requires --activation-witness-sha256" - )); - } - if runtime_process.is_none() { - return Err(anyhow!( - "--write-admission-witness requires --runtime-process-id and --runtime-process-starttime-ticks" - )); - } - } - return Ok(Self { - common, - mode: Mode::HealthAdmissionValidation(HealthAdmissionValidationOptions { - daemon_id: daemon_id - .ok_or_else(|| anyhow!("health admission validation requires --daemon"))?, - deployment_request_id: validation_deployment_request_id.ok_or_else(|| { - anyhow!("health admission validation requires --deployment-request-id") - })?, - release_id: validation_release_id.ok_or_else(|| { - anyhow!("health admission validation requires --release-id") - })?, - release_witness_sha256: validation_release_witness_sha256.ok_or_else(|| { - anyhow!("health admission validation requires --release-witness-sha256") - })?, - source_commit: validation_source_revision.ok_or_else(|| { - anyhow!("health admission validation requires --source-revision") - })?, - activation_witness_sha256: validation_activation_witness_sha256, - write_admission_witness, - runtime_process, - }), - }); - } - - if runtime_admission_validation { - let input = validation_input - .ok_or_else(|| anyhow!("runtime admission validation requires --input"))?; - let input_daemon = daemon_id - .as_deref() - .ok_or_else(|| anyhow!("runtime admission validation requires --daemon"))?; - require_supported_runtime_admission_pair(input_daemon, &input)?; - let runtime_process = runtime_process.ok_or_else(|| { - anyhow!( - "runtime admission validation requires --runtime-process-id and --runtime-process-starttime-ticks" - ) - })?; - return Ok(Self { - common, - mode: Mode::RuntimeAdmissionValidation(RuntimeAdmissionValidationOptions { - input, - daemon_id: daemon_id - .ok_or_else(|| anyhow!("runtime admission validation requires --daemon"))?, - deployment_request_id: validation_deployment_request_id.ok_or_else(|| { - anyhow!("runtime admission validation requires --deployment-request-id") - })?, - release_id: validation_release_id.ok_or_else(|| { - anyhow!("runtime admission validation requires --release-id") - })?, - release_witness_sha256: validation_release_witness_sha256.ok_or_else(|| { - anyhow!("runtime admission validation requires --release-witness-sha256") - })?, - source_commit: validation_source_revision.ok_or_else(|| { - anyhow!("runtime admission validation requires --source-revision") - })?, - activation_witness_sha256: validation_activation_witness_sha256.ok_or_else( - || { - anyhow!( - "runtime admission validation requires --activation-witness-sha256" - ) - }, - )?, - runtime_process, - }), - }); - } - - if swarm_profile.as_deref() == Some("yggdrasil-local") - && common.release_authority_store_path.is_none() - { - return Err(anyhow!( - "--release-authority-store is required for yggdrasil-local release targets" - )); - } - // Epiphany is one supervised target, not a prerequisite for Idunn. - // Without its migration trust anchor only Epiphany's legacy signed - // health is inadmissible; unrelated targets retain their authority. - if swarm_profile.as_deref() == Some("yggdrasil-local") - && (common.deployment_brake_store_path.is_none() - || common.deployment_brake_operator_anchor_path.is_none()) - { - return Err(anyhow!( - "--deployment-brake-store and --deployment-brake-operator-anchor are required for yggdrasil-local" - )); - } - - if let Some(action) = lifecycle_action { - if swarm_profile.is_some() { - return Err(anyhow!( - "lifecycle command publication uses --daemon, not --swarm-profile\n\n{}", - help_text() - )); - } - let Some(daemon_id) = daemon_id else { - return Err(anyhow!( - "lifecycle command publication requires --daemon\n\n{}", - help_text() - )); - }; - return Ok(Self { - common, - mode: Mode::LifecycleCommand(LifecycleCommandOptions { - daemon_id, - action, - requested_by, - detail: command_detail, - }), - }); - } - - let mode = match (swarm_profile, daemon_id) { - (Some(profile), None) => Mode::Swarm(SwarmOptions { profile, repo_root }), - (Some(_), Some(_)) => { - return Err(anyhow!( - "use either --swarm-profile or --daemon, not both\n\n{}", - help_text() - )); - } - (None, Some(daemon_id)) => { - let interval_seconds = interval_seconds.unwrap_or(30); - if interval_seconds == 0 { - return Err(anyhow!("--interval-seconds must be greater than zero")); - } - Mode::Single(DaemonTarget { - daemon_id: daemon_id.clone(), - verse_id, - name: name.unwrap_or(daemon_id), - health_contract: health_contract("manual.cultnet-rudp-daemon-health", "failed"), - deploy_command, - restart_command, - release: None, - enabled, - interval_seconds, - }) - } - (None, None) => { - return Err(anyhow!( - "either --daemon or --swarm-profile is required\n\n{}", - help_text() - )); - } - }; - - if common.public_health_query_bind.is_some() && !matches!(mode, Mode::Swarm(_)) { - return Err(anyhow!( - "--public-health-query-bind requires --swarm-profile" - )); - } - if common.public_health_query_bind.is_some() - && (common.public_health_store_path.is_none() - || common.service_identity_store_path.is_none()) - { - return Err(anyhow!( - "--public-health-query-bind requires --public-health-store and --service-identity-store" - )); - } - - Ok(Self { common, mode }) - } -} - -fn validate_minimum_source_revision_cli_shape(args: &[String]) -> Result<()> { - if args.len() != 5 { - return Err(anyhow!( - "validate-minimum-source-revision accepts only --daemon and --source-revision" - )); - } - let mut seen = BTreeSet::new(); - for pair in args[1..].chunks_exact(2) { - if pair[1].trim().is_empty() - || !matches!(pair[0].as_str(), "--daemon" | "--source-revision") - || !seen.insert(pair[0].as_str()) - { - return Err(anyhow!( - "validate-minimum-source-revision accepts one --daemon and one --source-revision" - )); - } - } - if !seen.contains("--daemon") || !seen.contains("--source-revision") { - return Err(anyhow!( - "validate-minimum-source-revision requires --daemon and --source-revision" - )); - } - Ok(()) -} - -fn take_value(args: &mut impl Iterator, name: &str) -> Result { - args.next() - .ok_or_else(|| anyhow!("{name} requires a value")) -} - -fn publish_lifecycle_command( - command: &LifecycleCommandOptions, - options: &CommonOptions, -) -> Result<()> { - let now = timestamp()?; - let action = match command.action { - LifecycleAction::Restart => "restart", - LifecycleAction::Redeploy => "redeploy", - }; - let record = IdunnLifecycleCommandRecord { - command_id: format!("manual:{}:{}:{}", action, command.daemon_id, now), - daemon_id: command.daemon_id.clone(), - action: action.to_string(), - state: "pending".to_string(), - requested_by: command.requested_by.clone(), - requested_at: now.clone(), - detail: command.detail.clone(), - claimed_at: String::new(), - result_id: String::new(), - }; - let store_lock = Arc::new(Mutex::new(())); - with_store_node(options, &store_lock, |node| { - node.put(&record.command_id, &record)?; - Ok(()) - })?; - println!( - "Idunn lifecycle command {} queued for {} as {}.", - record.action, record.daemon_id, record.command_id - ); - Ok(()) -} - -fn run_restart( - request: &odin_core::IdunnRestartRequestRecord, - requested_at: &str, - options: &CommonOptions, -) -> IdunnRestartResultRecord { - let result_id = format!("result:{}", request.request_id); - match run_continuity_shell(&request.command, options, None) { - Ok(output) if output.status.success() => IdunnRestartResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "succeeded".to_string(), - detail: command_output_detail("restart command exited successfully", &output), - completed_at: requested_at.to_string(), - }, - Ok(output) => IdunnRestartResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: command_output_detail( - &format!("restart command exited with {}", output.status), - &output, - ), - completed_at: requested_at.to_string(), - }, - Err(error) => IdunnRestartResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: format!("restart command could not run: {error}"), - completed_at: requested_at.to_string(), - }, - } -} - -fn run_deployment( - request: &odin_core::IdunnDeploymentRequestRecord, - requested_at: &str, - options: &CommonOptions, - store_lock: &Arc>, -) -> IdunnDeploymentResultRecord { - let result_id = format!("result:{}", request.request_id); - if let Err(error) = revalidate_deployment_request(request, options, store_lock) { - return IdunnDeploymentResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: format!("deployment authority revalidation failed: {error:#}"), - completed_at: requested_at.to_string(), - }; - } - match run_braked_shell( - &request.command, - options, - &request.source_revision, - &request.request_id, - Some(request), - ) { - Ok(output) if output.status.success() => IdunnDeploymentResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "succeeded".to_string(), - detail: command_output_detail("deployment command exited successfully", &output), - completed_at: requested_at.to_string(), - }, - Ok(output) => IdunnDeploymentResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: command_output_detail( - &format!("deployment command exited with {}", output.status), - &output, - ), - completed_at: requested_at.to_string(), - }, - Err(error) => IdunnDeploymentResultRecord { - result_id, - request_id: request.request_id.clone(), - daemon_id: request.daemon_id.clone(), - state: "failed".to_string(), - detail: format!("deployment command could not run: {error:#}"), - completed_at: requested_at.to_string(), - }, - } -} - -fn command_output_detail(prefix: &str, output: &std::process::Output) -> String { - let mut detail = prefix.to_string(); - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - let stdout = stdout.trim(); - let stderr = stderr.trim(); - if !stdout.is_empty() { - detail.push_str("; stdout: "); - detail.push_str(&truncate_detail(stdout, 600)); - } - if !stderr.is_empty() { - detail.push_str("; stderr: "); - detail.push_str(&truncate_detail(stderr, 600)); - } - detail -} - -fn truncate_detail(value: &str, max_chars: usize) -> String { - let chars: Vec = value.chars().collect(); - if chars.len() <= max_chars { - return value.to_string(); - } - - let separator = "......"; - let separator_len = separator.chars().count(); - if max_chars <= separator_len { - return chars.into_iter().take(max_chars).collect(); - } - - let retained = max_chars - separator_len; - let head_len = retained / 3; - let tail_len = retained - head_len; - let head: String = chars.iter().take(head_len).collect(); - let tail: String = chars.iter().skip(chars.len() - tail_len).collect(); - format!("{head}{separator}{tail}") -} - -fn run_state_migration( - target: &DaemonTarget, - release: &ReleaseTarget, - deployment: &IdunnDeploymentRequestRecord, - requested_at: &str, - options: &CommonOptions, -) -> Option { - let plan_id = migration_plan_id(target); - let result_id = format!("result:{plan_id}:{requested_at}"); - let Some(command) = release.state_migration_command.as_deref() else { - return Some(IdunnStateMigrationResultRecord { - result_id, - plan_id, - daemon_id: target.daemon_id.clone(), - state: "noop".to_string(), - detail: "daemon declared no state migration command".to_string(), - completed_at: requested_at.to_string(), - }); - }; - // Migration is consequential deployment work. It consumes the same exact - // release/request grant as the deployment spawn that follows it. - match run_braked_shell( - command, - options, - &deployment.source_revision, - &deployment.request_id, - Some(deployment), - ) { - Ok(output) if output.status.success() => Some(IdunnStateMigrationResultRecord { - result_id, - plan_id, - daemon_id: target.daemon_id.clone(), - state: "succeeded".to_string(), - detail: command_output_detail("state migration command exited successfully", &output), - completed_at: requested_at.to_string(), - }), - Ok(output) => Some(IdunnStateMigrationResultRecord { - result_id, - plan_id, - daemon_id: target.daemon_id.clone(), - state: "failed".to_string(), - detail: command_output_detail( - &format!("state migration command exited with {}", output.status), - &output, - ), - completed_at: requested_at.to_string(), - }), - Err(error) => Some(IdunnStateMigrationResultRecord { - result_id, - plan_id, - daemon_id: target.daemon_id.clone(), - state: "failed".to_string(), - detail: format!("state migration command could not run: {error}"), - completed_at: requested_at.to_string(), - }), - } -} - -fn rollout_result_record( - target: &DaemonTarget, - release: &ReleaseTarget, - deployment: &IdunnDeploymentResultRecord, - migration: Option<&IdunnStateMigrationResultRecord>, - completed_at: &str, -) -> IdunnRolloutResultRecord { - let migration_detail = migration - .map(|result| format!("migration {}: {}", result.state, result.detail)) - .unwrap_or_else(|| "migration not declared".to_string()); - let downtime_detail = if release.zero_downtime_capability == "zero-downtime" { - "zero-downtime strategy declared" - } else { - "restart-required strategy; zero downtime not claimed" - }; - IdunnRolloutResultRecord { - result_id: format!("result:{}:{completed_at}", rollout_plan_id(target)), - plan_id: rollout_plan_id(target), - daemon_id: target.daemon_id.clone(), - state: deployment.state.clone(), - detail: format!( - "{migration_detail}; deployment {}: {}; {downtime_detail}", - deployment.state, deployment.detail - ), - completed_at: completed_at.to_string(), - } -} - -fn run_shell( - command: &str, - options: &CommonOptions, - deployment_brake: Option<(&str, &str)>, - deployment: Option<&IdunnDeploymentRequestRecord>, -) -> Result { - let mut process = if cfg!(windows) { - let path = windows_command_path(); - let command = format!(r#"set "PATH={path}" && set "Path={path}" && {command}"#); - let mut process = Command::new("cmd"); - process.arg("/D").arg("/S").arg("/C").arg(command); - apply_windows_command_environment(&mut process, &path); - process - } else { - let mut process = Command::new("sh"); - process.arg("-c").arg(command); - process - }; - - process - .env("IDUNN_ACTUATOR", "1") - .env("IDUNN_COMMAND_AUTHORITY", "idunn-daemon"); - if let Some(request) = deployment { - process - .env("IDUNN_SOURCE_COMMIT", &request.source_revision) - .env("IDUNN_REPOSITORY_FULL_NAME", &request.repository_full_name) - .env("IDUNN_UPSTREAM_REF", &request.upstream_ref) - .env( - "BIFROST_RELEASE_AUTHORITY_ID", - &request.release_authority_id, - ) - .env( - "BIFROST_RELEASE_AUTHORITY_SHA256", - &request.release_authority_envelope_sha256, - ) - .env("IDUNN_DEPLOYMENT_REQUEST_ID", &request.request_id); - process.env( - "IDUNN_REQUIRES_BIFROST_AUTHORITY", - if request.requires_bifrost_authority { - "true" - } else { - "false" - }, - ); - } - if let Some(endpoint) = actuator_idunn_rudp_health_endpoint(options) { - process - .env("IDUNN_RUDP_HEALTH", &endpoint) - .env("ODIN_IDUNN_RUDP_HEALTH", &endpoint); - } - - let stdout_path = command_output_path("stdout")?; - let stderr_path = command_output_path("stderr")?; - let stdout_file = File::create(&stdout_path) - .with_context(|| format!("creating {}", stdout_path.display()))?; - let stderr_file = File::create(&stderr_path) - .with_context(|| format!("creating {}", stderr_path.display()))?; - - configure_command_lifetime(&mut process); - let spawn = || { - process - .stdout(Stdio::from(stdout_file)) - .stderr(Stdio::from(stderr_file)) - .spawn() - .with_context(|| format!("running command {command:?}")) - }; - let child = if let Some((release_id, deployment_id)) = deployment_brake { - // Hold the root-owned deployment-authority lock from the final read - // through spawn. This gate owns artifact/configuration mutation only. - with_verified_deployment_brake(options, release_id, deployment_id, spawn)? - } else { - // Restarting an already-admitted body is continuity physiology. It - // deliberately does not consume deployment authority. - spawn()? - }; - let status = wait_for_child_status_with_timeout( - child, - Duration::from_secs(options.command_timeout_seconds), - command, - )?; - let stdout = fs::read(&stdout_path).unwrap_or_default(); - let stderr = fs::read(&stderr_path).unwrap_or_default(); - let _ = fs::remove_file(&stdout_path); - let _ = fs::remove_file(&stderr_path); - Ok(std::process::Output { - status, - stdout, - stderr, - }) -} - -/// The single final consequence gate. Callers may plan and persist before this, -/// but every migration, deployment, and restart re-opens both root-owned files -/// here, immediately before `Command::spawn`. -fn run_braked_shell( - command: &str, - options: &CommonOptions, - release_id: &str, - deployment_id: &str, - deployment: Option<&IdunnDeploymentRequestRecord>, -) -> Result { - run_shell( - command, - options, - Some((release_id, deployment_id)), - deployment, - ) -} - -fn run_continuity_shell( - command: &str, - options: &CommonOptions, - deployment: Option<&IdunnDeploymentRequestRecord>, -) -> Result { - run_shell(command, options, None, deployment) -} - -fn with_verified_deployment_brake( - options: &CommonOptions, - release_id: &str, - deployment_id: &str, - consequence: impl FnOnce() -> Result, -) -> Result { - let store_path = options - .deployment_brake_store_path - .as_ref() - .ok_or_else(|| anyhow!("--deployment-brake-store is required for actuation"))?; - SingleFileMessagePackBackingStore::new(store_path) - .with_read_only_shared_snapshot(|entries| { - verify_deployment_brake_snapshot(options, release_id, deployment_id, entries)?; - consequence() - }) - .with_context(|| format!("reading deployment brake {}", store_path.display())) -} - -fn verify_deployment_brake_snapshot( - options: &CommonOptions, - release_id: &str, - deployment_id: &str, - entries: Vec, -) -> Result<()> { - let anchor_path = options - .deployment_brake_operator_anchor_path - .as_ref() - .ok_or_else(|| anyhow!("--deployment-brake-operator-anchor is required for actuation"))?; - let runtime_id = options - .deployment_brake_runtime_id - .as_deref() - .ok_or_else(|| anyhow!("deployment brake runtime id is not configured"))?; - let observation = match entries.as_slice() { - [entry] - if entry.r#type == IdunnDeploymentBrakeRecord::TYPE - && entry.key == cultnet_rs::IDUNN_DEPLOYMENT_BRAKE_ID - && entry.schema_id.as_deref() == Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA) => - { - match rmp_serde::from_slice::(&entry.payload) { - Ok(record) - if rmp_serde::to_vec(&record).ok().as_deref() - == Some(entry.payload.as_slice()) => - { - record - } - _ => return Err(anyhow!("deployment brake is corrupt")), - } - } - [] => return Err(anyhow!("deployment brake is missing")), - _ => return Err(anyhow!("deployment brake store is corrupt or ambiguous")), - }; - let anchor_entries = SingleFileMessagePackBackingStore::new(anchor_path) - .pull_all_read_only_snapshot() - .with_context(|| { - format!( - "reading deployment brake operator anchor {}", - anchor_path.display() - ) - })?; - let [anchor_entry] = anchor_entries.as_slice() else { - return Err(anyhow!( - "deployment brake operator anchor is missing or ambiguous" - )); - }; - if anchor_entry.r#type != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_TYPE - || anchor_entry.key != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_KEY - || anchor_entry.schema_id.as_deref() - != Some(IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_SCHEMA) - { - return Err(anyhow!("deployment brake operator anchor is foreign")); - } - let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&anchor_entry.payload) - .context("decoding deployment brake operator anchor")?; - if rmp_serde::to_vec(&anchor)? != anchor_entry.payload { - return Err(anyhow!("deployment brake operator anchor is noncanonical")); - } - evaluate_idunn_deployment_brake( - IdunnDeploymentBrakeObservation::Present(&observation), - &anchor, - runtime_id, - release_id, - deployment_id, - unix_epoch_millis()?, - ) - .map_err(|denial| anyhow!("deployment brake denied actuation: {denial:?}")) -} - -fn actuator_idunn_rudp_health_endpoint(options: &CommonOptions) -> Option { - let bind = options.rudp_health_bind?; - let host = if bind.ip().is_unspecified() { - "127.0.0.1".to_string() - } else { - bind.ip().to_string() - }; - Some(format!("{host}:{}", bind.port())) -} - -fn run_operator_alarm_command(options: &CommonOptions, alarm: &IdunnOperatorAlarmRecord) { - let Some(command) = options.operator_alarm_command.as_deref() else { - return; - }; - if command.trim().is_empty() { - return; - } - - let mut process = if cfg!(windows) { - let path = windows_command_path(); - let command = format!(r#"set "PATH={path}" && set "Path={path}" && {command}"#); - let mut process = Command::new("cmd"); - process.arg("/D").arg("/S").arg("/C").arg(command); - apply_windows_command_environment(&mut process, &path); - process - } else { - let mut process = Command::new("sh"); - process.arg("-c").arg(command); - process - }; - - configure_command_lifetime(&mut process); - let output = process - .env("IDUNN_ALARM_ID", &alarm.alarm_id) - .env("IDUNN_ALARM_DAEMON_ID", &alarm.daemon_id) - .env("IDUNN_ALARM_SEVERITY", &alarm.severity) - .env("IDUNN_ALARM_REASON", &alarm.reason) - .env("IDUNN_ALARM_ESCALATION_TARGET", &alarm.escalation_target) - .env("IDUNN_ALARM_RAISED_AT", &alarm.raised_at) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn(); - - match output { - Ok(child) => match wait_for_child_with_timeout( - child, - Duration::from_secs(options.command_timeout_seconds), - command, - ) { - Ok(output) if output.status.success() => { - println!( - "Idunn operator alarm command completed for {}.", - alarm.daemon_id - ); - } - Ok(output) => { - eprintln!( - "Idunn operator alarm command failed for {} with {}.", - alarm.daemon_id, output.status - ); - } - Err(error) => { - eprintln!( - "Idunn operator alarm command could not run for {}: {}", - alarm.daemon_id, error - ); - } - }, - Err(error) => { - eprintln!( - "Idunn operator alarm command could not run for {}: {}", - alarm.daemon_id, error - ); - } - } -} - -fn windows_command_path() -> String { - [ - r"C:\WINDOWS\system32", - r"C:\WINDOWS", - r"C:\WINDOWS\System32\Wbem", - r"C:\WINDOWS\System32\WindowsPowerShell\v1.0", - r"C:\WINDOWS\System32\OpenSSH", - r"C:\Program Files\Git\cmd", - r"C:\Program Files\nodejs", - r"C:\Program Files\Docker\Docker\resources\bin", - r"C:\Users\Meta\AppData\Local\Programs\Ollama", - r"C:\Program Files\dotnet", - r"C:\Users\Meta\.cargo\bin", - ] - .join(";") -} - -fn apply_windows_command_environment(process: &mut Command, path: &str) { - if !cfg!(windows) { - return; - } - - process.env("PATH", path); - process.env("Path", path); -} - -#[cfg(unix)] -fn configure_command_lifetime(process: &mut Command) { - use std::os::unix::process::CommandExt; - process.process_group(0); -} - -#[cfg(not(unix))] -fn configure_command_lifetime(_process: &mut Command) {} - -#[cfg(unix)] -fn terminate_command_lifetime(child: &mut Child) { - let process_group = i32::try_from(child.id()).expect("Unix child pid exceeded i32"); - // Idunn created the child as leader of this private process group. Killing - // the group ends sudo, shells, and deployment descendants under the same - // command lifetime instead of merely severing the parent we can see. - unsafe { - libc::kill(-process_group, libc::SIGKILL); - } -} - -#[cfg(not(unix))] -fn terminate_command_lifetime(child: &mut Child) { - let _ = child.kill(); -} - -fn wait_for_child_with_timeout( - mut child: Child, - timeout: Duration, - command: &str, -) -> Result { - let started_at = Instant::now(); - - loop { - if child - .try_wait() - .with_context(|| format!("waiting on command {command:?}"))? - .is_some() - { - return child - .wait_with_output() - .with_context(|| format!("collecting output for command {command:?}")); - } - - if started_at.elapsed() >= timeout { - terminate_command_lifetime(&mut child); - let _ = child.wait_with_output(); - return Err(anyhow!( - "command timed out after {} seconds: {command:?}", - timeout.as_secs() - )); - } - - thread::sleep(Duration::from_millis(100)); - } -} - -fn wait_for_child_status_with_timeout( - mut child: Child, - timeout: Duration, - command: &str, -) -> Result { - let started_at = Instant::now(); - - loop { - if let Some(status) = child - .try_wait() - .with_context(|| format!("waiting on command {command:?}"))? - { - return Ok(status); - } - - if started_at.elapsed() >= timeout { - terminate_command_lifetime(&mut child); - let _ = child.wait(); - return Err(anyhow!( - "command timed out after {} seconds: {command:?}", - timeout.as_secs() - )); - } - - thread::sleep(Duration::from_millis(100)); - } -} - -fn command_output_path(kind: &str) -> Result { - let millis = unix_epoch_millis()?; - Ok(env::temp_dir().join(format!( - "idunn-command-{kind}-{}-{millis}.log", - std::process::id() - ))) -} - -fn timestamp() -> Result { - timestamp_from_unix_millis(unix_epoch_millis()?) -} - -fn timestamp_from_unix_millis(millis: u64) -> Result { - let millis = i64::try_from(millis).context("system clock milliseconds exceeded i64")?; - let timestamp = chrono::DateTime::from_timestamp_millis(millis) - .ok_or_else(|| anyhow!("system clock milliseconds are out of range"))?; - Ok(timestamp.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)) -} - -fn unix_epoch_millis() -> Result { - let millis = SystemTime::now() - .duration_since(UNIX_EPOCH) - .context("system clock is before Unix epoch")? - .as_millis(); - u64::try_from(millis).context("system clock milliseconds exceeded u64") -} - -fn help_text() -> &'static str { - concat!( - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--release-authority-store ] [--daemon-health-trust-store ] [--deploy-command ] [--restart-command ] [--operator-alarm-command ] [--rudp-health-bind ] [--trusted-epiphany-health-identity-store ] [--execute] [--interval-seconds ] [--command-timeout-seconds ] [--repo-root ] [--swarm-profile ] [--service-identity-store ] [--public-health-store ] [--public-health-query-bind ]\n", - " idunn restart --daemon [--store ] [--requested-by ] [--detail ]\n", - " idunn redeploy --daemon [--store ] [--requested-by ] [--detail ]\n", - " idunn validate-minimum-source-revision --daemon --source-revision \n", - " idunn validate-health-admission --store --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision [--activation-witness-sha256 ] [--write-admission-witness --runtime-process-id --runtime-process-starttime-ticks ]\n", - " idunn validate-runtime-admission --input --daemon --deployment-request-id --release-id --release-witness-sha256 --source-revision --activation-witness-sha256 --runtime-process-id --runtime-process-starttime-ticks \n\n", - "Idunn supervises owner-authenticated CultNet/RUDP health with --daemon, or a built-in swarm supervisor with --swarm-profile starfire-local or yggdrasil-local. Generic signed health requires a root-owned CultCache trust store via --daemon-health-trust-store. Unsigned idunn.daemon_health packets are diagnostic-only and cannot satisfy lifecycle health. Yggdrasil release targets that require Bifrost authorization need an explicit CultCache path via --release-authority-store; source-observed targets do not borrow that authority. The Epiphany v0 migration path additionally requires its pinned host identity. RUDP health ingress is disabled unless --rudp-health-bind is supplied. The read-only outward projection listener is disabled unless --public-health-query-bind is supplied with the dedicated public store and Idunn service identity; it serves only target-catalog authenticated-provider projection keys and never opens private Idunn state. Runtime traffic admission publication supports exactly yggdrasil-codex-connector at /etc/gamecult/codex-connector/runtime/traffic-admission.cc and yggdrasil-ghostlight at /etc/gamecult/ghostlight-dungeon/runtime/traffic-admission.cc. Each v2 grant requires exact generic warming/traffic-admission-pending health, complete release and activation lineage, and root-observed Linux PID/starttime identity. Idunn holds the Idunn-owned source store and root-owned trust-store shared locks in that fixed order from exact snapshot revalidation through root-owned grant CAS publication; the locks coordinate their respective owners and do not own the decision. It takes a fresh clock and re-observes /proc before and immediately after publication; failure revokes the grant. The grant and its sibling lock are sealed for read-only target snapshots. Ordinary health validation remains active-only, while read-side grant validation requires and re-observes the exact expected process identity. The restart/redeploy verbs publish typed idunn.lifecycle_command.v1 records; the running supervisor claims them and executes only through its configured command boundary." - ) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn unconfigured_actuation_options(store_path: PathBuf) -> CommonOptions { - CommonOptions { - store_path, - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: true, - command_timeout_seconds: 2, - } - } - - #[test] - fn continuity_restart_does_not_consume_deployment_brake_authority() { - let root = tempfile::tempdir().unwrap(); - let options = unconfigured_actuation_options(root.path().join("idunn.cc")); - let request = IdunnRestartRequestRecord { - request_id: "restart:test:continuity".into(), - daemon_id: "test-daemon".into(), - command: "exit 0".into(), - authority: "idunn-supervisor-command.test".into(), - requested_at: "unix:100".into(), - }; - - let result = run_restart(&request, "unix:100", &options); - - assert_eq!(result.state, "succeeded"); - let deployment_error = run_braked_shell( - "exit 0", - &options, - "deployment-release", - "deployment-request", - None, - ) - .unwrap_err() - .to_string(); - assert!(deployment_error.contains("--deployment-brake-store is required")); - } - - #[test] - fn coarse_unix_admission_time_covers_its_whole_second() { - assert_eq!(parse_timestamp_millis("unix:42"), Some(42_000)); - assert_eq!(parse_admission_ceiling_millis("unix:42"), Some(42_999)); - assert_eq!( - parse_admission_ceiling_millis("2026-08-20T18:24:06.321Z"), - parse_timestamp_millis("2026-08-20T18:24:06.321Z") - ); - } - - #[test] - fn runtime_timestamp_preserves_the_health_admission_millisecond() { - let timestamp = timestamp_from_unix_millis(42_123).unwrap(); - - assert_eq!(timestamp, "1970-01-01T00:00:42.123Z"); - assert_eq!(parse_timestamp_millis(×tamp), Some(42_123)); - assert_eq!(parse_admission_ceiling_millis(×tamp), Some(42_123)); - } - - #[test] - fn deployable_source_authority_ignores_documentation_only_commits() { - let root = tempfile::tempdir().unwrap(); - let repo = root.path().to_path_buf(); - let git = |args: &[&str]| { - let status = Command::new("git") - .arg("-C") - .arg(&repo) - .args(args) - .status() - .unwrap(); - assert!(status.success(), "git command failed: {args:?}"); - }; - - git(&["init", "-b", "main"]); - git(&["config", "user.email", "idunn-test@gamecult.org"]); - git(&["config", "user.name", "Idunn Test"]); - fs::create_dir_all(repo.join("crates/runtime")).unwrap(); - fs::write(repo.join("crates/runtime/lib.rs"), "pub fn live() {}\n").unwrap(); - git(&["add", "."]); - git(&["commit", "-m", "runtime"]); - let runtime_commit = git_revision(&repo, "HEAD").unwrap(); - - fs::create_dir_all(repo.join("docs")).unwrap(); - fs::write(repo.join("docs/status.md"), "runtime is live\n").unwrap(); - git(&["add", "."]); - git(&["commit", "-m", "docs"]); - let documentation_commit = git_revision(&repo, "HEAD").unwrap(); - assert_ne!(documentation_commit, runtime_commit); - git(&[ - "update-ref", - "refs/remotes/origin/main", - &documentation_commit, - ]); - - let release = with_source_change_pathspecs( - release_target("Test", repo.clone(), "restart", None, "restart"), - DEPLOYABLE_SOURCE_PATHS, - ); - assert_eq!( - SystemReleaseStatePort.desired_revision(&release).unwrap(), - runtime_commit - ); - - fs::write( - repo.join("crates/runtime/lib.rs"), - "pub fn live() { println!(\"awake\"); }\n", - ) - .unwrap(); - git(&["add", "."]); - git(&["commit", "-m", "runtime change"]); - let changed_runtime_commit = git_revision(&repo, "HEAD").unwrap(); - git(&[ - "update-ref", - "refs/remotes/origin/main", - &changed_runtime_commit, - ]); - assert_eq!( - SystemReleaseStatePort.desired_revision(&release).unwrap(), - changed_runtime_commit - ); - } - - #[test] - fn minimum_source_proofs_keep_offline_continuity_independent_of_canonical_drift() { - let root = tempfile::tempdir().unwrap(); - let repo = root.path().to_path_buf(); - let git = |args: &[&str]| { - let status = Command::new("git") - .arg("-C") - .arg(&repo) - .args(args) - .status() - .unwrap(); - assert!(status.success(), "git command failed: {args:?}"); - }; - - git(&["init", "-b", "main"]); - git(&["config", "user.email", "idunn-test@gamecult.org"]); - git(&["config", "user.name", "Idunn Test"]); - fs::write(repo.join("body.txt"), "pre-floor\n").unwrap(); - git(&["add", "."]); - git(&["commit", "-m", "pre-floor"]); - let pre_floor = git_revision(&repo, "HEAD").unwrap(); - - fs::write(repo.join("body.txt"), "floor\n").unwrap(); - git(&["commit", "-am", "floor"]); - let floor = git_revision(&repo, "HEAD").unwrap(); - fs::write(repo.join("body.txt"), "candidate\n").unwrap(); - git(&["commit", "-am", "candidate"]); - let candidate = git_revision(&repo, "HEAD").unwrap(); - git(&["update-ref", "refs/remotes/origin/canonical", &candidate]); - const OFFLINE_ORIGIN: &str = "https://127.0.0.1:9/idunn-offline-proof.git"; - git(&["remote", "add", "origin", OFFLINE_ORIGIN]); - let floor_for_policy: &'static str = Box::leak(floor.clone().into_boxed_str()); - let policy = CompiledMinimumSourceRevisionPolicy { - daemon_id: "offline-test-daemon", - repository_full_name: "GameCult/OfflineTest", - canonical_repository_url: OFFLINE_ORIGIN, - upstream_ref: "refs/heads/canonical", - canonical_fetched_ref: "refs/remotes/origin/canonical", - repository_path: "/not-used-by-local-proof", - minimum_source_revision: floor_for_policy, - }; - - require_minimum_source_revision(&repo, &candidate, &policy).unwrap(); - require_deployment_source_revision(&repo, &candidate, &policy).unwrap(); - assert!(require_minimum_source_revision(&repo, &pre_floor, &policy).is_err()); - - fs::write(repo.join("body.txt"), "ahead of fetched canonical\n").unwrap(); - git(&["commit", "-am", "ahead of fetched canonical"]); - let not_fetched = git_revision(&repo, "HEAD").unwrap(); - require_minimum_source_revision(&repo, ¬_fetched, &policy).unwrap(); - assert!(require_deployment_source_revision(&repo, ¬_fetched, &policy).is_err()); - - git(&["checkout", "-b", "unrelated", &pre_floor]); - fs::write(repo.join("body.txt"), "unrelated\n").unwrap(); - git(&["commit", "-am", "unrelated"]); - let unrelated = git_revision(&repo, "HEAD").unwrap(); - assert!(require_minimum_source_revision(&repo, &unrelated, &policy).is_err()); - - git(&[ - "remote", - "set-url", - "origin", - "https://github.com/attacker/substitute.git", - ]); - assert!(require_minimum_source_revision(&repo, &candidate, &policy).is_err()); - } - - #[test] - fn target_actuation_gate_serializes_request_owners() { - let gate = Arc::new(TargetActuationGate::new()); - let (entered_tx, entered_rx) = std::sync::mpsc::channel(); - let (release_tx, release_rx) = std::sync::mpsc::channel(); - let first_gate = Arc::clone(&gate); - let first_entered = entered_tx.clone(); - let first = thread::spawn(move || { - with_target_actuation_gate(&first_gate.lock, || { - first_entered.send("manual").unwrap(); - release_rx.recv().unwrap(); - Ok(()) - }) - .unwrap(); - }); - assert_eq!(entered_rx.recv().unwrap(), "manual"); - - let second_gate = Arc::clone(&gate); - let second_entered = entered_tx; - let second = thread::spawn(move || { - with_target_actuation_gate(&second_gate.lock, || { - second_entered.send("automatic").unwrap(); - Ok(()) - }) - .unwrap(); - }); - assert!( - entered_rx.recv_timeout(Duration::from_millis(100)).is_err(), - "automatic request owner entered while manual actuation was live" - ); - release_tx.send(()).unwrap(); - assert_eq!(entered_rx.recv().unwrap(), "automatic"); - first.join().unwrap(); - second.join().unwrap(); - } - - #[test] - fn blocked_actuation_does_not_block_observation_and_admission_work() { - let gate = Arc::new(TargetActuationGate::new()); - assert!(!gate.reserved.swap(true, Ordering::AcqRel)); - let manual_guard = gate.lock.lock().unwrap(); - let (actuation_tx, actuation_rx) = std::sync::mpsc::channel(); - schedule_target_actuation(Arc::clone(&gate), "epiphany".into(), move || { - actuation_tx.send("actuated").unwrap(); - Ok(()) - }); - - assert!( - actuation_rx - .recv_timeout(Duration::from_millis(100)) - .is_err() - ); - // This is the target loop's independent signed-health admission lane: - // it does not acquire or await the consequence gate. - let admitted_health = "accepted"; - assert_eq!(admitted_health, "accepted"); - - drop(manual_guard); - gate.reserved.store(false, Ordering::Release); - let (actuation_tx, actuation_rx) = std::sync::mpsc::channel(); - schedule_target_actuation(Arc::clone(&gate), "epiphany".into(), move || { - actuation_tx.send("actuated").unwrap(); - Ok(()) - }); - assert_eq!(actuation_rx.recv().unwrap(), "actuated"); - while gate.reserved.load(Ordering::Acquire) { - thread::yield_now(); - } - } - - #[cfg(unix)] - fn descendant_marker_command(marker: &std::path::Path) -> Child { - let mut command = Command::new("sh"); - command - .arg("-c") - .arg(format!("(sleep 1; touch '{}') & wait", marker.display())) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()); - configure_command_lifetime(&mut command); - command.spawn().expect("spawn process-group test command") - } - - #[cfg(unix)] - #[test] - fn status_timeout_terminates_command_descendants() { - let marker = - env::temp_dir().join(format!("idunn-status-descendant-{}", std::process::id())); - let _ = fs::remove_file(&marker); - let error = wait_for_child_status_with_timeout( - descendant_marker_command(&marker), - Duration::from_millis(100), - "descendant timeout test", - ) - .expect_err("command should time out"); - assert!(error.to_string().contains("timed out")); - thread::sleep(Duration::from_millis(1200)); - assert!( - !marker.exists(), - "timed-out descendant survived to write marker" - ); - } - - #[cfg(unix)] - #[test] - fn output_timeout_terminates_command_descendants() { - let marker = - env::temp_dir().join(format!("idunn-output-descendant-{}", std::process::id())); - let _ = fs::remove_file(&marker); - let error = wait_for_child_with_timeout( - descendant_marker_command(&marker), - Duration::from_millis(100), - "descendant output timeout test", - ) - .expect_err("command should time out"); - assert!(error.to_string().contains("timed out")); - thread::sleep(Duration::from_millis(1200)); - assert!( - !marker.exists(), - "timed-out descendant survived to write marker" - ); - } - use cultnet_rs::{CultNetRawDocumentRecord, CultNetRawPayloadEncoding}; - use ed25519_dalek::{Signer, SigningKey}; - use std::cell::RefCell; - - const EPIPHANY_SHA: &str = "0123456789abcdef0123456789abcdef01234567"; - - #[test] - fn truncated_command_detail_preserves_failure_tail() { - let value = format!("{}fatal: promotion failed", "compiler noise\n".repeat(100)); - let detail = truncate_detail(&value, 120); - - assert_eq!(detail.chars().count(), 120); - assert!(detail.starts_with("compiler noise")); - assert!(detail.contains("......")); - assert!(detail.ends_with("fatal: promotion failed")); - } - - #[test] - fn short_command_detail_is_unchanged() { - assert_eq!(truncate_detail("precise failure", 120), "precise failure"); - } - - fn authority_record(status: &str) -> BifrostRepositoryReleaseAuthorityRecord { - let repository = "GameCult/Epiphany"; - let upstream_ref = "refs/heads/main"; - BifrostRepositoryReleaseAuthorityRecord { - authority_id: release_authority_id(repository, upstream_ref, EPIPHANY_SHA), - command_id: "cultmesh-command-epiphany-release".to_string(), - crossing_receipt_id: "crossing_epiphany_release".to_string(), - repository_full_name: repository.to_string(), - upstream_ref: upstream_ref.to_string(), - commit_sha: EPIPHANY_SHA.to_string(), - decision: "authorize".to_string(), - status: status.to_string(), - policy_decision_id: "policy-epiphany-release".to_string(), - authority_reference: "bifrost:release:epiphany".to_string(), - actor_identity: "metacrat".to_string(), - source_kind: "bifrost.governance.topic".to_string(), - source_id: "topic-epiphany-release".to_string(), - epiphany_run_id: "run-epiphany-release".to_string(), - epiphany_lane_id: "hands-publication".to_string(), - epiphany_agent_identity: "epiphany".to_string(), - external_receipt_url: "https://github.com/GameCult/Epiphany/commit/0123456789abcdef0123456789abcdef01234567".to_string(), - external_receipt_id: EPIPHANY_SHA.to_string(), - authorized_at: "2026-07-16T00:00:00Z".to_string(), - expires_at: String::new(), - revoked_at: if status == "revoked" { "2026-07-16T01:00:00Z".to_string() } else { String::new() }, - revocation_reason: if status == "revoked" { "operator revoked release".to_string() } else { String::new() }, - } - } - - fn authority_store(record: &BifrostRepositoryReleaseAuthorityRecord) -> PathBuf { - let path = env::temp_dir().join(format!( - "idunn-bifrost-authority-{}-{}.cc", - std::process::id(), - RELEASE_AUTHORITY_SNAPSHOT_SEQUENCE.fetch_add(1, Ordering::Relaxed) - )); - write_authority_store(&path, record); - path - } - - fn write_authority_store( - path: &std::path::Path, - record: &BifrostRepositoryReleaseAuthorityRecord, - ) { - let mut backing = SingleFileMessagePackBackingStore::new(path); - backing - .push(&CultCacheEnvelope { - key: record.authority_id.clone(), - r#type: BifrostRepositoryReleaseAuthorityRecord::TYPE.to_string(), - payload: rmp_serde::to_vec_named(&BifrostReleaseAuthorityWire::from(record)) - .unwrap(), - stored_at: "2026-07-16T00:00:00Z".to_string(), - schema_id: Some(odin_core::BIFROST_REPOSITORY_RELEASE_AUTHORITY_SCHEMA.to_string()), - }) - .unwrap(); - } - - fn decode_hex_fixture(value: &str) -> Vec { - let bytes = value.trim().as_bytes(); - assert_eq!(bytes.len() % 2, 0); - bytes - .chunks_exact(2) - .map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap()) - .collect() - } - - #[test] - fn bifrost_release_authority_golden_contract_decodes_and_seals() { - let record = authority_record("authorized"); - let path = env::temp_dir().join(format!( - "idunn-bifrost-typescript-golden-{}-{}.cc", - std::process::id(), - RELEASE_AUTHORITY_SNAPSHOT_SEQUENCE.fetch_add(1, Ordering::Relaxed) - )); - let payload = decode_hex_fixture(include_str!( - "../tests/fixtures/bifrost-release-authority-v1.payload.hex" - )); - let decoded: BifrostReleaseAuthorityWire = rmp_serde::from_slice(&payload).unwrap(); - assert_eq!(decoded.authority_id, record.authority_id); - let mut backing = SingleFileMessagePackBackingStore::new(&path); - backing - .push(&CultCacheEnvelope { - key: record.authority_id.clone(), - r#type: BifrostRepositoryReleaseAuthorityRecord::TYPE.to_string(), - payload, - stored_at: "2026-07-16T16:56:33.727Z".to_string(), - schema_id: Some(odin_core::BIFROST_REPOSITORY_RELEASE_AUTHORITY_SCHEMA.to_string()), - }) - .unwrap(); - let authorization = CultCacheReleaseAuthorityPort { store_path: &path } - .authorize( - "GameCult/Epiphany", - "refs/heads/main", - EPIPHANY_SHA, - "unix:1784246400", - ) - .unwrap(); - - assert_eq!( - authorization.authority_id, - format!("release:GameCult/Epiphany:refs/heads/main:{EPIPHANY_SHA}") - ); - assert_eq!(authorization.envelope_sha256.len(), 64); - assert!( - authorization - .envelope_sha256 - .bytes() - .all(|byte| byte.is_ascii_hexdigit()) - ); - let _ = fs::remove_file(path); - } - - #[test] - fn revoked_bifrost_release_authority_fails_closed() { - let record = authority_record("revoked"); - let path = authority_store(&record); - let error = CultCacheReleaseAuthorityPort { store_path: &path } - .authorize( - "GameCult/Epiphany", - "refs/heads/main", - EPIPHANY_SHA, - "unix:1784246400", - ) - .unwrap_err() - .to_string(); - - assert!(error.contains("not currently authorized")); - let _ = fs::remove_file(path); - } - - #[test] - fn bifrost_receipt_selects_release_independently_of_newer_upstream_head() { - let record = authority_record("authorized"); - let path = authority_store(&record); - let selected = CultCacheReleaseAuthorityPort { store_path: &path } - .select("GameCult/Epiphany", "refs/heads/main", "unix:1784246400") - .unwrap(); - - assert_eq!(selected.source_revision, EPIPHANY_SHA); - assert_ne!( - selected.source_revision, "ffffffffffffffffffffffffffffffffffffffff", - "a newer observed main must not replace the authorized release" - ); - let _ = fs::remove_file(path); - } - - #[test] - fn multiple_current_bifrost_authorities_fail_release_selection() { - let first = authority_record("authorized"); - let path = authority_store(&first); - let mut second = first.clone(); - second.commit_sha = "fedcba9876543210fedcba9876543210fedcba98".to_string(); - second.authority_id = release_authority_id( - &second.repository_full_name, - &second.upstream_ref, - &second.commit_sha, - ); - second.external_receipt_id = second.commit_sha.clone(); - second.external_receipt_url = format!( - "https://github.com/{}/commit/{}", - second.repository_full_name, second.commit_sha - ); - write_authority_store(&path, &second); - - let error = CultCacheReleaseAuthorityPort { store_path: &path } - .select("GameCult/Epiphany", "refs/heads/main", "unix:1784246400") - .unwrap_err() - .to_string(); - - assert!(error.contains("exactly one current authorized receipt")); - assert!(error.contains("found 2")); - let _ = fs::remove_file(path); - } - - #[test] - fn bifrost_external_proof_must_bind_exact_commit() { - let mut record = authority_record("authorized"); - record.external_receipt_id = "ffffffffffffffffffffffffffffffffffffffff".to_string(); - let path = authority_store(&record); - let error = CultCacheReleaseAuthorityPort { store_path: &path } - .authorize( - "GameCult/Epiphany", - "refs/heads/main", - EPIPHANY_SHA, - "unix:1784246400", - ) - .unwrap_err() - .to_string(); - - assert!(error.contains("external GitHub proof")); - let _ = fs::remove_file(path); - } - - #[test] - fn pre_actuation_revalidation_observes_bifrost_revocation() { - let authorized = authority_record("authorized"); - let path = authority_store(&authorized); - let authorization = CultCacheReleaseAuthorityPort { store_path: &path } - .authorize( - "GameCult/Epiphany", - "refs/heads/main", - EPIPHANY_SHA, - "unix:1784246400", - ) - .unwrap(); - let mut request = IdunnDeploymentRequestRecord { - request_id: "deploy:yggdrasil-epiphany:test".to_string(), - daemon_id: "yggdrasil-epiphany".to_string(), - command: "must-not-run".to_string(), - authority: "idunn-supervisor-command.manual".to_string(), - requested_at: "unix:1784246400".to_string(), - repository_full_name: String::new(), - upstream_ref: String::new(), - source_revision: String::new(), - release_authority_id: String::new(), - release_authority_envelope_sha256: String::new(), - requires_bifrost_authority: false, - }; - apply_release_authorization(&mut request, &authorization); - write_authority_store(&path, &authority_record("revoked")); - let options = CommonOptions { - store_path: env::temp_dir().join("unused-idunn-store.cc"), - release_authority_store_path: Some(path.clone()), - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: true, - command_timeout_seconds: 1, - }; - - let error = revalidate_deployment_request(&request, &options, &Arc::new(Mutex::new(()))) - .unwrap_err() - .to_string(); - - assert!(error.contains("not currently authorized")); - let privileged_error = - validate_release_authority_at_privileged_boundary(&ReleaseAuthorityValidationOptions { - store_path: path.clone(), - repository_full_name: request.repository_full_name.clone(), - upstream_ref: request.upstream_ref.clone(), - source_revision: request.source_revision.clone(), - authority_id: request.release_authority_id.clone(), - envelope_sha256: request.release_authority_envelope_sha256.clone(), - }) - .unwrap_err() - .to_string(); - assert!(privileged_error.contains("not currently authorized")); - let _ = fs::remove_file(path); - } - - #[test] - fn yggdrasil_profile_requires_explicit_bifrost_authority_store() { - let error = Options::parse( - ["--swarm-profile", "yggdrasil-local"] - .into_iter() - .map(ToString::to_string), - ) - .unwrap_err() - .to_string(); - - assert!(error.contains("--release-authority-store is required")); - } - - #[test] - fn privileged_authority_validator_requires_exact_frozen_lineage() { - let authority_id = format!("release:GameCult/Epiphany:refs/heads/main:{EPIPHANY_SHA}"); - let options = Options::parse( - [ - "validate-release-authority", - "--release-authority-store", - "C:/authority.cc", - "--repository-full-name", - "GameCult/Epiphany", - "--upstream-ref", - "refs/heads/main", - "--source-revision", - EPIPHANY_SHA, - "--release-authority-id", - &authority_id, - "--release-authority-sha256", - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - ] - .into_iter() - .map(ToString::to_string), - ) - .unwrap(); - - let Mode::ReleaseAuthorityValidation(validation) = options.mode else { - panic!("expected privileged release-authority validation posture"); - }; - assert_eq!(validation.repository_full_name, "GameCult/Epiphany"); - assert_eq!(validation.source_revision, EPIPHANY_SHA); - assert_eq!(validation.authority_id, authority_id); - } - - #[test] - fn minimum_source_validator_cli_derives_every_authority_input_from_daemon() { - for (daemon_id, expected_floor) in [ - ( - "yggdrasil-codex-connector", - CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION, - ), - ("yggdrasil-ghostlight", GHOSTLIGHT_MINIMUM_SOURCE_REVISION), - ] { - let options = Options::parse( - [ - "validate-minimum-source-revision", - "--daemon", - daemon_id, - "--source-revision", - expected_floor, - ] - .into_iter() - .map(ToString::to_string), - ) - .unwrap(); - let Mode::MinimumSourceRevisionValidation(validation) = options.mode else { - panic!("expected minimum-source validation posture"); - }; - assert_eq!(validation.daemon_id, daemon_id); - assert_eq!(validation.source_revision, expected_floor); - } - - for extra in ["--store", "--repository-full-name", "--upstream-ref"] { - let error = Options::parse( - [ - "validate-minimum-source-revision", - "--daemon", - "yggdrasil-ghostlight", - "--source-revision", - GHOSTLIGHT_MINIMUM_SOURCE_REVISION, - extra, - "caller-selected", - ] - .into_iter() - .map(ToString::to_string), - ) - .unwrap_err() - .to_string(); - assert!(error.contains("accepts only --daemon and --source-revision")); - } - } - - #[test] - fn compiled_minimum_source_policy_is_exactly_two_fixed_authorities() { - assert_eq!(COMPILED_MINIMUM_SOURCE_REVISION_POLICIES.len(), 2); - let connector = - compiled_minimum_source_revision_policy_for_daemon("yggdrasil-codex-connector") - .unwrap(); - assert_eq!(connector.repository_full_name, "GameCult/CodexConnector"); - assert_eq!( - connector.canonical_repository_url, - "https://github.com/GameCult/CodexConnector.git" - ); - assert_eq!( - connector.upstream_ref, - "refs/heads/codex/ghostlight-release-binding" - ); - assert_eq!( - connector.canonical_fetched_ref, - "refs/remotes/origin/codex/ghostlight-release-binding" - ); - assert_eq!(connector.repository_path, "/srv/build/CodexConnector"); - assert_eq!( - connector.minimum_source_revision, - "68fe94b9475823de92ed766a97ea962d48723708" - ); - - let ghostlight = - compiled_minimum_source_revision_policy_for_daemon("yggdrasil-ghostlight").unwrap(); - assert_eq!(ghostlight.repository_full_name, "GameCult/Ghostlight"); - assert_eq!( - ghostlight.canonical_repository_url, - "https://github.com/GameCult/Ghostlight.git" - ); - assert_eq!( - ghostlight.upstream_ref, - "refs/heads/codex/ghostlight-dungeon-mvp" - ); - assert_eq!( - ghostlight.canonical_fetched_ref, - "refs/remotes/origin/codex/ghostlight-dungeon-mvp" - ); - assert_eq!(ghostlight.repository_path, "/srv/build/Ghostlight"); - assert_eq!( - ghostlight.minimum_source_revision, - "6bb686981050e048c070376e7f7ac86d177e8f18" - ); - assert!(compiled_minimum_source_revision_policy_for_daemon("yggdrasil-odin").is_none()); - } - - fn runtime_admission_cli_args(command: &str) -> Vec { - vec![ - command.into(), - "--daemon".into(), - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(), - "--deployment-request-id".into(), - "deploy-connector-1".into(), - "--release-id".into(), - "release-connector-1".into(), - "--release-witness-sha256".into(), - format!("sha256-{}", "a".repeat(64)), - "--source-revision".into(), - "b".repeat(40), - "--activation-witness-sha256".into(), - format!("sha256-{}", "c".repeat(64)), - "--runtime-process-id".into(), - "4101".into(), - "--runtime-process-starttime-ticks".into(), - "98765".into(), - ] - } - - #[test] - fn parser_keeps_runtime_admission_issuance_on_the_fixed_probation_lane() { - let mut args = runtime_admission_cli_args("validate-health-admission"); - args.extend([ - "--store".into(), - "/var/lib/gamecult/idunn/idunn.keepalive.cc".into(), - "--write-admission-witness".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let options = Options::parse(args.into_iter()).unwrap(); - let Mode::HealthAdmissionValidation(validation) = options.mode else { - panic!("expected health admission validation posture"); - }; - assert_eq!( - validation.write_admission_witness.as_deref(), - Some(Path::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH)) - ); - assert!(validation.activation_witness_sha256.is_some()); - assert_eq!( - validation.runtime_process, - Some(RuntimeProcessObservation { - process_id: 4_101, - starttime_ticks: 98_765, - }) - ); - - let mut ghostlight = runtime_admission_cli_args("validate-health-admission"); - ghostlight[2] = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); - ghostlight.extend([ - "--write-admission-witness".into(), - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let ghostlight = Options::parse(ghostlight.into_iter()).unwrap(); - let Mode::HealthAdmissionValidation(ghostlight) = ghostlight.mode else { - panic!("expected Ghostlight health admission validation posture"); - }; - assert_eq!( - ghostlight.write_admission_witness.as_deref(), - Some(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)) - ); - - let mut missing_activation = runtime_admission_cli_args("validate-health-admission"); - missing_activation.drain(11..13); - missing_activation.extend([ - "--write-admission-witness".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let error = Options::parse(missing_activation.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("requires --activation-witness-sha256")); - - let mut missing_process = runtime_admission_cli_args("validate-health-admission"); - missing_process.drain(13..17); - missing_process.extend([ - "--write-admission-witness".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let error = Options::parse(missing_process.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("requires --runtime-process-id")); - - let mut caller_selected = runtime_admission_cli_args("validate-health-admission"); - caller_selected.extend([ - "--write-admission-witness".into(), - "/tmp/traffic-admission.cc".into(), - ]); - let error = Options::parse(caller_selected.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("two exact supported absolute paths")); - - let mut other_daemon = runtime_admission_cli_args("validate-health-admission"); - other_daemon[2] = "yggdrasil-other-daemon".into(); - other_daemon.extend([ - "--write-admission-witness".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let error = Options::parse(other_daemon.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("daemon/path pair is unsupported")); - - let error = - Options::parse(runtime_admission_cli_args("validate-health-admission").into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("belongs only to runtime traffic issuance or validation")); - } - - #[test] - fn runtime_admission_policy_is_exactly_two_non_substitutable_pairs() { - assert_eq!( - runtime_admission_policy_for_path(Path::new( - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH - )) - .unwrap(), - ( - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_GROUP - ) - ); - assert_eq!( - runtime_admission_policy_for_path(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)) - .unwrap(), - ( - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_GROUP - ) - ); - assert!( - require_supported_runtime_admission_pair( - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) - ) - .is_err() - ); - assert!( - require_supported_runtime_admission_pair( - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY, - Path::new(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) - ) - .is_err() - ); - assert!( - runtime_admission_policy_for_path(Path::new( - "/etc/gamecult/ghostlight-dungeon/runtime/./traffic-admission.cc" - )) - .is_err() - ); - assert_eq!( - runtime_admission_lock_path(Path::new(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)), - PathBuf::from(format!( - "{}.lock", - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH - )) - ); - assert_eq!( - runtime_admission_custody_paths(Path::new( - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH - )), - [ - PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH), - PathBuf::from(format!( - "{}.lock", - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH - )), - ] - ); - } - - #[cfg(unix)] - #[test] - fn sealed_runtime_admission_custody_refuses_a_missing_sibling_lock() { - let root = tempfile::tempdir().unwrap(); - let data_path = root.path().join("traffic-admission.cc"); - let lock_path = runtime_admission_lock_path(&data_path); - let error = validate_runtime_admission_leaf(&lock_path, 0, "test", true) - .unwrap_err() - .to_string(); - - assert!(error.contains("inspecting runtime traffic admission")); - assert!(error.contains("traffic-admission.cc.lock")); - } - - #[test] - fn parser_exposes_read_only_runtime_admission_validation() { - let mut args = runtime_admission_cli_args("validate-runtime-admission"); - args.extend([ - "--input".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let options = Options::parse(args.into_iter()).unwrap(); - let Mode::RuntimeAdmissionValidation(validation) = options.mode else { - panic!("expected runtime admission validation posture"); - }; - assert_eq!( - validation.input, - PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH) - ); - assert_eq!( - validation.daemon_id, - IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY - ); - assert_eq!(validation.runtime_process.process_id, 4_101); - assert_eq!(validation.runtime_process.starttime_ticks, 98_765); - - let mut ghostlight = runtime_admission_cli_args("validate-runtime-admission"); - ghostlight[2] = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); - ghostlight.extend([ - "--input".into(), - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let ghostlight = Options::parse(ghostlight.into_iter()).unwrap(); - let Mode::RuntimeAdmissionValidation(ghostlight) = ghostlight.mode else { - panic!("expected Ghostlight runtime admission validation posture"); - }; - assert_eq!( - ghostlight.daemon_id, - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY - ); - assert_eq!( - ghostlight.input, - PathBuf::from(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH) - ); - - let mut substituted = runtime_admission_cli_args("validate-runtime-admission"); - substituted.extend([ - "--input".into(), - GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let error = Options::parse(substituted.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("daemon/path pair is unsupported")); - - let mut missing_process = runtime_admission_cli_args("validate-runtime-admission"); - missing_process.drain(13..17); - missing_process.extend([ - "--input".into(), - CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH.into(), - ]); - let error = Options::parse(missing_process.into_iter()) - .unwrap_err() - .to_string(); - assert!(error.contains("requires --runtime-process-id")); - assert!(help_text().contains("validate-runtime-admission")); - assert!(help_text().contains(GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_PATH)); - assert!(help_text().contains(IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY)); - } - - fn target(default_failure_state: &str, deploy_command: Option<&str>) -> DaemonTarget { - DaemonTarget { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - health_contract: health_contract("test.contract", default_failure_state), - deploy_command: deploy_command.map(ToString::to_string), - restart_command: Some("restart test".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - } - } - - #[test] - fn target_catalog_requires_health_contract() { - let mut target = target("failed", None); - target.health_contract.id = String::new(); - - let error = validate_targets(&[target]).unwrap_err().to_string(); - - assert!(error.contains("has no health contract")); - } - - #[test] - fn stale_deployment_contract_requires_deploy_authority() { - let error = validate_targets(&[target("stale-deployment", None)]) - .unwrap_err() - .to_string(); - - assert!(error.contains("has no deploy command")); - } - - #[test] - fn parser_rejects_removed_health_command_lane() { - let error = Options::parse( - [ - "--daemon", - "test-daemon", - "--health-command", - "health-test.cmd", - ] - .into_iter() - .map(ToString::to_string), - ) - .unwrap_err() - .to_string(); - - assert!(error.contains("--health-command has been removed")); - assert!(error.contains(CULTNET_RUDP_PROTOCOL_ID)); - } - - #[test] - fn parser_does_not_default_rudp_health_ingress_to_localhost() { - let options = Options::parse( - ["--daemon", "test-daemon"] - .into_iter() - .map(ToString::to_string), - ) - .unwrap(); - - assert_eq!(options.common.rudp_health_bind, None); - } - - #[test] - fn parser_accepts_explicit_rudp_health_ingress() { - let options = Options::parse( - [ - "--daemon", - "test-daemon", - "--rudp-health-bind", - "0.0.0.0:17870", - ] - .into_iter() - .map(ToString::to_string), - ) - .unwrap(); - - assert_eq!( - options.common.rudp_health_bind, - Some("0.0.0.0:17870".parse().unwrap()) - ); - } - - #[test] - fn swarm_surgery_plan_moves_on_after_muninn_family_is_upgraded() { - let starfire_muninn = DaemonTarget { - daemon_id: "starfire-muninn".to_string(), - verse_id: "starfire.local".to_string(), - name: "Starfire Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-local-telemetry-and-quest-access", - "degraded", - ), - deploy_command: None, - restart_command: Some("restart-starfire-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let nightwing_muninn = DaemonTarget { - daemon_id: "nightwing-muninn".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-remote-telemetry-and-move-hid", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-nightwing-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let raven_muninn = DaemonTarget { - daemon_id: "muninn".to_string(), - verse_id: "raven.local".to_string(), - name: "Raven Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-remote-telemetry-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let odin = DaemonTarget { - daemon_id: "odin".to_string(), - verse_id: "starfire.local".to_string(), - name: "Odin".to_string(), - health_contract: health_contract("odin.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: Some("restart-odin.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let stonks = DaemonTarget { - daemon_id: "stonks".to_string(), - verse_id: "starfire.local".to_string(), - name: "Stonks".to_string(), - health_contract: health_contract("stonks.cultnet-rudp-market-health", "failed"), - deploy_command: None, - restart_command: Some("restart-stonks.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let weksa = DaemonTarget { - daemon_id: "weksa".to_string(), - verse_id: "starfire.local".to_string(), - name: "Weksa".to_string(), - health_contract: health_contract("weksa.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: Some("restart-weksa.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 60, - }; - let voidbot = DaemonTarget { - daemon_id: "voidbot".to_string(), - verse_id: "starfire.local".to_string(), - name: "VoidBot local stack".to_string(), - health_contract: health_contract("voidbot.cultnet-rudp-stack-health", "failed"), - deploy_command: None, - restart_command: Some("restart-voidbot.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 60, - }; - let yggdrasil_gjallar = DaemonTarget { - daemon_id: "yggdrasil-gjallar".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Gjallar Eve surface compositor".to_string(), - health_contract: health_contract("gjallar.cultnet-rudp-composition-health", "failed"), - deploy_command: Some("idunn-yggdrasil deploy gjallar".to_string()), - restart_command: Some("idunn-yggdrasil restart gjallar".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let mimir = DaemonTarget { - daemon_id: "mimir-eve-dashboard".to_string(), - verse_id: "starfire.local".to_string(), - name: "Mimir Eve dashboard".to_string(), - health_contract: health_contract("mimir.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: None, - release: None, - enabled: true, - interval_seconds: 30, - }; - let nightwing_eve_dashboard = DaemonTarget { - daemon_id: "nightwing-eve-dashboard".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Eve dashboard".to_string(), - health_contract: health_contract( - "nightwing.cultnet-rudp-eve-dashboard-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-nightwing-eve-dashboard.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let nightwing_eve_browser_reference = DaemonTarget { - daemon_id: "nightwing-eve-browser-reference".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Eve browser reference".to_string(), - health_contract: health_contract( - "nightwing.cultnet-rudp-browser-reference-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-nightwing-eve-browser-reference.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let vili = DaemonTarget { - daemon_id: "vili".to_string(), - verse_id: "raven.local".to_string(), - name: "Vili".to_string(), - health_contract: health_contract("vili.cultnet-rudp-animation-health", "failed"), - deploy_command: None, - restart_command: Some("restart-vili.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let raven_sleipnir = DaemonTarget { - daemon_id: "raven-sleipnir".to_string(), - verse_id: "raven.local".to_string(), - name: "Raven Sleipnir".to_string(), - health_contract: health_contract("sleipnir.cultnet-rudp-input-mirror-health", "failed"), - deploy_command: Some("deploy-raven-sleipnir.ps1".to_string()), - restart_command: Some("restart-raven-sleipnir.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let yggdrasil_streampixels = DaemonTarget { - daemon_id: "yggdrasil-streampixels".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil StreamPixels".to_string(), - health_contract: health_contract("streampixels.cultnet-rudp-service-health", "failed"), - deploy_command: Some("deploy-yggdrasil-streampixels.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - let yggdrasil_heimdall = DaemonTarget { - daemon_id: "yggdrasil-heimdall".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Heimdall".to_string(), - health_contract: health_contract("heimdall.cultnet-rudp-provider-health", "failed"), - deploy_command: Some("deploy-yggdrasil-heimdall.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - let yggdrasil_repixelizer = DaemonTarget { - daemon_id: "yggdrasil-repixelizer".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil repixelizer".to_string(), - health_contract: health_contract("repixelizer.cultnet-rudp-service-health", "failed"), - deploy_command: Some("deploy-yggdrasil-repixelizer.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - - let targets = vec![ - odin, - mimir, - nightwing_eve_dashboard.clone(), - nightwing_eve_browser_reference.clone(), - vili.clone(), - raven_sleipnir.clone(), - stonks, - weksa.clone(), - voidbot.clone(), - yggdrasil_gjallar.clone(), - starfire_muninn.clone(), - nightwing_muninn.clone(), - raven_muninn.clone(), - yggdrasil_streampixels.clone(), - yggdrasil_heimdall.clone(), - yggdrasil_repixelizer.clone(), - ]; - let plans = daemon_surgery_plans(&targets, "unix:100"); - let plan = swarm_surgery_plan("starfire-local", &targets, &plans, "unix:100"); - - assert_eq!(plan.plan_id, "swarm-surgery:starfire-local"); - assert_eq!(plan.status, "active-transport-migration"); - assert_eq!(plan.next_target, "none"); - assert!(plan.current_phase.contains( - "Yggdrasil Gjallar now consumes Odin's accepted gamecult.eve.surface_state snapshot over CultNet/RUDP" - )); - assert!( - plan.cut_line.contains( - "live yggdrasil-heimdall publishes heimdall.cultnet-rudp-provider-health" - ) - ); - assert!(plan.cut_line.contains("GameCult\\Vili")); - assert!(plan.cut_line.contains("configured health route")); - assert!(!plan.cut_line.contains("10.77.0.4")); - assert!(plan.cut_line.contains("Nightwing Muninn")); - assert!(plan.cut_line.contains("voidbot-swarm-state.cc")); - assert!( - plan.cut_line - .contains("/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc") - ); - assert!(plan.cut_line.contains("Starfire Muninn")); - assert!(plan.cut_line.contains("configured health contract")); - assert!(!plan.cut_line.contains("127.0.0.1")); - assert!(!plan.cut_line.contains("10.77.0.3")); - assert!(plan.cut_line.contains("GameCult-Muninn-Activate")); - assert!(plan.cut_line.contains("GameCult-Muninn-VideoProof")); - assert!( - plan.cut_line - .contains("hidden PowerShell entrypoints directly") - ); - assert!( - plan.cut_line - .contains("archived instead of command-probe health paths") - ); - assert!(plan.cut_line.contains("yggdrasil-streampixels")); - assert!(plan.cut_line.contains("configured Yggdrasil health route")); - assert!(!plan.cut_line.contains("10.77.0.1")); - assert!( - plan.cut_line - .contains("heimdall.cultnet-rudp-provider-health") - ); - assert!( - plan.cut_line - .contains("/srv/heimdall/cultcache/heimdall.service.cc") - ); - assert!( - plan.cut_line - .contains("repixelizer.cultnet-rudp-service-health") - ); - assert!( - plan.cut_line - .contains("/srv/repixelizer/cultcache/repixelizer.service.cc") - ); - assert!(plan.cut_line.contains("Live Yggdrasil Gjallar publishes")); - assert!( - plan.cut_line - .contains("/var/lib/gamecult/gjallar/gjallar.service.cc") - ); - assert!( - plan.cut_line - .contains("Live Mimir Eve dashboard publishes CultMesh state") - ); - assert!( - plan.cut_line - .contains("/var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp") - ); - assert!( - plan.cut_line - .contains("/var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc") - ); - assert!( - plan.cut_line - .contains("live Nightwing Eve browser reference publishes a boundary store") - ); - assert!(plan.cut_line.contains( - "/var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc" - )); - assert!(plan.cut_line.contains("surface:gamecult.network.status")); - assert!(plan.cut_line.contains("configured Gjallar Odin endpoint")); - assert!(!plan.cut_line.contains("nightwing-gjallar")); - assert!(!plan.cut_line.contains("renderer-only lowering")); - assert!(plan.verification_layer.contains("CultMesh keepalive store")); - assert!( - plan.invariants - .iter() - .any(|invariant| invariant.contains("cultnet.transport.rudp.v0")) - ); - assert!( - plan.invariants - .iter() - .any(|invariant| invariant.contains("Raven") - && invariant.contains("background-only") - && invariant.contains("visible terminal")) - ); - assert!( - plan.invariants - .iter() - .any(|invariant| invariant.contains("Raven") - && invariant.contains("Task Scheduler") - && invariant.contains(".cmd")) - ); - - let raven_plan = daemon_surgery_plan(&raven_muninn, "unix:100"); - assert_eq!( - raven_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - raven_plan - .current_mechanism - .contains("configured Idunn health endpoint") - ); - assert!(raven_plan.current_mechanism.contains("GameCult-Muninn")); - assert!( - raven_plan - .current_mechanism - .contains("C:\\Meta\\Odin\\state\\muninn.telemetry.cc") - ); - assert!( - raven_plan - .current_mechanism - .contains("C:\\Meta\\Odin\\state\\muninn.activate.cc") - ); - assert!(raven_plan.current_mechanism.contains("platform defaults")); - assert!(raven_plan.cut_line.contains("daemon-owned witness")); - assert!( - raven_plan - .cut_line - .contains("health-muninn.cmd remains archived") - ); - assert!(raven_plan.steps.iter().any( - |step| step.contains("GameCult-Muninn-VideoProof") && step.contains("wscript.exe") - )); - assert!(raven_plan.steps.iter().any( - |step| step.contains("PowerShell entrypoints directly") && step.contains("cmdPath") - )); - assert!(raven_plan.steps.iter().any(|step| { - step.contains("muninn.activate.cc") && step.contains("activation path") - })); - assert!(raven_plan.steps.iter().any(|step| { - step.contains("auto-claiming PS Move hosts") && step.contains("explicit Move flags") - })); - assert!(raven_plan.steps.iter().any(|step| { - step.contains("--idunn-rudp-health from the configured Idunn health endpoint") - && step.contains("muninn.cultnet-rudp-remote-telemetry-health") - })); - assert!(!raven_plan.current_mechanism.contains("192.168.1.66:17870")); - assert!( - !raven_plan - .steps - .iter() - .any(|step| step.contains("192.168.1.66:17870")) - ); - assert!(raven_plan.blockers.is_empty()); - - let starfire_plan = daemon_surgery_plan(&starfire_muninn, "unix:100"); - assert_eq!( - starfire_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - starfire_plan - .current_mechanism - .contains("configured Idunn health endpoint") - ); - assert!( - starfire_plan - .current_mechanism - .contains("archives a corrupt CultCache store") - ); - assert!( - starfire_plan - .current_mechanism - .contains("C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc") - ); - assert!(starfire_plan.cut_line.contains("daemon-owned witness")); - assert!( - starfire_plan - .cut_line - .contains("health-starfire-muninn.cmd stays archived") - ); - assert!(starfire_plan.steps.iter().any( - |step| step.contains("starfire.muninn.telemetry.cc") && step.contains(".lock") - )); - - let nightwing_plan = daemon_surgery_plan(&nightwing_muninn, "unix:100"); - assert_eq!( - nightwing_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - nightwing_plan - .current_mechanism - .contains("configured Idunn health endpoint") - ); - assert!( - nightwing_plan - .current_mechanism - .contains("configured RUDP route") - ); - assert!( - nightwing_plan - .current_mechanism - .contains("/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc") - ); - assert!( - nightwing_plan - .current_mechanism - .contains("-DiscoverMoveState, -ClaimUsbMoves, or explicit -MoveState values") - ); - assert!(nightwing_plan.cut_line.contains("daemon-owned witness")); - assert!( - nightwing_plan - .cut_line - .contains("health-nightwing-muninn.cmd stays archived") - ); - assert!(nightwing_plan.steps.iter().any(|step| { - step.contains("/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc") - && step.contains("provider advertisement") - })); - assert!( - nightwing_plan - .steps - .iter() - .any(|step| step.contains("restart-nightwing-muninn.ps1") - && step.contains("long-running serve body")) - ); - assert!(nightwing_plan.steps.iter().any(|step| { - step.contains("explicit -DiscoverMoveState/-ClaimUsbMoves") - && step.contains("Move evidence/runtime arguments") - })); - - let weksa_plan = daemon_surgery_plan(&weksa, "unix:100"); - assert_eq!( - weksa_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!(weksa_plan.current_mechanism.contains("command_boundary")); - assert!( - weksa_plan - .current_mechanism - .contains("typed CultMesh/CultNet command documents") - ); - assert!( - weksa_plan - .cut_line - .contains("local command health must not satisfy daemon truth") - ); - assert!(weksa_plan.steps.iter().any(|step| { - step.contains("speech_provider.mimo.voicedesign") - && step.contains("typed CultMesh/CultNet") - })); - - let voidbot_plan = daemon_surgery_plan(&voidbot, "unix:100"); - assert_eq!( - voidbot_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - voidbot_plan - .current_mechanism - .contains("voidbot.cultnet-rudp-stack-health") - ); - assert!( - voidbot_plan - .current_mechanism - .contains("voidbot-swarm-state.cc") - ); - assert!( - voidbot_plan - .cut_line - .contains("operations probe as a debug witness") - ); - - let gjallar_plan = daemon_surgery_plan(&yggdrasil_gjallar, "unix:100"); - assert_eq!( - gjallar_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - gjallar_plan - .current_mechanism - .contains("runs headless beside Odin and Idunn") - ); - assert!( - gjallar_plan - .cut_line - .contains("Retire the Nightwing service") - ); - assert!( - gjallar_plan - .current_mechanism - .contains("accepted provider snapshot") - ); - - let nightwing_eve_dashboard_plan = - daemon_surgery_plan(&nightwing_eve_dashboard, "unix:100"); - assert_eq!( - nightwing_eve_dashboard_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - nightwing_eve_dashboard_plan - .current_mechanism - .contains("nightwing.cultnet-rudp-eve-dashboard-health") - ); - assert!( - nightwing_eve_dashboard_plan - .current_mechanism - .contains("/var/lib/gamecult/eve-dashboard/cultmesh/eve-dashboard.ccmp") - ); - assert!( - nightwing_eve_dashboard_plan - .current_mechanism - .contains("/var/lib/gamecult/eve-dashboard/cultcache/eve-dashboard.service.cc") - ); - assert!( - nightwing_eve_dashboard_plan - .steps - .iter() - .any(|step| step.contains("Mimir.EveDashboard systemd process")) - ); - - let nightwing_eve_browser_reference_plan = - daemon_surgery_plan(&nightwing_eve_browser_reference, "unix:100"); - assert_eq!( - nightwing_eve_browser_reference_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - nightwing_eve_browser_reference_plan - .current_mechanism - .contains("nightwing.cultnet-rudp-browser-reference-health") - ); - assert!( - nightwing_eve_browser_reference_plan - .current_mechanism - .contains("/var/lib/gamecult/eve-browser-reference/cultcache/eve-browser-reference.service.cc") - ); - assert!( - nightwing_eve_browser_reference_plan - .steps - .iter() - .any(|step| step.contains("Mimir.EveBrowserReference service process")) - ); - - let vili_plan = daemon_surgery_plan(&vili, "unix:100"); - assert_eq!( - vili_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!(vili_plan.current_mechanism.contains("vili.service.cc")); - assert!(vili_plan.current_mechanism.contains("GameCult\\Vili")); - assert!( - vili_plan - .steps - .iter() - .any(|step| step.contains("command_boundary")) - ); - assert!( - vili_plan - .steps - .iter() - .any(|step| step.contains("0.0.0.0:17870") && step.contains("Raven")) - ); - assert!(vili_plan.blockers.is_empty()); - - let streampixels_plan = daemon_surgery_plan(&yggdrasil_streampixels, "unix:100"); - assert_eq!( - streampixels_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - streampixels_plan - .current_mechanism - .contains("requires STREAMPIXELS_IDUNN_RUDP_HEALTH") - ); - assert!( - streampixels_plan - .current_mechanism - .contains("configured RUDP route") - ); - assert!( - !streampixels_plan - .current_mechanism - .contains("10.77.0.2:17870") - ); - assert!( - streampixels_plan - .current_mechanism - .contains("/srv/streampixels/env/service.env") - ); - assert!( - streampixels_plan - .cut_line - .contains("deployment/debug witnesses") - ); - assert!( - streampixels_plan - .steps - .iter() - .any(|step| step.contains("CultLib cultnet-ts/cultcache-ts snapshot")) - ); - assert!( - streampixels_plan - .steps - .iter() - .any(|step| step.contains("serial pnpm workspace build")) - ); - - let heimdall_plan = daemon_surgery_plan(&yggdrasil_heimdall, "unix:100"); - assert_eq!( - heimdall_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - heimdall_plan - .current_mechanism - .contains("/srv/heimdall/cultcache/heimdall.service.cc") - ); - assert!( - heimdall_plan - .steps - .iter() - .any(|step| step.contains("CultLib cultnet-ts/cultcache-ts snapshot")) - ); - assert!( - heimdall_plan - .steps - .iter() - .any(|step| step.contains("product web discovery") - && step.contains("daemon truth")) - ); - - let repixelizer_plan = daemon_surgery_plan(&yggdrasil_repixelizer, "unix:100"); - assert_eq!( - repixelizer_plan.status, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - repixelizer_plan - .current_mechanism - .contains("/srv/repixelizer/cultcache/repixelizer.service.cc") - ); - assert!( - repixelizer_plan - .steps - .iter() - .any(|step| step.contains("CultLib cultcache-py snapshot")) - ); - } - - #[test] - fn voidbot_release_authority_is_yggdrasil_local_only() { - let yggdrasil = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .expect("yggdrasil-local targets"); - let voidbot = yggdrasil - .iter() - .find(|target| target.daemon_id == "yggdrasil-voidbot") - .expect("Yggdrasil VoidBot target"); - - assert_eq!(voidbot.verse_id, "yggdrasil.local"); - let deploy = voidbot - .deploy_command - .as_deref() - .expect("VoidBot deploy command"); - assert!(deploy.starts_with("sudo -n /usr/local/libexec/idunn-yggdrasil deploy voidbot")); - assert!(deploy.contains("$IDUNN_SOURCE_COMMIT")); - assert!(deploy.contains("$BIFROST_RELEASE_AUTHORITY_SHA256")); - assert!(deploy.contains("$IDUNN_DEPLOYMENT_REQUEST_ID")); - assert_eq!( - voidbot.restart_command.as_deref(), - Some("sudo -n /usr/local/libexec/idunn-yggdrasil restart voidbot") - ); - let release = voidbot.release.as_ref().expect("VoidBot release target"); - assert_eq!(release.repo_path, PathBuf::from("/srv/build/VoidBot")); - assert_eq!(release.upstream_remote, "origin"); - assert_eq!(release.upstream_branch, "main"); - assert!(!release.requires_bifrost_authority); - - let starfire = swarm_targets(&SwarmOptions { - profile: "starfire-local".to_string(), - repo_root: PathBuf::from("E:/Projects/Odin"), - }) - .expect("starfire-local targets"); - assert!( - starfire - .iter() - .all(|target| target.daemon_id != "voidbot" - && target.daemon_id != "yggdrasil-voidbot") - ); - } - - #[test] - fn epiphany_yggdrasil_target_uses_source_change_authority_and_exact_witness() { - let yggdrasil = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .expect("yggdrasil-local targets"); - let epiphany = yggdrasil - .iter() - .find(|target| target.daemon_id == "yggdrasil-epiphany") - .expect("Yggdrasil Epiphany target"); - - assert_eq!(epiphany.verse_id, "yggdrasil.local"); - assert_eq!( - epiphany.health_contract.id, - "epiphany.cultnet-rudp-runtime-health" - ); - assert_eq!(epiphany.health_contract.default_failure_state, "failed"); - assert_eq!(epiphany.interval_seconds, 300); - assert_eq!( - epiphany.restart_command.as_deref(), - Some("sudo -n /usr/local/libexec/idunn-yggdrasil restart epiphany") - ); - let deploy = epiphany - .deploy_command - .as_deref() - .expect("Epiphany deploy command"); - assert!(deploy.starts_with("sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany")); - assert!(deploy.contains("$IDUNN_SOURCE_COMMIT")); - assert!(deploy.contains("$IDUNN_DEPLOYMENT_REQUEST_ID")); - assert!(!deploy.contains("$IDUNN_REPOSITORY_FULL_NAME")); - assert!(!deploy.contains("$IDUNN_UPSTREAM_REF")); - assert!(!deploy.contains("$IDUNN_REQUIRES_BIFROST_AUTHORITY")); - - let release = epiphany.release.as_ref().expect("Epiphany release target"); - assert_eq!(release.repo, "Epiphany"); - assert_eq!(release.repository_full_name, "GameCult/Epiphany"); - assert_eq!(release.repo_path, PathBuf::from("/srv/build/Epiphany")); - assert_eq!(release.upstream_remote, "origin"); - assert_eq!(release.upstream_branch, "codex/epiphany-shakedown-live"); - assert_eq!(release.rollout_strategy, "restart-after-verified-build"); - assert_eq!(release.state_migration_command, None); - assert_eq!(release.zero_downtime_capability, "restart-required"); - assert_eq!( - release.deployed_revision_witness, - Some(PathBuf::from("/srv/epiphany/deploy/deployment.env")) - ); - assert!(!release.requires_bifrost_authority); - assert_eq!( - release.source_change_pathspecs, - vec![ - ".", - ":(exclude)docs/**", - ":(exclude)notes/**", - ":(exclude)state/ledgers.msgpack", - ":(exclude)state/map.yaml", - ":(exclude)state/scratch*", - ":(exclude)*.md", - ] - ); - - let capstone_17 = yggdrasil - .iter() - .find(|target| target.daemon_id == "yggdrasil-epiphany-capstone-17") - .expect("Yggdrasil Epiphany Capstone 17 target"); - assert!(!capstone_17.enabled, "Capstone 17 must never auto-actuate"); - assert!(capstone_17.restart_command.is_none()); - assert_eq!( - capstone_17.deploy_command.as_deref(), - Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" - ) - ); - let capstone_17_release = capstone_17 - .release - .as_ref() - .expect("Capstone 17 release target"); - assert_eq!( - capstone_17_release.rollout_strategy, - "fresh-container-capstone" - ); - assert_eq!( - capstone_17_release.artifact_strategy, - "immutable-docker-image-id" - ); - assert!(capstone_17_release.source_change_pathspecs.is_empty()); - assert_eq!( - capstone_17_release.artifact_witness_root, - Some(PathBuf::from("/srv/epiphany/artifacts")) - ); - assert_eq!( - capstone_17_release.deployed_revision_witness, - Some(PathBuf::from("/srv/epiphany/capstone-17/deployment.env")) - ); - - for legacy in yggdrasil - .iter() - .filter(|target| { - !matches!( - target.daemon_id.as_str(), - "yggdrasil-bifrost-persona-feedback" - ) - }) - .filter_map(|target| target.release.as_ref()) - { - assert!( - !legacy.requires_bifrost_authority, - "{} must stay on its explicit source authority posture", - legacy.repo - ); - } - } - - #[test] - fn container_artifact_witness_binds_exact_source_and_image_id() { - let root = tempfile::tempdir().unwrap(); - let source = "a".repeat(40); - let image_id = format!("sha256:{}", "b".repeat(64)); - fs::write( - root.path().join(format!("{source}.env")), - format!( - "schema=idunn.epiphany_image_artifact.v1\nsource_commit={source}\nimage_id={image_id}\n" - ), - ) - .unwrap(); - assert_eq!( - read_container_artifact_witness(root.path(), &source).unwrap(), - image_id - ); - assert!(read_container_artifact_witness(root.path(), &"c".repeat(40)).is_err()); - } - - #[test] - fn yggdrasil_service_and_root_actuator_hold_the_privileged_release_boundary() { - let unit = include_str!("../../../scripts/linux/idunn-yggdrasil.service"); - let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); - let sudoers = include_str!("../../../scripts/linux/idunn-yggdrasil.sudoers"); - - assert!(unit.contains( - "--release-authority-store /srv/bifrost/state/repository-release-authority.cc" - )); - assert!(unit.contains("--command-timeout-seconds 21600")); - assert!(unit.contains("--repo-root /var/lib/gamecult/idunn")); - assert!(!unit.contains("--repo-root /srv/odin/source")); - assert!(unit.contains( - "--deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc" - )); - assert!(unit.contains("ReadOnlyPaths=/var/lib/gamecult/idunn-authority")); - assert!( - !unit - .lines() - .any(|line| line == "ReadWritePaths=/var/lib/gamecult/idunn-authority") - ); - assert!(unit.contains("/etc/gamecult/codex-connector")); - assert!( - unit.split_ascii_whitespace() - .any(|entry| entry == "/etc/gamecult/ghostlight-dungeon") - ); - assert!( - !unit - .split_ascii_whitespace() - .any(|entry| entry == "/etc/gamecult/ghostlight") - ); - assert!(unit.contains("/etc/systemd/system/codex-connector.service")); - assert!(unit.contains("/etc/systemd/system/epiphany-swarm.service")); - assert!(unit.contains("/etc/systemd/system/ghostlight-dungeon.service")); - assert!(!unit.contains("ReadWritePaths=/etc ")); - assert!( - !unit.contains("--deployment-brake-store /var/lib/gamecult/idunn/deployment-brake.cc") - ); - assert!(!unit.contains("--trusted-epiphany-health-identity-store")); - assert!(actuator.contains("deploy:voidbot|deploy:epiphany|deploy:epiphany-capstone-17")); - assert!(actuator.contains("/usr/local/bin/idunn validate-release-authority")); - assert!(actuator.contains("bifrost-persona-feedback)")); - assert!(actuator.contains("requires_bifrost_authority=true")); - assert!(actuator.contains("repository_full_name=GameCult/Bifrost")); - assert!(actuator.contains("daemon_id=yggdrasil-bifrost-persona-feedback")); - assert!(actuator.contains("deployment-brake-status")); - assert!(actuator.contains("/usr/local/bin/idunn validate-minimum-source-revision")); - assert_eq!( - actuator - .matches("minimum_source_revision_required=true") - .count(), - 2 - ); - assert!(!actuator.contains(CODEX_CONNECTOR_MINIMUM_SOURCE_REVISION)); - assert!(!actuator.contains(GHOSTLIGHT_MINIMUM_SOURCE_REVISION)); - assert!(actuator.contains("--release-id \"$source_commit\"")); - assert!(actuator.contains("--deployment-id \"$deployment_request_id\"")); - assert!(actuator.contains("\"deploy:$daemon_id:\"?*|\"manual:redeploy:$daemon_id:\"?*")); - assert!(actuator.contains("require_root_owned_regular_path \"$manifest\"")); - assert!(actuator.contains("[ -L \"$component\" ]")); - assert!(actuator.contains("[ -f \"$component\" ]")); - assert!(actuator.contains("[ \"$owner\" = 0 ]")); - assert!(actuator.contains("group/world-writable")); - assert!(actuator.contains("exec 9<\"$brake_lock\"")); - assert!(actuator.contains("/usr/bin/flock --shared 9")); - assert!( - actuator.contains( - "generation_lock=/var/lib/gamecult/idunn-authority/idunn-generation.lock" - ) - ); - assert!(actuator.contains("require_root_owned_regular_path \"$generation_lock\"")); - assert!(actuator.contains("exec 8<\"$generation_lock\"")); - assert!(actuator.contains("/usr/bin/flock --shared 8")); - assert!(actuator.contains("run_manifest_while_holding_authority_locks")); - assert!(actuator.contains("/usr/bin/setsid \"$@\" 8<&- 9<&- &")); - assert!(!actuator.contains("/usr/bin/setsid \"$@\" 9<&- &")); - assert!(!actuator.contains("/usr/bin/setsid \"$@\" &")); - assert!(actuator.contains("kill -s \"$1\" \"-$manifest_pid\"")); - assert!(actuator.contains("wait \"$manifest_pid\"")); - assert!(actuator.contains("manifest_descendants_leaked")); - assert!(!actuator.contains("exec \"$manifest\"")); - assert_eq!( - actuator - .matches("require_root_owned_regular_path \"$compose\"") - .count(), - 3 - ); - assert!(actuator.contains("[ \"$#\" -eq 6 ]")); - assert!(actuator.contains("[ \"$#\" -eq 2 ]")); - assert!(!actuator.contains("target_requires_bifrost_authority")); - let policy_derivation = actuator - .find("requires_bifrost_authority=true") - .expect("root target policy"); - let derived_mode_branch = actuator - .find("case \"$requires_bifrost_authority\" in") - .expect("authority mode branch"); - let minimum_source_validation = actuator - .find("validate-minimum-source-revision") - .expect("minimum-source boundary"); - let deployment_brake_validation = actuator - .find("deployment-brake-status") - .expect("deployment-brake boundary"); - let manifest_execution = actuator - .find("run_manifest_while_holding_authority_locks") - .expect("manifest execution boundary"); - let generation_lock_acquisition = actuator - .rfind("acquire_idunn_generation_lock") - .expect("generation-lock acquisition"); - let target_dispatch = actuator - .find("case \"$action:$target\" in") - .expect("target dispatch"); - assert!( - policy_derivation < derived_mode_branch, - "root target policy must be derived before its mode is consumed" - ); - assert!( - deployment_brake_validation < minimum_source_validation - && minimum_source_validation < manifest_execution, - "compiled minimum-source proof must run after brake admission and before actuation" - ); - assert!( - generation_lock_acquisition < target_dispatch, - "generation lock must be acquired before any target dispatch" - ); - assert!(actuator.contains("IDUNN_SOURCE_COMMIT=\"$source_commit\"")); - assert!( - actuator.contains("BIFROST_RELEASE_AUTHORITY_SHA256=\"$release_authority_sha256\"") - ); - assert!( - actuator.contains("IDUNN_REQUIRES_BIFROST_AUTHORITY=\"$requires_bifrost_authority\"") - ); - assert!(sudoers.contains("Cmnd_Alias IDUNN_YGGDRASIL_DEPLOY")); - assert!(sudoers.contains("/usr/local/libexec/idunn-yggdrasil deploy ghostlight *")); - assert!(sudoers.contains("/usr/local/libexec/idunn-yggdrasil restart ghostlight")); - assert!(!sudoers.contains("/usr/local/libexec/idunn-yggdrasil *")); - } - - #[test] - fn yggdrasil_root_actuator_policy_matches_every_typed_release_target() { - let targets = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .expect("yggdrasil-local targets"); - let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); - let sudoers = include_str!("../../../scripts/linux/idunn-yggdrasil.sudoers"); - - for target in targets.iter().filter(|target| target.release.is_some()) { - let actuator_name = target - .daemon_id - .strip_prefix("yggdrasil-") - .expect("Yggdrasil daemon id"); - let release = target.release.as_ref().unwrap(); - let policy_marker = format!(" {actuator_name})"); - let policy = actuator - .split_once(policy_marker.as_str()) - .unwrap_or_else(|| panic!("missing root policy for {actuator_name}")) - .1 - .split_once(";;") - .expect("bounded root target policy") - .0; - let expected_daemon = format!("daemon_id={}", target.daemon_id); - let expected_repository = - format!("repository_full_name={}", release.repository_full_name); - let expected_ref = format!("upstream_ref=refs/heads/{}", release.upstream_branch); - let expected_authority = format!( - "requires_bifrost_authority={}", - release.requires_bifrost_authority - ); - let expected_deploy_case = format!("deploy:{actuator_name}"); - let expected_sudo_deploy = - format!("/usr/local/libexec/idunn-yggdrasil deploy {actuator_name} *"); - assert!(policy.contains(expected_daemon.as_str())); - assert!(policy.contains(expected_repository.as_str())); - assert!(policy.contains(expected_ref.as_str())); - assert!(policy.contains(expected_authority.as_str())); - if release.minimum_source_revision.is_some() { - assert!(policy.contains("minimum_source_revision_required=true")); - } else { - assert!(!policy.contains("minimum_source_revision_required=true")); - } - assert!(actuator.contains(expected_deploy_case.as_str())); - assert!(sudoers.contains(expected_sudo_deploy.as_str())); - if target.restart_command.is_some() { - let expected_sudo_restart = - format!("/usr/local/libexec/idunn-yggdrasil restart {actuator_name}"); - assert!(sudoers.contains(expected_sudo_restart.as_str())); - } - } - } - - #[test] - fn yggdrasil_runtime_catalog_binds_discovery_auth_and_dungeon_releases() { - let targets = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .expect("yggdrasil-local targets"); - let actuator = include_str!("../../../scripts/linux/idunn-yggdrasil"); - - for (daemon_id, repo, branch, witness, actuator_name, restart_on_missing) in [ - ( - "yggdrasil-odin", - "Odin", - "codex/ygg-idunn-independent-bootstrap", - "/srv/odin/deploy/deployment.env", - "odin", - true, - ), - ( - "yggdrasil-codex-connector", - "CodexConnector", - "codex/ghostlight-release-binding", - "/srv/codex-connector/deploy/deployment.env", - "codex-connector", - true, - ), - ( - "yggdrasil-ghostlight", - "Ghostlight", - "codex/ghostlight-dungeon-mvp", - "/srv/ghostlight/deploy/deployment.env", - "ghostlight", - true, - ), - ( - "yggdrasil-heimdall", - "Heimdall", - "main", - "/srv/heimdall/deploy/deployment.env", - "heimdall", - false, - ), - ( - "yggdrasil-gjallar", - "Gjallar", - "codex/yggdrasil-aggregate-daemon", - "/srv/gjallar/deploy/deployment.env", - "gjallar", - false, - ), - ] { - let target = targets - .iter() - .find(|target| target.daemon_id == daemon_id) - .unwrap_or_else(|| panic!("missing {daemon_id}")); - assert_eq!( - target.health_contract.restart_on_missing_publication, - restart_on_missing - ); - assert_eq!( - target.restart_command.as_deref(), - Some(format!( - "sudo -n /usr/local/libexec/idunn-yggdrasil restart {actuator_name}" - )) - .as_deref() - ); - let release = target.release.as_ref().expect("release target"); - assert_eq!(release.repo, repo); - assert_eq!(release.upstream_branch, branch); - assert_eq!( - release.deployed_revision_witness, - Some(PathBuf::from(witness)) - ); - assert!(!release.requires_bifrost_authority); - assert_eq!(release.source_change_pathspecs, DEPLOYABLE_SOURCE_PATHS); - assert!(target.enabled); - } - - assert!(actuator.contains( - "deploy:epiphany-capstone-17|deploy:odin|deploy:codex-connector|deploy:ghostlight" - )); - assert!( - actuator.contains( - "restart:epiphany|restart:odin|restart:codex-connector|restart:ghostlight" - ) - ); - assert!(actuator.contains("restart:heimdall")); - assert!(actuator.contains("deploy:heimdall")); - assert!(actuator.contains("restart:gjallar")); - assert!(actuator.contains("deploy:gjallar")); - } - - struct FakeReleaseStatePort { - fetch_error: Option, - desired: std::result::Result, - deployed: RefCell>, - } - - impl ReleaseStatePort for FakeReleaseStatePort { - fn fetch(&self, _release: &ReleaseTarget) -> Result<()> { - self.fetch_error - .as_ref() - .map_or(Ok(()), |error| Err(anyhow!(error.clone()))) - } - - fn desired_revision(&self, _release: &ReleaseTarget) -> Result { - self.desired.clone().map_err(|error| anyhow!(error)) - } - - fn deployed_revision(&self, _release: &ReleaseTarget) -> Result { - self.deployed - .borrow() - .clone() - .map_err(|error| anyhow!(error)) - } - } - - struct MissingWitnessReleaseStatePort { - desired: String, - } - - impl ReleaseStatePort for MissingWitnessReleaseStatePort { - fn fetch(&self, _release: &ReleaseTarget) -> Result<()> { - Ok(()) - } - - fn desired_revision(&self, _release: &ReleaseTarget) -> Result { - Ok(self.desired.clone()) - } - - fn deployed_revision(&self, _release: &ReleaseTarget) -> Result { - Err( - std::io::Error::new(std::io::ErrorKind::NotFound, "deployment witness is absent") - .into(), - ) - } - } - - fn release_desired(target: &DaemonTarget) -> IdunnDesiredDaemonRecord { - IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(target), - command_boundary_id: command_boundary_id(target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - } - } - - #[test] - fn matching_release_witness_does_not_override_daemon_health() { - let target = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .unwrap() - .into_iter() - .find(|target| target.daemon_id == "yggdrasil-voidbot") - .unwrap(); - let port = FakeReleaseStatePort { - fetch_error: None, - desired: Ok("abc123".to_string()), - deployed: RefCell::new(Ok("abc123".to_string())), - }; - - assert!( - evaluate_release_drift( - &target, - &release_desired(&target), - target.release.as_ref().unwrap(), - &port, - "unix:100" - ) - .is_none() - ); - } - - #[test] - fn release_drift_plans_one_deploy_then_matching_witness_stops_it() { - let target = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .unwrap() - .into_iter() - .find(|target| target.daemon_id == "yggdrasil-voidbot") - .unwrap(); - let desired = release_desired(&target); - let port = FakeReleaseStatePort { - fetch_error: None, - desired: Ok("new456".to_string()), - deployed: RefCell::new(Ok("old123".to_string())), - }; - let stale = evaluate_release_drift( - &target, - &desired, - target.release.as_ref().unwrap(), - &port, - "unix:100", - ) - .unwrap(); - assert_eq!(stale.state, "stale-deployment"); - assert!( - plan_keepalive(&desired, &stale, "unix:100") - .deployment_request - .is_some() - ); - assert!(verify_release_witness_current(target.release.as_ref().unwrap(), &port).is_err()); - - *port.deployed.borrow_mut() = Ok("new456".to_string()); - assert!(verify_release_witness_current(target.release.as_ref().unwrap(), &port).is_ok()); - assert!( - evaluate_release_drift( - &target, - &desired, - target.release.as_ref().unwrap(), - &port, - "unix:101" - ) - .is_none() - ); - } - - #[test] - fn absent_release_witness_is_first_deployment_not_dependency_failure() { - let target = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .unwrap() - .into_iter() - .find(|target| target.daemon_id == "yggdrasil-bifrost-persona-feedback") - .unwrap(); - let desired = release_desired(&target); - let health = evaluate_release_drift( - &target, - &desired, - target.release.as_ref().unwrap(), - &MissingWitnessReleaseStatePort { - desired: "abc123".to_string(), - }, - "unix:100", - ) - .unwrap(); - - assert_eq!(health.state, "stale-deployment"); - assert_eq!(health.transport, "idunn.release-not-deployed"); - assert!( - health - .detail - .contains("no deployed revision witness exists") - ); - assert!( - plan_keepalive(&desired, &health, "unix:100") - .deployment_request - .is_some() - ); - } - - #[test] - fn unavailable_fetch_or_witness_alarms_without_deploying() { - let target = swarm_targets(&SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }) - .unwrap() - .into_iter() - .find(|target| target.daemon_id == "yggdrasil-voidbot") - .unwrap(); - let desired = release_desired(&target); - let ports = [ - FakeReleaseStatePort { - fetch_error: Some("network unavailable".to_string()), - desired: Ok("new456".to_string()), - deployed: RefCell::new(Ok("old123".to_string())), - }, - FakeReleaseStatePort { - fetch_error: None, - desired: Ok("new456".to_string()), - deployed: RefCell::new(Err("witness missing".to_string())), - }, - ]; - for port in ports { - let unavailable = evaluate_release_drift( - &target, - &desired, - target.release.as_ref().unwrap(), - &port, - "unix:100", - ) - .unwrap(); - assert_eq!(unavailable.state, "dependency-unavailable"); - let plan = plan_keepalive(&desired, &unavailable, "unix:100"); - assert!(plan.deployment_request.is_none()); - assert!(plan.operator_alarm.is_some()); - } - } - - #[test] - fn starfire_and_nightwing_muninn_restart_when_health_publication_is_missing() { - let options = SwarmOptions { - profile: "starfire-local".to_string(), - repo_root: PathBuf::from("E:/Projects/Odin"), - }; - let targets = swarm_targets(&options).expect("starfire-local targets"); - - for daemon_id in ["starfire-muninn", "nightwing-muninn"] { - let target = targets - .iter() - .find(|target| target.daemon_id == daemon_id) - .expect("Muninn target"); - assert!(target.health_contract.restart_on_missing_publication); - - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: target.enabled, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(target), - command_boundary_id: command_boundary_id(target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - }; - let health = missing_daemon_published_health(target, &desired, "unix:101"); - let plan = plan_keepalive(&desired, &health, "unix:102"); - - assert_eq!(health.state, "failed"); - assert_eq!(plan.decision.action, "restart"); - assert_eq!( - plan.restart_request.expect("restart request").command, - target.restart_command.clone().expect("restart command") - ); - assert!(plan.operator_alarm.is_none()); - } - } - - #[test] - fn bifrost_persona_feedback_target_uses_daemon_health_and_idunn_deployment() { - let options = SwarmOptions { - profile: "yggdrasil-local".to_string(), - repo_root: PathBuf::from("/srv/odin/source"), - }; - let targets = swarm_targets(&options).expect("yggdrasil-local targets"); - let target = targets - .iter() - .find(|target| target.daemon_id == "yggdrasil-bifrost-persona-feedback") - .expect("Bifrost Persona-feedback target"); - assert_eq!( - target.health_contract.id, - "bifrost.cultnet-rudp-persona-feedback-health" - ); - assert_eq!( - target.deploy_command.as_deref(), - Some( - "sudo -n /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback \"$IDUNN_SOURCE_COMMIT\" \"$IDUNN_DEPLOYMENT_REQUEST_ID\" \"$BIFROST_RELEASE_AUTHORITY_ID\" \"$BIFROST_RELEASE_AUTHORITY_SHA256\"" - ) - ); - assert!(target.restart_command.is_none()); - let release = target.release.as_ref().expect("release target"); - assert!(release.requires_bifrost_authority); - assert_eq!(release.repository_full_name, "GameCult/Bifrost"); - assert_eq!( - release.repo_path, - PathBuf::from("/srv/build/Bifrost-persona-feedback") - ); - - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: None, - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(target), - command_boundary_id: command_boundary_id(target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - }; - let health = missing_daemon_published_health(target, &desired, "unix:101"); - assert_eq!(health.state, "dependency-unavailable"); - let plan = plan_keepalive(&desired, &health, "unix:102"); - assert_ne!(plan.decision.action, "deploy"); - assert!(plan.deployment_request.is_none()); - assert!(plan.restart_request.is_none()); - } - - #[test] - fn fresh_daemon_published_rudp_health_is_the_health_owner() { - let desired = IdunnDesiredDaemonRecord { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - enabled: true, - health_command: None, - restart_command: Some("restart test".to_string()), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - deploy_command: None, - health_contract: "test.cultnet-rudp-health".to_string(), - transport_profile_id: "transport:test-daemon".to_string(), - command_boundary_id: "command-boundary:test-daemon".to_string(), - }; - let mut health = IdunnDaemonHealthRecord { - daemon_id: "test-daemon".to_string(), - state: "active".to_string(), - detail: "daemon published".to_string(), - observed_at: "unix:95".to_string(), - health_contract: "test.cultnet-rudp-health".to_string(), - publication_source: "daemon-published".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - }; - - assert!(is_fresh_daemon_published_health( - &health, &desired, "unix:100" - )); - - health.observed_at = "1970-01-01T00:01:35.0000000+00:00".to_string(); - assert!(is_fresh_daemon_published_health( - &health, &desired, "unix:100" - )); - - health.observed_at = "unix:1".to_string(); - assert!(!is_fresh_daemon_published_health( - &health, &desired, "unix:100" - )); - - health.observed_at = "unix:95".to_string(); - health.publication_source = "debug-command".to_string(); - assert!(!is_fresh_daemon_published_health( - &health, &desired, "unix:100" - )); - } - - #[test] - fn evaluate_target_health_refuses_bare_daemon_published_row_without_admission() { - let now_seconds = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(); - let now = format!("unix:{now_seconds}"); - let store_path = std::env::temp_dir().join(format!( - "idunn-test-store-{}.cc", - SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_nanos() - )); - let options = CommonOptions { - store_path: store_path.clone(), - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }; - let store_lock = Arc::new(Mutex::new(())); - let target = DaemonTarget { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - health_contract: health_contract("test.cultnet-rudp-health", "failed"), - deploy_command: None, - restart_command: Some("restart test".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: target.enabled, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: now.clone(), - }; - let health = IdunnDaemonHealthRecord { - daemon_id: target.daemon_id.clone(), - state: "active".to_string(), - detail: "daemon published".to_string(), - observed_at: now.clone(), - health_contract: target.health_contract.id.clone(), - publication_source: "daemon-published".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - }; - - with_store_node(&options, &store_lock, |node| { - node.put(&health.daemon_id, &health)?; - Ok(()) - }) - .unwrap(); - - let (health_key, selected, authenticated) = - evaluate_target_health(&target, &options, &store_lock, &desired, &now).unwrap(); - - assert_eq!(health_key, "observation:test-daemon"); - assert_eq!(selected.publication_source, "idunn-supervisor-observation"); - assert_ne!(selected.state, "active"); - assert!(authenticated.is_none()); - - let _ = std::fs::remove_file(store_path); - } - - #[test] - fn evaluate_target_health_reports_missing_daemon_publication_without_probe_lane() { - let store_path = std::env::temp_dir().join(format!( - "idunn-test-store-{}.cc", - SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_nanos() - )); - let options = CommonOptions { - store_path: store_path.clone(), - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }; - let store_lock = Arc::new(Mutex::new(())); - let target = DaemonTarget { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - health_contract: health_contract("test.cultnet-rudp-health", "failed"), - deploy_command: None, - restart_command: Some("restart test".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: target.enabled, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - }; - - let (health_key, selected, authenticated) = - evaluate_target_health(&target, &options, &store_lock, &desired, "unix:100").unwrap(); - - assert_eq!(health_key, "observation:test-daemon"); - assert_eq!(selected.state, "dependency-unavailable"); - assert_eq!(selected.publication_source, "idunn-supervisor-observation"); - assert_eq!(selected.transport, "cultmesh.missing-daemon-publication"); - assert!(authenticated.is_none()); - assert!( - selected - .detail - .contains("Idunn did not run local health probes") - ); - - let _ = std::fs::remove_file(store_path); - } - - #[test] - fn locally_supervised_health_waits_for_continuous_missing_boundary() { - let target = DaemonTarget { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - health_contract: locally_supervised_health_contract( - "test.cultnet-rudp-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart test".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: None, - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - }; - let now = Instant::now(); - let mut missing_since = None; - let mut health = missing_daemon_published_health(&target, &desired, "unix:100"); - - apply_missing_publication_grace( - &target, - &desired, - &mut health, - false, - &mut missing_since, - now, - ); - - assert_eq!(health.state, "degraded"); - assert!(missing_since.is_some()); - assert!( - plan_keepalive(&desired, &health, "unix:100") - .restart_request - .is_none() - ); - - let mut health = missing_daemon_published_health(&target, &desired, "unix:161"); - apply_missing_publication_grace( - &target, - &desired, - &mut health, - false, - &mut missing_since, - now + Duration::from_secs(61), - ); - - assert_eq!(health.state, "failed"); - assert!( - plan_keepalive(&desired, &health, "unix:161") - .restart_request - .is_some() - ); - } - - #[test] - fn only_authenticated_daemon_publication_resets_missing_continuity() { - let target = DaemonTarget { - daemon_id: "test-daemon".to_string(), - verse_id: "test.local".to_string(), - name: "Test daemon".to_string(), - health_contract: locally_supervised_health_contract( - "test.cultnet-rudp-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart test".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: None, - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - authority: "idunn-supervisor-command".to_string(), - max_silence_seconds: 60, - observed_at: "unix:100".to_string(), - }; - let mut health = IdunnDaemonHealthRecord { - daemon_id: target.daemon_id.clone(), - state: "active".to_string(), - detail: "daemon published".to_string(), - health_contract: target.health_contract.id.clone(), - publication_source: "daemon-published".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - observed_at: "unix:100".to_string(), - }; - let mut missing_since = Some(Instant::now() - Duration::from_secs(120)); - - apply_missing_publication_grace( - &target, - &desired, - &mut health, - false, - &mut missing_since, - Instant::now(), - ); - - assert!(missing_since.is_some()); - assert_eq!(health.state, "active"); - apply_missing_publication_grace( - &target, - &desired, - &mut health, - true, - &mut missing_since, - Instant::now(), - ); - assert!(missing_since.is_none()); - } - - #[test] - fn stonks_transport_profile_marks_provider_store_live() { - let stonks = DaemonTarget { - daemon_id: "stonks".to_string(), - verse_id: "starfire.local".to_string(), - name: "Stonks".to_string(), - health_contract: health_contract("stonks.cultnet-rudp-market-health", "failed"), - deploy_command: None, - restart_command: Some("restart-stonks.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - - let profile = daemon_transport_profile(&stonks, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert!( - profile - .current_transport - .contains("daemon-owned-cultcache-provider-store") - ); - assert!( - profile - .cut_line - .contains("Renderer/debug surfaces live outside daemon transport authority") - ); - } - - #[test] - fn weksa_transport_profile_marks_provider_store_live() { - let weksa = DaemonTarget { - daemon_id: "weksa".to_string(), - verse_id: "starfire.local".to_string(), - name: "Weksa".to_string(), - health_contract: health_contract("weksa.cultnet-rudp-provider-health", "failed"), - deploy_command: None, - restart_command: Some("restart-weksa.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 60, - }; - - let profile = daemon_transport_profile(&weksa, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert_eq!( - profile.current_transport, - "daemon-published-rudp-health + daemon-owned-cultcache-provider-store + odin-cultmesh-command-documents" - ); - assert!( - profile - .cut_line - .contains("typed CultMesh/CultNet command documents") - ); - } - - #[test] - fn streampixels_transport_profile_marks_provider_store_live() { - let streampixels = DaemonTarget { - daemon_id: "yggdrasil-streampixels".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil StreamPixels".to_string(), - health_contract: health_contract("streampixels.cultnet-rudp-service-health", "failed"), - deploy_command: Some("deploy-yggdrasil-streampixels.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - - let profile = daemon_transport_profile(&streampixels, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert!( - profile - .current_transport - .contains("daemon-owned-cultcache-service-boundary") - ); - assert!(!profile.current_transport.contains("local-proof")); - assert!(profile.cut_line.contains("configured service health route")); - assert!(!profile.cut_line.contains("10.77.0.1")); - assert!(profile.cut_line.contains("service-owned witness")); - } - - #[test] - fn heimdall_transport_profile_marks_boundary_store_live() { - let heimdall = DaemonTarget { - daemon_id: "yggdrasil-heimdall".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Heimdall".to_string(), - health_contract: health_contract("heimdall.cultnet-rudp-provider-health", "failed"), - deploy_command: Some("deploy-yggdrasil-heimdall.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - - let profile = daemon_transport_profile(&heimdall, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert!( - profile - .current_transport - .contains("daemon-owned-cultcache-boundary-store") - ); - assert!( - profile - .cut_line - .contains("/srv/heimdall/cultcache/heimdall.service.cc") - ); - } - - #[test] - fn repixelizer_transport_profile_marks_rudp_boundary_store_live() { - let repixelizer = DaemonTarget { - daemon_id: "yggdrasil-repixelizer".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil repixelizer".to_string(), - health_contract: health_contract("repixelizer.cultnet-rudp-service-health", "failed"), - deploy_command: Some("deploy-yggdrasil-repixelizer.cmd".to_string()), - restart_command: None, - release: None, - enabled: true, - interval_seconds: 300, - }; - - let profile = daemon_transport_profile(&repixelizer, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert!( - profile - .current_transport - .contains("daemon-published-rudp-health") - ); - assert!( - profile - .cut_line - .contains("/srv/repixelizer/cultcache/repixelizer.service.cc") - ); - } - - #[test] - fn voidbot_transport_profile_marks_provider_store_live() { - let voidbot = DaemonTarget { - daemon_id: "voidbot".to_string(), - verse_id: "starfire.local".to_string(), - name: "VoidBot local stack".to_string(), - health_contract: health_contract("voidbot.cultnet-rudp-stack-health", "failed"), - deploy_command: None, - restart_command: Some("restart-voidbot.cmd".to_string()), - release: None, - enabled: true, - interval_seconds: 60, - }; - - let profile = daemon_transport_profile(&voidbot, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert_eq!( - profile.current_transport, - "daemon-published-rudp-health + daemon-owned-cultmesh-provider-store" - ); - assert!(profile.cut_line.contains("voidbot-swarm-state.cc")); - } - - #[test] - fn gjallar_transport_profile_marks_partial_rudp_health() { - let gjallar = DaemonTarget { - daemon_id: "yggdrasil-gjallar".to_string(), - verse_id: "yggdrasil.local".to_string(), - name: "Yggdrasil Gjallar Eve surface compositor".to_string(), - health_contract: health_contract("gjallar.cultnet-rudp-composition-health", "failed"), - deploy_command: Some("idunn-yggdrasil deploy gjallar".to_string()), - restart_command: Some("idunn-yggdrasil restart gjallar".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - - let profile = daemon_transport_profile(&gjallar, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert_eq!( - profile.current_transport, - "daemon-published-rudp-health + daemon-owned-cultcache-aggregate-surface + local-odin-cultnet-rudp-snapshot-input" - ); - assert!(profile.cut_line.contains("typed aggregate Eve surface")); - assert!(profile.cut_line.contains("no framebuffer")); - } - - #[test] - fn muninn_transport_profiles_mark_live_rudp_health() { - let raven = DaemonTarget { - daemon_id: "muninn".to_string(), - verse_id: "raven.local".to_string(), - name: "Raven Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-remote-telemetry-health", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let starfire = DaemonTarget { - daemon_id: "starfire-muninn".to_string(), - verse_id: "starfire.local".to_string(), - name: "Starfire Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-local-telemetry-and-quest-access", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-starfire-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let nightwing = DaemonTarget { - daemon_id: "nightwing-muninn".to_string(), - verse_id: "nightwing.local".to_string(), - name: "Nightwing Muninn".to_string(), - health_contract: health_contract( - "muninn.cultnet-rudp-remote-telemetry-and-move-hid", - "failed", - ), - deploy_command: None, - restart_command: Some("restart-nightwing-muninn.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - - let raven_profile = daemon_transport_profile(&raven, "unix:100"); - assert_eq!( - raven_profile.state, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - raven_profile - .current_transport - .contains("daemon-owned-cultcache-telemetry-store") - ); - assert!( - raven_profile - .current_transport - .contains("background-only hidden task launch") - ); - assert!( - raven_profile - .cut_line - .contains("C:\\Meta\\Odin\\state\\muninn.telemetry.cc") - ); - - let starfire_profile = daemon_transport_profile(&starfire, "unix:100"); - assert_eq!( - starfire_profile.state, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - starfire_profile - .current_transport - .contains("daemon-owned-cultcache-telemetry-store") - ); - assert!(starfire_profile.cut_line.contains("Quest ADB availability")); - assert!( - starfire_profile - .cut_line - .contains("C:\\Meta\\Odin\\state\\starfire.muninn.telemetry.cc") - ); - - let nightwing_profile = daemon_transport_profile(&nightwing, "unix:100"); - assert_eq!( - nightwing_profile.state, - "partial-rudp-health-and-provider-store-live" - ); - assert!( - nightwing_profile - .current_transport - .contains("daemon-owned-cultcache-telemetry-store") - ); - assert!(nightwing_profile.cut_line.contains("Move HID evidence")); - assert!( - nightwing_profile - .cut_line - .contains("/home/metacrat/.local/state/gamecult/muninn/muninn.telemetry.cc") - ); - } - - #[test] - fn sleipnir_transport_profile_marks_live_rudp_health() { - let sleipnir = DaemonTarget { - daemon_id: "raven-sleipnir".to_string(), - verse_id: "raven.local".to_string(), - name: "Raven Sleipnir".to_string(), - health_contract: health_contract("sleipnir.cultnet-rudp-input-mirror-health", "failed"), - deploy_command: Some("deploy-raven-sleipnir.ps1".to_string()), - restart_command: Some("restart-raven-sleipnir.ps1".to_string()), - release: None, - enabled: true, - interval_seconds: 30, - }; - - let profile = daemon_transport_profile(&sleipnir, "unix:100"); - - assert_eq!(profile.state, "partial-rudp-health-and-provider-store-live"); - assert_eq!( - profile.current_transport, - "daemon-published-rudp-health + daemon-owned-cultcache-input-mirror-store" - ); - assert!(profile.cut_line.contains("Raven input mirror runtime")); - assert!( - profile - .cut_line - .contains("scheduled task is an Idunn restart lowering") - ); - } - - #[test] - fn runtime_transport_check_proves_rudp_loopback() { - let check = runtime_transport_check("2026-06-15T00:00:00Z"); - - assert_eq!(check.check_id, "idunn-runtime-rudp-loopback"); - assert_eq!(check.transport, CULTNET_RUDP_PROTOCOL_ID); - assert_eq!(check.state, "available"); - } - - #[test] - fn rudp_health_ingress_quarantines_unsigned_health_without_authority() { - let health = IdunnDaemonHealthRecord { - daemon_id: "test-daemon".to_string(), - state: "active".to_string(), - detail: "published by daemon".to_string(), - observed_at: "2026-06-15T00:00:00Z".to_string(), - health_contract: "test.cultnet-rudp-health".to_string(), - publication_source: "daemon-published".to_string(), - transport: CULTNET_RUDP_PROTOCOL_ID.to_string(), - }; - let message = CultNetMessage::DocumentPutRaw { - message_id: "test-health".to_string(), - document: CultNetRawDocumentRecord { - schema_id: "idunn.daemon_health".to_string(), - record_key: health.daemon_id.clone(), - stored_at: "2026-06-15T00:00:00Z".to_string(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: rmp_serde::to_vec(&IdunnDaemonHealthWireV1 { - daemon_id: health.daemon_id.clone(), - state: health.state.clone(), - detail: health.detail.clone(), - observed_at: health.observed_at.clone(), - health_contract: health.health_contract.clone(), - publication_source: health.publication_source.clone(), - transport: health.transport.clone(), - }) - .unwrap(), - source_runtime_id: Some("test-daemon".to_string()), - source_agent_id: None, - source_role: Some("daemon-health-publisher".to_string()), - tags: Some(vec!["cultnet.transport.rudp.v0".to_string()]), - }, - }; - - let root = tempfile::tempdir().unwrap(); - let options = CommonOptions { - store_path: root.path().join("idunn.cc"), - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }; - let lock = Arc::new(Mutex::new(())); - let outcome = - admit_health_from_rudp_message(&message, &options, &lock, "2026-06-15T00:00:01Z") - .unwrap(); - assert_eq!(outcome.authority, "diagnostic-only"); - assert!(health_from_rudp_message(&message, &options).is_err()); - with_store_node(&options, &lock, |node| { - assert!( - node.get::(&health.daemon_id)? - .is_none() - ); - let diagnostic = node - .get::(&format!( - "diagnostic:{}", - health.daemon_id - ))? - .expect("unsigned diagnostic"); - assert_eq!(diagnostic.claimed_state, "active"); - assert_eq!(diagnostic.authority, "diagnostic-only"); - Ok(()) - }) - .unwrap(); - } - - fn generic_signed_health_fixture( - sequence: u64, - ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { - generic_signed_health_fixture_with_release(sequence, false) - } - - fn generic_signed_health_fixture_with_release( - sequence: u64, - release_bound: bool, - ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { - let root = tempfile::tempdir().unwrap(); - let trust_path = root.path().join("health-trust.cc"); - let store_path = root.path().join("idunn.cc"); - let signer = cultnet_rs::enroll_service_identity_at::( - &root.path().join("provider-health-identity.cc"), - ) - .unwrap(); - let public_key = signer.entry().public_key.clone(); - let identity_id = signer.entry().identity_id.clone(); - let binding = IdunnDaemonHealthTrustBindingRecord { - schema_version: odin_core::IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), - binding_id: "root/test-daemon/health".into(), - daemon_id: "test-daemon".into(), - health_contract: "test.signed-health".into(), - source_runtime_id: "test-runtime".into(), - signer_identity_id: identity_id.clone(), - signer_public_key: public_key, - binding_authority: "root".into(), - bound_at_unix_millis: 1_784_483_100_000, - release_binding_required: release_bound, - private_state_exposed: false, - }; - SingleFileMessagePackBackingStore::new(&trust_path) - .push(&typed_envelope(&binding.binding_id, &binding, "unix:1784483100").unwrap()) - .unwrap(); - let observed_at_unix_millis = chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00Z") - .unwrap() - .timestamp_millis() as u64; - let mut statement = IdunnSignedDaemonHealthRecord { - schema_version: odin_core::IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA.into(), - daemon_id: binding.daemon_id.clone(), - health_contract: binding.health_contract.clone(), - source_runtime_id: binding.source_runtime_id.clone(), - state: "active".into(), - detail: "signed provider health".into(), - signer_identity_id: identity_id, - publisher_incarnation_id: "00000000-0000-4000-8000-000000000031".into(), - publisher_sequence: sequence, - observed_at_unix_millis, - release_id: release_bound.then(|| "release-test-1".into()), - release_witness_sha256: release_bound.then(|| format!("sha256-{}", "a".repeat(64))), - source_commit: release_bound.then(|| "b".repeat(40)), - deployment_id: release_bound.then(|| "deploy-test-1".into()), - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - private_state_exposed: false, - activation_witness_sha256: release_bound.then(|| format!("sha256-{}", "f".repeat(64))), - }; - let unsigned = rmp_serde::to_vec(&statement).unwrap(); - statement.signature = signer - .sign::(&unsigned) - .signature; - let message = CultNetMessage::DocumentPutRaw { - message_id: format!("signed-health-{sequence}"), - document: CultNetRawDocumentRecord { - schema_id: SIGNED_DAEMON_HEALTH_SCHEMA_ID.into(), - record_key: statement.daemon_id.clone(), - stored_at: "2026-07-19T12:00:00Z".into(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: rmp_serde::to_vec(&statement).unwrap(), - source_runtime_id: Some(statement.source_runtime_id.clone()), - source_agent_id: Some(statement.signer_identity_id.clone()), - source_role: Some("daemon-health-publisher".into()), - tags: Some(vec![CULTNET_RUDP_PROTOCOL_ID.into()]), - }, - }; - if release_bound { - let request = IdunnDeploymentRequestRecord { - request_id: "deploy-test-1".into(), - daemon_id: "test-daemon".into(), - command: "deploy".into(), - authority: "idunn".into(), - requested_at: "2026-07-19T11:59:00Z".into(), - repository_full_name: "GameCult/Test".into(), - upstream_ref: "refs/heads/main".into(), - source_revision: "b".repeat(40), - release_authority_id: "bifrost-test".into(), - release_authority_envelope_sha256: format!("sha256-{}", "c".repeat(64)), - requires_bifrost_authority: true, - }; - let head = IdunnCurrentDeploymentRequestRecord { - daemon_id: "test-daemon".into(), - request_id: request.request_id.clone(), - sequence: 1, - updated_at: "2026-07-19T11:59:00Z".into(), - }; - let mut backing = SingleFileMessagePackBackingStore::new(&store_path); - backing - .push(&typed_envelope(&request.request_id, &request, "unix:1784462340").unwrap()) - .unwrap(); - backing - .push(&typed_envelope(&head.daemon_id, &head, "unix:1784462340").unwrap()) - .unwrap(); - } - ( - message, - CommonOptions { - store_path, - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: Some(trust_path), - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }, - root, - ) - } - - fn advance_generic_signed_health_fixture( - message: &CultNetMessage, - root: &Path, - sequence: u64, - ) -> CultNetMessage { - let CultNetMessage::DocumentPutRaw { document, .. } = message else { - panic!("signed health fixture must be a raw document"); - }; - let signer = open_service_identity_at::( - &root.join("provider-health-identity.cc"), - ) - .unwrap(); - let mut statement: IdunnSignedDaemonHealthRecord = - rmp_serde::from_slice(&document.payload).unwrap(); - statement.publisher_sequence = sequence; - statement.signature.clear(); - let unsigned = rmp_serde::to_vec(&statement).unwrap(); - statement.signature = signer - .sign::(&unsigned) - .signature; - CultNetMessage::DocumentPutRaw { - message_id: format!("signed-health-{sequence}"), - document: CultNetRawDocumentRecord { - payload: rmp_serde::to_vec(&statement).unwrap(), - ..document.clone() - }, - } - } - - fn legacy_generic_signed_health_fixture() -> (CultNetMessage, CommonOptions, tempfile::TempDir) - { - let (mut message, options, root) = generic_signed_health_fixture(1); - let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { - unreachable!() - }; - let mut legacy: IdunnSignedDaemonHealthRecord = - rmp_serde::from_slice(&document.payload).unwrap(); - legacy.activation_witness_sha256 = None; - legacy.signature.clear(); - let mut unsigned = rmp_serde::to_vec(&legacy).unwrap(); - assert_eq!(&unsigned[..3], &[0xdc, 0, 18]); - assert_eq!(unsigned.pop(), Some(0xc0)); - unsigned[2] = 17; - let signer = open_service_identity_at::( - &root.path().join("provider-health-identity.cc"), - ) - .unwrap(); - legacy.signature = signer - .sign::(&unsigned) - .signature; - document.payload = rmp_serde::to_vec(&legacy).unwrap(); - assert_eq!(document.payload.pop(), Some(0xc0)); - document.payload[2] = 17; - (message, options, root) - } - - #[test] - fn generic_signed_health_requires_root_binding_and_monotonic_admission() { - let (first, options, _root) = generic_signed_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - let outcome = - admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:01Z") - .unwrap(); - assert_eq!(outcome.authority, "authenticated"); - assert!( - admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:02Z") - .is_err() - ); - with_store_node(&options, &lock, |node| { - let health = node - .get::("test-daemon")? - .expect("authenticated health"); - assert_eq!(health.publication_source, "daemon-authenticated"); - let admission = node - .get::("test-daemon")? - .expect("generic admission"); - assert_eq!(admission.publisher_sequence, 1); - assert_eq!(admission.trust_binding_id, "root/test-daemon/health"); - assert_eq!(admission.activation_witness_sha256, None); - let statement = node - .get::(&admission.daemon_id)? - .expect("signed statement"); - assert_eq!( - admission.signed_health_sha256, - format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(&statement)?) - ) - ); - let (_, binding_sha256) = load_daemon_health_trust_binding(&options, &statement)?; - assert_eq!(admission.trust_binding_sha256, binding_sha256); - Ok(()) - }) - .unwrap(); - let target = DaemonTarget { - daemon_id: "test-daemon".into(), - verse_id: "test.local".into(), - name: "Test daemon".into(), - health_contract: health_contract("test.signed-health", "failed"), - deploy_command: None, - restart_command: Some("restart test".into()), - release: None, - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: None, - authority: "idunn-supervisor-command".into(), - max_silence_seconds: 60, - observed_at: "2026-07-19T12:00:02Z".into(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - }; - let (health_key, selected, authenticated) = - evaluate_target_health(&target, &options, &lock, &desired, "2026-07-19T12:00:02Z") - .unwrap(); - assert_eq!(health_key, "test-daemon"); - assert_eq!(selected.publication_source, "daemon-authenticated"); - assert!(authenticated.is_some()); - - let (mut forged, mut no_trust, _other_root) = generic_signed_health_fixture(2); - no_trust.daemon_health_trust_store_path = None; - assert!( - admit_health_from_rudp_message( - &forged, - &no_trust, - &Arc::new(Mutex::new(())), - "2026-07-19T12:00:01Z" - ) - .is_err() - ); - if let CultNetMessage::DocumentPutRaw { document, .. } = &mut forged { - document.payload[0] ^= 1; - } - assert!(health_from_rudp_message(&forged, &options).is_err()); - } - - #[test] - fn legacy_generic_health_remains_admissible_without_activation_authority() { - let (message, options, _root) = legacy_generic_signed_health_fixture(); - let raw_payload = match &message { - CultNetMessage::DocumentPutRaw { document, .. } => document.payload.clone(), - _ => unreachable!(), - }; - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - with_store_node(&options, &lock, |node| { - let admission = node - .get::("test-daemon")? - .expect("legacy generic admission"); - assert_eq!(admission.activation_witness_sha256, None); - assert_eq!( - admission.signed_health_sha256, - format!("sha256-{:x}", Sha256::digest(&raw_payload)) - ); - Ok(()) - }) - .unwrap(); - } - - #[test] - fn generic_health_replaces_one_daemon_keyed_statement_instead_of_growing_history() { - let (first, options, root) = generic_signed_health_fixture(1); - let second = advance_generic_signed_health_fixture(&first, root.path(), 2); - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&first, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - admit_health_from_rudp_message(&second, &options, &lock, "2026-07-19T12:00:02Z").unwrap(); - - let entries = SingleFileMessagePackBackingStore::new(&options.store_path) - .pull_all_read_only_snapshot() - .unwrap(); - let statements = entries - .iter() - .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) - .collect::>(); - assert_eq!(statements.len(), 1); - assert_eq!(statements[0].key, "test-daemon"); - let admission = entries - .iter() - .find(|entry| { - entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE - && entry.key == "test-daemon" - }) - .map(|entry| { - rmp_serde::from_slice::( - &entry.payload, - ) - .unwrap() - }) - .unwrap(); - assert_eq!(admission.publisher_sequence, 2); - let statement: IdunnSignedDaemonHealthRecord = - rmp_serde::from_slice(&statements[0].payload).unwrap(); - assert_eq!(statement.publisher_sequence, 2); - assert_eq!( - admission.signed_health_sha256, - format!( - "sha256-{:x}", - Sha256::digest(rmp_serde::to_vec(&statement).unwrap()) - ) - ); - } - - #[test] - fn startup_compaction_keeps_only_each_admissions_exact_signed_statement() { - let (message, options, _root) = generic_signed_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - let mut backing = SingleFileMessagePackBackingStore::new(&options.store_path); - let mut entries = backing.pull_all_read_only_snapshot().unwrap(); - let admission = entries - .iter() - .find(|entry| entry.r#type == IdunnAuthenticatedDaemonHealthAdmissionRecord::TYPE) - .map(|entry| { - rmp_serde::from_slice::( - &entry.payload, - ) - .unwrap() - }) - .unwrap(); - let current_index = entries - .iter() - .position(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) - .unwrap(); - let mut legacy_current = entries.remove(current_index); - legacy_current.key = admission.signed_health_sha256.clone(); - let mut obsolete = legacy_current.clone(); - obsolete.key = format!("sha256-{}", "d".repeat(64)); - entries.push(legacy_current); - entries.push(obsolete); - backing.push_all(&entries, Default::default()).unwrap(); - - assert_eq!( - compact_authenticated_health_statement_history(&options, &lock).unwrap(), - 1 - ); - assert_eq!( - compact_authenticated_health_statement_history(&options, &lock).unwrap(), - 0 - ); - let compacted = backing.pull_all_read_only_snapshot().unwrap(); - let statements = compacted - .iter() - .filter(|entry| entry.r#type == IdunnSignedDaemonHealthRecord::TYPE) - .collect::>(); - assert_eq!(statements.len(), 1); - assert_eq!(statements[0].key, "test-daemon"); - let statement: IdunnSignedDaemonHealthRecord = - rmp_serde::from_slice(&statements[0].payload).unwrap(); - assert_eq!(statement.publisher_sequence, 1); - } - - #[test] - fn operational_history_compaction_is_bounded_and_keeps_the_current_deployment_chain() { - let (_message, options, _root) = generic_signed_health_fixture(1); - let mut entries = Vec::new(); - for daemon_id in ["alpha", "beta"] { - for index in 0..12 { - let observed_at = format!("2026-08-23T00:00:{index:02}Z"); - let decision = odin_core::IdunnKeepaliveDecisionRecord { - decision_id: format!("decision:{daemon_id}:{index:02}"), - daemon_id: daemon_id.into(), - action: "observe".into(), - reason: "fixture".into(), - authority: "idunn-supervisor-command".into(), - decided_at: observed_at.clone(), - }; - entries - .push(typed_envelope(&decision.decision_id, &decision, &observed_at).unwrap()); - let alarm = IdunnOperatorAlarmRecord { - alarm_id: format!("alarm:{daemon_id}:{index:02}"), - daemon_id: daemon_id.into(), - severity: "operator-action-required".into(), - reason: "fixture".into(), - escalation_target: "bifrost.operator-notification".into(), - raised_at: observed_at.clone(), - }; - entries.push(typed_envelope(&alarm.alarm_id, &alarm, &observed_at).unwrap()); - let request = IdunnDeploymentRequestRecord { - request_id: format!("deploy:{daemon_id}:{index:02}"), - daemon_id: daemon_id.into(), - command: "deploy fixture".into(), - authority: "idunn-supervisor-command".into(), - requested_at: observed_at.clone(), - repository_full_name: "GameCult/Fixture".into(), - upstream_ref: "refs/heads/main".into(), - source_revision: "a".repeat(40), - release_authority_id: "release:fixture".into(), - release_authority_envelope_sha256: format!("sha256-{}", "a".repeat(64)), - requires_bifrost_authority: false, - }; - entries.push(typed_envelope(&request.request_id, &request, &observed_at).unwrap()); - let result = IdunnDeploymentResultRecord { - result_id: format!("result:{}", request.request_id), - request_id: request.request_id, - daemon_id: daemon_id.into(), - state: "failed".into(), - detail: "fixture".into(), - completed_at: observed_at.clone(), - }; - entries.push(typed_envelope(&result.result_id, &result, &observed_at).unwrap()); - } - for index in 0..40 { - let requested_at = format!("2026-08-23T00:01:{index:02}Z"); - let command = IdunnLifecycleCommandRecord { - command_id: format!("command:{daemon_id}:{index:02}"), - daemon_id: daemon_id.into(), - action: "restart".into(), - state: "rejected".into(), - requested_by: "fixture".into(), - requested_at: requested_at.clone(), - detail: "fixture".into(), - claimed_at: requested_at.clone(), - result_id: String::new(), - }; - entries.push(typed_envelope(&command.command_id, &command, &requested_at).unwrap()); - } - } - let head = IdunnCurrentDeploymentRequestRecord { - daemon_id: "alpha".into(), - request_id: "deploy:alpha:00".into(), - sequence: 1, - updated_at: "2026-08-23T00:00:00Z".into(), - }; - entries.push(typed_envelope(&head.daemon_id, &head, &head.updated_at).unwrap()); - let mut backing = SingleFileMessagePackBackingStore::new(&options.store_path); - backing.push_all(&entries, Default::default()).unwrap(); - - assert!(compact_operational_history(&options, &Arc::new(Mutex::new(()))).unwrap() > 0); - assert_eq!( - compact_operational_history(&options, &Arc::new(Mutex::new(()))).unwrap(), - 0 - ); - let compacted = backing.pull_all_read_only_snapshot().unwrap(); - let count = |kind: &str| { - compacted - .iter() - .filter(|entry| entry.r#type == kind) - .count() - }; - assert_eq!(count(odin_core::IdunnKeepaliveDecisionRecord::TYPE), 16); - assert_eq!(count(IdunnOperatorAlarmRecord::TYPE), 16); - assert_eq!(count(IdunnLifecycleCommandRecord::TYPE), 64); - assert_eq!(count(IdunnDeploymentRequestRecord::TYPE), 17); - assert_eq!(count(IdunnDeploymentResultRecord::TYPE), 17); - assert!(compacted.iter().any(|entry| { - entry.r#type == IdunnDeploymentRequestRecord::TYPE && entry.key == "deploy:alpha:00" - })); - assert!(compacted.iter().any(|entry| { - entry.r#type == IdunnDeploymentResultRecord::TYPE - && entry.key == "result:deploy:alpha:00" - })); - assert!(compacted.iter().any(|entry| { - entry.r#type == IdunnCurrentDeploymentRequestRecord::TYPE && entry.key == "alpha" - })); - } - - #[test] - fn deployment_validator_accepts_exact_generic_authenticated_health() { - let (message, options, _root) = generic_signed_health_fixture_with_release(1, true); - let lock = Arc::new(Mutex::new(())); - with_store_node(&options, &lock, |node| { - node.put( - "test-daemon", - &IdunnDesiredDaemonRecord { - daemon_id: "test-daemon".into(), - verse_id: "test.local".into(), - name: "Test daemon".into(), - enabled: true, - health_command: None, - restart_command: None, - deploy_command: Some("deploy test".into()), - authority: "idunn-supervisor-command".into(), - max_silence_seconds: 60, - observed_at: "2026-07-19T12:00:00Z".into(), - health_contract: "test.signed-health".into(), - transport_profile_id: "test-transport".into(), - command_boundary_id: "test-command".into(), - }, - )?; - Ok(()) - }) - .unwrap(); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - - let exact = HealthAdmissionValidationOptions { - daemon_id: "test-daemon".into(), - deployment_request_id: "deploy-test-1".into(), - release_id: "release-test-1".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: Some(format!("sha256-{}", "f".repeat(64))), - write_admission_witness: None, - runtime_process: None, - }; - validate_health_admission_at(&exact, &options, "2026-07-19T12:00:02Z").unwrap(); - - let mut substituted = exact.clone(); - substituted.release_witness_sha256 = format!("sha256-{}", "f".repeat(64)); - let error = validate_health_admission_at(&substituted, &options, "2026-07-19T12:00:02Z") - .unwrap_err() - .to_string(); - assert!(error.contains("does not prove the exact active candidate")); - substituted.release_witness_sha256 = format!("sha256-{}", "a".repeat(64)); - substituted.activation_witness_sha256 = Some(format!("sha256-{}", "e".repeat(64))); - let error = validate_health_admission_at(&substituted, &options, "2026-07-19T12:00:02Z") - .unwrap_err() - .to_string(); - assert!(error.contains("does not prove the exact active candidate")); - - with_store_node(&options, &lock, |node| { - let mut admission = node - .get::("test-daemon")? - .expect("authenticated admission"); - admission.release_id = Some("caller-substituted-release".into()); - node.put(&admission.daemon_id, &admission)?; - Ok(()) - }) - .unwrap(); - let mut forged_expected = exact; - forged_expected.release_id = "caller-substituted-release".into(); - let error = - validate_health_admission_at(&forged_expected, &options, "2026-07-19T12:00:02Z") - .unwrap_err() - .to_string(); - assert!(error.contains("no fresh authenticated health admission")); - } - - fn runtime_traffic_admission_record_fixture() -> IdunnRuntimeTrafficAdmissionRecord { - IdunnRuntimeTrafficAdmissionRecord { - schema_version: IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into(), - daemon_id: IDUNN_CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(), - release_id: "release-connector-1".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - deployment_id: "deploy-connector-1".into(), - activation_witness_sha256: format!("sha256-{}", "c".repeat(64)), - signed_health_sha256: format!("sha256-{}", "d".repeat(64)), - publisher_incarnation_id: "connector/boot-7/process-11".into(), - publisher_sequence: 42, - signer_identity_id: "connector-health-signer".into(), - runtime_process_id: 4_101, - runtime_process_starttime_ticks: 98_765, - } - } - - fn runtime_admission_validation_fixture() -> RuntimeAdmissionValidationOptions { - let record = runtime_traffic_admission_record_fixture(); - RuntimeAdmissionValidationOptions { - input: PathBuf::from(CODEX_CONNECTOR_RUNTIME_TRAFFIC_ADMISSION_PATH), - daemon_id: record.daemon_id, - deployment_request_id: record.deployment_id, - release_id: record.release_id, - release_witness_sha256: record.release_witness_sha256, - source_commit: record.source_commit, - activation_witness_sha256: record.activation_witness_sha256, - runtime_process: RuntimeProcessObservation { - process_id: record.runtime_process_id, - starttime_ticks: record.runtime_process_starttime_ticks, - }, - } - } - - #[test] - fn runtime_admission_lineage_refuses_stale_grant_replay() { - let record = runtime_traffic_admission_record_fixture(); - let exact = runtime_admission_validation_fixture(); - validate_runtime_admission_lineage(&record, &exact).unwrap(); - - let mut next_deployment = exact; - next_deployment.deployment_request_id = "deploy-connector-2".into(); - next_deployment.activation_witness_sha256 = format!("sha256-{}", "e".repeat(64)); - let error = validate_runtime_admission_lineage(&record, &next_deployment) - .unwrap_err() - .to_string(); - assert!(error.contains("exact expected release tuple")); - - let mut next_process = runtime_admission_validation_fixture(); - next_process.runtime_process.starttime_ticks += 1; - let error = validate_runtime_admission_lineage(&record, &next_process) - .unwrap_err() - .to_string(); - assert!(error.contains("exact expected release tuple")); - } - - #[test] - fn linux_proc_stat_parser_reads_exact_pid_and_field_22() { - let stat = - "4101 (connector worker ) odd) S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 98765 20"; - assert_eq!( - parse_linux_proc_stat_starttime_ticks(stat, 4_101).unwrap(), - 98_765 - ); - assert!(parse_linux_proc_stat_starttime_ticks(stat, 4_102).is_err()); - assert!(parse_linux_proc_stat_starttime_ticks("4101 worker S 1", 4_101).is_err()); - assert!( - parse_linux_proc_stat_starttime_ticks( - "4101 (worker) S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0", - 4_101 - ) - .is_err() - ); - } - - #[test] - fn runtime_admission_mutation_authority_is_root_only() { - require_runtime_admission_writer_uid(0).unwrap(); - let error = require_runtime_admission_writer_uid(1) - .unwrap_err() - .to_string(); - assert!(error.contains("requires root")); - } - - fn warming_authenticated_runtime_admission_fixture() - -> IdunnAuthenticatedDaemonHealthAdmissionRecord { - let grant = runtime_traffic_admission_record_fixture(); - IdunnAuthenticatedDaemonHealthAdmissionRecord { - schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), - daemon_id: grant.daemon_id, - health_contract: "codex-connector.authenticated-health".into(), - source_runtime_id: "codex-connector".into(), - state: "warming".into(), - observed_at_unix_millis: 1_788_200_000_000, - admitted_at_unix_millis: 1_788_200_000_001, - trust_binding_id: "root/yggdrasil-codex-connector/health".into(), - trust_binding_sha256: format!("sha256-{}", "e".repeat(64)), - signer_identity_id: grant.signer_identity_id, - publisher_incarnation_id: grant.publisher_incarnation_id, - publisher_sequence: grant.publisher_sequence, - signed_health_sha256: grant.signed_health_sha256, - release_id: Some(grant.release_id), - release_witness_sha256: Some(grant.release_witness_sha256), - source_commit: Some(grant.source_commit), - deployment_id: Some(grant.deployment_id), - private_state_exposed: false, - activation_witness_sha256: Some(grant.activation_witness_sha256), - } - } - - #[test] - fn runtime_admission_probation_is_warming_pending_only() { - assert!(validate_health_admission_posture("active", "ready", false).is_ok()); - assert!(validate_health_admission_posture("warming", "ready", false).is_err()); - assert!( - validate_health_admission_posture( - "warming", - RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL, - true - ) - .is_ok() - ); - assert!( - validate_health_admission_posture( - "active", - RUNTIME_TRAFFIC_ADMISSION_PENDING_DETAIL, - true - ) - .is_err() - ); - assert!(validate_health_admission_posture("warming", "ready", true).is_err()); - } - - #[test] - fn runtime_admission_is_derived_only_from_complete_authenticated_lineage() { - let admission = warming_authenticated_runtime_admission_fixture(); - let process = RuntimeProcessObservation { - process_id: 4_101, - starttime_ticks: 98_765, - }; - admission.validate().unwrap(); - assert_eq!( - runtime_traffic_admission_from(&admission, process).unwrap(), - runtime_traffic_admission_record_fixture() - ); - - let mut missing_activation = admission.clone(); - missing_activation.activation_witness_sha256 = None; - assert!(runtime_traffic_admission_from(&missing_activation, process).is_err()); - - let mut active = admission; - active.state = "active".into(); - assert!(runtime_traffic_admission_from(&active, process).is_err()); - - let mut ghostlight = warming_authenticated_runtime_admission_fixture(); - ghostlight.daemon_id = IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY.into(); - let activation = ghostlight.activation_witness_sha256.clone().unwrap(); - let ghostlight = runtime_traffic_admission_from(&ghostlight, process).unwrap(); - assert_eq!( - ghostlight.daemon_id, - IDUNN_GHOSTLIGHT_RUNTIME_TRAFFIC_ADMISSION_RECORD_KEY - ); - assert_eq!(ghostlight.activation_witness_sha256, activation); - } - - #[test] - fn runtime_admission_snapshot_is_exact_and_canonical() { - let record = runtime_traffic_admission_record_fixture(); - let envelope = CultCacheEnvelope { - key: record.daemon_id.clone(), - r#type: IdunnRuntimeTrafficAdmissionRecord::TYPE.into(), - payload: rmp_serde::to_vec(&record).unwrap(), - stored_at: "2026-09-02T12:00:00Z".into(), - schema_id: Some(IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA.into()), - }; - assert_eq!( - decode_runtime_traffic_admission_snapshot(std::slice::from_ref(&envelope)).unwrap(), - record - ); - - let mut wrong_schema = envelope.clone(); - wrong_schema.schema_id = Some("idunn.runtime_traffic_admission.v0".into()); - assert!(decode_runtime_traffic_admission_snapshot(&[wrong_schema]).is_err()); - - let mut invalid_timestamp = envelope.clone(); - invalid_timestamp.stored_at = "caller-selected-time".into(); - assert!(decode_runtime_traffic_admission_snapshot(&[invalid_timestamp]).is_err()); - - let mut noncanonical = envelope.clone(); - assert_eq!(noncanonical.payload.first(), Some(&0x9d)); - noncanonical.payload.splice(0..1, [0xdc, 0, 13]); - let error = decode_runtime_traffic_admission_snapshot(&[noncanonical]) - .unwrap_err() - .to_string(); - assert!(error.contains("canonical positional MessagePack")); - - assert!(decode_runtime_traffic_admission_snapshot(&[envelope.clone(), envelope]).is_err()); - } - - #[test] - fn unavailable_release_source_cannot_mask_authenticated_daemon_health() { - let (message, options, root) = generic_signed_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - let target = DaemonTarget { - daemon_id: "test-daemon".into(), - verse_id: "test.local".into(), - name: "Test daemon".into(), - health_contract: health_contract("test.signed-health", "failed"), - deploy_command: Some("deploy test".into()), - restart_command: Some("restart test".into()), - release: Some(with_deployed_revision_witness( - release_target( - "Missing", - root.path().join("missing-repository"), - "restart-after-verified-build", - None, - "restart-required", - ), - root.path().join("missing-deployment-witness"), - )), - enabled: true, - interval_seconds: 30, - }; - let desired = IdunnDesiredDaemonRecord { - daemon_id: target.daemon_id.clone(), - verse_id: target.verse_id.clone(), - name: target.name.clone(), - enabled: true, - health_command: None, - restart_command: target.restart_command.clone(), - deploy_command: target.deploy_command.clone(), - authority: "idunn-supervisor-command".into(), - max_silence_seconds: 60, - observed_at: "2026-07-19T12:00:02Z".into(), - health_contract: target.health_contract.id.clone(), - transport_profile_id: transport_profile_id(&target), - command_boundary_id: command_boundary_id(&target), - }; - - let (health_key, selected, authenticated) = - evaluate_target_health(&target, &options, &lock, &desired, "2026-07-19T12:00:02Z") - .unwrap(); - - assert_eq!(health_key, "test-daemon"); - assert_eq!(selected.state, "active"); - assert_eq!(selected.publication_source, "daemon-authenticated"); - assert!(authenticated.is_some()); - } - - #[test] - fn current_root_authorized_signer_rotation_supersedes_stale_admission() { - let existing = IdunnAuthenticatedDaemonHealthAdmissionRecord { - schema_version: IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA.into(), - daemon_id: "test-daemon".into(), - health_contract: "test.signed-health".into(), - source_runtime_id: "test-runtime".into(), - state: "active".into(), - observed_at_unix_millis: 100, - admitted_at_unix_millis: 101, - trust_binding_id: "root/test-daemon/health".into(), - trust_binding_sha256: format!("sha256-{}", "a".repeat(64)), - signer_identity_id: "old-signer".into(), - publisher_incarnation_id: "old-incarnation".into(), - publisher_sequence: 49, - signed_health_sha256: format!("sha256-{}", "b".repeat(64)), - release_id: None, - release_witness_sha256: None, - source_commit: None, - deployment_id: None, - private_state_exposed: false, - activation_witness_sha256: None, - }; - - let mut same_incarnation = existing.clone(); - same_incarnation.publisher_sequence += 1; - assert!(generic_signed_health_advances(&existing, &same_incarnation)); - same_incarnation.publisher_sequence = existing.publisher_sequence; - assert!(!generic_signed_health_advances( - &existing, - &same_incarnation - )); - - let mut rotated = existing.clone(); - rotated.trust_binding_sha256 = format!("sha256-{}", "c".repeat(64)); - rotated.signer_identity_id = "current-root-signer".into(); - rotated.publisher_incarnation_id = "new-incarnation".into(); - rotated.publisher_sequence = 1; - rotated.observed_at_unix_millis = 102; - rotated.admitted_at_unix_millis = 103; - assert!(generic_signed_health_advances(&existing, &rotated)); - - rotated.observed_at_unix_millis = existing.observed_at_unix_millis; - assert!(!generic_signed_health_advances(&existing, &rotated)); - rotated.observed_at_unix_millis = existing.observed_at_unix_millis - 1; - assert!(!generic_signed_health_advances(&existing, &rotated)); - rotated.observed_at_unix_millis = 102; - rotated.daemon_id = "other-daemon".into(); - assert!(!generic_signed_health_advances(&existing, &rotated)); - } - - fn projection_fixture( - release_bound: bool, - ) -> ( - CommonOptions, - Arc>, - IdunnDesiredDaemonRecord, - AuthenticatedProviderHealthSource, - Arc, - tempfile::TempDir, - ) { - let (message, mut options, root) = - generic_signed_health_fixture_with_release(1, release_bound); - let identity_path = root.path().join("idunn-identity.ccmp"); - let public_path = root.path().join("public-health.cc"); - cultnet_rs::enroll_service_identity_at::(&identity_path).unwrap(); - options.service_identity_store_path = Some(identity_path); - options.public_health_store_path = Some(public_path); - let publisher = initialize_projection_publisher(&options) - .unwrap() - .expect("configured publisher"); - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-19T12:00:01Z").unwrap(); - let desired = IdunnDesiredDaemonRecord { - daemon_id: "test-daemon".into(), - verse_id: "test.local".into(), - name: "Test daemon".into(), - enabled: true, - health_command: None, - restart_command: None, - deploy_command: None, - authority: "idunn-supervisor-command".into(), - max_silence_seconds: 60, - observed_at: "2026-07-19T12:00:02Z".into(), - health_contract: "test.signed-health".into(), - transport_profile_id: "test-transport".into(), - command_boundary_id: "test-command".into(), - }; - let source = - read_fresh_daemon_published_health(&options, &lock, &desired, "2026-07-19T12:00:02Z") - .unwrap() - .unwrap() - .projection_source - .unwrap(); - (options, lock, desired, source, publisher, root) - } - - #[test] - fn runtime_issuance_frozen_trust_snapshot_refuses_same_tuple_signer_rotation() { - let (options, _lock, desired, source, _publisher, _root) = projection_fixture(false); - let candidate = RuntimeTrafficIssuanceCandidate { - grant: runtime_traffic_admission_record_fixture(), - desired, - source, - }; - let trust_path = options.daemon_health_trust_store_path.as_ref().unwrap(); - let snapshot = SingleFileMessagePackBackingStore::new(trust_path) - .pull_all_read_only_snapshot() - .unwrap(); - validate_runtime_traffic_issuance_trust_snapshot(&snapshot, &candidate).unwrap(); - - let mut rotated = candidate.source.binding.clone(); - rotated.signer_public_key = vec![29; 32]; - rotated.signer_identity_id = derive_service_identity_id::( - &rotated.signer_public_key, - ) - .unwrap(); - rotated.bound_at_unix_millis += 1; - rotated.validate().unwrap(); - let mut rotated_snapshot = snapshot; - let binding_envelope = rotated_snapshot - .iter_mut() - .find(|entry| entry.r#type == IdunnDaemonHealthTrustBindingRecord::TYPE) - .unwrap(); - binding_envelope.payload = rmp_serde::to_vec(&rotated).unwrap(); - assert!( - validate_runtime_traffic_issuance_trust_snapshot(&rotated_snapshot, &candidate) - .is_err() - ); - } - - #[test] - fn runtime_issuance_freshness_uses_the_locked_mint_clock() { - let (_options, _lock, desired, source, _publisher, _root) = projection_fixture(false); - let admission = source.admission; - let freshness_limit = - admission.observed_at_unix_millis + u64::from(desired.max_silence_seconds) * 1_000; - validate_runtime_traffic_issuance_freshness( - &desired, - &admission, - ×tamp_from_unix_millis(freshness_limit).unwrap(), - ) - .unwrap(); - assert!( - validate_runtime_traffic_issuance_freshness( - &desired, - &admission, - ×tamp_from_unix_millis(freshness_limit + 1).unwrap(), - ) - .is_err() - ); - assert!( - validate_runtime_traffic_issuance_freshness( - &desired, - &admission, - ×tamp_from_unix_millis(admission.admitted_at_unix_millis - 1).unwrap(), - ) - .is_err() - ); - } - - fn public_projection( - options: &CommonOptions, - ) -> IdunnAuthenticatedProviderHealthProjectionRecord { - let entries = SingleFileMessagePackBackingStore::new( - options.public_health_store_path.as_ref().unwrap(), - ) - .pull_all_read_only_snapshot() - .unwrap(); - assert_eq!(entries.len(), 1); - assert_eq!( - entries[0].r#type, - IdunnAuthenticatedProviderHealthProjectionRecord::TYPE - ); - rmp_serde::from_slice(&entries[0].payload).unwrap() - } - - fn public_query_target(desired: &IdunnDesiredDaemonRecord) -> DaemonTarget { - DaemonTarget { - daemon_id: desired.daemon_id.clone(), - verse_id: desired.verse_id.clone(), - name: desired.name.clone(), - health_contract: health_contract(&desired.health_contract, "failed"), - deploy_command: None, - restart_command: None, - release: None, - enabled: true, - interval_seconds: 30, - } - } - - fn connect_public_query_client( - server_addr: SocketAddr, - ) -> CultNetRudpSocketTransportConnection { - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - socket - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( - "epiphany-test-consumer", - socket, - server_addr, - IDUNN_PUBLIC_HEALTH_QUERY_CONNECTION_ID, - )) - .unwrap(); - client.connect(Vec::new()).unwrap(); - let deadline = Instant::now() + Duration::from_secs(2); - while !client.connected() { - client.receive_once().unwrap(); - client.poll_resends().unwrap(); - assert!( - Instant::now() < deadline, - "public query handshake timed out" - ); - } - client - } - - fn receive_public_query_message( - client: &mut CultNetRudpSocketTransportConnection, - ) -> CultNetMessage { - let deadline = Instant::now() + Duration::from_secs(2); - loop { - if let Some(frame) = client.receive_once().unwrap() { - assert_eq!(frame.channel_id, "schema"); - return decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) - .unwrap(); - } - client.poll_resends().unwrap(); - assert!(Instant::now() < deadline, "public query response timed out"); - } - } - - #[test] - fn public_query_server_returns_exact_allowlisted_bytes_and_correlation() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let public_path = options.public_health_store_path.as_ref().unwrap(); - let stored = SingleFileMessagePackBackingStore::new(public_path) - .pull_all_read_only_snapshot() - .unwrap() - .remove(0); - let server = IdunnPublicHealthSnapshotServer::new( - public_path.clone(), - &[public_query_target(&desired)], - ) - .unwrap(); - let request = CultNetMessage::SnapshotRequest { - message_id: "status-correlation-7".into(), - schema_ids: Some(vec![ - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - ]), - record_keys: Some(vec![stored.key.clone()]), - }; - let CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } = server.serve(&request).unwrap() - else { - panic!("expected raw public health snapshot"); - }; - assert_eq!(message_id, "status-correlation-7"); - assert_eq!(documents.len(), 1); - assert_eq!(documents[0].payload, stored.payload); - assert_eq!( - documents[0].source_runtime_id.as_deref(), - Some(IDUNN_PUBLIC_HEALTH_QUERY_RUNTIME_ID) - ); - assert_eq!( - documents[0].source_role.as_deref(), - Some(IDUNN_PUBLIC_HEALTH_QUERY_ROLE) - ); - - for request in [ - CultNetMessage::SnapshotRequest { - message_id: "unknown-schema".into(), - schema_ids: Some(vec!["private.idunn.state.v0".into()]), - record_keys: None, - }, - CultNetMessage::SnapshotRequest { - message_id: "unknown-key".into(), - schema_ids: None, - record_keys: Some(vec!["provider-health:not-in-target-catalog".into()]), - }, - ] { - let CultNetMessage::SnapshotResponseRaw { documents, .. } = - server.serve(&request).unwrap() - else { - panic!("expected bounded empty response"); - }; - assert!(documents.is_empty()); - } - } - - #[test] - fn public_query_store_contamination_is_fatal_and_private_type_is_never_served() { - let (options, _lock, desired, _source, _publisher, root) = projection_fixture(false); - let private_path = root.path().join("private-in-public.cc"); - let private = IdunnDaemonHealthRecord { - daemon_id: desired.daemon_id.clone(), - state: "active".into(), - detail: "private managed judgment".into(), - observed_at: "2026-07-19T12:00:02Z".into(), - health_contract: desired.health_contract.clone(), - publication_source: "daemon-authenticated".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }; - SingleFileMessagePackBackingStore::new(&private_path) - .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:02Z").unwrap()) - .unwrap(); - assert!( - IdunnPublicHealthSnapshotServer::new(private_path, &[public_query_target(&desired)]) - .is_err() - ); - - let public_path = options.public_health_store_path.as_ref().unwrap(); - let server = IdunnPublicHealthSnapshotServer::new( - public_path.clone(), - &[public_query_target(&desired)], - ) - .unwrap(); - SingleFileMessagePackBackingStore::new(public_path) - .push(&typed_envelope(&private.daemon_id, &private, "2026-07-19T12:00:03Z").unwrap()) - .unwrap(); - assert!( - server - .serve(&CultNetMessage::SnapshotRequest { - message_id: "private-probe".into(), - schema_ids: None, - record_keys: None, - }) - .is_err() - ); - } - - #[test] - fn public_query_rudp_is_multi_peer_and_survives_malformed_request() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let server = IdunnPublicHealthSnapshotServer::new( - options.public_health_store_path.clone().unwrap(), - &[public_query_target(&desired)], - ) - .unwrap(); - let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - let server_addr = socket.local_addr().unwrap(); - socket - .set_read_timeout(Some(Duration::from_millis(100))) - .unwrap(); - let stop = Arc::new(AtomicBool::new(false)); - let worker_stop = Arc::clone(&stop); - let worker = thread::spawn(move || { - run_public_health_query_listener_loop(socket, server, Some(worker_stop)) - }); - - let mut hostile = connect_public_query_client(server_addr); - hostile.send("schema", vec![0xc1]).unwrap(); - assert!(matches!( - receive_public_query_message(&mut hostile), - CultNetMessage::Error { .. } - )); - - let mut first = connect_public_query_client(server_addr); - let mut second = connect_public_query_client(server_addr); - for (client, id) in [(&mut first, "first-peer"), (&mut second, "second-peer")] { - let request = CultNetMessage::SnapshotRequest { - message_id: id.into(), - schema_ids: Some(vec![ - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - ]), - record_keys: Some(vec![provider_health_projection_key( - &desired.daemon_id, - &desired.health_contract, - )]), - }; - client - .send( - "schema", - encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0) - .unwrap(), - ) - .unwrap(); - let CultNetMessage::SnapshotResponseRaw { - message_id, - documents, - } = receive_public_query_message(client) - else { - panic!("expected peer snapshot response"); - }; - assert_eq!(message_id, id); - assert_eq!(documents.len(), 1); - } - stop.store(true, Ordering::Release); - worker.join().unwrap().unwrap(); - } - - #[test] - fn projection_is_signed_canonical_public_state_without_provider_detail() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let projection = public_projection(&options); - assert_eq!(projection.projection_sequence, 1); - assert_eq!( - projection.expires_at_unix_millis, - source.admission.observed_at_unix_millis + 60_000 - ); - assert!( - !rmp_serde::to_vec(&projection) - .unwrap() - .windows(b"signed provider health".len()) - .any(|window| window == b"signed provider health") - ); - let signature = projection.signature.clone(); - let mut unsigned = projection.clone(); - unsigned.signature.clear(); - let anchor = publisher.signer.trust_anchor().unwrap(); - cultnet_rs::verify_service_identity_signature::< - IdunnServiceIdentity, - IdunnAuthenticatedProviderHealthProjectionPurpose, - >( - &anchor, - &rmp_serde::to_vec(&unsigned).unwrap(), - &cultnet_rs::ServiceIdentitySignature { - identity_id: projection.idunn_signer_identity_id.clone(), - signature, - }, - ) - .unwrap(); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") - .unwrap(); - assert_eq!( - public_projection(&options), - projection, - "same source refreshed public state" - ); - } - - #[test] - fn projection_rejoins_trust_and_refuses_rotation_or_source_mutation() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - let trust_path = options.daemon_health_trust_store_path.as_ref().unwrap(); - let backing = SingleFileMessagePackBackingStore::new(trust_path); - let current = backing.pull_all_read_only_snapshot().unwrap().remove(0); - let mut rotated = source.binding.clone(); - rotated.binding_id = "root/test-daemon/health-rotated".into(); - assert!( - backing - .compare_exchange( - &[CultCacheExpectedEnvelope { - key: source.binding.binding_id.clone(), - r#type: IdunnDaemonHealthTrustBindingRecord::TYPE.into(), - current: Some(current), - }], - &[ - typed_envelope(&rotated.binding_id, &rotated, "2026-07-19T12:00:02Z") - .unwrap() - ], - ) - .unwrap() - ); - assert!( - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .is_err() - ); - assert!(!options.public_health_store_path.as_ref().unwrap().exists()); - - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - with_store_node(&options, &lock, |node| { - let mut admission = source.admission.clone(); - admission.publisher_sequence += 1; - node.put(&admission.daemon_id, &admission)?; - Ok(()) - }) - .unwrap(); - assert!( - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .is_err() - ); - assert!(!options.public_health_store_path.as_ref().unwrap().exists()); - } - - #[test] - fn projection_refuses_superseded_deployment_and_does_not_delete_prior_row() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(true); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let prior = public_projection(&options); - with_store_node(&options, &lock, |node| { - let mut head = source.deployment_head.clone().unwrap(); - head.request_id = "deploy-test-2".into(); - head.sequence += 1; - node.put(&head.daemon_id, &head)?; - Ok(()) - }) - .unwrap(); - assert!( - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") - .is_err() - ); - assert_eq!(public_projection(&options), prior); - } - - #[test] - fn projection_store_alias_contamination_and_restart_are_bounded() { - let (mut options, _lock, _desired, _source, _publisher, root) = projection_fixture(false); - options.public_health_store_path = Some(options.store_path.clone()); - assert!(initialize_projection_publisher(&options).is_err()); - - let (options, lock, desired, source, first, second_root) = projection_fixture(false); - first - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let first_projection = public_projection(&options); - let second = initialize_projection_publisher(&options).unwrap().unwrap(); - second - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:03Z") - .unwrap(); - let second_projection = public_projection(&options); - assert_eq!(second_projection.projection_sequence, 2); - assert_ne!( - second_projection.projection_incarnation_id, - first_projection.projection_incarnation_id - ); - - let public_path = options.public_health_store_path.as_ref().unwrap(); - let backing = SingleFileMessagePackBackingStore::new(public_path); - let current_envelope = backing.pull_all_read_only_snapshot().unwrap().remove(0); - let mut corrupted = second_projection.clone(); - corrupted.signature[0] ^= 1; - let corrupted_envelope = CultCacheEnvelope { - payload: rmp_serde::to_vec(&corrupted).unwrap(), - ..current_envelope.clone() - }; - assert!( - backing - .compare_exchange( - &[CultCacheExpectedEnvelope { - key: current_envelope.key.clone(), - r#type: current_envelope.r#type.clone(), - current: Some(current_envelope), - }], - &[corrupted_envelope], - ) - .unwrap() - ); - assert!( - second - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:04Z") - .is_err() - ); - - let contaminated_path = root.path().join("contaminated-public.cc"); - SingleFileMessagePackBackingStore::new(&contaminated_path) - .push(&typed_envelope(&desired.daemon_id, &desired, "2026-07-19T12:00:02Z").unwrap()) - .unwrap(); - let mut contaminated = options.clone(); - contaminated.public_health_store_path = Some(contaminated_path); - let contaminated_publisher = initialize_projection_publisher(&contaminated) - .unwrap() - .unwrap(); - assert!( - contaminated_publisher - .publish_if_current( - &contaminated, - &lock, - &desired, - &source, - "2026-07-19T12:00:04Z", - ) - .is_err() - ); - drop(second_root); - } - - #[test] - fn non_authenticated_paths_cannot_refresh_or_remove_public_projection() { - let (options, lock, desired, source, publisher, _root) = projection_fixture(false); - publisher - .publish_if_current(&options, &lock, &desired, &source, "2026-07-19T12:00:02Z") - .unwrap(); - let prior = public_projection(&options); - publish_authenticated_provider_health( - Some(&publisher), - &options, - &lock, - &desired, - None, - "2026-07-19T12:00:30Z", - ) - .unwrap(); - assert_eq!(public_projection(&options), prior); - } - - #[test] - fn projection_cas_serializes_competing_idunn_incarnations() { - let (options, lock, desired, source, first, _root) = projection_fixture(false); - let second = initialize_projection_publisher(&options).unwrap().unwrap(); - let barrier = Arc::new(std::sync::Barrier::new(3)); - let spawn_publish = |publisher: Arc| { - let options = options.clone(); - let lock = Arc::clone(&lock); - let desired = desired.clone(); - let source = source.clone(); - let barrier = Arc::clone(&barrier); - thread::spawn(move || { - barrier.wait(); - publisher.publish_if_current( - &options, - &lock, - &desired, - &source, - "2026-07-19T12:00:02Z", - ) - }) - }; - let one = spawn_publish(first); - let two = spawn_publish(second); - barrier.wait(); - one.join().unwrap().unwrap(); - two.join().unwrap().unwrap(); - let final_projection = public_projection(&options); - assert_eq!(final_projection.projection_sequence, 2); - } - - #[test] - fn generic_signed_health_requires_canonical_bytes_and_preserves_millisecond_clock() { - let (mut message, options, root) = generic_signed_health_fixture(1); - let mut statement: IdunnSignedDaemonHealthRecord = match &message { - CultNetMessage::DocumentPutRaw { document, .. } => { - rmp_serde::from_slice(&document.payload).unwrap() - } - _ => unreachable!(), - }; - statement.observed_at_unix_millis = - chrono::DateTime::parse_from_rfc3339("2026-07-19T12:00:00.500Z") - .unwrap() - .timestamp_millis() as u64; - statement.signature.clear(); - let signer = cultnet_rs::open_service_identity_at::( - &root.path().join("provider-health-identity.cc"), - ) - .unwrap(); - statement.signature = signer - .sign::(&rmp_serde::to_vec(&statement).unwrap()) - .signature; - let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { - unreachable!() - }; - document.payload = rmp_serde::to_vec(&statement).unwrap(); - - admit_health_from_rudp_message( - &message, - &options, - &Arc::new(Mutex::new(())), - "2026-07-19T12:00:00.500Z", - ) - .unwrap(); - - let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { - unreachable!() - }; - let canonical = rmp_serde::to_vec(&statement).unwrap(); - let string_marker_index = if canonical.first() == Some(&0xdc) { - 3 - } else { - 1 - }; - let string_marker = canonical[string_marker_index]; - assert!((0xa0..=0xbf).contains(&string_marker)); - let string_length = string_marker & 0x1f; - let mut noncanonical = Vec::with_capacity(canonical.len() + 1); - noncanonical.extend_from_slice(&canonical[..string_marker_index]); - noncanonical.extend_from_slice(&[0xd9, string_length]); - noncanonical.extend_from_slice(&canonical[string_marker_index + 1..]); - document.payload = noncanonical; - let error = - authenticate_generic_signed_health(document, &options, "2026-07-19T12:00:00.500Z") - .unwrap_err() - .to_string(); - assert!(error.contains("canonical positional MessagePack")); - } - - fn signed_epiphany_health_fixture( - sequence: u64, - ) -> (CultNetMessage, CommonOptions, tempfile::TempDir) { - let root = tempfile::tempdir().unwrap(); - let identity_path = root.path().join("host.ccmp"); - let store_path = root.path().join("idunn.cc"); - let key = SigningKey::from_bytes(&[23; 32]); - let public_key = key.verifying_key().to_bytes().to_vec(); - let identity_id = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, public_key.as_slice()].concat()) - ); - let identity = EpiphanyHostIdentityWire { - schema_version: HOST_IDENTITY_TYPE.into(), - identity_id: identity_id.clone(), - public_key, - assurance: "test".into(), - identity_created_at: "2026-07-16T00:00:00Z".into(), - source_identity_record_sha256: format!("sha256-{}", "c".repeat(64)), - }; - let mut backing = SingleFileMessagePackBackingStore::new(&identity_path); - backing - .push(&CultCacheEnvelope { - key: HOST_IDENTITY_KEY.into(), - r#type: HOST_IDENTITY_TYPE.into(), - payload: rmp_serde::to_vec(&identity).unwrap(), - stored_at: identity.identity_created_at.clone(), - schema_id: Some(HOST_IDENTITY_TYPE.into()), - }) - .unwrap(); - let mut signed = EpiphanySignedRuntimeHealthWire { - schema_version: EPIPHANY_SIGNED_RUNTIME_HEALTH_SCHEMA_VERSION.into(), - health: IdunnDaemonHealthWireV1 { - daemon_id: "yggdrasil-epiphany".into(), - state: "warming".into(), - detail: "authenticated checkpoint progress".into(), - observed_at: "2026-07-16T00:01:00Z".into(), - health_contract: EPIPHANY_HEALTH_CONTRACT.into(), - publication_source: "daemon-published".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }, - source_runtime_id: EPIPHANY_HEALTH_SOURCE_RUNTIME.into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), - publisher_incarnation_id: "00000000-0000-4000-8000-000000000001".into(), - publisher_sequence: sequence, - publisher_process_id: 42, - publisher_process_creation_token: 7, - publisher_process_created_at: "2026-07-16T00:00:30Z".into(), - publisher_executable_path: "/srv/epiphany/app/current/epiphany-daemon-supervisor" - .into(), - signer_identity_id: identity_id, - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - }; - let statement = rmp_serde::to_vec_named(&signed).unwrap(); - signed.signature = key - .sign(&host_signature_message( - EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, - &statement, - )) - .to_bytes() - .to_vec(); - let message = CultNetMessage::DocumentPutRaw { - message_id: format!("signed-health-{sequence}"), - document: CultNetRawDocumentRecord { - schema_id: EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE.into(), - record_key: signed.health.daemon_id.clone(), - stored_at: signed.health.observed_at.clone(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload: rmp_serde::to_vec_named(&signed).unwrap(), - source_runtime_id: Some(EPIPHANY_HEALTH_SOURCE_RUNTIME.into()), - source_agent_id: None, - source_role: Some("daemon-health-publisher".into()), - tags: Some(vec![CULTNET_RUDP_PROTOCOL_ID.into()]), - }, - }; - let options = CommonOptions { - store_path, - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: Some(identity_path), - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }; - persist_current_deployment_request( - &options, - &Arc::new(Mutex::new(())), - &IdunnDeploymentRequestRecord { - request_id: "deploy:yggdrasil-epiphany:fixture".into(), - daemon_id: "yggdrasil-epiphany".into(), - command: "fixture".into(), - authority: "idunn-supervisor-command.manual".into(), - requested_at: "unix:1784160000".into(), - repository_full_name: "GameCult/Epiphany".into(), - upstream_ref: "refs/heads/codex/epiphany-shakedown-live".into(), - source_revision: "b".repeat(40), - release_authority_id: String::new(), - release_authority_envelope_sha256: String::new(), - requires_bifrost_authority: false, - }, - ) - .unwrap(); - (message, options, root) - } - - #[test] - fn signed_epiphany_health_requires_pinned_identity_and_advancing_sequence() { - let (message, options, _root) = signed_epiphany_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - let admitted = - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:01Z") - .unwrap(); - assert_eq!(admitted.state, "warming"); - validate_health_admission_at( - &HealthAdmissionValidationOptions { - daemon_id: "yggdrasil-epiphany".into(), - deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: None, - write_admission_witness: None, - runtime_process: None, - }, - &options, - "2026-07-16T00:01:02Z", - ) - .unwrap_err(); - let mut active_message = message.clone(); - let CultNetMessage::DocumentPutRaw { document, .. } = &mut active_message else { - unreachable!() - }; - let mut active: EpiphanySignedRuntimeHealthWire = - rmp_serde::from_slice(&document.payload).unwrap(); - active.health.state = "active".into(); - active.publisher_sequence = 2; - active.signature.clear(); - let key = SigningKey::from_bytes(&[23; 32]); - let statement = rmp_serde::to_vec_named(&active).unwrap(); - active.signature = key - .sign(&host_signature_message( - EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, - &statement, - )) - .to_bytes() - .to_vec(); - document.payload = rmp_serde::to_vec_named(&active).unwrap(); - admit_health_from_rudp_message(&active_message, &options, &lock, "2026-07-16T00:01:02Z") - .unwrap(); - validate_health_admission_at( - &HealthAdmissionValidationOptions { - daemon_id: "yggdrasil-epiphany".into(), - deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: None, - write_admission_witness: None, - runtime_process: None, - }, - &options, - "2026-07-16T00:01:03Z", - ) - .unwrap(); - assert!( - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:03Z",) - .is_err() - ); - - let mut newer = with_store_node(&options, &lock, |node| { - node.get::("deploy:yggdrasil-epiphany:fixture")? - .ok_or_else(|| anyhow!("fixture request missing")) - }) - .unwrap(); - newer.request_id = "deploy:yggdrasil-epiphany:aaa".into(); - newer.requested_at = "unix:1784160000".into(); - let live_error = persist_current_deployment_request(&options, &lock, &newer) - .unwrap_err() - .to_string(); - assert!(live_error.contains("remains live; refusing supersession")); - with_store_node(&options, &lock, |node| { - node.put( - "result:deploy:yggdrasil-epiphany:fixture", - &IdunnDeploymentResultRecord { - result_id: "result:deploy:yggdrasil-epiphany:fixture".into(), - request_id: "deploy:yggdrasil-epiphany:fixture".into(), - daemon_id: "yggdrasil-epiphany".into(), - state: "succeeded".into(), - detail: "fixture deployment completed".into(), - completed_at: "unix:1784160001".into(), - }, - )?; - Ok(()) - }) - .unwrap(); - persist_current_deployment_request(&options, &lock, &newer).unwrap(); - let stale_error = validate_health_admission_at( - &HealthAdmissionValidationOptions { - daemon_id: "yggdrasil-epiphany".into(), - deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: None, - write_admission_witness: None, - runtime_process: None, - }, - &options, - "2026-07-16T00:02:00Z", - ) - .unwrap_err() - .to_string(); - assert!(stale_error.contains("superseded deployment request")); - assert!( - admit_health_from_rudp_message( - &active_message, - &options, - &lock, - "2026-07-16T00:02:00Z", - ) - .is_err() - ); - - let (mut alien, alien_options, _alien_root) = signed_epiphany_health_fixture(2); - if let CultNetMessage::DocumentPutRaw { document, .. } = &mut alien { - document.source_runtime_id = Some("alien-runtime".into()); - } - assert!(health_from_rudp_message(&alien, &alien_options).is_err()); - - let (mut forged, forged_options, _forged_root) = signed_epiphany_health_fixture(3); - let CultNetMessage::DocumentPutRaw { document, .. } = &mut forged else { - unreachable!() - }; - let mut forged_wire: EpiphanySignedRuntimeHealthWire = - rmp_serde::from_slice(&document.payload).unwrap(); - forged_wire.signature[0] ^= 1; - document.payload = rmp_serde::to_vec_named(&forged_wire).unwrap(); - assert!(health_from_rudp_message(&forged, &forged_options).is_err()); - } - - #[test] - fn startup_terminalizes_interrupted_deployment_before_supersession() { - let (_message, options, _root) = signed_epiphany_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - let prior = with_store_node(&options, &lock, |node| { - node.get::("deploy:yggdrasil-epiphany:fixture")? - .ok_or_else(|| anyhow!("fixture request missing")) - }) - .unwrap(); - - assert_eq!( - terminalize_interrupted_deployment_requests(&options, &lock, "2026-07-16T00:02:00Z",) - .unwrap(), - 1 - ); - let result = with_store_node(&options, &lock, |node| { - node.get::("result:deploy:yggdrasil-epiphany:fixture")? - .ok_or_else(|| anyhow!("recovered result missing")) - }) - .unwrap(); - assert_eq!(result.request_id, prior.request_id); - assert_eq!(result.daemon_id, prior.daemon_id); - assert_eq!(result.state, "failed"); - assert_eq!(result.completed_at, "2026-07-16T00:02:00Z"); - assert!( - result - .detail - .contains("owning Idunn daemon incarnation stopped") - ); - assert_eq!( - terminalize_interrupted_deployment_requests(&options, &lock, "2026-07-16T00:03:00Z",) - .unwrap(), - 0 - ); - - let mut successor = prior; - successor.request_id = "deploy:yggdrasil-epiphany:successor".into(); - successor.requested_at = "2026-07-16T00:04:00Z".into(); - let head = persist_current_deployment_request(&options, &lock, &successor).unwrap(); - assert_eq!(head.request_id, successor.request_id); - assert_eq!(head.sequence, 2); - } - - #[test] - fn pinned_epiphany_health_identity_load_is_read_only() { - let (_message, options, _root) = signed_epiphany_health_fixture(1); - let identity_path = options - .trusted_epiphany_health_identity_store - .as_deref() - .expect("fixture trust anchor"); - let lock_path = identity_path.with_file_name(format!( - "{}.lock", - identity_path - .file_name() - .expect("trust anchor filename") - .to_string_lossy() - )); - if lock_path.exists() { - std::fs::remove_file(&lock_path).unwrap(); - } - let mut permissions = std::fs::metadata(identity_path).unwrap().permissions(); - permissions.set_readonly(true); - std::fs::set_permissions(identity_path, permissions).unwrap(); - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions( - identity_path.parent().unwrap(), - std::fs::Permissions::from_mode(0o555), - ) - .unwrap(); - } - - let identity = load_epiphany_health_identity(identity_path).unwrap(); - - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions( - identity_path.parent().unwrap(), - std::fs::Permissions::from_mode(0o755), - ) - .unwrap(); - } - - assert_eq!(identity.schema_version, HOST_IDENTITY_TYPE); - assert!(!lock_path.exists()); - } - - #[test] - fn unsigned_epiphany_health_is_rejected() { - let (mut message, options, _root) = signed_epiphany_health_fixture(1); - let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { - unreachable!() - }; - let signed: EpiphanySignedRuntimeHealthWire = - rmp_serde::from_slice(&document.payload).unwrap(); - document.schema_id = "idunn.daemon_health".into(); - document.payload = rmp_serde::to_vec(&signed.health).unwrap(); - assert!(health_from_rudp_message(&message, &options).is_err()); - } - - #[test] - fn signed_admission_must_remain_the_current_health_observation() { - let (message, options, _root) = signed_epiphany_health_fixture(1); - let lock = Arc::new(Mutex::new(())); - admit_health_from_rudp_message(&message, &options, &lock, "2026-07-16T00:01:01Z").unwrap(); - with_store_node(&options, &lock, |node| { - let mut health = node - .get::("yggdrasil-epiphany")? - .unwrap(); - health.state = "failed".into(); - health.observed_at = "2026-07-16T00:01:02Z".into(); - node.put(&health.daemon_id, &health)?; - Ok(()) - }) - .unwrap(); - let error = validate_health_admission_at( - &HealthAdmissionValidationOptions { - daemon_id: "yggdrasil-epiphany".into(), - deployment_request_id: "deploy:yggdrasil-epiphany:fixture".into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: None, - write_admission_witness: None, - runtime_process: None, - }, - &options, - "2026-07-16T00:01:03Z", - ) - .unwrap_err() - .to_string(); - assert!(error.contains("not the current daemon health observation")); - } - - #[test] - fn generic_health_cannot_substitute_for_signed_admission() { - let root = tempfile::tempdir().unwrap(); - let options = CommonOptions { - store_path: root.path().join("idunn.cc"), - release_authority_store_path: None, - deployment_brake_store_path: None, - deployment_brake_operator_anchor_path: None, - deployment_brake_runtime_id: None, - operator_alarm_command: None, - rudp_health_bind: None, - trusted_epiphany_health_identity_store: None, - daemon_health_trust_store_path: None, - service_identity_store_path: None, - public_health_store_path: None, - public_health_query_bind: None, - execute: false, - command_timeout_seconds: 1, - }; - let lock = Arc::new(Mutex::new(())); - with_store_node(&options, &lock, |node| { - node.put( - "yggdrasil-epiphany", - &IdunnDaemonHealthRecord { - daemon_id: "yggdrasil-epiphany".into(), - state: "active".into(), - detail: "counterfeit generic observation".into(), - observed_at: "2026-07-16T00:01:00Z".into(), - health_contract: EPIPHANY_HEALTH_CONTRACT.into(), - publication_source: "daemon-published".into(), - transport: CULTNET_RUDP_PROTOCOL_ID.into(), - }, - )?; - Ok(()) - }) - .unwrap(); - let error = validate_health_admission( - &HealthAdmissionValidationOptions { - daemon_id: "yggdrasil-epiphany".into(), - deployment_request_id: "counterfeit".into(), - release_id: "release-test".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - activation_witness_sha256: None, - write_admission_witness: None, - runtime_process: None, - }, - &options, - ) - .unwrap_err() - .to_string(); - assert!(error.contains("no signed health admission")); - } - - #[test] - fn signed_admission_freshness_rejects_stale_future_and_inverted_time() { - let mut admission = IdunnSignedHealthAdmissionRecord { - daemon_id: "yggdrasil-epiphany".into(), - state: "active".into(), - observed_at: "unix:100".into(), - admitted_at: "unix:101".into(), - health_contract: EPIPHANY_HEALTH_CONTRACT.into(), - deployment_request_id: "request".into(), - release_id: "release".into(), - release_witness_sha256: format!("sha256-{}", "a".repeat(64)), - source_commit: "b".repeat(40), - publisher_incarnation_id: "00000000-0000-4000-8000-000000000001".into(), - publisher_sequence: 1, - publisher_process_created_at: "2026-07-16T00:00:00Z".into(), - signer_identity_id: "signer".into(), - signed_health_sha256: format!("sha256-{}", "c".repeat(64)), - }; - validate_admission_fresh_at(&admission, "unix:280", 180).unwrap(); - assert!(validate_admission_fresh_at(&admission, "unix:282", 180).is_err()); - admission.observed_at = "unix:102".into(); - assert!(validate_admission_fresh_at(&admission, "unix:102", 180).is_err()); - admission.observed_at = "unix:100".into(); - admission.admitted_at = "unix:103".into(); - assert!(validate_admission_fresh_at(&admission, "unix:102", 180).is_err()); - } - - #[test] - fn concurrent_signed_health_writers_cannot_regress_sequence() { - let (message_one, options, _root) = signed_epiphany_health_fixture(1); - let (mut message_two, _, _) = signed_epiphany_health_fixture(2); - // Re-sign sequence two with the trust anchor belonging to the shared fixture. - let CultNetMessage::DocumentPutRaw { document, .. } = &mut message_two else { - unreachable!() - }; - let mut signed: EpiphanySignedRuntimeHealthWire = - rmp_serde::from_slice(&document.payload).unwrap(); - signed.signer_identity_id = match &message_one { - CultNetMessage::DocumentPutRaw { document, .. } => { - let first: EpiphanySignedRuntimeHealthWire = - rmp_serde::from_slice(&document.payload).unwrap(); - first.signer_identity_id - } - _ => unreachable!(), - }; - signed.signature.clear(); - let key = SigningKey::from_bytes(&[23; 32]); - let statement = rmp_serde::to_vec_named(&signed).unwrap(); - signed.signature = key - .sign(&host_signature_message( - EPIPHANY_SIGNED_RUNTIME_HEALTH_TYPE, - &statement, - )) - .to_bytes() - .to_vec(); - document.payload = rmp_serde::to_vec_named(&signed).unwrap(); - - let barrier = Arc::new(std::sync::Barrier::new(3)); - let mut workers = Vec::new(); - for message in [message_one, message_two] { - let options = options.clone(); - let barrier = barrier.clone(); - workers.push(std::thread::spawn(move || { - barrier.wait(); - admit_health_from_rudp_message( - &message, - &options, - &Arc::new(Mutex::new(())), - "2026-07-16T00:01:01Z", - ) - })); - } - barrier.wait(); - for worker in workers { - let _ = worker.join().unwrap(); - } - let final_admission = with_store_node(&options, &Arc::new(Mutex::new(())), |node| { - node.get::("yggdrasil-epiphany")? - .ok_or_else(|| anyhow!("final admission missing")) - }) - .unwrap(); - assert_eq!(final_admission.publisher_sequence, 2); - } +fn main() -> anyhow::Result<()> { + idunn_daemon::control_plane::run(std::env::args().skip(1)) } diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index 87d0b0f..e45e039 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit 87d0b0f2c829ef2e7defa2bf774f1e3b9cc6a1d0 +Subproject commit e45e039ebecb005fad65602b7822b7b8a37f2428 From 92d2b27e15336f5c385956e5d1646bf84b2e04a7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Sep 2026 23:54:20 +0200 Subject: [PATCH 78/93] Harden Idunn native authority drivers --- crates/idunn-daemon/src/control_plane.rs | 8 +- crates/idunn-daemon/src/deployment.rs | 172 +-- crates/idunn-daemon/src/deployment_plan.rs | 6 +- crates/idunn-daemon/src/drivers.rs | 1301 ++++++++++++++++++-- 4 files changed, 1277 insertions(+), 210 deletions(-) diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs index 6ca48f2..a668807 100644 --- a/crates/idunn-daemon/src/control_plane.rs +++ b/crates/idunn-daemon/src/control_plane.rs @@ -205,7 +205,13 @@ fn parse_serve(mut args: impl Iterator) -> Result { } ensure!(options.poll_millis > 0, "--poll-millis must be positive"); options.source_identity = match (source_uid, source_gid) { - (Some(uid), Some(gid)) => Some(ProcessIdentity { uid, gid }), + (Some(uid), Some(gid)) => { + ensure!( + uid > 0 && gid > 0, + "source UID and GID must be unprivileged" + ); + Some(ProcessIdentity { uid, gid }) + } (None, None) => None, _ => bail!("--source-uid and --source-gid must be supplied together"), }; diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index 6376d73..d4fca8a 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -262,8 +262,6 @@ pub struct OperatorBinding { #[serde(default)] pub process_write_lease: Option, pub brakes: BrakeBinding, - #[serde(default)] - pub state_transition: Option, pub rollout: RolloutBinding, pub placement: PlacementBinding, #[serde(default)] @@ -306,6 +304,7 @@ pub struct GitlinkBinding { pub struct RunnerBinding { pub driver: RunnerDriver, pub image: String, + pub user: String, #[serde(default)] pub affordances: BTreeSet, pub cache_root: Option, @@ -319,6 +318,8 @@ pub struct RunnerBinding { pub network_profile: Option, pub memory_mebibytes: u32, pub cpu_quota_percent: u32, + pub pids_limit: u32, + pub tmpfs_mebibytes: u32, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -406,22 +407,6 @@ pub struct BrakeBinding { pub lifecycle_store: PathBuf, } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct StateTransitionBinding { - pub policy: StateTransitionPolicy, - pub archive_root: Option, - pub backup_root: Option, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StateTransitionPolicy { - Preserve, - FencedMigration, - FreshRoot, -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct RouteBinding { @@ -810,6 +795,7 @@ impl OperatorBinding { for (id, runner) in &self.runners { require_id(id, "runner binding")?; require_pinned_image(&runner.image)?; + require_container_identity(&runner.user)?; ensure!( runner.memory_mebibytes > 0, "runner memory must be positive" @@ -818,6 +804,10 @@ impl OperatorBinding { (1..=100_000).contains(&runner.cpu_quota_percent), "runner CPU quota is outside 1..=100000" ); + ensure!( + runner.pids_limit > 0 && runner.tmpfs_mebibytes > 0, + "runner process or temporary-filesystem limit is zero" + ); if let Some(cache_root) = runner.cache_root.as_ref() { require_absolute_path(cache_root, "runner cache root")?; } @@ -859,6 +849,11 @@ impl OperatorBinding { } require_identity(&self.workload.user, "workload user")?; require_identity(&self.workload.group, "workload group")?; + ensure!( + !matches!(self.workload.user.as_str(), "root" | "0") + && !matches!(self.workload.group.as_str(), "root" | "0"), + "ordinary Idunn workloads cannot run as root" + ); require_id(&self.workload.unit_prefix, "workload unit prefix")?; require_id(&self.runtime_identity.runtime_id, "runtime id")?; require_id( @@ -914,6 +909,49 @@ impl OperatorBinding { ); } } + let mut authority_paths = vec![ + self.brakes.deployment_store.clone(), + self.brakes.lifecycle_store.clone(), + ]; + if let Some(write_lease) = &self.process_write_lease { + authority_paths.push(write_lease.record_path.clone()); + authority_paths.push(write_lease.lock_path()); + } + ensure!( + authority_paths.iter().enumerate().all(|(index, path)| { + authority_paths[index + 1..] + .iter() + .all(|other| !paths_overlap(path, other)) + }), + "Idunn authority paths overlap" + ); + let mut protected_paths = authority_paths; + protected_paths.extend([ + self.workload.release_root.clone(), + self.workload.runtime_root.clone(), + self.runtime_identity.trust_anchor_store.clone(), + ]); + if let Some(route) = &self.route { + protected_paths.push(route.config_path.clone()); + } + if let Some(release_authority) = &self.repository.release_authority_store { + protected_paths.push(release_authority.clone()); + } + for authority_path in protected_paths { + for writable_path in self + .workload + .read_write_paths + .iter() + .chain(self.workload.state_root.iter()) + { + ensure!( + !paths_overlap(&authority_path, writable_path), + "workload writable path {} overlaps Idunn authority {}", + writable_path.display(), + authority_path.display() + ); + } + } for capability in &self.workload.capabilities { require_value(capability, "workload capability")?; } @@ -946,31 +984,12 @@ impl OperatorBinding { "private port range starts at zero" ); ensure!( - route.private_port_start <= route.private_port_end, - "private port range is inverted" + route.private_port_start < route.private_port_end, + "candidate rollout requires at least two private ports" ); require_absolute_path(&route.config_path, "route config")?; require_unit(&route.reload_unit, "route reload unit")?; } - if let Some(transition) = &self.state_transition { - match transition.policy { - StateTransitionPolicy::Preserve => {} - StateTransitionPolicy::FencedMigration => ensure!( - transition.backup_root.is_some(), - "fenced migration has no backup root" - ), - StateTransitionPolicy::FreshRoot => ensure!( - transition.archive_root.is_some(), - "fresh-root transition has no whole-root archive" - ), - } - if let Some(path) = transition.archive_root.as_ref() { - require_absolute_path(path, "state archive root")?; - } - if let Some(path) = transition.backup_root.as_ref() { - require_absolute_path(path, "state backup root")?; - } - } ensure!( self.rollout.retain_releases >= 2, "candidate rollout must retain at least current and prior releases" @@ -1055,12 +1074,17 @@ impl OperatorBinding { self.workload.state_root.is_some() == declaration.state.is_some(), "state root binding must exist exactly when the recipe declares state" ); - ensure!( - self.state_transition.is_some() == declaration.state.is_some(), - "state-transition binding must exist exactly when the recipe declares state" - ); for step in &declaration.steps { let binding = &self.runners[&step.runner]; + ensure!( + !binding + .environment + .contains_key(&declaration.source_stamp_environment) + && !binding + .secret_files + .contains_key(&declaration.source_stamp_environment), + "runner cannot replace the Idunn source stamp" + ); ensure!( binding.affordances.contains(&RunnerAffordance::SourceRead), "runner {} cannot read the frozen source", @@ -1198,20 +1222,6 @@ impl OperatorBinding { (false, _, None) => {} _ => bail!("route binding does not match the launch contract"), } - if let Some(transition) = &self.state_transition { - match transition.policy { - StateTransitionPolicy::Preserve => {} - StateTransitionPolicy::FencedMigration => ensure!( - declaration - .state - .as_ref() - .and_then(|state| state.migration.as_ref()) - .is_some(), - "binding requests migration but target declares no migrator" - ), - StateTransitionPolicy::FreshRoot => {} - } - } for dependency in declaration .dependencies .iter() @@ -1411,6 +1421,16 @@ fn require_pinned_image(value: &str) -> Result<()> { require_sha256(digest, "runner image digest") } +fn require_container_identity(value: &str) -> Result<()> { + let (uid, gid) = value + .split_once(':') + .context("runner user must be an explicit numeric UID:GID")?; + let uid: u32 = uid.parse().context("runner UID is not a u32")?; + let gid: u32 = gid.parse().context("runner GID is not a u32")?; + ensure!(uid > 0 && gid > 0, "runner identity must be unprivileged"); + Ok(()) +} + fn require_identity(value: &str, label: &str) -> Result<()> { require_id(value, label) } @@ -1529,12 +1549,15 @@ recipe_path = "deployment/idunn/recipe.toml" [runners.rust] driver = "docker" image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" +user = "1000:1000" affordances = ["source-read", "artifact-write", "build-cache"] cache_root = "/srv/ghostlight/build-cache" allowed_programs = ["cargo"] network_profile = "build-dependency-egress" memory_mebibytes = 8192 cpu_quota_percent = 400 +pids_limit = 512 +tmpfs_mebibytes = 1024 [workload] driver = "systemd-transient" @@ -1577,10 +1600,6 @@ record_path = "/etc/gamecult/test-service/runtime/process-write-lease.cc" deployment_store = "/var/lib/gamecult/idunn-authority/test-service-deployment-brake.cc" lifecycle_store = "/var/lib/gamecult/idunn-authority/test-service-lifecycle-brake.cc" -[state_transition] -policy = "fresh-root" -archive_root = "/var/lib/gamecult/test-service-cold-archive" - [rollout] strategy = "candidate-then-promote" drain_seconds = 30 @@ -1635,6 +1654,17 @@ nodes = ["yggdrasil"] assert!(OperatorBinding::parse(&affordance_without_secret).is_err()); } + #[test] + fn runner_cannot_replace_the_source_stamp() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let forged = BINDING.replace( + "[workload]", + "[runners.rust.environment]\nTEST_SERVICE_BUILD_COMMIT = \"forged\"\n\n[workload]", + ); + let binding = OperatorBinding::parse(&forged).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + #[test] fn route_transport_mismatch_is_rejected() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); @@ -1670,21 +1700,6 @@ nodes = ["yggdrasil"] assert!(binding.admit(&recipe).is_err()); } - #[test] - fn fresh_root_keeps_slot_preservation_semantics_inside_the_new_state_root() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let binding = OperatorBinding::parse(BINDING).unwrap(); - binding.admit(&recipe).unwrap(); - assert_eq!( - recipe.state.as_ref().unwrap().slots[0].recovery, - StateRecovery::Preserve - ); - assert_eq!( - binding.state_transition.as_ref().unwrap().policy, - StateTransitionPolicy::FreshRoot - ); - } - #[test] fn state_slot_path_cannot_escape_the_operator_owned_root() { let input = RECIPE.replace( @@ -1792,15 +1807,10 @@ nodes = ["yggdrasil"] .replace( "[process_write_lease]\nrecord_path = \"/etc/gamecult/test-service/runtime/process-write-lease.cc\"\n\n", "", - ) - .replace( - "[state_transition]\npolicy = \"fresh-root\"\narchive_root = \"/var/lib/gamecult/test-service-cold-archive\"\n\n", - "", ); let binding = OperatorBinding::parse(&binding_text).unwrap(); binding.admit(&recipe).unwrap(); assert!(binding.workload.state_root.is_none()); - assert!(binding.state_transition.is_none()); } #[test] diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index e50db70..88066c8 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -1026,11 +1026,14 @@ recipe_path = "deployment/idunn/recipe.toml" [runners.rust] driver = "docker" image = "rust@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +user = "1000:1000" affordances = ["source-read", "artifact-write"] allowed_programs = ["cargo"] network_profile = "build-dependency-egress" memory_mebibytes = 2048 cpu_quota_percent = 200 +pids_limit = 512 +tmpfs_mebibytes = 512 [workload] driver = "systemd-transient" @@ -1067,9 +1070,6 @@ reload_unit = "nginx.service" deployment_store = "/var/lib/gamecult/idunn/service-deployment-brake.cc" lifecycle_store = "/var/lib/gamecult/idunn/service-lifecycle-brake.cc" -[state_transition] -policy = "preserve" - [rollout] strategy = "candidate-then-promote" drain_seconds = 30 diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs index 52c189c..319bf44 100644 --- a/crates/idunn-daemon/src/drivers.rs +++ b/crates/idunn-daemon/src/drivers.rs @@ -69,26 +69,46 @@ pub trait RunnerPort { pub struct MaterializedRelease { pub release: SealedRelease, pub root: PathBuf, - pub artifacts: Vec, - pub external_inputs: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct InstalledReleaseObservation { + pub sealed_release_id: String, + pub root: PathBuf, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct WorkloadObservation { pub unit: String, + pub unit_description: String, pub invocation_id: String, pub main_pid: u32, pub process_start_time: u64, pub executable: PathBuf, pub executable_sha256: String, pub runtime_instance_id: String, + pub user: String, + pub group: String, + pub working_directory: PathBuf, + pub runtime_bundle: PathBuf, + pub command_line_sha256: String, + pub environment_names: Vec, + pub environment_contract_sha256: String, + pub control_group: String, } pub trait WorkloadPort { - fn start( + fn install( &self, plan: &CompiledDeploymentPlan, release: &MaterializedRelease, + ) -> Result; + + fn start( + &self, + plan: &CompiledDeploymentPlan, + release: &SealedRelease, + installed: &InstalledReleaseObservation, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, ) -> Result; @@ -138,10 +158,22 @@ pub struct PresenceObservation { } pub trait WriteLeasePort { - fn fence(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()>; - fn grant(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; - fn revoke(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result<()>; - fn observe(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; + fn revoke_exact(&self, incumbent: Option<&IdunnProcessWriteLeaseRecord>) -> Result<()>; + fn observe_empty(&self) -> Result; + fn grant( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result; + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result; } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -152,6 +184,11 @@ pub struct RouteObservation { } pub trait RoutePort { + fn preflight( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + ) -> Result<()>; fn promote( &self, expected: &IdunnExpectedIncarnationRecord, @@ -178,7 +215,7 @@ impl GitSourceDriver { Self { source_root: source_root.into(), identity, - git_program: PathBuf::from("git"), + git_program: PathBuf::from("/usr/bin/git"), } } @@ -187,8 +224,20 @@ impl GitSourceDriver { I: IntoIterator, S: AsRef, { + ensure!( + self.git_program.is_absolute(), + "Git source driver program is not absolute" + ); let mut command = Command::new(&self.git_program); - command.args(args).stdin(Stdio::null()); + command + .args(args) + .stdin(Stdio::null()) + .env_clear() + .env("HOME", self.source_root.join(".home")) + .env("PATH", "/usr/bin:/bin") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_TERMINAL_PROMPT", "0") + .env("LANG", "C.UTF-8"); apply_identity(&mut command, self.identity)?; let output = command.output().with_context(|| { format!( @@ -432,6 +481,7 @@ impl SourcePort for GitSourceDriver { "repository checkout is outside Idunn's source authority root" ); ensure_source_directory(&self.source_root, self.identity)?; + ensure_source_directory(&self.source_root.join(".home"), self.identity)?; ensure_source_directory_tree( &self.source_root, binding @@ -521,7 +571,7 @@ pub struct DockerRunnerDriver { impl Default for DockerRunnerDriver { fn default() -> Self { Self { - docker_program: PathBuf::from("docker"), + docker_program: PathBuf::from("/usr/bin/docker"), } } } @@ -532,6 +582,10 @@ impl DockerRunnerDriver { I: IntoIterator, S: AsRef, { + ensure!( + self.docker_program.is_absolute(), + "Docker runner program is not absolute" + ); let output = Command::new(&self.docker_program) .args(args) .stdin(Stdio::null()) @@ -553,6 +607,7 @@ impl DockerRunnerDriver { workspace: &Path, working_directory: &Path, argv: &[String], + required_environment: &std::collections::BTreeSet, source_stamp: (&str, &str), ) -> Result<()> { ensure!(!argv.is_empty(), "runner command is empty"); @@ -571,19 +626,23 @@ impl DockerRunnerDriver { OsString::from("--env"), OsString::from(format!("{}={}", source_stamp.0, source_stamp.1)), ]); - for (name, value) in &runner.environment { - args.push(OsString::from("--env")); - args.push(OsString::from(format!("{name}={value}"))); - } - for (name, path) in &runner.secret_files { - args.push(OsString::from("--env")); - args.push(OsString::from(format!("{name}=/run/idunn/secrets/{name}"))); - args.push(OsString::from("--mount")); - args.push(bind_mount( - path, - &format!("/run/idunn/secrets/{name}"), - true, - )?); + for name in required_environment { + if let Some(value) = runner.environment.get(name) { + args.push(OsString::from("--env")); + args.push(OsString::from(format!("{name}={value}"))); + } else if let Some(path) = runner.secret_files.get(name) { + validate_runner_secret(path, container_identity(&runner.user)?)?; + args.push(OsString::from("--env")); + args.push(OsString::from(format!("{name}=/run/idunn/secrets/{name}"))); + args.push(OsString::from("--mount")); + args.push(bind_mount( + path, + &format!("/run/idunn/secrets/{name}"), + true, + )?); + } else { + bail!("runner lacks declared step environment {name}") + } } args.push(runner.image.clone().into()); args.extend(argv.iter().map(OsString::from)); @@ -592,6 +651,7 @@ impl DockerRunnerDriver { } fn base_run_args(&self, runner: &RunnerBinding, workspace: &Path) -> Result> { + let identity = container_identity(&runner.user)?; let mut args = vec![ OsString::from("run"), OsString::from("--rm"), @@ -606,10 +666,23 @@ impl DockerRunnerDriver { )), OsString::from("--mount"), bind_mount(workspace, "/workspace", false)?, + OsString::from("--user"), + OsString::from(&runner.user), + OsString::from("--cap-drop"), + OsString::from("ALL"), + OsString::from("--security-opt"), + OsString::from("no-new-privileges"), + OsString::from("--read-only"), + OsString::from("--pids-limit"), + OsString::from(runner.pids_limit.to_string()), + OsString::from("--tmpfs"), + OsString::from(format!( + "/tmp:rw,nosuid,nodev,noexec,size={}m", + runner.tmpfs_mebibytes + )), ]; if let Some(cache_root) = &runner.cache_root { - fs::create_dir_all(cache_root) - .with_context(|| format!("creating runner cache {}", cache_root.display()))?; + ensure_runner_cache_root(cache_root, identity)?; args.push(OsString::from("--mount")); args.push(bind_mount(cache_root, "/cache", false)?); } @@ -752,6 +825,24 @@ impl RunnerPort for DockerRunnerDriver { workspaces.insert(runner_id.clone(), workspace); } + for input in &declaration.external_inputs { + let workspace = &workspaces[&input.runner]; + let destination = workspace.join(&input.destination); + ensure!( + destination.starts_with(workspace), + "external input escaped its runner workspace" + ); + if let Some(parent) = destination.parent() { + fs::create_dir_all(parent)?; + } + } + for (runner_id, workspace) in &workspaces { + assign_runner_tree( + workspace, + container_identity(&binding.runners[runner_id].user)?, + )?; + } + let mut external_inputs = Vec::new(); for input in &declaration.external_inputs { external_inputs.push(self.materialize_external_input( @@ -777,6 +868,7 @@ impl RunnerPort for DockerRunnerDriver { workspace, &step.working_directory, &step.argv, + &step.required_environment, ( &declaration.source_stamp_environment, &source.facts.revision, @@ -806,8 +898,6 @@ impl RunnerPort for DockerRunnerDriver { Ok(MaterializedRelease { release, root: staging_root.to_owned(), - artifacts, - external_inputs, }) } } @@ -862,14 +952,14 @@ impl SystemdTransientWorkloadDriver { suffix.len() == 64 && suffix.bytes().all(|byte| byte.is_ascii_hexdigit()), "runtime instance id is not a SHA-256 digest" ); - Ok(format!("{prefix}-{}.service", &suffix[..16])) + Ok(format!("{prefix}-{suffix}.service")) } fn install_release( &self, plan: &CompiledDeploymentPlan, release: &MaterializedRelease, - ) -> Result { + ) -> Result { release.release.validate_against(plan)?; let (_, binding) = plan.parsed_inputs()?; let release_root = &binding.workload.release_root; @@ -895,7 +985,7 @@ impl SystemdTransientWorkloadDriver { } } } - for artifact in &release.artifacts { + for artifact in &release.release.artifacts { let path = installed.join(&artifact.destination); let (sha256, size_bytes) = digest_artifact(&path)?; ensure!( @@ -903,11 +993,41 @@ impl SystemdTransientWorkloadDriver { "installed artifact {} differs from its sealed receipt", artifact.artifact_id ); - if artifact.executable { - set_executable_read_only(&path)?; - } } - Ok(installed) + harden_installed_release(&installed, &release.release.artifacts)?; + Ok(InstalledReleaseObservation { + sealed_release_id: release.release.sealed_release_id.clone(), + root: installed, + }) + } + + fn validate_installed_release( + &self, + plan: &CompiledDeploymentPlan, + release: &SealedRelease, + installed: &InstalledReleaseObservation, + ) -> Result<()> { + release.validate_against(plan)?; + let (_, binding) = plan.parsed_inputs()?; + ensure!( + installed.sealed_release_id == release.sealed_release_id + && installed.root + == binding + .workload + .release_root + .join(&release.sealed_release_id), + "installed release observation belongs to another sealed release" + ); + for artifact in &release.artifacts { + let path = installed.root.join(&artifact.destination); + let (sha256, size_bytes) = digest_artifact(&path)?; + ensure!( + format!("sha256-{sha256}") == artifact.sha256 && size_bytes == artifact.size_bytes, + "installed artifact {} differs from its sealed receipt", + artifact.artifact_id + ); + } + harden_installed_release(&installed.root, &release.artifacts) } fn prepare_runtime_bundle( @@ -942,6 +1062,7 @@ impl SystemdTransientWorkloadDriver { schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), }, )?; + harden_runtime_bundle(&bundle)?; Ok(bundle) } @@ -954,8 +1075,13 @@ impl SystemdTransientWorkloadDriver { OsString::from("--property=LoadState"), OsString::from("--property=ActiveState"), OsString::from("--property=SubState"), + OsString::from("--property=Description"), OsString::from("--property=InvocationID"), OsString::from("--property=MainPID"), + OsString::from("--property=User"), + OsString::from("--property=Group"), + OsString::from("--property=WorkingDirectory"), + OsString::from("--property=ControlGroup"), ]) .stdin(Stdio::null()) .output() @@ -991,6 +1117,7 @@ impl SystemdTransientWorkloadDriver { unit: &str, expected_executable: &Path, runtime_instance_id: &str, + environment_names: &[String], ) -> Result { let values = self .show_unit(unit)? @@ -1009,6 +1136,20 @@ impl SystemdTransientWorkloadDriver { .filter(|value| value.len() == 32 && value.bytes().all(|byte| byte.is_ascii_hexdigit())) .context("systemd unit has no canonical invocation id")? .clone(); + let unit_description = required_systemd_property(&values, "Description")?.to_owned(); + let user = required_systemd_property(&values, "User")?.to_owned(); + let group = required_systemd_property(&values, "Group")?.to_owned(); + let working_directory = + PathBuf::from(required_systemd_property(&values, "WorkingDirectory")?); + ensure!( + working_directory.is_absolute(), + "systemd unit working directory is not absolute" + ); + let control_group = required_systemd_property(&values, "ControlGroup")?.to_owned(); + ensure!( + control_group.starts_with('/') && !control_group.contains(['\n', '\r', '\0']), + "systemd unit control group is invalid" + ); let main_pid: u32 = values .get("MainPID") .context("systemd unit has no MainPID")? @@ -1024,17 +1165,138 @@ impl SystemdTransientWorkloadDriver { ); let executable_sha256 = sha256_id(&fs::read(&executable)?); let process_start_time = linux_process_start_time(&process_root.join("stat"))?; + let process_control_groups = fs::read_to_string(process_root.join("cgroup"))?; + ensure!( + process_control_groups.lines().any(|line| { + line.split_once("::") + .is_some_and(|(_, path)| path == control_group) + }), + "systemd MainPID is outside the unit control group" + ); + let command_line = fs::read(process_root.join("cmdline"))?; + ensure!( + !command_line.is_empty(), + "systemd MainPID has no command line" + ); + let process_environment = read_process_environment(&process_root.join("environ"))?; + let selected_environment = + select_process_environment(&process_environment, environment_names)?; + let runtime_bundle = PathBuf::from( + selected_environment + .get(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) + .context("systemd MainPID lacks the Idunn runtime bundle environment")?, + ); + ensure!( + runtime_bundle.is_absolute(), + "runtime bundle path is not absolute" + ); Ok(WorkloadObservation { unit: unit.to_owned(), + unit_description, invocation_id, main_pid, process_start_time, executable, executable_sha256, runtime_instance_id: runtime_instance_id.to_owned(), + user, + group, + working_directory, + runtime_bundle, + command_line_sha256: sha256_id(&command_line), + environment_names: environment_names.to_vec(), + environment_contract_sha256: sha256_id(&rmp_serde::to_vec(&selected_environment)?), + control_group, }) } + fn launch_command( + &self, + declaration: &TargetDeclaration, + binding: &OperatorBinding, + installed: &Path, + ) -> Result> { + let executable_artifact = + release_artifact(declaration, &declaration.service.executable_artifact)?; + let executable = installed.join(&executable_artifact.destination); + ensure!(executable.is_file(), "sealed service executable is absent"); + let mut command = vec![executable.into_os_string()]; + for argument in &declaration.service.arguments { + command.push(match argument { + LaunchArgument::Literal { value } => value.into(), + LaunchArgument::Binding { name } => { + binding.workload.argument_bindings[name].clone().into() + } + }); + } + Ok(command) + } + + fn launch_environment( + &self, + binding: &OperatorBinding, + bundle: &Path, + ) -> Result> { + let mut environment = binding.workload.environment.clone(); + for (name, path) in &binding.workload.secret_files { + ensure!( + environment + .insert(name.clone(), path.display().to_string()) + .is_none(), + "workload environment and secret bindings collide" + ); + } + ensure!( + environment + .insert( + IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into(), + bundle.display().to_string(), + ) + .is_none(), + "operator binding attempts to replace the Idunn runtime bundle" + ); + Ok(environment) + } + + fn validate_launch_observation( + &self, + observation: &WorkloadObservation, + declaration: &TargetDeclaration, + binding: &OperatorBinding, + installed: &Path, + bundle: &Path, + ) -> Result<()> { + let command = self.launch_command(declaration, binding, installed)?; + let environment = self.launch_environment(binding, bundle)?; + let environment_names = environment.keys().cloned().collect::>(); + let expected_description = format!( + "Idunn {} {}", + binding.target, + bundle + .file_name() + .context("runtime bundle has no instance id")? + .to_string_lossy() + ); + ensure!( + observation.unit_description == expected_description + && observation.user == binding.workload.user + && observation.group == binding.workload.group + && observation.working_directory == installed + && observation.runtime_bundle == bundle + && observation.command_line_sha256 == sha256_id(&proc_command_line(&command)) + && observation.environment_names == environment_names + && observation.environment_contract_sha256 + == sha256_id(&rmp_serde::to_vec(&environment)?), + "running workload launch contract differs from the admitted launch" + ); + ensure!( + Path::new(&observation.control_group).file_name() + == Some(OsStr::new(&observation.unit)), + "running workload control group belongs to another unit" + ); + Ok(()) + } + fn start_transient( &self, declaration: &TargetDeclaration, @@ -1065,6 +1327,14 @@ impl SystemdTransientWorkloadDriver { release_artifact(declaration, &declaration.service.executable_artifact)?; let executable = installed.join(&executable_artifact.destination); ensure!(executable.is_file(), "sealed service executable is absent"); + let unit_description = format!( + "Idunn {} {}", + binding.target, + bundle + .file_name() + .context("runtime bundle has no instance id")? + .to_string_lossy() + ); let mut args = vec![ OsString::from("--no-block"), OsString::from(format!("--unit={unit}")), @@ -1081,6 +1351,7 @@ impl SystemdTransientWorkloadDriver { OsString::from("--property=RestrictSUIDSGID=yes"), OsString::from("--property=LockPersonality=yes"), OsString::from("--property=UMask=0027"), + OsString::from(format!("--property=Description={unit_description}")), OsString::from(format!("--property=User={}", binding.workload.user)), OsString::from(format!("--property=Group={}", binding.workload.group)), OsString::from(format!( @@ -1108,6 +1379,14 @@ impl SystemdTransientWorkloadDriver { state_root.display() ))); } + if let Some(write_lease) = &binding.process_write_lease { + for path in [write_lease.record_path.clone(), write_lease.lock_path()] { + args.push(OsString::from(format!( + "--property=ReadOnlyPaths={}", + path.display() + ))); + } + } for path in &binding.workload.read_only_paths { args.push(OsString::from(format!( "--property=ReadOnlyPaths={}", @@ -1131,21 +1410,19 @@ impl SystemdTransientWorkloadDriver { ))); } } - if !binding.workload.capabilities.is_empty() { - let capabilities = binding - .workload - .capabilities - .iter() - .cloned() - .collect::>() - .join(" "); - args.push(OsString::from(format!( - "--property=CapabilityBoundingSet={capabilities}" - ))); - args.push(OsString::from(format!( - "--property=AmbientCapabilities={capabilities}" - ))); - } + let capabilities = binding + .workload + .capabilities + .iter() + .cloned() + .collect::>() + .join(" "); + args.push(OsString::from(format!( + "--property=CapabilityBoundingSet={capabilities}" + ))); + args.push(OsString::from(format!( + "--property=AmbientCapabilities={capabilities}" + ))); for (name, value) in &binding.workload.environment { args.push(OsString::from(format!("--setenv={name}={value}"))); } @@ -1159,62 +1436,90 @@ impl SystemdTransientWorkloadDriver { path.display() ))); } - args.push(executable.into_os_string()); - for argument in &declaration.service.arguments { - args.push(match argument { - LaunchArgument::Literal { value } => value.into(), - LaunchArgument::Binding { name } => { - binding.workload.argument_bindings[name].clone().into() - } - }); - } + args.extend(self.launch_command(declaration, binding, installed)?); self.command(&self.systemd_run_program, args)?; Ok(()) } } impl WorkloadPort for SystemdTransientWorkloadDriver { - fn start( + fn install( &self, plan: &CompiledDeploymentPlan, release: &MaterializedRelease, + ) -> Result { + self.install_release(plan, release) + } + + fn start( + &self, + plan: &CompiledDeploymentPlan, + release: &SealedRelease, + installed: &InstalledReleaseObservation, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, ) -> Result { plan.validate()?; - release.release.validate_against(plan)?; + release.validate_against(plan)?; + self.validate_installed_release(plan, release, installed)?; expected.validate()?; activation.validate()?; ensure!( expected.plan_id == plan.plan_id - && expected.sealed_release_id == release.release.sealed_release_id + && expected.sealed_release_id == release.sealed_release_id && activation.expected_projection_sha256 == expected.canonical_sha256()?, "workload inputs do not describe one sealed incarnation" ); let (declaration, binding) = plan.parsed_inputs()?; - let installed = self.install_release(plan, release)?; let executable_artifact = release_artifact(&declaration, &declaration.service.executable_artifact)?; - let executable = installed.join(&executable_artifact.destination); + let executable = installed.root.join(&executable_artifact.destination); let unit = self.unit_name( &binding.workload.unit_prefix, &activation.runtime_instance_id, )?; let bundle = self.prepare_runtime_bundle(&binding, expected, activation)?; + let environment_names = self + .launch_environment(&binding, &bundle)? + .into_keys() + .collect::>(); if self.show_unit(&unit)?.is_some() { - let observation = - self.observe_unit(&unit, &executable, &activation.runtime_instance_id)?; + let observation = self.observe_unit( + &unit, + &executable, + &activation.runtime_instance_id, + &environment_names, + )?; + self.validate_launch_observation( + &observation, + &declaration, + &binding, + &installed.root, + &bundle, + )?; ensure!( observation.executable_sha256 == expected.artifact_sha256, "running workload executable differs from Expected" ); return Ok(observation); } - self.start_transient(&declaration, &binding, &installed, &bundle, &unit)?; + self.start_transient(&declaration, &binding, &installed.root, &bundle, &unit)?; let mut last_error = None; for _ in 0..100 { - match self.observe_unit(&unit, &executable, &activation.runtime_instance_id) { + match self.observe_unit( + &unit, + &executable, + &activation.runtime_instance_id, + &environment_names, + ) { Ok(observation) => { + self.validate_launch_observation( + &observation, + &declaration, + &binding, + &installed.root, + &bundle, + )?; ensure!( observation.executable_sha256 == expected.artifact_sha256, "started workload executable differs from Expected" @@ -1241,8 +1546,12 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { && activation.runtime_instance_id == prior.runtime_instance_id, "workload observation belongs to another activation" ); - let observed = - self.observe_unit(&prior.unit, &prior.executable, &prior.runtime_instance_id)?; + let observed = self.observe_unit( + &prior.unit, + &prior.executable, + &prior.runtime_instance_id, + &prior.environment_names, + )?; ensure!( observed == *prior && observed.executable_sha256 == expected.artifact_sha256, "native workload identity changed after observation" @@ -1251,6 +1560,29 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { } fn stop(&self, observation: &WorkloadObservation) -> Result<()> { + let Some(values) = self.show_unit(&observation.unit)? else { + return Ok(()); + }; + let active = required_systemd_property(&values, "ActiveState")?; + let sub = required_systemd_property(&values, "SubState")?; + if active == "active" && sub == "running" { + let current = self.observe_unit( + &observation.unit, + &observation.executable, + &observation.runtime_instance_id, + &observation.environment_names, + )?; + ensure!( + current == *observation, + "refusing to stop a workload whose native identity changed" + ); + } else { + ensure!( + values.get("InvocationID") == Some(&observation.invocation_id) + && matches!(active, "inactive" | "failed"), + "refusing to stop a foreign or transitional systemd unit" + ); + } self.command( &self.systemctl_program, [OsString::from("stop"), OsString::from(&observation.unit)], @@ -1272,11 +1604,20 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { /// Idunn's compare-exchange therefore fences the old writer before replacing /// the record. Route membership never enters this store. pub struct CultCacheWriteLeaseDriver { + pub target: String, pub record_path: PathBuf, } impl CultCacheWriteLeaseDriver { + pub fn new(target: impl Into, record_path: impl Into) -> Self { + Self { + target: target.into(), + record_path: record_path.into(), + } + } + fn current(&self) -> Result> { + validate_root_authority_path(&self.record_path)?; if !self.record_path.exists() { return Ok(None); } @@ -1293,6 +1634,10 @@ impl CultCacheWriteLeaseDriver { envelope.key == lease.target, "write-lease key is not its target" ); + ensure!( + lease.target == self.target, + "write-lease store belongs to another target" + ); Ok(Some((envelope.clone(), lease))) } _ => bail!("process write-lease store is foreign or ambiguous"), @@ -1308,31 +1653,93 @@ impl CultCacheWriteLeaseDriver { schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), }) } + + fn validate_grant( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result<()> { + expected.validate()?; + activation.validate()?; + lease.validate()?; + let expected_sha256 = expected.canonical_sha256()?; + let activation_sha256 = activation.canonical_sha256()?; + let warming_presence_sha256 = warming + .signed_presence_sha256 + .as_deref() + .context("warming observation has no signed presence")?; + ensure!( + self.target == expected.target + && lease.target == expected.target + && lease.expected_projection_sha256 == expected_sha256 + && lease.plan_id == expected.plan_id + && lease.incarnation_id == expected.incarnation_id + && lease.sealed_release_id == expected.sealed_release_id + && lease.activation_witness_sha256 == activation_sha256 + && lease.runtime_id == expected.runtime_id + && lease.runtime_instance_id == activation.runtime_instance_id + && lease.state_schema_generation + == expected + .state_schema_generation + .as_deref() + .context("write-lease Expected has no state generation")? + && lease.state_contract_sha256 + == expected + .state_contract_sha256 + .as_deref() + .context("write-lease Expected has no state contract")? + && lease.warming_presence_sha256 == warming_presence_sha256 + && warming.present + && warming.state.as_deref() == Some("warming") + && warming.runtime_instance_id.as_deref() + == Some(activation.runtime_instance_id.as_str()) + && warming.write_lease_sha256.is_none() + && warming.disagreements.is_empty(), + "process write lease does not bind the exact warming candidate" + ); + Ok(()) + } } impl WriteLeasePort for CultCacheWriteLeaseDriver { - fn fence(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()> { - expected.validate()?; - let Some((envelope, _)) = self.current()? else { + fn revoke_exact(&self, incumbent: Option<&IdunnProcessWriteLeaseRecord>) -> Result<()> { + if let Some(incumbent) = incumbent { + incumbent.validate()?; + ensure!( + incumbent.target == self.target, + "incumbent write lease belongs to another target" + ); + } + let current = self.current()?; + let Some((envelope, current_lease)) = current else { return Ok(()); }; + ensure!( + incumbent == Some(¤t_lease), + "refusing to revoke an unexpected process write lease" + ); let store = SingleFileMessagePackBackingStore::new(&self.record_path); ensure!( - store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: envelope.r#type.clone(), - key: envelope.key.clone(), - current: Some(envelope), - }], - &[], - )?, + store.compare_exchange_snapshot(std::slice::from_ref(&envelope), &[])?, "process write lease changed while fencing the incumbent" ); Ok(()) } - fn grant(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { - lease.validate()?; + fn observe_empty(&self) -> Result { + Ok(self.current()?.is_none()) + } + + fn grant( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result { + self.validate_grant(expected, activation, warming, lease)?; if let Some((_, current)) = self.current()? { ensure!( current == *lease, @@ -1340,9 +1747,6 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { ); return lease.canonical_sha256(); } - if let Some(parent) = self.record_path.parent() { - fs::create_dir_all(parent)?; - } let envelope = self.envelope(lease)?; ensure!( SingleFileMessagePackBackingStore::new(&self.record_path).compare_exchange( @@ -1355,34 +1759,19 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { )?, "process write-lease grant lost its empty-store compare-exchange" ); + harden_root_authority_file(&self.record_path)?; + harden_root_authority_file(&authority_lock_path(&self.record_path))?; lease.canonical_sha256() } - fn revoke(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result<()> { - lease.validate()?; - let Some((envelope, current)) = self.current()? else { - return Ok(()); - }; - ensure!( - current == *lease, - "refusing to revoke another process write lease" - ); - ensure!( - SingleFileMessagePackBackingStore::new(&self.record_path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: envelope.r#type.clone(), - key: envelope.key.clone(), - current: Some(envelope), - }], - &[], - )?, - "process write lease changed while revoking it" - ); - Ok(()) - } - - fn observe(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { - lease.validate()?; + fn observe( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result { + self.validate_grant(expected, activation, warming, lease)?; Ok(self .current()? .is_some_and(|(_, current)| current == *lease)) @@ -1619,7 +2008,8 @@ impl NginxRouteDriver { .as_ref() .context("expected incarnation has no route")?; ensure!( - route.route_id == self.binding.route_id, + route.route_id == self.binding.route_id + && route.stable_endpoint == self.binding.stable_endpoint, "route driver binding differs from Expected" ); let (candidate_host, candidate_port) = endpoint_host_port( @@ -1651,13 +2041,12 @@ impl NginxRouteDriver { Ok(rendered.into_bytes()) } - fn loaded_contains(&self, rendered: &[u8]) -> Result { + fn loaded_matches(&self, rendered: &[u8]) -> Result { let output = self.command(&self.nginx_program, [OsString::from("-T")])?; - let mut loaded = output.stdout; - loaded.extend_from_slice(&output.stderr); - Ok(loaded - .windows(rendered.len()) - .any(|window| window == rendered)) + let loaded = std::str::from_utf8(&output.stdout) + .context("nginx loaded configuration is not UTF-8")?; + Ok(nginx_config_section(loaded, &self.binding.config_path)? + .is_some_and(|section| section.as_bytes() == rendered)) } fn reload(&self) -> Result<()> { @@ -1700,6 +2089,31 @@ impl NginxRouteDriver { } impl RoutePort for NginxRouteDriver { + fn preflight( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + ) -> Result<()> { + let rendered = self.render(expected, runtime_instance_id)?; + ensure!(!rendered.is_empty(), "candidate route rendered empty"); + self.command(&self.nginx_program, [OsString::from("-t")])?; + let current = match fs::read(&self.binding.config_path) { + Ok(bytes) => Some(bytes), + Err(error) if error.kind() == ErrorKind::NotFound => None, + Err(error) => return Err(error).context("reading route preflight baseline"), + }; + let output = self.command(&self.nginx_program, [OsString::from("-T")])?; + let dump = std::str::from_utf8(&output.stdout) + .context("nginx loaded configuration is not UTF-8")?; + let loaded = nginx_config_section(dump, &self.binding.config_path)?; + match (current.as_deref(), loaded) { + (None, None) => {} + (Some(current), Some(loaded)) if current == loaded.as_bytes() => {} + _ => bail!("nginx route baseline differs from its loaded configuration"), + } + Ok(()) + } + fn promote( &self, expected: &IdunnExpectedIncarnationRecord, @@ -1719,7 +2133,7 @@ impl RoutePort for NginxRouteDriver { "candidate route validation or reload failed", )?; } - match self.loaded_contains(&rendered) { + match self.loaded_matches(&rendered) { Ok(true) => {} Ok(false) => self.fail_after_rollback( prior.as_deref(), @@ -1755,7 +2169,7 @@ impl RoutePort for NginxRouteDriver { Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false), Err(error) => return Err(error).context("reading route observation"), }; - Ok(current == rendered && self.loaded_contains(&rendered)?) + Ok(current == rendered && self.loaded_matches(&rendered)?) } } @@ -1826,18 +2240,120 @@ fn rfc3339_millis(millis: u64) -> Result { } #[cfg(unix)] -fn set_executable_read_only(path: &Path) -> Result<()> { - use std::os::unix::fs::PermissionsExt; - ensure!(path.is_file(), "executable artifact is not a regular file"); - fs::set_permissions(path, fs::Permissions::from_mode(0o555))?; +fn harden_installed_release(root: &Path, artifacts: &[ArtifactReceipt]) -> Result<()> { + let executable_paths = artifacts + .iter() + .filter(|artifact| artifact.executable) + .map(|artifact| root.join(&artifact.destination)) + .collect::>(); + harden_root_tree(root, root, &executable_paths) +} + +#[cfg(unix)] +fn harden_root_tree( + root: &Path, + current: &Path, + executable_paths: &std::collections::BTreeSet, +) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(current)?; + ensure!( + metadata.uid() == 0, + "sealed release entry is not root-owned" + ); + if metadata.is_dir() { + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + harden_root_tree(root, &entry.path(), executable_paths)?; + } + fs::set_permissions(current, fs::Permissions::from_mode(0o555))?; + } else if metadata.is_file() { + fs::set_permissions( + current, + fs::Permissions::from_mode(if executable_paths.contains(current) { + 0o555 + } else { + 0o444 + }), + )?; + } else if metadata.file_type().is_symlink() { + validate_release_symlink(root, current)?; + } else { + bail!("sealed release contains a special filesystem entry") + } + Ok(()) +} + +#[cfg(unix)] +fn validate_release_symlink(root: &Path, path: &Path) -> Result<()> { + let target = fs::read_link(path)?; + ensure!(target.is_relative(), "sealed release symlink is absolute"); + ensure!( + target.components().all(|component| matches!( + component, + std::path::Component::CurDir | std::path::Component::Normal(_) + )), + "sealed release symlink has a parent traversal" + ); + let destination = path + .parent() + .context("sealed release symlink has no parent")? + .join(target); + ensure!( + destination.starts_with(root), + "sealed release symlink escaped its release" + ); Ok(()) } #[cfg(not(unix))] -fn set_executable_read_only(_path: &Path) -> Result<()> { +fn harden_installed_release(_root: &Path, _artifacts: &[ArtifactReceipt]) -> Result<()> { bail!("systemd workload installation requires Unix permissions") } +#[cfg(unix)] +fn harden_runtime_bundle(bundle: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(bundle)?; + let parent = bundle.parent().context("runtime bundle has no parent")?; + let parent_metadata = fs::symlink_metadata(parent)?; + ensure!( + metadata.is_dir() && !metadata.file_type().is_symlink() && metadata.uid() == 0, + "runtime bundle is not a root-owned directory" + ); + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "runtime bundle root is not root-owned and service-nonwritable" + ); + for name in [ + "expected.cc", + "expected.cc.lock", + "activation.cc", + "activation.cc.lock", + ] { + let path = bundle.join(name); + let metadata = fs::symlink_metadata(&path)?; + ensure!( + metadata.is_file() && !metadata.file_type().is_symlink() && metadata.uid() == 0, + "runtime bundle document is not root-owned" + ); + fs::set_permissions(path, fs::Permissions::from_mode(0o444))?; + } + fs::set_permissions(bundle, fs::Permissions::from_mode(0o555))?; + Ok(()) +} + +#[cfg(not(unix))] +fn harden_runtime_bundle(_bundle: &Path) -> Result<()> { + bail!("systemd runtime bundles require Unix permissions") +} + fn linux_process_start_time(stat_path: &Path) -> Result { let stat = fs::read_to_string(stat_path) .with_context(|| format!("reading process stat {}", stat_path.display()))?; @@ -1852,6 +2368,62 @@ fn linux_process_start_time(stat_path: &Path) -> Result { .context("process start-time field is not a u64") } +fn required_systemd_property<'a>( + values: &'a BTreeMap, + name: &str, +) -> Result<&'a str> { + values + .get(name) + .filter(|value| !value.is_empty()) + .map(String::as_str) + .with_context(|| format!("systemd unit has no {name}")) +} + +fn read_process_environment(path: &Path) -> Result>> { + let bytes = fs::read(path) + .with_context(|| format!("reading process environment {}", path.display()))?; + Ok(bytes + .split(|byte| *byte == 0) + .filter(|entry| !entry.is_empty()) + .map(<[u8]>::to_vec) + .collect()) +} + +fn select_process_environment( + entries: &[Vec], + names: &[String], +) -> Result> { + ensure!( + names.windows(2).all(|pair| pair[0] < pair[1]), + "workload environment witness names are not unique and sorted" + ); + let mut selected = BTreeMap::new(); + for name in names { + let prefix = format!("{name}=").into_bytes(); + let matches = entries + .iter() + .filter(|entry| entry.starts_with(&prefix)) + .collect::>(); + ensure!( + matches.len() == 1, + "workload process environment has missing or duplicate {name}" + ); + let value = std::str::from_utf8(&matches[0][prefix.len()..]) + .with_context(|| format!("workload environment {name} is not UTF-8"))?; + selected.insert(name.clone(), value.to_owned()); + } + Ok(selected) +} + +fn proc_command_line(command: &[OsString]) -> Vec { + let mut encoded = Vec::new(); + for argument in command { + encoded.extend_from_slice(argument.as_encoded_bytes()); + encoded.push(0); + } + encoded +} + fn endpoint_host_port(endpoint: &str, scheme: &str) -> Result<(String, u16)> { let authority = endpoint .strip_prefix(scheme) @@ -1893,6 +2465,24 @@ fn nginx_identifier(route_id: &str) -> Result { Ok(identifier) } +fn nginx_config_section<'a>(dump: &'a str, config_path: &Path) -> Result> { + let marker = format!("# configuration file {}:\n", config_path.display()); + let matches = dump.match_indices(&marker).collect::>(); + ensure!( + matches.len() <= 1, + "nginx loaded configuration contains duplicate sections for {}", + config_path.display() + ); + let Some((start, _)) = matches.first().copied() else { + return Ok(None); + }; + let content = &dump[start + marker.len()..]; + let end = content + .find("\n# configuration file ") + .map_or(content.len(), |index| index + 1); + Ok(Some(&content[..end])) +} + fn atomic_replace(path: &Path, bytes: &[u8]) -> Result<()> { let parent = path.parent().context("route path has no parent")?; fs::create_dir_all(parent)?; @@ -2110,6 +2700,149 @@ fn ensure_source_directory_tree( Ok(()) } +fn container_identity(value: &str) -> Result { + let (uid, gid) = value + .split_once(':') + .context("runner user is not numeric UID:GID")?; + let identity = ProcessIdentity { + uid: uid.parse().context("runner UID is not a u32")?, + gid: gid.parse().context("runner GID is not a u32")?, + }; + ensure!( + identity.uid > 0 && identity.gid > 0, + "runner identity must be unprivileged" + ); + Ok(identity) +} + +#[cfg(unix)] +fn ensure_runner_cache_root(path: &Path, identity: ProcessIdentity) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "runner cache admission requires root Idunn" + ); + let parent = path.parent().context("runner cache root has no parent")?; + let parent_metadata = fs::symlink_metadata(parent) + .with_context(|| format!("inspecting runner cache parent {}", parent.display()))?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "runner cache parent is not root-owned and nonwritable" + ); + match fs::symlink_metadata(path) { + Ok(metadata) => ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == identity.uid + && metadata.gid() == identity.gid + && metadata.permissions().mode() & 0o777 == 0o700, + "runner cache is not a dedicated exact-identity 0700 directory" + ), + Err(error) if error.kind() == ErrorKind::NotFound => { + fs::create_dir(path) + .with_context(|| format!("creating runner cache {}", path.display()))?; + let path_c = std::ffi::CString::new(path.as_os_str().as_bytes()) + .context("runner cache path contains a NUL byte")?; + if unsafe { libc::lchown(path_c.as_ptr(), identity.uid, identity.gid) } != 0 { + return Err(std::io::Error::last_os_error()) + .context("assigning runner cache owner"); + } + fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; + } + Err(error) => { + return Err(error) + .with_context(|| format!("inspecting runner cache {}", path.display())); + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn ensure_runner_cache_root(_path: &Path, _identity: ProcessIdentity) -> Result<()> { + bail!("runner cache admission requires a Unix actuator") +} + +#[cfg(unix)] +fn validate_runner_secret(path: &Path, identity: ProcessIdentity) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "runner secret admission requires root Idunn" + ); + let parent = path.parent().context("runner secret has no parent")?; + let parent_metadata = fs::symlink_metadata(parent) + .with_context(|| format!("inspecting runner secret parent {}", parent.display()))?; + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("inspecting runner secret {}", path.display()))?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "runner secret parent is not root-owned and nonwritable" + ); + ensure!( + metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == identity.gid + && metadata.permissions().mode() & 0o777 == 0o440, + "runner secret is not root-owned, exact-group-bound, and 0440" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_runner_secret(_path: &Path, _identity: ProcessIdentity) -> Result<()> { + bail!("runner secret admission requires a Unix actuator") +} + +#[cfg(unix)] +fn assign_runner_tree(path: &Path, identity: ProcessIdentity) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::PermissionsExt; + + ensure!( + unsafe { libc::geteuid() } == 0, + "runner workspace ownership requires root Idunn" + ); + let metadata = fs::symlink_metadata(path)?; + if metadata.is_dir() { + let mut entries = fs::read_dir(path)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + assign_runner_tree(&entry.path(), identity)?; + } + fs::set_permissions(path, fs::Permissions::from_mode(0o750))?; + } else if metadata.is_file() { + let executable = metadata.permissions().mode() & 0o111 != 0; + fs::set_permissions( + path, + fs::Permissions::from_mode(if executable { 0o750 } else { 0o640 }), + )?; + } else if !metadata.file_type().is_symlink() { + bail!("runner workspace contains a special filesystem entry") + } + let path_c = std::ffi::CString::new(path.as_os_str().as_bytes()) + .context("runner workspace path contains a NUL byte")?; + let result = unsafe { libc::lchown(path_c.as_ptr(), identity.uid, identity.gid) }; + if result != 0 { + return Err(std::io::Error::last_os_error()).context("assigning runner workspace owner"); + } + Ok(()) +} + +#[cfg(not(unix))] +fn assign_runner_tree(_path: &Path, _identity: ProcessIdentity) -> Result<()> { + bail!("non-root runner identities require a Unix actuator") +} + fn ensure_source_directory(path: &Path, identity: Option) -> Result<()> { match fs::symlink_metadata(path) { Ok(metadata) => { @@ -2141,6 +2874,87 @@ fn ensure_source_directory(path: &Path, identity: Option) -> Re } } +fn authority_lock_path(path: &Path) -> PathBuf { + let mut lock = path.as_os_str().to_os_string(); + lock.push(".lock"); + PathBuf::from(lock) +} + +#[cfg(unix)] +fn validate_root_authority_path(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "process write-lease actuation requires root Idunn" + ); + let parent = path + .parent() + .context("process write-lease path has no parent")?; + let parent_metadata = fs::symlink_metadata(parent) + .with_context(|| format!("inspecting write-lease parent {}", parent.display()))?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "write-lease parent is not root-owned and service-nonwritable" + ); + for authority_file in [path.to_owned(), authority_lock_path(path)] { + match fs::symlink_metadata(&authority_file) { + Ok(metadata) => ensure!( + metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == parent_metadata.gid() + && metadata.permissions().mode() & 0o022 == 0, + "write-lease authority file {} is not root-owned and service-nonwritable", + authority_file.display() + ), + Err(error) if error.kind() == ErrorKind::NotFound => {} + Err(error) => { + return Err(error).with_context(|| { + format!( + "inspecting write-lease authority file {}", + authority_file.display() + ) + }); + } + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn validate_root_authority_path(_path: &Path) -> Result<()> { + bail!("process write-lease actuation requires a Unix authority path") +} + +#[cfg(unix)] +fn harden_root_authority_file(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let parent = path + .parent() + .context("process write-lease authority file has no parent")?; + let parent_metadata = fs::symlink_metadata(parent)?; + let metadata = fs::symlink_metadata(path)?; + ensure!( + metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == parent_metadata.gid(), + "new write-lease authority file has the wrong owner" + ); + fs::set_permissions(path, fs::Permissions::from_mode(0o640))?; + Ok(()) +} + +#[cfg(not(unix))] +fn harden_root_authority_file(_path: &Path) -> Result<()> { + bail!("process write-lease actuation requires Unix permissions") +} + #[cfg(unix)] fn assign_source_directory_owner(path: &Path, identity: Option) -> Result<()> { use std::os::unix::ffi::OsStrExt; @@ -2213,7 +3027,24 @@ fn apply_identity(command: &mut Command, identity: Option) -> R #[cfg(unix)] { use std::os::unix::process::CommandExt; - command.uid(identity.uid).gid(identity.gid); + ensure!( + identity.uid > 0 && identity.gid > 0, + "configured source identity must be unprivileged" + ); + unsafe { + command.pre_exec(move || { + if libc::setgroups(0, std::ptr::null()) != 0 { + return Err(std::io::Error::last_os_error()); + } + if libc::setgid(identity.gid) != 0 { + return Err(std::io::Error::last_os_error()); + } + if libc::setuid(identity.uid) != 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(()) + }); + } Ok(()) } #[cfg(not(unix))] @@ -2223,3 +3054,223 @@ fn apply_identity(command: &mut Command, identity: Option) -> R bail!("configured process identities require a Unix actuator") } } + +#[cfg(test)] +mod tests { + use super::*; + use cultnet_rs::{ + IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, + IDUNN_RUNTIME_ACTIVATION_SCHEMA, + }; + + fn digest(byte: char) -> String { + format!("sha256-{}", byte.to_string().repeat(64)) + } + + fn expected() -> IdunnExpectedIncarnationRecord { + IdunnExpectedIncarnationRecord { + schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), + target: "service".into(), + plan_id: digest('1'), + incarnation_id: "incarnation-1".into(), + sealed_release_id: digest('2'), + source_repository: "github.com/GameCult/Service".into(), + source_revision: "3".repeat(40), + recipe_sha256: digest('4'), + runtime_id: "service-runtime".into(), + expected_signer_identity_id: "service-signer".into(), + health_contract: "service.health.v1".into(), + artifact_sha256: digest('5'), + state_schema_generation: Some("state-v1".into()), + state_contract_sha256: Some(digest('6')), + write_lease_required: true, + route: None, + capabilities: Vec::new(), + dependencies: Vec::new(), + } + } + + fn activation(expected: &IdunnExpectedIncarnationRecord) -> IdunnRuntimeActivationRecord { + IdunnRuntimeActivationRecord { + schema_version: IDUNN_RUNTIME_ACTIVATION_SCHEMA.into(), + expected_projection_sha256: expected.canonical_sha256().unwrap(), + runtime_instance_id: digest('7'), + issued_at_unix_millis: 100, + } + } + + fn warming(activation: &IdunnRuntimeActivationRecord) -> PresenceObservation { + PresenceObservation { + correlation_sha256: digest('8'), + signed_presence_sha256: Some(digest('9')), + runtime_instance_id: Some(activation.runtime_instance_id.clone()), + present: true, + state: Some("warming".into()), + write_lease_sha256: None, + disagreements: Vec::new(), + } + } + + fn lease( + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + warming: &PresenceObservation, + ) -> IdunnProcessWriteLeaseRecord { + IdunnProcessWriteLeaseRecord { + schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256().unwrap(), + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: activation.canonical_sha256().unwrap(), + state_schema_generation: expected.state_schema_generation.clone().unwrap(), + state_contract_sha256: expected.state_contract_sha256.clone().unwrap(), + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + warming_presence_sha256: warming.signed_presence_sha256.clone().unwrap(), + lease_epoch: 1, + issued_at_unix_millis: 200, + } + } + + #[test] + fn nginx_loaded_observation_is_bound_to_the_exact_file_section() -> Result<()> { + let path = Path::new("/etc/nginx/idunn-routes/service.conf"); + let rendered = "upstream idunn_service {\n server 127.0.0.1:4104;\n}\n"; + let dump = format!( + "# configuration file /etc/nginx/nginx.conf:\nevents {{}}\n# configuration file {}:\n{rendered}# configuration file /etc/nginx/other.conf:\n{rendered}", + path.display() + ); + assert_eq!(nginx_config_section(&dump, path)?, Some(rendered)); + assert_eq!( + nginx_config_section(&dump, Path::new("/etc/nginx/missing.conf"))?, + None + ); + let duplicate = format!("{dump}# configuration file {}:\n{rendered}", path.display()); + assert!(nginx_config_section(&duplicate, path).is_err()); + Ok(()) + } + + #[test] + fn process_environment_witness_is_sorted_exact_and_secret_free() -> Result<()> { + let entries = vec![ + b"B=two".to_vec(), + b"A=one".to_vec(), + b"UNRELATED=ignored".to_vec(), + ]; + let selected = select_process_environment(&entries, &["A".into(), "B".into()])?; + assert_eq!(selected.get("A").map(String::as_str), Some("one")); + assert!(!selected.contains_key("UNRELATED")); + assert!(select_process_environment(&entries, &["B".into(), "A".into()]).is_err()); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn write_lease_revoke_is_exact_and_never_deletes_a_surprise() -> Result<()> { + let temp = tempfile::tempdir()?; + let path = temp.path().join("lease.cc"); + let driver = CultCacheWriteLeaseDriver::new("service", &path); + let expected = expected(); + let activation = activation(&expected); + let warming = warming(&activation); + let lease = lease(&expected, &activation, &warming); + let lease_sha256 = driver.grant(&expected, &activation, &warming, &lease)?; + assert_eq!(lease_sha256, lease.canonical_sha256()?); + assert!(driver.observe(&expected, &activation, &warming, &lease)?); + + let mut surprise = lease.clone(); + surprise.lease_epoch = 2; + assert!(driver.revoke_exact(Some(&surprise)).is_err()); + assert!(driver.observe(&expected, &activation, &warming, &lease)?); + + driver.revoke_exact(Some(&lease))?; + assert!(driver.observe_empty()?); + driver.revoke_exact(Some(&lease))?; + Ok(()) + } + + #[cfg(unix)] + #[test] + fn installed_release_is_root_owned_and_nonwritable() -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("release"); + fs::create_dir(&root)?; + let artifact = root.join("service"); + fs::write(&artifact, b"sealed")?; + fs::set_permissions(&artifact, fs::Permissions::from_mode(0o777))?; + harden_installed_release( + &root, + &[ArtifactReceipt { + artifact_id: "service".into(), + destination: PathBuf::from("service"), + sha256: sha256_id(b"sealed"), + size_bytes: 6, + executable: true, + }], + )?; + let root_metadata = fs::symlink_metadata(&root)?; + let artifact_metadata = fs::symlink_metadata(&artifact)?; + assert_eq!(root_metadata.uid(), 0); + assert_eq!(root_metadata.permissions().mode() & 0o777, 0o555); + assert_eq!(artifact_metadata.uid(), 0); + assert_eq!(artifact_metadata.permissions().mode() & 0o777, 0o555); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn runner_cache_is_created_once_for_one_exact_identity() -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let temp = tempfile::tempdir()?; + fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o700))?; + let cache = temp.path().join("cache"); + let identity = ProcessIdentity { + uid: 1000, + gid: 1000, + }; + ensure_runner_cache_root(&cache, identity)?; + let metadata = fs::symlink_metadata(&cache)?; + assert_eq!((metadata.uid(), metadata.gid()), (1000, 1000)); + assert_eq!(metadata.permissions().mode() & 0o777, 0o700); + assert!(ensure_runner_cache_root(&cache, identity).is_ok()); + assert!( + ensure_runner_cache_root( + &cache, + ProcessIdentity { + uid: 1001, + gid: 1001 + } + ) + .is_err() + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn runner_secret_is_root_owned_and_bound_to_the_runner_group() -> Result<()> { + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o700))?; + let secret = temp.path().join("token"); + fs::write(&secret, b"secret")?; + let secret_c = std::ffi::CString::new(secret.as_os_str().as_bytes())?; + ensure!(unsafe { libc::lchown(secret_c.as_ptr(), 0, 1000) } == 0); + fs::set_permissions(&secret, fs::Permissions::from_mode(0o440))?; + let identity = ProcessIdentity { + uid: 1000, + gid: 1000, + }; + validate_runner_secret(&secret, identity)?; + fs::set_permissions(&secret, fs::Permissions::from_mode(0o444))?; + assert!(validate_runner_secret(&secret, identity).is_err()); + Ok(()) + } +} From af1d4e537e6a8e5000eb54742030d5d9ce149891 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 00:28:29 +0200 Subject: [PATCH 79/93] Freeze exact deployment source before actuation --- crates/idunn-daemon/src/deployment_plan.rs | 21 +- crates/idunn-daemon/src/drivers.rs | 1195 +++++++++++++++++--- 2 files changed, 1035 insertions(+), 181 deletions(-) diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index 88066c8..f3a9571 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -25,7 +25,6 @@ pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; pub struct GitlinkTreeFact { pub origin: String, pub revision: String, - pub tree_entry_revision: String, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -44,6 +43,7 @@ pub struct SourceSelectionFacts { pub schema: String, pub origin: String, pub admitted_ref: String, + pub admitted_ref_revision: String, pub revision: String, pub source_tree: String, pub recipe_path: PathBuf, @@ -68,6 +68,10 @@ impl SourceSelectionFacts { self.admitted_ref == binding.repository.admitted_ref, "selected ref differs from binding" ); + require_sha1( + &self.admitted_ref_revision, + "selected admitted-ref revision", + )?; require_sha1(&self.revision, "selected revision")?; require_sha1(&self.source_tree, "selected source tree")?; ensure!( @@ -92,7 +96,10 @@ impl SourceSelectionFacts { "selected source differs from the pinned revision" ); } - (SourceSelectionPolicy::RefHead, SourceSelection::RefHead) => {} + (SourceSelectionPolicy::RefHead, SourceSelection::RefHead) => ensure!( + self.revision == self.admitted_ref_revision, + "ref-head selection differs from the fetched admitted-ref revision" + ), ( SourceSelectionPolicy::SignedRelease, SourceSelection::SignedRelease { @@ -118,12 +125,6 @@ impl SourceSelectionFacts { path.display() ); require_sha1(&receipt.revision, "Gitlink revision")?; - require_sha1(&receipt.tree_entry_revision, "Gitlink tree entry")?; - ensure!( - receipt.revision == receipt.tree_entry_revision, - "Gitlink {} revision differs from the selected tree entry", - path.display() - ); } Ok(()) } @@ -1089,6 +1090,7 @@ nodes = ["yggdrasil"] schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), origin: "https://github.com/GameCult/Service.git".into(), admitted_ref: "refs/heads/main".into(), + admitted_ref_revision: "2222222222222222222222222222222222222222".into(), revision: "2222222222222222222222222222222222222222".into(), source_tree: "3333333333333333333333333333333333333333".into(), recipe_path: "deployment/idunn/recipe.toml".into(), @@ -1233,7 +1235,6 @@ nodes = ["yggdrasil"] GitlinkTreeFact { origin: "https://github.com/GameCult/Lib.git".into(), revision: "4444444444444444444444444444444444444444".into(), - tree_entry_revision: "4444444444444444444444444444444444444444".into(), }, ); facts.validate_against(&binding).unwrap(); @@ -1241,7 +1242,7 @@ nodes = ["yggdrasil"] .gitlinks .get_mut(&PathBuf::from("vendor/lib")) .unwrap() - .tree_entry_revision = "5555555555555555555555555555555555555555".into(); + .origin = "https://github.com/GameCult/Wrong.git".into(); assert!(facts.validate_against(&binding).is_err()); } diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs index 319bf44..cffc796 100644 --- a/crates/idunn-daemon/src/drivers.rs +++ b/crates/idunn-daemon/src/drivers.rs @@ -41,18 +41,82 @@ pub struct ProcessIdentity { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct FrozenSource { +struct GitTreeEntry { + mode: String, + kind: String, + object: String, + path: PathBuf, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ResolvedSource { pub facts: SourceSelectionFacts, pub recipe_bytes: Vec, - pub root: PathBuf, +} + +impl ResolvedSource { + pub fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { + self.facts.validate_against(binding)?; + ensure!(!self.recipe_bytes.is_empty(), "resolved recipe is empty"); + ensure!( + sha256_id(&self.recipe_bytes) == self.facts.recipe_blob_sha256, + "resolved recipe bytes differ from the selected Git object" + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FrozenSource { + receipt: FrozenSourceReceipt, + facts: SourceSelectionFacts, + recipe_bytes: Vec, + root: PathBuf, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct FrozenSourceReceipt { + pub transaction_id: String, + pub plan_id: String, + pub snapshot_sha256: String, +} + +impl FrozenSourceReceipt { + pub fn validate_against(&self, plan: &CompiledDeploymentPlan) -> Result<()> { + plan.validate()?; + require_driver_id(&self.transaction_id, "source transaction")?; + ensure!(self.plan_id == plan.plan_id, "frozen source plan differs"); + require_sha256_id(&self.snapshot_sha256, "frozen source snapshot") + } + + fn snapshot_component(&self) -> &str { + self.snapshot_sha256 + .strip_prefix("sha256-") + .expect("validated frozen source digest") + } } pub trait SourcePort { - fn select( + fn resolve( &self, binding: &OperatorBinding, + resolution_id: &str, selected_at_unix_millis: u64, + ) -> Result; + + fn freeze( + &self, + transaction_id: &str, + plan: &CompiledDeploymentPlan, + ) -> Result; + + fn observe_frozen( + &self, + plan: &CompiledDeploymentPlan, + receipt: &FrozenSourceReceipt, ) -> Result; + + fn cleanup(&self, transaction_id: &str, receipt: Option<&FrozenSourceReceipt>) -> Result<()>; } pub trait RunnerPort { @@ -203,23 +267,32 @@ pub trait RoutePort { /// Fixed-argv Git source driver. It never interprets recipe text as a command /// and never derives source policy from the target repository. The configured -/// identity performs every Git read and materialization. +/// identity performs every Git/network read; root Idunn extracts only exact Git +/// object archives into a separate transaction-owned immutable store. pub struct GitSourceDriver { - pub source_root: PathBuf, + pub source_cache_root: PathBuf, + pub frozen_source_root: PathBuf, pub identity: Option, pub git_program: PathBuf, + pub tar_program: PathBuf, } impl GitSourceDriver { - pub fn new(source_root: impl Into, identity: Option) -> Self { + pub fn new( + source_cache_root: impl Into, + frozen_source_root: impl Into, + identity: Option, + ) -> Self { Self { - source_root: source_root.into(), + source_cache_root: source_cache_root.into(), + frozen_source_root: frozen_source_root.into(), identity, git_program: PathBuf::from("/usr/bin/git"), + tar_program: PathBuf::from("/usr/bin/tar"), } } - fn git(&self, args: I) -> Result + fn git_command(&self, args: I) -> Result where I: IntoIterator, S: AsRef, @@ -233,13 +306,21 @@ impl GitSourceDriver { .args(args) .stdin(Stdio::null()) .env_clear() - .env("HOME", self.source_root.join(".home")) + .env("HOME", self.source_cache_root.join(".home")) .env("PATH", "/usr/bin:/bin") .env("GIT_CONFIG_NOSYSTEM", "1") .env("GIT_TERMINAL_PROMPT", "0") .env("LANG", "C.UTF-8"); apply_identity(&mut command, self.identity)?; - let output = command.output().with_context(|| { + Ok(command) + } + + fn git(&self, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { + let output = self.git_command(args)?.output().with_context(|| { format!( "starting fixed-argv Git driver {}", self.git_program.display() @@ -278,9 +359,21 @@ impl GitSourceDriver { checkout.as_os_str().to_owned(), ])?; } + let checkout_metadata = fs::symlink_metadata(checkout) + .with_context(|| format!("inspecting source checkout {}", checkout.display()))?; ensure!( - checkout.join(".git").exists(), - "source checkout is not a Git worktree" + checkout_metadata.is_dir() && !checkout_metadata.file_type().is_symlink(), + "source checkout is not a native directory" + ); + let canonical_checkout = checkout.canonicalize()?; + ensure!( + canonical_checkout == *checkout, + "source checkout traverses a symlink or noncanonical path" + ); + let git_metadata = fs::symlink_metadata(checkout.join(".git"))?; + ensure!( + git_metadata.is_dir() && !git_metadata.file_type().is_symlink(), + "source checkout has no native Git object directory" ); let actual_origin = self.git_text([ OsString::from("-C"), @@ -296,14 +389,18 @@ impl GitSourceDriver { Ok(()) } - fn admitted_ref_name(binding: &OperatorBinding) -> String { - format!("refs/idunn/admitted/{}", binding.target) + fn admitted_ref_name(binding: &OperatorBinding, resolution_id: &str) -> Result { + require_driver_id(resolution_id, "source resolution")?; + Ok(format!( + "refs/idunn/resolutions/{}/{}", + binding.target, resolution_id + )) } fn resolve_selected_revision( &self, binding: &OperatorBinding, - fetched_ref: &str, + admitted_ref_revision: &str, ) -> Result<(String, SourceSelection)> { let checkout = &binding.repository.checkout; let (revision, selection) = match binding.repository.selection { @@ -315,15 +412,9 @@ impl GitSourceDriver { .context("pinned source binding lost its exact revision")?, SourceSelection::PinnedObject, ), - SourceSelectionPolicy::RefHead => ( - self.git_text([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from(format!("{fetched_ref}^{{commit}}")), - ])?, - SourceSelection::RefHead, - ), + SourceSelectionPolicy::RefHead => { + (admitted_ref_revision.to_owned(), SourceSelection::RefHead) + } SourceSelectionPolicy::SignedRelease => { bail!("signed-release selection requires a release-authority source port") } @@ -344,146 +435,152 @@ impl GitSourceDriver { OsString::from("merge-base"), OsString::from("--is-ancestor"), revision.clone().into(), - fetched_ref.into(), + admitted_ref_revision.into(), ]) .context("selected source is outside the fetched admitted ref")?; Ok((revision, selection)) } - fn gitlink_fact( - &self, - binding: &OperatorBinding, - revision: &str, - path: &Path, - ) -> Result { - let output = self.git_text([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("ls-tree"), - revision.into(), - OsString::from("--"), - path.as_os_str().to_owned(), - ])?; - let mut fields = output.split_whitespace(); - ensure!( - fields.next() == Some("160000"), - "bound Gitlink is not a commit tree entry" - ); - ensure!( - fields.next() == Some("commit"), - "bound Gitlink has the wrong object kind" - ); - let tree_revision = fields - .next() - .context("Gitlink tree entry has no revision")?; - require_git_sha(tree_revision, "Gitlink tree revision")?; - let origin = binding.repository.gitlinks[path].origin.clone(); - Ok(GitlinkTreeFact { - origin, - revision: tree_revision.to_owned(), - tree_entry_revision: tree_revision.to_owned(), - }) - } - - fn materialize_worktree( - &self, - binding: &OperatorBinding, - revision: &str, - gitlinks: &BTreeMap, - ) -> Result { - let target_root = self.source_root.join(&binding.target); - let root = target_root.join(format!("{}-{}", revision, Uuid::new_v4())); - self.git([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("worktree"), - OsString::from("add"), - OsString::from("--detach"), - root.as_os_str().to_owned(), - revision.into(), - ])?; - - for (path, fact) in gitlinks { - let destination = root.join(path); + fn git_tree_entries(&self, repository: &Path, revision: &str) -> Result> { + let output = self + .git([ + OsString::from("-C"), + repository.as_os_str().to_owned(), + OsString::from("ls-tree"), + OsString::from("-r"), + OsString::from("-z"), + revision.into(), + ])? + .stdout; + let mut entries = Vec::new(); + for record in output + .split(|byte| *byte == 0) + .filter(|record| !record.is_empty()) + { + let tab = record + .iter() + .position(|byte| *byte == b'\t') + .context("Git tree record has no path delimiter")?; + let header = std::str::from_utf8(&record[..tab]) + .context("Git tree record header is not UTF-8")?; + let path = + std::str::from_utf8(&record[tab + 1..]).context("Git tree path is not UTF-8")?; + let mut fields = header.split(' '); + let mode = fields.next().context("Git tree record has no mode")?; + let kind = fields.next().context("Git tree record has no kind")?; + let object = fields.next().context("Git tree record has no object")?; + ensure!(fields.next().is_none(), "Git tree record has extra fields"); + require_git_sha(object, "Git tree object")?; + let path = PathBuf::from(path); + let normalized = normalized_relative(&path)?; ensure!( - destination.starts_with(&root), - "Gitlink escaped the frozen source root" + normalized == path.to_string_lossy(), + "Git tree path is not normalized" ); - if destination.exists() { - fs::remove_dir(&destination).with_context(|| { - format!( - "removing empty Gitlink placeholder {}", - destination.display() - ) - })?; - } - if let Some(parent) = destination.parent() { - ensure_source_directory_tree(&root, parent, self.identity)?; - } - self.git([ - OsString::from("clone"), - OsString::from("--filter=blob:none"), - OsString::from("--no-checkout"), - OsString::from("--origin"), - OsString::from("origin"), - fact.origin.clone().into(), - destination.as_os_str().to_owned(), - ])?; - self.git([ - OsString::from("-C"), - destination.as_os_str().to_owned(), - OsString::from("fetch"), - OsString::from("--no-tags"), - OsString::from("origin"), - fact.revision.clone().into(), - ])?; - self.git([ - OsString::from("-C"), - destination.as_os_str().to_owned(), - OsString::from("checkout"), - OsString::from("--detach"), - fact.revision.clone().into(), - ])?; - let actual = self.git_text([ - OsString::from("-C"), - destination.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from("HEAD"), - ])?; ensure!( - actual == fact.revision, - "materialized Gitlink revision differs" + path.components().all(|component| !matches!( + component, + std::path::Component::Normal(value) if value == ".git" + )), + "Git tree contains forbidden .git metadata" ); + entries.push(GitTreeEntry { + mode: mode.to_owned(), + kind: kind.to_owned(), + object: object.to_owned(), + path, + }); } - Ok(root) + entries.sort_by(|left, right| left.path.cmp(&right.path)); + ensure!( + entries.windows(2).all(|pair| pair[0].path != pair[1].path), + "Git tree emits a path twice" + ); + Ok(entries) } -} -impl SourcePort for GitSourceDriver { - fn select( + fn exact_recipe_and_gitlinks( &self, binding: &OperatorBinding, - selected_at_unix_millis: u64, - ) -> Result { - binding.validate()?; + revision: &str, + ) -> Result<(Vec, BTreeMap)> { + let entries = self.git_tree_entries(&binding.repository.checkout, revision)?; + let recipe = entries + .iter() + .find(|entry| entry.path == binding.repository.recipe_path) + .context("selected tree has no deployment recipe")?; + ensure!( + matches!(recipe.mode.as_str(), "100644" | "100755") && recipe.kind == "blob", + "deployment recipe is not a regular Git blob" + ); + let recipe_bytes = self + .git([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("cat-file"), + OsString::from("blob"), + recipe.object.clone().into(), + ])? + .stdout; + ensure!( + !recipe_bytes.is_empty(), + "selected deployment recipe is empty" + ); + + let observed_gitlinks = entries + .iter() + .filter(|entry| entry.mode == "160000") + .map(|entry| { + ensure!(entry.kind == "commit", "Gitlink has the wrong object kind"); + Ok((entry.path.clone(), entry.object.clone())) + }) + .collect::>>()?; + let expected_paths = binding + .repository + .gitlinks + .keys() + .cloned() + .collect::>(); + let observed_paths = observed_gitlinks + .keys() + .cloned() + .collect::>(); + ensure!( + observed_paths == expected_paths, + "selected tree Gitlinks do not exactly match operator bindings" + ); + let gitlinks = observed_gitlinks + .into_iter() + .map(|(path, revision)| { + let origin = binding.repository.gitlinks[&path].origin.clone(); + (path, GitlinkTreeFact { origin, revision }) + }) + .collect(); + Ok((recipe_bytes, gitlinks)) + } + + fn prepare_source_root(&self, binding: &OperatorBinding) -> Result<()> { #[cfg(unix)] ensure!( unsafe { libc::geteuid() } != 0 || self.identity.is_some(), "root Idunn must configure an unprivileged source identity" ); ensure!( - selected_at_unix_millis > 0, - "source selection has no timestamp" - ); - ensure!( - binding.repository.checkout.starts_with(&self.source_root) - && binding.repository.checkout != self.source_root, + binding + .repository + .checkout + .starts_with(&self.source_cache_root) + && binding.repository.checkout != self.source_cache_root, "repository checkout is outside Idunn's source authority root" ); - ensure_source_directory(&self.source_root, self.identity)?; - ensure_source_directory(&self.source_root.join(".home"), self.identity)?; + ensure_source_directory(&self.source_cache_root, self.identity)?; + ensure!( + self.source_cache_root.canonicalize()? == self.source_cache_root, + "source cache root traverses a symlink or noncanonical path" + ); + ensure_source_directory(&self.source_cache_root.join(".home"), self.identity)?; ensure_source_directory_tree( - &self.source_root, + &self.source_cache_root, binding .repository .checkout @@ -491,9 +588,175 @@ impl SourcePort for GitSourceDriver { .context("repository checkout has no parent directory")?, self.identity, )?; - ensure_source_directory(&self.source_root.join(&binding.target), self.identity)?; - self.ensure_checkout(binding)?; - let fetched_ref = Self::admitted_ref_name(binding); + self.ensure_checkout(binding) + } + + fn verify_exact_source( + &self, + binding: &OperatorBinding, + resolved: &ResolvedSource, + ) -> Result<()> { + let actual_tree = self.git_text([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from(format!("{}^{{tree}}", resolved.facts.revision)), + ])?; + ensure!( + actual_tree == resolved.facts.source_tree, + "selected revision no longer resolves to the frozen source tree" + ); + let (recipe_bytes, gitlinks) = + self.exact_recipe_and_gitlinks(binding, &resolved.facts.revision)?; + ensure!( + recipe_bytes == resolved.recipe_bytes, + "selected recipe object differs from the durable source resolution" + ); + ensure!( + gitlinks == resolved.facts.gitlinks, + "Gitlinks differ from the durable source resolution" + ); + Ok(()) + } + + fn git_archive_into( + &self, + repository: &Path, + revision: &str, + prefix: Option<&Path>, + destination: &Path, + ) -> Result<()> { + ensure!( + self.tar_program.is_absolute(), + "source archive extractor is not absolute" + ); + let mut archive_args = vec![ + OsString::from("-C"), + repository.as_os_str().to_owned(), + OsString::from("archive"), + OsString::from("--format=tar"), + ]; + if let Some(prefix) = prefix { + archive_args.push(OsString::from(format!( + "--prefix={}/", + normalized_relative(prefix)? + ))); + } + archive_args.push(revision.into()); + let mut archive = self.git_command(archive_args)?; + archive.stdout(Stdio::piped()).stderr(Stdio::piped()); + let mut archive = archive.spawn().context("starting exact Git archive")?; + let archive_stdout = archive.stdout.take().context("Git archive has no stdout")?; + let mut extractor = Command::new(&self.tar_program); + extractor + .args([ + OsString::from("--extract"), + OsString::from("--file=-"), + OsString::from("--directory"), + destination.as_os_str().to_owned(), + OsString::from("--no-same-owner"), + ]) + .stdin(Stdio::from(archive_stdout)) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .env_clear() + .env("LANG", "C.UTF-8"); + let extractor = extractor + .spawn() + .context("starting fixed-argv source archive extractor")?; + let archive_output = archive + .wait_with_output() + .context("waiting for exact Git archive")?; + let extractor_output = extractor + .wait_with_output() + .context("waiting for source archive extractor")?; + ensure!( + archive_output.status.success(), + "Git archive failed: {}", + String::from_utf8_lossy(&archive_output.stderr).trim() + ); + ensure!( + extractor_output.status.success(), + "source archive extraction failed: {}", + String::from_utf8_lossy(&extractor_output.stderr).trim() + ); + Ok(()) + } + + fn materialize_gitlink_archive( + &self, + binding: &OperatorBinding, + path: &Path, + fact: &GitlinkTreeFact, + destination: &Path, + ) -> Result<()> { + let gitlink_root = self + .source_cache_root + .join(".gitlinks") + .join(&binding.target); + ensure_source_directory_tree(&self.source_cache_root, &gitlink_root, self.identity)?; + let checkout = gitlink_root.join(format!("{}-{}", fact.revision, Uuid::new_v4())); + let result = (|| { + self.git([ + OsString::from("clone"), + OsString::from("--filter=blob:none"), + OsString::from("--no-checkout"), + OsString::from("--origin"), + OsString::from("origin"), + fact.origin.clone().into(), + checkout.as_os_str().to_owned(), + ])?; + self.git([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("fetch"), + OsString::from("--no-tags"), + OsString::from("origin"), + fact.revision.clone().into(), + ])?; + let actual = self.git_text([ + OsString::from("-C"), + checkout.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from(format!("{}^{{commit}}", fact.revision)), + ])?; + ensure!(actual == fact.revision, "Gitlink exact revision is absent"); + ensure!( + !self + .git_tree_entries(&checkout, &fact.revision)? + .iter() + .any(|entry| entry.mode == "160000"), + "nested Gitlinks are not admitted in Idunn v1" + ); + self.git_archive_into(&checkout, &fact.revision, Some(path), destination) + })(); + let cleanup = if checkout.exists() { + remove_tree_inside(&gitlink_root, &checkout) + } else { + Ok(()) + }; + match (result, cleanup) { + (Ok(()), Ok(())) => Ok(()), + (Err(error), _) => Err(error), + (Ok(()), Err(error)) => Err(error.context("cleaning exact Gitlink checkout")), + } + } +} + +impl SourcePort for GitSourceDriver { + fn resolve( + &self, + binding: &OperatorBinding, + resolution_id: &str, + selected_at_unix_millis: u64, + ) -> Result { + binding.validate()?; + ensure!( + selected_at_unix_millis > 0, + "source selection has no timestamp" + ); + self.prepare_source_root(binding)?; + let fetched_ref = Self::admitted_ref_name(binding, resolution_id)?; self.git([ OsString::from("-C"), binding.repository.checkout.as_os_str().to_owned(), @@ -506,7 +769,15 @@ impl SourcePort for GitSourceDriver { binding.repository.admitted_ref )), ])?; - let (revision, selection) = self.resolve_selected_revision(binding, &fetched_ref)?; + let admitted_ref_revision = self.git_text([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("rev-parse"), + OsString::from(format!("{fetched_ref}^{{commit}}")), + ])?; + require_git_sha(&admitted_ref_revision, "fetched admitted-ref revision")?; + let (revision, selection) = + self.resolve_selected_revision(binding, &admitted_ref_revision)?; let source_tree = self.git_text([ OsString::from("-C"), binding.repository.checkout.as_os_str().to_owned(), @@ -514,35 +785,12 @@ impl SourcePort for GitSourceDriver { OsString::from(format!("{revision}^{{tree}}")), ])?; require_git_sha(&source_tree, "selected source tree")?; - let recipe_spec = format!("{}:{}", revision, binding.repository.recipe_path.display()); - let recipe_bytes = self - .git([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("show"), - recipe_spec.into(), - ])? - .stdout; - ensure!( - !recipe_bytes.is_empty(), - "selected deployment recipe is empty" - ); - - let mut gitlinks = BTreeMap::new(); - for path in binding.repository.gitlinks.keys() { - gitlinks.insert(path.clone(), self.gitlink_fact(binding, &revision, path)?); - } - let root = self.materialize_worktree(binding, &revision, &gitlinks)?; - let materialized_recipe = fs::read(root.join(&binding.repository.recipe_path)) - .context("reading materialized deployment recipe")?; - ensure!( - materialized_recipe == recipe_bytes, - "materialized recipe differs from the selected Git object" - ); + let (recipe_bytes, gitlinks) = self.exact_recipe_and_gitlinks(binding, &revision)?; let facts = SourceSelectionFacts { schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), origin: binding.repository.origin.clone(), admitted_ref: binding.repository.admitted_ref.clone(), + admitted_ref_revision, revision, source_tree, recipe_path: binding.repository.recipe_path.clone(), @@ -552,12 +800,156 @@ impl SourcePort for GitSourceDriver { selected_at_unix_millis, }; facts.validate_against(binding)?; - Ok(FrozenSource { + let resolved = ResolvedSource { facts, recipe_bytes, + }; + resolved.validate_against(binding)?; + Ok(resolved) + } + + fn freeze( + &self, + transaction_id: &str, + plan: &CompiledDeploymentPlan, + ) -> Result { + plan.validate()?; + require_driver_id(transaction_id, "source transaction")?; + let (_, binding) = plan.parsed_inputs()?; + let resolved = ResolvedSource { + facts: plan.source.clone(), + recipe_bytes: plan.recipe_blob.clone(), + }; + resolved.validate_against(&binding)?; + #[cfg(unix)] + ensure!( + unsafe { libc::geteuid() } == 0 && self.identity.is_some(), + "freezing source requires root Idunn with an unprivileged Git identity" + ); + self.prepare_source_root(&binding)?; + self.git([ + OsString::from("-C"), + binding.repository.checkout.as_os_str().to_owned(), + OsString::from("fetch"), + OsString::from("--no-tags"), + OsString::from("origin"), + resolved.facts.revision.clone().into(), + ]) + .context("fetching the durable exact source revision")?; + self.verify_exact_source(&binding, &resolved)?; + let transaction_root = + prepare_frozen_transaction_root(&self.frozen_source_root, transaction_id)?; + let partial = transaction_root.join(".partial"); + prepare_frozen_source_destination(&partial)?; + let materialization = (|| { + self.git_archive_into( + &binding.repository.checkout, + &resolved.facts.revision, + None, + &partial, + )?; + for (path, fact) in &resolved.facts.gitlinks { + self.materialize_gitlink_archive(&binding, path, fact, &partial)?; + } + let recipe_path = partial.join(&resolved.facts.recipe_path); + let recipe_metadata = fs::symlink_metadata(&recipe_path)?; + ensure!( + recipe_metadata.is_file() && !recipe_metadata.file_type().is_symlink(), + "frozen deployment recipe is not a regular file" + ); + let materialized_recipe = + fs::read(&recipe_path).context("reading frozen deployment recipe")?; + ensure!( + materialized_recipe == resolved.recipe_bytes, + "frozen recipe differs from the durable source resolution" + ); + harden_frozen_source(&partial)?; + validate_frozen_source(&partial)?; + frozen_source_sha256(&partial) + })(); + let snapshot_sha256 = match materialization { + Ok(snapshot_sha256) => snapshot_sha256, + Err(error) => { + let _ = remove_frozen_transaction_root(&self.frozen_source_root, transaction_id); + return Err(error); + } + }; + let snapshot_component = snapshot_sha256 + .strip_prefix("sha256-") + .expect("generated frozen source digest"); + let final_root = transaction_root.join(snapshot_component); + fs::rename(&partial, &final_root).context("publishing immutable frozen source")?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o500))?; + } + let receipt = FrozenSourceReceipt { + transaction_id: transaction_id.to_owned(), + plan_id: plan.plan_id.clone(), + snapshot_sha256, + }; + receipt.validate_against(plan)?; + Ok(receipt) + } + + fn observe_frozen( + &self, + plan: &CompiledDeploymentPlan, + receipt: &FrozenSourceReceipt, + ) -> Result { + receipt.validate_against(plan)?; + validate_frozen_source_store(&self.frozen_source_root)?; + let root = self + .frozen_source_root + .join(&receipt.transaction_id) + .join(receipt.snapshot_component()); + let canonical_store = self.frozen_source_root.canonicalize()?; + let canonical_root = root.canonicalize()?; + ensure!( + canonical_store == self.frozen_source_root + && canonical_root == root + && canonical_root.starts_with(&canonical_store), + "frozen source receipt resolves outside its authority root" + ); + validate_frozen_source(&root)?; + ensure!( + frozen_source_sha256(&root)? == receipt.snapshot_sha256, + "frozen source snapshot differs from its receipt" + ); + let recipe_path = root.join(&plan.source.recipe_path); + let recipe_metadata = fs::symlink_metadata(&recipe_path)?; + ensure!( + recipe_metadata.is_file() && !recipe_metadata.file_type().is_symlink(), + "observed deployment recipe is not a regular file" + ); + ensure!( + fs::read(&recipe_path)? == plan.recipe_blob, + "observed deployment recipe differs from the persisted plan" + ); + Ok(FrozenSource { + receipt: receipt.clone(), + facts: plan.source.clone(), + recipe_bytes: plan.recipe_blob.clone(), root, }) } + + fn cleanup(&self, transaction_id: &str, receipt: Option<&FrozenSourceReceipt>) -> Result<()> { + require_driver_id(transaction_id, "source transaction")?; + if let Some(receipt) = receipt { + ensure!( + receipt.transaction_id == transaction_id, + "frozen source cleanup receipt belongs to another transaction" + ); + require_sha256_id(&receipt.snapshot_sha256, "frozen source snapshot")?; + } + let transaction_root = self.frozen_source_root.join(transaction_id); + if !transaction_root.exists() { + return Ok(()); + } + remove_frozen_transaction_root(&self.frozen_source_root, transaction_id) + } } /// Docker is only a runner substrate. Recipe argv stays an argv vector, the @@ -807,6 +1199,7 @@ impl RunnerPort for DockerRunnerDriver { sealed_at_unix_millis: u64, ) -> Result { plan.validate()?; + source.receipt.validate_against(plan)?; ensure!( source.facts == plan.source && source.recipe_bytes == plan.recipe_blob, "runner source differs from the compiled plan" @@ -2239,6 +2632,337 @@ fn rfc3339_millis(millis: u64) -> Result { ) } +#[cfg(unix)] +fn validate_frozen_source_store(store: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!(store.is_absolute(), "frozen source store is not absolute"); + let canonical_store = store + .canonicalize() + .with_context(|| format!("resolving frozen source store {}", store.display()))?; + ensure!( + canonical_store == store, + "frozen source store traverses a symlink or noncanonical path" + ); + let metadata = fs::symlink_metadata(store)?; + ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.permissions().mode() & 0o022 == 0, + "frozen source store is not root-owned and nonwritable" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_frozen_source_store(_store: &Path) -> Result<()> { + bail!("frozen source materialization requires a Unix actuator") +} + +#[cfg(unix)] +fn prepare_frozen_transaction_root(store: &Path, transaction_id: &str) -> Result { + use std::os::unix::fs::PermissionsExt; + + require_driver_id(transaction_id, "source transaction")?; + validate_frozen_source_store(store)?; + let transaction_root = store.join(transaction_id); + if transaction_root.exists() { + remove_frozen_transaction_root(store, transaction_id)?; + } + fs::create_dir(&transaction_root)?; + fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o700))?; + Ok(transaction_root) +} + +#[cfg(not(unix))] +fn prepare_frozen_transaction_root(_store: &Path, _transaction_id: &str) -> Result { + bail!("frozen source materialization requires a Unix actuator") +} + +#[cfg(unix)] +fn remove_frozen_transaction_root(store: &Path, transaction_id: &str) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + require_driver_id(transaction_id, "source transaction")?; + validate_frozen_source_store(store)?; + let transaction_root = store.join(transaction_id); + let metadata = fs::symlink_metadata(&transaction_root)?; + ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.permissions().mode() & 0o022 == 0, + "frozen source transaction root is not Idunn-owned" + ); + fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o700))?; + fs::remove_dir_all(&transaction_root).with_context(|| { + format!( + "removing exact frozen source transaction {}", + transaction_root.display() + ) + }) +} + +#[cfg(not(unix))] +fn remove_frozen_transaction_root(_store: &Path, _transaction_id: &str) -> Result<()> { + bail!("frozen source cleanup requires a Unix actuator") +} + +#[cfg(unix)] +fn prepare_frozen_source_destination(destination: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "frozen source materialization requires root Idunn" + ); + ensure!( + destination.is_absolute(), + "frozen source destination is not absolute" + ); + let parent = destination + .parent() + .context("frozen source destination has no parent")?; + ensure!(destination != parent, "frozen source destination is broad"); + let canonical_parent = parent + .canonicalize() + .with_context(|| format!("resolving frozen source parent {}", parent.display()))?; + ensure!( + canonical_parent == parent, + "frozen source parent traverses a symlink or noncanonical path" + ); + let parent_metadata = fs::symlink_metadata(parent)?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "frozen source parent is not root-owned and nonwritable" + ); + if destination.exists() { + remove_frozen_source_destination(destination)?; + } + fs::create_dir(destination) + .with_context(|| format!("creating frozen source {}", destination.display()))?; + fs::set_permissions(destination, fs::Permissions::from_mode(0o700))?; + Ok(()) +} + +#[cfg(not(unix))] +fn prepare_frozen_source_destination(_destination: &Path) -> Result<()> { + bail!("frozen source materialization requires a Unix actuator") +} + +#[cfg(unix)] +fn remove_frozen_source_destination(destination: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + destination.is_absolute(), + "frozen source destination is not absolute" + ); + let parent = destination + .parent() + .context("frozen source destination has no parent")?; + ensure!(destination != parent, "frozen source destination is broad"); + let parent_metadata = fs::symlink_metadata(parent)?; + let metadata = fs::symlink_metadata(destination)?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0, + "frozen source parent is not root-owned and nonwritable" + ); + ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.permissions().mode() & 0o022 == 0, + "existing frozen source is not an Idunn-owned directory" + ); + fs::remove_dir_all(destination) + .with_context(|| format!("removing exact frozen source {}", destination.display())) +} + +#[cfg(not(unix))] +fn remove_frozen_source_destination(_destination: &Path) -> Result<()> { + bail!("frozen source materialization requires a Unix actuator") +} + +#[cfg(unix)] +fn harden_frozen_source(root: &Path) -> Result<()> { + harden_frozen_source_tree(root, root) +} + +#[cfg(unix)] +fn harden_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(current)?; + ensure!(metadata.uid() == 0, "frozen source entry is not root-owned"); + ensure!( + current == root || current.file_name() != Some(OsStr::new(".git")), + "frozen source contains forbidden .git metadata" + ); + if metadata.is_dir() { + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + harden_frozen_source_tree(root, &entry.path())?; + } + fs::set_permissions(current, fs::Permissions::from_mode(0o555))?; + } else if metadata.is_file() { + let mode = if metadata.permissions().mode() & 0o111 == 0 { + 0o444 + } else { + 0o555 + }; + fs::set_permissions(current, fs::Permissions::from_mode(mode))?; + } else if metadata.file_type().is_symlink() { + validate_frozen_source_symlink(root, current)?; + } else { + bail!("frozen source contains a special filesystem entry") + } + Ok(()) +} + +#[cfg(unix)] +fn validate_frozen_source_symlink(root: &Path, path: &Path) -> Result<()> { + let target = fs::read_link(path)?; + ensure!(target.is_relative(), "frozen source symlink is absolute"); + let parent = path + .parent() + .context("frozen source symlink has no parent")?; + let mut components = parent + .strip_prefix(root)? + .components() + .filter_map(|component| match component { + std::path::Component::Normal(value) => Some(value.to_os_string()), + _ => None, + }) + .collect::>(); + for component in target.components() { + match component { + std::path::Component::CurDir => {} + std::path::Component::Normal(value) => components.push(value.to_os_string()), + std::path::Component::ParentDir => { + ensure!( + components.pop().is_some(), + "frozen source symlink escapes its root" + ); + } + std::path::Component::RootDir | std::path::Component::Prefix(_) => { + bail!("frozen source symlink is absolute") + } + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn harden_frozen_source(_root: &Path) -> Result<()> { + bail!("frozen source materialization requires Unix permissions") +} + +#[cfg(unix)] +fn validate_frozen_source(root: &Path) -> Result<()> { + validate_frozen_source_tree(root, root) +} + +#[cfg(unix)] +fn validate_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(current)?; + ensure!(metadata.uid() == 0, "frozen source entry is not root-owned"); + ensure!( + current == root || current.file_name() != Some(OsStr::new(".git")), + "frozen source contains forbidden .git metadata" + ); + if metadata.is_dir() { + ensure!( + metadata.permissions().mode() & 0o777 == 0o555, + "frozen source directory is not 0555" + ); + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + validate_frozen_source_tree(root, &entry.path())?; + } + } else if metadata.is_file() { + ensure!( + matches!(metadata.permissions().mode() & 0o777, 0o444 | 0o555), + "frozen source file has a noncanonical mode" + ); + } else if metadata.file_type().is_symlink() { + validate_frozen_source_symlink(root, current)?; + } else { + bail!("frozen source contains a special filesystem entry") + } + Ok(()) +} + +#[cfg(not(unix))] +fn validate_frozen_source(_root: &Path) -> Result<()> { + bail!("frozen source observation requires Unix permissions") +} + +#[cfg(unix)] +fn frozen_source_sha256(root: &Path) -> Result { + let mut hasher = Sha256::new(); + hash_frozen_source_tree(root, root, &mut hasher)?; + Ok(format!("sha256-{:x}", hasher.finalize())) +} + +#[cfg(unix)] +fn hash_frozen_source_tree(root: &Path, current: &Path, hasher: &mut Sha256) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + let path = entry.path(); + let relative = normalized_relative(path.strip_prefix(root)?)?; + let metadata = fs::symlink_metadata(&path)?; + if metadata.is_dir() { + hasher.update(b"dir\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hash_frozen_source_tree(root, &path, hasher)?; + } else if metadata.is_file() { + let bytes = fs::read(&path)?; + hasher.update(b"file\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hasher.update( + (metadata.permissions().mode() & 0o111 != 0) + .to_string() + .as_bytes(), + ); + hasher.update(b"\0"); + hasher.update((bytes.len() as u64).to_le_bytes()); + hasher.update(&bytes); + } else if metadata.file_type().is_symlink() { + let target = fs::read_link(&path)?; + hasher.update(b"link\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hasher.update(target.as_os_str().as_encoded_bytes()); + hasher.update(b"\0"); + } else { + bail!("frozen source contains a special filesystem entry") + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn frozen_source_sha256(_root: &Path) -> Result { + bail!("frozen source observation requires Unix permissions") +} + #[cfg(unix)] fn harden_installed_release(root: &Path, artifacts: &[ArtifactReceipt]) -> Result<()> { let executable_paths = artifacts @@ -2678,6 +3402,32 @@ fn require_git_sha(value: &str, label: &str) -> Result<()> { Ok(()) } +fn require_sha256_id(value: &str, label: &str) -> Result<()> { + let digest = value + .strip_prefix("sha256-") + .with_context(|| format!("{label} has no sha256 prefix"))?; + ensure!( + digest.len() == 64 + && digest + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), + "{label} is not a lowercase SHA-256 id" + ); + Ok(()) +} + +fn require_driver_id(value: &str, label: &str) -> Result<()> { + ensure!( + !value.is_empty() + && value.len() <= 128 + && value + .bytes() + .all(|byte| { byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-') }), + "{label} id is invalid" + ); + Ok(()) +} + fn ensure_source_directory_tree( root: &Path, directory: &Path, @@ -3067,6 +3817,26 @@ mod tests { format!("sha256-{}", byte.to_string().repeat(64)) } + #[cfg(unix)] + fn git_at(repository: &Path, arguments: &[&str]) -> Result { + let output = Command::new("/usr/bin/git") + .arg("-C") + .arg(repository) + .args(arguments) + .env_clear() + .env("HOME", repository) + .env("PATH", "/usr/bin:/bin") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("LANG", "C.UTF-8") + .output()?; + ensure!( + output.status.success(), + "test Git failed: {}", + String::from_utf8_lossy(&output.stderr).trim() + ); + Ok(String::from_utf8(output.stdout)?.trim().to_owned()) + } + fn expected() -> IdunnExpectedIncarnationRecord { IdunnExpectedIncarnationRecord { schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), @@ -3273,4 +4043,87 @@ mod tests { assert!(validate_runner_secret(&secret, identity).is_err()); Ok(()) } + + #[cfg(unix)] + #[test] + fn exact_git_archive_becomes_root_owned_immutable_source_without_git_metadata() -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt, symlink}; + + let temp = tempfile::tempdir()?; + let repository = temp.path().join("repository"); + fs::create_dir(&repository)?; + git_at(&repository, &["init", "--initial-branch=main"])?; + git_at(&repository, &["config", "user.name", "Idunn Test"])?; + git_at( + &repository, + &["config", "user.email", "idunn-test@example.invalid"], + )?; + fs::write(repository.join("deployment.toml"), b"target = 'test'\n")?; + let script = repository.join("build.sh"); + fs::write(&script, b"#!/bin/sh\nexit 0\n")?; + fs::set_permissions(&script, fs::Permissions::from_mode(0o755))?; + fs::create_dir(repository.join("links"))?; + symlink("../deployment.toml", repository.join("links/deployment"))?; + git_at(&repository, &["add", "--all"])?; + git_at(&repository, &["commit", "-m", "fixture"])?; + let revision = git_at(&repository, &["rev-parse", "HEAD"])?; + + let parent = temp.path().join("transaction"); + fs::create_dir(&parent)?; + fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?; + let destination = parent.join("source"); + prepare_frozen_source_destination(&destination)?; + let driver = GitSourceDriver::new( + temp.path().join("source-cache"), + temp.path().join("frozen-source"), + None, + ); + driver.git_archive_into(&repository, &revision, None, &destination)?; + driver.git_archive_into( + &repository, + &revision, + Some(Path::new("vendor/fixture")), + &destination, + )?; + harden_frozen_source(&destination)?; + + assert!(!destination.join(".git").exists()); + assert_eq!( + fs::symlink_metadata(destination.join("deployment.toml"))? + .permissions() + .mode() + & 0o777, + 0o444 + ); + let script_metadata = fs::symlink_metadata(destination.join("build.sh"))?; + assert_eq!(script_metadata.uid(), 0); + assert_eq!(script_metadata.permissions().mode() & 0o777, 0o555); + assert!(destination.join("vendor/fixture/deployment.toml").is_file()); + validate_frozen_source_symlink(&destination, &destination.join("links/deployment"))?; + validate_frozen_source(&destination)?; + let digest = frozen_source_sha256(&destination)?; + let recipe = destination.join("deployment.toml"); + fs::set_permissions(&recipe, fs::Permissions::from_mode(0o644))?; + assert!(validate_frozen_source(&destination).is_err()); + fs::set_permissions(&recipe, fs::Permissions::from_mode(0o444))?; + fs::write(&recipe, b"target = 'changed'\n")?; + assert_ne!(frozen_source_sha256(&destination)?, digest); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn frozen_source_rejects_a_symlink_that_escapes_the_transaction_tree() -> Result<()> { + use std::os::unix::fs::{PermissionsExt, symlink}; + + let temp = tempfile::tempdir()?; + let parent = temp.path().join("transaction"); + fs::create_dir(&parent)?; + fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?; + let destination = parent.join("source"); + prepare_frozen_source_destination(&destination)?; + symlink("../../outside", destination.join("escape"))?; + assert!(harden_frozen_source(&destination).is_err()); + Ok(()) + } } From d527d72700a5109a15624a97a43569ce02110647 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 00:36:41 +0200 Subject: [PATCH 80/93] Map Idunn source actuation authority --- docs/idunn.md | 58 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/docs/idunn.md b/docs/idunn.md index 4446a61..7e26343 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -118,6 +118,50 @@ Managed dependency candidates are derived from validated Expected documents. External operator bindings remain expected configuration only; they cannot impersonate signed presence or readiness. +### Source resolution and actuation map + +- **Owner:** the Idunn deployment transaction owns the transition from one + observation of a moving admitted ref to runner-readable immutable source. + The source driver resolves through a transaction-unique fetched-ref witness; + the control loop must compile and durably persist the complete + `CompiledDeploymentPlan` before any source freeze or runner actuation. +- **Inputs:** the root-admitted raw operator-binding bytes; the exact repository + declaration blob read from the selected Git tree; one unique source-resolution + ID; the fetched admitted-ref revision; the selected commit and tree; and the + complete Gitlink path, origin, and revision set admitted by that binding. +- **Outputs:** private `ResolvedSource` facts used to compile the plan; a + plan-only exact archive in root-owned transaction storage; and a + `FrozenSourceReceipt` binding the transaction ID, plan ID, and snapshot digest. + The receipt is embedded in the observed `FrozenSource` and must validate + against the plan before a runner may consume the tree. +- **Derived state:** the source-identity checkout, unique fetched ref, temporary + Gitlink checkouts, extracted runner workspaces, and in-memory `FrozenSource` + view are disposable projections. None may replace the persisted plan, + receipt, or sealed release as transaction authority. +- **Forbidden writers:** a repository identity may perform Git and network + reads but cannot write the root-owned frozen tree. A moving ref, mutable + checkout, caller-supplied parsed binding, fresh recipe read, or incomplete + Gitlink subset cannot revise a compiled transaction. Resume may not resolve a + fresh binding or ref, and runner actuation may not accept source without its + plan-bound receipt. +- **Shared path:** initial execution resolves once, compiles and persists the + full plan, freezes only from that plan, persists the receipt, re-observes the + immutable snapshot, and runs the bound builders. Retry and crash recovery use + the same persisted plan and receipt; if freeze must be repeated, it fetches + only the exact planned revisions and never observes the moving ref again. + Transaction source storage is cleaned only after the sealed release has been + durably persisted. +- **Deletion line:** source-owned worktrees and Git metadata do not cross into + actuation storage; source selection does not return a runner-usable mutable + checkout; and no resume path is permitted to reconstruct authority from the + current repository binding or branch head. +- **Verification layer:** focused source-driver tests observe the unique + admitted-ref witness, exact Gitlink-set equality, root ownership, canonical + read-only modes, bounded symlinks, snapshot digest, and plan/receipt binding. + Transaction crash tests must additionally prove that every boundary after + plan persistence resumes without ref resolution and that cleanup cannot run + before sealed-release persistence. + ## Promotion and continuity For a routed stateful singleton, Idunn starts the candidate privately, observes @@ -158,9 +202,11 @@ graph conditions hold. ## Current implementation boundary `crates/idunn-daemon/src/deployment.rs` and `deployment_plan.rs` implement the -deterministic declaration, binding, plan, release, and Expected foundation. -They do not yet drive Git, runners, systemd, proxies, CultCache, CultMesh, -brakes, runtime activations, write leases, or runtime health verification. -Runtime integration must consume these contracts; it must not recreate target -catalogs, raw deployment commands, root Git inspection, or a second admission -opinion. +deterministic declaration, binding, plan, release, and Expected contracts. +`drivers.rs` implements the narrow source resolution/freeze, runner, systemd, +write-lease, topology, and route actuator ports. The transaction engine still +must persist and sequence the compiled plan, frozen-source receipt, sealed +release, activation, lease, readiness, route, and admitted generation. Driver +ports are consequences, not a second deployment controller; runtime integration +must not recreate target catalogs, raw deployment commands, root Git inspection, +or a second admission opinion. From 412978aeb1c6471d234af430f21ac00627daa025 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 05:23:06 +0200 Subject: [PATCH 81/93] Rebuild Idunn deployment transaction authority --- crates/idunn-daemon/src/control_plane.rs | 4171 ++++++++++++++++++-- crates/idunn-daemon/src/deployment.rs | 211 +- crates/idunn-daemon/src/deployment_plan.rs | 318 +- crates/idunn-daemon/src/drivers.rs | 2356 +++++++++-- vendor/cultnet-rs | 2 +- 5 files changed, 6375 insertions(+), 683 deletions(-) diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs index a668807..3a4fe62 100644 --- a/crates/idunn-daemon/src/control_plane.rs +++ b/crates/idunn-daemon/src/control_plane.rs @@ -1,50 +1,79 @@ -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::env; -use std::fs; +use std::fs::{self, File, OpenOptions}; use std::path::{Path, PathBuf}; use std::thread; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -use anyhow::{Context, Result, anyhow, bail, ensure}; +use anyhow::{anyhow, bail, ensure, Context, Result}; use cultcache_rs::{ CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, }; +use cultnet_rs::{ + authenticate_odin_runtime_topology_correlation, derive_service_identity_id, + evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, open_service_identity_at, + verify_idunn_deployment_brake_authorization, verify_runtime_authority, + AuthenticatedOdinRuntimeTopologyCorrelation, GameCultProviderHealthIdentity, + IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, + IdunnDeploymentBrakeRecord, IdunnExpectedIncarnationRecord, IdunnLifecycleBrakeObservation, + IdunnLifecycleBrakeRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, + IdunnRuntimeActivationRecord, IdunnServiceIdentity, OdinTopologyAuthenticationContext, + OdinTopologyIdentity, ServiceIdentityProfile, ServiceIdentitySigner, + ServiceIdentityTrustAnchor, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCHEMA, + IDUNN_PROCESS_WRITE_LEASE_SCHEMA, +}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; use uuid::Uuid; -use crate::deployment::OperatorBinding; -use crate::drivers::ProcessIdentity; +use crate::deployment::{DependencyKind, OperatorBinding, StartupOrder}; +use crate::deployment_plan::{ + compile_deployment_plan, CompiledDeploymentPlan, DependencyProviderAuthority, SealedRelease, +}; +use crate::drivers::{ + CultCacheTopologyDriver, CultCacheWriteLeaseDriver, DockerRunnerDriver, FrozenSourceReceipt, + GitSourceDriver, InstalledReleaseObservation, NginxRouteDriver, ProcessIdentity, + RouteObservation, RoutePort, RoutePreflightReceipt, RunnerPort, SourcePort, + SystemdTransientWorkloadDriver, TopologyPort, WorkloadObservation, WorkloadPort, + WriteLeasePort, +}; -const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v1"; +const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v2"; +const DEPLOYMENT_TRANSACTION_SCHEMA: &str = "idunn.deployment_transaction.v1"; +const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v1"; +const DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS: u64 = 30_000; +const DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS: u64 = 2_000; +/// An immutable request. Execution state deliberately does not fit in this +/// record; status is derived from the transactions created for it. #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.deployment_command", - schema = "idunn.deployment_command.v1" + schema = "idunn.deployment_command.v2" )] -struct DeploymentCommandRecord { +struct DeploymentCommand { #[cultcache(key = 0)] schema_version: String, #[cultcache(key = 1)] command_id: String, #[cultcache(key = 2)] - selector: String, + kind: CommandKind, #[cultcache(key = 3)] - requested_by: String, + selector: String, #[cultcache(key = 4)] - requested_at_unix_millis: u64, + requested_by: String, #[cultcache(key = 5)] - state: String, - #[cultcache(key = 6)] - owner_instance_id: Option, - #[cultcache(key = 7)] - started_at_unix_millis: Option, - #[cultcache(key = 8)] - completed_at_unix_millis: Option, - #[cultcache(key = 9)] - detail: String, + requested_at_unix_millis: u64, } -impl DeploymentCommandRecord { +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +enum CommandKind { + Deploy, + Continuity, +} + +impl DeploymentCommand { fn validate(&self) -> Result<()> { ensure!( self.schema_version == DEPLOYMENT_COMMAND_SCHEMA, @@ -57,44 +86,1040 @@ impl DeploymentCommandRecord { self.requested_at_unix_millis > 0, "deployment command has no request time" ); - ensure!( - matches!( - self.state.as_str(), - "pending" | "running" | "succeeded" | "failed" + match self.kind { + CommandKind::Deploy => ensure!( + !self.command_id.starts_with("continuity-"), + "operator deployment uses the continuity command namespace" ), - "deployment command state is invalid" + CommandKind::Continuity => ensure!( + self.command_id.starts_with("continuity-") + && !self.selector.starts_with("profile:"), + "continuity command identity or selector is invalid" + ), + } + Ok(()) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +#[repr(u8)] +enum DeploymentPhase { + Sealing, + Starting, + Warming, + Fencing, + Leasing, + AwaitingReady, + Routing, + Committing, + Complete, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct DeploymentAuthorization { + authorization_id: String, + brake_sha256: String, + canonical_brake_bytes: Vec, + authorized_at_unix_millis: u64, +} + +impl DeploymentAuthorization { + fn validate_shape(&self) -> Result<()> { + require_id(&self.authorization_id, "deployment authorization id")?; + ensure!( + !self.canonical_brake_bytes.is_empty() + && self.brake_sha256 == sha256_id(&self.canonical_brake_bytes) + && self.authorized_at_unix_millis > 0, + "deployment authorization receipt is incomplete" + ); + let record: IdunnDeploymentBrakeRecord = + rmp_serde::from_slice(&self.canonical_brake_bytes)?; + record.validate()?; + ensure!( + rmp_serde::to_vec(&record)? == self.canonical_brake_bytes + && record.authorization_id.as_deref() == Some(&self.authorization_id), + "deployment authorization receipt is noncanonical or mismatched" + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct TopologyEvidence { + canonical_bytes: Vec, + canonical_sha256: String, + signer_identity_id: String, + publisher_sequence: u64, + admitted_at_unix_millis: u64, +} + +impl TopologyEvidence { + fn from_authenticated( + topology: &AuthenticatedOdinRuntimeTopologyCorrelation, + admitted_at_unix_millis: u64, + ) -> Result { + let record = topology.record(); + let evidence = Self { + canonical_bytes: topology.canonical_bytes().to_vec(), + canonical_sha256: sha256_id(topology.canonical_bytes()), + signer_identity_id: record.signer_identity_id.clone(), + publisher_sequence: record.publisher_sequence, + admitted_at_unix_millis, + }; + evidence.validate_shape()?; + Ok(evidence) + } + + fn validate_shape(&self) -> Result<()> { + ensure!( + !self.canonical_bytes.is_empty() + && self.canonical_sha256 == sha256_id(&self.canonical_bytes), + "topology evidence bytes or digest are invalid" + ); + require_id(&self.signer_identity_id, "Odin topology signer")?; + ensure!( + self.publisher_sequence > 0 && self.admitted_at_unix_millis > 0, + "topology evidence sequence or admission time is invalid" + ); + Ok(()) + } +} + +/// Opaque capability minted only after the transaction CAS advances the Odin +/// replay cursor and stores these exact bytes. Adjacent drivers may inspect the +/// authenticated receipt, but cannot construct replay admission themselves. +#[derive(Clone, Debug)] +pub struct SequenceAdmittedWarming { + transaction_id: String, + evidence: TopologyEvidence, + authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, +} + +impl SequenceAdmittedWarming { + #[cfg(test)] + pub(crate) fn for_test( + transaction_id: impl Into, + authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, + admitted_at_unix_millis: u64, + ) -> Result { + let record = authenticated.record(); + ensure!( + record.present + && !record.ready + && record.observed_presence_state.as_deref() == Some("warming") + && record.observed_write_lease_sha256.is_none() + && record.disagreements.is_empty(), + "test receipt is not exact Warming evidence" + ); + let transaction_id = transaction_id.into(); + require_id(&transaction_id, "test transaction id")?; + let evidence = + TopologyEvidence::from_authenticated(&authenticated, admitted_at_unix_millis)?; + Ok(Self { + transaction_id, + evidence, + authenticated, + }) + } + + pub(crate) fn authenticated(&self) -> &AuthenticatedOdinRuntimeTopologyCorrelation { + &self.authenticated + } + + pub(crate) fn transaction_id(&self) -> &str { + &self.transaction_id + } + + pub(crate) fn evidence_sha256(&self) -> &str { + &self.evidence.canonical_sha256 + } +} + +/// Ready proof with the same construction boundary. Dependency planning and +/// promotion receive this token, never caller-supplied digest/sequence tuples. +#[derive(Clone, Debug)] +pub(crate) struct SequenceAdmittedReady { + transaction_id: String, + evidence: TopologyEvidence, + expected: IdunnExpectedIncarnationRecord, + authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, +} + +impl SequenceAdmittedReady { + #[cfg(test)] + pub(crate) fn for_test( + expected: &IdunnExpectedIncarnationRecord, + authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, + admitted_at_unix_millis: u64, + ) -> Result { + expected.validate()?; + let record = authenticated.record(); + ensure!( + record.expected_projection_sha256 == expected.canonical_sha256()? + && record.target == expected.target + && record.runtime_id == expected.runtime_id + && record.present + && record.ready + && record.observed_presence_state.as_deref() == Some("active") + && record.disagreements.is_empty(), + "test receipt is not exact Ready evidence" + ); + let evidence = + TopologyEvidence::from_authenticated(&authenticated, admitted_at_unix_millis)?; + Ok(Self { + transaction_id: "test-sequence-admission".into(), + evidence, + expected: expected.clone(), + authenticated, + }) + } + + pub(crate) fn authenticated(&self) -> &AuthenticatedOdinRuntimeTopologyCorrelation { + &self.authenticated + } + + pub(crate) fn expected(&self) -> &IdunnExpectedIncarnationRecord { + &self.expected + } + + pub(crate) fn transaction_id(&self) -> &str { + &self.transaction_id + } + + pub(crate) fn evidence_sha256(&self) -> &str { + &self.evidence.canonical_sha256 + } + + pub(crate) fn publisher_sequence(&self) -> u64 { + self.evidence.publisher_sequence + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] +enum FencingEvidence { + SkippedStateless, + Revoked { + incumbent_lease_sha256: Option, + candidate_lease_path_verified_empty: bool, + }, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] +enum LeasingEvidence { + SkippedStateless, + Prepared { + lease: IdunnProcessWriteLeaseRecord, + lease_sha256: String, + }, + Granted { + lease: IdunnProcessWriteLeaseRecord, + lease_sha256: String, + }, +} + +impl LeasingEvidence { + fn lease(&self) -> Option<&IdunnProcessWriteLeaseRecord> { + match self { + Self::SkippedStateless | Self::Prepared { .. } => None, + Self::Granted { lease, .. } => Some(lease), + } + } + + fn prepared_lease(&self) -> Option<(&IdunnProcessWriteLeaseRecord, &str)> { + match self { + Self::Prepared { + lease, + lease_sha256, + } => Some((lease, lease_sha256)), + Self::SkippedStateless | Self::Granted { .. } => None, + } + } + + fn lease_sha256(&self) -> Option<&str> { + match self { + Self::SkippedStateless | Self::Prepared { .. } => None, + Self::Granted { lease_sha256, .. } => Some(lease_sha256), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] +enum RoutingEvidence { + SkippedUnrouted, + Promoted { observation: RouteObservation }, +} + +impl RoutingEvidence { + fn observation(&self) -> Option<&RouteObservation> { + match self { + Self::SkippedUnrouted => None, + Self::Promoted { observation } => Some(observation), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct IsolationEvidence { + candidate_uid: u32, + candidate_pid_namespace_id: u64, + candidate_mount_namespace_id: u64, + incumbent_uid: Option, + incumbent_pid_namespace_id: Option, + incumbent_mount_namespace_id: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "outcome", rename_all = "kebab-case", deny_unknown_fields)] +enum TransactionCompletion { + Admitted { generation_id: String }, + FailedBeforeFencing { error: String }, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +enum CleanupEvidence { + Pending, + Skipped, + Complete, +} + +impl CleanupEvidence { + fn is_complete(self) -> bool { + matches!(self, Self::Skipped | Self::Complete) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct PreFencingAbort { + error: String, + candidate_stop: CleanupEvidence, + topology_reconciliation: CleanupEvidence, + source_cleanup: CleanupEvidence, +} + +impl PreFencingAbort { + fn is_complete(&self) -> bool { + self.candidate_stop.is_complete() + && self.topology_reconciliation.is_complete() + && self.source_cleanup.is_complete() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] +enum IncumbentCleanupEvidence { + SkippedNoIncumbent, + Pending { + generation_id: String, + workload: WorkloadObservation, + }, + Complete { + generation_id: String, + }, +} + +impl IncumbentCleanupEvidence { + fn is_complete(&self) -> bool { + matches!(self, Self::SkippedNoIncumbent | Self::Complete { .. }) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +enum SourceCleanupEvidence { + SkippedContinuity, + Pending, + Complete, +} + +impl SourceCleanupEvidence { + fn is_complete(self) -> bool { + !matches!(self, Self::Pending) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct PostCommitCleanup { + incumbent: IncumbentCleanupEvidence, + source: SourceCleanupEvidence, +} + +impl PostCommitCleanup { + fn is_complete(&self) -> bool { + self.incumbent.is_complete() && self.source.is_complete() + } +} + +/// The sole owner of an in-flight deployment decision. Every actuator result +/// is durable here before a later phase is entered. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.deployment_transaction", + schema = "idunn.deployment_transaction.v1" +)] +struct DeploymentTransaction { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + transaction_id: String, + #[cultcache(key = 2)] + command_id: String, + #[cultcache(key = 3)] + command_kind: CommandKind, + #[cultcache(key = 4)] + target: String, + #[cultcache(key = 5)] + ordinal: u32, + #[cultcache(key = 6)] + phase: DeploymentPhase, + #[cultcache(key = 7)] + created_at_unix_millis: u64, + #[cultcache(key = 8)] + updated_at_unix_millis: u64, + #[cultcache(key = 9)] + incumbent_generation_id: Option, + #[cultcache(key = 10)] + plan: Option, + #[cultcache(key = 11)] + frozen_source: Option, + #[cultcache(key = 12)] + sealed_release: Option, + #[cultcache(key = 13)] + installed_release: Option, + #[cultcache(key = 14)] + expected: Option, + #[cultcache(key = 15)] + expected_publication_sha256: Option, + #[cultcache(key = 16)] + deployment_authorization: Option, + #[cultcache(key = 17)] + lifecycle_authorized_at_unix_millis: Option, + #[cultcache(key = 18)] + activation: Option, + #[cultcache(key = 19)] + workload: Option, + #[cultcache(key = 20)] + activation_publication_sha256: Option, + #[cultcache(key = 21)] + latest_odin_observation: Option, + #[cultcache(key = 22)] + warming: Option, + #[cultcache(key = 23)] + route_preflight: Option, + #[cultcache(key = 24)] + isolation: Option, + #[cultcache(key = 25)] + fencing: Option, + #[cultcache(key = 26)] + leasing: Option, + #[cultcache(key = 27)] + ready: Option, + #[cultcache(key = 28)] + routing: Option, + #[cultcache(key = 29)] + odin_publisher_sequence_cursor: u64, + #[cultcache(key = 30)] + last_error: Option, + #[cultcache(key = 31)] + completion: Option, + #[cultcache(key = 32)] + pre_fencing_abort: Option, + #[cultcache(key = 33)] + post_commit_cleanup: Option, +} + +impl DeploymentTransaction { + fn new( + command: &DeploymentCommand, + target: String, + ordinal: u32, + incumbent: Option<&AdmittedGeneration>, + now: u64, + ) -> Result { + let transaction = Self { + schema_version: DEPLOYMENT_TRANSACTION_SCHEMA.into(), + transaction_id: format!("tx-{}", Uuid::new_v4()), + command_id: command.command_id.clone(), + command_kind: command.kind, + target, + ordinal, + phase: DeploymentPhase::Sealing, + created_at_unix_millis: now, + updated_at_unix_millis: now, + incumbent_generation_id: incumbent.map(|value| value.generation_id.clone()), + plan: None, + frozen_source: None, + sealed_release: None, + installed_release: None, + expected: None, + expected_publication_sha256: None, + deployment_authorization: None, + lifecycle_authorized_at_unix_millis: None, + activation: None, + workload: None, + activation_publication_sha256: None, + latest_odin_observation: None, + warming: None, + route_preflight: None, + isolation: None, + fencing: None, + leasing: None, + ready: None, + routing: None, + odin_publisher_sequence_cursor: incumbent + .map_or(0, |value| value.odin_publisher_sequence_cursor), + last_error: None, + completion: None, + pre_fencing_abort: None, + post_commit_cleanup: None, + }; + transaction.validate()?; + Ok(transaction) + } + + fn from_continuity( + command: &DeploymentCommand, + incumbent: &AdmittedGeneration, + now: u64, + ) -> Result { + let mut transaction = + Self::new(command, incumbent.target.clone(), 0, Some(incumbent), now)?; + transaction.plan = Some(incumbent.plan.clone()); + transaction.sealed_release = Some(incumbent.sealed_release.clone()); + transaction.installed_release = Some(incumbent.installed_release.clone()); + transaction.expected = Some(incumbent.expected.clone()); + transaction.validate()?; + Ok(transaction) + } + + fn rejected(command: &DeploymentCommand, error: anyhow::Error, now: u64) -> Result { + let mut transaction = Self::new(command, command.selector.clone(), 0, None, now)?; + let detail = truncate(&format!("{error:#}"), 2048); + transaction.phase = DeploymentPhase::Complete; + transaction.updated_at_unix_millis = now; + transaction.last_error = Some(detail.clone()); + transaction.pre_fencing_abort = Some(PreFencingAbort { + error: detail.clone(), + candidate_stop: CleanupEvidence::Skipped, + topology_reconciliation: CleanupEvidence::Skipped, + source_cleanup: CleanupEvidence::Skipped, + }); + transaction.completion = Some(TransactionCompletion::FailedBeforeFencing { error: detail }); + transaction.validate()?; + Ok(transaction) + } + + fn is_terminal(&self) -> bool { + if self.phase != DeploymentPhase::Complete { + return false; + } + match &self.completion { + Some(TransactionCompletion::FailedBeforeFencing { .. }) => self + .pre_fencing_abort + .as_ref() + .is_some_and(PreFencingAbort::is_complete), + Some(TransactionCompletion::Admitted { .. }) => self + .post_commit_cleanup + .as_ref() + .is_some_and(PostCommitCleanup::is_complete), + None => false, + } + } + + /// Complete transactions retain retryable cleanup work, but admission has + /// already transferred current-incarnation authority to AdmittedGeneration. + fn owns_target_authority(&self) -> bool { + self.phase != DeploymentPhase::Complete + } + + fn validate(&self) -> Result<()> { + ensure!( + self.schema_version == DEPLOYMENT_TRANSACTION_SCHEMA, + "deployment transaction schema is unsupported" + ); + require_id(&self.transaction_id, "deployment transaction id")?; + require_id(&self.command_id, "transaction command id")?; + require_id(&self.target, "transaction target")?; + ensure!( + self.created_at_unix_millis > 0 + && self.updated_at_unix_millis >= self.created_at_unix_millis, + "deployment transaction timestamps are invalid" + ); + if let Some(generation) = &self.incumbent_generation_id { + require_id(generation, "incumbent generation id")?; + } + if let Some(plan) = &self.plan { + plan.validate()?; + ensure!( + plan.parsed_inputs()?.1.target == self.target, + "transaction plan belongs to another target" + ); + } + if let (Some(plan), Some(frozen)) = (&self.plan, &self.frozen_source) { + frozen.validate_against(plan)?; + } + if let (Some(plan), Some(release)) = (&self.plan, &self.sealed_release) { + release.validate_against(plan)?; + } + if let Some(expected) = &self.expected { + expected.validate()?; + ensure!(expected.target == self.target, "Expected target differs"); + if let Some(plan) = &self.plan { + ensure!(expected.plan_id == plan.plan_id, "Expected plan differs"); + } + if let Some(release) = &self.sealed_release { + ensure!( + expected.sealed_release_id == release.sealed_release_id, + "Expected release differs" + ); + } + if let Some(digest) = &self.expected_publication_sha256 { + ensure!( + digest == &expected.canonical_sha256()?, + "Expected publication receipt differs" + ); + } + } + if let (Some(expected), Some(activation)) = (&self.expected, &self.activation) { + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()?, + "activation belongs to another Expected projection" + ); + if let Some(workload) = &self.workload { + ensure!( + workload.runtime_instance_id == activation.runtime_instance_id, + "workload belongs to another activation" + ); + } + if let Some(digest) = &self.activation_publication_sha256 { + ensure!( + digest == &activation.canonical_sha256()?, + "activation publication receipt differs" + ); + } + } + for evidence in [&self.latest_odin_observation, &self.warming, &self.ready] + .into_iter() + .flatten() + { + evidence.validate_shape()?; + ensure!( + evidence.publisher_sequence <= self.odin_publisher_sequence_cursor, + "topology evidence exceeds the transaction replay cursor" + ); + } + if let Some(leasing) = &self.leasing { + if let Some(expected) = &self.expected { + ensure!( + matches!( + (expected.write_lease_required, leasing), + (true, LeasingEvidence::Prepared { .. }) + | (true, LeasingEvidence::Granted { .. }) + | (false, LeasingEvidence::SkippedStateless) + ), + "lease evidence differs from the Expected state contract" + ); + } + let lease_and_sha256 = match leasing { + LeasingEvidence::SkippedStateless => None, + LeasingEvidence::Prepared { + lease, + lease_sha256, + } + | LeasingEvidence::Granted { + lease, + lease_sha256, + } => Some((lease, lease_sha256)), + }; + if let Some((lease, lease_sha256)) = lease_and_sha256 { + lease.validate()?; + ensure!( + lease.canonical_sha256()? == *lease_sha256, + "lease evidence digest differs" + ); + } + } + if let (Some(expected), Some(fencing)) = (&self.expected, &self.fencing) { + match fencing { + FencingEvidence::SkippedStateless => ensure!( + !expected.write_lease_required, + "stateful candidate claims stateless fencing" + ), + FencingEvidence::Revoked { + candidate_lease_path_verified_empty, + .. + } => ensure!( + *candidate_lease_path_verified_empty == expected.write_lease_required, + "candidate lease-path fencing differs from Expected" + ), + } + } + if let Some(error) = &self.last_error { + require_detail(error, "transaction error")?; + } + if let Some(authorization) = &self.deployment_authorization { + authorization.validate_shape()?; + } + if let Some(abort) = &self.pre_fencing_abort { + require_detail(&abort.error, "pre-fencing abort error")?; + ensure!( + self.post_commit_cleanup.is_none(), + "aborted transaction also carries post-commit cleanup" + ); + ensure!( + matches!( + (self.workload.is_some(), abort.candidate_stop), + (true, CleanupEvidence::Pending | CleanupEvidence::Complete) + | (false, CleanupEvidence::Skipped) + ), + "abort candidate-stop evidence differs from its candidate observation" + ); + let topology_cleanup_required = self.command_kind == CommandKind::Deploy + && self.expected_publication_sha256.is_some(); + ensure!( + matches!( + (topology_cleanup_required, abort.topology_reconciliation), + (true, CleanupEvidence::Pending | CleanupEvidence::Complete) + | (false, CleanupEvidence::Skipped) + ), + "abort topology cleanup differs from published deployment Expected" + ); + ensure!( + matches!( + (self.command_kind, abort.source_cleanup), + ( + CommandKind::Deploy, + CleanupEvidence::Pending | CleanupEvidence::Complete + ) | (CommandKind::Continuity, CleanupEvidence::Skipped) + ) || (self.command_kind == CommandKind::Deploy + && abort.source_cleanup == CleanupEvidence::Skipped + && self.plan.is_none() + && self.frozen_source.is_none() + && self.expected.is_none() + && self.phase == DeploymentPhase::Complete), + "abort source cleanup differs from command work" + ); + ensure!( + self.phase < DeploymentPhase::Fencing || self.phase == DeploymentPhase::Complete, + "pre-fencing abort crossed the fencing boundary" + ); + } + + let failed = matches!( + self.completion, + Some(TransactionCompletion::FailedBeforeFencing { .. }) + ); + if failed { + ensure!( + self.phase == DeploymentPhase::Complete, + "terminal failure is not Complete" + ); + let abort = required(&self.pre_fencing_abort, "terminal abort evidence")?; + let TransactionCompletion::FailedBeforeFencing { error } = + self.completion.as_ref().unwrap() + else { + unreachable!() + }; + ensure!( + abort.is_complete() && abort.error == *error, + "terminal failure lacks complete matching abort evidence" + ); + } else { + ensure!( + !matches!( + self.completion, + Some(TransactionCompletion::FailedBeforeFencing { .. }) + ), + "non-failure branch carries failure completion" + ); + if self.phase >= DeploymentPhase::Starting { + ensure!( + self.plan.is_some() + && self.sealed_release.is_some() + && self.installed_release.is_some() + && self.expected.is_some() + && self.expected_publication_sha256.is_some(), + "Starting transaction lacks sealed release evidence" + ); + match self.command_kind { + CommandKind::Deploy => ensure!( + self.frozen_source.is_some() && self.deployment_authorization.is_some(), + "deployment entered Starting without frozen source and brake consumption" + ), + CommandKind::Continuity => ensure!( + self.lifecycle_authorized_at_unix_millis.is_some() + && self.deployment_authorization.is_none(), + "continuity entered Starting without its lifecycle gate" + ), + } + } + if self.phase >= DeploymentPhase::Warming { + ensure!( + self.activation.is_some() + && self.workload.is_some() + && self.activation_publication_sha256.is_some(), + "Warming transaction lacks observed activation" + ); + } + if self.phase >= DeploymentPhase::Fencing { + ensure!( + self.warming.is_some() && self.isolation.is_some(), + "Fencing transaction lacks warming or isolation evidence" + ); + ensure!( + self.expected + .as_ref() + .and_then(|expected| expected.route.as_ref()) + .is_some() + == self.route_preflight.is_some(), + "route preflight does not match routed Expected" + ); + } + if self.phase >= DeploymentPhase::Leasing { + ensure!(self.fencing.is_some(), "Leasing lacks fencing evidence"); + } + if self.phase >= DeploymentPhase::AwaitingReady { + ensure!( + matches!( + self.leasing.as_ref(), + Some(LeasingEvidence::SkippedStateless) + | Some(LeasingEvidence::Granted { .. }) + ), + "AwaitingReady lacks finalized lease evidence" + ); + } + if self.phase >= DeploymentPhase::Routing { + ensure!(self.ready.is_some(), "Routing lacks Ready evidence"); + } + if self.phase >= DeploymentPhase::Committing { + ensure!(self.routing.is_some(), "Committing lacks routing evidence"); + } + if self.phase == DeploymentPhase::Complete { + ensure!( + matches!( + self.completion, + Some(TransactionCompletion::Admitted { .. }) + ), + "successful Complete transaction lacks admission receipt" + ); + ensure!( + self.pre_fencing_abort.is_none() && self.post_commit_cleanup.is_some(), + "admitted transaction lacks exclusive post-commit cleanup evidence" + ); + let TransactionCompletion::Admitted { generation_id } = + self.completion.as_ref().unwrap() + else { + unreachable!() + }; + ensure!( + generation_id == &format!("generation-{}", self.transaction_id), + "admission completion names another transaction generation" + ); + } else { + ensure!( + self.completion.is_none() && self.post_commit_cleanup.is_none(), + "non-Complete transaction has completion or post-commit state" + ); + } + } + if let Some(cleanup) = &self.post_commit_cleanup { + ensure!( + self.phase == DeploymentPhase::Complete + && matches!( + self.completion, + Some(TransactionCompletion::Admitted { .. }) + ), + "post-commit cleanup exists without an admitted Complete transaction" + ); + match (&self.incumbent_generation_id, &cleanup.incumbent) { + (None, IncumbentCleanupEvidence::SkippedNoIncumbent) => {} + ( + Some(expected), + IncumbentCleanupEvidence::Pending { generation_id, .. } + | IncumbentCleanupEvidence::Complete { generation_id }, + ) if expected == generation_id => {} + _ => bail!("post-commit incumbent cleanup names another generation"), + } + ensure!( + matches!( + (self.command_kind, cleanup.source), + (CommandKind::Deploy, SourceCleanupEvidence::Pending) + | (CommandKind::Deploy, SourceCleanupEvidence::Complete) + | ( + CommandKind::Continuity, + SourceCleanupEvidence::SkippedContinuity + ) + ), + "post-commit source cleanup differs from command kind" + ); + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct AdmittedOdinAuthority { + signer_identity_id: String, + signer_public_key: Vec, +} + +impl AdmittedOdinAuthority { + fn from_anchor(anchor: &ServiceIdentityTrustAnchor) -> Result { + ensure!( + derive_service_identity_id::(&anchor.public_key)? + == anchor.identity_id, + "Odin topology anchor identity differs from its key" + ); + Ok(Self { + signer_identity_id: anchor.identity_id.clone(), + signer_public_key: anchor.public_key.clone(), + }) + } + + fn validate(&self) -> Result<()> { + require_id(&self.signer_identity_id, "admitted Odin signer")?; + ensure!( + derive_service_identity_id::(&self.signer_public_key)? + == self.signer_identity_id, + "admitted Odin signer identity differs from its key" + ); + Ok(()) + } +} + +/// The only current-incarnation owner. Unit state, route state, topology +/// projections, and CLI output are observations of this record, never peers. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.admitted_generation", + schema = "idunn.admitted_generation.v1" +)] +struct AdmittedGeneration { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + target: String, + #[cultcache(key = 2)] + generation_id: String, + #[cultcache(key = 3)] + command_id: String, + #[cultcache(key = 4)] + transaction_id: String, + #[cultcache(key = 5)] + admitted_at_unix_millis: u64, + #[cultcache(key = 6)] + plan: CompiledDeploymentPlan, + #[cultcache(key = 7)] + sealed_release: SealedRelease, + #[cultcache(key = 8)] + installed_release: InstalledReleaseObservation, + #[cultcache(key = 9)] + expected: IdunnExpectedIncarnationRecord, + #[cultcache(key = 10)] + activation: IdunnRuntimeActivationRecord, + #[cultcache(key = 11)] + workload: WorkloadObservation, + #[cultcache(key = 12)] + leasing: LeasingEvidence, + #[cultcache(key = 13)] + ready: TopologyEvidence, + #[cultcache(key = 14)] + latest_odin_observation: TopologyEvidence, + #[cultcache(key = 15)] + routing: RoutingEvidence, + #[cultcache(key = 16)] + odin_authority: AdmittedOdinAuthority, + #[cultcache(key = 17)] + odin_publisher_sequence_cursor: u64, +} + +impl AdmittedGeneration { + fn from_transaction( + transaction: &DeploymentTransaction, + odin_authority: AdmittedOdinAuthority, + now: u64, + ) -> Result { + ensure!( + transaction.phase == DeploymentPhase::Committing, + "only Committing can create an admitted generation" + ); + let generation = Self { + schema_version: ADMITTED_GENERATION_SCHEMA.into(), + target: transaction.target.clone(), + generation_id: format!("generation-{}", transaction.transaction_id), + command_id: transaction.command_id.clone(), + transaction_id: transaction.transaction_id.clone(), + admitted_at_unix_millis: now, + plan: required(&transaction.plan, "transaction plan")?.clone(), + sealed_release: required(&transaction.sealed_release, "sealed release")?.clone(), + installed_release: required(&transaction.installed_release, "installed release")? + .clone(), + expected: required(&transaction.expected, "Expected projection")?.clone(), + activation: required(&transaction.activation, "activation")?.clone(), + workload: required(&transaction.workload, "workload")?.clone(), + leasing: required(&transaction.leasing, "lease disposition")?.clone(), + ready: required(&transaction.ready, "Ready receipt")?.clone(), + latest_odin_observation: required( + &transaction.latest_odin_observation, + "latest Odin receipt", + )? + .clone(), + routing: required(&transaction.routing, "route disposition")?.clone(), + odin_authority, + odin_publisher_sequence_cursor: transaction.odin_publisher_sequence_cursor, + }; + generation.validate()?; + Ok(generation) + } + + fn validate(&self) -> Result<()> { + ensure!( + self.schema_version == ADMITTED_GENERATION_SCHEMA, + "admitted generation schema is unsupported" + ); + require_id(&self.target, "admitted target")?; + require_id(&self.generation_id, "admitted generation id")?; + require_id(&self.command_id, "admitted command id")?; + require_id(&self.transaction_id, "admitted transaction id")?; + ensure!(self.admitted_at_unix_millis > 0, "admission has no time"); + self.plan.validate()?; + self.sealed_release.validate_against(&self.plan)?; + self.expected.validate()?; + self.activation.validate()?; + self.ready.validate_shape()?; + self.latest_odin_observation.validate_shape()?; + self.odin_authority.validate()?; + ensure!( + self.target == self.expected.target + && self.expected.plan_id == self.plan.plan_id + && self.expected.sealed_release_id == self.sealed_release.sealed_release_id + && self.activation.expected_projection_sha256 + == self.expected.canonical_sha256()? + && self.activation.runtime_instance_id == self.workload.runtime_instance_id + && self.ready.publisher_sequence <= self.odin_publisher_sequence_cursor + && self.latest_odin_observation.publisher_sequence + == self.odin_publisher_sequence_cursor, + "admitted generation evidence does not describe one incarnation" ); ensure!( - self.detail.len() <= 2048 && !self.detail.contains('\0'), - "deployment command detail is invalid" + self.expected.write_lease_required == self.leasing.lease().is_some(), + "admitted write-lease disposition differs from Expected" + ); + ensure!( + self.expected.route.is_some() == self.routing.observation().is_some(), + "admitted route disposition differs from Expected" ); - match self.state.as_str() { - "pending" => { - ensure!( - self.owner_instance_id.is_none() - && self.started_at_unix_millis.is_none() - && self.completed_at_unix_millis.is_none(), - "pending deployment command has execution state" - ); - } - "running" => { - ensure!( - self.owner_instance_id.is_some() - && self.started_at_unix_millis.is_some() - && self.completed_at_unix_millis.is_none(), - "running deployment command has partial ownership" - ); - } - "succeeded" | "failed" => { - ensure!( - self.owner_instance_id.is_some() - && self.started_at_unix_millis.is_some() - && self.completed_at_unix_millis.is_some(), - "terminal deployment command has partial receipt state" - ); - } - _ => unreachable!(), - } Ok(()) } } @@ -108,7 +1133,11 @@ struct RuntimeOptions { topology_store: PathBuf, odin_correlation_store: PathBuf, odin_trust_anchor: PathBuf, + idunn_identity_store: PathBuf, + deployment_brake_operator_anchor: PathBuf, source_identity: Option, + topology_maximum_age_millis: u64, + topology_maximum_future_skew_millis: u64, poll_millis: u64, } @@ -124,7 +1153,15 @@ impl Default for RuntimeOptions { "/var/lib/gamecult/odin/idunn-runtime-topology.cc", ), odin_trust_anchor: PathBuf::from("/etc/gamecult/idunn/odin-topology-anchor.cc"), + idunn_identity_store: PathBuf::from( + "/var/lib/gamecult/idunn/idunn-service-identity.cc", + ), + deployment_brake_operator_anchor: PathBuf::from( + "/etc/gamecult/idunn/deployment-brake-operator-anchor.cc", + ), source_identity: None, + topology_maximum_age_millis: DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS, + topology_maximum_future_skew_millis: DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS, poll_millis: 500, } } @@ -196,6 +1233,18 @@ fn parse_serve(mut args: impl Iterator) -> Result { options.odin_correlation_store = path_value(&mut args, &argument)? } "--odin-trust-anchor" => options.odin_trust_anchor = path_value(&mut args, &argument)?, + "--idunn-identity-store" => { + options.idunn_identity_store = path_value(&mut args, &argument)? + } + "--deployment-brake-operator-anchor" => { + options.deployment_brake_operator_anchor = path_value(&mut args, &argument)? + } + "--topology-maximum-age-millis" => { + options.topology_maximum_age_millis = u64_value(&mut args, &argument)? + } + "--topology-maximum-future-skew-millis" => { + options.topology_maximum_future_skew_millis = u64_value(&mut args, &argument)? + } "--source-uid" => source_uid = Some(u32_value(&mut args, &argument)?), "--source-gid" => source_gid = Some(u32_value(&mut args, &argument)?), "--poll-millis" => options.poll_millis = u64_value(&mut args, &argument)?, @@ -203,7 +1252,10 @@ fn parse_serve(mut args: impl Iterator) -> Result { _ => bail!("unknown Idunn serve option {argument:?}"), } } - ensure!(options.poll_millis > 0, "--poll-millis must be positive"); + ensure!( + options.poll_millis > 0 && options.topology_maximum_age_millis > 0, + "poll and topology maximum age must be positive" + ); options.source_identity = match (source_uid, source_gid) { (Some(uid), Some(gid)) => { ensure!( @@ -268,46 +1320,328 @@ fn parse_status(mut args: impl Iterator) -> Result { }) } -fn serve(options: RuntimeOptions) -> Result<()> { - for path in [ - &options.state_store, - &options.topology_store, - &options.staging_root, - ] { - let directory = if path.extension().is_some() { - path.parent() - .context("configured Idunn path has no parent")? - } else { - path.as_path() - }; - fs::create_dir_all(directory) - .with_context(|| format!("creating Idunn directory {}", directory.display()))?; - } - let instance_id = format!("idunn-{}", Uuid::new_v4()); - loop { - if let Some(command) = claim_next(&options.state_store, &instance_id)? { - let result = execute(&options, &command); - complete(&options.state_store, &command, result)?; - } else { - thread::sleep(Duration::from_millis(options.poll_millis)); +#[derive(Clone)] +struct Stored { + envelope: CultCacheEnvelope, + value: T, +} + +#[derive(Default)] +struct ControlSnapshot { + commands: Vec>, + transactions: Vec>, + admitted: Vec>, +} + +impl ControlSnapshot { + fn read(path: &Path) -> Result { + if !path.exists() { + return Ok(Self::default()); } + let mut snapshot = Self::default(); + for envelope in SingleFileMessagePackBackingStore::new(path) + .pull_all_read_only_snapshot() + .context("reading Idunn control snapshot")? + { + match envelope.r#type.as_str() { + DeploymentCommand::TYPE => { + ensure!( + envelope.schema_id.as_deref() == Some(DEPLOYMENT_COMMAND_SCHEMA), + "Idunn control store contains an unsupported command" + ); + let value: DeploymentCommand = decode_record(&envelope)?; + value.validate()?; + ensure!( + envelope.key == value.command_id, + "deployment command key differs from its identity" + ); + snapshot.commands.push(Stored { envelope, value }); + } + DeploymentTransaction::TYPE => { + ensure!( + envelope.schema_id.as_deref() == Some(DEPLOYMENT_TRANSACTION_SCHEMA), + "Idunn control store contains an unsupported transaction" + ); + let value: DeploymentTransaction = decode_record(&envelope)?; + value.validate()?; + ensure!( + envelope.key == value.transaction_id, + "deployment transaction key differs from its identity" + ); + snapshot.transactions.push(Stored { envelope, value }); + } + AdmittedGeneration::TYPE => { + ensure!( + envelope.schema_id.as_deref() == Some(ADMITTED_GENERATION_SCHEMA), + "Idunn control store contains an unsupported admitted generation" + ); + let value: AdmittedGeneration = decode_record(&envelope)?; + value.validate()?; + ensure!( + envelope.key == value.target, + "admitted generation key is not its target" + ); + snapshot.admitted.push(Stored { envelope, value }); + } + _ => bail!("Idunn control store contains a foreign document"), + } + } + snapshot.validate_relations()?; + Ok(snapshot) + } + + fn validate_relations(&self) -> Result<()> { + let command_ids = self + .commands + .iter() + .map(|stored| stored.value.command_id.as_str()) + .collect::>(); + ensure!( + command_ids.len() == self.commands.len(), + "Idunn control store contains duplicate commands" + ); + let transaction_ids = self + .transactions + .iter() + .map(|stored| stored.value.transaction_id.as_str()) + .collect::>(); + ensure!( + transaction_ids.len() == self.transactions.len(), + "Idunn control store contains duplicate transactions" + ); + let admitted_targets = self + .admitted + .iter() + .map(|stored| stored.value.target.as_str()) + .collect::>(); + ensure!( + admitted_targets.len() == self.admitted.len(), + "Idunn control store contains duplicate current generations" + ); + for transaction in &self.transactions { + let command = self + .commands + .iter() + .find(|candidate| candidate.value.command_id == transaction.value.command_id) + .context("deployment transaction has no immutable command")?; + ensure!( + command.value.kind == transaction.value.command_kind, + "transaction kind differs from its immutable command" + ); + } + let mut live_targets = BTreeSet::new(); + for transaction in self + .transactions + .iter() + .filter(|stored| stored.value.owns_target_authority()) + { + ensure!( + live_targets.insert(transaction.value.target.as_str()), + "multiple transactions claim current-incarnation authority for one target" + ); + if let Some(fencing) = transaction + .value + .fencing + .as_ref() + .filter(|_| transaction.value.phase < DeploymentPhase::Complete) + { + let incumbent = self.admitted_for(&transaction.value.target); + let incumbent_matches = + match (&transaction.value.incumbent_generation_id, incumbent) { + (None, None) => true, + (Some(expected), Some(current)) => current.value.generation_id == *expected, + _ => false, + }; + ensure!( + incumbent_matches, + "fenced transaction incumbent differs from current admission" + ); + let incumbent_lease_sha256 = incumbent + .and_then(|stored| stored.value.leasing.lease()) + .map(IdunnProcessWriteLeaseRecord::canonical_sha256) + .transpose()?; + match fencing { + FencingEvidence::SkippedStateless => ensure!( + incumbent_lease_sha256.is_none(), + "stateless fencing omitted the admitted incumbent lease" + ), + FencingEvidence::Revoked { + incumbent_lease_sha256: recorded, + .. + } => { + ensure!( + recorded == &incumbent_lease_sha256, + "fencing evidence names another incumbent lease" + ); + ensure!( + incumbent_lease_sha256.is_some() + || transaction + .value + .expected + .as_ref() + .is_some_and(|expected| expected.write_lease_required), + "stateless transaction did not use its explicit fencing skip" + ); + } + } + } + } + let mut authorizations = BTreeSet::new(); + for authorization in self + .transactions + .iter() + .filter_map(|stored| stored.value.deployment_authorization.as_ref()) + { + ensure!( + authorizations.insert(authorization.authorization_id.as_str()), + "deployment authorization was consumed more than once" + ); + } + Ok(()) + } + + fn admitted_for(&self, target: &str) -> Option<&Stored> { + self.admitted + .iter() + .find(|stored| stored.value.target == target) + } + + fn transaction_for_command(&self, command_id: &str) -> Vec<&DeploymentTransaction> { + let mut values = self + .transactions + .iter() + .filter(|stored| stored.value.command_id == command_id) + .map(|stored| &stored.value) + .collect::>(); + values.sort_by_key(|value| value.ordinal); + values + } + + fn has_earlier_authority_sibling(&self, transaction: &DeploymentTransaction) -> bool { + self.transactions.iter().any(|stored| { + stored.value.owns_target_authority() + && stored.value.command_id == transaction.command_id + && stored.value.ordinal < transaction.ordinal + }) + } + + fn max_odin_sequence(&self, signer_identity_id: &str) -> u64 { + let transaction_max = self + .transactions + .iter() + .filter_map(|stored| { + stored + .value + .latest_odin_observation + .as_ref() + .filter(|evidence| evidence.signer_identity_id == signer_identity_id) + .map(|evidence| evidence.publisher_sequence) + }) + .max() + .unwrap_or(0); + let admitted_max = self + .admitted + .iter() + .filter(|stored| stored.value.odin_authority.signer_identity_id == signer_identity_id) + .map(|stored| stored.value.odin_publisher_sequence_cursor) + .max() + .unwrap_or(0); + transaction_max.max(admitted_max) } } -fn execute(options: &RuntimeOptions, command: &DeploymentCommandRecord) -> Result { - let bindings = load_bindings(&options.bindings_dir)?; - let targets = resolve_selector(&bindings, &command.selector)?; +fn decode_record(envelope: &CultCacheEnvelope) -> Result +where + T: for<'de> Deserialize<'de> + Serialize, +{ + let value: T = rmp_serde::from_slice(&envelope.payload)?; ensure!( - !targets.is_empty(), - "deployment selector resolved to no targets" + rmp_serde::to_vec(&value)? == envelope.payload, + "Idunn control store contains a noncanonical record" ); - bail!( - "typed deployment drivers are not yet connected for {}", - targets.join(",") + Ok(value) +} + +fn command_envelope(value: &DeploymentCommand, now: u64) -> Result { + value.validate()?; + typed_envelope( + &value.command_id, + DeploymentCommand::TYPE, + DEPLOYMENT_COMMAND_SCHEMA, + value, + now, ) } -fn load_bindings(directory: &Path) -> Result> { +fn transaction_envelope(value: &DeploymentTransaction, now: u64) -> Result { + value.validate()?; + typed_envelope( + &value.transaction_id, + DeploymentTransaction::TYPE, + DEPLOYMENT_TRANSACTION_SCHEMA, + value, + now, + ) +} + +fn admitted_envelope(value: &AdmittedGeneration, now: u64) -> Result { + value.validate()?; + typed_envelope( + &value.target, + AdmittedGeneration::TYPE, + ADMITTED_GENERATION_SCHEMA, + value, + now, + ) +} + +fn typed_envelope( + key: &str, + record_type: &str, + schema: &str, + value: &T, + now: u64, +) -> Result { + Ok(CultCacheEnvelope { + key: key.into(), + r#type: record_type.into(), + payload: rmp_serde::to_vec(value)?, + stored_at: rfc3339_millis(now)?, + schema_id: Some(schema.into()), + }) +} + +fn replace_transaction( + store_path: &Path, + current: &Stored, + next: &DeploymentTransaction, +) -> Result<()> { + next.validate()?; + ensure!( + current.value.transaction_id == next.transaction_id, + "transaction replacement changes identity" + ); + ensure!( + SingleFileMessagePackBackingStore::new(store_path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentTransaction::TYPE.into(), + key: current.value.transaction_id.clone(), + current: Some(current.envelope.clone()), + }], + &[transaction_envelope(next, next.updated_at_unix_millis)?], + )?, + "deployment transaction changed before its compare-exchange" + ); + Ok(()) +} + +#[derive(Clone)] +struct LoadedBinding { + binding: OperatorBinding, + bytes: Vec, +} + +fn load_bindings(directory: &Path) -> Result> { let mut paths = fs::read_dir(directory) .with_context(|| format!("reading Idunn binding directory {}", directory.display()))? .map(|entry| entry.map(|entry| entry.path())) @@ -324,7 +1658,9 @@ fn load_bindings(directory: &Path) -> Result> .with_context(|| format!("validating operator binding {}", path.display()))?; let target = binding.target.clone(); ensure!( - bindings.insert(target.clone(), binding).is_none(), + bindings + .insert(target.clone(), LoadedBinding { binding, bytes }) + .is_none(), "operator binding target {target} is duplicated" ); } @@ -333,14 +1669,14 @@ fn load_bindings(directory: &Path) -> Result> } fn resolve_selector( - bindings: &BTreeMap, + bindings: &BTreeMap, selector: &str, ) -> Result> { - let targets = if let Some(profile) = selector.strip_prefix("profile:") { + let mut targets = if let Some(profile) = selector.strip_prefix("profile:") { bindings .values() - .filter(|binding| binding.profiles.contains(profile)) - .map(|binding| binding.target.clone()) + .filter(|binding| binding.binding.profiles.contains(profile)) + .map(|binding| binding.binding.target.clone()) .collect::>() } else { ensure!( @@ -353,6 +1689,11 @@ fn resolve_selector( !targets.is_empty(), "unknown or empty deployment profile {selector}" ); + targets.sort_by(|left, right| { + (left != "odin") + .cmp(&(right != "odin")) + .then_with(|| left.cmp(right)) + }); Ok(targets) } @@ -367,188 +1708,2119 @@ fn submit( fs::create_dir_all(parent)?; } let now = now_millis()?; - let record = DeploymentCommandRecord { + let command = DeploymentCommand { schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), command_id: format!("up-{}", Uuid::new_v4()), + kind: CommandKind::Deploy, selector: selector.into(), requested_by: requested_by.into(), requested_at_unix_millis: now, - state: "pending".into(), - owner_instance_id: None, - started_at_unix_millis: None, - completed_at_unix_millis: None, - detail: String::new(), }; - record.validate()?; - let store = SingleFileMessagePackBackingStore::new(store_path); - let next = typed_envelope(&record, now)?; - if !store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentCommandRecord::TYPE.into(), - key: record.command_id.clone(), - current: None, - }], - &[next], - )? { - bail!("deployment command id collided") - } - println!("{}", record.command_id); + command.validate()?; + ensure!( + SingleFileMessagePackBackingStore::new(store_path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentCommand::TYPE.into(), + key: command.command_id.clone(), + current: None, + }], + &[command_envelope(&command, now)?], + )?, + "deployment command id collided" + ); + println!("{}", command.command_id); if !wait { return Ok(()); } let deadline = now.saturating_add(timeout_seconds.saturating_mul(1000)); loop { - let current = read_command(store_path, &record.command_id)? - .context("submitted deployment command disappeared")?; - match current.state.as_str() { - "succeeded" => { - println!("succeeded {}", current.detail); - return Ok(()); + let snapshot = ControlSnapshot::read(store_path)?; + ensure!( + snapshot + .commands + .iter() + .any(|stored| stored.value.command_id == command.command_id), + "submitted deployment command disappeared" + ); + let transactions = snapshot.transaction_for_command(&command.command_id); + if !transactions.is_empty() && transactions.iter().all(|value| value.is_terminal()) { + if let Some(error) = + transactions + .iter() + .find_map(|transaction| match &transaction.completion { + Some(TransactionCompletion::FailedBeforeFencing { error }) => { + Some(error.as_str()) + } + _ => None, + }) + { + bail!("deployment failed: {error}") } - "failed" => bail!("deployment failed: {}", current.detail), - _ if now_millis()? >= deadline => bail!("deployment command timed out"), - _ => thread::sleep(Duration::from_millis(250)), + println!("succeeded {} target(s)", transactions.len()); + return Ok(()); } + if now_millis()? >= deadline { + bail!("deployment command timed out") + } + thread::sleep(Duration::from_millis(250)); } } fn status(store_path: &Path, command_id: Option<&str>) -> Result<()> { - let mut commands = read_commands(store_path)?; + let snapshot = ControlSnapshot::read(store_path)?; + let mut commands = snapshot.commands.iter().collect::>(); + commands.sort_by_key(|stored| stored.value.requested_at_unix_millis); if let Some(command_id) = command_id { - commands.retain(|command| command.command_id == command_id); + commands.retain(|stored| stored.value.command_id == command_id); ensure!(!commands.is_empty(), "deployment command is unknown"); } - commands.sort_by_key(|command| command.requested_at_unix_millis); - for command in commands { + for stored in commands { + let command = &stored.value; + let transactions = snapshot.transaction_for_command(&command.command_id); + let (state, detail) = derived_command_status(&transactions); println!( "{} {} {} {}", - command.command_id, command.selector, command.state, command.detail + command.command_id, command.selector, state, detail ); } Ok(()) } -fn claim_next(store_path: &Path, instance_id: &str) -> Result> { - let store = SingleFileMessagePackBackingStore::new(store_path); - let mut candidates = read_command_envelopes(store_path)? - .into_iter() - .filter(|(_, record)| record.state == "pending") - .collect::>(); - candidates.sort_by_key(|(_, command)| command.requested_at_unix_millis); - for (current, mut command) in candidates { - let now = now_millis()?; - command.state = "running".into(); - command.owner_instance_id = Some(instance_id.into()); - command.started_at_unix_millis = Some(now); - command.validate()?; - if store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentCommandRecord::TYPE.into(), - key: command.command_id.clone(), - current: Some(current), - }], - &[typed_envelope(&command, now)?], - )? { - return Ok(Some(command)); - } +fn derived_command_status(transactions: &[&DeploymentTransaction]) -> (&'static str, String) { + if transactions.is_empty() { + return ("queued", String::new()); } - Ok(None) + if let Some(error) = transactions + .iter() + .find_map(|transaction| match &transaction.completion { + Some(TransactionCompletion::FailedBeforeFencing { error }) => Some(error.clone()), + _ => None, + }) + { + return ("failed", error); + } + if transactions.iter().all(|transaction| { + transaction.is_terminal() + && matches!( + transaction.completion, + Some(TransactionCompletion::Admitted { .. }) + ) + }) { + return ( + "succeeded", + transactions + .iter() + .map(|transaction| transaction.target.as_str()) + .collect::>() + .join(","), + ); + } + let current = transactions + .iter() + .filter(|transaction| !transaction.is_terminal()) + .map(|transaction| format!("{}:{:?}", transaction.target, transaction.phase)) + .collect::>() + .join(","); + ("running", current) } -fn complete( - store_path: &Path, - running: &DeploymentCommandRecord, - result: Result, -) -> Result<()> { - let store = SingleFileMessagePackBackingStore::new(store_path); - let current = read_command_envelopes(store_path)? - .into_iter() - .find(|(_, command)| command.command_id == running.command_id) - .context("running deployment command disappeared")?; - ensure!( - current.1 == *running, - "deployment command changed while its owner was executing" - ); - let now = now_millis()?; - let mut terminal = running.clone(); - terminal.completed_at_unix_millis = Some(now); - match result { - Ok(detail) => { - terminal.state = "succeeded".into(); - terminal.detail = detail; +struct ProcessLock { + file: File, + #[cfg(not(unix))] + path: PathBuf, +} + +impl ProcessLock { + fn acquire(store_path: &Path) -> Result { + let path = sibling_path(store_path, ".daemon.lock"); + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; } - Err(error) => { - terminal.state = "failed".into(); - terminal.detail = truncate(&format!("{error:#}"), 2048); + #[cfg(unix)] + { + use std::os::fd::AsRawFd; + let file = OpenOptions::new() + .create(true) + .read(true) + .write(true) + .open(&path) + .with_context(|| format!("opening Idunn daemon lock {}", path.display()))?; + let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; + ensure!(result == 0, "another Idunn daemon owns this control store"); + Ok(Self { file }) + } + #[cfg(not(unix))] + { + let file = OpenOptions::new() + .create_new(true) + .read(true) + .write(true) + .open(&path) + .with_context(|| format!("another Idunn daemon owns {}", path.display()))?; + Ok(Self { file, path }) } } - terminal.validate()?; +} + +impl Drop for ProcessLock { + fn drop(&mut self) { + #[cfg(unix)] + { + use std::os::fd::AsRawFd; + let _ = unsafe { libc::flock(self.file.as_raw_fd(), libc::LOCK_UN) }; + } + #[cfg(not(unix))] + { + let _ = fs::remove_file(&self.path); + } + } +} + +struct Engine { + options: RuntimeOptions, + idunn_signer: ServiceIdentitySigner, + idunn_anchor: ServiceIdentityTrustAnchor, + bootstrap_odin_authority: AdmittedOdinAuthority, + source: GitSourceDriver, + runner: DockerRunnerDriver, + workload: SystemdTransientWorkloadDriver, +} + +impl Engine { + fn open(options: RuntimeOptions) -> Result { + let idunn_signer = + open_service_identity_at::(&options.idunn_identity_store) + .context("opening Idunn activation identity")?; + let idunn_anchor = idunn_signer.trust_anchor()?; + let odin_anchor = read_trust_anchor::(&options.odin_trust_anchor) + .context("reading bootstrap Odin topology anchor")?; + let bootstrap_odin_authority = AdmittedOdinAuthority::from_anchor(&odin_anchor)?; + let source = GitSourceDriver::new( + &options.source_root, + options.staging_root.join("frozen-sources"), + options.source_identity, + ); + Ok(Self { + options, + idunn_signer, + idunn_anchor, + bootstrap_odin_authority, + source, + runner: DockerRunnerDriver::default(), + workload: SystemdTransientWorkloadDriver::default(), + }) + } + + fn topology(&self) -> CultCacheTopologyDriver { + CultCacheTopologyDriver { + projection_store: self.options.topology_store.clone(), + correlation_store: self.options.odin_correlation_store.clone(), + } + } + + fn current_odin_authority(&self, snapshot: &ControlSnapshot) -> Result { + let authority = snapshot + .admitted_for("odin") + .map(|stored| stored.value.odin_authority.clone()) + .unwrap_or_else(|| self.bootstrap_odin_authority.clone()); + authority.validate()?; + Ok(authority) + } + + fn trusted_topology_context(&self, now: u64) -> OdinTopologyAuthenticationContext { + OdinTopologyAuthenticationContext { + trusted_received_at_unix_millis: now, + maximum_age_millis: self.options.topology_maximum_age_millis, + maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, + } + } + + fn validate_durable_authority(&self, snapshot: &ControlSnapshot) -> Result<()> { + let operator_anchor = read_trust_anchor::( + &self.options.deployment_brake_operator_anchor, + )?; + for stored in &snapshot.transactions { + let transaction = &stored.value; + if let Some(authorization) = &transaction.deployment_authorization { + authorization.validate_shape()?; + let record: IdunnDeploymentBrakeRecord = + rmp_serde::from_slice(&authorization.canonical_brake_bytes)?; + verify_idunn_deployment_brake_authorization(&record, &operator_anchor)?; + let expected = required(&transaction.expected, "authorized Expected projection")?; + ensure!( + record.authorized_release_id.as_deref() + == Some(expected.sealed_release_id.as_str()) + && record.authorized_deployment_id.as_deref() + == Some(transaction.transaction_id.as_str()) + && record.runtime_id == expected.runtime_id, + "durable deployment authorization names another release or transaction" + ); + } + let lease = transaction + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + if let Some(evidence) = &transaction.latest_odin_observation { + let authenticated = self.authenticate_topology_bytes( + snapshot, + transaction, + &evidence.canonical_bytes, + lease, + evidence.admitted_at_unix_millis, + )?; + validate_authenticated_evidence(evidence, &authenticated)?; + } + if let Some(evidence) = &transaction.warming { + let authenticated = self.authenticate_topology_bytes( + snapshot, + transaction, + &evidence.canonical_bytes, + None, + evidence.admitted_at_unix_millis, + )?; + validate_authenticated_evidence(evidence, &authenticated)?; + ensure!( + is_semantic_warming(&authenticated), + "durable Warming label is not exact semantic Warming" + ); + } + if let Some(evidence) = &transaction.ready { + let authenticated = self.authenticate_topology_bytes( + snapshot, + transaction, + &evidence.canonical_bytes, + lease, + evidence.admitted_at_unix_millis, + )?; + validate_authenticated_evidence(evidence, &authenticated)?; + ensure!( + is_semantic_ready(&authenticated), + "durable Ready label is not exact semantic Ready" + ); + } + } + let odin_authority = self.current_odin_authority(snapshot)?; + for stored in &snapshot.admitted { + let generation = &stored.value; + let authority = self.runtime_authority_parts( + &generation.plan, + &generation.expected, + &generation.activation, + )?; + let latest = authenticate_odin_runtime_topology_correlation( + &generation.latest_odin_observation.canonical_bytes, + &authority, + generation.leasing.lease_sha256(), + &odin_authority.signer_public_key, + self.trusted_topology_context( + generation.latest_odin_observation.admitted_at_unix_millis, + ), + )?; + validate_authenticated_evidence(&generation.latest_odin_observation, &latest)?; + let ready = authenticate_odin_runtime_topology_correlation( + &generation.ready.canonical_bytes, + &authority, + generation.leasing.lease_sha256(), + &odin_authority.signer_public_key, + self.trusted_topology_context(generation.ready.admitted_at_unix_millis), + )?; + validate_authenticated_evidence(&generation.ready, &ready)?; + ensure!( + is_semantic_ready(&ready), + "admitted generation Ready label is not exact semantic Ready" + ); + } + Ok(()) + } +} + +fn serve(options: RuntimeOptions) -> Result<()> { + for path in [ + &options.state_store, + &options.topology_store, + &options.staging_root, + ] { + let directory = if path.extension().is_some() { + path.parent() + .context("configured Idunn path has no parent")? + } else { + path.as_path() + }; + fs::create_dir_all(directory) + .with_context(|| format!("creating Idunn directory {}", directory.display()))?; + } + let _lock = ProcessLock::acquire(&options.state_store)?; + ControlSnapshot::read(&options.state_store).context("validating all Idunn records")?; + let engine = Engine::open(options)?; + engine.validate_durable_authority(&ControlSnapshot::read(&engine.options.state_store)?)?; + + loop { + if engine.resume_one_transaction()? { + thread::sleep(Duration::from_millis(engine.options.poll_millis)); + continue; + } + if engine.supervise_one_admitted_generation()? { + continue; + } + if engine.freeze_one_queued_command()? { + continue; + } + thread::sleep(Duration::from_millis(engine.options.poll_millis)); + } +} + +impl Engine { + /// Startup and every later loop use the same order: unfinished ownership + /// work first, admitted-body continuity second, new commands last. A + /// waiting transaction yields without relaxing ordinal order inside its + /// own command, so one target brake cannot suspend unrelated continuity. + fn resume_one_transaction(&self) -> Result { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let mut candidates = snapshot + .transactions + .iter() + .filter(|stored| !stored.value.is_terminal()) + .collect::>(); + candidates.sort_by_key(|stored| { + ( + stored.value.created_at_unix_millis, + stored.value.ordinal, + stored.value.transaction_id.as_str(), + ) + }); + for current in candidates { + if snapshot.has_earlier_authority_sibling(¤t.value) { + continue; + } + match self.advance_transaction(current) { + Ok(()) => {} + Err(error) if current.value.phase < DeploymentPhase::Fencing => { + self.begin_pre_fencing_abort(current, error)?; + } + Err(error) => self.record_resumable_error(current, &error)?, + } + let after = ControlSnapshot::read(&self.options.state_store)?; + let live = after + .transactions + .iter() + .find(|stored| stored.value.transaction_id == current.value.transaction_id) + .context("transaction disappeared while checking scheduler progress")?; + if live.envelope != current.envelope { + return Ok(true); + } + } + Ok(false) + } + + fn freeze_one_queued_command(&self) -> Result { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let transaction_commands = snapshot + .transactions + .iter() + .map(|stored| stored.value.command_id.as_str()) + .collect::>(); + let mut queued = snapshot + .commands + .iter() + .filter(|stored| { + stored.value.kind == CommandKind::Deploy + && !transaction_commands.contains(stored.value.command_id.as_str()) + }) + .collect::>(); + queued.sort_by_key(|stored| stored.value.requested_at_unix_millis); + let Some(command) = queued.first() else { + return Ok(false); + }; + let bindings = match load_bindings(&self.options.bindings_dir) { + Ok(bindings) => bindings, + Err(error) => { + eprintln!( + "Idunn left command {} queued because operator bindings are invalid: {error:#}", + command.value.command_id + ); + return Ok(false); + } + }; + let targets = match resolve_selector(&bindings, &command.value.selector) { + Ok(targets) => targets, + Err(error) => { + let now = now_millis()?; + let rejected = DeploymentTransaction::rejected(&command.value, error, now)?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store) + .compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentTransaction::TYPE.into(), + key: rejected.transaction_id.clone(), + current: None, + }], + &[transaction_envelope(&rejected, now)?], + )?, + "bad selector changed before refusal was recorded" + ); + return Ok(true); + } + }; + let busy = snapshot + .transactions + .iter() + .filter(|stored| stored.value.owns_target_authority()) + .map(|stored| stored.value.target.as_str()) + .collect::>(); + if targets.iter().any(|target| busy.contains(target.as_str())) { + return Ok(false); + } + let now = now_millis()?; + let transactions = targets + .into_iter() + .enumerate() + .map(|(ordinal, target)| { + DeploymentTransaction::new( + &command.value, + target.clone(), + u32::try_from(ordinal)?, + snapshot.admitted_for(&target).map(|stored| &stored.value), + now, + ) + }) + .collect::>>()?; + let expected = transactions + .iter() + .map(|transaction| CultCacheExpectedEnvelope { + r#type: DeploymentTransaction::TYPE.into(), + key: transaction.transaction_id.clone(), + current: None, + }) + .collect::>(); + let next = transactions + .iter() + .map(|transaction| transaction_envelope(transaction, now)) + .collect::>>()?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store) + .compare_exchange(&expected, &next)?, + "queued command lost its ordered transaction creation CAS" + ); + Ok(true) + } + + fn supervise_one_admitted_generation(&self) -> Result { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let busy = snapshot + .transactions + .iter() + .filter(|stored| stored.value.owns_target_authority()) + .map(|stored| stored.value.target.as_str()) + .collect::>(); + let mut admitted = snapshot.admitted.iter().collect::>(); + admitted.sort_by_key(|stored| stored.value.target.as_str()); + for current in admitted { + if busy.contains(current.value.target.as_str()) { + continue; + } + match self.refresh_admitted_topology(&snapshot, current) { + Ok(true) => return Ok(true), + Ok(false) => {} + Err(error) => eprintln!( + "Idunn preserved admitted {} after rejecting topology observation: {error:#}", + current.value.target + ), + } + if self + .workload + .observe( + ¤t.value.expected, + ¤t.value.activation, + ¤t.value.workload, + ) + .is_ok() + { + continue; + } + let now = now_millis()?; + let command = DeploymentCommand { + schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), + command_id: format!("continuity-{}", Uuid::new_v4()), + kind: CommandKind::Continuity, + selector: current.value.target.clone(), + requested_by: "idunn-continuity".into(), + requested_at_unix_millis: now, + }; + command.validate()?; + let transaction = + DeploymentTransaction::from_continuity(&command, ¤t.value, now)?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store) + .compare_exchange( + &[ + CultCacheExpectedEnvelope { + r#type: DeploymentCommand::TYPE.into(), + key: command.command_id.clone(), + current: None, + }, + CultCacheExpectedEnvelope { + r#type: DeploymentTransaction::TYPE.into(), + key: transaction.transaction_id.clone(), + current: None, + }, + ], + &[ + command_envelope(&command, now)?, + transaction_envelope(&transaction, now)?, + ], + )?, + "continuity scheduling lost its command/transaction CAS" + ); + return Ok(true); + } + Ok(false) + } + + fn refresh_admitted_topology( + &self, + snapshot: &ControlSnapshot, + current: &Stored, + ) -> Result { + let Some(received) = self.topology().receive(¤t.value.target)? else { + return Ok(false); + }; + ensure!( + received.target == current.value.target, + "topology transport substituted admitted target" + ); + let now = now_millis()?; + let authority = self.runtime_authority_parts( + ¤t.value.plan, + ¤t.value.expected, + ¤t.value.activation, + )?; + let odin_authority = self.current_odin_authority(snapshot)?; + let authenticated = authenticate_odin_runtime_topology_correlation( + &received.canonical_bytes, + &authority, + current.value.leasing.lease_sha256(), + &odin_authority.signer_public_key, + self.trusted_topology_context(now), + )?; + let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; + if !sequence_requires_admission( + Some(¤t.value.latest_odin_observation), + snapshot.max_odin_sequence(&evidence.signer_identity_id), + &evidence, + )? { + return Ok(false); + } + let mut next = current.value.clone(); + next.latest_odin_observation = evidence.clone(); + next.odin_publisher_sequence_cursor = evidence.publisher_sequence; + if is_semantic_ready(&authenticated) { + next.ready = evidence; + } + next.validate()?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: AdmittedGeneration::TYPE.into(), + key: current.value.target.clone(), + current: Some(current.envelope.clone()), + }], + &[admitted_envelope(&next, now)?], + )?, + "admitted generation changed before topology sequence CAS" + ); + Ok(true) + } + + fn advance_transaction(&self, current: &Stored) -> Result<()> { + if current.value.pre_fencing_abort.is_some() && current.value.completion.is_none() { + return self.advance_pre_fencing_abort(current); + } + match current.value.phase { + DeploymentPhase::Sealing => self.advance_sealing(current), + DeploymentPhase::Starting => self.advance_starting(current), + DeploymentPhase::Warming => self.advance_warming(current), + DeploymentPhase::Fencing => self.advance_fencing(current), + DeploymentPhase::Leasing => self.advance_leasing(current), + DeploymentPhase::AwaitingReady => self.advance_awaiting_ready(current), + DeploymentPhase::Routing => self.advance_routing(current), + DeploymentPhase::Committing => self.advance_committing(current), + DeploymentPhase::Complete => self.advance_post_commit_cleanup(current), + } + } + + fn advance_sealing(&self, current: &Stored) -> Result<()> { + if current.value.plan.is_none() { + ensure!( + current.value.command_kind == CommandKind::Deploy, + "continuity transaction lost its admitted plan" + ); + let bindings = load_bindings(&self.options.bindings_dir)?; + let loaded = bindings + .get(¤t.value.target) + .context("operator binding disappeared before sealing")?; + let now = now_millis()?; + let resolved = + self.source + .resolve(&loaded.binding, ¤t.value.transaction_id, now)?; + let providers = self.current_ready_provider_tokens( + &ControlSnapshot::read(&self.options.state_store)?, + now, + )?; + let candidate_port = self.select_candidate_port(&loaded.binding)?; + let plan = compile_deployment_plan( + &resolved.recipe_bytes, + &loaded.bytes, + resolved.facts, + format!("incarnation-{}", current.value.transaction_id), + candidate_port, + now, + &providers, + )?; + return self.persist_same_phase(current, |next| { + next.plan = Some(plan); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + + if current.value.command_kind == CommandKind::Deploy + && current.value.frozen_source.is_none() + { + let now = now_millis()?; + let plan = required(¤t.value.plan, "transaction plan")?; + let receipt = self.source.freeze(¤t.value.transaction_id, plan)?; + return self.persist_same_phase(current, |next| { + next.frozen_source = Some(receipt); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + + if current.value.sealed_release.is_none() + || current.value.installed_release.is_none() + || current.value.expected.is_none() + { + ensure!( + current.value.command_kind == CommandKind::Deploy, + "continuity transaction lost its admitted release evidence" + ); + let now = now_millis()?; + let plan = required(¤t.value.plan, "transaction plan")?; + let frozen_receipt = required(¤t.value.frozen_source, "frozen source")?; + let frozen = self.source.observe_frozen(plan, frozen_receipt)?; + let materialized = + self.runner + .materialize(&frozen, plan, &self.options.staging_root, now)?; + let installed = self.workload.install(plan, &materialized)?; + let expected = materialized.release.expected_projection(plan)?; + return self.persist_same_phase(current, |next| { + next.sealed_release = Some(materialized.release); + next.installed_release = Some(installed); + next.expected = Some(expected); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + + if current.value.expected_publication_sha256.is_none() { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + let digest = self.topology().publish_expected(expected)?; + return self.persist_same_phase(current, |next| { + next.expected_publication_sha256 = Some(digest); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + + let now = now_millis()?; + let mut next = current.value.clone(); + match current.value.command_kind { + CommandKind::Deploy => { + let Some(authorization) = self.deployment_authorization(current, now)? else { + return self.record_gate_wait( + current, + "deployment brake has not released this exact transaction", + ); + }; + next.deployment_authorization = Some(authorization); + } + CommandKind::Continuity => { + if !self.lifecycle_allows(current, now)? { + return self + .record_gate_wait(current, "lifecycle brake denies continuity restart"); + } + next.lifecycle_authorized_at_unix_millis = Some(now); + } + } + validate_live_providers_for_deploy(current.value.command_kind, || { + self.validate_selected_providers_current( + required(¤t.value.plan, "transaction plan")?, + now, + ) + })?; + next.phase = DeploymentPhase::Starting; + next.updated_at_unix_millis = now; + next.last_error = None; + replace_transaction(&self.options.state_store, current, &next) + } + + fn advance_starting(&self, current: &Stored) -> Result<()> { + let expected = required(¤t.value.expected, "Expected projection")?; + let plan = required(¤t.value.plan, "transaction plan")?; + let release = required(¤t.value.sealed_release, "sealed release")?; + let installed = required(¤t.value.installed_release, "installed release")?; + if current.value.activation.is_none() { + validate_live_providers_for_deploy(current.value.command_kind, || { + self.validate_selected_providers_current(plan, now_millis()?) + })?; + let now = now_millis()?; + let runtime_instance_id = runtime_instance_id(¤t.value.transaction_id)?; + let launch = IdunnRuntimeActivationLaunch::issue( + expected, + runtime_instance_id, + now, + &self.idunn_signer, + )?; + let activation = self.workload.prepare_activation(plan, expected, launch)?; + return self.persist_same_phase(current, |next| { + next.activation = Some(activation); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + if current.value.workload.is_none() { + let now = now_millis()?; + let activation = required(¤t.value.activation, "activation")?; + let observation = self + .workload + .start_prepared(plan, release, installed, expected, activation)?; + return self.persist_same_phase(current, |next| { + next.workload = Some(observation); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + if current.value.activation_publication_sha256.is_none() { + let now = now_millis()?; + let activation = required(¤t.value.activation, "activation")?; + let workload = required(¤t.value.workload, "workload")?; + self.workload.observe(expected, activation, workload)?; + let digest = self + .topology() + .publish_observed_activation(expected, activation, workload)?; + return self.persist_same_phase(current, |next| { + next.activation_publication_sha256 = Some(digest); + next.updated_at_unix_millis = now; + next.last_error = None; + Ok(()) + }); + } + let mut next = current.value.clone(); + next.phase = DeploymentPhase::Warming; + next.updated_at_unix_millis = now_millis()?; + next.last_error = None; + replace_transaction(&self.options.state_store, current, &next) + } + + fn advance_warming(&self, current: &Stored) -> Result<()> { + let expected = required(¤t.value.expected, "Expected projection")?; + let activation = required(¤t.value.activation, "activation")?; + let workload = required(¤t.value.workload, "workload")?; + self.workload.observe(expected, activation, workload)?; + { + let Some((admitted, authenticated)) = self.admit_latest_topology(current, None)? else { + return Ok(()); + }; + if admitted.envelope != current.envelope { + return Ok(()); + } + let semantic_warming = is_semantic_warming(&authenticated); + let latest = required( + &admitted.value.latest_odin_observation, + "sequence-admitted warming evidence", + )?; + if current.value.warming.as_ref() == Some(latest) { + ensure!(semantic_warming, "stored Warming receipt changed meaning"); + } else if semantic_warming { + let evidence = latest.clone(); + let _token = SequenceAdmittedWarming { + transaction_id: admitted.value.transaction_id.clone(), + evidence: evidence.clone(), + authenticated, + }; + return self.persist_same_phase(&admitted, |next| { + next.warming = Some(evidence); + Ok(()) + }); + } else { + return Ok(()); + } + } + + if expected.route.is_some() && current.value.route_preflight.is_none() { + let now = now_millis()?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; + let incumbent_route = + incumbent.and_then(|generation| generation.value.routing.observation().cloned()); + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let route_binding = binding + .route + .context("routed Expected lost route binding")?; + let driver = NginxRouteDriver::new(route_binding); + let receipt = driver.preflight( + expected, + &activation.runtime_instance_id, + incumbent_route.as_ref(), + )?; + return self.persist_same_phase(current, |next| { + next.route_preflight = Some(receipt); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + + if current.value.isolation.is_none() { + let now = now_millis()?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; + let isolation = + prove_isolation(workload, incumbent.map(|value| &value.value.workload))?; + return self.persist_same_phase(current, |next| { + next.isolation = Some(isolation); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + + self.transition(current, DeploymentPhase::Fencing) + } + + fn advance_fencing(&self, current: &Stored) -> Result<()> { + if current.value.fencing.is_none() { + let now = now_millis()?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; + let incumbent_lease = incumbent.and_then(|generation| generation.value.leasing.lease()); + let expected = required(¤t.value.expected, "Expected projection")?; + let evidence = if expected.write_lease_required || incumbent_lease.is_some() { + let incumbent_lease_path = incumbent_lease + .map(|_| -> Result { + Ok(incumbent + .context("incumbent lease has no admitted generation")? + .value + .plan + .parsed_inputs()? + .1 + .process_write_lease + .context("incumbent lease has no admitted binding")? + .record_path) + }) + .transpose()?; + if let (Some(lease), Some(path)) = (incumbent_lease, &incumbent_lease_path) { + let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, path); + driver.revoke_exact(Some(lease))?; + ensure!( + driver.observe_empty()?, + "incumbent write lease remained after exact fencing" + ); + } + let candidate_lease_path = if expected.write_lease_required { + Some( + current + .value + .plan + .as_ref() + .unwrap() + .parsed_inputs()? + .1 + .process_write_lease + .context("stateful candidate has no write-lease binding")? + .record_path, + ) + } else { + None + }; + if let Some(path) = &candidate_lease_path { + if incumbent_lease_path.as_ref() != Some(path) { + let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, path); + driver.revoke_exact(None)?; + ensure!( + driver.observe_empty()?, + "candidate write-lease path was not empty before grant" + ); + } + } + FencingEvidence::Revoked { + incumbent_lease_sha256: incumbent_lease + .map(IdunnProcessWriteLeaseRecord::canonical_sha256) + .transpose()?, + candidate_lease_path_verified_empty: candidate_lease_path.is_some(), + } + } else { + FencingEvidence::SkippedStateless + }; + return self.persist_same_phase(current, |next| { + next.fencing = Some(evidence); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + self.transition(current, DeploymentPhase::Leasing) + } + + fn advance_leasing(&self, current: &Stored) -> Result<()> { + if current.value.leasing.is_none() { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + let evidence = if expected.write_lease_required { + let warming = self.rehydrate_warming_token(¤t.value, now)?; + ensure!( + warming.transaction_id() == current.value.transaction_id + && Some(warming.evidence_sha256()) + == current + .value + .warming + .as_ref() + .map(|value| value.canonical_sha256.as_str()), + "Warming token does not bind this transaction's durable admission" + ); + let activation = required(¤t.value.activation, "activation")?; + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let _lease_path = binding + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let epoch = self + .exact_incumbent(&snapshot, ¤t.value)? + .and_then(|generation| generation.value.leasing.lease()) + .map_or(1, |lease| lease.lease_epoch.saturating_add(1)); + let warming_presence_sha256 = warming + .authenticated() + .record() + .signed_presence_sha256 + .clone() + .context("warming token has no signed presence")?; + let lease = IdunnProcessWriteLeaseRecord { + schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256()?, + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: activation.canonical_sha256()?, + state_schema_generation: expected + .state_schema_generation + .clone() + .context("stateful Expected has no schema generation")?, + state_contract_sha256: expected + .state_contract_sha256 + .clone() + .context("stateful Expected has no state contract")?, + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + warming_presence_sha256, + lease_epoch: epoch, + issued_at_unix_millis: now, + }; + lease.validate()?; + let lease_sha256 = lease.canonical_sha256()?; + LeasingEvidence::Prepared { + lease, + lease_sha256, + } + } else { + LeasingEvidence::SkippedStateless + }; + return self.persist_same_phase(current, |next| { + next.leasing = Some(evidence); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + + if let Some((lease, prepared_sha256)) = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::prepared_lease) + { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + let activation = required(¤t.value.activation, "activation")?; + let warming = self.rehydrate_warming_token(¤t.value, now)?; + ensure!( + warming.transaction_id() == current.value.transaction_id + && Some(warming.evidence_sha256()) + == current + .value + .warming + .as_ref() + .map(|value| value.canonical_sha256.as_str()), + "Warming token does not bind this transaction's durable admission" + ); + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let lease_path = binding + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); + let granted_sha256 = driver.grant(expected, activation, &warming, lease)?; + ensure!( + granted_sha256 == prepared_sha256, + "granted write lease differs from the durable prepared lease" + ); + let lease = lease.clone(); + return self.persist_same_phase(current, |next| { + next.leasing = Some(LeasingEvidence::Granted { + lease, + lease_sha256: granted_sha256, + }); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + + self.transition(current, DeploymentPhase::AwaitingReady) + } + + fn advance_awaiting_ready(&self, current: &Stored) -> Result<()> { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + if expected.write_lease_required { + let activation = required(¤t.value.activation, "activation")?; + let warming = self.rehydrate_warming_token(¤t.value, now)?; + let lease = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease) + .context("stateful transaction has no process write lease")?; + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let path = binding + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + ensure!( + CultCacheWriteLeaseDriver::new(¤t.value.target, path) + .observe(expected, activation, &warming, lease)?, + "candidate process write lease is no longer exact" + ); + } + { + let current_lease = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + let Some((admitted, authenticated)) = + self.admit_latest_topology(current, current_lease)? + else { + return Ok(()); + }; + if admitted.envelope != current.envelope { + return Ok(()); + } + let semantic_ready = is_semantic_ready(&authenticated); + let latest = required( + &admitted.value.latest_odin_observation, + "sequence-admitted Ready evidence", + )?; + if current.value.ready.as_ref() == Some(latest) { + ensure!(semantic_ready, "stored Ready receipt changed meaning"); + } else if semantic_ready { + let evidence = latest.clone(); + let _token = SequenceAdmittedReady { + transaction_id: admitted.value.transaction_id.clone(), + evidence: evidence.clone(), + expected: expected.clone(), + authenticated, + }; + return self.persist_same_phase(&admitted, |next| { + next.ready = Some(evidence); + Ok(()) + }); + } else { + return Ok(()); + } + } + self.transition(current, DeploymentPhase::Routing) + } + + fn advance_routing(&self, current: &Stored) -> Result<()> { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + let activation = required(¤t.value.activation, "activation")?; + if current.value.routing.is_none() { + let current_lease = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + if let Some((admitted, authenticated)) = + self.admit_latest_topology(current, current_lease)? + { + if admitted.envelope != current.envelope { + return Ok(()); + } + let latest = required( + &admitted.value.latest_odin_observation, + "latest sequence-admitted topology", + )?; + if current.value.ready.as_ref() != Some(latest) { + if is_semantic_ready(&authenticated) { + let evidence = latest.clone(); + let _token = SequenceAdmittedReady { + transaction_id: admitted.value.transaction_id.clone(), + evidence: evidence.clone(), + expected: expected.clone(), + authenticated, + }; + return self.persist_same_phase(&admitted, |next| { + next.ready = Some(evidence); + Ok(()) + }); + } + return Ok(()); + } + } + let ready = self.rehydrate_ready_token(¤t.value, now, false)?; + ensure!( + ready.transaction_id() == current.value.transaction_id, + "Ready token belongs to another transaction" + ); + let evidence = if expected.route.is_some() { + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let route_binding = binding.route.context("routed plan has no route binding")?; + let preflight = required(¤t.value.route_preflight, "route preflight")?; + let driver = NginxRouteDriver::new(route_binding); + let exact_candidate = RouteObservation { + route_id: preflight.route_id.clone(), + runtime_instance_id: preflight.candidate_runtime_instance_id.clone(), + membership_sha256: preflight.candidate_membership_sha256.clone(), + }; + ensure!( + exact_candidate.runtime_instance_id == activation.runtime_instance_id, + "route preflight belongs to another runtime instance" + ); + let observation = if driver.observe(expected, &exact_candidate)? { + exact_candidate + } else { + driver.promote(expected, &activation.runtime_instance_id, preflight)? + }; + ensure!( + driver.observe(expected, &observation)?, + "promoted route is not the exact candidate membership" + ); + RoutingEvidence::Promoted { observation } + } else { + RoutingEvidence::SkippedUnrouted + }; + return self.persist_same_phase(current, |next| { + next.routing = Some(evidence); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + self.transition(current, DeploymentPhase::Committing) + } + + fn advance_committing(&self, current: &Stored) -> Result<()> { + let now = now_millis()?; + let expected = required(¤t.value.expected, "Expected projection")?; + let activation = required(¤t.value.activation, "activation")?; + let workload = required(¤t.value.workload, "workload")?; + self.workload.observe(expected, activation, workload)?; + let _ready = self.rehydrate_ready_token(¤t.value, now, false)?; + + if let Some(lease) = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease) + { + let warming = self.rehydrate_warming_token(¤t.value, now)?; + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let path = binding + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + ensure!( + CultCacheWriteLeaseDriver::new(¤t.value.target, path) + .observe(expected, activation, &warming, lease)?, + "write lease changed before admission commit" + ); + } + if let Some(route) = current + .value + .routing + .as_ref() + .and_then(RoutingEvidence::observation) + { + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let driver = + NginxRouteDriver::new(binding.route.context("routed plan has no route binding")?); + ensure!( + driver.observe(expected, route)?, + "route changed before admission commit" + ); + } + + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; + let odin_authority = self.current_odin_authority(&snapshot)?; + let generation = AdmittedGeneration::from_transaction(¤t.value, odin_authority, now)?; + let post_commit_cleanup = PostCommitCleanup { + incumbent: match incumbent { + Some(incumbent) => IncumbentCleanupEvidence::Pending { + generation_id: incumbent.value.generation_id.clone(), + workload: incumbent.value.workload.clone(), + }, + None => IncumbentCleanupEvidence::SkippedNoIncumbent, + }, + source: match current.value.command_kind { + CommandKind::Deploy => SourceCleanupEvidence::Pending, + CommandKind::Continuity => SourceCleanupEvidence::SkippedContinuity, + }, + }; + let mut complete = current.value.clone(); + complete.phase = DeploymentPhase::Complete; + complete.updated_at_unix_millis = now; + complete.last_error = None; + complete.completion = Some(TransactionCompletion::Admitted { + generation_id: generation.generation_id.clone(), + }); + complete.post_commit_cleanup = Some(post_commit_cleanup); + complete.validate()?; + + let admitted_expected = CultCacheExpectedEnvelope { + r#type: AdmittedGeneration::TYPE.into(), + key: generation.target.clone(), + current: incumbent.map(|stored| stored.envelope.clone()), + }; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( + &[ + CultCacheExpectedEnvelope { + r#type: DeploymentTransaction::TYPE.into(), + key: current.value.transaction_id.clone(), + current: Some(current.envelope.clone()), + }, + admitted_expected, + ], + &[ + transaction_envelope(&complete, now)?, + admitted_envelope(&generation, now)?, + ], + )?, + "incumbent or transaction changed before atomic admission commit" + ); + Ok(()) + } + + fn exact_incumbent<'a>( + &self, + snapshot: &'a ControlSnapshot, + transaction: &DeploymentTransaction, + ) -> Result>> { + let current = snapshot.admitted_for(&transaction.target); + match (&transaction.incumbent_generation_id, current) { + (None, None) => Ok(None), + (Some(expected), Some(current)) if current.value.generation_id == *expected => { + Ok(Some(current)) + } + _ => bail!("target incumbent changed after transaction creation"), + } + } + + fn select_candidate_port(&self, binding: &OperatorBinding) -> Result> { + let Some(route) = &binding.route else { + return Ok(None); + }; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let mut used = BTreeSet::new(); + for plan in snapshot + .admitted + .iter() + .map(|stored| &stored.value.plan) + .chain( + snapshot + .transactions + .iter() + .filter(|stored| stored.value.owns_target_authority()) + .filter_map(|stored| stored.value.plan.as_ref()), + ) + { + if let Some(port) = plan.candidate_port { + used.insert(port); + } + } + (route.private_port_start..=route.private_port_end) + .find(|port| !used.contains(port)) + .map(Some) + .context("no private candidate port remains in the operator range") + } + + fn deployment_authorization( + &self, + current: &Stored, + now: u64, + ) -> Result> { + let expected = required(¤t.value.expected, "Expected projection")?; + let plan = required(¤t.value.plan, "transaction plan")?; + let binding = plan.parsed_inputs()?.1; + let Some((record, canonical_bytes)) = + read_deployment_brake(&binding.brakes.deployment_store)? + else { + return Ok(None); + }; + let anchor = read_trust_anchor::( + &self.options.deployment_brake_operator_anchor, + )?; + if evaluate_idunn_deployment_brake( + IdunnDeploymentBrakeObservation::Present(&record), + &anchor, + &expected.runtime_id, + &expected.sealed_release_id, + ¤t.value.transaction_id, + now, + ) + .is_err() + { + return Ok(None); + } + let authorization_id = record + .authorization_id + .clone() + .context("released brake has no authorization id")?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + ensure!( + !snapshot.transactions.iter().any(|stored| { + stored + .value + .deployment_authorization + .as_ref() + .is_some_and(|used| used.authorization_id == authorization_id) + }), + "deployment authorization was already consumed" + ); + Ok(Some(DeploymentAuthorization { + authorization_id, + brake_sha256: sha256_id(&canonical_bytes), + canonical_brake_bytes: canonical_bytes, + authorized_at_unix_millis: now, + })) + } + + fn lifecycle_allows(&self, current: &Stored, now: u64) -> Result { + let plan = required(¤t.value.plan, "continuity plan")?; + let binding = plan.parsed_inputs()?.1; + let expected = required(¤t.value.expected, "continuity Expected")?; + match read_lifecycle_brake(&binding.brakes.lifecycle_store) { + Ok(Some(record)) => Ok(evaluate_idunn_continuity_restart( + IdunnLifecycleBrakeObservation::Present(&record), + &expected.runtime_id, + ¤t.value.target, + now, + ) + .is_ok()), + Ok(None) => Ok(evaluate_idunn_continuity_restart( + IdunnLifecycleBrakeObservation::Missing, + &expected.runtime_id, + ¤t.value.target, + now, + ) + .is_ok()), + Err(_) => Ok(false), + } + } + + fn record_gate_wait( + &self, + current: &Stored, + detail: &str, + ) -> Result<()> { + if current.value.last_error.as_deref() == Some(detail) { + return Ok(()); + } + let mut next = current.value.clone(); + next.last_error = Some(detail.into()); + next.updated_at_unix_millis = now_millis()?; + replace_transaction(&self.options.state_store, current, &next) + } + + fn runtime_authority( + &self, + transaction: &DeploymentTransaction, + ) -> Result { + self.runtime_authority_parts( + required(&transaction.plan, "transaction plan")?, + required(&transaction.expected, "Expected projection")?, + required(&transaction.activation, "activation")?, + ) + } + + fn runtime_authority_parts( + &self, + plan: &CompiledDeploymentPlan, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result { + let binding = plan.parsed_inputs()?.1; + let provider_anchor = read_trust_anchor::( + &binding.runtime_identity.trust_anchor_store, + )?; + verify_runtime_authority( + expected, + activation, + &self.idunn_anchor, + &provider_anchor.public_key, + ) + } + + fn authenticate_topology_bytes( + &self, + snapshot: &ControlSnapshot, + transaction: &DeploymentTransaction, + canonical_bytes: &[u8], + current_write_lease_sha256: Option<&str>, + trusted_received_at: u64, + ) -> Result { + let authority = self.runtime_authority(transaction)?; + let odin_authority = self.current_odin_authority(snapshot)?; + authenticate_odin_runtime_topology_correlation( + canonical_bytes, + &authority, + current_write_lease_sha256, + &odin_authority.signer_public_key, + self.trusted_topology_context(trusted_received_at), + ) + } + + /// Persist every newly authenticated publisher sequence before deciding + /// whether it means Warming, Ready, degraded, or disagreement. + fn admit_latest_topology( + &self, + current: &Stored, + current_write_lease_sha256: Option<&str>, + ) -> Result< + Option<( + Stored, + AuthenticatedOdinRuntimeTopologyCorrelation, + )>, + > { + let Some(received) = self.topology().receive(¤t.value.target)? else { + return Ok(None); + }; + ensure!( + received.target == current.value.target, + "topology transport substituted target" + ); + let now = now_millis()?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let live = snapshot + .transactions + .iter() + .find(|stored| stored.value.transaction_id == current.value.transaction_id) + .context("transaction disappeared before topology admission")?; + ensure!( + live.envelope == current.envelope, + "transaction changed before topology admission" + ); + let authenticated = self.authenticate_topology_bytes( + &snapshot, + &live.value, + &received.canonical_bytes, + current_write_lease_sha256, + now, + )?; + let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; + if !sequence_requires_admission( + live.value.latest_odin_observation.as_ref(), + snapshot.max_odin_sequence(&evidence.signer_identity_id), + &evidence, + )? { + return Ok(Some((live.clone(), authenticated))); + } + let mut next = live.value.clone(); + next.latest_odin_observation = Some(evidence.clone()); + next.odin_publisher_sequence_cursor = evidence.publisher_sequence; + next.updated_at_unix_millis = now; + next.last_error = None; + replace_transaction(&self.options.state_store, live, &next)?; + let admitted = ControlSnapshot::read(&self.options.state_store)? + .transactions + .into_iter() + .find(|stored| stored.value.transaction_id == next.transaction_id) + .context("sequence-admitted transaction disappeared")?; + ensure!( + admitted.value.latest_odin_observation.as_ref() == Some(&evidence) + && admitted.value.odin_publisher_sequence_cursor == evidence.publisher_sequence, + "topology sequence was not durably admitted" + ); + Ok(Some((admitted, authenticated))) + } + + fn rehydrate_warming_token( + &self, + transaction: &DeploymentTransaction, + _now: u64, + ) -> Result { + let evidence = required(&transaction.warming, "durable warming evidence")?.clone(); + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let authenticated = self.authenticate_topology_bytes( + &snapshot, + transaction, + &evidence.canonical_bytes, + None, + evidence.admitted_at_unix_millis, + )?; + validate_authenticated_evidence(&evidence, &authenticated)?; + ensure!( + is_semantic_warming(&authenticated), + "durable warming evidence no longer authenticates as Warming" + ); + Ok(SequenceAdmittedWarming { + transaction_id: transaction.transaction_id.clone(), + evidence, + authenticated, + }) + } + + fn rehydrate_ready_token( + &self, + transaction: &DeploymentTransaction, + now: u64, + require_current: bool, + ) -> Result { + let evidence = required(&transaction.ready, "durable Ready evidence")?.clone(); + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let current_lease = transaction + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + let authenticated = self.authenticate_topology_bytes( + &snapshot, + transaction, + &evidence.canonical_bytes, + current_lease, + if require_current { + now + } else { + evidence.admitted_at_unix_millis + }, + )?; + validate_authenticated_evidence(&evidence, &authenticated)?; + ensure!( + is_semantic_ready(&authenticated), + "durable Ready evidence no longer authenticates as Ready" + ); + Ok(SequenceAdmittedReady { + transaction_id: transaction.transaction_id.clone(), + evidence, + expected: required(&transaction.expected, "Expected projection")?.clone(), + authenticated, + }) + } + + fn current_ready_provider_tokens( + &self, + snapshot: &ControlSnapshot, + now: u64, + ) -> Result> { + let mut providers = Vec::new(); + for stored in &snapshot.admitted { + if stored.value.latest_odin_observation != stored.value.ready { + continue; + } + match self.rehydrate_admitted_ready(snapshot, &stored.value, now) { + Ok(provider) => providers.push(provider), + Err(error) => eprintln!( + "Idunn excluded non-current provider {}: {error:#}", + stored.value.target + ), + } + } + Ok(providers) + } + + fn rehydrate_admitted_ready( + &self, + snapshot: &ControlSnapshot, + generation: &AdmittedGeneration, + now: u64, + ) -> Result { + ensure!( + generation.latest_odin_observation == generation.ready, + "admitted provider latest topology is not Ready" + ); + let authority = self.runtime_authority_parts( + &generation.plan, + &generation.expected, + &generation.activation, + )?; + let odin_authority = self.current_odin_authority(snapshot)?; + let current_lease = generation.leasing.lease_sha256(); + let authenticated = authenticate_odin_runtime_topology_correlation( + &generation.ready.canonical_bytes, + &authority, + current_lease, + &odin_authority.signer_public_key, + self.trusted_topology_context(now), + )?; + validate_authenticated_evidence(&generation.ready, &authenticated)?; + ensure!( + is_semantic_ready(&authenticated), + "admitted provider no longer has current exact Ready evidence" + ); + Ok(SequenceAdmittedReady { + transaction_id: generation.transaction_id.clone(), + evidence: generation.ready.clone(), + expected: generation.expected.clone(), + authenticated, + }) + } + + fn validate_selected_providers_current( + &self, + plan: &CompiledDeploymentPlan, + now: u64, + ) -> Result<()> { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + for selection in &plan.dependencies { + let Some(provider) = &selection.provider else { + ensure!( + selection.requirement.kind == DependencyKind::Optional, + "required dependency has no provider" + ); + continue; + }; + let DependencyProviderAuthority::ManagedReady { + target, + incarnation_id, + plan_id, + sealed_release_id, + expected_projection_sha256, + odin_topology_correlation_sha256, + odin_topology_publisher_sequence, + } = &provider.authority + else { + continue; + }; + let admitted = snapshot + .admitted_for(target) + .context("selected managed dependency is no longer admitted")?; + let token = self.rehydrate_admitted_ready(&snapshot, &admitted.value, now)?; + ensure!( + admitted.value.expected.incarnation_id == *incarnation_id + && admitted.value.plan.plan_id == *plan_id + && admitted.value.sealed_release.sealed_release_id == *sealed_release_id + && admitted.value.expected.canonical_sha256()? == *expected_projection_sha256 + && token.evidence_sha256() == odin_topology_correlation_sha256 + && token.publisher_sequence() == *odin_topology_publisher_sequence, + "selected dependency authority changed before actuation" + ); + if selection.requirement.startup == StartupOrder::BeforeStart { + ensure!( + token.authenticated().record().ready, + "before-start dependency is not Ready" + ); + } + } + Ok(()) + } + + fn persist_same_phase( + &self, + current: &Stored, + mutation: F, + ) -> Result<()> + where + F: FnOnce(&mut DeploymentTransaction) -> Result<()>, + { + let mut next = current.value.clone(); + mutation(&mut next)?; + ensure!( + next.phase == current.value.phase, + "same-phase evidence update changed phase" + ); + next.updated_at_unix_millis = now_millis()?; + next.last_error = None; + replace_transaction(&self.options.state_store, current, &next) + } + + fn transition( + &self, + current: &Stored, + next_phase: DeploymentPhase, + ) -> Result<()> { + ensure!( + next_phase as u8 == current.value.phase as u8 + 1, + "deployment phase transition is not adjacent" + ); + let mut next = current.value.clone(); + next.phase = next_phase; + next.updated_at_unix_millis = now_millis()?; + next.last_error = None; + replace_transaction(&self.options.state_store, current, &next) + } + + fn record_resumable_error( + &self, + current: &Stored, + error: &anyhow::Error, + ) -> Result<()> { + let detail = truncate(&format!("{error:#}"), 2048); + if current.value.last_error.as_deref() == Some(detail.as_str()) { + return Ok(()); + } + let mut next = current.value.clone(); + next.last_error = Some(detail); + next.updated_at_unix_millis = now_millis()?; + replace_transaction(&self.options.state_store, current, &next) + } + + fn begin_pre_fencing_abort( + &self, + current: &Stored, + error: anyhow::Error, + ) -> Result<()> { + ensure!( + current.value.phase < DeploymentPhase::Fencing, + "post-fence transaction cannot terminal-fail" + ); + ensure!( + current.value.pre_fencing_abort.is_none(), + "pre-fencing abort intent is already durable" + ); + let abort = PreFencingAbort { + error: truncate(&format!("{error:#}"), 2048), + candidate_stop: if current.value.workload.is_some() { + CleanupEvidence::Pending + } else { + CleanupEvidence::Skipped + }, + topology_reconciliation: if current.value.command_kind == CommandKind::Deploy + && current.value.expected_publication_sha256.is_some() + { + CleanupEvidence::Pending + } else { + CleanupEvidence::Skipped + }, + source_cleanup: if current.value.command_kind == CommandKind::Deploy { + CleanupEvidence::Pending + } else { + CleanupEvidence::Skipped + }, + }; + self.persist_same_phase(current, |next| { + next.pre_fencing_abort = Some(abort); + Ok(()) + }) + } + + fn advance_pre_fencing_abort(&self, current: &Stored) -> Result<()> { + ensure!( + current.value.phase < DeploymentPhase::Fencing, + "pre-fencing abort crossed the fencing boundary" + ); + let abort = required(¤t.value.pre_fencing_abort, "pre-fencing abort intent")?; + if abort.candidate_stop == CleanupEvidence::Pending { + self.workload + .stop(required( + ¤t.value.workload, + "pre-fencing candidate observation", + )?) + .context("stopping exact pre-fence candidate")?; + return self.persist_same_phase(current, |next| { + next.pre_fencing_abort.as_mut().unwrap().candidate_stop = CleanupEvidence::Complete; + Ok(()) + }); + } + if abort.topology_reconciliation == CleanupEvidence::Pending { + let expected = required(¤t.value.expected, "failed Expected projection")?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + if let Some(incumbent) = self.exact_incumbent(&snapshot, ¤t.value)? { + let topology = self.topology(); + let expected_sha256 = topology.publish_expected(&incumbent.value.expected)?; + ensure!( + expected_sha256 == incumbent.value.expected.canonical_sha256()?, + "restored incumbent Expected receipt differs" + ); + if let Ok(observation) = self.workload.observe( + &incumbent.value.expected, + &incumbent.value.activation, + &incumbent.value.workload, + ) { + let activation_sha256 = topology.publish_observed_activation( + &incumbent.value.expected, + &incumbent.value.activation, + &observation, + )?; + ensure!( + activation_sha256 == incumbent.value.activation.canonical_sha256()?, + "restored incumbent activation receipt differs" + ); + } + } else { + self.topology() + .withdraw_expected(expected) + .context("withdrawing exact failed Expected projection")?; + } + return self.persist_same_phase(current, |next| { + next.pre_fencing_abort + .as_mut() + .unwrap() + .topology_reconciliation = CleanupEvidence::Complete; + Ok(()) + }); + } + if abort.source_cleanup == CleanupEvidence::Pending { + self.source + .cleanup( + ¤t.value.transaction_id, + current.value.frozen_source.as_ref(), + ) + .context("cleaning failed pre-fence source")?; + return self.persist_same_phase(current, |next| { + next.pre_fencing_abort.as_mut().unwrap().source_cleanup = CleanupEvidence::Complete; + Ok(()) + }); + } + ensure!( + abort.is_complete(), + "pre-fencing abort cleanup is incomplete" + ); + let mut next = current.value.clone(); + next.phase = DeploymentPhase::Complete; + next.updated_at_unix_millis = now_millis()?; + next.last_error = Some(abort.error.clone()); + next.completion = Some(TransactionCompletion::FailedBeforeFencing { + error: abort.error.clone(), + }); + replace_transaction(&self.options.state_store, current, &next) + } + + fn advance_post_commit_cleanup(&self, current: &Stored) -> Result<()> { + let cleanup = required( + ¤t.value.post_commit_cleanup, + "post-commit cleanup evidence", + )?; + if let IncumbentCleanupEvidence::Pending { + generation_id, + workload, + } = &cleanup.incumbent + { + self.workload + .stop(workload) + .with_context(|| format!("retiring admitted incumbent {generation_id}"))?; + let generation_id = generation_id.clone(); + return self.persist_same_phase(current, |next| { + next.post_commit_cleanup.as_mut().unwrap().incumbent = + IncumbentCleanupEvidence::Complete { generation_id }; + Ok(()) + }); + } + if cleanup.source == SourceCleanupEvidence::Pending { + self.source + .cleanup( + ¤t.value.transaction_id, + current.value.frozen_source.as_ref(), + ) + .context("cleaning committed deployment source")?; + return self.persist_same_phase(current, |next| { + next.post_commit_cleanup.as_mut().unwrap().source = SourceCleanupEvidence::Complete; + Ok(()) + }); + } + ensure!(cleanup.is_complete(), "post-commit cleanup is incomplete"); + Ok(()) + } +} + +fn prove_isolation( + candidate: &WorkloadObservation, + incumbent: Option<&WorkloadObservation>, +) -> Result { ensure!( - store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentCommandRecord::TYPE.into(), - key: terminal.command_id.clone(), - current: Some(current.0), - }], - &[typed_envelope(&terminal, now)?], - )?, - "deployment command changed before its terminal receipt" + candidate.dynamic_user + && candidate.private_pids + && candidate.private_mounts + && candidate.process_uids[0] > 0 + && candidate.pid_namespace_id > 0 + && candidate.mount_namespace_id > 0, + "candidate lacks dynamic identity or private namespaces" + ); + let evidence = if let Some(incumbent) = incumbent { + ensure!( + incumbent.dynamic_user + && incumbent.private_pids + && incumbent.private_mounts + && incumbent.process_uids[0] > 0 + && incumbent.pid_namespace_id > 0 + && incumbent.mount_namespace_id > 0, + "incumbent lacks admitted dynamic identity or private namespaces" + ); + ensure!( + candidate.process_uids[0] != incumbent.process_uids[0] + && candidate.pid_namespace_id != incumbent.pid_namespace_id + && candidate.mount_namespace_id != incumbent.mount_namespace_id, + "candidate and incumbent are not distinct by UID, PID namespace, and mount namespace" + ); + IsolationEvidence { + candidate_uid: candidate.process_uids[0], + candidate_pid_namespace_id: candidate.pid_namespace_id, + candidate_mount_namespace_id: candidate.mount_namespace_id, + incumbent_uid: Some(incumbent.process_uids[0]), + incumbent_pid_namespace_id: Some(incumbent.pid_namespace_id), + incumbent_mount_namespace_id: Some(incumbent.mount_namespace_id), + } + } else { + IsolationEvidence { + candidate_uid: candidate.process_uids[0], + candidate_pid_namespace_id: candidate.pid_namespace_id, + candidate_mount_namespace_id: candidate.mount_namespace_id, + incumbent_uid: None, + incumbent_pid_namespace_id: None, + incumbent_mount_namespace_id: None, + } + }; + Ok(evidence) +} + +fn sequence_requires_admission( + latest: Option<&TopologyEvidence>, + maximum_admitted_sequence: u64, + candidate: &TopologyEvidence, +) -> Result { + candidate.validate_shape()?; + if latest.is_some_and(|existing| { + existing.canonical_bytes == candidate.canonical_bytes + && existing.canonical_sha256 == candidate.canonical_sha256 + && existing.signer_identity_id == candidate.signer_identity_id + && existing.publisher_sequence == candidate.publisher_sequence + }) { + return Ok(false); + } + ensure!( + candidate.publisher_sequence > maximum_admitted_sequence, + "Odin topology publisher sequence was replayed or reordered" + ); + Ok(true) +} + +fn validate_live_providers_for_deploy(command_kind: CommandKind, validation: F) -> Result<()> +where + F: FnOnce() -> Result<()>, +{ + match command_kind { + CommandKind::Deploy => validation(), + CommandKind::Continuity => Ok(()), + } +} + +fn validate_authenticated_evidence( + evidence: &TopologyEvidence, + authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation, +) -> Result<()> { + let record = authenticated.record(); + ensure!( + authenticated.canonical_bytes() == evidence.canonical_bytes.as_slice() + && sha256_id(authenticated.canonical_bytes()) == evidence.canonical_sha256 + && record.signer_identity_id == evidence.signer_identity_id + && record.publisher_sequence == evidence.publisher_sequence, + "durable topology evidence differs from its authenticated receipt" ); Ok(()) } -fn read_commands(path: &Path) -> Result> { - Ok(read_command_envelopes(path)? - .into_iter() - .map(|(_, record)| record) - .collect()) +fn is_semantic_warming(authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation) -> bool { + let record = authenticated.record(); + record.present + && !record.ready + && record.observed_presence_state.as_deref() == Some("warming") + && record.observed_write_lease_sha256.is_none() + && record.disagreements.is_empty() } -fn read_command(path: &Path, command_id: &str) -> Result> { - Ok(read_commands(path)? - .into_iter() - .find(|command| command.command_id == command_id)) +fn is_semantic_ready(authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation) -> bool { + let record = authenticated.record(); + record.present + && record.ready + && record.observed_presence_state.as_deref() == Some("active") + && record.disagreements.is_empty() } -fn read_command_envelopes( - path: &Path, -) -> Result> { - if !path.exists() { - return Ok(Vec::new()); - } - SingleFileMessagePackBackingStore::new(path) - .pull_all_read_only_snapshot()? - .into_iter() - .map(|envelope| { - ensure!( - envelope.r#type == DeploymentCommandRecord::TYPE - && envelope.schema_id.as_deref() == Some(DEPLOYMENT_COMMAND_SCHEMA), - "Idunn control store contains a foreign document" - ); - let record: DeploymentCommandRecord = rmp_serde::from_slice(&envelope.payload)?; - ensure!( - rmp_serde::to_vec(&record)? == envelope.payload - && envelope.key == record.command_id, - "Idunn control store contains a noncanonical command" - ); - record.validate()?; - Ok((envelope, record)) - }) - .collect() -} - -fn typed_envelope(record: &DeploymentCommandRecord, millis: u64) -> Result { +fn read_deployment_brake(path: &Path) -> Result)>> { + let Some(envelope) = read_single_envelope(path)? else { + return Ok(None); + }; + ensure!( + envelope.r#type == IdunnDeploymentBrakeRecord::TYPE + && envelope.schema_id.as_deref() == Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA), + "deployment brake store contains a foreign record" + ); + let record: IdunnDeploymentBrakeRecord = rmp_serde::from_slice(&envelope.payload)?; record.validate()?; - let stored_at = chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) - .context("deployment command timestamp is out of range")? - .to_rfc3339(); - Ok(CultCacheEnvelope { - key: record.command_id.clone(), - r#type: DeploymentCommandRecord::TYPE.into(), - payload: rmp_serde::to_vec(record)?, - stored_at, - schema_id: Some(DEPLOYMENT_COMMAND_SCHEMA.into()), - }) + ensure!( + rmp_serde::to_vec(&record)? == envelope.payload && envelope.key == record.brake_id, + "deployment brake is noncanonical or keyed by another authority" + ); + Ok(Some((record, envelope.payload))) +} + +fn read_lifecycle_brake(path: &Path) -> Result> { + let Some(envelope) = read_single_envelope(path)? else { + return Ok(None); + }; + ensure!( + envelope.r#type == IdunnLifecycleBrakeRecord::TYPE + && envelope.schema_id.as_deref() == Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA), + "lifecycle brake store contains a foreign record" + ); + IdunnLifecycleBrakeRecord::decode_canonical(&envelope.payload).map(Some) +} + +fn read_single_envelope(path: &Path) -> Result> { + if !path.exists() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + match entries.as_slice() { + [] => Ok(None), + [entry] => Ok(Some(entry.clone())), + _ => bail!("single-record authority store is ambiguous"), + } +} + +fn read_trust_anchor(path: &Path) -> Result { + let envelope = read_single_envelope(path)? + .with_context(|| format!("service identity trust anchor {} is absent", path.display()))?; + ensure!( + envelope.r#type == P::TRUST_ANCHOR_TYPE + && envelope.key == P::TRUST_ANCHOR_KEY + && envelope.schema_id.as_deref() == Some(P::TRUST_ANCHOR_SCHEMA), + "service identity trust anchor belongs to another profile" + ); + let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&envelope.payload)?; + ensure!( + rmp_serde::to_vec(&anchor)? == envelope.payload + && derive_service_identity_id::

(&anchor.public_key)? == anchor.identity_id, + "service identity trust anchor is noncanonical or self-inconsistent" + ); + Ok(anchor) +} + +fn required<'a, T>(value: &'a Option, label: &str) -> Result<&'a T> { + value.as_ref().with_context(|| format!("{label} is absent")) } fn now_millis() -> Result { @@ -559,6 +3831,18 @@ fn now_millis() -> Result { .try_into()?) } +fn rfc3339_millis(millis: u64) -> Result { + chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) + .context("timestamp is out of range") + .map(|value| value.to_rfc3339()) +} + +fn sibling_path(path: &Path, suffix: &str) -> PathBuf { + let mut value = path.as_os_str().to_os_string(); + value.push(suffix); + PathBuf::from(value) +} + fn string_value(args: &mut impl Iterator, name: &str) -> Result { args.next() .ok_or_else(|| anyhow!("{name} requires a value")) @@ -608,25 +3892,157 @@ fn require_value(value: &str, label: &str) -> Result<()> { Ok(()) } +fn require_detail(value: &str, label: &str) -> Result<()> { + ensure!( + !value.is_empty() && value.len() <= 2048 && !value.contains('\0'), + "{label} is invalid" + ); + Ok(()) +} + fn truncate(value: &str, length: usize) -> String { value.chars().take(length).collect() } +fn sha256_id(bytes: &[u8]) -> String { + let digest = Sha256::digest(bytes); + let mut encoded = String::with_capacity(71); + encoded.push_str("sha256-"); + for byte in digest { + use std::fmt::Write as _; + write!(&mut encoded, "{byte:02x}").expect("writing to String cannot fail"); + } + encoded +} + +fn runtime_instance_id(transaction_id: &str) -> Result { + require_id(transaction_id, "runtime transaction id")?; + Ok(sha256_id( + format!("gamecult.idunn.runtime-instance.v1:{transaction_id}").as_bytes(), + )) +} + fn usage() -> &'static str { - "Idunn deployment and continuity control plane\n\n\ + "Idunn deployment, admission, and continuity control plane\n\n\ idunn serve [runtime options]\n\ idunn up [--state-store PATH] [--no-wait]\n\ idunn status [--state-store PATH] [--command ID]\n\n\ - Recipes declare build, package, launch, health, state, and capability semantics.\n\ - Operator bindings select source, runner, workload, route, brakes, and placement." + Recipes describe capability and process requirements. Idunn seals exact\n\ + source and artifacts, admits one incarnation, and delegates execution to\n\ + systemd and routing mechanics to the configured proxy." } #[cfg(test)] mod tests { use super::*; + fn command(kind: CommandKind) -> DeploymentCommand { + DeploymentCommand { + schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), + command_id: match kind { + CommandKind::Deploy => "up-test".into(), + CommandKind::Continuity => "continuity-test".into(), + }, + kind, + selector: "ghostlight".into(), + requested_by: "operator".into(), + requested_at_unix_millis: 100, + } + } + + fn topology(sequence: u64, byte: u8) -> TopologyEvidence { + let canonical_bytes = vec![byte]; + TopologyEvidence { + canonical_sha256: sha256_id(&canonical_bytes), + canonical_bytes, + signer_identity_id: "odin-signer".into(), + publisher_sequence: sequence, + admitted_at_unix_millis: 100, + } + } + + fn workload(uid: u32, pid_namespace_id: u64, mount_namespace_id: u64) -> WorkloadObservation { + WorkloadObservation { + unit: format!("idunn-{uid}.service"), + unit_description: format!("Idunn test {uid}"), + invocation_id: format!("invocation-{uid}"), + exec_main_start_timestamp_monotonic: 1, + service_type: "exec".into(), + restart_policy: "always".into(), + kill_mode: "control-group".into(), + dynamic_user: true, + systemd_user: format!("u{uid}"), + systemd_group: format!("u{uid}"), + supplementary_groups: String::new(), + capability_bounding_set: String::new(), + ambient_capabilities: String::new(), + private_mounts: true, + private_pids: true, + protect_proc: "invisible".into(), + proc_subset: "pid".into(), + no_new_privileges: true, + umask: "0077".into(), + inaccessible_paths: String::new(), + load_credential: String::new(), + main_pid: uid, + process_start_time: 1, + process_uids: [uid; 4], + process_gids: [uid; 4], + process_groups: vec![uid], + process_cap_inheritable: 0, + process_cap_permitted: 0, + process_cap_effective: 0, + process_cap_bounding: 0, + process_cap_ambient: 0, + process_no_new_privileges: true, + process_namespace_pids: vec![1], + mount_namespace_id, + pid_namespace_id, + executable: PathBuf::from("/opt/test/bin/service"), + executable_device: 1, + executable_inode: 1, + executable_sha256: sha256_id(&[1]), + runtime_instance_id: sha256_id(&uid.to_be_bytes()), + working_directory: PathBuf::from("/opt/test"), + runtime_bundle: PathBuf::from("/run/test"), + command_line_sha256: sha256_id(&[2]), + environment_names: Vec::new(), + environment_contract_sha256: sha256_id(&[3]), + control_group: format!("/system.slice/idunn-{uid}.service"), + credentials_directory: PathBuf::from("/run/credentials/test"), + activation_credential_device: 1, + activation_credential_inode: 2, + activation_credential_uid: uid, + activation_credential_gid: uid, + activation_credential_mode: 0o400, + activation_credential_size: 32, + activation_signer_identity_id: "activation".into(), + activation_signer_public_key: vec![1; 32], + service_credentials: Vec::new(), + } + } + + fn write_lease() -> IdunnProcessWriteLeaseRecord { + IdunnProcessWriteLeaseRecord { + schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), + target: "ghostlight".into(), + expected_projection_sha256: sha256_id(&[1]), + plan_id: sha256_id(&[2]), + incarnation_id: "incarnation-test".into(), + sealed_release_id: sha256_id(&[3]), + activation_witness_sha256: sha256_id(&[4]), + state_schema_generation: "ghostlight-state-v1".into(), + state_contract_sha256: sha256_id(&[5]), + runtime_id: "ghostlight".into(), + runtime_instance_id: sha256_id(&[6]), + warming_presence_sha256: sha256_id(&[7]), + lease_epoch: 1, + issued_at_unix_millis: 100, + } + } + #[test] - fn cli_has_no_raw_command_or_compiled_profile_authority() { + fn cli_exposes_only_declarative_commands() { for arguments in [ vec!["up", "ghostlight", "--deploy-command", "sh -c bad"], vec!["serve", "--swarm-profile", "yggdrasil-local"], @@ -652,6 +4068,268 @@ mod tests { } } + #[test] + fn deployment_command_is_immutable_positional_fact() -> Result<()> { + let value = command(CommandKind::Deploy); + value.validate()?; + let encoded = rmp_serde::to_vec(&value)?; + assert_eq!(encoded[0], 0x96); + assert!(!encoded.windows(7).any(|window| window == b"running")); + assert!(!encoded.windows(5).any(|window| window == b"owner")); + Ok(()) + } + + #[test] + fn phases_are_one_exact_forward_chain() { + let phases = [ + DeploymentPhase::Sealing, + DeploymentPhase::Starting, + DeploymentPhase::Warming, + DeploymentPhase::Fencing, + DeploymentPhase::Leasing, + DeploymentPhase::AwaitingReady, + DeploymentPhase::Routing, + DeploymentPhase::Committing, + DeploymentPhase::Complete, + ]; + assert!(phases + .windows(2) + .all(|pair| pair[1] as u8 == pair[0] as u8 + 1)); + } + + #[test] + fn every_new_odin_sequence_must_advance_and_exact_retry_is_idempotent() -> Result<()> { + let first = topology(7, 1); + assert!(sequence_requires_admission(None, 6, &first)?); + assert!(!sequence_requires_admission( + Some(&first), + 7, + &topology(7, 1) + )?); + assert!(sequence_requires_admission( + Some(&first), + 7, + &topology(8, 2) + )?); + assert!(sequence_requires_admission(Some(&first), 7, &topology(7, 2)).is_err()); + assert!(sequence_requires_admission(Some(&first), 7, &topology(6, 3)).is_err()); + Ok(()) + } + + #[test] + fn stale_odin_provider_receipt_cannot_gate_continuity_restart() -> Result<()> { + let touched = std::cell::Cell::new(false); + validate_live_providers_for_deploy(CommandKind::Continuity, || { + touched.set(true); + bail!("Odin provider receipt is stale") + })?; + assert!(!touched.get()); + assert!(validate_live_providers_for_deploy(CommandKind::Deploy, || { + bail!("Odin provider receipt is stale") + }) + .is_err()); + Ok(()) + } + + #[test] + fn prepared_write_lease_is_durable_but_not_admitted_authority() -> Result<()> { + let lease = write_lease(); + lease.validate()?; + let prepared = LeasingEvidence::Prepared { + lease: lease.clone(), + lease_sha256: lease.canonical_sha256()?, + }; + let encoded = rmp_serde::to_vec(&prepared)?; + let decoded: LeasingEvidence = rmp_serde::from_slice(&encoded)?; + assert_eq!(decoded, prepared); + assert!(decoded.lease().is_none()); + assert!(decoded.lease_sha256().is_none()); + assert_eq!(decoded.prepared_lease().unwrap().0, &lease); + Ok(()) + } + + #[test] + fn runtime_instance_identity_is_stable_for_activation_prepare_replay() -> Result<()> { + let first = runtime_instance_id("tx-one")?; + assert_eq!(runtime_instance_id("tx-one")?, first); + assert_ne!(runtime_instance_id("tx-two")?, first); + Ok(()) + } + + #[test] + fn pre_fencing_abort_is_durable_before_cleanup_and_cannot_resume_as_success() -> Result<()> { + let command = command(CommandKind::Deploy); + let mut transaction = + DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; + transaction.pre_fencing_abort = Some(PreFencingAbort { + error: "sealed source was rejected".into(), + candidate_stop: CleanupEvidence::Skipped, + topology_reconciliation: CleanupEvidence::Skipped, + source_cleanup: CleanupEvidence::Pending, + }); + transaction.validate()?; + assert!(!transaction.is_terminal()); + + transaction + .pre_fencing_abort + .as_mut() + .unwrap() + .source_cleanup = CleanupEvidence::Complete; + transaction.phase = DeploymentPhase::Complete; + transaction.last_error = Some("sealed source was rejected".into()); + transaction.completion = Some(TransactionCompletion::FailedBeforeFencing { + error: "sealed source was rejected".into(), + }); + transaction.validate()?; + assert!(transaction.is_terminal()); + Ok(()) + } + + #[test] + fn bad_selector_becomes_one_terminal_refusal_record() -> Result<()> { + let command = command(CommandKind::Deploy); + let transaction = DeploymentTransaction::rejected( + &command, + anyhow!("deployment selector is unknown"), + 100, + )?; + assert!(transaction.is_terminal()); + assert_eq!(transaction.target, command.selector); + assert!(matches!( + transaction.completion, + Some(TransactionCompletion::FailedBeforeFencing { .. }) + )); + Ok(()) + } + + #[test] + fn post_commit_cleanup_keeps_exact_incumbent_work_owned_until_retired() -> Result<()> { + let incumbent = workload(1001, 40, 50); + let cleanup = PostCommitCleanup { + incumbent: IncumbentCleanupEvidence::Pending { + generation_id: "generation-old".into(), + workload: incumbent.clone(), + }, + source: SourceCleanupEvidence::Pending, + }; + let encoded = rmp_serde::to_vec(&cleanup)?; + let mut replayed: PostCommitCleanup = rmp_serde::from_slice(&encoded)?; + assert_eq!(replayed, cleanup); + assert!(!replayed.is_complete()); + replayed.incumbent = IncumbentCleanupEvidence::Complete { + generation_id: "generation-old".into(), + }; + assert!(!replayed.is_complete()); + replayed.source = SourceCleanupEvidence::Complete; + assert!(replayed.is_complete()); + Ok(()) + } + + #[test] + fn candidate_and_incumbent_must_differ_in_all_three_native_boundaries() { + let incumbent = workload(1001, 40, 50); + assert!(prove_isolation(&workload(1002, 41, 51), Some(&incumbent)).is_ok()); + assert!(prove_isolation(&workload(1001, 41, 51), Some(&incumbent)).is_err()); + assert!(prove_isolation(&workload(1002, 40, 51), Some(&incumbent)).is_err()); + assert!(prove_isolation(&workload(1002, 41, 50), Some(&incumbent)).is_err()); + } + + #[test] + fn exactly_one_inflight_transaction_may_own_a_target() -> Result<()> { + let command = command(CommandKind::Deploy); + let one = DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; + let two = DeploymentTransaction::new(&command, "ghostlight".into(), 1, None, 100)?; + let snapshot = ControlSnapshot { + commands: vec![Stored { + envelope: command_envelope(&command, 100)?, + value: command, + }], + transactions: vec![ + Stored { + envelope: transaction_envelope(&one, 100)?, + value: one, + }, + Stored { + envelope: transaction_envelope(&two, 100)?, + value: two, + }, + ], + admitted: Vec::new(), + }; + assert!(snapshot.validate_relations().is_err()); + Ok(()) + } + + #[test] + fn command_ordinals_do_not_turn_one_target_wait_into_a_global_brake() -> Result<()> { + let first_command = command(CommandKind::Deploy); + let first = DeploymentTransaction::new(&first_command, "odin".into(), 0, None, 100)?; + let second = DeploymentTransaction::new(&first_command, "ghostlight".into(), 1, None, 100)?; + let mut unrelated_command = command(CommandKind::Deploy); + unrelated_command.command_id = "up-unrelated".into(); + let unrelated = + DeploymentTransaction::new(&unrelated_command, "huginn".into(), 0, None, 101)?; + let snapshot = ControlSnapshot { + commands: Vec::new(), + transactions: [&first, &second, &unrelated] + .into_iter() + .map(|value| Stored { + envelope: transaction_envelope(value, 100).unwrap(), + value: value.clone(), + }) + .collect(), + admitted: Vec::new(), + }; + assert!(!snapshot.has_earlier_authority_sibling(&first)); + assert!(snapshot.has_earlier_authority_sibling(&second)); + assert!(!snapshot.has_earlier_authority_sibling(&unrelated)); + Ok(()) + } + + #[test] + fn complete_cleanup_remains_retryable_without_owning_the_current_incarnation() -> Result<()> { + let command = command(CommandKind::Deploy); + let mut transaction = + DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; + transaction.phase = DeploymentPhase::Complete; + transaction.completion = Some(TransactionCompletion::Admitted { + generation_id: format!("generation-{}", transaction.transaction_id), + }); + transaction.post_commit_cleanup = Some(PostCommitCleanup { + incumbent: IncumbentCleanupEvidence::SkippedNoIncumbent, + source: SourceCleanupEvidence::Pending, + }); + + assert!(!transaction.is_terminal()); + assert!(!transaction.owns_target_authority()); + Ok(()) + } + + #[test] + fn legacy_mutable_command_schema_is_rejected() -> Result<()> { + let temporary = tempfile::tempdir()?; + let path = temporary.path().join("control.cc"); + let legacy = CultCacheEnvelope { + key: "up-legacy".into(), + r#type: DeploymentCommand::TYPE.into(), + payload: rmp_serde::to_vec(&(DEPLOYMENT_COMMAND_SCHEMA, "up-legacy", "running"))?, + stored_at: rfc3339_millis(100)?, + schema_id: Some("idunn.deployment_command.v1".into()), + }; + assert!( + SingleFileMessagePackBackingStore::new(&path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: DeploymentCommand::TYPE.into(), + key: "up-legacy".into(), + current: None, + }], + &[legacy], + )? + ); + assert!(ControlSnapshot::read(&path).is_err()); + Ok(()) + } + #[test] fn source_identity_is_explicit_and_atomic() { let partial = parse( @@ -678,27 +4356,4 @@ mod tests { }) ); } - - #[test] - fn deployment_command_receipt_is_positional_and_fail_closed() -> Result<()> { - let record = DeploymentCommandRecord { - schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: "up-test".into(), - selector: "ghostlight".into(), - requested_by: "operator".into(), - requested_at_unix_millis: 100, - state: "pending".into(), - owner_instance_id: None, - started_at_unix_millis: None, - completed_at_unix_millis: None, - detail: String::new(), - }; - record.validate()?; - let payload = rmp_serde::to_vec(&record)?; - assert_eq!(payload[0], 0x9a); - let mut invalid = record; - invalid.state = "succeeded".into(); - assert!(invalid.validate().is_err()); - Ok(()) - } } diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index d4fca8a..2dcd5b5 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -1,12 +1,14 @@ use std::collections::{BTreeMap, BTreeSet}; use std::path::{Component, Path, PathBuf}; -use anyhow::{Context, Result, bail, ensure}; +use anyhow::{bail, ensure, Context, Result}; use serde::{Deserialize, Serialize}; pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; -pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v1"; +pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v2"; pub const IDUNN_RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE"; +pub const IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_CANDIDATE_BIND"; +pub const IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -172,6 +174,9 @@ pub struct MigrationDeclaration { #[serde(rename_all = "kebab-case")] pub enum StateKind { CultcacheFile, + /// Process-owned state imposed by an external runtime contract. Idunn + /// preserves and fences the file but never interprets its payload. + ExternalFile, } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] @@ -332,8 +337,8 @@ pub enum RunnerDriver { #[serde(deny_unknown_fields)] pub struct WorkloadBinding { pub driver: WorkloadDriver, - pub user: String, - pub group: String, + #[serde(default)] + pub state_group: Option, pub unit_prefix: String, pub release_root: PathBuf, #[serde(default)] @@ -346,8 +351,6 @@ pub struct WorkloadBinding { #[serde(default)] pub read_write_paths: BTreeSet, #[serde(default)] - pub capabilities: BTreeSet, - #[serde(default)] pub devices: BTreeSet, pub memory_mebibytes: u32, pub cpu_quota_percent: u32, @@ -590,6 +593,20 @@ impl TargetDeclaration { .contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), "service does not require the standard Idunn runtime bundle" ); + ensure!( + self.service + .required_environment + .contains(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT) + == self.service.route_required, + "routed services must require exactly one Idunn-owned candidate bind" + ); + ensure!( + self.service + .required_environment + .contains(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT) + == self.write_lease_required(), + "state-writing services must require exactly one Idunn-owned process write lease" + ); for name in &self.service.optional_environment { require_environment_name(name)?; ensure!( @@ -847,13 +864,19 @@ impl OperatorBinding { "runner {id} secret files and secret-read affordance disagree" ); } - require_identity(&self.workload.user, "workload user")?; - require_identity(&self.workload.group, "workload group")?; + let needs_state_group = + self.workload.state_root.is_some() || !self.workload.read_write_paths.is_empty(); ensure!( - !matches!(self.workload.user.as_str(), "root" | "0") - && !matches!(self.workload.group.as_str(), "root" | "0"), - "ordinary Idunn workloads cannot run as root" + self.workload.state_group.is_some() == needs_state_group, + "a fixed state group must exist exactly when the dynamic workload has writable paths" ); + if let Some(group) = &self.workload.state_group { + require_identity(group, "workload state group")?; + ensure!( + !matches!(group.as_str(), "root" | "0"), + "ordinary Idunn workload state cannot use the root group" + ); + } require_id(&self.workload.unit_prefix, "workload unit prefix")?; require_id(&self.runtime_identity.runtime_id, "runtime id")?; require_id( @@ -952,9 +975,6 @@ impl OperatorBinding { ); } } - for capability in &self.workload.capabilities { - require_value(capability, "workload capability")?; - } for name in self .workload .environment @@ -962,6 +982,11 @@ impl OperatorBinding { .chain(self.workload.secret_files.keys()) { require_environment_name(name)?; + ensure!( + name != IDUNN_RUNTIME_BUNDLE_ENVIRONMENT + && name != IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, + "operator binding attempts to replace Idunn-owned workload environment" + ); } for value in self.workload.environment.values() { require_value(value, "workload environment value")?; @@ -996,11 +1021,11 @@ impl OperatorBinding { ); ensure!( self.placement.desired_replicas == 1, - "operator binding v1 admits exactly one replica" + "operator binding v2 admits exactly one replica" ); ensure!( self.placement.nodes.len() == 1, - "operator binding v1 admits exactly one node" + "operator binding v2 admits exactly one node" ); for node in &self.placement.nodes { require_id(node, "placement node")?; @@ -1154,8 +1179,27 @@ impl OperatorBinding { && !self .workload .secret_files - .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), - "operator binding cannot replace the standard Idunn runtime bundle" + .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) + && !self + .workload + .environment + .contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT) + && !self + .workload + .secret_files + .contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT), + "operator binding cannot replace Idunn-owned launch environment" + ); + ensure!( + !self + .workload + .environment + .contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT) + && !self + .workload + .secret_files + .contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT), + "operator binding cannot replace the Idunn-owned process write lease" ); let mut available_environment: BTreeSet<_> = self .workload @@ -1165,6 +1209,12 @@ impl OperatorBinding { .cloned() .collect(); available_environment.insert(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into()); + if declaration.service.route_required { + available_environment.insert(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into()); + } + if declaration.write_lease_required() { + available_environment.insert(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into()); + } ensure!( declaration .service @@ -1193,6 +1243,22 @@ impl OperatorBinding { .collect(); let bound_arguments: BTreeSet<_> = self.workload.argument_bindings.keys().cloned().collect(); + match ( + self.workload.state_root.as_ref(), + self.workload.argument_bindings.get("state_root"), + ) { + (Some(state_root), Some(argument)) => ensure!( + Path::new(argument) == state_root.as_path(), + "state_root argument binding differs from the admitted workload state root" + ), + (Some(_), None) => bail!("stateful workload has no state_root argument binding"), + (None, Some(_)) => bail!("stateless workload carries a state_root argument binding"), + (None, None) => {} + } + ensure!( + required_arguments.contains("state_root") == self.workload.state_root.is_some(), + "recipe state_root argument must exist exactly when the workload has a state root" + ); ensure!( required_arguments == bound_arguments, "operator argument bindings do not exactly match the launch contract" @@ -1503,7 +1569,7 @@ arguments = [ ] transport = "http" route_required = true -required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "TEST_SERVICE_BIND"] +required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_PROCESS_WRITE_LEASE", "GAMECULT_IDUNN_RUNTIME_BUNDLE"] [service.health] contract = "test-service.cultnet-service-health" @@ -1534,7 +1600,7 @@ startup = "before-promotion" "#; const BINDING: &str = r#" -schema = "gamecult.idunn.operator_binding.v1" +schema = "gamecult.idunn.operator_binding.v2" target = "test-service" profiles = ["aetheria", "full-gamecult"] @@ -1561,8 +1627,7 @@ tmpfs_mebibytes = 1024 [workload] driver = "systemd-transient" -user = "test-service" -group = "test-service" +state_group = "test-service" unit_prefix = "idunn-test-service" release_root = "/srv/test-service/releases" state_root = "/var/lib/gamecult/test-service" @@ -1572,9 +1637,6 @@ hardening = "strict" memory_mebibytes = 2048 cpu_quota_percent = 200 -[workload.environment] -TEST_SERVICE_BIND = "idunn.private_endpoint" - [workload.argument_bindings] state_root = "/var/lib/gamecult/test-service" @@ -1693,13 +1755,33 @@ nodes = ["yggdrasil"] fn extra_operator_environment_is_rejected() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); let input = BINDING.replace( - "TEST_SERVICE_BIND = \"idunn.private_endpoint\"", - "TEST_SERVICE_BIND = \"idunn.private_endpoint\"\nUNDECLARED = \"no\"", + "[workload.argument_bindings]", + "[workload.environment]\nUNDECLARED = \"no\"\n\n[workload.argument_bindings]", ); let binding = OperatorBinding::parse(&input).unwrap(); assert!(binding.admit(&recipe).is_err()); } + #[test] + fn operator_cannot_replace_idunn_owned_launch_inputs() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + for name in [ + IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, + IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, + IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, + ] { + let input = BINDING.replace( + "[workload.argument_bindings]", + &format!( + "[workload.environment]\n{name} = \"forged\"\n\n[workload.argument_bindings]" + ), + ); + let rejected = OperatorBinding::parse(&input) + .and_then(|binding| binding.admit(&recipe).map(|_| ())); + assert!(rejected.is_err()); + } + } + #[test] fn state_slot_path_cannot_escape_the_operator_owned_root() { let input = RECIPE.replace( @@ -1709,6 +1791,28 @@ nodes = ["yggdrasil"] assert!(TargetDeclaration::parse(&input).is_err()); } + #[test] + fn mismatched_state_root_argument_binding_is_rejected() { + let recipe = TargetDeclaration::parse(RECIPE).unwrap(); + let mismatched = BINDING.replace( + "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"", + "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/another-service\"", + ); + let binding = OperatorBinding::parse(&mismatched).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + + #[test] + fn external_file_state_keeps_process_bound_writer_and_lease_semantics() { + let input = RECIPE.replace("kind = \"cultcache-file\"", "kind = \"external-file\""); + let declaration = TargetDeclaration::parse(&input).unwrap(); + let slot = &declaration.state.as_ref().unwrap().slots[0]; + assert_eq!(slot.kind, StateKind::ExternalFile); + assert_eq!(slot.writer, StateWriter::ProcessBoundSingleWriter); + assert_eq!(slot.startup, StateStartup::CreateOrOpenAfterWriteLease); + assert!(declaration.write_lease_required()); + } + #[test] fn state_slots_cannot_claim_overlapping_paths() { let input = RECIPE.replace( @@ -1721,27 +1825,49 @@ nodes = ["yggdrasil"] #[test] fn workload_mount_cannot_bypass_write_lease_controlled_state() { let input = BINDING.replace( - "cpu_quota_percent = 200\n\n[workload.environment]", - "cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.environment]", + "cpu_quota_percent = 200\n\n[workload.argument_bindings]", + "cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.argument_bindings]", ); assert!(OperatorBinding::parse(&input).is_err()); } #[test] - fn v1_binding_is_explicitly_singleton() { - assert!( - OperatorBinding::parse( - &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") - ) - .is_err() + fn v2_binding_is_explicitly_singleton() { + assert!(OperatorBinding::parse( + &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") + ) + .is_err()); + assert!(OperatorBinding::parse(&BINDING.replace( + "nodes = [\"yggdrasil\"]", + "nodes = [\"yggdrasil\", \"raven\"]" + )) + .is_err()); + } + + #[test] + fn legacy_static_workload_identity_has_no_authority_in_v2() { + assert!(OperatorBinding::parse(&BINDING.replace( + "schema = \"gamecult.idunn.operator_binding.v2\"", + "schema = \"gamecult.idunn.operator_binding.v1\"", + ),) + .is_err()); + let with_static_identity = BINDING.replace( + "driver = \"systemd-transient\"", + "driver = \"systemd-transient\"\nuser = \"test-service\"\ngroup = \"test-service\"", ); + assert!(OperatorBinding::parse(&with_static_identity).is_err()); + } + + #[test] + fn writable_state_requires_one_non_root_state_group() { assert!( - OperatorBinding::parse(&BINDING.replace( - "nodes = [\"yggdrasil\"]", - "nodes = [\"yggdrasil\", \"raven\"]" - )) - .is_err() + OperatorBinding::parse(&BINDING.replace("state_group = \"test-service\"\n", ""),) + .is_err() ); + assert!(OperatorBinding::parse( + &BINDING.replace("state_group = \"test-service\"", "state_group = \"root\""), + ) + .is_err()); } #[test] @@ -1773,7 +1899,8 @@ nodes = ["yggdrasil"] .replace( "startup = \"create-or-open-after-write-lease\"", "startup = \"open-at-start\"", - ), + ) + .replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", ""), ) .unwrap(); binding.admit(&read_only_recipe).unwrap(); @@ -1792,6 +1919,7 @@ nodes = ["yggdrasil"] "arguments = [\n { kind = \"literal\", value = \"--state-root\" },\n { kind = \"binding\", name = \"state_root\" },\n]", "arguments = []", ); + recipe_text = recipe_text.replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", ""); let state_start = recipe_text.find("\n[state]\n").unwrap(); let state_end = recipe_text.find("\n[[provides]]\n").unwrap(); recipe_text.replace_range(state_start..state_end, ""); @@ -1799,6 +1927,7 @@ nodes = ["yggdrasil"] assert!(recipe.state.is_none()); let binding_text = BINDING + .replace("state_group = \"test-service\"\n", "") .replace("state_root = \"/var/lib/gamecult/test-service\"\n", "") .replace( "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"\n\n", diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index f3a9571..91096f6 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -1,23 +1,24 @@ use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; -use anyhow::{Context, Result, bail, ensure}; +use anyhow::{bail, ensure, Context, Result}; pub use cultnet_rs::IdunnExpectedIncarnationRecord as ExpectedIncarnation; use cultnet_rs::{ - GameCultRuntimeCapability, IDUNN_EXPECTED_INCARNATION_SCHEMA, IdunnExpectedDependency, - IdunnExpectedRoute, + IdunnExpectedCapability, IdunnExpectedDependency, IdunnExpectedRoute, + IDUNN_EXPECTED_INCARNATION_SCHEMA, }; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; +use crate::control_plane::SequenceAdmittedReady; use crate::deployment::{ - CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, - ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration, - capability_compatible, + capability_compatible, CapabilityDependency, DependencyKind, ExternalCapabilityBinding, + OperatorBinding, ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, + TargetDeclaration, }; pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; -pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v1"; +pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v2"; pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -132,13 +133,15 @@ impl SourceSelectionFacts { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] -pub enum ExpectedProviderAuthority { - ManagedIncarnation { +pub enum DependencyProviderAuthority { + ManagedReady { target: String, incarnation_id: String, plan_id: String, sealed_release_id: String, expected_projection_sha256: String, + odin_topology_correlation_sha256: String, + odin_topology_publisher_sequence: u64, }, ExternalOperatorBinding { binding_target: String, @@ -147,9 +150,9 @@ pub enum ExpectedProviderAuthority { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct ExpectedProviderRef { +pub struct DependencyProviderRef { pub provider_id: String, - pub authority: ExpectedProviderAuthority, + pub authority: DependencyProviderAuthority, pub capability: String, pub schema: String, pub compatibility: String, @@ -157,29 +160,39 @@ pub struct ExpectedProviderRef { pub endpoint: Option, } -impl ExpectedProviderRef { +impl DependencyProviderRef { pub fn validate(&self) -> Result<()> { - require_token(&self.provider_id, "expected provider id")?; + require_token(&self.provider_id, "dependency provider id")?; require_contract(&self.capability, &self.schema, &self.compatibility)?; - ensure!(self.capacity > 0, "expected provider capacity is zero"); + ensure!(self.capacity > 0, "dependency provider capacity is zero"); if let Some(endpoint) = &self.endpoint { - require_value(endpoint, "expected provider endpoint")?; + require_value(endpoint, "dependency provider endpoint")?; } match &self.authority { - ExpectedProviderAuthority::ManagedIncarnation { + DependencyProviderAuthority::ManagedReady { target, incarnation_id, plan_id, sealed_release_id, expected_projection_sha256, + odin_topology_correlation_sha256, + odin_topology_publisher_sequence, } => { require_token(target, "managed provider target")?; require_token(incarnation_id, "managed provider incarnation")?; require_sha256(plan_id, "managed provider plan")?; require_sha256(sealed_release_id, "managed provider sealed release")?; require_sha256(expected_projection_sha256, "managed expected projection")?; + require_sha256( + odin_topology_correlation_sha256, + "managed Odin topology correlation", + )?; + ensure!( + *odin_topology_publisher_sequence > 0, + "managed Odin topology sequence is zero" + ); } - ExpectedProviderAuthority::ExternalOperatorBinding { binding_target } => { + DependencyProviderAuthority::ExternalOperatorBinding { binding_target } => { require_token(binding_target, "external binding target")?; ensure!( self.endpoint.is_some(), @@ -204,14 +217,14 @@ impl ExpectedProviderRef { fn is_external_binding(&self) -> bool { matches!( &self.authority, - ExpectedProviderAuthority::ExternalOperatorBinding { .. } + DependencyProviderAuthority::ExternalOperatorBinding { .. } ) } } pub fn external_binding_provider_refs( binding: &OperatorBinding, -) -> Result> { +) -> Result> { binding.validate()?; let providers = binding .external_capabilities @@ -227,10 +240,10 @@ pub fn external_binding_provider_refs( fn expected_external_provider( binding_target: &str, capability: &ExternalCapabilityBinding, -) -> ExpectedProviderRef { - ExpectedProviderRef { +) -> DependencyProviderRef { + DependencyProviderRef { provider_id: capability.provider_id.clone(), - authority: ExpectedProviderAuthority::ExternalOperatorBinding { + authority: DependencyProviderAuthority::ExternalOperatorBinding { binding_target: binding_target.to_owned(), }, capability: capability.capability.clone(), @@ -241,34 +254,61 @@ fn expected_external_provider( } } -fn managed_expected_provider_refs( - expected_incarnations: &[ExpectedIncarnation], -) -> Result> { +/// Projects one authenticated Ready correlation into dependency-selection +/// inputs. Authentication is not replay admission: the control-plane store +/// must atomically admit `publisher_sequence` before calling this function and +/// must re-check the selected receipt before promotion. +fn managed_ready_provider_refs( + admitted: &SequenceAdmittedReady, +) -> Result> { + let expected = admitted.expected(); + let topology = admitted.authenticated(); + expected.validate()?; + let expected_projection_sha256 = expected.canonical_sha256()?; + let observed = topology.record(); + ensure!( + observed.expected_projection_sha256 == expected_projection_sha256, + "Ready provider topology names a different Expected projection" + ); + ensure!( + observed.target == expected.target && observed.runtime_id == expected.runtime_id, + "Ready provider topology names a different target or runtime" + ); + ensure!( + observed.expected + && observed.present + && observed.ready + && observed.observed_presence_state.as_deref() == Some("active") + && observed.disagreements.is_empty(), + "managed provider topology is not Ready" + ); + let odin_topology_correlation_sha256 = admitted.evidence_sha256().to_owned(); + let endpoint = expected + .route + .as_ref() + .map(|route| route.candidate_endpoint.clone()); let mut providers = Vec::new(); - for expected in expected_incarnations { - expected.validate()?; - let expected_projection_sha256 = expected.canonical_sha256()?; - let endpoint = expected - .route - .as_ref() - .map(|route| route.candidate_endpoint.clone()); - for capability in &expected.capabilities { - providers.push(ExpectedProviderRef { - provider_id: expected.runtime_id.clone(), - authority: ExpectedProviderAuthority::ManagedIncarnation { - target: expected.target.clone(), - incarnation_id: expected.incarnation_id.clone(), - plan_id: expected.plan_id.clone(), - sealed_release_id: expected.sealed_release_id.clone(), - expected_projection_sha256: expected_projection_sha256.clone(), - }, - capability: capability.capability.clone(), - schema: capability.schema.clone(), - compatibility: capability.compatibility.clone(), - capacity: capability.capacity, - endpoint: endpoint.clone(), - }); - } + for capability in &observed.observed_capabilities { + providers.push(DependencyProviderRef { + provider_id: observed.runtime_id.clone(), + authority: DependencyProviderAuthority::ManagedReady { + target: expected.target.clone(), + incarnation_id: expected.incarnation_id.clone(), + plan_id: expected.plan_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + expected_projection_sha256: expected_projection_sha256.clone(), + odin_topology_correlation_sha256: odin_topology_correlation_sha256.clone(), + odin_topology_publisher_sequence: admitted.publisher_sequence(), + }, + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.capacity, + endpoint: endpoint.clone(), + }); + } + for provider in &providers { + provider.validate()?; } Ok(providers) } @@ -277,7 +317,7 @@ fn managed_expected_provider_refs( #[serde(deny_unknown_fields)] pub struct DependencySelection { pub requirement: CapabilityDependency, - pub provider: Option, + pub provider: Option, } impl DependencySelection { @@ -321,7 +361,7 @@ impl DependencySelection { ) = match &self.provider { None => (None, None, None, None), Some(provider) => match &provider.authority { - ExpectedProviderAuthority::ManagedIncarnation { + DependencyProviderAuthority::ManagedReady { expected_projection_sha256, .. } => ( @@ -330,7 +370,7 @@ impl DependencySelection { Some(expected_projection_sha256.clone()), provider.endpoint.clone(), ), - ExpectedProviderAuthority::ExternalOperatorBinding { .. } => ( + DependencyProviderAuthority::ExternalOperatorBinding { .. } => ( Some(provider.provider_id.clone()), Some("external-operator-binding".into()), None, @@ -355,7 +395,7 @@ impl DependencySelection { pub fn select_dependencies( declaration: &TargetDeclaration, - providers: &[ExpectedProviderRef], + providers: &[DependencyProviderRef], ) -> Result> { declaration.validate()?; let mut unique_contracts = BTreeSet::new(); @@ -368,7 +408,7 @@ pub fn select_dependencies( provider.schema.as_str(), provider.compatibility.as_str(), )), - "expected provider contract is duplicated" + "dependency provider contract is duplicated" ); } for conflict in &declaration.conflicts { @@ -508,14 +548,14 @@ impl CompiledDeploymentPlan { } #[allow(clippy::too_many_arguments)] -pub fn compile_deployment_plan( +pub(crate) fn compile_deployment_plan( recipe_bytes: &[u8], binding_bytes: &[u8], source: SourceSelectionFacts, incarnation_id: impl Into, candidate_port: Option, created_at_unix_millis: u64, - managed_expected_incarnations: &[ExpectedIncarnation], + managed_ready_providers: &[SequenceAdmittedReady], ) -> Result { let recipe_text = std::str::from_utf8(recipe_bytes).context("recipe is not UTF-8")?; let binding_text = std::str::from_utf8(binding_bytes).context("binding is not UTF-8")?; @@ -533,7 +573,10 @@ pub fn compile_deployment_plan( created_at_unix_millis >= source.selected_at_unix_millis, "deployment plan predates source selection" ); - let mut providers = managed_expected_provider_refs(managed_expected_incarnations)?; + let mut providers = Vec::new(); + for provider in managed_ready_providers { + providers.extend(managed_ready_provider_refs(provider)?); + } providers.extend(external_binding_provider_refs(&binding)?); let dependencies = select_dependencies(&declaration, &providers)?; let mut plan = CompiledDeploymentPlan { @@ -697,11 +740,11 @@ impl SealedRelease { let mut capabilities = declaration .provides .iter() - .map(|capability| GameCultRuntimeCapability { + .map(|capability| IdunnExpectedCapability { capability: capability.capability.clone(), schema: capability.schema.clone(), compatibility: capability.compatibility.clone(), - capacity: capability.capacity, + minimum_capacity: capability.capacity, }) .collect::>(); capabilities.sort_by(|left, right| { @@ -951,6 +994,13 @@ fn sha256_id(bytes: &[u8]) -> String { #[cfg(test)] mod tests { use super::*; + use cultnet_rs::{ + authenticate_odin_runtime_topology_correlation, enroll_service_identity_at, + verify_runtime_authority, GameCultProviderHealthIdentity, GameCultRuntimeCapability, + IdunnRuntimeActivationLaunch, IdunnServiceIdentity, OdinRuntimeTopologyCorrelationPurpose, + OdinRuntimeTopologyCorrelationRecord, OdinTopologyAuthenticationContext, + OdinTopologyIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, + }; const RECIPE: &str = r#" schema = "gamecult.idunn.target_declaration.v1" @@ -981,9 +1031,12 @@ executable = true [service] executable_artifact = "daemon" +arguments = [ + { kind = "binding", name = "state_root" }, +] transport = "http" route_required = true -required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "SERVICE_BIND"] +required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_RUNTIME_BUNDLE"] [service.health] contract = "service.health" @@ -1013,7 +1066,7 @@ compatibility = "v1" "#; const BINDING: &str = r#" -schema = "gamecult.idunn.operator_binding.v1" +schema = "gamecult.idunn.operator_binding.v2" target = "service" [repository] @@ -1038,8 +1091,7 @@ tmpfs_mebibytes = 512 [workload] driver = "systemd-transient" -user = "service" -group = "service" +state_group = "service" unit_prefix = "idunn-service" release_root = "/srv/service/releases" state_root = "/var/lib/gamecult/service" @@ -1049,8 +1101,8 @@ hardening = "strict" memory_mebibytes = 1024 cpu_quota_percent = 100 -[workload.environment] -SERVICE_BIND = "idunn.private_endpoint" +[workload.argument_bindings] +state_root = "/var/lib/gamecult/service" [runtime_identity] runtime_id = "service-yggdrasil" @@ -1124,17 +1176,94 @@ nodes = ["yggdrasil"] stable_endpoint: "tcp://10.77.0.1:17871".into(), candidate_endpoint: "tcp://127.0.0.1:17871".into(), }), - capabilities: vec![GameCultRuntimeCapability { + capabilities: vec![IdunnExpectedCapability { capability: "odin.verse-rendezvous".into(), schema: "odin.verse-topology.v1".into(), compatibility: "v1".into(), - capacity: 1, + minimum_capacity: 1, }], dependencies: Vec::new(), } } + fn ready_odin_provider(runtime_id: &str, capacity: u32) -> SequenceAdmittedReady { + (|| -> Result { + let root = tempfile::tempdir()?; + let provider = enroll_service_identity_at::( + &root.path().join("provider.cc"), + )?; + let idunn = + enroll_service_identity_at::(&root.path().join("idunn.cc"))?; + let odin_signer = + enroll_service_identity_at::(&root.path().join("odin.cc"))?; + let mut expected = odin(); + expected.runtime_id = runtime_id.into(); + expected.expected_signer_identity_id = provider.entry().identity_id.clone(); + let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest(b'8'), 100, &idunn)?; + let activation = launch.activation().clone(); + launch.write_credential(std::io::sink())?; + let authority = verify_runtime_authority( + &expected, + &activation, + &idunn.trust_anchor()?, + &provider.entry().public_key, + )?; + let mut topology = OdinRuntimeTopologyCorrelationRecord { + schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256()?, + expected: true, + current_activation_sha256: Some(activation.canonical_sha256()?), + signed_presence_sha256: Some(digest(b'9')), + observed_presence_state: Some("active".into()), + observed_presence_publisher_sequence: Some(1), + observed_write_lease_sha256: None, + observed_capabilities: vec![GameCultRuntimeCapability { + capability: "odin.verse-rendezvous".into(), + schema: "odin.verse-topology.v1".into(), + compatibility: "v1".into(), + capacity, + }], + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: Some(activation.runtime_instance_id.clone()), + present: true, + ready: true, + dependencies: Vec::new(), + disagreements: Vec::new(), + signer_identity_id: odin_signer.entry().identity_id.clone(), + publisher_sequence: 7, + observed_at_unix_millis: 110, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + }; + topology.signature = odin_signer + .sign::( + &topology.unsigned_signature_payload()?, + ) + .signature; + let bytes = topology.canonical_bytes()?; + let authenticated = authenticate_odin_runtime_topology_correlation( + &bytes, + &authority, + None, + &odin_signer.entry().public_key, + OdinTopologyAuthenticationContext { + trusted_received_at_unix_millis: 120, + maximum_age_millis: 30, + maximum_future_skew_millis: 5, + }, + )?; + SequenceAdmittedReady::for_test(&expected, authenticated, 120) + })() + .unwrap() + } + fn plan() -> CompiledDeploymentPlan { + let providers = [ready_odin_provider("odin-yggdrasil", 1)]; + plan_with(&providers) + } + + fn plan_with(providers: &[SequenceAdmittedReady]) -> CompiledDeploymentPlan { compile_deployment_plan( RECIPE.as_bytes(), BINDING.as_bytes(), @@ -1142,7 +1271,7 @@ nodes = ["yggdrasil"] "service-incarnation-1", Some(18001), 110, - &[odin()], + providers, ) .unwrap() } @@ -1170,8 +1299,9 @@ nodes = ["yggdrasil"] #[test] fn compiler_parses_the_exact_bytes_it_receipts() { - let first = plan(); - let second = plan(); + let providers = [ready_odin_provider("odin-yggdrasil", 1)]; + let first = plan_with(&providers); + let second = plan_with(&providers); assert_eq!(first, second); assert_eq!(first.recipe_blob.as_slice(), RECIPE.as_bytes()); assert_eq!(first.binding_blob.as_slice(), BINDING.as_bytes()); @@ -1183,18 +1313,17 @@ nodes = ["yggdrasil"] "contract = \"service.health\"", "contract = \"service.health.v2\"", ); - assert!( - compile_deployment_plan( - changed.as_bytes(), - BINDING.as_bytes(), - source(RECIPE), - "service-incarnation-2", - Some(18002), - 111, - &[odin()], - ) - .is_err() - ); + let providers = [ready_odin_provider("odin-yggdrasil", 1)]; + assert!(compile_deployment_plan( + changed.as_bytes(), + BINDING.as_bytes(), + source(RECIPE), + "service-incarnation-2", + Some(18002), + 111, + &providers, + ) + .is_err()); let mut corrupted = first; corrupted .binding_blob @@ -1247,16 +1376,29 @@ nodes = ["yggdrasil"] } #[test] - fn dependency_selection_uses_expected_identity_without_readiness() { + fn dependency_selection_uses_observed_ready_capacity_and_stable_identity_order() { let declaration = TargetDeclaration::parse(RECIPE).unwrap(); - let mut later = odin(); - later.runtime_id = "odin-z".into(); - let providers = managed_expected_provider_refs(&[later, odin()]).unwrap(); + let admitted = [ + ready_odin_provider("odin-z", 4), + ready_odin_provider("odin-yggdrasil", 2), + ]; + let providers = admitted + .iter() + .flat_map(|provider| managed_ready_provider_refs(provider).unwrap()) + .collect::>(); let selected = select_dependencies(&declaration, &providers).unwrap(); assert_eq!( selected[0].provider.as_ref().unwrap().provider_id, "odin-yggdrasil" ); + assert_eq!(selected[0].provider.as_ref().unwrap().capacity, 2); + assert!(matches!( + &selected[0].provider.as_ref().unwrap().authority, + DependencyProviderAuthority::ManagedReady { + odin_topology_publisher_sequence: 7, + .. + } + )); } #[test] @@ -1276,7 +1418,7 @@ nodes = ["yggdrasil"] let selected = select_dependencies(&declaration, &providers).unwrap(); assert!(matches!( &providers[0].authority, - ExpectedProviderAuthority::ExternalOperatorBinding { .. } + DependencyProviderAuthority::ExternalOperatorBinding { .. } )); assert_eq!(providers[0].capacity, 2); assert_eq!( @@ -1349,7 +1491,7 @@ nodes = ["yggdrasil"] "service-incarnation-sorted", Some(18002), 111, - &[odin()], + &[ready_odin_provider("odin-yggdrasil", 1)], ) .unwrap(); let release = SealedRelease::new( diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs index cffc796..a6875ab 100644 --- a/crates/idunn-daemon/src/drivers.rs +++ b/crates/idunn-daemon/src/drivers.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use std::ffi::{OsStr, OsString}; -use std::fs; -use std::io::{ErrorKind, Write}; +use std::fs::{self, OpenOptions}; +use std::io::{ErrorKind, Read, Write}; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; use std::thread; @@ -16,8 +16,10 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use uuid::Uuid; +use crate::control_plane::SequenceAdmittedWarming; use crate::deployment::{ - ArtifactOutput, ArtifactSource, IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, LaunchArgument, + ArtifactOutput, ArtifactSource, IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, + IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, LaunchArgument, OperatorBinding, RouteBinding, RouteDriver, RunnerBinding, SourceSelectionPolicy, TargetDeclaration, WorkloadNetwork, }; @@ -27,11 +29,10 @@ use crate::deployment_plan::{ }; use cultnet_rs::{ IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, - IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, - IdunnRuntimeActivationRecord, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, - OdinRuntimeTopologyCorrelationPurpose, OdinRuntimeTopologyCorrelationRecord, - OdinTopologyIdentity, ServiceIdentityProfile, ServiceIdentitySignature, - ServiceIdentityTrustAnchor, verify_service_identity_signature, + IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, IDUNN_RUNTIME_ACTIVATION_SCHEMA, + IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, + IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, + ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, OdinRuntimeTopologyCorrelationRecord, }; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -141,24 +142,91 @@ pub struct InstalledReleaseObservation { pub root: PathBuf, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ServiceCredentialObservation { + pub environment_name: String, + pub delivered_path: PathBuf, + pub device: u64, + pub inode: u64, + pub uid: u32, + pub gid: u32, + pub mode: u32, + pub size: u64, + pub sha256: String, +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct WorkloadObservation { pub unit: String, pub unit_description: String, pub invocation_id: String, + pub exec_main_start_timestamp_monotonic: u64, + pub service_type: String, + pub restart_policy: String, + pub kill_mode: String, + pub dynamic_user: bool, + pub systemd_user: String, + pub systemd_group: String, + pub supplementary_groups: String, + pub capability_bounding_set: String, + pub ambient_capabilities: String, + pub private_mounts: bool, + pub private_pids: bool, + pub protect_proc: String, + pub proc_subset: String, + pub no_new_privileges: bool, + pub umask: String, + pub inaccessible_paths: String, + pub load_credential: String, pub main_pid: u32, pub process_start_time: u64, + pub process_uids: [u32; 4], + pub process_gids: [u32; 4], + pub process_groups: Vec, + pub process_cap_inheritable: u64, + pub process_cap_permitted: u64, + pub process_cap_effective: u64, + pub process_cap_bounding: u64, + pub process_cap_ambient: u64, + pub process_no_new_privileges: bool, + pub process_namespace_pids: Vec, + pub mount_namespace_id: u64, + pub pid_namespace_id: u64, pub executable: PathBuf, + pub executable_device: u64, + pub executable_inode: u64, pub executable_sha256: String, pub runtime_instance_id: String, - pub user: String, - pub group: String, pub working_directory: PathBuf, pub runtime_bundle: PathBuf, pub command_line_sha256: String, pub environment_names: Vec, pub environment_contract_sha256: String, pub control_group: String, + pub credentials_directory: PathBuf, + pub activation_credential_device: u64, + pub activation_credential_inode: u64, + pub activation_credential_uid: u32, + pub activation_credential_gid: u32, + pub activation_credential_mode: u32, + pub activation_credential_size: u64, + pub activation_signer_identity_id: String, + pub activation_signer_public_key: Vec, + pub service_credentials: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +struct LinuxProcessSecurityObservation { + uids: [u32; 4], + gids: [u32; 4], + groups: Vec, + cap_inheritable: u64, + cap_permitted: u64, + cap_effective: u64, + cap_bounding: u64, + cap_ambient: u64, + no_new_privileges: bool, + namespace_pids: Vec, } pub trait WorkloadPort { @@ -168,7 +236,14 @@ pub trait WorkloadPort { release: &MaterializedRelease, ) -> Result; - fn start( + fn prepare_activation( + &self, + plan: &CompiledDeploymentPlan, + expected: &IdunnExpectedIncarnationRecord, + launch: IdunnRuntimeActivationLaunch, + ) -> Result; + + fn start_prepared( &self, plan: &CompiledDeploymentPlan, release: &SealedRelease, @@ -188,37 +263,21 @@ pub trait WorkloadPort { } pub trait TopologyPort { - type ReadyReceipt: Clone; - fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result; + fn withdraw_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()>; fn publish_observed_activation( &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, observation: &WorkloadObservation, ) -> Result; - fn present( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result>; - fn ready( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - write_lease_sha256: Option<&str>, - ) -> Result>; + fn receive(&self, target: &str) -> Result>; } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct PresenceObservation { - pub correlation_sha256: String, - pub signed_presence_sha256: Option, - pub runtime_instance_id: Option, - pub present: bool, - pub state: Option, - pub write_lease_sha256: Option, - pub disagreements: Vec, +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReceivedOdinTopologyCorrelation { + pub target: String, + pub canonical_bytes: Vec, } pub trait WriteLeasePort { @@ -228,14 +287,14 @@ pub trait WriteLeasePort { &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, lease: &IdunnProcessWriteLeaseRecord, ) -> Result; fn observe( &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, lease: &IdunnProcessWriteLeaseRecord, ) -> Result; } @@ -247,16 +306,27 @@ pub struct RouteObservation { pub membership_sha256: String, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct RoutePreflightReceipt { + pub route_id: String, + pub candidate_runtime_instance_id: String, + pub candidate_membership_sha256: String, + pub incumbent_runtime_instance_id: Option, + pub incumbent_membership_sha256: Option, +} + pub trait RoutePort { fn preflight( &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, - ) -> Result<()>; + incumbent: Option<&RouteObservation>, + ) -> Result; fn promote( &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, + preflight: &RoutePreflightReceipt, ) -> Result; fn observe( &self, @@ -1303,14 +1373,16 @@ pub struct SystemdTransientWorkloadDriver { pub systemd_run_program: PathBuf, pub systemctl_program: PathBuf, pub proc_root: PathBuf, + pub credential_root: PathBuf, } impl Default for SystemdTransientWorkloadDriver { fn default() -> Self { Self { - systemd_run_program: PathBuf::from("systemd-run"), - systemctl_program: PathBuf::from("systemctl"), + systemd_run_program: PathBuf::from("/usr/bin/systemd-run"), + systemctl_program: PathBuf::from("/usr/bin/systemctl"), proc_root: PathBuf::from("/proc"), + credential_root: PathBuf::from("/run/idunn/activation-credentials"), } } } @@ -1321,9 +1393,15 @@ impl SystemdTransientWorkloadDriver { I: IntoIterator, S: AsRef, { + ensure!( + program.is_absolute(), + "workload actuator program is not absolute" + ); let output = Command::new(program) .args(args) .stdin(Stdio::null()) + .env_clear() + .env("LANG", "C.UTF-8") .output() .with_context(|| format!("starting workload actuator {}", program.display()))?; if !output.status.success() { @@ -1348,6 +1426,155 @@ impl SystemdTransientWorkloadDriver { Ok(format!("{prefix}-{suffix}.service")) } + fn activation_credential_source( + &self, + activation: &IdunnRuntimeActivationRecord, + ) -> Result { + activation.validate()?; + ensure!( + self.credential_root.is_absolute() + && !self + .credential_root + .as_os_str() + .to_string_lossy() + .chars() + .any(char::is_whitespace), + "activation credential root must be an absolute path without whitespace" + ); + Ok(self + .credential_root + .join(format!("{}.credential", activation.runtime_instance_id))) + } + + fn write_activation_credential( + &self, + launch: IdunnRuntimeActivationLaunch, + ) -> Result<(IdunnRuntimeActivationRecord, PathBuf)> { + #[cfg(unix)] + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + + ensure_activation_credential_root(&self.credential_root)?; + let expected_activation = launch.activation().clone(); + let source = self.activation_credential_source(&expected_activation)?; + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + options.mode(0o400); + let mut file = options.open(&source).with_context(|| { + format!( + "creating one-shot activation credential {}", + source.display() + ) + })?; + let written = (|| -> Result { + #[cfg(unix)] + file.set_permissions(fs::Permissions::from_mode(0o400))?; + let activation = launch + .write_credential(&mut file) + .context("writing one-shot activation credential")?; + file.flush()?; + file.sync_all()?; + Ok(activation) + })(); + drop(file); + let activation = match written { + Ok(activation) => activation, + Err(error) => { + return match remove_activation_credential_source(&source) { + Ok(()) => Err(error), + Err(cleanup) => Err(error.context(format!( + "deleting incomplete activation credential also failed: {cleanup:#}" + ))), + }; + } + }; + let validation = (|| -> Result<()> { + ensure!( + activation == expected_activation, + "activation launch changed while writing its credential" + ); + validate_activation_credential_source(&source)?; + sync_parent_directory(&source) + })(); + if let Err(error) = validation { + return match remove_activation_credential_source(&source) { + Ok(()) => Err(error), + Err(cleanup) => Err(error.context(format!( + "deleting invalid activation credential also failed: {cleanup:#}" + ))), + }; + } + Ok((activation, source)) + } + + fn validate_prepared_activation_credential( + &self, + activation: &IdunnRuntimeActivationRecord, + source: &Path, + ) -> Result<()> { + validate_activation_credential_source(source)?; + let signer = IdunnRuntimeActivationSigner::from_credential_reader( + open_native_read_only(source).with_context(|| { + format!( + "opening prepared activation credential {}", + source.display() + ) + })?, + )?; + ensure!( + signer.identity_id() == activation.activation_signer_identity_id + && signer.public_key() == activation.activation_signer_public_key, + "prepared activation credential differs from the persisted activation" + ); + Ok(()) + } + + fn stop_submitted_unit(&self, unit: &str, expected_description: &str) -> Result<()> { + let Some(values) = self.show_unit(unit)? else { + return Ok(()); + }; + ensure!( + values.get("Description").map(String::as_str) == Some(expected_description), + "refusing to stop a submitted unit whose description changed" + ); + self.command( + &self.systemctl_program, + [OsString::from("stop"), OsString::from(unit)], + )?; + if let Some(values) = self.show_unit(unit)? { + ensure!( + values + .get("ActiveState") + .is_some_and(|state| { matches!(state.as_str(), "inactive" | "failed") }), + "submitted systemd unit remained active after stop" + ); + } + Ok(()) + } + + fn failed_start_error( + &self, + primary: anyhow::Error, + unit: &str, + expected_description: &str, + credential_source: &Path, + ) -> anyhow::Error { + let stop = self.stop_submitted_unit(unit, expected_description); + let remove = remove_activation_credential_source(credential_source); + match (stop, remove) { + (Ok(()), Ok(())) => primary, + (Err(stop), Ok(())) => { + primary.context(format!("stopping failed candidate also failed: {stop:#}")) + } + (Ok(()), Err(remove)) => primary.context(format!( + "deleting failed candidate activation credential also failed: {remove:#}" + )), + (Err(stop), Err(remove)) => primary.context(format!( + "failed candidate cleanup also failed (stop: {stop:#}; credential delete: {remove:#})" + )), + } + } + fn install_release( &self, plan: &CompiledDeploymentPlan, @@ -1460,6 +1687,10 @@ impl SystemdTransientWorkloadDriver { } fn show_unit(&self, unit: &str) -> Result>> { + ensure!( + self.systemctl_program.is_absolute(), + "systemctl program is not absolute" + ); let output = Command::new(&self.systemctl_program) .args([ OsString::from("show"), @@ -1471,12 +1702,30 @@ impl SystemdTransientWorkloadDriver { OsString::from("--property=Description"), OsString::from("--property=InvocationID"), OsString::from("--property=MainPID"), + OsString::from("--property=ExecMainStartTimestampMonotonic"), + OsString::from("--property=Type"), + OsString::from("--property=Restart"), + OsString::from("--property=KillMode"), + OsString::from("--property=DynamicUser"), OsString::from("--property=User"), OsString::from("--property=Group"), + OsString::from("--property=SupplementaryGroups"), + OsString::from("--property=CapabilityBoundingSet"), + OsString::from("--property=AmbientCapabilities"), + OsString::from("--property=PrivateMounts"), + OsString::from("--property=PrivatePIDs"), + OsString::from("--property=ProtectProc"), + OsString::from("--property=ProcSubset"), + OsString::from("--property=NoNewPrivileges"), + OsString::from("--property=UMask"), + OsString::from("--property=InaccessiblePaths"), + OsString::from("--property=LoadCredential"), OsString::from("--property=WorkingDirectory"), OsString::from("--property=ControlGroup"), ]) .stdin(Stdio::null()) + .env_clear() + .env("LANG", "C.UTF-8") .output() .with_context(|| format!("observing systemd unit {unit}"))?; let text = @@ -1510,8 +1759,17 @@ impl SystemdTransientWorkloadDriver { unit: &str, expected_executable: &Path, runtime_instance_id: &str, + activation_signer_identity_id: &str, + activation_signer_public_key: &[u8], environment_names: &[String], + service_credential_names: &[String], ) -> Result { + ensure!( + service_credential_names + .windows(2) + .all(|pair| pair[0] < pair[1]), + "service credential names are not unique and sorted" + ); let values = self .show_unit(unit)? .with_context(|| format!("systemd unit {unit} is absent"))?; @@ -1529,9 +1787,53 @@ impl SystemdTransientWorkloadDriver { .filter(|value| value.len() == 32 && value.bytes().all(|byte| byte.is_ascii_hexdigit())) .context("systemd unit has no canonical invocation id")? .clone(); + let exec_main_start_timestamp_monotonic = + required_systemd_property(&values, "ExecMainStartTimestampMonotonic")? + .parse::() + .context("systemd unit has no numeric main-process start timestamp")?; + ensure!( + exec_main_start_timestamp_monotonic > 0, + "systemd unit has no main-process start timestamp" + ); let unit_description = required_systemd_property(&values, "Description")?.to_owned(); - let user = required_systemd_property(&values, "User")?.to_owned(); - let group = required_systemd_property(&values, "Group")?.to_owned(); + let service_type = required_systemd_property(&values, "Type")?.to_owned(); + let restart_policy = required_systemd_property(&values, "Restart")?.to_owned(); + let kill_mode = required_systemd_property(&values, "KillMode")?.to_owned(); + let dynamic_user = parse_systemd_boolean(&values, "DynamicUser")?; + let systemd_user = systemd_property(&values, "User")?.to_owned(); + let systemd_group = systemd_property(&values, "Group")?.to_owned(); + let supplementary_groups = systemd_property(&values, "SupplementaryGroups")?.to_owned(); + let capability_bounding_set = + systemd_property(&values, "CapabilityBoundingSet")?.to_owned(); + let ambient_capabilities = systemd_property(&values, "AmbientCapabilities")?.to_owned(); + let private_mounts = parse_systemd_boolean(&values, "PrivateMounts")?; + let private_pids = parse_systemd_boolean(&values, "PrivatePIDs")?; + let protect_proc = required_systemd_property(&values, "ProtectProc")?.to_owned(); + let proc_subset = required_systemd_property(&values, "ProcSubset")?.to_owned(); + let no_new_privileges = parse_systemd_boolean(&values, "NoNewPrivileges")?; + let umask = required_systemd_property(&values, "UMask")?.to_owned(); + let inaccessible_paths = + required_systemd_property(&values, "InaccessiblePaths")?.to_owned(); + let load_credential = required_systemd_property(&values, "LoadCredential")?.to_owned(); + ensure!( + service_type == "exec" + && restart_policy == "no" + && kill_mode == "mixed" + && dynamic_user + && systemd_user.is_empty() + && supplementary_groups.is_empty() + && capability_bounding_set.is_empty() + && ambient_capabilities.is_empty() + && private_mounts + && private_pids + && protect_proc == "invisible" + && proc_subset == "all" + && no_new_privileges + && umask == "0007" + && inaccessible_paths == self.credential_root.display().to_string() + && load_credential == "[unprintable]", + "systemd workload isolation properties differ from the Idunn contract" + ); let working_directory = PathBuf::from(required_systemd_property(&values, "WorkingDirectory")?); ensure!( @@ -1550,20 +1852,91 @@ impl SystemdTransientWorkloadDriver { .context("systemd MainPID is not a u32")?; ensure!(main_pid > 0, "systemd unit has no live main process"); let process_root = self.proc_root.join(main_pid.to_string()); - let executable = fs::read_link(process_root.join("exe")) + let process_executable = process_root.join("exe"); + let executable = fs::read_link(&process_executable) .with_context(|| format!("observing executable for process {main_pid}"))?; + let mut executable_file = open_proc_magic_link(&process_executable)?; + let executable_metadata = executable_file.metadata()?; + let expected_executable_metadata = fs::metadata(expected_executable)?; + #[cfg(unix)] + let (executable_device, executable_inode) = { + use std::os::unix::fs::MetadataExt; + ensure!( + executable_metadata.is_file() + && executable_metadata.dev() == expected_executable_metadata.dev() + && executable_metadata.ino() == expected_executable_metadata.ino(), + "systemd started an executable inode outside the sealed release" + ); + (executable_metadata.dev(), executable_metadata.ino()) + }; + #[cfg(not(unix))] + let (executable_device, executable_inode) = (0, 0); ensure!( fs::canonicalize(&executable)? == fs::canonicalize(expected_executable)?, "systemd started an executable outside the sealed release" ); - let executable_sha256 = sha256_id(&fs::read(&executable)?); + let executable_sha256 = sha256_reader(&mut executable_file)?; let process_start_time = linux_process_start_time(&process_root.join("stat"))?; - let process_control_groups = fs::read_to_string(process_root.join("cgroup"))?; + let process_security = linux_process_security(&process_root.join("status"), main_pid)?; ensure!( - process_control_groups.lines().any(|line| { - line.split_once("::") - .is_some_and(|(_, path)| path == control_group) - }), + process_security + .uids + .iter() + .all(|uid| *uid == process_security.uids[0]) + && (61_184..=65_519).contains(&process_security.uids[0]), + "systemd workload does not have one unprivileged process uid" + ); + ensure!( + process_security + .gids + .iter() + .all(|gid| *gid == process_security.gids[0]) + && process_security.gids[0] > 0 + && process_security + .groups + .iter() + .all(|gid| *gid == process_security.gids[0]), + "systemd workload has foreign supplementary groups" + ); + ensure!( + [ + process_security.cap_inheritable, + process_security.cap_permitted, + process_security.cap_effective, + process_security.cap_bounding, + process_security.cap_ambient, + ] + .iter() + .all(|capabilities| *capabilities == 0), + "systemd workload retained Linux capabilities" + ); + ensure!( + process_security.no_new_privileges, + "systemd workload lacks kernel no-new-privileges enforcement" + ); + ensure!( + process_security.namespace_pids.len() >= 2 + && process_security.namespace_pids[0] == main_pid + && process_security.namespace_pids.last() == Some(&1) + && process_security.namespace_pids.iter().all(|pid| *pid > 0), + "systemd workload is not observed in a private pid namespace" + ); + let mount_namespace_id = linux_namespace_id(&process_root.join("ns/mnt"), "mnt")?; + let pid_namespace_id = linux_namespace_id(&process_root.join("ns/pid"), "pid")?; + let observer_mount_namespace_id = + linux_namespace_id(&self.proc_root.join("self/ns/mnt"), "mnt")?; + let observer_pid_namespace_id = + linux_namespace_id(&self.proc_root.join("self/ns/pid"), "pid")?; + ensure!( + mount_namespace_id != observer_mount_namespace_id + && pid_namespace_id != observer_pid_namespace_id, + "systemd workload did not receive private mount and pid namespaces" + ); + let process_control_groups = fs::read_to_string(process_root.join("cgroup"))?; + let process_control_groups = process_control_groups.lines().collect::>(); + ensure!( + process_control_groups.len() == 1 + && process_control_groups[0] == format!("0::{control_group}"), "systemd MainPID is outside the unit control group" ); let command_line = fs::read(process_root.join("cmdline"))?; @@ -1583,23 +1956,184 @@ impl SystemdTransientWorkloadDriver { runtime_bundle.is_absolute(), "runtime bundle path is not absolute" ); + let credentials_directory = PathBuf::from(required_process_environment_value( + &process_environment, + "CREDENTIALS_DIRECTORY", + )?); + ensure!( + credentials_directory == Path::new("/run/credentials").join(unit), + "systemd MainPID exposed an unexpected credential directory" + ); + let credential_path = path_inside_process_root( + &process_root.join("root"), + &credentials_directory.join(IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME), + )?; + let credential_file = open_native_read_only(&credential_path).with_context(|| { + format!( + "opening delivered activation credential {}", + credential_path.display() + ) + })?; + let credential_metadata = credential_file.metadata()?; + ensure!( + credential_metadata.is_file() && credential_metadata.len() == 32, + "delivered activation credential is not one exact 32-byte native file" + ); + let activation_signer = + IdunnRuntimeActivationSigner::from_credential_reader(credential_file)?; + ensure!( + activation_signer.identity_id() == activation_signer_identity_id + && activation_signer.public_key() == activation_signer_public_key, + "delivered activation credential differs from the Idunn-signed activation" + ); + #[cfg(unix)] + let ( + activation_credential_device, + activation_credential_inode, + activation_credential_uid, + activation_credential_gid, + activation_credential_mode, + ) = { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + ( + credential_metadata.dev(), + credential_metadata.ino(), + credential_metadata.uid(), + credential_metadata.gid(), + credential_metadata.permissions().mode() & 0o777, + ) + }; + #[cfg(not(unix))] + let ( + activation_credential_device, + activation_credential_inode, + activation_credential_uid, + activation_credential_gid, + activation_credential_mode, + ) = (0, 0, 0, 0, 0); + ensure!( + activation_credential_uid == process_security.uids[0] + && activation_credential_gid == process_security.gids[0] + && activation_credential_mode == 0o400, + "delivered activation credential is not readable only by the workload identity" + ); + let mut service_credentials = Vec::new(); + for name in service_credential_names { + let delivered_path = credentials_directory.join(name); + let delivered_value = delivered_path.display().to_string(); + ensure!( + selected_environment.get(name) == Some(&delivered_value), + "workload secret environment does not name its delivered systemd credential" + ); + let observer_path = + path_inside_process_root(&process_root.join("root"), &delivered_path)?; + let mut file = open_native_read_only(&observer_path).with_context(|| { + format!( + "opening delivered service credential {}", + delivered_path.display() + ) + })?; + let metadata = file.metadata()?; + #[cfg(unix)] + let (device, inode, uid, gid, mode, links) = { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + ( + metadata.dev(), + metadata.ino(), + metadata.uid(), + metadata.gid(), + metadata.permissions().mode() & 0o777, + metadata.nlink(), + ) + }; + #[cfg(not(unix))] + let (device, inode, uid, gid, mode, links) = (0, 0, 0, 0, 0, 0); + ensure!( + metadata.is_file() + && metadata.len() > 0 + && links == 1 + && uid == process_security.uids[0] + && gid == process_security.gids[0] + && mode == 0o400, + "delivered service credential is not readable only by the workload identity" + ); + service_credentials.push(ServiceCredentialObservation { + environment_name: name.clone(), + delivered_path, + device, + inode, + uid, + gid, + mode, + size: metadata.len(), + sha256: sha256_reader(&mut file)?, + }); + } + let final_values = self + .show_unit(unit)? + .with_context(|| format!("systemd unit {unit} vanished during observation"))?; + ensure!( + final_values == values + && linux_process_start_time(&process_root.join("stat"))? == process_start_time, + "systemd workload identity changed during native observation" + ); Ok(WorkloadObservation { unit: unit.to_owned(), unit_description, invocation_id, + exec_main_start_timestamp_monotonic, + service_type, + restart_policy, + kill_mode, + dynamic_user, + systemd_user, + systemd_group, + supplementary_groups, + capability_bounding_set, + ambient_capabilities, + private_mounts, + private_pids, + protect_proc, + proc_subset, + no_new_privileges, + umask, + inaccessible_paths, + load_credential, main_pid, process_start_time, + process_uids: process_security.uids, + process_gids: process_security.gids, + process_groups: process_security.groups, + process_cap_inheritable: process_security.cap_inheritable, + process_cap_permitted: process_security.cap_permitted, + process_cap_effective: process_security.cap_effective, + process_cap_bounding: process_security.cap_bounding, + process_cap_ambient: process_security.cap_ambient, + process_no_new_privileges: process_security.no_new_privileges, + process_namespace_pids: process_security.namespace_pids, + mount_namespace_id, + pid_namespace_id, executable, + executable_device, + executable_inode, executable_sha256, runtime_instance_id: runtime_instance_id.to_owned(), - user, - group, working_directory, runtime_bundle, command_line_sha256: sha256_id(&command_line), environment_names: environment_names.to_vec(), environment_contract_sha256: sha256_id(&rmp_serde::to_vec(&selected_environment)?), control_group, + credentials_directory, + activation_credential_device, + activation_credential_inode, + activation_credential_uid, + activation_credential_gid, + activation_credential_mode, + activation_credential_size: credential_metadata.len(), + activation_signer_identity_id: activation_signer.identity_id(), + activation_signer_public_key: activation_signer.public_key(), + service_credentials, }) } @@ -1629,12 +2163,18 @@ impl SystemdTransientWorkloadDriver { &self, binding: &OperatorBinding, bundle: &Path, + expected: &IdunnExpectedIncarnationRecord, + unit: &str, ) -> Result> { let mut environment = binding.workload.environment.clone(); - for (name, path) in &binding.workload.secret_files { + let credentials_directory = Path::new("/run/credentials").join(unit); + for name in binding.workload.secret_files.keys() { ensure!( environment - .insert(name.clone(), path.display().to_string()) + .insert( + name.clone(), + credentials_directory.join(name).display().to_string(), + ) .is_none(), "workload environment and secret bindings collide" ); @@ -1648,6 +2188,42 @@ impl SystemdTransientWorkloadDriver { .is_none(), "operator binding attempts to replace the Idunn runtime bundle" ); + match &expected.route { + Some(route) => { + let (host, port) = endpoint_host_port( + &route.candidate_endpoint, + &format!("{}://", route.transport), + )?; + ensure!( + environment + .insert( + IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into(), + format!("{host}:{port}"), + ) + .is_none(), + "operator binding attempts to replace the Idunn candidate bind" + ); + } + None => ensure!( + !environment.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT), + "unrouted workload carries an Idunn candidate bind" + ), + } + match &binding.process_write_lease { + Some(write_lease) => ensure!( + environment + .insert( + IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into(), + write_lease.record_path.display().to_string(), + ) + .is_none(), + "operator binding attempts to replace the Idunn process write lease" + ), + None => ensure!( + !environment.contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT), + "stateless workload carries an Idunn process write lease" + ), + } Ok(environment) } @@ -1658,9 +2234,11 @@ impl SystemdTransientWorkloadDriver { binding: &OperatorBinding, installed: &Path, bundle: &Path, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, ) -> Result<()> { let command = self.launch_command(declaration, binding, installed)?; - let environment = self.launch_environment(binding, bundle)?; + let environment = self.launch_environment(binding, bundle, expected, &observation.unit)?; let environment_names = environment.keys().cloned().collect::>(); let expected_description = format!( "Idunn {} {}", @@ -1670,16 +2248,51 @@ impl SystemdTransientWorkloadDriver { .context("runtime bundle has no instance id")? .to_string_lossy() ); + ensure!( + observation.unit.ends_with(".service"), + "workload unit has no service suffix" + ); + let expected_group = binding.workload.state_group.as_deref(); + let expected_group_id = expected_group.map(resolve_group_id).transpose()?; + let expected_credentials_directory = Path::new("/run/credentials").join(&observation.unit); ensure!( observation.unit_description == expected_description - && observation.user == binding.workload.user - && observation.group == binding.workload.group + && observation.service_type == "exec" + && observation.restart_policy == "no" + && observation.kill_mode == "mixed" + && observation.dynamic_user + && observation.systemd_user.is_empty() + && match expected_group { + Some(group) => observation.systemd_group == group, + None => observation.systemd_group.is_empty(), + } + && observation.supplementary_groups.is_empty() + && observation.capability_bounding_set.is_empty() + && observation.ambient_capabilities.is_empty() + && observation.private_mounts + && observation.private_pids + && observation.protect_proc == "invisible" + && observation.proc_subset == "all" + && observation.no_new_privileges + && observation.umask == "0007" + && observation.inaccessible_paths == self.credential_root.display().to_string() + && observation.load_credential == "[unprintable]" && observation.working_directory == installed && observation.runtime_bundle == bundle + && observation.credentials_directory == expected_credentials_directory && observation.command_line_sha256 == sha256_id(&proc_command_line(&command)) && observation.environment_names == environment_names && observation.environment_contract_sha256 - == sha256_id(&rmp_serde::to_vec(&environment)?), + == sha256_id(&rmp_serde::to_vec(&environment)?) + && observation.activation_credential_size == 32 + && observation.activation_signer_identity_id + == activation.activation_signer_identity_id + && observation.activation_signer_public_key + == activation.activation_signer_public_key + && match expected_group_id { + Some(group_id) => observation.process_gids[0] == group_id, + None => observation.process_gids[0] == observation.process_uids[0], + }, "running workload launch contract differs from the admitted launch" ); ensure!( @@ -1690,12 +2303,33 @@ impl SystemdTransientWorkloadDriver { Ok(()) } + fn validate_writable_bindings(&self, binding: &OperatorBinding) -> Result<()> { + let Some(state_group) = binding.workload.state_group.as_deref() else { + ensure!( + binding.workload.state_root.is_none() + && binding.workload.read_write_paths.is_empty(), + "dynamic workload writable paths have no fixed state group" + ); + return Ok(()); + }; + let state_group_id = resolve_group_id(state_group)?; + if let Some(state_root) = &binding.workload.state_root { + validate_workload_writable_path(state_root, state_group_id, true)?; + } + for path in &binding.workload.read_write_paths { + validate_workload_writable_path(path, state_group_id, false)?; + } + Ok(()) + } + fn start_transient( &self, declaration: &TargetDeclaration, binding: &OperatorBinding, installed: &Path, bundle: &Path, + expected: &IdunnExpectedIncarnationRecord, + credential_source: &Path, unit: &str, ) -> Result<()> { ensure!( @@ -1716,6 +2350,8 @@ impl SystemdTransientWorkloadDriver { .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), "operator binding attempts to replace the Idunn runtime bundle" ); + validate_service_credential_sources(&binding.workload.secret_files)?; + let environment = self.launch_environment(binding, bundle, expected, unit)?; let executable_artifact = release_artifact(declaration, &declaration.service.executable_artifact)?; let executable = installed.join(&executable_artifact.destination); @@ -1730,11 +2366,21 @@ impl SystemdTransientWorkloadDriver { ); let mut args = vec![ OsString::from("--no-block"), + OsString::from("--no-ask-password"), + OsString::from("--expand-environment=no"), OsString::from(format!("--unit={unit}")), OsString::from("--property=Type=exec"), OsString::from("--property=Restart=no"), OsString::from("--property=KillMode=mixed"), + OsString::from("--property=DynamicUser=yes"), + OsString::from("--property=SupplementaryGroups="), + OsString::from("--property=CapabilityBoundingSet="), + OsString::from("--property=AmbientCapabilities="), OsString::from("--property=NoNewPrivileges=yes"), + OsString::from("--property=PrivateMounts=yes"), + OsString::from("--property=PrivatePIDs=yes"), + OsString::from("--property=ProtectProc=invisible"), + OsString::from("--property=ProcSubset=all"), OsString::from("--property=PrivateTmp=yes"), OsString::from("--property=ProtectSystem=strict"), OsString::from("--property=ProtectHome=yes"), @@ -1743,10 +2389,16 @@ impl SystemdTransientWorkloadDriver { OsString::from("--property=ProtectKernelTunables=yes"), OsString::from("--property=RestrictSUIDSGID=yes"), OsString::from("--property=LockPersonality=yes"), - OsString::from("--property=UMask=0027"), + OsString::from("--property=UMask=0007"), OsString::from(format!("--property=Description={unit_description}")), - OsString::from(format!("--property=User={}", binding.workload.user)), - OsString::from(format!("--property=Group={}", binding.workload.group)), + OsString::from(format!( + "--property=InaccessiblePaths={}", + self.credential_root.display() + )), + OsString::from(format!( + "--property=LoadCredential={IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME}:{}", + credential_source.display() + )), OsString::from(format!( "--property=MemoryMax={}M", binding.workload.memory_mebibytes @@ -1758,11 +2410,10 @@ impl SystemdTransientWorkloadDriver { OsString::from(format!("--working-directory={}", installed.display())), OsString::from(format!("--property=ReadOnlyPaths={}", installed.display())), OsString::from(format!("--property=ReadOnlyPaths={}", bundle.display())), - OsString::from(format!( - "--setenv={IDUNN_RUNTIME_BUNDLE_ENVIRONMENT}={}", - bundle.display() - )), ]; + if let Some(state_group) = &binding.workload.state_group { + args.push(OsString::from(format!("--property=Group={state_group}"))); + } if binding.workload.network == WorkloadNetwork::None { args.push(OsString::from("--property=PrivateNetwork=yes")); } @@ -1775,7 +2426,7 @@ impl SystemdTransientWorkloadDriver { if let Some(write_lease) = &binding.process_write_lease { for path in [write_lease.record_path.clone(), write_lease.lock_path()] { args.push(OsString::from(format!( - "--property=ReadOnlyPaths={}", + "--property=ReadOnlyPaths=-{}", path.display() ))); } @@ -1803,29 +2454,12 @@ impl SystemdTransientWorkloadDriver { ))); } } - let capabilities = binding - .workload - .capabilities - .iter() - .cloned() - .collect::>() - .join(" "); - args.push(OsString::from(format!( - "--property=CapabilityBoundingSet={capabilities}" - ))); - args.push(OsString::from(format!( - "--property=AmbientCapabilities={capabilities}" - ))); - for (name, value) in &binding.workload.environment { + for (name, value) in environment { args.push(OsString::from(format!("--setenv={name}={value}"))); } for (name, path) in &binding.workload.secret_files { args.push(OsString::from(format!( - "--property=ReadOnlyPaths={}", - path.display() - ))); - args.push(OsString::from(format!( - "--setenv={name}={}", + "--property=LoadCredential={name}:{}", path.display() ))); } @@ -1844,7 +2478,64 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { self.install_release(plan, release) } - fn start( + fn prepare_activation( + &self, + plan: &CompiledDeploymentPlan, + expected: &IdunnExpectedIncarnationRecord, + launch: IdunnRuntimeActivationLaunch, + ) -> Result { + plan.validate()?; + expected.validate()?; + let proposed_activation = launch.activation(); + proposed_activation.validate()?; + ensure!( + expected.plan_id == plan.plan_id + && proposed_activation.expected_projection_sha256 == expected.canonical_sha256()?, + "prepared activation does not belong to the deployment plan" + ); + let (_, binding) = plan.parsed_inputs()?; + ensure!( + expected.target == binding.target, + "prepared activation target differs from the operator binding" + ); + let unit = self.unit_name( + &binding.workload.unit_prefix, + &proposed_activation.runtime_instance_id, + )?; + ensure!( + self.show_unit(&unit)?.is_none(), + "refusing to replace prepared activation material while its deterministic unit exists" + ); + ensure_activation_credential_root(&self.credential_root)?; + let source = self.activation_credential_source(proposed_activation)?; + match fs::symlink_metadata(&source) { + Ok(_) => remove_activation_credential_source(&source) + .context("retiring an orphaned pre-persistence activation credential")?, + Err(error) if error.kind() == ErrorKind::NotFound => {} + Err(error) => { + return Err(error).with_context(|| { + format!( + "inspecting pre-persistence activation credential {}", + source.display() + ) + }); + } + } + let (activation, written_source) = self.write_activation_credential(launch)?; + if written_source != source { + let error = + anyhow::anyhow!("prepared activation used a nondeterministic credential path"); + return match remove_activation_credential_source(&written_source) { + Ok(()) => Err(error), + Err(cleanup) => Err(error.context(format!( + "deleting nondeterministic activation credential also failed: {cleanup:#}" + ))), + }; + } + Ok(activation) + } + + fn start_prepared( &self, plan: &CompiledDeploymentPlan, release: &SealedRelease, @@ -1864,6 +2555,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { "workload inputs do not describe one sealed incarnation" ); let (declaration, binding) = plan.parsed_inputs()?; + self.validate_writable_bindings(&binding)?; let executable_artifact = release_artifact(&declaration, &declaration.service.executable_artifact)?; let executable = installed.root.join(&executable_artifact.destination); @@ -1872,58 +2564,160 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { &activation.runtime_instance_id, )?; let bundle = self.prepare_runtime_bundle(&binding, expected, activation)?; + let service_credential_names = binding + .workload + .secret_files + .keys() + .cloned() + .collect::>(); let environment_names = self - .launch_environment(&binding, &bundle)? + .launch_environment(&binding, &bundle, expected, &unit)? .into_keys() .collect::>(); - if self.show_unit(&unit)?.is_some() { - let observation = self.observe_unit( - &unit, - &executable, - &activation.runtime_instance_id, - &environment_names, - )?; - self.validate_launch_observation( - &observation, + let unit_description = format!( + "Idunn {} {}", + binding.target, activation.runtime_instance_id + ); + let credential_source = self.activation_credential_source(activation)?; + let unit_exists = self.show_unit(&unit)?.is_some(); + match fs::symlink_metadata(&credential_source) { + Ok(_) => { + if let Err(error) = + self.validate_prepared_activation_credential(activation, &credential_source) + { + return if unit_exists { + Err(self.failed_start_error( + error, + &unit, + &unit_description, + &credential_source, + )) + } else { + match remove_activation_credential_source(&credential_source) { + Ok(()) => Err(error), + Err(cleanup) => Err(error.context(format!( + "deleting mismatched prepared credential also failed: {cleanup:#}" + ))), + } + }; + } + } + Err(error) if error.kind() == ErrorKind::NotFound && unit_exists => {} + Err(error) if error.kind() == ErrorKind::NotFound => { + bail!( + "prepared activation has neither a credential source nor an existing exact unit" + ) + } + Err(error) => { + return Err(error).with_context(|| { + format!( + "inspecting prepared activation credential {}", + credential_source.display() + ) + }); + } + } + if !unit_exists { + if let Err(error) = self.start_transient( &declaration, &binding, &installed.root, &bundle, - )?; - ensure!( - observation.executable_sha256 == expected.artifact_sha256, - "running workload executable differs from Expected" - ); - return Ok(observation); + expected, + &credential_source, + &unit, + ) { + return Err(self.failed_start_error( + error, + &unit, + &unit_description, + &credential_source, + )); + } } - self.start_transient(&declaration, &binding, &installed.root, &bundle, &unit)?; let mut last_error = None; for _ in 0..100 { match self.observe_unit( &unit, &executable, &activation.runtime_instance_id, + &activation.activation_signer_identity_id, + &activation.activation_signer_public_key, &environment_names, + &service_credential_names, ) { Ok(observation) => { - self.validate_launch_observation( + let validation = self.validate_launch_observation( &observation, &declaration, &binding, &installed.root, &bundle, - )?; - ensure!( - observation.executable_sha256 == expected.artifact_sha256, - "started workload executable differs from Expected" + expected, + &activation, ); - return Ok(observation); + match validation.and_then(|()| { + ensure!( + observation.executable_sha256 == expected.artifact_sha256, + "started workload executable differs from Expected" + ); + Ok(()) + }) { + Ok(()) => { + if let Err(error) = + remove_activation_credential_source(&credential_source) + { + return Err(self.failed_start_error( + error.context( + "deleting observed one-shot activation credential source", + ), + &unit, + &unit_description, + &credential_source, + )); + } + let after_unlink = self.observe_unit( + &unit, + &executable, + &activation.runtime_instance_id, + &activation.activation_signer_identity_id, + &activation.activation_signer_public_key, + &environment_names, + &service_credential_names, + ); + return match after_unlink { + Ok(after_unlink) if after_unlink == observation => Ok(after_unlink), + Ok(_) => Err(self.failed_start_error( + anyhow::anyhow!( + "workload identity changed after activation source deletion" + ), + &unit, + &unit_description, + &credential_source, + )), + Err(error) => Err(self.failed_start_error( + error.context( + "reobserving copied activation credential after source deletion", + ), + &unit, + &unit_description, + &credential_source, + )), + }; + } + Err(error) => last_error = Some(error), + } } Err(error) => last_error = Some(error), } thread::sleep(Duration::from_millis(50)); } - Err(last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate"))) + Err(self.failed_start_error( + last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate")), + &unit, + &unit_description, + &credential_source, + )) } fn observe( @@ -1942,14 +2736,41 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { let observed = self.observe_unit( &prior.unit, &prior.executable, - &prior.runtime_instance_id, + &activation.runtime_instance_id, + &activation.activation_signer_identity_id, + &activation.activation_signer_public_key, &prior.environment_names, + &prior + .service_credentials + .iter() + .map(|credential| credential.environment_name.clone()) + .collect::>(), )?; ensure!( observed == *prior && observed.executable_sha256 == expected.artifact_sha256, "native workload identity changed after observation" ); - Ok(observed) + let credential_source = self.activation_credential_source(activation)?; + remove_activation_credential_source(&credential_source) + .context("retiring a recovered activation credential source")?; + let after_unlink = self.observe_unit( + &prior.unit, + &prior.executable, + &activation.runtime_instance_id, + &activation.activation_signer_identity_id, + &activation.activation_signer_public_key, + &prior.environment_names, + &prior + .service_credentials + .iter() + .map(|credential| credential.environment_name.clone()) + .collect::>(), + )?; + ensure!( + after_unlink == observed, + "native workload identity changed after recovered credential cleanup" + ); + Ok(after_unlink) } fn stop(&self, observation: &WorkloadObservation) -> Result<()> { @@ -1963,7 +2784,14 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { &observation.unit, &observation.executable, &observation.runtime_instance_id, + &observation.activation_signer_identity_id, + &observation.activation_signer_public_key, &observation.environment_names, + &observation + .service_credentials + .iter() + .map(|credential| credential.environment_name.clone()) + .collect::>(), )?; ensure!( current == *observation, @@ -1972,6 +2800,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { } else { ensure!( values.get("InvocationID") == Some(&observation.invocation_id) + && values.get("Description") == Some(&observation.unit_description) && matches!(active, "inactive" | "failed"), "refusing to stop a foreign or transitional systemd unit" ); @@ -2051,7 +2880,7 @@ impl CultCacheWriteLeaseDriver { &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, lease: &IdunnProcessWriteLeaseRecord, ) -> Result<()> { expected.validate()?; @@ -2059,6 +2888,7 @@ impl CultCacheWriteLeaseDriver { lease.validate()?; let expected_sha256 = expected.canonical_sha256()?; let activation_sha256 = activation.canonical_sha256()?; + let warming = warming.authenticated().record(); let warming_presence_sha256 = warming .signed_presence_sha256 .as_deref() @@ -2085,10 +2915,11 @@ impl CultCacheWriteLeaseDriver { .context("write-lease Expected has no state contract")? && lease.warming_presence_sha256 == warming_presence_sha256 && warming.present - && warming.state.as_deref() == Some("warming") + && !warming.ready + && warming.observed_presence_state.as_deref() == Some("warming") && warming.runtime_instance_id.as_deref() == Some(activation.runtime_instance_id.as_str()) - && warming.write_lease_sha256.is_none() + && warming.observed_write_lease_sha256.is_none() && warming.disagreements.is_empty(), "process write lease does not bind the exact warming candidate" ); @@ -2129,7 +2960,7 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, lease: &IdunnProcessWriteLeaseRecord, ) -> Result { self.validate_grant(expected, activation, warming, lease)?; @@ -2161,7 +2992,7 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, lease: &IdunnProcessWriteLeaseRecord, ) -> Result { self.validate_grant(expected, activation, warming, lease)?; @@ -2177,82 +3008,9 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { pub struct CultCacheTopologyDriver { pub projection_store: PathBuf, pub correlation_store: PathBuf, - pub odin_trust_anchor_store: PathBuf, -} - -impl CultCacheTopologyDriver { - fn anchor(&self) -> Result { - let entries = SingleFileMessagePackBackingStore::new(&self.odin_trust_anchor_store) - .pull_all_read_only_snapshot() - .with_context(|| { - format!( - "reading Odin topology trust anchor {}", - self.odin_trust_anchor_store.display() - ) - })?; - let [envelope] = entries.as_slice() else { - bail!("Odin topology trust-anchor store is missing or ambiguous") - }; - ensure!( - envelope.r#type == ::TRUST_ANCHOR_TYPE - && envelope.schema_id.as_deref() - == Some(::TRUST_ANCHOR_SCHEMA) - && envelope.key - == ::TRUST_ANCHOR_KEY, - "Odin topology trust anchor belongs to another signing profile" - ); - rmp_serde::from_slice(&envelope.payload).context("decoding Odin topology trust anchor") - } - - fn correlation( - &self, - expected: &IdunnExpectedIncarnationRecord, - current_write_lease_sha256: Option<&str>, - ) -> Result> { - if !self.correlation_store.exists() { - return Ok(None); - } - let entries = SingleFileMessagePackBackingStore::new(&self.correlation_store) - .pull_all_read_only_snapshot()?; - let mut matches = entries.iter().filter(|envelope| { - envelope.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE - && envelope.key == expected.target - }); - let Some(envelope) = matches.next() else { - return Ok(None); - }; - ensure!( - matches.next().is_none(), - "Odin topology correlation is ambiguous" - ); - ensure!( - envelope.schema_id.as_deref() == Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA), - "Odin topology correlation schema is foreign" - ); - let (receipt, unsigned) = - OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload( - &envelope.payload, - )?; - ensure!( - envelope.key == receipt.target, - "Odin correlation key is not its target" - ); - receipt.validate_against_expected(expected, current_write_lease_sha256)?; - let proof = ServiceIdentitySignature { - identity_id: receipt.signer_identity_id.clone(), - signature: receipt.signature.clone(), - }; - verify_service_identity_signature::< - OdinTopologyIdentity, - OdinRuntimeTopologyCorrelationPurpose, - >(&self.anchor()?, &unsigned, &proof)?; - Ok(Some(receipt)) - } } impl TopologyPort for CultCacheTopologyDriver { - type ReadyReceipt = OdinRuntimeTopologyCorrelationRecord; - fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result { expected.validate()?; upsert_record( @@ -2268,6 +3026,61 @@ impl TopologyPort for CultCacheTopologyDriver { expected.canonical_sha256() } + fn withdraw_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()> { + expected.validate()?; + if !self.projection_store.exists() { + return Ok(()); + } + let expected_sha256 = expected.canonical_sha256()?; + let store = SingleFileMessagePackBackingStore::new(&self.projection_store); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let mut found_expected = false; + let mut found_activation = false; + let mut retained = Vec::with_capacity(entries.len()); + for envelope in &entries { + if envelope.key != expected.target { + retained.push(envelope.clone()); + continue; + } + if envelope.r#type == IdunnExpectedIncarnationRecord::TYPE { + ensure!( + !found_expected + && envelope.schema_id.as_deref() + == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA) + && IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)? + == *expected, + "refusing to withdraw a substituted Expected projection" + ); + found_expected = true; + continue; + } + if envelope.r#type == IdunnRuntimeActivationRecord::TYPE { + let activation = + IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)?; + ensure!( + !found_activation + && envelope.schema_id.as_deref() + == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA) + && activation.expected_projection_sha256 == expected_sha256 + && activation.runtime_id == expected.runtime_id, + "refusing to withdraw a substituted runtime activation" + ); + found_activation = true; + continue; + } + retained.push(envelope.clone()); + } + if !found_expected && !found_activation { + return Ok(()); + } + if store.compare_exchange_snapshot(&entries, &retained)? { + return Ok(()); + } + } + bail!("Expected projection changed repeatedly while withdrawing it") + } + fn publish_observed_activation( &self, expected: &IdunnExpectedIncarnationRecord, @@ -2295,59 +3108,31 @@ impl TopologyPort for CultCacheTopologyDriver { activation.canonical_sha256() } - fn present( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result> { - expected.validate()?; - activation.validate()?; - let Some(receipt) = self.correlation(expected, None)? else { + fn receive(&self, target: &str) -> Result> { + require_driver_id(target, "topology target")?; + if !self.correlation_store.exists() { return Ok(None); - }; - let activation_sha256 = activation.canonical_sha256()?; - let exact_activation = receipt.current_activation_sha256.as_deref() - == Some(activation_sha256.as_str()) - && receipt.runtime_instance_id.as_deref() - == Some(activation.runtime_instance_id.as_str()); - let mut disagreements = receipt - .disagreements - .iter() - .map(|disagreement| disagreement.code.clone()) - .collect::>(); - if !exact_activation { - disagreements.push("idunn-current-activation-mismatch".into()); } - disagreements.sort(); - disagreements.dedup(); - Ok(Some(PresenceObservation { - correlation_sha256: receipt.canonical_sha256()?, - signed_presence_sha256: receipt.signed_presence_sha256.clone(), - runtime_instance_id: receipt.runtime_instance_id.clone(), - present: receipt.present && exact_activation, - state: receipt.observed_presence_state.clone(), - write_lease_sha256: receipt.observed_write_lease_sha256.clone(), - disagreements, - })) - } - - fn ready( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - write_lease_sha256: Option<&str>, - ) -> Result> { - expected.validate()?; - activation.validate()?; - let Some(receipt) = self.correlation(expected, write_lease_sha256)? else { + let entries = SingleFileMessagePackBackingStore::new(&self.correlation_store) + .pull_all_read_only_snapshot()?; + let mut matches = entries.iter().filter(|envelope| { + envelope.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE && envelope.key == target + }); + let Some(envelope) = matches.next() else { return Ok(None); }; - let activation_sha256 = activation.canonical_sha256()?; - let exact_activation = receipt.current_activation_sha256.as_deref() - == Some(activation_sha256.as_str()) - && receipt.runtime_instance_id.as_deref() - == Some(activation.runtime_instance_id.as_str()); - Ok((receipt.ready && exact_activation).then_some(receipt)) + ensure!( + matches.next().is_none(), + "Odin topology correlation is ambiguous" + ); + ensure!( + envelope.schema_id.as_deref() == Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA), + "Odin topology correlation schema is foreign" + ); + Ok(Some(ReceivedOdinTopologyCorrelation { + target: target.to_owned(), + canonical_bytes: envelope.payload.clone(), + })) } } @@ -2357,15 +3142,19 @@ impl TopologyPort for CultCacheTopologyDriver { pub struct NginxRouteDriver { pub binding: RouteBinding, pub nginx_program: PathBuf, + pub systemd_run_program: PathBuf, pub systemctl_program: PathBuf, + pub preflight_root: PathBuf, } impl NginxRouteDriver { pub fn new(binding: RouteBinding) -> Self { Self { binding, - nginx_program: PathBuf::from("nginx"), - systemctl_program: PathBuf::from("systemctl"), + nginx_program: PathBuf::from("/usr/sbin/nginx"), + systemd_run_program: PathBuf::from("/usr/bin/systemd-run"), + systemctl_program: PathBuf::from("/usr/bin/systemctl"), + preflight_root: PathBuf::from("/run/idunn/route-preflight"), } } @@ -2374,9 +3163,15 @@ impl NginxRouteDriver { I: IntoIterator, S: AsRef, { + ensure!( + program.is_absolute(), + "route actuator program is not absolute" + ); let output = Command::new(program) .args(args) .stdin(Stdio::null()) + .env_clear() + .env("LANG", "C.UTF-8") .output() .with_context(|| format!("starting route actuator {}", program.display()))?; if !output.status.success() { @@ -2442,6 +3237,31 @@ impl NginxRouteDriver { .is_some_and(|section| section.as_bytes() == rendered)) } + fn current_and_loaded(&self) -> Result<(Option>, Option>)> { + let current = match fs::read(&self.binding.config_path) { + Ok(bytes) => Some(bytes), + Err(error) if error.kind() == ErrorKind::NotFound => None, + Err(error) => return Err(error).context("reading route fragment"), + }; + let output = self.command(&self.nginx_program, [OsString::from("-T")])?; + let dump = std::str::from_utf8(&output.stdout) + .context("nginx loaded configuration is not UTF-8")?; + let loaded = nginx_config_section(dump, &self.binding.config_path)? + .map(|section| section.as_bytes().to_vec()); + Ok((current, loaded)) + } + + fn write_fragment(&self, content: Option<&[u8]>) -> Result<()> { + match content { + Some(bytes) => atomic_replace(&self.binding.config_path, bytes), + None => match fs::remove_file(&self.binding.config_path) { + Ok(()) => sync_parent_directory(&self.binding.config_path), + Err(error) if error.kind() == ErrorKind::NotFound => Ok(()), + Err(error) => Err(error).context("removing route fragment"), + }, + } + } + fn reload(&self) -> Result<()> { self.command(&self.nginx_program, [OsString::from("-t")])?; self.command( @@ -2454,15 +3274,47 @@ impl NginxRouteDriver { Ok(()) } - fn restore(&self, prior: Option<&[u8]>) -> Result<()> { - match prior { - Some(bytes) => atomic_replace(&self.binding.config_path, bytes)?, - None => match fs::remove_file(&self.binding.config_path) { - Ok(()) => {} - Err(error) if error.kind() == ErrorKind::NotFound => {} - Err(error) => return Err(error).context("removing unadmitted route fragment"), - }, + fn validate_candidate_in_private_mount(&self, rendered: &[u8]) -> Result<()> { + ensure_route_preflight_root(&self.preflight_root)?; + let route = nginx_identifier(&self.binding.route_id)?; + let nonce = Uuid::new_v4(); + let candidate = self + .preflight_root + .join(format!("{route}-{nonce}.candidate")); + write_root_owned_file(&candidate, rendered, 0o400)?; + let unit = format!("idunn-nginx-preflight-{nonce}"); + let validation = self.command( + &self.systemd_run_program, + [ + OsString::from("--wait"), + OsString::from("--collect"), + OsString::from("--quiet"), + OsString::from(format!("--unit={unit}")), + OsString::from("--property=Type=exec"), + OsString::from("--property=PrivateMounts=yes"), + systemd_read_only_bind_property(&candidate, &self.binding.config_path)?, + OsString::from("--"), + self.nginx_program.clone().into_os_string(), + OsString::from("-t"), + ], + ); + let cleanup = remove_exact_root_owned_file(&candidate, 0o400); + match (validation, cleanup) { + (Ok(_), Ok(())) => Ok(()), + (Err(validation), Ok(())) => { + Err(validation).context("candidate route is invalid in a private mount namespace") + } + (Ok(_), Err(cleanup)) => { + Err(cleanup).context("deleting route preflight material") + } + (Err(validation), Err(cleanup)) => Err(validation).context(format!( + "candidate route is invalid; deleting its private preflight material also failed: {cleanup:#}" + )), } + } + + fn restore(&self, prior: Option<&[u8]>) -> Result<()> { + self.write_fragment(prior)?; self.reload() } @@ -2486,38 +3338,63 @@ impl RoutePort for NginxRouteDriver { &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, - ) -> Result<()> { + incumbent: Option<&RouteObservation>, + ) -> Result { let rendered = self.render(expected, runtime_instance_id)?; ensure!(!rendered.is_empty(), "candidate route rendered empty"); - self.command(&self.nginx_program, [OsString::from("-t")])?; - let current = match fs::read(&self.binding.config_path) { - Ok(bytes) => Some(bytes), - Err(error) if error.kind() == ErrorKind::NotFound => None, - Err(error) => return Err(error).context("reading route preflight baseline"), - }; - let output = self.command(&self.nginx_program, [OsString::from("-T")])?; - let dump = std::str::from_utf8(&output.stdout) - .context("nginx loaded configuration is not UTF-8")?; - let loaded = nginx_config_section(dump, &self.binding.config_path)?; - match (current.as_deref(), loaded) { - (None, None) => {} - (Some(current), Some(loaded)) if current == loaded.as_bytes() => {} - _ => bail!("nginx route baseline differs from its loaded configuration"), + let (current, loaded) = self.current_and_loaded()?; + ensure!( + current == loaded, + "nginx route baseline differs from its loaded configuration" + ); + let incumbent_membership_sha256 = current.as_ref().map(|bytes| sha256_id(bytes)); + match incumbent { + Some(incumbent) => ensure!( + incumbent.route_id == self.binding.route_id + && Some(incumbent.membership_sha256.as_str()) + == incumbent_membership_sha256.as_deref(), + "route preflight baseline differs from the admitted incumbent" + ), + None => ensure!( + incumbent_membership_sha256.is_none(), + "route preflight found an unadmitted incumbent" + ), } - Ok(()) + self.validate_candidate_in_private_mount(&rendered)?; + let after = self.current_and_loaded()?; + ensure!( + after.0 == current && after.1 == current, + "nginx route baseline changed during candidate validation" + ); + Ok(RoutePreflightReceipt { + route_id: self.binding.route_id.clone(), + candidate_runtime_instance_id: runtime_instance_id.to_owned(), + candidate_membership_sha256: sha256_id(&rendered), + incumbent_runtime_instance_id: incumbent + .map(|observation| observation.runtime_instance_id.clone()), + incumbent_membership_sha256, + }) } fn promote( &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, + preflight: &RoutePreflightReceipt, ) -> Result { let rendered = self.render(expected, runtime_instance_id)?; - let prior = match fs::read(&self.binding.config_path) { - Ok(bytes) => Some(bytes), - Err(error) if error.kind() == ErrorKind::NotFound => None, - Err(error) => return Err(error).context("reading current route fragment"), - }; + ensure!( + preflight.route_id == self.binding.route_id + && preflight.candidate_runtime_instance_id == runtime_instance_id + && preflight.candidate_membership_sha256 == sha256_id(&rendered), + "route preflight does not authorize this candidate membership" + ); + let (prior, loaded) = self.current_and_loaded()?; + let prior_sha256 = prior.as_ref().map(|bytes| sha256_id(bytes)); + ensure!( + prior == loaded && prior_sha256 == preflight.incumbent_membership_sha256, + "route baseline changed after preflight" + ); atomic_replace(&self.binding.config_path, &rendered)?; if let Err(error) = self.reload() { self.fail_after_rollback( @@ -3078,6 +3955,433 @@ fn harden_runtime_bundle(_bundle: &Path) -> Result<()> { bail!("systemd runtime bundles require Unix permissions") } +#[cfg(unix)] +fn ensure_activation_credential_root(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "activation credential actuation requires root Idunn" + ); + ensure!( + path.is_absolute(), + "activation credential root is not absolute" + ); + let parent = path + .parent() + .context("activation credential root has no parent")?; + let parent_metadata = fs::symlink_metadata(parent).with_context(|| { + format!( + "inspecting activation credential parent {}", + parent.display() + ) + })?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0 + && parent.canonicalize()? == parent, + "activation credential parent is not a canonical root-owned directory" + ); + match fs::symlink_metadata(path) { + Ok(metadata) => ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == 0o700 + && path.canonicalize()? == path, + "activation credential root is not an exact root-only native directory" + ), + Err(error) if error.kind() == ErrorKind::NotFound => { + fs::create_dir(path).with_context(|| { + format!("creating activation credential root {}", path.display()) + })?; + fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; + let metadata = fs::symlink_metadata(path)?; + ensure!( + metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == 0o700, + "new activation credential root has the wrong authority" + ); + } + Err(error) => { + return Err(error).with_context(|| { + format!("inspecting activation credential root {}", path.display()) + }); + } + } + Ok(()) +} + +#[cfg(not(unix))] +fn ensure_activation_credential_root(_path: &Path) -> Result<()> { + bail!("systemd activation credentials require a Unix authority path") +} + +#[cfg(unix)] +fn validate_activation_credential_source(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("inspecting activation credential source {}", path.display()))?; + ensure!( + metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == 0o400 + && metadata.nlink() == 1 + && metadata.len() == 32, + "activation credential source is not one exact root-owned 0400 32-byte file" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_activation_credential_source(_path: &Path) -> Result<()> { + bail!("systemd activation credentials require Unix file authority") +} + +#[cfg(unix)] +fn validate_service_credential_sources(sources: &BTreeMap) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + for (name, path) in sources { + ensure!( + path.is_absolute(), + "service credential {name} is not absolute" + ); + let parent = path + .parent() + .context("service credential source has no parent")?; + let parent_metadata = fs::symlink_metadata(parent).with_context(|| { + format!("inspecting service credential parent {}", parent.display()) + })?; + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("inspecting service credential source {}", path.display()))?; + ensure!( + parent.canonicalize()? == parent + && parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.permissions().mode() & 0o022 == 0 + && path.canonicalize()? == path.as_path() + && metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == 0o400 + && metadata.nlink() == 1 + && metadata.len() > 0, + "service credential {name} is not one canonical root-owned 0400 file" + ); + } + Ok(()) +} + +#[cfg(not(unix))] +fn validate_service_credential_sources(_sources: &BTreeMap) -> Result<()> { + bail!("systemd service credentials require Unix file authority") +} + +fn remove_activation_credential_source(path: &Path) -> Result<()> { + match fs::symlink_metadata(path) { + Ok(metadata) => { + validate_activation_credential_source_for_removal(path, &metadata)?; + fs::remove_file(path).with_context(|| { + format!("deleting activation credential source {}", path.display()) + })?; + sync_parent_directory(path)?; + ensure!( + matches!(fs::symlink_metadata(path), Err(error) if error.kind() == ErrorKind::NotFound), + "activation credential source remained after deletion" + ); + Ok(()) + } + Err(error) if error.kind() == ErrorKind::NotFound => Ok(()), + Err(error) => Err(error) + .with_context(|| format!("inspecting activation credential source {}", path.display())), + } +} + +#[cfg(unix)] +fn validate_activation_credential_source_for_removal( + path: &Path, + metadata: &fs::Metadata, +) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let parent = path + .parent() + .context("activation credential source has no parent")?; + let parent_metadata = fs::symlink_metadata(parent)?; + ensure!( + parent_metadata.is_dir() + && !parent_metadata.file_type().is_symlink() + && parent_metadata.uid() == 0 + && parent_metadata.gid() == 0 + && parent_metadata.permissions().mode() & 0o777 == 0o700 + && metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.nlink() == 1 + && metadata.len() <= 32, + "refusing to delete a surprising activation credential source" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_activation_credential_source_for_removal( + _path: &Path, + _metadata: &fs::Metadata, +) -> Result<()> { + bail!("systemd activation credentials require Unix file authority") +} + +fn sync_parent_directory(path: &Path) -> Result<()> { + let parent = path.parent().context("file has no parent to synchronize")?; + let directory = open_native_read_only(parent)?; + directory + .sync_all() + .with_context(|| format!("synchronizing directory {}", parent.display())) +} + +fn open_native_read_only(path: &Path) -> Result { + let mut options = OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW); + } + Ok(options.open(path)?) +} + +fn open_proc_magic_link(path: &Path) -> Result { + let mut options = OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.custom_flags(libc::O_CLOEXEC); + } + options + .open(path) + .with_context(|| format!("opening procfs executable {}", path.display())) +} + +fn sha256_reader(reader: &mut impl Read) -> Result { + let mut digest = Sha256::new(); + let mut buffer = [0_u8; 64 * 1024]; + loop { + let read = reader.read(&mut buffer)?; + if read == 0 { + break; + } + digest.update(&buffer[..read]); + } + Ok(format!("sha256-{:x}", digest.finalize())) +} + +fn path_inside_process_root(process_root: &Path, absolute_path: &Path) -> Result { + ensure!( + process_root.is_absolute() && absolute_path.is_absolute(), + "process-root projection requires absolute paths" + ); + ensure!( + absolute_path.components().all(|component| matches!( + component, + std::path::Component::RootDir | std::path::Component::Normal(_) + )), + "process-root projection path is not normalized" + ); + Ok(process_root.join(absolute_path.strip_prefix(Path::new("/"))?)) +} + +fn linux_process_security( + status_path: &Path, + expected_host_pid: u32, +) -> Result { + let status = fs::read_to_string(status_path) + .with_context(|| format!("reading process status {}", status_path.display()))?; + let uids = linux_status_u32_array(&status, "Uid")?; + let gids = linux_status_u32_array(&status, "Gid")?; + let groups = linux_status_u32_values(&status, "Groups")?; + let cap_inheritable = linux_status_hex_u64(&status, "CapInh")?; + let cap_permitted = linux_status_hex_u64(&status, "CapPrm")?; + let cap_effective = linux_status_hex_u64(&status, "CapEff")?; + let cap_bounding = linux_status_hex_u64(&status, "CapBnd")?; + let cap_ambient = linux_status_hex_u64(&status, "CapAmb")?; + let no_new_privileges = match linux_status_value(&status, "NoNewPrivs")? { + "1" => true, + "0" => false, + _ => bail!("process NoNewPrivs value is invalid"), + }; + let namespace_pids = linux_status_u32_values(&status, "NSpid")?; + ensure!( + namespace_pids.first() == Some(&expected_host_pid), + "process status belongs to another host pid" + ); + Ok(LinuxProcessSecurityObservation { + uids, + gids, + groups, + cap_inheritable, + cap_permitted, + cap_effective, + cap_bounding, + cap_ambient, + no_new_privileges, + namespace_pids, + }) +} + +fn linux_status_value<'a>(status: &'a str, name: &str) -> Result<&'a str> { + let prefix = format!("{name}:"); + let matches = status + .lines() + .filter_map(|line| line.strip_prefix(&prefix)) + .collect::>(); + ensure!( + matches.len() == 1, + "process status has zero or duplicate {name} fields" + ); + Ok(matches[0].trim()) +} + +fn linux_status_u32_values(status: &str, name: &str) -> Result> { + linux_status_value(status, name)? + .split_whitespace() + .map(|value| { + value + .parse::() + .with_context(|| format!("process status {name} value is not a u32")) + }) + .collect() +} + +fn linux_status_u32_array(status: &str, name: &str) -> Result<[u32; 4]> { + linux_status_u32_values(status, name)? + .try_into() + .map_err(|_| anyhow::anyhow!("process status {name} does not have four values")) +} + +fn linux_status_hex_u64(status: &str, name: &str) -> Result { + u64::from_str_radix(linux_status_value(status, name)?, 16) + .with_context(|| format!("process status {name} value is not hexadecimal")) +} + +fn linux_namespace_id(path: &Path, kind: &str) -> Result { + let target = fs::read_link(path) + .with_context(|| format!("reading Linux namespace link {}", path.display()))?; + let target = target + .to_str() + .context("Linux namespace link is not UTF-8")?; + let prefix = format!("{kind}:["); + let id = target + .strip_prefix(&prefix) + .and_then(|value| value.strip_suffix(']')) + .context("Linux namespace link has an unexpected shape")?; + let id = id + .parse::() + .context("Linux namespace id is not a u64")?; + ensure!(id > 0, "Linux namespace id is zero"); + Ok(id) +} + +#[cfg(unix)] +fn resolve_group_id(group: &str) -> Result { + use std::ffi::CString; + + if let Ok(group_id) = group.parse::() { + ensure!(group_id > 0, "state group must be unprivileged"); + return Ok(group_id); + } + let group = CString::new(group).context("state group contains a NUL byte")?; + let mut buffer_size = 16_384_usize; + loop { + let mut entry = std::mem::MaybeUninit::::uninit(); + let mut result = std::ptr::null_mut(); + let mut buffer = vec![0_u8; buffer_size]; + let status = unsafe { + libc::getgrnam_r( + group.as_ptr(), + entry.as_mut_ptr(), + buffer.as_mut_ptr().cast(), + buffer.len(), + &mut result, + ) + }; + if status == libc::ERANGE && buffer_size < 1_048_576 { + buffer_size *= 2; + continue; + } + ensure!( + status == 0, + "resolving state group failed with errno {status}" + ); + ensure!(!result.is_null(), "configured state group does not exist"); + let entry = unsafe { entry.assume_init() }; + ensure!(entry.gr_gid > 0, "state group must be unprivileged"); + return Ok(entry.gr_gid); + } +} + +#[cfg(not(unix))] +fn resolve_group_id(_group: &str) -> Result { + bail!("systemd workload groups require a Unix actuator") +} + +#[cfg(unix)] +fn validate_workload_writable_path( + path: &Path, + state_group_id: u32, + require_setgid: bool, +) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(path) + .with_context(|| format!("inspecting workload writable path {}", path.display()))?; + let mode = metadata.permissions().mode(); + ensure!( + !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == state_group_id + && mode & 0o002 == 0, + "workload writable path {} is not preprovisioned root:state-group without world write", + path.display() + ); + if metadata.is_dir() { + ensure!( + mode & 0o070 == 0o070 && (!require_setgid || mode & 0o2000 != 0), + "workload writable directory {} lacks group access or setgid inheritance", + path.display() + ); + } else { + ensure!( + metadata.is_file() && mode & 0o060 == 0o060, + "workload writable file is special or lacks group access" + ); + } + Ok(()) +} + +#[cfg(not(unix))] +fn validate_workload_writable_path( + _path: &Path, + _state_group_id: u32, + _require_setgid: bool, +) -> Result<()> { + bail!("systemd workload writable paths require Unix permissions") +} + fn linux_process_start_time(stat_path: &Path) -> Result { let stat = fs::read_to_string(stat_path) .with_context(|| format!("reading process stat {}", stat_path.display()))?; @@ -3096,11 +4400,24 @@ fn required_systemd_property<'a>( values: &'a BTreeMap, name: &str, ) -> Result<&'a str> { + let value = systemd_property(values, name)?; + ensure!(!value.is_empty(), "systemd unit has no {name}"); + Ok(value) +} + +fn systemd_property<'a>(values: &'a BTreeMap, name: &str) -> Result<&'a str> { values .get(name) - .filter(|value| !value.is_empty()) .map(String::as_str) - .with_context(|| format!("systemd unit has no {name}")) + .with_context(|| format!("systemd unit did not expose {name}")) +} + +fn parse_systemd_boolean(values: &BTreeMap, name: &str) -> Result { + match systemd_property(values, name)? { + "yes" => Ok(true), + "no" => Ok(false), + _ => bail!("systemd unit exposed a non-boolean {name}"), + } } fn read_process_environment(path: &Path) -> Result>> { @@ -3113,6 +4430,22 @@ fn read_process_environment(path: &Path) -> Result>> { .collect()) } +fn required_process_environment_value<'a>(entries: &'a [Vec], name: &str) -> Result<&'a str> { + let prefix = format!("{name}=").into_bytes(); + let matches = entries + .iter() + .filter_map(|entry| entry.strip_prefix(prefix.as_slice())) + .collect::>(); + ensure!( + matches.len() == 1, + "process environment has zero or duplicate {name} values" + ); + let value = std::str::from_utf8(matches[0]) + .with_context(|| format!("process environment {name} is not UTF-8"))?; + ensure!(!value.is_empty(), "process environment {name} is empty"); + Ok(value) +} + fn select_process_environment( entries: &[Vec], names: &[String], @@ -3208,28 +4541,167 @@ fn nginx_config_section<'a>(dump: &'a str, config_path: &Path) -> Result Result<()> { + #[cfg(unix)] + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + + ensure!(path.is_absolute(), "route path is not absolute"); let parent = path.parent().context("route path has no parent")?; - fs::create_dir_all(parent)?; + ensure_route_authority_parent(parent)?; + match fs::symlink_metadata(path) { + Ok(_) => validate_root_owned_file(path, 0o644)?, + Err(error) if error.kind() == ErrorKind::NotFound => {} + Err(error) => return Err(error).context("inspecting route fragment"), + } let file_name = path .file_name() .context("route path has no file name")? .to_string_lossy(); let temporary = parent.join(format!(".{file_name}.idunn-{}", Uuid::new_v4())); - let mut file = fs::OpenOptions::new() - .create_new(true) - .write(true) + let mut options = fs::OpenOptions::new(); + options.create_new(true).write(true); + #[cfg(unix)] + options.mode(0o644); + let mut file = options .open(&temporary) .with_context(|| format!("creating route stage {}", temporary.display()))?; + #[cfg(unix)] + file.set_permissions(fs::Permissions::from_mode(0o644))?; file.write_all(bytes)?; file.sync_all()?; drop(file); + validate_root_owned_file(&temporary, 0o644)?; if let Err(error) = fs::rename(&temporary, path) { let _ = fs::remove_file(&temporary); return Err(error).with_context(|| format!("publishing route {}", path.display())); } + sync_parent_directory(path)?; + validate_root_owned_file(path, 0o644) +} + +#[cfg(unix)] +fn ensure_route_authority_parent(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!( + unsafe { libc::geteuid() } == 0, + "route actuation requires root Idunn" + ); + ensure!(path.is_absolute(), "route authority parent is not absolute"); + if !path.exists() { + let ancestor = path + .parent() + .context("route authority parent has no ancestor")?; + let ancestor_metadata = fs::symlink_metadata(ancestor)?; + ensure!( + ancestor.canonicalize()? == ancestor + && ancestor_metadata.is_dir() + && !ancestor_metadata.file_type().is_symlink() + && ancestor_metadata.uid() == 0 + && ancestor_metadata.permissions().mode() & 0o022 == 0, + "route authority ancestor is not canonical root-owned and nonwritable" + ); + fs::create_dir(path)?; + fs::set_permissions(path, fs::Permissions::from_mode(0o755))?; + } + let metadata = fs::symlink_metadata(path)?; + ensure!( + path.canonicalize()? == path + && metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o022 == 0, + "route authority parent is not canonical root-owned and nonwritable" + ); Ok(()) } +#[cfg(not(unix))] +fn ensure_route_authority_parent(_path: &Path) -> Result<()> { + bail!("nginx route actuation requires Unix file authority") +} + +#[cfg(unix)] +fn ensure_route_preflight_root(path: &Path) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + ensure!(path.is_absolute(), "route preflight root is not absolute"); + if !path.exists() { + let parent = path + .parent() + .context("route preflight root has no parent")?; + ensure_route_authority_parent(parent)?; + fs::create_dir(path)?; + fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; + } + let metadata = fs::symlink_metadata(path)?; + ensure!( + unsafe { libc::geteuid() } == 0 + && path.canonicalize()? == path + && metadata.is_dir() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == 0o700, + "route preflight root is not one canonical root-only directory" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn ensure_route_preflight_root(_path: &Path) -> Result<()> { + bail!("nginx route preflight requires Unix file authority") +} + +#[cfg(unix)] +fn write_root_owned_file(path: &Path, bytes: &[u8], mode: u32) -> Result<()> { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + + let mut options = OpenOptions::new(); + options.create_new(true).write(true).mode(mode); + let mut file = options.open(path)?; + file.set_permissions(fs::Permissions::from_mode(mode))?; + file.write_all(bytes)?; + file.sync_all()?; + drop(file); + sync_parent_directory(path)?; + validate_root_owned_file(path, mode) +} + +#[cfg(not(unix))] +fn write_root_owned_file(_path: &Path, _bytes: &[u8], _mode: u32) -> Result<()> { + bail!("root-owned route material requires Unix file authority") +} + +#[cfg(unix)] +fn validate_root_owned_file(path: &Path, mode: u32) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let metadata = fs::symlink_metadata(path)?; + ensure!( + path.canonicalize()? == path + && metadata.is_file() + && !metadata.file_type().is_symlink() + && metadata.uid() == 0 + && metadata.gid() == 0 + && metadata.permissions().mode() & 0o777 == mode + && metadata.nlink() == 1, + "route authority material is not one canonical root-owned file" + ); + Ok(()) +} + +#[cfg(not(unix))] +fn validate_root_owned_file(_path: &Path, _mode: u32) -> Result<()> { + bail!("root-owned route material requires Unix file authority") +} + +fn remove_exact_root_owned_file(path: &Path, mode: u32) -> Result<()> { + validate_root_owned_file(path, mode)?; + fs::remove_file(path)?; + sync_parent_directory(path) +} + fn bind_mount(source: &Path, destination: &str, read_only: bool) -> Result { ensure!(source.is_absolute(), "Docker bind source is not absolute"); let text = source.to_str().context("Docker bind source is not UTF-8")?; @@ -3245,6 +4717,28 @@ fn bind_mount(source: &Path, destination: &str, read_only: bool) -> Result Result { + ensure!( + source.is_absolute() && destination.is_absolute(), + "systemd bind paths are not absolute" + ); + let source = source + .to_str() + .context("systemd bind source is not UTF-8")?; + let destination = destination + .to_str() + .context("systemd bind destination is not UTF-8")?; + ensure!( + [source, destination] + .iter() + .all(|path| !path.contains([':', '\n', '\r', '\0'])), + "systemd bind path contains an unescaped property delimiter" + ); + Ok(OsString::from(format!( + "--property=BindReadOnlyPaths={source}:{destination}" + ))) +} + fn normalized_relative(path: &Path) -> Result { ensure!(path.is_relative(), "runner path is not relative"); let mut parts = Vec::new(); @@ -3809,8 +5303,11 @@ fn apply_identity(command: &mut Command, identity: Option) -> R mod tests { use super::*; use cultnet_rs::{ - IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, - IDUNN_RUNTIME_ACTIVATION_SCHEMA, + GameCultProviderHealthIdentity, IDUNN_EXPECTED_INCARNATION_SCHEMA, + IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IdunnServiceIdentity, + OdinRuntimeTopologyCorrelationPurpose, OdinTopologyAuthenticationContext, + OdinTopologyIdentity, authenticate_odin_runtime_topology_correlation, + enroll_service_identity_at, verify_runtime_authority, }; fn digest(byte: char) -> String { @@ -3860,31 +5357,74 @@ mod tests { } } - fn activation(expected: &IdunnExpectedIncarnationRecord) -> IdunnRuntimeActivationRecord { - IdunnRuntimeActivationRecord { - schema_version: IDUNN_RUNTIME_ACTIVATION_SCHEMA.into(), - expected_projection_sha256: expected.canonical_sha256().unwrap(), - runtime_instance_id: digest('7'), - issued_at_unix_millis: 100, - } - } - - fn warming(activation: &IdunnRuntimeActivationRecord) -> PresenceObservation { - PresenceObservation { - correlation_sha256: digest('8'), + fn authenticated_warming( + root: &Path, + ) -> Result<( + IdunnExpectedIncarnationRecord, + IdunnRuntimeActivationRecord, + SequenceAdmittedWarming, + )> { + let provider = enroll_service_identity_at::( + &root.join("provider.cc"), + )?; + let idunn = enroll_service_identity_at::(&root.join("idunn.cc"))?; + let odin = enroll_service_identity_at::(&root.join("odin.cc"))?; + let mut expected = expected(); + expected.expected_signer_identity_id = provider.entry().identity_id.clone(); + let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest('7'), 100, &idunn)?; + let activation = launch.activation().clone(); + launch.write_credential(std::io::sink())?; + let authority = verify_runtime_authority( + &expected, + &activation, + &idunn.trust_anchor()?, + &provider.entry().public_key, + )?; + let mut warming = OdinRuntimeTopologyCorrelationRecord { + schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256()?, + expected: true, + current_activation_sha256: Some(activation.canonical_sha256()?), signed_presence_sha256: Some(digest('9')), + observed_presence_state: Some("warming".into()), + observed_presence_publisher_sequence: Some(1), + observed_write_lease_sha256: None, + observed_capabilities: Vec::new(), + runtime_id: expected.runtime_id.clone(), runtime_instance_id: Some(activation.runtime_instance_id.clone()), present: true, - state: Some("warming".into()), - write_lease_sha256: None, + ready: false, + dependencies: Vec::new(), disagreements: Vec::new(), - } + signer_identity_id: odin.entry().identity_id.clone(), + publisher_sequence: 1, + observed_at_unix_millis: 110, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + }; + warming.signature = odin + .sign::(&warming.unsigned_signature_payload()?) + .signature; + let warming = authenticate_odin_runtime_topology_correlation( + &warming.canonical_bytes()?, + &authority, + None, + &odin.entry().public_key, + OdinTopologyAuthenticationContext { + trusted_received_at_unix_millis: 120, + maximum_age_millis: 30, + maximum_future_skew_millis: 5, + }, + )?; + let warming = SequenceAdmittedWarming::for_test("test-transaction", warming, 120)?; + Ok((expected, activation, warming)) } fn lease( expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, - warming: &PresenceObservation, + warming: &SequenceAdmittedWarming, ) -> IdunnProcessWriteLeaseRecord { IdunnProcessWriteLeaseRecord { schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), @@ -3898,7 +5438,12 @@ mod tests { state_contract_sha256: expected.state_contract_sha256.clone().unwrap(), runtime_id: expected.runtime_id.clone(), runtime_instance_id: activation.runtime_instance_id.clone(), - warming_presence_sha256: warming.signed_presence_sha256.clone().unwrap(), + warming_presence_sha256: warming + .authenticated() + .record() + .signed_presence_sha256 + .clone() + .unwrap(), lease_epoch: 1, issued_at_unix_millis: 200, } @@ -3922,6 +5467,89 @@ mod tests { Ok(()) } + #[cfg(unix)] + #[test] + fn nginx_preflight_validates_candidate_and_binds_the_loaded_incumbent() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let config = temp.path().join("route.conf"); + let loaded = temp.path().join("loaded.conf"); + let nginx = temp.path().join("nginx"); + let systemd_run = temp.path().join("systemd-run"); + let systemctl = temp.path().join("systemctl"); + fs::write( + &nginx, + format!( + "#!/bin/sh\nconfig='{}'\nloaded='{}'\nif [ \"$1\" = \"-T\" ]; then\n if [ -f \"$loaded\" ]; then\n printf '# configuration file %s:\\n' \"$config\"\n /bin/cat \"$loaded\"\n fi\n exit 0\nfi\nif [ \"$1\" = \"-t\" ]; then\n candidate=${{IDUNN_TEST_SHADOW:-$config}}\n /bin/grep -q 'server 127.0.0.1:4104' \"$candidate\"\n exit $?\nfi\nexit 64\n", + config.display(), + loaded.display(), + ), + )?; + fs::write( + &systemd_run, + "#!/bin/sh\nshadow=\nwhile [ \"$#\" -gt 0 ]; do\n case \"$1\" in\n --property=BindReadOnlyPaths=*) binding=${1#--property=BindReadOnlyPaths=}; shadow=${binding%%:*} ;; + --) shift; break ;; + esac\n shift\ndone\nIDUNN_TEST_SHADOW=\"$shadow\"; export IDUNN_TEST_SHADOW\nexec \"$@\"\n", + )?; + fs::write( + &systemctl, + format!( + "#!/bin/sh\nif [ \"$1\" = \"reload\" ]; then\n /bin/cp '{}' '{}'\n exit 0\nfi\nexit 64\n", + config.display(), + loaded.display(), + ), + )?; + fs::set_permissions(&nginx, fs::Permissions::from_mode(0o755))?; + fs::set_permissions(&systemd_run, fs::Permissions::from_mode(0o755))?; + fs::set_permissions(&systemctl, fs::Permissions::from_mode(0o755))?; + + let binding = RouteBinding { + driver: RouteDriver::NginxHttp, + route_id: "service-route".into(), + stable_endpoint: "http://127.0.0.1:4103".into(), + private_host: "127.0.0.1".into(), + private_port_start: 4104, + private_port_end: 4109, + config_path: config.clone(), + reload_unit: "nginx.service".into(), + }; + let driver = NginxRouteDriver { + binding, + nginx_program: nginx, + systemd_run_program: systemd_run, + systemctl_program: systemctl, + preflight_root: temp.path().join("preflight"), + }; + let mut candidate = expected(); + candidate.route = Some(cultnet_rs::IdunnExpectedRoute { + route_id: "service-route".into(), + transport: "http".into(), + stable_endpoint: "http://127.0.0.1:4103".into(), + candidate_endpoint: "http://127.0.0.1:4104".into(), + }); + let preflight = driver.preflight(&candidate, &digest('a'), None)?; + assert!(!config.exists()); + assert!(!loaded.exists()); + assert_eq!(fs::read_dir(&driver.preflight_root)?.count(), 0); + + let admitted = driver.promote(&candidate, &digest('a'), &preflight)?; + assert!(driver.observe(&candidate, &admitted)?); + let admitted_bytes = fs::read(&config)?; + assert_eq!(fs::read(&loaded)?, admitted_bytes); + + let next_preflight = driver.preflight(&candidate, &digest('b'), Some(&admitted))?; + assert_eq!(fs::read(&config)?, admitted_bytes); + fs::write(&config, b"foreign route\n")?; + assert!( + driver + .promote(&candidate, &digest('b'), &next_preflight) + .is_err() + ); + assert_eq!(fs::read(&loaded)?, admitted_bytes); + Ok(()) + } + #[test] fn process_environment_witness_is_sorted_exact_and_secret_free() -> Result<()> { let entries = vec![ @@ -3936,15 +5564,153 @@ mod tests { Ok(()) } + #[test] + fn topology_transport_returns_opaque_odin_bytes_without_deciding_ready() -> Result<()> { + let temp = tempfile::tempdir()?; + let correlation_store = temp.path().join("correlation.cc"); + let opaque = b"not a topology receipt".to_vec(); + upsert_record( + &correlation_store, + CultCacheEnvelope { + key: "service".into(), + r#type: OdinRuntimeTopologyCorrelationRecord::TYPE.into(), + payload: opaque.clone(), + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into()), + }, + )?; + let driver = CultCacheTopologyDriver { + projection_store: temp.path().join("projection.cc"), + correlation_store, + }; + + assert_eq!( + driver.receive("service")?, + Some(ReceivedOdinTopologyCorrelation { + target: "service".into(), + canonical_bytes: opaque, + }) + ); + Ok(()) + } + + #[test] + fn expected_withdrawal_is_exact_atomic_and_idempotent() -> Result<()> { + let temp = tempfile::tempdir()?; + let (expected, activation, _) = authenticated_warming(temp.path())?; + let projection_store = temp.path().join("projection.cc"); + let driver = CultCacheTopologyDriver { + projection_store: projection_store.clone(), + correlation_store: temp.path().join("correlation.cc"), + }; + driver.publish_expected(&expected)?; + upsert_record( + &projection_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: activation.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + )?; + upsert_record( + &projection_store, + CultCacheEnvelope { + key: "unrelated".into(), + r#type: "test.unrelated".into(), + payload: vec![0x90], + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some("test.unrelated.v1".into()), + }, + )?; + + driver.withdraw_expected(&expected)?; + let remaining = SingleFileMessagePackBackingStore::new(&projection_store) + .pull_all_read_only_snapshot()?; + assert_eq!(remaining.len(), 1); + assert_eq!(remaining[0].key, "unrelated"); + driver.withdraw_expected(&expected)?; + + let substituted_store = temp.path().join("substituted-expected.cc"); + let substituted_driver = CultCacheTopologyDriver { + projection_store: substituted_store.clone(), + correlation_store: temp.path().join("substituted-correlation.cc"), + }; + substituted_driver.publish_expected(&expected)?; + let mut substituted_expected = expected.clone(); + substituted_expected.incarnation_id = "another-incarnation".into(); + assert!( + substituted_driver + .withdraw_expected(&substituted_expected) + .is_err() + ); + + let substituted_activation_store = temp.path().join("substituted-activation.cc"); + let substituted_activation_driver = CultCacheTopologyDriver { + projection_store: substituted_activation_store.clone(), + correlation_store: temp.path().join("substituted-activation-correlation.cc"), + }; + substituted_activation_driver.publish_expected(&expected)?; + let mut substituted_activation = activation; + substituted_activation.expected_projection_sha256 = digest('a'); + upsert_record( + &substituted_activation_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: substituted_activation.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + )?; + assert!( + substituted_activation_driver + .withdraw_expected(&expected) + .is_err() + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn service_credential_sources_require_one_root_owned_0400_inode() -> Result<()> { + use std::os::unix::fs::{PermissionsExt, symlink}; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("credentials"); + fs::create_dir(&root)?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o700))?; + let source = root.join("token"); + fs::write(&source, b"secret")?; + fs::set_permissions(&source, fs::Permissions::from_mode(0o400))?; + let mut sources = BTreeMap::from([("SERVICE_TOKEN".into(), source.clone())]); + + validate_service_credential_sources(&sources)?; + + fs::set_permissions(&source, fs::Permissions::from_mode(0o600))?; + assert!(validate_service_credential_sources(&sources).is_err()); + fs::set_permissions(&source, fs::Permissions::from_mode(0o400))?; + + let hardlink = root.join("token-hardlink"); + fs::hard_link(&source, &hardlink)?; + assert!(validate_service_credential_sources(&sources).is_err()); + fs::remove_file(&hardlink)?; + + let symlink_path = root.join("token-symlink"); + symlink(&source, &symlink_path)?; + sources.insert("SERVICE_TOKEN".into(), symlink_path); + assert!(validate_service_credential_sources(&sources).is_err()); + Ok(()) + } + #[cfg(unix)] #[test] fn write_lease_revoke_is_exact_and_never_deletes_a_surprise() -> Result<()> { let temp = tempfile::tempdir()?; let path = temp.path().join("lease.cc"); let driver = CultCacheWriteLeaseDriver::new("service", &path); - let expected = expected(); - let activation = activation(&expected); - let warming = warming(&activation); + let (expected, activation, warming) = authenticated_warming(temp.path())?; let lease = lease(&expected, &activation, &warming); let lease_sha256 = driver.grant(&expected, &activation, &warming, &lease)?; assert_eq!(lease_sha256, lease.canonical_sha256()?); diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index e45e039..e542225 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit e45e039ebecb005fad65602b7822b7b8a37f2428 +Subproject commit e542225c197656ee6fb651518f9bebafb52ecf92 From 86925539084a189d9df7fe0f2b91aa1a5be6f0c0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 06:02:54 +0200 Subject: [PATCH 82/93] Fence parent-only runtime signing credentials --- Cargo.lock | 1 + crates/idunn-daemon/src/control_plane.rs | 117 ++-- crates/idunn-daemon/src/deployment.rs | 61 +- crates/idunn-daemon/src/deployment_plan.rs | 3 + crates/idunn-daemon/src/drivers.rs | 620 +++++++++++++-------- vendor/cultnet-rs | 2 +- 6 files changed, 523 insertions(+), 281 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4625713..5606f06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,6 +228,7 @@ dependencies = [ "socket2", "uuid", "windows-sys 0.61.2", + "zeroize", ] [[package]] diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs index 3a4fe62..9eb1a8f 100644 --- a/crates/idunn-daemon/src/control_plane.rs +++ b/crates/idunn-daemon/src/control_plane.rs @@ -400,14 +400,14 @@ impl CleanupEvidence { #[serde(deny_unknown_fields)] struct PreFencingAbort { error: String, - candidate_stop: CleanupEvidence, + candidate_cleanup: CleanupEvidence, topology_reconciliation: CleanupEvidence, source_cleanup: CleanupEvidence, } impl PreFencingAbort { fn is_complete(&self) -> bool { - self.candidate_stop.is_complete() + self.candidate_cleanup.is_complete() && self.topology_reconciliation.is_complete() && self.source_cleanup.is_complete() } @@ -609,7 +609,7 @@ impl DeploymentTransaction { transaction.last_error = Some(detail.clone()); transaction.pre_fencing_abort = Some(PreFencingAbort { error: detail.clone(), - candidate_stop: CleanupEvidence::Skipped, + candidate_cleanup: CleanupEvidence::Skipped, topology_reconciliation: CleanupEvidence::Skipped, source_cleanup: CleanupEvidence::Skipped, }); @@ -708,6 +708,10 @@ impl DeploymentTransaction { ); } } + ensure!( + self.workload.is_none() || self.activation.is_some(), + "workload observation exists without its prepared activation" + ); for evidence in [&self.latest_odin_observation, &self.warming, &self.ready] .into_iter() .flatten() @@ -778,11 +782,14 @@ impl DeploymentTransaction { ); ensure!( matches!( - (self.workload.is_some(), abort.candidate_stop), + ( + self.workload.is_some() || self.activation.is_some(), + abort.candidate_cleanup + ), (true, CleanupEvidence::Pending | CleanupEvidence::Complete) | (false, CleanupEvidence::Skipped) ), - "abort candidate-stop evidence differs from its candidate observation" + "abort candidate cleanup differs from its prepared activation" ); let topology_cleanup_required = self.command_kind == CommandKind::Deploy && self.expected_publication_sha256.is_some(); @@ -2860,38 +2867,6 @@ impl Engine { let expected = required(¤t.value.expected, "Expected projection")?; let activation = required(¤t.value.activation, "activation")?; if current.value.routing.is_none() { - let current_lease = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - if let Some((admitted, authenticated)) = - self.admit_latest_topology(current, current_lease)? - { - if admitted.envelope != current.envelope { - return Ok(()); - } - let latest = required( - &admitted.value.latest_odin_observation, - "latest sequence-admitted topology", - )?; - if current.value.ready.as_ref() != Some(latest) { - if is_semantic_ready(&authenticated) { - let evidence = latest.clone(); - let _token = SequenceAdmittedReady { - transaction_id: admitted.value.transaction_id.clone(), - evidence: evidence.clone(), - expected: expected.clone(), - authenticated, - }; - return self.persist_same_phase(&admitted, |next| { - next.ready = Some(evidence); - Ok(()) - }); - } - return Ok(()); - } - } let ready = self.rehydrate_ready_token(¤t.value, now, false)?; ensure!( ready.transaction_id() == current.value.transaction_id, @@ -3503,11 +3478,10 @@ impl Engine { ); let abort = PreFencingAbort { error: truncate(&format!("{error:#}"), 2048), - candidate_stop: if current.value.workload.is_some() { - CleanupEvidence::Pending - } else { - CleanupEvidence::Skipped - }, + candidate_cleanup: candidate_cleanup_requirement( + current.value.activation.is_some(), + current.value.workload.is_some(), + ), topology_reconciliation: if current.value.command_kind == CommandKind::Deploy && current.value.expected_publication_sha256.is_some() { @@ -3533,15 +3507,22 @@ impl Engine { "pre-fencing abort crossed the fencing boundary" ); let abort = required(¤t.value.pre_fencing_abort, "pre-fencing abort intent")?; - if abort.candidate_stop == CleanupEvidence::Pending { + if abort.candidate_cleanup == CleanupEvidence::Pending { + if let Some(workload) = ¤t.value.workload { + self.workload + .stop(workload) + .context("stopping exact pre-fence candidate")?; + } self.workload - .stop(required( - ¤t.value.workload, - "pre-fencing candidate observation", - )?) - .context("stopping exact pre-fence candidate")?; + .discard_prepared( + required(¤t.value.plan, "pre-fencing candidate plan")?, + required(¤t.value.expected, "pre-fencing Expected projection")?, + required(¤t.value.activation, "pre-fencing activation")?, + ) + .context("discarding exact pre-fence activation material")?; return self.persist_same_phase(current, |next| { - next.pre_fencing_abort.as_mut().unwrap().candidate_stop = CleanupEvidence::Complete; + next.pre_fencing_abort.as_mut().unwrap().candidate_cleanup = + CleanupEvidence::Complete; Ok(()) }); } @@ -3646,6 +3627,17 @@ impl Engine { } } +fn candidate_cleanup_requirement( + has_prepared_activation: bool, + has_workload_observation: bool, +) -> CleanupEvidence { + if has_prepared_activation || has_workload_observation { + CleanupEvidence::Pending + } else { + CleanupEvidence::Skipped + } +} + fn prove_isolation( candidate: &WorkloadObservation, incumbent: Option<&WorkloadObservation>, @@ -4009,13 +4001,8 @@ mod tests { environment_names: Vec::new(), environment_contract_sha256: sha256_id(&[3]), control_group: format!("/system.slice/idunn-{uid}.service"), - credentials_directory: PathBuf::from("/run/credentials/test"), - activation_credential_device: 1, - activation_credential_inode: 2, - activation_credential_uid: uid, - activation_credential_gid: uid, - activation_credential_mode: 0o400, - activation_credential_size: 32, + credentials_directory: None, + parent_only_file_descriptors: Vec::new(), activation_signer_identity_id: "activation".into(), activation_signer_public_key: vec![1; 32], service_credentials: Vec::new(), @@ -4163,7 +4150,7 @@ mod tests { DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; transaction.pre_fencing_abort = Some(PreFencingAbort { error: "sealed source was rejected".into(), - candidate_stop: CleanupEvidence::Skipped, + candidate_cleanup: CleanupEvidence::Skipped, topology_reconciliation: CleanupEvidence::Skipped, source_cleanup: CleanupEvidence::Pending, }); @@ -4185,6 +4172,22 @@ mod tests { Ok(()) } + #[test] + fn activation_without_workload_still_requires_durable_candidate_cleanup() { + assert_eq!( + candidate_cleanup_requirement(true, false), + CleanupEvidence::Pending + ); + assert_eq!( + candidate_cleanup_requirement(true, true), + CleanupEvidence::Pending + ); + assert_eq!( + candidate_cleanup_requirement(false, false), + CleanupEvidence::Skipped + ); + } + #[test] fn bad_selector_becomes_one_terminal_refusal_record() -> Result<()> { let command = command(CommandKind::Deploy); diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index 2dcd5b5..ded7e2b 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -9,6 +9,8 @@ pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v2"; pub const IDUNN_RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE"; pub const IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_CANDIDATE_BIND"; pub const IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE"; +pub const RUNTIME_PRESENCE_IDENTITY_BINDING: &str = "GAMECULT_RUNTIME_PRESENCE_IDENTITY"; +pub const RUNTIME_PRESENCE_IDENTITY_FD_NAME: &str = "gamecult-runtime-presence-identity"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -1000,6 +1002,27 @@ impl OperatorBinding { for path in self.workload.secret_files.values() { require_absolute_path(path, "secret file")?; } + ensure!( + !self + .workload + .environment + .contains_key(RUNTIME_PRESENCE_IDENTITY_BINDING), + "runtime presence identity must be a parent-only secret source, not environment" + ); + let presence_identity = self + .workload + .secret_files + .get(RUNTIME_PRESENCE_IDENTITY_BINDING) + .context("workload has no parent-only runtime presence identity source")?; + ensure!( + self.workload + .secret_files + .iter() + .filter(|(_, path)| *path == presence_identity) + .count() + == 1, + "runtime presence identity source is duplicated under another secret binding" + ); if let Some(route) = self.route.as_ref() { require_id(&route.route_id, "route id")?; require_value(&route.stable_endpoint, "stable endpoint")?; @@ -1205,7 +1228,12 @@ impl OperatorBinding { .workload .environment .keys() - .chain(self.workload.secret_files.keys()) + .chain( + self.workload + .secret_files + .keys() + .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING), + ) .cloned() .collect(); available_environment.insert(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into()); @@ -1228,6 +1256,10 @@ impl OperatorBinding { .union(&declaration.service.optional_environment) .cloned() .collect(); + ensure!( + !declared_environment.contains(RUNTIME_PRESENCE_IDENTITY_BINDING), + "runtime presence identity is a named parent-only descriptor, not service environment" + ); ensure!( available_environment.is_subset(&declared_environment), "operator service environment contains an undeclared launch input" @@ -1640,6 +1672,9 @@ cpu_quota_percent = 200 [workload.argument_bindings] state_root = "/var/lib/gamecult/test-service" +[workload.secret_files] +GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/test-service/runtime-presence-identity.cc" + [runtime_identity] runtime_id = "test-service-yggdrasil" expected_signer_identity_id = "test-service-runtime-signer" @@ -1762,6 +1797,30 @@ nodes = ["yggdrasil"] assert!(binding.admit(&recipe).is_err()); } + #[test] + fn runtime_presence_identity_is_exactly_one_parent_only_secret_source() { + let secret = "[workload.secret_files]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n"; + assert!(OperatorBinding::parse(&BINDING.replace(secret, "")).is_err()); + assert!(OperatorBinding::parse(&BINDING.replace( + secret, + "[workload.environment]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n", + )) + .is_err()); + assert!(OperatorBinding::parse(&BINDING.replace( + secret, + "[workload.secret_files]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\nDUPLICATE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n", + )) + .is_err()); + + let recipe = TargetDeclaration::parse(&RECIPE.replace( + "required_environment = [", + "required_environment = [\"GAMECULT_RUNTIME_PRESENCE_IDENTITY\", ", + )) + .unwrap(); + let binding = OperatorBinding::parse(BINDING).unwrap(); + assert!(binding.admit(&recipe).is_err()); + } + #[test] fn operator_cannot_replace_idunn_owned_launch_inputs() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index 91096f6..ca4469a 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -1104,6 +1104,9 @@ cpu_quota_percent = 100 [workload.argument_bindings] state_root = "/var/lib/gamecult/service" +[workload.secret_files] +GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/service/runtime-presence-identity.cc" + [runtime_identity] runtime_id = "service-yggdrasil" expected_signer_identity_id = "service-runtime-signer" diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs index a6875ab..c4bfe5e 100644 --- a/crates/idunn-daemon/src/drivers.rs +++ b/crates/idunn-daemon/src/drivers.rs @@ -20,19 +20,21 @@ use crate::control_plane::SequenceAdmittedWarming; use crate::deployment::{ ArtifactOutput, ArtifactSource, IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, LaunchArgument, - OperatorBinding, RouteBinding, RouteDriver, RunnerBinding, SourceSelectionPolicy, - TargetDeclaration, WorkloadNetwork, + OperatorBinding, RUNTIME_PRESENCE_IDENTITY_BINDING, RUNTIME_PRESENCE_IDENTITY_FD_NAME, + RouteBinding, RouteDriver, RunnerBinding, SourceSelectionPolicy, TargetDeclaration, + WorkloadNetwork, }; use crate::deployment_plan::{ ArtifactReceipt, CompiledDeploymentPlan, ExternalInputMaterializationReceipt, GitlinkTreeFact, SOURCE_SELECTION_FACTS_SCHEMA, SealedRelease, SourceSelection, SourceSelectionFacts, }; use cultnet_rs::{ - IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, - IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, IDUNN_RUNTIME_ACTIVATION_SCHEMA, - IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, - IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, + GameCultProviderHealthIdentity, IDUNN_EXPECTED_INCARNATION_SCHEMA, + IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, + IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, + IdunnRuntimeActivationLaunch, IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, OdinRuntimeTopologyCorrelationRecord, + open_service_identity_credential_reader, }; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -155,6 +157,26 @@ pub struct ServiceCredentialObservation { pub sha256: String, } +/// Root-owned source metadata for one descriptor that PID1 opens and passes +/// only to the service's initial process. The process must consume and close +/// descriptors 3 and 4 before spawning any child; no filesystem path is +/// projected into the workload environment. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ParentOnlyFileDescriptorObservation { + pub fd_number: u32, + pub fd_name: String, + pub source_path: PathBuf, + pub access: String, + pub device: u64, + pub inode: u64, + pub uid: u32, + pub gid: u32, + pub mode: u32, + pub links: u64, + pub size: u64, + pub sha256: String, +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct WorkloadObservation { pub unit: String, @@ -203,13 +225,8 @@ pub struct WorkloadObservation { pub environment_names: Vec, pub environment_contract_sha256: String, pub control_group: String, - pub credentials_directory: PathBuf, - pub activation_credential_device: u64, - pub activation_credential_inode: u64, - pub activation_credential_uid: u32, - pub activation_credential_gid: u32, - pub activation_credential_mode: u32, - pub activation_credential_size: u64, + pub credentials_directory: Option, + pub parent_only_file_descriptors: Vec, pub activation_signer_identity_id: String, pub activation_signer_public_key: Vec, pub service_credentials: Vec, @@ -229,6 +246,12 @@ struct LinuxProcessSecurityObservation { namespace_pids: Vec, } +#[derive(Clone, Debug, PartialEq, Eq)] +struct SystemdUnitObservation { + properties: BTreeMap, + open_files: Vec, +} + pub trait WorkloadPort { fn install( &self, @@ -252,6 +275,13 @@ pub trait WorkloadPort { activation: &IdunnRuntimeActivationRecord, ) -> Result; + fn discard_prepared( + &self, + plan: &CompiledDeploymentPlan, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result<()>; + fn observe( &self, expected: &IdunnExpectedIncarnationRecord, @@ -1529,10 +1559,53 @@ impl SystemdTransientWorkloadDriver { Ok(()) } + fn parent_only_file_descriptors( + &self, + binding: &OperatorBinding, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result> { + let activation_source = self.activation_credential_source(activation)?; + self.validate_prepared_activation_credential(activation, &activation_source)?; + let presence_source = binding + .workload + .secret_files + .get(RUNTIME_PRESENCE_IDENTITY_BINDING) + .context("workload has no parent-only runtime presence identity source")?; + let presence_signer = + open_service_identity_credential_reader::( + open_native_read_only(presence_source).with_context(|| { + format!( + "opening runtime presence identity source {}", + presence_source.display() + ) + })?, + )?; + ensure!( + presence_signer.entry().identity_id == expected.expected_signer_identity_id, + "runtime presence identity source differs from Expected" + ); + let descriptors = vec![ + observe_parent_only_file_descriptor( + 3, + IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, + &activation_source, + )?, + observe_parent_only_file_descriptor( + 4, + RUNTIME_PRESENCE_IDENTITY_FD_NAME, + presence_source, + )?, + ]; + parent_only_open_file_properties(&descriptors)?; + Ok(descriptors) + } + fn stop_submitted_unit(&self, unit: &str, expected_description: &str) -> Result<()> { - let Some(values) = self.show_unit(unit)? else { + let Some(observation) = self.show_unit(unit)? else { return Ok(()); }; + let values = &observation.properties; ensure!( values.get("Description").map(String::as_str) == Some(expected_description), "refusing to stop a submitted unit whose description changed" @@ -1541,7 +1614,8 @@ impl SystemdTransientWorkloadDriver { &self.systemctl_program, [OsString::from("stop"), OsString::from(unit)], )?; - if let Some(values) = self.show_unit(unit)? { + if let Some(observation) = self.show_unit(unit)? { + let values = &observation.properties; ensure!( values .get("ActiveState") @@ -1552,29 +1626,6 @@ impl SystemdTransientWorkloadDriver { Ok(()) } - fn failed_start_error( - &self, - primary: anyhow::Error, - unit: &str, - expected_description: &str, - credential_source: &Path, - ) -> anyhow::Error { - let stop = self.stop_submitted_unit(unit, expected_description); - let remove = remove_activation_credential_source(credential_source); - match (stop, remove) { - (Ok(()), Ok(())) => primary, - (Err(stop), Ok(())) => { - primary.context(format!("stopping failed candidate also failed: {stop:#}")) - } - (Ok(()), Err(remove)) => primary.context(format!( - "deleting failed candidate activation credential also failed: {remove:#}" - )), - (Err(stop), Err(remove)) => primary.context(format!( - "failed candidate cleanup also failed (stop: {stop:#}; credential delete: {remove:#})" - )), - } - } - fn install_release( &self, plan: &CompiledDeploymentPlan, @@ -1686,7 +1737,7 @@ impl SystemdTransientWorkloadDriver { Ok(bundle) } - fn show_unit(&self, unit: &str) -> Result>> { + fn show_unit(&self, unit: &str) -> Result> { ensure!( self.systemctl_program.is_absolute(), "systemctl program is not absolute" @@ -1720,6 +1771,7 @@ impl SystemdTransientWorkloadDriver { OsString::from("--property=UMask"), OsString::from("--property=InaccessiblePaths"), OsString::from("--property=LoadCredential"), + OsString::from("--property=OpenFile"), OsString::from("--property=WorkingDirectory"), OsString::from("--property=ControlGroup"), ]) @@ -1731,10 +1783,15 @@ impl SystemdTransientWorkloadDriver { let text = std::str::from_utf8(&output.stdout).context("systemd show output is not UTF-8")?; let mut values = BTreeMap::new(); + let mut open_files = Vec::new(); for line in text.lines().filter(|line| !line.is_empty()) { let (name, value) = line .split_once('=') .context("systemd show output is malformed")?; + if name == "OpenFile" { + open_files.push(value.to_owned()); + continue; + } ensure!( values.insert(name.to_owned(), value.to_owned()).is_none(), "systemd show property is duplicated" @@ -1751,7 +1808,10 @@ impl SystemdTransientWorkloadDriver { "systemd show failed for {unit}: {}", String::from_utf8_lossy(&output.stderr).trim() ); - Ok(Some(values)) + Ok(Some(SystemdUnitObservation { + properties: values, + open_files, + })) } fn observe_unit( @@ -1763,6 +1823,7 @@ impl SystemdTransientWorkloadDriver { activation_signer_public_key: &[u8], environment_names: &[String], service_credential_names: &[String], + parent_only_file_descriptors: &[ParentOnlyFileDescriptorObservation], ) -> Result { ensure!( service_credential_names @@ -1770,9 +1831,15 @@ impl SystemdTransientWorkloadDriver { .all(|pair| pair[0] < pair[1]), "service credential names are not unique and sorted" ); - let values = self + let unit_observation = self .show_unit(unit)? .with_context(|| format!("systemd unit {unit} is absent"))?; + let values = &unit_observation.properties; + ensure!( + unit_observation.open_files + == parent_only_open_file_properties(parent_only_file_descriptors)?, + "systemd parent-only descriptor contract differs from the Idunn launch" + ); ensure!( values .get("ActiveState") @@ -1831,7 +1898,12 @@ impl SystemdTransientWorkloadDriver { && no_new_privileges && umask == "0007" && inaccessible_paths == self.credential_root.display().to_string() - && load_credential == "[unprintable]", + && load_credential + == if service_credential_names.is_empty() { + "" + } else { + "[unprintable]" + }, "systemd workload isolation properties differ from the Idunn contract" ); let working_directory = @@ -1956,70 +2028,41 @@ impl SystemdTransientWorkloadDriver { runtime_bundle.is_absolute(), "runtime bundle path is not absolute" ); - let credentials_directory = PathBuf::from(required_process_environment_value( - &process_environment, - "CREDENTIALS_DIRECTORY", - )?); - ensure!( - credentials_directory == Path::new("/run/credentials").join(unit), - "systemd MainPID exposed an unexpected credential directory" - ); - let credential_path = path_inside_process_root( - &process_root.join("root"), - &credentials_directory.join(IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME), - )?; - let credential_file = open_native_read_only(&credential_path).with_context(|| { - format!( - "opening delivered activation credential {}", - credential_path.display() - ) - })?; - let credential_metadata = credential_file.metadata()?; - ensure!( - credential_metadata.is_file() && credential_metadata.len() == 32, - "delivered activation credential is not one exact 32-byte native file" - ); - let activation_signer = - IdunnRuntimeActivationSigner::from_credential_reader(credential_file)?; - ensure!( - activation_signer.identity_id() == activation_signer_identity_id - && activation_signer.public_key() == activation_signer_public_key, - "delivered activation credential differs from the Idunn-signed activation" - ); - #[cfg(unix)] - let ( - activation_credential_device, - activation_credential_inode, - activation_credential_uid, - activation_credential_gid, - activation_credential_mode, - ) = { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - ( - credential_metadata.dev(), - credential_metadata.ino(), - credential_metadata.uid(), - credential_metadata.gid(), - credential_metadata.permissions().mode() & 0o777, - ) + let credentials_directory = if service_credential_names.is_empty() { + ensure!( + !process_environment + .iter() + .any(|entry| entry.starts_with(b"CREDENTIALS_DIRECTORY=")), + "systemd MainPID exposed an unowned credential directory" + ); + None + } else { + let path = PathBuf::from(required_process_environment_value( + &process_environment, + "CREDENTIALS_DIRECTORY", + )?); + ensure!( + path == Path::new("/run/credentials").join(unit), + "systemd MainPID exposed an unexpected credential directory" + ); + Some(path) }; - #[cfg(not(unix))] - let ( - activation_credential_device, - activation_credential_inode, - activation_credential_uid, - activation_credential_gid, - activation_credential_mode, - ) = (0, 0, 0, 0, 0); + let activation_descriptor = &parent_only_file_descriptors[0]; ensure!( - activation_credential_uid == process_security.uids[0] - && activation_credential_gid == process_security.gids[0] - && activation_credential_mode == 0o400, - "delivered activation credential is not readable only by the workload identity" + activation_descriptor.fd_name == IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME + && activation_descriptor.size == 32 + && activation_descriptor.uid == 0 + && activation_descriptor.gid == 0 + && activation_descriptor.mode == 0o400 + && activation_descriptor.links == 1, + "activation signing descriptor is not exact root-only source material" ); let mut service_credentials = Vec::new(); for name in service_credential_names { - let delivered_path = credentials_directory.join(name); + let delivered_path = credentials_directory + .as_ref() + .context("service credential has no systemd credential directory")? + .join(name); let delivered_value = delivered_path.display().to_string(); ensure!( selected_environment.get(name) == Some(&delivered_value), @@ -2073,7 +2116,7 @@ impl SystemdTransientWorkloadDriver { .show_unit(unit)? .with_context(|| format!("systemd unit {unit} vanished during observation"))?; ensure!( - final_values == values + final_values == unit_observation && linux_process_start_time(&process_root.join("stat"))? == process_start_time, "systemd workload identity changed during native observation" ); @@ -2125,14 +2168,9 @@ impl SystemdTransientWorkloadDriver { environment_contract_sha256: sha256_id(&rmp_serde::to_vec(&selected_environment)?), control_group, credentials_directory, - activation_credential_device, - activation_credential_inode, - activation_credential_uid, - activation_credential_gid, - activation_credential_mode, - activation_credential_size: credential_metadata.len(), - activation_signer_identity_id: activation_signer.identity_id(), - activation_signer_public_key: activation_signer.public_key(), + parent_only_file_descriptors: parent_only_file_descriptors.to_vec(), + activation_signer_identity_id: activation_signer_identity_id.to_owned(), + activation_signer_public_key: activation_signer_public_key.to_vec(), service_credentials, }) } @@ -2168,7 +2206,12 @@ impl SystemdTransientWorkloadDriver { ) -> Result> { let mut environment = binding.workload.environment.clone(); let credentials_directory = Path::new("/run/credentials").join(unit); - for name in binding.workload.secret_files.keys() { + for name in binding + .workload + .secret_files + .keys() + .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) + { ensure!( environment .insert( @@ -2254,7 +2297,28 @@ impl SystemdTransientWorkloadDriver { ); let expected_group = binding.workload.state_group.as_deref(); let expected_group_id = expected_group.map(resolve_group_id).transpose()?; - let expected_credentials_directory = Path::new("/run/credentials").join(&observation.unit); + let regular_credential_names = binding + .workload + .secret_files + .keys() + .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) + .cloned() + .collect::>(); + let expected_credentials_directory = (!regular_credential_names.is_empty()) + .then(|| Path::new("/run/credentials").join(&observation.unit)); + let descriptor_properties = + parent_only_open_file_properties(&observation.parent_only_file_descriptors)?; + let activation_source = self.activation_credential_source(activation)?; + let presence_source = binding + .workload + .secret_files + .get(RUNTIME_PRESENCE_IDENTITY_BINDING) + .context("workload has no parent-only runtime presence identity source")?; + let observed_credential_names = observation + .service_credentials + .iter() + .map(|credential| credential.environment_name.clone()) + .collect::>(); ensure!( observation.unit_description == expected_description && observation.service_type == "exec" @@ -2276,7 +2340,12 @@ impl SystemdTransientWorkloadDriver { && observation.no_new_privileges && observation.umask == "0007" && observation.inaccessible_paths == self.credential_root.display().to_string() - && observation.load_credential == "[unprintable]" + && observation.load_credential + == if regular_credential_names.is_empty() { + "" + } else { + "[unprintable]" + } && observation.working_directory == installed && observation.runtime_bundle == bundle && observation.credentials_directory == expected_credentials_directory @@ -2284,11 +2353,15 @@ impl SystemdTransientWorkloadDriver { && observation.environment_names == environment_names && observation.environment_contract_sha256 == sha256_id(&rmp_serde::to_vec(&environment)?) - && observation.activation_credential_size == 32 + && descriptor_properties.len() == 2 + && observation.parent_only_file_descriptors[0].source_path == activation_source + && observation.parent_only_file_descriptors[0].size == 32 + && observation.parent_only_file_descriptors[1].source_path == *presence_source && observation.activation_signer_identity_id == activation.activation_signer_identity_id && observation.activation_signer_public_key == activation.activation_signer_public_key + && observed_credential_names == regular_credential_names && match expected_group_id { Some(group_id) => observation.process_gids[0] == group_id, None => observation.process_gids[0] == observation.process_uids[0], @@ -2329,7 +2402,7 @@ impl SystemdTransientWorkloadDriver { installed: &Path, bundle: &Path, expected: &IdunnExpectedIncarnationRecord, - credential_source: &Path, + parent_only_file_descriptors: &[ParentOnlyFileDescriptorObservation], unit: &str, ) -> Result<()> { ensure!( @@ -2351,6 +2424,8 @@ impl SystemdTransientWorkloadDriver { "operator binding attempts to replace the Idunn runtime bundle" ); validate_service_credential_sources(&binding.workload.secret_files)?; + let parent_only_open_files = + parent_only_open_file_properties(parent_only_file_descriptors)?; let environment = self.launch_environment(binding, bundle, expected, unit)?; let executable_artifact = release_artifact(declaration, &declaration.service.executable_artifact)?; @@ -2395,10 +2470,6 @@ impl SystemdTransientWorkloadDriver { "--property=InaccessiblePaths={}", self.credential_root.display() )), - OsString::from(format!( - "--property=LoadCredential={IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME}:{}", - credential_source.display() - )), OsString::from(format!( "--property=MemoryMax={}M", binding.workload.memory_mebibytes @@ -2411,6 +2482,9 @@ impl SystemdTransientWorkloadDriver { OsString::from(format!("--property=ReadOnlyPaths={}", installed.display())), OsString::from(format!("--property=ReadOnlyPaths={}", bundle.display())), ]; + for open_file in parent_only_open_files { + args.push(OsString::from(format!("--property=OpenFile={open_file}"))); + } if let Some(state_group) = &binding.workload.state_group { args.push(OsString::from(format!("--property=Group={state_group}"))); } @@ -2457,7 +2531,12 @@ impl SystemdTransientWorkloadDriver { for (name, value) in environment { args.push(OsString::from(format!("--setenv={name}={value}"))); } - for (name, path) in &binding.workload.secret_files { + for (name, path) in binding + .workload + .secret_files + .iter() + .filter(|(name, _)| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) + { args.push(OsString::from(format!( "--property=LoadCredential={name}:{}", path.display() @@ -2568,72 +2647,26 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { .workload .secret_files .keys() + .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) .cloned() .collect::>(); let environment_names = self .launch_environment(&binding, &bundle, expected, &unit)? .into_keys() .collect::>(); - let unit_description = format!( - "Idunn {} {}", - binding.target, activation.runtime_instance_id - ); - let credential_source = self.activation_credential_source(activation)?; + let parent_only_file_descriptors = + self.parent_only_file_descriptors(&binding, expected, activation)?; let unit_exists = self.show_unit(&unit)?.is_some(); - match fs::symlink_metadata(&credential_source) { - Ok(_) => { - if let Err(error) = - self.validate_prepared_activation_credential(activation, &credential_source) - { - return if unit_exists { - Err(self.failed_start_error( - error, - &unit, - &unit_description, - &credential_source, - )) - } else { - match remove_activation_credential_source(&credential_source) { - Ok(()) => Err(error), - Err(cleanup) => Err(error.context(format!( - "deleting mismatched prepared credential also failed: {cleanup:#}" - ))), - } - }; - } - } - Err(error) if error.kind() == ErrorKind::NotFound && unit_exists => {} - Err(error) if error.kind() == ErrorKind::NotFound => { - bail!( - "prepared activation has neither a credential source nor an existing exact unit" - ) - } - Err(error) => { - return Err(error).with_context(|| { - format!( - "inspecting prepared activation credential {}", - credential_source.display() - ) - }); - } - } if !unit_exists { - if let Err(error) = self.start_transient( + self.start_transient( &declaration, &binding, &installed.root, &bundle, expected, - &credential_source, + &parent_only_file_descriptors, &unit, - ) { - return Err(self.failed_start_error( - error, - &unit, - &unit_description, - &credential_source, - )); - } + )?; } let mut last_error = None; for _ in 0..100 { @@ -2645,6 +2678,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { &activation.activation_signer_public_key, &environment_names, &service_credential_names, + &parent_only_file_descriptors, ) { Ok(observation) => { let validation = self.validate_launch_observation( @@ -2663,48 +2697,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { ); Ok(()) }) { - Ok(()) => { - if let Err(error) = - remove_activation_credential_source(&credential_source) - { - return Err(self.failed_start_error( - error.context( - "deleting observed one-shot activation credential source", - ), - &unit, - &unit_description, - &credential_source, - )); - } - let after_unlink = self.observe_unit( - &unit, - &executable, - &activation.runtime_instance_id, - &activation.activation_signer_identity_id, - &activation.activation_signer_public_key, - &environment_names, - &service_credential_names, - ); - return match after_unlink { - Ok(after_unlink) if after_unlink == observation => Ok(after_unlink), - Ok(_) => Err(self.failed_start_error( - anyhow::anyhow!( - "workload identity changed after activation source deletion" - ), - &unit, - &unit_description, - &credential_source, - )), - Err(error) => Err(self.failed_start_error( - error.context( - "reobserving copied activation credential after source deletion", - ), - &unit, - &unit_description, - &credential_source, - )), - }; - } + Ok(()) => return Ok(observation), Err(error) => last_error = Some(error), } } @@ -2712,12 +2705,40 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { } thread::sleep(Duration::from_millis(50)); } - Err(self.failed_start_error( - last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate")), - &unit, - &unit_description, - &credential_source, - )) + Err(last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate"))) + } + + fn discard_prepared( + &self, + plan: &CompiledDeploymentPlan, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result<()> { + plan.validate()?; + expected.validate()?; + activation.validate()?; + ensure!( + expected.plan_id == plan.plan_id + && activation.expected_projection_sha256 == expected.canonical_sha256()?, + "discarded activation does not belong to the prepared deployment" + ); + let (_, binding) = plan.parsed_inputs()?; + ensure!( + binding.target == expected.target, + "discarded activation target differs from its binding" + ); + let unit = self.unit_name( + &binding.workload.unit_prefix, + &activation.runtime_instance_id, + )?; + let unit_description = format!( + "Idunn {} {}", + binding.target, activation.runtime_instance_id + ); + self.stop_submitted_unit(&unit, &unit_description) + .context("stopping prepared candidate without a durable workload observation")?; + remove_activation_credential_source(&self.activation_credential_source(activation)?) + .context("deleting discarded activation credential source") } fn observe( @@ -2745,6 +2766,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { .iter() .map(|credential| credential.environment_name.clone()) .collect::>(), + &prior.parent_only_file_descriptors, )?; ensure!( observed == *prior && observed.executable_sha256 == expected.artifact_sha256, @@ -2765,6 +2787,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { .iter() .map(|credential| credential.environment_name.clone()) .collect::>(), + &prior.parent_only_file_descriptors, )?; ensure!( after_unlink == observed, @@ -2774,9 +2797,10 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { } fn stop(&self, observation: &WorkloadObservation) -> Result<()> { - let Some(values) = self.show_unit(&observation.unit)? else { + let Some(unit_observation) = self.show_unit(&observation.unit)? else { return Ok(()); }; + let values = &unit_observation.properties; let active = required_systemd_property(&values, "ActiveState")?; let sub = required_systemd_property(&values, "SubState")?; if active == "active" && sub == "running" { @@ -2792,6 +2816,7 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { .iter() .map(|credential| credential.environment_name.clone()) .collect::>(), + &observation.parent_only_file_descriptors, )?; ensure!( current == *observation, @@ -2809,7 +2834,8 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { &self.systemctl_program, [OsString::from("stop"), OsString::from(&observation.unit)], )?; - if let Some(values) = self.show_unit(&observation.unit)? { + if let Some(unit_observation) = self.show_unit(&observation.unit)? { + let values = &unit_observation.properties; ensure!( values .get("ActiveState") @@ -3955,6 +3981,112 @@ fn harden_runtime_bundle(_bundle: &Path) -> Result<()> { bail!("systemd runtime bundles require Unix permissions") } +fn observe_parent_only_file_descriptor( + fd_number: u32, + fd_name: &str, + source_path: &Path, +) -> Result { + validate_open_file_component(fd_name, "parent-only descriptor name")?; + ensure!( + source_path.is_absolute(), + "parent-only descriptor source is not absolute" + ); + validate_open_file_component( + &source_path.as_os_str().to_string_lossy(), + "parent-only descriptor source", + )?; + let mut file = open_native_read_only(source_path).with_context(|| { + format!( + "opening parent-only descriptor source {}", + source_path.display() + ) + })?; + let metadata = file.metadata()?; + ensure!( + metadata.is_file() && metadata.len() > 0, + "parent-only descriptor source is not one nonempty native file" + ); + #[cfg(unix)] + let (device, inode, uid, gid, mode, links) = { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + ( + metadata.dev(), + metadata.ino(), + metadata.uid(), + metadata.gid(), + metadata.permissions().mode() & 0o777, + metadata.nlink(), + ) + }; + #[cfg(not(unix))] + let (device, inode, uid, gid, mode, links) = (0, 0, 0, 0, 0, 0); + ensure!( + uid == 0 && gid == 0 && mode == 0o400 && links == 1, + "parent-only descriptor source is not root-owned, 0400, and singly linked" + ); + Ok(ParentOnlyFileDescriptorObservation { + fd_number, + fd_name: fd_name.to_owned(), + source_path: source_path.to_owned(), + access: "read-only".into(), + device, + inode, + uid, + gid, + mode, + links, + size: metadata.len(), + sha256: sha256_reader(&mut file)?, + }) +} + +fn parent_only_open_file_properties( + descriptors: &[ParentOnlyFileDescriptorObservation], +) -> Result> { + ensure!( + descriptors.len() == 2 + && descriptors[0].fd_number == 3 + && descriptors[0].fd_name == IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME + && descriptors[1].fd_number == 4 + && descriptors[1].fd_name == RUNTIME_PRESENCE_IDENTITY_FD_NAME, + "parent-only descriptor set is not the exact ordered activation/presence pair" + ); + descriptors + .iter() + .map(|descriptor| { + validate_open_file_component(&descriptor.fd_name, "parent-only descriptor name")?; + validate_open_file_component( + &descriptor.source_path.as_os_str().to_string_lossy(), + "parent-only descriptor source", + )?; + ensure!( + descriptor.source_path.is_absolute() + && descriptor.access == "read-only" + && descriptor.uid == 0 + && descriptor.gid == 0 + && descriptor.mode == 0o400 + && descriptor.links == 1 + && descriptor.size > 0, + "parent-only descriptor metadata is outside the Idunn contract" + ); + Ok(format!( + "{}:{}:{}", + descriptor.source_path.display(), + descriptor.fd_name, + descriptor.access + )) + }) + .collect() +} + +fn validate_open_file_component(value: &str, label: &str) -> Result<()> { + ensure!( + !value.is_empty() && !value.contains(':') && !value.chars().any(char::is_control), + "{label} cannot be represented by systemd OpenFile" + ); + Ok(()) +} + #[cfg(unix)] fn ensure_activation_credential_root(path: &Path) -> Result<()> { use std::os::unix::fs::{MetadataExt, PermissionsExt}; @@ -5314,6 +5446,50 @@ mod tests { format!("sha256-{}", byte.to_string().repeat(64)) } + fn parent_only_descriptor( + fd_number: u32, + fd_name: &str, + source: &str, + ) -> ParentOnlyFileDescriptorObservation { + ParentOnlyFileDescriptorObservation { + fd_number, + fd_name: fd_name.into(), + source_path: PathBuf::from(source), + access: "read-only".into(), + device: 1, + inode: u64::from(fd_number), + uid: 0, + gid: 0, + mode: 0o400, + links: 1, + size: 32, + sha256: digest('a'), + } + } + + #[test] + fn signer_sources_lower_to_exactly_two_ordered_parent_only_open_files() -> Result<()> { + let activation = parent_only_descriptor( + 3, + IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, + "/run/idunn/activation-credentials/activation.credential", + ); + let presence = parent_only_descriptor( + 4, + RUNTIME_PRESENCE_IDENTITY_FD_NAME, + "/etc/gamecult/service/runtime-presence-identity.cc", + ); + assert_eq!( + parent_only_open_file_properties(&[activation.clone(), presence.clone()])?, + vec![ + "/run/idunn/activation-credentials/activation.credential:gamecult-idunn-runtime-activation-key:read-only", + "/etc/gamecult/service/runtime-presence-identity.cc:gamecult-runtime-presence-identity:read-only", + ] + ); + assert!(parent_only_open_file_properties(&[presence, activation]).is_err()); + Ok(()) + } + #[cfg(unix)] fn git_at(repository: &Path, arguments: &[&str]) -> Result { let output = Command::new("/usr/bin/git") diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs index e542225..9dcb88d 160000 --- a/vendor/cultnet-rs +++ b/vendor/cultnet-rs @@ -1 +1 @@ -Subproject commit e542225c197656ee6fb651518f9bebafb52ecf92 +Subproject commit 9dcb88df1361b157a48502f43093fe7a25ca22b7 From 65cf2b2f5c6e3b44651371c36d5349454bb870ab Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 06:05:23 +0200 Subject: [PATCH 83/93] Reject pre-OpenFile control records --- crates/idunn-daemon/src/control_plane.rs | 42 ++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs index 9eb1a8f..225bc27 100644 --- a/crates/idunn-daemon/src/control_plane.rs +++ b/crates/idunn-daemon/src/control_plane.rs @@ -39,8 +39,8 @@ use crate::drivers::{ }; const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v2"; -const DEPLOYMENT_TRANSACTION_SCHEMA: &str = "idunn.deployment_transaction.v1"; -const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v1"; +const DEPLOYMENT_TRANSACTION_SCHEMA: &str = "idunn.deployment_transaction.v2"; +const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v2"; const DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS: u64 = 30_000; const DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS: u64 = 2_000; @@ -4333,6 +4333,44 @@ mod tests { Ok(()) } + #[test] + fn loadcredential_era_control_schemas_are_rejected_before_rehydration() -> Result<()> { + for (record_type, key, schema) in [ + ( + DeploymentTransaction::TYPE, + "tx-legacy-workload-observation", + "idunn.deployment_transaction.v1", + ), + ( + AdmittedGeneration::TYPE, + "legacy-admitted-target", + "idunn.admitted_generation.v1", + ), + ] { + let temporary = tempfile::tempdir()?; + let path = temporary.path().join("control.cc"); + let legacy = CultCacheEnvelope { + key: key.into(), + r#type: record_type.into(), + payload: Vec::new(), + stored_at: rfc3339_millis(100)?, + schema_id: Some(schema.into()), + }; + assert!( + SingleFileMessagePackBackingStore::new(&path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: record_type.into(), + key: key.into(), + current: None, + }], + &[legacy], + )? + ); + assert!(ControlSnapshot::read(&path).is_err()); + } + Ok(()) + } + #[test] fn source_identity_is_explicit_and_atomic() { let partial = parse( From 70ad92c8e37883a9ea0a3a9e4a8c7a53dd66c568 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Sep 2026 23:14:48 +0200 Subject: [PATCH 84/93] Restore Idunn starfire paths after the E: drive loss The projects root moved from E: to F:, but Idunn's starfire-local surfaces still named E:\Projects. Local Idunn died on 2026-08-16 with "fatal: cannot change to 'E:\Projects\Heimdall'" and every one of its 16 operator alarms also failed (0xfffd0000), because notify-idunn-operator-alarm.cmd shelled to an E: path that no longer existed. The supervisor was down for 18 days and could not say so. Rather than swap one hardcoded drive letter for another, derive the projects root the way this catalog already derives its repo root: - notify-idunn-operator-alarm.cmd invokes its sibling via %~dp0. - notify-idunn-operator-alarm.ps1 resolves Bifrost from the projects root. - idunn-deployment-targets.ps1 gains $projectsRoot and rebases 19 LocalPath values onto it. The remote /srv/build/VoidBot path is left alone. Verified: the catalog resolves 15 of 20 targets to real directories, the remote target is untouched, and the alarm chain now runs to exit 0 emitting a valid gamecult.operator_dm_request.v1 under IDUNN_OPERATOR_ALARM_DRY_RUN. Still broken, not addressed here: stonks, vili, StreamPixels and repixelizer have no directory under F:\Projects, and the Bifrost publisher's non-dry path throws "definition.schema.parse is not a function" against the current CultMesh. Co-Authored-By: Claude Opus 5 --- scripts/idunn-deployment-targets.ps1 | 39 +++++++++++++------------ scripts/notify-idunn-operator-alarm.cmd | 2 +- scripts/notify-idunn-operator-alarm.ps1 | 4 ++- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/scripts/idunn-deployment-targets.ps1 b/scripts/idunn-deployment-targets.ps1 index 9eb7241..86c7e7a 100644 --- a/scripts/idunn-deployment-targets.ps1 +++ b/scripts/idunn-deployment-targets.ps1 @@ -1,4 +1,5 @@ $repoRoot = Split-Path -Parent $PSScriptRoot +$projectsRoot = Split-Path -Parent $repoRoot $IdunnDeploymentSharedActuators = @( "$repoRoot\scripts\deploy-yggdrasil-source-app.ps1" @@ -8,7 +9,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "odin" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "starfire" Service = "odin-coordinator" Status = "runtime-enforced" @@ -21,7 +22,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "idunn" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "starfire" Service = "idunn swarm supervisor" Status = "runtime-enforced" @@ -34,7 +35,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "hermodr" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "starfire" Service = "Hermodr browser lowering" Status = "runtime-enforced" @@ -47,7 +48,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "stonks" Repo = "Stonks" - LocalPath = "E:\Projects\Stonks" + LocalPath = "$projectsRoot\Stonks" Host = "starfire" Service = "stonks daemon" Status = "runtime-enforced" @@ -60,7 +61,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "weksa" Repo = "weksa" - LocalPath = "E:\Projects\weksa" + LocalPath = "$projectsRoot\weksa" Host = "starfire" Service = "weksa daemon" Status = "runtime-enforced" @@ -73,7 +74,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "starfire-muninn" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "starfire" Service = "muninn serve --host starfire" Status = "runtime-enforced" @@ -86,7 +87,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "raven-muninn" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "raven" Service = "muninn serve --host raven" Status = "runtime-enforced" @@ -99,7 +100,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "nightwing-muninn" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "nightwing" Service = "muninn serve --host nightwing" Status = "runtime-enforced" @@ -112,7 +113,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "vili" Repo = "Vili" - LocalPath = "E:\Projects\Vili" + LocalPath = "$projectsRoot\Vili" Host = "raven" Service = "GameCult\\Vili" Status = "runtime-enforced" @@ -125,7 +126,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "raven-sleipnir" Repo = "Odin" - LocalPath = "E:\Projects\Odin" + LocalPath = "$projectsRoot\Odin" Host = "raven" Service = "sleipnir --host raven" Status = "runtime-enforced" @@ -156,7 +157,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "yggdrasil-bifrost" Repo = "Bifrost" - LocalPath = "E:\Projects\Bifrost" + LocalPath = "$projectsRoot\Bifrost" Host = "yggdrasil" Service = "bifrost.service" Status = "blocked" @@ -169,7 +170,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "yggdrasil-streampixels" Repo = "StreamPixels" - LocalPath = "E:\Projects\StreamPixels" + LocalPath = "$projectsRoot\StreamPixels" Host = "yggdrasil" Service = "streampixels-service/streampixels-web" Status = "enforced" @@ -187,7 +188,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "yggdrasil-heimdall" Repo = "Heimdall" - LocalPath = "E:\Projects\Heimdall" + LocalPath = "$projectsRoot\Heimdall" Host = "yggdrasil" Service = "heimdall.service" Status = "enforced" @@ -205,7 +206,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "yggdrasil-repixelizer" Repo = "repixelizer" - LocalPath = "E:\Projects\repixelizer" + LocalPath = "$projectsRoot\repixelizer" Host = "yggdrasil" Service = "repixelizer-gui.service" Status = "enforced" @@ -223,7 +224,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "github-pages-gamecult-site" Repo = "gamecult-site" - LocalPath = "E:\Projects\gamecult-site" + LocalPath = "$projectsRoot\gamecult-site" Host = "github-pages" Service = "gamecult.org/www" Status = "external-owned" @@ -236,7 +237,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "eve-ipad-evecanvas" Repo = "Eve" - LocalPath = "E:\Projects\Eve" + LocalPath = "$projectsRoot\Eve" Host = "eve-ipad" Service = "org.gamecult.evecanvas" Status = "blocked" @@ -249,7 +250,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "periwinkle-eve-android" Repo = "Eve" - LocalPath = "E:\Projects\Eve" + LocalPath = "$projectsRoot\Eve" Host = "periwinkle" Service = "Eve Android proof APK" Status = "blocked" @@ -262,7 +263,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "nightwing-eve-dashboard" Repo = "Mimir" - LocalPath = "E:\Projects\Mimir" + LocalPath = "$projectsRoot\Mimir" Host = "nightwing" Service = "nightwing-eve-dashboard.service" Status = "archived" @@ -275,7 +276,7 @@ $IdunnDeploymentTargets = @( [pscustomobject]@{ Id = "nightwing-eve-browser-reference" Repo = "Eve" - LocalPath = "E:\Projects\Eve" + LocalPath = "$projectsRoot\Eve" Host = "nightwing" Service = "nightwing-eve-browser-reference.service" Status = "runtime-enforced" diff --git a/scripts/notify-idunn-operator-alarm.cmd b/scripts/notify-idunn-operator-alarm.cmd index 33b17d2..7955f4f 100644 --- a/scripts/notify-idunn-operator-alarm.cmd +++ b/scripts/notify-idunn-operator-alarm.cmd @@ -1,2 +1,2 @@ @echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\notify-idunn-operator-alarm.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0notify-idunn-operator-alarm.ps1" diff --git a/scripts/notify-idunn-operator-alarm.ps1 b/scripts/notify-idunn-operator-alarm.ps1 index 76c51a3..e8f6a97 100644 --- a/scripts/notify-idunn-operator-alarm.ps1 +++ b/scripts/notify-idunn-operator-alarm.ps1 @@ -1,7 +1,9 @@ Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" -$bifrostRoot = "E:\Projects\Bifrost" +$repoRoot = Split-Path -Parent $PSScriptRoot +$projectsRoot = Split-Path -Parent $repoRoot +$bifrostRoot = Join-Path $projectsRoot "Bifrost" $publisherScript = Join-Path $bifrostRoot "tools\operator-notification.mjs" if (-not (Test-Path -LiteralPath $publisherScript)) { From 515a864ef0a8e25e9b0b9bc9bd8e3c329b5cb2d0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Sep 2026 01:05:40 +0200 Subject: [PATCH 85/93] Rebase Idunn actuator scripts off the projects root The previous commit fixed the target catalog and the operator alarm, but the actuators the catalog invokes carried the same E: paths. Idunn would have resolved every target and then failed on the first restart or deploy it ran, for exactly the reason the alarm failed. Rebased 41 references across 27 scripts: - .cmd bodies invoke their sibling .ps1 through %~dp0 instead of an absolute drive path, and restart-weksa.cmd reaches its sibling repo the same way. - .ps1 parameter defaults and assignments derive from $PSScriptRoot: one level up for paths inside Odin, two for sibling repos under the projects root. Left deliberately alone: $RemoteViliRoot and $StateRoot in restart-vili.ps1. Both are consumed inside the EncodedCommand block that restart-vili ssh's to raven, so they describe raven's filesystem, not starfire's. Whether raven also moved off E: is an operator question, not something to infer from here. Verified: all 46 scripts under scripts/ parse clean, and the derivation resolves to F:\Projects\Odin and F:\Projects on this host. Co-Authored-By: Claude Opus 5 --- scripts/activate-muninn-raven-av-srt.cmd | 2 +- scripts/deploy-raven-sleipnir.ps1 | 2 +- scripts/deploy-yggdrasil-heimdall.cmd | 2 +- scripts/deploy-yggdrasil-heimdall.ps1 | 10 +++++----- scripts/deploy-yggdrasil-repixelizer.cmd | 2 +- scripts/deploy-yggdrasil-repixelizer.ps1 | 10 +++++----- scripts/deploy-yggdrasil-source-app.ps1 | 2 +- scripts/deploy-yggdrasil-streampixels.cmd | 2 +- scripts/deploy-yggdrasil-streampixels.ps1 | 10 +++++----- scripts/health-idunn-swarm-deployment-coverage.cmd | 2 +- scripts/health-odin.ps1 | 2 +- scripts/health-voidbot.ps1 | 2 +- scripts/restart-muninn.cmd | 2 +- scripts/restart-nightwing-eve-browser-reference.cmd | 2 +- scripts/restart-nightwing-muninn.cmd | 2 +- scripts/restart-odin.cmd | 2 +- scripts/restart-odin.ps1 | 2 +- scripts/restart-raven-capture-mux.cmd | 2 +- scripts/restart-starfire-muninn.cmd | 2 +- scripts/restart-stonks.cmd | 2 +- scripts/restart-stonks.ps1 | 2 +- scripts/restart-vili.ps1 | 4 ++-- scripts/restart-weksa.cmd | 2 +- scripts/start-idunn-local.ps1 | 2 +- scripts/start-odin.ps1 | 4 ++-- scripts/stop-hermodr.ps1 | 2 +- scripts/stop-odin.ps1 | 2 +- 27 files changed, 41 insertions(+), 41 deletions(-) diff --git a/scripts/activate-muninn-raven-av-srt.cmd b/scripts/activate-muninn-raven-av-srt.cmd index badf485..279d605 100644 --- a/scripts/activate-muninn-raven-av-srt.cmd +++ b/scripts/activate-muninn-raven-av-srt.cmd @@ -1,2 +1,2 @@ @echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\activate-muninn-raven-av-srt.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0activate-muninn-raven-av-srt.ps1 diff --git a/scripts/deploy-raven-sleipnir.ps1 b/scripts/deploy-raven-sleipnir.ps1 index c3ba91f..ff1215e 100644 --- a/scripts/deploy-raven-sleipnir.ps1 +++ b/scripts/deploy-raven-sleipnir.ps1 @@ -1,5 +1,5 @@ param( - [string] $LocalSleipnirExe = "E:\Projects\Odin\target\release\sleipnir.exe", + [string] $LocalSleipnirExe = (Join-Path (Split-Path -Parent $PSScriptRoot) "target\release\sleipnir.exe"), [string] $RavenHost = "raven", [string] $RemoteSleipnirExe = "C:\Meta\Odin\Sleipnir\sleipnir.exe", [string] $SshUser = "madman's lullaby", diff --git a/scripts/deploy-yggdrasil-heimdall.cmd b/scripts/deploy-yggdrasil-heimdall.cmd index d7f9027..d15d7b0 100644 --- a/scripts/deploy-yggdrasil-heimdall.cmd +++ b/scripts/deploy-yggdrasil-heimdall.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo This deployment wrapper is an Idunn actuator. Configure Idunn; do not deploy manually. exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\deploy-yggdrasil-heimdall.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0deploy-yggdrasil-heimdall.ps1 diff --git a/scripts/deploy-yggdrasil-heimdall.ps1 b/scripts/deploy-yggdrasil-heimdall.ps1 index fb1815f..f7d46cc 100644 --- a/scripts/deploy-yggdrasil-heimdall.ps1 +++ b/scripts/deploy-yggdrasil-heimdall.ps1 @@ -1,13 +1,13 @@ param( [string] $SshTarget = "yggdrasil", - [string] $RepoRoot = "E:\Projects\Heimdall", - [string] $CultLibRoot = "E:\Projects\CultLib", + [string] $RepoRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "Heimdall"), + [string] $CultLibRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "CultLib"), [string] $AppUser = "heimdall", [string] $RemoteAppHome = "/srv/heimdall", [string] $RemoteTarballName = "heimdall-source.tar", [string] $RemoteCultLibTarballName = "heimdall-cultlib.tar", - [string] $DeployScript = "E:\Projects\gamecult-ops\scripts\deploy-heimdall.sh", - [string] $CheckScript = "E:\Projects\gamecult-ops\scripts\check-heimdall.sh", + [string] $DeployScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\deploy-heimdall.sh"), + [string] $CheckScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\check-heimdall.sh"), [string] $UpstreamRemote = "origin", [string] $UpstreamBranch = "main", [string] $CultLibUpstreamRemote = "origin", @@ -42,7 +42,7 @@ if ([string]::IsNullOrWhiteSpace($cultLibCommit)) { throw "Could not determine CultLib git revision." } -$scratch = Join-Path "E:\Projects\Odin\scratch\idunn-deploy" "yggdrasil-heimdall" +$scratch = Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn-deploy") "yggdrasil-heimdall" $sourceTarPath = Join-Path $scratch $RemoteTarballName $cultLibTarPath = Join-Path $scratch $RemoteCultLibTarballName $manifestPath = Join-Path $scratch "deployment-manifest.txt" diff --git a/scripts/deploy-yggdrasil-repixelizer.cmd b/scripts/deploy-yggdrasil-repixelizer.cmd index b1ede46..ec0edcc 100644 --- a/scripts/deploy-yggdrasil-repixelizer.cmd +++ b/scripts/deploy-yggdrasil-repixelizer.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo This deployment wrapper is an Idunn actuator. Configure Idunn; do not deploy manually. exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\deploy-yggdrasil-repixelizer.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0deploy-yggdrasil-repixelizer.ps1 diff --git a/scripts/deploy-yggdrasil-repixelizer.ps1 b/scripts/deploy-yggdrasil-repixelizer.ps1 index 74d34d6..c5b0b13 100644 --- a/scripts/deploy-yggdrasil-repixelizer.ps1 +++ b/scripts/deploy-yggdrasil-repixelizer.ps1 @@ -1,13 +1,13 @@ param( [string] $SshTarget = "yggdrasil", - [string] $RepoRoot = "E:\Projects\repixelizer", - [string] $CultLibRoot = "E:\Projects\CultLib", + [string] $RepoRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "repixelizer"), + [string] $CultLibRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "CultLib"), [string] $AppUser = "repixelizer", [string] $RemoteAppHome = "/srv/repixelizer", [string] $RemoteTarballName = "repixelizer-source.tar", [string] $RemoteCultLibTarballName = "repixelizer-cultlib.tar", - [string] $DeployScript = "E:\Projects\gamecult-ops\scripts\deploy-repixelizer-gui.sh", - [string] $CheckScript = "E:\Projects\gamecult-ops\scripts\check-repixelizer-gui.sh", + [string] $DeployScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\deploy-repixelizer-gui.sh"), + [string] $CheckScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\check-repixelizer-gui.sh"), [string] $UpstreamRemote = "origin", [string] $UpstreamBranch = "main", [string] $CultLibUpstreamRemote = "origin", @@ -43,7 +43,7 @@ if ([string]::IsNullOrWhiteSpace($cultLibCommit)) { throw "Could not determine CultLib git revision." } -$scratch = Join-Path "E:\Projects\Odin\scratch\idunn-deploy" "yggdrasil-repixelizer" +$scratch = Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn-deploy") "yggdrasil-repixelizer" $sourceTarPath = Join-Path $scratch $RemoteTarballName $cultLibTarPath = Join-Path $scratch $RemoteCultLibTarballName $manifestPath = Join-Path $scratch "deployment-manifest.txt" diff --git a/scripts/deploy-yggdrasil-source-app.ps1 b/scripts/deploy-yggdrasil-source-app.ps1 index fd90401..15408f2 100644 --- a/scripts/deploy-yggdrasil-source-app.ps1 +++ b/scripts/deploy-yggdrasil-source-app.ps1 @@ -42,7 +42,7 @@ if ([string]::IsNullOrWhiteSpace($commit)) { throw "Could not determine git revision for $RepoRoot $sourceRef" } -$scratch = Join-Path "E:\Projects\Odin\scratch\idunn-deploy" $AppId +$scratch = Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn-deploy") $AppId $tarPath = Join-Path $scratch $RemoteTarballName $manifestPath = Join-Path $scratch "deployment-manifest.txt" New-Item -ItemType Directory -Force -Path $scratch | Out-Null diff --git a/scripts/deploy-yggdrasil-streampixels.cmd b/scripts/deploy-yggdrasil-streampixels.cmd index 3ec3cdb..7c05142 100644 --- a/scripts/deploy-yggdrasil-streampixels.cmd +++ b/scripts/deploy-yggdrasil-streampixels.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo This deployment wrapper is an Idunn actuator. Configure Idunn; do not deploy manually. exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\deploy-yggdrasil-streampixels.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0deploy-yggdrasil-streampixels.ps1 diff --git a/scripts/deploy-yggdrasil-streampixels.ps1 b/scripts/deploy-yggdrasil-streampixels.ps1 index b40a12d..d58e8ad 100644 --- a/scripts/deploy-yggdrasil-streampixels.ps1 +++ b/scripts/deploy-yggdrasil-streampixels.ps1 @@ -1,13 +1,13 @@ param( [string] $SshTarget = "yggdrasil", - [string] $RepoRoot = "E:\Projects\StreamPixels", - [string] $CultLibRoot = "E:\Projects\CultLib", + [string] $RepoRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "StreamPixels"), + [string] $CultLibRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "CultLib"), [string] $AppUser = "streampixels", [string] $RemoteAppHome = "/srv/streampixels", [string] $RemoteTarballName = "streampixels-source.tar", [string] $RemoteCultLibTarballName = "streampixels-cultlib.tar", - [string] $DeployScript = "E:\Projects\gamecult-ops\scripts\deploy-streampixels-preview.sh", - [string] $CheckScript = "E:\Projects\gamecult-ops\scripts\check-streampixels-preview.sh", + [string] $DeployScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\deploy-streampixels-preview.sh"), + [string] $CheckScript = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "gamecult-ops\scripts\check-streampixels-preview.sh"), [string] $UpstreamRemote = "origin", [string] $UpstreamBranch = "main", [string] $CultLibUpstreamRemote = "origin", @@ -42,7 +42,7 @@ if ([string]::IsNullOrWhiteSpace($cultLibCommit)) { throw "Could not determine CultLib git revision." } -$scratch = Join-Path "E:\Projects\Odin\scratch\idunn-deploy" "yggdrasil-streampixels" +$scratch = Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn-deploy") "yggdrasil-streampixels" $sourceTarPath = Join-Path $scratch $RemoteTarballName $cultLibTarPath = Join-Path $scratch $RemoteCultLibTarballName $manifestPath = Join-Path $scratch "deployment-manifest.txt" diff --git a/scripts/health-idunn-swarm-deployment-coverage.cmd b/scripts/health-idunn-swarm-deployment-coverage.cmd index 9df2737..5d3ea8c 100644 --- a/scripts/health-idunn-swarm-deployment-coverage.cmd +++ b/scripts/health-idunn-swarm-deployment-coverage.cmd @@ -1,2 +1,2 @@ @echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\health-idunn-swarm-deployment-coverage.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0health-idunn-swarm-deployment-coverage.ps1 diff --git a/scripts/health-odin.ps1 b/scripts/health-odin.ps1 index 02a53e6..1d26b09 100644 --- a/scripts/health-odin.ps1 +++ b/scripts/health-odin.ps1 @@ -1,5 +1,5 @@ param( - [string] $StateDir = "E:\Projects\Odin\scratch\odin", + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\odin"), [int] $MaxSilenceSeconds = 120, [string] $CultNetRudpBind = "0.0.0.0:17871" ) diff --git a/scripts/health-voidbot.ps1 b/scripts/health-voidbot.ps1 index 6f6b28a..cc3196a 100644 --- a/scripts/health-voidbot.ps1 +++ b/scripts/health-voidbot.ps1 @@ -1,5 +1,5 @@ param( - [string] $VoidBotRoot = "E:\Projects\VoidBot", + [string] $VoidBotRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "VoidBot"), [int] $MaxOrchestratorAgeSeconds = 180, [int] $MaxCultMeshStoreAgeSeconds = 180 ) diff --git a/scripts/restart-muninn.cmd b/scripts/restart-muninn.cmd index 8e0b018..4785b68 100644 --- a/scripts/restart-muninn.cmd +++ b/scripts/restart-muninn.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-muninn.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-muninn.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-muninn.ps1 diff --git a/scripts/restart-nightwing-eve-browser-reference.cmd b/scripts/restart-nightwing-eve-browser-reference.cmd index 33c36d7..fcf2b2b 100644 --- a/scripts/restart-nightwing-eve-browser-reference.cmd +++ b/scripts/restart-nightwing-eve-browser-reference.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-nightwing-eve-browser-reference.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-nightwing-eve-browser-reference.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-nightwing-eve-browser-reference.ps1 diff --git a/scripts/restart-nightwing-muninn.cmd b/scripts/restart-nightwing-muninn.cmd index f98ca46..59823d4 100644 --- a/scripts/restart-nightwing-muninn.cmd +++ b/scripts/restart-nightwing-muninn.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-nightwing-muninn.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-nightwing-muninn.ps1 -DiscoverMoveState -ClaimUsbMoves +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-nightwing-muninn.ps1 -DiscoverMoveState -ClaimUsbMoves diff --git a/scripts/restart-odin.cmd b/scripts/restart-odin.cmd index 28a1c3b..3b5534c 100644 --- a/scripts/restart-odin.cmd +++ b/scripts/restart-odin.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-odin.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-odin.ps1 +"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-odin.ps1 diff --git a/scripts/restart-odin.ps1 b/scripts/restart-odin.ps1 index a285bab..334977b 100644 --- a/scripts/restart-odin.ps1 +++ b/scripts/restart-odin.ps1 @@ -1,5 +1,5 @@ param( - [string] $StateDir = "E:\Projects\Odin\scratch\odin", + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\odin"), [string] $CultNetRudpBind = "0.0.0.0:17871", [string] $IdunnRudpHealth = $(if ($env:ODIN_IDUNN_RUDP_HEALTH) { $env:ODIN_IDUNN_RUDP_HEALTH } else { $env:IDUNN_RUDP_HEALTH }), [string] $IdunnDaemon = "odin", diff --git a/scripts/restart-raven-capture-mux.cmd b/scripts/restart-raven-capture-mux.cmd index 5bba9ff..50520b1 100644 --- a/scripts/restart-raven-capture-mux.cmd +++ b/scripts/restart-raven-capture-mux.cmd @@ -1,2 +1,2 @@ @echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-muninn.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-muninn.ps1 diff --git a/scripts/restart-starfire-muninn.cmd b/scripts/restart-starfire-muninn.cmd index 76f1929..fa78a28 100644 --- a/scripts/restart-starfire-muninn.cmd +++ b/scripts/restart-starfire-muninn.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-starfire-muninn.cmd is an Idunn actuator wrapper. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-starfire-muninn.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-starfire-muninn.ps1 diff --git a/scripts/restart-stonks.cmd b/scripts/restart-stonks.cmd index 2b459d5..fd9ff6c 100644 --- a/scripts/restart-stonks.cmd +++ b/scripts/restart-stonks.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-stonks.cmd is an Idunn actuator body. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\restart-stonks.ps1 +powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0restart-stonks.ps1 diff --git a/scripts/restart-stonks.ps1 b/scripts/restart-stonks.ps1 index c3f5f0e..3366720 100644 --- a/scripts/restart-stonks.ps1 +++ b/scripts/restart-stonks.ps1 @@ -1,6 +1,6 @@ $ErrorActionPreference = "Stop" -$stonksRoot = "E:\Projects\Stonks" +$stonksRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "Stonks") $stateDir = Join-Path $stonksRoot "scratch\stonks" $pidPath = Join-Path $stateDir "stonks.pid" $startScript = Join-Path $stonksRoot "scripts\start-stonks.ps1" diff --git a/scripts/restart-vili.ps1 b/scripts/restart-vili.ps1 index 060e5de..92a8715 100644 --- a/scripts/restart-vili.ps1 +++ b/scripts/restart-vili.ps1 @@ -1,7 +1,7 @@ param( [string] $RavenHost = "raven", - [string] $LocalViliRoot = "E:\Projects\Vili", - [string] $LocalCultLibRoot = "E:\Projects\CultLib", + [string] $LocalViliRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "Vili"), + [string] $LocalCultLibRoot = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "CultLib"), [string] $RemoteViliRoot = "E:\Projects\Vili", [int] $Port = 8824, [string] $HostName = "0.0.0.0", diff --git a/scripts/restart-weksa.cmd b/scripts/restart-weksa.cmd index 382d4b6..8ddbf57 100644 --- a/scripts/restart-weksa.cmd +++ b/scripts/restart-weksa.cmd @@ -7,4 +7,4 @@ if /I not "%IDUNN_COMMAND_AUTHORITY%"=="idunn-daemon" ( echo restart-weksa.cmd is an Idunn actuator body. Redeploy by poking Idunn; direct service restart is not an owned path. 1>&2 exit /b 1 ) -call E:\Projects\weksa\scripts\restart-weksa-daemon.cmd +call %~dp0..\..\weksa\scripts\restart-weksa-daemon.cmd diff --git a/scripts/start-idunn-local.ps1 b/scripts/start-idunn-local.ps1 index adf75fd..acb34b9 100644 --- a/scripts/start-idunn-local.ps1 +++ b/scripts/start-idunn-local.ps1 @@ -1,6 +1,6 @@ param( [int] $StaleAfterSeconds = 120, - [string] $StateDir = "E:\Projects\Odin\scratch\idunn" + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn") ) $ErrorActionPreference = "Stop" diff --git a/scripts/start-odin.ps1 b/scripts/start-odin.ps1 index a24d235..47e0751 100644 --- a/scripts/start-odin.ps1 +++ b/scripts/start-odin.ps1 @@ -1,5 +1,5 @@ param( - [string] $StateDir = "E:\Projects\Odin\scratch\odin", + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\odin"), [string] $CultNetRudpBind = "0.0.0.0:17871", [string] $IdunnRudpHealth = $(if ($env:ODIN_IDUNN_RUDP_HEALTH) { $env:ODIN_IDUNN_RUDP_HEALTH } else { $env:IDUNN_RUDP_HEALTH }), [string] $IdunnDaemon = "odin", @@ -39,7 +39,7 @@ if (Test-Path $pidPath) { Remove-Item -LiteralPath $pidPath -Force } -$env:NODE_PATH = "E:\Projects\CultLib\packages" +$env:NODE_PATH = (Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) "CultLib\packages") $args = @( $scriptPath, "--stateDir", $StateDir, diff --git a/scripts/stop-hermodr.ps1 b/scripts/stop-hermodr.ps1 index 692db2d..849e71e 100644 --- a/scripts/stop-hermodr.ps1 +++ b/scripts/stop-hermodr.ps1 @@ -1,5 +1,5 @@ param( - [string] $StateDir = "E:\Projects\Odin\scratch\Hermodr" + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\Hermodr") ) $ErrorActionPreference = "Stop" diff --git a/scripts/stop-odin.ps1 b/scripts/stop-odin.ps1 index cca0a5f..cfbb412 100644 --- a/scripts/stop-odin.ps1 +++ b/scripts/stop-odin.ps1 @@ -1,5 +1,5 @@ param( - [string] $StateDir = "E:\Projects\Odin\scratch\odin" + [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\odin") ) $ErrorActionPreference = "Stop" From 27cc8156bcfbbe9d581fb8b65725c49ca44ec098 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Sep 2026 14:54:58 +0200 Subject: [PATCH 86/93] Consume CultCache and CultNet from CultLib instead of forking them The CultNet submodule and the in-tree CultCache copy are gone. Both now come from CultLib, and vendor/cultmesh-rs is the only fork left -- it too consumes CultLib, so exactly one version of each crate spans the graph. That mattered: mixing sources would have put two cultnet-rs crates in one build, and the types would not have unified. The in-flight CultMesh RUDP document server comes with it because it is a prerequisite, not a coincidence. The previously committed CultMesh calls assume_connected(), a CultNet method Odin's fork carried and CultLib does not, so the old code cannot build against CultLib at all. The newer code no longer calls it. Verified by reverting the file and watching it fail. Three API differences needed adapting: CultLib's transport options carry a reconnect_policy the fork lacked (7 sites), its create_ack builds the packet straight from ack state and so cannot fail, and its transport profile owns each channel's reliable expiry. Everything but odin-daemon builds; odin-daemon is Unix-only and does not compile on this machine either way. muninn and sleipnir pass 162 tests. The one failure, rudp_media_transport_options_follow_low_latency_profile, expects an Unreliable media channel while every CultNet lineage including both revisions Odin had pinned declares it Reliable, so it failed before this change too. The in-flight idunn-daemon work is deliberately left uncommitted; the committed tree was verified to build without it. Also left alone: the untracked odin-daemon crate and deployment recipe, and the root manifest, whose now-dead vendor excludes are inert. That recipe still requires the vendor/cultnet-rs gitlink and will refuse admission until that line is dropped -- the fix is in the working tree, waiting on that directory being tracked. Co-Authored-By: Claude Opus 5 --- .gitmodules | 3 - Cargo.lock | 30 +- crates/idunn-daemon/Cargo.toml | 4 +- crates/muninn-daemon/Cargo.toml | 2 +- crates/muninn-daemon/src/main.rs | 9 +- crates/odin-core/Cargo.toml | 4 +- crates/sleipnir-daemon/Cargo.toml | 4 +- crates/sleipnir-daemon/src/main.rs | 2 + src/Idunn/README.md | 2 +- vendor/cultcache-rs/.gitignore | 2 - vendor/cultcache-rs/Cargo.toml | 30 - vendor/cultcache-rs/README.md | 332 ----- .../cultcache-rs-derive/Cargo.toml | 13 - .../cultcache-rs-derive/src/lib.rs | 278 ---- .../examples/cultcache_interop.rs | 121 -- vendor/cultcache-rs/src/lib.rs | 1241 ----------------- vendor/cultmesh-rs/Cargo.lock | 149 +- vendor/cultmesh-rs/Cargo.toml | 4 +- vendor/cultmesh-rs/src/lib.rs | 572 ++++++-- .../cultmesh-rs/src/rudp_document_server.rs | 647 +++++++++ .../cultmesh-rs/tests/rudp_document_server.rs | 553 ++++++++ vendor/cultnet-rs | 1 - 22 files changed, 1818 insertions(+), 2185 deletions(-) delete mode 100644 .gitmodules delete mode 100644 vendor/cultcache-rs/.gitignore delete mode 100644 vendor/cultcache-rs/Cargo.toml delete mode 100644 vendor/cultcache-rs/README.md delete mode 100644 vendor/cultcache-rs/cultcache-rs-derive/Cargo.toml delete mode 100644 vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs delete mode 100644 vendor/cultcache-rs/examples/cultcache_interop.rs delete mode 100644 vendor/cultcache-rs/src/lib.rs create mode 100644 vendor/cultmesh-rs/src/rudp_document_server.rs create mode 100644 vendor/cultmesh-rs/tests/rudp_document_server.rs delete mode 160000 vendor/cultnet-rs diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index efa780e..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "vendor/cultnet-rs"] - path = vendor/cultnet-rs - url = https://github.com/GameCult/cultnet-rs.git diff --git a/Cargo.lock b/Cargo.lock index 5606f06..9aa3bfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,22 +175,25 @@ dependencies = [ [[package]] name = "cultcache-rs" version = "0.1.0" +source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" dependencies = [ "anyhow", "chrono", "cultcache-rs-derive", "fs2", + "redb", "rmp-serde", "serde", "serde_bytes", "serde_json", "uuid", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] name = "cultcache-rs-derive" version = "0.1.0" +source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" dependencies = [ "proc-macro2", "quote", @@ -210,6 +213,7 @@ dependencies = [ [[package]] name = "cultnet-rs" version = "0.1.0" +source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" dependencies = [ "aes-gcm", "anyhow", @@ -638,6 +642,21 @@ dependencies = [ "tempfile", ] +[[package]] +name = "odin-daemon" +version = "0.1.0" +dependencies = [ + "anyhow", + "chrono", + "cultcache-rs", + "cultmesh-rs", + "cultnet-rs", + "fs2", + "rmp-serde", + "serde", + "tempfile", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -775,6 +794,15 @@ dependencies = [ "getrandom 0.3.4", ] +[[package]] +name = "redb" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de6c3b63e007e90ce536ec2ae4690826136a20ec8dbbbb400daef1bb999d2e36" +dependencies = [ + "libc", +] + [[package]] name = "rmp" version = "0.8.15" diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index 4288137..e37dca1 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -20,8 +20,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultcache-rs = { path = "../../vendor/cultcache-rs" } -cultnet-rs = { path = "../../vendor/cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } odin-core = { path = "../odin-core" } rmp-serde = "1.3" sha2 = "0.10" diff --git a/crates/muninn-daemon/Cargo.toml b/crates/muninn-daemon/Cargo.toml index 6a88bfa..eca171c 100644 --- a/crates/muninn-daemon/Cargo.toml +++ b/crates/muninn-daemon/Cargo.toml @@ -16,7 +16,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { path = "../../vendor/cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } libc = "0.2" muninn-move-tracker = { path = "../muninn-move-tracker" } muninn-psmoveapi-tracker = { path = "../muninn-psmoveapi-tracker", optional = true } diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index ac29ce7..11e422a 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -7,7 +7,7 @@ use crate::media_packetizer::{ }; use anyhow::{Context, Result, anyhow}; use cultmesh_rs::{ - CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, CultMesh, CultMeshNodeOptions, + CultMesh, CultMeshNodeOptions, CultMeshRudpDocumentPublishOptions, CultMeshRudpSnapshotOptions, CultMeshSharedMemoryFrameRing, CultMeshStreamBodyTransport, CultMeshStreamCatalog, CultMeshStreamClock, CultMeshStreamDescriptor, CultMeshStreamKind, @@ -688,6 +688,7 @@ fn run_provider_command_ingress( max_fragment_bytes: Some(1200), max_pending_reliable_packets: Some(64), media_reliable_expire_after_ms: None, + reconnect_policy: None, }, )?; loop { @@ -832,7 +833,6 @@ fn pull_odin_obs_catalog_snapshot(node: &mut cultmesh_rs::CultMeshNode, options: connect_timeout: Duration::from_millis(2000), response_timeout: Duration::from_millis(5000), resend_delay_ms: 15, - connection_id: CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, ..CultMeshRudpSnapshotOptions::default() }) { eprintln!("Muninn could not pull OBS catalog from Odin: {error:#}"); @@ -5475,6 +5475,7 @@ fn run_hid_controller_rudp_ingress( max_fragment_bytes: Some(HID_CONTROLLER_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), + reconnect_policy: None, })?; let mut sources = live_move_state_sources(&options) .into_iter() @@ -6019,6 +6020,7 @@ fn create_hid_controller_stream(options: &Options) -> Result Result<()> { "odin-media-receiver-route".to_string(), "muninn.media-rudp".to_string(), ], - connection_id: CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, ..CultMeshRudpDocumentPublishOptions::default() }, ) @@ -8975,7 +8976,6 @@ fn publish_capture_command_to_odin( "odin-cultmesh-command-route".to_string(), "muninn.capture-stream".to_string(), ], - connection_id: CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, ..CultMeshRudpDocumentPublishOptions::default() }, ) @@ -12002,6 +12002,7 @@ mod tests { max_fragment_bytes: Some(1200), max_pending_reliable_packets: Some(64), media_reliable_expire_after_ms: None, + reconnect_policy: None, }, ) .unwrap(); diff --git a/crates/odin-core/Cargo.toml b/crates/odin-core/Cargo.toml index 9e8d08c..5752a4d 100644 --- a/crates/odin-core/Cargo.toml +++ b/crates/odin-core/Cargo.toml @@ -11,10 +11,10 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { path = "../../vendor/cultcache-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } cultmesh-rs = { path = "../../vendor/cultmesh-rs" } serde_json = "1" -cultnet-rs = { path = "../../vendor/cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] diff --git a/crates/sleipnir-daemon/Cargo.toml b/crates/sleipnir-daemon/Cargo.toml index 89294ea..f08651a 100644 --- a/crates/sleipnir-daemon/Cargo.toml +++ b/crates/sleipnir-daemon/Cargo.toml @@ -12,8 +12,8 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { path = "../../vendor/cultnet-rs" } -cultcache-rs = { path = "../../vendor/cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } odin-core = { path = "../odin-core" } rmp-serde = "1.3" serde_json = "1" diff --git a/crates/sleipnir-daemon/src/main.rs b/crates/sleipnir-daemon/src/main.rs index 1f4189f..29be8d3 100644 --- a/crates/sleipnir-daemon/src/main.rs +++ b/crates/sleipnir-daemon/src/main.rs @@ -2227,6 +2227,7 @@ fn create_rudp_stream( max_fragment_bytes: Some(HID_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), + reconnect_policy: None, })?; transport.connect(Vec::new())?; return Ok(Some(ActiveRudpStream { @@ -2267,6 +2268,7 @@ fn create_rudp_stream( max_fragment_bytes: Some(HID_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), + reconnect_policy: None, })?, target: None, last_connect_attempt_at: None, diff --git a/src/Idunn/README.md b/src/Idunn/README.md index b404cf8..5f09e8e 100644 --- a/src/Idunn/README.md +++ b/src/Idunn/README.md @@ -299,7 +299,7 @@ From here on out, the work is not "teach another daemon to speak." It is "delete the witness masks and keep ownership where it belongs." Rust no longer gets to claim the transport is imaginary: -`vendor/cultnet-rs` includes the canonical CNR0 `cultnet.transport.rudp.v0` +CultLib's `cultnet-rs` carries the canonical CNR0 `cultnet.transport.rudp.v0` session path for acknowledged CultNet messages over UDP. That is substrate only. Idunn still marks daemon profiles as `migration-required` until each daemon actually publishes health and command diff --git a/vendor/cultcache-rs/.gitignore b/vendor/cultcache-rs/.gitignore deleted file mode 100644 index 96ef6c0..0000000 --- a/vendor/cultcache-rs/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -Cargo.lock diff --git a/vendor/cultcache-rs/Cargo.toml b/vendor/cultcache-rs/Cargo.toml deleted file mode 100644 index 17ba246..0000000 --- a/vendor/cultcache-rs/Cargo.toml +++ /dev/null @@ -1,30 +0,0 @@ -[package] -name = "cultcache-rs" -version = "0.1.0" -edition = "2024" -publish = false - -[workspace] -members = ["cultcache-rs-derive"] - -[lib] -name = "cultcache_rs" -path = "src/lib.rs" - -[dependencies] -anyhow = "1" -chrono = { version = "0.4", features = ["serde"] } -cultcache-rs-derive = { path = "cultcache-rs-derive" } -fs2 = "0.4" -rmp-serde = "1.3" -serde = { version = "1", features = ["derive"] } -serde_bytes = "0.11" -serde_json = "1" -uuid = { version = "1", features = ["v4"] } - -[target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.61", features = ["Win32_Storage_FileSystem"] } - -[dev-dependencies] -pretty_assertions = "1.4" -tempfile = "3" diff --git a/vendor/cultcache-rs/README.md b/vendor/cultcache-rs/README.md deleted file mode 100644 index b21c324..0000000 --- a/vendor/cultcache-rs/README.md +++ /dev/null @@ -1,332 +0,0 @@ -# CultCache Rust - -`cultcache-rs` is the Rust port of the useful part of GameCult's CultCache: -a polymorphism-aware in-memory cache that remembers through pluggable backing -stores. - -Consumer code should think in domain types: - -```rust -let player = cache.get_required::("player:meta")?; -cache.put("player:meta", &player)?; -``` - -The persistence layer should deal with envelopes, routing, MessagePack payload -bytes, and schema identity. Application code should not paw at loose JSON files -like a sad little bureaucrat with a clipboard. - -## What It Is - -CultCache is a domain cache with persistence adapters. - -- `CultCache` is the query and mutation surface. -- Domain structs implement `DatabaseEntry`. -- Entries are stored behind a `type::key` identity, so multiple entry types - can share a logical key without colliding. -- Each payload is encoded directly from the known `DatabaseEntry` type into - MessagePack bytes. -- Backing stores are adapters, not the public data model. -- Writes persist to the resolved backing store before the in-memory cache is - updated. -- Type-specific backing stores beat generic backing stores. -- `SingleFileMessagePackBackingStore` is the first concrete store and guards - file access with a sidecar lock file. - -This is not an ORM, not a database, and not distributed consensus in a novelty -hat. If multiple processes write the same backing file, use an external lock or -a coordinator. - -## Current API - -```rust -use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore}; - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "settings")] -struct Settings { - #[cultcache(key = 0)] - theme: String, - - #[cultcache(key = 1, default)] - retries: u32, -} - -let mut cache = CultCache::new(); -cache.register_entry_type::()?; -cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.cc")); -cache.pull_all_backing_stores()?; - -cache.put("app", &Settings { - theme: "ash".to_string(), - retries: 3, -})?; - -let settings = cache.get_required::("app")?; -# Ok::<(), anyhow::Error>(()) -``` - -For a closed set of entries, generate a registry instead of repeating yourself: - -```rust -use cultcache_rs::cultcache_registry; - -cultcache_registry!(GameCultEntries { - Settings, - PlayerData, -}); - -let mut cache = CultCache::new(); -cache.register_registry(GameCultEntries)?; -``` - -The important part is that callers retrieve domain values directly: - -```rust -let settings: Settings = cache.get_required("app")?; -let all_settings: Vec = cache.get_all()?; -``` - -The cache is the ergonomic surface. The backing store is the memory prosthetic. - -## How This Maps From Original CultCache - -The original C# CultCache relies on runtime reflection: - -- cacheable models inherit from `DatabaseEntry` -- the cache scans child classes at startup -- `Get(id)` returns a `T` -- optional `INamedEntry` enables name lookup -- optional field/property indexes are registered by member name -- backing stores pull/push/delete `DatabaseEntry` values - -Rust does not have C#-style assembly scanning or runtime subclass discovery. -The closest honest Rust translation is: - -- domain structs implement a marker trait, `DatabaseEntry` -- the derive macro generates a positional array formatter from explicit integer - field slots -- the envelope carries the polymorphic type discriminator -- unknown persisted type ids fail closed instead of constructing arbitrary - runtime types -- a generated registry should eventually replace repetitive manual - registration - -Manual `register_entry_type::()` is still accepted, but entry identity no -longer needs a hand-written impl: `#[derive(DatabaseEntry)]` declares it beside -the domain type. `cultcache_registry!` provides the current generated resolver -surface for a closed entry set. - -## Intended Rust Ergonomics - -The target user-facing shape should be: - -```rust -#[derive(Clone, DatabaseEntry)] -#[cultcache(type = "player")] -pub struct PlayerData { - #[cultcache(key = 0)] - pub id: String, - - #[cultcache(key = 1)] - pub name: String, - - #[cultcache(key = 2)] - pub faction: String, -} - -let mut cache = CultCache::builder() - .with_generated_entries(game_entries()) - .with_generic_store(SingleFileMessagePackBackingStore::new("cache.cc")) - .build()?; - -cache.pull_all_backing_stores()?; -let player = cache.get_required::("player:ari")?; -``` - -The current `cultcache_registry!` macro is deliberately simple. The remaining -ergonomic endpoint is automatic inventory or build-script generation so users -do not have to maintain the registry list by hand. - -The registry is the Rust equivalent of C# reflection and MessagePack resolver -setup. It should know: - -- entry type id -- generated MessagePack/serde formatter path -- Rust type name / schema name -- optional store domain route -- optional name key extractor -- optional secondary indexes -- optional global singleton marker - -In other words: code generation should restore the original CultCache feeling -without pretending Rust has runtime reflection hiding under the floorboards. - -## Current Surface - -- `CultCache::new` -- `register_entry_type::` -- `add_generic_backing_store` -- `add_backing_store` -- `pull_all_backing_stores` -- `get::` -- `get_required::` -- `get_envelope::` -- `get_required_envelope::` -- `get_all::` -- `put::` -- `put_envelope::` -- `update::` -- `delete::` -- `snapshot` -- `SingleFileMessagePackBackingStore` - -## Backing Store Routing - -Generic store: - -```rust -cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.cc")); -``` - -Type-specific store: - -```rust -cache.add_backing_store( - SingleFileMessagePackBackingStore::new("players.cc"), - ["player"], -); -``` - -When writing a `PlayerData`, the cache checks type-specific stores first. If none -match, it writes to the first generic store. Later matching stores are mirrors. - -This mirrors the C# behavior: - -- specific domain stores own their domain -- the first generic store is the primary generic write target -- later generic stores mirror writes -- this is not multi-master - -## Persistence Semantics - -`put` persists before mutating the in-memory cache. If persistence fails, the -cache does not pretend the write succeeded. - -The envelope is MessagePack, and the payload inside each envelope is also raw -MessagePack bytes encoded from the registered concrete `DatabaseEntry` type. -That avoids the old bootstrap path where payloads were normalized through -`serde_json::Value`. - -For bit-compatible neighbors, that also enables a real fast lane: - -- `get_envelope::()` exports the canonical persisted bytes for a typed entry -- `put_envelope::()` ingests the same envelope into another cache instance - without re-encoding the payload first - -It still decodes once for validation and typed reads. We are not pretending one -`Vec` became metaphysically zero-copy because we believed in it harder. But -the stupid decode/re-encode loop is gone. - -The `DatabaseEntry` derive does not trust Rust source field order as the durable -schema. Every persisted member must declare a stable integer slot: - -```rust -#[derive(Clone, DatabaseEntry)] -#[cultcache(type = "player")] -pub struct PlayerData { - #[cultcache(key = 0)] - pub id: String, - - #[cultcache(key = 1)] - pub name: String, - - #[cultcache(key = 2, default)] - pub level: u32, -} -``` - -The derive emits a tuple/array formatter. Gaps serialize as nil. Missing fields -marked `default` use `Default::default()` when older payloads are read. Deleted -field slots should stay reserved until an explicit store migration rewrites the -data. - -The single-file MessagePack store rewrites an atomic snapshot. That is a sane -starting point for small typed state surfaces, settings, Epiphany agent memory, -heartbeat state, and other compact control-plane data. Large corpora should use -a sharded store or a real database instead of asking one file to become a -warehouse and then acting wounded when physics invoices us. - -The single-file store uses a sidecar lock file for shared reads and exclusive -writes. That protects ordinary multi-process access to the same file. It is -still not a multi-master replication protocol, and it is not a substitute for a -coordinator when higher-level write ordering matters. - -## Near-Term Ergonomic Improvements - -1. **Derive macro** - - `#[derive(DatabaseEntry)]` - - `#[cultcache(type = "...")]` - - `#[cultcache(key = N)]` on every persisted field - - tuple/array formatter generation landed - - optional `#[cultcache(name)]`, `#[cultcache(index)]`, and - `#[cultcache(global)]` are still future work - -2. **Generated registry** - - a `CultCacheRegistry` trait - - `cache.register_registry(GameCultEntries)` - - basic macro registry landed - - automatic inventory/build-script generation and store routes are future work - -3. **Name and index lookups** - - `cache.get_by_name::("Potion")` - - `cache.get_by_index::("faction", "Lucent")` - - generated extractors instead of reflection strings - -4. **Schema/version metadata** - - schema version in the envelope or payload metadata - - explicit migration hooks - - refusal on unknown persisted entry types unless a migration/resolver is - installed - -5. **Projection helpers** - - JSON projection for review and git diffs - - vector stripping / large-field elision for agent memory surfaces - -## Why Not Skip Registration Immediately? - -Rust can only deserialize polymorphic data into concrete types if something -maps the persisted `type` discriminator to the Rust type. In C#, reflection and -MessagePack resolvers can discover a closed `DatabaseEntry` inheritance tree at -runtime and route it through known formatters. In Rust, that map has to come from -somewhere: - -- explicit registration -- a generated registry -- a macro inventory crate -- a hand-written resolver - -Explicit registration is the simplest honest implementation. Generated registry -is the ergonomic destination. Hand-written resolver tables are the punishment -we give ourselves if we get lazy. - -## Security Model - -CultCache should never parse arbitrary structured data into arbitrary runtime -types. It only accepts persisted envelopes whose `type` discriminator resolves -to a known `DatabaseEntry` type registered in the cache or generated registry. - -That is the Rust version of the original trick: - -- C# refuses to store objects outside the `DatabaseEntry` root type. -- Runtime-visible `DatabaseEntry` subclasses get dedicated formatters. -- Unknown or unregistered types are rejected. -- MessagePack payload bytes deserialize through known concrete formatters, not - an open-world type loader or generic structured-value bridge. - -The derive/registry work should preserve that shape. Its job is to remove -manual registration ceremony, not to loosen the closed-world boundary. - -## License - -Private GameCult infrastructure for now. Public packaging can wait until the -API grows enough teeth to deserve strangers. diff --git a/vendor/cultcache-rs/cultcache-rs-derive/Cargo.toml b/vendor/cultcache-rs/cultcache-rs-derive/Cargo.toml deleted file mode 100644 index 74d52a4..0000000 --- a/vendor/cultcache-rs/cultcache-rs-derive/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "cultcache-rs-derive" -version = "0.1.0" -edition = "2024" -publish = false - -[lib] -proc-macro = true - -[dependencies] -proc-macro2 = "1" -quote = "1" -syn = { version = "2", features = ["derive", "parsing"] } diff --git a/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs b/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs deleted file mode 100644 index 7d61319..0000000 --- a/vendor/cultcache-rs/cultcache-rs-derive/src/lib.rs +++ /dev/null @@ -1,278 +0,0 @@ -use proc_macro::TokenStream; -use quote::format_ident; -use quote::quote; -use syn::Data; -use syn::DeriveInput; -use syn::Fields; -use syn::LitInt; -use syn::LitStr; -use syn::Type; -use syn::parse_macro_input; - -#[proc_macro_derive(DatabaseEntry, attributes(cultcache))] -pub fn derive_database_entry(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - let ident = input.ident.clone(); - let mut entry_type: Option = None; - let mut schema_name: Option = None; - - for attribute in input - .attrs - .iter() - .filter(|attr| attr.path().is_ident("cultcache")) - { - let parse_result = attribute.parse_nested_meta(|meta| { - if meta.path.is_ident("type") { - entry_type = Some(meta.value()?.parse()?); - Ok(()) - } else if meta.path.is_ident("schema") { - schema_name = Some(meta.value()?.parse()?); - Ok(()) - } else { - Err(meta.error("unsupported cultcache attribute")) - } - }); - if let Err(error) = parse_result { - return error.to_compile_error().into(); - } - } - - let Some(entry_type) = entry_type else { - return syn::Error::new_spanned( - ident, - "DatabaseEntry derive requires #[cultcache(type = \"...\")]", - ) - .to_compile_error() - .into(); - }; - let schema_name = schema_name.unwrap_or_else(|| LitStr::new(&ident.to_string(), ident.span())); - - let fields = match input.data { - Data::Struct(data) => match data.fields { - Fields::Named(fields) => fields.named, - _ => { - return syn::Error::new_spanned( - ident, - "DatabaseEntry derive requires a named struct", - ) - .to_compile_error() - .into(); - } - }, - _ => { - return syn::Error::new_spanned(ident, "DatabaseEntry derive requires a struct") - .to_compile_error() - .into(); - } - }; - - let mut slots: Vec = Vec::new(); - for field in fields { - let Some(field_ident) = field.ident else { - return syn::Error::new_spanned(field.ty, "DatabaseEntry derive requires named fields") - .to_compile_error() - .into(); - }; - let mut key: Option = None; - let mut default = false; - let mut bytes = false; - for attribute in field - .attrs - .iter() - .filter(|attr| attr.path().is_ident("cultcache")) - { - let parse_result = attribute.parse_nested_meta(|meta| { - if meta.path.is_ident("key") { - let value: LitInt = meta.value()?.parse()?; - key = Some(value.base10_parse()?); - Ok(()) - } else if meta.path.is_ident("default") { - default = true; - Ok(()) - } else if meta.path.is_ident("bytes") { - bytes = true; - Ok(()) - } else { - Err(meta.error("unsupported field cultcache attribute")) - } - }); - if let Err(error) = parse_result { - return error.to_compile_error().into(); - } - } - let Some(key) = key else { - return syn::Error::new_spanned( - field_ident, - "DatabaseEntry fields require #[cultcache(key = N)]", - ) - .to_compile_error() - .into(); - }; - if slots.iter().any(|slot| slot.key == key) { - return syn::Error::new_spanned( - field_ident, - format!("duplicate DatabaseEntry slot {key}"), - ) - .to_compile_error() - .into(); - } - slots.push(CultCacheSlot { - key, - ident: field_ident, - ty: field.ty, - default, - bytes, - }); - } - slots.sort_by_key(|slot| slot.key); - let max_slot = slots.iter().map(|slot| slot.key).max().unwrap_or(0); - - let serialize_elements = (0..=max_slot).map(|slot| { - if let Some(field) = slots.iter().find(|field| field.key == slot) { - let field_ident = &field.ident; - if field.bytes { - quote! { - tuple.serialize_element(::serde_bytes::Bytes::new(&self.#field_ident))?; - } - } else { - quote! { - tuple.serialize_element(&self.#field_ident)?; - } - } - } else { - quote! { - tuple.serialize_element(&())?; - } - } - }); - - let option_declarations = slots.iter().map(|field| { - let variable = format_ident!("slot_{}", field.key); - let ty = &field.ty; - if field.bytes { - quote! { - let mut #variable: ::std::option::Option<::serde_bytes::ByteBuf> = ::std::option::Option::None; - } - } else { - quote! { - let mut #variable: ::std::option::Option<#ty> = ::std::option::Option::None; - } - } - }); - - let deserialize_match_arms = (0..=max_slot).map(|slot| { - if let Some(field) = slots.iter().find(|field| field.key == slot) { - let variable = format_ident!("slot_{}", field.key); - let ty = &field.ty; - if field.bytes { - quote! { - #slot => { - #variable = seq.next_element::<::serde_bytes::ByteBuf>()?; - } - } - } else { - quote! { - #slot => { - #variable = seq.next_element::<#ty>()?; - } - } - } - } else { - quote! { - #slot => { - let _ = seq.next_element::<::serde::de::IgnoredAny>()?; - } - } - } - }); - - let build_fields = slots.iter().map(|field| { - let field_ident = &field.ident; - let variable = format_ident!("slot_{}", field.key); - if field.bytes && field.default { - quote! { - #field_ident: #variable.unwrap_or_default().into_vec() - } - } else if field.bytes { - let field_name = field_ident.to_string(); - quote! { - #field_ident: #variable.ok_or_else(|| ::serde::de::Error::missing_field(#field_name))?.into_vec() - } - } else if field.default { - quote! { - #field_ident: #variable.unwrap_or_default() - } - } else { - let field_name = field_ident.to_string(); - quote! { - #field_ident: #variable.ok_or_else(|| ::serde::de::Error::missing_field(#field_name))? - } - } - }); - - quote! { - impl ::cultcache_rs::DatabaseEntry for #ident { - const TYPE: &'static str = #entry_type; - const SCHEMA_NAME: &'static str = #schema_name; - } - - impl ::serde::Serialize for #ident { - fn serialize(&self, serializer: S) -> ::std::result::Result - where - S: ::serde::Serializer, - { - use ::serde::ser::SerializeTuple; - let mut tuple = serializer.serialize_tuple(#max_slot + 1)?; - #(#serialize_elements)* - tuple.end() - } - } - - impl<'de> ::serde::Deserialize<'de> for #ident { - fn deserialize(deserializer: D) -> ::std::result::Result - where - D: ::serde::Deserializer<'de>, - { - struct CultCacheVisitor; - - impl<'de> ::serde::de::Visitor<'de> for CultCacheVisitor { - type Value = #ident; - - fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - formatter.write_str(concat!("a CultCache array for ", stringify!(#ident))) - } - - fn visit_seq(self, mut seq: A) -> ::std::result::Result - where - A: ::serde::de::SeqAccess<'de>, - { - #(#option_declarations)* - let mut index = 0usize; - while index <= #max_slot { - match index { - #(#deserialize_match_arms)* - _ => unreachable!(), - } - index += 1; - } - while let ::std::option::Option::Some(_) = seq.next_element::<::serde::de::IgnoredAny>()? {} - Ok(#ident { - #(#build_fields),* - }) - } - } - - deserializer.deserialize_tuple(#max_slot + 1, CultCacheVisitor) - } - } - } - .into() -} - -struct CultCacheSlot { - key: usize, - ident: syn::Ident, - ty: Type, - default: bool, - bytes: bool, -} diff --git a/vendor/cultcache-rs/examples/cultcache_interop.rs b/vendor/cultcache-rs/examples/cultcache_interop.rs deleted file mode 100644 index 4049acf..0000000 --- a/vendor/cultcache-rs/examples/cultcache_interop.rs +++ /dev/null @@ -1,121 +0,0 @@ -use anyhow::Result; -use anyhow::anyhow; -use cultcache_rs::CultCache; -use cultcache_rs::DatabaseEntry; -use cultcache_rs::SingleFileMessagePackBackingStore; -use serde_json::json; -use std::collections::BTreeMap; - -const INTEROP_TYPE: &str = "cultcache.interop-note"; -const INTEROP_SCHEMA_VERSION: &str = "cultcache.interop_note.v1"; - -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "cultcache.interop-note", schema = "CultCacheInteropNote")] -struct CultCacheInteropNote { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - document_id: String, - #[cultcache(key = 2)] - author_runtime_id: String, - #[cultcache(key = 3)] - title: String, - #[cultcache(key = 4)] - body: String, - #[cultcache(key = 5, default)] - tags: Vec, -} - -fn main() -> Result<()> { - let mut args = std::env::args().skip(1); - let mode = args - .next() - .ok_or_else(|| anyhow!("expected mode: write | read"))?; - let options = parse_args(args.collect()); - let file = require_arg(&options, "file")?; - - match mode.as_str() { - "write" => write_note(file, require_arg(&options, "runtime-id")?)?, - "read" => read_note(file)?, - _ => return Err(anyhow!("unknown mode {mode}")), - } - Ok(()) -} - -fn write_note(file: &str, runtime_id: &str) -> Result<()> { - let mut cache = build_cache(file)?; - cache.pull_all_backing_stores()?; - let note = CultCacheInteropNote { - schema_version: INTEROP_SCHEMA_VERSION.to_string(), - document_id: format!("note:{runtime_id}"), - author_runtime_id: runtime_id.to_string(), - title: format!("{runtime_id} wrote a CultCache note"), - body: "The v1 store format is the contract.".to_string(), - tags: vec![ - runtime_id.to_string(), - "rust".to_string(), - "interop".to_string(), - ], - }; - cache.put(¬e.document_id, ¬e)?; - print_note(¬e) -} - -fn read_note(file: &str) -> Result<()> { - let mut cache = build_cache(file)?; - cache.pull_all_backing_stores()?; - let notes = cache.get_all::()?; - let note = notes - .first() - .ok_or_else(|| anyhow!("no {INTEROP_TYPE} records found"))?; - print_note(note) -} - -fn build_cache(file: &str) -> Result { - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(file)); - Ok(cache) -} - -fn print_note(note: &CultCacheInteropNote) -> Result<()> { - println!( - "{}", - serde_json::to_string(&json!({ - "schemaVersion": note.schema_version, - "documentId": note.document_id, - "authorRuntimeId": note.author_runtime_id, - "title": note.title, - "body": note.body, - "tags": note.tags, - }))? - ); - Ok(()) -} - -fn parse_args(args: Vec) -> BTreeMap { - let mut parsed = BTreeMap::new(); - let mut index = 0; - while index < args.len() { - let token = &args[index]; - if !token.starts_with("--") { - index += 1; - continue; - } - let name = token.trim_start_matches("--").to_string(); - let value = args - .get(index + 1) - .cloned() - .unwrap_or_else(|| panic!("missing value for --{name}")); - parsed.insert(name, value); - index += 2; - } - parsed -} - -fn require_arg<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { - options - .get(name) - .map(String::as_str) - .ok_or_else(|| anyhow!("missing required argument --{name}")) -} diff --git a/vendor/cultcache-rs/src/lib.rs b/vendor/cultcache-rs/src/lib.rs deleted file mode 100644 index 72ebf7e..0000000 --- a/vendor/cultcache-rs/src/lib.rs +++ /dev/null @@ -1,1241 +0,0 @@ -use anyhow::Context; -use anyhow::Result; -use anyhow::anyhow; -use serde::Serialize; -use serde::de::DeserializeOwned; -use std::collections::BTreeMap; -use std::collections::BTreeSet; -use std::fs; -use std::fs::File; -use std::fs::OpenOptions; -use std::io::Write; -use std::path::Path; -use std::path::PathBuf; - -extern crate self as cultcache_rs; - -pub use cultcache_rs_derive::DatabaseEntry; - -pub trait DatabaseEntry: Serialize + DeserializeOwned + Clone + Send + 'static { - const TYPE: &'static str; - const SCHEMA_NAME: &'static str = "DatabaseEntry"; -} - -pub trait CultCacheRegistry { - fn register_entries(&self, cache: &mut CultCache) -> Result<()>; -} - -#[macro_export] -macro_rules! cultcache_registry { - ($name:ident { $($entry:ty),* $(,)? }) => { - #[derive(Clone, Copy, Debug, Default)] - pub struct $name; - - impl $crate::CultCacheRegistry for $name { - fn register_entries(&self, cache: &mut $crate::CultCache) -> ::anyhow::Result<()> { - $( - cache.register_entry_type::<$entry>()?; - )* - Ok(()) - } - } - }; -} - -#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CultCacheEnvelope { - pub key: String, - #[serde(rename = "type")] - pub r#type: String, - #[serde(with = "serde_bytes")] - pub payload: Vec, - pub stored_at: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub schema_id: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultCacheExpectedEnvelope { - pub key: String, - pub r#type: String, - pub current: Option, -} - -#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -struct PersistedStoreSnapshot( - String, - Vec, - Vec, -); - -#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -struct PersistedSchemaCatalogEntry( - String, - String, - String, - String, - String, - Vec, - Vec, -); - -#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -struct PersistedSchemaCatalogMember( - u32, - String, - String, - bool, - bool, - Option, - bool, - Option, -); - -#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -struct PersistedRecord( - String, - String, - String, - #[serde(with = "serde_bytes")] Vec, -); - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct PushAllOptions { - pub soft: bool, -} - -pub trait CacheBackingStore: Send { - fn pull_all(&self) -> Result>; - fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()>; - fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()>; - - fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> { - let existing = self.pull_all()?; - for entry in existing { - self.delete(&entry)?; - } - for entry in entries { - self.push(entry)?; - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SingleFileMessagePackBackingStore { - path: PathBuf, -} - -impl SingleFileMessagePackBackingStore { - pub fn new(path: impl Into) -> Self { - Self { path: path.into() } - } - - pub fn path(&self) -> &Path { - &self.path - } - - /// Reads one atomically published snapshot without acquiring or creating - /// the sibling writer lock. This is for immutable, externally owned - /// documents such as pinned trust anchors where the reader has no write - /// authority over the containing directory. - pub fn pull_all_read_only_snapshot(&self) -> Result> { - self.read_all_unlocked() - } - - /// Holds the pre-created sibling lock shared while a read-only consumer - /// evaluates and acts on one snapshot. The reader never creates or writes - /// either file, so an authority owner can grant only read access. Writers - /// using `compare_exchange` cannot replace the snapshot until `action` - /// returns. - pub fn with_read_only_shared_snapshot( - &self, - action: impl FnOnce(Vec) -> Result, - ) -> Result { - self.with_read_only_shared_snapshot_and_unlock_diagnostic(action, |_| {}) - } - - pub fn with_read_only_shared_snapshot_and_unlock_diagnostic( - &self, - action: impl FnOnce(Vec) -> Result, - on_unlock_failure: impl FnOnce(anyhow::Error), - ) -> Result { - self.with_read_only_shared_snapshot_using( - action, - |lock| { - fs2::FileExt::unlock(lock) - .with_context(|| format!("failed to unlock {}", self.lock_path().display())) - }, - on_unlock_failure, - ) - } - - fn with_read_only_shared_snapshot_using( - &self, - action: impl FnOnce(Vec) -> Result, - unlock: U, - on_unlock_failure: D, - ) -> Result - where - U: FnOnce(&File) -> Result<()>, - D: FnOnce(anyhow::Error), - { - let lock_path = self.lock_path(); - let lock = OpenOptions::new() - .read(true) - .open(&lock_path) - .with_context(|| format!("failed to open pre-created {}", lock_path.display()))?; - fs2::FileExt::lock_shared(&lock) - .with_context(|| format!("failed to lock {}", lock_path.display()))?; - let guard = ReadOnlySharedLockGuard::new(lock, unlock, on_unlock_failure); - let result = self.read_all_unlocked().and_then(action); - drop(guard); - result - } - - pub fn compare_exchange( - &self, - expected: &[CultCacheExpectedEnvelope], - replacements: &[CultCacheEnvelope], - ) -> Result { - self.with_exclusive_lock(|| { - let mut entries = self.read_all_unlocked()?; - for condition in expected { - let current = entries - .iter() - .find(|entry| entry.r#type == condition.r#type && entry.key == condition.key); - if current != condition.current.as_ref() { - return Ok(false); - } - if let Some(envelope) = condition.current.as_ref() - && (envelope.r#type != condition.r#type || envelope.key != condition.key) - { - return Err(anyhow!( - "CultCache compare-exchange expectation identity differs from its envelope" - )); - } - } - let mut replacement_ids = BTreeSet::new(); - for replacement in replacements { - if replacement.key.trim().is_empty() || replacement.r#type.trim().is_empty() { - return Err(anyhow!( - "CultCache compare-exchange replacement identity is empty" - )); - } - if !replacement_ids.insert(entry_id(replacement)) { - return Err(anyhow!( - "CultCache compare-exchange contains a duplicate replacement" - )); - } - } - entries.retain(|entry| !replacement_ids.contains(&entry_id(entry))); - entries.extend_from_slice(replacements); - entries.sort_by_key(entry_id); - self.write_all_unlocked(&entries)?; - Ok(true) - }) - } - - /// Atomically replaces an entire snapshot only when the persisted entries - /// still exactly match `expected`. This is the bounded migration primitive - /// for removing obsolete identities without racing another writer between - /// a read and a whole-store rewrite. - pub fn compare_exchange_snapshot( - &self, - expected: &[CultCacheEnvelope], - replacements: &[CultCacheEnvelope], - ) -> Result { - self.with_exclusive_lock(|| { - let current = self.read_all_unlocked()?; - if current != expected { - return Ok(false); - } - let mut replacement_ids = BTreeSet::new(); - for replacement in replacements { - if replacement.key.trim().is_empty() || replacement.r#type.trim().is_empty() { - return Err(anyhow!("CultCache snapshot replacement identity is empty")); - } - if !replacement_ids.insert(entry_id(replacement)) { - return Err(anyhow!( - "CultCache snapshot replacement contains a duplicate identity" - )); - } - } - let mut ordered = replacements.to_vec(); - ordered.sort_by_key(entry_id); - self.write_all_unlocked(&ordered)?; - Ok(true) - }) - } - - fn read_all_unlocked(&self) -> Result> { - if !self.path.exists() { - return Ok(Vec::new()); - } - let bytes = fs::read(&self.path) - .with_context(|| format!("failed to read {}", self.path.display()))?; - if bytes.is_empty() { - return Ok(Vec::new()); - } - decode_store_snapshot(&bytes) - .or_else(|_| rmp_serde::from_slice(&bytes)) - .with_context(|| format!("failed to decode MessagePack {}", self.path.display())) - } - - fn write_all_unlocked(&self, entries: &[CultCacheEnvelope]) -> Result<()> { - if let Some(parent) = self.path.parent() { - fs::create_dir_all(parent) - .with_context(|| format!("failed to create {}", parent.display()))?; - } - let bytes = rmp_serde::to_vec(&encode_store_snapshot(entries)) - .context("failed to encode MessagePack")?; - let tmp_path = temporary_path_for(&self.path); - let mut temporary = OpenOptions::new() - .create_new(true) - .write(true) - .open(&tmp_path) - .with_context(|| format!("failed to create {}", tmp_path.display()))?; - temporary - .write_all(&bytes) - .with_context(|| format!("failed to write {}", tmp_path.display()))?; - temporary - .sync_all() - .with_context(|| format!("failed to sync {}", tmp_path.display()))?; - drop(temporary); - replace_file_atomically(&tmp_path, &self.path)?; - #[cfg(unix)] - if let Some(parent) = self.path.parent() { - File::open(parent) - .and_then(|directory| directory.sync_all()) - .with_context(|| format!("failed to sync {}", parent.display()))?; - } - Ok(()) - } - - fn with_shared_lock(&self, action: impl FnOnce() -> Result) -> Result { - let lock = self.open_lock_file()?; - fs2::FileExt::lock_shared(&lock) - .with_context(|| format!("failed to lock {}", self.lock_path().display()))?; - let result = action(); - fs2::FileExt::unlock(&lock) - .with_context(|| format!("failed to unlock {}", self.lock_path().display()))?; - result - } - - fn with_exclusive_lock(&self, action: impl FnOnce() -> Result) -> Result { - let lock = self.open_lock_file()?; - fs2::FileExt::lock_exclusive(&lock) - .with_context(|| format!("failed to lock {}", self.lock_path().display()))?; - let result = action(); - fs2::FileExt::unlock(&lock) - .with_context(|| format!("failed to unlock {}", self.lock_path().display()))?; - result - } - - fn open_lock_file(&self) -> Result { - let lock_path = self.lock_path(); - if let Some(parent) = lock_path.parent() { - fs::create_dir_all(parent) - .with_context(|| format!("failed to create {}", parent.display()))?; - } - OpenOptions::new() - .create(true) - .read(true) - .truncate(false) - .write(true) - .open(&lock_path) - .with_context(|| format!("failed to open {}", lock_path.display())) - } - - fn lock_path(&self) -> PathBuf { - let mut lock_name = self - .path - .file_name() - .map(|value| value.to_os_string()) - .unwrap_or_else(|| "cultcache.cc".into()); - lock_name.push(".lock"); - self.path.with_file_name(lock_name) - } -} - -struct ReadOnlySharedLockGuard -where - U: FnOnce(&File) -> Result<()>, - D: FnOnce(anyhow::Error), -{ - lock: Option, - unlock: Option, - on_unlock_failure: Option, -} - -impl ReadOnlySharedLockGuard -where - U: FnOnce(&File) -> Result<()>, - D: FnOnce(anyhow::Error), -{ - fn new(lock: File, unlock: U, on_unlock_failure: D) -> Self { - Self { lock: Some(lock), unlock: Some(unlock), on_unlock_failure: Some(on_unlock_failure) } - } -} - -impl Drop for ReadOnlySharedLockGuard -where - U: FnOnce(&File) -> Result<()>, - D: FnOnce(anyhow::Error), -{ - fn drop(&mut self) { - let (Some(lock), Some(unlock)) = (self.lock.as_ref(), self.unlock.take()) else { return; }; - if let Err(error) = unlock(lock) - && let Some(on_unlock_failure) = self.on_unlock_failure.take() - { - on_unlock_failure(error); - } - } -} - -#[cfg(unix)] -fn replace_file_atomically(source: &Path, destination: &Path) -> Result<()> { - fs::rename(source, destination).with_context(|| { - format!( - "failed to atomically replace {} with {}", - destination.display(), - source.display() - ) - }) -} - -#[cfg(windows)] -fn replace_file_atomically(source: &Path, destination: &Path) -> Result<()> { - use std::os::windows::ffi::OsStrExt; - use windows_sys::Win32::Storage::FileSystem::{ - MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW, - }; - let source: Vec = source.as_os_str().encode_wide().chain(Some(0)).collect(); - let destination: Vec = destination - .as_os_str() - .encode_wide() - .chain(Some(0)) - .collect(); - let result = unsafe { - MoveFileExW( - source.as_ptr(), - destination.as_ptr(), - MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH, - ) - }; - if result == 0 { - return Err(std::io::Error::last_os_error()).context("atomic CultCache replacement failed"); - } - Ok(()) -} - -impl CacheBackingStore for SingleFileMessagePackBackingStore { - fn pull_all(&self) -> Result> { - self.with_shared_lock(|| self.read_all_unlocked()) - } - - fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()> { - self.with_exclusive_lock(|| { - let mut entries = self.read_all_unlocked()?; - entries.retain(|candidate| entry_id(candidate) != entry_id(entry)); - entries.push(entry.clone()); - entries.sort_by_key(entry_id); - self.write_all_unlocked(&entries) - }) - } - - fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()> { - self.with_exclusive_lock(|| { - let mut entries = self.read_all_unlocked()?; - entries.retain(|candidate| entry_id(candidate) != entry_id(entry)); - self.write_all_unlocked(&entries) - }) - } - - fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> { - self.with_exclusive_lock(|| { - let mut entries = entries.to_vec(); - entries.sort_by_key(entry_id); - self.write_all_unlocked(&entries) - }) - } -} - -struct CultCacheStoreRegistration { - store: Box, - types: BTreeSet, -} - -pub struct CultCache { - definitions: BTreeMap, - entries: BTreeMap, - stores: Vec, -} - -impl CultCache { - pub fn new() -> Self { - Self { - definitions: BTreeMap::new(), - entries: BTreeMap::new(), - stores: Vec::new(), - } - } - - pub fn register_entry_type(&mut self) -> Result<()> { - if T::TYPE.trim().is_empty() { - return Err(anyhow!( - "CultCache entry types must declare a non-empty type" - )); - } - if let Some(existing_schema) = self.definitions.get(T::TYPE) - && *existing_schema != T::SCHEMA_NAME - { - return Err(anyhow!( - "CultCache already has a different definition registered for type {:?}", - T::TYPE - )); - } - self.definitions.insert(T::TYPE.to_string(), T::SCHEMA_NAME); - Ok(()) - } - - pub fn register_document_type(&mut self) -> Result<()> { - self.register_entry_type::() - } - - pub fn register_registry(&mut self, registry: R) -> Result<&mut Self> { - registry.register_entries(self)?; - Ok(self) - } - - pub fn add_backing_store( - &mut self, - store: impl CacheBackingStore + 'static, - types: impl IntoIterator>, - ) { - self.stores.push(CultCacheStoreRegistration { - store: Box::new(store), - types: types.into_iter().map(Into::into).collect(), - }); - } - - pub fn add_generic_backing_store(&mut self, store: impl CacheBackingStore + 'static) { - self.add_backing_store(store, Vec::::new()); - } - - pub fn pull_all_backing_stores(&mut self) -> Result<()> { - self.entries.clear(); - let known_types: BTreeSet = self.definitions.keys().cloned().collect(); - for registration in &mut self.stores { - for entry in registration.store.pull_all()? { - if !known_types.contains(&entry.r#type) { - return Err(anyhow!( - "No schema is registered for persisted entry type {:?}", - entry.r#type - )); - } - self.entries.insert(entry_id(&entry), entry); - } - } - Ok(()) - } - - pub fn get(&self, key: &str) -> Result> { - self.require_entry_type::()?; - let Some(entry) = self.entries.get(&entry_id_parts(T::TYPE, key)) else { - return Ok(None); - }; - let payload = rmp_serde::from_slice(&entry.payload).with_context(|| { - format!( - "failed to decode CultCache entry {:?} at key {:?} as {}", - T::TYPE, - key, - T::SCHEMA_NAME - ) - })?; - Ok(Some(payload)) - } - - pub fn get_required(&self, key: &str) -> Result { - self.get::(key)? - .ok_or_else(|| anyhow!("CultCache has no {:?} entry at key {:?}", T::TYPE, key)) - } - - pub fn get_envelope(&self, key: &str) -> Result> { - self.require_entry_type::()?; - Ok(self.entries.get(&entry_id_parts(T::TYPE, key)).cloned()) - } - - pub fn get_required_envelope(&self, key: &str) -> Result { - self.get_envelope::(key)? - .ok_or_else(|| anyhow!("CultCache has no {:?} envelope at key {:?}", T::TYPE, key)) - } - - pub fn get_all(&self) -> Result> { - self.require_entry_type::()?; - let mut values = Vec::new(); - for entry in self.entries.values() { - if entry.r#type != T::TYPE { - continue; - } - values.push(rmp_serde::from_slice(&entry.payload).with_context(|| { - format!( - "failed to decode CultCache entry {:?} at key {:?} as {}", - T::TYPE, - entry.key, - T::SCHEMA_NAME - ) - })?); - } - Ok(values) - } - - pub fn put(&mut self, key: impl Into, value: &T) -> Result { - self.require_entry_type::()?; - let key = key.into(); - let payload = rmp_serde::to_vec(value).with_context(|| { - format!( - "failed to encode CultCache entry {:?} at key {:?} as {}", - T::TYPE, - key, - T::SCHEMA_NAME - ) - })?; - let parsed: T = rmp_serde::from_slice(&payload).with_context(|| { - format!( - "failed to validate CultCache entry {:?} at key {:?} as {}", - T::TYPE, - key, - T::SCHEMA_NAME - ) - })?; - let entry = CultCacheEnvelope { - key: key.clone(), - r#type: T::TYPE.to_string(), - payload, - stored_at: now_utc_second(), - schema_id: Some(T::TYPE.to_string()), - }; - let route = self.resolve_route_indices(T::TYPE); - let Some(primary_index) = route.first().copied() else { - return Err(anyhow!( - "No backing store is registered for entry type {:?}", - T::TYPE - )); - }; - self.stores[primary_index].store.push(&entry)?; - for mirror_index in route.iter().skip(1).copied() { - self.stores[mirror_index].store.push(&entry)?; - } - self.entries.insert(entry_id(&entry), entry); - Ok(parsed) - } - - pub fn put_envelope(&mut self, entry: CultCacheEnvelope) -> Result { - self.require_entry_type::()?; - if entry.r#type != T::TYPE { - return Err(anyhow!( - "CultCache envelope type {:?} does not match registered Rust type {:?}", - entry.r#type, - T::TYPE - )); - } - if entry.key.trim().is_empty() { - return Err(anyhow!( - "CultCache envelope keys for type {:?} must be non-empty", - T::TYPE - )); - } - if entry.stored_at.trim().is_empty() { - return Err(anyhow!( - "CultCache envelope stored_at for type {:?} must be non-empty", - T::TYPE - )); - } - - let parsed: T = rmp_serde::from_slice(&entry.payload).with_context(|| { - format!( - "failed to validate CultCache envelope {:?} at key {:?} as {}", - T::TYPE, - entry.key, - T::SCHEMA_NAME - ) - })?; - let route = self.resolve_route_indices(T::TYPE); - let Some(primary_index) = route.first().copied() else { - return Err(anyhow!( - "No backing store is registered for entry type {:?}", - T::TYPE - )); - }; - self.stores[primary_index].store.push(&entry)?; - for mirror_index in route.iter().skip(1).copied() { - self.stores[mirror_index].store.push(&entry)?; - } - self.entries.insert(entry_id(&entry), entry); - Ok(parsed) - } - - pub fn put_raw_envelope(&mut self, entry: CultCacheEnvelope) -> Result<()> { - if !self.definitions.contains_key(&entry.r#type) { - return Err(anyhow!( - "No entry type registered for CultCache envelope type {:?}", - entry.r#type - )); - } - if entry.key.trim().is_empty() { - return Err(anyhow!( - "CultCache envelope keys for type {:?} must be non-empty", - entry.r#type - )); - } - if entry.stored_at.trim().is_empty() { - return Err(anyhow!( - "CultCache envelope stored_at for type {:?} must be non-empty", - entry.r#type - )); - } - let route = self.resolve_route_indices(&entry.r#type); - let Some(primary_index) = route.first().copied() else { - return Err(anyhow!( - "No backing store is registered for entry type {:?}", - entry.r#type - )); - }; - self.stores[primary_index].store.push(&entry)?; - for mirror_index in route.iter().skip(1).copied() { - self.stores[mirror_index].store.push(&entry)?; - } - self.entries.insert(entry_id(&entry), entry); - Ok(()) - } - - pub fn update(&mut self, key: &str, updater: F) -> Result - where - T: DatabaseEntry, - F: FnOnce(Option) -> T, - { - let current = self.get::(key)?; - self.put::(key.to_string(), &updater(current)) - } - - pub fn delete(&mut self, key: &str) -> Result { - self.require_entry_type::()?; - let id = entry_id_parts(T::TYPE, key); - let Some(entry) = self.entries.get(&id).cloned() else { - return Ok(false); - }; - let route = self.resolve_route_indices(T::TYPE); - let Some(primary_index) = route.first().copied() else { - return Err(anyhow!( - "No backing store is registered for entry type {:?}", - T::TYPE - )); - }; - self.stores[primary_index].store.delete(&entry)?; - for mirror_index in route.iter().skip(1).copied() { - self.stores[mirror_index].store.delete(&entry)?; - } - self.entries.remove(&id); - Ok(true) - } - - pub fn snapshot(&self) -> Vec { - self.entries.values().cloned().collect() - } - - fn require_entry_type(&self) -> Result<()> { - match self.definitions.get(T::TYPE) { - Some(schema_name) if *schema_name == T::SCHEMA_NAME => Ok(()), - _ => Err(anyhow!( - "CultCache entry type {:?} is not registered on this cache instance", - T::TYPE - )), - } - } - - fn resolve_route_indices(&self, type_id: &str) -> Vec { - let type_specific: Vec = self - .stores - .iter() - .enumerate() - .filter_map(|(index, registration)| { - registration.types.contains(type_id).then_some(index) - }) - .collect(); - if !type_specific.is_empty() { - return type_specific; - } - self.stores - .iter() - .enumerate() - .filter_map(|(index, registration)| registration.types.is_empty().then_some(index)) - .collect() - } -} - -impl Default for CultCache { - fn default() -> Self { - Self::new() - } -} - -fn entry_id(entry: &CultCacheEnvelope) -> String { - entry_id_parts(&entry.r#type, &entry.key) -} - -fn entry_id_parts(r#type: &str, key: &str) -> String { - format!("{type}::{key}", type = r#type) -} - -fn now_utc_second() -> String { - chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) -} - -fn encode_store_snapshot(entries: &[CultCacheEnvelope]) -> PersistedStoreSnapshot { - let mut schema_names = BTreeMap::::new(); - for entry in entries { - schema_names.insert( - entry - .schema_id - .clone() - .unwrap_or_else(|| entry.r#type.clone()), - entry.r#type.clone(), - ); - } - - let catalog = schema_names - .into_iter() - .map(|(schema_id, document_type)| { - PersistedSchemaCatalogEntry( - schema_id.clone(), - document_type, - format!("{schema_id}.v1"), - schema_id.clone(), - format!( - "{{\"schemaName\":\"{}\",\"schemaVersion\":\"{}.v1\",\"members\":[]}}", - escape_json_string(&schema_id), - escape_json_string(&schema_id) - ), - vec![schema_id], - Vec::new(), - ) - }) - .collect(); - let records = entries - .iter() - .map(|entry| { - PersistedRecord( - entry.key.clone(), - entry - .schema_id - .clone() - .unwrap_or_else(|| entry.r#type.clone()), - entry.stored_at.clone(), - entry.payload.clone(), - ) - }) - .collect(); - - PersistedStoreSnapshot("cultcache.store.v1".to_string(), catalog, records) -} - -fn decode_store_snapshot(bytes: &[u8]) -> Result> { - let snapshot: PersistedStoreSnapshot = - rmp_serde::from_slice(bytes).context("failed to decode CultCache v1 snapshot")?; - if snapshot.0 != "cultcache.store.v1" { - return Err(anyhow!("unsupported CultCache snapshot {}", snapshot.0)); - } - - let catalog = snapshot - .1 - .into_iter() - .map(|entry| (entry.0, entry.1)) - .collect::>(); - snapshot - .2 - .into_iter() - .map(|record| { - let r#type = catalog.get(&record.1).cloned().ok_or_else(|| { - anyhow!( - "CultCache record {:?} references missing schema {:?}", - record.0, - record.1 - ) - })?; - Ok(CultCacheEnvelope { - key: record.0, - r#type, - stored_at: record.2, - payload: record.3, - schema_id: Some(record.1), - }) - }) - .collect() -} - -fn escape_json_string(value: &str) -> String { - value.replace('\\', "\\\\").replace('"', "\\\"") -} - -fn temporary_path_for(path: &Path) -> PathBuf { - let mut file_name = path - .file_name() - .map(|value| value.to_os_string()) - .unwrap_or_else(|| "cultcache.cc".into()); - file_name.push(format!(".{}.tmp", uuid::Uuid::new_v4())); - path.with_file_name(file_name) -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - use std::sync::mpsc; - use std::time::Duration; - - #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] - #[cultcache(type = "settings")] - struct Settings { - #[cultcache(key = 0)] - theme: String, - #[cultcache(key = 1, default)] - retries: u32, - } - - #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] - #[cultcache(type = "note")] - struct Note { - #[cultcache(key = 0)] - title: String, - #[cultcache(key = 1)] - body: String, - } - - cultcache_registry!(TestEntries { Settings, Note }); - - #[test] - fn captured_snapshot_cannot_outlive_a_shared_consequence_gate() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("authority.cc"); - let mut store = SingleFileMessagePackBackingStore::new(&store_path); - let released = CultCacheEnvelope { - key: "brake".into(), - r#type: "brake".into(), - payload: b"released".to_vec(), - stored_at: "2026-07-20T00:00:00Z".into(), - schema_id: Some("brake.v0".into()), - }; - store.push(&released)?; - - let writer_path = store_path.clone(); - let (started_tx, started_rx) = mpsc::channel(); - let (finished_tx, finished_rx) = mpsc::channel(); - store.with_read_only_shared_snapshot(|captured| { - assert_eq!(captured, vec![released.clone()]); - std::thread::spawn(move || { - started_tx.send(()).unwrap(); - let engaged = CultCacheEnvelope { - payload: b"engaged".to_vec(), - ..released - }; - let mut writer = SingleFileMessagePackBackingStore::new(writer_path); - writer.push(&engaged).unwrap(); - finished_tx.send(()).unwrap(); - }); - started_rx.recv_timeout(Duration::from_secs(1))?; - assert!(finished_rx.recv_timeout(Duration::from_millis(100)).is_err()); - Ok(()) - })?; - finished_rx.recv_timeout(Duration::from_secs(1))?; - assert_eq!(store.pull_all_read_only_snapshot()?[0].payload, b"engaged"); - Ok(()) - } - - #[test] - fn post_action_unlock_failure_is_diagnostic_not_action_result() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("authority.cc"); - let mut store = SingleFileMessagePackBackingStore::new(&store_path); - store.push(&CultCacheEnvelope { - key: "brake".into(), - r#type: "brake".into(), - payload: b"released".to_vec(), - stored_at: "2026-07-20T00:00:00Z".into(), - schema_id: Some("brake".into()), - })?; - - let mut diagnostic = None; - let result = store.with_read_only_shared_snapshot_using( - |_| Ok(42), - |_| Err(anyhow!("injected post-action unlock failure")), - |error| diagnostic = Some(error.to_string()), - ); - - assert_eq!(result?, 42); - assert_eq!(diagnostic.as_deref(), Some("injected post-action unlock failure")); - Ok(()) - } - - #[test] - fn whole_snapshot_replacement_is_atomic_and_rejects_stale_or_duplicate_inputs() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("compact.cc"); - let mut store = SingleFileMessagePackBackingStore::new(&store_path); - let first = CultCacheEnvelope { - key: "first".into(), - r#type: "note".into(), - payload: b"old".to_vec(), - stored_at: "2026-08-23T00:00:00Z".into(), - schema_id: Some("note.v1".into()), - }; - let obsolete = CultCacheEnvelope { - key: "obsolete".into(), - payload: b"discard".to_vec(), - ..first.clone() - }; - store.push(&first)?; - store.push(&obsolete)?; - let expected = store.pull_all_read_only_snapshot()?; - let replacement = CultCacheEnvelope { - payload: b"current".to_vec(), - ..first.clone() - }; - - assert!(store.compare_exchange_snapshot(&expected, std::slice::from_ref(&replacement))?); - assert_eq!(store.pull_all_read_only_snapshot()?, [replacement.clone()]); - assert!(!store.compare_exchange_snapshot(&expected, &[])?); - assert!( - store - .compare_exchange_snapshot( - std::slice::from_ref(&replacement), - &[replacement.clone(), replacement.clone()] - ) - .is_err() - ); - Ok(()) - } - - #[test] - fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let settings = Settings { - theme: "ash".to_string(), - retries: 3, - }; - - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - cache.pull_all_backing_stores()?; - cache.put("app", &settings)?; - assert_eq!(cache.get_required::("app")?, settings); - - let mut reloaded = CultCache::new(); - reloaded.register_entry_type::()?; - reloaded.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - reloaded.pull_all_backing_stores()?; - assert_eq!(reloaded.get_required::("app")?, settings); - Ok(()) - } - - #[test] - fn entry_identity_is_polymorphic_by_type_and_key() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let mut cache = CultCache::new(); - cache.register_registry(TestEntries)?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - - cache.put( - "shared", - &Settings { - theme: "green".to_string(), - retries: 1, - }, - )?; - cache.put( - "shared", - &Note { - title: "same key".to_string(), - body: "different type".to_string(), - }, - )?; - - assert_eq!(cache.snapshot().len(), 2); - assert_eq!(cache.get_required::("shared")?.title, "same key"); - assert_eq!(cache.get_required::("shared")?.theme, "green"); - Ok(()) - } - - #[test] - fn type_specific_store_routes_before_generic_store() -> Result<()> { - let temp = tempfile::tempdir()?; - let generic_path = temp.path().join("generic.cc"); - let settings_path = temp.path().join("settings.cc"); - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&generic_path)); - cache.add_backing_store( - SingleFileMessagePackBackingStore::new(&settings_path), - ["settings"], - ); - - cache.put( - "app", - &Settings { - theme: "ash".to_string(), - retries: 3, - }, - )?; - cache.put( - "memo", - &Note { - title: "hello".to_string(), - body: "world".to_string(), - }, - )?; - - let generic_entries = SingleFileMessagePackBackingStore::new(&generic_path).pull_all()?; - let settings_entries = SingleFileMessagePackBackingStore::new(&settings_path).pull_all()?; - assert_eq!(generic_entries[0].r#type, "note"); - assert_eq!(settings_entries[0].r#type, "settings"); - Ok(()) - } - - #[test] - fn update_and_delete_follow_the_cache_api() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - - let updated = cache.update::("app", |current| { - let mut current = current.unwrap_or(Settings { - theme: "ash".to_string(), - retries: 0, - }); - current.retries += 1; - current - })?; - assert_eq!(updated.retries, 1); - assert!(cache.delete::("app")?); - assert!(cache.get::("app")?.is_none()); - Ok(()) - } - - #[test] - fn pull_rejects_unregistered_persisted_entry_type() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let mut store = SingleFileMessagePackBackingStore::new(&store_path); - store.push(&CultCacheEnvelope { - key: "unknown".to_string(), - r#type: "unregistered".to_string(), - payload: rmp_serde::to_vec(&1_u8)?, - stored_at: now_utc_second(), - schema_id: Some("unregistered".to_string()), - })?; - - let mut cache = CultCache::new(); - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - let error = cache.pull_all_backing_stores().unwrap_err(); - assert!( - error - .to_string() - .contains("No schema is registered for persisted entry type") - ); - Ok(()) - } - - #[test] - fn payload_is_binary_messagepack_not_json_value() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - cache.put( - "app", - &Settings { - theme: "ash".to_string(), - retries: 3, - }, - )?; - - let entry = cache.snapshot().remove(0); - let decoded: Settings = rmp_serde::from_slice(&entry.payload)?; - assert_eq!(decoded.theme, "ash"); - assert!(!entry.payload.is_empty()); - Ok(()) - } - - #[test] - fn corrupted_payload_fails_during_typed_retrieval() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cache.cc"); - let mut store = SingleFileMessagePackBackingStore::new(&store_path); - store.push(&CultCacheEnvelope { - key: "app".to_string(), - r#type: "settings".to_string(), - payload: vec![0xc1], - stored_at: now_utc_second(), - schema_id: Some("settings".to_string()), - })?; - - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); - cache.pull_all_backing_stores()?; - let error = cache.get_required::("app").unwrap_err(); - assert!( - error - .to_string() - .contains("failed to decode CultCache entry") - ); - Ok(()) - } - - #[test] - fn put_envelope_reuses_existing_messagepack_payload() -> Result<()> { - let temp = tempfile::tempdir()?; - let origin_store = temp.path().join("origin.cc"); - let target_store = temp.path().join("target.cc"); - - let mut origin = CultCache::new(); - origin.register_entry_type::()?; - origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store)); - origin.put( - "app", - &Settings { - theme: "ash".to_string(), - retries: 3, - }, - )?; - - let envelope = origin.get_required_envelope::("app")?; - - let mut target = CultCache::new(); - target.register_entry_type::()?; - target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store)); - let applied = target.put_envelope::(envelope.clone())?; - - assert_eq!( - applied, - Settings { - theme: "ash".to_string(), - retries: 3, - } - ); - assert_eq!(target.get_required::("app")?, applied); - assert_eq!( - target.get_required_envelope::("app")?.payload, - envelope.payload - ); - Ok(()) - } -} diff --git a/vendor/cultmesh-rs/Cargo.lock b/vendor/cultmesh-rs/Cargo.lock index 328295c..7f68a9b 100644 --- a/vendor/cultmesh-rs/Cargo.lock +++ b/vendor/cultmesh-rs/Cargo.lock @@ -65,10 +65,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] -name = "bitflags" -version = "2.11.1" +name = "base64ct" +version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "bitflags" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84d7ced0ae9557296835c32bf1b1e02b44c746701f898460fb000d7eaa84f00a" [[package]] name = "block-buffer" @@ -125,6 +131,12 @@ dependencies = [ "inout", ] +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -173,6 +185,7 @@ dependencies = [ "serde_bytes", "serde_json", "uuid", + "windows-sys 0.61.2", ] [[package]] @@ -206,6 +219,7 @@ dependencies = [ "base64", "chrono", "cultcache-rs", + "ed25519-dalek", "hmac", "rand", "rmp-serde", @@ -216,6 +230,45 @@ dependencies = [ "sha2", "socket2", "uuid", + "windows-sys 0.61.2", + "zeroize", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "zeroize", ] [[package]] @@ -235,6 +288,30 @@ dependencies = [ "subtle", ] +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek", + "ed25519", + "serde", + "sha2", + "subtle", + "zeroize", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -257,6 +334,12 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + [[package]] name = "find-msvc-tools" version = "0.1.9" @@ -515,6 +598,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + [[package]] name = "polyval" version = "0.6.2" @@ -654,6 +747,15 @@ dependencies = [ "serde_bytes", ] +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "1.1.4" @@ -749,6 +851,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "slab" version = "0.4.12" @@ -765,6 +876,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "subtle" version = "2.6.1" @@ -797,9 +918,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.20.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ce102ab67701b8526c123c1bab5cbe42d7040ccfd0f64af1a385808d2f43de" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" [[package]] name = "unicode-ident" @@ -825,9 +946,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.23.1" +version = "1.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddd74a9687298c6858e9b88ec8935ec45d22e8fd5e6394fa1bd4e99a87789c76" +checksum = "d258b83ceec21034727ecee8c382cfa6c3e133699b0742c64571814fb420c9f7" dependencies = [ "getrandom 0.4.2", "js-sys", @@ -1208,24 +1329,30 @@ checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "zerocopy" -version = "0.8.48" +version = "0.8.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +checksum = "3b065d4f0e55f82fae73202e189638116a87c55ab6b8e6c2721e13dd9d854ad1" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.48" +version = "0.8.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +checksum = "0b631b19d36a892ab55420c92dbc83ccd79274f25be714855d3074aa71cab639" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" + [[package]] name = "zmij" version = "1.0.21" diff --git a/vendor/cultmesh-rs/Cargo.toml b/vendor/cultmesh-rs/Cargo.toml index 9a719d3..441481d 100644 --- a/vendor/cultmesh-rs/Cargo.toml +++ b/vendor/cultmesh-rs/Cargo.toml @@ -10,8 +10,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { path = "../cultcache-rs" } -cultnet-rs = { path = "../cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] diff --git a/vendor/cultmesh-rs/src/lib.rs b/vendor/cultmesh-rs/src/lib.rs index 3217a28..708198f 100644 --- a/vendor/cultmesh-rs/src/lib.rs +++ b/vendor/cultmesh-rs/src/lib.rs @@ -12,7 +12,9 @@ use cultnet_rs::decode_cultnet_message_from_slice; use cultnet_rs::encode_cultnet_message_to_vec; use serde::Serialize; use std::collections::BTreeMap; +use std::collections::hash_map::RandomState; use std::env; +use std::hash::{BuildHasher, Hasher}; use std::net::SocketAddr; use std::net::UdpSocket; use std::path::Path; @@ -20,6 +22,10 @@ use std::thread; use std::time::Duration; use std::time::Instant; +mod rudp_document_server; + +pub use rudp_document_server::*; + pub const CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID: u32 = 0x0d1d_0002; pub trait CultMeshDocumentSet: Clone + Send + Sync + 'static { @@ -72,7 +78,6 @@ pub struct CultMeshNodeOptions { pub struct CultMeshRudpDocumentPublishOptions { pub target: SocketAddr, pub runtime_id: String, - pub connection_id: u32, pub connect_timeout: Duration, pub flush_timeout: Duration, pub poll_interval: Duration, @@ -86,7 +91,10 @@ pub struct CultMeshRudpDocumentPublishOptions { pub struct CultMeshRudpSnapshotOptions { pub target: SocketAddr, pub runtime_id: String, - pub connection_id: u32, + /// Exact correlation id for the SnapshotRequest. Raw callers must supply + /// this; `CultMeshNode::pull_rudp_catalog_snapshot` generates one when it + /// owns the request. + pub message_id: String, pub connect_timeout: Duration, pub response_timeout: Duration, pub poll_interval: Duration, @@ -110,7 +118,7 @@ impl Default for CultMeshRudpSnapshotOptions { Self { target: SocketAddr::from(([0, 0, 0, 0], 0)), runtime_id: "cultmesh-rudp-snapshot-client".to_string(), - connection_id: CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, + message_id: String::new(), connect_timeout: Duration::from_secs(3), response_timeout: Duration::from_secs(3), poll_interval: Duration::from_millis(10), @@ -136,7 +144,6 @@ impl Default for CultMeshRudpDocumentPublishOptions { Self { target: SocketAddr::from(([0, 0, 0, 0], 0)), runtime_id: "cultmesh-rudp-document-publisher".to_string(), - connection_id: CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, connect_timeout: Duration::from_secs(3), flush_timeout: Duration::from_millis(300), poll_interval: Duration::from_millis(10), @@ -294,7 +301,10 @@ impl CultMeshStreamDescriptor { } pub fn metadata_schema_id(mut self, metadata_schema_id: impl Into) -> Result { - self.metadata_schema_id = Some(require_non_empty(metadata_schema_id.into(), "metadata_schema_id")?); + self.metadata_schema_id = Some(require_non_empty( + metadata_schema_id.into(), + "metadata_schema_id", + )?); Ok(self) } } @@ -388,7 +398,11 @@ pub struct CultMeshSharedMemoryFrameRing { } impl CultMeshSharedMemoryFrameRing { - pub fn new(stream_id: impl Into, slot_count: usize, slot_byte_length: usize) -> Result { + pub fn new( + stream_id: impl Into, + slot_count: usize, + slot_byte_length: usize, + ) -> Result { let stream_id = require_non_empty(stream_id.into(), "stream_id")?; if slot_count == 0 { anyhow::bail!("slot_count must be greater than zero"); @@ -494,7 +508,11 @@ impl CultMeshStreamCatalog { self.streams.values().collect() } - pub fn find(&self, verse_id: &str, kind: Option) -> Vec<&CultMeshStreamDescriptor> { + pub fn find( + &self, + verse_id: &str, + kind: Option, + ) -> Vec<&CultMeshStreamDescriptor> { self.streams .values() .filter(|stream| stream.verse_id == verse_id && kind.is_none_or(|k| stream.kind == k)) @@ -521,7 +539,9 @@ impl CultMeshStreamCatalog { .iter() .copied() .find(|candidate| consumer.supported_transports.contains(candidate)) - .ok_or_else(|| anyhow::anyhow!("stream and consumer have no compatible body transport"))?; + .ok_or_else(|| { + anyhow::anyhow!("stream and consumer have no compatible body transport") + })?; let producer_max = if stream.max_in_flight_frames == 0 { u32::MAX } else { @@ -671,8 +691,16 @@ impl CultMeshNode { pub fn pull_rudp_catalog_snapshot( &mut self, - options: CultMeshRudpSnapshotOptions, + mut options: CultMeshRudpSnapshotOptions, ) -> Result { + if options.message_id.trim().is_empty() { + options.message_id = format!( + "{}:snapshot:{}:{}", + options.runtime_id, + unix_millis(), + fresh_rudp_connection_epoch() + ); + } let response = request_raw_snapshot_from_rudp_catalog(options)?; let cultnet_rs::CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { anyhow::bail!("expected cultnet.snapshot_response_raw.v0"); @@ -722,58 +750,38 @@ fn strip_schema_version_suffix(value: &str) -> &str { } } -fn request_raw_snapshot_from_rudp_catalog( +/// Request a raw CultNet snapshot without importing it into a local cache. +/// +/// The returned value is the exact validated `SnapshotResponseRaw`, including +/// its correlation `message_id`. Each call binds a fresh source port, chooses a +/// fresh connection epoch, and succeeds only after the reliable request is +/// acknowledged. +pub fn request_raw_snapshot_from_rudp_catalog( options: CultMeshRudpSnapshotOptions, ) -> Result { if options.runtime_id.trim().is_empty() { anyhow::bail!("runtime_id must be non-empty"); } - let bind_addr = if options.target.is_ipv4() { - SocketAddr::from(([0, 0, 0, 0], 0)) - } else { - SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 0], 0)) - }; - let socket = UdpSocket::bind(bind_addr)?; - socket.set_read_timeout(Some(options.poll_interval))?; - let mut client = CultNetRudpSocketTransportConnection::new( - CultNetRudpSocketTransportOptions { - runtime_id: options.runtime_id.clone(), - socket, - mode: cultnet_rs::CultNetRudpSocketMode::Client, - remote_addr: Some(options.target), - connection_id: options.connection_id, - initial_sequence: 1, - resend_delay_ms: options.resend_delay_ms, - transport_id: Some("cultmesh-rudp-snapshot-client".to_string()), - max_payload_bytes: None, - max_fragment_bytes: Some(1200), - max_pending_reliable_packets: None, - media_reliable_expire_after_ms: None, - }, + if options.message_id.trim().is_empty() { + anyhow::bail!("raw CultMesh RUDP snapshot requires a message_id"); + } + let message_id = options.message_id.clone(); + let mut client = connect_rudp_client( + options.target, + &options.runtime_id, + options.connect_timeout, + options.poll_interval, + options.resend_delay_ms, + "cultmesh-rudp-snapshot-client", )?; - client.connect(Vec::new())?; - let connect_deadline = Instant::now() + options.connect_timeout; - while !client.connected() && Instant::now() < connect_deadline { - let _ = client.receive_once()?; - client.poll_resends()?; - thread::sleep(options.poll_interval); - } - if !client.connected() { - anyhow::bail!( - "timed out connecting CultMesh RUDP snapshot client {} to {}", - options.runtime_id, - options.target - ); - } - let message_id = format!("{}:snapshot:{}", options.runtime_id, unix_millis()); let request = cultnet_rs::CultNetMessage::SnapshotRequest { message_id: message_id.clone(), schema_ids: options.schema_ids, record_keys: options.record_keys, }; let payload = encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; - client.send("schema", payload)?; + let receipt = client.send_reliable("schema", payload)?; let response_deadline = Instant::now() + options.response_timeout; let mut snapshot_response = None; while Instant::now() < response_deadline { @@ -789,7 +797,6 @@ fn request_raw_snapshot_from_rudp_catalog( .. } if received == &message_id => { snapshot_response = Some(message); - break; } cultnet_rs::CultNetMessage::Error { error } => { let _ = client.disconnect(b"snapshot-error".to_vec()); @@ -799,15 +806,32 @@ fn request_raw_snapshot_from_rudp_catalog( } } } + match client.reliable_send_status(&receipt) { + cultnet_rs::CultNetRudpReliableSendStatus::Acknowledged + if snapshot_response.is_some() => + { + let _ = client.disconnect(b"snapshot-complete".to_vec()); + return Ok(snapshot_response.expect("response was checked as present")); + } + cultnet_rs::CultNetRudpReliableSendStatus::Invalidated => { + anyhow::bail!("CultMesh RUDP snapshot request receipt was invalidated") + } + _ => {} + } client.poll_resends()?; thread::sleep(options.poll_interval); } - if let Some(message) = snapshot_response { - let _ = client.disconnect(b"snapshot-complete".to_vec()); - return Ok(message); - } let _ = client.disconnect(b"snapshot-timeout".to_vec()); - anyhow::bail!("timed out waiting for CultMesh RUDP snapshot from {}", options.target) + if snapshot_response.is_some() { + anyhow::bail!( + "timed out waiting for reliable acknowledgement of CultMesh RUDP snapshot request to {}", + options.target + ); + } + anyhow::bail!( + "timed out waiting for CultMesh RUDP snapshot from {}", + options.target + ) } fn unix_millis() -> u128 { @@ -817,56 +841,100 @@ fn unix_millis() -> u128 { .as_millis() } -fn publish_cultnet_message_to_rudp_catalog( +/// Publish one raw CultNet schema message to a RUDP catalog. +/// +/// Each call owns a fresh connection epoch and returns only after the catalog +/// acknowledges reliable application admission. +pub fn publish_cultnet_message_to_rudp_catalog( message: &cultnet_rs::CultNetMessage, options: CultMeshRudpDocumentPublishOptions, ) -> Result<()> { if options.runtime_id.trim().is_empty() { anyhow::bail!("runtime_id must be non-empty"); } - let bind_addr = if options.target.is_ipv4() { + let mut client = connect_rudp_client( + options.target, + &options.runtime_id, + options.connect_timeout, + options.poll_interval, + options.resend_delay_ms, + "cultmesh-rudp-document-publisher", + )?; + + let payload = encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?; + let receipt = client.send_reliable("schema", payload)?; + let flush_deadline = Instant::now() + options.flush_timeout; + while Instant::now() < flush_deadline { + let _ = client.receive_once()?; + match client.reliable_send_status(&receipt) { + cultnet_rs::CultNetRudpReliableSendStatus::Acknowledged => { + let _ = client.disconnect(b"document-acknowledged".to_vec()); + return Ok(()); + } + cultnet_rs::CultNetRudpReliableSendStatus::Invalidated => { + anyhow::bail!("CultMesh RUDP document receipt was invalidated") + } + cultnet_rs::CultNetRudpReliableSendStatus::Pending => {} + } + client.poll_resends()?; + thread::sleep(options.poll_interval); + } + let _ = client.disconnect(b"document-unacknowledged".to_vec()); + anyhow::bail!( + "timed out waiting for reliable acknowledgement from CultMesh RUDP catalog {}", + options.target + ) +} + +fn connect_rudp_client( + target: SocketAddr, + runtime_id: &str, + connect_timeout: Duration, + poll_interval: Duration, + resend_delay_ms: u64, + transport_id: &str, +) -> Result { + let bind_addr = if target.is_ipv4() { SocketAddr::from(([0, 0, 0, 0], 0)) } else { SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 0], 0)) }; let socket = UdpSocket::bind(bind_addr)?; - socket.set_read_timeout(Some(options.poll_interval))?; - let mut client = CultNetRudpSocketTransportConnection::new( - CultNetRudpSocketTransportOptions { - runtime_id: options.runtime_id.clone(), + socket.set_read_timeout(Some(poll_interval))?; + let mut client = + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { + runtime_id: runtime_id.to_string(), socket, mode: cultnet_rs::CultNetRudpSocketMode::Client, - remote_addr: Some(options.target), - connection_id: options.connection_id, + remote_addr: Some(target), + connection_id: fresh_rudp_connection_epoch(), initial_sequence: 1, - resend_delay_ms: options.resend_delay_ms, - transport_id: Some("cultmesh-rudp-document-publisher".to_string()), + resend_delay_ms, + transport_id: Some(transport_id.to_string()), max_payload_bytes: None, max_fragment_bytes: Some(1200), - max_pending_reliable_packets: None, + max_pending_reliable_packets: Some(1024), media_reliable_expire_after_ms: None, - }, - )?; + reconnect_policy: None, + })?; client.connect(Vec::new())?; - let connect_deadline = Instant::now() + options.connect_timeout; + let connect_deadline = Instant::now() + connect_timeout; while !client.connected() && Instant::now() < connect_deadline { let _ = client.receive_once()?; client.poll_resends()?; - thread::sleep(options.poll_interval); + thread::sleep(poll_interval); } if !client.connected() { - client.assume_connected(); + anyhow::bail!("timed out connecting CultMesh RUDP client {runtime_id} to {target}"); } + Ok(client) +} - let payload = encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?; - client.send("schema", payload)?; - let flush_deadline = Instant::now() + options.flush_timeout; - while Instant::now() < flush_deadline { - let _ = client.receive_once()?; - client.poll_resends()?; - thread::sleep(options.poll_interval); - } - Ok(()) +fn fresh_rudp_connection_epoch() -> u32 { + let mut hasher = RandomState::new().build_hasher(); + hasher.write_u128(unix_millis()); + let epoch = hasher.finish() as u32; + if epoch == 0 { 1 } else { epoch } } #[derive(Clone, Copy, Debug, Default)] @@ -879,8 +947,9 @@ impl CultMesh { return parse_rudp_socket_addr(&text); } if text.starts_with("cultmesh://") { - let resolved = default_cultmesh_rudp_endpoint_resolver(&text)? - .ok_or_else(|| anyhow::anyhow!("CultMesh URI {text} did not resolve to a RUDP endpoint"))?; + let resolved = default_cultmesh_rudp_endpoint_resolver(&text)?.ok_or_else(|| { + anyhow::anyhow!("CultMesh URI {text} did not resolve to a RUDP endpoint") + })?; return parse_rudp_socket_addr(&resolved); } parse_socket_addr(&text) @@ -959,10 +1028,7 @@ pub fn resolve_rudp_endpoint(endpoint: &str) -> Result { } fn parse_rudp_socket_addr(value: &str) -> Result { - let text = value - .trim() - .strip_prefix("rudp://") - .unwrap_or(value.trim()); + let text = value.trim().strip_prefix("rudp://").unwrap_or(value.trim()); parse_socket_addr(text) } @@ -976,7 +1042,13 @@ fn default_cultmesh_rudp_endpoint_resolver(uri: &str) -> Result> let authority = cultmesh_uri_authority(uri)?; let slug = authority .chars() - .map(|ch| if ch.is_ascii_alphanumeric() { ch.to_ascii_uppercase() } else { '_' }) + .map(|ch| { + if ch.is_ascii_alphanumeric() { + ch.to_ascii_uppercase() + } else { + '_' + } + }) .collect::() .trim_matches('_') .to_string(); @@ -1006,11 +1078,7 @@ fn cultmesh_uri_authority(uri: &str) -> Result { let rest = uri .strip_prefix("cultmesh://") .ok_or_else(|| anyhow::anyhow!("CultMesh URI must start with cultmesh://"))?; - let authority = rest - .split(['/', '?', '#']) - .next() - .unwrap_or("") - .trim(); + let authority = rest.split(['/', '?', '#']).next().unwrap_or("").trim(); if authority.is_empty() { anyhow::bail!("CultMesh URI must include an authority"); } @@ -1084,33 +1152,33 @@ mod tests { socket.set_read_timeout(Some(Duration::from_millis(10)))?; let target = socket.local_addr()?; let (sender, receiver) = std::sync::mpsc::channel(); + let delivered = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let delivered_by_sink = delivered.clone(); let server = thread::spawn(move || -> Result<()> { - let mut server = CultNetRudpSocketTransportConnection::new( - CultNetRudpSocketTransportOptions::server( - "odin-test-catalog", - socket, - CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID, - ), + let mut server = CultMeshRudpDocumentServer::new( + socket, + move |receipt: CultMeshRudpRawDocumentReceipt| { + sender + .send(( + receipt.document.schema_id, + receipt.document.record_key, + receipt.document.source_runtime_id, + )) + .ok(); + delivered_by_sink.store(true, std::sync::atomic::Ordering::SeqCst); + Ok(()) + }, + |_query: &CultMeshRudpSnapshotQuery| Ok(Vec::new()), + CultMeshSystemClock::default(), + CultMeshRudpDocumentServerOptions::default(), )?; let deadline = Instant::now() + Duration::from_secs(2); while Instant::now() < deadline { - if let Some(frame) = server.receive_once()? { - let message = cultnet_rs::decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - )?; - if let cultnet_rs::CultNetMessage::DocumentPutRaw { document, .. } = message { - sender - .send(( - document.schema_id, - document.record_key, - document.source_runtime_id, - )) - .ok(); - return Ok(()); - } + server.poll_once()?; + if delivered.load(std::sync::atomic::Ordering::SeqCst) { + return Ok(()); } - server.poll_resends()?; + thread::sleep(Duration::from_millis(1)); } anyhow::bail!("timed out waiting for RUDP document put") }); @@ -1129,6 +1197,232 @@ mod tests { Ok(()) } + #[test] + fn document_publish_fails_when_catalog_is_unreachable() -> Result<()> { + let temp = tempfile::tempdir()?; + let node = CultMesh::create_node( + temp.path().join("cultmesh.cc"), + TestDocuments, + Default::default(), + )?; + let unused = UdpSocket::bind("127.0.0.1:0")?; + let target = unused.local_addr()?; + drop(unused); + + let error = node + .publish_document_to_rudp_catalog( + "note", + &Note { + body: "no false success".into(), + }, + CultMeshRudpDocumentPublishOptions { + target, + connect_timeout: Duration::from_millis(30), + poll_interval: Duration::from_millis(2), + ..CultMeshRudpDocumentPublishOptions::default() + }, + ) + .expect_err("unreachable catalog must fail closed"); + assert!(error.to_string().contains("timed out connecting")); + Ok(()) + } + + #[test] + fn document_publish_requires_application_acknowledgement() -> Result<()> { + let temp = tempfile::tempdir()?; + let node = CultMesh::create_node( + temp.path().join("cultmesh.cc"), + TestDocuments, + Default::default(), + )?; + let socket = UdpSocket::bind("127.0.0.1:0")?; + let target = socket.local_addr()?; + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let server_stop = stop.clone(); + let server = thread::spawn(move || -> Result<()> { + let mut server = CultMeshRudpDocumentServer::new( + socket, + |_receipt: CultMeshRudpRawDocumentReceipt| { + anyhow::bail!("injected durable sink failure") + }, + |_query: &CultMeshRudpSnapshotQuery| Ok(Vec::new()), + CultMeshSystemClock::default(), + CultMeshRudpDocumentServerOptions::default(), + )?; + while !server_stop.load(std::sync::atomic::Ordering::SeqCst) { + let _ = server.poll_once(); + thread::sleep(Duration::from_millis(1)); + } + Ok(()) + }); + + let error = node + .publish_document_to_rudp_catalog( + "note", + &Note { + body: "still not accepted".into(), + }, + CultMeshRudpDocumentPublishOptions { + target, + connect_timeout: Duration::from_millis(100), + flush_timeout: Duration::from_millis(50), + poll_interval: Duration::from_millis(2), + resend_delay_ms: 5, + ..CultMeshRudpDocumentPublishOptions::default() + }, + ) + .expect_err("unacknowledged document must fail closed"); + stop.store(true, std::sync::atomic::Ordering::SeqCst); + server.join().expect("server thread should not panic")?; + assert!(error.to_string().contains("reliable acknowledgement")); + Ok(()) + } + + #[test] + fn raw_snapshot_preserves_caller_challenge_and_raw_response() -> Result<()> { + let expected = cultnet_rs::CultNetRawDocumentRecord { + schema_id: "cultmesh.test.raw.v0".into(), + record_key: "route-proof".into(), + stored_at: "2026-09-03T00:00:00Z".into(), + payload_encoding: cultnet_rs::CultNetRawPayloadEncoding::Messagepack, + payload: vec![0xc4, 0x03, 0, 0xff, 1], + source_runtime_id: Some("odin-test".into()), + source_agent_id: None, + source_role: None, + tags: None, + }; + let socket = UdpSocket::bind("127.0.0.1:0")?; + let target = socket.local_addr()?; + let challenge = "9aa63ad8-83b5-4cc4-ae4a-91d361d5f845".to_string(); + let server_document = expected.clone(); + let server_challenge = challenge.clone(); + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let server_stop = stop.clone(); + let server = thread::spawn(move || -> Result<()> { + let mut server = CultMeshRudpDocumentServer::new( + socket, + |_receipt: CultMeshRudpRawDocumentReceipt| Ok(()), + move |query: &CultMeshRudpSnapshotQuery| { + if query.message_id != server_challenge { + anyhow::bail!("snapshot challenge changed") + } + Ok(vec![server_document.clone()]) + }, + CultMeshSystemClock::default(), + CultMeshRudpDocumentServerOptions::default(), + )?; + while !server_stop.load(std::sync::atomic::Ordering::SeqCst) { + server.poll_once()?; + thread::sleep(Duration::from_millis(1)); + } + Ok(()) + }); + + let response = request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { + target, + runtime_id: "route-proof-test".into(), + message_id: challenge.clone(), + connect_timeout: Duration::from_millis(100), + response_timeout: Duration::from_millis(500), + poll_interval: Duration::from_millis(2), + resend_delay_ms: 5, + schema_ids: Some(vec!["cultmesh.test.raw.v0".into()]), + record_keys: Some(vec!["route-proof".into()]), + }); + stop.store(true, std::sync::atomic::Ordering::SeqCst); + server.join().expect("server thread should not panic")?; + let response = response?; + assert_eq!( + response, + cultnet_rs::CultNetMessage::SnapshotResponseRaw { + message_id: challenge, + documents: vec![expected], + } + ); + Ok(()) + } + + #[test] + fn raw_snapshot_rejects_response_without_request_acknowledgement() -> Result<()> { + let socket = UdpSocket::bind("127.0.0.1:0")?; + socket.set_read_timeout(Some(Duration::from_millis(200)))?; + let target = socket.local_addr()?; + let server = thread::spawn(move || -> Result<()> { + let mut wire = vec![0_u8; 65_535]; + let (received, peer) = socket.recv_from(&mut wire)?; + let connect = cultnet_rs::decode_rudp_packet(&wire[..received])?; + let mut session = + cultnet_rs::CultNetRudpSession::new(cultnet_rs::CultNetRudpSessionOptions { + connection_id: connect.connection_id, + initial_sequence: 1, + resend_delay_ms: 5, + max_pending_reliable_packets: Some(64), + }); + let accept = session.accept_connect(&connect, 1, Vec::new())?; + socket.send_to(&cultnet_rs::encode_rudp_packet(&accept)?, peer)?; + + loop { + let (received, received_peer) = socket.recv_from(&mut wire)?; + if received_peer != peer { + continue; + } + let packet = cultnet_rs::decode_rudp_packet(&wire[..received])?; + let result = session.receive(&packet, 2)?; + let Some(frame) = result.delivered.into_iter().next() else { + continue; + }; + let request = decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + )?; + let cultnet_rs::CultNetMessage::SnapshotRequest { message_id, .. } = request else { + anyhow::bail!("expected snapshot request") + }; + let payload = encode_cultnet_message_to_vec( + &cultnet_rs::CultNetMessage::SnapshotResponseRaw { + message_id, + documents: Vec::new(), + }, + CultNetWireContract::CultNetSchemaV0, + )?; + let mut packets = session.send_many( + "schema", + payload, + cultnet_rs::CultNetRudpSendOptions { + reliable: true, + ordered: true, + sequenced: false, + now_ms: 2, + reliable_expire_after_ms: None, + }, + Some(1200), + )?; + for packet in &mut packets { + packet.ack = 0; + packet.ack_mask = 0; + socket.send_to(&cultnet_rs::encode_rudp_packet(packet)?, peer)?; + } + return Ok(()); + } + }); + + let error = request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { + target, + runtime_id: "unacknowledged-snapshot-test".into(), + message_id: "b7987efb-69c5-4c42-9355-b6e09535ad21".into(), + connect_timeout: Duration::from_millis(100), + response_timeout: Duration::from_millis(50), + poll_interval: Duration::from_millis(2), + resend_delay_ms: 5, + schema_ids: None, + record_keys: None, + }) + .expect_err("response without request ACK must fail closed"); + server.join().expect("server thread should not panic")?; + assert!(error.to_string().contains("reliable acknowledgement")); + Ok(()) + } + #[test] fn resolves_rudp_endpoint_from_cultmesh_uri_bootstrap_env() -> Result<()> { let previous = env::var("CULTMESH_URI_ODIN_RUDP").ok(); @@ -1154,8 +1448,9 @@ mod tests { unsafe { env::remove_var("CULTMESH_URI_MISSING_ODIN_RUDP"); } - let error = CultMesh::resolve_rudp_endpoint("cultmesh://missing-odin/rendezvous/provider-catalog") - .expect_err("unresolved CultMesh URI should fail"); + let error = + CultMesh::resolve_rudp_endpoint("cultmesh://missing-odin/rendezvous/provider-catalog") + .expect_err("unresolved CultMesh URI should fail"); unsafe { if let Some(value) = previous { env::set_var("CULTMESH_URI_MISSING_ODIN_RUDP", value); @@ -1180,25 +1475,23 @@ mod tests { body: "catalog truth".to_string(), })?; - node.cache.put_raw_envelope(cultcache_rs::CultCacheEnvelope { - key: "note".to_string(), - r#type: node - .documents - .binding_by_schema_id("cultmesh.test.note.v0") - .expect("test binding") - .document_type - .clone(), - payload, - stored_at: "2026-07-03T00:00:00Z".to_string(), - schema_id: Some("cultmesh.test.note.v0".to_string()), - })?; + node.cache + .put_raw_envelope(cultcache_rs::CultCacheEnvelope { + key: "note".to_string(), + r#type: node + .documents + .binding_by_schema_id("cultmesh.test.note.v0") + .expect("test binding") + .document_type + .clone(), + payload, + stored_at: "2026-07-03T00:00:00Z".to_string(), + schema_id: Some("cultmesh.test.note.v0".to_string()), + })?; node.flush()?; let reloaded = CultMesh::create_node(&store_path, TestDocuments, Default::default())?; - assert_eq!( - reloaded.get_required::("note")?.body, - "catalog truth" - ); + assert_eq!(reloaded.get_required::("note")?.body, "catalog truth"); assert_eq!(reloaded.cache().snapshot()[0].r#type, Note::TYPE); Ok(()) } @@ -1232,15 +1525,18 @@ mod tests { )?; let negotiation = catalog.negotiate("muninn:nightwing:move-evidence", &consumer)?; - assert_eq!(negotiation.transport, CultMeshStreamBodyTransport::SharedMemory); - assert_eq!(negotiation.copy_budget, CultMeshStreamCopyBudget::ZeroCopyTarget); + assert_eq!( + negotiation.transport, + CultMeshStreamBodyTransport::SharedMemory + ); + assert_eq!( + negotiation.copy_budget, + CultMeshStreamCopyBudget::ZeroCopyTarget + ); let handle = { - let ring = catalog.create_shared_memory_ring( - "muninn:nightwing:move-evidence", - 4, - 128, - )?; + let ring = + catalog.create_shared_memory_ring("muninn:nightwing:move-evidence", 4, 128)?; ring.try_publish_copy(&[1, 2, 3], 42, 0)? .expect("frame should publish") }; diff --git a/vendor/cultmesh-rs/src/rudp_document_server.rs b/vendor/cultmesh-rs/src/rudp_document_server.rs new file mode 100644 index 0000000..bebeb6c --- /dev/null +++ b/vendor/cultmesh-rs/src/rudp_document_server.rs @@ -0,0 +1,647 @@ +use anyhow::{Result, anyhow}; +use cultnet_rs::{ + CultNetMessage, CultNetRawDocumentRecord, CultNetRudpPacket, CultNetRudpPacketType, + CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, CultNetWireContract, + decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, + encode_rudp_packet, +}; +use std::collections::BTreeMap; +use std::io::ErrorKind; +use std::net::{SocketAddr, UdpSocket}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +const MAX_UDP_DATAGRAM_BYTES: usize = 65_535; + +/// The transport identity of one remote CultNet RUDP session. +/// +/// Connection identifiers are scoped to a remote socket address. Two clients +/// may therefore use the same connection identifier without sharing ordering, +/// acknowledgement, resend, or fragment state. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct CultMeshRudpSessionKey { + pub remote_addr: SocketAddr, + pub connection_id: u32, +} + +/// A raw document plus the receipt fact owned by the receiving process. +/// +/// `document.payload` is the exact byte vector decoded from the CultNet raw +/// record. The trusted receipt time lives beside the sender-owned record so the +/// transport never rewrites its metadata or payload. +#[derive(Clone, Debug, PartialEq)] +pub struct CultMeshRudpRawDocumentReceipt { + pub session: CultMeshRudpSessionKey, + pub message_id: String, + pub transport_sequence: u32, + pub received_at_unix_millis: u64, + pub document: CultNetRawDocumentRecord, +} + +/// The caller-visible query for a read-only raw snapshot. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultMeshRudpSnapshotQuery { + pub session: CultMeshRudpSessionKey, + pub message_id: String, + pub requested_at_unix_millis: u64, + pub schema_ids: Option>, + pub record_keys: Option>, +} + +/// Caller-owned admission/persistence port for received raw documents. +pub trait CultMeshRudpRawDocumentSink { + fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()>; +} + +impl CultMeshRudpRawDocumentSink for F +where + F: FnMut(CultMeshRudpRawDocumentReceipt) -> Result<()>, +{ + fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { + self(receipt) + } +} + +/// Caller-owned catalog port for serving raw snapshot requests. +/// +/// The caller decides which records the requester may see. CultMesh preserves +/// those records as raw CultNet documents and does not interpret their schemas. +pub trait CultMeshRudpSnapshotSource { + fn raw_snapshot( + &mut self, + query: &CultMeshRudpSnapshotQuery, + ) -> Result>; +} + +impl CultMeshRudpSnapshotSource for F +where + F: FnMut(&CultMeshRudpSnapshotQuery) -> Result>, +{ + fn raw_snapshot( + &mut self, + query: &CultMeshRudpSnapshotQuery, + ) -> Result> { + self(query) + } +} + +/// Trusted local clocks for receipt facts and transport maintenance. +/// +/// Receipt time is wall time. Expiry and resend decisions use the monotonic +/// value, so a wall-clock correction cannot resurrect or pin a session. +pub trait CultMeshRudpServerClock { + fn now_unix_millis(&self) -> u64; + fn now_monotonic_millis(&self) -> u64; +} + +#[derive(Clone, Debug)] +pub struct CultMeshSystemClock { + monotonic_origin: Instant, +} + +impl Default for CultMeshSystemClock { + fn default() -> Self { + Self { + monotonic_origin: Instant::now(), + } + } +} + +impl CultMeshRudpServerClock for CultMeshSystemClock { + fn now_unix_millis(&self) -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis() + .try_into() + .unwrap_or(u64::MAX) + } + + fn now_monotonic_millis(&self) -> u64 { + self.monotonic_origin + .elapsed() + .as_millis() + .try_into() + .unwrap_or(u64::MAX) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultMeshRudpDocumentServerOptions { + pub max_sessions: usize, + pub session_idle_timeout: Duration, + pub session_max_lifetime: Duration, + /// Conservative payload admission budget across all live sessions. + /// + /// Admitted ingress and queued snapshot-response bytes are charged until + /// the session ends even when CultNet releases them sooner. Together with + /// CultNet's per-session receive caps, this makes retained payload memory + /// finite without duplicating its fragment/ordering machinery here. + pub max_admitted_payload_bytes: usize, + pub max_admitted_payload_bytes_per_session: usize, + pub max_snapshot_response_bytes: usize, + pub max_snapshot_documents: usize, + pub resend_delay: Duration, + pub max_pending_reliable_packets_per_session: usize, + pub max_fragment_bytes: usize, +} + +impl Default for CultMeshRudpDocumentServerOptions { + fn default() -> Self { + Self { + max_sessions: 64, + session_idle_timeout: Duration::from_secs(30), + session_max_lifetime: Duration::from_secs(15 * 60), + max_admitted_payload_bytes: 32 * 1024 * 1024, + max_admitted_payload_bytes_per_session: 4 * 1024 * 1024, + max_snapshot_response_bytes: 1024 * 1024, + max_snapshot_documents: 4096, + resend_delay: Duration::from_millis(50), + max_pending_reliable_packets_per_session: 1024, + max_fragment_bytes: 1200, + } + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct CultMeshRudpMaintenance { + pub sessions_expired: usize, + pub packets_resent: usize, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CultMeshRudpApplicationOperation { + DocumentPutRaw, + SnapshotRequest, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CultMeshRudpApplicationRejection { + pub session: CultMeshRudpSessionKey, + pub operation: CultMeshRudpApplicationOperation, + pub message_id: String, + pub reason: String, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum CultMeshRudpPollOutcome { + Idle, + Handled, + ApplicationRejected(CultMeshRudpApplicationRejection), +} + +struct SessionEntry { + session: CultNetRudpSession, + created_at_monotonic_millis: u64, + last_activity_monotonic_millis: u64, + admitted_payload_bytes: usize, +} + +/// A synchronous, multi-session CultNet RUDP raw-document server. +/// +/// The server owns exactly one UDP socket. Call `poll_once` from an existing +/// daemon loop; no executor or background thread is created here. +pub struct CultMeshRudpDocumentServer { + socket: UdpSocket, + sessions: BTreeMap, + sink: S, + snapshot_source: Q, + clock: C, + options: CultMeshRudpDocumentServerOptions, +} + +impl CultMeshRudpDocumentServer +where + S: CultMeshRudpRawDocumentSink, + Q: CultMeshRudpSnapshotSource, + C: CultMeshRudpServerClock, +{ + pub fn new( + socket: UdpSocket, + sink: S, + snapshot_source: Q, + clock: C, + options: CultMeshRudpDocumentServerOptions, + ) -> Result { + validate_options(&options)?; + socket.local_addr().map_err(|error| { + anyhow!("CultMesh RUDP server requires a bound UDP socket: {error}") + })?; + socket.set_nonblocking(true)?; + Ok(Self { + socket, + sessions: BTreeMap::new(), + sink, + snapshot_source, + clock, + options, + }) + } + + pub fn local_addr(&self) -> Result { + Ok(self.socket.local_addr()?) + } + + pub fn session_count(&self) -> usize { + self.sessions.len() + } + + /// Poll transport maintenance and receive at most one UDP datagram. + /// + /// Application rejection invalidates only the responsible peer session and + /// is returned as data so a daemon can log it and keep serving. Local + /// socket and server-state failures remain errors. + pub fn poll_once(&mut self) -> Result { + self.maintain()?; + let mut wire = vec![0_u8; MAX_UDP_DATAGRAM_BYTES]; + let (received, remote_addr) = match self.socket.recv_from(&mut wire) { + Ok(value) => value, + Err(error) if error.kind() == ErrorKind::WouldBlock => { + return Ok(CultMeshRudpPollOutcome::Idle); + } + Err(error) => return Err(error.into()), + }; + wire.truncate(received); + + let packet = match decode_rudp_packet(&wire) { + Ok(packet) => packet, + Err(_) => return Ok(CultMeshRudpPollOutcome::Handled), + }; + let now_unix = self.clock.now_unix_millis(); + let now_monotonic = self.clock.now_monotonic_millis(); + let key = CultMeshRudpSessionKey { + remote_addr, + connection_id: packet.connection_id, + }; + + if packet.packet_type == CultNetRudpPacketType::Connect { + self.accept_connection(key, &packet, now_monotonic)?; + return Ok(CultMeshRudpPollOutcome::Handled); + } + + if !self.sessions.contains_key(&key) { + return Ok(CultMeshRudpPollOutcome::Handled); + } + if packet.packet_type == CultNetRudpPacketType::Data + && packet.channel_id == "schema" + && !packet.reliable + { + return Ok(CultMeshRudpPollOutcome::Handled); + } + let payload_charge = if packet.packet_type == CultNetRudpPacketType::Data { + packet.payload.len() + } else { + 0 + }; + if !self.payload_budget_allows(key, payload_charge) { + return Ok(CultMeshRudpPollOutcome::Handled); + } + + let result = { + let entry = self + .sessions + .get_mut(&key) + .expect("checked session must remain present"); + entry.last_activity_monotonic_millis = now_monotonic; + entry.admitted_payload_bytes = + entry.admitted_payload_bytes.saturating_add(payload_charge); + entry.session.receive(&packet, now_monotonic) + }; + let result = match result { + Ok(result) => result, + Err(_) => { + self.sessions.remove(&key); + return Ok(CultMeshRudpPollOutcome::Handled); + } + }; + + if let Some(reply) = result.reply { + self.send_packet(key.remote_addr, &reply)?; + } + + if result.disconnected { + self.sessions.remove(&key); + return Ok(CultMeshRudpPollOutcome::Handled); + } + + for frame in result.delivered { + if frame.channel_id != "schema" { + continue; + } + let message = match decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + ) { + Ok(message) => message, + Err(_) => continue, + }; + if let Some(rejection) = self.deliver_application_message( + key, + frame.sequence, + message, + now_unix, + now_monotonic, + )? { + self.sessions.remove(&key); + return Ok(CultMeshRudpPollOutcome::ApplicationRejected(rejection)); + } + } + + if packet.reliable { + let ack = self + .sessions + .get_mut(&key) + .ok_or_else(|| anyhow!("CultMesh RUDP session disappeared before ACK"))? + .session + .create_ack(); + self.send_packet(key.remote_addr, &ack)?; + } + Ok(CultMeshRudpPollOutcome::Handled) + } + + /// Expire idle sessions and resend reliable packets whose deadline passed. + pub fn maintain(&mut self) -> Result { + let now = self.clock.now_monotonic_millis(); + let idle_timeout_ms = duration_millis(self.options.session_idle_timeout); + let lifetime_ms = duration_millis(self.options.session_max_lifetime); + let before = self.sessions.len(); + self.sessions.retain(|_, entry| { + now.saturating_sub(entry.last_activity_monotonic_millis) <= idle_timeout_ms + && now.saturating_sub(entry.created_at_monotonic_millis) <= lifetime_ms + }); + let expired = before - self.sessions.len(); + + let mut resends = Vec::new(); + for (key, entry) in &mut self.sessions { + for packet in entry.session.due_resends(now) { + resends.push((key.remote_addr, packet)); + } + } + for (remote_addr, packet) in &resends { + self.send_packet(*remote_addr, packet)?; + } + Ok(CultMeshRudpMaintenance { + sessions_expired: expired, + packets_resent: resends.len(), + }) + } + + fn accept_connection( + &mut self, + key: CultMeshRudpSessionKey, + packet: &CultNetRudpPacket, + now: u64, + ) -> Result<()> { + if !self.sessions.contains_key(&key) { + if self.sessions.len() >= self.options.max_sessions { + return Ok(()); + } + self.sessions.insert( + key, + SessionEntry { + session: CultNetRudpSession::new(CultNetRudpSessionOptions { + connection_id: key.connection_id, + initial_sequence: 1, + resend_delay_ms: duration_millis(self.options.resend_delay), + max_pending_reliable_packets: Some( + self.options.max_pending_reliable_packets_per_session, + ), + }), + created_at_monotonic_millis: now, + last_activity_monotonic_millis: now, + admitted_payload_bytes: 0, + }, + ); + } + + let accept = { + let entry = self + .sessions + .get_mut(&key) + .expect("accepted session must be present"); + // The same key is a retransmitted Connect for the existing epoch. + // A genuinely fresh client incarnation must choose a fresh id. + entry.last_activity_monotonic_millis = now; + entry.session.accept_connect(packet, now, Vec::new())? + }; + self.send_packet(key.remote_addr, &accept) + } + + fn deliver_application_message( + &mut self, + key: CultMeshRudpSessionKey, + transport_sequence: u32, + message: CultNetMessage, + now_unix: u64, + now: u64, + ) -> Result> { + match message { + CultNetMessage::DocumentPutRaw { + message_id, + document, + } => { + let receipt = CultMeshRudpRawDocumentReceipt { + session: key, + message_id: message_id.clone(), + transport_sequence, + received_at_unix_millis: now_unix, + document, + }; + if let Err(error) = self.sink.accept_raw_document(receipt) { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::DocumentPutRaw, + message_id, + reason: format!("{error:#}"), + })); + } + } + CultNetMessage::SnapshotRequest { + message_id, + schema_ids, + record_keys, + } => { + let query = CultMeshRudpSnapshotQuery { + session: key, + message_id: message_id.clone(), + requested_at_unix_millis: now_unix, + schema_ids, + record_keys, + }; + let documents = match self.snapshot_source.raw_snapshot(&query) { + Ok(documents) => documents, + Err(error) => { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: format!("{error:#}"), + })); + } + }; + if documents.len() > self.options.max_snapshot_documents { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: format!( + "snapshot returned {} documents; limit is {}", + documents.len(), + self.options.max_snapshot_documents + ), + })); + } + let response = CultNetMessage::SnapshotResponseRaw { + message_id: message_id.clone(), + documents, + }; + let payload = match encode_cultnet_message_to_vec( + &response, + CultNetWireContract::CultNetSchemaV0, + ) { + Ok(payload) => payload, + Err(error) => { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: format!("snapshot response could not be encoded: {error:#}"), + })); + } + }; + if payload.len() > self.options.max_snapshot_response_bytes { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: format!( + "snapshot response is {} bytes; limit is {}", + payload.len(), + self.options.max_snapshot_response_bytes + ), + })); + } + if !self.payload_budget_allows(key, payload.len()) { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: "retained payload budget is full".into(), + })); + } + let payload_bytes = payload.len(); + let entry = self + .sessions + .get_mut(&key) + .ok_or_else(|| anyhow!("CultMesh RUDP session disappeared before response"))?; + let packets = match entry.session.send_many( + "schema", + payload, + CultNetRudpSendOptions { + reliable: true, + ordered: true, + sequenced: false, + now_ms: now, + reliable_expire_after_ms: None, + }, + Some(self.options.max_fragment_bytes), + ) { + Ok(packets) => packets, + Err(error) => { + return Ok(Some(CultMeshRudpApplicationRejection { + session: key, + operation: CultMeshRudpApplicationOperation::SnapshotRequest, + message_id, + reason: format!("snapshot response could not be queued: {error:#}"), + })); + } + }; + entry.admitted_payload_bytes = + entry.admitted_payload_bytes.saturating_add(payload_bytes); + for packet in &packets { + self.send_packet(key.remote_addr, packet)?; + } + } + _ => {} + } + Ok(None) + } + + fn payload_budget_allows(&self, key: CultMeshRudpSessionKey, bytes: usize) -> bool { + let Some(session_bytes) = self + .sessions + .get(&key) + .map(|entry| entry.admitted_payload_bytes) + else { + return false; + }; + if session_bytes + .checked_add(bytes) + .is_none_or(|total| total > self.options.max_admitted_payload_bytes_per_session) + { + return false; + } + self.sessions + .values() + .try_fold(0_usize, |total, entry| { + total.checked_add(entry.admitted_payload_bytes) + }) + .and_then(|total| total.checked_add(bytes)) + .is_some_and(|total| total <= self.options.max_admitted_payload_bytes) + } + + fn send_packet(&mut self, remote_addr: SocketAddr, packet: &CultNetRudpPacket) -> Result<()> { + let wire = encode_rudp_packet(packet)?; + self.socket.send_to(&wire, remote_addr)?; + Ok(()) + } +} + +fn validate_options(options: &CultMeshRudpDocumentServerOptions) -> Result<()> { + if options.max_sessions == 0 { + return Err(anyhow!("max_sessions must be greater than zero")); + } + if options.session_idle_timeout.is_zero() { + return Err(anyhow!("session_idle_timeout must be greater than zero")); + } + if options.session_max_lifetime.is_zero() { + return Err(anyhow!("session_max_lifetime must be greater than zero")); + } + if options.max_admitted_payload_bytes == 0 { + return Err(anyhow!( + "max_admitted_payload_bytes must be greater than zero" + )); + } + if options.max_admitted_payload_bytes_per_session == 0 + || options.max_admitted_payload_bytes_per_session > options.max_admitted_payload_bytes + { + return Err(anyhow!( + "per-session admitted payload limit must be non-zero and no larger than the server limit" + )); + } + if options.max_snapshot_response_bytes == 0 + || options.max_snapshot_response_bytes > options.max_admitted_payload_bytes_per_session + { + return Err(anyhow!( + "snapshot response limit must be non-zero and fit in one session's payload budget" + )); + } + if options.max_snapshot_documents == 0 { + return Err(anyhow!("max_snapshot_documents must be greater than zero")); + } + if options.resend_delay.is_zero() { + return Err(anyhow!("resend_delay must be greater than zero")); + } + if options.max_pending_reliable_packets_per_session == 0 { + return Err(anyhow!( + "max_pending_reliable_packets_per_session must be greater than zero" + )); + } + if options.max_fragment_bytes == 0 { + return Err(anyhow!("max_fragment_bytes must be greater than zero")); + } + Ok(()) +} + +fn duration_millis(duration: Duration) -> u64 { + duration.as_millis().try_into().unwrap_or(u64::MAX) +} diff --git a/vendor/cultmesh-rs/tests/rudp_document_server.rs b/vendor/cultmesh-rs/tests/rudp_document_server.rs new file mode 100644 index 0000000..1e57aaf --- /dev/null +++ b/vendor/cultmesh-rs/tests/rudp_document_server.rs @@ -0,0 +1,553 @@ +use anyhow::Result; +use cultmesh_rs::{ + CultMeshRudpApplicationOperation, CultMeshRudpDocumentServer, + CultMeshRudpDocumentServerOptions, CultMeshRudpPollOutcome, CultMeshRudpRawDocumentReceipt, + CultMeshRudpRawDocumentSink, CultMeshRudpServerClock, CultMeshRudpSnapshotQuery, + CultMeshRudpSnapshotSource, +}; +use cultnet_rs::{ + CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, + CultNetRudpReliableSendReceipt, CultNetRudpReliableSendStatus, CultNetRudpSocketMode, + CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, + decode_cultnet_message_from_slice, encode_cultnet_message_to_vec, +}; +use pretty_assertions::assert_eq; +use std::net::{SocketAddr, UdpSocket}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; + +#[derive(Default)] +struct SinkState { + receipts: Vec, + failures_remaining: usize, +} + +#[derive(Clone, Default)] +struct Sink(Arc>); + +impl Sink { + fn fail_once() -> Self { + Self(Arc::new(Mutex::new(SinkState { + failures_remaining: 1, + ..Default::default() + }))) + } +} + +impl CultMeshRudpRawDocumentSink for Sink { + fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { + let mut state = self.0.lock().unwrap(); + if state.failures_remaining > 0 { + state.failures_remaining -= 1; + anyhow::bail!("injected sink failure"); + } + state.receipts.push(receipt); + Ok(()) + } +} + +#[derive(Default)] +struct SourceState { + documents: Vec, + failures_remaining: usize, + calls: usize, +} + +#[derive(Clone, Default)] +struct Source(Arc>); + +impl Source { + fn documents(documents: Vec) -> Self { + Self(Arc::new(Mutex::new(SourceState { + documents, + ..Default::default() + }))) + } + + fn fail_once(documents: Vec) -> Self { + Self(Arc::new(Mutex::new(SourceState { + documents, + failures_remaining: 1, + calls: 0, + }))) + } +} + +impl CultMeshRudpSnapshotSource for Source { + fn raw_snapshot( + &mut self, + _: &CultMeshRudpSnapshotQuery, + ) -> Result> { + let mut state = self.0.lock().unwrap(); + state.calls += 1; + if state.failures_remaining > 0 { + state.failures_remaining -= 1; + anyhow::bail!("injected source failure"); + } + Ok(state.documents.clone()) + } +} + +#[derive(Clone)] +struct Clock { + unix: Arc, + monotonic: Arc, +} + +impl Clock { + fn new(now: u64) -> Self { + Self { + unix: Arc::new(AtomicU64::new(now)), + monotonic: Arc::new(AtomicU64::new(now)), + } + } + + fn set(&self, now: u64) { + self.unix.store(now, Ordering::SeqCst); + self.monotonic.store(now, Ordering::SeqCst); + } + + fn set_unix(&self, now: u64) { + self.unix.store(now, Ordering::SeqCst); + } + + fn set_monotonic(&self, now: u64) { + self.monotonic.store(now, Ordering::SeqCst); + } +} + +impl CultMeshRudpServerClock for Clock { + fn now_unix_millis(&self) -> u64 { + self.unix.load(Ordering::SeqCst) + } + + fn now_monotonic_millis(&self) -> u64 { + self.monotonic.load(Ordering::SeqCst) + } +} + +type Server = CultMeshRudpDocumentServer; + +fn document(key: &str, payload: Vec) -> CultNetRawDocumentRecord { + CultNetRawDocumentRecord { + schema_id: "test.raw.v1".into(), + record_key: key.into(), + stored_at: "2026-09-03T00:00:00Z".into(), + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload, + source_runtime_id: Some(format!("runtime-{key}")), + source_agent_id: None, + source_role: None, + tags: None, + } +} + +fn server( + options: CultMeshRudpDocumentServerOptions, + clock: Clock, + sink: Sink, + source: Source, +) -> Result { + CultMeshRudpDocumentServer::new( + UdpSocket::bind("127.0.0.1:0")?, + sink, + source, + clock, + options, + ) +} + +fn client(target: SocketAddr, id: u32) -> Result { + let socket = UdpSocket::bind("127.0.0.1:0")?; + socket.set_nonblocking(true)?; + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { + runtime_id: "test-client".into(), + socket, + mode: CultNetRudpSocketMode::Client, + remote_addr: Some(target), + connection_id: id, + initial_sequence: 1, + resend_delay_ms: 10, + transport_id: None, + max_payload_bytes: None, + max_fragment_bytes: Some(1200), + max_pending_reliable_packets: Some(64), + media_reliable_expire_after_ms: None, + }) +} + +fn connect( + server: &mut Server, + clients: &mut [&mut CultNetRudpSocketTransportConnection], +) -> Result<()> { + for client in &mut *clients { + client.connect(Vec::new())?; + } + for _ in 0..500 { + server.poll_once()?; + for client in &mut *clients { + client.receive_once()?; + } + if clients.iter().all(|client| client.connected()) { + for _ in 0..clients.len() { + server.poll_once()?; + } + return Ok(()); + } + thread::sleep(Duration::from_millis(1)); + } + anyhow::bail!("client connection timed out") +} + +fn send(client: &mut CultNetRudpSocketTransportConnection, message: &CultNetMessage) -> Result<()> { + client.send( + "schema", + encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?, + ) +} + +fn send_reliable( + client: &mut CultNetRudpSocketTransportConnection, + message: &CultNetMessage, +) -> Result { + client.send_reliable( + "schema", + encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?, + ) +} + +#[test] +fn same_connection_id_is_peer_scoped_and_raw_bytes_are_untouched() -> Result<()> { + let clock = Clock::new(42_000); + let sink = Sink::default(); + let mut server = server(Default::default(), clock, sink.clone(), Source::default())?; + let mut a = client(server.local_addr()?, 77)?; + let mut b = client(server.local_addr()?, 77)?; + connect(&mut server, &mut [&mut a, &mut b])?; + + let a_bytes = vec![0xc4, 0x04, 0x00, 0xff, 0x80, 0x01]; + let b_bytes = vec![0x92, 0x01, 0xc4, 0x02, 0x00, 0xff]; + send( + &mut a, + &CultNetMessage::DocumentPutRaw { + message_id: "a".into(), + document: document("a", a_bytes.clone()), + }, + )?; + send( + &mut b, + &CultNetMessage::DocumentPutRaw { + message_id: "b".into(), + document: document("b", b_bytes.clone()), + }, + )?; + for _ in 0..500 { + server.poll_once()?; + a.receive_once()?; + b.receive_once()?; + if sink.0.lock().unwrap().receipts.len() == 2 { + break; + } + } + + let receipts = sink.0.lock().unwrap(); + assert_eq!(server.session_count(), 2); + assert_eq!(receipts.receipts.len(), 2); + assert_ne!( + receipts.receipts[0].session.remote_addr, + receipts.receipts[1].session.remote_addr + ); + assert_eq!( + receipts + .receipts + .iter() + .find(|r| r.message_id == "a") + .unwrap() + .document + .payload, + a_bytes + ); + assert_eq!( + receipts + .receipts + .iter() + .find(|r| r.message_id == "b") + .unwrap() + .document + .payload, + b_bytes + ); + assert!( + receipts + .receipts + .iter() + .all(|r| r.received_at_unix_millis == 42_000) + ); + Ok(()) +} + +#[test] +fn duplicate_connect_preserves_epoch_and_fresh_epoch_is_separate() -> Result<()> { + let clock = Clock::new(45_000); + let sink = Sink::default(); + let mut server = server(Default::default(), clock, sink.clone(), Source::default())?; + let target = server.local_addr()?; + let mut incumbent = client(target, 91)?; + connect(&mut server, &mut [&mut incumbent])?; + + incumbent.connect(Vec::new())?; + server.poll_once()?; + incumbent.receive_once()?; + assert_eq!(server.session_count(), 1); + + send( + &mut incumbent, + &CultNetMessage::DocumentPutRaw { + message_id: "after-duplicate".into(), + document: document("incumbent", vec![1, 2, 3]), + }, + )?; + for _ in 0..20 { + server.poll_once()?; + if sink.0.lock().unwrap().receipts.len() == 1 { + break; + } + } + assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); + + let mut fresh_epoch = client(target, 92)?; + connect(&mut server, &mut [&mut fresh_epoch])?; + assert_eq!(server.session_count(), 2); + Ok(()) +} + +#[test] +fn application_rejection_is_nonfatal_peer_scoped_and_unacknowledged() -> Result<()> { + let clock = Clock::new(48_000); + let sink = Sink::fail_once(); + let source = Source::fail_once(vec![document("snapshot", vec![4, 5, 6])]); + let mut server = server( + Default::default(), + clock.clone(), + sink.clone(), + source.clone(), + )?; + let target = server.local_addr()?; + + let mut publisher = client(target, 101)?; + let mut snapshot_client = client(target, 102)?; + let mut survivor = client(target, 103)?; + connect( + &mut server, + &mut [&mut publisher, &mut snapshot_client, &mut survivor], + )?; + let publish_receipt = send_reliable( + &mut publisher, + &CultNetMessage::DocumentPutRaw { + message_id: "rejected-put".into(), + document: document("rejected", vec![0, 255, 1]), + }, + )?; + let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = server.poll_once()? else { + panic!("sink rejection must be returned as a nonfatal poll outcome"); + }; + assert_eq!(rejection.session.connection_id, 101); + assert_eq!( + rejection.operation, + CultMeshRudpApplicationOperation::DocumentPutRaw + ); + assert_eq!(rejection.message_id, "rejected-put"); + assert!(rejection.reason.contains("injected sink failure")); + assert_eq!(server.session_count(), 2); + publisher.receive_once()?; + assert_eq!( + publisher.reliable_send_status(&publish_receipt), + CultNetRudpReliableSendStatus::Pending + ); + + let snapshot_receipt = send_reliable( + &mut snapshot_client, + &CultNetMessage::SnapshotRequest { + message_id: "rejected-snapshot".into(), + schema_ids: None, + record_keys: None, + }, + )?; + let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = server.poll_once()? else { + panic!("source rejection must be returned as a nonfatal poll outcome"); + }; + assert_eq!(rejection.session.connection_id, 102); + assert_eq!( + rejection.operation, + CultMeshRudpApplicationOperation::SnapshotRequest + ); + assert_eq!(rejection.message_id, "rejected-snapshot"); + assert!(rejection.reason.contains("injected source failure")); + assert_eq!(server.session_count(), 1); + snapshot_client.receive_once()?; + assert_eq!( + snapshot_client.reliable_send_status(&snapshot_receipt), + CultNetRudpReliableSendStatus::Pending + ); + + let survivor_receipt = send_reliable( + &mut survivor, + &CultNetMessage::DocumentPutRaw { + message_id: "accepted-after-rejections".into(), + document: document("survivor", vec![9, 8, 7]), + }, + )?; + assert_eq!(server.poll_once()?, CultMeshRudpPollOutcome::Handled); + for _ in 0..20 { + survivor.receive_once()?; + if survivor.reliable_send_status(&survivor_receipt) + == CultNetRudpReliableSendStatus::Acknowledged + { + break; + } + } + assert_eq!( + survivor.reliable_send_status(&survivor_receipt), + CultNetRudpReliableSendStatus::Acknowledged + ); + assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); + assert_eq!(source.0.lock().unwrap().calls, 1); + Ok(()) +} + +#[test] +fn snapshot_response_resends_until_acknowledged() -> Result<()> { + let clock = Clock::new(51_000); + let expected = document("catalog", vec![0x81, 0xa1, 0x78, 0x2a]); + let source = Source::documents(vec![expected.clone()]); + let options = CultMeshRudpDocumentServerOptions { + resend_delay: Duration::from_millis(10), + ..Default::default() + }; + let mut server = server(options, clock.clone(), Sink::default(), source)?; + let mut client = client(server.local_addr()?, 88)?; + connect(&mut server, &mut [&mut client])?; + send( + &mut client, + &CultNetMessage::SnapshotRequest { + message_id: "snapshot".into(), + schema_ids: Some(vec!["test.raw.v1".into()]), + record_keys: Some(vec!["catalog".into()]), + }, + )?; + server.poll_once()?; + + clock.set(51_011); + assert_eq!(server.maintain()?.packets_resent, 1); + let response = loop { + if let Some(frame) = client.receive_once()? { + break decode_cultnet_message_from_slice( + &frame.payload, + CultNetWireContract::CultNetSchemaV0, + )?; + } + }; + assert_eq!( + response, + CultNetMessage::SnapshotResponseRaw { + message_id: "snapshot".into(), + documents: vec![expected], + } + ); + + for _ in 0..20 { + server.poll_once()?; + } + clock.set(51_022); + assert_eq!(server.maintain()?.packets_resent, 0); + Ok(()) +} + +#[test] +fn payload_and_snapshot_output_budgets_fail_closed() -> Result<()> { + let clock = Clock::new(55_000); + let sink = Sink::default(); + let message = CultNetMessage::DocumentPutRaw { + message_id: "budget".into(), + document: document("budget", vec![7; 64]), + }; + let encoded = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?; + let options = CultMeshRudpDocumentServerOptions { + max_admitted_payload_bytes: encoded.len(), + max_admitted_payload_bytes_per_session: encoded.len(), + max_snapshot_response_bytes: encoded.len(), + ..Default::default() + }; + let mut budget_server = server(options, clock, sink.clone(), Source::default())?; + let target = budget_server.local_addr()?; + let mut first = client(target, 111)?; + let mut second = client(target, 112)?; + connect(&mut budget_server, &mut [&mut first, &mut second])?; + send(&mut first, &message)?; + budget_server.poll_once()?; + send(&mut second, &message)?; + budget_server.poll_once()?; + assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); + + let oversized = Source::documents(vec![document("oversized", vec![8; 512])]); + let options = CultMeshRudpDocumentServerOptions { + max_snapshot_response_bytes: 128, + ..Default::default() + }; + let mut snapshot_server = server(options, Clock::new(56_000), Sink::default(), oversized)?; + let mut snapshot_client = client(snapshot_server.local_addr()?, 113)?; + connect(&mut snapshot_server, &mut [&mut snapshot_client])?; + let receipt = send_reliable( + &mut snapshot_client, + &CultNetMessage::SnapshotRequest { + message_id: "too-large".into(), + schema_ids: None, + record_keys: None, + }, + )?; + let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = snapshot_server.poll_once()? + else { + panic!("oversized snapshot must reject only its session"); + }; + assert_eq!( + rejection.operation, + CultMeshRudpApplicationOperation::SnapshotRequest + ); + assert_eq!(rejection.message_id, "too-large"); + assert!(rejection.reason.contains("snapshot response is")); + assert_eq!(snapshot_server.session_count(), 0); + snapshot_client.receive_once()?; + assert_eq!( + snapshot_client.reliable_send_status(&receipt), + CultNetRudpReliableSendStatus::Pending + ); + Ok(()) +} + +#[test] +fn session_cap_and_expiry_use_monotonic_time() -> Result<()> { + let clock = Clock::new(60_000); + let options = CultMeshRudpDocumentServerOptions { + max_sessions: 1, + session_idle_timeout: Duration::from_secs(1), + session_max_lifetime: Duration::from_millis(100), + ..Default::default() + }; + let mut server = server(options, clock.clone(), Sink::default(), Source::default())?; + let target = server.local_addr()?; + let mut admitted = client(target, 1)?; + connect(&mut server, &mut [&mut admitted])?; + let mut rejected = client(target, 1)?; + rejected.connect(Vec::new())?; + server.poll_once()?; + assert_eq!(server.session_count(), 1); + assert!(!rejected.connected()); + + clock.set_unix(1); + clock.set_monotonic(60_101); + assert_eq!(server.maintain()?.sessions_expired, 1); + assert_eq!(server.session_count(), 0); + Ok(()) +} diff --git a/vendor/cultnet-rs b/vendor/cultnet-rs deleted file mode 160000 index 9dcb88d..0000000 --- a/vendor/cultnet-rs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9dcb88df1361b157a48502f43093fe7a25ca22b7 From bc2fdedcb0604a1125031980bbdde6e126ea978f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Sep 2026 15:08:44 +0200 Subject: [PATCH 87/93] Refuse agent scratch .codex-tmp had grown to 50MB of build tarballs and lockfile snapshots. Co-Authored-By: Claude Opus 5 --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 54bda84..70c0dde 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ target/ *.pid artifacts/ **/artifacts/ + +# Agent scratch. Reached 50MB of tarballs and lockfile snapshots once. +.codex-tmp/ From eedce4f3de2aefccaa6e74bb5770b57cd8c6a87b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Sep 2026 15:13:38 +0200 Subject: [PATCH 88/93] Land the Idunn projection, UDP route actuation, and the Odin daemon This had been sitting uncommitted in the worktree. It is real work rather than stray edits: RouteBinding gains stable_socket and an NginxStreamUdp driver so a route can be actuated over UDP, control_plane and drivers grow the Idunn-owned projection and bounded RUDP transport, and crates/odin-daemon arrives as a new member of the workspace. The deployment recipe comes with it, including the required_gitlinks fix the core-stack repoint made necessary: it named vendor/cultnet-rs, which no longer exists now that CultNet comes from CultLib, so Idunn would have refused to admit the target. The list is empty because Odin has no submodules left; the core stack is pinned by revision in Cargo.lock inside the frozen source tree. Everything except odin-daemon builds here. odin-daemon reads systemd listen file descriptors through std::os::fd and cannot compile on Windows by design, so Linux CI is its first real witness. muninn and sleipnir pass 162 tests; the one failure expects an Unreliable media channel that no CultNet lineage has ever declared, and predates this. Committed rather than left in flight. rustfmt reports 94 files across the repo, which predates this work, so formatting is left for its own pass instead of being buried in a 4,000-line diff. Co-Authored-By: Claude Opus 5 --- Cargo.toml | 1 + crates/idunn-daemon/src/control_plane.rs | 2111 +++++++++++++++++--- crates/idunn-daemon/src/deployment.rs | 144 +- crates/idunn-daemon/src/deployment_plan.rs | 74 +- crates/idunn-daemon/src/drivers.rs | 1826 ++++++++++++++--- crates/idunn-daemon/src/provisioning.rs | 542 ++++- crates/odin-daemon/Cargo.toml | 23 + crates/odin-daemon/src/lib.rs | 1832 +++++++++++++++++ crates/odin-daemon/src/main.rs | 1248 ++++++++++++ deployment/idunn/recipe.toml | 72 + 10 files changed, 7204 insertions(+), 669 deletions(-) create mode 100644 crates/odin-daemon/Cargo.toml create mode 100644 crates/odin-daemon/src/lib.rs create mode 100644 crates/odin-daemon/src/main.rs create mode 100644 deployment/idunn/recipe.toml diff --git a/Cargo.toml b/Cargo.toml index 76fabb0..09a0ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "crates/muninn-move-tracker", "crates/muninn-psmoveapi-tracker", "crates/odin-core", + "crates/odin-daemon", ] resolver = "3" exclude = [ diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs index 225bc27..1f8361b 100644 --- a/crates/idunn-daemon/src/control_plane.rs +++ b/crates/idunn-daemon/src/control_plane.rs @@ -5,35 +5,36 @@ use std::path::{Path, PathBuf}; use std::thread; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -use anyhow::{anyhow, bail, ensure, Context, Result}; +use anyhow::{Context, Result, anyhow, bail, ensure}; use cultcache_rs::{ CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, }; use cultnet_rs::{ - authenticate_odin_runtime_topology_correlation, derive_service_identity_id, - evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, open_service_identity_at, - verify_idunn_deployment_brake_authorization, verify_runtime_authority, AuthenticatedOdinRuntimeTopologyCorrelation, GameCultProviderHealthIdentity, + IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, IdunnDeploymentBrakeRecord, IdunnExpectedIncarnationRecord, IdunnLifecycleBrakeObservation, IdunnLifecycleBrakeRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, IdunnRuntimeActivationRecord, IdunnServiceIdentity, OdinTopologyAuthenticationContext, - OdinTopologyIdentity, ServiceIdentityProfile, ServiceIdentitySigner, - ServiceIdentityTrustAnchor, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCHEMA, - IDUNN_PROCESS_WRITE_LEASE_SCHEMA, + OdinTopologyDisagreement, OdinTopologyIdentity, RuntimePresenceAuthenticationContext, + ServiceIdentityProfile, ServiceIdentitySigner, ServiceIdentityTrustAnchor, + authenticate_odin_runtime_topology_correlation, authenticate_runtime_presence_claim, + correlate_runtime_presence_claim, derive_service_identity_id, + evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, open_service_identity_at, + verify_idunn_deployment_brake_authorization, verify_runtime_authority, }; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use uuid::Uuid; -use crate::deployment::{DependencyKind, OperatorBinding, StartupOrder}; +use crate::deployment::{DependencyKind, OperatorBinding, RouteBinding, capability_compatible}; use crate::deployment_plan::{ - compile_deployment_plan, CompiledDeploymentPlan, DependencyProviderAuthority, SealedRelease, + CompiledDeploymentPlan, DependencyProviderAuthority, SealedRelease, compile_deployment_plan, }; use crate::drivers::{ CultCacheTopologyDriver, CultCacheWriteLeaseDriver, DockerRunnerDriver, FrozenSourceReceipt, GitSourceDriver, InstalledReleaseObservation, NginxRouteDriver, ProcessIdentity, - RouteObservation, RoutePort, RoutePreflightReceipt, RunnerPort, SourcePort, + RouteObservation, RoutePreflightReceipt, RunnerPort, SourcePort, SystemdTransientWorkloadDriver, TopologyPort, WorkloadObservation, WorkloadPort, WriteLeasePort, }; @@ -188,17 +189,127 @@ impl TopologyEvidence { } } -/// Opaque capability minted only after the transaction CAS advances the Odin -/// replay cursor and stores these exact bytes. Adjacent drivers may inspect the -/// authenticated receipt, but cannot construct replay admission themselves. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct RuntimePresenceEvidence { + canonical_bytes: Vec, + canonical_sha256: String, + message_id: String, + challenged_at_unix_millis: u64, + admitted_at_unix_millis: u64, +} + +impl RuntimePresenceEvidence { + fn from_present( + present: &cultnet_rs::VerifiedRuntimePresence, + message_id: String, + challenged_at_unix_millis: u64, + admitted_at_unix_millis: u64, + ) -> Result { + let evidence = Self { + canonical_bytes: present.canonical_bytes().to_vec(), + canonical_sha256: present.signed_presence_sha256().to_owned(), + message_id, + challenged_at_unix_millis, + admitted_at_unix_millis, + }; + evidence.validate_shape()?; + Ok(evidence) + } + + fn validate_shape(&self) -> Result<()> { + ensure!( + !self.canonical_bytes.is_empty() + && self.canonical_sha256 == sha256_id(&self.canonical_bytes), + "runtime presence evidence bytes or digest are invalid" + ); + require_id(&self.message_id, "runtime presence challenge")?; + ensure!( + self.challenged_at_unix_millis > 0 + && self.admitted_at_unix_millis >= self.challenged_at_unix_millis, + "runtime presence evidence timeline is invalid" + ); + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "source", rename_all = "kebab-case", deny_unknown_fields)] +enum WarmingEvidence { + OdinTopology { evidence: TopologyEvidence }, + FirstOdinDirect { evidence: RuntimePresenceEvidence }, +} + +impl WarmingEvidence { + fn validate_shape(&self) -> Result<()> { + match self { + Self::OdinTopology { evidence } => evidence.validate_shape(), + Self::FirstOdinDirect { evidence } => evidence.validate_shape(), + } + } + + fn canonical_sha256(&self) -> &str { + match self { + Self::OdinTopology { evidence } => &evidence.canonical_sha256, + Self::FirstOdinDirect { evidence } => &evidence.canonical_sha256, + } + } +} + +/// Opaque capability minted only after the transaction CAS stores exact +/// Warming evidence. Adjacent drivers receive only the candidate identity and +/// signed-presence digest needed to bind the write lease; they cannot inspect +/// or construct semantic admission. #[derive(Clone, Debug)] pub struct SequenceAdmittedWarming { transaction_id: String, - evidence: TopologyEvidence, - authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, + evidence: WarmingEvidence, + signed_presence_sha256: String, + runtime_instance_id: String, } impl SequenceAdmittedWarming { + fn from_topology( + transaction_id: String, + evidence: TopologyEvidence, + authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, + ) -> Result { + let record = authenticated.record(); + let signed_presence_sha256 = record + .signed_presence_sha256 + .clone() + .context("Warming topology has no signed presence")?; + let runtime_instance_id = record + .runtime_instance_id + .clone() + .context("Warming topology has no runtime instance")?; + Ok(Self { + transaction_id, + evidence: WarmingEvidence::OdinTopology { evidence }, + signed_presence_sha256, + runtime_instance_id, + }) + } + + fn from_first_odin_presence( + transaction_id: String, + evidence: RuntimePresenceEvidence, + present: cultnet_rs::VerifiedRuntimePresence, + ) -> Result { + ensure!( + present.canonical_bytes() == evidence.canonical_bytes + && present.signed_presence_sha256() == evidence.canonical_sha256, + "direct first-Odin Warming evidence differs from its authenticated presence" + ); + let runtime_instance_id = present.record().runtime_instance_id.clone(); + Ok(Self { + transaction_id, + signed_presence_sha256: evidence.canonical_sha256.clone(), + evidence: WarmingEvidence::FirstOdinDirect { evidence }, + runtime_instance_id, + }) + } + #[cfg(test)] pub(crate) fn for_test( transaction_id: impl Into, @@ -218,15 +329,21 @@ impl SequenceAdmittedWarming { require_id(&transaction_id, "test transaction id")?; let evidence = TopologyEvidence::from_authenticated(&authenticated, admitted_at_unix_millis)?; - Ok(Self { - transaction_id, - evidence, - authenticated, - }) - } - - pub(crate) fn authenticated(&self) -> &AuthenticatedOdinRuntimeTopologyCorrelation { - &self.authenticated + let signed_presence_sha256 = record + .signed_presence_sha256 + .clone() + .context("test Warming receipt has no signed presence")?; + let runtime_instance_id = record + .runtime_instance_id + .clone() + .context("test Warming receipt has no runtime instance")?; + let token = Self::from_topology(transaction_id, evidence, authenticated)?; + ensure!( + token.signed_presence_sha256 == signed_presence_sha256 + && token.runtime_instance_id == runtime_instance_id, + "test Warming token extraction changed" + ); + Ok(token) } pub(crate) fn transaction_id(&self) -> &str { @@ -234,7 +351,15 @@ impl SequenceAdmittedWarming { } pub(crate) fn evidence_sha256(&self) -> &str { - &self.evidence.canonical_sha256 + self.evidence.canonical_sha256() + } + + pub(crate) fn signed_presence_sha256(&self) -> &str { + &self.signed_presence_sha256 + } + + pub(crate) fn runtime_instance_id(&self) -> &str { + &self.runtime_instance_id } } @@ -352,14 +477,27 @@ impl LeasingEvidence { #[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] enum RoutingEvidence { SkippedUnrouted, - Promoted { observation: RouteObservation }, + Promoted { + observation: RouteObservation, + promoted_at_unix_millis: u64, + }, } impl RoutingEvidence { fn observation(&self) -> Option<&RouteObservation> { match self { Self::SkippedUnrouted => None, - Self::Promoted { observation } => Some(observation), + Self::Promoted { observation, .. } => Some(observation), + } + } + + fn promoted_at_unix_millis(&self) -> Option { + match self { + Self::SkippedUnrouted => None, + Self::Promoted { + promoted_at_unix_millis, + .. + } => Some(*promoted_at_unix_millis), } } } @@ -464,7 +602,7 @@ impl PostCommitCleanup { #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.deployment_transaction", - schema = "idunn.deployment_transaction.v1" + schema = "idunn.deployment_transaction.v2" )] struct DeploymentTransaction { #[cultcache(key = 0)] @@ -512,7 +650,7 @@ struct DeploymentTransaction { #[cultcache(key = 21)] latest_odin_observation: Option, #[cultcache(key = 22)] - warming: Option, + warming: Option, #[cultcache(key = 23)] route_preflight: Option, #[cultcache(key = 24)] @@ -641,6 +779,17 @@ impl DeploymentTransaction { self.phase != DeploymentPhase::Complete } + /// Admission has moved to the new generation at Complete, but an exact + /// draining incumbent still reserves its process and candidate endpoint. + /// No later mutation for the same target may overlap that cleanup. + fn blocks_new_target_mutation(&self) -> bool { + self.owns_target_authority() + || self + .post_commit_cleanup + .as_ref() + .is_some_and(|cleanup| !cleanup.is_complete()) + } + fn validate(&self) -> Result<()> { ensure!( self.schema_version == DEPLOYMENT_TRANSACTION_SCHEMA, @@ -712,7 +861,7 @@ impl DeploymentTransaction { self.workload.is_none() || self.activation.is_some(), "workload observation exists without its prepared activation" ); - for evidence in [&self.latest_odin_observation, &self.warming, &self.ready] + for evidence in [&self.latest_odin_observation, &self.ready] .into_iter() .flatten() { @@ -722,6 +871,19 @@ impl DeploymentTransaction { "topology evidence exceeds the transaction replay cursor" ); } + if let Some(warming) = &self.warming { + warming.validate_shape()?; + match warming { + WarmingEvidence::OdinTopology { evidence } => ensure!( + evidence.publisher_sequence <= self.odin_publisher_sequence_cursor, + "Warming topology evidence exceeds the transaction replay cursor" + ), + WarmingEvidence::FirstOdinDirect { .. } => ensure!( + self.target == "odin" && self.incumbent_generation_id.is_none(), + "direct Warming evidence is reserved for first Odin bootstrap" + ), + } + } if let Some(leasing) = &self.leasing { if let Some(expected) = &self.expected { ensure!( @@ -891,6 +1053,9 @@ impl DeploymentTransaction { == self.route_preflight.is_some(), "route preflight does not match routed Expected" ); + if let Some(preflight) = &self.route_preflight { + preflight.validate()?; + } } if self.phase >= DeploymentPhase::Leasing { ensure!(self.fencing.is_some(), "Leasing lacks fencing evidence"); @@ -911,6 +1076,18 @@ impl DeploymentTransaction { if self.phase >= DeploymentPhase::Committing { ensure!(self.routing.is_some(), "Committing lacks routing evidence"); } + if let Some(RoutingEvidence::Promoted { + observation, + promoted_at_unix_millis, + }) = &self.routing + { + observation.validate()?; + ensure!( + *promoted_at_unix_millis > 0 + && *promoted_at_unix_millis <= self.updated_at_unix_millis, + "route promotion time is outside the durable transaction timeline" + ); + } if self.phase == DeploymentPhase::Complete { ensure!( matches!( @@ -1010,7 +1187,7 @@ impl AdmittedOdinAuthority { #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.admitted_generation", - schema = "idunn.admitted_generation.v1" + schema = "idunn.admitted_generation.v2" )] struct AdmittedGeneration { #[cultcache(key = 0)] @@ -1049,6 +1226,8 @@ struct AdmittedGeneration { odin_authority: AdmittedOdinAuthority, #[cultcache(key = 17)] odin_publisher_sequence_cursor: u64, + #[cultcache(key = 18)] + route_repair_started_at_unix_millis: Option, } impl AdmittedGeneration { @@ -1085,6 +1264,7 @@ impl AdmittedGeneration { routing: required(&transaction.routing, "route disposition")?.clone(), odin_authority, odin_publisher_sequence_cursor: transaction.odin_publisher_sequence_cursor, + route_repair_started_at_unix_millis: None, }; generation.validate()?; Ok(generation) @@ -1127,6 +1307,20 @@ impl AdmittedGeneration { self.expected.route.is_some() == self.routing.observation().is_some(), "admitted route disposition differs from Expected" ); + if let Some(route) = self.routing.observation() { + route.validate()?; + ensure!( + route.runtime_instance_id == self.activation.runtime_instance_id, + "admitted route observation belongs to another runtime instance" + ); + } + if let Some(started_at) = self.route_repair_started_at_unix_millis { + ensure!(started_at > 0, "admitted route repair has no start time"); + ensure!( + matches!(&self.routing, RoutingEvidence::Promoted { .. }), + "only a promoted routed generation can own route repair intent" + ); + } Ok(()) } } @@ -1532,7 +1726,7 @@ impl ControlSnapshot { }) } - fn max_odin_sequence(&self, signer_identity_id: &str) -> u64 { + fn max_odin_sequence(&self, target: &str, signer_identity_id: &str) -> u64 { let transaction_max = self .transactions .iter() @@ -1541,7 +1735,10 @@ impl ControlSnapshot { .value .latest_odin_observation .as_ref() - .filter(|evidence| evidence.signer_identity_id == signer_identity_id) + .filter(|evidence| { + stored.value.target == target + && evidence.signer_identity_id == signer_identity_id + }) .map(|evidence| evidence.publisher_sequence) }) .max() @@ -1549,7 +1746,10 @@ impl ControlSnapshot { let admitted_max = self .admitted .iter() - .filter(|stored| stored.value.odin_authority.signer_identity_id == signer_identity_id) + .filter(|stored| { + stored.value.target == target + && stored.value.odin_authority.signer_identity_id == signer_identity_id + }) .map(|stored| stored.value.odin_publisher_sequence_cursor) .max() .unwrap_or(0); @@ -1672,9 +1872,85 @@ fn load_bindings(directory: &Path) -> Result> { ); } ensure!(!bindings.is_empty(), "Idunn binding directory is empty"); + validate_route_bindings(&bindings)?; Ok(bindings) } +fn validate_route_bindings(bindings: &BTreeMap) -> Result<()> { + let routes = bindings + .iter() + .filter_map(|(target, loaded)| { + loaded + .binding + .route + .as_ref() + .map(|route| (target.as_str(), route)) + }) + .collect::>(); + validate_route_binding_set(&routes) +} + +fn validate_route_binding_set(routes: &[(&str, &RouteBinding)]) -> Result<()> { + for (index, (target, route)) in routes.iter().enumerate() { + let (stable_host, stable_port) = route.stable_socket()?; + let private_host = route + .private_host + .parse::() + .context("validated route private host stopped being an IP address")?; + ensure!( + stable_host != private_host + || !(route.private_port_start..=route.private_port_end).contains(&stable_port), + "route {target} stable socket overlaps its candidate port range" + ); + + for (other_target, other) in &routes[index + 1..] { + ensure!( + route.route_id != other.route_id, + "route id {} is shared by targets {target} and {other_target}", + route.route_id + ); + ensure!( + route.config_path != other.config_path, + "route fragment {} is shared by targets {target} and {other_target}", + route.config_path.display() + ); + let other_stable = other.stable_socket()?; + ensure!( + route.driver != other.driver || (stable_host, stable_port) != other_stable, + "stable route socket {}:{} is shared by targets {target} and {other_target}", + stable_host, + stable_port + ); + if route.driver != other.driver { + continue; + } + let other_private_host = other + .private_host + .parse::() + .context("validated route private host stopped being an IP address")?; + let ranges_overlap = route.private_host == other.private_host + && route.private_port_start <= other.private_port_end + && other.private_port_start <= route.private_port_end; + ensure!( + !ranges_overlap, + "candidate port ranges overlap for targets {target} and {other_target}" + ); + ensure!( + stable_host != other_private_host + || !(other.private_port_start..=other.private_port_end).contains(&stable_port), + "stable route socket for {target} overlaps {other_target}'s candidate range" + ); + ensure!( + other_stable.0 != private_host + || !(route.private_port_start..=route.private_port_end) + .contains(&other_stable.1), + "stable route socket for {other_target} overlaps {target}'s candidate range" + ); + } + } + Ok(()) +} + fn resolve_selector( bindings: &BTreeMap, selector: &str, @@ -1978,18 +2254,38 @@ impl Engine { validate_authenticated_evidence(evidence, &authenticated)?; } if let Some(evidence) = &transaction.warming { - let authenticated = self.authenticate_topology_bytes( - snapshot, - transaction, - &evidence.canonical_bytes, - None, - evidence.admitted_at_unix_millis, - )?; - validate_authenticated_evidence(evidence, &authenticated)?; - ensure!( - is_semantic_warming(&authenticated), - "durable Warming label is not exact semantic Warming" - ); + match evidence { + WarmingEvidence::OdinTopology { evidence } => { + let authenticated = self.authenticate_topology_bytes( + snapshot, + transaction, + &evidence.canonical_bytes, + None, + evidence.admitted_at_unix_millis, + )?; + validate_authenticated_evidence(evidence, &authenticated)?; + let incumbent_lease_sha256 = + self.incumbent_lease_sha256_for_warming(snapshot, transaction)?; + ensure!( + is_semantic_warming( + required(&transaction.expected, "Warming Expected projection",)?, + required(&transaction.activation, "Warming activation")?, + incumbent_lease_sha256.as_deref(), + &authenticated, + )?, + "durable Warming gate is not supported by current runtime evidence" + ); + } + WarmingEvidence::FirstOdinDirect { evidence } => { + self.authenticate_first_odin_warming_presence( + transaction, + &evidence.message_id, + evidence.challenged_at_unix_millis, + evidence.admitted_at_unix_millis, + &evidence.canonical_bytes, + )?; + } + } } if let Some(evidence) = &transaction.ready { let authenticated = self.authenticate_topology_bytes( @@ -2062,14 +2358,9 @@ fn serve(options: RuntimeOptions) -> Result<()> { engine.validate_durable_authority(&ControlSnapshot::read(&engine.options.state_store)?)?; loop { - if engine.resume_one_transaction()? { - thread::sleep(Duration::from_millis(engine.options.poll_millis)); - continue; - } - if engine.supervise_one_admitted_generation()? { - continue; - } - if engine.freeze_one_queued_command()? { + let transaction_progress = engine.resume_one_transaction()?; + let continuity_progress = engine.supervise_one_admitted_generation()?; + if !transaction_progress && !continuity_progress && engine.freeze_one_queued_command()? { continue; } thread::sleep(Duration::from_millis(engine.options.poll_millis)); @@ -2083,6 +2374,7 @@ impl Engine { /// own command, so one target brake cannot suspend unrelated continuity. fn resume_one_transaction(&self) -> Result { let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let mut progressed = false; let mut candidates = snapshot .transactions .iter() @@ -2099,12 +2391,22 @@ impl Engine { if snapshot.has_earlier_authority_sibling(¤t.value) { continue; } - match self.advance_transaction(current) { - Ok(()) => {} - Err(error) if current.value.phase < DeploymentPhase::Fencing => { - self.begin_pre_fencing_abort(current, error)?; + if let Err(error) = self.advance_transaction(current) { + let latest_snapshot = ControlSnapshot::read(&self.options.state_store)?; + let latest = latest_snapshot + .transactions + .iter() + .find(|stored| stored.value.transaction_id == current.value.transaction_id) + .context("transaction disappeared while recording an execution error")?; + if latest.value.is_terminal() { + progressed = true; + } else if latest.value.phase < DeploymentPhase::Fencing + && latest.value.pre_fencing_abort.is_none() + { + self.begin_pre_fencing_abort(latest, error)?; + } else { + self.record_resumable_error(latest, &error)?; } - Err(error) => self.record_resumable_error(current, &error)?, } let after = ControlSnapshot::read(&self.options.state_store)?; let live = after @@ -2113,10 +2415,10 @@ impl Engine { .find(|stored| stored.value.transaction_id == current.value.transaction_id) .context("transaction disappeared while checking scheduler progress")?; if live.envelope != current.envelope { - return Ok(true); + progressed = true; } } - Ok(false) + Ok(progressed) } fn freeze_one_queued_command(&self) -> Result { @@ -2171,7 +2473,7 @@ impl Engine { let busy = snapshot .transactions .iter() - .filter(|stored| stored.value.owns_target_authority()) + .filter(|stored| stored.value.blocks_new_target_mutation()) .map(|stored| stored.value.target.as_str()) .collect::>(); if targets.iter().any(|target| busy.contains(target.as_str())) { @@ -2213,35 +2515,161 @@ impl Engine { fn supervise_one_admitted_generation(&self) -> Result { let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let busy = snapshot - .transactions - .iter() - .filter(|stored| stored.value.owns_target_authority()) - .map(|stored| stored.value.target.as_str()) - .collect::>(); + let mut progressed = false; let mut admitted = snapshot.admitted.iter().collect::>(); admitted.sort_by_key(|stored| stored.value.target.as_str()); for current in admitted { - if busy.contains(current.value.target.as_str()) { + let blocker = snapshot.transactions.iter().find(|stored| { + stored.value.target == current.value.target + && stored.value.blocks_new_target_mutation() + }); + if blocker.is_some_and(|stored| { + stored.value.phase >= DeploymentPhase::Fencing + && stored.value.phase < DeploymentPhase::Complete + }) { continue; } + match self.supervise_admitted_route(current) { + Ok(true) => { + progressed = true; + continue; + } + Ok(false) => {} + Err(error) => eprintln!( + "Idunn rejected admitted {} route continuity: {error:#}", + current.value.target + ), + } match self.refresh_admitted_topology(&snapshot, current) { - Ok(true) => return Ok(true), + Ok(true) => { + progressed = true; + continue; + } Ok(false) => {} Err(error) => eprintln!( "Idunn preserved admitted {} after rejecting topology observation: {error:#}", current.value.target ), } - if self - .workload - .observe( - ¤t.value.expected, - ¤t.value.activation, - ¤t.value.workload, - ) - .is_ok() - { + let mut operational_error = None; + let observation = match self.workload.observe( + ¤t.value.expected, + ¤t.value.activation, + ¤t.value.workload, + ) { + Ok(observation) => { + let lease_is_missing = if let Some(lease) = current.value.leasing.lease() { + let lease_health = (|| -> Result { + let lease_path = current + .value + .plan + .parsed_inputs()? + .1 + .process_write_lease + .context("admitted lease has no operator binding")? + .record_path; + let driver = + CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); + if driver.observe_exact(lease)? { + return Ok(true); + } + if driver.observe_empty()? { + return Ok(false); + } + bail!("write-lease store contains unexpected authority") + })(); + match lease_health { + Ok(true) => false, + Ok(false) => { + operational_error = + Some(anyhow!("admitted physical write lease is missing")); + true + } + Err(error) => { + eprintln!( + "Idunn preserved admitted {} after refusing an unsafe write-lease mutation: {error:#}", + current.value.target + ); + continue; + } + } + } else { + false + }; + if lease_is_missing { + None + } else { + Some(observation) + } + } + Err(error) => { + operational_error = Some(error); + None + } + }; + if let Some(observation) = observation { + if blocker.is_none() + || blocker.is_some_and(|stored| stored.value.phase == DeploymentPhase::Complete) + { + let repair = (|| -> Result<()> { + let topology = self.topology(); + let provider_anchor = self.provider_anchor_for_plan(¤t.value.plan)?; + if !topology.admitted_runtime_projection_is_exact( + ¤t.value.expected, + &provider_anchor, + ¤t.value.activation, + current.value.leasing.lease(), + )? { + ensure!( + topology + .publish_expected(¤t.value.expected, &provider_anchor,)? + == current.value.expected.canonical_sha256()?, + "admitted Expected projection repair differs" + ); + ensure!( + topology.publish_observed_activation( + ¤t.value.expected, + ¤t.value.activation, + &observation, + )? == current.value.activation.canonical_sha256()?, + "admitted activation projection repair differs" + ); + if let Some(lease) = current.value.leasing.lease() { + ensure!( + topology.publish_process_write_lease( + ¤t.value.expected, + ¤t.value.activation, + lease, + )? == lease.canonical_sha256()?, + "admitted write-lease projection repair differs" + ); + } + } + Ok(()) + })(); + if let Err(error) = repair { + eprintln!( + "Idunn preserved admitted {} after refusing topology projection repair: {error:#}", + current.value.target + ); + } + } + continue; + } + let workload_error = operational_error + .context("admitted operational state has neither observation nor error")?; + if let Some(blocker) = blocker { + if blocker.value.phase < DeploymentPhase::Fencing + && blocker.value.pre_fencing_abort.is_none() + { + self.begin_pre_fencing_abort( + blocker, + anyhow!( + "admitted incumbent failed before candidate fencing; deployment yielded to continuity: {workload_error:#}" + ), + )?; + progressed = true; + } continue; } let now = now_millis()?; @@ -2278,9 +2706,129 @@ impl Engine { )?, "continuity scheduling lost its command/transaction CAS" ); + progressed = true; + } + Ok(progressed) + } + + fn supervise_admitted_route(&self, current: &Stored) -> Result { + let Some(expected_route) = current.value.expected.route.as_ref() else { + ensure!( + matches!(¤t.value.routing, RoutingEvidence::SkippedUnrouted) + && current.value.route_repair_started_at_unix_millis.is_none(), + "unrouted admitted generation carries route authority" + ); + return Ok(false); + }; + let RoutingEvidence::Promoted { + observation, + promoted_at_unix_millis, + } = ¤t.value.routing + else { + bail!("routed admitted generation has no promoted route receipt") + }; + ensure!( + observation.route_id == expected_route.route_id + && observation.runtime_instance_id == current.value.activation.runtime_instance_id, + "admitted route receipt names another incarnation" + ); + let binding = current.value.plan.parsed_inputs()?.1; + let lease_driver = if let Some(lease) = current.value.leasing.lease() { + let lease_path = &binding + .process_write_lease + .as_ref() + .context("stateful admitted generation has no write-lease binding")? + .record_path; + let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); + ensure!( + driver.observe_exact(lease)?, + "admitted process write lease is no longer exact" + ); + Some(driver) + } else { + None + }; + let route_binding = binding + .route + .context("routed admitted generation has no operator route binding")?; + let driver = NginxRouteDriver::new(route_binding); + + let membership_is_exact = + driver.observe_membership(¤t.value.expected, &observation.membership_sha256)?; + let now = now_millis()?; + if membership_is_exact + && route_observation_is_current( + observation.observed_at_unix_millis, + now, + self.options.topology_maximum_age_millis, + self.options.topology_maximum_future_skew_millis, + ) + && current.value.route_repair_started_at_unix_millis.is_none() + { + return Ok(false); + } + if current.value.route_repair_started_at_unix_millis.is_none() { + let mut next = current.value.clone(); + next.route_repair_started_at_unix_millis = Some(now); + next.validate()?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store) + .compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: AdmittedGeneration::TYPE.into(), + key: current.value.target.clone(), + current: Some(current.envelope.clone()), + }], + &[admitted_envelope(&next, now)?], + )?, + "admitted generation changed before route repair intent CAS" + ); return Ok(true); } - Ok(false) + driver + .restore_admitted_membership(¤t.value.expected, &observation.membership_sha256)?; + let authority = self.runtime_authority_parts( + ¤t.value.plan, + ¤t.value.expected, + ¤t.value.activation, + )?; + let refreshed = self.prove_stable_route_against( + ¤t.value.expected, + ¤t.value.activation, + &authority, + current.value.leasing.lease_sha256(), + &driver, + observation.membership_sha256.clone(), + )?; + ensure!( + driver.observe_membership(¤t.value.expected, &refreshed.membership_sha256)?, + "admitted route membership changed during its continuity challenge" + ); + if let (Some(lease_driver), Some(lease)) = (&lease_driver, current.value.leasing.lease()) { + ensure!( + lease_driver.observe_exact(lease)?, + "admitted process write lease changed during its continuity challenge" + ); + } + let mut next = current.value.clone(); + next.routing = RoutingEvidence::Promoted { + observation: refreshed, + promoted_at_unix_millis: *promoted_at_unix_millis, + }; + next.route_repair_started_at_unix_millis = None; + next.validate()?; + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: AdmittedGeneration::TYPE.into(), + key: current.value.target.clone(), + current: Some(current.envelope.clone()), + }], + &[admitted_envelope(&next, now)?], + )?, + "admitted generation changed before route continuity receipt CAS" + ); + Ok(true) } fn refresh_admitted_topology( @@ -2312,7 +2860,7 @@ impl Engine { let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; if !sequence_requires_admission( Some(¤t.value.latest_odin_observation), - snapshot.max_odin_sequence(&evidence.signer_identity_id), + snapshot.max_odin_sequence(¤t.value.target, &evidence.signer_identity_id), &evidence, )? { return Ok(false); @@ -2434,8 +2982,12 @@ impl Engine { if current.value.expected_publication_sha256.is_none() { let now = now_millis()?; + let plan = required(¤t.value.plan, "transaction plan")?; let expected = required(¤t.value.expected, "Expected projection")?; - let digest = self.topology().publish_expected(expected)?; + let provider_anchor = self.provider_anchor_for_plan(plan)?; + let digest = self + .topology() + .publish_expected(expected, &provider_anchor)?; return self.persist_same_phase(current, |next| { next.expected_publication_sha256 = Some(digest); next.updated_at_unix_millis = now; @@ -2541,35 +3093,64 @@ impl Engine { let activation = required(¤t.value.activation, "activation")?; let workload = required(¤t.value.workload, "workload")?; self.workload.observe(expected, activation, workload)?; - { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent_lease_sha256 = + self.incumbent_lease_sha256_for_warming(&snapshot, ¤t.value)?; + if current.value.warming.is_none() { + if current.value.target == "odin" && snapshot.admitted_for("odin").is_none() { + ensure!( + current.value.incumbent_generation_id.is_none() + && expected.write_lease_required, + "first Odin bootstrap must be a stateful first incarnation" + ); + let (evidence, present) = self.observe_first_odin_warming(¤t.value)?; + let _token = SequenceAdmittedWarming::from_first_odin_presence( + current.value.transaction_id.clone(), + evidence.clone(), + present, + )?; + return self.persist_same_phase(current, |next| { + next.warming = Some(WarmingEvidence::FirstOdinDirect { evidence }); + Ok(()) + }); + } let Some((admitted, authenticated)) = self.admit_latest_topology(current, None)? else { return Ok(()); }; if admitted.envelope != current.envelope { return Ok(()); } - let semantic_warming = is_semantic_warming(&authenticated); + let semantic_warming = is_semantic_warming( + expected, + activation, + incumbent_lease_sha256.as_deref(), + &authenticated, + )?; let latest = required( &admitted.value.latest_odin_observation, "sequence-admitted warming evidence", )?; - if current.value.warming.as_ref() == Some(latest) { - ensure!(semantic_warming, "stored Warming receipt changed meaning"); - } else if semantic_warming { + if semantic_warming { let evidence = latest.clone(); - let _token = SequenceAdmittedWarming { - transaction_id: admitted.value.transaction_id.clone(), - evidence: evidence.clone(), + let _token = SequenceAdmittedWarming::from_topology( + admitted.value.transaction_id.clone(), + evidence.clone(), authenticated, - }; + )?; return self.persist_same_phase(&admitted, |next| { - next.warming = Some(evidence); + next.warming = Some(WarmingEvidence::OdinTopology { evidence }); Ok(()) }); } else { return Ok(()); } } + let warming = self.rehydrate_warming_token(¤t.value, now_millis()?, false)?; + ensure!( + warming.transaction_id() == current.value.transaction_id + && warming.runtime_instance_id() == activation.runtime_instance_id.as_str(), + "durable Warming evidence belongs to another candidate" + ); if expected.route.is_some() && current.value.route_preflight.is_none() { let now = now_millis()?; @@ -2632,12 +3213,21 @@ impl Engine { }) .transpose()?; if let (Some(lease), Some(path)) = (incumbent_lease, &incumbent_lease_path) { + let incumbent = incumbent.context("incumbent lease lost its generation")?; + self.workload + .stop(&incumbent.value.workload) + .context("stopping the exact incumbent before revoking its lifetime-held write lease")?; let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, path); driver.revoke_exact(Some(lease))?; ensure!( driver.observe_empty()?, "incumbent write lease remained after exact fencing" ); + self.topology().withdraw_process_write_lease( + &incumbent.value.expected, + &incumbent.value.activation, + Some(lease), + )?; } let candidate_lease_path = if expected.write_lease_required { Some( @@ -2665,6 +3255,11 @@ impl Engine { ); } } + self.topology().withdraw_process_write_lease( + expected, + required(¤t.value.activation, "candidate activation")?, + None, + )?; FencingEvidence::Revoked { incumbent_lease_sha256: incumbent_lease .map(IdunnProcessWriteLeaseRecord::canonical_sha256) @@ -2684,74 +3279,64 @@ impl Engine { } fn advance_leasing(&self, current: &Stored) -> Result<()> { - if current.value.leasing.is_none() { - let now = now_millis()?; - let expected = required(¤t.value.expected, "Expected projection")?; - let evidence = if expected.write_lease_required { - let warming = self.rehydrate_warming_token(¤t.value, now)?; - ensure!( - warming.transaction_id() == current.value.transaction_id - && Some(warming.evidence_sha256()) - == current - .value - .warming - .as_ref() - .map(|value| value.canonical_sha256.as_str()), - "Warming token does not bind this transaction's durable admission" - ); - let activation = required(¤t.value.activation, "activation")?; - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let _lease_path = binding - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let epoch = self - .exact_incumbent(&snapshot, ¤t.value)? - .and_then(|generation| generation.value.leasing.lease()) - .map_or(1, |lease| lease.lease_epoch.saturating_add(1)); - let warming_presence_sha256 = warming - .authenticated() - .record() - .signed_presence_sha256 - .clone() - .context("warming token has no signed presence")?; - let lease = IdunnProcessWriteLeaseRecord { - schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), - target: expected.target.clone(), - expected_projection_sha256: expected.canonical_sha256()?, - plan_id: expected.plan_id.clone(), - incarnation_id: expected.incarnation_id.clone(), - sealed_release_id: expected.sealed_release_id.clone(), - activation_witness_sha256: activation.canonical_sha256()?, - state_schema_generation: expected - .state_schema_generation - .clone() - .context("stateful Expected has no schema generation")?, - state_contract_sha256: expected - .state_contract_sha256 - .clone() - .context("stateful Expected has no state contract")?, - runtime_id: expected.runtime_id.clone(), - runtime_instance_id: activation.runtime_instance_id.clone(), - warming_presence_sha256, - lease_epoch: epoch, - issued_at_unix_millis: now, - }; - lease.validate()?; - let lease_sha256 = lease.canonical_sha256()?; - LeasingEvidence::Prepared { - lease, - lease_sha256, - } - } else { - LeasingEvidence::SkippedStateless - }; - return self.persist_same_phase(current, |next| { - next.leasing = Some(evidence); - next.updated_at_unix_millis = now; - Ok(()) - }); + let expected = required(¤t.value.expected, "Expected projection")?; + if !expected.write_lease_required { + if current.value.leasing.is_none() { + let now = now_millis()?; + return self.persist_same_phase(current, |next| { + next.leasing = Some(LeasingEvidence::SkippedStateless); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + return self.transition(current, DeploymentPhase::AwaitingReady); + } + + let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let lease_path = binding + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); + + if matches!( + current.value.leasing.as_ref(), + Some(LeasingEvidence::Granted { .. }) + ) { + let activation = required(¤t.value.activation, "activation")?; + let warming = self.rehydrate_warming_token(¤t.value, now_millis()?, false)?; + let lease = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease) + .context("granted Leasing evidence has no write lease")?; + let recorded_sha256 = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256) + .context("granted Leasing evidence has no lease digest")?; + ensure!( + driver.observe_exact(lease)?, + "physical write lease disappeared after Granted became durable" + ); + self.workload.observe( + expected, + activation, + required(¤t.value.workload, "candidate workload")?, + )?; + ensure!( + driver.grant(expected, activation, &warming, lease)? == recorded_sha256, + "replayed physical write lease differs from Granted evidence" + ); + ensure!( + self.topology() + .publish_process_write_lease(expected, activation, lease)? + == recorded_sha256, + "replayed write-lease projection differs from Granted evidence" + ); + return self.transition(current, DeploymentPhase::AwaitingReady); } if let Some((lease, prepared_sha256)) = current @@ -2761,42 +3346,101 @@ impl Engine { .and_then(LeasingEvidence::prepared_lease) { let now = now_millis()?; - let expected = required(¤t.value.expected, "Expected projection")?; let activation = required(¤t.value.activation, "activation")?; - let warming = self.rehydrate_warming_token(¤t.value, now)?; - ensure!( - warming.transaction_id() == current.value.transaction_id - && Some(warming.evidence_sha256()) - == current - .value - .warming - .as_ref() - .map(|value| value.canonical_sha256.as_str()), - "Warming token does not bind this transaction's durable admission" - ); - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let lease_path = binding - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); + let historical_warming = self.rehydrate_warming_token(¤t.value, now, false)?; + let physical_is_exact = driver.observe_exact(lease)?; + let warming = if physical_is_exact { + historical_warming + } else { + ensure!( + driver.observe_empty()?, + "candidate write-lease store contains authority other than its durable prepared lease" + ); + match self.rehydrate_warming_token(¤t.value, now, true) { + Ok(warming) => warming, + Err(_) => { + let Some((admitted, fresh_evidence, fresh_warming)) = + self.fresh_warming_for_lease(current, now)? + else { + return Ok(()); + }; + let replacement = self.prepare_candidate_write_lease( + &admitted.value, + &fresh_warming, + now, + )?; + let replacement_sha256 = replacement.canonical_sha256()?; + return self.persist_same_phase(&admitted, |next| { + next.warming = Some(fresh_evidence); + next.leasing = Some(LeasingEvidence::Prepared { + lease: replacement, + lease_sha256: replacement_sha256, + }); + next.updated_at_unix_millis = now; + Ok(()) + }); + } + } + }; + self.workload.observe( + expected, + activation, + required(¤t.value.workload, "candidate workload")?, + )?; + if !physical_is_exact { + ensure!( + driver.observe_empty()?, + "candidate write-lease store changed before physical grant" + ); + } let granted_sha256 = driver.grant(expected, activation, &warming, lease)?; ensure!( granted_sha256 == prepared_sha256, "granted write lease differs from the durable prepared lease" ); + let projected_sha256 = self + .topology() + .publish_process_write_lease(expected, activation, lease)?; + ensure!( + projected_sha256 == granted_sha256, + "projected write lease differs from the granted process authority" + ); let lease = lease.clone(); return self.persist_same_phase(current, |next| { next.leasing = Some(LeasingEvidence::Granted { lease, - lease_sha256: granted_sha256, + lease_sha256: projected_sha256, }); next.updated_at_unix_millis = now; Ok(()) }); } - self.transition(current, DeploymentPhase::AwaitingReady) + ensure!( + current.value.leasing.is_none(), + "stateful Leasing phase carries invalid lease evidence" + ); + ensure!( + driver.observe_empty()?, + "candidate write-lease store contains authority before lease preparation" + ); + let now = now_millis()?; + let Some((admitted, fresh_evidence, fresh_warming)) = + self.fresh_warming_for_lease(current, now)? + else { + return Ok(()); + }; + let lease = self.prepare_candidate_write_lease(&admitted.value, &fresh_warming, now)?; + let lease_sha256 = lease.canonical_sha256()?; + self.persist_same_phase(&admitted, |next| { + next.warming = Some(fresh_evidence); + next.leasing = Some(LeasingEvidence::Prepared { + lease, + lease_sha256, + }); + next.updated_at_unix_millis = now; + Ok(()) + }) } fn advance_awaiting_ready(&self, current: &Stored) -> Result<()> { @@ -2804,7 +3448,7 @@ impl Engine { let expected = required(¤t.value.expected, "Expected projection")?; if expected.write_lease_required { let activation = required(¤t.value.activation, "activation")?; - let warming = self.rehydrate_warming_token(¤t.value, now)?; + let warming = self.rehydrate_warming_token(¤t.value, now, false)?; let lease = current .value .leasing @@ -2863,43 +3507,108 @@ impl Engine { } fn advance_routing(&self, current: &Stored) -> Result<()> { - let now = now_millis()?; let expected = required(¤t.value.expected, "Expected projection")?; let activation = required(¤t.value.activation, "activation")?; if current.value.routing.is_none() { - let ready = self.rehydrate_ready_token(¤t.value, now, false)?; + let current_lease = current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + let Some((admitted, authenticated)) = + self.admit_latest_topology(current, current_lease)? + else { + return Ok(()); + }; + if admitted.envelope != current.envelope { + return Ok(()); + } ensure!( - ready.transaction_id() == current.value.transaction_id, + is_semantic_ready(&authenticated), + "latest Odin observation is not Ready at route admission" + ); + let latest = required( + &admitted.value.latest_odin_observation, + "current route-admission topology evidence", + )?; + if admitted.value.ready.as_ref() != Some(latest) { + let latest = latest.clone(); + return self.persist_same_phase(&admitted, |next| { + next.ready = Some(latest); + Ok(()) + }); + } + let ready = self.rehydrate_ready_token(&admitted.value, now_millis()?, true)?; + ensure!( + ready.transaction_id() == admitted.value.transaction_id, "Ready token belongs to another transaction" ); + validate_live_providers_for_deploy(admitted.value.command_kind, || { + self.validate_selected_providers_current( + required(&admitted.value.plan, "transaction plan")?, + now_millis()?, + ) + })?; + self.ensure_transaction_write_lease_current(&admitted.value, now_millis()?)?; let evidence = if expected.route.is_some() { - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let binding = admitted.value.plan.as_ref().unwrap().parsed_inputs()?.1; let route_binding = binding.route.context("routed plan has no route binding")?; - let preflight = required(¤t.value.route_preflight, "route preflight")?; + let preflight = required(&admitted.value.route_preflight, "route preflight")?; let driver = NginxRouteDriver::new(route_binding); - let exact_candidate = RouteObservation { - route_id: preflight.route_id.clone(), - runtime_instance_id: preflight.candidate_runtime_instance_id.clone(), - membership_sha256: preflight.candidate_membership_sha256.clone(), - }; ensure!( - exact_candidate.runtime_instance_id == activation.runtime_instance_id, + preflight.candidate_runtime_instance_id == activation.runtime_instance_id, "route preflight belongs to another runtime instance" ); - let observation = if driver.observe(expected, &exact_candidate)? { - exact_candidate - } else { - driver.promote(expected, &activation.runtime_instance_id, preflight)? + let rollback_allowed = may_rollback_route_after_failed_proof(required( + &admitted.value.fencing, + "route fencing evidence", + )?); + let membership_sha256 = driver.install( + expected, + &activation.runtime_instance_id, + preflight, + rollback_allowed, + )?; + let observation = match self.prove_stable_route( + &admitted.value, + &driver, + membership_sha256, + ) { + Ok(observation) => observation, + Err(proof_error) if !rollback_allowed => { + return Err(proof_error).context( + "incumbent authority was fenced; candidate route remains installed for fail-closed retry", + ); + } + Err(proof_error) => { + return match driver.rollback( + expected, + &activation.runtime_instance_id, + preflight, + ) { + Ok(()) => Err(proof_error) + .context("candidate did not answer its stable route challenge"), + Err(rollback_error) => Err(proof_error).context(format!( + "candidate did not answer its stable route challenge; exact route rollback also failed: {rollback_error:#}" + )), + }; + } }; ensure!( - driver.observe(expected, &observation)?, - "promoted route is not the exact candidate membership" + driver.observe_membership(expected, &observation.membership_sha256)?, + "route membership changed during its signed stable-listener observation" ); - RoutingEvidence::Promoted { observation } + self.ensure_transaction_write_lease_current(&admitted.value, now_millis()?)?; + let promoted_at_unix_millis = observation.observed_at_unix_millis; + RoutingEvidence::Promoted { + observation, + promoted_at_unix_millis, + } } else { RoutingEvidence::SkippedUnrouted }; - return self.persist_same_phase(current, |next| { + let now = now_millis()?; + return self.persist_same_phase(&admitted, |next| { next.routing = Some(evidence); next.updated_at_unix_millis = now; Ok(()) @@ -2909,64 +3618,145 @@ impl Engine { } fn advance_committing(&self, current: &Stored) -> Result<()> { - let now = now_millis()?; - let expected = required(¤t.value.expected, "Expected projection")?; - let activation = required(¤t.value.activation, "activation")?; - let workload = required(¤t.value.workload, "workload")?; - self.workload.observe(expected, activation, workload)?; - let _ready = self.rehydrate_ready_token(¤t.value, now, false)?; - - if let Some(lease) = current + let current_lease_sha256 = current .value .leasing .as_ref() - .and_then(LeasingEvidence::lease) - { - let warming = self.rehydrate_warming_token(¤t.value, now)?; - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let path = binding - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - ensure!( - CultCacheWriteLeaseDriver::new(¤t.value.target, path) - .observe(expected, activation, &warming, lease)?, - "write lease changed before admission commit" - ); + .and_then(LeasingEvidence::lease_sha256) + .map(str::to_owned); + let Some((ready_current, authenticated)) = + self.admit_latest_topology(current, current_lease_sha256.as_deref())? + else { + return Ok(()); + }; + if ready_current.envelope != current.envelope { + return Ok(()); } - if let Some(route) = current + ensure!( + is_semantic_ready(&authenticated), + "latest Odin observation is not Ready at admission commit" + ); + ensure!( + ready_current.value.latest_odin_observation.as_ref() + == ready_current.value.ready.as_ref(), + "latest Odin observation differs from the durable Ready receipt at admission commit" + ); + self.rehydrate_ready_token(&ready_current.value, now_millis()?, true)?; + validate_live_providers_for_deploy(ready_current.value.command_kind, || { + self.validate_selected_providers_current( + required(&ready_current.value.plan, "transaction plan")?, + now_millis()?, + ) + })?; + + let expected = required(&ready_current.value.expected, "Expected projection")?; + let activation = required(&ready_current.value.activation, "activation")?; + let workload = required(&ready_current.value.workload, "workload")?; + self.workload.observe(expected, activation, workload)?; + self.ensure_transaction_write_lease_current(&ready_current.value, now_millis()?)?; + if let Some(route) = ready_current .value .routing .as_ref() .and_then(RoutingEvidence::observation) { - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; + let binding = ready_current + .value + .plan + .as_ref() + .unwrap() + .parsed_inputs()? + .1; let driver = NginxRouteDriver::new(binding.route.context("routed plan has no route binding")?); ensure!( - driver.observe(expected, route)?, - "route changed before admission commit" + driver.observe_membership(expected, &route.membership_sha256)?, + "route membership changed before admission commit" ); + let current_route = self.prove_stable_route( + &ready_current.value, + &driver, + route.membership_sha256.clone(), + )?; + ensure!( + current_route.route_id == route.route_id + && current_route.runtime_instance_id == route.runtime_instance_id, + "stable route changed incarnation before admission commit" + ); + ensure!( + driver.observe_membership(expected, &route.membership_sha256)?, + "route membership changed during the final signed admission challenge" + ); + self.ensure_transaction_write_lease_current(&ready_current.value, now_millis()?)?; } + let current_lease_sha256 = ready_current + .value + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256) + .map(str::to_owned); + let Some((commit_current, authenticated)) = + self.admit_latest_topology(&ready_current, current_lease_sha256.as_deref())? + else { + return Ok(()); + }; + if commit_current.envelope != ready_current.envelope { + return Ok(()); + } + ensure!( + is_semantic_ready(&authenticated), + "latest Odin observation is not Ready after the final admission challenge" + ); + ensure!( + commit_current.value.latest_odin_observation.as_ref() + == commit_current.value.ready.as_ref(), + "latest Odin observation differs from the durable Ready receipt after the final admission challenge" + ); + let now = now_millis()?; + self.rehydrate_ready_token(&commit_current.value, now, true)?; + validate_live_providers_for_deploy(commit_current.value.command_kind, || { + self.validate_selected_providers_current( + required(&commit_current.value.plan, "transaction plan")?, + now, + ) + })?; + self.workload.observe( + required(&commit_current.value.expected, "Expected projection")?, + required(&commit_current.value.activation, "activation")?, + required(&commit_current.value.workload, "workload")?, + )?; + self.ensure_transaction_write_lease_current(&commit_current.value, now)?; + let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; + let incumbent = self.exact_incumbent(&snapshot, &commit_current.value)?; let odin_authority = self.current_odin_authority(&snapshot)?; - let generation = AdmittedGeneration::from_transaction(¤t.value, odin_authority, now)?; + let generation = + AdmittedGeneration::from_transaction(&commit_current.value, odin_authority, now)?; let post_commit_cleanup = PostCommitCleanup { incumbent: match incumbent { + Some(incumbent) + if incumbent_was_stopped_during_fencing(required( + &commit_current.value.fencing, + "commit fencing evidence", + )?) => + { + IncumbentCleanupEvidence::Complete { + generation_id: incumbent.value.generation_id.clone(), + } + } Some(incumbent) => IncumbentCleanupEvidence::Pending { generation_id: incumbent.value.generation_id.clone(), workload: incumbent.value.workload.clone(), }, None => IncumbentCleanupEvidence::SkippedNoIncumbent, }, - source: match current.value.command_kind { + source: match commit_current.value.command_kind { CommandKind::Deploy => SourceCleanupEvidence::Pending, CommandKind::Continuity => SourceCleanupEvidence::SkippedContinuity, }, }; - let mut complete = current.value.clone(); + let mut complete = commit_current.value.clone(); complete.phase = DeploymentPhase::Complete; complete.updated_at_unix_millis = now; complete.last_error = None; @@ -2986,8 +3776,8 @@ impl Engine { &[ CultCacheExpectedEnvelope { r#type: DeploymentTransaction::TYPE.into(), - key: current.value.transaction_id.clone(), - current: Some(current.envelope.clone()), + key: commit_current.value.transaction_id.clone(), + current: Some(commit_current.envelope.clone()), }, admitted_expected, ], @@ -3016,6 +3806,187 @@ impl Engine { } } + fn incumbent_lease_sha256_for_warming( + &self, + snapshot: &ControlSnapshot, + transaction: &DeploymentTransaction, + ) -> Result> { + if let Some(fencing) = &transaction.fencing { + return match fencing { + FencingEvidence::SkippedStateless => Ok(None), + FencingEvidence::Revoked { + incumbent_lease_sha256, + .. + } => Ok(incumbent_lease_sha256.clone()), + }; + } + self.exact_incumbent(snapshot, transaction)? + .and_then(|incumbent| incumbent.value.leasing.lease()) + .map(IdunnProcessWriteLeaseRecord::canonical_sha256) + .transpose() + } + + fn fresh_warming_for_lease( + &self, + current: &Stored, + now: u64, + ) -> Result< + Option<( + Stored, + WarmingEvidence, + SequenceAdmittedWarming, + )>, + > { + let prior = self.rehydrate_warming_token(¤t.value, now, false)?; + match required(¤t.value.warming, "pre-fence Warming evidence")?.clone() { + WarmingEvidence::FirstOdinDirect { .. } => { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + ensure!( + snapshot.admitted_for("odin").is_none() + && self.exact_incumbent(&snapshot, ¤t.value)?.is_none(), + "direct first-Odin Warming refresh found an admitted Odin" + ); + let (evidence, present) = self.observe_first_odin_warming(¤t.value)?; + let token = SequenceAdmittedWarming::from_first_odin_presence( + current.value.transaction_id.clone(), + evidence.clone(), + present, + )?; + ensure!( + token.signed_presence_sha256() != prior.signed_presence_sha256(), + "first Odin replayed its pre-fence Warming presence" + ); + Ok(Some(( + current.clone(), + WarmingEvidence::FirstOdinDirect { evidence }, + token, + ))) + } + WarmingEvidence::OdinTopology { + evidence: prior_evidence, + } => { + let Some((admitted, authenticated)) = self.admit_latest_topology(current, None)? + else { + return Ok(None); + }; + let fresh_evidence = required( + &admitted.value.latest_odin_observation, + "post-fence Odin Warming observation", + )? + .clone(); + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let incumbent_lease_sha256 = + self.incumbent_lease_sha256_for_warming(&snapshot, &admitted.value)?; + if !is_semantic_warming( + required(&admitted.value.expected, "Warming Expected projection")?, + required(&admitted.value.activation, "Warming activation")?, + incumbent_lease_sha256.as_deref(), + &authenticated, + )? { + return Ok(None); + } + let token = SequenceAdmittedWarming::from_topology( + admitted.value.transaction_id.clone(), + fresh_evidence.clone(), + authenticated, + )?; + if !provider_warming_advanced( + prior_evidence.publisher_sequence, + prior.signed_presence_sha256(), + fresh_evidence.publisher_sequence, + token.signed_presence_sha256(), + ) { + return Ok(None); + } + Ok(Some(( + admitted, + WarmingEvidence::OdinTopology { + evidence: fresh_evidence, + }, + token, + ))) + } + } + } + + fn prepare_candidate_write_lease( + &self, + transaction: &DeploymentTransaction, + warming: &SequenceAdmittedWarming, + now: u64, + ) -> Result { + let expected = required(&transaction.expected, "Expected projection")?; + let activation = required(&transaction.activation, "activation")?; + ensure!( + expected.write_lease_required + && warming.transaction_id() == transaction.transaction_id + && warming.runtime_instance_id() == activation.runtime_instance_id, + "fresh Warming token does not own this stateful candidate" + ); + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let epoch = self + .exact_incumbent(&snapshot, transaction)? + .and_then(|generation| generation.value.leasing.lease()) + .map_or(1, |lease| lease.lease_epoch.saturating_add(1)); + let lease = IdunnProcessWriteLeaseRecord { + schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256()?, + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: activation.canonical_sha256()?, + state_schema_generation: expected + .state_schema_generation + .clone() + .context("stateful Expected has no schema generation")?, + state_contract_sha256: expected + .state_contract_sha256 + .clone() + .context("stateful Expected has no state contract")?, + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + warming_presence_sha256: warming.signed_presence_sha256().to_owned(), + lease_epoch: epoch, + issued_at_unix_millis: now, + }; + lease.validate()?; + Ok(lease) + } + + fn ensure_transaction_write_lease_current( + &self, + transaction: &DeploymentTransaction, + now: u64, + ) -> Result<()> { + let expected = required(&transaction.expected, "Expected projection")?; + let lease = transaction + .leasing + .as_ref() + .and_then(LeasingEvidence::lease); + ensure!( + expected.write_lease_required == lease.is_some(), + "transaction write-lease disposition differs from Expected" + ); + let Some(lease) = lease else { + return Ok(()); + }; + let activation = required(&transaction.activation, "activation")?; + let warming = self.rehydrate_warming_token(transaction, now, false)?; + let lease_path = required(&transaction.plan, "transaction plan")? + .parsed_inputs()? + .1 + .process_write_lease + .context("stateful target has no write-lease binding")? + .record_path; + ensure!( + CultCacheWriteLeaseDriver::new(&transaction.target, lease_path) + .observe(expected, activation, &warming, lease,)?, + "process write lease changed across the authority boundary" + ); + Ok(()) + } + fn select_candidate_port(&self, binding: &OperatorBinding) -> Result> { let Some(route) = &binding.route else { return Ok(None); @@ -3030,7 +4001,7 @@ impl Engine { snapshot .transactions .iter() - .filter(|stored| stored.value.owns_target_authority()) + .filter(|stored| stored.value.blocks_new_target_mutation()) .filter_map(|stored| stored.value.plan.as_ref()), ) { @@ -3143,16 +4114,203 @@ impl Engine { ) } + fn authenticate_routed_presence( + &self, + authority: &cultnet_rs::VerifiedRuntimeAuthority, + current_write_lease_sha256: Option<&str>, + message_id: &str, + challenged_at_unix_millis: u64, + received_at_unix_millis: u64, + canonical_presence: &[u8], + ) -> Result<(String, u64)> { + ensure!( + received_at_unix_millis >= challenged_at_unix_millis, + "route observation predates its challenge" + ); + let authenticated = authenticate_runtime_presence_claim( + canonical_presence, + authority, + RuntimePresenceAuthenticationContext { + trusted_received_at_unix_millis: received_at_unix_millis, + maximum_age_millis: self.options.topology_maximum_age_millis, + maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, + }, + )?; + let signed_presence_sha256 = authenticated.signed_presence_sha256().to_owned(); + let correlation = correlate_runtime_presence_claim(authenticated, authority)?; + ensure!( + correlation.disagreements().is_empty(), + "stable route answered with a runtime that disagrees with current authority" + ); + let present = correlation.into_undisputed_present()?; + let presence = present.record(); + ensure!( + presence.observed_at_unix_millis >= challenged_at_unix_millis, + "stable route returned a presence minted before the route challenge" + ); + ensure!( + presence.state == "active", + "stable route runtime is not Active" + ); + ensure!( + presence.detail == format!("route-observation:{message_id}"), + "stable route response is not bound to the exact challenge" + ); + ensure!( + presence.write_lease_sha256.as_deref() == current_write_lease_sha256, + "stable route runtime does not hold the exact current process write lease" + ); + Ok((signed_presence_sha256, received_at_unix_millis)) + } + + fn authenticate_first_odin_warming_presence( + &self, + transaction: &DeploymentTransaction, + message_id: &str, + challenged_at_unix_millis: u64, + received_at_unix_millis: u64, + canonical_presence: &[u8], + ) -> Result { + ensure!( + transaction.target == "odin" + && transaction.incumbent_generation_id.is_none() + && required(&transaction.expected, "first Odin Expected")?.write_lease_required, + "direct Warming presence is reserved for stateful first Odin bootstrap" + ); + ensure!( + received_at_unix_millis >= challenged_at_unix_millis, + "first Odin Warming observation predates its challenge" + ); + let authority = self.runtime_authority(transaction)?; + let authenticated = authenticate_runtime_presence_claim( + canonical_presence, + &authority, + RuntimePresenceAuthenticationContext { + trusted_received_at_unix_millis: received_at_unix_millis, + maximum_age_millis: self.options.topology_maximum_age_millis, + maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, + }, + )?; + let correlation = correlate_runtime_presence_claim(authenticated, &authority)?; + ensure!( + correlation.disagreements().is_empty(), + "first Odin Warming presence disagrees with its Expected activation" + ); + let present = correlation.into_undisputed_present()?; + let presence = present.record(); + ensure!( + presence.observed_at_unix_millis >= challenged_at_unix_millis + && presence.state == "warming" + && presence.write_lease_sha256.is_none() + && presence.detail == format!("idunn-warming:{message_id}"), + "first Odin candidate did not return exact fresh pre-lease Warming evidence" + ); + Ok(present) + } + + fn observe_first_odin_warming( + &self, + transaction: &DeploymentTransaction, + ) -> Result<(RuntimePresenceEvidence, cultnet_rs::VerifiedRuntimePresence)> { + let expected = required(&transaction.expected, "first Odin Expected")?; + let binding = required(&transaction.plan, "first Odin plan")? + .parsed_inputs()? + .1; + let driver = NginxRouteDriver::new( + binding + .route + .context("first Odin bootstrap has no candidate route binding")?, + ); + let message_id = format!("warming-{}", Uuid::new_v4().simple()); + let challenged_at_unix_millis = now_millis()?; + let response = driver.request_candidate_runtime_presence(expected, &message_id)?; + ensure!( + response.message_id == message_id, + "first Odin candidate transport substituted its challenge identity" + ); + let admitted_at_unix_millis = now_millis()?; + let present = self.authenticate_first_odin_warming_presence( + transaction, + &message_id, + challenged_at_unix_millis, + admitted_at_unix_millis, + &response.canonical_presence, + )?; + let evidence = RuntimePresenceEvidence::from_present( + &present, + message_id, + challenged_at_unix_millis, + admitted_at_unix_millis, + )?; + Ok((evidence, present)) + } + + fn prove_stable_route( + &self, + transaction: &DeploymentTransaction, + driver: &NginxRouteDriver, + membership_sha256: String, + ) -> Result { + let expected = required(&transaction.expected, "route Expected projection")?; + let activation = required(&transaction.activation, "route activation")?; + let authority = self.runtime_authority(transaction)?; + let current_write_lease_sha256 = transaction + .leasing + .as_ref() + .and_then(LeasingEvidence::lease_sha256); + self.prove_stable_route_against( + expected, + activation, + &authority, + current_write_lease_sha256, + driver, + membership_sha256, + ) + } + + fn prove_stable_route_against( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + authority: &cultnet_rs::VerifiedRuntimeAuthority, + current_write_lease_sha256: Option<&str>, + driver: &NginxRouteDriver, + membership_sha256: String, + ) -> Result { + let message_id = format!("route-{}", Uuid::new_v4().simple()); + let challenged_at_unix_millis = now_millis()?; + let response = driver.request_runtime_presence(expected, &message_id)?; + ensure!( + response.message_id == message_id, + "route transport substituted its challenge identity" + ); + let received_at_unix_millis = now_millis()?; + let (signed_presence_sha256, observed_at_unix_millis) = self.authenticate_routed_presence( + authority, + current_write_lease_sha256, + &message_id, + challenged_at_unix_millis, + received_at_unix_millis, + &response.canonical_presence, + )?; + let observation = RouteObservation { + route_id: driver.binding.route_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + membership_sha256, + signed_presence_sha256, + observed_at_unix_millis, + }; + observation.validate()?; + Ok(observation) + } + fn runtime_authority_parts( &self, plan: &CompiledDeploymentPlan, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, ) -> Result { - let binding = plan.parsed_inputs()?.1; - let provider_anchor = read_trust_anchor::( - &binding.runtime_identity.trust_anchor_store, - )?; + let provider_anchor = self.provider_anchor_for_plan(plan)?; verify_runtime_authority( expected, activation, @@ -3161,6 +4319,22 @@ impl Engine { ) } + fn provider_anchor_for_plan( + &self, + plan: &CompiledDeploymentPlan, + ) -> Result { + let binding = plan.parsed_inputs()?.1; + let provider_anchor = read_trust_anchor::( + &binding.runtime_identity.trust_anchor_store, + )?; + ensure!( + provider_anchor.schema_version + == ::TRUST_ANCHOR_SCHEMA, + "provider runtime presence trust anchor schema is unsupported" + ); + Ok(provider_anchor) + } + fn authenticate_topology_bytes( &self, snapshot: &ControlSnapshot, @@ -3220,7 +4394,7 @@ impl Engine { let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; if !sequence_requires_admission( live.value.latest_odin_observation.as_ref(), - snapshot.max_odin_sequence(&evidence.signer_identity_id), + snapshot.max_odin_sequence(&live.value.target, &evidence.signer_identity_id), &evidence, )? { return Ok(Some((live.clone(), authenticated))); @@ -3247,27 +4421,61 @@ impl Engine { fn rehydrate_warming_token( &self, transaction: &DeploymentTransaction, - _now: u64, + now: u64, + require_current: bool, ) -> Result { let evidence = required(&transaction.warming, "durable warming evidence")?.clone(); - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let authenticated = self.authenticate_topology_bytes( - &snapshot, - transaction, - &evidence.canonical_bytes, - None, - evidence.admitted_at_unix_millis, - )?; - validate_authenticated_evidence(&evidence, &authenticated)?; - ensure!( - is_semantic_warming(&authenticated), - "durable warming evidence no longer authenticates as Warming" - ); - Ok(SequenceAdmittedWarming { - transaction_id: transaction.transaction_id.clone(), - evidence, - authenticated, - }) + match evidence { + WarmingEvidence::OdinTopology { evidence } => { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let authenticated = self.authenticate_topology_bytes( + &snapshot, + transaction, + &evidence.canonical_bytes, + None, + if require_current { + now + } else { + evidence.admitted_at_unix_millis + }, + )?; + validate_authenticated_evidence(&evidence, &authenticated)?; + let incumbent_lease_sha256 = + self.incumbent_lease_sha256_for_warming(&snapshot, transaction)?; + ensure!( + is_semantic_warming( + required(&transaction.expected, "Warming Expected projection")?, + required(&transaction.activation, "Warming activation")?, + incumbent_lease_sha256.as_deref(), + &authenticated, + )?, + "durable warming evidence no longer satisfies the Warming gate" + ); + SequenceAdmittedWarming::from_topology( + transaction.transaction_id.clone(), + evidence, + authenticated, + ) + } + WarmingEvidence::FirstOdinDirect { evidence } => { + let present = self.authenticate_first_odin_warming_presence( + transaction, + &evidence.message_id, + evidence.challenged_at_unix_millis, + if require_current { + now + } else { + evidence.admitted_at_unix_millis + }, + &evidence.canonical_bytes, + )?; + SequenceAdmittedWarming::from_first_odin_presence( + transaction.transaction_id.clone(), + evidence, + present, + ) + } + } } fn rehydrate_ready_token( @@ -3399,16 +4607,32 @@ impl Engine { && admitted.value.plan.plan_id == *plan_id && admitted.value.sealed_release.sealed_release_id == *sealed_release_id && admitted.value.expected.canonical_sha256()? == *expected_projection_sha256 - && token.evidence_sha256() == odin_topology_correlation_sha256 - && token.publisher_sequence() == *odin_topology_publisher_sequence, + && token.publisher_sequence() >= *odin_topology_publisher_sequence, "selected dependency authority changed before actuation" ); - if selection.requirement.startup == StartupOrder::BeforeStart { - ensure!( - token.authenticated().record().ready, - "before-start dependency is not Ready" - ); - } + ensure!( + token.publisher_sequence() != *odin_topology_publisher_sequence + || token.evidence_sha256() == odin_topology_correlation_sha256, + "selected dependency Odin sequence changed evidence" + ); + ensure!( + token + .authenticated() + .record() + .observed_capabilities + .iter() + .any(|capability| { + capability_compatible( + &selection.requirement.capability, + &selection.requirement.schema, + &selection.requirement.compatibility, + &capability.capability, + &capability.schema, + &capability.compatibility, + ) && capability.capacity >= selection.requirement.minimum_capacity + }), + "selected dependency no longer provides its required capability" + ); } Ok(()) } @@ -3531,29 +4755,35 @@ impl Engine { let snapshot = ControlSnapshot::read(&self.options.state_store)?; if let Some(incumbent) = self.exact_incumbent(&snapshot, ¤t.value)? { let topology = self.topology(); - let expected_sha256 = topology.publish_expected(&incumbent.value.expected)?; + let failed_provider_anchor = self.provider_anchor_for_plan(required( + ¤t.value.plan, + "failed transaction plan", + )?)?; + let admitted_provider_anchor = + self.provider_anchor_for_plan(&incumbent.value.plan)?; + let expected_sha256 = topology.restore_admitted_expected_only( + expected, + &failed_provider_anchor, + current.value.activation.as_ref(), + &incumbent.value.expected, + &admitted_provider_anchor, + &incumbent.value.activation, + incumbent.value.leasing.lease(), + )?; ensure!( expected_sha256 == incumbent.value.expected.canonical_sha256()?, "restored incumbent Expected receipt differs" ); - if let Ok(observation) = self.workload.observe( - &incumbent.value.expected, - &incumbent.value.activation, - &incumbent.value.workload, - ) { - let activation_sha256 = topology.publish_observed_activation( - &incumbent.value.expected, - &incumbent.value.activation, - &observation, - )?; - ensure!( - activation_sha256 == incumbent.value.activation.canonical_sha256()?, - "restored incumbent activation receipt differs" - ); - } } else { + let plan = required(¤t.value.plan, "failed transaction plan")?; + let provider_anchor = self.provider_anchor_for_plan(plan)?; self.topology() - .withdraw_expected(expected) + .withdraw_expected( + expected, + &provider_anchor, + current.value.activation.as_ref(), + None, + ) .context("withdrawing exact failed Expected projection")?; } return self.persist_same_phase(current, |next| { @@ -3600,6 +4830,21 @@ impl Engine { workload, } = &cleanup.incumbent { + let binding = required(¤t.value.plan, "committed transaction plan")? + .parsed_inputs()? + .1; + if let Some(promoted_at_unix_millis) = required( + ¤t.value.routing, + "committed transaction routing evidence", + )? + .promoted_at_unix_millis() + { + let retire_not_before = + route_drain_deadline(promoted_at_unix_millis, binding.rollout.drain_seconds)?; + if now_millis()? < retire_not_before { + return Ok(()); + } + } self.workload .stop(workload) .with_context(|| format!("retiring admitted incumbent {generation_id}"))?; @@ -3627,6 +4872,50 @@ impl Engine { } } +fn route_drain_deadline(promoted_at_unix_millis: u64, drain_seconds: u32) -> Result { + promoted_at_unix_millis + .checked_add( + u64::from(drain_seconds) + .checked_mul(1_000) + .context("route drain duration overflows milliseconds")?, + ) + .context("route drain deadline overflows Unix milliseconds") +} + +fn route_observation_is_current( + observed_at_unix_millis: u64, + now_unix_millis: u64, + maximum_age_millis: u64, + maximum_future_skew_millis: u64, +) -> bool { + observed_at_unix_millis <= now_unix_millis.saturating_add(maximum_future_skew_millis) + && now_unix_millis.saturating_sub(observed_at_unix_millis) <= maximum_age_millis +} + +fn provider_warming_advanced( + prior_odin_sequence: u64, + prior_signed_presence_sha256: &str, + candidate_odin_sequence: u64, + candidate_signed_presence_sha256: &str, +) -> bool { + candidate_odin_sequence > prior_odin_sequence + && candidate_signed_presence_sha256 != prior_signed_presence_sha256 +} + +fn may_rollback_route_after_failed_proof(fencing: &FencingEvidence) -> bool { + matches!(fencing, FencingEvidence::SkippedStateless) +} + +fn incumbent_was_stopped_during_fencing(fencing: &FencingEvidence) -> bool { + matches!( + fencing, + FencingEvidence::Revoked { + incumbent_lease_sha256: Some(_), + .. + } + ) +} + fn candidate_cleanup_requirement( has_prepared_activation: bool, has_workload_observation: bool, @@ -3734,13 +5023,54 @@ fn validate_authenticated_evidence( Ok(()) } -fn is_semantic_warming(authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation) -> bool { +fn is_semantic_warming( + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + incumbent_lease_sha256: Option<&str>, + authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation, +) -> Result { let record = authenticated.record(); - record.present - && !record.ready - && record.observed_presence_state.as_deref() == Some("warming") - && record.observed_write_lease_sha256.is_none() - && record.disagreements.is_empty() + if !record.present || record.observed_write_lease_sha256.is_some() { + return Ok(false); + } + let expected_projection_detail = format!( + "expected:{};activation:{}", + expected.canonical_sha256()?, + activation.canonical_sha256()? + ); + if !warming_disagreements_match_incumbent( + &expected_projection_detail, + incumbent_lease_sha256, + &record.disagreements, + ) { + return Ok(false); + } + Ok(if expected.write_lease_required { + !record.ready && record.observed_presence_state.as_deref() == Some("warming") + } else { + matches!( + record.observed_presence_state.as_deref(), + Some("warming" | "active") + ) + }) +} + +fn warming_disagreements_match_incumbent( + expected_projection_detail: &str, + incumbent_lease_sha256: Option<&str>, + disagreements: &[OdinTopologyDisagreement], +) -> bool { + if disagreements.is_empty() { + true + } else if let (Some(incumbent_lease_sha256), [disagreement]) = + (incumbent_lease_sha256, disagreements) + { + disagreement.code == "projected-write-lease" + && disagreement.expected.as_deref() == Some(expected_projection_detail) + && disagreement.observed.as_deref() == Some(incumbent_lease_sha256) + } else { + false + } } fn is_semantic_ready(authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation) -> bool { @@ -3928,6 +5258,28 @@ fn usage() -> &'static str { mod tests { use super::*; + fn digest(byte: char) -> String { + byte.to_string().repeat(64) + } + + fn route_binding( + route_id: &str, + stable_port: u16, + private_port_start: u16, + private_port_end: u16, + ) -> RouteBinding { + RouteBinding { + driver: crate::deployment::RouteDriver::NginxStreamTcp, + route_id: route_id.into(), + stable_endpoint: format!("tcp://127.0.0.1:{stable_port}"), + private_host: "127.0.0.1".into(), + private_port_start, + private_port_end, + config_path: PathBuf::from(format!("/etc/nginx/idunn-stream-routes/{route_id}.conf")), + reload_unit: "nginx.service".into(), + } + } + fn command(kind: CommandKind) -> DeploymentCommand { DeploymentCommand { schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), @@ -4055,6 +5407,37 @@ mod tests { } } + #[test] + fn route_bindings_are_one_global_socket_and_candidate_authority_map() { + let first = route_binding("first", 4103, 14103, 14111); + let second = route_binding("second", 8831, 18831, 18839); + validate_route_binding_set(&[("first", &first), ("second", &second)]).unwrap(); + + let mut duplicate_id = second.clone(); + duplicate_id.route_id = first.route_id.clone(); + assert!( + validate_route_binding_set(&[("first", &first), ("second", &duplicate_id)]).is_err() + ); + + let mut overlapping_candidates = second.clone(); + overlapping_candidates.private_port_start = 14111; + overlapping_candidates.private_port_end = 14120; + assert!( + validate_route_binding_set(&[("first", &first), ("second", &overlapping_candidates),]) + .is_err() + ); + + let mut stable_inside_other_range = second; + stable_inside_other_range.stable_endpoint = "tcp://127.0.0.1:14105".into(); + assert!( + validate_route_binding_set(&[ + ("first", &first), + ("second", &stable_inside_other_range), + ]) + .is_err() + ); + } + #[test] fn deployment_command_is_immutable_positional_fact() -> Result<()> { let value = command(CommandKind::Deploy); @@ -4079,9 +5462,11 @@ mod tests { DeploymentPhase::Committing, DeploymentPhase::Complete, ]; - assert!(phases - .windows(2) - .all(|pair| pair[1] as u8 == pair[0] as u8 + 1)); + assert!( + phases + .windows(2) + .all(|pair| pair[1] as u8 == pair[0] as u8 + 1) + ); } #[test] @@ -4103,6 +5488,44 @@ mod tests { Ok(()) } + #[test] + fn odin_sequence_cursors_are_scoped_by_target_and_signer() -> Result<()> { + let command = command(CommandKind::Deploy); + let mut ghostlight = + DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; + ghostlight.latest_odin_observation = Some(topology(7, 1)); + ghostlight.odin_publisher_sequence_cursor = 7; + let mut odin = DeploymentTransaction::new(&command, "odin".into(), 1, None, 100)?; + odin.latest_odin_observation = Some(topology(41, 2)); + odin.odin_publisher_sequence_cursor = 41; + let envelope = |key: &str| CultCacheEnvelope { + key: key.into(), + r#type: DeploymentTransaction::TYPE.into(), + payload: Vec::new(), + stored_at: "1970-01-01T00:00:00.100Z".into(), + schema_id: Some(DEPLOYMENT_TRANSACTION_SCHEMA.into()), + }; + let snapshot = ControlSnapshot { + commands: Vec::new(), + transactions: vec![ + Stored { + envelope: envelope("ghostlight"), + value: ghostlight, + }, + Stored { + envelope: envelope("odin"), + value: odin, + }, + ], + admitted: Vec::new(), + }; + + assert_eq!(snapshot.max_odin_sequence("ghostlight", "odin-signer"), 7); + assert_eq!(snapshot.max_odin_sequence("odin", "odin-signer"), 41); + assert_eq!(snapshot.max_odin_sequence("ghostlight", "other-signer"), 0); + Ok(()) + } + #[test] fn stale_odin_provider_receipt_cannot_gate_continuity_restart() -> Result<()> { let touched = std::cell::Cell::new(false); @@ -4111,10 +5534,12 @@ mod tests { bail!("Odin provider receipt is stale") })?; assert!(!touched.get()); - assert!(validate_live_providers_for_deploy(CommandKind::Deploy, || { - bail!("Odin provider receipt is stale") - }) - .is_err()); + assert!( + validate_live_providers_for_deploy(CommandKind::Deploy, || { + bail!("Odin provider receipt is stale") + }) + .is_err() + ); Ok(()) } @@ -4228,6 +5653,103 @@ mod tests { Ok(()) } + #[test] + fn routed_incumbent_retirement_waits_for_the_declared_drain_deadline() -> Result<()> { + assert_eq!(route_drain_deadline(1_000, 30)?, 31_000); + assert!(route_drain_deadline(u64::MAX, 1).is_err()); + Ok(()) + } + + #[test] + fn admitted_route_receipt_refreshes_when_stale_or_implausibly_future_dated() { + assert!(route_observation_is_current(900, 1_000, 100, 10)); + assert!(route_observation_is_current(1_010, 1_000, 100, 10)); + assert!(!route_observation_is_current(899, 1_000, 100, 10)); + assert!(!route_observation_is_current(1_011, 1_000, 100, 10)); + } + + #[test] + fn lease_warming_requires_both_new_odin_and_new_provider_evidence() { + let old = digest('1'); + let fresh = digest('2'); + assert!(!provider_warming_advanced(7, &old, 7, &fresh)); + assert!(!provider_warming_advanced(7, &old, 8, &old)); + assert!(provider_warming_advanced(7, &old, 8, &fresh)); + } + + #[test] + fn failed_route_proof_never_restores_any_fenced_incumbent() { + assert!(!may_rollback_route_after_failed_proof( + &FencingEvidence::Revoked { + incumbent_lease_sha256: Some(digest('1')), + candidate_lease_path_verified_empty: false, + } + )); + assert!(!may_rollback_route_after_failed_proof( + &FencingEvidence::Revoked { + incumbent_lease_sha256: None, + candidate_lease_path_verified_empty: true, + } + )); + assert!(may_rollback_route_after_failed_proof( + &FencingEvidence::SkippedStateless + )); + } + + #[test] + fn fenced_writer_is_already_retired_before_post_commit_cleanup() { + assert!(incumbent_was_stopped_during_fencing( + &FencingEvidence::Revoked { + incumbent_lease_sha256: Some(digest('1')), + candidate_lease_path_verified_empty: false, + } + )); + assert!(!incumbent_was_stopped_during_fencing( + &FencingEvidence::Revoked { + incumbent_lease_sha256: None, + candidate_lease_path_verified_empty: true, + } + )); + assert!(!incumbent_was_stopped_during_fencing( + &FencingEvidence::SkippedStateless + )); + } + + #[test] + fn warming_accepts_only_the_exact_projected_incumbent_lease_disagreement() { + let expected_detail = format!("expected:{};activation:{}", digest('1'), digest('2')); + let incumbent = digest('3'); + let exact = OdinTopologyDisagreement { + code: "projected-write-lease".into(), + expected: Some(expected_detail.clone()), + observed: Some(incumbent.clone()), + }; + + assert!(warming_disagreements_match_incumbent( + &expected_detail, + Some(&incumbent), + &[exact.clone()], + )); + assert!(!warming_disagreements_match_incumbent( + &expected_detail, + None, + &[exact.clone()], + )); + + let mut substituted = exact.clone(); + substituted.observed = Some(digest('4')); + assert!(!warming_disagreements_match_incumbent( + &expected_detail, + Some(&incumbent), + &[substituted], + )); + assert!(!warming_disagreements_match_incumbent( + &expected_detail, + Some(&incumbent), + &[exact.clone(), exact], + )); + } + #[test] fn candidate_and_incumbent_must_differ_in_all_three_native_boundaries() { let incumbent = workload(1001, 40, 50); @@ -4305,6 +5827,7 @@ mod tests { assert!(!transaction.is_terminal()); assert!(!transaction.owns_target_authority()); + assert!(transaction.blocks_new_target_mutation()); Ok(()) } diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs index ded7e2b..bffd316 100644 --- a/crates/idunn-daemon/src/deployment.rs +++ b/crates/idunn-daemon/src/deployment.rs @@ -1,7 +1,8 @@ use std::collections::{BTreeMap, BTreeSet}; +use std::net::IpAddr; use std::path::{Component, Path, PathBuf}; -use anyhow::{bail, ensure, Context, Result}; +use anyhow::{Context, Result, bail, ensure}; use serde::{Deserialize, Serialize}; pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; @@ -425,11 +426,45 @@ pub struct RouteBinding { pub reload_unit: String, } +impl RouteBinding { + pub(crate) fn stable_socket(&self) -> Result<(IpAddr, u16)> { + let authority = match self.driver { + RouteDriver::NginxStreamTcp => self + .stable_endpoint + .strip_prefix("http://") + .or_else(|| self.stable_endpoint.strip_prefix("tcp://")), + RouteDriver::NginxStreamUdp => self.stable_endpoint.strip_prefix("rudp://"), + } + .context("stable endpoint transport does not match its route driver")?; + ensure!( + !authority + .chars() + .any(|character| matches!(character, '/' | '?' | '#' | '@' | '[' | ']')), + "stable endpoint is not a plain IP address and port" + ); + let (host, port) = authority + .rsplit_once(':') + .context("stable endpoint has no port")?; + let host = host + .parse::() + .context("stable endpoint host is not an IP address")?; + ensure!( + matches!(host, IpAddr::V4(_)), + "route binding v2 stable endpoints use explicit IPv4 addresses" + ); + let port = port + .parse::() + .context("stable endpoint port is not a u16")?; + ensure!(port > 0, "stable endpoint port is zero"); + Ok((host, port)) + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum RouteDriver { - NginxHttp, NginxStreamTcp, + NginxStreamUdp, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -1027,6 +1062,14 @@ impl OperatorBinding { require_id(&route.route_id, "route id")?; require_value(&route.stable_endpoint, "stable endpoint")?; require_host(&route.private_host)?; + let private_host: IpAddr = route + .private_host + .parse() + .context("route private host is not an IP address")?; + ensure!( + private_host.is_loopback(), + "systemd-transient candidate routes must use a loopback private host" + ); ensure!( route.private_port_start > 0, "private port range starts at zero" @@ -1035,6 +1078,7 @@ impl OperatorBinding { route.private_port_start < route.private_port_end, "candidate rollout requires at least two private ports" ); + route.stable_socket()?; require_absolute_path(&route.config_path, "route config")?; require_unit(&route.reload_unit, "route reload unit")?; } @@ -1301,11 +1345,10 @@ impl OperatorBinding { self.route.as_ref(), ) { (true, ServiceTransport::Http, Some(route)) - if route.driver == RouteDriver::NginxHttp => + if route.driver == RouteDriver::NginxStreamTcp => { ensure!( - route.stable_endpoint.starts_with("http://") - || route.stable_endpoint.starts_with("https://"), + route.stable_endpoint.starts_with("http://"), "HTTP stable endpoint is not an HTTP URI" ); } @@ -1317,6 +1360,14 @@ impl OperatorBinding { "TCP stable endpoint is not a TCP URI" ); } + (true, ServiceTransport::Rudp, Some(route)) + if route.driver == RouteDriver::NginxStreamUdp => + { + ensure!( + route.stable_endpoint.starts_with("rudp://"), + "RUDP stable endpoint is not an RUDP URI" + ); + } (false, _, None) => {} _ => bail!("route binding does not match the launch contract"), } @@ -1681,13 +1732,13 @@ expected_signer_identity_id = "test-service-runtime-signer" trust_anchor_store = "/etc/gamecult/trust/test-service.cc" [route] -driver = "nginx-http" +driver = "nginx-stream-tcp" route_id = "test-service-public" -stable_endpoint = "https://yggdrasil.gamecult.org/test-service/" +stable_endpoint = "http://127.0.0.1:18830" private_host = "127.0.0.1" private_port_start = 18831 private_port_end = 18839 -config_path = "/etc/nginx/idunn-routes/test-service.conf" +config_path = "/etc/nginx/idunn-stream-routes/test-service.conf" reload_unit = "nginx.service" [process_write_lease] @@ -1765,16 +1816,33 @@ nodes = ["yggdrasil"] #[test] fn route_transport_mismatch_is_rejected() { let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let binding = - OperatorBinding::parse(&BINDING.replace("nginx-http", "nginx-stream-tcp")).unwrap(); - assert!(binding.admit(&recipe).is_err()); + assert!( + OperatorBinding::parse(&BINDING.replace("nginx-stream-tcp", "nginx-stream-udp")) + .is_err() + ); - let binding = OperatorBinding::parse(&BINDING.replace( - "https://yggdrasil.gamecult.org/test-service/", - "tcp://yggdrasil.gamecult.org:443", - )) + assert!( + OperatorBinding::parse( + &BINDING.replace("http://127.0.0.1:18830", "tcp://yggdrasil.gamecult.org:443",), + ) + .is_err() + ); + assert!(recipe.service.route_required); + } + + #[test] + fn rudp_route_uses_the_udp_stream_driver() { + let recipe = TargetDeclaration::parse( + &RECIPE.replace("transport = \"http\"", "transport = \"rudp\""), + ) .unwrap(); - assert!(binding.admit(&recipe).is_err()); + let binding = OperatorBinding::parse( + &BINDING + .replace("nginx-stream-tcp", "nginx-stream-udp") + .replace("http://127.0.0.1:18830", "rudp://127.0.0.1:17872"), + ) + .unwrap(); + assert!(binding.admit(&recipe).is_ok()); } #[test] @@ -1892,24 +1960,30 @@ nodes = ["yggdrasil"] #[test] fn v2_binding_is_explicitly_singleton() { - assert!(OperatorBinding::parse( - &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") - ) - .is_err()); - assert!(OperatorBinding::parse(&BINDING.replace( - "nodes = [\"yggdrasil\"]", - "nodes = [\"yggdrasil\", \"raven\"]" - )) - .is_err()); + assert!( + OperatorBinding::parse( + &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") + ) + .is_err() + ); + assert!( + OperatorBinding::parse(&BINDING.replace( + "nodes = [\"yggdrasil\"]", + "nodes = [\"yggdrasil\", \"raven\"]" + )) + .is_err() + ); } #[test] fn legacy_static_workload_identity_has_no_authority_in_v2() { - assert!(OperatorBinding::parse(&BINDING.replace( - "schema = \"gamecult.idunn.operator_binding.v2\"", - "schema = \"gamecult.idunn.operator_binding.v1\"", - ),) - .is_err()); + assert!( + OperatorBinding::parse(&BINDING.replace( + "schema = \"gamecult.idunn.operator_binding.v2\"", + "schema = \"gamecult.idunn.operator_binding.v1\"", + ),) + .is_err() + ); let with_static_identity = BINDING.replace( "driver = \"systemd-transient\"", "driver = \"systemd-transient\"\nuser = \"test-service\"\ngroup = \"test-service\"", @@ -1923,10 +1997,12 @@ nodes = ["yggdrasil"] OperatorBinding::parse(&BINDING.replace("state_group = \"test-service\"\n", ""),) .is_err() ); - assert!(OperatorBinding::parse( - &BINDING.replace("state_group = \"test-service\"", "state_group = \"root\""), - ) - .is_err()); + assert!( + OperatorBinding::parse( + &BINDING.replace("state_group = \"test-service\"", "state_group = \"root\""), + ) + .is_err() + ); } #[test] diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs index ca4469a..9475701 100644 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ b/crates/idunn-daemon/src/deployment_plan.rs @@ -1,20 +1,20 @@ use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; -use anyhow::{bail, ensure, Context, Result}; +use anyhow::{Context, Result, bail, ensure}; pub use cultnet_rs::IdunnExpectedIncarnationRecord as ExpectedIncarnation; use cultnet_rs::{ - IdunnExpectedCapability, IdunnExpectedDependency, IdunnExpectedRoute, - IDUNN_EXPECTED_INCARNATION_SCHEMA, + IDUNN_EXPECTED_INCARNATION_SCHEMA, IdunnExpectedCapability, IdunnExpectedDependency, + IdunnExpectedRoute, }; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use crate::control_plane::SequenceAdmittedReady; use crate::deployment::{ - capability_compatible, CapabilityDependency, DependencyKind, ExternalCapabilityBinding, - OperatorBinding, ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, - TargetDeclaration, + CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, + ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration, + capability_compatible, }; pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; @@ -286,11 +286,11 @@ fn managed_ready_provider_refs( let endpoint = expected .route .as_ref() - .map(|route| route.candidate_endpoint.clone()); + .map(|route| route.stable_endpoint.clone()); let mut providers = Vec::new(); for capability in &observed.observed_capabilities { providers.push(DependencyProviderRef { - provider_id: observed.runtime_id.clone(), + provider_id: expected.target.clone(), authority: DependencyProviderAuthority::ManagedReady { target: expected.target.clone(), incarnation_id: expected.incarnation_id.clone(), @@ -995,11 +995,12 @@ fn sha256_id(bytes: &[u8]) -> String { mod tests { use super::*; use cultnet_rs::{ + GameCultProviderHealthIdentity, GameCultRuntimeCapability, IdunnRuntimeActivationLaunch, + IdunnServiceIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, + OdinRuntimeTopologyCorrelationPurpose, OdinRuntimeTopologyCorrelationRecord, + OdinTopologyAuthenticationContext, OdinTopologyIdentity, authenticate_odin_runtime_topology_correlation, enroll_service_identity_at, - verify_runtime_authority, GameCultProviderHealthIdentity, GameCultRuntimeCapability, - IdunnRuntimeActivationLaunch, IdunnServiceIdentity, OdinRuntimeTopologyCorrelationPurpose, - OdinRuntimeTopologyCorrelationRecord, OdinTopologyAuthenticationContext, - OdinTopologyIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, + verify_runtime_authority, }; const RECIPE: &str = r#" @@ -1113,13 +1114,13 @@ expected_signer_identity_id = "service-runtime-signer" trust_anchor_store = "/etc/gamecult/trust/service.cc" [route] -driver = "nginx-http" +driver = "nginx-stream-tcp" route_id = "service" -stable_endpoint = "https://example.invalid/service/" +stable_endpoint = "http://127.0.0.1:17999" private_host = "127.0.0.1" private_port_start = 18000 private_port_end = 18009 -config_path = "/etc/nginx/idunn-routes/service.conf" +config_path = "/etc/nginx/idunn-stream-routes/service.conf" reload_unit = "nginx.service" [brakes] @@ -1189,7 +1190,7 @@ nodes = ["yggdrasil"] } } - fn ready_odin_provider(runtime_id: &str, capacity: u32) -> SequenceAdmittedReady { + fn ready_odin_provider(target: &str, runtime_id: &str, capacity: u32) -> SequenceAdmittedReady { (|| -> Result { let root = tempfile::tempdir()?; let provider = enroll_service_identity_at::( @@ -1200,6 +1201,7 @@ nodes = ["yggdrasil"] let odin_signer = enroll_service_identity_at::(&root.path().join("odin.cc"))?; let mut expected = odin(); + expected.target = target.into(); expected.runtime_id = runtime_id.into(); expected.expected_signer_identity_id = provider.entry().identity_id.clone(); let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest(b'8'), 100, &idunn)?; @@ -1262,7 +1264,7 @@ nodes = ["yggdrasil"] } fn plan() -> CompiledDeploymentPlan { - let providers = [ready_odin_provider("odin-yggdrasil", 1)]; + let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; plan_with(&providers) } @@ -1302,7 +1304,7 @@ nodes = ["yggdrasil"] #[test] fn compiler_parses_the_exact_bytes_it_receipts() { - let providers = [ready_odin_provider("odin-yggdrasil", 1)]; + let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; let first = plan_with(&providers); let second = plan_with(&providers); assert_eq!(first, second); @@ -1316,17 +1318,19 @@ nodes = ["yggdrasil"] "contract = \"service.health\"", "contract = \"service.health.v2\"", ); - let providers = [ready_odin_provider("odin-yggdrasil", 1)]; - assert!(compile_deployment_plan( - changed.as_bytes(), - BINDING.as_bytes(), - source(RECIPE), - "service-incarnation-2", - Some(18002), - 111, - &providers, - ) - .is_err()); + let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; + assert!( + compile_deployment_plan( + changed.as_bytes(), + BINDING.as_bytes(), + source(RECIPE), + "service-incarnation-2", + Some(18002), + 111, + &providers, + ) + .is_err() + ); let mut corrupted = first; corrupted .binding_blob @@ -1382,8 +1386,8 @@ nodes = ["yggdrasil"] fn dependency_selection_uses_observed_ready_capacity_and_stable_identity_order() { let declaration = TargetDeclaration::parse(RECIPE).unwrap(); let admitted = [ - ready_odin_provider("odin-z", 4), - ready_odin_provider("odin-yggdrasil", 2), + ready_odin_provider("odin-z", "odin-runtime-z", 4), + ready_odin_provider("odin-yggdrasil", "odin-runtime-yggdrasil", 2), ]; let providers = admitted .iter() @@ -1395,6 +1399,10 @@ nodes = ["yggdrasil"] "odin-yggdrasil" ); assert_eq!(selected[0].provider.as_ref().unwrap().capacity, 2); + assert_eq!( + selected[0].provider.as_ref().unwrap().endpoint.as_deref(), + Some("tcp://10.77.0.1:17871") + ); assert!(matches!( &selected[0].provider.as_ref().unwrap().authority, DependencyProviderAuthority::ManagedReady { @@ -1459,7 +1467,7 @@ nodes = ["yggdrasil"] assert!(!expected.write_lease_required); assert_eq!( expected.dependencies[0].provider_id.as_deref(), - Some("odin-yggdrasil") + Some("odin") ); assert_eq!( expected.dependencies[0].provider_authority.as_deref(), @@ -1494,7 +1502,7 @@ nodes = ["yggdrasil"] "service-incarnation-sorted", Some(18002), 111, - &[ready_odin_provider("odin-yggdrasil", 1)], + &[ready_odin_provider("odin", "odin-yggdrasil", 1)], ) .unwrap(); let release = SealedRelease::new( diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs index c4bfe5e..1554408 100644 --- a/crates/idunn-daemon/src/drivers.rs +++ b/crates/idunn-daemon/src/drivers.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; use std::ffi::{OsStr, OsString}; use std::fs::{self, OpenOptions}; use std::io::{ErrorKind, Read, Write}; +use std::net::{SocketAddr, TcpStream}; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; use std::thread; @@ -10,8 +11,9 @@ use std::time::Duration; use anyhow::{Context, Result, bail, ensure}; use cultcache_rs::{ CacheBackingStore, CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, - SingleFileMessagePackBackingStore, + SingleFileMessagePackBackingStore, TryCompareExchangeSnapshotOutcome, }; +use cultmesh_rs::{CultMeshRudpSnapshotOptions, request_raw_snapshot_from_rudp_catalog}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use uuid::Uuid; @@ -29,11 +31,16 @@ use crate::deployment_plan::{ SOURCE_SELECTION_FACTS_SCHEMA, SealedRelease, SourceSelection, SourceSelectionFacts, }; use cultnet_rs::{ - GameCultProviderHealthIdentity, IDUNN_EXPECTED_INCARNATION_SCHEMA, + CultNetMessage, CultNetRawPayloadEncoding, CultNetWireContract, + GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultProviderHealthIdentity, + GameCultServiceTrustAnchorRecord, IDUNN_EXPECTED_INCARNATION_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, OdinRuntimeTopologyCorrelationRecord, + ServiceIdentityProfile, ServiceIdentityTrustAnchor, decode_cultnet_message_from_slice, + derive_service_identity_id, encode_cultnet_message_to_vec, encode_frame, open_service_identity_credential_reader, }; @@ -293,14 +300,36 @@ pub trait WorkloadPort { } pub trait TopologyPort { - fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result; - fn withdraw_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()>; + fn publish_expected( + &self, + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, + ) -> Result; + fn withdraw_expected( + &self, + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, + activation: Option<&IdunnRuntimeActivationRecord>, + lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result<()>; fn publish_observed_activation( &self, expected: &IdunnExpectedIncarnationRecord, activation: &IdunnRuntimeActivationRecord, observation: &WorkloadObservation, ) -> Result; + fn publish_process_write_lease( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result; + fn withdraw_process_write_lease( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result<()>; fn receive(&self, target: &str) -> Result>; } @@ -313,6 +342,7 @@ pub struct ReceivedOdinTopologyCorrelation { pub trait WriteLeasePort { fn revoke_exact(&self, incumbent: Option<&IdunnProcessWriteLeaseRecord>) -> Result<()>; fn observe_empty(&self) -> Result; + fn observe_exact(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; fn grant( &self, expected: &IdunnExpectedIncarnationRecord, @@ -334,6 +364,22 @@ pub struct RouteObservation { pub route_id: String, pub runtime_instance_id: String, pub membership_sha256: String, + pub signed_presence_sha256: String, + pub observed_at_unix_millis: u64, +} + +impl RouteObservation { + pub fn validate(&self) -> Result<()> { + require_driver_id(&self.route_id, "route observation")?; + require_sha256_id(&self.runtime_instance_id, "route runtime instance")?; + require_sha256_id(&self.membership_sha256, "route membership")?; + require_sha256_id(&self.signed_presence_sha256, "route signed presence")?; + ensure!( + self.observed_at_unix_millis > 0, + "route observation has no trusted receipt time" + ); + Ok(()) + } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -343,26 +389,53 @@ pub struct RoutePreflightReceipt { pub candidate_membership_sha256: String, pub incumbent_runtime_instance_id: Option, pub incumbent_membership_sha256: Option, + pub incumbent_configuration: Option>, } -pub trait RoutePort { - fn preflight( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - incumbent: Option<&RouteObservation>, - ) -> Result; - fn promote( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - preflight: &RoutePreflightReceipt, - ) -> Result; - fn observe( - &self, - expected: &IdunnExpectedIncarnationRecord, - observation: &RouteObservation, - ) -> Result; +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RouteSnapshotResponse { + pub message_id: String, + pub canonical_presence: Vec, +} + +const ROUTE_SNAPSHOT_TIMEOUT: Duration = Duration::from_secs(3); +const ROUTE_SNAPSHOT_MAX_BYTES: usize = 1024 * 1024; +const ROUTE_HTTP_MAX_HEADER_BYTES: usize = 32 * 1024; +const ROUTE_HTTP_SNAPSHOT_PATH: &str = "/cultnet/snapshot"; + +impl RoutePreflightReceipt { + pub fn validate(&self) -> Result<()> { + require_driver_id(&self.route_id, "route preflight")?; + require_sha256_id( + &self.candidate_runtime_instance_id, + "candidate route runtime instance", + )?; + require_sha256_id( + &self.candidate_membership_sha256, + "candidate route membership", + )?; + match ( + &self.incumbent_runtime_instance_id, + &self.incumbent_membership_sha256, + &self.incumbent_configuration, + ) { + (None, None, None) => {} + (Some(runtime), Some(membership), Some(configuration)) => { + require_sha256_id(runtime, "incumbent route runtime instance")?; + require_sha256_id(membership, "incumbent route membership")?; + ensure!( + !configuration.is_empty(), + "incumbent route configuration is empty" + ); + ensure!( + sha256_id(configuration) == *membership, + "incumbent route configuration differs from its membership digest" + ); + } + _ => bail!("incumbent route preflight evidence is partial"), + } + Ok(()) + } } /// Fixed-argv Git source driver. It never interprets recipe text as a command @@ -1295,7 +1368,7 @@ impl RunnerPort for DockerRunnerDriver { &self, source: &FrozenSource, plan: &CompiledDeploymentPlan, - staging_root: &Path, + staging_parent: &Path, sealed_at_unix_millis: u64, ) -> Result { plan.validate()?; @@ -1305,11 +1378,12 @@ impl RunnerPort for DockerRunnerDriver { "runner source differs from the compiled plan" ); let (declaration, binding) = plan.parsed_inputs()?; - ensure!( - !staging_root.exists(), - "release staging root already exists" - ); - fs::create_dir_all(staging_root)?; + fs::create_dir_all(staging_parent)?; + let staging_root = staging_parent.join(&source.receipt.transaction_id); + if staging_root.exists() { + remove_tree_inside(staging_parent, &staging_root)?; + } + fs::create_dir_all(&staging_root)?; let mut workspaces = BTreeMap::new(); for runner_id in binding.runners.keys() { @@ -1375,12 +1449,12 @@ impl RunnerPort for DockerRunnerDriver { &declaration, source, &workspaces, - staging_root, + &staging_root, &artifact.id, )?); } for workspace in workspaces.values() { - remove_tree_inside(staging_root, workspace)?; + remove_tree_inside(&staging_root, workspace)?; } let release = SealedRelease::new( plan, @@ -1390,7 +1464,7 @@ impl RunnerPort for DockerRunnerDriver { )?; Ok(MaterializedRelease { release, - root: staging_root.to_owned(), + root: staging_root, }) } } @@ -2848,9 +2922,10 @@ impl WorkloadPort for SystemdTransientWorkloadDriver { } /// One dedicated CultCache file is the write-lease authority for one target. -/// Its sibling lock is shared by the target only around durable commit edges; -/// Idunn's compare-exchange therefore fences the old writer before replacing -/// the record. Route membership never enters this store. +/// The admitted target holds its sibling lock shared for that process lifetime. +/// Grant and revocation therefore make one nonblocking CAS attempt; revocation +/// follows an exact incumbent stop. A foreign or stuck holder cannot freeze +/// Idunn's control loop. Route membership never enters this store. pub struct CultCacheWriteLeaseDriver { pub target: String, pub record_path: PathBuf, @@ -2914,11 +2989,6 @@ impl CultCacheWriteLeaseDriver { lease.validate()?; let expected_sha256 = expected.canonical_sha256()?; let activation_sha256 = activation.canonical_sha256()?; - let warming = warming.authenticated().record(); - let warming_presence_sha256 = warming - .signed_presence_sha256 - .as_deref() - .context("warming observation has no signed presence")?; ensure!( self.target == expected.target && lease.target == expected.target @@ -2939,14 +3009,8 @@ impl CultCacheWriteLeaseDriver { .state_contract_sha256 .as_deref() .context("write-lease Expected has no state contract")? - && lease.warming_presence_sha256 == warming_presence_sha256 - && warming.present - && !warming.ready - && warming.observed_presence_state.as_deref() == Some("warming") - && warming.runtime_instance_id.as_deref() - == Some(activation.runtime_instance_id.as_str()) - && warming.observed_write_lease_sha256.is_none() - && warming.disagreements.is_empty(), + && lease.warming_presence_sha256 == warming.signed_presence_sha256() + && warming.runtime_instance_id() == activation.runtime_instance_id.as_str(), "process write lease does not bind the exact warming candidate" ); Ok(()) @@ -2971,17 +3035,32 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { "refusing to revoke an unexpected process write lease" ); let store = SingleFileMessagePackBackingStore::new(&self.record_path); - ensure!( - store.compare_exchange_snapshot(std::slice::from_ref(&envelope), &[])?, - "process write lease changed while fencing the incumbent" - ); - Ok(()) + match store.try_compare_exchange_snapshot(std::slice::from_ref(&envelope), &[])? { + TryCompareExchangeSnapshotOutcome::Exchanged => Ok(()), + TryCompareExchangeSnapshotOutcome::Mismatch => { + bail!("process write lease changed while fencing the incumbent") + } + TryCompareExchangeSnapshotOutcome::LockContended => { + bail!("process write lease is held by a shared-lock consumer") + } + } } fn observe_empty(&self) -> Result { Ok(self.current()?.is_none()) } + fn observe_exact(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { + lease.validate()?; + ensure!( + lease.target == self.target, + "observed process write lease belongs to another target" + ); + Ok(self + .current()? + .is_some_and(|(_, current)| current == *lease)) + } + fn grant( &self, expected: &IdunnExpectedIncarnationRecord, @@ -2995,20 +3074,21 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { current == *lease, "another process already owns the write lease" ); + harden_root_authority_file(&self.record_path)?; + harden_root_authority_file(&authority_lock_path(&self.record_path))?; return lease.canonical_sha256(); } let envelope = self.envelope(lease)?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.record_path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), - key: lease.target.clone(), - current: None, - }], - &[envelope], - )?, - "process write-lease grant lost its empty-store compare-exchange" - ); + let store = SingleFileMessagePackBackingStore::new(&self.record_path); + match store.try_compare_exchange_snapshot(&[], &[envelope])? { + TryCompareExchangeSnapshotOutcome::Exchanged => {} + TryCompareExchangeSnapshotOutcome::Mismatch => { + bail!("process write-lease grant lost its empty-store compare-exchange") + } + TryCompareExchangeSnapshotOutcome::LockContended => { + bail!("process write lease is held by a shared-lock consumer") + } + } harden_root_authority_file(&self.record_path)?; harden_root_authority_file(&authority_lock_path(&self.record_path))?; lease.canonical_sha256() @@ -3028,43 +3108,581 @@ impl WriteLeasePort for CultCacheWriteLeaseDriver { } } -/// The local CultCache projection is Idunn's typed provider surface into -/// CultMesh. Odin writes a separate signed correlation store. Reading that -/// store never lets Idunn manufacture Present or Ready from its own config. +/// Idunn projects desired identity plus its own activation and current-lease +/// facts here. Service presence is absent by construction; only Odin may +/// correlate these records with signed runtime observation into Present/Ready. pub struct CultCacheTopologyDriver { pub projection_store: PathBuf, pub correlation_store: PathBuf, } -impl TopologyPort for CultCacheTopologyDriver { - fn publish_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result { +impl CultCacheTopologyDriver { + /// Atomically demotes a failed pre-fence candidate back to the admitted + /// generation's Expected projection. Runtime activation and write-lease + /// records are deliberately absent until admitted-body supervision proves + /// the physical process and lease again. + pub fn restore_admitted_expected_only( + &self, + failed_expected: &IdunnExpectedIncarnationRecord, + failed_provider_anchor: &ServiceIdentityTrustAnchor, + failed_activation: Option<&IdunnRuntimeActivationRecord>, + admitted_expected: &IdunnExpectedIncarnationRecord, + admitted_provider_anchor: &ServiceIdentityTrustAnchor, + admitted_activation: &IdunnRuntimeActivationRecord, + admitted_lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result { + failed_expected.validate()?; + admitted_expected.validate()?; + ensure!( + failed_expected.target == admitted_expected.target, + "projection restoration crosses deployment targets" + ); + let failed_anchor = runtime_presence_trust_anchor(failed_expected, failed_provider_anchor)?; + let admitted_anchor = + runtime_presence_trust_anchor(admitted_expected, admitted_provider_anchor)?; + if let Some(activation) = failed_activation { + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == failed_expected.canonical_sha256()?, + "failed activation does not bind the failed Expected projection" + ); + } + admitted_activation.validate()?; + ensure!( + admitted_activation.expected_projection_sha256 + == admitted_expected.canonical_sha256()?, + "admitted activation does not bind the admitted Expected projection" + ); + if let Some(lease) = admitted_lease { + validate_topology_lease(admitted_expected, admitted_activation, lease)?; + } + if let Some(parent) = self.projection_store.parent() { + fs::create_dir_all(parent)?; + } + let store = SingleFileMessagePackBackingStore::new(&self.projection_store); + let target = admitted_expected.target.as_str(); + let anchor_key = runtime_presence_trust_anchor_id(target); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let current_expected = + projection_entry(&entries, IdunnExpectedIncarnationRecord::TYPE, target)?; + if let Some(envelope) = current_expected { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA), + "restored Expected projection schema is foreign" + ); + let current = IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)?; + ensure!( + current == *failed_expected || current == *admitted_expected, + "refusing to replace an unknown Expected projection" + ); + } + let current_anchor = projection_entry( + &entries, + GameCultServiceTrustAnchorRecord::TYPE, + &anchor_key, + )?; + if let Some(envelope) = current_anchor { + let current = service_trust_anchor_from_envelope(envelope)?; + ensure!( + current == failed_anchor || current == admitted_anchor, + "refusing to replace an unknown runtime presence trust anchor" + ); + } + let current_activation = + projection_entry(&entries, IdunnRuntimeActivationRecord::TYPE, target)?; + if let Some(envelope) = current_activation { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA), + "restored activation projection schema is foreign" + ); + let current = IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)?; + ensure!( + failed_activation == Some(¤t) || admitted_activation == ¤t, + "refusing to remove an unknown runtime activation projection" + ); + } + let current_lease = + projection_entry(&entries, IdunnProcessWriteLeaseRecord::TYPE, target)?; + if let Some(envelope) = current_lease { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA), + "restored write-lease projection schema is foreign" + ); + let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + admitted_lease == Some(¤t), + "refusing to remove an unknown process write-lease projection" + ); + } + if current_expected.is_some_and(|envelope| { + IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload) + .is_ok_and(|current| current == *admitted_expected) + }) && current_anchor.is_some_and(|envelope| { + service_trust_anchor_from_envelope(envelope) + .is_ok_and(|current| current == admitted_anchor) + }) && current_activation.is_none() + && current_lease.is_none() + { + return admitted_expected.canonical_sha256(); + } + let mut replacement = entries + .iter() + .filter(|envelope| { + !((envelope.key == target + && matches!( + envelope.r#type.as_str(), + IdunnExpectedIncarnationRecord::TYPE + | IdunnRuntimeActivationRecord::TYPE + | IdunnProcessWriteLeaseRecord::TYPE + )) + || (envelope.key == anchor_key + && envelope.r#type == GameCultServiceTrustAnchorRecord::TYPE)) + }) + .cloned() + .collect::>(); + replacement.extend([ + CultCacheEnvelope { + key: admitted_expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: admitted_expected.canonical_bytes()?, + stored_at: chrono::Utc::now().to_rfc3339(), + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + CultCacheEnvelope { + key: admitted_anchor.trust_anchor_id.clone(), + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + payload: rmp_serde::to_vec(&admitted_anchor)?, + stored_at: rfc3339_millis(admitted_anchor.bound_at_unix_millis)?, + schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), + }, + ]); + if store.compare_exchange_snapshot(&entries, &replacement)? { + return admitted_expected.canonical_sha256(); + } + } + bail!("topology projection changed repeatedly during admitted restoration") + } + + pub fn admitted_runtime_projection_is_exact( + &self, + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, + activation: &IdunnRuntimeActivationRecord, + lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result { expected.validate()?; - upsert_record( - &self.projection_store, - CultCacheEnvelope { - key: expected.target.clone(), + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()?, + "admitted activation does not bind its Expected projection" + ); + if let Some(lease) = lease { + validate_topology_lease(expected, activation, lease)?; + } + if !self.projection_store.exists() { + return Ok(false); + } + let entries = SingleFileMessagePackBackingStore::new(&self.projection_store) + .pull_all_read_only_snapshot()?; + let anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; + let expected_is_exact = match projection_entry( + &entries, + IdunnExpectedIncarnationRecord::TYPE, + &expected.target, + )? { + Some(envelope) => { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA), + "admitted Expected projection schema is foreign" + ); + ensure!( + IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)? + == *expected, + "admitted Expected projection was replaced by another incarnation" + ); + true + } + None => false, + }; + let anchor_is_exact = match projection_entry( + &entries, + GameCultServiceTrustAnchorRecord::TYPE, + &anchor.trust_anchor_id, + )? { + Some(envelope) => { + ensure!( + service_trust_anchor_from_envelope(envelope)? == anchor, + "admitted runtime presence trust anchor was replaced" + ); + true + } + None => false, + }; + let activation_is_exact = match projection_entry( + &entries, + IdunnRuntimeActivationRecord::TYPE, + &expected.target, + )? { + Some(envelope) => { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA), + "admitted activation projection schema is foreign" + ); + ensure!( + IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)? + == *activation, + "admitted activation projection was replaced" + ); + true + } + None => false, + }; + let lease_is_exact = match projection_entry( + &entries, + IdunnProcessWriteLeaseRecord::TYPE, + &expected.target, + )? { + Some(envelope) => { + ensure!( + envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA), + "admitted write-lease projection schema is foreign" + ); + let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + lease == Some(¤t), + "admitted write-lease projection was replaced" + ); + true + } + None => lease.is_none(), + }; + Ok(expected_is_exact && anchor_is_exact && activation_is_exact && lease_is_exact) + } +} + +fn runtime_presence_trust_anchor( + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, +) -> Result { + expected.validate()?; + ensure!( + provider_anchor.schema_version + == ::TRUST_ANCHOR_SCHEMA, + "runtime presence trust anchor schema is unsupported" + ); + ensure!( + derive_service_identity_id::(&provider_anchor.public_key)? + == provider_anchor.identity_id + && provider_anchor.identity_id == expected.expected_signer_identity_id, + "runtime presence trust anchor does not bind the Expected signer" + ); + let bound_at_unix_millis: u64 = + chrono::DateTime::parse_from_rfc3339(&provider_anchor.identity_created_at)? + .timestamp_millis() + .try_into() + .context("runtime presence trust anchor creation time predates Unix epoch")?; + let anchor = GameCultServiceTrustAnchorRecord { + schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), + trust_anchor_id: runtime_presence_trust_anchor_id(&expected.target), + service_id: expected.target.clone(), + runtime_id: expected.runtime_id.clone(), + signer_identity_id: provider_anchor.identity_id.clone(), + signer_public_key: provider_anchor.public_key.clone(), + signature_algorithm: "ed25519".into(), + signing_purpose: GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE.into(), + signed_schema: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), + binding_authority: "root".into(), + bound_at_unix_millis, + expires_at_unix_millis: None, + private_state_exposed: false, + }; + anchor.validate()?; + Ok(anchor) +} + +fn runtime_presence_trust_anchor_id(target: &str) -> String { + format!("root/{target}/runtime-presence") +} + +fn validate_topology_lease( + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: &IdunnProcessWriteLeaseRecord, +) -> Result<()> { + expected.validate()?; + activation.validate()?; + lease.validate()?; + ensure!( + expected.write_lease_required + && lease.target == expected.target + && lease.expected_projection_sha256 == expected.canonical_sha256()? + && lease.plan_id == expected.plan_id + && lease.incarnation_id == expected.incarnation_id + && lease.sealed_release_id == expected.sealed_release_id + && lease.activation_witness_sha256 == activation.canonical_sha256()? + && lease.state_schema_generation + == expected + .state_schema_generation + .as_deref() + .context("write-lease Expected has no state generation")? + && lease.state_contract_sha256 + == expected + .state_contract_sha256 + .as_deref() + .context("write-lease Expected has no state contract")? + && lease.runtime_id == expected.runtime_id + && lease.runtime_instance_id == activation.runtime_instance_id, + "topology write lease does not bind the exact Expected activation" + ); + Ok(()) +} + +fn service_trust_anchor_from_envelope( + envelope: &CultCacheEnvelope, +) -> Result { + ensure!( + envelope.schema_id.as_deref() == Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA), + "service trust-anchor projection schema is foreign" + ); + let anchor: GameCultServiceTrustAnchorRecord = rmp_serde::from_slice(&envelope.payload)?; + ensure!( + rmp_serde::to_vec(&anchor)? == envelope.payload, + "service trust-anchor projection is noncanonical" + ); + anchor.validate()?; + Ok(anchor) +} + +fn replace_records_atomically(path: &Path, replacements: &[CultCacheEnvelope]) -> Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + ensure!( + replacements.iter().enumerate().all(|(index, entry)| { + replacements[index + 1..].iter().all(|other| { + (entry.r#type.as_str(), entry.key.as_str()) + != (other.r#type.as_str(), other.key.as_str()) + }) + }), + "atomic projection set contains a duplicate identity" + ); + let store = SingleFileMessagePackBackingStore::new(path); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let conditions = replacements + .iter() + .map(|replacement| { + Ok(CultCacheExpectedEnvelope { + r#type: replacement.r#type.clone(), + key: replacement.key.clone(), + current: projection_entry(&entries, &replacement.r#type, &replacement.key)? + .cloned(), + }) + }) + .collect::>>()?; + if conditions + .iter() + .zip(replacements) + .all(|(condition, replacement)| condition.current.as_ref() == Some(replacement)) + { + return Ok(()); + } + if store.compare_exchange(&conditions, replacements)? { + return Ok(()); + } + } + bail!("CultCache projection changed too often to publish atomic set") +} + +fn projection_entry<'a>( + entries: &'a [CultCacheEnvelope], + r#type: &str, + key: &str, +) -> Result> { + let mut matches = entries + .iter() + .filter(|entry| entry.r#type == r#type && entry.key == key); + let current = matches.next(); + ensure!( + matches.next().is_none(), + "CultCache projection identity is ambiguous" + ); + Ok(current) +} + +fn insert_exact_process_lease( + path: &Path, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + replacement: CultCacheEnvelope, +) -> Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let store = SingleFileMessagePackBackingStore::new(path); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let projected_expected = projection_entry( + &entries, + IdunnExpectedIncarnationRecord::TYPE, + &expected.target, + )? + .context("process write lease has no current Expected projection")?; + ensure!( + projected_expected.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA) + && IdunnExpectedIncarnationRecord::decode_canonical(&projected_expected.payload)? + == *expected, + "process write lease current Expected projection is substituted" + ); + let projected_activation = projection_entry( + &entries, + IdunnRuntimeActivationRecord::TYPE, + &expected.target, + )? + .context("process write lease has no observed activation projection")?; + ensure!( + projected_activation.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA) + && IdunnRuntimeActivationRecord::decode_canonical(&projected_activation.payload)? + == *activation, + "process write lease observed activation projection is substituted" + ); + let projected_anchor = service_trust_anchor_from_envelope( + projection_entry( + &entries, + GameCultServiceTrustAnchorRecord::TYPE, + &runtime_presence_trust_anchor_id(&expected.target), + )? + .context("process write lease has no runtime presence trust anchor")?, + )?; + ensure!( + projected_anchor.service_id == expected.target + && projected_anchor.runtime_id == expected.runtime_id + && projected_anchor.signer_identity_id == expected.expected_signer_identity_id + && projected_anchor.signing_purpose + == GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE + && projected_anchor.signed_schema == GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, + "process write lease runtime presence trust anchor is substituted" + ); + let current = projection_entry(&entries, &replacement.r#type, &replacement.key)?; + if current == Some(&replacement) { + return Ok(()); + } + ensure!( + current.is_none(), + "refusing to replace a different current process write-lease projection" + ); + let conditions = [ + CultCacheExpectedEnvelope { r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: expected.canonical_bytes()?, - stored_at: chrono::Utc::now().to_rfc3339(), - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + key: expected.target.clone(), + current: Some(projected_expected.clone()), }, + CultCacheExpectedEnvelope { + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + key: expected.target.clone(), + current: Some(projected_activation.clone()), + }, + CultCacheExpectedEnvelope { + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + key: runtime_presence_trust_anchor_id(&expected.target), + current: projection_entry( + &entries, + GameCultServiceTrustAnchorRecord::TYPE, + &runtime_presence_trust_anchor_id(&expected.target), + )? + .cloned(), + }, + CultCacheExpectedEnvelope { + r#type: replacement.r#type.clone(), + key: replacement.key.clone(), + current: None, + }, + ]; + if store.compare_exchange(&conditions, std::slice::from_ref(&replacement))? { + return Ok(()); + } + } + bail!("CultCache projection changed too often to publish exact record") +} + +impl TopologyPort for CultCacheTopologyDriver { + fn publish_expected( + &self, + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, + ) -> Result { + expected.validate()?; + let anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; + replace_records_atomically( + &self.projection_store, + &[ + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: expected.canonical_bytes()?, + stored_at: chrono::Utc::now().to_rfc3339(), + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + CultCacheEnvelope { + key: anchor.trust_anchor_id.clone(), + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + payload: rmp_serde::to_vec(&anchor)?, + stored_at: rfc3339_millis(anchor.bound_at_unix_millis)?, + schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), + }, + ], )?; expected.canonical_sha256() } - fn withdraw_expected(&self, expected: &IdunnExpectedIncarnationRecord) -> Result<()> { + fn withdraw_expected( + &self, + expected: &IdunnExpectedIncarnationRecord, + provider_anchor: &ServiceIdentityTrustAnchor, + activation: Option<&IdunnRuntimeActivationRecord>, + lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result<()> { expected.validate()?; + let exact_anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; + if let Some(activation) = activation { + activation.validate()?; + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()? + && activation.runtime_id == expected.runtime_id, + "withdrawn activation does not bind the exact Expected projection" + ); + } + if let Some(lease) = lease { + validate_topology_lease( + expected, + activation.context("withdrawn lease has no exact activation")?, + lease, + )?; + } if !self.projection_store.exists() { return Ok(()); } - let expected_sha256 = expected.canonical_sha256()?; let store = SingleFileMessagePackBackingStore::new(&self.projection_store); for _ in 0..8 { let entries = store.pull_all_read_only_snapshot()?; let mut found_expected = false; + let mut found_anchor = false; let mut found_activation = false; + let mut found_lease = false; let mut retained = Vec::with_capacity(entries.len()); for envelope in &entries { + if envelope.r#type == GameCultServiceTrustAnchorRecord::TYPE + && envelope.key == exact_anchor.trust_anchor_id + { + ensure!( + !found_anchor + && service_trust_anchor_from_envelope(envelope)? == exact_anchor, + "refusing to withdraw a substituted runtime presence trust anchor" + ); + found_anchor = true; + continue; + } if envelope.key != expected.target { retained.push(envelope.clone()); continue; @@ -3082,22 +3700,34 @@ impl TopologyPort for CultCacheTopologyDriver { continue; } if envelope.r#type == IdunnRuntimeActivationRecord::TYPE { - let activation = + let current = IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)?; ensure!( !found_activation && envelope.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA) - && activation.expected_projection_sha256 == expected_sha256 - && activation.runtime_id == expected.runtime_id, + && activation == Some(¤t), "refusing to withdraw a substituted runtime activation" ); found_activation = true; continue; } + if envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE { + let current = + IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + !found_lease + && envelope.schema_id.as_deref() + == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) + && lease == Some(¤t), + "refusing to withdraw a substituted process write lease" + ); + found_lease = true; + continue; + } retained.push(envelope.clone()); } - if !found_expected && !found_activation { + if !found_expected && !found_anchor && !found_activation && !found_lease { return Ok(()); } if store.compare_exchange_snapshot(&entries, &retained)? { @@ -3134,6 +3764,73 @@ impl TopologyPort for CultCacheTopologyDriver { activation.canonical_sha256() } + fn publish_process_write_lease( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: &IdunnProcessWriteLeaseRecord, + ) -> Result { + validate_topology_lease(expected, activation, lease)?; + insert_exact_process_lease( + &self.projection_store, + expected, + activation, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), + payload: lease.canonical_bytes()?, + stored_at: rfc3339_millis(lease.issued_at_unix_millis)?, + schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), + }, + )?; + lease.canonical_sha256() + } + + fn withdraw_process_write_lease( + &self, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: Option<&IdunnProcessWriteLeaseRecord>, + ) -> Result<()> { + expected.validate()?; + activation.validate()?; + if let Some(lease) = lease { + validate_topology_lease(expected, activation, lease)?; + } + if !self.projection_store.exists() { + return Ok(()); + } + let store = SingleFileMessagePackBackingStore::new(&self.projection_store); + for _ in 0..8 { + let entries = store.pull_all_read_only_snapshot()?; + let mut found = false; + let mut retained = Vec::with_capacity(entries.len()); + for envelope in &entries { + if envelope.key != expected.target + || envelope.r#type != IdunnProcessWriteLeaseRecord::TYPE + { + retained.push(envelope.clone()); + continue; + } + let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + !found + && envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) + && lease == Some(¤t), + "refusing to withdraw an unexpected process write-lease projection" + ); + found = true; + } + if !found { + return Ok(()); + } + if store.compare_exchange_snapshot(&entries, &retained)? { + return Ok(()); + } + } + bail!("process write-lease projection changed repeatedly while withdrawing it") + } + fn receive(&self, target: &str) -> Result> { require_driver_id(target, "topology target")?; if !self.correlation_store.exists() { @@ -3163,8 +3860,9 @@ impl TopologyPort for CultCacheTopologyDriver { } /// nginx owns proxy mechanics. Idunn supplies one exact backend membership, -/// validates the complete nginx configuration, reloads it, and then observes -/// that nginx's loaded configuration contains those exact bytes. +/// validates the complete nginx configuration, and reloads it. Configuration +/// bytes are actuator state, not proof that the selected runtime answered on +/// the stable route; the control plane admits that proof separately. pub struct NginxRouteDriver { pub binding: RouteBinding, pub nginx_program: PathBuf, @@ -3211,12 +3909,9 @@ impl NginxRouteDriver { Ok(output) } - fn render( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - ) -> Result> { + fn render(&self, expected: &IdunnExpectedIncarnationRecord) -> Result> { expected.validate()?; + let expected_projection_sha256 = expected.canonical_sha256()?; let route = expected .route .as_ref() @@ -3226,13 +3921,14 @@ impl NginxRouteDriver { && route.stable_endpoint == self.binding.stable_endpoint, "route driver binding differs from Expected" ); - let (candidate_host, candidate_port) = endpoint_host_port( - &route.candidate_endpoint, - match self.binding.driver { - RouteDriver::NginxHttp => "http://", - RouteDriver::NginxStreamTcp => "tcp://", - }, - )?; + let endpoint_prefix = match (self.binding.driver, route.transport.as_str()) { + (RouteDriver::NginxStreamTcp, "http") => "http://", + (RouteDriver::NginxStreamTcp, "tcp") => "tcp://", + (RouteDriver::NginxStreamUdp, "rudp") => "rudp://", + _ => bail!("route driver cannot carry the Expected transport"), + }; + let (candidate_host, candidate_port) = + endpoint_host_port(&route.candidate_endpoint, endpoint_prefix)?; ensure!( candidate_host == self.binding.private_host && (self.binding.private_port_start..=self.binding.private_port_end) @@ -3241,40 +3937,30 @@ impl NginxRouteDriver { ); let upstream = nginx_identifier(&self.binding.route_id)?; let rendered = match self.binding.driver { - RouteDriver::NginxHttp => format!( - "# Idunn runtime {runtime_instance_id}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n keepalive 16;\n}}\n" - ), RouteDriver::NginxStreamTcp => { let (stable_host, stable_port) = - endpoint_host_port(&route.stable_endpoint, "tcp://")?; + endpoint_host_port(&route.stable_endpoint, endpoint_prefix)?; format!( - "# Idunn runtime {runtime_instance_id}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n" + "# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n" + ) + } + RouteDriver::NginxStreamUdp => { + let (stable_host, stable_port) = + endpoint_host_port(&route.stable_endpoint, "rudp://")?; + format!( + "# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port} udp reuseport;\n proxy_pass {upstream};\n}}\n" ) } }; Ok(rendered.into_bytes()) } - fn loaded_matches(&self, rendered: &[u8]) -> Result { - let output = self.command(&self.nginx_program, [OsString::from("-T")])?; - let loaded = std::str::from_utf8(&output.stdout) - .context("nginx loaded configuration is not UTF-8")?; - Ok(nginx_config_section(loaded, &self.binding.config_path)? - .is_some_and(|section| section.as_bytes() == rendered)) - } - - fn current_and_loaded(&self) -> Result<(Option>, Option>)> { - let current = match fs::read(&self.binding.config_path) { + fn current_configuration(&self) -> Result>> { + Ok(match fs::read(&self.binding.config_path) { Ok(bytes) => Some(bytes), Err(error) if error.kind() == ErrorKind::NotFound => None, Err(error) => return Err(error).context("reading route fragment"), - }; - let output = self.command(&self.nginx_program, [OsString::from("-T")])?; - let dump = std::str::from_utf8(&output.stdout) - .context("nginx loaded configuration is not UTF-8")?; - let loaded = nginx_config_section(dump, &self.binding.config_path)? - .map(|section| section.as_bytes().to_vec()); - Ok((current, loaded)) + }) } fn write_fragment(&self, content: Option<&[u8]>) -> Result<()> { @@ -3344,12 +4030,12 @@ impl NginxRouteDriver { self.reload() } - fn fail_after_rollback( + fn fail_after_rollback( &self, prior: Option<&[u8]>, failure: anyhow::Error, context: &str, - ) -> Result<()> { + ) -> Result { match self.restore(prior) { Ok(()) => Err(failure).context(context.to_owned()), Err(rollback) => Err(failure).context(format!( @@ -3357,22 +4043,16 @@ impl NginxRouteDriver { )), } } -} -impl RoutePort for NginxRouteDriver { - fn preflight( + pub fn preflight( &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, incumbent: Option<&RouteObservation>, ) -> Result { - let rendered = self.render(expected, runtime_instance_id)?; + let rendered = self.render(expected)?; ensure!(!rendered.is_empty(), "candidate route rendered empty"); - let (current, loaded) = self.current_and_loaded()?; - ensure!( - current == loaded, - "nginx route baseline differs from its loaded configuration" - ); + let current = self.current_configuration()?; let incumbent_membership_sha256 = current.as_ref().map(|bytes| sha256_id(bytes)); match incumbent { Some(incumbent) => ensure!( @@ -3387,86 +4067,379 @@ impl RoutePort for NginxRouteDriver { ), } self.validate_candidate_in_private_mount(&rendered)?; - let after = self.current_and_loaded()?; + let after = self.current_configuration()?; ensure!( - after.0 == current && after.1 == current, + after == current, "nginx route baseline changed during candidate validation" ); - Ok(RoutePreflightReceipt { + let receipt = RoutePreflightReceipt { route_id: self.binding.route_id.clone(), candidate_runtime_instance_id: runtime_instance_id.to_owned(), candidate_membership_sha256: sha256_id(&rendered), incumbent_runtime_instance_id: incumbent .map(|observation| observation.runtime_instance_id.clone()), incumbent_membership_sha256, - }) + incumbent_configuration: current, + }; + receipt.validate()?; + Ok(receipt) } - fn promote( + /// Install the candidate route membership and require nginx to accept the + /// complete configuration. Success is not route admission: callers must + /// still obtain a fresh signed runtime observation through the stable + /// listener. + pub fn install( &self, expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, preflight: &RoutePreflightReceipt, - ) -> Result { - let rendered = self.render(expected, runtime_instance_id)?; + rollback_allowed: bool, + ) -> Result { + preflight.validate()?; + let rendered = self.render(expected)?; ensure!( preflight.route_id == self.binding.route_id && preflight.candidate_runtime_instance_id == runtime_instance_id && preflight.candidate_membership_sha256 == sha256_id(&rendered), "route preflight does not authorize this candidate membership" ); - let (prior, loaded) = self.current_and_loaded()?; + let prior = self.current_configuration()?; let prior_sha256 = prior.as_ref().map(|bytes| sha256_id(bytes)); + let candidate_already_written = prior.as_deref() == Some(rendered.as_slice()); ensure!( - prior == loaded && prior_sha256 == preflight.incumbent_membership_sha256, + candidate_already_written + || (prior == preflight.incumbent_configuration + && prior_sha256 == preflight.incumbent_membership_sha256), "route baseline changed after preflight" ); - atomic_replace(&self.binding.config_path, &rendered)?; + if !candidate_already_written { + atomic_replace(&self.binding.config_path, &rendered)?; + } if let Err(error) = self.reload() { - self.fail_after_rollback( - prior.as_deref(), - error, - "candidate route validation or reload failed", - )?; + if rollback_allowed { + return self.fail_after_rollback( + preflight.incumbent_configuration.as_deref(), + error, + "candidate route validation or reload failed", + ); + } + return Err(error).context( + "candidate route reload failed after incumbent route authority was fenced; candidate fragment retained for retry", + ); } - match self.loaded_matches(&rendered) { - Ok(true) => {} - Ok(false) => self.fail_after_rollback( - prior.as_deref(), - anyhow::anyhow!("nginx loaded configuration omitted the admitted membership"), - "candidate route observation failed", - )?, - Err(error) => self.fail_after_rollback( - prior.as_deref(), - error, - "candidate route observation failed", - )?, - } - Ok(RouteObservation { - route_id: self.binding.route_id.clone(), - runtime_instance_id: runtime_instance_id.to_owned(), - membership_sha256: sha256_id(&rendered), - }) + ensure!( + self.current_configuration()?.as_deref() == Some(rendered.as_slice()), + "candidate route fragment changed during reload" + ); + Ok(sha256_id(&rendered)) } - fn observe( + pub fn observe_membership( &self, expected: &IdunnExpectedIncarnationRecord, - observation: &RouteObservation, + membership_sha256: &str, ) -> Result { - let rendered = self.render(expected, &observation.runtime_instance_id)?; + let rendered = self.render(expected)?; ensure!( - observation.route_id == self.binding.route_id - && observation.membership_sha256 == sha256_id(&rendered), + membership_sha256 == sha256_id(&rendered), "route observation does not describe the expected membership" ); - let current = match fs::read(&self.binding.config_path) { - Ok(bytes) => bytes, - Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false), - Err(error) => return Err(error).context("reading route observation"), - }; - Ok(current == rendered && self.loaded_matches(&rendered)?) + Ok(self.current_configuration()?.as_deref() == Some(rendered.as_slice())) } + + /// Restore the exact membership owned by an admitted generation. Any + /// different fragment is drift, not a rollback baseline, so it is never + /// restored after a failed reload. A failed reload removes the newly + /// written fragment so the next continuity pass cannot mistake disk bytes + /// for an adopted route. The caller must still challenge the stable + /// listener to prove that nginx workers adopted this membership. + pub fn restore_admitted_membership( + &self, + expected: &IdunnExpectedIncarnationRecord, + membership_sha256: &str, + ) -> Result<()> { + let rendered = self.render(expected)?; + ensure!( + membership_sha256 == sha256_id(&rendered), + "admitted route receipt does not describe the expected membership" + ); + self.validate_candidate_in_private_mount(&rendered)?; + if self.current_configuration()?.as_deref() != Some(rendered.as_slice()) { + atomic_replace(&self.binding.config_path, &rendered)?; + } + if let Err(reload) = self.reload() { + return match self.write_fragment(None) { + Ok(()) => Err(reload).context("reloading the exact admitted route membership"), + Err(cleanup) => Err(reload).context(format!( + "reloading the exact admitted route membership; removing its unproved fragment also failed: {cleanup:#}" + )), + }; + } + ensure!( + self.current_configuration()?.as_deref() == Some(rendered.as_slice()), + "admitted route fragment changed during restoration" + ); + Ok(()) + } + + /// Ask the stable listener for one freshly minted provider-owned presence + /// statement. This method validates the CultNet wire exchange and exact + /// document selection only; Idunn's control plane authenticates and + /// correlates the signed payload against current authority. + pub fn request_runtime_presence( + &self, + expected: &IdunnExpectedIncarnationRecord, + message_id: &str, + ) -> Result { + let route = expected + .route + .as_ref() + .context("route challenge has no Expected route")?; + ensure!( + route.route_id == self.binding.route_id + && route.stable_endpoint == self.binding.stable_endpoint, + "route challenge binding differs from Expected" + ); + self.request_runtime_presence_at(expected, message_id, &route.stable_endpoint) + } + + /// The bootstrap exception for the first managed Odin observes Warming on + /// the exact candidate endpoint. It does not install or admit a route. + pub fn request_candidate_runtime_presence( + &self, + expected: &IdunnExpectedIncarnationRecord, + message_id: &str, + ) -> Result { + let route = expected + .route + .as_ref() + .context("candidate challenge has no Expected route")?; + ensure!( + route.route_id == self.binding.route_id, + "candidate challenge binding differs from Expected" + ); + self.render(expected)?; + self.request_runtime_presence_at(expected, message_id, &route.candidate_endpoint) + } + + fn request_runtime_presence_at( + &self, + expected: &IdunnExpectedIncarnationRecord, + message_id: &str, + endpoint: &str, + ) -> Result { + expected.validate()?; + require_driver_id(message_id, "route challenge message")?; + let route = expected + .route + .as_ref() + .context("route challenge has no Expected route")?; + let request = CultNetMessage::SnapshotRequest { + message_id: message_id.to_owned(), + schema_ids: Some(vec![GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()]), + record_keys: Some(vec![expected.target.clone()]), + }; + let endpoint_prefix = match route.transport.as_str() { + "http" => "http://", + "tcp" => "tcp://", + "rudp" => "rudp://", + _ => bail!("route challenge transport is unsupported"), + }; + let (host, port) = endpoint_host_port(endpoint, endpoint_prefix)?; + let target = SocketAddr::new( + host.parse() + .context("route challenge endpoint host is not an IP address")?, + port, + ); + let response = match route.transport.as_str() { + "http" => { + let payload = + encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; + let response = request_http_snapshot(target, &payload)?; + decode_cultnet_message_from_slice(&response, CultNetWireContract::CultNetSchemaV0)? + } + "tcp" => { + let payload = + encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; + let response = request_tcp_snapshot(target, &payload)?; + decode_cultnet_message_from_slice(&response, CultNetWireContract::CultNetSchemaV0)? + } + "rudp" => request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { + target, + runtime_id: "idunn-route-observer".into(), + message_id: message_id.to_owned(), + schema_ids: Some(vec![GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()]), + record_keys: Some(vec![expected.target.clone()]), + ..CultMeshRudpSnapshotOptions::default() + })?, + _ => bail!("route challenge transport is unsupported"), + }; + exact_route_snapshot_response(response, message_id, &expected.target) + } + + /// Restore the exact preflight baseline. This may only replace the exact + /// candidate authorized by the receipt; an unrelated writer is never + /// overwritten as a side effect of rollback. + pub fn rollback( + &self, + expected: &IdunnExpectedIncarnationRecord, + runtime_instance_id: &str, + preflight: &RoutePreflightReceipt, + ) -> Result<()> { + preflight.validate()?; + let rendered = self.render(expected)?; + ensure!( + preflight.route_id == self.binding.route_id + && preflight.candidate_runtime_instance_id == runtime_instance_id + && preflight.candidate_membership_sha256 == sha256_id(&rendered), + "route preflight does not authorize rollback of this candidate membership" + ); + ensure!( + self.current_configuration()?.as_deref() == Some(rendered.as_slice()), + "candidate route changed before rollback" + ); + self.restore(preflight.incumbent_configuration.as_deref()) + } +} + +fn request_tcp_snapshot(target: SocketAddr, payload: &[u8]) -> Result> { + let mut stream = TcpStream::connect_timeout(&target, ROUTE_SNAPSHOT_TIMEOUT) + .with_context(|| format!("connecting stable CultNet TCP route {target}"))?; + stream.set_read_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; + stream.set_write_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; + stream.write_all(&encode_frame(payload)?)?; + stream.flush()?; + + let mut header = [0_u8; 4]; + stream + .read_exact(&mut header) + .context("reading stable CultNet TCP response frame")?; + let length = u32::from_be_bytes(header) as usize; + ensure!( + (1..=ROUTE_SNAPSHOT_MAX_BYTES).contains(&length), + "stable CultNet TCP response exceeds the route observation bound" + ); + let mut response = vec![0_u8; length]; + stream + .read_exact(&mut response) + .context("reading stable CultNet TCP response payload")?; + Ok(response) +} + +fn request_http_snapshot(target: SocketAddr, payload: &[u8]) -> Result> { + let mut stream = TcpStream::connect_timeout(&target, ROUTE_SNAPSHOT_TIMEOUT) + .with_context(|| format!("connecting stable CultNet HTTP route {target}"))?; + stream.set_read_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; + stream.set_write_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; + let head = format!( + "POST {ROUTE_HTTP_SNAPSHOT_PATH} HTTP/1.1\r\nHost: {target}\r\nContent-Type: application/msgpack\r\nAccept: application/msgpack\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + payload.len() + ); + stream.write_all(head.as_bytes())?; + stream.write_all(payload)?; + stream.flush()?; + + let maximum = ROUTE_HTTP_MAX_HEADER_BYTES + .checked_add(ROUTE_SNAPSHOT_MAX_BYTES) + .context("route HTTP response bound overflow")?; + let mut response = Vec::new(); + (&mut stream) + .take((maximum + 1) as u64) + .read_to_end(&mut response) + .context("reading stable CultNet HTTP response")?; + ensure!( + response.len() <= maximum, + "stable CultNet HTTP response exceeds the route observation bound" + ); + let header_end = response + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map(|index| index + 4) + .context("stable CultNet HTTP response has no header boundary")?; + ensure!( + header_end <= ROUTE_HTTP_MAX_HEADER_BYTES, + "stable CultNet HTTP response headers exceed their bound" + ); + let headers = std::str::from_utf8(&response[..header_end]) + .context("stable CultNet HTTP response headers are not UTF-8")?; + let mut lines = headers.split("\r\n"); + let status = lines + .next() + .context("stable CultNet HTTP response has no status")?; + ensure!( + matches!(status, "HTTP/1.1 200 OK" | "HTTP/1.0 200 OK"), + "stable CultNet HTTP route rejected the snapshot challenge: {status}" + ); + let mut content_length = None; + for line in lines.filter(|line| !line.is_empty()) { + let (name, value) = line + .split_once(':') + .context("stable CultNet HTTP response contains a malformed header")?; + if name.eq_ignore_ascii_case("transfer-encoding") { + bail!("stable CultNet HTTP response uses unsupported transfer encoding"); + } + if name.eq_ignore_ascii_case("content-length") { + ensure!( + content_length.is_none(), + "stable CultNet HTTP response repeats Content-Length" + ); + content_length = Some( + value + .trim() + .parse::() + .context("stable CultNet HTTP Content-Length is invalid")?, + ); + } + } + let content_length = + content_length.context("stable CultNet HTTP response has no Content-Length")?; + ensure!( + content_length <= ROUTE_SNAPSHOT_MAX_BYTES + && response.len().saturating_sub(header_end) == content_length, + "stable CultNet HTTP response body differs from its bounded Content-Length" + ); + Ok(response[header_end..].to_vec()) +} + +fn exact_route_snapshot_response( + response: CultNetMessage, + message_id: &str, + target: &str, +) -> Result { + let CultNetMessage::SnapshotResponseRaw { + message_id: response_id, + documents, + } = response + else { + bail!("stable route returned a non-raw snapshot response") + }; + ensure!( + response_id == message_id, + "stable route snapshot response belongs to another challenge" + ); + ensure!( + documents.len() == 1, + "stable route snapshot response is not the exact singleton presence document" + ); + let document = documents + .into_iter() + .next() + .expect("singleton response length was checked"); + ensure!( + document.schema_id == GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA + && document.record_key == target + && document.payload_encoding == CultNetRawPayloadEncoding::Messagepack, + "stable route substituted the runtime presence document" + ); + ensure!( + !document.payload.is_empty() && document.payload.len() <= ROUTE_SNAPSHOT_MAX_BYTES, + "stable route presence payload is empty or exceeds its bound" + ); + Ok(RouteSnapshotResponse { + message_id: response_id, + canonical_presence: document.payload, + }) } fn release_artifact<'a>( @@ -4654,24 +5627,6 @@ fn nginx_identifier(route_id: &str) -> Result { Ok(identifier) } -fn nginx_config_section<'a>(dump: &'a str, config_path: &Path) -> Result> { - let marker = format!("# configuration file {}:\n", config_path.display()); - let matches = dump.match_indices(&marker).collect::>(); - ensure!( - matches.len() <= 1, - "nginx loaded configuration contains duplicate sections for {}", - config_path.display() - ); - let Some((start, _)) = matches.first().copied() else { - return Ok(None); - }; - let content = &dump[start + marker.len()..]; - let end = content - .find("\n# configuration file ") - .map_or(content.len(), |index| index + 1); - Ok(Some(&content[..end])) -} - fn atomic_replace(path: &Path, bytes: &[u8]) -> Result<()> { #[cfg(unix)] use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; @@ -5539,6 +6494,7 @@ mod tests { IdunnExpectedIncarnationRecord, IdunnRuntimeActivationRecord, SequenceAdmittedWarming, + ServiceIdentityTrustAnchor, )> { let provider = enroll_service_identity_at::( &root.join("provider.cc"), @@ -5594,7 +6550,7 @@ mod tests { }, )?; let warming = SequenceAdmittedWarming::for_test("test-transaction", warming, 120)?; - Ok((expected, activation, warming)) + Ok((expected, activation, warming, provider.trust_anchor()?)) } fn lease( @@ -5614,52 +6570,81 @@ mod tests { state_contract_sha256: expected.state_contract_sha256.clone().unwrap(), runtime_id: expected.runtime_id.clone(), runtime_instance_id: activation.runtime_instance_id.clone(), - warming_presence_sha256: warming - .authenticated() - .record() - .signed_presence_sha256 - .clone() - .unwrap(), + warming_presence_sha256: warming.signed_presence_sha256().to_owned(), lease_epoch: 1, issued_at_unix_millis: 200, } } + fn topology_driver(root: &Path, name: &str) -> CultCacheTopologyDriver { + CultCacheTopologyDriver { + projection_store: root.join(format!("{name}.cc")), + correlation_store: root.join(format!("{name}-correlation.cc")), + } + } + + fn project_activation( + driver: &CultCacheTopologyDriver, + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + ) -> Result<()> { + ensure!( + activation.expected_projection_sha256 == expected.canonical_sha256()?, + "test activation belongs to another Expected" + ); + upsert_record( + &driver.projection_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: activation.canonical_bytes()?, + stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + ) + } + #[test] - fn nginx_loaded_observation_is_bound_to_the_exact_file_section() -> Result<()> { - let path = Path::new("/etc/nginx/idunn-routes/service.conf"); - let rendered = "upstream idunn_service {\n server 127.0.0.1:4104;\n}\n"; - let dump = format!( - "# configuration file /etc/nginx/nginx.conf:\nevents {{}}\n# configuration file {}:\n{rendered}# configuration file /etc/nginx/other.conf:\n{rendered}", - path.display() - ); - assert_eq!(nginx_config_section(&dump, path)?, Some(rendered)); - assert_eq!( - nginx_config_section(&dump, Path::new("/etc/nginx/missing.conf"))?, - None - ); - let duplicate = format!("{dump}# configuration file {}:\n{rendered}", path.display()); - assert!(nginx_config_section(&duplicate, path).is_err()); + fn nginx_rudp_route_is_rendered_as_a_udp_stream_proxy() -> Result<()> { + let driver = NginxRouteDriver::new(RouteBinding { + driver: RouteDriver::NginxStreamUdp, + route_id: "odin-rudp".into(), + stable_endpoint: "rudp://127.0.0.1:17872".into(), + private_host: "127.0.0.1".into(), + private_port_start: 27872, + private_port_end: 27879, + config_path: "/etc/nginx/idunn-stream-routes/odin-rudp.conf".into(), + reload_unit: "nginx.service".into(), + }); + let mut candidate = expected(); + candidate.route = Some(cultnet_rs::IdunnExpectedRoute { + route_id: "odin-rudp".into(), + transport: "rudp".into(), + stable_endpoint: "rudp://127.0.0.1:17872".into(), + candidate_endpoint: "rudp://127.0.0.1:27872".into(), + }); + let rendered = String::from_utf8(driver.render(&candidate)?)?; + assert!(rendered.contains("listen 127.0.0.1:17872 udp reuseport;")); + assert!(rendered.contains("server 127.0.0.1:27872;")); Ok(()) } #[cfg(unix)] #[test] - fn nginx_preflight_validates_candidate_and_binds_the_loaded_incumbent() -> Result<()> { + fn nginx_preflight_binds_exact_incumbent_configuration_without_claiming_live_route() + -> Result<()> { use std::os::unix::fs::PermissionsExt; let temp = tempfile::tempdir()?; let config = temp.path().join("route.conf"); - let loaded = temp.path().join("loaded.conf"); let nginx = temp.path().join("nginx"); let systemd_run = temp.path().join("systemd-run"); let systemctl = temp.path().join("systemctl"); fs::write( &nginx, format!( - "#!/bin/sh\nconfig='{}'\nloaded='{}'\nif [ \"$1\" = \"-T\" ]; then\n if [ -f \"$loaded\" ]; then\n printf '# configuration file %s:\\n' \"$config\"\n /bin/cat \"$loaded\"\n fi\n exit 0\nfi\nif [ \"$1\" = \"-t\" ]; then\n candidate=${{IDUNN_TEST_SHADOW:-$config}}\n /bin/grep -q 'server 127.0.0.1:4104' \"$candidate\"\n exit $?\nfi\nexit 64\n", + "#!/bin/sh\nconfig='{}'\nif [ \"$1\" = \"-t\" ]; then\n candidate=${{IDUNN_TEST_SHADOW:-$config}}\n /bin/grep -q 'server 127.0.0.1:4104' \"$candidate\"\n exit $?\nfi\nexit 64\n", config.display(), - loaded.display(), ), )?; fs::write( @@ -5670,18 +6655,14 @@ mod tests { )?; fs::write( &systemctl, - format!( - "#!/bin/sh\nif [ \"$1\" = \"reload\" ]; then\n /bin/cp '{}' '{}'\n exit 0\nfi\nexit 64\n", - config.display(), - loaded.display(), - ), + "#!/bin/sh\nif [ -e \"$0.fail\" ]; then exit 65; fi\nif [ \"$1\" = \"reload\" ]; then echo reload >> \"$0.calls\"; exit 0; fi\nexit 64\n", )?; fs::set_permissions(&nginx, fs::Permissions::from_mode(0o755))?; fs::set_permissions(&systemd_run, fs::Permissions::from_mode(0o755))?; fs::set_permissions(&systemctl, fs::Permissions::from_mode(0o755))?; let binding = RouteBinding { - driver: RouteDriver::NginxHttp, + driver: RouteDriver::NginxStreamTcp, route_id: "service-route".into(), stable_endpoint: "http://127.0.0.1:4103".into(), private_host: "127.0.0.1".into(), @@ -5694,7 +6675,7 @@ mod tests { binding, nginx_program: nginx, systemd_run_program: systemd_run, - systemctl_program: systemctl, + systemctl_program: systemctl.clone(), preflight_root: temp.path().join("preflight"), }; let mut candidate = expected(); @@ -5706,23 +6687,58 @@ mod tests { }); let preflight = driver.preflight(&candidate, &digest('a'), None)?; assert!(!config.exists()); - assert!(!loaded.exists()); assert_eq!(fs::read_dir(&driver.preflight_root)?.count(), 0); - let admitted = driver.promote(&candidate, &digest('a'), &preflight)?; - assert!(driver.observe(&candidate, &admitted)?); + let candidate_bytes = driver.render(&candidate)?; + fs::write(&config, &candidate_bytes)?; + let membership_sha256 = driver.install(&candidate, &digest('a'), &preflight, true)?; + assert_eq!( + fs::read_to_string(systemctl.with_extension("calls"))?, + "reload\n" + ); + assert!(driver.observe_membership(&candidate, &membership_sha256)?); let admitted_bytes = fs::read(&config)?; - assert_eq!(fs::read(&loaded)?, admitted_bytes); + assert!(std::str::from_utf8(&admitted_bytes)?.contains("listen 127.0.0.1:4103;")); + let admitted = RouteObservation { + route_id: "service-route".into(), + runtime_instance_id: digest('a'), + membership_sha256, + signed_presence_sha256: digest('c'), + observed_at_unix_millis: 1, + }; let next_preflight = driver.preflight(&candidate, &digest('b'), Some(&admitted))?; assert_eq!(fs::read(&config)?, admitted_bytes); + fs::write(systemctl.with_extension("fail"), b"fail\n")?; + assert!( + driver + .install(&candidate, &digest('b'), &next_preflight, false) + .is_err() + ); + assert_eq!(fs::read(&config)?, admitted_bytes); + fs::remove_file(systemctl.with_extension("fail"))?; + driver.install(&candidate, &digest('b'), &next_preflight, false)?; fs::write(&config, b"foreign route\n")?; assert!( driver - .promote(&candidate, &digest('b'), &next_preflight) + .install(&candidate, &digest('b'), &next_preflight, true) .is_err() ); - assert_eq!(fs::read(&loaded)?, admitted_bytes); + driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; + assert_eq!(fs::read(&config)?, admitted_bytes); + assert!(driver.observe_membership(&candidate, &admitted.membership_sha256)?); + + fs::write(&config, b"foreign route after admission\n")?; + fs::write(systemctl.with_extension("fail"), b"fail\n")?; + assert!( + driver + .restore_admitted_membership(&candidate, &admitted.membership_sha256) + .is_err() + ); + assert!(!config.exists()); + fs::remove_file(systemctl.with_extension("fail"))?; + driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; + assert_eq!(fs::read(&config)?, admitted_bytes); Ok(()) } @@ -5773,65 +6789,70 @@ mod tests { #[test] fn expected_withdrawal_is_exact_atomic_and_idempotent() -> Result<()> { let temp = tempfile::tempdir()?; - let (expected, activation, _) = authenticated_warming(temp.path())?; - let projection_store = temp.path().join("projection.cc"); - let driver = CultCacheTopologyDriver { - projection_store: projection_store.clone(), - correlation_store: temp.path().join("correlation.cc"), - }; - driver.publish_expected(&expected)?; - upsert_record( - &projection_store, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: activation.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - )?; - upsert_record( - &projection_store, - CultCacheEnvelope { - key: "unrelated".into(), - r#type: "test.unrelated".into(), - payload: vec![0x90], - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some("test.unrelated.v1".into()), - }, - )?; - - driver.withdraw_expected(&expected)?; - let remaining = SingleFileMessagePackBackingStore::new(&projection_store) + let (expected, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; + let lease = lease(&expected, &activation, &warming); + let driver = topology_driver(temp.path(), "projection"); + driver.publish_expected(&expected, &provider_anchor)?; + let published = SingleFileMessagePackBackingStore::new(&driver.projection_store) .pull_all_read_only_snapshot()?; - assert_eq!(remaining.len(), 1); - assert_eq!(remaining[0].key, "unrelated"); - driver.withdraw_expected(&expected)?; + assert_eq!(published.len(), 2); + assert!(published.iter().any(|entry| { + entry.key == expected.target && entry.r#type == IdunnExpectedIncarnationRecord::TYPE + })); + let published_anchor = published + .iter() + .find(|entry| entry.r#type == GameCultServiceTrustAnchorRecord::TYPE) + .context("atomic Expected set has no provider trust anchor")?; + assert_eq!( + service_trust_anchor_from_envelope(published_anchor)?, + runtime_presence_trust_anchor(&expected, &provider_anchor)? + ); + project_activation(&driver, &expected, &activation)?; + driver.publish_process_write_lease(&expected, &activation, &lease)?; - let substituted_store = temp.path().join("substituted-expected.cc"); - let substituted_driver = CultCacheTopologyDriver { - projection_store: substituted_store.clone(), - correlation_store: temp.path().join("substituted-correlation.cc"), - }; - substituted_driver.publish_expected(&expected)?; let mut substituted_expected = expected.clone(); substituted_expected.incarnation_id = "another-incarnation".into(); + upsert_record( + &driver.projection_store, + CultCacheEnvelope { + key: expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: substituted_expected.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + )?; assert!( - substituted_driver - .withdraw_expected(&substituted_expected) + driver + .withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease)) .is_err() ); + driver.publish_expected(&expected, &provider_anchor)?; - let substituted_activation_store = temp.path().join("substituted-activation.cc"); - let substituted_activation_driver = CultCacheTopologyDriver { - projection_store: substituted_activation_store.clone(), - correlation_store: temp.path().join("substituted-activation-correlation.cc"), - }; - substituted_activation_driver.publish_expected(&expected)?; - let mut substituted_activation = activation; + let mut substituted_anchor = runtime_presence_trust_anchor(&expected, &provider_anchor)?; + substituted_anchor.runtime_id = "another-runtime".into(); + substituted_anchor.validate()?; + upsert_record( + &driver.projection_store, + CultCacheEnvelope { + key: substituted_anchor.trust_anchor_id.clone(), + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + payload: rmp_serde::to_vec(&substituted_anchor)?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), + }, + )?; + assert!( + driver + .withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease)) + .is_err() + ); + driver.publish_expected(&expected, &provider_anchor)?; + + let mut substituted_activation = activation.clone(); substituted_activation.expected_projection_sha256 = digest('a'); upsert_record( - &substituted_activation_store, + &driver.projection_store, CultCacheEnvelope { key: expected.target.clone(), r#type: IdunnRuntimeActivationRecord::TYPE.into(), @@ -5841,10 +6862,199 @@ mod tests { }, )?; assert!( - substituted_activation_driver - .withdraw_expected(&expected) + driver + .withdraw_expected(&expected, &provider_anchor, Some(&activation), None) .is_err() ); + project_activation(&driver, &expected, &activation)?; + + let mut substituted_lease = lease.clone(); + substituted_lease.lease_epoch += 1; + assert!( + driver + .publish_process_write_lease(&expected, &activation, &substituted_lease) + .is_err() + ); + assert!( + driver + .withdraw_expected( + &expected, + &provider_anchor, + Some(&activation), + Some(&substituted_lease), + ) + .is_err() + ); + + upsert_record( + &driver.projection_store, + CultCacheEnvelope { + key: "unrelated".into(), + r#type: "test.unrelated".into(), + payload: vec![0x90], + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some("test.unrelated.v1".into()), + }, + )?; + driver.withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease))?; + let remaining = SingleFileMessagePackBackingStore::new(&driver.projection_store) + .pull_all_read_only_snapshot()?; + assert_eq!(remaining.len(), 1); + assert_eq!(remaining[0].key, "unrelated"); + driver.withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease))?; + Ok(()) + } + + #[test] + fn incumbent_projection_restoration_demotes_to_expected_only_and_rejects_unknown_authority() + -> Result<()> { + let temp = tempfile::tempdir()?; + let (incumbent, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; + let lease = lease(&incumbent, &activation, &warming); + let driver = topology_driver(temp.path(), "restoration"); + driver.publish_expected(&incumbent, &provider_anchor)?; + project_activation(&driver, &incumbent, &activation)?; + driver.publish_process_write_lease(&incumbent, &activation, &lease)?; + + let mut candidate = incumbent.clone(); + candidate.plan_id = digest('a'); + candidate.incarnation_id = "candidate-incarnation".into(); + candidate.sealed_release_id = digest('b'); + candidate.artifact_sha256 = digest('c'); + let mut candidate_activation = activation.clone(); + candidate_activation.expected_projection_sha256 = candidate.canonical_sha256()?; + candidate_activation.runtime_instance_id = digest('d'); + driver.publish_expected(&candidate, &provider_anchor)?; + project_activation(&driver, &candidate, &candidate_activation)?; + + upsert_record( + &driver.projection_store, + CultCacheEnvelope { + key: "unrelated".into(), + r#type: "test.unrelated".into(), + payload: vec![0x90], + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some("test.unrelated.v1".into()), + }, + )?; + + assert_eq!( + driver.restore_admitted_expected_only( + &candidate, + &provider_anchor, + Some(&candidate_activation), + &incumbent, + &provider_anchor, + &activation, + Some(&lease), + )?, + incumbent.canonical_sha256()? + ); + + let restored = SingleFileMessagePackBackingStore::new(&driver.projection_store) + .pull_all_read_only_snapshot()?; + assert_eq!(restored.len(), 3); + assert_eq!( + IdunnExpectedIncarnationRecord::decode_canonical( + &restored + .iter() + .find(|entry| entry.r#type == IdunnExpectedIncarnationRecord::TYPE) + .unwrap() + .payload, + )?, + incumbent + ); + assert_eq!( + service_trust_anchor_from_envelope( + restored + .iter() + .find(|entry| entry.r#type == GameCultServiceTrustAnchorRecord::TYPE) + .unwrap(), + )?, + runtime_presence_trust_anchor(&incumbent, &provider_anchor)? + ); + assert!( + restored + .iter() + .all(|entry| entry.r#type != IdunnRuntimeActivationRecord::TYPE + && entry.r#type != IdunnProcessWriteLeaseRecord::TYPE) + ); + assert!( + restored + .iter() + .any(|entry| entry.key == "unrelated" && entry.r#type == "test.unrelated") + ); + let idempotent_bytes = fs::read(&driver.projection_store)?; + driver.restore_admitted_expected_only( + &candidate, + &provider_anchor, + Some(&candidate_activation), + &incumbent, + &provider_anchor, + &activation, + Some(&lease), + )?; + assert_eq!(fs::read(&driver.projection_store)?, idempotent_bytes); + + let admitted_anchor = runtime_presence_trust_anchor(&incumbent, &provider_anchor)?; + let mut unknown_expected = incumbent.clone(); + unknown_expected.incarnation_id = "unknown-incarnation".into(); + let mut unknown_anchor = admitted_anchor.clone(); + unknown_anchor.runtime_id = "unknown-runtime".into(); + unknown_anchor.validate()?; + let mut unknown_activation = activation.clone(); + unknown_activation.runtime_instance_id = digest('e'); + let mut unknown_lease = lease.clone(); + unknown_lease.lease_epoch += 1; + let substitutions = [ + CultCacheEnvelope { + key: incumbent.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: unknown_expected.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }, + CultCacheEnvelope { + key: admitted_anchor.trust_anchor_id.clone(), + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + payload: rmp_serde::to_vec(&unknown_anchor)?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), + }, + CultCacheEnvelope { + key: incumbent.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: unknown_activation.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }, + CultCacheEnvelope { + key: incumbent.target.clone(), + r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), + payload: unknown_lease.canonical_bytes()?, + stored_at: "2026-09-03T00:00:00Z".into(), + schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), + }, + ]; + for substitution in substitutions { + fs::write(&driver.projection_store, &idempotent_bytes)?; + upsert_record(&driver.projection_store, substitution)?; + let before = fs::read(&driver.projection_store)?; + assert!( + driver + .restore_admitted_expected_only( + &candidate, + &provider_anchor, + Some(&candidate_activation), + &incumbent, + &provider_anchor, + &activation, + Some(&lease), + ) + .is_err() + ); + assert_eq!(fs::read(&driver.projection_store)?, before); + } Ok(()) } @@ -5886,19 +7096,75 @@ mod tests { let temp = tempfile::tempdir()?; let path = temp.path().join("lease.cc"); let driver = CultCacheWriteLeaseDriver::new("service", &path); - let (expected, activation, warming) = authenticated_warming(temp.path())?; + let (expected, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; let lease = lease(&expected, &activation, &warming); + let topology = topology_driver(temp.path(), "lease-projection"); + topology.publish_expected(&expected, &provider_anchor)?; + project_activation(&topology, &expected, &activation)?; + let lease_sha256 = driver.grant(&expected, &activation, &warming, &lease)?; assert_eq!(lease_sha256, lease.canonical_sha256()?); assert!(driver.observe(&expected, &activation, &warming, &lease)?); + assert!( + !SingleFileMessagePackBackingStore::new(&topology.projection_store) + .pull_all_read_only_snapshot()? + .iter() + .any(|entry| entry.r#type == IdunnProcessWriteLeaseRecord::TYPE) + ); + assert_eq!( + topology.publish_process_write_lease(&expected, &activation, &lease)?, + lease_sha256 + ); let mut surprise = lease.clone(); surprise.lease_epoch = 2; assert!(driver.revoke_exact(Some(&surprise)).is_err()); assert!(driver.observe(&expected, &activation, &warming, &lease)?); + SingleFileMessagePackBackingStore::new(&path).with_read_only_shared_snapshot(|_| { + let denial = driver + .revoke_exact(Some(&lease)) + .expect_err("shared lifetime holder must deny without blocking"); + assert!( + denial + .to_string() + .contains("held by a shared-lock consumer") + ); + Ok(()) + })?; + assert!(driver.observe(&expected, &activation, &warming, &lease)?); + driver.revoke_exact(Some(&lease))?; assert!(driver.observe_empty()?); + topology.withdraw_process_write_lease(&expected, &activation, Some(&lease))?; + assert!( + !SingleFileMessagePackBackingStore::new(&topology.projection_store) + .pull_all_read_only_snapshot()? + .iter() + .any(|entry| entry.r#type == IdunnProcessWriteLeaseRecord::TYPE) + ); + topology.withdraw_process_write_lease(&expected, &activation, Some(&lease))?; + driver.revoke_exact(Some(&lease))?; + + SingleFileMessagePackBackingStore::new(&path).with_read_only_shared_snapshot( + |snapshot| { + assert!(snapshot.is_empty()); + let denial = driver + .grant(&expected, &activation, &warming, &lease) + .expect_err("shared lifetime holder must deny grant without blocking"); + assert!( + denial + .to_string() + .contains("held by a shared-lock consumer") + ); + Ok(()) + }, + )?; + assert!(driver.observe_empty()?); + assert_eq!( + driver.grant(&expected, &activation, &warming, &lease)?, + lease.canonical_sha256()? + ); driver.revoke_exact(Some(&lease))?; Ok(()) } diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 33612b8..1487768 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -5,10 +5,12 @@ use cultcache_rs::{ use cultnet_rs::{ GameCultProviderHealthIdentity, IDUNN_DEPLOYMENT_BRAKE_AUTHORITY, IDUNN_DEPLOYMENT_BRAKE_ID, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_SCOPE, IDUNN_DEPLOYMENT_RELEASE_PURPOSE, + IDUNN_LIFECYCLE_BRAKE_AUTHORITY, IDUNN_LIFECYCLE_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCOPE, IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, - IdunnDeploymentBrakeRecord, IdunnDeploymentBrakeReleasePurpose, IdunnServiceIdentity, - ServiceIdentityProfile, ServiceIdentityTrustAnchor, derive_service_identity_id, - enroll_service_identity_at, evaluate_idunn_deployment_brake, + IdunnDeploymentBrakeRecord, IdunnDeploymentBrakeReleasePurpose, IdunnLifecycleBrakeObservation, + IdunnLifecycleBrakeRecord, IdunnServiceIdentity, OdinTopologyIdentity, ServiceIdentityProfile, + ServiceIdentityTrustAnchor, derive_service_identity_id, enroll_service_identity_at, + evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, export_service_identity_trust_anchor, open_service_identity_at, }; use odin_core::{ @@ -49,39 +51,34 @@ pub fn run(args: impl IntoIterator) -> Result<()> { let public_key = provider_health_public_key(&options)?; println!("{public_key}"); } - "enroll-idunn-identity" => { - require_only(&options, &["private-store"])?; - enroll_service_identity_at::(&path(&options, "private-store")?)?; - } + "export-provider-health-public-anchor" => export_identity_anchor::< + GameCultProviderHealthIdentity, + >( + &options, "provider health public anchor" + )?, + "enroll-idunn-identity" => enroll_identity::(&options)?, "export-idunn-public-anchor" => { - require_only(&options, &["private-store", "public-anchor"])?; - let private = path(&options, "private-store")?; - let public = path(&options, "public-anchor")?; - reject_alias(&private, &public)?; - refuse_existing(&public, "public anchor")?; - let signer = open_service_identity_at::(&private)?; - export_service_identity_trust_anchor(&signer, &public)?; + export_identity_anchor::(&options, "Idunn public anchor")? + } + "enroll-odin-topology-identity" => enroll_identity::(&options)?, + "export-odin-topology-public-anchor" => { + export_identity_anchor::(&options, "Odin topology public anchor")? } "enroll-deployment-brake-operator" => { - require_only(&options, &["private-store"])?; - enroll_service_identity_at::(&path( - &options, - "private-store", - )?)?; + enroll_identity::(&options)? } "export-deployment-brake-operator-anchor" => { - require_only(&options, &["private-store", "public-anchor"])?; - let private = path(&options, "private-store")?; - let public = path(&options, "public-anchor")?; - reject_alias(&private, &public)?; - refuse_existing(&public, "deployment brake operator anchor")?; - let signer = - open_service_identity_at::(&private)?; - export_service_identity_trust_anchor(&signer, &public)?; + export_identity_anchor::( + &options, + "deployment brake operator anchor", + )? } "deployment-brake-engage" => deployment_brake_engage(&options)?, "deployment-brake-release" => deployment_brake_release(&options)?, "deployment-brake-status" => deployment_brake_status(&options)?, + "lifecycle-brake-engage" => lifecycle_brake_engage(&options)?, + "lifecycle-brake-release" => lifecycle_brake_release(&options)?, + "lifecycle-brake-status" => lifecycle_brake_status(&options)?, "create-daemon-health-trust-binding" => create_health_binding(&options)?, "add-daemon-health-trust-binding" => add_health_binding(&options)?, "require-daemon-health-release-binding" => require_health_binding_release(&options)?, @@ -97,6 +94,26 @@ pub fn run(args: impl IntoIterator) -> Result<()> { Ok(()) } +fn enroll_identity(options: &BTreeMap) -> Result<()> { + require_only(options, &["private-store"])?; + enroll_service_identity_at::

(&path(options, "private-store")?)?; + Ok(()) +} + +fn export_identity_anchor( + options: &BTreeMap, + label: &str, +) -> Result<()> { + require_only(options, &["private-store", "public-anchor"])?; + let private = path(options, "private-store")?; + let public = path(options, "public-anchor")?; + reject_alias(&private, &public)?; + refuse_existing(&public, label)?; + let signer = open_service_identity_at::

(&private)?; + export_service_identity_trust_anchor(&signer, &public)?; + Ok(()) +} + fn enroll_provider_health_identity(options: &BTreeMap) -> Result { require_only(options, &["private-store"])?; let signer = enroll_service_identity_at::(&path( @@ -262,6 +279,184 @@ fn deployment_brake_status(options: &BTreeMap) -> Result<()> { Ok(()) } +fn lifecycle_brake_engage(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "store", + "runtime-id", + "target", + "reason", + "updated-at-unix-millis", + ], + )?; + let updated = parse_u64(options, "updated-at-unix-millis")?; + let record = IdunnLifecycleBrakeRecord { + schema_version: IDUNN_LIFECYCLE_BRAKE_SCHEMA.into(), + authority: IDUNN_LIFECYCLE_BRAKE_AUTHORITY.into(), + runtime_id: required(options, "runtime-id")?.into(), + target: required(options, "target")?.into(), + scope: IDUNN_LIFECYCLE_BRAKE_SCOPE.into(), + status: "engaged".into(), + reason: required(options, "reason")?.into(), + updated_at_unix_millis: updated, + released_until_unix_millis: None, + }; + replace_lifecycle_brake(&path(options, "store")?, record) +} + +fn lifecycle_brake_release(options: &BTreeMap) -> Result<()> { + allow_only( + options, + &[ + "store", + "runtime-id", + "target", + "reason", + "updated-at-unix-millis", + "released-until-unix-millis", + ], + )?; + let updated = parse_u64(options, "updated-at-unix-millis")?; + let record = IdunnLifecycleBrakeRecord { + schema_version: IDUNN_LIFECYCLE_BRAKE_SCHEMA.into(), + authority: IDUNN_LIFECYCLE_BRAKE_AUTHORITY.into(), + runtime_id: required(options, "runtime-id")?.into(), + target: required(options, "target")?.into(), + scope: IDUNN_LIFECYCLE_BRAKE_SCOPE.into(), + status: "released".into(), + reason: required(options, "reason")?.into(), + updated_at_unix_millis: updated, + released_until_unix_millis: options + .get("released-until-unix-millis") + .map(|value| { + value + .parse() + .context("--released-until-unix-millis must be u64") + }) + .transpose()?, + }; + replace_lifecycle_brake(&path(options, "store")?, record) +} + +fn lifecycle_brake_status(options: &BTreeMap) -> Result<()> { + require_only( + options, + &["store", "runtime-id", "target", "now-unix-millis"], + )?; + let runtime_id = lifecycle_identifier(options, "runtime-id")?; + let target = lifecycle_identifier(options, "target")?; + let now = parse_u64(options, "now-unix-millis")?; + match read_lifecycle_brake(&path(options, "store")?)? { + None => { + evaluate_idunn_continuity_restart( + IdunnLifecycleBrakeObservation::Missing, + runtime_id, + target, + now, + ) + .map_err(|denial| anyhow!("lifecycle brake denied continuity restart: {denial:?}"))?; + println!("allowed runtime={runtime_id} target={target} brake=absent"); + } + Some(record) => { + evaluate_idunn_continuity_restart( + IdunnLifecycleBrakeObservation::Present(&record), + runtime_id, + target, + now, + ) + .map_err(|denial| anyhow!("lifecycle brake denied continuity restart: {denial:?}"))?; + if let Some(expires) = record.released_until_unix_millis { + println!( + "allowed runtime={} target={} brake=released expires={expires}", + record.runtime_id, record.target + ); + } else { + println!( + "allowed runtime={} target={} brake=released expires=never", + record.runtime_id, record.target + ); + } + } + } + Ok(()) +} + +fn replace_lifecycle_brake(path: &Path, record: IdunnLifecycleBrakeRecord) -> Result<()> { + record.validate()?; + let store = SingleFileMessagePackBackingStore::new(path); + let entries = store.pull_all_read_only_snapshot()?; + let current = match entries.as_slice() { + [] => None, + [entry] + if entry.r#type == IdunnLifecycleBrakeRecord::TYPE + && entry.schema_id.as_deref() == Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA) => + { + let current_record = IdunnLifecycleBrakeRecord::decode_canonical(&entry.payload)?; + if entry.key != current_record.target { + bail!("lifecycle brake envelope key does not match its target"); + } + if current_record.runtime_id != record.runtime_id + || current_record.target != record.target + { + bail!("lifecycle brake store is bound to another runtime or target"); + } + if record.updated_at_unix_millis <= current_record.updated_at_unix_millis { + bail!("lifecycle brake update time must advance"); + } + Some(entry.clone()) + } + _ => bail!("lifecycle brake store is corrupt or ambiguous"), + }; + let next = typed_envelope( + &record.target, + &record, + IDUNN_LIFECYCLE_BRAKE_SCHEMA, + record.updated_at_unix_millis, + )?; + if !store.compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: IdunnLifecycleBrakeRecord::TYPE.into(), + key: record.target.clone(), + current, + }], + &[next], + )? { + bail!("lifecycle brake changed during transition"); + } + Ok(()) +} + +fn read_lifecycle_brake(path: &Path) -> Result> { + if !path.exists() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let entry = match entries.as_slice() { + [] => return Ok(None), + [entry] => entry, + _ => bail!("lifecycle brake store is ambiguous"), + }; + if entry.r#type != IdunnLifecycleBrakeRecord::TYPE + || entry.schema_id.as_deref() != Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA) + { + bail!("lifecycle brake store contains a foreign record"); + } + let record = IdunnLifecycleBrakeRecord::decode_canonical(&entry.payload)?; + if entry.key != record.target { + bail!("lifecycle brake envelope key does not match its target"); + } + Ok(Some(record)) +} + +fn lifecycle_identifier<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { + let value = required(options, name)?; + if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { + bail!("--{name} is empty, oversized, or contains control characters"); + } + Ok(value) +} + fn replace_brake(path: &Path, record: IdunnDeploymentBrakeRecord, millis: u64) -> Result<()> { let store = SingleFileMessagePackBackingStore::new(path); let entries = store.pull_all_read_only_snapshot()?; @@ -907,7 +1102,7 @@ fn normalized(path: &Path) -> Result { } fn usage() -> &'static str { - "Usage: idunn-provision enroll-provider-health-identity --private-store \n idunn-provision provider-health-public-key --private-store \n idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision require-daemon-health-release-binding --store --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " + "Usage: idunn-provision enroll-provider-health-identity --private-store \n idunn-provision provider-health-public-key --private-store \n idunn-provision export-provider-health-public-anchor --private-store --public-anchor \n idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-odin-topology-identity --private-store \n idunn-provision export-odin-topology-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision lifecycle-brake-engage --store --runtime-id --target --reason --updated-at-unix-millis \n idunn-provision lifecycle-brake-release --store --runtime-id --target --reason --updated-at-unix-millis [--released-until-unix-millis ]\n idunn-provision lifecycle-brake-status --store --runtime-id --target --now-unix-millis \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision require-daemon-health-release-binding --store --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " } #[cfg(test)] @@ -1476,6 +1671,144 @@ mod tests { Ok(()) } + #[test] + fn provider_and_odin_anchor_exports_are_profile_exact_immutable_and_non_aliasing() -> Result<()> + { + let temp = TempDir::new()?; + let provider_private = temp.path().join("provider-private.cc"); + let provider_anchor = temp.path().join("provider-anchor.cc"); + invoke(&[ + "enroll-provider-health-identity", + "--private-store", + provider_private.to_str().unwrap(), + ])?; + assert!( + invoke(&[ + "export-provider-health-public-anchor", + "--private-store", + provider_private.to_str().unwrap(), + "--public-anchor", + provider_private.to_str().unwrap(), + ]) + .is_err() + ); + invoke(&[ + "export-provider-health-public-anchor", + "--private-store", + provider_private.to_str().unwrap(), + "--public-anchor", + provider_anchor.to_str().unwrap(), + ])?; + let provider_entries = store_snapshot(&provider_anchor)?; + let [provider_envelope] = provider_entries.as_slice() else { + panic!("provider anchor store must contain exactly one envelope"); + }; + assert_eq!( + provider_envelope.r#type, + GameCultProviderHealthIdentity::TRUST_ANCHOR_TYPE + ); + assert_eq!( + provider_envelope.key, + GameCultProviderHealthIdentity::TRUST_ANCHOR_KEY + ); + assert_eq!( + provider_envelope.schema_id.as_deref(), + Some(GameCultProviderHealthIdentity::TRUST_ANCHOR_SCHEMA) + ); + let provider: ServiceIdentityTrustAnchor = + rmp_serde::from_slice(&provider_envelope.payload)?; + assert_eq!(rmp_serde::to_vec(&provider)?, provider_envelope.payload); + assert_eq!( + provider.identity_id, + derive_service_identity_id::(&provider.public_key)? + ); + assert!(open_service_identity_at::(&provider_private).is_err()); + let provider_before = std::fs::read(&provider_anchor)?; + assert!( + invoke(&[ + "export-provider-health-public-anchor", + "--private-store", + provider_private.to_str().unwrap(), + "--public-anchor", + provider_anchor.to_str().unwrap(), + ]) + .is_err() + ); + assert_eq!(std::fs::read(&provider_anchor)?, provider_before); + + let odin_private = temp.path().join("odin-private.cc"); + let odin_anchor = temp.path().join("odin-anchor.cc"); + invoke(&[ + "enroll-odin-topology-identity", + "--private-store", + odin_private.to_str().unwrap(), + ])?; + assert!(open_service_identity_at::(&odin_private).is_err()); + assert!( + invoke(&[ + "export-odin-topology-public-anchor", + "--private-store", + odin_private.to_str().unwrap(), + "--public-anchor", + odin_private.to_str().unwrap(), + ]) + .is_err() + ); + invoke(&[ + "export-odin-topology-public-anchor", + "--private-store", + odin_private.to_str().unwrap(), + "--public-anchor", + odin_anchor.to_str().unwrap(), + ])?; + let odin_entries = store_snapshot(&odin_anchor)?; + let [odin_envelope] = odin_entries.as_slice() else { + panic!("Odin anchor store must contain exactly one envelope"); + }; + assert_eq!( + odin_envelope.r#type, + OdinTopologyIdentity::TRUST_ANCHOR_TYPE + ); + assert_eq!(odin_envelope.key, OdinTopologyIdentity::TRUST_ANCHOR_KEY); + assert_eq!( + odin_envelope.schema_id.as_deref(), + Some(OdinTopologyIdentity::TRUST_ANCHOR_SCHEMA) + ); + let odin: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&odin_envelope.payload)?; + assert_eq!(rmp_serde::to_vec(&odin)?, odin_envelope.payload); + assert_eq!( + odin.identity_id, + derive_service_identity_id::(&odin.public_key)? + ); + assert_ne!(provider.identity_id, odin.identity_id); + let odin_before = std::fs::read(&odin_anchor)?; + assert!( + invoke(&[ + "export-odin-topology-public-anchor", + "--private-store", + odin_private.to_str().unwrap(), + "--public-anchor", + odin_anchor.to_str().unwrap(), + ]) + .is_err() + ); + assert_eq!(std::fs::read(&odin_anchor)?, odin_before); + + let wrong_profile_output = temp.path().join("wrong-profile.cc"); + assert!( + invoke(&[ + "export-provider-health-public-anchor", + "--private-store", + odin_private.to_str().unwrap(), + "--public-anchor", + wrong_profile_output.to_str().unwrap(), + ]) + .is_err() + ); + assert!(!wrong_profile_output.exists()); + Ok(()) + } + #[test] fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { let temp = TempDir::new()?; @@ -1916,6 +2249,159 @@ mod tests { Ok(()) } + #[test] + fn lifecycle_brake_is_absence_allow_and_exact_to_runtime_target_and_expiry() -> Result<()> { + let temp = TempDir::new()?; + let missing = temp.path().join("missing-lifecycle.cc"); + invoke(&[ + "lifecycle-brake-status", + "--store", + missing.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--now-unix-millis", + "100", + ])?; + assert!(!missing.exists()); + + let store = temp.path().join("ghostlight-lifecycle.cc"); + invoke(&[ + "lifecycle-brake-engage", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--reason", + "operator maintenance", + "--updated-at-unix-millis", + "200", + ])?; + let engaged = read_lifecycle_brake(&store)?.context("lifecycle brake must exist")?; + assert_eq!(engaged.status, "engaged"); + assert_eq!(engaged.runtime_id, "idunn-yggdrasil"); + assert_eq!(engaged.target, "ghostlight"); + assert!(read_brake(&store).is_err()); + assert!( + invoke(&[ + "lifecycle-brake-status", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--now-unix-millis", + "201", + ]) + .is_err() + ); + + let engaged_bytes = std::fs::read(&store)?; + assert!( + invoke(&[ + "lifecycle-brake-release", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-nightwing", + "--target", + "ghostlight", + "--reason", + "wrong runtime", + "--updated-at-unix-millis", + "300", + ]) + .is_err() + ); + assert!( + invoke(&[ + "lifecycle-brake-release", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "odin", + "--reason", + "wrong target", + "--updated-at-unix-millis", + "300", + ]) + .is_err() + ); + assert_eq!(std::fs::read(&store)?, engaged_bytes); + + invoke(&[ + "lifecycle-brake-release", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--reason", + "bounded continuity window", + "--updated-at-unix-millis", + "300", + "--released-until-unix-millis", + "500", + ])?; + invoke(&[ + "lifecycle-brake-status", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--now-unix-millis", + "499", + ])?; + for (runtime, target, now) in [ + ("idunn-nightwing", "ghostlight", "499"), + ("idunn-yggdrasil", "odin", "499"), + ("idunn-yggdrasil", "ghostlight", "500"), + ] { + assert!( + invoke(&[ + "lifecycle-brake-status", + "--store", + store.to_str().unwrap(), + "--runtime-id", + runtime, + "--target", + target, + "--now-unix-millis", + now, + ]) + .is_err() + ); + } + let released_bytes = std::fs::read(&store)?; + assert!( + invoke(&[ + "lifecycle-brake-engage", + "--store", + store.to_str().unwrap(), + "--runtime-id", + "idunn-yggdrasil", + "--target", + "ghostlight", + "--reason", + "stale replay", + "--updated-at-unix-millis", + "300", + ]) + .is_err() + ); + assert_eq!(std::fs::read(&store)?, released_bytes); + Ok(()) + } + #[test] fn brake_compare_exchange_rejects_stale_transition_snapshot() -> Result<()> { let temp = TempDir::new()?; diff --git a/crates/odin-daemon/Cargo.toml b/crates/odin-daemon/Cargo.toml new file mode 100644 index 0000000..e50553d --- /dev/null +++ b/crates/odin-daemon/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "odin-daemon" +version = "0.1.0" +edition.workspace = true +license.workspace = true +publish.workspace = true + +[lib] +name = "odin_daemon" +path = "src/lib.rs" + +[dependencies] +anyhow = "1" +chrono = "0.4" +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultmesh-rs = { path = "../../vendor/cultmesh-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +fs2 = "0.4" +rmp-serde = "1.3" +serde = "1" + +[dev-dependencies] +tempfile = "3" diff --git a/crates/odin-daemon/src/lib.rs b/crates/odin-daemon/src/lib.rs new file mode 100644 index 0000000..bfd38e6 --- /dev/null +++ b/crates/odin-daemon/src/lib.rs @@ -0,0 +1,1832 @@ +//! Deterministic Odin authority for runtime-topology correlation. +//! +//! This library owns no listener or lifecycle loop. Callers provide Idunn's +//! read-only projection, a trusted receive time with any service-owned +//! presence, and narrow clock/store/signer implementations. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use anyhow::{Context, Result, bail, ensure}; +use chrono::{DateTime, SecondsFormat, Utc}; +use cultcache_rs::{ + CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, +}; +use cultnet_rs::{ + GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultRuntimePresenceHealthRecord, + GameCultServiceTrustAnchorRecord, IDUNN_EXPECTED_INCARNATION_SCHEMA, + IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedDependency, + IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationPurpose, + IdunnRuntimeActivationRecord, IdunnServiceIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, + OdinRuntimeDependencyEvidence, OdinRuntimeTopologyCorrelationPurpose, + OdinRuntimeTopologyCorrelationRecord, OdinTopologyAuthenticationContext, + OdinTopologyDisagreement, OdinTopologyIdentity, RuntimePresenceAuthenticationContext, + ServiceIdentitySignature, ServiceIdentitySigner, ServiceIdentityTrustAnchor, + authenticate_odin_runtime_topology_correlation, authenticate_runtime_presence_claim, + correlate_runtime_presence_claim, verify_runtime_authority, verify_service_identity_signature, + verify_service_identity_signature_with_public_key, +}; + +const CAS_ATTEMPTS: usize = 8; + +#[derive(Clone, Copy, Debug)] +pub struct AuthenticationPolicy { + pub presence_maximum_age_millis: u64, + pub presence_maximum_future_skew_millis: u64, + pub correlation_maximum_age_millis: u64, + pub correlation_maximum_future_skew_millis: u64, +} + +impl Default for AuthenticationPolicy { + fn default() -> Self { + Self { + presence_maximum_age_millis: 30_000, + presence_maximum_future_skew_millis: 5_000, + correlation_maximum_age_millis: 30_000, + correlation_maximum_future_skew_millis: 5_000, + } + } +} + +pub trait Clock { + fn now_unix_millis(&self) -> Result; +} + +#[derive(Clone, Copy, Debug, Default)] +pub struct SystemClock; + +impl Clock for SystemClock { + fn now_unix_millis(&self) -> Result { + Ok(SystemTime::now() + .duration_since(UNIX_EPOCH) + .context("system clock predates the Unix epoch")? + .as_millis() + .try_into() + .context("system clock does not fit the topology timestamp")?) + } +} + +/// One exact Idunn-owned projection. The service anchor is provider lookup +/// material selected by Expected; it is not runtime observation. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct IdunnRuntimeProjection { + pub expected: IdunnExpectedIncarnationRecord, + pub provider_anchor: Option, + pub activation: Option, + pub current_lease: Option, +} + +impl IdunnRuntimeProjection { + pub fn validate(&self) -> Result<()> { + self.expected.validate()?; + if let Some(anchor) = &self.provider_anchor { + anchor.validate()?; + } + if let Some(activation) = &self.activation { + activation.validate()?; + } + if let Some(lease) = &self.current_lease { + lease.validate()?; + } + Ok(()) + } +} + +pub trait IdunnProjectionSource { + fn current_projection(&self, target: &str) -> Result>; +} + +/// Read-only adapter for the atomic projection file published by Idunn. +pub struct CultCacheIdunnProjectionSource { + path: PathBuf, +} + +impl CultCacheIdunnProjectionSource { + pub fn new(path: impl Into) -> Self { + Self { path: path.into() } + } + + pub fn path(&self) -> &Path { + &self.path + } +} + +impl IdunnProjectionSource for CultCacheIdunnProjectionSource { + fn current_projection(&self, target: &str) -> Result> { + if !self.path.is_file() { + return Ok(None); + } + let entries = + SingleFileMessagePackBackingStore::new(&self.path).pull_all_read_only_snapshot()?; + let expected_envelope = + unique_envelope(&entries, IdunnExpectedIncarnationRecord::TYPE, target)?; + let anchor_key = runtime_presence_anchor_id(target); + let anchor_envelope = unique_envelope( + &entries, + GameCultServiceTrustAnchorRecord::TYPE, + &anchor_key, + )?; + let activation_envelope = + unique_envelope(&entries, IdunnRuntimeActivationRecord::TYPE, target)?; + let lease_envelope = unique_envelope(&entries, IdunnProcessWriteLeaseRecord::TYPE, target)?; + + let Some(expected_envelope) = expected_envelope else { + ensure!( + anchor_envelope.is_none() + && activation_envelope.is_none() + && lease_envelope.is_none(), + "Idunn projection has runtime authority without Expected" + ); + return Ok(None); + }; + ensure_schema(expected_envelope, IDUNN_EXPECTED_INCARNATION_SCHEMA)?; + let expected = + IdunnExpectedIncarnationRecord::decode_canonical(&expected_envelope.payload)?; + ensure!( + expected.target == target, + "Expected projection key is substituted" + ); + + let provider_anchor = anchor_envelope + .map(|envelope| { + ensure_schema(envelope, GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA)?; + decode_canonical(&envelope.payload, "provider trust anchor") + }) + .transpose()?; + + let activation = activation_envelope + .map(|envelope| { + ensure_schema(envelope, IDUNN_RUNTIME_ACTIVATION_SCHEMA)?; + IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload) + }) + .transpose()?; + + let current_lease = lease_envelope + .map(|envelope| { + ensure_schema(envelope, IDUNN_PROCESS_WRITE_LEASE_SCHEMA)?; + IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload) + }) + .transpose()?; + let projection = IdunnRuntimeProjection { + expected, + provider_anchor, + activation, + current_lease, + }; + projection.validate()?; + Ok(Some(projection)) + } +} + +pub trait OdinCorrelationSigner { + fn identity_id(&self) -> &str; + fn public_key(&self) -> &[u8]; + fn sign_correlation(&self, unsigned_payload: &[u8]) -> Result>; +} + +impl OdinCorrelationSigner for ServiceIdentitySigner { + fn identity_id(&self) -> &str { + &self.entry().identity_id + } + + fn public_key(&self) -> &[u8] { + &self.entry().public_key + } + + fn sign_correlation(&self, unsigned_payload: &[u8]) -> Result> { + Ok(self + .sign::(unsigned_payload) + .signature) + } +} + +impl OdinCorrelationSigner for &T { + fn identity_id(&self) -> &str { + (*self).identity_id() + } + + fn public_key(&self) -> &[u8] { + (*self).public_key() + } + + fn sign_correlation(&self, unsigned_payload: &[u8]) -> Result> { + (*self).sign_correlation(unsigned_payload) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AdmittedPresence { + pub canonical_bytes: Vec, + pub trusted_received_at_unix_millis: u64, + pub stored_at: String, +} + +impl AdmittedPresence { + fn new(canonical_bytes: Vec, trusted_received_at_unix_millis: u64) -> Result { + Ok(Self { + canonical_bytes, + trusted_received_at_unix_millis, + stored_at: rfc3339_millis(trusted_received_at_unix_millis)?, + }) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct StoredCorrelation { + pub canonical_bytes: Vec, + pub stored_at: String, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OdinStoreQuery { + pub target: String, + pub provider_signer_identity_id: String, + pub dependency_targets: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OdinStoreSnapshot { + pub query: OdinStoreQuery, + pub presence: Option, + pub correlation: Option, + pub dependency_correlations: BTreeMap>, +} + +pub trait OdinTopologyStore { + fn read(&self, query: OdinStoreQuery) -> Result; + + fn compare_exchange( + &self, + observed: &OdinStoreSnapshot, + replacement_presence: Option<&AdmittedPresence>, + replacement_correlation: &StoredCorrelation, + ) -> Result; + + fn withdraw_correlation(&self, target: &str) -> Result<()>; +} + +/// Odin's durable replay and correlation store. It persists existing CultNet +/// documents directly instead of inventing a parallel admission schema. +pub struct CultCacheOdinTopologyStore { + path: PathBuf, +} + +impl CultCacheOdinTopologyStore { + pub fn new(path: impl Into) -> Self { + Self { path: path.into() } + } + + pub fn path(&self) -> &Path { + &self.path + } +} + +impl OdinTopologyStore for CultCacheOdinTopologyStore { + fn read(&self, mut query: OdinStoreQuery) -> Result { + query.dependency_targets.sort(); + query.dependency_targets.dedup(); + let entries = if self.path.is_file() { + SingleFileMessagePackBackingStore::new(&self.path).pull_all_read_only_snapshot()? + } else { + Vec::new() + }; + let presence_key = presence_store_key(&query.target, &query.provider_signer_identity_id); + let presence = unique_envelope( + &entries, + GameCultRuntimePresenceHealthRecord::TYPE, + &presence_key, + )? + .map(decode_presence_envelope) + .transpose()?; + let correlation = unique_envelope( + &entries, + OdinRuntimeTopologyCorrelationRecord::TYPE, + &query.target, + )? + .map(decode_correlation_envelope) + .transpose()?; + let mut dependency_correlations = BTreeMap::new(); + for dependency_target in &query.dependency_targets { + let current = unique_envelope( + &entries, + OdinRuntimeTopologyCorrelationRecord::TYPE, + dependency_target, + )? + .map(decode_correlation_envelope) + .transpose()?; + dependency_correlations.insert(dependency_target.clone(), current); + } + Ok(OdinStoreSnapshot { + query, + presence, + correlation, + dependency_correlations, + }) + } + + fn compare_exchange( + &self, + observed: &OdinStoreSnapshot, + replacement_presence: Option<&AdmittedPresence>, + replacement_correlation: &StoredCorrelation, + ) -> Result { + let presence_key = presence_store_key( + &observed.query.target, + &observed.query.provider_signer_identity_id, + ); + let mut conditions = BTreeMap::new(); + insert_condition( + &mut conditions, + CultCacheExpectedEnvelope { + key: presence_key.clone(), + r#type: GameCultRuntimePresenceHealthRecord::TYPE.into(), + current: observed + .presence + .as_ref() + .map(|presence| presence_envelope(&presence_key, presence)), + }, + )?; + insert_condition( + &mut conditions, + CultCacheExpectedEnvelope { + key: observed.query.target.clone(), + r#type: OdinRuntimeTopologyCorrelationRecord::TYPE.into(), + current: observed + .correlation + .as_ref() + .map(|correlation| correlation_envelope(&observed.query.target, correlation)), + }, + )?; + for (target, correlation) in &observed.dependency_correlations { + insert_condition( + &mut conditions, + CultCacheExpectedEnvelope { + key: target.clone(), + r#type: OdinRuntimeTopologyCorrelationRecord::TYPE.into(), + current: correlation + .as_ref() + .map(|correlation| correlation_envelope(target, correlation)), + }, + )?; + } + let mut replacements = vec![correlation_envelope( + &observed.query.target, + replacement_correlation, + )]; + if let Some(presence) = replacement_presence { + replacements.push(presence_envelope(&presence_key, presence)); + } + if let Some(parent) = self.path.parent() { + std::fs::create_dir_all(parent)?; + } + SingleFileMessagePackBackingStore::new(&self.path) + .compare_exchange(&conditions.into_values().collect::>(), &replacements) + } + + fn withdraw_correlation(&self, target: &str) -> Result<()> { + for _ in 0..CAS_ATTEMPTS { + if !self.path.is_file() { + return Ok(()); + } + let entries = + SingleFileMessagePackBackingStore::new(&self.path).pull_all_read_only_snapshot()?; + let current = + unique_envelope(&entries, OdinRuntimeTopologyCorrelationRecord::TYPE, target)?; + let Some(current) = current else { + return Ok(()); + }; + let replacement = entries + .iter() + .filter(|entry| *entry != current) + .cloned() + .collect::>(); + if SingleFileMessagePackBackingStore::new(&self.path) + .compare_exchange_snapshot(&entries, &replacement)? + { + return Ok(()); + } + } + bail!("Odin topology store changed repeatedly while withdrawing correlation") + } +} + +pub struct OdinTopologyAuthority { + projections: P, + store: S, + signer: K, + clock: C, + idunn_anchor: ServiceIdentityTrustAnchor, + policy: AuthenticationPolicy, +} + +impl OdinTopologyAuthority +where + P: IdunnProjectionSource, + S: OdinTopologyStore, + K: OdinCorrelationSigner, + C: Clock, +{ + pub fn new( + projections: P, + store: S, + signer: K, + clock: C, + idunn_anchor: ServiceIdentityTrustAnchor, + policy: AuthenticationPolicy, + ) -> Self { + Self { + projections, + store, + signer, + clock, + idunn_anchor, + policy, + } + } + + /// Correlate current Expected with any previously admitted raw observation. + /// With no admitted observation this can only produce Expected, never Present. + pub fn refresh(&self, target: &str) -> Result>> { + let Some(projection) = self.projections.current_projection(target)? else { + self.store.withdraw_correlation(target)?; + return Ok(None); + }; + self.reconcile(projection, None).map(Some) + } + + /// Authenticate and monotonically admit one exact provider-owned presence. + pub fn admit_presence( + &self, + target: &str, + canonical_presence: &[u8], + trusted_received_at_unix_millis: u64, + ) -> Result> { + let projection = self + .projections + .current_projection(target)? + .context("runtime presence has no current Expected projection")?; + self.reconcile( + projection, + Some((canonical_presence, trusted_received_at_unix_millis)), + ) + } + + /// Return the exact signed bytes admitted by Odin for transport. + pub fn current_signed_correlation(&self, target: &str) -> Result>> { + self.refresh(target) + } + + fn reconcile( + &self, + projection: IdunnRuntimeProjection, + incoming: Option<(&[u8], u64)>, + ) -> Result> { + projection.validate()?; + let now = self.clock.now_unix_millis()?; + let (authority, projection_disagreements) = + classify_runtime_authority(&projection, &self.idunn_anchor, now)?; + let (lease_sha256, lease_disagreement) = classify_current_lease(&projection)?; + let query = OdinStoreQuery { + target: projection.expected.target.clone(), + provider_signer_identity_id: projection.expected.expected_signer_identity_id.clone(), + dependency_targets: managed_dependency_targets(&projection.expected.dependencies), + }; + + for _ in 0..CAS_ATTEMPTS { + let observed = self.store.read(query.clone())?; + let admitted = self.select_presence(&observed, incoming, authority.as_ref())?; + let dependencies = + self.dependency_evidence(&projection.expected.dependencies, &observed, now)?; + let (presence, mut disagreements, replacement_presence) = match admitted { + SelectedPresence::None => (None, projection_disagreements.clone(), None), + SelectedPresence::Stale(stored) => { + let record = decode_presence(&stored.canonical_bytes)?; + let mut disagreements = projection_disagreements.clone(); + disagreements.push(OdinTopologyDisagreement { + code: "stored-presence-not-current".into(), + expected: projection + .activation + .as_ref() + .map(IdunnRuntimeActivationRecord::canonical_sha256) + .transpose()?, + observed: Some(record.activation_witness_sha256), + }); + (None, disagreements, None) + } + SelectedPresence::Current { + correlation, + replacement, + } => { + let mut disagreements = projection_disagreements.clone(); + disagreements.extend_from_slice(correlation.disagreements()); + (Some(correlation), disagreements, replacement) + } + }; + if let Some(disagreement) = &lease_disagreement { + disagreements.push(disagreement.clone()); + } + if presence.is_none() { + disagreements.push(OdinTopologyDisagreement { + code: "signed-presence-missing".into(), + expected: Some("dual-authenticated-runtime-presence".into()), + observed: None, + }); + disagreements.extend(projection.expected.capabilities.iter().enumerate().map( + |(index, capability)| OdinTopologyDisagreement { + code: format!("expected-capability-{index:03}-missing"), + expected: Some(format!( + "{}/{}/{} capacity>={}", + capability.capability, + capability.schema, + capability.compatibility, + capability.minimum_capacity + )), + observed: None, + }, + )); + } + + let observed_write_lease = if let Some(presence) = &presence { + correlate_write_lease( + &projection.expected, + lease_sha256.as_deref(), + presence.claim().record().write_lease_sha256.as_deref(), + &presence.claim().record().state, + &mut disagreements, + ) + } else { + None + }; + disagreements.sort_by(|left, right| left.code.cmp(&right.code)); + + let prior = observed + .correlation + .as_ref() + .map(|stored| decode_correlation(&stored.canonical_bytes)) + .transpose()?; + let publisher_sequence = prior.as_ref().map_or(Ok(1), |record| { + record + .publisher_sequence + .checked_add(1) + .context("Odin topology publisher sequence exhausted") + })?; + let presence_record = presence.as_ref().map(|value| value.claim().record()); + let ready = presence_record.is_some_and(|record| record.state == "active") + && disagreements.is_empty() + && dependencies + .iter() + .all(|dependency| dependency.kind == "optional" || dependency.ready); + let mut correlation = OdinRuntimeTopologyCorrelationRecord { + schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(), + target: projection.expected.target.clone(), + expected_projection_sha256: projection.expected.canonical_sha256()?, + expected: true, + current_activation_sha256: projection + .activation + .as_ref() + .map(IdunnRuntimeActivationRecord::canonical_sha256) + .transpose()?, + signed_presence_sha256: presence + .as_ref() + .map(|value| value.claim().signed_presence_sha256().into()), + observed_presence_state: presence_record.map(|record| record.state.clone()), + observed_presence_publisher_sequence: presence_record + .map(|record| record.publisher_sequence), + observed_write_lease_sha256: observed_write_lease, + observed_capabilities: presence_record + .map_or_else(Vec::new, |record| record.capabilities.clone()), + runtime_id: projection.expected.runtime_id.clone(), + runtime_instance_id: projection + .activation + .as_ref() + .map(|activation| activation.runtime_instance_id.clone()), + present: presence.is_some(), + ready, + dependencies, + disagreements, + signer_identity_id: self.signer.identity_id().into(), + publisher_sequence, + observed_at_unix_millis: now, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + }; + if let Some(prior) = &prior + && same_correlation_facts(prior, &correlation) + { + return Ok(observed + .correlation + .context("decoded prior correlation disappeared")? + .canonical_bytes); + } + correlation.signature = self + .signer + .sign_correlation(&correlation.unsigned_signature_payload()?)?; + let canonical = correlation.canonical_bytes()?; + correlation.validate_against_expected(&projection.expected, lease_sha256.as_deref())?; + let (signed, unsigned) = + OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload(&canonical)?; + verify_service_identity_signature_with_public_key::< + OdinTopologyIdentity, + OdinRuntimeTopologyCorrelationPurpose, + >( + self.signer.public_key(), + &unsigned, + &ServiceIdentitySignature { + identity_id: signed.signer_identity_id, + signature: signed.signature, + }, + )?; + if let Some(authority) = &authority { + authenticate_odin_runtime_topology_correlation( + &canonical, + authority, + lease_sha256.as_deref(), + self.signer.public_key(), + OdinTopologyAuthenticationContext { + trusted_received_at_unix_millis: now, + maximum_age_millis: self.policy.correlation_maximum_age_millis, + maximum_future_skew_millis: self + .policy + .correlation_maximum_future_skew_millis, + }, + )?; + } + let stored_correlation = StoredCorrelation { + canonical_bytes: canonical.clone(), + stored_at: rfc3339_millis(now)?, + }; + if self.store.compare_exchange( + &observed, + replacement_presence.as_ref(), + &stored_correlation, + )? { + return Ok(canonical); + } + } + bail!("Odin topology store changed repeatedly during correlation") + } + + fn select_presence( + &self, + observed: &OdinStoreSnapshot, + incoming: Option<(&[u8], u64)>, + authority: Option<&cultnet_rs::VerifiedRuntimeAuthority>, + ) -> Result { + let stored_record = observed + .presence + .as_ref() + .map(|stored| decode_presence(&stored.canonical_bytes)) + .transpose()?; + let Some(authority) = authority else { + ensure!( + incoming.is_none(), + "runtime presence has no exact current activation and provider anchor" + ); + return Ok(observed + .presence + .as_ref() + .map_or(SelectedPresence::None, |stored| { + SelectedPresence::Stale(stored.clone()) + })); + }; + let selected = if let Some((bytes, received_at)) = incoming { + if let Some(stored_presence) = observed + .presence + .as_ref() + .filter(|stored| stored.canonical_bytes == bytes) + { + let stored_claim = authenticate_runtime_presence_claim( + &stored_presence.canonical_bytes, + authority, + self.presence_context(stored_presence.trusted_received_at_unix_millis), + )?; + return Ok(SelectedPresence::Current { + correlation: correlate_runtime_presence_claim(stored_claim, authority)?, + replacement: None, + }); + } + let claim = authenticate_runtime_presence_claim( + bytes, + authority, + self.presence_context(received_at), + )?; + if let Some(stored) = &stored_record { + if claim.record().publisher_sequence < stored.publisher_sequence { + bail!("runtime presence publisher sequence was reordered"); + } + if claim.record().publisher_sequence == stored.publisher_sequence { + bail!("runtime presence publisher sequence was reused with different bytes"); + } + } + let replacement = AdmittedPresence::new(bytes.to_vec(), received_at)?; + Some((claim, Some(replacement))) + } else if let Some(stored) = &observed.presence { + match authenticate_runtime_presence_claim( + &stored.canonical_bytes, + authority, + self.presence_context(stored.trusted_received_at_unix_millis), + ) { + Ok(claim) => Some((claim, None)), + Err(error) => { + let prior_matches_current = + observed.correlation.as_ref().is_some_and(|value| { + decode_correlation(&value.canonical_bytes).is_ok_and(|record| { + record.expected_projection_sha256 == authority.expected_sha256() + && record.current_activation_sha256.as_deref() + == Some(authority.activation_sha256()) + }) + }); + if prior_matches_current { + return Err(error).context("stored current presence failed authentication"); + } + return Ok(SelectedPresence::Stale(stored.clone())); + } + } + } else { + None + }; + match selected { + Some((claim, replacement)) => Ok(SelectedPresence::Current { + correlation: correlate_runtime_presence_claim(claim, authority)?, + replacement, + }), + None => Ok(SelectedPresence::None), + } + } + + fn dependency_evidence( + &self, + requirements: &[IdunnExpectedDependency], + observed: &OdinStoreSnapshot, + now: u64, + ) -> Result> { + requirements + .iter() + .map(|requirement| { + let mut evidence = OdinRuntimeDependencyEvidence { + kind: requirement.kind.clone(), + capability: requirement.capability.clone(), + schema: requirement.schema.clone(), + compatibility: requirement.compatibility.clone(), + provider_id: requirement.provider_id.clone(), + provider_authority: requirement.provider_authority.clone(), + provider_expected_projection_sha256: requirement + .provider_expected_projection_sha256 + .clone(), + provider_endpoint: requirement.provider_endpoint.clone(), + observed_capacity: None, + provider_evidence_sha256: None, + ready: false, + }; + let Some(provider_id) = requirement.provider_id.as_deref() else { + return Ok(evidence); + }; + if requirement.provider_authority.as_deref() != Some("managed-incarnation") { + return Ok(evidence); + } + let Some(provider_projection) = self.projections.current_projection(provider_id)? + else { + return Ok(evidence); + }; + provider_projection.validate()?; + if provider_projection.expected.canonical_sha256()? + != requirement + .provider_expected_projection_sha256 + .as_deref() + .unwrap_or_default() + { + return Ok(evidence); + } + if let Some(endpoint) = requirement.provider_endpoint.as_deref() + && provider_projection + .expected + .route + .as_ref() + .map(|route| route.stable_endpoint.as_str()) + != Some(endpoint) + { + return Ok(evidence); + } + let (provider_authority, authority_disagreements) = + classify_runtime_authority(&provider_projection, &self.idunn_anchor, now)?; + let Some(provider_authority) = provider_authority else { + return Ok(evidence); + }; + if !authority_disagreements.is_empty() { + return Ok(evidence); + } + let Some(stored) = observed + .dependency_correlations + .get(provider_id) + .and_then(Option::as_ref) + else { + return Ok(evidence); + }; + let (current_lease_sha256, _) = classify_current_lease(&provider_projection)?; + let Ok(authenticated) = authenticate_odin_runtime_topology_correlation( + &stored.canonical_bytes, + &provider_authority, + current_lease_sha256.as_deref(), + self.signer.public_key(), + OdinTopologyAuthenticationContext { + trusted_received_at_unix_millis: now, + maximum_age_millis: self.policy.correlation_maximum_age_millis, + maximum_future_skew_millis: self + .policy + .correlation_maximum_future_skew_millis, + }, + ) else { + return Ok(evidence); + }; + let provider = authenticated.record(); + let capacity = provider + .observed_capabilities + .iter() + .find(|capability| { + capability.capability == requirement.capability + && capability.schema == requirement.schema + && capability.compatibility == requirement.compatibility + }) + .map(|capability| capability.capacity); + evidence.observed_capacity = capacity; + evidence.provider_evidence_sha256 = Some(provider.canonical_sha256()?); + evidence.ready = provider.ready + && capacity.is_some_and(|capacity| capacity >= requirement.minimum_capacity); + Ok(evidence) + }) + .collect() + } + + fn presence_context( + &self, + trusted_received_at_unix_millis: u64, + ) -> RuntimePresenceAuthenticationContext { + RuntimePresenceAuthenticationContext { + trusted_received_at_unix_millis, + maximum_age_millis: self.policy.presence_maximum_age_millis, + maximum_future_skew_millis: self.policy.presence_maximum_future_skew_millis, + } + } +} + +enum SelectedPresence { + None, + Stale(AdmittedPresence), + Current { + correlation: cultnet_rs::RuntimePresenceCorrelation, + replacement: Option, + }, +} + +fn correlate_write_lease( + expected: &IdunnExpectedIncarnationRecord, + current: Option<&str>, + observed: Option<&str>, + presence_state: &str, + disagreements: &mut Vec, +) -> Option { + if presence_state == "warming" && observed.is_none() { + return None; + } + if expected.write_lease_required && current.is_some() && current == observed { + return observed.map(str::to_string); + } + if expected.write_lease_required { + disagreements.push(OdinTopologyDisagreement { + code: "write-lease".into(), + expected: Some( + current + .unwrap_or("current-idunn-process-write-lease") + .into(), + ), + observed: observed.map(str::to_string), + }); + } else if observed.is_some() { + disagreements.push(OdinTopologyDisagreement { + code: "unexpected-write-lease".into(), + expected: None, + observed: observed.map(str::to_string), + }); + } + None +} + +fn same_correlation_facts( + prior: &OdinRuntimeTopologyCorrelationRecord, + next: &OdinRuntimeTopologyCorrelationRecord, +) -> bool { + let mut normalized = next.clone(); + normalized.publisher_sequence = prior.publisher_sequence; + normalized.observed_at_unix_millis = prior.observed_at_unix_millis; + normalized.signature = prior.signature.clone(); + &normalized == prior +} + +fn managed_dependency_targets(requirements: &[IdunnExpectedDependency]) -> Vec { + requirements + .iter() + .filter(|dependency| { + dependency.provider_authority.as_deref() == Some("managed-incarnation") + }) + .filter_map(|dependency| dependency.provider_id.clone()) + .collect() +} + +fn classify_runtime_authority( + projection: &IdunnRuntimeProjection, + idunn_anchor: &ServiceIdentityTrustAnchor, + now: u64, +) -> Result<( + Option, + Vec, +)> { + let expected = &projection.expected; + let expected_sha256 = expected.canonical_sha256()?; + let mut disagreements = Vec::new(); + match &projection.provider_anchor { + None => disagreements.push(OdinTopologyDisagreement { + code: "provider-trust-anchor-missing".into(), + expected: Some(expected.expected_signer_identity_id.clone()), + observed: None, + }), + Some(anchor) => { + push_disagreement( + &mut disagreements, + "provider-trust-anchor-id", + Some(runtime_presence_anchor_id(&expected.target)), + Some(anchor.trust_anchor_id.clone()), + ); + push_disagreement( + &mut disagreements, + "provider-service-id", + Some(expected.target.clone()), + Some(anchor.service_id.clone()), + ); + push_disagreement( + &mut disagreements, + "provider-runtime-id", + Some(expected.runtime_id.clone()), + Some(anchor.runtime_id.clone()), + ); + push_disagreement( + &mut disagreements, + "provider-signer-identity", + Some(expected.expected_signer_identity_id.clone()), + Some(anchor.signer_identity_id.clone()), + ); + push_disagreement( + &mut disagreements, + "provider-signing-purpose", + Some(GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE.into()), + Some(anchor.signing_purpose.clone()), + ); + push_disagreement( + &mut disagreements, + "provider-signed-schema", + Some(GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()), + Some(anchor.signed_schema.clone()), + ); + if anchor.bound_at_unix_millis > now + || anchor + .expires_at_unix_millis + .is_some_and(|expires_at| now >= expires_at) + { + disagreements.push(OdinTopologyDisagreement { + code: "provider-trust-anchor-time".into(), + expected: Some(format!("current-at:{now}")), + observed: Some(format!( + "bound:{};expires:{}", + anchor.bound_at_unix_millis, + anchor + .expires_at_unix_millis + .map_or_else(|| "none".into(), |value| value.to_string()) + )), + }); + } + } + } + match &projection.activation { + None => disagreements.push(OdinTopologyDisagreement { + code: "current-activation-missing".into(), + expected: Some(expected_sha256.clone()), + observed: None, + }), + Some(activation) => { + if activation.idunn_signer_identity_id != idunn_anchor.identity_id { + disagreements.push(OdinTopologyDisagreement { + code: "activation-idunn-signer".into(), + expected: Some(idunn_anchor.identity_id.clone()), + observed: Some(activation.idunn_signer_identity_id.clone()), + }); + } else { + verify_service_identity_signature::< + IdunnServiceIdentity, + IdunnRuntimeActivationPurpose, + >( + idunn_anchor, + &activation.unsigned_signature_payload()?, + &ServiceIdentitySignature { + identity_id: activation.idunn_signer_identity_id.clone(), + signature: activation.signature.clone(), + }, + ) + .context("verifying projected Idunn runtime activation")?; + } + push_disagreement( + &mut disagreements, + "activation-expected-projection", + Some(expected_sha256.clone()), + Some(activation.expected_projection_sha256.clone()), + ); + push_disagreement( + &mut disagreements, + "activation-runtime-id", + Some(expected.runtime_id.clone()), + Some(activation.runtime_id.clone()), + ); + } + } + disagreements.sort_by(|left, right| left.code.cmp(&right.code)); + let authority = if disagreements.is_empty() { + let anchor = projection.provider_anchor.as_ref().unwrap(); + let activation = projection.activation.as_ref().unwrap(); + Some(verify_runtime_authority( + expected, + activation, + idunn_anchor, + &anchor.signer_public_key, + )?) + } else { + None + }; + Ok((authority, disagreements)) +} + +fn classify_current_lease( + projection: &IdunnRuntimeProjection, +) -> Result<(Option, Option)> { + let Some(lease) = &projection.current_lease else { + return Ok((None, None)); + }; + let lease_sha256 = lease.canonical_sha256()?; + let activation = projection.activation.as_ref(); + let binds = activation + .map(|activation| lease_binds(&projection.expected, activation, lease)) + .transpose()? + .unwrap_or(false); + if projection.expected.write_lease_required && binds { + return Ok((Some(lease_sha256), None)); + } + let expected_activation = activation + .map(IdunnRuntimeActivationRecord::canonical_sha256) + .transpose()? + .unwrap_or_else(|| "none".into()); + Ok(( + None, + Some(OdinTopologyDisagreement { + code: "projected-write-lease".into(), + expected: Some(format!( + "expected:{};activation:{expected_activation}", + projection.expected.canonical_sha256()? + )), + observed: Some(lease_sha256), + }), + )) +} + +fn lease_binds( + expected: &IdunnExpectedIncarnationRecord, + activation: &IdunnRuntimeActivationRecord, + lease: &IdunnProcessWriteLeaseRecord, +) -> Result { + Ok(lease.target == expected.target + && lease.expected_projection_sha256 == expected.canonical_sha256()? + && lease.plan_id == expected.plan_id + && lease.incarnation_id == expected.incarnation_id + && lease.sealed_release_id == expected.sealed_release_id + && lease.activation_witness_sha256 == activation.canonical_sha256()? + && Some(lease.state_schema_generation.as_str()) + == expected.state_schema_generation.as_deref() + && Some(lease.state_contract_sha256.as_str()) == expected.state_contract_sha256.as_deref() + && lease.runtime_id == expected.runtime_id + && lease.runtime_instance_id == activation.runtime_instance_id) +} + +fn push_disagreement( + disagreements: &mut Vec, + code: &str, + expected: Option, + observed: Option, +) { + if expected != observed { + disagreements.push(OdinTopologyDisagreement { + code: code.into(), + expected, + observed, + }); + } +} + +fn runtime_presence_anchor_id(target: &str) -> String { + format!("root/{target}/runtime-presence") +} + +fn presence_store_key(target: &str, signer_identity_id: &str) -> String { + format!("{target}/{signer_identity_id}") +} + +fn unique_envelope<'a>( + entries: &'a [CultCacheEnvelope], + record_type: &str, + key: &str, +) -> Result> { + let mut matches = entries + .iter() + .filter(|entry| entry.r#type == record_type && entry.key == key); + let current = matches.next(); + ensure!(matches.next().is_none(), "CultCache identity is ambiguous"); + Ok(current) +} + +fn ensure_schema(envelope: &CultCacheEnvelope, schema: &str) -> Result<()> { + ensure!( + envelope.schema_id.as_deref() == Some(schema), + "CultCache envelope schema is substituted" + ); + Ok(()) +} + +fn decode_canonical(bytes: &[u8], label: &str) -> Result +where + T: serde::de::DeserializeOwned + serde::Serialize, +{ + let value = rmp_serde::from_slice(bytes).with_context(|| format!("decoding {label}"))?; + ensure!( + rmp_serde::to_vec(&value)? == bytes, + "{label} is not canonical positional MessagePack" + ); + Ok(value) +} + +fn decode_presence(bytes: &[u8]) -> Result { + let record: GameCultRuntimePresenceHealthRecord = decode_canonical(bytes, "runtime presence")?; + record.validate()?; + Ok(record) +} + +fn decode_correlation(bytes: &[u8]) -> Result { + Ok(OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload(bytes)?.0) +} + +fn decode_presence_envelope(envelope: &CultCacheEnvelope) -> Result { + ensure_schema(envelope, GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA)?; + decode_presence(&envelope.payload)?; + Ok(AdmittedPresence { + canonical_bytes: envelope.payload.clone(), + trusted_received_at_unix_millis: parse_rfc3339_millis(&envelope.stored_at)?, + stored_at: envelope.stored_at.clone(), + }) +} + +fn decode_correlation_envelope(envelope: &CultCacheEnvelope) -> Result { + ensure_schema(envelope, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA)?; + let record = decode_correlation(&envelope.payload)?; + ensure!( + record.target == envelope.key, + "stored correlation target is substituted" + ); + ensure!( + parse_rfc3339_millis(&envelope.stored_at)? == record.observed_at_unix_millis, + "stored correlation receipt time is substituted" + ); + Ok(StoredCorrelation { + canonical_bytes: envelope.payload.clone(), + stored_at: envelope.stored_at.clone(), + }) +} + +fn presence_envelope(key: &str, presence: &AdmittedPresence) -> CultCacheEnvelope { + CultCacheEnvelope { + key: key.into(), + r#type: GameCultRuntimePresenceHealthRecord::TYPE.into(), + payload: presence.canonical_bytes.clone(), + stored_at: presence.stored_at.clone(), + schema_id: Some(GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()), + } +} + +fn correlation_envelope(target: &str, correlation: &StoredCorrelation) -> CultCacheEnvelope { + CultCacheEnvelope { + key: target.into(), + r#type: OdinRuntimeTopologyCorrelationRecord::TYPE.into(), + payload: correlation.canonical_bytes.clone(), + stored_at: correlation.stored_at.clone(), + schema_id: Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into()), + } +} + +fn insert_condition( + conditions: &mut BTreeMap<(String, String), CultCacheExpectedEnvelope>, + condition: CultCacheExpectedEnvelope, +) -> Result<()> { + let identity = (condition.r#type.clone(), condition.key.clone()); + if let Some(current) = conditions.get(&identity) { + ensure!( + current.current == condition.current, + "store read set is incoherent" + ); + } else { + conditions.insert(identity, condition); + } + Ok(()) +} + +fn rfc3339_millis(value: u64) -> Result { + let value: i64 = value + .try_into() + .context("timestamp exceeds RFC3339 range")?; + Ok(DateTime::::from_timestamp_millis(value) + .context("timestamp exceeds RFC3339 range")? + .to_rfc3339_opts(SecondsFormat::Millis, true)) +} + +fn parse_rfc3339_millis(value: &str) -> Result { + DateTime::parse_from_rfc3339(value)? + .timestamp_millis() + .try_into() + .context("CultCache timestamp predates Unix epoch") +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use cultnet_rs::{ + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultProviderHealthIdentity, + GameCultRuntimeCapability, GameCultRuntimePresenceHealthPurpose, IdunnExpectedCapability, + IdunnExpectedRoute, IdunnRuntimeActivationLaunch, IdunnRuntimeActivationSigner, + IdunnServiceIdentity, enroll_service_identity_at, + }; + use tempfile::TempDir; + + use super::*; + + const NOW: u64 = 1_000_000; + + #[derive(Clone, Copy)] + struct FixedClock(u64); + + impl Clock for FixedClock { + fn now_unix_millis(&self) -> Result { + Ok(self.0) + } + } + + struct TestService { + projection: IdunnRuntimeProjection, + provider_signer: ServiceIdentitySigner, + activation_signer: IdunnRuntimeActivationSigner, + } + + impl TestService { + fn signed_presence(&self, sequence: u64, edit: F) -> Result> + where + F: FnOnce(&mut GameCultRuntimePresenceHealthRecord), + { + let expected = &self.projection.expected; + let activation = self.projection.activation.as_ref().unwrap(); + let mut record = GameCultRuntimePresenceHealthRecord { + schema_version: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: expected.canonical_sha256()?, + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: activation.canonical_sha256()?, + state_schema_generation: expected.state_schema_generation.clone(), + state_contract_sha256: expected.state_contract_sha256.clone(), + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + bound_endpoint: expected + .route + .as_ref() + .map(|route| route.candidate_endpoint.clone()), + capabilities: expected + .capabilities + .iter() + .map(|capability| GameCultRuntimeCapability { + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.minimum_capacity, + }) + .collect(), + health_contract: expected.health_contract.clone(), + state: "active".into(), + detail: "ready".into(), + write_lease_sha256: self + .projection + .current_lease + .as_ref() + .map(IdunnProcessWriteLeaseRecord::canonical_sha256) + .transpose()?, + signer_identity_id: self.provider_signer.entry().identity_id.clone(), + publisher_sequence: sequence, + observed_at_unix_millis: NOW - 10, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + activation_signer_identity_id: activation.activation_signer_identity_id.clone(), + activation_signature: Vec::new(), + }; + edit(&mut record); + let proof_payload = record.canonical_proof_payload()?; + record.signature = self + .provider_signer + .sign::(&proof_payload) + .signature; + record.activation_signature = self.activation_signer.sign_presence_proof(&record)?; + record.validate()?; + Ok(rmp_serde::to_vec(&record)?) + } + } + + struct TestWorld { + _temp: TempDir, + projection_path: PathBuf, + topology_path: PathBuf, + identity_root: PathBuf, + idunn_signer: ServiceIdentitySigner, + idunn_anchor: ServiceIdentityTrustAnchor, + odin_signer: ServiceIdentitySigner, + } + + impl TestWorld { + fn new() -> Result { + let temp = tempfile::tempdir()?; + let identity_root = temp.path().join("identities"); + let idunn_signer = enroll_service_identity_at::( + &identity_root.join("idunn.cc"), + )?; + let idunn_anchor = idunn_signer.trust_anchor()?; + let odin_signer = + enroll_service_identity_at::(&identity_root.join("odin.cc"))?; + Ok(Self { + projection_path: temp.path().join("idunn-projection.cc"), + topology_path: temp.path().join("odin-topology.cc"), + identity_root, + idunn_signer, + idunn_anchor, + odin_signer, + _temp: temp, + }) + } + + fn service( + &self, + target: &str, + dependencies: Vec, + stateful: bool, + ) -> Result { + let provider_signer = enroll_service_identity_at::( + &self.identity_root.join(format!("{target}-provider.cc")), + )?; + let expected = IdunnExpectedIncarnationRecord { + schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), + target: target.into(), + plan_id: digest('1'), + incarnation_id: format!("{target}/generation-1"), + sealed_release_id: digest('2'), + source_repository: format!("github.com/GameCult/{target}"), + source_revision: "3".repeat(40), + recipe_sha256: digest('4'), + runtime_id: format!("{target}-runtime"), + expected_signer_identity_id: provider_signer.entry().identity_id.clone(), + health_contract: format!("{target}.runtime-health.v1"), + artifact_sha256: digest('5'), + state_schema_generation: stateful.then(|| "state-v1".into()), + state_contract_sha256: stateful.then(|| digest('6')), + write_lease_required: stateful, + route: Some(IdunnExpectedRoute { + route_id: format!("{target}-route"), + transport: "tcp".into(), + stable_endpoint: format!("tcp://{target}.internal:1000"), + candidate_endpoint: format!("tcp://127.0.0.1:{}", target.len() + 10_000), + }), + capabilities: vec![IdunnExpectedCapability { + capability: "data".into(), + schema: "data.v1".into(), + compatibility: "v1".into(), + minimum_capacity: 1, + }], + dependencies, + }; + expected.validate()?; + let launch = IdunnRuntimeActivationLaunch::issue( + &expected, + digest('7'), + NOW - 20, + &self.idunn_signer, + )?; + let activation = launch.activation().clone(); + let mut credential = Vec::new(); + let emitted = launch.write_credential(&mut credential)?; + ensure!( + emitted == activation, + "activation launch changed while emitted" + ); + let activation_signer = + IdunnRuntimeActivationSigner::from_credential_reader(credential.as_slice())?; + let provider_anchor = GameCultServiceTrustAnchorRecord { + schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), + trust_anchor_id: runtime_presence_anchor_id(target), + service_id: target.into(), + runtime_id: expected.runtime_id.clone(), + signer_identity_id: provider_signer.entry().identity_id.clone(), + signer_public_key: provider_signer.entry().public_key.clone(), + signature_algorithm: "ed25519".into(), + signing_purpose: GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE.into(), + signed_schema: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), + binding_authority: "root".into(), + bound_at_unix_millis: NOW - 100, + expires_at_unix_millis: None, + private_state_exposed: false, + }; + let current_lease = if stateful { + Some(IdunnProcessWriteLeaseRecord { + schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), + target: target.into(), + expected_projection_sha256: expected.canonical_sha256()?, + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: activation.canonical_sha256()?, + state_schema_generation: expected.state_schema_generation.clone().unwrap(), + state_contract_sha256: expected.state_contract_sha256.clone().unwrap(), + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + warming_presence_sha256: digest('8'), + lease_epoch: 1, + issued_at_unix_millis: NOW - 5, + }) + } else { + None + }; + let projection = IdunnRuntimeProjection { + expected, + provider_anchor: Some(provider_anchor), + activation: Some(activation), + current_lease, + }; + projection.validate()?; + self.publish_projection(&projection)?; + Ok(TestService { + projection, + provider_signer, + activation_signer, + }) + } + + fn publish_projection(&self, projection: &IdunnRuntimeProjection) -> Result<()> { + let store = SingleFileMessagePackBackingStore::new(&self.projection_path); + let current = if self.projection_path.is_file() { + store.pull_all_read_only_snapshot()? + } else { + Vec::new() + }; + let anchor_key = runtime_presence_anchor_id(&projection.expected.target); + let mut next: Vec<_> = current + .iter() + .filter(|envelope| { + !((envelope.r#type == IdunnExpectedIncarnationRecord::TYPE + || envelope.r#type == IdunnRuntimeActivationRecord::TYPE + || envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE) + && envelope.key == projection.expected.target) + && !(envelope.r#type == GameCultServiceTrustAnchorRecord::TYPE + && envelope.key == anchor_key) + }) + .cloned() + .collect(); + next.push(CultCacheEnvelope { + key: projection.expected.target.clone(), + r#type: IdunnExpectedIncarnationRecord::TYPE.into(), + payload: projection.expected.canonical_bytes()?, + stored_at: rfc3339_millis(NOW - 30)?, + schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), + }); + if let Some(anchor) = &projection.provider_anchor { + next.push(CultCacheEnvelope { + key: anchor_key, + r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), + payload: rmp_serde::to_vec(anchor)?, + stored_at: rfc3339_millis(anchor.bound_at_unix_millis)?, + schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), + }); + } + if let Some(activation) = &projection.activation { + next.push(CultCacheEnvelope { + key: projection.expected.target.clone(), + r#type: IdunnRuntimeActivationRecord::TYPE.into(), + payload: activation.canonical_bytes()?, + stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, + schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), + }); + } + if let Some(lease) = &projection.current_lease { + next.push(CultCacheEnvelope { + key: projection.expected.target.clone(), + r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), + payload: lease.canonical_bytes()?, + stored_at: rfc3339_millis(lease.issued_at_unix_millis)?, + schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), + }); + } + ensure!( + store.compare_exchange_snapshot(¤t, &next)?, + "test projection CAS failed" + ); + Ok(()) + } + + fn engine( + &self, + now: u64, + ) -> OdinTopologyAuthority< + CultCacheIdunnProjectionSource, + CultCacheOdinTopologyStore, + &ServiceIdentitySigner, + FixedClock, + > { + OdinTopologyAuthority::new( + CultCacheIdunnProjectionSource::new(&self.projection_path), + CultCacheOdinTopologyStore::new(&self.topology_path), + &self.odin_signer, + FixedClock(now), + self.idunn_anchor.clone(), + AuthenticationPolicy::default(), + ) + } + + fn replace_provider_generation(&self, service: &mut TestService) -> Result<()> { + service.projection.expected.plan_id = digest('9'); + service.projection.expected.incarnation_id = + format!("{}/generation-2", service.projection.expected.target); + service.projection.expected.validate()?; + let launch = IdunnRuntimeActivationLaunch::issue( + &service.projection.expected, + digest('a'), + NOW + 10, + &self.idunn_signer, + )?; + service.projection.activation = Some(launch.activation().clone()); + service.projection.current_lease = None; + self.publish_projection(&service.projection) + } + } + + fn digest(byte: char) -> String { + format!("sha256-{}", byte.to_string().repeat(64)) + } + + fn decode_signed(bytes: &[u8]) -> Result { + Ok(OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload(bytes)?.0) + } + + #[test] + fn partial_or_mismatched_projection_remains_signed_expected_only() -> Result<()> { + let world = TestWorld::new()?; + let mut service = world.service("ghostlight", Vec::new(), false)?; + service.projection.activation = None; + let anchor = service.projection.provider_anchor.as_mut().unwrap(); + anchor.trust_anchor_id = "root/other/runtime-presence".into(); + anchor.service_id = "other".into(); + world.publish_projection(&service.projection)?; + + let bytes = world.engine(NOW).refresh("ghostlight")?.unwrap(); + let record = decode_signed(&bytes)?; + assert!(record.expected); + assert!(!record.present); + assert!(!record.ready); + assert!(record.signed_presence_sha256.is_none()); + let codes: Vec<_> = record + .disagreements + .iter() + .map(|value| value.code.as_str()) + .collect(); + assert!(codes.contains(&"current-activation-missing")); + assert!(codes.contains(&"provider-service-id")); + assert!(codes.contains(&"provider-trust-anchor-id")); + assert!(codes.contains(&"signed-presence-missing")); + Ok(()) + } + + #[test] + fn authenticated_disagreement_is_present_but_not_ready() -> Result<()> { + let world = TestWorld::new()?; + let service = world.service("ghostlight", Vec::new(), false)?; + let presence = service.signed_presence(1, |record| { + record.health_contract = "ghostlight.other-health.v1".into(); + })?; + + let record = decode_signed(&world.engine(NOW).admit_presence( + "ghostlight", + &presence, + NOW, + )?)?; + assert!(record.present); + assert!(!record.ready); + assert!( + record + .disagreements + .iter() + .any(|value| value.code == "health-contract") + ); + Ok(()) + } + + #[test] + fn exact_active_presence_and_current_lease_are_ready() -> Result<()> { + let world = TestWorld::new()?; + let service = world.service("ghostlight", Vec::new(), true)?; + let presence = service.signed_presence(1, |_| {})?; + + let record = decode_signed(&world.engine(NOW).admit_presence( + "ghostlight", + &presence, + NOW, + )?)?; + assert!(record.present); + assert!(record.ready); + assert_eq!( + record.observed_write_lease_sha256, + service + .projection + .current_lease + .as_ref() + .map(IdunnProcessWriteLeaseRecord::canonical_sha256) + .transpose()? + ); + Ok(()) + } + + #[test] + fn stale_projected_lease_is_visible_while_candidate_warming_is_present() -> Result<()> { + let world = TestWorld::new()?; + let mut service = world.service("ghostlight", Vec::new(), true)?; + let stale = service.projection.current_lease.as_mut().unwrap(); + stale.expected_projection_sha256 = digest('a'); + stale.plan_id = digest('b'); + stale.incarnation_id = "ghostlight/incumbent".into(); + stale.sealed_release_id = digest('c'); + stale.activation_witness_sha256 = digest('d'); + stale.runtime_instance_id = digest('e'); + world.publish_projection(&service.projection)?; + let warming = service.signed_presence(1, |record| { + record.state = "warming".into(); + record.detail = "starting".into(); + record.write_lease_sha256 = None; + })?; + + let record = decode_signed(&world.engine(NOW).admit_presence( + "ghostlight", + &warming, + NOW, + )?)?; + assert!(record.present); + assert!(!record.ready); + assert_eq!(record.observed_presence_state.as_deref(), Some("warming")); + assert!(record.observed_write_lease_sha256.is_none()); + assert!( + record + .disagreements + .iter() + .any(|value| value.code == "projected-write-lease") + ); + Ok(()) + } + + #[test] + fn provider_sequence_admission_is_monotonic_and_exact_retry_is_idempotent() -> Result<()> { + let world = TestWorld::new()?; + let service = world.service("ghostlight", Vec::new(), false)?; + let sequence_two = service.signed_presence(2, |_| {})?; + let engine = world.engine(NOW); + + let first = engine.admit_presence("ghostlight", &sequence_two, NOW)?; + let duplicate = engine.admit_presence("ghostlight", &sequence_two, NOW + 60_000)?; + assert_eq!(duplicate, first); + assert_eq!( + engine.current_signed_correlation("ghostlight")?, + Some(first) + ); + + let lower = service.signed_presence(1, |_| {})?; + assert!(engine.admit_presence("ghostlight", &lower, NOW).is_err()); + let reused = service.signed_presence(2, |record| record.detail = "different".into())?; + assert!(engine.admit_presence("ghostlight", &reused, NOW).is_err()); + + let entries = SingleFileMessagePackBackingStore::new(&world.topology_path) + .pull_all_read_only_snapshot()?; + let stored = unique_envelope( + &entries, + GameCultRuntimePresenceHealthRecord::TYPE, + &presence_store_key( + "ghostlight", + &service.projection.expected.expected_signer_identity_id, + ), + )? + .unwrap(); + assert_eq!(stored.payload, sequence_two); + assert_eq!(parse_rfc3339_millis(&stored.stored_at)?, NOW); + Ok(()) + } + + #[test] + fn restart_rehydrates_exact_bytes_without_retimestamping() -> Result<()> { + let world = TestWorld::new()?; + let service = world.service("ghostlight", Vec::new(), false)?; + let presence = service.signed_presence(1, |_| {})?; + let first = world + .engine(NOW) + .admit_presence("ghostlight", &presence, NOW)?; + + let after_restart = world.engine(NOW + 100).refresh("ghostlight")?.unwrap(); + assert_eq!(after_restart, first); + assert_eq!(decode_signed(&after_restart)?.observed_at_unix_millis, NOW); + Ok(()) + } + + #[test] + fn withdrawn_expected_removes_current_correlation_but_preserves_presence_history() -> Result<()> + { + let world = TestWorld::new()?; + let service = world.service("ghostlight", Vec::new(), false)?; + let presence = service.signed_presence(1, |_| {})?; + let engine = world.engine(NOW); + engine.admit_presence("ghostlight", &presence, NOW)?; + assert!(engine.current_signed_correlation("ghostlight")?.is_some()); + + std::fs::remove_file(&world.projection_path)?; + assert!(engine.current_signed_correlation("ghostlight")?.is_none()); + + let entries = SingleFileMessagePackBackingStore::new(&world.topology_path) + .pull_all_read_only_snapshot()?; + assert!(entries.iter().any(|entry| { + entry.r#type == GameCultRuntimePresenceHealthRecord::TYPE && entry.payload == presence + })); + assert!(!entries.iter().any(|entry| { + entry.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE && entry.key == "ghostlight" + })); + Ok(()) + } + + #[test] + fn dependencies_consume_only_exact_current_admitted_correlations() -> Result<()> { + let world = TestWorld::new()?; + let mut provider = world.service("provider", Vec::new(), false)?; + let provider_expected_sha256 = provider.projection.expected.canonical_sha256()?; + let provider_endpoint = provider + .projection + .expected + .route + .as_ref() + .unwrap() + .stable_endpoint + .clone(); + let requirement = IdunnExpectedDependency { + kind: "required".into(), + capability: "data".into(), + schema: "data.v1".into(), + compatibility: "v1".into(), + minimum_capacity: 1, + startup: "before-promotion".into(), + provider_id: Some("provider".into()), + provider_authority: Some("managed-incarnation".into()), + provider_expected_projection_sha256: Some(provider_expected_sha256), + provider_endpoint: Some(provider_endpoint), + }; + let consumer = world.service("consumer", vec![requirement], false)?; + let provider_presence = provider.signed_presence(1, |_| {})?; + let consumer_presence = consumer.signed_presence(1, |_| {})?; + let engine = world.engine(NOW); + + engine.admit_presence("provider", &provider_presence, NOW)?; + let ready = decode_signed(&engine.admit_presence("consumer", &consumer_presence, NOW)?)?; + assert!(ready.ready); + assert!(ready.dependencies[0].ready); + assert!(ready.dependencies[0].provider_evidence_sha256.is_some()); + + world.replace_provider_generation(&mut provider)?; + let no_longer_exact = decode_signed(&engine.refresh("consumer")?.unwrap())?; + assert!(no_longer_exact.present); + assert!(!no_longer_exact.ready); + assert!(!no_longer_exact.dependencies[0].ready); + assert!( + no_longer_exact.dependencies[0] + .provider_evidence_sha256 + .is_none() + ); + Ok(()) + } +} diff --git a/crates/odin-daemon/src/main.rs b/crates/odin-daemon/src/main.rs new file mode 100644 index 0000000..99094d3 --- /dev/null +++ b/crates/odin-daemon/src/main.rs @@ -0,0 +1,1248 @@ +use std::cell::RefCell; +use std::collections::{BTreeMap, BTreeSet, VecDeque}; +use std::fs::{File, OpenOptions}; +use std::net::{SocketAddr, UdpSocket}; +use std::os::fd::{FromRawFd, RawFd}; +use std::path::{Path, PathBuf}; +use std::rc::Rc; +use std::thread; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +use anyhow::{Context, Result, anyhow, bail, ensure}; +use chrono::{DateTime, Utc}; +use cultcache_rs::{ + CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, +}; +use cultmesh_rs::{ + CultMeshRudpDocumentPublishOptions, CultMeshRudpDocumentServer, + CultMeshRudpDocumentServerOptions, CultMeshRudpPollOutcome, CultMeshRudpRawDocumentReceipt, + CultMeshRudpRawDocumentSink, CultMeshRudpSnapshotQuery, CultMeshRudpSnapshotSource, + CultMeshSystemClock, publish_cultnet_message_to_rudp_catalog, +}; +use cultnet_rs::{ + CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, + GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, GameCultProviderHealthIdentity, + GameCultRuntimeCapability, GameCultRuntimePresenceHealthPurpose, + GameCultRuntimePresenceHealthRecord, IDUNN_EXPECTED_INCARNATION_SCHEMA, + IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, + IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, + IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, IdunnServiceIdentity, + ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, OdinRuntimeTopologyCorrelationRecord, + OdinTopologyIdentity, ServiceIdentityProfile, ServiceIdentitySigner, + ServiceIdentityTrustAnchor, derive_service_identity_id, + open_service_identity_credential_reader, verify_runtime_authority, +}; +use fs2::FileExt; +use odin_daemon::{ + AuthenticationPolicy, CultCacheIdunnProjectionSource, CultCacheOdinTopologyStore, + IdunnProjectionSource, OdinTopologyAuthority, SystemClock, +}; + +const TARGET: &str = "odin"; +const HEALTH_CONTRACT: &str = "odin.runtime-health.v1"; +const RENDEZVOUS_CAPABILITY: &str = "odin.verse-rendezvous"; +const RENDEZVOUS_SCHEMA: &str = "odin.verse-topology.v1"; +const RENDEZVOUS_COMPATIBILITY: &str = "v1"; +const STATE_SCHEMA_GENERATION: &str = "odin-v2"; +// rmp-serde SHA-256 of deployment/idunn/recipe.toml's exact [state] value. +const STATE_CONTRACT_SHA256: &str = + "sha256-4f2f2dcd931d16f6b02bf295f41227b867aa982765661d55fa9f29fb2db7e449"; + +const RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE"; +const CANDIDATE_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_CANDIDATE_BIND"; +const PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE"; +const TOPOLOGY_IDENTITY_ENVIRONMENT: &str = "ODIN_TOPOLOGY_IDENTITY"; +const SYSTEMD_LISTEN_PID_ENVIRONMENT: &str = "LISTEN_PID"; +const SYSTEMD_LISTEN_FDS_ENVIRONMENT: &str = "LISTEN_FDS"; +const SYSTEMD_LISTEN_FDNAMES_ENVIRONMENT: &str = "LISTEN_FDNAMES"; +const ACTIVATION_SIGNER_FD_NAME: &str = IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME; +const PROVIDER_SIGNER_FD_NAME: &str = "gamecult-runtime-presence-identity"; +const SYSTEMD_LISTEN_FDS_START: RawFd = 3; + +const BOOTSTRAP_LEASE_TIMEOUT: Duration = Duration::from_secs(60); +const SELF_PUBLISH_TIMEOUT: Duration = Duration::from_secs(6); +const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); +const PROJECTION_REFRESH_INTERVAL: Duration = Duration::from_millis(250); +const IDLE_POLL_INTERVAL: Duration = Duration::from_millis(2); +const MAX_RECENT_WARMING_PROOFS: usize = 64; +const WARMING_PROOF_LIFETIME_MILLIS: u64 = 60_000; +const CAS_ATTEMPTS: usize = 8; + +type TopologyAuthority = OdinTopologyAuthority< + CultCacheIdunnProjectionSource, + CultCacheOdinTopologyStore, + ServiceIdentitySigner, + SystemClock, +>; + +#[derive(Clone, Debug)] +struct Options { + store: PathBuf, + idunn_projection: PathBuf, + idunn_anchor: PathBuf, +} + +struct RuntimeAuthority { + expected: IdunnExpectedIncarnationRecord, + expected_sha256: String, + activation: IdunnRuntimeActivationRecord, + activation_sha256: String, + activation_signer: IdunnRuntimeActivationSigner, + provider_signer: ServiceIdentitySigner, +} + +struct ProcessWriteLeaseGuard { + _lock: File, + record: IdunnProcessWriteLeaseRecord, + sha256: String, +} + +struct RuntimeState { + options: Options, + candidate: SocketAddr, + authority_material: RuntimeAuthority, + idunn_anchor: Option, + topology_signer: Option>, + topology: Option, + write_lease: Option, + write_lease_path: PathBuf, + recent_warming_proofs: VecDeque<(String, u64)>, + publisher_sequence: u64, +} + +#[derive(Clone)] +struct SinkHandle(Rc>); + +#[derive(Clone)] +struct SnapshotHandle(Rc>); + +impl CultMeshRudpRawDocumentSink for SinkHandle { + fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { + self.0.borrow_mut().accept_raw_document(receipt) + } +} + +impl CultMeshRudpSnapshotSource for SnapshotHandle { + fn raw_snapshot( + &mut self, + query: &CultMeshRudpSnapshotQuery, + ) -> Result> { + self.0.borrow_mut().raw_snapshot(query) + } +} + +impl RuntimeState { + fn open(options: Options, candidate: SocketAddr) -> Result { + let authority_material = load_runtime_authority(Path::new(&required_environment( + RUNTIME_BUNDLE_ENVIRONMENT, + )?))?; + require_expected_contract(&authority_material.expected, candidate)?; + + let idunn_anchor = read_trust_anchor::(&options.idunn_anchor)?; + let projection_source = CultCacheIdunnProjectionSource::new(&options.idunn_projection); + let projection = projection_source + .current_projection(TARGET)? + .context("Idunn projection has no Expected Odin incarnation")?; + ensure!( + projection.expected == authority_material.expected + && projection.activation.as_ref() == Some(&authority_material.activation), + "Idunn live projection differs from Odin's immutable runtime bundle" + ); + let provider_anchor = projection + .provider_anchor + .as_ref() + .context("Idunn projection has no Odin runtime-presence trust anchor")?; + ensure!( + provider_anchor.signer_identity_id + == authority_material.provider_signer.entry().identity_id + && provider_anchor.signer_public_key + == authority_material.provider_signer.entry().public_key, + "Odin provider signer differs from Idunn's projected trust anchor" + ); + verify_runtime_authority( + &authority_material.expected, + &authority_material.activation, + &idunn_anchor, + &provider_anchor.signer_public_key, + )?; + + let topology_identity_path = + PathBuf::from(required_environment(TOPOLOGY_IDENTITY_ENVIRONMENT)?); + let topology_signer = open_service_identity_credential_reader::( + File::open(&topology_identity_path).with_context(|| { + format!( + "opening Odin topology identity credential {}", + topology_identity_path.display() + ) + })?, + )?; + let store_sequence = prior_self_publisher_sequence( + &options.store, + authority_material + .provider_signer + .entry() + .identity_id + .as_str(), + )?; + + Ok(Self { + options, + candidate, + authority_material, + idunn_anchor: Some(idunn_anchor), + topology_signer: Some(topology_signer), + topology: None, + write_lease: None, + write_lease_path: PathBuf::from(required_environment(PROCESS_WRITE_LEASE_ENVIRONMENT)?), + recent_warming_proofs: VecDeque::new(), + publisher_sequence: store_sequence, + }) + } + + fn activated(&self) -> bool { + self.topology.is_some() && self.write_lease.is_some() + } + + fn try_activate(&mut self) -> Result { + if self.activated() { + return Ok(true); + } + let Some(lease) = acquire_process_write_lease( + &self.write_lease_path, + &self.authority_material, + &self.recent_warming_proofs, + )? + else { + return Ok(false); + }; + let Some(projected) = CultCacheIdunnProjectionSource::new(&self.options.idunn_projection) + .current_projection(TARGET)? + else { + return Ok(false); + }; + if projected.current_lease.as_ref() != Some(&lease.record) { + return Ok(false); + } + let signer = self + .topology_signer + .take() + .context("Odin topology signer was already consumed")?; + let idunn_anchor = self + .idunn_anchor + .take() + .context("Idunn trust anchor was already consumed")?; + self.write_lease = Some(lease); + self.topology = Some(OdinTopologyAuthority::new( + CultCacheIdunnProjectionSource::new(&self.options.idunn_projection), + CultCacheOdinTopologyStore::new(&self.options.store), + signer, + SystemClock, + idunn_anchor, + AuthenticationPolicy::default(), + )); + Ok(true) + } + + fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { + ensure!( + self.activated(), + "Odin does not admit or persist provider documents before its process-write lease" + ); + validate_raw_document_shape(&receipt.document)?; + if receipt.document.schema_id == GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA { + let presence = decode_presence(&receipt.document.payload)?; + ensure!( + receipt.document.record_key == presence.target, + "runtime-presence document key differs from its signed target" + ); + self.topology + .as_ref() + .context("Odin topology authority is absent")? + .admit_presence( + &presence.target, + &receipt.document.payload, + receipt.received_at_unix_millis, + )?; + return Ok(()); + } + persist_generic_document(&self.options.store, &receipt.document) + } + + fn raw_snapshot( + &mut self, + query: &CultMeshRudpSnapshotQuery, + ) -> Result> { + validate_snapshot_filters(query)?; + if exact_self_presence_query(query) { + let (state, detail) = if self.activated() { + self.require_current_write_lease()?; + ("active", format!("route-observation:{}", query.message_id)) + } else { + ("warming", format!("idunn-warming:{}", query.message_id)) + }; + return Ok(vec![self.signed_presence_document(state, &detail)?]); + } + ensure!( + self.activated(), + "Odin catalog is unavailable until Idunn grants its process-write lease" + ); + self.refresh_all_correlations()?; + self.stored_snapshot(query) + } + + fn signed_presence_document( + &mut self, + state: &str, + detail: &str, + ) -> Result { + let now = unix_millis()?; + self.publisher_sequence = self + .publisher_sequence + .checked_add(1) + .context("Odin runtime-presence publisher sequence exhausted")?; + let expected = &self.authority_material.expected; + let activation = &self.authority_material.activation; + let source_runtime_id = expected.runtime_id.clone(); + let mut record = GameCultRuntimePresenceHealthRecord { + schema_version: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), + target: expected.target.clone(), + expected_projection_sha256: self.authority_material.expected_sha256.clone(), + plan_id: expected.plan_id.clone(), + incarnation_id: expected.incarnation_id.clone(), + sealed_release_id: expected.sealed_release_id.clone(), + activation_witness_sha256: self.authority_material.activation_sha256.clone(), + state_schema_generation: expected.state_schema_generation.clone(), + state_contract_sha256: expected.state_contract_sha256.clone(), + runtime_id: expected.runtime_id.clone(), + runtime_instance_id: activation.runtime_instance_id.clone(), + bound_endpoint: Some(format!("rudp://{}", self.candidate)), + capabilities: expected + .capabilities + .iter() + .map(|capability| GameCultRuntimeCapability { + capability: capability.capability.clone(), + schema: capability.schema.clone(), + compatibility: capability.compatibility.clone(), + capacity: capability.minimum_capacity, + }) + .collect(), + health_contract: expected.health_contract.clone(), + state: state.into(), + detail: detail.into(), + write_lease_sha256: self.write_lease.as_ref().map(|lease| lease.sha256.clone()), + signer_identity_id: self + .authority_material + .provider_signer + .entry() + .identity_id + .clone(), + publisher_sequence: self.publisher_sequence, + observed_at_unix_millis: now, + signature_algorithm: "ed25519".into(), + signature: Vec::new(), + activation_signer_identity_id: activation.activation_signer_identity_id.clone(), + activation_signature: Vec::new(), + }; + ensure!( + (state == "warming" && record.write_lease_sha256.is_none()) + || (state == "active" && record.write_lease_sha256.is_some()), + "Odin presence state does not match its process-write authority" + ); + let proof_payload = record.canonical_proof_payload()?; + record.signature = self + .authority_material + .provider_signer + .sign::(&proof_payload) + .signature; + record.activation_signature = self + .authority_material + .activation_signer + .sign_presence_proof(&record)?; + record.validate()?; + let payload = rmp_serde::to_vec(&record)?; + ensure!( + rmp_serde::from_slice::(&payload)? == record, + "Odin runtime presence is not canonical MessagePack" + ); + if state == "warming" { + remember_recent_warming_proof( + &mut self.recent_warming_proofs, + record.canonical_sha256()?, + now, + ); + } + Ok(CultNetRawDocumentRecord { + schema_id: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), + record_key: TARGET.into(), + stored_at: rfc3339_millis(now)?, + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload, + source_runtime_id: Some(source_runtime_id), + source_agent_id: Some(record.signer_identity_id), + source_role: Some("runtime-presence-health-publisher".into()), + tags: Some(vec!["cultnet.transport.rudp.v0".into()]), + }) + } + + fn active_document_put(&mut self, detail: &str) -> Result { + self.require_current_write_lease()?; + let document = self.signed_presence_document("active", detail)?; + let sequence = self.publisher_sequence; + Ok(CultNetMessage::DocumentPutRaw { + message_id: format!( + "odin-presence:{}:{}:{sequence}", + self.authority_material.expected.runtime_id, + self.authority_material.activation.runtime_instance_id + ), + document, + }) + } + + fn require_current_write_lease(&self) -> Result<()> { + let held = self + .write_lease + .as_ref() + .context("Odin has no process-write lease")?; + let current = read_process_write_lease(&self.write_lease_path)? + .context("Odin process-write lease was withdrawn")?; + ensure!( + current == held.record && current.canonical_sha256()? == held.sha256, + "Odin process-write lease changed after admission" + ); + Ok(()) + } + + fn refresh_all_correlations(&mut self) -> Result<()> { + self.require_current_write_lease()?; + let topology = self + .topology + .as_ref() + .context("Odin topology authority is absent")?; + let mut targets = projection_targets(&self.options.idunn_projection)?; + targets.extend(correlation_targets(&self.options.store)?); + for target in targets { + topology.refresh(&target)?; + } + Ok(()) + } + + fn stored_snapshot( + &self, + query: &CultMeshRudpSnapshotQuery, + ) -> Result> { + let entries = if self.options.store.is_file() { + SingleFileMessagePackBackingStore::new(&self.options.store) + .pull_all_read_only_snapshot()? + } else { + Vec::new() + }; + let projections = CultCacheIdunnProjectionSource::new(&self.options.idunn_projection); + let mut selected = BTreeMap::new(); + for envelope in entries { + let Some(schema_id) = envelope.schema_id.clone() else { + continue; + }; + let document = if envelope.r#type == GameCultRuntimePresenceHealthRecord::TYPE { + let presence = decode_presence(&envelope.payload)?; + let Some(projection) = projections.current_projection(&presence.target)? else { + continue; + }; + if projection.expected.expected_signer_identity_id != presence.signer_identity_id + || projection.expected.canonical_sha256()? + != presence.expected_projection_sha256 + || projection + .activation + .as_ref() + .map(IdunnRuntimeActivationRecord::canonical_sha256) + .transpose()? + .as_deref() + != Some(presence.activation_witness_sha256.as_str()) + || projection + .activation + .as_ref() + .map(|activation| activation.runtime_instance_id.as_str()) + != Some(presence.runtime_instance_id.as_str()) + { + continue; + } + CultNetRawDocumentRecord { + schema_id, + record_key: presence.target.clone(), + stored_at: envelope.stored_at, + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload: envelope.payload, + source_runtime_id: Some(presence.runtime_id), + source_agent_id: Some(presence.signer_identity_id), + source_role: Some("runtime-presence-health-publisher".into()), + tags: Some(vec!["odin-observed".into()]), + } + } else if envelope.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE { + let (correlation, _) = + OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload( + &envelope.payload, + )?; + CultNetRawDocumentRecord { + schema_id, + record_key: correlation.target, + stored_at: envelope.stored_at, + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload: envelope.payload, + source_runtime_id: Some(self.authority_material.expected.runtime_id.clone()), + source_agent_id: Some(correlation.signer_identity_id), + source_role: Some("odin-topology-correlation".into()), + tags: Some(vec!["odin-owned".into()]), + } + } else { + CultNetRawDocumentRecord { + schema_id, + record_key: envelope.key, + stored_at: envelope.stored_at, + payload_encoding: CultNetRawPayloadEncoding::Messagepack, + payload: envelope.payload, + source_runtime_id: None, + source_agent_id: None, + source_role: None, + tags: None, + } + }; + if !query_allows(query, &document) { + continue; + } + let identity = (document.schema_id.clone(), document.record_key.clone()); + ensure!( + selected.insert(identity, document).is_none(), + "Odin catalog contains duplicate public document identities" + ); + } + Ok(selected.into_values().collect()) + } +} + +fn main() -> Result<()> { + let options = parse_options(std::env::args().skip(1))?; + let candidate: SocketAddr = required_environment(CANDIDATE_BIND_ENVIRONMENT)? + .parse() + .context("parsing Idunn candidate bind")?; + ensure!( + candidate.ip().is_loopback() && candidate.port() != 0, + "Odin candidate bind must be one fixed loopback socket" + ); + let socket = UdpSocket::bind(candidate) + .with_context(|| format!("binding Odin CultNet RUDP candidate {candidate}"))?; + let state = Rc::new(RefCell::new(RuntimeState::open(options, candidate)?)); + let mut server = CultMeshRudpDocumentServer::new( + socket, + SinkHandle(state.clone()), + SnapshotHandle(state.clone()), + CultMeshSystemClock::default(), + CultMeshRudpDocumentServerOptions::default(), + )?; + + let bootstrap_started = Instant::now(); + while !state.borrow_mut().try_activate()? { + let progressed = poll_server(&mut server)?; + ensure!( + bootstrap_started.elapsed() < BOOTSTRAP_LEASE_TIMEOUT, + "timed out waiting for Idunn to admit Odin's exact Warming proof and grant its lease" + ); + if !progressed { + thread::sleep(IDLE_POLL_INTERVAL); + } + } + + publish_self_presence(&state, &mut server, "ready")?; + state.borrow_mut().refresh_all_correlations()?; + let mut last_heartbeat = Instant::now(); + let mut last_projection_refresh = Instant::now(); + loop { + let progressed = poll_server(&mut server)?; + if last_projection_refresh.elapsed() >= PROJECTION_REFRESH_INTERVAL { + state.borrow_mut().refresh_all_correlations()?; + last_projection_refresh = Instant::now(); + } + if last_heartbeat.elapsed() >= HEARTBEAT_INTERVAL { + publish_self_presence(&state, &mut server, "ready")?; + last_heartbeat = Instant::now(); + } + if !progressed { + thread::sleep(IDLE_POLL_INTERVAL); + } + } +} + +fn publish_self_presence( + state: &Rc>, + server: &mut CultMeshRudpDocumentServer, + detail: &str, +) -> Result<()> { + let (message, options) = { + let mut state = state.borrow_mut(); + let message = state.active_document_put(detail)?; + let mut options = CultMeshRudpDocumentPublishOptions::odin( + state.candidate, + state.authority_material.expected.runtime_id.clone(), + ); + options.connect_timeout = Duration::from_secs(2); + options.flush_timeout = Duration::from_secs(2); + options.source_agent_id = Some( + state + .authority_material + .provider_signer + .entry() + .identity_id + .clone(), + ); + options.source_role = Some("runtime-presence-health-publisher".into()); + options.tags = vec!["cultnet.transport.rudp.v0".into()]; + (message, options) + }; + let publisher = + thread::spawn(move || publish_cultnet_message_to_rudp_catalog(&message, options)); + let started = Instant::now(); + while !publisher.is_finished() { + let progressed = poll_server(server)?; + ensure!( + started.elapsed() < SELF_PUBLISH_TIMEOUT, + "Odin self-presence publication did not complete" + ); + if !progressed { + thread::sleep(IDLE_POLL_INTERVAL); + } + } + publisher + .join() + .map_err(|_| anyhow!("Odin self-presence publisher panicked"))??; + Ok(()) +} + +fn poll_server( + server: &mut CultMeshRudpDocumentServer, +) -> Result { + match server.poll_once()? { + CultMeshRudpPollOutcome::Idle => Ok(false), + CultMeshRudpPollOutcome::Handled => Ok(true), + CultMeshRudpPollOutcome::ApplicationRejected(rejection) => { + eprintln!( + "Odin rejected CultMesh RUDP application message {:?} {} from {:?}: {}", + rejection.operation, rejection.message_id, rejection.session, rejection.reason + ); + Ok(true) + } + } +} + +fn parse_options(args: impl Iterator) -> Result { + let mut args = args.peekable(); + let mut values = BTreeMap::new(); + while let Some(name) = args.next() { + let name = name + .strip_prefix("--") + .with_context(|| format!("expected --option, got {name:?}"))?; + ensure!( + matches!(name, "store" | "idunn-projection" | "idunn-anchor"), + "unsupported Odin option --{name}" + ); + let value = args + .next() + .with_context(|| format!("missing value for --{name}"))?; + ensure!( + values + .insert(name.to_owned(), PathBuf::from(value)) + .is_none(), + "duplicate Odin option --{name}" + ); + } + let take = |name: &str| -> Result { + let path = values + .get(name) + .cloned() + .with_context(|| format!("--{name} is required"))?; + ensure!(path.is_absolute(), "--{name} must be an absolute path"); + Ok(path) + }; + let options = Options { + store: take("store")?, + idunn_projection: take("idunn-projection")?, + idunn_anchor: take("idunn-anchor")?, + }; + ensure!( + options.store != options.idunn_projection + && options.store != options.idunn_anchor + && options.idunn_projection != options.idunn_anchor, + "Odin store, Idunn projection, and Idunn anchor paths must be distinct" + ); + Ok(options) +} + +fn load_runtime_authority(bundle: &Path) -> Result { + ensure!(bundle.is_absolute(), "Idunn runtime bundle is not absolute"); + let (expected_key, expected_payload) = read_single_runtime_record( + &bundle.join("expected.cc"), + IdunnExpectedIncarnationRecord::TYPE, + IDUNN_EXPECTED_INCARNATION_SCHEMA, + )?; + let expected = IdunnExpectedIncarnationRecord::decode_canonical(&expected_payload)?; + ensure!( + expected_key == expected.target, + "Expected key is substituted" + ); + let (activation_key, activation_payload) = read_single_runtime_record( + &bundle.join("activation.cc"), + IdunnRuntimeActivationRecord::TYPE, + IDUNN_RUNTIME_ACTIVATION_SCHEMA, + )?; + let activation = IdunnRuntimeActivationRecord::decode_canonical(&activation_payload)?; + ensure!( + activation_key == expected.target, + "activation key is substituted" + ); + let expected_sha256 = expected.canonical_sha256()?; + ensure!( + activation.expected_projection_sha256 == expected_sha256 + && activation.runtime_id == expected.runtime_id, + "activation does not bind Odin's Expected projection" + ); + let (activation_credential, provider_identity) = take_runtime_signer_descriptors()?; + let activation_signer = + IdunnRuntimeActivationSigner::from_credential_reader(activation_credential)?; + ensure!( + activation_signer.identity_id() == activation.activation_signer_identity_id + && activation_signer.public_key() == activation.activation_signer_public_key, + "activation credential does not belong to Odin's activation" + ); + let provider_signer = open_service_identity_credential_reader::( + provider_identity, + )?; + ensure!( + provider_signer.entry().identity_id == expected.expected_signer_identity_id, + "provider credential is not the signer selected by Expected" + ); + Ok(RuntimeAuthority { + expected, + expected_sha256, + activation_sha256: activation.canonical_sha256()?, + activation, + activation_signer, + provider_signer, + }) +} + +fn require_expected_contract( + expected: &IdunnExpectedIncarnationRecord, + bind: SocketAddr, +) -> Result<()> { + ensure!( + expected.target == TARGET + && expected.health_contract == HEALTH_CONTRACT + && expected.state_schema_generation.as_deref() == Some(STATE_SCHEMA_GENERATION) + && expected.state_contract_sha256.as_deref() == Some(STATE_CONTRACT_SHA256) + && expected.write_lease_required, + "Odin Expected projection differs from its compiled runtime/state contract" + ); + let route = expected + .route + .as_ref() + .context("Odin Expected projection has no admitted route")?; + ensure!( + route.transport == "rudp" && route.candidate_endpoint == format!("rudp://{bind}"), + "Odin candidate bind differs from Expected" + ); + let capability = expected + .capabilities + .iter() + .find(|capability| { + capability.capability == RENDEZVOUS_CAPABILITY + && capability.schema == RENDEZVOUS_SCHEMA + && capability.compatibility == RENDEZVOUS_COMPATIBILITY + }) + .context("Odin Expected projection omits its rendezvous capability")?; + ensure!( + capability.minimum_capacity > 0 && expected.capabilities.len() == 1, + "Odin Expected capability set differs from its compiled contract" + ); + ensure!( + expected.dependencies.is_empty(), + "Odin bootstrap cannot depend on a managed daemon" + ); + Ok(()) +} + +fn read_single_runtime_record( + path: &Path, + expected_type: &str, + expected_schema: &str, +) -> Result<(String, Vec)> { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [envelope] = entries.as_slice() else { + bail!("runtime authority store must contain exactly one record"); + }; + ensure!( + envelope.r#type == expected_type && envelope.schema_id.as_deref() == Some(expected_schema), + "runtime authority store has the wrong typed envelope" + ); + Ok((envelope.key.clone(), envelope.payload.clone())) +} + +fn take_runtime_signer_descriptors() -> Result<(File, File)> { + let pid = required_environment(SYSTEMD_LISTEN_PID_ENVIRONMENT)?; + let count = required_environment(SYSTEMD_LISTEN_FDS_ENVIRONMENT)?; + let names = required_environment(SYSTEMD_LISTEN_FDNAMES_ENVIRONMENT)?; + ensure!( + pid == std::process::id().to_string(), + "systemd signer descriptors belong to another process" + ); + ensure!(count == "2", "Odin requires exactly two signer descriptors"); + ensure!( + names == format!("{ACTIVATION_SIGNER_FD_NAME}:{PROVIDER_SIGNER_FD_NAME}"), + "systemd signer descriptor names or order differ from Idunn's contract" + ); + // SAFETY: the exact systemd LISTEN_* contract above assigns sole ownership + // of descriptors 3 and 4 to this process, and this is the first FD consumer. + let activation = unsafe { File::from_raw_fd(SYSTEMD_LISTEN_FDS_START) }; + // SAFETY: descriptor 4 is distinct and covered by the same exact contract. + let provider = unsafe { File::from_raw_fd(SYSTEMD_LISTEN_FDS_START + 1) }; + Ok((activation, provider)) +} + +fn read_trust_anchor(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [envelope] = entries.as_slice() else { + bail!("service trust-anchor store must contain exactly one document"); + }; + ensure!( + envelope.r#type == P::TRUST_ANCHOR_TYPE + && envelope.key == P::TRUST_ANCHOR_KEY + && envelope.schema_id.as_deref() == Some(P::TRUST_ANCHOR_SCHEMA), + "service trust anchor belongs to another identity profile" + ); + let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&envelope.payload)?; + ensure!( + rmp_serde::to_vec(&anchor)? == envelope.payload + && anchor.schema_version == P::TRUST_ANCHOR_SCHEMA + && derive_service_identity_id::

(&anchor.public_key)? == anchor.identity_id, + "service trust anchor is noncanonical or self-inconsistent" + ); + Ok(anchor) +} + +fn acquire_process_write_lease( + path: &Path, + authority: &RuntimeAuthority, + recent_warming: &VecDeque<(String, u64)>, +) -> Result> { + let Some(observed) = read_process_write_lease(path)? else { + return Ok(None); + }; + let now = unix_millis()?; + let warming_is_ours = + recent_warming_proof(recent_warming, &observed.warming_presence_sha256, now); + let exact_incarnation = observed.target == authority.expected.target + && observed.expected_projection_sha256 == authority.expected_sha256 + && observed.plan_id == authority.expected.plan_id + && observed.incarnation_id == authority.expected.incarnation_id + && observed.sealed_release_id == authority.expected.sealed_release_id + && observed.activation_witness_sha256 == authority.activation_sha256 + && Some(observed.state_schema_generation.as_str()) + == authority.expected.state_schema_generation.as_deref() + && Some(observed.state_contract_sha256.as_str()) + == authority.expected.state_contract_sha256.as_deref() + && observed.runtime_id == authority.expected.runtime_id + && observed.runtime_instance_id == authority.activation.runtime_instance_id; + if !warming_is_ours || !exact_incarnation { + return Ok(None); + } + let lock_path = sibling_lock_path(path)?; + let lock = OpenOptions::new() + .read(true) + .open(&lock_path) + .with_context(|| format!("opening process-write-lease lock {}", lock_path.display()))?; + FileExt::lock_shared(&lock)?; + let current = read_process_write_lease(path)? + .context("process-write lease disappeared while acquiring its lifetime lock")?; + ensure!( + current == observed, + "process-write lease changed while acquiring its lifetime lock" + ); + Ok(Some(ProcessWriteLeaseGuard { + _lock: lock, + sha256: current.canonical_sha256()?, + record: current, + })) +} + +fn remember_recent_warming_proof( + recent_warming: &mut VecDeque<(String, u64)>, + sha256: String, + now: u64, +) { + while recent_warming + .front() + .is_some_and(|(_, issued)| now.saturating_sub(*issued) > WARMING_PROOF_LIFETIME_MILLIS) + { + recent_warming.pop_front(); + } + recent_warming.push_back((sha256, now)); + while recent_warming.len() > MAX_RECENT_WARMING_PROOFS { + recent_warming.pop_front(); + } +} + +fn recent_warming_proof( + recent_warming: &VecDeque<(String, u64)>, + expected_sha256: &str, + now: u64, +) -> bool { + recent_warming.iter().any(|(sha256, issued)| { + sha256 == expected_sha256 && now.saturating_sub(*issued) <= WARMING_PROOF_LIFETIME_MILLIS + }) +} + +fn read_process_write_lease(path: &Path) -> Result> { + if !path.is_file() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [envelope] = entries.as_slice() else { + ensure!(entries.is_empty(), "process-write-lease store is ambiguous"); + return Ok(None); + }; + ensure!( + envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE + && envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA), + "process-write-lease store has the wrong typed envelope" + ); + let lease = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; + ensure!( + envelope.key == lease.target, + "process-write-lease key differs from its target" + ); + Ok(Some(lease)) +} + +fn prior_self_publisher_sequence(path: &Path, signer_identity_id: &str) -> Result { + if !path.is_file() { + return Ok(0); + } + SingleFileMessagePackBackingStore::new(path) + .pull_all_read_only_snapshot()? + .into_iter() + .filter(|entry| entry.r#type == GameCultRuntimePresenceHealthRecord::TYPE) + .try_fold(0, |maximum, entry| { + let presence = decode_presence(&entry.payload)?; + Ok( + if presence.target == TARGET && presence.signer_identity_id == signer_identity_id { + maximum.max(presence.publisher_sequence) + } else { + maximum + }, + ) + }) +} + +fn persist_generic_document(path: &Path, document: &CultNetRawDocumentRecord) -> Result<()> { + let document_type = document_type_for_schema(&document.schema_id)?; + ensure!( + !matches!( + document_type.as_str(), + "gamecult.runtime_presence_health" + | "odin.runtime_topology_correlation" + | "idunn.expected_incarnation" + | "idunn.runtime_activation" + | "idunn.process_write_lease" + | "gamecult.service_trust_anchor" + ), + "generic provider traffic cannot write an authority-owned Odin/Idunn document" + ); + let replacement = CultCacheEnvelope { + key: document.record_key.clone(), + r#type: document_type.clone(), + payload: document.payload.clone(), + stored_at: document.stored_at.clone(), + schema_id: Some(document.schema_id.clone()), + }; + let store = SingleFileMessagePackBackingStore::new(path); + for _ in 0..CAS_ATTEMPTS { + let entries = if path.is_file() { + store.pull_all_read_only_snapshot()? + } else { + Vec::new() + }; + let mut matches = entries + .iter() + .filter(|entry| entry.r#type == document_type && entry.key == document.record_key); + let current = matches.next().cloned(); + ensure!( + matches.next().is_none(), + "generic CultCache identity is ambiguous" + ); + if current.as_ref() == Some(&replacement) { + return Ok(()); + } + if store.compare_exchange( + &[CultCacheExpectedEnvelope { + key: document.record_key.clone(), + r#type: document_type.clone(), + current, + }], + &[replacement.clone()], + )? { + return Ok(()); + } + } + bail!("Odin catalog changed repeatedly while persisting a provider document") +} + +fn document_type_for_schema(schema_id: &str) -> Result { + ensure!( + !schema_id.is_empty() && schema_id.trim() == schema_id, + "provider schema id is empty or padded" + ); + let Some((prefix, version)) = schema_id.rsplit_once(".v") else { + bail!("provider schema id has no explicit version suffix"); + }; + ensure!( + !prefix.is_empty() + && !version.is_empty() + && version.chars().all(|value| value.is_ascii_digit()), + "provider schema id has an invalid version suffix" + ); + Ok(prefix.into()) +} + +fn validate_raw_document_shape(document: &CultNetRawDocumentRecord) -> Result<()> { + ensure!( + document.payload_encoding == CultNetRawPayloadEncoding::Messagepack + && !document.record_key.is_empty() + && document.record_key.trim() == document.record_key + && !document.payload.is_empty(), + "raw provider document has an invalid key, encoding, or payload" + ); + document_type_for_schema(&document.schema_id)?; + DateTime::parse_from_rfc3339(&document.stored_at) + .context("raw provider document has an invalid stored_at")?; + Ok(()) +} + +fn decode_presence(payload: &[u8]) -> Result { + let presence: GameCultRuntimePresenceHealthRecord = rmp_serde::from_slice(payload)?; + ensure!( + rmp_serde::to_vec(&presence)? == payload, + "runtime presence is not canonical MessagePack" + ); + presence.validate()?; + Ok(presence) +} + +fn projection_targets(path: &Path) -> Result> { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + entries + .into_iter() + .filter(|entry| entry.r#type == IdunnExpectedIncarnationRecord::TYPE) + .map(|entry| { + ensure!( + entry.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA), + "Idunn projection contains an Expected record under the wrong schema" + ); + let expected = IdunnExpectedIncarnationRecord::decode_canonical(&entry.payload)?; + ensure!( + entry.key == expected.target, + "Idunn Expected key is substituted" + ); + Ok(expected.target) + }) + .collect() +} + +fn correlation_targets(path: &Path) -> Result> { + if !path.is_file() { + return Ok(BTreeSet::new()); + } + SingleFileMessagePackBackingStore::new(path) + .pull_all_read_only_snapshot()? + .into_iter() + .filter(|entry| entry.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE) + .map(|entry| { + ensure!( + entry.schema_id.as_deref() == Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA), + "Odin correlation is stored under the wrong schema" + ); + let (record, _) = + OdinRuntimeTopologyCorrelationRecord::decode_canonical_signed_payload( + &entry.payload, + )?; + ensure!( + entry.key == record.target, + "Odin correlation key is substituted" + ); + Ok(record.target) + }) + .collect() +} + +fn validate_snapshot_filters(query: &CultMeshRudpSnapshotQuery) -> Result<()> { + ensure!( + !query.message_id.is_empty() && query.message_id.trim() == query.message_id, + "snapshot message id is empty or padded" + ); + for (label, values) in [ + ("schema", query.schema_ids.as_ref()), + ("record key", query.record_keys.as_ref()), + ] { + if let Some(values) = values { + ensure!(!values.is_empty(), "snapshot {label} filter is empty"); + let unique = values.iter().collect::>(); + ensure!( + unique.len() == values.len() + && values + .iter() + .all(|value| !value.is_empty() && value.trim() == value), + "snapshot {label} filter is duplicated, empty, or padded" + ); + } + } + Ok(()) +} + +fn exact_self_presence_query(query: &CultMeshRudpSnapshotQuery) -> bool { + query.schema_ids.as_deref() == Some(&[GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.to_owned()]) + && query.record_keys.as_deref() == Some(&[TARGET.to_owned()]) +} + +fn query_allows(query: &CultMeshRudpSnapshotQuery, document: &CultNetRawDocumentRecord) -> bool { + query + .schema_ids + .as_ref() + .is_none_or(|values| values.contains(&document.schema_id)) + && query + .record_keys + .as_ref() + .is_none_or(|values| values.contains(&document.record_key)) +} + +fn sibling_lock_path(path: &Path) -> Result { + let mut name = path + .file_name() + .context("CultCache authority path has no filename")? + .to_os_string(); + name.push(".lock"); + Ok(path.with_file_name(name)) +} + +fn required_environment(name: &str) -> Result { + std::env::var(name) + .ok() + .filter(|value| !value.is_empty() && value.trim() == value) + .with_context(|| format!("{name} is required for an Idunn-managed Odin")) +} + +fn unix_millis() -> Result { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .context("system clock predates Unix epoch")? + .as_millis() + .try_into() + .context("Unix time exceeds u64") +} + +fn rfc3339_millis(value: u64) -> Result { + let millis: i64 = value + .try_into() + .context("timestamp exceeds RFC3339 range")?; + Ok(DateTime::::from_timestamp_millis(millis) + .context("timestamp exceeds RFC3339 range")? + .to_rfc3339()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn options_are_exact_and_absolute() { + let parsed = parse_options( + [ + "--store", + "/var/lib/gamecult/odin-v2/topology.cc", + "--idunn-projection", + "/var/lib/gamecult/idunn-projection/topology.cc", + "--idunn-anchor", + "/etc/gamecult/idunn/idunn-public-anchor.cc", + ] + .into_iter() + .map(str::to_owned), + ) + .unwrap(); + assert_eq!( + parsed.store, + Path::new("/var/lib/gamecult/odin-v2/topology.cc") + ); + assert!( + parse_options( + [ + "--store", + "relative", + "--idunn-projection", + "/p", + "--idunn-anchor", + "/a" + ] + .into_iter() + .map(str::to_owned) + ) + .is_err() + ); + } + + #[test] + fn schema_to_document_type_is_versioned_and_deterministic() { + assert_eq!( + document_type_for_schema("heimdall.command_boundary.v1").unwrap(), + "heimdall.command_boundary" + ); + for value in ["heimdall.command_boundary", "heimdall.v", ".v1", " x.v1"] { + assert!(document_type_for_schema(value).is_err()); + } + } + + #[test] + fn only_exact_singleton_self_presence_query_is_a_route_challenge() { + let exact = CultMeshRudpSnapshotQuery { + session: cultmesh_rs::CultMeshRudpSessionKey { + remote_addr: "127.0.0.1:1".parse().unwrap(), + connection_id: 7, + }, + message_id: "challenge".into(), + requested_at_unix_millis: 1, + schema_ids: Some(vec![GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()]), + record_keys: Some(vec![TARGET.into()]), + }; + assert!(exact_self_presence_query(&exact)); + let mut broad = exact.clone(); + broad.record_keys = None; + assert!(!exact_self_presence_query(&broad)); + } + + #[test] + fn first_odin_accepts_only_recent_provider_owned_warming_proofs() { + let mut proofs = VecDeque::new(); + remember_recent_warming_proof(&mut proofs, "warming-one".into(), 100); + assert!(recent_warming_proof( + &proofs, + "warming-one", + 100 + WARMING_PROOF_LIFETIME_MILLIS, + )); + assert!(!recent_warming_proof( + &proofs, + "warming-one", + 101 + WARMING_PROOF_LIFETIME_MILLIS, + )); + assert!(!recent_warming_proof(&proofs, "foreign-proof", 100)); + + for index in 0..=MAX_RECENT_WARMING_PROOFS { + remember_recent_warming_proof(&mut proofs, format!("warming-{index}"), 200); + } + assert_eq!(proofs.len(), MAX_RECENT_WARMING_PROOFS); + assert_eq!(proofs.back().unwrap().0, "warming-64"); + assert!(!proofs.iter().any(|(proof, _)| proof == "warming-one")); + } +} diff --git a/deployment/idunn/recipe.toml b/deployment/idunn/recipe.toml new file mode 100644 index 0000000..f4455df --- /dev/null +++ b/deployment/idunn/recipe.toml @@ -0,0 +1,72 @@ +schema = "gamecult.idunn.target_declaration.v1" +target = "odin" +source_stamp_environment = "ODIN_BUILD_COMMIT" +required_gitlinks = [] + +[[steps]] +id = "test-odin-daemon" +phase = "test" +runner = "rust-build" +argv = ["cargo", "test", "--locked", "-p", "odin-daemon"] + +[[steps]] +id = "build-odin-daemon" +phase = "build" +runner = "rust-build" +argv = [ + "cargo", + "build", + "--locked", + "--release", + "-p", + "odin-daemon", + "--bin", + "odin-daemon", +] + +[[artifacts]] +id = "odin-daemon" +source_kind = "runner-output" +runner = "rust-build" +source = "target/release/odin-daemon" +destination = "odin-daemon" +executable = true + +[service] +executable_artifact = "odin-daemon" +arguments = [ + { kind = "literal", value = "--store" }, + { kind = "binding", name = "store_path" }, + { kind = "literal", value = "--idunn-projection" }, + { kind = "binding", name = "idunn_projection_path" }, + { kind = "literal", value = "--idunn-anchor" }, + { kind = "binding", name = "idunn_anchor_path" }, +] +transport = "rudp" +route_required = true +required_environment = [ + "GAMECULT_IDUNN_CANDIDATE_BIND", + "GAMECULT_IDUNN_PROCESS_WRITE_LEASE", + "GAMECULT_IDUNN_RUNTIME_BUNDLE", + "ODIN_TOPOLOGY_IDENTITY", +] + +[service.health] +contract = "odin.runtime-health.v1" + +[state] +schema_generation = "odin-v2" + +[[state.slots]] +id = "topology" +relative_path = "topology.cc" +kind = "cultcache-file" +schema = "odin.runtime_topology_correlation.v2" +writer = "process-bound-single-writer" +recovery = "preserve" +startup = "create-or-open-after-write-lease" + +[[provides]] +capability = "odin.verse-rendezvous" +schema = "odin.verse-topology.v1" +compatibility = "v1" From 3d939a85234dd2fb8a3bf2e6dcc2a4cc90dabd9b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 4 Sep 2026 15:28:32 +0200 Subject: [PATCH 89/93] Apply rustfmt The drift was 94 hunks across 4 files, not 94 files as I described it earlier. muninn-daemon carries almost all of it; sleipnir-daemon needed four lines. Checked rather than assumed. Ignoring whitespace, the only remaining deltas are trailing commas rustfmt adds when it wraps an argument list, and braces removed from single-expression closure bodies. Both are semantics-preserving. sleipnir-daemon's change is whitespace and nothing else. Everything except the Unix-only odin-daemon builds; muninn and sleipnir pass 162 tests, with the same pre-existing media-channel failure. vendor/cultmesh-rs is its own workspace so `cargo fmt` does not reach it, but it reports no drift of its own. Co-Authored-By: Claude Opus 5 --- crates/muninn-daemon/src/main.rs | 914 +++++++++++++----- crates/muninn-psmoveapi-tracker/src/lib.rs | 53 +- .../examples/dump_move_tracker_health.rs | 25 +- crates/sleipnir-daemon/src/main.rs | 4 +- 4 files changed, 727 insertions(+), 269 deletions(-) diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index 11e422a..6fed050 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -7,31 +7,28 @@ use crate::media_packetizer::{ }; use anyhow::{Context, Result, anyhow}; use cultmesh_rs::{ - CultMesh, CultMeshNodeOptions, - CultMeshRudpDocumentPublishOptions, CultMeshRudpSnapshotOptions, CultMeshSharedMemoryFrameRing, - CultMeshStreamBodyTransport, CultMeshStreamCatalog, CultMeshStreamClock, - CultMeshStreamDescriptor, CultMeshStreamKind, + CultMesh, CultMeshNodeOptions, CultMeshRudpDocumentPublishOptions, CultMeshRudpSnapshotOptions, + CultMeshSharedMemoryFrameRing, CultMeshStreamBodyTransport, CultMeshStreamCatalog, + CultMeshStreamClock, CultMeshStreamDescriptor, CultMeshStreamKind, }; use cultnet_rs::{ CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetTransportFrame, CultNetWireContract, decode_cultnet_message_from_slice, encode_cultnet_message_to_vec, }; +#[cfg(feature = "psmoveapi-tracker")] +use odin_core::MuninnMoveTrackerHealthRecord; use odin_core::{ EVE_PROVIDER_ADVERTISEMENT_SCHEMA, EveProviderAdvertisementRecord, EveSurfaceStateRecord, - IdunnDaemonHealthRecord, - MUNINN_MOVE_HUE_PROGRAM_SCHEMA, - MUNINN_OBS_STREAM_CATALOG_SCHEMA, + IdunnDaemonHealthRecord, MUNINN_MOVE_HUE_PROGRAM_SCHEMA, MUNINN_OBS_STREAM_CATALOG_SCHEMA, MuninnCaptureStreamCommandRecord, MuninnCaptureStreamRecord, MuninnCommandBoundaryCompatRecord, MuninnHidControllerStateRecord, MuninnMediaReceiverFeedbackRecord, - MuninnMoveControllerStateRecord, MuninnMoveEvidenceTransportHealthRecord, MuninnMoveHueProgramRecord, MuninnMoveIdentityRecord, - MuninnMoveLightCommandRecord, + MuninnMoveControllerStateRecord, MuninnMoveEvidenceTransportHealthRecord, + MuninnMoveHueProgramRecord, MuninnMoveIdentityRecord, MuninnMoveLightCommandRecord, MuninnMoveMarkerCandidateRecord, MuninnObsStreamCatalogRecord, MuninnQuestAccessRecord, MuninnTelemetrySurfaceRecord, MuninnTransportProfileCompatRecord, OdinDocuments, OdinEndpointQuery, discover_provider_endpoints, }; -#[cfg(feature = "psmoveapi-tracker")] -use odin_core::MuninnMoveTrackerHealthRecord; use serde::{Deserialize, Serialize}; use serde_json::json; use std::collections::{HashMap, HashSet, VecDeque}; @@ -46,7 +43,11 @@ use std::net::{SocketAddr, UdpSocket}; use std::path::Path; use std::path::PathBuf; use std::process::{Child, Command, Stdio}; -use std::sync::{Arc, Mutex, atomic::{AtomicU64, Ordering}, mpsc}; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicU64, Ordering}, + mpsc, +}; use std::thread; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; @@ -316,7 +317,8 @@ struct ActiveMoveMarkerCameraSource { frame_source: MoveMarkerFrameSource, sequence: u64, #[cfg(feature = "psmoveapi-tracker")] - psmoveapi_observations: Option>>>>, + psmoveapi_observations: + Option>>>>, #[cfg(feature = "psmoveapi-tracker")] psmoveapi_health: Option>>>, } @@ -409,17 +411,20 @@ fn serve(options: Options) -> Result<()> { let mut active_capture_streams = Vec::new(); let move_evidence_rudp_sender = start_hid_controller_rudp_ingress( &options, - move_evidence_stream.as_ref().map(|stream| Arc::clone(&stream.counters)), + move_evidence_stream + .as_ref() + .map(|stream| Arc::clone(&stream.counters)), )?; if let Some(stream) = move_evidence_stream.as_mut() { stream.rudp_sender = move_evidence_rudp_sender; } - let move_evidence_transport_health = move_evidence_stream.as_ref().map(|stream| { - MoveEvidenceTransportHealth { - stream_id: stream.stream_id.clone(), - counters: Arc::clone(&stream.counters), - } - }); + let move_evidence_transport_health = + move_evidence_stream + .as_ref() + .map(|stream| MoveEvidenceTransportHealth { + stream_id: stream.stream_id.clone(), + counters: Arc::clone(&stream.counters), + }); let latest_move_controller_states = Arc::new(Mutex::new(Vec::new())); if let Some(stream) = move_evidence_stream.take() { start_move_evidence_aggregator( @@ -470,11 +475,10 @@ fn serve(options: Options) -> Result<()> { None, hid_controller_stream.as_mut(), )?; - let controller_states: Vec = - active_move_states - .iter() - .filter_map(|state| state.latest_move_record.clone()) - .collect(); + let controller_states: Vec = active_move_states + .iter() + .filter_map(|state| state.latest_move_record.clone()) + .collect(); if let Ok(mut latest) = latest_move_controller_states.lock() { *latest = controller_states; } @@ -557,9 +561,9 @@ fn load_or_initialize_move_hue_program( ) -> Result>> { let key = move_hue_program_key(&options.host_id); let mut node = open_node(options, "muninn-move-hue-program-bootstrap")?; - let program = node.get::(&key)?.unwrap_or_else(|| { - bootstrap_move_hue_program(options) - }); + let program = node + .get::(&key)? + .unwrap_or_else(|| bootstrap_move_hue_program(options)); validate_move_hue_program(&program)?; node.put(&key, &program)?; Ok(Arc::new(Mutex::new(program))) @@ -592,9 +596,14 @@ fn validate_move_hue_program(program: &MuninnMoveHueProgramRecord) -> Result<()> program.order_mode.as_str(), "descending" | "ascending" | "bounce" | "rotating-lead" | "golden-permutation" ) { - return Err(anyhow!("unsupported Move hue order mode {}", program.order_mode)); + return Err(anyhow!( + "unsupported Move hue order mode {}", + program.order_mode + )); + } + if program.transition_percent > 100 { + return Err(anyhow!("Move hue transition_percent must be at most 100")); } - if program.transition_percent > 100 { return Err(anyhow!("Move hue transition_percent must be at most 100")); } Ok(()) } @@ -607,7 +616,11 @@ fn move_hue_program_timestamp_ns(program: &MuninnMoveHueProgramRecord, now_ns: i } fn effective_transition_percent(program: &MuninnMoveHueProgramRecord) -> u8 { - if program.transition_percent_explicit { program.transition_percent } else { 100 } + if program.transition_percent_explicit { + program.transition_percent + } else { + 100 + } } fn start_move_hue_program_sync_worker( @@ -621,32 +634,34 @@ fn start_move_hue_program_sync_worker( .as_ref() .cloned() .unwrap_or_else(|| options.store_path.clone()); - thread::spawn(move || loop { - let key = move_hue_program_key(&options.host_id); - let main_program = open_node(&options, "muninn-move-hue-program-sync") + thread::spawn(move || { + loop { + let key = move_hue_program_key(&options.host_id); + let main_program = open_node(&options, "muninn-move-hue-program-sync") + .ok() + .and_then(|node| node.get::(&key).ok().flatten()); + let activation_program = open_node( + &activation_options, + "muninn-move-hue-program-activation-sync", + ) .ok() .and_then(|node| node.get::(&key).ok().flatten()); - let activation_program = open_node( - &activation_options, - "muninn-move-hue-program-activation-sync", - ) - .ok() - .and_then(|node| node.get::(&key).ok().flatten()); - let program = [main_program, activation_program] - .into_iter() - .flatten() - .filter(|program| validate_move_hue_program(program).is_ok()) - .max_by_key(|program| unix_timestamp_sort_key(&program.updated_at)); - if let Some(program) = program - && let Ok(mut current) = runtime_program.lock() - && *current != program - { - *current = program.clone(); - if let Ok(mut node) = open_node(&options, "muninn-move-hue-program-projection") { - let _ = node.put(&key, &program); + let program = [main_program, activation_program] + .into_iter() + .flatten() + .filter(|program| validate_move_hue_program(program).is_ok()) + .max_by_key(|program| unix_timestamp_sort_key(&program.updated_at)); + if let Some(program) = program + && let Ok(mut current) = runtime_program.lock() + && *current != program + { + *current = program.clone(); + if let Ok(mut node) = open_node(&options, "muninn-move-hue-program-projection") { + let _ = node.put(&key, &program); + } } + thread::sleep(Duration::from_millis(250)); } - thread::sleep(Duration::from_millis(250)); }); } @@ -674,8 +689,8 @@ fn run_provider_command_ingress( options: Options, runtime_program: Arc>, ) -> Result<()> { - let mut transport = CultNetRudpSocketTransportConnection::new( - CultNetRudpSocketTransportOptions { + let mut transport = + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { runtime_id: format!("muninn-{}-command-ingress", options.host_id), socket, mode: cultnet_rs::CultNetRudpSocketMode::Server, @@ -688,9 +703,8 @@ fn run_provider_command_ingress( max_fragment_bytes: Some(1200), max_pending_reliable_packets: Some(64), media_reliable_expire_after_ms: None, - reconnect_policy: None, - }, - )?; + reconnect_policy: None, + })?; loop { if let Some(frame) = transport.receive_once()? && frame.channel_id == "schema" @@ -735,7 +749,10 @@ fn apply_provider_command_document( .and_then(serde_json::Value::as_str) .unwrap_or_default(); if provider_id != muninn_provider_id(options) { - return Err(anyhow!("Move hue command targets provider {provider_id}, not {}", muninn_provider_id(options))); + return Err(anyhow!( + "Move hue command targets provider {provider_id}, not {}", + muninn_provider_id(options) + )); } let mut program = runtime_program .lock() @@ -753,8 +770,12 @@ fn apply_provider_command_document( if let Some(cycle_ms) = payload.get("cycleMs").and_then(serde_json::Value::as_u64) { program.cycle_ms = cycle_ms; } - if let Some(value) = payload.get("transitionPercent").and_then(serde_json::Value::as_u64) { - program.transition_percent = u8::try_from(value).map_err(|_| anyhow!("Move hue transitionPercent must be at most 100"))?; + if let Some(value) = payload + .get("transitionPercent") + .and_then(serde_json::Value::as_u64) + { + program.transition_percent = u8::try_from(value) + .map_err(|_| anyhow!("Move hue transitionPercent must be at most 100"))?; program.transition_percent_explicit = true; } program.requested_by = value @@ -1519,9 +1540,10 @@ fn merge_move_state_sources( continue; } } - if !sources.iter().any(|discovered| { - discovered == source || discovered.move_id == source.move_id - }) { + if !sources + .iter() + .any(|discovered| discovered == source || discovered.move_id == source.move_id) + { sources.push(source.clone()); } } @@ -3032,7 +3054,12 @@ fn publish_move_hue_eve_surface( .unwrap_or_else(|| bootstrap_move_hue_program(options)); let provider_id = muninn_provider_id(options); let surface_key = move_hue_surface_key(&options.host_id); - let action = |id: &str, label: &str, mode: Option<&str>, order_mode: Option<&str>, cycle_ms: Option, transition_percent: Option| { + let action = |id: &str, + label: &str, + mode: Option<&str>, + order_mode: Option<&str>, + cycle_ms: Option, + transition_percent: Option| { json!({ "id": format!("{provider_id}.move-hue.{id}"), "kind": "card", @@ -3816,7 +3843,10 @@ fn publish_move_evidence_stream_frame( let published_at_ns = timestamp_ns()?; let frame_id = format!("{}:{}", stream.stream_id, stream.frame_counter); stream.frame_counter = stream.frame_counter.saturating_add(1); - stream.counters.produced_frames.fetch_add(1, Ordering::Relaxed); + stream + .counters + .produced_frames + .fetch_add(1, Ordering::Relaxed); let frame = MuninnMoveEvidenceStreamFrame( &frame_id, &stream.producer_peer_id, @@ -3827,8 +3857,13 @@ fn publish_move_evidence_stream_frame( let payload = rmp_serde::to_vec(&frame).context("encoding Muninn Move evidence stream frame")?; if let Some(sender) = stream.rudp_sender.as_ref() { - if let Ok(mut latest) = sender.lock() { *latest = Some(payload.clone()); } - stream.counters.remote_handoffs.fetch_add(1, Ordering::Relaxed); + if let Ok(mut latest) = sender.lock() { + *latest = Some(payload.clone()); + } + stream + .counters + .remote_handoffs + .fetch_add(1, Ordering::Relaxed); } if let Some(path) = stream.snapshot_path.as_deref() { write_move_evidence_snapshot( @@ -3841,15 +3876,20 @@ fn publish_move_evidence_stream_frame( )?; } let handle = if stream.local_ring_enabled { - let ring: &mut CultMeshSharedMemoryFrameRing = stream.catalog - .ring_mut(&stream.stream_id) - .ok_or_else(|| anyhow!("missing Muninn Move evidence ring"))?; + let ring: &mut CultMeshSharedMemoryFrameRing = + stream + .catalog + .ring_mut(&stream.stream_id) + .ok_or_else(|| anyhow!("missing Muninn Move evidence ring"))?; ring.try_publish_copy(&payload, published_at_ns, 0)? } else { None }; if let Some(handle) = handle { - stream.counters.local_ring_admissions.fetch_add(1, Ordering::Relaxed); + stream + .counters + .local_ring_admissions + .fetch_add(1, Ordering::Relaxed); stream.catalog.publish_frame(handle.clone())?; Ok(Some(handle)) } else { @@ -4699,18 +4739,27 @@ fn active_move_marker_camera_sources( ); #[cfg(feature = "psmoveapi-tracker")] let (psmoveapi_observations, psmoveapi_health) = if options.move_psmoveapi_tracker { - video_device_index(&source.device_path).and_then(|camera_index| { - Some(start_psmoveapi_tracker_worker( - options.store_path.clone(), - options.host_id.clone(), - source.camera_id.clone(), - camera_index, - options.move_tracker_camera_exposure_milli.get(&source.camera_id) - .copied().unwrap_or(options.move_tracker_exposure_milli) as f32 / 1000.0, - serve_move_state_sources(options, true), - Arc::clone(&_move_hue_program), - )) - }).map_or((None, None), |(observations, health)| (Some(observations), Some(health))) + video_device_index(&source.device_path) + .and_then(|camera_index| { + Some(start_psmoveapi_tracker_worker( + options.store_path.clone(), + options.host_id.clone(), + source.camera_id.clone(), + camera_index, + options + .move_tracker_camera_exposure_milli + .get(&source.camera_id) + .copied() + .unwrap_or(options.move_tracker_exposure_milli) + as f32 + / 1000.0, + serve_move_state_sources(options, true), + Arc::clone(&_move_hue_program), + )) + }) + .map_or((None, None), |(observations, health)| { + (Some(observations), Some(health)) + }) } else { (None, None) }; @@ -4821,7 +4870,11 @@ fn publish_move_marker_camera_frames( #[cfg(feature = "psmoveapi-tracker")] if let Some(latest) = camera.psmoveapi_observations.as_ref() { let observed_at = timestamp()?; - let observations = latest.lock().ok().and_then(|mut value| value.take()).unwrap_or_default(); + let observations = latest + .lock() + .ok() + .and_then(|mut value| value.take()) + .unwrap_or_default(); marker_candidates.extend(observations.into_iter().map(|observation| { let radius = observation.radius_px.max(0.0); MuninnMoveMarkerCandidateRecord { @@ -4875,52 +4928,94 @@ fn start_psmoveapi_tracker_worker( exposure: f32, move_state_sources: Vec, move_hue_program: Arc>, -) -> (Arc>>>, Arc>>) { +) -> ( + Arc>>>, + Arc>>, +) { let observations_latest = Arc::new(Mutex::new(None)); let health_latest = Arc::new(Mutex::new(None)); let observations_output = Arc::clone(&observations_latest); let health_output = Arc::clone(&health_latest); thread::spawn(move || { - let mut command = Command::new(env::current_exe().unwrap_or_else(|_| PathBuf::from("muninn"))); - command.arg("move-tracker-worker") - .arg("--store").arg(store_path) - .arg("--host").arg(&host_id) - .arg("--move-marker-camera").arg(format!("{camera_id}=/dev/video{camera_index}")) - .arg("--move-tracker-exposure-milli").arg(((exposure * 1000.0).round() as u32).to_string()); + let mut command = + Command::new(env::current_exe().unwrap_or_else(|_| PathBuf::from("muninn"))); + command + .arg("move-tracker-worker") + .arg("--store") + .arg(store_path) + .arg("--host") + .arg(&host_id) + .arg("--move-marker-camera") + .arg(format!("{camera_id}=/dev/video{camera_index}")) + .arg("--move-tracker-exposure-milli") + .arg(((exposure * 1000.0).round() as u32).to_string()); for source in move_state_sources { - command.arg("--move-state").arg(format!("{}={}", source.move_id, source.hidraw_path)); + command + .arg("--move-state") + .arg(format!("{}={}", source.move_id, source.hidraw_path)); } - let child = command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::inherit()).spawn(); - let Ok(mut child) = child else { return; }; - let Some(mut stdin) = child.stdin.take() else { return; }; + let child = command + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::inherit()) + .spawn(); + let Ok(mut child) = child else { + return; + }; + let Some(mut stdin) = child.stdin.take() else { + return; + }; let program_source = Arc::clone(&move_hue_program); thread::spawn(move || { let mut previous = None; loop { - let Some(program) = program_source.lock().ok().map(|value| MoveTrackerWorkerProgram::from(&*value)) else { return; }; + let Some(program) = program_source + .lock() + .ok() + .map(|value| MoveTrackerWorkerProgram::from(&*value)) + else { + return; + }; if previous.as_ref() != Some(&program) { - if write_length_framed_message(&mut stdin, &program).is_err() { return; } + if write_length_framed_message(&mut stdin, &program).is_err() { + return; + } previous = Some(program); } thread::sleep(Duration::from_millis(100)); } }); - let Some(stdout) = child.stdout.take() else { return; }; + let Some(stdout) = child.stdout.take() else { + return; + }; let mut reader = BufReader::new(stdout); loop { let frame = match read_move_tracker_worker_frame(&mut reader) { Ok(frame) => frame, Err(error) => { - eprintln!("Muninn Move tracker worker stream stopped camera={camera_id}: {error:#}"); + eprintln!( + "Muninn Move tracker worker stream stopped camera={camera_id}: {error:#}" + ); break; } }; - let observations = frame.observations.into_iter().map(|value| muninn_psmoveapi_tracker::PsmoveApiObservation { - move_id: value.move_id, center_x_px: value.center_x_px, center_y_px: value.center_y_px, - radius_px: value.radius_px, age_ms: value.age_ms, - }).collect(); - if let Ok(mut latest) = health_output.lock() { *latest = Some(frame.health); } - if let Ok(mut latest) = observations_output.lock() { *latest = Some(observations); } + let observations = frame + .observations + .into_iter() + .map(|value| muninn_psmoveapi_tracker::PsmoveApiObservation { + move_id: value.move_id, + center_x_px: value.center_x_px, + center_y_px: value.center_y_px, + radius_px: value.radius_px, + age_ms: value.age_ms, + }) + .collect(); + if let Ok(mut latest) = health_output.lock() { + *latest = Some(frame.health); + } + if let Ok(mut latest) = observations_output.lock() { + *latest = Some(observations); + } } let _ = child.wait(); }); @@ -4937,14 +5032,19 @@ fn read_length_framed_message Deserialize<'de>>(reader: &mut impl Re let mut length = [0u8; 4]; reader.read_exact(&mut length)?; let length = u32::from_le_bytes(length) as usize; - if length > 1024 * 1024 { return Err(anyhow!("Move tracker worker frame exceeds 1 MiB")); } + if length > 1024 * 1024 { + return Err(anyhow!("Move tracker worker frame exceeds 1 MiB")); + } let mut payload = vec![0u8; length]; reader.read_exact(&mut payload)?; rmp_serde::from_slice(&payload).context("decoding length-framed MessagePack") } #[cfg(feature = "psmoveapi-tracker")] -fn write_move_tracker_worker_frame(writer: &mut impl Write, frame: &MoveTrackerWorkerFrame) -> Result<()> { +fn write_move_tracker_worker_frame( + writer: &mut impl Write, + frame: &MoveTrackerWorkerFrame, +) -> Result<()> { write_length_framed_message(writer, frame) } @@ -4960,33 +5060,52 @@ fn write_length_framed_message(writer: &mut impl Write, value: &impl Serialize) #[cfg(feature = "psmoveapi-tracker")] impl From<&MuninnMoveHueProgramRecord> for MoveTrackerWorkerProgram { fn from(value: &MuninnMoveHueProgramRecord) -> Self { - Self { mode: value.mode.clone(), cycle_ms: value.cycle_ms, epoch_ns: value.epoch_ns, - hold_at_ns: value.hold_at_ns, order_mode: value.order_mode.clone(), transition_percent: value.transition_percent, - transition_percent_explicit: value.transition_percent_explicit } + Self { + mode: value.mode.clone(), + cycle_ms: value.cycle_ms, + epoch_ns: value.epoch_ns, + hold_at_ns: value.hold_at_ns, + order_mode: value.order_mode.clone(), + transition_percent: value.transition_percent, + transition_percent_explicit: value.transition_percent_explicit, + } } } #[cfg(feature = "psmoveapi-tracker")] impl MoveTrackerWorkerProgram { fn as_record(&self, host_id: &str) -> MuninnMoveHueProgramRecord { - MuninnMoveHueProgramRecord { program_id: move_hue_program_key(host_id), host_id: host_id.to_string(), - mode: self.mode.clone(), cycle_ms: self.cycle_ms, epoch_ns: self.epoch_ns, hold_at_ns: self.hold_at_ns, - requested_by: "parent-worker-pipe".to_string(), updated_at: "worker-live".to_string(), order_mode: self.order_mode.clone(), - transition_percent: self.transition_percent, transition_percent_explicit: self.transition_percent_explicit } + MuninnMoveHueProgramRecord { + program_id: move_hue_program_key(host_id), + host_id: host_id.to_string(), + mode: self.mode.clone(), + cycle_ms: self.cycle_ms, + epoch_ns: self.epoch_ns, + hold_at_ns: self.hold_at_ns, + requested_by: "parent-worker-pipe".to_string(), + updated_at: "worker-live".to_string(), + order_mode: self.order_mode.clone(), + transition_percent: self.transition_percent, + transition_percent_explicit: self.transition_percent_explicit, + } } } #[cfg(feature = "psmoveapi-tracker")] fn run_move_tracker_worker(options: Options) -> Result<()> { - let source = options.move_marker_camera_sources.first() + let source = options + .move_marker_camera_sources + .first() .context("move-tracker-worker requires one --move-marker-camera")?; if options.move_marker_camera_sources.len() != 1 { return Err(anyhow!("move-tracker-worker accepts exactly one camera")); } let camera_index = video_device_index(&source.device_path) .context("move-tracker-worker camera must be /dev/videoN")?; - let mut roster = serve_move_state_sources(&options, true).into_iter() - .map(|source| source.move_id).collect::>(); + let mut roster = serve_move_state_sources(&options, true) + .into_iter() + .map(|source| source.move_id) + .collect::>(); roster.sort(); roster.dedup(); let mut input = BufReader::new(std::io::stdin()); @@ -5003,7 +5122,8 @@ fn run_move_tracker_worker(options: Options) -> Result<()> { }); let exposure = options.move_tracker_exposure_milli as f32 / 1000.0; let colors = tracker_colors(&roster, &program); - let mut tracker = muninn_psmoveapi_tracker::PsmoveApiTracker::open(camera_index, exposure, &colors)?; + let mut tracker = + muninn_psmoveapi_tracker::PsmoveApiTracker::open(camera_index, exposure, &colors)?; let camera_info = tracker.camera_info().clone(); let mut update_count = 0u64; let mut observation_count = 0u64; @@ -5022,7 +5142,9 @@ fn run_move_tracker_worker(options: Options) -> Result<()> { let stdout = std::io::stdout(); let mut writer = stdout.lock(); loop { - while let Ok(latest) = program_receiver.try_recv() { program = latest.as_record(&options.host_id); } + while let Ok(latest) = program_receiver.try_recv() { + program = latest.as_record(&options.host_id); + } if last_calibration.elapsed() >= Duration::from_secs(30) { tracker.observe_connected(); last_calibration = Instant::now(); @@ -5032,45 +5154,98 @@ fn run_move_tracker_worker(options: Options) -> Result<()> { tracker.set_expected_color(identity, *color); } let observed_now = Instant::now(); - let observations = tracker.update().into_iter().filter(|observation| { - match admit_psmoveapi_observation(observation, camera_info.width, camera_info.height, - admitted.get(&observation.move_id), observed_now) { - Ok(next) => { admitted.insert(observation.move_id.clone(), next); true } - Err(PsmoveApiObservationRejection::Stale) => { rejected_stale_count = rejected_stale_count.saturating_add(1); false } - Err(PsmoveApiObservationRejection::Radius) => { rejected_radius_count = rejected_radius_count.saturating_add(1); false } - Err(PsmoveApiObservationRejection::Bounds) => { rejected_bounds_count = rejected_bounds_count.saturating_add(1); false } - Err(PsmoveApiObservationRejection::Continuity) => { rejected_continuity_count = rejected_continuity_count.saturating_add(1); false } - } - }).collect::>(); + let observations = tracker + .update() + .into_iter() + .filter(|observation| { + match admit_psmoveapi_observation( + observation, + camera_info.width, + camera_info.height, + admitted.get(&observation.move_id), + observed_now, + ) { + Ok(next) => { + admitted.insert(observation.move_id.clone(), next); + true + } + Err(PsmoveApiObservationRejection::Stale) => { + rejected_stale_count = rejected_stale_count.saturating_add(1); + false + } + Err(PsmoveApiObservationRejection::Radius) => { + rejected_radius_count = rejected_radius_count.saturating_add(1); + false + } + Err(PsmoveApiObservationRejection::Bounds) => { + rejected_bounds_count = rejected_bounds_count.saturating_add(1); + false + } + Err(PsmoveApiObservationRejection::Continuity) => { + rejected_continuity_count = rejected_continuity_count.saturating_add(1); + false + } + } + }) + .collect::>(); update_count = update_count.saturating_add(1); observation_count = observation_count.saturating_add(observations.len() as u64); - if !observations.is_empty() { last_observation_at = timestamp()?; } + if !observations.is_empty() { + last_observation_at = timestamp()?; + } if last_image_evidence.elapsed() >= Duration::from_millis(250) { if let Some((_, _, rgb)) = tracker.rgb_image() { - (image_mean_rgb, image_peak_rgb, color_evidence_move_ids, color_evidence_pixel_counts) = - summarize_tracker_rgb(&rgb, &expected_colors); + ( + image_mean_rgb, + image_peak_rgb, + color_evidence_move_ids, + color_evidence_pixel_counts, + ) = summarize_tracker_rgb(&rgb, &expected_colors); } last_image_evidence = Instant::now(); } let health = MuninnMoveTrackerHealthRecord { - health_id: format!("muninn:{}:{}:move-tracker-health", options.host_id, source.camera_id), - host_id: options.host_id.clone(), camera_id: source.camera_id.clone(), camera_index, - state: "running".to_string(), camera_name: camera_info.name.clone(), camera_api: camera_info.api.clone(), - width: camera_info.width, height: camera_info.height, exposure: camera_info.exposure, - calibrated_controller_count: tracker.tracked_controller_count() as u32, update_count, observation_count, - latest_observation_count: observations.len() as u32, last_observation_at: last_observation_at.clone(), - detail: "private subprocess worker".to_string(), updated_at: timestamp()?, - image_mean_rgb: image_mean_rgb.clone(), image_peak_rgb: image_peak_rgb.clone(), + health_id: format!( + "muninn:{}:{}:move-tracker-health", + options.host_id, source.camera_id + ), + host_id: options.host_id.clone(), + camera_id: source.camera_id.clone(), + camera_index, + state: "running".to_string(), + camera_name: camera_info.name.clone(), + camera_api: camera_info.api.clone(), + width: camera_info.width, + height: camera_info.height, + exposure: camera_info.exposure, + calibrated_controller_count: tracker.tracked_controller_count() as u32, + update_count, + observation_count, + latest_observation_count: observations.len() as u32, + last_observation_at: last_observation_at.clone(), + detail: "private subprocess worker".to_string(), + updated_at: timestamp()?, + image_mean_rgb: image_mean_rgb.clone(), + image_peak_rgb: image_peak_rgb.clone(), color_evidence_move_ids: color_evidence_move_ids.clone(), color_evidence_pixel_counts: color_evidence_pixel_counts.clone(), - rejected_stale_count, rejected_radius_count, rejected_bounds_count, rejected_continuity_count, + rejected_stale_count, + rejected_radius_count, + rejected_bounds_count, + rejected_continuity_count, }; let frame = MoveTrackerWorkerFrame { health, - observations: observations.into_iter().map(|value| MoveTrackerWorkerObservation { - move_id: value.move_id, center_x_px: value.center_x_px, center_y_px: value.center_y_px, - radius_px: value.radius_px, age_ms: value.age_ms, - }).collect(), + observations: observations + .into_iter() + .map(|value| MoveTrackerWorkerObservation { + move_id: value.move_id, + center_x_px: value.center_x_px, + center_y_px: value.center_y_px, + radius_px: value.radius_px, + age_ms: value.age_ms, + }) + .collect(), }; write_move_tracker_worker_frame(&mut writer, &frame)?; thread::sleep(Duration::from_millis(4)); @@ -5079,23 +5254,49 @@ fn run_move_tracker_worker(options: Options) -> Result<()> { #[cfg(feature = "psmoveapi-tracker")] #[derive(Clone, Copy, Debug, PartialEq)] -struct AdmittedMoveObservation { center_x_px: f32, center_y_px: f32, radius_px: f32, admitted_at: Instant } +struct AdmittedMoveObservation { + center_x_px: f32, + center_y_px: f32, + radius_px: f32, + admitted_at: Instant, +} #[cfg(feature = "psmoveapi-tracker")] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum PsmoveApiObservationRejection { Stale, Radius, Bounds, Continuity } +enum PsmoveApiObservationRejection { + Stale, + Radius, + Bounds, + Continuity, +} #[cfg(feature = "psmoveapi-tracker")] -fn admit_psmoveapi_observation(observation: &muninn_psmoveapi_tracker::PsmoveApiObservation, width: u32, height: u32, - previous: Option<&AdmittedMoveObservation>, now: Instant) -> std::result::Result { - if observation.age_ms < 0 || observation.age_ms > 50 { return Err(PsmoveApiObservationRejection::Stale); } - if !observation.radius_px.is_finite() || observation.radius_px < 2.0 { return Err(PsmoveApiObservationRejection::Radius); } - if !observation.center_x_px.is_finite() || !observation.center_y_px.is_finite() || observation.center_x_px < 0.0 - || observation.center_x_px >= width as f32 || observation.center_y_px < 0.0 || observation.center_y_px >= height as f32 { +fn admit_psmoveapi_observation( + observation: &muninn_psmoveapi_tracker::PsmoveApiObservation, + width: u32, + height: u32, + previous: Option<&AdmittedMoveObservation>, + now: Instant, +) -> std::result::Result { + if observation.age_ms < 0 || observation.age_ms > 50 { + return Err(PsmoveApiObservationRejection::Stale); + } + if !observation.radius_px.is_finite() || observation.radius_px < 2.0 { + return Err(PsmoveApiObservationRejection::Radius); + } + if !observation.center_x_px.is_finite() + || !observation.center_y_px.is_finite() + || observation.center_x_px < 0.0 + || observation.center_x_px >= width as f32 + || observation.center_y_px < 0.0 + || observation.center_y_px >= height as f32 + { return Err(PsmoveApiObservationRejection::Bounds); } if let Some(previous) = previous { - let elapsed = now.saturating_duration_since(previous.admitted_at).as_secs_f32(); + let elapsed = now + .saturating_duration_since(previous.admitted_at) + .as_secs_f32(); let dx = observation.center_x_px - previous.center_x_px; let dy = observation.center_y_px - previous.center_y_px; let distance = (dx * dx + dy * dy).sqrt(); @@ -5104,70 +5305,163 @@ fn admit_psmoveapi_observation(observation: &muninn_psmoveapi_tracker::PsmoveApi return Err(PsmoveApiObservationRejection::Continuity); } } - Ok(AdmittedMoveObservation { center_x_px: observation.center_x_px, center_y_px: observation.center_y_px, - radius_px: observation.radius_px, admitted_at: now }) + Ok(AdmittedMoveObservation { + center_x_px: observation.center_x_px, + center_y_px: observation.center_y_px, + radius_px: observation.radius_px, + admitted_at: now, + }) } #[cfg(all(test, feature = "psmoveapi-tracker"))] mod psmoveapi_observation_admission_tests { use super::*; - fn observation(x: f32, y: f32, radius: f32, age_ms: i32) -> muninn_psmoveapi_tracker::PsmoveApiObservation { - muninn_psmoveapi_tracker::PsmoveApiObservation { move_id: "move-test".to_string(), center_x_px: x, center_y_px: y, radius_px: radius, age_ms } + fn observation( + x: f32, + y: f32, + radius: f32, + age_ms: i32, + ) -> muninn_psmoveapi_tracker::PsmoveApiObservation { + muninn_psmoveapi_tracker::PsmoveApiObservation { + move_id: "move-test".to_string(), + center_x_px: x, + center_y_px: y, + radius_px: radius, + age_ms, + } } #[test] fn rejects_stale_tiny_and_out_of_frame_positions() { let now = Instant::now(); - assert_eq!(Err(PsmoveApiObservationRejection::Stale), admit_psmoveapi_observation(&observation(100.0, 100.0, 8.0, 51), 640, 480, None, now)); - assert_eq!(Err(PsmoveApiObservationRejection::Radius), admit_psmoveapi_observation(&observation(100.0, 100.0, 0.7, 1), 640, 480, None, now)); - assert_eq!(Err(PsmoveApiObservationRejection::Bounds), admit_psmoveapi_observation(&observation(641.0, 100.0, 8.0, 1), 640, 480, None, now)); + assert_eq!( + Err(PsmoveApiObservationRejection::Stale), + admit_psmoveapi_observation(&observation(100.0, 100.0, 8.0, 51), 640, 480, None, now) + ); + assert_eq!( + Err(PsmoveApiObservationRejection::Radius), + admit_psmoveapi_observation(&observation(100.0, 100.0, 0.7, 1), 640, 480, None, now) + ); + assert_eq!( + Err(PsmoveApiObservationRejection::Bounds), + admit_psmoveapi_observation(&observation(641.0, 100.0, 8.0, 1), 640, 480, None, now) + ); } #[test] fn rejects_fresh_teleport_without_calling_age_confidence() { let now = Instant::now(); - let previous = admit_psmoveapi_observation(&observation(100.0, 100.0, 8.0, 1), 640, 480, None, now).unwrap(); - assert_eq!(Err(PsmoveApiObservationRejection::Continuity), admit_psmoveapi_observation( - &observation(500.0, 400.0, 8.0, 1), 640, 480, Some(&previous), now + Duration::from_millis(4))); - assert!(admit_psmoveapi_observation(&observation(108.0, 104.0, 8.3, 1), 640, 480, Some(&previous), now + Duration::from_millis(4)).is_ok()); + let previous = + admit_psmoveapi_observation(&observation(100.0, 100.0, 8.0, 1), 640, 480, None, now) + .unwrap(); + assert_eq!( + Err(PsmoveApiObservationRejection::Continuity), + admit_psmoveapi_observation( + &observation(500.0, 400.0, 8.0, 1), + 640, + 480, + Some(&previous), + now + Duration::from_millis(4) + ) + ); + assert!( + admit_psmoveapi_observation( + &observation(108.0, 104.0, 8.3, 1), + 640, + 480, + Some(&previous), + now + Duration::from_millis(4) + ) + .is_ok() + ); } } #[cfg(feature = "psmoveapi-tracker")] -fn summarize_tracker_rgb(rgb: &[u8], expected: &[(String, [u8; 3])]) -> (Vec, Vec, Vec, Vec) { +fn summarize_tracker_rgb( + rgb: &[u8], + expected: &[(String, [u8; 3])], +) -> (Vec, Vec, Vec, Vec) { let mut sums = [0u64; 3]; let mut peaks = [0u32; 3]; let mut counts = vec![0u32; expected.len()]; let pixels = rgb.len() / 3; for pixel in rgb.chunks_exact(3) { - for channel in 0..3 { sums[channel] += u64::from(pixel[channel]); peaks[channel] = peaks[channel].max(u32::from(pixel[channel])); } + for channel in 0..3 { + sums[channel] += u64::from(pixel[channel]); + peaks[channel] = peaks[channel].max(u32::from(pixel[channel])); + } let max = *pixel.iter().max().unwrap_or(&0); let min = *pixel.iter().min().unwrap_or(&0); - if max < 40 || max.saturating_sub(min) < 30 { continue; } - let pixel_norm = (pixel.iter().map(|value| f64::from(*value).powi(2)).sum::()).sqrt(); + if max < 40 || max.saturating_sub(min) < 30 { + continue; + } + let pixel_norm = (pixel + .iter() + .map(|value| f64::from(*value).powi(2)) + .sum::()) + .sqrt(); for (index, (_, color)) in expected.iter().enumerate() { - let color_norm = (color.iter().map(|value| f64::from(*value).powi(2)).sum::()).sqrt(); - let dot = pixel.iter().zip(color).map(|(left, right)| f64::from(*left) * f64::from(*right)).sum::(); + let color_norm = (color + .iter() + .map(|value| f64::from(*value).powi(2)) + .sum::()) + .sqrt(); + let dot = pixel + .iter() + .zip(color) + .map(|(left, right)| f64::from(*left) * f64::from(*right)) + .sum::(); if pixel_norm > 0.0 && color_norm > 0.0 && dot / (pixel_norm * color_norm) >= 0.985 { counts[index] = counts[index].saturating_add(1); } } } - let means = if pixels == 0 { vec![0, 0, 0] } else { sums.iter().map(|sum| (*sum / pixels as u64) as u32).collect() }; - (means, peaks.to_vec(), expected.iter().map(|(id, _)| id.clone()).collect(), counts) + let means = if pixels == 0 { + vec![0, 0, 0] + } else { + sums.iter() + .map(|sum| (*sum / pixels as u64) as u32) + .collect() + }; + ( + means, + peaks.to_vec(), + expected.iter().map(|(id, _)| id.clone()).collect(), + counts, + ) } #[cfg(feature = "psmoveapi-tracker")] -fn tracker_colors(roster: &[String], program: &MuninnMoveHueProgramRecord) -> Vec<(String, [u8; 3])> { - let now_ns = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos() as i128; +fn tracker_colors( + roster: &[String], + program: &MuninnMoveHueProgramRecord, +) -> Vec<(String, [u8; 3])> { + let now_ns = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() as i128; let timestamp_ns = move_hue_program_timestamp_ns(program, now_ns); - roster.iter().filter_map(|identity| scheduled_golden_move_color_with_order( - identity, roster, i128::from(program.epoch_ns), i128::from(program.cycle_ms) * 1_000_000, - timestamp_ns, &program.order_mode, effective_transition_percent(program), - ).map(|(color, _, _)| (identity.clone(), [color.0, color.1, color.2]))).collect() + roster + .iter() + .filter_map(|identity| { + scheduled_golden_move_color_with_order( + identity, + roster, + i128::from(program.epoch_ns), + i128::from(program.cycle_ms) * 1_000_000, + timestamp_ns, + &program.order_mode, + effective_transition_percent(program), + ) + .map(|(color, _, _)| (identity.clone(), [color.0, color.1, color.2])) + }) + .collect() } #[cfg(not(feature = "psmoveapi-tracker"))] fn run_move_tracker_worker(_options: Options) -> Result<()> { - Err(anyhow!("move-tracker-worker requires the psmoveapi-tracker feature")) + Err(anyhow!( + "move-tracker-worker requires the psmoveapi-tracker feature" + )) } #[cfg(feature = "psmoveapi-tracker")] @@ -5200,30 +5494,47 @@ struct MoveTrackerWorkerProgram { } #[cfg(feature = "psmoveapi-tracker")] -fn publish_move_tracker_health(node: &mut cultmesh_rs::CultMeshNode, active: &mut [ActiveMoveMarkerCameraSource]) -> Result<()> { +fn publish_move_tracker_health( + node: &mut cultmesh_rs::CultMeshNode, + active: &mut [ActiveMoveMarkerCameraSource], +) -> Result<()> { for camera in active { - let Some(latest) = camera.psmoveapi_health.as_ref() else { continue; }; + let Some(latest) = camera.psmoveapi_health.as_ref() else { + continue; + }; let record = latest.lock().ok().and_then(|mut value| value.take()); - if let Some(record) = record { node.put(&record.health_id, &record)?; } + if let Some(record) = record { + node.put(&record.health_id, &record)?; + } } Ok(()) } #[cfg(not(feature = "psmoveapi-tracker"))] -fn publish_move_tracker_health(_node: &mut cultmesh_rs::CultMeshNode, _active: &mut [ActiveMoveMarkerCameraSource]) -> Result<()> { Ok(()) } +fn publish_move_tracker_health( + _node: &mut cultmesh_rs::CultMeshNode, + _active: &mut [ActiveMoveMarkerCameraSource], +) -> Result<()> { + Ok(()) +} fn publish_move_evidence_transport_health( node: &mut cultmesh_rs::CultMeshNode, options: &Options, stream: Option<&MoveEvidenceTransportHealth>, ) -> Result<()> { - let Some(stream) = stream else { return Ok(()); }; + let Some(stream) = stream else { + return Ok(()); + }; let record = MuninnMoveEvidenceTransportHealthRecord { health_id: format!("muninn:{}:move-evidence-transport-health", options.host_id), host_id: options.host_id.clone(), stream_id: stream.stream_id.clone(), produced_frames: stream.counters.produced_frames.load(Ordering::Relaxed), - local_ring_admissions: stream.counters.local_ring_admissions.load(Ordering::Relaxed), + local_ring_admissions: stream + .counters + .local_ring_admissions + .load(Ordering::Relaxed), remote_handoffs: stream.counters.remote_handoffs.load(Ordering::Relaxed), remote_sends: stream.counters.remote_sends.load(Ordering::Relaxed), updated_at: timestamp()?, @@ -5234,7 +5545,11 @@ fn publish_move_evidence_transport_health( #[cfg(feature = "psmoveapi-tracker")] fn video_device_index(path: &Path) -> Option { - path.file_name()?.to_str()?.strip_prefix("video")?.parse().ok() + path.file_name()? + .to_str()? + .strip_prefix("video")? + .parse() + .ok() } fn stable_marker_camera_source_hash(source: &MoveMarkerFrameSource) -> u64 { @@ -5475,7 +5790,7 @@ fn run_hid_controller_rudp_ingress( max_fragment_bytes: Some(HID_CONTROLLER_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), - reconnect_policy: None, + reconnect_policy: None, })?; let mut sources = live_move_state_sources(&options) .into_iter() @@ -5545,7 +5860,10 @@ fn run_hid_controller_rudp_ingress( continue; } if transport.connected() { - let payload = move_evidence_latest.lock().ok().and_then(|mut latest| latest.take()); + let payload = move_evidence_latest + .lock() + .ok() + .and_then(|mut latest| latest.take()); if let Some(payload) = payload { if let Err(error) = transport.send("move-evidence", payload) { eprintln!("Muninn Move evidence RUDP send warning: {error:#}"); @@ -6020,7 +6338,7 @@ fn create_hid_controller_stream(options: &Options) -> Result Result<()> { let mut transport = connect_idunn_rudp_health(options)?; transport - .send("schema", idunn_health_payload(options, state, detail, observed_at)?) + .send( + "schema", + idunn_health_payload(options, state, detail, observed_at)?, + ) .with_context(|| format!("sending Idunn health to {}", options.endpoint))?; Ok(()) } @@ -8728,7 +9054,10 @@ fn run_daemon_health_publisher(options: &Options) -> Result<()> { Err(error) => ("failed", error.to_string()), }; transport - .send("schema", idunn_health_payload(idunn, state, &detail, &observed_at)?) + .send( + "schema", + idunn_health_payload(idunn, state, &detail, &observed_at)?, + ) .with_context(|| format!("sending Idunn health to {}", idunn.endpoint))?; next_publish_at = Instant::now() + cadence; } @@ -10059,14 +10388,20 @@ impl Options { } "--move-tracker-camera-exposure" => { let value = take_value(&mut args, "--move-tracker-camera-exposure")?; - let (camera_id, exposure) = value.split_once('=') + let (camera_id, exposure) = value + .split_once('=') .context("--move-tracker-camera-exposure must be camera-id=0..1000")?; - let exposure = exposure.parse::() + let exposure = exposure + .parse::() .context("--move-tracker-camera-exposure must be camera-id=0..1000")?; if camera_id.trim().is_empty() || exposure > 1000 { - return Err(anyhow!("--move-tracker-camera-exposure must be camera-id=0..1000")); + return Err(anyhow!( + "--move-tracker-camera-exposure must be camera-id=0..1000" + )); } - options.move_tracker_camera_exposure_milli.insert(camera_id.to_string(), exposure); + options + .move_tracker_camera_exposure_milli + .insert(camera_id.to_string(), exposure); } "--move-evidence-stream" => { options.move_evidence_stream_id = @@ -10208,8 +10543,7 @@ impl Options { options.move_hue_mode = Some(take_value(&mut args, "--move-hue-mode")?) } "--move-hue-order" => { - options.move_hue_order_mode = - Some(take_value(&mut args, "--move-hue-order")?) + options.move_hue_order_mode = Some(take_value(&mut args, "--move-hue-order")?) } "--command" => options.command_id = Some(take_value(&mut args, "--command")?), "--move-host" => { @@ -10291,7 +10625,9 @@ impl Options { )); } if options.move_tracker_exposure_milli > 1000 { - return Err(anyhow!("--move-tracker-exposure-milli must be within 0..=1000")); + return Err(anyhow!( + "--move-tracker-exposure-milli must be within 0..=1000" + )); } if options.move_psmoveapi_tracker && !cfg!(feature = "psmoveapi-tracker") { return Err(anyhow!( @@ -11988,8 +12324,8 @@ mod tests { client_socket .set_read_timeout(Some(Duration::from_millis(5))) .unwrap(); - let mut client = CultNetRudpSocketTransportConnection::new( - CultNetRudpSocketTransportOptions { + let mut client = + CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { runtime_id: "muninn-provider-command-test-client".to_string(), socket: client_socket, mode: cultnet_rs::CultNetRudpSocketMode::Client, @@ -12002,10 +12338,9 @@ mod tests { max_fragment_bytes: Some(1200), max_pending_reliable_packets: Some(64), media_reliable_expire_after_ms: None, - reconnect_policy: None, - }, - ) - .unwrap(); + reconnect_policy: None, + }) + .unwrap(); client.connect(Vec::new()).unwrap(); let deadline = Instant::now() + Duration::from_secs(2); while !client.connected() && Instant::now() < deadline { @@ -12101,8 +12436,15 @@ mod tests { "move-000704a800d0", ]; let colors = roster.map(default_move_color_for_identity); - assert_eq!(colors.into_iter().collect::>().len(), roster.len()); - assert!(colors.into_iter().all(|color| color.0.max(color.1).max(color.2) == 255)); + assert_eq!( + colors.into_iter().collect::>().len(), + roster.len() + ); + assert!( + colors + .into_iter() + .all(|color| color.0.max(color.1).max(color.2) == 255) + ); } #[test] @@ -12118,7 +12460,11 @@ mod tests { .map(|identity| golden_move_color_for_roster(identity, &roster)) .collect::>(); assert_eq!(colors.iter().copied().collect::>().len(), 4); - assert!(colors.iter().all(|color| color.0.max(color.1).max(color.2) == 255)); + assert!( + colors + .iter() + .all(|color| color.0.max(color.1).max(color.2) == 255) + ); assert!(colors.iter().any(|color| color.0 == 255 && color.1 < 100)); assert!(colors.iter().any(|color| color.1 == 255 && color.0 < 100)); assert!(colors.iter().any(|color| color.2 == 255 && color.0 < 100)); @@ -12126,7 +12472,9 @@ mod tests { #[test] fn scheduled_golden_colors_shift_window_in_descending_move_order() { - let roster = (0..4).map(|index| format!("move-{index}")).collect::>(); + let roster = (0..4) + .map(|index| format!("move-{index}")) + .collect::>(); let state = |millis: i128| { roster .iter() @@ -12144,44 +12492,91 @@ mod tests { .collect::>() }; - assert_eq!(state(0), vec![(0, false), (1, false), (2, false), (3, true)]); - assert_eq!(state(249), vec![(0, false), (1, false), (2, false), (3, true)]); - assert_eq!(state(250), vec![(0, false), (1, false), (2, true), (4, false)]); - assert_eq!(state(500), vec![(0, false), (1, true), (3, false), (4, false)]); - assert_eq!(state(750), vec![(0, true), (2, false), (3, false), (4, false)]); - assert_eq!(state(1_000), vec![(1, false), (2, false), (3, false), (4, true)]); + assert_eq!( + state(0), + vec![(0, false), (1, false), (2, false), (3, true)] + ); + assert_eq!( + state(249), + vec![(0, false), (1, false), (2, false), (3, true)] + ); + assert_eq!( + state(250), + vec![(0, false), (1, false), (2, true), (4, false)] + ); + assert_eq!( + state(500), + vec![(0, false), (1, true), (3, false), (4, false)] + ); + assert_eq!( + state(750), + vec![(0, true), (2, false), (3, false), (4, false)] + ); + assert_eq!( + state(1_000), + vec![(1, false), (2, false), (3, false), (4, true)] + ); } #[test] fn scheduled_golden_color_smoothersteps_across_the_full_subslot() { - let roster = (0..4).map(|index| format!("move-{index}")).collect::>(); + let roster = (0..4) + .map(|index| format!("move-{index}")) + .collect::>(); let color_at = |millis: i128| { - scheduled_golden_move_color( + scheduled_golden_move_color("move-3", &roster, 0, 1_000_000_000, millis * 1_000_000) + .unwrap() + .0 + }; + + assert_eq!( + color_at(0), + hsv_to_rgb( + (3.0 * 0.618_033_988_749_894_9_f64).fract() * 360.0, + 1.0, + 1.0 + ) + ); + assert_eq!( + color_at(125), + hsv_to_rgb( + wrapped_hue_lerp( + (3.0 * 0.618_033_988_749_894_9_f64).fract(), + (4.0 * 0.618_033_988_749_894_9_f64).fract(), + 0.5, + ) * 360.0, + 1.0, + 1.0 + ) + ); + assert_eq!( + color_at(250), + hsv_to_rgb( + (4.0 * 0.618_033_988_749_894_9_f64).fract() * 360.0, + 1.0, + 1.0 + ) + ); + } + + #[test] + fn scheduled_golden_color_transition_uses_selected_subslot_percentage() { + let roster = (0..4) + .map(|index| format!("move-{index}")) + .collect::>(); + let color_at = |millis: i128| { + scheduled_golden_move_color_with_order( "move-3", &roster, 0, 1_000_000_000, millis * 1_000_000, + "descending", + 25, ) .unwrap() .0 }; - - assert_eq!(color_at(0), hsv_to_rgb((3.0 * 0.618_033_988_749_894_9_f64).fract() * 360.0, 1.0, 1.0)); - assert_eq!(color_at(125), hsv_to_rgb(wrapped_hue_lerp( - (3.0 * 0.618_033_988_749_894_9_f64).fract(), - (4.0 * 0.618_033_988_749_894_9_f64).fract(), - 0.5, - ) * 360.0, 1.0, 1.0)); - assert_eq!(color_at(250), hsv_to_rgb((4.0 * 0.618_033_988_749_894_9_f64).fract() * 360.0, 1.0, 1.0)); - } - - #[test] - fn scheduled_golden_color_transition_uses_selected_subslot_percentage() { - let roster = (0..4).map(|index| format!("move-{index}")).collect::>(); - let color_at = |millis: i128| scheduled_golden_move_color_with_order( - "move-3", &roster, 0, 1_000_000_000, millis * 1_000_000, "descending", 25, - ).unwrap().0; let source = (3.0 * 0.618_033_988_749_894_9_f64).fract(); let target = (4.0 * 0.618_033_988_749_894_9_f64).fract(); assert_ne!(color_at(31), hsv_to_rgb(source * 360.0, 1.0, 1.0)); @@ -12192,10 +12587,20 @@ mod tests { #[test] fn zero_percent_transition_hard_steps_at_subslot_boundary() { - let roster = (0..4).map(|index| format!("move-{index}")).collect::>(); + let roster = (0..4) + .map(|index| format!("move-{index}")) + .collect::>(); let color = scheduled_golden_move_color_with_order( - "move-3", &roster, 0, 1_000_000_000, 0, "descending", 0, - ).unwrap().0; + "move-3", + &roster, + 0, + 1_000_000_000, + 0, + "descending", + 0, + ) + .unwrap() + .0; let target = (4.0 * 0.618_033_988_749_894_9_f64).fract(); assert_eq!(color, hsv_to_rgb(target * 360.0, 1.0, 1.0)); } @@ -12206,8 +12611,14 @@ mod tests { assert_eq!(move_hue_update_order(4, 0, "ascending"), vec![0, 1, 2, 3]); assert_eq!(move_hue_update_order(4, 0, "bounce"), vec![3, 2, 1, 0]); assert_eq!(move_hue_update_order(4, 1, "bounce"), vec![0, 1, 2, 3]); - assert_eq!(move_hue_update_order(4, 0, "rotating-lead"), vec![3, 2, 1, 0]); - assert_eq!(move_hue_update_order(4, 1, "rotating-lead"), vec![2, 1, 0, 3]); + assert_eq!( + move_hue_update_order(4, 0, "rotating-lead"), + vec![3, 2, 1, 0] + ); + assert_eq!( + move_hue_update_order(4, 1, "rotating-lead"), + vec![2, 1, 0, 3] + ); let first = move_hue_update_order(4, 23, "golden-permutation"); assert_eq!(first, move_hue_update_order(4, 23, "golden-permutation")); assert_eq!(first.iter().copied().collect::>().len(), 4); @@ -13122,10 +13533,12 @@ Device 00:07:04:A8:00:D0 (public) .value .get("routes") .and_then(|value| value.as_array()) - .and_then(|routes| routes.iter().find(|route| { - route.get("role").and_then(|value| value.as_str()) - == Some("muninn provider command ingress") - })) + .and_then(|routes| { + routes.iter().find(|route| { + route.get("role").and_then(|value| value.as_str()) + == Some("muninn provider command ingress") + }) + }) .expect("provider-owned command route"); assert_eq!( command_route.get("uri").and_then(|value| value.as_str()), @@ -13618,7 +14031,10 @@ Device 00:07:04:A8:00:D0 (public) hidraw_path: "/dev/input/js2".to_string(), }]; - assert_eq!(merge_move_state_sources(discovered.clone(), &configured), discovered); + assert_eq!( + merge_move_state_sources(discovered.clone(), &configured), + discovered + ); } #[test] @@ -13701,7 +14117,11 @@ Device 00:07:04:A8:00:D0 (public) .and_then(CultMeshSharedMemoryFrameRing::try_acquire_latest_read) .expect("latest frame should be readable"); let decoded: DecodedMoveEvidenceStreamFrame = rmp_serde::from_slice(lease.bytes()).unwrap(); - let remote_payload = rudp_latest.lock().unwrap().take().expect("RUDP evidence copy"); + let remote_payload = rudp_latest + .lock() + .unwrap() + .take() + .expect("RUDP evidence copy"); assert_eq!(remote_payload, lease.bytes()); assert_eq!(decoded.0, "muninn:nightwing:move-evidence:0"); diff --git a/crates/muninn-psmoveapi-tracker/src/lib.rs b/crates/muninn-psmoveapi-tracker/src/lib.rs index 31eea08..0548abf 100644 --- a/crates/muninn-psmoveapi-tracker/src/lib.rs +++ b/crates/muninn-psmoveapi-tracker/src/lib.rs @@ -166,19 +166,29 @@ mod linux { } let serial = unsafe { text(psmove_get_serial(handle)) }; let move_id = format!("move-{}", serial.replace(':', "").to_ascii_lowercase()); - if self.controllers.iter().any(|controller| controller.move_id == move_id) { + if self + .controllers + .iter() + .any(|controller| controller.move_id == move_id) + { unsafe { psmove_disconnect(handle) }; continue; } - let Some((_, color)) = self.colors.iter().find(|(identity, _)| { - identity.eq_ignore_ascii_case(&move_id) - }) else { + let Some((_, color)) = self + .colors + .iter() + .find(|(identity, _)| identity.eq_ignore_ascii_case(&move_id)) + else { unsafe { psmove_disconnect(handle) }; continue; }; let status = unsafe { psmove_tracker_enable_color_observer( - self.tracker, handle, color[0], color[1], color[2] + self.tracker, + handle, + color[0], + color[1], + color[2], ) }; if status == TRACKER_CALIBRATED { @@ -192,9 +202,11 @@ mod linux { } pub fn set_expected_color(&mut self, move_id: &str, color: [u8; 3]) -> bool { - let Some(controller) = self.controllers.iter().find(|controller| { - controller.move_id.eq_ignore_ascii_case(move_id) - }) else { + let Some(controller) = self + .controllers + .iter() + .find(|controller| controller.move_id.eq_ignore_ascii_case(move_id)) + else { return false; }; unsafe { @@ -275,7 +287,9 @@ mod linux { if value.is_null() { return String::new(); } - unsafe { CStr::from_ptr(value) }.to_string_lossy().into_owned() + unsafe { CStr::from_ptr(value) } + .to_string_lossy() + .into_owned() } } @@ -301,20 +315,29 @@ impl PsmoveApiTracker { unreachable!() } - pub fn calibrate_connected(&mut self) -> usize { 0 } + pub fn calibrate_connected(&mut self) -> usize { + 0 + } - pub fn observe_connected(&mut self) -> usize { 0 } + pub fn observe_connected(&mut self) -> usize { + 0 + } - pub fn set_expected_color(&mut self, _move_id: &str, _color: [u8; 3]) -> bool { false } + pub fn set_expected_color(&mut self, _move_id: &str, _color: [u8; 3]) -> bool { + false + } - pub fn tracked_controller_count(&self) -> usize { 0 } + pub fn tracked_controller_count(&self) -> usize { + 0 + } pub fn update(&mut self) -> Vec { Vec::new() } - - pub fn rgb_image(&self) -> Option<(u32, u32, Vec)> { None } + pub fn rgb_image(&self) -> Option<(u32, u32, Vec)> { + None + } } #[cfg(test)] diff --git a/crates/odin-core/examples/dump_move_tracker_health.rs b/crates/odin-core/examples/dump_move_tracker_health.rs index e8e9bc8..ad99d97 100644 --- a/crates/odin-core/examples/dump_move_tracker_health.rs +++ b/crates/odin-core/examples/dump_move_tracker_health.rs @@ -1,6 +1,8 @@ use anyhow::{Context, Result}; use cultmesh_rs::{CultMesh, CultMeshNodeOptions}; -use odin_core::{MuninnMoveEvidenceTransportHealthRecord, MuninnMoveTrackerHealthRecord, OdinDocuments}; +use odin_core::{ + MuninnMoveEvidenceTransportHealthRecord, MuninnMoveTrackerHealthRecord, OdinDocuments, +}; use std::{env, path::PathBuf}; fn main() -> Result<()> { @@ -42,16 +44,29 @@ fn main() -> Result<()> { record.last_observation_at, record.image_mean_rgb, record.image_peak_rgb, - record.color_evidence_move_ids.iter().cloned().zip(record.color_evidence_pixel_counts.iter().copied()).collect::>(), + record + .color_evidence_move_ids + .iter() + .cloned() + .zip(record.color_evidence_pixel_counts.iter().copied()) + .collect::>(), record.updated_at, record.detail, ); } - for record in node.cache().get_all::().unwrap_or_default() { + for record in node + .cache() + .get_all::() + .unwrap_or_default() + { println!( "stream={} produced={} local_admitted={} remote_handoffs={} remote_sends={} updated={}", - record.stream_id, record.produced_frames, record.local_ring_admissions, - record.remote_handoffs, record.remote_sends, record.updated_at, + record.stream_id, + record.produced_frames, + record.local_ring_admissions, + record.remote_handoffs, + record.remote_sends, + record.updated_at, ); } Ok(()) diff --git a/crates/sleipnir-daemon/src/main.rs b/crates/sleipnir-daemon/src/main.rs index 29be8d3..06b056f 100644 --- a/crates/sleipnir-daemon/src/main.rs +++ b/crates/sleipnir-daemon/src/main.rs @@ -2227,7 +2227,7 @@ fn create_rudp_stream( max_fragment_bytes: Some(HID_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), - reconnect_policy: None, + reconnect_policy: None, })?; transport.connect(Vec::new())?; return Ok(Some(ActiveRudpStream { @@ -2268,7 +2268,7 @@ fn create_rudp_stream( max_fragment_bytes: Some(HID_RUDP_MAX_FRAGMENT_BYTES), max_pending_reliable_packets: Some(256), media_reliable_expire_after_ms: Some(25), - reconnect_policy: None, + reconnect_policy: None, })?, target: None, last_connect_attempt_at: None, From ba8b4afbb6fe900aeadd7106d6b19c7bb56741aa Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 5 Sep 2026 17:35:54 +0200 Subject: [PATCH 90/93] Bump CultLib to ae0ac0d and give odin-core its missing rmp-serde dev-dep Moves all six CultLib pins from cbde940 to ae0ac0d, which carries the reconciled cultmesh-rs. The delta is three commits and near-nothing in the packages Odin consumes: a license line, two lines in cultnet-rs replication, and one additive pub fn. The dev-dependency is a real pre-existing defect, not fallout from the bump. odin-core's tests use rmp_serde in crates/odin-core/src/idunn.rs but never declared it, so the lib-test target has never compiled. Verified pre-existing: 9 errors at the old pin, 8 at the new. With the dep declared, odin-core tests compile and 22 pass -- including idunn_records_round_trip_through_cultmesh, which matters for the extraction this branch is preparing. Note for anyone checking this on Windows: odin-daemon does not build here and that is expected, not a regression. It imports std::os::fd and from_raw_fd, which are Unix-only. Identical failure at both pins. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 7 ++++--- crates/idunn-daemon/Cargo.toml | 4 ++-- crates/muninn-daemon/Cargo.toml | 2 +- crates/odin-core/Cargo.toml | 5 +++-- crates/odin-daemon/Cargo.toml | 4 ++-- crates/sleipnir-daemon/Cargo.toml | 4 ++-- vendor/cultmesh-rs/Cargo.toml | 4 ++-- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9aa3bfa..1e81d6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,7 +175,7 @@ dependencies = [ [[package]] name = "cultcache-rs" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" +source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" dependencies = [ "anyhow", "chrono", @@ -193,7 +193,7 @@ dependencies = [ [[package]] name = "cultcache-rs-derive" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" +source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" dependencies = [ "proc-macro2", "quote", @@ -213,7 +213,7 @@ dependencies = [ [[package]] name = "cultnet-rs" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585#cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585" +source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" dependencies = [ "aes-gcm", "anyhow", @@ -637,6 +637,7 @@ dependencies = [ "cultmesh-rs", "cultnet-rs", "pretty_assertions", + "rmp-serde", "serde", "serde_json", "tempfile", diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index e37dca1..9defe95 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -20,8 +20,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } odin-core = { path = "../odin-core" } rmp-serde = "1.3" sha2 = "0.10" diff --git a/crates/muninn-daemon/Cargo.toml b/crates/muninn-daemon/Cargo.toml index eca171c..7a4bb65 100644 --- a/crates/muninn-daemon/Cargo.toml +++ b/crates/muninn-daemon/Cargo.toml @@ -16,7 +16,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } libc = "0.2" muninn-move-tracker = { path = "../muninn-move-tracker" } muninn-psmoveapi-tracker = { path = "../muninn-psmoveapi-tracker", optional = true } diff --git a/crates/odin-core/Cargo.toml b/crates/odin-core/Cargo.toml index 5752a4d..c730b0d 100644 --- a/crates/odin-core/Cargo.toml +++ b/crates/odin-core/Cargo.toml @@ -11,12 +11,13 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } cultmesh-rs = { path = "../../vendor/cultmesh-rs" } serde_json = "1" -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] +rmp-serde = "1.3" pretty_assertions = "1.4" tempfile = "3" diff --git a/crates/odin-daemon/Cargo.toml b/crates/odin-daemon/Cargo.toml index e50553d..7b859b3 100644 --- a/crates/odin-daemon/Cargo.toml +++ b/crates/odin-daemon/Cargo.toml @@ -12,9 +12,9 @@ path = "src/lib.rs" [dependencies] anyhow = "1" chrono = "0.4" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } fs2 = "0.4" rmp-serde = "1.3" serde = "1" diff --git a/crates/sleipnir-daemon/Cargo.toml b/crates/sleipnir-daemon/Cargo.toml index f08651a..430f45d 100644 --- a/crates/sleipnir-daemon/Cargo.toml +++ b/crates/sleipnir-daemon/Cargo.toml @@ -12,8 +12,8 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } odin-core = { path = "../odin-core" } rmp-serde = "1.3" serde_json = "1" diff --git a/vendor/cultmesh-rs/Cargo.toml b/vendor/cultmesh-rs/Cargo.toml index 441481d..e3f4090 100644 --- a/vendor/cultmesh-rs/Cargo.toml +++ b/vendor/cultmesh-rs/Cargo.toml @@ -10,8 +10,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "cbde940df2f5ee0ccac50e8dbfe9f36ffbebb585", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] From 0995222c17789163c0421df81c9ad4564b7a435e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 5 Sep 2026 17:43:24 +0200 Subject: [PATCH 91/93] Sever idunn-daemon's last import from odin-core MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IdunnDaemonHealthTrustBindingRecord and its schema constant moved to cultnet-rs (GameCult/CultLib@c13b6ba). odin-core now imports them from cultnet_rs instead of defining them, and idunn-daemon/provisioning.rs pulls all six provider-health symbols from cultnet_rs directly, merged into its existing cultnet_rs use block. Dropped the odin-core path dependency from idunn-daemon/Cargo.toml — nothing in the crate reaches odin_core:: anymore. Bumped the CultLib git rev across all six consuming manifests to the new commit. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 7 ++- crates/idunn-daemon/Cargo.toml | 5 +- crates/idunn-daemon/src/provisioning.rs | 16 +++--- crates/muninn-daemon/Cargo.toml | 2 +- crates/odin-core/Cargo.toml | 4 +- crates/odin-core/src/documents.rs | 70 ++----------------------- crates/odin-daemon/Cargo.toml | 4 +- crates/sleipnir-daemon/Cargo.toml | 4 +- vendor/cultmesh-rs/Cargo.toml | 4 +- 9 files changed, 26 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e81d6f..ffb1e41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,7 +175,7 @@ dependencies = [ [[package]] name = "cultcache-rs" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" +source = "git+https://github.com/GameCult/CultLib.git?rev=c13b6ba07e99f2535b455da74d07d88c308731f5#c13b6ba07e99f2535b455da74d07d88c308731f5" dependencies = [ "anyhow", "chrono", @@ -193,7 +193,7 @@ dependencies = [ [[package]] name = "cultcache-rs-derive" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" +source = "git+https://github.com/GameCult/CultLib.git?rev=c13b6ba07e99f2535b455da74d07d88c308731f5#c13b6ba07e99f2535b455da74d07d88c308731f5" dependencies = [ "proc-macro2", "quote", @@ -213,7 +213,7 @@ dependencies = [ [[package]] name = "cultnet-rs" version = "0.1.0" -source = "git+https://github.com/GameCult/CultLib.git?rev=ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55#ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55" +source = "git+https://github.com/GameCult/CultLib.git?rev=c13b6ba07e99f2535b455da74d07d88c308731f5#c13b6ba07e99f2535b455da74d07d88c308731f5" dependencies = [ "aes-gcm", "anyhow", @@ -514,7 +514,6 @@ dependencies = [ "cultnet-rs", "ed25519-dalek", "libc", - "odin-core", "rmp-serde", "serde", "sha2", diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml index 9defe95..d8427e5 100644 --- a/crates/idunn-daemon/Cargo.toml +++ b/crates/idunn-daemon/Cargo.toml @@ -20,9 +20,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } -odin-core = { path = "../odin-core" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } rmp-serde = "1.3" sha2 = "0.10" chrono = "0.4" diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs index 1487768..8ae58a9 100644 --- a/crates/idunn-daemon/src/provisioning.rs +++ b/crates/idunn-daemon/src/provisioning.rs @@ -3,9 +3,13 @@ use cultcache_rs::{ CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, }; use cultnet_rs::{ - GameCultProviderHealthIdentity, IDUNN_DEPLOYMENT_BRAKE_AUTHORITY, IDUNN_DEPLOYMENT_BRAKE_ID, - IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_SCOPE, IDUNN_DEPLOYMENT_RELEASE_PURPOSE, - IDUNN_LIFECYCLE_BRAKE_AUTHORITY, IDUNN_LIFECYCLE_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCOPE, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultProviderHealthIdentity, + GameCultServiceTrustAnchorRecord, IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_AUTHORITY, + IDUNN_DEPLOYMENT_BRAKE_ID, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_SCOPE, + IDUNN_DEPLOYMENT_RELEASE_PURPOSE, IDUNN_LIFECYCLE_BRAKE_AUTHORITY, IDUNN_LIFECYCLE_BRAKE_SCHEMA, + IDUNN_LIFECYCLE_BRAKE_SCOPE, IdunnDaemonHealthTrustBindingRecord, IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, IdunnDeploymentBrakeRecord, IdunnDeploymentBrakeReleasePurpose, IdunnLifecycleBrakeObservation, IdunnLifecycleBrakeRecord, IdunnServiceIdentity, OdinTopologyIdentity, ServiceIdentityProfile, @@ -13,12 +17,6 @@ use cultnet_rs::{ evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, export_service_identity_trust_anchor, open_service_identity_at, }; -use odin_core::{ - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultServiceTrustAnchorRecord, - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnDaemonHealthTrustBindingRecord, -}; use std::collections::{BTreeMap, BTreeSet}; use std::path::{Path, PathBuf}; diff --git a/crates/muninn-daemon/Cargo.toml b/crates/muninn-daemon/Cargo.toml index 7a4bb65..75e16cf 100644 --- a/crates/muninn-daemon/Cargo.toml +++ b/crates/muninn-daemon/Cargo.toml @@ -16,7 +16,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } libc = "0.2" muninn-move-tracker = { path = "../muninn-move-tracker" } muninn-psmoveapi-tracker = { path = "../muninn-psmoveapi-tracker", optional = true } diff --git a/crates/odin-core/Cargo.toml b/crates/odin-core/Cargo.toml index c730b0d..74c10f0 100644 --- a/crates/odin-core/Cargo.toml +++ b/crates/odin-core/Cargo.toml @@ -11,10 +11,10 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } cultmesh-rs = { path = "../../vendor/cultmesh-rs" } serde_json = "1" -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] diff --git a/crates/odin-core/src/documents.rs b/crates/odin-core/src/documents.rs index 5371cb4..8347e4e 100644 --- a/crates/odin-core/src/documents.rs +++ b/crates/odin-core/src/documents.rs @@ -2,11 +2,14 @@ use cultcache_rs::DatabaseEntry; pub use cultnet_rs::{ GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultServiceTrustAnchorRecord, IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, IDUNN_PROVIDER_ACTIVE_REASON, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IDUNN_PROVIDER_ACTIVE_REASON, IDUNN_PROVIDER_DEGRADED_REASON, IDUNN_PROVIDER_FAILED_REASON, IDUNN_PROVIDER_WARMING_REASON, IDUNN_SIGNED_DAEMON_HEALTH_SCHEMA, IdunnAuthenticatedProviderHealthProjectionRecord, - IdunnSignedDaemonHealthRecord, authenticated_provider_health_reason_code, + IdunnDaemonHealthTrustBindingRecord, IdunnSignedDaemonHealthRecord, + authenticated_provider_health_reason_code, }; +#[cfg(test)] use cultnet_rs::{GameCultProviderHealthIdentity, derive_service_identity_id}; use serde_json::Value; @@ -47,7 +50,6 @@ pub const IDUNN_DAEMON_TRANSPORT_PROFILE_SCHEMA: &str = "idunn.daemon_transport_ pub const IDUNN_COMMAND_BOUNDARY_SCHEMA: &str = "idunn.command_boundary.v1"; pub const IDUNN_RUNTIME_TRANSPORT_CHECK_SCHEMA: &str = "idunn.runtime_transport_check.v1"; pub const IDUNN_RUDP_HEALTH_INGRESS_SCHEMA: &str = "idunn.rudp_health_ingress.v1"; -pub const IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA: &str = "idunn.daemon_health_trust_binding.v1"; pub const IDUNN_AUTHENTICATED_DAEMON_HEALTH_ADMISSION_SCHEMA: &str = "idunn.authenticated_daemon_health_admission.v1"; pub const IDUNN_RUNTIME_TRAFFIC_ADMISSION_SCHEMA: &str = "idunn.runtime_traffic_admission.v2"; @@ -329,68 +331,6 @@ pub struct IdunnSignedHealthAdmissionRecord { pub signed_health_sha256: String, } -/// Root-owned admission binding. Store ownership is the physical authority; -/// this document names the only signer accepted for one daemon/contract/runtime. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.daemon_health_trust_binding", - schema = "idunn.daemon_health_trust_binding.v1" -)] -pub struct IdunnDaemonHealthTrustBindingRecord { - #[cultcache(key = 0)] - pub schema_version: String, - #[cultcache(key = 1)] - pub binding_id: String, - #[cultcache(key = 2)] - pub daemon_id: String, - #[cultcache(key = 3)] - pub health_contract: String, - #[cultcache(key = 4)] - pub source_runtime_id: String, - #[cultcache(key = 5)] - pub signer_identity_id: String, - #[cultcache(key = 6)] - pub signer_public_key: Vec, - #[cultcache(key = 7)] - pub binding_authority: String, - #[cultcache(key = 8)] - pub bound_at_unix_millis: u64, - #[cultcache(key = 9)] - pub release_binding_required: bool, - #[cultcache(key = 10)] - pub private_state_exposed: bool, -} - -impl IdunnDaemonHealthTrustBindingRecord { - pub fn validate(&self) -> Result<()> { - if self.schema_version != IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA { - bail!("daemon health trust binding schema is unsupported"); - } - validate_identifier(&self.binding_id, "binding id")?; - validate_identifier(&self.daemon_id, "daemon id")?; - validate_identifier(&self.health_contract, "health contract")?; - validate_identifier(&self.source_runtime_id, "source runtime id")?; - validate_identifier(&self.signer_identity_id, "signer identity id")?; - if self.signer_public_key.len() != 32 - || self.binding_authority != "root" - || self.bound_at_unix_millis == 0 - { - bail!("daemon health trust binding authority or key is invalid"); - } - if self.private_state_exposed { - bail!("daemon health trust binding exposes private state"); - } - if self.signer_identity_id - != derive_service_identity_id::( - &self.signer_public_key, - )? - { - bail!("daemon health trust binding identity does not match its public key"); - } - Ok(()) - } -} - /// Idunn-owned judgment joining one verified provider statement to the exact /// root trust binding used to admit it. Managed-health reads must rejoin this /// record with both current objects; a health row by itself is not authority. diff --git a/crates/odin-daemon/Cargo.toml b/crates/odin-daemon/Cargo.toml index 7b859b3..7b78846 100644 --- a/crates/odin-daemon/Cargo.toml +++ b/crates/odin-daemon/Cargo.toml @@ -12,9 +12,9 @@ path = "src/lib.rs" [dependencies] anyhow = "1" chrono = "0.4" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } fs2 = "0.4" rmp-serde = "1.3" serde = "1" diff --git a/crates/sleipnir-daemon/Cargo.toml b/crates/sleipnir-daemon/Cargo.toml index 430f45d..71fcba5 100644 --- a/crates/sleipnir-daemon/Cargo.toml +++ b/crates/sleipnir-daemon/Cargo.toml @@ -12,8 +12,8 @@ path = "src/main.rs" [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } odin-core = { path = "../odin-core" } rmp-serde = "1.3" serde_json = "1" diff --git a/vendor/cultmesh-rs/Cargo.toml b/vendor/cultmesh-rs/Cargo.toml index e3f4090..741c6d8 100644 --- a/vendor/cultmesh-rs/Cargo.toml +++ b/vendor/cultmesh-rs/Cargo.toml @@ -10,8 +10,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "ae0ac0d3c92ccaee1a7eee2bbe033740c9ea2e55", package = "cultnet-rs" } +cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } +cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } [dev-dependencies] From 7ea146dd3699e571a7e6a75c1f00b9a9c067ad84 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 5 Sep 2026 18:09:11 +0200 Subject: [PATCH 92/93] Cut idunn-daemon and the vendored cultmesh fork out of Odin Idunn now lives at https://github.com/GameCult/Idunn with its full 136-commit history. Odin no longer contains the deployment authority that manages it. Also removes vendor/cultmesh-rs, the private fork that had diverged from CultLib in both directions. Its unique capability -- the RUDP document server and the message_id correlation rework -- was reconciled into CultLib first, so this deletes a duplicate rather than a capability. The four crates that consumed it by path (muninn-daemon, odin-core, odin-daemon, sleipnir-daemon) now consume CultLib at c13b6ba like everything else. What deliberately stays in Odin: crates/odin-core/src/idunn.rs, which is Odin's own projection of Idunn state for aggregation and is Odin's to own; src/odin/idunn-rudp.cjs, its client for talking to Idunn; and deployment/idunn/recipe.toml, which despite the path is Odin's own deployment recipe expressed in Idunn's format -- it builds odin-daemon. odin-core: 22 passed, 0 failed, unchanged across the cut. odin-daemon still fails to compile on Windows for the pre-existing Unix-only std::os::fd reason. Co-Authored-By: Claude Opus 5 --- Cargo.toml | 2 - crates/idunn-daemon/Cargo.toml | 37 - .../idunn-daemon/src/bin/idunn-provision.rs | 6 - crates/idunn-daemon/src/control_plane.rs | 5923 ------------- crates/idunn-daemon/src/deployment.rs | 2110 ----- crates/idunn-daemon/src/deployment_plan.rs | 1560 ---- crates/idunn-daemon/src/drivers.rs | 7337 ----------------- crates/idunn-daemon/src/lib.rs | 5 - crates/idunn-daemon/src/main.rs | 3 - crates/idunn-daemon/src/provisioning.rs | 2455 ------ crates/idunn-daemon/tests/fixtures/README.md | 12 - .../bifrost-release-authority-v1.payload.hex | 1 - crates/muninn-daemon/Cargo.toml | 2 +- crates/odin-core/Cargo.toml | 2 +- crates/odin-daemon/Cargo.toml | 2 +- crates/sleipnir-daemon/Cargo.toml | 2 +- vendor/cultmesh-rs/Cargo.lock | 1360 --- vendor/cultmesh-rs/Cargo.toml | 20 - vendor/cultmesh-rs/src/lib.rs | 1557 ---- .../cultmesh-rs/src/rudp_document_server.rs | 647 -- .../cultmesh-rs/tests/rudp_document_server.rs | 553 -- 21 files changed, 4 insertions(+), 23592 deletions(-) delete mode 100644 crates/idunn-daemon/Cargo.toml delete mode 100644 crates/idunn-daemon/src/bin/idunn-provision.rs delete mode 100644 crates/idunn-daemon/src/control_plane.rs delete mode 100644 crates/idunn-daemon/src/deployment.rs delete mode 100644 crates/idunn-daemon/src/deployment_plan.rs delete mode 100644 crates/idunn-daemon/src/drivers.rs delete mode 100644 crates/idunn-daemon/src/lib.rs delete mode 100644 crates/idunn-daemon/src/main.rs delete mode 100644 crates/idunn-daemon/src/provisioning.rs delete mode 100644 crates/idunn-daemon/tests/fixtures/README.md delete mode 100644 crates/idunn-daemon/tests/fixtures/bifrost-release-authority-v1.payload.hex delete mode 100644 vendor/cultmesh-rs/Cargo.lock delete mode 100644 vendor/cultmesh-rs/Cargo.toml delete mode 100644 vendor/cultmesh-rs/src/lib.rs delete mode 100644 vendor/cultmesh-rs/src/rudp_document_server.rs delete mode 100644 vendor/cultmesh-rs/tests/rudp_document_server.rs diff --git a/Cargo.toml b/Cargo.toml index 09a0ec3..d6d1640 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] members = [ "crates/sleipnir-daemon", - "crates/idunn-daemon", "crates/muninn-daemon", "crates/muninn-move-tracker", "crates/muninn-psmoveapi-tracker", @@ -11,7 +10,6 @@ members = [ resolver = "3" exclude = [ "vendor/cultcache-rs", - "vendor/cultmesh-rs", "vendor/cultnet-rs", ] diff --git a/crates/idunn-daemon/Cargo.toml b/crates/idunn-daemon/Cargo.toml deleted file mode 100644 index d8427e5..0000000 --- a/crates/idunn-daemon/Cargo.toml +++ /dev/null @@ -1,37 +0,0 @@ -[package] -name = "idunn-daemon" -version = "0.1.0" -edition.workspace = true -license.workspace = true -publish.workspace = true - -[[bin]] -name = "idunn" -path = "src/main.rs" - -[[bin]] -name = "idunn-provision" -path = "src/bin/idunn-provision.rs" - -[lib] -name = "idunn_daemon" -path = "src/lib.rs" - -[dependencies] -anyhow = "1" -cultmesh-rs = { path = "../../vendor/cultmesh-rs" } -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } -rmp-serde = "1.3" -sha2 = "0.10" -chrono = "0.4" -serde = { version = "1", features = ["derive"] } -ed25519-dalek = "2.2" -toml = "0.8" -uuid = { version = "1", features = ["v4"] } - -[dev-dependencies] -tempfile = "3" - -[target.'cfg(unix)'.dependencies] -libc = "0.2" diff --git a/crates/idunn-daemon/src/bin/idunn-provision.rs b/crates/idunn-daemon/src/bin/idunn-provision.rs deleted file mode 100644 index cab36d3..0000000 --- a/crates/idunn-daemon/src/bin/idunn-provision.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - if let Err(error) = idunn_daemon::provisioning::run(std::env::args().skip(1)) { - eprintln!("idunn-provision: {error:#}"); - std::process::exit(1); - } -} diff --git a/crates/idunn-daemon/src/control_plane.rs b/crates/idunn-daemon/src/control_plane.rs deleted file mode 100644 index 1f8361b..0000000 --- a/crates/idunn-daemon/src/control_plane.rs +++ /dev/null @@ -1,5923 +0,0 @@ -use std::collections::{BTreeMap, BTreeSet}; -use std::env; -use std::fs::{self, File, OpenOptions}; -use std::path::{Path, PathBuf}; -use std::thread; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; - -use anyhow::{Context, Result, anyhow, bail, ensure}; -use cultcache_rs::{ - CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, -}; -use cultnet_rs::{ - AuthenticatedOdinRuntimeTopologyCorrelation, GameCultProviderHealthIdentity, - IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_LIFECYCLE_BRAKE_SCHEMA, IDUNN_PROCESS_WRITE_LEASE_SCHEMA, - IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, - IdunnDeploymentBrakeRecord, IdunnExpectedIncarnationRecord, IdunnLifecycleBrakeObservation, - IdunnLifecycleBrakeRecord, IdunnProcessWriteLeaseRecord, IdunnRuntimeActivationLaunch, - IdunnRuntimeActivationRecord, IdunnServiceIdentity, OdinTopologyAuthenticationContext, - OdinTopologyDisagreement, OdinTopologyIdentity, RuntimePresenceAuthenticationContext, - ServiceIdentityProfile, ServiceIdentitySigner, ServiceIdentityTrustAnchor, - authenticate_odin_runtime_topology_correlation, authenticate_runtime_presence_claim, - correlate_runtime_presence_claim, derive_service_identity_id, - evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, open_service_identity_at, - verify_idunn_deployment_brake_authorization, verify_runtime_authority, -}; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; -use uuid::Uuid; - -use crate::deployment::{DependencyKind, OperatorBinding, RouteBinding, capability_compatible}; -use crate::deployment_plan::{ - CompiledDeploymentPlan, DependencyProviderAuthority, SealedRelease, compile_deployment_plan, -}; -use crate::drivers::{ - CultCacheTopologyDriver, CultCacheWriteLeaseDriver, DockerRunnerDriver, FrozenSourceReceipt, - GitSourceDriver, InstalledReleaseObservation, NginxRouteDriver, ProcessIdentity, - RouteObservation, RoutePreflightReceipt, RunnerPort, SourcePort, - SystemdTransientWorkloadDriver, TopologyPort, WorkloadObservation, WorkloadPort, - WriteLeasePort, -}; - -const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v2"; -const DEPLOYMENT_TRANSACTION_SCHEMA: &str = "idunn.deployment_transaction.v2"; -const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v2"; -const DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS: u64 = 30_000; -const DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS: u64 = 2_000; - -/// An immutable request. Execution state deliberately does not fit in this -/// record; status is derived from the transactions created for it. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.deployment_command", - schema = "idunn.deployment_command.v2" -)] -struct DeploymentCommand { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - command_id: String, - #[cultcache(key = 2)] - kind: CommandKind, - #[cultcache(key = 3)] - selector: String, - #[cultcache(key = 4)] - requested_by: String, - #[cultcache(key = 5)] - requested_at_unix_millis: u64, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -enum CommandKind { - Deploy, - Continuity, -} - -impl DeploymentCommand { - fn validate(&self) -> Result<()> { - ensure!( - self.schema_version == DEPLOYMENT_COMMAND_SCHEMA, - "deployment command schema is unsupported" - ); - require_id(&self.command_id, "deployment command id")?; - require_selector(&self.selector)?; - require_value(&self.requested_by, "deployment requester")?; - ensure!( - self.requested_at_unix_millis > 0, - "deployment command has no request time" - ); - match self.kind { - CommandKind::Deploy => ensure!( - !self.command_id.starts_with("continuity-"), - "operator deployment uses the continuity command namespace" - ), - CommandKind::Continuity => ensure!( - self.command_id.starts_with("continuity-") - && !self.selector.starts_with("profile:"), - "continuity command identity or selector is invalid" - ), - } - Ok(()) - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -#[repr(u8)] -enum DeploymentPhase { - Sealing, - Starting, - Warming, - Fencing, - Leasing, - AwaitingReady, - Routing, - Committing, - Complete, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct DeploymentAuthorization { - authorization_id: String, - brake_sha256: String, - canonical_brake_bytes: Vec, - authorized_at_unix_millis: u64, -} - -impl DeploymentAuthorization { - fn validate_shape(&self) -> Result<()> { - require_id(&self.authorization_id, "deployment authorization id")?; - ensure!( - !self.canonical_brake_bytes.is_empty() - && self.brake_sha256 == sha256_id(&self.canonical_brake_bytes) - && self.authorized_at_unix_millis > 0, - "deployment authorization receipt is incomplete" - ); - let record: IdunnDeploymentBrakeRecord = - rmp_serde::from_slice(&self.canonical_brake_bytes)?; - record.validate()?; - ensure!( - rmp_serde::to_vec(&record)? == self.canonical_brake_bytes - && record.authorization_id.as_deref() == Some(&self.authorization_id), - "deployment authorization receipt is noncanonical or mismatched" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct TopologyEvidence { - canonical_bytes: Vec, - canonical_sha256: String, - signer_identity_id: String, - publisher_sequence: u64, - admitted_at_unix_millis: u64, -} - -impl TopologyEvidence { - fn from_authenticated( - topology: &AuthenticatedOdinRuntimeTopologyCorrelation, - admitted_at_unix_millis: u64, - ) -> Result { - let record = topology.record(); - let evidence = Self { - canonical_bytes: topology.canonical_bytes().to_vec(), - canonical_sha256: sha256_id(topology.canonical_bytes()), - signer_identity_id: record.signer_identity_id.clone(), - publisher_sequence: record.publisher_sequence, - admitted_at_unix_millis, - }; - evidence.validate_shape()?; - Ok(evidence) - } - - fn validate_shape(&self) -> Result<()> { - ensure!( - !self.canonical_bytes.is_empty() - && self.canonical_sha256 == sha256_id(&self.canonical_bytes), - "topology evidence bytes or digest are invalid" - ); - require_id(&self.signer_identity_id, "Odin topology signer")?; - ensure!( - self.publisher_sequence > 0 && self.admitted_at_unix_millis > 0, - "topology evidence sequence or admission time is invalid" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct RuntimePresenceEvidence { - canonical_bytes: Vec, - canonical_sha256: String, - message_id: String, - challenged_at_unix_millis: u64, - admitted_at_unix_millis: u64, -} - -impl RuntimePresenceEvidence { - fn from_present( - present: &cultnet_rs::VerifiedRuntimePresence, - message_id: String, - challenged_at_unix_millis: u64, - admitted_at_unix_millis: u64, - ) -> Result { - let evidence = Self { - canonical_bytes: present.canonical_bytes().to_vec(), - canonical_sha256: present.signed_presence_sha256().to_owned(), - message_id, - challenged_at_unix_millis, - admitted_at_unix_millis, - }; - evidence.validate_shape()?; - Ok(evidence) - } - - fn validate_shape(&self) -> Result<()> { - ensure!( - !self.canonical_bytes.is_empty() - && self.canonical_sha256 == sha256_id(&self.canonical_bytes), - "runtime presence evidence bytes or digest are invalid" - ); - require_id(&self.message_id, "runtime presence challenge")?; - ensure!( - self.challenged_at_unix_millis > 0 - && self.admitted_at_unix_millis >= self.challenged_at_unix_millis, - "runtime presence evidence timeline is invalid" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "source", rename_all = "kebab-case", deny_unknown_fields)] -enum WarmingEvidence { - OdinTopology { evidence: TopologyEvidence }, - FirstOdinDirect { evidence: RuntimePresenceEvidence }, -} - -impl WarmingEvidence { - fn validate_shape(&self) -> Result<()> { - match self { - Self::OdinTopology { evidence } => evidence.validate_shape(), - Self::FirstOdinDirect { evidence } => evidence.validate_shape(), - } - } - - fn canonical_sha256(&self) -> &str { - match self { - Self::OdinTopology { evidence } => &evidence.canonical_sha256, - Self::FirstOdinDirect { evidence } => &evidence.canonical_sha256, - } - } -} - -/// Opaque capability minted only after the transaction CAS stores exact -/// Warming evidence. Adjacent drivers receive only the candidate identity and -/// signed-presence digest needed to bind the write lease; they cannot inspect -/// or construct semantic admission. -#[derive(Clone, Debug)] -pub struct SequenceAdmittedWarming { - transaction_id: String, - evidence: WarmingEvidence, - signed_presence_sha256: String, - runtime_instance_id: String, -} - -impl SequenceAdmittedWarming { - fn from_topology( - transaction_id: String, - evidence: TopologyEvidence, - authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, - ) -> Result { - let record = authenticated.record(); - let signed_presence_sha256 = record - .signed_presence_sha256 - .clone() - .context("Warming topology has no signed presence")?; - let runtime_instance_id = record - .runtime_instance_id - .clone() - .context("Warming topology has no runtime instance")?; - Ok(Self { - transaction_id, - evidence: WarmingEvidence::OdinTopology { evidence }, - signed_presence_sha256, - runtime_instance_id, - }) - } - - fn from_first_odin_presence( - transaction_id: String, - evidence: RuntimePresenceEvidence, - present: cultnet_rs::VerifiedRuntimePresence, - ) -> Result { - ensure!( - present.canonical_bytes() == evidence.canonical_bytes - && present.signed_presence_sha256() == evidence.canonical_sha256, - "direct first-Odin Warming evidence differs from its authenticated presence" - ); - let runtime_instance_id = present.record().runtime_instance_id.clone(); - Ok(Self { - transaction_id, - signed_presence_sha256: evidence.canonical_sha256.clone(), - evidence: WarmingEvidence::FirstOdinDirect { evidence }, - runtime_instance_id, - }) - } - - #[cfg(test)] - pub(crate) fn for_test( - transaction_id: impl Into, - authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, - admitted_at_unix_millis: u64, - ) -> Result { - let record = authenticated.record(); - ensure!( - record.present - && !record.ready - && record.observed_presence_state.as_deref() == Some("warming") - && record.observed_write_lease_sha256.is_none() - && record.disagreements.is_empty(), - "test receipt is not exact Warming evidence" - ); - let transaction_id = transaction_id.into(); - require_id(&transaction_id, "test transaction id")?; - let evidence = - TopologyEvidence::from_authenticated(&authenticated, admitted_at_unix_millis)?; - let signed_presence_sha256 = record - .signed_presence_sha256 - .clone() - .context("test Warming receipt has no signed presence")?; - let runtime_instance_id = record - .runtime_instance_id - .clone() - .context("test Warming receipt has no runtime instance")?; - let token = Self::from_topology(transaction_id, evidence, authenticated)?; - ensure!( - token.signed_presence_sha256 == signed_presence_sha256 - && token.runtime_instance_id == runtime_instance_id, - "test Warming token extraction changed" - ); - Ok(token) - } - - pub(crate) fn transaction_id(&self) -> &str { - &self.transaction_id - } - - pub(crate) fn evidence_sha256(&self) -> &str { - self.evidence.canonical_sha256() - } - - pub(crate) fn signed_presence_sha256(&self) -> &str { - &self.signed_presence_sha256 - } - - pub(crate) fn runtime_instance_id(&self) -> &str { - &self.runtime_instance_id - } -} - -/// Ready proof with the same construction boundary. Dependency planning and -/// promotion receive this token, never caller-supplied digest/sequence tuples. -#[derive(Clone, Debug)] -pub(crate) struct SequenceAdmittedReady { - transaction_id: String, - evidence: TopologyEvidence, - expected: IdunnExpectedIncarnationRecord, - authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, -} - -impl SequenceAdmittedReady { - #[cfg(test)] - pub(crate) fn for_test( - expected: &IdunnExpectedIncarnationRecord, - authenticated: AuthenticatedOdinRuntimeTopologyCorrelation, - admitted_at_unix_millis: u64, - ) -> Result { - expected.validate()?; - let record = authenticated.record(); - ensure!( - record.expected_projection_sha256 == expected.canonical_sha256()? - && record.target == expected.target - && record.runtime_id == expected.runtime_id - && record.present - && record.ready - && record.observed_presence_state.as_deref() == Some("active") - && record.disagreements.is_empty(), - "test receipt is not exact Ready evidence" - ); - let evidence = - TopologyEvidence::from_authenticated(&authenticated, admitted_at_unix_millis)?; - Ok(Self { - transaction_id: "test-sequence-admission".into(), - evidence, - expected: expected.clone(), - authenticated, - }) - } - - pub(crate) fn authenticated(&self) -> &AuthenticatedOdinRuntimeTopologyCorrelation { - &self.authenticated - } - - pub(crate) fn expected(&self) -> &IdunnExpectedIncarnationRecord { - &self.expected - } - - pub(crate) fn transaction_id(&self) -> &str { - &self.transaction_id - } - - pub(crate) fn evidence_sha256(&self) -> &str { - &self.evidence.canonical_sha256 - } - - pub(crate) fn publisher_sequence(&self) -> u64 { - self.evidence.publisher_sequence - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] -enum FencingEvidence { - SkippedStateless, - Revoked { - incumbent_lease_sha256: Option, - candidate_lease_path_verified_empty: bool, - }, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] -enum LeasingEvidence { - SkippedStateless, - Prepared { - lease: IdunnProcessWriteLeaseRecord, - lease_sha256: String, - }, - Granted { - lease: IdunnProcessWriteLeaseRecord, - lease_sha256: String, - }, -} - -impl LeasingEvidence { - fn lease(&self) -> Option<&IdunnProcessWriteLeaseRecord> { - match self { - Self::SkippedStateless | Self::Prepared { .. } => None, - Self::Granted { lease, .. } => Some(lease), - } - } - - fn prepared_lease(&self) -> Option<(&IdunnProcessWriteLeaseRecord, &str)> { - match self { - Self::Prepared { - lease, - lease_sha256, - } => Some((lease, lease_sha256)), - Self::SkippedStateless | Self::Granted { .. } => None, - } - } - - fn lease_sha256(&self) -> Option<&str> { - match self { - Self::SkippedStateless | Self::Prepared { .. } => None, - Self::Granted { lease_sha256, .. } => Some(lease_sha256), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] -enum RoutingEvidence { - SkippedUnrouted, - Promoted { - observation: RouteObservation, - promoted_at_unix_millis: u64, - }, -} - -impl RoutingEvidence { - fn observation(&self) -> Option<&RouteObservation> { - match self { - Self::SkippedUnrouted => None, - Self::Promoted { observation, .. } => Some(observation), - } - } - - fn promoted_at_unix_millis(&self) -> Option { - match self { - Self::SkippedUnrouted => None, - Self::Promoted { - promoted_at_unix_millis, - .. - } => Some(*promoted_at_unix_millis), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct IsolationEvidence { - candidate_uid: u32, - candidate_pid_namespace_id: u64, - candidate_mount_namespace_id: u64, - incumbent_uid: Option, - incumbent_pid_namespace_id: Option, - incumbent_mount_namespace_id: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "outcome", rename_all = "kebab-case", deny_unknown_fields)] -enum TransactionCompletion { - Admitted { generation_id: String }, - FailedBeforeFencing { error: String }, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -enum CleanupEvidence { - Pending, - Skipped, - Complete, -} - -impl CleanupEvidence { - fn is_complete(self) -> bool { - matches!(self, Self::Skipped | Self::Complete) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct PreFencingAbort { - error: String, - candidate_cleanup: CleanupEvidence, - topology_reconciliation: CleanupEvidence, - source_cleanup: CleanupEvidence, -} - -impl PreFencingAbort { - fn is_complete(&self) -> bool { - self.candidate_cleanup.is_complete() - && self.topology_reconciliation.is_complete() - && self.source_cleanup.is_complete() - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "result", rename_all = "kebab-case", deny_unknown_fields)] -enum IncumbentCleanupEvidence { - SkippedNoIncumbent, - Pending { - generation_id: String, - workload: WorkloadObservation, - }, - Complete { - generation_id: String, - }, -} - -impl IncumbentCleanupEvidence { - fn is_complete(&self) -> bool { - matches!(self, Self::SkippedNoIncumbent | Self::Complete { .. }) - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -enum SourceCleanupEvidence { - SkippedContinuity, - Pending, - Complete, -} - -impl SourceCleanupEvidence { - fn is_complete(self) -> bool { - !matches!(self, Self::Pending) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct PostCommitCleanup { - incumbent: IncumbentCleanupEvidence, - source: SourceCleanupEvidence, -} - -impl PostCommitCleanup { - fn is_complete(&self) -> bool { - self.incumbent.is_complete() && self.source.is_complete() - } -} - -/// The sole owner of an in-flight deployment decision. Every actuator result -/// is durable here before a later phase is entered. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.deployment_transaction", - schema = "idunn.deployment_transaction.v2" -)] -struct DeploymentTransaction { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - transaction_id: String, - #[cultcache(key = 2)] - command_id: String, - #[cultcache(key = 3)] - command_kind: CommandKind, - #[cultcache(key = 4)] - target: String, - #[cultcache(key = 5)] - ordinal: u32, - #[cultcache(key = 6)] - phase: DeploymentPhase, - #[cultcache(key = 7)] - created_at_unix_millis: u64, - #[cultcache(key = 8)] - updated_at_unix_millis: u64, - #[cultcache(key = 9)] - incumbent_generation_id: Option, - #[cultcache(key = 10)] - plan: Option, - #[cultcache(key = 11)] - frozen_source: Option, - #[cultcache(key = 12)] - sealed_release: Option, - #[cultcache(key = 13)] - installed_release: Option, - #[cultcache(key = 14)] - expected: Option, - #[cultcache(key = 15)] - expected_publication_sha256: Option, - #[cultcache(key = 16)] - deployment_authorization: Option, - #[cultcache(key = 17)] - lifecycle_authorized_at_unix_millis: Option, - #[cultcache(key = 18)] - activation: Option, - #[cultcache(key = 19)] - workload: Option, - #[cultcache(key = 20)] - activation_publication_sha256: Option, - #[cultcache(key = 21)] - latest_odin_observation: Option, - #[cultcache(key = 22)] - warming: Option, - #[cultcache(key = 23)] - route_preflight: Option, - #[cultcache(key = 24)] - isolation: Option, - #[cultcache(key = 25)] - fencing: Option, - #[cultcache(key = 26)] - leasing: Option, - #[cultcache(key = 27)] - ready: Option, - #[cultcache(key = 28)] - routing: Option, - #[cultcache(key = 29)] - odin_publisher_sequence_cursor: u64, - #[cultcache(key = 30)] - last_error: Option, - #[cultcache(key = 31)] - completion: Option, - #[cultcache(key = 32)] - pre_fencing_abort: Option, - #[cultcache(key = 33)] - post_commit_cleanup: Option, -} - -impl DeploymentTransaction { - fn new( - command: &DeploymentCommand, - target: String, - ordinal: u32, - incumbent: Option<&AdmittedGeneration>, - now: u64, - ) -> Result { - let transaction = Self { - schema_version: DEPLOYMENT_TRANSACTION_SCHEMA.into(), - transaction_id: format!("tx-{}", Uuid::new_v4()), - command_id: command.command_id.clone(), - command_kind: command.kind, - target, - ordinal, - phase: DeploymentPhase::Sealing, - created_at_unix_millis: now, - updated_at_unix_millis: now, - incumbent_generation_id: incumbent.map(|value| value.generation_id.clone()), - plan: None, - frozen_source: None, - sealed_release: None, - installed_release: None, - expected: None, - expected_publication_sha256: None, - deployment_authorization: None, - lifecycle_authorized_at_unix_millis: None, - activation: None, - workload: None, - activation_publication_sha256: None, - latest_odin_observation: None, - warming: None, - route_preflight: None, - isolation: None, - fencing: None, - leasing: None, - ready: None, - routing: None, - odin_publisher_sequence_cursor: incumbent - .map_or(0, |value| value.odin_publisher_sequence_cursor), - last_error: None, - completion: None, - pre_fencing_abort: None, - post_commit_cleanup: None, - }; - transaction.validate()?; - Ok(transaction) - } - - fn from_continuity( - command: &DeploymentCommand, - incumbent: &AdmittedGeneration, - now: u64, - ) -> Result { - let mut transaction = - Self::new(command, incumbent.target.clone(), 0, Some(incumbent), now)?; - transaction.plan = Some(incumbent.plan.clone()); - transaction.sealed_release = Some(incumbent.sealed_release.clone()); - transaction.installed_release = Some(incumbent.installed_release.clone()); - transaction.expected = Some(incumbent.expected.clone()); - transaction.validate()?; - Ok(transaction) - } - - fn rejected(command: &DeploymentCommand, error: anyhow::Error, now: u64) -> Result { - let mut transaction = Self::new(command, command.selector.clone(), 0, None, now)?; - let detail = truncate(&format!("{error:#}"), 2048); - transaction.phase = DeploymentPhase::Complete; - transaction.updated_at_unix_millis = now; - transaction.last_error = Some(detail.clone()); - transaction.pre_fencing_abort = Some(PreFencingAbort { - error: detail.clone(), - candidate_cleanup: CleanupEvidence::Skipped, - topology_reconciliation: CleanupEvidence::Skipped, - source_cleanup: CleanupEvidence::Skipped, - }); - transaction.completion = Some(TransactionCompletion::FailedBeforeFencing { error: detail }); - transaction.validate()?; - Ok(transaction) - } - - fn is_terminal(&self) -> bool { - if self.phase != DeploymentPhase::Complete { - return false; - } - match &self.completion { - Some(TransactionCompletion::FailedBeforeFencing { .. }) => self - .pre_fencing_abort - .as_ref() - .is_some_and(PreFencingAbort::is_complete), - Some(TransactionCompletion::Admitted { .. }) => self - .post_commit_cleanup - .as_ref() - .is_some_and(PostCommitCleanup::is_complete), - None => false, - } - } - - /// Complete transactions retain retryable cleanup work, but admission has - /// already transferred current-incarnation authority to AdmittedGeneration. - fn owns_target_authority(&self) -> bool { - self.phase != DeploymentPhase::Complete - } - - /// Admission has moved to the new generation at Complete, but an exact - /// draining incumbent still reserves its process and candidate endpoint. - /// No later mutation for the same target may overlap that cleanup. - fn blocks_new_target_mutation(&self) -> bool { - self.owns_target_authority() - || self - .post_commit_cleanup - .as_ref() - .is_some_and(|cleanup| !cleanup.is_complete()) - } - - fn validate(&self) -> Result<()> { - ensure!( - self.schema_version == DEPLOYMENT_TRANSACTION_SCHEMA, - "deployment transaction schema is unsupported" - ); - require_id(&self.transaction_id, "deployment transaction id")?; - require_id(&self.command_id, "transaction command id")?; - require_id(&self.target, "transaction target")?; - ensure!( - self.created_at_unix_millis > 0 - && self.updated_at_unix_millis >= self.created_at_unix_millis, - "deployment transaction timestamps are invalid" - ); - if let Some(generation) = &self.incumbent_generation_id { - require_id(generation, "incumbent generation id")?; - } - if let Some(plan) = &self.plan { - plan.validate()?; - ensure!( - plan.parsed_inputs()?.1.target == self.target, - "transaction plan belongs to another target" - ); - } - if let (Some(plan), Some(frozen)) = (&self.plan, &self.frozen_source) { - frozen.validate_against(plan)?; - } - if let (Some(plan), Some(release)) = (&self.plan, &self.sealed_release) { - release.validate_against(plan)?; - } - if let Some(expected) = &self.expected { - expected.validate()?; - ensure!(expected.target == self.target, "Expected target differs"); - if let Some(plan) = &self.plan { - ensure!(expected.plan_id == plan.plan_id, "Expected plan differs"); - } - if let Some(release) = &self.sealed_release { - ensure!( - expected.sealed_release_id == release.sealed_release_id, - "Expected release differs" - ); - } - if let Some(digest) = &self.expected_publication_sha256 { - ensure!( - digest == &expected.canonical_sha256()?, - "Expected publication receipt differs" - ); - } - } - if let (Some(expected), Some(activation)) = (&self.expected, &self.activation) { - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()?, - "activation belongs to another Expected projection" - ); - if let Some(workload) = &self.workload { - ensure!( - workload.runtime_instance_id == activation.runtime_instance_id, - "workload belongs to another activation" - ); - } - if let Some(digest) = &self.activation_publication_sha256 { - ensure!( - digest == &activation.canonical_sha256()?, - "activation publication receipt differs" - ); - } - } - ensure!( - self.workload.is_none() || self.activation.is_some(), - "workload observation exists without its prepared activation" - ); - for evidence in [&self.latest_odin_observation, &self.ready] - .into_iter() - .flatten() - { - evidence.validate_shape()?; - ensure!( - evidence.publisher_sequence <= self.odin_publisher_sequence_cursor, - "topology evidence exceeds the transaction replay cursor" - ); - } - if let Some(warming) = &self.warming { - warming.validate_shape()?; - match warming { - WarmingEvidence::OdinTopology { evidence } => ensure!( - evidence.publisher_sequence <= self.odin_publisher_sequence_cursor, - "Warming topology evidence exceeds the transaction replay cursor" - ), - WarmingEvidence::FirstOdinDirect { .. } => ensure!( - self.target == "odin" && self.incumbent_generation_id.is_none(), - "direct Warming evidence is reserved for first Odin bootstrap" - ), - } - } - if let Some(leasing) = &self.leasing { - if let Some(expected) = &self.expected { - ensure!( - matches!( - (expected.write_lease_required, leasing), - (true, LeasingEvidence::Prepared { .. }) - | (true, LeasingEvidence::Granted { .. }) - | (false, LeasingEvidence::SkippedStateless) - ), - "lease evidence differs from the Expected state contract" - ); - } - let lease_and_sha256 = match leasing { - LeasingEvidence::SkippedStateless => None, - LeasingEvidence::Prepared { - lease, - lease_sha256, - } - | LeasingEvidence::Granted { - lease, - lease_sha256, - } => Some((lease, lease_sha256)), - }; - if let Some((lease, lease_sha256)) = lease_and_sha256 { - lease.validate()?; - ensure!( - lease.canonical_sha256()? == *lease_sha256, - "lease evidence digest differs" - ); - } - } - if let (Some(expected), Some(fencing)) = (&self.expected, &self.fencing) { - match fencing { - FencingEvidence::SkippedStateless => ensure!( - !expected.write_lease_required, - "stateful candidate claims stateless fencing" - ), - FencingEvidence::Revoked { - candidate_lease_path_verified_empty, - .. - } => ensure!( - *candidate_lease_path_verified_empty == expected.write_lease_required, - "candidate lease-path fencing differs from Expected" - ), - } - } - if let Some(error) = &self.last_error { - require_detail(error, "transaction error")?; - } - if let Some(authorization) = &self.deployment_authorization { - authorization.validate_shape()?; - } - if let Some(abort) = &self.pre_fencing_abort { - require_detail(&abort.error, "pre-fencing abort error")?; - ensure!( - self.post_commit_cleanup.is_none(), - "aborted transaction also carries post-commit cleanup" - ); - ensure!( - matches!( - ( - self.workload.is_some() || self.activation.is_some(), - abort.candidate_cleanup - ), - (true, CleanupEvidence::Pending | CleanupEvidence::Complete) - | (false, CleanupEvidence::Skipped) - ), - "abort candidate cleanup differs from its prepared activation" - ); - let topology_cleanup_required = self.command_kind == CommandKind::Deploy - && self.expected_publication_sha256.is_some(); - ensure!( - matches!( - (topology_cleanup_required, abort.topology_reconciliation), - (true, CleanupEvidence::Pending | CleanupEvidence::Complete) - | (false, CleanupEvidence::Skipped) - ), - "abort topology cleanup differs from published deployment Expected" - ); - ensure!( - matches!( - (self.command_kind, abort.source_cleanup), - ( - CommandKind::Deploy, - CleanupEvidence::Pending | CleanupEvidence::Complete - ) | (CommandKind::Continuity, CleanupEvidence::Skipped) - ) || (self.command_kind == CommandKind::Deploy - && abort.source_cleanup == CleanupEvidence::Skipped - && self.plan.is_none() - && self.frozen_source.is_none() - && self.expected.is_none() - && self.phase == DeploymentPhase::Complete), - "abort source cleanup differs from command work" - ); - ensure!( - self.phase < DeploymentPhase::Fencing || self.phase == DeploymentPhase::Complete, - "pre-fencing abort crossed the fencing boundary" - ); - } - - let failed = matches!( - self.completion, - Some(TransactionCompletion::FailedBeforeFencing { .. }) - ); - if failed { - ensure!( - self.phase == DeploymentPhase::Complete, - "terminal failure is not Complete" - ); - let abort = required(&self.pre_fencing_abort, "terminal abort evidence")?; - let TransactionCompletion::FailedBeforeFencing { error } = - self.completion.as_ref().unwrap() - else { - unreachable!() - }; - ensure!( - abort.is_complete() && abort.error == *error, - "terminal failure lacks complete matching abort evidence" - ); - } else { - ensure!( - !matches!( - self.completion, - Some(TransactionCompletion::FailedBeforeFencing { .. }) - ), - "non-failure branch carries failure completion" - ); - if self.phase >= DeploymentPhase::Starting { - ensure!( - self.plan.is_some() - && self.sealed_release.is_some() - && self.installed_release.is_some() - && self.expected.is_some() - && self.expected_publication_sha256.is_some(), - "Starting transaction lacks sealed release evidence" - ); - match self.command_kind { - CommandKind::Deploy => ensure!( - self.frozen_source.is_some() && self.deployment_authorization.is_some(), - "deployment entered Starting without frozen source and brake consumption" - ), - CommandKind::Continuity => ensure!( - self.lifecycle_authorized_at_unix_millis.is_some() - && self.deployment_authorization.is_none(), - "continuity entered Starting without its lifecycle gate" - ), - } - } - if self.phase >= DeploymentPhase::Warming { - ensure!( - self.activation.is_some() - && self.workload.is_some() - && self.activation_publication_sha256.is_some(), - "Warming transaction lacks observed activation" - ); - } - if self.phase >= DeploymentPhase::Fencing { - ensure!( - self.warming.is_some() && self.isolation.is_some(), - "Fencing transaction lacks warming or isolation evidence" - ); - ensure!( - self.expected - .as_ref() - .and_then(|expected| expected.route.as_ref()) - .is_some() - == self.route_preflight.is_some(), - "route preflight does not match routed Expected" - ); - if let Some(preflight) = &self.route_preflight { - preflight.validate()?; - } - } - if self.phase >= DeploymentPhase::Leasing { - ensure!(self.fencing.is_some(), "Leasing lacks fencing evidence"); - } - if self.phase >= DeploymentPhase::AwaitingReady { - ensure!( - matches!( - self.leasing.as_ref(), - Some(LeasingEvidence::SkippedStateless) - | Some(LeasingEvidence::Granted { .. }) - ), - "AwaitingReady lacks finalized lease evidence" - ); - } - if self.phase >= DeploymentPhase::Routing { - ensure!(self.ready.is_some(), "Routing lacks Ready evidence"); - } - if self.phase >= DeploymentPhase::Committing { - ensure!(self.routing.is_some(), "Committing lacks routing evidence"); - } - if let Some(RoutingEvidence::Promoted { - observation, - promoted_at_unix_millis, - }) = &self.routing - { - observation.validate()?; - ensure!( - *promoted_at_unix_millis > 0 - && *promoted_at_unix_millis <= self.updated_at_unix_millis, - "route promotion time is outside the durable transaction timeline" - ); - } - if self.phase == DeploymentPhase::Complete { - ensure!( - matches!( - self.completion, - Some(TransactionCompletion::Admitted { .. }) - ), - "successful Complete transaction lacks admission receipt" - ); - ensure!( - self.pre_fencing_abort.is_none() && self.post_commit_cleanup.is_some(), - "admitted transaction lacks exclusive post-commit cleanup evidence" - ); - let TransactionCompletion::Admitted { generation_id } = - self.completion.as_ref().unwrap() - else { - unreachable!() - }; - ensure!( - generation_id == &format!("generation-{}", self.transaction_id), - "admission completion names another transaction generation" - ); - } else { - ensure!( - self.completion.is_none() && self.post_commit_cleanup.is_none(), - "non-Complete transaction has completion or post-commit state" - ); - } - } - if let Some(cleanup) = &self.post_commit_cleanup { - ensure!( - self.phase == DeploymentPhase::Complete - && matches!( - self.completion, - Some(TransactionCompletion::Admitted { .. }) - ), - "post-commit cleanup exists without an admitted Complete transaction" - ); - match (&self.incumbent_generation_id, &cleanup.incumbent) { - (None, IncumbentCleanupEvidence::SkippedNoIncumbent) => {} - ( - Some(expected), - IncumbentCleanupEvidence::Pending { generation_id, .. } - | IncumbentCleanupEvidence::Complete { generation_id }, - ) if expected == generation_id => {} - _ => bail!("post-commit incumbent cleanup names another generation"), - } - ensure!( - matches!( - (self.command_kind, cleanup.source), - (CommandKind::Deploy, SourceCleanupEvidence::Pending) - | (CommandKind::Deploy, SourceCleanupEvidence::Complete) - | ( - CommandKind::Continuity, - SourceCleanupEvidence::SkippedContinuity - ) - ), - "post-commit source cleanup differs from command kind" - ); - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct AdmittedOdinAuthority { - signer_identity_id: String, - signer_public_key: Vec, -} - -impl AdmittedOdinAuthority { - fn from_anchor(anchor: &ServiceIdentityTrustAnchor) -> Result { - ensure!( - derive_service_identity_id::(&anchor.public_key)? - == anchor.identity_id, - "Odin topology anchor identity differs from its key" - ); - Ok(Self { - signer_identity_id: anchor.identity_id.clone(), - signer_public_key: anchor.public_key.clone(), - }) - } - - fn validate(&self) -> Result<()> { - require_id(&self.signer_identity_id, "admitted Odin signer")?; - ensure!( - derive_service_identity_id::(&self.signer_public_key)? - == self.signer_identity_id, - "admitted Odin signer identity differs from its key" - ); - Ok(()) - } -} - -/// The only current-incarnation owner. Unit state, route state, topology -/// projections, and CLI output are observations of this record, never peers. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache( - type = "idunn.admitted_generation", - schema = "idunn.admitted_generation.v2" -)] -struct AdmittedGeneration { - #[cultcache(key = 0)] - schema_version: String, - #[cultcache(key = 1)] - target: String, - #[cultcache(key = 2)] - generation_id: String, - #[cultcache(key = 3)] - command_id: String, - #[cultcache(key = 4)] - transaction_id: String, - #[cultcache(key = 5)] - admitted_at_unix_millis: u64, - #[cultcache(key = 6)] - plan: CompiledDeploymentPlan, - #[cultcache(key = 7)] - sealed_release: SealedRelease, - #[cultcache(key = 8)] - installed_release: InstalledReleaseObservation, - #[cultcache(key = 9)] - expected: IdunnExpectedIncarnationRecord, - #[cultcache(key = 10)] - activation: IdunnRuntimeActivationRecord, - #[cultcache(key = 11)] - workload: WorkloadObservation, - #[cultcache(key = 12)] - leasing: LeasingEvidence, - #[cultcache(key = 13)] - ready: TopologyEvidence, - #[cultcache(key = 14)] - latest_odin_observation: TopologyEvidence, - #[cultcache(key = 15)] - routing: RoutingEvidence, - #[cultcache(key = 16)] - odin_authority: AdmittedOdinAuthority, - #[cultcache(key = 17)] - odin_publisher_sequence_cursor: u64, - #[cultcache(key = 18)] - route_repair_started_at_unix_millis: Option, -} - -impl AdmittedGeneration { - fn from_transaction( - transaction: &DeploymentTransaction, - odin_authority: AdmittedOdinAuthority, - now: u64, - ) -> Result { - ensure!( - transaction.phase == DeploymentPhase::Committing, - "only Committing can create an admitted generation" - ); - let generation = Self { - schema_version: ADMITTED_GENERATION_SCHEMA.into(), - target: transaction.target.clone(), - generation_id: format!("generation-{}", transaction.transaction_id), - command_id: transaction.command_id.clone(), - transaction_id: transaction.transaction_id.clone(), - admitted_at_unix_millis: now, - plan: required(&transaction.plan, "transaction plan")?.clone(), - sealed_release: required(&transaction.sealed_release, "sealed release")?.clone(), - installed_release: required(&transaction.installed_release, "installed release")? - .clone(), - expected: required(&transaction.expected, "Expected projection")?.clone(), - activation: required(&transaction.activation, "activation")?.clone(), - workload: required(&transaction.workload, "workload")?.clone(), - leasing: required(&transaction.leasing, "lease disposition")?.clone(), - ready: required(&transaction.ready, "Ready receipt")?.clone(), - latest_odin_observation: required( - &transaction.latest_odin_observation, - "latest Odin receipt", - )? - .clone(), - routing: required(&transaction.routing, "route disposition")?.clone(), - odin_authority, - odin_publisher_sequence_cursor: transaction.odin_publisher_sequence_cursor, - route_repair_started_at_unix_millis: None, - }; - generation.validate()?; - Ok(generation) - } - - fn validate(&self) -> Result<()> { - ensure!( - self.schema_version == ADMITTED_GENERATION_SCHEMA, - "admitted generation schema is unsupported" - ); - require_id(&self.target, "admitted target")?; - require_id(&self.generation_id, "admitted generation id")?; - require_id(&self.command_id, "admitted command id")?; - require_id(&self.transaction_id, "admitted transaction id")?; - ensure!(self.admitted_at_unix_millis > 0, "admission has no time"); - self.plan.validate()?; - self.sealed_release.validate_against(&self.plan)?; - self.expected.validate()?; - self.activation.validate()?; - self.ready.validate_shape()?; - self.latest_odin_observation.validate_shape()?; - self.odin_authority.validate()?; - ensure!( - self.target == self.expected.target - && self.expected.plan_id == self.plan.plan_id - && self.expected.sealed_release_id == self.sealed_release.sealed_release_id - && self.activation.expected_projection_sha256 - == self.expected.canonical_sha256()? - && self.activation.runtime_instance_id == self.workload.runtime_instance_id - && self.ready.publisher_sequence <= self.odin_publisher_sequence_cursor - && self.latest_odin_observation.publisher_sequence - == self.odin_publisher_sequence_cursor, - "admitted generation evidence does not describe one incarnation" - ); - ensure!( - self.expected.write_lease_required == self.leasing.lease().is_some(), - "admitted write-lease disposition differs from Expected" - ); - ensure!( - self.expected.route.is_some() == self.routing.observation().is_some(), - "admitted route disposition differs from Expected" - ); - if let Some(route) = self.routing.observation() { - route.validate()?; - ensure!( - route.runtime_instance_id == self.activation.runtime_instance_id, - "admitted route observation belongs to another runtime instance" - ); - } - if let Some(started_at) = self.route_repair_started_at_unix_millis { - ensure!(started_at > 0, "admitted route repair has no start time"); - ensure!( - matches!(&self.routing, RoutingEvidence::Promoted { .. }), - "only a promoted routed generation can own route repair intent" - ); - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct RuntimeOptions { - state_store: PathBuf, - bindings_dir: PathBuf, - source_root: PathBuf, - staging_root: PathBuf, - topology_store: PathBuf, - odin_correlation_store: PathBuf, - odin_trust_anchor: PathBuf, - idunn_identity_store: PathBuf, - deployment_brake_operator_anchor: PathBuf, - source_identity: Option, - topology_maximum_age_millis: u64, - topology_maximum_future_skew_millis: u64, - poll_millis: u64, -} - -impl Default for RuntimeOptions { - fn default() -> Self { - Self { - state_store: PathBuf::from("/var/lib/gamecult/idunn/control.cc"), - bindings_dir: PathBuf::from("/etc/gamecult/idunn/bindings"), - source_root: PathBuf::from("/var/lib/gamecult/idunn/sources"), - staging_root: PathBuf::from("/var/lib/gamecult/idunn/staging"), - topology_store: PathBuf::from("/var/lib/gamecult/idunn/topology.cc"), - odin_correlation_store: PathBuf::from( - "/var/lib/gamecult/odin/idunn-runtime-topology.cc", - ), - odin_trust_anchor: PathBuf::from("/etc/gamecult/idunn/odin-topology-anchor.cc"), - idunn_identity_store: PathBuf::from( - "/var/lib/gamecult/idunn/idunn-service-identity.cc", - ), - deployment_brake_operator_anchor: PathBuf::from( - "/etc/gamecult/idunn/deployment-brake-operator-anchor.cc", - ), - source_identity: None, - topology_maximum_age_millis: DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS, - topology_maximum_future_skew_millis: DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS, - poll_millis: 500, - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -enum Command { - Serve(RuntimeOptions), - Up { - selector: String, - requested_by: String, - state_store: PathBuf, - wait: bool, - timeout_seconds: u64, - }, - Status { - state_store: PathBuf, - command_id: Option, - }, -} - -pub fn run(args: impl Iterator) -> Result<()> { - match parse(args)? { - Command::Serve(options) => serve(options), - Command::Up { - selector, - requested_by, - state_store, - wait, - timeout_seconds, - } => submit( - &state_store, - &selector, - &requested_by, - wait, - timeout_seconds, - ), - Command::Status { - state_store, - command_id, - } => status(&state_store, command_id.as_deref()), - } -} - -fn parse(args: impl Iterator) -> Result { - let mut args = args.peekable(); - let command = args.next().ok_or_else(|| anyhow!(usage()))?; - match command.as_str() { - "serve" => parse_serve(args), - "up" => parse_up(args), - "status" => parse_status(args), - "--help" | "-h" | "help" => bail!(usage()), - _ => bail!("unknown Idunn command {command:?}\n\n{}", usage()), - } -} - -fn parse_serve(mut args: impl Iterator) -> Result { - let mut options = RuntimeOptions::default(); - let mut source_uid = None; - let mut source_gid = None; - while let Some(argument) = args.next() { - match argument.as_str() { - "--state-store" => options.state_store = path_value(&mut args, &argument)?, - "--bindings-dir" => options.bindings_dir = path_value(&mut args, &argument)?, - "--source-root" => options.source_root = path_value(&mut args, &argument)?, - "--staging-root" => options.staging_root = path_value(&mut args, &argument)?, - "--topology-store" => options.topology_store = path_value(&mut args, &argument)?, - "--odin-correlation-store" => { - options.odin_correlation_store = path_value(&mut args, &argument)? - } - "--odin-trust-anchor" => options.odin_trust_anchor = path_value(&mut args, &argument)?, - "--idunn-identity-store" => { - options.idunn_identity_store = path_value(&mut args, &argument)? - } - "--deployment-brake-operator-anchor" => { - options.deployment_brake_operator_anchor = path_value(&mut args, &argument)? - } - "--topology-maximum-age-millis" => { - options.topology_maximum_age_millis = u64_value(&mut args, &argument)? - } - "--topology-maximum-future-skew-millis" => { - options.topology_maximum_future_skew_millis = u64_value(&mut args, &argument)? - } - "--source-uid" => source_uid = Some(u32_value(&mut args, &argument)?), - "--source-gid" => source_gid = Some(u32_value(&mut args, &argument)?), - "--poll-millis" => options.poll_millis = u64_value(&mut args, &argument)?, - "--help" | "-h" => bail!(usage()), - _ => bail!("unknown Idunn serve option {argument:?}"), - } - } - ensure!( - options.poll_millis > 0 && options.topology_maximum_age_millis > 0, - "poll and topology maximum age must be positive" - ); - options.source_identity = match (source_uid, source_gid) { - (Some(uid), Some(gid)) => { - ensure!( - uid > 0 && gid > 0, - "source UID and GID must be unprivileged" - ); - Some(ProcessIdentity { uid, gid }) - } - (None, None) => None, - _ => bail!("--source-uid and --source-gid must be supplied together"), - }; - Ok(Command::Serve(options)) -} - -fn parse_up(mut args: impl Iterator) -> Result { - let selector = args - .next() - .ok_or_else(|| anyhow!("idunn up requires a service or profile selector"))?; - require_selector(&selector)?; - let mut state_store = RuntimeOptions::default().state_store; - let mut requested_by = env::var("SUDO_USER") - .or_else(|_| env::var("USER")) - .or_else(|_| env::var("USERNAME")) - .unwrap_or_else(|_| "operator".into()); - let mut wait = true; - let mut timeout_seconds = 1800; - while let Some(argument) = args.next() { - match argument.as_str() { - "--state-store" => state_store = path_value(&mut args, &argument)?, - "--requested-by" => requested_by = string_value(&mut args, &argument)?, - "--no-wait" => wait = false, - "--timeout-seconds" => timeout_seconds = u64_value(&mut args, &argument)?, - "--help" | "-h" => bail!(usage()), - _ => bail!("unknown Idunn up option {argument:?}"), - } - } - ensure!(timeout_seconds > 0, "--timeout-seconds must be positive"); - require_value(&requested_by, "deployment requester")?; - Ok(Command::Up { - selector, - requested_by, - state_store, - wait, - timeout_seconds, - }) -} - -fn parse_status(mut args: impl Iterator) -> Result { - let mut state_store = RuntimeOptions::default().state_store; - let mut command_id = None; - while let Some(argument) = args.next() { - match argument.as_str() { - "--state-store" => state_store = path_value(&mut args, &argument)?, - "--command" => command_id = Some(string_value(&mut args, &argument)?), - "--help" | "-h" => bail!(usage()), - _ => bail!("unknown Idunn status option {argument:?}"), - } - } - Ok(Command::Status { - state_store, - command_id, - }) -} - -#[derive(Clone)] -struct Stored { - envelope: CultCacheEnvelope, - value: T, -} - -#[derive(Default)] -struct ControlSnapshot { - commands: Vec>, - transactions: Vec>, - admitted: Vec>, -} - -impl ControlSnapshot { - fn read(path: &Path) -> Result { - if !path.exists() { - return Ok(Self::default()); - } - let mut snapshot = Self::default(); - for envelope in SingleFileMessagePackBackingStore::new(path) - .pull_all_read_only_snapshot() - .context("reading Idunn control snapshot")? - { - match envelope.r#type.as_str() { - DeploymentCommand::TYPE => { - ensure!( - envelope.schema_id.as_deref() == Some(DEPLOYMENT_COMMAND_SCHEMA), - "Idunn control store contains an unsupported command" - ); - let value: DeploymentCommand = decode_record(&envelope)?; - value.validate()?; - ensure!( - envelope.key == value.command_id, - "deployment command key differs from its identity" - ); - snapshot.commands.push(Stored { envelope, value }); - } - DeploymentTransaction::TYPE => { - ensure!( - envelope.schema_id.as_deref() == Some(DEPLOYMENT_TRANSACTION_SCHEMA), - "Idunn control store contains an unsupported transaction" - ); - let value: DeploymentTransaction = decode_record(&envelope)?; - value.validate()?; - ensure!( - envelope.key == value.transaction_id, - "deployment transaction key differs from its identity" - ); - snapshot.transactions.push(Stored { envelope, value }); - } - AdmittedGeneration::TYPE => { - ensure!( - envelope.schema_id.as_deref() == Some(ADMITTED_GENERATION_SCHEMA), - "Idunn control store contains an unsupported admitted generation" - ); - let value: AdmittedGeneration = decode_record(&envelope)?; - value.validate()?; - ensure!( - envelope.key == value.target, - "admitted generation key is not its target" - ); - snapshot.admitted.push(Stored { envelope, value }); - } - _ => bail!("Idunn control store contains a foreign document"), - } - } - snapshot.validate_relations()?; - Ok(snapshot) - } - - fn validate_relations(&self) -> Result<()> { - let command_ids = self - .commands - .iter() - .map(|stored| stored.value.command_id.as_str()) - .collect::>(); - ensure!( - command_ids.len() == self.commands.len(), - "Idunn control store contains duplicate commands" - ); - let transaction_ids = self - .transactions - .iter() - .map(|stored| stored.value.transaction_id.as_str()) - .collect::>(); - ensure!( - transaction_ids.len() == self.transactions.len(), - "Idunn control store contains duplicate transactions" - ); - let admitted_targets = self - .admitted - .iter() - .map(|stored| stored.value.target.as_str()) - .collect::>(); - ensure!( - admitted_targets.len() == self.admitted.len(), - "Idunn control store contains duplicate current generations" - ); - for transaction in &self.transactions { - let command = self - .commands - .iter() - .find(|candidate| candidate.value.command_id == transaction.value.command_id) - .context("deployment transaction has no immutable command")?; - ensure!( - command.value.kind == transaction.value.command_kind, - "transaction kind differs from its immutable command" - ); - } - let mut live_targets = BTreeSet::new(); - for transaction in self - .transactions - .iter() - .filter(|stored| stored.value.owns_target_authority()) - { - ensure!( - live_targets.insert(transaction.value.target.as_str()), - "multiple transactions claim current-incarnation authority for one target" - ); - if let Some(fencing) = transaction - .value - .fencing - .as_ref() - .filter(|_| transaction.value.phase < DeploymentPhase::Complete) - { - let incumbent = self.admitted_for(&transaction.value.target); - let incumbent_matches = - match (&transaction.value.incumbent_generation_id, incumbent) { - (None, None) => true, - (Some(expected), Some(current)) => current.value.generation_id == *expected, - _ => false, - }; - ensure!( - incumbent_matches, - "fenced transaction incumbent differs from current admission" - ); - let incumbent_lease_sha256 = incumbent - .and_then(|stored| stored.value.leasing.lease()) - .map(IdunnProcessWriteLeaseRecord::canonical_sha256) - .transpose()?; - match fencing { - FencingEvidence::SkippedStateless => ensure!( - incumbent_lease_sha256.is_none(), - "stateless fencing omitted the admitted incumbent lease" - ), - FencingEvidence::Revoked { - incumbent_lease_sha256: recorded, - .. - } => { - ensure!( - recorded == &incumbent_lease_sha256, - "fencing evidence names another incumbent lease" - ); - ensure!( - incumbent_lease_sha256.is_some() - || transaction - .value - .expected - .as_ref() - .is_some_and(|expected| expected.write_lease_required), - "stateless transaction did not use its explicit fencing skip" - ); - } - } - } - } - let mut authorizations = BTreeSet::new(); - for authorization in self - .transactions - .iter() - .filter_map(|stored| stored.value.deployment_authorization.as_ref()) - { - ensure!( - authorizations.insert(authorization.authorization_id.as_str()), - "deployment authorization was consumed more than once" - ); - } - Ok(()) - } - - fn admitted_for(&self, target: &str) -> Option<&Stored> { - self.admitted - .iter() - .find(|stored| stored.value.target == target) - } - - fn transaction_for_command(&self, command_id: &str) -> Vec<&DeploymentTransaction> { - let mut values = self - .transactions - .iter() - .filter(|stored| stored.value.command_id == command_id) - .map(|stored| &stored.value) - .collect::>(); - values.sort_by_key(|value| value.ordinal); - values - } - - fn has_earlier_authority_sibling(&self, transaction: &DeploymentTransaction) -> bool { - self.transactions.iter().any(|stored| { - stored.value.owns_target_authority() - && stored.value.command_id == transaction.command_id - && stored.value.ordinal < transaction.ordinal - }) - } - - fn max_odin_sequence(&self, target: &str, signer_identity_id: &str) -> u64 { - let transaction_max = self - .transactions - .iter() - .filter_map(|stored| { - stored - .value - .latest_odin_observation - .as_ref() - .filter(|evidence| { - stored.value.target == target - && evidence.signer_identity_id == signer_identity_id - }) - .map(|evidence| evidence.publisher_sequence) - }) - .max() - .unwrap_or(0); - let admitted_max = self - .admitted - .iter() - .filter(|stored| { - stored.value.target == target - && stored.value.odin_authority.signer_identity_id == signer_identity_id - }) - .map(|stored| stored.value.odin_publisher_sequence_cursor) - .max() - .unwrap_or(0); - transaction_max.max(admitted_max) - } -} - -fn decode_record(envelope: &CultCacheEnvelope) -> Result -where - T: for<'de> Deserialize<'de> + Serialize, -{ - let value: T = rmp_serde::from_slice(&envelope.payload)?; - ensure!( - rmp_serde::to_vec(&value)? == envelope.payload, - "Idunn control store contains a noncanonical record" - ); - Ok(value) -} - -fn command_envelope(value: &DeploymentCommand, now: u64) -> Result { - value.validate()?; - typed_envelope( - &value.command_id, - DeploymentCommand::TYPE, - DEPLOYMENT_COMMAND_SCHEMA, - value, - now, - ) -} - -fn transaction_envelope(value: &DeploymentTransaction, now: u64) -> Result { - value.validate()?; - typed_envelope( - &value.transaction_id, - DeploymentTransaction::TYPE, - DEPLOYMENT_TRANSACTION_SCHEMA, - value, - now, - ) -} - -fn admitted_envelope(value: &AdmittedGeneration, now: u64) -> Result { - value.validate()?; - typed_envelope( - &value.target, - AdmittedGeneration::TYPE, - ADMITTED_GENERATION_SCHEMA, - value, - now, - ) -} - -fn typed_envelope( - key: &str, - record_type: &str, - schema: &str, - value: &T, - now: u64, -) -> Result { - Ok(CultCacheEnvelope { - key: key.into(), - r#type: record_type.into(), - payload: rmp_serde::to_vec(value)?, - stored_at: rfc3339_millis(now)?, - schema_id: Some(schema.into()), - }) -} - -fn replace_transaction( - store_path: &Path, - current: &Stored, - next: &DeploymentTransaction, -) -> Result<()> { - next.validate()?; - ensure!( - current.value.transaction_id == next.transaction_id, - "transaction replacement changes identity" - ); - ensure!( - SingleFileMessagePackBackingStore::new(store_path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentTransaction::TYPE.into(), - key: current.value.transaction_id.clone(), - current: Some(current.envelope.clone()), - }], - &[transaction_envelope(next, next.updated_at_unix_millis)?], - )?, - "deployment transaction changed before its compare-exchange" - ); - Ok(()) -} - -#[derive(Clone)] -struct LoadedBinding { - binding: OperatorBinding, - bytes: Vec, -} - -fn load_bindings(directory: &Path) -> Result> { - let mut paths = fs::read_dir(directory) - .with_context(|| format!("reading Idunn binding directory {}", directory.display()))? - .map(|entry| entry.map(|entry| entry.path())) - .collect::>>()?; - paths.retain(|path| path.extension() == Some(std::ffi::OsStr::new("toml"))); - paths.sort(); - let mut bindings = BTreeMap::new(); - for path in paths { - let bytes = fs::read(&path) - .with_context(|| format!("reading operator binding {}", path.display()))?; - let text = std::str::from_utf8(&bytes) - .with_context(|| format!("operator binding {} is not UTF-8", path.display()))?; - let binding = OperatorBinding::parse(text) - .with_context(|| format!("validating operator binding {}", path.display()))?; - let target = binding.target.clone(); - ensure!( - bindings - .insert(target.clone(), LoadedBinding { binding, bytes }) - .is_none(), - "operator binding target {target} is duplicated" - ); - } - ensure!(!bindings.is_empty(), "Idunn binding directory is empty"); - validate_route_bindings(&bindings)?; - Ok(bindings) -} - -fn validate_route_bindings(bindings: &BTreeMap) -> Result<()> { - let routes = bindings - .iter() - .filter_map(|(target, loaded)| { - loaded - .binding - .route - .as_ref() - .map(|route| (target.as_str(), route)) - }) - .collect::>(); - validate_route_binding_set(&routes) -} - -fn validate_route_binding_set(routes: &[(&str, &RouteBinding)]) -> Result<()> { - for (index, (target, route)) in routes.iter().enumerate() { - let (stable_host, stable_port) = route.stable_socket()?; - let private_host = route - .private_host - .parse::() - .context("validated route private host stopped being an IP address")?; - ensure!( - stable_host != private_host - || !(route.private_port_start..=route.private_port_end).contains(&stable_port), - "route {target} stable socket overlaps its candidate port range" - ); - - for (other_target, other) in &routes[index + 1..] { - ensure!( - route.route_id != other.route_id, - "route id {} is shared by targets {target} and {other_target}", - route.route_id - ); - ensure!( - route.config_path != other.config_path, - "route fragment {} is shared by targets {target} and {other_target}", - route.config_path.display() - ); - let other_stable = other.stable_socket()?; - ensure!( - route.driver != other.driver || (stable_host, stable_port) != other_stable, - "stable route socket {}:{} is shared by targets {target} and {other_target}", - stable_host, - stable_port - ); - if route.driver != other.driver { - continue; - } - let other_private_host = other - .private_host - .parse::() - .context("validated route private host stopped being an IP address")?; - let ranges_overlap = route.private_host == other.private_host - && route.private_port_start <= other.private_port_end - && other.private_port_start <= route.private_port_end; - ensure!( - !ranges_overlap, - "candidate port ranges overlap for targets {target} and {other_target}" - ); - ensure!( - stable_host != other_private_host - || !(other.private_port_start..=other.private_port_end).contains(&stable_port), - "stable route socket for {target} overlaps {other_target}'s candidate range" - ); - ensure!( - other_stable.0 != private_host - || !(route.private_port_start..=route.private_port_end) - .contains(&other_stable.1), - "stable route socket for {other_target} overlaps {target}'s candidate range" - ); - } - } - Ok(()) -} - -fn resolve_selector( - bindings: &BTreeMap, - selector: &str, -) -> Result> { - let mut targets = if let Some(profile) = selector.strip_prefix("profile:") { - bindings - .values() - .filter(|binding| binding.binding.profiles.contains(profile)) - .map(|binding| binding.binding.target.clone()) - .collect::>() - } else { - ensure!( - bindings.contains_key(selector), - "unknown deployment target {selector}" - ); - vec![selector.to_owned()] - }; - ensure!( - !targets.is_empty(), - "unknown or empty deployment profile {selector}" - ); - targets.sort_by(|left, right| { - (left != "odin") - .cmp(&(right != "odin")) - .then_with(|| left.cmp(right)) - }); - Ok(targets) -} - -fn submit( - store_path: &Path, - selector: &str, - requested_by: &str, - wait: bool, - timeout_seconds: u64, -) -> Result<()> { - if let Some(parent) = store_path.parent() { - fs::create_dir_all(parent)?; - } - let now = now_millis()?; - let command = DeploymentCommand { - schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: format!("up-{}", Uuid::new_v4()), - kind: CommandKind::Deploy, - selector: selector.into(), - requested_by: requested_by.into(), - requested_at_unix_millis: now, - }; - command.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(store_path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentCommand::TYPE.into(), - key: command.command_id.clone(), - current: None, - }], - &[command_envelope(&command, now)?], - )?, - "deployment command id collided" - ); - println!("{}", command.command_id); - if !wait { - return Ok(()); - } - let deadline = now.saturating_add(timeout_seconds.saturating_mul(1000)); - loop { - let snapshot = ControlSnapshot::read(store_path)?; - ensure!( - snapshot - .commands - .iter() - .any(|stored| stored.value.command_id == command.command_id), - "submitted deployment command disappeared" - ); - let transactions = snapshot.transaction_for_command(&command.command_id); - if !transactions.is_empty() && transactions.iter().all(|value| value.is_terminal()) { - if let Some(error) = - transactions - .iter() - .find_map(|transaction| match &transaction.completion { - Some(TransactionCompletion::FailedBeforeFencing { error }) => { - Some(error.as_str()) - } - _ => None, - }) - { - bail!("deployment failed: {error}") - } - println!("succeeded {} target(s)", transactions.len()); - return Ok(()); - } - if now_millis()? >= deadline { - bail!("deployment command timed out") - } - thread::sleep(Duration::from_millis(250)); - } -} - -fn status(store_path: &Path, command_id: Option<&str>) -> Result<()> { - let snapshot = ControlSnapshot::read(store_path)?; - let mut commands = snapshot.commands.iter().collect::>(); - commands.sort_by_key(|stored| stored.value.requested_at_unix_millis); - if let Some(command_id) = command_id { - commands.retain(|stored| stored.value.command_id == command_id); - ensure!(!commands.is_empty(), "deployment command is unknown"); - } - for stored in commands { - let command = &stored.value; - let transactions = snapshot.transaction_for_command(&command.command_id); - let (state, detail) = derived_command_status(&transactions); - println!( - "{} {} {} {}", - command.command_id, command.selector, state, detail - ); - } - Ok(()) -} - -fn derived_command_status(transactions: &[&DeploymentTransaction]) -> (&'static str, String) { - if transactions.is_empty() { - return ("queued", String::new()); - } - if let Some(error) = transactions - .iter() - .find_map(|transaction| match &transaction.completion { - Some(TransactionCompletion::FailedBeforeFencing { error }) => Some(error.clone()), - _ => None, - }) - { - return ("failed", error); - } - if transactions.iter().all(|transaction| { - transaction.is_terminal() - && matches!( - transaction.completion, - Some(TransactionCompletion::Admitted { .. }) - ) - }) { - return ( - "succeeded", - transactions - .iter() - .map(|transaction| transaction.target.as_str()) - .collect::>() - .join(","), - ); - } - let current = transactions - .iter() - .filter(|transaction| !transaction.is_terminal()) - .map(|transaction| format!("{}:{:?}", transaction.target, transaction.phase)) - .collect::>() - .join(","); - ("running", current) -} - -struct ProcessLock { - file: File, - #[cfg(not(unix))] - path: PathBuf, -} - -impl ProcessLock { - fn acquire(store_path: &Path) -> Result { - let path = sibling_path(store_path, ".daemon.lock"); - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - #[cfg(unix)] - { - use std::os::fd::AsRawFd; - let file = OpenOptions::new() - .create(true) - .read(true) - .write(true) - .open(&path) - .with_context(|| format!("opening Idunn daemon lock {}", path.display()))?; - let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; - ensure!(result == 0, "another Idunn daemon owns this control store"); - Ok(Self { file }) - } - #[cfg(not(unix))] - { - let file = OpenOptions::new() - .create_new(true) - .read(true) - .write(true) - .open(&path) - .with_context(|| format!("another Idunn daemon owns {}", path.display()))?; - Ok(Self { file, path }) - } - } -} - -impl Drop for ProcessLock { - fn drop(&mut self) { - #[cfg(unix)] - { - use std::os::fd::AsRawFd; - let _ = unsafe { libc::flock(self.file.as_raw_fd(), libc::LOCK_UN) }; - } - #[cfg(not(unix))] - { - let _ = fs::remove_file(&self.path); - } - } -} - -struct Engine { - options: RuntimeOptions, - idunn_signer: ServiceIdentitySigner, - idunn_anchor: ServiceIdentityTrustAnchor, - bootstrap_odin_authority: AdmittedOdinAuthority, - source: GitSourceDriver, - runner: DockerRunnerDriver, - workload: SystemdTransientWorkloadDriver, -} - -impl Engine { - fn open(options: RuntimeOptions) -> Result { - let idunn_signer = - open_service_identity_at::(&options.idunn_identity_store) - .context("opening Idunn activation identity")?; - let idunn_anchor = idunn_signer.trust_anchor()?; - let odin_anchor = read_trust_anchor::(&options.odin_trust_anchor) - .context("reading bootstrap Odin topology anchor")?; - let bootstrap_odin_authority = AdmittedOdinAuthority::from_anchor(&odin_anchor)?; - let source = GitSourceDriver::new( - &options.source_root, - options.staging_root.join("frozen-sources"), - options.source_identity, - ); - Ok(Self { - options, - idunn_signer, - idunn_anchor, - bootstrap_odin_authority, - source, - runner: DockerRunnerDriver::default(), - workload: SystemdTransientWorkloadDriver::default(), - }) - } - - fn topology(&self) -> CultCacheTopologyDriver { - CultCacheTopologyDriver { - projection_store: self.options.topology_store.clone(), - correlation_store: self.options.odin_correlation_store.clone(), - } - } - - fn current_odin_authority(&self, snapshot: &ControlSnapshot) -> Result { - let authority = snapshot - .admitted_for("odin") - .map(|stored| stored.value.odin_authority.clone()) - .unwrap_or_else(|| self.bootstrap_odin_authority.clone()); - authority.validate()?; - Ok(authority) - } - - fn trusted_topology_context(&self, now: u64) -> OdinTopologyAuthenticationContext { - OdinTopologyAuthenticationContext { - trusted_received_at_unix_millis: now, - maximum_age_millis: self.options.topology_maximum_age_millis, - maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, - } - } - - fn validate_durable_authority(&self, snapshot: &ControlSnapshot) -> Result<()> { - let operator_anchor = read_trust_anchor::( - &self.options.deployment_brake_operator_anchor, - )?; - for stored in &snapshot.transactions { - let transaction = &stored.value; - if let Some(authorization) = &transaction.deployment_authorization { - authorization.validate_shape()?; - let record: IdunnDeploymentBrakeRecord = - rmp_serde::from_slice(&authorization.canonical_brake_bytes)?; - verify_idunn_deployment_brake_authorization(&record, &operator_anchor)?; - let expected = required(&transaction.expected, "authorized Expected projection")?; - ensure!( - record.authorized_release_id.as_deref() - == Some(expected.sealed_release_id.as_str()) - && record.authorized_deployment_id.as_deref() - == Some(transaction.transaction_id.as_str()) - && record.runtime_id == expected.runtime_id, - "durable deployment authorization names another release or transaction" - ); - } - let lease = transaction - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - if let Some(evidence) = &transaction.latest_odin_observation { - let authenticated = self.authenticate_topology_bytes( - snapshot, - transaction, - &evidence.canonical_bytes, - lease, - evidence.admitted_at_unix_millis, - )?; - validate_authenticated_evidence(evidence, &authenticated)?; - } - if let Some(evidence) = &transaction.warming { - match evidence { - WarmingEvidence::OdinTopology { evidence } => { - let authenticated = self.authenticate_topology_bytes( - snapshot, - transaction, - &evidence.canonical_bytes, - None, - evidence.admitted_at_unix_millis, - )?; - validate_authenticated_evidence(evidence, &authenticated)?; - let incumbent_lease_sha256 = - self.incumbent_lease_sha256_for_warming(snapshot, transaction)?; - ensure!( - is_semantic_warming( - required(&transaction.expected, "Warming Expected projection",)?, - required(&transaction.activation, "Warming activation")?, - incumbent_lease_sha256.as_deref(), - &authenticated, - )?, - "durable Warming gate is not supported by current runtime evidence" - ); - } - WarmingEvidence::FirstOdinDirect { evidence } => { - self.authenticate_first_odin_warming_presence( - transaction, - &evidence.message_id, - evidence.challenged_at_unix_millis, - evidence.admitted_at_unix_millis, - &evidence.canonical_bytes, - )?; - } - } - } - if let Some(evidence) = &transaction.ready { - let authenticated = self.authenticate_topology_bytes( - snapshot, - transaction, - &evidence.canonical_bytes, - lease, - evidence.admitted_at_unix_millis, - )?; - validate_authenticated_evidence(evidence, &authenticated)?; - ensure!( - is_semantic_ready(&authenticated), - "durable Ready label is not exact semantic Ready" - ); - } - } - let odin_authority = self.current_odin_authority(snapshot)?; - for stored in &snapshot.admitted { - let generation = &stored.value; - let authority = self.runtime_authority_parts( - &generation.plan, - &generation.expected, - &generation.activation, - )?; - let latest = authenticate_odin_runtime_topology_correlation( - &generation.latest_odin_observation.canonical_bytes, - &authority, - generation.leasing.lease_sha256(), - &odin_authority.signer_public_key, - self.trusted_topology_context( - generation.latest_odin_observation.admitted_at_unix_millis, - ), - )?; - validate_authenticated_evidence(&generation.latest_odin_observation, &latest)?; - let ready = authenticate_odin_runtime_topology_correlation( - &generation.ready.canonical_bytes, - &authority, - generation.leasing.lease_sha256(), - &odin_authority.signer_public_key, - self.trusted_topology_context(generation.ready.admitted_at_unix_millis), - )?; - validate_authenticated_evidence(&generation.ready, &ready)?; - ensure!( - is_semantic_ready(&ready), - "admitted generation Ready label is not exact semantic Ready" - ); - } - Ok(()) - } -} - -fn serve(options: RuntimeOptions) -> Result<()> { - for path in [ - &options.state_store, - &options.topology_store, - &options.staging_root, - ] { - let directory = if path.extension().is_some() { - path.parent() - .context("configured Idunn path has no parent")? - } else { - path.as_path() - }; - fs::create_dir_all(directory) - .with_context(|| format!("creating Idunn directory {}", directory.display()))?; - } - let _lock = ProcessLock::acquire(&options.state_store)?; - ControlSnapshot::read(&options.state_store).context("validating all Idunn records")?; - let engine = Engine::open(options)?; - engine.validate_durable_authority(&ControlSnapshot::read(&engine.options.state_store)?)?; - - loop { - let transaction_progress = engine.resume_one_transaction()?; - let continuity_progress = engine.supervise_one_admitted_generation()?; - if !transaction_progress && !continuity_progress && engine.freeze_one_queued_command()? { - continue; - } - thread::sleep(Duration::from_millis(engine.options.poll_millis)); - } -} - -impl Engine { - /// Startup and every later loop use the same order: unfinished ownership - /// work first, admitted-body continuity second, new commands last. A - /// waiting transaction yields without relaxing ordinal order inside its - /// own command, so one target brake cannot suspend unrelated continuity. - fn resume_one_transaction(&self) -> Result { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let mut progressed = false; - let mut candidates = snapshot - .transactions - .iter() - .filter(|stored| !stored.value.is_terminal()) - .collect::>(); - candidates.sort_by_key(|stored| { - ( - stored.value.created_at_unix_millis, - stored.value.ordinal, - stored.value.transaction_id.as_str(), - ) - }); - for current in candidates { - if snapshot.has_earlier_authority_sibling(¤t.value) { - continue; - } - if let Err(error) = self.advance_transaction(current) { - let latest_snapshot = ControlSnapshot::read(&self.options.state_store)?; - let latest = latest_snapshot - .transactions - .iter() - .find(|stored| stored.value.transaction_id == current.value.transaction_id) - .context("transaction disappeared while recording an execution error")?; - if latest.value.is_terminal() { - progressed = true; - } else if latest.value.phase < DeploymentPhase::Fencing - && latest.value.pre_fencing_abort.is_none() - { - self.begin_pre_fencing_abort(latest, error)?; - } else { - self.record_resumable_error(latest, &error)?; - } - } - let after = ControlSnapshot::read(&self.options.state_store)?; - let live = after - .transactions - .iter() - .find(|stored| stored.value.transaction_id == current.value.transaction_id) - .context("transaction disappeared while checking scheduler progress")?; - if live.envelope != current.envelope { - progressed = true; - } - } - Ok(progressed) - } - - fn freeze_one_queued_command(&self) -> Result { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let transaction_commands = snapshot - .transactions - .iter() - .map(|stored| stored.value.command_id.as_str()) - .collect::>(); - let mut queued = snapshot - .commands - .iter() - .filter(|stored| { - stored.value.kind == CommandKind::Deploy - && !transaction_commands.contains(stored.value.command_id.as_str()) - }) - .collect::>(); - queued.sort_by_key(|stored| stored.value.requested_at_unix_millis); - let Some(command) = queued.first() else { - return Ok(false); - }; - let bindings = match load_bindings(&self.options.bindings_dir) { - Ok(bindings) => bindings, - Err(error) => { - eprintln!( - "Idunn left command {} queued because operator bindings are invalid: {error:#}", - command.value.command_id - ); - return Ok(false); - } - }; - let targets = match resolve_selector(&bindings, &command.value.selector) { - Ok(targets) => targets, - Err(error) => { - let now = now_millis()?; - let rejected = DeploymentTransaction::rejected(&command.value, error, now)?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store) - .compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentTransaction::TYPE.into(), - key: rejected.transaction_id.clone(), - current: None, - }], - &[transaction_envelope(&rejected, now)?], - )?, - "bad selector changed before refusal was recorded" - ); - return Ok(true); - } - }; - let busy = snapshot - .transactions - .iter() - .filter(|stored| stored.value.blocks_new_target_mutation()) - .map(|stored| stored.value.target.as_str()) - .collect::>(); - if targets.iter().any(|target| busy.contains(target.as_str())) { - return Ok(false); - } - let now = now_millis()?; - let transactions = targets - .into_iter() - .enumerate() - .map(|(ordinal, target)| { - DeploymentTransaction::new( - &command.value, - target.clone(), - u32::try_from(ordinal)?, - snapshot.admitted_for(&target).map(|stored| &stored.value), - now, - ) - }) - .collect::>>()?; - let expected = transactions - .iter() - .map(|transaction| CultCacheExpectedEnvelope { - r#type: DeploymentTransaction::TYPE.into(), - key: transaction.transaction_id.clone(), - current: None, - }) - .collect::>(); - let next = transactions - .iter() - .map(|transaction| transaction_envelope(transaction, now)) - .collect::>>()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store) - .compare_exchange(&expected, &next)?, - "queued command lost its ordered transaction creation CAS" - ); - Ok(true) - } - - fn supervise_one_admitted_generation(&self) -> Result { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let mut progressed = false; - let mut admitted = snapshot.admitted.iter().collect::>(); - admitted.sort_by_key(|stored| stored.value.target.as_str()); - for current in admitted { - let blocker = snapshot.transactions.iter().find(|stored| { - stored.value.target == current.value.target - && stored.value.blocks_new_target_mutation() - }); - if blocker.is_some_and(|stored| { - stored.value.phase >= DeploymentPhase::Fencing - && stored.value.phase < DeploymentPhase::Complete - }) { - continue; - } - match self.supervise_admitted_route(current) { - Ok(true) => { - progressed = true; - continue; - } - Ok(false) => {} - Err(error) => eprintln!( - "Idunn rejected admitted {} route continuity: {error:#}", - current.value.target - ), - } - match self.refresh_admitted_topology(&snapshot, current) { - Ok(true) => { - progressed = true; - continue; - } - Ok(false) => {} - Err(error) => eprintln!( - "Idunn preserved admitted {} after rejecting topology observation: {error:#}", - current.value.target - ), - } - let mut operational_error = None; - let observation = match self.workload.observe( - ¤t.value.expected, - ¤t.value.activation, - ¤t.value.workload, - ) { - Ok(observation) => { - let lease_is_missing = if let Some(lease) = current.value.leasing.lease() { - let lease_health = (|| -> Result { - let lease_path = current - .value - .plan - .parsed_inputs()? - .1 - .process_write_lease - .context("admitted lease has no operator binding")? - .record_path; - let driver = - CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); - if driver.observe_exact(lease)? { - return Ok(true); - } - if driver.observe_empty()? { - return Ok(false); - } - bail!("write-lease store contains unexpected authority") - })(); - match lease_health { - Ok(true) => false, - Ok(false) => { - operational_error = - Some(anyhow!("admitted physical write lease is missing")); - true - } - Err(error) => { - eprintln!( - "Idunn preserved admitted {} after refusing an unsafe write-lease mutation: {error:#}", - current.value.target - ); - continue; - } - } - } else { - false - }; - if lease_is_missing { - None - } else { - Some(observation) - } - } - Err(error) => { - operational_error = Some(error); - None - } - }; - if let Some(observation) = observation { - if blocker.is_none() - || blocker.is_some_and(|stored| stored.value.phase == DeploymentPhase::Complete) - { - let repair = (|| -> Result<()> { - let topology = self.topology(); - let provider_anchor = self.provider_anchor_for_plan(¤t.value.plan)?; - if !topology.admitted_runtime_projection_is_exact( - ¤t.value.expected, - &provider_anchor, - ¤t.value.activation, - current.value.leasing.lease(), - )? { - ensure!( - topology - .publish_expected(¤t.value.expected, &provider_anchor,)? - == current.value.expected.canonical_sha256()?, - "admitted Expected projection repair differs" - ); - ensure!( - topology.publish_observed_activation( - ¤t.value.expected, - ¤t.value.activation, - &observation, - )? == current.value.activation.canonical_sha256()?, - "admitted activation projection repair differs" - ); - if let Some(lease) = current.value.leasing.lease() { - ensure!( - topology.publish_process_write_lease( - ¤t.value.expected, - ¤t.value.activation, - lease, - )? == lease.canonical_sha256()?, - "admitted write-lease projection repair differs" - ); - } - } - Ok(()) - })(); - if let Err(error) = repair { - eprintln!( - "Idunn preserved admitted {} after refusing topology projection repair: {error:#}", - current.value.target - ); - } - } - continue; - } - let workload_error = operational_error - .context("admitted operational state has neither observation nor error")?; - if let Some(blocker) = blocker { - if blocker.value.phase < DeploymentPhase::Fencing - && blocker.value.pre_fencing_abort.is_none() - { - self.begin_pre_fencing_abort( - blocker, - anyhow!( - "admitted incumbent failed before candidate fencing; deployment yielded to continuity: {workload_error:#}" - ), - )?; - progressed = true; - } - continue; - } - let now = now_millis()?; - let command = DeploymentCommand { - schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: format!("continuity-{}", Uuid::new_v4()), - kind: CommandKind::Continuity, - selector: current.value.target.clone(), - requested_by: "idunn-continuity".into(), - requested_at_unix_millis: now, - }; - command.validate()?; - let transaction = - DeploymentTransaction::from_continuity(&command, ¤t.value, now)?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store) - .compare_exchange( - &[ - CultCacheExpectedEnvelope { - r#type: DeploymentCommand::TYPE.into(), - key: command.command_id.clone(), - current: None, - }, - CultCacheExpectedEnvelope { - r#type: DeploymentTransaction::TYPE.into(), - key: transaction.transaction_id.clone(), - current: None, - }, - ], - &[ - command_envelope(&command, now)?, - transaction_envelope(&transaction, now)?, - ], - )?, - "continuity scheduling lost its command/transaction CAS" - ); - progressed = true; - } - Ok(progressed) - } - - fn supervise_admitted_route(&self, current: &Stored) -> Result { - let Some(expected_route) = current.value.expected.route.as_ref() else { - ensure!( - matches!(¤t.value.routing, RoutingEvidence::SkippedUnrouted) - && current.value.route_repair_started_at_unix_millis.is_none(), - "unrouted admitted generation carries route authority" - ); - return Ok(false); - }; - let RoutingEvidence::Promoted { - observation, - promoted_at_unix_millis, - } = ¤t.value.routing - else { - bail!("routed admitted generation has no promoted route receipt") - }; - ensure!( - observation.route_id == expected_route.route_id - && observation.runtime_instance_id == current.value.activation.runtime_instance_id, - "admitted route receipt names another incarnation" - ); - let binding = current.value.plan.parsed_inputs()?.1; - let lease_driver = if let Some(lease) = current.value.leasing.lease() { - let lease_path = &binding - .process_write_lease - .as_ref() - .context("stateful admitted generation has no write-lease binding")? - .record_path; - let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); - ensure!( - driver.observe_exact(lease)?, - "admitted process write lease is no longer exact" - ); - Some(driver) - } else { - None - }; - let route_binding = binding - .route - .context("routed admitted generation has no operator route binding")?; - let driver = NginxRouteDriver::new(route_binding); - - let membership_is_exact = - driver.observe_membership(¤t.value.expected, &observation.membership_sha256)?; - let now = now_millis()?; - if membership_is_exact - && route_observation_is_current( - observation.observed_at_unix_millis, - now, - self.options.topology_maximum_age_millis, - self.options.topology_maximum_future_skew_millis, - ) - && current.value.route_repair_started_at_unix_millis.is_none() - { - return Ok(false); - } - if current.value.route_repair_started_at_unix_millis.is_none() { - let mut next = current.value.clone(); - next.route_repair_started_at_unix_millis = Some(now); - next.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store) - .compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: current.value.target.clone(), - current: Some(current.envelope.clone()), - }], - &[admitted_envelope(&next, now)?], - )?, - "admitted generation changed before route repair intent CAS" - ); - return Ok(true); - } - driver - .restore_admitted_membership(¤t.value.expected, &observation.membership_sha256)?; - let authority = self.runtime_authority_parts( - ¤t.value.plan, - ¤t.value.expected, - ¤t.value.activation, - )?; - let refreshed = self.prove_stable_route_against( - ¤t.value.expected, - ¤t.value.activation, - &authority, - current.value.leasing.lease_sha256(), - &driver, - observation.membership_sha256.clone(), - )?; - ensure!( - driver.observe_membership(¤t.value.expected, &refreshed.membership_sha256)?, - "admitted route membership changed during its continuity challenge" - ); - if let (Some(lease_driver), Some(lease)) = (&lease_driver, current.value.leasing.lease()) { - ensure!( - lease_driver.observe_exact(lease)?, - "admitted process write lease changed during its continuity challenge" - ); - } - let mut next = current.value.clone(); - next.routing = RoutingEvidence::Promoted { - observation: refreshed, - promoted_at_unix_millis: *promoted_at_unix_millis, - }; - next.route_repair_started_at_unix_millis = None; - next.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: current.value.target.clone(), - current: Some(current.envelope.clone()), - }], - &[admitted_envelope(&next, now)?], - )?, - "admitted generation changed before route continuity receipt CAS" - ); - Ok(true) - } - - fn refresh_admitted_topology( - &self, - snapshot: &ControlSnapshot, - current: &Stored, - ) -> Result { - let Some(received) = self.topology().receive(¤t.value.target)? else { - return Ok(false); - }; - ensure!( - received.target == current.value.target, - "topology transport substituted admitted target" - ); - let now = now_millis()?; - let authority = self.runtime_authority_parts( - ¤t.value.plan, - ¤t.value.expected, - ¤t.value.activation, - )?; - let odin_authority = self.current_odin_authority(snapshot)?; - let authenticated = authenticate_odin_runtime_topology_correlation( - &received.canonical_bytes, - &authority, - current.value.leasing.lease_sha256(), - &odin_authority.signer_public_key, - self.trusted_topology_context(now), - )?; - let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; - if !sequence_requires_admission( - Some(¤t.value.latest_odin_observation), - snapshot.max_odin_sequence(¤t.value.target, &evidence.signer_identity_id), - &evidence, - )? { - return Ok(false); - } - let mut next = current.value.clone(); - next.latest_odin_observation = evidence.clone(); - next.odin_publisher_sequence_cursor = evidence.publisher_sequence; - if is_semantic_ready(&authenticated) { - next.ready = evidence; - } - next.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: current.value.target.clone(), - current: Some(current.envelope.clone()), - }], - &[admitted_envelope(&next, now)?], - )?, - "admitted generation changed before topology sequence CAS" - ); - Ok(true) - } - - fn advance_transaction(&self, current: &Stored) -> Result<()> { - if current.value.pre_fencing_abort.is_some() && current.value.completion.is_none() { - return self.advance_pre_fencing_abort(current); - } - match current.value.phase { - DeploymentPhase::Sealing => self.advance_sealing(current), - DeploymentPhase::Starting => self.advance_starting(current), - DeploymentPhase::Warming => self.advance_warming(current), - DeploymentPhase::Fencing => self.advance_fencing(current), - DeploymentPhase::Leasing => self.advance_leasing(current), - DeploymentPhase::AwaitingReady => self.advance_awaiting_ready(current), - DeploymentPhase::Routing => self.advance_routing(current), - DeploymentPhase::Committing => self.advance_committing(current), - DeploymentPhase::Complete => self.advance_post_commit_cleanup(current), - } - } - - fn advance_sealing(&self, current: &Stored) -> Result<()> { - if current.value.plan.is_none() { - ensure!( - current.value.command_kind == CommandKind::Deploy, - "continuity transaction lost its admitted plan" - ); - let bindings = load_bindings(&self.options.bindings_dir)?; - let loaded = bindings - .get(¤t.value.target) - .context("operator binding disappeared before sealing")?; - let now = now_millis()?; - let resolved = - self.source - .resolve(&loaded.binding, ¤t.value.transaction_id, now)?; - let providers = self.current_ready_provider_tokens( - &ControlSnapshot::read(&self.options.state_store)?, - now, - )?; - let candidate_port = self.select_candidate_port(&loaded.binding)?; - let plan = compile_deployment_plan( - &resolved.recipe_bytes, - &loaded.bytes, - resolved.facts, - format!("incarnation-{}", current.value.transaction_id), - candidate_port, - now, - &providers, - )?; - return self.persist_same_phase(current, |next| { - next.plan = Some(plan); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - - if current.value.command_kind == CommandKind::Deploy - && current.value.frozen_source.is_none() - { - let now = now_millis()?; - let plan = required(¤t.value.plan, "transaction plan")?; - let receipt = self.source.freeze(¤t.value.transaction_id, plan)?; - return self.persist_same_phase(current, |next| { - next.frozen_source = Some(receipt); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - - if current.value.sealed_release.is_none() - || current.value.installed_release.is_none() - || current.value.expected.is_none() - { - ensure!( - current.value.command_kind == CommandKind::Deploy, - "continuity transaction lost its admitted release evidence" - ); - let now = now_millis()?; - let plan = required(¤t.value.plan, "transaction plan")?; - let frozen_receipt = required(¤t.value.frozen_source, "frozen source")?; - let frozen = self.source.observe_frozen(plan, frozen_receipt)?; - let materialized = - self.runner - .materialize(&frozen, plan, &self.options.staging_root, now)?; - let installed = self.workload.install(plan, &materialized)?; - let expected = materialized.release.expected_projection(plan)?; - return self.persist_same_phase(current, |next| { - next.sealed_release = Some(materialized.release); - next.installed_release = Some(installed); - next.expected = Some(expected); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - - if current.value.expected_publication_sha256.is_none() { - let now = now_millis()?; - let plan = required(¤t.value.plan, "transaction plan")?; - let expected = required(¤t.value.expected, "Expected projection")?; - let provider_anchor = self.provider_anchor_for_plan(plan)?; - let digest = self - .topology() - .publish_expected(expected, &provider_anchor)?; - return self.persist_same_phase(current, |next| { - next.expected_publication_sha256 = Some(digest); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - - let now = now_millis()?; - let mut next = current.value.clone(); - match current.value.command_kind { - CommandKind::Deploy => { - let Some(authorization) = self.deployment_authorization(current, now)? else { - return self.record_gate_wait( - current, - "deployment brake has not released this exact transaction", - ); - }; - next.deployment_authorization = Some(authorization); - } - CommandKind::Continuity => { - if !self.lifecycle_allows(current, now)? { - return self - .record_gate_wait(current, "lifecycle brake denies continuity restart"); - } - next.lifecycle_authorized_at_unix_millis = Some(now); - } - } - validate_live_providers_for_deploy(current.value.command_kind, || { - self.validate_selected_providers_current( - required(¤t.value.plan, "transaction plan")?, - now, - ) - })?; - next.phase = DeploymentPhase::Starting; - next.updated_at_unix_millis = now; - next.last_error = None; - replace_transaction(&self.options.state_store, current, &next) - } - - fn advance_starting(&self, current: &Stored) -> Result<()> { - let expected = required(¤t.value.expected, "Expected projection")?; - let plan = required(¤t.value.plan, "transaction plan")?; - let release = required(¤t.value.sealed_release, "sealed release")?; - let installed = required(¤t.value.installed_release, "installed release")?; - if current.value.activation.is_none() { - validate_live_providers_for_deploy(current.value.command_kind, || { - self.validate_selected_providers_current(plan, now_millis()?) - })?; - let now = now_millis()?; - let runtime_instance_id = runtime_instance_id(¤t.value.transaction_id)?; - let launch = IdunnRuntimeActivationLaunch::issue( - expected, - runtime_instance_id, - now, - &self.idunn_signer, - )?; - let activation = self.workload.prepare_activation(plan, expected, launch)?; - return self.persist_same_phase(current, |next| { - next.activation = Some(activation); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - if current.value.workload.is_none() { - let now = now_millis()?; - let activation = required(¤t.value.activation, "activation")?; - let observation = self - .workload - .start_prepared(plan, release, installed, expected, activation)?; - return self.persist_same_phase(current, |next| { - next.workload = Some(observation); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - if current.value.activation_publication_sha256.is_none() { - let now = now_millis()?; - let activation = required(¤t.value.activation, "activation")?; - let workload = required(¤t.value.workload, "workload")?; - self.workload.observe(expected, activation, workload)?; - let digest = self - .topology() - .publish_observed_activation(expected, activation, workload)?; - return self.persist_same_phase(current, |next| { - next.activation_publication_sha256 = Some(digest); - next.updated_at_unix_millis = now; - next.last_error = None; - Ok(()) - }); - } - let mut next = current.value.clone(); - next.phase = DeploymentPhase::Warming; - next.updated_at_unix_millis = now_millis()?; - next.last_error = None; - replace_transaction(&self.options.state_store, current, &next) - } - - fn advance_warming(&self, current: &Stored) -> Result<()> { - let expected = required(¤t.value.expected, "Expected projection")?; - let activation = required(¤t.value.activation, "activation")?; - let workload = required(¤t.value.workload, "workload")?; - self.workload.observe(expected, activation, workload)?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent_lease_sha256 = - self.incumbent_lease_sha256_for_warming(&snapshot, ¤t.value)?; - if current.value.warming.is_none() { - if current.value.target == "odin" && snapshot.admitted_for("odin").is_none() { - ensure!( - current.value.incumbent_generation_id.is_none() - && expected.write_lease_required, - "first Odin bootstrap must be a stateful first incarnation" - ); - let (evidence, present) = self.observe_first_odin_warming(¤t.value)?; - let _token = SequenceAdmittedWarming::from_first_odin_presence( - current.value.transaction_id.clone(), - evidence.clone(), - present, - )?; - return self.persist_same_phase(current, |next| { - next.warming = Some(WarmingEvidence::FirstOdinDirect { evidence }); - Ok(()) - }); - } - let Some((admitted, authenticated)) = self.admit_latest_topology(current, None)? else { - return Ok(()); - }; - if admitted.envelope != current.envelope { - return Ok(()); - } - let semantic_warming = is_semantic_warming( - expected, - activation, - incumbent_lease_sha256.as_deref(), - &authenticated, - )?; - let latest = required( - &admitted.value.latest_odin_observation, - "sequence-admitted warming evidence", - )?; - if semantic_warming { - let evidence = latest.clone(); - let _token = SequenceAdmittedWarming::from_topology( - admitted.value.transaction_id.clone(), - evidence.clone(), - authenticated, - )?; - return self.persist_same_phase(&admitted, |next| { - next.warming = Some(WarmingEvidence::OdinTopology { evidence }); - Ok(()) - }); - } else { - return Ok(()); - } - } - let warming = self.rehydrate_warming_token(¤t.value, now_millis()?, false)?; - ensure!( - warming.transaction_id() == current.value.transaction_id - && warming.runtime_instance_id() == activation.runtime_instance_id.as_str(), - "durable Warming evidence belongs to another candidate" - ); - - if expected.route.is_some() && current.value.route_preflight.is_none() { - let now = now_millis()?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; - let incumbent_route = - incumbent.and_then(|generation| generation.value.routing.observation().cloned()); - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let route_binding = binding - .route - .context("routed Expected lost route binding")?; - let driver = NginxRouteDriver::new(route_binding); - let receipt = driver.preflight( - expected, - &activation.runtime_instance_id, - incumbent_route.as_ref(), - )?; - return self.persist_same_phase(current, |next| { - next.route_preflight = Some(receipt); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - - if current.value.isolation.is_none() { - let now = now_millis()?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; - let isolation = - prove_isolation(workload, incumbent.map(|value| &value.value.workload))?; - return self.persist_same_phase(current, |next| { - next.isolation = Some(isolation); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - - self.transition(current, DeploymentPhase::Fencing) - } - - fn advance_fencing(&self, current: &Stored) -> Result<()> { - if current.value.fencing.is_none() { - let now = now_millis()?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent = self.exact_incumbent(&snapshot, ¤t.value)?; - let incumbent_lease = incumbent.and_then(|generation| generation.value.leasing.lease()); - let expected = required(¤t.value.expected, "Expected projection")?; - let evidence = if expected.write_lease_required || incumbent_lease.is_some() { - let incumbent_lease_path = incumbent_lease - .map(|_| -> Result { - Ok(incumbent - .context("incumbent lease has no admitted generation")? - .value - .plan - .parsed_inputs()? - .1 - .process_write_lease - .context("incumbent lease has no admitted binding")? - .record_path) - }) - .transpose()?; - if let (Some(lease), Some(path)) = (incumbent_lease, &incumbent_lease_path) { - let incumbent = incumbent.context("incumbent lease lost its generation")?; - self.workload - .stop(&incumbent.value.workload) - .context("stopping the exact incumbent before revoking its lifetime-held write lease")?; - let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, path); - driver.revoke_exact(Some(lease))?; - ensure!( - driver.observe_empty()?, - "incumbent write lease remained after exact fencing" - ); - self.topology().withdraw_process_write_lease( - &incumbent.value.expected, - &incumbent.value.activation, - Some(lease), - )?; - } - let candidate_lease_path = if expected.write_lease_required { - Some( - current - .value - .plan - .as_ref() - .unwrap() - .parsed_inputs()? - .1 - .process_write_lease - .context("stateful candidate has no write-lease binding")? - .record_path, - ) - } else { - None - }; - if let Some(path) = &candidate_lease_path { - if incumbent_lease_path.as_ref() != Some(path) { - let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, path); - driver.revoke_exact(None)?; - ensure!( - driver.observe_empty()?, - "candidate write-lease path was not empty before grant" - ); - } - } - self.topology().withdraw_process_write_lease( - expected, - required(¤t.value.activation, "candidate activation")?, - None, - )?; - FencingEvidence::Revoked { - incumbent_lease_sha256: incumbent_lease - .map(IdunnProcessWriteLeaseRecord::canonical_sha256) - .transpose()?, - candidate_lease_path_verified_empty: candidate_lease_path.is_some(), - } - } else { - FencingEvidence::SkippedStateless - }; - return self.persist_same_phase(current, |next| { - next.fencing = Some(evidence); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - self.transition(current, DeploymentPhase::Leasing) - } - - fn advance_leasing(&self, current: &Stored) -> Result<()> { - let expected = required(¤t.value.expected, "Expected projection")?; - if !expected.write_lease_required { - if current.value.leasing.is_none() { - let now = now_millis()?; - return self.persist_same_phase(current, |next| { - next.leasing = Some(LeasingEvidence::SkippedStateless); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - return self.transition(current, DeploymentPhase::AwaitingReady); - } - - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let lease_path = binding - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - let driver = CultCacheWriteLeaseDriver::new(¤t.value.target, lease_path); - - if matches!( - current.value.leasing.as_ref(), - Some(LeasingEvidence::Granted { .. }) - ) { - let activation = required(¤t.value.activation, "activation")?; - let warming = self.rehydrate_warming_token(¤t.value, now_millis()?, false)?; - let lease = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease) - .context("granted Leasing evidence has no write lease")?; - let recorded_sha256 = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256) - .context("granted Leasing evidence has no lease digest")?; - ensure!( - driver.observe_exact(lease)?, - "physical write lease disappeared after Granted became durable" - ); - self.workload.observe( - expected, - activation, - required(¤t.value.workload, "candidate workload")?, - )?; - ensure!( - driver.grant(expected, activation, &warming, lease)? == recorded_sha256, - "replayed physical write lease differs from Granted evidence" - ); - ensure!( - self.topology() - .publish_process_write_lease(expected, activation, lease)? - == recorded_sha256, - "replayed write-lease projection differs from Granted evidence" - ); - return self.transition(current, DeploymentPhase::AwaitingReady); - } - - if let Some((lease, prepared_sha256)) = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::prepared_lease) - { - let now = now_millis()?; - let activation = required(¤t.value.activation, "activation")?; - let historical_warming = self.rehydrate_warming_token(¤t.value, now, false)?; - let physical_is_exact = driver.observe_exact(lease)?; - let warming = if physical_is_exact { - historical_warming - } else { - ensure!( - driver.observe_empty()?, - "candidate write-lease store contains authority other than its durable prepared lease" - ); - match self.rehydrate_warming_token(¤t.value, now, true) { - Ok(warming) => warming, - Err(_) => { - let Some((admitted, fresh_evidence, fresh_warming)) = - self.fresh_warming_for_lease(current, now)? - else { - return Ok(()); - }; - let replacement = self.prepare_candidate_write_lease( - &admitted.value, - &fresh_warming, - now, - )?; - let replacement_sha256 = replacement.canonical_sha256()?; - return self.persist_same_phase(&admitted, |next| { - next.warming = Some(fresh_evidence); - next.leasing = Some(LeasingEvidence::Prepared { - lease: replacement, - lease_sha256: replacement_sha256, - }); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - } - }; - self.workload.observe( - expected, - activation, - required(¤t.value.workload, "candidate workload")?, - )?; - if !physical_is_exact { - ensure!( - driver.observe_empty()?, - "candidate write-lease store changed before physical grant" - ); - } - let granted_sha256 = driver.grant(expected, activation, &warming, lease)?; - ensure!( - granted_sha256 == prepared_sha256, - "granted write lease differs from the durable prepared lease" - ); - let projected_sha256 = self - .topology() - .publish_process_write_lease(expected, activation, lease)?; - ensure!( - projected_sha256 == granted_sha256, - "projected write lease differs from the granted process authority" - ); - let lease = lease.clone(); - return self.persist_same_phase(current, |next| { - next.leasing = Some(LeasingEvidence::Granted { - lease, - lease_sha256: projected_sha256, - }); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - - ensure!( - current.value.leasing.is_none(), - "stateful Leasing phase carries invalid lease evidence" - ); - ensure!( - driver.observe_empty()?, - "candidate write-lease store contains authority before lease preparation" - ); - let now = now_millis()?; - let Some((admitted, fresh_evidence, fresh_warming)) = - self.fresh_warming_for_lease(current, now)? - else { - return Ok(()); - }; - let lease = self.prepare_candidate_write_lease(&admitted.value, &fresh_warming, now)?; - let lease_sha256 = lease.canonical_sha256()?; - self.persist_same_phase(&admitted, |next| { - next.warming = Some(fresh_evidence); - next.leasing = Some(LeasingEvidence::Prepared { - lease, - lease_sha256, - }); - next.updated_at_unix_millis = now; - Ok(()) - }) - } - - fn advance_awaiting_ready(&self, current: &Stored) -> Result<()> { - let now = now_millis()?; - let expected = required(¤t.value.expected, "Expected projection")?; - if expected.write_lease_required { - let activation = required(¤t.value.activation, "activation")?; - let warming = self.rehydrate_warming_token(¤t.value, now, false)?; - let lease = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease) - .context("stateful transaction has no process write lease")?; - let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let path = binding - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - ensure!( - CultCacheWriteLeaseDriver::new(¤t.value.target, path) - .observe(expected, activation, &warming, lease)?, - "candidate process write lease is no longer exact" - ); - } - { - let current_lease = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - let Some((admitted, authenticated)) = - self.admit_latest_topology(current, current_lease)? - else { - return Ok(()); - }; - if admitted.envelope != current.envelope { - return Ok(()); - } - let semantic_ready = is_semantic_ready(&authenticated); - let latest = required( - &admitted.value.latest_odin_observation, - "sequence-admitted Ready evidence", - )?; - if current.value.ready.as_ref() == Some(latest) { - ensure!(semantic_ready, "stored Ready receipt changed meaning"); - } else if semantic_ready { - let evidence = latest.clone(); - let _token = SequenceAdmittedReady { - transaction_id: admitted.value.transaction_id.clone(), - evidence: evidence.clone(), - expected: expected.clone(), - authenticated, - }; - return self.persist_same_phase(&admitted, |next| { - next.ready = Some(evidence); - Ok(()) - }); - } else { - return Ok(()); - } - } - self.transition(current, DeploymentPhase::Routing) - } - - fn advance_routing(&self, current: &Stored) -> Result<()> { - let expected = required(¤t.value.expected, "Expected projection")?; - let activation = required(¤t.value.activation, "activation")?; - if current.value.routing.is_none() { - let current_lease = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - let Some((admitted, authenticated)) = - self.admit_latest_topology(current, current_lease)? - else { - return Ok(()); - }; - if admitted.envelope != current.envelope { - return Ok(()); - } - ensure!( - is_semantic_ready(&authenticated), - "latest Odin observation is not Ready at route admission" - ); - let latest = required( - &admitted.value.latest_odin_observation, - "current route-admission topology evidence", - )?; - if admitted.value.ready.as_ref() != Some(latest) { - let latest = latest.clone(); - return self.persist_same_phase(&admitted, |next| { - next.ready = Some(latest); - Ok(()) - }); - } - let ready = self.rehydrate_ready_token(&admitted.value, now_millis()?, true)?; - ensure!( - ready.transaction_id() == admitted.value.transaction_id, - "Ready token belongs to another transaction" - ); - validate_live_providers_for_deploy(admitted.value.command_kind, || { - self.validate_selected_providers_current( - required(&admitted.value.plan, "transaction plan")?, - now_millis()?, - ) - })?; - self.ensure_transaction_write_lease_current(&admitted.value, now_millis()?)?; - let evidence = if expected.route.is_some() { - let binding = admitted.value.plan.as_ref().unwrap().parsed_inputs()?.1; - let route_binding = binding.route.context("routed plan has no route binding")?; - let preflight = required(&admitted.value.route_preflight, "route preflight")?; - let driver = NginxRouteDriver::new(route_binding); - ensure!( - preflight.candidate_runtime_instance_id == activation.runtime_instance_id, - "route preflight belongs to another runtime instance" - ); - let rollback_allowed = may_rollback_route_after_failed_proof(required( - &admitted.value.fencing, - "route fencing evidence", - )?); - let membership_sha256 = driver.install( - expected, - &activation.runtime_instance_id, - preflight, - rollback_allowed, - )?; - let observation = match self.prove_stable_route( - &admitted.value, - &driver, - membership_sha256, - ) { - Ok(observation) => observation, - Err(proof_error) if !rollback_allowed => { - return Err(proof_error).context( - "incumbent authority was fenced; candidate route remains installed for fail-closed retry", - ); - } - Err(proof_error) => { - return match driver.rollback( - expected, - &activation.runtime_instance_id, - preflight, - ) { - Ok(()) => Err(proof_error) - .context("candidate did not answer its stable route challenge"), - Err(rollback_error) => Err(proof_error).context(format!( - "candidate did not answer its stable route challenge; exact route rollback also failed: {rollback_error:#}" - )), - }; - } - }; - ensure!( - driver.observe_membership(expected, &observation.membership_sha256)?, - "route membership changed during its signed stable-listener observation" - ); - self.ensure_transaction_write_lease_current(&admitted.value, now_millis()?)?; - let promoted_at_unix_millis = observation.observed_at_unix_millis; - RoutingEvidence::Promoted { - observation, - promoted_at_unix_millis, - } - } else { - RoutingEvidence::SkippedUnrouted - }; - let now = now_millis()?; - return self.persist_same_phase(&admitted, |next| { - next.routing = Some(evidence); - next.updated_at_unix_millis = now; - Ok(()) - }); - } - self.transition(current, DeploymentPhase::Committing) - } - - fn advance_committing(&self, current: &Stored) -> Result<()> { - let current_lease_sha256 = current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256) - .map(str::to_owned); - let Some((ready_current, authenticated)) = - self.admit_latest_topology(current, current_lease_sha256.as_deref())? - else { - return Ok(()); - }; - if ready_current.envelope != current.envelope { - return Ok(()); - } - ensure!( - is_semantic_ready(&authenticated), - "latest Odin observation is not Ready at admission commit" - ); - ensure!( - ready_current.value.latest_odin_observation.as_ref() - == ready_current.value.ready.as_ref(), - "latest Odin observation differs from the durable Ready receipt at admission commit" - ); - self.rehydrate_ready_token(&ready_current.value, now_millis()?, true)?; - validate_live_providers_for_deploy(ready_current.value.command_kind, || { - self.validate_selected_providers_current( - required(&ready_current.value.plan, "transaction plan")?, - now_millis()?, - ) - })?; - - let expected = required(&ready_current.value.expected, "Expected projection")?; - let activation = required(&ready_current.value.activation, "activation")?; - let workload = required(&ready_current.value.workload, "workload")?; - self.workload.observe(expected, activation, workload)?; - self.ensure_transaction_write_lease_current(&ready_current.value, now_millis()?)?; - if let Some(route) = ready_current - .value - .routing - .as_ref() - .and_then(RoutingEvidence::observation) - { - let binding = ready_current - .value - .plan - .as_ref() - .unwrap() - .parsed_inputs()? - .1; - let driver = - NginxRouteDriver::new(binding.route.context("routed plan has no route binding")?); - ensure!( - driver.observe_membership(expected, &route.membership_sha256)?, - "route membership changed before admission commit" - ); - let current_route = self.prove_stable_route( - &ready_current.value, - &driver, - route.membership_sha256.clone(), - )?; - ensure!( - current_route.route_id == route.route_id - && current_route.runtime_instance_id == route.runtime_instance_id, - "stable route changed incarnation before admission commit" - ); - ensure!( - driver.observe_membership(expected, &route.membership_sha256)?, - "route membership changed during the final signed admission challenge" - ); - self.ensure_transaction_write_lease_current(&ready_current.value, now_millis()?)?; - } - - let current_lease_sha256 = ready_current - .value - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256) - .map(str::to_owned); - let Some((commit_current, authenticated)) = - self.admit_latest_topology(&ready_current, current_lease_sha256.as_deref())? - else { - return Ok(()); - }; - if commit_current.envelope != ready_current.envelope { - return Ok(()); - } - ensure!( - is_semantic_ready(&authenticated), - "latest Odin observation is not Ready after the final admission challenge" - ); - ensure!( - commit_current.value.latest_odin_observation.as_ref() - == commit_current.value.ready.as_ref(), - "latest Odin observation differs from the durable Ready receipt after the final admission challenge" - ); - let now = now_millis()?; - self.rehydrate_ready_token(&commit_current.value, now, true)?; - validate_live_providers_for_deploy(commit_current.value.command_kind, || { - self.validate_selected_providers_current( - required(&commit_current.value.plan, "transaction plan")?, - now, - ) - })?; - self.workload.observe( - required(&commit_current.value.expected, "Expected projection")?, - required(&commit_current.value.activation, "activation")?, - required(&commit_current.value.workload, "workload")?, - )?; - self.ensure_transaction_write_lease_current(&commit_current.value, now)?; - - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent = self.exact_incumbent(&snapshot, &commit_current.value)?; - let odin_authority = self.current_odin_authority(&snapshot)?; - let generation = - AdmittedGeneration::from_transaction(&commit_current.value, odin_authority, now)?; - let post_commit_cleanup = PostCommitCleanup { - incumbent: match incumbent { - Some(incumbent) - if incumbent_was_stopped_during_fencing(required( - &commit_current.value.fencing, - "commit fencing evidence", - )?) => - { - IncumbentCleanupEvidence::Complete { - generation_id: incumbent.value.generation_id.clone(), - } - } - Some(incumbent) => IncumbentCleanupEvidence::Pending { - generation_id: incumbent.value.generation_id.clone(), - workload: incumbent.value.workload.clone(), - }, - None => IncumbentCleanupEvidence::SkippedNoIncumbent, - }, - source: match commit_current.value.command_kind { - CommandKind::Deploy => SourceCleanupEvidence::Pending, - CommandKind::Continuity => SourceCleanupEvidence::SkippedContinuity, - }, - }; - let mut complete = commit_current.value.clone(); - complete.phase = DeploymentPhase::Complete; - complete.updated_at_unix_millis = now; - complete.last_error = None; - complete.completion = Some(TransactionCompletion::Admitted { - generation_id: generation.generation_id.clone(), - }); - complete.post_commit_cleanup = Some(post_commit_cleanup); - complete.validate()?; - - let admitted_expected = CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: generation.target.clone(), - current: incumbent.map(|stored| stored.envelope.clone()), - }; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( - &[ - CultCacheExpectedEnvelope { - r#type: DeploymentTransaction::TYPE.into(), - key: commit_current.value.transaction_id.clone(), - current: Some(commit_current.envelope.clone()), - }, - admitted_expected, - ], - &[ - transaction_envelope(&complete, now)?, - admitted_envelope(&generation, now)?, - ], - )?, - "incumbent or transaction changed before atomic admission commit" - ); - Ok(()) - } - - fn exact_incumbent<'a>( - &self, - snapshot: &'a ControlSnapshot, - transaction: &DeploymentTransaction, - ) -> Result>> { - let current = snapshot.admitted_for(&transaction.target); - match (&transaction.incumbent_generation_id, current) { - (None, None) => Ok(None), - (Some(expected), Some(current)) if current.value.generation_id == *expected => { - Ok(Some(current)) - } - _ => bail!("target incumbent changed after transaction creation"), - } - } - - fn incumbent_lease_sha256_for_warming( - &self, - snapshot: &ControlSnapshot, - transaction: &DeploymentTransaction, - ) -> Result> { - if let Some(fencing) = &transaction.fencing { - return match fencing { - FencingEvidence::SkippedStateless => Ok(None), - FencingEvidence::Revoked { - incumbent_lease_sha256, - .. - } => Ok(incumbent_lease_sha256.clone()), - }; - } - self.exact_incumbent(snapshot, transaction)? - .and_then(|incumbent| incumbent.value.leasing.lease()) - .map(IdunnProcessWriteLeaseRecord::canonical_sha256) - .transpose() - } - - fn fresh_warming_for_lease( - &self, - current: &Stored, - now: u64, - ) -> Result< - Option<( - Stored, - WarmingEvidence, - SequenceAdmittedWarming, - )>, - > { - let prior = self.rehydrate_warming_token(¤t.value, now, false)?; - match required(¤t.value.warming, "pre-fence Warming evidence")?.clone() { - WarmingEvidence::FirstOdinDirect { .. } => { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - ensure!( - snapshot.admitted_for("odin").is_none() - && self.exact_incumbent(&snapshot, ¤t.value)?.is_none(), - "direct first-Odin Warming refresh found an admitted Odin" - ); - let (evidence, present) = self.observe_first_odin_warming(¤t.value)?; - let token = SequenceAdmittedWarming::from_first_odin_presence( - current.value.transaction_id.clone(), - evidence.clone(), - present, - )?; - ensure!( - token.signed_presence_sha256() != prior.signed_presence_sha256(), - "first Odin replayed its pre-fence Warming presence" - ); - Ok(Some(( - current.clone(), - WarmingEvidence::FirstOdinDirect { evidence }, - token, - ))) - } - WarmingEvidence::OdinTopology { - evidence: prior_evidence, - } => { - let Some((admitted, authenticated)) = self.admit_latest_topology(current, None)? - else { - return Ok(None); - }; - let fresh_evidence = required( - &admitted.value.latest_odin_observation, - "post-fence Odin Warming observation", - )? - .clone(); - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let incumbent_lease_sha256 = - self.incumbent_lease_sha256_for_warming(&snapshot, &admitted.value)?; - if !is_semantic_warming( - required(&admitted.value.expected, "Warming Expected projection")?, - required(&admitted.value.activation, "Warming activation")?, - incumbent_lease_sha256.as_deref(), - &authenticated, - )? { - return Ok(None); - } - let token = SequenceAdmittedWarming::from_topology( - admitted.value.transaction_id.clone(), - fresh_evidence.clone(), - authenticated, - )?; - if !provider_warming_advanced( - prior_evidence.publisher_sequence, - prior.signed_presence_sha256(), - fresh_evidence.publisher_sequence, - token.signed_presence_sha256(), - ) { - return Ok(None); - } - Ok(Some(( - admitted, - WarmingEvidence::OdinTopology { - evidence: fresh_evidence, - }, - token, - ))) - } - } - } - - fn prepare_candidate_write_lease( - &self, - transaction: &DeploymentTransaction, - warming: &SequenceAdmittedWarming, - now: u64, - ) -> Result { - let expected = required(&transaction.expected, "Expected projection")?; - let activation = required(&transaction.activation, "activation")?; - ensure!( - expected.write_lease_required - && warming.transaction_id() == transaction.transaction_id - && warming.runtime_instance_id() == activation.runtime_instance_id, - "fresh Warming token does not own this stateful candidate" - ); - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let epoch = self - .exact_incumbent(&snapshot, transaction)? - .and_then(|generation| generation.value.leasing.lease()) - .map_or(1, |lease| lease.lease_epoch.saturating_add(1)); - let lease = IdunnProcessWriteLeaseRecord { - schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), - target: expected.target.clone(), - expected_projection_sha256: expected.canonical_sha256()?, - plan_id: expected.plan_id.clone(), - incarnation_id: expected.incarnation_id.clone(), - sealed_release_id: expected.sealed_release_id.clone(), - activation_witness_sha256: activation.canonical_sha256()?, - state_schema_generation: expected - .state_schema_generation - .clone() - .context("stateful Expected has no schema generation")?, - state_contract_sha256: expected - .state_contract_sha256 - .clone() - .context("stateful Expected has no state contract")?, - runtime_id: expected.runtime_id.clone(), - runtime_instance_id: activation.runtime_instance_id.clone(), - warming_presence_sha256: warming.signed_presence_sha256().to_owned(), - lease_epoch: epoch, - issued_at_unix_millis: now, - }; - lease.validate()?; - Ok(lease) - } - - fn ensure_transaction_write_lease_current( - &self, - transaction: &DeploymentTransaction, - now: u64, - ) -> Result<()> { - let expected = required(&transaction.expected, "Expected projection")?; - let lease = transaction - .leasing - .as_ref() - .and_then(LeasingEvidence::lease); - ensure!( - expected.write_lease_required == lease.is_some(), - "transaction write-lease disposition differs from Expected" - ); - let Some(lease) = lease else { - return Ok(()); - }; - let activation = required(&transaction.activation, "activation")?; - let warming = self.rehydrate_warming_token(transaction, now, false)?; - let lease_path = required(&transaction.plan, "transaction plan")? - .parsed_inputs()? - .1 - .process_write_lease - .context("stateful target has no write-lease binding")? - .record_path; - ensure!( - CultCacheWriteLeaseDriver::new(&transaction.target, lease_path) - .observe(expected, activation, &warming, lease,)?, - "process write lease changed across the authority boundary" - ); - Ok(()) - } - - fn select_candidate_port(&self, binding: &OperatorBinding) -> Result> { - let Some(route) = &binding.route else { - return Ok(None); - }; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let mut used = BTreeSet::new(); - for plan in snapshot - .admitted - .iter() - .map(|stored| &stored.value.plan) - .chain( - snapshot - .transactions - .iter() - .filter(|stored| stored.value.blocks_new_target_mutation()) - .filter_map(|stored| stored.value.plan.as_ref()), - ) - { - if let Some(port) = plan.candidate_port { - used.insert(port); - } - } - (route.private_port_start..=route.private_port_end) - .find(|port| !used.contains(port)) - .map(Some) - .context("no private candidate port remains in the operator range") - } - - fn deployment_authorization( - &self, - current: &Stored, - now: u64, - ) -> Result> { - let expected = required(¤t.value.expected, "Expected projection")?; - let plan = required(¤t.value.plan, "transaction plan")?; - let binding = plan.parsed_inputs()?.1; - let Some((record, canonical_bytes)) = - read_deployment_brake(&binding.brakes.deployment_store)? - else { - return Ok(None); - }; - let anchor = read_trust_anchor::( - &self.options.deployment_brake_operator_anchor, - )?; - if evaluate_idunn_deployment_brake( - IdunnDeploymentBrakeObservation::Present(&record), - &anchor, - &expected.runtime_id, - &expected.sealed_release_id, - ¤t.value.transaction_id, - now, - ) - .is_err() - { - return Ok(None); - } - let authorization_id = record - .authorization_id - .clone() - .context("released brake has no authorization id")?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - ensure!( - !snapshot.transactions.iter().any(|stored| { - stored - .value - .deployment_authorization - .as_ref() - .is_some_and(|used| used.authorization_id == authorization_id) - }), - "deployment authorization was already consumed" - ); - Ok(Some(DeploymentAuthorization { - authorization_id, - brake_sha256: sha256_id(&canonical_bytes), - canonical_brake_bytes: canonical_bytes, - authorized_at_unix_millis: now, - })) - } - - fn lifecycle_allows(&self, current: &Stored, now: u64) -> Result { - let plan = required(¤t.value.plan, "continuity plan")?; - let binding = plan.parsed_inputs()?.1; - let expected = required(¤t.value.expected, "continuity Expected")?; - match read_lifecycle_brake(&binding.brakes.lifecycle_store) { - Ok(Some(record)) => Ok(evaluate_idunn_continuity_restart( - IdunnLifecycleBrakeObservation::Present(&record), - &expected.runtime_id, - ¤t.value.target, - now, - ) - .is_ok()), - Ok(None) => Ok(evaluate_idunn_continuity_restart( - IdunnLifecycleBrakeObservation::Missing, - &expected.runtime_id, - ¤t.value.target, - now, - ) - .is_ok()), - Err(_) => Ok(false), - } - } - - fn record_gate_wait( - &self, - current: &Stored, - detail: &str, - ) -> Result<()> { - if current.value.last_error.as_deref() == Some(detail) { - return Ok(()); - } - let mut next = current.value.clone(); - next.last_error = Some(detail.into()); - next.updated_at_unix_millis = now_millis()?; - replace_transaction(&self.options.state_store, current, &next) - } - - fn runtime_authority( - &self, - transaction: &DeploymentTransaction, - ) -> Result { - self.runtime_authority_parts( - required(&transaction.plan, "transaction plan")?, - required(&transaction.expected, "Expected projection")?, - required(&transaction.activation, "activation")?, - ) - } - - fn authenticate_routed_presence( - &self, - authority: &cultnet_rs::VerifiedRuntimeAuthority, - current_write_lease_sha256: Option<&str>, - message_id: &str, - challenged_at_unix_millis: u64, - received_at_unix_millis: u64, - canonical_presence: &[u8], - ) -> Result<(String, u64)> { - ensure!( - received_at_unix_millis >= challenged_at_unix_millis, - "route observation predates its challenge" - ); - let authenticated = authenticate_runtime_presence_claim( - canonical_presence, - authority, - RuntimePresenceAuthenticationContext { - trusted_received_at_unix_millis: received_at_unix_millis, - maximum_age_millis: self.options.topology_maximum_age_millis, - maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, - }, - )?; - let signed_presence_sha256 = authenticated.signed_presence_sha256().to_owned(); - let correlation = correlate_runtime_presence_claim(authenticated, authority)?; - ensure!( - correlation.disagreements().is_empty(), - "stable route answered with a runtime that disagrees with current authority" - ); - let present = correlation.into_undisputed_present()?; - let presence = present.record(); - ensure!( - presence.observed_at_unix_millis >= challenged_at_unix_millis, - "stable route returned a presence minted before the route challenge" - ); - ensure!( - presence.state == "active", - "stable route runtime is not Active" - ); - ensure!( - presence.detail == format!("route-observation:{message_id}"), - "stable route response is not bound to the exact challenge" - ); - ensure!( - presence.write_lease_sha256.as_deref() == current_write_lease_sha256, - "stable route runtime does not hold the exact current process write lease" - ); - Ok((signed_presence_sha256, received_at_unix_millis)) - } - - fn authenticate_first_odin_warming_presence( - &self, - transaction: &DeploymentTransaction, - message_id: &str, - challenged_at_unix_millis: u64, - received_at_unix_millis: u64, - canonical_presence: &[u8], - ) -> Result { - ensure!( - transaction.target == "odin" - && transaction.incumbent_generation_id.is_none() - && required(&transaction.expected, "first Odin Expected")?.write_lease_required, - "direct Warming presence is reserved for stateful first Odin bootstrap" - ); - ensure!( - received_at_unix_millis >= challenged_at_unix_millis, - "first Odin Warming observation predates its challenge" - ); - let authority = self.runtime_authority(transaction)?; - let authenticated = authenticate_runtime_presence_claim( - canonical_presence, - &authority, - RuntimePresenceAuthenticationContext { - trusted_received_at_unix_millis: received_at_unix_millis, - maximum_age_millis: self.options.topology_maximum_age_millis, - maximum_future_skew_millis: self.options.topology_maximum_future_skew_millis, - }, - )?; - let correlation = correlate_runtime_presence_claim(authenticated, &authority)?; - ensure!( - correlation.disagreements().is_empty(), - "first Odin Warming presence disagrees with its Expected activation" - ); - let present = correlation.into_undisputed_present()?; - let presence = present.record(); - ensure!( - presence.observed_at_unix_millis >= challenged_at_unix_millis - && presence.state == "warming" - && presence.write_lease_sha256.is_none() - && presence.detail == format!("idunn-warming:{message_id}"), - "first Odin candidate did not return exact fresh pre-lease Warming evidence" - ); - Ok(present) - } - - fn observe_first_odin_warming( - &self, - transaction: &DeploymentTransaction, - ) -> Result<(RuntimePresenceEvidence, cultnet_rs::VerifiedRuntimePresence)> { - let expected = required(&transaction.expected, "first Odin Expected")?; - let binding = required(&transaction.plan, "first Odin plan")? - .parsed_inputs()? - .1; - let driver = NginxRouteDriver::new( - binding - .route - .context("first Odin bootstrap has no candidate route binding")?, - ); - let message_id = format!("warming-{}", Uuid::new_v4().simple()); - let challenged_at_unix_millis = now_millis()?; - let response = driver.request_candidate_runtime_presence(expected, &message_id)?; - ensure!( - response.message_id == message_id, - "first Odin candidate transport substituted its challenge identity" - ); - let admitted_at_unix_millis = now_millis()?; - let present = self.authenticate_first_odin_warming_presence( - transaction, - &message_id, - challenged_at_unix_millis, - admitted_at_unix_millis, - &response.canonical_presence, - )?; - let evidence = RuntimePresenceEvidence::from_present( - &present, - message_id, - challenged_at_unix_millis, - admitted_at_unix_millis, - )?; - Ok((evidence, present)) - } - - fn prove_stable_route( - &self, - transaction: &DeploymentTransaction, - driver: &NginxRouteDriver, - membership_sha256: String, - ) -> Result { - let expected = required(&transaction.expected, "route Expected projection")?; - let activation = required(&transaction.activation, "route activation")?; - let authority = self.runtime_authority(transaction)?; - let current_write_lease_sha256 = transaction - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - self.prove_stable_route_against( - expected, - activation, - &authority, - current_write_lease_sha256, - driver, - membership_sha256, - ) - } - - fn prove_stable_route_against( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - authority: &cultnet_rs::VerifiedRuntimeAuthority, - current_write_lease_sha256: Option<&str>, - driver: &NginxRouteDriver, - membership_sha256: String, - ) -> Result { - let message_id = format!("route-{}", Uuid::new_v4().simple()); - let challenged_at_unix_millis = now_millis()?; - let response = driver.request_runtime_presence(expected, &message_id)?; - ensure!( - response.message_id == message_id, - "route transport substituted its challenge identity" - ); - let received_at_unix_millis = now_millis()?; - let (signed_presence_sha256, observed_at_unix_millis) = self.authenticate_routed_presence( - authority, - current_write_lease_sha256, - &message_id, - challenged_at_unix_millis, - received_at_unix_millis, - &response.canonical_presence, - )?; - let observation = RouteObservation { - route_id: driver.binding.route_id.clone(), - runtime_instance_id: activation.runtime_instance_id.clone(), - membership_sha256, - signed_presence_sha256, - observed_at_unix_millis, - }; - observation.validate()?; - Ok(observation) - } - - fn runtime_authority_parts( - &self, - plan: &CompiledDeploymentPlan, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result { - let provider_anchor = self.provider_anchor_for_plan(plan)?; - verify_runtime_authority( - expected, - activation, - &self.idunn_anchor, - &provider_anchor.public_key, - ) - } - - fn provider_anchor_for_plan( - &self, - plan: &CompiledDeploymentPlan, - ) -> Result { - let binding = plan.parsed_inputs()?.1; - let provider_anchor = read_trust_anchor::( - &binding.runtime_identity.trust_anchor_store, - )?; - ensure!( - provider_anchor.schema_version - == ::TRUST_ANCHOR_SCHEMA, - "provider runtime presence trust anchor schema is unsupported" - ); - Ok(provider_anchor) - } - - fn authenticate_topology_bytes( - &self, - snapshot: &ControlSnapshot, - transaction: &DeploymentTransaction, - canonical_bytes: &[u8], - current_write_lease_sha256: Option<&str>, - trusted_received_at: u64, - ) -> Result { - let authority = self.runtime_authority(transaction)?; - let odin_authority = self.current_odin_authority(snapshot)?; - authenticate_odin_runtime_topology_correlation( - canonical_bytes, - &authority, - current_write_lease_sha256, - &odin_authority.signer_public_key, - self.trusted_topology_context(trusted_received_at), - ) - } - - /// Persist every newly authenticated publisher sequence before deciding - /// whether it means Warming, Ready, degraded, or disagreement. - fn admit_latest_topology( - &self, - current: &Stored, - current_write_lease_sha256: Option<&str>, - ) -> Result< - Option<( - Stored, - AuthenticatedOdinRuntimeTopologyCorrelation, - )>, - > { - let Some(received) = self.topology().receive(¤t.value.target)? else { - return Ok(None); - }; - ensure!( - received.target == current.value.target, - "topology transport substituted target" - ); - let now = now_millis()?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let live = snapshot - .transactions - .iter() - .find(|stored| stored.value.transaction_id == current.value.transaction_id) - .context("transaction disappeared before topology admission")?; - ensure!( - live.envelope == current.envelope, - "transaction changed before topology admission" - ); - let authenticated = self.authenticate_topology_bytes( - &snapshot, - &live.value, - &received.canonical_bytes, - current_write_lease_sha256, - now, - )?; - let evidence = TopologyEvidence::from_authenticated(&authenticated, now)?; - if !sequence_requires_admission( - live.value.latest_odin_observation.as_ref(), - snapshot.max_odin_sequence(&live.value.target, &evidence.signer_identity_id), - &evidence, - )? { - return Ok(Some((live.clone(), authenticated))); - } - let mut next = live.value.clone(); - next.latest_odin_observation = Some(evidence.clone()); - next.odin_publisher_sequence_cursor = evidence.publisher_sequence; - next.updated_at_unix_millis = now; - next.last_error = None; - replace_transaction(&self.options.state_store, live, &next)?; - let admitted = ControlSnapshot::read(&self.options.state_store)? - .transactions - .into_iter() - .find(|stored| stored.value.transaction_id == next.transaction_id) - .context("sequence-admitted transaction disappeared")?; - ensure!( - admitted.value.latest_odin_observation.as_ref() == Some(&evidence) - && admitted.value.odin_publisher_sequence_cursor == evidence.publisher_sequence, - "topology sequence was not durably admitted" - ); - Ok(Some((admitted, authenticated))) - } - - fn rehydrate_warming_token( - &self, - transaction: &DeploymentTransaction, - now: u64, - require_current: bool, - ) -> Result { - let evidence = required(&transaction.warming, "durable warming evidence")?.clone(); - match evidence { - WarmingEvidence::OdinTopology { evidence } => { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let authenticated = self.authenticate_topology_bytes( - &snapshot, - transaction, - &evidence.canonical_bytes, - None, - if require_current { - now - } else { - evidence.admitted_at_unix_millis - }, - )?; - validate_authenticated_evidence(&evidence, &authenticated)?; - let incumbent_lease_sha256 = - self.incumbent_lease_sha256_for_warming(&snapshot, transaction)?; - ensure!( - is_semantic_warming( - required(&transaction.expected, "Warming Expected projection")?, - required(&transaction.activation, "Warming activation")?, - incumbent_lease_sha256.as_deref(), - &authenticated, - )?, - "durable warming evidence no longer satisfies the Warming gate" - ); - SequenceAdmittedWarming::from_topology( - transaction.transaction_id.clone(), - evidence, - authenticated, - ) - } - WarmingEvidence::FirstOdinDirect { evidence } => { - let present = self.authenticate_first_odin_warming_presence( - transaction, - &evidence.message_id, - evidence.challenged_at_unix_millis, - if require_current { - now - } else { - evidence.admitted_at_unix_millis - }, - &evidence.canonical_bytes, - )?; - SequenceAdmittedWarming::from_first_odin_presence( - transaction.transaction_id.clone(), - evidence, - present, - ) - } - } - } - - fn rehydrate_ready_token( - &self, - transaction: &DeploymentTransaction, - now: u64, - require_current: bool, - ) -> Result { - let evidence = required(&transaction.ready, "durable Ready evidence")?.clone(); - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - let current_lease = transaction - .leasing - .as_ref() - .and_then(LeasingEvidence::lease_sha256); - let authenticated = self.authenticate_topology_bytes( - &snapshot, - transaction, - &evidence.canonical_bytes, - current_lease, - if require_current { - now - } else { - evidence.admitted_at_unix_millis - }, - )?; - validate_authenticated_evidence(&evidence, &authenticated)?; - ensure!( - is_semantic_ready(&authenticated), - "durable Ready evidence no longer authenticates as Ready" - ); - Ok(SequenceAdmittedReady { - transaction_id: transaction.transaction_id.clone(), - evidence, - expected: required(&transaction.expected, "Expected projection")?.clone(), - authenticated, - }) - } - - fn current_ready_provider_tokens( - &self, - snapshot: &ControlSnapshot, - now: u64, - ) -> Result> { - let mut providers = Vec::new(); - for stored in &snapshot.admitted { - if stored.value.latest_odin_observation != stored.value.ready { - continue; - } - match self.rehydrate_admitted_ready(snapshot, &stored.value, now) { - Ok(provider) => providers.push(provider), - Err(error) => eprintln!( - "Idunn excluded non-current provider {}: {error:#}", - stored.value.target - ), - } - } - Ok(providers) - } - - fn rehydrate_admitted_ready( - &self, - snapshot: &ControlSnapshot, - generation: &AdmittedGeneration, - now: u64, - ) -> Result { - ensure!( - generation.latest_odin_observation == generation.ready, - "admitted provider latest topology is not Ready" - ); - let authority = self.runtime_authority_parts( - &generation.plan, - &generation.expected, - &generation.activation, - )?; - let odin_authority = self.current_odin_authority(snapshot)?; - let current_lease = generation.leasing.lease_sha256(); - let authenticated = authenticate_odin_runtime_topology_correlation( - &generation.ready.canonical_bytes, - &authority, - current_lease, - &odin_authority.signer_public_key, - self.trusted_topology_context(now), - )?; - validate_authenticated_evidence(&generation.ready, &authenticated)?; - ensure!( - is_semantic_ready(&authenticated), - "admitted provider no longer has current exact Ready evidence" - ); - Ok(SequenceAdmittedReady { - transaction_id: generation.transaction_id.clone(), - evidence: generation.ready.clone(), - expected: generation.expected.clone(), - authenticated, - }) - } - - fn validate_selected_providers_current( - &self, - plan: &CompiledDeploymentPlan, - now: u64, - ) -> Result<()> { - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - for selection in &plan.dependencies { - let Some(provider) = &selection.provider else { - ensure!( - selection.requirement.kind == DependencyKind::Optional, - "required dependency has no provider" - ); - continue; - }; - let DependencyProviderAuthority::ManagedReady { - target, - incarnation_id, - plan_id, - sealed_release_id, - expected_projection_sha256, - odin_topology_correlation_sha256, - odin_topology_publisher_sequence, - } = &provider.authority - else { - continue; - }; - let admitted = snapshot - .admitted_for(target) - .context("selected managed dependency is no longer admitted")?; - let token = self.rehydrate_admitted_ready(&snapshot, &admitted.value, now)?; - ensure!( - admitted.value.expected.incarnation_id == *incarnation_id - && admitted.value.plan.plan_id == *plan_id - && admitted.value.sealed_release.sealed_release_id == *sealed_release_id - && admitted.value.expected.canonical_sha256()? == *expected_projection_sha256 - && token.publisher_sequence() >= *odin_topology_publisher_sequence, - "selected dependency authority changed before actuation" - ); - ensure!( - token.publisher_sequence() != *odin_topology_publisher_sequence - || token.evidence_sha256() == odin_topology_correlation_sha256, - "selected dependency Odin sequence changed evidence" - ); - ensure!( - token - .authenticated() - .record() - .observed_capabilities - .iter() - .any(|capability| { - capability_compatible( - &selection.requirement.capability, - &selection.requirement.schema, - &selection.requirement.compatibility, - &capability.capability, - &capability.schema, - &capability.compatibility, - ) && capability.capacity >= selection.requirement.minimum_capacity - }), - "selected dependency no longer provides its required capability" - ); - } - Ok(()) - } - - fn persist_same_phase( - &self, - current: &Stored, - mutation: F, - ) -> Result<()> - where - F: FnOnce(&mut DeploymentTransaction) -> Result<()>, - { - let mut next = current.value.clone(); - mutation(&mut next)?; - ensure!( - next.phase == current.value.phase, - "same-phase evidence update changed phase" - ); - next.updated_at_unix_millis = now_millis()?; - next.last_error = None; - replace_transaction(&self.options.state_store, current, &next) - } - - fn transition( - &self, - current: &Stored, - next_phase: DeploymentPhase, - ) -> Result<()> { - ensure!( - next_phase as u8 == current.value.phase as u8 + 1, - "deployment phase transition is not adjacent" - ); - let mut next = current.value.clone(); - next.phase = next_phase; - next.updated_at_unix_millis = now_millis()?; - next.last_error = None; - replace_transaction(&self.options.state_store, current, &next) - } - - fn record_resumable_error( - &self, - current: &Stored, - error: &anyhow::Error, - ) -> Result<()> { - let detail = truncate(&format!("{error:#}"), 2048); - if current.value.last_error.as_deref() == Some(detail.as_str()) { - return Ok(()); - } - let mut next = current.value.clone(); - next.last_error = Some(detail); - next.updated_at_unix_millis = now_millis()?; - replace_transaction(&self.options.state_store, current, &next) - } - - fn begin_pre_fencing_abort( - &self, - current: &Stored, - error: anyhow::Error, - ) -> Result<()> { - ensure!( - current.value.phase < DeploymentPhase::Fencing, - "post-fence transaction cannot terminal-fail" - ); - ensure!( - current.value.pre_fencing_abort.is_none(), - "pre-fencing abort intent is already durable" - ); - let abort = PreFencingAbort { - error: truncate(&format!("{error:#}"), 2048), - candidate_cleanup: candidate_cleanup_requirement( - current.value.activation.is_some(), - current.value.workload.is_some(), - ), - topology_reconciliation: if current.value.command_kind == CommandKind::Deploy - && current.value.expected_publication_sha256.is_some() - { - CleanupEvidence::Pending - } else { - CleanupEvidence::Skipped - }, - source_cleanup: if current.value.command_kind == CommandKind::Deploy { - CleanupEvidence::Pending - } else { - CleanupEvidence::Skipped - }, - }; - self.persist_same_phase(current, |next| { - next.pre_fencing_abort = Some(abort); - Ok(()) - }) - } - - fn advance_pre_fencing_abort(&self, current: &Stored) -> Result<()> { - ensure!( - current.value.phase < DeploymentPhase::Fencing, - "pre-fencing abort crossed the fencing boundary" - ); - let abort = required(¤t.value.pre_fencing_abort, "pre-fencing abort intent")?; - if abort.candidate_cleanup == CleanupEvidence::Pending { - if let Some(workload) = ¤t.value.workload { - self.workload - .stop(workload) - .context("stopping exact pre-fence candidate")?; - } - self.workload - .discard_prepared( - required(¤t.value.plan, "pre-fencing candidate plan")?, - required(¤t.value.expected, "pre-fencing Expected projection")?, - required(¤t.value.activation, "pre-fencing activation")?, - ) - .context("discarding exact pre-fence activation material")?; - return self.persist_same_phase(current, |next| { - next.pre_fencing_abort.as_mut().unwrap().candidate_cleanup = - CleanupEvidence::Complete; - Ok(()) - }); - } - if abort.topology_reconciliation == CleanupEvidence::Pending { - let expected = required(¤t.value.expected, "failed Expected projection")?; - let snapshot = ControlSnapshot::read(&self.options.state_store)?; - if let Some(incumbent) = self.exact_incumbent(&snapshot, ¤t.value)? { - let topology = self.topology(); - let failed_provider_anchor = self.provider_anchor_for_plan(required( - ¤t.value.plan, - "failed transaction plan", - )?)?; - let admitted_provider_anchor = - self.provider_anchor_for_plan(&incumbent.value.plan)?; - let expected_sha256 = topology.restore_admitted_expected_only( - expected, - &failed_provider_anchor, - current.value.activation.as_ref(), - &incumbent.value.expected, - &admitted_provider_anchor, - &incumbent.value.activation, - incumbent.value.leasing.lease(), - )?; - ensure!( - expected_sha256 == incumbent.value.expected.canonical_sha256()?, - "restored incumbent Expected receipt differs" - ); - } else { - let plan = required(¤t.value.plan, "failed transaction plan")?; - let provider_anchor = self.provider_anchor_for_plan(plan)?; - self.topology() - .withdraw_expected( - expected, - &provider_anchor, - current.value.activation.as_ref(), - None, - ) - .context("withdrawing exact failed Expected projection")?; - } - return self.persist_same_phase(current, |next| { - next.pre_fencing_abort - .as_mut() - .unwrap() - .topology_reconciliation = CleanupEvidence::Complete; - Ok(()) - }); - } - if abort.source_cleanup == CleanupEvidence::Pending { - self.source - .cleanup( - ¤t.value.transaction_id, - current.value.frozen_source.as_ref(), - ) - .context("cleaning failed pre-fence source")?; - return self.persist_same_phase(current, |next| { - next.pre_fencing_abort.as_mut().unwrap().source_cleanup = CleanupEvidence::Complete; - Ok(()) - }); - } - ensure!( - abort.is_complete(), - "pre-fencing abort cleanup is incomplete" - ); - let mut next = current.value.clone(); - next.phase = DeploymentPhase::Complete; - next.updated_at_unix_millis = now_millis()?; - next.last_error = Some(abort.error.clone()); - next.completion = Some(TransactionCompletion::FailedBeforeFencing { - error: abort.error.clone(), - }); - replace_transaction(&self.options.state_store, current, &next) - } - - fn advance_post_commit_cleanup(&self, current: &Stored) -> Result<()> { - let cleanup = required( - ¤t.value.post_commit_cleanup, - "post-commit cleanup evidence", - )?; - if let IncumbentCleanupEvidence::Pending { - generation_id, - workload, - } = &cleanup.incumbent - { - let binding = required(¤t.value.plan, "committed transaction plan")? - .parsed_inputs()? - .1; - if let Some(promoted_at_unix_millis) = required( - ¤t.value.routing, - "committed transaction routing evidence", - )? - .promoted_at_unix_millis() - { - let retire_not_before = - route_drain_deadline(promoted_at_unix_millis, binding.rollout.drain_seconds)?; - if now_millis()? < retire_not_before { - return Ok(()); - } - } - self.workload - .stop(workload) - .with_context(|| format!("retiring admitted incumbent {generation_id}"))?; - let generation_id = generation_id.clone(); - return self.persist_same_phase(current, |next| { - next.post_commit_cleanup.as_mut().unwrap().incumbent = - IncumbentCleanupEvidence::Complete { generation_id }; - Ok(()) - }); - } - if cleanup.source == SourceCleanupEvidence::Pending { - self.source - .cleanup( - ¤t.value.transaction_id, - current.value.frozen_source.as_ref(), - ) - .context("cleaning committed deployment source")?; - return self.persist_same_phase(current, |next| { - next.post_commit_cleanup.as_mut().unwrap().source = SourceCleanupEvidence::Complete; - Ok(()) - }); - } - ensure!(cleanup.is_complete(), "post-commit cleanup is incomplete"); - Ok(()) - } -} - -fn route_drain_deadline(promoted_at_unix_millis: u64, drain_seconds: u32) -> Result { - promoted_at_unix_millis - .checked_add( - u64::from(drain_seconds) - .checked_mul(1_000) - .context("route drain duration overflows milliseconds")?, - ) - .context("route drain deadline overflows Unix milliseconds") -} - -fn route_observation_is_current( - observed_at_unix_millis: u64, - now_unix_millis: u64, - maximum_age_millis: u64, - maximum_future_skew_millis: u64, -) -> bool { - observed_at_unix_millis <= now_unix_millis.saturating_add(maximum_future_skew_millis) - && now_unix_millis.saturating_sub(observed_at_unix_millis) <= maximum_age_millis -} - -fn provider_warming_advanced( - prior_odin_sequence: u64, - prior_signed_presence_sha256: &str, - candidate_odin_sequence: u64, - candidate_signed_presence_sha256: &str, -) -> bool { - candidate_odin_sequence > prior_odin_sequence - && candidate_signed_presence_sha256 != prior_signed_presence_sha256 -} - -fn may_rollback_route_after_failed_proof(fencing: &FencingEvidence) -> bool { - matches!(fencing, FencingEvidence::SkippedStateless) -} - -fn incumbent_was_stopped_during_fencing(fencing: &FencingEvidence) -> bool { - matches!( - fencing, - FencingEvidence::Revoked { - incumbent_lease_sha256: Some(_), - .. - } - ) -} - -fn candidate_cleanup_requirement( - has_prepared_activation: bool, - has_workload_observation: bool, -) -> CleanupEvidence { - if has_prepared_activation || has_workload_observation { - CleanupEvidence::Pending - } else { - CleanupEvidence::Skipped - } -} - -fn prove_isolation( - candidate: &WorkloadObservation, - incumbent: Option<&WorkloadObservation>, -) -> Result { - ensure!( - candidate.dynamic_user - && candidate.private_pids - && candidate.private_mounts - && candidate.process_uids[0] > 0 - && candidate.pid_namespace_id > 0 - && candidate.mount_namespace_id > 0, - "candidate lacks dynamic identity or private namespaces" - ); - let evidence = if let Some(incumbent) = incumbent { - ensure!( - incumbent.dynamic_user - && incumbent.private_pids - && incumbent.private_mounts - && incumbent.process_uids[0] > 0 - && incumbent.pid_namespace_id > 0 - && incumbent.mount_namespace_id > 0, - "incumbent lacks admitted dynamic identity or private namespaces" - ); - ensure!( - candidate.process_uids[0] != incumbent.process_uids[0] - && candidate.pid_namespace_id != incumbent.pid_namespace_id - && candidate.mount_namespace_id != incumbent.mount_namespace_id, - "candidate and incumbent are not distinct by UID, PID namespace, and mount namespace" - ); - IsolationEvidence { - candidate_uid: candidate.process_uids[0], - candidate_pid_namespace_id: candidate.pid_namespace_id, - candidate_mount_namespace_id: candidate.mount_namespace_id, - incumbent_uid: Some(incumbent.process_uids[0]), - incumbent_pid_namespace_id: Some(incumbent.pid_namespace_id), - incumbent_mount_namespace_id: Some(incumbent.mount_namespace_id), - } - } else { - IsolationEvidence { - candidate_uid: candidate.process_uids[0], - candidate_pid_namespace_id: candidate.pid_namespace_id, - candidate_mount_namespace_id: candidate.mount_namespace_id, - incumbent_uid: None, - incumbent_pid_namespace_id: None, - incumbent_mount_namespace_id: None, - } - }; - Ok(evidence) -} - -fn sequence_requires_admission( - latest: Option<&TopologyEvidence>, - maximum_admitted_sequence: u64, - candidate: &TopologyEvidence, -) -> Result { - candidate.validate_shape()?; - if latest.is_some_and(|existing| { - existing.canonical_bytes == candidate.canonical_bytes - && existing.canonical_sha256 == candidate.canonical_sha256 - && existing.signer_identity_id == candidate.signer_identity_id - && existing.publisher_sequence == candidate.publisher_sequence - }) { - return Ok(false); - } - ensure!( - candidate.publisher_sequence > maximum_admitted_sequence, - "Odin topology publisher sequence was replayed or reordered" - ); - Ok(true) -} - -fn validate_live_providers_for_deploy(command_kind: CommandKind, validation: F) -> Result<()> -where - F: FnOnce() -> Result<()>, -{ - match command_kind { - CommandKind::Deploy => validation(), - CommandKind::Continuity => Ok(()), - } -} - -fn validate_authenticated_evidence( - evidence: &TopologyEvidence, - authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation, -) -> Result<()> { - let record = authenticated.record(); - ensure!( - authenticated.canonical_bytes() == evidence.canonical_bytes.as_slice() - && sha256_id(authenticated.canonical_bytes()) == evidence.canonical_sha256 - && record.signer_identity_id == evidence.signer_identity_id - && record.publisher_sequence == evidence.publisher_sequence, - "durable topology evidence differs from its authenticated receipt" - ); - Ok(()) -} - -fn is_semantic_warming( - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - incumbent_lease_sha256: Option<&str>, - authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation, -) -> Result { - let record = authenticated.record(); - if !record.present || record.observed_write_lease_sha256.is_some() { - return Ok(false); - } - let expected_projection_detail = format!( - "expected:{};activation:{}", - expected.canonical_sha256()?, - activation.canonical_sha256()? - ); - if !warming_disagreements_match_incumbent( - &expected_projection_detail, - incumbent_lease_sha256, - &record.disagreements, - ) { - return Ok(false); - } - Ok(if expected.write_lease_required { - !record.ready && record.observed_presence_state.as_deref() == Some("warming") - } else { - matches!( - record.observed_presence_state.as_deref(), - Some("warming" | "active") - ) - }) -} - -fn warming_disagreements_match_incumbent( - expected_projection_detail: &str, - incumbent_lease_sha256: Option<&str>, - disagreements: &[OdinTopologyDisagreement], -) -> bool { - if disagreements.is_empty() { - true - } else if let (Some(incumbent_lease_sha256), [disagreement]) = - (incumbent_lease_sha256, disagreements) - { - disagreement.code == "projected-write-lease" - && disagreement.expected.as_deref() == Some(expected_projection_detail) - && disagreement.observed.as_deref() == Some(incumbent_lease_sha256) - } else { - false - } -} - -fn is_semantic_ready(authenticated: &AuthenticatedOdinRuntimeTopologyCorrelation) -> bool { - let record = authenticated.record(); - record.present - && record.ready - && record.observed_presence_state.as_deref() == Some("active") - && record.disagreements.is_empty() -} - -fn read_deployment_brake(path: &Path) -> Result)>> { - let Some(envelope) = read_single_envelope(path)? else { - return Ok(None); - }; - ensure!( - envelope.r#type == IdunnDeploymentBrakeRecord::TYPE - && envelope.schema_id.as_deref() == Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA), - "deployment brake store contains a foreign record" - ); - let record: IdunnDeploymentBrakeRecord = rmp_serde::from_slice(&envelope.payload)?; - record.validate()?; - ensure!( - rmp_serde::to_vec(&record)? == envelope.payload && envelope.key == record.brake_id, - "deployment brake is noncanonical or keyed by another authority" - ); - Ok(Some((record, envelope.payload))) -} - -fn read_lifecycle_brake(path: &Path) -> Result> { - let Some(envelope) = read_single_envelope(path)? else { - return Ok(None); - }; - ensure!( - envelope.r#type == IdunnLifecycleBrakeRecord::TYPE - && envelope.schema_id.as_deref() == Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA), - "lifecycle brake store contains a foreign record" - ); - IdunnLifecycleBrakeRecord::decode_canonical(&envelope.payload).map(Some) -} - -fn read_single_envelope(path: &Path) -> Result> { - if !path.exists() { - return Ok(None); - } - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - match entries.as_slice() { - [] => Ok(None), - [entry] => Ok(Some(entry.clone())), - _ => bail!("single-record authority store is ambiguous"), - } -} - -fn read_trust_anchor(path: &Path) -> Result { - let envelope = read_single_envelope(path)? - .with_context(|| format!("service identity trust anchor {} is absent", path.display()))?; - ensure!( - envelope.r#type == P::TRUST_ANCHOR_TYPE - && envelope.key == P::TRUST_ANCHOR_KEY - && envelope.schema_id.as_deref() == Some(P::TRUST_ANCHOR_SCHEMA), - "service identity trust anchor belongs to another profile" - ); - let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&envelope.payload)?; - ensure!( - rmp_serde::to_vec(&anchor)? == envelope.payload - && derive_service_identity_id::

(&anchor.public_key)? == anchor.identity_id, - "service identity trust anchor is noncanonical or self-inconsistent" - ); - Ok(anchor) -} - -fn required<'a, T>(value: &'a Option, label: &str) -> Result<&'a T> { - value.as_ref().with_context(|| format!("{label} is absent")) -} - -fn now_millis() -> Result { - Ok(SystemTime::now() - .duration_since(UNIX_EPOCH) - .context("system clock predates Unix epoch")? - .as_millis() - .try_into()?) -} - -fn rfc3339_millis(millis: u64) -> Result { - chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) - .context("timestamp is out of range") - .map(|value| value.to_rfc3339()) -} - -fn sibling_path(path: &Path, suffix: &str) -> PathBuf { - let mut value = path.as_os_str().to_os_string(); - value.push(suffix); - PathBuf::from(value) -} - -fn string_value(args: &mut impl Iterator, name: &str) -> Result { - args.next() - .ok_or_else(|| anyhow!("{name} requires a value")) -} - -fn path_value(args: &mut impl Iterator, name: &str) -> Result { - Ok(PathBuf::from(string_value(args, name)?)) -} - -fn u32_value(args: &mut impl Iterator, name: &str) -> Result { - string_value(args, name)? - .parse() - .with_context(|| format!("{name} requires a u32")) -} - -fn u64_value(args: &mut impl Iterator, name: &str) -> Result { - string_value(args, name)? - .parse() - .with_context(|| format!("{name} requires a u64")) -} - -fn require_selector(value: &str) -> Result<()> { - let value = value.strip_prefix("profile:").unwrap_or(value); - require_id(value, "deployment selector") -} - -fn require_id(value: &str, label: &str) -> Result<()> { - ensure!( - !value.is_empty() - && value.len() <= 256 - && value.bytes().all(|byte| { - byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/') - }), - "{label} is invalid" - ); - Ok(()) -} - -fn require_value(value: &str, label: &str) -> Result<()> { - ensure!( - !value.trim().is_empty() - && value == value.trim() - && value.len() <= 1024 - && !value.contains('\0'), - "{label} is invalid" - ); - Ok(()) -} - -fn require_detail(value: &str, label: &str) -> Result<()> { - ensure!( - !value.is_empty() && value.len() <= 2048 && !value.contains('\0'), - "{label} is invalid" - ); - Ok(()) -} - -fn truncate(value: &str, length: usize) -> String { - value.chars().take(length).collect() -} - -fn sha256_id(bytes: &[u8]) -> String { - let digest = Sha256::digest(bytes); - let mut encoded = String::with_capacity(71); - encoded.push_str("sha256-"); - for byte in digest { - use std::fmt::Write as _; - write!(&mut encoded, "{byte:02x}").expect("writing to String cannot fail"); - } - encoded -} - -fn runtime_instance_id(transaction_id: &str) -> Result { - require_id(transaction_id, "runtime transaction id")?; - Ok(sha256_id( - format!("gamecult.idunn.runtime-instance.v1:{transaction_id}").as_bytes(), - )) -} - -fn usage() -> &'static str { - "Idunn deployment, admission, and continuity control plane\n\n\ - idunn serve [runtime options]\n\ - idunn up [--state-store PATH] [--no-wait]\n\ - idunn status [--state-store PATH] [--command ID]\n\n\ - Recipes describe capability and process requirements. Idunn seals exact\n\ - source and artifacts, admits one incarnation, and delegates execution to\n\ - systemd and routing mechanics to the configured proxy." -} - -#[cfg(test)] -mod tests { - use super::*; - - fn digest(byte: char) -> String { - byte.to_string().repeat(64) - } - - fn route_binding( - route_id: &str, - stable_port: u16, - private_port_start: u16, - private_port_end: u16, - ) -> RouteBinding { - RouteBinding { - driver: crate::deployment::RouteDriver::NginxStreamTcp, - route_id: route_id.into(), - stable_endpoint: format!("tcp://127.0.0.1:{stable_port}"), - private_host: "127.0.0.1".into(), - private_port_start, - private_port_end, - config_path: PathBuf::from(format!("/etc/nginx/idunn-stream-routes/{route_id}.conf")), - reload_unit: "nginx.service".into(), - } - } - - fn command(kind: CommandKind) -> DeploymentCommand { - DeploymentCommand { - schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: match kind { - CommandKind::Deploy => "up-test".into(), - CommandKind::Continuity => "continuity-test".into(), - }, - kind, - selector: "ghostlight".into(), - requested_by: "operator".into(), - requested_at_unix_millis: 100, - } - } - - fn topology(sequence: u64, byte: u8) -> TopologyEvidence { - let canonical_bytes = vec![byte]; - TopologyEvidence { - canonical_sha256: sha256_id(&canonical_bytes), - canonical_bytes, - signer_identity_id: "odin-signer".into(), - publisher_sequence: sequence, - admitted_at_unix_millis: 100, - } - } - - fn workload(uid: u32, pid_namespace_id: u64, mount_namespace_id: u64) -> WorkloadObservation { - WorkloadObservation { - unit: format!("idunn-{uid}.service"), - unit_description: format!("Idunn test {uid}"), - invocation_id: format!("invocation-{uid}"), - exec_main_start_timestamp_monotonic: 1, - service_type: "exec".into(), - restart_policy: "always".into(), - kill_mode: "control-group".into(), - dynamic_user: true, - systemd_user: format!("u{uid}"), - systemd_group: format!("u{uid}"), - supplementary_groups: String::new(), - capability_bounding_set: String::new(), - ambient_capabilities: String::new(), - private_mounts: true, - private_pids: true, - protect_proc: "invisible".into(), - proc_subset: "pid".into(), - no_new_privileges: true, - umask: "0077".into(), - inaccessible_paths: String::new(), - load_credential: String::new(), - main_pid: uid, - process_start_time: 1, - process_uids: [uid; 4], - process_gids: [uid; 4], - process_groups: vec![uid], - process_cap_inheritable: 0, - process_cap_permitted: 0, - process_cap_effective: 0, - process_cap_bounding: 0, - process_cap_ambient: 0, - process_no_new_privileges: true, - process_namespace_pids: vec![1], - mount_namespace_id, - pid_namespace_id, - executable: PathBuf::from("/opt/test/bin/service"), - executable_device: 1, - executable_inode: 1, - executable_sha256: sha256_id(&[1]), - runtime_instance_id: sha256_id(&uid.to_be_bytes()), - working_directory: PathBuf::from("/opt/test"), - runtime_bundle: PathBuf::from("/run/test"), - command_line_sha256: sha256_id(&[2]), - environment_names: Vec::new(), - environment_contract_sha256: sha256_id(&[3]), - control_group: format!("/system.slice/idunn-{uid}.service"), - credentials_directory: None, - parent_only_file_descriptors: Vec::new(), - activation_signer_identity_id: "activation".into(), - activation_signer_public_key: vec![1; 32], - service_credentials: Vec::new(), - } - } - - fn write_lease() -> IdunnProcessWriteLeaseRecord { - IdunnProcessWriteLeaseRecord { - schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), - target: "ghostlight".into(), - expected_projection_sha256: sha256_id(&[1]), - plan_id: sha256_id(&[2]), - incarnation_id: "incarnation-test".into(), - sealed_release_id: sha256_id(&[3]), - activation_witness_sha256: sha256_id(&[4]), - state_schema_generation: "ghostlight-state-v1".into(), - state_contract_sha256: sha256_id(&[5]), - runtime_id: "ghostlight".into(), - runtime_instance_id: sha256_id(&[6]), - warming_presence_sha256: sha256_id(&[7]), - lease_epoch: 1, - issued_at_unix_millis: 100, - } - } - - #[test] - fn cli_exposes_only_declarative_commands() { - for arguments in [ - vec!["up", "ghostlight", "--deploy-command", "sh -c bad"], - vec!["serve", "--swarm-profile", "yggdrasil-local"], - vec!["serve", "--restart-command", "bad"], - vec!["validate-runtime-admission"], - ] { - assert!(parse(arguments.into_iter().map(str::to_owned)).is_err()); - } - } - - #[test] - fn garden_path_accepts_service_and_profile_selectors() { - for selector in ["ghostlight", "profile:aetheria", "profile:full-gamecult"] { - let parsed = - parse(["up", selector, "--no-wait"].into_iter().map(str::to_owned)).unwrap(); - let Command::Up { - selector: actual, .. - } = parsed - else { - panic!("expected up command") - }; - assert_eq!(actual, selector); - } - } - - #[test] - fn route_bindings_are_one_global_socket_and_candidate_authority_map() { - let first = route_binding("first", 4103, 14103, 14111); - let second = route_binding("second", 8831, 18831, 18839); - validate_route_binding_set(&[("first", &first), ("second", &second)]).unwrap(); - - let mut duplicate_id = second.clone(); - duplicate_id.route_id = first.route_id.clone(); - assert!( - validate_route_binding_set(&[("first", &first), ("second", &duplicate_id)]).is_err() - ); - - let mut overlapping_candidates = second.clone(); - overlapping_candidates.private_port_start = 14111; - overlapping_candidates.private_port_end = 14120; - assert!( - validate_route_binding_set(&[("first", &first), ("second", &overlapping_candidates),]) - .is_err() - ); - - let mut stable_inside_other_range = second; - stable_inside_other_range.stable_endpoint = "tcp://127.0.0.1:14105".into(); - assert!( - validate_route_binding_set(&[ - ("first", &first), - ("second", &stable_inside_other_range), - ]) - .is_err() - ); - } - - #[test] - fn deployment_command_is_immutable_positional_fact() -> Result<()> { - let value = command(CommandKind::Deploy); - value.validate()?; - let encoded = rmp_serde::to_vec(&value)?; - assert_eq!(encoded[0], 0x96); - assert!(!encoded.windows(7).any(|window| window == b"running")); - assert!(!encoded.windows(5).any(|window| window == b"owner")); - Ok(()) - } - - #[test] - fn phases_are_one_exact_forward_chain() { - let phases = [ - DeploymentPhase::Sealing, - DeploymentPhase::Starting, - DeploymentPhase::Warming, - DeploymentPhase::Fencing, - DeploymentPhase::Leasing, - DeploymentPhase::AwaitingReady, - DeploymentPhase::Routing, - DeploymentPhase::Committing, - DeploymentPhase::Complete, - ]; - assert!( - phases - .windows(2) - .all(|pair| pair[1] as u8 == pair[0] as u8 + 1) - ); - } - - #[test] - fn every_new_odin_sequence_must_advance_and_exact_retry_is_idempotent() -> Result<()> { - let first = topology(7, 1); - assert!(sequence_requires_admission(None, 6, &first)?); - assert!(!sequence_requires_admission( - Some(&first), - 7, - &topology(7, 1) - )?); - assert!(sequence_requires_admission( - Some(&first), - 7, - &topology(8, 2) - )?); - assert!(sequence_requires_admission(Some(&first), 7, &topology(7, 2)).is_err()); - assert!(sequence_requires_admission(Some(&first), 7, &topology(6, 3)).is_err()); - Ok(()) - } - - #[test] - fn odin_sequence_cursors_are_scoped_by_target_and_signer() -> Result<()> { - let command = command(CommandKind::Deploy); - let mut ghostlight = - DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; - ghostlight.latest_odin_observation = Some(topology(7, 1)); - ghostlight.odin_publisher_sequence_cursor = 7; - let mut odin = DeploymentTransaction::new(&command, "odin".into(), 1, None, 100)?; - odin.latest_odin_observation = Some(topology(41, 2)); - odin.odin_publisher_sequence_cursor = 41; - let envelope = |key: &str| CultCacheEnvelope { - key: key.into(), - r#type: DeploymentTransaction::TYPE.into(), - payload: Vec::new(), - stored_at: "1970-01-01T00:00:00.100Z".into(), - schema_id: Some(DEPLOYMENT_TRANSACTION_SCHEMA.into()), - }; - let snapshot = ControlSnapshot { - commands: Vec::new(), - transactions: vec![ - Stored { - envelope: envelope("ghostlight"), - value: ghostlight, - }, - Stored { - envelope: envelope("odin"), - value: odin, - }, - ], - admitted: Vec::new(), - }; - - assert_eq!(snapshot.max_odin_sequence("ghostlight", "odin-signer"), 7); - assert_eq!(snapshot.max_odin_sequence("odin", "odin-signer"), 41); - assert_eq!(snapshot.max_odin_sequence("ghostlight", "other-signer"), 0); - Ok(()) - } - - #[test] - fn stale_odin_provider_receipt_cannot_gate_continuity_restart() -> Result<()> { - let touched = std::cell::Cell::new(false); - validate_live_providers_for_deploy(CommandKind::Continuity, || { - touched.set(true); - bail!("Odin provider receipt is stale") - })?; - assert!(!touched.get()); - assert!( - validate_live_providers_for_deploy(CommandKind::Deploy, || { - bail!("Odin provider receipt is stale") - }) - .is_err() - ); - Ok(()) - } - - #[test] - fn prepared_write_lease_is_durable_but_not_admitted_authority() -> Result<()> { - let lease = write_lease(); - lease.validate()?; - let prepared = LeasingEvidence::Prepared { - lease: lease.clone(), - lease_sha256: lease.canonical_sha256()?, - }; - let encoded = rmp_serde::to_vec(&prepared)?; - let decoded: LeasingEvidence = rmp_serde::from_slice(&encoded)?; - assert_eq!(decoded, prepared); - assert!(decoded.lease().is_none()); - assert!(decoded.lease_sha256().is_none()); - assert_eq!(decoded.prepared_lease().unwrap().0, &lease); - Ok(()) - } - - #[test] - fn runtime_instance_identity_is_stable_for_activation_prepare_replay() -> Result<()> { - let first = runtime_instance_id("tx-one")?; - assert_eq!(runtime_instance_id("tx-one")?, first); - assert_ne!(runtime_instance_id("tx-two")?, first); - Ok(()) - } - - #[test] - fn pre_fencing_abort_is_durable_before_cleanup_and_cannot_resume_as_success() -> Result<()> { - let command = command(CommandKind::Deploy); - let mut transaction = - DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; - transaction.pre_fencing_abort = Some(PreFencingAbort { - error: "sealed source was rejected".into(), - candidate_cleanup: CleanupEvidence::Skipped, - topology_reconciliation: CleanupEvidence::Skipped, - source_cleanup: CleanupEvidence::Pending, - }); - transaction.validate()?; - assert!(!transaction.is_terminal()); - - transaction - .pre_fencing_abort - .as_mut() - .unwrap() - .source_cleanup = CleanupEvidence::Complete; - transaction.phase = DeploymentPhase::Complete; - transaction.last_error = Some("sealed source was rejected".into()); - transaction.completion = Some(TransactionCompletion::FailedBeforeFencing { - error: "sealed source was rejected".into(), - }); - transaction.validate()?; - assert!(transaction.is_terminal()); - Ok(()) - } - - #[test] - fn activation_without_workload_still_requires_durable_candidate_cleanup() { - assert_eq!( - candidate_cleanup_requirement(true, false), - CleanupEvidence::Pending - ); - assert_eq!( - candidate_cleanup_requirement(true, true), - CleanupEvidence::Pending - ); - assert_eq!( - candidate_cleanup_requirement(false, false), - CleanupEvidence::Skipped - ); - } - - #[test] - fn bad_selector_becomes_one_terminal_refusal_record() -> Result<()> { - let command = command(CommandKind::Deploy); - let transaction = DeploymentTransaction::rejected( - &command, - anyhow!("deployment selector is unknown"), - 100, - )?; - assert!(transaction.is_terminal()); - assert_eq!(transaction.target, command.selector); - assert!(matches!( - transaction.completion, - Some(TransactionCompletion::FailedBeforeFencing { .. }) - )); - Ok(()) - } - - #[test] - fn post_commit_cleanup_keeps_exact_incumbent_work_owned_until_retired() -> Result<()> { - let incumbent = workload(1001, 40, 50); - let cleanup = PostCommitCleanup { - incumbent: IncumbentCleanupEvidence::Pending { - generation_id: "generation-old".into(), - workload: incumbent.clone(), - }, - source: SourceCleanupEvidence::Pending, - }; - let encoded = rmp_serde::to_vec(&cleanup)?; - let mut replayed: PostCommitCleanup = rmp_serde::from_slice(&encoded)?; - assert_eq!(replayed, cleanup); - assert!(!replayed.is_complete()); - replayed.incumbent = IncumbentCleanupEvidence::Complete { - generation_id: "generation-old".into(), - }; - assert!(!replayed.is_complete()); - replayed.source = SourceCleanupEvidence::Complete; - assert!(replayed.is_complete()); - Ok(()) - } - - #[test] - fn routed_incumbent_retirement_waits_for_the_declared_drain_deadline() -> Result<()> { - assert_eq!(route_drain_deadline(1_000, 30)?, 31_000); - assert!(route_drain_deadline(u64::MAX, 1).is_err()); - Ok(()) - } - - #[test] - fn admitted_route_receipt_refreshes_when_stale_or_implausibly_future_dated() { - assert!(route_observation_is_current(900, 1_000, 100, 10)); - assert!(route_observation_is_current(1_010, 1_000, 100, 10)); - assert!(!route_observation_is_current(899, 1_000, 100, 10)); - assert!(!route_observation_is_current(1_011, 1_000, 100, 10)); - } - - #[test] - fn lease_warming_requires_both_new_odin_and_new_provider_evidence() { - let old = digest('1'); - let fresh = digest('2'); - assert!(!provider_warming_advanced(7, &old, 7, &fresh)); - assert!(!provider_warming_advanced(7, &old, 8, &old)); - assert!(provider_warming_advanced(7, &old, 8, &fresh)); - } - - #[test] - fn failed_route_proof_never_restores_any_fenced_incumbent() { - assert!(!may_rollback_route_after_failed_proof( - &FencingEvidence::Revoked { - incumbent_lease_sha256: Some(digest('1')), - candidate_lease_path_verified_empty: false, - } - )); - assert!(!may_rollback_route_after_failed_proof( - &FencingEvidence::Revoked { - incumbent_lease_sha256: None, - candidate_lease_path_verified_empty: true, - } - )); - assert!(may_rollback_route_after_failed_proof( - &FencingEvidence::SkippedStateless - )); - } - - #[test] - fn fenced_writer_is_already_retired_before_post_commit_cleanup() { - assert!(incumbent_was_stopped_during_fencing( - &FencingEvidence::Revoked { - incumbent_lease_sha256: Some(digest('1')), - candidate_lease_path_verified_empty: false, - } - )); - assert!(!incumbent_was_stopped_during_fencing( - &FencingEvidence::Revoked { - incumbent_lease_sha256: None, - candidate_lease_path_verified_empty: true, - } - )); - assert!(!incumbent_was_stopped_during_fencing( - &FencingEvidence::SkippedStateless - )); - } - - #[test] - fn warming_accepts_only_the_exact_projected_incumbent_lease_disagreement() { - let expected_detail = format!("expected:{};activation:{}", digest('1'), digest('2')); - let incumbent = digest('3'); - let exact = OdinTopologyDisagreement { - code: "projected-write-lease".into(), - expected: Some(expected_detail.clone()), - observed: Some(incumbent.clone()), - }; - - assert!(warming_disagreements_match_incumbent( - &expected_detail, - Some(&incumbent), - &[exact.clone()], - )); - assert!(!warming_disagreements_match_incumbent( - &expected_detail, - None, - &[exact.clone()], - )); - - let mut substituted = exact.clone(); - substituted.observed = Some(digest('4')); - assert!(!warming_disagreements_match_incumbent( - &expected_detail, - Some(&incumbent), - &[substituted], - )); - assert!(!warming_disagreements_match_incumbent( - &expected_detail, - Some(&incumbent), - &[exact.clone(), exact], - )); - } - - #[test] - fn candidate_and_incumbent_must_differ_in_all_three_native_boundaries() { - let incumbent = workload(1001, 40, 50); - assert!(prove_isolation(&workload(1002, 41, 51), Some(&incumbent)).is_ok()); - assert!(prove_isolation(&workload(1001, 41, 51), Some(&incumbent)).is_err()); - assert!(prove_isolation(&workload(1002, 40, 51), Some(&incumbent)).is_err()); - assert!(prove_isolation(&workload(1002, 41, 50), Some(&incumbent)).is_err()); - } - - #[test] - fn exactly_one_inflight_transaction_may_own_a_target() -> Result<()> { - let command = command(CommandKind::Deploy); - let one = DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; - let two = DeploymentTransaction::new(&command, "ghostlight".into(), 1, None, 100)?; - let snapshot = ControlSnapshot { - commands: vec![Stored { - envelope: command_envelope(&command, 100)?, - value: command, - }], - transactions: vec![ - Stored { - envelope: transaction_envelope(&one, 100)?, - value: one, - }, - Stored { - envelope: transaction_envelope(&two, 100)?, - value: two, - }, - ], - admitted: Vec::new(), - }; - assert!(snapshot.validate_relations().is_err()); - Ok(()) - } - - #[test] - fn command_ordinals_do_not_turn_one_target_wait_into_a_global_brake() -> Result<()> { - let first_command = command(CommandKind::Deploy); - let first = DeploymentTransaction::new(&first_command, "odin".into(), 0, None, 100)?; - let second = DeploymentTransaction::new(&first_command, "ghostlight".into(), 1, None, 100)?; - let mut unrelated_command = command(CommandKind::Deploy); - unrelated_command.command_id = "up-unrelated".into(); - let unrelated = - DeploymentTransaction::new(&unrelated_command, "huginn".into(), 0, None, 101)?; - let snapshot = ControlSnapshot { - commands: Vec::new(), - transactions: [&first, &second, &unrelated] - .into_iter() - .map(|value| Stored { - envelope: transaction_envelope(value, 100).unwrap(), - value: value.clone(), - }) - .collect(), - admitted: Vec::new(), - }; - assert!(!snapshot.has_earlier_authority_sibling(&first)); - assert!(snapshot.has_earlier_authority_sibling(&second)); - assert!(!snapshot.has_earlier_authority_sibling(&unrelated)); - Ok(()) - } - - #[test] - fn complete_cleanup_remains_retryable_without_owning_the_current_incarnation() -> Result<()> { - let command = command(CommandKind::Deploy); - let mut transaction = - DeploymentTransaction::new(&command, "ghostlight".into(), 0, None, 100)?; - transaction.phase = DeploymentPhase::Complete; - transaction.completion = Some(TransactionCompletion::Admitted { - generation_id: format!("generation-{}", transaction.transaction_id), - }); - transaction.post_commit_cleanup = Some(PostCommitCleanup { - incumbent: IncumbentCleanupEvidence::SkippedNoIncumbent, - source: SourceCleanupEvidence::Pending, - }); - - assert!(!transaction.is_terminal()); - assert!(!transaction.owns_target_authority()); - assert!(transaction.blocks_new_target_mutation()); - Ok(()) - } - - #[test] - fn legacy_mutable_command_schema_is_rejected() -> Result<()> { - let temporary = tempfile::tempdir()?; - let path = temporary.path().join("control.cc"); - let legacy = CultCacheEnvelope { - key: "up-legacy".into(), - r#type: DeploymentCommand::TYPE.into(), - payload: rmp_serde::to_vec(&(DEPLOYMENT_COMMAND_SCHEMA, "up-legacy", "running"))?, - stored_at: rfc3339_millis(100)?, - schema_id: Some("idunn.deployment_command.v1".into()), - }; - assert!( - SingleFileMessagePackBackingStore::new(&path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: DeploymentCommand::TYPE.into(), - key: "up-legacy".into(), - current: None, - }], - &[legacy], - )? - ); - assert!(ControlSnapshot::read(&path).is_err()); - Ok(()) - } - - #[test] - fn loadcredential_era_control_schemas_are_rejected_before_rehydration() -> Result<()> { - for (record_type, key, schema) in [ - ( - DeploymentTransaction::TYPE, - "tx-legacy-workload-observation", - "idunn.deployment_transaction.v1", - ), - ( - AdmittedGeneration::TYPE, - "legacy-admitted-target", - "idunn.admitted_generation.v1", - ), - ] { - let temporary = tempfile::tempdir()?; - let path = temporary.path().join("control.cc"); - let legacy = CultCacheEnvelope { - key: key.into(), - r#type: record_type.into(), - payload: Vec::new(), - stored_at: rfc3339_millis(100)?, - schema_id: Some(schema.into()), - }; - assert!( - SingleFileMessagePackBackingStore::new(&path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: record_type.into(), - key: key.into(), - current: None, - }], - &[legacy], - )? - ); - assert!(ControlSnapshot::read(&path).is_err()); - } - Ok(()) - } - - #[test] - fn source_identity_is_explicit_and_atomic() { - let partial = parse( - ["serve", "--source-uid", "1001"] - .into_iter() - .map(str::to_owned), - ) - .unwrap_err() - .to_string(); - assert!(partial.contains("supplied together")); - let Command::Serve(options) = parse( - ["serve", "--source-uid", "1001", "--source-gid", "1002"] - .into_iter() - .map(str::to_owned), - ) - .unwrap() else { - panic!("expected serve") - }; - assert_eq!( - options.source_identity, - Some(ProcessIdentity { - uid: 1001, - gid: 1002 - }) - ); - } -} diff --git a/crates/idunn-daemon/src/deployment.rs b/crates/idunn-daemon/src/deployment.rs deleted file mode 100644 index bffd316..0000000 --- a/crates/idunn-daemon/src/deployment.rs +++ /dev/null @@ -1,2110 +0,0 @@ -use std::collections::{BTreeMap, BTreeSet}; -use std::net::IpAddr; -use std::path::{Component, Path, PathBuf}; - -use anyhow::{Context, Result, bail, ensure}; -use serde::{Deserialize, Serialize}; - -pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1"; -pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v2"; -pub const IDUNN_RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE"; -pub const IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_CANDIDATE_BIND"; -pub const IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE"; -pub const RUNTIME_PRESENCE_IDENTITY_BINDING: &str = "GAMECULT_RUNTIME_PRESENCE_IDENTITY"; -pub const RUNTIME_PRESENCE_IDENTITY_FD_NAME: &str = "gamecult-runtime-presence-identity"; - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct TargetDeclaration { - pub schema: String, - pub target: String, - pub source_stamp_environment: String, - #[serde(default)] - pub required_gitlinks: BTreeSet, - #[serde(default)] - pub steps: Vec, - #[serde(default)] - pub external_inputs: Vec, - pub artifacts: Vec, - pub service: ServiceDeclaration, - #[serde(default)] - pub state: Option, - #[serde(default)] - pub provides: Vec, - #[serde(default)] - pub dependencies: Vec, - #[serde(default)] - pub conflicts: Vec, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RunnerAffordance { - SourceRead, - ArtifactWrite, - BuildCache, - SecretRead, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RecipeStep { - pub id: String, - pub phase: RecipePhase, - pub runner: String, - pub argv: Vec, - #[serde(default = "default_working_directory")] - pub working_directory: PathBuf, - #[serde(default)] - pub required_environment: BTreeSet, -} - -fn default_working_directory() -> PathBuf { - PathBuf::from(".") -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RecipePhase { - Prepare, - Test, - Build, - Acceptance, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ExternalInput { - pub id: String, - pub url: String, - pub sha256: String, - pub runner: String, - pub destination: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ArtifactOutput { - pub id: String, - pub source_kind: ArtifactSource, - pub runner: Option, - pub source: PathBuf, - pub destination: PathBuf, - #[serde(default)] - pub expected_sha256: Option, - #[serde(default)] - pub executable: bool, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum ArtifactSource { - RunnerOutput, - WorktreeTree, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ServiceDeclaration { - pub executable_artifact: String, - #[serde(default)] - pub required_adjacent_artifacts: BTreeSet, - #[serde(default)] - pub arguments: Vec, - pub transport: ServiceTransport, - #[serde(default)] - pub route_required: bool, - pub health: HealthDeclaration, - #[serde(default)] - pub required_environment: BTreeSet, - #[serde(default)] - pub optional_environment: BTreeSet, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct HealthDeclaration { - pub contract: String, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum ServiceTransport { - Http, - Tcp, - Rudp, - Private, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] -pub enum LaunchArgument { - Literal { value: String }, - Binding { name: String }, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct StateDeclaration { - pub schema_generation: String, - pub slots: Vec, - pub migration: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct StateSlot { - pub id: String, - pub relative_path: PathBuf, - pub kind: StateKind, - pub schema: String, - pub writer: StateWriter, - pub recovery: StateRecovery, - pub startup: StateStartup, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct MigrationDeclaration { - pub executable_artifact: String, - pub from_generations: BTreeSet, - pub to_generation: String, - #[serde(default)] - pub arguments: Vec, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StateKind { - CultcacheFile, - /// Process-owned state imposed by an external runtime contract. Idunn - /// preserves and fences the file but never interprets its payload. - ExternalFile, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StateWriter { - ProcessBoundSingleWriter, - None, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StateRecovery { - Preserve, - Rebuildable, - ExternalAuthority, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StateStartup { - CreateOrOpenAfterWriteLease, - OpenAtStart, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ProvidedCapability { - pub capability: String, - pub schema: String, - pub compatibility: String, - #[serde(default = "one")] - pub capacity: u32, -} - -fn one() -> u32 { - 1 -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct CapabilityDependency { - pub kind: DependencyKind, - pub capability: String, - pub schema: String, - pub compatibility: String, - #[serde(default = "one")] - pub minimum_capacity: u32, - #[serde(default)] - pub startup: StartupOrder, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum DependencyKind { - Bootstrap, - Required, - Optional, - SharedInfrastructure, - Private, - ExternalOperatorBinding, -} - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum StartupOrder { - #[default] - BeforePromotion, - BeforeStart, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct CapabilityConflict { - pub capability: String, - pub reason: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct OperatorBinding { - pub schema: String, - pub target: String, - pub repository: RepositoryBinding, - pub runners: BTreeMap, - pub workload: WorkloadBinding, - pub runtime_identity: RuntimeIdentityBinding, - pub route: Option, - #[serde(default)] - pub process_write_lease: Option, - pub brakes: BrakeBinding, - pub rollout: RolloutBinding, - pub placement: PlacementBinding, - #[serde(default)] - pub external_capabilities: Vec, - #[serde(default)] - pub profiles: BTreeSet, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RepositoryBinding { - pub origin: String, - pub admitted_ref: String, - pub minimum_revision: String, - pub selection: SourceSelectionPolicy, - pub pinned_revision: Option, - pub release_authority_store: Option, - pub checkout: PathBuf, - pub recipe_path: PathBuf, - #[serde(default)] - pub gitlinks: BTreeMap, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum SourceSelectionPolicy { - PinnedObject, - RefHead, - SignedRelease, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct GitlinkBinding { - pub origin: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RunnerBinding { - pub driver: RunnerDriver, - pub image: String, - pub user: String, - #[serde(default)] - pub affordances: BTreeSet, - pub cache_root: Option, - #[serde(default)] - pub allowed_programs: BTreeSet, - #[serde(default)] - pub environment: BTreeMap, - #[serde(default)] - pub secret_files: BTreeMap, - #[serde(default)] - pub network_profile: Option, - pub memory_mebibytes: u32, - pub cpu_quota_percent: u32, - pub pids_limit: u32, - pub tmpfs_mebibytes: u32, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RunnerDriver { - Docker, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct WorkloadBinding { - pub driver: WorkloadDriver, - #[serde(default)] - pub state_group: Option, - pub unit_prefix: String, - pub release_root: PathBuf, - #[serde(default)] - pub state_root: Option, - pub runtime_root: PathBuf, - pub network: WorkloadNetwork, - pub hardening: WorkloadHardening, - #[serde(default)] - pub read_only_paths: BTreeSet, - #[serde(default)] - pub read_write_paths: BTreeSet, - #[serde(default)] - pub devices: BTreeSet, - pub memory_mebibytes: u32, - pub cpu_quota_percent: u32, - #[serde(default)] - pub environment: BTreeMap, - #[serde(default)] - pub secret_files: BTreeMap, - #[serde(default)] - pub argument_bindings: BTreeMap, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum WorkloadDriver { - SystemdTransient, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum WorkloadNetwork { - HostPrivate, - None, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum WorkloadHardening { - Strict, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RuntimeIdentityBinding { - pub runtime_id: String, - pub expected_signer_identity_id: String, - pub trust_anchor_store: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ProcessWriteLeaseBinding { - pub record_path: PathBuf, -} - -impl ProcessWriteLeaseBinding { - pub fn lock_path(&self) -> PathBuf { - let mut lock_path = self.record_path.as_os_str().to_os_string(); - lock_path.push(".lock"); - PathBuf::from(lock_path) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct BrakeBinding { - pub deployment_store: PathBuf, - pub lifecycle_store: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RouteBinding { - pub driver: RouteDriver, - pub route_id: String, - pub stable_endpoint: String, - pub private_host: String, - pub private_port_start: u16, - pub private_port_end: u16, - pub config_path: PathBuf, - pub reload_unit: String, -} - -impl RouteBinding { - pub(crate) fn stable_socket(&self) -> Result<(IpAddr, u16)> { - let authority = match self.driver { - RouteDriver::NginxStreamTcp => self - .stable_endpoint - .strip_prefix("http://") - .or_else(|| self.stable_endpoint.strip_prefix("tcp://")), - RouteDriver::NginxStreamUdp => self.stable_endpoint.strip_prefix("rudp://"), - } - .context("stable endpoint transport does not match its route driver")?; - ensure!( - !authority - .chars() - .any(|character| matches!(character, '/' | '?' | '#' | '@' | '[' | ']')), - "stable endpoint is not a plain IP address and port" - ); - let (host, port) = authority - .rsplit_once(':') - .context("stable endpoint has no port")?; - let host = host - .parse::() - .context("stable endpoint host is not an IP address")?; - ensure!( - matches!(host, IpAddr::V4(_)), - "route binding v2 stable endpoints use explicit IPv4 addresses" - ); - let port = port - .parse::() - .context("stable endpoint port is not a u16")?; - ensure!(port > 0, "stable endpoint port is zero"); - Ok((host, port)) - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RouteDriver { - NginxStreamTcp, - NginxStreamUdp, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct RolloutBinding { - pub strategy: RolloutStrategy, - pub drain_seconds: u32, - pub retain_releases: u32, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum RolloutStrategy { - CandidateThenPromote, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct PlacementBinding { - pub desired_replicas: u32, - pub nodes: BTreeSet, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ExternalCapabilityBinding { - pub provider_id: String, - pub capability: String, - pub schema: String, - pub compatibility: String, - #[serde(default = "one")] - pub capacity: u32, - pub endpoint: String, -} - -impl TargetDeclaration { - pub fn parse(input: &str) -> Result { - let declaration: Self = - toml::from_str(input).context("decoding strict Idunn target declaration")?; - declaration.validate()?; - Ok(declaration) - } - - pub fn validate(&self) -> Result<()> { - ensure!( - self.schema == TARGET_DECLARATION_SCHEMA, - "unsupported target declaration schema" - ); - require_id(&self.target, "target")?; - require_environment_name(&self.source_stamp_environment)?; - for path in &self.required_gitlinks { - require_relative_path(path, "required gitlink", false)?; - } - ensure!(!self.steps.is_empty(), "no recipe steps declared"); - ensure!(!self.artifacts.is_empty(), "no artifact outputs declared"); - - let mut runner_ids = BTreeSet::new(); - unique_ids( - self.steps.iter().map(|step| step.id.as_str()), - "recipe step", - )?; - for step in &self.steps { - require_id(&step.runner, "runner class")?; - runner_ids.insert(step.runner.as_str()); - ensure!(!step.argv.is_empty(), "step {} has empty argv", step.id); - for argument in &step.argv { - require_value(argument, "step argv")?; - } - require_program(&step.argv[0], "step program")?; - require_relative_path(&step.working_directory, "step working directory", true)?; - for name in &step.required_environment { - require_environment_name(name)?; - } - } - - let mut artifact_ids = BTreeSet::new(); - let mut destinations = BTreeSet::new(); - for artifact in &self.artifacts { - require_id(&artifact.id, "artifact")?; - ensure!( - artifact_ids.insert(artifact.id.as_str()), - "artifact id {} is declared twice", - artifact.id - ); - require_relative_path(&artifact.source, "artifact source", false)?; - require_leaf_path(&artifact.destination, "artifact destination")?; - if let Some(expected_sha256) = &artifact.expected_sha256 { - require_sha256(expected_sha256, "expected artifact sha256")?; - } - ensure!( - destinations.insert(artifact.destination.as_path()), - "artifact destination {} is declared twice", - artifact.destination.display() - ); - match (artifact.source_kind, artifact.runner.as_deref()) { - (ArtifactSource::RunnerOutput, Some(runner)) => { - require_id(runner, "artifact runner class")?; - runner_ids.insert(runner); - } - (ArtifactSource::WorktreeTree, None) => {} - (ArtifactSource::RunnerOutput, None) => { - bail!("runner-output artifact {} has no runner class", artifact.id) - } - (ArtifactSource::WorktreeTree, Some(_)) => bail!( - "worktree-tree artifact {} cannot name a runner class", - artifact.id - ), - } - } - let mut external_input_ids = BTreeSet::new(); - let mut external_input_destinations = BTreeSet::new(); - for input in &self.external_inputs { - require_id(&input.id, "external input")?; - ensure!( - external_input_ids.insert(input.id.as_str()), - "external input id {} is declared twice", - input.id - ); - ensure!( - !artifact_ids.contains(input.id.as_str()), - "external input id {} collides with an artifact id", - input.id - ); - require_value(&input.url, "external input URL")?; - https_host(&input.url)?; - require_sha256(&input.sha256, "external input sha256")?; - require_id(&input.runner, "external input runner class")?; - runner_ids.insert(input.runner.as_str()); - require_relative_path(&input.destination, "external input destination", false)?; - ensure!( - external_input_destinations - .insert((input.runner.as_str(), input.destination.as_path())), - "external input destination {} is declared twice for runner {}", - input.destination.display(), - input.runner - ); - } - ensure!( - !runner_ids.is_empty(), - "target declaration uses no runner classes" - ); - - ensure!( - artifact_ids.contains(self.service.executable_artifact.as_str()), - "service executable artifact is not declared" - ); - for artifact in &self.service.required_adjacent_artifacts { - ensure!( - artifact_ids.contains(artifact.as_str()), - "adjacent service artifact {artifact} is not declared" - ); - } - for argument in &self.service.arguments { - validate_launch_argument(argument)?; - } - require_capability(&self.service.health.contract, "health contract")?; - for name in &self.service.required_environment { - require_environment_name(name)?; - } - ensure!( - self.service - .required_environment - .contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), - "service does not require the standard Idunn runtime bundle" - ); - ensure!( - self.service - .required_environment - .contains(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT) - == self.service.route_required, - "routed services must require exactly one Idunn-owned candidate bind" - ); - ensure!( - self.service - .required_environment - .contains(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT) - == self.write_lease_required(), - "state-writing services must require exactly one Idunn-owned process write lease" - ); - for name in &self.service.optional_environment { - require_environment_name(name)?; - ensure!( - !self.service.required_environment.contains(name), - "service environment {name} is both required and optional" - ); - } - - if let Some(state) = &self.state { - require_capability(&state.schema_generation, "state schema generation")?; - ensure!(!state.slots.is_empty(), "state contract has no slots"); - let slot_ids = unique_ids( - state.slots.iter().map(|slot| slot.id.as_str()), - "state slot", - )?; - let mut slot_paths = BTreeSet::::new(); - for slot in &state.slots { - require_relative_path(&slot.relative_path, "state slot path", false)?; - ensure!( - slot_paths - .iter() - .all(|other| !paths_overlap(other, &slot.relative_path)), - "state slot path {} overlaps another declared slot", - slot.relative_path.display() - ); - ensure!( - slot_paths.insert(slot.relative_path.clone()), - "state slot path {} is declared twice", - slot.relative_path.display() - ); - require_capability(&slot.schema, "state slot schema")?; - match slot.writer { - StateWriter::ProcessBoundSingleWriter => ensure!( - slot.startup == StateStartup::CreateOrOpenAfterWriteLease, - "writable state slot {} must open after its write lease", - slot.id - ), - StateWriter::None => ensure!( - slot.startup == StateStartup::OpenAtStart, - "non-writable state slot {} cannot wait for a write lease", - slot.id - ), - } - if slot.recovery == StateRecovery::ExternalAuthority { - ensure!( - slot.writer == StateWriter::None, - "externally authoritative state slot {} cannot name the service as writer", - slot.id - ); - } - } - if let Some(migration) = state.migration.as_ref() { - ensure!( - artifact_ids.contains(migration.executable_artifact.as_str()), - "migration executable artifact is not declared" - ); - ensure!( - !migration.from_generations.is_empty(), - "migration has no source generations" - ); - for generation in &migration.from_generations { - require_capability(generation, "migration source generation")?; - } - ensure!( - migration.to_generation == state.schema_generation, - "migration destination differs from state generation" - ); - for argument in &migration.arguments { - validate_launch_argument(argument)?; - } - } - ensure!(slot_ids.len() == state.slots.len(), "duplicate state slot"); - } - - let mut provided = BTreeSet::new(); - for capability in &self.provides { - require_capability_contract( - &capability.capability, - &capability.schema, - &capability.compatibility, - )?; - ensure!( - capability.capacity > 0, - "provided capability capacity must be positive" - ); - ensure!( - provided.insert(capability.capability.as_str()), - "capability {} is provided twice", - capability.capability - ); - } - let mut required = BTreeSet::new(); - for dependency in &self.dependencies { - require_capability_contract( - &dependency.capability, - &dependency.schema, - &dependency.compatibility, - )?; - ensure!( - dependency.minimum_capacity > 0, - "dependency capacity must be positive" - ); - if dependency.kind == DependencyKind::Bootstrap { - ensure!( - dependency.startup == StartupOrder::BeforeStart, - "bootstrap dependency {} must be available before start", - dependency.capability - ); - } - ensure!( - required.insert((dependency.kind, dependency.capability.as_str())), - "dependency {} is declared twice for the same kind", - dependency.capability - ); - } - for conflict in &self.conflicts { - require_capability(&conflict.capability, "conflicting capability")?; - require_value(&conflict.reason, "conflict reason")?; - } - Ok(()) - } - - pub fn write_lease_required(&self) -> bool { - self.state.as_ref().is_some_and(|state| { - state - .slots - .iter() - .any(|slot| slot.writer == StateWriter::ProcessBoundSingleWriter) - }) - } -} - -impl OperatorBinding { - pub fn parse(input: &str) -> Result { - let binding: Self = - toml::from_str(input).context("decoding strict Idunn operator binding")?; - binding.validate()?; - Ok(binding) - } - - pub fn validate(&self) -> Result<()> { - ensure!( - self.schema == OPERATOR_BINDING_SCHEMA, - "unsupported operator binding schema" - ); - require_id(&self.target, "target")?; - ensure!( - self.repository.origin.starts_with("https://"), - "repository origin must use HTTPS" - ); - ensure!( - self.repository.origin.ends_with(".git"), - "repository origin must name a Git repository" - ); - ensure!( - self.repository.admitted_ref.starts_with("refs/heads/"), - "admitted ref must be a full branch ref" - ); - require_sha1(&self.repository.minimum_revision, "minimum revision")?; - match self.repository.selection { - SourceSelectionPolicy::PinnedObject => { - let revision = self.repository.pinned_revision.as_deref().ok_or_else(|| { - anyhow::anyhow!("pinned-object selection has no pinned revision") - })?; - require_sha1(revision, "pinned revision")?; - ensure!( - self.repository.release_authority_store.is_none(), - "pinned-object selection cannot name a release-authority store" - ); - } - SourceSelectionPolicy::RefHead => { - ensure!( - self.repository.pinned_revision.is_none(), - "ref-head selection cannot name a pinned revision" - ); - ensure!( - self.repository.release_authority_store.is_none(), - "ref-head selection cannot name a release-authority store" - ); - } - SourceSelectionPolicy::SignedRelease => { - ensure!( - self.repository.pinned_revision.is_none(), - "signed-release selection cannot name a pinned revision" - ); - let store = self - .repository - .release_authority_store - .as_ref() - .ok_or_else(|| { - anyhow::anyhow!("signed-release selection has no authority store") - })?; - require_absolute_path(store, "release authority store")?; - } - } - require_absolute_path(&self.repository.checkout, "repository checkout")?; - require_relative_path(&self.repository.recipe_path, "recipe path", false)?; - for (path, gitlink) in &self.repository.gitlinks { - require_relative_path(path, "gitlink binding", false)?; - require_git_origin(&gitlink.origin, "gitlink origin")?; - } - ensure!(!self.runners.is_empty(), "operator binding has no runners"); - for (id, runner) in &self.runners { - require_id(id, "runner binding")?; - require_pinned_image(&runner.image)?; - require_container_identity(&runner.user)?; - ensure!( - runner.memory_mebibytes > 0, - "runner memory must be positive" - ); - ensure!( - (1..=100_000).contains(&runner.cpu_quota_percent), - "runner CPU quota is outside 1..=100000" - ); - ensure!( - runner.pids_limit > 0 && runner.tmpfs_mebibytes > 0, - "runner process or temporary-filesystem limit is zero" - ); - if let Some(cache_root) = runner.cache_root.as_ref() { - require_absolute_path(cache_root, "runner cache root")?; - } - for program in &runner.allowed_programs { - require_program(program, "allowed runner program")?; - } - ensure!( - !runner.allowed_programs.is_empty(), - "runner {id} has no allowed programs" - ); - if let Some(profile) = &runner.network_profile { - require_capability(profile, "runner network profile")?; - } - for name in runner.environment.keys().chain(runner.secret_files.keys()) { - require_environment_name(name)?; - } - for value in runner.environment.values() { - require_value(value, "runner environment value")?; - } - for path in runner.secret_files.values() { - require_absolute_path(path, "runner secret file")?; - } - if runner.affordances.contains(&RunnerAffordance::BuildCache) { - ensure!( - runner.cache_root.is_some(), - "build-cache runner {id} has no cache root" - ); - } - ensure!( - runner.affordances.contains(&RunnerAffordance::BuildCache) - == runner.cache_root.is_some(), - "runner {id} cache root and build-cache affordance disagree" - ); - ensure!( - runner.affordances.contains(&RunnerAffordance::SecretRead) - == !runner.secret_files.is_empty(), - "runner {id} secret files and secret-read affordance disagree" - ); - } - let needs_state_group = - self.workload.state_root.is_some() || !self.workload.read_write_paths.is_empty(); - ensure!( - self.workload.state_group.is_some() == needs_state_group, - "a fixed state group must exist exactly when the dynamic workload has writable paths" - ); - if let Some(group) = &self.workload.state_group { - require_identity(group, "workload state group")?; - ensure!( - !matches!(group.as_str(), "root" | "0"), - "ordinary Idunn workload state cannot use the root group" - ); - } - require_id(&self.workload.unit_prefix, "workload unit prefix")?; - require_id(&self.runtime_identity.runtime_id, "runtime id")?; - require_id( - &self.runtime_identity.expected_signer_identity_id, - "expected signer identity id", - )?; - require_absolute_path( - &self.runtime_identity.trust_anchor_store, - "trust anchor store", - )?; - for (label, path) in [ - ("release root", &self.workload.release_root), - ("runtime root", &self.workload.runtime_root), - ("deployment brake", &self.brakes.deployment_store), - ("lifecycle brake", &self.brakes.lifecycle_store), - ] { - require_absolute_path(path, label)?; - } - if let Some(state_root) = &self.workload.state_root { - require_absolute_path(state_root, "state root")?; - } - if let Some(write_lease) = &self.process_write_lease { - require_absolute_path(&write_lease.record_path, "process write-lease record")?; - require_absolute_path(&write_lease.lock_path(), "process write-lease lock")?; - } - ensure!( - self.brakes.deployment_store != self.brakes.lifecycle_store, - "deployment and lifecycle brakes are identical" - ); - ensure!( - self.workload.memory_mebibytes > 0, - "workload memory must be positive" - ); - ensure!( - (1..=100_000).contains(&self.workload.cpu_quota_percent), - "workload CPU quota is outside 1..=100000" - ); - for path in self - .workload - .read_only_paths - .iter() - .chain(self.workload.read_write_paths.iter()) - .chain(self.workload.devices.iter()) - { - require_absolute_path(path, "workload path")?; - } - for path in &self.workload.read_write_paths { - if let Some(state_root) = &self.workload.state_root { - ensure!( - !paths_overlap(path, state_root), - "workload read-write path {} overlaps the write-lease-controlled state root", - path.display() - ); - } - } - let mut authority_paths = vec![ - self.brakes.deployment_store.clone(), - self.brakes.lifecycle_store.clone(), - ]; - if let Some(write_lease) = &self.process_write_lease { - authority_paths.push(write_lease.record_path.clone()); - authority_paths.push(write_lease.lock_path()); - } - ensure!( - authority_paths.iter().enumerate().all(|(index, path)| { - authority_paths[index + 1..] - .iter() - .all(|other| !paths_overlap(path, other)) - }), - "Idunn authority paths overlap" - ); - let mut protected_paths = authority_paths; - protected_paths.extend([ - self.workload.release_root.clone(), - self.workload.runtime_root.clone(), - self.runtime_identity.trust_anchor_store.clone(), - ]); - if let Some(route) = &self.route { - protected_paths.push(route.config_path.clone()); - } - if let Some(release_authority) = &self.repository.release_authority_store { - protected_paths.push(release_authority.clone()); - } - for authority_path in protected_paths { - for writable_path in self - .workload - .read_write_paths - .iter() - .chain(self.workload.state_root.iter()) - { - ensure!( - !paths_overlap(&authority_path, writable_path), - "workload writable path {} overlaps Idunn authority {}", - writable_path.display(), - authority_path.display() - ); - } - } - for name in self - .workload - .environment - .keys() - .chain(self.workload.secret_files.keys()) - { - require_environment_name(name)?; - ensure!( - name != IDUNN_RUNTIME_BUNDLE_ENVIRONMENT - && name != IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, - "operator binding attempts to replace Idunn-owned workload environment" - ); - } - for value in self.workload.environment.values() { - require_value(value, "workload environment value")?; - } - for name in self.workload.argument_bindings.keys() { - require_id(name, "argument binding")?; - } - for value in self.workload.argument_bindings.values() { - require_value(value, "argument binding value")?; - } - for path in self.workload.secret_files.values() { - require_absolute_path(path, "secret file")?; - } - ensure!( - !self - .workload - .environment - .contains_key(RUNTIME_PRESENCE_IDENTITY_BINDING), - "runtime presence identity must be a parent-only secret source, not environment" - ); - let presence_identity = self - .workload - .secret_files - .get(RUNTIME_PRESENCE_IDENTITY_BINDING) - .context("workload has no parent-only runtime presence identity source")?; - ensure!( - self.workload - .secret_files - .iter() - .filter(|(_, path)| *path == presence_identity) - .count() - == 1, - "runtime presence identity source is duplicated under another secret binding" - ); - if let Some(route) = self.route.as_ref() { - require_id(&route.route_id, "route id")?; - require_value(&route.stable_endpoint, "stable endpoint")?; - require_host(&route.private_host)?; - let private_host: IpAddr = route - .private_host - .parse() - .context("route private host is not an IP address")?; - ensure!( - private_host.is_loopback(), - "systemd-transient candidate routes must use a loopback private host" - ); - ensure!( - route.private_port_start > 0, - "private port range starts at zero" - ); - ensure!( - route.private_port_start < route.private_port_end, - "candidate rollout requires at least two private ports" - ); - route.stable_socket()?; - require_absolute_path(&route.config_path, "route config")?; - require_unit(&route.reload_unit, "route reload unit")?; - } - ensure!( - self.rollout.retain_releases >= 2, - "candidate rollout must retain at least current and prior releases" - ); - ensure!( - self.placement.desired_replicas == 1, - "operator binding v2 admits exactly one replica" - ); - ensure!( - self.placement.nodes.len() == 1, - "operator binding v2 admits exactly one node" - ); - for node in &self.placement.nodes { - require_id(node, "placement node")?; - } - let mut external_contracts = BTreeSet::new(); - for capability in &self.external_capabilities { - require_id(&capability.provider_id, "external provider id")?; - require_capability_contract( - &capability.capability, - &capability.schema, - &capability.compatibility, - )?; - ensure!( - capability.capacity > 0, - "external capability capacity must be positive" - ); - require_value(&capability.endpoint, "external capability endpoint")?; - ensure!( - external_contracts.insert(( - capability.provider_id.as_str(), - capability.capability.as_str(), - capability.schema.as_str(), - capability.compatibility.as_str(), - )), - "external provider capability is declared twice" - ); - } - for profile in &self.profiles { - require_capability(profile, "profile")?; - } - Ok(()) - } - - pub fn admit(&self, declaration: &TargetDeclaration) -> Result<()> { - ensure!( - self.target == declaration.target, - "binding target does not match declaration" - ); - let bound_gitlinks: BTreeSet<_> = self.repository.gitlinks.keys().cloned().collect(); - ensure!( - bound_gitlinks == declaration.required_gitlinks, - "operator gitlink bindings do not exactly match required gitlinks" - ); - let declared_runners: BTreeSet<_> = declaration - .steps - .iter() - .map(|step| step.runner.as_str()) - .chain( - declaration - .external_inputs - .iter() - .map(|input| input.runner.as_str()), - ) - .chain( - declaration - .artifacts - .iter() - .filter_map(|artifact| artifact.runner.as_deref()), - ) - .collect(); - let bound_runners: BTreeSet<_> = self.runners.keys().map(String::as_str).collect(); - ensure!( - declared_runners == bound_runners, - "operator runner classes do not exactly match the target recipe" - ); - ensure!( - self.process_write_lease.is_some() == declaration.write_lease_required(), - "process write-lease binding must exist exactly when writable state requires it" - ); - ensure!( - self.workload.state_root.is_some() == declaration.state.is_some(), - "state root binding must exist exactly when the recipe declares state" - ); - for step in &declaration.steps { - let binding = &self.runners[&step.runner]; - ensure!( - !binding - .environment - .contains_key(&declaration.source_stamp_environment) - && !binding - .secret_files - .contains_key(&declaration.source_stamp_environment), - "runner cannot replace the Idunn source stamp" - ); - ensure!( - binding.affordances.contains(&RunnerAffordance::SourceRead), - "runner {} cannot read the frozen source", - step.runner - ); - ensure!( - binding.allowed_programs.contains(&step.argv[0]), - "runner {} does not admit program {}", - step.runner, - step.argv[0] - ); - let available: BTreeSet<_> = binding - .environment - .keys() - .chain(binding.secret_files.keys()) - .cloned() - .collect(); - ensure!( - step.required_environment.is_subset(&available), - "step {} lacks required runner environment", - step.id - ); - } - for artifact in declaration - .artifacts - .iter() - .filter(|artifact| artifact.source_kind == ArtifactSource::RunnerOutput) - { - let runner_id = artifact - .runner - .as_deref() - .expect("validated runner-output artifact"); - let binding = &self.runners[runner_id]; - ensure!( - binding - .affordances - .contains(&RunnerAffordance::ArtifactWrite), - "runner {runner_id} cannot publish artifact {}", - artifact.id - ); - } - for input in &declaration.external_inputs { - let binding = &self.runners[&input.runner]; - ensure!( - binding.affordances.contains(&RunnerAffordance::SourceRead), - "external input {} runner cannot read the frozen source", - input.id - ); - ensure!( - binding - .affordances - .contains(&RunnerAffordance::ArtifactWrite), - "external input {} runner cannot materialize inputs", - input.id - ); - ensure!( - binding.network_profile.is_some(), - "external input {} runner has no operator-bound network profile", - input.id - ); - } - ensure!( - !self - .workload - .environment - .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) - && !self - .workload - .secret_files - .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) - && !self - .workload - .environment - .contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT) - && !self - .workload - .secret_files - .contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT), - "operator binding cannot replace Idunn-owned launch environment" - ); - ensure!( - !self - .workload - .environment - .contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT) - && !self - .workload - .secret_files - .contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT), - "operator binding cannot replace the Idunn-owned process write lease" - ); - let mut available_environment: BTreeSet<_> = self - .workload - .environment - .keys() - .chain( - self.workload - .secret_files - .keys() - .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING), - ) - .cloned() - .collect(); - available_environment.insert(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into()); - if declaration.service.route_required { - available_environment.insert(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into()); - } - if declaration.write_lease_required() { - available_environment.insert(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into()); - } - ensure!( - declaration - .service - .required_environment - .is_subset(&available_environment), - "operator service environment omits a required launch input" - ); - let declared_environment: BTreeSet<_> = declaration - .service - .required_environment - .union(&declaration.service.optional_environment) - .cloned() - .collect(); - ensure!( - !declared_environment.contains(RUNTIME_PRESENCE_IDENTITY_BINDING), - "runtime presence identity is a named parent-only descriptor, not service environment" - ); - ensure!( - available_environment.is_subset(&declared_environment), - "operator service environment contains an undeclared launch input" - ); - let required_arguments: BTreeSet<_> = declaration - .service - .arguments - .iter() - .filter_map(|argument| match argument { - LaunchArgument::Binding { name } => Some(name.clone()), - LaunchArgument::Literal { .. } => None, - }) - .collect(); - let bound_arguments: BTreeSet<_> = - self.workload.argument_bindings.keys().cloned().collect(); - match ( - self.workload.state_root.as_ref(), - self.workload.argument_bindings.get("state_root"), - ) { - (Some(state_root), Some(argument)) => ensure!( - Path::new(argument) == state_root.as_path(), - "state_root argument binding differs from the admitted workload state root" - ), - (Some(_), None) => bail!("stateful workload has no state_root argument binding"), - (None, Some(_)) => bail!("stateless workload carries a state_root argument binding"), - (None, None) => {} - } - ensure!( - required_arguments.contains("state_root") == self.workload.state_root.is_some(), - "recipe state_root argument must exist exactly when the workload has a state root" - ); - ensure!( - required_arguments == bound_arguments, - "operator argument bindings do not exactly match the launch contract" - ); - match ( - declaration.service.route_required, - declaration.service.transport, - self.route.as_ref(), - ) { - (true, ServiceTransport::Http, Some(route)) - if route.driver == RouteDriver::NginxStreamTcp => - { - ensure!( - route.stable_endpoint.starts_with("http://"), - "HTTP stable endpoint is not an HTTP URI" - ); - } - (true, ServiceTransport::Tcp, Some(route)) - if route.driver == RouteDriver::NginxStreamTcp => - { - ensure!( - route.stable_endpoint.starts_with("tcp://"), - "TCP stable endpoint is not a TCP URI" - ); - } - (true, ServiceTransport::Rudp, Some(route)) - if route.driver == RouteDriver::NginxStreamUdp => - { - ensure!( - route.stable_endpoint.starts_with("rudp://"), - "RUDP stable endpoint is not an RUDP URI" - ); - } - (false, _, None) => {} - _ => bail!("route binding does not match the launch contract"), - } - for dependency in declaration - .dependencies - .iter() - .filter(|dependency| dependency.kind == DependencyKind::ExternalOperatorBinding) - { - ensure!( - self.external_capabilities.iter().any(|binding| { - capability_compatible( - &dependency.capability, - &dependency.schema, - &dependency.compatibility, - &binding.capability, - &binding.schema, - &binding.compatibility, - ) && binding.capacity >= dependency.minimum_capacity - }), - "external capability {} has no compatible operator binding", - dependency.capability - ); - } - Ok(()) - } -} - -pub fn capability_compatible( - required_capability: &str, - required_schema: &str, - required_compatibility: &str, - provided_capability: &str, - provided_schema: &str, - provided_compatibility: &str, -) -> bool { - required_capability == provided_capability - && required_schema == provided_schema - && required_compatibility == provided_compatibility -} - -fn unique_ids<'a>(values: impl Iterator, label: &str) -> Result> { - let mut seen = BTreeSet::new(); - for value in values { - require_id(value, label)?; - ensure!(seen.insert(value), "duplicate {label} id {value}"); - } - Ok(seen) -} - -fn require_id(value: &str, label: &str) -> Result<()> { - ensure!(!value.is_empty(), "{label} is empty"); - ensure!(value.len() <= 96, "{label} is too long"); - ensure!( - value.bytes().all(|byte| byte.is_ascii_lowercase() - || byte.is_ascii_digit() - || matches!(byte, b'-' | b'_' | b'.' | b':')), - "{label} contains an invalid character" - ); - Ok(()) -} - -fn require_capability(value: &str, label: &str) -> Result<()> { - require_id(value, label) -} - -fn require_capability_contract(capability: &str, schema: &str, compatibility: &str) -> Result<()> { - require_capability(capability, "capability")?; - require_capability(schema, "capability schema")?; - require_capability(compatibility, "capability compatibility") -} - -fn require_value(value: &str, label: &str) -> Result<()> { - ensure!(!value.trim().is_empty(), "{label} is empty"); - ensure!( - !value.chars().any(char::is_control), - "{label} contains a control character" - ); - ensure!(value.len() <= 4_096, "{label} is too long"); - Ok(()) -} - -fn validate_launch_argument(argument: &LaunchArgument) -> Result<()> { - match argument { - LaunchArgument::Literal { value } => require_value(value, "literal launch argument"), - LaunchArgument::Binding { name } => require_id(name, "launch argument binding"), - } -} - -fn require_program(value: &str, label: &str) -> Result<()> { - require_value(value, label)?; - ensure!( - !value.contains('/') && !value.contains('\\') && !value.chars().any(char::is_whitespace), - "{label} must be one executable name" - ); - Ok(()) -} - -fn require_git_origin(value: &str, label: &str) -> Result<()> { - ensure!(value.starts_with("https://"), "{label} must use HTTPS"); - ensure!( - value.ends_with(".git"), - "{label} must name a Git repository" - ); - require_value(value, label) -} - -fn https_host(value: &str) -> Result<&str> { - let remainder = value - .strip_prefix("https://") - .ok_or_else(|| anyhow::anyhow!("artifact URL must use HTTPS"))?; - let authority = remainder.split('/').next().unwrap_or_default(); - ensure!(!authority.is_empty(), "artifact URL has no host"); - ensure!( - !authority.contains('@') - && !authority.contains(':') - && !authority.contains('?') - && !authority.contains('#'), - "artifact URL authority is not a bare host" - ); - require_host(authority)?; - Ok(authority) -} - -fn require_environment_name(value: &str) -> Result<()> { - ensure!( - !value.is_empty() && value.len() <= 128, - "environment name is empty or too long" - ); - ensure!( - value - .bytes() - .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_'), - "environment name contains an invalid character" - ); - Ok(()) -} - -fn require_relative_path(path: &Path, label: &str, allow_current: bool) -> Result<()> { - ensure!(!path.as_os_str().is_empty(), "{label} is empty"); - ensure!(!path.is_absolute(), "{label} must be relative"); - let mut saw_normal = false; - for component in path.components() { - match component { - Component::Normal(_) => saw_normal = true, - Component::CurDir if allow_current => {} - _ => bail!("{label} contains a forbidden path component"), - } - } - ensure!(allow_current || saw_normal, "{label} must name a path"); - Ok(()) -} - -fn require_leaf_path(path: &Path, label: &str) -> Result<()> { - require_relative_path(path, label, false)?; - ensure!( - path.components().count() == 1, - "{label} must be a single filename" - ); - Ok(()) -} - -fn require_absolute_path(path: &Path, label: &str) -> Result<()> { - ensure!(path.is_absolute(), "{label} must be absolute"); - ensure!( - path.components() - .all(|component| !matches!(component, Component::ParentDir)), - "{label} contains a parent traversal" - ); - Ok(()) -} - -fn paths_overlap(left: &Path, right: &Path) -> bool { - left.starts_with(right) || right.starts_with(left) -} - -fn require_sha1(value: &str, label: &str) -> Result<()> { - require_lower_hex(value, 40, label) -} - -fn require_sha256(value: &str, label: &str) -> Result<()> { - require_lower_hex(value, 64, label) -} - -fn require_lower_hex(value: &str, length: usize, label: &str) -> Result<()> { - ensure!(value.len() == length, "{label} has the wrong length"); - ensure!( - value - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), - "{label} must be lowercase hexadecimal" - ); - Ok(()) -} - -fn require_pinned_image(value: &str) -> Result<()> { - let Some((name, digest)) = value.rsplit_once("@sha256:") else { - bail!("runner image is not pinned by sha256 digest"); - }; - require_value(name, "runner image name")?; - require_sha256(digest, "runner image digest") -} - -fn require_container_identity(value: &str) -> Result<()> { - let (uid, gid) = value - .split_once(':') - .context("runner user must be an explicit numeric UID:GID")?; - let uid: u32 = uid.parse().context("runner UID is not a u32")?; - let gid: u32 = gid.parse().context("runner GID is not a u32")?; - ensure!(uid > 0 && gid > 0, "runner identity must be unprivileged"); - Ok(()) -} - -fn require_identity(value: &str, label: &str) -> Result<()> { - require_id(value, label) -} - -fn require_unit(value: &str, label: &str) -> Result<()> { - require_value(value, label)?; - ensure!( - value.ends_with(".service"), - "{label} must be a systemd service unit" - ); - ensure!( - !value.contains('/') && !value.contains('\\'), - "{label} must not contain a path" - ); - Ok(()) -} - -fn require_host(value: &str) -> Result<()> { - require_value(value, "host")?; - ensure!( - !value.contains('/') && !value.contains('@'), - "host contains an invalid delimiter" - ); - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - const RECIPE: &str = r#" -schema = "gamecult.idunn.target_declaration.v1" -target = "test-service" -source_stamp_environment = "TEST_SERVICE_BUILD_COMMIT" - -[[steps]] -id = "test" -phase = "test" -runner = "rust" -argv = ["cargo", "test", "--locked", "-p", "test-service"] - -[[steps]] -id = "build" -phase = "build" -runner = "rust" -argv = ["cargo", "build", "--locked", "--release", "-p", "test-service"] - -[[external_inputs]] -id = "tool" -url = "https://registry.npmjs.org/tool.tgz" -sha256 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -runner = "rust" -destination = "inputs/tool.tgz" - -[[artifacts]] -id = "daemon" -source_kind = "runner-output" -runner = "rust" -source = "target/release/test-service" -destination = "test-service" -expected_sha256 = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" -executable = true - -[service] -executable_artifact = "daemon" -required_adjacent_artifacts = [] -arguments = [ - { kind = "literal", value = "--state-root" }, - { kind = "binding", name = "state_root" }, -] -transport = "http" -route_required = true -required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_PROCESS_WRITE_LEASE", "GAMECULT_IDUNN_RUNTIME_BUNDLE"] - -[service.health] -contract = "test-service.cultnet-service-health" - -[state] -schema_generation = "v1" - -[[state.slots]] -id = "world" -relative_path = "world.cc" -kind = "cultcache-file" -schema = "test-service.state.v1" -writer = "process-bound-single-writer" -recovery = "preserve" -startup = "create-or-open-after-write-lease" - -[[provides]] -capability = "test-service.runtime" -schema = "test-service.state.v1" -compatibility = "v1" - -[[dependencies]] -kind = "shared-infrastructure" -capability = "odin.verse-rendezvous" -schema = "odin.verse-topology.v1" -compatibility = "v1" -startup = "before-promotion" -"#; - - const BINDING: &str = r#" -schema = "gamecult.idunn.operator_binding.v2" -target = "test-service" -profiles = ["aetheria", "full-gamecult"] - -[repository] -origin = "https://github.com/GameCult/TestService.git" -admitted_ref = "refs/heads/main" -minimum_revision = "13d5136b69bcd3a52c400d66a940f94a47122e48" -selection = "ref-head" -checkout = "/srv/build/TestService" -recipe_path = "deployment/idunn/recipe.toml" - -[runners.rust] -driver = "docker" -image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" -user = "1000:1000" -affordances = ["source-read", "artifact-write", "build-cache"] -cache_root = "/srv/ghostlight/build-cache" -allowed_programs = ["cargo"] -network_profile = "build-dependency-egress" -memory_mebibytes = 8192 -cpu_quota_percent = 400 -pids_limit = 512 -tmpfs_mebibytes = 1024 - -[workload] -driver = "systemd-transient" -state_group = "test-service" -unit_prefix = "idunn-test-service" -release_root = "/srv/test-service/releases" -state_root = "/var/lib/gamecult/test-service" -runtime_root = "/etc/gamecult/test-service/runtime" -network = "host-private" -hardening = "strict" -memory_mebibytes = 2048 -cpu_quota_percent = 200 - -[workload.argument_bindings] -state_root = "/var/lib/gamecult/test-service" - -[workload.secret_files] -GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/test-service/runtime-presence-identity.cc" - -[runtime_identity] -runtime_id = "test-service-yggdrasil" -expected_signer_identity_id = "test-service-runtime-signer" -trust_anchor_store = "/etc/gamecult/trust/test-service.cc" - -[route] -driver = "nginx-stream-tcp" -route_id = "test-service-public" -stable_endpoint = "http://127.0.0.1:18830" -private_host = "127.0.0.1" -private_port_start = 18831 -private_port_end = 18839 -config_path = "/etc/nginx/idunn-stream-routes/test-service.conf" -reload_unit = "nginx.service" - -[process_write_lease] -record_path = "/etc/gamecult/test-service/runtime/process-write-lease.cc" - -[brakes] -deployment_store = "/var/lib/gamecult/idunn-authority/test-service-deployment-brake.cc" -lifecycle_store = "/var/lib/gamecult/idunn-authority/test-service-lifecycle-brake.cc" - -[rollout] -strategy = "candidate-then-promote" -drain_seconds = 30 -retain_releases = 2 - -[placement] -desired_replicas = 1 -nodes = ["yggdrasil"] -"#; - - #[test] - fn strict_recipe_and_binding_admit_without_host_privilege_in_recipe() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let binding = OperatorBinding::parse(BINDING).unwrap(); - binding.admit(&recipe).unwrap(); - assert_eq!(recipe.target, "test-service"); - assert_eq!(binding.route.as_ref().unwrap().private_port_start, 18831); - } - - #[test] - fn unknown_recipe_field_is_rejected() { - let input = RECIPE.replace( - "target = \"test-service\"", - "target = \"test-service\"\nroot = true", - ); - assert!(TargetDeclaration::parse(&input).is_err()); - } - - #[test] - fn operator_cannot_omit_an_affordance_derived_from_the_recipe() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let input = BINDING.replace( - "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", - "affordances = [\"source-read\", \"build-cache\"]", - ); - let binding = OperatorBinding::parse(&input).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn runner_mounts_require_their_exact_operator_affordance() { - let cache_without_affordance = BINDING.replace( - "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", - "affordances = [\"source-read\", \"artifact-write\"]", - ); - assert!(OperatorBinding::parse(&cache_without_affordance).is_err()); - - let affordance_without_secret = BINDING.replace( - "affordances = [\"source-read\", \"artifact-write\", \"build-cache\"]", - "affordances = [\"source-read\", \"artifact-write\", \"build-cache\", \"secret-read\"]", - ); - assert!(OperatorBinding::parse(&affordance_without_secret).is_err()); - } - - #[test] - fn runner_cannot_replace_the_source_stamp() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let forged = BINDING.replace( - "[workload]", - "[runners.rust.environment]\nTEST_SERVICE_BUILD_COMMIT = \"forged\"\n\n[workload]", - ); - let binding = OperatorBinding::parse(&forged).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn route_transport_mismatch_is_rejected() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - assert!( - OperatorBinding::parse(&BINDING.replace("nginx-stream-tcp", "nginx-stream-udp")) - .is_err() - ); - - assert!( - OperatorBinding::parse( - &BINDING.replace("http://127.0.0.1:18830", "tcp://yggdrasil.gamecult.org:443",), - ) - .is_err() - ); - assert!(recipe.service.route_required); - } - - #[test] - fn rudp_route_uses_the_udp_stream_driver() { - let recipe = TargetDeclaration::parse( - &RECIPE.replace("transport = \"http\"", "transport = \"rudp\""), - ) - .unwrap(); - let binding = OperatorBinding::parse( - &BINDING - .replace("nginx-stream-tcp", "nginx-stream-udp") - .replace("http://127.0.0.1:18830", "rudp://127.0.0.1:17872"), - ) - .unwrap(); - assert!(binding.admit(&recipe).is_ok()); - } - - #[test] - fn raw_unit_template_is_rejected() { - let input = RECIPE.replace( - "executable_artifact = \"daemon\"", - "executable_artifact = \"daemon\"\nunit_template = \"deployment/root.service\"", - ); - assert!(TargetDeclaration::parse(&input).is_err()); - } - - #[test] - fn extra_operator_environment_is_rejected() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let input = BINDING.replace( - "[workload.argument_bindings]", - "[workload.environment]\nUNDECLARED = \"no\"\n\n[workload.argument_bindings]", - ); - let binding = OperatorBinding::parse(&input).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn runtime_presence_identity_is_exactly_one_parent_only_secret_source() { - let secret = "[workload.secret_files]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n"; - assert!(OperatorBinding::parse(&BINDING.replace(secret, "")).is_err()); - assert!(OperatorBinding::parse(&BINDING.replace( - secret, - "[workload.environment]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n", - )) - .is_err()); - assert!(OperatorBinding::parse(&BINDING.replace( - secret, - "[workload.secret_files]\nGAMECULT_RUNTIME_PRESENCE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\nDUPLICATE_IDENTITY = \"/etc/gamecult/test-service/runtime-presence-identity.cc\"\n\n", - )) - .is_err()); - - let recipe = TargetDeclaration::parse(&RECIPE.replace( - "required_environment = [", - "required_environment = [\"GAMECULT_RUNTIME_PRESENCE_IDENTITY\", ", - )) - .unwrap(); - let binding = OperatorBinding::parse(BINDING).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn operator_cannot_replace_idunn_owned_launch_inputs() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - for name in [ - IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, - IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, - IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, - ] { - let input = BINDING.replace( - "[workload.argument_bindings]", - &format!( - "[workload.environment]\n{name} = \"forged\"\n\n[workload.argument_bindings]" - ), - ); - let rejected = OperatorBinding::parse(&input) - .and_then(|binding| binding.admit(&recipe).map(|_| ())); - assert!(rejected.is_err()); - } - } - - #[test] - fn state_slot_path_cannot_escape_the_operator_owned_root() { - let input = RECIPE.replace( - "relative_path = \"world.cc\"", - "relative_path = \"../world.cc\"", - ); - assert!(TargetDeclaration::parse(&input).is_err()); - } - - #[test] - fn mismatched_state_root_argument_binding_is_rejected() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let mismatched = BINDING.replace( - "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"", - "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/another-service\"", - ); - let binding = OperatorBinding::parse(&mismatched).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn external_file_state_keeps_process_bound_writer_and_lease_semantics() { - let input = RECIPE.replace("kind = \"cultcache-file\"", "kind = \"external-file\""); - let declaration = TargetDeclaration::parse(&input).unwrap(); - let slot = &declaration.state.as_ref().unwrap().slots[0]; - assert_eq!(slot.kind, StateKind::ExternalFile); - assert_eq!(slot.writer, StateWriter::ProcessBoundSingleWriter); - assert_eq!(slot.startup, StateStartup::CreateOrOpenAfterWriteLease); - assert!(declaration.write_lease_required()); - } - - #[test] - fn state_slots_cannot_claim_overlapping_paths() { - let input = RECIPE.replace( - "startup = \"create-or-open-after-write-lease\"", - "startup = \"create-or-open-after-write-lease\"\n\n[[state.slots]]\nid = \"nested\"\nrelative_path = \"world.cc/index.cc\"\nkind = \"cultcache-file\"\nschema = \"test-service.index.v1\"\nwriter = \"process-bound-single-writer\"\nrecovery = \"preserve\"\nstartup = \"create-or-open-after-write-lease\"", - ); - assert!(TargetDeclaration::parse(&input).is_err()); - } - - #[test] - fn workload_mount_cannot_bypass_write_lease_controlled_state() { - let input = BINDING.replace( - "cpu_quota_percent = 200\n\n[workload.argument_bindings]", - "cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.argument_bindings]", - ); - assert!(OperatorBinding::parse(&input).is_err()); - } - - #[test] - fn v2_binding_is_explicitly_singleton() { - assert!( - OperatorBinding::parse( - &BINDING.replace("desired_replicas = 1", "desired_replicas = 2") - ) - .is_err() - ); - assert!( - OperatorBinding::parse(&BINDING.replace( - "nodes = [\"yggdrasil\"]", - "nodes = [\"yggdrasil\", \"raven\"]" - )) - .is_err() - ); - } - - #[test] - fn legacy_static_workload_identity_has_no_authority_in_v2() { - assert!( - OperatorBinding::parse(&BINDING.replace( - "schema = \"gamecult.idunn.operator_binding.v2\"", - "schema = \"gamecult.idunn.operator_binding.v1\"", - ),) - .is_err() - ); - let with_static_identity = BINDING.replace( - "driver = \"systemd-transient\"", - "driver = \"systemd-transient\"\nuser = \"test-service\"\ngroup = \"test-service\"", - ); - assert!(OperatorBinding::parse(&with_static_identity).is_err()); - } - - #[test] - fn writable_state_requires_one_non_root_state_group() { - assert!( - OperatorBinding::parse(&BINDING.replace("state_group = \"test-service\"\n", ""),) - .is_err() - ); - assert!( - OperatorBinding::parse( - &BINDING.replace("state_group = \"test-service\"", "state_group = \"root\""), - ) - .is_err() - ); - } - - #[test] - fn external_input_uses_an_operator_bound_network_profile() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let binding = OperatorBinding::parse(BINDING).unwrap(); - binding.admit(&recipe).unwrap(); - let isolated = BINDING.replace("network_profile = \"build-dependency-egress\"\n", ""); - let binding = OperatorBinding::parse(&isolated).unwrap(); - assert!(binding.admit(&recipe).is_err()); - } - - #[test] - fn process_write_lease_exists_exactly_for_process_writable_state() { - let recipe = TargetDeclaration::parse(RECIPE).unwrap(); - let without_lease = BINDING.replace( - "[process_write_lease]\nrecord_path = \"/etc/gamecult/test-service/runtime/process-write-lease.cc\"\n\n", - "", - ); - let binding = OperatorBinding::parse(&without_lease).unwrap(); - assert!(binding.admit(&recipe).is_err()); - - let read_only_recipe = TargetDeclaration::parse( - &RECIPE - .replace( - "writer = \"process-bound-single-writer\"", - "writer = \"none\"", - ) - .replace( - "startup = \"create-or-open-after-write-lease\"", - "startup = \"open-at-start\"", - ) - .replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", ""), - ) - .unwrap(); - binding.admit(&read_only_recipe).unwrap(); - - let binding = OperatorBinding::parse(BINDING).unwrap(); - assert_eq!( - binding.process_write_lease.as_ref().unwrap().lock_path(), - PathBuf::from("/etc/gamecult/test-service/runtime/process-write-lease.cc.lock") - ); - assert!(binding.admit(&read_only_recipe).is_err()); - } - - #[test] - fn stateless_target_has_no_state_binding_or_generation() { - let mut recipe_text = RECIPE.replace( - "arguments = [\n { kind = \"literal\", value = \"--state-root\" },\n { kind = \"binding\", name = \"state_root\" },\n]", - "arguments = []", - ); - recipe_text = recipe_text.replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", ""); - let state_start = recipe_text.find("\n[state]\n").unwrap(); - let state_end = recipe_text.find("\n[[provides]]\n").unwrap(); - recipe_text.replace_range(state_start..state_end, ""); - let recipe = TargetDeclaration::parse(&recipe_text).unwrap(); - assert!(recipe.state.is_none()); - - let binding_text = BINDING - .replace("state_group = \"test-service\"\n", "") - .replace("state_root = \"/var/lib/gamecult/test-service\"\n", "") - .replace( - "[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"\n\n", - "", - ) - .replace( - "[process_write_lease]\nrecord_path = \"/etc/gamecult/test-service/runtime/process-write-lease.cc\"\n\n", - "", - ); - let binding = OperatorBinding::parse(&binding_text).unwrap(); - binding.admit(&recipe).unwrap(); - assert!(binding.workload.state_root.is_none()); - } - - #[test] - fn bootstrap_dependency_must_be_available_before_start() { - let invalid = RECIPE.replace("kind = \"shared-infrastructure\"", "kind = \"bootstrap\""); - assert!(TargetDeclaration::parse(&invalid).is_err()); - let valid = invalid.replace( - "startup = \"before-promotion\"", - "startup = \"before-start\"", - ); - TargetDeclaration::parse(&valid).unwrap(); - } - - #[test] - fn dependency_compatibility_requires_exact_typed_contract() { - assert!(capability_compatible( - "odin.verse-rendezvous", - "odin.verse-topology.v1", - "v1", - "odin.verse-rendezvous", - "odin.verse-topology.v1", - "v1", - )); - assert!(!capability_compatible( - "odin.verse-rendezvous", - "odin.verse-topology.v1", - "v1", - "odin.verse-rendezvous", - "odin.verse-topology.v2", - "v2", - )); - } -} diff --git a/crates/idunn-daemon/src/deployment_plan.rs b/crates/idunn-daemon/src/deployment_plan.rs deleted file mode 100644 index 9475701..0000000 --- a/crates/idunn-daemon/src/deployment_plan.rs +++ /dev/null @@ -1,1560 +0,0 @@ -use std::collections::{BTreeMap, BTreeSet}; -use std::path::PathBuf; - -use anyhow::{Context, Result, bail, ensure}; -pub use cultnet_rs::IdunnExpectedIncarnationRecord as ExpectedIncarnation; -use cultnet_rs::{ - IDUNN_EXPECTED_INCARNATION_SCHEMA, IdunnExpectedCapability, IdunnExpectedDependency, - IdunnExpectedRoute, -}; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; - -use crate::control_plane::SequenceAdmittedReady; -use crate::deployment::{ - CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, - ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration, - capability_compatible, -}; - -pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; -pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v2"; -pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct GitlinkTreeFact { - pub origin: String, - pub revision: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "policy", rename_all = "kebab-case", deny_unknown_fields)] -pub enum SourceSelection { - PinnedObject, - RefHead, - SignedRelease { release_authority_id: String }, -} - -/// Private facts emitted by the future narrow source driver. Structural -/// validation here does not prove Git ancestry, signatures, or object custody. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct SourceSelectionFacts { - pub schema: String, - pub origin: String, - pub admitted_ref: String, - pub admitted_ref_revision: String, - pub revision: String, - pub source_tree: String, - pub recipe_path: PathBuf, - pub recipe_blob_sha256: String, - pub gitlinks: BTreeMap, - pub selection: SourceSelection, - pub selected_at_unix_millis: u64, -} - -impl SourceSelectionFacts { - pub fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { - binding.validate()?; - ensure!( - self.schema == SOURCE_SELECTION_FACTS_SCHEMA, - "unsupported source-selection facts schema" - ); - ensure!( - self.origin == binding.repository.origin, - "selected origin differs from binding" - ); - ensure!( - self.admitted_ref == binding.repository.admitted_ref, - "selected ref differs from binding" - ); - require_sha1( - &self.admitted_ref_revision, - "selected admitted-ref revision", - )?; - require_sha1(&self.revision, "selected revision")?; - require_sha1(&self.source_tree, "selected source tree")?; - ensure!( - self.recipe_path == binding.repository.recipe_path, - "selected recipe path differs from binding" - ); - require_sha256(&self.recipe_blob_sha256, "selected recipe blob")?; - ensure!( - self.selected_at_unix_millis > 0, - "source-selection facts have no time" - ); - - match (&binding.repository.selection, &self.selection) { - (SourceSelectionPolicy::PinnedObject, SourceSelection::PinnedObject) => { - let pinned_revision = binding - .repository - .pinned_revision - .as_deref() - .context("validated pinned binding has no revision")?; - ensure!( - self.revision == pinned_revision, - "selected source differs from the pinned revision" - ); - } - (SourceSelectionPolicy::RefHead, SourceSelection::RefHead) => ensure!( - self.revision == self.admitted_ref_revision, - "ref-head selection differs from the fetched admitted-ref revision" - ), - ( - SourceSelectionPolicy::SignedRelease, - SourceSelection::SignedRelease { - release_authority_id, - }, - ) => { - require_token(release_authority_id, "release authority id")?; - } - _ => bail!("source-selection facts do not match operator policy"), - } - - let expected_paths: BTreeSet<_> = binding.repository.gitlinks.keys().cloned().collect(); - let observed_paths: BTreeSet<_> = self.gitlinks.keys().cloned().collect(); - ensure!( - expected_paths == observed_paths, - "Gitlink tree facts differ from binding" - ); - for (path, receipt) in &self.gitlinks { - let expected = &binding.repository.gitlinks[path]; - ensure!( - receipt.origin == expected.origin, - "Gitlink {} origin differs from binding", - path.display() - ); - require_sha1(&receipt.revision, "Gitlink revision")?; - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)] -pub enum DependencyProviderAuthority { - ManagedReady { - target: String, - incarnation_id: String, - plan_id: String, - sealed_release_id: String, - expected_projection_sha256: String, - odin_topology_correlation_sha256: String, - odin_topology_publisher_sequence: u64, - }, - ExternalOperatorBinding { - binding_target: String, - }, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct DependencyProviderRef { - pub provider_id: String, - pub authority: DependencyProviderAuthority, - pub capability: String, - pub schema: String, - pub compatibility: String, - pub capacity: u32, - pub endpoint: Option, -} - -impl DependencyProviderRef { - pub fn validate(&self) -> Result<()> { - require_token(&self.provider_id, "dependency provider id")?; - require_contract(&self.capability, &self.schema, &self.compatibility)?; - ensure!(self.capacity > 0, "dependency provider capacity is zero"); - if let Some(endpoint) = &self.endpoint { - require_value(endpoint, "dependency provider endpoint")?; - } - match &self.authority { - DependencyProviderAuthority::ManagedReady { - target, - incarnation_id, - plan_id, - sealed_release_id, - expected_projection_sha256, - odin_topology_correlation_sha256, - odin_topology_publisher_sequence, - } => { - require_token(target, "managed provider target")?; - require_token(incarnation_id, "managed provider incarnation")?; - require_sha256(plan_id, "managed provider plan")?; - require_sha256(sealed_release_id, "managed provider sealed release")?; - require_sha256(expected_projection_sha256, "managed expected projection")?; - require_sha256( - odin_topology_correlation_sha256, - "managed Odin topology correlation", - )?; - ensure!( - *odin_topology_publisher_sequence > 0, - "managed Odin topology sequence is zero" - ); - } - DependencyProviderAuthority::ExternalOperatorBinding { binding_target } => { - require_token(binding_target, "external binding target")?; - ensure!( - self.endpoint.is_some(), - "external binding has no configured endpoint" - ); - } - } - Ok(()) - } - - fn compatible_with(&self, dependency: &CapabilityDependency) -> bool { - capability_compatible( - &dependency.capability, - &dependency.schema, - &dependency.compatibility, - &self.capability, - &self.schema, - &self.compatibility, - ) && self.capacity >= dependency.minimum_capacity - } - - fn is_external_binding(&self) -> bool { - matches!( - &self.authority, - DependencyProviderAuthority::ExternalOperatorBinding { .. } - ) - } -} - -pub fn external_binding_provider_refs( - binding: &OperatorBinding, -) -> Result> { - binding.validate()?; - let providers = binding - .external_capabilities - .iter() - .map(|capability| expected_external_provider(&binding.target, capability)) - .collect::>(); - for provider in &providers { - provider.validate()?; - } - Ok(providers) -} - -fn expected_external_provider( - binding_target: &str, - capability: &ExternalCapabilityBinding, -) -> DependencyProviderRef { - DependencyProviderRef { - provider_id: capability.provider_id.clone(), - authority: DependencyProviderAuthority::ExternalOperatorBinding { - binding_target: binding_target.to_owned(), - }, - capability: capability.capability.clone(), - schema: capability.schema.clone(), - compatibility: capability.compatibility.clone(), - capacity: capability.capacity, - endpoint: Some(capability.endpoint.clone()), - } -} - -/// Projects one authenticated Ready correlation into dependency-selection -/// inputs. Authentication is not replay admission: the control-plane store -/// must atomically admit `publisher_sequence` before calling this function and -/// must re-check the selected receipt before promotion. -fn managed_ready_provider_refs( - admitted: &SequenceAdmittedReady, -) -> Result> { - let expected = admitted.expected(); - let topology = admitted.authenticated(); - expected.validate()?; - let expected_projection_sha256 = expected.canonical_sha256()?; - let observed = topology.record(); - ensure!( - observed.expected_projection_sha256 == expected_projection_sha256, - "Ready provider topology names a different Expected projection" - ); - ensure!( - observed.target == expected.target && observed.runtime_id == expected.runtime_id, - "Ready provider topology names a different target or runtime" - ); - ensure!( - observed.expected - && observed.present - && observed.ready - && observed.observed_presence_state.as_deref() == Some("active") - && observed.disagreements.is_empty(), - "managed provider topology is not Ready" - ); - let odin_topology_correlation_sha256 = admitted.evidence_sha256().to_owned(); - let endpoint = expected - .route - .as_ref() - .map(|route| route.stable_endpoint.clone()); - let mut providers = Vec::new(); - for capability in &observed.observed_capabilities { - providers.push(DependencyProviderRef { - provider_id: expected.target.clone(), - authority: DependencyProviderAuthority::ManagedReady { - target: expected.target.clone(), - incarnation_id: expected.incarnation_id.clone(), - plan_id: expected.plan_id.clone(), - sealed_release_id: expected.sealed_release_id.clone(), - expected_projection_sha256: expected_projection_sha256.clone(), - odin_topology_correlation_sha256: odin_topology_correlation_sha256.clone(), - odin_topology_publisher_sequence: admitted.publisher_sequence(), - }, - capability: capability.capability.clone(), - schema: capability.schema.clone(), - compatibility: capability.compatibility.clone(), - capacity: capability.capacity, - endpoint: endpoint.clone(), - }); - } - for provider in &providers { - provider.validate()?; - } - Ok(providers) -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct DependencySelection { - pub requirement: CapabilityDependency, - pub provider: Option, -} - -impl DependencySelection { - fn validate(&self) -> Result<()> { - require_contract( - &self.requirement.capability, - &self.requirement.schema, - &self.requirement.compatibility, - )?; - ensure!( - self.requirement.minimum_capacity > 0, - "dependency capacity is zero" - ); - if let Some(provider) = &self.provider { - provider.validate()?; - ensure!( - provider.compatible_with(&self.requirement), - "selected provider is incompatible" - ); - ensure!( - (self.requirement.kind == DependencyKind::ExternalOperatorBinding) - == provider.is_external_binding(), - "selected provider authority does not match dependency kind" - ); - } else { - ensure!( - self.requirement.kind == DependencyKind::Optional, - "required dependency is unresolved" - ); - } - Ok(()) - } - - fn expected_projection(&self) -> Result { - self.validate()?; - let ( - provider_id, - provider_authority, - provider_expected_projection_sha256, - provider_endpoint, - ) = match &self.provider { - None => (None, None, None, None), - Some(provider) => match &provider.authority { - DependencyProviderAuthority::ManagedReady { - expected_projection_sha256, - .. - } => ( - Some(provider.provider_id.clone()), - Some("managed-incarnation".into()), - Some(expected_projection_sha256.clone()), - provider.endpoint.clone(), - ), - DependencyProviderAuthority::ExternalOperatorBinding { .. } => ( - Some(provider.provider_id.clone()), - Some("external-operator-binding".into()), - None, - provider.endpoint.clone(), - ), - }, - }; - Ok(IdunnExpectedDependency { - kind: dependency_kind_name(self.requirement.kind).into(), - capability: self.requirement.capability.clone(), - schema: self.requirement.schema.clone(), - compatibility: self.requirement.compatibility.clone(), - minimum_capacity: self.requirement.minimum_capacity, - startup: startup_order_name(self.requirement.startup).into(), - provider_id, - provider_authority, - provider_expected_projection_sha256, - provider_endpoint, - }) - } -} - -pub fn select_dependencies( - declaration: &TargetDeclaration, - providers: &[DependencyProviderRef], -) -> Result> { - declaration.validate()?; - let mut unique_contracts = BTreeSet::new(); - for provider in providers { - provider.validate()?; - ensure!( - unique_contracts.insert(( - provider.provider_id.as_str(), - provider.capability.as_str(), - provider.schema.as_str(), - provider.compatibility.as_str(), - )), - "dependency provider contract is duplicated" - ); - } - for conflict in &declaration.conflicts { - if providers - .iter() - .any(|provider| provider.capability == conflict.capability) - { - bail!( - "capability conflict {}: {}", - conflict.capability, - conflict.reason - ); - } - } - declaration - .dependencies - .iter() - .map(|dependency| { - let mut candidates = providers - .iter() - .filter(|provider| provider.compatible_with(dependency)) - .filter(|provider| { - (dependency.kind == DependencyKind::ExternalOperatorBinding) - == provider.is_external_binding() - }) - .cloned() - .collect::>(); - candidates.sort_by(|left, right| { - left.provider_id - .cmp(&right.provider_id) - .then_with(|| left.endpoint.cmp(&right.endpoint)) - }); - let provider = candidates.into_iter().next(); - if provider.is_none() && dependency.kind != DependencyKind::Optional { - bail!( - "no expected provider satisfies {} {} {}", - dependency.capability, - dependency.schema, - dependency.compatibility - ); - } - let selection = DependencySelection { - requirement: dependency.clone(), - provider, - }; - selection.validate()?; - Ok(selection) - }) - .collect() -} - -/// Private Idunn control-plane state. This contains host binding details and is -/// never an Odin/CultMesh projection. The shared CultNet Expected incarnation -/// is the sanitized topology projection derived only after a release validates -/// against this plan. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct CompiledDeploymentPlan { - pub schema: String, - pub plan_id: String, - pub incarnation_id: String, - pub created_at_unix_millis: u64, - pub source: SourceSelectionFacts, - pub recipe_blob: Vec, - pub binding_blob: Vec, - pub dependencies: Vec, - pub candidate_port: Option, -} - -impl CompiledDeploymentPlan { - pub fn validate(&self) -> Result<()> { - ensure!( - self.schema == COMPILED_DEPLOYMENT_PLAN_SCHEMA, - "unsupported deployment-plan schema" - ); - require_sha256(&self.plan_id, "plan id")?; - require_token(&self.incarnation_id, "plan incarnation")?; - ensure!( - self.created_at_unix_millis >= self.source.selected_at_unix_millis, - "deployment plan predates source selection" - ); - let (declaration, binding) = self.parsed_inputs()?; - self.source.validate_against(&binding)?; - ensure!( - sha256_id(&self.recipe_blob) == self.source.recipe_blob_sha256, - "selected recipe blob differs from the exact plan recipe bytes" - ); - let declared_requirements: Vec<_> = declaration.dependencies.iter().collect(); - let selected_requirements: Vec<_> = self - .dependencies - .iter() - .map(|selection| &selection.requirement) - .collect(); - ensure!( - declared_requirements == selected_requirements, - "selected graph differs from recipe dependencies" - ); - for selection in &self.dependencies { - selection.validate()?; - } - match (&binding.route, self.candidate_port) { - (Some(bound), Some(port)) => { - ensure!( - (bound.private_port_start..=bound.private_port_end).contains(&port), - "candidate port is outside the operator range" - ); - } - (None, None) => {} - (Some(_), None) => bail!("routed deployment has no candidate port"), - (None, Some(_)) => bail!("private deployment selected a routed candidate port"), - } - ensure!( - self.plan_id == self.recomputed_plan_id()?, - "deployment plan digest is not canonical" - ); - Ok(()) - } - - pub(crate) fn parsed_inputs(&self) -> Result<(TargetDeclaration, OperatorBinding)> { - let recipe_text = std::str::from_utf8(&self.recipe_blob) - .context("stored deployment recipe is not UTF-8")?; - let binding_text = std::str::from_utf8(&self.binding_blob) - .context("stored operator binding is not UTF-8")?; - let declaration = TargetDeclaration::parse(recipe_text)?; - let binding = OperatorBinding::parse(binding_text)?; - binding.admit(&declaration)?; - Ok((declaration, binding)) - } - - fn recomputed_plan_id(&self) -> Result { - let mut unsigned = self.clone(); - unsigned.plan_id.clear(); - Ok(sha256_id( - &rmp_serde::to_vec(&unsigned).context("encoding deployment plan identity")?, - )) - } -} - -#[allow(clippy::too_many_arguments)] -pub(crate) fn compile_deployment_plan( - recipe_bytes: &[u8], - binding_bytes: &[u8], - source: SourceSelectionFacts, - incarnation_id: impl Into, - candidate_port: Option, - created_at_unix_millis: u64, - managed_ready_providers: &[SequenceAdmittedReady], -) -> Result { - let recipe_text = std::str::from_utf8(recipe_bytes).context("recipe is not UTF-8")?; - let binding_text = std::str::from_utf8(binding_bytes).context("binding is not UTF-8")?; - let declaration = TargetDeclaration::parse(recipe_text)?; - let binding = OperatorBinding::parse(binding_text)?; - binding.admit(&declaration)?; - source.validate_against(&binding)?; - ensure!( - sha256_id(recipe_bytes) == source.recipe_blob_sha256, - "selected recipe blob differs from compiled recipe bytes" - ); - let incarnation_id = incarnation_id.into(); - require_token(&incarnation_id, "deployment incarnation")?; - ensure!( - created_at_unix_millis >= source.selected_at_unix_millis, - "deployment plan predates source selection" - ); - let mut providers = Vec::new(); - for provider in managed_ready_providers { - providers.extend(managed_ready_provider_refs(provider)?); - } - providers.extend(external_binding_provider_refs(&binding)?); - let dependencies = select_dependencies(&declaration, &providers)?; - let mut plan = CompiledDeploymentPlan { - schema: COMPILED_DEPLOYMENT_PLAN_SCHEMA.into(), - plan_id: String::new(), - incarnation_id, - created_at_unix_millis, - source, - recipe_blob: recipe_bytes.to_vec(), - binding_blob: binding_bytes.to_vec(), - dependencies, - candidate_port, - }; - plan.plan_id = plan.recomputed_plan_id()?; - plan.validate()?; - Ok(plan) -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ArtifactReceipt { - pub artifact_id: String, - pub destination: PathBuf, - pub sha256: String, - pub size_bytes: u64, - pub executable: bool, -} - -impl ArtifactReceipt { - fn validate(&self, declaration: &TargetDeclaration) -> Result<()> { - require_token(&self.artifact_id, "artifact receipt id")?; - require_sha256(&self.sha256, "artifact digest")?; - ensure!(self.size_bytes > 0, "sealed artifact is empty"); - let declared = declaration - .artifacts - .iter() - .find(|artifact| artifact.id == self.artifact_id) - .with_context(|| format!("plan declares no artifact {}", self.artifact_id))?; - ensure!( - self.destination == declared.destination && self.executable == declared.executable, - "artifact receipt differs from declared output" - ); - if let Some(expected_sha256) = &declared.expected_sha256 { - ensure!( - self.sha256 == prefixed_sha256(expected_sha256), - "artifact receipt differs from the recipe-pinned digest" - ); - } - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct ExternalInputMaterializationReceipt { - pub input_id: String, - pub url: String, - pub sha256: String, - pub runner: String, - pub destination: PathBuf, - pub size_bytes: u64, -} - -impl ExternalInputMaterializationReceipt { - fn validate(&self, declaration: &TargetDeclaration) -> Result<()> { - require_token(&self.input_id, "external input receipt id")?; - require_sha256(&self.sha256, "external input materialized digest")?; - ensure!(self.size_bytes > 0, "materialized external input is empty"); - let declared = declaration - .external_inputs - .iter() - .find(|input| input.id == self.input_id) - .with_context(|| format!("plan declares no external input {}", self.input_id))?; - ensure!( - self.url == declared.url - && self.sha256 == prefixed_sha256(&declared.sha256) - && self.runner == declared.runner - && self.destination == declared.destination, - "external input receipt differs from its pinned declaration" - ); - Ok(()) - } -} - -/// Private Idunn release state. The content address covers the exact plan ID, -/// sorted full artifact receipts, and sorted full external-input -/// materialization receipts. The plan remains a separate private record and is -/// supplied explicitly for validation. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct SealedRelease { - pub schema: String, - pub sealed_release_id: String, - pub plan_id: String, - pub artifacts: Vec, - pub external_inputs: Vec, - pub sealed_at_unix_millis: u64, -} - -impl SealedRelease { - pub fn new( - plan: &CompiledDeploymentPlan, - mut artifacts: Vec, - mut external_inputs: Vec, - sealed_at_unix_millis: u64, - ) -> Result { - plan.validate()?; - artifacts.sort_by(|left, right| left.artifact_id.cmp(&right.artifact_id)); - external_inputs.sort_by(|left, right| left.input_id.cmp(&right.input_id)); - let mut release = Self { - schema: SEALED_RELEASE_SCHEMA.into(), - sealed_release_id: String::new(), - plan_id: plan.plan_id.clone(), - artifacts, - external_inputs, - sealed_at_unix_millis, - }; - release.validate_contents(plan)?; - release.sealed_release_id = release.recomputed_release_id()?; - release.validate_against(plan)?; - Ok(release) - } - - pub fn validate_against(&self, plan: &CompiledDeploymentPlan) -> Result<()> { - require_sha256(&self.sealed_release_id, "sealed release id")?; - self.validate_contents(plan)?; - ensure!( - self.sealed_release_id == self.recomputed_release_id()?, - "sealed release content address is wrong" - ); - Ok(()) - } - - pub fn expected_projection( - &self, - plan: &CompiledDeploymentPlan, - ) -> Result { - self.validate_against(plan)?; - let (declaration, binding) = plan.parsed_inputs()?; - let route = match (&binding.route, plan.candidate_port) { - (Some(bound), Some(port)) => Some(IdunnExpectedRoute { - route_id: bound.route_id.clone(), - transport: endpoint_scheme(declaration.service.transport)?.into(), - stable_endpoint: bound.stable_endpoint.clone(), - candidate_endpoint: format!( - "{}://{}:{port}", - endpoint_scheme(declaration.service.transport)?, - bound.private_host - ), - }), - (None, None) => None, - _ => bail!("plan route and candidate port disagree"), - }; - let artifact_sha256 = self - .artifacts - .iter() - .find(|artifact| artifact.artifact_id == declaration.service.executable_artifact) - .context("sealed release has no executable service artifact")? - .sha256 - .clone(); - let mut capabilities = declaration - .provides - .iter() - .map(|capability| IdunnExpectedCapability { - capability: capability.capability.clone(), - schema: capability.schema.clone(), - compatibility: capability.compatibility.clone(), - minimum_capacity: capability.capacity, - }) - .collect::>(); - capabilities.sort_by(|left, right| { - (&left.capability, &left.schema, &left.compatibility).cmp(&( - &right.capability, - &right.schema, - &right.compatibility, - )) - }); - let mut dependencies = plan - .dependencies - .iter() - .map(DependencySelection::expected_projection) - .collect::>>()?; - dependencies.sort_by(|left, right| { - (&left.capability, &left.schema, &left.compatibility).cmp(&( - &right.capability, - &right.schema, - &right.compatibility, - )) - }); - let expected = ExpectedIncarnation { - schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), - target: declaration.target.clone(), - plan_id: plan.plan_id.clone(), - incarnation_id: plan.incarnation_id.clone(), - sealed_release_id: self.sealed_release_id.clone(), - source_repository: canonical_repository_identity(&plan.source.origin)?, - source_revision: plan.source.revision.clone(), - recipe_sha256: plan.source.recipe_blob_sha256.clone(), - runtime_id: binding.runtime_identity.runtime_id.clone(), - expected_signer_identity_id: binding - .runtime_identity - .expected_signer_identity_id - .clone(), - health_contract: declaration.service.health.contract.clone(), - artifact_sha256, - state_schema_generation: declaration - .state - .as_ref() - .map(|state| state.schema_generation.clone()), - state_contract_sha256: declaration - .state - .as_ref() - .map(canonical_state_contract_sha256) - .transpose()?, - write_lease_required: declaration.write_lease_required(), - route, - capabilities, - dependencies, - }; - expected.validate()?; - Ok(expected) - } - - fn validate_contents(&self, plan: &CompiledDeploymentPlan) -> Result<()> { - ensure!( - self.schema == SEALED_RELEASE_SCHEMA, - "unsupported sealed-release schema" - ); - plan.validate()?; - let (declaration, _) = plan.parsed_inputs()?; - require_sha256(&self.plan_id, "sealed plan id")?; - ensure!( - self.plan_id == plan.plan_id, - "sealed release refers to a different plan" - ); - ensure!( - self.sealed_at_unix_millis >= plan.created_at_unix_millis, - "release was sealed before it was planned" - ); - ensure!( - self.artifacts - .windows(2) - .all(|pair| pair[0].artifact_id < pair[1].artifact_id), - "artifact receipts are not in canonical order" - ); - ensure!( - self.external_inputs - .windows(2) - .all(|pair| pair[0].input_id < pair[1].input_id), - "external input receipts are not in canonical order" - ); - let declared_ids = declaration - .artifacts - .iter() - .map(|artifact| artifact.id.as_str()) - .collect::>(); - let observed_ids = self - .artifacts - .iter() - .map(|artifact| artifact.artifact_id.as_str()) - .collect::>(); - ensure!( - declared_ids == observed_ids && observed_ids.len() == self.artifacts.len(), - "artifact receipts differ from declared outputs" - ); - for artifact in &self.artifacts { - artifact.validate(&declaration)?; - } - let declared_input_ids = declaration - .external_inputs - .iter() - .map(|input| input.id.as_str()) - .collect::>(); - let observed_input_ids = self - .external_inputs - .iter() - .map(|input| input.input_id.as_str()) - .collect::>(); - ensure!( - declared_input_ids == observed_input_ids - && observed_input_ids.len() == self.external_inputs.len(), - "external input receipts differ from pinned declarations" - ); - for input in &self.external_inputs { - input.validate(&declaration)?; - } - Ok(()) - } - - fn recomputed_release_id(&self) -> Result { - Ok(sha256_id( - &rmp_serde::to_vec(&( - self.schema.as_str(), - self.plan_id.as_str(), - &self.artifacts, - &self.external_inputs, - )) - .context("encoding sealed release identity")?, - )) - } -} - -fn canonical_state_contract_sha256(state: &StateDeclaration) -> Result { - Ok(sha256_id( - &rmp_serde::to_vec(state).context("encoding canonical state contract")?, - )) -} - -fn endpoint_scheme(transport: ServiceTransport) -> Result<&'static str> { - match transport { - ServiceTransport::Http => Ok("http"), - ServiceTransport::Tcp => Ok("tcp"), - ServiceTransport::Rudp | ServiceTransport::Private => { - bail!("service transport has no stable route scheme") - } - } -} - -fn dependency_kind_name(kind: DependencyKind) -> &'static str { - match kind { - DependencyKind::Bootstrap => "bootstrap", - DependencyKind::Required => "required", - DependencyKind::Optional => "optional", - DependencyKind::SharedInfrastructure => "shared-infrastructure", - DependencyKind::Private => "private", - DependencyKind::ExternalOperatorBinding => "external-operator-binding", - } -} - -fn startup_order_name(startup: StartupOrder) -> &'static str { - match startup { - StartupOrder::BeforePromotion => "before-promotion", - StartupOrder::BeforeStart => "before-start", - } -} - -fn canonical_repository_identity(origin: &str) -> Result { - let identity = origin - .strip_prefix("https://") - .and_then(|origin| origin.strip_suffix(".git")) - .context("validated Git origin is not a canonical HTTPS repository")?; - Ok(identity.to_owned()) -} - -fn prefixed_sha256(raw_sha256: &str) -> String { - format!("sha256-{raw_sha256}") -} - -fn require_token(value: &str, label: &str) -> Result<()> { - ensure!( - !value.is_empty() && value.len() <= 128, - "{label} is empty or too long" - ); - ensure!( - value - .bytes() - .all(|byte| byte.is_ascii_alphanumeric() - || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/')), - "{label} contains a forbidden character" - ); - Ok(()) -} - -fn require_contract(capability: &str, schema: &str, compatibility: &str) -> Result<()> { - require_token(capability, "capability")?; - require_token(schema, "capability schema")?; - require_token(compatibility, "capability compatibility") -} - -fn require_value(value: &str, label: &str) -> Result<()> { - ensure!( - !value.trim().is_empty() && value == value.trim(), - "{label} is empty or noncanonical" - ); - ensure!( - value.len() <= 4096 && !value.contains('\0'), - "{label} is oversized or contains NUL" - ); - Ok(()) -} - -fn require_sha1(value: &str, label: &str) -> Result<()> { - require_lower_hex(value, 40, label) -} - -fn require_sha256(value: &str, label: &str) -> Result<()> { - let digest = value - .strip_prefix("sha256-") - .with_context(|| format!("{label} has no sha256- prefix"))?; - require_lower_hex(digest, 64, label) -} - -fn require_lower_hex(value: &str, length: usize, label: &str) -> Result<()> { - ensure!(value.len() == length, "{label} has the wrong length"); - ensure!( - value - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), - "{label} is not lowercase hexadecimal" - ); - Ok(()) -} - -fn sha256_id(bytes: &[u8]) -> String { - let digest = Sha256::digest(bytes); - let mut encoded = String::with_capacity(71); - encoded.push_str("sha256-"); - for byte in digest { - use std::fmt::Write as _; - write!(&mut encoded, "{byte:02x}").expect("writing to String cannot fail"); - } - encoded -} - -#[cfg(test)] -mod tests { - use super::*; - use cultnet_rs::{ - GameCultProviderHealthIdentity, GameCultRuntimeCapability, IdunnRuntimeActivationLaunch, - IdunnServiceIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, - OdinRuntimeTopologyCorrelationPurpose, OdinRuntimeTopologyCorrelationRecord, - OdinTopologyAuthenticationContext, OdinTopologyIdentity, - authenticate_odin_runtime_topology_correlation, enroll_service_identity_at, - verify_runtime_authority, - }; - - const RECIPE: &str = r#" -schema = "gamecult.idunn.target_declaration.v1" -target = "service" -source_stamp_environment = "SERVICE_BUILD_COMMIT" - -[[steps]] -id = "build" -phase = "build" -runner = "rust" -argv = ["cargo", "build", "--locked"] - -[[external_inputs]] -id = "toolchain-index" -url = "https://example.invalid/toolchain-index" -sha256 = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" -runner = "rust" -destination = "inputs/toolchain-index" - -[[artifacts]] -id = "daemon" -source_kind = "runner-output" -runner = "rust" -source = "target/release/service" -destination = "service" -expected_sha256 = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" -executable = true - -[service] -executable_artifact = "daemon" -arguments = [ - { kind = "binding", name = "state_root" }, -] -transport = "http" -route_required = true -required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_RUNTIME_BUNDLE"] - -[service.health] -contract = "service.health" - -[state] -schema_generation = "v1" - -[[state.slots]] -id = "runtime-cache" -relative_path = "runtime-cache.cc" -kind = "cultcache-file" -schema = "service.runtime-cache.v1" -writer = "none" -recovery = "rebuildable" -startup = "open-at-start" - -[[provides]] -capability = "service.runtime" -schema = "service.runtime.v1" -compatibility = "v1" - -[[dependencies]] -kind = "shared-infrastructure" -capability = "odin.verse-rendezvous" -schema = "odin.verse-topology.v1" -compatibility = "v1" -"#; - - const BINDING: &str = r#" -schema = "gamecult.idunn.operator_binding.v2" -target = "service" - -[repository] -origin = "https://github.com/GameCult/Service.git" -admitted_ref = "refs/heads/main" -minimum_revision = "1111111111111111111111111111111111111111" -selection = "ref-head" -checkout = "/srv/build/Service" -recipe_path = "deployment/idunn/recipe.toml" - -[runners.rust] -driver = "docker" -image = "rust@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -user = "1000:1000" -affordances = ["source-read", "artifact-write"] -allowed_programs = ["cargo"] -network_profile = "build-dependency-egress" -memory_mebibytes = 2048 -cpu_quota_percent = 200 -pids_limit = 512 -tmpfs_mebibytes = 512 - -[workload] -driver = "systemd-transient" -state_group = "service" -unit_prefix = "idunn-service" -release_root = "/srv/service/releases" -state_root = "/var/lib/gamecult/service" -runtime_root = "/run/gamecult/idunn/service" -network = "host-private" -hardening = "strict" -memory_mebibytes = 1024 -cpu_quota_percent = 100 - -[workload.argument_bindings] -state_root = "/var/lib/gamecult/service" - -[workload.secret_files] -GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/service/runtime-presence-identity.cc" - -[runtime_identity] -runtime_id = "service-yggdrasil" -expected_signer_identity_id = "service-runtime-signer" -trust_anchor_store = "/etc/gamecult/trust/service.cc" - -[route] -driver = "nginx-stream-tcp" -route_id = "service" -stable_endpoint = "http://127.0.0.1:17999" -private_host = "127.0.0.1" -private_port_start = 18000 -private_port_end = 18009 -config_path = "/etc/nginx/idunn-stream-routes/service.conf" -reload_unit = "nginx.service" - -[brakes] -deployment_store = "/var/lib/gamecult/idunn/service-deployment-brake.cc" -lifecycle_store = "/var/lib/gamecult/idunn/service-lifecycle-brake.cc" - -[rollout] -strategy = "candidate-then-promote" -drain_seconds = 30 -retain_releases = 2 - -[placement] -desired_replicas = 1 -nodes = ["yggdrasil"] -"#; - - fn digest(byte: u8) -> String { - format!("sha256-{}", char::from(byte).to_string().repeat(64)) - } - - fn source(recipe: &str) -> SourceSelectionFacts { - SourceSelectionFacts { - schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), - origin: "https://github.com/GameCult/Service.git".into(), - admitted_ref: "refs/heads/main".into(), - admitted_ref_revision: "2222222222222222222222222222222222222222".into(), - revision: "2222222222222222222222222222222222222222".into(), - source_tree: "3333333333333333333333333333333333333333".into(), - recipe_path: "deployment/idunn/recipe.toml".into(), - recipe_blob_sha256: sha256_id(recipe.as_bytes()), - gitlinks: BTreeMap::new(), - selection: SourceSelection::RefHead, - selected_at_unix_millis: 100, - } - } - - fn odin() -> ExpectedIncarnation { - ExpectedIncarnation { - schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), - target: "odin".into(), - plan_id: digest(b'a'), - incarnation_id: "odin-incarnation-1".into(), - sealed_release_id: digest(b'e'), - source_repository: "github.com/GameCult/Odin".into(), - source_revision: "2222222222222222222222222222222222222222".into(), - recipe_sha256: digest(b'b'), - runtime_id: "odin-yggdrasil".into(), - expected_signer_identity_id: "odin-runtime-signer".into(), - health_contract: "odin.cultnet-service-health".into(), - artifact_sha256: digest(b'f'), - state_schema_generation: None, - state_contract_sha256: None, - write_lease_required: false, - route: Some(IdunnExpectedRoute { - route_id: "odin-private".into(), - transport: "tcp".into(), - stable_endpoint: "tcp://10.77.0.1:17871".into(), - candidate_endpoint: "tcp://127.0.0.1:17871".into(), - }), - capabilities: vec![IdunnExpectedCapability { - capability: "odin.verse-rendezvous".into(), - schema: "odin.verse-topology.v1".into(), - compatibility: "v1".into(), - minimum_capacity: 1, - }], - dependencies: Vec::new(), - } - } - - fn ready_odin_provider(target: &str, runtime_id: &str, capacity: u32) -> SequenceAdmittedReady { - (|| -> Result { - let root = tempfile::tempdir()?; - let provider = enroll_service_identity_at::( - &root.path().join("provider.cc"), - )?; - let idunn = - enroll_service_identity_at::(&root.path().join("idunn.cc"))?; - let odin_signer = - enroll_service_identity_at::(&root.path().join("odin.cc"))?; - let mut expected = odin(); - expected.target = target.into(); - expected.runtime_id = runtime_id.into(); - expected.expected_signer_identity_id = provider.entry().identity_id.clone(); - let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest(b'8'), 100, &idunn)?; - let activation = launch.activation().clone(); - launch.write_credential(std::io::sink())?; - let authority = verify_runtime_authority( - &expected, - &activation, - &idunn.trust_anchor()?, - &provider.entry().public_key, - )?; - let mut topology = OdinRuntimeTopologyCorrelationRecord { - schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(), - target: expected.target.clone(), - expected_projection_sha256: expected.canonical_sha256()?, - expected: true, - current_activation_sha256: Some(activation.canonical_sha256()?), - signed_presence_sha256: Some(digest(b'9')), - observed_presence_state: Some("active".into()), - observed_presence_publisher_sequence: Some(1), - observed_write_lease_sha256: None, - observed_capabilities: vec![GameCultRuntimeCapability { - capability: "odin.verse-rendezvous".into(), - schema: "odin.verse-topology.v1".into(), - compatibility: "v1".into(), - capacity, - }], - runtime_id: expected.runtime_id.clone(), - runtime_instance_id: Some(activation.runtime_instance_id.clone()), - present: true, - ready: true, - dependencies: Vec::new(), - disagreements: Vec::new(), - signer_identity_id: odin_signer.entry().identity_id.clone(), - publisher_sequence: 7, - observed_at_unix_millis: 110, - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - }; - topology.signature = odin_signer - .sign::( - &topology.unsigned_signature_payload()?, - ) - .signature; - let bytes = topology.canonical_bytes()?; - let authenticated = authenticate_odin_runtime_topology_correlation( - &bytes, - &authority, - None, - &odin_signer.entry().public_key, - OdinTopologyAuthenticationContext { - trusted_received_at_unix_millis: 120, - maximum_age_millis: 30, - maximum_future_skew_millis: 5, - }, - )?; - SequenceAdmittedReady::for_test(&expected, authenticated, 120) - })() - .unwrap() - } - - fn plan() -> CompiledDeploymentPlan { - let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; - plan_with(&providers) - } - - fn plan_with(providers: &[SequenceAdmittedReady]) -> CompiledDeploymentPlan { - compile_deployment_plan( - RECIPE.as_bytes(), - BINDING.as_bytes(), - source(RECIPE), - "service-incarnation-1", - Some(18001), - 110, - providers, - ) - .unwrap() - } - - fn artifact_receipt() -> ArtifactReceipt { - ArtifactReceipt { - artifact_id: "daemon".into(), - destination: "service".into(), - sha256: digest(b'c'), - size_bytes: 42, - executable: true, - } - } - - fn external_input_receipt() -> ExternalInputMaterializationReceipt { - ExternalInputMaterializationReceipt { - input_id: "toolchain-index".into(), - url: "https://example.invalid/toolchain-index".into(), - sha256: digest(b'd'), - runner: "rust".into(), - destination: "inputs/toolchain-index".into(), - size_bytes: 17, - } - } - - #[test] - fn compiler_parses_the_exact_bytes_it_receipts() { - let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; - let first = plan_with(&providers); - let second = plan_with(&providers); - assert_eq!(first, second); - assert_eq!(first.recipe_blob.as_slice(), RECIPE.as_bytes()); - assert_eq!(first.binding_blob.as_slice(), BINDING.as_bytes()); - assert_eq!( - sha256_id(&first.recipe_blob), - first.source.recipe_blob_sha256 - ); - let changed = RECIPE.replace( - "contract = \"service.health\"", - "contract = \"service.health.v2\"", - ); - let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; - assert!( - compile_deployment_plan( - changed.as_bytes(), - BINDING.as_bytes(), - source(RECIPE), - "service-incarnation-2", - Some(18002), - 111, - &providers, - ) - .is_err() - ); - let mut corrupted = first; - corrupted - .binding_blob - .extend_from_slice(b"\nunknown = true\n"); - assert!(corrupted.validate().is_err()); - } - - #[test] - fn pinned_policy_requires_the_exact_operator_pin() { - let pinned_binding = BINDING.replace( - "selection = \"ref-head\"", - "selection = \"pinned-object\"\npinned_revision = \"2222222222222222222222222222222222222222\"", - ); - let binding = OperatorBinding::parse(&pinned_binding).unwrap(); - assert!(source(RECIPE).validate_against(&binding).is_err()); - let mut facts = source(RECIPE); - facts.selection = SourceSelection::PinnedObject; - facts.validate_against(&binding).unwrap(); - } - - #[test] - fn gitlink_fact_must_match_the_selected_superproject_tree() { - let recipe = RECIPE.replace( - "source_stamp_environment = \"SERVICE_BUILD_COMMIT\"", - "source_stamp_environment = \"SERVICE_BUILD_COMMIT\"\nrequired_gitlinks = [\"vendor/lib\"]", - ); - let binding_text = BINDING.replace( - "recipe_path = \"deployment/idunn/recipe.toml\"", - "recipe_path = \"deployment/idunn/recipe.toml\"\ngitlinks = { \"vendor/lib\" = { origin = \"https://github.com/GameCult/Lib.git\" } }", - ); - let binding = OperatorBinding::parse(&binding_text).unwrap(); - binding - .admit(&TargetDeclaration::parse(&recipe).unwrap()) - .unwrap(); - let mut facts = source(&recipe); - facts.gitlinks.insert( - "vendor/lib".into(), - GitlinkTreeFact { - origin: "https://github.com/GameCult/Lib.git".into(), - revision: "4444444444444444444444444444444444444444".into(), - }, - ); - facts.validate_against(&binding).unwrap(); - facts - .gitlinks - .get_mut(&PathBuf::from("vendor/lib")) - .unwrap() - .origin = "https://github.com/GameCult/Wrong.git".into(); - assert!(facts.validate_against(&binding).is_err()); - } - - #[test] - fn dependency_selection_uses_observed_ready_capacity_and_stable_identity_order() { - let declaration = TargetDeclaration::parse(RECIPE).unwrap(); - let admitted = [ - ready_odin_provider("odin-z", "odin-runtime-z", 4), - ready_odin_provider("odin-yggdrasil", "odin-runtime-yggdrasil", 2), - ]; - let providers = admitted - .iter() - .flat_map(|provider| managed_ready_provider_refs(provider).unwrap()) - .collect::>(); - let selected = select_dependencies(&declaration, &providers).unwrap(); - assert_eq!( - selected[0].provider.as_ref().unwrap().provider_id, - "odin-yggdrasil" - ); - assert_eq!(selected[0].provider.as_ref().unwrap().capacity, 2); - assert_eq!( - selected[0].provider.as_ref().unwrap().endpoint.as_deref(), - Some("tcp://10.77.0.1:17871") - ); - assert!(matches!( - &selected[0].provider.as_ref().unwrap().authority, - DependencyProviderAuthority::ManagedReady { - odin_topology_publisher_sequence: 7, - .. - } - )); - } - - #[test] - fn external_binding_produces_expected_configuration_only() { - let input = BINDING.replace( - "[placement]", - "[[external_capabilities]]\nprovider_id = \"operator-archive\"\ncapability = \"archive.store\"\nschema = \"archive.store.v1\"\ncompatibility = \"v1\"\ncapacity = 2\nendpoint = \"s3://operator-bound\"\n\n[placement]", - ); - let binding = OperatorBinding::parse(&input).unwrap(); - let providers = external_binding_provider_refs(&binding).unwrap(); - let declaration = TargetDeclaration::parse(&RECIPE.replace( - "kind = \"shared-infrastructure\"\ncapability = \"odin.verse-rendezvous\"\nschema = \"odin.verse-topology.v1\"", - "kind = \"external-operator-binding\"\ncapability = \"archive.store\"\nschema = \"archive.store.v1\"", - )) - .unwrap(); - binding.admit(&declaration).unwrap(); - let selected = select_dependencies(&declaration, &providers).unwrap(); - assert!(matches!( - &providers[0].authority, - DependencyProviderAuthority::ExternalOperatorBinding { .. } - )); - assert_eq!(providers[0].capacity, 2); - assert_eq!( - selected[0].provider.as_ref().unwrap().provider_id, - "operator-archive" - ); - } - - #[test] - fn expected_projection_is_sanitized_and_names_the_selected_graph() { - let plan = plan(); - let release = SealedRelease::new( - &plan, - vec![artifact_receipt()], - vec![external_input_receipt()], - 120, - ) - .unwrap(); - let expected = release.expected_projection(&plan).unwrap(); - assert_eq!(expected.plan_id, plan.plan_id); - assert_eq!(expected.sealed_release_id, release.sealed_release_id); - assert_eq!(expected.incarnation_id, "service-incarnation-1"); - assert_eq!(expected.runtime_id, "service-yggdrasil"); - assert_eq!( - expected.expected_signer_identity_id, - "service-runtime-signer" - ); - assert_eq!(expected.health_contract, "service.health"); - assert_eq!(expected.artifact_sha256, digest(b'c')); - assert_eq!(expected.source_repository, "github.com/GameCult/Service"); - assert_eq!(expected.source_revision, plan.source.revision); - assert_eq!(expected.recipe_sha256, plan.source.recipe_blob_sha256); - assert_eq!(expected.state_schema_generation.as_deref(), Some("v1")); - assert!(expected.state_contract_sha256.is_some()); - assert!(!expected.write_lease_required); - assert_eq!( - expected.dependencies[0].provider_id.as_deref(), - Some("odin") - ); - assert_eq!( - expected.dependencies[0].provider_authority.as_deref(), - Some("managed-incarnation") - ); - assert_eq!( - expected.route.as_ref().unwrap().candidate_endpoint, - "http://127.0.0.1:18001" - ); - assert_eq!(expected.route.as_ref().unwrap().transport, "http"); - assert!(expected.canonical_sha256().unwrap().starts_with("sha256-")); - let mut invalid_release = release; - invalid_release.artifacts[0].size_bytes += 1; - assert!(invalid_release.expected_projection(&plan).is_err()); - } - - #[test] - fn expected_projection_sorts_shared_capability_and_dependency_claims() { - let recipe = RECIPE - .replace( - "[[provides]]\ncapability = \"service.runtime\"", - "[[provides]]\ncapability = \"zeta.runtime\"\nschema = \"zeta.runtime.v1\"\ncompatibility = \"v1\"\n\n[[provides]]\ncapability = \"service.runtime\"", - ) - .replace( - "[[dependencies]]\nkind = \"shared-infrastructure\"", - "[[dependencies]]\nkind = \"optional\"\ncapability = \"zeta.optional\"\nschema = \"zeta.optional.v1\"\ncompatibility = \"v1\"\n\n[[dependencies]]\nkind = \"shared-infrastructure\"", - ); - let plan = compile_deployment_plan( - recipe.as_bytes(), - BINDING.as_bytes(), - source(&recipe), - "service-incarnation-sorted", - Some(18002), - 111, - &[ready_odin_provider("odin", "odin-yggdrasil", 1)], - ) - .unwrap(); - let release = SealedRelease::new( - &plan, - vec![artifact_receipt()], - vec![external_input_receipt()], - 120, - ) - .unwrap(); - let expected = release.expected_projection(&plan).unwrap(); - assert_eq!( - expected - .capabilities - .iter() - .map(|capability| capability.capability.as_str()) - .collect::>(), - ["service.runtime", "zeta.runtime"] - ); - assert_eq!( - expected - .dependencies - .iter() - .map(|dependency| dependency.capability.as_str()) - .collect::>(), - ["odin.verse-rendezvous", "zeta.optional"] - ); - expected.validate().unwrap(); - } - - #[test] - fn sealed_release_address_covers_artifacts_and_external_inputs() { - let receipt = artifact_receipt(); - let input = external_input_receipt(); - let plan = plan(); - let mut wrong_artifact = receipt.clone(); - wrong_artifact.sha256 = digest(b'f'); - assert!(SealedRelease::new(&plan, vec![wrong_artifact], vec![input.clone()], 120).is_err()); - assert!(SealedRelease::new(&plan, vec![receipt.clone()], vec![], 120).is_err()); - let release = - SealedRelease::new(&plan, vec![receipt.clone()], vec![input.clone()], 120).unwrap(); - let later_receipt_time = - SealedRelease::new(&plan, vec![receipt], vec![input], 130).unwrap(); - release.validate_against(&plan).unwrap(); - assert_eq!( - release.sealed_release_id, - later_receipt_time.sealed_release_id - ); - let mut changed = release.clone(); - changed.artifacts[0].size_bytes = 43; - assert!(changed.validate_against(&plan).is_err()); - let mut changed = release; - changed.external_inputs[0].size_bytes = 18; - assert!(changed.validate_against(&plan).is_err()); - } -} diff --git a/crates/idunn-daemon/src/drivers.rs b/crates/idunn-daemon/src/drivers.rs deleted file mode 100644 index 1554408..0000000 --- a/crates/idunn-daemon/src/drivers.rs +++ /dev/null @@ -1,7337 +0,0 @@ -use std::collections::BTreeMap; -use std::ffi::{OsStr, OsString}; -use std::fs::{self, OpenOptions}; -use std::io::{ErrorKind, Read, Write}; -use std::net::{SocketAddr, TcpStream}; -use std::path::{Path, PathBuf}; -use std::process::{Command, Output, Stdio}; -use std::thread; -use std::time::Duration; - -use anyhow::{Context, Result, bail, ensure}; -use cultcache_rs::{ - CacheBackingStore, CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, - SingleFileMessagePackBackingStore, TryCompareExchangeSnapshotOutcome, -}; -use cultmesh_rs::{CultMeshRudpSnapshotOptions, request_raw_snapshot_from_rudp_catalog}; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; -use uuid::Uuid; - -use crate::control_plane::SequenceAdmittedWarming; -use crate::deployment::{ - ArtifactOutput, ArtifactSource, IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, - IDUNN_RUNTIME_BUNDLE_ENVIRONMENT, IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, LaunchArgument, - OperatorBinding, RUNTIME_PRESENCE_IDENTITY_BINDING, RUNTIME_PRESENCE_IDENTITY_FD_NAME, - RouteBinding, RouteDriver, RunnerBinding, SourceSelectionPolicy, TargetDeclaration, - WorkloadNetwork, -}; -use crate::deployment_plan::{ - ArtifactReceipt, CompiledDeploymentPlan, ExternalInputMaterializationReceipt, GitlinkTreeFact, - SOURCE_SELECTION_FACTS_SCHEMA, SealedRelease, SourceSelection, SourceSelectionFacts, -}; -use cultnet_rs::{ - CultNetMessage, CultNetRawPayloadEncoding, CultNetWireContract, - GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultProviderHealthIdentity, - GameCultServiceTrustAnchorRecord, IDUNN_EXPECTED_INCARNATION_SCHEMA, - IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, - IDUNN_RUNTIME_ACTIVATION_SCHEMA, IdunnExpectedIncarnationRecord, IdunnProcessWriteLeaseRecord, - IdunnRuntimeActivationLaunch, IdunnRuntimeActivationRecord, IdunnRuntimeActivationSigner, - ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA, OdinRuntimeTopologyCorrelationRecord, - ServiceIdentityProfile, ServiceIdentityTrustAnchor, decode_cultnet_message_from_slice, - derive_service_identity_id, encode_cultnet_message_to_vec, encode_frame, - open_service_identity_credential_reader, -}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct ProcessIdentity { - pub uid: u32, - pub gid: u32, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct GitTreeEntry { - mode: String, - kind: String, - object: String, - path: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ResolvedSource { - pub facts: SourceSelectionFacts, - pub recipe_bytes: Vec, -} - -impl ResolvedSource { - pub fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { - self.facts.validate_against(binding)?; - ensure!(!self.recipe_bytes.is_empty(), "resolved recipe is empty"); - ensure!( - sha256_id(&self.recipe_bytes) == self.facts.recipe_blob_sha256, - "resolved recipe bytes differ from the selected Git object" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct FrozenSource { - receipt: FrozenSourceReceipt, - facts: SourceSelectionFacts, - recipe_bytes: Vec, - root: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct FrozenSourceReceipt { - pub transaction_id: String, - pub plan_id: String, - pub snapshot_sha256: String, -} - -impl FrozenSourceReceipt { - pub fn validate_against(&self, plan: &CompiledDeploymentPlan) -> Result<()> { - plan.validate()?; - require_driver_id(&self.transaction_id, "source transaction")?; - ensure!(self.plan_id == plan.plan_id, "frozen source plan differs"); - require_sha256_id(&self.snapshot_sha256, "frozen source snapshot") - } - - fn snapshot_component(&self) -> &str { - self.snapshot_sha256 - .strip_prefix("sha256-") - .expect("validated frozen source digest") - } -} - -pub trait SourcePort { - fn resolve( - &self, - binding: &OperatorBinding, - resolution_id: &str, - selected_at_unix_millis: u64, - ) -> Result; - - fn freeze( - &self, - transaction_id: &str, - plan: &CompiledDeploymentPlan, - ) -> Result; - - fn observe_frozen( - &self, - plan: &CompiledDeploymentPlan, - receipt: &FrozenSourceReceipt, - ) -> Result; - - fn cleanup(&self, transaction_id: &str, receipt: Option<&FrozenSourceReceipt>) -> Result<()>; -} - -pub trait RunnerPort { - fn materialize( - &self, - source: &FrozenSource, - plan: &CompiledDeploymentPlan, - staging_root: &Path, - sealed_at_unix_millis: u64, - ) -> Result; -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct MaterializedRelease { - pub release: SealedRelease, - pub root: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct InstalledReleaseObservation { - pub sealed_release_id: String, - pub root: PathBuf, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ServiceCredentialObservation { - pub environment_name: String, - pub delivered_path: PathBuf, - pub device: u64, - pub inode: u64, - pub uid: u32, - pub gid: u32, - pub mode: u32, - pub size: u64, - pub sha256: String, -} - -/// Root-owned source metadata for one descriptor that PID1 opens and passes -/// only to the service's initial process. The process must consume and close -/// descriptors 3 and 4 before spawning any child; no filesystem path is -/// projected into the workload environment. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ParentOnlyFileDescriptorObservation { - pub fd_number: u32, - pub fd_name: String, - pub source_path: PathBuf, - pub access: String, - pub device: u64, - pub inode: u64, - pub uid: u32, - pub gid: u32, - pub mode: u32, - pub links: u64, - pub size: u64, - pub sha256: String, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct WorkloadObservation { - pub unit: String, - pub unit_description: String, - pub invocation_id: String, - pub exec_main_start_timestamp_monotonic: u64, - pub service_type: String, - pub restart_policy: String, - pub kill_mode: String, - pub dynamic_user: bool, - pub systemd_user: String, - pub systemd_group: String, - pub supplementary_groups: String, - pub capability_bounding_set: String, - pub ambient_capabilities: String, - pub private_mounts: bool, - pub private_pids: bool, - pub protect_proc: String, - pub proc_subset: String, - pub no_new_privileges: bool, - pub umask: String, - pub inaccessible_paths: String, - pub load_credential: String, - pub main_pid: u32, - pub process_start_time: u64, - pub process_uids: [u32; 4], - pub process_gids: [u32; 4], - pub process_groups: Vec, - pub process_cap_inheritable: u64, - pub process_cap_permitted: u64, - pub process_cap_effective: u64, - pub process_cap_bounding: u64, - pub process_cap_ambient: u64, - pub process_no_new_privileges: bool, - pub process_namespace_pids: Vec, - pub mount_namespace_id: u64, - pub pid_namespace_id: u64, - pub executable: PathBuf, - pub executable_device: u64, - pub executable_inode: u64, - pub executable_sha256: String, - pub runtime_instance_id: String, - pub working_directory: PathBuf, - pub runtime_bundle: PathBuf, - pub command_line_sha256: String, - pub environment_names: Vec, - pub environment_contract_sha256: String, - pub control_group: String, - pub credentials_directory: Option, - pub parent_only_file_descriptors: Vec, - pub activation_signer_identity_id: String, - pub activation_signer_public_key: Vec, - pub service_credentials: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct LinuxProcessSecurityObservation { - uids: [u32; 4], - gids: [u32; 4], - groups: Vec, - cap_inheritable: u64, - cap_permitted: u64, - cap_effective: u64, - cap_bounding: u64, - cap_ambient: u64, - no_new_privileges: bool, - namespace_pids: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct SystemdUnitObservation { - properties: BTreeMap, - open_files: Vec, -} - -pub trait WorkloadPort { - fn install( - &self, - plan: &CompiledDeploymentPlan, - release: &MaterializedRelease, - ) -> Result; - - fn prepare_activation( - &self, - plan: &CompiledDeploymentPlan, - expected: &IdunnExpectedIncarnationRecord, - launch: IdunnRuntimeActivationLaunch, - ) -> Result; - - fn start_prepared( - &self, - plan: &CompiledDeploymentPlan, - release: &SealedRelease, - installed: &InstalledReleaseObservation, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result; - - fn discard_prepared( - &self, - plan: &CompiledDeploymentPlan, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result<()>; - - fn observe( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - prior: &WorkloadObservation, - ) -> Result; - - fn stop(&self, observation: &WorkloadObservation) -> Result<()>; -} - -pub trait TopologyPort { - fn publish_expected( - &self, - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, - ) -> Result; - fn withdraw_expected( - &self, - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, - activation: Option<&IdunnRuntimeActivationRecord>, - lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result<()>; - fn publish_observed_activation( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - observation: &WorkloadObservation, - ) -> Result; - fn publish_process_write_lease( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result; - fn withdraw_process_write_lease( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result<()>; - fn receive(&self, target: &str) -> Result>; -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ReceivedOdinTopologyCorrelation { - pub target: String, - pub canonical_bytes: Vec, -} - -pub trait WriteLeasePort { - fn revoke_exact(&self, incumbent: Option<&IdunnProcessWriteLeaseRecord>) -> Result<()>; - fn observe_empty(&self) -> Result; - fn observe_exact(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result; - fn grant( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result; - fn observe( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result; -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct RouteObservation { - pub route_id: String, - pub runtime_instance_id: String, - pub membership_sha256: String, - pub signed_presence_sha256: String, - pub observed_at_unix_millis: u64, -} - -impl RouteObservation { - pub fn validate(&self) -> Result<()> { - require_driver_id(&self.route_id, "route observation")?; - require_sha256_id(&self.runtime_instance_id, "route runtime instance")?; - require_sha256_id(&self.membership_sha256, "route membership")?; - require_sha256_id(&self.signed_presence_sha256, "route signed presence")?; - ensure!( - self.observed_at_unix_millis > 0, - "route observation has no trusted receipt time" - ); - Ok(()) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct RoutePreflightReceipt { - pub route_id: String, - pub candidate_runtime_instance_id: String, - pub candidate_membership_sha256: String, - pub incumbent_runtime_instance_id: Option, - pub incumbent_membership_sha256: Option, - pub incumbent_configuration: Option>, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct RouteSnapshotResponse { - pub message_id: String, - pub canonical_presence: Vec, -} - -const ROUTE_SNAPSHOT_TIMEOUT: Duration = Duration::from_secs(3); -const ROUTE_SNAPSHOT_MAX_BYTES: usize = 1024 * 1024; -const ROUTE_HTTP_MAX_HEADER_BYTES: usize = 32 * 1024; -const ROUTE_HTTP_SNAPSHOT_PATH: &str = "/cultnet/snapshot"; - -impl RoutePreflightReceipt { - pub fn validate(&self) -> Result<()> { - require_driver_id(&self.route_id, "route preflight")?; - require_sha256_id( - &self.candidate_runtime_instance_id, - "candidate route runtime instance", - )?; - require_sha256_id( - &self.candidate_membership_sha256, - "candidate route membership", - )?; - match ( - &self.incumbent_runtime_instance_id, - &self.incumbent_membership_sha256, - &self.incumbent_configuration, - ) { - (None, None, None) => {} - (Some(runtime), Some(membership), Some(configuration)) => { - require_sha256_id(runtime, "incumbent route runtime instance")?; - require_sha256_id(membership, "incumbent route membership")?; - ensure!( - !configuration.is_empty(), - "incumbent route configuration is empty" - ); - ensure!( - sha256_id(configuration) == *membership, - "incumbent route configuration differs from its membership digest" - ); - } - _ => bail!("incumbent route preflight evidence is partial"), - } - Ok(()) - } -} - -/// Fixed-argv Git source driver. It never interprets recipe text as a command -/// and never derives source policy from the target repository. The configured -/// identity performs every Git/network read; root Idunn extracts only exact Git -/// object archives into a separate transaction-owned immutable store. -pub struct GitSourceDriver { - pub source_cache_root: PathBuf, - pub frozen_source_root: PathBuf, - pub identity: Option, - pub git_program: PathBuf, - pub tar_program: PathBuf, -} - -impl GitSourceDriver { - pub fn new( - source_cache_root: impl Into, - frozen_source_root: impl Into, - identity: Option, - ) -> Self { - Self { - source_cache_root: source_cache_root.into(), - frozen_source_root: frozen_source_root.into(), - identity, - git_program: PathBuf::from("/usr/bin/git"), - tar_program: PathBuf::from("/usr/bin/tar"), - } - } - - fn git_command(&self, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - ensure!( - self.git_program.is_absolute(), - "Git source driver program is not absolute" - ); - let mut command = Command::new(&self.git_program); - command - .args(args) - .stdin(Stdio::null()) - .env_clear() - .env("HOME", self.source_cache_root.join(".home")) - .env("PATH", "/usr/bin:/bin") - .env("GIT_CONFIG_NOSYSTEM", "1") - .env("GIT_TERMINAL_PROMPT", "0") - .env("LANG", "C.UTF-8"); - apply_identity(&mut command, self.identity)?; - Ok(command) - } - - fn git(&self, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - let output = self.git_command(args)?.output().with_context(|| { - format!( - "starting fixed-argv Git driver {}", - self.git_program.display() - ) - })?; - if !output.status.success() { - bail!( - "Git source driver exited with {}: {}", - output.status, - String::from_utf8_lossy(&output.stderr).trim() - ); - } - Ok(output) - } - - fn git_text(&self, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - let output = self.git(args)?; - let value = String::from_utf8(output.stdout).context("Git emitted non-UTF-8 text")?; - Ok(value.trim().to_owned()) - } - - fn ensure_checkout(&self, binding: &OperatorBinding) -> Result<()> { - let checkout = &binding.repository.checkout; - if !checkout.exists() { - self.git([ - OsString::from("clone"), - OsString::from("--filter=blob:none"), - OsString::from("--no-checkout"), - OsString::from("--origin"), - OsString::from("origin"), - binding.repository.origin.clone().into(), - checkout.as_os_str().to_owned(), - ])?; - } - let checkout_metadata = fs::symlink_metadata(checkout) - .with_context(|| format!("inspecting source checkout {}", checkout.display()))?; - ensure!( - checkout_metadata.is_dir() && !checkout_metadata.file_type().is_symlink(), - "source checkout is not a native directory" - ); - let canonical_checkout = checkout.canonicalize()?; - ensure!( - canonical_checkout == *checkout, - "source checkout traverses a symlink or noncanonical path" - ); - let git_metadata = fs::symlink_metadata(checkout.join(".git"))?; - ensure!( - git_metadata.is_dir() && !git_metadata.file_type().is_symlink(), - "source checkout has no native Git object directory" - ); - let actual_origin = self.git_text([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("remote"), - OsString::from("get-url"), - OsString::from("origin"), - ])?; - ensure!( - actual_origin == binding.repository.origin, - "source checkout origin differs from the operator binding" - ); - Ok(()) - } - - fn admitted_ref_name(binding: &OperatorBinding, resolution_id: &str) -> Result { - require_driver_id(resolution_id, "source resolution")?; - Ok(format!( - "refs/idunn/resolutions/{}/{}", - binding.target, resolution_id - )) - } - - fn resolve_selected_revision( - &self, - binding: &OperatorBinding, - admitted_ref_revision: &str, - ) -> Result<(String, SourceSelection)> { - let checkout = &binding.repository.checkout; - let (revision, selection) = match binding.repository.selection { - SourceSelectionPolicy::PinnedObject => ( - binding - .repository - .pinned_revision - .clone() - .context("pinned source binding lost its exact revision")?, - SourceSelection::PinnedObject, - ), - SourceSelectionPolicy::RefHead => { - (admitted_ref_revision.to_owned(), SourceSelection::RefHead) - } - SourceSelectionPolicy::SignedRelease => { - bail!("signed-release selection requires a release-authority source port") - } - }; - require_git_sha(&revision, "selected source revision")?; - self.git([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("merge-base"), - OsString::from("--is-ancestor"), - binding.repository.minimum_revision.clone().into(), - revision.clone().into(), - ]) - .context("selected source is below the operator minimum revision")?; - self.git([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("merge-base"), - OsString::from("--is-ancestor"), - revision.clone().into(), - admitted_ref_revision.into(), - ]) - .context("selected source is outside the fetched admitted ref")?; - Ok((revision, selection)) - } - - fn git_tree_entries(&self, repository: &Path, revision: &str) -> Result> { - let output = self - .git([ - OsString::from("-C"), - repository.as_os_str().to_owned(), - OsString::from("ls-tree"), - OsString::from("-r"), - OsString::from("-z"), - revision.into(), - ])? - .stdout; - let mut entries = Vec::new(); - for record in output - .split(|byte| *byte == 0) - .filter(|record| !record.is_empty()) - { - let tab = record - .iter() - .position(|byte| *byte == b'\t') - .context("Git tree record has no path delimiter")?; - let header = std::str::from_utf8(&record[..tab]) - .context("Git tree record header is not UTF-8")?; - let path = - std::str::from_utf8(&record[tab + 1..]).context("Git tree path is not UTF-8")?; - let mut fields = header.split(' '); - let mode = fields.next().context("Git tree record has no mode")?; - let kind = fields.next().context("Git tree record has no kind")?; - let object = fields.next().context("Git tree record has no object")?; - ensure!(fields.next().is_none(), "Git tree record has extra fields"); - require_git_sha(object, "Git tree object")?; - let path = PathBuf::from(path); - let normalized = normalized_relative(&path)?; - ensure!( - normalized == path.to_string_lossy(), - "Git tree path is not normalized" - ); - ensure!( - path.components().all(|component| !matches!( - component, - std::path::Component::Normal(value) if value == ".git" - )), - "Git tree contains forbidden .git metadata" - ); - entries.push(GitTreeEntry { - mode: mode.to_owned(), - kind: kind.to_owned(), - object: object.to_owned(), - path, - }); - } - entries.sort_by(|left, right| left.path.cmp(&right.path)); - ensure!( - entries.windows(2).all(|pair| pair[0].path != pair[1].path), - "Git tree emits a path twice" - ); - Ok(entries) - } - - fn exact_recipe_and_gitlinks( - &self, - binding: &OperatorBinding, - revision: &str, - ) -> Result<(Vec, BTreeMap)> { - let entries = self.git_tree_entries(&binding.repository.checkout, revision)?; - let recipe = entries - .iter() - .find(|entry| entry.path == binding.repository.recipe_path) - .context("selected tree has no deployment recipe")?; - ensure!( - matches!(recipe.mode.as_str(), "100644" | "100755") && recipe.kind == "blob", - "deployment recipe is not a regular Git blob" - ); - let recipe_bytes = self - .git([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("cat-file"), - OsString::from("blob"), - recipe.object.clone().into(), - ])? - .stdout; - ensure!( - !recipe_bytes.is_empty(), - "selected deployment recipe is empty" - ); - - let observed_gitlinks = entries - .iter() - .filter(|entry| entry.mode == "160000") - .map(|entry| { - ensure!(entry.kind == "commit", "Gitlink has the wrong object kind"); - Ok((entry.path.clone(), entry.object.clone())) - }) - .collect::>>()?; - let expected_paths = binding - .repository - .gitlinks - .keys() - .cloned() - .collect::>(); - let observed_paths = observed_gitlinks - .keys() - .cloned() - .collect::>(); - ensure!( - observed_paths == expected_paths, - "selected tree Gitlinks do not exactly match operator bindings" - ); - let gitlinks = observed_gitlinks - .into_iter() - .map(|(path, revision)| { - let origin = binding.repository.gitlinks[&path].origin.clone(); - (path, GitlinkTreeFact { origin, revision }) - }) - .collect(); - Ok((recipe_bytes, gitlinks)) - } - - fn prepare_source_root(&self, binding: &OperatorBinding) -> Result<()> { - #[cfg(unix)] - ensure!( - unsafe { libc::geteuid() } != 0 || self.identity.is_some(), - "root Idunn must configure an unprivileged source identity" - ); - ensure!( - binding - .repository - .checkout - .starts_with(&self.source_cache_root) - && binding.repository.checkout != self.source_cache_root, - "repository checkout is outside Idunn's source authority root" - ); - ensure_source_directory(&self.source_cache_root, self.identity)?; - ensure!( - self.source_cache_root.canonicalize()? == self.source_cache_root, - "source cache root traverses a symlink or noncanonical path" - ); - ensure_source_directory(&self.source_cache_root.join(".home"), self.identity)?; - ensure_source_directory_tree( - &self.source_cache_root, - binding - .repository - .checkout - .parent() - .context("repository checkout has no parent directory")?, - self.identity, - )?; - self.ensure_checkout(binding) - } - - fn verify_exact_source( - &self, - binding: &OperatorBinding, - resolved: &ResolvedSource, - ) -> Result<()> { - let actual_tree = self.git_text([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from(format!("{}^{{tree}}", resolved.facts.revision)), - ])?; - ensure!( - actual_tree == resolved.facts.source_tree, - "selected revision no longer resolves to the frozen source tree" - ); - let (recipe_bytes, gitlinks) = - self.exact_recipe_and_gitlinks(binding, &resolved.facts.revision)?; - ensure!( - recipe_bytes == resolved.recipe_bytes, - "selected recipe object differs from the durable source resolution" - ); - ensure!( - gitlinks == resolved.facts.gitlinks, - "Gitlinks differ from the durable source resolution" - ); - Ok(()) - } - - fn git_archive_into( - &self, - repository: &Path, - revision: &str, - prefix: Option<&Path>, - destination: &Path, - ) -> Result<()> { - ensure!( - self.tar_program.is_absolute(), - "source archive extractor is not absolute" - ); - let mut archive_args = vec![ - OsString::from("-C"), - repository.as_os_str().to_owned(), - OsString::from("archive"), - OsString::from("--format=tar"), - ]; - if let Some(prefix) = prefix { - archive_args.push(OsString::from(format!( - "--prefix={}/", - normalized_relative(prefix)? - ))); - } - archive_args.push(revision.into()); - let mut archive = self.git_command(archive_args)?; - archive.stdout(Stdio::piped()).stderr(Stdio::piped()); - let mut archive = archive.spawn().context("starting exact Git archive")?; - let archive_stdout = archive.stdout.take().context("Git archive has no stdout")?; - let mut extractor = Command::new(&self.tar_program); - extractor - .args([ - OsString::from("--extract"), - OsString::from("--file=-"), - OsString::from("--directory"), - destination.as_os_str().to_owned(), - OsString::from("--no-same-owner"), - ]) - .stdin(Stdio::from(archive_stdout)) - .stdout(Stdio::null()) - .stderr(Stdio::piped()) - .env_clear() - .env("LANG", "C.UTF-8"); - let extractor = extractor - .spawn() - .context("starting fixed-argv source archive extractor")?; - let archive_output = archive - .wait_with_output() - .context("waiting for exact Git archive")?; - let extractor_output = extractor - .wait_with_output() - .context("waiting for source archive extractor")?; - ensure!( - archive_output.status.success(), - "Git archive failed: {}", - String::from_utf8_lossy(&archive_output.stderr).trim() - ); - ensure!( - extractor_output.status.success(), - "source archive extraction failed: {}", - String::from_utf8_lossy(&extractor_output.stderr).trim() - ); - Ok(()) - } - - fn materialize_gitlink_archive( - &self, - binding: &OperatorBinding, - path: &Path, - fact: &GitlinkTreeFact, - destination: &Path, - ) -> Result<()> { - let gitlink_root = self - .source_cache_root - .join(".gitlinks") - .join(&binding.target); - ensure_source_directory_tree(&self.source_cache_root, &gitlink_root, self.identity)?; - let checkout = gitlink_root.join(format!("{}-{}", fact.revision, Uuid::new_v4())); - let result = (|| { - self.git([ - OsString::from("clone"), - OsString::from("--filter=blob:none"), - OsString::from("--no-checkout"), - OsString::from("--origin"), - OsString::from("origin"), - fact.origin.clone().into(), - checkout.as_os_str().to_owned(), - ])?; - self.git([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("fetch"), - OsString::from("--no-tags"), - OsString::from("origin"), - fact.revision.clone().into(), - ])?; - let actual = self.git_text([ - OsString::from("-C"), - checkout.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from(format!("{}^{{commit}}", fact.revision)), - ])?; - ensure!(actual == fact.revision, "Gitlink exact revision is absent"); - ensure!( - !self - .git_tree_entries(&checkout, &fact.revision)? - .iter() - .any(|entry| entry.mode == "160000"), - "nested Gitlinks are not admitted in Idunn v1" - ); - self.git_archive_into(&checkout, &fact.revision, Some(path), destination) - })(); - let cleanup = if checkout.exists() { - remove_tree_inside(&gitlink_root, &checkout) - } else { - Ok(()) - }; - match (result, cleanup) { - (Ok(()), Ok(())) => Ok(()), - (Err(error), _) => Err(error), - (Ok(()), Err(error)) => Err(error.context("cleaning exact Gitlink checkout")), - } - } -} - -impl SourcePort for GitSourceDriver { - fn resolve( - &self, - binding: &OperatorBinding, - resolution_id: &str, - selected_at_unix_millis: u64, - ) -> Result { - binding.validate()?; - ensure!( - selected_at_unix_millis > 0, - "source selection has no timestamp" - ); - self.prepare_source_root(binding)?; - let fetched_ref = Self::admitted_ref_name(binding, resolution_id)?; - self.git([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("fetch"), - OsString::from("--force"), - OsString::from("--no-tags"), - OsString::from("origin"), - OsString::from(format!( - "+{}:{fetched_ref}", - binding.repository.admitted_ref - )), - ])?; - let admitted_ref_revision = self.git_text([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from(format!("{fetched_ref}^{{commit}}")), - ])?; - require_git_sha(&admitted_ref_revision, "fetched admitted-ref revision")?; - let (revision, selection) = - self.resolve_selected_revision(binding, &admitted_ref_revision)?; - let source_tree = self.git_text([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("rev-parse"), - OsString::from(format!("{revision}^{{tree}}")), - ])?; - require_git_sha(&source_tree, "selected source tree")?; - let (recipe_bytes, gitlinks) = self.exact_recipe_and_gitlinks(binding, &revision)?; - let facts = SourceSelectionFacts { - schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), - origin: binding.repository.origin.clone(), - admitted_ref: binding.repository.admitted_ref.clone(), - admitted_ref_revision, - revision, - source_tree, - recipe_path: binding.repository.recipe_path.clone(), - recipe_blob_sha256: sha256_id(&recipe_bytes), - gitlinks, - selection, - selected_at_unix_millis, - }; - facts.validate_against(binding)?; - let resolved = ResolvedSource { - facts, - recipe_bytes, - }; - resolved.validate_against(binding)?; - Ok(resolved) - } - - fn freeze( - &self, - transaction_id: &str, - plan: &CompiledDeploymentPlan, - ) -> Result { - plan.validate()?; - require_driver_id(transaction_id, "source transaction")?; - let (_, binding) = plan.parsed_inputs()?; - let resolved = ResolvedSource { - facts: plan.source.clone(), - recipe_bytes: plan.recipe_blob.clone(), - }; - resolved.validate_against(&binding)?; - #[cfg(unix)] - ensure!( - unsafe { libc::geteuid() } == 0 && self.identity.is_some(), - "freezing source requires root Idunn with an unprivileged Git identity" - ); - self.prepare_source_root(&binding)?; - self.git([ - OsString::from("-C"), - binding.repository.checkout.as_os_str().to_owned(), - OsString::from("fetch"), - OsString::from("--no-tags"), - OsString::from("origin"), - resolved.facts.revision.clone().into(), - ]) - .context("fetching the durable exact source revision")?; - self.verify_exact_source(&binding, &resolved)?; - let transaction_root = - prepare_frozen_transaction_root(&self.frozen_source_root, transaction_id)?; - let partial = transaction_root.join(".partial"); - prepare_frozen_source_destination(&partial)?; - let materialization = (|| { - self.git_archive_into( - &binding.repository.checkout, - &resolved.facts.revision, - None, - &partial, - )?; - for (path, fact) in &resolved.facts.gitlinks { - self.materialize_gitlink_archive(&binding, path, fact, &partial)?; - } - let recipe_path = partial.join(&resolved.facts.recipe_path); - let recipe_metadata = fs::symlink_metadata(&recipe_path)?; - ensure!( - recipe_metadata.is_file() && !recipe_metadata.file_type().is_symlink(), - "frozen deployment recipe is not a regular file" - ); - let materialized_recipe = - fs::read(&recipe_path).context("reading frozen deployment recipe")?; - ensure!( - materialized_recipe == resolved.recipe_bytes, - "frozen recipe differs from the durable source resolution" - ); - harden_frozen_source(&partial)?; - validate_frozen_source(&partial)?; - frozen_source_sha256(&partial) - })(); - let snapshot_sha256 = match materialization { - Ok(snapshot_sha256) => snapshot_sha256, - Err(error) => { - let _ = remove_frozen_transaction_root(&self.frozen_source_root, transaction_id); - return Err(error); - } - }; - let snapshot_component = snapshot_sha256 - .strip_prefix("sha256-") - .expect("generated frozen source digest"); - let final_root = transaction_root.join(snapshot_component); - fs::rename(&partial, &final_root).context("publishing immutable frozen source")?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o500))?; - } - let receipt = FrozenSourceReceipt { - transaction_id: transaction_id.to_owned(), - plan_id: plan.plan_id.clone(), - snapshot_sha256, - }; - receipt.validate_against(plan)?; - Ok(receipt) - } - - fn observe_frozen( - &self, - plan: &CompiledDeploymentPlan, - receipt: &FrozenSourceReceipt, - ) -> Result { - receipt.validate_against(plan)?; - validate_frozen_source_store(&self.frozen_source_root)?; - let root = self - .frozen_source_root - .join(&receipt.transaction_id) - .join(receipt.snapshot_component()); - let canonical_store = self.frozen_source_root.canonicalize()?; - let canonical_root = root.canonicalize()?; - ensure!( - canonical_store == self.frozen_source_root - && canonical_root == root - && canonical_root.starts_with(&canonical_store), - "frozen source receipt resolves outside its authority root" - ); - validate_frozen_source(&root)?; - ensure!( - frozen_source_sha256(&root)? == receipt.snapshot_sha256, - "frozen source snapshot differs from its receipt" - ); - let recipe_path = root.join(&plan.source.recipe_path); - let recipe_metadata = fs::symlink_metadata(&recipe_path)?; - ensure!( - recipe_metadata.is_file() && !recipe_metadata.file_type().is_symlink(), - "observed deployment recipe is not a regular file" - ); - ensure!( - fs::read(&recipe_path)? == plan.recipe_blob, - "observed deployment recipe differs from the persisted plan" - ); - Ok(FrozenSource { - receipt: receipt.clone(), - facts: plan.source.clone(), - recipe_bytes: plan.recipe_blob.clone(), - root, - }) - } - - fn cleanup(&self, transaction_id: &str, receipt: Option<&FrozenSourceReceipt>) -> Result<()> { - require_driver_id(transaction_id, "source transaction")?; - if let Some(receipt) = receipt { - ensure!( - receipt.transaction_id == transaction_id, - "frozen source cleanup receipt belongs to another transaction" - ); - require_sha256_id(&receipt.snapshot_sha256, "frozen source snapshot")?; - } - let transaction_root = self.frozen_source_root.join(transaction_id); - if !transaction_root.exists() { - return Ok(()); - } - remove_frozen_transaction_root(&self.frozen_source_root, transaction_id) - } -} - -/// Docker is only a runner substrate. Recipe argv stays an argv vector, the -/// operator binding selects the exact image/network/mount affordances, and the -/// driver returns complete materialization receipts rather than build truth by -/// convention. -pub struct DockerRunnerDriver { - pub docker_program: PathBuf, -} - -impl Default for DockerRunnerDriver { - fn default() -> Self { - Self { - docker_program: PathBuf::from("/usr/bin/docker"), - } - } -} - -impl DockerRunnerDriver { - fn docker(&self, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - ensure!( - self.docker_program.is_absolute(), - "Docker runner program is not absolute" - ); - let output = Command::new(&self.docker_program) - .args(args) - .stdin(Stdio::null()) - .output() - .with_context(|| format!("starting Docker runner {}", self.docker_program.display()))?; - if !output.status.success() { - bail!( - "Docker runner exited with {}: {}", - output.status, - String::from_utf8_lossy(&output.stderr).trim() - ); - } - Ok(output) - } - - fn run_in_workspace( - &self, - runner: &RunnerBinding, - workspace: &Path, - working_directory: &Path, - argv: &[String], - required_environment: &std::collections::BTreeSet, - source_stamp: (&str, &str), - ) -> Result<()> { - ensure!(!argv.is_empty(), "runner command is empty"); - ensure!( - runner.allowed_programs.contains(&argv[0]), - "runner program {} is not operator-bound", - argv[0] - ); - let mut args = self.base_run_args(runner, workspace)?; - args.extend([ - OsString::from("--workdir"), - OsString::from(format!( - "/workspace/{}", - normalized_relative(working_directory)? - )), - OsString::from("--env"), - OsString::from(format!("{}={}", source_stamp.0, source_stamp.1)), - ]); - for name in required_environment { - if let Some(value) = runner.environment.get(name) { - args.push(OsString::from("--env")); - args.push(OsString::from(format!("{name}={value}"))); - } else if let Some(path) = runner.secret_files.get(name) { - validate_runner_secret(path, container_identity(&runner.user)?)?; - args.push(OsString::from("--env")); - args.push(OsString::from(format!("{name}=/run/idunn/secrets/{name}"))); - args.push(OsString::from("--mount")); - args.push(bind_mount( - path, - &format!("/run/idunn/secrets/{name}"), - true, - )?); - } else { - bail!("runner lacks declared step environment {name}") - } - } - args.push(runner.image.clone().into()); - args.extend(argv.iter().map(OsString::from)); - self.docker(args)?; - Ok(()) - } - - fn base_run_args(&self, runner: &RunnerBinding, workspace: &Path) -> Result> { - let identity = container_identity(&runner.user)?; - let mut args = vec![ - OsString::from("run"), - OsString::from("--rm"), - OsString::from("--network"), - OsString::from(runner.network_profile.as_deref().unwrap_or("none")), - OsString::from("--memory"), - OsString::from(format!("{}m", runner.memory_mebibytes)), - OsString::from("--cpus"), - OsString::from(format!( - "{:.2}", - f64::from(runner.cpu_quota_percent) / 100.0 - )), - OsString::from("--mount"), - bind_mount(workspace, "/workspace", false)?, - OsString::from("--user"), - OsString::from(&runner.user), - OsString::from("--cap-drop"), - OsString::from("ALL"), - OsString::from("--security-opt"), - OsString::from("no-new-privileges"), - OsString::from("--read-only"), - OsString::from("--pids-limit"), - OsString::from(runner.pids_limit.to_string()), - OsString::from("--tmpfs"), - OsString::from(format!( - "/tmp:rw,nosuid,nodev,noexec,size={}m", - runner.tmpfs_mebibytes - )), - ]; - if let Some(cache_root) = &runner.cache_root { - ensure_runner_cache_root(cache_root, identity)?; - args.push(OsString::from("--mount")); - args.push(bind_mount(cache_root, "/cache", false)?); - } - Ok(args) - } - - fn materialize_external_input( - &self, - declaration: &TargetDeclaration, - binding: &OperatorBinding, - workspaces: &BTreeMap, - input_id: &str, - ) -> Result { - let input = declaration - .external_inputs - .iter() - .find(|candidate| candidate.id == input_id) - .context("external input declaration disappeared")?; - let runner = &binding.runners[&input.runner]; - let workspace = &workspaces[&input.runner]; - let destination = workspace.join(&input.destination); - ensure!( - destination.starts_with(workspace), - "external input escaped its runner workspace" - ); - if let Some(parent) = destination.parent() { - fs::create_dir_all(parent)?; - } - let mut args = self.base_run_args(runner, workspace)?; - args.extend([ - runner.image.clone().into(), - OsString::from("curl"), - OsString::from("--fail"), - OsString::from("--location"), - OsString::from("--proto"), - OsString::from("=https"), - OsString::from("--output"), - OsString::from(format!( - "/workspace/{}", - normalized_relative(&input.destination)? - )), - input.url.clone().into(), - ]); - self.docker(args)?; - let bytes = fs::read(&destination).with_context(|| { - format!( - "reading materialized external input {}", - destination.display() - ) - })?; - ensure!(!bytes.is_empty(), "materialized external input is empty"); - let digest = raw_sha256(&bytes); - ensure!( - digest == input.sha256, - "materialized external input digest differs from its recipe pin" - ); - Ok(ExternalInputMaterializationReceipt { - input_id: input.id.clone(), - url: input.url.clone(), - sha256: format!("sha256-{digest}"), - runner: input.runner.clone(), - destination: input.destination.clone(), - size_bytes: bytes.len().try_into()?, - }) - } - - fn collect_artifact( - &self, - declaration: &TargetDeclaration, - source: &FrozenSource, - workspaces: &BTreeMap, - staging_root: &Path, - artifact_id: &str, - ) -> Result { - let artifact = declaration - .artifacts - .iter() - .find(|candidate| candidate.id == artifact_id) - .context("artifact declaration disappeared")?; - let source_path = match artifact.source_kind { - ArtifactSource::RunnerOutput => workspaces - .get( - artifact - .runner - .as_deref() - .context("runner artifact lost its runner")?, - ) - .context("runner workspace is absent")? - .join(&artifact.source), - ArtifactSource::WorktreeTree => source.root.join(&artifact.source), - }; - ensure!(source_path.exists(), "declared artifact output is absent"); - let destination = staging_root.join(&artifact.destination); - ensure!( - destination.starts_with(staging_root), - "artifact destination escaped its release staging root" - ); - copy_artifact(&source_path, &destination)?; - let (sha256, size_bytes) = digest_artifact(&destination)?; - if let Some(expected) = &artifact.expected_sha256 { - ensure!( - &sha256 == expected, - "artifact output differs from its recipe-pinned digest" - ); - } - Ok(ArtifactReceipt { - artifact_id: artifact.id.clone(), - destination: artifact.destination.clone(), - sha256: format!("sha256-{sha256}"), - size_bytes, - executable: artifact.executable, - }) - } -} - -impl RunnerPort for DockerRunnerDriver { - fn materialize( - &self, - source: &FrozenSource, - plan: &CompiledDeploymentPlan, - staging_parent: &Path, - sealed_at_unix_millis: u64, - ) -> Result { - plan.validate()?; - source.receipt.validate_against(plan)?; - ensure!( - source.facts == plan.source && source.recipe_bytes == plan.recipe_blob, - "runner source differs from the compiled plan" - ); - let (declaration, binding) = plan.parsed_inputs()?; - fs::create_dir_all(staging_parent)?; - let staging_root = staging_parent.join(&source.receipt.transaction_id); - if staging_root.exists() { - remove_tree_inside(staging_parent, &staging_root)?; - } - fs::create_dir_all(&staging_root)?; - - let mut workspaces = BTreeMap::new(); - for runner_id in binding.runners.keys() { - let workspace = staging_root.join(format!(".runner-{runner_id}")); - copy_tree(&source.root, &workspace)?; - workspaces.insert(runner_id.clone(), workspace); - } - - for input in &declaration.external_inputs { - let workspace = &workspaces[&input.runner]; - let destination = workspace.join(&input.destination); - ensure!( - destination.starts_with(workspace), - "external input escaped its runner workspace" - ); - if let Some(parent) = destination.parent() { - fs::create_dir_all(parent)?; - } - } - for (runner_id, workspace) in &workspaces { - assign_runner_tree( - workspace, - container_identity(&binding.runners[runner_id].user)?, - )?; - } - - let mut external_inputs = Vec::new(); - for input in &declaration.external_inputs { - external_inputs.push(self.materialize_external_input( - &declaration, - &binding, - &workspaces, - &input.id, - )?); - } - for step in &declaration.steps { - let runner = &binding.runners[&step.runner]; - let workspace = &workspaces[&step.runner]; - for required in &step.required_environment { - ensure!( - runner.environment.contains_key(required) - || runner.secret_files.contains_key(required), - "step {} lacks operator-bound environment {required}", - step.id - ); - } - self.run_in_workspace( - runner, - workspace, - &step.working_directory, - &step.argv, - &step.required_environment, - ( - &declaration.source_stamp_environment, - &source.facts.revision, - ), - )?; - } - - let mut artifacts = Vec::new(); - for artifact in &declaration.artifacts { - artifacts.push(self.collect_artifact( - &declaration, - source, - &workspaces, - &staging_root, - &artifact.id, - )?); - } - for workspace in workspaces.values() { - remove_tree_inside(&staging_root, workspace)?; - } - let release = SealedRelease::new( - plan, - artifacts.clone(), - external_inputs.clone(), - sealed_at_unix_millis, - )?; - Ok(MaterializedRelease { - release, - root: staging_root, - }) - } -} - -/// systemd owns process execution. This driver lowers one validated launch -/// contract into a transient unit and then proves the native process and -/// executable that systemd actually started. It does not decide admission, -/// readiness, write authority, or route membership. -pub struct SystemdTransientWorkloadDriver { - pub systemd_run_program: PathBuf, - pub systemctl_program: PathBuf, - pub proc_root: PathBuf, - pub credential_root: PathBuf, -} - -impl Default for SystemdTransientWorkloadDriver { - fn default() -> Self { - Self { - systemd_run_program: PathBuf::from("/usr/bin/systemd-run"), - systemctl_program: PathBuf::from("/usr/bin/systemctl"), - proc_root: PathBuf::from("/proc"), - credential_root: PathBuf::from("/run/idunn/activation-credentials"), - } - } -} - -impl SystemdTransientWorkloadDriver { - fn command(&self, program: &Path, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - ensure!( - program.is_absolute(), - "workload actuator program is not absolute" - ); - let output = Command::new(program) - .args(args) - .stdin(Stdio::null()) - .env_clear() - .env("LANG", "C.UTF-8") - .output() - .with_context(|| format!("starting workload actuator {}", program.display()))?; - if !output.status.success() { - bail!( - "workload actuator {} exited with {}: {}", - program.display(), - output.status, - String::from_utf8_lossy(&output.stderr).trim() - ); - } - Ok(output) - } - - fn unit_name(&self, prefix: &str, runtime_instance_id: &str) -> Result { - let suffix = runtime_instance_id - .strip_prefix("sha256-") - .context("runtime instance id has no sha256 prefix")?; - ensure!( - suffix.len() == 64 && suffix.bytes().all(|byte| byte.is_ascii_hexdigit()), - "runtime instance id is not a SHA-256 digest" - ); - Ok(format!("{prefix}-{suffix}.service")) - } - - fn activation_credential_source( - &self, - activation: &IdunnRuntimeActivationRecord, - ) -> Result { - activation.validate()?; - ensure!( - self.credential_root.is_absolute() - && !self - .credential_root - .as_os_str() - .to_string_lossy() - .chars() - .any(char::is_whitespace), - "activation credential root must be an absolute path without whitespace" - ); - Ok(self - .credential_root - .join(format!("{}.credential", activation.runtime_instance_id))) - } - - fn write_activation_credential( - &self, - launch: IdunnRuntimeActivationLaunch, - ) -> Result<(IdunnRuntimeActivationRecord, PathBuf)> { - #[cfg(unix)] - use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; - - ensure_activation_credential_root(&self.credential_root)?; - let expected_activation = launch.activation().clone(); - let source = self.activation_credential_source(&expected_activation)?; - let mut options = OpenOptions::new(); - options.write(true).create_new(true); - #[cfg(unix)] - options.mode(0o400); - let mut file = options.open(&source).with_context(|| { - format!( - "creating one-shot activation credential {}", - source.display() - ) - })?; - let written = (|| -> Result { - #[cfg(unix)] - file.set_permissions(fs::Permissions::from_mode(0o400))?; - let activation = launch - .write_credential(&mut file) - .context("writing one-shot activation credential")?; - file.flush()?; - file.sync_all()?; - Ok(activation) - })(); - drop(file); - let activation = match written { - Ok(activation) => activation, - Err(error) => { - return match remove_activation_credential_source(&source) { - Ok(()) => Err(error), - Err(cleanup) => Err(error.context(format!( - "deleting incomplete activation credential also failed: {cleanup:#}" - ))), - }; - } - }; - let validation = (|| -> Result<()> { - ensure!( - activation == expected_activation, - "activation launch changed while writing its credential" - ); - validate_activation_credential_source(&source)?; - sync_parent_directory(&source) - })(); - if let Err(error) = validation { - return match remove_activation_credential_source(&source) { - Ok(()) => Err(error), - Err(cleanup) => Err(error.context(format!( - "deleting invalid activation credential also failed: {cleanup:#}" - ))), - }; - } - Ok((activation, source)) - } - - fn validate_prepared_activation_credential( - &self, - activation: &IdunnRuntimeActivationRecord, - source: &Path, - ) -> Result<()> { - validate_activation_credential_source(source)?; - let signer = IdunnRuntimeActivationSigner::from_credential_reader( - open_native_read_only(source).with_context(|| { - format!( - "opening prepared activation credential {}", - source.display() - ) - })?, - )?; - ensure!( - signer.identity_id() == activation.activation_signer_identity_id - && signer.public_key() == activation.activation_signer_public_key, - "prepared activation credential differs from the persisted activation" - ); - Ok(()) - } - - fn parent_only_file_descriptors( - &self, - binding: &OperatorBinding, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result> { - let activation_source = self.activation_credential_source(activation)?; - self.validate_prepared_activation_credential(activation, &activation_source)?; - let presence_source = binding - .workload - .secret_files - .get(RUNTIME_PRESENCE_IDENTITY_BINDING) - .context("workload has no parent-only runtime presence identity source")?; - let presence_signer = - open_service_identity_credential_reader::( - open_native_read_only(presence_source).with_context(|| { - format!( - "opening runtime presence identity source {}", - presence_source.display() - ) - })?, - )?; - ensure!( - presence_signer.entry().identity_id == expected.expected_signer_identity_id, - "runtime presence identity source differs from Expected" - ); - let descriptors = vec![ - observe_parent_only_file_descriptor( - 3, - IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, - &activation_source, - )?, - observe_parent_only_file_descriptor( - 4, - RUNTIME_PRESENCE_IDENTITY_FD_NAME, - presence_source, - )?, - ]; - parent_only_open_file_properties(&descriptors)?; - Ok(descriptors) - } - - fn stop_submitted_unit(&self, unit: &str, expected_description: &str) -> Result<()> { - let Some(observation) = self.show_unit(unit)? else { - return Ok(()); - }; - let values = &observation.properties; - ensure!( - values.get("Description").map(String::as_str) == Some(expected_description), - "refusing to stop a submitted unit whose description changed" - ); - self.command( - &self.systemctl_program, - [OsString::from("stop"), OsString::from(unit)], - )?; - if let Some(observation) = self.show_unit(unit)? { - let values = &observation.properties; - ensure!( - values - .get("ActiveState") - .is_some_and(|state| { matches!(state.as_str(), "inactive" | "failed") }), - "submitted systemd unit remained active after stop" - ); - } - Ok(()) - } - - fn install_release( - &self, - plan: &CompiledDeploymentPlan, - release: &MaterializedRelease, - ) -> Result { - release.release.validate_against(plan)?; - let (_, binding) = plan.parsed_inputs()?; - let release_root = &binding.workload.release_root; - fs::create_dir_all(release_root) - .with_context(|| format!("creating release root {}", release_root.display()))?; - let installed = release_root.join(&release.release.sealed_release_id); - if !installed.exists() { - let temporary = release_root.join(format!( - ".install-{}-{}", - release.release.sealed_release_id, - Uuid::new_v4() - )); - copy_tree(&release.root, &temporary)?; - if let Err(error) = fs::rename(&temporary, &installed) { - let cleanup = remove_tree_inside(release_root, &temporary); - if installed.exists() && error.kind() == ErrorKind::AlreadyExists { - cleanup?; - } else { - cleanup?; - return Err(error).with_context(|| { - format!("installing sealed release {}", installed.display()) - }); - } - } - } - for artifact in &release.release.artifacts { - let path = installed.join(&artifact.destination); - let (sha256, size_bytes) = digest_artifact(&path)?; - ensure!( - format!("sha256-{sha256}") == artifact.sha256 && size_bytes == artifact.size_bytes, - "installed artifact {} differs from its sealed receipt", - artifact.artifact_id - ); - } - harden_installed_release(&installed, &release.release.artifacts)?; - Ok(InstalledReleaseObservation { - sealed_release_id: release.release.sealed_release_id.clone(), - root: installed, - }) - } - - fn validate_installed_release( - &self, - plan: &CompiledDeploymentPlan, - release: &SealedRelease, - installed: &InstalledReleaseObservation, - ) -> Result<()> { - release.validate_against(plan)?; - let (_, binding) = plan.parsed_inputs()?; - ensure!( - installed.sealed_release_id == release.sealed_release_id - && installed.root - == binding - .workload - .release_root - .join(&release.sealed_release_id), - "installed release observation belongs to another sealed release" - ); - for artifact in &release.artifacts { - let path = installed.root.join(&artifact.destination); - let (sha256, size_bytes) = digest_artifact(&path)?; - ensure!( - format!("sha256-{sha256}") == artifact.sha256 && size_bytes == artifact.size_bytes, - "installed artifact {} differs from its sealed receipt", - artifact.artifact_id - ); - } - harden_installed_release(&installed.root, &release.artifacts) - } - - fn prepare_runtime_bundle( - &self, - binding: &OperatorBinding, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result { - let bundle = binding - .workload - .runtime_root - .join(&activation.runtime_instance_id); - fs::create_dir_all(&bundle) - .with_context(|| format!("creating runtime bundle {}", bundle.display()))?; - write_immutable_record( - &bundle.join("expected.cc"), - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: expected.canonical_bytes()?, - stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), - }, - )?; - write_immutable_record( - &bundle.join("activation.cc"), - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: activation.canonical_bytes()?, - stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - )?; - harden_runtime_bundle(&bundle)?; - Ok(bundle) - } - - fn show_unit(&self, unit: &str) -> Result> { - ensure!( - self.systemctl_program.is_absolute(), - "systemctl program is not absolute" - ); - let output = Command::new(&self.systemctl_program) - .args([ - OsString::from("show"), - OsString::from(unit), - OsString::from("--no-pager"), - OsString::from("--property=LoadState"), - OsString::from("--property=ActiveState"), - OsString::from("--property=SubState"), - OsString::from("--property=Description"), - OsString::from("--property=InvocationID"), - OsString::from("--property=MainPID"), - OsString::from("--property=ExecMainStartTimestampMonotonic"), - OsString::from("--property=Type"), - OsString::from("--property=Restart"), - OsString::from("--property=KillMode"), - OsString::from("--property=DynamicUser"), - OsString::from("--property=User"), - OsString::from("--property=Group"), - OsString::from("--property=SupplementaryGroups"), - OsString::from("--property=CapabilityBoundingSet"), - OsString::from("--property=AmbientCapabilities"), - OsString::from("--property=PrivateMounts"), - OsString::from("--property=PrivatePIDs"), - OsString::from("--property=ProtectProc"), - OsString::from("--property=ProcSubset"), - OsString::from("--property=NoNewPrivileges"), - OsString::from("--property=UMask"), - OsString::from("--property=InaccessiblePaths"), - OsString::from("--property=LoadCredential"), - OsString::from("--property=OpenFile"), - OsString::from("--property=WorkingDirectory"), - OsString::from("--property=ControlGroup"), - ]) - .stdin(Stdio::null()) - .env_clear() - .env("LANG", "C.UTF-8") - .output() - .with_context(|| format!("observing systemd unit {unit}"))?; - let text = - std::str::from_utf8(&output.stdout).context("systemd show output is not UTF-8")?; - let mut values = BTreeMap::new(); - let mut open_files = Vec::new(); - for line in text.lines().filter(|line| !line.is_empty()) { - let (name, value) = line - .split_once('=') - .context("systemd show output is malformed")?; - if name == "OpenFile" { - open_files.push(value.to_owned()); - continue; - } - ensure!( - values.insert(name.to_owned(), value.to_owned()).is_none(), - "systemd show property is duplicated" - ); - } - if values - .get("LoadState") - .is_some_and(|value| value == "not-found") - { - return Ok(None); - } - ensure!( - output.status.success(), - "systemd show failed for {unit}: {}", - String::from_utf8_lossy(&output.stderr).trim() - ); - Ok(Some(SystemdUnitObservation { - properties: values, - open_files, - })) - } - - fn observe_unit( - &self, - unit: &str, - expected_executable: &Path, - runtime_instance_id: &str, - activation_signer_identity_id: &str, - activation_signer_public_key: &[u8], - environment_names: &[String], - service_credential_names: &[String], - parent_only_file_descriptors: &[ParentOnlyFileDescriptorObservation], - ) -> Result { - ensure!( - service_credential_names - .windows(2) - .all(|pair| pair[0] < pair[1]), - "service credential names are not unique and sorted" - ); - let unit_observation = self - .show_unit(unit)? - .with_context(|| format!("systemd unit {unit} is absent"))?; - let values = &unit_observation.properties; - ensure!( - unit_observation.open_files - == parent_only_open_file_properties(parent_only_file_descriptors)?, - "systemd parent-only descriptor contract differs from the Idunn launch" - ); - ensure!( - values - .get("ActiveState") - .is_some_and(|value| value == "active") - && values - .get("SubState") - .is_some_and(|value| value == "running"), - "systemd unit {unit} is not running" - ); - let invocation_id = values - .get("InvocationID") - .filter(|value| value.len() == 32 && value.bytes().all(|byte| byte.is_ascii_hexdigit())) - .context("systemd unit has no canonical invocation id")? - .clone(); - let exec_main_start_timestamp_monotonic = - required_systemd_property(&values, "ExecMainStartTimestampMonotonic")? - .parse::() - .context("systemd unit has no numeric main-process start timestamp")?; - ensure!( - exec_main_start_timestamp_monotonic > 0, - "systemd unit has no main-process start timestamp" - ); - let unit_description = required_systemd_property(&values, "Description")?.to_owned(); - let service_type = required_systemd_property(&values, "Type")?.to_owned(); - let restart_policy = required_systemd_property(&values, "Restart")?.to_owned(); - let kill_mode = required_systemd_property(&values, "KillMode")?.to_owned(); - let dynamic_user = parse_systemd_boolean(&values, "DynamicUser")?; - let systemd_user = systemd_property(&values, "User")?.to_owned(); - let systemd_group = systemd_property(&values, "Group")?.to_owned(); - let supplementary_groups = systemd_property(&values, "SupplementaryGroups")?.to_owned(); - let capability_bounding_set = - systemd_property(&values, "CapabilityBoundingSet")?.to_owned(); - let ambient_capabilities = systemd_property(&values, "AmbientCapabilities")?.to_owned(); - let private_mounts = parse_systemd_boolean(&values, "PrivateMounts")?; - let private_pids = parse_systemd_boolean(&values, "PrivatePIDs")?; - let protect_proc = required_systemd_property(&values, "ProtectProc")?.to_owned(); - let proc_subset = required_systemd_property(&values, "ProcSubset")?.to_owned(); - let no_new_privileges = parse_systemd_boolean(&values, "NoNewPrivileges")?; - let umask = required_systemd_property(&values, "UMask")?.to_owned(); - let inaccessible_paths = - required_systemd_property(&values, "InaccessiblePaths")?.to_owned(); - let load_credential = required_systemd_property(&values, "LoadCredential")?.to_owned(); - ensure!( - service_type == "exec" - && restart_policy == "no" - && kill_mode == "mixed" - && dynamic_user - && systemd_user.is_empty() - && supplementary_groups.is_empty() - && capability_bounding_set.is_empty() - && ambient_capabilities.is_empty() - && private_mounts - && private_pids - && protect_proc == "invisible" - && proc_subset == "all" - && no_new_privileges - && umask == "0007" - && inaccessible_paths == self.credential_root.display().to_string() - && load_credential - == if service_credential_names.is_empty() { - "" - } else { - "[unprintable]" - }, - "systemd workload isolation properties differ from the Idunn contract" - ); - let working_directory = - PathBuf::from(required_systemd_property(&values, "WorkingDirectory")?); - ensure!( - working_directory.is_absolute(), - "systemd unit working directory is not absolute" - ); - let control_group = required_systemd_property(&values, "ControlGroup")?.to_owned(); - ensure!( - control_group.starts_with('/') && !control_group.contains(['\n', '\r', '\0']), - "systemd unit control group is invalid" - ); - let main_pid: u32 = values - .get("MainPID") - .context("systemd unit has no MainPID")? - .parse() - .context("systemd MainPID is not a u32")?; - ensure!(main_pid > 0, "systemd unit has no live main process"); - let process_root = self.proc_root.join(main_pid.to_string()); - let process_executable = process_root.join("exe"); - let executable = fs::read_link(&process_executable) - .with_context(|| format!("observing executable for process {main_pid}"))?; - let mut executable_file = open_proc_magic_link(&process_executable)?; - let executable_metadata = executable_file.metadata()?; - let expected_executable_metadata = fs::metadata(expected_executable)?; - #[cfg(unix)] - let (executable_device, executable_inode) = { - use std::os::unix::fs::MetadataExt; - ensure!( - executable_metadata.is_file() - && executable_metadata.dev() == expected_executable_metadata.dev() - && executable_metadata.ino() == expected_executable_metadata.ino(), - "systemd started an executable inode outside the sealed release" - ); - (executable_metadata.dev(), executable_metadata.ino()) - }; - #[cfg(not(unix))] - let (executable_device, executable_inode) = (0, 0); - ensure!( - fs::canonicalize(&executable)? == fs::canonicalize(expected_executable)?, - "systemd started an executable outside the sealed release" - ); - let executable_sha256 = sha256_reader(&mut executable_file)?; - let process_start_time = linux_process_start_time(&process_root.join("stat"))?; - let process_security = linux_process_security(&process_root.join("status"), main_pid)?; - ensure!( - process_security - .uids - .iter() - .all(|uid| *uid == process_security.uids[0]) - && (61_184..=65_519).contains(&process_security.uids[0]), - "systemd workload does not have one unprivileged process uid" - ); - ensure!( - process_security - .gids - .iter() - .all(|gid| *gid == process_security.gids[0]) - && process_security.gids[0] > 0 - && process_security - .groups - .iter() - .all(|gid| *gid == process_security.gids[0]), - "systemd workload has foreign supplementary groups" - ); - ensure!( - [ - process_security.cap_inheritable, - process_security.cap_permitted, - process_security.cap_effective, - process_security.cap_bounding, - process_security.cap_ambient, - ] - .iter() - .all(|capabilities| *capabilities == 0), - "systemd workload retained Linux capabilities" - ); - ensure!( - process_security.no_new_privileges, - "systemd workload lacks kernel no-new-privileges enforcement" - ); - ensure!( - process_security.namespace_pids.len() >= 2 - && process_security.namespace_pids[0] == main_pid - && process_security.namespace_pids.last() == Some(&1) - && process_security.namespace_pids.iter().all(|pid| *pid > 0), - "systemd workload is not observed in a private pid namespace" - ); - let mount_namespace_id = linux_namespace_id(&process_root.join("ns/mnt"), "mnt")?; - let pid_namespace_id = linux_namespace_id(&process_root.join("ns/pid"), "pid")?; - let observer_mount_namespace_id = - linux_namespace_id(&self.proc_root.join("self/ns/mnt"), "mnt")?; - let observer_pid_namespace_id = - linux_namespace_id(&self.proc_root.join("self/ns/pid"), "pid")?; - ensure!( - mount_namespace_id != observer_mount_namespace_id - && pid_namespace_id != observer_pid_namespace_id, - "systemd workload did not receive private mount and pid namespaces" - ); - let process_control_groups = fs::read_to_string(process_root.join("cgroup"))?; - let process_control_groups = process_control_groups.lines().collect::>(); - ensure!( - process_control_groups.len() == 1 - && process_control_groups[0] == format!("0::{control_group}"), - "systemd MainPID is outside the unit control group" - ); - let command_line = fs::read(process_root.join("cmdline"))?; - ensure!( - !command_line.is_empty(), - "systemd MainPID has no command line" - ); - let process_environment = read_process_environment(&process_root.join("environ"))?; - let selected_environment = - select_process_environment(&process_environment, environment_names)?; - let runtime_bundle = PathBuf::from( - selected_environment - .get(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) - .context("systemd MainPID lacks the Idunn runtime bundle environment")?, - ); - ensure!( - runtime_bundle.is_absolute(), - "runtime bundle path is not absolute" - ); - let credentials_directory = if service_credential_names.is_empty() { - ensure!( - !process_environment - .iter() - .any(|entry| entry.starts_with(b"CREDENTIALS_DIRECTORY=")), - "systemd MainPID exposed an unowned credential directory" - ); - None - } else { - let path = PathBuf::from(required_process_environment_value( - &process_environment, - "CREDENTIALS_DIRECTORY", - )?); - ensure!( - path == Path::new("/run/credentials").join(unit), - "systemd MainPID exposed an unexpected credential directory" - ); - Some(path) - }; - let activation_descriptor = &parent_only_file_descriptors[0]; - ensure!( - activation_descriptor.fd_name == IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME - && activation_descriptor.size == 32 - && activation_descriptor.uid == 0 - && activation_descriptor.gid == 0 - && activation_descriptor.mode == 0o400 - && activation_descriptor.links == 1, - "activation signing descriptor is not exact root-only source material" - ); - let mut service_credentials = Vec::new(); - for name in service_credential_names { - let delivered_path = credentials_directory - .as_ref() - .context("service credential has no systemd credential directory")? - .join(name); - let delivered_value = delivered_path.display().to_string(); - ensure!( - selected_environment.get(name) == Some(&delivered_value), - "workload secret environment does not name its delivered systemd credential" - ); - let observer_path = - path_inside_process_root(&process_root.join("root"), &delivered_path)?; - let mut file = open_native_read_only(&observer_path).with_context(|| { - format!( - "opening delivered service credential {}", - delivered_path.display() - ) - })?; - let metadata = file.metadata()?; - #[cfg(unix)] - let (device, inode, uid, gid, mode, links) = { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - ( - metadata.dev(), - metadata.ino(), - metadata.uid(), - metadata.gid(), - metadata.permissions().mode() & 0o777, - metadata.nlink(), - ) - }; - #[cfg(not(unix))] - let (device, inode, uid, gid, mode, links) = (0, 0, 0, 0, 0, 0); - ensure!( - metadata.is_file() - && metadata.len() > 0 - && links == 1 - && uid == process_security.uids[0] - && gid == process_security.gids[0] - && mode == 0o400, - "delivered service credential is not readable only by the workload identity" - ); - service_credentials.push(ServiceCredentialObservation { - environment_name: name.clone(), - delivered_path, - device, - inode, - uid, - gid, - mode, - size: metadata.len(), - sha256: sha256_reader(&mut file)?, - }); - } - let final_values = self - .show_unit(unit)? - .with_context(|| format!("systemd unit {unit} vanished during observation"))?; - ensure!( - final_values == unit_observation - && linux_process_start_time(&process_root.join("stat"))? == process_start_time, - "systemd workload identity changed during native observation" - ); - Ok(WorkloadObservation { - unit: unit.to_owned(), - unit_description, - invocation_id, - exec_main_start_timestamp_monotonic, - service_type, - restart_policy, - kill_mode, - dynamic_user, - systemd_user, - systemd_group, - supplementary_groups, - capability_bounding_set, - ambient_capabilities, - private_mounts, - private_pids, - protect_proc, - proc_subset, - no_new_privileges, - umask, - inaccessible_paths, - load_credential, - main_pid, - process_start_time, - process_uids: process_security.uids, - process_gids: process_security.gids, - process_groups: process_security.groups, - process_cap_inheritable: process_security.cap_inheritable, - process_cap_permitted: process_security.cap_permitted, - process_cap_effective: process_security.cap_effective, - process_cap_bounding: process_security.cap_bounding, - process_cap_ambient: process_security.cap_ambient, - process_no_new_privileges: process_security.no_new_privileges, - process_namespace_pids: process_security.namespace_pids, - mount_namespace_id, - pid_namespace_id, - executable, - executable_device, - executable_inode, - executable_sha256, - runtime_instance_id: runtime_instance_id.to_owned(), - working_directory, - runtime_bundle, - command_line_sha256: sha256_id(&command_line), - environment_names: environment_names.to_vec(), - environment_contract_sha256: sha256_id(&rmp_serde::to_vec(&selected_environment)?), - control_group, - credentials_directory, - parent_only_file_descriptors: parent_only_file_descriptors.to_vec(), - activation_signer_identity_id: activation_signer_identity_id.to_owned(), - activation_signer_public_key: activation_signer_public_key.to_vec(), - service_credentials, - }) - } - - fn launch_command( - &self, - declaration: &TargetDeclaration, - binding: &OperatorBinding, - installed: &Path, - ) -> Result> { - let executable_artifact = - release_artifact(declaration, &declaration.service.executable_artifact)?; - let executable = installed.join(&executable_artifact.destination); - ensure!(executable.is_file(), "sealed service executable is absent"); - let mut command = vec![executable.into_os_string()]; - for argument in &declaration.service.arguments { - command.push(match argument { - LaunchArgument::Literal { value } => value.into(), - LaunchArgument::Binding { name } => { - binding.workload.argument_bindings[name].clone().into() - } - }); - } - Ok(command) - } - - fn launch_environment( - &self, - binding: &OperatorBinding, - bundle: &Path, - expected: &IdunnExpectedIncarnationRecord, - unit: &str, - ) -> Result> { - let mut environment = binding.workload.environment.clone(); - let credentials_directory = Path::new("/run/credentials").join(unit); - for name in binding - .workload - .secret_files - .keys() - .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) - { - ensure!( - environment - .insert( - name.clone(), - credentials_directory.join(name).display().to_string(), - ) - .is_none(), - "workload environment and secret bindings collide" - ); - } - ensure!( - environment - .insert( - IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into(), - bundle.display().to_string(), - ) - .is_none(), - "operator binding attempts to replace the Idunn runtime bundle" - ); - match &expected.route { - Some(route) => { - let (host, port) = endpoint_host_port( - &route.candidate_endpoint, - &format!("{}://", route.transport), - )?; - ensure!( - environment - .insert( - IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into(), - format!("{host}:{port}"), - ) - .is_none(), - "operator binding attempts to replace the Idunn candidate bind" - ); - } - None => ensure!( - !environment.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT), - "unrouted workload carries an Idunn candidate bind" - ), - } - match &binding.process_write_lease { - Some(write_lease) => ensure!( - environment - .insert( - IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into(), - write_lease.record_path.display().to_string(), - ) - .is_none(), - "operator binding attempts to replace the Idunn process write lease" - ), - None => ensure!( - !environment.contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT), - "stateless workload carries an Idunn process write lease" - ), - } - Ok(environment) - } - - fn validate_launch_observation( - &self, - observation: &WorkloadObservation, - declaration: &TargetDeclaration, - binding: &OperatorBinding, - installed: &Path, - bundle: &Path, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result<()> { - let command = self.launch_command(declaration, binding, installed)?; - let environment = self.launch_environment(binding, bundle, expected, &observation.unit)?; - let environment_names = environment.keys().cloned().collect::>(); - let expected_description = format!( - "Idunn {} {}", - binding.target, - bundle - .file_name() - .context("runtime bundle has no instance id")? - .to_string_lossy() - ); - ensure!( - observation.unit.ends_with(".service"), - "workload unit has no service suffix" - ); - let expected_group = binding.workload.state_group.as_deref(); - let expected_group_id = expected_group.map(resolve_group_id).transpose()?; - let regular_credential_names = binding - .workload - .secret_files - .keys() - .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) - .cloned() - .collect::>(); - let expected_credentials_directory = (!regular_credential_names.is_empty()) - .then(|| Path::new("/run/credentials").join(&observation.unit)); - let descriptor_properties = - parent_only_open_file_properties(&observation.parent_only_file_descriptors)?; - let activation_source = self.activation_credential_source(activation)?; - let presence_source = binding - .workload - .secret_files - .get(RUNTIME_PRESENCE_IDENTITY_BINDING) - .context("workload has no parent-only runtime presence identity source")?; - let observed_credential_names = observation - .service_credentials - .iter() - .map(|credential| credential.environment_name.clone()) - .collect::>(); - ensure!( - observation.unit_description == expected_description - && observation.service_type == "exec" - && observation.restart_policy == "no" - && observation.kill_mode == "mixed" - && observation.dynamic_user - && observation.systemd_user.is_empty() - && match expected_group { - Some(group) => observation.systemd_group == group, - None => observation.systemd_group.is_empty(), - } - && observation.supplementary_groups.is_empty() - && observation.capability_bounding_set.is_empty() - && observation.ambient_capabilities.is_empty() - && observation.private_mounts - && observation.private_pids - && observation.protect_proc == "invisible" - && observation.proc_subset == "all" - && observation.no_new_privileges - && observation.umask == "0007" - && observation.inaccessible_paths == self.credential_root.display().to_string() - && observation.load_credential - == if regular_credential_names.is_empty() { - "" - } else { - "[unprintable]" - } - && observation.working_directory == installed - && observation.runtime_bundle == bundle - && observation.credentials_directory == expected_credentials_directory - && observation.command_line_sha256 == sha256_id(&proc_command_line(&command)) - && observation.environment_names == environment_names - && observation.environment_contract_sha256 - == sha256_id(&rmp_serde::to_vec(&environment)?) - && descriptor_properties.len() == 2 - && observation.parent_only_file_descriptors[0].source_path == activation_source - && observation.parent_only_file_descriptors[0].size == 32 - && observation.parent_only_file_descriptors[1].source_path == *presence_source - && observation.activation_signer_identity_id - == activation.activation_signer_identity_id - && observation.activation_signer_public_key - == activation.activation_signer_public_key - && observed_credential_names == regular_credential_names - && match expected_group_id { - Some(group_id) => observation.process_gids[0] == group_id, - None => observation.process_gids[0] == observation.process_uids[0], - }, - "running workload launch contract differs from the admitted launch" - ); - ensure!( - Path::new(&observation.control_group).file_name() - == Some(OsStr::new(&observation.unit)), - "running workload control group belongs to another unit" - ); - Ok(()) - } - - fn validate_writable_bindings(&self, binding: &OperatorBinding) -> Result<()> { - let Some(state_group) = binding.workload.state_group.as_deref() else { - ensure!( - binding.workload.state_root.is_none() - && binding.workload.read_write_paths.is_empty(), - "dynamic workload writable paths have no fixed state group" - ); - return Ok(()); - }; - let state_group_id = resolve_group_id(state_group)?; - if let Some(state_root) = &binding.workload.state_root { - validate_workload_writable_path(state_root, state_group_id, true)?; - } - for path in &binding.workload.read_write_paths { - validate_workload_writable_path(path, state_group_id, false)?; - } - Ok(()) - } - - fn start_transient( - &self, - declaration: &TargetDeclaration, - binding: &OperatorBinding, - installed: &Path, - bundle: &Path, - expected: &IdunnExpectedIncarnationRecord, - parent_only_file_descriptors: &[ParentOnlyFileDescriptorObservation], - unit: &str, - ) -> Result<()> { - ensure!( - declaration - .service - .required_environment - .contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), - "target does not declare the standard Idunn runtime bundle" - ); - ensure!( - !binding - .workload - .environment - .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT) - && !binding - .workload - .secret_files - .contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT), - "operator binding attempts to replace the Idunn runtime bundle" - ); - validate_service_credential_sources(&binding.workload.secret_files)?; - let parent_only_open_files = - parent_only_open_file_properties(parent_only_file_descriptors)?; - let environment = self.launch_environment(binding, bundle, expected, unit)?; - let executable_artifact = - release_artifact(declaration, &declaration.service.executable_artifact)?; - let executable = installed.join(&executable_artifact.destination); - ensure!(executable.is_file(), "sealed service executable is absent"); - let unit_description = format!( - "Idunn {} {}", - binding.target, - bundle - .file_name() - .context("runtime bundle has no instance id")? - .to_string_lossy() - ); - let mut args = vec![ - OsString::from("--no-block"), - OsString::from("--no-ask-password"), - OsString::from("--expand-environment=no"), - OsString::from(format!("--unit={unit}")), - OsString::from("--property=Type=exec"), - OsString::from("--property=Restart=no"), - OsString::from("--property=KillMode=mixed"), - OsString::from("--property=DynamicUser=yes"), - OsString::from("--property=SupplementaryGroups="), - OsString::from("--property=CapabilityBoundingSet="), - OsString::from("--property=AmbientCapabilities="), - OsString::from("--property=NoNewPrivileges=yes"), - OsString::from("--property=PrivateMounts=yes"), - OsString::from("--property=PrivatePIDs=yes"), - OsString::from("--property=ProtectProc=invisible"), - OsString::from("--property=ProcSubset=all"), - OsString::from("--property=PrivateTmp=yes"), - OsString::from("--property=ProtectSystem=strict"), - OsString::from("--property=ProtectHome=yes"), - OsString::from("--property=ProtectControlGroups=yes"), - OsString::from("--property=ProtectKernelModules=yes"), - OsString::from("--property=ProtectKernelTunables=yes"), - OsString::from("--property=RestrictSUIDSGID=yes"), - OsString::from("--property=LockPersonality=yes"), - OsString::from("--property=UMask=0007"), - OsString::from(format!("--property=Description={unit_description}")), - OsString::from(format!( - "--property=InaccessiblePaths={}", - self.credential_root.display() - )), - OsString::from(format!( - "--property=MemoryMax={}M", - binding.workload.memory_mebibytes - )), - OsString::from(format!( - "--property=CPUQuota={}%", - binding.workload.cpu_quota_percent - )), - OsString::from(format!("--working-directory={}", installed.display())), - OsString::from(format!("--property=ReadOnlyPaths={}", installed.display())), - OsString::from(format!("--property=ReadOnlyPaths={}", bundle.display())), - ]; - for open_file in parent_only_open_files { - args.push(OsString::from(format!("--property=OpenFile={open_file}"))); - } - if let Some(state_group) = &binding.workload.state_group { - args.push(OsString::from(format!("--property=Group={state_group}"))); - } - if binding.workload.network == WorkloadNetwork::None { - args.push(OsString::from("--property=PrivateNetwork=yes")); - } - if let Some(state_root) = &binding.workload.state_root { - args.push(OsString::from(format!( - "--property=ReadWritePaths={}", - state_root.display() - ))); - } - if let Some(write_lease) = &binding.process_write_lease { - for path in [write_lease.record_path.clone(), write_lease.lock_path()] { - args.push(OsString::from(format!( - "--property=ReadOnlyPaths=-{}", - path.display() - ))); - } - } - for path in &binding.workload.read_only_paths { - args.push(OsString::from(format!( - "--property=ReadOnlyPaths={}", - path.display() - ))); - } - for path in &binding.workload.read_write_paths { - args.push(OsString::from(format!( - "--property=ReadWritePaths={}", - path.display() - ))); - } - if binding.workload.devices.is_empty() { - args.push(OsString::from("--property=PrivateDevices=yes")); - } else { - args.push(OsString::from("--property=DevicePolicy=closed")); - for device in &binding.workload.devices { - args.push(OsString::from(format!( - "--property=DeviceAllow={} rw", - device.display() - ))); - } - } - for (name, value) in environment { - args.push(OsString::from(format!("--setenv={name}={value}"))); - } - for (name, path) in binding - .workload - .secret_files - .iter() - .filter(|(name, _)| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) - { - args.push(OsString::from(format!( - "--property=LoadCredential={name}:{}", - path.display() - ))); - } - args.extend(self.launch_command(declaration, binding, installed)?); - self.command(&self.systemd_run_program, args)?; - Ok(()) - } -} - -impl WorkloadPort for SystemdTransientWorkloadDriver { - fn install( - &self, - plan: &CompiledDeploymentPlan, - release: &MaterializedRelease, - ) -> Result { - self.install_release(plan, release) - } - - fn prepare_activation( - &self, - plan: &CompiledDeploymentPlan, - expected: &IdunnExpectedIncarnationRecord, - launch: IdunnRuntimeActivationLaunch, - ) -> Result { - plan.validate()?; - expected.validate()?; - let proposed_activation = launch.activation(); - proposed_activation.validate()?; - ensure!( - expected.plan_id == plan.plan_id - && proposed_activation.expected_projection_sha256 == expected.canonical_sha256()?, - "prepared activation does not belong to the deployment plan" - ); - let (_, binding) = plan.parsed_inputs()?; - ensure!( - expected.target == binding.target, - "prepared activation target differs from the operator binding" - ); - let unit = self.unit_name( - &binding.workload.unit_prefix, - &proposed_activation.runtime_instance_id, - )?; - ensure!( - self.show_unit(&unit)?.is_none(), - "refusing to replace prepared activation material while its deterministic unit exists" - ); - ensure_activation_credential_root(&self.credential_root)?; - let source = self.activation_credential_source(proposed_activation)?; - match fs::symlink_metadata(&source) { - Ok(_) => remove_activation_credential_source(&source) - .context("retiring an orphaned pre-persistence activation credential")?, - Err(error) if error.kind() == ErrorKind::NotFound => {} - Err(error) => { - return Err(error).with_context(|| { - format!( - "inspecting pre-persistence activation credential {}", - source.display() - ) - }); - } - } - let (activation, written_source) = self.write_activation_credential(launch)?; - if written_source != source { - let error = - anyhow::anyhow!("prepared activation used a nondeterministic credential path"); - return match remove_activation_credential_source(&written_source) { - Ok(()) => Err(error), - Err(cleanup) => Err(error.context(format!( - "deleting nondeterministic activation credential also failed: {cleanup:#}" - ))), - }; - } - Ok(activation) - } - - fn start_prepared( - &self, - plan: &CompiledDeploymentPlan, - release: &SealedRelease, - installed: &InstalledReleaseObservation, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result { - plan.validate()?; - release.validate_against(plan)?; - self.validate_installed_release(plan, release, installed)?; - expected.validate()?; - activation.validate()?; - ensure!( - expected.plan_id == plan.plan_id - && expected.sealed_release_id == release.sealed_release_id - && activation.expected_projection_sha256 == expected.canonical_sha256()?, - "workload inputs do not describe one sealed incarnation" - ); - let (declaration, binding) = plan.parsed_inputs()?; - self.validate_writable_bindings(&binding)?; - let executable_artifact = - release_artifact(&declaration, &declaration.service.executable_artifact)?; - let executable = installed.root.join(&executable_artifact.destination); - let unit = self.unit_name( - &binding.workload.unit_prefix, - &activation.runtime_instance_id, - )?; - let bundle = self.prepare_runtime_bundle(&binding, expected, activation)?; - let service_credential_names = binding - .workload - .secret_files - .keys() - .filter(|name| name.as_str() != RUNTIME_PRESENCE_IDENTITY_BINDING) - .cloned() - .collect::>(); - let environment_names = self - .launch_environment(&binding, &bundle, expected, &unit)? - .into_keys() - .collect::>(); - let parent_only_file_descriptors = - self.parent_only_file_descriptors(&binding, expected, activation)?; - let unit_exists = self.show_unit(&unit)?.is_some(); - if !unit_exists { - self.start_transient( - &declaration, - &binding, - &installed.root, - &bundle, - expected, - &parent_only_file_descriptors, - &unit, - )?; - } - let mut last_error = None; - for _ in 0..100 { - match self.observe_unit( - &unit, - &executable, - &activation.runtime_instance_id, - &activation.activation_signer_identity_id, - &activation.activation_signer_public_key, - &environment_names, - &service_credential_names, - &parent_only_file_descriptors, - ) { - Ok(observation) => { - let validation = self.validate_launch_observation( - &observation, - &declaration, - &binding, - &installed.root, - &bundle, - expected, - &activation, - ); - match validation.and_then(|()| { - ensure!( - observation.executable_sha256 == expected.artifact_sha256, - "started workload executable differs from Expected" - ); - Ok(()) - }) { - Ok(()) => return Ok(observation), - Err(error) => last_error = Some(error), - } - } - Err(error) => last_error = Some(error), - } - thread::sleep(Duration::from_millis(50)); - } - Err(last_error.unwrap_or_else(|| anyhow::anyhow!("systemd did not expose the candidate"))) - } - - fn discard_prepared( - &self, - plan: &CompiledDeploymentPlan, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result<()> { - plan.validate()?; - expected.validate()?; - activation.validate()?; - ensure!( - expected.plan_id == plan.plan_id - && activation.expected_projection_sha256 == expected.canonical_sha256()?, - "discarded activation does not belong to the prepared deployment" - ); - let (_, binding) = plan.parsed_inputs()?; - ensure!( - binding.target == expected.target, - "discarded activation target differs from its binding" - ); - let unit = self.unit_name( - &binding.workload.unit_prefix, - &activation.runtime_instance_id, - )?; - let unit_description = format!( - "Idunn {} {}", - binding.target, activation.runtime_instance_id - ); - self.stop_submitted_unit(&unit, &unit_description) - .context("stopping prepared candidate without a durable workload observation")?; - remove_activation_credential_source(&self.activation_credential_source(activation)?) - .context("deleting discarded activation credential source") - } - - fn observe( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - prior: &WorkloadObservation, - ) -> Result { - expected.validate()?; - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()? - && activation.runtime_instance_id == prior.runtime_instance_id, - "workload observation belongs to another activation" - ); - let observed = self.observe_unit( - &prior.unit, - &prior.executable, - &activation.runtime_instance_id, - &activation.activation_signer_identity_id, - &activation.activation_signer_public_key, - &prior.environment_names, - &prior - .service_credentials - .iter() - .map(|credential| credential.environment_name.clone()) - .collect::>(), - &prior.parent_only_file_descriptors, - )?; - ensure!( - observed == *prior && observed.executable_sha256 == expected.artifact_sha256, - "native workload identity changed after observation" - ); - let credential_source = self.activation_credential_source(activation)?; - remove_activation_credential_source(&credential_source) - .context("retiring a recovered activation credential source")?; - let after_unlink = self.observe_unit( - &prior.unit, - &prior.executable, - &activation.runtime_instance_id, - &activation.activation_signer_identity_id, - &activation.activation_signer_public_key, - &prior.environment_names, - &prior - .service_credentials - .iter() - .map(|credential| credential.environment_name.clone()) - .collect::>(), - &prior.parent_only_file_descriptors, - )?; - ensure!( - after_unlink == observed, - "native workload identity changed after recovered credential cleanup" - ); - Ok(after_unlink) - } - - fn stop(&self, observation: &WorkloadObservation) -> Result<()> { - let Some(unit_observation) = self.show_unit(&observation.unit)? else { - return Ok(()); - }; - let values = &unit_observation.properties; - let active = required_systemd_property(&values, "ActiveState")?; - let sub = required_systemd_property(&values, "SubState")?; - if active == "active" && sub == "running" { - let current = self.observe_unit( - &observation.unit, - &observation.executable, - &observation.runtime_instance_id, - &observation.activation_signer_identity_id, - &observation.activation_signer_public_key, - &observation.environment_names, - &observation - .service_credentials - .iter() - .map(|credential| credential.environment_name.clone()) - .collect::>(), - &observation.parent_only_file_descriptors, - )?; - ensure!( - current == *observation, - "refusing to stop a workload whose native identity changed" - ); - } else { - ensure!( - values.get("InvocationID") == Some(&observation.invocation_id) - && values.get("Description") == Some(&observation.unit_description) - && matches!(active, "inactive" | "failed"), - "refusing to stop a foreign or transitional systemd unit" - ); - } - self.command( - &self.systemctl_program, - [OsString::from("stop"), OsString::from(&observation.unit)], - )?; - if let Some(unit_observation) = self.show_unit(&observation.unit)? { - let values = &unit_observation.properties; - ensure!( - values - .get("ActiveState") - .is_some_and(|state| state == "inactive" || state == "failed"), - "systemd unit remained active after stop" - ); - } - Ok(()) - } -} - -/// One dedicated CultCache file is the write-lease authority for one target. -/// The admitted target holds its sibling lock shared for that process lifetime. -/// Grant and revocation therefore make one nonblocking CAS attempt; revocation -/// follows an exact incumbent stop. A foreign or stuck holder cannot freeze -/// Idunn's control loop. Route membership never enters this store. -pub struct CultCacheWriteLeaseDriver { - pub target: String, - pub record_path: PathBuf, -} - -impl CultCacheWriteLeaseDriver { - pub fn new(target: impl Into, record_path: impl Into) -> Self { - Self { - target: target.into(), - record_path: record_path.into(), - } - } - - fn current(&self) -> Result> { - validate_root_authority_path(&self.record_path)?; - if !self.record_path.exists() { - return Ok(None); - } - let entries = SingleFileMessagePackBackingStore::new(&self.record_path) - .pull_all_read_only_snapshot()?; - match entries.as_slice() { - [] => Ok(None), - [envelope] - if envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE - && envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) => - { - let lease = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; - ensure!( - envelope.key == lease.target, - "write-lease key is not its target" - ); - ensure!( - lease.target == self.target, - "write-lease store belongs to another target" - ); - Ok(Some((envelope.clone(), lease))) - } - _ => bail!("process write-lease store is foreign or ambiguous"), - } - } - - fn envelope(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { - Ok(CultCacheEnvelope { - key: lease.target.clone(), - r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), - payload: lease.canonical_bytes()?, - stored_at: rfc3339_millis(lease.issued_at_unix_millis)?, - schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), - }) - } - - fn validate_grant( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result<()> { - expected.validate()?; - activation.validate()?; - lease.validate()?; - let expected_sha256 = expected.canonical_sha256()?; - let activation_sha256 = activation.canonical_sha256()?; - ensure!( - self.target == expected.target - && lease.target == expected.target - && lease.expected_projection_sha256 == expected_sha256 - && lease.plan_id == expected.plan_id - && lease.incarnation_id == expected.incarnation_id - && lease.sealed_release_id == expected.sealed_release_id - && lease.activation_witness_sha256 == activation_sha256 - && lease.runtime_id == expected.runtime_id - && lease.runtime_instance_id == activation.runtime_instance_id - && lease.state_schema_generation - == expected - .state_schema_generation - .as_deref() - .context("write-lease Expected has no state generation")? - && lease.state_contract_sha256 - == expected - .state_contract_sha256 - .as_deref() - .context("write-lease Expected has no state contract")? - && lease.warming_presence_sha256 == warming.signed_presence_sha256() - && warming.runtime_instance_id() == activation.runtime_instance_id.as_str(), - "process write lease does not bind the exact warming candidate" - ); - Ok(()) - } -} - -impl WriteLeasePort for CultCacheWriteLeaseDriver { - fn revoke_exact(&self, incumbent: Option<&IdunnProcessWriteLeaseRecord>) -> Result<()> { - if let Some(incumbent) = incumbent { - incumbent.validate()?; - ensure!( - incumbent.target == self.target, - "incumbent write lease belongs to another target" - ); - } - let current = self.current()?; - let Some((envelope, current_lease)) = current else { - return Ok(()); - }; - ensure!( - incumbent == Some(¤t_lease), - "refusing to revoke an unexpected process write lease" - ); - let store = SingleFileMessagePackBackingStore::new(&self.record_path); - match store.try_compare_exchange_snapshot(std::slice::from_ref(&envelope), &[])? { - TryCompareExchangeSnapshotOutcome::Exchanged => Ok(()), - TryCompareExchangeSnapshotOutcome::Mismatch => { - bail!("process write lease changed while fencing the incumbent") - } - TryCompareExchangeSnapshotOutcome::LockContended => { - bail!("process write lease is held by a shared-lock consumer") - } - } - } - - fn observe_empty(&self) -> Result { - Ok(self.current()?.is_none()) - } - - fn observe_exact(&self, lease: &IdunnProcessWriteLeaseRecord) -> Result { - lease.validate()?; - ensure!( - lease.target == self.target, - "observed process write lease belongs to another target" - ); - Ok(self - .current()? - .is_some_and(|(_, current)| current == *lease)) - } - - fn grant( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result { - self.validate_grant(expected, activation, warming, lease)?; - if let Some((_, current)) = self.current()? { - ensure!( - current == *lease, - "another process already owns the write lease" - ); - harden_root_authority_file(&self.record_path)?; - harden_root_authority_file(&authority_lock_path(&self.record_path))?; - return lease.canonical_sha256(); - } - let envelope = self.envelope(lease)?; - let store = SingleFileMessagePackBackingStore::new(&self.record_path); - match store.try_compare_exchange_snapshot(&[], &[envelope])? { - TryCompareExchangeSnapshotOutcome::Exchanged => {} - TryCompareExchangeSnapshotOutcome::Mismatch => { - bail!("process write-lease grant lost its empty-store compare-exchange") - } - TryCompareExchangeSnapshotOutcome::LockContended => { - bail!("process write lease is held by a shared-lock consumer") - } - } - harden_root_authority_file(&self.record_path)?; - harden_root_authority_file(&authority_lock_path(&self.record_path))?; - lease.canonical_sha256() - } - - fn observe( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result { - self.validate_grant(expected, activation, warming, lease)?; - Ok(self - .current()? - .is_some_and(|(_, current)| current == *lease)) - } -} - -/// Idunn projects desired identity plus its own activation and current-lease -/// facts here. Service presence is absent by construction; only Odin may -/// correlate these records with signed runtime observation into Present/Ready. -pub struct CultCacheTopologyDriver { - pub projection_store: PathBuf, - pub correlation_store: PathBuf, -} - -impl CultCacheTopologyDriver { - /// Atomically demotes a failed pre-fence candidate back to the admitted - /// generation's Expected projection. Runtime activation and write-lease - /// records are deliberately absent until admitted-body supervision proves - /// the physical process and lease again. - pub fn restore_admitted_expected_only( - &self, - failed_expected: &IdunnExpectedIncarnationRecord, - failed_provider_anchor: &ServiceIdentityTrustAnchor, - failed_activation: Option<&IdunnRuntimeActivationRecord>, - admitted_expected: &IdunnExpectedIncarnationRecord, - admitted_provider_anchor: &ServiceIdentityTrustAnchor, - admitted_activation: &IdunnRuntimeActivationRecord, - admitted_lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result { - failed_expected.validate()?; - admitted_expected.validate()?; - ensure!( - failed_expected.target == admitted_expected.target, - "projection restoration crosses deployment targets" - ); - let failed_anchor = runtime_presence_trust_anchor(failed_expected, failed_provider_anchor)?; - let admitted_anchor = - runtime_presence_trust_anchor(admitted_expected, admitted_provider_anchor)?; - if let Some(activation) = failed_activation { - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == failed_expected.canonical_sha256()?, - "failed activation does not bind the failed Expected projection" - ); - } - admitted_activation.validate()?; - ensure!( - admitted_activation.expected_projection_sha256 - == admitted_expected.canonical_sha256()?, - "admitted activation does not bind the admitted Expected projection" - ); - if let Some(lease) = admitted_lease { - validate_topology_lease(admitted_expected, admitted_activation, lease)?; - } - if let Some(parent) = self.projection_store.parent() { - fs::create_dir_all(parent)?; - } - let store = SingleFileMessagePackBackingStore::new(&self.projection_store); - let target = admitted_expected.target.as_str(); - let anchor_key = runtime_presence_trust_anchor_id(target); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let current_expected = - projection_entry(&entries, IdunnExpectedIncarnationRecord::TYPE, target)?; - if let Some(envelope) = current_expected { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA), - "restored Expected projection schema is foreign" - ); - let current = IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)?; - ensure!( - current == *failed_expected || current == *admitted_expected, - "refusing to replace an unknown Expected projection" - ); - } - let current_anchor = projection_entry( - &entries, - GameCultServiceTrustAnchorRecord::TYPE, - &anchor_key, - )?; - if let Some(envelope) = current_anchor { - let current = service_trust_anchor_from_envelope(envelope)?; - ensure!( - current == failed_anchor || current == admitted_anchor, - "refusing to replace an unknown runtime presence trust anchor" - ); - } - let current_activation = - projection_entry(&entries, IdunnRuntimeActivationRecord::TYPE, target)?; - if let Some(envelope) = current_activation { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA), - "restored activation projection schema is foreign" - ); - let current = IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)?; - ensure!( - failed_activation == Some(¤t) || admitted_activation == ¤t, - "refusing to remove an unknown runtime activation projection" - ); - } - let current_lease = - projection_entry(&entries, IdunnProcessWriteLeaseRecord::TYPE, target)?; - if let Some(envelope) = current_lease { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA), - "restored write-lease projection schema is foreign" - ); - let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; - ensure!( - admitted_lease == Some(¤t), - "refusing to remove an unknown process write-lease projection" - ); - } - if current_expected.is_some_and(|envelope| { - IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload) - .is_ok_and(|current| current == *admitted_expected) - }) && current_anchor.is_some_and(|envelope| { - service_trust_anchor_from_envelope(envelope) - .is_ok_and(|current| current == admitted_anchor) - }) && current_activation.is_none() - && current_lease.is_none() - { - return admitted_expected.canonical_sha256(); - } - let mut replacement = entries - .iter() - .filter(|envelope| { - !((envelope.key == target - && matches!( - envelope.r#type.as_str(), - IdunnExpectedIncarnationRecord::TYPE - | IdunnRuntimeActivationRecord::TYPE - | IdunnProcessWriteLeaseRecord::TYPE - )) - || (envelope.key == anchor_key - && envelope.r#type == GameCultServiceTrustAnchorRecord::TYPE)) - }) - .cloned() - .collect::>(); - replacement.extend([ - CultCacheEnvelope { - key: admitted_expected.target.clone(), - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: admitted_expected.canonical_bytes()?, - stored_at: chrono::Utc::now().to_rfc3339(), - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), - }, - CultCacheEnvelope { - key: admitted_anchor.trust_anchor_id.clone(), - r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), - payload: rmp_serde::to_vec(&admitted_anchor)?, - stored_at: rfc3339_millis(admitted_anchor.bound_at_unix_millis)?, - schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), - }, - ]); - if store.compare_exchange_snapshot(&entries, &replacement)? { - return admitted_expected.canonical_sha256(); - } - } - bail!("topology projection changed repeatedly during admitted restoration") - } - - pub fn admitted_runtime_projection_is_exact( - &self, - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, - activation: &IdunnRuntimeActivationRecord, - lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result { - expected.validate()?; - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()?, - "admitted activation does not bind its Expected projection" - ); - if let Some(lease) = lease { - validate_topology_lease(expected, activation, lease)?; - } - if !self.projection_store.exists() { - return Ok(false); - } - let entries = SingleFileMessagePackBackingStore::new(&self.projection_store) - .pull_all_read_only_snapshot()?; - let anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; - let expected_is_exact = match projection_entry( - &entries, - IdunnExpectedIncarnationRecord::TYPE, - &expected.target, - )? { - Some(envelope) => { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA), - "admitted Expected projection schema is foreign" - ); - ensure!( - IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)? - == *expected, - "admitted Expected projection was replaced by another incarnation" - ); - true - } - None => false, - }; - let anchor_is_exact = match projection_entry( - &entries, - GameCultServiceTrustAnchorRecord::TYPE, - &anchor.trust_anchor_id, - )? { - Some(envelope) => { - ensure!( - service_trust_anchor_from_envelope(envelope)? == anchor, - "admitted runtime presence trust anchor was replaced" - ); - true - } - None => false, - }; - let activation_is_exact = match projection_entry( - &entries, - IdunnRuntimeActivationRecord::TYPE, - &expected.target, - )? { - Some(envelope) => { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA), - "admitted activation projection schema is foreign" - ); - ensure!( - IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)? - == *activation, - "admitted activation projection was replaced" - ); - true - } - None => false, - }; - let lease_is_exact = match projection_entry( - &entries, - IdunnProcessWriteLeaseRecord::TYPE, - &expected.target, - )? { - Some(envelope) => { - ensure!( - envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA), - "admitted write-lease projection schema is foreign" - ); - let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; - ensure!( - lease == Some(¤t), - "admitted write-lease projection was replaced" - ); - true - } - None => lease.is_none(), - }; - Ok(expected_is_exact && anchor_is_exact && activation_is_exact && lease_is_exact) - } -} - -fn runtime_presence_trust_anchor( - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, -) -> Result { - expected.validate()?; - ensure!( - provider_anchor.schema_version - == ::TRUST_ANCHOR_SCHEMA, - "runtime presence trust anchor schema is unsupported" - ); - ensure!( - derive_service_identity_id::(&provider_anchor.public_key)? - == provider_anchor.identity_id - && provider_anchor.identity_id == expected.expected_signer_identity_id, - "runtime presence trust anchor does not bind the Expected signer" - ); - let bound_at_unix_millis: u64 = - chrono::DateTime::parse_from_rfc3339(&provider_anchor.identity_created_at)? - .timestamp_millis() - .try_into() - .context("runtime presence trust anchor creation time predates Unix epoch")?; - let anchor = GameCultServiceTrustAnchorRecord { - schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), - trust_anchor_id: runtime_presence_trust_anchor_id(&expected.target), - service_id: expected.target.clone(), - runtime_id: expected.runtime_id.clone(), - signer_identity_id: provider_anchor.identity_id.clone(), - signer_public_key: provider_anchor.public_key.clone(), - signature_algorithm: "ed25519".into(), - signing_purpose: GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE.into(), - signed_schema: GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into(), - binding_authority: "root".into(), - bound_at_unix_millis, - expires_at_unix_millis: None, - private_state_exposed: false, - }; - anchor.validate()?; - Ok(anchor) -} - -fn runtime_presence_trust_anchor_id(target: &str) -> String { - format!("root/{target}/runtime-presence") -} - -fn validate_topology_lease( - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - lease: &IdunnProcessWriteLeaseRecord, -) -> Result<()> { - expected.validate()?; - activation.validate()?; - lease.validate()?; - ensure!( - expected.write_lease_required - && lease.target == expected.target - && lease.expected_projection_sha256 == expected.canonical_sha256()? - && lease.plan_id == expected.plan_id - && lease.incarnation_id == expected.incarnation_id - && lease.sealed_release_id == expected.sealed_release_id - && lease.activation_witness_sha256 == activation.canonical_sha256()? - && lease.state_schema_generation - == expected - .state_schema_generation - .as_deref() - .context("write-lease Expected has no state generation")? - && lease.state_contract_sha256 - == expected - .state_contract_sha256 - .as_deref() - .context("write-lease Expected has no state contract")? - && lease.runtime_id == expected.runtime_id - && lease.runtime_instance_id == activation.runtime_instance_id, - "topology write lease does not bind the exact Expected activation" - ); - Ok(()) -} - -fn service_trust_anchor_from_envelope( - envelope: &CultCacheEnvelope, -) -> Result { - ensure!( - envelope.schema_id.as_deref() == Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA), - "service trust-anchor projection schema is foreign" - ); - let anchor: GameCultServiceTrustAnchorRecord = rmp_serde::from_slice(&envelope.payload)?; - ensure!( - rmp_serde::to_vec(&anchor)? == envelope.payload, - "service trust-anchor projection is noncanonical" - ); - anchor.validate()?; - Ok(anchor) -} - -fn replace_records_atomically(path: &Path, replacements: &[CultCacheEnvelope]) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - ensure!( - replacements.iter().enumerate().all(|(index, entry)| { - replacements[index + 1..].iter().all(|other| { - (entry.r#type.as_str(), entry.key.as_str()) - != (other.r#type.as_str(), other.key.as_str()) - }) - }), - "atomic projection set contains a duplicate identity" - ); - let store = SingleFileMessagePackBackingStore::new(path); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let conditions = replacements - .iter() - .map(|replacement| { - Ok(CultCacheExpectedEnvelope { - r#type: replacement.r#type.clone(), - key: replacement.key.clone(), - current: projection_entry(&entries, &replacement.r#type, &replacement.key)? - .cloned(), - }) - }) - .collect::>>()?; - if conditions - .iter() - .zip(replacements) - .all(|(condition, replacement)| condition.current.as_ref() == Some(replacement)) - { - return Ok(()); - } - if store.compare_exchange(&conditions, replacements)? { - return Ok(()); - } - } - bail!("CultCache projection changed too often to publish atomic set") -} - -fn projection_entry<'a>( - entries: &'a [CultCacheEnvelope], - r#type: &str, - key: &str, -) -> Result> { - let mut matches = entries - .iter() - .filter(|entry| entry.r#type == r#type && entry.key == key); - let current = matches.next(); - ensure!( - matches.next().is_none(), - "CultCache projection identity is ambiguous" - ); - Ok(current) -} - -fn insert_exact_process_lease( - path: &Path, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - replacement: CultCacheEnvelope, -) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - let store = SingleFileMessagePackBackingStore::new(path); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let projected_expected = projection_entry( - &entries, - IdunnExpectedIncarnationRecord::TYPE, - &expected.target, - )? - .context("process write lease has no current Expected projection")?; - ensure!( - projected_expected.schema_id.as_deref() == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA) - && IdunnExpectedIncarnationRecord::decode_canonical(&projected_expected.payload)? - == *expected, - "process write lease current Expected projection is substituted" - ); - let projected_activation = projection_entry( - &entries, - IdunnRuntimeActivationRecord::TYPE, - &expected.target, - )? - .context("process write lease has no observed activation projection")?; - ensure!( - projected_activation.schema_id.as_deref() == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA) - && IdunnRuntimeActivationRecord::decode_canonical(&projected_activation.payload)? - == *activation, - "process write lease observed activation projection is substituted" - ); - let projected_anchor = service_trust_anchor_from_envelope( - projection_entry( - &entries, - GameCultServiceTrustAnchorRecord::TYPE, - &runtime_presence_trust_anchor_id(&expected.target), - )? - .context("process write lease has no runtime presence trust anchor")?, - )?; - ensure!( - projected_anchor.service_id == expected.target - && projected_anchor.runtime_id == expected.runtime_id - && projected_anchor.signer_identity_id == expected.expected_signer_identity_id - && projected_anchor.signing_purpose - == GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE - && projected_anchor.signed_schema == GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, - "process write lease runtime presence trust anchor is substituted" - ); - let current = projection_entry(&entries, &replacement.r#type, &replacement.key)?; - if current == Some(&replacement) { - return Ok(()); - } - ensure!( - current.is_none(), - "refusing to replace a different current process write-lease projection" - ); - let conditions = [ - CultCacheExpectedEnvelope { - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - key: expected.target.clone(), - current: Some(projected_expected.clone()), - }, - CultCacheExpectedEnvelope { - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - key: expected.target.clone(), - current: Some(projected_activation.clone()), - }, - CultCacheExpectedEnvelope { - r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), - key: runtime_presence_trust_anchor_id(&expected.target), - current: projection_entry( - &entries, - GameCultServiceTrustAnchorRecord::TYPE, - &runtime_presence_trust_anchor_id(&expected.target), - )? - .cloned(), - }, - CultCacheExpectedEnvelope { - r#type: replacement.r#type.clone(), - key: replacement.key.clone(), - current: None, - }, - ]; - if store.compare_exchange(&conditions, std::slice::from_ref(&replacement))? { - return Ok(()); - } - } - bail!("CultCache projection changed too often to publish exact record") -} - -impl TopologyPort for CultCacheTopologyDriver { - fn publish_expected( - &self, - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, - ) -> Result { - expected.validate()?; - let anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; - replace_records_atomically( - &self.projection_store, - &[ - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: expected.canonical_bytes()?, - stored_at: chrono::Utc::now().to_rfc3339(), - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), - }, - CultCacheEnvelope { - key: anchor.trust_anchor_id.clone(), - r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), - payload: rmp_serde::to_vec(&anchor)?, - stored_at: rfc3339_millis(anchor.bound_at_unix_millis)?, - schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), - }, - ], - )?; - expected.canonical_sha256() - } - - fn withdraw_expected( - &self, - expected: &IdunnExpectedIncarnationRecord, - provider_anchor: &ServiceIdentityTrustAnchor, - activation: Option<&IdunnRuntimeActivationRecord>, - lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result<()> { - expected.validate()?; - let exact_anchor = runtime_presence_trust_anchor(expected, provider_anchor)?; - if let Some(activation) = activation { - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()? - && activation.runtime_id == expected.runtime_id, - "withdrawn activation does not bind the exact Expected projection" - ); - } - if let Some(lease) = lease { - validate_topology_lease( - expected, - activation.context("withdrawn lease has no exact activation")?, - lease, - )?; - } - if !self.projection_store.exists() { - return Ok(()); - } - let store = SingleFileMessagePackBackingStore::new(&self.projection_store); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let mut found_expected = false; - let mut found_anchor = false; - let mut found_activation = false; - let mut found_lease = false; - let mut retained = Vec::with_capacity(entries.len()); - for envelope in &entries { - if envelope.r#type == GameCultServiceTrustAnchorRecord::TYPE - && envelope.key == exact_anchor.trust_anchor_id - { - ensure!( - !found_anchor - && service_trust_anchor_from_envelope(envelope)? == exact_anchor, - "refusing to withdraw a substituted runtime presence trust anchor" - ); - found_anchor = true; - continue; - } - if envelope.key != expected.target { - retained.push(envelope.clone()); - continue; - } - if envelope.r#type == IdunnExpectedIncarnationRecord::TYPE { - ensure!( - !found_expected - && envelope.schema_id.as_deref() - == Some(IDUNN_EXPECTED_INCARNATION_SCHEMA) - && IdunnExpectedIncarnationRecord::decode_canonical(&envelope.payload)? - == *expected, - "refusing to withdraw a substituted Expected projection" - ); - found_expected = true; - continue; - } - if envelope.r#type == IdunnRuntimeActivationRecord::TYPE { - let current = - IdunnRuntimeActivationRecord::decode_canonical(&envelope.payload)?; - ensure!( - !found_activation - && envelope.schema_id.as_deref() - == Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA) - && activation == Some(¤t), - "refusing to withdraw a substituted runtime activation" - ); - found_activation = true; - continue; - } - if envelope.r#type == IdunnProcessWriteLeaseRecord::TYPE { - let current = - IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; - ensure!( - !found_lease - && envelope.schema_id.as_deref() - == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) - && lease == Some(¤t), - "refusing to withdraw a substituted process write lease" - ); - found_lease = true; - continue; - } - retained.push(envelope.clone()); - } - if !found_expected && !found_anchor && !found_activation && !found_lease { - return Ok(()); - } - if store.compare_exchange_snapshot(&entries, &retained)? { - return Ok(()); - } - } - bail!("Expected projection changed repeatedly while withdrawing it") - } - - fn publish_observed_activation( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - observation: &WorkloadObservation, - ) -> Result { - expected.validate()?; - activation.validate()?; - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()? - && activation.runtime_instance_id == observation.runtime_instance_id - && observation.executable_sha256 == expected.artifact_sha256, - "observed activation does not name the Expected native process" - ); - upsert_record( - &self.projection_store, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: activation.canonical_bytes()?, - stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - )?; - activation.canonical_sha256() - } - - fn publish_process_write_lease( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - lease: &IdunnProcessWriteLeaseRecord, - ) -> Result { - validate_topology_lease(expected, activation, lease)?; - insert_exact_process_lease( - &self.projection_store, - expected, - activation, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), - payload: lease.canonical_bytes()?, - stored_at: rfc3339_millis(lease.issued_at_unix_millis)?, - schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), - }, - )?; - lease.canonical_sha256() - } - - fn withdraw_process_write_lease( - &self, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - lease: Option<&IdunnProcessWriteLeaseRecord>, - ) -> Result<()> { - expected.validate()?; - activation.validate()?; - if let Some(lease) = lease { - validate_topology_lease(expected, activation, lease)?; - } - if !self.projection_store.exists() { - return Ok(()); - } - let store = SingleFileMessagePackBackingStore::new(&self.projection_store); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let mut found = false; - let mut retained = Vec::with_capacity(entries.len()); - for envelope in &entries { - if envelope.key != expected.target - || envelope.r#type != IdunnProcessWriteLeaseRecord::TYPE - { - retained.push(envelope.clone()); - continue; - } - let current = IdunnProcessWriteLeaseRecord::decode_canonical(&envelope.payload)?; - ensure!( - !found - && envelope.schema_id.as_deref() == Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA) - && lease == Some(¤t), - "refusing to withdraw an unexpected process write-lease projection" - ); - found = true; - } - if !found { - return Ok(()); - } - if store.compare_exchange_snapshot(&entries, &retained)? { - return Ok(()); - } - } - bail!("process write-lease projection changed repeatedly while withdrawing it") - } - - fn receive(&self, target: &str) -> Result> { - require_driver_id(target, "topology target")?; - if !self.correlation_store.exists() { - return Ok(None); - } - let entries = SingleFileMessagePackBackingStore::new(&self.correlation_store) - .pull_all_read_only_snapshot()?; - let mut matches = entries.iter().filter(|envelope| { - envelope.r#type == OdinRuntimeTopologyCorrelationRecord::TYPE && envelope.key == target - }); - let Some(envelope) = matches.next() else { - return Ok(None); - }; - ensure!( - matches.next().is_none(), - "Odin topology correlation is ambiguous" - ); - ensure!( - envelope.schema_id.as_deref() == Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA), - "Odin topology correlation schema is foreign" - ); - Ok(Some(ReceivedOdinTopologyCorrelation { - target: target.to_owned(), - canonical_bytes: envelope.payload.clone(), - })) - } -} - -/// nginx owns proxy mechanics. Idunn supplies one exact backend membership, -/// validates the complete nginx configuration, and reloads it. Configuration -/// bytes are actuator state, not proof that the selected runtime answered on -/// the stable route; the control plane admits that proof separately. -pub struct NginxRouteDriver { - pub binding: RouteBinding, - pub nginx_program: PathBuf, - pub systemd_run_program: PathBuf, - pub systemctl_program: PathBuf, - pub preflight_root: PathBuf, -} - -impl NginxRouteDriver { - pub fn new(binding: RouteBinding) -> Self { - Self { - binding, - nginx_program: PathBuf::from("/usr/sbin/nginx"), - systemd_run_program: PathBuf::from("/usr/bin/systemd-run"), - systemctl_program: PathBuf::from("/usr/bin/systemctl"), - preflight_root: PathBuf::from("/run/idunn/route-preflight"), - } - } - - fn command(&self, program: &Path, args: I) -> Result - where - I: IntoIterator, - S: AsRef, - { - ensure!( - program.is_absolute(), - "route actuator program is not absolute" - ); - let output = Command::new(program) - .args(args) - .stdin(Stdio::null()) - .env_clear() - .env("LANG", "C.UTF-8") - .output() - .with_context(|| format!("starting route actuator {}", program.display()))?; - if !output.status.success() { - bail!( - "route actuator {} exited with {}: {}", - program.display(), - output.status, - String::from_utf8_lossy(&output.stderr).trim() - ); - } - Ok(output) - } - - fn render(&self, expected: &IdunnExpectedIncarnationRecord) -> Result> { - expected.validate()?; - let expected_projection_sha256 = expected.canonical_sha256()?; - let route = expected - .route - .as_ref() - .context("expected incarnation has no route")?; - ensure!( - route.route_id == self.binding.route_id - && route.stable_endpoint == self.binding.stable_endpoint, - "route driver binding differs from Expected" - ); - let endpoint_prefix = match (self.binding.driver, route.transport.as_str()) { - (RouteDriver::NginxStreamTcp, "http") => "http://", - (RouteDriver::NginxStreamTcp, "tcp") => "tcp://", - (RouteDriver::NginxStreamUdp, "rudp") => "rudp://", - _ => bail!("route driver cannot carry the Expected transport"), - }; - let (candidate_host, candidate_port) = - endpoint_host_port(&route.candidate_endpoint, endpoint_prefix)?; - ensure!( - candidate_host == self.binding.private_host - && (self.binding.private_port_start..=self.binding.private_port_end) - .contains(&candidate_port), - "Expected candidate endpoint is outside the route binding" - ); - let upstream = nginx_identifier(&self.binding.route_id)?; - let rendered = match self.binding.driver { - RouteDriver::NginxStreamTcp => { - let (stable_host, stable_port) = - endpoint_host_port(&route.stable_endpoint, endpoint_prefix)?; - format!( - "# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n" - ) - } - RouteDriver::NginxStreamUdp => { - let (stable_host, stable_port) = - endpoint_host_port(&route.stable_endpoint, "rudp://")?; - format!( - "# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port} udp reuseport;\n proxy_pass {upstream};\n}}\n" - ) - } - }; - Ok(rendered.into_bytes()) - } - - fn current_configuration(&self) -> Result>> { - Ok(match fs::read(&self.binding.config_path) { - Ok(bytes) => Some(bytes), - Err(error) if error.kind() == ErrorKind::NotFound => None, - Err(error) => return Err(error).context("reading route fragment"), - }) - } - - fn write_fragment(&self, content: Option<&[u8]>) -> Result<()> { - match content { - Some(bytes) => atomic_replace(&self.binding.config_path, bytes), - None => match fs::remove_file(&self.binding.config_path) { - Ok(()) => sync_parent_directory(&self.binding.config_path), - Err(error) if error.kind() == ErrorKind::NotFound => Ok(()), - Err(error) => Err(error).context("removing route fragment"), - }, - } - } - - fn reload(&self) -> Result<()> { - self.command(&self.nginx_program, [OsString::from("-t")])?; - self.command( - &self.systemctl_program, - [ - OsString::from("reload"), - OsString::from(&self.binding.reload_unit), - ], - )?; - Ok(()) - } - - fn validate_candidate_in_private_mount(&self, rendered: &[u8]) -> Result<()> { - ensure_route_preflight_root(&self.preflight_root)?; - let route = nginx_identifier(&self.binding.route_id)?; - let nonce = Uuid::new_v4(); - let candidate = self - .preflight_root - .join(format!("{route}-{nonce}.candidate")); - write_root_owned_file(&candidate, rendered, 0o400)?; - let unit = format!("idunn-nginx-preflight-{nonce}"); - let validation = self.command( - &self.systemd_run_program, - [ - OsString::from("--wait"), - OsString::from("--collect"), - OsString::from("--quiet"), - OsString::from(format!("--unit={unit}")), - OsString::from("--property=Type=exec"), - OsString::from("--property=PrivateMounts=yes"), - systemd_read_only_bind_property(&candidate, &self.binding.config_path)?, - OsString::from("--"), - self.nginx_program.clone().into_os_string(), - OsString::from("-t"), - ], - ); - let cleanup = remove_exact_root_owned_file(&candidate, 0o400); - match (validation, cleanup) { - (Ok(_), Ok(())) => Ok(()), - (Err(validation), Ok(())) => { - Err(validation).context("candidate route is invalid in a private mount namespace") - } - (Ok(_), Err(cleanup)) => { - Err(cleanup).context("deleting route preflight material") - } - (Err(validation), Err(cleanup)) => Err(validation).context(format!( - "candidate route is invalid; deleting its private preflight material also failed: {cleanup:#}" - )), - } - } - - fn restore(&self, prior: Option<&[u8]>) -> Result<()> { - self.write_fragment(prior)?; - self.reload() - } - - fn fail_after_rollback( - &self, - prior: Option<&[u8]>, - failure: anyhow::Error, - context: &str, - ) -> Result { - match self.restore(prior) { - Ok(()) => Err(failure).context(context.to_owned()), - Err(rollback) => Err(failure).context(format!( - "{context}; route rollback also failed: {rollback:#}" - )), - } - } - - pub fn preflight( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - incumbent: Option<&RouteObservation>, - ) -> Result { - let rendered = self.render(expected)?; - ensure!(!rendered.is_empty(), "candidate route rendered empty"); - let current = self.current_configuration()?; - let incumbent_membership_sha256 = current.as_ref().map(|bytes| sha256_id(bytes)); - match incumbent { - Some(incumbent) => ensure!( - incumbent.route_id == self.binding.route_id - && Some(incumbent.membership_sha256.as_str()) - == incumbent_membership_sha256.as_deref(), - "route preflight baseline differs from the admitted incumbent" - ), - None => ensure!( - incumbent_membership_sha256.is_none(), - "route preflight found an unadmitted incumbent" - ), - } - self.validate_candidate_in_private_mount(&rendered)?; - let after = self.current_configuration()?; - ensure!( - after == current, - "nginx route baseline changed during candidate validation" - ); - let receipt = RoutePreflightReceipt { - route_id: self.binding.route_id.clone(), - candidate_runtime_instance_id: runtime_instance_id.to_owned(), - candidate_membership_sha256: sha256_id(&rendered), - incumbent_runtime_instance_id: incumbent - .map(|observation| observation.runtime_instance_id.clone()), - incumbent_membership_sha256, - incumbent_configuration: current, - }; - receipt.validate()?; - Ok(receipt) - } - - /// Install the candidate route membership and require nginx to accept the - /// complete configuration. Success is not route admission: callers must - /// still obtain a fresh signed runtime observation through the stable - /// listener. - pub fn install( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - preflight: &RoutePreflightReceipt, - rollback_allowed: bool, - ) -> Result { - preflight.validate()?; - let rendered = self.render(expected)?; - ensure!( - preflight.route_id == self.binding.route_id - && preflight.candidate_runtime_instance_id == runtime_instance_id - && preflight.candidate_membership_sha256 == sha256_id(&rendered), - "route preflight does not authorize this candidate membership" - ); - let prior = self.current_configuration()?; - let prior_sha256 = prior.as_ref().map(|bytes| sha256_id(bytes)); - let candidate_already_written = prior.as_deref() == Some(rendered.as_slice()); - ensure!( - candidate_already_written - || (prior == preflight.incumbent_configuration - && prior_sha256 == preflight.incumbent_membership_sha256), - "route baseline changed after preflight" - ); - if !candidate_already_written { - atomic_replace(&self.binding.config_path, &rendered)?; - } - if let Err(error) = self.reload() { - if rollback_allowed { - return self.fail_after_rollback( - preflight.incumbent_configuration.as_deref(), - error, - "candidate route validation or reload failed", - ); - } - return Err(error).context( - "candidate route reload failed after incumbent route authority was fenced; candidate fragment retained for retry", - ); - } - ensure!( - self.current_configuration()?.as_deref() == Some(rendered.as_slice()), - "candidate route fragment changed during reload" - ); - Ok(sha256_id(&rendered)) - } - - pub fn observe_membership( - &self, - expected: &IdunnExpectedIncarnationRecord, - membership_sha256: &str, - ) -> Result { - let rendered = self.render(expected)?; - ensure!( - membership_sha256 == sha256_id(&rendered), - "route observation does not describe the expected membership" - ); - Ok(self.current_configuration()?.as_deref() == Some(rendered.as_slice())) - } - - /// Restore the exact membership owned by an admitted generation. Any - /// different fragment is drift, not a rollback baseline, so it is never - /// restored after a failed reload. A failed reload removes the newly - /// written fragment so the next continuity pass cannot mistake disk bytes - /// for an adopted route. The caller must still challenge the stable - /// listener to prove that nginx workers adopted this membership. - pub fn restore_admitted_membership( - &self, - expected: &IdunnExpectedIncarnationRecord, - membership_sha256: &str, - ) -> Result<()> { - let rendered = self.render(expected)?; - ensure!( - membership_sha256 == sha256_id(&rendered), - "admitted route receipt does not describe the expected membership" - ); - self.validate_candidate_in_private_mount(&rendered)?; - if self.current_configuration()?.as_deref() != Some(rendered.as_slice()) { - atomic_replace(&self.binding.config_path, &rendered)?; - } - if let Err(reload) = self.reload() { - return match self.write_fragment(None) { - Ok(()) => Err(reload).context("reloading the exact admitted route membership"), - Err(cleanup) => Err(reload).context(format!( - "reloading the exact admitted route membership; removing its unproved fragment also failed: {cleanup:#}" - )), - }; - } - ensure!( - self.current_configuration()?.as_deref() == Some(rendered.as_slice()), - "admitted route fragment changed during restoration" - ); - Ok(()) - } - - /// Ask the stable listener for one freshly minted provider-owned presence - /// statement. This method validates the CultNet wire exchange and exact - /// document selection only; Idunn's control plane authenticates and - /// correlates the signed payload against current authority. - pub fn request_runtime_presence( - &self, - expected: &IdunnExpectedIncarnationRecord, - message_id: &str, - ) -> Result { - let route = expected - .route - .as_ref() - .context("route challenge has no Expected route")?; - ensure!( - route.route_id == self.binding.route_id - && route.stable_endpoint == self.binding.stable_endpoint, - "route challenge binding differs from Expected" - ); - self.request_runtime_presence_at(expected, message_id, &route.stable_endpoint) - } - - /// The bootstrap exception for the first managed Odin observes Warming on - /// the exact candidate endpoint. It does not install or admit a route. - pub fn request_candidate_runtime_presence( - &self, - expected: &IdunnExpectedIncarnationRecord, - message_id: &str, - ) -> Result { - let route = expected - .route - .as_ref() - .context("candidate challenge has no Expected route")?; - ensure!( - route.route_id == self.binding.route_id, - "candidate challenge binding differs from Expected" - ); - self.render(expected)?; - self.request_runtime_presence_at(expected, message_id, &route.candidate_endpoint) - } - - fn request_runtime_presence_at( - &self, - expected: &IdunnExpectedIncarnationRecord, - message_id: &str, - endpoint: &str, - ) -> Result { - expected.validate()?; - require_driver_id(message_id, "route challenge message")?; - let route = expected - .route - .as_ref() - .context("route challenge has no Expected route")?; - let request = CultNetMessage::SnapshotRequest { - message_id: message_id.to_owned(), - schema_ids: Some(vec![GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()]), - record_keys: Some(vec![expected.target.clone()]), - }; - let endpoint_prefix = match route.transport.as_str() { - "http" => "http://", - "tcp" => "tcp://", - "rudp" => "rudp://", - _ => bail!("route challenge transport is unsupported"), - }; - let (host, port) = endpoint_host_port(endpoint, endpoint_prefix)?; - let target = SocketAddr::new( - host.parse() - .context("route challenge endpoint host is not an IP address")?, - port, - ); - let response = match route.transport.as_str() { - "http" => { - let payload = - encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; - let response = request_http_snapshot(target, &payload)?; - decode_cultnet_message_from_slice(&response, CultNetWireContract::CultNetSchemaV0)? - } - "tcp" => { - let payload = - encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; - let response = request_tcp_snapshot(target, &payload)?; - decode_cultnet_message_from_slice(&response, CultNetWireContract::CultNetSchemaV0)? - } - "rudp" => request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { - target, - runtime_id: "idunn-route-observer".into(), - message_id: message_id.to_owned(), - schema_ids: Some(vec![GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA.into()]), - record_keys: Some(vec![expected.target.clone()]), - ..CultMeshRudpSnapshotOptions::default() - })?, - _ => bail!("route challenge transport is unsupported"), - }; - exact_route_snapshot_response(response, message_id, &expected.target) - } - - /// Restore the exact preflight baseline. This may only replace the exact - /// candidate authorized by the receipt; an unrelated writer is never - /// overwritten as a side effect of rollback. - pub fn rollback( - &self, - expected: &IdunnExpectedIncarnationRecord, - runtime_instance_id: &str, - preflight: &RoutePreflightReceipt, - ) -> Result<()> { - preflight.validate()?; - let rendered = self.render(expected)?; - ensure!( - preflight.route_id == self.binding.route_id - && preflight.candidate_runtime_instance_id == runtime_instance_id - && preflight.candidate_membership_sha256 == sha256_id(&rendered), - "route preflight does not authorize rollback of this candidate membership" - ); - ensure!( - self.current_configuration()?.as_deref() == Some(rendered.as_slice()), - "candidate route changed before rollback" - ); - self.restore(preflight.incumbent_configuration.as_deref()) - } -} - -fn request_tcp_snapshot(target: SocketAddr, payload: &[u8]) -> Result> { - let mut stream = TcpStream::connect_timeout(&target, ROUTE_SNAPSHOT_TIMEOUT) - .with_context(|| format!("connecting stable CultNet TCP route {target}"))?; - stream.set_read_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; - stream.set_write_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; - stream.write_all(&encode_frame(payload)?)?; - stream.flush()?; - - let mut header = [0_u8; 4]; - stream - .read_exact(&mut header) - .context("reading stable CultNet TCP response frame")?; - let length = u32::from_be_bytes(header) as usize; - ensure!( - (1..=ROUTE_SNAPSHOT_MAX_BYTES).contains(&length), - "stable CultNet TCP response exceeds the route observation bound" - ); - let mut response = vec![0_u8; length]; - stream - .read_exact(&mut response) - .context("reading stable CultNet TCP response payload")?; - Ok(response) -} - -fn request_http_snapshot(target: SocketAddr, payload: &[u8]) -> Result> { - let mut stream = TcpStream::connect_timeout(&target, ROUTE_SNAPSHOT_TIMEOUT) - .with_context(|| format!("connecting stable CultNet HTTP route {target}"))?; - stream.set_read_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; - stream.set_write_timeout(Some(ROUTE_SNAPSHOT_TIMEOUT))?; - let head = format!( - "POST {ROUTE_HTTP_SNAPSHOT_PATH} HTTP/1.1\r\nHost: {target}\r\nContent-Type: application/msgpack\r\nAccept: application/msgpack\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", - payload.len() - ); - stream.write_all(head.as_bytes())?; - stream.write_all(payload)?; - stream.flush()?; - - let maximum = ROUTE_HTTP_MAX_HEADER_BYTES - .checked_add(ROUTE_SNAPSHOT_MAX_BYTES) - .context("route HTTP response bound overflow")?; - let mut response = Vec::new(); - (&mut stream) - .take((maximum + 1) as u64) - .read_to_end(&mut response) - .context("reading stable CultNet HTTP response")?; - ensure!( - response.len() <= maximum, - "stable CultNet HTTP response exceeds the route observation bound" - ); - let header_end = response - .windows(4) - .position(|window| window == b"\r\n\r\n") - .map(|index| index + 4) - .context("stable CultNet HTTP response has no header boundary")?; - ensure!( - header_end <= ROUTE_HTTP_MAX_HEADER_BYTES, - "stable CultNet HTTP response headers exceed their bound" - ); - let headers = std::str::from_utf8(&response[..header_end]) - .context("stable CultNet HTTP response headers are not UTF-8")?; - let mut lines = headers.split("\r\n"); - let status = lines - .next() - .context("stable CultNet HTTP response has no status")?; - ensure!( - matches!(status, "HTTP/1.1 200 OK" | "HTTP/1.0 200 OK"), - "stable CultNet HTTP route rejected the snapshot challenge: {status}" - ); - let mut content_length = None; - for line in lines.filter(|line| !line.is_empty()) { - let (name, value) = line - .split_once(':') - .context("stable CultNet HTTP response contains a malformed header")?; - if name.eq_ignore_ascii_case("transfer-encoding") { - bail!("stable CultNet HTTP response uses unsupported transfer encoding"); - } - if name.eq_ignore_ascii_case("content-length") { - ensure!( - content_length.is_none(), - "stable CultNet HTTP response repeats Content-Length" - ); - content_length = Some( - value - .trim() - .parse::() - .context("stable CultNet HTTP Content-Length is invalid")?, - ); - } - } - let content_length = - content_length.context("stable CultNet HTTP response has no Content-Length")?; - ensure!( - content_length <= ROUTE_SNAPSHOT_MAX_BYTES - && response.len().saturating_sub(header_end) == content_length, - "stable CultNet HTTP response body differs from its bounded Content-Length" - ); - Ok(response[header_end..].to_vec()) -} - -fn exact_route_snapshot_response( - response: CultNetMessage, - message_id: &str, - target: &str, -) -> Result { - let CultNetMessage::SnapshotResponseRaw { - message_id: response_id, - documents, - } = response - else { - bail!("stable route returned a non-raw snapshot response") - }; - ensure!( - response_id == message_id, - "stable route snapshot response belongs to another challenge" - ); - ensure!( - documents.len() == 1, - "stable route snapshot response is not the exact singleton presence document" - ); - let document = documents - .into_iter() - .next() - .expect("singleton response length was checked"); - ensure!( - document.schema_id == GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA - && document.record_key == target - && document.payload_encoding == CultNetRawPayloadEncoding::Messagepack, - "stable route substituted the runtime presence document" - ); - ensure!( - !document.payload.is_empty() && document.payload.len() <= ROUTE_SNAPSHOT_MAX_BYTES, - "stable route presence payload is empty or exceeds its bound" - ); - Ok(RouteSnapshotResponse { - message_id: response_id, - canonical_presence: document.payload, - }) -} - -fn release_artifact<'a>( - declaration: &'a TargetDeclaration, - artifact_id: &str, -) -> Result<&'a ArtifactOutput> { - declaration - .artifacts - .iter() - .find(|artifact| artifact.id == artifact_id) - .with_context(|| format!("target declares no artifact {artifact_id}")) -} - -fn write_immutable_record(path: &Path, envelope: CultCacheEnvelope) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - let mut store = SingleFileMessagePackBackingStore::new(path); - match store.pull_all_read_only_snapshot()?.as_slice() { - [] => store.push(&envelope), - [current] if current == &envelope => Ok(()), - _ => bail!( - "immutable runtime document {} already differs", - path.display() - ), - } -} - -fn upsert_record(path: &Path, replacement: CultCacheEnvelope) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - let store = SingleFileMessagePackBackingStore::new(path); - for _ in 0..8 { - let entries = store.pull_all_read_only_snapshot()?; - let mut matches = entries - .iter() - .filter(|entry| entry.r#type == replacement.r#type && entry.key == replacement.key); - let current = matches.next().cloned(); - ensure!( - matches.next().is_none(), - "CultCache projection identity is ambiguous" - ); - if current.as_ref() == Some(&replacement) { - return Ok(()); - } - if store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: replacement.r#type.clone(), - key: replacement.key.clone(), - current, - }], - std::slice::from_ref(&replacement), - )? { - return Ok(()); - } - } - bail!("CultCache projection changed too often to publish") -} - -fn rfc3339_millis(millis: u64) -> Result { - Ok( - chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) - .context("runtime timestamp is out of range")? - .to_rfc3339(), - ) -} - -#[cfg(unix)] -fn validate_frozen_source_store(store: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!(store.is_absolute(), "frozen source store is not absolute"); - let canonical_store = store - .canonicalize() - .with_context(|| format!("resolving frozen source store {}", store.display()))?; - ensure!( - canonical_store == store, - "frozen source store traverses a symlink or noncanonical path" - ); - let metadata = fs::symlink_metadata(store)?; - ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.permissions().mode() & 0o022 == 0, - "frozen source store is not root-owned and nonwritable" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_frozen_source_store(_store: &Path) -> Result<()> { - bail!("frozen source materialization requires a Unix actuator") -} - -#[cfg(unix)] -fn prepare_frozen_transaction_root(store: &Path, transaction_id: &str) -> Result { - use std::os::unix::fs::PermissionsExt; - - require_driver_id(transaction_id, "source transaction")?; - validate_frozen_source_store(store)?; - let transaction_root = store.join(transaction_id); - if transaction_root.exists() { - remove_frozen_transaction_root(store, transaction_id)?; - } - fs::create_dir(&transaction_root)?; - fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o700))?; - Ok(transaction_root) -} - -#[cfg(not(unix))] -fn prepare_frozen_transaction_root(_store: &Path, _transaction_id: &str) -> Result { - bail!("frozen source materialization requires a Unix actuator") -} - -#[cfg(unix)] -fn remove_frozen_transaction_root(store: &Path, transaction_id: &str) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - require_driver_id(transaction_id, "source transaction")?; - validate_frozen_source_store(store)?; - let transaction_root = store.join(transaction_id); - let metadata = fs::symlink_metadata(&transaction_root)?; - ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.permissions().mode() & 0o022 == 0, - "frozen source transaction root is not Idunn-owned" - ); - fs::set_permissions(&transaction_root, fs::Permissions::from_mode(0o700))?; - fs::remove_dir_all(&transaction_root).with_context(|| { - format!( - "removing exact frozen source transaction {}", - transaction_root.display() - ) - }) -} - -#[cfg(not(unix))] -fn remove_frozen_transaction_root(_store: &Path, _transaction_id: &str) -> Result<()> { - bail!("frozen source cleanup requires a Unix actuator") -} - -#[cfg(unix)] -fn prepare_frozen_source_destination(destination: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "frozen source materialization requires root Idunn" - ); - ensure!( - destination.is_absolute(), - "frozen source destination is not absolute" - ); - let parent = destination - .parent() - .context("frozen source destination has no parent")?; - ensure!(destination != parent, "frozen source destination is broad"); - let canonical_parent = parent - .canonicalize() - .with_context(|| format!("resolving frozen source parent {}", parent.display()))?; - ensure!( - canonical_parent == parent, - "frozen source parent traverses a symlink or noncanonical path" - ); - let parent_metadata = fs::symlink_metadata(parent)?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "frozen source parent is not root-owned and nonwritable" - ); - if destination.exists() { - remove_frozen_source_destination(destination)?; - } - fs::create_dir(destination) - .with_context(|| format!("creating frozen source {}", destination.display()))?; - fs::set_permissions(destination, fs::Permissions::from_mode(0o700))?; - Ok(()) -} - -#[cfg(not(unix))] -fn prepare_frozen_source_destination(_destination: &Path) -> Result<()> { - bail!("frozen source materialization requires a Unix actuator") -} - -#[cfg(unix)] -fn remove_frozen_source_destination(destination: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - destination.is_absolute(), - "frozen source destination is not absolute" - ); - let parent = destination - .parent() - .context("frozen source destination has no parent")?; - ensure!(destination != parent, "frozen source destination is broad"); - let parent_metadata = fs::symlink_metadata(parent)?; - let metadata = fs::symlink_metadata(destination)?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "frozen source parent is not root-owned and nonwritable" - ); - ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.permissions().mode() & 0o022 == 0, - "existing frozen source is not an Idunn-owned directory" - ); - fs::remove_dir_all(destination) - .with_context(|| format!("removing exact frozen source {}", destination.display())) -} - -#[cfg(not(unix))] -fn remove_frozen_source_destination(_destination: &Path) -> Result<()> { - bail!("frozen source materialization requires a Unix actuator") -} - -#[cfg(unix)] -fn harden_frozen_source(root: &Path) -> Result<()> { - harden_frozen_source_tree(root, root) -} - -#[cfg(unix)] -fn harden_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(current)?; - ensure!(metadata.uid() == 0, "frozen source entry is not root-owned"); - ensure!( - current == root || current.file_name() != Some(OsStr::new(".git")), - "frozen source contains forbidden .git metadata" - ); - if metadata.is_dir() { - let mut entries = fs::read_dir(current)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - harden_frozen_source_tree(root, &entry.path())?; - } - fs::set_permissions(current, fs::Permissions::from_mode(0o555))?; - } else if metadata.is_file() { - let mode = if metadata.permissions().mode() & 0o111 == 0 { - 0o444 - } else { - 0o555 - }; - fs::set_permissions(current, fs::Permissions::from_mode(mode))?; - } else if metadata.file_type().is_symlink() { - validate_frozen_source_symlink(root, current)?; - } else { - bail!("frozen source contains a special filesystem entry") - } - Ok(()) -} - -#[cfg(unix)] -fn validate_frozen_source_symlink(root: &Path, path: &Path) -> Result<()> { - let target = fs::read_link(path)?; - ensure!(target.is_relative(), "frozen source symlink is absolute"); - let parent = path - .parent() - .context("frozen source symlink has no parent")?; - let mut components = parent - .strip_prefix(root)? - .components() - .filter_map(|component| match component { - std::path::Component::Normal(value) => Some(value.to_os_string()), - _ => None, - }) - .collect::>(); - for component in target.components() { - match component { - std::path::Component::CurDir => {} - std::path::Component::Normal(value) => components.push(value.to_os_string()), - std::path::Component::ParentDir => { - ensure!( - components.pop().is_some(), - "frozen source symlink escapes its root" - ); - } - std::path::Component::RootDir | std::path::Component::Prefix(_) => { - bail!("frozen source symlink is absolute") - } - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn harden_frozen_source(_root: &Path) -> Result<()> { - bail!("frozen source materialization requires Unix permissions") -} - -#[cfg(unix)] -fn validate_frozen_source(root: &Path) -> Result<()> { - validate_frozen_source_tree(root, root) -} - -#[cfg(unix)] -fn validate_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(current)?; - ensure!(metadata.uid() == 0, "frozen source entry is not root-owned"); - ensure!( - current == root || current.file_name() != Some(OsStr::new(".git")), - "frozen source contains forbidden .git metadata" - ); - if metadata.is_dir() { - ensure!( - metadata.permissions().mode() & 0o777 == 0o555, - "frozen source directory is not 0555" - ); - let mut entries = fs::read_dir(current)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - validate_frozen_source_tree(root, &entry.path())?; - } - } else if metadata.is_file() { - ensure!( - matches!(metadata.permissions().mode() & 0o777, 0o444 | 0o555), - "frozen source file has a noncanonical mode" - ); - } else if metadata.file_type().is_symlink() { - validate_frozen_source_symlink(root, current)?; - } else { - bail!("frozen source contains a special filesystem entry") - } - Ok(()) -} - -#[cfg(not(unix))] -fn validate_frozen_source(_root: &Path) -> Result<()> { - bail!("frozen source observation requires Unix permissions") -} - -#[cfg(unix)] -fn frozen_source_sha256(root: &Path) -> Result { - let mut hasher = Sha256::new(); - hash_frozen_source_tree(root, root, &mut hasher)?; - Ok(format!("sha256-{:x}", hasher.finalize())) -} - -#[cfg(unix)] -fn hash_frozen_source_tree(root: &Path, current: &Path, hasher: &mut Sha256) -> Result<()> { - use std::os::unix::fs::PermissionsExt; - - let mut entries = fs::read_dir(current)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - let path = entry.path(); - let relative = normalized_relative(path.strip_prefix(root)?)?; - let metadata = fs::symlink_metadata(&path)?; - if metadata.is_dir() { - hasher.update(b"dir\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hash_frozen_source_tree(root, &path, hasher)?; - } else if metadata.is_file() { - let bytes = fs::read(&path)?; - hasher.update(b"file\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hasher.update( - (metadata.permissions().mode() & 0o111 != 0) - .to_string() - .as_bytes(), - ); - hasher.update(b"\0"); - hasher.update((bytes.len() as u64).to_le_bytes()); - hasher.update(&bytes); - } else if metadata.file_type().is_symlink() { - let target = fs::read_link(&path)?; - hasher.update(b"link\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hasher.update(target.as_os_str().as_encoded_bytes()); - hasher.update(b"\0"); - } else { - bail!("frozen source contains a special filesystem entry") - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn frozen_source_sha256(_root: &Path) -> Result { - bail!("frozen source observation requires Unix permissions") -} - -#[cfg(unix)] -fn harden_installed_release(root: &Path, artifacts: &[ArtifactReceipt]) -> Result<()> { - let executable_paths = artifacts - .iter() - .filter(|artifact| artifact.executable) - .map(|artifact| root.join(&artifact.destination)) - .collect::>(); - harden_root_tree(root, root, &executable_paths) -} - -#[cfg(unix)] -fn harden_root_tree( - root: &Path, - current: &Path, - executable_paths: &std::collections::BTreeSet, -) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(current)?; - ensure!( - metadata.uid() == 0, - "sealed release entry is not root-owned" - ); - if metadata.is_dir() { - let mut entries = fs::read_dir(current)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - harden_root_tree(root, &entry.path(), executable_paths)?; - } - fs::set_permissions(current, fs::Permissions::from_mode(0o555))?; - } else if metadata.is_file() { - fs::set_permissions( - current, - fs::Permissions::from_mode(if executable_paths.contains(current) { - 0o555 - } else { - 0o444 - }), - )?; - } else if metadata.file_type().is_symlink() { - validate_release_symlink(root, current)?; - } else { - bail!("sealed release contains a special filesystem entry") - } - Ok(()) -} - -#[cfg(unix)] -fn validate_release_symlink(root: &Path, path: &Path) -> Result<()> { - let target = fs::read_link(path)?; - ensure!(target.is_relative(), "sealed release symlink is absolute"); - ensure!( - target.components().all(|component| matches!( - component, - std::path::Component::CurDir | std::path::Component::Normal(_) - )), - "sealed release symlink has a parent traversal" - ); - let destination = path - .parent() - .context("sealed release symlink has no parent")? - .join(target); - ensure!( - destination.starts_with(root), - "sealed release symlink escaped its release" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn harden_installed_release(_root: &Path, _artifacts: &[ArtifactReceipt]) -> Result<()> { - bail!("systemd workload installation requires Unix permissions") -} - -#[cfg(unix)] -fn harden_runtime_bundle(bundle: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(bundle)?; - let parent = bundle.parent().context("runtime bundle has no parent")?; - let parent_metadata = fs::symlink_metadata(parent)?; - ensure!( - metadata.is_dir() && !metadata.file_type().is_symlink() && metadata.uid() == 0, - "runtime bundle is not a root-owned directory" - ); - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "runtime bundle root is not root-owned and service-nonwritable" - ); - for name in [ - "expected.cc", - "expected.cc.lock", - "activation.cc", - "activation.cc.lock", - ] { - let path = bundle.join(name); - let metadata = fs::symlink_metadata(&path)?; - ensure!( - metadata.is_file() && !metadata.file_type().is_symlink() && metadata.uid() == 0, - "runtime bundle document is not root-owned" - ); - fs::set_permissions(path, fs::Permissions::from_mode(0o444))?; - } - fs::set_permissions(bundle, fs::Permissions::from_mode(0o555))?; - Ok(()) -} - -#[cfg(not(unix))] -fn harden_runtime_bundle(_bundle: &Path) -> Result<()> { - bail!("systemd runtime bundles require Unix permissions") -} - -fn observe_parent_only_file_descriptor( - fd_number: u32, - fd_name: &str, - source_path: &Path, -) -> Result { - validate_open_file_component(fd_name, "parent-only descriptor name")?; - ensure!( - source_path.is_absolute(), - "parent-only descriptor source is not absolute" - ); - validate_open_file_component( - &source_path.as_os_str().to_string_lossy(), - "parent-only descriptor source", - )?; - let mut file = open_native_read_only(source_path).with_context(|| { - format!( - "opening parent-only descriptor source {}", - source_path.display() - ) - })?; - let metadata = file.metadata()?; - ensure!( - metadata.is_file() && metadata.len() > 0, - "parent-only descriptor source is not one nonempty native file" - ); - #[cfg(unix)] - let (device, inode, uid, gid, mode, links) = { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - ( - metadata.dev(), - metadata.ino(), - metadata.uid(), - metadata.gid(), - metadata.permissions().mode() & 0o777, - metadata.nlink(), - ) - }; - #[cfg(not(unix))] - let (device, inode, uid, gid, mode, links) = (0, 0, 0, 0, 0, 0); - ensure!( - uid == 0 && gid == 0 && mode == 0o400 && links == 1, - "parent-only descriptor source is not root-owned, 0400, and singly linked" - ); - Ok(ParentOnlyFileDescriptorObservation { - fd_number, - fd_name: fd_name.to_owned(), - source_path: source_path.to_owned(), - access: "read-only".into(), - device, - inode, - uid, - gid, - mode, - links, - size: metadata.len(), - sha256: sha256_reader(&mut file)?, - }) -} - -fn parent_only_open_file_properties( - descriptors: &[ParentOnlyFileDescriptorObservation], -) -> Result> { - ensure!( - descriptors.len() == 2 - && descriptors[0].fd_number == 3 - && descriptors[0].fd_name == IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME - && descriptors[1].fd_number == 4 - && descriptors[1].fd_name == RUNTIME_PRESENCE_IDENTITY_FD_NAME, - "parent-only descriptor set is not the exact ordered activation/presence pair" - ); - descriptors - .iter() - .map(|descriptor| { - validate_open_file_component(&descriptor.fd_name, "parent-only descriptor name")?; - validate_open_file_component( - &descriptor.source_path.as_os_str().to_string_lossy(), - "parent-only descriptor source", - )?; - ensure!( - descriptor.source_path.is_absolute() - && descriptor.access == "read-only" - && descriptor.uid == 0 - && descriptor.gid == 0 - && descriptor.mode == 0o400 - && descriptor.links == 1 - && descriptor.size > 0, - "parent-only descriptor metadata is outside the Idunn contract" - ); - Ok(format!( - "{}:{}:{}", - descriptor.source_path.display(), - descriptor.fd_name, - descriptor.access - )) - }) - .collect() -} - -fn validate_open_file_component(value: &str, label: &str) -> Result<()> { - ensure!( - !value.is_empty() && !value.contains(':') && !value.chars().any(char::is_control), - "{label} cannot be represented by systemd OpenFile" - ); - Ok(()) -} - -#[cfg(unix)] -fn ensure_activation_credential_root(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "activation credential actuation requires root Idunn" - ); - ensure!( - path.is_absolute(), - "activation credential root is not absolute" - ); - let parent = path - .parent() - .context("activation credential root has no parent")?; - let parent_metadata = fs::symlink_metadata(parent).with_context(|| { - format!( - "inspecting activation credential parent {}", - parent.display() - ) - })?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0 - && parent.canonicalize()? == parent, - "activation credential parent is not a canonical root-owned directory" - ); - match fs::symlink_metadata(path) { - Ok(metadata) => ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == 0o700 - && path.canonicalize()? == path, - "activation credential root is not an exact root-only native directory" - ), - Err(error) if error.kind() == ErrorKind::NotFound => { - fs::create_dir(path).with_context(|| { - format!("creating activation credential root {}", path.display()) - })?; - fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; - let metadata = fs::symlink_metadata(path)?; - ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == 0o700, - "new activation credential root has the wrong authority" - ); - } - Err(error) => { - return Err(error).with_context(|| { - format!("inspecting activation credential root {}", path.display()) - }); - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn ensure_activation_credential_root(_path: &Path) -> Result<()> { - bail!("systemd activation credentials require a Unix authority path") -} - -#[cfg(unix)] -fn validate_activation_credential_source(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("inspecting activation credential source {}", path.display()))?; - ensure!( - metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == 0o400 - && metadata.nlink() == 1 - && metadata.len() == 32, - "activation credential source is not one exact root-owned 0400 32-byte file" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_activation_credential_source(_path: &Path) -> Result<()> { - bail!("systemd activation credentials require Unix file authority") -} - -#[cfg(unix)] -fn validate_service_credential_sources(sources: &BTreeMap) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - for (name, path) in sources { - ensure!( - path.is_absolute(), - "service credential {name} is not absolute" - ); - let parent = path - .parent() - .context("service credential source has no parent")?; - let parent_metadata = fs::symlink_metadata(parent).with_context(|| { - format!("inspecting service credential parent {}", parent.display()) - })?; - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("inspecting service credential source {}", path.display()))?; - ensure!( - parent.canonicalize()? == parent - && parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0 - && path.canonicalize()? == path.as_path() - && metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == 0o400 - && metadata.nlink() == 1 - && metadata.len() > 0, - "service credential {name} is not one canonical root-owned 0400 file" - ); - } - Ok(()) -} - -#[cfg(not(unix))] -fn validate_service_credential_sources(_sources: &BTreeMap) -> Result<()> { - bail!("systemd service credentials require Unix file authority") -} - -fn remove_activation_credential_source(path: &Path) -> Result<()> { - match fs::symlink_metadata(path) { - Ok(metadata) => { - validate_activation_credential_source_for_removal(path, &metadata)?; - fs::remove_file(path).with_context(|| { - format!("deleting activation credential source {}", path.display()) - })?; - sync_parent_directory(path)?; - ensure!( - matches!(fs::symlink_metadata(path), Err(error) if error.kind() == ErrorKind::NotFound), - "activation credential source remained after deletion" - ); - Ok(()) - } - Err(error) if error.kind() == ErrorKind::NotFound => Ok(()), - Err(error) => Err(error) - .with_context(|| format!("inspecting activation credential source {}", path.display())), - } -} - -#[cfg(unix)] -fn validate_activation_credential_source_for_removal( - path: &Path, - metadata: &fs::Metadata, -) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let parent = path - .parent() - .context("activation credential source has no parent")?; - let parent_metadata = fs::symlink_metadata(parent)?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.gid() == 0 - && parent_metadata.permissions().mode() & 0o777 == 0o700 - && metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.nlink() == 1 - && metadata.len() <= 32, - "refusing to delete a surprising activation credential source" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_activation_credential_source_for_removal( - _path: &Path, - _metadata: &fs::Metadata, -) -> Result<()> { - bail!("systemd activation credentials require Unix file authority") -} - -fn sync_parent_directory(path: &Path) -> Result<()> { - let parent = path.parent().context("file has no parent to synchronize")?; - let directory = open_native_read_only(parent)?; - directory - .sync_all() - .with_context(|| format!("synchronizing directory {}", parent.display())) -} - -fn open_native_read_only(path: &Path) -> Result { - let mut options = OpenOptions::new(); - options.read(true); - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt; - options.custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW); - } - Ok(options.open(path)?) -} - -fn open_proc_magic_link(path: &Path) -> Result { - let mut options = OpenOptions::new(); - options.read(true); - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt; - options.custom_flags(libc::O_CLOEXEC); - } - options - .open(path) - .with_context(|| format!("opening procfs executable {}", path.display())) -} - -fn sha256_reader(reader: &mut impl Read) -> Result { - let mut digest = Sha256::new(); - let mut buffer = [0_u8; 64 * 1024]; - loop { - let read = reader.read(&mut buffer)?; - if read == 0 { - break; - } - digest.update(&buffer[..read]); - } - Ok(format!("sha256-{:x}", digest.finalize())) -} - -fn path_inside_process_root(process_root: &Path, absolute_path: &Path) -> Result { - ensure!( - process_root.is_absolute() && absolute_path.is_absolute(), - "process-root projection requires absolute paths" - ); - ensure!( - absolute_path.components().all(|component| matches!( - component, - std::path::Component::RootDir | std::path::Component::Normal(_) - )), - "process-root projection path is not normalized" - ); - Ok(process_root.join(absolute_path.strip_prefix(Path::new("/"))?)) -} - -fn linux_process_security( - status_path: &Path, - expected_host_pid: u32, -) -> Result { - let status = fs::read_to_string(status_path) - .with_context(|| format!("reading process status {}", status_path.display()))?; - let uids = linux_status_u32_array(&status, "Uid")?; - let gids = linux_status_u32_array(&status, "Gid")?; - let groups = linux_status_u32_values(&status, "Groups")?; - let cap_inheritable = linux_status_hex_u64(&status, "CapInh")?; - let cap_permitted = linux_status_hex_u64(&status, "CapPrm")?; - let cap_effective = linux_status_hex_u64(&status, "CapEff")?; - let cap_bounding = linux_status_hex_u64(&status, "CapBnd")?; - let cap_ambient = linux_status_hex_u64(&status, "CapAmb")?; - let no_new_privileges = match linux_status_value(&status, "NoNewPrivs")? { - "1" => true, - "0" => false, - _ => bail!("process NoNewPrivs value is invalid"), - }; - let namespace_pids = linux_status_u32_values(&status, "NSpid")?; - ensure!( - namespace_pids.first() == Some(&expected_host_pid), - "process status belongs to another host pid" - ); - Ok(LinuxProcessSecurityObservation { - uids, - gids, - groups, - cap_inheritable, - cap_permitted, - cap_effective, - cap_bounding, - cap_ambient, - no_new_privileges, - namespace_pids, - }) -} - -fn linux_status_value<'a>(status: &'a str, name: &str) -> Result<&'a str> { - let prefix = format!("{name}:"); - let matches = status - .lines() - .filter_map(|line| line.strip_prefix(&prefix)) - .collect::>(); - ensure!( - matches.len() == 1, - "process status has zero or duplicate {name} fields" - ); - Ok(matches[0].trim()) -} - -fn linux_status_u32_values(status: &str, name: &str) -> Result> { - linux_status_value(status, name)? - .split_whitespace() - .map(|value| { - value - .parse::() - .with_context(|| format!("process status {name} value is not a u32")) - }) - .collect() -} - -fn linux_status_u32_array(status: &str, name: &str) -> Result<[u32; 4]> { - linux_status_u32_values(status, name)? - .try_into() - .map_err(|_| anyhow::anyhow!("process status {name} does not have four values")) -} - -fn linux_status_hex_u64(status: &str, name: &str) -> Result { - u64::from_str_radix(linux_status_value(status, name)?, 16) - .with_context(|| format!("process status {name} value is not hexadecimal")) -} - -fn linux_namespace_id(path: &Path, kind: &str) -> Result { - let target = fs::read_link(path) - .with_context(|| format!("reading Linux namespace link {}", path.display()))?; - let target = target - .to_str() - .context("Linux namespace link is not UTF-8")?; - let prefix = format!("{kind}:["); - let id = target - .strip_prefix(&prefix) - .and_then(|value| value.strip_suffix(']')) - .context("Linux namespace link has an unexpected shape")?; - let id = id - .parse::() - .context("Linux namespace id is not a u64")?; - ensure!(id > 0, "Linux namespace id is zero"); - Ok(id) -} - -#[cfg(unix)] -fn resolve_group_id(group: &str) -> Result { - use std::ffi::CString; - - if let Ok(group_id) = group.parse::() { - ensure!(group_id > 0, "state group must be unprivileged"); - return Ok(group_id); - } - let group = CString::new(group).context("state group contains a NUL byte")?; - let mut buffer_size = 16_384_usize; - loop { - let mut entry = std::mem::MaybeUninit::::uninit(); - let mut result = std::ptr::null_mut(); - let mut buffer = vec![0_u8; buffer_size]; - let status = unsafe { - libc::getgrnam_r( - group.as_ptr(), - entry.as_mut_ptr(), - buffer.as_mut_ptr().cast(), - buffer.len(), - &mut result, - ) - }; - if status == libc::ERANGE && buffer_size < 1_048_576 { - buffer_size *= 2; - continue; - } - ensure!( - status == 0, - "resolving state group failed with errno {status}" - ); - ensure!(!result.is_null(), "configured state group does not exist"); - let entry = unsafe { entry.assume_init() }; - ensure!(entry.gr_gid > 0, "state group must be unprivileged"); - return Ok(entry.gr_gid); - } -} - -#[cfg(not(unix))] -fn resolve_group_id(_group: &str) -> Result { - bail!("systemd workload groups require a Unix actuator") -} - -#[cfg(unix)] -fn validate_workload_writable_path( - path: &Path, - state_group_id: u32, - require_setgid: bool, -) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("inspecting workload writable path {}", path.display()))?; - let mode = metadata.permissions().mode(); - ensure!( - !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == state_group_id - && mode & 0o002 == 0, - "workload writable path {} is not preprovisioned root:state-group without world write", - path.display() - ); - if metadata.is_dir() { - ensure!( - mode & 0o070 == 0o070 && (!require_setgid || mode & 0o2000 != 0), - "workload writable directory {} lacks group access or setgid inheritance", - path.display() - ); - } else { - ensure!( - metadata.is_file() && mode & 0o060 == 0o060, - "workload writable file is special or lacks group access" - ); - } - Ok(()) -} - -#[cfg(not(unix))] -fn validate_workload_writable_path( - _path: &Path, - _state_group_id: u32, - _require_setgid: bool, -) -> Result<()> { - bail!("systemd workload writable paths require Unix permissions") -} - -fn linux_process_start_time(stat_path: &Path) -> Result { - let stat = fs::read_to_string(stat_path) - .with_context(|| format!("reading process stat {}", stat_path.display()))?; - let close = stat - .rfind(')') - .context("process stat has no command terminator")?; - let fields = stat[close + 1..].split_whitespace().collect::>(); - fields - .get(19) - .context("process stat has no start-time field")? - .parse() - .context("process start-time field is not a u64") -} - -fn required_systemd_property<'a>( - values: &'a BTreeMap, - name: &str, -) -> Result<&'a str> { - let value = systemd_property(values, name)?; - ensure!(!value.is_empty(), "systemd unit has no {name}"); - Ok(value) -} - -fn systemd_property<'a>(values: &'a BTreeMap, name: &str) -> Result<&'a str> { - values - .get(name) - .map(String::as_str) - .with_context(|| format!("systemd unit did not expose {name}")) -} - -fn parse_systemd_boolean(values: &BTreeMap, name: &str) -> Result { - match systemd_property(values, name)? { - "yes" => Ok(true), - "no" => Ok(false), - _ => bail!("systemd unit exposed a non-boolean {name}"), - } -} - -fn read_process_environment(path: &Path) -> Result>> { - let bytes = fs::read(path) - .with_context(|| format!("reading process environment {}", path.display()))?; - Ok(bytes - .split(|byte| *byte == 0) - .filter(|entry| !entry.is_empty()) - .map(<[u8]>::to_vec) - .collect()) -} - -fn required_process_environment_value<'a>(entries: &'a [Vec], name: &str) -> Result<&'a str> { - let prefix = format!("{name}=").into_bytes(); - let matches = entries - .iter() - .filter_map(|entry| entry.strip_prefix(prefix.as_slice())) - .collect::>(); - ensure!( - matches.len() == 1, - "process environment has zero or duplicate {name} values" - ); - let value = std::str::from_utf8(matches[0]) - .with_context(|| format!("process environment {name} is not UTF-8"))?; - ensure!(!value.is_empty(), "process environment {name} is empty"); - Ok(value) -} - -fn select_process_environment( - entries: &[Vec], - names: &[String], -) -> Result> { - ensure!( - names.windows(2).all(|pair| pair[0] < pair[1]), - "workload environment witness names are not unique and sorted" - ); - let mut selected = BTreeMap::new(); - for name in names { - let prefix = format!("{name}=").into_bytes(); - let matches = entries - .iter() - .filter(|entry| entry.starts_with(&prefix)) - .collect::>(); - ensure!( - matches.len() == 1, - "workload process environment has missing or duplicate {name}" - ); - let value = std::str::from_utf8(&matches[0][prefix.len()..]) - .with_context(|| format!("workload environment {name} is not UTF-8"))?; - selected.insert(name.clone(), value.to_owned()); - } - Ok(selected) -} - -fn proc_command_line(command: &[OsString]) -> Vec { - let mut encoded = Vec::new(); - for argument in command { - encoded.extend_from_slice(argument.as_encoded_bytes()); - encoded.push(0); - } - encoded -} - -fn endpoint_host_port(endpoint: &str, scheme: &str) -> Result<(String, u16)> { - let authority = endpoint - .strip_prefix(scheme) - .with_context(|| format!("endpoint does not use {scheme}"))?; - ensure!( - !authority - .chars() - .any(|character| matches!(character, '/' | '?' | '#' | '@')) - && !authority.starts_with('['), - "route endpoint is not a plain host and port" - ); - let (host, port) = authority - .rsplit_once(':') - .context("route endpoint has no port")?; - ensure!( - !host.is_empty() - && host - .bytes() - .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-')), - "route endpoint host is invalid" - ); - let port: u16 = port.parse().context("route endpoint port is not a u16")?; - ensure!(port > 0, "route endpoint port is zero"); - Ok((host.to_owned(), port)) -} - -fn nginx_identifier(route_id: &str) -> Result { - ensure!(!route_id.is_empty(), "nginx route id is empty"); - let mut identifier = String::from("idunn_"); - for byte in route_id.bytes() { - match byte { - b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => { - identifier.push(char::from(byte)); - } - b'-' | b'.' => identifier.push('_'), - _ => bail!("route id cannot lower to an nginx identifier"), - } - } - Ok(identifier) -} - -fn atomic_replace(path: &Path, bytes: &[u8]) -> Result<()> { - #[cfg(unix)] - use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; - - ensure!(path.is_absolute(), "route path is not absolute"); - let parent = path.parent().context("route path has no parent")?; - ensure_route_authority_parent(parent)?; - match fs::symlink_metadata(path) { - Ok(_) => validate_root_owned_file(path, 0o644)?, - Err(error) if error.kind() == ErrorKind::NotFound => {} - Err(error) => return Err(error).context("inspecting route fragment"), - } - let file_name = path - .file_name() - .context("route path has no file name")? - .to_string_lossy(); - let temporary = parent.join(format!(".{file_name}.idunn-{}", Uuid::new_v4())); - let mut options = fs::OpenOptions::new(); - options.create_new(true).write(true); - #[cfg(unix)] - options.mode(0o644); - let mut file = options - .open(&temporary) - .with_context(|| format!("creating route stage {}", temporary.display()))?; - #[cfg(unix)] - file.set_permissions(fs::Permissions::from_mode(0o644))?; - file.write_all(bytes)?; - file.sync_all()?; - drop(file); - validate_root_owned_file(&temporary, 0o644)?; - if let Err(error) = fs::rename(&temporary, path) { - let _ = fs::remove_file(&temporary); - return Err(error).with_context(|| format!("publishing route {}", path.display())); - } - sync_parent_directory(path)?; - validate_root_owned_file(path, 0o644) -} - -#[cfg(unix)] -fn ensure_route_authority_parent(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "route actuation requires root Idunn" - ); - ensure!(path.is_absolute(), "route authority parent is not absolute"); - if !path.exists() { - let ancestor = path - .parent() - .context("route authority parent has no ancestor")?; - let ancestor_metadata = fs::symlink_metadata(ancestor)?; - ensure!( - ancestor.canonicalize()? == ancestor - && ancestor_metadata.is_dir() - && !ancestor_metadata.file_type().is_symlink() - && ancestor_metadata.uid() == 0 - && ancestor_metadata.permissions().mode() & 0o022 == 0, - "route authority ancestor is not canonical root-owned and nonwritable" - ); - fs::create_dir(path)?; - fs::set_permissions(path, fs::Permissions::from_mode(0o755))?; - } - let metadata = fs::symlink_metadata(path)?; - ensure!( - path.canonicalize()? == path - && metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o022 == 0, - "route authority parent is not canonical root-owned and nonwritable" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn ensure_route_authority_parent(_path: &Path) -> Result<()> { - bail!("nginx route actuation requires Unix file authority") -} - -#[cfg(unix)] -fn ensure_route_preflight_root(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!(path.is_absolute(), "route preflight root is not absolute"); - if !path.exists() { - let parent = path - .parent() - .context("route preflight root has no parent")?; - ensure_route_authority_parent(parent)?; - fs::create_dir(path)?; - fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; - } - let metadata = fs::symlink_metadata(path)?; - ensure!( - unsafe { libc::geteuid() } == 0 - && path.canonicalize()? == path - && metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == 0o700, - "route preflight root is not one canonical root-only directory" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn ensure_route_preflight_root(_path: &Path) -> Result<()> { - bail!("nginx route preflight requires Unix file authority") -} - -#[cfg(unix)] -fn write_root_owned_file(path: &Path, bytes: &[u8], mode: u32) -> Result<()> { - use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; - - let mut options = OpenOptions::new(); - options.create_new(true).write(true).mode(mode); - let mut file = options.open(path)?; - file.set_permissions(fs::Permissions::from_mode(mode))?; - file.write_all(bytes)?; - file.sync_all()?; - drop(file); - sync_parent_directory(path)?; - validate_root_owned_file(path, mode) -} - -#[cfg(not(unix))] -fn write_root_owned_file(_path: &Path, _bytes: &[u8], _mode: u32) -> Result<()> { - bail!("root-owned route material requires Unix file authority") -} - -#[cfg(unix)] -fn validate_root_owned_file(path: &Path, mode: u32) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let metadata = fs::symlink_metadata(path)?; - ensure!( - path.canonicalize()? == path - && metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == 0 - && metadata.permissions().mode() & 0o777 == mode - && metadata.nlink() == 1, - "route authority material is not one canonical root-owned file" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_root_owned_file(_path: &Path, _mode: u32) -> Result<()> { - bail!("root-owned route material requires Unix file authority") -} - -fn remove_exact_root_owned_file(path: &Path, mode: u32) -> Result<()> { - validate_root_owned_file(path, mode)?; - fs::remove_file(path)?; - sync_parent_directory(path) -} - -fn bind_mount(source: &Path, destination: &str, read_only: bool) -> Result { - ensure!(source.is_absolute(), "Docker bind source is not absolute"); - let text = source.to_str().context("Docker bind source is not UTF-8")?; - ensure!( - !text - .chars() - .any(|character| matches!(character, ',' | '\n' | '\r')), - "Docker bind source contains a forbidden mount character" - ); - let read_only = if read_only { ",readonly" } else { "" }; - Ok(OsString::from(format!( - "type=bind,src={text},dst={destination}{read_only}" - ))) -} - -fn systemd_read_only_bind_property(source: &Path, destination: &Path) -> Result { - ensure!( - source.is_absolute() && destination.is_absolute(), - "systemd bind paths are not absolute" - ); - let source = source - .to_str() - .context("systemd bind source is not UTF-8")?; - let destination = destination - .to_str() - .context("systemd bind destination is not UTF-8")?; - ensure!( - [source, destination] - .iter() - .all(|path| !path.contains([':', '\n', '\r', '\0'])), - "systemd bind path contains an unescaped property delimiter" - ); - Ok(OsString::from(format!( - "--property=BindReadOnlyPaths={source}:{destination}" - ))) -} - -fn normalized_relative(path: &Path) -> Result { - ensure!(path.is_relative(), "runner path is not relative"); - let mut parts = Vec::new(); - for component in path.components() { - match component { - std::path::Component::CurDir => {} - std::path::Component::Normal(value) => parts.push( - value - .to_str() - .context("runner path contains non-UTF-8")? - .to_owned(), - ), - _ => bail!("runner path escapes its workspace"), - } - } - Ok(parts.join("/")) -} - -fn copy_tree(source: &Path, destination: &Path) -> Result<()> { - ensure!(source.is_dir(), "source tree is not a directory"); - ensure!(!destination.exists(), "destination tree already exists"); - fs::create_dir_all(destination)?; - copy_tree_contents(source, destination) -} - -fn copy_tree_contents(source: &Path, destination: &Path) -> Result<()> { - let mut entries = fs::read_dir(source)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - let source_path = entry.path(); - let destination_path = destination.join(entry.file_name()); - let metadata = fs::symlink_metadata(&source_path)?; - if metadata.is_dir() { - fs::create_dir(&destination_path)?; - copy_tree_contents(&source_path, &destination_path)?; - } else if metadata.is_file() { - fs::copy(&source_path, &destination_path)?; - } else if metadata.file_type().is_symlink() { - copy_symlink(&source_path, &destination_path)?; - } else { - bail!("source tree contains a special filesystem entry") - } - } - Ok(()) -} - -#[cfg(unix)] -fn copy_symlink(source: &Path, destination: &Path) -> Result<()> { - std::os::unix::fs::symlink(fs::read_link(source)?, destination)?; - Ok(()) -} - -#[cfg(windows)] -fn copy_symlink(source: &Path, destination: &Path) -> Result<()> { - let target = fs::read_link(source)?; - if source.is_dir() { - std::os::windows::fs::symlink_dir(target, destination)?; - } else { - std::os::windows::fs::symlink_file(target, destination)?; - } - Ok(()) -} - -fn copy_artifact(source: &Path, destination: &Path) -> Result<()> { - let metadata = fs::symlink_metadata(source)?; - if metadata.is_dir() { - copy_tree(source, destination) - } else if metadata.is_file() { - if let Some(parent) = destination.parent() { - fs::create_dir_all(parent)?; - } - fs::copy(source, destination)?; - Ok(()) - } else { - bail!("artifact output is not a regular file or directory") - } -} - -fn digest_artifact(path: &Path) -> Result<(String, u64)> { - let metadata = fs::symlink_metadata(path)?; - if metadata.is_file() { - let bytes = fs::read(path)?; - return Ok((raw_sha256(&bytes), bytes.len().try_into()?)); - } - ensure!(metadata.is_dir(), "artifact is not a file or directory"); - let mut hasher = Sha256::new(); - let mut size = 0_u64; - digest_tree(path, path, &mut hasher, &mut size)?; - ensure!(size > 0, "artifact directory has no file content"); - Ok((format!("{:x}", hasher.finalize()), size)) -} - -fn digest_tree(root: &Path, current: &Path, hasher: &mut Sha256, size: &mut u64) -> Result<()> { - let mut entries = fs::read_dir(current)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - let path = entry.path(); - let relative = path.strip_prefix(root)?; - let relative = normalized_relative(relative)?; - let metadata = fs::symlink_metadata(&path)?; - if metadata.is_dir() { - hasher.update(b"dir\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - digest_tree(root, &path, hasher, size)?; - } else if metadata.is_file() { - let bytes = fs::read(&path)?; - hasher.update(b"file\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hasher.update((bytes.len() as u64).to_le_bytes()); - hasher.update(&bytes); - *size = size.saturating_add(bytes.len().try_into()?); - } else if metadata.file_type().is_symlink() { - let target = fs::read_link(&path)?; - hasher.update(b"link\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hasher.update(target.as_os_str().as_encoded_bytes()); - *size = size.saturating_add(target.as_os_str().len().try_into()?); - } else { - bail!("artifact tree contains a special filesystem entry") - } - } - Ok(()) -} - -fn raw_sha256(bytes: &[u8]) -> String { - format!("{:x}", Sha256::digest(bytes)) -} - -fn remove_tree_inside(root: &Path, target: &Path) -> Result<()> { - let root = root.canonicalize()?; - let target = target.canonicalize()?; - ensure!( - target.starts_with(&root) && target != root, - "refusing broad tree removal" - ); - fs::remove_dir_all(&target) - .with_context(|| format!("removing disposable runner workspace {}", target.display())) -} - -fn sha256_id(bytes: &[u8]) -> String { - format!("sha256-{:x}", Sha256::digest(bytes)) -} - -fn require_git_sha(value: &str, label: &str) -> Result<()> { - ensure!( - value.len() == 40 - && value - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), - "{label} is not a lowercase SHA-1 object id" - ); - Ok(()) -} - -fn require_sha256_id(value: &str, label: &str) -> Result<()> { - let digest = value - .strip_prefix("sha256-") - .with_context(|| format!("{label} has no sha256 prefix"))?; - ensure!( - digest.len() == 64 - && digest - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)), - "{label} is not a lowercase SHA-256 id" - ); - Ok(()) -} - -fn require_driver_id(value: &str, label: &str) -> Result<()> { - ensure!( - !value.is_empty() - && value.len() <= 128 - && value - .bytes() - .all(|byte| { byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-') }), - "{label} id is invalid" - ); - Ok(()) -} - -fn ensure_source_directory_tree( - root: &Path, - directory: &Path, - identity: Option, -) -> Result<()> { - ensure!( - directory.starts_with(root), - "source directory escaped Idunn's source authority root" - ); - ensure_source_directory(root, identity)?; - let relative = directory.strip_prefix(root)?; - let mut current = root.to_owned(); - for component in relative.components() { - let std::path::Component::Normal(component) = component else { - bail!("source directory is not normalized") - }; - current.push(component); - ensure_source_directory(¤t, identity)?; - } - Ok(()) -} - -fn container_identity(value: &str) -> Result { - let (uid, gid) = value - .split_once(':') - .context("runner user is not numeric UID:GID")?; - let identity = ProcessIdentity { - uid: uid.parse().context("runner UID is not a u32")?, - gid: gid.parse().context("runner GID is not a u32")?, - }; - ensure!( - identity.uid > 0 && identity.gid > 0, - "runner identity must be unprivileged" - ); - Ok(identity) -} - -#[cfg(unix)] -fn ensure_runner_cache_root(path: &Path, identity: ProcessIdentity) -> Result<()> { - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "runner cache admission requires root Idunn" - ); - let parent = path.parent().context("runner cache root has no parent")?; - let parent_metadata = fs::symlink_metadata(parent) - .with_context(|| format!("inspecting runner cache parent {}", parent.display()))?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "runner cache parent is not root-owned and nonwritable" - ); - match fs::symlink_metadata(path) { - Ok(metadata) => ensure!( - metadata.is_dir() - && !metadata.file_type().is_symlink() - && metadata.uid() == identity.uid - && metadata.gid() == identity.gid - && metadata.permissions().mode() & 0o777 == 0o700, - "runner cache is not a dedicated exact-identity 0700 directory" - ), - Err(error) if error.kind() == ErrorKind::NotFound => { - fs::create_dir(path) - .with_context(|| format!("creating runner cache {}", path.display()))?; - let path_c = std::ffi::CString::new(path.as_os_str().as_bytes()) - .context("runner cache path contains a NUL byte")?; - if unsafe { libc::lchown(path_c.as_ptr(), identity.uid, identity.gid) } != 0 { - return Err(std::io::Error::last_os_error()) - .context("assigning runner cache owner"); - } - fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; - } - Err(error) => { - return Err(error) - .with_context(|| format!("inspecting runner cache {}", path.display())); - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn ensure_runner_cache_root(_path: &Path, _identity: ProcessIdentity) -> Result<()> { - bail!("runner cache admission requires a Unix actuator") -} - -#[cfg(unix)] -fn validate_runner_secret(path: &Path, identity: ProcessIdentity) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "runner secret admission requires root Idunn" - ); - let parent = path.parent().context("runner secret has no parent")?; - let parent_metadata = fs::symlink_metadata(parent) - .with_context(|| format!("inspecting runner secret parent {}", parent.display()))?; - let metadata = fs::symlink_metadata(path) - .with_context(|| format!("inspecting runner secret {}", path.display()))?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "runner secret parent is not root-owned and nonwritable" - ); - ensure!( - metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == identity.gid - && metadata.permissions().mode() & 0o777 == 0o440, - "runner secret is not root-owned, exact-group-bound, and 0440" - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_runner_secret(_path: &Path, _identity: ProcessIdentity) -> Result<()> { - bail!("runner secret admission requires a Unix actuator") -} - -#[cfg(unix)] -fn assign_runner_tree(path: &Path, identity: ProcessIdentity) -> Result<()> { - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::PermissionsExt; - - ensure!( - unsafe { libc::geteuid() } == 0, - "runner workspace ownership requires root Idunn" - ); - let metadata = fs::symlink_metadata(path)?; - if metadata.is_dir() { - let mut entries = fs::read_dir(path)?.collect::>>()?; - entries.sort_by_key(|entry| entry.file_name()); - for entry in entries { - assign_runner_tree(&entry.path(), identity)?; - } - fs::set_permissions(path, fs::Permissions::from_mode(0o750))?; - } else if metadata.is_file() { - let executable = metadata.permissions().mode() & 0o111 != 0; - fs::set_permissions( - path, - fs::Permissions::from_mode(if executable { 0o750 } else { 0o640 }), - )?; - } else if !metadata.file_type().is_symlink() { - bail!("runner workspace contains a special filesystem entry") - } - let path_c = std::ffi::CString::new(path.as_os_str().as_bytes()) - .context("runner workspace path contains a NUL byte")?; - let result = unsafe { libc::lchown(path_c.as_ptr(), identity.uid, identity.gid) }; - if result != 0 { - return Err(std::io::Error::last_os_error()).context("assigning runner workspace owner"); - } - Ok(()) -} - -#[cfg(not(unix))] -fn assign_runner_tree(_path: &Path, _identity: ProcessIdentity) -> Result<()> { - bail!("non-root runner identities require a Unix actuator") -} - -fn ensure_source_directory(path: &Path, identity: Option) -> Result<()> { - match fs::symlink_metadata(path) { - Ok(metadata) => { - ensure!( - metadata.is_dir() && !metadata.file_type().is_symlink(), - "source authority path {} is not a native directory", - path.display() - ); - validate_source_directory_owner(path, &metadata, identity) - } - Err(error) if error.kind() == ErrorKind::NotFound => { - let parent = path - .parent() - .context("source authority directory has no parent")?; - ensure!( - parent.is_dir(), - "source authority parent {} is absent", - parent.display() - ); - fs::create_dir(path).with_context(|| { - format!("creating source authority directory {}", path.display()) - })?; - assign_source_directory_owner(path, identity)?; - let metadata = fs::symlink_metadata(path)?; - validate_source_directory_owner(path, &metadata, identity) - } - Err(error) => Err(error) - .with_context(|| format!("inspecting source authority path {}", path.display())), - } -} - -fn authority_lock_path(path: &Path) -> PathBuf { - let mut lock = path.as_os_str().to_os_string(); - lock.push(".lock"); - PathBuf::from(lock) -} - -#[cfg(unix)] -fn validate_root_authority_path(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - ensure!( - unsafe { libc::geteuid() } == 0, - "process write-lease actuation requires root Idunn" - ); - let parent = path - .parent() - .context("process write-lease path has no parent")?; - let parent_metadata = fs::symlink_metadata(parent) - .with_context(|| format!("inspecting write-lease parent {}", parent.display()))?; - ensure!( - parent_metadata.is_dir() - && !parent_metadata.file_type().is_symlink() - && parent_metadata.uid() == 0 - && parent_metadata.permissions().mode() & 0o022 == 0, - "write-lease parent is not root-owned and service-nonwritable" - ); - for authority_file in [path.to_owned(), authority_lock_path(path)] { - match fs::symlink_metadata(&authority_file) { - Ok(metadata) => ensure!( - metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == parent_metadata.gid() - && metadata.permissions().mode() & 0o022 == 0, - "write-lease authority file {} is not root-owned and service-nonwritable", - authority_file.display() - ), - Err(error) if error.kind() == ErrorKind::NotFound => {} - Err(error) => { - return Err(error).with_context(|| { - format!( - "inspecting write-lease authority file {}", - authority_file.display() - ) - }); - } - } - } - Ok(()) -} - -#[cfg(not(unix))] -fn validate_root_authority_path(_path: &Path) -> Result<()> { - bail!("process write-lease actuation requires a Unix authority path") -} - -#[cfg(unix)] -fn harden_root_authority_file(path: &Path) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let parent = path - .parent() - .context("process write-lease authority file has no parent")?; - let parent_metadata = fs::symlink_metadata(parent)?; - let metadata = fs::symlink_metadata(path)?; - ensure!( - metadata.is_file() - && !metadata.file_type().is_symlink() - && metadata.uid() == 0 - && metadata.gid() == parent_metadata.gid(), - "new write-lease authority file has the wrong owner" - ); - fs::set_permissions(path, fs::Permissions::from_mode(0o640))?; - Ok(()) -} - -#[cfg(not(unix))] -fn harden_root_authority_file(_path: &Path) -> Result<()> { - bail!("process write-lease actuation requires Unix permissions") -} - -#[cfg(unix)] -fn assign_source_directory_owner(path: &Path, identity: Option) -> Result<()> { - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::PermissionsExt; - - if let Some(identity) = identity { - let path = std::ffi::CString::new(path.as_os_str().as_bytes()) - .context("source authority path contains a NUL byte")?; - let result = unsafe { libc::chown(path.as_ptr(), identity.uid, identity.gid) }; - if result != 0 { - return Err(std::io::Error::last_os_error()) - .context("assigning source authority directory owner"); - } - } - fs::set_permissions(path, fs::Permissions::from_mode(0o750))?; - Ok(()) -} - -#[cfg(not(unix))] -fn assign_source_directory_owner(_path: &Path, identity: Option) -> Result<()> { - ensure!( - identity.is_none(), - "configured source identities require a Unix actuator" - ); - Ok(()) -} - -#[cfg(unix)] -fn validate_source_directory_owner( - path: &Path, - metadata: &fs::Metadata, - identity: Option, -) -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let expected = identity.unwrap_or(ProcessIdentity { - uid: unsafe { libc::geteuid() }, - gid: unsafe { libc::getegid() }, - }); - ensure!( - metadata.uid() == expected.uid && metadata.gid() == expected.gid, - "source authority directory {} has the wrong owner", - path.display() - ); - ensure!( - metadata.permissions().mode() & 0o200 != 0, - "source authority directory {} is not owner-writable", - path.display() - ); - Ok(()) -} - -#[cfg(not(unix))] -fn validate_source_directory_owner( - _path: &Path, - _metadata: &fs::Metadata, - identity: Option, -) -> Result<()> { - ensure!( - identity.is_none(), - "configured source identities require a Unix actuator" - ); - Ok(()) -} - -fn apply_identity(command: &mut Command, identity: Option) -> Result<()> { - let Some(identity) = identity else { - return Ok(()); - }; - #[cfg(unix)] - { - use std::os::unix::process::CommandExt; - ensure!( - identity.uid > 0 && identity.gid > 0, - "configured source identity must be unprivileged" - ); - unsafe { - command.pre_exec(move || { - if libc::setgroups(0, std::ptr::null()) != 0 { - return Err(std::io::Error::last_os_error()); - } - if libc::setgid(identity.gid) != 0 { - return Err(std::io::Error::last_os_error()); - } - if libc::setuid(identity.uid) != 0 { - return Err(std::io::Error::last_os_error()); - } - Ok(()) - }); - } - Ok(()) - } - #[cfg(not(unix))] - { - let _ = command; - let _ = identity; - bail!("configured process identities require a Unix actuator") - } -} - -#[cfg(test)] -mod tests { - use super::*; - use cultnet_rs::{ - GameCultProviderHealthIdentity, IDUNN_EXPECTED_INCARNATION_SCHEMA, - IDUNN_PROCESS_WRITE_LEASE_SCHEMA, IdunnServiceIdentity, - OdinRuntimeTopologyCorrelationPurpose, OdinTopologyAuthenticationContext, - OdinTopologyIdentity, authenticate_odin_runtime_topology_correlation, - enroll_service_identity_at, verify_runtime_authority, - }; - - fn digest(byte: char) -> String { - format!("sha256-{}", byte.to_string().repeat(64)) - } - - fn parent_only_descriptor( - fd_number: u32, - fd_name: &str, - source: &str, - ) -> ParentOnlyFileDescriptorObservation { - ParentOnlyFileDescriptorObservation { - fd_number, - fd_name: fd_name.into(), - source_path: PathBuf::from(source), - access: "read-only".into(), - device: 1, - inode: u64::from(fd_number), - uid: 0, - gid: 0, - mode: 0o400, - links: 1, - size: 32, - sha256: digest('a'), - } - } - - #[test] - fn signer_sources_lower_to_exactly_two_ordered_parent_only_open_files() -> Result<()> { - let activation = parent_only_descriptor( - 3, - IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME, - "/run/idunn/activation-credentials/activation.credential", - ); - let presence = parent_only_descriptor( - 4, - RUNTIME_PRESENCE_IDENTITY_FD_NAME, - "/etc/gamecult/service/runtime-presence-identity.cc", - ); - assert_eq!( - parent_only_open_file_properties(&[activation.clone(), presence.clone()])?, - vec![ - "/run/idunn/activation-credentials/activation.credential:gamecult-idunn-runtime-activation-key:read-only", - "/etc/gamecult/service/runtime-presence-identity.cc:gamecult-runtime-presence-identity:read-only", - ] - ); - assert!(parent_only_open_file_properties(&[presence, activation]).is_err()); - Ok(()) - } - - #[cfg(unix)] - fn git_at(repository: &Path, arguments: &[&str]) -> Result { - let output = Command::new("/usr/bin/git") - .arg("-C") - .arg(repository) - .args(arguments) - .env_clear() - .env("HOME", repository) - .env("PATH", "/usr/bin:/bin") - .env("GIT_CONFIG_NOSYSTEM", "1") - .env("LANG", "C.UTF-8") - .output()?; - ensure!( - output.status.success(), - "test Git failed: {}", - String::from_utf8_lossy(&output.stderr).trim() - ); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - - fn expected() -> IdunnExpectedIncarnationRecord { - IdunnExpectedIncarnationRecord { - schema_version: IDUNN_EXPECTED_INCARNATION_SCHEMA.into(), - target: "service".into(), - plan_id: digest('1'), - incarnation_id: "incarnation-1".into(), - sealed_release_id: digest('2'), - source_repository: "github.com/GameCult/Service".into(), - source_revision: "3".repeat(40), - recipe_sha256: digest('4'), - runtime_id: "service-runtime".into(), - expected_signer_identity_id: "service-signer".into(), - health_contract: "service.health.v1".into(), - artifact_sha256: digest('5'), - state_schema_generation: Some("state-v1".into()), - state_contract_sha256: Some(digest('6')), - write_lease_required: true, - route: None, - capabilities: Vec::new(), - dependencies: Vec::new(), - } - } - - fn authenticated_warming( - root: &Path, - ) -> Result<( - IdunnExpectedIncarnationRecord, - IdunnRuntimeActivationRecord, - SequenceAdmittedWarming, - ServiceIdentityTrustAnchor, - )> { - let provider = enroll_service_identity_at::( - &root.join("provider.cc"), - )?; - let idunn = enroll_service_identity_at::(&root.join("idunn.cc"))?; - let odin = enroll_service_identity_at::(&root.join("odin.cc"))?; - let mut expected = expected(); - expected.expected_signer_identity_id = provider.entry().identity_id.clone(); - let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest('7'), 100, &idunn)?; - let activation = launch.activation().clone(); - launch.write_credential(std::io::sink())?; - let authority = verify_runtime_authority( - &expected, - &activation, - &idunn.trust_anchor()?, - &provider.entry().public_key, - )?; - let mut warming = OdinRuntimeTopologyCorrelationRecord { - schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(), - target: expected.target.clone(), - expected_projection_sha256: expected.canonical_sha256()?, - expected: true, - current_activation_sha256: Some(activation.canonical_sha256()?), - signed_presence_sha256: Some(digest('9')), - observed_presence_state: Some("warming".into()), - observed_presence_publisher_sequence: Some(1), - observed_write_lease_sha256: None, - observed_capabilities: Vec::new(), - runtime_id: expected.runtime_id.clone(), - runtime_instance_id: Some(activation.runtime_instance_id.clone()), - present: true, - ready: false, - dependencies: Vec::new(), - disagreements: Vec::new(), - signer_identity_id: odin.entry().identity_id.clone(), - publisher_sequence: 1, - observed_at_unix_millis: 110, - signature_algorithm: "ed25519".into(), - signature: Vec::new(), - }; - warming.signature = odin - .sign::(&warming.unsigned_signature_payload()?) - .signature; - let warming = authenticate_odin_runtime_topology_correlation( - &warming.canonical_bytes()?, - &authority, - None, - &odin.entry().public_key, - OdinTopologyAuthenticationContext { - trusted_received_at_unix_millis: 120, - maximum_age_millis: 30, - maximum_future_skew_millis: 5, - }, - )?; - let warming = SequenceAdmittedWarming::for_test("test-transaction", warming, 120)?; - Ok((expected, activation, warming, provider.trust_anchor()?)) - } - - fn lease( - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - warming: &SequenceAdmittedWarming, - ) -> IdunnProcessWriteLeaseRecord { - IdunnProcessWriteLeaseRecord { - schema_version: IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into(), - target: expected.target.clone(), - expected_projection_sha256: expected.canonical_sha256().unwrap(), - plan_id: expected.plan_id.clone(), - incarnation_id: expected.incarnation_id.clone(), - sealed_release_id: expected.sealed_release_id.clone(), - activation_witness_sha256: activation.canonical_sha256().unwrap(), - state_schema_generation: expected.state_schema_generation.clone().unwrap(), - state_contract_sha256: expected.state_contract_sha256.clone().unwrap(), - runtime_id: expected.runtime_id.clone(), - runtime_instance_id: activation.runtime_instance_id.clone(), - warming_presence_sha256: warming.signed_presence_sha256().to_owned(), - lease_epoch: 1, - issued_at_unix_millis: 200, - } - } - - fn topology_driver(root: &Path, name: &str) -> CultCacheTopologyDriver { - CultCacheTopologyDriver { - projection_store: root.join(format!("{name}.cc")), - correlation_store: root.join(format!("{name}-correlation.cc")), - } - } - - fn project_activation( - driver: &CultCacheTopologyDriver, - expected: &IdunnExpectedIncarnationRecord, - activation: &IdunnRuntimeActivationRecord, - ) -> Result<()> { - ensure!( - activation.expected_projection_sha256 == expected.canonical_sha256()?, - "test activation belongs to another Expected" - ); - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: activation.canonical_bytes()?, - stored_at: rfc3339_millis(activation.issued_at_unix_millis)?, - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - ) - } - - #[test] - fn nginx_rudp_route_is_rendered_as_a_udp_stream_proxy() -> Result<()> { - let driver = NginxRouteDriver::new(RouteBinding { - driver: RouteDriver::NginxStreamUdp, - route_id: "odin-rudp".into(), - stable_endpoint: "rudp://127.0.0.1:17872".into(), - private_host: "127.0.0.1".into(), - private_port_start: 27872, - private_port_end: 27879, - config_path: "/etc/nginx/idunn-stream-routes/odin-rudp.conf".into(), - reload_unit: "nginx.service".into(), - }); - let mut candidate = expected(); - candidate.route = Some(cultnet_rs::IdunnExpectedRoute { - route_id: "odin-rudp".into(), - transport: "rudp".into(), - stable_endpoint: "rudp://127.0.0.1:17872".into(), - candidate_endpoint: "rudp://127.0.0.1:27872".into(), - }); - let rendered = String::from_utf8(driver.render(&candidate)?)?; - assert!(rendered.contains("listen 127.0.0.1:17872 udp reuseport;")); - assert!(rendered.contains("server 127.0.0.1:27872;")); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn nginx_preflight_binds_exact_incumbent_configuration_without_claiming_live_route() - -> Result<()> { - use std::os::unix::fs::PermissionsExt; - - let temp = tempfile::tempdir()?; - let config = temp.path().join("route.conf"); - let nginx = temp.path().join("nginx"); - let systemd_run = temp.path().join("systemd-run"); - let systemctl = temp.path().join("systemctl"); - fs::write( - &nginx, - format!( - "#!/bin/sh\nconfig='{}'\nif [ \"$1\" = \"-t\" ]; then\n candidate=${{IDUNN_TEST_SHADOW:-$config}}\n /bin/grep -q 'server 127.0.0.1:4104' \"$candidate\"\n exit $?\nfi\nexit 64\n", - config.display(), - ), - )?; - fs::write( - &systemd_run, - "#!/bin/sh\nshadow=\nwhile [ \"$#\" -gt 0 ]; do\n case \"$1\" in\n --property=BindReadOnlyPaths=*) binding=${1#--property=BindReadOnlyPaths=}; shadow=${binding%%:*} ;; - --) shift; break ;; - esac\n shift\ndone\nIDUNN_TEST_SHADOW=\"$shadow\"; export IDUNN_TEST_SHADOW\nexec \"$@\"\n", - )?; - fs::write( - &systemctl, - "#!/bin/sh\nif [ -e \"$0.fail\" ]; then exit 65; fi\nif [ \"$1\" = \"reload\" ]; then echo reload >> \"$0.calls\"; exit 0; fi\nexit 64\n", - )?; - fs::set_permissions(&nginx, fs::Permissions::from_mode(0o755))?; - fs::set_permissions(&systemd_run, fs::Permissions::from_mode(0o755))?; - fs::set_permissions(&systemctl, fs::Permissions::from_mode(0o755))?; - - let binding = RouteBinding { - driver: RouteDriver::NginxStreamTcp, - route_id: "service-route".into(), - stable_endpoint: "http://127.0.0.1:4103".into(), - private_host: "127.0.0.1".into(), - private_port_start: 4104, - private_port_end: 4109, - config_path: config.clone(), - reload_unit: "nginx.service".into(), - }; - let driver = NginxRouteDriver { - binding, - nginx_program: nginx, - systemd_run_program: systemd_run, - systemctl_program: systemctl.clone(), - preflight_root: temp.path().join("preflight"), - }; - let mut candidate = expected(); - candidate.route = Some(cultnet_rs::IdunnExpectedRoute { - route_id: "service-route".into(), - transport: "http".into(), - stable_endpoint: "http://127.0.0.1:4103".into(), - candidate_endpoint: "http://127.0.0.1:4104".into(), - }); - let preflight = driver.preflight(&candidate, &digest('a'), None)?; - assert!(!config.exists()); - assert_eq!(fs::read_dir(&driver.preflight_root)?.count(), 0); - - let candidate_bytes = driver.render(&candidate)?; - fs::write(&config, &candidate_bytes)?; - let membership_sha256 = driver.install(&candidate, &digest('a'), &preflight, true)?; - assert_eq!( - fs::read_to_string(systemctl.with_extension("calls"))?, - "reload\n" - ); - assert!(driver.observe_membership(&candidate, &membership_sha256)?); - let admitted_bytes = fs::read(&config)?; - assert!(std::str::from_utf8(&admitted_bytes)?.contains("listen 127.0.0.1:4103;")); - let admitted = RouteObservation { - route_id: "service-route".into(), - runtime_instance_id: digest('a'), - membership_sha256, - signed_presence_sha256: digest('c'), - observed_at_unix_millis: 1, - }; - - let next_preflight = driver.preflight(&candidate, &digest('b'), Some(&admitted))?; - assert_eq!(fs::read(&config)?, admitted_bytes); - fs::write(systemctl.with_extension("fail"), b"fail\n")?; - assert!( - driver - .install(&candidate, &digest('b'), &next_preflight, false) - .is_err() - ); - assert_eq!(fs::read(&config)?, admitted_bytes); - fs::remove_file(systemctl.with_extension("fail"))?; - driver.install(&candidate, &digest('b'), &next_preflight, false)?; - fs::write(&config, b"foreign route\n")?; - assert!( - driver - .install(&candidate, &digest('b'), &next_preflight, true) - .is_err() - ); - driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; - assert_eq!(fs::read(&config)?, admitted_bytes); - assert!(driver.observe_membership(&candidate, &admitted.membership_sha256)?); - - fs::write(&config, b"foreign route after admission\n")?; - fs::write(systemctl.with_extension("fail"), b"fail\n")?; - assert!( - driver - .restore_admitted_membership(&candidate, &admitted.membership_sha256) - .is_err() - ); - assert!(!config.exists()); - fs::remove_file(systemctl.with_extension("fail"))?; - driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; - assert_eq!(fs::read(&config)?, admitted_bytes); - Ok(()) - } - - #[test] - fn process_environment_witness_is_sorted_exact_and_secret_free() -> Result<()> { - let entries = vec![ - b"B=two".to_vec(), - b"A=one".to_vec(), - b"UNRELATED=ignored".to_vec(), - ]; - let selected = select_process_environment(&entries, &["A".into(), "B".into()])?; - assert_eq!(selected.get("A").map(String::as_str), Some("one")); - assert!(!selected.contains_key("UNRELATED")); - assert!(select_process_environment(&entries, &["B".into(), "A".into()]).is_err()); - Ok(()) - } - - #[test] - fn topology_transport_returns_opaque_odin_bytes_without_deciding_ready() -> Result<()> { - let temp = tempfile::tempdir()?; - let correlation_store = temp.path().join("correlation.cc"); - let opaque = b"not a topology receipt".to_vec(); - upsert_record( - &correlation_store, - CultCacheEnvelope { - key: "service".into(), - r#type: OdinRuntimeTopologyCorrelationRecord::TYPE.into(), - payload: opaque.clone(), - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into()), - }, - )?; - let driver = CultCacheTopologyDriver { - projection_store: temp.path().join("projection.cc"), - correlation_store, - }; - - assert_eq!( - driver.receive("service")?, - Some(ReceivedOdinTopologyCorrelation { - target: "service".into(), - canonical_bytes: opaque, - }) - ); - Ok(()) - } - - #[test] - fn expected_withdrawal_is_exact_atomic_and_idempotent() -> Result<()> { - let temp = tempfile::tempdir()?; - let (expected, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; - let lease = lease(&expected, &activation, &warming); - let driver = topology_driver(temp.path(), "projection"); - driver.publish_expected(&expected, &provider_anchor)?; - let published = SingleFileMessagePackBackingStore::new(&driver.projection_store) - .pull_all_read_only_snapshot()?; - assert_eq!(published.len(), 2); - assert!(published.iter().any(|entry| { - entry.key == expected.target && entry.r#type == IdunnExpectedIncarnationRecord::TYPE - })); - let published_anchor = published - .iter() - .find(|entry| entry.r#type == GameCultServiceTrustAnchorRecord::TYPE) - .context("atomic Expected set has no provider trust anchor")?; - assert_eq!( - service_trust_anchor_from_envelope(published_anchor)?, - runtime_presence_trust_anchor(&expected, &provider_anchor)? - ); - project_activation(&driver, &expected, &activation)?; - driver.publish_process_write_lease(&expected, &activation, &lease)?; - - let mut substituted_expected = expected.clone(); - substituted_expected.incarnation_id = "another-incarnation".into(); - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: substituted_expected.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), - }, - )?; - assert!( - driver - .withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease)) - .is_err() - ); - driver.publish_expected(&expected, &provider_anchor)?; - - let mut substituted_anchor = runtime_presence_trust_anchor(&expected, &provider_anchor)?; - substituted_anchor.runtime_id = "another-runtime".into(); - substituted_anchor.validate()?; - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: substituted_anchor.trust_anchor_id.clone(), - r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), - payload: rmp_serde::to_vec(&substituted_anchor)?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), - }, - )?; - assert!( - driver - .withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease)) - .is_err() - ); - driver.publish_expected(&expected, &provider_anchor)?; - - let mut substituted_activation = activation.clone(); - substituted_activation.expected_projection_sha256 = digest('a'); - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: expected.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: substituted_activation.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - )?; - assert!( - driver - .withdraw_expected(&expected, &provider_anchor, Some(&activation), None) - .is_err() - ); - project_activation(&driver, &expected, &activation)?; - - let mut substituted_lease = lease.clone(); - substituted_lease.lease_epoch += 1; - assert!( - driver - .publish_process_write_lease(&expected, &activation, &substituted_lease) - .is_err() - ); - assert!( - driver - .withdraw_expected( - &expected, - &provider_anchor, - Some(&activation), - Some(&substituted_lease), - ) - .is_err() - ); - - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: "unrelated".into(), - r#type: "test.unrelated".into(), - payload: vec![0x90], - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some("test.unrelated.v1".into()), - }, - )?; - driver.withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease))?; - let remaining = SingleFileMessagePackBackingStore::new(&driver.projection_store) - .pull_all_read_only_snapshot()?; - assert_eq!(remaining.len(), 1); - assert_eq!(remaining[0].key, "unrelated"); - driver.withdraw_expected(&expected, &provider_anchor, Some(&activation), Some(&lease))?; - Ok(()) - } - - #[test] - fn incumbent_projection_restoration_demotes_to_expected_only_and_rejects_unknown_authority() - -> Result<()> { - let temp = tempfile::tempdir()?; - let (incumbent, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; - let lease = lease(&incumbent, &activation, &warming); - let driver = topology_driver(temp.path(), "restoration"); - driver.publish_expected(&incumbent, &provider_anchor)?; - project_activation(&driver, &incumbent, &activation)?; - driver.publish_process_write_lease(&incumbent, &activation, &lease)?; - - let mut candidate = incumbent.clone(); - candidate.plan_id = digest('a'); - candidate.incarnation_id = "candidate-incarnation".into(); - candidate.sealed_release_id = digest('b'); - candidate.artifact_sha256 = digest('c'); - let mut candidate_activation = activation.clone(); - candidate_activation.expected_projection_sha256 = candidate.canonical_sha256()?; - candidate_activation.runtime_instance_id = digest('d'); - driver.publish_expected(&candidate, &provider_anchor)?; - project_activation(&driver, &candidate, &candidate_activation)?; - - upsert_record( - &driver.projection_store, - CultCacheEnvelope { - key: "unrelated".into(), - r#type: "test.unrelated".into(), - payload: vec![0x90], - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some("test.unrelated.v1".into()), - }, - )?; - - assert_eq!( - driver.restore_admitted_expected_only( - &candidate, - &provider_anchor, - Some(&candidate_activation), - &incumbent, - &provider_anchor, - &activation, - Some(&lease), - )?, - incumbent.canonical_sha256()? - ); - - let restored = SingleFileMessagePackBackingStore::new(&driver.projection_store) - .pull_all_read_only_snapshot()?; - assert_eq!(restored.len(), 3); - assert_eq!( - IdunnExpectedIncarnationRecord::decode_canonical( - &restored - .iter() - .find(|entry| entry.r#type == IdunnExpectedIncarnationRecord::TYPE) - .unwrap() - .payload, - )?, - incumbent - ); - assert_eq!( - service_trust_anchor_from_envelope( - restored - .iter() - .find(|entry| entry.r#type == GameCultServiceTrustAnchorRecord::TYPE) - .unwrap(), - )?, - runtime_presence_trust_anchor(&incumbent, &provider_anchor)? - ); - assert!( - restored - .iter() - .all(|entry| entry.r#type != IdunnRuntimeActivationRecord::TYPE - && entry.r#type != IdunnProcessWriteLeaseRecord::TYPE) - ); - assert!( - restored - .iter() - .any(|entry| entry.key == "unrelated" && entry.r#type == "test.unrelated") - ); - let idempotent_bytes = fs::read(&driver.projection_store)?; - driver.restore_admitted_expected_only( - &candidate, - &provider_anchor, - Some(&candidate_activation), - &incumbent, - &provider_anchor, - &activation, - Some(&lease), - )?; - assert_eq!(fs::read(&driver.projection_store)?, idempotent_bytes); - - let admitted_anchor = runtime_presence_trust_anchor(&incumbent, &provider_anchor)?; - let mut unknown_expected = incumbent.clone(); - unknown_expected.incarnation_id = "unknown-incarnation".into(); - let mut unknown_anchor = admitted_anchor.clone(); - unknown_anchor.runtime_id = "unknown-runtime".into(); - unknown_anchor.validate()?; - let mut unknown_activation = activation.clone(); - unknown_activation.runtime_instance_id = digest('e'); - let mut unknown_lease = lease.clone(); - unknown_lease.lease_epoch += 1; - let substitutions = [ - CultCacheEnvelope { - key: incumbent.target.clone(), - r#type: IdunnExpectedIncarnationRecord::TYPE.into(), - payload: unknown_expected.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_EXPECTED_INCARNATION_SCHEMA.into()), - }, - CultCacheEnvelope { - key: admitted_anchor.trust_anchor_id.clone(), - r#type: GameCultServiceTrustAnchorRecord::TYPE.into(), - payload: rmp_serde::to_vec(&unknown_anchor)?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into()), - }, - CultCacheEnvelope { - key: incumbent.target.clone(), - r#type: IdunnRuntimeActivationRecord::TYPE.into(), - payload: unknown_activation.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_RUNTIME_ACTIVATION_SCHEMA.into()), - }, - CultCacheEnvelope { - key: incumbent.target.clone(), - r#type: IdunnProcessWriteLeaseRecord::TYPE.into(), - payload: unknown_lease.canonical_bytes()?, - stored_at: "2026-09-03T00:00:00Z".into(), - schema_id: Some(IDUNN_PROCESS_WRITE_LEASE_SCHEMA.into()), - }, - ]; - for substitution in substitutions { - fs::write(&driver.projection_store, &idempotent_bytes)?; - upsert_record(&driver.projection_store, substitution)?; - let before = fs::read(&driver.projection_store)?; - assert!( - driver - .restore_admitted_expected_only( - &candidate, - &provider_anchor, - Some(&candidate_activation), - &incumbent, - &provider_anchor, - &activation, - Some(&lease), - ) - .is_err() - ); - assert_eq!(fs::read(&driver.projection_store)?, before); - } - Ok(()) - } - - #[cfg(unix)] - #[test] - fn service_credential_sources_require_one_root_owned_0400_inode() -> Result<()> { - use std::os::unix::fs::{PermissionsExt, symlink}; - - let temp = tempfile::tempdir()?; - let root = temp.path().join("credentials"); - fs::create_dir(&root)?; - fs::set_permissions(&root, fs::Permissions::from_mode(0o700))?; - let source = root.join("token"); - fs::write(&source, b"secret")?; - fs::set_permissions(&source, fs::Permissions::from_mode(0o400))?; - let mut sources = BTreeMap::from([("SERVICE_TOKEN".into(), source.clone())]); - - validate_service_credential_sources(&sources)?; - - fs::set_permissions(&source, fs::Permissions::from_mode(0o600))?; - assert!(validate_service_credential_sources(&sources).is_err()); - fs::set_permissions(&source, fs::Permissions::from_mode(0o400))?; - - let hardlink = root.join("token-hardlink"); - fs::hard_link(&source, &hardlink)?; - assert!(validate_service_credential_sources(&sources).is_err()); - fs::remove_file(&hardlink)?; - - let symlink_path = root.join("token-symlink"); - symlink(&source, &symlink_path)?; - sources.insert("SERVICE_TOKEN".into(), symlink_path); - assert!(validate_service_credential_sources(&sources).is_err()); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn write_lease_revoke_is_exact_and_never_deletes_a_surprise() -> Result<()> { - let temp = tempfile::tempdir()?; - let path = temp.path().join("lease.cc"); - let driver = CultCacheWriteLeaseDriver::new("service", &path); - let (expected, activation, warming, provider_anchor) = authenticated_warming(temp.path())?; - let lease = lease(&expected, &activation, &warming); - let topology = topology_driver(temp.path(), "lease-projection"); - topology.publish_expected(&expected, &provider_anchor)?; - project_activation(&topology, &expected, &activation)?; - - let lease_sha256 = driver.grant(&expected, &activation, &warming, &lease)?; - assert_eq!(lease_sha256, lease.canonical_sha256()?); - assert!(driver.observe(&expected, &activation, &warming, &lease)?); - assert!( - !SingleFileMessagePackBackingStore::new(&topology.projection_store) - .pull_all_read_only_snapshot()? - .iter() - .any(|entry| entry.r#type == IdunnProcessWriteLeaseRecord::TYPE) - ); - assert_eq!( - topology.publish_process_write_lease(&expected, &activation, &lease)?, - lease_sha256 - ); - - let mut surprise = lease.clone(); - surprise.lease_epoch = 2; - assert!(driver.revoke_exact(Some(&surprise)).is_err()); - assert!(driver.observe(&expected, &activation, &warming, &lease)?); - - SingleFileMessagePackBackingStore::new(&path).with_read_only_shared_snapshot(|_| { - let denial = driver - .revoke_exact(Some(&lease)) - .expect_err("shared lifetime holder must deny without blocking"); - assert!( - denial - .to_string() - .contains("held by a shared-lock consumer") - ); - Ok(()) - })?; - assert!(driver.observe(&expected, &activation, &warming, &lease)?); - - driver.revoke_exact(Some(&lease))?; - assert!(driver.observe_empty()?); - topology.withdraw_process_write_lease(&expected, &activation, Some(&lease))?; - assert!( - !SingleFileMessagePackBackingStore::new(&topology.projection_store) - .pull_all_read_only_snapshot()? - .iter() - .any(|entry| entry.r#type == IdunnProcessWriteLeaseRecord::TYPE) - ); - topology.withdraw_process_write_lease(&expected, &activation, Some(&lease))?; - driver.revoke_exact(Some(&lease))?; - - SingleFileMessagePackBackingStore::new(&path).with_read_only_shared_snapshot( - |snapshot| { - assert!(snapshot.is_empty()); - let denial = driver - .grant(&expected, &activation, &warming, &lease) - .expect_err("shared lifetime holder must deny grant without blocking"); - assert!( - denial - .to_string() - .contains("held by a shared-lock consumer") - ); - Ok(()) - }, - )?; - assert!(driver.observe_empty()?); - assert_eq!( - driver.grant(&expected, &activation, &warming, &lease)?, - lease.canonical_sha256()? - ); - driver.revoke_exact(Some(&lease))?; - Ok(()) - } - - #[cfg(unix)] - #[test] - fn installed_release_is_root_owned_and_nonwritable() -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let temp = tempfile::tempdir()?; - let root = temp.path().join("release"); - fs::create_dir(&root)?; - let artifact = root.join("service"); - fs::write(&artifact, b"sealed")?; - fs::set_permissions(&artifact, fs::Permissions::from_mode(0o777))?; - harden_installed_release( - &root, - &[ArtifactReceipt { - artifact_id: "service".into(), - destination: PathBuf::from("service"), - sha256: sha256_id(b"sealed"), - size_bytes: 6, - executable: true, - }], - )?; - let root_metadata = fs::symlink_metadata(&root)?; - let artifact_metadata = fs::symlink_metadata(&artifact)?; - assert_eq!(root_metadata.uid(), 0); - assert_eq!(root_metadata.permissions().mode() & 0o777, 0o555); - assert_eq!(artifact_metadata.uid(), 0); - assert_eq!(artifact_metadata.permissions().mode() & 0o777, 0o555); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn runner_cache_is_created_once_for_one_exact_identity() -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt}; - - let temp = tempfile::tempdir()?; - fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o700))?; - let cache = temp.path().join("cache"); - let identity = ProcessIdentity { - uid: 1000, - gid: 1000, - }; - ensure_runner_cache_root(&cache, identity)?; - let metadata = fs::symlink_metadata(&cache)?; - assert_eq!((metadata.uid(), metadata.gid()), (1000, 1000)); - assert_eq!(metadata.permissions().mode() & 0o777, 0o700); - assert!(ensure_runner_cache_root(&cache, identity).is_ok()); - assert!( - ensure_runner_cache_root( - &cache, - ProcessIdentity { - uid: 1001, - gid: 1001 - } - ) - .is_err() - ); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn runner_secret_is_root_owned_and_bound_to_the_runner_group() -> Result<()> { - use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::PermissionsExt; - - let temp = tempfile::tempdir()?; - fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o700))?; - let secret = temp.path().join("token"); - fs::write(&secret, b"secret")?; - let secret_c = std::ffi::CString::new(secret.as_os_str().as_bytes())?; - ensure!(unsafe { libc::lchown(secret_c.as_ptr(), 0, 1000) } == 0); - fs::set_permissions(&secret, fs::Permissions::from_mode(0o440))?; - let identity = ProcessIdentity { - uid: 1000, - gid: 1000, - }; - validate_runner_secret(&secret, identity)?; - fs::set_permissions(&secret, fs::Permissions::from_mode(0o444))?; - assert!(validate_runner_secret(&secret, identity).is_err()); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn exact_git_archive_becomes_root_owned_immutable_source_without_git_metadata() -> Result<()> { - use std::os::unix::fs::{MetadataExt, PermissionsExt, symlink}; - - let temp = tempfile::tempdir()?; - let repository = temp.path().join("repository"); - fs::create_dir(&repository)?; - git_at(&repository, &["init", "--initial-branch=main"])?; - git_at(&repository, &["config", "user.name", "Idunn Test"])?; - git_at( - &repository, - &["config", "user.email", "idunn-test@example.invalid"], - )?; - fs::write(repository.join("deployment.toml"), b"target = 'test'\n")?; - let script = repository.join("build.sh"); - fs::write(&script, b"#!/bin/sh\nexit 0\n")?; - fs::set_permissions(&script, fs::Permissions::from_mode(0o755))?; - fs::create_dir(repository.join("links"))?; - symlink("../deployment.toml", repository.join("links/deployment"))?; - git_at(&repository, &["add", "--all"])?; - git_at(&repository, &["commit", "-m", "fixture"])?; - let revision = git_at(&repository, &["rev-parse", "HEAD"])?; - - let parent = temp.path().join("transaction"); - fs::create_dir(&parent)?; - fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?; - let destination = parent.join("source"); - prepare_frozen_source_destination(&destination)?; - let driver = GitSourceDriver::new( - temp.path().join("source-cache"), - temp.path().join("frozen-source"), - None, - ); - driver.git_archive_into(&repository, &revision, None, &destination)?; - driver.git_archive_into( - &repository, - &revision, - Some(Path::new("vendor/fixture")), - &destination, - )?; - harden_frozen_source(&destination)?; - - assert!(!destination.join(".git").exists()); - assert_eq!( - fs::symlink_metadata(destination.join("deployment.toml"))? - .permissions() - .mode() - & 0o777, - 0o444 - ); - let script_metadata = fs::symlink_metadata(destination.join("build.sh"))?; - assert_eq!(script_metadata.uid(), 0); - assert_eq!(script_metadata.permissions().mode() & 0o777, 0o555); - assert!(destination.join("vendor/fixture/deployment.toml").is_file()); - validate_frozen_source_symlink(&destination, &destination.join("links/deployment"))?; - validate_frozen_source(&destination)?; - let digest = frozen_source_sha256(&destination)?; - let recipe = destination.join("deployment.toml"); - fs::set_permissions(&recipe, fs::Permissions::from_mode(0o644))?; - assert!(validate_frozen_source(&destination).is_err()); - fs::set_permissions(&recipe, fs::Permissions::from_mode(0o444))?; - fs::write(&recipe, b"target = 'changed'\n")?; - assert_ne!(frozen_source_sha256(&destination)?, digest); - Ok(()) - } - - #[cfg(unix)] - #[test] - fn frozen_source_rejects_a_symlink_that_escapes_the_transaction_tree() -> Result<()> { - use std::os::unix::fs::{PermissionsExt, symlink}; - - let temp = tempfile::tempdir()?; - let parent = temp.path().join("transaction"); - fs::create_dir(&parent)?; - fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?; - let destination = parent.join("source"); - prepare_frozen_source_destination(&destination)?; - symlink("../../outside", destination.join("escape"))?; - assert!(harden_frozen_source(&destination).is_err()); - Ok(()) - } -} diff --git a/crates/idunn-daemon/src/lib.rs b/crates/idunn-daemon/src/lib.rs deleted file mode 100644 index 7899c59..0000000 --- a/crates/idunn-daemon/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod control_plane; -pub mod deployment; -pub mod deployment_plan; -pub mod drivers; -pub mod provisioning; diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs deleted file mode 100644 index e7bd0a5..0000000 --- a/crates/idunn-daemon/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() -> anyhow::Result<()> { - idunn_daemon::control_plane::run(std::env::args().skip(1)) -} diff --git a/crates/idunn-daemon/src/provisioning.rs b/crates/idunn-daemon/src/provisioning.rs deleted file mode 100644 index 8ae58a9..0000000 --- a/crates/idunn-daemon/src/provisioning.rs +++ /dev/null @@ -1,2455 +0,0 @@ -use anyhow::{Context, Result, anyhow, bail}; -use cultcache_rs::{ - CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, -}; -use cultnet_rs::{ - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultProviderHealthIdentity, - GameCultServiceTrustAnchorRecord, IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_AUTHORITY, - IDUNN_DEPLOYMENT_BRAKE_ID, IDUNN_DEPLOYMENT_BRAKE_SCHEMA, IDUNN_DEPLOYMENT_BRAKE_SCOPE, - IDUNN_DEPLOYMENT_RELEASE_PURPOSE, IDUNN_LIFECYCLE_BRAKE_AUTHORITY, IDUNN_LIFECYCLE_BRAKE_SCHEMA, - IDUNN_LIFECYCLE_BRAKE_SCOPE, IdunnDaemonHealthTrustBindingRecord, - IdunnDeploymentBrakeObservation, IdunnDeploymentBrakeOperatorIdentity, - IdunnDeploymentBrakeRecord, IdunnDeploymentBrakeReleasePurpose, IdunnLifecycleBrakeObservation, - IdunnLifecycleBrakeRecord, IdunnServiceIdentity, OdinTopologyIdentity, ServiceIdentityProfile, - ServiceIdentityTrustAnchor, derive_service_identity_id, enroll_service_identity_at, - evaluate_idunn_continuity_restart, evaluate_idunn_deployment_brake, - export_service_identity_trust_anchor, open_service_identity_at, -}; -use std::collections::{BTreeMap, BTreeSet}; -use std::path::{Path, PathBuf}; - -trait RootDistributionDocument: DatabaseEntry { - fn root_key(&self) -> &str; -} - -impl RootDistributionDocument for IdunnDaemonHealthTrustBindingRecord { - fn root_key(&self) -> &str { - &self.binding_id - } -} - -impl RootDistributionDocument for GameCultServiceTrustAnchorRecord { - fn root_key(&self) -> &str { - &self.trust_anchor_id - } -} - -pub fn run(args: impl IntoIterator) -> Result<()> { - let mut args = args.into_iter(); - let command = args.next().ok_or_else(|| anyhow!(usage()))?; - let options = parse_options(args)?; - match command.as_str() { - "enroll-provider-health-identity" => { - let public_key = enroll_provider_health_identity(&options)?; - println!("{public_key}"); - } - "provider-health-public-key" => { - let public_key = provider_health_public_key(&options)?; - println!("{public_key}"); - } - "export-provider-health-public-anchor" => export_identity_anchor::< - GameCultProviderHealthIdentity, - >( - &options, "provider health public anchor" - )?, - "enroll-idunn-identity" => enroll_identity::(&options)?, - "export-idunn-public-anchor" => { - export_identity_anchor::(&options, "Idunn public anchor")? - } - "enroll-odin-topology-identity" => enroll_identity::(&options)?, - "export-odin-topology-public-anchor" => { - export_identity_anchor::(&options, "Odin topology public anchor")? - } - "enroll-deployment-brake-operator" => { - enroll_identity::(&options)? - } - "export-deployment-brake-operator-anchor" => { - export_identity_anchor::( - &options, - "deployment brake operator anchor", - )? - } - "deployment-brake-engage" => deployment_brake_engage(&options)?, - "deployment-brake-release" => deployment_brake_release(&options)?, - "deployment-brake-status" => deployment_brake_status(&options)?, - "lifecycle-brake-engage" => lifecycle_brake_engage(&options)?, - "lifecycle-brake-release" => lifecycle_brake_release(&options)?, - "lifecycle-brake-status" => lifecycle_brake_status(&options)?, - "create-daemon-health-trust-binding" => create_health_binding(&options)?, - "add-daemon-health-trust-binding" => add_health_binding(&options)?, - "require-daemon-health-release-binding" => require_health_binding_release(&options)?, - "rotate-daemon-health-trust-signer" => rotate_health_binding_signer(&options)?, - "validate-daemon-health-trust-binding" => { - require_only(&options, &["input"])?; - validate_health_binding_store(&path(&options, "input")?)?; - } - "create-provider-projection-trust-anchor" => create_projection_anchor(&options)?, - "validate-provider-projection-trust-anchor" => validate_projection_anchor(&options)?, - _ => bail!("unknown command {command:?}\n{}", usage()), - } - Ok(()) -} - -fn enroll_identity(options: &BTreeMap) -> Result<()> { - require_only(options, &["private-store"])?; - enroll_service_identity_at::

(&path(options, "private-store")?)?; - Ok(()) -} - -fn export_identity_anchor( - options: &BTreeMap, - label: &str, -) -> Result<()> { - require_only(options, &["private-store", "public-anchor"])?; - let private = path(options, "private-store")?; - let public = path(options, "public-anchor")?; - reject_alias(&private, &public)?; - refuse_existing(&public, label)?; - let signer = open_service_identity_at::

(&private)?; - export_service_identity_trust_anchor(&signer, &public)?; - Ok(()) -} - -fn enroll_provider_health_identity(options: &BTreeMap) -> Result { - require_only(options, &["private-store"])?; - let signer = enroll_service_identity_at::(&path( - options, - "private-store", - )?)?; - encode_public_key(&signer.entry().public_key) -} - -fn provider_health_public_key(options: &BTreeMap) -> Result { - require_only(options, &["private-store"])?; - let signer = open_service_identity_at::(&path( - options, - "private-store", - )?)?; - encode_public_key(&signer.entry().public_key) -} - -fn deployment_brake_engage(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "store", - "runtime-id", - "owner", - "reason", - "observed-at-unix-millis", - ], - )?; - let observed = parse_u64(options, "observed-at-unix-millis")?; - let record = IdunnDeploymentBrakeRecord { - schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), - brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), - authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), - runtime_id: required(options, "runtime-id")?.into(), - status: "engaged".into(), - scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), - reason: required(options, "reason")?.into(), - observed_at_unix_millis: observed, - expires_at_unix_millis: None, - authorization_id: None, - authorization_purpose: None, - authorized_release_id: None, - authorized_deployment_id: None, - authorized_by: None, - authorization_issued_at_unix_millis: None, - authorization_expires_at_unix_millis: None, - signature_algorithm: None, - signature: None, - private_state_exposed: false, - updated_by: required(options, "owner")?.into(), - }; - record.validate()?; - replace_brake(&path(options, "store")?, record, observed) -} - -fn deployment_brake_release(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "store", - "private-store", - "runtime-id", - "owner", - "reason", - "authorization-id", - "release-id", - "deployment-id", - "issued-at-unix-millis", - "expires-at-unix-millis", - ], - )?; - let issued = parse_u64(options, "issued-at-unix-millis")?; - let expires = parse_u64(options, "expires-at-unix-millis")?; - let signer = open_service_identity_at::(&path( - options, - "private-store", - )?)?; - let mut record = IdunnDeploymentBrakeRecord { - schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), - brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), - authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), - runtime_id: required(options, "runtime-id")?.into(), - status: "released".into(), - scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), - reason: required(options, "reason")?.into(), - observed_at_unix_millis: issued, - expires_at_unix_millis: Some(expires), - authorization_id: Some(required(options, "authorization-id")?.into()), - authorization_purpose: Some(IDUNN_DEPLOYMENT_RELEASE_PURPOSE.into()), - authorized_release_id: Some(required(options, "release-id")?.into()), - authorized_deployment_id: Some(required(options, "deployment-id")?.into()), - authorized_by: Some(signer.trust_anchor()?.identity_id), - authorization_issued_at_unix_millis: Some(issued), - authorization_expires_at_unix_millis: Some(expires), - signature_algorithm: Some("ed25519".into()), - signature: None, - private_state_exposed: false, - updated_by: required(options, "owner")?.into(), - }; - record.signature = Some( - signer - .sign::(&rmp_serde::to_vec(&record)?) - .signature, - ); - record.validate()?; - replace_brake(&path(options, "store")?, record, issued) -} - -fn deployment_brake_status(options: &BTreeMap) -> Result<()> { - allow_only( - options, - &[ - "store", - "operator-anchor", - "runtime-id", - "release-id", - "deployment-id", - "now-unix-millis", - ], - )?; - let exact_actuation_fields = ["release-id", "deployment-id", "now-unix-millis"]; - let exact_actuation_field_count = exact_actuation_fields - .into_iter() - .filter(|field| options.contains_key(*field)) - .count(); - if exact_actuation_field_count != 0 - && exact_actuation_field_count != exact_actuation_fields.len() - { - bail!("exact deployment-brake actuation validation requires release, deployment, and time"); - } - let record = read_brake(&path(options, "store")?)?; - if record.runtime_id != required(options, "runtime-id")? { - bail!("deployment brake belongs to another runtime") - } - if record.status == "engaged" { - if exact_actuation_field_count != 0 { - bail!("engaged deployment brake denies exact actuation validation"); - } - println!( - "engaged runtime={} scope={} owner={} reason={}", - record.runtime_id, record.scope, record.updated_by, record.reason - ); - return Ok(()); - } - let anchor = read_operator_anchor(&path(options, "operator-anchor")?)?; - evaluate_idunn_deployment_brake( - IdunnDeploymentBrakeObservation::Present(&record), - &anchor, - required(options, "runtime-id")?, - required(options, "release-id")?, - required(options, "deployment-id")?, - parse_u64(options, "now-unix-millis")?, - ) - .map_err(|d| anyhow!("deployment brake denied actuation: {d:?}"))?; - println!( - "released runtime={} release={} deployment={} owner={}", - record.runtime_id, - record.authorized_release_id.unwrap(), - record.authorized_deployment_id.unwrap(), - record.updated_by - ); - Ok(()) -} - -fn lifecycle_brake_engage(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "store", - "runtime-id", - "target", - "reason", - "updated-at-unix-millis", - ], - )?; - let updated = parse_u64(options, "updated-at-unix-millis")?; - let record = IdunnLifecycleBrakeRecord { - schema_version: IDUNN_LIFECYCLE_BRAKE_SCHEMA.into(), - authority: IDUNN_LIFECYCLE_BRAKE_AUTHORITY.into(), - runtime_id: required(options, "runtime-id")?.into(), - target: required(options, "target")?.into(), - scope: IDUNN_LIFECYCLE_BRAKE_SCOPE.into(), - status: "engaged".into(), - reason: required(options, "reason")?.into(), - updated_at_unix_millis: updated, - released_until_unix_millis: None, - }; - replace_lifecycle_brake(&path(options, "store")?, record) -} - -fn lifecycle_brake_release(options: &BTreeMap) -> Result<()> { - allow_only( - options, - &[ - "store", - "runtime-id", - "target", - "reason", - "updated-at-unix-millis", - "released-until-unix-millis", - ], - )?; - let updated = parse_u64(options, "updated-at-unix-millis")?; - let record = IdunnLifecycleBrakeRecord { - schema_version: IDUNN_LIFECYCLE_BRAKE_SCHEMA.into(), - authority: IDUNN_LIFECYCLE_BRAKE_AUTHORITY.into(), - runtime_id: required(options, "runtime-id")?.into(), - target: required(options, "target")?.into(), - scope: IDUNN_LIFECYCLE_BRAKE_SCOPE.into(), - status: "released".into(), - reason: required(options, "reason")?.into(), - updated_at_unix_millis: updated, - released_until_unix_millis: options - .get("released-until-unix-millis") - .map(|value| { - value - .parse() - .context("--released-until-unix-millis must be u64") - }) - .transpose()?, - }; - replace_lifecycle_brake(&path(options, "store")?, record) -} - -fn lifecycle_brake_status(options: &BTreeMap) -> Result<()> { - require_only( - options, - &["store", "runtime-id", "target", "now-unix-millis"], - )?; - let runtime_id = lifecycle_identifier(options, "runtime-id")?; - let target = lifecycle_identifier(options, "target")?; - let now = parse_u64(options, "now-unix-millis")?; - match read_lifecycle_brake(&path(options, "store")?)? { - None => { - evaluate_idunn_continuity_restart( - IdunnLifecycleBrakeObservation::Missing, - runtime_id, - target, - now, - ) - .map_err(|denial| anyhow!("lifecycle brake denied continuity restart: {denial:?}"))?; - println!("allowed runtime={runtime_id} target={target} brake=absent"); - } - Some(record) => { - evaluate_idunn_continuity_restart( - IdunnLifecycleBrakeObservation::Present(&record), - runtime_id, - target, - now, - ) - .map_err(|denial| anyhow!("lifecycle brake denied continuity restart: {denial:?}"))?; - if let Some(expires) = record.released_until_unix_millis { - println!( - "allowed runtime={} target={} brake=released expires={expires}", - record.runtime_id, record.target - ); - } else { - println!( - "allowed runtime={} target={} brake=released expires=never", - record.runtime_id, record.target - ); - } - } - } - Ok(()) -} - -fn replace_lifecycle_brake(path: &Path, record: IdunnLifecycleBrakeRecord) -> Result<()> { - record.validate()?; - let store = SingleFileMessagePackBackingStore::new(path); - let entries = store.pull_all_read_only_snapshot()?; - let current = match entries.as_slice() { - [] => None, - [entry] - if entry.r#type == IdunnLifecycleBrakeRecord::TYPE - && entry.schema_id.as_deref() == Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA) => - { - let current_record = IdunnLifecycleBrakeRecord::decode_canonical(&entry.payload)?; - if entry.key != current_record.target { - bail!("lifecycle brake envelope key does not match its target"); - } - if current_record.runtime_id != record.runtime_id - || current_record.target != record.target - { - bail!("lifecycle brake store is bound to another runtime or target"); - } - if record.updated_at_unix_millis <= current_record.updated_at_unix_millis { - bail!("lifecycle brake update time must advance"); - } - Some(entry.clone()) - } - _ => bail!("lifecycle brake store is corrupt or ambiguous"), - }; - let next = typed_envelope( - &record.target, - &record, - IDUNN_LIFECYCLE_BRAKE_SCHEMA, - record.updated_at_unix_millis, - )?; - if !store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: IdunnLifecycleBrakeRecord::TYPE.into(), - key: record.target.clone(), - current, - }], - &[next], - )? { - bail!("lifecycle brake changed during transition"); - } - Ok(()) -} - -fn read_lifecycle_brake(path: &Path) -> Result> { - if !path.exists() { - return Ok(None); - } - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let entry = match entries.as_slice() { - [] => return Ok(None), - [entry] => entry, - _ => bail!("lifecycle brake store is ambiguous"), - }; - if entry.r#type != IdunnLifecycleBrakeRecord::TYPE - || entry.schema_id.as_deref() != Some(IDUNN_LIFECYCLE_BRAKE_SCHEMA) - { - bail!("lifecycle brake store contains a foreign record"); - } - let record = IdunnLifecycleBrakeRecord::decode_canonical(&entry.payload)?; - if entry.key != record.target { - bail!("lifecycle brake envelope key does not match its target"); - } - Ok(Some(record)) -} - -fn lifecycle_identifier<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { - let value = required(options, name)?; - if value.trim().is_empty() || value.len() > 256 || value.chars().any(char::is_control) { - bail!("--{name} is empty, oversized, or contains control characters"); - } - Ok(value) -} - -fn replace_brake(path: &Path, record: IdunnDeploymentBrakeRecord, millis: u64) -> Result<()> { - let store = SingleFileMessagePackBackingStore::new(path); - let entries = store.pull_all_read_only_snapshot()?; - let current = match entries.as_slice() { - [] => None, - [entry] - if entry.r#type == IdunnDeploymentBrakeRecord::TYPE - && entry.key == IDUNN_DEPLOYMENT_BRAKE_ID => - { - Some(entry.clone()) - } - _ => bail!("deployment brake store is corrupt or ambiguous"), - }; - let next = typed_envelope( - IDUNN_DEPLOYMENT_BRAKE_ID, - &record, - IDUNN_DEPLOYMENT_BRAKE_SCHEMA, - millis, - )?; - if !store.compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: IdunnDeploymentBrakeRecord::TYPE.into(), - key: IDUNN_DEPLOYMENT_BRAKE_ID.into(), - current, - }], - &[next], - )? { - bail!("deployment brake changed during transition"); - } - Ok(()) -} - -fn read_brake(path: &Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let [entry] = entries.as_slice() else { - bail!("deployment brake is missing or ambiguous") - }; - if entry.r#type != IdunnDeploymentBrakeRecord::TYPE - || entry.key != IDUNN_DEPLOYMENT_BRAKE_ID - || entry.schema_id.as_deref() != Some(IDUNN_DEPLOYMENT_BRAKE_SCHEMA) - { - bail!("deployment brake is foreign") - } - let record: IdunnDeploymentBrakeRecord = rmp_serde::from_slice(&entry.payload)?; - if rmp_serde::to_vec(&record)? != entry.payload { - bail!("deployment brake is noncanonical") - } - record.validate()?; - Ok(record) -} - -fn read_operator_anchor(path: &Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let [entry] = entries.as_slice() else { - bail!("operator anchor is missing or ambiguous") - }; - if entry.r#type != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_TYPE - || entry.key != IdunnDeploymentBrakeOperatorIdentity::TRUST_ANCHOR_KEY - { - bail!("operator anchor is foreign") - } - Ok(rmp_serde::from_slice(&entry.payload)?) -} - -fn validate_health_binding_store(path: &Path) -> Result<()> { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - validated_health_bindings(&entries)?; - Ok(()) -} - -fn validated_health_bindings( - entries: &[CultCacheEnvelope], -) -> Result> { - if entries.is_empty() { - bail!("daemon health trust store is empty"); - } - let mut keys = BTreeSet::new(); - let mut tuples = BTreeSet::new(); - let mut bindings = Vec::with_capacity(entries.len()); - for envelope in entries { - if envelope.r#type != IdunnDaemonHealthTrustBindingRecord::TYPE - || envelope.schema_id.as_deref() != Some(IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA) - { - bail!("trust store contains an alien type or schema"); - } - let binding: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&envelope.payload)?; - if rmp_serde::to_vec(&binding)? != envelope.payload || envelope.key != binding.binding_id { - bail!("trust store contains a noncanonical or mismatched binding"); - } - binding.validate()?; - if !keys.insert(binding.binding_id.clone()) - || !tuples.insert(( - binding.daemon_id.clone(), - binding.health_contract.clone(), - binding.source_runtime_id.clone(), - )) - { - bail!("trust store contains a duplicate binding id or tuple"); - } - bindings.push(binding); - } - Ok(bindings) -} - -fn create_health_binding(options: &BTreeMap) -> Result<()> { - let record = health_binding(options)?; - write_new_typed( - &path(options, "output")?, - &record.binding_id, - &record, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - record.bound_at_unix_millis, - ) -} - -fn health_binding( - options: &BTreeMap, -) -> Result { - require_only( - options, - &[ - "output", - "binding-id", - "daemon", - "health-contract", - "source-runtime", - "signer-public-key-hex", - "bound-at-unix-millis", - "release-binding-required", - ], - )?; - let public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; - let record = IdunnDaemonHealthTrustBindingRecord { - schema_version: IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), - binding_id: required(options, "binding-id")?.into(), - daemon_id: required(options, "daemon")?.into(), - health_contract: required(options, "health-contract")?.into(), - source_runtime_id: required(options, "source-runtime")?.into(), - signer_identity_id: derive_service_identity_id::( - &public_key, - )?, - signer_public_key: public_key, - binding_authority: "root".into(), - bound_at_unix_millis: parse_u64(options, "bound-at-unix-millis")?, - release_binding_required: parse_bool(options, "release-binding-required")?, - private_state_exposed: false, - }; - record.validate()?; - Ok(record) -} - -fn add_health_binding(options: &BTreeMap) -> Result<()> { - let record = health_binding(options)?; - let output = path(options, "output")?; - let store = SingleFileMessagePackBackingStore::new(&output); - let existing = store.pull_all_read_only_snapshot()?; - if existing.is_empty() { - bail!("trust store must already exist; create the first binding explicitly"); - } - let mut keys = BTreeSet::new(); - let mut tuples = BTreeSet::new(); - let mut expected = Vec::with_capacity(existing.len() + 1); - for envelope in existing { - if envelope.r#type != IdunnDaemonHealthTrustBindingRecord::TYPE - || envelope.schema_id.as_deref() != Some(IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA) - { - bail!("trust store contains an alien type or schema"); - } - let binding: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&envelope.payload)?; - if rmp_serde::to_vec(&binding)? != envelope.payload || envelope.key != binding.binding_id { - bail!("trust store contains a noncanonical or mismatched binding"); - } - binding.validate()?; - if !keys.insert(binding.binding_id.clone()) { - bail!("trust store contains duplicate binding ids"); - } - if !tuples.insert(( - binding.daemon_id, - binding.health_contract, - binding.source_runtime_id, - )) { - bail!("trust store contains duplicate daemon/contract/runtime tuples"); - } - expected.push(CultCacheExpectedEnvelope { - r#type: envelope.r#type.clone(), - key: envelope.key.clone(), - current: Some(envelope), - }); - } - if keys.contains(&record.binding_id) { - bail!("binding id already exists"); - } - if tuples.contains(&( - record.daemon_id.clone(), - record.health_contract.clone(), - record.source_runtime_id.clone(), - )) { - bail!("daemon/contract/runtime tuple already exists"); - } - expected.push(CultCacheExpectedEnvelope { - r#type: IdunnDaemonHealthTrustBindingRecord::TYPE.into(), - key: record.binding_id.clone(), - current: None, - }); - let envelope = typed_envelope( - &record.binding_id, - &record, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - record.bound_at_unix_millis, - )?; - if !store.compare_exchange(&expected, &[envelope])? { - bail!("trust store changed during validated append"); - } - Ok(()) -} - -fn require_health_binding_release(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "store", - "binding-id", - "daemon", - "health-contract", - "source-runtime", - "signer-public-key-hex", - ], - )?; - let store_path = path(options, "store")?; - let store = SingleFileMessagePackBackingStore::new(&store_path); - let entries = store.pull_all_read_only_snapshot()?; - let signer_public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; - require_health_binding_release_from_snapshot( - &store, - entries, - required(options, "binding-id")?, - required(options, "daemon")?, - required(options, "health-contract")?, - required(options, "source-runtime")?, - &signer_public_key, - ) -} - -fn require_health_binding_release_from_snapshot( - store: &SingleFileMessagePackBackingStore, - entries: Vec, - binding_id: &str, - daemon_id: &str, - health_contract: &str, - source_runtime_id: &str, - signer_public_key: &[u8], -) -> Result<()> { - let bindings = validated_health_bindings(&entries)?; - let target_index = bindings - .iter() - .position(|binding| binding.binding_id == binding_id) - .ok_or_else(|| anyhow!("named daemon health trust binding does not exist"))?; - let current = &bindings[target_index]; - if current.daemon_id != daemon_id - || current.health_contract != health_contract - || current.source_runtime_id != source_runtime_id - || current.signer_public_key != signer_public_key - { - bail!("named daemon health trust binding does not match the expected immutable fields"); - } - if current.release_binding_required { - return Ok(()); - } - - let mut next = current.clone(); - next.release_binding_required = true; - next.validate()?; - let mut replacement = entries[target_index].clone(); - replacement.payload = rmp_serde::to_vec(&next)?; - let expected = entries - .into_iter() - .map(|envelope| CultCacheExpectedEnvelope { - r#type: envelope.r#type.clone(), - key: envelope.key.clone(), - current: Some(envelope), - }) - .collect::>(); - if !store.compare_exchange(&expected, &[replacement])? { - let latest_entries = store.pull_all_read_only_snapshot()?; - let latest_bindings = validated_health_bindings(&latest_entries)?; - if let Some(latest) = latest_bindings - .iter() - .find(|binding| binding.binding_id == binding_id) - && latest.daemon_id == daemon_id - && latest.health_contract == health_contract - && latest.source_runtime_id == source_runtime_id - && latest.signer_public_key == signer_public_key - && latest.release_binding_required - { - return Ok(()); - } - bail!("daemon health trust store changed during release-binding transition"); - } - Ok(()) -} - -fn rotate_health_binding_signer(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "output", - "binding-id", - "signer-public-key-hex", - "bound-at-unix-millis", - ], - )?; - let output = path(options, "output")?; - validate_health_binding_store(&output)?; - let store = SingleFileMessagePackBackingStore::new(&output); - let existing = store.pull_all_read_only_snapshot()?; - let binding_id = required(options, "binding-id")?; - let public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; - let bound_at = parse_u64(options, "bound-at-unix-millis")?; - let mut expected = Vec::with_capacity(existing.len()); - let mut replacement = None; - for envelope in existing { - let binding: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&envelope.payload)?; - if binding.binding_id == binding_id { - if bound_at <= binding.bound_at_unix_millis { - bail!("rotated signer binding time must advance"); - } - if binding.signer_public_key == public_key { - bail!("rotated signer must differ from the current signer"); - } - let mut rotated = binding.clone(); - rotated.signer_identity_id = - derive_service_identity_id::(&public_key)?; - rotated.signer_public_key = public_key.clone(); - rotated.bound_at_unix_millis = bound_at; - rotated.validate()?; - replacement = Some(rotated); - } - expected.push(CultCacheExpectedEnvelope { - r#type: envelope.r#type.clone(), - key: envelope.key.clone(), - current: Some(envelope), - }); - } - let replacement = replacement.ok_or_else(|| anyhow!("binding id does not exist"))?; - let envelope = typed_envelope( - &replacement.binding_id, - &replacement, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - bound_at, - )?; - if !store.compare_exchange(&expected, &[envelope])? { - bail!("trust store changed during validated signer rotation"); - } - validate_health_binding_store(&output) -} - -fn typed_envelope( - key: &str, - value: &T, - schema: &str, - millis: u64, -) -> Result { - let stored_at = chrono::DateTime::from_timestamp_millis(i64::try_from(millis)?) - .ok_or_else(|| anyhow!("document timestamp is out of range"))? - .to_rfc3339(); - Ok(CultCacheEnvelope { - key: key.into(), - r#type: T::TYPE.into(), - payload: rmp_serde::to_vec(value)?, - stored_at, - schema_id: Some(schema.into()), - }) -} - -fn create_projection_anchor(options: &BTreeMap) -> Result<()> { - require_only( - options, - &[ - "output", - "trust-anchor-id", - "runtime-id", - "idunn-public-anchor", - "bound-at-unix-millis", - "expires-at-unix-millis", - ], - )?; - let output = path(options, "output")?; - let low_path = path(options, "idunn-public-anchor")?; - reject_alias(&output, &low_path)?; - let low = read_low_level_idunn_anchor(&low_path)?; - let bound = parse_u64(options, "bound-at-unix-millis")?; - let record = GameCultServiceTrustAnchorRecord { - schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), - trust_anchor_id: required(options, "trust-anchor-id")?.into(), - service_id: "idunn".into(), - runtime_id: required(options, "runtime-id")?.into(), - signer_identity_id: derive_service_identity_id::(&low.public_key)?, - signer_public_key: low.public_key, - signature_algorithm: "ed25519".into(), - signing_purpose: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(), - signed_schema: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), - binding_authority: "root".into(), - bound_at_unix_millis: bound, - expires_at_unix_millis: options - .get("expires-at-unix-millis") - .map(|value| value.parse().context("expires-at-unix-millis must be u64")) - .transpose()?, - private_state_exposed: false, - }; - record.validate()?; - write_new_typed( - &output, - &record.trust_anchor_id, - &record, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - bound, - ) -} - -fn validate_projection_anchor(options: &BTreeMap) -> Result<()> { - require_only(options, &["input", "idunn-public-anchor"])?; - let input = path(options, "input")?; - let low_path = path(options, "idunn-public-anchor")?; - reject_alias(&input, &low_path)?; - let record = read_typed::( - &input, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - )?; - record.validate()?; - let low = read_low_level_idunn_anchor(&low_path)?; - if record.signer_public_key != low.public_key || record.signer_identity_id != low.identity_id { - bail!("projection trust anchor does not match the supplied Idunn public anchor"); - } - Ok(()) -} - -fn write_new_typed( - path: &Path, - key: &str, - value: &T, - schema: &str, - millis: u64, -) -> Result<()> { - refuse_existing(path, "root distribution document")?; - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent)?; - } - let stored_at = chrono::DateTime::from_timestamp_millis( - i64::try_from(millis).context("document timestamp is out of range")?, - ) - .ok_or_else(|| anyhow!("document timestamp is out of range"))? - .to_rfc3339(); - let envelope = CultCacheEnvelope { - key: key.into(), - r#type: T::TYPE.into(), - payload: rmp_serde::to_vec(value)?, - stored_at, - schema_id: Some(schema.into()), - }; - let created = SingleFileMessagePackBackingStore::new(path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: T::TYPE.into(), - key: key.into(), - current: None, - }], - &[envelope], - )?; - if !created { - bail!("root distribution document lost atomic create race; refusing replacement"); - } - Ok(()) -} - -fn read_typed(path: &Path, schema: &str) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let [entry] = entries.as_slice() else { - bail!("root distribution file must contain exactly one document"); - }; - if entry.r#type != T::TYPE || entry.schema_id.as_deref() != Some(schema) { - bail!("root distribution document has the wrong type or schema"); - } - let value: T = rmp_serde::from_slice(&entry.payload).context("decoding typed root document")?; - if rmp_serde::to_vec(&value)? != entry.payload { - bail!("root distribution document is not canonical positional MessagePack"); - } - if entry.key != value.root_key() { - bail!("root distribution envelope key does not match its typed document"); - } - Ok(value) -} - -fn read_low_level_idunn_anchor(path: &Path) -> Result { - let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; - let [entry] = entries.as_slice() else { - bail!("Idunn public anchor store must contain exactly one document"); - }; - if entry.r#type != IdunnServiceIdentity::TRUST_ANCHOR_TYPE - || entry.key != IdunnServiceIdentity::TRUST_ANCHOR_KEY - || entry.schema_id.as_deref() != Some(IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA) - { - bail!("public anchor does not belong to the Idunn service identity profile"); - } - let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&entry.payload)?; - if rmp_serde::to_vec(&anchor)? != entry.payload - || anchor.schema_version != IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA - || anchor.identity_id - != derive_service_identity_id::(&anchor.public_key)? - { - bail!("Idunn public anchor is malformed or names the wrong key"); - } - Ok(anchor) -} - -fn parse_options(args: impl Iterator) -> Result> { - let mut args = args.peekable(); - let mut out = BTreeMap::new(); - while let Some(name) = args.next() { - let name = name - .strip_prefix("--") - .ok_or_else(|| anyhow!("expected --option, got {name:?}"))?; - if name.contains("seed") || name.contains("private-key") { - bail!("private seed/key input is forbidden; enroll a protected identity instead"); - } - let value = args - .next() - .ok_or_else(|| anyhow!("missing value for --{name}"))?; - if out.insert(name.into(), value).is_some() { - bail!("duplicate option --{name}"); - } - } - Ok(out) -} - -fn require_only(options: &BTreeMap, names: &[&str]) -> Result<()> { - for name in names { - required(options, name)?; - } - if let Some(name) = options.keys().find(|name| !names.contains(&name.as_str())) { - bail!("unsupported option --{name}"); - } - Ok(()) -} - -fn allow_only(options: &BTreeMap, names: &[&str]) -> Result<()> { - if let Some(name) = options.keys().find(|name| !names.contains(&name.as_str())) { - bail!("unsupported option --{name}"); - } - Ok(()) -} - -fn required<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { - options - .get(name) - .map(String::as_str) - .ok_or_else(|| anyhow!("missing --{name}")) -} - -fn path(options: &BTreeMap, name: &str) -> Result { - let value = required(options, name)?; - if value.trim().is_empty() { - bail!("--{name} must not be empty"); - } - Ok(PathBuf::from(value)) -} - -fn parse_u64(options: &BTreeMap, name: &str) -> Result { - required(options, name)? - .parse() - .with_context(|| format!("--{name} must be u64")) -} - -fn parse_bool(options: &BTreeMap, name: &str) -> Result { - match required(options, name)? { - "true" => Ok(true), - "false" => Ok(false), - _ => bail!("--{name} must be true or false"), - } -} - -fn decode_public_key(value: &str) -> Result> { - if value.len() != 64 - || !value - .bytes() - .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) - { - bail!("signer public key must be exactly 64 lowercase hexadecimal characters"); - } - (0..32) - .map(|index| u8::from_str_radix(&value[index * 2..index * 2 + 2], 16).map_err(Into::into)) - .collect() -} - -fn encode_public_key(value: &[u8]) -> Result { - if value.len() != 32 { - bail!("provider health public key must be exactly 32 bytes"); - } - const HEX: &[u8; 16] = b"0123456789abcdef"; - let mut encoded = String::with_capacity(64); - for byte in value { - encoded.push(HEX[(byte >> 4) as usize] as char); - encoded.push(HEX[(byte & 0x0f) as usize] as char); - } - Ok(encoded) -} - -fn refuse_existing(path: &Path, label: &str) -> Result<()> { - if path.exists() { - bail!( - "{label} {} already exists; replacement is forbidden", - path.display() - ); - } - Ok(()) -} - -fn reject_alias(first: &Path, second: &Path) -> Result<()> { - if normalized(first)? == normalized(second)? { - bail!("private/public or input/output paths must not alias"); - } - Ok(()) -} - -fn normalized(path: &Path) -> Result { - let absolute = if path.is_absolute() { - path.to_path_buf() - } else { - std::env::current_dir()?.join(path) - }; - let parent = absolute - .parent() - .ok_or_else(|| anyhow!("path has no parent"))?; - let parent = parent - .canonicalize() - .unwrap_or_else(|_| parent.to_path_buf()); - Ok(parent.join( - absolute - .file_name() - .ok_or_else(|| anyhow!("path has no filename"))?, - )) -} - -fn usage() -> &'static str { - "Usage: idunn-provision enroll-provider-health-identity --private-store \n idunn-provision provider-health-public-key --private-store \n idunn-provision export-provider-health-public-anchor --private-store --public-anchor \n idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision enroll-odin-topology-identity --private-store \n idunn-provision export-odin-topology-public-anchor --private-store --public-anchor \n idunn-provision enroll-deployment-brake-operator --private-store \n idunn-provision export-deployment-brake-operator-anchor --private-store --public-anchor \n idunn-provision deployment-brake-engage --store --runtime-id --owner --reason --observed-at-unix-millis \n idunn-provision deployment-brake-release --store --private-store --runtime-id --owner --reason --authorization-id --release-id --deployment-id --issued-at-unix-millis --expires-at-unix-millis \n idunn-provision deployment-brake-status --store --operator-anchor --runtime-id [--release-id --deployment-id --now-unix-millis ]\n idunn-provision lifecycle-brake-engage --store --runtime-id --target --reason --updated-at-unix-millis \n idunn-provision lifecycle-brake-release --store --runtime-id --target --reason --updated-at-unix-millis [--released-until-unix-millis ]\n idunn-provision lifecycle-brake-status --store --runtime-id --target --now-unix-millis \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision add-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision require-daemon-health-release-binding --store --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex \n idunn-provision rotate-daemon-health-trust-signer --output --binding-id --signer-public-key-hex --bound-at-unix-millis \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " -} - -#[cfg(test)] -mod tests { - use super::*; - use cultcache_rs::CacheBackingStore; - use tempfile::TempDir; - - fn invoke(args: &[&str]) -> Result<()> { - run(args.iter().map(|value| value.to_string())) - } - - fn replace_typed( - path: &Path, - key: &str, - schema: &str, - value: &T, - ) -> Result<()> { - std::fs::remove_file(path)?; - SingleFileMessagePackBackingStore::new(path).push(&CultCacheEnvelope { - key: key.into(), - r#type: T::TYPE.into(), - payload: rmp_serde::to_vec(value)?, - stored_at: "2026-07-19T20:00:00Z".into(), - schema_id: Some(schema.into()), - }) - } - - fn binding_args<'a>( - command: &'a str, - output: &'a str, - id: &'a str, - daemon: &'a str, - runtime: &'a str, - ) -> Vec<&'a str> { - vec![ - command, - "--output", - output, - "--binding-id", - id, - "--daemon", - daemon, - "--health-contract", - "provider.health", - "--source-runtime", - runtime, - "--signer-public-key-hex", - "1111111111111111111111111111111111111111111111111111111111111111", - "--bound-at-unix-millis", - "1784483100000", - "--release-binding-required", - "false", - ] - } - - const TEST_SIGNER_HEX: &str = - "1111111111111111111111111111111111111111111111111111111111111111"; - const WRONG_SIGNER_HEX: &str = - "2222222222222222222222222222222222222222222222222222222222222222"; - - fn write_test_binding( - command: &str, - store: &str, - id: &str, - daemon: &str, - runtime: &str, - ) -> Result<()> { - invoke(&binding_args(command, store, id, daemon, runtime)) - } - - fn require_test_binding( - store: &str, - id: &str, - daemon: &str, - contract: &str, - runtime: &str, - signer: &str, - ) -> Result<()> { - invoke(&[ - "require-daemon-health-release-binding", - "--store", - store, - "--binding-id", - id, - "--daemon", - daemon, - "--health-contract", - contract, - "--source-runtime", - runtime, - "--signer-public-key-hex", - signer, - ]) - } - - fn store_snapshot(path: &Path) -> Result> { - SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot() - } - - #[test] - fn append_health_binding_preserves_existing_and_rejects_collisions_and_aliens() -> Result<()> { - let temp = TempDir::new()?; - let store = temp.path().join("trust.cc"); - let path = store.to_str().unwrap(); - invoke(&binding_args( - "create-daemon-health-trust-binding", - path, - "one", - "daemon-one", - "runtime-one", - ))?; - let before = SingleFileMessagePackBackingStore::new(&store) - .pull_all_read_only_snapshot()? - .remove(0) - .payload; - invoke(&binding_args( - "add-daemon-health-trust-binding", - path, - "two", - "daemon-two", - "runtime-two", - ))?; - let entries = - SingleFileMessagePackBackingStore::new(&store).pull_all_read_only_snapshot()?; - assert_eq!(entries.len(), 2); - assert_eq!( - entries - .iter() - .find(|entry| entry.key == "one") - .unwrap() - .payload, - before - ); - assert!( - invoke(&binding_args( - "add-daemon-health-trust-binding", - path, - "two", - "daemon-three", - "runtime-three" - )) - .is_err() - ); - assert!( - invoke(&binding_args( - "add-daemon-health-trust-binding", - path, - "three", - "daemon-one", - "runtime-one" - )) - .is_err() - ); - SingleFileMessagePackBackingStore::new(&store).push(&CultCacheEnvelope { - key: "alien".into(), - r#type: "alien".into(), - payload: vec![], - stored_at: "2026-07-19T20:00:00Z".into(), - schema_id: Some("alien.v0".into()), - })?; - assert!( - invoke(&binding_args( - "add-daemon-health-trust-binding", - path, - "four", - "daemon-four", - "runtime-four" - )) - .is_err() - ); - Ok(()) - } - - #[test] - fn require_release_binding_changes_only_the_named_flag_and_is_idempotent() -> Result<()> { - let temp = TempDir::new()?; - let store = temp.path().join("trust.cc"); - let path = store.to_str().unwrap(); - write_test_binding( - "create-daemon-health-trust-binding", - path, - "one", - "daemon-one", - "runtime-one", - )?; - write_test_binding( - "add-daemon-health-trust-binding", - path, - "two", - "daemon-two", - "runtime-two", - )?; - let before = store_snapshot(&store)?; - - require_test_binding( - path, - "one", - "daemon-one", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - )?; - let after = store_snapshot(&store)?; - let before_target = before.iter().find(|entry| entry.key == "one").unwrap(); - let after_target = after.iter().find(|entry| entry.key == "one").unwrap(); - let mut expected: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&before_target.payload)?; - expected.release_binding_required = true; - assert_eq!( - rmp_serde::from_slice::(&after_target.payload)?, - expected - ); - let mut expected_envelope = before_target.clone(); - expected_envelope.payload = after_target.payload.clone(); - assert_eq!(after_target, &expected_envelope); - assert_eq!( - after.iter().find(|entry| entry.key == "two"), - before.iter().find(|entry| entry.key == "two") - ); - - require_test_binding( - path, - "one", - "daemon-one", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - )?; - assert_eq!(store_snapshot(&store)?, after); - require_health_binding_release_from_snapshot( - &SingleFileMessagePackBackingStore::new(&store), - before, - "one", - "daemon-one", - "provider.health", - "runtime-one", - &decode_public_key(TEST_SIGNER_HEX)?, - )?; - Ok(()) - } - - #[test] - fn require_release_binding_refuses_wrong_identity_and_invalid_store_without_writing() - -> Result<()> { - let temp = TempDir::new()?; - let store = temp.path().join("trust.cc"); - let path = store.to_str().unwrap(); - write_test_binding( - "create-daemon-health-trust-binding", - path, - "one", - "daemon-one", - "runtime-one", - )?; - let before = store_snapshot(&store)?; - for (id, daemon, contract, runtime, signer) in [ - ( - "one", - "wrong", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - ), - ("one", "daemon-one", "wrong", "runtime-one", TEST_SIGNER_HEX), - ( - "one", - "daemon-one", - "provider.health", - "wrong", - TEST_SIGNER_HEX, - ), - ( - "one", - "daemon-one", - "provider.health", - "runtime-one", - WRONG_SIGNER_HEX, - ), - ( - "missing", - "daemon-one", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - ), - ] { - assert!(require_test_binding(path, id, daemon, contract, runtime, signer).is_err()); - } - assert!( - invoke(&[ - "require-daemon-health-release-binding", - "--store", - path, - "--binding-id", - "one", - "--daemon", - "daemon-one", - "--health-contract", - "provider.health", - "--signer-public-key-hex", - TEST_SIGNER_HEX, - ]) - .is_err() - ); - assert_eq!(store_snapshot(&store)?, before); - - SingleFileMessagePackBackingStore::new(&store).push(&CultCacheEnvelope { - key: "alien".into(), - r#type: "alien".into(), - payload: vec![], - stored_at: "2026-07-19T20:00:00Z".into(), - schema_id: Some("alien.v0".into()), - })?; - let foreign = store_snapshot(&store)?; - assert!( - require_test_binding( - path, - "one", - "daemon-one", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - ) - .is_err() - ); - assert_eq!(store_snapshot(&store)?, foreign); - - let ambiguous = temp.path().join("ambiguous.cc"); - let ambiguous_path = ambiguous.to_str().unwrap(); - write_test_binding( - "create-daemon-health-trust-binding", - ambiguous_path, - "one", - "daemon-one", - "runtime-one", - )?; - let mut duplicate: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&store_snapshot(&ambiguous)?.remove(0).payload)?; - duplicate.binding_id = "duplicate".into(); - SingleFileMessagePackBackingStore::new(&ambiguous).push(&typed_envelope( - &duplicate.binding_id, - &duplicate, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - duplicate.bound_at_unix_millis, - )?)?; - let ambiguous_before = store_snapshot(&ambiguous)?; - assert!( - require_test_binding( - ambiguous_path, - "one", - "daemon-one", - "provider.health", - "runtime-one", - TEST_SIGNER_HEX, - ) - .is_err() - ); - assert_eq!(store_snapshot(&ambiguous)?, ambiguous_before); - Ok(()) - } - - #[test] - fn require_release_binding_cas_covers_the_whole_validated_store() -> Result<()> { - let temp = TempDir::new()?; - let store_path = temp.path().join("trust.cc"); - let path = store_path.to_str().unwrap(); - write_test_binding( - "create-daemon-health-trust-binding", - path, - "one", - "daemon-one", - "runtime-one", - )?; - write_test_binding( - "add-daemon-health-trust-binding", - path, - "two", - "daemon-two", - "runtime-two", - )?; - let stale = store_snapshot(&store_path)?; - require_test_binding( - path, - "two", - "daemon-two", - "provider.health", - "runtime-two", - TEST_SIGNER_HEX, - )?; - - let store = SingleFileMessagePackBackingStore::new(&store_path); - let error = require_health_binding_release_from_snapshot( - &store, - stale, - "one", - "daemon-one", - "provider.health", - "runtime-one", - &decode_public_key(TEST_SIGNER_HEX)?, - ) - .unwrap_err(); - assert!(error.to_string().contains("changed during release-binding")); - let records = validated_health_bindings(&store_snapshot(&store_path)?)?; - assert!( - !records - .iter() - .find(|binding| binding.binding_id == "one") - .unwrap() - .release_binding_required - ); - assert!( - records - .iter() - .find(|binding| binding.binding_id == "two") - .unwrap() - .release_binding_required - ); - Ok(()) - } - - #[test] - fn concurrent_health_binding_appends_have_one_cas_winner() -> Result<()> { - let temp = TempDir::new()?; - let store = temp.path().join("trust.cc"); - let path = store.to_str().unwrap().to_string(); - invoke(&binding_args( - "create-daemon-health-trust-binding", - &path, - "one", - "daemon-one", - "runtime-one", - ))?; - let barrier = std::sync::Arc::new(std::sync::Barrier::new(3)); - let workers = [ - ("two", "daemon-two", "runtime-two"), - ("three", "daemon-three", "runtime-three"), - ] - .map(|(id, daemon, runtime)| { - let path = path.clone(); - let barrier = barrier.clone(); - std::thread::spawn(move || { - barrier.wait(); - invoke(&binding_args( - "add-daemon-health-trust-binding", - &path, - id, - daemon, - runtime, - )) - }) - }); - barrier.wait(); - let wins = workers - .into_iter() - .map(|worker| worker.join().unwrap()) - .filter(Result::is_ok) - .count(); - assert!((1..=2).contains(&wins)); - assert_eq!( - SingleFileMessagePackBackingStore::new(&store) - .pull_all_read_only_snapshot()? - .len(), - 1 + wins - ); - Ok(()) - } - - #[test] - fn provider_health_identity_enrollment_is_profile_exact_and_immutable() -> Result<()> { - let temp = TempDir::new()?; - let private = temp.path().join("provider-health-identity.cc"); - let options = BTreeMap::from([( - "private-store".to_string(), - private.to_string_lossy().into_owned(), - )]); - - let public_key_hex = enroll_provider_health_identity(&options)?; - let signer = open_service_identity_at::(&private)?; - assert_eq!( - public_key_hex, - encode_public_key(&signer.entry().public_key)? - ); - assert_eq!( - decode_public_key(&public_key_hex)?, - signer.entry().public_key - ); - - let entries = - SingleFileMessagePackBackingStore::new(&private).pull_all_read_only_snapshot()?; - let [entry] = entries.as_slice() else { - panic!("provider health identity store must contain exactly one entry"); - }; - assert_eq!(entry.r#type, GameCultProviderHealthIdentity::PRIVATE_TYPE); - assert_eq!(entry.key, GameCultProviderHealthIdentity::PRIVATE_KEY); - assert_eq!( - entry.schema_id.as_deref(), - Some(GameCultProviderHealthIdentity::PRIVATE_SCHEMA) - ); - assert!(open_service_identity_at::(&private).is_err()); - - let before = std::fs::read(&private)?; - assert!( - invoke(&[ - "enroll-provider-health-identity", - "--private-store", - private.to_str().unwrap(), - ]) - .is_err() - ); - assert_eq!(std::fs::read(&private)?, before); - - let unauthorized = temp.path().join("unauthorized.cc"); - assert!( - invoke(&[ - "enroll-provider-health-identity", - "--private-store", - unauthorized.to_str().unwrap(), - "--release-binding-required", - "true", - ]) - .is_err() - ); - assert!(!unauthorized.exists()); - Ok(()) - } - - #[test] - fn provider_health_public_key_is_read_only_and_rejects_wrong_profile_or_options() -> Result<()> - { - let temp = TempDir::new()?; - let provider = temp.path().join("provider-health.cc"); - let provider_options = BTreeMap::from([( - "private-store".to_string(), - provider.to_string_lossy().into_owned(), - )]); - let expected = enroll_provider_health_identity(&provider_options)?; - let provider_before = std::fs::read(&provider)?; - assert_eq!(provider_health_public_key(&provider_options)?, expected); - assert_eq!(std::fs::read(&provider)?, provider_before); - - let wrong_profile = temp.path().join("idunn.cc"); - enroll_service_identity_at::(&wrong_profile)?; - let wrong_before = std::fs::read(&wrong_profile)?; - assert!( - invoke(&[ - "provider-health-public-key", - "--private-store", - wrong_profile.to_str().unwrap(), - ]) - .is_err() - ); - assert_eq!(std::fs::read(&wrong_profile)?, wrong_before); - - assert!( - invoke(&[ - "provider-health-public-key", - "--private-store", - provider.to_str().unwrap(), - "--release-binding-required", - "true", - ]) - .is_err() - ); - assert_eq!(std::fs::read(&provider)?, provider_before); - Ok(()) - } - - #[test] - fn provider_and_odin_anchor_exports_are_profile_exact_immutable_and_non_aliasing() -> Result<()> - { - let temp = TempDir::new()?; - let provider_private = temp.path().join("provider-private.cc"); - let provider_anchor = temp.path().join("provider-anchor.cc"); - invoke(&[ - "enroll-provider-health-identity", - "--private-store", - provider_private.to_str().unwrap(), - ])?; - assert!( - invoke(&[ - "export-provider-health-public-anchor", - "--private-store", - provider_private.to_str().unwrap(), - "--public-anchor", - provider_private.to_str().unwrap(), - ]) - .is_err() - ); - invoke(&[ - "export-provider-health-public-anchor", - "--private-store", - provider_private.to_str().unwrap(), - "--public-anchor", - provider_anchor.to_str().unwrap(), - ])?; - let provider_entries = store_snapshot(&provider_anchor)?; - let [provider_envelope] = provider_entries.as_slice() else { - panic!("provider anchor store must contain exactly one envelope"); - }; - assert_eq!( - provider_envelope.r#type, - GameCultProviderHealthIdentity::TRUST_ANCHOR_TYPE - ); - assert_eq!( - provider_envelope.key, - GameCultProviderHealthIdentity::TRUST_ANCHOR_KEY - ); - assert_eq!( - provider_envelope.schema_id.as_deref(), - Some(GameCultProviderHealthIdentity::TRUST_ANCHOR_SCHEMA) - ); - let provider: ServiceIdentityTrustAnchor = - rmp_serde::from_slice(&provider_envelope.payload)?; - assert_eq!(rmp_serde::to_vec(&provider)?, provider_envelope.payload); - assert_eq!( - provider.identity_id, - derive_service_identity_id::(&provider.public_key)? - ); - assert!(open_service_identity_at::(&provider_private).is_err()); - let provider_before = std::fs::read(&provider_anchor)?; - assert!( - invoke(&[ - "export-provider-health-public-anchor", - "--private-store", - provider_private.to_str().unwrap(), - "--public-anchor", - provider_anchor.to_str().unwrap(), - ]) - .is_err() - ); - assert_eq!(std::fs::read(&provider_anchor)?, provider_before); - - let odin_private = temp.path().join("odin-private.cc"); - let odin_anchor = temp.path().join("odin-anchor.cc"); - invoke(&[ - "enroll-odin-topology-identity", - "--private-store", - odin_private.to_str().unwrap(), - ])?; - assert!(open_service_identity_at::(&odin_private).is_err()); - assert!( - invoke(&[ - "export-odin-topology-public-anchor", - "--private-store", - odin_private.to_str().unwrap(), - "--public-anchor", - odin_private.to_str().unwrap(), - ]) - .is_err() - ); - invoke(&[ - "export-odin-topology-public-anchor", - "--private-store", - odin_private.to_str().unwrap(), - "--public-anchor", - odin_anchor.to_str().unwrap(), - ])?; - let odin_entries = store_snapshot(&odin_anchor)?; - let [odin_envelope] = odin_entries.as_slice() else { - panic!("Odin anchor store must contain exactly one envelope"); - }; - assert_eq!( - odin_envelope.r#type, - OdinTopologyIdentity::TRUST_ANCHOR_TYPE - ); - assert_eq!(odin_envelope.key, OdinTopologyIdentity::TRUST_ANCHOR_KEY); - assert_eq!( - odin_envelope.schema_id.as_deref(), - Some(OdinTopologyIdentity::TRUST_ANCHOR_SCHEMA) - ); - let odin: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&odin_envelope.payload)?; - assert_eq!(rmp_serde::to_vec(&odin)?, odin_envelope.payload); - assert_eq!( - odin.identity_id, - derive_service_identity_id::(&odin.public_key)? - ); - assert_ne!(provider.identity_id, odin.identity_id); - let odin_before = std::fs::read(&odin_anchor)?; - assert!( - invoke(&[ - "export-odin-topology-public-anchor", - "--private-store", - odin_private.to_str().unwrap(), - "--public-anchor", - odin_anchor.to_str().unwrap(), - ]) - .is_err() - ); - assert_eq!(std::fs::read(&odin_anchor)?, odin_before); - - let wrong_profile_output = temp.path().join("wrong-profile.cc"); - assert!( - invoke(&[ - "export-provider-health-public-anchor", - "--private-store", - odin_private.to_str().unwrap(), - "--public-anchor", - wrong_profile_output.to_str().unwrap(), - ]) - .is_err() - ); - assert!(!wrong_profile_output.exists()); - Ok(()) - } - - #[test] - fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { - let temp = TempDir::new()?; - let private = temp.path().join("identity.cc"); - let public = temp.path().join("identity-public.cc"); - invoke(&[ - "enroll-idunn-identity", - "--private-store", - private.to_str().unwrap(), - ])?; - assert!( - invoke(&[ - "enroll-idunn-identity", - "--private-store", - private.to_str().unwrap() - ]) - .is_err() - ); - assert!( - invoke(&[ - "export-idunn-public-anchor", - "--private-store", - private.to_str().unwrap(), - "--public-anchor", - private.to_str().unwrap(), - ]) - .is_err() - ); - invoke(&[ - "export-idunn-public-anchor", - "--private-store", - private.to_str().unwrap(), - "--public-anchor", - public.to_str().unwrap(), - ])?; - assert!( - invoke(&[ - "export-idunn-public-anchor", - "--private-store", - private.to_str().unwrap(), - "--public-anchor", - public.to_str().unwrap(), - ]) - .is_err() - ); - assert!( - invoke(&[ - "enroll-idunn-identity", - "--private-store", - temp.path().join("new.cc").to_str().unwrap(), - "--private-seed", - "00", - ]) - .is_err() - ); - Ok(()) - } - - #[test] - fn health_binding_derives_identity_and_rejects_overwrite_and_corruption() -> Result<()> { - let temp = TempDir::new()?; - let output = temp.path().join("provider-binding.cc"); - let key = "07".repeat(32); - let args = [ - "create-daemon-health-trust-binding", - "--output", - output.to_str().unwrap(), - "--binding-id", - "root/epiphany/health", - "--daemon", - "yggdrasil-epiphany", - "--health-contract", - "epiphany.cultnet-rudp-runtime-health", - "--source-runtime", - "epiphany-daemon-supervisor", - "--signer-public-key-hex", - &key, - "--bound-at-unix-millis", - "1784483100000", - "--release-binding-required", - "true", - ]; - invoke(&args)?; - assert!(invoke(&args).is_err()); - let mut record = read_typed::( - &output, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - )?; - assert_eq!( - record.signer_identity_id, - derive_service_identity_id::( - &record.signer_public_key - )? - ); - record.signer_identity_id = "caller-chosen".into(); - replace_typed( - &output, - &record.binding_id, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - &record, - )?; - assert!( - invoke(&[ - "validate-daemon-health-trust-binding", - "--input", - output.to_str().unwrap() - ]) - .is_err() - ); - - record.signer_identity_id = derive_service_identity_id::( - &record.signer_public_key, - )?; - record.schema_version = "wrong.schema.v9".into(); - replace_typed( - &output, - &record.binding_id, - IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, - &record, - )?; - assert!( - invoke(&[ - "validate-daemon-health-trust-binding", - "--input", - output.to_str().unwrap() - ]) - .is_err() - ); - Ok(()) - } - - #[test] - fn projection_anchor_is_exact_profile_and_matches_low_level_anchor() -> Result<()> { - let temp = TempDir::new()?; - let private = temp.path().join("identity.cc"); - let public = temp.path().join("public.cc"); - let root = temp.path().join("root-anchor.cc"); - invoke(&[ - "enroll-idunn-identity", - "--private-store", - private.to_str().unwrap(), - ])?; - invoke(&[ - "export-idunn-public-anchor", - "--private-store", - private.to_str().unwrap(), - "--public-anchor", - public.to_str().unwrap(), - ])?; - let create = [ - "create-provider-projection-trust-anchor", - "--output", - root.to_str().unwrap(), - "--trust-anchor-id", - "root/idunn/provider-health", - "--runtime-id", - "idunn-yggdrasil", - "--idunn-public-anchor", - public.to_str().unwrap(), - "--bound-at-unix-millis", - "1784483100000", - "--expires-at-unix-millis", - "1815000000000", - ]; - invoke(&create)?; - invoke(&[ - "validate-provider-projection-trust-anchor", - "--input", - root.to_str().unwrap(), - "--idunn-public-anchor", - public.to_str().unwrap(), - ])?; - assert!(invoke(&create).is_err()); - assert!( - invoke(&[ - "validate-provider-projection-trust-anchor", - "--input", - public.to_str().unwrap(), - "--idunn-public-anchor", - public.to_str().unwrap(), - ]) - .is_err() - ); - - let mut record = read_typed::( - &root, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - )?; - record.signing_purpose = "wrong-purpose".into(); - replace_typed( - &root, - &record.trust_anchor_id, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - &record, - )?; - assert!( - invoke(&[ - "validate-provider-projection-trust-anchor", - "--input", - root.to_str().unwrap(), - "--idunn-public-anchor", - public.to_str().unwrap(), - ]) - .is_err() - ); - - record.signing_purpose = - IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(); - record.signer_public_key = vec![9; 32]; - replace_typed( - &root, - &record.trust_anchor_id, - GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, - &record, - )?; - assert!( - invoke(&[ - "validate-provider-projection-trust-anchor", - "--input", - root.to_str().unwrap(), - "--idunn-public-anchor", - public.to_str().unwrap(), - ]) - .is_err() - ); - Ok(()) - } - - #[test] - fn release_binding_policy_is_explicit_and_boolean() { - let temp = TempDir::new().unwrap(); - let output = temp.path().join("binding.cc"); - let base = [ - "create-daemon-health-trust-binding", - "--output", - output.to_str().unwrap(), - "--binding-id", - "root/provider/health", - "--daemon", - "provider", - "--health-contract", - "provider.health", - "--source-runtime", - "provider-runtime", - "--signer-public-key-hex", - "0707070707070707070707070707070707070707070707070707070707070707", - "--bound-at-unix-millis", - "1784483100000", - ]; - assert!(invoke(&base).is_err()); - let mut partial = base.to_vec(); - partial.extend(["--release-binding-required", "sometimes"]); - assert!(invoke(&partial).is_err()); - } - - #[test] - fn signer_rotation_changes_only_the_named_trust_binding_signer() -> Result<()> { - let temp = TempDir::new()?; - let output = temp.path().join("binding.cc"); - invoke(&[ - "create-daemon-health-trust-binding", - "--output", - output.to_str().unwrap(), - "--binding-id", - "heimdall-yggdrasil-provider-health", - "--daemon", - "yggdrasil-heimdall", - "--health-contract", - "heimdall.cultnet-rudp-provider-health", - "--source-runtime", - "heimdall-service", - "--signer-public-key-hex", - "0707070707070707070707070707070707070707070707070707070707070707", - "--bound-at-unix-millis", - "100", - "--release-binding-required", - "false", - ])?; - assert!( - invoke(&[ - "rotate-daemon-health-trust-signer", - "--output", - output.to_str().unwrap(), - "--binding-id", - "heimdall-yggdrasil-provider-health", - "--signer-public-key-hex", - "0808080808080808080808080808080808080808080808080808080808080808", - "--bound-at-unix-millis", - "100", - ]) - .is_err() - ); - invoke(&[ - "rotate-daemon-health-trust-signer", - "--output", - output.to_str().unwrap(), - "--binding-id", - "heimdall-yggdrasil-provider-health", - "--signer-public-key-hex", - "0808080808080808080808080808080808080808080808080808080808080808", - "--bound-at-unix-millis", - "101", - ])?; - let [envelope] = SingleFileMessagePackBackingStore::new(&output) - .pull_all_read_only_snapshot()? - .try_into() - .map_err(|_| anyhow!("expected one trust binding"))?; - let binding: IdunnDaemonHealthTrustBindingRecord = - rmp_serde::from_slice(&envelope.payload)?; - assert_eq!(binding.binding_id, "heimdall-yggdrasil-provider-health"); - assert_eq!(binding.daemon_id, "yggdrasil-heimdall"); - assert_eq!( - binding.health_contract, - "heimdall.cultnet-rudp-provider-health" - ); - assert_eq!(binding.source_runtime_id, "heimdall-service"); - assert_eq!(binding.signer_public_key, vec![8; 32]); - assert_eq!(binding.bound_at_unix_millis, 101); - assert!(!binding.release_binding_required); - validate_health_binding_store(&output) - } - - #[test] - fn brake_operator_can_issue_one_exact_bounded_rollout_grant() -> Result<()> { - let temp = TempDir::new()?; - let private = temp.path().join("operator-private.cc"); - let anchor = temp.path().join("operator-public.cc"); - let store = temp.path().join("brake.cc"); - invoke(&[ - "enroll-deployment-brake-operator", - "--private-store", - private.to_str().unwrap(), - ])?; - invoke(&[ - "export-deployment-brake-operator-anchor", - "--private-store", - private.to_str().unwrap(), - "--public-anchor", - anchor.to_str().unwrap(), - ])?; - invoke(&[ - "deployment-brake-engage", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - "--owner", - "operator/metacrat", - "--reason", - "sleep mode", - "--observed-at-unix-millis", - "100", - ])?; - assert_eq!(read_brake(&store)?.status, "engaged"); - invoke(&[ - "deployment-brake-status", - "--store", - store.to_str().unwrap(), - "--operator-anchor", - anchor.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - ])?; - assert!( - invoke(&[ - "deployment-brake-status", - "--store", - store.to_str().unwrap(), - "--operator-anchor", - anchor.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - "--release-id", - "commit-4", - "--deployment-id", - "request-4", - "--now-unix-millis", - "150", - ]) - .is_err() - ); - invoke(&[ - "deployment-brake-release", - "--store", - store.to_str().unwrap(), - "--private-store", - private.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - "--owner", - "operator/metacrat", - "--reason", - "one rollout", - "--authorization-id", - "auth/r4", - "--release-id", - "commit-4", - "--deployment-id", - "request-4", - "--issued-at-unix-millis", - "200", - "--expires-at-unix-millis", - "800", - ])?; - invoke(&[ - "deployment-brake-status", - "--store", - store.to_str().unwrap(), - "--operator-anchor", - anchor.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - "--release-id", - "commit-4", - "--deployment-id", - "request-4", - "--now-unix-millis", - "500", - ])?; - assert!( - invoke(&[ - "deployment-brake-status", - "--store", - store.to_str().unwrap(), - "--operator-anchor", - anchor.to_str().unwrap(), - "--runtime-id", - "yggdrasil", - "--release-id", - "substituted", - "--deployment-id", - "request-4", - "--now-unix-millis", - "500" - ]) - .is_err() - ); - Ok(()) - } - - #[test] - fn lifecycle_brake_is_absence_allow_and_exact_to_runtime_target_and_expiry() -> Result<()> { - let temp = TempDir::new()?; - let missing = temp.path().join("missing-lifecycle.cc"); - invoke(&[ - "lifecycle-brake-status", - "--store", - missing.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--now-unix-millis", - "100", - ])?; - assert!(!missing.exists()); - - let store = temp.path().join("ghostlight-lifecycle.cc"); - invoke(&[ - "lifecycle-brake-engage", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--reason", - "operator maintenance", - "--updated-at-unix-millis", - "200", - ])?; - let engaged = read_lifecycle_brake(&store)?.context("lifecycle brake must exist")?; - assert_eq!(engaged.status, "engaged"); - assert_eq!(engaged.runtime_id, "idunn-yggdrasil"); - assert_eq!(engaged.target, "ghostlight"); - assert!(read_brake(&store).is_err()); - assert!( - invoke(&[ - "lifecycle-brake-status", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--now-unix-millis", - "201", - ]) - .is_err() - ); - - let engaged_bytes = std::fs::read(&store)?; - assert!( - invoke(&[ - "lifecycle-brake-release", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-nightwing", - "--target", - "ghostlight", - "--reason", - "wrong runtime", - "--updated-at-unix-millis", - "300", - ]) - .is_err() - ); - assert!( - invoke(&[ - "lifecycle-brake-release", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "odin", - "--reason", - "wrong target", - "--updated-at-unix-millis", - "300", - ]) - .is_err() - ); - assert_eq!(std::fs::read(&store)?, engaged_bytes); - - invoke(&[ - "lifecycle-brake-release", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--reason", - "bounded continuity window", - "--updated-at-unix-millis", - "300", - "--released-until-unix-millis", - "500", - ])?; - invoke(&[ - "lifecycle-brake-status", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--now-unix-millis", - "499", - ])?; - for (runtime, target, now) in [ - ("idunn-nightwing", "ghostlight", "499"), - ("idunn-yggdrasil", "odin", "499"), - ("idunn-yggdrasil", "ghostlight", "500"), - ] { - assert!( - invoke(&[ - "lifecycle-brake-status", - "--store", - store.to_str().unwrap(), - "--runtime-id", - runtime, - "--target", - target, - "--now-unix-millis", - now, - ]) - .is_err() - ); - } - let released_bytes = std::fs::read(&store)?; - assert!( - invoke(&[ - "lifecycle-brake-engage", - "--store", - store.to_str().unwrap(), - "--runtime-id", - "idunn-yggdrasil", - "--target", - "ghostlight", - "--reason", - "stale replay", - "--updated-at-unix-millis", - "300", - ]) - .is_err() - ); - assert_eq!(std::fs::read(&store)?, released_bytes); - Ok(()) - } - - #[test] - fn brake_compare_exchange_rejects_stale_transition_snapshot() -> Result<()> { - let temp = TempDir::new()?; - let store_path = temp.path().join("brake.cc"); - let base = IdunnDeploymentBrakeRecord { - schema_version: IDUNN_DEPLOYMENT_BRAKE_SCHEMA.into(), - brake_id: IDUNN_DEPLOYMENT_BRAKE_ID.into(), - authority: IDUNN_DEPLOYMENT_BRAKE_AUTHORITY.into(), - runtime_id: "yggdrasil".into(), - status: "engaged".into(), - scope: IDUNN_DEPLOYMENT_BRAKE_SCOPE.into(), - reason: "first".into(), - observed_at_unix_millis: 100, - expires_at_unix_millis: None, - authorization_id: None, - authorization_purpose: None, - authorized_release_id: None, - authorized_deployment_id: None, - authorized_by: None, - authorization_issued_at_unix_millis: None, - authorization_expires_at_unix_millis: None, - signature_algorithm: None, - signature: None, - private_state_exposed: false, - updated_by: "operator/a".into(), - }; - replace_brake(&store_path, base.clone(), 100)?; - let stale = SingleFileMessagePackBackingStore::new(&store_path) - .pull_all_read_only_snapshot()? - .remove(0); - let mut changed = base.clone(); - changed.reason = "racing writer".into(); - changed.observed_at_unix_millis = 101; - replace_brake(&store_path, changed, 101)?; - let candidate = typed_envelope( - IDUNN_DEPLOYMENT_BRAKE_ID, - &base, - IDUNN_DEPLOYMENT_BRAKE_SCHEMA, - 102, - )?; - assert!( - !SingleFileMessagePackBackingStore::new(&store_path).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: IdunnDeploymentBrakeRecord::TYPE.into(), - key: IDUNN_DEPLOYMENT_BRAKE_ID.into(), - current: Some(stale) - }], - &[candidate] - )? - ); - Ok(()) - } -} diff --git a/crates/idunn-daemon/tests/fixtures/README.md b/crates/idunn-daemon/tests/fixtures/README.md deleted file mode 100644 index 70060bf..0000000 --- a/crates/idunn-daemon/tests/fixtures/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Bifrost release-authority wire fixture - -`bifrost-release-authority-v1.payload.hex` is the exact MessagePack payload -emitted on 2026-07-16 by Bifrost's TypeScript CultCache writer -`tools/bifrost-repository-release-authority.mjs` for `GameCult/Epiphany`, -`refs/heads/main`, commit -`0123456789abcdef0123456789abcdef01234567`. - -It is checked in because the TypeScript runtime emits a camelCase MessagePack -map while Rust `DatabaseEntry` derives tuple payloads. Idunn must prove it can -decode the producer's real wire shape rather than round-tripping a fixture it -serialized itself. diff --git a/crates/idunn-daemon/tests/fixtures/bifrost-release-authority-v1.payload.hex b/crates/idunn-daemon/tests/fixtures/bifrost-release-authority-v1.payload.hex deleted file mode 100644 index e67843a..0000000 --- a/crates/idunn-daemon/tests/fixtures/bifrost-release-authority-v1.payload.hex +++ /dev/null @@ -1 +0,0 @@ -de0018aa736368656d614e616d65d924626966726f73742e7265706f7369746f72795f72656c656173655f617574686f72697479ad736368656d6156657273696f6ed927626966726f73742e7265706f7369746f72795f72656c656173655f617574686f726974792e7631ab617574686f726974794964d95272656c656173653a47616d6543756c742f4570697068616e793a726566732f68656164732f6d61696e3a30313233343536373839616263646566303132333435363738396162636465663031323334353637a9636f6d6d616e644964d92163756c746d6573682d636f6d6d616e642d6570697068616e792d72656c65617365b163726f7373696e67526563656970744964b963726f7373696e675f6570697068616e795f72656c65617365b27265706f7369746f727946756c6c4e616d65b147616d6543756c742f4570697068616e79ab757073747265616d526566af726566732f68656164732f6d61696ea9636f6d6d6974536861d92830313233343536373839616263646566303132333435363738396162636465663031323334353637a86465636973696f6ea9617574686f72697a65a6737461747573aa617574686f72697a6564b0706f6c6963794465636973696f6e4964b7706f6c6963792d6570697068616e792d72656c65617365b2617574686f726974795265666572656e6365b8626966726f73743a72656c656173653a6570697068616e79ad6163746f724964656e74697479a86d65746163726174aa736f757263654b696e64b8626966726f73742e676f7665726e616e63652e746f706963a8736f757263654964b6746f7069632d6570697068616e792d72656c65617365ad6570697068616e7952756e4964b472756e2d6570697068616e792d72656c65617365ae6570697068616e794c616e654964b168616e64732d7075626c69636174696f6eb56570697068616e794167656e744964656e74697479a86570697068616e79b265787465726e616c5265636569707455726cd95468747470733a2f2f6769746875622e636f6d2f47616d6543756c742f4570697068616e792f636f6d6d69742f30313233343536373839616263646566303132333435363738396162636465663031323334353637b165787465726e616c526563656970744964d92830313233343536373839616263646566303132333435363738396162636465663031323334353637ac617574686f72697a65644174b4323032362d30372d31365430303a30303a30305aa9657870697265734174a0a97265766f6b65644174a0b07265766f636174696f6e526561736f6ea0 diff --git a/crates/muninn-daemon/Cargo.toml b/crates/muninn-daemon/Cargo.toml index 75e16cf..9a2fc33 100644 --- a/crates/muninn-daemon/Cargo.toml +++ b/crates/muninn-daemon/Cargo.toml @@ -15,7 +15,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" -cultmesh-rs = { path = "../../vendor/cultmesh-rs" } +cultmesh-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultmesh-rs" } cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } libc = "0.2" muninn-move-tracker = { path = "../muninn-move-tracker" } diff --git a/crates/odin-core/Cargo.toml b/crates/odin-core/Cargo.toml index 74c10f0..3d62c96 100644 --- a/crates/odin-core/Cargo.toml +++ b/crates/odin-core/Cargo.toml @@ -12,7 +12,7 @@ path = "src/lib.rs" [dependencies] anyhow = "1" cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } -cultmesh-rs = { path = "../../vendor/cultmesh-rs" } +cultmesh-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultmesh-rs" } serde_json = "1" cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } serde = { version = "1", features = ["derive"] } diff --git a/crates/odin-daemon/Cargo.toml b/crates/odin-daemon/Cargo.toml index 7b78846..a7c0dac 100644 --- a/crates/odin-daemon/Cargo.toml +++ b/crates/odin-daemon/Cargo.toml @@ -13,7 +13,7 @@ path = "src/lib.rs" anyhow = "1" chrono = "0.4" cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } -cultmesh-rs = { path = "../../vendor/cultmesh-rs" } +cultmesh-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultmesh-rs" } cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } fs2 = "0.4" rmp-serde = "1.3" diff --git a/crates/sleipnir-daemon/Cargo.toml b/crates/sleipnir-daemon/Cargo.toml index 71fcba5..6179ff5 100644 --- a/crates/sleipnir-daemon/Cargo.toml +++ b/crates/sleipnir-daemon/Cargo.toml @@ -11,7 +11,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" -cultmesh-rs = { path = "../../vendor/cultmesh-rs" } +cultmesh-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultmesh-rs" } cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } odin-core = { path = "../odin-core" } diff --git a/vendor/cultmesh-rs/Cargo.lock b/vendor/cultmesh-rs/Cargo.lock deleted file mode 100644 index 7f68a9b..0000000 --- a/vendor/cultmesh-rs/Cargo.lock +++ /dev/null @@ -1,1360 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "aead" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" -dependencies = [ - "crypto-common", - "generic-array", -] - -[[package]] -name = "aes" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" -dependencies = [ - "cfg-if", - "cipher", - "cpufeatures", -] - -[[package]] -name = "aes-gcm" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" -dependencies = [ - "aead", - "aes", - "cipher", - "ctr", - "ghash", - "subtle", -] - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "anyhow" -version = "1.0.102" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" - -[[package]] -name = "autocfg" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" - -[[package]] -name = "base64" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" - -[[package]] -name = "base64ct" -version = "1.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" - -[[package]] -name = "bitflags" -version = "2.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84d7ced0ae9557296835c32bf1b1e02b44c746701f898460fb000d7eaa84f00a" - -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - -[[package]] -name = "bumpalo" -version = "3.20.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" - -[[package]] -name = "cc" -version = "1.2.62" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98" -dependencies = [ - "find-msvc-tools", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[package]] -name = "chrono" -version = "0.4.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" -dependencies = [ - "iana-time-zone", - "js-sys", - "num-traits", - "serde", - "wasm-bindgen", - "windows-link", -] - -[[package]] -name = "cipher" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" -dependencies = [ - "crypto-common", - "inout", -] - -[[package]] -name = "const-oid" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" - -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - -[[package]] -name = "cpufeatures" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" -dependencies = [ - "libc", -] - -[[package]] -name = "crypto-common" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" -dependencies = [ - "generic-array", - "rand_core 0.6.4", - "typenum", -] - -[[package]] -name = "ctr" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" -dependencies = [ - "cipher", -] - -[[package]] -name = "cultcache-rs" -version = "0.1.0" -dependencies = [ - "anyhow", - "chrono", - "cultcache-rs-derive", - "fs2", - "rmp-serde", - "serde", - "serde_bytes", - "serde_json", - "uuid", - "windows-sys 0.61.2", -] - -[[package]] -name = "cultcache-rs-derive" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "cultmesh-rs" -version = "0.1.0" -dependencies = [ - "anyhow", - "cultcache-rs", - "cultnet-rs", - "pretty_assertions", - "rmp-serde", - "serde", - "tempfile", -] - -[[package]] -name = "cultnet-rs" -version = "0.1.0" -dependencies = [ - "aes-gcm", - "anyhow", - "base64", - "chrono", - "cultcache-rs", - "ed25519-dalek", - "hmac", - "rand", - "rmp-serde", - "rmpv", - "serde", - "serde_bytes", - "serde_json", - "sha2", - "socket2", - "uuid", - "windows-sys 0.61.2", - "zeroize", -] - -[[package]] -name = "curve25519-dalek" -version = "4.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" -dependencies = [ - "cfg-if", - "cpufeatures", - "curve25519-dalek-derive", - "digest", - "fiat-crypto", - "rustc_version", - "subtle", - "zeroize", -] - -[[package]] -name = "curve25519-dalek-derive" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "der" -version = "0.7.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" -dependencies = [ - "const-oid", - "zeroize", -] - -[[package]] -name = "diff" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", - "subtle", -] - -[[package]] -name = "ed25519" -version = "2.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" -dependencies = [ - "pkcs8", - "signature", -] - -[[package]] -name = "ed25519-dalek" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" -dependencies = [ - "curve25519-dalek", - "ed25519", - "serde", - "sha2", - "subtle", - "zeroize", -] - -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "errno" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" -dependencies = [ - "libc", - "windows-sys 0.61.2", -] - -[[package]] -name = "fastrand" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" - -[[package]] -name = "fiat-crypto" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" - -[[package]] -name = "find-msvc-tools" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" - -[[package]] -name = "foldhash" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" - -[[package]] -name = "fs2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "futures-core" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" - -[[package]] -name = "futures-task" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" - -[[package]] -name = "futures-util" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" -dependencies = [ - "futures-core", - "futures-task", - "pin-project-lite", - "slab", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "getrandom" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - -[[package]] -name = "getrandom" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" -dependencies = [ - "cfg-if", - "libc", - "r-efi 5.3.0", - "wasip2", -] - -[[package]] -name = "getrandom" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" -dependencies = [ - "cfg-if", - "libc", - "r-efi 6.0.0", - "wasip2", - "wasip3", -] - -[[package]] -name = "ghash" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" -dependencies = [ - "opaque-debug", - "polyval", -] - -[[package]] -name = "hashbrown" -version = "0.15.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" -dependencies = [ - "foldhash", -] - -[[package]] -name = "hashbrown" -version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "hmac" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" -dependencies = [ - "digest", -] - -[[package]] -name = "iana-time-zone" -version = "0.1.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - -[[package]] -name = "id-arena" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" - -[[package]] -name = "indexmap" -version = "2.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" -dependencies = [ - "equivalent", - "hashbrown 0.17.1", - "serde", - "serde_core", -] - -[[package]] -name = "inout" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" -dependencies = [ - "generic-array", -] - -[[package]] -name = "itoa" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" - -[[package]] -name = "js-sys" -version = "0.3.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "142bc4740e452c1e57ade0cbc129f139c9093e354346f0872ef985f4f5cf5f11" -dependencies = [ - "cfg-if", - "futures-util", - "once_cell", - "wasm-bindgen", -] - -[[package]] -name = "leb128fmt" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" - -[[package]] -name = "libc" -version = "0.2.186" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" - -[[package]] -name = "linux-raw-sys" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" - -[[package]] -name = "log" -version = "0.4.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5" - -[[package]] -name = "memchr" -version = "2.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8" - -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - -[[package]] -name = "opaque-debug" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" - -[[package]] -name = "pin-project-lite" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" - -[[package]] -name = "pkcs8" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" -dependencies = [ - "der", - "spki", -] - -[[package]] -name = "polyval" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" -dependencies = [ - "cfg-if", - "cpufeatures", - "opaque-debug", - "universal-hash", -] - -[[package]] -name = "ppv-lite86" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" -dependencies = [ - "zerocopy", -] - -[[package]] -name = "pretty_assertions" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" -dependencies = [ - "diff", - "yansi", -] - -[[package]] -name = "prettyplease" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" -dependencies = [ - "proc-macro2", - "syn", -] - -[[package]] -name = "proc-macro2" -version = "1.0.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "r-efi" -version = "5.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" - -[[package]] -name = "r-efi" -version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" - -[[package]] -name = "rand" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" -dependencies = [ - "rand_chacha", - "rand_core 0.9.5", -] - -[[package]] -name = "rand_chacha" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" -dependencies = [ - "ppv-lite86", - "rand_core 0.9.5", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom 0.2.17", -] - -[[package]] -name = "rand_core" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" -dependencies = [ - "getrandom 0.3.4", -] - -[[package]] -name = "rmp" -version = "0.8.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ba8be72d372b2c9b35542551678538b562e7cf86c3315773cae48dfbfe7790c" -dependencies = [ - "num-traits", -] - -[[package]] -name = "rmp-serde" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f81bee8c8ef9b577d1681a70ebbc962c232461e397b22c208c43c04b67a155" -dependencies = [ - "rmp", - "serde", -] - -[[package]] -name = "rmpv" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a4e1d4b9b938a26d2996af33229f0ca0956c652c1375067f0b45291c1df8417" -dependencies = [ - "rmp", - "serde", - "serde_bytes", -] - -[[package]] -name = "rustc_version" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" -dependencies = [ - "semver", -] - -[[package]] -name = "rustix" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.61.2", -] - -[[package]] -name = "rustversion" -version = "1.0.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" - -[[package]] -name = "semver" -version = "1.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" - -[[package]] -name = "serde" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" -dependencies = [ - "serde_core", - "serde_derive", -] - -[[package]] -name = "serde_bytes" -version = "0.11.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" -dependencies = [ - "serde", - "serde_core", -] - -[[package]] -name = "serde_core" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.150" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" -dependencies = [ - "itoa", - "memchr", - "serde", - "serde_core", - "zmij", -] - -[[package]] -name = "sha2" -version = "0.10.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "signature" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" -dependencies = [ - "rand_core 0.6.4", -] - -[[package]] -name = "slab" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" - -[[package]] -name = "socket2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "spki" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" -dependencies = [ - "base64ct", - "der", -] - -[[package]] -name = "subtle" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" - -[[package]] -name = "syn" -version = "2.0.117" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "tempfile" -version = "3.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" -dependencies = [ - "fastrand", - "getrandom 0.4.2", - "once_cell", - "rustix", - "windows-sys 0.61.2", -] - -[[package]] -name = "typenum" -version = "1.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" - -[[package]] -name = "unicode-ident" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" - -[[package]] -name = "unicode-xid" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" - -[[package]] -name = "universal-hash" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" -dependencies = [ - "crypto-common", - "subtle", -] - -[[package]] -name = "uuid" -version = "1.23.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d258b83ceec21034727ecee8c382cfa6c3e133699b0742c64571814fb420c9f7" -dependencies = [ - "getrandom 0.4.2", - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - -[[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" - -[[package]] -name = "wasip2" -version = "1.0.3+wasi-0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" -dependencies = [ - "wit-bindgen 0.57.1", -] - -[[package]] -name = "wasip3" -version = "0.4.0+wasi-0.3.0-rc-2026-01-06" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" -dependencies = [ - "wit-bindgen 0.51.0", -] - -[[package]] -name = "wasm-bindgen" -version = "0.2.122" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed04576f974d2b2fba0f38c51dbc5518011e38c36bf1143164be765528fd409" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.122" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "916151b09da36bd82f6615cbf3a419e2f0ba23a03c6160e8e92eb6bd4aa1dec6" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.122" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "299047362ccbfce148b67ab7e73349f77748e00c8296f9542adfad2ad82c5c5e" -dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.122" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a929b2c61f11ba3e9bc35b50c1f25cb38e0e892c0c231ae2b8cf78d5dad4437" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "wasm-encoder" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" -dependencies = [ - "leb128fmt", - "wasmparser", -] - -[[package]] -name = "wasm-metadata" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" -dependencies = [ - "anyhow", - "indexmap", - "wasm-encoder", - "wasmparser", -] - -[[package]] -name = "wasmparser" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" -dependencies = [ - "bitflags", - "hashbrown 0.15.5", - "indexmap", - "semver", -] - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-core" -version = "0.62.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" -dependencies = [ - "windows-implement", - "windows-interface", - "windows-link", - "windows-result", - "windows-strings", -] - -[[package]] -name = "windows-implement" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "windows-interface" -version = "0.59.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" - -[[package]] -name = "windows-result" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-strings" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-sys" -version = "0.61.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "wit-bindgen" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" -dependencies = [ - "wit-bindgen-rust-macro", -] - -[[package]] -name = "wit-bindgen" -version = "0.57.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" - -[[package]] -name = "wit-bindgen-core" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" -dependencies = [ - "anyhow", - "heck", - "wit-parser", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" -dependencies = [ - "anyhow", - "heck", - "indexmap", - "prettyplease", - "syn", - "wasm-metadata", - "wit-bindgen-core", - "wit-component", -] - -[[package]] -name = "wit-bindgen-rust-macro" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" -dependencies = [ - "anyhow", - "prettyplease", - "proc-macro2", - "quote", - "syn", - "wit-bindgen-core", - "wit-bindgen-rust", -] - -[[package]] -name = "wit-component" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" -dependencies = [ - "anyhow", - "bitflags", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder", - "wasm-metadata", - "wasmparser", - "wit-parser", -] - -[[package]] -name = "wit-parser" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser", -] - -[[package]] -name = "yansi" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" - -[[package]] -name = "zerocopy" -version = "0.8.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b065d4f0e55f82fae73202e189638116a87c55ab6b8e6c2721e13dd9d854ad1" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b631b19d36a892ab55420c92dbc83ccd79274f25be714855d3074aa71cab639" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "zeroize" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" - -[[package]] -name = "zmij" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/vendor/cultmesh-rs/Cargo.toml b/vendor/cultmesh-rs/Cargo.toml deleted file mode 100644 index 741c6d8..0000000 --- a/vendor/cultmesh-rs/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "cultmesh-rs" -version = "0.1.0" -edition = "2024" -publish = false - -[lib] -name = "cultmesh_rs" -path = "src/lib.rs" - -[dependencies] -anyhow = "1" -cultcache-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultcache-rs" } -cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "c13b6ba07e99f2535b455da74d07d88c308731f5", package = "cultnet-rs" } -serde = { version = "1", features = ["derive"] } - -[dev-dependencies] -pretty_assertions = "1.4.1" -rmp-serde = "1" -tempfile = "3" diff --git a/vendor/cultmesh-rs/src/lib.rs b/vendor/cultmesh-rs/src/lib.rs deleted file mode 100644 index 708198f..0000000 --- a/vendor/cultmesh-rs/src/lib.rs +++ /dev/null @@ -1,1557 +0,0 @@ -use anyhow::{Context, Result}; -use cultcache_rs::CultCache; -use cultcache_rs::CultCacheEnvelope; -use cultcache_rs::DatabaseEntry; -use cultcache_rs::SingleFileMessagePackBackingStore; -use cultnet_rs::CultNetDocumentPutOptions; -use cultnet_rs::CultNetDocumentRegistry; -use cultnet_rs::CultNetRudpSocketTransportConnection; -use cultnet_rs::CultNetRudpSocketTransportOptions; -use cultnet_rs::CultNetWireContract; -use cultnet_rs::decode_cultnet_message_from_slice; -use cultnet_rs::encode_cultnet_message_to_vec; -use serde::Serialize; -use std::collections::BTreeMap; -use std::collections::hash_map::RandomState; -use std::env; -use std::hash::{BuildHasher, Hasher}; -use std::net::SocketAddr; -use std::net::UdpSocket; -use std::path::Path; -use std::thread; -use std::time::Duration; -use std::time::Instant; - -mod rudp_document_server; - -pub use rudp_document_server::*; - -pub const CULTMESH_RUDP_DOCUMENT_CATALOG_CONNECTION_ID: u32 = 0x0d1d_0002; - -pub trait CultMeshDocumentSet: Clone + Send + Sync + 'static { - fn register_cache(&self, cache: &mut CultCache) -> Result<()>; - fn register_documents(&self, registry: &mut CultNetDocumentRegistry) -> Result<()>; -} - -#[macro_export] -macro_rules! cultmesh_documents { - ($name:ident { $($entry:ty => $schema_version:expr),* $(,)? }) => { - #[derive(Clone, Copy, Debug, Default)] - pub struct $name; - - impl $crate::CultMeshDocumentSet for $name { - fn register_cache( - &self, - cache: &mut cultcache_rs::CultCache, - ) -> ::anyhow::Result<()> { - $( - cache.register_entry_type::<$entry>()?; - )* - Ok(()) - } - - fn register_documents( - &self, - registry: &mut cultnet_rs::CultNetDocumentRegistry, - ) -> ::anyhow::Result<()> { - $( - registry.register( - cultnet_rs::CultNetDocumentBinding::for_entry_with_schema_id::<$entry>( - $schema_version.to_string(), - $schema_version.to_string(), - ), - ); - )* - Ok(()) - } - } - }; -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshNodeOptions { - pub runtime_id: String, - pub pull_on_start: bool, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpDocumentPublishOptions { - pub target: SocketAddr, - pub runtime_id: String, - pub connect_timeout: Duration, - pub flush_timeout: Duration, - pub poll_interval: Duration, - pub resend_delay_ms: u64, - pub source_agent_id: Option, - pub source_role: Option, - pub tags: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpSnapshotOptions { - pub target: SocketAddr, - pub runtime_id: String, - /// Exact correlation id for the SnapshotRequest. Raw callers must supply - /// this; `CultMeshNode::pull_rudp_catalog_snapshot` generates one when it - /// owns the request. - pub message_id: String, - pub connect_timeout: Duration, - pub response_timeout: Duration, - pub poll_interval: Duration, - pub resend_delay_ms: u64, - pub schema_ids: Option>, - pub record_keys: Option>, -} - -impl CultMeshRudpSnapshotOptions { - pub fn odin(target: SocketAddr, runtime_id: impl Into) -> Self { - Self { - target, - runtime_id: runtime_id.into(), - ..Self::default() - } - } -} - -impl Default for CultMeshRudpSnapshotOptions { - fn default() -> Self { - Self { - target: SocketAddr::from(([0, 0, 0, 0], 0)), - runtime_id: "cultmesh-rudp-snapshot-client".to_string(), - message_id: String::new(), - connect_timeout: Duration::from_secs(3), - response_timeout: Duration::from_secs(3), - poll_interval: Duration::from_millis(10), - resend_delay_ms: 50, - schema_ids: None, - record_keys: None, - } - } -} - -impl CultMeshRudpDocumentPublishOptions { - pub fn odin(target: SocketAddr, runtime_id: impl Into) -> Self { - Self { - target, - runtime_id: runtime_id.into(), - ..Self::default() - } - } -} - -impl Default for CultMeshRudpDocumentPublishOptions { - fn default() -> Self { - Self { - target: SocketAddr::from(([0, 0, 0, 0], 0)), - runtime_id: "cultmesh-rudp-document-publisher".to_string(), - connect_timeout: Duration::from_secs(3), - flush_timeout: Duration::from_millis(300), - poll_interval: Duration::from_millis(10), - resend_delay_ms: 50, - source_agent_id: None, - source_role: None, - tags: Vec::new(), - } - } -} - -impl Default for CultMeshNodeOptions { - fn default() -> Self { - Self { - runtime_id: "cultmesh-local".to_string(), - pull_on_start: true, - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpEndpoint { - pub host: String, - pub port: u16, - pub addr: SocketAddr, -} - -pub struct CultMeshNode { - runtime_id: String, - cache: CultCache, - documents: CultNetDocumentRegistry, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultMeshStreamKind { - Audio, - Video, - Tensor, - Bytes, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultMeshStreamBodyTransport { - SharedMemory, - SharedD3d12Texture, - SharedD3d11Texture, - DmaBuf, - IoSurface, - AHardwareBuffer, - CultCachePage, - InlineBytes, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultMeshStreamAccess { - Read, - Write, - ReadWrite, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultMeshStreamCopyBudget { - ZeroCopyTarget, - OneCopyFallback, - OpaqueRuntime, -} - -#[derive(Clone, Debug, PartialEq)] -pub struct CultMeshStreamClock { - pub clock_domain_id: String, - pub source_id: Option, - pub sample_rate: i32, - pub offset_to_verse_time_ns: i64, - pub confidence: f64, - pub evidence_kind: Option, -} - -impl CultMeshStreamClock { - pub fn new(clock_domain_id: impl Into) -> Result { - let clock_domain_id = require_non_empty(clock_domain_id.into(), "clock_domain_id")?; - Ok(Self { - clock_domain_id, - source_id: None, - sample_rate: 0, - offset_to_verse_time_ns: 0, - confidence: 0.0, - evidence_kind: None, - }) - } - - pub fn source_id(mut self, source_id: impl Into) -> Result { - self.source_id = Some(require_non_empty(source_id.into(), "source_id")?); - Ok(self) - } - - pub fn confidence(mut self, confidence: f64) -> Self { - self.confidence = confidence; - self - } - - pub fn evidence_kind(mut self, evidence_kind: impl Into) -> Result { - self.evidence_kind = Some(require_non_empty(evidence_kind.into(), "evidence_kind")?); - Ok(self) - } -} - -#[derive(Clone, Debug, PartialEq)] -pub struct CultMeshStreamDescriptor { - pub stream_id: String, - pub verse_id: String, - pub owner_peer_id: String, - pub kind: CultMeshStreamKind, - pub clock: CultMeshStreamClock, - pub preferred_transports: Vec, - pub label: Option, - pub required_access: CultMeshStreamAccess, - pub max_in_flight_frames: u32, - pub metadata_schema_id: Option, -} - -impl CultMeshStreamDescriptor { - pub fn new( - stream_id: impl Into, - verse_id: impl Into, - owner_peer_id: impl Into, - kind: CultMeshStreamKind, - clock: CultMeshStreamClock, - preferred_transports: Vec, - ) -> Result { - if preferred_transports.is_empty() { - anyhow::bail!("preferred_transports must not be empty"); - } - Ok(Self { - stream_id: require_non_empty(stream_id.into(), "stream_id")?, - verse_id: require_non_empty(verse_id.into(), "verse_id")?, - owner_peer_id: require_non_empty(owner_peer_id.into(), "owner_peer_id")?, - kind, - clock, - preferred_transports, - label: None, - required_access: CultMeshStreamAccess::Read, - max_in_flight_frames: 0, - metadata_schema_id: None, - }) - } - - pub fn label(mut self, label: impl Into) -> Result { - self.label = Some(require_non_empty(label.into(), "label")?); - Ok(self) - } - - pub fn max_in_flight_frames(mut self, max_in_flight_frames: u32) -> Self { - self.max_in_flight_frames = max_in_flight_frames; - self - } - - pub fn metadata_schema_id(mut self, metadata_schema_id: impl Into) -> Result { - self.metadata_schema_id = Some(require_non_empty( - metadata_schema_id.into(), - "metadata_schema_id", - )?); - Ok(self) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshStreamConsumerProfile { - pub peer_id: String, - pub verse_id: String, - pub supported_transports: Vec, - pub accepted_kinds: Vec, - pub max_in_flight_frames: u32, -} - -impl CultMeshStreamConsumerProfile { - pub fn new( - peer_id: impl Into, - verse_id: impl Into, - supported_transports: Vec, - ) -> Result { - if supported_transports.is_empty() { - anyhow::bail!("supported_transports must not be empty"); - } - Ok(Self { - peer_id: require_non_empty(peer_id.into(), "peer_id")?, - verse_id: require_non_empty(verse_id.into(), "verse_id")?, - supported_transports, - accepted_kinds: Vec::new(), - max_in_flight_frames: 0, - }) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshStreamNegotiation { - pub stream_id: String, - pub producer_peer_id: String, - pub consumer_peer_id: String, - pub transport: CultMeshStreamBodyTransport, - pub access: CultMeshStreamAccess, - pub max_in_flight_frames: u32, - pub copy_budget: CultMeshStreamCopyBudget, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshStreamFrameHandle { - pub stream_id: String, - pub sequence: u64, - pub timestamp_ns: i64, - pub duration_ns: i64, - pub transport: CultMeshStreamBodyTransport, - pub byte_length: usize, - pub resource_key: Option, - pub unavoidable_copy_count: u32, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshSharedMemoryFrameRingStats { - pub stream_id: String, - pub slot_count: usize, - pub slot_byte_length: usize, - pub published_frames: u64, - pub dropped_frames: u64, - pub blocked_writes: u64, - pub latest_sequence: u64, - pub unavoidable_copy_count: u64, -} - -pub struct CultMeshFrameReadLease<'a> { - pub handle: CultMeshStreamFrameHandle, - bytes: &'a [u8], -} - -impl CultMeshFrameReadLease<'_> { - pub fn bytes(&self) -> &[u8] { - self.bytes - } -} - -#[derive(Clone, Debug)] -pub struct CultMeshSharedMemoryFrameRing { - stream_id: String, - slot_byte_length: usize, - slots: Vec)>>, - write_cursor: usize, - latest_slot: Option, - next_sequence: u64, - published_frames: u64, - dropped_frames: u64, - blocked_writes: u64, - unavoidable_copy_count: u64, -} - -impl CultMeshSharedMemoryFrameRing { - pub fn new( - stream_id: impl Into, - slot_count: usize, - slot_byte_length: usize, - ) -> Result { - let stream_id = require_non_empty(stream_id.into(), "stream_id")?; - if slot_count == 0 { - anyhow::bail!("slot_count must be greater than zero"); - } - if slot_byte_length == 0 { - anyhow::bail!("slot_byte_length must be greater than zero"); - } - Ok(Self { - stream_id, - slot_byte_length, - slots: vec![None; slot_count], - write_cursor: 0, - latest_slot: None, - next_sequence: 0, - published_frames: 0, - dropped_frames: 0, - blocked_writes: 0, - unavoidable_copy_count: 0, - }) - } - - pub fn try_publish_copy( - &mut self, - bytes: &[u8], - timestamp_ns: i64, - duration_ns: i64, - ) -> Result> { - if bytes.len() > self.slot_byte_length { - anyhow::bail!("frame byte length exceeds slot size"); - } - if self.slots.is_empty() { - self.blocked_writes += 1; - return Ok(None); - } - - let slot_index = self.write_cursor; - if self.slots[slot_index].is_some() { - self.dropped_frames += 1; - } - let handle = CultMeshStreamFrameHandle { - stream_id: self.stream_id.clone(), - sequence: self.next_sequence, - timestamp_ns, - duration_ns, - transport: CultMeshStreamBodyTransport::SharedMemory, - byte_length: bytes.len(), - resource_key: Some(format!("{}:slot:{slot_index}", self.stream_id)), - unavoidable_copy_count: 1, - }; - self.slots[slot_index] = Some((handle.clone(), bytes.to_vec())); - self.latest_slot = Some(slot_index); - self.write_cursor = (slot_index + 1) % self.slots.len(); - self.next_sequence = self.next_sequence.saturating_add(1); - self.published_frames = self.published_frames.saturating_add(1); - self.unavoidable_copy_count = self.unavoidable_copy_count.saturating_add(1); - Ok(Some(handle)) - } - - pub fn try_acquire_latest_read(&self) -> Option> { - let slot_index = self.latest_slot?; - let (handle, bytes) = self.slots[slot_index].as_ref()?; - Some(CultMeshFrameReadLease { - handle: handle.clone(), - bytes, - }) - } - - pub fn stats(&self) -> CultMeshSharedMemoryFrameRingStats { - let latest_sequence = self - .latest_slot - .and_then(|slot| self.slots[slot].as_ref().map(|(handle, _)| handle.sequence)) - .unwrap_or(0); - CultMeshSharedMemoryFrameRingStats { - stream_id: self.stream_id.clone(), - slot_count: self.slots.len(), - slot_byte_length: self.slot_byte_length, - published_frames: self.published_frames, - dropped_frames: self.dropped_frames, - blocked_writes: self.blocked_writes, - latest_sequence, - unavoidable_copy_count: self.unavoidable_copy_count, - } - } -} - -#[derive(Clone, Debug, Default)] -pub struct CultMeshStreamCatalog { - streams: BTreeMap, - latest_frames: BTreeMap, - rings: BTreeMap, -} - -impl CultMeshStreamCatalog { - pub fn declare(&mut self, stream: CultMeshStreamDescriptor) { - self.streams.insert(stream.stream_id.clone(), stream); - } - - pub fn get(&self, stream_id: &str) -> Option<&CultMeshStreamDescriptor> { - self.streams.get(stream_id) - } - - pub fn streams(&self) -> Vec<&CultMeshStreamDescriptor> { - self.streams.values().collect() - } - - pub fn find( - &self, - verse_id: &str, - kind: Option, - ) -> Vec<&CultMeshStreamDescriptor> { - self.streams - .values() - .filter(|stream| stream.verse_id == verse_id && kind.is_none_or(|k| stream.kind == k)) - .collect() - } - - pub fn negotiate( - &self, - stream_id: &str, - consumer: &CultMeshStreamConsumerProfile, - ) -> Result { - let stream = self - .streams - .get(stream_id) - .ok_or_else(|| anyhow::anyhow!("unknown CultMesh stream '{stream_id}'"))?; - if stream.verse_id != consumer.verse_id { - anyhow::bail!("stream and consumer must belong to the same Verse"); - } - if !consumer.accepted_kinds.is_empty() && !consumer.accepted_kinds.contains(&stream.kind) { - anyhow::bail!("consumer does not accept stream kind"); - } - let transport = stream - .preferred_transports - .iter() - .copied() - .find(|candidate| consumer.supported_transports.contains(candidate)) - .ok_or_else(|| { - anyhow::anyhow!("stream and consumer have no compatible body transport") - })?; - let producer_max = if stream.max_in_flight_frames == 0 { - u32::MAX - } else { - stream.max_in_flight_frames - }; - let consumer_max = if consumer.max_in_flight_frames == 0 { - u32::MAX - } else { - consumer.max_in_flight_frames - }; - Ok(CultMeshStreamNegotiation { - stream_id: stream.stream_id.clone(), - producer_peer_id: stream.owner_peer_id.clone(), - consumer_peer_id: consumer.peer_id.clone(), - transport, - access: stream.required_access, - max_in_flight_frames: producer_max.min(consumer_max), - copy_budget: copy_budget_for(transport), - }) - } - - pub fn create_shared_memory_ring( - &mut self, - stream_id: &str, - slot_count: usize, - slot_byte_length: usize, - ) -> Result<&mut CultMeshSharedMemoryFrameRing> { - if !self.streams.contains_key(stream_id) { - anyhow::bail!("unknown CultMesh stream '{stream_id}'"); - } - self.rings.insert( - stream_id.to_string(), - CultMeshSharedMemoryFrameRing::new(stream_id, slot_count, slot_byte_length)?, - ); - Ok(self - .rings - .get_mut(stream_id) - .expect("ring was inserted for stream")) - } - - pub fn ring(&self, stream_id: &str) -> Option<&CultMeshSharedMemoryFrameRing> { - self.rings.get(stream_id) - } - - pub fn ring_mut(&mut self, stream_id: &str) -> Option<&mut CultMeshSharedMemoryFrameRing> { - self.rings.get_mut(stream_id) - } - - pub fn publish_frame(&mut self, handle: CultMeshStreamFrameHandle) -> Result<()> { - if !self.streams.contains_key(&handle.stream_id) { - anyhow::bail!("unknown CultMesh stream '{}'", handle.stream_id); - } - self.latest_frames.insert(handle.stream_id.clone(), handle); - Ok(()) - } - - pub fn latest_frame(&self, stream_id: &str) -> Option<&CultMeshStreamFrameHandle> { - self.latest_frames.get(stream_id) - } -} - -fn copy_budget_for(transport: CultMeshStreamBodyTransport) -> CultMeshStreamCopyBudget { - match transport { - CultMeshStreamBodyTransport::SharedMemory - | CultMeshStreamBodyTransport::SharedD3d12Texture - | CultMeshStreamBodyTransport::SharedD3d11Texture - | CultMeshStreamBodyTransport::DmaBuf - | CultMeshStreamBodyTransport::IoSurface - | CultMeshStreamBodyTransport::AHardwareBuffer => CultMeshStreamCopyBudget::ZeroCopyTarget, - CultMeshStreamBodyTransport::CultCachePage => CultMeshStreamCopyBudget::OneCopyFallback, - CultMeshStreamBodyTransport::InlineBytes => CultMeshStreamCopyBudget::OpaqueRuntime, - } -} - -fn require_non_empty(value: String, name: &str) -> Result { - if value.trim().is_empty() { - anyhow::bail!("{name} must be non-empty"); - } - Ok(value) -} - -impl CultMeshNode { - pub fn runtime_id(&self) -> &str { - &self.runtime_id - } - - pub fn cache(&self) -> &CultCache { - &self.cache - } - - pub fn documents(&self) -> &CultNetDocumentRegistry { - &self.documents - } - - pub fn get(&self, key: &str) -> Result> { - self.cache.get::(key) - } - - pub fn get_required(&self, key: &str) -> Result { - self.cache.get_required::(key) - } - - pub fn put(&mut self, key: impl Into, value: &T) -> Result { - self.cache.put(key, value) - } - - pub fn pull_all_backing_stores(&mut self) -> Result<()> { - self.cache.pull_all_backing_stores() - } - - pub fn delete(&mut self, key: &str) -> Result { - self.cache.delete::(key) - } - - pub fn flush(&mut self) -> Result<()> { - Ok(()) - } - - pub fn publish_document_to_rudp_catalog( - &self, - key: impl Into, - value: &T, - options: CultMeshRudpDocumentPublishOptions, - ) -> Result<()> - where - T: DatabaseEntry + Serialize, - { - let key = key.into(); - let message = self.documents.create_raw_document_put_message( - format!("{}:{}:{}", options.runtime_id, T::TYPE, key), - key, - value, - CultNetDocumentPutOptions { - source_runtime_id: Some(options.runtime_id.clone()), - source_agent_id: options.source_agent_id.clone(), - source_role: options.source_role.clone(), - tags: if options.tags.is_empty() { - None - } else { - Some(options.tags.clone()) - }, - ..CultNetDocumentPutOptions::default() - }, - )?; - publish_cultnet_message_to_rudp_catalog(&message, options) - } - - pub fn pull_rudp_catalog_snapshot( - &mut self, - mut options: CultMeshRudpSnapshotOptions, - ) -> Result { - if options.message_id.trim().is_empty() { - options.message_id = format!( - "{}:snapshot:{}:{}", - options.runtime_id, - unix_millis(), - fresh_rudp_connection_epoch() - ); - } - let response = request_raw_snapshot_from_rudp_catalog(options)?; - let cultnet_rs::CultNetMessage::SnapshotResponseRaw { documents, .. } = response else { - anyhow::bail!("expected cultnet.snapshot_response_raw.v0"); - }; - let mut applied = 0usize; - for document in documents { - let Some(document_type) = - registered_document_type_for_schema(&self.documents, &document.schema_id) - else { - continue; - }; - self.cache.put_raw_envelope(CultCacheEnvelope { - key: document.record_key, - r#type: document_type, - payload: document.payload, - stored_at: document.stored_at, - schema_id: Some(document.schema_id), - })?; - applied += 1; - } - Ok(applied) - } -} - -fn registered_document_type_for_schema( - documents: &CultNetDocumentRegistry, - schema_id: &str, -) -> Option { - let normalized = strip_schema_version_suffix(schema_id); - if normalized != schema_id { - return Some(normalized.to_string()); - } - documents - .binding_by_schema_id(schema_id) - .map(|binding| binding.document_type.clone()) - .or_else(|| documents.binding(schema_id).map(|_| schema_id.to_string())) -} - -fn strip_schema_version_suffix(value: &str) -> &str { - let Some((prefix, suffix)) = value.rsplit_once(".v") else { - return value; - }; - if suffix.chars().all(|character| character.is_ascii_digit()) { - prefix - } else { - value - } -} - -/// Request a raw CultNet snapshot without importing it into a local cache. -/// -/// The returned value is the exact validated `SnapshotResponseRaw`, including -/// its correlation `message_id`. Each call binds a fresh source port, chooses a -/// fresh connection epoch, and succeeds only after the reliable request is -/// acknowledged. -pub fn request_raw_snapshot_from_rudp_catalog( - options: CultMeshRudpSnapshotOptions, -) -> Result { - if options.runtime_id.trim().is_empty() { - anyhow::bail!("runtime_id must be non-empty"); - } - if options.message_id.trim().is_empty() { - anyhow::bail!("raw CultMesh RUDP snapshot requires a message_id"); - } - let message_id = options.message_id.clone(); - let mut client = connect_rudp_client( - options.target, - &options.runtime_id, - options.connect_timeout, - options.poll_interval, - options.resend_delay_ms, - "cultmesh-rudp-snapshot-client", - )?; - - let request = cultnet_rs::CultNetMessage::SnapshotRequest { - message_id: message_id.clone(), - schema_ids: options.schema_ids, - record_keys: options.record_keys, - }; - let payload = encode_cultnet_message_to_vec(&request, CultNetWireContract::CultNetSchemaV0)?; - let receipt = client.send_reliable("schema", payload)?; - let response_deadline = Instant::now() + options.response_timeout; - let mut snapshot_response = None; - while Instant::now() < response_deadline { - if let Some(frame) = client.receive_once()? { - if frame.channel_id == "schema" { - let message = decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - )?; - match &message { - cultnet_rs::CultNetMessage::SnapshotResponseRaw { - message_id: received, - .. - } if received == &message_id => { - snapshot_response = Some(message); - } - cultnet_rs::CultNetMessage::Error { error } => { - let _ = client.disconnect(b"snapshot-error".to_vec()); - anyhow::bail!("CultMesh RUDP snapshot failed: {error}"); - } - _ => {} - } - } - } - match client.reliable_send_status(&receipt) { - cultnet_rs::CultNetRudpReliableSendStatus::Acknowledged - if snapshot_response.is_some() => - { - let _ = client.disconnect(b"snapshot-complete".to_vec()); - return Ok(snapshot_response.expect("response was checked as present")); - } - cultnet_rs::CultNetRudpReliableSendStatus::Invalidated => { - anyhow::bail!("CultMesh RUDP snapshot request receipt was invalidated") - } - _ => {} - } - client.poll_resends()?; - thread::sleep(options.poll_interval); - } - let _ = client.disconnect(b"snapshot-timeout".to_vec()); - if snapshot_response.is_some() { - anyhow::bail!( - "timed out waiting for reliable acknowledgement of CultMesh RUDP snapshot request to {}", - options.target - ); - } - anyhow::bail!( - "timed out waiting for CultMesh RUDP snapshot from {}", - options.target - ) -} - -fn unix_millis() -> u128 { - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_millis() -} - -/// Publish one raw CultNet schema message to a RUDP catalog. -/// -/// Each call owns a fresh connection epoch and returns only after the catalog -/// acknowledges reliable application admission. -pub fn publish_cultnet_message_to_rudp_catalog( - message: &cultnet_rs::CultNetMessage, - options: CultMeshRudpDocumentPublishOptions, -) -> Result<()> { - if options.runtime_id.trim().is_empty() { - anyhow::bail!("runtime_id must be non-empty"); - } - let mut client = connect_rudp_client( - options.target, - &options.runtime_id, - options.connect_timeout, - options.poll_interval, - options.resend_delay_ms, - "cultmesh-rudp-document-publisher", - )?; - - let payload = encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?; - let receipt = client.send_reliable("schema", payload)?; - let flush_deadline = Instant::now() + options.flush_timeout; - while Instant::now() < flush_deadline { - let _ = client.receive_once()?; - match client.reliable_send_status(&receipt) { - cultnet_rs::CultNetRudpReliableSendStatus::Acknowledged => { - let _ = client.disconnect(b"document-acknowledged".to_vec()); - return Ok(()); - } - cultnet_rs::CultNetRudpReliableSendStatus::Invalidated => { - anyhow::bail!("CultMesh RUDP document receipt was invalidated") - } - cultnet_rs::CultNetRudpReliableSendStatus::Pending => {} - } - client.poll_resends()?; - thread::sleep(options.poll_interval); - } - let _ = client.disconnect(b"document-unacknowledged".to_vec()); - anyhow::bail!( - "timed out waiting for reliable acknowledgement from CultMesh RUDP catalog {}", - options.target - ) -} - -fn connect_rudp_client( - target: SocketAddr, - runtime_id: &str, - connect_timeout: Duration, - poll_interval: Duration, - resend_delay_ms: u64, - transport_id: &str, -) -> Result { - let bind_addr = if target.is_ipv4() { - SocketAddr::from(([0, 0, 0, 0], 0)) - } else { - SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 0], 0)) - }; - let socket = UdpSocket::bind(bind_addr)?; - socket.set_read_timeout(Some(poll_interval))?; - let mut client = - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: runtime_id.to_string(), - socket, - mode: cultnet_rs::CultNetRudpSocketMode::Client, - remote_addr: Some(target), - connection_id: fresh_rudp_connection_epoch(), - initial_sequence: 1, - resend_delay_ms, - transport_id: Some(transport_id.to_string()), - max_payload_bytes: None, - max_fragment_bytes: Some(1200), - max_pending_reliable_packets: Some(1024), - media_reliable_expire_after_ms: None, - reconnect_policy: None, - })?; - client.connect(Vec::new())?; - let connect_deadline = Instant::now() + connect_timeout; - while !client.connected() && Instant::now() < connect_deadline { - let _ = client.receive_once()?; - client.poll_resends()?; - thread::sleep(poll_interval); - } - if !client.connected() { - anyhow::bail!("timed out connecting CultMesh RUDP client {runtime_id} to {target}"); - } - Ok(client) -} - -fn fresh_rudp_connection_epoch() -> u32 { - let mut hasher = RandomState::new().build_hasher(); - hasher.write_u128(unix_millis()); - let epoch = hasher.finish() as u32; - if epoch == 0 { 1 } else { epoch } -} - -#[derive(Clone, Copy, Debug, Default)] -pub struct CultMesh; - -impl CultMesh { - pub fn resolve_rudp_endpoint(endpoint: &str) -> Result { - let text = require_non_empty(endpoint.trim().to_string(), "endpoint")?; - if text.starts_with("rudp://") { - return parse_rudp_socket_addr(&text); - } - if text.starts_with("cultmesh://") { - let resolved = default_cultmesh_rudp_endpoint_resolver(&text)?.ok_or_else(|| { - anyhow::anyhow!("CultMesh URI {text} did not resolve to a RUDP endpoint") - })?; - return parse_rudp_socket_addr(&resolved); - } - parse_socket_addr(&text) - } - - pub fn create_node( - store_path: impl AsRef, - documents: D, - options: CultMeshNodeOptions, - ) -> Result - where - D: CultMeshDocumentSet, - { - let mut cache = CultCache::new(); - documents.register_cache(&mut cache)?; - cache - .add_generic_backing_store(SingleFileMessagePackBackingStore::new(store_path.as_ref())); - if options.pull_on_start { - cache.pull_all_backing_stores()?; - } - - let mut registry = CultNetDocumentRegistry::new(); - documents.register_documents(&mut registry)?; - - Ok(CultMeshNode { - runtime_id: options.runtime_id, - cache, - documents: registry, - }) - } - - pub fn start_node( - store_path: impl AsRef, - documents: D, - options: CultMeshNodeOptions, - ) -> Result - where - D: CultMeshDocumentSet, - { - Self::create_node(store_path, documents, options) - } - - pub fn create_stream_catalog() -> CultMeshStreamCatalog { - CultMeshStreamCatalog::default() - } -} - -pub fn create_node( - store_path: impl AsRef, - documents: D, - options: CultMeshNodeOptions, -) -> Result -where - D: CultMeshDocumentSet, -{ - CultMesh::create_node(store_path, documents, options) -} - -pub fn start_node( - store_path: impl AsRef, - documents: D, - options: CultMeshNodeOptions, -) -> Result -where - D: CultMeshDocumentSet, -{ - CultMesh::start_node(store_path, documents, options) -} - -pub fn create_stream_catalog() -> CultMeshStreamCatalog { - CultMesh::create_stream_catalog() -} - -pub fn resolve_rudp_endpoint(endpoint: &str) -> Result { - CultMesh::resolve_rudp_endpoint(endpoint) -} - -fn parse_rudp_socket_addr(value: &str) -> Result { - let text = value.trim().strip_prefix("rudp://").unwrap_or(value.trim()); - parse_socket_addr(text) -} - -fn parse_socket_addr(value: &str) -> Result { - value - .parse() - .with_context(|| format!("RUDP endpoint must be a socket address, got {value:?}")) -} - -fn default_cultmesh_rudp_endpoint_resolver(uri: &str) -> Result> { - let authority = cultmesh_uri_authority(uri)?; - let slug = authority - .chars() - .map(|ch| { - if ch.is_ascii_alphanumeric() { - ch.to_ascii_uppercase() - } else { - '_' - } - }) - .collect::() - .trim_matches('_') - .to_string(); - if slug.is_empty() { - return Ok(None); - } - - for key in [ - format!("CULTMESH_URI_{slug}_RUDP"), - format!("{slug}_CULTMESH_RUDP_ENDPOINT"), - ] { - if let Ok(value) = env::var(&key) { - let text = value.trim(); - if !text.is_empty() { - return Ok(Some(if text.starts_with("rudp://") { - text.to_string() - } else { - format!("rudp://{text}") - })); - } - } - } - Ok(None) -} - -fn cultmesh_uri_authority(uri: &str) -> Result { - let rest = uri - .strip_prefix("cultmesh://") - .ok_or_else(|| anyhow::anyhow!("CultMesh URI must start with cultmesh://"))?; - let authority = rest.split(['/', '?', '#']).next().unwrap_or("").trim(); - if authority.is_empty() { - anyhow::bail!("CultMesh URI must include an authority"); - } - Ok(authority.to_string()) -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[derive(Clone, Debug, PartialEq, Eq, cultcache_rs::DatabaseEntry)] - #[cultcache(type = "cultmesh.test.note", schema = "CultMeshTestNote")] - struct Note { - #[cultcache(key = 0)] - body: String, - } - - cultmesh_documents!(TestDocuments { - Note => "cultmesh.test.note.v0", - }); - - #[test] - fn node_round_trips_registered_documents() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cultmesh.cc"); - let note = Note { - body: "blessed circuit".to_string(), - }; - - let mut node = CultMesh::create_node( - &store_path, - TestDocuments, - CultMeshNodeOptions { - runtime_id: "test-runtime".to_string(), - ..CultMeshNodeOptions::default() - }, - )?; - assert_eq!(node.runtime_id(), "test-runtime"); - node.put("note", ¬e)?; - node.flush()?; - - let reloaded = CultMesh::create_node(&store_path, TestDocuments, Default::default())?; - assert_eq!(reloaded.get_required::("note")?, note); - assert!( - reloaded - .documents() - .binding_by_schema_id("cultmesh.test.note.v0") - .is_some() - ); - Ok(()) - } - - #[test] - fn node_publishes_registered_document_to_rudp_catalog() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cultmesh.cc"); - let note = Note { - body: "respect odin once".to_string(), - }; - let node = CultMesh::create_node( - &store_path, - TestDocuments, - CultMeshNodeOptions { - runtime_id: "muninn-test".to_string(), - ..CultMeshNodeOptions::default() - }, - )?; - - let socket = UdpSocket::bind("127.0.0.1:0")?; - socket.set_read_timeout(Some(Duration::from_millis(10)))?; - let target = socket.local_addr()?; - let (sender, receiver) = std::sync::mpsc::channel(); - let delivered = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let delivered_by_sink = delivered.clone(); - let server = thread::spawn(move || -> Result<()> { - let mut server = CultMeshRudpDocumentServer::new( - socket, - move |receipt: CultMeshRudpRawDocumentReceipt| { - sender - .send(( - receipt.document.schema_id, - receipt.document.record_key, - receipt.document.source_runtime_id, - )) - .ok(); - delivered_by_sink.store(true, std::sync::atomic::Ordering::SeqCst); - Ok(()) - }, - |_query: &CultMeshRudpSnapshotQuery| Ok(Vec::new()), - CultMeshSystemClock::default(), - CultMeshRudpDocumentServerOptions::default(), - )?; - let deadline = Instant::now() + Duration::from_secs(2); - while Instant::now() < deadline { - server.poll_once()?; - if delivered.load(std::sync::atomic::Ordering::SeqCst) { - return Ok(()); - } - thread::sleep(Duration::from_millis(1)); - } - anyhow::bail!("timed out waiting for RUDP document put") - }); - - node.publish_document_to_rudp_catalog( - "note", - ¬e, - CultMeshRudpDocumentPublishOptions::odin(target, "muninn-test"), - )?; - let received = receiver.recv_timeout(Duration::from_secs(2))?; - server.join().expect("server thread should not panic")?; - - assert_eq!(received.0, "cultmesh.test.note.v0"); - assert_eq!(received.1, "note"); - assert_eq!(received.2.as_deref(), Some("muninn-test")); - Ok(()) - } - - #[test] - fn document_publish_fails_when_catalog_is_unreachable() -> Result<()> { - let temp = tempfile::tempdir()?; - let node = CultMesh::create_node( - temp.path().join("cultmesh.cc"), - TestDocuments, - Default::default(), - )?; - let unused = UdpSocket::bind("127.0.0.1:0")?; - let target = unused.local_addr()?; - drop(unused); - - let error = node - .publish_document_to_rudp_catalog( - "note", - &Note { - body: "no false success".into(), - }, - CultMeshRudpDocumentPublishOptions { - target, - connect_timeout: Duration::from_millis(30), - poll_interval: Duration::from_millis(2), - ..CultMeshRudpDocumentPublishOptions::default() - }, - ) - .expect_err("unreachable catalog must fail closed"); - assert!(error.to_string().contains("timed out connecting")); - Ok(()) - } - - #[test] - fn document_publish_requires_application_acknowledgement() -> Result<()> { - let temp = tempfile::tempdir()?; - let node = CultMesh::create_node( - temp.path().join("cultmesh.cc"), - TestDocuments, - Default::default(), - )?; - let socket = UdpSocket::bind("127.0.0.1:0")?; - let target = socket.local_addr()?; - let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let server_stop = stop.clone(); - let server = thread::spawn(move || -> Result<()> { - let mut server = CultMeshRudpDocumentServer::new( - socket, - |_receipt: CultMeshRudpRawDocumentReceipt| { - anyhow::bail!("injected durable sink failure") - }, - |_query: &CultMeshRudpSnapshotQuery| Ok(Vec::new()), - CultMeshSystemClock::default(), - CultMeshRudpDocumentServerOptions::default(), - )?; - while !server_stop.load(std::sync::atomic::Ordering::SeqCst) { - let _ = server.poll_once(); - thread::sleep(Duration::from_millis(1)); - } - Ok(()) - }); - - let error = node - .publish_document_to_rudp_catalog( - "note", - &Note { - body: "still not accepted".into(), - }, - CultMeshRudpDocumentPublishOptions { - target, - connect_timeout: Duration::from_millis(100), - flush_timeout: Duration::from_millis(50), - poll_interval: Duration::from_millis(2), - resend_delay_ms: 5, - ..CultMeshRudpDocumentPublishOptions::default() - }, - ) - .expect_err("unacknowledged document must fail closed"); - stop.store(true, std::sync::atomic::Ordering::SeqCst); - server.join().expect("server thread should not panic")?; - assert!(error.to_string().contains("reliable acknowledgement")); - Ok(()) - } - - #[test] - fn raw_snapshot_preserves_caller_challenge_and_raw_response() -> Result<()> { - let expected = cultnet_rs::CultNetRawDocumentRecord { - schema_id: "cultmesh.test.raw.v0".into(), - record_key: "route-proof".into(), - stored_at: "2026-09-03T00:00:00Z".into(), - payload_encoding: cultnet_rs::CultNetRawPayloadEncoding::Messagepack, - payload: vec![0xc4, 0x03, 0, 0xff, 1], - source_runtime_id: Some("odin-test".into()), - source_agent_id: None, - source_role: None, - tags: None, - }; - let socket = UdpSocket::bind("127.0.0.1:0")?; - let target = socket.local_addr()?; - let challenge = "9aa63ad8-83b5-4cc4-ae4a-91d361d5f845".to_string(); - let server_document = expected.clone(); - let server_challenge = challenge.clone(); - let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); - let server_stop = stop.clone(); - let server = thread::spawn(move || -> Result<()> { - let mut server = CultMeshRudpDocumentServer::new( - socket, - |_receipt: CultMeshRudpRawDocumentReceipt| Ok(()), - move |query: &CultMeshRudpSnapshotQuery| { - if query.message_id != server_challenge { - anyhow::bail!("snapshot challenge changed") - } - Ok(vec![server_document.clone()]) - }, - CultMeshSystemClock::default(), - CultMeshRudpDocumentServerOptions::default(), - )?; - while !server_stop.load(std::sync::atomic::Ordering::SeqCst) { - server.poll_once()?; - thread::sleep(Duration::from_millis(1)); - } - Ok(()) - }); - - let response = request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { - target, - runtime_id: "route-proof-test".into(), - message_id: challenge.clone(), - connect_timeout: Duration::from_millis(100), - response_timeout: Duration::from_millis(500), - poll_interval: Duration::from_millis(2), - resend_delay_ms: 5, - schema_ids: Some(vec!["cultmesh.test.raw.v0".into()]), - record_keys: Some(vec!["route-proof".into()]), - }); - stop.store(true, std::sync::atomic::Ordering::SeqCst); - server.join().expect("server thread should not panic")?; - let response = response?; - assert_eq!( - response, - cultnet_rs::CultNetMessage::SnapshotResponseRaw { - message_id: challenge, - documents: vec![expected], - } - ); - Ok(()) - } - - #[test] - fn raw_snapshot_rejects_response_without_request_acknowledgement() -> Result<()> { - let socket = UdpSocket::bind("127.0.0.1:0")?; - socket.set_read_timeout(Some(Duration::from_millis(200)))?; - let target = socket.local_addr()?; - let server = thread::spawn(move || -> Result<()> { - let mut wire = vec![0_u8; 65_535]; - let (received, peer) = socket.recv_from(&mut wire)?; - let connect = cultnet_rs::decode_rudp_packet(&wire[..received])?; - let mut session = - cultnet_rs::CultNetRudpSession::new(cultnet_rs::CultNetRudpSessionOptions { - connection_id: connect.connection_id, - initial_sequence: 1, - resend_delay_ms: 5, - max_pending_reliable_packets: Some(64), - }); - let accept = session.accept_connect(&connect, 1, Vec::new())?; - socket.send_to(&cultnet_rs::encode_rudp_packet(&accept)?, peer)?; - - loop { - let (received, received_peer) = socket.recv_from(&mut wire)?; - if received_peer != peer { - continue; - } - let packet = cultnet_rs::decode_rudp_packet(&wire[..received])?; - let result = session.receive(&packet, 2)?; - let Some(frame) = result.delivered.into_iter().next() else { - continue; - }; - let request = decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - )?; - let cultnet_rs::CultNetMessage::SnapshotRequest { message_id, .. } = request else { - anyhow::bail!("expected snapshot request") - }; - let payload = encode_cultnet_message_to_vec( - &cultnet_rs::CultNetMessage::SnapshotResponseRaw { - message_id, - documents: Vec::new(), - }, - CultNetWireContract::CultNetSchemaV0, - )?; - let mut packets = session.send_many( - "schema", - payload, - cultnet_rs::CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms: 2, - reliable_expire_after_ms: None, - }, - Some(1200), - )?; - for packet in &mut packets { - packet.ack = 0; - packet.ack_mask = 0; - socket.send_to(&cultnet_rs::encode_rudp_packet(packet)?, peer)?; - } - return Ok(()); - } - }); - - let error = request_raw_snapshot_from_rudp_catalog(CultMeshRudpSnapshotOptions { - target, - runtime_id: "unacknowledged-snapshot-test".into(), - message_id: "b7987efb-69c5-4c42-9355-b6e09535ad21".into(), - connect_timeout: Duration::from_millis(100), - response_timeout: Duration::from_millis(50), - poll_interval: Duration::from_millis(2), - resend_delay_ms: 5, - schema_ids: None, - record_keys: None, - }) - .expect_err("response without request ACK must fail closed"); - server.join().expect("server thread should not panic")?; - assert!(error.to_string().contains("reliable acknowledgement")); - Ok(()) - } - - #[test] - fn resolves_rudp_endpoint_from_cultmesh_uri_bootstrap_env() -> Result<()> { - let previous = env::var("CULTMESH_URI_ODIN_RUDP").ok(); - unsafe { - env::set_var("CULTMESH_URI_ODIN_RUDP", "127.0.0.1:17871"); - } - let result = CultMesh::resolve_rudp_endpoint("cultmesh://odin/rendezvous/provider-catalog"); - unsafe { - if let Some(value) = previous { - env::set_var("CULTMESH_URI_ODIN_RUDP", value); - } else { - env::remove_var("CULTMESH_URI_ODIN_RUDP"); - } - } - - assert_eq!(result?, "127.0.0.1:17871".parse()?); - Ok(()) - } - - #[test] - fn rejects_unresolved_cultmesh_rudp_endpoint() { - let previous = env::var("CULTMESH_URI_MISSING_ODIN_RUDP").ok(); - unsafe { - env::remove_var("CULTMESH_URI_MISSING_ODIN_RUDP"); - } - let error = - CultMesh::resolve_rudp_endpoint("cultmesh://missing-odin/rendezvous/provider-catalog") - .expect_err("unresolved CultMesh URI should fail"); - unsafe { - if let Some(value) = previous { - env::set_var("CULTMESH_URI_MISSING_ODIN_RUDP", value); - } - } - assert!(error.to_string().contains("did not resolve")); - } - - #[test] - fn raw_snapshot_application_uses_registered_document_type() -> Result<()> { - let temp = tempfile::tempdir()?; - let store_path = temp.path().join("cultmesh.cc"); - let mut node = CultMesh::create_node( - &store_path, - TestDocuments, - CultMeshNodeOptions { - runtime_id: "snapshot-apply-test".to_string(), - ..CultMeshNodeOptions::default() - }, - )?; - let payload = rmp_serde::to_vec(&Note { - body: "catalog truth".to_string(), - })?; - - node.cache - .put_raw_envelope(cultcache_rs::CultCacheEnvelope { - key: "note".to_string(), - r#type: node - .documents - .binding_by_schema_id("cultmesh.test.note.v0") - .expect("test binding") - .document_type - .clone(), - payload, - stored_at: "2026-07-03T00:00:00Z".to_string(), - schema_id: Some("cultmesh.test.note.v0".to_string()), - })?; - node.flush()?; - - let reloaded = CultMesh::create_node(&store_path, TestDocuments, Default::default())?; - assert_eq!(reloaded.get_required::("note")?.body, "catalog truth"); - assert_eq!(reloaded.cache().snapshot()[0].r#type, Note::TYPE); - Ok(()) - } - - #[test] - fn stream_catalog_negotiates_and_publishes_latest_shared_memory_frame() -> Result<()> { - let mut catalog = CultMesh::create_stream_catalog(); - let stream = CultMeshStreamDescriptor::new( - "muninn:nightwing:move-evidence", - "mimir-live", - "muninn:nightwing", - CultMeshStreamKind::Bytes, - CultMeshStreamClock::new("muninn:nightwing:clock")? - .source_id("muninn:nightwing:move-evidence")? - .confidence(1.0) - .evidence_kind("muninn-move-evidence")?, - vec![ - CultMeshStreamBodyTransport::SharedMemory, - CultMeshStreamBodyTransport::CultCachePage, - ], - )? - .label("Muninn Move evidence")? - .max_in_flight_frames(4) - .metadata_schema_id("mimir.muninn_move_evidence_stream_frame.v1")?; - catalog.declare(stream); - - let consumer = CultMeshStreamConsumerProfile::new( - "mimir:starfire", - "mimir-live", - vec![CultMeshStreamBodyTransport::SharedMemory], - )?; - let negotiation = catalog.negotiate("muninn:nightwing:move-evidence", &consumer)?; - - assert_eq!( - negotiation.transport, - CultMeshStreamBodyTransport::SharedMemory - ); - assert_eq!( - negotiation.copy_budget, - CultMeshStreamCopyBudget::ZeroCopyTarget - ); - - let handle = { - let ring = - catalog.create_shared_memory_ring("muninn:nightwing:move-evidence", 4, 128)?; - ring.try_publish_copy(&[1, 2, 3], 42, 0)? - .expect("frame should publish") - }; - catalog.publish_frame(handle.clone())?; - - assert_eq!( - catalog.latest_frame("muninn:nightwing:move-evidence"), - Some(&handle) - ); - let lease = catalog - .ring("muninn:nightwing:move-evidence") - .and_then(CultMeshSharedMemoryFrameRing::try_acquire_latest_read) - .expect("latest frame should be readable"); - assert_eq!(lease.handle.sequence, 0); - assert_eq!(lease.bytes(), &[1, 2, 3]); - Ok(()) - } -} diff --git a/vendor/cultmesh-rs/src/rudp_document_server.rs b/vendor/cultmesh-rs/src/rudp_document_server.rs deleted file mode 100644 index bebeb6c..0000000 --- a/vendor/cultmesh-rs/src/rudp_document_server.rs +++ /dev/null @@ -1,647 +0,0 @@ -use anyhow::{Result, anyhow}; -use cultnet_rs::{ - CultNetMessage, CultNetRawDocumentRecord, CultNetRudpPacket, CultNetRudpPacketType, - CultNetRudpSendOptions, CultNetRudpSession, CultNetRudpSessionOptions, CultNetWireContract, - decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, - encode_rudp_packet, -}; -use std::collections::BTreeMap; -use std::io::ErrorKind; -use std::net::{SocketAddr, UdpSocket}; -use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; - -const MAX_UDP_DATAGRAM_BYTES: usize = 65_535; - -/// The transport identity of one remote CultNet RUDP session. -/// -/// Connection identifiers are scoped to a remote socket address. Two clients -/// may therefore use the same connection identifier without sharing ordering, -/// acknowledgement, resend, or fragment state. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct CultMeshRudpSessionKey { - pub remote_addr: SocketAddr, - pub connection_id: u32, -} - -/// A raw document plus the receipt fact owned by the receiving process. -/// -/// `document.payload` is the exact byte vector decoded from the CultNet raw -/// record. The trusted receipt time lives beside the sender-owned record so the -/// transport never rewrites its metadata or payload. -#[derive(Clone, Debug, PartialEq)] -pub struct CultMeshRudpRawDocumentReceipt { - pub session: CultMeshRudpSessionKey, - pub message_id: String, - pub transport_sequence: u32, - pub received_at_unix_millis: u64, - pub document: CultNetRawDocumentRecord, -} - -/// The caller-visible query for a read-only raw snapshot. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpSnapshotQuery { - pub session: CultMeshRudpSessionKey, - pub message_id: String, - pub requested_at_unix_millis: u64, - pub schema_ids: Option>, - pub record_keys: Option>, -} - -/// Caller-owned admission/persistence port for received raw documents. -pub trait CultMeshRudpRawDocumentSink { - fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()>; -} - -impl CultMeshRudpRawDocumentSink for F -where - F: FnMut(CultMeshRudpRawDocumentReceipt) -> Result<()>, -{ - fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { - self(receipt) - } -} - -/// Caller-owned catalog port for serving raw snapshot requests. -/// -/// The caller decides which records the requester may see. CultMesh preserves -/// those records as raw CultNet documents and does not interpret their schemas. -pub trait CultMeshRudpSnapshotSource { - fn raw_snapshot( - &mut self, - query: &CultMeshRudpSnapshotQuery, - ) -> Result>; -} - -impl CultMeshRudpSnapshotSource for F -where - F: FnMut(&CultMeshRudpSnapshotQuery) -> Result>, -{ - fn raw_snapshot( - &mut self, - query: &CultMeshRudpSnapshotQuery, - ) -> Result> { - self(query) - } -} - -/// Trusted local clocks for receipt facts and transport maintenance. -/// -/// Receipt time is wall time. Expiry and resend decisions use the monotonic -/// value, so a wall-clock correction cannot resurrect or pin a session. -pub trait CultMeshRudpServerClock { - fn now_unix_millis(&self) -> u64; - fn now_monotonic_millis(&self) -> u64; -} - -#[derive(Clone, Debug)] -pub struct CultMeshSystemClock { - monotonic_origin: Instant, -} - -impl Default for CultMeshSystemClock { - fn default() -> Self { - Self { - monotonic_origin: Instant::now(), - } - } -} - -impl CultMeshRudpServerClock for CultMeshSystemClock { - fn now_unix_millis(&self) -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_millis() - .try_into() - .unwrap_or(u64::MAX) - } - - fn now_monotonic_millis(&self) -> u64 { - self.monotonic_origin - .elapsed() - .as_millis() - .try_into() - .unwrap_or(u64::MAX) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpDocumentServerOptions { - pub max_sessions: usize, - pub session_idle_timeout: Duration, - pub session_max_lifetime: Duration, - /// Conservative payload admission budget across all live sessions. - /// - /// Admitted ingress and queued snapshot-response bytes are charged until - /// the session ends even when CultNet releases them sooner. Together with - /// CultNet's per-session receive caps, this makes retained payload memory - /// finite without duplicating its fragment/ordering machinery here. - pub max_admitted_payload_bytes: usize, - pub max_admitted_payload_bytes_per_session: usize, - pub max_snapshot_response_bytes: usize, - pub max_snapshot_documents: usize, - pub resend_delay: Duration, - pub max_pending_reliable_packets_per_session: usize, - pub max_fragment_bytes: usize, -} - -impl Default for CultMeshRudpDocumentServerOptions { - fn default() -> Self { - Self { - max_sessions: 64, - session_idle_timeout: Duration::from_secs(30), - session_max_lifetime: Duration::from_secs(15 * 60), - max_admitted_payload_bytes: 32 * 1024 * 1024, - max_admitted_payload_bytes_per_session: 4 * 1024 * 1024, - max_snapshot_response_bytes: 1024 * 1024, - max_snapshot_documents: 4096, - resend_delay: Duration::from_millis(50), - max_pending_reliable_packets_per_session: 1024, - max_fragment_bytes: 1200, - } - } -} - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct CultMeshRudpMaintenance { - pub sessions_expired: usize, - pub packets_resent: usize, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CultMeshRudpApplicationOperation { - DocumentPutRaw, - SnapshotRequest, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct CultMeshRudpApplicationRejection { - pub session: CultMeshRudpSessionKey, - pub operation: CultMeshRudpApplicationOperation, - pub message_id: String, - pub reason: String, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum CultMeshRudpPollOutcome { - Idle, - Handled, - ApplicationRejected(CultMeshRudpApplicationRejection), -} - -struct SessionEntry { - session: CultNetRudpSession, - created_at_monotonic_millis: u64, - last_activity_monotonic_millis: u64, - admitted_payload_bytes: usize, -} - -/// A synchronous, multi-session CultNet RUDP raw-document server. -/// -/// The server owns exactly one UDP socket. Call `poll_once` from an existing -/// daemon loop; no executor or background thread is created here. -pub struct CultMeshRudpDocumentServer { - socket: UdpSocket, - sessions: BTreeMap, - sink: S, - snapshot_source: Q, - clock: C, - options: CultMeshRudpDocumentServerOptions, -} - -impl CultMeshRudpDocumentServer -where - S: CultMeshRudpRawDocumentSink, - Q: CultMeshRudpSnapshotSource, - C: CultMeshRudpServerClock, -{ - pub fn new( - socket: UdpSocket, - sink: S, - snapshot_source: Q, - clock: C, - options: CultMeshRudpDocumentServerOptions, - ) -> Result { - validate_options(&options)?; - socket.local_addr().map_err(|error| { - anyhow!("CultMesh RUDP server requires a bound UDP socket: {error}") - })?; - socket.set_nonblocking(true)?; - Ok(Self { - socket, - sessions: BTreeMap::new(), - sink, - snapshot_source, - clock, - options, - }) - } - - pub fn local_addr(&self) -> Result { - Ok(self.socket.local_addr()?) - } - - pub fn session_count(&self) -> usize { - self.sessions.len() - } - - /// Poll transport maintenance and receive at most one UDP datagram. - /// - /// Application rejection invalidates only the responsible peer session and - /// is returned as data so a daemon can log it and keep serving. Local - /// socket and server-state failures remain errors. - pub fn poll_once(&mut self) -> Result { - self.maintain()?; - let mut wire = vec![0_u8; MAX_UDP_DATAGRAM_BYTES]; - let (received, remote_addr) = match self.socket.recv_from(&mut wire) { - Ok(value) => value, - Err(error) if error.kind() == ErrorKind::WouldBlock => { - return Ok(CultMeshRudpPollOutcome::Idle); - } - Err(error) => return Err(error.into()), - }; - wire.truncate(received); - - let packet = match decode_rudp_packet(&wire) { - Ok(packet) => packet, - Err(_) => return Ok(CultMeshRudpPollOutcome::Handled), - }; - let now_unix = self.clock.now_unix_millis(); - let now_monotonic = self.clock.now_monotonic_millis(); - let key = CultMeshRudpSessionKey { - remote_addr, - connection_id: packet.connection_id, - }; - - if packet.packet_type == CultNetRudpPacketType::Connect { - self.accept_connection(key, &packet, now_monotonic)?; - return Ok(CultMeshRudpPollOutcome::Handled); - } - - if !self.sessions.contains_key(&key) { - return Ok(CultMeshRudpPollOutcome::Handled); - } - if packet.packet_type == CultNetRudpPacketType::Data - && packet.channel_id == "schema" - && !packet.reliable - { - return Ok(CultMeshRudpPollOutcome::Handled); - } - let payload_charge = if packet.packet_type == CultNetRudpPacketType::Data { - packet.payload.len() - } else { - 0 - }; - if !self.payload_budget_allows(key, payload_charge) { - return Ok(CultMeshRudpPollOutcome::Handled); - } - - let result = { - let entry = self - .sessions - .get_mut(&key) - .expect("checked session must remain present"); - entry.last_activity_monotonic_millis = now_monotonic; - entry.admitted_payload_bytes = - entry.admitted_payload_bytes.saturating_add(payload_charge); - entry.session.receive(&packet, now_monotonic) - }; - let result = match result { - Ok(result) => result, - Err(_) => { - self.sessions.remove(&key); - return Ok(CultMeshRudpPollOutcome::Handled); - } - }; - - if let Some(reply) = result.reply { - self.send_packet(key.remote_addr, &reply)?; - } - - if result.disconnected { - self.sessions.remove(&key); - return Ok(CultMeshRudpPollOutcome::Handled); - } - - for frame in result.delivered { - if frame.channel_id != "schema" { - continue; - } - let message = match decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - ) { - Ok(message) => message, - Err(_) => continue, - }; - if let Some(rejection) = self.deliver_application_message( - key, - frame.sequence, - message, - now_unix, - now_monotonic, - )? { - self.sessions.remove(&key); - return Ok(CultMeshRudpPollOutcome::ApplicationRejected(rejection)); - } - } - - if packet.reliable { - let ack = self - .sessions - .get_mut(&key) - .ok_or_else(|| anyhow!("CultMesh RUDP session disappeared before ACK"))? - .session - .create_ack(); - self.send_packet(key.remote_addr, &ack)?; - } - Ok(CultMeshRudpPollOutcome::Handled) - } - - /// Expire idle sessions and resend reliable packets whose deadline passed. - pub fn maintain(&mut self) -> Result { - let now = self.clock.now_monotonic_millis(); - let idle_timeout_ms = duration_millis(self.options.session_idle_timeout); - let lifetime_ms = duration_millis(self.options.session_max_lifetime); - let before = self.sessions.len(); - self.sessions.retain(|_, entry| { - now.saturating_sub(entry.last_activity_monotonic_millis) <= idle_timeout_ms - && now.saturating_sub(entry.created_at_monotonic_millis) <= lifetime_ms - }); - let expired = before - self.sessions.len(); - - let mut resends = Vec::new(); - for (key, entry) in &mut self.sessions { - for packet in entry.session.due_resends(now) { - resends.push((key.remote_addr, packet)); - } - } - for (remote_addr, packet) in &resends { - self.send_packet(*remote_addr, packet)?; - } - Ok(CultMeshRudpMaintenance { - sessions_expired: expired, - packets_resent: resends.len(), - }) - } - - fn accept_connection( - &mut self, - key: CultMeshRudpSessionKey, - packet: &CultNetRudpPacket, - now: u64, - ) -> Result<()> { - if !self.sessions.contains_key(&key) { - if self.sessions.len() >= self.options.max_sessions { - return Ok(()); - } - self.sessions.insert( - key, - SessionEntry { - session: CultNetRudpSession::new(CultNetRudpSessionOptions { - connection_id: key.connection_id, - initial_sequence: 1, - resend_delay_ms: duration_millis(self.options.resend_delay), - max_pending_reliable_packets: Some( - self.options.max_pending_reliable_packets_per_session, - ), - }), - created_at_monotonic_millis: now, - last_activity_monotonic_millis: now, - admitted_payload_bytes: 0, - }, - ); - } - - let accept = { - let entry = self - .sessions - .get_mut(&key) - .expect("accepted session must be present"); - // The same key is a retransmitted Connect for the existing epoch. - // A genuinely fresh client incarnation must choose a fresh id. - entry.last_activity_monotonic_millis = now; - entry.session.accept_connect(packet, now, Vec::new())? - }; - self.send_packet(key.remote_addr, &accept) - } - - fn deliver_application_message( - &mut self, - key: CultMeshRudpSessionKey, - transport_sequence: u32, - message: CultNetMessage, - now_unix: u64, - now: u64, - ) -> Result> { - match message { - CultNetMessage::DocumentPutRaw { - message_id, - document, - } => { - let receipt = CultMeshRudpRawDocumentReceipt { - session: key, - message_id: message_id.clone(), - transport_sequence, - received_at_unix_millis: now_unix, - document, - }; - if let Err(error) = self.sink.accept_raw_document(receipt) { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::DocumentPutRaw, - message_id, - reason: format!("{error:#}"), - })); - } - } - CultNetMessage::SnapshotRequest { - message_id, - schema_ids, - record_keys, - } => { - let query = CultMeshRudpSnapshotQuery { - session: key, - message_id: message_id.clone(), - requested_at_unix_millis: now_unix, - schema_ids, - record_keys, - }; - let documents = match self.snapshot_source.raw_snapshot(&query) { - Ok(documents) => documents, - Err(error) => { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: format!("{error:#}"), - })); - } - }; - if documents.len() > self.options.max_snapshot_documents { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: format!( - "snapshot returned {} documents; limit is {}", - documents.len(), - self.options.max_snapshot_documents - ), - })); - } - let response = CultNetMessage::SnapshotResponseRaw { - message_id: message_id.clone(), - documents, - }; - let payload = match encode_cultnet_message_to_vec( - &response, - CultNetWireContract::CultNetSchemaV0, - ) { - Ok(payload) => payload, - Err(error) => { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: format!("snapshot response could not be encoded: {error:#}"), - })); - } - }; - if payload.len() > self.options.max_snapshot_response_bytes { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: format!( - "snapshot response is {} bytes; limit is {}", - payload.len(), - self.options.max_snapshot_response_bytes - ), - })); - } - if !self.payload_budget_allows(key, payload.len()) { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: "retained payload budget is full".into(), - })); - } - let payload_bytes = payload.len(); - let entry = self - .sessions - .get_mut(&key) - .ok_or_else(|| anyhow!("CultMesh RUDP session disappeared before response"))?; - let packets = match entry.session.send_many( - "schema", - payload, - CultNetRudpSendOptions { - reliable: true, - ordered: true, - sequenced: false, - now_ms: now, - reliable_expire_after_ms: None, - }, - Some(self.options.max_fragment_bytes), - ) { - Ok(packets) => packets, - Err(error) => { - return Ok(Some(CultMeshRudpApplicationRejection { - session: key, - operation: CultMeshRudpApplicationOperation::SnapshotRequest, - message_id, - reason: format!("snapshot response could not be queued: {error:#}"), - })); - } - }; - entry.admitted_payload_bytes = - entry.admitted_payload_bytes.saturating_add(payload_bytes); - for packet in &packets { - self.send_packet(key.remote_addr, packet)?; - } - } - _ => {} - } - Ok(None) - } - - fn payload_budget_allows(&self, key: CultMeshRudpSessionKey, bytes: usize) -> bool { - let Some(session_bytes) = self - .sessions - .get(&key) - .map(|entry| entry.admitted_payload_bytes) - else { - return false; - }; - if session_bytes - .checked_add(bytes) - .is_none_or(|total| total > self.options.max_admitted_payload_bytes_per_session) - { - return false; - } - self.sessions - .values() - .try_fold(0_usize, |total, entry| { - total.checked_add(entry.admitted_payload_bytes) - }) - .and_then(|total| total.checked_add(bytes)) - .is_some_and(|total| total <= self.options.max_admitted_payload_bytes) - } - - fn send_packet(&mut self, remote_addr: SocketAddr, packet: &CultNetRudpPacket) -> Result<()> { - let wire = encode_rudp_packet(packet)?; - self.socket.send_to(&wire, remote_addr)?; - Ok(()) - } -} - -fn validate_options(options: &CultMeshRudpDocumentServerOptions) -> Result<()> { - if options.max_sessions == 0 { - return Err(anyhow!("max_sessions must be greater than zero")); - } - if options.session_idle_timeout.is_zero() { - return Err(anyhow!("session_idle_timeout must be greater than zero")); - } - if options.session_max_lifetime.is_zero() { - return Err(anyhow!("session_max_lifetime must be greater than zero")); - } - if options.max_admitted_payload_bytes == 0 { - return Err(anyhow!( - "max_admitted_payload_bytes must be greater than zero" - )); - } - if options.max_admitted_payload_bytes_per_session == 0 - || options.max_admitted_payload_bytes_per_session > options.max_admitted_payload_bytes - { - return Err(anyhow!( - "per-session admitted payload limit must be non-zero and no larger than the server limit" - )); - } - if options.max_snapshot_response_bytes == 0 - || options.max_snapshot_response_bytes > options.max_admitted_payload_bytes_per_session - { - return Err(anyhow!( - "snapshot response limit must be non-zero and fit in one session's payload budget" - )); - } - if options.max_snapshot_documents == 0 { - return Err(anyhow!("max_snapshot_documents must be greater than zero")); - } - if options.resend_delay.is_zero() { - return Err(anyhow!("resend_delay must be greater than zero")); - } - if options.max_pending_reliable_packets_per_session == 0 { - return Err(anyhow!( - "max_pending_reliable_packets_per_session must be greater than zero" - )); - } - if options.max_fragment_bytes == 0 { - return Err(anyhow!("max_fragment_bytes must be greater than zero")); - } - Ok(()) -} - -fn duration_millis(duration: Duration) -> u64 { - duration.as_millis().try_into().unwrap_or(u64::MAX) -} diff --git a/vendor/cultmesh-rs/tests/rudp_document_server.rs b/vendor/cultmesh-rs/tests/rudp_document_server.rs deleted file mode 100644 index 1e57aaf..0000000 --- a/vendor/cultmesh-rs/tests/rudp_document_server.rs +++ /dev/null @@ -1,553 +0,0 @@ -use anyhow::Result; -use cultmesh_rs::{ - CultMeshRudpApplicationOperation, CultMeshRudpDocumentServer, - CultMeshRudpDocumentServerOptions, CultMeshRudpPollOutcome, CultMeshRudpRawDocumentReceipt, - CultMeshRudpRawDocumentSink, CultMeshRudpServerClock, CultMeshRudpSnapshotQuery, - CultMeshRudpSnapshotSource, -}; -use cultnet_rs::{ - CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, - CultNetRudpReliableSendReceipt, CultNetRudpReliableSendStatus, CultNetRudpSocketMode, - CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, - decode_cultnet_message_from_slice, encode_cultnet_message_to_vec, -}; -use pretty_assertions::assert_eq; -use std::net::{SocketAddr, UdpSocket}; -use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; -use std::thread; -use std::time::Duration; - -#[derive(Default)] -struct SinkState { - receipts: Vec, - failures_remaining: usize, -} - -#[derive(Clone, Default)] -struct Sink(Arc>); - -impl Sink { - fn fail_once() -> Self { - Self(Arc::new(Mutex::new(SinkState { - failures_remaining: 1, - ..Default::default() - }))) - } -} - -impl CultMeshRudpRawDocumentSink for Sink { - fn accept_raw_document(&mut self, receipt: CultMeshRudpRawDocumentReceipt) -> Result<()> { - let mut state = self.0.lock().unwrap(); - if state.failures_remaining > 0 { - state.failures_remaining -= 1; - anyhow::bail!("injected sink failure"); - } - state.receipts.push(receipt); - Ok(()) - } -} - -#[derive(Default)] -struct SourceState { - documents: Vec, - failures_remaining: usize, - calls: usize, -} - -#[derive(Clone, Default)] -struct Source(Arc>); - -impl Source { - fn documents(documents: Vec) -> Self { - Self(Arc::new(Mutex::new(SourceState { - documents, - ..Default::default() - }))) - } - - fn fail_once(documents: Vec) -> Self { - Self(Arc::new(Mutex::new(SourceState { - documents, - failures_remaining: 1, - calls: 0, - }))) - } -} - -impl CultMeshRudpSnapshotSource for Source { - fn raw_snapshot( - &mut self, - _: &CultMeshRudpSnapshotQuery, - ) -> Result> { - let mut state = self.0.lock().unwrap(); - state.calls += 1; - if state.failures_remaining > 0 { - state.failures_remaining -= 1; - anyhow::bail!("injected source failure"); - } - Ok(state.documents.clone()) - } -} - -#[derive(Clone)] -struct Clock { - unix: Arc, - monotonic: Arc, -} - -impl Clock { - fn new(now: u64) -> Self { - Self { - unix: Arc::new(AtomicU64::new(now)), - monotonic: Arc::new(AtomicU64::new(now)), - } - } - - fn set(&self, now: u64) { - self.unix.store(now, Ordering::SeqCst); - self.monotonic.store(now, Ordering::SeqCst); - } - - fn set_unix(&self, now: u64) { - self.unix.store(now, Ordering::SeqCst); - } - - fn set_monotonic(&self, now: u64) { - self.monotonic.store(now, Ordering::SeqCst); - } -} - -impl CultMeshRudpServerClock for Clock { - fn now_unix_millis(&self) -> u64 { - self.unix.load(Ordering::SeqCst) - } - - fn now_monotonic_millis(&self) -> u64 { - self.monotonic.load(Ordering::SeqCst) - } -} - -type Server = CultMeshRudpDocumentServer; - -fn document(key: &str, payload: Vec) -> CultNetRawDocumentRecord { - CultNetRawDocumentRecord { - schema_id: "test.raw.v1".into(), - record_key: key.into(), - stored_at: "2026-09-03T00:00:00Z".into(), - payload_encoding: CultNetRawPayloadEncoding::Messagepack, - payload, - source_runtime_id: Some(format!("runtime-{key}")), - source_agent_id: None, - source_role: None, - tags: None, - } -} - -fn server( - options: CultMeshRudpDocumentServerOptions, - clock: Clock, - sink: Sink, - source: Source, -) -> Result { - CultMeshRudpDocumentServer::new( - UdpSocket::bind("127.0.0.1:0")?, - sink, - source, - clock, - options, - ) -} - -fn client(target: SocketAddr, id: u32) -> Result { - let socket = UdpSocket::bind("127.0.0.1:0")?; - socket.set_nonblocking(true)?; - CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions { - runtime_id: "test-client".into(), - socket, - mode: CultNetRudpSocketMode::Client, - remote_addr: Some(target), - connection_id: id, - initial_sequence: 1, - resend_delay_ms: 10, - transport_id: None, - max_payload_bytes: None, - max_fragment_bytes: Some(1200), - max_pending_reliable_packets: Some(64), - media_reliable_expire_after_ms: None, - }) -} - -fn connect( - server: &mut Server, - clients: &mut [&mut CultNetRudpSocketTransportConnection], -) -> Result<()> { - for client in &mut *clients { - client.connect(Vec::new())?; - } - for _ in 0..500 { - server.poll_once()?; - for client in &mut *clients { - client.receive_once()?; - } - if clients.iter().all(|client| client.connected()) { - for _ in 0..clients.len() { - server.poll_once()?; - } - return Ok(()); - } - thread::sleep(Duration::from_millis(1)); - } - anyhow::bail!("client connection timed out") -} - -fn send(client: &mut CultNetRudpSocketTransportConnection, message: &CultNetMessage) -> Result<()> { - client.send( - "schema", - encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?, - ) -} - -fn send_reliable( - client: &mut CultNetRudpSocketTransportConnection, - message: &CultNetMessage, -) -> Result { - client.send_reliable( - "schema", - encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0)?, - ) -} - -#[test] -fn same_connection_id_is_peer_scoped_and_raw_bytes_are_untouched() -> Result<()> { - let clock = Clock::new(42_000); - let sink = Sink::default(); - let mut server = server(Default::default(), clock, sink.clone(), Source::default())?; - let mut a = client(server.local_addr()?, 77)?; - let mut b = client(server.local_addr()?, 77)?; - connect(&mut server, &mut [&mut a, &mut b])?; - - let a_bytes = vec![0xc4, 0x04, 0x00, 0xff, 0x80, 0x01]; - let b_bytes = vec![0x92, 0x01, 0xc4, 0x02, 0x00, 0xff]; - send( - &mut a, - &CultNetMessage::DocumentPutRaw { - message_id: "a".into(), - document: document("a", a_bytes.clone()), - }, - )?; - send( - &mut b, - &CultNetMessage::DocumentPutRaw { - message_id: "b".into(), - document: document("b", b_bytes.clone()), - }, - )?; - for _ in 0..500 { - server.poll_once()?; - a.receive_once()?; - b.receive_once()?; - if sink.0.lock().unwrap().receipts.len() == 2 { - break; - } - } - - let receipts = sink.0.lock().unwrap(); - assert_eq!(server.session_count(), 2); - assert_eq!(receipts.receipts.len(), 2); - assert_ne!( - receipts.receipts[0].session.remote_addr, - receipts.receipts[1].session.remote_addr - ); - assert_eq!( - receipts - .receipts - .iter() - .find(|r| r.message_id == "a") - .unwrap() - .document - .payload, - a_bytes - ); - assert_eq!( - receipts - .receipts - .iter() - .find(|r| r.message_id == "b") - .unwrap() - .document - .payload, - b_bytes - ); - assert!( - receipts - .receipts - .iter() - .all(|r| r.received_at_unix_millis == 42_000) - ); - Ok(()) -} - -#[test] -fn duplicate_connect_preserves_epoch_and_fresh_epoch_is_separate() -> Result<()> { - let clock = Clock::new(45_000); - let sink = Sink::default(); - let mut server = server(Default::default(), clock, sink.clone(), Source::default())?; - let target = server.local_addr()?; - let mut incumbent = client(target, 91)?; - connect(&mut server, &mut [&mut incumbent])?; - - incumbent.connect(Vec::new())?; - server.poll_once()?; - incumbent.receive_once()?; - assert_eq!(server.session_count(), 1); - - send( - &mut incumbent, - &CultNetMessage::DocumentPutRaw { - message_id: "after-duplicate".into(), - document: document("incumbent", vec![1, 2, 3]), - }, - )?; - for _ in 0..20 { - server.poll_once()?; - if sink.0.lock().unwrap().receipts.len() == 1 { - break; - } - } - assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); - - let mut fresh_epoch = client(target, 92)?; - connect(&mut server, &mut [&mut fresh_epoch])?; - assert_eq!(server.session_count(), 2); - Ok(()) -} - -#[test] -fn application_rejection_is_nonfatal_peer_scoped_and_unacknowledged() -> Result<()> { - let clock = Clock::new(48_000); - let sink = Sink::fail_once(); - let source = Source::fail_once(vec![document("snapshot", vec![4, 5, 6])]); - let mut server = server( - Default::default(), - clock.clone(), - sink.clone(), - source.clone(), - )?; - let target = server.local_addr()?; - - let mut publisher = client(target, 101)?; - let mut snapshot_client = client(target, 102)?; - let mut survivor = client(target, 103)?; - connect( - &mut server, - &mut [&mut publisher, &mut snapshot_client, &mut survivor], - )?; - let publish_receipt = send_reliable( - &mut publisher, - &CultNetMessage::DocumentPutRaw { - message_id: "rejected-put".into(), - document: document("rejected", vec![0, 255, 1]), - }, - )?; - let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = server.poll_once()? else { - panic!("sink rejection must be returned as a nonfatal poll outcome"); - }; - assert_eq!(rejection.session.connection_id, 101); - assert_eq!( - rejection.operation, - CultMeshRudpApplicationOperation::DocumentPutRaw - ); - assert_eq!(rejection.message_id, "rejected-put"); - assert!(rejection.reason.contains("injected sink failure")); - assert_eq!(server.session_count(), 2); - publisher.receive_once()?; - assert_eq!( - publisher.reliable_send_status(&publish_receipt), - CultNetRudpReliableSendStatus::Pending - ); - - let snapshot_receipt = send_reliable( - &mut snapshot_client, - &CultNetMessage::SnapshotRequest { - message_id: "rejected-snapshot".into(), - schema_ids: None, - record_keys: None, - }, - )?; - let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = server.poll_once()? else { - panic!("source rejection must be returned as a nonfatal poll outcome"); - }; - assert_eq!(rejection.session.connection_id, 102); - assert_eq!( - rejection.operation, - CultMeshRudpApplicationOperation::SnapshotRequest - ); - assert_eq!(rejection.message_id, "rejected-snapshot"); - assert!(rejection.reason.contains("injected source failure")); - assert_eq!(server.session_count(), 1); - snapshot_client.receive_once()?; - assert_eq!( - snapshot_client.reliable_send_status(&snapshot_receipt), - CultNetRudpReliableSendStatus::Pending - ); - - let survivor_receipt = send_reliable( - &mut survivor, - &CultNetMessage::DocumentPutRaw { - message_id: "accepted-after-rejections".into(), - document: document("survivor", vec![9, 8, 7]), - }, - )?; - assert_eq!(server.poll_once()?, CultMeshRudpPollOutcome::Handled); - for _ in 0..20 { - survivor.receive_once()?; - if survivor.reliable_send_status(&survivor_receipt) - == CultNetRudpReliableSendStatus::Acknowledged - { - break; - } - } - assert_eq!( - survivor.reliable_send_status(&survivor_receipt), - CultNetRudpReliableSendStatus::Acknowledged - ); - assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); - assert_eq!(source.0.lock().unwrap().calls, 1); - Ok(()) -} - -#[test] -fn snapshot_response_resends_until_acknowledged() -> Result<()> { - let clock = Clock::new(51_000); - let expected = document("catalog", vec![0x81, 0xa1, 0x78, 0x2a]); - let source = Source::documents(vec![expected.clone()]); - let options = CultMeshRudpDocumentServerOptions { - resend_delay: Duration::from_millis(10), - ..Default::default() - }; - let mut server = server(options, clock.clone(), Sink::default(), source)?; - let mut client = client(server.local_addr()?, 88)?; - connect(&mut server, &mut [&mut client])?; - send( - &mut client, - &CultNetMessage::SnapshotRequest { - message_id: "snapshot".into(), - schema_ids: Some(vec!["test.raw.v1".into()]), - record_keys: Some(vec!["catalog".into()]), - }, - )?; - server.poll_once()?; - - clock.set(51_011); - assert_eq!(server.maintain()?.packets_resent, 1); - let response = loop { - if let Some(frame) = client.receive_once()? { - break decode_cultnet_message_from_slice( - &frame.payload, - CultNetWireContract::CultNetSchemaV0, - )?; - } - }; - assert_eq!( - response, - CultNetMessage::SnapshotResponseRaw { - message_id: "snapshot".into(), - documents: vec![expected], - } - ); - - for _ in 0..20 { - server.poll_once()?; - } - clock.set(51_022); - assert_eq!(server.maintain()?.packets_resent, 0); - Ok(()) -} - -#[test] -fn payload_and_snapshot_output_budgets_fail_closed() -> Result<()> { - let clock = Clock::new(55_000); - let sink = Sink::default(); - let message = CultNetMessage::DocumentPutRaw { - message_id: "budget".into(), - document: document("budget", vec![7; 64]), - }; - let encoded = encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0)?; - let options = CultMeshRudpDocumentServerOptions { - max_admitted_payload_bytes: encoded.len(), - max_admitted_payload_bytes_per_session: encoded.len(), - max_snapshot_response_bytes: encoded.len(), - ..Default::default() - }; - let mut budget_server = server(options, clock, sink.clone(), Source::default())?; - let target = budget_server.local_addr()?; - let mut first = client(target, 111)?; - let mut second = client(target, 112)?; - connect(&mut budget_server, &mut [&mut first, &mut second])?; - send(&mut first, &message)?; - budget_server.poll_once()?; - send(&mut second, &message)?; - budget_server.poll_once()?; - assert_eq!(sink.0.lock().unwrap().receipts.len(), 1); - - let oversized = Source::documents(vec![document("oversized", vec![8; 512])]); - let options = CultMeshRudpDocumentServerOptions { - max_snapshot_response_bytes: 128, - ..Default::default() - }; - let mut snapshot_server = server(options, Clock::new(56_000), Sink::default(), oversized)?; - let mut snapshot_client = client(snapshot_server.local_addr()?, 113)?; - connect(&mut snapshot_server, &mut [&mut snapshot_client])?; - let receipt = send_reliable( - &mut snapshot_client, - &CultNetMessage::SnapshotRequest { - message_id: "too-large".into(), - schema_ids: None, - record_keys: None, - }, - )?; - let CultMeshRudpPollOutcome::ApplicationRejected(rejection) = snapshot_server.poll_once()? - else { - panic!("oversized snapshot must reject only its session"); - }; - assert_eq!( - rejection.operation, - CultMeshRudpApplicationOperation::SnapshotRequest - ); - assert_eq!(rejection.message_id, "too-large"); - assert!(rejection.reason.contains("snapshot response is")); - assert_eq!(snapshot_server.session_count(), 0); - snapshot_client.receive_once()?; - assert_eq!( - snapshot_client.reliable_send_status(&receipt), - CultNetRudpReliableSendStatus::Pending - ); - Ok(()) -} - -#[test] -fn session_cap_and_expiry_use_monotonic_time() -> Result<()> { - let clock = Clock::new(60_000); - let options = CultMeshRudpDocumentServerOptions { - max_sessions: 1, - session_idle_timeout: Duration::from_secs(1), - session_max_lifetime: Duration::from_millis(100), - ..Default::default() - }; - let mut server = server(options, clock.clone(), Sink::default(), Source::default())?; - let target = server.local_addr()?; - let mut admitted = client(target, 1)?; - connect(&mut server, &mut [&mut admitted])?; - let mut rejected = client(target, 1)?; - rejected.connect(Vec::new())?; - server.poll_once()?; - assert_eq!(server.session_count(), 1); - assert!(!rejected.connected()); - - clock.set_unix(1); - clock.set_monotonic(60_101); - assert_eq!(server.maintain()?.sessions_expired, 1); - assert_eq!(server.session_count(), 0); - Ok(()) -} From 222fe038963467a0da6173291bce1f04cd7c7241 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 5 Sep 2026 18:16:38 +0200 Subject: [PATCH 93/93] Move Idunn's unit files, docs and operator scripts out of Odin Completes the extraction. Unit files and documentation went to the Idunn repository, where a service's own deployment surface belongs. Operator scripts went to gamecult-ops, which is where fleet tooling lives. To Idunn: scripts/linux/idunn-yggdrasil{,.service,.sudoers}, docs/idunn.md, docs/idunn-deployment-authority.md, docs/idunn-signed-daemon-health-authority.md, src/Idunn/README.md. To gamecult-ops/scripts/idunn/: the eight PowerShell and cmd operator tools -- deployment targets, startup install, swarm coverage health, operator alarm notification, local start. Staying in Odin, deliberately: crates/odin-core/src/idunn.rs, Odin's own projection of Idunn state for aggregation; src/odin/idunn-rudp.cjs, its client for talking to Idunn; test/idunn-signed-health.test.cjs, its own integration test; and deployment/idunn/recipe.toml, which despite the path builds odin-daemon and is Odin's recipe expressed in Idunn's format. docs/transport-shortcut-inventory.md is a dated evidence ledger citing several of the moved script paths. Its entries are left as written and a path note added at the top -- rewriting a ledger to match a later reorganisation would destroy the record it exists to keep. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 79 +--- docs/idunn-deployment-authority.md | 355 ---------------- docs/idunn-signed-daemon-health-authority.md | 227 ----------- docs/idunn.md | 212 ---------- docs/transport-shortcut-inventory.md | 7 + ...health-idunn-swarm-deployment-coverage.cmd | 2 - ...health-idunn-swarm-deployment-coverage.ps1 | 79 ---- scripts/idunn-deployment-targets.ps1 | 289 ------------- scripts/install-idunn-startup.ps1 | 26 -- scripts/linux/idunn-yggdrasil | 383 ------------------ scripts/linux/idunn-yggdrasil.service | 26 -- scripts/linux/idunn-yggdrasil.sudoers | 24 -- scripts/notify-idunn-operator-alarm.cmd | 2 - scripts/notify-idunn-operator-alarm.ps1 | 25 -- scripts/show-idunn-deployment-targets.ps1 | 8 - scripts/start-idunn-local.ps1 | 179 -------- src/Idunn/README.md | 319 --------------- 17 files changed, 8 insertions(+), 2234 deletions(-) delete mode 100644 docs/idunn-deployment-authority.md delete mode 100644 docs/idunn-signed-daemon-health-authority.md delete mode 100644 docs/idunn.md delete mode 100644 scripts/health-idunn-swarm-deployment-coverage.cmd delete mode 100644 scripts/health-idunn-swarm-deployment-coverage.ps1 delete mode 100644 scripts/idunn-deployment-targets.ps1 delete mode 100644 scripts/install-idunn-startup.ps1 delete mode 100644 scripts/linux/idunn-yggdrasil delete mode 100644 scripts/linux/idunn-yggdrasil.service delete mode 100644 scripts/linux/idunn-yggdrasil.sudoers delete mode 100644 scripts/notify-idunn-operator-alarm.cmd delete mode 100644 scripts/notify-idunn-operator-alarm.ps1 delete mode 100644 scripts/show-idunn-deployment-targets.ps1 delete mode 100644 scripts/start-idunn-local.ps1 delete mode 100644 src/Idunn/README.md diff --git a/Cargo.lock b/Cargo.lock index ffb1e41..15e6e47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,6 +203,7 @@ dependencies = [ [[package]] name = "cultmesh-rs" version = "0.1.0" +source = "git+https://github.com/GameCult/CultLib.git?rev=c13b6ba07e99f2535b455da74d07d88c308731f5#c13b6ba07e99f2535b455da74d07d88c308731f5" dependencies = [ "anyhow", "cultcache-rs", @@ -503,25 +504,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" -[[package]] -name = "idunn-daemon" -version = "0.1.0" -dependencies = [ - "anyhow", - "chrono", - "cultcache-rs", - "cultmesh-rs", - "cultnet-rs", - "ed25519-dalek", - "libc", - "rmp-serde", - "serde", - "sha2", - "tempfile", - "toml", - "uuid", -] - [[package]] name = "indexmap" version = "2.14.0" @@ -920,15 +902,6 @@ dependencies = [ "zmij", ] -[[package]] -name = "serde_spanned" -version = "0.6.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" -dependencies = [ - "serde", -] - [[package]] name = "sha2" version = "0.10.9" @@ -1025,47 +998,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "toml" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.22.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "toml_write", - "winnow", -] - -[[package]] -name = "toml_write" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" - [[package]] name = "typenum" version = "1.20.1" @@ -1395,15 +1327,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "winnow" -version = "0.7.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" -dependencies = [ - "memchr", -] - [[package]] name = "wit-bindgen" version = "0.51.0" diff --git a/docs/idunn-deployment-authority.md b/docs/idunn-deployment-authority.md deleted file mode 100644 index fafa1f2..0000000 --- a/docs/idunn-deployment-authority.md +++ /dev/null @@ -1,355 +0,0 @@ -# Idunn Deployment Authority - -Idunn is GameCult's deployment, runtime-admission, continuity, and future swarm -control plane. It decides which exact workload incarnation may act. It does not -replace systemd, container runtimes, nginx, or a future general-purpose -orchestrator. - -The architectural test is simple: - -- Where and how a workload runs is generic infrastructure work. Use the - configured systemd, container, proxy, or later Nomad/Kubernetes driver. -- Which GameCult incarnation has authority to serve, write state, or represent - an admitted workload is Idunn work. - -Idunn and Odin are closely integrated but do not share authority. Idunn owns -the admitted physical swarm. Odin owns the discoverable semantic topology of -the Verse. A service owns its signed runtime presence, capabilities, capacity, -and health. CultMesh carries typed projections between them. - -## Authority map - -- **Owner:** Idunn alone selects an exact source, admits its repository recipe - under an operator binding, records the resulting artifact and process - provenance, grants a process-bound state-write lease, separately admits - route membership, and keeps the admitted incarnation alive. -- **Inputs:** root-admitted operator bindings; source-owned strict deployment - declarations; exact Git objects read as the `idunn` identity; signed - deployment or lifecycle-brake state; signed incarnation-bound service - presence and health; Odin's observed capability topology when graph changes - require semantic discovery; and observations from the configured workload - and route drivers. -- **Outputs:** typed CultCache deployment plans and receipts; an Idunn-owned - expected-fleet CultMesh projection; process-bound write grants; admitted - route membership; deployment and continuity decisions; and explicit - expected/present/ready disagreement records. -- **Derived state:** release dashboards, operator summaries, route displays, - capacity summaries, and Odin's correlation views are projections. A target - count, proxy backend, service-manager unit, container label, or configured - endpoint cannot establish admission by itself. -- **Forbidden writers:** target repositories cannot grant themselves users, - secrets, mounts, network, capabilities, resources, host paths, routes, - placement, source refs, or rollout policy. Odin cannot promote a process or - grant state writes. nginx cannot choose membership. systemd and container - runtimes cannot choose the admitted incarnation. Service configuration cannot - impersonate observed presence or readiness. Persisted shell strings and - repository-supplied root programs are not executable authority. -- **Shared paths:** `idunn up`, scheduled deployment, manual deployment, - scaling, restart, host reboot recovery, and crash recovery use the same - source-to-recipe-to-artifact provenance and the same distinct write-lease and - route-membership primitives. - Deployment may change that chain only while the deployment brake admits the - exact transaction. Continuity may restart only the already-admitted chain and - is governed by a separate lifecycle brake. -- **Deletion line:** hard-coded target copies, raw `deploy_command` and - `restart_command` state, `sh -c` actuation, root Git inspection, staged - `/srv/odin/deploy-manifests`, target-specific gamecult-ops deploy programs, - duplicate target units, and stop-incumbent-before-probe rollouts must cease to - decide Ghostlight or CodexConnector deployment before either target is - promoted through this design. - -### Current implementation boundary - -`deployment.rs` and `deployment_plan.rs` are the deterministic contract -foundation only. They parse one strict source declaration and one root-admitted -operator binding from the exact raw blobs retained by the compiled plan, check -private source-selection facts, derive managed providers from validated Expected -documents, and content-address a sealed release. They do not yet -drive Git, runners, systemd, nginx, brakes, CultCache, CultMesh, or the legacy -Idunn supervisor. - -The full operator binding, compiled plan, source facts, and sealed release -are private Idunn control-plane state. They contain host paths and operational -bindings and must never be published as Odin topology. Only the sanitized -`ExpectedIncarnation` projection is shaped for later CultMesh publication. It -is derived only from a sealed release validated against its compiled plan and -names both content addresses and the executable artifact digest. Present and -Ready remain Odin-owned correlations over independently verified runtime -evidence; the deployment-plan module deliberately does not model them. - -## The three truths Odin exposes - -For each workload incarnation, Odin keeps three distinct observations: - -1. **Expected** — Idunn has sealed this exact incarnation and has published its - plan and release identities, admitted runtime and signer identities, health - and optional canonical typed-MessagePack state-contract identities, - endpoints, declared capabilities, and dependency role. -2. **Present** — Idunn has published the launch-scoped activation only after its - actuator observed the exact runtime instance, and that instance has - established its own matching signed CultNet/CultMesh runtime session. -3. **Ready** — the observed incarnation satisfies its signed capability and - health contract and its required dependency graph is ready. - -Correlation never repairs disagreement. Incarnation, runtime identity, endpoint, -schema, capability, and dependency mismatches remain visible and block -promotion. Configuration contributes only expected state; it cannot supply -present or ready state. - -Every Idunn-managed deployment has an Odin-visible expected projection before -dependent services are promoted, and an Odin-observed runtime projection before -they are considered ready. - -## Source-owned deployment declaration - -Each deployable repository publishes one small, reviewable declaration at a -fixed path such as `deployment/idunn/recipe.toml`. Unknown fields are rejected. -The declaration contains no host commands or privileges. It describes: - -- schema and target identity; -- requested runner profiles; -- build and test argv executed inside those runners; -- pinned generic HTTPS inputs and immutable artifact outputs; -- a constrained launch contract naming the packaged executable, literal - arguments, operator-bound argument slots, and required environment names; -- one health contract and, only for stateful services, a state/schema contract; -- capabilities provided, including schema and compatibility information; -- bootstrap, required, optional, shared-infrastructure, private, and external - operator-bound capability dependencies; -- startup ordering, conflicts, migration conditions, and minimum capacity when - they matter. - -Dependencies resolve by typed capability/provider compatibility. A declaration -names a daemon only when identity itself is the contract; ordinary dependencies -must not hard-code a provider instance. - -Runtime health uses the shared `warming`, `active`, `degraded`, and `failed` -ontology. A recipe names only its health contract. It cannot redefine those -states, readiness, or operator-facing detail. Bootstrap dependencies must be -available before process start; the remaining dependency kinds retain their -typed intent without making this foundation a scheduler. - -The declaration may run repository code only inside a runner sandbox admitted -by the operator binding. It cannot publish a host path, Docker argument, -systemd property, nginx fragment, UID, secret path, or capability grant. Idunn -deterministically lowers the constrained launch contract through the configured -workload driver; repository-owned raw unit or container-runtime text is not an -input. - -## Operator binding - -An operator binding is static root-admitted configuration, not observed state. -For one target on one fleet it owns: - -- canonical repository origin, admitted ref, and minimum revision; -- exact runner kind, image digest, and affordances for each requested profile; -- source, cache, output, release, and runtime roots, plus state roots and - transition policy only when the recipe declares state; -- preconfigured runner network profiles, mounts, identities, secrets, - capabilities, resource bounds, and device access; -- workload driver, transient-unit namespace, private endpoint range, and stable - route; -- runtime identity, expected signer identity, and trust-anchor store; -- a process-write-lease record only when the recipe declares process-bound - writable state; its sibling lock path is derived rather than configured; -- route driver and its root-owned configuration destination; -- rollout, drain, irreversible-cut, and retention policy; -- one admitted replica on one named node in v1; later placement and scaling are - delegated to an established orchestrator driver; -- externally supplied capability bindings. - -A recipe request that has no compatible admitted binding fails closed. A -binding can grant less than requested; it cannot silently substitute a -different semantic capability. - -`gamecult-ops` publishes these fleet bindings and provisions host substrate. It -does not own application build or deployment programs. - -## Typed deployment state - -Mutable control-plane state will be CultCache. Runtime integration must use -typed records for: - -- requested service or profile; -- resolved capability graph and exact providers; -- exact source and recipe digest; -- runner binding and artifact digests; -- full materialization receipts for every pinned external input and artifact; -- candidate runtime identity, private endpoint, and signed runtime health; -- launch-scoped runtime activation, published as current only after workload- - driver observation; -- process-bound write lease; -- independently admitted route membership; -- expected, present, and ready correlation; -- promotion, drain, failure, and continuity results. - -Old records containing raw commands are readable history only. No executor may -interpret their command text. - -State transition is also typed authority. The repository may package a -migration artifact and declare its accepted source schemas, destination schema, -and literal/operator-bound argv. The operator binding admits the state roots, -backup root, and whether this rollout permits migration or a fresh-root cut. -Idunn alone issues a fenced one-shot migration grant bound to the exact source, -artifact, state schema generation, and deployment incarnation. A target -migration never receives general deployment or lifecycle authority. - -## Initial drivers and replaceable boundary - -The first Yggdrasil implementation uses: - -- transient systemd units for process lifetime and isolation, regenerated from - Idunn's admitted state after Idunn starts; -- Docker only for admitted build/test/package runners; -- nginx HTTP proxying for Ghostlight; -- nginx stream proxying for CodexConnector's stable TCP endpoint; -- atomic root-owned files for process-bound write leases and separately - generated proxy membership; -- graceful nginx validation and reload for route changes. - -Idunn calls narrow workload, route, and write-lease/fencing driver ports. Those -ports express stage, start, observe, stop, validate route, fence an incumbent, -publish a process-bound write lease, and separately promote route membership. -They do not express GameCult admission decisions. A later Nomad or Kubernetes -driver may implement the same consequences. Idunn must not grow general -bin-packing, distributed scheduling, service-mesh, container-format, -cryptography, or consensus logic; when those become requirements, place -Idunn's admission semantics over an established orchestrator. - -## Candidate promotion - -For a singleton incarnation: - -1. Resolve the required capability graph. Reuse a compatible admitted shared - provider, especially Odin, rather than spawning a duplicate. -2. Publish desired fleet intent. Intent is not an Expected incarnation. -3. Freeze the exact source and recipe as `idunn`, then copy and verify it into a - root-owned actuation stage. The privileged driver never opens Git. -4. Build, test, and package inside the bound runners. Seal the artifact, - declaration, binding, launch contract, and input digests. -5. Derive and publish the release-bound Expected incarnation from the validated - sealed release and its exact compiled plan. -6. Issue one opaque runtime-instance activation bound to the Expected digest, - pass the exact Expected and activation documents through the actuator's - isolated credential mechanism, and start the candidate privately. It has no - stable route or canonical write lease. -7. After the workload driver observes the exact native runtime and executable - artifact, publish that activation as current. Require the candidate's own - signed matching `warming` presence. Render and validate the proposed route - before changing either authority. -8. Open a named promotion transaction and separately fence the incumbent - writer. The incumbent may remain routed but its process-bound write authority - is revoked and it must return a retryable unavailable result. -9. Grant the private candidate its own process-bound write lease. Keep it off - the stable route and wait for signed `active` health and the independently - correlated Ready observation. -10. Publish the candidate backend, gracefully reload the route driver, and - observe that the proxy has adopted it. Proxy lag is explicit transaction - state, not readiness. -11. Clear the promotion fence only after route adoption is observed, then drain - and stop the incumbent. - -The state-write lease and route membership are distinct authorities. Fencing -the incumbent does not grant the candidate or change the route. Granting the -candidate permits that exact private process to write but does not route stable -traffic. Changing the route grants no write authority. The promotion -transaction clears its fence only after the independently observed write and -route authorities name the candidate. A PID, endpoint, symlink, or unit alone -grants neither authority. - -Promotion persists crash-resumable phases: planned, materialized, sealed, -activation-issued, candidate-started, activation-observed, warming, -incumbent-fenced, write-lease-granted, ready-observed, route-observed, -fence-cleared, draining, and complete. A dead Idunn cannot leave a second -writer. Its successor resumes the exact sealed transaction or restores the -prior admitted incarnation through the same fence; it never infers completion -from a symlink or eventual health convergence. - -An irreversible state cut fails closed after the write lease moves. A reversible -cut may restore the incumbent only through the same process-write-lease -primitive and only while its artifact and state contract remain valid. - -## Continuity and outage behavior - -Continuity restarts the exact sealed release, binding, endpoint, runtime -identity, and route membership. Every restart receives a new runtime-instance -activation and, for a state writer, a new process-bound lease only after the new -warming presence is observed. A lease from the dead process is never reused. A -continuity restart does not fetch a branch, rebuild, rerun a migration, alter -the launch contract, or consume the deployment brake. An explicit lifecycle -brake may suspend this actuation without changing deployment authority. - -Idunn starts and recovers from its own durable admitted state. Odin is the first -managed semantic daemon, never an Idunn bootstrap dependency. Odin's initial -admission is the sole graph-bootstrap exception: Idunn uses its root-admitted -local binding, publishes Odin's expected projection into durable CultMesh state, -and waits for Odin's observed activation and signed presence before admitting -dependents. - -When Odin is unavailable, Idunn continues health observation and continuity for -already admitted incarnations and leaves current routes in place. It does not -begin a graph-changing transaction, and a merely sealed candidate may not start -promotion. Only a transaction whose incumbent has already been fenced may -promote when its frozen evidence already contains the exact Odin Ready receipt -for this runtime instance and signed-presence digest. Otherwise it waits, rolls -back through the same fence when reversible, or fails closed; Idunn never -manufactures Ready locally. New deployment, dependency -substitution, promotion, or scaling waits for semantic discovery. When Odin -returns, disagreement is correlated and exposed before graph-changing work -resumes. - -Source selection is explicit per binding: a pinned object, the captured head of -an admitted ref, or a signed release authorization. The current deterministic -foundation retains the exact selected commit, tree, recipe blob, and Gitlink -facts, but shape validation is not proof of Git ancestry, signature authority, -or object custody. Those proofs belong to the narrow Idunn-owned source driver -that will produce these private facts. “Whatever the branch points to when root -runsâ€� is not a selection policy. - -## Garden path - -The ordinary operator surface is: - -```text -install/start Idunn -idunn up ghostlight -idunn up profile:aetheria -idunn up profile:full-gamecult -``` - -`idunn up` ensures a compatible Odin is admitted first, publishes desired fleet -state, resolves the capability graph, stages candidates, waits for signed -presence and readiness, then promotes routes. An “Idunn scriptâ€� means the small -repository declaration plus this standard command, not another imperative host -deployment program. - -## Verification - -The first live cut is complete only when tests and host observations prove: - -- exact source → recipe → artifact → process provenance; -- an issued but driver-unobserved activation cannot establish Present, and a - prior runtime instance cannot replay presence after restart; -- target requests cannot grant an omitted runner or host affordance; -- old command-bearing records and old target scripts are unexecutable; -- a candidate cannot write before its process-bound lease or receive the stable - route before separate route admission; -- route membership and write leases remain separate records and name the same - incarnation and process before the promotion fence clears; -- expected, present, and ready disagreement blocks promotion and remains - observable; -- replayed or stale signed health cannot satisfy present or ready; -- external operator-bound dependencies still require their declared observed - health/readiness contract; configuration supplies only expected state; -- a compatible shared Odin is reused and a second provider is not spawned; -- Idunn death during every promotion phase resumes or fails closed without a - second writer; -- proxy-driver lag remains visible and cannot be mistaken for route admission; -- the old process rejects writes immediately after fence or lease revocation; -- same-release crash recovery works while the deployment brake is engaged; -- the lifecycle brake can stop continuity without admitting a deployment; -- Odin loss preserves current continuity/routes and blocks graph changes; -- the incumbent remains routed until the candidate has signed `active` health - and is correlated Ready; -- the old unit, release, state root, grant, and route cannot regain authority; -- Ghostlight restart preserves the exact world journal and digest identity. diff --git a/docs/idunn-signed-daemon-health-authority.md b/docs/idunn-signed-daemon-health-authority.md deleted file mode 100644 index c2cff0c..0000000 --- a/docs/idunn-signed-daemon-health-authority.md +++ /dev/null @@ -1,227 +0,0 @@ -# Idunn signed daemon-health authority - -## Objective - -Idunn must know which daemon authored a health statement before that statement -can affect lifecycle judgment or operator status. A typed packet is a shape, -not an identity. The current generic `idunn.daemon_health.v1` RUDP path checks -shape, daemon id, contract, self-declared publication source, and freshness but -does not authenticate the publisher. It is diagnostic input until replaced. - -## Owner - -- Each daemon owns its health statement and signs it with its pinned service or - host identity. -- Idunn owns admission, freshness, monotonic lineage, and the outward projection - of current authenticated provider health. Lifecycle judgment remains private - Idunn policy and is not smuggled into that provider-health statement. -- Root deployment configuration owns the trust binding between daemon id, - health contract, source runtime, and signer identity. - -No renderer, transport peer, systemd unit, PID probe, caller boolean, or typed -but unsigned packet owns health. - -Authenticated daemon health is selected before release-source inspection. -Release state may request a rollout of an otherwise healthy body, but a missing -checkout or upstream cannot replace the daemon's liveness statement or block -restart of an unhealthy admitted body. Continuity restart and deployment are -separate authorities: only the latter is gated by the deployment brake. - -## Inputs - -The daemon-authored statement binds: - -- exact daemon id and health contract; -- source runtime id and signer identity; -- observation time, publisher incarnation, and monotonic sequence; -- bounded health state and detail; -- release id, release witness, source revision, and deployment request when the - managed target is release-bound; -- `private_state_exposed=false`; -- an Ed25519 signature over the complete unsigned statement under a - domain-separated signing purpose. - -The Idunn trust binding names the only public key allowed to speak for that -daemon/contract/runtime tuple and whether release identity is required. - -Yggdrasil Odin uses the generic signed contract with daemon -`yggdrasil-odin`, source runtime `odin-yggdrasil`, and a root-provisioned -Ed25519 key readable only by the Odin runtime group. Its health signature owns -liveness. Exact Odin and CultLib revisions remain deployment authority in the -immutable release and `/srv/odin/deploy/deployment.env`; neither Docker health -nor the signed liveness statement may rewrite that witness. - -## Outputs - -Idunn persists two separate current facts per daemon: - -1. one admitted daemon-authored statement, keyed by daemon id, plus its exact - digest in the admission; -2. an Idunn-authored `idunn.authenticated_provider_health_projection.v1` - record binding the trust record, admission, exact provider lineage, and - freshness interval. - -Signed heartbeats are current liveness state, not an append-only event ledger. -Advancing a daemon's publisher sequence atomically replaces that daemon's prior -statement, admission, and compatibility health row. Startup atomically compacts -legacy digest-keyed statement history to the one exact statement named by each -current admission. A missing or digest-mismatched witness fails closed; it is -never reconstructed from process probes or a later heartbeat. - -The surrounding keepalive store follows a separate bounded-retention policy. -It keeps exact current authority plus small per-daemon diagnostic windows; -historical pulse decisions, repeated alarms, and superseded actuation receipts -cannot make authentication fsync cost grow with host age. The current -deployment head and every record required to rejoin active signed health remain -pinned through compaction. - -The outward record exists only while a generic provider-authored admission is -current and still matches its root trust and optional release binding. Missing -publication, release drift, dependency failure, target absence, and recovery -policy are absence of this projection. They are not synthetic provider states. -The projected state is exactly the authenticated provider state. Its only -explanation is a closed Idunn-generated reason code paired to that state: -`authenticated_provider_active`, `_warming`, `_degraded`, or `_failed`. -There is no free-text detail field. - -Consumers such as Epiphany Discord Status receive only the outward projection -through CultNet/CultMesh and verify Idunn's pinned identity. Reading Idunn's -private store is not a consumer protocol. - -The query transport is an explicit, read-only CultNet/RUDP listener enabled by -`--public-health-query-bind `. It has no default and is valid only with -`--swarm-profile`, `--service-identity-store`, and `--public-health-store`. -The listener opens only the dedicated public store and derives its allowlist -from the configured target catalog's daemon/health-contract pairs. It returns -the exact stored projection payload bytes with fixed source runtime -`idunn-daemon` and role `authenticated-provider-health-projector`. It has no -handle to Idunn's private stores and accepts no mutation message. - -## Derived state - -- Process and systemd observations are diagnostic evidence only. -- RUDP source endpoints and connection ids are transport routing only. -- `publication_source`, `transport`, `ready`, and `healthy` strings are display - data unless contained inside a valid signed statement. -- An unsigned legacy health packet may be retained temporarily for diagnostics - but cannot satisfy managed health, prevent recovery, or produce a healthy - outward projection. - -## Forbidden writers - -- The inbound packet cannot choose its own trust anchor. -- A daemon cannot change its configured daemon id, contract, or source runtime - by signing different values. -- Idunn cannot manufacture daemon-authored health from PID, executable, - container, HTTP, systemd, or command exit observations. -- Epiphany, Bifrost, VoidBot, Eve, and Discord cannot infer Idunn acceptance - from a sent packet. -- A generic typed `idunn.daemon_health.v1` packet cannot fall back into - lifecycle authority when signature verification fails or trust is absent. - -## Shared paths - -Every managed daemon uses the same verification primitive: - -`signed statement -> root trust binding -> signature and tuple verification -> -monotonic admission -> target/deployment join -> Idunn outward projection` - -Epiphany's deployed `epiphany.idunn_signed_runtime_health.v0` path is migration -evidence and the first working implementation of the signature half. It is not -the permanent excuse for a service-specific verifier. The generic contract -must absorb its invariants before the special path is deleted. - -## Cut line - -1. Add generic signed-health and trust-binding contracts. -2. Make generic Idunn admission verify the pinned tuple, signature, sequence, - incarnation, freshness, and release requirements. -3. Migrate Epiphany and Bifrost publishers and install their root-owned trust - bindings. -4. Publish and verify Idunn's outward signed authenticated-provider-health - projection using the fixed CultNet signature profile - `idunn.authenticated-provider-health-projection.v1`. -5. Point Discord Status at the outward projections. -6. Delete generic unsigned health from managed-health authority and delete the - Epiphany-only verifier after its last publisher migrates. - -The rebuild is incomplete while an unsigned packet can keep a managed daemon -healthy or while a status consumer must inspect Idunn's private store. - -## Implemented cut (2026-07-19) - -The deployed-lineage source now has the first executable generic admission -path. `idunn.signed_daemon_health.v1` is verified against an exact root-store -daemon/contract/runtime binding, the signer identity is derived from the bound -public key, the Ed25519 signature covers the positional empty-signature -statement under the domain-separated document purpose, and Idunn atomically -persists the signed statement, its generic admission, and the compatibility -health row. Managed-health selection rejoins all three with the current root -binding on every read; trust rotation invalidates old health. Sequence, -incarnation, observation, freshness, and optional deployment lineage advance -under CAS. - -Unsigned `idunn.daemon_health.v1` packets now persist only as -`idunn.unsigned_daemon_health_diagnostic.v1` under `diagnostic:`. -They cannot overwrite the daemon health key, create an admission, reset the -missing-publication clock, suppress recovery, or produce authenticated health. -The evaluation clock is passed through the whole target cycle instead of being -resampled inside the health selector. - -Idunn now also has the local outward projection writer. One publisher opens an -already-enrolled Idunn service identity before workers start, captures an -incarnation-stable root trust snapshot, and writes only -`idunn.authenticated_provider_health_projection.v1` envelopes to a distinct -public store. Each write rejoins the exact health, admission, signed statement, -root binding, and optional current deployment lineage. The signed positional -record has a fixed hash-derived key, a store-monotonic sequence, a process UUID, -and an expiry capped by the provider observation's managed-health silence -window. Missing, legacy, unsigned, drifted, or mutated input cannot write, -refresh, or delete a projection; an old row simply expires for consumers. - -The public projection has a dedicated multi-peer CultNet/RUDP snapshot -listener. Startup synchronously binds the explicit address and validates the -public store before target workers start. Bind failure or public-store -contamination is fatal startup evidence. Each request rereads only that store -and passes exact records through CultNet's read-only snapshot server with the -target-derived `(schema,key)` allowlist. Unknown schema/key requests return no -record. Malformed datagrams or messages are refused per peer without killing -the daemon. No request can write, delete, import, or inspect private Idunn -state. - -This source is deliberately not deployable yet: Bifrost and the remaining -generic publishers still emit the unsigned diagnostic contract, production -root trust and Idunn identity stores have not been installed, and Epiphany -still uses its signed v0 migration path. The query transport and Epiphany -consumer verifier now exist in source, but neither makes an unmigrated -publisher authoritative. Deploying this intermediate source would correctly -classify unmigrated daemons as missing health and could therefore actuate -recovery. Publisher migration and trust enrollment must land before promotion. - -## Verification layer - -Hostile tests must prove: - -- valid shape with no signature is not admitted; -- wrong key, daemon, contract, runtime, release, deployment, or trust record is - not admitted; -- replay, sequence rollback, time rollback, incarnation substitution, and - future/stale observations are not admitted; -- a live PID, existing executable, green HTTP response, or generic `ready` - string cannot repair failed authentication; -- one daemon's trust record cannot authenticate another daemon; -- unsigned legacy health cannot suppress recovery or yield outward healthy; -- outward provider health is signed by Idunn under the fixed, typed purpose and - binds the exact admitted statement; -- missing health, release drift, and dependency failure cannot write or refresh - the projection; any prior row expires instead of being rewritten as an - Idunn-authored provider state; -- caller-selected signing purposes, signer ids, reason strings, and synthetic - states fail closed; -- private detail and filesystem/process internals never enter the operator - projection. -- repeated signed heartbeats keep exactly one signed statement per admitted - daemon rather than growing the monolithic keepalive store; -- startup compaction keeps only the exact statement named by each admission, - is idempotent, and loses cleanly to a concurrent whole-store mutation rather - than overwriting it. diff --git a/docs/idunn.md b/docs/idunn.md deleted file mode 100644 index 7e26343..0000000 --- a/docs/idunn.md +++ /dev/null @@ -1,212 +0,0 @@ -# Idunn - -Idunn is GameCult's independent deployment-admission and continuity control -plane. It decides which exact workload incarnation may run, write canonical -state, receive traffic, and be kept alive. It does not depend on Odin or any -managed workload in order to start or recover its own durable admitted state. - -Systemd, container runtimes, proxies, and a future Nomad or Kubernetes driver -provide generic execution and routing. Idunn owns the GameCult-specific -decision about which source, release, runtime identity, process write lease, and -route membership are authoritative. - -The detailed contract is -[`idunn-deployment-authority.md`](idunn-deployment-authority.md). - -## Authority map - -- **Owner:** Idunn selects exact source facts under root policy, compiles the - repository declaration against an operator binding, seals materialized - inputs and artifacts, admits one runtime incarnation, separately controls its - process-bound write lease and route membership, and preserves its continuity. -- **Inputs:** the exact raw repository declaration and operator-binding blobs; - private source-driver facts; artifact and external-input materialization - receipts; separate deployment and lifecycle brakes; Odin's observed semantic - topology when a graph-changing decision needs discovery; signed runtime - presence and health; Idunn's current observed-activation projection; and - observations from narrow execution and route drivers. -- **Outputs:** private compiled plans and sealed releases; a release-bound - sanitized Expected projection for Odin/CultMesh; one launch-scoped runtime - activation published only after driver observation; process write-lease and - route decisions; continuity actions; and explicit disagreement or failure - records. -- **Derived state:** dashboards, operator summaries, proxy displays, process - listings, and Odin correlation views are projections. They do not establish - deployment, write, route, presence, or readiness authority. -- **Forbidden writers:** repositories cannot grant host privileges, identities, - trust anchors, state roots, write leases, routes, placement, or rollout - policy. Odin cannot deploy, promote, route, or grant writes. Services cannot - configure themselves Present or Ready. Proxies and process managers cannot - select the admitted incarnation. Raw command strings are not deployment - authority. -- **Shared path:** `idunn up`, scheduled deployment, operator deployment, - restart, crash recovery, and host reboot recovery all use the same admitted - source-to-release identity. Continuity may restart only that identity; it - cannot fetch, rebuild, migrate, or silently change configuration. - -## Repository declaration - -A deployable repository publishes one strict declaration, normally -`deployment/idunn/recipe.toml`. It may contain: - -- literal build, test, and package argv for named runner classes; -- generic pinned HTTPS inputs with SHA-256, runner, and relative destination; -- immutable artifact outputs, optionally pinned to an expected SHA-256; -- a constrained launch contract; -- one health-contract identity; -- an optional typed state contract with state schema generation and slots; -- provided capabilities, dependency kinds, startup requirements, capacity, and - conflicts. - -The recipe does not configure runtime health states. GameCult runtime health -uses the shared `warming`, `active`, `degraded`, and `failed` ontology. Present -and Ready are Odin correlations over independently signed runtime evidence, not -recipe fields. - -Stateless services declare no state block. Their operator binding therefore has -no state root, state-transition policy, or process write-lease record. - -## Operator binding - -The root-admitted operator binding supplies the physical policy a repository -cannot grant itself: - -- source origin, admitted ref, selection policy, checkout, and Gitlink origins; -- pinned runner images, literal-program allowlists, resources, mounts, secrets, - and preconfigured network profiles; -- workload driver, identity, isolation, release root, and runtime root; -- runtime ID, expected signer identity, and trust-anchor store; -- optional state root and state-transition policy; -- an optional process write-lease record exactly when a state slot is - process-bound writable; its sibling `.lock` path is derived; -- optional route driver and stable/private endpoint binding; -- independent deployment and lifecycle brakes; -- one replica on one admitted node in the v1 foundation. - -Route membership is not a write lease. A write lease is not route membership. -Neither may be inferred from a PID, unit, socket, symlink, or health message. - -## Deterministic release foundation - -The compiled plan retains the exact raw declaration and binding blobs and -parses those exact bytes again during validation. Its content address therefore -covers the real inputs rather than caller-supplied digests beside mutable parsed -copies. - -Source-selection facts currently preserve the private driver output: the -exact commit, source tree, recipe blob, admitted ref, and Gitlink tree entries. -The contract does not claim that shape validation proves ancestry, signatures, -or object custody. Those proofs belong to the narrow source driver that will -produce the facts. - -A sealed release binds the plan ID to sorted full artifact receipts and sorted -full external-input materialization receipts. Only a sealed release validated -against its exact plan can derive Expected. Expected includes the plan and -release IDs, runtime and signer identities, health contract, node, route, -executable artifact digest, capabilities, selected dependencies, and the -optional canonical state-contract digest computed from the typed -`StateDeclaration` MessagePack encoding. - -Expected is stable release and topology intent. Each actuator launch receives a -separate tiny `idunn.runtime_activation.v1` containing the Expected digest and -one Idunn-issued opaque runtime-instance ID. Idunn publishes that activation as -current only after the workload driver observes the matching native process and -artifact. Present requires both that Idunn observation and the service's -independent signed presence; either document alone is insufficient. - -Managed dependency candidates are derived from validated Expected documents. -External operator bindings remain expected configuration only; they cannot -impersonate signed presence or readiness. - -### Source resolution and actuation map - -- **Owner:** the Idunn deployment transaction owns the transition from one - observation of a moving admitted ref to runner-readable immutable source. - The source driver resolves through a transaction-unique fetched-ref witness; - the control loop must compile and durably persist the complete - `CompiledDeploymentPlan` before any source freeze or runner actuation. -- **Inputs:** the root-admitted raw operator-binding bytes; the exact repository - declaration blob read from the selected Git tree; one unique source-resolution - ID; the fetched admitted-ref revision; the selected commit and tree; and the - complete Gitlink path, origin, and revision set admitted by that binding. -- **Outputs:** private `ResolvedSource` facts used to compile the plan; a - plan-only exact archive in root-owned transaction storage; and a - `FrozenSourceReceipt` binding the transaction ID, plan ID, and snapshot digest. - The receipt is embedded in the observed `FrozenSource` and must validate - against the plan before a runner may consume the tree. -- **Derived state:** the source-identity checkout, unique fetched ref, temporary - Gitlink checkouts, extracted runner workspaces, and in-memory `FrozenSource` - view are disposable projections. None may replace the persisted plan, - receipt, or sealed release as transaction authority. -- **Forbidden writers:** a repository identity may perform Git and network - reads but cannot write the root-owned frozen tree. A moving ref, mutable - checkout, caller-supplied parsed binding, fresh recipe read, or incomplete - Gitlink subset cannot revise a compiled transaction. Resume may not resolve a - fresh binding or ref, and runner actuation may not accept source without its - plan-bound receipt. -- **Shared path:** initial execution resolves once, compiles and persists the - full plan, freezes only from that plan, persists the receipt, re-observes the - immutable snapshot, and runs the bound builders. Retry and crash recovery use - the same persisted plan and receipt; if freeze must be repeated, it fetches - only the exact planned revisions and never observes the moving ref again. - Transaction source storage is cleaned only after the sealed release has been - durably persisted. -- **Deletion line:** source-owned worktrees and Git metadata do not cross into - actuation storage; source selection does not return a runner-usable mutable - checkout; and no resume path is permitted to reconstruct authority from the - current repository binding or branch head. -- **Verification layer:** focused source-driver tests observe the unique - admitted-ref witness, exact Gitlink-set equality, root ownership, canonical - read-only modes, bounded symlinks, snapshot digest, and plan/receipt binding. - Transaction crash tests must additionally prove that every boundary after - plan persistence resumes without ref resolution and that cleanup cannot run - before sealed-release persistence. - -## Promotion and continuity - -For a routed stateful singleton, Idunn starts the candidate privately, observes -the exact actuator launch, publishes its current activation, and observes its -signed warming presence. It then fences the incumbent writer, grants the -candidate its exact process-bound write lease, and waits for signed active -health plus Ready correlation. It changes and observes route membership only -afterward. Only after the proxy has adopted the candidate may Idunn clear the -promotion fence and drain the incumbent. - -The deployment brake gates changes to source, release, configuration, schema, -unit, or authority binding. The lifecycle brake separately gates restart of an -already-admitted incarnation. Neither target brake may gate Idunn itself or an -unrelated service. - -If Odin is unavailable, Idunn preserves existing routes and continuity for -already-admitted incarnations. It starts no graph-changing deployment or -promotion. A merely sealed candidate waits. An already-fenced transaction may -promote only when its frozen evidence already contains the exact Odin Ready -receipt for this runtime instance and presence digest; otherwise it waits, -rolls back through the same fence when reversible, or fails closed. - -## Operator surface - -The intended entry points are small: - -```text -idunn up ghostlight -idunn up profile:aetheria -idunn up profile:full-gamecult -``` - -Idunn ensures one compatible Odin exists, publishes the desired fleet, resolves -typed capability dependencies, admits candidates, waits for independently -observed presence and readiness, and promotes routes only when the required -graph conditions hold. - -## Current implementation boundary - -`crates/idunn-daemon/src/deployment.rs` and `deployment_plan.rs` implement the -deterministic declaration, binding, plan, release, and Expected contracts. -`drivers.rs` implements the narrow source resolution/freeze, runner, systemd, -write-lease, topology, and route actuator ports. The transaction engine still -must persist and sequence the compiled plan, frozen-source receipt, sealed -release, activation, lease, readiness, route, and admitted generation. Driver -ports are consequences, not a second deployment controller; runtime integration -must not recreate target catalogs, raw deployment commands, root Git inspection, -or a second admission opinion. diff --git a/docs/transport-shortcut-inventory.md b/docs/transport-shortcut-inventory.md index b7f407f..96d59e8 100644 --- a/docs/transport-shortcut-inventory.md +++ b/docs/transport-shortcut-inventory.md @@ -1,5 +1,12 @@ # Transport Shortcut Inventory +> **Path note, 2026-09-05.** Idunn was extracted to +> https://github.com/GameCult/Idunn. Entries below citing `scripts/idunn-*.ps1`, +> `scripts/health-idunn-*`, `scripts/notify-idunn-*` or `scripts/start-idunn-local.ps1` +> refer to paths that moved to `gamecult-ops/scripts/idunn/`; unit files and Idunn +> documentation moved to the Idunn repository. This is a dated ledger and the +> entries are left as written. + Status date: 2026-07-03 ## Authority Map diff --git a/scripts/health-idunn-swarm-deployment-coverage.cmd b/scripts/health-idunn-swarm-deployment-coverage.cmd deleted file mode 100644 index 5d3ea8c..0000000 --- a/scripts/health-idunn-swarm-deployment-coverage.cmd +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File %~dp0health-idunn-swarm-deployment-coverage.ps1 diff --git a/scripts/health-idunn-swarm-deployment-coverage.ps1 b/scripts/health-idunn-swarm-deployment-coverage.ps1 deleted file mode 100644 index 03b2a73..0000000 --- a/scripts/health-idunn-swarm-deployment-coverage.ps1 +++ /dev/null @@ -1,79 +0,0 @@ -$ErrorActionPreference = "Stop" - -. (Join-Path $PSScriptRoot "idunn-deployment-targets.ps1") - -$unknown = @($IdunnDeploymentTargets | Where-Object { $_.Status -notin @("enforced", "runtime-enforced", "blocked", "external-owned", "not-runtime", "archived") }) -$enforcedWithoutDeploy = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "enforced" -and [string]::IsNullOrWhiteSpace($_.Deploy) }) -$enforcedWithoutHealth = @($IdunnDeploymentTargets | Where-Object { $_.Status -in @("enforced", "runtime-enforced") -and $_.Id -ne "idunn" -and [string]::IsNullOrWhiteSpace($_.Health) }) -$enforcedWithoutUpstream = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "enforced" -and ([string]::IsNullOrWhiteSpace($_.UpstreamRemote) -or [string]::IsNullOrWhiteSpace($_.UpstreamBranch)) }) -$enforcedWithoutRollout = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "enforced" -and ([string]::IsNullOrWhiteSpace($_.RolloutStrategy) -or [string]::IsNullOrWhiteSpace($_.StateMigration) -or [string]::IsNullOrWhiteSpace($_.ZeroDowntime)) }) -$blockedWithoutReason = @($IdunnDeploymentTargets | Where-Object { $_.Status -in @("blocked", "external-owned") -and [string]::IsNullOrWhiteSpace($_.Reason) }) -$deployScriptsWithoutIdunnGuard = @( - $IdunnDeploymentTargets | - Where-Object { $_.Status -eq "enforced" -and -not [string]::IsNullOrWhiteSpace($_.Deploy) } | - Where-Object { - if ($_.Deploy -match '^sudo -n /usr/local/libexec/idunn-yggdrasil deploy [a-z0-9-]+$') { - return $false - } - -not (Test-Path -LiteralPath $_.Deploy) -or - -not ((Get-Content -Raw -LiteralPath $_.Deploy) -match 'IDUNN_ACTUATOR') - } -) -$knownActuatorPaths = @{} -foreach ($target in $IdunnDeploymentTargets) { - foreach ($path in @($target.Deploy, $target.Restart) + @($target.Aliases)) { - if (-not [string]::IsNullOrWhiteSpace($path) -and (Test-Path -LiteralPath $path)) { - $knownActuatorPaths[[System.IO.Path]::GetFullPath($path).ToLowerInvariant()] = $target.Id - } - } -} -foreach ($path in $IdunnDeploymentSharedActuators) { - if (-not [string]::IsNullOrWhiteSpace($path)) { - $knownActuatorPaths[[System.IO.Path]::GetFullPath($path).ToLowerInvariant()] = "shared" - } -} -$guardedActuatorsWithoutCatalog = @( - Get-ChildItem -LiteralPath $PSScriptRoot -File | - Where-Object { - $_.Name -match '^(deploy|restart)-.*\.(ps1|cmd)$' -and - (Get-Content -Raw -LiteralPath $_.FullName) -match 'IDUNN_ACTUATOR' -and - -not $knownActuatorPaths.ContainsKey([System.IO.Path]::GetFullPath($_.FullName).ToLowerInvariant()) - } -) - -$issues = @() -if ($unknown.Count) { - $issues += "unknown status: $($unknown.Id -join ', ')" -} -if ($enforcedWithoutDeploy.Count) { - $issues += "enforced target missing deploy command: $($enforcedWithoutDeploy.Id -join ', ')" -} -if ($enforcedWithoutHealth.Count) { - $issues += "enforced target missing health command: $($enforcedWithoutHealth.Id -join ', ')" -} -if ($enforcedWithoutUpstream.Count) { - $issues += "enforced target missing upstream remote/branch: $($enforcedWithoutUpstream.Id -join ', ')" -} -if ($enforcedWithoutRollout.Count) { - $issues += "enforced target missing rollout/migration/downtime contract: $($enforcedWithoutRollout.Id -join ', ')" -} -if ($deployScriptsWithoutIdunnGuard.Count) { - $issues += "enforced deploy script missing Idunn actuator guard: $($deployScriptsWithoutIdunnGuard.Id -join ', ')" -} -if ($guardedActuatorsWithoutCatalog.Count) { - $issues += "guarded deploy/restart actuator missing Idunn catalog entry: $($guardedActuatorsWithoutCatalog.Name -join ', ')" -} -if ($blockedWithoutReason.Count) { - $issues += "blocked/external target missing reason: $($blockedWithoutReason.Id -join ', ')" -} - -if ($issues.Count) { - throw "Idunn deployment target catalog is incoherent: $($issues -join '; ')" -} - -$enforced = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "enforced" }) -$runtimeEnforced = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "runtime-enforced" }) -$blocked = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "blocked" }) -$external = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "external-owned" }) -$archived = @($IdunnDeploymentTargets | Where-Object { $_.Status -eq "archived" }) -Write-Host "Idunn deployment catalog coherent: $($IdunnDeploymentTargets.Count) targets, $($enforced.Count) deploy-enforced with upstream rollout contracts, $($runtimeEnforced.Count) runtime-enforced, $($blocked.Count) blocked, $($external.Count) external-owned, $($archived.Count) archived, $($knownActuatorPaths.Count) cataloged actuator paths." diff --git a/scripts/idunn-deployment-targets.ps1 b/scripts/idunn-deployment-targets.ps1 deleted file mode 100644 index 86c7e7a..0000000 --- a/scripts/idunn-deployment-targets.ps1 +++ /dev/null @@ -1,289 +0,0 @@ -$repoRoot = Split-Path -Parent $PSScriptRoot -$projectsRoot = Split-Path -Parent $repoRoot - -$IdunnDeploymentSharedActuators = @( - "$repoRoot\scripts\deploy-yggdrasil-source-app.ps1" -) - -$IdunnDeploymentTargets = @( - [pscustomobject]@{ - Id = "odin" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "starfire" - Service = "odin-coordinator" - Status = "runtime-enforced" - Health = "daemon-published-rudp:odin.cultnet-rudp-provider-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-odin.ps1" - Aliases = @("$repoRoot\scripts\restart-odin.cmd") - Reason = "Central Idunn keeps Odin's all-seer/rendezvous daemon alive; Odin owns Verse discovery while Idunn owns lifecycle actuation." - }, - [pscustomobject]@{ - Id = "idunn" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "starfire" - Service = "idunn swarm supervisor" - Status = "runtime-enforced" - Health = $null - Deploy = $null - Restart = $null - Aliases = @() - Reason = "start-idunn-local.ps1 is the bootstrap for Idunn itself; it is not a deploy/restart actuator over another daemon." - }, - [pscustomobject]@{ - Id = "hermodr" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "starfire" - Service = "Hermodr browser lowering" - Status = "runtime-enforced" - Health = "daemon-published-rudp:hermodr.cultnet-rudp-browser-lowering-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-hermodr.ps1" - Aliases = @() - Reason = "Idunn keeps Hermodr's browser lowering alive; Hermodr publishes its own RUDP health while Odin remains the CultMesh discovery authority." - }, - [pscustomobject]@{ - Id = "stonks" - Repo = "Stonks" - LocalPath = "$projectsRoot\Stonks" - Host = "starfire" - Service = "stonks daemon" - Status = "runtime-enforced" - Health = "daemon-published-rudp:stonks.cultnet-rudp-market-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-stonks.cmd" - Aliases = @() - Reason = "Stonks publishes market/provider state and daemon health through CultMesh/CultNet; local restart is an Idunn actuator only." - }, - [pscustomobject]@{ - Id = "weksa" - Repo = "weksa" - LocalPath = "$projectsRoot\weksa" - Host = "starfire" - Service = "weksa daemon" - Status = "runtime-enforced" - Health = "daemon-published-rudp:weksa.cultnet-rudp-provider-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-weksa.cmd" - Aliases = @() - Reason = "Weksa provider state and health are daemon-owned CultMesh/CultNet records; local restart is an Idunn actuator only." - }, - [pscustomobject]@{ - Id = "starfire-muninn" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "starfire" - Service = "muninn serve --host starfire" - Status = "runtime-enforced" - Health = "daemon-published-rudp:muninn.cultnet-rudp-local-telemetry-and-quest-access" - Deploy = $null - Restart = "$repoRoot\scripts\restart-starfire-muninn.ps1" - Aliases = @("$repoRoot\scripts\restart-starfire-muninn.cmd") - Reason = "Central Idunn keeps Starfire-local Muninn alive for Quest access and local telemetry surfaces." - }, - [pscustomobject]@{ - Id = "raven-muninn" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "raven" - Service = "muninn serve --host raven" - Status = "runtime-enforced" - Health = "daemon-published-rudp:muninn.cultnet-rudp-remote-telemetry-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-muninn.ps1" - Aliases = @("$repoRoot\scripts\restart-muninn.cmd") - Reason = "Central Idunn keeps Raven Muninn's remote telemetry posture alive without activating A/V capture as part of keepalive." - }, - [pscustomobject]@{ - Id = "nightwing-muninn" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "nightwing" - Service = "muninn serve --host nightwing" - Status = "runtime-enforced" - Health = "daemon-published-rudp:muninn.cultnet-rudp-remote-telemetry-and-move-hid" - Deploy = $null - Restart = "$repoRoot\scripts\restart-nightwing-muninn.ps1" - Aliases = @("$repoRoot\scripts\restart-nightwing-muninn.cmd") - Reason = "Central Idunn reaches Nightwing over SSH and keeps the host-local Muninn Move HID daemon alive." - }, - [pscustomobject]@{ - Id = "vili" - Repo = "Vili" - LocalPath = "$projectsRoot\Vili" - Host = "raven" - Service = "GameCult\\Vili" - Status = "runtime-enforced" - Health = "daemon-published-rudp:vili.cultnet-rudp-animation-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-vili.cmd" - Aliases = @("$repoRoot\scripts\restart-vili.ps1") - Reason = "Central Idunn keeps Raven Vili's typed motion daemon alive; command requests and health are CultMesh/CultNet-owned." - }, - [pscustomobject]@{ - Id = "raven-sleipnir" - Repo = "Odin" - LocalPath = "$projectsRoot\Odin" - Host = "raven" - Service = "sleipnir --host raven" - Status = "runtime-enforced" - Health = "daemon-published-rudp:sleipnir.cultnet-rudp-input-mirror-health" - Deploy = "$repoRoot\scripts\deploy-raven-sleipnir.ps1" - Restart = "$repoRoot\scripts\restart-raven-sleipnir.ps1" - Aliases = @() - Reason = "Central Idunn reaches Raven over SSH and keeps Sleipnir's hidden scheduled task alive while Sleipnir publishes daemon-owned RUDP health." - }, - [pscustomobject]@{ - Id = "yggdrasil-voidbot" - Repo = "VoidBot" - LocalPath = "/srv/build/VoidBot" - Host = "yggdrasil" - Service = "voidbot.service" - Status = "enforced" - Health = "daemon-published-rudp:voidbot.cultnet-rudp-stack-health" - Deploy = "sudo -n /usr/local/libexec/idunn-yggdrasil deploy voidbot" - Restart = "sudo -n /usr/local/libexec/idunn-yggdrasil restart voidbot" - Aliases = @() - UpstreamRemote = "origin" - UpstreamBranch = "main" - RolloutStrategy = "restart-after-verified-build" - StateMigration = "daemon-owned-noop" - ZeroDowntime = "restart-required" - Reason = "Yggdrasil-local Idunn deploys upstream VoidBot releases through the narrow root-owned manifest; local checkouts have no deployment authority." - }, - [pscustomobject]@{ - Id = "yggdrasil-bifrost" - Repo = "Bifrost" - LocalPath = "$projectsRoot\Bifrost" - Host = "yggdrasil" - Service = "bifrost.service" - Status = "blocked" - Health = $null - Deploy = $null - Restart = $null - Aliases = @() - Reason = "Committed Bifrost HEAD currently expects UserAccounts.HeimdallAccountId, but Yggdrasil's production database lacks that column and EF reports no pending migration. Idunn must not enforce Bifrost freshness until the Bifrost schema migration path is fixed." - }, - [pscustomobject]@{ - Id = "yggdrasil-streampixels" - Repo = "StreamPixels" - LocalPath = "$projectsRoot\StreamPixels" - Host = "yggdrasil" - Service = "streampixels-service/streampixels-web" - Status = "enforced" - Health = "daemon-published-rudp:streampixels.cultnet-rudp-service-health" - Deploy = "$repoRoot\scripts\deploy-yggdrasil-streampixels.cmd" - Restart = $null - Aliases = @("$repoRoot\scripts\deploy-yggdrasil-streampixels.ps1") - UpstreamRemote = "origin" - UpstreamBranch = "main" - RolloutStrategy = "restart-after-verified-build" - StateMigration = "daemon-owned-noop" - ZeroDowntime = "restart-required" - Reason = "Idunn packages upstream origin/main for StreamPixels, runs the existing ops deploy/check scripts, and verifies the remote manifest." - }, - [pscustomobject]@{ - Id = "yggdrasil-heimdall" - Repo = "Heimdall" - LocalPath = "$projectsRoot\Heimdall" - Host = "yggdrasil" - Service = "heimdall.service" - Status = "enforced" - Health = "daemon-published-rudp:heimdall.cultnet-rudp-provider-health" - Deploy = "$repoRoot\scripts\deploy-yggdrasil-heimdall.cmd" - Restart = $null - Aliases = @("$repoRoot\scripts\deploy-yggdrasil-heimdall.ps1") - UpstreamRemote = "origin" - UpstreamBranch = "main" - RolloutStrategy = "restart-after-verified-build" - StateMigration = "daemon-owned-noop" - ZeroDowntime = "restart-required" - Reason = "Idunn packages upstream origin/main for Heimdall, runs the existing ops deploy/check scripts, and verifies the remote manifest." - }, - [pscustomobject]@{ - Id = "yggdrasil-repixelizer" - Repo = "repixelizer" - LocalPath = "$projectsRoot\repixelizer" - Host = "yggdrasil" - Service = "repixelizer-gui.service" - Status = "enforced" - Health = "daemon-published-rudp:repixelizer.cultnet-rudp-service-health" - Deploy = "$repoRoot\scripts\deploy-yggdrasil-repixelizer.cmd" - Restart = $null - Aliases = @("$repoRoot\scripts\deploy-yggdrasil-repixelizer.ps1") - UpstreamRemote = "origin" - UpstreamBranch = "main" - RolloutStrategy = "restart-after-verified-build" - StateMigration = "daemon-owned-noop" - ZeroDowntime = "restart-required" - Reason = "Idunn packages upstream origin/main for repixelizer, runs the existing ops deploy/check scripts, and verifies the remote manifest." - }, - [pscustomobject]@{ - Id = "github-pages-gamecult-site" - Repo = "gamecult-site" - LocalPath = "$projectsRoot\gamecult-site" - Host = "github-pages" - Service = "gamecult.org/www" - Status = "external-owned" - Health = $null - Deploy = $null - Restart = $null - Aliases = @() - Reason = "GitHub Actions/Pages owns deployment freshness; Idunn should ingest workflow/deployment status before claiming automatic deploy authority." - }, - [pscustomobject]@{ - Id = "eve-ipad-evecanvas" - Repo = "Eve" - LocalPath = "$projectsRoot\Eve" - Host = "eve-ipad" - Service = "org.gamecult.evecanvas" - Status = "blocked" - Health = $null - Deploy = $null - Restart = $null - Aliases = @() - Reason = "Jailbroken iPad native app deployment exists only as an operator runbook; no safe noninteractive Idunn deploy command yet." - }, - [pscustomobject]@{ - Id = "periwinkle-eve-android" - Repo = "Eve" - LocalPath = "$projectsRoot\Eve" - Host = "periwinkle" - Service = "Eve Android proof APK" - Status = "blocked" - Health = $null - Deploy = $null - Restart = $null - Aliases = @() - Reason = "Android USB install requires on-device approval; Idunn must not claim automatic deploy authority until that boundary is solved." - }, - [pscustomobject]@{ - Id = "nightwing-eve-dashboard" - Repo = "Mimir" - LocalPath = "$projectsRoot\Mimir" - Host = "nightwing" - Service = "nightwing-eve-dashboard.service" - Status = "archived" - Health = $null - Deploy = $null - Restart = $null - Aliases = @("$repoRoot\scripts\restart-nightwing-eve-dashboard.cmd") - Reason = "Nightwing Eve dashboard's old HTTP/WebSocket broker is archived. Rebuild it only as typed CultMesh/Odin state publication plus renderer lowering before restoring Idunn lifecycle authority." - }, - [pscustomobject]@{ - Id = "nightwing-eve-browser-reference" - Repo = "Eve" - LocalPath = "$projectsRoot\Eve" - Host = "nightwing" - Service = "nightwing-eve-browser-reference.service" - Status = "runtime-enforced" - Health = "daemon-published-rudp:nightwing.cultnet-rudp-browser-reference-health" - Deploy = $null - Restart = "$repoRoot\scripts\restart-nightwing-eve-browser-reference.ps1" - Aliases = @("$repoRoot\scripts\restart-nightwing-eve-browser-reference.cmd") - Reason = "Nightwing Eve browser reference publishes daemon-owned boundary witnesses and RUDP health; systemd restart is an Idunn actuator only." - } -) diff --git a/scripts/install-idunn-startup.ps1 b/scripts/install-idunn-startup.ps1 deleted file mode 100644 index 4dd5d85..0000000 --- a/scripts/install-idunn-startup.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -param( - [string] $TaskName = "Idunn Local Keepalive", - [int] $IntervalSeconds = 30 -) - -$ErrorActionPreference = "Stop" - -$repoRoot = Split-Path -Parent $PSScriptRoot -$scriptPath = Join-Path $repoRoot "scripts\start-idunn-local.ps1" -$powershell = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe" -$taskCommand = "`"$powershell`" -NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$scriptPath`" -IntervalSeconds $IntervalSeconds" - -& schtasks.exe /Create /TN $TaskName /SC ONLOGON /TR $taskCommand /F | Out-Null -if ($LASTEXITCODE -ne 0) { - $startupDir = [Environment]::GetFolderPath("Startup") - $startupScript = Join-Path $startupDir "$TaskName.cmd" - $cmd = "@echo off`r`ncd /d `"$repoRoot`"`r`n$taskCommand`r`n" - [System.IO.File]::WriteAllText($startupScript, $cmd, [System.Text.UTF8Encoding]::new($false)) - Write-Host "Scheduled task install was denied; installed Startup folder launcher instead:" - Write-Host " $startupScript" -} else { - Write-Host "Installed scheduled task: $TaskName" -} - -Write-Host "Starting Idunn watchdogs now..." -& $scriptPath -IntervalSeconds $IntervalSeconds diff --git a/scripts/linux/idunn-yggdrasil b/scripts/linux/idunn-yggdrasil deleted file mode 100644 index 1d83b52..0000000 --- a/scripts/linux/idunn-yggdrasil +++ /dev/null @@ -1,383 +0,0 @@ -#!/bin/sh -set -eu - -PATH=/usr/sbin:/usr/bin:/sbin:/bin -export PATH -LC_ALL=C -export LC_ALL - -if [ "$(id -u)" -ne 0 ]; then - echo "idunn-yggdrasil must run through its narrow sudo boundary" >&2 - exit 1 -fi - -action="${1:-}" -target="${2:-}" -compose=/srv/compose/yggdrasil-apps.yaml -brake_store=/var/lib/gamecult/idunn-authority/deployment-brake.cc -brake_lock=/var/lib/gamecult/idunn-authority/deployment-brake.cc.lock -brake_anchor=/etc/gamecult/idunn/deployment-brake-operator-anchor.cc -generation_lock=/var/lib/gamecult/idunn-authority/idunn-generation.lock -idunn_provision=/usr/local/bin/idunn-provision - -require_root_owned_nonwritable_component() { - component="$1" - kind="$2" - if [ -L "$component" ]; then - echo "privileged actuator path component must not be a symlink: $component" >&2 - exit 1 - fi - case "$kind" in - file) [ -f "$component" ] || { echo "privileged actuator path component is not a regular file: $component" >&2; exit 1; } ;; - directory) [ -d "$component" ] || { echo "privileged actuator path component is not a directory: $component" >&2; exit 1; } ;; - *) echo "internal privileged actuator path-kind error" >&2; exit 1 ;; - esac - owner="$(stat -c '%u' -- "$component")" - mode="$(stat -c '%a' -- "$component")" - [ "$owner" = 0 ] || { echo "privileged actuator path component is not root-owned: $component" >&2; exit 1; } - case "$mode" in - ''|*[!0-7]*) echo "privileged actuator path component has an unreadable mode: $component" >&2; exit 1 ;; - esac - group_mode=$(( (mode / 10) % 10 )) - other_mode=$(( mode % 10 )) - if [ $((group_mode & 2)) -ne 0 ] || [ $((other_mode & 2)) -ne 0 ]; then - echo "privileged actuator path component is group/world-writable: $component" >&2 - exit 1 - fi -} - -require_root_owned_regular_path() { - path="$1" - case "$path" in - /*) ;; - *) echo "privileged actuator path must be absolute: $path" >&2; exit 1 ;; - esac - ancestor="$(dirname -- "$path")" - while :; do - require_root_owned_nonwritable_component "$ancestor" directory - [ "$ancestor" = / ] && break - ancestor="$(dirname -- "$ancestor")" - done - require_root_owned_nonwritable_component "$path" file -} - -require_root_owned_resolved_executable() { - entrypoint="$1" - ancestor="$(dirname -- "$entrypoint")" - while :; do - require_root_owned_nonwritable_component "$ancestor" directory - [ "$ancestor" = / ] && break - ancestor="$(dirname -- "$ancestor")" - done - [ -e "$entrypoint" ] || { echo "privileged executable entrypoint is missing: $entrypoint" >&2; exit 1; } - [ "$(stat -c '%u' -- "$entrypoint")" = 0 ] || { echo "privileged executable entrypoint is not root-owned: $entrypoint" >&2; exit 1; } - resolved="$(readlink -f -- "$entrypoint")" - [ -n "$resolved" ] || { echo "privileged executable entrypoint cannot be resolved: $entrypoint" >&2; exit 1; } - require_root_owned_regular_path "$resolved" - [ -x "$resolved" ] || { echo "privileged executable is not executable: $resolved" >&2; exit 1; } -} - -acquire_idunn_generation_lock() { - # Every target consequence observes one installed Idunn generation. The - # self-bootstrap owns the exclusive side of this lock while replacing and - # accepting that generation; target manifests hold the shared side from - # admission through completion. This lock does not decide brake posture, - # target lifecycle, or release authority. - require_root_owned_regular_path "$generation_lock" - exec 8<"$generation_lock" - /usr/bin/flock --shared 8 -} - -deployment_target_policy() { - manifest_takes_action=false - minimum_source_revision_required=false - case "$target" in - odin) - daemon_id=yggdrasil-odin - repository_full_name=GameCult/Odin - upstream_ref=refs/heads/codex/ygg-idunn-independent-bootstrap - requires_bifrost_authority=false - manifest_takes_action=true - ;; - codex-connector) - daemon_id=yggdrasil-codex-connector - repository_full_name=GameCult/CodexConnector - upstream_ref=refs/heads/codex/ghostlight-release-binding - requires_bifrost_authority=false - minimum_source_revision_required=true - manifest_takes_action=true - ;; - ghostlight) - daemon_id=yggdrasil-ghostlight - repository_full_name=GameCult/Ghostlight - upstream_ref=refs/heads/codex/ghostlight-dungeon-mvp - requires_bifrost_authority=false - minimum_source_revision_required=true - manifest_takes_action=true - ;; - voidbot) - daemon_id=yggdrasil-voidbot - repository_full_name=GameCult/VoidBot - upstream_ref=refs/heads/main - requires_bifrost_authority=false - manifest_takes_action=true - ;; - heimdall) - daemon_id=yggdrasil-heimdall - repository_full_name=GameCult/Heimdall - upstream_ref=refs/heads/main - requires_bifrost_authority=false - ;; - epiphany-capstone-17) - daemon_id=yggdrasil-epiphany-capstone-17 - repository_full_name=GameCult/Epiphany - upstream_ref=refs/heads/codex/epiphany-shakedown-live - requires_bifrost_authority=false - manifest_takes_action=true - ;; - epiphany) - daemon_id=yggdrasil-epiphany - repository_full_name=GameCult/Epiphany - upstream_ref=refs/heads/codex/epiphany-shakedown-live - requires_bifrost_authority=false - manifest_takes_action=true - ;; - bifrost-persona-feedback) - daemon_id=yggdrasil-bifrost-persona-feedback - repository_full_name=GameCult/Bifrost - upstream_ref=refs/heads/main - requires_bifrost_authority=true - ;; - repixelizer) - daemon_id=yggdrasil-repixelizer - repository_full_name=GameCult/repixelizer - upstream_ref=refs/heads/main - requires_bifrost_authority=false - ;; - streampixels) - daemon_id=yggdrasil-streampixels - repository_full_name=GameCult/StreamPixels - upstream_ref=refs/heads/main - requires_bifrost_authority=false - ;; - gjallar) - daemon_id=yggdrasil-gjallar - repository_full_name=GameCult/Gjallar - upstream_ref=refs/heads/codex/yggdrasil-aggregate-daemon - requires_bifrost_authority=false - ;; - *) echo "deployment target has no root actuator policy: $target" >&2; exit 1 ;; - esac -} - -require_signed_deployment_grant() { - source_commit="$3" - deployment_request_id="$4" - release_authority_id="$5" - release_authority_sha256="$6" - - deployment_target_policy - case "$source_commit" in - *[!0-9a-f]*|'') echo "invalid or missing source commit" >&2; exit 1 ;; - esac - [ "${#source_commit}" -eq 40 ] || { echo "source commit must be 40 lowercase hex characters" >&2; exit 1; } - case "$deployment_request_id" in - *[!A-Za-z0-9._:/+-]*|'') echo "invalid or missing Idunn deployment request id" >&2; exit 1 ;; - esac - [ "${#deployment_request_id}" -le 256 ] || { echo "Idunn deployment request id is oversized" >&2; exit 1; } - case "$deployment_request_id" in - "deploy:$daemon_id:"?*|"manual:redeploy:$daemon_id:"?*) ;; - *) echo "signed deployment request is not for root target $target" >&2; exit 1 ;; - esac - - case "$requires_bifrost_authority" in - true) - [ -n "$release_authority_id" ] || { echo "missing Bifrost release authority id" >&2; exit 1; } - case "$release_authority_sha256" in - *[!0-9a-f]*|'') echo "invalid or missing Bifrost release authority digest" >&2; exit 1 ;; - esac - [ "${#release_authority_sha256}" -eq 64 ] || { echo "Bifrost release authority digest must be 64 lowercase hex characters" >&2; exit 1; } - ;; - false) - [ -z "$release_authority_id" ] || { echo "source-observed release cannot claim Bifrost authority id" >&2; exit 1; } - [ -z "$release_authority_sha256" ] || { echo "source-observed release cannot claim Bifrost authority digest" >&2; exit 1; } - ;; - esac - - require_root_owned_resolved_executable "$idunn_provision" - require_root_owned_regular_path "$brake_store" - require_root_owned_regular_path "$brake_lock" - require_root_owned_regular_path "$brake_anchor" - - # This validation is the deployment-admission point. The signed expiry is a - # start-before deadline: it denies a later transaction, but does not become a - # timer-shaped veto over one already admitted. The root actuator remains the - # sole shared-lock holder through the synchronous consequence, so an explicit - # engage/release transition cannot race or rewrite that transaction. - exec 9<"$brake_lock" - /usr/bin/flock --shared 9 - now_unix_millis="$(/usr/bin/date +%s%3N)" - case "$now_unix_millis" in - ''|*[!0-9]*) echo "root actuator clock did not produce Unix milliseconds" >&2; exit 1 ;; - esac - "$idunn_provision" deployment-brake-status \ - --store "$brake_store" \ - --operator-anchor "$brake_anchor" \ - --runtime-id yggdrasil \ - --release-id "$source_commit" \ - --deployment-id "$deployment_request_id" \ - --now-unix-millis "$now_unix_millis" - - if [ "$minimum_source_revision_required" = true ]; then - require_root_owned_resolved_executable /usr/local/bin/idunn - /usr/local/bin/idunn validate-minimum-source-revision \ - --daemon "$daemon_id" \ - --source-revision "$source_commit" - fi - - if [ "$requires_bifrost_authority" = true ]; then - require_root_owned_resolved_executable /usr/local/bin/idunn - /usr/local/bin/idunn validate-release-authority \ - --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc \ - --release-authority-store /srv/bifrost/state/repository-release-authority.cc \ - --repository-full-name "$repository_full_name" \ - --upstream-ref "$upstream_ref" \ - --source-revision "$source_commit" \ - --release-authority-id "$release_authority_id" \ - --release-authority-sha256 "$release_authority_sha256" - fi -} - -manifest_group_exists() { - kill -s 0 "-$manifest_pid" 2>/dev/null -} - -signal_manifest_group() { - kill -s "$1" "-$manifest_pid" 2>/dev/null || true -} - -forward_manifest_signal() { - manifest_wait_interrupted=true - signal_manifest_group "$1" -} - -run_manifest_while_holding_authority_locks() { - # Stay alive as the sole lock owner. The child must not inherit descriptors - # 8 or 9: flock locks attach to shared open-file descriptions, so an - # inherited descriptor could retain or unlock the parent's generation or - # deployment transaction. The owned process group also prevents ordinary - # descendants from outliving interruption. - require_root_owned_resolved_executable /usr/bin/setsid - /usr/bin/setsid "$@" 8<&- 9<&- & - manifest_pid=$! - manifest_wait_interrupted=false - trap 'forward_manifest_signal HUP' HUP - trap 'forward_manifest_signal INT' INT - trap 'forward_manifest_signal TERM' TERM - while :; do - manifest_wait_interrupted=false - if wait "$manifest_pid"; then - manifest_status=0 - else - manifest_status=$? - fi - if [ "$manifest_wait_interrupted" = true ] && manifest_group_exists; then - continue - fi - break - done - trap - HUP INT TERM - - manifest_descendants_leaked=false - if manifest_group_exists; then - manifest_descendants_leaked=true - signal_manifest_group TERM - for _ in 1 2 3 4 5; do - manifest_group_exists || break - sleep 1 - done - fi - if manifest_group_exists; then - signal_manifest_group KILL - for _ in 1 2 3 4 5; do - manifest_group_exists || break - sleep 1 - done - fi - if manifest_group_exists; then - echo "deployment manifest process group survived SIGKILL" >&2 - return 1 - fi - if [ "$manifest_descendants_leaked" = true ] && [ "$manifest_status" -eq 0 ]; then - echo "deployment manifest left a descendant process behind" >&2 - return 1 - fi - return "$manifest_status" -} - -case "$action" in - deploy) - [ "$#" -eq 6 ] || { echo "deploy requires exact target, source, request, and release-authority arguments" >&2; exit 2; } - ;; - restart) - [ "$#" -eq 2 ] || { echo "restart accepts only one exact target" >&2; exit 2; } - ;; - *) - echo "unsupported Idunn Yggdrasil actuator action: $action" >&2 - exit 2 - ;; -esac - -acquire_idunn_generation_lock - -case "$action:$target" in - restart:heimdall) - require_root_owned_regular_path "$compose" - exec docker compose -f "$compose" restart heimdall - ;; - restart:repixelizer) - require_root_owned_regular_path "$compose" - exec docker compose -f "$compose" restart repixelizer - ;; - restart:streampixels) - require_root_owned_regular_path "$compose" - docker compose -f "$compose" restart streampixels-service streampixels-web - ;; - restart:gjallar) - exec systemctl restart gjallar-yggdrasil.service - ;; - restart:voidbot|restart:epiphany|restart:odin|restart:codex-connector|restart:ghostlight) - manifest="/srv/odin/deploy-manifests/$target" - require_root_owned_regular_path "$manifest" - [ -x "$manifest" ] || { echo "root actuator manifest is not executable: $manifest" >&2; exit 1; } - export IDUNN_ACTUATOR=1 - export IDUNN_COMMAND_AUTHORITY=idunn-daemon - run_manifest_while_holding_authority_locks "$manifest" restart - exit $? - ;; - deploy:voidbot|deploy:epiphany|deploy:epiphany-capstone-17|deploy:odin|deploy:codex-connector|deploy:ghostlight|deploy:heimdall|deploy:repixelizer|deploy:streampixels|deploy:bifrost-persona-feedback|deploy:gjallar) - manifest="/srv/odin/deploy-manifests/$target" - require_root_owned_regular_path "$manifest" - [ -x "$manifest" ] || { echo "root actuator manifest is not executable: $manifest" >&2; exit 1; } - require_signed_deployment_grant "$@" - export IDUNN_ACTUATOR=1 - export IDUNN_COMMAND_AUTHORITY=idunn-daemon - export IDUNN_SOURCE_COMMIT="$source_commit" - export IDUNN_REPOSITORY_FULL_NAME="$repository_full_name" - export IDUNN_UPSTREAM_REF="$upstream_ref" - export BIFROST_RELEASE_AUTHORITY_ID="$release_authority_id" - export BIFROST_RELEASE_AUTHORITY_SHA256="$release_authority_sha256" - export IDUNN_DEPLOYMENT_REQUEST_ID="$deployment_request_id" - export IDUNN_REQUIRES_BIFROST_AUTHORITY="$requires_bifrost_authority" - if [ "$manifest_takes_action" = true ]; then - run_manifest_while_holding_authority_locks "$manifest" "$action" - exit $? - fi - run_manifest_while_holding_authority_locks "$manifest" - exit $? - ;; - *) - echo "unsupported Idunn Yggdrasil actuator request: $action $target" >&2 - exit 2 - ;; -esac diff --git a/scripts/linux/idunn-yggdrasil.service b/scripts/linux/idunn-yggdrasil.service deleted file mode 100644 index 0597806..0000000 --- a/scripts/linux/idunn-yggdrasil.service +++ /dev/null @@ -1,26 +0,0 @@ -[Unit] -Description=Idunn Yggdrasil-local lifecycle coordinator -After=network-online.target docker.service -Wants=network-online.target -Requires=docker.service - -[Service] -Type=simple -User=idunn -Group=idunn -# Operational cancellation ceiling only. Target daemons own readiness through -# typed health; this duration must never be interpreted as deployment success. -ExecStart=/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /var/lib/gamecult/idunn --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --release-authority-store /srv/bifrost/state/repository-release-authority.cc --deployment-brake-store /var/lib/gamecult/idunn-authority/deployment-brake.cc --deployment-brake-operator-anchor /etc/gamecult/idunn/deployment-brake-operator-anchor.cc --daemon-health-trust-store /etc/gamecult/idunn/daemon-health-trust.cc --rudp-health-bind 10.77.0.1:17870 --service-identity-store /var/lib/gamecult/idunn/idunn-service-identity.cc --public-health-store /var/lib/gamecult/idunn/authenticated-provider-health.cc --public-health-query-bind 127.0.0.1:17871 --command-timeout-seconds 21600 --execute -Restart=always -RestartSec=5 -TimeoutStopSec=20 -KillSignal=SIGINT -UMask=027 -PrivateTmp=yes -ProtectHome=yes -ProtectSystem=full -ReadWritePaths=/etc/gamecult/idunn /etc/gamecult/codex-connector /etc/gamecult/ghostlight-dungeon /etc/systemd/system/codex-connector.service /etc/systemd/system/epiphany-swarm.service /etc/systemd/system/ghostlight-dungeon.service /var/lib/gamecult/idunn /srv/build /srv/voidbot -ReadOnlyPaths=/var/lib/gamecult/idunn-authority - -[Install] -WantedBy=multi-user.target diff --git a/scripts/linux/idunn-yggdrasil.sudoers b/scripts/linux/idunn-yggdrasil.sudoers deleted file mode 100644 index dad6e44..0000000 --- a/scripts/linux/idunn-yggdrasil.sudoers +++ /dev/null @@ -1,24 +0,0 @@ -Cmnd_Alias IDUNN_YGGDRASIL_DEPLOY = \ - /usr/local/libexec/idunn-yggdrasil deploy odin *, \ - /usr/local/libexec/idunn-yggdrasil deploy codex-connector *, \ - /usr/local/libexec/idunn-yggdrasil deploy ghostlight *, \ - /usr/local/libexec/idunn-yggdrasil deploy voidbot *, \ - /usr/local/libexec/idunn-yggdrasil deploy heimdall *, \ - /usr/local/libexec/idunn-yggdrasil deploy epiphany-capstone-17 *, \ - /usr/local/libexec/idunn-yggdrasil deploy epiphany *, \ - /usr/local/libexec/idunn-yggdrasil deploy bifrost-persona-feedback *, \ - /usr/local/libexec/idunn-yggdrasil deploy repixelizer *, \ - /usr/local/libexec/idunn-yggdrasil deploy streampixels *, \ - /usr/local/libexec/idunn-yggdrasil deploy gjallar * -Cmnd_Alias IDUNN_YGGDRASIL_RESTART = \ - /usr/local/libexec/idunn-yggdrasil restart odin, \ - /usr/local/libexec/idunn-yggdrasil restart codex-connector, \ - /usr/local/libexec/idunn-yggdrasil restart ghostlight, \ - /usr/local/libexec/idunn-yggdrasil restart voidbot, \ - /usr/local/libexec/idunn-yggdrasil restart heimdall, \ - /usr/local/libexec/idunn-yggdrasil restart epiphany, \ - /usr/local/libexec/idunn-yggdrasil restart repixelizer, \ - /usr/local/libexec/idunn-yggdrasil restart streampixels, \ - /usr/local/libexec/idunn-yggdrasil restart gjallar - -idunn ALL=(root) NOPASSWD: IDUNN_YGGDRASIL_DEPLOY, IDUNN_YGGDRASIL_RESTART diff --git a/scripts/notify-idunn-operator-alarm.cmd b/scripts/notify-idunn-operator-alarm.cmd deleted file mode 100644 index 7955f4f..0000000 --- a/scripts/notify-idunn-operator-alarm.cmd +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0notify-idunn-operator-alarm.ps1" diff --git a/scripts/notify-idunn-operator-alarm.ps1 b/scripts/notify-idunn-operator-alarm.ps1 deleted file mode 100644 index e8f6a97..0000000 --- a/scripts/notify-idunn-operator-alarm.ps1 +++ /dev/null @@ -1,25 +0,0 @@ -Set-StrictMode -Version Latest -$ErrorActionPreference = "Stop" - -$repoRoot = Split-Path -Parent $PSScriptRoot -$projectsRoot = Split-Path -Parent $repoRoot -$bifrostRoot = Join-Path $projectsRoot "Bifrost" -$publisherScript = Join-Path $bifrostRoot "tools\operator-notification.mjs" - -if (-not (Test-Path -LiteralPath $publisherScript)) { - throw "Bifrost operator-notification publisher is missing at $publisherScript." -} - -Push-Location -LiteralPath $bifrostRoot -try { - $arguments = @($publisherScript, "publish-idunn-alarm") - if ($env:IDUNN_OPERATOR_ALARM_DRY_RUN -in @("1", "true", "yes", "on")) { - $arguments += "--dry-run" - } - & node @arguments - if ($LASTEXITCODE -ne 0) { - throw "Bifrost operator-notification publisher exited with code $LASTEXITCODE." - } -} finally { - Pop-Location -} diff --git a/scripts/show-idunn-deployment-targets.ps1 b/scripts/show-idunn-deployment-targets.ps1 deleted file mode 100644 index dce3eb6..0000000 --- a/scripts/show-idunn-deployment-targets.ps1 +++ /dev/null @@ -1,8 +0,0 @@ -$ErrorActionPreference = "Stop" - -. (Join-Path $PSScriptRoot "idunn-deployment-targets.ps1") - -$IdunnDeploymentTargets | - Sort-Object Status, Id | - Select-Object Id, Repo, Host, Service, Status, Reason | - Format-Table -AutoSize -Wrap diff --git a/scripts/start-idunn-local.ps1 b/scripts/start-idunn-local.ps1 deleted file mode 100644 index acb34b9..0000000 --- a/scripts/start-idunn-local.ps1 +++ /dev/null @@ -1,179 +0,0 @@ -param( - [int] $StaleAfterSeconds = 120, - [string] $StateDir = (Join-Path (Split-Path -Parent $PSScriptRoot) "scratch\idunn") -) - -$ErrorActionPreference = "Stop" - -$repoRoot = Split-Path -Parent $PSScriptRoot -$idunnExe = Join-Path $repoRoot "target\debug\idunn.exe" -$operatorAlarmCommand = Join-Path $repoRoot "scripts\notify-idunn-operator-alarm.cmd" -$pidPath = Join-Path $StateDir "idunn.pid" -$storePath = Join-Path $StateDir "idunn.keepalive.cc" -$outLog = Join-Path $StateDir "idunn.out.log" -$errLog = Join-Path $StateDir "idunn.err.log" - -New-Item -ItemType Directory -Force -Path $StateDir | Out-Null - -function Get-IdunnStartupDetail { - $detail = "" - if (Test-Path -LiteralPath $outLog) { $detail += Get-Content -Raw -LiteralPath $outLog } - if (Test-Path -LiteralPath $errLog) { $detail += Get-Content -Raw -LiteralPath $errLog } - return $detail -} - -function Move-CorruptIdunnStoreAside { - param( - [string] $StorePath - ) - - if (-not (Test-Path -LiteralPath $StorePath)) { - return $null - } - - $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" - $corruptPath = Join-Path $StateDir "idunn.keepalive.corrupt-$timestamp.cc" - Move-Item -LiteralPath $StorePath -Destination $corruptPath -Force - Remove-Item -LiteralPath "$StorePath.lock" -Force -ErrorAction SilentlyContinue - Write-Host "Archived corrupt Idunn store to $corruptPath" - return $corruptPath -} - -function Start-IdunnProcess { - param( - [string] $IdunnExe, - [object[]] $Arguments, - [string] $RepoRoot, - [string] $OutLog, - [string] $ErrLog, - [string] $PidPath - ) - - $process = Start-Process -FilePath $IdunnExe ` - -ArgumentList $Arguments ` - -WorkingDirectory $RepoRoot ` - -WindowStyle Hidden ` - -PassThru ` - -RedirectStandardOutput $OutLog ` - -RedirectStandardError $ErrLog - $process.Id | Set-Content -Encoding ASCII -LiteralPath $PidPath - Start-Sleep -Seconds 2 - return $process -} - -if (-not (Test-Path -LiteralPath $idunnExe)) { - Push-Location -LiteralPath $repoRoot - try { - & cargo build -p idunn-daemon - if ($LASTEXITCODE -ne 0) { - throw "cargo build -p idunn-daemon failed with exit code $LASTEXITCODE" - } - } finally { - Pop-Location - } -} - -function Test-IdunnSupervisorHealthy { - param( - [string] $PidPath, - [string] $StorePath, - [int] $StaleAfterSeconds - ) - - if (-not (Test-Path -LiteralPath $PidPath)) { - return $false - } - - $pidText = (Get-Content -LiteralPath $PidPath -Raw).Trim() - if ($pidText -notmatch "^\d+$") { - Remove-Item -LiteralPath $PidPath -Force -ErrorAction SilentlyContinue - return $false - } - - $process = Get-Process -Id ([int] $pidText) -ErrorAction SilentlyContinue - if ($null -eq $process) { - Remove-Item -LiteralPath $PidPath -Force -ErrorAction SilentlyContinue - return $false - } - - if (Test-Path -LiteralPath $StorePath) { - $storeAgeSeconds = ([DateTime]::UtcNow - (Get-Item -LiteralPath $StorePath).LastWriteTimeUtc).TotalSeconds - if ($storeAgeSeconds -le $StaleAfterSeconds) { - return $true - } - - Write-Host "Idunn swarm store is stale ($([math]::Round($storeAgeSeconds))s old). Restarting supervisor." - } else { - $processAgeSeconds = ([DateTime]::UtcNow - $process.StartTime.ToUniversalTime()).TotalSeconds - if ($processAgeSeconds -le $StaleAfterSeconds) { - return $true - } - - Write-Host "Idunn supervisor never wrote $StorePath within $StaleAfterSeconds seconds. Restarting supervisor." - } - - Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue - Remove-Item -LiteralPath $PidPath -Force -ErrorAction SilentlyContinue - return $false -} - -if (Test-IdunnSupervisorHealthy -PidPath $pidPath -StorePath $storePath -StaleAfterSeconds $StaleAfterSeconds) { - Write-Host "Idunn swarm supervisor already running." - return -} - -$arguments = @( - "--swarm-profile", "starfire-local", - "--repo-root", $repoRoot, - "--store", $storePath, - "--rudp-health-bind", "0.0.0.0:17870", - "--command-timeout-seconds", "600", - "--execute" -) - -if (Test-Path -LiteralPath $operatorAlarmCommand) { - $arguments += @("--operator-alarm-command", $operatorAlarmCommand) -} - -$process = Start-IdunnProcess ` - -IdunnExe $idunnExe ` - -Arguments $arguments ` - -RepoRoot $repoRoot ` - -OutLog $outLog ` - -ErrLog $errLog ` - -PidPath $pidPath - -if ($process.HasExited) { - $detail = Get-IdunnStartupDetail - if ($detail -like "*failed to decode MessagePack*" -and (Test-Path -LiteralPath $storePath)) { - Move-CorruptIdunnStoreAside -StorePath $storePath | Out-Null - $process = Start-IdunnProcess ` - -IdunnExe $idunnExe ` - -Arguments $arguments ` - -RepoRoot $repoRoot ` - -OutLog $outLog ` - -ErrLog $errLog ` - -PidPath $pidPath - } -} - -if ($process.HasExited) { - $detail = Get-IdunnStartupDetail - throw "Idunn swarm supervisor exited immediately with code $($process.ExitCode).`n$detail" -} - -Write-Host "Started Idunn swarm supervisor as PID $($process.Id)." -Write-Host " store: $storePath" - -function ConvertTo-WindowsArgument { - param([string] $Value) - if ($Value -notmatch '[\s"]') { - return $Value - } - return '"' + ($Value -replace '(\\*)"', '$1$1\"' -replace '(\\+)$', '$1$1') + '"' -} - -function Join-WindowsArguments { - param([object[]] $Arguments) - return (($Arguments | ForEach-Object { ConvertTo-WindowsArgument -Value ([string] $_) }) -join " ") -} diff --git a/src/Idunn/README.md b/src/Idunn/README.md deleted file mode 100644 index 5f09e8e..0000000 --- a/src/Idunn/README.md +++ /dev/null @@ -1,319 +0,0 @@ -# Idunn - -Idunn is the keepalive daemon for the Odin swarm. - -In plain language: Idunn is the little service whose job is to know which -GameCult daemons are supposed to be alive, bring them back after a reboot or -crash, watch whether they are still healthy, and raise a clear alarm when a -human needs to intervene. - -It is named for Idunn, the keeper of the apples that keep the gods young. The -job is not glamorous. That is the point. Good infrastructure should make -important things boring. - -## What Idunn Is For - -GameCult has many daemons: Odin, Bifrost, VoidBot, Mimir, Heimdall, Eve -providers, service workers, renderers, bridges, and local machine helpers. Each -one owns its own work, but each one should not have to carry its own private -lifecycle machinery. - -Idunn centralizes that lifecycle work: - -- start known daemons after machine boot; -- ensure deployed daemon artifacts match the desired target revision; -- restart daemons after crashes; -- watch health and freshness signals; -- avoid restarting services when authority is unclear; -- record every deployment/restart request and result as typed state; -- record release targets, deployment artifacts, state migrations, and rollout - results as typed state; -- escalate to an operator through Bifrost when human action is needed. - -The desired shape is simple: - -```text -Odin knows what exists. -Idunn keeps it alive. -Bifrost carries operator/public crossings. -Each daemon owns its own work and health publication. -``` - -## Who It Helps - -Idunn is for anyone running a local GameCult swarm or operating a hosted -GameCult machine. - -For a developer, Idunn should mean fewer mystery services to restart by hand. -For an operator, Idunn should mean health problems become visible, witnessed, -and routed to the right place. For daemon authors, Idunn should mean they can -publish health, transport profiles, and command boundaries over CultNet RUDP -instead of building one more private watchdog. - -## Current State - -Idunn is a Rust daemon inside Odin's Cargo workspace. Starfire and Yggdrasil -run separate host-local swarm profiles. Starfire owns its local adjunct daemons -and remote display/telemetry lanes. Yggdrasil owns its hosted release targets, -including Odin and VoidBot. Idunn starts independently of every managed target, -so an Odin outage cannot remove the organ responsible for recovering Odin. -Each target declares a daemon-owned health contract and keeps -its own interval and deploy/restart authority. Daemon truth comes from typed -CultNet/RUDP publication and daemon-owned boundary stores. Shell scripts remain -deployment, restart, and manual diagnostic lowerings only; they do not satisfy -health. -The scheduler and continuity witness now belong to one Rust process instead of -a PowerShell-herded pile of one-daemon workers. - -VoidBot deployment authority is deliberately absent from `starfire-local`. -Pushing VoidBot's upstream `main` branch is the release input; Yggdrasil-local -Idunn observes `/srv/build/VoidBot` and invokes only -`sudo -n /usr/local/libexec/idunn-yggdrasil deploy voidbot`. The privileged -actuator delegates deploy and restart to the root-owned executable -`/srv/odin/deploy-manifests/voidbot`. A developer checkout is not a deployment -target and agents must not create a local keepalive or deploy path for VoidBot. -Each cycle fetches `origin/main` in `/srv/build/VoidBot` and compares it with -`DEPLOYED_REVISION` in `/srv/voidbot/deploy/deployment.env`. Drift owns the -deployment decision. An unavailable fetch or witness raises an operator alarm -instead of guessing. The privileged manifest must atomically update that -witness only after the new release is verified; Idunn rejects an otherwise -successful command when the witness has not converged. - -```text -scratch/idunn/idunn.keepalive.cc -``` - -## Run It - -From `E:\Projects\Odin`: - -```powershell -npm run idunn:build -npm run idunn:start -- --daemon demo --rudp-health-bind -``` - -`--rudp-health-bind` is explicit. Bare one-daemon Idunn does not open a -localhost health ingress. - -Install the local Starfire boot watchdog: - -```powershell -.\scripts\install-idunn-startup.ps1 -``` - -That task starts one `idunn.exe` swarm supervisor at user logon. The Rust -runtime owns the target catalog for Odin, Stonks, Muninn, the enforced -Yggdrasil source artifact lanes, and the Nightwing display services. The -current Mimir dashboard is observed but not restarted until its restart -authority is named. - -It also starts `idunn-swarm-deployment-coverage`, which watches the deployment -target catalog in `scripts/idunn-deployment-targets.ps1`. A repo/service is not -allowed to vanish into shrug-space: it is either enforced by Idunn, explicitly -blocked with the missing authority named, external-owned, or not a runtime. - -To record that a daemon has not published health, run a manual target without a -matching `idunn.daemon_health` RUDP publication: - -```powershell -npm run idunn:start -- --daemon demo --restart-command "echo restart demo" -``` - -Idunn writes a non-actuating missing-publication health record and plans from -that typed absence. It does not run a local health command to decide daemon -truth. - -To actually allow restart/deploy actuation after typed health planning decides -one is needed: - -```powershell -npm run idunn:start -- --daemon demo --deploy-command "echo deploy demo" --restart-command "echo restart demo" --execute -``` - -To keep watching on a resident interval: - -```powershell -npm run idunn:start -- --daemon demo --interval-seconds 30 -``` - -To run the built-in Starfire-local swarm profile directly: - -```powershell -npm run idunn:start -- --swarm-profile starfire-local --repo-root E:\Projects\Odin --execute -``` - -The Yggdrasil service runs the hosted profile directly: - -```sh -/usr/local/bin/idunn --swarm-profile yggdrasil-local --repo-root /srv/odin/source --store /var/lib/gamecult/idunn/yggdrasil.keepalive.cc --rudp-health-bind 10.77.0.1:17870 --execute -``` - -Optional store override: - -```powershell -npm run idunn:start -- --daemon demo --store E:\path\to\idunn.keepalive.cc -``` - -## What Daemons Should Publish - -Idunn should not guess private service truth. A daemon should publish: - -- its service id and Verse id; -- where its durable `.cc` state or witness lives; -- a health or freshness signal; -- the command boundary for deployment or artifact refresh, if one exists; -- the command boundary for restart or recovery, if one exists; -- what operator action is needed when automatic recovery is unsafe. - -If that information is missing, Idunn should fail closed and create an operator -alarm instead of improvising. - -For the repo swarm, deployment ownership begins with catalog coverage. Use: - -```powershell -.\scripts\show-idunn-deployment-targets.ps1 -.\scripts\health-idunn-swarm-deployment-coverage.cmd -``` - -The enforced targets are Nightwing Gjallar plus the Yggdrasil source artifact -lanes whose ops runbooks can produce and verify deployment manifests. Each -enforced target must declare upstream remote/branch, rollout strategy, state -migration authority, and zero-downtime capability. Idunn deploys the declared -declared upstream revision, not arbitrary local `HEAD`. Ghostlight's catalog -entry remains disabled until its Linux body and signed health identity are -admitted. Bifrost is blocked until -its production database migration path is coherent; mobile device installs -remain blocked at their physical approval boundaries; GitHub Pages remains -external-owned. - -## Typed Records - -- `idunn.desired_daemon.v1` -- `idunn.daemon_health.v1` -- `idunn.signed_health_admission.v1` -- `idunn.keepalive_decision.v1` -- `idunn.deployment_request.v2` -- `idunn.current_deployment_request.v1` -- `idunn.deployment_result.v1` -- `idunn.release_target.v1` -- `idunn.deployment_artifact.v1` -- `idunn.state_migration_plan.v1` -- `idunn.state_migration_result.v1` -- `idunn.rollout_plan.v1` -- `idunn.rollout_result.v1` -- `idunn.restart_request.v1` -- `idunn.restart_result.v1` -- `idunn.operator_alarm.v1` -- `idunn.daemon_surgery_plan.v1` -- `idunn.daemon_transport_profile.v1` -- `idunn.command_boundary.v1` -- `idunn.runtime_transport_check.v1` -- `idunn.rudp_health_ingress.v1` - -Idunn publishes one `idunn.daemon_surgery_plan.v1` record per swarm target when -the swarm starts. Those records make the CultNet/RUDP migration queue explicit: -owner, objective, current mechanism, intended authority, cut line, -steps, blockers, severity, and status. - -It also publishes one `idunn.daemon_transport_profile.v1` and one -`idunn.command_boundary.v1` per target. The desired daemon record links to -both. The transport profile names `cultnet.transport.rudp.v0` as the target and -marks daemon publication and daemon-owned witness stores as the health -substrate. The command boundary names restart, deploy, health, and alarm -authority separately; health authority belongs to daemon-published RUDP state. - -At startup Idunn also publishes `idunn.runtime_transport_check.v1`, currently a -loopback CultNet hello over `cultnet.transport.rudp.v0`. That proves Idunn's -Rust body can use the RUDP substrate before it asks the rest of the swarm to -walk through the same door. - -For deploy-enforced targets, startup also publishes `idunn.release_target.v1`, -`idunn.deployment_artifact.v1`, `idunn.state_migration_plan.v1`, and -`idunn.rollout_plan.v1`. During deployment, Idunn runs any declared -daemon-owned migration command before the deploy command; a failed migration -stops the rollout and raises an operator alarm. Zero downtime is recorded only -when the target declares a real in-place, blue/green, or rolling strategy; -otherwise Idunn says `restart-required` and verifies that path honestly. - -Deploy scripts are Idunn actuators. Agents should configure the target catalog, -release target, command boundary, migration command, and daemon publication -surface, then let Idunn run the rollout. Direct script execution without -`IDUNN_ACTUATOR=1` fails on purpose; the little hand reaching for the deploy -button has been removed from the machine. - -`scripts/start-idunn-local.ps1` launches the local swarm with -`--rudp-health-bind 0.0.0.0:17870` so host-local and explicitly configured peers -can publish to the configured Idunn health endpoint. It accepts raw -`idunn.daemon_health` document puts on the canonical CNR0 RUDP `schema` channel -and writes them into the keepalive store. That is daemon-owned health -publication, not restart/deploy authority. On Windows, UDP reset reports from -closed one-shot publishers are treated as nonfatal ingress noise so one accepted -health frame cannot kill the listener. - -Epiphany uses a separate signed runtime-health schema. Idunn joins that document -to the exact latest Bifrost-authorized deployment request and persists -`idunn.signed_health_admission.v1`. The privileged admission verifier reads only -that fresh, current, request-bound record; generic health remains observation. - -When a fresh daemon-published RUDP health record exists for a target, Idunn uses -that record for keepalive planning. The record must match the daemon id, health -contract, RUDP transport, and freshness window. If it is absent or stale, Idunn -publishes a missing-publication observation and raises/plans from that typed -absence instead of running a local health probe. - -The active transport cut is no longer "make the daemons speak RUDP at all." -Nightwing Gjallar now consumes Odin's accepted -`surface:gamecult.network.status` snapshot over CultNet/RUDP, so every active -Starfire-local daemon target now publishes daemon-owned RUDP health plus typed -boundary state. The remaining debt is deleting bridge-only lowerings and -xenos-boundary ingestion shims without letting them reclaim ownership. Odin -still owes removal of hashed-store/C# witness shims once direct interoperable -witness publication is ready. - -Muninn media uses a codec-owned prediction model, not a custom visual-delta -codec. The canonical LAN profile is -`muninn.rudp.low_latency_h264_lan.v1`: NVENC H.264 owns inter-frame prediction, -motion search, residuals, keyframe production, and the actual lossy visual -encoding; CultNet/RUDP owns bounded delivery of typed video access units, audio -packets, receiver feedback, retransmit pressure, expiry, and keyframe requests. -Late media is dropped when it misses the fixed latency budget. Optical flow, -foveation, ROI maps, or stochastic patch experiments may become future Muninn -profiles only after this hardware-codec RUDP path is measured and proven -insufficient. - -Raven Muninn task actions are also an explicit ops invariant: Task Scheduler -must execute `wscript.exe //B //Nologo` hidden launcher actions directly for -`GameCult-Muninn`, `GameCult-Muninn-Activate`, and -`GameCult-Muninn-VideoProof`, never raw `.cmd` wrappers, and the hidden VBS -layer must call `.ps1` launchers directly instead of `cmdPath` trampolines. -Those three live Raven tasks have been repaired and verified. The repair -actuator now uploads its Raven PowerShell body with `sftp` and runs a tiny -cleanup wrapper, so the hidden-task repair does not hit Windows command-line -limits before it can run. The long-running Muninn serve bodies on Raven, -Nightwing, and Starfire now also carry their own `--idunn-rudp-health`, -`--idunn-daemon`, and `--idunn-health-contract` arguments, and live Idunn -accepts those daemon-owned health records directly instead of relying on -one-shot health commands for the truth. - -From here on out, the work is not "teach another daemon to speak." It is -"delete the witness masks and keep ownership where it belongs." - -Rust no longer gets to claim the transport is imaginary: -CultLib's `cultnet-rs` carries the canonical CNR0 `cultnet.transport.rudp.v0` -session path for acknowledged CultNet messages over UDP. That is substrate -only. Idunn still marks daemon profiles as -`migration-required` until each daemon actually publishes health and command -boundary records through that path. - -## Boundaries - -Idunn is not Odin. It does not decide which Verses exist. - -Idunn is not Bifrost. It does not own public crossings, Discord delivery, owner -DMs, or governance transport. - -Idunn is not Eve. It does not own presentation. - -Idunn is the keeper of continuity. If a daemon should be alive, Idunn should -know that, check it, help it recover, and leave a witness trail sharp enough -that nobody has to guess what happened.