From 23474b1e35a97fd603405681960601516e73ce07 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 15 Jul 2026 21:24:58 +0100 Subject: [PATCH] Bind runtimes to one repository Body store --- epiphany-core/src/repository_body_observer.rs | 191 +++++++++++++++++- epiphany-core/src/runtime_spine.rs | 1 + ...onceptual-substitution-audit-2026-07-12.md | 4 + notes/epiphany-current-algorithmic-map.md | 8 + notes/fresh-workspace-handoff.md | 5 + state/ledgers.msgpack | Bin 511313 -> 511883 bytes state/map.yaml | 2 +- 7 files changed, 200 insertions(+), 11 deletions(-) diff --git a/epiphany-core/src/repository_body_observer.rs b/epiphany-core/src/repository_body_observer.rs index ef2803bd..f8864d21 100644 --- a/epiphany-core/src/repository_body_observer.rs +++ b/epiphany-core/src/repository_body_observer.rs @@ -17,8 +17,10 @@ pub const BODY_BINDING_TYPE: &str = "epiphany.repository_body.binding"; pub const BODY_OBSERVATION_TYPE: &str = "epiphany.repository_body.observation"; pub const BODY_HEAD_TYPE: &str = "epiphany.repository_body.head"; pub const BODY_MANIFEST_TYPE: &str = "epiphany.repository_body.manifest"; +pub const RUNTIME_BODY_STORE_BINDING_TYPE: &str = "epiphany.runtime.repository_body_store_binding"; pub const BODY_BINDING_KEY: &str = "binding"; pub const BODY_HEAD_KEY: &str = "current"; +pub const RUNTIME_BODY_STORE_BINDING_KEY: &str = "repository-body-store"; pub const BODY_SCHEMA_VERSION: &str = "epiphany.repository_body.v2"; #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] @@ -51,6 +53,28 @@ pub struct RepositoryBodyBinding { pub global_excludes_policy: String, } +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "epiphany.runtime.repository_body_store_binding", + schema = "RuntimeRepositoryBodyStoreBinding" +)] +pub struct RuntimeRepositoryBodyStoreBinding { + #[cultcache(key = 0)] + pub schema_version: String, + #[cultcache(key = 1)] + pub binding_id: String, + #[cultcache(key = 2)] + pub runtime_id: String, + #[cultcache(key = 3)] + pub swarm_id: String, + #[cultcache(key = 4)] + pub workspace_id: String, + #[cultcache(key = 5)] + pub body_store_path: String, + #[cultcache(key = 6)] + pub body_binding_sha256: String, +} + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "epiphany.repository_body.observation", @@ -181,19 +205,38 @@ pub fn bind_repository_body( }; let opening = load_body_envelopes(store)?; validate_binding(&opening, &binding)?; - if find(&opening, BODY_BINDING_TYPE, BODY_BINDING_KEY).is_some() { - return Ok(binding); - } - if !SingleFileMessagePackBackingStore::new(store).insert_entry_if_absent(envelope( - BODY_BINDING_TYPE, - BODY_BINDING_KEY, - &binding, - )?)? { + if find(&opening, BODY_BINDING_TYPE, BODY_BINDING_KEY).is_none() + && !SingleFileMessagePackBackingStore::new(store).insert_entry_if_absent(envelope( + BODY_BINDING_TYPE, + BODY_BINDING_KEY, + &binding, + )?)? + { bail!("repository Body immutable binding lost insert race; reload before retrying"); } + bind_runtime_body_store(runtime_store, store, &binding)?; Ok(binding) } +pub fn runtime_repository_body_store_binding( + runtime_store: &Path, +) -> Result> { + if !runtime_store.exists() { + return Ok(None); + } + let entries = SingleFileMessagePackBackingStore::new(runtime_store).pull_all()?; + let Some(env) = find( + &entries, + RUNTIME_BODY_STORE_BINDING_TYPE, + RUNTIME_BODY_STORE_BINDING_KEY, + ) else { + return Ok(None); + }; + let binding: RuntimeRepositoryBodyStoreBinding = decode(env)?; + validate_runtime_body_store_binding(runtime_store, &binding)?; + Ok(Some(binding)) +} + pub fn observe_repository_body( repo: &Path, store: &Path, @@ -750,6 +793,93 @@ fn validate_stored_binding(binding: &RepositoryBodyBinding) -> Result<()> { } Ok(()) } +fn bind_runtime_body_store( + runtime_store: &Path, + body_store: &Path, + body: &RepositoryBodyBinding, +) -> Result { + let runtime = load_valid_runtime_binding(runtime_store)?; + require_runtime_matches(body, &runtime)?; + let body_store_path = std::fs::canonicalize(body_store) + .context("failed to canonicalize bound repository Body store")? + .to_string_lossy() + .into_owned(); + let binding = RuntimeRepositoryBodyStoreBinding { + schema_version: BODY_SCHEMA_VERSION.into(), + binding_id: RUNTIME_BODY_STORE_BINDING_KEY.into(), + runtime_id: runtime.runtime_id, + swarm_id: runtime.swarm_id, + workspace_id: body.workspace_id.clone(), + body_store_path, + body_binding_sha256: body_binding_sha256(body)?, + }; + let backing = SingleFileMessagePackBackingStore::new(runtime_store); + let opening = backing.pull_all()?; + if let Some(env) = find( + &opening, + RUNTIME_BODY_STORE_BINDING_TYPE, + RUNTIME_BODY_STORE_BINDING_KEY, + ) { + let existing: RuntimeRepositoryBodyStoreBinding = decode(env)?; + if existing != binding { + bail!("runtime repository Body-store immutable binding collision"); + } + validate_runtime_body_store_binding(runtime_store, &existing)?; + return Ok(existing); + } + if !backing.insert_entry_if_absent(envelope( + RUNTIME_BODY_STORE_BINDING_TYPE, + RUNTIME_BODY_STORE_BINDING_KEY, + &binding, + )?)? { + bail!("runtime repository Body-store binding lost insert race; reload before retrying"); + } + validate_runtime_body_store_binding(runtime_store, &binding)?; + Ok(binding) +} +fn validate_runtime_body_store_binding( + runtime_store: &Path, + binding: &RuntimeRepositoryBodyStoreBinding, +) -> Result<()> { + if binding.schema_version != BODY_SCHEMA_VERSION + || binding.binding_id != RUNTIME_BODY_STORE_BINDING_KEY + || binding.runtime_id.trim().is_empty() + || binding.swarm_id.trim().is_empty() + || binding.workspace_id.trim().is_empty() + || binding.body_store_path.trim().is_empty() + || binding.body_binding_sha256.trim().is_empty() + { + bail!("runtime repository Body-store binding is invalid"); + } + let runtime = load_valid_runtime_binding(runtime_store)?; + if binding.runtime_id != runtime.runtime_id || binding.swarm_id != runtime.swarm_id { + bail!("runtime repository Body-store binding disagrees with runtime identity"); + } + let body_store = PathBuf::from(&binding.body_store_path); + let canonical = + std::fs::canonicalize(&body_store).context("runtime repository Body store is missing")?; + if canonical.to_string_lossy() != binding.body_store_path { + bail!("runtime repository Body-store locator is not canonical"); + } + let entries = load_body_envelopes(&body_store)?; + let env = find(&entries, BODY_BINDING_TYPE, BODY_BINDING_KEY) + .ok_or_else(|| anyhow!("runtime repository Body store has no Body binding"))?; + let body: RepositoryBodyBinding = decode(env)?; + validate_stored_binding(&body)?; + require_runtime_matches(&body, &runtime)?; + if body.workspace_id != binding.workspace_id + || body_binding_sha256(&body)? != binding.body_binding_sha256 + { + bail!("runtime repository Body-store binding hash collision"); + } + Ok(()) +} +fn body_binding_sha256(binding: &RepositoryBodyBinding) -> Result { + Ok(format!( + "{:x}", + Sha256::digest(rmp_serde::to_vec_named(binding)?) + )) +} fn validate_body_chain( entries: &[CultCacheEnvelope], binding: &RepositoryBodyBinding, @@ -933,6 +1063,13 @@ mod tests { crate::ensure_agent_memory_swarm_identity(&agents, swarm_id)?; crate::bind_runtime_to_agent_memory_swarm(&runtime, &agents, "2026-07-15T00:00:01Z")?; bind_repository_body(repo, &store, &runtime, workspace)?; + let route = runtime_repository_body_store_binding(&runtime)? + .ok_or_else(|| anyhow!("runtime lost repository Body-store binding"))?; + assert_eq!(route.workspace_id, workspace); + assert_eq!( + PathBuf::from(route.body_store_path), + std::fs::canonicalize(&store)? + ); Ok((store, runtime)) } #[test] @@ -1036,6 +1173,34 @@ mod tests { Ok(()) } #[test] + fn runtime_body_store_route_is_immutable_and_validated() -> Result<()> { + let d = repo()?; + let state = tempfile::tempdir()?; + let (store, runtime) = bound(d.path(), state.path(), "w", "r", "s")?; + let route = runtime_repository_body_store_binding(&runtime)?.unwrap(); + let second = state.path().join("second-body.cc"); + assert!( + bind_repository_body(d.path(), &second, &runtime, "w") + .unwrap_err() + .to_string() + .contains("immutable binding collision") + ); + let moved = state.path().join("moved-body.cc"); + std::fs::rename(&store, &moved)?; + assert!( + runtime_repository_body_store_binding(&runtime) + .unwrap_err() + .to_string() + .contains("Body store is missing") + ); + std::fs::rename(&moved, &store)?; + assert_eq!( + runtime_repository_body_store_binding(&runtime)?.unwrap(), + route + ); + Ok(()) + } + #[test] fn stale_head_cannot_overwrite() -> Result<()> { let d = repo()?; let state = tempfile::tempdir()?; @@ -1103,8 +1268,14 @@ mod tests { .contains("outside") ); assert!(!d.path().join("body.cc").exists()); - let store = state.path().join("corrupt-body.cc"); - bind_repository_body(d.path(), &store, &runtime, "corrupt")?; + let corrupt_state = tempfile::tempdir()?; + let (store, runtime) = bound( + d.path(), + corrupt_state.path(), + "corrupt", + "corrupt-runtime", + "corrupt-swarm", + )?; write(&d.path().join(".git").join("HEAD"), "not-a-ref")?; assert!(observe_repository_body(d.path(), &store, &runtime).is_err()); assert!(load_repository_body_status(&store)?.is_none()); diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index 84f46051..8a4660a9 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -821,6 +821,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::()?; diff --git a/notes/conceptual-substitution-audit-2026-07-12.md b/notes/conceptual-substitution-audit-2026-07-12.md index 8a32b957..81394ce4 100644 --- a/notes/conceptual-substitution-audit-2026-07-12.md +++ b/notes/conceptual-substitution-audit-2026-07-12.md @@ -1844,3 +1844,7 @@ identity is a domain-separated raw-content manifest root over workspace/scope/ policy plus ordered UTF-8 paths, modes/kinds, byte lengths, raw SHA-256 hashes, non-followed symlink-target hashes, and nonrecursive gitlink OIDs. Manifest and manifest-root head share the observation CAS. +The runtime now holds one immutable route to that external store, bound by +canonical locator plus exact Body-binding hash and revalidated on read. The route +does not substitute for a pre-thought Body observation basis or model grounding; +it merely prevents later callers from choosing whichever store flatters them. diff --git a/notes/epiphany-current-algorithmic-map.md b/notes/epiphany-current-algorithmic-map.md index e7c1be6f..6c4ed260 100644 --- a/notes/epiphany-current-algorithmic-map.md +++ b/notes/epiphany-current-algorithmic-map.md @@ -792,6 +792,14 @@ SHA-256 root over workspace, scope policy, and those entries is authoritative Body identity. Manifest, observation, and manifest-root head land in one CAS. Gitlinks remain nonrecursive. Unsafe or unrepresentable paths fail closed. +The bind rite also writes one immutable runtime-side +`RuntimeRepositoryBodyStoreBinding`. It names the canonical external Body-store +locator and hashes the exact Body binding while repeating runtime, swarm, and +workspace identity. Every read reopens the Body store and validates that chain. +One runtime cannot silently switch between Body stores; relocation or replacement +requires a future explicit migration receipt. This is routing authority only. It +does not claim that a Modeling launch, result, or admission is grounded yet. + ## Mind readiness join boundary (2026-07-15) A truthful whole-repository readiness join does not yet exist. Mind must own a diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 9ae86d8e..6e9f017c 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -1652,6 +1652,11 @@ clean-filtered tree OID is auxiliary. Raw file bytes (or non-followed symlink target bytes) feed an ordered manifest whose domain-separated SHA-256 root is the authoritative Body identity. Manifest, observation, and manifest-root head commit atomically. Gitlinks are nonrecursive; unrepresentable paths fail closed. +Bind now installs one immutable runtime-side Body-store route containing the +canonical external locator and exact Body-binding hash. Reads validate runtime, +swarm, workspace, path, and Body binding; a runtime cannot substitute a second +Body store. The route is the missing nerve needed by launch/admission, not proof +that any current RepoModel was grounded through it. ## Readiness join remains deliberately impossible (2026-07-15) diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 762080eed61102cbe116f45ec6fb093b24a0b020..d8e0d44e969df83dd57fdd4a1d865d228aeb4391 100644 GIT binary patch delta 557 zcmYLGv2GJV5ao^}mPnTZAt98JC}@1Pb2yNME)sM!NtA+aeK*dn*xNaF_I$Wvf5Vr{s|GDLAUVwr60`O%RN-Ip#e$!irdXa#LuhM%w0JsR3=-D`ObQBIvTL zy&4&W73(c5ppFHPhk^|J+Jc*{c5|f#-DqDxrYu>VDFgW4qqEX*fPtY*Nz;{kmHAdh z5>y1PLUGXrkqQngR34>SQun!0uIxAD-g2cfk(i_S_0t}_oZw9CI8*^f6a2&6A}bu173H5*FDOS88Sv(=TZv``aOgvC;-ILmWapDYWF&$evZm zuOu}y?MjdyUQ>=Hm^qWj)>aQr(dn@kww)o2DXWDAXvE=X9Ed{CU3j{`zxe;V^JsB# Oxv{?3``LLC^=<%uK*_WK delta 61 zcmeC4FMn~C{00d|ZbNe`Q-i2u4D4t0HY+n0GBO@*p32ldm5C9EK{PWEvj8zG5VLKc J%EZ3s6aZiy6P*A6 diff --git a/state/map.yaml b/state/map.yaml index e79b0a47..44b4ae86 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -1,6 +1,6 @@ experiment: name: EpiphanyAgent - repository_body_observer: "Native Rust explicitly binds caller workspace ID to an existing validated runtime/swarm/source identity plus canonical Git root/object format/scope/policy. Two equal sanitized temporary-index scans enumerate ignore-aware UTF-8 paths, Git modes, and nonrecursive gitlinks with global excludes disabled; raw regular bytes/lengths and non-followed symlink-target bytes are SHA-256 hashed. The domain-separated ordered raw manifest root, not the clean-filtered auxiliary tree OID, is authoritative Body identity. Every Git subprocess strips ambient repository/worktree/object/ref/index/namespace/replacement/graft/shallow and config-injection authority. Observe cannot mint identity. CultCache MessagePack atomically commits immutable manifest+observation and a manifest-root head by exact CAS. Canonical store-path checks precede writes; corrupt HEAD, sparse checkout, unsafe/unrepresentable paths, racing reads, and repo/runtime substitution fail closed. Only accepted stable observations persist; errors advance no head. Unchanged raw manifest preserves generation; status is read-pure. The observer owns bounded current-state identity, makes no historical continuity claim, and has no Ready field or downstream integration." + repository_body_observer: "Native Rust explicitly binds caller workspace ID to an existing validated runtime/swarm/source identity plus canonical Git root/object format/scope/policy. Bind also installs one immutable runtime-side RuntimeRepositoryBodyStoreBinding naming the canonical external Body-store locator and exact Body-binding hash; every read revalidates runtime/swarm/workspace/path/body identity and refuses a second store. Two equal sanitized temporary-index scans enumerate ignore-aware UTF-8 paths, Git modes, and nonrecursive gitlinks with global excludes disabled; raw regular bytes/lengths and non-followed symlink-target bytes are SHA-256 hashed. The domain-separated ordered raw manifest root, not the clean-filtered auxiliary tree OID, is authoritative Body identity. Every Git subprocess strips ambient repository/worktree/object/ref/index/namespace/replacement/graft/shallow and config-injection authority. Observe cannot mint identity. CultCache MessagePack atomically commits immutable manifest+observation and a manifest-root head by exact CAS. Canonical store-path checks precede writes; corrupt HEAD, sparse checkout, unsafe/unrepresentable paths, racing reads, and repo/runtime substitution fail closed. Only accepted stable observations persist; errors advance no head. Unchanged raw manifest preserves generation; status is read-pure. The observer owns bounded current-state identity, makes no historical continuity claim, and has no Ready field. The runtime route is locator authority only, not proof of model grounding." repository_readiness_join: "Mind must own one race-bounded derived repository-readiness operation from exact authenticated authorities: freshly observe Body root R1, validate canonical RepoModel plus exact Mind admission and Body-grounding receipt, authenticated semantic projection eligibility for that admitted model, and workspace retrieval coverage bound to R1 and a named scope policy, then freshly observe R2 and require R2=R1. Its output is observed-ready-at with a truth interval ending at the second scan, not timeless Ready; later reads revalidate. A→B→A is harmless for content-addressed artifacts. Watcher/journal continuity and Hands receipts are invalidation triggers only. Legacy retrieval Ready/empty dirty paths, timestamps, watcher silence, Git OIDs, Qdrant existence, counts, and unrelated generations are forbidden substitutes." status: active phase: phase_6_reflection_boundary