From 2771a0276ba010bb1b69364d6eda6fe96b848fa8 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Mon, 26 Oct 2026 15:51:08 -0500 Subject: [PATCH 1/4] feat(sandbox): carry a remote sandbox's session attestation to its owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CB-12B's publication gate fails every managed change from a remote sandbox with MissingAttestation: the sandbox's agent signs a session-end attestation into its own cache, and the session state file carrying the MAC key never leaves the VM — so the owner's gate has no attestation to find and no key to verify one under. SubmitAttestation is the missing transport. A sandbox submits the attestation's own serialized bytes (ATST + postcard, schema "atomic.attestation.v1" — what it saved is exactly what the owner loads, same hash both sides) plus the session state file its MAC signature verifies under. The owner saves the attestation in its graph and files the session state under .atomic/sessions/.json — the gate's own lookup path, sanitized with the same safe_session_id, and only when the state carries a non-empty mac_key. Guards mirror PublishProvenance: attestation.write capability, own-session ownership, revalidation, the exclusive gate, and a malformed attestation is a domain refusal that writes nothing. The agent submits at session end (and on a fully-idle Stop) whenever it runs in a remote sandbox, failing the session end closed on transport failure — an unattested session is exactly what this prevents. A DID-signed attestation rides without a MAC key: the host may resolve DID keys later; signature policy is not the transport's to decide. The default sandbox grant grows attestation.write; SubmitChange/ PublishProvenance are unchanged. --- .../src/turn/orchestrator/attestation.rs | 43 ++++++ atomic-cli/src/commands/sandbox.rs | 4 +- atomic-cli/src/remote_sandbox.rs | 39 ++++- .../tests/it/remote_sandbox_client_test.rs | 90 ++++++++++++ .../src/repository/provenance_gate.rs | 6 +- .../src/repository/remote_cache.rs | 91 ++++++++++++ .../services/sandbox/sandbox_messages.proto | 32 ++++ .../services/sandbox/sandbox_service.proto | 13 ++ libatomic/src/daemon/sandbox_grants.rs | 2 + libatomic/src/daemon/sandbox_wire.rs | 29 ++++ libatomic/src/daemon/services_sandbox.rs | 124 +++++++++++++++- libatomic/tests/sandbox_service_test.rs | 138 ++++++++++++++++++ 12 files changed, 604 insertions(+), 7 deletions(-) diff --git a/atomic-agent/src/turn/orchestrator/attestation.rs b/atomic-agent/src/turn/orchestrator/attestation.rs index d689cb3c..da6a92d2 100644 --- a/atomic-agent/src/turn/orchestrator/attestation.rs +++ b/atomic-agent/src/turn/orchestrator/attestation.rs @@ -424,6 +424,49 @@ impl TurnOrchestrator { // unattested operation and no durable refusal (review R3/R6 probe). match repo.save_attestation(&attestation) { Ok(hash) => { + // CB-12B: a remote sandbox's owner never sees its cache. Hand + // the attestation — and the session state file its MAC + // signature verifies under — to the repository, or every + // managed change this session recorded fails the owner's + // publication gate with `MissingAttestation` (its gate reads + // the key from the owner's `.atomic/sessions/.json`, + // which never leaves the sandbox any other way). A + // DID-signed attestation rides without a MAC key too: the + // host may resolve DID keys later, and signature policy is + // not the transport's to decide. Failure is a typed error — + // an unattested session is exactly the failure mode this + // submission exists to prevent (review R3/R6). + if repo.is_remote_sandbox() { + let bytes = attestation.serialize().map_err(|e| { + crate::error::AgentError::AttestationFailed { + session_id: session.session_id.clone(), + reason: format!( + "failed to serialize the attestation for the owner: {e}" + ), + } + })?; + // Exactly the JSON the session store writes — the owner + // files it verbatim where its gate reads it. + let session_json = serde_json::to_string_pretty(&*session).map_err(|e| { + crate::error::AgentError::AttestationFailed { + session_id: session.session_id.clone(), + reason: format!( + "failed to serialize the session state for the owner: {e}" + ), + } + })?; + repo.submit_remote_sandbox_attestation( + &session.session_id, + &bytes, + &session_json, + ) + .map_err(|e| { + crate::error::AgentError::AttestationFailed { + session_id: session.session_id.clone(), + reason: format!("failed to submit the attestation to the owner: {e}"), + } + })?; + } log::info!( "Created signed attestation {} for session {} ({}{} changes, {} models, +{} -{}, wall: {})", hash.to_base32(), diff --git a/atomic-cli/src/commands/sandbox.rs b/atomic-cli/src/commands/sandbox.rs index bb997395..b0f08353 100644 --- a/atomic-cli/src/commands/sandbox.rs +++ b/atomic-cli/src/commands/sandbox.rs @@ -305,13 +305,15 @@ impl Command for Seal { // --------------------------------------------------------------------------- /// What a remote sandbox's grant carries when `--capability` is not given: -/// enough to read and land its view's work and its agent's provenance. +/// enough to read and land its view's work, its agent's provenance, and its +/// agent's session attestations. const DEFAULT_CAPABILITIES: &[&str] = &[ "sandbox.read", "sandbox.submit", "provenance.read", "provenance.write", "agent.checkpoint", + "attestation.write", "vault.read", "vault.write", ]; diff --git a/atomic-cli/src/remote_sandbox.rs b/atomic-cli/src/remote_sandbox.rs index ca172993..3dd3650f 100644 --- a/atomic-cli/src/remote_sandbox.rs +++ b/atomic-cli/src/remote_sandbox.rs @@ -16,7 +16,8 @@ //! `diff`, `log`, `restore`, `intent`, ...): `Repository` resolves the //! pointer to the cache, and the cache calls back here through the //! [`RemoteSandboxLink`] this module installs for slices, change files, -//! submissions and provenance. The agent hooks' provenance journal goes over +//! submissions, provenance and session attestations. The agent hooks' +//! provenance journal goes over //! ProvenanceService with the same token (`agent::provenance_rpc`). use std::path::{Path, PathBuf}; @@ -323,6 +324,42 @@ impl RemoteSandboxLink for CliLink { } }) } + + fn submit_attestation( + &self, + root: &Path, + session_id: &str, + attestation: Vec, + session_json: String, + ) -> Result, String> { + let session_id = session_id.to_string(); + exchange(root, move |repository, target, channel| async move { + let response = sandbox_client(channel) + .submit_attestation(pb::SubmitAttestationRequest { + repository: Some(repository), + meta: Some(pb::RequestMeta { + request_id: uuid::Uuid::new_v4().to_string(), + observed_at: None, + }), + session_id, + attestation: Some(wire::attestation_bytes(attestation)), + session_json, + target: Some(target), + }) + .await + .map_err(refused)? + .into_inner(); + match response.outcome.ok_or("no outcome in SubmitAttestation")? { + pb::submit_attestation_response::Outcome::Submitted(submitted) => Ok(Ok( + wire::hash_from_proto(submitted.attestation_hash.as_ref())?, + )), + pb::submit_attestation_response::Outcome::Refused(info) => { + Ok(Err(wire::rejection_from_error_info(&info) + .ok_or_else(|| info.message.clone())?)) + } + } + }) + } } /// Install the link, once per process. Harmless outside a remote sandbox: diff --git a/atomic-cli/tests/it/remote_sandbox_client_test.rs b/atomic-cli/tests/it/remote_sandbox_client_test.rs index a8239187..a8cde0f3 100644 --- a/atomic-cli/tests/it/remote_sandbox_client_test.rs +++ b/atomic-cli/tests/it/remote_sandbox_client_test.rs @@ -688,6 +688,96 @@ fn an_agent_turn_in_a_remote_sandbox_lands_with_its_provenance() { assert_only_the_cache(&vm); } +/// A remote sandbox's session attestation rides to its owner (CB-12B): the +/// session ends in the sandbox, and the owner's repository carries the +/// attestation covering the turn's change — verified under the session MAC +/// key, filed where the owner's publication gate reads it. +#[test] +fn a_remote_sandboxs_session_attestation_reaches_its_owner() { + let (_server, env, host) = setup(); + let host = host.path(); + let vms = TempDir::new().unwrap(); + let vm = vms.path().join("work"); + open(&env, host, "exp-1", &vm); + ok(env.run(&vm, &["sandbox", "materialize"]), "materialize"); + + let cwd = vm.to_str().unwrap(); + let now = "2026-01-01T00:00:00Z"; + let sid = "agent-session"; + let turn = |n: u32| { + serde_json::json!({ + "session_id": sid, "cwd": cwd, "model": "m", "provider": "p", + "turn_number": n, "intent_title": "greet the reader", "timestamp": now, + }) + }; + ok( + hook( + &env, + &vm, + "session-start", + serde_json::json!({ + "session_id": sid, "cwd": cwd, "model": "m", "provider": "p", + "turn_number": 0, "timestamp": now, + }), + ), + "session-start", + ); + ok(hook(&env, &vm, "turn-start", turn(1)), "turn-start"); + write(&vm, "README.md", "hello\nreader\n"); + ok(hook(&env, &vm, "turn-end", turn(1)), "turn-end"); + ok( + hook( + &env, + &vm, + "session-end", + serde_json::json!({ + "session_id": sid, "cwd": cwd, "turn_number": 1, "timestamp": now, + }), + ), + "session-end", + ); + + // The turn's change, from the session ledger the session-end + // publication built on the host. + let repo = atomic_repository::Repository::open_existing(host).unwrap(); + let (_, turns) = repo + .get_session_ledger(sid) + .unwrap() + .unwrap_or_else(|| panic!("no session ledger for {sid} on the host")); + let change = turns + .iter() + .flat_map(|turn| turn.change_hashes.iter()) + .copied() + .next() + .unwrap_or_else(|| panic!("no change in {sid}'s ledger: {turns:?}")); + + // The owner carries the attestation for that change and session. + let attestations = repo.find_attestations_for_change(&change).unwrap(); + let (_, attestation) = attestations + .iter() + .find(|(_, attest)| attest.session_id == sid) + .unwrap_or_else(|| panic!("no attestation for {sid}: {attestations:?}")); + + // The owner's session state file carries the MAC key, where the + // publication gate reads it — and the attestation verifies under it. + let session_json = std::fs::read( + host.join(".atomic") + .join("sessions") + .join(format!("{sid}.json")), + ) + .unwrap_or_else(|e| panic!("no session state filed on the host: {e}")); + let value: Value = serde_json::from_slice(&session_json).unwrap(); + assert!( + value["mac_key"].as_str().is_some_and(|k| !k.is_empty()), + "{value}" + ); + let key = + atomic_repository::repository::provenance_gate::local_session_mac_key_provider(&repo)(sid) + .expect("the gate resolves the session's MAC key"); + assert!(attestation.verify_mac(&key)); + assert_only_the_cache(&vm); +} + /// A sherpa turn's provenance is the full account of how the change was /// made: the transcript the harness writes (assistant text, tool calls, tool /// results — Claude Code's JSONL shape) rides the change as its unhashed diff --git a/atomic-repository/src/repository/provenance_gate.rs b/atomic-repository/src/repository/provenance_gate.rs index 5c714742..ec00d174 100644 --- a/atomic-repository/src/repository/provenance_gate.rs +++ b/atomic-repository/src/repository/provenance_gate.rs @@ -355,7 +355,11 @@ fn kind_rank(blocker: &GateBlocker) -> u8 { /// Validate a session id for local file lookup (mirrors the agent session /// store's traversal rules). Returns `None` for ids that cannot name a file. -fn safe_session_id(session: &str) -> Option<&str> { +/// +/// Public: the serve side of the sandbox attestation transport files a +/// remote sandbox's session state under the same `/.json` +/// path this gate later reads it from — one sanitizer, both ends. +pub fn safe_session_id(session: &str) -> Option<&str> { if session.is_empty() || session.contains('/') || session.contains('\\') diff --git a/atomic-repository/src/repository/remote_cache.rs b/atomic-repository/src/repository/remote_cache.rs index 7bd9772d..790ba27a 100644 --- a/atomic-repository/src/repository/remote_cache.rs +++ b/atomic-repository/src/repository/remote_cache.rs @@ -345,6 +345,18 @@ pub trait RemoteSandboxLink: Send + Sync { Result, String, >; + /// `SubmitAttestation`: a session-end attestation (`Attestation:: + /// serialize` bytes) plus the session state file it verifies under, + /// saved in the repository. Without this, a remote sandbox's agent + /// attestations die with the sandbox and every managed change it + /// recorded fails the owner's publication gate (CB-12B). + fn submit_attestation( + &self, + root: &Path, + session_id: &str, + attestation: Vec, + session_json: String, + ) -> Result, String>; } static LINK: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -879,6 +891,44 @@ impl Repository { }) } + /// In a remote sandbox, a session attestation also rides to the owner: + /// the signed bytes the sandbox saved, plus the session state file that + /// carries its MAC key. The owner files both — without them its + /// publication gate fails every managed change this session recorded + /// (CB-12B). Returns the attestation's hash on the owner. + pub fn submit_remote_sandbox_attestation( + &self, + session_id: &str, + attestation: &[u8], + session_json: &str, + ) -> Result { + if !self.is_remote_sandbox() { + return Err(RepositoryError::InvalidOperation { + message: "only a remote sandbox submits attestations to an owner".to_string(), + }); + } + // The same sanitizer the owner files the session state under; a + // session id that cannot name a file here cannot name one there. + if super::provenance_gate::safe_session_id(session_id).is_none() { + return Err(RepositoryError::InvalidOperation { + message: format!("refusing session id {session_id:?} as a file name"), + }); + } + link()? + .submit_attestation( + &self.root, + session_id, + attestation.to_vec(), + session_json.to_string(), + ) + .map_err(|message| RepositoryError::InvalidOperation { message })? + .map_err(|rejection| { + RepositoryError::Apply(format!( + "the repository refused the attestation: {rejection}" + )) + }) + } + /// In a remote sandbox, what applying a recorded change means: hand it /// to the owner, and take the view as it is once it lands. /// @@ -1017,6 +1067,47 @@ impl Repository { Ok(Ok(self.publish_local_provenance_checkpoint(&graph, turn)?)) } + /// Serve side: file a remote sandbox's session state under + /// `.atomic/sessions/.json` — the path the publication gate's + /// [`super::provenance_gate::local_session_mac_key_provider`] reads the + /// session MAC key from, so the owner can verify the session's + /// MAC-signed attestations. + /// + /// The session id is sanitized with the gate's own + /// [`super::provenance_gate::safe_session_id`] before it names a file. + /// The state is only adopted when it carries a non-empty `mac_key`: a + /// session state file without one verifies nothing at the gate, so the + /// owner never takes it. Returns whether the file was written. + pub fn save_sandbox_session_state( + &self, + session_id: &str, + session_json: &str, + ) -> Result { + let Some(safe) = super::provenance_gate::safe_session_id(session_id) else { + return Err(RepositoryError::InvalidOperation { + message: format!("refusing session id {session_id:?} as a file name"), + }); + }; + if session_json.is_empty() { + return Ok(false); + } + let value: serde_json::Value = + serde_json::from_str(session_json).map_err(|e| RepositoryError::InvalidOperation { + message: format!("not a session state file: {e}"), + })?; + let carries_key = value + .get("mac_key") + .and_then(serde_json::Value::as_str) + .is_some_and(|key| !key.is_empty()); + if !carries_key { + return Ok(false); + } + let sessions = self.dot_dir().join("sessions"); + std::fs::create_dir_all(&sessions)?; + std::fs::write(sessions.join(format!("{safe}.json")), session_json)?; + Ok(true) + } + /// Cache side: the view's state as the cache has it (base32) — what a /// change recorded here is recorded against. pub fn remote_sandbox_view_state(&self) -> Result { diff --git a/libatomic/proto/atomic/services/sandbox/sandbox_messages.proto b/libatomic/proto/atomic/services/sandbox/sandbox_messages.proto index 92b93b94..c9144419 100644 --- a/libatomic/proto/atomic/services/sandbox/sandbox_messages.proto +++ b/libatomic/proto/atomic/services/sandbox/sandbox_messages.proto @@ -341,6 +341,38 @@ message ChangeRefused { optional SandboxSkeleton skeleton = 2; // present on stale-view refusals } +// A session-end attestation from a remote sandbox (CB-12B transport): the +// signed audit node the sandbox's agent created at session end, plus the +// session state file it verifies under. Both ride to the owner, or every +// managed change the session recorded fails the owner's publication gate +// with MissingAttestation — the gate reads the session MAC key from the +// owner's `.atomic/sessions/.json`, which never leaves the sandbox any +// other way. +message SubmitAttestationRequest { + RepositoryRef repository = 1; + RequestMeta meta = 2; + string session_id = 3; // the session the attestation covers + VersionedBytes attestation = 4; // `Attestation::serialize` (ATST + postcard), + // schema "atomic.attestation.v1" + // The sandbox's session state file, exactly as its SessionStore writes it. + // The owner files it under `.atomic/sessions/.json` — and only + // when it carries a non-empty `mac_key`: a session state file without one + // verifies nothing at the gate, so the owner never adopts it. + string session_json = 5; + optional ViewRef target = 6; +} +message SubmitAttestationResponse { + oneof outcome { + AttestationSubmitted submitted = 1; + ErrorInfo refused = 2; // e.g. a malformed attestation + } + ResponseMeta meta = 100; +} +message AttestationSubmitted { + Hash attestation_hash = 1; // the attestation's content hash, as saved + bool session_state = 2; // whether the session state file was written +} + // ————— grants and sandbox tokens (sandbox-domain; formerly grant.proto) ————— diff --git a/libatomic/proto/atomic/services/sandbox/sandbox_service.proto b/libatomic/proto/atomic/services/sandbox/sandbox_service.proto index fc5e7910..1ac3515b 100644 --- a/libatomic/proto/atomic/services/sandbox/sandbox_service.proto +++ b/libatomic/proto/atomic/services/sandbox/sandbox_service.proto @@ -131,4 +131,17 @@ service SandboxService { option (atomic.allowed_caller) = CALLER_CLASS_SANDBOX; option (atomic.effect) = RPC_EFFECT_REPOSITORY_WRITE; } + // A session-end attestation, riding to the owner with the session state + // file it verifies under (CB-12B): the owner's publication gate fails + // closed without both. Session ownership is enforced — a sandbox submits + // attestations for its own sessions only — and the session id is + // sanitized before it names a file under the owner's `.atomic/sessions`. + rpc SubmitAttestation(SubmitAttestationRequest) returns (SubmitAttestationResponse) { + option (atomic.execution_scope) = EXECUTION_SCOPE_BOTH; + option (atomic.required_capability) = "attestation.write"; + option (atomic.allowed_caller) = CALLER_CLASS_LOCAL; + option (atomic.allowed_caller) = CALLER_CLASS_REMOTE_IDENTITY; + option (atomic.allowed_caller) = CALLER_CLASS_SANDBOX; + option (atomic.effect) = RPC_EFFECT_REPOSITORY_WRITE; + } } diff --git a/libatomic/src/daemon/sandbox_grants.rs b/libatomic/src/daemon/sandbox_grants.rs index 8d0570f1..b67e9233 100644 --- a/libatomic/src/daemon/sandbox_grants.rs +++ b/libatomic/src/daemon/sandbox_grants.rs @@ -74,6 +74,7 @@ pub const SANDBOX_METHODS: &[(&str, &str)] = &[ ("SandboxService", "GetChanges"), ("SandboxService", "SubmitChange"), ("SandboxService", "PublishProvenance"), + ("SandboxService", "SubmitAttestation"), ("ProvenanceService", "ReserveTurn"), ("ProvenanceService", "AppendEnvelopes"), ("ProvenanceService", "PrepareCheckpoint"), @@ -114,6 +115,7 @@ pub const SANDBOX_SCOPED_METHODS: &[(&str, &str)] = &[ ("SandboxService", "GetChanges"), ("SandboxService", "SubmitChange"), ("SandboxService", "PublishProvenance"), + ("SandboxService", "SubmitAttestation"), ("ProvenanceService", "ReserveTurn"), ("ProvenanceService", "AppendEnvelopes"), ("ProvenanceService", "PrepareCheckpoint"), diff --git a/libatomic/src/daemon/sandbox_wire.rs b/libatomic/src/daemon/sandbox_wire.rs index 80357677..e7437ea5 100644 --- a/libatomic/src/daemon/sandbox_wire.rs +++ b/libatomic/src/daemon/sandbox_wire.rs @@ -28,6 +28,8 @@ pub const PROVENANCE_GRAPH_SCHEMA: &str = "atomic.prov.graph.v1"; pub const SESSION_TURN_SCHEMA: &str = "atomic.session.turn.v1"; /// A [`SessionCheckpointPublication`] as JSON. pub const PUBLICATION_SCHEMA: &str = "atomic.session.checkpoint-publication.v1"; +/// A serialized attestation (`Attestation::serialize`: ATST magic + postcard). +pub const ATTESTATION_SCHEMA: &str = "atomic.attestation.v1"; /// `ErrorInfo.context` key naming a [`SubmitRejection`] variant. pub const REJECTION_REASON: &str = "reason"; @@ -368,6 +370,18 @@ pub fn publication_from( .map_err(|e| e.to_string()) } +/// An attestation's serialized bytes (`Attestation::serialize`) on the wire. +/// What travels is exactly what the sending side saved, so the hash the +/// owner computes over it is the hash the sender did. +pub fn attestation_bytes(attestation: Vec) -> pb::VersionedBytes { + versioned(ATTESTATION_SCHEMA, attestation) +} + +/// The serialized attestation a [`pb::SubmitAttestationRequest`] carries. +pub fn attestation_from(bytes: Option<&pb::VersionedBytes>) -> Result, String> { + Ok(payload(bytes, ATTESTATION_SCHEMA)?.to_vec()) +} + #[cfg(test)] mod tests { use super::*; @@ -400,6 +414,21 @@ mod tests { } } + #[test] + fn an_attestation_survives_the_wire_untouched() { + let bytes = b"ATST\x04payload".to_vec(); + let carried = attestation_bytes(bytes.clone()); + let back = attestation_from(Some(&carried)).unwrap(); + assert_eq!(back, bytes); + // Another schema never reads as an attestation. + let foreign = pb::VersionedBytes { + schema: SESSION_TURN_SCHEMA.to_string(), + payload: bytes, + }; + assert!(attestation_from(Some(&foreign)).is_err()); + assert!(attestation_from(None).is_err()); + } + #[test] fn every_rejection_survives_the_wire() { for rejection in [ diff --git a/libatomic/src/daemon/services_sandbox.rs b/libatomic/src/daemon/services_sandbox.rs index 4888616b..4d3f57cc 100644 --- a/libatomic/src/daemon/services_sandbox.rs +++ b/libatomic/src/daemon/services_sandbox.rs @@ -7,10 +7,11 @@ //! - Grant admin (`OpenSandbox`/`RenewSandbox`/`CloseSandbox`), local callers //! only, through the state's [`super::sandbox_grants::SandboxGrants`]. //! - The remote-sandbox data ops (`Materialize`, `GetFileStates`, -//! `GetChanges`, `SubmitChange`, `PublishProvenance`) on the repository's -//! remote-cache kernel (`atomic_repository::remote_cache`). A sandbox -//! caller reaches exactly its grant's view of its grant's repository; a -//! local caller names the view. +//! `GetChanges`, `SubmitChange`, `PublishProvenance`, `SubmitAttestation`) +//! on the repository's remote-cache kernel +//! (`atomic_repository::remote_cache`). A sandbox caller reaches exactly +//! its grant's view of its grant's repository; a local caller names the +//! view. use std::collections::BTreeMap; use std::sync::Arc; @@ -842,4 +843,119 @@ impl sandbox_service_server::SandboxService for SandboxImpl { meta: Some(meta), })) } + + /// `SubmitAttestation`: a remote sandbox's session-end attestation — the + /// signed audit node its agent created — plus the session state file it + /// verifies under, saved in the repository (CB-12B). + /// + /// The gate that decides whether a managed change may cross a + /// publication boundary needs two things this sandbox holds and no other + /// transport carries: a same-session attestation over the change, and the + /// session MAC key to verify it (`.atomic/sessions/.json`). Both ride + /// here, or every managed change the session recorded fails closed on the + /// owner with `MissingAttestation`. + /// + /// A sandbox submits attestations for its own sessions only, and the + /// session id is sanitized before it names a file + /// ([`Repository::save_sandbox_session_state`], the gate's own path + /// rules). A malformed attestation is a domain refusal — nothing is + /// written. The session state is only adopted when it carries a + /// non-empty `mac_key`; a DID-signed attestation rides regardless (the + /// owner may resolve DID keys later — signature policy is not the + /// transport's to decide). + async fn submit_attestation( + &self, + request: Request, + ) -> Result, Status> { + let caller = self.state.sandbox_caller(&request)?; + let request = request.into_inner(); + let repository = repository_ref(&request.repository)?; + let handle = self.state.resolve(&repository)?; + self.state.log_rpc("SubmitAttestation", Some(&handle)); + let view = data_view( + caller.as_ref(), + &handle, + &repository, + request.target.as_ref(), + "attestation.write", + )?; + let session_id = request.session_id; + if session_id.is_empty() { + return Err(invalid("session_id is required")); + } + let bytes = wire::attestation_from(request.attestation.as_ref()).map_err(invalid)?; + let attestation = match atomic_core::change::attestation::Attestation::deserialize(&bytes) { + Ok((attestation, _)) => attestation, + Err(e) => { + return Ok(Response::new(SubmitAttestationResponse { + outcome: Some(submit_attestation_response::Outcome::Refused( + wire::rejection_error_info(&SubmitRejection::Malformed(e.to_string())), + )), + meta: response_meta(&request.meta), + })) + } + }; + if attestation.session_id != session_id { + return Err(invalid("the attestation names another session")); + } + if let Some(caller) = &caller { + let owns = self + .state + .sandbox_grants() + .owns_session(&caller.grant, &session_id) + .map_err(|e| e.status())?; + if !owns { + return Err(domain_status( + ErrorCode::Forbidden, + format!( + "session '{}' is not this sandbox's (view '{}')", + session_id, + caller.view() + ), + )); + } + } + let meta = request.meta.clone(); + let session_json = request.session_json; + let gate_handle = handle.clone(); + let _gate = gate_handle.exclusive().await; + if let Some(caller) = &caller { + caller.revalidate(&self.state)?; + } + let outcome = tokio::task::spawn_blocking(move || { + let repo = handle.repository()?; + check_view(&repo, caller.as_ref(), &view)?; + // The session state first: a gate that runs between the two + // writes never sees an attestation whose key it cannot read. + let session_state = repo + .save_sandbox_session_state(&session_id, &session_json) + .map_err(repository_error)?; + let hash = repo + .save_attestation(&attestation) + .map_err(repository_error)?; + use atomic_core::types::Base32; + tracing::info!( + "Saved a remote sandbox's attestation {} for session {} (session state {})", + hash.to_base32(), + session_id, + if session_state { + "written" + } else { + "not carried" + }, + ); + Ok::<_, Status>(submit_attestation_response::Outcome::Submitted( + AttestationSubmitted { + attestation_hash: Some(hash_proto(&hash)), + session_state, + }, + )) + }) + .await + .map_err(joined)??; + Ok(Response::new(SubmitAttestationResponse { + outcome: Some(outcome), + meta: response_meta(&meta), + })) + } } diff --git a/libatomic/tests/sandbox_service_test.rs b/libatomic/tests/sandbox_service_test.rs index 18decade..972d045c 100644 --- a/libatomic/tests/sandbox_service_test.rs +++ b/libatomic/tests/sandbox_service_test.rs @@ -40,6 +40,7 @@ const ALL: &[&str] = &[ "provenance.read", "provenance.write", "agent.checkpoint", + "attestation.write", "vault.read", "vault.write", ]; @@ -397,6 +398,42 @@ impl RemoteSandboxLink for TestLink { )), } } + + fn submit_attestation( + &self, + root: &Path, + session_id: &str, + attestation: Vec, + session_json: String, + ) -> Result, String> { + let route = Self::route(root)?; + let service = SandboxImpl { + state: Arc::clone(&route.state), + }; + let response = route + .rt + .block_on(service.submit_attestation(with_token( + SubmitAttestationRequest { + repository: Some(route.repository.clone()), + meta: None, + session_id: session_id.to_string(), + attestation: Some(wire::attestation_bytes(attestation)), + session_json, + target: None, + }, + &route.token, + ))) + .map_err(status_text)? + .into_inner(); + match response.outcome.ok_or("no outcome")? { + submit_attestation_response::Outcome::Submitted(submitted) => Ok(Ok( + wire::hash_from_proto(submitted.attestation_hash.as_ref())?, + )), + submit_attestation_response::Outcome::Refused(info) => Ok(Err( + wire::rejection_from_error_info(&info).ok_or_else(|| info.message.clone())?, + )), + } + } } // ── the sandbox: a directory with a pointer and a cache ───────────────── @@ -1339,3 +1376,104 @@ fn a_sandbox_publishes_provenance_for_its_own_work() { "{refused}" ); } + +/// A session attestation rides to the owner with the session state file it +/// verifies under (CB-12B): after a sandbox submits one, the owner's +/// repository carries the attestation covering the sandbox's change, the +/// owner's `.atomic/sessions/.json` holds the MAC key, and the gate's +/// own key provider reads it — what the sandbox saves is exactly what the +/// owner loads. +#[test] +fn a_sandboxs_session_attestation_reaches_its_owner() { + let host = host(); + host.draft("exp-1"); + let opened = host.open("exp-1", ALL); + let vm = materialize(&host, &opened); + write(&vm.root, "README.md", "hello\nreader\n"); + let change = record(&vm.cache, "greet the reader").unwrap(); + // The sandbox's session, claimed the way a sandbox's agent hooks claim + // one: a provenance turn reserved through its token. + reserve(&host, Some(&opened.token), "agent-session", 1).unwrap(); + + // The session state file, as the sandbox's agent store writes it — with + // the MAC key its attestation verifies under. + let mac_key = "ab".repeat(32); + let session_json = serde_json::json!({ + "session_id": "agent-session", + "mac_key": mac_key, + }) + .to_string(); + let mut attestation = atomic_core::change::attestation::Attestation::builder( + "agent-session", + atomic_core::change::attestation::AttestAgent::new( + "claude-code", + "Claude Code", + "anthropic", + ), + ) + .changes_covered(vec![change]) + .build(); + attestation.sign_with_mac(&mac_key); + assert!(attestation.verify_mac(&mac_key)); + let bytes = attestation.serialize().unwrap(); + + let submitted = vm + .cache + .submit_remote_sandbox_attestation("agent-session", &bytes, &session_json) + .unwrap(); + let (_, local) = atomic_core::change::attestation::Attestation::deserialize(&bytes).unwrap(); + assert_eq!(submitted, local, "the owner saved what the sandbox signed"); + + // The owner finds the attestation for the change... + let found = host.with_repo(|repo| repo.find_attestations_for_change(&change).unwrap()); + let (_, theirs) = found + .iter() + .find(|(hash, attest)| *hash == submitted && attest.session_id == "agent-session") + .unwrap_or_else(|| panic!("no attestation for the sandbox's change: {found:?}")); + + // ...its gate reads the session's MAC key from the owner's own session + // state file, and the attestation verifies under it. + let key = host.with_repo(|repo| { + let provider = + atomic_repository::repository::provenance_gate::local_session_mac_key_provider(repo); + provider("agent-session") + }); + assert_eq!(key.as_deref(), Some(mac_key.as_str())); + assert!(theirs.verify_mac(&mac_key)); + assert!( + host.root + .join(".atomic") + .join("sessions") + .join("agent-session.json") + .is_file(), + "the owner filed the session state" + ); + + // A session the sandbox does not own is refused, and nothing is written. + let mut foreign_attest = atomic_core::change::attestation::Attestation::builder( + "another-session", + atomic_core::change::attestation::AttestAgent::new( + "claude-code", + "Claude Code", + "anthropic", + ), + ) + .changes_covered(vec![change]) + .build(); + foreign_attest.sign_with_mac(&mac_key); + let refused = vm + .cache + .submit_remote_sandbox_attestation( + "another-session", + &foreign_attest.serialize().unwrap(), + &session_json, + ) + .unwrap_err(); + assert!(refused.to_string().contains("FORBIDDEN"), "{refused}"); + assert!(!host + .root + .join(".atomic") + .join("sessions") + .join("another-session.json") + .is_file()); +} From fe7879de832e170cc9182d148a6e5b3aa7f8ee8b Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 27 Oct 2026 13:23:32 -0500 Subject: [PATCH 2/4] feat(gate): verify DID-signed attestations in the publication gate Agent VMs sign attestations with Ed25519 DID signatures when ATOMIC_AGENT_IDENTITY is set, but the publication gate only verified session-MAC signatures, so every DID-signed attestation was refused with AttestationNotVerifying on the serving host. - Add a DidKeyProvider callback (signer DID -> Ed25519 public key) alongside MacKeyProvider, threaded through evaluate_publication_gate and enforce_publication_gate; all callers updated. - Select verification by signature class in the gate: MAC-signed attestations use the session MAC key as before, DID-signed attestations resolve the signer's public key through the new provider (cached per signer) and verify with verify_with_did; a missing key is AttestationUnverifiable (fail closed), a wrong key is AttestationNotVerifying. Unsigned historical V1/V2 unchanged. - Add local_did_key_provider resolving a signer through the host's default identity store (by name, then by DID over all identities, accepting did:atomic and did:key renderings), mirroring resolve_author_for. Read-only; no repo write lock. Wired into the host-side callers (shared-view insertion, triage promotion, revise, triage report, pre-push hook, Git ref export, Atomic push); the receiving boundary (verify_receive) passes None for both providers. - Trust semantics unchanged and policy-only: a DID signer satisfies the gate only when listed in [git.trust] signers or equal to the repository identity. No default trust invented. - Tests: DID-signed attestation passes with provider key and trusted signer; no key -> unverifiable (with and without a provider); wrong key -> not verifying; verifying but unlisted signer -> untrusted. MAC path tests stay green unchanged. --- atomic-cli/src/commands/git/hooks.rs | 9 +- atomic-cli/src/commands/git/push.rs | 11 +- atomic-cli/src/commands/git/verify_receive.rs | 5 +- atomic-cli/src/commands/push/command.rs | 11 +- atomic-repository/src/repository/insert.rs | 24 +- .../src/repository/provenance_gate.rs | 349 ++++++++++++++++-- atomic-repository/src/repository/revise.rs | 16 +- atomic-repository/src/triage/report.rs | 2 + 8 files changed, 380 insertions(+), 47 deletions(-) diff --git a/atomic-cli/src/commands/git/hooks.rs b/atomic-cli/src/commands/git/hooks.rs index 2c68b568..65438b0c 100644 --- a/atomic-cli/src/commands/git/hooks.rs +++ b/atomic-cli/src/commands/git/hooks.rs @@ -819,7 +819,14 @@ pub(crate) fn run_pre_push_verification_with_input(root: &Path, input: &str) -> .map_err(CliError::Repository)?; let provider = atomic_repository::repository::provenance_gate::local_session_mac_key_provider(&repo); - match repo.evaluate_publication_gate(&closure, &gate_config(&repo), Some(&provider)) { + let did_provider = + atomic_repository::repository::provenance_gate::local_did_key_provider(&repo); + match repo.evaluate_publication_gate( + &closure, + &gate_config(&repo), + Some(&provider), + Some(&did_provider), + ) { Ok(verdict) if verdict.allowed() => { print_info(&format!( "pre-push (local, advisory): {current_view} passed local verification \ diff --git a/atomic-cli/src/commands/git/push.rs b/atomic-cli/src/commands/git/push.rs index ddd0481d..5dcdbb57 100644 --- a/atomic-cli/src/commands/git/push.rs +++ b/atomic-cli/src/commands/git/push.rs @@ -130,8 +130,17 @@ impl Command for Push { atomic_repository::repository::provenance_gate::local_session_mac_key_provider( &readonly_repo, ); + let did_provider = + atomic_repository::repository::provenance_gate::local_did_key_provider( + &readonly_repo, + ); readonly_repo - .enforce_publication_gate("Git ref export", &closure, Some(&provider)) + .enforce_publication_gate( + "Git ref export", + &closure, + Some(&provider), + Some(&did_provider), + ) .map_err(CliError::Repository)?; } // RFC §8.3 (CB-8B): remote export of a view with unresolved Atomic diff --git a/atomic-cli/src/commands/git/verify_receive.rs b/atomic-cli/src/commands/git/verify_receive.rs index ee3546ae..6aa58237 100644 --- a/atomic-cli/src/commands/git/verify_receive.rs +++ b/atomic-cli/src/commands/git/verify_receive.rs @@ -252,10 +252,11 @@ fn verify_ref_update( // Trusted provenance gate over the complete delta (transitive // dependencies included). Receiving boundaries hold no local session - // MAC keys: managed attestations verify as unverifiable and fail closed. + // MAC keys and no DID resolver: managed attestations verify as + // unverifiable and fail closed. let closure = atomic_repository::repository::provenance_gate::reachable_closure(repo, &delta) .map_err(|error| format!("cannot compute the reachable closure: {error}"))?; - match repo.evaluate_publication_gate(&closure, &gate_config(repo), None) { + match repo.evaluate_publication_gate(&closure, &gate_config(repo), None, None) { Ok(verdict) if verdict.allowed() => Ok(verdict.managed_changes), Ok(verdict) => Err(format!( "managed provenance gate refused ({}/{} managed of {} checked):\n{}", diff --git a/atomic-cli/src/commands/push/command.rs b/atomic-cli/src/commands/push/command.rs index f56a19e5..00d88345 100644 --- a/atomic-cli/src/commands/push/command.rs +++ b/atomic-cli/src/commands/push/command.rs @@ -724,8 +724,15 @@ impl Push { atomic_repository::repository::provenance_gate::local_session_mac_key_provider( &repo, ); - repo.enforce_publication_gate("Atomic push", &push_closure, Some(&provider)) - .map_err(CliError::Repository)?; + let did_provider = + atomic_repository::repository::provenance_gate::local_did_key_provider(&repo); + repo.enforce_publication_gate( + "Atomic push", + &push_closure, + Some(&provider), + Some(&did_provider), + ) + .map_err(CliError::Repository)?; } } diff --git a/atomic-repository/src/repository/insert.rs b/atomic-repository/src/repository/insert.rs index 94cec399..e64d2b66 100644 --- a/atomic-repository/src/repository/insert.rs +++ b/atomic-repository/src/repository/insert.rs @@ -1789,7 +1789,13 @@ impl Repository { let closure = super::provenance_gate::reachable_closure(self, std::slice::from_ref(hash))?; let provider = super::provenance_gate::local_session_mac_key_provider(self); - self.enforce_publication_gate("shared-view insertion", &closure, Some(&provider))?; + let did_provider = super::provenance_gate::local_did_key_provider(self); + self.enforce_publication_gate( + "shared-view insertion", + &closure, + Some(&provider), + Some(&did_provider), + )?; } } @@ -2215,7 +2221,13 @@ impl Repository { if shared_target { let closure = super::provenance_gate::reachable_closure(self, &to_insert)?; let provider = super::provenance_gate::local_session_mac_key_provider(self); - self.enforce_publication_gate("shared-view insertion", &closure, Some(&provider))?; + let did_provider = super::provenance_gate::local_did_key_provider(self); + self.enforce_publication_gate( + "shared-view insertion", + &closure, + Some(&provider), + Some(&did_provider), + )?; options.skip_publication_gate = true; } } @@ -2759,7 +2771,13 @@ impl Repository { if target_is_shared { let closure = super::provenance_gate::reachable_closure(self, &source_changes)?; let provider = super::provenance_gate::local_session_mac_key_provider(self); - self.enforce_publication_gate("triage promotion", &closure, Some(&provider))?; + let did_provider = super::provenance_gate::local_did_key_provider(self); + self.enforce_publication_gate( + "triage promotion", + &closure, + Some(&provider), + Some(&did_provider), + )?; } // When the source view is Draft, its changes were recorded against diff --git a/atomic-repository/src/repository/provenance_gate.rs b/atomic-repository/src/repository/provenance_gate.rs index ec00d174..3f71a5f6 100644 --- a/atomic-repository/src/repository/provenance_gate.rs +++ b/atomic-repository/src/repository/provenance_gate.rs @@ -30,15 +30,19 @@ //! //! # What is explicitly NOT claimed //! -//! The verdict carries a limitation register. As of CB-12A review 8 the -//! attestation signature is a session-scoped keyed-Blake3 MAC, **not** the -//! RFC-required agent-DID signature, and it omits boundary/outcome/binding/ -//! provenance roots (F3); fabricated capture evidence is not detectable here -//! (F2); full boundary/manifest/ref/operation ancestry and leases are absent -//! (F4/F5). A passing gate therefore proves *configured-policy enforcement -//! over the available evidence*, nothing stronger. RFC §19 Q4 (open-source -//! trust default) is UNRESOLVED: no default trust is granted or invented -//! here; only explicitly configured signers ever satisfy the trust check. +//! The verdict carries a limitation register. As of CB-12A review 8 the gate +//! verifies both signature classes it can encounter: session-scoped +//! keyed-Blake3 MACs and Ed25519 agent-DID signatures (V4), each through an +//! injected key provider. Verification is still provider-dependent — without +//! the session MAC key or a DID resolver in context, attestations are +//! *unverifiable* and fail closed — and attestations still omit +//! boundary/outcome/binding/provenance roots (F3); fabricated capture +//! evidence is not detectable here (F2); full boundary/manifest/ref/ +//! operation ancestry and leases are absent (F4/F5). A passing gate +//! therefore proves *configured-policy enforcement over the available +//! evidence*, nothing stronger. RFC §19 Q4 (open-source trust default) is +//! UNRESOLVED: no default trust is granted or invented here; only +//! explicitly configured signers ever satisfy the trust check. //! //! # Content vs provenance separation (RFC §5.2/§12.8) //! @@ -67,6 +71,20 @@ use crate::Repository; /// *unverifiable* and the gate fails closed. pub type MacKeyProvider<'a> = &'a dyn Fn(&str) -> Option; +/// Optional callback supplying an attestation signer DID's Ed25519 public +/// key (32 bytes). +/// +/// Agent VMs sign attestations with DID signatures +/// (`Attestation::sign_with_did`) because `ATOMIC_AGENT_IDENTITY` is set; +/// unlike the session MAC key the DID public key is not a secret, but it is +/// also not carried in the attestation bytes (`did:atomic` is a blake3 +/// fingerprint of the key, not the key itself), so a host that wants to +/// verify DID-signed evidence must resolve the signer through its identity +/// store. Callers without a resolver pass `None` — DID-signed attestations +/// then verify as *unverifiable* and the gate fails closed, exactly like +/// MAC-signed evidence without the MAC key. +pub type DidKeyProvider<'a> = &'a dyn Fn(&str) -> Option<[u8; 32]>; + /// Configuration for one gate evaluation. #[derive(Debug, Clone)] pub struct PublicationGateConfig { @@ -293,8 +311,10 @@ impl GateVerdict { fn default_limitations() -> Vec { vec![ - "attestations are session-MAC authenticated, not agent-DID signed; boundary, outcome, \ - binding and provenance roots are not yet covered by the signature (CB-12A review F3)" + "attestation verification is key-provider-dependent (session MAC key or DID signer \ + resolver); without the key in context the evidence is unverifiable and fails closed, \ + and boundary, outcome, binding and provenance roots are not yet covered by the \ + signature (CB-12A review F3)" .to_string(), "fabricated commit-time capture evidence is not detectable at this gate (CB-12A review F2)" .to_string(), @@ -391,6 +411,43 @@ pub fn local_session_mac_key_provider(repo: &Repository) -> impl Fn(&str) -> Opt } } +/// A [`DidKeyProvider`] that resolves an attestation signer DID to its +/// Ed25519 public key through the serving host's default identity store +/// (`~/.atomic/identities/`, `ATOMIC_CONFIG_DIR`-relocatable). +/// +/// Resolution mirrors the host's author resolution +/// (`libatomic::daemon::services::resolve_author_for`): a signer that names +/// a stored identity (by name) resolves to that identity's key; otherwise the +/// store is scanned for an identity whose DID — derived from its public key +/// (`atomic_canonical::did::did_for_public_key`, `did:atomic:`; the +/// `did:key` rendering matches too) — equals the signer. A signer that +/// resolves to nothing yields `None`, which makes its attestation +/// unverifiable — fail closed. +/// +/// Read-only store access: no secret keys are loaded and the repository's +/// write lock is never taken. The `repo` parameter exists for call-site +/// symmetry with [`local_session_mac_key_provider`]; the identity store is +/// global, not per-repository. +pub fn local_did_key_provider(_repo: &Repository) -> impl Fn(&str) -> Option<[u8; 32]> { + move |signer: &str| { + let store = atomic_identity::IdentityStore::open_default().ok()?; + // A signer that names a stored identity resolves to that identity. + if let Ok(identity) = store.load_by_name(signer) { + return Some(*identity.public_key.as_bytes()); + } + // Otherwise scan for an identity whose DID matches the signer + // (either `did:atomic` or `did:key` rendering of its public key). + store + .list() + .ok()? + .iter() + .find(|identity| { + atomic_canonical::did::did_matches_public_key(signer, &identity.public_key) + }) + .map(|identity| *identity.public_key.as_bytes()) + } +} + impl Repository { /// Evaluate the trusted provenance gate over a change closure. /// @@ -405,6 +462,7 @@ impl Repository { changes: &[Hash], config: &PublicationGateConfig, mac_key_provider: Option>, + did_key_provider: Option>, ) -> Result { let mut verdict = GateVerdict { checked: changes.len(), @@ -413,9 +471,11 @@ impl Repository { limitations: default_limitations(), }; - // Per-session caches: ledger status and MAC keys are session-scoped. + // Per-session caches: ledger status and MAC keys are session-scoped; + // DID keys are signer-scoped. let mut ledger_cache: BTreeMap = BTreeMap::new(); let mut mac_key_cache: BTreeMap> = BTreeMap::new(); + let mut did_key_cache: BTreeMap> = BTreeMap::new(); for hash in changes { let change_b32 = hash.to_base32(); @@ -595,16 +655,17 @@ impl Repository { .iter() .filter(|(_, attest)| attest.session_id == session) .collect(); - let key_available = mac_key_cache - .entry(session.clone()) - .or_insert_with(|| mac_key_provider.and_then(|provider| provider(&session))) - .clone(); let mut checks: Vec = same_session .iter() .map(|(attest_hash, attest)| { - let signature_available = attest.signature.is_some(); - match (signature_available, &key_available) { - (false, _) => AttestationCheck { + // Signature-class selection: a MAC-signed attestation + // needs the session MAC key; a DID-signed attestation + // needs the signer's Ed25519 public key. Both are + // authentication material resolved through injected + // providers — never trust decisions. + if attest.signature.is_none() { + // Unsigned historical V1/V2: unverifiable, as ever. + AttestationCheck { hash: attest_hash.to_base32(), signer: attest.signer.clone(), verified: None, @@ -612,23 +673,55 @@ impl Repository { "attestation carries no signature (historical V1/V2 format)" .to_string(), ), - }, - (true, None) => AttestationCheck { - hash: attest_hash.to_base32(), - signer: attest.signer.clone(), - verified: None, - unverifiable_reason: Some( - "session MAC key unavailable in this context".to_string(), - ), - }, - (true, Some(key)) => { - let verified = attest.verify_mac(key); - AttestationCheck { + } + } else if let Some(did) = attest.signer_did() { + // V4 DID path: resolve the signer's public key + // (cached per signer — one lookup per signer, not + // per attestation). + let key = *did_key_cache + .entry(did.to_string()) + .or_insert_with(|| did_key_provider.and_then(|provider| provider(did))); + match key { + None => AttestationCheck { hash: attest_hash.to_base32(), signer: attest.signer.clone(), - verified: Some(verified), + verified: None, + unverifiable_reason: Some( + "DID signer key unavailable in this context".to_string(), + ), + }, + Some(key) => AttestationCheck { + hash: attest_hash.to_base32(), + signer: attest.signer.clone(), + verified: Some(attest.verify_with_did(&key)), unverifiable_reason: None, - } + }, + } + } else { + // MAC path (or a signer-less signature, which fails + // MAC verification as tamper evidence): the session + // key authenticates, cached per session as today. + let key_available = mac_key_cache + .entry(session.clone()) + .or_insert_with(|| { + mac_key_provider.and_then(|provider| provider(&session)) + }) + .clone(); + match key_available { + None => AttestationCheck { + hash: attest_hash.to_base32(), + signer: attest.signer.clone(), + verified: None, + unverifiable_reason: Some( + "session MAC key unavailable in this context".to_string(), + ), + }, + Some(key) => AttestationCheck { + hash: attest_hash.to_base32(), + signer: attest.signer.clone(), + verified: Some(attest.verify_mac(&key)), + unverifiable_reason: None, + }, } } }) @@ -719,9 +812,11 @@ impl Repository { boundary: &str, changes: &[Hash], mac_key_provider: Option>, + did_key_provider: Option>, ) -> Result { let config = PublicationGateConfig::from_repo(self)?; - let verdict = self.evaluate_publication_gate(changes, &config, mac_key_provider)?; + let verdict = + self.evaluate_publication_gate(changes, &config, mac_key_provider, did_key_provider)?; if !verdict.allowed() { return Err(RepositoryError::PublicationGateRefused { boundary: boundary.to_string(), @@ -841,6 +936,34 @@ mod tests { install_attestation_inner(repo, hash, sign).0 } + /// A fresh Ed25519 agent keypair with its `did:atomic` DID. + fn agent_keypair() -> (atomic_identity::KeyPair, String) { + let keypair = atomic_identity::KeyPair::generate(); + let did = atomic_canonical::did::did_for_public_key(&keypair.public); + (keypair, did) + } + + /// Install a V4 DID-signed attestation covering `hash`, signed by + /// `keypair`'s identity (signer recorded as its `did:atomic` DID). + fn install_did_attestation( + repo: &Repository, + hash: &Hash, + keypair: &atomic_identity::KeyPair, + ) -> String { + let did = atomic_canonical::did::did_for_public_key(&keypair.public); + let mut attestation = Attestation::builder( + SESSION, + AttestAgent::new("test-agent", "Test Agent", "test-vendor"), + ) + .changes_covered(vec![*hash]) + .build(); + attestation.sign_with_did(&did, keypair.secret.as_bytes()); + let att_hash = repo + .save_attestation(&attestation) + .expect("attestation saves"); + att_hash.to_base32() + } + fn install_attestation_inner( repo: &Repository, hash: &Hash, @@ -887,7 +1010,7 @@ mod tests { let hash = repo.save_change(&change).expect("save"); let verdict = repo - .evaluate_publication_gate(&[hash], &config_with_signer("x"), Some(&provider)) + .evaluate_publication_gate(&[hash], &config_with_signer("x"), Some(&provider), None) .expect("gate runs"); assert!(verdict.allowed()); assert_eq!(verdict.checked, 1); @@ -968,6 +1091,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&no_provider), + None, ) .expect("gate runs"); assert!( @@ -991,6 +1115,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!(!verdict.allowed()); @@ -1025,6 +1150,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!( @@ -1034,6 +1160,148 @@ mod tests { ); } + // --------------------------------------------------------------------- + // V4: DID-signed attestations (agent VMs sign with + // `ATOMIC_AGENT_IDENTITY`; the gate must verify them through an + // injected DID key provider and evaluate the DID signer against the + // same policy-only trust config). + // --------------------------------------------------------------------- + + /// A DID-signed attestation whose signer's key the provider resolves + /// and whose DID the trust config lists passes with no attestation + /// blocker. + #[test] + fn did_signed_attestation_with_trusted_signer_passes() { + let (_dir, repo) = create_temp_repo(); + let (keypair, did) = agent_keypair(); + let change = managed_change("did signed trusted"); + let hash = repo.save_change(&change).expect("save"); + install_provenance_root_and_ledger(&repo, &hash); + install_did_attestation(&repo, &hash, &keypair); + + let public = *keypair.public.as_bytes(); + let did_provider = move |_signer: &str| Some(public); + let verdict = repo + .evaluate_publication_gate( + &[hash], + &config_with_signer(&did), + Some(&no_provider), + Some(&did_provider), + ) + .expect("gate runs"); + assert!( + verdict.allowed(), + "expected a verifying, DID-signed attestation from a trusted signer to pass: {}", + verdict.refusal_report() + ); + } + + /// No DID key in context (receiving remote, no resolver): the + /// DID-signed attestation is unverifiable and fails closed — never + /// silently accepted. + #[test] + fn did_signed_attestation_without_did_key_fails_closed_unverifiable() { + let (_dir, repo) = create_temp_repo(); + let (keypair, _did) = agent_keypair(); + let change = managed_change("did signed no key"); + let hash = repo.save_change(&change).expect("save"); + install_provenance_root_and_ledger(&repo, &hash); + install_did_attestation(&repo, &hash, &keypair); + + // A provider that resolves nothing... + let verdict = repo + .evaluate_publication_gate( + &[hash], + &config_with_signer("did:atomic:irrelevant"), + Some(&no_provider), + Some(&|_signer: &str| None), + ) + .expect("gate runs"); + assert!(!verdict.allowed()); + assert!( + blocker_kinds(&verdict).contains(&"unverifiable"), + "{verdict:?}" + ); + + // ...and no provider at all: the same refusal. + let verdict = repo + .evaluate_publication_gate( + &[hash], + &config_with_signer("did:atomic:irrelevant"), + Some(&no_provider), + None, + ) + .expect("gate runs"); + assert!(!verdict.allowed()); + assert!( + blocker_kinds(&verdict).contains(&"unverifiable"), + "{verdict:?}" + ); + } + + /// A resolved but wrong key (signer DID does not match the signing key) + /// is tamper/forgery evidence: the signature does not verify. + #[test] + fn did_signed_attestation_with_wrong_key_is_refused() { + let (_dir, repo) = create_temp_repo(); + let (keypair, _did) = agent_keypair(); + let (other, _other_did) = agent_keypair(); + let change = managed_change("did signed wrong key"); + let hash = repo.save_change(&change).expect("save"); + install_provenance_root_and_ledger(&repo, &hash); + install_did_attestation(&repo, &hash, &keypair); + + let wrong = *other.public.as_bytes(); + let did_provider = move |_signer: &str| Some(wrong); + let verdict = repo + .evaluate_publication_gate( + &[hash], + &config_with_signer("did:atomic:irrelevant"), + Some(&no_provider), + Some(&did_provider), + ) + .expect("gate runs"); + assert!(!verdict.allowed()); + assert!( + blocker_kinds(&verdict).contains(&"signature"), + "{verdict:?}" + ); + } + + /// A verifying DID signature from a signer the trust policy does not + /// list is refused: no default trust exists for DID signers either. + #[test] + fn did_signed_attestation_with_untrusted_signer_is_refused() { + let (_dir, repo) = create_temp_repo(); + let (keypair, _did) = agent_keypair(); + let change = managed_change("did signed untrusted"); + let hash = repo.save_change(&change).expect("save"); + install_provenance_root_and_ledger(&repo, &hash); + install_did_attestation(&repo, &hash, &keypair); + + let public = *keypair.public.as_bytes(); + let did_provider = move |_signer: &str| Some(public); + // The key verifies, but the signer is configured nowhere (empty + // policy, no repository identity): policy-only trust. + let verdict = repo + .evaluate_publication_gate( + &[hash], + &PublicationGateConfig { + trust: GitTrustConfig::default(), + repository_identity: None, + }, + Some(&no_provider), + Some(&did_provider), + ) + .expect("gate runs"); + assert!(!verdict.allowed()); + let kinds = blocker_kinds(&verdict); + assert!( + kinds.contains(&"signer") && !kinds.contains(&"signature"), + "{verdict:?}" + ); + } + #[test] fn tampered_envelope_fails_closed() { let (_dir, repo) = create_temp_repo(); @@ -1046,6 +1314,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!(blocker_kinds(&verdict).contains(&"tampered"), "{verdict:?}"); @@ -1064,6 +1333,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&no_provider), + None, ) .expect("gate runs"); assert!(!verdict.allowed()); @@ -1086,6 +1356,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!(!verdict.allowed()); @@ -1118,6 +1389,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!( @@ -1143,6 +1415,7 @@ mod tests { repository_identity: None, }, Some(&provider), + None, ) .expect("gate runs"); assert!(blocker_kinds(&verdict).contains(&"signer"), "{verdict:?}"); @@ -1156,7 +1429,7 @@ mod tests { repository_identity: None, }; let verdict = repo - .evaluate_publication_gate(&[hash], &revoked_config, Some(&provider)) + .evaluate_publication_gate(&[hash], &revoked_config, Some(&provider), None) .expect("gate runs"); assert!(blocker_kinds(&verdict).contains(&"signer"), "{verdict:?}"); } @@ -1184,6 +1457,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); let kinds = blocker_kinds(&verdict); @@ -1222,6 +1496,7 @@ mod tests { &[hash], &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!( @@ -1260,6 +1535,7 @@ mod tests { &closure, &config_with_signer("session-mac:sess-gate"), Some(&provider), + None, ) .expect("gate runs"); assert!(!verdict.allowed()); @@ -1354,6 +1630,7 @@ mod privacy_tests { repository_identity: None, }, Some(&|_| Some("dd44dd44".to_string())), + None, ) .expect("gate runs"); let report = verdict.refusal_report(); diff --git a/atomic-repository/src/repository/revise.rs b/atomic-repository/src/repository/revise.rs index 5fa59404..05b3845b 100644 --- a/atomic-repository/src/repository/revise.rs +++ b/atomic-repository/src/repository/revise.rs @@ -90,7 +90,13 @@ impl Repository { .collect(); let closure = super::provenance_gate::reachable_closure(self, &roots)?; let provider = super::provenance_gate::local_session_mac_key_provider(self); - self.enforce_publication_gate("revise reinsertion", &closure, Some(&provider))?; + let did_provider = super::provenance_gate::local_did_key_provider(self); + self.enforce_publication_gate( + "revise reinsertion", + &closure, + Some(&provider), + Some(&did_provider), + )?; } let mut header = original.hashed.header.clone(); @@ -111,7 +117,13 @@ impl Repository { if self.get_view_info(&view)?.scope == atomic_core::pristine::ViewScope::Shared { let closure = super::provenance_gate::reachable_closure(self, &plan.hashes)?; let provider = super::provenance_gate::local_session_mac_key_provider(self); - self.enforce_publication_gate("revised history", &closure, Some(&provider))?; + let did_provider = super::provenance_gate::local_did_key_provider(self); + self.enforce_publication_gate( + "revised history", + &closure, + Some(&provider), + Some(&did_provider), + )?; } let result = (|| { for (_, hash) in history[target_index..].iter().rev() { diff --git a/atomic-repository/src/triage/report.rs b/atomic-repository/src/triage/report.rs index 8c0a180a..2bb96f9f 100644 --- a/atomic-repository/src/triage/report.rs +++ b/atomic-repository/src/triage/report.rs @@ -706,6 +706,7 @@ pub fn build_report( candidate_hashes.dedup(); if !candidate_hashes.is_empty() { let provider = crate::repository::provenance_gate::local_session_mac_key_provider(repo); + let did_provider = crate::repository::provenance_gate::local_did_key_provider(repo); match repo.evaluate_publication_gate( &candidate_hashes, &crate::repository::provenance_gate::PublicationGateConfig::from_repo(repo) @@ -714,6 +715,7 @@ pub fn build_report( repository_identity: None, }), Some(&provider), + Some(&did_provider), ) { Ok(verdict) => { for blocker in &verdict.blocks { From da087d647f572ff2415e5c26c9ba812787b26ed6 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 27 Oct 2026 14:25:08 -0500 Subject: [PATCH 3/4] Resolve attestation signer DIDs through the repository's own identity store first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The publication gate's local_did_key_provider resolved signer DIDs only through the serving host's global store (~/.atomic/identities), so an attestation signed by a delegated agent identity — which lives in the project clone it worked in, not the serving account's global store — never resolved: AttestationUnverifiable, fail closed, every promote. The provider is repo-aware now: IdentityStore::open_existing on /identities first (existing-only, so resolution never creates files), falling back to open_default. Resolution rules unchanged: name lookup, then a scan matching did_matches_public_key in both did:atomic and did:key renderings. A host that writes the agent identity into the clone (beside its [git.trust] signers entry) makes the gate self-sufficient on the receiving side too. --- .../src/repository/provenance_gate.rs | 147 +++++++++++++++--- 1 file changed, 127 insertions(+), 20 deletions(-) diff --git a/atomic-repository/src/repository/provenance_gate.rs b/atomic-repository/src/repository/provenance_gate.rs index 3f71a5f6..1c3d0347 100644 --- a/atomic-repository/src/repository/provenance_gate.rs +++ b/atomic-repository/src/repository/provenance_gate.rs @@ -412,8 +412,9 @@ pub fn local_session_mac_key_provider(repo: &Repository) -> impl Fn(&str) -> Opt } /// A [`DidKeyProvider`] that resolves an attestation signer DID to its -/// Ed25519 public key through the serving host's default identity store -/// (`~/.atomic/identities/`, `ATOMIC_CONFIG_DIR`-relocatable). +/// Ed25519 public key through the repository's own identity store first +/// (`/.atomic/identities/`), falling back to the serving host's +/// default store (`~/.atomic/identities/`, `ATOMIC_CONFIG_DIR`-relocatable). /// /// Resolution mirrors the host's author resolution /// (`libatomic::daemon::services::resolve_author_for`): a signer that names @@ -424,28 +425,52 @@ pub fn local_session_mac_key_provider(repo: &Repository) -> impl Fn(&str) -> Opt /// resolves to nothing yields `None`, which makes its attestation /// unverifiable — fail closed. /// +/// The repo-local store first is what makes a hosted clone self-sufficient: +/// an attestation's signer (a delegated agent identity) lives with the +/// project — written into the clone by the host that opens the sandbox — +/// not necessarily in the serving account's global store. A receiving +/// repository without a local store simply falls back, and a clone with +/// neither resolves nothing. +/// /// Read-only store access: no secret keys are loaded and the repository's -/// write lock is never taken. The `repo` parameter exists for call-site -/// symmetry with [`local_session_mac_key_provider`]; the identity store is -/// global, not per-repository. -pub fn local_did_key_provider(_repo: &Repository) -> impl Fn(&str) -> Option<[u8; 32]> { +/// write lock is never taken. The repo-local store is opened existing-only +/// (`IdentityStore::open_existing`), so resolution never creates files. +pub fn local_did_key_provider(repo: &Repository) -> impl Fn(&str) -> Option<[u8; 32]> { + let local_store = repo.dot_dir().join("identities"); move |signer: &str| { - let store = atomic_identity::IdentityStore::open_default().ok()?; - // A signer that names a stored identity resolves to that identity. - if let Ok(identity) = store.load_by_name(signer) { - return Some(*identity.public_key.as_bytes()); + // Repo-local first: the identities the clone carries with it. + if let Some(key) = atomic_identity::IdentityStore::open_existing(&local_store) + .ok() + .and_then(|store| did_key_from_store(&store, signer)) + { + return Some(key); } - // Otherwise scan for an identity whose DID matches the signer - // (either `did:atomic` or `did:key` rendering of its public key). - store - .list() - .ok()? - .iter() - .find(|identity| { - atomic_canonical::did::did_matches_public_key(signer, &identity.public_key) - }) - .map(|identity| *identity.public_key.as_bytes()) + // Then the serving host's global store. + atomic_identity::IdentityStore::open_default() + .ok() + .and_then(|store| did_key_from_store(&store, signer)) + } +} + +/// Resolve a signer DID (or identity name) to a public key through one store: +/// a signer that names a stored identity resolves to that identity; otherwise +/// the store is scanned for an identity whose DID matches the signer (either +/// the `did:atomic` or the `did:key` rendering of its public key). +fn did_key_from_store(store: &atomic_identity::IdentityStore, signer: &str) -> Option<[u8; 32]> { + // A signer that names a stored identity resolves to that identity. + if let Ok(identity) = store.load_by_name(signer) { + return Some(*identity.public_key.as_bytes()); } + // Otherwise scan for an identity whose DID matches the signer + // (either `did:atomic` or `did:key` rendering of its public key). + store + .list() + .ok()? + .iter() + .find(|identity| { + atomic_canonical::did::did_matches_public_key(signer, &identity.public_key) + }) + .map(|identity| *identity.public_key.as_bytes()) } impl Repository { @@ -1574,6 +1599,88 @@ mod tests { let outcome = repo.insert_change(&hash, crate::InsertOptions::default().view("gate-draft")); assert!(outcome.is_ok(), "draft insertion is not gated: {error:?}"); } + + /// A DID signer written into the clone's repo-local identity store + /// (`/identities`) resolves through it: name, `did:atomic`, and + /// `did:key` forms all reach the identity's public key, and a signer + /// matching nothing resolves to nothing — fail closed. + #[test] + fn did_provider_resolves_the_clones_repo_local_identity() { + use atomic_canonical::did; + use atomic_identity::{Identity, IdentityStore, KeyPair}; + + let (_dir, repo) = create_temp_repo(); + let keypair = KeyPair::generate(); + let agent = Identity::new("aaron+rook", &keypair); + IdentityStore::open(&repo.dot_dir().join("identities")) + .expect("the clone's identity store opens") + .save(&agent) + .expect("the agent identity is saved into the clone"); + + let provider = super::local_did_key_provider(&repo); + // Both DID renderings resolve to the agent's public key. + assert_eq!( + provider(&did::did_for_public_key(&keypair.public)), + Some(*keypair.public.as_bytes()) + ); + assert_eq!( + provider(&did::did_key_for_public_key(&keypair.public)), + Some(*keypair.public.as_bytes()) + ); + // A signer that names the stored identity resolves too. + assert_eq!(provider("aaron+rook"), Some(*keypair.public.as_bytes())); + // A signer matching nothing resolves to nothing — unverifiable, + // fail closed. + let other = KeyPair::generate(); + assert_eq!(provider(&did::did_for_public_key(&other.public)), None); + } + + /// The global store is the fallback: a signer the clone does not carry + /// locally still resolves through the serving host's default store, + /// and the repo-local store wins when both could answer. + #[test] + fn did_provider_falls_back_to_the_global_store() { + use atomic_canonical::did; + use atomic_identity::{Identity, IdentityStore, KeyPair}; + + // Relocate the global store for the duration of the test (the same + // override `IdentityStore::open_default` reads), and restore it. + let global = tempfile::TempDir::new().unwrap(); + let previous = std::env::var_os("ATOMIC_CONFIG_DIR"); + std::env::set_var("ATOMIC_CONFIG_DIR", global.path()); + + let global_keypair = KeyPair::generate(); + let host = Identity::new("host-account", &global_keypair); + IdentityStore::open_default() + .expect("the relocated global store opens") + .save(&host) + .expect("the host identity is saved globally"); + + let (_dir, repo) = create_temp_repo(); + let local_keypair = KeyPair::generate(); + let agent = Identity::new("aaron+rook", &local_keypair); + IdentityStore::open(&repo.dot_dir().join("identities")) + .expect("the clone's identity store opens") + .save(&agent) + .expect("the agent identity is saved into the clone"); + + let provider = super::local_did_key_provider(&repo); + // The host's signer is not in the clone: the fallback resolves it. + assert_eq!( + provider(&did::did_for_public_key(&global_keypair.public)), + Some(*global_keypair.public.as_bytes()) + ); + // The clone's own signer resolves — repo-local first. + assert_eq!( + provider(&did::did_for_public_key(&local_keypair.public)), + Some(*local_keypair.public.as_bytes()) + ); + + match previous { + Some(value) => std::env::set_var("ATOMIC_CONFIG_DIR", value), + None => std::env::remove_var("ATOMIC_CONFIG_DIR"), + } + } } #[cfg(test)] From c0fe1d66c66d50752763aaba38c26c25bfb88ac7 Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Tue, 27 Oct 2026 15:18:47 -0500 Subject: [PATCH 4/4] fix(agent): a resumed round adopts its durable session history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A managed expedition runs every dispatch and resume of the same expedition id as ONE Atomic session, but each round starts a fresh sandbox VM: the session JSON inside the previous VM dies with it, and a session-start in the new VM created a session at zero turns with no recorded changes. The durable history survived — in the repository owner's session ledger and provenance journal — but nothing read it back: - the resumed round numbered its turn like the previous round's, so its checkpoint reservation hit the previous round's completed journal turn and the owner refused it ("Provenance turn 1 is not running"), the turn-end hook failed BEFORE its session save, and the round's recorded change hashes were lost — its session-end then had nothing left to attest, and the publication gate refused the promote with MissingProvenanceRoot + MissingAttestation; - the round's checkpoint could not chain its graph to the session's previous provenance (a remote sandbox's ledger read sees only its cache, which holds no turns), so the owner refused the append ("turn N is not an append"). A freshly created session now hydrates from the durable history before anything else runs (session-start and the turn hooks' fallback creation): - the journal sink — whose provenance RPCs reach the owner's store even from a remote sandbox — is probed upward from the local count while the session's turns exist; each answered turn advances turn_count and contributes its checkpoint's change hashes; - the repository's own session ledger supplies the same where it is local (the authoritative record for a same-machine resume). The checkpoint path derives its previous-provenance chain link the same way when the ledger read yields none: from the durable journal's latest published turn. GetTurn's wire form already carries both the checkpoint source's change hashes and the turn's final graph hash; JournalTurnStatus now surfaces them (both sink implementations). Tests: the orchestrator test seeds a round-1 ledger turn, attestation and change, then drives a fresh-local-state session through a resumed round — pinned: turn_count adoption, recorded-hash adoption, and a round-2 attestation that covers only the new change and chains to round 1's. The remote-sandbox e2e runs two full dispatches in two sandboxes on one session id — pinned: both rounds publish ledger turns, the round-2 change carries its provenance root, and a same-session attestation covering the round-2 change verifies where the owner's gate reads (a resumed sandbox cannot see the owner's earlier attestations, so its incremental attestation degrades to covering the session's whole hydrated history — harmless to the gate). --- atomic-agent/src/turn/orchestrator/mod.rs | 11 ++ .../src/turn/orchestrator/provenance.rs | 34 +++- .../src/turn/orchestrator/session_start.rs | 138 ++++++++++++++ atomic-agent/src/turn/orchestrator/tests.rs | 159 +++++++++++++++++ .../src/commands/agent/provenance_rpc.rs | 18 ++ .../tests/it/remote_sandbox_client_test.rs | 168 ++++++++++++++++++ libatomic/src/daemon/journal_sink.rs | 6 + 7 files changed, 533 insertions(+), 1 deletion(-) diff --git a/atomic-agent/src/turn/orchestrator/mod.rs b/atomic-agent/src/turn/orchestrator/mod.rs index 33ae13f9..577a7e0b 100644 --- a/atomic-agent/src/turn/orchestrator/mod.rs +++ b/atomic-agent/src/turn/orchestrator/mod.rs @@ -321,6 +321,17 @@ pub struct JournalTurnStatus { pub provenance_id: u64, pub generation: u64, pub lifecycle: JournalTurnLifecycle, + /// The change hashes this turn's checkpoint claim explains — the + /// durable record of what the turn recorded. A resumed session's + /// fresh local state adopts these so its attestation knows what the + /// session's earlier turns already covered (and can chain to them). + /// Empty for a turn that never reached its checkpoint claim. + pub checkpoint_change_hashes: Vec, + /// The provenance graph hash this turn published (`final_hash`) — the + /// chain link a resumed round's checkpoint needs when its local ledger + /// read (a remote sandbox's cache) holds no previous turn. `None` for + /// a turn that never bound its graph. + pub final_provenance_hash: Option, } /// Committed provenance-journal boundary supplied by the CLI owner client. diff --git a/atomic-agent/src/turn/orchestrator/provenance.rs b/atomic-agent/src/turn/orchestrator/provenance.rs index d899d37e..61fbe4a1 100644 --- a/atomic-agent/src/turn/orchestrator/provenance.rs +++ b/atomic-agent/src/turn/orchestrator/provenance.rs @@ -911,7 +911,7 @@ impl TurnOrchestrator { } })? }; - let (previous_provenance, ledger_turn_number) = ledger + let (mut previous_provenance, ledger_turn_number) = ledger .map(|(_, turns)| { ( turns.last().map(|turn| turn.provenance_hash), @@ -919,6 +919,38 @@ impl TurnOrchestrator { ) }) .unwrap_or((None, 0)); + // CB-12B: a resumed round runs in a NEW sandbox whose cache holds + // no session turns, so the ledger read above yields no chain link + // — and the owner would refuse the checkpoint's append as + // non-contiguous ("turn N is not an append"). The durable journal + // (reachable through the sink's provenance RPCs, which a sandbox's + // cache does not shadow) knows the session's previous turn and its + // published graph hash: derive the link from there. A genuinely + // fresh session has no previous turn anywhere and stays unchained. + if previous_provenance.is_none() { + if let Some(sink) = &self.journal_sink { + let mut probe = session.turn_count.max(1); + while previous_provenance.is_none() && probe > 0 { + match sink.turn_status(session_id, probe) { + Ok(Some(status)) => { + previous_provenance = status.final_provenance_hash; + } + Ok(None) => break, + Err(error) => { + log::debug!( + "Could not probe the durable journal for session {} turn \ + {} while chaining the checkpoint: {} (non-fatal)", + session_id, + probe, + error + ); + break; + } + } + probe -= 1; + } + } + } let source = super::JournalCheckpointSource { agent_name: session.agent_name.clone(), agent_display_name: session.agent_display_name.clone(), diff --git a/atomic-agent/src/turn/orchestrator/session_start.rs b/atomic-agent/src/turn/orchestrator/session_start.rs index 08b38048..3f54d84c 100644 --- a/atomic-agent/src/turn/orchestrator/session_start.rs +++ b/atomic-agent/src/turn/orchestrator/session_start.rs @@ -132,6 +132,12 @@ impl TurnOrchestrator { } }; + // CB-12B: a resumed dispatch creates a fresh local session state — + // the previous round ran in a different (since-destroyed) sandbox, + // so the session JSON never carried over. Adopt the durable history + // from the repository's session ledger before anything else runs. + self.hydrate_session_from_durable_ledger(&mut session)?; + // Only sessions CREATED here get the run stamp — the early return // above keeps pre-existing sessions out of the run's attribution. if let Some(stamp) = self.managed_stamp() { @@ -379,6 +385,133 @@ impl TurnOrchestrator { Ok(DispatchResult::new(session_id, session.phase).with_message(message)) } + /// Hydrate a freshly created session from the durable session ledger. + /// + /// A resumed dispatch starts a fresh local session state: the previous + /// round ran in a different (since-destroyed) sandbox, so + /// `.atomic/sessions/.json` — the runtime fallback — is gone, and a + /// session created for the same id starts at zero turns with no + /// recorded changes. The DURABLE evidence of the previous round + /// survives in the repository's session ledger (a remote sandbox's + /// ledger lives with its owner), so that is what a fresh session for an + /// existing id must adopt: + /// + /// - `turn_count` — the number of published ledger turns — so the next + /// turn numbers past the durable history instead of colliding with + /// the previous round's journal turn. A colliding reservation hits + /// the previous checkpoint's `ProvenanceCheckpointConflict`, the + /// turn-end hook fails BEFORE its session save, and the round's + /// recorded change hashes — and with them its same-session + /// attestation — are lost; + /// - `recorded_change_hashes` — each published turn's changes — so the + /// session-end attestation knows what the earlier rounds of this + /// same session already covered, attests only the new round's + /// changes, and chains to the session's latest attestation via + /// `previous_attestation`. + /// + /// A session id with no durable ledger (a genuinely fresh session) is + /// left untouched. Best-effort on read failures, matching + /// [`Self::sync_session_lifecycle`]: the JSON state remains the + /// runtime fallback either way. + pub(crate) fn hydrate_session_from_durable_ledger( + &self, + session: &mut AgentSession, + ) -> AgentResult<()> { + // The journal sink reaches the durable owner store wherever the + // session runs — in a remote sandbox the provenance RPCs carry it + // to the repository's owner (the local cache holds no journal). + // Probe upward from the local count while durable turns exist: + // each answered turn is history this same session already + // published, so adopt its number and its checkpoint's changes. + if let Some(sink) = &self.journal_sink { + let mut turn_number = session.turn_count.saturating_add(1); + loop { + match sink.turn_status(&session.session_id, turn_number) { + Ok(Some(status)) => { + log::info!( + "Resumed session {}: durable journal turn {} exists ({:?}) — \ + adopting it into a fresh local state", + session.session_id, + turn_number, + status.lifecycle, + ); + session.turn_count = turn_number; + for hash in status.checkpoint_change_hashes { + if !session.recorded_change_hashes.contains(&hash) { + session.recorded_change_hashes.push(hash); + } + } + turn_number = turn_number.saturating_add(1); + } + Ok(None) => break, + Err(error) => { + log::debug!( + "Could not probe the durable journal for session {} turn {}: \ + {} (non-fatal)", + session.session_id, + turn_number, + error + ); + break; + } + } + } + } + + // The repository's session ledger is the authoritative local + // record; where the journal is remote (a sandbox's cache) it holds + // no turns and this is a no-op, and where it is local it is the + // source of truth for a resumed same-machine session. + let repo = match atomic_repository::Repository::open_readonly_wait( + &self.repo_root, + wait_budget::database_wait(), + ) { + Ok(repo) => repo, + Err(error) => { + log::debug!( + "Could not open the repository to hydrate session {} from its \ + durable ledger: {} (non-fatal)", + session.session_id, + error, + ); + return Ok(()); + } + }; + let ledger = match repo.get_session_ledger(&session.session_id) { + Ok(ledger) => ledger, + Err(error) => { + log::warn!( + "Could not read the durable session ledger for {}: {} (non-fatal)", + session.session_id, + error + ); + return Ok(()); + } + }; + let Some((_record, turns)) = ledger else { + return Ok(()); + }; + if turns.is_empty() { + return Ok(()); + } + let durable_turn_count = turns.len() as u32; + if durable_turn_count > session.turn_count { + log::info!( + "Resumed session {}: adopting {} durable ledger turn(s) into a \ + fresh local state", + session.session_id, + durable_turn_count, + ); + session.turn_count = durable_turn_count; + } + for hash in turns.iter().flat_map(|turn| turn.change_hashes.iter()) { + if !session.recorded_change_hashes.contains(hash) { + session.recorded_change_hashes.push(*hash); + } + } + Ok(()) + } + /// Best-effort lifecycle reconciliation into the Atomic session ledger. /// /// `ended_at: None` marks the session active (clearing a stale end @@ -586,6 +719,11 @@ impl TurnOrchestrator { } } + // CB-12B: a resumed dispatch has fresh local state — adopt + // the durable ledger before persisting, exactly as + // `handle_session_start` does. + self.hydrate_session_from_durable_ledger(&mut session)?; + self.coordinate_session_view(&session.view_name)?; // Persist so subsequent hooks find this session. diff --git a/atomic-agent/src/turn/orchestrator/tests.rs b/atomic-agent/src/turn/orchestrator/tests.rs index 02a09a33..115af80d 100644 --- a/atomic-agent/src/turn/orchestrator/tests.rs +++ b/atomic-agent/src/turn/orchestrator/tests.rs @@ -2172,3 +2172,162 @@ fn mac_signed_attestation_is_never_trusted_evidence() { "session-MAC signers must be refused as trusted evidence, got {err:?}" ); } + +// CB-12B: a resumed expedition round starts a FRESH local session — the +// previous round ran in a different (since-destroyed) sandbox, so the +// session JSON never carried over — and must adopt the durable history +// from the repository's session ledger before numbering or attesting +// anything: the turn count so the next turn numbers past the previous +// round's published journal turn (a colliding reservation fails the +// previous checkpoint with `ProvenanceCheckpointConflict` and the +// turn-end hook dies before its session save), and the recorded change +// hashes so the resumed round's session-end attestation covers ONLY the +// new round's change and chains to the session's previous attestation. +#[tokio::test] +async fn a_resumed_session_adopts_its_durable_ledger_and_chains_its_attestation() { + use atomic_core::change::attestation::{AttestAgent, Attestation}; + use atomic_core::change::provenance_graph::{ + ProvenanceGraphBuilder, ProvenanceNode, ProvenanceNodeKind, + }; + use atomic_core::change::session::SessionTurn; + + let dir = TempDir::new().unwrap(); + Repository::init(dir.path()).unwrap(); + let sid = "sess-resumed"; + + // Round 1, as its durable owner sees it: a recorded change, a + // published ledger turn explaining it, and a same-session + // attestation covering it. + let round_one_change = { + let repo = Repository::open(dir.path()).unwrap(); + let working_copy = repo.require_working_copy_id().unwrap(); + fs::write(dir.path().join("round-one.txt"), "round one\n").unwrap(); + repo.add( + working_copy, + "round-one.txt", + atomic_repository::tracking::TrackingOptions::default(), + ) + .unwrap(); + let outcome = repo + .record( + working_copy, + atomic_core::change::ChangeHeader::new("round one"), + atomic_repository::record::RecordOptions::new().with_all(true), + ) + .unwrap(); + *outcome.hash() + }; + let round_one_attestation = { + let repo = Repository::open(dir.path()).unwrap(); + repo.upsert_session_lifecycle(sid, Some("dev".into()), None, None) + .unwrap(); + let graph = ProvenanceGraphBuilder::new(sid, "sherpa") + .nodes(vec![ProvenanceNode { + id: "goal-1".into(), + kind: ProvenanceNodeKind::Goal, + timestamp: 1000, + summary: "round one".into(), + detail: None, + change_hash: None, + tool_name: None, + tool_call_id: None, + duration_ms: None, + classified: false, + confidence: None, + consolidated_from: Vec::new(), + }]) + .changes_explained(vec![round_one_change]) + .build(); + let provenance_hash = repo.change_store().save_provenance_graph(&graph).unwrap(); + let turn = SessionTurn { + session_id: sid.to_string(), + turn_number: 0, + goal: Some("round one".into()), + provenance_hash, + change_hashes: vec![round_one_change], + previous_provenance: None, + timestamp: 1000, + plan_id: None, + todos: Vec::new(), + boundary_start: None, + boundary_end: None, + outcome: None, + }; + repo.publish_provenance_checkpoint(&graph, turn).unwrap(); + drop(repo); + + let repo = Repository::open(dir.path()).unwrap(); + let mut attestation = + Attestation::builder(sid, AttestAgent::new("sherpa", "Sherpa", "openai")) + .changes_covered(vec![round_one_change]) + .build(); + attestation.sign_with_mac("resumed-session-mac-key-0123456789abcdef"); + repo.save_attestation(&attestation).unwrap() + }; + + // Round 2 — the resumed dispatch: a fresh local state (no session + // JSON, exactly like a newly forked sandbox), same session id. + let mut orch = make_orchestrator(&dir); + orch.set_agent("sherpa", "Sherpa"); + + orch.dispatch(session_start_event(sid)).await.unwrap(); + + // The fresh session adopted the durable history: the previous + // round's turn count and its recorded change hashes. + let session = orch.session_store.load(sid).unwrap().unwrap(); + assert_eq!( + session.turn_count, 1, + "a resumed session must adopt the durable ledger's turn count", + ); + assert_eq!( + session.recorded_change_hashes, + vec![round_one_change], + "a resumed session must adopt the changes its earlier rounds recorded", + ); + + // The resumed round's turn records past the durable history... + orch.dispatch(turn_start_event(sid, "round two")) + .await + .unwrap(); + fs::write(dir.path().join("round-two.txt"), "round two\n").unwrap(); + let turn_result = orch.dispatch(turn_end_event(sid)).await.unwrap(); + assert!(turn_result.was_recorded(), "the resumed round must record"); + let round_two_change = turn_result + .change_recorded + .as_ref() + .expect("the resumed round recorded a change") + .hash; + assert_ne!(round_two_change, round_one_change); + + // ...and its session-end attestation covers ONLY the new round's + // change, chained to the session's round-1 attestation. + orch.dispatch(session_end_event(sid)).await.unwrap(); + + let repo = Repository::open(dir.path()).unwrap(); + let attestation_hashes: Vec<_> = repo + .change_store() + .iter_attestations() + .filter_map(|r| r.ok()) + .collect(); + assert_eq!( + attestation_hashes.len(), + 2, + "the resumed round must add exactly one attestation", + ); + let round_two_attestation = *attestation_hashes + .iter() + .find(|hash| **hash != round_one_attestation) + .expect("the resumed round's attestation exists"); + let attest = repo.load_attestation(&round_two_attestation).unwrap(); + assert_eq!( + attest.changes_covered, + vec![round_two_change], + "the resumed round's attestation covers only the new round's change", + ); + assert_eq!( + attest.previous_attestation, + Some(round_one_attestation), + "the resumed round's attestation chains to the session's previous one", + ); + assert_eq!(attest.session_id, sid); +} diff --git a/atomic-cli/src/commands/agent/provenance_rpc.rs b/atomic-cli/src/commands/agent/provenance_rpc.rs index 5370e8e3..ba0b67e8 100644 --- a/atomic-cli/src/commands/agent/provenance_rpc.rs +++ b/atomic-cli/src/commands/agent/provenance_rpc.rs @@ -458,6 +458,24 @@ fn journal_turn_status(turn: &pb::StoredProvenanceTurn) -> anyhow::Result JournalTurnStatus { provenance_id: turn.provenance_id.get(), generation: turn.generation, lifecycle, + checkpoint_change_hashes: turn + .checkpoint_attempt + .as_ref() + .map(|attempt| attempt.source.change_hashes.clone()) + .unwrap_or_default(), + final_provenance_hash: turn.final_hash, } }