From c137e16ba5da5a86600703584ea085e3fecd16ff Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 10 Aug 2026 01:51:52 +0100 Subject: [PATCH] Archive completed model generations --- epiphany-core/src/bin/epiphany-swarm.rs | 17 +- epiphany-core/src/lib.rs | 17 +- epiphany-core/src/runtime_spine.rs | 1222 +++++++++++++++-- notes/epiphany-current-algorithmic-map.md | 20 + notes/fresh-workspace-handoff.md | 26 + ...phany.runtime.archived-session.schema.json | 36 + schemas/cultnet/index.json | 11 + state/map.yaml | 10 +- 8 files changed, 1253 insertions(+), 106 deletions(-) create mode 100644 schemas/cultnet/epiphany.runtime.archived-session.schema.json diff --git a/epiphany-core/src/bin/epiphany-swarm.rs b/epiphany-core/src/bin/epiphany-swarm.rs index df86f35f..8502accb 100644 --- a/epiphany-core/src/bin/epiphany-swarm.rs +++ b/epiphany-core/src/bin/epiphany-swarm.rs @@ -13,10 +13,10 @@ use epiphany_core::{ load_resident_self_state, observe_process_instance, pending_resident_self_acks, prepare_resident_self_launch, publish_resident_provider_readiness, resident_prepared_launch_thread_id, resident_self_child_claim, - resident_self_local_provider_status, retain_coordinator_run_receipts, - retain_resident_self_lifecycles, settle_resident_self_exited_coordinator, - terminate_process_instance, validate_persona_feedback_store_separation, - validate_resident_self_store_separation, + resident_self_local_provider_status, retain_completed_model_sessions, + retain_coordinator_run_receipts, retain_resident_self_lifecycles, + settle_resident_self_exited_coordinator, terminate_process_instance, + validate_persona_feedback_store_separation, validate_resident_self_store_separation, }; use serde_json::json; use std::collections::{BTreeMap, BTreeSet}; @@ -141,6 +141,11 @@ fn retain_runtime_receipts(args: &Args, state: &ResidentSelfState) -> Result<()> &preserved, &Utc::now().to_rfc3339(), )?; + retain_completed_model_sessions( + &args.policy.runtime_store, + args.retained_completed_model_sessions, + &Utc::now().to_rfc3339(), + )?; Ok(()) } @@ -449,6 +454,7 @@ struct Args { provider_freshness_seconds: u64, retained_closed_lifecycles: usize, retained_coordinator_receipts: usize, + retained_completed_model_sessions: usize, persona_feedback_source_store: PathBuf, persona_feedback_store: PathBuf, bifrost_feedback_trust_anchor: PathBuf, @@ -556,6 +562,9 @@ impl Args { retained_coordinator_receipts: u64v("--retained-coordinator-receipts", 256)? .try_into() .context("--retained-coordinator-receipts exceeds platform size")?, + retained_completed_model_sessions: u64v("--retained-completed-model-sessions", 256)? + .try_into() + .context("--retained-completed-model-sessions exceeds platform size")?, persona_feedback_source_store: path("--persona-feedback-source-store")?, persona_feedback_store: path("--persona-feedback-store")?, bifrost_feedback_trust_anchor: path("--bifrost-feedback-trust-anchor")?, diff --git a/epiphany-core/src/lib.rs b/epiphany-core/src/lib.rs index 85692337..4eea15a0 100644 --- a/epiphany-core/src/lib.rs +++ b/epiphany-core/src/lib.rs @@ -970,8 +970,11 @@ pub use repository_body_observer::*; pub use repository_readiness::*; pub use resident_readiness::*; pub use resident_self::*; +pub use runtime_spine::ARCHIVED_RUNTIME_SESSION_SCHEMA_VERSION; +pub use runtime_spine::ARCHIVED_RUNTIME_SESSION_TYPE; pub use runtime_spine::COORDINATOR_RUN_RECEIPT_SCHEMA_VERSION; pub use runtime_spine::COORDINATOR_RUN_RECEIPT_TYPE; +pub use runtime_spine::EpiphanyArchivedRuntimeSession; pub use runtime_spine::EpiphanyCoordinatorRunReceipt; pub use runtime_spine::EpiphanyCoordinatorRunReceiptRetentionHead; pub use runtime_spine::EpiphanyJobInterruptRequest; @@ -985,13 +988,13 @@ pub use runtime_spine::EpiphanyRuntimeJobResult; pub use runtime_spine::EpiphanyRuntimeJobSnapshot; pub use runtime_spine::EpiphanyRuntimeJobStatus; pub use runtime_spine::EpiphanyRuntimeModelExecutionBinding; -pub use runtime_spine::EpiphanyRuntimeToolExecutionBinding; pub use runtime_spine::EpiphanyRuntimeReorientWorkerResult; pub use runtime_spine::EpiphanyRuntimeRoleWorkerResult; pub use runtime_spine::EpiphanyRuntimeSession; pub use runtime_spine::EpiphanyRuntimeSessionStatus; pub use runtime_spine::EpiphanyRuntimeSpineStatus; pub use runtime_spine::EpiphanyRuntimeSwarmBinding; +pub use runtime_spine::EpiphanyRuntimeToolExecutionBinding; pub use runtime_spine::EpiphanyRuntimeWorkerLaunchRequest; pub use runtime_spine::EpiphanyToolInvocationStatus; pub use runtime_spine::PreparedRuntimeSpineHeartbeatJob; @@ -1002,8 +1005,6 @@ pub use runtime_spine::RUNTIME_JOB_RESULT_TYPE; pub use runtime_spine::RUNTIME_JOB_TYPE; pub use runtime_spine::RUNTIME_MODEL_EXECUTION_BINDING_SCHEMA_VERSION; pub use runtime_spine::RUNTIME_MODEL_EXECUTION_BINDING_TYPE; -pub use runtime_spine::RUNTIME_TOOL_EXECUTION_BINDING_SCHEMA_VERSION; -pub use runtime_spine::RUNTIME_TOOL_EXECUTION_BINDING_TYPE; pub use runtime_spine::RUNTIME_REORIENT_WORKER_RESULT_SCHEMA_VERSION; pub use runtime_spine::RUNTIME_REORIENT_WORKER_RESULT_TYPE; pub use runtime_spine::RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION; @@ -1012,6 +1013,8 @@ pub use runtime_spine::RUNTIME_SESSION_TYPE; pub use runtime_spine::RUNTIME_SPINE_SCHEMA_VERSION; pub use runtime_spine::RUNTIME_SWARM_BINDING_KEY; pub use runtime_spine::RUNTIME_SWARM_BINDING_SCHEMA_VERSION; +pub use runtime_spine::RUNTIME_TOOL_EXECUTION_BINDING_SCHEMA_VERSION; +pub use runtime_spine::RUNTIME_TOOL_EXECUTION_BINDING_TYPE; pub use runtime_spine::RUNTIME_WORKER_LAUNCH_REQUEST_SCHEMA_VERSION; pub use runtime_spine::RUNTIME_WORKER_LAUNCH_REQUEST_TYPE; pub use runtime_spine::RepoFrontierVerdictModelingLaunchAuthority; @@ -1029,6 +1032,7 @@ pub use runtime_spine::RuntimeTypedFulfillmentEvidence; pub use runtime_spine::RuntimeTypedRequestRef; pub use runtime_spine::amend_repo_frontier_execution; pub use runtime_spine::append_runtime_event; +pub use runtime_spine::archive_completed_model_session; pub use runtime_spine::bind_runtime_repository_domain; pub use runtime_spine::bind_runtime_to_agent_memory_swarm; pub use runtime_spine::canonical_repo_frontier_plan_candidate_id; @@ -1051,12 +1055,12 @@ pub use runtime_spine::ensure_runtime_session; pub use runtime_spine::initialize_runtime_spine; pub use runtime_spine::intake_user_repo_frontier_proposal; pub use runtime_spine::migrate_legacy_repo_model_projection_obligation; +pub use runtime_spine::open_runtime_model_execution; pub use runtime_spine::open_runtime_spine_heartbeat_job; pub use runtime_spine::operator_repo_frontier_plan_review_is_current; pub use runtime_spine::pending_repo_frontier_plan_reviews; pub use runtime_spine::plan_runtime_spine_heartbeat_launch; pub use runtime_spine::prepare_runtime_spine_heartbeat_job; -pub use runtime_spine::retain_coordinator_run_receipts; pub use runtime_spine::promote_autonomous_direction_options_for_modeling; pub use runtime_spine::put_continuity_recovery_receipt; pub use runtime_spine::put_coordinator_run_receipt; @@ -1075,15 +1079,16 @@ pub use runtime_spine::put_repo_frontier_verification_request; pub use runtime_spine::put_repo_frontier_work_proposal; pub use runtime_spine::put_runtime_reorient_worker_result; pub use runtime_spine::put_runtime_role_worker_result; -pub use runtime_spine::open_runtime_model_execution; pub use runtime_spine::put_runtime_tool_execution_intent; pub use runtime_spine::put_runtime_tool_execution_receipt; pub use runtime_spine::put_soul_verdict_receipt; pub use runtime_spine::put_substrate_gate_repo_access_grant_receipt; pub use runtime_spine::relinquish_repo_frontier_hands_route; pub use runtime_spine::replace_or_append_epiphany_job_binding; -pub use runtime_spine::runtime_continuity_recovery_receipt; pub use runtime_spine::require_runtime_tool_execution_binding; +pub use runtime_spine::retain_completed_model_sessions; +pub use runtime_spine::retain_coordinator_run_receipts; +pub use runtime_spine::runtime_continuity_recovery_receipt; pub use runtime_spine::runtime_current_repo_model; pub use runtime_spine::runtime_eyes_evidence_packet; pub use runtime_spine::runtime_hands_action_intent; diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index 0455c706..e2cb49fc 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -134,10 +134,12 @@ use epiphany_model_adapter::EpiphanyModelAdapterStatus; use epiphany_model_adapter::EpiphanyModelReceipt; use epiphany_model_adapter::EpiphanyModelRequest; use epiphany_model_adapter::EpiphanyModelStreamEvent; +use epiphany_model_adapter::EpiphanyModelStreamPayload; use epiphany_openai_adapter::EpiphanyOpenAiAdapterStatus; use epiphany_openai_adapter::EpiphanyOpenAiModelReceipt; use epiphany_openai_adapter::EpiphanyOpenAiModelRequest; use epiphany_openai_adapter::EpiphanyOpenAiStreamEvent; +use epiphany_openai_adapter::EpiphanyOpenAiStreamPayload; use epiphany_state_model::EpiphanyJobBinding; use epiphany_state_model::EpiphanyJobKind; use epiphany_state_model::EpiphanyRuntimeLink; @@ -160,10 +162,9 @@ use std::path::PathBuf; pub const RUNTIME_IDENTITY_TYPE: &str = "epiphany.runtime.identity"; pub const RUNTIME_SESSION_TYPE: &str = "epiphany.runtime.session"; pub const RUNTIME_JOB_TYPE: &str = "epiphany.runtime.job"; -pub const RUNTIME_MODEL_EXECUTION_BINDING_TYPE: &str = - "epiphany.runtime.model_execution_binding"; -pub const RUNTIME_TOOL_EXECUTION_BINDING_TYPE: &str = - "epiphany.runtime.tool_execution_binding"; +pub const RUNTIME_MODEL_EXECUTION_BINDING_TYPE: &str = "epiphany.runtime.model_execution_binding"; +pub const RUNTIME_TOOL_EXECUTION_BINDING_TYPE: &str = "epiphany.runtime.tool_execution_binding"; +pub const ARCHIVED_RUNTIME_SESSION_TYPE: &str = "epiphany.runtime.archived_session"; pub const RUNTIME_WORKER_LAUNCH_REQUEST_TYPE: &str = "epiphany.runtime.worker_launch_request"; pub const RUNTIME_ROLE_WORKER_RESULT_TYPE: &str = "epiphany.runtime.role_worker_result"; pub const RUNTIME_REORIENT_WORKER_RESULT_TYPE: &str = "epiphany.runtime.reorient_worker_result"; @@ -193,6 +194,7 @@ pub const RUNTIME_MODEL_EXECUTION_BINDING_SCHEMA_VERSION: &str = "epiphany.runtime.model_execution_binding.v0"; pub const RUNTIME_TOOL_EXECUTION_BINDING_SCHEMA_VERSION: &str = "epiphany.runtime.tool_execution_binding.v0"; +pub const ARCHIVED_RUNTIME_SESSION_SCHEMA_VERSION: &str = "epiphany.runtime.archived_session.v0"; pub const RUNTIME_WORKER_LAUNCH_REQUEST_SCHEMA_VERSION: &str = "epiphany.runtime.worker_launch_request.v1"; pub const RUNTIME_ROLE_WORKER_RESULT_SCHEMA_VERSION: &str = @@ -371,6 +373,38 @@ pub struct EpiphanyRuntimeToolExecutionBinding { pub bound_at: String, } +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "epiphany.runtime.archived_session", + schema = "EpiphanyArchivedRuntimeSession" +)] +pub struct EpiphanyArchivedRuntimeSession { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub archive_id: String, + #[cultcache(key = 2)] + pub session_id: String, + #[cultcache(key = 3)] + pub archived_at: String, + #[cultcache(key = 4)] + pub job_ids: Vec, + #[cultcache(key = 5)] + pub job_result_ids: Vec, + #[cultcache(key = 6)] + pub model_request_ids: Vec, + #[cultcache(key = 7)] + pub tool_intent_ids: Vec, + #[cultcache(key = 8)] + pub terminal_job_status_counts: BTreeMap, + #[cultcache(key = 9)] + pub retired_type_counts: BTreeMap, + #[cultcache(key = 10)] + pub retired_envelope_count: u64, + #[cultcache(key = 11)] + pub retired_chain_digest: String, +} + #[derive(Clone, Debug, PartialEq, DatabaseEntry)] #[cultcache( type = "epiphany.runtime.worker_launch_request", @@ -1005,6 +1039,7 @@ pub fn runtime_spine_cache(store_path: impl AsRef) -> Result { cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; + cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; cache.register_entry_type::()?; @@ -1260,6 +1295,7 @@ pub fn create_runtime_session( let mut cache = runtime_spine_cache(store_path.as_ref())?; cache.pull_all_backing_stores()?; require_identity(&cache)?; + require_runtime_identity_not_archived(&cache, "session", &options.session_id)?; if cache .get::(&options.session_id)? .is_some() @@ -1293,6 +1329,7 @@ pub fn ensure_runtime_session( let mut cache = runtime_spine_cache(store_path.as_ref())?; cache.pull_all_backing_stores()?; require_identity(&cache)?; + require_runtime_identity_not_archived(&cache, "session", &options.session_id)?; if let Some(existing) = cache.get::(&options.session_id)? { if matches!( existing.status, @@ -1399,6 +1436,7 @@ pub fn create_runtime_job( let mut cache = runtime_spine_cache(store_path.as_ref())?; cache.pull_all_backing_stores()?; require_identity(&cache)?; + require_runtime_identity_not_archived(&cache, "job", &options.job_id)?; let session = cache .get::(&options.session_id)? .ok_or_else(|| anyhow!("runtime session {:?} does not exist", options.session_id))?; @@ -1452,7 +1490,10 @@ pub fn open_runtime_model_execution( ) -> Result { validate_non_empty(&session_options.session_id, "model execution session id")?; validate_non_empty(&session_options.objective, "model execution objective")?; - validate_non_empty(&session_options.created_at, "model execution session creation time")?; + validate_non_empty( + &session_options.created_at, + "model execution session creation time", + )?; validate_non_empty(&job_options.job_id, "model execution job id")?; validate_non_empty(&job_options.role, "model execution job role")?; validate_non_empty(&job_options.created_at, "model execution job creation time")?; @@ -1479,17 +1520,22 @@ pub fn open_runtime_model_execution( let mut cache = runtime_spine_cache(store_path)?; cache.pull_all_backing_stores()?; require_identity(&cache)?; + require_runtime_identity_not_archived(&cache, "session", &session_options.session_id)?; + require_runtime_identity_not_archived(&cache, "job", &job_options.job_id)?; + require_runtime_identity_not_archived(&cache, "model-request", &model_request.request_id)?; let existing_session = cache.get::(&session_options.session_id)?; - let session = existing_session.clone().unwrap_or_else(|| EpiphanyRuntimeSession { - schema_version: RUNTIME_SPINE_SCHEMA_VERSION.to_string(), - session_id: session_options.session_id.clone(), - objective: session_options.objective.clone(), - status: EpiphanyRuntimeSessionStatus::Active, - created_at: session_options.created_at.clone(), - updated_at: session_options.created_at.clone(), - coordinator_note: session_options.coordinator_note.clone(), - metadata: BTreeMap::new(), - }); + let session = existing_session + .clone() + .unwrap_or_else(|| EpiphanyRuntimeSession { + schema_version: RUNTIME_SPINE_SCHEMA_VERSION.to_string(), + session_id: session_options.session_id.clone(), + objective: session_options.objective.clone(), + status: EpiphanyRuntimeSessionStatus::Active, + created_at: session_options.created_at.clone(), + updated_at: session_options.created_at.clone(), + coordinator_note: session_options.coordinator_note.clone(), + metadata: BTreeMap::new(), + }); if matches!( session.status, EpiphanyRuntimeSessionStatus::Completed | EpiphanyRuntimeSessionStatus::Archived @@ -1499,7 +1545,10 @@ pub fn open_runtime_model_execution( session.session_id )); } - if cache.get::(&job_options.job_id)?.is_some() { + if cache + .get::(&job_options.job_id)? + .is_some() + { return Err(anyhow!( "model execution job {:?} already exists", job_options.job_id @@ -1589,9 +1638,7 @@ pub fn open_runtime_model_execution( .prepare_entry(&provider_request.request_id, provider_request)? .0, ]; - if !runtime_spine_backing_store(store_path)? - .compare_and_swap_batch(&expected, replacements)? - { + if !runtime_spine_backing_store(store_path)?.compare_and_swap_batch(&expected, replacements)? { return Err(anyhow!( "model execution request publication lost its snapshot fence" )); @@ -1619,6 +1666,7 @@ pub fn put_runtime_tool_execution_intent( let mut cache = runtime_spine_cache(store_path)?; cache.pull_all_backing_stores()?; require_identity(&cache)?; + require_runtime_identity_not_archived(&cache, "tool-intent", &intent.intent_id)?; let session = cache .get::(session_id)? .ok_or_else(|| anyhow!("tool execution session {session_id:?} does not exist"))?; @@ -1693,8 +1741,7 @@ pub fn put_runtime_tool_execution_intent( )); } for envelope in [ - cache - .get_envelope::(model_request_id)?, + cache.get_envelope::(model_request_id)?, cache.get_envelope::(model_request_id)?, cache.get_envelope::(model_request_id)?, ] { @@ -1723,9 +1770,7 @@ pub fn put_runtime_tool_execution_intent( .prepare_entry(&tool_invocation_intent_key(&intent.intent_id), intent)? .0, ); - if !runtime_spine_backing_store(store_path)? - .compare_and_swap_batch(&expected, replacements)? - { + if !runtime_spine_backing_store(store_path)?.compare_and_swap_batch(&expected, replacements)? { return Err(anyhow!( "tool execution intent publication lost its ownership fence" )); @@ -1797,7 +1842,10 @@ pub fn put_runtime_tool_execution_receipt( validate_non_empty(&receipt.receipt_id, "tool execution receipt id")?; validate_non_empty(&receipt.intent_id, "tool execution receipt intent id")?; validate_non_empty(&receipt.status, "tool execution receipt status")?; - validate_non_empty(&receipt.completed_at, "tool execution receipt completion time")?; + validate_non_empty( + &receipt.completed_at, + "tool execution receipt completion time", + )?; if !matches!(receipt.status.as_str(), "completed" | "failed") { return Err(anyhow!("tool execution receipt status is not terminal")); } @@ -1806,9 +1854,7 @@ pub fn put_runtime_tool_execution_receipt( let mut cache = runtime_spine_cache(store_path)?; cache.pull_all_backing_stores()?; let intent = cache - .get::(&tool_invocation_intent_key( - &receipt.intent_id, - ))? + .get::(&tool_invocation_intent_key(&receipt.intent_id))? .ok_or_else(|| anyhow!("tool execution receipt lost its intent"))?; if receipt.adapter != intent.adapter || receipt.server != intent.server @@ -1851,6 +1897,521 @@ pub fn put_runtime_tool_execution_receipt( Ok(()) } +pub fn archive_completed_model_session( + store_path: impl AsRef, + session_id: &str, + archived_at: &str, +) -> Result { + archive_completed_model_session_with_before_commit(store_path, session_id, archived_at, || { + Ok(()) + }) +} + +pub fn retain_completed_model_sessions( + store_path: impl AsRef, + retain_recent: usize, + archived_at: &str, +) -> Result> { + chrono::DateTime::parse_from_rfc3339(archived_at) + .map_err(|error| anyhow!("runtime session retention timestamp is invalid: {error}"))?; + let store_path = store_path.as_ref(); + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + require_identity(&cache)?; + + let jobs = cache.get_all::()?; + let bindings = cache.get_all::()?; + let mut candidates = cache + .get_all::()? + .into_iter() + .filter(|session| session.status == EpiphanyRuntimeSessionStatus::Completed) + .filter(|session| { + let session_jobs = jobs + .iter() + .filter(|job| job.session_id == session.session_id) + .collect::>(); + !session_jobs.is_empty() + && session_jobs.iter().all(|job| { + job.role == "openai-model-adapter" + && bindings.iter().any(|binding| binding.job_id == job.job_id) + }) + }) + .collect::>(); + candidates.sort_by(|left, right| { + right + .updated_at + .cmp(&left.updated_at) + .then_with(|| right.session_id.cmp(&left.session_id)) + }); + + let mut archived = Vec::new(); + for session in candidates.into_iter().skip(retain_recent.max(1)) { + archived.push(archive_completed_model_session( + store_path, + &session.session_id, + archived_at, + )?); + } + Ok(archived) +} + +fn archive_completed_model_session_with_before_commit( + store_path: impl AsRef, + session_id: &str, + archived_at: &str, + before_commit: F, +) -> Result +where + F: FnOnce() -> Result<()>, +{ + validate_non_empty(session_id, "archived runtime session id")?; + chrono::DateTime::parse_from_rfc3339(archived_at) + .map_err(|error| anyhow!("runtime session archive timestamp is invalid: {error}"))?; + let store_path = store_path.as_ref(); + let mut cache = runtime_spine_cache(store_path)?; + cache.pull_all_backing_stores()?; + require_identity(&cache)?; + if let Some(existing) = cache.get::(session_id)? { + if existing.schema_version != ARCHIVED_RUNTIME_SESSION_SCHEMA_VERSION + || existing.archive_id != session_id + || existing.session_id != session_id + || !existing.retired_chain_digest.starts_with("sha256:") + { + return Err(anyhow!("archived runtime session tombstone is invalid")); + } + if cache.get::(session_id)?.is_some() { + return Err(anyhow!( + "archived runtime session still has live session authority" + )); + } + return Ok(existing); + } + let session = cache + .get::(session_id)? + .ok_or_else(|| anyhow!("runtime session {session_id:?} does not exist"))?; + if session.status != EpiphanyRuntimeSessionStatus::Completed { + return Err(anyhow!("runtime session {session_id:?} is not completed")); + } + let mut jobs = cache + .get_all::()? + .into_iter() + .filter(|job| job.session_id == session_id) + .collect::>(); + jobs.sort_by(|left, right| left.job_id.cmp(&right.job_id)); + if jobs.is_empty() + || jobs.iter().any(|job| { + job.role != "openai-model-adapter" + || matches!( + job.status, + EpiphanyRuntimeJobStatus::Queued + | EpiphanyRuntimeJobStatus::Running + | EpiphanyRuntimeJobStatus::WaitingForReview + ) + }) + { + return Err(anyhow!( + "runtime session archive accepts only terminal model-adapter jobs" + )); + } + let job_ids = jobs + .iter() + .map(|job| job.job_id.clone()) + .collect::>(); + let mut job_results = cache + .get_all::()? + .into_iter() + .filter(|result| result.session_id == session_id) + .collect::>(); + job_results.sort_by(|left, right| left.result_id.cmp(&right.result_id)); + if jobs.iter().any(|job| { + job_results + .iter() + .filter(|result| result.job_id == job.job_id) + .count() + != 1 + }) || job_results + .iter() + .any(|result| !job_ids.contains(&result.job_id)) + { + return Err(anyhow!( + "runtime session archive requires exactly one terminal result per job" + )); + } + if cache + .get_all::()? + .iter() + .any(|item| job_ids.contains(&item.job_id)) + || cache + .get_all::()? + .iter() + .any(|item| job_ids.contains(&item.job_id)) + || cache + .get_all::()? + .iter() + .any(|item| job_ids.contains(&item.job_id)) + || cache + .get_all::()? + .iter() + .any(|item| item.session_id == session_id) + { + return Err(anyhow!( + "runtime session archive refuses outer-worker or coordinator authority" + )); + } + + let mut model_bindings = cache + .get_all::()? + .into_iter() + .filter(|binding| binding.session_id == session_id) + .collect::>(); + model_bindings.sort_by(|left, right| left.request_id.cmp(&right.request_id)); + if jobs.iter().any(|job| { + model_bindings + .iter() + .filter(|binding| binding.job_id == job.job_id) + .count() + != 1 + }) || model_bindings + .iter() + .any(|binding| !job_ids.contains(&binding.job_id)) + { + return Err(anyhow!( + "runtime session archive requires one model execution binding per job" + )); + } + let model_request_ids = model_bindings + .iter() + .map(|binding| binding.request_id.clone()) + .collect::>(); + for request_id in &model_request_ids { + let native_request = cache + .get::(request_id)? + .ok_or_else(|| anyhow!("archived model execution lost native request"))?; + let provider_request = cache + .get::(request_id)? + .ok_or_else(|| anyhow!("archived model execution lost provider request"))?; + if native_request.request_id != *request_id + || provider_request.request_id != *request_id + || native_request.conversation_id != provider_request.conversation_id + || native_request.model != provider_request.model + { + return Err(anyhow!( + "archived model execution request family is inconsistent" + )); + } + let mut native_events = cache + .get_all::()? + .into_iter() + .filter(|event| event.request_id == *request_id) + .collect::>(); + native_events.sort_by_key(|event| event.sequence); + let native_terminals = native_events + .iter() + .filter(|event| { + matches!( + &event.payload, + EpiphanyModelStreamPayload::Completed { .. } + | EpiphanyModelStreamPayload::Failed { .. } + ) + }) + .collect::>(); + let mut provider_events = cache + .get_all::()? + .into_iter() + .filter(|event| event.request_id == *request_id) + .collect::>(); + provider_events.sort_by_key(|event| event.sequence); + let provider_terminals = provider_events + .iter() + .filter(|event| { + matches!( + &event.payload, + EpiphanyOpenAiStreamPayload::Completed { .. } + | EpiphanyOpenAiStreamPayload::Failed { .. } + ) + }) + .collect::>(); + if native_terminals.len() != 1 + || provider_terminals.len() != 1 + || native_events.last().map(|event| event.sequence) + != native_terminals.first().map(|event| event.sequence) + || provider_events.last().map(|event| event.sequence) + != provider_terminals.first().map(|event| event.sequence) + { + return Err(anyhow!( + "runtime session archive requires one native and provider terminal stream event" + )); + } + match &native_terminals[0].payload { + EpiphanyModelStreamPayload::Completed { receipt } => { + if cache.get::(request_id)?.as_ref() != Some(receipt) { + return Err(anyhow!( + "runtime session archive found inconsistent native model receipt" + )); + } + } + EpiphanyModelStreamPayload::Failed { .. } => { + if cache.get::(request_id)?.is_some() { + return Err(anyhow!( + "failed native model stream retained a success receipt" + )); + } + } + _ => unreachable!("terminal event filtered above"), + } + match &provider_terminals[0].payload { + EpiphanyOpenAiStreamPayload::Completed { receipt } => { + if cache + .get::(request_id)? + .as_ref() + != Some(receipt) + { + return Err(anyhow!( + "runtime session archive found inconsistent provider model receipt" + )); + } + } + EpiphanyOpenAiStreamPayload::Failed { .. } => { + if cache + .get::(request_id)? + .is_some() + { + return Err(anyhow!( + "failed provider model stream retained a success receipt" + )); + } + } + _ => unreachable!("terminal event filtered above"), + } + } + + let mut tool_bindings = cache + .get_all::()? + .into_iter() + .filter(|binding| binding.session_id == session_id) + .collect::>(); + tool_bindings.sort_by(|left, right| left.intent_id.cmp(&right.intent_id)); + if tool_bindings.iter().any(|binding| { + !job_ids.contains(&binding.job_id) + || binding + .model_request_id + .as_ref() + .is_some_and(|request_id| !model_request_ids.contains(request_id)) + }) { + return Err(anyhow!( + "runtime session archive found foreign tool execution ownership" + )); + } + let tool_intent_ids = tool_bindings + .iter() + .map(|binding| binding.intent_id.clone()) + .collect::>(); + let unbound_model_intent = cache + .get_all::()? + .into_iter() + .any(|intent| { + intent + .model_request_id + .as_ref() + .is_some_and(|request_id| model_request_ids.contains(request_id)) + && !tool_intent_ids.contains(&intent.intent_id) + }); + if unbound_model_intent { + return Err(anyhow!( + "runtime session archive found a legacy unbound model tool intent" + )); + } + for binding in &tool_bindings { + require_runtime_tool_execution_binding(store_path, &binding.intent_id)?; + let receipt = cache + .get::(&tool_invocation_receipt_key(&binding.intent_id))? + .ok_or_else(|| anyhow!("runtime session archive found pending tool authority"))?; + if receipt.intent_id != binding.intent_id + || !matches!(receipt.status.as_str(), "completed" | "failed") + { + return Err(anyhow!( + "runtime session archive found invalid tool terminal evidence" + )); + } + } + + let events = cache + .get_all::()? + .into_iter() + .filter(|event| event.session_id.as_deref() == Some(session_id)) + .collect::>(); + if events + .iter() + .filter(|event| event.event_type == "session.completed" && event.job_id.is_none()) + .count() + != 1 + { + return Err(anyhow!( + "runtime session archive requires one session completion event" + )); + } + + let snapshot = cache.snapshot_envelopes(); + let mut retired_identities = BTreeSet::<(String, String)>::new(); + retired_identities.insert(( + EpiphanyRuntimeSession::TYPE.to_string(), + session_id.to_string(), + )); + for job in &jobs { + retired_identities.insert((EpiphanyRuntimeJob::TYPE.to_string(), job.job_id.clone())); + } + for result in &job_results { + retired_identities.insert(( + EpiphanyRuntimeJobResult::TYPE.to_string(), + result.result_id.clone(), + )); + } + for event in &events { + retired_identities.insert(( + EpiphanyRuntimeEvent::TYPE.to_string(), + event.event_id.clone(), + )); + } + for binding in &model_bindings { + retired_identities.insert(( + EpiphanyRuntimeModelExecutionBinding::TYPE.to_string(), + binding.binding_id.clone(), + )); + retired_identities.insert(( + EpiphanyModelRequest::TYPE.to_string(), + binding.request_id.clone(), + )); + retired_identities.insert(( + EpiphanyOpenAiModelRequest::TYPE.to_string(), + binding.request_id.clone(), + )); + for event in cache + .get_all::()? + .into_iter() + .filter(|event| event.request_id == binding.request_id) + { + retired_identities.insert(( + EpiphanyModelStreamEvent::TYPE.to_string(), + format!("{}:{:08}", event.request_id, event.sequence), + )); + } + for event in cache + .get_all::()? + .into_iter() + .filter(|event| event.request_id == binding.request_id) + { + retired_identities.insert(( + EpiphanyOpenAiStreamEvent::TYPE.to_string(), + format!("{}:{:08}", event.request_id, event.sequence), + )); + } + if cache + .get::(&binding.request_id)? + .is_some() + { + retired_identities.insert(( + EpiphanyModelReceipt::TYPE.to_string(), + binding.request_id.clone(), + )); + } + if cache + .get::(&binding.request_id)? + .is_some() + { + retired_identities.insert(( + EpiphanyOpenAiModelReceipt::TYPE.to_string(), + binding.request_id.clone(), + )); + } + } + for binding in &tool_bindings { + retired_identities.insert(( + EpiphanyRuntimeToolExecutionBinding::TYPE.to_string(), + binding.binding_id.clone(), + )); + retired_identities.insert(( + EpiphanyToolInvocationIntent::TYPE.to_string(), + tool_invocation_intent_key(&binding.intent_id), + )); + retired_identities.insert(( + EpiphanyToolInvocationReceipt::TYPE.to_string(), + tool_invocation_receipt_key(&binding.intent_id), + )); + } + let mut deletions = retired_identities + .iter() + .map(|(document_type, key)| { + snapshot + .iter() + .find(|entry| entry.r#type == *document_type && entry.key == *key) + .cloned() + .ok_or_else(|| { + anyhow!("runtime session archive lost exact envelope {document_type:?}/{key:?}") + }) + }) + .collect::>>()?; + deletions.sort_by(|left, right| { + left.r#type + .cmp(&right.r#type) + .then(left.key.cmp(&right.key)) + }); + let mut retired_type_counts = BTreeMap::new(); + let mut digest = Sha256::new(); + digest.update(b"epiphany-runtime-archived-session-root"); + for entry in &deletions { + *retired_type_counts.entry(entry.r#type.clone()).or_default() += 1; + for bytes in [ + entry.r#type.as_bytes(), + entry.key.as_bytes(), + entry.payload.as_slice(), + ] { + digest.update((bytes.len() as u64).to_le_bytes()); + digest.update(bytes); + } + } + let mut terminal_job_status_counts = BTreeMap::new(); + for job in &jobs { + let status = match job.status { + EpiphanyRuntimeJobStatus::Completed => "completed", + EpiphanyRuntimeJobStatus::Failed => "failed", + EpiphanyRuntimeJobStatus::Cancelled => "cancelled", + _ => unreachable!("open jobs refused above"), + }; + *terminal_job_status_counts + .entry(status.to_string()) + .or_default() += 1; + } + let archive = EpiphanyArchivedRuntimeSession { + schema_version: ARCHIVED_RUNTIME_SESSION_SCHEMA_VERSION.to_string(), + archive_id: session_id.to_string(), + session_id: session_id.to_string(), + archived_at: archived_at.to_string(), + job_ids: job_ids.into_iter().collect(), + job_result_ids: job_results + .iter() + .map(|result| result.result_id.clone()) + .collect(), + model_request_ids: model_request_ids.into_iter().collect(), + tool_intent_ids: tool_intent_ids.into_iter().collect(), + terminal_job_status_counts, + retired_type_counts, + retired_envelope_count: deletions.len() as u64, + retired_chain_digest: format!("sha256:{:x}", digest.finalize()), + }; + let (replacement, _) = cache.prepare_entry(session_id, &archive)?; + before_commit()?; + if !runtime_spine_backing_store(store_path)?.replace_and_delete_if_snapshot_unchanged( + &snapshot, + vec![replacement], + &deletions, + )? { + return Err(anyhow!( + "runtime session archive lost its full snapshot fence" + )); + } + Ok(archive) +} + pub fn plan_runtime_spine_heartbeat_launch( state: &EpiphanyThreadState, options: RuntimeSpineHeartbeatLaunchPlanOptions, @@ -3244,8 +3805,7 @@ fn validate_proposal_modeling_worker_fulfillment( "Hands" | "Eyes" | "Imagination" ) || (proposal.source_kind == crate::RepoFrontierProposalSourceKind::Imagination - && (upserts[0].recommended_next_organ == "Hands" - || upserts[0].adopted_plan.is_some())) + && (upserts[0].recommended_next_organ == "Hands" || upserts[0].adopted_plan.is_some())) { return Err(anyhow!( "proposal Modeling fulfillment result is not one safe proposal-citing routeable frontier" @@ -9738,6 +10298,33 @@ fn require_identity(cache: &CultCache) -> Result { .ok_or_else(|| anyhow!("runtime spine is missing identity; run init first")) } +fn require_runtime_identity_not_archived( + cache: &CultCache, + identity_kind: &str, + identity: &str, +) -> Result<()> { + let collision = cache + .get_all::()? + .into_iter() + .find(|archive| match identity_kind { + "session" => archive.session_id == identity, + "job" => archive.job_ids.iter().any(|item| item == identity), + "model-request" => archive + .model_request_ids + .iter() + .any(|item| item == identity), + "tool-intent" => archive.tool_intent_ids.iter().any(|item| item == identity), + _ => false, + }); + if let Some(archive) = collision { + return Err(anyhow!( + "runtime {identity_kind} identity {identity:?} was retired by archive {:?}", + archive.archive_id + )); + } + Ok(()) +} + pub fn runtime_registered_document_types() -> Vec { let mut document_types = Vec::new(); for contract in epiphany_mutation_contracts() { @@ -10472,6 +11059,18 @@ fn epiphany_mutation_contracts() -> Vec { "Model-derived tool intents must inherit the exact owning model execution; direct intents require explicit runtime ownership.", ], ), + mutation_contract( + ARCHIVED_RUNTIME_SESSION_TYPE, + ARCHIVED_RUNTIME_SESSION_SCHEMA_VERSION, + vec![CultNetDocumentOperation::Snapshot], + CultNetMutationAuthority::ReadOnly, + vec![], + vec![], + vec![ + "Runtime spine alone archives an exact completed model-session generation under a full snapshot fence.", + "The tombstone preserves retired identities and digest evidence, blocks ID reuse, and cannot satisfy execution authority.", + ], + ), mutation_contract( MODEL_ADAPTER_STATUS_TYPE, MODEL_ADAPTER_STATUS_SCHEMA_VERSION, @@ -10919,6 +11518,101 @@ pub(crate) mod tests { use cultnet_rs::decode_cultnet_message_from_slice; use tempfile::tempdir; + fn close_test_model_generation( + store: &Path, + suffix: &str, + created_at: &str, + completed_at: &str, + ) -> Result<()> { + let session_id = format!("session-{suffix}"); + let job_id = format!("job-{suffix}"); + let request_id = format!("request-{suffix}"); + let model_request = EpiphanyModelRequest::new( + request_id.clone(), + format!("conversation-{suffix}"), + "openai-codex", + "gpt-test", + "Return one terminal result.", + ); + let provider_request = EpiphanyOpenAiModelRequest::new( + request_id.clone(), + format!("conversation-{suffix}"), + "gpt-test", + "Return one terminal result.", + ); + open_runtime_model_execution( + store, + RuntimeSpineSessionOptions { + session_id: session_id.clone(), + objective: format!("Close model generation {suffix}."), + created_at: created_at.to_string(), + coordinator_note: "retention test".to_string(), + }, + RuntimeSpineJobOptions { + job_id: job_id.clone(), + session_id: session_id.clone(), + role: "openai-model-adapter".to_string(), + created_at: created_at.to_string(), + summary: "Bound model generation.".to_string(), + artifact_refs: Vec::new(), + }, + &model_request, + &provider_request, + created_at, + )?; + let native_receipt = EpiphanyModelReceipt::new(&request_id, "openai-codex", "gpt-test"); + let provider_receipt = EpiphanyOpenAiModelReceipt::new(&request_id, "gpt-test"); + let mut cache = runtime_spine_cache(store)?; + cache.pull_all_backing_stores()?; + cache.put( + &format!("{request_id}:00000000"), + &EpiphanyModelStreamEvent { + schema_id: MODEL_STREAM_EVENT_SCHEMA_VERSION.to_string(), + request_id: request_id.clone(), + provider: "openai-codex".to_string(), + sequence: 0, + payload: EpiphanyModelStreamPayload::Completed { + receipt: native_receipt.clone(), + }, + }, + )?; + cache.put( + &format!("{request_id}:00000000"), + &EpiphanyOpenAiStreamEvent { + schema_id: OPENAI_MODEL_STREAM_EVENT_SCHEMA_VERSION.to_string(), + request_id: request_id.clone(), + sequence: 0, + payload: EpiphanyOpenAiStreamPayload::Completed { + receipt: provider_receipt.clone(), + }, + }, + )?; + cache.put(&request_id, &native_receipt)?; + cache.put(&request_id, &provider_receipt)?; + complete_runtime_job( + store, + RuntimeSpineJobResultOptions { + result_id: format!("result-{suffix}"), + job_id, + completed_at: completed_at.to_string(), + verdict: "pass".to_string(), + summary: "Model generation completed.".to_string(), + next_safe_move: "Close the session.".to_string(), + evidence_refs: Vec::new(), + artifact_refs: Vec::new(), + }, + )?; + close_runtime_session( + store, + RuntimeSpineSessionClosureOptions { + session_id, + completed_at: completed_at.to_string(), + summary: "Model generation closed.".to_string(), + }, + )?; + Ok(()) + } + fn repo_model_bootstrap() -> crate::EpiphanyMemoryGraphSnapshot { crate::EpiphanyMemoryGraphSnapshot { schema_version: Some(crate::MEMORY_GRAPH_SCHEMA_VERSION.to_string()), @@ -12179,8 +12873,7 @@ pub(crate) mod tests { hostile_result.admitted_model_direction_consideration_request_id = None; hostile_result.admitted_model_direction_consideration_result_msgpack = None; let mut eyes_patch = hostile_patch.clone(); - let crate::RepoModelPatchOperation::UpsertFrontier { item } = - &mut eyes_patch.operations[0] + let crate::RepoModelPatchOperation::UpsertFrontier { item } = &mut eyes_patch.operations[0] else { panic!("autonomous fixture must upsert one frontier") }; @@ -14896,25 +15589,35 @@ pub(crate) mod tests { Some(provider_request.clone()) ); assert_eq!( - cache.get::("session-model")?.unwrap().status, + cache + .get::("session-model")? + .unwrap() + .status, EpiphanyRuntimeSessionStatus::Active ); assert_eq!( - cache.get::("job-model")?.unwrap().status, + cache + .get::("job-model")? + .unwrap() + .status, EpiphanyRuntimeJobStatus::Queued ); - assert!(cache - .get::("event-job-opened-job-model")? - .is_some()); - assert!(open_runtime_model_execution( - &store, - session_options.clone(), - job_options.clone(), - &model_request, - &provider_request, - "2026-08-10T00:00:04Z", - ) - .is_err()); + assert!( + cache + .get::("event-job-opened-job-model")? + .is_some() + ); + assert!( + open_runtime_model_execution( + &store, + session_options.clone(), + job_options.clone(), + &model_request, + &provider_request, + "2026-08-10T00:00:04Z", + ) + .is_err() + ); let foreign_provider = EpiphanyOpenAiModelRequest::new( "request-foreign", @@ -14931,32 +15634,40 @@ pub(crate) mod tests { ); let mut foreign_job_options = job_options; foreign_job_options.job_id = "job-foreign".to_string(); - assert!(open_runtime_model_execution( - &store, - session_options, - foreign_job_options, - &foreign_model, - &foreign_provider, - "2026-08-10T00:00:05Z", - ) - .is_err()); + assert!( + open_runtime_model_execution( + &store, + session_options, + foreign_job_options, + &foreign_model, + &foreign_provider, + "2026-08-10T00:00:05Z", + ) + .is_err() + ); let mut cache = runtime_spine_cache(&store)?; cache.pull_all_backing_stores()?; - assert!(cache - .get::("request-foreign")? - .is_none()); - assert!(cache - .get::("request-foreign")? - .is_none()); - assert!(cache - .get::("request-foreign")? - .is_none()); - assert!(cache - .get::("job-foreign")? - .is_none()); - assert!(cache - .get::("event-job-opened-job-foreign")? - .is_none()); + assert!( + cache + .get::("request-foreign")? + .is_none() + ); + assert!( + cache + .get::("request-foreign")? + .is_none() + ); + assert!( + cache + .get::("request-foreign")? + .is_none() + ); + assert!(cache.get::("job-foreign")?.is_none()); + assert!( + cache + .get::("event-job-opened-job-foreign")? + .is_none() + ); Ok(()) } @@ -15041,24 +15752,28 @@ pub(crate) mod tests { "2026-08-10T02:00:05Z", ) .with_model_call("call-2", "request-missing"); - assert!(put_runtime_tool_execution_intent( - &store, - "session-tools", - "job-tools", - &hostile_intent, - "2026-08-10T02:00:05Z", - ) - .is_err()); + assert!( + put_runtime_tool_execution_intent( + &store, + "session-tools", + "job-tools", + &hostile_intent, + "2026-08-10T02:00:05Z", + ) + .is_err() + ); let mut cache = runtime_spine_cache(&store)?; cache.pull_all_backing_stores()?; - assert!(cache - .get::("intent-hostile")? - .is_none()); - assert!(cache - .get::(&tool_invocation_intent_key( - "intent-hostile" - ))? - .is_none()); + assert!( + cache + .get::("intent-hostile")? + .is_none() + ); + assert!( + cache + .get::(&tool_invocation_intent_key("intent-hostile"))? + .is_none() + ); create_runtime_job( &store, @@ -15116,15 +15831,340 @@ pub(crate) mod tests { assert!(put_runtime_tool_execution_receipt(&store, &hostile_receipt).is_err()); let mut cache = runtime_spine_cache(&store)?; cache.pull_all_backing_stores()?; - assert!(cache - .get::(&tool_invocation_receipt_key( - "intent-model-tool" - ))? - .is_none()); + assert!( + cache + .get::(&tool_invocation_receipt_key( + "intent-model-tool" + ))? + .is_none() + ); assert!(require_runtime_tool_execution_binding(&store, "unbound").is_err()); Ok(()) } + #[test] + fn archived_model_session_is_exact_terminal_tombstone() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("runtime.msgpack"); + initialize_runtime_spine( + &store, + RuntimeSpineInitOptions { + runtime_id: "epiphany-test".to_string(), + display_name: "Epiphany Test".to_string(), + created_at: "2026-08-10T03:00:00Z".to_string(), + }, + )?; + append_runtime_event( + &store, + RuntimeSpineEventOptions { + event_id: "event-unrelated".to_string(), + occurred_at: "2026-08-10T03:00:00Z".to_string(), + event_type: "semantic.observation".to_string(), + source: "test".to_string(), + session_id: None, + job_id: None, + summary: "Unrelated retained runtime evidence.".to_string(), + }, + )?; + let session_options = RuntimeSpineSessionOptions { + session_id: "session-archive".to_string(), + objective: "Archive one closed model generation.".to_string(), + created_at: "2026-08-10T03:00:01Z".to_string(), + coordinator_note: "test".to_string(), + }; + let job_options = RuntimeSpineJobOptions { + job_id: "job-archive".to_string(), + session_id: "session-archive".to_string(), + role: "openai-model-adapter".to_string(), + created_at: "2026-08-10T03:00:02Z".to_string(), + summary: "Archive model job.".to_string(), + artifact_refs: Vec::new(), + }; + let model_request = EpiphanyModelRequest::new( + "request-archive", + "conversation-archive", + "openai-codex", + "gpt-test", + "Return one terminal result.", + ); + let provider_request = EpiphanyOpenAiModelRequest::new( + "request-archive", + "conversation-archive", + "gpt-test", + "Return one terminal result.", + ); + open_runtime_model_execution( + &store, + session_options.clone(), + job_options.clone(), + &model_request, + &provider_request, + "2026-08-10T03:00:03Z", + )?; + let tool_intent = EpiphanyToolInvocationIntent::new( + "intent-archive", + "epiphany-tool-runtime", + "source", + "read", + "{}", + "model-runtime", + "Read one source.", + "2026-08-10T03:00:04Z", + ) + .with_model_call("call-archive", "request-archive"); + put_runtime_tool_execution_intent( + &store, + "session-archive", + "job-archive", + &tool_intent, + "2026-08-10T03:00:04Z", + )?; + let native_receipt = + EpiphanyModelReceipt::new("request-archive", "openai-codex", "gpt-test"); + let provider_receipt = EpiphanyOpenAiModelReceipt::new("request-archive", "gpt-test"); + let native_event = EpiphanyModelStreamEvent { + schema_id: MODEL_STREAM_EVENT_SCHEMA_VERSION.to_string(), + request_id: "request-archive".to_string(), + provider: "openai-codex".to_string(), + sequence: 0, + payload: EpiphanyModelStreamPayload::Completed { + receipt: native_receipt.clone(), + }, + }; + let provider_event = EpiphanyOpenAiStreamEvent { + schema_id: OPENAI_MODEL_STREAM_EVENT_SCHEMA_VERSION.to_string(), + request_id: "request-archive".to_string(), + sequence: 0, + payload: EpiphanyOpenAiStreamPayload::Completed { + receipt: provider_receipt.clone(), + }, + }; + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + cache.put("request-archive:00000000", &native_event)?; + cache.put("request-archive:00000000", &provider_event)?; + cache.put("request-archive", &native_receipt)?; + cache.put("request-archive", &provider_receipt)?; + complete_runtime_job( + &store, + RuntimeSpineJobResultOptions { + result_id: "result-archive".to_string(), + job_id: "job-archive".to_string(), + completed_at: "2026-08-10T03:00:05Z".to_string(), + verdict: "pass".to_string(), + summary: "Model generation completed.".to_string(), + next_safe_move: "Close the session.".to_string(), + evidence_refs: Vec::new(), + artifact_refs: Vec::new(), + }, + )?; + close_runtime_session( + &store, + RuntimeSpineSessionClosureOptions { + session_id: "session-archive".to_string(), + completed_at: "2026-08-10T03:00:06Z".to_string(), + summary: "Model generation closed.".to_string(), + }, + )?; + assert!( + archive_completed_model_session(&store, "session-archive", "2026-08-10T03:00:07Z") + .is_err() + ); + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + assert!( + cache + .get::("session-archive")? + .is_some() + ); + + let tool_receipt = EpiphanyToolInvocationReceipt::new( + "receipt-archive", + "intent-archive", + "epiphany-tool-runtime", + "source", + "read", + "completed", + "unix-ms:3", + ); + put_runtime_tool_execution_receipt(&store, &tool_receipt)?; + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + let identity_before = cache + .get_envelope::(RUNTIME_IDENTITY_KEY)? + .expect("identity envelope"); + let unrelated_before = cache + .get_envelope::("event-unrelated")? + .expect("unrelated event envelope"); + assert!( + archive_completed_model_session_with_before_commit( + &store, + "session-archive", + "2026-08-10T03:00:07Z", + || { + append_runtime_event( + &store, + RuntimeSpineEventOptions { + event_id: "event-archive-race".to_string(), + occurred_at: "2026-08-10T03:00:07Z".to_string(), + event_type: "concurrent.observation".to_string(), + source: "test".to_string(), + session_id: None, + job_id: None, + summary: "Invalidate the archive snapshot.".to_string(), + }, + )?; + Ok(()) + } + ) + .is_err() + ); + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + assert!( + cache + .get::("session-archive")? + .is_none() + ); + assert!( + cache + .get::("session-archive")? + .is_some() + ); + let race_before = cache + .get_envelope::("event-archive-race")? + .expect("concurrent event envelope"); + let archive = + archive_completed_model_session(&store, "session-archive", "2026-08-10T03:00:07Z")?; + assert_eq!(archive.job_ids, vec!["job-archive"]); + assert_eq!(archive.job_result_ids, vec!["result-archive"]); + assert_eq!(archive.model_request_ids, vec!["request-archive"]); + assert_eq!(archive.tool_intent_ids, vec!["intent-archive"]); + assert!(archive.retired_envelope_count >= 15); + assert!(archive.retired_chain_digest.starts_with("sha256:")); + assert_eq!( + archive_completed_model_session(&store, "session-archive", "2026-08-10T03:00:08Z")?, + archive + ); + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + assert_eq!( + cache + .get_envelope::(RUNTIME_IDENTITY_KEY)? + .expect("identity envelope"), + identity_before + ); + assert_eq!( + cache + .get_envelope::("event-unrelated")? + .expect("unrelated event envelope"), + unrelated_before + ); + assert_eq!( + cache + .get_envelope::("event-archive-race")? + .expect("concurrent event envelope"), + race_before + ); + assert!( + cache + .get::("session-archive")? + .is_none() + ); + assert!(cache.get::("job-archive")?.is_none()); + assert!( + cache + .get::("request-archive")? + .is_none() + ); + assert!( + cache + .get::(&tool_invocation_intent_key("intent-archive"))? + .is_none() + ); + assert!(create_runtime_session(&store, session_options).is_err()); + assert!( + create_runtime_session( + &store, + RuntimeSpineSessionOptions { + session_id: "session-other".to_string(), + objective: "Try archived IDs.".to_string(), + created_at: "2026-08-10T03:00:09Z".to_string(), + coordinator_note: "test".to_string(), + } + ) + .is_ok() + ); + assert!( + create_runtime_job( + &store, + RuntimeSpineJobOptions { + session_id: "session-other".to_string(), + ..job_options + } + ) + .is_err() + ); + assert_eq!(runtime_spine_status(&store)?.pending_tool_invocations, 0); + Ok(()) + } + + #[test] + fn completed_model_session_retention_keeps_newest_generation() -> Result<()> { + let temp = tempdir()?; + let store = temp.path().join("runtime.msgpack"); + initialize_runtime_spine( + &store, + RuntimeSpineInitOptions { + runtime_id: "epiphany-test".to_string(), + display_name: "Epiphany Test".to_string(), + created_at: "2026-08-10T04:00:00Z".to_string(), + }, + )?; + close_test_model_generation( + &store, + "older", + "2026-08-10T04:00:01Z", + "2026-08-10T04:00:02Z", + )?; + close_test_model_generation( + &store, + "newer", + "2026-08-10T04:00:03Z", + "2026-08-10T04:00:04Z", + )?; + + let archived = retain_completed_model_sessions(&store, 1, "2026-08-10T04:00:05Z")?; + assert_eq!(archived.len(), 1); + assert_eq!(archived[0].session_id, "session-older"); + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + assert!( + cache + .get::("session-older")? + .is_none() + ); + assert!( + cache + .get::("session-older")? + .is_some() + ); + assert!( + cache + .get::("session-newer")? + .is_some() + ); + assert!(retain_completed_model_sessions(&store, 0, "2026-08-10T04:00:06Z")?.is_empty()); + let mut cache = runtime_spine_cache(&store)?; + cache.pull_all_backing_stores()?; + assert!( + cache + .get::("session-newer")? + .is_some() + ); + Ok(()) + } + #[test] fn runtime_spine_opens_and_completes_native_jobs() -> Result<()> { let temp = tempdir()?; diff --git a/notes/epiphany-current-algorithmic-map.md b/notes/epiphany-current-algorithmic-map.md index a7489b7a..de71fb29 100644 --- a/notes/epiphany-current-algorithmic-map.md +++ b/notes/epiphany-current-algorithmic-map.md @@ -2438,6 +2438,26 @@ semantic/Mind/consequence families remain untouched. The tombstone must retain the exact session, job/result, model-request, and tool-intent identities as an ID-reuse barrier plus counts and a digest of bulky retired envelopes. +That narrow owner now exists in source as +`EpiphanyArchivedRuntimeSession` and `archive_completed_model_session`. +Runtime spine admits only the closed model-generation shape above, verifies the +native/provider terminal streams and receipts plus every bound tool receipt, +and refuses outer worker/coordinator membership. One full-snapshot-fenced +replacement writes the exact-ID tombstone and deletes only its enumerated inner +execution envelopes. Tombstone IDs block later session, job, model-request, or +tool-intent aliasing; the tombstone itself is never enumerated as runnable +authority. Stale-snapshot, missing-tool-receipt, byte-identity, idempotence, and +ID-reuse negatives pass. + +`retain_completed_model_sessions` selects only completed generations whose +jobs are all model-adapter jobs with explicit model bindings, orders them by +session closure time, and keeps at least the newest even when configured zero. +Resident Self invokes it beside coordinator-receipt retention only after the +shared no-active/no-prepared authority gate. The default retained window is +256. A two-generation test proves the older generation becomes a tombstone and +the newest remains live. Exact package and copied-state proof remain required; +conversation-wide sessions and semantic history remain separate owners. + The coordinator receipt accumulator is implemented in source. Runtime owns the typed head, cumulative status counts, chained digest, and full-snapshot-fenced replacement/deletion. Resident Self supplies the cross-store preservation set diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 11151da6..44e12079 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -1512,3 +1512,29 @@ Six OpenAI CLI and 15 library tests pass, including terminal receipt/session assertions. The next cut is the narrow archived model-session tombstone; it must retain exact IDs and delete only inner execution envelopes, never outer worker or semantic authority. + +## Narrow model-generation archive — source-proven 2026-08-10 + +Runtime spine now owns `EpiphanyArchivedRuntimeSession`. Archive admission is +deliberately narrow: the session is Completed; every member job is an +`openai-model-adapter` job with exactly one model-execution binding and terminal +job result; native and OpenAI requests, streams, and receipts agree and are +terminal; and every bound tool intent has one authenticated terminal receipt. +Outer worker launches/results, coordinator receipts, semantic/Mind/consequence +documents, unknown rows, and unrelated runtime envelopes are not members. + +One full-snapshot-fenced replacement writes a tombstone containing the exact +session/job/result/request/intent IDs, terminal counts, retired-family counts, +and chained envelope digest while deleting only that inner execution family. +Those retained IDs are the permanent reuse barrier. A stale snapshot refuses; +an unreceipted tool refuses; exact replay is idempotent; and unrelated envelopes +remain byte-identical. Resident Self calls the bounded owner only while neither +active nor prepared launch authority exists. The configurable default window +is 256 and at least the newest completed model generation always survives. + +The exact archive test and two-generation retention-window test pass, as do all +eight `epiphany-swarm` tests and `cargo check`. The schema and CultNet catalog +parse. This cut is not yet committed, packaged, or exercised on copied live +state. Next commit/push it, package only that exact commit with Starfire's +persistent Linux cache, then prove tombstone non-resurrection and byte identity +on copied state. Active c005 remains untouchable. Yggdrasil never builds. diff --git a/schemas/cultnet/epiphany.runtime.archived-session.schema.json b/schemas/cultnet/epiphany.runtime.archived-session.schema.json new file mode 100644 index 00000000..27465dd2 --- /dev/null +++ b/schemas/cultnet/epiphany.runtime.archived-session.schema.json @@ -0,0 +1,36 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://gamecult.dev/epiphany/cultnet/epiphany.runtime.archived-session.schema.json", + "title": "Epiphany Archived Runtime Session", + "description": "Permanent tombstone and evidence accumulator for one fully closed model-session execution generation.", + "type": "object", + "additionalProperties": false, + "properties": { + "schemaVersion": { "type": "string", "const": "epiphany.runtime.archived_session.v0" }, + "archiveId": { "type": "string", "minLength": 1 }, + "sessionId": { "type": "string", "minLength": 1 }, + "archivedAt": { "type": "string", "format": "date-time" }, + "jobIds": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }, + "jobResultIds": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }, + "modelRequestIds": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }, + "toolIntentIds": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true }, + "terminalJobStatusCounts": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } }, + "retiredTypeCounts": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } }, + "retiredEnvelopeCount": { "type": "integer", "minimum": 1 }, + "retiredChainDigest": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" } + }, + "required": [ + "schemaVersion", + "archiveId", + "sessionId", + "archivedAt", + "jobIds", + "jobResultIds", + "modelRequestIds", + "toolIntentIds", + "terminalJobStatusCounts", + "retiredTypeCounts", + "retiredEnvelopeCount", + "retiredChainDigest" + ] +} diff --git a/schemas/cultnet/index.json b/schemas/cultnet/index.json index d21770e4..14f7b4d8 100644 --- a/schemas/cultnet/index.json +++ b/schemas/cultnet/index.json @@ -59,6 +59,17 @@ "title": "Epiphany Runtime Tool Execution Binding", "path": "epiphany.runtime.tool-execution-binding.schema.json" }, + { + "schemaId": "https://gamecult.dev/epiphany/cultnet/epiphany.runtime.archived-session.schema.json", + "kind": "document_payload", + "wireContracts": [ + "cultnet.schema.v0" + ], + "schemaVersion": "epiphany.runtime.archived_session.v0", + "documentType": "epiphany.runtime.archived_session", + "title": "Epiphany Archived Runtime Session", + "path": "epiphany.runtime.archived-session.schema.json" + }, { "schemaId": "https://gamecult.dev/epiphany/cultnet/epiphany.runtime.job.schema.json", "kind": "document_payload", diff --git a/state/map.yaml b/state/map.yaml index aa6dd0eb..a975bb2b 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -35,10 +35,10 @@ invariants: - Evidence and git retain history; this map retains only state that changes future decisions. current_status: - summary: Epiphany is a supervised engineering alpha on Starfire, not deployment-ready. Exact b8138a6b active-child SIGTERM and restart, exact 9f369528 deterministic warm packaging, exact c930f505 cross-container ownership, exact ed15f83a resident lifecycle retention, and exact 16c4b19f coordinator receipt and Heartbeat artifact retention are proven. Gamecult-ops 059821f makes journald the sole Yggdrasil supervisor/projector stream-retention owner. Exact 16c4b19f genuine idle and exact a987d2f7 active cognition both completed 900-pulse bounded plateaus with clean direct-PID exits. The active run proves restart-persistent cooldown, exact-grant terminality under deliberate missing-ack and marker-regression corruption, multiple later terminal grants without resurrection, bounded artifact retention, and flat terminal resource state. Runtime-owned typed fulfillment is now accepted live: c1b461b2 falsified mutable Body/admission coupling, c9738fdb repaired the owner split, and exact 6dd9b132 packaging completed one autonomous Eyes continuation through coordinator exit, awaiting-fulfillment, one authenticated result, one planned acknowledgement, grant terminality, and clean shutdown. All 50 runtime-spine and 24 resident focused tests pass. Broad session/model/event/tool retention and stable Starfire firewall ownership remain open. Yggdrasil is the small public crossing body, never the release forge. - latest_phase: Exact 6dd9b132 fulfillment remains accepted. Runtime spine owns atomic model/tool execution membership and immutable tool receipts. Retained-reference inspection then falsified the proposed archive unit: worker model sessions were role-long and could never close while future workers remained possible. Worker sessions now derive from exact outer job_id, contain that worker's initial/followup model jobs, and close on success. Loop-stall/round-limit paths terminalize unexecuted intents, fail the outer worker, and close the unique inner session. Six OpenAI CLI and 15 library tests pass. Semantic/outer-worker families remain outside the archive unit; no deletion exists yet. + summary: Epiphany is a supervised engineering alpha on Starfire, not deployment-ready. Exact b8138a6b active-child SIGTERM and restart, exact 9f369528 deterministic warm packaging, exact c930f505 cross-container ownership, exact ed15f83a resident lifecycle retention, and exact 16c4b19f coordinator receipt and Heartbeat artifact retention are proven. Gamecult-ops 059821f makes journald the sole Yggdrasil supervisor/projector stream-retention owner. Exact 16c4b19f genuine idle and exact a987d2f7 active cognition both completed 900-pulse bounded plateaus with clean direct-PID exits. Runtime-owned typed fulfillment is accepted live under exact 6dd9b132. Runtime spine now owns source-proven model/tool execution membership and a narrow completed-model-session tombstone; exact packaging and copied-state archive proof remain open. Stable Starfire firewall ownership also remains open. Yggdrasil is the small public crossing body, never the release forge. + latest_phase: The narrow runtime archive owner is implemented in source. A completed model-generation session can retire only when every model-adapter job has one binding and terminal result, native/provider streams agree and are terminal, and every bound tool intent has an immutable terminal receipt. One full-snapshot transaction replaces those exact inner execution envelopes with an ID-bearing digest tombstone; outer worker, semantic, coordinator, unknown, and unrelated runtime rows are excluded. Resident Self invokes bounded retention only with no active or prepared launch and keeps at least the newest generation. Focused archive, retention-window, and all eight swarm tests pass; packaging/live proof is next. operational_state: - local_worktree: coordinator_receipt_retention_packaged_proven + local_worktree: archived_model_session_source_proven_uncommitted operator_wireguard: restored yggdrasil: canonical_small_public_crossing_body starfire: temporary_epiphany_cognition_body @@ -68,8 +68,8 @@ current_status: starfire_linux_modeling_peak: 109887488_bytes_resident_body_plus_modeling_circuit yggdrasil_epiphany_service_contract: repaired_in_gamecult_ops_812b427_not_installed resident_physiology: authenticated_9b8df0b4_c005_bound_to_eyes_tools_nervous_system_with_217dd_and_bd96305e_helpers_running - unresolved_authority: Coordinator receipt, Heartbeat pulse-artifact, deployed supervisor/projector log retention, genuine-idle and active resource plateaus, exact-grant terminality, restart-persistent cooldown refusal, schedule/action issuance cardinality, and runtime-owned typed fulfillment are accepted. Broader semantic/runtime session, job, event, model, and tool histories remain unbounded pending a typed archived-session owner. Repository Body observation still performs two authenticated full-tree Git index/manifest passes. Execute-mode workers require a persistent process body. Keyed storage has not earned live migration. One Discord consequence remains blocked by the absent Yggdrasil credential. - next_action: Implement the narrow archived model-session tombstone. Admit only a Completed session containing model-adapter jobs where every job has one explicit model binding and terminal result, every native/provider stream is terminal, and every tool binding has an immutable receipt. Retain exact session/job/result/request/intent IDs plus counts and chained digest as an ID-reuse barrier; delete only exact inner session/job/event/model/tool envelopes under a full-snapshot fence. Refuse legacy unbound rows, nonterminal families, stale snapshots, semantic/outer-worker deletion, and session recreation. Preserve active c005. Yggdrasil never builds. + unresolved_authority: Coordinator receipt, Heartbeat pulse-artifact, deployed supervisor/projector log retention, genuine-idle and active resource plateaus, exact-grant terminality, restart-persistent cooldown refusal, schedule/action issuance cardinality, and runtime-owned typed fulfillment are accepted. Narrow model-generation retention is source-proven but not yet packaged or exercised against copied live state; semantic and conversation-wide runtime histories remain outside its authority. Repository Body observation still performs two authenticated full-tree Git index/manifest passes. Execute-mode workers require a persistent process body. Keyed storage has not earned live migration. One Discord consequence remains blocked by the absent Yggdrasil credential. + next_action: Commit and push the narrow archived-model-session owner, package that exact commit using Starfire's persistent Linux cache, authenticate all 24 binaries and witness, then exercise bounded retention only on copied state. Prove the older fully closed generation becomes one exact tombstone, the newest survives, unrelated/outer/semantic envelopes remain byte-identical, ID reuse and work resurrection fail, and replay is stable. Preserve active c005. Yggdrasil never builds. active_subgoals: - Prove Persona, Self, Eyes, Modeling, Imagination, Hands, Soul, Mind, Continuity, and nervous-system behavior individually.